Roman Numeral Converter - Stuck on error [solved]

Roman Numeral Converter - Stuck on error [solved]
0

#1

Hi!

I have the following error: undefined is not an object (evaluating ‘arr1[i][0]’)
, but can’t figure out where the problem in my code is…

Here is my code:

function convertToRoman(num) {

  var arr1 = [[1000,"M"],[500,"D"],[100,"C"],[50,"L"],[10,"X"],[5,"V"],[1,"I"]];
  var arr2 = [];

  for (var i=0; i= arr1.length; i++) {

    if (arr1[i][0] < num) {
      num -= arr1[i][0];
      arr2.unshift(arr1[i][1]);
    } else if ( num = 1) { 
      return "I";
    } else { return "0" ;
    }
  }
 return arr2.joint('');
}


#2

you have a weird conditional ‘i = arr1.length’

which is not a conditional at all as you are assigning the length of arr1 to i

I think you meant to write
i < arr1.length ?


#3

Just noticed a couple of other errors, thanks!


#4

Managed to fix the bugs :slight_smile:

function convertToRoman(num) {

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

  for (var i=0; i < arr1.length ; i++) {
      
      
     while (num > arr1[i][0]) {
     		 num -= arr1[i][0];
    		 arr2.push(arr1[i][1]);
      }
      
      if (num === arr1[i][0]) {
      arr2.push(arr1[i][1]);
      return arr2.join("")
      }
      }
    }