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

Commit

Permalink
feat(armstrong): add 'armstrong' option (#244)
Browse files Browse the repository at this point in the history
* feat(armstrong): add 'armstrong' option

* Update index.js

* Update index.js
  • Loading branch information
yeohsoonkeat authored and Kent C. Dodds committed Feb 13, 2019
1 parent d00b122 commit c8ee1a3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/armstrong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default armstrong

/**
* This method will check if a number is an armstrong number.
* @param {Number} num number to check
* @return{Boolean} True or false
*/

function armstrong(num) {
let eachDigit = 0
let check = 0
let digit = 0
for (let i = num; i > 0; i = Math.floor(i / 10)) {
digit = digit + 1
}
for (let i = num; i > 0; i = Math.floor(i / 10)) {
eachDigit = i % 10
check = check + Math.pow(eachDigit, digit)
}
if (check === num) {
return true
} else {
return false
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import removeElementByIndex from './removeElementByIndex'
import clone from './clone'
import arrMultiply from './array-multiplier'
import second from './second'
import armstrong from './armstrong'

export {
reverseArrayInPlace,
Expand Down Expand Up @@ -188,4 +189,5 @@ export {
clone,
arrMultiply,
second,
armstrong,
}
16 changes: 16 additions & 0 deletions test/armstrong.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import test from 'ava'
import {armstrong} from '../src'

test('This number is not an armstrong number', t => {
const object = 123
const expected = false
const actual = armstrong(object)
t.deepEqual(actual, expected)
})

test('This number is an armstrong number', t => {
const object = 371
const expected = true
const actual = armstrong(object)
t.deepEqual(actual, expected)
})

0 comments on commit c8ee1a3

Please sign in to comment.