2

My objective is to test if a word (under 10 letters) that a user enters is a palindrome. I want to do this my comparing the first letter to the last, 2nd letter to the 2nd to last, 3rd letter to the 3rd to last...

I am using a for loop and an array to do this. I cannot use the reverse() method. My main issue is formatting the comparison equation that I have:

(lettersArray[i] + 1) == (lettersArray[i].length - 1)

This is supposed to compare the first to last, 2nd and 2nd to last, and so on. Is this the right format? Am I right in my method of accessing the last index in the array and counting it down? Please let me kow what I am doing wrong since it's not running. Here is my code:

var usersWord = prompt("Enter a Palindrome");

var lettersArray =usersWord.split(""); // this is the array

for(var i=0; lettersArray.length < 11; i++) {

    if((letters[i] + 1) == (lettersArray.length[i]-1)) {
        alert("is palindrome");
    } //end if statement

    else{
        alert("is not palindrome");
    } // end else statement

 } // end for statement
  • More checks for palindrom in this question: http://stackoverflow.com/questions/14813369/palindrome-check-in-javascript/14813569#14813569 – dfsq Mar 01 '15 at 19:16
  • 1
    Why are you checking `lettersArray.length < 11`? – dfsq Mar 01 '15 at 19:17
  • 1. You only need to check the first half of the string and compare it with the second half. 2. You need to check all those letters until they either do not match or you run out of characters – Ed Heal Mar 01 '15 at 19:18
  • I was checking lettersArray.length < 11 because there can only be 10 characters in the array. i realized it should be i < lettersArray.length after I posted the questions. Thanks for pointing that out. –  Mar 01 '15 at 19:23
  • You only need to check the first half against the second half. i.e. i < lettersArray.length/2 – Ed Heal Mar 01 '15 at 19:26
  • Now that you've split the letters, you can just invoke `.reverse().join('')` and compare for equality. `x = "racecar"; x.split('').reverse().join('') == x // true` – user229044 Mar 01 '15 at 19:35
  • @dfsq: more than enough, I'd say. – georg Mar 01 '15 at 19:35

1 Answers1

2

The issue with your code is that you are reporting results after checking each character; however, we can't tell whether a word is a palindrome or not without checking every character. A function such as the following might better suit your requirements.

function checkPalindrome(word) {    
    var len = word.length;
    for (var i = 0; i < (len / 2); i++) {
        if (word.charAt(i) !== word.charAt(len - 1 - i))
             return false;
    }
    return true;
}

Another important point to note is that you should be checking until word.length / 2, not an arbitrary number 11 that may change depending on the word used as an input. Note also that word.length / 2 is an optimized case. The loop could also have been run till the word length but there is no need to check again that last char == first char when already first char == last char

manan
  • 1,385
  • 13
  • 23
  • Thanks Manan! I've known of this solution, I was just trying to see if my way of comparing 1st & last, 2nd to 2nd last (and so on) is feasible. –  Mar 01 '15 at 19:25
  • I am also not familiar with the charAt() method, so I was trying to find a way around it. –  Mar 01 '15 at 19:26
  • I don't think I'd have called the length variable l -- it's too easy to misread. Other than that, well written answer +1. – Software Engineer Mar 01 '15 at 19:27