Having an issue with a challenge but think I have it right

Tell us what’s happening:
Describe your issue in detail here.

I don’t understand why this is giving me an error. I thought maybe it had to do with it not being a string so I did try to use a convert .toString() as well in case that fixed the issue but it’s still failing. I’m seeing the output as correct though. I don’t want the answer cause it is a challenge but I am curious if you’re seeing an issue here that tells you it doesn’t work. I did run this in VSCode to make sure and it seems to run fine as output if I console.log it out.

**Your code so far**

let romanConverted = '';

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

convertToRoman('2');
**Your browser information:**

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

Challenge: Roman Numeral Converter

Link to the challenge:

Sooo you’ve made a global variable, romanConverted - but does that get cleared between tests?

At a guess, while it looks like its working for the first test, it just keeps accumulating for every subsequent one.

Try to find a way to avoid globals, and have the function return the recursive call added to the string each time.

1 Like

snowmonkey is right.
For all the tests, remember to turn the function call into a console.log(convertToRoman('2'))
This way you actually see what your code produces and if you call it several times, you’ll see the problem with global variables.

Generally: NEVER use globals UNLESS it’s a constant.

2 Likes

Ah shoot. I never thought about it being called more than once. I didn’t see it cause I kept testing the console.log but only by changing the one call, not multiple calls

1 Like

I feel dirty with this solution but it works.

function convertToRoman(num, romanConverted = ‘’) {

if(num <= 0) {

    return romanConverted;

} else if(num >= 1000) {

    romanConverted += 'M';

    return convertToRoman(num - 1000, romanConverted);

} else if(num >= 900) {

    romanConverted += 'CM';

    return convertToRoman(num - 900, romanConverted);

} else if(num >= 500) {

    romanConverted += 'D';

    return convertToRoman(num - 500, romanConverted);

} else if(num >= 400) {

    romanConverted += 'CD';

    return convertToRoman(num - 400, romanConverted);

} else if(num >= 100) {

    romanConverted += 'C';

    return convertToRoman(num - 100, romanConverted);

} else if(num >= 90) {

    romanConverted += 'XC';

    return convertToRoman(num - 90, romanConverted);

} else if(num >= 50) {

    romanConverted += 'L';

    return convertToRoman(num - 50, romanConverted);

} else if(num >= 40) {

    romanConverted += 'XL';

    return convertToRoman(num - 40, romanConverted);

} else if(num >= 10) {

    romanConverted += 'X';

    return convertToRoman(num - 10, romanConverted);

} else if(num >= 9) {

    romanConverted += 'IX';

    return convertToRoman(num - 9, romanConverted);

} else if(num >= 5) {

    romanConverted += 'V';

    return convertToRoman(num - 5, romanConverted);

} else if(num >= 4) {

    romanConverted += 'IV';

    return convertToRoman(num - 4, romanConverted);

} else if(num >= 1) {

    romanConverted += 'I';

    return convertToRoman(num - 1, romanConverted);

}

}

convertToRoman(2);

convertToRoman(4);

1 Like

Good ^^
Yeah this it kind of the issue with recursions, if you want to add values.
You could try wrapping the recursion into another function where you declare the romanConverted variable. I think that should work.

Though honestly, your solution for the most part is basically a while-loop with extra steps. So if you feel like your solution is dirty, just try transforming it into an actual while-loop :wink:

There must be a better way with recursion to fix it but maybe not.

I really wouldn’t use recursion for this challenge. The recursion makes the logic much harder.

1 Like

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