My Algorithm for this challenge
- delete all non-alphanumeric characters
- parse the resulting string to lowercase
- copy string in reverse format, compare with string from step 2, if both are identical, return true.
My code keep returning this
palindrome("0_0 (: /-\ :) 0-0")
should return true.
My code so far
function palindrome(str) {
//CODE BLOCK II |filter characters
for (let cnt = 0; cnt < str.length; cnt++) {
//def regex to test presence of non-alphanumeric characters, replace with void ''
if (/\W+/.test(str[cnt]) == true) {
str = str.replace(str[cnt], '');
}
//def regex to test presence of space characters, replace with void ''
if (/\s/.test(str[cnt]) == true) {
str = str.replace(str[cnt], '');
}
//reject underscores
if (/_/.test(str[cnt]) == true) {
str = str.replace(str[cnt], '');
}
}
//CODE BLOCK II | parse characters to same case
str = str.toLowerCase();
//loop to check characters form index 0
let cnt;
let cntIArr = [];
let cntJArr = [];
//loop to check characters form last index
for (cnt = 0; cnt < str.length; cnt++) {
cntIArr.push(str[cnt]);
cntJArr.unshift(str[cnt]);
}
//CODE BLOCK III | comparison
let iArr = '';
let jArr = '';
for (let cnt = 0; cnt < str.length; cnt++) {
//merge string
jArr += cntJArr[cnt];
iArr += cntIArr[cnt];
}
return (iArr === jArr) ?
true : false;
}
Challenge: Palindrome Checker
Link to the challenge: