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

Commit c3a61db

Browse files
eliassilva8Kent C. Dodds
authored and
Kent C. Dodds
committed
feat(convertToRoman): Add 'convertToRoman' function (#122) (#123)
1 parent 956c1b7 commit c3a61db

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/convertToRoman.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default convertToRoman
2+
3+
/**
4+
*Original Source: https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript
5+
*This method will convert a number into a roman numeral
6+
* @param {Number} num - number to convert
7+
* @return {String} - roman numeral of the number
8+
*/
9+
10+
function convertToRoman(num) {
11+
const arr1 = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
12+
const arr2 = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
13+
let roman = ''
14+
for (let i = 0; i < arr1.length; i++) {
15+
if (num / arr1[i] >= 1) {
16+
num = num - arr1[i]
17+
roman = roman + arr2[i]
18+
i--
19+
}
20+
}
21+
return roman
22+
}

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import isOdd from './is-odd'
4141
import isNumeric from './is-numeric'
4242
import max from './max'
4343
import slugify from './slugify'
44+
import convertToRoman from './convertToRoman'
4445

4546
export {
4647
isOdd,
@@ -86,4 +87,5 @@ export {
8687
isNumeric,
8788
max,
8889
slugify,
90+
convertToRoman,
8991
}

test/convertToRoman.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from 'ava'
2+
import {convertToRoman} from '../src'
3+
4+
test('convert to roman numeral', t => {
5+
const object = 24
6+
const expected = 'XXIV'
7+
const actual = convertToRoman(object)
8+
t.deepEqual(actual, expected)
9+
})

0 commit comments

Comments
 (0)