[Feedback] Check for Palindromes

[Feedback] Check for Palindromes
0

#1

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;
}
}`


#2

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, “”);
return(str===str.split(’’).reverse().join(’’));

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 .


#3

This block is equivalent to

return noSpace === revStr;

#4

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:

// THIS FUNCTION TESTS STRINGS FOR PALINDROMES

    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){
      		newA.push(arr[i]);
      		rev.push(arr[i]);
      	}
      }
    	
    	rev.reverse();

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

    return false;

    }