Guest User

Untitled

a guest
Oct 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. const isHappy = function(n) {
  2. let set = new Set()
  3. let temp = calculate(n)
  4. if(set.has(temp)) return false
  5.  
  6. if (temp === 1) return true
  7.  
  8. set.add(temp)
  9. return isHappy(temp)
  10. }
  11.  
  12. const calculate = n =>
  13. [...n.toString().replace(/^0+/, '')].reduce((acc, e) => {
  14. return acc + Math.pow(parseInt(e), 2)
  15. }, 0)
  16.  
  17. // 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