Skip to content

Instantly share code, notes, and snippets.

@federicobucchi
Last active August 29, 2015 14:20
Show Gist options
  • Save federicobucchi/8ee05c8f6bcb47e439e4 to your computer and use it in GitHub Desktop.
Save federicobucchi/8ee05c8f6bcb47e439e4 to your computer and use it in GitHub Desktop.
Happy Number
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