JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

This is the program for the Roman numeral converter certification project.
While debugging I found out that all the return values and outputs for different test cases are correct but then also the online compiler is not passing all the test cases.
Please see this issue ASAP so that I can complete my certification.

let ans = [];
function convertToRoman(num) {
  
if(num >= 1000)
{
  ans.push("M");
  return convertToRoman(num-1000);
}
else if(num >= 900)
{
  ans.push("CM");
  return convertToRoman(num-900);
}
else if(num >= 500)
{
  ans.push("D");
  return convertToRoman(num-500);
}
else if(num >= 400)
{
  ans.push("CD");
  return convertToRoman(num-400);
}
else if(num >= 100)
{
  ans.push("C");
  return convertToRoman(num-100);
}
else if(num >= 90)
{
  ans.push("XC");
  return convertToRoman(num-90);
}
else if(num >= 50)
{
  ans.push("L");
  return convertToRoman(num-50);
}
else if(num >= 40)
{
  ans.push("XL");
  return convertToRoman(num-40);
}
else if(num >= 10)
{
  ans.push("X");
  return convertToRoman(num-10);
}
else if(num >= 9)
{
  ans.push("IX");
  return convertToRoman(num-9);
}
else if(num >= 5)
{
  ans.push("V");
  return convertToRoman(num-5);
}
else if(num >= 4)
{
  ans.push("IV");
  return convertToRoman(num-4);
}
else if(num >= 1)
{
    ans.push("I");
  return convertToRoman(num-1);
}
else
{
  
  const rans = ans.join("").toString();
 // console.log(rans);
  return rans;
}
//console.log(ans);
 //return ans;
}

convertToRoman(3999);

Your browser information:

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

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

Link to the challenge:

toss some function calls like:

convertToRoman(2);
convertToRoman(2);

at the bottom.
tell me if you see a problem with what it is returning

At the bottom, I am getting this kind of messages
for all the test cases.

convertToRoman(2)

should return the string

II

.

Don’t run the built in test. Just call your function yourself at the bottom with any number twice, and see what it is returning.
Either remove the // from commented section you have with a console log for rans near the end, or console log the function call like so:

console.log(convertToRoman(2));
console.log(convertToRoman(2));

You code is working fine when you call it once, but the test is running your code multiple times in a row.

Edit: looks like it is actually calling it for each test, and with a minor change could pass the test, but it probably should not let you. But the bug you have is keeping some data around that you would probably not want for calling it multiple time.

Thanks got the bug fixed it. :sweat_smile:

1 Like

Out of curiosity which part did you change? Like i said one “fix” lets you pass the test, but probably shouldn’t. It will still break if you call it multiple times in a row depending on how you fixed it, and i would like to know you are learning the right lesson from this bug.

just reassigned the value of the ans as an empty string before returning rans as
ans = ;

OK, i was worried that you just deleted the function call at the end, which would have actually let you pass the test without fixing your global variable problem at all.

Resetting ans works, but it would be much better if you try and avoid using global variable as much as possible, as seen with the bug keeping the previous function call answer. And the way you are using recursion now is kinda like “fake” recursion. And honestly im sure i did the same thing a few times trying to get the hang of recursion when i was learning. Normally you would be simplifying the problem with each call like you are, but in some way combine the answers later inside the recursive function. You might as well be using a normal loop and keep ans inside your function to build up your return with how it is set up now.

Just some things to consider and work on in the future. Happy coding!

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