Hey guys, I’m wondering if anyone could give me some feedback on my Roman Numeral Solution. On one hand I quite like what I’ve done as there is recursion and emphasis on functional programming so I D.R.Y. But after completing the project and looking at the solutions suggested I’m wondering if I overdid it? How would this fly in a dev environment? Thanks
function convertToRoman(num) {
// Divide num by 1000 to get a base figure to derive number of thousands in num.
// Will then be used to derive number of hundreds, tens and singles that make num.
let baseNum = (num/1000).toFixed(3);
function integerCheck(base) {
let checker = Math.floor(base);
return (base - checker) * 10;
}
let thousands = Math.floor(baseNum);
let hundreds = Math.floor(integerCheck(baseNum));
let tens = Math.floor(integerCheck(integerCheck(baseNum)))
let ones = Math.round(integerCheck(integerCheck(integerCheck(baseNum))));
let romanArr = [];
function pushRomanDigit(arr, num, singleNum, midNum, maxNum) {
if (singleNum == "M") {
for (let i = 0; i < num; i++) {
romanArr.unshift("M");
}
} else {
if (num >= 5) {
if (num == 9) {
arr.unshift(maxNum);
arr.unshift(singleNum);
} else {
let pushI = num - 5;
for (let j = 1; j <= pushI; j++) {
arr.unshift(singleNum);
}
arr.unshift(midNum);
}
} else {
if (num <= 4) {
if (num == 4) {
arr.unshift(midNum);
arr.unshift(singleNum);
} else {
for (let j = 1; j <= num; j++) {
arr.unshift(singleNum);
}
}
}
}
}
return arr;
}
pushRomanDigit(romanArr, ones, "I", "V", "X");
pushRomanDigit(romanArr, tens, "X", "L", "C");
pushRomanDigit(romanArr, hundreds, "C", "D", "M");
pushRomanDigit(romanArr, thousands, "M", "", "");
console.log(romanArr);
return romanArr.join("");
}
convertToRoman(4234)