Iterated digits squaring
Solutions
Solution 1 (Click to Show/Hide)
function iteratedSquare(n) {
const squaredN = n
.toString()
.split('')
.reduce((sum, digit) => sum + parseInt(digit) ** 2, 0);
return squaredN === 1 || squaredN === 89 ?
squaredN :
iteratedSquare(squaredN)
}
Solution 2 (Click to Show/Hide)
function iteratedSquare(n) {
let squaredN = n;
while (squaredN !== 1 && squaredN !== 89) {
squaredN = squaredN
.toString()
.split('')
.reduce((sum, digit) => sum + parseInt(digit) ** 2, 0);
}
return squaredN;
}