- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Verification if a number is Palindrome in JavaScript
Let’s say, we have to write a function that takes in a Number and returns a boolean based on the fact whether or not the number is palindrome. One restriction is that we have to do this without converting the number into a string or any other data type.
Palindrome numbers are those numbers which read the same from both backward and forward.
For example −
121 343 12321
Therefore, let’s write the code for this function −
Example
const isPalindrome = (num) => { // Finding the appropriate factor to extract the first digit let factor = 1; while (num / factor >= 10){ factor *= 10; } while (num) { let first = Math.floor(num / factor); let last = num % 10; // If first and last digit not same return false if (first != last){ return false; } // Removing the first and last digit from number num = Math.floor((num % factor) / 10); // Reducing factor by a factor of 2 as 2 digits are dropped factor = factor / 100; } return true; }; console.log(isPalindrome(123241)); console.log(isPalindrome(12321)); console.log(isPalindrome(145232541)); console.log(isPalindrome(1231));
Output
The output in the console will be −
false true true false
- Related Articles
- Check if a number is Palindrome in C++
- Check if a number is Palindrome in PL/SQLs
- Check if binary representation of a number is palindrome in Python
- Bash program to check if the Number is a Palindrome?
- Palindrome in Python: How to check a number is palindrome?
- JavaScript - Find if string is a palindrome (Check for punctuation)
- Check whether sum of digit of a number is Palindrome - JavaScript
- Python program to check if a given string is number Palindrome
- Check if number is palindrome or not in Octal in Python
- Checking if a string can be made palindrome in JavaScript
- Write a C# program to check if a number is Palindrome or not
- Counting steps to make number palindrome in JavaScript
- Recursive program to check if number is palindrome or not in C++
- Finding the nth palindrome number amongst whole numbers in JavaScript
- Checking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript

Advertisements