Created
October 22, 2017 01:53
-
-
Save casprwang/647e21b26495d042dde2dd6ed103b174 to your computer and use it in GitHub Desktop.
classic closure miss consideration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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