My problem with freeCodeCamp - Roman Numeral Converter

My problem with freeCodeCamp - Roman Numeral Converter
0

#1

Tell us what’s happening:
Why are the “helpful hints” for the challenge so different to code solutions given?

I’ve spent a while typing out the code below (my fault), but felt there must be a better way.

Looking at even the basic solution, its so elegant yet there is no where that tells you how to write elegant code.

I guess I just want to rant.

But also want to ask, where can I learn about writing good code and coming up with elegant solutions?

Your code so far

function convertToRoman(num) {
  var numArr=[];
  var romArr=[];
  numArr=num.toString().split("");
  
  var ones=numArr[numArr.length-1];
  var romOnes=0; 
  if (ones==0) {
    romOnes="";
  } else if (ones>0 & ones<4) {
    romOnes="I";
    for (i=0; i<ones-1; i++) {
      romOnes = romOnes + "I";
    }
  } else if (ones==4) {
    romOnes="IV";
  } else if (ones==5) {
    romOnes="V";
  } else if (ones>5 && ones<9) {
    romOnes="V";
    for (i=0; i<ones-5; i++) {
      romOnes = romOnes + "I";
    }
  } else if (ones==9) {
    romOnes="IX";
  }
  
  if (numArr.length>1) {
    var tens=numArr[numArr.length-2];
    var romTens=0; 
    if (tens==0) {
      romTens="";
    } else if (tens>0 & tens<4) {
      romTens="X";
      for (i=0; i<tens-1; i++) {
        romTens = romTens + "X";
      }
    } else if (tens==4) {
      romTens="XL";
    } else if (tens==5) {
      romTens="L";
    } else if (tens>5 && tens<9) {
      romTens="L";
      for (i=0; i<tens-5; i++) {
        romTens = romTens + "X";
      }
    } else if (tens==9) {
      romTens="XC";
   }
  }
  
   if (numArr.length>2) { 
    var hundreds=numArr[numArr.length-3];
    var romHundreds=0; 
    if (hundreds==0) {
      romHundreds="";
    } else if (hundreds>0 & hundreds<4) {
      romHundreds="C";
      for (i=0; i<hundreds; i++) {
        romHundreds = romHundreds + "C";
      }
    } else if (hundreds==4) {
      romHundreds="CD";
    } else if (hundreds==5) {
      romHundreds="D";
    } else if (hundreds>5 && hundreds<9) {
      romHundreds="D";
      for (i=0; i<hundreds-5; i++) {
        romHundreds = romHundreds + "C";
      }
    } else if (hundreds==9) {
      romHundreds="CM";
    }
  }
  
 return num;
  }

convertToRoman(99);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

Not sure how to answer this question, but the helpful hints are just suggestions of functions and concepts you “could” use in a solution.

Not sure what you mean by “good code”, but elegant solutions come from experience and learning from other elegant solutions. When I say elegant, I mean the solution is readable (with variable names describing what the variable contains and function names which explain what the function does), DRY (not repetition of code), and efficient (no extra unnecessary looping or over use of memory resources).

Just keep practicing challenges and then rethinking and redoing the challenges until you come up with an elegant solution of your own. There are so many ways to solve most of these challenges.


#3

You do understand that your current solution above does not pass the tests?


#4

Yes i know its unfinished. I felt frustrated at the point i stopped at and that was when I looked at the hints. I will try and finish it this way and then try ‘an elagent solution’ after.

Maybe i will start storing my various solutions on github to see my progress.


#5

Thank you for this randell. Really puts things in perspective.


#6

That is a fantastic idea. You could have branches for the different versions.