Roman Numeral Converter - Legacy Curriculum

Hello, I’ve already completed the beta roman numeral converter successfully, but I’ve gone back to complete the legacy section challenge. I’ve created my code and I get all of the correct answers when displayed in the console, but the challenges all fail and I’m not sure why. Any help is greatly appreciated. Thank you :slight_smile:

### Your code so far


let romanValue = '';
const numeralValues = [
  ['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]
]


function convertToRoman(num) {
 

for (let i = 0; i < numeralValues.length; i++) {
    if ((num / numeralValues[i][1]) >= 1) {
      num -= numeralValues[i][1];
      romanValue += numeralValues[i][0];
      
      return convertToRoman(num);
    } 
    
  }
  return romanValue;
}

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Learn Basic JavaScript by Building a Role Playing Game - Step 165

Welcome to the forum @nate10363

Your code passes.
Reset the step and try again. If that doesn’t work, refresh the page, disable dark mode, disable ad blockers. Or, use another browser.
If the above steps do not work, you may need to restart the computer.

Happy coding

thank you for having me < 3

the action that actually accepted the code was removing the default convertToRoman(36) call at the bottom of the code lol

thank you for the suggestions, though! I’m sure they’ll be used in the future

1 Like

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

I’m sooo glad I found this post. I knew my code was working but wouldn’t pass tests. Once I commented out the original call to convertToRoman(36), it worked.

Thanks for posting what worked.
Sean C

1 Like

I would go for a more effective fix than that! You shouldn’t be using global variables like that.

I actually move the variable to global as an attempt to try anything to get the check marks lol. Thank you for the insight :slight_smile:

The intent of the projects isn’t to get checkmarks. The intent is to make something that correctly meets the design goals. In this case, global variable (ab)use makes it harder to write a reusable function that meets the project description.

I meant more that I knew my original solution without a global variable should pass, so I was trying anything to pass the solution, as I’ve had odd aspects pass in fcc when the typical solution wouldn’t. This was just the permutation of changes that I happened to have posted at the time. I appreciate your comments, though, and will definitely be more cautious of global variables in the future