JavaScript Basics Pt. 12 - More Practice
JavaScriptTranscript
English (Auto-generated)
Welcome back everyone to Javascript Basics Part 12. So we are going to do a couple more practice problems to get you a little more used to javascript and functions what So practice is important which is a very obvious statement but it's obvious because it's true, especially when it comes to learning how to code and program. So we are going to continue to do these and we will continue to do more of these whether that it's it's at the end of uh end of another lesson or an entire lesson on its own because we're going to start learning slightly more difficult things as we advance through javascript but I will be there to help you through and hold your hand. We also want to get used to using javascript functions which I mentioned the last lesson are going to be very important and we will be using them far more often going forward. Alright, Let's go. So this is going to be our 3rd exercise first two in the last lesson. So for this one you want to create an array of numbers from 1 to 5, then you will declare a function that returns the first number in that array by itself, you add a console log to your function to see the result and then don't forget to call your function. Alright, so again, same muscles, last time I want you to try this on your own, feel free to google whatever you need to google to help you accomplish this on your own but if you still struggle with it and you need help and you want to know what the answer is. I will help you out with that. Alright so go ahead and pause right here and then I will see you on the other end. Alright so hopefully you were able to accomplish that on your own maybe with a little help from google but if not we will give it a try. So we want to, first thing to do is we'll head over to replicate And then we will create an array of numbers from one through 5. Alright, here we are. Let's do are again. So declaring that variable or declare that array with the variable. Let the chinese consulate. You want action. Oh I can use that 12345. Perfect. Then next lesson their function that returns the first number in that array by itself. Then you add a counselor to your function to see the result. Alright so let's call it. I'm just going to call this my function again. Very easy. Okay I don't feel like I think we have another name but so will declare that function with the function keyword, put the parentheses at the end of the name of the function and then put the colored races after that. And then so inside of here again we want to return the first number in that array by itself. So for that you're going to want to use the shift method. So similar to the last exercise we did. We'll just call it rex you that was called. Let shift equal name of which is our so our that shift. So after we returned the first numbers in that array by itself will add a console log to see the result. So from here we're going to return until that log shift. Then down here we will call the function and hit run That is going to return the # one. So if you want to check it again here, let's make it let's make it seven. Right again And return seven. So the shift method is what is going to pop off the first item in that array and then return it on its own. So hopefully you're able to accomplish this one on your own or with some help from google again. Using some help from google or stack overflow is always perfectly fine because when your web developer, you're gonna have the entire internet to your disposal or at your disposal. All right, let's move on to that 4th exercise. So for this one you will declare a function. That takes two parameters. You can name the parameters whatever you want, maybe A and B or X and y. Then you will use mathematical operators to add these two numbers and then you will have the function. Print out the sum of blank and blank is blank. You'll print that to the council. And so you want to do some string concatenation and some mathematical operators for this. Alright, so again, go ahead and give this a shot. If not we will do this one together. So go ahead and hit pause and then I'll see you on the other end. Alright, how did you do? Hopefully this one wasn't too bad. Especially after the previous exercises we've done. Alright, so let's go ahead and give this one a shot together. So first thing, what do you want to do is declare a function that takes two parameters and you can name them whatever you want. So I'm going to call this function edition and then the parameters inside help axel will be X and Y parameters go inside the parentheses after the function name all that with the curly braces as always. So let's see here, next part is use mathematical operators to add these two numbers. So I think the best thing for that if you want to use we want that result to the inside that string that we're going to print to the console, we'll call it. Would you declare a variable names? I'm gonna name mine some. And then so I will be X plus y. And now this is where we want to from the console log with some of that with that string inside So we wanted to say that some of the blank and blank is blank to the console. So we use some string concatenation for this one. Since I will do the son of space plus Mexico to the string start with the space again. And space plus y plus space is space so And Yeah let's add that period at the end just like that first string concatenation actually as we did last year we want to call our function and the parameters we want inside let's say three and eight. Let's go ahead and hit rough. Let's see I got an error here. What? Yeah, what is it not liking? Oh wait here we go, forgot the final plus sign after some. Here we go. The sum of three and 8 is 11. Perfect. Alright. So see this one wasn't too difficult either. We declared our function past it to parameters of your choosing and then we declared a function if you hear my airfare going off. No you don't. Sorry. Um Then we declare a variable that handles the addition for us and then we finally returned console log with some string concatenation inside. So the sum of the first variable and the second variable is the sum of those two numbers. So it wasn't too difficult I hope. Alright everyone. So that is the end of this section of exercises. And so next up we are going to learn some more things we can do with functions. Uh some more you're going to learn some more logic that you can use with javascript to solve some bigger problems. All right everyone. Thanks for tuning in and I will see you in the next lesson. Have a good day.