Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit c8ee1a3

Browse files
yeohsoonkeatKent C. Dodds
authored and
Kent C. Dodds
committed
feat(armstrong): add 'armstrong' option (#244)
* feat(armstrong): add 'armstrong' option * Update index.js * Update index.js
1 parent d00b122 commit c8ee1a3

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/armstrong.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default armstrong
2+
3+
/**
4+
* This method will check if a number is an armstrong number.
5+
* @param {Number} num number to check
6+
* @return{Boolean} True or false
7+
*/
8+
9+
function armstrong(num) {
10+
let eachDigit = 0
11+
let check = 0
12+
let digit = 0
13+
for (let i = num; i > 0; i = Math.floor(i / 10)) {
14+
digit = digit + 1
15+
}
16+
for (let i = num; i > 0; i = Math.floor(i / 10)) {
17+
eachDigit = i % 10
18+
check = check + Math.pow(eachDigit, digit)
19+
}
20+
if (check === num) {
21+
return true
22+
} else {
23+
return false
24+
}
25+
}

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import removeElementByIndex from './removeElementByIndex'
9292
import clone from './clone'
9393
import arrMultiply from './array-multiplier'
9494
import second from './second'
95+
import armstrong from './armstrong'
9596

9697
export {
9798
reverseArrayInPlace,
@@ -188,4 +189,5 @@ export {
188189
clone,
189190
arrMultiply,
190191
second,
192+
armstrong,
191193
}

test/armstrong.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import test from 'ava'
2+
import {armstrong} from '../src'
3+
4+
test('This number is not an armstrong number', t => {
5+
const object = 123
6+
const expected = false
7+
const actual = armstrong(object)
8+
t.deepEqual(actual, expected)
9+
})
10+
11+
test('This number is an armstrong number', t => {
12+
const object = 371
13+
const expected = true
14+
const actual = armstrong(object)
15+
t.deepEqual(actual, expected)
16+
})

0 commit comments

Comments
 (0)