[Solved] Roman Numeral Converter - Lost

[Solved] Roman Numeral Converter - Lost
0

#1

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

Can anyone nudge me in the right direction?

Cheers :smile:


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


#2

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

#3

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!