Romanized code seems to fail

Hi!
I have a problem with this code to convert a given number into a roman numeral.
My code works fine, every output that I have tried matches the answer , but when I run the test, it seems that everything is wrong and I check and everything is fine and the answer is of type string.
Can you help me figure it out what it is.
Thanks in advance
Challenge ; https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter

Code:

let result = ""
function convertToRoman(num) {
let dic = {"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}
let roman = ""
let comp = 0
for (let i in dic) {
    if(num >= dic[i]){
      roman = i
      comp = dic[i]
    }
}
result += roman 
num = num - comp
if(num === 0){
return result
} else {
  return convertToRoman(num)
}
}

convertToRoman(36);

and what happens if you call the function twice?

convertToRoman(5); // returns "V";
convertToRoman(10); // returns "VX";

something going weird, here, don’t you think?

do you think you can figure out why this is happening?


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums

1 Like

Thank you, now I see, the previous call holds the ouput on the stack.

it’s not the stack

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

I fixed it but when I run again the test it doesn’t seem to work.

function convertToRoman(num) {
let dic = {"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}
let result = ""
let roman = ""
let comp = 0
var convert = num => {

for (let i in dic) {
    if(num >= dic[i]){
      roman = i
      comp = dic[i]
    }
}
result += roman 
num = num - comp
if(num === 0){
console.log(result)
return result
}
return convert(num)
}

convert(num)
}

convertToRoman(36); // retuns XXXVI
convertToRoman(2); // retuns II
convertToRoman(97); // retuns XCVII
convertToRoman(50); // retuns L
convertToRoman(891); // retuns DCCCXCI

The convertToRoman function isn’t returning the value (it’s returning undefined). You have to return the value of calling convert().

2 Likes

Thank you, I returned convert() and it worked.