
- 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
Finding persistence of number in JavaScript
We are required to write a JavaScript function that takes in an positive integer and returns its additive persistence
The additive persistence of an integer, say n, is the number of times we have to replace the number with the sum of its digits until the number becomes a single digit integer.
For example −
If the number is −
1679583
Then,
1 + 6 + 7 + 9 + 5 + 8 + 3 = 39 // 1 Pass 3 + 9 = 12 // 2 Pass 1 + 2 = 3 // 3 Pass
Therefore, the output should be 3.
Example
Following is the code −
const num = 1679583; const sumDigit = (num, sum = 0) => { if(num){ return sumDigit(Math.floor(num / 10), sum + num % 10); }; return sum; }; const persistence = num => { num = Math.abs(num); let res = 0; while(num > 9){ num = sumDigit(num); res++; }; return res; }; console.log(persistence(num));
Output
Following is the output in the console −
3
- Related Articles
- Finding product of Number digits in JavaScript
- Finding Gapful number in JavaScript
- Finding place value of a number in JavaScript
- Finding number of spaces in a string JavaScript
- Finding nearest Gapful number in JavaScript
- Finding the number of words in a string JavaScript
- Finding whether a number is triangular number in JavaScript
- Finding number plate based on registration number in JavaScript
- Finding unlike number in an array - JavaScript
- Finding closed loops in a number - JavaScript
- Finding smallest number using recursion in JavaScript
- Finding the smallest fitting number in JavaScript
- Finding the nth prime number in JavaScript
- Finding deviations in two Number arrays in JavaScript
- Finding Number of Days Between Two Dates JavaScript

Advertisements