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
Link to the challenge: