Why is the test not passing when the console log of the same test is displaying the correct answer?

image
the console displays MMMCMXCIX for [convertToRoman(3999)]
but for some reason, the test for [convertToRoman(3999) should return the string MMMCMXCIX] is not passing
Am I missing something?

  **Your code so far**

var numerals = ["M", "D", "C", "L", "X", "V", "I"];
var values = [1000, 500, 100, 50, 10, 5, 1];

function convertToRoman(num) {
if (!values.length) {
  return "";
}
if (num >= values[0]) {
  return numerals[0] + convertToRoman(num-values[0]);
} 
if (num*1.5 >= values[0]) {
  var place = "1" + "0".repeat(String(num).split("").length-1);
  var floorNum = Math.floor(num/place)*place;
  for (var i=0; i<values.length; i++) {
    if (values.includes(floorNum+values[i])) {
      return numerals[i] + numerals[values.indexOf(floorNum+values[i])] + convertToRoman(num-floorNum);
    }
  }
}
numerals.shift();
values.shift();
return convertToRoman(num);
}


console.log(convertToRoman(3999));
  **Your browser information:**

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

Challenge: Roman Numeral Converter

Link to the challenge:

Because you’re mutating the global variable numerals and values, your function only works one time.

1 Like

I see, removing the last line passes all the tests thank you
will find a different way to approach this so the lists are not mutated

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