Roman Numeral Converter working but freeCodeCamp not accepting my result

Hi. I 'm new in coding, and also in javaScript. I have problem while trying to solve the second challenge of the javaScript “Roman Numeral Converter”. I mean it’s give output correctly but still why its not marking as correct? Please help me anyone. Forgive my English.

My code so far—

//Roman numerals and Arabic numerals object---

const numeralsObj = {
  '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,
}

//Extracting all the values of the object in an array---

let valueArray = [];
for (let key in numeralsObj) {
  if (numeralsObj.hasOwnProperty(key)) {
    valueArray.push(numeralsObj[key]);
  }
}

//Extracting value in a array from an array in respective to a given number---

let roman = [];
const romanNumArr = function (arr, nu) {
  const remain = nu - arr[0];
  roman.push(arr[0])
  if (remain >= arr[0]) {
    romanNumArr(arr, remain);
  } else {
    const b = arr.filter(el => el > remain);

    arr.splice(0, b.length);

    if (arr[0] < remain) {
      romanNumArr(arr, remain);
    } else {
      if (remain !== 0) {
        roman.push(remain);
      }
    }
  }
}

function convertToRoman(num) {
  let romanArray = [];
  const ar = valueArray.filter((el) => el <= num);
  // Calling the function
  romanNumArr(ar, num);

  //Extracting key from the object in respective to the Arabic array of values---

  roman.forEach((romNum) => Object.keys(numeralsObj).forEach(key => {
    if (numeralsObj[key] === romNum) {
      romanArray.push(key);
    }
  }));

  //Joing the array---

  num = romanArray.join("");
  console.log(num);
  return num;
}

convertToRoman(891);

I’ve got it. I think I’ve to empty the global variable before moving to to execute another function, else it keeps my past key in that “roman” array. So before returning the function i’ve reset it to empty like this roman = ;

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