-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathisPalindrome.js
More file actions
27 lines (22 loc) · 862 Bytes
/
isPalindrome.js
File metadata and controls
27 lines (22 loc) · 862 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
const isPalindrome = (word, cb) => {
const arr = word.toLowerCase().split(''),
validCharacters = 'abcdefghijklmnopqrstuvwxyz'.split('');
var wordArr = [];
wordArr = arr.filter((val) => {
if (validCharacters.includes(val)) return val;
});
var length = wordArr.length;
for(let index1=0, index2=length-1; index1 < (length/2); index1++, index2--) {
if (wordArr[index1] == ' ' || validCharacters.includes(wordArr[index1]) == false ) {
index1++;
} else if (wordArr[index2] == ' ' || validCharacters.includes(wordArr[index2]) == false) {
index2--;
}
if (wordArr[index1] != wordArr[index2]) return cb(false);
}
cb(true);
};
isPalindrome("Madam I'm Adam", (isPalindrome) => {
if (isPalindrome == true) console.log('True');
else console.log('False');
});