Skip to content

Instantly share code, notes, and snippets.

@loonylou
Last active June 12, 2018 01:21
Show Gist options
  • Save loonylou/faa340c15d7b65eac674529b358cfda7 to your computer and use it in GitHub Desktop.
Save loonylou/faa340c15d7b65eac674529b358cfda7 to your computer and use it in GitHub Desktop.
Roman Numeral Convertor - FCC
function convertToRoman(num) {
let numeralString = [];
const numerals = [
{'value': 1000, 'symbol': 'M'},
{'value': 900, 'symbol': 'CM'},
{'value': 500, 'symbol': 'D'},
{'value': 400, 'symbol': 'CD'},
{'value': 100, 'symbol': 'C'},
{'value': 90, 'symbol': 'XC'},
{'value': 50, 'symbol': 'L'},
{'value': 40, 'symbol': 'XL'},
{'value': 10, 'symbol': 'X'},
{'value': 9, 'symbol': 'IX'},
{'value': 5, 'symbol': 'V'},
{'value': 4, 'symbol': 'IV'},
{'value': 1, 'symbol': 'I'}
]
numerals.forEach((numeral) => {
while (num >= numeral.value) {
numeralString += numeral.symbol;
num -= numeral.value;
}
});
//console.log(numeralString);
return numeralString;
}
////////////////////////////// TESTS //////////////////////////////
//convertToRoman(2)
//"II"
//convertToRoman(3)
//"III"
//convertToRoman(4)
//"IV"
//convertToRoman(5)
//"V"
// convertToRoman(9)
//"IX"
// convertToRoman(12)
//"XII"
// convertToRoman(16)
//"XVI"
// convertToRoman(29)
//"XXIX"
// convertToRoman(44)
//"XLIV"
// convertToRoman(45)
//"XLV"
// convertToRoman(68)
//"LXVIII"
// convertToRoman(83)
//"LXXXIII"
// convertToRoman(97)
//"XCVII"
// convertToRoman(99)
//"XCIX"
// convertToRoman(400)
//"CD"
// convertToRoman(500)
//"D"
// convertToRoman(501)
//"DI"
// convertToRoman(649)
//"DCXLIX"
// convertToRoman(798)
//"DCCXCVIII"
// convertToRoman(891)
//"DCCCXCI"
// convertToRoman(1000)
//"M"
// convertToRoman(1004)
//"MIV"
// convertToRoman(1006)
//"MVI"
// convertToRoman(1023)
//"MXXIII"
// convertToRoman(2014)
//"MMXIV"
// convertToRoman(3999)
//"MMMCMXCIX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment