How to improve your reasoning skills/your program? [Roman Converter]

Hi everyone,

I just completed it the “Roman Converter”, I didn’t look at the hints and felt kind of dirty while writing my program, you can easily understand why:

function convertToRoman(num) {
  if (num < 10) {
    return unitConverter(num);
  } else {
    num = num.toString();
    var thousands = Number(num[num.length-4]);
    var hundreds = Number(num[num.length-3]);
    var tens = Number(num[num.length-2]);
    var unit = Number(num[num.length-1]);
   if (num <100) {
    return tensConverter(tens)+unitConverter(unit);
  } else if (num < 1000){
    return hundredsConverter(hundreds)+tensConverter(tens)+unitConverter(unit);
  } else if (num < 10000) {
    return thousandsConverter(thousands)+hundredsConverter(hundreds)+tensConverter(tens)+unitConverter(unit);
  } else {
    return "You broke the converter!"
  }
}
}

var unitConverter = function(num){
  var string = "";
  if (num<4) {
    for (i=1; i<=num; i++){
      string += "I";
    }
  } else if (num === 4) {
    string = "IV";
  } else if (num < 9) {
    string = "V";
    for (i=5; i<num; i++){
      string += "I";
    }
  } else {
    string = "IX";
  }

  return string;
};
var tensConverter = function(num){
  var string = "";
  if (num<4) {
    for (i=1; i<=num; i++){
      string += "X";
    }
  } else if (num === 4) {
    string = "XL";
  } else if (num < 9) {
    string = "L";
    for (i=5; i<num; i++){
      string += "X";
    }
  } else {
    string = "XC";
  }

  return string;
};

var hundredsConverter = function(num){
  var string = "";
  if (num<4) {
    for (i=1; i<=num; i++){
      string += "C";
    }
  } else if (num === 4) {
    string = "CD";
  } else if (num < 9) {
    string = "D";
    for (i=5; i<num; i++){
      string += "C";
    }
  } else {
    string = "CM";
  }

  return string;
};

var thousandsConverter = function(num){
  var string = "";
    for (i=1; i<=num; i++){
      string += "M";
    }
  return string;
};

I compared my code to the solution: freeCodeCamp Algorithm Challenge Guide: Roman Numeral Converter

While I understand the solution I was far from thinking about using an array, and the code looks much more elegant. What do you suggest me to do? I started web development <5 months ago and JavaScript even more recently. If you relate to my situation and got out of it, how did you do it?

Thank you a lot :pray:

Completely normal.

You built a working converter. And you understood the “better” solution. You’re learning. Pat yourself on the back and move on to the next challenge.

It’s good to read other code when you’re done to see how to improve, but don’t let that frustrate you because your code wasn’t as efficient or well-factored your first try. It’s not fair to yourself.

Congratulations on completing the algorithm without looking at the hints. That one took me awhile.