Palindrome Checker J.S

Hey guys I’m new to this platform so first I’d like to say hi, also I don’t know why my code for the palindrome checker doesn’t work.

function palindrome(str) {
  var letters = /^[a-zA-Z]+$/;
  if (str.value.match(letters))
  {
    var reversestr = splitString.reverse();
    if (reversestr === str)
    {
      return true;
    }

  }
  else 
  {
    return false;
  }
  return false;
}

Have you copy pasted chunks of this code from different sources? If not, I apologize, but there are several issues and the code doesn’t really make sense, none of the bits of it fit together:

  • your regex matches only if the string is one or more letters. But, for example, “A man, a plan, a canal: Panama” is a valid palindrome - you should ignore casing, punctuation and spaces, you should not flat out disregard strings that have any of these things.
  • to compare properly, you need to make sure the string has no spaces or punctuation, and is all the same case. So a regex will be needed, but not used like you have (you will want to replace).
  • you have a pointless return false after the other return false: if the condition is false, then it will execute whatever is in the else block, and as that is return false nothing else can happen after that.
  • what is str.value?
  • what is splitStr?
  • reverse is something you do to arrays, which implies reversestr is an array. An array is not equal to a string, so reversestr == str will always be false.

DanCouper is right, there are several syntax and logic errors with your solution. It will need to be mostly rewritten. Second of all, welcome to FCC, It looks like you joined and immediately tried the palindrome check.

I would recommend starting from the beginning of the javascript certification, going through all the challenges first. The palindrome check is probably the easiest of the javascript projects, so you will definitely want a strong base in javascript and programming logic before you attempt them.

Good luck!

Thanks so much @DanCouper and @gtheginger maybe I need to take my time, this seemed a lot easier than it actually was I guess.

My logic was:

  1. test weather characters in string are in the alphabet.
  2. if yes test weather the reverse of the string is equal to the string. and that about it. Thank you guys! Also thank you for the warm welcome.
1 Like
  1. You don’t need to check this. Remove punctuation & spaces: if there’s nothing left after that then sure, return false.
  2. Because you’re not doing 1, and you’re not ignoring the case, this won’t work (“Hannah” is a palindrome, but if you just naïvely reverse and check if it’s the same, it will return false)

Echoing what @qtheginger says — I think you’re missing quite a bit of knowledge here. Amongst several other issues, just to reiterate: str.value isn’t a thing, splitStr doesn’t exist in your code, and you’ve missed a step off reversing a string

absolutely your right thank you for your help.

2 Likes