Build a Roman Numeral Converter Project

Hey guys, I’m currently working on the roman numeral converter project. I looked into the example source code and I find this particular code odd:

const convertToRoman = num => {
  const ref = [
    ['M', 1000],
    ['CM', 900],
    ['D', 500],
    ['CD', 400],
    ['C', 100],
    ['XC', 90],
    ['L', 50],
    ['XL', 40],
    ['X', 10],
    ['IX', 9],
    ['V', 5],
    ['IV', 4],
    ['I', 1]
  ];
  const res = [];

  ref.forEach(function (arr) {
    while (num >= arr[1]) {
      res.push(arr[0]);
      num -= arr[1];
    }
  });

  return res.join('');
};

From what i can see,the forEach function is accessing arr by the column number instead of the row number… So arr[1] accesses the 2nd column which contains the numbers, while arr[0] accesses the 1st column which contains the roman numeral equivalent…

My question is, isn’t it supposed to be accessed by row instead of column ? So arr[1] should be [‘CM’, 900] and so on… Am I missing something here ? Please correct me if I’m understanding this the wrong way. Thank You!

Do not, do not, do not read the source code of other people’s certification projects before completing your own. You absolutely should not be using that code before you write your own.

The forEach callback arr parameter is passed the nested arrays.

const arrayOfArrays = [
  ["one", 1],
  ["two", 2],
  ["three", 3],
];

arrayOfArrays.forEach((nestedArray) => {
  console.log(nestedArray);  // ["one", 1] ["two", 2]  ["three", 3]
  console.log(nestedArray[0]); // "one" "two" "three"
  console.log(nestedArray[1]); // 1 2 3
});

I’m sorry I wasn’t made clear that I couldn’t read the example’s source code…

So this is specifically because of the forEach function ? thank you so much for the info!

Yes, that is how a lot of the array methods work. The array method callback is passed each of the array elements as the first argument.

The APIs look something like (element, index, originalArray)

The main point is just that you should come up with your own solution.

But reading code can be very useful as well, as seen by the question it sparked and the knowledge you might have gained in the process.

When learning to code, part of what you are learning is the problem-solving process, which you won’t learn if you do not go through that process.

I’d double check the academic honesty policy

You can do research while doing these projects but shouldn’t be looking at other people’s solution code