Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const isHappy = function(n) {
- let set = new Set()
- let temp = calculate(n)
- if(set.has(temp)) return false
- if (temp === 1) return true
- set.add(temp)
- return isHappy(temp)
- }
- const calculate = n =>
- [...n.toString().replace(/^0+/, '')].reduce((acc, e) => {
- return acc + Math.pow(parseInt(e), 2)
- }, 0)
- // that won't work with an error of Maximum call stack reached, cause I was letting new Set all the time.
Add Comment
Please, Sign In to add comment