JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Tell us what’s happening:
I think I have the basic logic of the solution, but the problem is that I cannot seem to find out how to convert the roman numeral array into a string.
It currently returns ['X', 'X', 'X', 'V', 'I']

I tried doing return [].join("") below the base case, and also return rem.join("") at the end of the if statement within the for loop. But I think that is converting the result into a string when it is calling the function back and it says rem.unshift... is not a function
Some guidance would be appreciated. My logic is still very cloudy.

Your code so far

function convertToRoman(num) {
  let romanIndex = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
  let arabicIndex = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  if (num === 0) {
    return [];
  } else {
  for (let i = 0; i < arabicIndex.length; i++) {
    if (num >= arabicIndex[i]) {
       let rem = convertToRoman(num - arabicIndex[i]);
       rem.unshift(romanIndex[i]);
       return rem;
      }
    }
  } 
  return "";
}
   
console.log(convertToRoman(36));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Link to the challenge:

Yes! I realized that rem.join(“”) is returning a string and it was causing problems for the unshift method.
I will try the .concat() approach. Thank you so much for your prompt advice!

My bad. Didn’t know I wasn’t supposed to post solutions in the forum.
Will keep in mind from now on.
I was wondering about result ""; at the end. It seemed like it wasn’t doing anything, but the code passed so I just left it there.
Thanks again for your help.

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