Tell us what’s happening:
This code works, but originally only have of the cases were working. That was because I didn’t use the hints advice of adding in values like 900, 400, etc… I don’t understand why adding those values changes anything. Can someone explain the logical process of why those values are necessary to make the code function?
Your code so far
function convertToRoman(num) {
let roman = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ];
let decimal = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ];
let romStr = '';
for(let i=0;i<roman.length; i++){
while(decimal[i]<=num){
romStr += roman[i];
num -= decimal[i];
}
}
console.log(romStr)
return romStr;
}
convertToRoman(36);
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS aarch64 11151.29.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.49 Safari/537.36.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter