2

I have been trying to make an input that gets a array and checks if it is a palindrome number or not, by the locations of the numbers (with i = 0 and j = array.length).

I just don't know how can I continue from here and what is wrong with my code.

That is my function:

function isPalindrome() {
    var input;
    var array = input.split("");

    var i = 0;
    var j = array.length;

    while (i < j) {
        if (array[i] != array[j]) {
            return false;  
        } else {
            i++;
            j--;
        }
    }

    return true;
}

I created a fiddle.

tjati
  • 5,761
  • 4
  • 41
  • 56
Roni Litman
  • 923
  • 1
  • 8
  • 20
  • You're not using function parameters correctly. This is actually pretty common in people who start programming for the first time. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions – Benjamin Gruenbaum May 08 '15 at 14:11
  • 1
    you need to pass something into the function `isPalindrome(input){ // la la la }` – Pogrindis May 08 '15 at 14:12
  • 1
    why don't you reverse the string and check to see if the 2 strings are the same ? Have a look [here](http://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript) and pick your reverse implementation –  May 08 '15 at 14:16
  • As an optimisation to Edi's answer,when you take the reverse of the string and compare the two strings ,you need to check only till half of the string length – function May 08 '15 at 14:22
  • @user4517807 is that really an optimization though? Checking for equality of two strings is pretty efficient. – gin93r May 08 '15 at 14:53

6 Answers6

4

A few things. You didn't have a parameter for your isPalindrome method. You weren't passing the value to it either. You also can reverse the array and check it without doing extra loops. ie: 1221 reversed == 1221 (true) whereas 1223 reversed != 3221 (false)

See my fiddle.

$("#checkPalindrome").click(function () {
    // added .val() here so we get the value.
    var inp = $("#input").val();
    $("#result").html("("+isPalindrome(inp)+")");

});

// added param str
function isPalindrome(str) {
   // split the string into an array, reverse it, join it into a string again
   // then check if it's equal to the original string passed.
   return str.split('').reverse().join('') == str;
}
gin93r
  • 1,551
  • 4
  • 21
  • 39
1

allright so this took me a little bit longer than expected, but my go at this is like this :

$("#checkPalindrome").click(function () {
var number = $("#input").val();
$("#result").text(isPalindrome(number));

});
function isPalindrome(number) {
    return String(number) == String(number).split("").reverse().join("");
}

http://jsfiddle.net/qajzadby/11/ love me some 1 line functions >:) also fixed your number retrieval

Gerton
  • 676
  • 3
  • 14
  • 2
    I think `.val()` is just going to return a string since it's coming from an input. Probably no need to cast it as a string. Not that it's hurting anything. – gin93r May 08 '15 at 14:27
0

First of all, input in undefined. If it may be equal to some string, then looks like this line:

if (array[i] != array[j]) 

is the culprit, because j is equal to array.length initially. You should take array[j - 1].

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
0

There were a few things in your fiddle that needed fixing

  1. You're not reading the value of the field. Change this var input = $("#input") to var input = $("#input").val()

  2. You're not passing the value to the function. Change this:

    function isPalindrome() {
         var input;
    

    to

    function isPalindrome(input) {
    
  3. You set j equal to array.length, in order to read the last element in the array that should be array.length-1

Here's an updated, and working, fiddle: http://jsfiddle.net/qajzadby/10/

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Thanks, it worked. Can you tell me why do i need to use array.length-1 and not array.length? didn't really get the problem. for exmaple- 123321. the right "1" is array.length, isn't it? – Roni Litman May 08 '15 at 14:23
  • An array with values `1,2,3,3,2,1` has a length of 6. However, arrays are zero based so the last element has the index `5`. – Jamiec May 08 '15 at 14:25
0

Couple of things are going here...

1)You are not calling your function with some input eg->isPalindrome(myPalindromeNumber)

2)array.length will return the total number of elements in te array so you should be indexing as array[i]!=array[j-1]

function
  • 1,298
  • 1
  • 14
  • 41
  • 1
    Your last point is not true, OP original code works just as well on odd-length as even-length input – Jamiec May 08 '15 at 14:26
0
/* TODO: check if a number is a פילנדום on input's content change */
$("#checkPalindrome").click(function () {
    var input = $("#input").val();
    $("#result").text(isPalindrome(input));

});

function isPalindrome(input) {

    var array = input.split("");

    var i = 0;
    var j = array.length;

    for(i=0;i < j ;i++){
        if(array[i] != array[j-i-1]){
            return false;
       }
    }

    return true;
}  

DEMO

ozil
  • 6,930
  • 9
  • 33
  • 56