JavaScript Algorithms for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys how are you doing can you hear me now we should be live this is the first live on javascript mastery channel so just in the comments feel free to let me know can you hear me can you see everything and then we're gonna start with um cold wars exercises we're gonna solve some algorithms live so you guys will be able to help me here and we'll just go through them i'll learn something practice javascript and yeah just hang out a bit okay yeah great i'm getting comments uh hello everyone great everyone seems that everyone can hear me uh so yeah this is the first live on javascript mastery channel and i taught let's do some live exercises challenges algorithms let's i'm gonna let you guys see me making mistakes because that's not something you can usually see on youtube videos they are pre-recorded it's great but now you're going to go with you're going to actually go through the process of actually solving an exercise with me so i'm going to give you my thought process how to approach something and how to actually solve the thing hopefully right i created a new codewords account i found codewords to be a great website for practicing and we are in there we can start training awesome great so this is how it's going to look like we have a code explanation on the left side or rather the explanation of what our program should be doing on the right side we can actually put our code solution you can write our code here or we can even take it one step further and simply do it inside of our visual studio code so for now we're going to do it here and once the exercises get a bit harder we can then move to visual studio code so with that said let's actually jump into the first one in here it says categorize new number so i'm going to read it for you and then we're going to try to solve it so it says the western suburbs crockett club has two categories of memberships senior and open they would like your help with an application form that will tell prospective members which category they will be placed to be a senior member sorry to be a senior a member must be at least 55 years old to have a handipa greater than 7 and have a handicap greater than seven and this croquet club handicaps range from -2 to plus 26 the better the player the lower the handicap so usually there is some kind of story right but with all programming exercises the thing that you want to be looking at is the input input and the output to be more specific there you will be able to see what your function or program should take in and what it should bring out and i see we're getting some comments uh saying hi hello everyone and i got one questions that says for how long am i gonna stream we'll see uh let's try to go we'll see we'll go with the flow couldn't be one hour but can be two hours if you guys are here and and you like what you're seeing so great input in this case uh consists of a list of lists more specifically that's basically an array of an arrays as you can see right there let me know if the text is big enough i can make it a bit bigger for you guys like this and each list contains information for a single potential member information consists of an integer for the person's age and an integer for the person's handicap note for the f uh the input will be int list list which is a list list this doesn't seem like javascript to me right now but looks like it is we're gonna just keep moving with it um okay moving forwards um there we go yeah these are our inputs so i'm going to take these inputs and i'm going to put them right here at the top as a comment so these are our inputs um great uh i getting some comments saying maybe solve something uh with uh four or five cata that's some system in here in um in um in cold wars i'm not even sure what cata is but we're gonna try go through it is uh four or five cata easier than what we're doing currently or is it harder great so yeah let's let's move further uh the output will consist of a list of string values uh open or senior stating whether the respective member is to be placed in the senior or open category so for this kind of input we're getting this kind of outputs so for that we know that we need to have a function in this case i always like to use es6 plus so we can convert this function into a arrow function great with that i just make it a bit bigger for you and let's see so the first thing here i think is the age let's see the first thing uh to be senior yeah the first thing is the age and the second thing is a handicap so for someone to be senior they simply need to be plus 55 for the first thing so the first thing here needs to be plus 55 and the second thing i think needs to be greater than seven so in there i'm going to type greater than 7 and this needs to be greater than 55 great so now for each one of these we need to figure out if this number is greater than 55 and if this number is greater than 55 as well and we're gonna see if that works um okay and some people are saying now the lower the cata the more difficult the problem is and uh we'll see yeah they think that lower cata would be better okay great yeah we'll see we'll start with this and then we can move to even lower or or harder kata depending on how well we do here of course okay so we're getting the data of course since we have a list of things we want to have we want to map over it for this you can use the for loop or simply you can also use the map so we're going to just do the data and then we're going to map over it inside of here i don't usually like to call things data but that's how they call it it's not really meaningful but in this case let's simply do something like data and then each thing here is going to be data for a specific person let's call that info so in the info we are looking at two different things we are looking at the age and we are looking at this number right there the age needs to be greater than 55 so we are going to say if info.age is greater than 55 and so we can use the and operator and if handicap that's going to be info dot handicap but unfortunately i just noticed we're not working with objects here so we can we need to use info 0 and info 1 to access these elements of an array and if info 1 is greater than 7 at least that's how i understood this then we want to return something in this map more specifically this would be a filter so let's see if this is true we simply want to return that specific info or we want to return that so i'm going to expand this and this is going to be our if case so if info is greater than 55 and if iphone 1 is greater than 7 in there then we can do something like this we can return uh we can return senior right because if these are correct then we should return senior and that should be placed in an array else we want to return something else we want to return as the string says so return and let's do open just like so don't forget your semicolons and finally we have to return that whole thing that this returns which is an array of different strings either seniors or opens great question there i see um ahmed asked why not for each if we use the four each of with 4h we could go through everything but unfortunately we couldn't return it because 4 each doesn't have a return value with this we can actually return it from a function and that way our function is going to return the expected output now with that said let's see if this is actually gonna work now i'm gonna click attempt for the first time and we'll see okay we did pass some things but unfortunately we failed some and looks like we failed a longer one let's see why maybe i didn't read the instructions correctly that's normal and someone said don't check the solution before you try it yeah we will not i'll just try to go through it with you guys so let's see uh each list containing information information consists of integer person's age and person's handicap and do both of these need to be correct let's see okay there we go the to be a senior must be 55 years old and have a handicap greater than seven um so looks like it's not uh picking it up although we are checking for handy but greater than 55 and info greater than 7 someone mentioned maybe more than equal to sorry greater than or equal to let's check that out that's a great thing let's see and that is actually the correct answer so we were really close uh awesome i think will stanley mentioned that first and uh leon as well so great job then uh awesome we successfully solved our first exercise in what was it like 10 minutes less than 10 minutes and someone asked does this output an array and the answer is yes it does so we are returning an array right here someone asked what result would for each give us so we know how to use the map instead uh four each doesn't give us any output uh if we try doing the same thing with four each i can do that right there for each always returns an undefined as you can see if it now click attempt we got nothing we got undefined for each one so if you want to do some simple cons of logs or something in that case you can use a for each but whenever you have to actually return some data in those cases you need to use the map okay great uh so let's see if that works let's test it out one more time and that should work um someone says create an empty array and push senior or open into it yeah that works but in this case we are also putting it like immediately so that's uh that same way but yeah on great way if we wanted to use um if you wanted to use for each then in that case we would have to push so let's see how we can actually um there we go something happened right now i didn't click anything but yeah it looks like i completed it okay so that's it can i get back to the solution maybe i wanted to optimize it a bit or with uh cold wars you cannot go back let's see so if you go to solutions looks like it only tells me how the solution um looks like right afterwards let me see if i can uh train again it's loading yeah let's see someone else did something like this uh but let me also click tr uh train again to see if it's gonna let me okay it does let me so we can get back great i just copied someone else's solution um just to see how they did it usually when you solve these you just you don't just want to simply solve it and that's it you want to research what's the best possible way to actually solve it someone said you could have used a for loop and pushed into an array of course that's also a possible solution great job adam so this is what other guy did i like his way of array destructuring so now let's let's try to get back and let's try to form r our own most optimized solution so we get the data you can see he also used the data.map and then what he did here is he used array destructuring instead of simply having info you can take out the things from the info you can take out the age and the handicap by using array destructuring as you can see here and then he did the same thing as we did age is greater than 54 or we can say equal to or greater than 55 and handicap is greater than uh seven looks like he used 54 here that's okay and then in this case he used the ternary operator to return either senior if those two cases are correct or open if only this case is correct so with that said this is great i think we should be able to submit it but what we can also do is we can just immediately return this since this is an arrow function it's a long one but with arrow functions you're going to notice this from from time to time so now let's click attempt and let's see if our output is going to pass it says your output will be shown here let's see awesome i see someone saying python for the win yeah we like we like javascript here but python is also great uh so let's see this is still loading your results are going to be shown here great uh looks like it's it's loading let me try to refresh how are you guys liking the stream so far this is the first time we're doing this i guess we have to write it from the start um but so let's let's just move to the next one if we if we can or it's not going to let us let me see if i go here pass yeah there we go we can see our pass solution so that's great we can use our past one and then simply modify it so what we said uh we're gonna simply use this uh if we can return this if and simply make it into a ternary expression so we can say if both of these are correct then return the senior else we can use the else just the colon sign else we're gonna return the open and that's it now what we can do is simply put that also in here because it's an instant return and then this is also an instant return so we can just put it in one line and i think our solution should pass so i'm gonna click attempt and it says sending request looks like it failed where did we fail it i think we're missing a parenthesis right here okay we have some errors uh let's see data.map that's great oh yeah we're missing something here let's see yeah this is here info zero info one we said we're gonna destructure this so that's gonna be age and handicap and we can use agent handicap here i agree this is not really readable because it's all in one line so let's see oh i think we are missing a closing tag for the map yeah that that's correct great uh i see some people are joining right now you're not late we're just starting we're getting warmed up uh great and i think uh everybody's having great comments that's amazing adam said uh curious if you use your function expression is just a preference or if there is an anything deeper uh i love this way better and then any live stream this is better than the first presidential election great yeah i'm glad for someone this is gonna be really boring for some it's gonna be really interesting uh but everybody seems to be enjoying it so far uh so yeah this is it this is our first solution looks really elegant maybe a bit too long so maybe in this case i would even dare to say that having it in two lines would be maybe just a bit more readable and adam to answer your question uh error functions are simply just a bit more modern more clean and you can do instant returns so let's attempt it one more time and finally move to the next question not to bore you guys great so we can go back i guess here and we're going to continue doing these so we have dubstep which is 6q and let's start training it okay should i even read this or should we go straight to the input and output what do you guys think we can go ahead and give it a read right uh polycarpus works as a dj in the best bear land nightclub and he often uses dubstep music in his performance recently he decided to take a couple of old songs and make dubstep remixes from them let's assume that a song consists of some numbers of words that don't contain wob to make this dubstep remix of the song polycarpus inserts a certain number of words wob wob okay i see guys you just someone just want to read the input and output but let's uh let's just do let's just try seeing the let's just come through it um so we have we basically have to create a song of words and that's going to be dubstep so for example a song with words i am x can transform into a dubstep remix as well i'm not gonna even read it and cannot transform into this um recently johnny has heard polycarpus new dubstep dubstrap track but since he isn't into modern music he decided to find out what was the initial song that polycarp was remixed help johnny restore the original song oh this is going to be uh hard uh i can see i see some people say read some people say read it some people say just go through the inputs and outputs you know just for fun these are great these are really really nicely put together so i'm going to read through those but just came through those and go to inputs and exp outputs and i see adam asked do professional programmers even read the problem or is it all about manipulation of input and output great question yeah sometimes the problem is going to contain like key things to solve the exercise but in most cases or sorry for even for simpler exercises input and output are going to be enough but sometimes you will have to take a look at that so now let's see the input consists of a simple or a single non-empty string consisting only of uppercase english letters the string's length doesn't exceed 200 characters and the output returned the words of the initial song that polycarpus used to make a dubstep remix separate the words with a space okay so we have this i'm gonna take it and we're gonna put it as a comment right here let's do that right now for these we could even move to our visual studio code but for now i think it's gonna be fine so song decoder uh yeah let's see yeah for example uh adam to answer your question in this case uh if we just see the input which is this and the output which is this would we know how to actually decode it because by looking at it i don't see a clear way to record it so let's let's go through this one more time insert a certain number of words before the first word of the song the number may be zero after the last word the number may be zero and between words at least one between any pair of neighboring words and then the boy glues together all the words including wob in one string and plays the song at the club so i'm gonna take that string actually uh so we're gonna say inserts a certain number of words here i'm gonna take that can i make this a bit smaller looks like i cannot great so we have this and then we're gonna simply put it in comments like this just so we can see it all right in here and then that's gonna be the the basically the thing we have to do insert a certain number of words wob before the first word of the song so that's the first thing after the last word the number may be zero and between words okay so we have to insert the word before the first word after the last word and between words at least one between any pair of neighboring words so let's uh indent this properly and then the big boy glues together all the words including wob in one string and plays the song so we don't really care about that the only thing we care about is this inserts a certain number of words wob before the first word after the first word and between words so at least one between any pair of neighboring words by seeing that we have this now we can see wob we and then we have wubb r wob and then we are the champions basically we get that so we have to remove the waps right remove ups between the boards a space and that's basically what we're getting so that's great i think someone just mentioned that in the comments so let's do that right now i'm gonna put these as comments and let's start con song decoder we get the song which is basically one single string and in this case this is a task where you have to use string manipulation so to do string manipulation we can either use regular expressions or we can simply you know find vobs and simply remove them so for now what we can do is we can um find each and every wob so we can do string dot find i think and then we need to put a space for each wob i think the best case for this would be string that replace what replace does is replace takes in a a string that we have to find or a regular expression but in this case we can use this wob so we're saying replace every wob with a space just like so and now some of you might be wondering well how did you know about this string that replace right um well this is something you're going to have to learn right you you just go through some this you're getting get this in time the more you learn the more of these you're gonna google and um simply get them so for the first time i wouldn't know this right this is not something you're born with so what i would do is i would google replace something with something and i'm going to teach you how to google right here replace uh something uh replace string javascript so how can we replace something in a string okay javascript string libraries so now i'm teaching you how to google which is an extremely important skill as a programmer as you can see usually what you want to go for is the mdn in mdn you can find everything so in this case i'm going to click the string.prototype.replace and in there you'll be able to see how this method behaves as you can see if we have this thing we want to replace this specific thing in this case dog with monkey and that's it uh let's try with that and then we're going to try your guys solution uh that you're giving in the comments so in this case string that replace and we want to replace wob and we want to replace it with a space so now i'm gonna simply console log this because this is not yet i think a done solution so let's try doing that and let's click test okay uh string is not defined yeah because we have to do song that replace i'm gonna do run another test and there we go what we get uh what should we replace can we see the real output i don't think we are we're getting just that but let me try to return this thing instead of replacing it's a return song that replace wob with space uh option replaced by one space we're getting only the tests here i'm interested if we can see what our actual output was uh let me see so in here we have sample tests instructions and then skip and there we go let me try clicking attempt if that's going to give us something more no warp should replace with one space expected a okay great so it it doesn't work uh someone mentioned replace all that's a great question let's see does this have heil i think uh like this let's just search javascript replace all let's just check it out and there we go replace all javascript great um someone is asking is this my first experience with cold wars yeah i used other websites to to do practices but i found cold wars to be great uh so that's why i'm basically with you guys going for the first time uh through codewars so we can use replace all and let's see how that's gonna go and we get song that's replace all is not a function so let's see replace whole method returns a string and we're calling that on a string so that's good and that should actually return something uh there may be multiple spaces here we're gonna see no there is only one here so if i do that and if we click test song does replace all is not a function let's just use this the old replace and then we're gonna set it to global yeah but i'm not i'm not interested i'm interested in why i'm not seeing the output of this uh so i want to take this thing into the console maybe i have to call it down below uh or you know what at this point we're just gonna move to visual studio code it's gonna be easier so i'm gonna go here open a new javascript file and that's going to be codewars.js in here you'll be able to see it better so after i do this we're going to save it and right there there we go we should see the javascript tile i think right now if we saved it as js which i think we did there we go we're back in awesome so yeah we can just use these simple regex but let's just see what's happening in here i think we'll be able to run it with node so what we can do is open the terminal let's do that right now and there we go make it just a bit smaller there we go so i'm going to take the whole thing right here and now we can test it with node properly so we're gonna do something like this um let's see uh yeah we're gonna call the song decoder and then in here we're simply going to pass in our demo string which in this case can be this okay there we go now we should be able to run it with node so if i do ls here i can do node uh code words i i i didn't name it properly but that's going to be code words dot js in this case if we are in the right directory which it looks we are not let's see where are we that's going to be into cd desktop and then we can do clear and we can get our code boards great we need to console.log it so i'm going to do that right now awesome and let's run it great so we are getting this which means it is replacing this but let's try using replace all as someone in the comments suggested in that case replace all is not a function so looks like replace all is not yet built in but what we can do is we need to use a simple regular expression so in here i'm going to go back here and what we're going to do is we're going to use the regular expression instead of a simple string so if i go back in here we can take this regular expression and if you don't if you guys don't know what regular expressions are they are basically the ways for us we can loop or find something in the string so in this case i'm going to replace this wob with wub in a regular expression and there we have the g g stands for global so i think this should replace it on a global scale but we're gonna see so if i do that right now let's try to run it and there we go this is successful yeah it looks like the looks like the replace all method didn't work i'm not sure if it's not yet built in or something but yeah basically whop g is correct and i see you guys in the comments someone was saying that so that was great wap g and now let's try with something different in here or you know what we can immediately try bringing that as a solution let's see if i go back in here and replace this function test okay multiple spaces should be replaced by only one space okay so we need to replace swab and we need to find multiple occurrences of of the same thing so in this case we need to use we need to use the regular expression so i'm going to use regular there is a thing called regex101 we're going to use that so if you go to regular expression 101 in there you can test your regular expressions regex101.com so in here we need to find a regular expression uh that basically finds all occurrences of wap not only one so if you do wab slash g as we did right here so wab g you can see what's going to happen if we have wubb wub there we go wub test wob it properly finds all the matches but unfortunately if it finds vob wubb it's going to take them as different inputs so what's happening is that instead of here giving us a test or let's do something like this wob and then test one instead of giving us the word test and then test one what's gonna happen is they're gonna be two spaces in here and the exercise doesn't want that we need to have only one space so we need to find a way to match not only one singular wob but also all incoming globs i know there are some things to match multiple things but let's see uh how should we match that of course if we do this then it's gonna take them but that's only only gonna work for two different occurrences is there someone in the chat who knows his regular expressions uh we need to use something here to make it basically work for everything um for as long as we have like if it's we need to replace all instances of that in this case let's see if that's something help us here we need to use start of string end of string sequences sequences quantifiers there we go this is it more yeah so what we need to use is right here zero one or more that's it plus so we need to use a plus uh so if i go here if i type a plus let's see if that's gonna do it that still doesn't do it maybe we have to make this into a specific block so i need to make this maybe like this let's see still doesn't do it do i have to use forward slashes nope let's see so we have to match this whole word do i use it as a string still doesn't do it let's see uh so if i do that and there we go a plus oh it's taking my maybe plus as a yes that's correct but we need to make this into the whole expression by itself so maybe by there we go this is great uh so what's happening right now is i place this into first capturing group and then we can have many wobs as we want so in this case if you use this regular expression i think it's going to take them as one instance while blob not as two instances so if you go back in here or rather in here we can try using that regular expression right here i'm going to click test and looks like okay that's great we fixed it so that part is fixed but now in this case uh looks like we also have to remove the trailing spaces so we need to remove the spaces at the start and spaces at the end i think there is the javascript trim method so if you use string that trim that should actually work let's see string that trim removes white spaces from both start and the end of the string you can see if we have a greeting or hello world here that's gonna remove all the white spaces before and all the white spaces afterwards so that works great now we basically have to use the string that trim method so if i go back here what we have to do is afterwards this we simply call the dot trim method as this is a simple function we can simply put it in one line and that should be it this is an instant return so now what we have basically is just this function con song decoder and let's see if we'll be able to bring back the old songs from dubsteps if i click test this actually passes you have passed all the tests that's great we just learned some regular expressions as well as the trim operator and also the replace operator so i just want to ask you guys can you see how meaningful this is right how well it works how how well you can learn things and you can learn the the language itself just by simply doing some exercises and this is also great for problem solving in general i know that some people ask me about some problem solving so yeah this is a great way to do some problem solving okay great with that said we're gonna try to attempt to solve it so let's see if that's gonna do it there we go all tests pass and we are ready to submit the solution i see that somebody asked will this video be saved for the future reference most likely yes this is the first of this kind so i'm still like wondering if it's going to be saved or not but most likely yes um awesome so let's see now what i'd usually like to do is see some other exercises right there so how other people solve them in this case you can see this one is identical i replace wob plus and then trim that's great but what did somebody else use split okay so this was some i think somebody mentioned this in comments um to use the split so in here we split it by wub we filter it uh we pass in the this is kind of unreadable uh this is not so readable but this is um yeah but that's fine i guess this one take a look at this one uh we also split it but then they filter it in programming this is used uh this is called a magic number or a magic variable why magic because it doesn't have anything um like any meaning why x why did he use x in a filter and not why a meaningful variable name so we're kind of lost here we don't know what this is solving in this case i like his solution he's this is basically the same as ours you can see a lot of people just did the same thing as we did uh i don't want to look at this one uh yeah so you can see this one is obviously oh this is i'm not sure even i'm not even sure what's happening here uh so that's good and let's keep moving forwards yeah generally most are similar to ours so how are you satisfied this is cool this was very cool so i'm going to say very and that's it i guess we can move to the next cata okay great um and now let's see we have highest and lowest uh and um now i want to ask you guys what type of catas do you want to do and where can i see what kata is this i can see this is the 7q highest and lowest would you guys like something simpler or something more complex okay great while i'm waiting for your questions let me just uh tell you this you know how we used a lot of methods uh in in last one i just want to show you a course i released just not so long ago basically a few days ago if you take a look up here what i did basically and this could be the answer if you're asking whether you want to you know how you learn those methods that we use like the trim that replace and everything else in this course i basically covered all of these methods four arrays uh four strings as well you can see there is strings intro basic properties substring split a string repeat string reverse string and everything else is in there so if you take some interest you can basically go to the course it's just www complete path to javascript mastery and yeah feel free to take a look i might even give some discounts for the course just at the time of this um interview but yeah awesome so we're doing this it's crazy this seems like an interview right because usually on youtube you guys always see like people having pre-recorded videos and these are usually you know pair perfect they are pre-recorded everything is correct there are no mistakes but in this case um i'm also a human right so we all make mistakes and you guys here are to help me and we are here to solve these together so let's let's keep that you guys want it more complex which is great looks like this highest and lowest is one one of the simple ones so what we can do uh let's just you know uh let me see how can i choose between the harder ones so if i go here to home i can rank up i guess i can choose javascript fundamentals let's see what else can we do there we go 7q you know what let's just go yeah let's just do this one and then you guys are going to tell me how we can skip it and then move forwards but this one seems like a really straightforward one so it says create a function to determine the sum of two lowest positive numbers given an array of minimum four positive integers no floats that basically means that only whole numbers are going to be passed in so for example when an array is passed like 19 5 42 277 the output should be seven uh because that's the sum of the two lowest integers five and two and that should return um and then output should be seven okay so this one should return this thing here and i see you guys have some more questions is c plus plus better or javascript for speed and interviews uh c plus is more low level than javascript is so in that case it's going to be faster as a programming language but each one of these has different you know purposes javascript is used on the web c plus plus is used in a lot of different things um and then somebody asked are fang like facebook amazon google netflix and all of these companies impressed when we when we write code in c plus plus java or js i guess everybody has its own programming language it's fine whatever you do so okay let's let's let's keep going with this this one is not as easy as i assumed it's going to be first we have to find two different lowest integers in this array and then what we have to do is we have to add them together and return them so in this case i'm gonna take this comment right there we're gonna pass paste it right here like so and i'm also gonna take this function right here somebody said uh or you know what i'm not going to look at the comments for this one let me try let's try doing it ourselves uh usually you want to try doing everything yourself before you see the solution online so i'm going to return this or i'm going to make this into an arrow function and simply we're going to take this as an example once we have the array of 19 5 42 277 the output should be 7 because 5 plus 2 is equal to 7. okay great so first of all we have to go through these numbers and then find two lowest ones what i would do in this case is i would sort them so i would do numbers.sort and that sort is gonna sort numbers um not correctly uh that's that's uh that's that's the thing you wanna know with sort it doesn't work as you would assume it would so for example if i pass the numbers or rather if i make the numbers i'm gonna mock that right here now if i make the numbers equal to uh one two three or let's do something like five eight seven one two doesn't really matter and now if i pass them in here if so if i call the sum of two smallest numbers and if i pass in those numbers you would assume that it's gonna rank them up from lowest to largest right if i do this and let's just simply console log the output if i do console log output what's gonna happen is it's not or rather it is gonna simply you know we have to return this but it is going to sort them properly but the question is why it will those numbers but it won't some other numbers so as you can see we do get 1578 which works but now take a look at this if i now add for example 11 22 55 and any other number like 123 you know anything really anything if we do this now do you think it's gonna work now let me know in the comments i'm gonna run it now and there we go we get 1 11 123 22 3 5 55. so obviously that doesn't work right so what we want to do in that case is we want to use a sort function to make it work so we don't simply want to you know do this because it's not going to sort them for bigger numbers we don't want to add 1 and 11 what we want to do is we want to do 1 and three in this case for that we have to use the proper sort method so let's find more about sort how we can do the sort in this case of course we can google it so you can do here you can google you can do sort and sort is going to tell you it sorts the letters or rather alphabet characters properly like d f j m but with numbers it doesn't sort them in an ascending order you have to find out how to work to that so i want to show you a lecture from the course i did this because this is a really important part in javascript so if you go to i think that's going to be in here in strings and then we want to sort a string so i'm going to search for array sort yeah that's going to be in the array we have to do is we have to have a separator or rather a sort function so in here we want to have a function that sorts things properly in this case we're saying made two things be a and b and then we're doing a minus b this is going to sort numbers in a specific order if we use a minus b that means that it's going to put each and every next character um before the first one so it's going to sort them from lowest to highest characters or rather numbers but if you exchange this to b minus a then it's going to do a descending order let's try that so if i go back in here and if i try doing something like this a minus sorry a and b and then we return a minus b now if we try running node as you can see we get 1 3 5 7 seven eight now the sort works as it should that's great so now once we have numbers sorted what we wanna do is we simply wanna take the first two numbers right uh we know that this returns an array so what we can basically do what we can do is we can immediately do some array destructuring const first number and then we can also do const second number like so and this is basically pulling the two first numbers or the two lowest numbers uh from the array and then in here what we can return is first number plus second number and if i'm not mistaken this should actually work let's check it out in this case we're passing this but let's pass our original array that they gave us so in this case that's going to be this what i want to do is have that and now let's try it out as you can see we get 7 as the exercise says what do they say nice right i see one comment or question asking why doesn't it subtract the numbers uh what's happening here is this is a function in here that's uh responsible for sorting so we're basically saying have two characters like a and b it's going to go through the characters and it's so it's going to say a minus is basically saying sort them in an ascending order make the 5 exchange by 19 because 5 is lower than 19. then it's going to go keep looking is 19 greater than 42 it's slower than so we're going to keep it there 2 is lower than 42 so we're going to move it here then 42 is lower than 77 that's also going to work now 5 is lower than 19 that works but 2 is also lower than 19 so we're going to move it here and basically we're left off with this and one final thing is going to move five or rather two to this side and that's our sorted array that we're gonna have and then two and five are two lowest numbers and that works we immediately get our response with that said we can basically take this and can we simplify it even more i don't think we do um that's basically it yeah this is how i would leave it so we can go back with this and we can go right in here and simply paste the code now let's try to test it as you can see that solution works and now let's try attempt it and see if it passes it passes for absolutely all tests that's great we can submit the solution and move forward okay it's submitting it right now and i see someone in the comments i think uh let me take a look great uh adam asked is the course available uh yeah the course is available the link is in the description and in there you should also have the discount code uh for the youtube so everyone who comes and buys the course from youtube uh it really it basically gives you the discount you had just have to enter it at the checkout yeah basically the link is in the description uh adam make sure to check it out awesome with that said let's move let's keep moving forwards so check out codewordsred i guess that's a subscription we don't want to get that for now and then we can keep moving forwards so sum of two lowest positive integers uh isn't this what we did right now yeah there we go these are all the solutions so what they did is basically the same thing they got an array and then what we have is number.sort the same thing but now take a look compare this approach uh with the one that we have i think if we go to my solution we should be able to see it let's see uh only show my solutions it's loading right now but i just wanted to let you know how this thing looks so much worse than es6 because you can see we have a function here a and b we have a return here uh you know everything is here but only the logic should really appear yeah there we go so this is the solution so you can see how much cleaner does it look like great so now let's move to the all solutions and let's let's see what's happening there it's loading a bit slower since we're on stream but that's that's great no worries great so we have that one oh take a look at this one this one is basically identical as ours that's great that means that you usually did it correctly because a lot of people really did it the same way um the same way as you did it in this case he only used var and a function but it's basically the same this one this one on the other hand uh yeah you can see we're doing we're doing a for loop here and he decided to go for a basic approach of just using a4 or rather he did more of the things himself rather than using a predefined predefined you know sort method that javascript offers so this could even have the you know higher or rather lower complexity someone asked about the big o of the sort method that's a great question and the complexity of the algorithm we could go a bit into complexity of algorithms but let's try to get the complexity of this one so what we're doing is we have the n numbers right there and then this loop is looping n times and then he has a simple if else and then that's it so the complexity of this algorithm would be o of n basically saying that um as we put more characters into it or numbers as we put like five characters the complexity is going to be 005 and if we keep moving if we add 100 characters it's going to be o 100 so it's not going to you know extend exponentially or it's not going to be less performant if you have a lot of characters on the other hand a great question is what is the o of n of the sort method is this more performant or is it not um and somebody said surprised that nobody used min 1 and mean to approach uh yeah somebody yeah somebody did a virus great you just noticed it so yeah that's that's cool a lot of different solutions a lot of different things this guy you sort them slice them reduce them but it also works so that's it um and we can even google that you know let's do that right now so we are interested in o of n so o of n of the sort javascript right we want to do that so let's check it out right now of sort javascript let's see if google is going to give us uh something so let's see syntax for sorting in javascript you may yeah looks like it's o n squared let's take a look it's always interesting learning these complex things this is you know more complex than simply solving the exercise so yeah there we have it tldr too long didn't read we just got the explanation so in this case for a raise containing 10 or fewer elements time complexity is that sort so what we can do is um it's yeah it's o of n squared and space complexity is of one yeah great so basically the solution with a normal for loop uh what is happening is that um you know yeah it it simply it is more performant but this way is simply easier let's say for us what we can do um and um let's i was just thinking about something to sort it yeah let's take a look the complexity sort uh i was meaning to pull some kind of a visualization for you know sorts and stuff like that but this also it's it's not that big of a deal let's simply move to the next one so in here let's go to the next cata and let's see what's happening the next one is list filtering how are you guys doing by the way we're just doing a live stream where are you from how are you doing what's happening you loving the stream so far i got a question uh from uh bogdan here and i'm gonna put it in here in in comments because it's a great question so he asked what's your opinion on algorithms and the big o notation when it comes to the front-end more specifically front-end developers so what should front-end developers should they even be concerned with the big o notation uh let's see so that's a great question uh what's basically you know if you're working in low level languages like c or stuff like that you're really concerned with complexity or rather with speed and efficiency you're most likely working on some machines that you really need to be performant and in this case when you're working with javascript you're most likely working on web and you don't really care about you know the efficiency that much because the difference of o of n and o of n squared in the situation where we had the sort method and we also had the normal for loop method you know for for 10 inputs what's going to happen for 10 inputs the complexity is going to be uh 10 squared here which is basically going to be a 100 and in this case it's going to be 10. four thousand inputs this is gonna be a thousand and this is gonna be ten thousand but you know javascript actually sorts this in i would say you know less than less than a millisecond so if this took less than a millisecond something like this if it took this much time this much seconds and this thing took uh let's say this much second because it's it's not that big of a difference but in this case if you have a larger array like thousand elements or something this thing could have taken literally one millisecond but this thing wouldn't be so much more performant it would be like 0.01 maybe who knows but it's not that big of a deal you cannot really notice the difference that much in front and development so i would say don't really worry about that too much try to make your code as readable as possible but still it's good to have that knowledge in your brain and just just you can you know think about it from that side that's great awesome so with that said let's move forwards let's see do we have some more questions or comments uh great um will the stream stay afterwards most likely yes since this is the first one i'm still thinking about it but most likely it will and if you guys like it i will even you know do more live streams of this type so we have some people from ukraine south africa someone asking is there a schedule or this is a one-time thing this is the first time if you guys seem to be liking it i will make it a scheduled thing and we'll see more more of these happening more often netherlands joining in and uh that's it so let's move let's move forwards so is in this cata you're required to given a string replace every letter with its position in the alphabet this is an interesting one so if anything in the text isn't a letter ignore it and don't return it but replace letters a1 and b2 and so on so we have an example alphabet the sunset runs um sunset sets at 12 o'clock should they turn 20 8 5 and so on great we have some people here from czech republic los angeles serbia awesome welcome to all of you so let's keep moving forward uh in this case we want to take all the letters from the alphabet and we want to map them as numbers so in this case i'm going to take this whole thing in here and we're going to paste it to our visual studio code i'm going to take it here make this a bit smaller it doesn't have to be that big and make it as a comment i think somebody asked how do you make everything as a comment at once you simply highlight it and that's going to be command or control forward slash um great so yeah let's let's do it so we need to first make take the alphabet and then map it to numbers uh what we have to do is we have to go to i guess google and then we can have the alphabet numbers uh that's going to be the easiest way to do it i guess so if i do this alphabet numbers and simply copy i'm too lazy to write the alphabet and the numbers for each letter myself so myself so what we're going to do is alphabet numbers copy and paste that should be good uh let's see we're getting some fancy letters and numbers we don't want that while that is loading uh someone asked me where i am from i'm from croatia nice to meet you and somebody's asking if they have some knowledge of javascript um should i also learn something like view angular or react you should first master javascript like really really master it that's the main premise of the course i created is to fully master javascript and only then you know move to these frameworks because if you move with to the frameworks without having proper knowledge of javascript you're going to have a hard time because all of these you know frameworks and libraries basically are simply javascript nothing more and now looks like this is not going to work because i'm trying to be too general so let's simply do javascript just so we can get an array of of alphabet javascript let me see alphabet javascript array yeah this is what we want to do basically how to generate an array of alphabet in jquery no we don't want jquery we're going to just get back yeah somebody say you can just uh type a b c d e but we're just gonna uh you know copy it let me see if there is a way to actually take it immediately generate the alphabet in javascript looks like that's gonna work i know this is taking more than it should be but you know it's live so that's fine so this is crazy there we go this is what i wanted and then i guess we have to make each one just take the numbers for that this is also a cool one we generate numbers and then for that we take the string but let's simply take all the letters from the alphabet like this so i'm going to take that and now we have all the letters the letters are going to be both uppercased and lowercase so we're gonna have to take care about that uh somebody was nice enough or kind enough to bring them all in an array for me in the comments so uh that's that's great so if i do that i'm gonna simply take that thing that somebody pasted in there let's hope they're not there they're not mistakes there he even had const alphabet there so thank you so much i think it's daniel great um and now what we can do is we can basically have our function did i copy the function i don't think i did let's take the function and then in here we want to take the text the text is going to be this thing so if i take that i'm just going to put it here as a reference okay we have it here as a reference and now what we want to do is we first want to take that text and we want to put it to lowercase to lowercase because all things in our uh in our letters array are lowercase and then this thing can have uppercase letters we want to make it you know unique we want to make it all universal so in this case we're going to do that and then text that to lowercase and what we can do is return it once it is lowercased we want to see what characters is it having so we want to loop over the characters of that thing for that what could we use we can use the uh for method to loop through each character do we have to take care of the spaces i don't think we have no the s is there so we basically have to remove all the white spaces as well we can do that by doing dot split or dot join that's going to be dot join and we can simply join it but this this goes on an array this is a string uh so let's first let's first yeah a string used to separate one element from an array what we can do is first split it i guess yeah let's put it into different words and then join it so that's split or you know what this is already what do you guys say should we just join them join the words right there or something something different i'll try to answer the questions while you let me know so somebody asked for the course please consider giving regional discounts i am giving discounts both to students and to people from low-income countries so feel free to shoot me an email you should be able to find it in in the description i just say where you're from and and that's it i'll give you a discount somebody asked have you ever considered living and getting a job in the us that's a great question who knows life is long so yeah we'll see on the go um great so in this case what we want to do is string the lowercase and we want to map over the letters i guess so we have to find the value of that lever and of that thing in this array and that's going to be array dot index off i think so alphabet dot index off of a specific character so in here we need to put a specific character in this case uh you know the easiest way would maybe be to loop over it but let me just see this uh remove white uh remove a white space uh remove spaces from string even some things like this you know it's still you can still google it it's gonna be right there pretty easily let me show you that so yeah we basically we can use the replace method one more time to replace all the white spaces how handy is that right so we can say uh that lowercase and then we can say dot replace and we can use that uh method we learned string that replace we can pass in that regular expression and simply remove that so how can we test it out now well let's simply try to console.log this thing and then see what that is going to give us so if i do that let me see there we go and now what i can do is i can simply call the function so that's going to be alphabet position and what we're going to do is we're going to pass in this string right here and let's see how it's going to look like there we go let's call this thing right now and it's going to say syntax error an invalid token uh let's see where is that invalid token that should be at line 16 seems to be so no maybe it's happening right here somewhere but i'm not seeing it alphabet replace uh invalid token const alphabet that seems alright to me but let me know if you see something that's uh that's not correct line nine yeah yeah it's definitely in the alphabet did you daniel did you send the correct alphabet over let's take a look uh looks like there is a comma missing i guess or something i'm not seeing it on the first one so yeah it's definitely there let's see what is happening invalid token and this is this is an array of strings all of this seems to be okay okay mno p q r s t u v w x y z this seems fine to me what do you guys say we can also use the different one that we found should also work okay let me take a look at that okay we can also use this thing uh looks like this one doesn't work so like this alphabet and then what we're gonna do is say const alphabet is equal to the string of characters and simply split them like so to make them into an array so if you do that now let's see if this is going to do it let's call it one more time there we go the sunset 12 0 clock and do we have to remove this character or not uh i'm not sure if they specified that uh let's see yeah i think we have to remove all special characters right so for that we have to find a regular expression to remove all the characters uh so what we have to do is basically we can google it again remove special characters from string in javascript you know you're going to find yourself googling a lot which is not a good thing so if i bring this back here yeah you can see a google something but i'm going to show you an amazing tool right now which i'm actually part of i want to help you use that so in here we say string that replace and it basically says take all the characters that are alphabetical characters we can do that let's do that right now so i'm going to take this regular expression and simply instead of empty i'm going to paste that so it's going to say keep all the things that are alphabetical but i don't think yeah yeah this is going to do it or sorry that are not so this is the not operator so we have to remove everything and in this case are we going to have that special character as you can see now we have the spaces so we have to remove this i think and there we go the sunset sets 12 o'clock that works no special characters no spaces that's great so now we have all the characters now we have to loop over them and then display a specific character so since this is an array of characters what we can do is we can split them i would say so let's do something like that dot split we're going to split each and every character into an array and then we can map over that array where we have each letter and then what we want to do is we want to take the alphabet and say in which number or in which space in this alphabet is that specific letter in and this part is going to return it now what's happening you can see prettier did this for me we have a lot of methods happening here a lot of logic so what i'm going to do is i'm going to exchange this to a const make this into an arrow function and finally what we're gonna do is just have it like this we can have it an instant return so just remove the curly braces and have it as an instant return with simply parentheses it just spaces it out like this i don't quite like that so i'm going to go back to having curly braces and a normal return statement so now this should be so the solution let's take a look i'm going to console.log the output of this and then finally let's see what the output should be so the string should be like this now if i call it we get minus one and that's it so it couldn't find the index off of a specific letter looks like um let's see what's happening so we're mapping over it we have we split all the characters oh i'm missing a separator right there this needs to be a separator to separate all the characters one by one so now if we go there we have 19 7 4 18 20. oh we are taking the index off of the letter right but we have to make it one step bigger because uh this is an index indexes starts at zero but we actually want to increment it by one so in here we're going to return this plus one in that case if we run it again as you can see we got 20 8 5 19 all the way to the end 15 3 eleven fifteen three eleven this seems to be correct i'm gonna take this function and try to put it as a solution yeah great i'm gonna go back in here they lost my browser and there we go awesome i'm gonna paste our solution and let's click test it didn't pass because i didn't copy the alphabet what a mistake there we go i'm to copy the alphabet right here let me close this and going right here paste the alphabet and simply test it out in this case it didn't pass so looks like there is one one small thing we have uh 28 oh who would have who would find an error right here right uh instead got 28 5 19 21 14 9 5 20 19 5 20 19 1 20 20 23 5 oh we have to have oh it expected a string yeah my bad so it has to return a string and not an array see that we have it currently as an array so what we have to do is we have to join it back into a string so we have to have dot join and then join it back into a string i think this is actually gonna do it if i now click test it still failed because we have to join them but add a space in between so i just added a space right there now if i call it there we go the solution passed uh bogdan also helped us solve something and uh yeah that's it we sold yet another solution uh yeah somebody roger just said uh dot join and there we go we just implemented our dot join and everything works perfectly now you can see we used a lot of methods right here right and if you didn't know these methods at the top of your head which you of course cannot right because you're just you know um starting to learn javascript how are you gonna know that these even exist uh well of course first you have to go you have to get some practice but where is the time or where where is the place that you can actually learn these methods right well of course you could just google them or try googling each and everything but that's kind of slow like if you if you don't know the two lowercase method you would have to google oh how to make a string lowercase in javascript right and then you would go oh how to remove all special characters how to um split all the characters how to join it back into array how to map over them that loses your time you're wasting time on that uh but what you can do is you know first of all you can you know find a lot of places where you can find these but that's why i did in my course in the course you can see we have all of these different methods starting from strings in detail where you have all the different things about strings like change string case uppercase lowercase search for a substring get a substring split a string repeat trim reverse and then for arrays even more methods so this is extremely useful if you want to take a deep dive into javascript i would definitely suggest taking a look in the description um awesome great and with that said uh even though yeah i did this i still had to google some things right you cannot simply remember everything but for that there is a great software called mem.dev i'm not sure if you have heard of it but it's a great basically website that allows you to practice spaced repetition to learn things that you want to code see never forget what you learn so now that we know how to use this you have to actually remember how to use them how to write them just so you don't have to go back to google every time so now let me show you how i would use that you would go back in here log in and then you can see i already have some things that i practiced right here for example how to push method works and stuff like that but basically the only thing you have to do is click create a new card and now in here you can enter a code snippet to remember so what we could do is we can practice each one of these methods for example let's take the first one at the start i'm going to take this thing and then we can go back in here i'm going to paste this make this equal to javascript and we can say return text dot to lowercase or we can simply do it like this text that to lowercase now what you want to learn here what you want to remember you simply highlight it i highlight dot to lower case that's the method to lower case this card is about javascript and i just learned how to lowercase a string like that okay lowercase a string we save it and that's it our card is safe let's do a few more let's do a replace so how or let's do a split split a string so i'm going to copy this string dot split i'm gonna highlight this one and let's save it how to split there we go and let's add a few more let's add a map so in this case you can learn how to add a map in this case that's to be array dot map and we're going to just paste ours there we go so now i'm going to save that as well and i'm going to highlight this whole part or you can highlight basically whatever you want in this case we're adding letters in this case i would just want to do map so how to uh map over the array great so if i now save these you can go to your cards and you can train you can train on the cards you added so if i click train here and start training now we can go back to remembering what we had so it's in here it says map over the array that's an easy one right we can just say map that's it we can say and now it asks you was too hard for you or was it easy it was easy right this one i added this one previously add elements in the middle of the array oh this one is stuff right we have to use names dot and that's going to be most likely slice where do we want to add it we want to add it maybe at the second position and what do we want to add in here we had john bob david and mark but looks like we added jenny and johnny so we want to add jenny and johnny after the second place let's see if that is correct and looks like it is just a bit off we have to say two and zero i completely missed that second thing so i'm going to say this wasn't too easy this was just good now we get a second one but i think you get the point you add things onto this and then you practice that way you won't have to google all the time you will simply be able to remember everything on top of your mind and that is on how to be a good developer first learn something learn all the methods and then practice them that way your problem solving will be so much easier because you're going to have to worry only about the actual problem not how the methods themselves work great i've been talking for a long time let me take a glass of water and let's keep it moving uh so let's go to the next one i'm gonna click where where do we have it this is already done right so i should be able to attempt it and then we should be able to submit it everything passed let's submit it and let's see other people's solution there we go it's submitting right now i think we're gonna do just a few more maybe one or two what do you guys say okay great it is submitted and take a look at this uh uh first of all we unlocked a new privilege 25 honor now you have the ability to vote on beta kata that's cool i guess but take a look at this oh we have one more we ranked up nice so what's happening basically is that the first solution right there is almost exactly the same as ours that that's amazing uh and then the second one is really similar but it loops through the things and it uses ascii codes to generate alphabet we just took the easier way and we just copy the alphabet right there oh this one is cool uh text to uppercase so he decided to go to uppercase not lowercase that's fine he also used the uppercase replace here the same as we did he used a split the same as we did but then he optimized his map to use the character codes and not the character not the alphabet we created so this could be a better solution that's great okay now let's keep moving to the next one i guess if we click next one here we'll be able to see what it is and this one is vowel count let's try to solve that one we can click train um somebody asked uh there is exercises like sorting or group array of objects i i think there are a lot of exercises you'll be able to find something you want to train on and then adam said uh thank you for creating for showing memdev yeah no worries it's it's a great software to use with that said let's keep the pace up and let's keep moving to the next one which is vowel count uh remember the number of count of vowels in a given string we will consider a e e o u uh but not y and the input string will only consist of lowercase letters and or spaces so for example if we if we have the word abracadabra it should be five because it's a a a a a five vowels great uh with that said let's grab this move to our visual studio code and let's try to implement the logic for counting the vowels now i'm gonna not do anything here for a minute and i want you all here who are watching to turn on your brain and write me in the comments on how would you solve this specific problem how would you count the vowels okay comments are starting to get in uh leon said uh for off loop uh and yell said create a regex to only count the a and i guess everything else only the vowels uh and then somebody else said if character is equal to a e and then all the others then increment the vowel count that's that's that's nice solutions um let me see yeah let's try with uh well let's try with leon solution first i'm gonna copy his whole thing here and let's try let's try by doing that so he said take this take the whole part if character is equal to this in this case we would have to make a four off loop right or we can make a for loop it doesn't really matter uh this is a for loop but for off is sometimes it just looks a bit better so we can say four off and we can say cons for const letter like this const letter of a string so letter of string and then if letter if letter is equal to and then he said a or all the other vowels what we can do unfortunately we cannot simply do it like this with commas that's not going to work uh we need to have a separate if check in each one of these right so what we can say if letter is equal to this or if letter is equal to this or letter is equal to this or letter is equal to this i missed it uh like this always use triple equals by the way it's it is simply better and or letter is equal to this thing let's take a look yeah i completely lost myself there in all the equalities so we have this letter is equal to and there we go something like this should work right if we add all the or signs in between not like this and then i i'll completely lost myself there but this this should do it so if i do this letter is equal to a letter is equal to e letter is equal to i o or u in that case we want to do something uh so if i take it like this in that case what we want to do let's see if the preview is going to move this into each line for me looks like it it won't but let's let's do that ourselves it's just going to be easier to see what's happening so we have all of these so if this is the case in that case uh somebody say i missed the i it's there i hope so yeah this is gonna work and now what we want to do if this is the case we want to increment the uh vowels count right so that's to be vowels count plus plus or simply plus equal to one it's whatever you choose and then simply return vowels count right let's see if that's going to work so if we have the word abracadabra as we had it here that means that once we call the get count and make let me make this a bit smaller once we call the get count and put the word abracadabra in there we're gonna also console log it and now let's see what the output is going to be there we go this doesn't work we have something with this if that's not good for that's good wallace count the areas in line five looks like that's interesting what did we miss a letter letter letter it seems all right but looks like it's complaining about something so what is that if letter is equal to this oh yeah or or or yeah it looks like it's not getting it right now which is kind of weird but also fine this is i would say this is not the best approach right because you have to be really precise on what you want to do there uh and yell suggested uh let me see regular expressions that would be great i think that would be a bit cleaner uh somebody else said make an array of wobbles and loop through check if current character is included in the vowels array that would be better that would be better adam said i define vowels both lowercase and uppercase and then loop through them with an array then he would increment vowels by one every time they see a vowel somebody even gave the whole solution right there arvin awesome so let me take a look yeah i would use i would most likely create an array with characters a and then all of these and then simply increment the count if they are included in the array so let me take the solution that somebody else provided and let's see how clean is that there are some good programmers here watching this video that's awesome so this is the method let's see first you know what let me without without even looking at it let me try testing it and see if it passes so currently it doesn't pass because n is undefined uh what did he mean by n let's think about it so he's looking for these characters without the y and then or c dot that's character dot repeat and uh yeah so this is um a lot of logic here happening i wouldn't say it's clean it's it's really clean because it's you know it's hard for me to read as soon as i saw it but i guess we want to split the string map over the characters and then if c matches so if there is a match then return a then return a c that repeat n so the number of times the character is repeating but why don't we simply increment that so why don't we have the let vowel count and then simply set it to zero at default um and then simply in there oh there there we go and then simply increment it so if this is the case we want to increment it and if that is the case we don't want to do yeah so this is this is kind of weird i wouldn't do that this way but still thank you so much for your solution uh maybe you had something different in mind that i'm not seeing right now uh but yeah so let's let's try doing that uh one step at a time so we have this regular expression which i think is good so we can say uh we can loop over this whole thing and then what we can say once we loop over it we have the i let's use the for off again because it's just a bit cleaner we have the letter and we have the string in this case what we want to do is we want to say if um letter dot match that match is the matter for regular expressions uh and now if that is correct we simply want to increment the vowel count plus plus and then return the vowel count let's see if this is gonna make it if if the regular expression is correct and i want to return the vowel count okay let's take a look and that needs to be inside of the function we are losing our second parentheses here and that's good let's try to run it and we get five which means this is correct uh ivan just sent a new solution which i get guess now works uh but it is just a bit more confusing than the current one so this is uh his approach i'm gonna put it here thank you by the way so much for sending it so this is his approach uh it's good but right now right off the bat i wouldn't know you know uh what is it doing let me let me try calling it and see if it works now yeah unfortunately there's still some errors uh but that's that's that's fine you know it's it's not a problem we have this approach we simply have this and then we are seeing if letters match these characters and if that is the case what we do is we simply increment the vowel count does this make sense for you guys what do you say um is this clear enough or should we make it even clearer for me personally i think this is uh this is just just good enough we could of course go for something uh more uh i don't know a bit shorter maybe or something but i would say this is uh good for now i'm gonna take it back here and what we're gonna do is simply test it out and see if it works oh it doesn't work it expected five but instead got zero uh looks like i accidentally copied it over the whole thing so let's do it like this i test it out and what do we get it passed all the tests let's attempt to pass the whole thing and it does work somebody asked how about using a reducer that's a great question maybe you yeah you meant to like the reduce array method that's a great question so reduce method is the most complex array method without any doubt um so what what happens is that reduce tries to get all the elements in the array so it gets all the elements in the array like a abc and the main goal to reduce is to output a single value so that that's the main goal of the reduce you know um in this case what we want to do is take in the array of characters in this case let's say abracadabra so it's going to be a b r a i'm going to do just the first four and then if this is the case we want to output two so question if we can use the reduce method is absolutely correct we can it is possible so to do that we would first have to transform our string into a into an array so let's do that right now string and we already learned how to do that right string dot and then we simply have to split it split it into specific characters now that we have that we can use the reduce method so we can use reduce reduce takes in two things and it also takes in the initial value so initial value is going to be 0 and the things that the reduce takes are going to be current value and also it is going to be the accumulator so its accumulator first and then the current value and i will explain how the reduce method works but let's first try to see how we can solve this without it so if we do this reduce method works that in in a way that it first takes the first thing so the initial value and then each time it increments the current value to the accumulator for example if the initial value is zero at the start it's going to start adding new values in this case a b and c to it but what we want to do is we don't want to simply add these because these are no numbers we simply want to take uh the accumulator the accumulator is going to be the current letter so we can call it like that letter and now if the letter is one of those uh then we want to do something so we can say if letter.match we're going to use the same if statement if letter.match then we want to return um we want to return accumulator or rather let's think about it we want to return accumulator so this is going to be letter no this is going to stay accumulator and this is going to be a letter my bad there so there we're going to return the accumulator uh plus one i guess right let's see if this is going to make sense i know this is a bit confusing right now but i'll try to explain it in a better way so if i do this right now and try to call it again we do get zero because we are not returning anything we have to return this const vowel count so in this case our vowel count is going to become this thing and that could potentially work unfortunately we're getting nan so let's see what the accumulator is equal to i'm going to kansa log the accumulator and let's see what that is equal to it is equal to undefined each time so this split potentially didn't work properly looks like it did but maybe it's getting oh it's getting it to zero yeah so this is the accumulator but the letter should be correct right so letter letter should be each time changing to oh it's not being changed that's interesting so it's still taking the just one thing um let's see why let's see what let's see what this thing is after we split the string right there i killed somebody asked about the accumulator that length that could be a great thing uh yeah that could fix it but let me first see if the console logs string that split that should be pretty straightforward right we should simply get a string into yeah we do get the characters uh right there um so for each character we are in there and we want to check each character what the actual thing is turn accumulator outside of the if i think that is the correct yeah that's definitely the correct thing let's take a look what's happening if we do that we get five awesome i see all of you guys yeah return zero on false you need to return accumulator even outside of the if block that's amazing great job guys so we have this i see there are a lot of great developers right here awesome so if you do this for abracadabra we do get the number five as you can see here in the console that works perfectly what we want to do is we can now simplify it we have a simple if and else in this case we can use a ternary operator so we can say if this is true then return this in this case accumulator plus 1 else return the accumulator like this and we can simply put the return in front of it considering that we just have one thing inside of there which is a return we don't even have to have to return we can just leave it as an immediate return from a function like this and then you end up with something that looks like this which is a huge function but i guess should be fine let's see we have a syntax error somewhere are we missing a parenthesis we sure are let's space it out properly so have the reduce and after the reduce what we have is the initial parameter and then we have zero right so if we do this we don't have it oh there we go i just found it so this is the thing that shouldn't shouldn't have been there now we have that we have the zero close one close the second and there we go that's great so now what we're doing is we're simply returning the output immediately right there if i save it that doesn't look all that good but we can even make it into one line arrow function we just declare vowel count here and then we immediately return it there is no need to even put it in a variable we can just say return and then put the value of the vowel count right there and then we're going to end up with looking something like this and we can even put it in one line this is a huge one-liner i wouldn't recommend doing that but you know if you have your code or text put to a bit smaller you can see it great but for something like this uh this would even make sense you know what let's just put it into a new line and put a normal return there it's just a bit more visible it depends on the personal preference so now i'm going to take this code and we're going to put it back to codeworks you can see this is our previous approach and this is the new approach now i want to ask you i'm going to take this approach here and i want to ask you a question which approach do you guys prefer do you prefer the first approach or do you prefer the second approach and i can even make a poll i think there is a uh straw poll online so i'm going to make a real time poll you'll be able to vote in the comments and then we'll get what you think so i'm going to say which one do you prefer uh and i'm gonna put that right here straw poll as you can see so which one do you prefer you're gonna say the one with the reduce and the other one is gonna be a bit longer one but also you know works great and now i'm gonna create that poll and i'm gonna send you a link to the poll in the description and then you'll be able to vote on that so i'm gonna share it url and there we go you can vote in the comments you can click on that link and then you should be able to vote i'm really interested in seeing what do you guys say which one do you prefer which function here makes more sense to you of course both approaches are correct it is only a personal preference i'm gonna wait for you guys to comment a bit let me know what you think oh did they did they close a poll i think i'm gonna open it one more time to look the the results coming in let me get it right here right now it's opening it right now and i think you guys are starting to comment uh and here are the results let's see there they should be populating in real time so if we get that okay that's interesting so votes are coming in i'm gonna i'm reading your comments people are saying somebody mentioned the concise approach because it looks cleaner somebody said i like the original one before you did that so it really depends on the person but seems that most people like the longer one because it's more readable and this is this is a thing that is definitely important to consider once you're writing code should the code be more concise like this one here is or should it be more readable for everyone reading it that that's a great question right and in this case looks like it's uh it's kind of mixed but still the longer one the more readable one definitely wins the round an interesting thing definitely would be to check if there is any time differences in the execution of these two but i don't think we can do that right now do we have yeah we do have the execution time but i don't think it's going to be much of a difference so if i just uh take this one the longer one and if i click test let's see how long is it going to take almost a second and if we do get to the second one so if i go back here and copy the more concise one and paste it right here let's see if it's going to be a bit different so it's it's generally the same so it's absolutely the same i wouldn't say there is any performance you know differences especially no at this level oh but this is just one test can i click the attempt to see if it's actually gonna do more tests let's see yeah it's also five tests uh for one second and if i get the older one back let's see what that's going to be and let's see attempt this one is also it's the same it depends sometimes it's going to be higher sometimes it's going to be lower but generally it's absolutely the same for a small number of inputs it's don't think about the performance on this level of of things it's all going to be extremely fast and let me read through the comments uh somebody said don't be afraid of functional programming yeah i agree with that uh the functional approach is quite okay and it seems like seven people actually write the like the functional approach javascript is becoming more and more functional so this here is you know functional because as you can see we have a string we use a function called split or a method rather we also reduce it and then that's it this way is more you know old school with for loop and everything else but yeah it's just a personal preference with that said let's go back and close this and let's submit the solution right here that's great we're going to take a look at some other people how they did it somebody said uh readable code is much more appreciated because saving lines can do only so much but longer code makes it more shareable i agree working with working with teammates you need to write proper code performance is not significantly increased exactly yeah performance is not increased by the thing that is increased is readability so it really depends on you now let's see if we're gonna get some other solutions from other people oh this is a great one uh this is actually an amazing one i like this it is even shorter so what this guy did um is that he did the same check as we did so he said if it if it is true then uh just show the length of the matches because this thing the match actually doesn't return one thing the match returns the i think the number of matches or the whole it returns the whole array of all the matches so what's happening here is if you take a look this is yeah this is definitely the better one so you can see const uh get count and let's do that right now or they they copy the whole thing i did so let me let me just get this get count right there close it and take a look at this one for this for you to be able to understand this you need to know what the string that match returns so in that case let's just take this string that match and let's console log it string that match i'm going to comment out all the other approaches and just put this thing here just to show you what does it return i'm going to open the console right now and let's run it we have a syntax error we're missing i think a parenthesis here if we call it one more time as you can see what this thing returns it returns all the matches right i see a lot of people are saying oh nice that's clean yeah it is it is really clean so it returns all the matches so what this is saying if there are matches this must be an array right so if this is an array give me the length of the array and that's going to be it the length of all the matches is the number that we want is the number that the solution needs or if there are not any matches then in that case simply put it as an empty array so that we can still get the length property out of it uh in that case the program is not going to break but it's going to work even if there are no matches and now knowing this we can even put this in a single line and i would say that this is the cleanest solution once you understand how everything works so this is great whoever did this 125 uh warriors that's amazing and i would say that's the best thing about cold wars is that you know you can um you can actually see other people's solutions and learn from them i see there is a question about the or uh array yeah basically if if there are no matches this is going to be null or undefined and if it's now undefined i'm not sure right now what the match returns in that case we want to have the empty array because otherwise we cannot call the length of the of null or undefined with that said that's a great solution but on the other hand you can see we have some with uh more complex things like double double for loops the complexity is going to be a bit higher in this one he counted every single letter and then he counted the vowels and then incremented the vowels that's a bit longer one right uh i think this one is is uh cleaner and also if we talk about complexity if you talk the big o notation this one has just a single single loop right so the complexity of this one is going to actually be o let me write that here don't mess with me o of n right so if you have five different elements uh what happens is that it's going to basically loop through it five times right if you have ten elements it's gonna be tame ten times but what's gonna happen here is that the complexity is gonna increase a lot uh what do i mean by that this here is loop inside of the loop so in this case the complexity is going to be o n squared in that case for five inputs we would have to make 25 different loops right that's not ideal uh i mean for for 100 or for 200 it's it's fine right 100 100 and then in here 100 but this time it is not a 100 it is actually 10 000 but imagine for 10 000 inputs the complexity is getting so much higher so much more complex so much less efficient right so this case this would be better than using a you know double loop um let's see what else do we have this is also a great one and yeah with that said uh i think we should be wrapping it up for today's episode right it is the first one in the row uh if you guys liked it just feel free to let me know in the comments what did you like the most what did you maybe like me to implement or to change and then most likely we are actually going to make this into a series uh so you can you know keep watching and then potentially keep learning let me know if you learned something from this and more specifically what is the thing that you learned somebody said that he learned about uh this thing right now the match and also the the or or operator right here let me know everything i'm gonna be waiting for comments and then i'll respond to those and i think we'll be wrapping it up it's almost uh two hours passed let me know how many of you also uh stayed from the beginning all the way till the end okay waiting to waiting for comments to start coming in because it's it's there's a huge delay you may know not you might you might not notice that but there is a delay between me actually speaking and getting the comments now the comments are starting to come in and uh malignant said enjoyed everything adam said learned about regular expressions and mem dev that's awesome i appreciate all the hard work no problem thank you so much uh awesome great so yeah i really appreciate you guys all here uh it's great uh teaching you it's great doing this live uh steve has stiff says this this was really good thank you so much it is the first live stream so i know we're still you know practicing and trying to make this better do you like this approach of having the the code wars and visual studio code and solving the solving these is at random basically not picking any exercises but going through going through everything and trying to solve it step by step and uh yeah great awesome so this is the satisfaction rate but this is the completion rate so see not a lot of people actually completed uh that awesome this is this is great um everybody says okay awesome great great to have you here guys we're gonna slowly be shutting it down let's see what we'll be doing next time uh so right now how do we actually you know keep going up this route so we completed cata we're almost up to promotion i think that's nice rank breakdown here uh overall 7q i'm not sure how this system works i'm going to research that a bit more but seems that we progressed quite a lot these are all the solutions we we went through 8q 7q 6q awesome so 6 completed things six different solutions here if you guys want me to throw a link to all these solutions i'm gonna do that um with that said make sure to take a look at the course uh the course is right here it's gonna be live i mean it already is live you can take a look at the link in the description a lot of great things shown in the course are actually here in the curriculum i try to make this the best you know course and javascript uh that that is out there i try to make it a lot of methods a lot of different things so yeah you really don't have to be waiting for anything you can start learning javascript right now check out the mem dev and if you don't have anything else to tell me in the comments see you in the next one that's it you successfully witnessed the first live stream that on javascript mastery channel thank you so much appreciate every single one of you and see you in the next one you
Info
Channel: JavaScript Mastery
Views: 25,583
Rating: 4.9752321 out of 5
Keywords: javascript algorithms, javascript tutorial, binary search, js algorithms, javascript arrays, javascript objects, computer science, algorithms tutorial, javascript algorithms and data structures, javascript for beginners, javascript algorithms for beginners, javascript algorithms and data structures projects cash register, javascript tutorial for beginners, javascript tutorial 2020, javascript tutorial full course, javascript tutorial advanced
Id: y-Qj4vRrwv4
Channel Id: undefined
Length: 111min 37sec (6697 seconds)
Published: Wed Oct 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.