Problem Explanation:
- You will create a program that converts an integer to a roman numeral.
Hint: 1
- Creating an array with the Roman Numerals and one with the decimal equivalent for the new forms will be very helpful.
Hint: 2
- If you add the numbers that go before the new letter is introduced, it will save you plenty of code, like values for 4, 9, and 40.
Hint: 3
- You can't have more than three consecutive Roman numerals together.
Spoiler Alert!
Solution ahead!
Code Solution:
var convert = function(num) {
// Create arrays with default conversion with matching indices.
var decimalValue = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ];
var romanNumeral = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ];
//empty string variable for the final roman number
var romanized = '';
// Loop through the indices of the decimalValue array.
for (var index = 0; index < decimalValue.length; index++) {
// Continue to loop while the value at the current index will fit into numCopy
while (decimalValue[index] <= num) {
// Add the Roman numeral & decrease numCopy by the decimal equivalent.
romanized += romanNumeral[index];
num -= decimalValue[index];
}
}
return romanized;
};
Code Explanation:
- Read comments on code.
Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @SaintPeter @benschac for your help with Algorithm: Roman Numeral Converter
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)