Bonfire: Roman Numeral Converter

So the task in hand is to convert decimal numbers in to roman numbers, from all the challenges so far this is my worst solution super cheesed this guy, I wanted to re do it after reading some of the clever solutions out there but I will leave it there as posterity.

With that said let’s look at a cool solution to this task.

function romanize(num) {
  var lookup = {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},
      roman = '',
  for ( i in lookup ) {
    while ( num >= lookup[i] ) {
      roman  = i;
      num -= lookup[i];
  return roman;

So with the FOR he goes through the list and when the target number surpasses the one in the list the while activates and start decomposing the target number, for instance said you want to convert the number 47, FOR goes until XL then the while take the 40 from the list then he adds the key to the var roman and uses the number to subtract from 47-40=7 an so on, really smart. I learned a lot from this code.


Bonfire: Roman Numeral Converter

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s