Check for Palindromes any suggestions?

Check for Palindromes any suggestions?
0.0 0

#1

Tell us what’s happening:

I don’t know whats wrong with this code;

Your code so far

function palindrome(str) 
{
  // Good luck!
  var arr=[];
  
  str=str.replace(" ","");
  str=str.replace(/\W/,"");
  arr=str.split("");
  arr=arr.reverse();
  var n=arr.length/2;
   var len=arr.length;
  
 
  for(var i=0;i==n;i++)
  {
   if(n==i)
   {
     if(arr[i]===arr[n])
     {
       return true;
     }
     return false;
   } 
    else if(arr[i]!==arr[len-1])
    {
      return false;
    }
    else
      len--;
    
  }
  return true;
}



palindrome("eye");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:


#2

There are several issues here. First your loop condition is i==n, the loop will be executed while this condition is true, since from the start it is never true it will also never execute and move straight to the “return true” after the loop. Second, you have a lot of return statements in your for loop, that will end the function immediately when the return is hit. Have you really done all the work by the time you hit any of those returns?
Other than that a lot of your if statements also do not make very much sense to me, such as if (n == i) this is your loop condition, at this point you’ve done nothing which would change that condition and you will not be in the loop unless it’s true. Then you nest an if (arr[i] === arr[n] given that you only get to this point if n == i how could the same index in the same array hold a value that’s different?
I’ve pointed out some oddities in your code, you need to fix those issues and work more on the logic. If you need more help feel free to ask.
This part is on the right track though:

if(arr[i]!==arr[len-1])
    {
      return false;
    }
    else
      len--;

#3

Thanx @Logiar. Error was, in for loop condition (i==n) and also in str.replace(regex,"");