Syntax Video
Can You Reverse Engineer Code? Clash of Code: Reverse Mode!
Transcript
Welcome to another Clash of Code. In this video, all three of us are going to compete to solve these JavaScript problems, but with a twist. Today is reverse mode. So, there will be no problem description. All we'll see are inputs and outputs, and we have to figure out what code to write to get that output given those inputs. Is everyone ready? Oh, I'm ready. All right, I'm ready. Nice. Let's do it.
This game in reverse mode, expected input, expected output. You input one, you get 1 through 10. And then this input gives us 21 and it's 21 42 63 84. So it's increasing by 21 each time. I think that's what we need to do. And then we do it 10 times. So take the number, repeat it 10 times, multiply and add it to itself that many times. So I guess it would be multiplied. So what if I did uh array from and then we give it a length of 10 and then for each element we take i + 1 which is going to be 1 2 3 4 5 6 7 8 9 10 and multiply it times the input 7 8 9 there's always 10. Okay, so we basically take in n console error is how you take what n is. n is one. Okay, so we want to make a new array of 10 items.
So array from with a length of 10. Then we are going to have a map. So we don't care about the first item because it's empty and then we want the index from that we will return the index times n. So we can do a four um i= 0 i is less than 10 and then i should be less than or equal to 10. Oh no I is zero. Um, let's start I at one cuz we're just multiplying it by Yeah, we're I should be one. Okay, we'll log this all out uh by joining it together on a space. I think I think that should do it if I didn't do anything crazy.
Let's try it. Yes. No. Okay. Oh, I see. I need to do I + 1 times in in parenthesis order of operation counts. Yes. Okay, this is it. Sick. console dot constancer is equal to console log answer [Laughter] standard output stream nojs version 20.
What the hell did I do? N is not defined. Why? Oh, it's a capital who is a capital N. That's stupid. I guess I do want to take this and turn it into a variable here. So, a equals print a * i and then um no, I don't want to submit my code. I want to play out test cases. Um, found nothing. We basically are off by one.
So, I + 1 * n. Okay. And then we simply join with a space. Good. Play all test cases. Uhoh. Okay. So clearly I'm doing something stupid. So var a parse int for i.
Oh, it's always going to be that. So we don't want it to go once. We want it to go 10 times. I'm getting 21. So, I basically want to return times I want. That's what I did. Success.
Submit. Got it. Damn, CJ. A minute and a bit. Yeah, this is stupid. It's expecting to have them all as like a um string maybe. I don't know why it's like asking to concatenate them all or something. I really wanted to do some like short, but I wasn't trying to make it as short as possible. Right. It's kind of nice. be just like doing it as fast as possible. Just solve it. What?
Whatever it takes. Yeah. Um Oh, because it's plus a print statement. Folks, you'll have to forgive me. I'm simultaneously trying to solve the problem and optimize as I go, which like is not my thing. That's the read line business. Where am I using a Did he get it though? Will that work? Yeah, he's done. He just doesn't He just needs to Okay, perfect. Scott. Scott, what? But we had a time limit. No, no, just submit.
Yeah, you don't need to shorten it. Did you Did you get it? No, I thought it was I thought it was shortest. No, no, no. Just submit when you're done. When you're ready. I thought it was shortest and fastest. I was going for shortest. It's only uh I could have got I got that way faster. I was just going for shortest. A [ __ ] Yeah, that's okay. I was optimizing it. All right, I'll share my code first. All right, share my code.
Uh you can look at my code. But it might not even be the shortest, but it is short cuz I was trying to get it very short. I just Yeah, I read the line. I made an array. I pushed it and then I joined it. Nice. You started your for loop at one up to 11. But I guess that makes sense cuz then you didn't have to do I plus one. Oh, that's the move. Yeah. None of the none of the rest is the move though. Well, I was going for shortest. Yeah, that's true. Yeah. Okay.
Mine. All right. It is exactly the same as what I did. So, uh, make an array. Array from Yeah. is a good trick. And then you the underscore is throws out the first argument because when you are mapping over an array with nothing in it, you have to get to the index value. So you use underscore just to kind of throw it away. Uh and then you return I I had it way faster, but I forgot about my parenthesis and it was goofing up. It was adding the one first. Yeah. How was How did I miss that? It was supposed to be fastest. I'm so confused.
just re-watch the intro and I don't I don't know. But uh yeah, I solved it the same way and I had the exact same problem as Wes. I just basically condensed his run in half. I also did number times in instead of in times number. That's like the only difference. But I did that as well, but then I thought for some reason I was trying to debug it, so I put it put it wherever.
All right, let's do another one. Yeah, we got time for one more. Quick as possible. All right, so we're going for fastest. Fastest. The moment you're done, just submit. Okay. All right, here we go. All right, clash. Now, let's do this. Uh, input two, hello. Expected, hello, hello. Input seven, blah. Output blah blah blah. Uh, we have n uh four i equals uh zero. Oh my dear god, why do I reach for a for loop in this? I could have reached for anything else. Well, I is less than N or equal to N and then I ++. We're going to print um uh I guess I just need an array.
We'll use the same trick as the last one. So, I'll just do a new array. The length of the array needs to be in. And then for each element, we literally just need the word itself. So basically you take the first line and the second line and you repeat it however many times that you want. So um console.log array from length of w and we will map return n and then we'll join that all together with a new line and print this out. That should do it.
Yes. Done. Oh my god. Print. Uh, expected nothing. Found hello. Expected nothing. Expected hello. Oh. Um, CJ, they already penned. What is going on here? No.JS version. Oh, I did it like this.
Ah, I got second. Whoa. Nice, Scott. Great work. Now I got to debug. I thought I had this. W is not defi. Why in the hell is there capitals for their variables? I did this last time. length W. Oh, cuz this has to be a console.
I think I have to parse that, right? You don't want to parse it. Oh, I got them backwards. N. That's the hardest part of these is like figuring out what the variables even are. Come on. Come on. N is the length. W is the actual character. Play all test cases. Boom.
Submit. Wes. You know it's bad when I beat you by a minute. Goofed it up. All right. Me first. Same as the last one. Create an array and then from each of those array return the value that was provided and then just join them with a new line character.
That's too much work. Well, Scott, if you look at his solution, I don't know if you know this, but if you do console. That lets you debug without affecting the output. So, I don't know that. No. Yeah. Yeah. So, that's a good trick if you want to see like what something is, but you don't want to mess with what your output is. Yeah. I did a similar thing. I did a simple for loop. Uh my first run head I is is less than or equal to, which of course failed immediately, and then I was like, ah, okay. And then just frazzled by seeing everybody else submit. But uh yeah, pretty simple. Nice. Yeah, my solution is same same as Wes's. We're on the same wavelength here. Beauty. Yeah. You guys are both I feel like the for loop is easier here. But I I do love a for loop, but not the like I I plus+ 91 semicolons everywhere. It's so easy. It's so easy to mess up like the less than or the greater than or like this is just like I need this many numbers. Array from has it for you. Mhm. Yeah. I think you can also do array and just pass it a straight up number. Um, but then you
have to tag a fill. No, then you have to fill them. Yeah. And then you have to map them cuz you cannot map over empty array slots, which is why array from is so nice because you can map them as you create them. That's why the for loop very simple guys are like joining and arrays. You're not getting arrays with that uh with for those for loops. Scott, the answer did not request an array. just requested a bunch of strings. I'll put on new lines. All right, thank you so much for watching. This was super fun. Uh definitely give these a try and let us know how you did. Uh if you enjoyed this, you'll enjoy these videos as well.
Quote it with credit and a link. Quoting Syntax