Roman Numeral Converter - JavaScript Algorithms and Data Structures Projects - Free Code Camp

Поделиться
HTML-код
  • Опубликовано: 30 мар 2020
  • In this JavaScript algorithm and data structure project we create a roman numeral converter. This video constitutes one part of many where I cover the FreeCodeCamp (www.freecodecamp.org) curriculum. My goal with these videos is to support early stage programmers to learn more quickly and understand the coursework more deeply. Enjoy!
  • ХоббиХобби

Комментарии • 42

  • @sjeasley9594
    @sjeasley9594 Год назад +3

    Bruh, the fact that you took all this time to walk us through this.... Man, you the real MVP bro. Straight up.

  • @JonathanWrightSA
    @JonathanWrightSA 2 года назад +14

    I'm still wrapping my head around this stuff, but once I got the pattern you were going for by XC, I was able to write the rest of the script. Interestingly, this is exactly the format I was going for while I sat in the sun scribbling on a notepad with pen and paper, but just could not get it right. The "+=" aspect is what I'm still grasping and how to and when to use it.
    I need to buy you a beer someday. That is all.

  • @zken1856
    @zken1856 2 года назад +5

    Thank you Mr. Ian
    function convertToRoman(num) {
    let answer = "";
    let pairs = {
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1,
    }
    let keys = Object.keys(pairs);
    console.log(keys);
    while (num > 0) {
    let letter = "I"
    for (let i = 0; i < keys.length; i++) {
    if (num >= pairs[keys[i]]) {
    letter = keys[i];
    break;
    }
    }
    answer += letter;
    num = num - pairs[letter];
    }
    return answer;
    }
    console.log(convertToRoman(3999));

  • @yvanneee4454
    @yvanneee4454 Год назад +1

    I deeply understand now the use of += thing by this tutorial. so grateful for this sir

  • @nishikantbawache3390
    @nishikantbawache3390 3 года назад +1

    I didn't come across this tutorial in the first attempt. Looked all over to only find solutions which confused me more.
    Thanks a lot for taking it step by step, and building it into an easy to understand tutorial.

  • @guyarnav
    @guyarnav Год назад +1

    what a simple solution to a complex problem, thanks for a great tutorial !

  • @lewiszig5862
    @lewiszig5862 Год назад

    Great. The videos are still valid. Thanks.

  • @Dman_colts
    @Dman_colts Год назад +3

    I found this as a way easier method if this helps anyone stuck!
    function convertToRoman(num) {
    let romanNum = {
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1
    }
    let answer = "";
    for (let prop in romanNum) {
    while (num >= romanNum[prop]) {
    num -= romanNum[prop];
    answer += prop;
    }
    } return answer
    }
    convertToRoman(36);

  • @carlosvenegas8981
    @carlosvenegas8981 3 года назад

    awesome, thanks for walking through your thoughts

  • @DavePueyoCarter
    @DavePueyoCarter 2 года назад

    Brilliant tutorial, thank you!

  • @callmeFernie
    @callmeFernie 3 года назад +2

    You literally snapped Ian! haha Great job as usual!

    • @UsefulProgrammer
      @UsefulProgrammer  3 года назад +1

      Snapped? LoL What do you mean? Thank you for watching.

  • @arikelvara
    @arikelvara 4 года назад +8

    This was an amazing tutorial!

  • @aaronargottelopez3488
    @aaronargottelopez3488 4 года назад +2

    Great job! Thanks for everything.

  • @shaileshhd4964
    @shaileshhd4964 2 года назад +1

    This is a good pattern for beginners, we can also use objects to solve, which will reduce codes.

  • @salazar2441
    @salazar2441 Год назад +1

    Others in their 20s: where did my relationship go wrong?
    Me in my 20s: Where did my code go wrong?

  • @techgix
    @techgix 2 года назад

    the best really learnt alot

  • @fujisan0388
    @fujisan0388 4 года назад +1

    Thank you for making this it was really good

  • @abudavid5929
    @abudavid5929 3 года назад +8

    The curriculum of fcc is really difficult especially the algorithms

  • @guilleb9395
    @guilleb9395 2 года назад +1

    its alive!

  • @khachikgobalyan9172
    @khachikgobalyan9172 2 года назад

    Thank you!!!

  • @isiaqridwanbukola4856
    @isiaqridwanbukola4856 Год назад

    Thanks a lot, man.
    I was actually using an if statement only, l had to come to check my useful programmer when I noticed my line of code is getting longer and longer. I was writing my codes like this before:
    if (num > 14 && num < 19) {
    // return "X"+"V"+"I".repeat(num - 15)
    // }
    // if (num > 18 && num < 21) {
    // return "X"+"I".repeat(20 - num)+"X"
    // }
    // if (num > 20 && num < 24) {
    // return "X"+"X"+"I".repeat(num - 20)
    you know how tedious and long that could be before it can execute all the tasks, smiles.

  • @luisjosebolile
    @luisjosebolile 3 года назад +6

    Thanks ... this was difficult .... i made this
    function convertToRoman(num) {
    let toRest = num ;
    let roman = {I:1, IV:4, V:5, IX:9, X:10, XL: 40, L:50, XC:90, C:100, CD:400, D:500, CM: 900, M:1000};
    let romanVal = (Object.values(roman));
    let romanKey = (Object.keys(roman));
    let count = romanVal.length-1
    let romanNum =[]
    while(count >= 0){
    if(romanVal[count] > toRest){
    count--;
    } else {
    romanNum.push(romanKey[count])
    toRest -=romanVal[count]
    }
    }
    return romanNum.join("");
    }
    convertToRoman(36);

    • @anon8275
      @anon8275 Год назад

      i ended up finding a youtube video that broke this one down
      no offense to UP but i really dont like how this video turned out. i sat here and watched this video almost twice (and pulled my hair out the entire time)
      and kept writing onto the while loop and it kept breaking earlier parts of the loop (copied it LINE BY LINE AND IT WAS STILL BREAKING D:)
      i got to the end of the video and the 400-649 parts of the loop kept getting broken..

  • @alphagee_k
    @alphagee_k 5 месяцев назад

    Recursion or a for loop should be quicker but this way is very easy to understand

  • @abudavid5929
    @abudavid5929 3 года назад +1

    Thanks Amazing tutorial

  • @staaustin2970
    @staaustin2970 2 года назад +1

    I would like to point that if you invert the order you wont need the seconds validations

  • @amonrashidi6601
    @amonrashidi6601 7 месяцев назад

    I feel like you could’ve went about this in a much simpler way with a for loop that iterates through all the numbers and assigns there value somehow!

  • @JD-hq1kn
    @JD-hq1kn 2 года назад

    For the first time I saw you on top of the screen

  • @cosslogan1043
    @cosslogan1043 3 года назад +2

    Could you use a switch for these type of problems ? I haven't seen a lot of coverage of switch so far in the lessons. Also Thanks for these videos, very informative.

    • @janphilipwitt2785
      @janphilipwitt2785 3 года назад +3

      Yep you can :D
      function convertToRoman(num) {
      let singles=num.toString().split("").reverse()
      let ones=singles[0]
      let tens=singles[1]
      let hundreds=singles[2]
      let thousands=singles[3]
      let arr=[]
      switch (ones){
      case "1":
      arr.push("I")
      break;
      case "2":
      arr.push ("II")
      break;
      case "3":
      arr.push("III")
      break;
      case "4":
      arr.push("IV")
      break;
      case"5":
      arr.push("V")
      break;
      case"6":
      arr.push("VI")
      break;
      case "7":
      arr.push("VII")
      break;
      case "8":
      arr.push("VIII")
      break;
      case "9":
      arr.push("IX")
      break;
      }
      switch (tens) {
      case "0":
      break;
      case "1":
      arr.unshift("X")
      break;
      case"2":
      arr.unshift("XX")
      break;
      case"3":
      arr.unshift("XXX")
      break;
      case"4":
      arr.unshift("XL")
      break;
      case"5":
      arr.unshift("XXL")
      break;
      case"6":
      arr.unshift("LX")
      break;
      case"7":
      arr.unshift("LXX")
      break;
      case"8":
      arr.unshift("LXXX")
      break;
      case"9":
      arr.unshift("XC")
      break;
      }
      switch(hundreds){
      case "0":
      break;
      case "1":
      arr.unshift("C")
      break;
      case "2":
      arr.unshift("CC")
      break;
      case "3":
      arr.unshift("CCC")
      break;
      case "4":
      arr.unshift("CD")
      break;
      case "5":
      arr.unshift("D")
      break;
      case "6":
      arr.unshift("DC")
      break;
      case "7":
      arr.unshift("DCC")
      break;
      case "8":
      arr.unshift("DCCC")
      break;
      case "9":
      arr.unshift("CM")
      break;
      }
      switch(thousands){
      case "1":
      arr.unshift("M")
      break
      case "2":
      arr.unshift("MM")
      break;
      case "3":
      arr.unshift("MMM")
      break;
      }
      return arr.join("")
      }
      convertToRoman()

  • @DannyYang1
    @DannyYang1 Год назад +2

    I appreciate your videos. I went from BIG to small:
    function convertToRoman(num) {
    let romanNum = "";
    while (num > 0) {
    if (num >= 1000) {
    romanNum += "M";
    num -= 1000
    } else if (num >= 900) {
    romanNum += "CM"
    num -= 900
    } else if (num >= 500) {
    romanNum += "D"
    num -= 500
    } else if (num >= 400) {
    romanNum += "CD"
    num -= 400
    } else if (num >= 100) {
    romanNum += "C"
    num -= 100
    } else if (num >= 90) {
    romanNum += "XC"
    num -= 90
    } else if (num >= 50) {
    romanNum += "L"
    num -= 50
    } else if (num >= 40) {
    romanNum += "XL"
    num -= 40
    } else if (num >= 10) {
    romanNum += "X"
    num -= 10
    } else if (num >= 9) {
    romanNum += "IX"
    num -= 9
    } else if (num >= 5) {
    romanNum += "V"
    num -= 5
    } else if (num >= 4) {
    romanNum += "IV"
    num -= 4
    } else if (num >= 1) {
    romanNum += "I"
    num -= 1
    }
    }
    return romanNum;
    }
    console.log(convertToRoman(36));

    • @lewiszig5862
      @lewiszig5862 Год назад

      This is very readable to me as well. Only that at the end of your function, "num" would have been mutated down to zero. Am sure you can handle that. Thanks for sharing