-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome-number.js
More file actions
43 lines (42 loc) · 1.04 KB
/
palindrome-number.js
File metadata and controls
43 lines (42 loc) · 1.04 KB
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
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function (x) {
// first attempt
// const s = x.toString();
// for (let i = 0; i < s.length / 2; i++) {
// if (s[i] !== s[s.length - i - 1]) {
// return false;
// }
// }
// return true;
// second attempt
let dup = x;
let rev = 0;
while (x > 0) {
console.log("xxxx", x);
// obtiene la parte entera del resto de dividir entre 10
// por ejemplo 121 % 10 = 1.
// lo que se busca es obtener el ultimo digito.
let res = x % 10;
// una vez obtenido el ultimo numero lo insertamos en el reverso
// x ej. 0 * 10 + 1 = 1;
rev = rev * 10 + res;
// x pasa a ser el mismo numero excepto el ultimo digito.
x = parseInt(x / 10);
// console.log("--------------------");
// console.log("rev", rev);
}
// first pass 121
// res = 1, rev = 1, x = 12
// second pass
// res = 2, rev = 12, x = 1
// third pass
// res = 1,rev = 121, x = 0
if (dup === rev) {
return true;
} else {
return false;
}
};