Project 2 of JavaScript -Roman Numeral Converter

The function convertToRoman(4) is not returning correct string for numbers above 3,
kindly help me please

   ** 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;
}

var c=convertToRoman(8);
console.log(c);
   **Your browser information:**

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

Challenge: Roman Numeral Converter

Link to the challenge:

Hi @ambali_zade !

Whenever you are stuck with a project and don’t understand why the tests are failing, always run through your code using one of the test cases.

Test out your code using this function call convertToRoman(4)

Right now that function call returns IIII which is incorrect.

Think through the logic of your current code here

 for (var key in romanToNum) {
    //while 4 is greater than or equal to 1
    while (num >= romanToNum[key]) {
      //add I to the roman variable
      roman += key;
      //subtract 1 from num
      num -= romanToNum[key];
    }
  }

Now take a look at your object

You will need to rethink the ordering for that object.

Once you make that change to the object then the test will pass.

Hope that helps!

1 Like

Thanks a lot! Now I understood what was wrong.

1 Like

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