3

There seems to be a problem with this code as in it doesn't check for the largest palindrome. I mixed it with the example they showed of largest palindrome from two digit numbers (The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99) and it worked. I've looked around, but I still don't understand exactly why my code doesn't show the final product. If someone is willing to explain instead of just giving the answer, that would be quite helpful

for(var i = 100; i < 1000; i++) {
  for(var j = 100; j < 1000; j++) {
    var total = String(i*j);
    var regularI = total.substring(0, Math.floor(total.length/2));
    var regularJ = total.substring(total.length/2,total.length);
    var reversedJ = regularJ.split("").reverse().join("");

    if(regularI === reversedJ) {
       console.log("SUCCESS\nTotal: " + (i*j) + "\nI: " + i + "\nJ: " + j);
    }
  }
}
  • 1
    Totally unrelated to your question, but since you have your `var`s inside your for loops, take a look at this about [javascript closures](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1) – Josef Engelfrost Nov 11 '15 at 02:01

1 Answers1

4
993 * 913 = 906609
995 * 583 = 580085

Suppose the first number in the multiplication corresponds to i, and the second to j. In your nested for-loop with i and j, the result of the former multiplication will be output before the latter. You can see this if you look at the end of your code's output. Therefore, the final success that is output is not guaranteed to be the largest palindrome. It's possible that the largest palindrome appears earlier in your output.

One straightforward method of fixing this problem is to keep a list of palindromes you've found in an array. After the nested for-loop completes, you can sort the array by number value and get the largest number by looking at the last position:

var palindromes = [];

for(var i = 100; i < 1000; i++) {
  for(var j = 100; j < 1000; j++) {
    var total = String(i*j);
    var regularI = total.substring(0, Math.floor(total.length/2));
    var regularJ = total.substring(total.length/2,total.length);
    var reversedJ = regularJ.split("").reverse().join("");

    if(regularI === reversedJ) {
      palindromes.push(Number(total));
    }
  }
}

// sorts the array in-place
// "default sort order is according to string Unicode code points" - Mozilla docs
// so we'll need to pass in a comparison function.
palindromes.sort(function(a, b) {
  return a - b;
});
console.log(palindromes[palindromes.length-1]);

If you do this, you'll see that the largest palindrome is indeed 906609, a number that appeared third-to-last (and once more, earlier) in your nested for-loop.

Another way of solving this would be to dedicate a variable, say maxSoFar, to keeping track of the highest palindrome value you've seen.

There are other ways of solving this problem, but I'll leave that up to you to discover.

Shashank
  • 13,713
  • 5
  • 37
  • 63
  • 1
    CoderJS: **Bottomline**: the last found palindrome is not necessarily the largest one. Nice job, @Shashank. – Shomz Nov 11 '15 at 01:01