-
Notifications
You must be signed in to change notification settings - Fork 21
/
PalindromeNumber.js
51 lines (43 loc) · 791 Bytes
/
PalindromeNumber.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @param {number} x
* @return {boolean}
*/
let isPalindrome = function (x) {
if (x < 10 && x >= 0) {
return true;
}
if (x % 10 === 0) {
return false;
}
let y = 0;
while (x > y) {
y = y * 10 + x % 10;
x = parseInt(x / 10);
}
return x === y || x === parseInt(y / 10);
};
if (!isPalindrome(-1)) {
console.log("pass")
} else {
console.error("failed")
}
if (isPalindrome(1)) {
console.log("pass")
} else {
console.error("failed")
}
if (isPalindrome(121)) {
console.log("pass")
} else {
console.error("failed")
}
if (isPalindrome(1001)) {
console.log("pass")
} else {
console.error("failed")
}
if (!isPalindrome(1000021)) {
console.log("pass")
} else {
console.error("failed")
}