Check for Palindromes- Help me improve solution!

Tell us what’s happening:
I got everything to work but instead of having to write each individual letter in the for loop what could I have used instead? Also after I .toLowerCase() the word. It still seemed to have some uppcase letters which i then had to add:
|| str.charAt(i) == “P” as well as || str.charAt(i) == “M” or else I would be missing some letters in my array called newStr. Why is that?

Your code so far

function palindrome(str) {

  var newStr = [];
  
  lowStr = str.toLowerCase();
  for (var i = 0; i < lowStr.length; i++) {
    if (lowStr.charAt(i) == 
        "a" || str.charAt(i) == "b" || str.charAt(i) == "c" || str.charAt(i) == "d" || str.charAt(i) ==
        "e" || str.charAt(i) == "f" || str.charAt(i) == "g" || str.charAt(i) == "h" || str.charAt(i) ==
        "i" || str.charAt(i) == "j" || str.charAt(i) == "k" || str.charAt(i) == "l" || str.charAt(i) ==
        "m" || str.charAt(i) == "n" || str.charAt(i) == "o" || str.charAt(i) == "p" || str.charAt(i) ==
        "q" || str.charAt(i) == "r" || str.charAt(i) == "s" || str.charAt(i) == "t" || str.charAt(i) ==
        "u" || str.charAt(i) == "v" || str.charAt(i) == "w" || str.charAt(i) == "x" || str.charAt(i) ==
        "y" || str.charAt(i) == "z" || str.charAt(i) == "1" || str.charAt(i) == "2" || str.charAt(i) ==
        "3" || str.charAt(i) == "4" || str.charAt(i) == "5" || str.charAt(i) == "6" || str.charAt(i) ==
        "7" || str.charAt(i) == "8" || str.charAt(i) == "9" || str.charAt(i) == "0" || str.charAt(i) == 
        "P" || str.charAt(i) == "M") {
      newStr.push(lowStr.charAt(i));
    }
  }
  
  var joinedNewStr = newStr.join("");         //original str with non alphanumeric characters removed
  var reverser = newStr.reverse();            //reverses newStr array
  var joiner = reverser.join("");             //joins reversed newStr array back into a string
  
  
  if (joiner == joinedNewStr) {               //checks if original == reversed version
    return true;
  } else {
    return false;
  }
}

palindrome("My age is 0, 0 si ega ym.");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/check-for-palindromes

Well, for starters, you could look at using regular expressions to remove all non-letter characters from your code.

There are also some improvements you can make to make it work faster when you’re doing the actual comparison, but just figuring out the RegEx thing will cut a whooooole lot of lines out of your solution.

Ask if you need more help with the RegEx, but for now, I’ll just leave you to research it on your own :wink:

My solution not using regEx was the opposite of yours. I removed the illegal chars (chars not being checked) instead!

Spoiler alert:
https://jsfiddle.net/JohnnyBizzel/1LnLkopp/

But RegEx is the best way.

I have blurred your solution by putting spoiler tags ([spoiler] and [/spoiler]) on the line before and after your code, to avoid spoiling a complete solution for other campers who may come across your solution via the forum search.

I actually used RegEx initially and tried to exclude non-word characters with \W but i couldn’t figure out how to also exclude underscores ("_")

I had something like:

lowStr.replace(/\W/g, “”)

If you are just posting code which is not a full challenge solution, there is no need to blur the code.