-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem004.js
36 lines (30 loc) · 931 Bytes
/
problem004.js
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
36
function problem004() {
let largestPalindrome = 0;
let largestPalindromeA = 0;
let largestPalindromeB = 0;
let a = 999;
while (a >= 100) {
b = 999;
while (b >= a) {
if (a * b <= largestPalindrome) {
break; // Since a*b is always going to be too small
}
if (isPalindrome(a * b)) {
largestPalindrome = a * b;
largestPalindromeA = a;
largestPalindromeB = b;
}
b--;
}
a--;
}
return [largestPalindrome, largestPalindromeA, largestPalindromeB];
}
function isPalindrome(numberValidate) {
let strNumber = numberValidate.toString();
let re = /[\W_]/g;
let lowRegStr = strNumber.toLowerCase().replace(re, '');
let reverseStr = lowRegStr.split('').reverse().join('');
return reverseStr === lowRegStr;
}
module.exports = problem004;