Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter

Tell us what’s happening:
my code gives exact result as required for all the tests but it wont except it, I tried in my code editor too. is it because I have assigned my variable outside the function?

  **Your code so far**

var myRomanNumber = '';
function convertToRoman(num) {
  const romanArrayPair = {
    1: 'I',
    4: 'IV',
    5: 'V',
    9: 'IX',
    10: 'X',
    40: 'XL',
    50: 'L',
    90: 'XC',
    100: 'C',
    400: 'CD',
    500: 'D',
    900: 'CM',
    1000: 'M'
  };
  
  const keys = Object.keys(romanArrayPair);
  const values = Object.values(romanArrayPair);
  for (let i=0; i<keys.length+1; i++) {
    if (keys[i] == num) {
      myRomanNumber += values[i]
      return myRomanNumber;
      
    } else if ((keys[i] > num) || i==keys.length) {
      myRomanNumber = myRomanNumber + values[i-1];
      num = num - keys[i-1];
      if (num !== 0) {
        convertToRoman(num);
      }
      return myRomanNumber;
    }
  }
  
    

      
}

convertToRoman(36);



      **Your browser information:**
chrome


**Challenge:**  Roman Numeral Converter

**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests 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

Thank you very much.

1 Like

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