# Converting 1950 to Roman Literal using programming?

I=1
V=5
X=10
L=50
C=100
D=500
M=1000
Now, I want to calculate what’s 1950 in Roman literal.

Can you share the thought process for this? Without any coding? (I’ll write the code myself if you could share the thinking process).

Convert a number into a Roman numeral in JavaScript - Stack Overflow
…and see how many and how different all the responses are, love seeing how other people do the same thing in code and this answer has heaps!

``````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);
``````

I actually don’t know how the hell I was able to solve this problem. Programming is coming to me, I need to spend more time on it.

Your so close!..but its broken, try entering 9 and see what you get. You just missed a few things in your dict.
And yeah, its all time and practice. The more familiar you are with the possibilities the quicker your brain will think that way.

This was mine…

``````
let values = [
[1, "I"],
[4, "IV"],
[5, "V"],
[9, "IX"],
[10, "X"],
[40, "XL"],
[50, "L"],
[90, "XC"],
[100, "C"],
[400, "CD"],
[500, "D"],
[900, "CM"],
[1000, "M"]
];

let decimal = 1951;

let roman = "";

while (decimal > 0) {
for (let i = values.length - 1; i >= 0; i--) {
if (decimal - values[i] >= 0) {
decimal -= values[i];
roman += values[i];
break;
}
}
}
console.log(roman);
``````
1 Like

Can you tell me how do you decide what you need in a dictionary? That’s the important question.

Its in the link of my first post. You asked for an explanation without code and thats what that is, I just copied its logic.

my algorithm is wrong even if I add some things in dictionary. How do I fix that issue? Do I need to change the algorithm completely?