Roman Numeral Converter - Tests failing, but correct answer is being returned?

I have written what appears to be a working (though possibly non-elegant!) solution to the challenge, but the test validator says the returns are wrong??

Logging the returns out to the console shows correct response - what am I missing here?

// build an object to hold temp values

let myObj = {
  runTot: 0,
  ret: ''
}


function convertToRoman(num) {
  // asign value of num to object before we start
  myObj.runTot = num;
  
  // Numerals
  const thousandsArr = ['','M','MM','MMM', 'MMMM'];
  const hundredsArr = [ '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'];
  const tensArr = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'];
  const unitsArr = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];

    


  // general purpose function to modify the object
  function decadeCalc(div,array){
    if (myObj.runTot/div >=1){      // the running total / the decade unit is >= 1
      let whole = Math.floor(myObj.runTot/div);  // how many
      myObj.ret += array[whole];    // add the correct numeral to the output
      myObj.runTot-= whole*div;     // what remains
    }
  }
  
  // now call the function for each decade calc
  decadeCalc(1000,thousandsArr); // thousands
  decadeCalc(100,hundredsArr); // hundreds
  decadeCalc(10,tensArr); // tens
  decadeCalc(1,unitsArr); // unitss
 
  return myObj.ret;
}

console.log(convertToRoman(16));

Ouput from above example : is XVI which is correct.

Help!

  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; CrOS aarch64 13816.34.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.51 Safari/537.36.

Challenge: Roman Numeral Converter

Link to the challenge:

Add the following to the beginning of convertToRoman():

console.log('myObj.ret = ' + myObj.ret);

outout is : myObj.ret=XVI

still non the wiser

Screenshot 2021-04-28 00.42.50

Its definitely a string, and has no leading or trailing spaces…

Did you add that console.log I gave you to the very beginning of the function? If so, you should be getting a lot of console logs and I think you will see the issue (hopefully).

Let me ask you this. What should the value of myObj.ret be when the function first starts executing?

when it is first called the value of myObj.ret is ‘’

as the decades are called the value of myObj.ret is incremented by the expected value

Right, I will agree with you, when it is first called it is the empty string. Now what about the second time it is called, or third time? Remember, the tests are calling your function several times in a row.

1 Like

The return value is being built correctly, and is only returned once the build is completed, not incrementally, as the decadeCalc calls are within the main function

I’m trying to help you figure out what the problem is here so I don’t just want to outright tell you the solution. I understand what your function does and the logic is just fine, it will work if you fix this one issue I have been trying to steer you towards.

I’ll ask another question. Why are you using a global object to store your return value? This is almost never a good idea.

2 Likes

Stop calling your function at the bottom and run the test, @bbsmooth is making good and correct points, but the reason you are falling is because you are calling the function

Yes, moving the object into the scope of the function passes the tests.

I had not considered the multiple calls by the test validator… Duh!

Agreed, this would have worked as well, but if calling your function causes it to stop working then I would argue the solution is to fix the function, not stop calling it :slight_smile:

1 Like

That is true, and I was actually surprised to see the object was not persisting, but I guess each test is treated as its own entire thing.

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