Happy Number
Start with a positive integer n and repeat one step: replace the number by the sum of the squares of its digits. For example, 82 becomes 8² + 2² = 68.
n is happy if this process eventually reaches 1 (after which it stays at 1, since 1² = 1). Otherwise the numbers fall into a loop that never contains 1.
Return true if n is happy and false otherwise.
Constraints
1 <= n <= 2^31 - 1
Examples
Example 1
Input: n = 19 Output: true
19 → 1² + 9² = 82 → 64 + 4 = 68 → 36 + 64 = 100 → 1. It reaches 1, so 19 is happy.
Example 2
Input: n = 4 Output: false
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4: the sequence loops without ever reaching 1.
Example 3
Input: n = 1 Output: true
1 is already 1.
JavaScript you'll use: Numbers, Math and integer tricks Map and Set
/**
* @param {number} n
* @return {boolean}
*/
function isHappy(n) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.