1

As an exercise, I'm trying to create a function that returns the palindromic numbers resulting from multiplying three-digit numbers. As far as I can tell, the function is running through numbers correctly, however, the resulting array is incorrect. I don't need the solution to the palindrome problem...just an idea of what I might be missing. Have I run into some limitation?

var palindromic = function() {
var a = [];
var res = [];
for (var i = 100; i < 1000; i++) {
    a.push(i);
}
var ar = a.slice(0);
a.map(function(x) {
    for (var j = 0; j < ar.length; j++) {
        var result = x * ar[j];
        if (result.toString() ===   result.toString().split("").reverse().join("")) {
            res.push(result);
        }
    }
  })
return res;
};
1
  • I just noticed that when I change the function to multiply all numbers between 0 and 99 instead of 100 and 999 it works properly. ?? Commented Oct 24, 2013 at 22:52

1 Answer 1

1

Pretty sure it's just trying to call console.log() 810,000 times. If you comment the console.log line, it works just fine.

var palindromic = function() {
var a = [];
var res = [];
for (var i = 100; i < 1000; i++) {
    a.push(i);
}
var ar = a.slice(0);
a.map(function(x) {
    for (var j = 0; j < ar.length; j++) {
        var result = x * ar[j];
        //console.log(x + " : " + ar[j] + ' = ' + result);
        if (result.toString() ===   result.toString().split("").reverse().join("")) {
            res.push(result);
        }
    }
});
return res;
};

console.log(palindromic());
Sign up to request clarification or add additional context in comments.

5 Comments

That was just to see if it was looping properly. The highest number in the array ends up being 99,999, however, the numbers should go into the 100,000s.
From this fiddle, I'm getting 906,609 as the largest. Array.map may not traverse in-order, so res has no guarantee of being ordered.
Similar to Tom, if I run this code changing the initial array definition to between 900 to < 1000, I get the following numbers, all in the 100,000s: 819918, 824428, 819918, 906609, 824428, 886688, 861168, 888888, 861168, 888888, 886688, 906609
It worked for me as well when I changed the array definition. Do you know why that is? The 100 to < 1000 version, even when sorted, doesn't contain numbers larger than 99,999.
I'm confused on that assertion - the fiddle clearly logs 906,609 as the largest, which I believe is the correct answer to the question "what is the largest palindrome number less than 1,000,000". How are you compiling this? Node, browser, website?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.