Roman numeral converter test

Tell us what’s happening:
Describe your issue in detail here.
the issue about the test. I’ve tried it myself, and it gives exactly the task given. is it my code that has a bug or the test has an issue?

  **Your code so far**

function convertToRoman(num) {
const romanNum = [["I","V"], ["X", "L"], ["C", "D"], ["M", "V̅"]];
let str = '';
let okStr = '';
let digits = num.toString().split("").map(iNum => parseInt(iNum)).reverse();
  if (digits[i] == 5){
    str += romanNum[i][1];
  if (digits[i] == 9){
    str += romanNum[i+1][0] + romanNum[i][0];
  if (digits[i] == 4){
    str += romanNum[i][1] + romanNum[i][0];
  if (digits[i] < 4 && digits != 0){
    str += romanNum[i][0].repeat(digits[i]);
  if (digits[i] > 5){
    str += romanNum[i][0].repeat(digits[i] - 5) + romanNum[i][1];

for (i=str.length -1; i >= 0; i--){
okStr += str[i];
return okStr;
  **Your browser information:**

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

Challenge: Roman Numeral Converter

Link to the challenge:

What do the failing tests say?

While technically your code does work you have committed a very serious JS crime against humanity and thus the tests are not passing. You should always use either let or const to declare variables. Not only are you not doing this, you aren’t even using var. I’m shaking with a combination of rage and fear just thinking about this :slight_smile:

OK, I’m being overly dramatic because I’m feeling a little silly this morning. Bottom line, always use let or const when declaring a variable, even if you are declaring those variables in for loops.

1 Like

In this case the tests aren’t offering any helpful advice. You’d have to have the browser’s console open to get an idea of the issue.

Ah, adding


will also show the error

Hi, I might be wrong here since I haven’t read all the material leading up to that challenge yet. But from my little experience in MathLab I would guess that your for-loop don’t actually know what the value of “i” is when it suppose to start the loop. So I think the system is confused since the for-loop is only suppose to start when i=0, but it doesn’t have any information about the current value for “i” when the code reaches your for-loop.
I would therefore guess that adding “let i=0”, "const i=0 or “var i=0” (whatever your preference is for variable-declaration :slight_smile: ) just before the line with the for-loop might at least help if nothing else.
Let me know how it goes.

1 Like

It does know, because the value of i is set in the for loop itself:

for (i=0; 

This tells the loop to star with i set to 0.

You are correct, you should always use either let or const when declaring a variable, but in this case there is no choice, you must use let because you want the value of i to change as the loop is executed.


Yes, that’s true. the test didn’t say anything. I have test it in the console myself, but still it works just fine.

What do you mean by ‘test it in the console yourself’? If you add the one line I said to the bottom of the code in the freeCodeCamp editor, it makes the error really apparent.

1 Like

Oh jees, I didn’t know that. thank you, sir. now it works well. so I’ll always remember to not doing those crime anymore!

no, dude. It works now, the problem was that I didn’t declare the variable for iteration in my loop, I see that freecodecamp really tried to make this test a perfect practice. so, while my code works in other console like chrome, in this test I failed. but thanks for your help!

If, dude, you add


then you get the error message

ReferenceError: assignment to undeclared variable i

which, like I said, exactly describes the problem.