Can't pass test cases for roman numeral converter

Tell us what’s happening

Hello,
I tried to solve the roman numeral problem in the project. I am getting correct output for this but I couldn’t pass any of the test cases. Any suggestions?
Thank you.

Code so far:


//-----------ROMAN NUMERAL GENERATOR--------
let globalSymObj = {"M":1000,"D":500,"C":100,"L":50,"X":10,"V":5,"I":1}

let passSymArr = {
 1:{ le3:['I'], le8:['I','V'], e9:['I','X']},
 10: {le3:['X'], le8:['X','L'], e9:['X','C']},
 100: {le3:['C'], le8:['C','D'], e9:['C','M']},
 1000:{le3:['M']}
}
let ansArr = []

//----Function 1---------to generate n times a symbol--------

function nTimesSymbol(n,symbol) {
 let myArr1=[];
 for(let i=0;i<n;i++) {
  myArr1.push(symbol);
 }
 return myArr1;
}

//--------Function 2---------to deal with multiple symbols------

function multipleSymbols(n,symArr,multiplier){
let myArr2=[];
if(n===0)
return null;
myArr2 = myArr2.concat(symArr[1]);

if(multiplier*n === (globalSymObj[symArr[1]]-globalSymObj[symArr[0]]) )
  myArr2 = symArr[0].concat(myArr2);
else
 myArr2 = myArr2.concat(nTimesSymbol((n*multiplier-globalSymObj[symArr[1]])/multiplier,symArr[0]))

 return myArr2;
}

//---------------Function 3-----------------------
function  mainFunction(num,multiplier,symArr) {
 
 if(num<=3 && num!==0){
   ansArr = ansArr.concat(nTimesSymbol(num,symArr["le3"][0]))
   return ansArr;
 }
 else if(num<=8){
 ansArr = ansArr.concat(multipleSymbols(num,symArr["le8"],multiplier))
 return ansArr;
 }
 else if(num===9){
   ansArr =ansArr.concat(multipleSymbols(num,symArr["e9"],multiplier));
 return ansArr;
 }
}
//-------------Function 4 ------------------

function convertToRoman(num){
 let input = num.toString();
 let myArr3= input.split('');
 for(let i=0;i<myArr3.length;i++){
   let multiplier= Math.pow(10,myArr3.length-i-1)
  ansArr = mainFunction(Number(myArr3[i]),multiplier,passSymArr[multiplier])
 }
  return ansArr.join("");
}

console.log(convertToRoman(44));

Challenge: Roman Numeral Converter

Link to the challenge:

Add a few more test cases to print out at the end of code and check if it gives an expected result.

But I already checked most of the test cases given there & it gives expected results.

Did you check what happens if you try to convert two numbers one after the another?

1 Like

Oh, I didn’t check that way earlier. I got it now. Thank you sanity.

1 Like