/**
* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 Ă— 99.
*
* Find the largest palindrome made from the product of two 3-digit numbers.
*/
// 999
// 99
// 999
// 999 * 999 = 998001
// 998899
const MAX = 999;
const MIN = 100;
const MAX_PRODUCT = Math.pow(MAX, 2);
const MIN_PRODUCT = Math.pow(MIN, 2);
const findFactor = (num) => {
let n = MAX;
let factor;
do {
if (!(num % n) && (Number(num / n).toString(10).length === 3)) {
factor = n;
}
n--;
} while (!factor && (n > MIN));
return factor;
};
const reverse = (str) => {
return str.split("").reverse().join("");
}
const isPalindrome = (n) => {
let numAsString = Number(n).toString(10);
return numAsString === reverse(numAsString);
};
const main = limit => {
let maxPalindrome;
let n = (limit - 1);
do {
const palindromeNumber = isPalindrome(n);
if (palindromeNumber) {
const factor = findFactor(n);
if (factor) {
maxPalindrome = n;
}
}
n--;
} while (!maxPalindrome && (n > MIN_PRODUCT));
return maxPalindrome;
};
console.log(main(MAX_PRODUCT));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.