Hey, I’m solving the palindrome problem and the every function is not working as I thought it would. I displayed the arrays and they are both right. The thing is that the return is giving true to nope when comparing the arr1 [n,o,p,e] and the arr2[e,p,o,n].
Could anyone help me understand why the every function acts like this and what should I change so that I can solve it using the every()?
function palindrome(str) {
//clean up the array
var regex = /[^a-zA-Z]/g;
//check if reverse array equals to str array after cleaning up
var arr1 = str.replace(regex,'').toLowerCase().split('');
console.log(arr1);
var arr2 = arr1.reverse();
console.log(arr2);
return arr1.every((index) => arr2[index] === arr1[index]);
}
palindrome("nope");
Thanks!
I changed it but it still shows true to “nope”
function palindrome(str) {
//clean up the array
var regex = /[^a-zA-Z]/g;
//check if reverse array equals to str array after cleaning up
var arr1 = str.replace(regex,'').toLowerCase().split('');
//console.log(arr1);
var arr2 = arr1.reverse();
//console.log(arr2);
return arr1.every((element, index) => element === arr2[index]);
}
palindrome("nope");