This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(armstrong): add 'armstrong' option (#244)
* feat(armstrong): add 'armstrong' option * Update index.js * Update index.js
- Loading branch information
1 parent
d00b122
commit c8ee1a3
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |