Easy to read palindrome checker I made a palindrome checker that's supposed to be designed to be simple and easy to read. Please let me know what you think. I believe the time complexity is \$\mathcal{O}(n)\$ but I'm not too sure about that:

Challenge: You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.

function reverseString(str)
{ 
  str = str.replace(/[^\w\s]|_/g, "").toLowerCase().split(" ").join("");
  var array = [];
  for(var i = str.length ; i &gt;=0; i--)
  {
    array.push(str[i])
  }
  return(array.join(""));
}


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

function palindrome(str) {
  str = str.replace(/[^\w\s]|_/g, "").toLowerCase().split(" ").join("");
  if(str === reverseString(str))
  {
   return true; 
  }
  else
  {
    return false;
  }
}
Time complexity

Your time complexity is linear but you can save a few traversals over the string and lower the constant factor as you improve readability. Checking whether a string is a palindrome can be done in one pass with two pointers at each end of the string (plus some conditionals for your special characters), but this gains speed at the expense of readability; I'd encourage a round of clean-up before going for optimizations.

Repeated code

Repeated code harms maintainability and readability. Notice that the line

str.replace(/[^\w\s]|_/g, "").toLowerCase().split(" ").join("");


appears in two places in the code. If you decide to change one to accept a different regex but forget to change the other one, you've introduced a potentially subtle bug into your program. Move this to its own function to avoid duplication.

Use accurate function names and use builtins

reverseString is a confusing function: it does more than reversing a string as advertised: it also strips whitespace and punctuation, which would be very surprising if I called this function as a user of your library without knowing its internals. All functions should operate as black boxes that perform the task they claim to, nothing more or less.

The array prototype already has a reverse() function, so there's no need to write this out by hand.

Avoid unnecessary verbosity

The code:

  if(str === reverseString(str))
  {
   return true; 
  }
  else
  {
    return false;
  }


is clearer written as return str === reverseString(str);, which says "return the logical result of comparing str and its reversal".

Improve the regex to match your specification

Including spaces in your regex substitution to "" is easier than .split(" ").join(""). If you wish to remove all non-alphanumeric characters, a regex like /[^a-z\d]/gi reflects your written specification accurately (or use \W if you don't mind including underscores).

Style remarks


JS uses K&amp;R braces instead of Allman by convention.
Add a blank line above for and if blocks to ease vertical congestion.
Add a space around keywords and operators like for( and &gt;=0, which are clearer as for ( and &gt;= 0.
No need for parentheses around a return value.
array.push(str[i]) is missing a semicolon.
CodeReview's snippet autoformatter will automatically do most of this for you.


Rewrite 1



const palindrome = str =&gt; {
  str = str.replace(/[^a-z\d]/gi, "").toLowerCase();
  return str === str.split("").reverse().join("");
};

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




Rewrite 2: uglier, but faster

Benchmark



const palindrome = str =&gt; {
  str = str.replace(/[^a-z\d]/gi, "").toLowerCase();
  let left = 0;
  let right = str.length;

  while (left &lt; right) {
    if (str[left++] !== str[--right]) {
      return false;
    }
  }

  return true;
};

[
  "",
  "a",
  "aba",
  "racecar",
  "racecar ",
  " racecar",
  " race car",
  " r  r a c e   c a  rr     ",
  ".a .. r . ... . .{9f08e988-1e35-4dc6-a24a-5c7e03bce5ba}$ $!ace ca r3  a",
].forEach(test =&gt; console.log(palindrome(test)));

console.log();
[
  "ab",
  "abc",
  "racecars",
  "racescar",
  " ra scecar",
  " r   sace car",
  "a r  r a c e   c a  rr     ",
  " r  r a c e   c a  rr     a",
  ".a .. r . ... . .$$$ $!aces ca r  a",
].forEach(test =&gt; console.log(palindrome(test)));