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

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

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

sjeasley
Автор

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.

JonathanWrightZA
Автор

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.

nishikantbawache
Автор

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

yvanneee
Автор

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

guyarnav
Автор

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;
}


zken
Автор

You literally snapped Ian! haha Great job as usual!

callmeFernie
Автор

Great. The videos are still valid. Thanks.

lewiszig
Автор

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);

Dman_Ranks
Автор

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

shaileshhd
Автор

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;
}

DannyYang
Автор

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.

isiaqridwanbukola
Автор

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

Me in my 20s: Where did my code go wrong?

salazar
Автор

The curriculum of fcc is really difficult especially the algorithms

abudavid
Автор

awesome, thanks for walking through your thoughts

carlosvenegas
Автор

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.

cosslogan
Автор

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

staaustin
Автор

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

JD-hqkn
Автор

Thank you for making this it was really good

fujisan
Автор

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 {

toRest -=romanVal[count]
}
}
return romanNum.join("");
}

convertToRoman(36);

luisjosebolile
visit shbcf.ru