Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-4-largest-palindrome-product.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

1.6 KiB
Raw Blame History

id, challengeType, title, forumTopicId
id challengeType title forumTopicId
5900f3701000cf542c50fe83 5 Problem 4: Largest palindrome product 302065

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.

Instructions

Tests

tests:
  - text: <code>largestPalindromeProduct(2)</code> should return a number.
    testString: assert(typeof largestPalindromeProduct(2) === 'number');
  - text: <code>largestPalindromeProduct(2)</code> should return 9009.
    testString: assert.strictEqual(largestPalindromeProduct(2), 9009);
  - text: <code>largestPalindromeProduct(3)</code> should return 906609.
    testString: assert.strictEqual(largestPalindromeProduct(3), 906609);

Challenge Seed

function largestPalindromeProduct(n) {
  // Good luck!
  return true;
}

largestPalindromeProduct(3);

Solution

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);
}