-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome-Checker.js
More file actions
25 lines (18 loc) · 1.04 KB
/
Palindrome-Checker.js
File metadata and controls
25 lines (18 loc) · 1.04 KB
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
/*
Palindrome Checker
Return true if the given string is a palindrome. Otherwise, return false.
A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
Note: You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.
We'll pass strings with varying formats, such as racecar, RaceCar, and race CAR among others.
We'll also pass strings with special symbols, such as 2A3*3a2, 2A3 3a2, and 2_A3*3#A2.
*/
function palindrome(str) {
let transformed = str.replace(/[^a-z0-9]/gi, '').toLowerCase();
let reversed = [...transformed].reverse().join("");
if(transformed.length % 2 == 0 ) {
return transformed.substr(0, (transformed.length / 2)) === reversed.substr(0, (transformed.length / 2)) ;
} else {
return transformed.substr(0, ((transformed.length + 1)/ 2)) === reversed.substr(0, ((transformed.length + 1) / 2));
}
}
palindrome("eye");