Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Checking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript
We are required to write a JavaScript function that takes in a number, sums its digits and checks whether that sum is a Palindrome number or not. The function should return true if the sum is Palindrome, false otherwise.
For example, if the number is 697, then the sum of its digits will be 6 + 9 + 7 = 22, which indeed, is a Palindrome number. Therefore, our function should return true for 697.
Understanding the Problem
A palindrome number reads the same forwards and backwards. For example: 11, 22, 121, 1331 are palindromes. Our task is to:
- Calculate the sum of all digits in a number
- Check if that sum forms a palindrome
- Return true if palindrome, false otherwise
Step-by-Step Solution
We'll create two helper functions: one to sum digits and another to check if a number is a palindrome.
const num = 697;
// Function to calculate sum of digits
const sumDigit = (num, sum = 0) => {
if (num) {
return sumDigit(Math.floor(num / 10), sum + (num % 10));
}
return sum;
};
// Function to check if a number is palindrome
const isPalindrome = num => {
const reversed = +String(num)
.split("")
.reverse()
.join("");
return reversed === num;
};
// Main function to check if sum of digits forms palindrome
const isSumPalindrome = num => isPalindrome(sumDigit(num));
console.log("Number:", num);
console.log("Sum of digits:", sumDigit(num));
console.log("Is sum a palindrome?", isSumPalindrome(num));
Number: 697 Sum of digits: 22 Is sum a palindrome? true
Testing with Multiple Examples
Let's test our function with different numbers to verify it works correctly:
const testNumbers = [697, 123, 456, 191, 999];
testNumbers.forEach(num => {
const digitSum = sumDigit(num);
const result = isSumPalindrome(num);
console.log(`${num} -> sum: ${digitSum} -> palindrome: ${result}`);
});
697 -> sum: 22 -> palindrome: true 123 -> sum: 6 -> palindrome: true 456 -> sum: 15 -> palindrome: false 191 -> sum: 11 -> palindrome: true 999 -> sum: 27 -> palindrome: false
How It Works
The sumDigit function uses recursion to extract each digit using modulo (%) and integer division. The isPalindrome function converts the number to string, reverses it, and compares with the original number.
Alternative Iterative Approach
For those who prefer iterative solutions, here's an alternative implementation:
function sumDigitsIterative(num) {
let sum = 0;
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
return sum;
}
function isPalindromeIterative(num) {
const str = num.toString();
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) {
return false;
}
left++;
right--;
}
return true;
}
const checkSumPalindrome = num => isPalindromeIterative(sumDigitsIterative(num));
console.log("Using iterative approach:");
console.log("697 ->", checkSumPalindrome(697));
console.log("456 ->", checkSumPalindrome(456));
Using iterative approach: 697 -> true 456 -> false
Conclusion
This solution efficiently calculates the sum of digits and checks if it forms a palindrome. Both recursive and iterative approaches work well, with the recursive version being more concise and the iterative version being more explicit in its logic.
