# Roman Numeral Converter - Shouldn't this work?

So I tried a recursion solution to the roman numeral converter problem. Any number that i test by console logging the function returns what I want. However the “running the tests” says that none of them pass. Does anybody know why? I’m aware this is not the most efficient solution but shouldn’t it still work?

``````let newNum = '';

function convertToRoman(num) {

if (num < 1) {
return newNum
} else if (num >= 1000) {
newNum += 'M';
return convertToRoman(num-1000);
} else if (num >= 900) {
newNum += 'CM';
return convertToRoman(num-900);
} else if (num >= 500) {
newNum += 'D';
return convertToRoman(num-500);
} else if (num >= 400) {
newNum += 'CD';
return convertToRoman(num-400);
} else if (num >= 100) {
newNum += 'C';
return convertToRoman(num-100);
} else if (num >= 90) {
newNum += 'XC';
return convertToRoman(num-90);
} else if (num >= 50) {
newNum += 'L';
return convertToRoman(num-50);
} else if (num >= 40) {
newNum += 'XL';
return convertToRoman(num-40);
} else if (num >= 10) {
newNum += 'X';
return convertToRoman(num-10);
} else if (num >= 9) {
newNum += 'IX';
return convertToRoman(num-9);
} else if (num >= 5) {
newNum += 'V';
return convertToRoman(num-5);
} else if (num >= 4) {
newNum += 'IV';
return convertToRoman(num-4);
} else if (num >= 3) {
newNum += 'III';
return convertToRoman(num-3);
} else if (num >= 2) {
newNum += 'II';
return convertToRoman(num-2);
} else if (num >= 1) {
newNum += 'I';
return convertToRoman(num-1);
}
return newNum;
}
``````

Hi @regrettti, your solution “works” but has a major flaws: it uses a global variable.
This means that that value will keep the previous value assigned to it on each different function calls.

To test it, simply try in console logging two invocation of your function, you will see that the input of each consequent call is concatenated to the previous.

Example:

``````convertToRoman(1) // I
convertToRoman(2) // III
convertToRoman(5) // IIIV
``````

Hope this helps

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