Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Palindrome Challenge - Syntax correct, why is the output returning false?
    Hi, I am a newbie working on the easy challenges. I have been working on the Palindrome challenge. I have verified the syntax is correct, but the output is returning false regardless what input I use. I have gone through the code several times, and the order seems logical to me. I have looked at example codes, but they seem to take different approaches and I want to see if my code is workable for this solution. Any advice on where I am going wrong would be appreciated. Example: var str = "eye" str === reverseWord should return true. function Palindrome(str) { var reverseLetter = str.split('').reverse().join(''); var reverseWord = null; for (var i = 0; i < str.length; i++) { reverseWord += str[i]; } if (str === reverseWord) { return true; } else { return false; } }
    shamineau posted this question on 6/24/14 | javascript, palindrome
    Answers
  • +
  • 3
  • -
  • Hey. Adding letters to null looks like the problem, change null to empty quotes.
  • +
  • 3
  • -
  • Also, you declare the var reverseLetter but you never use it again to make any comparisons, by the looks of your code reverseWord is never changed, you just make a direct copy of str with another name.
  • +
  • 2
  • -
  • There is no need for you to have one both reverseLetter and reverseWord. You have the reversed word in your reverseLetter line. After you have that, it is a simple comparitive to see if their the same here is a working version of your code function workingPalindrome(str){ var reverseWord = str.split('').reverse().join(''); if(str == reverseWord){ return true }else{ return false } }
  • +
  • 0
  • -
  • Also you don't appear to be taking into accout spaces between your words/letters. You need to negate the spaces, too.
    Log in to write an answer.