MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 5
Problem: Converting decimal number to Roman numeral.
Problem: Roman numeral is ancient Romans method of writing numbers. The function of the program is to take a given number and return the corresponding Roman numeral.
Algorithm
I created a list of decimal values, corresponding list of Roman numerals and an empty romanized string.
I iterated through all the numbers in the list of decimal values then added the corresponding Roman numeral value to the empty string declared as long as the decimal value is less than the given number. The given number is decreased by subtracting the decimal value from it.
JavaScript Code
function romanNumeral(num) {
let decimalValue = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
let romanValue = [M","CM","D","CD","C","XC","L","XL","X","V","IV","I"];
let romanized = " ";
for(let i = 0; i < decimalValue.length; i++){
while(decimalValue[i] <= num) {
romanized += romanValue[i];
num -= decimalValue[i]; }
}
return romanized;
};