Roman Numeral Converter Challenge Stuck here!

Roman Numeral Converter Challenge Stuck here!
0

#1

An easy way I try to find that I wrote some code partially works but not for all challenges. Please help to correct my coding.
’’'
function convertToRoman(num) {
var index=0;
var number=0;
var romans="";
var numberRomans=[“I”,“IV”,“V”,“IX”,“X”,“XL”, “L”,“XC”,“C”,“CD”,“D”,“CM”,“M”];
for(index=0;index<numberRomans.length;index++){
for(num=1;num<numberRomans.length;number++){
if(num){
romans+=numberRomans[index];
}
}
}
return romans;
}

convertToRoman(5);
’’’


#2

How does your code work?

You have num as function parameter and then you are assigning num = 1 (basically losing your function’s input value) in second for loop and never changing the num so your second for loop is running forever.

Or am I missing something?


#3

I corrected it now but the second for loop is still overflow. Actualy the code works for only 13 steps and not for the others
function convertToRoman(num) {
var index=0;

var romans="";
var numberRomans=[“I”,“IV”,“V”,“IX”,“X”,“XL”, “L”,“XC”,“C”,“CD”,“D”,“CM”,“M”];
for(index=0;index<numberRomans.length;index++){
while(num++){
if(num){
romans+=numberRomans[index];
}
}
}
return romans;
}


#4

It’s still an infinite loop - num++ will always be true.

Can you add comments to your code and explain step-by-step how it works? I think it will help you to understand what is wrong.