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