Help with palindrome checker

I am getting false as a value but when I enter it in the exercise it says:
palindrome(“not a palindrome”) should return false.
palindrome(“nope”) should return false.
palindrome(“almostomla”) should return false.
palindrome(“1 eye for of 1 eye.”) should return false.
palindrome(“five|_/|four”) should return false.

Here is my code:

function palindrome(str) {
	let pttr = /\w/i;
  	let newStr = [];
  	str = str.split('');
	
  	for( let i = 0; i < str.length; i++ ){
  		if( pttr.test(str[i]) && str[i] !== '_' ){
  			newStr.push(str[i]);
  		}
  	}

  	let orderedArr = newStr.join("").toLowerCase();
  	let reverseArr = newStr.sort((a, b) => b - a ).join("").toLowerCase();

  	if( orderedArr === reverseArr ){
  		return true;
  	}

  	else {
  		return false;
  	}
}

console.log( palindrome("not a palindrome") ); //false
console.log( palindrome("nope") ); //false
console.log( palindrome("almostomla") ); //false
console.log( palindrome("1 eye for of 1 eye.") ); //false
console.log( palindrome("five|\_/|four") ); //false

What is it am I missing?

try adding a console.log({orderedArr, reverseArr}) and see if it is what you expect

I did and it shows the expected results. But I’m still getting that it should return false.

Hey @deanwantspie,

is there any particular reason you decided to name an array as newStr and then strings as orderedArr and reverseArr. This is a bit funny I have to say :slight_smile:

On a serious note, why do you think that sorted string (assuming your sort method works) would be reversed? While it’s true for "abc" it won’t work with "abba", hope you understand this.