Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})