-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp004.js
More file actions
36 lines (32 loc) · 819 Bytes
/
p004.js
File metadata and controls
36 lines (32 loc) · 819 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
36
function isPalindrome(x) {
var str = x.toString();
for (var i = 0; i <= str.length/2; i++) {
if (str[i] != str[(str.length-i)-1]) return false;
}
return true;
}
function largestPalinOfDigits(x) {
var min = Math.pow(10, x-1);
var max = Math.pow(10, x)-1;
var curr = 0;
for (var i = max; i >= min; i--) {
for (var j = i; j >= min; j--) {
var prod = i*j;
if (prod > curr && isPalindrome(prod)) curr = prod;
}
}
return curr;
}
test("Palindrom tests", function() {
ok(isPalindrome(1));
ok(isPalindrome(11));
ok(isPalindrome(101));
ok(isPalindrome(1001));
ok(isPalindrome(111));
ok(!isPalindrome(102));
ok(!isPalindrome(201));
});
test("Largest palindome tests", function() {
equal(largestPalinOfDigits(2), 9009);
equal(largestPalinOfDigits(3), 906609);
});