Roman Numeral Converter help for better iteration

Tell us what’s happening:
I have almost completed this challenge. And I got all the test to pass by add values 2000 = MM and 3000 = MMM to my romanNumerals object. Any ideas on how to get values > 1999 to repeat M without adding 2000, and 3000 to my romanNumeral object.

   **Your code so far**

function convertToRoman(num) {
 /*Create a table of Roman numerals */
 const romanNumerals = {
      0: '',
     1 : "I",
      2: "II",
      3: "III",
      4: "IV",
      5: 'V',
      6: "VI",
      7: "VII",
      8: "VIII",
      9: "IX",
     10: "X",
     20: "XX",
     30: "XXX",
     40: "XL",
     50: "L",
     60: "LX",
     70: "LXX",
     80: "LXXX",
     90: "XC",
     100: "C",
     200: "CC",
     300: "CCC",
     400: "CD",
     500: "D",
     600: "DC",
     700: "DCC",
     800: "DCCC",
     900: "CM",
    1000: "M",
    2000:'MM',
    3000: 'MMM',
 }
  /* Convert Number to String */
 const numArr = num.toString().split('').map(ele => ele= [ele]);

 /* Convert Numbers to place Values */

let placeValue = [];
 for(let i = 0; i < numArr.length; i++ ){
  placeValue.push(numArr[i] + '0'.repeat(numArr.length - 1 - i))
}

/*Replace place values of '0' repeating > 1 time to '0 */
for(let ele in placeValue){
 if(ele === '0'> 1){
   ele = 0;
 }
};

/*Create a variable to hold romanNumeral string */
let romanNumeral = ""

/* Found cooresponding roman numeral value and used array.map and array.join to return roman numeral string */
romanNumeral =(placeValue.map(function(element){
 if(element > 1000){
  return  element = romanNumerals[element].repeat(element/1000)
 } else{
   return element = romanNumerals[element]
 }
 })
).join('')

console.log(romanNumeral)



return romanNumeral;
}

convertToRoman(36);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36

Challenge: Roman Numeral Converter

Link to the challenge:

I’m taking look at your code and I noticed

for(let ele in placeValue){
 if(ele === '0'> 1){
   ele = 0;
 }

For (in) loops are problematic when it comes to accessing things in a particular order and are simply slower than alternatives, I would recommend replacing the in with of which is guaranteed to do things in numeric order.

Generally speaking most any other type of loop work well with array like objects

1 Like

You need to remove the repeat method in this
return element = romanNumerals[element].repeat(element/1000)
and it’ll pass

So my final code I changed
“romanNumeral =(placeValue.map(function(element){
if(element > 1000){
return element = romanNumerals[element].repeat(element/1000)
} else{
return element = romanNumerals[element]
}
})
).join(’’)”
to
"romanNumeral = (placeValue.map(function(element){
return element = romanNumerals[element]
})
).join(’ ').

I was hoping to find a way to get the correct roman numeral for values > 1999 without having to add 2000 = “MM” and 3000 = “MMM” to my romanNumerals object.

Thanks. I will be making that change.

You can take the 3000 and 2000 out. Your repeat was on the right track for that, but instead of saying romanNumerals[element].repeat(element/1000) you would say romanNumerals[1000].repeat(element/1000).

This makes since because when element is 3000 then what you are practically saying is give me three M(s) or three 1000(s) and to that you simply access your object at the key of "1000" and it will give you three M(s) as element/1000 or more specifically 3000/1000 equals 3

1 Like

THANK YOU!!! I am a novice, so it is reassuring that I was very close to solving this problem. I refactored my code and this worked (like you said lol). I appreciate the help.