For almost a week I’ve been trying to get past the Roman Numeral challenge, so out of desperation I scrolled down through the hint section and eventually looked at the basic answer.
When I read the answer I could kick myself in the head, it seemed clean and straightforward. Problem is although I understand the answer and could easily recreate it without help, I don’t just want to copy it over and while thinking of another solution to the challenge, the basic answer has become a “once you see it, you can’t unsee it” situation.
I’m still hesitant to use the hint solution, so I thought of improving the code by replacing the for loop with a map function and combine the arrays to a single array of objects, but with little succes.
Here’s what I got so far:
function convertToRoman(num) {
let convertIndex =
[
{M : 1000},
{CM : 900},
{D : 500},
{CD : 400},
{C : 100},
{XC : 90},
{L : 50},
{XL : 40},
{X : 10},
{IX : 9},
{V : 5},
{IV : 4},
{I : 1}
]
//While num => 0, Map for each item.value <= num, return item.key; substract item.value from num and convert the returned keynames to a string
let romanized = convertIndex.map((item) => {while (num => 0) {if (item <= num) {num -= item.value; return item.key} } }).toString() //<====== WARNING: Results in a infinite loop!
return romanized;
}
convertToRoman(16);
Anyone has any idea if what I’m trying to do is even possible or if this would solve the plagiarism dilemma?
Any insight is welcome!