MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 5

in coding •  6 years ago  (edited)

IMG_20190126_220846.jpg

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

  1. I created a list of decimal values, corresponding list of Roman numerals and an empty romanized string.

  2. 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;

};

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!