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

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

#1

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.

Your code so far

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");
  
 return total;
}

convertToRoman(4);

Your browser information:

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.

Link to the challenge:


#2

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


#3

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.


#4

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


#5

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)