Hi, everyone!
The code below does it’s job, but could you please point to the main deficiencies of it?
I came up with this approach on my own, before I’ve watched the project walk-through video and I see that there are more… concise ways to solve the problem.
What would you, as more experienced devs, say about my approach? what should I work on?
I am trying to get as much as I can from these projects on FCC and would really appreciate your comments.
Thank you!
Code for review:
const romArr = [['I','V','X'],['X','L','C'],['C','D','M'],['M']]
const translator = (digit, number) => {
let result;
switch(number){
case 1:
result = romArr[digit][0]
break;
case 2:
result = romArr[digit][0].repeat(2)
break;
case 3:
result = romArr[digit][0].repeat(3)
break;
case 4:
result = romArr[digit][0]+romArr[digit][1]
break;
case 5:
result = romArr[digit][1]
break;
case 6:
result = romArr[digit][1]+romArr[digit][0]
break;
case 7:
result = romArr[digit][1]+romArr[digit][0].repeat(2)
break;
case 8:
result = romArr[digit][1]+romArr[digit][0].repeat(3)
break;
case 9:
result = romArr[digit][0]+romArr[digit][2]
break;
default:
result = ''
break
}
return result
}
const convertToRoman = num => {
if(!(num>0&&num<4000)){
throw Error('invalid number');
} else {
let accumArr = [];
let numArr = (''+num).split('').reverse();
for(let digit=0; digit<numArr.length; digit++){
let numVal = parseInt(numArr[digit],10);
let transRes = translator(digit, numVal);
accumArr.unshift(transRes);
}
let result = accumArr.join('');
return result
}
}