Tell us what’s happening:
For “two (2) n-digit numbers” using 3 for “n” means starting with 999 * 999. Working down you will never get the answer required of “906609” and here is why:
999 x 909 = 908091
999 x 908 = 907092
999 x 907 = 906093
999 x 906 = 905094
999 x 905 = 904095
The first time a number begins with “906” is 999 * 907 = 906093.
Could someone tell me how anyone gets the correct answer to this?
Your code so far
function largestPalindromeProduct(n) {
if(n > 4 || n < 2){
console.log("Please enter a number from 2 to 4");
return 0;
}
let sum = 0;
let sumString = "";
let rightSumString = "";
let leftSumString = "";
let sumStringArr = [];
const numberArray = [99,999,9999];
for(let x = numberArray[n-2]; x > 1; x--){
for(let y = numberArray[n-2];y > 1; y--){
sum = x * y;
sumString = sum.toString();
sumStringArr = sumString.split("");
let lengthCount = (sumStringArr.length / 2) - 1;
console.log(x + " x " + y + " = " + sum);
for(let z = 0; z < lengthCount;z++){
leftSumString += sumStringArr[z];
rightSumString += sumStringArr.pop();
}
if(leftSumString === rightSumString){
return sum;
}
leftSumString = "";
rightSumString = "";
sumStringArr = [];
}
}
}
let result = largestPalindromeProduct(3);
console.log(result);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Project Euler Problems 1 to 100 - Problem 4: Largest palindrome product