-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfourth.js
More file actions
33 lines (25 loc) · 770 Bytes
/
fourth.js
File metadata and controls
33 lines (25 loc) · 770 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
const palindromeCheck = (num) => {
let stringNum = num.toString();
stringNum = stringNum.split("");
stringNum = stringNum.map((num) => parseInt(num));
let reverseNum = [];
for(let i = 1; i <= stringNum.length; i++) {
reverseNum.push(stringNum[stringNum.length - i]);
}
if(JSON.stringify(stringNum) === JSON.stringify(reverseNum)) {
return true;
}
return false;
}
const largestPalindromeProduct = (n) => {
let palindromes = [];
for(let i = 1; i <= 10**n; i++) {
for(let j = 1; j <= 10**n; j++) {
if(palindromeCheck(i*j)) {
palindromes.push(i*j);
}
}
}
return palindromes[palindromes.length - 1];
}
console.log(largestPalindromeProduct(3));