- #1
shivajikobardan
- 674
- 54
- Homework Statement
- Converting decimal to Roman Literal using programming?
- Relevant Equations
- None
JavaScript:
var dict = {
1: "I",
5: "V",
10: "X",
50: "L",
100: "C",
500: "D",
1000: "M",
};
ordinary_literal = Number(prompt("Enter the value of year in foramt 2010"));
//2010 as input
let roman_value = "";
let minDifference = Infinity;
while (ordinary_literal > 0) {
for (var key in dict) {
var difference = Math.abs(ordinary_literal - key);
if (difference < minDifference) {
minDifference = difference;
key_to_use = key;
}
}
roman_value += dict[key_to_use];
ordinary_literal -= key_to_use;
}
console.log(roman_value);
The code fails when pressed 9.
It's because of dictionary. But how do I realize what's missing in dictionary? how do I realize what else to add in this dictionary? Is there a technique to do so?