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

**URL:** <https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779>\
**Category:** JavaScript Bootcamp\
**Created:** [September 21, 2022, 8:30am UTC](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779 "2022-09-21T08:30:22Z")\
**Posts on this page:** 7\
**Page:** 3

<div class="post-metadata">

**Author:** ![Jane2963637](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/jane2963637/32/6117_2.png) [@Jane2963637](https://group.classcentral.com/u/Jane2963637)\
**Post date:** [October 6, 2022, 6:18pm UTC](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779/41 "2022-10-06T18:18:25Z")

</div>

Thank you Carlos! 😃

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

Happy coding to you as well! 😁

---

<div class="post-metadata">

**Author:** ![Tzerio](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/tzerio/32/6142_2.png) [@Tzerio](https://group.classcentral.com/u/Tzerio)\
**Post date:** [October 6, 2022, 6:57pm UTC](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779/42 "2022-10-06T18:57:53Z")

</div>

I looked a bit around in the internet and found something here: [The Order of Javascript Object Keys - DEV Community](https://dev.to/frehner/the-order-of-js-object-keys-458d#comment-2200f)

> 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 😉

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.

---

<div class="post-metadata">

**Author:** ![stevenr2945563](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@stevenr2945563](https://group.classcentral.com/u/stevenr2945563)\
**Post date:** [October 6, 2022, 7:52pm UTC](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779/43 "2022-10-06T19:52:04Z")

</div>

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.

```auto
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])
}

```

```auto
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.

---

<div class="post-metadata">

**Author:** ![danc2947636](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@danc2947636](https://group.classcentral.com/u/danc2947636)\
**Post date:** [October 17, 2022, 6:09am UTC](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779/44 "2022-10-17T06:09:41Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![emdubbsdev](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@emdubbsdev](https://group.classcentral.com/u/emdubbsdev)\
**Post date:** [November 2, 2022, 10:11pm UTC](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779/45 "2022-11-02T22:11:58Z")

</div>

Here’s my code for the roman numeral converter 😄

````auto
/* 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));
    ```
````

---

<div class="post-metadata">

**Author:** ![Charly](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/charly/32/6285_2.png) [@Charly](https://group.classcentral.com/u/Charly)\
**Post date:** [November 14, 2022, 3:20pm UTC](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779/46 "2022-11-14T15:20:28Z")

</div>

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! 😅 😆

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/ccstudygroup/original/1X/36206832181ed26910a18e33d8bfef087cf661b1.svg) [@system](https://group.classcentral.com/u/system)\
**Post date:** [December 29, 2022, 3:20pm UTC](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779/47 "2022-12-29T15:20:52Z")

</div>

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

[Previous page](https://group.classcentral.com/t/post-your-roman-numeral-converter-solutions-here-fall-2022/35779.md?page=2)
