Syntax Video
We play CLASH OF CODE! Shortest JavaScript challenge
Transcript
It's another clash of code here and this time we're seeing who can write the most tur code. Who can write this stuff the fastest, but also specifically the shortest. So, let's get to it. All right, boys. If you look at the bottom right, it says code size. So, by default, you're you're getting kicked off with 278 characters. And you'll see this number go down. So, if you delete the comments, you're instantly going to be down to 65 characters. And new lines and spaces do count. So if you remove that last new line, you're down to 65 characters. Um, so you're going to have to think about like spaces, semicolons.
One trick I will give you is built into Clash of Code is instead of using console.log, you can use the word print. Oh, like it's a print function similar to console log. Yeah. So instead of console log, just say print parenthesis and that will also console log it. Oh, okay. So everyone everyone have it? Yes.
Also, I don't understand this problem, but I guess that's Well, okay. Good luck. See you. All right. Later. Print the product of all digits of a number in What the hell is a product? So, we're given a number uh for in this example given 1 2 3 4 and the output is going to be the product of the digits. So, 4 * 3 is 12 * 2 is 24. 24 * 1 is 24. So, the output is 24. N I assume is going to be we can immediately console. To see what N is. So, play test case 1 2 3 4. So, that's a string. So we need to split it. Nums. Let's see where we're at. N.Split is not a function.
What is n? I would imagine you want to split this number and then this is already an int. So if we were to do two string split and then do I don't want to do a reduce. Gosh, we actually have plenty of time here. So I'm going to write this with a reduce first. What the hell is a product? What is a product?
What the hell is a product? Product of dition refers to the result of multiplying all the individual I don't even know. So you're not adding them. I'm sorry I had to look that up folks. For example, if the number 2 3 4 the product of all Oh okay okay. Okay. So the initial value is zero and in the reduce you have the previous value and the current value and you are going to do P plus the current value. parse int or let's just say let and let's change this other one to let as well that's shorter and then we can say print t. We play all test cases. It failed. Okay, last time I I used a reduce and I think I'm probably going to regret this again. Here's a a code golf tip. Split. you do have to pass in the empty space if you want to split it. But instead, if you pass in uh just if you use it as a template function like this, that's going to be less characters than
with uh parenthesis and quotes. So 1 * 2 * 3. Okay, so we basically return the accumulator multiplied by the number um and then let's just console log the total. Oh no, maybe I don't need to to start with that. Success. Okay. And I guess the initial value should be one here. Woof. I don't am I going to even solve this if this is a number. This is a number. This is a number. Why would it have found P1? This is going to give us an array of 1 2 3 4 just to test that out. We're going to print out in and you should see yeah 1 2 3 4. So that's what we're getting. And then we need to multiply each one. So I think the best way to do this is with the reduce. We reduce it. We have the t total and then we have each individual value. and we want to multiply t * v and start this all off at zero. But I we might not need a starting value because of that. So if that's the case, does this work? Yeah,
let's try all the test cases. Okay, good. Now let's take that and make this total. Still good. Okay, now how do we make this smaller? I think instead of storing it in a separate variable, we can just directly print it. That's going to get me down to 44 characters. So, what I want to do is rewrite this with a for loop and see how many characters we're at. So, this gives us 44 characters. Let's remind ourselves of that. Oh my gosh, I am going to get ripped apart in the comments for this performance here. So, then 1 + 2 is two.
Everything's working. Now, how do we make this smaller? to string and dotsplit. Um, I wonder if we have a number and we spread that into an array, does that convert it to a string? No. But what if we Yes. Okay. So, we want to spread and then concatenate an empty string. Folks, what am I doing wrong here? This is the answer. I T is 24. Do I need to do print T? Is that what I need to do? Found P1. Oh, is it just reading these console logs? Oh, you [ __ ] Okay, okay, we'll need probably like the total product. So, I'll initialize that to zero inside of the parenthesis here. So, this is a trick because like technically you could put it here, but then you have a new line, but because arguments don't matter in JavaScript, I can just put it all there.
So, that initializes our product and then we need a loop over the character. So, we'll have a for loop. So, let's say uh value v uh in the number n. We would have to split this still to get it into actually no I guess we wouldn't have to split it because we can use a four in or four of loop across a string. So my question is what is n coming in as? It's coming in as a num or obviously it's wrapped in a parse int. So what if we take that out string? So we don't even need to string. Okay then we can take this read line and split that. Good.
This is me not knowing how code golfing works. This is why CJ wins. Okay. Playout test cases. Yes. Yes. Yes. Success. Okay. Now I have to determine if I can make this smaller, which obviously I can cuz I could just do that and then say print N and then play out test cases and it's going to pass. So if we just print out V, what am I getting here? So 1 2 3 4. Yeah, that that's what I would expect. So V is 1 2 3 4 and then P is going to be multiplied by the value of V. I thought that was right. Now, this split can be further made shorter by spreading. Is that even shorter?
Good. Let's check them all. Make sure they're all good. Okay, good. And then we change this accumulator to a num to n. Okay. Play all test cases. Good. We could take the spaces out. Then how many characters are we at? So before it was 44. This gets us down to 41 characters.
That's huge. I'm curious. Yeah. Instead of storing in in a variable, I can literally do a read line right here. And then we're down to 37 characters. You know what? I'm I think I'm just going to submit and then watch watch the boys sweat because I don't think they're going to get down to 37 characters. So, if it's comes in as a string, I don't need to parse an int and I can get rid of that. And ultimately, like, do I need to even save it as a variable? Can I just do this? I can play all test cases.
Let's go. I don't think I'm gonna get it any smaller than this. Maybe I will, but uh not right now. So, submit. Yes. Oh, so CJ beat me by double using a reduce. No, we keep we'll keep it at that. I think that's I think that's as short as this sucker's going to get. I'm going to submit 41 bytes. Oh, I absolutely Oh, he's got 37. No. Oh, I beat Scott. So, if we look at mine, I did a print read line. I just removed the parse int and then I split it. Spaces. Spaces. Oh, at the bottom. No, in between your your parameters. Yeah, those are extra characters. In your arrow function.
Oh, yeah. That hurt me. You coerced it into a number. It's not necessary when you do math with a string. I didn't know what product was. So, initially I had it being Google it. I had to Google it, too. But I had it being addition mathematician. So, I didn't even think about that because I was just like, "Oh, but oh, I'll change the plus to a multiplication." All right, move on. You know? Yeah. All righty. Let's see. Wes is 41. Did he take a very similar approach? Nice. You spread it into an array. Yeah, the spread was less than split. And you also don't need to start your if you don't start your reduce with a second argument, it just takes the two first values. Ah, man. So you Yeah, you you that comma one that you had the reduced master wins. Yeah. Or not wins, I guess.
Well, like last time I got bit. Oh, wait. Did I leave a line in here with line two? Yeah, you have an extra character. Oh, you could have gotten it to 40. Uh, you still beat me by three, though. Let's see. CJ's. Yeah. Are you guys ready for this? There's a lot to talk about. Of course. Well, a four of loop works with strings because a string is an iterator. Four of is going to be shorter than splitting and reducing. Um, here's another little trick. If you need to create a variable, put it inside the invocation of a function. So you see how I have key P key equals 1 right there.
That removes one character because it doesn't have to be on the previous line and then JavaScript just ignores the arguments. I didn't create any variables. If you need to in the future, I'll say this is a Yeah, crazy. Absolutely killing it. Another one. Yeah, let's let's keep going. All right. 3 2 1 Let's go. Okay, this next one says it's a long description. I think I think Scott's going to have a problem with this one. A man wants to rent an apartment in the city center. This man earns β¬8 per hour. Consider that he works for 6 hours per day. God damn it, I hate word puzzles. For 5 days a week for 4 weeks, the apartment costs B for 4 weeks. What is this, math class? Uh, you must also assume that you must consider spending C every 4 weeks for other reasons. It is necessary to produce true if he can afford the apartment or false if he cannot. A is the euro of profit per hour. B is the cost of apartment per four weeks and C is other spending. So
this is how much he makes and these are the costs. So this is his I for income, expense for expense and S for spending. We'll say apartment. Okay, I'm changing the name of these. We're going to call that monthly pay is equal to a * 6 * 5.
Um, and then con monthly expenses is equal to the apartment costs b 4 weeks. So that's b plus so let's assume that he's spending cur every four weeks. So c. So if the monthly pay is greater or equal to monthly expenses console.log true else we're going to log false. Okay. Let's see. I'm not even sure if it wants a string of true or a string of false. Right. So let's run false. Expected true. Why? If he's working 6 hours a day, 5 days a week, that's going to be 6 * 5. And then the euros per hour is a. So this is going to be the uh income is going to be 6 * 5 * a. So that's cuz that's euros per hour.
And then the apartment cost be euros per 4 weeks. So we just want to know does the income 6 hours per day 5 days per week. Oh for 4 weeks. So total income here would also be multiplied by four if it gives us the the total four-week income. Okay. So multiply that by four. Then we want to know does the income minus the cost of the apartment which is B per four weeks. So that's the total amount over the cross over the four weeks. You must also assume that you must consider spending CO every four weeks. So then it's going to be minus C as well. So we just want to know if income minus B minus C is greater than zero first try. Okay. So all of that for a very simple calculation. He works six hours per day. So, his total income is going to be equal to his hourly rate time 6* 5 for 4 weeks. Look at me. I feel
like this is relatively easy wrapped up into an obnoxious word puzzle about this man's life who I don't even know his name. We never even took the time to learn this man's name and yet here we are judging him based on his expenses. Uh, let's make it as short as possible. Well, first of all, I'm going to reassign read line to b R so that way I don't have to type read line over and over again. And then inside of here, can I do a equals r, b= r, and c equals r? I might have to put that on the next line, but let's just see if this will get us ab and c. r is not defined. Yeah. So, I'm going to need to put a b and c on the next line. R is not a function. Oh, because it has to be assigned to read line without invoking it. Yeah, so that'll get us there. So this problem is deceptively simple and like yeah parse int is short but number is shorter. Play all test cases it works. We can get rid of this i variable like that. What else can we get rid of? Number read line yes
plus number read line yes. Here um we can get rid of two variables that way. And you can then do this. Another thing we can do is pre-calculate uh 6 * 5 * 4 120. Now, I'm very aware that CJ is going to have some dumb [ __ ] way to get around this number read line business. I just am because it's not like that tur it's not that short of an answer here. If we read in R, we know that that's a the first time we do it.
So, that can just go right there. And then the next time we do it will be B. So, that can go over here. And then the next time we do it will be C. So that can go over here. Yes. And then now we can inline income here. I don't think I'll need parenthesis because of precedence. And then we'll just print this. Yeah. Honestly feel kind of bad. I don't I don't think the boys are going to get this this short. So the guy's monthly expenses are greater than Why is it false? What's going on here? The CJ is done already. 37. I'm not even trying to get it as as small as possible. This is like this doesn't make any sense this question. Well, it does make sense, but why am I not understanding it? Oh, I'm so stupid. 6 days a week time 4. How do I forget that? Okay. Okay, those are all right. All right. So, now let's figure out how to do it in the shortest amount of code. CJ submitted with 37. That's [ __ ] Number one [ __ ] right there. Cuz you can't get past read line and those are all kind of long. You
could do one times read line. Does that get me any closer? That gets me closer, but it doesn't get me anywhere near. All right. Now we need to figure out how do we get this parse int to be much smaller. What's a better way? Time 1. So b * 1. Is that still going to work?
Yeah. Okay. Is that the shortest way you can parse an int? What are the other ways? Like how do you get past this read line [ __ ] Um what else can I possibly eliminate here? All right, I'm going to submit. I'm just going to accept my fate here. Oh, I could just replace that with 120. 38. Oh, that's even shorter. Yeah. So, just don't do the multiplication. Oh, I could have gotten shorter. Yeah, Wes has a good point. I hear him talking right now.
Dang. Okay, Wes might beat me then. Chat, how is CJ at 37? R is greater. What if I just do greater then? Would all the tests pass? They do still pass. I'm at 37. Psych. Take it, C. Oh, CJ's going to win cuz he did it faster than me. Now I got 4 minutes. I'm You know what? I I see the guys are done and I don't care.
I'm going to I'm going to figure this out. He's going to he's going to beat me. I know it. The thing is he's he has he hasn't gotten over the fact of parsing. You don't need to parse. So that could that could save him some characters. So now, how do we get rid of one of these multiplication signs? I bet CJ has the exact same answer. I don't know that there's any more. H I don't know. I think I'm going to have to tie with CJ. He's going to win it, though.
I'm submitting it. Submit. Yeah, Wes, we think you could have done it. You think I could have? You could have. I didn't I didn't pre-calculate the multiplication. So, that's like three characters instead of two. Shoot. Oh, but there's there's one more trick I used. I I'll have to see your code, but there's one more trick that that Yeah, let's let's see Scott's crabby code first. Hold on. Yeah, I don't know what what you I obviously I could have gotten rid of this multiplication, this parsing, and I did pre-calculate, but what did you guys Is there an alternative to read line?
read line. You say R equals read line. So then you call it three times, but you only aliased it once. Yeah. And then you and then you wouldn't need one times because it would just work. And you would have had we would have we would have all tied if I mean, no, you you guys would have won cuz I didn't pre-calculate 120. So let's look at Wes's solution. All right. Yeah. Okay.
You don't need these. Um, what are you adding? Oh, okay. Actually, look at look at my maths. Yeah. So literally Yeah. So what is 6 * 5 * 4 and then just use that I could have gotten like three characters reduced and then if you would have used my solution Wes like oh yeah very the first five minutes I couldn't get the the problem to work and I forgot to multiply it by five or by four I was talking about how we're judging this man for his his expenses without knowing anything about him.
All right let's do another All right, one more. A stationary rocket is launched and travels with a constant acceleration of 2 m/s squared. This means that every second the rocket's speed increases by 2 m/ second. The rocket reaches its goal in in seconds. Print the distance traveled by the rocket. Oh man, this is some like algebra here. Isn't that like logarithmic or some exponential? It's exponential [ __ ] No, I got to I bet CJ's going to He has better math skills.
Division. I don't I have to what's what's the calculation here? This is physics class. Okay. Um what is that even called? Is that to the power of I'm asking AI what this is called? Kinematics. To get the distance you need to know speed at m / s time how many seconds? Are we to assume here that initial speed is equal to zero? Maybe I like I I guess. So again, we're just going to see if we can get this solved first. The total amount of time t is what we get here. So I'm going to go ahead and just read this in. And here's a another shortening tip that I don't think the boys know. So if you do need to actually parse it, just use the plus sign. That's the unary plus. And then you don't have to say pars or whatever else. So that's going to give us the time t. And then how do we get to the final answer, which is the distance traveled by the rocket in meters? Maths 2 to the second power. I know I'm going
to look like a fool not knowing how to how to do this physics problem. Okay, so if we're traveling 2 m is that that's 2 m every second, right? So 2 m over seconds squared. Is that the is that the the formula? The total number of seconds is t. So is this our formula? 2 / t ^2.
Let's see what we're working with here. We got 101. I didn't know we were doing math challenges here. Does that mean every second it goes [Music] faster? Let's try a for loop. And then every single second it takes is n. So as long as i is less than the total seconds um or equal to, I suppose. And then we're i ++ing for each second. And let's do a let current speed. This is so much harder under pressure than people realize. It's like I got to talk through this while I'm reading and doing for loops. T ^2 * 2. I literally don't even know how to do this. Okay. Put the distance traveled in meters by the rocket. Oh, it it's increasing. So, I guess that's where a formula comes into play because the acceleration is increasing every second.
So, that's why it's not just a standard multiplication. I'm going to I'm just going to search the web for this and we can ask afterwards if that was okay that I did that. Calculate distance. given acceleration. Let's see what we're working with here. n is not iterable. Man, it's been a while since I wrote a straight up for loop. So, we go all the way up. Okay, so here what we'll do is we'll say let me m is equal to 0 and then m += i * 2 and then console log m. Okay, so I'm off by a little bit. So, what if we start this at 1? Why am I off by 11?
That seems like a suspicious amount. Okay, so the current speed increases every second plus 2 m/ second. Okay, this is such a dumb way to do this, but hey, I'm working through it, folks. And we want to solve for D. And if we have the total speed, is this even work this way? Gosh, I'm too dumb for this. All right. D is equal to current speed time N. I'm not confident this is going to work. And honestly, I'm not confident that I understand how to solve this problem with the math skills that I have. Acceleration 2 m/s squared.
Initial velo velocity 0. The time 101 seconds distance. So this gets it right. Acceleration to distance formula. So d is the distance where v is the initial velocity. So we know it's zero. So this part of the equation is zero. And then we have 12 * acceleration which is 2 * the time squared. What am I missing? N + 1. Let's try. No. Damn it. The very first second it's 2 m. The second it's 4 m. Right. Let me share this article in the Slack. There. There's that if he wants to use it. Oh, CJ's acceleration to distance calculator. Okay. Maybe I don't know what acceleration to distance is. All right, we get to use this app now. So let distance instead of at the end, let distance is equal to zero. And each time I'm saying distance is now going to be equal to n, which is the amount of seconds times the current
speed and then at the end I should have distance. Okay, so instead of calculating at the end, I should be brutal. Same answer. I guess that doesn't make a single difference. Uh, am I fundamentally thinking about this the wrong way or do I calculate the distance before? But that's double. I'm doubled for some reason. Why am I doubled? What the hell? I'm sure there's a very reasonable explanation for this. Why am I doubled and it worked? That's very bizarre to me. My math is so bad that I have no clue why that is even the case at all.
Okay, so we do 0.5 * a, which is the acceleration, which in this case is 2 * t ^2. Okay, that's right. I Oh man, I see Scott flipping out over there. I can't hear what he's saying, but I think it's plain and simple. Why is this so darn hard?
Freaking CJ goes to computer science, thinks he could freaking do math to the power of and he's he's amazing web developer. Try try click a button CJ. Okay, let's look at the stupid calculations written in latex. So basically 0 + half * A. So here we got to say 0.5 * 2 * 2 which is I * the power of 2. Let's see if we got this. Ha. No, I'm about ready to say f this one. Let's at least tweak this. Uh, let S is equal to speed and instead of current speed, we're going to say speed. This divided by two thing is crazy to me because how does that make any sense, folks? I got to say I'm just totally confused here because I don't think there's any particular reason that I understand right now how I
solve this problem. Um, I mean, obviously we don't need this parse into. Okay. And now I'm just trying to optimize here. I got 4 minutes. Here's what I'm going to do. We know we can simplify the math. We didn't do that last time. 1/2 * 2 is literally just 1. So, literally, it's just time squared. Is that the Is that the solution here?
Because we have a we have a constant acceleration of 2 m/s squared. That's it. And I'm down to 20 characters. Okay. I'm gonna have to man CJ with 20 lines. You got to be [ __ ] me. All right, you know what I'm gonna do? I'm gonna cheat. Let's see what's coming in. 101 is expecting this.
Okay, 1111 is expecting this. 264 is expecting this and 22 is expecting this. Man, I feel so ill equipped given that I do not understand why this works. Now I'm just deleting [ __ ] until I can get this smaller. I What am I doing? I'm moving deck chairs right now. Okay. The reason why is that I don't know math.
There's a mathematical equation where I do not have to do a for loop. And do I know what that is? No, I don't. Am I ever going to? Yeah, once I Google it. I got it. Okay. Uh let's rename this to L. Play all test cases. This is so so stupid. I'm going to have to read some math books. All right, submit. I hate life. Boom. Submit. I finished. I finished. I cheated. But you only got 25% cuz it runs it through against uh other tests, not just ones. It does. Oh, I was I I made a lookup table of all the answers and it passed all test cases.
Does sometimes that will get you 100% or will get you like 90%, but it runs it against uh a bunch of different scenarios as well. Okay. I This This one was stupid cuz like I agree. I'm not a freaking rocket scientist. I'm not a math guy. Yeah, I looked up the formula. I was struggling for like You looked up the formula. Yeah, I put it in the Slack. You ignored the Slack the whole time cuz I think I cuz Wes clicked on the formula, too. Oh, I got the right answer and I could not tell you how. I did I did the math and I got the right answer and I was like, I don't know why this works. All right, let's look at Wes's. I was like 11 off every single time. I could not figure out why I was 11 off. So, I just deleted it and I got a lookup table. So, this works for the the test cases in the editor cuz it's literally given this input, you get that output. All tests pass. Yeah. In the in the example, but then it runs it against a bunch of other stuff. Uh, it doesn't I don't think that matters.
But yeah, actually, I'm really proud of Scott for figuring it out without the Yeah. So, yeah, I wrote it out as like a pure word problem first. I obviously I did the read lines. I set s to zero and I did a a four over each second. So each second I would first calculate the distance uh with the speed times the second and then I would update the speed and then it wasn't giving me the right answers until I divided it by two. Why?
I don't know. You're literally you might be a genius. Yeah, you you're Isaac Newton. Like you have actually you have you have manually calculated the distance formula just by talking through the problem. No, this is great. Like like your math is completely right according to the formula. Oh, [ __ ] CJ. Look at CJ's answer. Oh my god, guys. Let's see here. So the formula is 12 * the acceleration, which is 2 * the time squared. Oh man, that's where the 1/2 comes in or dividing by two because you're taking the acceleration times the time squared and your for loop calculates the time squared. I I will say that Scott is actually the winner here cuz he did the work. Duh. Yes, I was at one point I was just deleting things and being like, does this work?
No. All right. Does this work? No. Let us know how how did you do on these? Have you tried these uh Clash of Code? What are things that we're missing? Can we do one that's like actual web development and not like rockets? Cuz like I feel like I could I have some tricks up my sleeve. That could be fun. I don't know if a site exists, but maybe we do that like implement a a date drop down in the fewest number of something like that. Thanks for tuning in. Click here for other Clash of Code CSS battles that we've done in the past. Let us know if you want to see more of these cuz they are both extremely fun and extremely frustrating all at the same time. Yeah. Uh, we'll catch you in the next one, folks. Peace.
Quote it with credit and a link. Quoting Syntax