Palindrome not working for almostomla

Tell us what’s happening:
Hy all,

Hope I’m not messing up this first post. I got the palindrome challenge (you can find it at the link below for ease) and the code lower in this message fails to check all challenges. Tested the steps one by one and all should be ok.

Can anyone explain to me why the challenges are not met? And where does that error come from?

Challenges not ok:
// running tests
palindrome(“almostomla”) should return false.
palindrome(“1 eye for of 1 eye.”) should return false.
Cannot read property ‘length’ of null
// tests completed

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker/

Your code so far


function palindrome(str) 
{
  str = str.toLowerCase();
  str = str.match(/[A-Za-z]/gi);
  for (var i = 0; i<(str.length-(str.length % 2))/2 ; i++)
  { 
    if (str[i] == str[str.length-1-i])
    {
      return true;
    } 
    else
    {
      return false
    }
  }
  // Good luck!
}



palindrome("almostomla");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker/

Found where the error comes from. Need help with the other 2 challenges.

Read more and managed to fix the challenge for “palindrome(“1 eye for of 1 eye.”) should return false.”

New code is lower.

For the “Almostomla” challenge, still doesn’t work.

function palindrome(str)
{
str = str.toLowerCase();
str = str.match(/[A-Za-z0-9]/gi);
if (str == null)
{
return true;
}
else
{
for (var i = 0; i<(str.length-(str.length % 2))/2 ; i++)
{
if (str[i] == str[str.length-1-i])
{
console.log(true);
return true;
}
else
{
console.log(false);
return false;
}
}
}
// Good luck
}

palindrome(“1 eye for of 1 eye.”);

Found it. Fixed :slight_smile: Seems i still have to ask to find the answers on my own :)))

1 Like