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

Commit

Permalink
feat(convertToRoman): Add 'convertToRoman' function (#122) (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassilva8 authored and Kent C. Dodds committed Oct 24, 2017
1 parent 956c1b7 commit c3a61db
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/convertToRoman.js
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
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import isOdd from './is-odd'
import isNumeric from './is-numeric'
import max from './max'
import slugify from './slugify'
import convertToRoman from './convertToRoman'

export {
isOdd,
Expand Down Expand Up @@ -86,4 +87,5 @@ export {
isNumeric,
max,
slugify,
convertToRoman,
}
9 changes: 9 additions & 0 deletions test/convertToRoman.test.js
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)
})

0 comments on commit c3a61db

Please sign in to comment.