What's wrong with my switch for the Roman Numeral Converter challenge?

What's wrong with my switch for the Roman Numeral Converter challenge?
0

#1

I’m sure there easier and more efficient ways to do this challenge, but I would like to know how I messed up my way! So why isn’t this way giving me any results for hundR? The console returns nothing.

``````function convertToRoman(num) {
var arr = [];
//convert number to string to array
num = num + "";
arr = num.split("");
//split number into groups
var thou = (arr[arr.length - arr.length]);
var hund = (arr[arr.length - (arr.length-1)]);
var tens = (arr[arr.length - (arr.length-2)]);
var ones = (arr[arr.length - (arr.length-3)]);
//changes groups into roman
//thousands
var thouR = "";
for (var i = 0; i < thou ; i++){
thouR += "M";
}
//hundreds
var hundR = "";
switch (hund){
case 0:
case 1:
case 2:
case 3:
for (var j = 0; j < hund ; j++){
hundR += "C";
}
break;
case 4:
hundR = "CD";
break;
case 5:
case 6:
case 7:
case 8:
hundR = "D";
for (var k = 5; k < hund ; k++){
hundR += "C";
}
break;
case 9:
hundR = "CM";
break;
}

return hundR;

}

convertToRoman(3399);
``````

#2

You need to use triple backticks to post code to the forum.
See this post for details.

#3

Thanks for doing that, I’ll check out that post.

#4

Your switch cases are looking for numbers(number type).Switch case is using strict equality).The variable hund is a string.

#5

btw `arr.length - arr.length` is always zero - you can simplify the digits to

``````let thou=arr[0]
let hund=arr[1]
``````

the reason is `switch` uses strict equality `===`

this means `hund` which is the string `"3"` does not match `case 3:` which has the number `3`

Change to

``````let hund=parseInt(arr[1], 10)
``````

#6

Thank you, appreciate the help.

#7

That was silly of me, thanks for the help!