Why is it almost there - but not there yet?

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");

arr1[index] and arr2[index] are always undefined so undefined === undefined is always true

the first parameter of the callback is always the array element

the index is the second parameter

check the documentation

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");

move this below the next line and check the value

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.