I'm writing a code in order to know if a word is a Palindrome. I think my code is correct but I get undefined on "newStr". Like the console tells me that : console.log(newStr) // undefinedracerar I don't understand why and I bet that why I get "false" for my return because I think the code is correct. Here my code below. Thanks
function palindrome(str) {
// console.log(str);
str = str.split(" ").join("").toLowerCase();
// console.log(str);
// console.log(str.length);
let newStr = "";
for (let i = str.length; i >= 0; i--) {
newStr = newStr + str[i];
}
console.log(newStr);
if (str === newStr) {
return true;
} else return false;
}
console.log(palindrome("rarecar"));