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
7
  • More checks for palindrom in this question: stackoverflow.com/questions/14813369/… Commented Mar 1, 2015 at 19:16
  • 1
    Why are you checking lettersArray.length < 11? Commented Mar 1, 2015 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 Commented Mar 1, 2015 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. Commented Mar 1, 2015 at 19:23
  • You only need to check the first half against the second half. i.e. i < lettersArray.length/2 Commented Mar 1, 2015 at 19:26

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
I am also not familiar with the charAt() method, so I was trying to find a way around it.
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.