May 25th, 2024
What's missing in this code?
function additivePersistence(num) {
let n = num;
let count = 0;
while (n >= 10) {
n = n
.toString()
.
.reduce((acc, digit) => acc +
, 0);
;
}
return count;
}
// How many iterations to reach a single-digit number?
// additivePersistence(1579583) -> 3
// 1. 1 + 5 + 7 + 9 + 5 + 8 + 3 = 38
// 2. 3 + 8 = 11
// 3. 1 + 1 = 3
// additivePersistence(123456) -> 2
// 1 + 2 + 3 + 4 + 5 + 6 = 21
// 2 + 1 = 3
// additivePersistence(7) -> 0
// (no iteration needed for 7)
Type or select from these options