Roman Numeral Converter receiving the write answer but being told its wrong

Tell us what’s happening:

Perhaps I did this different than was wanted but I’m getting the right answers but its telling me I’m getting them all wrong except for convertToRoman(4) which for some reason changes from red to green and back depending on what number I put in to the program.

``````function numOfX(num, string){
for(var i =0; i<num; i++){
total+=string;
}
}
function convertToRoman(num) {
numOfX(Math.floor(num/1000), "M");
num= num%1000;
if(num>=900){
total+="CM";
num -= 900;
}
numOfX(Math.floor(num/500), "D");
num= num%500;
if(num>=400){
total+="CD";
num -= 400;
}
numOfX(Math.floor(num/100), "C");
num= num%100;
if(num>=90){
total+="XC";
num -= 90;
}
numOfX(Math.floor(num/50), "L");
num= num%50;
if(num>=40){
total+="XL";
num -= 40;
}
numOfX(Math.floor(num/10), "X");
num= num%10;
if(num>=9){
total+="IX";
num -= 9;
}
numOfX(Math.floor(num/5), "V");
num= num%5;
if(num>=4){
total+="IV";
num -= 4;
}
numOfX(Math.floor(num/1), "I");

}

convertToRoman(4);
``````

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36`.

https://www.freecodecamp.org/challenges/roman-numeral-converter

Make sure your “total” variable is declared inside the function. I think thats it.

(to clarify, the tests run one after the other. so any variable left outside the function isn’t reset and the previous total will combine with the expected total)

Good luck

You might also need to put the numOfX function inside the convertToRoman function, for reasons that are less clear to me at the moment
Edit: (because of the scope of the total variable once its moved inside convert function, it seems)

I suppose you could alternatively just declare total globally, but initialize it at the top of convertToRoman with total="";
this, way you dont have to move the other function into convertToRoman.

added a total=""; to the top of convertToRoman and it worked. Thanks a ton this is all new to me.

No problem,
You’ll get there, I’ve only been coding …I think 4 months now.
(I had a few false starts where I might have picked up a few things before that too though)