Skip to content

Instantly share code, notes, and snippets.

@casprwang
Created October 22, 2017 01:53
Show Gist options
  • Save casprwang/647e21b26495d042dde2dd6ed103b174 to your computer and use it in GitHub Desktop.
Save casprwang/647e21b26495d042dde2dd6ed103b174 to your computer and use it in GitHub Desktop.
classic closure miss consideration
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment