freeCodeCamp/curriculum/challenges/portuguese/10-coding-interview-prep/project-euler/problem-4-largest-palindrom...

1.3 KiB
Raw Blame History

id title challengeType forumTopicId dashedName
5900f3701000cf542c50fe83 Problem 4: Largest palindrome product 5 302065 problem-4-largest-palindrome-product

--description--

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.

--hints--

largestPalindromeProduct(2) should return a number.

assert(typeof largestPalindromeProduct(2) === 'number');

largestPalindromeProduct(2) should return 9009.

assert.strictEqual(largestPalindromeProduct(2), 9009);

largestPalindromeProduct(3) should return 906609.

assert.strictEqual(largestPalindromeProduct(3), 906609);

--seed--

--seed-contents--

function largestPalindromeProduct(n) {

  return true;
}

largestPalindromeProduct(3);

--solutions--

const largestPalindromeProduct = (digit)=>{
  let start = 1;
  let end = Number(`1e${digit}`) - 1;
 let palindrome = [];
  for(let i=start;i<=end;i++){
    for(let j=start;j<=end;j++){
      let product = i*j;
      let palindromeRegex = /\b(\d)(\d?)(\d?).?\3\2\1\b/gi;
      palindromeRegex.test(product) && palindrome.push(product);
    }
 }
 return Math.max(...palindrome);
}