Categories
Leetcode Recursion

Happy Number

let isHappy = function(n) {
let visited = {};

function sumOfSquare(num) {
if (visited[num]) return false;
visited[num] = true;

let sum = 0;
while (num > 0) {
let d = num % 10; //?
num = Math.floor(num / 10); //?
sum += d * d;
}

if (sum === 1) {
return true;
}
else {
return sumOfSquare(sum);
}
}

return sumOfSquare(n);
};

Leave a Reply

Your email address will not be published. Required fields are marked *