 All right. Hello, everyone. If you're first time here, hopefully you've been doing the course already. If not, I highly suggest you go check it out. For those of you, if you're coming back, hopefully you finished Unit 4 or either started Unit 5 or you're there. So let's get started. Hello, hi. I am Susan Hoyt. I live here in Houston, Texas, where it's 80 degrees in December, but you know, I'm a computer science educator for about 25 and on 26 years. I have been with Sailor for the lower year now. I do love teaching coding. It opens up so much to so many people who may have never had a chance to be exposed to it. And I love seeing people who I've taught and stuff coming back and working at Microsoft and doing all these cool things. So I do love to hang out with my dogs, love reading books, and I do like the warm weather by a sand and beach area, though, not in Houston. All right. This is the first in a series of three courses that will cover JavaScript. There's going to be six units in this first course and we are now on Unit 5. So we are on the basics of functions. From last week we covered Unit 4. We talked about the certification. I talk about this every time. How do you get your free certificate? Well, you work through all the course and then you get to that final assessment. You have to get a 70 or above on that assessment in order to receive your certificate. You can retake the exam as many times as you want, but you have to get a week between before you can try again. You can keep trying and keep trying and hopefully as you go through this and you do the end of the unit assessment, that should help you prepare for this exam. We also have been discussing using Code Runner because that is how a lot of the assessment exam questions are in the units here. So I will show that again today also. We did use a linter last week. Okay, I just hand to find some of our syntax errors. So that was something different that we did. Sorry, I got the mouse that likes to go crazy when I touch it. Okay. So what other topics did we actually cover? We talked about conditional statements, if, if else, switch case, while, do, while, for loops, relational, Boolean operators. And we looked at some code that did interactive choices based on our conditions. So we're going to do this unit. Well, first of all, this unit isn't a huge unit, but it should take you approximately two hours to complete. When you successfully complete this, you should be able to identify types of functions, use functions with arguments, compare type of function declarations, and write code that will call a function. So, in my mouse. So we'll start with a video that introduces you to functions. That's what 5.1 begins with immediately. Basically, what is a function? It's, it's a set of code that will help eliminate the need to write the same code repeatedly. So if you have something that you're going to keep using over and over again, maybe not repetitively, but you just need it. Maybe at step one, and then you need to do some other things. And then at step 25, you need to use that same thing again. You would use a function, and a function can kind of module modularize your code. So it's easier to modify easier to read I can see what you're doing and you don't have to just keep copying and pasting that same code where you end up with 2000 lines of code, when maybe it could be 500. So functions has two steps and you have to have those steps or they won't work. First, you have to define your function, you have to put your set of actions together, give it a meaningful name, and then the second step is call the function. So once you define it that sets it there and gets it ready to go but if you don't call it, it will never happen. So in the video where they talk about functions. I just grabbed a screenshot right here from it. And basically they say I have a function. His name is my function. And I put these parentheses the parentheses are really important in functions because that's us, let's us know they are a function. Then I have a curly brace to start and a curly brace to it. This is defining my function. Below it is calling my function. So this is a call to that function. Do you have to have them in order like this. No you don't. You could write all your functions at some point have them all the top if you wanted, then way down at the bottom is where you could have all your calls to them and set up your code however you like it and or however someone says that they need you to do it in your office or So again you declare it with this keyword function so there's our keyword function. Then you say your name and this is my function is what I called it so that is my meaningful name for it. Then you put all your commands that you want to do inside here. This looks almost like a scratch kind of program where we're doing dragging and dropping and stuff and it's a very good video I highly suggest you watch it. Some famous people who code and some people who from Microsoft it's a it's a good video. Okay, so then after we created again we call it by saying its name so I say the function name, and then I put parentheses again I can't forget the parentheses because I forget parentheses it's going to have some problems. If you don't ever call the function, it won't execute. So I have tons of examples to show you so bear with me. So it looks a little different. This is one way that we can define a function. We can define it as a constant co nst so that means what constant does is it means, hey you can never change this. So that means if somewhere along the way I was writing and I thought I forgot to write my function called square and I tried to write it again for whatever reason, it would tell me squares already been defined, you can't redefine it. So this is a way to just make sure that it could never be changed as to what it does, and it's just another way that we can define functions and JavaScript. So in this case we use the word const, which is a keyword. This case we named our function. This is nothing. Do you see it just says function x that this is not our function name. This is square and square is equal to our function. So our function doesn't actually have a name like we just saw here, we said function my function. This functions value is set to be square. So square will get whatever function returns. Because this is our declarations is where we declared everything. Then as we come down here console dot log. So we're going to print something out. We say the name square, and we pass it 12. Now when we pass it 12. It comes up here and square 12 goes there. And so now when 12 comes in here it's going to return 12 times 12, which is 144. So this function does have a parameter. So you notice here we've got something in between those parentheses. And over here we did not. So we're going to see a whole bunch of different examples that have parameters, they may have more than one, they may have none. So whenever you do have a parameter, we must send it a value. There are some examples I'll show you where you may have accidentally sent too many you may have forgotten to send it, and I'll show you what happens when you do that. Alright, let me see where I'm at. Okay, so what I wanted to show you. Whoops, let me go back was let me grab this and notice I'm in replete again. So I'm in replete and while I'm in it, I'm going to already be logged in. So I'm going to create something. And when I create something I like to use Node.js when I'm doing this. Again, it shows some weird name here and I don't care at this point. If you're going to try to organize all these examples I definitely suggest that you go ahead and give it some names. That way you know where they are and you could go back and look at them. Alright. Oh, I did that again. I did it in the wrong spot. Hold on. So right here is what we had just looked at. So I'm going to grab this one real fast. And I'm going to bring it over here. Alright, so this is what we had. And so if I run this. It says 144 because I printed out the result of square and I sent square 12. If I sent it something else like 10, then the value changes right here. If I didn't do this, let's say I did not, I forgot to call it. I wrote it all and everything like that, but I forgot to call it. So now if I run it, nothing happens. So I've made it and I have it, but if I don't call it that means it never gets executed. So let's go back over here now. And now what I really want to show you is something that I did not show you in unit three because we were pressed for time. And so I want to show you the interactive references that we have in this course. If we go back to unit three, there's two places that you can run examples and use as a reference. If you forgot the syntax. Another example, I just confused you by showing you something and you're like, okay, I'm going to have to go recheck that out. Then you choose a topic. It'll say the definition you hit the next page and you'll choose try it. So let's do one of these right now. Let's go to 3.4. And I'm going to show you where to go. So if I come here. I go to three and I'm in the course right here. So if I go to three, and I go down here to 3.4 and I'm going to do that one first 3.4. And I see what is a statement. That's not what I want. See right here I see JavaScript reserved keywords. So if I grab that notice it shows me a whole bunch of reserved keywords. So it's got a whole bunch of stuff and you notice when I mouse over them they all look like they have a link and they do. So let's go to function. Again, let's say I'm doing this. I forgot how to use a function. I forgot how to use a break or anything like that. I come here and I go, okay, I need to look at a function again. So I click on it. When I click on function, it gives me its definition. The function keyword can be used to define a function. Great. So I go next page. And it says try it. So I click here to try an example. And again, we're going to use code runner. So if I click here. It opens up and I did one already. So I'm going to go continue my attempt. Give it just a second to load. All right, so then it kind of tells me a little bit more about function. It tells me that parameters are optional. And it gives me a whole bunch of other little definitions and stuff. And then it says, hey, here's an example for you. So this is a small example. This anonymous function. So this is like the one we did that anonymous. It doesn't have an in here, but it's associated with this constant get rectangle area. It calculates the area of a rectangle using two parameters. See how there's two things in here now a width and a height. So it says select your check button to try the example. So I go check. And it tells me what my, my expected output is, which is 12. And notice, it says my input was three and four. That's what I sent it. Then it says that it's expected to return 12, and it did return 12. Now let's say you want to play around with this. Well, things you could do is you could grab this, and you can go throw it into replete, and you can change the values and change stuff around. You can even change it here. It'll just tell you at the bottom here, it'll just say, hey, that's not the value I expected, because it's still expecting 12. But if you could change it, you could decide to send it four and four. And then you do check. And so now it says, okay, that's not, that's not, that's the input you did. No, that's not the input. It says, okay, that's not it. And it tells you this is what my output would be. So it look a little weird if you don't have something you want to copy and paste it into, but you can still mess around with it inside here. And then you can always go reset answer. And it'll reset it. So let's go back. And so this was again. In unit three, and it was 3.4 intro to JavaScript, JavaScript statements. And it was underneath reserved words. Now there's also a second place where you can have this. So this is 3.4 reserve keywords. There's also one up here in 3.3 operators. So if I go to expressions and operators reference on this one, then the interesting thing about this is that it says it's going to document all this stuff. So I decided I don't remember how the bullion and works, I forgotten, are they both true or they both false, I forgot. So look at all these words I can go through. I can go through all this. Okay, well I don't see what I want there. So now I'm going to come over here. I've got these operators I could check into. And that's not what I'm looking for. So I keep going. Okay, here's some more. Still don't see what I'm looking for. I keep going. Okay, here's some plus minus. Okay, that's not what I want though. Let me keep going. Now here I've got my addition, subtraction, all this. Here's my modulus, the remainder. I might forget about that and may want to check it. My exponent still going because I want that and Okay, we've got our less than our greater than less than or equal to greater than or equal to. I have my quality we have the quality of equal equal not equal our triple equal and are not and then to equals I'm still going. We have bitwise stuff which we haven't talked about but maybe you're doing something that you want to come back here and look at that. Okay, now this is why I have to be careful. This has bitwise and this is not the one we want to look for if we're looking for a Boolean one. Okay, so we want to keep going. Ah, there it is. There's my logical and see how it has a two ampersand. So if I click on it, again, it's going to open up. And it tells me a little bit about it. And it tells me it'll be true if and only if all the operands are true otherwise it's false. Maybe that's all I needed to know. If not, I go next page because I decided I want to play around with this and I go click here. Once I go click here, I'm going to continue my attempt because I tried it before. Now I have right here an example. And again, I can check it. Now this one, it says expected false got true. So let's reset my answer because I was playing around with it. All right, now let's check it again. Okay, now we're good. Whatever changes I made I fixed. So I expected false and I got false. A greater than zero. That is true. A greater than zero. That is false because negative two is not greater than zero. So if I wanted to play around with it and make it true, I could make both of those statements true, which is what I was playing around with earlier. And now I check it again, I got what I wanted to get, but it's still going to tell me I was wrong, but that's okay. If you don't like to mess around with this because it's telling you that it's not the output it expected, you can always copy paste this and throw it into replica. All right. That's what I just wanted to touch on because this is a really good reference for you to go to on anything you might not remember, or you might just want to learn more about. Okay, back onto functions. All right. So this example right here, which let me grab this. And I'll make it big again. So this example shows functions that return values on ones that do not, and it also shows functions with no parameters and one with two parameters. So I'm going to grab this example. I'm going to throw it back into roughly and we're going to look at it together. And it is right here. So let me grab it. So right now, I see that I made a constant make noise. It's an anonymous function. And it all it does is print out plain. So notice there's no return here. There's also nothing inside the parentheses. So that means it doesn't have any parameters. So that means when I want to call this function or make this function execute, I say the name parentheses, but this one has nothing I need to send it so I just have the parentheses like that. So if I run it, it's just going to output plain. And we're going to run it in a minute. So here's the one down here that's called power. It is a function that's going to take in two things, a base and an exponent. So I have result is one, I even have a loop here and going through this, I'm going to return the power. So here I send it power by saying power. And I send it to method. I'm two methods. I'm sorry, two parameters to which goes and matches up with base and 10, which matches up with expo. It executes this, and then it will return, see how this one has a return, it will return the results where it came from. And this one, I'm just saying the name and having it go there because it actually prints it out itself. And this one, I'm saying to print out, and then I'm actually calling it, because it returns something so whatever it returns goes back to where I called it from. So when it returns the result it goes right back here, and then it prints it. So let me run this real fast. And then the clean comes from us saying make noise. And then this down here comes from us calling power. Now watch this. I don't call my method. Or my function, sorry. Notice it didn't happen. It only did the one zero two four down here. So if I don't call a method, even though I wrote it, it will not execute. I don't have to have it right next to it. What if I decided I wanted to do it after this one. And I decided I wanted to say the name of the function parentheses. Now, I wrote this one great that doesn't mean anything that has nothing to do with the order, and I wrote this one still doesn't have to do with the order. The order is where these statements are. So the first thing it's going to do is my console dot log power two comma 10, then it's going to do make noise. So watch. So we've got the 102 for now first because it did this line. And then after that it came down here and it did the point, because it called make noise, which did point. All right. Let's continue on. So we're looking at this. You hear bindings and scope and you hear stuff like that. So I'm going to try to touch on it briefly and then it goes into it in some more detail in some of the text in there, and it's very important to understand what's the difference between a local and a global scope so this is kind of a quick introduction to it. Notice over here I said let X is 10. And inside here I also use bar. So when I declare something as let or constant that makes them local to the block they're declared it. So what does that mean. Well this let is going to be known through here. This let would only be known from that curly brace to that one. And this one will be known throughout the whole block that they're. So if they're inside curly braces, that's where they're going to be known. If they're not inside curly braces, they're going to be known inside that pot. If you use bar, those are visible everywhere. So even though I put bar Z in here. So the Z is known everywhere. It's not just known from this curly brace to this one. But if you notice why here. I don't have another why anywhere outside of those curly braces that I defined. So why is only known from this curly brace to this one. So I'm going to show you a couple examples here in a minute I'm going to grab both of these. I'm going to try to reference why outside of here it's going to give me an error, because since I use let that makes it local. Okay, when I use bar it's available to everyone. So sometimes you can get into trouble using bar, because you can accidentally change something way down the way and not realize that you changed it. So this one and kind of see how this in works in here because notice I have an in here that would be known through this block and then this in that is my parameter is only going to be known inside here. So they're two different ends, even though they have the same variable name. All right, so let's go to that. So let's do this one first and talk about that why and how it gets weird. Okay, so you notice here I said let X equals 10. So since I used a lot it's not global, but it will be known from here all the way down to there. Now inside this if I have this curly brace and that one. So that means when I declare things inside here with a lot they're only known from here to there. So I did a bar Z and sit in here. When I do bar it's global what does that mean it means no matter where you stick it, everybody can use it. So, Z is 30 here, I could take the outside of here and change it if I wanted to. So let's play around with this for a second. So I'm going to run it first and we're going to talk about what the output is. So this says let X is 10. If true means it's always going to happen. So X is 10, then I let Y be 20, and I have Z as a bar set equal to 30. So, if I add those three together I get 60. So, when I come down here, and I print, I print X plus Z. So X is now 10 and Z is what 30 because I haven't changed anyone so this right here is where that 40 comes from. Okay, now what if I said oh I wanted to do X plus Y. So then I decide to do X plus Y. And then it tells me that Y is not defined. And why is not defined because I said let Y inside here. So when I put it inside there again I can only use Y from there to there. So, I said my Z is 30 inside here. Now, as I'm out here, you notice I could use that Z. Now what if I accidentally decided to change the out here. And then I'm going to do C again. And let's see if I changed its value outside of that. Oh, sorry, I forgot to take off the light. It's that. So, it was still my 60 and 40 from the original thing right here I've changed Z to 50, even though I did Z between here, because I said bar, I can use it outside here and I can change it. I can use bar and sometimes get you in trouble, because you accidentally changed something you didn't mean to change, because you forgot about it. So, always be careful and you should try to avoid using bar because of that. Now if we wanted to do something else, let's look at what if I did this, let's do 100 to make it an easy number. I've got a lead X is 10 here, and a lead X is 100 there. So let's see what happens when I run this. Alright, so I came in here X was 10, then I came in here and said no X is 100. And when I said that it said to do wise 20 Z is 30 so all those together added together are now 150 makes sense. So down down here, I try to add X plus Z X plus Z is 40 and why is that because I declared another X inside here, who's only known from here to there. So when I am inside here, my X is 100. When I'm outside here guess what X is 10. Okay. Alright, let me grab that other example. Hold on. Hold on. I forgot which example I was going to show you the other one. It's the have one. Okay, so let me grab this again because I think I closed it. I was right there. I think I was just kidding. Okay, let's grab this one now and do kind of the same thing really fast. Okay, so now we have constant have this function. It's got one parameter. It's going to return that parameter divided by two. So down here I said to let in equals 10. So that means when I call have, and I call it with 100. Then it should do 50. Let's see what happens. All right, so what happened here. So in was 10. And it came up here whenever I did whatever I did this it came up here but it didn't come up here as that 10. I sent what to it. 100. So when I send 100 it like when I did this 10 it doesn't just mean in appears 10. When I call the function it takes 100 passes it to 10. Now in this is 100. 100 divided by two is 50. So it returns 50 back where it called it from so it returns 50 right here. So that is where the 50 came from. Then I say, okay now print out in. Well in is still 10 because I didn't change it. All I did was use 10 as a parameter use in as a parameter here. So let's try something inside here real fast because I told you this in is different than that. So let's see if you believe. Okay, so let's do inside here. You really wouldn't do this but I just want to show you. So I send it 100. Great, I send it 100 and then right after that I go changes to 500 I don't want it to be a hundred. So it should actually return to 50. So let's see if that happens. Okay it did. But right here do you see I said in is 500. What did it print out for it. Okay, so let's see if it returns to 50 because this in right here is different than that in this in only has scope right here inside this function. So if I change in here, it's not going to affect it outside here. So my end still stayed as 10. All right, let's keep going. All right, the call stack. That's what the computer uses to make sure it knows where it is, especially when it has a lot of function calls, it's got to keep track of it. So if you look at this example, this is called function greet. It has one parameter called who, and inside it, it just print something out so you notice it's not a return method, or return function, I'm sorry, I teach Java to so it's a return, but it's not a return function because it doesn't have a return here. So all it does is console dot log. Hello, and then it prints out the name of whoever you serve. So, in order to call this function, I say the function name Greek, I send it a name. So in this case I sent it here. When Harry goes up here, and when Harry gets there, it comes inside the method or the function, and it prints it. It prints Hello, Harry. Then when it finishes, it gets right here. It goes back to the next statement after where it was. So that means it goes now down to here. So let's talk about this over here. So it's not in the function as I come through here, we aren't into the function we're right in front of the G. So now it says call Greek and send Harry. So now we're going to go into Greek. So we come up here and we go into Greek. After we get into Greek we start. Now we're into console dot log. We go ahead and do what that says. And now we're still in Greek. But now we're going to finish and we're no longer in the function. So we go back. This is where I had finished. Now I go over here and now I'm in console dot log. And I'm not in the function anymore. All I'm going to do is print by every time a function is called the current context is stored on the stack when a function returns or completes it removes the top context from the stack, think a stack of plates, whatever you put on top is the first thing that comes out. So be careful with your personal function calls so you don't run out of stack space. So let's do this one real fast now. Recursion is when a function calls itself. And in this case we have two functions that keep calling each other. And they don't have any way to stop. So we will end up with an issue and I will talk about that when I get over here. Okay, so a function called chicken, no parameters, it returns and calls a. So that means it goes to a bit a has no parameters, and it returns now wait it calls chicken. So now it goes to chicken, who then goes to a, who then goes to chicken. So this is a recursive call between two functions. So if we did not have this statement right here it wouldn't be an issue because it would never execute. So if I didn't have a call to one of them. It wouldn't be an issue. Nothing happens. So if I go ahead and call chicken. Then, let's see what happens. Notice it's referencing a stack, it says that the maximum call stack size exceeded. So I am in an infinite recursion right here. Now, do you think it would have changed if I would have called a first. Absolutely not it's not going to change because a calls chicken, and does the same exact thing. So, whenever you have functions pulling each other you have to be careful. Another way that you can have issues is if the function, if you accidentally had a function call itself. So, if a function keeps calling itself and that's going to be another place where you're going to get stuck in infinite recursion. Okay, what about the number of parameters matching up with your function call. So if you pass too many, and this is where JavaScript is very unique in this. If you pass too many, it ignores them. If you pass too few, the missing one get get assigned the value undefined. So, that's a con because you can pass the wrong amount and not be notified. And so you could be making a mistake and you can't figure out what's happening just because you accidentally sent too many or not enough. The pro of it is that you can call a function with different numbers of parameters so let's look at an example of this. So let's grab this guy real fast. So I have this function called square square has one parameter. And here's its beginning and here it's at I know this is kind of a weird way to write the function because it's all online. It doesn't happen. Yeah, it does. Can we make it look better. Sure, we can do this. And then we can put this on another line and make it look a lot better. Now, I'm going to since it's a return method or return function. I am going to want to print it out. So I said console dot log. Now I'm going to call square. I just told you that square takes in one parameter. How many did I give up three. I gave it a four I gave it a true and I gave it a hedgehog. I just said I don't even care just call it with whatever. Now, with some other languages you're going to have it screaming at you, but in job in JavaScript they're like, Okay, well I guess you meant the four. So we'll go ahead and take the four and return 16 to you. So what if I made them all numbers how would it pick. How does it pick which number that it decides to keep. Well, let's see. So it's still kept the four. Okay, what if I did something like this. And this. Let's do for like this. Now you know what let's keep it. Let's do that. What happens with this. I suggest anytime you're learning new stuff that you just sit here and you play around with it you say okay well what happens if I did that what happens if I do this. Now, it did it's weird conversion here it took that for as a string and it said oh you need that to be a number let me fix that for you. Now what if I would have had it as this. Now, it couldn't figure out that I could use that seven maybe it just said it's not a number. So it made it undefined because it was thrown off. So you can see we're sending too many or let's not send it up let's not send one at all. Since JavaScript just kind of fixes it for you sometimes you can run into problems. So I didn't send anything so that means it said x was undefined. So it was undefined times undefined is not a number. So that's where that came from. All right so I got a couple more examples I want to show you this one is going to show you what happens if. Let's just grab it for just so I can explain. All right, let's go back and I bet it's on the previous page sorry about that. Too many copy and paste for me. All right so let's do this minus one. All right, so let's look at it function minus so our functions name is minus it takes into parameters a and b. And this if says if B is undefined which means I only send it one parameter like this one. Then it says just return the negative of that number. Otherwise return a minus B. So this has an if here to go ahead and catch it if I don't send it that way I can just have one minus function that can work as a unary operator also. So if I run this 10 came in 10 came in as a because I matched those up, because it's a return function remember I'm going to try to print it out so it's going to return back to where I called it. So 10 comes in here be is definitely undefined back here so I'm getting some message in the chat that can't be heard so hold on just one second. I, I'm sorry in the chat I'm checking right now I can I can hear it on my YouTube and so maybe try refreshing your page. But I can hear it on on another on another one so hopefully that fixes but I think we have it if anyone else is having trouble please let me know, but we can continue sorry about that. Alright, so we caught that B was undefined because we sent 10 we just sent it one. So a got 10 B did not get anything so it was undefined. So I return the opposite of a, which means I'm going to return negative 10 to right here. Once I return from a function I don't come back. So when you see this return I'm out of there. So then I also can send it to so I can send it 10 and five is 10 B is five. And so he's not undefined because it has a value five and so I will return 10 minus five and I'll return that result right back to where I called it. So five will come back here and I will print it. Alright, so let's look at that other one, which is here. This shows another unique way that you can do functions in JavaScript is you can actually set a default. For one of your parameters. So in this case, this function is called power. So this power is what let's run it. And it does not have function power. Let's do that power. And let's make sure everything else looks okay. Let's run that. Alright, so my function power has base. And if I don't get an exponent, I'm going to go ahead and default to two. And another way that you can have a way to catch it in case you only send one. So if I send you four, and I don't send you that second parameter, it's going to default to two. So this goes ahead and calculates the exponent so four to the second would be 16. If I do send the power, then it's no longer two. So if I send an exponent becomes the second parameter, which in this case is six. And so it becomes 64. So this is just another way that you can catch errors and hopefully avoid them by doing something like that. All right. All right, so let's go here. And these are the two we just talked about. So those are the two examples we discussed. Let's go on one more. Let's talk about declaring functions. These are going to be some of the type of functions that you will see and that you have seen. So we did our constant one first with our function, we don't put a name here. This is f that's going to hold our functions value. We can have parameters, we cannot have parameters, we can have more than one whatever we want to do. So this function is not a return function because it doesn't have the word return. It is simply going to print something out for us. Now the second one is that I am having a function G. It has two parameters, and it is a return. So it is going to return this value to wherever I called it from. The first one is the tricky kind of function which I will show you the steps to do. Now you may see this in some code that you look at online. I always joke with my students that a lot of times I think coding people like to make their stuff look as weird as possible so that people can't steal their code. I don't know, I don't have an excuse for that, but we will talk about this for a second. It's not really that great to use just because it's really hard. I can't look at this really fast, especially if I wasn't a JavaScript person and know exactly what it was doing. So let's move on. Okay, so when I declare functions, function declarations and JavaScript are hoisted to the top of the enclosing function or the global scope. So you can use the function before you declare it. So the difference between these two is that when I write a function, I don't have to have it declared before I use it. Now, when I use it as a var or as a constant for something like that, then I can't use it or call it until it has been declared. So this would have to be under here. So let me show you that really fast. And that just all depends on how you're sending up your code, how you want to have your program function. So let's get up here. Let's do this one. Not that one. Let's do this one. I think it's less confusing. So I have a function called hoisted. It has no parameters. And it is going to simply print out the word foo. So I have it declared here and I have determined what it is. Usually I declare variable then I can use it. In this case it says that I don't have to declare it first that it has global scope and so I can say it wherever I want. So let's look at that. It worked fine. Now, a lot of times you might see you may see coding where they put all their function calls and all their stuff here. And then at the very bottom of their code, the hub actual function declarations, because you may not need to know information hiding you may not need to know at all, what this does, you just may need to be able to use it. So you may not want it up here at the top of your code. You may want it all at the bottom. That way you don't have to worry about it. But if you decided that you wanted to actually make it a constant or a bar or something like that, then you can't make your code look like that because it won't work. So now I didn't name it a function. I said it's a bar. I did the constants a minute ago to so if I run it. It told me, I don't know what you're talking about because right here, I tried to call not hoisted. And it said, there's nothing above me called that so I don't know what you're talking about. So, when you are using a function this way you have to keep it in mind that you must declare it first. Now watch, let's move it down here and let's see if it works. I'm not saying it should. So, comment that out. And it works fine. So when you don't do it with the keyword function. And the other way that we just showed you, then you have to have it declared before you call it. All right. And this is what we just did. So this is besides the bar one this is the one that we did earlier. And again, this one would also have to be declared before I could call it. Otherwise, I will get an error saying I don't know what you're talking about. So now we're going to talk about this error function thing which I told you looks weird. So it is a compact alternative to traditional function expressions it's limited and it can't be used in all situations. So let's look at this. Let's say I had a function a that just returned a plus 100. So I had this one to start with. Let's break it down to what the error function would do. Step one, I remove the word function. So I took out the word function and now I just have my a. Now I place my arrow between the argument and the opening body bracket. So notice my arrow and you have to be aware of this arrow. It isn't greater than or equal to. It's equal greater. It actually looks like an arrow. And so I've still got my curly braces. The only thing I've done right now is I took off the word function, and I put this arrow in between those two. And then I removed the body braces. Now I'm going to get rid of my curly braces. And then I'm going to leave the word return. Okay. But then I said no I don't even need that. So I take it out. So now I've gotten rid of 123 more things. So now I just have this. Now I don't even need the parentheses around it. I just have the letter a, which implies a plus 100. And this is actually an arrow function. So if you saw this as you're going through someone's code you might quickly go Oh is a variable and I just added 100 to it. And then you may not realize, oh wait, that was a function. So if you see this weird arrow right here, remember that is actually a function, and it's a function that looks like this that's been brought down to something like that. Do I use that much. No, I don't. But I do know that when you go online and you start to look at some code and you start to look at examples, you may run into this so it's always good to know that you might see that. You might get into this example in action. So it has a nice video, and it has some stuff to have you write your name and it talks about a couple of these different functions so I'm going to grab that I'm going to put it over here somewhere. So I'm going to go back to my code and we're going to play around with this for a second. And again, I want to remind you that any example in action. You should go ahead, work through it, get your stuff and work exactly the same thing they do. Because there's nothing better learning than working along with them. All right. So I have my function not as a function declaration but as this bar. So that means I can't use it until I have written it. So it's written here. So now I can use it under here. All right, so let's look at this I have called right my name three times. So in this case, I think it should write Thomas three times. All right. So now I have right a name, which is a function and now look what it takes a parameter called name. And so I ran it. Why didn't it work. Why didn't know where if I keep running it still doesn't work. Why is that well because I haven't called it. I call it by saying isn't the method functions name right a name. That's my function name. parentheses, does it have something inside it that it needs. Yes it does. So I'm just going to send it a name. And then let's see if it works. It did because I sent it that now. So after that I decided I wanted to do the other one again. Now I don't have to put anything in there because right my name does not have any parameters. So I had it do that one again and now it wrote it. So as you're working through and going through this example and action with him he goes through a whole bunch of nice steps we're talking about. What if I do this what if I do that and I highly suggest that you work through those with him because it's a great example. All right. So practice review, then the practice review is great because all these are preparing you for that end of unit and end of course test. The practice review is using functions with conditionals. It'll calculate the speed. So let me get it because I have the code right here. Hopefully I didn't mess up the code when I copied it. And again if you mess up the code when you copy it, you just go right back and copy it again, because it'll all copy. Okay. I'm going to run it real fast. Holy cow what happened. Well, I was in what here. I was not in an HTML type thing was I and this is definitely HTML. So guess what I need to do. I'm going to have to make a new replete, which can I go here. Yeah. So I'm going to go create. That was definitely not. No JS stuff. It's what HTML CSS JS. So now I need to go to that. All right, let's try that again. Hold on give it a second. This definitely looks better. This looks like more of the stuff I needed because I needed some HTML stuff. So always be careful when we're going between things like that to make sure you're in the right type of editor. So if I run this. All right, I think I got the wrong one. Let's make sure I grab the right one. And I did not hold on. Let's try again. All right, so let's run it and see what happens first. Oh, it asked me to do something. It says enter the distance or negative one to end. All right, so I'm going to do 100 for distance because let's pretend I have no idea what I'm doing. And let's say for me to go 100 miles, it took me. I don't know 60 minutes. So then this gives me some kind of answer and I'm going to say okay and I'm going to try to look at it. Well it keeps asking me for this. So I'm going to hit because it tells me right here to hit negative one to end. So I'm going to hit negative one to end it so we can look at the code together. All right. So notice we have a function. Notice we're in the script part here, right. And we have a function called speed. So let's say we're in two parameters distance and time. It will return your distance divided by your time. So what did we do down here, we said to let D be the prompt of enter the distance and negative one to end. If you weren't here last week, we talked about while loops that had some kind of Sentinel number that would make them stop. And negative one is our Sentinel and we'll say while D is not equal to that negative one, then we'll go ahead and let them enter whatever they need to do. Now notice I have to ask here for them to enter a negative one to end. And I have to ask it again inside here. I have to give them a chance to enter something for that loop control variable before I started. And in the loop, I have to be able to change that loop control variable. Otherwise, I get stuck in an infinite loop. So if I run it again, let's look at it. So it says enter the distance. Okay, let's do 287. And then let's say it took me 90 minutes. Okay, so then it gives me my result. It prints it out. And now it says hey, do you want to do this again. And so if I say yes. I do another one if I want to stop I do negative one and I use that Sentinel number to go ahead and get rid of. All right. Let's get into the unit five assessment. So as you get into the unit five assessment, let's go over here and let me close some of these and let's get to it. All right, so I'm in unit five, let's go to it again. So we've gone through all of this stuff. Don't forget example and actions and practice reviews are very important to help you get ready for these assessments. So let's go look at the assessment. This assessment, I'm going to go ahead and say continue my attempt. And I'm going to go over to one. Actually I should have done a new preview. Let's just do that. Okay, so if you notice here we've got a matching fill in the blank ish come down multiple choice true or false we've got all kinds of different types of questions that's one through eight. Then we go next page and we get to nine. Nine says that we are going to test a function using a default value. So as a developer you'll test your functions using different inputs. So write a function called borrow that has two parameters, and it will return the amount times rate. The function should use a default value so remember that default value we used an equal. Okay, I'm going to do this. I'm going to say I need a function. And its name is borrow. And I'm going to send it to those. And I know that I'm going to put my curly braces. And I'm going to return amount times rate. Okay, so I said all that because they just told me all that right. Now, your job, because I'm not going to give you the answer on this I want you to definitely know is that you should now find a default value and be able to set our rate to a default value of 3.0. So that means you're going to add something here. And after you're done adding something there you will check it. And now I don't know what happened because I haven't finished the test yet so I'm not sure was I right or wrong because it didn't tell me to print anything out did it. So I have no idea so I'm going to wait, and I'm just going to say, maybe I'm right. I'm going to move to next page. Could I do console log and check it sure I could do that too. Now we're on this one. We're going to write code to call the can play internal JavaScript function to see if a user registered to play a game. So first of all, always read all the stuff. You have no CSS and no JS code so that means you're only going to be working right here in this window encoder. So you're only going to be working in here. So that means I'm going to make it big so I can put stuff in there. Write your code after the comments. Now remember I told you to watch out right here. It's hard to see these lets and these functions sometimes that light blue is really light. So always check make sure it's there. If you're writing stuff. Then it tells us assign true to register and call the function so I'm going to do one little step with you. So I'm going to assign true to register I do not have to say let again because I already said, let register. So I'm just going to take registered. And I'm going to assign it to true. The next step it tells me to do is call the function. Okay, what is the function name. Well come down here. Notice it says don't put any code in this area. So don't put anything in here because if you do you're going to mess it up and it's going to say you're wrong. So in here, I would say the name of the method or the function. So sorry keep saying method. And what do I need to send it. I need to send it registered. Okay, then it tells me to go ahead and do the false here. And then when I'm done. I can see immediately that it printed out a yes. So that's a good thing. Okay, I could check it right now. And it tells me hey something's not right with your stuff so I can check that. And then I could be like oh let me fix that then before I go on and I can finish which you can tell I didn't finish it I didn't do this part. So then once I finish all that and I say okay well done. I, I'm going to finish it. Once I finish, I submit it all. Then I go through. And when I do miss stuff if you notice it tells me hey this is what you're doing this is how things need to be. And it'll even give me some help here and tell me what it should look like. You can always use that to help and then reset it. I highly suggest that you try to not just sit there and copy that code because you might see this stuff in your final assessment so you can get that certificate. All right. You have gone through a lot of stuff today. So, next week, we have a ton of stuff to go over to because next week's episode has almost four hours of stuff in it. And next week we'll be talking about the DOM objects and some best practices for JavaScript. So you'll identify some properties of objects. You'll explain the tree of the document object model, create code that uses objects, model some best practices, explain what is exception handling and develop some real world examples using JavaScript. And the next session is December 13 at 530 Eastern, and we will cover units six. Thank you so much for coming. I hope this helped you some with our unit five, and I hope to see you next week. And let me just say for everyone that was watching. Thank you, Susan for taking us through this and thank you everyone for joining us and as as was just said, we'll be back next week for the final unit unit six. Again, the links to all the previous videos can be found down in the description of this video, as well as a link to the full course, which you can take at say.org. But thank you very much everyone for joining us and we'll see you next week. Goodbye.