# Roman Numeral Converter - Recursion not working

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

I’ve decided to try my hand at implementing recursion. However, it doesn’t seem to be working. The function only runs the first time.

``````  **Your code so far**
``````
``````
function convertToRoman(num) {
let amount = 0;

const romanToNum = {
1 : 'I', 2:'II', 3: 'III', 4:'IV', 5: 'V', 6:'VI', 7:'VII', 8:'VIII', 9:'IX', 10:'X', 20:'XX', 30:'XXX', 40:'XL', 50:'L', 60:'LX', 70:'LXX', 80:'LXXX', 90:'XC', 100:'C', 200:'CC', 300:'CCC', 400:'CD', 500:'D', 600:'DC', 700:'DCC', 800:'DCCC', 900:'CM', 1000:'M', 2000:'MM', 3000:'MMM'
}

const thousands = (num) => {
return Math.trunc(num/1000) * 1000;
}

const hundreds = (num) => {
return Math.trunc(num/100) * 100;
}

const tens = (num) => {
return Math.trunc(num/10) * 10;
}

let romanNumeral = [];
if (num === 0){
return;
}else{

if ((num - 1000) > 0){
amount = thousands(num)
romanNumeral.push(romanToNum[amount])
convertToRoman(num - amount)
}else if((num - 100) > 0){
amount = hundreds(num)
romanNumeral.push(romanToNum[amount])
convertToRoman(num - amount);
}else if ((num -10) > 0){
amount = tens(num)
romanNumeral.push(romanToNum[amount])
convertToRoman(num - amount);
}

return romanNumeral.join('');

}
}
console.log(convertToRoman(36));
console.log(convertToRoman(3999));
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (X11; CrOS x86_64 14324.62.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.77 Safari/537.36`

Challenge: Roman Numeral Converter

This absolutely is a bad fit for recursion, fwiw. Recursion should be used very rarely.

1 Like

Thanks for your reply. I’ve just been seeing it being used a lot and have difficulty using it.

I’ll use a loop

Its totally normal to have difficulty with it. It’s a hard idea and not a lot of problems fit it naturally.

There are a few problems with your recursive implementation:

• You aren’t returning a consistent value. Your base case is returning `undefined` and otherwise you are returning a string.

• You aren’t doing anything with the value returned by the recursive call. How can you gradually create a Roman numeral if you are throwing away the work that the recursive call is doing?

1 Like

This problem can be solved recursively. I did it once on a dare. This is a problem where recursion makes the solution less clear.

I do think that there is some value in practicing translating from an iterative solution to a recursive one. It can help build comfort and familiarity with both approaches. Often, when doing such an exercise I suggest building the (intuitive) iterative solution first and then looking at the looping mechanics and how you would translate that into a recursive approach. Don’t try coming at it from a completely different angle, but rather learn how looping logic and recursive logic relate to each other.

2 Likes

I’ll try that out. Managed to solve the challenge using a while loop.

Thank you.

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