
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Counting steps to make number palindrome in JavaScript
Problem
We are required to write a JavaScript function that takes in a number, num, as the first and the only argument.
Our function should return the number of special steps needed to obtain a palindrome. The special step is: "reverse the digits, and add to the original number". If the resulting number is not a palindrome, repeat the procedure with the sum until the resulting number is a palindrome.
For example, if the input to the function is −
Input
const num = 87;
Output
const output = 4;
Output Explanation
Because the steps involved are −
87 + 78 = 165; 165 + 561 = 726; 726 + 627 = 1353; 1353 + 3531 = 4884
Example
Following is the code −
const num = 87; const countSteps = (num) => { let res = 0; while (!isPalindrome(num)) { res++ num += +('' + num).split``.reverse().join`` }; return res; } const isPalindrome = num => { let i = 0 let str = '' + num while (i++ <= str.length / 2) { if (str[i] !== str[str.length - 1 - i]) return false }; return true } console.log(countSteps(num));
Output
4
- Related Articles
- Minimum Insertion Steps to Make a String Palindrome in C++
- Counting number of 9s encountered while counting up to n in JavaScript
- Number of letters in the counting JavaScript
- Minimum number of deletions to make a string palindrome in C++.
- Finding minimum steps to make array elements equal in JavaScript
- Minimum Number of Steps to Make Two Strings Anagram in C++
- Minimum number of Appends needed to make a string palindrome in C++
- Counting number of words in a sentence in JavaScript
- Counting divisors of a number using JavaScript
- Find minimum number of merge operations to make an array palindrome in C++
- Verification if a number is Palindrome in JavaScript
- Program to check minimum number of characters needed to make string palindrome in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Counting the number of 1s upto n in JavaScript
- Counting number of vowels in a string with JavaScript

Advertisements