Kanye West Net Worth Might Be Done Five Times: Understanding Object Frequency
It's fascinating to see how Google sorts and shows us based on the most frequent objects. This is the really cool part about learning how to attach a frontend like this, and boom! Your apps are going to be absolutely amazing and mind-blowing. Let's try this now. We're going to say `const user input = prompt('Write your sentence right');` and replace it with whatever the user types in. So, we're going to say `userInput` here and call it, yeah! Let's go ahead and run it.
**Writing a Function with User Input**
Now that we have our function set up, let's try writing it ourselves. We're going to ask the user for their input and then replace this with whatever they type in. We can say `const userInput = prompt('Write your sentence right');` and call it. Yeah! Let's go ahead and run it.
As we write our code, we need to understand how Google sorts and shows us based on the most frequent objects. It's like a game of object frequency! The only thing that occurs twice is "west." I imagine. So, let's just write `Kanye is also the goat` and hit okay. Run it!
**Breaking Down the Output**
And... it worked! The code broke down and showed us every single word. We can see that "Kanye" is two, "is" is one, "west" is two, "houses" is one, "great" is one, "music" is one, "but" is one, and "side" is one (which is also equal to 2). The code output was like a full breakdown of every single word. So fast and so quick!
**Challenges and Exercises**
We've done a lot of challenges and exercises today! If you want to deep dive and master arrays and objects, I recommend that you do just that. We'll have additional exercises for you to complete, and Lance will be your boot camp instructor for those. He's going to help you turn into a black belt in no time.
Lance is going to take you from white belt to blue belt. My job is to take you from... well, I'm not sure yet! But we'll figure it out together. If you want to keep exercising this, you can do that by going into the "Exercises" folder on the left-hand side.
**Additional Resources**
By the time you're watching this tutorial, we'll have a bunch of more exercises ready for you. For now, let's try one of these exercises: convert hours to seconds. You can click the link at the top and watch Lance's video explaining that exercise in detail.
Lance has created a ripple effect, and if you want to be able to do it on your own, you can fork this code and write your solution. Just hit "Fork" at the top of the Loom video, and you'll be able to run your code here. You can even watch Lance's video explaining that exercise in detail.
**Conclusion**
You're doing an excellent job so far! Pat yourself on the back because you're putting in the work and dedication. Coding is a lot of fun, but if you're at this stage, it's mainly for learning purposes. I tip my proverbial hat to you for making it this far with that said.
Let's keep on rocking and rolling! This is a channel where we help you become an incredible developer. I love your beautiful face... as always!
"WEBVTTKind: captionsLanguage: endo arrays and objects ever stress you out are you ever sitting there thinking like what's wrong with me why don't i get it maybe i should have been smarter maybe i should have became a doctor like my dad told me well not anymore things like mapping and filtering and reducing and all kinds of other stuff that might give you a headache i promise after this video that won't be the case anymore in this video you're gonna go from very beginner concepts of arrays and objects all the way to more intermediate and might i even say advanced because there are some higher order functions like mapreduce filter that we're gonna learn about and those are going to be very very very helpful and i will also tell you this a lot of the way that you've been doing things like summing up numbers or whatnot are actually wrong and outdated and you shouldn't be using that anymore javascript now has a more functional way of doing things and so even if you're more intermediate or advanced i bet you'll learn something new and there are mistakes you're making that this video will help you stop making with that said this video will have exercises for you to do for arrays and objects so you can get your skills up smash that like button and with that said let's just get into it if you're wondering who i am i'm rafikaze i'm the founder and ceo of clever programmer and the first thing we're going to do when we start this is i want you to go ahead and open and go to replit.comreplit.com because there you can actually code along with me in javascript and it's a full ide and it's very easy very fast and it's completely free so make sure you just go to replay.com right now and just follow along with me as we code the rest of this in the video that you're gonna be watching is actually a clip of me teaching a javascript course but if you just follow along from complete scratch and you just open up your own replica you should be able to follow along if you want access to that specific javascript course that's also completely free and we'll link it down below and you can go ahead and fork that rupple which means that you just get access to that entire course and all the content and all the materials you could reference it but i want you right now starting from complete scratch just go to replay.com and start from complete scratch with that said let's just jump right into the video all right so now let's jump in and start talking about arrays and right before we do that let's go ahead and comment out every single line that is still running so line 108 for me i'm going to comment out and i'm also going to comment out line 121 and so when i hit run nothing should be coming out to my console perfect my console is looking crispy clean that's exactly what we want so let's go ahead and start working on arrays i'm going to go here i'm going to say arrays okay and why do you need a raise well what if you want to hold multiple different things how are you going to do that so if you have groceries right in a variable you could only have one grocery like a fruit but arrays allow you to have multiple things so for example i could have an array that has a banana it also has um apple okay i can also have orange okay we can also have a pair all right i could have groceries like this and you could write the words too so this is just a string and i decided to put an emoji in it but you could decide to put like the actual word pair here if you want but i just think that this looks prettier right so that's why i'm having it like this now if i'm doing all right and remember to just put a const here okay so we're just going to say cons groceries and here we go we have all of these you know we got a banana we got an apple we got all this awesome stuff in our groceries right now let's console log groceries okay and let's hit save and let's see what happens i'm going to move this here and boom you can actually see that it printed out all of the groceries okay now how do i get just a banana if we if i want right so if you click it it shows you the zeroth element is a banana the first is the apple the second is orange and the third is pear okay and remember everything in coding when you start counting it usually starts from zero so it goes zero one two three and the length of this is four okay because there's four items in here four grocery items now how can we get just a banana if we want just a banana how do we do that well here's what we do we go console.log groceries we put this bracket and we do zero okay and let me hit save and let's see boom we just got the banana okay well what if i want an apple what do i do can you take a guess i'll give you five seconds see if you could write that yourself five four three two one all right let's do it so you would have to put in a one here okay so boom let's do that let's do a one let's hit save and boom you see we got an apple now how would i get not the orange but the pair so how would i get the pair make sure to count correctly zero one two three right how would i get the pair what do i need to put in these brackets to get that pair i'll give you five seconds here five four three two one all right let's do it we would do three okay and you can see that we got the pair okay so again this is zero this is one this is two and this is three okay if we want to get the orange we would simply pop in a two here okay let's give that a try make sure you're coding along with me and you're just exercising with me okay make sure you're not just passively watching this make sure you're coding along with me every single step of the way that's how you're gonna get better so this is called indexing okay this is like you grab one of the indexes okay so here i'm getting grabbing grab the second or like the third index in this case okay um or second index okay grab the second index that's basically what we're doing here or grab the second item if you want to say it in a more human language but in programming this is a zeroth index this is the first second third so if you ever hear like hey it's in the first index or the third index that just means where its position is in an array okay and arrays are really really cool and um i'll show you a lot more about them soon all right so take a look at that right at the bottom you can see that and let me remove let me cons let me comment out this line here okay and let's run it again and i'll move this up over here it's easier to see but take a look at this right over here before we add the cookie and we console log this out notice that there's no cookie in here right but after we do the push and we console log it out watch what happens now we have a cookie that's added that wasn't there before so that's what push does push adds things on to the end of the array okay so try adding something else try adding a chocolate try adding uh a blueberry try adding something and see what happens and see if you could do it yourself okay so add something more here let's you give this a try so i'll give you five seconds five four three two one all right now let's try adding something together so i'm gonna say groceries dot push okay and we're gonna say i'm gonna do let's do blueberries okay and then i'll console log here paste it here hit save and run and check it out we got we got one the regular uh groceries array then we got this with the cookie this time okay and then we push the blueberry and you can see there's a blueberry at the end there so that's how the push method works okay great so let's um comment out all of these for now all the console logs so if you run your code you should see basically nothing okay it should be empty just like this perfect now what we want to do is we want to try grabbing like what if i only wanted the banana okay what if i only wanted the banana and the apple okay those are the only two things or the orange and the pear and that's all i wanted okay well for this there is a method called slice okay so we're going to do that so we're going to says groceries and i'm going to say slice and i'm going to say 0 to 2 and let's see what happens okay and let's make sure we do console wrap this in console.log because otherwise it won't show it to the screen let's hit command enter or ctrl enter and boom look at that we're getting banana and apple so it's starting from zero including zero okay so basically what we're saying is start from zero inclusive and up to two okay but but not including two okay so basically it's giving you the the index this is giving you is either zero or one that's the index it's giving you so it's either giving you this or that okay so if you want to get the first two elements you can do it like this okay up to two if you want to get three elements you do this okay so if i run this it's gonna give me three elements if i do four and run it watch what happens it's gonna give me pretty much all of the elements right if i hit five it's gonna give me the cookie as well that that we added and if i do six it's going to give me all the way up until the blueberries that we've added because we still have the push grocery stop push there boom so you can see the blueberries so when i do zero to six it's gonna go from zero to actually the fifth index and give me uh up to that point cool all right now question for you what if i wanted not from zero but if i want it from right here the pair until the blueberry so what if i wanted this part of the array can you splice that and get that out of the array for me please take a few seconds take five seconds five four three two one and let's work on it together okay so i'm gonna try and i'm gonna do okay let's do up to six one two three four five okay like that this is just an example so let's do console.log and we're gonna do groceries dot slice and i'm gonna say so let's actually count together okay so we have zero one thank you baby appreciate it two three okay so basically we're gonna say give us from three two four five and six but it's gonna go up until five okay perfect so let's go ahead and do that so i'm gonna go zero sorry three two six and let's see if this gives us what we want so i'm gonna hit run and boom it gives us from a pear to a blueberry let's try another example okay keep doing this with me don't get lazy on this keep trying this with me while i take a sip of my water that my girlfriend brought me but try getting me apple to a pair okay try getting me this right over here okay take a second um and see if you can get me those three right here okay so take about five seconds all right five four or take take a little bit longer while i sip this water okay so let's give that a try i'm gonna go ahead and do i'm gonna change this to a one because one will get me the apple and it'll be zero one two three and four one ahead of the one that we need okay and let's go four let's hit run and i get apple to appear exactly like i wanted right it was that that's what i wanted and that's what we got perfect very very good very good okay let's keep making progress now all right so now before we graduate from uh slice and uh if you notice that i changed my shirt i just had to do an interview because they were because my girlfriend is a wow superhero she's candy crush and um they're doing a documentary on her so i had to go do the interview and i had to change my shirt because i wanted something black and my previous shirt was yellow and nice and poppy and so i don't know why i'm telling you that but i thought you should know let's go back all right um so here groceries slice so this is good but before we graduate i'm going to teach you about two different array methods okay so there's a couple of array methods that i want you to know so we went over slice we went over push i want you to learn index of and i want you to learn uh length okay this is probably the most like one of the most common ones length uh so let's just go through it together real quick so now i'm going to say console.log and we'll do groceries okay and i'm going to do index of and what index of does it it will give me the index of these fruits okay so let me just comment out these i comment these slices out here so i'm going to hit run let's just do let's just run that one so we can see all of our groceries or fruits perfect and so index of what it's going to do is if i ask for the index of orange it's going to literally give me what the index of that is so can you tell me what the index of the orange is for example banana is zero apple is one so orange is you guessed it too so let's see what it says all right so let's uncomment these lines and let's go here and we're gonna pop in orange here okay and we're gonna say hey what's the index of this orange let's run it and i just gave back two all right let's ask it about the pair and let's run it and it give back three because this is three okay so that's what index of does helpful to kind of know you won't be using it too often i mean honestly when i'm coding majority of the times i don't use it but i just thought since this is the array section i cover these methods because they're going to be helpful for you all right i'll comment this out for now so index of and then the other method you need to know is dot length okay so that one is going to be important so let's go ahead and do console.log groceries and can you guess what length is going to tell you dot length length starts from 1 and so it will take the overall length of this so however many items are in here is going to give you that back so let's count one two three four five six so length is going to give you back six all right let's give it a try and let's run it boom there we go see that six right there that's it that's that's what uh that's what length does okay perfect so now we can basically say you've graduated from arrays yay good job there's a bunch more to it but this is just an over simplified you know what arrays can do you'll learn a lot more about arrays a lot better once we get to the loops because there's a lot of cool things you could do with arrays they're like my favorite thing in coding i think arrays are literally my favorite thing and next up we're going to start learning about objects okay so let's go ahead and start learning about them now all right now let's talk about objects so here's the first thing we're going to do we're going to comment out line 144 and 135 so there's nothing being printed okay let's go ahead and hit run and look at that we have nothing coming out to the console log that's perfect all right now let's go ahead and let's talk about objects objects are really really important and they're really awesome so in just one second we're gonna work on right when i snap my fingers we're gonna work on a new object all right this is the object we're gonna be working on so as you can see here we have mr leonardo dicaprio he is our he's our object in this case and he has a couple of properties so his properties are he has a name so if you do person.name you know that should we need to set that to leonardo and then his shirt here will be white okay because if you can actually see his shirt is white back there so we need to create an object that actually represents that so let's go ahead and do that now so i'm going to put him over here kind of in the corner so you can see him now let's create an object to represent that so we're going to say const person and object is created by doing these squigglies okay so these squigglies represent an object so so far right here we have created essentially an empty object now what we're going to do is we're going to add some properties to that person object okay so we're going to add a name so the name of this person in this case will be leonardo and shirt will be white this is an object if i format this you see this is how an object looks if you want to make it look a little bit like prettier and kind of how you know pros write prose would probably make it look like that okay kind of how it looks like here so open squiggly close squiggly it doesn't matter if you have it on the same line or you don't okay so it doesn't really matter which way you have it both ways will work all right now this here pretty fairly represents what we have over here you have a person object the name is leonardo shirt is white now let's go ahead and console.log and access some of the properties of this object that we created so we're gonna do person.name and when i console this you're gonna see that the person name and i'm gonna move this up here okay when i when i run this you can see that the person name is indeed leonardo kind of just like what we wanted right in the start and can you guess what person that shirt is gonna give me when i type that in and when i hit run take a guess exactly you're gonna get white okay just like we had expected here because those are the properties and that's how you access them so that's how objects work so first let's learn different ways of accessing this object okay dot notation versus bracket notation these both of these you should know okay so how do you access objects so you're right now you're using a dot notation now here's an example of a bracket notation let's do console.log and we're going to say person and we're going to do name like this and let's run it and boom you also get leonardo right over here well give it a try try doing the shirt but with the bracket notation try accessing it i'll give you five seconds five four three two one all right let's see if you got it so i just triple clicked to copy and then i did command c to copy and then i went down and command v to paste you could do ctrl c or ctrl v to copy and paste and then double click the name inside here change that to shirt hit command s or ctrl s if you're on windows to save and automatically run apparently it's not automatically running so let's run click the button here or command enter and boom you can see that the shirt is coming out white right so we got different ways access an object that's really good now there are different ways to assign things to an object as well so for example i could give him a phone number okay so i could actually do person.phone so notice we don't actually have phone in here and i can do phone and we could give him a number like that okay person.phone and then we're gonna go ahead and do console.log and we'll say person. let's go ahead and try it let's see what happens and boom you can see that we actually get that phone number right there but also you can actually console log the entire person as well so you can see what that looks like so let's go console log person let's go ahead let's hit run and you can see you can see this object right over here name shirt phone so now this object has all of the things and it didn't have that before so it's kind of like how in arrays we were almost doing like push it's kind of like that but like it's just you're creating a new key on the fly and giving it a property another way you could actually create that property is by putting this in bracket notations as well because you could go like this bracket notation and say hey person phone is actually equal to this hit run and watch what happens boom you got the same thing and that also works so both work okay the reason why we're putting the phone number in quotes by the way as in we're making it a string is because if you didn't do that it would try to subtract it will do 1 minus 2 2 2 minus 3 3 3 minus 4 4 4 and evaluate to a big negative number that's not what we want right so put that in quotes because we want the exact phone number and phone numbers or strings all right so now let's say you want to create another person okay so i'll give you an exercise for you to do and let's go ahead and do this right now so i'm going to give you a different person with different name and different shirt and i'm going to need you to create an object a person object that represents that okay so you can call that object person2 all right so let's go ahead and do that here you're going to call this object person 2. all right so now instead of leonardo dicaprio you're going to get somebody better yours truly and you have the object right over here okay so this is the object that you have and these are the properties that this object have okay so try to see if you can make this object by yourself either pause my video or take a couple of seconds right now to do this but i'm gonna start doing this in five four three two one okay let's do it so i'm gonna basically there's a const person two and i'm going to say name is causey right and make sure to put a comma so after every property right so let's go through an object so we have squiggly bases squigglies in the start and closing okay instead of brackets object arrays have brackets and then you have a key and a value all right so objects are key value pairs all right so you have a key here and a value here a key here and a value here okay so a key you can also sometimes call it a property okay so here the key is name and the key is shirt okay and name and shirt is also the property so you could say kind of both ways so you put in the key right person name and you get back the value which is actually the name right you do the same thing here with the phone so you take your object you put in the key right and then that key unlocks and we set this value for that key again we do person.phone so we did the object and the key and then the key gives us the value right and kind of think of it like opening a lock all right so name here so this is the first key value the second key value we're going to set is shirt is black okay and uh that's it that's the way you represent this object right so if we do console log person2 and run it boom you can actually see that it's showing you quasi uh and shirt is black okay that's super cool and uh if i change this to you know what if i want quasi's name what if i only want the name can you try that for me if i only want the name boom hopefully you got this correct okay let's run it and now we're getting the name well what if i want the shirt as well same idea right if i run this we're going to also now get the shirt so shirt color and we got it as black let's turn this into a function that then says hi my name is qazi and my shirt is black okay so let's go ahead and create a function okay so remember how we create functions right there different ways you could do function or you could do the fancy way like this we'll call this function introducer because it introduces you to other people okay and this is gonna take in let's say we can give it a shirt and it will also take in a name so let's go name and shirt okay and if we want to turn this whole thing into oh sorry whoops and we're going to do if you want to do error functions this is how we're going to do them okay so we're going to go ahead and say name comma shirt and we're going to do arrows like this i'm going to go here all right now the final sentence that we're going to return right is going to basically be remember template literals all right so let's use the template literals again and we're gonna say hi my name is name okay and we're just gonna say name like this for now my name is name and the color of my shirt is whatever the color of your shirt is okay so in this case it should be like black so here let's say if we had it hard coded it would be like this but we don't want it hardco um we don't want it hard coded right so we're going to do here is name we're going to make it dynamic we're going to send name and remember and see if you can make this shirt dynamic as well boom we're going to say sure and we're going to save this into a variable called intro so const intro is equal to that uh but we're going to make an object first so just so you can keep practicing an object okay so we're basically going to say we're going to create a cons person object and this object is going to have a key name and the value of that key is also going to be named because that's what we're passing it i'm just going to say name right here and it's also going to have a shirt and that's just going to be the shirt that we pass it okay so these are values that we're giving it these are the actual keys that are hard coded okay so these will stay the same these will be exactly name and exactly shirt and this part will be dynamic these are the values that we give it and so then we're gonna in order to get this person's name remember what we have to do right it's person dot name perfect and now how are we gonna get this shirt we're gonna do person dot what person dot shirt perfect good job if you kept up with that okay so we're creating an object right here and we're creating the intro and now we're just gonna say return the intro all right that's our function and let's give it a try so let's go console.log let's call our function introducer and let's give it a name quasi and let's give it a shirt color and we'll say black and let's run this and look at that right we're getting hi my name is kazi and the color of my shirt is black beautiful so we're using template little i mean this is advanced this is awesome if you're doing this good job for doing this this is using this is a es6 error function right this function has two arguments and you created an object in here and then you're using template literals so there are a lot of concepts in here that you're doing so great job on following along and doing this with me even if you didn't get it it's good that you're you know you just followed along and like did it and hopefully you got it working on your computer like that's what's really important so here i have this and if i if i show you right hi my name is kazi and the color of my shirt is black now let's go ahead and comment out all the other stuff that we had here right so i'm going to comment this out um you can leave this or comment it out i'll just comment it out you can comment out pretty much all of this okay and i'll comment out this person right here too and we'll just hit run and it should just say hi my name is kazi and the color of my shirt is black okay so the only console log that should be happening is the one coming from this function this function is super cool because this function is like creating an object and then doing the template literals accessing data from that object very nice okay this is a very very nice uh well-written function so good job if you got here so far now try see if you can now do this call the same function but see if you can call it on leonardo and with the shirt being white so give that a try i'll give you five seconds five four three two one all right let's do it together so i'm gonna say console.log we're gonna say introducer and we're gonna see leonardo here right and the shirt color is going to be white these arguments have to match right so this matches that and then this matches that okay so these arguments the order here matters okay and uh let's run this and let's see if it does the leonardo one as well and it does hi my name is leonardo and the color of my shirt is shirt okay so we probably gotta say white here hi my name is leonardo and the color of my shirt is white beautiful this is doing exactly what we want we're going to go ahead and comment this out and so far you're doing absolutely fantastic all right now let's take this even a step further let's say that we want to have this person but we also want to calculate and have their net worth okay so i'm going to go ahead and refresh this page and let's just uncomment the quasi in black here perfect my name is kazzy and the color my shirt is black blah blah perfect we want to say uh this person has certain assets okay so assets they have is like let's say a hundred thousand dollars all right and the debt they have is fifty thousand dollars so if you want to calculate the net worth it's really easy it's just assets minus debt or assets minus you know you could say let's say liability all right so it's how much basically money do you have and then how much debt do you have it's very simple for an average person so if you want to calculate your net worth you could do that right now like it's very easy you could have a negative net worth or a zero net worth and if you have a positive net worth that's incredible so here you would have a net worth of fifty thousand right hundred thousand minus fifty thousand we could do that right and so we could say and my net worth is and i could do this and i could basically say uh person.assets minus person dot liability liabilities and paste that there okay so let's run this and let's see what we get hi my name is kazi and the color of my shirt is black and my net worth is fifty thousand dollars we could put a dollar sign here right and we could say we could say we you don't need to say usd but you could if you want and it says my name is blah blah my net worth is 50 000 usd so this dollar sign is uh pretty much to actually put a dollar sign and then the next dollar sign and squigglies are to basically say hey i'm writing javascript let's make this even nicer i'm going to teach you how to create methods inside of an object okay and anytime you've seen dot met something that's usually a method okay when you're calling it so like math.floor remember when you saw math.floor math.floor this is a method if you didn't have this math part here and it was just like this this would be a function and if this open close parenthesis weren't here this would be a property so i know it can be confusing but here's a great example all right you have an object right here properties are just with dot to car.name car.model those are all properties right but methods are car.start with the open close print card.drive open close print car.break open close paren so methods look like this all right so just kind of take a mental screenshot of this for now all right so here's what we're gonna do here's a really cool thing and kind of advanced thing that you could do with objects so i can actually create a net worth field and what this will do is what i essentially want to do is i'm going to say hey assets minus liabilities but if i do this i'm going to run into a problem okay so let's try that so let's remove all of this and let's paste just netperson.net worth and let's see what happens if we run this and it says assets that's not defined and you're probably thinking wait access is defined right here so why isn't it just using assets this is that's not how it works so in when you're defining an object because all of this is happening at the same exact time this isn't like created yet and so it doesn't know so what you want to be able to do is you want to be able to say person dot assets but it also won't really know that so here's the hack here's a way that we do this okay so we're going to create a function and yes you can actually store functions inside of objects which is kind of crazy open close paren all right we're just going to do this this is going to be your net worth function what it's going to do is it's going to return and we're going to say this dot assets minus this dot liabilities this thing is kind of weird but what this is referring to is this specific person object that you've created so it's literally like imagine plugging in person.assets and person.liabilities okay but you're going to use this this whole thing here is going to become 100 000 and this entire thing is going to evaluate to 50 000 and then this thing is going to evaluate to 50 000 like this and then once you return this it's going to pop up in your console so let's go back boom this assets sold liabilities and net worth is a method so even though we've defined as a function it is a method and i'll show you why because now in order to call it if you do person not net worth watch what's going to happen it's not going to work let's run it and we just got hi my name is kazi and the color of my shirt is black and my net worth is function return assets so it just like just like spit out this entire function that's not what we want we want the actual result so we need to call this method and this is how you call it okay boom you call it just like a function but because it has a dot here it's called a method so the person has a net worth method now let's run it and watch what happens boom right my name is causing color my shirt is black and my net worth is 50 000 and now to make this code even more dynamic assets and liabilities should be passed by the user okay so that could be like another thing that that might be nice right now it's hardcoded but that's okay we can just kind of leave it as is just to kind of show you that you could do some more advanced stuff with objects all right so let's run this okay and if you change the name it'll also work change the assets liabilities you'll get different numbers all right so if you got this far great job uh we're gonna hit run on this to just make sure we we're gucci but if you got this far you're almost an object master so good job okay you probably don't know like everything there is to know about objects okay but like you know more than 98 of the population so you should be proud of yourself give yourself a pat on the back right about now go ahead and do that for me and and you're beautiful and you matter and i'm happy that you're here so thank you warms my heart all right with that said let's continue all right now let's keep having more fun with the arrays because the more practice you get with arrays and loops the better of a programmer you're going to become because honestly that's like 90 of what you're doing in programming you're usually getting max a lot of data and you're looping through it and you're doing something to it okay that's like super common and so let's just get a bunch of different practices in all right so we're gonna go ahead and make another function and uh this function we're gonna make is going to be called letter counter okay so let's go letter counter okay you can also call it counter whatever you want and we're going to use it making an error function all right and basically what this function is going to do is it's going to take a look and see how many letters are in a given phrase okay so if i gave it a phrase let's say khan's phrase is equal to hey can you go to grocery store okay whatever all right this will go through this whole thing and it will basically just just count how many letters there are okay let's call it how many letters and so you're just gonna go through and this is gonna be a one there's gonna be a two this is gonna be a three a four a five for the empty space six seven eight nine so you can do this you can count an empty space as a letter that's totally fine so think about how you're gonna write a function like this right think about what are you gonna need to do well you're gonna need to loop through this and the cool part is you can actually loop through these types of phrases you can loop through strings as well first let's comment out um anything we kind of have up here okay perfect and now in here i'm gonna go ahead and i'm gonna say let's let's write a loop okay i'm gonna say for letter in phrase and let's see if this works and let's do console log and we'll say letter okay and let's hit run and let's see what happens okay it's running okay so what we need to do in order to make sure that this actually runs is we need to call this function that we just made right so let's go right here and i'm going to say console.log and we're going to say how many letters okay and let's just call that function and there we go okay one two three four five and then it ends with um undefined okay all good so this keeps looping through and then it just prints out the letter that's what we're looking for right and we actually don't even need to call console.log here because we're printing out from the function so let's try running this and now you see there's no undefined right and we we go from 0 to 40. but it needs to start from a 1. and if you think you're getting this or you know how to kind of solve this or if you even want to try i highly recommend you just pause my video right here and actually give this a try okay go ahead and try to like do this on your own and then you can come back to this but i want you to get a ton of practice with this stuff because that's how you're gonna get better not just by watching me but by doing it with me that's how you're going to get better so if we want to count the the letters in here right or this this how many are in there we can't start counting from zero okay so what i'm going to do is we can just make it where if i'm getting uh a letter here right oh and if you were a little bit confused like wait how are we when we run this how are we getting the numbers with this four loop right so you can also say for cons letter and if you change this to of because remember i taught you like i taught you four of loops earlier so if you do letter of phrase you're actually gonna and you run this you're gonna get letters the actual letters all right if you run um letter in phrase it's gonna get you the index at each one of those right so if you wanna get 41 well what we want to do is uh just do letter plus one okay so this will just increase it by one whatever it is and if and for whatever reason this letter thinks it's a string so it was doing that weird thing where it's attaching you know zero plus one is zero one and one plus one is one one it's attaching itself so it thinks letter is a string so just type cast it into a number so force it to be a number and then you get this okay so this is like one very silly way of doing this and you get oh i get 41 at the end so that's it that's how i'm getting my this is how i'm counting my letters but there are better ways i'm going to show you in just a second all right so now let's talk about better ways of doing this yeah so what we want to do is we want to have we want to set a variable here that let's say is so we're going to say let uh result equals zero okay let's go here and we're going to go we're basically going to say result is equal to number and we're going to call index and we're going to say plus 1. because all we're doing here is the loop already allows us to get up up to 41 right so we know that this is the correct answer all we want to do is store that correct 41 in result and then at the end of this loop we want to just return that result at the end that's all we're looking to do okay so because okay so i'll give you an example okay let's just do result is index and here let's just return result let's remove this line well we can keep that line so you can just see how the loop looks and we're gonna run this function but wrap it in console log all right actually we can do a trick here to make this look really nice so here we have return result right in order to make it we want to make it look like this result is 40. so there's a nice trick we can do in our return and we can basically say we can return it as an object so when you print it out the object is going to look really nice like this right and then there's a even nicer shortcut where if you just add in one thing and wrap it up in an object it's going to automatically set this as the key and use this as a variable for value so it's going to set the word result as the key and use the value 44 from this so it's a super cool little hack and you can use this pretty often so let's run this and boom you can see that result in this case is 40. so now you can see that result is returning pretty much the last value in this entire thing that we get right when we print out our uh indices so these are this is you know this is like basically saying this is zero this is one this is two but we just want to start it off with the one so um let's go ahead and all we need to do here to index is we just need to add a one to it and also remember we need to wrap the index in a string just like this oh sorry not in a string i'm sorry wrap index in a number because it is a string okay so we need to typecast it let's run this and boom you get result is 41. right so now phrase we can change the phrase right and again how many letters is a function that takes in the phrase so let's make this let's extract this argument out now and instead we'll put phrase over here and we'll pass it in the phrase okay so the function itself shouldn't have to worry about the phrase it should just wait for what the user is going to give it as a phrase and it says result is 41. now imagine i change this and i go prompt the user for you know write your phrase okay and now let's run this and now this pop-up happened at the top so i'm going to write my phrase i am wearing a nice green shirt right so when we do that let's go ahead and run this and i hit okay and let's see what happens here let's hit okay and right when i hit okay boom we got 31. all right so that that phrase has 31 characters now here's something sad i'm gonna tell you you can actually replace there is a method for strings that allows you to calculate the total of uh you know see the length of a string and that is just dot length okay so for example if i well we don't open up a shell let's just stay here so say i have a string here okay let's zoom in so you can see that better all right so say i have a string here that is banana if i just call dot length on this and run this you notice it'll give me a six okay and uh let's call let's say banana wants to know your slogan dot length it'll give me back 32 so a dot length is a property on anything that's of type string you can call anything that's a string you can call dot length on it okay and that returns the final answer so let me zoom out here which means which means i could actually remove my entire code right and basically just say the result is phrase.length change this whole thing with one line of code hit run write your phrase it's going to say something crazy and let's see if it can calculate it fast and it did and it goes result is 107 okay but let's write it in our way because remember that's the way you learn how to do it and right now it's important for you to manually learn to do these things because i want you to develop and focus on your skill set then once you get better normally what you would do is you know javascript string um length you will just like google this okay and see if that exists and you'll google this and you'll go somewhere and it will say that let's see w3schools is usually a really good resource and it's just showing you text make a text let it equal to hello world let length be text.length and it also allows you to try it yourself so you can always click that and you could hit run right and boom it says 12 here and what if i change this to something random and let's hit run and you can see it says 38 right over there yeah so uh this is how you would normally do it in real life you just google these things you have to memorize anything okay but right now i'm just kind of getting used to having you get used to exercises do them yourself okay now let's build it up and take it up a notch okay so let's let's uh make a function that sums things okay that's also going to require loops and arrays so i'm going to go ahead and we're going to comment this out and we're going to comment this out so we're going to go const uh sum array it's going to take in some numbers and we're going to have a result we're going to say let result equals zero okay and then we're going to have some kind of for loop action going on here and then we're gonna return the result and let's just do this trick like that we did last time and something is gonna go in the for loop okay something is gonna go inside of the for loop oh sorry we forgot to put the arrow functions not bad arrows there we go and what's going to go inside of this for loop is basically going to be imagine this right imagine how we would do this manually so say we have uh these numbers or let's say we have these numbers okay this we know that this should if we ever have 1 2 3 4 we know that this should equal to 10. now how are we going to actually sum this up in normal life what would our pattern be well i probably have some variable let's just call it result and i would set it to zero in the start or i might set it to whatever this number is right over here okay so you could you could do either or but let's just start it with being a zero okay so it starts off being a zero then what we do is we loop through and we make the result becomes one then we loop through again and we do one plus two and then result becomes two oh sorry three then we loop through again and we already have this three here so we add three to this three right here so we add this to that and we get result is equal to six and then we add four to that and we get ten so that's how we would do it kind of as a human being if we were to do it like manually and do it with each step and that's how we're gonna do it with our computer we're just gonna have it loop through and keep adding these numbers okay so let's go ahead and now write that loop so again try to give this a try and see if you can do this yourself okay because what you've learned so far you should be able to come very close to solving this so take five seconds to do it and also pause your pause my video in these five seconds to try it yourself so five four three two one all right let's get back to it so here we go i'm gonna say for const number of numbers all right and let's just call console log number and uh let's call our function console sum array okay and let's give it an array so we're going to have we're going to give it this array of no it's going to be called nums okay let's remove let's comment out all of this because we don't need that and let's pass our summary nums and let's just run this okay so we get back one two three four because it's looping through and printing number out each time so if i give it an array of one two three four it goes okay number is one the first time print it out number is two the second time print it out numbers to read the third time print it out so on and so forth all right so it's printing this out result is still saying zero so now what we need to do is we need to increment our result so let's go ahead and do that now okay so again try to give this a try and now we're going to do this so all i'm going to say is i'm going to say result and i'm going to say result plus and we're going to do whatever is the new number that i'm getting okay and let's just see if this works and i'm going to hit run and it says result is 10. interesting did that really work well let's try it with and let's add 5 to this array and let's see if that works and i'm going to hit run and we should get 15 and boom result is 15. beautiful okay so let's see um how this is happening what is going on all right so let's do something to visualize this code and see exactly how this works i'm going to copy this let's go over to pythontutor.com all right and then hit enter once you're in pythontutor.com you're going to go to related services where it says javascript tutor okay and click that this website is awesome so paste your code and uh just for simplicity sake i'm going to remove any console log because we don't really need that it's going to help us visualize so there's going to be no need for that and we don't need any comments either it's unnecessary for you to remove it but i'm just going to remove that for simple simplicity sake then hit this big button here that says visualize execution so let's click it okay perfect all right now on the left hand side is going to be our code on the right hand side is going to be visualizing exactly how this code is running very important to notice also this red arrow means the next line to execute so which is the next line of code it's gonna execute okay so it's already telling us that this is gonna be when i hit run or when i hit next to step through this this is the line of code that the computer is going to run and we're just going to go through step by step and see exactly which line runs exactly when and green arrow will mean the line that was just previously executed cool so this is our function let's walk through this all right so let's go ahead and hit next okay so now it sees we have a function called sum array and boom it's like okay i got you there it is there it is i see it let's hit next again now it's on const uh const nums as you can see right here right so this is the line that's about to execute next so let's hit next and it goes okay i see that there is a variable called nums and it's inside of this array okay or it's this array right here it refers to this array and this is the global frame because this is uh these are all variables that are outside of this function so these are accessible from anywhere and from everywhere okay and we'll talk more about scope in the upcoming videos now next line is going to execute is it's going to call this function summary with the argument nums okay and then our entire function is going to start running so let's hit next cool so now we're inside of our function and the first line of our function is just about to run so the first line is going to be what result is cool so let's uh right now result is undefined okay but once this line runs here result is going to have a value so let's hit next and boom result is now set to zero now okay so this green is just showing you the last line that ran now the red is showing you that we're about to start running our for loop and we're just gonna be teeter-tottering in between this line the for loop line line four and line five and line four and line five and line 4 and line 5. until the entire loop is done then we're gonna go to this line here and return the result at the end okay which and again remember uh you know we're going to touch base on scope but if you put this if you put things in the wrong scope wrong things will happen for example if you put this result inside of this these squigglies here right this is just going to keep returning every single result but that's not what we want we want to return the results after this entire for loop is done right and then we want to return this result okay so let's run through this and see what happens okay so right now it doesn't know what number is either okay but it does know it results as zero and it also knows that numbers refers to this array right here okay let's hit next okay boom boom boom all right there we go and now it says number is one perfect that's exactly what we wanted right number is one because the first time we're going through the array right it's a one that's why that's where the number is coming from now let's see what it's going to do the result so watch what's going to happen it's going to do results going to be 1 this time so it's going to do 1 plus or sorry result is zero right now so i'm going to do zero plus the number one okay let's try that and now you can see result is one let's do it again now number is two so two plus one result is going to be three okay three plus three result is going to be six six plus four result is going to be 10. okay and notice how it was just like boom boom boom boom it's just these lines running over and over again right and now the result is 15 now the loop ends because there are no more numbers to loop through and we go to result result is 15 and then we return the result at the end return value right and the return value here in this case is this object it was of type object and it says result is equal to 15. so there you have it a full visualization of a for loop exactly how it works and what is happening and hopefully that clarifies things for you a lot further so i want you to try something try writing this function some array by yourself from scratch and i know we just did the answer but i want you to give it a try without referring to this and see if you could create this function by yourself and if you could that would be fantastic and just give it give it give it a shot right and if you could get this working see you know that would be absolutely amazing and then check back with this and see how far you got all right let's do some more exercises with loops arrays objects whatever right because this is going to make you so much better so let's make a function called max supposed to const let's say find max okay or you could call it max as well and it's going to take in numbers and it's going to have um let's say uh cons result and then it's gonna return the result okay and then we're gonna have something some loop stuff happen here okay some loop stuff and here's how the algorithm is gonna work okay imagine we have an array of you know let's just say one two three and four and five okay imagine we have this array right here and let's just put commas here because that's how the array would look have these commas and what we're gonna do is we're initially gonna set result to one and then we're gonna loop through and we're gonna say hey it's two greater than one check these if it is then result set it to two okay then loop again all right is three greater than two if it is then set result to this then check between these two numbers if four is greater set four if five is greater set five and then once the loop is done it's going to return the biggest number okay and now imagine if you know in in let's say there was a case where we had a one here okay let's see if it would work if that same algorithm would work there okay set one to the biggest set one to result okay great now check between these twos two greater yes then make two the result okay then check between these is three greater okay make three the result check between these is four greater okay make four the result check between these is one greater no then don't make one the result keep for the result right and and then that's how we're we end up with four so that's kind of the algorithm that we would do okay now we just have to code it up so we're basically gonna say set result not to zero okay because imagine what if you had an array with only negative numbers in it well then zero would be the biggest number so that's a common trap do not set result to zero we're gonna instead say results is the first item of this array okay we're assuming we're getting back an array or we're getting an array here okay and then we're gonna say is loop so we're gonna say we're gonna write a four of loop const number of numbers and basically we're going to say if number is greater than result in that case what do we want to do we want to set result to that number that we just found and at the end when we're done with this loop we just simply return the result okay so let's go ahead and give this a try and let's comment out anything else that we might have running okay we don't have anything else running so we're oh we do so let's comment out these lines right here and let's just go console.log and i'm going to say max and let's give it numbers like one two three four five and let's see what it returns as the max aha yeah so this is not a const let's make this a let because we are going to be changing result okay so this error is saying assignment to constant variable so all it's saying is hey you cannot redefine a constant so that's why i'm changing it to a let now you can redefine it right here and we should be good and let's do that trick again let's return result as an object so it looks nice when we print it and you can see result is five now imagine we have one comma two one two so the max in this array is what it is a it's the four so let's see if it gives us a four yes it does beautiful look at that let's go 20 now and 10. and let's see if that works beautiful and now let's make this array now let's make this massive right like let's just um yeah let's let's take this right here and let's just paste a couple of times and run it and look at that it says result is 20 and if i make this a 3 over here and run it boom result is 30 i mean it will find it wherever the max is it will find that max right so this is a really nice function that we have created now let's keep building on this okay so this is all going to be making sense and trust me it's going to be so important for you once you start getting to building projects and doing those types of exercises you're going to wish that you spent the time to learn this because this is going to be a complete game changer for you all right so our max function is done let's work on a different function now and what this function will do is it will calculate frequency okay so let's go ahead and try that so we're going to say const frequency okay it's going to take in uh let's say a phrase that somebody will say and it'll tell you how many times uh that phrase or that or sorry that that letter occurs okay so let's just say letter frequency okay so what does that mean so say i said haha here's what it should return okay so if i call the function letter frequency on the word haha here's what it should return it should return an object and the object should say h occurs twice and a occurs twice that's what it should return so how do we create this function so it does that if you want to give it a try you can this will be hard because you'll need to use your object knowledge as well that we put together a while ago but this will be using you know summing up numbers looping through things iterating and all kinds of stuff like that and so uh incrementing variables it's going to be a tough one if you try to solve it yourself but you should give this a try right about now and we'll continue in five seconds five four three two one let's continue so i have letter frequency how do i determine this okay let's figure it out together first let's just go ahead and call this function so we're just going to say letter frequency and we're going to give it that phrase haha like this and uh let's console log that phrase let's delete that line and let's just make sure that this letter frequency is running okay haha is showing up perfect now what do we want to do we're we're getting haha well we need to actually start writing a for loop that will start going through all of those letters because that's going to be important okay so let's go ahead and write a for loop i'm going to say four letter const letter of phrase and let's just go console.log and we're just going to console.log each letter and let's see what we get we get h a h a okay great now this is where objects are going to start becoming very very handy because you know key value pairs are going to be absolutely clutch for this arrays are not a good solution for this okay but like objects are objects are also great for storing you know information in your phone book like on my iphone when i'm going through that is being stored in an object okay that's why i have you know my name my you know i could look up somebody by their name their phone number etc and objects are great for that so we're gonna go and look at something in the console okay and that's going to make our lives a lot easier so say we have something called word and this word has let's actually give it let's actually call it frequency right or freak um and let's say this frequency has that the h is once and that's it okay so it has one h in there now if we have a word like haha or whatever right or if we want to just check yo is h inside of the frequency we're actually able to check that and it goes yes h actually exists because it can if you do h and frequency what it does is it looks for h as a key ins and searches all the keys inside of an object okay so it goes yes h exists and uh if i go you know because of the word haha does a exist in frequency and when i look it says no that a does not exist that's actually false so using this we're actually able to see if we've already stored something in our object or not because the way that we want to do this is we want to go and say okay so say we have the word haha then i want to have a object i'll just write it out here and it should loop through and in the first loop it should notice that there's a letter h so as soon as i notice the letter h i make a key for h all right and because i know it showed up once i set it to one so i go that key is equal and it has a value of one now then i loop through that other word again and this time i see that we have a okay perfect well have i seen a before no i haven't so then i set a to one and so far so good okay then we loop through the third time and i see h again well i've already seen this before so what do i do if i've seen this before am i gonna go ahead and make another h and then set that to one you can't do that because in objects you can't have the same key repeating twice so we have this key here this key here so what will happen instead is we will end up overwriting this key and just do h is one and that's what it was in the first place but that's not the result we want we actually want to we actually want to increment this key right here this h1 right so we want to increment this one by doing plus one and making it a two so how do we do that well what we do is we go hey does h already exist and we do this little checker here right there and if it says yes h already exists then we go go to h and and and add one to its value so it goes okay i'm gonna go ahead and i'm gonna do that so we go to this and this becomes a two right and then we go to the fourth letter in haha and we do the same thing and we go hey does a actually exist in frequency and in this case it's supposed to be a true and it goes yes a does exist and so if that's true we then go okay find the existing a find its value and increment its value by one and then we get two oh okay so that's how that works that's how that works so let's go ahead and basically say that algorithm but in code this time so first we're gonna make a frequency make frequency object and that's gonna store all the memory slash all the data of what it has seen so far okay so this is so make a frequency object okay it's going to be called frequency kind of like we did here okay but in the start it's just going to be empty it's going to be an empty object then we obviously loop through all the letters of the phrase and we check if that letter that we found in phrase does that already exist in frequency so check if letter exists in frequency so you make sure to you know we're kind of writing pseudo code right now because we're just writing it kind of like code but in english and then later you just turn it into code and then you're done so if we check if the letter exists in frequency okay what next well if it exists remember if it exists we're gonna increment by one otherwise we set it to 1. so let's write that check if letter exists in frequency if it exists so this is going to be indented because this is going to be nested inside of this if condition and we're going to say if it exists well then increment the value by one otherwise set it set the value to one okay that's essentially all we're doing and so now take take some time to see if you can actually write this as code and then obviously at the end we're just going to return that frequency that we created okay so we're going to return that frequency so const or let's go let frequency equal an empty object so see if you could code up the rest of this okay take maybe a minute or five minutes or whatever and even if it takes you 20 minutes it's totally fine because this is normal okay you're not supposed to get it this fast this is supposed to take you a lot of time but take some time i'll give you five seconds to pause the video and try it on your own five four three two one all right let's do it together now check if the letter exists i'm going to say how do i check well i just checked by doing letter in frequency okay does that letter exist in frequency this will evaluate to true or if false that's it i'm going to say if letter is in frequency then do the following okay then we're going to say increment the value by plus one so how do we do that let's do that right now we're just going to say frequency of that letter so we're creating a new key on the fly and we're going to say equal to whatever it was before plus one okay so it's like whatever frequency of the letter previously was just like incremented by one okay if it doesn't exist which is the only other scenario then what we can actually say is we can go and say hey set the frequency of that letter to one that's it and then we just return that frequency okay so let's try it on this word haha and see what we get and look at that we get h is 2 and a is 2. that's crazy right like we just coded that out from scratch and it works so nicely and let's try um lol what are you doing later tonight lol haha okay let's run this and it just runs through every single letter right here and it says l happens about five times is that true let's see l so one two and then we got three four five yep so that looks like it happens five times and it says how many times o happens and it says how many times a comma happens how many spaces there are and it just shows you the whole freaking thing right so frequency is working as expected now one thing i'm going to show you is called incr these are incremental operators okay at least that's what i call them and they are plus plus minus minus plus equals okay um plus equals one and so these are incremental operators they're really really helpful and make everything shorter so for example this over here you won't ever see it written like this if somebody's coding this up they're gonna write this like this okay so it's gonna say plus equal one so plus equal one is the same thing as frequency letter is equal to frequency letter plus one so these two are equal and i just want you to kind of get used to seeing them okay and we can go and change some of our stuff above as well so like where it says result equals result plus number right how could we change this here try it on your own take about five seconds and this you should be able to do in five seconds five four three two one you could change this with a plus equal all right incrementing it like that okay and if you're incrementing something by one here's the cool thing you could also just do frequency plus plus and that just this just means that you're incrementing frequency by one so let me try hitting run you can see that this works right you can hit one run on this and you can see that this also works and gets the same result and we can you know or you can go back to the oldest one and see we get the same results so it's a matter of preference you never want to write it the long way you won't ever see it written the long way and you could write it in between these two ways so you could go plus equals one or you could go plus plus either work okay so i just thought you should know those you could also go minus minus and that will like decrement it okay and um you know so i'll just leave this as plus equals one for you just so you can wrap your mind around this and get used to seeing it okay so these are incremental operators they're very very useful and very commonly used now i'm going to remove all these comments or i can just leave them here if because you know this might this might kind of hurt your brain a little bit how's this frequency thing working so i'm going to leave that here for you all right now let's just keep building on this and let's keep taking it a step further so now instead of letter frequency let's improve our our skills and and do something called word frequency okay so we're going to build a new one i'm going to say const word frequency and see if you can try to challenge yourself to take a stab at this phrase and then we're gonna run this function okay and we're gonna do the similar similar type of approach we're going to create frequency here as an empty object and we're going to return frequency at the end all right um and inside of here we're going to do our like coding okay this is where we're going to be doing our logic now here's the one interesting part here i'm going to comment this out obviously let's comment that out let's go ahead and console.log word frequency to make sure it's running and let's give it a phrase the phrase is lol what is going on lol what law let's just say that okay lol what lol so we know that if we run that here's what we should get so if i run this function so let's have an example and this is also really good for documentation because by looking at this people can know how to call your function and then what your function actually returns at the end so i'm going to say word frequency and if i call this function with this phrase law what law it will return the following it'll say lol occurs twice and what occurs one time okay just like that and um this is what it should return so how do we make it so it does that well i wanna i have to teach you a couple of things for this so say i have a phrase and let's go phrase is equal to lol what lol so what we would really love for to happen is imagine we had an array and it had a it had things like this that'd be really nice because if we loop through something like that right in an array it would just loop through the words and we want to loop through the words instead of letters that's the only difference here between last exercise and this exercise okay so how then the question is how do we get it into an array so we can loop through it because if you try to loop through it in a string watch what's gonna happen okay so if you try to loop through in a string it's gonna loop through and print out every single letter from here so let's try to write a loop and loop through this i'm going to say 4 letter in phrase console.log letter okay and we're gonna hit enter we're gonna close the parentheses and run this right and it's just gonna oh sorry not not in let's run that loop again and say of of and it's just going to print out every single letter well that's going to be confusing because it's going to count letters for us and instead what we want to do is we want to count words entire words right and words in this case are separated by what what's a common thing all of these words are separated by take i'll give you a hint they're separated by these spaces okay so we can do something cool in javascript we can turn this into an array um and if we turn this into an array so let's say i give you phrase and if i call dot this method got split on it it's a string method and i say split it by spaces and i hit enter watch it what it's going to do it's going to turn that into an array and now that array very clearly has three items and if i loop through through these items watch what's going to happen so i'm just going to call it array okay and then let's just loop through this f4 word and array console.log word i'm sorry i'm just used to python so it's for of of of of so for word of array yeah law what law so look at that i'm actually looping through the words in that array and i can now in my object see if this word exists and if it does exist then i can increment it by one and if it doesn't exist then i can just set it to one so it's the same exact thing but just with words this time and all you have to do is turn it into an array of words and then you're good so let's give it a try now what we know so we're gonna once we get our phrase okay what's the first thing we're gonna do to it what's the first thing we're gonna do to that phrase to turn it into an array we're gonna do phrase dot split yeah this is going to turn it into an array but we need it to be separated by what it's separated by spaces in this case okay so boom this is going to give us what we need so now let's let's go ahead and um i'll do let's just call this words okay and um now let's console.log these words and hit run and watch what happens now i hit run and watch i got lol what lol okay if i have hello here and i run it lol hello yeah so now it's doing what we wanted to do now the algorithm is going to be pretty much the same as letter frequency very very similar to that so we're gonna basically use like a similar similar formula okay that we did here so we're gonna say we're already creating that frequency object there yeah and then we are returning it that's good so we're doing that here too we're creating it and we're returning it so that's good and we're gonna do the same thing so we're gonna say for const word of words okay let's just console log the words for now and let's just see what it's console logging so let's run it and you can see that it goes lol perfect every word and we're going to do the same thing check if it exists in the frequency if word and frequency then we're going to say we're going to increment it by 1. so we're going to go frequency word plus equals one else frequency word set it to one and at the end return frequency so let's try this and boom look at that lol is twice what is one time and it's already working right that's crazy so let's go yo yo yo and let's run it and here we're gonna see that yo is coming out three times lol is coming out two times and one is what is coming out one time so i mean this is so cool we just made it but there's something we can do to make it even better i want you to notice the similarities between this and the letter frequency thing that we wrote yeah and instead of calling it letter frequency let's just call it i mean actually let's leave it as letter frequency that's fine but they're very similar right the code here is very very similar to the code we have here for example we're creating a frequency object we're creating a frequency object we're returning frequency we're returning frequency right we're we have a loop that we go through we have a loop that we go through if it's detected in the frequency incremented by one if it's detected in frequency incremented by one uh if it's not detected set it to one if it's not detected set it to one i mean my gosh there must be something we could do because it looks pretty much like the same exact thing and watch there's something brilliant that we can do so we can actually delete this entire for loop let's get rid of this console log we can delete this let's get rid of this return here let's get rid of this frequency here and the only new thing we need in this is we just need to split it by spaces to break it into words right and let's go const words sorry i keep forgetting the cons sometimes and all we do here okay this is going to be so beautiful is we call our letter frequency function that we created earlier and we just pass it words and then ill know what to do with it so it goes in here and then it has instructions for what we need to do right and all we need to do here is just say return so let's try running this and look at that you guys we are getting the same exact answer and it is completely correct yo is three times what is once lol is two times okay now this is if this is not blowing your mind think about it like this when you go to google right and you type in kanye west and it starts auto completing it's using the same thing because it knows what are the things that are being searched frequently and because it knows their frequency it's showing them here so for example maybe this search over here right this kanye west runway that might be done let's say 10 times kanye west net worth might be done five times kanye west's girlfriend might be searched three times okay it's it's just an object and then google is sorting it and showing you based on the ones that were the most frequent that's the really cool part and this is why you know once you learn this here all you need to do is be able to attach a front end like this and boom your apps are going to be absolutely amazing and mind-blowing so let's try this now okay let's try our own function with different inputs we're going to say const user input is prompt uh write your sentence right and we're going to replace this with whatever the user types in so we're going to say user input here and call this and yeah let's go ahead and run it write your sentence and we're just going to say um kanye west has great music but west side is greater than greater than east side yeah because i live on the west coast in california and then you got all these people in new york and i like that my time gets back i get my three hours back when i come to california instead of losing my three hours when i go to new york so if you're in new york and you want to earn three hours back of your life forever come to california so let's try this yeah and um probably the only thing that occurs twice is west i imagine okay and then let's just write kanye is also the goat the goat that's not gonna work the goat okay let's set the goat and let's hit okay and run it and uh it came back all right and it broke it down and you can see it did a full breakdown of every single word kanye is two west is two houses one great is one music is one but is one side is one is two and the code is two thus two and it did it all so fast and so quick all right so we did a lot of challenges and exercises and if you wanna deep dive and do like a full on boot camp and master arrays and objects then i recommend you deep dive into exercises okay where we'll have additional exercises for you and lance he is going to be your boot camp instructor for those okay so if you want to become a like a legend he's going to help you turn into a black belt my job is to take you from white belt turning into a blue belt he will do that so if you want to for example you know because i'm going to move on to different topics now but if you want to just keep exercising this you can do that you can do that by going into say this exercises folder on the left hand side by the time you're watching this tutorial we'll have bunch of more exercises right now we have this one here that says convert convert hours to seconds you click that okay and then lance has this exercise prepared for you there's a rebel that you'll have to copy okay and paste in your urls and you'll be able to fork this and be able to code it on your own so this is the ripple that lance has created and if you want to be able to do it on your own hit fork at the top okay and then you can actually run this uh repl okay and uh you can write your code here and you could write your solution here okay and what lance will also be doing with some of these exercises he'll also have a link at the top of the loom video where you'll be able to click and watch his video explaining that exercise explaining that project in detail as well okay so think of him as a solid um you know boot camp instructor that just like kind of dives deep in with you all right so that those are going to be the extra resources available to you with that said with that said you're doing amazing pat yourself on the back i mean wow you're doing an excellent job so far by being here you're putting in the work you're putting in the effort i mean you could be doing so many things you could be playing with your kids right now you could be outside hang out with your friends you know you probably got off of work and you're putting in that time to learn this i mean that's huge dedication you're taking time away from all this other stuff you could be doing and doing this and now granted coding is a lot of fun but if you're in this stage i know you're doing it for learning and i tip my proverbial hat to you um huge props for making it this far with that said let's keep on rocking and rolling that was a lot i hope you enjoyed watching that video and learning more about javascript smash the like button if you haven't already make sure to subscribe to the channel because this is a channel where we help you become an incredible developer i love your beautiful face and as always i'll see you in the next videodo arrays and objects ever stress you out are you ever sitting there thinking like what's wrong with me why don't i get it maybe i should have been smarter maybe i should have became a doctor like my dad told me well not anymore things like mapping and filtering and reducing and all kinds of other stuff that might give you a headache i promise after this video that won't be the case anymore in this video you're gonna go from very beginner concepts of arrays and objects all the way to more intermediate and might i even say advanced because there are some higher order functions like mapreduce filter that we're gonna learn about and those are going to be very very very helpful and i will also tell you this a lot of the way that you've been doing things like summing up numbers or whatnot are actually wrong and outdated and you shouldn't be using that anymore javascript now has a more functional way of doing things and so even if you're more intermediate or advanced i bet you'll learn something new and there are mistakes you're making that this video will help you stop making with that said this video will have exercises for you to do for arrays and objects so you can get your skills up smash that like button and with that said let's just get into it if you're wondering who i am i'm rafikaze i'm the founder and ceo of clever programmer and the first thing we're going to do when we start this is i want you to go ahead and open and go to replit.comreplit.com because there you can actually code along with me in javascript and it's a full ide and it's very easy very fast and it's completely free so make sure you just go to replay.com right now and just follow along with me as we code the rest of this in the video that you're gonna be watching is actually a clip of me teaching a javascript course but if you just follow along from complete scratch and you just open up your own replica you should be able to follow along if you want access to that specific javascript course that's also completely free and we'll link it down below and you can go ahead and fork that rupple which means that you just get access to that entire course and all the content and all the materials you could reference it but i want you right now starting from complete scratch just go to replay.com and start from complete scratch with that said let's just jump right into the video all right so now let's jump in and start talking about arrays and right before we do that let's go ahead and comment out every single line that is still running so line 108 for me i'm going to comment out and i'm also going to comment out line 121 and so when i hit run nothing should be coming out to my console perfect my console is looking crispy clean that's exactly what we want so let's go ahead and start working on arrays i'm going to go here i'm going to say arrays okay and why do you need a raise well what if you want to hold multiple different things how are you going to do that so if you have groceries right in a variable you could only have one grocery like a fruit but arrays allow you to have multiple things so for example i could have an array that has a banana it also has um apple okay i can also have orange okay we can also have a pair all right i could have groceries like this and you could write the words too so this is just a string and i decided to put an emoji in it but you could decide to put like the actual word pair here if you want but i just think that this looks prettier right so that's why i'm having it like this now if i'm doing all right and remember to just put a const here okay so we're just going to say cons groceries and here we go we have all of these you know we got a banana we got an apple we got all this awesome stuff in our groceries right now let's console log groceries okay and let's hit save and let's see what happens i'm going to move this here and boom you can actually see that it printed out all of the groceries okay now how do i get just a banana if we if i want right so if you click it it shows you the zeroth element is a banana the first is the apple the second is orange and the third is pear okay and remember everything in coding when you start counting it usually starts from zero so it goes zero one two three and the length of this is four okay because there's four items in here four grocery items now how can we get just a banana if we want just a banana how do we do that well here's what we do we go console.log groceries we put this bracket and we do zero okay and let me hit save and let's see boom we just got the banana okay well what if i want an apple what do i do can you take a guess i'll give you five seconds see if you could write that yourself five four three two one all right let's do it so you would have to put in a one here okay so boom let's do that let's do a one let's hit save and boom you see we got an apple now how would i get not the orange but the pair so how would i get the pair make sure to count correctly zero one two three right how would i get the pair what do i need to put in these brackets to get that pair i'll give you five seconds here five four three two one all right let's do it we would do three okay and you can see that we got the pair okay so again this is zero this is one this is two and this is three okay if we want to get the orange we would simply pop in a two here okay let's give that a try make sure you're coding along with me and you're just exercising with me okay make sure you're not just passively watching this make sure you're coding along with me every single step of the way that's how you're gonna get better so this is called indexing okay this is like you grab one of the indexes okay so here i'm getting grabbing grab the second or like the third index in this case okay um or second index okay grab the second index that's basically what we're doing here or grab the second item if you want to say it in a more human language but in programming this is a zeroth index this is the first second third so if you ever hear like hey it's in the first index or the third index that just means where its position is in an array okay and arrays are really really cool and um i'll show you a lot more about them soon all right so take a look at that right at the bottom you can see that and let me remove let me cons let me comment out this line here okay and let's run it again and i'll move this up over here it's easier to see but take a look at this right over here before we add the cookie and we console log this out notice that there's no cookie in here right but after we do the push and we console log it out watch what happens now we have a cookie that's added that wasn't there before so that's what push does push adds things on to the end of the array okay so try adding something else try adding a chocolate try adding uh a blueberry try adding something and see what happens and see if you could do it yourself okay so add something more here let's you give this a try so i'll give you five seconds five four three two one all right now let's try adding something together so i'm gonna say groceries dot push okay and we're gonna say i'm gonna do let's do blueberries okay and then i'll console log here paste it here hit save and run and check it out we got we got one the regular uh groceries array then we got this with the cookie this time okay and then we push the blueberry and you can see there's a blueberry at the end there so that's how the push method works okay great so let's um comment out all of these for now all the console logs so if you run your code you should see basically nothing okay it should be empty just like this perfect now what we want to do is we want to try grabbing like what if i only wanted the banana okay what if i only wanted the banana and the apple okay those are the only two things or the orange and the pear and that's all i wanted okay well for this there is a method called slice okay so we're going to do that so we're going to says groceries and i'm going to say slice and i'm going to say 0 to 2 and let's see what happens okay and let's make sure we do console wrap this in console.log because otherwise it won't show it to the screen let's hit command enter or ctrl enter and boom look at that we're getting banana and apple so it's starting from zero including zero okay so basically what we're saying is start from zero inclusive and up to two okay but but not including two okay so basically it's giving you the the index this is giving you is either zero or one that's the index it's giving you so it's either giving you this or that okay so if you want to get the first two elements you can do it like this okay up to two if you want to get three elements you do this okay so if i run this it's gonna give me three elements if i do four and run it watch what happens it's gonna give me pretty much all of the elements right if i hit five it's gonna give me the cookie as well that that we added and if i do six it's going to give me all the way up until the blueberries that we've added because we still have the push grocery stop push there boom so you can see the blueberries so when i do zero to six it's gonna go from zero to actually the fifth index and give me uh up to that point cool all right now question for you what if i wanted not from zero but if i want it from right here the pair until the blueberry so what if i wanted this part of the array can you splice that and get that out of the array for me please take a few seconds take five seconds five four three two one and let's work on it together okay so i'm gonna try and i'm gonna do okay let's do up to six one two three four five okay like that this is just an example so let's do console.log and we're gonna do groceries dot slice and i'm gonna say so let's actually count together okay so we have zero one thank you baby appreciate it two three okay so basically we're gonna say give us from three two four five and six but it's gonna go up until five okay perfect so let's go ahead and do that so i'm gonna go zero sorry three two six and let's see if this gives us what we want so i'm gonna hit run and boom it gives us from a pear to a blueberry let's try another example okay keep doing this with me don't get lazy on this keep trying this with me while i take a sip of my water that my girlfriend brought me but try getting me apple to a pair okay try getting me this right over here okay take a second um and see if you can get me those three right here okay so take about five seconds all right five four or take take a little bit longer while i sip this water okay so let's give that a try i'm gonna go ahead and do i'm gonna change this to a one because one will get me the apple and it'll be zero one two three and four one ahead of the one that we need okay and let's go four let's hit run and i get apple to appear exactly like i wanted right it was that that's what i wanted and that's what we got perfect very very good very good okay let's keep making progress now all right so now before we graduate from uh slice and uh if you notice that i changed my shirt i just had to do an interview because they were because my girlfriend is a wow superhero she's candy crush and um they're doing a documentary on her so i had to go do the interview and i had to change my shirt because i wanted something black and my previous shirt was yellow and nice and poppy and so i don't know why i'm telling you that but i thought you should know let's go back all right um so here groceries slice so this is good but before we graduate i'm going to teach you about two different array methods okay so there's a couple of array methods that i want you to know so we went over slice we went over push i want you to learn index of and i want you to learn uh length okay this is probably the most like one of the most common ones length uh so let's just go through it together real quick so now i'm going to say console.log and we'll do groceries okay and i'm going to do index of and what index of does it it will give me the index of these fruits okay so let me just comment out these i comment these slices out here so i'm going to hit run let's just do let's just run that one so we can see all of our groceries or fruits perfect and so index of what it's going to do is if i ask for the index of orange it's going to literally give me what the index of that is so can you tell me what the index of the orange is for example banana is zero apple is one so orange is you guessed it too so let's see what it says all right so let's uncomment these lines and let's go here and we're gonna pop in orange here okay and we're gonna say hey what's the index of this orange let's run it and i just gave back two all right let's ask it about the pair and let's run it and it give back three because this is three okay so that's what index of does helpful to kind of know you won't be using it too often i mean honestly when i'm coding majority of the times i don't use it but i just thought since this is the array section i cover these methods because they're going to be helpful for you all right i'll comment this out for now so index of and then the other method you need to know is dot length okay so that one is going to be important so let's go ahead and do console.log groceries and can you guess what length is going to tell you dot length length starts from 1 and so it will take the overall length of this so however many items are in here is going to give you that back so let's count one two three four five six so length is going to give you back six all right let's give it a try and let's run it boom there we go see that six right there that's it that's that's what uh that's what length does okay perfect so now we can basically say you've graduated from arrays yay good job there's a bunch more to it but this is just an over simplified you know what arrays can do you'll learn a lot more about arrays a lot better once we get to the loops because there's a lot of cool things you could do with arrays they're like my favorite thing in coding i think arrays are literally my favorite thing and next up we're going to start learning about objects okay so let's go ahead and start learning about them now all right now let's talk about objects so here's the first thing we're going to do we're going to comment out line 144 and 135 so there's nothing being printed okay let's go ahead and hit run and look at that we have nothing coming out to the console log that's perfect all right now let's go ahead and let's talk about objects objects are really really important and they're really awesome so in just one second we're gonna work on right when i snap my fingers we're gonna work on a new object all right this is the object we're gonna be working on so as you can see here we have mr leonardo dicaprio he is our he's our object in this case and he has a couple of properties so his properties are he has a name so if you do person.name you know that should we need to set that to leonardo and then his shirt here will be white okay because if you can actually see his shirt is white back there so we need to create an object that actually represents that so let's go ahead and do that now so i'm going to put him over here kind of in the corner so you can see him now let's create an object to represent that so we're going to say const person and object is created by doing these squigglies okay so these squigglies represent an object so so far right here we have created essentially an empty object now what we're going to do is we're going to add some properties to that person object okay so we're going to add a name so the name of this person in this case will be leonardo and shirt will be white this is an object if i format this you see this is how an object looks if you want to make it look a little bit like prettier and kind of how you know pros write prose would probably make it look like that okay kind of how it looks like here so open squiggly close squiggly it doesn't matter if you have it on the same line or you don't okay so it doesn't really matter which way you have it both ways will work all right now this here pretty fairly represents what we have over here you have a person object the name is leonardo shirt is white now let's go ahead and console.log and access some of the properties of this object that we created so we're gonna do person.name and when i console this you're gonna see that the person name and i'm gonna move this up here okay when i when i run this you can see that the person name is indeed leonardo kind of just like what we wanted right in the start and can you guess what person that shirt is gonna give me when i type that in and when i hit run take a guess exactly you're gonna get white okay just like we had expected here because those are the properties and that's how you access them so that's how objects work so first let's learn different ways of accessing this object okay dot notation versus bracket notation these both of these you should know okay so how do you access objects so you're right now you're using a dot notation now here's an example of a bracket notation let's do console.log and we're going to say person and we're going to do name like this and let's run it and boom you also get leonardo right over here well give it a try try doing the shirt but with the bracket notation try accessing it i'll give you five seconds five four three two one all right let's see if you got it so i just triple clicked to copy and then i did command c to copy and then i went down and command v to paste you could do ctrl c or ctrl v to copy and paste and then double click the name inside here change that to shirt hit command s or ctrl s if you're on windows to save and automatically run apparently it's not automatically running so let's run click the button here or command enter and boom you can see that the shirt is coming out white right so we got different ways access an object that's really good now there are different ways to assign things to an object as well so for example i could give him a phone number okay so i could actually do person.phone so notice we don't actually have phone in here and i can do phone and we could give him a number like that okay person.phone and then we're gonna go ahead and do console.log and we'll say person. let's go ahead and try it let's see what happens and boom you can see that we actually get that phone number right there but also you can actually console log the entire person as well so you can see what that looks like so let's go console log person let's go ahead let's hit run and you can see you can see this object right over here name shirt phone so now this object has all of the things and it didn't have that before so it's kind of like how in arrays we were almost doing like push it's kind of like that but like it's just you're creating a new key on the fly and giving it a property another way you could actually create that property is by putting this in bracket notations as well because you could go like this bracket notation and say hey person phone is actually equal to this hit run and watch what happens boom you got the same thing and that also works so both work okay the reason why we're putting the phone number in quotes by the way as in we're making it a string is because if you didn't do that it would try to subtract it will do 1 minus 2 2 2 minus 3 3 3 minus 4 4 4 and evaluate to a big negative number that's not what we want right so put that in quotes because we want the exact phone number and phone numbers or strings all right so now let's say you want to create another person okay so i'll give you an exercise for you to do and let's go ahead and do this right now so i'm going to give you a different person with different name and different shirt and i'm going to need you to create an object a person object that represents that okay so you can call that object person2 all right so let's go ahead and do that here you're going to call this object person 2. all right so now instead of leonardo dicaprio you're going to get somebody better yours truly and you have the object right over here okay so this is the object that you have and these are the properties that this object have okay so try to see if you can make this object by yourself either pause my video or take a couple of seconds right now to do this but i'm gonna start doing this in five four three two one okay let's do it so i'm gonna basically there's a const person two and i'm going to say name is causey right and make sure to put a comma so after every property right so let's go through an object so we have squiggly bases squigglies in the start and closing okay instead of brackets object arrays have brackets and then you have a key and a value all right so objects are key value pairs all right so you have a key here and a value here a key here and a value here okay so a key you can also sometimes call it a property okay so here the key is name and the key is shirt okay and name and shirt is also the property so you could say kind of both ways so you put in the key right person name and you get back the value which is actually the name right you do the same thing here with the phone so you take your object you put in the key right and then that key unlocks and we set this value for that key again we do person.phone so we did the object and the key and then the key gives us the value right and kind of think of it like opening a lock all right so name here so this is the first key value the second key value we're going to set is shirt is black okay and uh that's it that's the way you represent this object right so if we do console log person2 and run it boom you can actually see that it's showing you quasi uh and shirt is black okay that's super cool and uh if i change this to you know what if i want quasi's name what if i only want the name can you try that for me if i only want the name boom hopefully you got this correct okay let's run it and now we're getting the name well what if i want the shirt as well same idea right if i run this we're going to also now get the shirt so shirt color and we got it as black let's turn this into a function that then says hi my name is qazi and my shirt is black okay so let's go ahead and create a function okay so remember how we create functions right there different ways you could do function or you could do the fancy way like this we'll call this function introducer because it introduces you to other people okay and this is gonna take in let's say we can give it a shirt and it will also take in a name so let's go name and shirt okay and if we want to turn this whole thing into oh sorry whoops and we're going to do if you want to do error functions this is how we're going to do them okay so we're going to go ahead and say name comma shirt and we're going to do arrows like this i'm going to go here all right now the final sentence that we're going to return right is going to basically be remember template literals all right so let's use the template literals again and we're gonna say hi my name is name okay and we're just gonna say name like this for now my name is name and the color of my shirt is whatever the color of your shirt is okay so in this case it should be like black so here let's say if we had it hard coded it would be like this but we don't want it hardco um we don't want it hard coded right so we're going to do here is name we're going to make it dynamic we're going to send name and remember and see if you can make this shirt dynamic as well boom we're going to say sure and we're going to save this into a variable called intro so const intro is equal to that uh but we're going to make an object first so just so you can keep practicing an object okay so we're basically going to say we're going to create a cons person object and this object is going to have a key name and the value of that key is also going to be named because that's what we're passing it i'm just going to say name right here and it's also going to have a shirt and that's just going to be the shirt that we pass it okay so these are values that we're giving it these are the actual keys that are hard coded okay so these will stay the same these will be exactly name and exactly shirt and this part will be dynamic these are the values that we give it and so then we're gonna in order to get this person's name remember what we have to do right it's person dot name perfect and now how are we gonna get this shirt we're gonna do person dot what person dot shirt perfect good job if you kept up with that okay so we're creating an object right here and we're creating the intro and now we're just gonna say return the intro all right that's our function and let's give it a try so let's go console.log let's call our function introducer and let's give it a name quasi and let's give it a shirt color and we'll say black and let's run this and look at that right we're getting hi my name is kazi and the color of my shirt is black beautiful so we're using template little i mean this is advanced this is awesome if you're doing this good job for doing this this is using this is a es6 error function right this function has two arguments and you created an object in here and then you're using template literals so there are a lot of concepts in here that you're doing so great job on following along and doing this with me even if you didn't get it it's good that you're you know you just followed along and like did it and hopefully you got it working on your computer like that's what's really important so here i have this and if i if i show you right hi my name is kazi and the color of my shirt is black now let's go ahead and comment out all the other stuff that we had here right so i'm going to comment this out um you can leave this or comment it out i'll just comment it out you can comment out pretty much all of this okay and i'll comment out this person right here too and we'll just hit run and it should just say hi my name is kazi and the color of my shirt is black okay so the only console log that should be happening is the one coming from this function this function is super cool because this function is like creating an object and then doing the template literals accessing data from that object very nice okay this is a very very nice uh well-written function so good job if you got here so far now try see if you can now do this call the same function but see if you can call it on leonardo and with the shirt being white so give that a try i'll give you five seconds five four three two one all right let's do it together so i'm gonna say console.log we're gonna say introducer and we're gonna see leonardo here right and the shirt color is going to be white these arguments have to match right so this matches that and then this matches that okay so these arguments the order here matters okay and uh let's run this and let's see if it does the leonardo one as well and it does hi my name is leonardo and the color of my shirt is shirt okay so we probably gotta say white here hi my name is leonardo and the color of my shirt is white beautiful this is doing exactly what we want we're going to go ahead and comment this out and so far you're doing absolutely fantastic all right now let's take this even a step further let's say that we want to have this person but we also want to calculate and have their net worth okay so i'm going to go ahead and refresh this page and let's just uncomment the quasi in black here perfect my name is kazzy and the color my shirt is black blah blah perfect we want to say uh this person has certain assets okay so assets they have is like let's say a hundred thousand dollars all right and the debt they have is fifty thousand dollars so if you want to calculate the net worth it's really easy it's just assets minus debt or assets minus you know you could say let's say liability all right so it's how much basically money do you have and then how much debt do you have it's very simple for an average person so if you want to calculate your net worth you could do that right now like it's very easy you could have a negative net worth or a zero net worth and if you have a positive net worth that's incredible so here you would have a net worth of fifty thousand right hundred thousand minus fifty thousand we could do that right and so we could say and my net worth is and i could do this and i could basically say uh person.assets minus person dot liability liabilities and paste that there okay so let's run this and let's see what we get hi my name is kazi and the color of my shirt is black and my net worth is fifty thousand dollars we could put a dollar sign here right and we could say we could say we you don't need to say usd but you could if you want and it says my name is blah blah my net worth is 50 000 usd so this dollar sign is uh pretty much to actually put a dollar sign and then the next dollar sign and squigglies are to basically say hey i'm writing javascript let's make this even nicer i'm going to teach you how to create methods inside of an object okay and anytime you've seen dot met something that's usually a method okay when you're calling it so like math.floor remember when you saw math.floor math.floor this is a method if you didn't have this math part here and it was just like this this would be a function and if this open close parenthesis weren't here this would be a property so i know it can be confusing but here's a great example all right you have an object right here properties are just with dot to car.name car.model those are all properties right but methods are car.start with the open close print card.drive open close print car.break open close paren so methods look like this all right so just kind of take a mental screenshot of this for now all right so here's what we're gonna do here's a really cool thing and kind of advanced thing that you could do with objects so i can actually create a net worth field and what this will do is what i essentially want to do is i'm going to say hey assets minus liabilities but if i do this i'm going to run into a problem okay so let's try that so let's remove all of this and let's paste just netperson.net worth and let's see what happens if we run this and it says assets that's not defined and you're probably thinking wait access is defined right here so why isn't it just using assets this is that's not how it works so in when you're defining an object because all of this is happening at the same exact time this isn't like created yet and so it doesn't know so what you want to be able to do is you want to be able to say person dot assets but it also won't really know that so here's the hack here's a way that we do this okay so we're going to create a function and yes you can actually store functions inside of objects which is kind of crazy open close paren all right we're just going to do this this is going to be your net worth function what it's going to do is it's going to return and we're going to say this dot assets minus this dot liabilities this thing is kind of weird but what this is referring to is this specific person object that you've created so it's literally like imagine plugging in person.assets and person.liabilities okay but you're going to use this this whole thing here is going to become 100 000 and this entire thing is going to evaluate to 50 000 and then this thing is going to evaluate to 50 000 like this and then once you return this it's going to pop up in your console so let's go back boom this assets sold liabilities and net worth is a method so even though we've defined as a function it is a method and i'll show you why because now in order to call it if you do person not net worth watch what's going to happen it's not going to work let's run it and we just got hi my name is kazi and the color of my shirt is black and my net worth is function return assets so it just like just like spit out this entire function that's not what we want we want the actual result so we need to call this method and this is how you call it okay boom you call it just like a function but because it has a dot here it's called a method so the person has a net worth method now let's run it and watch what happens boom right my name is causing color my shirt is black and my net worth is 50 000 and now to make this code even more dynamic assets and liabilities should be passed by the user okay so that could be like another thing that that might be nice right now it's hardcoded but that's okay we can just kind of leave it as is just to kind of show you that you could do some more advanced stuff with objects all right so let's run this okay and if you change the name it'll also work change the assets liabilities you'll get different numbers all right so if you got this far great job uh we're gonna hit run on this to just make sure we we're gucci but if you got this far you're almost an object master so good job okay you probably don't know like everything there is to know about objects okay but like you know more than 98 of the population so you should be proud of yourself give yourself a pat on the back right about now go ahead and do that for me and and you're beautiful and you matter and i'm happy that you're here so thank you warms my heart all right with that said let's continue all right now let's keep having more fun with the arrays because the more practice you get with arrays and loops the better of a programmer you're going to become because honestly that's like 90 of what you're doing in programming you're usually getting max a lot of data and you're looping through it and you're doing something to it okay that's like super common and so let's just get a bunch of different practices in all right so we're gonna go ahead and make another function and uh this function we're gonna make is going to be called letter counter okay so let's go letter counter okay you can also call it counter whatever you want and we're going to use it making an error function all right and basically what this function is going to do is it's going to take a look and see how many letters are in a given phrase okay so if i gave it a phrase let's say khan's phrase is equal to hey can you go to grocery store okay whatever all right this will go through this whole thing and it will basically just just count how many letters there are okay let's call it how many letters and so you're just gonna go through and this is gonna be a one there's gonna be a two this is gonna be a three a four a five for the empty space six seven eight nine so you can do this you can count an empty space as a letter that's totally fine so think about how you're gonna write a function like this right think about what are you gonna need to do well you're gonna need to loop through this and the cool part is you can actually loop through these types of phrases you can loop through strings as well first let's comment out um anything we kind of have up here okay perfect and now in here i'm gonna go ahead and i'm gonna say let's let's write a loop okay i'm gonna say for letter in phrase and let's see if this works and let's do console log and we'll say letter okay and let's hit run and let's see what happens okay it's running okay so what we need to do in order to make sure that this actually runs is we need to call this function that we just made right so let's go right here and i'm going to say console.log and we're going to say how many letters okay and let's just call that function and there we go okay one two three four five and then it ends with um undefined okay all good so this keeps looping through and then it just prints out the letter that's what we're looking for right and we actually don't even need to call console.log here because we're printing out from the function so let's try running this and now you see there's no undefined right and we we go from 0 to 40. but it needs to start from a 1. and if you think you're getting this or you know how to kind of solve this or if you even want to try i highly recommend you just pause my video right here and actually give this a try okay go ahead and try to like do this on your own and then you can come back to this but i want you to get a ton of practice with this stuff because that's how you're gonna get better not just by watching me but by doing it with me that's how you're going to get better so if we want to count the the letters in here right or this this how many are in there we can't start counting from zero okay so what i'm going to do is we can just make it where if i'm getting uh a letter here right oh and if you were a little bit confused like wait how are we when we run this how are we getting the numbers with this four loop right so you can also say for cons letter and if you change this to of because remember i taught you like i taught you four of loops earlier so if you do letter of phrase you're actually gonna and you run this you're gonna get letters the actual letters all right if you run um letter in phrase it's gonna get you the index at each one of those right so if you wanna get 41 well what we want to do is uh just do letter plus one okay so this will just increase it by one whatever it is and if and for whatever reason this letter thinks it's a string so it was doing that weird thing where it's attaching you know zero plus one is zero one and one plus one is one one it's attaching itself so it thinks letter is a string so just type cast it into a number so force it to be a number and then you get this okay so this is like one very silly way of doing this and you get oh i get 41 at the end so that's it that's how i'm getting my this is how i'm counting my letters but there are better ways i'm going to show you in just a second all right so now let's talk about better ways of doing this yeah so what we want to do is we want to have we want to set a variable here that let's say is so we're going to say let uh result equals zero okay let's go here and we're going to go we're basically going to say result is equal to number and we're going to call index and we're going to say plus 1. because all we're doing here is the loop already allows us to get up up to 41 right so we know that this is the correct answer all we want to do is store that correct 41 in result and then at the end of this loop we want to just return that result at the end that's all we're looking to do okay so because okay so i'll give you an example okay let's just do result is index and here let's just return result let's remove this line well we can keep that line so you can just see how the loop looks and we're gonna run this function but wrap it in console log all right actually we can do a trick here to make this look really nice so here we have return result right in order to make it we want to make it look like this result is 40. so there's a nice trick we can do in our return and we can basically say we can return it as an object so when you print it out the object is going to look really nice like this right and then there's a even nicer shortcut where if you just add in one thing and wrap it up in an object it's going to automatically set this as the key and use this as a variable for value so it's going to set the word result as the key and use the value 44 from this so it's a super cool little hack and you can use this pretty often so let's run this and boom you can see that result in this case is 40. so now you can see that result is returning pretty much the last value in this entire thing that we get right when we print out our uh indices so these are this is you know this is like basically saying this is zero this is one this is two but we just want to start it off with the one so um let's go ahead and all we need to do here to index is we just need to add a one to it and also remember we need to wrap the index in a string just like this oh sorry not in a string i'm sorry wrap index in a number because it is a string okay so we need to typecast it let's run this and boom you get result is 41. right so now phrase we can change the phrase right and again how many letters is a function that takes in the phrase so let's make this let's extract this argument out now and instead we'll put phrase over here and we'll pass it in the phrase okay so the function itself shouldn't have to worry about the phrase it should just wait for what the user is going to give it as a phrase and it says result is 41. now imagine i change this and i go prompt the user for you know write your phrase okay and now let's run this and now this pop-up happened at the top so i'm going to write my phrase i am wearing a nice green shirt right so when we do that let's go ahead and run this and i hit okay and let's see what happens here let's hit okay and right when i hit okay boom we got 31. all right so that that phrase has 31 characters now here's something sad i'm gonna tell you you can actually replace there is a method for strings that allows you to calculate the total of uh you know see the length of a string and that is just dot length okay so for example if i well we don't open up a shell let's just stay here so say i have a string here okay let's zoom in so you can see that better all right so say i have a string here that is banana if i just call dot length on this and run this you notice it'll give me a six okay and uh let's call let's say banana wants to know your slogan dot length it'll give me back 32 so a dot length is a property on anything that's of type string you can call anything that's a string you can call dot length on it okay and that returns the final answer so let me zoom out here which means which means i could actually remove my entire code right and basically just say the result is phrase.length change this whole thing with one line of code hit run write your phrase it's going to say something crazy and let's see if it can calculate it fast and it did and it goes result is 107 okay but let's write it in our way because remember that's the way you learn how to do it and right now it's important for you to manually learn to do these things because i want you to develop and focus on your skill set then once you get better normally what you would do is you know javascript string um length you will just like google this okay and see if that exists and you'll google this and you'll go somewhere and it will say that let's see w3schools is usually a really good resource and it's just showing you text make a text let it equal to hello world let length be text.length and it also allows you to try it yourself so you can always click that and you could hit run right and boom it says 12 here and what if i change this to something random and let's hit run and you can see it says 38 right over there yeah so uh this is how you would normally do it in real life you just google these things you have to memorize anything okay but right now i'm just kind of getting used to having you get used to exercises do them yourself okay now let's build it up and take it up a notch okay so let's let's uh make a function that sums things okay that's also going to require loops and arrays so i'm going to go ahead and we're going to comment this out and we're going to comment this out so we're going to go const uh sum array it's going to take in some numbers and we're going to have a result we're going to say let result equals zero okay and then we're going to have some kind of for loop action going on here and then we're gonna return the result and let's just do this trick like that we did last time and something is gonna go in the for loop okay something is gonna go inside of the for loop oh sorry we forgot to put the arrow functions not bad arrows there we go and what's going to go inside of this for loop is basically going to be imagine this right imagine how we would do this manually so say we have uh these numbers or let's say we have these numbers okay this we know that this should if we ever have 1 2 3 4 we know that this should equal to 10. now how are we going to actually sum this up in normal life what would our pattern be well i probably have some variable let's just call it result and i would set it to zero in the start or i might set it to whatever this number is right over here okay so you could you could do either or but let's just start it with being a zero okay so it starts off being a zero then what we do is we loop through and we make the result becomes one then we loop through again and we do one plus two and then result becomes two oh sorry three then we loop through again and we already have this three here so we add three to this three right here so we add this to that and we get result is equal to six and then we add four to that and we get ten so that's how we would do it kind of as a human being if we were to do it like manually and do it with each step and that's how we're gonna do it with our computer we're just gonna have it loop through and keep adding these numbers okay so let's go ahead and now write that loop so again try to give this a try and see if you can do this yourself okay because what you've learned so far you should be able to come very close to solving this so take five seconds to do it and also pause your pause my video in these five seconds to try it yourself so five four three two one all right let's get back to it so here we go i'm gonna say for const number of numbers all right and let's just call console log number and uh let's call our function console sum array okay and let's give it an array so we're going to have we're going to give it this array of no it's going to be called nums okay let's remove let's comment out all of this because we don't need that and let's pass our summary nums and let's just run this okay so we get back one two three four because it's looping through and printing number out each time so if i give it an array of one two three four it goes okay number is one the first time print it out number is two the second time print it out numbers to read the third time print it out so on and so forth all right so it's printing this out result is still saying zero so now what we need to do is we need to increment our result so let's go ahead and do that now okay so again try to give this a try and now we're going to do this so all i'm going to say is i'm going to say result and i'm going to say result plus and we're going to do whatever is the new number that i'm getting okay and let's just see if this works and i'm going to hit run and it says result is 10. interesting did that really work well let's try it with and let's add 5 to this array and let's see if that works and i'm going to hit run and we should get 15 and boom result is 15. beautiful okay so let's see um how this is happening what is going on all right so let's do something to visualize this code and see exactly how this works i'm going to copy this let's go over to pythontutor.com all right and then hit enter once you're in pythontutor.com you're going to go to related services where it says javascript tutor okay and click that this website is awesome so paste your code and uh just for simplicity sake i'm going to remove any console log because we don't really need that it's going to help us visualize so there's going to be no need for that and we don't need any comments either it's unnecessary for you to remove it but i'm just going to remove that for simple simplicity sake then hit this big button here that says visualize execution so let's click it okay perfect all right now on the left hand side is going to be our code on the right hand side is going to be visualizing exactly how this code is running very important to notice also this red arrow means the next line to execute so which is the next line of code it's gonna execute okay so it's already telling us that this is gonna be when i hit run or when i hit next to step through this this is the line of code that the computer is going to run and we're just going to go through step by step and see exactly which line runs exactly when and green arrow will mean the line that was just previously executed cool so this is our function let's walk through this all right so let's go ahead and hit next okay so now it sees we have a function called sum array and boom it's like okay i got you there it is there it is i see it let's hit next again now it's on const uh const nums as you can see right here right so this is the line that's about to execute next so let's hit next and it goes okay i see that there is a variable called nums and it's inside of this array okay or it's this array right here it refers to this array and this is the global frame because this is uh these are all variables that are outside of this function so these are accessible from anywhere and from everywhere okay and we'll talk more about scope in the upcoming videos now next line is going to execute is it's going to call this function summary with the argument nums okay and then our entire function is going to start running so let's hit next cool so now we're inside of our function and the first line of our function is just about to run so the first line is going to be what result is cool so let's uh right now result is undefined okay but once this line runs here result is going to have a value so let's hit next and boom result is now set to zero now okay so this green is just showing you the last line that ran now the red is showing you that we're about to start running our for loop and we're just gonna be teeter-tottering in between this line the for loop line line four and line five and line four and line five and line 4 and line 5. until the entire loop is done then we're gonna go to this line here and return the result at the end okay which and again remember uh you know we're going to touch base on scope but if you put this if you put things in the wrong scope wrong things will happen for example if you put this result inside of this these squigglies here right this is just going to keep returning every single result but that's not what we want we want to return the results after this entire for loop is done right and then we want to return this result okay so let's run through this and see what happens okay so right now it doesn't know what number is either okay but it does know it results as zero and it also knows that numbers refers to this array right here okay let's hit next okay boom boom boom all right there we go and now it says number is one perfect that's exactly what we wanted right number is one because the first time we're going through the array right it's a one that's why that's where the number is coming from now let's see what it's going to do the result so watch what's going to happen it's going to do results going to be 1 this time so it's going to do 1 plus or sorry result is zero right now so i'm going to do zero plus the number one okay let's try that and now you can see result is one let's do it again now number is two so two plus one result is going to be three okay three plus three result is going to be six six plus four result is going to be 10. okay and notice how it was just like boom boom boom boom it's just these lines running over and over again right and now the result is 15 now the loop ends because there are no more numbers to loop through and we go to result result is 15 and then we return the result at the end return value right and the return value here in this case is this object it was of type object and it says result is equal to 15. so there you have it a full visualization of a for loop exactly how it works and what is happening and hopefully that clarifies things for you a lot further so i want you to try something try writing this function some array by yourself from scratch and i know we just did the answer but i want you to give it a try without referring to this and see if you could create this function by yourself and if you could that would be fantastic and just give it give it give it a shot right and if you could get this working see you know that would be absolutely amazing and then check back with this and see how far you got all right let's do some more exercises with loops arrays objects whatever right because this is going to make you so much better so let's make a function called max supposed to const let's say find max okay or you could call it max as well and it's going to take in numbers and it's going to have um let's say uh cons result and then it's gonna return the result okay and then we're gonna have something some loop stuff happen here okay some loop stuff and here's how the algorithm is gonna work okay imagine we have an array of you know let's just say one two three and four and five okay imagine we have this array right here and let's just put commas here because that's how the array would look have these commas and what we're gonna do is we're initially gonna set result to one and then we're gonna loop through and we're gonna say hey it's two greater than one check these if it is then result set it to two okay then loop again all right is three greater than two if it is then set result to this then check between these two numbers if four is greater set four if five is greater set five and then once the loop is done it's going to return the biggest number okay and now imagine if you know in in let's say there was a case where we had a one here okay let's see if it would work if that same algorithm would work there okay set one to the biggest set one to result okay great now check between these twos two greater yes then make two the result okay then check between these is three greater okay make three the result check between these is four greater okay make four the result check between these is one greater no then don't make one the result keep for the result right and and then that's how we're we end up with four so that's kind of the algorithm that we would do okay now we just have to code it up so we're basically gonna say set result not to zero okay because imagine what if you had an array with only negative numbers in it well then zero would be the biggest number so that's a common trap do not set result to zero we're gonna instead say results is the first item of this array okay we're assuming we're getting back an array or we're getting an array here okay and then we're gonna say is loop so we're gonna say we're gonna write a four of loop const number of numbers and basically we're going to say if number is greater than result in that case what do we want to do we want to set result to that number that we just found and at the end when we're done with this loop we just simply return the result okay so let's go ahead and give this a try and let's comment out anything else that we might have running okay we don't have anything else running so we're oh we do so let's comment out these lines right here and let's just go console.log and i'm going to say max and let's give it numbers like one two three four five and let's see what it returns as the max aha yeah so this is not a const let's make this a let because we are going to be changing result okay so this error is saying assignment to constant variable so all it's saying is hey you cannot redefine a constant so that's why i'm changing it to a let now you can redefine it right here and we should be good and let's do that trick again let's return result as an object so it looks nice when we print it and you can see result is five now imagine we have one comma two one two so the max in this array is what it is a it's the four so let's see if it gives us a four yes it does beautiful look at that let's go 20 now and 10. and let's see if that works beautiful and now let's make this array now let's make this massive right like let's just um yeah let's let's take this right here and let's just paste a couple of times and run it and look at that it says result is 20 and if i make this a 3 over here and run it boom result is 30 i mean it will find it wherever the max is it will find that max right so this is a really nice function that we have created now let's keep building on this okay so this is all going to be making sense and trust me it's going to be so important for you once you start getting to building projects and doing those types of exercises you're going to wish that you spent the time to learn this because this is going to be a complete game changer for you all right so our max function is done let's work on a different function now and what this function will do is it will calculate frequency okay so let's go ahead and try that so we're going to say const frequency okay it's going to take in uh let's say a phrase that somebody will say and it'll tell you how many times uh that phrase or that or sorry that that letter occurs okay so let's just say letter frequency okay so what does that mean so say i said haha here's what it should return okay so if i call the function letter frequency on the word haha here's what it should return it should return an object and the object should say h occurs twice and a occurs twice that's what it should return so how do we create this function so it does that if you want to give it a try you can this will be hard because you'll need to use your object knowledge as well that we put together a while ago but this will be using you know summing up numbers looping through things iterating and all kinds of stuff like that and so uh incrementing variables it's going to be a tough one if you try to solve it yourself but you should give this a try right about now and we'll continue in five seconds five four three two one let's continue so i have letter frequency how do i determine this okay let's figure it out together first let's just go ahead and call this function so we're just going to say letter frequency and we're going to give it that phrase haha like this and uh let's console log that phrase let's delete that line and let's just make sure that this letter frequency is running okay haha is showing up perfect now what do we want to do we're we're getting haha well we need to actually start writing a for loop that will start going through all of those letters because that's going to be important okay so let's go ahead and write a for loop i'm going to say four letter const letter of phrase and let's just go console.log and we're just going to console.log each letter and let's see what we get we get h a h a okay great now this is where objects are going to start becoming very very handy because you know key value pairs are going to be absolutely clutch for this arrays are not a good solution for this okay but like objects are objects are also great for storing you know information in your phone book like on my iphone when i'm going through that is being stored in an object okay that's why i have you know my name my you know i could look up somebody by their name their phone number etc and objects are great for that so we're gonna go and look at something in the console okay and that's going to make our lives a lot easier so say we have something called word and this word has let's actually give it let's actually call it frequency right or freak um and let's say this frequency has that the h is once and that's it okay so it has one h in there now if we have a word like haha or whatever right or if we want to just check yo is h inside of the frequency we're actually able to check that and it goes yes h actually exists because it can if you do h and frequency what it does is it looks for h as a key ins and searches all the keys inside of an object okay so it goes yes h exists and uh if i go you know because of the word haha does a exist in frequency and when i look it says no that a does not exist that's actually false so using this we're actually able to see if we've already stored something in our object or not because the way that we want to do this is we want to go and say okay so say we have the word haha then i want to have a object i'll just write it out here and it should loop through and in the first loop it should notice that there's a letter h so as soon as i notice the letter h i make a key for h all right and because i know it showed up once i set it to one so i go that key is equal and it has a value of one now then i loop through that other word again and this time i see that we have a okay perfect well have i seen a before no i haven't so then i set a to one and so far so good okay then we loop through the third time and i see h again well i've already seen this before so what do i do if i've seen this before am i gonna go ahead and make another h and then set that to one you can't do that because in objects you can't have the same key repeating twice so we have this key here this key here so what will happen instead is we will end up overwriting this key and just do h is one and that's what it was in the first place but that's not the result we want we actually want to we actually want to increment this key right here this h1 right so we want to increment this one by doing plus one and making it a two so how do we do that well what we do is we go hey does h already exist and we do this little checker here right there and if it says yes h already exists then we go go to h and and and add one to its value so it goes okay i'm gonna go ahead and i'm gonna do that so we go to this and this becomes a two right and then we go to the fourth letter in haha and we do the same thing and we go hey does a actually exist in frequency and in this case it's supposed to be a true and it goes yes a does exist and so if that's true we then go okay find the existing a find its value and increment its value by one and then we get two oh okay so that's how that works that's how that works so let's go ahead and basically say that algorithm but in code this time so first we're gonna make a frequency make frequency object and that's gonna store all the memory slash all the data of what it has seen so far okay so this is so make a frequency object okay it's going to be called frequency kind of like we did here okay but in the start it's just going to be empty it's going to be an empty object then we obviously loop through all the letters of the phrase and we check if that letter that we found in phrase does that already exist in frequency so check if letter exists in frequency so you make sure to you know we're kind of writing pseudo code right now because we're just writing it kind of like code but in english and then later you just turn it into code and then you're done so if we check if the letter exists in frequency okay what next well if it exists remember if it exists we're gonna increment by one otherwise we set it to 1. so let's write that check if letter exists in frequency if it exists so this is going to be indented because this is going to be nested inside of this if condition and we're going to say if it exists well then increment the value by one otherwise set it set the value to one okay that's essentially all we're doing and so now take take some time to see if you can actually write this as code and then obviously at the end we're just going to return that frequency that we created okay so we're going to return that frequency so const or let's go let frequency equal an empty object so see if you could code up the rest of this okay take maybe a minute or five minutes or whatever and even if it takes you 20 minutes it's totally fine because this is normal okay you're not supposed to get it this fast this is supposed to take you a lot of time but take some time i'll give you five seconds to pause the video and try it on your own five four three two one all right let's do it together now check if the letter exists i'm going to say how do i check well i just checked by doing letter in frequency okay does that letter exist in frequency this will evaluate to true or if false that's it i'm going to say if letter is in frequency then do the following okay then we're going to say increment the value by plus one so how do we do that let's do that right now we're just going to say frequency of that letter so we're creating a new key on the fly and we're going to say equal to whatever it was before plus one okay so it's like whatever frequency of the letter previously was just like incremented by one okay if it doesn't exist which is the only other scenario then what we can actually say is we can go and say hey set the frequency of that letter to one that's it and then we just return that frequency okay so let's try it on this word haha and see what we get and look at that we get h is 2 and a is 2. that's crazy right like we just coded that out from scratch and it works so nicely and let's try um lol what are you doing later tonight lol haha okay let's run this and it just runs through every single letter right here and it says l happens about five times is that true let's see l so one two and then we got three four five yep so that looks like it happens five times and it says how many times o happens and it says how many times a comma happens how many spaces there are and it just shows you the whole freaking thing right so frequency is working as expected now one thing i'm going to show you is called incr these are incremental operators okay at least that's what i call them and they are plus plus minus minus plus equals okay um plus equals one and so these are incremental operators they're really really helpful and make everything shorter so for example this over here you won't ever see it written like this if somebody's coding this up they're gonna write this like this okay so it's gonna say plus equal one so plus equal one is the same thing as frequency letter is equal to frequency letter plus one so these two are equal and i just want you to kind of get used to seeing them okay and we can go and change some of our stuff above as well so like where it says result equals result plus number right how could we change this here try it on your own take about five seconds and this you should be able to do in five seconds five four three two one you could change this with a plus equal all right incrementing it like that okay and if you're incrementing something by one here's the cool thing you could also just do frequency plus plus and that just this just means that you're incrementing frequency by one so let me try hitting run you can see that this works right you can hit one run on this and you can see that this also works and gets the same result and we can you know or you can go back to the oldest one and see we get the same results so it's a matter of preference you never want to write it the long way you won't ever see it written the long way and you could write it in between these two ways so you could go plus equals one or you could go plus plus either work okay so i just thought you should know those you could also go minus minus and that will like decrement it okay and um you know so i'll just leave this as plus equals one for you just so you can wrap your mind around this and get used to seeing it okay so these are incremental operators they're very very useful and very commonly used now i'm going to remove all these comments or i can just leave them here if because you know this might this might kind of hurt your brain a little bit how's this frequency thing working so i'm going to leave that here for you all right now let's just keep building on this and let's keep taking it a step further so now instead of letter frequency let's improve our our skills and and do something called word frequency okay so we're going to build a new one i'm going to say const word frequency and see if you can try to challenge yourself to take a stab at this phrase and then we're gonna run this function okay and we're gonna do the similar similar type of approach we're going to create frequency here as an empty object and we're going to return frequency at the end all right um and inside of here we're going to do our like coding okay this is where we're going to be doing our logic now here's the one interesting part here i'm going to comment this out obviously let's comment that out let's go ahead and console.log word frequency to make sure it's running and let's give it a phrase the phrase is lol what is going on lol what law let's just say that okay lol what lol so we know that if we run that here's what we should get so if i run this function so let's have an example and this is also really good for documentation because by looking at this people can know how to call your function and then what your function actually returns at the end so i'm going to say word frequency and if i call this function with this phrase law what law it will return the following it'll say lol occurs twice and what occurs one time okay just like that and um this is what it should return so how do we make it so it does that well i wanna i have to teach you a couple of things for this so say i have a phrase and let's go phrase is equal to lol what lol so what we would really love for to happen is imagine we had an array and it had a it had things like this that'd be really nice because if we loop through something like that right in an array it would just loop through the words and we want to loop through the words instead of letters that's the only difference here between last exercise and this exercise okay so how then the question is how do we get it into an array so we can loop through it because if you try to loop through it in a string watch what's gonna happen okay so if you try to loop through in a string it's gonna loop through and print out every single letter from here so let's try to write a loop and loop through this i'm going to say 4 letter in phrase console.log letter okay and we're gonna hit enter we're gonna close the parentheses and run this right and it's just gonna oh sorry not not in let's run that loop again and say of of and it's just going to print out every single letter well that's going to be confusing because it's going to count letters for us and instead what we want to do is we want to count words entire words right and words in this case are separated by what what's a common thing all of these words are separated by take i'll give you a hint they're separated by these spaces okay so we can do something cool in javascript we can turn this into an array um and if we turn this into an array so let's say i give you phrase and if i call dot this method got split on it it's a string method and i say split it by spaces and i hit enter watch it what it's going to do it's going to turn that into an array and now that array very clearly has three items and if i loop through through these items watch what's going to happen so i'm just going to call it array okay and then let's just loop through this f4 word and array console.log word i'm sorry i'm just used to python so it's for of of of of so for word of array yeah law what law so look at that i'm actually looping through the words in that array and i can now in my object see if this word exists and if it does exist then i can increment it by one and if it doesn't exist then i can just set it to one so it's the same exact thing but just with words this time and all you have to do is turn it into an array of words and then you're good so let's give it a try now what we know so we're gonna once we get our phrase okay what's the first thing we're gonna do to it what's the first thing we're gonna do to that phrase to turn it into an array we're gonna do phrase dot split yeah this is going to turn it into an array but we need it to be separated by what it's separated by spaces in this case okay so boom this is going to give us what we need so now let's let's go ahead and um i'll do let's just call this words okay and um now let's console.log these words and hit run and watch what happens now i hit run and watch i got lol what lol okay if i have hello here and i run it lol hello yeah so now it's doing what we wanted to do now the algorithm is going to be pretty much the same as letter frequency very very similar to that so we're gonna basically use like a similar similar formula okay that we did here so we're gonna say we're already creating that frequency object there yeah and then we are returning it that's good so we're doing that here too we're creating it and we're returning it so that's good and we're gonna do the same thing so we're gonna say for const word of words okay let's just console log the words for now and let's just see what it's console logging so let's run it and you can see that it goes lol perfect every word and we're going to do the same thing check if it exists in the frequency if word and frequency then we're going to say we're going to increment it by 1. so we're going to go frequency word plus equals one else frequency word set it to one and at the end return frequency so let's try this and boom look at that lol is twice what is one time and it's already working right that's crazy so let's go yo yo yo and let's run it and here we're gonna see that yo is coming out three times lol is coming out two times and one is what is coming out one time so i mean this is so cool we just made it but there's something we can do to make it even better i want you to notice the similarities between this and the letter frequency thing that we wrote yeah and instead of calling it letter frequency let's just call it i mean actually let's leave it as letter frequency that's fine but they're very similar right the code here is very very similar to the code we have here for example we're creating a frequency object we're creating a frequency object we're returning frequency we're returning frequency right we're we have a loop that we go through we have a loop that we go through if it's detected in the frequency incremented by one if it's detected in frequency incremented by one uh if it's not detected set it to one if it's not detected set it to one i mean my gosh there must be something we could do because it looks pretty much like the same exact thing and watch there's something brilliant that we can do so we can actually delete this entire for loop let's get rid of this console log we can delete this let's get rid of this return here let's get rid of this frequency here and the only new thing we need in this is we just need to split it by spaces to break it into words right and let's go const words sorry i keep forgetting the cons sometimes and all we do here okay this is going to be so beautiful is we call our letter frequency function that we created earlier and we just pass it words and then ill know what to do with it so it goes in here and then it has instructions for what we need to do right and all we need to do here is just say return so let's try running this and look at that you guys we are getting the same exact answer and it is completely correct yo is three times what is once lol is two times okay now this is if this is not blowing your mind think about it like this when you go to google right and you type in kanye west and it starts auto completing it's using the same thing because it knows what are the things that are being searched frequently and because it knows their frequency it's showing them here so for example maybe this search over here right this kanye west runway that might be done let's say 10 times kanye west net worth might be done five times kanye west's girlfriend might be searched three times okay it's it's just an object and then google is sorting it and showing you based on the ones that were the most frequent that's the really cool part and this is why you know once you learn this here all you need to do is be able to attach a front end like this and boom your apps are going to be absolutely amazing and mind-blowing so let's try this now okay let's try our own function with different inputs we're going to say const user input is prompt uh write your sentence right and we're going to replace this with whatever the user types in so we're going to say user input here and call this and yeah let's go ahead and run it write your sentence and we're just going to say um kanye west has great music but west side is greater than greater than east side yeah because i live on the west coast in california and then you got all these people in new york and i like that my time gets back i get my three hours back when i come to california instead of losing my three hours when i go to new york so if you're in new york and you want to earn three hours back of your life forever come to california so let's try this yeah and um probably the only thing that occurs twice is west i imagine okay and then let's just write kanye is also the goat the goat that's not gonna work the goat okay let's set the goat and let's hit okay and run it and uh it came back all right and it broke it down and you can see it did a full breakdown of every single word kanye is two west is two houses one great is one music is one but is one side is one is two and the code is two thus two and it did it all so fast and so quick all right so we did a lot of challenges and exercises and if you wanna deep dive and do like a full on boot camp and master arrays and objects then i recommend you deep dive into exercises okay where we'll have additional exercises for you and lance he is going to be your boot camp instructor for those okay so if you want to become a like a legend he's going to help you turn into a black belt my job is to take you from white belt turning into a blue belt he will do that so if you want to for example you know because i'm going to move on to different topics now but if you want to just keep exercising this you can do that you can do that by going into say this exercises folder on the left hand side by the time you're watching this tutorial we'll have bunch of more exercises right now we have this one here that says convert convert hours to seconds you click that okay and then lance has this exercise prepared for you there's a rebel that you'll have to copy okay and paste in your urls and you'll be able to fork this and be able to code it on your own so this is the ripple that lance has created and if you want to be able to do it on your own hit fork at the top okay and then you can actually run this uh repl okay and uh you can write your code here and you could write your solution here okay and what lance will also be doing with some of these exercises he'll also have a link at the top of the loom video where you'll be able to click and watch his video explaining that exercise explaining that project in detail as well okay so think of him as a solid um you know boot camp instructor that just like kind of dives deep in with you all right so that those are going to be the extra resources available to you with that said with that said you're doing amazing pat yourself on the back i mean wow you're doing an excellent job so far by being here you're putting in the work you're putting in the effort i mean you could be doing so many things you could be playing with your kids right now you could be outside hang out with your friends you know you probably got off of work and you're putting in that time to learn this i mean that's huge dedication you're taking time away from all this other stuff you could be doing and doing this and now granted coding is a lot of fun but if you're in this stage i know you're doing it for learning and i tip my proverbial hat to you um huge props for making it this far with that said let's keep on rocking and rolling that was a lot i hope you enjoyed watching that video and learning more about javascript smash the like button if you haven't already make sure to subscribe to the channel because this is a channel where we help you become an incredible developer i love your beautiful face and as always i'll see you in the next video\n"