-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckForPalindromes.js
More file actions
26 lines (24 loc) · 875 Bytes
/
checkForPalindromes.js
File metadata and controls
26 lines (24 loc) · 875 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
/* 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. */
function palindrome(str) {
str = str.replace(/[^0-9a-z]/gi, '');
str = str.toLowerCase();
array = str.split('');
for (i = 0; i < array.length; i++) {
if (i - (array.length - 1 - i) == 0) {
return true;
}
else if (i - (array.length - 1 - i) == 1) {
if (array[i] == array[(array.length - 1) - i]) {
return true;
}
else if (array[i] != array[(array.length - 1) - i]) {
return false;
}
}
else if (array[i] != array[(array.length - 1) - i]) {
return false;
}
}
}
palindrome("eeyyee");