Skip to main content
View on YouTube to comment and like!

Syntax Video

3 Devs Play CLASH OF CODE! JavaScript Challenge

Chapter 1 00:00

Transcript

Welcome to Clash of Code. In this video, all three of us devs are going to compete to see who can solve this JavaScript problem the fastest. So, we're going to be all dropped into the same room. We have no idea what problem we're about to try and solve, but whoever does it the fastest will win. Is everyone ready? I'm ready. My nickname growing up was number one the fastest.

So, that's me. Let's do it. On the left hand side here is the problem description. It then tells you what your input is going to be. And you can see that the input is actually multiple lines. So it's going to give us three different lines. Each line has A, B, and C on it. And then your expected output is the total based on this problem description. On the right hand side, you can see your code. And you can see they already have it set up for you. So there's this read line function that's built into Clash of Code, and that's what's reading each one of these three lines on the left. So this code lines 6 through 8 is already ready to go. It's read A, B, and C in for us. Now it's your job to write the code to get the answer total in console log it out.

Okay. All right. Let's do it. All right. Let's go. Okay. So the problem says you are given three numbers A, B, and C. Starting from zero, compute the total. If the number is even, add it to the total. If the number is odd, subtract it from the total. It should be easy, right? So this says uh if the number is even, add it to the total. So I think we have to do A, B, and C. So yeah, we start with total. starts off as zero and then if a is even so we'll do mod 2 is zero then we'll increase total by a um so let's go ahead and say let total is equal to start and then we can say if and then let's just raw do this quick here if a modulo 2 we do total minus a if the number is odd subtract it from okay and then we can just copy these total Plus this is the exact type of thing that I make some dumb mistake on um really quickly. All right. So here

we got the accumulator. We are going to return the accumulator plus if the value is odd. So we'll use the module modulo operator. If the value is cleanly divisible by two. That's how you tell if a if a number is even or odd then it is even.

So then we will return the value. Otherwise we are going to return the value times 1. We're going to subtract it if b is odd. So if we do mod 2 does not equal zero. Oh I have to do this for every single one. I'm going to turn this into a function. Uh, so then we subtract it from the total and that's everything. So actually I need to pull this into a function that like modifies total. Found zero expected three. I already got the first one wrong. Okay. So maybe I'm just reading this incorrectly. If the number is even. So oh yes that is the problem here. So if a is modulo 2 let's do this turnary here. Total is equal to total minus 2 or total + 2. Play all test cases. Found -3 expected three.

What am I doing wrong here? So I take A, B, and C. If the number is even, add it to the total. Oh, I forgot to start with zero. Same thing. What? This is like the simplest thing in the world. Okay, I'm trying to get fancy with it. And this will take in a value. And then we need to call update total for AB and

C. If the number is even, add it to the total. Maybe I need an else. Oh, I guess else if doesn't even matter here. If the number is odd, subtract it. Oh, I did I subtract it from the total. Oh, I need to log out total here. That's the That's I had the hard-coded value. All right. Yeah, this is it. There's that. This is it. Done. Done.

console.log. Ah, freaking CJ caught it already. If it is divisible by two, it's even. So, if the number is even, add it to the total. This is just the manual way. Who cares about loops right now or functions? This is the dumbest possible solution to this. And I'm slow.

So, okay. I think once Wes and Scott figure out what it's asking for, they'll get it because this is a this is so Scott submitted. Yeah, it's a fairly simple problem. That was for for me my struggle was just straight up reading. Is it backwards? Oh, divisible by two is equal to zero. Then we return the val otherwise we return the value times -1 and I'll get that console log out of there. We will play it. What the [Laughter] hell? Oh, I got to add some debugging here, man. Say if is even return the accumulator plus the value otherwise we return the accumulator minus the value. I'm embarrassed at this. There we

go. Woo. Got it. Submit. Hold on. I got to unmute them. They're laughing at me. We are laughing at you. I'm sorry. I can't believe I can't believe I goofed that up so bad. I got I got so fancy. Yeah, that's why I was laughing. I was hearing you talk about your accumulator and I was like, "Oh, he he went too fancy on this." I I was trying to do it in a one line. Okay, so should we look at CJ's code first? Yeah. Uh, yeah.

Pretty simple. So, I have a Oh, man. Yeah. This is scalable, CJ. Just wait till you see mine, Wes. We're not trying to scale. We're trying to get to market fastest. And I got there three three minutes faster than you. I also use Modulo. I just did three straight turnaries cuz you're always going to have three inputs. Who cares?

Just this uh you're just modifying variables left and right. What aboutility? You could have a function. Who gives a [ __ ] about immutability in this? I don't. That's not the goal. I had mine like a oneliner in like 10 seconds and then I forgot that the value divided by two you have to check if it's zero. I thought that returned true or false. So it was the opposite. The lesson learned here is uh dirty code is going to work better than like trying to figure out a reduce too, I think. All right, that was super fun. Let's do another one. You guys ready? Yes. All right, now I got the hang of this. Hey, Randy, you could add some like Mario Kart like beep beep beep. Just a thought. That'd be fun.

Write a function that takes an array of numbers and returns the sum even numbers in the array. I'm not even going to read the rest. We're just going to do it. So, we're going to have the sum out here started off as zero. And then if this number we read in is divisible by two uh then sum gets increased by x and then afterwards we should log the whole sum. That should be it. Yes. Let's get rid of the stupid for loop. Nobody uses for loops anymore. So const total is equal to inputs dot reduce. We're going to start with zero and take the accumulator and the value and if the value is function takes array of numbers is even is divisible by two then oh I got don't forget the

equals zero then we are going to return the accumulator plus the value otherwise we're just going to return the accumulator cuz we're not taking it just no found nothing that's cuz I got to console log it so that we need a sum let sum equals zero if if we have a sum then we say sum uh is equal to if there is an even number so if x modulo 2 is even uh do nothing so sum is equal do sum uh plus zero otherwise we have sum plus x and then we console log out sum play all test case test case test case test case goj 34 seconds what the see the problem

with west is that he he's not thinking dumb enough you you got to think no he overthinks the dumb est possible solution the uh easiest whatever and it just so happens that all of my solutions I got to parse it okay now we got to take this why is he got to parse make this a explicit function what's he parsing um so const val is equal to parse int

valu this a value let's see what we get now six success success success success success as submit. Boom. Still last. Damn it. Who uses for loops though? Come on. The code is set up for it. All we needed was an accumulator outside the for loop and then inside the for loop an if statement. That's literally all we needed. Oh man, sounds like a memory leak waiting to happen. I simply just, like you said, created a sum outside and then did a turnary where it's I don't know why I did sum plus zero. Um, that's like the dumbest thing I've ever written in my entire life. Explicit for you.

Yeah. Yeah. I was just I was just like talking through it in my brain and I didn't even notice it until right now. But hey. Yeah. You could have done like plus equals. But I like that you're doing turnaries. I think you're ready for shortest mode cuz you've been writing like really tur solutions. Oh yeah. I deleted the stupid for loop and used the reduce like a proper programmer. Use your brain on reduce.

Yeah. Yeah. Somebody needs to take down CJ. We all know he's cheating. All of us. All he does is to freaking loop over numbers and add them together. No real skills in JavaScript at all. Yeah, let's get it. The last one. Yeah, you I want you guys to dethrone me. All right. Okay, this is a longer description. This could get interesting. In a world where people get powers by eating magical fruits, you'll be given in fruit types.

There are three types of fruit. Logia, parmesia, and zone. Logica, paramia. What is this? Zelda type [ __ ] This is so stupid. A loia fruit is valued for 100 belly. Oh my gosh. I don't watch Star Wars or any of these things. So, here's what I'm going to do. I'm going to have a lookup table of like values and inside of there we're going to have each one of these and each one is going to have a value. So, Logia is 100 belly, is zone is 25, and fruits is what? Man, this is the hardest part cuz CJ probably knows what fruits is already. Oh my god.

Fruits is Oh Jesus. Um if fruits is standard is just one of the fruits. So you're just reading one of the fruits. Okay. Uh sum is equal to fruits uh if fruits is equal to oh man this string this word at the end sum plus the word belly. We just need a total value.

I'll say if fruits is equal to loia then total plus equals 100. Fruits is equal to that then sum plus 100 otherwise um null no sum is equal to sum not sum plus zero. Okay. If fruits is equal to paramia zoan 75 I better have read this question correctly cuz I don't have time to go back and read again 75 25 consult log total why expected 200 believe oh we got to console log the total plus Found nothing. Why? Okay.

Boom. Bracket. My bad. That was an honest mistake. Doubled. I still lost. You guys did so much better than 3 seconds. Scott, you beat Oh, I beat West by three seconds. Wow. Wow. Wow. Perpetually number two. I'll take Yes. Perpetually last.

You guys did much better that time though. Yeah. I think we're realizing that this is like garbage code in these. So you just have to write I think half my time is still trying to figure out the problem or it's it's saying all these weird words, but what is it really asking for? Like if you look at my solution, it really just it's like a lookup table and then you could have I have a bunch of if statements, but you have a lookup table. I was going to do a lookup table, too. That's a good call.

Yeah. So on mine, I did the dumb if statement. Turn it over. Yeah. Yeah, that I just like it. Just like how it works. Um so yeah, that's what I did. Nice. Yeah, I did the same thing as well. Three if statements and uh but but plus equals instead of uh the turnary.

Yeah. All right, thanks everyone so much for watching. If you think you could beat us, definitely head over to Clash of Code and give some of these a try. Uh but I'm the reigning champ, so congratulations. I appreciate it. If you enjoyed this, definitely check out our CSS battle that we did here or maybe uh check out one of the u trivia games that we played in the past. And uh we'll see you in the next one.

Quote it with credit and a link. Quoting Syntax