Roman Numeral Converter is working properly on Repl.it, but it does not pass any tests and only goes to 3,999

I am working on the Roman Numeral Converter project found here. I cannot get it to pass any tests, but it seems to work fine on Repl.it. I also cannot get it to go over 3,999 on Repl.it. Thanks for any help.

function convertToRoman(num) {
  let numArr = num.toString().split("");

  let regNumArr = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];

  let romNumArr = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];

  let j = 0;

  let romNumArrFinal = [];

  let romNumStrFinal = "";

  for(i = numArr.length - 1; i >= 0; i--) {
    let tempNum = numArr[i]  * Math.pow(10, j);
    
    let firstDigit = Number(numArr[i]);
    
    switch (firstDigit) {
      case 1:
      case 4:
      case 5:
      case 9:
        romNumArrFinal.unshift(romNumArr[regNumArr.indexOf(firstDigit * Math.pow(10, j))]);
        break;
      default:
        for (let x = firstDigit; x > 0; x--) {
          romNumArrFinal.unshift(romNumArr[regNumArr.indexOf(1 * Math.pow(10, j))]);
        }
        break;
    } 

    j++;   
  }

  return romNumStrFinal = romNumArrFinal.join("");  
}

convertToRoman(2);

In order to get your repl.it environment to behave the same as the freeCodeCamp environment, add "use strict" at the top of your code. This will help you figure out the first problem.

1 Like

Thanks a lot! That “use strict”; at the top of the Repl.it console really helped a lot. I was able to pass all of the tests, but this still does not work with numbers larger than 3,999. I will pursue this a little longer before moving to the next challenge.

"use strict";

function convertToRoman(num) {
  let numArr = num.toString().split("");

  let regNumArr = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];

  let romNumArr = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];

  let j = 0;

  let romNumArrFinal = [];

  let romNumStrFinal = "";

  for(let i = numArr.length - 1; i >= 0; i--) {
    let firstDigit = Number(numArr[i]);
    
    switch (firstDigit) {
      case 1:
      case 4:
      case 5:
      case 9:
        romNumArrFinal.unshift(romNumArr[regNumArr.indexOf(firstDigit * Math.pow(10, j))]);
        break;
      default:
        if (firstDigit >= 5) {
          romNumArrFinal.unshift(romNumArr[regNumArr.indexOf(5 * Math.pow(10, j))]);
            
          firstDigit -= 5;

          for (let x = firstDigit; x > 0; x--) {
            romNumArrFinal.splice(1, 0, romNumArr[regNumArr.indexOf(1 * Math.pow(10, j))])
          }
        }
        else {
          for (let x = firstDigit; x > 0; x--) {
            romNumArrFinal.unshift(romNumArr[regNumArr.indexOf(1 * Math.pow(10, j))])
          }
        }
        break;
    } 

    j++;   
  }

  return romNumStrFinal = romNumArrFinal.join("");  
}

convertToRoman(2020);

It’s good that you used a spoiler since this it is a working example of one of the projects, but we can’t copy paste and see what’s happening. Are you getting an error of some sort?

I will also note that 4,000 is another break point in roman numerals because 5,000 is V with lineover so 4000 = MV

edit: after I wrote that I realized its because you are searching for 4,000 in regNumArr when you get to 4 * 10 ^3; so you’ll get an error (-1)

1 Like

As @pjonp says you normally need non-standard symbols for numbers bigger then 3999 (vinculum-based, like X̅, or apostrophus-based, like IƆ). But there’s no standardised system, it’s all pretty ad-hoc, it’s more sensible to just stop at 3999 as numerals aren’t useful as you get larger numbers: not being able to go above that shouldn’t be an issue

2 Likes

Thanks a lot! I will stop at 3,999 since there is no standardized system for numbers beyond this. I was actually thinking of going to 1 trillion, but I’ll just keep what I have. Thanks again!