DSK
DSK

Reputation: 13

If-else-statement returns undefined in palindrome function

In my palindrome function an if-else-statement returns undefined. Basically, I am trying to find the biggest palindromic number with three digits. For example with two digits: 99 * 91 = 9009.

var palindromic = function(n) {
    var save,
        result,
        counter = 900;

    var checker = function(string) {
        s = string.toString();

        if(!(s)) {
            return true;
        } else if(s[0] !== s[s.length - 1]) {
            return false;
        }
        checker(s.slice(1, -1));
    }

    var recursive = function() {
        result = counter * n;
        if(counter === n) {
            return;
        } else if(checker(result)) { // this line of code here, undefined.
            save = result;
        }
        counter++;
        recursive();
    }

    recursive();
    return save;
};

What is wrong? Any help is welcome!

Upvotes: 0

Views: 199

Answers (2)

user3589620
user3589620

Reputation:

A little spoiler and improving

You can do it without a recursion.

  1. Usage of a reverse function to reverse a string.
  2. Usage of two for loops, starting from the highest number.

function strReverse(str) {
    var reverse = "";
    for(var i = (str.length - 1); i >= 0; i -= 1) {
        reverse += str[i];
    }
    return reverse;
}

function palindromic(n) {
    var highestPalindrom = 0;
    // Start with the highest number! 
    for(var i = n; i > 1; i -= 1) {
        // Start also with the highest number!
        for(var j = n; j > 1; j -= 1) {
            // Get product
            var product = i * j;
            // Compare the string in reverse with the string itself
            // If it is true, then it is a palindrom!
            if(strReverse(product.toString()) === product.toString()) {
                highestPalindrom = product;
                // Break inner loop
                break;
            }
        }
        // Break outer loop
        break;
    }
    return highestPalindrom;
}

var hP = palindromic(99);
console.log(hP);

Upvotes: 0

11thdimension
11thdimension

Reputation: 10653

There are two problems in the code

  1. checker() should have return checker(s.slice(1,-1)); as last line.
  2. In recursive() when checker(result) is true recursive() should return.

Here's corrected code.

var palindromic = function (n) {
    var save,
        result, 
        counter = 900;

    var checker = function (s) {
        //s = string.toString();

        if ( !(s) ) {
            return true;
        } else if ( s[0] !== s[s.length-1] ) {
            return false;
        }
        return checker(s.slice(1,-1));
    }     

    var recursive = function () {
        result = counter * n;
        if ( counter === n ) {
            return;
        } else if ( checker(result + "") ) { // this line of code here, undefined.
            save = result;
            return;
        }
        counter++;
        recursive();
    } 

    recursive();
    return save;
};

Output:

palindromic(2)
2002
palindromic(3)
2772
palindromic(5)
5005
palindromic(6)
6006
palindromic(9)
8118
palindromic(23423)
188484881

Upvotes: 1

Related Questions