-
Notifications
You must be signed in to change notification settings - Fork 1
/
004-check-for-palindromes.js
42 lines (35 loc) · 1.02 KB
/
004-check-for-palindromes.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
39
40
41
42
/**
* Check for Palindromes
*/
const palindrome = ( str ) =>{
// Assign a front and back pointer.
let front = 0;
let back = str.length - 1;
// Back and pront pointers won't always meet
// in the middle, so use ( back > front )
while ( back > front ) {
// Increments front pointer if current
// character doesn't meet criteria.
while ( str[front].match(/[\W_]/)){
front++;
continue;
}
// Decrements back pointer if current character doesn't meet
// criteria.
while (str[back].match(/[\W_]/)){
back--;
continue;
}
// finally do the comparison on the current character.
if ( str[front].toLowerCase() !== str[back].toLowerCase()){
return false;
}
front++;
back--;
}
// If the whole string has been compared
// whithout returning false, it's a palindrime.
return true;
};
console.log(palindrome('eye'))
console.log(palindrome('eyes'));