Roman Numeral Converter...output as needed yet cannot pass any of the tests

Roman Numeral Converter...output as needed yet cannot pass any of the tests
0

#1

Tell us what’s happening:
I am unable to pass any of the tests although the output generated is correct…please help…cant figure out whats wrong

Your code so far

var arr1=[1,2,3,4,5,6,7,8,9,10,40,50,90,100,400,500,900,1000,2000,3000,4000];
var arr2=['I','II','III','IV','V','VI','VII','VIII','IX','X','XL','L','XC','C','CD','D','CM','M','MM','MMM','MMMM'];
var res="";
function convertToRoman(num)
{
var found=0;
for(var i=0;i<arr1.length;i++)
{
if(num==arr1[i])
{
found=1;
res=res+arr2[i];
break;
}
}//end of for
if(found==1)//number is found in the array
{
console.log(res);
 // return res;
}
else//number not present in the array
{
for(var k=0;k<arr1.length;k++){
if(num>arr1[k]&&num<arr1[k+1])
{
//console.log(arr2[k]);
//console.log(arr2[k+1]);
res=res+arr2[k];
convertToRoman(num-arr1[k]);

}


}
}//end of else


return res;
}//end of convertToRoman

convertToRoman(3999);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

I do have this line in my code(as the first line):
var arr1=[1,2,3,4,5,6,7,8,9,10,40,50,90,100,400,500,900,1000,2000,3000,4000];

The page isnt showing this line in “your code so far”


#3

put the variables inside the function instead of declaring them globally. FCC retains the values of global variables.
Your code still needs some changes as it doesn’t work for some inputs.


#4

Well you see…I cannot put var res="" inside the function as that would make the value of res equal to nothing (var res="") every time I call the function…can you probably suggest me something else?
Also will you please tell me what are those inputs for which my function fails to produce the correct output?


#5

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

You should really consider indenting your code, to make it more readable. It is much easier to read your code, if you use the following indentation.

var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000, 2000, 3000, 4000];
var arr2 = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M', 'MM', 'MMM', 'MMMM'];
var res = "";

function convertToRoman(num) {
  var found = 0;
  for (var i = 0; i < arr1.length; i++) {
    if (num == arr1[i]) {
      found = 1;
      res = res + arr2[i];
      break;
    }
  } //end of for
  if (found == 1) //number is found in the array
  {
    console.log(res);
    // return res;
  } else //number not present in the array
  {
    for (var k = 0; k < arr1.length; k++) {
      if (num > arr1[k] && num < arr1[k + 1]) {
        //console.log(arr2[k]);
        //console.log(arr2[k+1]);
        res = res + arr2[k];
        convertToRoman(num - arr1[k]);
      }
    }
  } //end of else


  return res;
} //end of convertToRoman

convertToRoman(3999);

#6

You do need to put the res initialization inside the convertToRoman function as @sonimadhuri recommends. The main issue with your code once you correct the global variable issue, is your function attempts a recursive call with:

convertToRoman(num - arr1[k]);

but you should be returning res concatenated with convertToRoman(num - arr1[k]).

So replace your line above with:

return res + convertToRoman(num - arr1[k]);

If not, then your solution will only return the value of res before the first call to convertToRoman inside the function.


#7

what you could do is instead of calling the function convertToRoman(num-arr1[k]) you could do
num=num-arr1[k]; and then while num>0 you keep adding the appropriate character
ex: convertToRoman(12);

num = 12 ;
// it is not equal to any of the elements in arr1;
while num>0
  --num>= 10 && num<20
    add 'X' to result
    num = 12 - 10 =2
 --num>=2 && num<3
   add 'II' to result
   num =2-2 =0 ;
   num is not greater than 0 so exit from the while loop

#8

The logic you have suggested can only work for 2 digit numbers(eg 12 as you said)…however for 3 digit numbers :eg:536
500<536<900
res=D;
num=num-arr1[k];//num=536-500=36
Now you observe that 36 is not present in arr1
(In case of 12 we got 12-10=2 directly in arr1)

Therefore I cannot solve this without calling the function again for num=36


#9

Thank you sir for looking into the problem…I haven’t tried your suggestions yet but I think they would wind up the challenge…


#10

@sonimadhuri
num>=2 && num<3
add ‘II’ to result
num =2-2 =0 ;

This condition can never occur because when num=2( it is already present in arr1 )so value of found becomes 1…the Roman value of 2 directly gets appended to res and the control does not go into the else statement


#11

when you get num=36 the while loop continues
36>=10 && 36<40
num = 36-10 =26,num>0 while continues
26>=10 && 26<40
num = 26-10 =16,num>0 while continues
16>=10 && 16<40
num=16-10 =6,num>0 while continues
6>= 6 && 6<7
num =6-6 =0,num = 0 while stops
Thus for every iteratio you add the corresponding character to the result string.
I tried out something like this and the code passed the test cases,not efficient though:

else//number not present in the array
{
while(num>0){
for(var k=0;k<arr1.length;k++){
if(num>=arr1[k]&&num<arr1[k+1])
{
res=res+arr2[k];
num=num-arr1[k];
break;  
}
}
}
}//end of else

#12

Okk I got your point now…thanks:)