Skip to content

Commit 4586222

Browse files
authored
Update Additive Persistence
1 parent 9358187 commit 4586222

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

Additive Persistence

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,11 @@
2424
* 4) Return number of loops as answer *
2525
* *
2626
***************************************************************************************/
27-
28-
function AdditivePersistence(num) {
29-
30-
var sum, loop = 0;
31-
var val1 = num.toString(10).split("").map(function(t){return parseInt(t)});
32-
33-
while (val1.length > 1) {
34-
sum = 0;
35-
val1.forEach( function(number) {
36-
sum = sum + number;
37-
});
38-
39-
val1 = sum.toString(10).split("").map(function(t){return parseInt(t)});
40-
loop++;
41-
}
42-
43-
return loop;
44-
27+
function additivePersistence(num) {
28+
let count = 0;
29+
while (num >= 10) {
30+
num = num.toString().split('').reduce((sum, digit) => sum + parseInt(digit), 0);
31+
count++;
32+
}
33+
return count;
4534
}

0 commit comments

Comments
 (0)