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

Syntax Video

We Tried Clash of Code… in Reverse Mode! (JavaScript Challenge)

Chapter 1 00:00

Transcript

Welcome to Clash of Code. In this video, Wes, Scott, and I are all going to compete to see who can solve these JavaScript problems the fastest. But today, we're doing reverse mode. So, we're not given any problem description. All we're given is a set of example inputs and a set of example outputs. So, given the inputs, given the outputs, we have to figure out what code to write to make that happen. Is everyone ready?

I'm ready to rock, I suppose. So, yeah. So stressful. So stressful. Let's do it. Okay. So left hand side we can see that given the input one we see one two we see two two five we see five five five. Okay. So given the number we need to output a pyramid of that number up to that number. So the first line of code here reads in the number um and then we just want a for loop.

So input one expected output one input two expected output to a 22. That means we're working with for loop here. for loop. So basically we have a number and we need to console log the number um but we want to we want it as a string as well. So two string repeat oh okay and we're going to say for the input as long as the uh for let i equals z is i is less than the input i ++ and then we're going to say we're going to push into d push and then we're going to push in first we're going to push in d um and then uh um um um and And then we'll repeat it I times.

And I think if we start I at one and go up less than or equal to length. That is the solution. There we go. Um and then I'm going to fill it with n.2 repeat d. I feel like CJ's already done. Play all test cases.

And then we are going to join it with a new line character. It's not split, is it? What is this? Uh, expected one found. It's a D. It's a string. I'm splitting the array by this. Oh, underscore I. We want to repeat it. I + 1.

Submit. Done. Damn it. How does CJ get it already? What a loser. What do you mean you found nothing? Console log, right? I'm console logging it. Is that how I output things? Yeah, your console log. We've done this like 11 times. Am I supposed to console log the to get the output or do I do print? Okay. D push. I'm pushing in. I'm pushing the D.

Oh, while I is greater than N. Oh, together with a string. What the hell was I doing that for? See, in CSS battles, he's BG color as his only move. And in the this coding game, his only move is a for loop. Unexpected token. Just submit. Just submit.

Just submit. Yeah. Yeah. Yeah. I see everybody waving their arms. Dude, phone a friend. You need to repeat in. Right now, you're just pushing pushing in a single in. Cuz if you're going to use an array, then you need to end up with an array where each element in the array has multiple items. So, you need multiple. You want to use the string repeat method. So, take your n, convert it to a string, and repeat it for your Well, or I could just do addition here, and it's not going to work for the first one. But there we go. Wait, how is this working?

I don't I don't need repeat. Oh, you got it. What did you just What did you How did you do this with addition? What? Okay, I'll have All right, I'll show since I got last place. I'll show you my code. All right, Scott's got the moves here now. I Yeah, look at me. No, I just took a string and I added the number to the string. String plus number equals string repeated. Yeah.

Oh. Oh, smart. Cuz string will concatenate. My fatal flaw was thinking about doing this in an array for some stupid reason. Like, oh, I'm going to turn it into array and then bump it out of the array. Why would I do that? This is a nice solution, though, because it's literally a great solution. Yeah, it's good cuz it's like a great solution.

It only took you six times as long to get there, but it's good. All right, let's see CJ's code. He obviously won it. So, Oh, he used a for loop as well. Yep. I'm a for loop boy today. I don't know why my brain's on a different planet today, but I was like, seems fine. Yeah, repeat. So yeah, from one up to n. I think the hard part is like they give you a number and so like I just turned it back into a string so that I could use the repeat method which is built into JavaScript.

So yeah, I made an array and I used the map the second argument of array from as a map argument and then you what you're doing you can access the index of that and and then I did what CJ was is is put it to string and then join that entire array. I'm big on like just get the data looking like what you want and then go and log it.

Yeah. Okay. Another one. Yes. Yeah. This Clash of Coding game combines the pressure of having to work under pressure. Shut up, Scott. All right. So, uh, we have the word string. We have an input with a bunch of zeros. Each line is one character less than the previous taken off the front of the input up until it doesn't have anything anymore. Okay. So I'm actually going to convert this string uh into an array. So we can just use the uh shift method to remove from the front. First of all, we want let's do array const answer is equal to array from uh the length is going to be the string.length and then we will return from that the index. We're going to do a an S dosplit

here. Then we're going to map and in this map or we'll do a four each e. Right. Man, I hate having to go fast on these things. No, this is so stupid. Scott, what are you thinking? I'll use a for loop uh that goes from zero to the length of the input. So while I is less than S.length S++ and then on each iteration we want to log out the current value of S joined back together and then after that we want to remove from the front. So I think it's shift to remove from the front of the array assign it to a constant variable.

Oh we have to go the opposite way. Um start number end number. Okay, so the start number plus this is this, my friends, is the issue with using uh four loops going to be optional. And we're going to try this.

Boom. Play all test cases. Oh, did Scott got it? Good job, Scott. I'm proud of you, boy. I'm proud of you. Do I need splice run? So I starts at zero. How did Scott get it already? Uh, we're incrementing each time while it's less than me. Oh, the we need to store input length as a static variable cuz this is changing. So, we need the initial length.

Oh, still going. Uh, that way it doesn't change. Man, I would have won that, but I spelled freaking length the word length wrong. I'm proud of him. All right, let me mute. Who do you think I am? I am. Wow. Scott comes back firing with like a like a full 20se second lead.

Yeah, good job. I'm moving slow today and I'm using four loops. This is a if you use a for loop you need to hold on to that length because that length is changing because basically I take the input on each iteration remove the first character and log it out log it out each time. Uh but length is changing so yeah I need that to be static and I think if you tried to use a for each on the converted array the length would be changing so things would get weird there too. I don't know. But look at you.

Getting shifty. Very rarely do I use any of these mutating methods, but shift I never use. And I was proud of myself for remembering that. That's the one that takes it takes off the front of the array. Pop takes off the end of the array. Oh man. All right, let's see what Wes did. Oh yes. Okay, Wes. Very similar to what I did in some ways.

I created an array and then just sliced from the back or no from the front. I thought I had to do the back, which is why I lost. And then Oh, no. I did from the back. That's what that's the minus uh multiplying it by negative one. Oh, negative one. Okay. Wow. Yeah. I feel like this is this is too much. This is how my brain works. You guys are bringing four loops out. No chance.

That's I didn't bring a for loop out. I brought a four each out and I just use the dang index. So, you split it and then you do a four each. You grab the index and then you slice it. Yeah. So, like you don't need the second parameter in slice. You just say start from the current index. Beautiful.

And you came up with that in a minute and 37 seconds. I'm so proud of you, Scott. This is amazing. Thank you. This is great. This is the first time I've ever gotten anything other than last place, I believe. Really? Uh, do you want to do one more? Winner takes all. I kind of want to win for once. I just get in second place the whole time. I did hear why I say he's going to win this one, though. And I I'm going to smash that doubt it button. Doubt it.

All right. Get this effing modal off the screen. I don't want to see this modal when it's a Get out of here. No way. Okay, so we're doing maths, but it's on multiple lines. Oh. Oh. Oh. Okay. Um Oh, boy. Expected input 5* 4. Okay. Five. That's just math. It's just math, I think. So, we could use eval, but we need a we need a map lookup. Um, so we need a map that says the x is the multiplication sign and then um the minus is the minus sign. But I guess we don't even need to do that.

Um so let's just say uh if map at equation uh then equation equals map at equation parse int number one. Um but how do you dang do the math?

What like um get out of here. It's not an int. I've already lost this one. I'm going to be honest. Um, should I do a case switch? Uh, let's just do an if statement.

If equation is equal to minus console.log number one minus number two. And honestly, it doesn't even need to be this complicated. So, if uh if the equation is an x, then the equation becomes the multiplication sign. That's literally all I wanted to do. And then now all we have to do is create a string that has all three of them in there. So number one, the equation, and number two, we join it all together, and then we run eval on this whole thing.

Oh man, what is that stupid JavaScript? West Boss here. Thank you so much for checking out beginner JavaScript. Uh, create a string that has all three of them in there. So number one the equation and number two join it all together divide is the last one. Um what is testes you execute.

Come on come on come on come on come on come on come come on come on evil unless assignment variable call let it nice. Uh I just went dirty. I can't even believe I even thought to do a case switch. Uh, me smashing the doubt it button is going to uh bite me in the ass here. Doubt it.

Am I crazy? Yeah. This is the exact type of thing I go to chat GPT for and which is why I am doing that right now. Oh, I just have to write proper JavaScript. That's all. West Boss here. Thank you so much for checking out beginner JavaScript. I did a double I did a single equals instead of a triple equals.

CJ submitted something. I'm I'm going to submit because I don't honestly know how to do this without actually looking it up. Uh but I'm guessing Wes did switch statements. Yeah, this is it. I just said I was going to write a switch statement, but I can never remember the syntax. I was like just if statements. Yeah, if you're not worried about it. Yeah, calculator 101. If it's a subtraction, do a subtraction.

I knew Eval was the method to get it done fast, though. I knew it was that. I just couldn't like I I I just couldn't get my brain to remember eval. I may have even said the word eval before, Randy. If I did, that'll be a fun edit, man. Is it execute? Execute. Um, what is the darn thing? But nice job, Wes. In a minute and a half, you wrote out all these if statements. I don't think I could write four if statements.

Copy paste and then and multiple cursors. And then the only one you really needed to change was the multiplication. Beautiful. Yeah, that was a fun one. I'm glad I finally won. The last one, winner takes all. Roll the clip. CJ said winner takes all. Do you want to do one more? Winner takes all. I think we should we should clash some some people other than us as well. Let us know who in the comments we should be clashing against in and in what languages as well. I think it'd be fun.

We should maybe do them in languages that we're not super comfortable in as well cuz we could do it in like 10 minutes. Rust. Yeah. I uh I can't wait to embarrass myself in front of other people. Story of my life.

Quote it with credit and a link. Quoting Syntax