JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

I need some help on figuring out why my code isn’t passing.

Your code so far

function convertToRoman(num) {
  var romanToNum={
 I:1,
IV:4,
 V:5,
IX:9,
X:10,
XL:40,
 L:50,
XC:90,
C:100,
CD:400,
 D:500,
 CM:900,
M:1000
};
var roman='';
for(var key in romanToNum){
while(num>=romanToNum[key]){
roman+=key;
num-=romanToNum[key];
}
}
return roman;
}

convertToRoman(36);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Link to the challenge:

I have applied formatting to your code so that other people can read it

function convertToRoman(num) {
  const romanToNum = {
    I: 1,
    IV: 4,
    V: 5,
    IX: 9,
    X: 10,
    XL: 40,
    L: 50,
    XC: 90,
    C: 100,
    CD: 400,
    D: 500,
    CM: 900,
    M: 1000
  };
  let roman = '';
  for (let key in romanToNum) {
    while (num >= romanToNum[key]) {
      roman += key;
      num -= romanToNum[key];
    }
  }
  return roman;
}

convertToRoman(36);

Note - you should not use var. let and const are preferred.


Can you be more specific about what is going wrong and what you have tried to fix it?

Okay will not use var anymore. And I figured it out! Just needed to rearrange the values in by object from greatest to least.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.