Syntax Video
writing javascript as fast as possible
Transcript
What's up? CJ here. Scott and Wes are out today, so it's just me and I thought I'd challenge myself. And my goal today is to see how many seven Q code I can solve in 7 minutes while sitting down. So, if you're not familiar, codewars.com has a bunch of algorithmic problems you can practice to to learn and get better at coding. And they range in difficulty.
So, if you look at the difficulty, 8Q is the easiest. Think like add two numbers together, reverse a string. 1 Q is the hardest. think like implement a SQL language parser or a compiler. 7Q is slightly harder than the easiest and that's what we're going to do today and I am going to sort them by popularity.
So the most popular ones and ones that I have not trained on. So these these will be problems that I have never seen before and never attempted. That's the deal. We're going to set a 7-minute timer. If you want to try at home, I'll link all of the ones that I try in the description. You can see how long it takes you to do them. Um, or you can try a random seed like me where you just basically go for all the ones you haven't trained on. Here we go. Seven minutes. Let's go. First one. Curing functions. Multiply all elements in an array. To complete this kata, you need to make a function multiply all which takes an array of integers as an argument. The function must return another function which takes a single integer as an argument and returns a new array. Okay, I think I can do this. So like 1 2 3, we return a new function.
So, this takes in the array and we need to return a new function that takes in the value and we want to do array.m mapap on that value. I think that's literally all they want. Um, okay. Next one. Help the fruit guy. We'll come back to see if we solve that one in a second one. In a second. Uh, our fruit guy has a bag of fruit represented as an array of strings where some fruits are rotten.
He wants to replace all the rotten pieces of the fruits with fresh ones. For example, apple rotten banana apple. The replaced array should be apple banana apple. Okay. Um, here are some notes which will be like more conditions, but that should be easy enough. We literally will just map over it. So, a bag of fruits, map over it to get each fruit. And then we want to just replace um rotten with nothing.
And we'll do two lowercase because uh bananas with a b. So, if the array is nil, null, none, or empty, it should return an empty array. So, we'll do that. So if not bag of fruits uh we'll return an empty array. The rotten fruit name will be in camel case.
The returned array should be in lower case. Okay, that should do it. Let's look at our previous one. We did not solve it. So zero is not a function. Um is I guess it's the opposite. They give us the value and then uh they pass in the array
return value must be an array. Array.mmap is not a function. Okay. Uh I have no idea. So they pass in they invoke multiply all with an array. We know that for sure. Like I can see that. Um, and so let's log that array. And then they invoke it a second time with a value. And so it should just be array.m map. Oh, no, no, no. Um, not the value, but it needs to be Oh, I I see my map function was all messed up. Okay, so we have uh each value in the array and we want to multiply that value that thing in the array by that value. I that's that's the issue. My map function I didn't even think about. Okay, we got that one right. Let's keep moving. Index capitalization. We have about four and a half minutes left. Given a string of lowercase letters and an array of integer indices, capitalize all the letters at the given indices. If an index is beyond the string, it should be ignored. So, uh, each of these indices needs to be capitalized in the result string. I think I can do that. So, we're
just going to do a map. So, we have the string, we'll split it, we'll map it. That's going to give us each character and each index. Um and then we will just say uh if indices includes this specific index then we're going to do C.2 uppercase otherwise just the character itself.
Join it back all together on the empty string. Go. Um okay. We got the curing one right. We got that one [music] right. We got that one right. We're moving right along. Sort by last character. We have about 3 and 1/2 minutes left. Given a string of words, you need to return an array of the words sorted alphabetically by the final character in each. If two words have the same last letter, the returned array should show them in the order they appeared in the given string. Okay, I think I can do this. Um, we'll do x.sort. We get access to a and b. But one thing you should do when sorting strings is uh use compare like compare local or something like that to local compare. So you can do um we want to find the last letter. So we'll do a at a.length length minus one. So that'll give us the last letter. And then we'll do the same thing with B. So B at B.length minus one. And this we will do a localal compare.
let's see. Next one. Incrementor. We've got about 2 and 1/2 minutes left. Given an input of an array of digits, return the array with each digit incremented by its position in the array. The first digit will be incremented by 1. The second digit by two. Make sure to start counting your positions from one. So given an array of 1 2 3, we get 2 4 6. 1 plus 1 2 + 2 3+ 3. Given an array of 4 6913, we get 58 28 58. Okay, that should be doable. We're just incrementing each one by the index. We're doing a bunch of maps in this speedrun. So we'll do nums.mmap. We get each value and we get the index and we want to do value plus index + one like that. I don't think we need it in parenthesis but go. Okay. So this one failed. X do.sort is not a function. Um what are they passing us in? Given a string of words.
Oh, so it's okay. So this is a string. We need to split it on spaces. Okay. Um so this will be the words and then uh return an array of the words sorted [music] alphabetically. Okay, so that was the issue. We get a string. There we go. Okay, so we got that one. We got that one. We got that one. And then did we get incremental? We did not. So for 46713 we are supposed to get 58.
Um are the numbers strings? I'm going to this this is weird. I'm going to cast the value to a number to make sure. Why is that happening?
Let's look at what numbers is. We only have 44 seconds left. I would be happy if I could just get this one working in 44 seconds. Um 46713. So 4 + 1 is 5. 6 + 2 is 8. 7 + 3 is 10.
Oh, they can't be more than zero. Um only contain a single digit. only the last digit. Oh, okay. Okay. Okay. So, um we should do uh math.
Floor of this whole thing divided by 10. Is that how do we need modulus? Modulus 10. Uh we're out of time. Just let me do add some parenthesis and [laughter] that should solve it. Let's see. Yes. Okay.
Ready to go. All right. So, we got uh one, two, three, four, five, seven kokatas in 7 minutes. That's decent. I think that's my record. Years ago, I tried to do this. I think my record was five. But let me know down in the comments how many did you do. Like I said, I'll link these exact ones and you can see how long it takes you to do them. And uh this was fun. All right, I'll catch you in the next
Quote it with credit and a link. Quoting Syntax