[Feedback] Check for Palindromes

Hi, I completed the Check for Palindromes challenge, but still feel like my code could be better.

Do I need to store everything in variables or could I of just removed the “var whatever =” and ran the code by itself in the line?

Any feedback on my code would be great! Thanks!

`function palindrome(str) {
// lower cases and condenses str
var lowCase = str.toLowerCase();
var noSpace = lowCase.replace(/[\W_]/g, “”);
//make into arrays for comparing
var arrStr = noSpace.split("");
var revStr = arrStr.reverse().join("");

if (noSpace === revStr) {
return true;
} else {
return false;

Well, I answer like as student, like you, so maybe another helps better next.
You can short safe into two lines:

str=str.toLowerCase().replace(/[\W_]/g, “”);

As I tried, through tests fits even a one line code:

return(str.toLowerCase().replace(/[\W_]/g, “”)==str.split(’’).reverse().join(’’));

But i am not sure if it is safe, in all JS compilators and browsers .

1 Like

This block is equivalent to

return noSpace === revStr;

You’re code looks really good. Man, it’s is waaaaaay cleaner than mine. I got a long way to go it seems.

Here is mine, it’s ugly, but it works:


    function palindrome(str) {
      // Good luck!
      var res = str.toLowerCase();
      var arr = res.split("");
      var newA = [];
      var rev = [];
      for(var i = 0; i < arr.length; i++){
      	if( /[a-z]/i.test(arr[i]) || /\d/i.test(arr[i]) === true){

    if (rev.toString() === newA.toString()){
    	console.log('Yeah, baby!');
    	return true;
    } else

    return false;