This repository was archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
956c1b7
commit c3a61db
Showing
3 changed files
with
33 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,22 @@ | ||
export default convertToRoman | ||
|
||
/** | ||
*Original Source: https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript | ||
*This method will convert a number into a roman numeral | ||
* @param {Number} num - number to convert | ||
* @return {String} - roman numeral of the number | ||
*/ | ||
|
||
function convertToRoman(num) { | ||
const arr1 = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] | ||
const arr2 = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'] | ||
let roman = '' | ||
for (let i = 0; i < arr1.length; i++) { | ||
if (num / arr1[i] >= 1) { | ||
num = num - arr1[i] | ||
roman = roman + arr2[i] | ||
i-- | ||
} | ||
} | ||
return roman | ||
} |
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,9 @@ | ||
import test from 'ava' | ||
import {convertToRoman} from '../src' | ||
|
||
test('convert to roman numeral', t => { | ||
const object = 24 | ||
const expected = 'XXIV' | ||
const actual = convertToRoman(object) | ||
t.deepEqual(actual, expected) | ||
}) |