Check for Palindromes - clean up

I solved this exercise quickly, but I am confident this wasn’t the fastest or cleanest way to do it. Could anyone offer any advice or material to read? I have very little experience in coding - but for these algorithm challenges I feel like playing the game “Human Resource Machine” has educated me more than some of the articles on Mozilla that are linked. I am reading through “Learn JavaScript and jQuery” by Duckett and “JavaScript: The Good Parts” by Crockford.

function palindrome(str) {
  var noSpace = str.replace(/\s+/gi, "");
  var noCaps = noSpace.toLowerCase();
  var outString = noCaps.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "");
  var array = [];
  array = outString.split("");
  var joinedA = "";
  joinedA = array.join("");
  if (outString == joinedA){
      return true;
  } else {
    return false;


If you intend to replace anything that’s not an alphanumeric character, you can use /[^a-z0-9]/gi. With this regex you’ll only need to do a .replace() once.

You don’t have to set array to [], as it gets overwritten by outString.split('') anyway. You can use

var array = outString.split('');

or even

var array = outputString.split('').reverse();

Same with joinedA. You didn’t have to assign it first an empty string, as it gets immediately overwritten by array.join('').

This is equivalent to

return outString == joinedA;

That said, if you have very little coding experience as you said, your solution isn’t bad. If you understood what you did then you’re doing fine. You’ll improve over time :thumbsup:


(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Thank you for the info!