Roman Numeral Converter: Am I on the right track?

Roman Numeral Converter: Am I on the right track?
0

#1

Hello. I’m currently working on the roman number converter problem and wondered if I am on the right track. Thanks!

 var arabNum = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
 var listRom = [['I', 'II', 'III', 'IV', 'VI', 'VII', 'VIII', 'IX'],['X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],['C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'],['M', 'MM', 'MMM', 'MMMM', 'MMMMM', 'MMMMMM', 'MMMMMMM', 'MMMMMMMM', 'MMMMMMMMM']];

function convertToRoman(num) {
  //convert num to array
  var numArr = Array.from(num);
  //reverse the array
  var revNum = numArr.reverse();
  //create an empty array for roman numeral
  var romanNum = [];
  //Go through reversed num digit by digit
  for (var i = 0; i < arabNum.length; i++) {
    if (arabNum[i] == revNum[i]) {
      romanNum.push(listRom[0][i]);
    } 
  }
  
  //More code...
  
  
  var newNum = romanNum.reverse().join();
 return newNum;
}

convertToRoman(36);


#2

You need to learn to use the console for debugging. Right now you’re programming blind. The line

var numArr = Array.from(num);

doesn’t do what you’re expecting. Right now it creates an empty array, which you can see in the console if you add the following line under it:

console.log(numArr);

Using a string to separate the digits is a good idea, you’ve just forgotten that num is a number and it needs to be converted to a string before you can use it this way.

I think your method could work. I would be partial to calculating the roman digits instead of listing them all since there is a logic to it, but if your method works it’s fine.


#3

I’m still shaky with debugging. Need more practice with it. I’ve also tried this method for solving the problem. Right now it’s not working but wondering if I’m on the right track with it.

var numTable = [
  [1000, 'M'],
  [900, 'CM'],
  [500, 'D'],
  [400, 'CD'],
  [100, 'C'],
  [90, 'XC'],
  [50, 'L'],
  [40, 'XL'],
  [10, 'X'],
  [9, 'IX'],
  [5, 'V'],
  [4, 'IV'],
  [1, 'I']
];

//Create a function which contains a for-loop

function numConverter(num) {
  var roman = [];
  if (num === 0) {
    return '';
  } else {
    for (var i = 0; i < numTable.length; i++) {
      if (num >= numTable[i][0]) {
        roman.push(numTable[i][1]);
        num -= numTable[i][0];
      }
    } return roman.join();
  }
}

#4

That’s a good approach too. It almost works. There’s a little problem with the loop that means it can’t use the same roman number twice.

Google the shortcut for the console in your browser and use console.log extensively to see what’s happening to your variables. You’re almost there.


#5

I think I have a solution to the challenge as it is producing the desired results. It is not being picked up by fCC, however. Any idea as to why? Thanks!

//Create an array with subarrays. Each subarray contains an arabic numeral and its
//roman counterpart
var numTable = [
  [1000, 'M'],
  [900, 'CM'],
  [500, 'D'],
  [400, 'CD'],
  [100, 'C'],
  [90, 'XC'],
  [50, 'L'],
  [40, 'XL'],
  [10, 'X'],
  [9, 'IX'],
  [5, 'V'],
  [4, 'IV'],
  [1, 'I']
];

//Create a function which contains a for-loop

function numConverter(num) {
  var roman = '';
  for (var i = 0; i < numTable.length; i++) {
    while (numTable[i][0] <= num) {
      roman += numTable[i][1];
      num -= numTable[i][0];
    }
  }
  return roman;
}

//test here:

numConverter(36);
numConverter(68);

#6

Ah never mind. Figured out what it was.