# Javascript Project: Roman Numeral converter question

Hello,

Working through the Roman Numeral converter problem and looking at one of the provided solutions:

``````var convertToRoman = function(num) {
var decimalValue = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var romanNumeral = [
"M",
"CM",
"D",
"CD",
"C",
"XC",
"L",
"XL",
"X",
"IX",
"V",
"IV",
"I"
];

var romanized = "";

for (var index = 0; index < decimalValue.length; index++) {
while (decimalValue[index] <= num) {
romanized += romanNumeral[index];
num -= decimalValue[index];
}
}

return romanized;
};
``````

So this approach uses an indexed value and pulls the values from the two arrays to be converted, I understand how this works, but I can’t seem to figure out how this approach can create for example decimal number 16 into roman numeral XVI, when the number 6 doesn’t exist in the decimal array?

Sorry I know the question is a little bit vague but I can’t get my head around the logic.

For 16, it finds 10 first (first number that is <= `num`).
That’s `X`. Subtract 10 from `num`, leaving 6.
For 6, it finds 5. That’s `V`. Subtract 5 from `num`, leaving 1. That’s `I`, finished as `XVI`: `num` is now 0 and loop finishes.

Thank you, that explained it nicely

1 Like