JavaScript Basics Pt. 11 - Practice


English (Auto-generated)

everyone to Javascript Basics Part 11. So we've covered a lot of concepts so far. I've done some mathematical operators, variables, stuff with arrays and objects. So now we are going to put some of those things into practice. So that's how to put those skills you've learned to the test. So before we go any deeper into javascript, I want you to practice these concepts we've learned. I'll give you the instructions for these small coding challenges we're about to do and if you can't find the answers, we'll do it together. And most importantly this is going to get used to working with functions, which will be very, very important going forward. So it's time for our very first exercise. So in this first exercise, I want you to write a function called my message inside the function, declare these two variables. Name and role for name. You can assign your name or anyone's name and same thing with roll. If you want to assign the world you have now it's your job or any role doesn't matter. Next thing you want to do is print a message to the console that says this, my name is blank and I'm a blank. So you'll replace those blanks with the name and role variables. And for this you want to use string concatenation. So you want to do this over in replica. So go ahead and open up your replicate. If you don't already have it ready to go. If not, you will just want to head to the replica dot com website, select a html CSS and javascript project for front end. Start that up and then just head to the script dot Js file and you will do your exercise there. So again, I want you to give this an honest try if you aren't able to find anything or figure it out on your own, you don't remember the concepts, feel free to google it when you're a web developer, you're going to have access to the internet. It's not like it's not like a no notes test inside the classroom, you can google whatever you need an answer to. So feel free to do that and if you still are struggling with it, just hang tight. We will do it together. So I'm going to take a moment here. I'll have you pause while you're working the exercise and then when you're done or you're ready to see what that answer is, just go ahead and hit play. So let's take a moment here while you work on that exercise. Okay, thank you. Alright, so how do you do hope you did? Well hopefully you were able to accomplish this little exercise on your own. If not, let's go ahead and head to replicate and try it together. So first thing we're going to want to do is write a function called my message. So declare function right there with the function. Keyword name right there. Follow that with the parentheses and then that will be followed with some curly braces. So we want to variables. So I'm going to declare these with let went up. So the first year of what we want is name. So we'll do let name equals and I'm going to use my name. So we've got my name there. Okay. And then we want the second variable to be role. So we'll do what role equals. And I will say web developer gonna put those in strength. Now this is where we want the string concatenation. So in here we'll do a console log that way. We can put the message down here to the council because that is where it's where you go since we're not really running these functions still on a web page. So we'll do the first part of our message here. Let's go and take a look back at what it says. My name is blank and I'm a blink. All right. So we'll start with my name is and something important. Hopefully you remember shrink concatenation does not include the spaces. So or does not automatically give you spaces. So you want to include this on your answer. My name is space. We'll do we'll add that's not the addition operator every year and it is plus name, her name, variable. And then the space again. And I'm a public space for our second variable role and then the last part. Hopefully you remember this period because you want a period at the end of that statement. So let's go ahead and do our function call right down here. So my message with pregnancies on top people hit run my name is Michael and I'm a web developer. Perfect. So this exercise is rather straightforward. Just some shrinking destination with some variables declared. Hopefully you were able to accomplish, accomplish this on your own or find the answer to an online. If not, no worries, we did it right here. Hopefully you've got an understanding of what we did. So I'm gonna go ahead and clear this right here and then clear the console as well. And then let's check out what that second exercise will be. So for the second exercise, I want you to declare a function that has an empty array inside you name the function and array anything you want and to to declare an empty array. It's going to look like this. So let array or whatever you want to name. It equals empty curly braces. You can't see right here because they're close together but empty set of curly braces with nothing itself. Then I want you to have the function, add something to the array. So you want to try and remember what method adds something to the end of an array and finally had the function. Return the array with the new adam inside and you want to use a console, log to get the results. So the same thing as last time we're going to take a moment here to give you a shot to try it out. Give it an honest go see if you can remember the concept. Hope if you have notes, refer back to those. If not google it and try and figure out how you add something to the end of an array. Uh If not again, we'll go over it together, so go ahead and pause here, give us a shot and then hit play when you're done. Thanks. Alright, so how do you do? Hopefully you were able to accomplish this and find the results online if you needed some assistance but not. Let's give this a go on our own. So first thing we wanna do is declare, function has an empty red side and you can name it whatever you want. Let's do function because we'll declare it. It's alright, function there. Ah function destroyed my function. We're gonna get super creative names here. And then for this array, let's name it are Hooray, here we go again. Thank you Right here. And the next thing we wanna do is want to have the function, add something to the array and then finally have the function, return that array with the new item inside. Alright, so for this, I think the best thing to do is to, we'll put this all inside a new variable. So let's call it lit, add something will be the name of my variable. And then so for this people do, we're going to use the push method to add something onto the end of the array? Because that's the method used to add something onto the end. So we will do our since that's the name of our ray are dot push and then inside these parentheses at the end of the push method you can add whatever you want here I'm going to add my name and finally at the end we're going to council dot log something. Since that's when you get the new array with the new item inside and finally again here at the bottom you will do your function call. So right up my function parentheses and had run whoops. Here we go. Let's see why is it uh what's that doing here? You know what here? Let's do turn from stop log. That's what I'm saying. Why is it still just returning the number 1? Mm hmm. What did I do wrong? Let's see here. Uh I promise you have practiced this before and it worked just fine when I did it. Hmm. So I guess let's troubleshoot this together. Let's see. We've got so we've got our empty right here scotch, our next variable. It's called add something which will push the string Michael to the array and then we are having the function, return a console log, it is variable. Yeah, let's see here we are going to figure this out real time. Uh let's see, gosh, is it returning the number? I think it's returning the number of items inside the store. Right script, Which method at 11 another. Right and returns the length of the new hooray okay okay. No. Alright so what I want now is be alright that oh my gosh let's zoom out of there. So what I want to print is the array. Sorry I had this confused with a future function we will be practicing. There we go. There it is. Alright so there is a new array with the item inside. Alright so that is how you will accomplish that problem. So declare the function given name, declare the empty variable inside. And then for this you could just put this inside the console log. I just did this so it's I guess a little cleaner so we'll do declare a new variable and it said will do the name of your raid that push with whatever you want to put inside and instead of returning that new item itself, that new variable because that's just going to tell you the number of items inside the array now because that's what push does we will now ask we will now print to the console the array because now it's something inside so they were I hope that you were able to accomplish this on your own um if not you know maybe review some of the previous concepts and don't be afraid we will continue to go over these problems together because we've got some more coming up so that's it for this lesson again we've got some more problems that we're going to tackle together the next lesson, and I will see you there. Thanks for watching.
23 Views 0 Likes 0 Comments

Let's practice some of the concepts we've learned!

Leave a comment (supports markdown format)