With the following code I'm checking if str
is a palindrome.
(A palindrome is a word or sentence that's spelled the same way both forward and backward).
Even tho they are the same, the function is still giving me a false return... Why is that?
I know I could join the arrays to single strings with .join('')
but I still would like to know whats wrong with my code and why it's not working
function palindrome(str) {
let newStr = str.toUpperCase().replace(/[^A-Z\d]/g, '').split('');
let newStrRev = [...newStr].reverse(); //.reverse() is not destructive anymore
console.log(typeof newStr, newStr); //OUTPUT: object [ 'R', 'A', 'C', 'E', 'C', 'A', 'R' ]
console.log(typeof newStrRev, newStrRev); //OUTPUT: object [ 'R', 'A', 'C', 'E', 'C', 'A', 'R' ]
if (newStr === newStrRev) {
return true
} else {
return false
}
}
console.log(palindrome("race car")); //OUTPUT: false