Roman Numeral Converter

Hello,
I have been wrestling with this code for a while now and finally got it working (I thought). Although I doubt its the best solution I was quite happy to have figured it out. Please could someone explain to me why I am not passing the test cases using this?


let i = 0;
let convert = "";

function convertToRoman(num){
  
 const roman = {   
   numerals: {
   1000: "M",
   900: "CM",
   500: "D",
   400: "CD",
   100: "C",
   90: "XC",
   50: "L",
   40: "XL",
   10: "X",
   9: "IX",
   5: "V",
   4: "IV",
   3: "III",
   2: "II",
   1: "I"},
   values: [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 3, 2 ,1]
 }
  // obfuscate function requirements from user
  function converter(num, i) {
 
    let numStore = num;
    // validation condition 1 to run function
    if(num <= 0 && convert.length == 0|| false || NaN ) {
    
    return "Not a number in roman numerals"
    } 
    // validation condition 2 to run function
    else if(numStore >= 5000){
      return "Please enter a smaller number"
    }

    //base-case
   else if (i > roman.values.length || numStore == 0) {    
    return convert
    }   
    //recursive condition 1
    else if(numStore > roman.values[i]) {
    convert = convert.concat(roman.numerals[roman.values[i]])
    numStore -= roman.values[i]    
    return converter(numStore, i)
    } 
    // recursive condition 2
    else if(numStore == roman.values[i]) {
    convert = convert.concat(roman.numerals[roman.values[i]])
    numStore -= roman.values[i]
    return converter(numStore, i)
    }
    // recursive condition 3
    else if(numStore < roman.values[i]) {   
    return converter(numStore, i + 1)
    }  

}
  // assign obsfucated function return value to variable
  let converted = converter(num, i, convert)
  // return converted value to user
  return converted.toUpperCase()}

console.log(convertToRoman(36));

Any help is greatly appreciated. Thanks.

Did you purposely write the function converter inside the function converToRoman? Or was that a mistake?

It was intentional because I thought I was originally failing the test cases due to having to pass the convertToRoman the i variable to produce a result. So I wrapped the function inside of another thinking it would work.

My understanding of the fCC test cases is that they don’t work well with global variables. So I would move all the global variables inside the convertToRoman to start with.

Thanks a lot, I moved them into function scope and it passes!

1 Like

Glad it worked. Try to rewrite the code without the inner function. You only needed a loop for this to work.


Can anyone help me about this case
why it go to undefined?


this is the console

Hi, take a second look at your loop and what your condition to end the loop is. I believe that could be the problem.

If you have a question about a specific challenge as it relates to your written code for that challenge, and you’ve tried to solve it at least 3 times so far and still need some help, just click the Ask for Help button located on the challenge (it looks like a question mark).
This button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Thank for your information

Doesn’t it always increment while the i smaller than hitung?

hi, this is not your thread. Please stop posting to it. We only post to someone else’s thread if we are answering their question. Right now you have posted multiple times with your own questions. Please make your own thread.