I just sadly spent 5 hours trying to figure this out. Thank gosh for my whiteboard. What do you think of my solution?
/*jshint esversion: 6 */
function convertToRoman(num) {
small = ['I', 'X', 'C', 'M'];
medium = ['V', 'L', 'D'];
large = ['X', 'C', 'M'];
let placeholder = '';
let i = num.toString().length;
num.toString()
.split('')
.map(elem => {
return Number(elem);
})
.forEach(digit => {
i = i - 1;
if (digit === 9) {
placeholder += small[i] + large[i];
} else if (digit < 9 && digit > 4) {
let times = (3 - (8 - digit));
placeholder += medium[i];
for (let x = 0; x < times; x++) {
placeholder += small[i];
}
} else if (digit === 4) {
placeholder += small[i] + medium[i];
} else {
for (let j = 0; j < digit; j++) {
placeholder += small[i];
}
}
});
return placeholder;
}
convertToRoman(36);