Last active
June 15, 2016 17:01
-
-
Save sophiabrandt/ac0e7a775505018698f50060abcdf925 to your computer and use it in GitHub Desktop.
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
function convertToRoman(num) { | |
// create an ES6-map | |
const romanNumbers = new Map() | |
.set(1000, 'M') | |
.set(900, 'CM') | |
.set(500, 'D') | |
.set(400, 'CD') | |
.set(100, 'C') | |
.set(90, 'XC') | |
.set(50, 'L') | |
.set(40, 'XL') | |
.set(10, 'X') | |
.set(9, 'IX') | |
.set(5, 'V') | |
.set(4, 'IV') | |
.set(1, 'I'); | |
let romanNum =''; | |
// loop over the romanNumbers map and substitute the input number with the roman equivalent | |
for (const [arabicLetter, romanLetter] of romanNumbers) { // destructuring the key value pairs from the map | |
while (num >= arabicLetter) { | |
romanNum += romanLetter; | |
num -= arabicLetter; | |
} | |
} | |
return romanNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment