Tell us what’s happening:
I got everything to work but instead of having to write each individual letter in the for loop what could I have used instead? Also after I .toLowerCase() the word. It still seemed to have some uppcase letters which i then had to add:
|| str.charAt(i) == “P” as well as || str.charAt(i) == “M” or else I would be missing some letters in my array called newStr. Why is that?
Your code so far
function palindrome(str) {
var newStr = [];
lowStr = str.toLowerCase();
for (var i = 0; i < lowStr.length; i++) {
if (lowStr.charAt(i) ==
"a" || str.charAt(i) == "b" || str.charAt(i) == "c" || str.charAt(i) == "d" || str.charAt(i) ==
"e" || str.charAt(i) == "f" || str.charAt(i) == "g" || str.charAt(i) == "h" || str.charAt(i) ==
"i" || str.charAt(i) == "j" || str.charAt(i) == "k" || str.charAt(i) == "l" || str.charAt(i) ==
"m" || str.charAt(i) == "n" || str.charAt(i) == "o" || str.charAt(i) == "p" || str.charAt(i) ==
"q" || str.charAt(i) == "r" || str.charAt(i) == "s" || str.charAt(i) == "t" || str.charAt(i) ==
"u" || str.charAt(i) == "v" || str.charAt(i) == "w" || str.charAt(i) == "x" || str.charAt(i) ==
"y" || str.charAt(i) == "z" || str.charAt(i) == "1" || str.charAt(i) == "2" || str.charAt(i) ==
"3" || str.charAt(i) == "4" || str.charAt(i) == "5" || str.charAt(i) == "6" || str.charAt(i) ==
"7" || str.charAt(i) == "8" || str.charAt(i) == "9" || str.charAt(i) == "0" || str.charAt(i) ==
"P" || str.charAt(i) == "M") {
newStr.push(lowStr.charAt(i));
}
}
var joinedNewStr = newStr.join(""); //original str with non alphanumeric characters removed
var reverser = newStr.reverse(); //reverses newStr array
var joiner = reverser.join(""); //joins reversed newStr array back into a string
if (joiner == joinedNewStr) { //checks if original == reversed version
return true;
} else {
return false;
}
}
palindrome("My age is 0, 0 si ega ym.");
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/check-for-palindromes