File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments