Post your Roman Numeral Converter solutions here! (Fall 2022)

Thank you Carlos! :smiley:

I will be trying the exercises again once I have gone through all the JavaScript exercises again. :slight_smile:

Happy coding to you as well! :grin:

3 Likes

I looked a bit around in the internet and found something here: The Order of Javascript Object Keys - DEV Community

When iterating an object, the system looks first for Strings that look like integer keys, and iterates those in numeric order, then iterates the remaining String keys, in insertion order, then iterates the Symbols, again in insertion order.

So it works here because all the keys are letters and are inserted in the right order from the biggest to the lowest.
That’s obviously not something you want to rely on if you use the object for more than for storing these numerals :wink:

edit:
Beware of trying to have the same key in your object as a number and as a string:
the key is always stored as a string and that results in having the key only one time with the value of the last insertion.

3 Likes

Thanks for this answer. It’s very helpful. To make sure I understood it, I wrote the following bit of code, and it did work as expected.

const myObj = {
  2: "prints second",
  M: "prints fourth",
  100: "prints third",
  A: "prints fifth",
  1: "prints first"
}

for (let obj in myObj){
  console.log(myObj[obj])
}
prints first
prints second
prints third
prints fourth
prints fifth

It seems to me that, all things being equal, it’s probably good practice to use an array when one wants to preserve order, rather than use an object and then remember and count on this specific functionality.

4 Likes
 function convertToRoman(num) {
  var roman = {
    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
  };
  var str = '';
  
  for (var i of Object.keys(roman)) {
    var r = Math.floor(num/roman[i]);
    num -= r * roman[i];
    str += i.repeat(r);
  }

  return str;
}
convertToRoman(36);

console.log(convertToRoman(187))
2 Likes

Here’s my code for the roman numeral converter :smile:

/* Converts digit numbers to roman numerals */

function convertToRoman(number) {
    let romanNumerals = 
        {
            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
        }
    
    let result = "";
    for (let i in romanNumerals) {
        while(number >= romanNumerals[i]) {
            result += i;
            number -= romanNumerals[i];
        }
       }
       return result;
    }
    
    console.log(convertToRoman(2081));
    ```
1 Like

Please don’t close this topic, I have not gotten to these exercises yet and I am hoping some people are still around to help once I do! :sweat_smile: :laughing:

1 Like

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.