1.4 KiB
1.4 KiB
id | challengeType | title | forumTopicId |
---|---|---|---|
5900f3701000cf542c50fe83 | 5 | Problem 4: Largest palindrome product | 302065 |
Description
n
-digit numbers.
Instructions
Tests
tests:
- 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);
}