Last active
August 29, 2015 14:20
-
-
Save federicobucchi/8ee05c8f6bcb47e439e4 to your computer and use it in GitHub Desktop.
Happy Number
This file contains 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
var isHappy = function(n) { | |
var arr = n.toString().split(''); | |
var times = 0; | |
var exp; | |
var scan = function(arr) { | |
exp = 0; | |
times += 1; | |
if (times > 10) { | |
return false; | |
} | |
for (var i = 0; i < arr.length; i++) { | |
exp += Math.pow(arr[i], 2); | |
} | |
if (exp === 1) { | |
return true; | |
} else { | |
arr = exp.toString().split(''); | |
return scan(arr); | |
} | |
} | |
return scan(arr); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment