JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Hello everyone!
In this solution…


Summary
function convertToRoman(num) {
 let romanNumbers= {.......}

let answer = "";

for (let n in romanNumbers){
while(num >= romanNumbers[n]){
answer += n;
num -= romanNumbers[n];
}
}

return answer;

}

convertToRoman(36);

Why the while loop dont stop when “num” becomes “6”?
and than we obtained the sum “XXX + VI”…that last VI, I dont get it…
My Regards to all!

I have no idea what romanNumbers is, so I can’t answer

Hi, sorry…
let romanNumbers= {
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
};

thanks for reply!

When it’s 6, V is added, so then num goes down to 1, and then I is added

but…when num = 6 and romanNumbers[n] = 10…
while(num >= romanNumbers[n])
the while cycle, should end??
Why doesn’t it end at that point?

it does tho, it’s the for…in that changes the value of n

1 Like

Now I get it!
romanNumbers[n] becomes 5, when num reaches the point of becoming 6, and so “num” remains greater than “romanNumbers[n]”…

I hope I get it right :slight_smile:

thank you so much ilenia!