The test passes anything that should be false.
However, if an input is supposed to be true i.e. it is a Palindrome, the function does not do this?
I believe .replace(/[\W_]/g,"");
removes anything non-alphanumerical and white spaces. Is this correct?
By default, the function should return true. As stated in the penultimate line of code after both for loops:
return true
It turns false correctly for any input that is not palindrome.
The for loop, if every character at position i equals the character at position j , then the if statement inside will not execute. As a result, the function returns true as default.
However, this is not happening? Could someone please provide a pointer in the correct direction. Thanks
function palindrome(str) {
var lcStr = str.toLowerCase()
var nonANstr = lcStr.replace(/[\W_]/g,"");
for (var i = 0 ; i<=nonANstr.length;i++) {
for (var j=nonANstr.length ; j>=0 ; j--) {
if (nonANstr[i]!=nonANstr[j]){
console.log(nonANstr[i])
console.log(nonANstr[j])
return false;
}
}
}
return true;
}
palindrome("eYYe_y");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker/