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!
Хобби
Bruh, the fact that you took all this time to walk us through this.... Man, you the real MVP bro. Straight up.
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.
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));
I deeply understand now the use of += thing by this tutorial. so grateful for this sir
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.
what a simple solution to a complex problem, thanks for a great tutorial !
Great. The videos are still valid. Thanks.
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);
awesome, thanks for walking through your thoughts
Brilliant tutorial, thank you!
You literally snapped Ian! haha Great job as usual!
Snapped? LoL What do you mean? Thank you for watching.
This was an amazing tutorial!
Thank you. I'm happy to hear you enjoyed it.
Great job! Thanks for everything.
Thank you for watching Aaron.
This is a good pattern for beginners, we can also use objects to solve, which will reduce codes.
Others in their 20s: where did my relationship go wrong?
Me in my 20s: Where did my code go wrong?
the best really learnt alot
Thank you for making this it was really good
Thank you again Fujisan.
The curriculum of fcc is really difficult especially the algorithms
Agreed. Programming is difficult.
its alive!
Thank you!!!
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.
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);
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..
Recursion or a for loop should be quicker but this way is very easy to understand
Thanks Amazing tutorial
Thank you for watching Abu.
I would like to point that if you invert the order you wont need the seconds validations
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!
For the first time I saw you on top of the screen
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.
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()
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));
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