Skip to content

Commit eb4b323

Browse files
added a palindrome.js file
1 parent 7cb96c3 commit eb4b323

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
let n = 12321;
2+
3+
if(palindrome(n)){
4+
console.log( n + " is a Palindrome number");
5+
}else{
6+
console.log( n + " is not a Palindrome number");
7+
}
8+
9+
/* function to check if a number is palindrome or not
10+
by reversing the number and comparing it with the original number */
11+
function palindrome(n){
12+
// create a temp variable to store the value of n
13+
let temp = n;
14+
// create a variable to store the reverse of n
15+
let rev = 0;
16+
// reverse the number
17+
while(temp > 0){
18+
let lastDigit = temp % 10;
19+
rev = rev * 10 + lastDigit;
20+
temp = Math.floor(temp / 10);
21+
}
22+
// check if the reverse is equal to n
23+
return rev == n;
24+
}
25+
26+
// using two pointers
27+
function palindromeTwoPointer(n){
28+
// convert the number to string
29+
let str = n.toString();
30+
// create two pointers
31+
let start = 0;
32+
let end = str.length - 1;
33+
// loop through the string
34+
while(start < end){
35+
// check if the characters at the two pointers are equal
36+
if(str[start] != str[end]){
37+
return false;
38+
}
39+
// increment start and decrement end
40+
start++;
41+
end--;
42+
}
43+
return true;
44+
}

0 commit comments

Comments
 (0)