Project Euler Problems 1 to 100 - Problem 4: Largest palindrome product

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

Nothing is saying that 906609 has to be a multiple of 999.

I see that, but it states “two n-digit numbers” so with 3 digit numbers 999 is the largest so please tell me what 2-3 digit numbers you multiplied to get the correct answer?

906609 = 913 * 993

OK. Thanks! Now I want to understand this, so if I reverse the loops (start at 1) I would get that answer by comparing each palindrome number with the previous until the largest is found. Does that sound right?
Because I worked the loops from 999 down so I would get the wrong answer because I wouldn’t get to 993 in the outer loop before an answer was found. Does my thinking sound right to you? I will try it to see. Also, there is a bug. If the number is “n” you can include an if statement to send back the answer required and it will pass you!

For example at the return:

if(leftSumString === rightSumString){         
          if(n === 3){
            return 906609;
          }
          return sum;
        }

You should have the program check for that.

You can, but that means you are wasting your time.

If you are thinking a nested loop, that’s probably going to be too slow of an approach.

@JeremyLT You’re right about the nested loop approach. I think I know now how to get the correct answer without using nested loops.