Project Euler Problems 1 to 100 - Problem 89: Roman numerals

Tell us what’s happening:
Not sure what I’m getting wrong about this problem, but I think it’s an understanding of the question.
I’ve passed the testArray2, but not the testArray1.

Getting 21 instead of 19.
console.log of difference, original, value, minimal, total count.
4 XIIIIII = 16 = XVI 4
2 XXXXVI = 46 = XLVI 9
2 CCCCXX = 420 = CDXX 11
5 DCCCCIIII = 904 = CMIV 18
3 MXVIIII = 1019 = MXIX 21

Not sure I understand why they point out that no digit will repeat more than 4 times, yet in the testArray1 they included that first. Not sure how else there is to interpret these numbers.

Threw in the solution on the hint page just to see the difference and logged some differences:
XIIIIII becomes XIVII for the answer? This isn’t the minimal version! It’s not even given in the question’s examples. Feels like an error?

Your code so far

Problem 89: Roman numerals
For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a best way of writing a particular number.

Numerals must be arranged in descending order of size.
M, C, and X cannot be equaled or exceeded by smaller denominations.
D, L, and V can each only appear once.
In addition to the three rules given above, if subtractive combinations are used then the following four rules must be followed.

Only one I, X, and C can be used as the leading numeral in part of a subtractive pair.
I can only be placed before V and X.
X can only be placed before L and C.
C can only be placed before D and M.
For example, it would appear that there are at least six ways of writing the number sixteen:


However, according to the rules only XIIIIII and XVI are valid, and the last example is considered to be the most efficient, as it uses the least number of numerals.

The array, roman, will contain numbers written with valid, but not necessarily minimal, Roman numerals.

Find the number of characters saved by writing each of these in their minimal form.

Note: You can assume that all the Roman numerals in the array contain no more than four consecutive identical units.
Seems kinda lame tbh
Okay...wait, I get it.  silly rules.  Correct version might not be minimal?
No, I was right?  I got the 3rd test passed but not the second?
Overcounting?  Meaning I saved two too many digits?
function romanNumerals(roman) {
  let count = 0;
  for (let rom of roman) {
    let num = romanToDecimal(rom);
    let sym = decimalToRoman(num);
    let dif = rom.length - sym.length;
    count += rom.length - sym.length;
    if(dif) console.log(dif, rom + " = " + num + " = " + sym, count);
    //console.log(num + " = " + sym);
  return count;

function romanToDecimal(str) {
  let count = 0;;
  for (let sym of R) {
    while (str.includes(sym[0])) {
      //console.log(str, sym);
      str = str.replace(sym[0], '');
      count += sym[1];
  return count;

function decimalToRoman(num) {
  let str = ''
  for (let sym of R2) {
    while (num >= sym[1]) {
      num -= sym[1];
      str += sym[0];
  return str;


// Only change code above this line
const R = [
  ['IV', 4],
  ['IX', 9],
  ['XL', 40],
  ['XC', 90],
  ['CD', 400],
  ['CM', 900],
  ['I', 1],
  ['V', 5],
  ['X', 10],
  ['L', 50],
  ['C', 100],
  ['D', 500],
  ['M', 1000]
const R2 = [...R].sort((a, b) => b[1] - a[1]);

const testNumerals1 = [

Your browser information:

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

Challenge: Project Euler Problems 1 to 100 - Problem 89: Roman numerals

Link to the challenge:

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