MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 5

in #coding6 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;

};

Coin Marketplace

STEEM 0.26
TRX 0.24
JST 0.038
BTC 95392.30
ETH 3285.89
USDT 1.00
SBD 3.39