Function palindrome

I have been trying this question for a while, I am not sure what is wrong with my code.
Odd number array with return true could not passed…

Shall I just go for split. reverse. join function then compare the two strings, instead of comparing arrays…

function palindrome(str) {
  // Good luck!

  str = str.replace(/[^a-zA-Z 0-9]+/g,'');
  str = str.toLowerCase();
  var arrNum = str.length;
  // make it int not flort
  var middlePoint = Math.floor (arrNum /2);
  var j = 0;

  while( j < middlePoint) 
    if (str [j] == str [arrNum -1 - j])
          //if array is odd number
          if (j == (arrNum -1-j))
               return true;
         return false;
  return true;


You are 99.99% there. Just remove the space in your regular expression (line #1).


OH!!! thank you so much! I really need to be trained to be super careful - it to
ok me about 3 hours :joy:

I have had problems where I spent days trying to figure out why my logic was only to find a typo. It is part of the joy of programming.

Good job on solving the challenge with a very efficient algorithm!:grinning:

1 Like

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

Thanks a million . I woundered how to post codes :):slight_smile: