JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

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

Check the comment on my recursive function on the bottom. Is it possible to access the variable I’m asking for?

  **Your code so far**
function convertToRoman(num) {
  
const Numerals = {
    Roman: {
      M : 1000,
      CM: 900,
      D: 500,
      CD: 400,
      C: 100,
      XC: 90,
      L: 50,
      XL: 40,
      X: 10,
      IX: 9,
      V: 5,
      IV: 4,
      I: 1
    },
    Arabic: {
      1000 : 'M',
      900: 'CM',
      500: 'D',
      400: 'CD',
      100: 'C',
      90: 'XC',
      50: 'L',
      40: 'XL',
      10: 'X',
      9: 'IX',
      5: 'V',
      4: 'IV',
      1: 'I'
    }
  }

  //console.log(Numerals.Roman.M +  Numerals.Roman.M)

  let conversionArr = [];
  let mutableNum = num;
  let decreasingNum = 0;
  let decreasingNum2 = 0;

  for (let key in Numerals.Roman) {
    //console.log(key, Numerals.Roman[key]) //key, value syntax
    decreasingNum = Math.max(mutableNum - Numerals.Roman[key])
    if (decreasingNum >= 0) {
      conversionArr.push(Numerals.Roman[key])
      break; //important stops the for loop so it doesn't go [10,9,etc.]
    }
  }
  console.log(decreasingNum);

  console.log(recurseToZero(decreasingNum));

  function recurseToZero(decNum) {
    for (let key in Numerals.Roman) {
    let newDecNum = Math.max(decNum - Numerals.Roman[key])
      if (newDecNum >= 0) {
        conversionArr.push(Numerals.Roman[key])
        break;
      }
    }
    if (newDecNum > 0) { //condition to keep recursing //How do I access the newDecNum inside the for loop?
      recurseToZero(newDecNum);
    }
  }

}
convertToRoman(36);


  **Your browser information:**

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

Challenge: JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Link to the challenge:

I figured it out, by changing the let keyword. Wasn’t a pretty solution, but worked, I guess.

1 Like

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