Palindrome project feedback Steve

Palindrome project feedback Steve
0

#1

I would appreciate any feedback on my code for the Palindrome project. Is there a better way to do this?

function palindrome(str) {
let strClean=str.replace(/[^0-9a-z]/gi, ‘’);
let strLower=strClean.toLowerCase();
let strReverse=strLower.split("").reverse().join("");
return strLower==strReverse;
}


#2

Executing .split("").reverse().join("") on a string is a very common solution to this problem, but it isn’t the most efficient.

A more efficient method is to split the string to make an array, and then compare values at either end of the array. If the values don’t match you can return false immediately. If they match you can compare values that are one place closer to the array’s center. Stop comparing at the center and return true.


#3

Thank you… how about this?

let strClean=str.replace(/[^0-9a-z]/gi, '');
let strLower=strClean.toLowerCase();
let arrClean=strLower.split("");
for (var i = 0; i < arrClean.length; i++) {
	if (arrClean[i]!==arrClean.length-1-i) {return false;};
}

return true;
}