-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome-checker.js
38 lines (34 loc) · 1.09 KB
/
palindrome-checker.js
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
36
37
38
// My Solution
function palindrome(str) {
str = str.replace(/\W|_/g,"").toLowerCase()
let strCpy = ""+str
let str1 = strCpy.substring(0,Math.floor(str.length / 2))
let str2 = ""
if(str.length % 2 != 0){
str2 =
strCpy.substring(Math.floor(str.length / 2)+1,str.length)
.split('').reverse().join('')
}
else {
str2 =
strCpy.substring(Math.floor(str.length / 2),str.length)
.split('').reverse().join('')
}
return str1 == str2;
}
/*
Tests:
palindrome("eye") should return a boolean.
palindrome("eye") should return true.
palindrome("_eye") should return true.
palindrome("race car") should return true.
palindrome("not a palindrome") should return false.
palindrome("A man, a plan, a canal. Panama") should return true.
palindrome("never odd or even") should return true.
palindrome("nope") should return false.
palindrome("almostomla") should return false.
palindrome("My age is 0, 0 si ega ym.") should return true.
palindrome("1 eye for of 1 eye.") should return false.
palindrome("0_0 (: /-\ :) 0-0") should return true.
palindrome("five|\_/|four") should return false.
*/