forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path202 Happy Number.js
More file actions
38 lines (30 loc) · 711 Bytes
/
Copy path202 Happy Number.js
File metadata and controls
38 lines (30 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Leetcode #202
// Language: Javascript
// Problem: https://leetcode.com/problems/happy-number/
// Author: Chihung Yu
/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function(n) {
if(n === null){
return false;
}
var val = n;
var hash = {};
while(!hash[val]){
if(val === 1){
return true;
}
hash[val] = true;
var sn = val + '';
var sarr = sn.split('');
var total = 0;
for(var i = 0; i < sarr.length; i++){
si = parseInt(sarr[i]);
total += Math.pow(si,2);
}
val = total;
}
return false;
};