Skip to content

Instantly share code, notes, and snippets.

@sophiabrandt
Last active June 15, 2016 17:01
Show Gist options
  • Save sophiabrandt/ac0e7a775505018698f50060abcdf925 to your computer and use it in GitHub Desktop.
Save sophiabrandt/ac0e7a775505018698f50060abcdf925 to your computer and use it in GitHub Desktop.
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