
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Palindrome Numbers in JavaScript
We are required to write a JavaScript function that takes in a number and determines whether or not it is a palindrome number.
Palindrome numbers − A palindrome number is that number which reads the same from both left and right sides.
For example −
343 is a palindrome number
6789876 is a palindrome number
456764 is not a palindrome number
Example
The code for this will be −
const num1 = 343; const num2 = 6789876; const num3 = 456764; const isPalindrome = num => { let length = Math.floor(Math.log(num) / Math.log(10) +1); while(length > 0) { let last = Math.abs(num − Math.floor(num/10)*10); let first = Math.floor(num / Math.pow(10, length −1)); if(first != last){ return false; }; num −= Math.pow(10, length−1) * first ; num = Math.floor(num/10); length −= 2; }; return true; }; console.log(isPalindrome(num1)); console.log(isPalindrome(num2)); console.log(isPalindrome(num3));
Output
And the output in the console will be −
true true false
Advertisements