Correct output but test cases don't pass

Tell us what’s happening:
I’m able to print correct output for all test cases but still, FCC doesn’t pass any test case.

Your code so far


// var str=''
function convertToRoman(num,str) {
 // console.log(num)
 var numArr=[3000,1000,900,500,400,100,90,50,40,10,9,5,4,1]
 var romArr= [
   'MMM',
   "M",
   "CM",
   "D",
   "CD",
   "C",
   "XC",
   "L",
   "XL",
   "X",
   "IX",
   "V",
   "IV",
   "I"
 ];
 if (num>numArr[0]){
   str+=romArr[0]
   if (num-numArr[parseInt(0)+1]!=0){
     str=convertToRoman(num-numArr[parseInt(0)],str)
 }
 }

 for (let i in numArr){
   // console.log(parseInt(i)+1)
   // console.log(numArr[i], numArr[i+1])
   if (num<numArr[i] & num>=numArr[parseInt(i)+1]){
     // console.log(numArr[parseInt(i)+1],'i+1')
     str+=romArr[parseInt(i)+1]
     // console.log(str)
     if (num-numArr[parseInt(i)+1]!=0){
     str=convertToRoman(num-numArr[parseInt(i)+1],str)
     }
     // else {
     //   return str
     // }
   }
 }
return str;
}

console.log(convertToRoman(3999,''));
// console.log('MMMCMXCIX')

Your browser information:

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

Challenge: Roman Numeral Converter

Link to the challenge:

Hello~!

You call your function with a number and a string.

The FCC tests call the function with only a number. So if I try to call your function like the FCC tests would, I get this:

If you want to do this (and there is no reason not to, it’s the best way to do what you’re trying to do) you need to provide a default value, otherwise you’re making it a requirement that the function accept two arguments, as @nhcarrigan says.

 function convertToRoman(num, str = "")

That says “if an argument for str is not given to the function, use an empty string”

This is normally how you want to set up recursive functions because you need a starting value – once the recursive calls start, there’s no issue, but you need something to kick it off

Thanks a lot for help, it worked!

1 Like