Skip to content

Commit b6c24d1

Browse files
authored
Create 1134-armstrong-number.js
1 parent d042d85 commit b6c24d1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

1134-armstrong-number.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
const isArmstrong = function(n) {
6+
//number of digits in N
7+
let k = ~~(Math.log10(n) + 1);
8+
//temporary variable (so we dont modify N)
9+
let x = n;
10+
//to hold sum
11+
let sum = 0;
12+
//get each digit
13+
while (x !== 0) {
14+
//add this digit^k to sum
15+
sum += Math.pow(x % 10, k);
16+
//get next digit
17+
x = ~~(x/10);
18+
}
19+
return sum == n;
20+
};

0 commit comments

Comments
 (0)