-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidPalindrome.js
More file actions
35 lines (26 loc) · 877 Bytes
/
ValidPalindrome.js
File metadata and controls
35 lines (26 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
*/
/**
* @param {string} s
* @return {boolean}
*/
// Mostly used regex for this
var isPalindrome = function(s) {
let cleaned = s.replace(/[^a-zA-Z0-9]/g, '');
let noSpace = cleaned.replace(/\s/g, '');
let down = noSpace.toLowerCase()
let reverse = []
for (let i = down.length; i >= 0; i--) {
let char = down.charAt(i)
reverse.push(char)
}
let final = reverse.join('')
if (final === down) {
return true;
} else {
return false
}
};
console.log(isPalindrome("A man a plan a canal: panama"))