-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromeNumber.js
More file actions
43 lines (26 loc) · 897 Bytes
/
palindromeNumber.js
File metadata and controls
43 lines (26 loc) · 897 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
37
38
39
40
// 9. Check for Palindrome Number
// using string reversal
function isPalindrome(num) {
let numStr = num.toString();
let result = numStr.split('').reverse().join('');
return numStr === result;
}
console.log(isPalindrome(12231)); // true
// using array every()
function isPalindromeArray(number){
let numStr = number.toString();
let numArr = numStr.split('');
return numArr.every((num, index) => num === numArr[numArr.length - 1 -index]);
}
console.log(isPalindromeArray(124321))
// using XOR operator
function palindromeCheckUsingBitOperation(number){
let numStr = number.toString();
let length = numStr.length;
let result= 0;
for(let i = 0; i < Math.floor(length / 2); i++){
result ^= numStr.charCodeAt(i) ^ numStr.charCodeAt(length - 1 -i);
}
return result === 0;
}
console.log(palindromeCheckUsingBitOperation(12345321))