# \[Solved\] Roman Numeral Converter - Lost

**URL:** <https://forum.freecodecamp.org/t/solved-roman-numeral-converter-lost/101074>\
**Category:** General\
**Created:** [March 29, 2017, 7:38am UTC](https://forum.freecodecamp.org/t/solved-roman-numeral-converter-lost/101074 "2017-03-29T07:38:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshamore](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/joshamore/32/50035_2.png) [@joshamore](https://forum.freecodecamp.org/u/joshamore)\
**Post date:** [March 29, 2017, 7:38am UTC](https://forum.freecodecamp.org/t/solved-roman-numeral-converter-lost/101074/1 "2017-03-29T07:38:46Z")

</div>

Hey guys,

I’m working on the Roman Numeral Converter (intermediate scripting challenge). I’ve been working at this for a few days now and don’t seem to making any progress.

**My code is supposed to:**

- Split the received number into individual digits.
- Multiply the digits (converting them into correct powers – eg. 99 will become an array: [90, 9]).
- Loop each power through a collection of if/else chains, which are supposed to push the correct roman numeral to an array.
- Join and return the array, which will contain the numerals.

My issue is in the looping section.

I was originally using two arrays that looked like this:

```
var numForKey = [1, 5, 10, 50, 100, 500, 1000];
var lettersForKey = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];

```

But I’ve changed it to the below (and tried to update the loops to work with the new array structure):

```
var numForKey = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
var lettersForKey = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'];

```

My code is here: [https://pastebin.com/PGAY93TU](https://pastebin.com/PGAY93TU)

Can anyone nudge me in the right direction?

Cheers 😄

* * *

I got this working. There were _many_ issues with my above solution. If you’re working through this problem right now and haven’t used indexOf(), I would highly recommend using it instead of adding another for loop into your code (what I did above).

Also, I had to remove the extended numerals from the standard key (IV, IX, XC, etc). I moved these into a separate key.

My solution is here: [https://pastebin.com/mzAmx9xw](https://pastebin.com/mzAmx9xw)

---

<div class="post-metadata">

**Author:** ![honmanyau](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/honmanyau/32/45787_2.png) [@honmanyau](https://forum.freecodecamp.org/u/honmanyau)\
**Post date:** [March 29, 2017, 1:47pm UTC](https://forum.freecodecamp.org/t/solved-roman-numeral-converter-lost/101074/2 "2017-03-29T13:47:15Z")

</div>

Hello there!

I don’t think the change in the arrays that you have mentioned is an issue—althought I personally prefer using the simpler arrays (without the 4 × 10n and 9 × 10n) since they are derived from the the Roman numerals of 1 × 10n, 5 × 10n, and 10 × 10n.

To be very honest, I haven’t looked through every single line of the code because I got lost when I got the following block:

```
for (var z = numForKey.length; z != 0; z--) {
    ...
}

```

You are already iterating through `numForKey` with the preceding block of code `for (var n = 0; n < numForKey.length; n++) { ... }` for each number in `multipliedNums`. Since `numForKey` and `lettersForKey` are mirrored in terms of index, I think you simply need to modify the logic in the preceding block of code:

```
`for (var n = 0; n < numForKey.length; n++) {
    ...
 }`

```

Some nudges I have, in no specific order and/or completeness, are:

- Before you do anything else, have a look at the current console output first. Then remove the first block of code mentioned above (the one with `var z = numForKey...`) and check again. And finally see what output you will get with, say, 66
- The logic you have in the second block of code mentioned above does not currently take into account 6 × 10n, 7 × 10n, and 8 × 10n
- You could convert `numForKey` and `lettersForKey` into arrays of arrays such that the index of a sub-array is the same as the position of a digit in `multipliedNums`—you may even want to clean up your code for multiplication if you choose to (not necessary to make things work)
- As suggested at the beginning, you could also use the old arrays without 4 × 10n and 9 × 10n and derive 4 × 10n’s and 9 × 10n accordingly. Remember that left is subtraction and right is addition, while taking into account 4 × 10n and 9 × 10n

---

<div class="post-metadata">

**Author:** ![joshamore](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/joshamore/32/50035_2.png) [@joshamore](https://forum.freecodecamp.org/u/joshamore)\
**Post date:** [April 1, 2017, 10:56pm UTC](https://forum.freecodecamp.org/t/solved-roman-numeral-converter-lost/101074/3 "2017-04-01T22:56:02Z")

</div>

Thanks for the thorough reply! You helped me find some of my _many_ mistakes.

I did end up removing the additional numerals and numbers from my keys. Instead, I moved them to a separate key. I found this solution more condensed than adding the logic to convert numerals requiring a subtracting element.

Again, thanks for all the help!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:25pm UTC](https://forum.freecodecamp.org/t/solved-roman-numeral-converter-lost/101074/4 "2021-01-16T17:25:39Z")

</div>

Closed on January 16, 2021, 5:25pm UTC


