Hello everyone
so i tried two ways to solve this chalenge ,
one by using the ascii charecters themselves and one by writting english lettters as roman numerals,
but it seems that the test didn’t accept either way,any idea what’s wrong here?
**Your code so far**
function convertToRoman(num) {
let str = num.toString().split("").reverse();
let str1 = [];
let hundreds = ["C","CC","CCC","CD","D","DC","DCC","DCCC","CM","M"]
//changing the first number to roman using a loop
for (let i = 1 ; i <= 10;i++){
if (parseInt(str[0]) === i){
str1.push(String.fromCharCode(8543+i));
}};
//changing the second number by a mix of if and switch statment
// i didn't like this one ,to many repetation
if(str[1]){
switch (parseInt(str[1])){
case 1:
str1.push(String.fromCharCode(8553));
break;
case 2:
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8553));
break;
case 3:
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8553));
break;
case 4:
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8556));
break;
case 5:
str1.push(String.fromCharCode(8556));
break;
case 6:
str1.push(String.fromCharCode(8556));
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8553));
break;
case 7:
str1.push(String.fromCharCode(8556));
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8553));
break;
case 8:
str1.push(String.fromCharCode(8556));
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8553));
break;
case 9:
str1.push(String.fromCharCode(8553));
str1.push(String.fromCharCode(8556));
break;
}}
//changing the hundreds to roman numerals using english letters
if(str[2]){
for (let y = 1; y <= 10; y++ ){
if(parseInt(str[2]) === y){
str1.push(hundreds[y-1])
}
}
}
str1 = str1.reverse().join("")
console.log(str1)
return num;
}
convertToRoman(9);
Challenge: Roman Numeral Converter
Link to the challenge: