Hi, am a begginer at Javascript programming. I would like to ask experienced programmers to check, whether my solution to the ** Roman Numeral Converter** project is elegant in terms of problem solving.
My code:
function convertToRoman(num) {
let one="I",five="V",ten="X";
function getCombinationElement(power){
switch (power){
case 1:
one="I";
five="V";
ten="X";
break;
case 2:
one="X";
five="L";
ten="C";
break;
case 3:
one="C";
five="D";
ten="M";
break;
case 4:
one="M";
five="v";
ten="x";
break;
}
return {
"0":"",
"1":one,
"2":one+one,
"3":one+one+one,
"4":one+five,
"5":five,
"6":five+one,
"7":five+one+one,
"8":five+one+one+one,
"9":one+ten
}
}
let roman="";
let splittedNum=num.toString().split("");
for (let i=0;i<splittedNum.length;i++){
splittedNum[i]=parseInt(splittedNum[i]);
}
for (let i=0;i<splittedNum.length;i++){
roman+=(getCombinationElement(splittedNum.length-i)[splittedNum[i]]);
}
console.log(roman);
return roman;
}
convertToRoman(400);
If not, what could I improve?
Thanks in advance!