We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c61789b commit 955dc86Copy full SHA for 955dc86
javascript/202-Happy-Number.js
@@ -0,0 +1,25 @@
1
+function isHappy(n) {
2
+ const visit = new Set();
3
+
4
+ while (!visit.has(n)) {
5
+ visit.add(n);
6
+ n = sumOfSquares(n);
7
8
+ if (n == 1) return true;
9
+ }
10
11
+ return false;
12
+}
13
14
+function sumOfSquares(n) {
15
+ let output = 0;
16
17
+ while (n) {
18
+ let digit = n % 10;
19
+ digit = digit ** 2;
20
+ output += digit;
21
+ n = Math.floor(n / 10);
22
23
24
+ return output;
25
0 commit comments