Largest palindrome product

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 n-digit numbers.

largestPalindromeProduct(2) should return 9009.

largestPalindromeProduct(3) should return 906609.

Solution

Solution 1

function largestPalindromeProduct(n) {
  let smallestNDigitNumber = Math.pow(10,n-1);
  let largestNDigitNumber = Math.pow(10,n)-1;
  let largest = 0;

  for(let i=largestNDigitNumber;i>=smallestNDigitNumber;i--){
   for(let j=largestNDigitNumber;j>=smallestNDigitNumber;j--){
     
     if(largest >i*j) break;

     if(isPalindrome(i*j)&&(largest <i*j)){
       largest =i*j;
     }
    } 
  }
  return largest;
}

function isPalindrome(n){
  let ch = n.toString();
  return ch === ch.split("").reverse().join("");
}