-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem4.js
More file actions
35 lines (27 loc) · 955 Bytes
/
problem4.js
File metadata and controls
35 lines (27 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Largest palindrome product
// Problem 4
// 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 3-digit numbers.
(function iife() {
console.log('Problem 4')
function numberIsPalindrome(num) {
const reverseNum = Number(String(num).split('').reverse().join(''))
return num === reverseNum
}
function calculateLargestPalindrome() {
const palindromeArray = []
let largestPalindrome = 0
for (var i = 999; i > 99; i--) {
for (var j = 999; j > 99; j--) {
var sum = i * j
if (numberIsPalindrome(sum) && sum > largestPalindrome) {
largestPalindrome = sum
}
}
}
return largestPalindrome
}
console.log('Largest palindrome product:', calculateLargestPalindrome())
})()
// Answer: 906609