# Strange problem on Roman Numerals Challenge

Strange problem on Roman Numerals Challenge
0

#1

Hello everyone,
There seems to be a small issue with my code and the Roman Numeral Challenge .js compiler. I can see my code running perfectly fine in both Firefox and Chrome web-developer console, still here in freeCodeCamp all numbers which are equal or above 500 fails the test.
I know there are other ways of solving this challenge, however, I believe it’s a good thing to point this problem here.
CODE:

``````function convertToRoman(num) {
var toString = num.toString(); var length = toString.length;
var array = [];
for (var i = 0; i < length; i++) array.push(toString[i]); array = array.reverse(); // Units firts, then decimals, then hundreds;
array.forEach(function (value, index) {
switch (index) {
case 0:
switch (value) {
case '1': array[index] = 'I'; break;
case '2': array[index] = 'II'; break;
case '3': array[index] = 'III'; break;
case '4': array[index] = 'IV'; break;
case '5': array[index] = 'V'; break;
case '6': array[index] = 'VI'; break;
case '7': array[index] = 'VII'; break;
case '8': array[index] = 'VIII'; break;
case '9': array[index] = 'IX'; break;
default: array[index] = ''; break;
}
break;
case 1:
switch (value) {
case '1': array[index] = 'X'; break;
case '2': array[index] = 'XX'; break;
case '3': array[index] = 'XXX'; break;
case '4': array[index] = 'XL'; break;
case '5': array[index] = 'L'; break;
case '6': array[index] = 'LX'; break;
case '7': array[index] = 'LXX'; break;
case '8': array[index] = 'LXXX'; break;
case '9': array[index] = 'XC'; break;
default: array[index] = ''; break;
}
break;
case 2:
switch (value) {
case '1': array[index] = 'C'; break;
case '2': array[index] = 'CC'; break;
case '3': array[index] = 'CCC'; break;
case '4': array[index] = 'CD'; break;
case '5': array[index] = 'D'; break;
case '6': array[index] = 'DC'; break;
case '7': array[index] = 'DCC'; break;
case '8': array[index] = 'DCCC'; break;
case '9': array[index] = 'CM'; break;
default: array[index] = ''; break;
}
break;
case 3:
switch (value) {
case '1': array[index] = 'M'; break;
case '2': array[index] = 'MM'; break;
case '3': array[index] = 'MMM'; break;
case '4': array[index] = 'MMMM'; break;
case '5': array[index] = 'MMMMM'; break;
case '6': array[index] = 'MMMMMM'; break;
case '7': array[index] = 'MMMMMMM'; break;
case '8': array[index] = 'MMMMMMMM'; break;
case '9': array[index] = 'MMMMMMMMM'; break;
default: array[index] = ''; break;
}
break;
default:
break;
}
});
return array.reverse().join("");
}
``````

Returned value from convertToRoman(500) in Chrome’s developer console:

``````D
``````

Returned value from convertToRoman(500) in FreeCodeCamp challenge page:

``````L
``````

#2

Rewrite the above as:

``````for (var i = 0; i < length; i++)
array.push(toString[i]);
array = array.reverse();
``````

It is more readable, plus it works. Not exactly sure why it failed written the other way. I will need to think about it some more.

#3

Now that’s a funny bug right here. Perhaps this is just the computer urging me to always write readable code. Thanks again—btw, now I see I could have saved some time by declaring array = toString.split("").reverse()