This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -92,6 +92,7 @@ import removeElementByIndex from './removeElementByIndex'
9292import clone from './clone'
9393import arrMultiply from './array-multiplier'
9494import second from './second'
95+ import armstrong from './armstrong'
9596
9697export {
9798 reverseArrayInPlace ,
@@ -188,4 +189,5 @@ export {
188189 clone ,
189190 arrMultiply ,
190191 second ,
192+ armstrong ,
191193}
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments