Mission accomplished without helpfull functions

Mission accomplished without helpfull functions
0

#1

Hello,
I write this topic just to know how Project Roman Numeral Converter supposed to be made with Array.splice(), Array.indexOf() and Array.join() because i made it without any of them (spoiler alert it can solve the mission).

Here’s my code (again if you want do it by yourself don’t read that :slight_smile: ):

function convertToRoman(num, newNum) {
  var base = 0;
  if(num/1000 >= 1){
    base = 1000;
  }
  else if(num/100 >= 1){
    base = 100;
  }
  else if(num/10 >= 1){
    base = 10;
  }
  else if(num/10 < 1){
    base = 1;
  }
  
  newNum = newNum ? newNum : '';
  var mainNumber = num/base;
  var modulo = num%base;
  var fifthNumber = '';
  var firstNumber = '';
  var tenthNumber = '';
  switch (base){
    case 1000: firstNumber = "M";
      fifthNumber = 'VV';//i know that it's not exactly this
      tenthNumber = 'XX';//i know that it's not exactly this
      break;
    case 100: firstNumber = "C";
      fifthNumber = 'D';
      tenthNumber = 'M';
      break;
    case 10: firstNumber = "X";
      fifthNumber = 'L';
      tenthNumber = 'C';
      break;
    case 1: firstNumber = "I";
      fifthNumber = 'V';
      tenthNumber = 'X';
      break;
  }
  
  var repetition = Math.floor(mainNumber);
  if(repetition < 4){
    for(var i=0; i<repetition; i++){
      newNum+=firstNumber;
    }
  }
  if(repetition === 4){
    newNum+=firstNumber;
    newNum+=fifthNumber;
  }
  if(repetition > 4 && repetition < 9){
    newNum+=fifthNumber;
    var difference = repetition-5;
    for(var j=0; j<difference; j++){
      newNum+=firstNumber;
    }
  }
  if(repetition === 9){
    newNum+=firstNumber;
    newNum+=tenthNumber;
  }


  if(modulo > 0){
    return convertToRoman(modulo, newNum);
  }else{
    return newNum;
  }
}

#2

There’s no need to use anything really complex.Your solution works, but is a bit lenghty. Consider two arrays, both of the same length. One has all the romans symbols and the other have the numerical value, in order.

Going through a for loop, I compare the num value with the arrays and when a match is found, i substract this from num.
You get the correct string value in the end.

If you are curious, see my code here


#3

Thank you @Mizu, gonna try to do it again (without looking your code before i finished it). I didn’t imagine it with two Arrays at all, i just tried to think about one Array :).