Transcript for:

in this massive course you will go from having no coding experience to having the essential skills of a front-end developer you will learn various front-end technologies and build a few projects along the way zach golwitzer teaches this course zack is an experienced software engineer and full stack developer hey everyone my name is zach and today in this 20 plus hour course i'm going to take you through the core foundations of front-end web development now before i get into what that actually entails i wanted to spend a minute to thank free codecamp for having me on the channel it's always a privilege to share my content with such a broad audience reach so many different people and i genuinely hope that you find this course helpful if you do be sure to give them a big thumbs up and a subscribe they're an awesome channel contributed to a lot of my early learnings as a web developer so without further ado let's get into it the first thing that i wanted to start off with is some background context because a lot of the content within this course was obviously not filmed all at once it's quite long it's something that i worked on over the course of a year pieced it together lesson by lesson and a lot of the content is on my youtube channel just separated out into different videos i wanted to compile it all together as it does follow a very logical sequence from start to end and it's perfect for the complete beginner so this should take you from never having written a single line of code in your life to having at least a solid grasp of what coding languages are particularly javascript and what is web development particularly front-end web development you should be able to build basic web pages and understand basic fundamentals of coding after this course is complete while this course is mainly geared for the complete beginner some of the later chapters of it where we get into css flexbox and stuff like that could actually be more intermediate level and be useful to anyone so be sure to look through the chapters below and you can skip around to what interests you so basically the structure here is the three main tenets of front-end web development and those would be html css and javascript now oftentimes you'll see tutorials kind of start with html and css and then move into the more complicated more challenging javascript i've actually reversed that a little bit we will be starting with javascript which is going to be the biggest section and i think it's useful to do that because number one if you're going to be a front-end web developer you have to know all three anyways and also i think there's a lot of concepts uh that you'll pick up learning a general programming language like javascript that will kind of help you catch on to the html and css and bring it full picture for you my goal with this course is to be the course that i needed when i was starting out and what i'm really trying to do here is connect all of these concepts together html css and javascript are not born in isolation they all work together to build a web page and i'll talk about this analogy a little bit later but you can think of these three core tenants of front-end web development just like you would building a house so when you build a house you've got the you know the framework the structure which is the concrete foundation all of the two by fours that go up and the roof that would be your html then you get in there you put some wiring in there you put some plumbing all the other stuff that would be your javascript which makes it actually functional and then finally you put the finish on you put you know the roof tiles you put paint on the outside the inside you just make it look good and that will be the css if you walk away with anything from this course i want you to understand most importantly the relationships between these three and have a pretty firm grasp of each of them in isolation as well the last thing before i actually get started is to let you know that in the video description there's going to be a link and that link is going to lead you to a page that gives you all the resources that you need for this course we'll have some time stamps some chapters so you can navigate around the course and then i will leave code snippets solutions to any of the challenges and even some additional resources and next steps for certain topics if you want to take them a little bit further so that's it without further ado let's jump into front end web development foundations i want to start here with a quick win i want you to see the power of code and see what we can do with it very early on so that you can kind of have that confidence to know that you don't have to go through you know download a bunch of crazy things onto your computer and and write a bunch of crazy lines of code in a terminal to actually do something cool since we're going to be jumping into code here in the first lesson before you know anything about it you're going to be confused and that is very okay don't worry about that i promise we'll go back through everything that we talked about in this video and couple videos from now you will probably be able to actually do what we're going to do in this video so if you're a fan of the netflix series stranger things you might remember in the first season the kids were talking to their professor about this thing called the upside down and basically what the professor was talking about was a lot of physics concepts and stuff like that but basically he was talking about this tightrope where there's a human on the tightrope and then a flea on the tightrope and the flea can actually go around this tightrope and see all different sides of it but the human cannot and that's ultimately what we're going to be doing throughout this series we're going to be looking at something that you've spent a lot of time with already and actually go to the other side of it and understand what's going on behind the scenes and the first thing that we're going to touch on is something called browser developer tools now you might have seen something like this before this is called a web browser and you probably use it every day of your life but have you ever right clicked and clicked inspect element and gone into this tool on the right where there's all these different you know applications that you can go through to see what's going on on this web page as a full stack web developer or more particularly a front-end developer which is kind of a subset of full stack development you're going to be using these developer tools a ton and you're going to get very used to them in this video i actually want to walk you through the most common parts of these developer tools and actually write some code within them to give you an idea of what this is all about the first thing i want you to do though is download the firefox web browser so just type in firefox download go to the download and get that onto your computer i want to be working with the same tool across the board although you have developer tools within any other browser whether that be edge or chrome or firefox you have these developer tools there but i want to make sure that you're looking at the same thing that i'm looking at and firefox is actually a great web browser for developers because it has some really advanced developer tools and in my opinion has probably some of the best developer tools of any browser so get that downloaded and then once you have it downloaded go to google.com and take a look at this pane on the right and if you don't remember i just right clicked anywhere on the page and i clicked inspect element now the first thing we're going to talk about here is the inspector now the inspector is probably the portion of developer tools that you will become most familiar with and the reason is it allows you to basically as you might expect to inspect the page that you're looking at or the web page so as i'm scrolling over these different lines of html code you'll see that different parts of the web page are actually highlighted and i can click down and drop this down and get more specific at what i'm selecting so i can basically see what's going on in this web page and then if i go to the bottom here these are all these stylings or the css styles that go along with the html again don't worry about this we're going to get into these you know languages later into the course but for now i just want to show you the landscape and to really show you what's going on let me open up another tab here go to google.com slash images and let's just type in dog let's grab any dog here i like this one here and we want to open this in a new tab so let's say copy link location now we come back here to the page and let's click this button up here this is kind of our selector for different elements of the page and let's click on this image here you can see at the top there well i can't show you but there's an img or image html tag let's click that and then let's go into this source attribute and paste in that link that we just grabbed of the dog photo once we press enter you'll see that our web page has actually updated and we can close the developer tools and google.com just got a makeover now this is not permanent changes obviously this would not be permanent changes but we can uh right click inspect element again let's click on the image and let's make this a little bit uh smaller so let's put a width of a hundred and now you can see that the dog is a little bit more styled on google's home page but as i said this is not permanent these developer tools are great because they allow you to make very temporary changes to your web page so you might have the code that you're writing you know on the back end and in your terminal or your code editor and those are the live changes but you can come here to just make little changes like you saw how the picture was too big so i went in and changed it to a different dimension and i can kind of test out these different changes and the power of this is just it allows you to do things in real time but the second that we click refresh on the page you're going to see that it goes back to the original web page so you are the only one seeing these changes within the inspect utility nobody else sees them and the changes are not permanent like i said we're going to be spending a ton of time here so don't worry if none of this really made sense but as you can see we're clicking through we can click on different elements so let's maybe click this little icon down here and you'll see how powerful this is once we get into this stuff in the bottom right where we can see the box model and we can actually look at the document object model or the dom we're going to get into all of these things but the next thing i want to show you is this thing called the console now the console is probably the second most popular tool within developer tools or at least in this series we're going to be using the console quite a lot and what the console is it's actually called a reple and that stands for read evaluate print and loop now you don't really need to know that what i want you to remember about this console is two things number one it allows you to write javascript and number two it allows you to actually modify the document object model or the dom and we're going to talk about that a little bit later in the series but these are the two main things that the console allows us to do and to show you i'm going to just give you a short example of how we can use the console to make some of the changes that we are also making here in the inspector so if you remember we kind of changed the image within the inspector we can also do that within the console so let's grab a different dog picture here just for fun grab this uh this one here like this and copy the image location and all that's doing is just copying the actual file that we're looking at from here we can go back to our our page and what we want to do is actually select the image tag on this page so all we have to do is say document.query selector and then type in img and you can see that when i do this this little element here that we were editing earlier is highlighted and down here it's actually printing out what we're going to get when we press enter so when we press enter we get this image but we actually have to save that in a variable real quick so let's say our image and again don't worry about what's going on here we'll talk about all of this later but we'll say document.query selector img we'll save that so now our image is actually representing this element on the page and then we can say our image dot set attribute and what i want to set is the source and i want to set that equal to the link that we just copied from that dog photo and when i press enter it's actually going to change this on the page and i can also resize this just like we did earlier so our image dot set attribute or actually no it's not set attribute it's style dot width equals let's say 100 i'm sorry it's actually not it is an attribute so let's set the attribute sorry i'm doing this in real time here so let's go with width and we'll go for 100 here and you can see that it goes down resizes accordingly so this is just a short example of how we can use the console to actually edit the document object model or we can just think of it as the web page for now not only that but we can also write pure vanilla javascript within this repel or this console and to show you that i've actually already showed you that because i saved our element in a variable a javascript variable but we could also write a function so we could write a function that says change image or something like that and it takes a url and then it's going to actually change the image so let's go ahead and make that function and what that's going to do is going to say document dot query selector img dot set attribute and we're going to say source and then we're going to pass in the url once again don't worry about what i'm doing here this is just examples and then i'm going to say enter down and that's our function so now i can execute this function and i can paste in that url and click enter and i guess it's already there so it's not going to change but if we went over to a different dog picture and run this function you'll see that it changes the image in the browser so we can write javascript we can modify the webpage all that stuff within the console we're actually going to be learning a lot of the initial concepts of javascript here in the console because it is so easy to set up and get going all you have to do is as we showed you just right click inspect element click on the console and boom you're ready to write javascript so it's a great place to start so that we don't have to set up all this elaborate stuff just to write basic javascript there are a couple of other features within developer tools that i want to show you here but they're less important than the first two that we looked at so the inspector and the console now just to remind you right click inspect element and from here i want to go up to the network tab so this network tab is something that you will be using a lot later on and if we refresh the page you're going to see that there's some activity here and what the what's happening here in this network tab is you're actually seeing all the resources that are being transferred to and from over the network to this web page so google's backend servers are actually require sending resources to the front end and you're actually seeing all these resources displayed in the browser so an example of that would be this google image right here and if we click on the images part you can see that the december holidays in the response is actually the image that we're looking at here and if you wanted to see other things about this such as the timings how long it took to load you know you can look at the http headers all that kind of stuff this will be very useful for us as we get into more the back end development where we're talking about api request and you know http and all that kind of stuff so we'll come back to this we'll spend some time here a lot later in the series but i wanted to expose you to it initially so you can see that this is something that we will be using now another thing we will be using if you click this little arrow over here is storage and storage is useful for a couple of things storage is going to be useful for tracking user behavior on a site so not to to play the blame game here but when we talk about facebook and all the cookies that facebook puts on uh your web browser on your mobile phone all that kind of stuff this is where this is being stored so you can see google is storing these cookies about us as the user and although these probably won't mean a whole lot to us as people using the site these have specific ids and all sorts of identifiers that allow google to know things about the user this is also great for authentication this is how we're going to actually log in and register people to our app and when we build our backend dashboard for editing our blog for our personal website we're going to need authentication because we don't want just anyone coming and editing our blog so this storage tab is what we're going to use to actually do that and while it's a very advanced topic that comes later down the road i also wanted to show it to you everything else that you see here whether it be the inspector console debugger network style editor all this kind of stuff everything else is not quite important enough for us to cover here so i'm just going to leave it there for now you probably won't even use anything outside of what i talked about for at least several months or even years or maybe even ever as a web developer all right so i think i've covered mostly what these developer tools are about just to review right-click anywhere on a web page inspect element and you'll get to these developer tools now the last thing i want to do in this video is actually show you what coding is all about and have you write your first javascript program well you probably will not understand the concepts behind the code we're about to write i want to get you in the habit of actually writing code and you know get you feeling comfortable in this console that we talked about earlier so open up a web browser hopefully the firefox browser you just downloaded put this video on the side open up the console so right click inspect go to the console you can even type clear with two little parentheses to clear the console and we are ready to get started now the goal of this program is going to be every time we click on google's web page so when i click it i'm actually clicking right now um i want the background of the page to actually change color and i want that color to be a random color we can do this with javascript code and i'm going to walk you through line by line how we're doing it and kind of explain what each line does just to give you a feeler again don't worry about how everything's working we're going to go into the details of the javascript language in future videos the first thing we have to do is save the body html tag in a variable so when i say body html tag every web page that you're going to go to has this thing called the body and this represents kind of the container for the entire web page aside from of course the topmost container which is called html so we want to select this body and this is what we want to actually change the background for so we can actually do this manually and we can come down here and right now you see on the body the background is hashtag fff which is just the hex code for the color white now we can type in green and you can see that the background actually changes in our web browser now what i want to do is do this in vanilla javascript and do it every time we click the screen so in order to do that the first thing that we have to accomplish is saving that body tag so let's type clear to get a clean space and then let's type in a variable so here's our javascript variable we want to set that equal to document.queryselector and then we want to get the body tag so when we press enter that's going to be the html body you can actually print that out and see that we have successfully retrieved that and stored it in that variable from here we actually need to create a function now this function we're going to save in a variable and i know this is getting a little bit confusing at this point but bear with me and type along as i'm doing this so we're going to go through this function and we're going to assign something called an array so this array is going to contain various colors of our choice so you can put as many as you want in here separated by commas i'm just going to put in a couple random colors here that i've saved so 2942 these are just hex codes so you can actually go hex codes something like that you can go to google and you can see all the different hex codes here there's various tools that you can use for this but anyways we're going to save these as a comma separated array and let's go add a few more and then you can also a valid css value would be something like red blue or green so we can also type in red blue green yellow orange maybe even purple alright so we have saved this colors array now the next thing that we have to do is get a random index from this array and when i say index each of these values are at a certain index within this array and we'll talk more about this later again but this one would be the zeroth index this would be the first index the second third so on and so forth to get this random index we have to actually use a built-in javascript library called the math library and we need a random random index between 0 and let's see 1 2 3 4 5 six seven eight uh there's eight items within this array so we want a index between zero and seven because uh arrays are zero index they don't start at one so we'll get a random index here that'll be equal to math.floor and we're going to pass in math.random please don't worry about what's going on here this is just an example and this will give us our random index and i actually pressed enter a little bit too early so we actually have to rewrite this function okay i've kind of returned to where i was before i pressed enter and screwed this all up but basically we're getting a random number between zero and seven and the way that we do that is actually get the length of the array by typing colors.length so let's not make that mistake again let's enter down a couple times and now we have to actually grab the random color and save that in a variable so we'll grab the random color that'll be equal to colors and then we'll insert our random index that we have generated then we are going to actually style the body so all we have to do is grab this uh variable that we saved up here called html body and then we want to go to the style and then background color not clip background color and then we want to set that equal to our random color here so that's going to actually do what we showed earlier and change the background color and then the last thing i want to do is just print to the console a confirmation that this worked so we'll do something like this we'll say the user clicked and set the color to and then we'll concatenate this string in here and say random color okay our function is done hopefully it works here and the way that we see if it works is by running it or executing it so we'll do random click function and it looks like the user has clicked and changed the color now to finish this program off the last thing we have to do is set an event listener on the html body element now this is a little bit advanced but it's kind of a cool feature of you know javascript and all that kind of stuff so all we have to do is go to html body and say on click so it's saying when the user clicks i want to execute this random click function all right so once i press enter now we have wired it up so that every time we click it's going to change the the color so i'm going to click a couple times since it's random we might get a couple duplicates here but then we can even close this and keep clicking and get all sorts of different results here so it's actually a pretty cool program that we just wrote and you can see you know even you who have never written a line of code before were able to kind of follow along and type what i was typing and write your first little program in other words you just did something that almost everyone in the world does not know how to do and will never know how to do your brain is probably in utter confusion right now but don't worry in the next couple videos we're going to actually be deep diving into this javascript programming language and we're going to be learning how i wrote this program what are all these different symbols that we're typing into the console and all that kind of good stuff so don't worry if you're completely confused this video was mainly to give you an introduction to the developer tools that you can have within any browser we're using firefox but it just gives you that introduction and also just some exposure and some comfort levels actually writing real javascript code and that's it for this first video be sure to hit the subscribe button and also that little bell so you're notified when new videos are posted to this series and thanks for watching today we're going to be talking about what is javascript and why do we use it let's see what wikipedia has to say because we know that's going to be simple javascript often abbreviated as js is a programming language that conforms and i think i'm going to stop right there because this is not a useful definition especially for most people watching this what you can remember javascript as is just a programming language and more specifically it's a programming language that is made for the web you have other languages like c plus plus python php ruby c sharp all these different languages and they all kind of have their unique characteristics and javascript's unique characteristic is that unlike these other languages that cannot run in a web browser javascript can't this has been the case for quite some time but recently as you can see the 1.0 version of webassembly has been released in four different browsers which does let you write all these other programming languages to run within the browser but for now just think of javascript as the programming language of the web because it's the easiest to write in the browser and even with web assembly there's a lot of complicated steps to get all of that set up so you say javascript is the programming language of the web but is that all it can do well the answer is no but if you asked that question 20 years ago you might have been laughed at since that time though javascript has gained a lot of traction in development communities and was the most used language in the stack overflow developer survey of 2020 and to name a few things that you can do with javascript let's pull this up here you can create web applications so there's lots of front end frameworks like angular here you've also got react i think this is the most popular one at the moment you can do back-end pieces of your web application or back-end servers with things like nest js you can also create desktop applications if i pull up my visual studio code which is a code editor this is actually a desktop application that was built using this tool called a framework called electron so you can build desktop applications for various operating systems you can also create mobile apps with react native and native script and then finally you can do ai and machine learning data science projects with tensorflow.js and a couple other libraries so to say that javascript is not a powerful language is a bit of a miss here because javascript has many different applications the next question that you might ask is can javascript get you a job and the answer to that is a resounding yes but javascript alone is not going to do it you have to actually learn frameworks such as the ones that i've gone through here angular react nest gis electron react native all of these frameworks that are built around javascript are what you get hired for and speaking of frameworks what is a javascript framework or any framework for that matter well the way that i think about it is the framework is something on top of a programming language so it's an abstraction on top of javascript that enables a developer to do something more efficiently in less time facebook is created largely with a framework a front-end framework called react and this is a very popular framework we just went over it and facebook could definitely have been built without a framework it could be built in just javascript html and css but the reason that it wasn't is because the developers there decided hey this is getting really repetitive and we have certain things that we do over and over and over again that we want to kind of automate and therefore react was born react is something that the developers at facebook kind of created in order to make their jobs easier and more efficient we can use a little bit of a construction analogy to think about frameworks if you're trying to build some commercial real estate building are you going to use a shovel and a ladder to create this whole entire building and you know drive all the materials back and forth and pick up trucks the answer is no you're going to get huge trucks you're going to get bulldozers you're going to get cranes you're going to get all sorts of big materials in order to build this huge building and it's the same thing with building web apps or honestly anything that you would build with code if you're trying to do it with just a language like javascript and you're you're not using some of the popular frameworks that have been built on top of that you're basically showing up to a construction site with a shovel and a pickup truck so you don't want to do that in order to be employable you need to understand how to use these different frameworks like angular or react or even a framework like electron which is enables you to write desktop applications like visual studio code all right i think that's enough on frameworks for now could do a whole video on it let's go to the next question which is is javascript the same as java or at least similar and the answer to that is no if you search enough on the internet you'll probably stumble upon the analogy of a car versus a carpet they both have car within the word but they're nothing alike and that's kind of the story with javascript and java now java was actually the inspiration for javascript way back in the day and javascript was meant to be a companion to java so they do have some similarity but when you're writing each of the languages they could not be more different the next question is javascript better than python or honestly insert any language there the answer to that is always going to be it depends and the reason that i say that is because all these different languages that you're choosing between have different unique use cases and while they can all kind of do the same thing they all are programming languages some of them are better for certain types of industries than others so if you're asking me to build an app like facebook i'm probably going to go with javascript and use some javascript frameworks because that's what those are made for they're made for creating apps like facebook but if you wanted me to do some machine learning and do you know build out the computer that sits in a tesla i'm probably not going to use javascript i'm probably going to use python because python has a much more mature ecosystem around this topic of machine learning ai data science all that kind of stuff so when you're asking whether you should go with javascript or another language it really depends on what kind of industry you're getting in what you're trying to build all that kind of stuff if you're trying to build a web app like facebook javascript is great if you're trying to do some machine learning python is great if you're trying to create an extremely efficient uh we'll say banking application or something like that a language like c plus which is much faster in execution than a javascript would be a good choice there's no right answer here but there are different ecosystems around each of these languages and you'll kind of notice as you study them that certain types of developers kind of gravitate towards different languages another question that you might have is html and css required to work with javascript and the answer to that is yes and no it kind of depends on where you're actually running that javascript now let's start with the front end because this is a little bit easier to explain and then we'll move on to the back end if you don't know the difference between those i actually made a video on the difference between front-end and back-end development anyways let's go over to my browser here on the left side of my screen i have an html file that is actually open in notepad here on the right not doing any fancy editors here just want to get a few points across so let's create a basic html document here i know i haven't actually gone through how this all works yet but we will get there i promise at some point here in the body of the html i'm just going to create a button that says click me so when we refresh the page we should see all of this reflected on our web page and you see that we have a very unstyled button which is just click me so this is a example of a web page this is no different than you know the home page of facebook except for the fact that it is not including both css and javascript so this is html alone now if we add css to the picture we're actually going to be able to style this now we can add some styles to this button so let's do that right now we'll add a background color of transparent and then i want to say the border is going to be 1 pixel solid navy then i want to add some padding to it so we'll say 20px we want the font size to be 1.4 rem just somewhere in that range and let's go ahead and close that bracket off save it and refresh the page you can see that we have a much better style button here we can also add some hover styles to it so let's go down here and say button hover and now i want the background color to be navy and i want the let's see the border to be none i want the color of the text to be white so that it stands out on that navy background save this refresh and you can see that we have a styled button that changes styles when we hover over it so this is the power of css but if i click this button if i click this button it's not going to do anything does nothing it's just a static element on the page and while it looks like it's changing looks like it's doing something that's only being done with css in html now in many web applications you have various buttons and forms that users can click on in order to do things within the app so i'm sure you've been to a login page before where you enter your email and your password and you press login well that is actually being triggered by javascript and while you do need the html and preferably css if you want a web app that people are going to visually like you need javascript to actually react to different interactions on the page by the user if you just have html and css your login form is going to not do anything and you're going to have a useless app to kind of give you an idea what i mean obviously this is not realistic but i added some javascript to this html document that also has css and you can now see that every time i click on this button it's going to add an element to the page so i'll click it and you can see that i'm adding a p tag with the text clicked the button every time i click it and this is just to demonstrate how once you add javascript to your html in css you can actually interact with the page rather than just looking at a static website well in this case all i did was add some stupid text to the page you can see that how every time we click the button we can fire an event and maybe send in the login credentials to our backend server authenticate get the response back and then redirect that user to a different page of our site when we're working with javascript on the front end or in the browser html and css are pretty much required for javascript to run now yes it could run without them but it would be completely useless you'd just be running it in the console like we did in the previous lesson so it has to have something to actually interact with now on the back end since javascript is both a front-end and back-end language we can actually run javascript without any html in css and it can be extremely useful when we're working with javascript in the browser html and css are required to make this javascript do anything useful because ultimately the javascript in the browser is all about taking those html elements and modifying them and interacting with back end data all that kind of stuff so html and css are required in the front end on the back end if we're using the node.js runtime which is the way that we can run backend javascript we don't need html in css let's write a simple node.js program to demonstrate that we don't need html and css in order to do that once again we have not covered this in this series yet so bear with me but we need to create a sample.js file and then we'll open this up with vim which is going to allow us to edit this so once we have this again haven't covered this either but we can make a do math function something like that where we can enter in two values and get some you know result so let's do a multiplication problem so we'll just we'll say return x times y and close the braces and then in order to actually run this program we are going to actually store it in a result variable so do math and we'll say 5 and 80 something like that big enough where we can't necessarily do it in our head we'll do like 87 or something and so it's going to store that in the result and then we want to print the result to the console so from there we can save and quit and then we can clear the console and run this sample dot js program and you'll see that it did that multiplication for us so obviously this is very useless and you wouldn't ever do such a simple function but think about back when we were working on the front end and when we clicked that button so let's say that it was a register form you add your email and your password you click the button the javascript in the front end is going to say okay the button was clicked i need to do something and so what it's going to do is grab all that data the email and password it's going to send it over to your backend server and your backend server is going to run the node.js you know version of javascript and it's going to say okay i got an email and a password let me look up this email in the database hash the password and compare the hashed password to the one within the database and then i'm going to send some data back to the front end in order to tell it whether the user has successfully authenticated so you can see how these different sides of javascript work in different ways but the point is no you don't need html and css on the back end but yes you need it on the front end and the last question that i'm going to cover is what is the difference between something like es5 or es6 and javascript well there's actually an organization called ecma international not even sure if i said that right but this is just a standards organization and in order to really visualize this i want to go into a quick example of something that's not related to javascript well not entirely and that is the iso 8601 date standard so we're going to go through this example and then i think everything else is going to make a little bit more sense so if i open up the console like we did in the first lesson of this series and i run let me save a date into a date variable and then i want to print this date as an iso string and you can see that i have this uniquely formatted date and it looks very standardized and that's because it is and it's actually standardized by this you know organization called iso which is i think the international standards organization and there's actually organizations like this across the world for various you know different standardized things and the same thing applies for javascript and that is where ekma international and the ecmascript specification or in other words you might hear this as es5 or es6 all different versions of this spec this is where these have originated and so javascript is actually an implementation of this specification and the reason this is so important is because of browser compatibility among other things so if you go to this mdn web docs article you can see a grid of all the javascript apis that are supported within different browsers and the reason that this ecma standards is so important is because the specification just needs to be updated and maintained in one place and then all of the different browser developers you've got different teams for each of these different browsers they can look at that specification and implement their browser knowing that all of these specifications are not going to just randomly change when you know say someone working on the javascript language decides that they want you know a new api feature so just remember javascript is an implementation of the es6 or es5 or whatever version you're working with it's an implementation of that specification all right i think that does it for this first rapid fire uh q a session that was not very rapid fire whatsoever but this last part of the video is totally optional it's for the people who really want to learn the ins and outs of web development and want to continue on with this series here and what we're going to be talking about in the last part of the video is just the question of what is a programming language so we talked about how javascript is a programming language and it's kind of unique because it can run on the backend and the front end and a browser but we didn't really cover what is a programming language and i think that this is a very important question to ask because if you're going to be writing a lot of code you probably should understand what's happening behind the scenes at least at a high level so for this last part what we're going to call a programming language is a group of tokens or symbols that when combined together in a very very specific way can be translated by something called a compiler into machine code or ones and zeros that all computers know how to run you can think about it like this um let's let's take a spoken language something that i don't know which is pretty much every language except for english but we'll call it i don't know something like portuguese if you started speaking portuguese to me my brain would not be able to comprehend what those words and symbols and expressions actually mean semantically although i can't understand portuguese what i can understand is the body language of someone speaking it so if you've got someone speaking portuguese and they have a very you know sad demeanor to them i can understand generally that what they're saying is not a happy thing and it's the same thing with computers well a computer cannot understand javascript it cannot understand python it cannot understand c plus it can't understand any of these languages but if we use something called a compiler which is just another program more code that actually reads other code and compiles it down into ones and zeros that a computer can actually understand so it's taking a language and it's compiling it down into ones and zeros or we can think of them as body language because all computers understand ones and zeros now this is where it gets really complex and i'm not going to actually dive into it during this video once we start talking about compilers then we have to get into the topic of operating system theory and which you know what is our target runtime that we're dealing with like where are we actually executing these ones and zeros because each computer is going to have a slightly different cpu slightly different alu or arithmetic logical unit all of these things matter when we're compiling code down and for each different language and kind of each different operating system you have to have slightly different compilers and other tools that i'm not mentioning here in order to do that now if you're someone who really wants to understand this in a great level of detail something that was very important to me then i recommend reading this book right here which is the elements of computing systems and you'll see within this book you know i'll see if i can show like a little page here but we actually get into things like the arithmetic logical unit and how these ones and zeros are actually translated into commands for the computer now i will say if you're going to read that book i will warn you right now it's going to take you months and it's very difficult to get through that said i came out of it learning in a ton and i was really happy with what i learned from that book but if you're trying to get a job quickly you're trying to become employable as fast as possible do not read that book it's going to take you too long it's going to distract you from actually writing web applications now if you're someone who wants to understand this stuff but doesn't have time to build a computer from scratch which is what you'll do in that book i recommend a book called code by charles petzl and this is going to give you all that information it's going to teach you the ones and zeros compiling all that kind of stuff but it's going to do it a little bit quicker and you're not going to be quite as hands-on with it and the final question of this video is where does javascript actually run and the answer to that is very similar to the question that i asked earlier which is is html and css required to run javascript and i said well it depends whether you're running it in the front end or the back end and what we're asking here is where does javascript run and this is where we get into the topic of something called a run time and a runtime is basically uh an environment where code is run and it gets into those topics that we just talked about with compilers and how do we target different platforms and compile down to ones and zeros and there's actually two run times for javascript and that would be the node.js runtime and then the browser runtime and believe it or not the browser runtime within google chrome uses something called the v8 engine and this is going to actually do all of that compilation that i was talking about and it's going to get into a bunch of other stuff some optimization using the just in time compilation method all that kind of stuff it's going to basically take your javascript code and convert it to something the computer can read and node.js which is kind of the backend version of javascript or the runtime that we're going to use for our backend javascript servers and stuff like that that also uses the v8 engine which is similar to chrome so you can kind of guess that the creators of node.js and the creators of the v8 engine that runs in chrome and node.js to kind of become good friends here over the last couple years but this is not the only way that we can run javascript you know you have node.js which is probably which is the most popular way to run javascript on the back end and google chrome which is one of the most popular browsers but in this course we're using firefox as our web browser because i think the developer tools are a little bit better there and with firefox we're not actually using the v8 engine in order to compile the javascript down we're using something called spidermonkey which is the original engine for javascript that was built within the netscape browser way back in the day and as a side note if you really want an interesting podcast to listen to look up browser wars i think it's by the podcast called business wars and it's a really cool story about netscape and how all this all these browsers gained market share really worth checking out in my opinion so in summary javascript can be run on the back end or the front end and if it's run on the back end in node.js it's being compiled down using the v8 engine if it's running on the front within google chrome it's also using the v8 if it's running in firefox it's using spider monkey if it's running in safari it's using some other compilation method and that i can't think of off the top of my head but ultimately these are all different run times and you do have to be aware of you know where you're running your javascript code because it's going to run differently there are things that you cannot run within the node.js runtime if i tried to let's say open up the node.js runtime here all i have to do is click type node and it'll bring me into the the console if i tried to say document.query selector and then grab the body tag of the html and i press enter it's going to say uncaught reference error document is not defined and that is because in the node.js runtime the document object model does not exist like it does in the browser so you have to be aware of these different runtimes and understand that certain types of code can run in the different run times it's all javascript but you kind of get what i'm saying here and at this point per usual i've uh droned on way too long longer than i ever thought that i would but you have reached the end of the second lesson of my full stack web developer roadmap series and while we haven't written any javascript yet well i guess we did in the first lesson but we haven't actually learned javascript yet we do have a lot of context around it now which was the purpose of this video i really wanted to put this video together in order to give you that context so that when we start getting into the details you know variables and data structures and functions and loops all that kind of stuff you have a little bit of a background to understand it from if you liked this video please give it a thumbs up and subscribe to the channel if you want to follow along with this series and series in the future and with that hope you enjoyed the video and i'll see you in the next lesson [Music] in this video we're going to be talking about javascript variables and data types and be sure to stick around till the end of the video because i have some challenges and some practice questions that you can use to apply the skills that you learned in this video before we start talking about variables and data types i just want to mention that as we go through this series i'm not going to be covering everything that there is to know about the javascript programming language there are a lot of different features within javascript a lot of built-in functions and you know syntax shortcuts and all sorts of things like that that you can do but they're not absolutely necessary for you to write javascript code for example let me create a basic array here so we'll say my array and then we'll put some numbers in it and this is actually what this video is all about so don't worry if you don't understand this quite yet but javascript has some built-in methods that we can use on this array so we could say myarray.push and then we can add a number to the end of this array so just return the length of our new array and if we print it out you can see that there's the number 50 at the end of it now that is an example of a very useful and commonly used built-in javascript prototype method that we're going to be covering in this series but there are other methods and there are a lot of them that we will not so for example we can say myarray.reverse now this one's pretty simple all it does is it reverses the order of the items within that array as you can see but this is not critical for you to learn to write good javascript code and be a web developer so i'm going to selectively exclude things like that and try to focus in on the most important things of javascript likewise there are also some very complex topics that we can discuss around the internals of javascript and how it works behind the scenes and how prototypes work and why it's technically not a class-based language and all these things are important for you to understand later down the road but when trying to get you from you know xero to deploying your first full stack web application they're not completely essential per se and believe it or not a lot of web developers that work full-time jobs and are employed getting paid lots of money don't fully understand the internals of javascript and that's why this series called you don't know javascript written by kyle simpson or his github username is getify and he's written this wonderful series or book series on javascript and how it works and you can go to each of the series on github and actually read all about you know how javascript works under the hood and he has a tremendous understanding of this language and explains it in great detail but i would say that you would use this as a reference and personally i think it's geared towards someone who has written javascript for some amount of time and it's something that i reference back to even now after writing javascript for about four plus years it's still a great resource to look back on so just know as we go through this series i'm not going to be covering absolutely everything and if you have questions be sure to just google it or go to this you don't know javascript series and pick the section you want to learn about and kind of read a little bit more about it and finally if i can get through this long intro we'll finally get to variables and data types but i want to show you one thing real quick and that is the console.log statement so when we're working within the javascript console in our firefox dev tools you can print a variable by using the console.log statement and this is built into javascript you can use it on the front end in the javascript console or the end and the node.js console and we can print something like hello world we've done this already once in a previous video but this is how we're printing we print different variables so if i just say my first variable and set it equal to 20 and then console.log that first variable you'll see that it prints it to the console that's going to be used heavily within this series so i want you to be familiar with it the next thing is the concept of comments so if you put two forward slashes before your code and then you write something this is obviously invalid javascript and you press enter it's not going to throw an error on you well if you type it like this it's going to say that there's a reference error because we haven't commented it out there's another way to write comments let me clear the screen here and you can put a forward slash and then a star and then you can enter down write whatever comments you want and then you just have to close it out by a star and then a forward slash and when we press enter it's not going to yell at us with any sort of error because we have written a comment so i just want wanted to basically introduce those concepts the console.log the single line comment and the multi-line comment you're going to see them a lot and they're very useful for you know marking up your code and remembering things about the code that you've written a big congrats if you're still watching this video it means that you are dedicated to learning how to code and for that reason let's jump into variables and data types in javascript so when i think of a variable in javascript i just think of a data container and a good analogy for this is the amazon warehouse so they've got these big vertical yellow stacks of containers with different products in them and then on the bottom they have little robots that kind of drive these containers around according to when the item items in the containers are meant to be shipped and i like to think of this as an analogy because javascript variables are very similar in that amazon warehouse each container is registered and all the robots know exactly where to find the container and the products that are within that container same thing goes for javascript variables your computer knows exactly where to find all of these javascript variables that you declare and assign values to and it's able to retrieve them instantly so you can think of javascript variables as you know you write them and then they are basically like little amazon products that get put into these containers and put in the back of the warehouse but the computer at all times knows exactly where to go in that warehouse to find the values of these variables in computer language we wouldn't really call it a warehouse we would just call it memory but it's a good analogy to go by and i think it'll help you remember what variables are all about just like amazon's containers can have various types of products stored within them so can javascript variables and we can look at this here we're going to just go through a couple examples of variables the first one will be we'll say your first variable and we'll say learning to code gives you super powers or something like that so that would be one example of a variable and that is actually a string variable we'll go into that a little bit later now you can have your second variable which will be equal to a number this time so equal to 10 and finally we'll say your third variable and that'll be equal to an object and we'll say that this object has a property that says something like hello world and this is also a valid variable and to print these variables once again we can use this console.log statement and we can print your first variable something like that and you can see that it prints it out to the console so these are just examples of writing variables and now i want to go into each component of these statements so as you can see we have this keyword called const we have a little space here then we have what seems to be our variable's name then we have this equal sign then we have some sort of data and a semicolon at the end these are all really critical parts to writing javascript variables because when the compiler goes to basically interpret um the code that you have written it reads it in a specific order and it looks at every single what we call token within the code so for example if i tried to define a new variable and this time i'll just say another hello world something like that this would be valid but if we took out just even this single little quotation and pressed enter it's going to give us a syntax error and you're going to get a lot of these syntax errors when you first start out because they basically say hey you haven't written this javascript program correctly you've missed a specific token within your code now a lot of times you don't have to actually worry about these syntax errors what i have pulled up here is visual studio code which is a code editor we're going to work with a little bit later in the series we're not there yet but what i want to show you is that if we tried to say new variable and then we define hello world with a semicolon at the end it's going to look fine but if we go over here and delete this little uh quotation you're going to see that visual studio code picks up on the fact that there's an error in this syntax so a lot of the times when you're writing code we're not going to see it you know here in the console which is what we're using right now but once we get into code editors it's going to point these things out and you don't really have to worry too much about getting everything right as you're writing your code the next thing we have to talk about with javascript variables is the concept of declaration and assignment to properly use a variable you have to do that sequence of steps in order so you first have to declare the variable then you have to assign a value to it now if we typed in you know something like new variable and i'm going to actually refresh the page so we clear out all of the variables that we had already written so if i say new variable something like that and we say hello world that is going to not only declare but also assign this variable at the same time so in many cases we're doing it simultaneously but we can also break it out into individual steps and we can do that by using the let keyword and we'll say let another variable equal we'll say 20 this time or actually no we need to just put a semicolon at the other end of it and by doing that we have actually declared the variable and now we have to assign a value to it and we don't have to put the let keyword at the beginning we just assign a value and we can print this to the console by saying another variable and you can see that it equals 20. so in the second example i declared it then i assigned it and it's important to understand this concept you don't necessarily have to you know think about this while you're coding but i wanted to point this out and it brings up another good point about this const versus let versus var keywords at the beginning of your variable expressions let me go ahead and write the same variable per se in three different ways so first we can say variable one equals and that's using the const keyword then we can say let variable we'll say 2 this time equals 10 that's valid and then we can say var variable 3. can't type here equals 10. and you can see that all three of these ways of writing a javascript variable are valid so you might ask the question well how do we decide between const let and var you need one of them at least to create a variable if we just tried to say um you know random random keyword and then variable 4 equals 10 it's going to give us an error because random is not an assigned keyword such as constellat or var let's go ahead and start with var and that is the only keyword that i would recommend against using it's kind of a legacy keyword within javascript it's something that existed in the old versions and standards of javascript and nowadays we have the constant let keywords which will pretty much cover every situation that we need to now the reason var is not used anymore is because it can be redeclared so if we say var my variable equals 10 and then we say var my variable equals i'm sorry i can't type today equals 20. it's going to actually update the value of that variable without yelling at you at all but if you use a keyword like const so we'll say another variable equals 10 and then we say const another variable equals 20 it's going to say that there's a syntax error and it's going to yell at us for re-declaring a constant variable with the var keyword being able to re-declare a variable seems convenient and it actually is when we're working within the console but when you're actually writing code it can be kind of dangerous and you can have some unwanted side effects by being able to redeclare a variable the next keyword i want to talk about is let and let is something that you will use whenever you expect to reassign a variable at some point in your code so we just kind of talked about re-declaring a variable and that's very different from reassigning a variable so if you remember you first declare the variable which is basically saying go back to our amazon analogy where we're talking about you know the containers and the robots have to remember where those containers are in the warehouse well your computer has to first register or declare these variables and then once it's declared it kind of allocates some memory space on the hard drive or something like that or in ram to basically remember where to retrieve that variable from so that's the declaration step and then the assignment step is where we take an actual value and we say okay put put this value in this variable and so the computer will go find that variable that was declared into memory and assign your new value to that you know little memory slot so we will always use let when we expect to reassign a variable so let me show you what this basically looks like so let's say that we have a counter and we start that counter at zero and then we want to actually increase the value of that counter so we can say counter equals counter plus one and we can also say counter plus plus that will do the same thing as the previous line that i just wrote so now we should have it equal to 2 and you can see that it is now equal to 2 and you can see that we are reassigning the value of this variable multiple times now if we tried to let me refresh the page real quick so that we can use this variable again and if we tried to say counter equals 0 with the const keyword and then we tried to say plus plus it's going to say that we have a type error because we can't assign or reassign a value to a constant variable so that's when you want to use constants is basically when you expect the value to never change so a good example of that is say we have a program that calculates some sort of i don't know income statement you want to put in a tax rate for this individual so you might program in something like a tax rate and that will be equal to 0.08 for eight percent and you press enter and now this constant variable is never going to change your tax rate's never going to change and that is the best use case for a constant variable so just remember you use let when you expect the value to be reassigned to a variable and you use const when you expect the variable to never change values now the the concept of a constant is pretty universal in programming and it spans across many languages oftentimes you'll see constants written something like this so we'll say tax rate equals 0.8 percent and you can see that the variable has been written in all caps and separated by an underscore now you'll see this very often but this is just a semantic difference and it's functionally the same as what we had written before and that actually leads us into our next discussion which is about variable naming conventions i'm going to paste a couple different versions of variables in here and we'll press enter and you can see that all of these are actually valid in javascript they're all written slightly differently but they're functionally kind of the same so these are all saying your first variable but javascript recognizes them as different variables that's why we're allowed to declare all of these at once and they all functionally operate the same but there are a couple of these that are more preferable in the javascript programming language than others so for example the first variable that we have here is written in something called camel case and that is basically where the first letter of your variable is lowercase and then each successive word or logical separated word within your variable is going to be capitalized so this is called camelcase and then there's another version down here which is called pascal case which is basically where it's the same thing as camelcase except the first letter is capitalized the second variable here where we have underscores in between lowercase letters is called snake case then you have this one right here that we just talked about which is called constant case and then finally you just have a totally lowercase variable and these are all different semantic styles that you can put on a javascript variable or any programming language variable and oftentimes these indicate to other programmers something about your code i'll talk about what these mean in just a second but first i want to talk about what makes these valid so you can really think of a variable in javascript kind of like a twitter hashtag so if you were to write a twitter hashtag and we'll say the full stack roadmap which is the one that we have for this series this would be a valid twitter hashtag because it's all one word now if i put a space here at the end then twitter's only going to recognize that first part or full stack and then it's going to treat roadmap as just normal text and that's exactly how javascript works with variables if we tried to define this as a variable so let's say const full stack roadmap equals 10 it's going to give us a syntax error because we have a space in between these two words to have a valid javascript variable you have to put everything in one word and use one of these conventions up here at the top now there's also a few things you can't do so we we cannot do something like this where we say full stack equals 10 you can't have these little quotes here this is not going to work and then you also cannot start a variable with a number so you can see that i put 1 at the beginning of this variable it's going to throw an error but if i put full stack 1 at the end so now we have our number at the end of the variable that works just fine so these are just some of the things to watch out for it's fairly intuitive and you'll get the hang of this really quickly and if you don't get the hang of it the javascript debugger is going to yell at you and it's basically going to give you a bunch of syntax errors all right so now that you know how to validly declare and assign a variable let's talk about something called a style guide and this goes back to the concept that we see up here where we have all these different semantic ways to write a variable they all work but some of them are more preferable than others depending on what language you're writing in and what you're trying to do with the individual variable that you're declaring if you type in javascript style guide into google you'll find a couple different style guides by different companies so this is google's javascript style guide and here would be airbnb's javascript style guide so basically what these are are specifications for the employees of these different companies that says here is how we want you to write your javascript for our company and what this might entail is different naming conventions different use cases of different javascript features all that kind of stuff there's a plethora of things that you'll find within these style guides but the one i want to look at is naming conventions so we can find this within each of the style guides kind of have to look for it here real quick so here is naming for google so we have the naming style guide for google and airbnb and you'll see that each of them recommend camel case so we can see package names need to be lower camel case class names are upper camel case let's go down to normal variables so non-constant field names should be lower camelcase local variables lower camelcase you kind of get the point they're all pointing towards this lower camel case convention and if we go to airbnb you'll see here that it says use camel case when naming objects functions and instances this is a bad example this would be a good example so you'll see within these two different companies they're both recommending camelcase and that's actually a recommendation across all of the javascript community so you'll see a lot of javascript code written in this camel case as we saw here with our first variable and this is what i would recommend you do as you're starting to learn how to write javascript the last thing i want to talk about with naming conventions for variables is what we're actually naming them so you'll see here that these are very descriptive um this is clearly indicating to the programmer that you're practicing you're writing your first variable but there's also going to be programs where you're going to have multiple variables within a single function or a class and you need to identify between these different variables so tell me would this be an appropriate way to name a variable if we just said q equals 20 well the answer is no because how are you going to ever know what that q is supposed to represent maybe it represents a counter in a for loop uh maybe it represents i don't know the the tax rate on something you know whatever it means you need to name your variable accordingly so if this 20 represents a 20 tax rate or something like that we need to call it tax rate for net income or something like that where you're clearly specifying what this value represents now there's also the the case where you can get too complex with this and write out a variable that's way too long and in that case it gets hard to write that code because you're constantly having to type this super long variable and it takes you longer so you want to find that middle ground where you're not being so vague where it's just saying the the variable's name is q and you're not so specific that you can't you know write your variable within 10 seconds the next thing we have to talk about is the left side of the variable expression and the right side of the variable expression so let's write our first variable javascript gives you super powers something like that we'll press enter and let's take a look at this so on the left side when i say the left side i'm talking about everything left of this little equal sign in the middle and when i say the right side i'm talking everything right of this little equal sign in the middle so it's very important to make a distinction between the left and the right because depending on what you're putting on each side you're going to get different behaviors the left side of the equal sign is always going to include two things it's going to include either const or let and you could use var but i don't recommend it and then it's going to have a single space right here and then it's going to have your properly constructed variable name and then i always use another space this is just good code formatting after that and then an equal sign and everything left of the equal sign is considered the target or that container that you're trying to put some sort of data or value into so this is where you're storing your container now on the right side of the equal sign you have the actual value that you're assigning so the right side could be one of many different things and this is where we get into the concept of data types in this case we have a string on the right side of our equal sign and then followed by a semicolon that's to indicate the end of the variable expression so i want to touch on that real quick let's say that we had one variable equals 20 and then const second variable equals 30. this would be a valid expression we wrote them on the same line but we ended each statement with this semicolon now if we were to pull this up again all i did was press the up arrow key to get my last statement in the console that's a quick shortcut for you and if we were to take out this semicolon here and let's go ahead and i don't know rename these variables so that we can actually experiment around with them so one and second and then press enter it's going to yell at us because we don't have a semicolon at the end of our first variable declaration and that's why that semicolon is so important it kind of indicates the end of an expression just for an example here's a bunch of code that i wrote that is demonstrating that we can have multiple different things on the right side of our expression so you can see in each line we have our basic variable declarations but on the right side we have anything from a number to another variable to a string to an object to an immediately invoked function to a combination sorry it keeps disappearing a combination or addition of variables and if we were to console.log variable 6 here you're going to see hello my name is zach and basically what that's doing is it's taking the value derived from variable 5 which is returning us this hello my name is and then it's adding to variable 3 which is zach so you can see how we have a lot of different options on the right side and this is always going to equal the value which could come from a function or a specific data type it can come from a lot of things and that is assigned to the target container on the left side of the equal sign all right let's go a little bit simpler here again i'm going to paste in three variables the first one we have is a number the second is a string and the third is a boolean so let's talk about these different data types a little bit there are a couple different data types in javascript and the three most common are sitting on the screen right now these are kind of the prim primitive data types now you might notice that as we have declared and assigned these variables we didn't really mention what data type we are specifying here and in a lot of languages such as c or c plus plus or java they actually require you to specify what type of data you are assigning to this variable another example of that would be typescript which is actually a superset of javascript that we might talk about a little bit later in the series but basically it's a it's a language that compiles down or it's actually transpiled down to javascript and it allows for type uh checking or static typing so this is how you would declare a variable within typescript so we would say the variable 1 equals a number and that is equal to 10. so in this declaration and assignment we are saying okay we have this variable one this is a number and here's the value which is a number assigned to it and this is what we would call a statically typed language and when we say statically typed all we mean by that is the fact that you are before you run your code telling the compiler what types of data to expect now javascript is what we would call a dynamically typed language and you can see this above where this variable 1 the compiler is going to have no clue what type of variable this is until it actually evaluates the assignment value that you've given it and figures out what type it is and you can actually see the type of each variable so if we typed in variable 1 using this built-in type of function we can also say variable 2 and variable 3 to see the different types of the variables so you might say well if javascript is dynamically typed and we don't really need to specify the data type that we're assigning to the variable then why should we even care well that is exactly the reason you should care because if you're writing code like this and you're assigning all sorts of different values to these variables it's really important that you always recognize what types of values you're assigning to your variables so that you know you don't try to do something like this where you say variable 1 plus variable 2 and then it will say 10 some value so clearly something happened here you tried to add a number in a string together but javascript has magically coerced the first variable which is a number into a string and it has combined those or concatenated those strings together into a single string so without understanding the different data types that you're assigning to your variables things like this are going to get very confusing and you might try to add things together or combine variables together and you don't know why they're combining in the way that they are this right here is actually called implicit coercion of a variable type we're not going to talk about this quite yet but basically what's happening is it's taking variable 1 which is of type number and variable 2 which is of type string and it's saying i want to coerce this number into a string and then combine the values together so you can have a variable such as number 1 will equal to will be equal to 10 as a string and then we can say number 2 equals 20 as a number and if we said the type of number one it's going to give us a string and the type of number two is going to be a number but what we can actually do is explicitly rather than implicitly coerce these two variables together so if we implicitly do it and say number one plus number two it's going to give us 10 20 because it is basically coerced this number of 20 into a string and concatenated together with this first string which is 10. but if i wanted to add these two together as numbers all i have to do is use the built-in math function to or not math i'm sorry number function to put the number 1 which is a string into a number and then i can add that to number 2 which is already a number and now it's going to give me the correct answer which is 30 and that is the addition of those two numbers so you can see that if you're not paying attention to the types of data that you're working with you might have unexpected results as we saw right here when we added a string and a number together all right so now that we've talked about the importance of data types let's go through each of them and just get an overview of what they look like so let's clear this out so the first one we're going to do is a string value and there are actually two ways that you can declare a string value you can first say something like hello world with these single quotations and that would be valid you can also say another string value and specify with double quotations hello world and that's going to work just the same there's no better you know way to do this but i would say just like we use lower camel case for writing javascript variables the convention within the industry is generally going to converge on these single quotes and a lot of code linters which actually a code linter is something that will take a big style guide like the ones we looked at before and it'll actually write these rules into code and then while you're typing your code within a code editor like visual studio code it will take all those rules and it will apply it to the code that you're writing as you're writing it so a lot of these code linters are going to specify that they want your code written with these single quotes rather than the double quotes that you can also write a string as moving on to numbers these are a little bit easier all you have to say let's say number values are variable all you have to do is specify a number and there's nothing nothing to it here now the one thing that you might mess up on at some point let's just make this a different variable you might try to add a comma in there to specify a larger number and when you press enter here it's going to give you a syntax error because this is not a valid way to write a number you always have to just write the number as one big number there's no commas or anything that you can put in it booleans are also pretty simple so if we want to write a boolean boolean value is going to either be equal to true or it's going to be equal to false so that's pretty simple there's only two possible values that you can put in there that's booleans now the next one is arrays and this is where we start to get into a little bit more complex data types so let me clear the screen to work on this real quick and if we wanted to say first array and then we want to put in some numbers here we can do that and you can see that an array is specified by these opening enclosing brackets and then values here that are separated by commas so you can think of an array and i know computer science people would yell at me for saying this but you can think of this array as just basically a list and this list is separated by commas and within this list you can actually put any data type that you want so we have numbers in this first array but we could also put in a variety of data types so we can put in a number a string here we can put in an object we're going to get to that in a second and we can also put in another array so we just put in another array within this array so these are all valid things you can put in an array and you can kind of mix them together you probably won't do this very often where you're mixing data types but just wanted to say that you can do that and that's part of javascript's dynamically typed language feature now the important thing to understand about arrays is that they're indexed and when we say index each of these values separated by commas are uh identified by a single index and these indexes start at zero so they don't start at one as you would think so in this first array you might say oh this value 10 is represented by the index of 1. well no that's not the case this is represented by the index of 0 and then you just count up in order so 10 would be represented by 0 20 would be represented by 1 30 by 2 and 40 by 3. and we can retrieve these values from the array by saying first array so you type out your array variable and then you use brackets just like you did to write them but this time you pass in the index so if we wanted to grab this value of 10 from the first array we just type in the zeroth index and press enter and it's going to give us that value from the array now we can also retrieve these other values so say from the second array let's say that we wanted to get this last value which is actually another array we could say console.log second array and then we can use brackets and we'll count this is zero this is one this is two and then this would be the third so we can say three and that's going to give us this array we can even tack on another set of brackets and we'll say we want the third or the actually the second value i'm sorry i'm misspeaking totally here we want the fourth value from this array by using the index of three and then let's say we want the zeroth value or this value of one from the the array that we've grabbed there and now you can see that we can grab values from nested arrays so that's the important thing to know about arrays you have to pay attention to the indexes and then you use bracket notation to retrieve those values from their respective indexes the next thing we'll talk about is objects and in javascript these are very important you might even hear the phrase that everything is an object in javascript and that that is true i don't want to get into the subtleties of it today but it is very important nonetheless to learn how these objects work in javascript because you're going to be using them in every single program that you ever write to understand objects just think of them as a complex container so going back to that amazon analogy where you have those carts of different containers and then within the containers let's say that you have other containers that have the amazon products so let's say that you have some sort of jewelry product that is sold on amazon you might have a big container and then you might have 30 different little containers within that big container and then within each of those 30 little containers you have different pieces of jewelry so that is kind of what a javascript object is all about you can make it infinitely complex infinitely nested there's just so much you can do with it so let's jump in and see what a basic object looks like so we'll say object variable and then this is indicated by these little curly braces this is what you enclose an object in and then within an object you have to give it properties so these properties are written just like a javascript variable and you can kind of think of them as variables contained within the object so we might say prop 1 and then you use a little colon to say okay that variable i want to assign the following value to so we'll say we want to assign uh the number 20. and then to create another property you have to put a comma after the first property so it's sort of kind of like an array where you have a bunch of different values separated by commas but in this case you kind of have to think of it as a key value pair so this is the key which would be the prop 1 or the variable and then this is the value right here on the other side of the colon if you want to say prop 2 and then we want to set that to 50 you can do so like that then you put your semicolon at the end press enter and now we have the object variable with prop 1 and prop 2. now to access these properties within the object we use something called dot notation so unlike the array where we would say object variable and then you put little brackets and say 0 or something like that we can't do that that's not going to work but if we said object variable dot prop 1 like that then we're going to get the value of 20. so we access the properties using dot notation and this can be infinitely nested so let's construct an object right here we'll say it's a nested object and it'll be equal to we'll say layer 1 and then layer 2 and we're just making a bunch of objects nested within each other and then we'll say target value will be 20. and so this target value is simply a property nested within several other properties within this object so we'll press enter and then to get that value you just say nested object dot layer 1 dot layer 2 dot layer 3 and then dot target value and you get the value of 20. so you can see how this dot notation allows you to basically grab the values of the properties off of the object and notice how when i'm using this dot notation i'm specifying the property so we could also do this um so we're going back to our object variable right up here so we can also like i said you can't use this zero here but what you can do is you can use this bracket notation you just have to define the property that you want to grab in that bracket notation so that's another way to retrieve a value from an object i prefer the dot notation usually but in some cases you might need this because maybe you know the name of the property so you know the name of the property is property one so instead of explicitly writing this property as a string you might store it in another variable and then pass the property value variable into your object i don't want to get too far into that because that's going down a little bit of a rabbit hole but you may need both of these in certain situations the last thing we'll talk about is functions and we're not really going to talk about them much but i did want to say that functions are valid things that you can assign to a variable and this goes back to the concept of everything is an object in javascript so let me just show you a brief example we'll say function container variable just to be descriptive here and then we can write a function like this and this function will maybe return the value of 20. so we are storing that function in this variable and then we can actually uh call this function like this and get the value of 20 that was returned from it but we haven't learned how functions work return values all that stuff so don't worry about this i just wanted to show you that this is a variable and we can actually assign a function to that variable so that's a valid thing to put on the right side of that equal side all right if you're still here huge congrats you're probably one of very few people that have the patience to sit through a video of mine that goes into sometimes excruciating detail about things but thank you for watching i just want to go through a quick summary of what we learned here and then offer you some exercises that you can use to actually practice the concepts we learned in this video so just to summarize when using variables we first have to declare it and then we have to assign it a value and these are often done in the same line of code the same step when we are using the different keywords so let const and var we probably never want to use var but when we use let it's because we want to reassign the value of a variable and when we use const it's because we expect the value of this variable to not change within the program next up the left side of the equal sign represents the variable that will hold the value that you're assigning so it's kind of the target for that data the right side represents the actual value there are many ways to write a javascript variable but camelcase is the preferred way you'll see this most often in the javascript community and then there are five main types of variables you have strings numbers booleans arrays in objects and those functions that we just talked about kind of fall into that object data type so the final thing is that a semicolon represents the end of a javascript expression you always put that at the end except if you're using an object in which case you can enter down onto multiple lines and you actually have to use commas to separate those different object properties that's it for the summary now let's move on to the challenges and practice for this video so like i said you're not going to be able to just watch these videos and learn how to code you have to actually apply them and at the end of each video i want to at least give you a few challenges to work through to apply your knowledge so i'm going to just put these challenges on the screen here for a few seconds and you can go ahead and work through them in the console within the firefox dev tools that we talked about in the previous lesson and how to use that and the answers are going to be on my github account and i'll link to those in the description so that you can see if you got them right all right on to the challenges for today good luck and i will see you in the next video [Music] today we're going to be talking about javascript operators and be sure to stick around till the end because i have a handful of challenges for you to try out and solve on your own to practice the skills that you learn in this video before we talk about javascript operators i want to do a little review of what we talked about in the previous video which was javascript variables and data types because they're going to actually come into play during this video so if we right click anywhere in our firefox web browser and click inspect element let me make this a little bit larger for us so that we can see and then you go to the console you're going to get into a repl or i think that stands for read evaluate print and loop which basically allows you to execute javascript without setting anything up so we're going to be using this to code in this lesson but also to review what we did last time so last time we talked about javascript variables and the way that you declare and assign a variable is one of three ways we can say const and then a space and then the variable name we're just going to call it a here and then you give it some sort of value this is a valid way to define a variable we can also do it with let a equals 20 and we could or not we can't use a actually because we already declared this up here with the const so we need to do something like b and then we can also use var but i don't recommend using this because it's kind of an outdated legacy feature of javascript but nevertheless it still works we can still declare and assign a variable that way now we also talked about how you cannot reassign something that you assigned as cons so if you wanted to say value will not change as the variable name and you set that equal to 20 and then you try to say value will not change equals 30 it's going to give you an error because you assigned it with const but if you say instead of const you say let and then value will change equals 20. then you can come down here and say value will change equals 30 and it lets you do that so that's the difference between const and let again we don't use var because it's kind of a legacy keyword in javascript now we also talked about the different data types that you can use so i'll just use the const keyword for our variables here we'll say my string equals hello world so we can define a string value which is represented by these little quotations the single quotations and declare that as a variable now we can also again i just press the up arrow on my keyboard to get that last command that we typed and we can kind of circle through all of the commands that i have typed along the way but anyways we can declare a string so we'll say the second string this is basically just a different variable and we can use double quotes to do the same thing so these are functionally equivalent some style guides would recommend that you use single quotes others would recommend double quotes it doesn't really matter you just got to pick one so that's how you define a string and if we wanted to define a number so my number we just give it a number that's pretty easy we also have booleans which are going to either be true or they're going to be false so that's pretty easy there's only two answers that you can possibly have there and then finally we have arrays and objects let me clear some space here so we'll start with my array and we can fill that with pretty much anything so we can do some numbers right here so that'll be our first array and then maybe we can say our second array will be something like string one and string two and if you remember we can access the properties from these arrays by using bracket notation so we just look for the index this would be the zeroth index the first index the second and the third so we use that here to identify different objects or different values within that array and then finally we have objects which are going to be a little bit more complex and they're kind of the the cornerstone of javascript but we can say property1 equals some value and then so that's a string value and we can access that by just saying using dot notation so we just say myobject dot and then property one and that's going to give us some value we can also with objects use bracket notation and if we pass in the string version of that property it will still give us some value so that's just a quick review of how javascript variables work and all the different data types now we're going to take these concepts and extend them a little bit further in talking about javascript operators so the first thing we got to basically resolve is what is an operator well an operator is something that exists in all coding languages not just javascript the one we're working in you'll see these operators in anything from javascript python you know java c c plus plus all of these different languages have this concept of operators and there's not a really great way to explain it without just jumping in but basically how i'd summarize it is an operator is something that allows you to either compare two values or more than two values you can modify values modify variables you know combine them together it just allows you some more flexibility in kind of to test the different equalities of different variables so i know that's a kind of a tough explanation but let's just jump in you'll see what i mean now there are four main types of operators we're looking at arithmetic assignment comparison and logical operators you'll see how these kind of fit in in just a second when we jump into them we'll start with some of the easiest ones which would be arithmetic operators so this would be plus minus or addition subtraction multiplication and division so you can see how this works it's pretty easy we can have a series of numbers and and remember we're using a javascript console which basically reads the value of a javascript expression and then prints it back to us once we start writing code in files later in the series we're not going to be able to like let's just say some random variable here equals 20 and you can see that all we have to do is type in some random variable and press enter and it's going to evaluate what the variable actually has stored in it we don't actually have to use this console.log statement that we have been looking at in prior videos you know you definitely can and it's going to print 20 but you don't have to you can just type it the variable so this is going to be very useful when looking at these operators here so in any javascript console you can just take numbers like this so we'll say 20 plus 50 and that will equal 70 and it shows how it evaluates those and this plus sign in the middle is considered a javascript operator so you can already start to see what these operators actually represent you can also do something like 50 minus 20 i'm trying to space it out here for us but we can say 50 minus 20 equals 30. and we can also do some division so 20 divided by 2 is 10 as well as multiplication 2 times 2 is going to be equal to 4. so these are all what we call operators this plus operator the subtraction operator division and multiplication and these are the most basic arithmetic operators at this point you might be wondering well what are we doing here i've never seen this syntax written before well this is not actually valid javascript code it works here in the console but if you try to write it in a file it's not going to work out so well generally you're going to be doing these operations and assigning the value to an actual variable so let's say that we want to do a result variable and we want to say 20 plus 50 divided by 2 or something like that and we save that and now we print out the result and it's going to be 45 because we did 20 plus 50 is going to be 70 divided by 2 is 45 and then it stores it within this result variable so what we call the right side here and this is going back to the prior lesson we talked a little bit about the left side of the equal sign and the right side of the equal sign so this equal sign right here so we've got left and then right everything on the right side is what we're going to be calling a javascript expression and so we're using all these different values along with the operators to create some sort of expression that is going to be evaluated in full and then once it's evaluated it's going to take that single value that it came up with and assign it to the variable on the left side of that equal sign there are actually a couple more arithmetic operators as well so we can use something called a modulus operator so let's do a hundred and then a little percent sign and then we'll say 50 and that's going to equal zero and basically what this is doing is doing a division problem so 100 divided by 50 and then it's figuring out what is the remainder of that division problem in this case 50 is a factor of 100 it goes into 100 two times with no remainders so we're going to get zero but what if we said a hundred modulus i don't know 77 well we're going to get 23 because 77 goes into 100 one time with a remainder of 23. so this may not seem very useful to you right now but i promise you there's going to be times when you're coding where this modulus operator will come in handy moving on we also have the exponent operator so if we wanted to say i don't know eight to the power of two so generally you would see it written like this well this is not actually what the exponent operator looks like we would have to put in 2 stars here and we'll say 8 to the power of 2 is going to equal 64. 8 times 8 is 64. and that's how we use the exponentiation operator so that's two little stars you can remember that where the multiplication is going to be 1 star so 8 times 2 is 16 and then if we do 8 double star 2 that's 8 to the power of 2. so that's the exponent operator and then finally there's a couple operators that are not going to kind of fit into this this schema that we've been looking at so far it's going to be a little bit confusing but i'll try to explain it best i can one of those is going to be the increment operator and then the next will be the decrement operator so let's say that we have a variable i'm going to intentionally mess this one up so let's say that we have a starting number and that will be equal to zero now notice what we used here we used the const keyword here so we cannot reassign this value so the operator that we're trying to use here is called the increment operator and the way that it works is you take a variable or some value and you put two plus signs at the end and when we press enter you're going to see that it's invalid because we have a const declaration or a value that cannot change now if we wanted to declare this with the let operator so we'll say start number this is a different uh actually let's just clear the whole screen so that we're we're very clear on what we're doing here so we'll say let starting number equal zero now this value can be reassigned so all we have to do is say starting number plus plus and when we press enter it's going to say zero but the actual value of the starting number variable is actually equal to 1 and we can see that by printing it out once more and if we do this increment operator again and then we print out starting number again we're going to get 2. so you can see how it's adding 1 to the variable and this is actually equivalent to saying starting number equals starting number plus 1. so you take the previous value and you add 1. so if we press enter now you're going to see that the starting number is equal to 3. now we can also do this with a decrement operator so we can say starting number actually let me clear the screen real quick we'll say starting number which is currently at three and put two little minus signs at the end and it's going to now equal two and we can do it again a couple minuses and now it's equal to one so you might say well why is this useful in many cases when you're going through loops which we're going to talk about in a future video this is going to come in handy so we just made it through the arithmetic operators but now we can move on to something called an assignment operator and believe it or not we've actually already covered this so when we write a variable my variable equals 20 we're actually using an operator already so this equal sign that we've talked about so much is actually an assignment operator and believe it or not this is the only assignment operator that you're ever going to need although this is all you'll ever need um pretty much forever i want to show you some of the other assignment operators that we can use just so that you know that they're out there but once again these are not required and quite honestly i don't even use these a lot of programmers that have been coding for 20 30 years will never use these so they're just this is just for educational purposes honestly i would recommend just writing your code out long form i'm going to show you what i mean in just a second let's say that we had a value we'll assign a variable equal to 10. notice how we use this let keyword so we can reassign this variable and what if we wanted to add 5 to it well this would be pretty simple all we would have to do is say x equals and then we want to take the previous value of x which is currently equal to 10 and we add using the addition operator 5. so that's the end of our expression press enter and now x is equal to 15. well there's actually a shorter way to write this and all we have to do is use a different assignment operator so the way that we would do this let's start over i'm going to refresh the page clear out all this nonsense up here and let's say that we have our variable again equal to 10 well we can also write it like this we'll say plus equal 5 and when i press enter this is actually going to reassign that variable of x from 10 to 15. so add 5 to it and you can see that if we print out x it's now equal to 15. so you can see this little operator here the plus and then the equal sign this is an assignment operator which works the same as just our normal equal sign but instead it's going to take whatever's on the right side of it and it's going to add it to the variable on the left side we also have this for every different operation so we have the plus equals so let's let's just start with x we'll use this as an example here again so we'll say plus equals 5 this should turn it into 20. we also have x minus equal to 5 which should bring it back down to 15. we have x divided by equals and then we'll say 3 so that should equal 5 and then finally we have x multiply equals we'll say 2 and that's going to equal 10. so you can see how we have several different assignment operators that are just shortcuts for us to do something that we could already do writing it out long hand and what i want to say about this like i said you don't need these operators all you really need is this one right here and that's the equal sign and the reason being is sure you could save a few characters of code but it's just a lot more clear if we wanted to take our value so let's get x this is currently equal to 10. it's much more clear in my opinion if we just say x equals x plus five rather than saying x plus or equal to five and it just eliminates the need to memorize a bunch of stuff you really just don't need these and i would recommend just not worrying about them so just to do a little recap we have our arithmetic operators so let me just write this out in a comment so we have our arithmetic operators and this is going to be plus minus multiply divide modulus increment decrement and i believe that covers all of it i apologize if i missed one here but these are all arithmetic operators and they allow us to basically modify different variables we also have assignment operators so assignment operators and this is going to be the equal sign that we're very familiar with the plus equals minus equals multiply equals and divide equals these are all valid assignment operators but once again i just recommend learning this one and being done with it now the next sort of operators that we can talk about are comparison operators and in my opinion these are the hardest to understand because it introduces a concept that we haven't really looked at a lot yet let's go ahead and start with a basic example so let's assign a result variable and we'll assign it equal to the following javascript expression and when we print out this result variable it's going to equal true and if you can just kind of logically think through this not to be confused with logical operators but if we can just think through this 20 is greater than 18 this is actually the greater than operator we're going to talk about all of these in a second but you can basically figure this out intuitively this is bigger than this so the result should be true right well let's bring in something a little bit more complicated so let's say something like i don't know constant result 2 is going to be equal to 20 equal sign three times and then 20. so in this case it's a little bit harder to understand what we're doing here we've got an equal sign here and then we have three more over here and this has probably got you thinking like well what's going on here the answer to this expression is going to be true and that is because 20 does equal 20 but we have to understand what's going on here on the right side of the equal sign before we can really think through this so to really understand this we have to get super super clear when we're talking about the left side of the equal sign in the right side of the equal sign we talked about this in the last lesson but we're going to go into this even deeper now so if we look at this second expression that we did this is the assignment operator and we have to be clear what we're looking at here so the left side is going to be the variable that we're assigning the right side over here too so this right side as i talked about a little bit earlier is going to evaluate to some value some single value and this is what we call a javascript expression and in this case we're using a special operator which is the triple equal sign and this triple equal sign behaves in a certain way that basically says well is the left side of it and the right side of it or maybe this a little bit confusing is 20 equal to 20 in both value and type so we talked about data types before and this is where that all comes into play so as we go through these make sure that you're being clear about what you're looking at so the left side versus the right side these equal signs over here are not doing the same thing as this equal sign right here think of this as kind of like the master equal sign that kind of determines everything about this whole javascript statement and then any other equal sign that you see are actually going to be operators that are doing some sort of comparison when we're talking about comparison operators in javascript there are four main comparison operators that have to do with equality so we have the triple equal sign we have the double equal sign we have the not equal sign with two right there and then we have the not equals with one so you can think of these two so this this one right here and this one right here these are kind of opposites while the double equals and the single exclamation and single equals are kind of functionally opposite now what do they actually do well to find out we need to start playing around with a few values so once again just like we could do 20 plus 20 and get 40 we can also evaluate different expressions using these comparison operators so if we said 20 double equals 20 that's going to evaluate to true we could also say 20 triple equals 20 is going to evaluate to true so what's the difference between the two well the difference is it has to do with the data type that we're working with so if you remember from the last lesson 20 written like this is going to be very different from the 20 written like this in the first case we're looking at a number in the second case we're looking at a string and you can see that the expression evaluates to true but we're not quite there yet so let's say we want a number value and that'll be equal to that 20 and then we also want a string value which is going to be equal to i don't know we'll say 50. but notice how we have these single quotations we could also have done double quotations here doesn't really matter it's just another way to write it and so now we have a number value and a string value and if we use the type of operator this is actually one that will we've we've already used a little bit but this is actually an operator itself and then we put the number value it's going to evaluate to number and we can also put in the string value and that will be a string so we know that this number variable in the string variable are of number and string types but we can try to compare them with each other using the double equals and triple equals equality so i kind of messed up here i meant to say that this string value should be equal to 20. so let me just go ahead and redo this once more so we'll say string value equals 20 and then we'll say number value equals 20 and we're back to where we started here and now what we can do is we can say string value double equals number value and this is going to be equal and it's going to return a value of true which is a boolean and the reason being is because while these are not the same data type they are the same value so if we were to convert this string value into a number so we can use the built-in javascript number function and pass in the string value we're going to get 20 which is a valid javascript number and what's happening here when we compare the string value to the number value using the double equal sign what it's doing is it's first converting them to the same data type so it might convert the string value to a number and then compare it to the number value so it's comparing apples to apples and then it checks whether the actual values are going to be equal to each other in this case they're both equal to 20 so we have a resultant expression of true now if we tried to do this with triple equals so string value triple equals number value we're going to get false because this time if we do triple equals it's not going to convert the types to match each other so basically what it's saying is um well yeah the numbers the actual values are the same but the data types are not so therefore this expression evaluates to false so when we're looking at these different the double equals and the triple equals the double equals does not care about the data type well the triple equals does care about the data type and in almost all cases i would recommend using triple equals because we're just being a lot more explicit about what we're writing in our code that way well you can see that string value does resolve the true when we compare it with the double equal sign this is only because it is implicitly coercing the data type of these values now if we did it with the triple equal sign it's obviously false because they're different values but like i said we could do the number string value so this is explicitly not implicitly like javascript does with the double equals but we are explicitly coercing this string into a number and then we can triple equals compare it to this number value and it's going to equal true like i mentioned these comparison operators are probably the most difficult of all the operators to kind of grasp your head around nevertheless hopefully this makes sense when we're talking about double equals versus triple equals and then furthermore if we were to set a result variable equal to the value of string value triple or double equals number value which we know equals true right here then what's going to happen even though we have these equal signs on the right side remember right side versus left side we have these this expression this entire expression on the right side of this equal sign and therefore what javascript is doing is it's saying okay first let me look at this is this expression let me evaluate it and see what it results in in this case it results in true so now i have a true value on the right and now i want to assign this into the result variable so we press enter and we're going to get the value of true in that result variable now moving on to the not equals or this little exclamation point with the double equals or the exclamation with a single equal sign these are basically the opposite of what we were doing with the double and triple equal signs so let's try this once more we'll say we want a number value of 20 and then we want a string value of also 20 but this is in string form so these are not going to be equal if we use the triple equal sign because that cares about the value and the type but it will be equal if we use the double equal sign now if we just plop in this exclamation part point it's going to reverse everything so if we do the exclamation point with a single equal sign and then we say string value that's basically saying um is the number value not equal to the string value and in this case it is equal to the string value because we're using only one equal sign so the answer is false i know that's a little bit tough to wrap your head around but if you think about it long enough it'll make sense now if we plop in this double equal sign and put in the string value this is going to evaluate to true because remember if we have the uh in in the case of let's say hold on one second triple equals this is going to carry care about the value and the type so it's the same deal with this exclamation point in the double equals so basically it's saying the number value is not equal to the string value because the types are not equal and it evaluates to true because no they're actually not equal to each other so just try to reverse it in your head don't think about this too much when the time comes you'll figure out how to use these but i just wanted to briefly go over them and show you that they're just the exact opposite of double and triple equals now with these equality operators it gets a little bit tricky when dealing with different data types so thus far we've only compared numbers and strings so if we said 20 triple equals 20 that equals true because it's a number in comparison to a number and they're of the same type we can also say 20 double equals 20 because you know that's kind of saying the same thing it doesn't care about the types the types are the same doesn't care but the values are the same so it equals true we can also compare strings so we can say hello world and that is going to be triple equals to hello world and that's going to equal true and we're also able to remove one of those equal signs and it's going to give us the same answer because the values are the same the types are the same it doesn't really matter there so we can use it with numbers and strings we can also do booleans so if we said true triple equals true that's going to give us true now i know that's a little bit of a tongue twister there but we're saying that the value of true which is a boolean is equal to another value of true so that expression evaluates to a truthy value and it prints true so i've said true about 50 times the last 30 seconds so let me stop doing that but basically we can do strings numbers and booleans with our equality operators now if we try to do arrays and objects the game changes a little bit so let's say that we have an object which is equal to we'll say property1 some value and then we'll we'll go down actually i needed to hold shift down while i did that so let me redo this real quick so starting over we have my object and this will be equal to property 1 which is equal to some string value and then we have we'll say property 2 is going to be equal to a number and there's our object we also will define my array and this will be equal to one two three four five just a bunch of numbers in there and all we have to say to prove this out is is my array equal to my array and the value here is going to be equal to true because you're comparing the same variable with itself but let's say that we created another array and this time it's going to equal the same exact thing so i'm going to put in the same exact numbers it's going to look exactly the same we'll press enter and now if we compare my array triple equals another array it's going to equal false so in the first case when we were comparing the same variable with itself it equals true which makes sense but why does this not equal each other why does my array not equal another array they look exactly the same and this is one of the quirky parts of javascript you'll hear that a lot but i'm not going to get into why these are not equal and actually how to compare two different arrays using javascript it's just too complex for what we're talking about but just know you cannot compare everything and the same goes for objects so if we said my object triple equals my object it's going to equal true because we're comparing the same exact object but if we say another object and we make that equal to the same exact one so property 1 is going to be equal to some value and then property 2 is going to be equal to 20. so this is the same exact object that we had up top but this time if we say my object triple equals another object we're going to get false and you might be wondering well maybe this is just because we're using triple equals well that's not the case even if we used my array double equals another array it's still going to be false and if we use my object double equals another object it will also be false so you cannot compare with these comparison operators arrays and objects you actually have to use a different library in a lot of cases and a lot of developers turn to a library called lodash so if we just type in lowdash into the browser you'll see that this is a utility library that basically provides you with a bunch of different methods that you can use to do a bunch of common javascript operations that would otherwise take you writing a good amount of code to do but again this is way too complex for what we're doing right now so we're going to go back and keep going with our discussion on operators so we've gotten through the bulk of these comparison operators the hardest ones are done now we just have to talk about numeric comparison operators and these will not come as a surprise we have the greater than less than greater or equal to and less than or equal to operators and you can see how these work 40 is greater than 20 that's true 20 is less than 40. that's also true 20 is less than or equal to 20. that is true and 20 is greater than or equal to 20. so you can see how these all work they're pretty common and if you took any sort of math class you'll know exactly how these work now the final operator that falls into this comparison operator category is something called the ternary operator and that is spelled let's see if i can spell this ternary is what i'm saying here and this ternary operator is going to look like this so we'll say we want to assign a result variable and we want to assign this equal to the following expression so we'll say 20 triple equals 20. we'll add a little question mark and then we'll say values match and then we'll add a colon and we'll say values do not match and we'll press enter and you're going to see that the result is values match so this is a very confusing expression to look at at first glance but you will get used to this this is something that a lot of programmers really like some of them do not like it because they think it makes it more complicated we haven't talked about conditionals yet but i think you'll be able to understand this a little bit so i'm going to try to explain it real quick this this expression right up here is going to be functionally equivalent to the following expression so we'll say if 20 triple equals 20 then we'll say return or actually no we need to redo this real quick we need to first say or declare a variable so we'll say result variable and we don't need to assign a value to it yet we'll say if 20 triple equals 20 then we want to assign the result variable equal to values match and if it does not equal so else that our result variable is going to be equal to values do not match if i can spell right okay so when we press enter it's going to say that it evaluated two values matched just like it showed up here when we printed the result if we type out result variable you can see that it has been assigned that value so i wanted to show you this even though we haven't gone through the if else statements yet the conditionals but i think it's fairly intuitive to understand we're saying if this expression evaluates to true then i want to assign the variable to what is after this question mark or the values match otherwise this else statement which is going to be substituted by the colon we want to assign it to values do not match so in other words this question mark is kind of like the if part and this colon is kind of like the else part and this uh everything left of the question mark is going to be the expression that we're actually evaluating and remember this is where it gets you know this is where it's kind of confusing when you're first starting out remember we have to keep the right side and the left side separate in our head so we're looking at this equal sign what is to the left side of the equal sign well that is the result variable now what is to the right side of the equal sign well that's everything here so this entire expression is to the right side and when javascript looks at this the first thing it does is it says what's on the right side and furthermore how do i evaluate that and then it uses the rules of this ternary operator to say okay first i want to look at 20 triple equals 20. what does that resolve to well that resolves to true for reasons we discussed before and therefore i'm going to grab this value right here now if it would have resolved to false then it would say okay i don't want this i actually want this one over here and then once it's found that answer it's done all of this work on the right side it's come to a single answer which is the value values match now it's going to assign that to the result variable so you have to be very clear with yourself whether we're talking about the right side which could be a really long expression or the left side which is the target variable that we're assigning the end result of the right side too we are finally done with the comparison operators i know this took up most of the video but they are very important and you'll see why once you start writing a good amount of code you're going to use them a ton now the last type of operator is what we call a logical operator and there's only three of them we have this double and sign we have the double pipe sign and then we have the exclamation point now the exclamation point is kind of the same as what we talked about earlier with this exclamation equals but it's kind of going to function in a different way using it as a logical operator we'll start with the and sign and basically all this is doing is it's saying it's looking at two different expressions so 20 triple equals 20 and then we put the and sign and now we can say 10 triple equals 10 and what it's saying is okay on the left side we have this expression 20 equals 20 which evaluates to true and then in order for the entire expression so this whole line in order for this to be true both sides have to evaluate to true so in this case we get true but we could also say 12 and in this case it's going to be false because the left side does equal true but in order for the entire expression to be true both sides have to be true and you can see on the right side these are not equal so we get false now the or operator if we did the same example and we said 20 triple equals 20 and then we use these double pipes this is basically or and now we can say 10 triple equals 12 and it's going to be equal to true because it's saying this entire expression is going to be equal to true if one of them is equal to true so in this case the right side is equal to true and that's all that we need for this to work now if we tried to write this expression where both sides are actually false i actually did that wrong there we only have one pipe in there somehow so we'll say 20 triple equals 18 double pipes 10 triple equals 12. both of these are false so it's going to be false now the last one that we're talking about let me clear the screen is going to be this exclamation point what the exclamation point does is basically reverses everything so generally we're going to be talking about a boolean value here so let's say that we have an expression 20 triple equals 20 which evaluates to true obviously if we put some parentheses around this and then we it still equals true but then we put an exclamation at the beginning of that statement it's going to reverse it to false now it's important to note you cannot put this at the end that's not valid you have to put it at the beginning of the expression that you're trying to reverse so that's what this exclamation point does and it's a little bit different than if we were to put in the exclamation and then the double equals that does a slightly different thing as we talked about earlier in the video so just to show you a practical example of how you might use one of these this is a pretty common one that i've seen and i've used in my apps and we're not going to actually be writing the code that i would write in an app but you'll kind of get the point so we'll have a variable that says is user logged in and we'll set it equal to true so our user is going to be logged in in this case and then we'll say does user have permission and we'll say false on this one so it might be a pretty common thing to see if we were to uh check both of these values to see whether a user can perform a specific action in an app so what we might do is we'll say can user perform action and then we'll set that equal to is user logged in and does user have permissions and in this case if we were to evaluate this expression on the right side of this equal sign that is user logged in as true but this does have permissions is false so this entire expression resolves to false so in many cases in our apps we might say if can user perform action or something like that then we can allow them to perform the action so that's just a practical example of how we might use these operators in the real world in the final part of this video before we get to the actual challenges that i'm going to give you is going to be combining all of these operators and this is where this concept of an expression really comes to life so i'm going to put a pretty complex expression on the screen here and we're going to walk through how it's actually working so we'll say result is equal to exclamation point we're going to put a couple parentheses here say 40 divided by 20 equals 2 and then we'll say something like i don't know true and then we'll say on the right side try to make this clear we'll say or and then put another parenthesis yes triple equals to no and when we evaluate this expression we're going to see that it equals false and this is actually a totally valid javascript expression although it's very confusing to look at but if we take this step by step i think you'll understand how we can combine all these operators into a single expression now i will say before we get into this you generally are not going to be writing something this complex if you're writing something that is this confusing to read then you could probably break it out into several steps and it's not a sign of intelligence when you're trying to make it so complicated like this you want to be as simple as possible and in many cases you're going to write more lines of code in order to improve readability of your code so this code is not readable this is something that would take someone you know let's say that i wrote this code and you had to go review this code this is going to take you you know at least 20 seconds to figure out what it's doing and in most cases we want it so that you can see exactly what the code is doing as quickly as possible nevertheless we can see how this evaluates by breaking it out into different steps so i'm going to just store each step in a variable and then we'll kind of work our way down to understanding the entire expression so if you remember we're talking about the right side versus the left side left side is the variable right side is the entire expression so we'll say step one is going to be equal to 40 divided by 20. so we'll start right here so we'll save that into step one now the second step is we take this 40 divided by 20 and use the equality operator to set it equal to 2. so we'll say step 2 is going to be equal to step 1 triple equals 2 okay so now we can check this at this point we're equal to true right now and the next step is going to be taking this entire expression that we just basically combined into step two and adding the and sign and putting it to true so we're going to say const step 3 is going to be equal to step 2 double and true and you can guess that since we already had it equal to true right here and we're doing another boolean value of true it's going to equal true so step 3 is actually equal to true true right now and you can see that right there and now we want to combine this with we've we've already done this entire expression here so maybe we want to store this in a variable so we'll say step 4 is going to be equal to yes triple equals no which is obviously going to be equal to false so that's our step four so now let's take our step three right here in our step four and combine them using these double pipes so we'll say step five is equal to step three double pipes and then step four and that's going to be equal to let's see step five will be equal to true i'll try to scroll up here so we've got this one um this one and this entire expression evaluated so the last thing we need to do is just add the exclamation point at the beginning of everything so all we have to do here is say step 6 is going to be equal to not step 5 and step six is going to be equal to false and if you see up here the total result of this entire expression is equal to false and all these steps that we did down here were just intermediary steps that we used to kind of break out this entire expression and figure out what the result variable was ultimately assigned so in summary an expression on the right side of the equal sign can be as complex as you want it to be and furthermore it can actually be something other than just what we've been working with we've been just looking at simple variables like numbers and strings and simple comparisons but it can be much more complex than that so you could even have something like a function evaluating to a specific variable so let me refresh the page and we're going to set result equal to a function and we haven't covered this yet but i just want to demonstrate what i'm talking about here so in this case we're going to return 20 from that function so what's happening here is again we have to be very conscious of the left side versus the right side so left side is going to be our result variable and right side is going to be the entire function so in this case we're going to set the right side equal to a function that returns a value of 20. now this is not going to automatically execute itself so we have to actually assign it using something called an immediately invoked function and once we do that we're going to actually be assigning that result variable on the left equal to whatever this function here returns when it is evaluated so you can see a result is equal to 20 and this kind of just demonstrates for us how the right side of this equal sign can be infinitely complex and that's why it's so important to understand that an expression should be solved in individual steps that's how the computer looks at it and that's how i would recommend you to look at it when you're trying to figure out what it's going to set that variable to like i've been saying throughout this series watching these videos alone is not going to do it for you you have to actually practice so let's jump into some of the challenges that i've put together for this video and i'll say that as we get better writing code and as we go through all of the different parts of javascript not just variables and operators but you know getting into loops and functions and conditionals and basically learning how to write code we're going to actually migrate ourselves over to a site called code wars so if you go over to codewars.com you're going to see this is actually a pretty popular site for doing different programming challenges and i'm going to actually start recommending challenges from this code wars site and we can basically work on them out of a profile and you know you can kind of go through these challenges on your own but for now we're not quite advanced enough to look at that so i'm going to pop up a few challenges on your screen and then i'm going to walk you through each of them and how we go about solving them if you want to get the most out of this i recommend trying them on your own first so pause the video for each of them and try to work them out in your browser or your dev tools console and then once you've figured them out i'll kind of go through the explanation all right so this is the first challenge and the question is why does this code not work and the answer is going to be because of the const declaration that we've made at the beginning of the code so if you remember when using const we cannot reassign the value so if we say const number variable equals 0 and then we try to reassign this using our increment operator it's going to give us an error but if we go ahead and change that to let instead of const so we'll say let number variable equals zero and then we can say number variable plus plus number variable plus plus and then do it one more time we're going to be able to do that and then in the end if we cancel that log number variable it's going to be equal to three so that is the solution to the first challenge challenge number two so this one is a little bit of a trick question these are not going to evaluate to the same value i think you could probably figure that out but the second part of this question is which one of these would you recommend and the answer to that i would not recommend either of these blocks of code so the first one is going to evaluate to false because we're using triple equals well the second one is going to to evaluate to true because we're using double equals but in this case the problem is not either block of code but the variables that we're declaring so this is how i would actually write this rather than what we have in both of those code blocks so we'll say first number equals 20 we'll say the second number also equals 20 and instead of the string value let's just put a number in there that's the real solution right there and then from there we can define our result which will be equal to first number triple equals second number because we always want to favor triple equals be explicit when we can and then now we can print out the result which is going to be equal to true so that's how i'd solve that problem challenge number three now this one is a little bit trickier but i think you probably could have gotten it let's refresh this screen real quick now the question was what does the expression five resolve two and the answer to that is that expression five let me make sure i get this right here answer five is going to resolve to false and this is why it's going to do that so let's just walk through each of the lines and see what they resolve to so we have a hundred we'll save them in the variables so expression one is a hundred modulus fifty and if you remember hundred divided by fifty is 2 with no remainder so we should get 0 for this so expression 1 should be equal to 0 you'll see that here now expression 2 is going to be equal to 100 divided by 50 which does equal two so that's going to be equal to two then expression three is going to be equal to expression one is less than expression two and in this case expression 1 was equal to 0 and expression 2 should be equal to 2. so this expression right here should evaluate to true so let's press enter on that and see if that's the case we'll say expression 3 that equals true as we would expect now expression 4 is going to be equal to expression 3 we'll use the and symbol and we'll say 300 plus 5 triple equals 305 so this is a little bit trickier how we look at this using the order of operations is we first look at this one on the right so we'll say 300 plus 5 what does that equal that equals 305 and then so we replace this basically and say 305 is triple equals to 305 which is going to be equal to true so then we kind of replace that with true and expression 3 is true so they're both true and expression 4 is going to result in true and then finally we're just going to reverse that so expression 5 is going to be equal to exclamation or the reverse of expression 4 so it's going to flip this true to false and we're going to get false for the final answer and if you tried to solve the problem of getting this all into one expression i'm going to just copy this into the console and just show you how it works you can see that we get false just like we just looked at and this is how i would combine this into a single expression remember the order of operations matters here moving on to challenge number four this is actually a slight review of last time combined with the concepts that we learned in this video so we're looking at an object and an array so we'll say my object i'm just going to write this out so that we have something to work with and we'll say prop 1 is equal to first value and then prop 2 is going to be equal to 20. so there's your object and then your array so my array here is going to be equal to 40 50 and 2. now if you remember from last time the way that we access properties from objects and arrays is with dot notation and bracket notation respectively so if we wanted to say get this value of 20 from this object all we would do is say my object dot prop 2. you could also use the bracket notation with a string for prop 2 and that will give you 20. but i would always prefer using this dot notation so we'll just get it this way all right so if we wanted to get one of the values from this array say we wanted to get 50 we would have to use bracket notation and if you remember an array is zero indexed so this is index 0 this is index 1 and this is index 2. so if we wanted to get that 50 all we would say is my array bracket and then we want to pass in 1 because the index of 50 is 1 and that will give us that value of 50. so knowing that this problem gets pretty easy so we said in our results array we are saying myobject dot prop 2 triple equals and then my array 0 divided by my array 2. so if you remember we'll store that in the result variable for now but you just have to basically break this down into different pieces so what is myobject.prop2 well that equals 20. now what is my array with the index of 0 well that is going to be equal to 40. and then what is my array with the index of 2 that is going to be equal to 2 if i can type it out here so we got 2. so basically what this expression is is it's saying 20 triple equals 40 divided by 2. and if we look at that expression that's going to resolve to true because 40 divided by 2 is 20 set equal to 20 is true so if you break it down into pieces it's not all that hard now challenge 5 is a little bit of a tough one but we're going to use the same method that we did in challenge 4 to solve it so i'm going to actually just copy in the object in the array or basically just all the code here so we'll copy this in we'll resolve it and let's open this up and we need to print the result which is going to be true now we have to walk through each step of this one by one to really understand it so the first thing that we have to remember is how to access different properties so if we wanted to access the quantity of nested object 1 all we would have to do is my object dot nested object quantity and that's going to give us a value of 5 down here if we were to evaluate that expression now the only tricky part here is this line right here we're actually putting the array or we're assigning each of these nested objects as the values of this array so to access this nested object number two we could do one of two things we could say myobject dot nested object 2 and then we might say the price which is going to give us 150. now since we put this into our array here we can also access it access it like this so we can say my array and then we would say we want the nested object number two which is the index one so this is index zero this is index one so we're gonna grab index one and you can see it's really nice in the console you'll see exactly what that evaluates to and then from there we can use dot notation to get the different attributes from it so the price which is 150. so knowing this it's pretty easy to evaluate this expression so if we looked at my array 0 dot price which is the first part of our little expression this is going to evaluate to 100. then we can say my array 0 dot quantity which is going to be 5 and then in the second part of this expression we have the second object in the array and we're just getting the price and the quantity so really what this result variable is saying is it's saying we want to look at a hundred times five and compare that we'll say that's greater than and then we'll say 150 times 2 and what this evaluates to is true because 100 times 5 is 500 which is greater than 150 times 2 which is going to be 300 so that's going to give you the result variable and you can see how if you just break it down into little steps you'll be able to figure out what the expression equals and you can actually access nested objects and array properties by mixing them together in today's video like i said we're talking about functions conditionals and loops and in the previous videos in lessons three and four we talked about javascript variables how do we declare and assign a variable and then also javascript expressions which includes all of the different operators whether those be arithmetic operators assignment operators logical operators etc that's what we're going to do in this video we're going to cover a lot of ground here and then we're going to supplement it with a ton of practice problems so i'm making a video where we're going to have 25 javascript challenges and i'm going to walk you through them and kind of explain how i'm going about solving them what i'm thinking about and we're going to use the topics that we covered within this video and the previous lessons to kind of solve these problems and we'll fill in a lot of the gaps that are not covered in the main content all right let's jump into conditionals in javascript so we've actually covered this already just briefly in previous lessons we haven't talked about how it works though so let me show you a basic conditional statement we're going to say if some string so we're talking about a string data type is triple equals to so we're comparing value and data type another string so these are obviously not going to be equal because although the data type is the same the value is not if that is the case we're going to console.log the strings are equal and then if it's not we're going to say else and we're going to console.log the strings are not equal all right so that is our basic conditional statement when i press enter you're going to see that the strings are not equal so it basically went into the second code path that's what we kind of call it is the the different paths that the code can take so we're saying if this is equal then i want to execute the code within these two brackets otherwise so else we're going to execute the code between these two brackets so there's a couple things i want to point out here just to connect the dots for the previous lesson that we talked about so let's just get the basic structure of an if then statement we have if and then else and this is basically what we're looking at and obviously there's indentation uh line breaks all that kind of stuff that's just formatting but this is the basic structure so in this parentheses we have a javascript expression and then we have some brackets that contains the code for the truthy uh code path so if that expression evaluates to true and then we have a brackets where we can put our code for the falsey path so if the expression is false and we've previously covered expressions in javascript in terms of variables so we might have said something like i don't know say 20 triple equals 20 and 50 triple equals 50. so obviously both of these are going to evaluate to true and then you combine it with the and operator and the whole expression evaluates to true and then we assign that true value to the variable right here now that's all we've looked at in prior videos but you can actually take this expression which is right of the equal sign and you can drop it into this uh parentheses for the conditional statement so just a different way to use an expression you'll see it a lot and it's something that i think is worth pointing out i'm going to drop a couple of different ways that we can write conditionals on the screen so it's a lot of code we're going to walk through each of these so let's open this up so i've declared a couple of variables up top so we have a number two number variables 20 and 10 and then a javascript expression which is basically saying is the first number greater than the second number which is going to result in true and then we've put these in some different conditionals so the most basic one that we can do is going to be a basic if statement so you don't even need to have an else statement at the end of this you can just use if and this is actually really useful you know you'll see as you start coding when this comes into play so we're saying if this javascript expression is equal to true that's what we mean by just putting it in there so usually you're you know kind of getting used to seeing like an equal sign put in an expression well if we're uh looking at a boolean all we have to do is drop it in the parentheses and it's going to going to evaluate to either true or false so since this does equal the true we would expect that this console.log statement will be reached and it'll print this expression is true and if you come down the first console log has been printed right here now moving on to the next one we have an if else statement i'm going to skip over this because we just covered it basically if js expression is true console.log this otherwise this and then finally we can include other conditions so not only can we do an if else statement we can also add some other code paths for our code to to go down so if this is equal to true we want to console.log the expression is true else if the first number is greater than zero so basically what we're saying here is let me sorry i closed that out there but if the js expression is false we're going to skip this first block of code and then we're going to go to the next condition so we know if we get to the next condition that first one is going to be false so we know js expression is false and then we're checking if the first number is greater than zero if that is the case then we're going to say the expression is false and the first number is greater than zero but if it is false so if the expression is false and the first number is less than or equal to zero we're going to fall into the else statement which is basically a catch-all for any other condition that we might have so the expression is false and first number zero or less so in this case obviously the variable is true so we uh go down this first code path and you see this expression is true down here or i guess this yeah the expression is true and then finally the last one that we have is just a formatting uh difference so we have an if statement but we've put it all on one line and this is just to demonstrate that we don't have to format it you know all pretty like we did up here although it helps for code readability and in other languages such as python the indentation and formatting actually does matter for running your code but in javascript it doesn't it's more of just a readability thing the last type of conditional that you might see is called a switch case statement and this is equivalent in many ways to the if else if else if else if else type conditional so if you have a lot of conditions that you're testing then a switch case statement might be appropriate for you so let me paste um some code on the screen again and we're going to execute this the output is the color is blue and we'll see exactly why in a second so first i put an array up here and i put the indexes or indices on top so the first one is an index of zero one two three four and then we have to get a random index so this is just uh some code that i wrote you don't have to understand this but we're basically going to get a random number between 0 and 4 and assign it to this random index variable so just think of this random index variable as carrying any value between 0 and 4 which happens to correspond with the indexes of the colors array then we're going to grab a random color from the array so remember we can get something from an array by passing in the index value using bracket notation and since random index here carries one of those values we can pass that in to the colors array so this the right side expression is going to evaluate to one of these colors but we don't know which one because we have a random index being generated then we come down here and we can write some basic conditionals so if the random color so again this is going to be one of the array values if it's orange we're going to say the color is orange and then you can kind of look down here and just see it's doing the same thing for all of the different conditions if we get to the last else statement there was no color found but that's never going to happen because we know that we have an index between 0 and 4. in this case the color turned out to be blue but it was a random color random choice we didn't know that as the developer before it happened now the reason that i bring this up is because this if else if statement where we have a lot of else ifs in here it gets very confusing and it's tough to read sometimes so this is why we might want something like a switch case statement and i'll show you the same code written in the switch case format oh and by the way if you're tired of getting into this browser console remember you just right click inspect element to get here i promise you we're going to get to an actual code editor soon but i just want to keep this focused on javascript we're not getting into setting up a local development environment yet so anyways here is that switch case statement same thing up here same colors array same random index same random color obviously since it's random it might not be blue which i think is the one we last saw so let's press enter it did turn out to be blue might have been a coincidence there but basically what we're doing is everything's the same up here but we come down to the switch statement and what we're saying is that we have a switch statement and we put the expression that we're evaluating in the parentheses so similar to the if then or uh or the if else statement we can put in that condition into the parentheses and then we come into these little brackets which is going to contain a bunch of cases so if the case is orange or in other words if the random color variable evaluates to the value of orange then we're going to hit this code right here and when we hit this code sorry about that when we hit this code the color is orange it's going to print that to the console and then this break statement is a special statement that we can write to basically say okay i found the correct case now stop executing the code entirely so if we were to get the random color orange it's going to print it to the console and then it's going to break and then basically all of the rest of this code down here will never be reached because we broke out of that statement so that's what it's doing it's basically just going down the the different cases it's saying okay what is random color equal does it equal orange no okay does it equal green no does it equal yellow no purple no and then we finally get to blue which it does equal and so it's going to console log that and then break out of the switch case statement now there's this last condition down here which is called default and this is basically the catch-all this is the else statement in our conditional and it's saying if random color does not evaluate to any of these cases let's go to the default value which we're just going to say no color is found so that is the basic switch case statement let me write this let me refresh and i'm going to write this in a little bit simpler format so we have switch and then here we have case are the different cases and then the break statements so this is obviously not valid but we have a switch then we put our condition in here which can be any javascript expression and then between the brackets we put all the different cases so switch state or switch case statements how often are you going to use them well in my experience i haven't used them a ton usually an if statement or an if else statement is going to be sufficient and oftentimes if you start using too many switch case statements there's that's kind of an indication that you may not be writing the most efficient code but that's to worry about later uh right now i just want you to kind of be aware of this understand how it works and uh just be familiar when you look it up and try to use it for the first time all right continuing on lesson five so conditionals functions and loops we're going to talk about loops right now and looping in javascript is a very effective concept and you will use it all the time so you really have to understand this um there's not a whole lot to it but i think the hardest thing is understanding why we need loops in the first place as we go through coding challenges a little bit later and you do more coding challenges just in general you're going to use loops a lot but it's generally just to solve some sort of challenge so you often will wonder to yourself well why do we actually need this in the real world and the reason at least in my opinion the reason we have loops or the most common use case of a loop is to iterate or loop over a set of resources so when we're talking about something called a resource that's basically saying that we have a bunch of pieces of data that are very similar to each other so often stored in a database and that might include something like if you have a blog it might be a blog post i'm trying to think of of another thing if you're running an app you have a bunch of users and there's a lot of different concepts that you can replicate but basically resources is a list of similar types of data and when we have those it's more efficient to actually loop over those than to manually print them out so again if we have an array something like this so i'll just say array and then we have 20 40 60 something like this we can go ahead and print to the console using this bracket notation so array of 0 and then array index 1 in array index 2. so that's how we can manually print each value of the array but when you have a thousand uh different blog posts that you want to render onto a web page it's not going to be efficient enough for you to just go into your code and start typing out each and every index not only that but once we add new blog posts to our database we're going to have to go in and change the code which is going to be extremely cumbersome it's not recommended you you just you won't get away with that in software engineering you have to do things more efficiently and that is when loops come in to demonstrate this let's take a look at uh maybe a sample array of blog posts so this is not something that you have seen before in my tutorial videos because we've been writing arrays on a single line but arrays just like you know conditionals and anything else in javascript can be indented and broken onto different lines this is all just a formatting thing so basically what we're doing here is we have three different objects so here's one object then we have another object and a third and you can see these objects are very similar looking in each of them we have a title property an author property publish date and content now obviously the values of these properties is going to be a little bit different for each of the objects but you can see that you know you can already start to infer that if we're looping over this array of blog posts we can use the fact that they have similar properties and let me just show you something really quickly that we could do using a loop now i'm not going to explain how the loop works yet but just to show you what i'm talking about so let me collapse this so we have some room here and i'm going to paste in this line of code this is what we call a loop and there are several ways to write a loop but again i'm not going to cover that quite yet but what you can see is that instead of hard coding the index the indices that we are passing in to our blog post array so in other words we want to retrieve a certain blog post object from that array we would need an index to do that instead of hard coding it we're using this variable called i so all i'm doing with the first four variables here is i'm grabbing a specific object from this array so i'm only grabbing one of them and then i'm taking all of the properties and grabbing the value of those properties and assigning it to each variable then i'm printing the variables below so when i press enter you're going to see it loops through all of our blog posts pretty much instantaneously or at least it seems like it to our human brain and it's going to loop through and it's going to print each of the values for each of the blog post so this is our first one what is javascript it's by me december 20th 2020 and some post content here and you can see that that is the first object in that array and then we go to how do arrays work and then how long does it take to learn coding so you can see that here and then finally here at the bottom so we have looped through every blog post in that array now let me talk about how these arrays actually work we're going to go into a little bit simpler examples so that you can really isolate these concepts in your head so let me write a basic loop in javascript so we're going to let i equal 0 we're going to go until i is equal to 100 that's just an arbitrary number that we're coming up with you can obviously pass a variable in place of this like we did with that first example and then we're going to increment the value of i so remember this from the last lesson this is basically the same thing i'm going to come out here just for a second that's the same thing as saying i equals i plus one so basically saying if i is zero then we're going to increment it by one and then we're going to put some brackets here and press enter and so everything within these brackets here is going to be considered one iteration of this loop so the code within these brackets is going to be executed as many times as we do this loop in this case we've set the uh stop condition at 100. so let's walk through each of these and kind of connect the dots between the previous lessons that we've talked about in this series and i think you're going to really understand this so the first thing that we see here is we declare this for loop using the four keyword so this is similar to how we have an if statement so we say if and then we put parentheses in some brackets now we're just saying 4. now in the parentheses is going to be the loop conditions the first one is going to be the variable that we're looping over so let i equals zero and notice this semicolon to end that statement we're just starting i at a value of zero and the reason that we start it at a value of zero is because arrays start at an index of zero so it makes sense that if we're looping through our array we want to start at the first value in that array which has an index of 0. you might also ask why do we choose i what is i well i is just a variable we could put in anything that we want we could say my iterator or something like that and then all we have to do is replace each occurrence of that with our new variable so we can choose whatever variable name we want and if you see this entire expression this is very similar to or it's the exact same thing that we use to declare a variable in javascript now the one thing you'll notice is that we're using the let keyword and that is really important it's important because this iterator variable so i'm going to flip this back to i really quickly because this is kind of like an industry standard a lot of developers will just use this as a convention when looping so we're going to use the let keyword because i is going to be reassigned throughout the loop as you can see at the end we talked about this is going to add 1 to i after each iteration of the loop so that's basically the same thing as reassigning it and if you remember from the previous lesson when we try to use this increment operator on a variable that was declared with the const keyword it doesn't work it throws an error so that's why we need the let keyword in here and then finally we have this uh code in the middle which is probably the most difficult to understand this is the stop condition so basically what we're saying here is we want to go through this loop we want to do as many loops until i equals a value of 100 so each time we go through the loop it's going to increment by one so basically since we're starting at zero and we're saying less than we're going to go through a hundred iterations of this loop so all i'm going to do here is just demonstrate this i want to console.log the value of i and you're going to see when i press enter it's just going to go 0 to 99. so we press enter and we got 99 here at the bottom and we started at zero so that's a hundred iterations remember starting at zero ending at 99 and the significance of this is you know we printed console.log we just printed a number here but you can do all sorts of things like we saw with the blog post array you can loop through the blog post array and assign values and all that kind of stuff to finish this topic off let's just go through a super simple example so let's make an array we'll call an arr for array and then we're going to put in a couple different values here so i'm just going to put some random strings in here these are string values nothing of significance you know just put whatever we want and then let's put in some numbers and then a couple more strings and what we're going to do here is combine our knowledge of conditionals that we looked at in the previous video and looping to do an example so let's write our loop here so we'll say i equals 0 and then we want to go until the array is complete so you'll use this quite often where you're going to look at the array you're looping over and you're going to grab the length of the array because that basically means we're going to loop until there's nothing left to loop through so that's a very common thing that you'll see within arrays and then of course our increment operator and then we'll jump into our loop and then in this case we want to set a conditional so we're going to say if the type of array and then we'll pass in i so remember this i is going to be incrementing each loop of the or each iteration of this loop so it starts at 0 and then it goes 1 2 3 4 5 all the way up to the length of this array so when we pass in i we're basically saying okay i want to grab whatever's in this array at the index that i is current currently at so it'll start with 0 and we'll grab this first value then it'll go to 1 and we'll grab this value so on and so forth so we're seeing the type of the array value so basically this is equivalent for the first iteration of this array if we copy this down here the first iteration is basically going to be saying type of this value and of course this is going to return string as we've seen in prior videos while the numbers are going to return the type of number so let's bring this back to what we had previously so we grab the array variable and we pass in i and what we're going to say is if the type of the array and i actually didn't even put a condition in here if it equals number so that's what it will return if we have one of these numbers then i want to console.log the array at that index so basically what this loop is going to do is it's going to loop through that an entire array and it's going to look at the value see if it's a number and if it is it's going to print it to the console in other words we're going to skip all those gibberish strings that we put in there so let's press enter and you can see that we have only the numbers printing so this is just a super simple example that demonstrates how we can use an array to quickly iterate over or use a loop sorry to iterate over an array in this part of lesson five we're talking about functions and we finally get to do something fun with javascript in the previous videos previous lessons we've been talking about the rather mundane parts of javascript things that you can't really do much with but when we combine all of those concepts with functions we have limitless possibilities that we can have with javascript of course there's other things but like i said in this video series i'm taking you through the most important parts we're not going into the super fine details yet we're going to do that through practice problems and through real world practice we're going to fill in those gaps there so again we're talking about functions in this video and the first way that i want to start this out is by writing out a basic function so here is how we write a function in javascript we're going to say my function so if you notice this is kind of similar to what we've been doing with the if statements the conditionals and the loops we have some sort of keyword at the beginning then we have so well this is different we have a name of the function and then we have a parentheses so it's kind of similar syntax and then we have these uh brackets here where our code will go so with the function we have to give it a name here and in the parentheses we don't actually have to put anything in there we'll talk about that in just a second let's go ahead and print something hello world this is my first function we're going to end that statement with the semicolon and press enter and what you'll notice is that it doesn't do anything and this is different from what we've talked about with some of the previous concepts where everything happens at once so remember if we you know declare a variable set it equal to 20 something like that when we press enter the variable is declared and it's assigned at the same time and with functions what's happening is we break out those steps into two steps so up here what we've done is we've declared the function but what we haven't done is called this function or what we call invoked this function those are kind of synonymous terms in order to do that we just grab the name of the function which as you can see similar to this variable this my function uh word right here this is just a variable that the computer remembers so since the computer remembers it since we already declared it it's going to uh recognize it when we type it out so if we said type of my function it's going to give us function because it remembers what that is and to execute this so i'm kind of using all these different words to execute a function to call a function and to invoke a function for our purposes they are all the same thing so anyways we call the function using these two parentheses right here and a semicolon and when we press enter it's going to say hello world this is my first function because it ran the code within the function so just to review with functions you first have to declare it and then you have to call invoke or execute it all the same kind of terminology now there is one way that we can declare and call the function in the same step and although you're not going to use these all that often i do want to show you them just so that you have an awareness of them and what these are called is an immediately invoked function so let's say that we write another function so another function here and we enter down we say console.log hello now if we press enter this is going to just declare it but all we have to do is wrap this entire function in some parentheses and then put our parentheses that we use to call it at the end semicolon and you're going to get this function to declare and execute at the same time obviously the computer does it in different steps but for our purposes it's happening all at once this is what we call an immediately invoked function and like i said you're not going to use this a ton but just be aware of it you might see it pop up in some stack overflow answers or you might even use it at some point so we got a basic function but it's not useful like what are you going to do with the function that we just wrote obviously there's more to it and the thing that i want to introduce next is the concept of parameters and arguments and what i would say with these two terms is they're kind of two sides of the same coin and once you get into more advanced programming programmers are probably going to be using these parameters and arguments terms interchangeably because like i said they're two sides of the same coin but for our purposes we're going to distinguish between these so let's write a function we'll say my function again and it will enter down onto a new line now what i want to do to add into this parentheses is going to be something called a parameter now these parameters are basically saying uh to the computer hey computer um i'm going to pass in uh some sort of values into this function so be ready for them so basically what i'm going to say to this function is hey i'm going to pass in some number and then i'm going to pass in some string value now obviously with javascript it's a dynamically typed language so you don't have to say like some number or some string this is just an example maybe we could even say something like param 1 and param 2. so let's just go with that these are arbitrary names that we have and then what i want to do is console.log param1 and then we'll go on to a new line up i accidentally messed that up let me go ahead and try this once more oh we really messed up here okay one second let's refresh the browser clear it out and then we need to enter to a new line and console.log param two all right so what we're doing here is we're taking the value of param one in the value of param2 which in this case within the function these are variables these are variables that we can retrieve the value from and print out so we're going to declare that function well as you can see it's a little bit later in the day here so i had to take a break but getting back to what we were talking about with these parameters so the question is what are they equal to what is param1 and pram 2 equal to well the answer to that is we don't know yet and we don't know because we haven't assigned them as what we call arguments so we know that this is a declared function but we still have to call or invoke it and to do that you know how to do that you just put in the little parentheses at the end and you'd press enter now if we just press enter right now it's going to return undefined because we didn't pass in any parameters so when this code tries to execute and it tries to reference these parameter variables there's no value to grab because we never gave it a value so to answer the question of what they represent well they represent the arguments that you pass into the function so if we have this function we can pass in say a number and then some string of some sort and now these are what we call the arguments now like i said arguments and parameters are kind of the you know two sides of the same coin but in this case whatever we pass in the values we pass in are called the arguments and then the initial variables that we were declaring would be the parameters so 20 is going to correspond to param 1 and then some string is going to correspond to param 2. so we should get 20 and then some string printed out when we run this function don't worry about this little undefined here that's just kind of part of the console we got our 20 and some string printed in order according to the function that we wrote so that's just a basic overview of arguments and parameters now the next thing that we're going to talk about is a brief introduction to scopes in javascript so when i say scope what i'm generally referring to is where these variables are available in the code so in other words we noticed that param1 and param2 were used within this function but if we were to just try to print param1 into the console it's not going to work and the reason is param1 is not available in the global scope it's only available in the function scope which is any code that's written between these two little brackets right here so we pass in these values but these are only available to the function it's not available to the rest of the code that you're writing in the file or the console that you know you're writing the code in so that's just a basic introduction to scopes there's a lot to this topic and as i said we're not trying to cover every last detail of javascript that's something that i think you'll have to fill in kind of outside of this tutorial and as we do practice problems and get into some more like real world examples you'll start to understand these scopes and i'll try to you know point them out as we go through like i said the you don't know javascript series is a great way to get a super deep dive into things topics like the topic of scopes but beware it's very complicated and there will be a lot of confusion if you kind of go down that route up until this time i've shown you how to write a function as we see on the screen right now but there is actually another way to write a function and let me show you how that's done so let's go ahead and clear this this console and we'll create a function just like we are used to creating so no parameters going in there just a basic function and we'll console.log something all right so that's our basic function now another way that we could write this is by assigning it to a variable so in this case the my function variable is already defined and it represents a function so we can't use that in our variable declaration but we can say something like another function and then we want to set that equal to a function so this is just a normal variable and all we have to do is say function define it and we'll console.log another thing all right so we set that equal to another function and if we say type of another function it's going to give us the type of function now i'm saying this word a lot so i gotta slow down here but anyways if we wanted to invoke this function or call it it works the same way that we were using it before so just like we call my function just like this and it prints out something we can invoke another function the same exact way even though it is a variable now there are a few little subtle differences between these two ways of writing functions and like i said it gets into some details that we're not quite ready for and i'm just going to leave out for now it's not and these details are not essential for you to write good code and get it up and running they're more like edge cases where your code doesn't work and you can't figure out why well it's because of this subtle nuance so i wouldn't worry about it too much all right so we've got these two different ways the main difference that i want you to recognize is the fact that this function right here does not have a name this is what we call an anonymous function and the reason we can use an anonymous function is because we are assigning it to a variable so we're giving it a name now up here we just do it all at once we just give it a name and we're done so if you tried to write a function without a name so something like this you're not going to get it to work because this is an anonymous function and you need to declare some sort of name and you see the syntax error it says function statement requires a name so that is not valid because it's anonymous you have to assign it to a variable now there is another way to write functions and this is a little bit tricky but i'd like to cover it because you're going to be using this next type of writing a function a ton so as of 2015 when the es6 standard came out so if you remember back from i think lesson 2 we talked about ecmascript standard and how there's a standards body that basically defines the spec for the javascript language well they upgraded that spec in 2015 with es6 and they came out with something called a arrow function so that arrow function looks like this so we'll say arrow function equals and then we'll put some parentheses we'll put an arrow and then write it just like normal so we'll say i am an arrow function all right so we press enter and like our other functions we can call it or invoke it the same way so we just press arrow function with the two parentheses at the end and boom it executes so it's functionally equivalent in a way again there's some subtle nuances that we're not going to get into but this arrow function is mainly here so it improves readability and another benefit that it has is it really helps out with the scopes of your you know your code and specifically the this keyword which we'll get into much later but it does improve on that that's why they came out with it in the first place so this is a very um different way of writing a function but here's the basic syntax you just have this little parentheses which is basically here's where you put the parameters then you have this arrow that's why they call it an arrow function so an equal sign and then a right caret or greater than sign and then you have these two brackets just like you would any function once again you cannot just use this alone this is kind of similar to that anonymous function we looked at and you can't just define a function like this and you know this this won't work because it doesn't have a name so you don't have a name specified it didn't throw an error like the previous one but you have no way of referencing back to this you don't know how to actually call it because there's no name for it so you have to assign these anonymous functions and arrow functions to some sort of variable these arrow functions or anonymous functions can also be used in something called a callback but again that's going to be a little bit later down the road all right from now on i'm going to be focusing a lot on these arrow functions i'm going to be writing them for the most part now obviously if you're defining a function on its own you have to write it you know with the normal syntax so you have to write it with the name and then the parentheses just like this but for a lot of cases we can just assign it to a variable and i'd like to get you practicing the arrow functions because you're going to use them so much so the next thing that we're going to talk about here is the concept of return values in functions so far we haven't returned anything from our functions and if we're writing these so let's say my function save it to a variable and use the arrow function syntax and then we just console.log something all right very simple my function does not actually return anything so if we were to assign a result variable to it so we're going to say my function and then we're going to call it so remember this is just executing the function we defined above and then it's assigning the result to that function to the result variable so we press enter and this is just the console.log that was executed as part of the function but if we type out result it's going to give us undefined and the reason is because our function didn't return anything so if we want our function to produce a value we have to put a return statement in there all right so let's go ahead and do that return function that's what i'm going to name it here that's just the variable we'll use the arrow syntax enter down and this time we're going to return a value of 20 and we also could have returned you know some string or we also could have returned a variable so if we wanted to you know make a variable called a and that equals 20. oh i accidentally hit enter there but if we wanted to we could come down here and return a now it's going to bug out on me because i've already declared it like right here on accident so let's let's refresh the page real quick so that we can get back into this so um we'll just say my function this time we've got the fat arrow function enter down we'll define a variable set it to 20 and then we will return that variable now we could also return like some sort of expression so what if we wanted to return a is greater than 30 which it's not obviously so this would return false so let's just see you know how these different return types affect the function we're just going to return a which will be 20 and now we want to set that equal to a result variable so we'll get my function we're going to execute it and then assign the result to that result variable and now when we print out the result it's going to equal 20 because it took the result value of the function and assigned it to that variable like i said you can do expressions here so if we wanted to define another function we'll just put 2 at the end so it's unique and then we want to say a is less than 30 which it is this will return true i'm just going to put a comment there and therefore we would expect that this whole function is going to evaluate this last expression here and then return it so let's press enter and then we'll say result 2 is equal to my function 2 and then if we get the value it's going to be true so you can see that there's a lot of different things that we can return from a function heck we could even return a function from a function but that's getting into way too much detail and it's not necessary for what we're trying to do so i'm going to skip that hopefully this gives you a brief overview of return values now the last thing that we have to talk about is what happens after the return value so let's say that we had i'm going to go ahead and clear the screen we'll say improper return function and then we're going to set that equal to our arrow function and we're going to return 20. or no let's go ahead and set a variable um i messed this up again it's kind of tough working in the console but we will eventually get to a code editor which is going to be a lot easier on us so let's say my function do this one more time and in this case let's define a variable so we'll say let my number equal 20 and then we're going to return my number now let's enter down onto the next line and then we'll say my number equals 50. all right so we're going to press enter and it's going to give us an error it says unreachable code after the return statement you won't get this from all like this is just in the console you may not get this error anywhere else but this is giving us a hint that we've done something wrong here but it is still valid so basically what we're saying here is we're going to run the function it's going to return 20 and that's because we've returned the value before we reassigned it so this is valid code to write but it's useless because this last line right here is never going to be reached no matter what happens it's always going to return beforehand and you'll never return a value of 50. so just know that for the return values whatever happens after it doesn't matter and you pretty much will never have a return value before the end of your function so let's say that we have a global variable we'll just say something like mynumber equals 20. all right so we're going to set that in the global scope again scopes are going to get important here soon so just kind of keep that in the back of your head now we're going to define a function so we're going to say let's go ahead and use let because in case i screw up again and we have to reassign it so we're going to go ahead and say my function and we're going to set that equal to an arrow function now we're going to say if my number so this kind of seems like going against what i told you earlier about scopes so if there's a parameter that has a function scope it cannot be accessed at the global level but if you have a global variable you can access it from within a function so it goes one way but not the other when it when it comes to scopes so since this isn't a it's in technically a higher scope than the function has it the function can actually recognize what this variable is equal to so we're going to say if my number and then we're going to enter down and actually i need an expression here if my number is we'll say less than 15 then i want to return returned the function early all right and then we're going to enter down and then we're going to return 40. these are all random numbers i'm making up here but it's just trying to show you that if you have a condition where you know if the number is less than 15 which it is not in this case you can actually return multiple things from the same function so my number is not less than 15 so we're going to actually skip this return function in return 40. but if we change that so let's say we change that to less than 50 and 20 is left less than 50. so we're going to actually return this function early and we'll never reach the second return statement so let's go ahead and enter that down and execute it and you'll see that we get that return to the function early output so you can have multiple return statements within a function just you can't have like two of them on the same line so if i were to recreate this function and maybe have you know like a return statement of 50 and then return 40 right after it both of these code paths are going to be reached so this doesn't make any sense you only want to return one value so that's just an overview of return values we're going to get a lot more into this as we start writing some more realistic code just wanted to give you a brief overview the last thing that i want to do prior to getting into the 25 practice exercises or coding challenges i have for you i want to talk a little bit about objects and functions together so this is a little bit of a confusing topic but it's going to hopefully help you understand when we start talking about the built-in functions of javascript we're going to talk about that in a few minutes hopefully this will help you understand that better so let's go ahead and define a function we're going to call it my function again and then let's i don't know return a value of 20 from it so this is a very simple function we'll go ahead and execute it just to demonstrate it returns 20. now what if i wanted to set this equal to another variable i can actually do that even though it's already called my function so i can say um you know alias you know an alias is just another name for something so we'll say alias variable equals my function all right so if we press enter now alias variable is exactly equal to this function declaration that we made above so if we wanted to execute that as alias variable we're going to get the same answer of 20. so we can assign it to different variables which means that we can also assign it to object properties so let's go ahead and clear the well i guess we don't need to clear the screen let's go ahead and create an object so we'll say my object and then we'll set that equal to prop 1 we'll just give it some random value just to have something there and then we'll say prop 2 is going to be equal to my function all right so we have declared my object in prop 2 just a random property name that i've given it is going to be exactly equal to the function declaration just like we made this alias variable equal to the function declaration so what does this mean well we can actually execute the function by accessing a value of or by accessing a property of the object so if we come down here and let's just go ahead and say my object and then we need to get a property from the object so let's go ahead and get property2 and you'll see in the autocomplete down here it's pretty much telling us that that is equal to my function now if we wanted to execute it we can still put those little parentheses at the end and it's going to execute and give us the same answer so what we did here was we grabbed an object we grabbed a property off of that object using the dot notation and then once we had that we just threw those little parentheses at the end and executed it so i just wanted to get you familiar with this because what we're about to talk about is the built-in functions of javascript so javascript has a bunch of built-in functions and objects and all sorts of other stuff that we can use without having to write any code and if you go to there's there's several resources that you can use to see these i'm on tutorials point which has a good list of them but they're they're all over documented pretty much everywhere and within javascript there are different data types as we have talked about so you have strings you have booleans you have numbers and all of these different data types will actually have some built-in functions that you can call on top of them so if we go here and just look at some of the different methods so method is another i guess synonym for function we can call like it's not completely synonymous but if you have like an object and then you have a function attached to that object as we just showed that's called an object method so it's kind of the same thing and you can see that we have number methods we have boolean methods and we have string methods and we have array methods probably down here somewhere so array methods and depending on the variable type that you're working with you can use all of these to modify your your variables and just take a bunch of shortcuts with your code so let me go ahead and show you a few of these just to give you an idea of how they work because when we go into the coding challenges throughout this course we're going to be using these uh as much as we can now the first thing i'll say here is don't try to memorize these these are not to be memorized there is documentation for a reason so that you can google it and look it up when you need it you'll pick up on a few of them over time but for the most part i only have a few of these actually committed to memory all right so let's go ahead and try out a few string methods so let's say that we wanted to i don't know use this replace method we go back to the console let's clear everything out and let's define ourself a string so we'll say my string equals um zac and then we're going to use the replace um method so maybe i spelled my name wrong that's how you spell my name but what if there's a k at the end so i might want to replace it so i can say my string dot replace and then i'm going to replace the h with a k and when we press enter it's going to return zach with a k and now my string is actually going to still be equal to the original because it didn't replace the value it just returned a new value so it doesn't alter the original value here it just returns a new one so if we wanted to say new name we can just say my string dot replace h and k and then we set that uh equal to new name and now now my new name is going to be spelled differently now something that i want you to notice when we're looking at this is that we're using the same notation here that we used earlier when we accessed that property of the object and executed it as a function now this works a little bit differently than that but you can kind of think of it that way so maybe the my string is considered your object and then you use dot notation to get a property off of that object called replace which happens to be a function that takes two parameters and the first parameter it takes is the letter or string that you want to find within your string so i want to find the letter h within my string and then i want to replace it with the string k just a letter so my string will be replaced so these are the parameters and if you were to look at the replace method and you go ahead and click on it it gives you a description of this and we're going to learn how to you know read through documentation a little bit later but anyways you can go down here and just see what kind of parameters are required for this so here's the syntax and it gives you the parameters that you want to search for then the parameters you want to replace it with a couple examples some output all that stuff so that's how i've you know figured out how to use that again we're going to go through it quite a bit as we go through the coding challenges i'll just go through a couple more examples just to get you comfortable with what's going on here so let's go ahead and use my string once again and this time i want to use the string method called to uppercase and you can see what that does is it returns us the uppercase version of our variable now i know we got a lot of different versions going on here so let's go ahead and clear that out and reprint my string so that is what my string variable is equal to right now we can also chain the methods together so this is a little bit of an odd concept to someone starting out but you'll see how this actually works um once we get into the challenges so let me just do a simple example here we have my string equal to zach but let's go ahead and say uh we want a new string or something so let's call it um i don't know let's make a new one we'll just call it str for string and this one we're going to say hello world in there so that that's the string right there and what we want to do is first make it uppercase so we'll say to uppercase and we'll execute that you know method that sits on the string object and that's going to output us hello world as it's kind of telling us here now we can put another dot there and we can chain these methods on top of each other so i'm going to use another method called split and what split does is it's going to take whatever delimiter that you put as a as a parameter or not a parameter as an argument so if we put in a space right here it's going to look at the string and it's going to say there's one space in there so i want to split hello and world apart from each other and then what it does there as you can see down here is it actually changes the data type from a string to an array where the first uh part of that array is the first the hello and the second is world so let's set this equal to a variable we'll just say a result variable is what we want and our result variable is now an array and from there we can actually do array methods so let's go ahead and go back to the documentation here and let's scroll down to some of our array methods that we can use so what would be a good one to use so we'll use the index of method so this just basically gives us the index of a value so we'll say result dot index of and then i want to search for the word world and when we press enter it's going to return us 1 because this is an index of 0 and this is an index of 1 and that matches the string that we are searching for that we passed in as an argument to this function now of course you don't have to do this by chaining them together you can do it in separate steps so in the case of above we made this to uppercase and then we split it into an array in a single step but if it gets too complex for you and you just want to stick to the basics you can do this in different steps so we'll say first let's see what string is equal to it's still equal to hello world because we you know while we did this right here we didn't modify the original variable we just assigned it to a new one so string still represents hello world and in this case we'll say step one variable we want to take that and we want to make it uppercase alright so we'll make it uppercase and then maybe just type it out to make sure that it worked now we're going to do step two right here where we say step one so we want to grab whatever values in step one and then we want to split that based on a space alright so we set that to step two and then step two we can print out and we see that it's an array and then we come to say step three and we'll say step two whatever value is in that variable and then we'll say index of like we did above and then we'll search for the word world and we'll set that to step three and we get an index of one so you can see we can split these out into different steps but i just wanted you to know that as you see some different syntax when you're reading through code this is a valid way to use these built-in methods in javascript all right there's a lot here there's a lot going on in this video and i would never expect you to take it all in at once and process it and understand every last bit of it and that is why we're going to use a bunch of practice exercises or coding challenges to brush up on these skills and really cement some of the concepts we've talked about variables we've talked about operators we've talked about conditionals so if else if else or else if doing a tongue twister there we've talked about loops and we've talked about functions so there's a lot going on here and to combine it all together and actually write code i think it's useful to go through some exercises in this video we're going to go through 25 maybe a few more we'll see how it goes but we're going to go through some coding challenges and these are going to be at the very beginner level so like very beginner level of coding and what i want to do with this is walk you through my own thought process and help you understand how i'm thinking about this and how you should use the previous lessons that we have gone through so lesson uh three and four where we talk about the basics of javascript i want to apply those to actual coding challenges like i've said throughout this series i'm not taking you extremely deep into the javascript programming language there are other people who have done that and i think at this point when you're just trying to get off the ground digging into complex topics like you know hoisting and advanced scopes and stuff like that it's not necessary for when you're just starting and you can fill in the blanks as you go later on and partially why i'm making this video with these challenges is to help you do that so if i haven't talked about it in the previous lessons hopefully we'll kind of fill in the blanks here with these challenges and to do so we're going to do this on a site called code wars and if you go here pull up the screen this is going to be the collection that i've put together so there's 25 exercises here that i put into a collection we'll see how it goes i might add a few more um if we blow through these but basically just go to hit the link in the description to find this uh series of questions and then get to this page right here create a profile go ahead and follow me too let's see what my profile is zachgall just look me up follow me i'll follow you back so that we can do this together and then once you get to the collection again link in the description you'll go to this uh top left corner and click train now and when we click this it's gonna drop us in to each of the problems uh that i have basically chosen for this video and i just want to give you a quick tour of code wars if you've never been on here before i actually just started using it for this purpose just to go through some practice exercises but basically on the left side here we have the instructions so it's going to tell us the instructions this code does not execute properly figure out why basically fix it you can see the output here this is when we actually run our test so if we run our test obviously it's going to fail and this is where we see the output and then over here is where we write our solution so we can write valid javascript code over in this terminal over here and then finally at the bottom this is the the sample test that we can look at so um obviously this is a little bit more advanced than we're um probably accustomed to at this point if you're watching this video but this is going to basically be a bunch of code that actually tests the code that you write so it takes the inputs that or it takes a set of inputs it puts those inputs through the code that you've written and then it gets the outputs and checks to see whether that matches the expected output so that's just a brief tour just a few other things you'll notice that we're writing in the javascript language that's what this video is about uh node is going to be the runtime so node.js is kind of the back end version of javascript but you know just like we've been working on in this series we can you know this is the browser dev tools console and this would be you know the same code that we're writing here so nothing new here and then finally this vim versus emacs thing if you click either of these it's going to actually drop you or it it's going to create this editor right here into like say a vim editor i have a video on this if you want to check out the vim code editor or text editor but don't click this for now that's a whole different uh ball game that we would get into if we started using those so keep those unchecked all right so from this point we're gonna go through the the exercises i'm gonna walk you through how i'm thinking about them the first one is rather simple if you've been following along with this roadmap series you'll know exactly what we need to do here it's saying this doesn't execute how do we fix it well right now if we you know ran it and did the output we ran it earlier it says it expected undefined to equal 1. so basically that's saying that we haven't returned anything from this function and therefore we just need to add a return statement right here and then we can test it and it should pass so we pass it go ahead and click attempt at the bottom right corner it passed all of our tests so we'll submit it all right once you submit a solution and you did it correctly it's going to show you other people's solutions to the same problem and this can be useful but also detrimental so listen up here the way that this can be useful is by scrolling down and you know just seeing the possibilities because you can solve every one of these problems in many different ways and so sometimes it's fun to see how other people have solved them and it also can open your eyes to different types of thinking that you you know wouldn't have thought about so it is good in that way it's not good because sometimes people will solve these in very clever ways and when i say clever that does not mean the best way now i'm not a competitive programmer i don't claim to be i don't really have any plans on becoming one but when you get into the world of competitive programming it's all about optimizing your solution writing the least amount of code or the fastest code um that kind of thing and i'm not that i just want to write some code that helps you understand the solution and help you read the code in the simplest way possible and some of these solutions down here for example this one right here this is unnecessary um there has you know nobody said that you have to check and validate all of the argument inputs so in this case a and b are the inputs to our function and what this person has done is basically checked to make sure that you know the the inputs are not some you know invalid input it makes sure that they're a number and this is totally unnecessary for this solution it never told us that we have to check that and this is just overly complicated in my opinion you'll see that there's a lot of votes for the clever and in many cases when you see a lot of votes for the clever solution it's probably not geared towards the beginner it's probably not what we want to focus on right now at least now i will point out one that's a little bit useful for us and that's down here so this would be our solution that's what we came up with we just added a return statement and called it a day now there's another way to do this that is actually it's it's very clever but it's also something that we talked about in a previous lesson so that i think as the previous video we actually might have mentioned this at one point but basically if you have an arrow function which is what we don't have an arrow function in the original solution we just have this you know standard function a named function but we could set this function equal to a variable called multiply and then we set it equal to an arrow function and with an arrow function if you put it all on one line you don't actually have to put a return statement in there so this is a nice nice thing to look at because it gives you kind of opens up your world to understanding oh i can write it like this as well all right so enough on this we're not going to spend this much time on every challenge but i wanted to walk through uh how you think about these solutions here all right so if you were if you clicked that button so let me actually go back to make sure that we're clear on this we'll go to collections go to the lesson five practice challenges and if you just click on one of these challenges it's going to take you to that challenge but when you click next it's not going to keep you within the collection so make sure that you've clicked this train now button so that when you solve the problem and click next it takes you to the next one in the collection so i guess we got to solve this one again real quick so we'll attempt it with that solution we passed it let's submit it all right and now we can pre press next uh next kata or kata i don't actually know how to say that i'm sure there's a reference to that that i'm not getting be sure to leave that in the comments if you do know what it means anyways this is the next problem that we have so let's go ahead and actually is this yeah this is it so let's train again i guess i've already opened this at one point um yeah when i was putting the collection together so the function is not returning the correct values can you figure out why so this is going to actually get us into the switch case statement that we talked about um just in the previous couple videos and if you remember with the switch case statement this is basically like saying if and then else if and then a bunch of else ifs to test a specific condition if you remember from the switch case statement syntax we are missing something right here that is causing our solution to be wrong and i think what's gonna happen if we execute this we're gonna get neptune uh every time so let's let's see if that's the case yeah so we got neptune every single time and the reason that's happening is we don't have any break statements here so in other words we're getting the id um passed in correctly so that's passed in as an argument id is the parameter that we're defining within the function and then what it's doing is it's looking at that id so that's in the switch statement and it's saying what does the id equal and then once it figures that out it goes to all the cases and so it says does the id equal one okay if it does we'll set the name equal to mercury and then we'll return the name down here but the problem is since there's no break statement it's always going to hit the last case so let's say that the id um you know we go down to the test and they're passing in two as an argument and we're expecting that that will return venus so if we you know pass in an id of two it's going to get passed in and then we're going to look at case one and say okay id does not equal one so let's go to the next one all right id does equal 2 so let's set the name equal to venus but instead of breaking and coming down to the return statement and returning that answer it doesn't do anything it's just going to keep going down all of the cases so it's going to say does case equal 3 does case equal 4 does case equal 5 so on and so forth until it hits the last one so we need to add some break statements in here and all we have to do is just put in break with a semicolon and we'll do that for all of these lines all right so we've got our break statement let's test this once more and this time we're passing it so that's all we needed to do we just this was purely a syntax error and it's something that you would have had to kind of look up the documentation for a switch case statement or watch my previous lesson to figure out so let's go ahead and attempt it we know it's going to pass and we'll submit it all right let's see what everyone else was doing there's some clever solutions here obviously this one is very clever but i would not recommend doing this at our skill level right now so looks like we did it did it okay in this case this person has opted to use a return statement which is valid but a break statement works just as well so you can see that this was the i guess the top solution most common and that's kind of what we used there the one thing i'll point out is i put a break statement after neptune but that wasn't really necessary since it's the last one all right we'll go to the next challenge this is reversing a string so let's go ahead and train on this so basically all we're doing is taking this string and reversing it and i know how to solve this solution with just a single line but i don't actually know exactly how to do that i can take a guess so let me try and just return string dot reverse and see if that works okay so reverse is not a function as i kind of suspected so in javascript as we talked about in the previous video there's a bunch of built-in functions that come with javascript and you can look these up so we'll just say javascript built-in functions and then we'll we'll go to tutorials point i like this one it's pretty simple and then if we go to the string methods that's where we want to go because what we're trying to do is reverse a string so any time that you're trying to modify a certain data type you can first look to those built-in methods so you don't have to do something manually so in this case we're working with a string and we need to reverse it so let's go ahead and see what we can do using the string methods and it turns out if we're just looking through these different string methods there's nothing that says reverse here um i had actually for some reason thought that we did have a reverse method don't know how that slipped my mind but we don't so we have to think of another solution now realistically if we were solving this problem um you know in the real world where we had to write some code that reversed the string what i'm going to do is this i'm opening up google and then i'm going to type in how to reverse a string javascript all right and usually there's going to be a stack overflow post because stack overflows where you basically get the answer to anything when you're coding and what's going to happen is we're going to find a solution here so i wasn't that far off we actually do have a reverse method but this is not going to be entirely apparent to you if you're like a complete beginner but what we're doing in this solution here is we're actually converting this to an array first so let's go back this is all i need to see for now let's go back to our documentation and take a look at the array methods so if we have a data type of an array we can actually reverse it so uh looks like there's a reverse method right here and i'm going to go ahead and go to the dev console just right click anywhere on the screen let's just show you how to do that right click inspect element if you've been watching the series this is not news to you and now we can type away with any javascript so let's see how we can do this with an array we'll just set an array equal to 1 2 3 and if we want to reverse this array we can just say array dot reverse and it's going to reverse the values in the array so basically what this stack overflow solution is telling us is that we first need to convert the string into an array by letter and then we need to reverse it and then we need to join it back into a string so this is a you know one step solution where we can basically do it all in one step we talked about chaining these built-in methods in just the prior video but i want to break this out into individual steps just for clarity so let's go ahead and do that right now the first thing we want to do is split the string into an array so basically it's going to take every character in the string and it's going to make each character a value of that array so again let's come back to our console let's say that we have a string and that's equal to hello all right and then we want to say string dot what were we using here we're using split and we're going to just split it with an empty character so if i can get back here so we'll say string.split and we'll put in just an empty character and that's going to give us an array where we have each letter being separated in the array all right so now that we have the array we can split or we can reverse it so let's just go back and do that here so the first thing we have to do is put it into array so string dot split and then we just need to pass in an empty character and then from there we can reverse it so as we saw so we'll say we'll save it into a reversed variable so we'll say array dot reverse alright so that's going to assign it there and then often times when i'm solving a problem like this i'm going to write some code and then actually test it out before i move on so let me go ahead and console.log the reversed value and then we'll click test which is obviously going to fail but what we do see is that we get some output so you'll see that we logged this to the console and this to the console which basically tells us that we have our like our reversed variable is an array and that array is reversed from the original string so the last thing we need to do since we have it in the form of an array data type is we need to take that reversed array and just combine it together into a string and to do that i think all we have to do is join it so if we do string dot split and then we say join it's going to join it by a comma so we actually will have to replace those commas let's see how the stack overflow solution did it okay let's see and we have to when we join it we have to also enter that empty string right there so that's just a little tricky part to this so let's go back to our problem and then we'll say final answer equals the reversed array so this is an array and then we join it by that empty character and then we go ahead and return the final answer and let's go ahead and test that you can see that we passed all the tests so let's attempt it i think we'll be fine here and we solved it so that's just how we take it out into different steps of course we could have solved this by just saying we want to do string dot split dot reverse join and this we can delete all these uh intermediate variables right here we can test this and it's gonna pass everything let's go ahead and attempt it and submit it so like i said there's a couple different ways to solve it but um you might be asking at this point well you're cheating like you went on stack overflow and figured this out that way and well yes if i was doing a programming competition um i would be cheating and that's you know that's why i'm not a competitive programmer i'd never want to be because google is you know your best friend when you're a programmer and it is not cheating to google something so um we'll try to do more of these on our own for the remainder of the video but for now i just really wanted to beat that into your head that it is okay to go to google and search something like that and that's how pretty much everyone that operates in a real world environment is going to do so just a tidbit of knowledge there take it for what it is all right so we'll go to the next challenge see what we got here okay even or odd let's see what we are trying to solve so it says create a function or write a script in shell that's not something that we're going to do that takes an integer as an argument and returns even so notice that they're asking for a string a capitalized string that says even for even numbers or uh capitalized odd for odd numbers all right so let's see what we can do here the first thing i'm thinking about is maybe a modulus operator but i don't even think that we [Music] need to do that or do we yeah that is correct so a modulus operator if we were to divide something by two right okay i don't know what i was thinking here so just divide it by two right so um let's let's go over to our little playground here and refresh just so that we can test a few things out so if we got 20 divided by 2 that comes out to an even number if we have 21 which is odd divided by 2 we have a decimal now we would have to complete some additional steps to actually figure out which one's a decimal which one's a whole number so i kind of like the idea of the modulus operator because if we do 20 instead of divided by 2 but modulus 2 it's going to give us 0 but 21 modulus 2 is going to give us 1. so basically every time we have an even number doing modulus 2 is going to return 0 because there's zero remainder if we do that division operation so in previous lessons we talked about this modulus operator in my video video about operators and basically what it's doing is it's doing a division problem and it's returning what the remainder of that division problem is so as we know going back to basic math if you divide something and there's no and there it goes into it evenly there's going to be a remainder of 0 which we're going to see for any even number so if we do 40 modulus 2 that's 0 if we do 2 modulus 2 that's 0. so that's basically how we're going to solve this and we could do some intermediate steps but we can actually just return the solution actually no we do have to do some intermediate steps so if we need a conditional in here we want to say if the number so we're grabbing that from the parameter and then you know the test cases are going to pass in different arguments and then this parameter called number just think of it as as a variable available to this function is going to be different per the test case so replace that number with whatever argument you have and then what we want to do is write an expression that says the number modulus 2 all right so we want to say if that is equal to so we'll use triple equals to check both the value and the data type to be the same and we want to set that equal to 0. so if the number modulus 2 equals 0 then we know that it is an even number so we'll drop down into our if statement and we'll return what we need to return based on the instructions over here called even all right and then we'll put an else statement so we know that if it's not even it is odd so we're going to just return in that case and let's test this out all right we passed all our tests let's go ahead and attempt it and we passed all of our tests so let's submit this answer and see what everyone else did to answer this i'm sure there's some more clever solution than what i came up with all right so this is just using the ternary operator we talked about this very briefly in the operators video but it's basically just a shorthand clever way to write an if-else statement and we could have used this but i just don't want to confuse anyone unnecessarily this looks to be our solution um actually there's not not much variance here so these are just abbreviated ways abbreviated ways to write our function they're just using the arrow functions rather than our classic function here so um we did pretty good on that one let's go to the next challenge all right we're counting sheep in this challenge see what we got here okay we are returning the number or the count of the vowels in the given string we will consider a e i o u as vowels for this uh problem but y is not going to be in there okay so that's good to know the input string will only consist of lowercase letters and or spaces all right so this is another point that i really want to bring up um if you're you know just starting out you're a beginner and you're solving problems this is kind of how an interview question might be laid out where the last part is going to tell you something about the input so obviously the the functions that we have been writing here they're being executed by something and that's something are the automated test these are this is a sample test down here but there's code that's going to run when we actually click this test button this site is going to execute our function with a bunch of different parameters to make sure that we implemented it correctly you know you don't want to just test it with one set of parameters you want to put in a bunch of different things and with that we could have an infinite number of things passed into this function as arguments and oftentimes in more complex problems and harder challenges they're going to challenge you to handle all of the different edge cases that might come up and when i say edge cases you know it might be something like okay what happens if i pass in an undefined value as my argument or what happens if i pass in um you know a number instead of a string into this as an argument well all these things need to be handled by your code so this last sentence here is it doesn't seem very useful and you might even ask like why is it here but this is telling you something about the input it's basically saying to us as the programmer it's saying hey this is only going to give you lowercase letters and or spaces and that's basically saying in other words we're not going to pass in numbers we're not going to pass in undefined values null values boolean values you know all you need to worry about are you know single lowercase letters and spaces all right so that is very useful information it's going to save us a lot of time when we're solving this problem all right so it says enter your magic here spelled incorrectly at least in english i don't know how spell it in other languages anyways let's see what we got here so we have a starting point now one thing i'm going to fix here they're using the var keyword and in the lessons i've basically told you we're not using this and i also stand by that so let's go ahead and set that equal to let because it looks like we're going to have to reassign this at some point all right so this is actually a fairly easy solution once you've been programming for a little bit how i see us solving this is first let's put in our input into an array of some sort and then i want to loop over that array and check if it's a vowel all right so let's go ahead and do that there's actually a lot of ways that you can solve this but let's just stay consistent with what we did in the previous one so we'll take we'll we'll convert this to an array by saying string dot split and then we want to pass in that empty character so if you remember what that does is it takes every letter of the string and it puts it out it expands it out into an array so going back to this tab we'll just say you know let's look at an array and then or no we'll look at a string so the string will be hello and then we can just say string dot split and then that's what it's basically going to do here so it's going to split it into an array and then from an array we know that we can write a loop so this is kind of the previous couple videos that we've been talking about loops so we're going to loop over every single value in that array so to do that we're going to say let i equal 0 this is just the syntax we use for looping and then we want to go for the length so the array that we just created has a certain length has a certain amount of characters but we don't know that beforehand so we're just going to pass in a ray dot length and then we're going to increment i using the increment operator all right so we're going through this array and now we're just going to write a simple if statement so we'll just say if let's say we'll grab from the array each of the indexes so remember i is going to increment it's going to go from 0 to 1 to 2 to 3 to 4 and all the way until it reaches the end of that array which has the individual letters of this you know input argument so we're going to grab that like basically this array um or arr bracket i is going to represent a single letter of that array so we're going to ask whether that equals um any of the vowels so there's there's actually some really short ways that you can write this but i'm going to go ahead and do this the clunky you know really drawn out way just because it's the most clear for a beginner so we'll say if the array or actually we can use a switch a switch statement um as we saw in a previous challenge so the the thing that we want to evaluate is that single letter and then we're going to come down here and we're going to say case does it equal a all right if it equals a then i want to take this val's count variable and increment it by 1. so that's what we're doing there and then we will break out of the statement remember that's very important all right so we're going to do this for all of the different vowels and we're just going to paste this code below here and it's going to get all messed up with formatting so let's see and like i said there is a much quicker solution and i'm sure that we'll see someone that used it but this is you know in the scope of what we've talked about already so i just want to stay consistent with what we've talked about you know not throw any huge curveballs here all right so we're getting to our last one let's pass that in we actually don't need a break statement for this last one here and so basically what what's going to happen here is we're going to loop through this array so basically we're going to look at every single letter in the array from that original string and we're going to ask does it equal a if it does we're going to increment the values count does it equal e increment the values count and if it doesn't equal any of those the val's count variable is going to stay where it is so at the end of this loop we're going to have a total count and then we just return it at the bottom so let's go ahead and run the test on this and see what we get you can see we passed one of the tests so let's go ahead and attempt it and we passed all five of them so we obviously got this correct so let's submit it guess it didn't submit so let's try that again sometimes this is a little bit slow it has to actually run a bunch of tests so all right so let's see what we got here the first answer very clever um this this gets into regular expressions which we haven't even come close to touching on and i don't want to so we'll skip that um this right here looks um a little bit similar to what we did so this is actually a double loop so we're actually looping through two different arrays i really don't want to get into that either because that that will make anyone's braden spin to be honest got some more regular expressions see i'm just looking for the one that i was thinking of that would be a lot okay there it is so we looped through this array and what we could have done is just checked instead of writing out this switch case statement um all we would had to do is put the vowels in an array and then we would have to you know take that array and use the includes method and then loop through all the letters and check if that letter was included in that val's array so that's what i was thinking of just a little bit easier way to do it looks like this person kind of solved it like us but like i said there's a ton of ways to solve each of these challenges you know we can solve them however we want so let's go to the next one looks like jenny's secret message i wonder what jenny's secret message is so let's train on this i gotta take a little break here solving all these these challenges tired all right here we go so jenny's secret message jenny has written a function that returns a greeting for a user however she's in love with johnny and would like to greet him slightly different that's romantic all right she added a special case to her function but she made a mistake so let's look at the function it looks like we're just trying to edit some code here all right the first thing i'm going to do here is just run the test see what we're getting kind of gives us some clues all right so now let's look at the code let's see what what's happening here is that we have a single parameter called name so that's basically saying all right this function expects a single argument to be passed through it when it's executed and that is called name then we're going to use that name to figure out if we're dealing with johnny if we're not dealing with johnny then we don't want to add you know any specialized message we just want to say hello to that name so up here this is how we would do that we're just concatenating a bunch of strings together so we're saying we have a hello string and then we're adding the name that is passed in and then an exclamation point so this is basically what we want to return for everyone except johnny all right so that's what we want to return there let's space it out a little bit and then if the name is johnny we want to return hello my love all we have to do here i think is reverse these so what's happening right now is we're going through this function and it never even gets to this if statement because we're already returning something so basically every time we run this function it's returning hello you know name with an exclamation point at the top it never checks to see if johnny is the name because that's like this code block down here just never gets reached so all we have to do is cut that from there and put that at the top so if the name equals johnny return hello my love so let's go ahead and test that and it's going to pass now something that you might be wondering about right here if you've been watching the videos is this if statement so normally we write an if statement with these brackets and then we would put the return right in there and that's how i prefer to see it but javascript has a bunch of little quirks and if you have a simple if statement like that we're just where you're just returning a single line you don't actually have to add the brackets that's just a syntactical thing this is totally fine if we run the test again it's going to pass and let's go ahead and attempt it so we passed the test let's go ahead and submit just going through a couple of the possible answers again this is another ternary or no it's not yeah this is using a ternary operator so it's basically enclosing that ternary operator within parentheses and then it's putting it in the middle of that string concatenation that we're doing so very clever but it's not something that we need to do totally unnecessary and it's honestly not not quite as readable as what we have done all the other ones these are uh rather simple this is a nice one this is a good solution if the name is johnny we return this else we return something else we kind of did something similar we just didn't add the else statement all right let's go to the next problem the next problem is n divisible by x and y let's see what we got here okay so we got quite a bit of quite a few examples create a function that checks if number n is divisible by two numbers x and y all inputs are positive non-zero digits so again this last sentence is useful because it's telling us hey we're not going to throw any you know crazy arguments through this function you don't have to worry about handling you know different types of data you know you don't have to worry about a string being passed into this data in that case you might have to convert it or just throw an error but in this case all inputs are positive non-zero digits so it's going to be from 1 to infinity basically all right so here are some of the examples we might as well use these so n equals 3 x equals 1 y equals 3. this is true because 3 is divisible by 1 and 3. all right n equals 12 x equals 2 y equals 6. true because 12 is divisible by 2 and 6. all right so it's basically you know looking at all these numbers trying to figure out if they're divisible by each other all right immediately i'm kind of thinking about that modulus operator again because anytime we look at something that says is it divisible by that comes in handy so and then also the other thing that i'm thinking about initially is that it's saying checks if number n is divisible by both of these numbers so this and is capitalized which tells me that we're probably going to be using this and operator at some point so just keeping that in mind as we solve it again all inputs are positive non-zero so basically what we're going to do is we're going to do two different math problems so we'll say result one is going to do is going to be equal to n divided by x and then result two is going to be n divided by y all right so we're just running both of the math problems and we're getting it into a result variable and then from here all we have to do is basically divide let's see so let's let's go ahead and console.log out these results just so that we can see some output that we're receiving so we will test it obviously everything failed but you can see what we were getting here so we're getting some decimals as we might expect if it doesn't go into it evenly now a good way to check whether a number is a whole number or a decimal is using that modulus operator and if you think about it if we divide a number by one it's going to always go into itself right unless it is a decimal so if we were to let's go over to our playground here if we divide this by 1 it's going to give us 20. 19 divided by 1 is 19. but 2.5 divided by 1 is 2.5 so it's not a whole number it's a decimal number and because of that the modulus operator is not going to return 0. so if we did 20 modulus 1 that's 0. 19 modulus 1 is 0. but 2.5 modulus 1 is 0.5 so any whole number is going to if we do modulus 1 is going to return us with 0. so we can use that in our solution here so we'll say if result 1 modulus 1 equals and then this is where that and statement comes in and result two modulus one equals zero then we know that we have a correct answer so let's see what it wants us to return again it says checks if it's divisible okay okay so we're returning true or false so in this case we're going to just return true and then if that's not true then we return false so if the if the first result the first division problem and the second division problem if both of those are not equal to zero when we use the modulus operator then we return false down here so let's test it see what we get looks like we passed that's great let's attempt it and you can see that we passed all the tests let's see what everyone else was doing here for this i'm going to guess that a lot of people solved it this way but of course there's always these clever folks out here that are just solving it in one line this one this is interesting right here i'm not going to go into it you know these are these are not totally relevant to us we solved it in a very straightforward way wasn't clever but it was very straightforward and that's all we're asking for here so feel free to look through these let's go on to the next challenge at this point i'm going to try to solve these a little bit quicker just so that we can blow through the rest of the video i'll point things out as we go of course but um i've kind of you know parked on a lot of the most important things like how to how to look at a test how to think about it how to google all that kind of stuff so we'll kind of we'll try to speed through these a little bit faster alright so this one is return negative in the simplest assignment you are given a number and have to make it or in this simple assignment you are given a number and have to make it negative but maybe the number is already negative all right so it's kind of implying to us that we have to check whether the number is negative first and then we have to apply some sort of operation on it so immediately we know that if we multiply something by a negative one then it turns it you know the opposite sign that it was so if we all we have to do is say if the number is less than zero we know that it's negative so we're just going to return the number because it's already negative otherwise if it's not less than zero then we're going to return the number multiplied by negative one and that should be the solution right there we passed i think this is going to work it's a pretty simple straightforward answer so again if you have caught on to this when we're using the if-else statements that's an opportunity to use a ternary operator and you're going to see that in some of these solutions so here is the ternary operator at work it's basically the same exact thing that we did has a little bit of a different thing so if you can just put a negative at the beginning of your variable to flip the sign of it that's just a shortcut in javascript but then there's another solution this is actually interesting if you use the built-in math function called the absolute method that's going to turn any number to a positive value so if we just turn everything that we get to a positive value and then reverse that to a negative we're always going to get a negative value so that that is a clever solution and honestly i think that's the best solution if you just thought of using this but of course if you're a beginner you're not going to think of using the math.absolute method because you probably don't know about it but again you can look up these javascript built-in functions and i'm sure that it this one might not show you yeah so we have the math methods down here and you could once again i'm on tutorials point i just searched for javascript built-in functions but we have these math methods and the first one is the absolute method so it returns the absolute value of a number and if you knew that you could think of it and just use that for our solution so anyways like i said we're not going for the most optimized solution we're going for the most straightforward and clear solution for a beginner all right so we're finding the smallest integer in the array for this one all right given an array of integers your solution should find the smallest integer all right so this looks like a perfect opportunity to write a loop and you can see in our solution here this is actually getting us into the concept of classes which we we have not talked about one bit so far but basically um each class is going to have you know several different instance methods um and then the find smallest int is actually an instance method so i'm not sure if we we might be able to edit this let me see well as you can see in the test it's instantiating this new class so we can't really do anything about it but i'm just telling you don't worry about the class right now all we have to do is um implement this method and we should be good so within this method we're going to get some arguments this is actually what we would call this would be params because that's what the params are what we define within the function declaration and the arguments or what we pass through it but again in those previous lessons i told you that arguments and parameters are two sides to the same coin and in this case that's been demonstrated there we're using args and params interchangeably all right so we'll just change it to params to say stay consistent here and then from here we're going to go ahead and loop through this array so the params that we're going to receive is an array of integers all right so and then it says at the bottom you can assume for the purpose of this challenge that the supplied array will not be empty so we don't have to handle the case where it's empty but it doesn't say anything well actually yeah it does we're going to get an array of integers so we're good so let's go ahead and assume that the params is an array so i actually want to name this something even more descriptive so we'll call this array of numbers just so that we can remember what we're doing and now we're going to make a for loop so basic for loop here and let's go ahead and go until we get to the end of this so we're going to grab the length of that array of numbers and then for each number in the array we're going to check if it is the smallest integer so to do this we actually have to declare a variable here that's going to keep track of the smallest integer as we loop through the array so let's just call this the smallest number and we're just going to declare it because we have not looped through any of the numbers yet so or actually yeah let's go ahead and declare it the smallest number that we can possibly get it's an array of integers and we might get negative numbers so let's just go ahead and initialize this at zero so just for comparison purposes you'll see what i mean in a second so as we loop through this array the way that we're going to get an individual number from the array is by using this syntax here so we're going to grab the array itself and then we're going to pass in the index which should be i which is going 0 1 2 3 all the way till the length of the array so that's going to represent our number and just for clarity i'm going to go ahead and say that so we'll just say individual number equals that so that's what we're going to do for each loop we're going to declare and assign a variable for that then we're going to check and we're going to say if the smallest number is greater than the individual number you also could have said if the individual number is obviously less than the smallest number either way works then we're going to set it to a new value so if the smallest number is greater than the individual number so the end of it well maybe we should name this something more descriptive so individual number uh array we'll just say array number that's a little bit more descriptive i'm sorry this is this is not right even at all i just need to take a little break here from talking so what we want to do is we want to say if the array number that we're looking at you know in this iteration if this is less than the existing smallest number then what we want to do is we want to set the smallest number equal to that array number so it takes its place as the new smallest number and basically what's going to happen is as we loop through the array it's going to check each number and if it's not smaller then it's just going to skip it and it's going to go to the next iteration of the array so we really can come down to the bottom of this and return from this function the smallest number after we've looped through all the numbers in the array let's go ahead and test this looks like we've failed some so i think this is because we didn't handle negative numbers let's see oh actually this is just a mistake on my part so i initialize this at zero but this is a problem if we have positive numbers because if we have an array that are you know full of positive numbers so let's say that we had an array that looked like this we had 2 we had 5 and we had 6. it's going to loop through all three of these numbers and it's going to compare it to our already smallest number of 0 and none of these are smaller so the smallest number at the end is going to return 0. so we need to actually not declare this we're going to actually just declare it and not assign it and then the first thing we're going to do at the very first iteration of the loop we're going to set the smallest number equal to that first number so we're just going to say if i equals 0 which basically means we're at the very first iteration of our loop we want to set the smallest number equal to the array of numbers and then we want to grab that index of i and actually we can we can actually put this below so let's get the number that we're talking about first and then we can replace this now with array number okay so just to review what what i just changed here what we're doing is we're saying we're going to initialize this or declare this variable it's not going to have a value yet so we have to assign it a value on the first iteration of our array so you know the first value of the array basically and we know that that's always going to be when i is equal to zero so first let's get the number that we are looking at in this specific iteration we'll assign it to the array number variable and then we're going to ask is this the first iteration of the array if it is i want to set the smallest number variable equal to the first number in the array so now we have something to compare against and in the same iteration so still on the first iteration of this array we're going to compare the array number to the smallest number they're going to be equal so we're not going to get there it'll go through and now it should work a little bit better for us and now we have an infinite loop because i'm just getting scatterbrained here and i actually set i equal to one when i was looking at this conditional and that is a big no-no because what's happening is now we're going through this for loop in each iteration of the loop we're resetting i to zero and therefore we have an infinite loop and that is actually something that you'll stumble upon a lot and infinite loops are not fun so make sure you don't make that mistake let's replace this with triple equals which is going to actually do a equality comparison we should be better off this time so we passed all of them let's attempt it and we passed the test at this point i'll probably take a break because i'm getting a little bit a little bit fuzzy in the brain here so let's submit this and let's just look at what other people did obviously there's a better solution in all cases but i don't think any of these are at the level that we're at right now at this point i just can't even think anymore so i'm going to take a break and we'll come back and finish the rest of the problems the next challenge is grasshopper summation so we'll see what that's all about it says write a program that finds the summation the sum of every number from one to the variable called number or num the number will always be a positive integer greater than zero remember pay attention to this right here we don't have to worry about negative numbers in this case let's just look at an example because i i want to go through that first so if the number is 8 it's just saying go from 1 to 8 and add everything together in succession so the first thing that we'll notice here is that it's only adding one so not it's not only adding one but the number that we're adding only increases by one so it looks very similar to a for loop so we can probably write a for loop so let's go ahead and template that out so we'll let i equal 0 to start we might have to change that actually and then we'll say i is less than num um yeah i is less than num because we want to go through as many iterations um as we need to get to num or the the value that's passed in and then we'll say i plus plus so we start at the value of zero and if we add zero to something that you know adds zero so that doesn't matter so we're fine doing that and then it wants to go until i is less than the number so in this case we may have to actually start this at i equals 1 and then we say i is less than or equal to number so let's go ahead and do that let's start it at 1 and then go up to the number and for starters i just want to print this out to the console and see what we get so i want to print out i each time in the loop and since it's not a very good you know place to do that i'm just going to go over here to my dev tools console and we're going to paste that in there and of course num is not defined because uh we don't have that in passed as a argument or anything to the function so let's just put a random number let's put 10 in there and press enter and what we're going to see is that it counts 1 2 3 4 all the way to 10 which since we started it at 1 we get to 10 but if we were to start this at 0 like we normally do for a loop and then we take away this equal sign it's going to go from 0 to 9 and if we add all these numbers it's not going to add up to the right sum so it's very important that we start it at an iteration of 1 and then we go all the way up to 10. so we put that little equal sign we also could have you know deleted that and gone up to less than 11. but you kind of get the point just be conscious of that now all we have to do is make a variable called the sum and we'll initialize that at zero because we start at a sum of zero and then all we have to do is say sum equals sum plus i so when i starts at one so we're saying zero plus one then i increments to two our sum is going to be stored at one so we do one plus two and then we go i equals 3 so our current sum is going to be 3 because we had 1 plus 2 so then 3 plus 3 is 6. so on and so forth so that's going to give us our sum another way we could write this if you were paying attention during the previous video where we went through the mathematical operators you can just do plus equals and then you can say i so what this is going to do is it's going to take some and it's going to say what is the value of the variable called sum and then once it finds that value it says take that value and add the value of i to it so this is an abbreviated version of doing that but again i'm going for readability here so i'm just going to say sum equals sum plus i that's the most clear way that we could probably write that so then we get to the bottom we've gone through all of our iterations so the sum variable should equal the sum of all of those different i values so now we just return the sum let's see what we get we pass both test cases i think we've got it all right so we passed all 105 test cases i'm not sure why anyone would want to write 105 test cases for this but nevertheless let's submit our answer all right let me just check to see where we are within this series we'll go to collections alright so we are on get the mean of an array so let's see where we are here all right so we're right here we've one two three four five six seven eight left all right not bad let's go ahead and train on this so get the mean of an array all right the instructions say it's the academic year's end fateful moment of the school report i'll remember that the averages must be calculated all the students come to you and treat you to calculate their average for them easy you just need to write a script alright when they say a script that's basically just saying you need to write some code return the average of the given array rounded down to its nearest integer the array will never be empty so you're always going to get a number in this array and it says round it down and what i'm going to think about immediately is we're going to use one of those utility functions or the built-in javascript functions so how do we round a number down let's go to the built-in functions we're already looking at the math methods and we can see that if we look at the floor method or function it says it returns the largest integer less than or equal to a number so basically rounds it down so math.floor is going to round our numbers down and if we go to the console here let's go ahead and clear this because we got so much stuff going on and if we say math.floor and then pass in a number 20.5 it's going to give us 20. so it just rounded it down so that's basically the round down function we're going to use that in our practice or our challenge here this is kind of a useless comment once again this marks marks right here i don't like the name that they've given this so i'm going to rename it because we can name our parameters anything that we want so i'm going to say score array so we're going to have all the test scores or whatever scores we're doing here now within that array let's see what do we have to do again we have to get the average okay so what is the average the average is going to be the sum of all the numbers divided by the count of all the numbers all right so that's your average formula pretty simple there so to get the count that's the easy part we'll just say the score array dot length so that's going to give us how many numbers are in this array and then for the sum unfortunately this is something i know from just writing code there is no sum function of an array like built into javascript so if you were to go to google let's go to google and search how to sum values in array javascript and let's go to stack overflow you can use this method called reduce and then you can actually use an arrow function but this is rather complicated and it's not really at the level that we're trying to be at right now right now we're just trying to do it in the most straightforward way possible and this kind of comes later when you're more experienced writing javascript code so in our case we can do it a little bit easier with a for loop so all we have to do is write ourselves a loop start it at index of zero we want to go until we've hit all the numbers in the array so we can actually in this case we've already gotten the length of the array so all we have to pass in here is the variable count that we stored that in so that's a way to simplify your code and then finally let's break down here all right so we're going to loop through each of these and what we want to do is add them all together so kind of similar to the last problem we did but in this case we want to assign so we don't want to count we don't want to say the same variable so we'll say the sum equals 0 right now and for each iteration we're just going to say sum equals the existing sum amount so it starts at zero and then each time we iterate through the array to the score array we're going to grab that individual number that we're looking at and add it to the existing sum so now we get down here to the bottom we have the count and we have the sum so let's go ahead and just return the average which is going to be the sum divided by the count let's see what this results in all right so we passed two of them but we failed one of them and the reason is because we forgot to round down i forgot that requirement so all we need to do is surround this with the math.floor function and that should return the right answer i could have broken this out onto the another line just for simplicity so we'll say the average is the sum divided by the count and then we can say rounded down equals math.floor and then pass in the average so take the variable we just assigned here and then we just want to return the rounded down variable and that should give us the same answer hopefully we get this right this time and we did let's attempt it we should be able to get this awesome so that's just how we would solve that problem so as you can see just to take a slight break and and review what we're doing here a big part of solving these problems is um obviously getting the instructions right and understanding what type of input we're going to be receiving into this function the second part with these easier these easier challenges we're going to do a lot of looping through arrays and stuff like that and that is actually really consistent with the real world when we're writing code and building apps we're going to be doing you know stuff like this quite a bit we're not going to be solving useless problems like this but we're going to be looping through arrays a lot and setting counter variables and some variables all that kind of stuff so this is actually going to be testing us on on very useful skills that you're going to use in the future all right so if you remember from that stack overflow question they're talking about reducing it to get the average and you can see that that's exactly what the the top solution or i guess the the best practice solution is doing it's it's basically using marks.reduce so this is going to get us the average then we're going to divide it by the length and then we're going to put it to the floor so basically round it down so that's that's what i was talking about with this stack overflow solution honestly if you ask me it's kind of weird that the array arrays don't have an average method seems like a very common one to have but nevertheless we don't so all right so this challenge looks like we have a picture to look at let's let's do some rock paper scissors okay so it says let's play you have to return which player won in case of a draw return draw okay and this is just a reminder of how rock paper scissors works i think most people would know that immediately when i look at this i'm not a huge fan of this so we're naming the variable that we're storing this arrow function in as rps or rock paper scissors um i'm going to just rename this because we need to have meaningful variables here so we're going to name it rock paper scissors spelled out and then p1 and p2 again these are not very descriptive so let's replace those parameters with something that makes more sense so player one and player two alright so now we've got this written out i like this a little bit better now let's go through our different cases so first thing you might want to do is write some pseudo code so pseudocode is basically um you know just writing out like logically how this should work and maybe this isn't even considered pseudocode but let's write out all of the different scenarios that we can have so we'll say rock is r paper is p and scissors is s all right so for player one over here and then player two and then the result so let's go ahead and write these out we have player one we'll do rock and then since there's three different possibilities we're going to have just a matrix of possibilities so we'll go rock rock rock paper paper paper scissors scissors scissors okay so that's all the possible plays that player one can do and then obviously we will go through with player two over here and we'll alternate so we'll go rock paper scissors and then again we'll go rock okay so in this comment we've written out all of the different possibilities and who's going to win in each case so you can see that there's three draws three times where player two wins and then three times where player one is going to win as we might expect so how can we convey this what we're seeing right here in code and to answer that there's there's a couple methods that you can go about doing it but once again i'm going to try to go with the simplest most straightforward and easy to read method so seeing this written out like this where we have all these different scenarios i think that we can say that we're gonna go with player one and just have three if statements so if player one chooses rock if player one chooses paper or scissors so we'll just put out those if statements real quick so we'll say if player one equals rock and then we're gonna copy that down okay so we got rock paper scissors so these are the three scenarios for player one and then for player two um we're obviously alternating between these so let's say player one plays rock so we could probably put a switch statement in here but i don't know if we need to so we'll just say if player two equals rock then that's going to be a draw so we're going to return draw then we'll say if player 2 equals paper then that is going to be return player 2 1. and then finally if player 2 equals scissors then in this case return player 1 one okay so now we can basically just copy these same three down into each of these and this is going to work so this one's complete so let's just fill out the rest of the results so player one uh chooses paper player two chooses rock it's going to be player one so if we're going paper against paper it's going to be a draw and then player 2 for that last one all right and then scissors versus rock so we're going to say player 2 one this one will be player one and then this one will be a draw let's go ahead and test this looks like we failed um and it's complaining that rps is not defined and that's just because i changed the name of the function at the beginning of this which i guess i can't do after all but i would not prefer to see it like this it should be more descriptive so let's go ahead and test that and it looks like all of them pass so we fulfilled all of our different scenarios that we wrote out for ourselves but i can guarantee you that there is a more efficient solution because in many cases we have like overlapping scenarios but if you're just thinking about this logically and you want the the easiest thing to read that you know even a beginner could go through and understand what it's doing this solution works okay so let's attempt it we passed our test and let's see how someone who is going to optimize it a little bit more would have solved this problem okay so here's the first one so it's saying if p1 is equal to p2 then we return a draw so that eliminates three of our scenarios that we had to write out in separate if statements so that's already reducing our amount of code by a lot then what they're going to do is they're going to put the rules within an object which is actually quite interesting so they're basically saying that for one player here is the rules so rock is always going to beat scissors paper is always going to beat rock and scissors is always going to beat paper so that's very interesting how they're doing that and then they're saying if p2 so player 2 is equal to the rules so it's grabbing whatever player 1 responded with and then it's going to grab the value of that and compare it to what player 2 responded with yeah so this is a very very smart solution very efficient but at this point after solving however many we've solved here 20 or so my brain's a little shot and we're going with the simple answer here now this is something a little bit closer to what we might have been able to do so we could have combined the conditions into expressions so rather than separating it out into a bunch of if statements we could have combined them together this is how we kind of started doing it so obviously there's a lot of solutions to this problem all right this next one looks relatively straightforward oh it says it's pretty straightforward so your goal is to create a function that removes the first and last characters of a string you're given one parameter the original string you don't have to worry with strings with less than two characters okay so this is a going to be a great time to use one of these built-in functions so let's go to the string methods and see what we have available to us in string so this is what we're looking at so we could use this character at because we could specify the end of the string in the beginning of the string with zero and then you know we could find the length of the string to find the last character that seems a little bit indirect though let's see we can also return a substring this is actually going to be the most direct route here so a substring is going to return the characters in a string beginning at the specified location through the specified number of characters okay so i'll show you how we're going to use this but first let's kind of get an idea of how it works so if we have a string and that string is hello what we can do is say string.substring and maybe i want to start at the very beginning which is going to have an index of 0. now we haven't really talked about this we've always talked about arrays having indexes but strings also do as well you can think of each individual character within the string as being a separate index so h is going to be an index of 0 e 1 l 2 so on and so forth so if we're starting at index of 0 that means we're starting at h and then the next argument that we get to pass in according to the instructions that we looked at here i kind of know how to use this already but you could click on here and it's going to say we're going to pass in what we want to start with and then the length that we want to grab so let's go ahead and grab two characters from this string so we start at h and then we grab two full characters starting there and we get he so if we would omit this we're just going to start at 0 or start at the beginning and get all of the characters but if we replace this with 1 we're going to get all the characters except for that first one so we're actually partially to our solution we've got the first character removed now we have to figure out how to remove the last character and to do that we have to figure out how long the string is in total and to find out how long the string is i believe there might be i think there's a method for that or a function so there should be yeah string.length so let's try that out let's get rid of this and we'll say string.length is equal to 5. so if we know the length of the string and we know that the string starts at an index of 0 we then know where our sub string is so all we have to do is say string.substring we want to start at index of 1 because that omits the first character of our string and then we want to end or not end we want to grab how many different letters well we want to grab let's see the string dot length minus uh what would it be 2 yeah it would be 2 because we've removed the first letter and we want to remove the last letter so we want to grab just the total length of the string minus two okay so that is our solution right there let's just go ahead over to our problem and write it out so this is pretty easy we just return string dot substring or maybe we can break this out so that it's you know clear as day for you uh length of string is going to be equal to string dot length and notice how i have not added these parentheses at the end that is just the nature of this function and i just know this by experience but you'd figure it out uh probably by googling it or at some point after you try doing this so let's say we tried to do this as we would think this makes logical sense but it's going to say string.length is not a function now go ahead and copy this just copy this and paste it into google and you're going to get stack overflow answer that tells you how to do this and it says oh you have to use just dot length and you don't have to add those parentheses so that's how you might figure that out on your own but i happen to know that that is how we write it so string that length and then we need to get the sub string so we'll just return that straight away string.substring we want to start at the first character or actually the second character and then we want to get the length of string minus two characters okay so let's see what we get pass the test as we expected okay let's submit it now as you can see there's a maybe even simpler way to do this so we can use the slice method um if we go to the built-in functions if we would have looked so we'd grab the substring method but there is a slice method which is right above it so extracts a section of a string and returns a new string well in this case i was just not being smart i overlooked that and didn't realize but if you just read through these this would obviously be a better selection than substring but it does give us the idea that you can solve this in multiple different ways and i'm sure that at some point yep down here someone solved it the way that we solved it in this case they're doing string dot length minus one which is very interesting oh so this is actually not the same so substring is different than the one that we used as you can see we have the one that we used and then substring so these are two different methods they work kind of similar a little bit different so that's why that one works that way but you can see that this person used exactly what we did so multiple ways to solve a problem let's see if we can blow through the last couple of these so let's go to sum of positive you get an array of numbers return the sum of all of the positive ones okay so we're going to loop through this we're going to grab every number from that array and then we're going to say if the individual number that we have grabbed is greater than or no yeah we'll say greater or equal to zero um i guess we don't even need that we just say greater than zero then we want to sum it but we need to have a sum variable so let's initialize a sum variable and set it equal to zero and then we'll say if it's a positive number we're going to say sum equals sum plus the number that we're using there and then at the bottom we just return the sum variable and we should get the correct answer past it all this is pretty similar to a previous question that we did in this video so we'll submit this as you can see this is a well commented solution that is kind of the same thing that we just did so we didn't do too bad now obviously you can use a simpler method so reduce similar to how you'd find the average of an array but once again this is this is pretty complex like you're using a pretty complex um function on an array and you're using a ternary operator within here so that's you know that is optimized to the max so we got some basic mathematical operations let's go ahead and go here it says your task is to create a function that does four basic math operations it should take three oper arguments so we want the operation which is going to tell us are we adding subtracting multiplying or dividing and then the two values we're doing so this is this rather simple this person has formatted this function a little differently than we're used to this is just a different way to format it i'm just going to bring it back to how we're used to seeing it doesn't really matter okay so the basic operation so the first thing we want to do is write a switch statement you're probably getting used to this at this point and for the switch statement we want to pass in the operation so it's going to evaluate that and see what the operation is and in the case that the operation is a plus we'll do something add a break statement and then we'll fill in the rest of these with case statements and then i believe all we have to do is just fill these in so we can return from here or we could set it equal to you know a variable so since we added the break statements let's go ahead and just initialize a variable we'll call it result and we're going to say the result is equal to value 1 plus value 2. we can copy this same line down and then at the end we can return the result whatever we get so let's indent these correctly doesn't really matter just for readability and then we'll just change the sign here so we can put a minus sign here we can put multiplication sign here and a division sign here all right let's go ahead and attempt it all right that worked i have a feeling that someone solved this in one line we'll see this is how we did it yep right here so this this is very simplified but let me explain what this is doing so if we type in eval let's look at javascript eval and we're going to learn about this a little bit so what this does the eval function evaluates or executes an argument so if we have three uh arguments passed in we have the operation we have the first number and the second number and it's always going to be the same um for each one so each one we're just substituting this operation well you might think oh hey let's or let's go ahead and train again on this one so that i can show you exactly what i'm talking about so let's go up here and in this case we might just say return the op or the value 1 and then the operation and then value 2. now this is going to obviously not work that's not valid javascript right here and all we're trying to do is we're trying to turn this operation into a javascript operator and all we have to do to make that happen is surround it in this eval statement right here and basically what it's going to do i believe you separate it by comma separated values or no you put it in one big string so let's go ahead and create our string so our operation string is going to equal value 1 plus operation plus value 2. and in this case some of these are going to be numbers so we yep that should work because value 1 is a string so everything else is going to be coerced into a string so that should work so operation plus value one value two and then i want to just console.log that so that we can see what it represents here so we'll console log that and comment out this return value for just a second so let's test that and you're going to see that the console log is giving us the following operations here and if you you know look at these these look like you know valid math operations but right now we just have them as a string but once we enclose it in the eval function so we say eval and then we pass in our operation string it's going to evaluate that string so in other words it's going to evaluate the string of four plus seven and then we can just return the result and this should work the same as our much longer code that we wrote so let's let's go back and just see what that one person wrote there they just you know abbreviated everything but did the same exact thing so just another way to solve it more efficient next up we have a string repeat write a function called repeat string which repeats the given string source exactly count number of times okay so this is what we're going to get they're going to pass in we want to repeat it six times or five times and here's the string that we want to repeat this is another classic example of where you can use a loop so we'll just set a loop equal to just the basic syntax we want to go until i is less than the i is less than n so let me replace these number of repeats and then the string that we want to repeat so number of repeats so we're looping through this and let's go ahead and make a starter string and we'll just set that equal to just an empty string here now all we're going to do is concatenate strings as many times as this loop goes so we'll just say starter string equals whatever exists in that starter string plus the string value and this should return the correct answer for us it works pretty consistent with what we've done in previous problems with the loop and then we have a variable up here that stores some sort of value that we later return after we've modified it several times with values in a loop and of course there's a an easier solution so you can see down here that we can just use the repeat function i didn't actually know this was a function but if you go to the string methods there should be some sort of repeat so let's see i'm not seeing it right now what are they doing here so that is a string method did i miss it let's search for it on the page don't see it here this might be a new method okay so maybe maybe use w3schools that's more updated that must have been a recently added method of some sort yeah it looks like this came out in es6 which is the newest version of javascript uh spec so that's probably why it's not on the other site there interesting so you can use a repeat string to repeat a string number of times very interesting okay go to the next problem convert a string to an array this should be very easy because we've already done it at least i think we've done so many problems at this point i think all we have to do is say string dot split and then we split it with an empty character oh that's not how we do it okay we don't want to split it like that so let's see oh okay so we want to split it by spaces so there's a space in between these words so all we have to do is press the spacebar in between these quotes and it adds a space in here that we want to split it by and this one does pass you can see that pretty much everyone solved it the exact same way here be getting to the end of these problems here congrats if you are still around and solving these problems with me i would presume that very few people would be so remove string spaces so remove the spaces from the string then return the resultant string now this one feels like there is some sort of built-in method that we can use since we didn't find the newer methods on tutorials point let's go to w3schools i'm thinking something called trim yeah like trim might do this says remove white space from both sides of a string i think that's what we're looking for here so let's go ahead and return x which should be a string like i don't know why they're calling it x here just call it a string so string dot trim see what that does this is not going to work looks like there's some numbers put in here and it looks like the trim method is only going to do this at the end of the string so let's try this out in our dev console let's do a test string uh string that has spaces between oh i'm just blanking out completely i got to name this variable so string equals that sorry about that i'm kind of losing it here all right so if we use string.trim it looks like what's happening is it's only removing the spaces at the end kind of as this tells us so i wonder if there's another method i don't know if there is so what we're going to do is we're just going to combine the methods together so first maybe we put this into a an array so we'll say string.split and we want to split it by a space so now that we have everything split out now we want to go through each of these in the array and we want to remove the white spaces around them so let's let's go ahead and write this code out and see what we get so we'll turn it into an array so we'll split it by spaces and then we're going to loop through this array as we've been doing and for each item in the array we're going to remove the spaces around it and we're not actually going to build a new array so this is going to introduce the push method so we'll say new array.push and what that's going to do is add an element to the end of the array so right now it is an empty array and we can push items into the array and what we want to push is going to be the trimmed version of each string that we have or each different array item which just happens to be the subsets of that original string so in this case we'll say trimmed string just breaking this out for clarity is going to be equal to the [Music] what are we looking at so the array at index i dot trim so now we know that this string is trimmed together there are no spaces anywhere and now we're going to push this into the array so at this point let's go ahead and console.log that new array that we're getting so let's test that and you're going to see that we have an array in every case that's going to have you know the characters with no spaces around it and in this case it's just an empty string that's not even a space or anything so if we combine that it's not going to add any spaces of any kind at this point we can use a method called join so all we have to do is um our final string is going to be equal to our new array.join and we want to join it by that empty character there and at this point we can return the final string and i think that that should solve the problem all right so we passed it looks good okay i think we're closing in on the end here beginner lost without a map given an array of integers return a new array with each value doubled okay so there's actually um it says for the beginner try to use the map method and this is basically just saying um to you as the beginner like hey you could solve this in you know an easier way but go ahead and try to use the map method so we'll we'll do that we might as well so we're going to get some array i'm just going to rename that so that it's not undescriptive and then we're going to go and look at the map method and this actually belongs to arrays so we go to javascript arrays and then we go down to the array methods or functions and you're going to see this one called map down here and what this map function is going to do is it's going to take the original value in the array and it's going to apply a transformation to it and that transformation is just basically going to return a brand new array where every unit of that array has been transformed so previously we have been going through these for loops and storing things in variables and in creating new variables and stuff like that but we could always use this map method so let's go ahead and try it so we're going to return the array dot map and then in this case we actually have to pass in an arrow function so this this is what we call like a a callback function we'll get into this a little bit later but we're going to have a callback function so this right here let's just break it out you know into a separate space so you can see what we're doing so all we're doing is writing an anonymous function or a arrow function and then within that arrow function we're going to break down into the new line and we're going to return a value so this is a very weird um setup here so i'm actually going to um i'm going to go ahead and remove this so i'm going to say our result array is equal to whatever this map function returns and then we're going to return the result later so that we don't have that weird syntax so basically whatever we return from this map method or this this function that's actually embedded within the map method whatever we return is going to be the transformation and then whatever we pass into it so we can just provide a parameter so array item is going to be what's going to be passed in there into there as an argument so we're going to just say array item multiplied by 2 which is what they want us to do with each value so this is going to return us an array with the values doubled in it and we return the result we passed i would suggest reading up on that method trying it out a little bit but just realized that what we've done here is we've put a function within this other function that takes that function as an argument so um to to summarize this we've basically passed in a function as an argument to another function and then that function actually executes our function that we passed in so it's it's a bit of a tongue twister but you will understand this with due time and i would definitely put this on your radar as something that's very useful in javascript at this point i'm doubting myself for picking 25 problems this is exhausting but again congrats if you're still around let's keep going all right is he going to survive a hero is on his way to the castle to complete his mission however he's been told that the castle is surrounded with a couple of powerful dragons each dragon takes two bullets to be defeated our hero has no idea how many bullets he should carry assuming he's going to grab a specific given number of bullets and move forward to fight another specific given number of dragons will he survive return true if yes false otherwise okay so basically what this is saying to us is that we don't know how many bullets are coming and we don't know how many dragons are coming either but we still have to figure out if this hero is going to defeat the dragon so let's see how we do that the rule is this each dragon takes two bullets to be defeated okay so basically what we could say is if the bullets is double or more than the amount of dragons then we return true so we'll basically just say in in this case let's go ahead and use the ternary operator i know it's late in the video brain's probably fried at this point but let's use the ternary operator we'll store it in a result variable so we'll say if the bullets divided by dragons is greater or equal to two then we're going to return true otherwise we'll return false and then we can just return the result but we don't even need to do that we can just return that ternary operator so let's go to the beginning and hit return so basically what it's doing is it's looking at this expression and it's evaluating whether that expression is true or false and then if it's true we return true if it's false we return false so there's actually some duplication going on here we don't even need this ternary operator in the first place all we have to do is return that expression because if this uh evaluates to true then the hero defeats the dragon if not then we return false so we should be able to just do this we passed it and it works so that's how you would solve this in the most simple way possible and honestly it's just a math problem plus a javascript expression with some operators array plus an array so let's train again i'm new to coding and now i want to get the sum of two arrays actually the sum of all their elements i'll appreciate for your help huh it looks like this person was not writing a challenge they were asking a question okay p.s each array includes only integer numbers output is a number two maybe they weren't asking a question very very weird wording for this okay so this is not going to work let's go ahead and test it as it's written you can't just add an array together but what you can do is you can loop through each value of the array and this is actually a perfect time to talk about embedded for loops let's go ahead and do that first we're going to write our outer for loop which is just what we've been writing all this time so we're going to say until i is less than array.1 or array1.length i plus plus and then we go into it okay but we also want to grab the values of array two so basically what we're trying to do here let's say that we have an array of one two three and an array of two three 4. our resultant array should be 3 5 7 because we added we added the number at index 0 for both arrays then we added the number at index 1 for both arrays and then we put it into a new one so that's what we're going for i'm going to leave this down here for a second just for our reference but in this first loop all we're able to get is actually we don't we don't need an embedded for loop i don't know what i was thinking there all we have to do is this we just have to say um array 1 value is going to be equal to array 1 i and then we can come down and say array 2 value is array 2i all right and then we want to say added value something like that so we'll say array 1 value plus array 2 value that gives us the sum and now we want to start building a new array so new array equals something empty and like we did in a previous problem we can use the push method to push a new value to that array so we'll just say the added value here and now when we get done with this for loop right here we should have this new array populated with our new value which should be this resultant summed array so let's return the new array and see how it goes looks like we failed in in a lot of cases okay let's see why oh so i misinterpreted this question entirely we want to actually give a single number okay so i'm new to okay blah blah so we got two arrays we want to sum all of their elements okay so we don't need a new array we just need a sum variable which is going to start at zero so the added value we can keep that then we can just say the sum equals the sum plus the added value and then return the sum at the end this time we passed it okay just misread the question and honestly i i don't know if this was the best question to choose i think this is our last uh easy question coming up maybe so alright so this one has some nice clear instructions that's nice so the introduction the first century spans from year one up to and including the year 100 the second from year 101 up to and including the year 200. the task is given a year return the century it is in so the century of 1705 returns the 18th century okay i mean this is just pretty basic so let's go ahead and solve it now it looks like the catch here is going to be these two examples so 1900 is going to be the 19th century while 1601 is going to be the 17th so like it says if it's you know one greater than the even year then we go up a century okay i believe we can use some sort of modulus operator here let me try um so let's let's say that we have a century of 1901. um let's go ahead and do modulus we'll just start with 100. okay so a hundred goes into 1901 uh well it would be what 19 times with a remainder of 1. but if we had 1900 modulus 100 we get zero okay so that's interesting so what we could do is we say if the century we have modulus 100 returns a zero then we want to just grab the first two letters or first two digits of that number and return it so in this case 1900 we just want to return 19 i believe right so we just want to return 19. so let's just say that this is this may be a bad solution but let's try it so if the year modulus 100 equals zero then that means it's an even even century so we're going to just say we want to return the first two digits and to do that we can let's go ahead and look at some of the the number values okay so pretty much what we're gonna have to do is we're gonna have to convert it to a string grab the first two you know digits or characters and then convert it back to a number this may be an indirect way to do it but we'll try it anyway so we'll say year to string then i'm going to chain chain something on here and i want to grab the sub string in this case i want to start at 0 and grab 2 characters and then i want to turn this entire thing into a number so we'll just cast it into a number this way and let's go ahead and test that to see if we solved a couple of these excellent so we solved the ones where it's an even now if it's not then we know that it's not an even year so it's always going to be one greater so 1705 returns 18. so again we can grab that substring so we're going to grab first two digits it's going to be equal to the year two string substring zero two so that gives us the first two digits and we wanna keep that as a number and since we're repeating ourselves here let's go ahead and put this at the top of our code and just get that from the very beginning and now we can take this expression here and replace it in here so all we have to say is first two digits so now that works the same we're turning that into a number but down here what we want to first do is we want to return a number of course well it's already okay never mind so this is going to give us a string we need to return a number so um the first thing we need to do is turn that into a number by saying first two digits and then we want to add since this is a number then we want to add one so i think this may work actually okay so the problem here is the test for year 89. since we're doing modulus 100 of course this is not going to work if it's less than 100. so in this one edge case we could just put this at the top at the very top and say if the year is less than 100 we just return 1. so that takes care of that case and we test it should pass all of them we'll see if it passes all the tests okay so clearly that was not that was a quick fix solution it didn't actually solve our greater problems so let's dig in to see what we're dealing with here obviously there are larger numbers at stake and much smaller numbers at stake so our attempt at using modulus 100 is not going to work very well so i believe our solution may be close i'm actually going to go ahead and see if there's something in the discussion down here so you can always look at the comments and suggestions to see what other people are thinking about we'll see okay these are very useless okay so this is not really helping us that much at this point my brain's so fried we're gonna go ahead and search this on google stack overflow uh we'll just say century from year javascript stack overflow just curious to see how other people solve this okay so it looks like what they're doing here is using the math.ceiling function let's see what this is okay so here's the the ceiling function it returns x rounded upwards to the nearest integer so we'll go back and attempt this again and see all those different test cases so let's just grab one of them here so 400 390 and let's see what math.ceiling returns for that oh and we have to divide it by a hundred so let's do that again so 4004 which is what we're trying to get at because we round this up to the nearest hundred okay so that's basically the answer then um couldn't think of that on my own here today so let's go ahead and try that um instead of all this stuff let's just return it math.ceiling is year over a hundred and this may not may not get the cases where it's an even or maybe it does let's attempt it all right there you go so that's that's very clever but um i guess in this case when i was trying to solve it here um i did not go through the scenario in my head where the years would be you know greater than say 1720 or 1930. like i didn't think about what if we have like year 20 000 something like that so in this case it's a little bit of the instructions like they didn't you know say a whole lot about what the input was going to be they didn't say that we're going to you know go up to a huge input like they ended up doing so that's partially the instructions and then it's partially you know i should have thought of all the edge cases and should have thought of the fact that the number could be greater than four digits like this so we solved the problem with a specific set of input but not all of the possible input and that's just that's very common when you're solving coding challenges you always have to think of the edge cases so while i didn't get this the first time it's definitely a good learning opportunity just to see you know how it is when you're trying to solve coding challenges all right for this one we're converting cat years and dog years in human years so we're only going to get whole numbers and we're only going to get human years one or greater okay so 15 cat years for the first year nine cat years for the second year and four for each year after that 15 dog years for the first year nine for the second and five after that so notice we have some similarities in the first two they both go up by 15 and nine in the first two years okay so what are we getting here what's the input um i have a cat and a dog i got them at the same time as getting a puppy that was human years ago so we're going to get a number passed in here return their respective ages now okay so the easiest one is going to be human years plus or no we just say human years in there so that should be the answer to the first part of the array the second two are a little bit harder so we have to get the dogs and the cats all right so the first thing that we want to do let's see let's say human years is one well in that case we're going to return 15 for both of them so let's make a variable here and then we'll say if human years equals 1 we want to return this right here so we're going to say human years and then we're going to say the cat's age is going to be what 15 and the dog's age will be 15. if the human years are equal to two let's see we're going to add 9 to that so we're going to return something very similar but this k this time we're going to return 24 here all right and then the remainder we're going to say in the first two years we get to 24 for both the dog and the cat but then they increment by different amounts so how do we solve this okay so so let's say cat excess years is going to be equal to human years minus two or we'll just say we can just say excess years and use this for both so we're basically trying to find you know how many years above the first two years are we looking at here and then from there we can probably just multiply it by whatever this multiple is so we'll say extra dog or no we'll start with cat extra cat years equals excess years times what four and then for the dog years we're gonna do five so we're just going to make a new variable and say extra dog years so then we just return at the bottom here if we have not hit either of these logical statements because if we return from here everything's done we don't reach any of the code below but if we have not reached either these code paths we're going to reach this and we're going to always return human years just as the number of years but then we're going to say we know that we've got at least two years if we reach this code so we'll say 24 plus the extra cat years and then we'll say 24 plus extra dog years let's see if this gets it right awesome we got it right so that's how i would solve that problem let's see how other people would solve it okay so this is interesting it's a similar concept we asked if it was the uh year one or year two and then we basically just did the same thing just in less code so i feel pretty good about that we did pretty good on that one so especially not being able to think i've been going at this for a while now all right i think we have like two more in total i'm gonna make it to the end our football team finished the championship the result of each match looks like x colon y results of all the matches are recorded in the collection all right write a function that takes such collection and counts the points of our team in the championship the rules for counting points if x is greater than y three points if x is less than y zero points if they're equal one point there are 10 matches total and x is going to be between 0 and 4 or not between it's going to be inclusive of 0 2 4 and then y is going to be 0 2 4. okay so this games let's just make this more specific that's going to be a games array we'll initialize by saying let total points equal zero and we're going to set up a for loop all right so we're going to go through each and we expect each value to equal something like this so we'll just say value and that's going to be games array with the index of i which starts at 0. so just for reference i'm going to say sample value in a comment and this will be 3 colon 1. all right so the first thing i gonna think about doing since 3 and 1 are used for calculating this i'm going to split them so we're going to say parts are going to be equal to value dot split and we're going to split it by a delimiter of a colon so now let's console.log the parts of this array and we're just going to see what that looks like so these are the values that we're getting in each iteration of the for loop so we're getting you know one and one two and two three and three so on and so forth and now what we're gonna do is we're gonna say all of these different scenarios so we're going to say if parts 0 so the first one if the first value so x yeah so x and y so the first one is x so if parts 0 which is going to be x we might as well break this out into a descriptive variable so there's your x and there's your y the first and second value of that array and we're going to say if x is greater than y we're just following the rules over here basically we're going to do something and if x is we'll say else if x equals y and then finally else which is just basically going to be that last condition which means x is less than y okay so let's just put it in there we'll say total points equals total points plus and in this case it's going to be three then down here let's just use a shortcut because we can so we'll do plus equal this is just an operator that does the same thing as here except just in a shorter way so we'll do plus equal and then in this case it's a tie so it's one point and then otherwise it's zero points so in this case we actually don't even need this last block because it's adding zero which doesn't even matter let's return the total points here all right we passed it awesome even going through the explanation there i will assume that you're pausing the video to do that oh that's an interesting solution right there i'm not going to get into that though okay at this point i believe this is the last question no we finished it looks like we've finished all of them i i don't know i think we skipped this last one the val count no we did that at the beginning anyways if you enjoyed this video give it a like subscribe to the channel uh please i'm very tired right now from going through all these challenges together with you be sure to follow me on code wars and i'll follow you back and stick around for the next video in this lesson series see you later [Music] now in the last couple of videos we have talked about the basics of javascript essentially so we started with some variables and we went through you know functions and loops and operators conditionals all that good stuff so at this point we're ready to jump into some of the built-in functions in objects within javascript which is going to save us a ton of time when we're actually writing our code because it basically uh it's a bunch of shortcuts essentially and also just kind of the basis of the javascript language here's what we're going to cover in this video and the next couple of videos but all encompassed within lesson six of the series so we're going to learn how to read documentation that's a really important thing as a developer we're going to do a quick start kind of an overview of callback functions they're a little bit confusing but we'll get through them and then we'll talk about primitive values versus object values within javascript talk about dates regular expressions string methods array methods um the math library error types and then a couple data types that we have not actually covered yet that would be not a number n a n and then also null and undefined values so we've got a pretty pretty big laundry list of things to cover uh in this lesson it's gonna take a little bit of time but i promise you when we get to the end you're going to have a much bigger overview of what the javascript language is all about now the one thing that i'll say as we go into this lesson is this is probably going to be the last one where we're just focusing solely on javascript we have to get through a lot of different things so we have to cover html and css and then a couple different other things before we're actually writing our web apps so this will be the last kind of lesson that we're talking about javascript alone now there are other things in javascript that we will not cover quite yet so that would be like the async await and promises and classes object-oriented programming you know it all kind of goes together and those are some more advanced javascript topics that i'm going to pick up later down the road when we've actually written some sort of substantial web app so hold tight on that i'm not going to cover it yet but we are going to get through the basics here and then we'll start getting into actually building that web app of ours the first thing that i want to do is actually go through how do we read documentation and we can start at the mozilla web docs which is called mdn for short and we've looked at this previously in the code challenges from the last lesson lesson five but we didn't actually go into depth on how to read this documentation and i think it's really important to do so so you get to the home page of the mdn web docs you go to technologies and you'll find javascript now javascript is documented in a variety of places this just happens to be what most people consider the official javascript documentation so once we're on the javascript homepage you can see on the left you've got this navigation bar there's some tutorials that you don't need to go to because this is a tutorial about javascript but then what we're interested is the references so when we talk about references that's generally going to be a big part of any sort of documentation that you're looking at generally with documentation you're going to have some sort of quick start guide just to get you started with whatever library or framework that you're reading the documentation for then you're going to have some sort of tutorial usually and then finally you're going to have an api reference sometimes they just call it a reference like they do here sometimes they call it the api sometimes they call it the api reference whatever the case it's all kind of centered around okay here's the core of of this library or language or framework and this is where you need to go to learn more about using it so that's what we're interested in for javascript and particularly right now since this video is all about the built-in objects and functions we can go to this built-in objects drop down in here you'll scroll down and you'll see a variety of different types of objects and these objects have things called instance methods it kind of gets into object oriented programming and i don't want to go there but that's why i'm saying objects and functions now it's going to say objects here but the functions are kind of what we call a method on top of these objects that that you can call from the object so um you can scroll down and see a bunch of different ones most of these are going to be irrelevant to you as the complete beginner and quite honestly they're going to be irrelevant to you um even as you get better so the point of this video is to point out the most important ones the ones that you're going to be that you're going to need to get the most familiar with and we'll go through them in more detail but i just wanted to give you an overview of this and then as you click through a specific object so let's say we go to string which as you'll learn in a little bit a little bit later in maybe the next video or something a string is actually an object in javascript like everything else so it's a little bit confusing but you'll get the point in a little bit now once we're on the string documentation page or reference it's going to give you an overview of everything and this is kind of the main home page for this string object now in javascript everything kind of behaves as an object and it has these things called methods on it which are the functions i'm talking about when i say method you can just think of it as a function that is you know attached to that object so as we scroll down here on the left you're going to see this thing says dot prototype dot whatever method we're talking about now the prototype part that's a complexity of the javascript language that we have not covered and we will not cover in this series that's a little bit uh above our pay grade and we don't need to get into that to actually start writing the code once again i've said this in previous lessons if you want to understand the prototype chain and all of these complex things about javascript um of course after you've kind of mastered the basics you can go to the you don't know javascript series it's completely free so you don't know javascript and it's actually hosted on github i think his name is kyle simpson i would go to the first edition branch here and then you can scroll down and just see all of the different books that he's written on javascript it's a really good guide that's going to take you through the nuances of this language anyways coming back to here so we have the object at the beginning.prototype and then the method so let's click on one of these methods and see how we actually read the spec or the reference for each method i think a good one to start with let's see which one do we want so we want to start with the pop method this is a really easy one to get used to and this is not part of the string spec so we need to go to arrays actually so let's go to javascript and then we'll go to built-in objects and click array now same thing you have all these methods here and we're going to go down here to the pop method now when we get to the pop method reference you're going to see a little summary and then an example this is the case for all of these different methods usually you should be able to figure out or get the gist of what this does and how to use it from just the starting section but it's really important to start developing the skill of learning how to read documentation and oftentimes it can be a little bit confusing especially since each language and site and framework and library it depends on how they've written their documentation it might look a little bit different so anyways the pop method removes the last element from an array and returns that element this method changes the length of the array all right so we know it is mutating or changing the existing array that we're working on now if we come down to the syntax section this is what i want to focus on in this video because if you are a more experienced developer you should be able to kind of figure out all of the nuances of a specific method or function from this syntax section now when we look at the pop method it just gives us this basic little syntax and you can see that there's nothing in this parentheses nothing that you pass in and then when you come down to the return value it says the return value is the removed element from the array and then it says undefined if the array is empty so it tells you here's what the normal return value is and then if you by chance pass in an empty array which you probably won't unless you're looping through it or something then it returns undefined so this is a really basic one and just by looking at this you can see how it works and we can come over to our console we can create some sort of array here so we'll just put a couple numbers in here now if we print the array it's one two three and then array.pop it's going to return the length of the array so 3 is the length of the array but now if we print the array we only have two elements in it because it removed the last one now this is a really simple one but the next one that we want to look at is going to be the join method and that's because it's going to add in a little bit harder documentation for us to read now before i do that though let me go back to the pop method so we got this overview section the syntax the description just gives you some more context around it and then you have some examples now as we get to the specifications this is going to actually take you to the specific link within the ecmascript spec and we talked about that in i think lesson two when we talked about what is javascript but this spec is going to we might as well click it and just see where it takes us so it should take us to the pop method you can see up here and this is the spec for you know the javascript language so just takes you there it's not something that you'll be probably clicking on all that often and then finally you come down here to browser compatibility and you'll see all the different browsers and then you'll also see a run time over here so node.js is not a browser but it's a runtime that runs javascript so it shows you the compatibility for this method within all the different browsers in this case the pop method is one of the oldest array methods in javascript so it's supported by all the browsers but as you'll see with some other methods that we look at it's not supported in all the different browsers and oftentimes it's internet explorer that doesn't support it so not a big deal all right so anyways let's go back to let's see not the pop method we wanted the join method which is going to be somewhere here all right so looking at the join method this is going to be a little bit more complex i'm going to kind of skip over this first section just to get an overview it says it creates and returns a new string by concatenating or joining all of the elements in an array all right so that's the basics now coming down down to the syntax where we're trying to focus on you'll see that this this code block here has a little bit more complex syntax going on so it says array.join and then in here we have this uh word called separator and we have these little brackets around it now what these brackets mean within the javascript or the mdn documentation it means that they are optional so the separator this is just a description like that word is arbitrary so it says separator just to give you an idea of what type of parameter it takes as i guess an argument so remember we talked about parameters and arguments two sides of the same coin parameters are what we define when we declare a function arguments are what we pass in to that function when we actually invoke it call it um you know whatever you want to say there so this in this case we have separator and we can read more about this here so the separator specifies a string to separate each pair of adjacent elements of the array the separator is converted to a string if necessary if omitted the array elements are separated with a comma if separator is an empty string all the elements are joined without any characters in between them so if you are following along with the 25 practice problems from the prior lesson we used this method a couple of times and we actually had to use that empty string to join all the elements together with nothing between them so this is where we would have found that now of course it's optional so if you don't pass anything in there it says if omitted the array elements are separated by a column so it's optional and then we come down to the return value and it says a string with all array elements joined if the array length is zero an empty string is returned so we can go ahead and test that too so let's go into our console we have an array here let's let's actually create a new one so let me clear the screen and we'll say string array and then we'll say hello world all right so what if we wanted to combine those together now we'd use the join method so we can say stringarray.join now as we see it right here we omit all of the arguments it just joins it with this little comma down here now if we go back and look at what else we can do so if the separator is an empty string it joins it without anything between them so let's put in an empty string here and you can see that it joins those together into one string with nothing between them now we can add a space here and press enter and this is probably closest to what we would want if we were doing this so that's just how you would read this now the last one i'm going to look at for reading documentation is a little bit trickier and we're going to have to kind of use these concepts we just talked about to get through it the example that we're going to do is the array.includes method so let's click on that wherever it is now this includes method determines whether an array includes a certain value among its entries returning true or false as appropriate now we're going to cover this a little bit later in this lesson so i'm not going to go too deep into it but let's go ahead and look at the documentation here it says this is the code block that we're looking at and you'll notice that we have a value to find and then we have the brackets here and the from index so breaking this down from how we understand it there are no brackets around the value to find which tells us that there is that is a required parameter you really can't run this method effectively without that required parameter now it has this comma here which basically means okay we're going to the next parameter that this function will take and then it's surrounded by these brackets at the beginning and end which means that the second parameter is going to be optional now if we look down into the parameters description value to find is the value that we're searching for and it says it is case sensitive so that's nice to know and then the optional parameter is the from index and it's basically saying you can start looking within this array at a certain index so you know if the value to find is earlier than that index you specify it's not going to include it so that's just how we would read the syntax of this and again we're going to go through this a little bit later in the lesson so i'm not going to do an example right now and just to point out what i was talking about earlier if you scroll down so includes is actually a little bit of a new method i believe i could be wrong on that but i thought that it was offered only in the latest spec of javascript so if you scroll down to the browser compatibility you'll see that internet explorer has no support for this method so if you're developing an application for that has to run effectively in internet explorer probably the only case that's going to happen is if you work for like a really large company and that company kind of has legacy systems and applications that still run an internet explorer you don't want to be using this includes method because it might screw up your application so that's just what i was talking about earlier in the next part of this lesson 6 here we're going to be talking about callback functions when i first started i was really confused about these and i honestly think that it's just because i never stumbled upon a good explanation something that really broke it out into the pieces that i could understand so hopefully in this video i can do that for you and prevent you from suffering from that confusion for several months like i did now the reason we're talking about this is because the built-in functions that we'll see later in this lesson are going to use callbacks or callback functions heavily and if you don't understand them you're not going to be able to use those built-in methods so with callback functions they're not any different from the functions we were writing in the previous lesson use you know use the same syntax so you can use the regular function syntax you can use uh anonymous functions or arrow functions it's all the same it's just a different way of using functions and it's just a little bit different way of thinking in general to start us off i'm going to paste in some code that we're going to actually understand by the end of this little lesson so this would be an example of using a callback function go ahead and pause the video just so that you can take a look at it and get yourself acquainted so i'll press enter here and you can also simplify that code that we just looked at into this right here so it does the same exact thing you can see the answer is the same and i would just pause the video and just look at those two code blocks just for a few seconds to just get your mindset around what we're doing here now the entire reason that i'm walking you through callbacks here is because oftentimes you're going to see code that looks like the second option here which is simplified yes and it's how i would write it but it definitely does not display the concepts that you're supposed to be understanding very well so in this section i'm going to be writing code that i normally would not write just because it's a little bit longer form and more broken out than i would write it you know if i was developing an application but hopefully you know that longer form of the code is going to clear up some of these concepts for you and really help you understand what callbacks are all about so let's take a look at each of these code blocks let me clear the screen and just get us the main code block and i have some comments pasted in here so let me clear the screen and we'll paste this in here maybe space it out a little bit so that we have some room to work with all right and if we look at this code here there's a couple things going on the first thing is we have two different functions here the main function right here and then the callback function so oftentimes you'll see the callback function kind of just embedded within that main function but in this case like i said i'm breaking it out for clarity so this main function it takes three arguments the first argument is going to be some number i just called it random number it's arbitrary um and then should call so this is going to be a boolean value uh value which is basically going to ask whether we should go into the callback and then the callback itself so walking through this code let's first look at result so the result variable is going to be set equal to the random number now what is random number now of course random number is a parameter to this function and therefore whatever we pass in as the argument to the function is going to be the value of random number so if we go to the bottom where we actually invoke or call this main function you'll see that the random number is going to be 20 right here so in this case random number equals 20 and therefore the result variable also equals 20 because we're assigning it now if we come down to this next part there's some comments here that's not affecting the code at all we just have a simple if statement so we say if should call so if you remember we can pass a boolean value alone we don't have to put any equal signs or anything and we can place that within a conditional to evaluate it so if should call is true then we're going to reach the code within this uh this conditional block if it's false then we just skip this conditional block and we return the result so basically if should call is false then we're going to skip this and the result is currently equal to 20 and then we return the result so the return of this main function is going to equal 20. but of course it returns 40 as we see down here so therefore the should call is true as we see here in the invocation of the function now the last part is the tricky part that's the callback now the first thing i want to point out here is that callback this name that i've given it is entirely arbitrary you'll see this very often you might see either this as callback or you might see the letters c and b for callback and that's probably a convention that you're going to come across but it doesn't have to be that way you can call that whatever you want it's just a normal parameter but in this case instead of passing in a number or a boolean like we did for the first two parameters we're actually passing in another function so basically what we're doing is we're passing in a function to another function and we're asking that you know parent function to execute the function that we passed in in this case my callback is what we've passed in you can see in the arguments down here at the bottom so we've passed in my callback which has just been declared hasn't been executed yet and so callback right here is going to equal the declaration of this function called mycallback so if you know how to run this if we wanted to run mycallback all we have to do is put these parentheses at the end and pass in some sort of number and you can see if we execute this we get 40 which is the same answer of course that would be the more direct way to do this but this is a contrived example that's hopefully going to explain callbacks to you all right so we've got this value called callback and that is equal to a function uh a function data type that has not been invoked yet so just a declaration and we can basically call this just like this but in this case the callback is actually the function that we want to call so we're doing that sorry about that we're doing that in this line right here so if should call that is true so we get down to this code right here and what we're doing is we're taking that function that's been passed in and we are executing it with the random number parameter which happens to equal 20. so we're basically doing what we did down here but within the main function we're letting our code actually execute the function so we're setting that equal to result which returns 40 and then we're going to return result at the bottom so that's going to return 40 and that's how we've gotten to our answer now like i showed you um with that second example a little bit ago it's a little bit more abbreviated way to do this and you do not have to write a separate function called mycallback in order to you know basically run a callback function so what we could have done in the first case this is how we executed main function we passed in 20 we passed in true and then we passed in a predefined function that we had already written up here but we could abbreviate this so let me collapse this main part and what we can do is say main function and we know that we have to pass in a random number so in this case we'll stay consistent we'll pass in a value of 20. we want the callback to actually be called within the function so we need to pass in true as the should call argument and then we need to pass in the function so in the first case we just put in my callback and it worked but we also could do it like this so we could write a anonymous function and this function is going to take a number so that's just the parameter that we're defining and we know that if we go up to the main function here it's going to pass in this random number as the argument so that's what number or num is going to represent and then we have to break it down here and then we have to return the num times 2 which is going to ultimately return us 40 and that's another way that we could write that callback there's other simpler ways so we can also do it like this so let's do main function pass in 20 true and then in this case let's just put in an arrow function so we have num and then we use our arrow function syntax and then we return the num times 2 and once again we get 40. so just another way to pass that in and we can even simplify this a little bit more so we can pass in the same parameters we don't actually need these parentheses here because it's a single argument so we'll take num and then we also don't need to break down onto the second line we can actually just take num times two and that's going to return it the same way and now we've gotten our answer of 40 once again so as you can see that's how we get from the most you know contrived simple elaborate example to the code that you're probably going to be seeing in a lot of examples this is what a lot of people use because it's just a much simpler quicker way to write a callback function so hopefully this section i know it's a little bit confusing but re-watch it again if you are not totally clear on this because we're going to be going into the built-in functions that use these callbacks very heavily and it's going to be important that you understand how they're working so you might understand callbacks now but why do we actually need them they seem overly complicated don't they well the reason we need them is in my opinion um this is just based from on experience and just observation the two things that it enables is reusability and asynchronous programming now i'll talk about each of these we're not going to go too much into asynchronous programming in this video for reasons i'm about to explain but let's start with the reusability thing and that is going to be crucial to really connecting the dots when we get into some of those more advanced array built-in methods that we're talking about later in the lesson to really understand the reusability thing we're going to look at a common array method that's called map so we're going to look at this a little bit later in the video too but i really want to show this as a lesson of why we need callback functions so what this map method does if we look at the documentation is it creates a new array populated with the results of calling a provided function on every element in the calling array so it's basically doing a transformation and you're able to define the function that has all that logic to figure out what it's doing to each element in the array let's take a look at a very simple example now this isn't going to make total sense but it will kind of connect the dots from what we just did with that original callback example so what i'm doing is constructing an array very simple one at that and then i'm going to define my custom map operation and we'll just say callback so we want to be very specific with what this function represents to us and then the parameter is going to be the item from the array that we're mapping over and what we're actually doing here is we're kind of iterating over the array so when we get each element of the array which is going to be passed in as an argument into this function we want to return a value and that value is going to be the transformed value that will kind of occupy the new array that we have so we're going to take the item from array and we're going to multiply it by 2. so we're basically going to take this entire array up here and for each element we're going to multiply it by two so we've got that function defined so far it's not really doing anything but if we take my array and then we actually we need to set this equal to a new array because that the map method is going to actually return the modified array it's not going to modify the original array so we're going to take myarray.map so we're just calling the function that's built into our array and what it requires is going to be a callback function and in this case we've already defined that so we're going to pass in my custom map operation callback and then we're going to execute it now if we look at the new array you're going to see that every element has been doubled and what it's done is it's looped through it's started at two then it's taken that value of two multiplied it by two to get four so on and so forth so you can see how we've kind of iterated over the array and applied some sort of transformation defined by our callback function now here is where we really understand this concept of reusability so in this case sure it works we're multiplying something by two but it doesn't really convey to us why this callback is so useful now when the creators of the javascript programming language introduced this map array what they wanted was to enable the developer to use this same method for a variety of use cases so what if we had an array that was full of strings so let me copy in a new array here i need to name it something different so we'll just say string array and this is going to have a bunch of strings in it and you might say okay well i need to find a different built-in method to map over this and do some sort of transformation well because we have a callback as one of the parameters of this predefined function we have a lot of flexibility and instead of looking for a new method we can just redefine our custom map operation now in this case i'm not going to do it long hand like we've been doing i'm going to show you the shortcut so we'll say not my array but the string array which we just defined we're going to call the map method on it and then we're going to pass in a callback function so let me template that out i'm going to have an arrow function right here and what i'm going to pass in is going to be the item from the array so we'll just say item from array we don't need any parentheses around that because it's a single argument and now the transformation that we're trying to do so if we do this on one line whatever is right of this arrow is going to be returned from that function so all we have to do is transform it in some different way so maybe we wanted to construct just the first letter of each of these words so maybe we wanted a new array that has the letter h w m n i and z all we have to do is maybe return the item from the array and actually what i'm going to do here is pass in no i don't need to i'm just going to take item from array and then i'm going to get it give it the zero index because you can access the first letter of any string by using the zero index second letter by one third by two so on and so forth so you can see that we've returned the first letter of each of these elements in the array and we did this for an array of strings by just redefining that callback function so that's the value of these callback functions it enables us to reuse the same function over and over again so in this case the built-in map function and kind of modify it for whatever needs that we have and we can do something way more complex than just multiplying by two we can have conditional logic in here we can say you know if the number is prime then we want to multiply it by 2. if it's not prime then we just want to return the number itself you know you can do all sorts of different things with this as complex as you want it all right so i think you probably understand why we why callbacks enable reusability why they're important you know how they work all that kind of stuff now let's move on to the asynchronous topic so i told you that callbacks enable asynchronous programming and what that gets us into is a whole other topic that i'm only going to briefly touch on here so when i say asynchronous all i'm talking about is code that does not run um immediately so with the code that we've run here on this screen you'll probably notice that when we press enter it executes immediately it's it pretty much seems instantaneous to our human brain now there are times when we're writing code that it might even take up to two three or four seconds for a piece of code to execute and a good example of that is let's say our application database that stores all of our data is you know on a in a different continent from where the user is actually uh clicking so maybe i host my database here in the united states but someone from i don't know italy or something is visiting my web app and has to access uh the web app from their browser in italy and so it's going to send off a request to my server and then to the database and that request to get all of the information for that web app and perform different functions is going to take a certain amount of time and it's not instantaneous so we have to handle that with our programming and you might just say well okay let's just wait until it's done and then execute the next line of code well in that case with a web app you're going to have multiple asynchronous operations going at the same time so you're going to have multiple calls to a web server that are going to take you know half a second one and a half seconds maybe even a little bit more and if you added all of those together and you just did it in order your web app is going to take 30 seconds to load and in that case nobody's going to use it so we have to run all this code kind of at the same time and for that reason we have asynchronous programming which basically allows us to run some code and then run other code while that previous code is still running now i know this kind of gets in the topic of like threads execution threads on a computer it's not really what we're talking about here and i really don't want to get into all of the details of this but as we go through this series we're going to be talking about asynchronous callbacks promises and the newer es6 async in a weight so these are all different ways that we can write asynchronous javascript code you'll see how it comes into play a little bit later in the series if you've been following along with this series you might have heard me say that everything in javascript is an object but i haven't really explained myself on that one and since we're talking about all the built-in objects within javascript in this lesson i really want to give an overview of this concept of primitives versus the actual objects themselves i'm going to put some code on the screen that will hopefully give you a little bit of an introduction and some things to think about here so when we run this code you can see that we've defined two different strings and we have compared these strings with each other so in the first case we've got this weird syntax here where we're actually using this thing called a new it's actually the new operator we're going to talk about in a second and then we are defining our string with this uppercase string which looks like some sort of object or something and if you go to the javascript docs so or documentation so let's go to javascript built-in objects and then you go down to string right here that is the same syntax the string that we use to define this string right here so string one is actually an object now string2 is what we call a primitive now this is because we didn't you know instantiate it with the object syntax and you can see if we compare the equality of these two the triple equals which compares both value and type is going to return false because in the first case we're looking at an object in the second case we're looking at a string primitive and if we use the double equals they're of course true because they are the same value they're just not the same type so at this point you probably probably have a lot of confusion in your head you're probably thinking a couple things well you're telling me that string one is not actually a string and the answer is yes it's it is not a string it is actually an object and then furthermore you're saying that this string down here so string two that is a string but how would we execute any of these built-in methods on something that's not an object so there's a couple floating questions here and to understand them you have to know a little bit of the behind the scenes stuff that's going on with the javascript programming language let's start off by asking the question what is a primitive now a primitive this is not a topic that is specific to javascript we talk about primitive data types across varying languages so you know python java c plus plus they all have their primitives but in javascript a primitive there are six different primitives that we can look at and we can go to the documentation here for primitive and it will maybe show us all of the different um primitives that we have so maybe it doesn't actually i thought that it did but anyways the six that we have are a string a number a big int boolean undefined and symbol some of these we haven't talked about but that's totally fine we don't need to talk about it yet but what you can think about uh when we're talking about primitives is that it is the simplest form of something so if a coding language had a periodic table of elements then the primitives would be those elements of the periodic table so that's kind of how i would think about it and those primitives can actually be used to build up other things now as you would expect a primitive data type does not have any sort of methods or functions that you can call on them so it doesn't make any sense if we were to come here in the console and let's define a string here so we've got some string and now i'm going to put a dot at the end of it just like i was calling a function or a method on an object and if i were to take this some string and say to uppercase so that is actually a built-in function on strings so if you go down to um the bottom here you can see that two uppercase is a built-in method on the string object but if we're saying that this is a string primitive then it shouldn't be able to work right well wrong it does work and that is because behind the scenes when we have a primitive value like this where we define it with those little quotes or the double quotes even it actually wraps it in that string object so what we did up here right here this is actually happening behind the scenes when you call a specific built-in method on a primitive data type within javascript now this is kind of interesting and all but what do you actually do with it well the answer to that is not really anything this is more of something that you should be aware of it's just good to know type of information so that you're not wondering in your head well what is going on here you know we're talking about strings but if we go to the documentation it sure looks like this is some sort of object here so it just clears that up for us and if you go to the string built-in object and scroll down a little bit we have this section called string primitives and string objects which kind of explains the same thing that i just talked about so feel free to read that and really dig in if you want to understand this a little bit better so the takeaway here is that you should not be using this syntax up here with the string constructor this is not the way that you write strings i taught you the correct way to write strings in the previous videos this is just for clarification now if you go to the actual documentation you'll see in this section on string primitives and objects down here it says for these reasons the code may break when it encounters string objects when it expects a primitive instead so the documentation even recommends to not define them like this because javascript takes care of it for you but anyways that's just some information around here now the next thing that i want to talk about is that keyword that we looked at because this is also a little bit confusing and as we go into the next couple sections of this lesson it's going to be important to understand and that is this new keyword which is actually considered an operator although we didn't talk about it when we covered operators in a previous lesson in the next section of this lesson we're going to be talking about javascript dates which you actually have to use that new operator for to actually create so if we wanted to create a date here what we have to say is new and then we pass in the date object with these parentheses at the end and this is what we call constructing a instance of that date object so you can kind of think of this in terms of like you have this template that you're working with so the date object would be the template and then you're going to actually take a copy of that template and store it in a new variable that has all of those attributes of the template it does everything that that template has kind of predefined it to do now this is kind of an object oriented programming discussion i don't want to get too far into it but just know that this new operator is a special keyword in javascript and that's what it's doing so if you actually look into the new keyword it's going to do a couple things so it creates a blank object and then it links that blank object to the parent so when i talk about template and an instance of that template that's what i'm kind of referring to so there are additional steps that get into the prototype chain again i'm not going to talk about that because it's another complexity that we don't need to visit right now in the previous parts of this lesson we've talked about a lot of the essential concepts that we're going to be using when we're talking about these built-in javascript objects and the first one we're going to be talking about is javascript dates now these javascript dates are a little bit confusing and i'll be honest i think a lot of developers even more experienced ones have a little bit of trouble with certain types of dates in javascript and the reason is because writing javascript dates is not really all about coding skill you also have to understand how time zones work and all of that stuff when you're actually getting into implementing the date objects and stuff like that now i've written a post on this so i'm on my site here so i'll just scroll down at some point here i talked about javascript dates so right here and with this date post i really go into detail about all of the different time zones and how we use dates and all the different nuances of it so i'd check that out if this is not enough detail for you but here we're going to actually go into the basics of writing dates and how we do that i showed it briefly in the last part of this lesson but all we have to do is define a variable and then assign a new date object or kind of a copy of that template which is date and we're going to assign it and now if we print out to the screen it's going to show us a date object here and it's going to be the current time because we passed in no parameters or no arguments to that date constructor now here's where dates get a little bit confusing what we have defined here the value that is stored within this date object actually represents the number of milliseconds that have elapsed since midnight on january 1st 1970 utc time zone that's a lot to handle in one sentence here and you might be asking why january 1st 1970 well the answer is not so simple and again i would read this post that i wrote to really get a background on that but basically it's an arbitrary date and time that we are basing all of our future dates off of and what i mean when i say the value that we're storing within this date is equal to the number of milliseconds because right here it sure doesn't look like milliseconds that we're printing but if you said now which represents the date that we just defined and then you say value of it's going to print the number of milliseconds since january 1st 1970 and that is the in the coordinated universal time zone so if you converted this to years and then days you would get exactly to this with dates there are a variety of things that you can pass into that constructor as an argument to define a date and what i'm going to do is just paste a bunch of code onto the screen because we don't need to go through writing all of it you can pause the video look through this for a second i'm going to walk through it but again read that post if you want a ton of information about dates so the first example we're passing in the the year the month the day and then the time or i guess hour minute second millisecond whatever and we're passing that in as comma separated arguments to the date constructor we can also pass in just a string in a variety of different formats as you can see in this code right here and then going down we can input a number of milliseconds so we can just give it the the value itself or we can pass it in as iso 8601 format again that post talks all about it and then finally you can do time zones down here you can pass in time zones this last part the date.now that's kind of an interesting thing that we can talk about here so you know we're used to creating a date by saying new date but you can also create a date with a static method that's what this is called here and you just say date.now represents the same thing as new date obviously not anymore because time has elapsed but it's just giving you the primitive value of that date now we're here in this video to talk about some of the instance methods that we can use on a date object the built-in method so let's define a date so that'll be equal to now or maybe maybe instead we'll just say my date because now is sometimes not descriptive because the second you press enter it's not now any longer so that's a little bit confusing so let's create a date here and that's going to represent pretty much you know right now minus a few seconds and from this date we can use a couple different methods so one of them will be the tostring method and that's going to print what we've used here in the console but of course this is a rebel so it kind of read evaluate print loop all that kind of good stuff so we can use this tostring method to print out the string of the date and this is actually going to print the value of that date which is stored in the unit the coordinated universal time or utc and it's going to convert it to our local time zone which is the time zone that our computer has stored on it so that's actually stored in some file on our computer i'm not going to go into that but anyways here we go for the next method so we have my date and then we can use the to iso string so the iso string is a specific format and let's scroll down in my post here because we've got it up anyways and i think i have a little screenshot of the utc time or not utc but iso 8601 and let's expand this just a little bit and you can see this is going to be the format that we're using for the iso 8601 standard pause the video look at this if you're uh curious all right so let's bring this back and well not clear the screen and then my date get date is going to give us the actual day of the month so today is the 16th of january so we get 16. we can also use my date dot get month which is a little bit unique because this is going to return the 0 or it's going to return the month of the year but it's going to use 0 indexing so instead of january being equal to one january is equal to zero which is what we're seeing here since this is being filmed in uh january so we'll go to my date dot get full year and that's gonna return 2021. um so those are some of the the basic methods that you can use on a javascript date and honestly these are probably the most common ones you won't be looking at anything else for the most part but you can go to the docs javascript documentation so again javascript built-in objects then you can go down to date and then you can see all of the different methods that we're using here something to point out you'll see this a couple times within the documentation if you look at the date.getyear method it has this little thumbs down icon which basically means that it's deprecated and a deprecated method is something that is no longer going to be supported so you don't want to use those now if you click on the get year method you'll notice that in our example we got we use the get full year method and the reason is because it says the get year method returns the year the specified date blah blah blah and it says it is no longer used and has been replaced by the get full year method so that's how i knew to go to that git full year method as a replacement so in the previous section we talked about javascript dates and now we're talking about the built-in object called regular expression and this is not just a built-in javascript object this is an an entire topic like i almost think of this as um another language that you're kind of trying to learn in programming it's obviously not turing complete or anything but regular expressions are basically a way to pattern match within strings and you might be asking well why would we want to do that and to show you why let me just open up a web application we'll just go to you need a budget and this is a budgeting app that i use and really like but if you go to the login page log in right here and you type in your email address but you type it in incorrectly it's going to say please enter a valid email address and you might be asking well how does it know what is an invalid email address it you know you may go through a bunch of different you know types of emails like a big long list of emails that's probably not going to be efficient of course but what we can do is we can use regular expressions to pattern match and basically say okay i expect to see these certain characters and then an at symbol because every email address has an at symbol and then some sort of valid domain or whatever for that email address so we can pattern match and that's why regular expressions are so powerful you can validate emails and you can also do a whole host of other things it's great for validation but it's not only for validation now as we're going through this just keep this in mind this is a very loaded topic as i said and you don't need to fully understand how to write these regular expressions it's really not a high priority thing for you right now if you're trying to learn how to code because you can get away without regular expressions while writing web apps they're just very nice to have they can really speed up your workflow uh down the road and you need to be at least exposed to them so all i ask here in this video is you understand what they are what they do and just how to read them in general just kind of understanding the symbols and be able to piece together what a regular expression is doing i wouldn't worry about learning and mastering how to write them so if we go to the javascript documentation let's see here we'll go to technologies javascript as we've been doing i'm just kind of reiterating this then you go to built-in objects and then we can go down to the regular expression object which is going to be reg exp and it says it's an object used for matching text with a pattern so as i said pattern matching now let's go ahead and write a basic regular expression i'll show you how to do this here so we'll say email validator regular expression and this will be kind of similar to what we just looked at where it's validating whether the email address is correct now this implementation that i'm about to write is by no means exhaustive and it's definitely going to miss a couple cases and i guarantee you you know ynab or you need a budget is using a much more complex expression than the one that i'm about to show you here so anyways let's go through this and just get an idea of what we're dealing with so let's say that our user input this is what we type into the form so you know i go here i type into the form i type something that's what this user input variable is going to represent and i might put in something like invalid email at g and obviously this is not a valid email so we should reject this by using this regular expression so we'll say a new variable we'll say it's is valid and then what we have to do is we take our email validator regular expression from up here and we can use one of the built-in methods on it so if you come down to the left you can see all the different built-in methods and one of these if we go down to methods are we even on the right one here okay we're on regular expression okay i was just missing it completely so here are the methods these are pretty confusing to look at but the two that we're looking at here is going to be exec and test so we can use test to just see if something is valid so we're going to go ahead and do that we're going to use the test and then we're going to pass in our user input so we're saying take this regular expression or this pattern that we've defined and map it up against this string that we have defined and if you press enter it's going to and then show what the value is it's going to be false because this is an invalid email and that expression up there that regular expression did not match it now if we look at this regular expression there's all these characters which probably look like complete gibberish to you but i'll quickly walk you through what this is doing and then we'll go back to the basics and really understand it so this first character just says i want to start at the beginning of the line this period represents all characters and then this plus sign that comes directly after after the period says i want to match any character one or more times then we have the at symbol which is just looking for the at symbol and then we have this period again so we want um any character one or more times and then we use this slash to basically escape and actually look for a period so if you have gmail.com we need that period and in order for this special character to be escaped we have to use this uh backslash or right here and then we use another period for any characters one or more times and finally the end of the line with the dollar sign so i know that's super confusing i would never expect you to follow along with what i just said but just to give you a taste of what we're dealing with here all right so let's go kind of to the very basics here with regular expressions i like to think of it in two pieces so you have the identifiers and then you have the quantifiers and the identifiers can be one of a ton of different things it can be special characters or just the characters or phrases that you're trying to match these identifiers are what you're actually trying to pattern match within the string so if we created a regular expression we'll say new regular expression and then we're going to pass in the letter g and now i'm going to pass in a couple of strings here so we'll say string 1 and string 2 and you'll see that if we're trying to match the letter g it only occurs in the second string right here so if we were to say the regular expression dot test and then pass in string one we're going to get false but if we test it against string two it's going to give us a value of true because it did in fact match the g in the middle of the string but in this case our regular expression is just taking a single character and looking for it which is probably not what you're going to be doing you're probably going to get a little bit more complex than that so maybe you test for an entire word so let's type another one so we'll say regex or regex2 some people would say regex some regex regular expressions whatever but anyways we're going to say this one is going to be equal to an entire word so in both strings we have the word favorite so now if we test the second regular expression up against both of the strings it's going to return true so no matter which string we put in there it's going to return true there as you can see so we can also match a full word in addition to just a single character but the interesting thing about regular expressions um or i guess maybe not interesting but important thing is that it's case sensitive so if i were to make another regular expression let me go ahead and just clear the screen for a second so we get some more room and let's say our regular expression is going to be equal to um favorite with capital letters at the beginning and the end and we of course need to bring in our strings again because we refresh the page but this count this time if we map up the regular expression and test it against string one we're going to get false because there are no capital there's no word in there called favorite that has a capital f and a capital e so you'll see string two also returns false so this is case sensitive you have to pay attention to the uppercase and lowercase letters now you don't have to just use single letters or single words or phrases you can use special characters and that's really where the power of a regular expression comes in so there are a multitude of special characters that we can use but i'm gonna put on the screen a couple of the really popular or common ones so the first one is going to be anything that we put in two brackets and another thing that i want to share here is that you don't have to write a regular expression like we did up here this is one way to do it just like we saw with the strings you can modify or create a string in two different ways now the more common way that we're going to be creating a regular expression is you put two forward slashes and then you put in your regular expression here so in this case i could put in g and that's going to be our regular expression that we wrote first we could also put in favorite and that's going to be a regular expression and as we saw you can actually execute methods on you know a literal like this so we can test string one against this but if we go back and lower case these letters now it's going to return true so this will be good for me to show this syntax so we can quickly loop through a bunch of different patterns here so we're just going to change the regular expression here and we're comparing it against string one the first thing you can do in a regular expression is a character group and all you do is have these two brackets and then whatever you define in that character group is going to apply to the regular expression so what we could say is a to z uppercase and what that is doing is matching all uppercase letters now in our string string 1 there are no uppercase letters so we return false because it doesn't match but we can also pass in uh a to z where we have all lowercase letters which obviously does uh does match at least one of these letters moving on we can also have 0 through 9 which is going to match all numbers within a string now there's no numbers here so it's not going to match returns false and then you can combine these together so you can match all uppercase letters all lowercase letters all numbers and then you could even match spaces in here so if we added a space here we could match uh spaces and this is going to return true because we're matching the spaces the lowercase letters so on and so forth so that's what we're using with these brackets you have a lot of different options within those brackets now we talked about earlier that this period when used in the context of a regular expression represents any character whatsoever now this is going to return true because it represents any character now the next one we'll look at is backslash d and this is going to match all numbers this is equivalent to saying brackets 0 through 9. so it's completely equivalent just a different way to write it in this case there's no numbers so it returns false we also have backslash s which will match any white space characters there's spaces between the words in these strings so it does match then we also have backslash w which matches all letters and numbers so alphanumeric so these are just some shortcuts that you can use as i said when i was interpreting that email validator this little carrot represents the beginning of a string so if we put in the caret and then we put in an f it's going to return false because the first letter of the string word testing it again so string 1 is actually m now if we put in m it's going to return true but we could put in the f returns false because we're starting at the beginning of the line but if we take that out it then returns true because we're no longer constrained to the beginning of the line now what we could do also is the end of the line which is a dollar sign so f is not the last letter of this string it's actually k so that's how we would make that return true and then finally we have these uh little groups that we can make so if you put parentheses here and then you can say um you could say code or uh stake or something like that and that's going to match either code the word code or the word stake and in this case we're looking at string one stake is in there so it's going to match so those are just some of the different ways that we can write the identifiers and you can see how flexible this is and how creative you can get to match different um patterns within a string or like a line of text now the next thing in the the second piece of this equation is going to be quantifiers now so far we have been using this test method to basically show whether or not we have matched a certain character in the string but if we let's rewrite this here so let's say that we want to match um all lowercase letters which returns true that's all that's telling us is that in string one we have at least one lowercase character now if we come over here and change this test method to exec it's going to do a little bit different operation and you can read about this here in exec it shows you kind of what it returns the different values of this array that it returns but basically what this is telling us here is that all that it matched when we wrote this regular expression is the letter m because it started at the beginning of the string it matched that one lowercase character and it said okay that's all i need to match now if we want to match more than just one character we want to match a section of a string then we have to actually tell it that and the way we do that is through quantifiers so there are a couple different quantifiers and i'll try to roll through all of them and explain all of them just really quickly as this again is just an overview so the first one is a star and the way that this works is you put your identifier at the beginning and then you follow that by a star or another quantifier that you're trying to use and so what this is saying is that i expect or i'm trying to match uh any lowercase letter and the star is saying i want to match any lowercase letter 0 or more times so in this case it's you know since we're matching 0 or more of the preceding character it's pretty much going to return something every time now we can change this to a plus sign which is going to match one or more of the preceding character and then we can put a question mark to represent either zero or one quantity of that previous character and in this case you see that the matched value is going to be only the first letter because we're looking for 0 or 1. now we can be explicit about this so we can say we want to match a certain amount of characters but before we do that i want to add a little space within this identifier because right now we're only matching this first word here so if we add a space in here now it's going to start matching the entire uh sentence or string so if we put in the plus sign you can see my favorite food is steak so it matches the entire string because they're all lowercase letters now if we wanted to be specific we wanted to match a certain amount all we'd have to do is these little brackets as a quantifier and then we can pass in a specific number so let's say that we wanted to match say five characters well it's going to get the first five characters so m y space that counts f a and that's going to match the first five characters now we can also do a range so we can say we want to match between two and six of the preceding character that we're looking at so it's just going to match 6 because that's the upper bound but you can match like a range of characters so these are all the different quantifiers and you can use these to take the pattern that you're matching and tell it how much of that pattern you want to match now before we go into kind of a final example to just tile the concepts together i want to show you one other thing and that is going to be in the documentation there is this thing called a flag so if you define a regular expression so we'll say our reg x is going to be equal to you know a new regular expression but we could also write it with these two slashes you can also pass in a flag and that goes at the end of these two slashes and here are all the possible flags in this case we might want to do a global search so it will search for the pattern even if it occurs multiple times and if you wanted to use that all you would have to do is write your regular expression here so we might say we want to match all lowercase letters one or more times and then at the end we put a g which represents that flag so that's just a a slight nuance to this and you'll see this so just be able to recognize it anyways let's move into a basic example where we can just apply those concepts of the identifiers and then the quantifiers and then we'll wrap it up for this uh section on regular expressions so we're going to write first a string so here's the string that we're going to uh want to match so we'll say hello world um and then let's put in a couple numbers 2021 and then let's put in a couple spaces here and we'll put in i don't know we'll put an at symbol in here as well and then more of a string at the end and our goal is to basically write a regular expression that is going to match this entire string so we can do it in a multitude of ways but i'll just go with the most obvious ways that i can think of so we'll write our regular expression and we're going to use this syntax here so the first thing that we'll notice is that we have some lowercase letters to start so we can use the brackets here so this is again not the most simple way to do this but this will hit home all these concepts for you so we'll say that we want to match lowercase letters and spaces so let's add in that space right there and we want to match that one or more times and then um actually i'm going to um do this in real time so that we can see what's happening so instead of putting it in a variable we'll do it how we did it before so we're going to look at all lowercase letters and then add a space and then i'm going to execute that against the string so we can kind of begin to see what we're matching here so we're looking for all lowercase letters one or more times so we got the hello world then we're looking for a comma we can just put the comma in as it is then we're looking for some spaces and numbers so why don't we pass in 0 to 9 and then another space and we want to match that one or more times and then we want to match the at symbol and then we want to match uh lowercase letters so again a to z and then we need a space in there and then we say one or more times and then for good measure this is not necessary but we can just put this little upwards carrot to indicate the beginning of the string and a dollar sign to indicate the end of the string you can see here that we have matched this entire string now of course we can do this a lot simpler all we need to do is put a period in there and a plus sign which matches any character one or more times and there we match the whole string but hopefully this was just a brief overview of a very basic explanation of regular expressions it's a built-in object within javascript like we talked about that you're going to stumble upon and eventually it may come in handy for you but most importantly just have an understanding of what they are what they do and how to read them so we've covered a lot in this lesson but we're finally making it to the string built-in object in javascript so again let's come to the beginning so technologies javascript built-in objects and then you go down to string right here so we kind of talked about the string object versus primitives got an overview of this but what we didn't do is talk about some of the methods that we can use on this string and what i want to do as i've been doing in this lesson all along is basically give you the most important methods the ones that i've used a lot myself that other developers use commonly and just give you a brief overview of them so that they're kind of in your memory bank and you have them for recall later when you have to use them in your code or maybe some code challenges or interviews or whatever so we're looking at strings and you can see there are a lot of different string methods that we can use and again this little downwards thumb means that it's a deprecated method so don't go trying to use any of those but again there's a lot of them here and i want to point out some of the most important ones so what we're going to be looking at let's go ahead and refresh this oh i don't know what i've done here okay there we go we'll clear the screen and what we're going to be looking at is the replace all method we're going to look at the two uppercase method the substring method the trim method and the match method so that's what we're going to be covering here and again just basic examples of each as we go through these i'm going to show you the documentation first because i really want you to get used to reading it and understanding how it's working so let's go to the replace all method it says this method returns a new string with with all matches of a pattern replaced by replacement the pattern can be a string or a regular expression so we just learned about that and the replacement can be a string or a function to be called for each match now we're not going to get into that function piece that's a little bit complicated but if we look at the syntax here you'll see some new symbols here so this little pipe means that there are multiple types of parameters that you can pass in or not multiple types of parameters because obviously there are multiple parameters but for this single parameter there's multiple types of data that you can pass in so in this case it's saying either a regular expression or just a string to match and then the second parameter so we have the comma here to say okay we're on a new parameter a new sub string so just a regular string or a function that will return basically whatever we want to replace it with so come down here to the parameters you can read a little bit more about that but we don't need to because i'm going to show you how to use it so let's go ahead and define a string really quickly so we'll say my dog jumped on the bed my dog is a bad dog all right and you'll notice that i have some weird capitalization in here which is going to be what we're replacing so we want to say we want a new string and then for the string we want to replace all so this is different from the replace method because obviously we're replacing all occurrences of the match rather than just the first and that's why i use this one more frequently because in most cases i want to replace all rather than just the first match so in this case we could just say we want to replace the word dog with cat and that's going to assign the new string to our variable and you're going to see my cat jumped on the bed my dog is a bad cat which obviously makes no sense whatsoever because we missed one and that's because this is case sensitive but what we can do is write a regular expression and we just learned about that so we might as well do it all right so we're going to say correct string equals my string dot replace all and then in this case we're going to pass in a regular expression and then we're going to replace that with uh lowercase cat so all occurrences of the the word dog uppercase and lowercase are going to be replaced with this right here so in this regular expression we're going to put a character group and we're going to say we want either an uppercase d or a lowercase d and then we want to match exactly so we're going to put these little brackets and put a 1 in there so we're going to match exactly one of these characters either uppercase or lowercase and then we're just going to pass in og because it's always going to end in og in the lower case form so then we're going to finally pass in let's see we need to pass in the global flag because we want to replace or find all instance of instances of that so let's go ahead and press enter and see what we got so the correct string it looks like we have cat in all of these instances so we replaced it correctly again this g at the end is just the global flag and there's a bunch of different flags that you can bring into a regular expression but this just means match all occurrences not just the first the next method we're looking at is the two uppercase method this is a pretty easy one let's go to the documentation take a look it says two upper case returns the calling string value converted to uppercase and the value will be converted to a string if it isn't one all right so very simple um very simple function here and we can pretty much figure out what it does by just experimenting around so we'll say that we want my string to be equal to some string and then all we do oh it's saying i redeclared my string so i need to refresh the browser because i already did that so my string equals some string and then we say my string dot 2 upper case and you'll see that it capitalizes every single character within that string so it's very straightforward you'll see where this comes in handy in just a second so the next one we're looking at is the substring method i use this quite a bit if we go to the documentation you're going to see that there are actually two methods and if you have been following along with this tutorial in the prior video the 25 challenges i actually used the wrong method here so you can see that we have the two methods we have the substring and then we have sub str and this sub sdr has the little downwards thumb which means don't use it it's deprecated and of course i used it in the previous video so apologize for that but we're going to be using the non-deprecated method and it says it returns the part of the string between the start and end end indices so if we go down to the function signature you can see that there's one required parameter it's not surrounded by those brackets and then one optional parameter and these represent the index of the first character that we want to include in the return string and then the last character if we want that otherwise it's just going to take everything after that first character so let's go ahead and give this a try um we'll use the same string here so we'll say my string dot substring and let's say that we want to get um everything starting at i don't know the letter m so if you think about this the s has an index of zero the o has an index of one m will be two e three the space will be four and the second s will be five so maybe we'll start it at five so we just get the second word and then if we don't pass in the second parameter it just goes to the end of the string which is kind of what we want but you could also pass in something like 7 and it kind of cuts it off there so that's kind of how the substring method works now i want to provide you with some common ways that we might use all these methods and one of the common ways that i've found to use the substring method is if you want to maybe capitalize the first word of a sentence so let's say that we wanted to take my string which is some string with all lower case and we just wanted to capitalize this s and combine that into the full stream so basically what we're looking for is this right here now if we wanted to convert that here's what we would do we would say let's just assign it to a result variable so we will say my string and then we want to grab the first letter of the entire string which is going to be that s now we're going to use that to uppercase method that we saw in the previous method right there and what that's going to do is it's going to grab that s and then it's going to capitalize it so all we're getting right now is a single letter so we can use the plus sign we can concatenate two strings together so now we have to get the remainder of the string right here and the way that we do that is using the substring method so we'll say my string and then we want to use the substring method and we want to start at an index of one because we've already got the first uh the zeroth index of s so we want to start the sub string at one all right and then we can go to the end of the string so all we would have to do is basically just leave it like that and we're done but if you i don't know maybe have a good reason to do this you could also grab my string dot length to get the very last character in the string as our bounds of that substring so if you press enter and then you type out result you can see that we have capitalized uh this string or sentence if it was longer so that's just a common use case of the substring method combined with the two upper case method and you can start to see how when we combine them together it makes our job a lot easier the next method we're talking about is the trim method and this may not seem all that useful when we first start looking at it but i will show you why we might use this in the real world so the trim method removes white space from both ends of a string notice it says both ends it doesn't remove white space from in the middle of the string white space in this context is all white space characters space tab no break space etc and all the line terminator characters so a carriage return here but we don't need to worry about that and the call signature the function signature is very simple you just call it on the string and it removes the white space so again you might say well this is not very useful but what if we got some external data that looked like this so this might be some data that you retrieved from an external database or something and you have no control over how you receive it and in this case we have some spaces um in these strings so how would we go about removing those in returning an object that has uh kind of a cleaned up piece of data well we can do that using a loop and the trim method so let's just write a loop a for loop so we did this a lot in the challenges the code challenges from the last lesson and then we also learned it in the last lesson so you should be familiar with this so we're going to write the loop i plus plus now we're going to loop through each piece of data and what i'm going to do is actually break this out so i'm going to say the current title is going to be equal to external data then we're going to grab the index that we're on dot title and then we're going to go down and we're going to grab the current author equals external data i dot author now we're coming down here and this is where we're going to reassign the values of these properties to the trimmed version of them so we're just going to say the external data i dot title is going to be equal to the current title dot trim so it's going to trim the white space and assign it back to that property then we'll do the same thing for the author so external data dot author equals the current author that we're iterating over dot trim and there is our for loop so if we print out external data we look at the array here you're going to see that all of these values have been trimmed of the white space so that's just a way that i've used it in the past something that has come in handy for me so i just wanted to show you that method for that reason now the final method that we're going to look at the built-in method on a string object is the match method now this is going to be very familiar to you because we learned about regular expressions already so i'm going to actually bring in something that we've already looked at here so let's write a regular expression and we'll just say we want to match uh any lowercase characters in a quantity of one or more and that's going to be our regular expression and then our string is going to just be a bunch of gibberish write this correctly a bunch of gibberish and then maybe some numbers in here and then some more gibberish letters all right so there's our string and there are actually two ways that we can test this the first way is something we've already seen where we just run the exec method on the string and of course since we're matching lowercase letters in a quantity of one or more it's going to grab all of these until it gets to the numbers and then it no longer matches so that's one way we could do it but we can also do it in reverse like it does the same exact thing except we're doing it from the string method so all we have to do is say string.match and then we pass in the regular expression and it's going to show us kind of the same thing now this is going to be exactly the same as long as you are not using the global flag so if we were to put in um let's say you know if we defined the regular expression and we put in a to z you know one or more and then we put this global flag in there that would be when these two methods do not behave identical but for our intents and purposes you can use either one it really doesn't matter just pick one and go with it to this point in this lesson we've talked about dates regular expressions and built-in string methods but we haven't talked about arrays and this is where it gets really fun now i will say that this list of methods that we're going to be going through you can actually see them on the screen all these different methods here so we have the push pop shift and on shift those are kind of grouped together we have slice splice these are actually different we have find index and index of they're kind of grouped together map for each includes filter and reduce and i chose these because these are the ones that you're going to be using most of the time and these represent kind of the bulk um you know if we can kind of go with that whole 80 20 rule this is this is the 80 20 rule of array methods and it's obviously not exhaustive and i'm not going to go into every last detail of each method but i hope that this can basically walk you through the basics of each so that when you have to use them it's not a complete shock you kind of know what's available to you which is kind of half the battle sometimes just remember these are not to memorize don't try to memorize these like any built-in method you can always look them up and as i've been going through in this video you know how to read the documentation now and i'll continue doing that for each of these array methods so that you can just get better and better at um you know acquainting yourself with the different syntax of this documentation so let's get started we'll start with the push pop shift and unshift methods and we're not going to really go through all of these in the documentation because they're so similar but let's go to javascript again built-in objects and we're going to the array object which has instance methods that we're going to be looking at here all right so we will start with pop we've actually already looked at this when we talked about reading documentation this is going to remove the last element from an array now we also have shift right here this is going to remove the first element of the array then you have the method that we're going to be using the most out of all of these which is the push method this just takes an element and or an element or more than one element and adds it to the end of the existing array and then finally the unshift method down here somewhere this is going to add an element or elements at the beginning of the array again i think the push method is probably going to be the most widely used but let's just go ahead and see what we're dealing with here so we'll make an array we'll add three elements in there and maybe let's go ahead and put a gap in there so we'll skip four and we'll put in five and six or something like that all right so there's our array so what if we wanted to um basically add the four in there so basically all we have to do is array.pop that's going to take off the six and it returns six not because the value is six but because there are um six elements or maybe maybe it did it does return the element that they that it removed from the array i'm sorry about that all right anyways we will go and do that again because we need to remove one more so now our array looks like this we just have one two three and now we can push 4 5 and 6 to the end and we are back to what we started with but this time it has all of the numbers in order so of course we can add the zero at the beginning so we just say array dot unshift put a zero in there and now our array has zero through six so you can kind of get an idea of how this works and there's not a whole lot to it all right let's move on to the slice method now this one is going to come in handy when you need to make a copy of an array and this is not going to be apparent why this is useful right now but once you start getting into front-end programming where you're writing code in either angular or react or view js or something like that there's this concept that we call state which basically um it's how the data flows through the front end of your application and when a user updates their profile or whatever and that data update affects another part of the application the state is what makes those changes propagated across the entire application and when we're dealing with state we often a deal are dealing with immutable objects things that cannot be changed so making a copy of the data that you're putting into state is going to be extremely useful and to do that you can use this slice method so let's look at it in the documentation we'll go to slice it says it returns a shallow copy of a portion of an array when it says shallow here that just means that you have like if you have nested objects or arrays it's not going to pick up on those it only gets like the first layer or whatever um you'll need a more advanced method to do like a deep copy of an object and i'll talk about that a little bit later in this lesson so basically what it's doing is it's taking two optional parameters so if we don't supply any sort of parameters at all then it's just going to let's see what it says so if we don't put any parameters at all it's just going to create a copy of the entire thing otherwise it will create a copy of a specific part of an array so let's see how this works we have an array already right here but let's say that we wanted to take out 2 3 and 4 or something like that so we've already got an array on the screen and let's say that we wanted to make a copy of the entire thing so we can say copy of the array is going to be array.slice we don't need to pass in any parameters because we just want the entire thing and now the copy of the array is going to be a perfect copy now if you were to compare these so copy of the array versus the array they're not going to be equal so i say that loosely but that is a concept that we talked about when we looked at the javascript operators in a previous lesson all right so if we wanted to grab like a specific part of this array so another copy and we want to grab everything starting at 3 until the end of the array you might say slice and then you want to give it an index of not 0 not 1 not 2 but you want to start at 3 so you want to give it an index of 3 and now another copy is going to be equal to three four five six so that's just a quick overview of the slice method the next method that we're dealing with is called splice and it is not to be confused with slice so if we go to the documentation click on splice it will say that this method changes the contents of an array by removing or replacing existing elements and or adding new elements in place so in other words this is useful if unshift and push those two methods we learned about are not sufficient you want to actually replace something in the middle of the array rather than just the beginning or the end now i want to look at the function signature on this one because it's kind of interesting so it's there's a lot going on but the only required parameter is the start which is the index at which you want to start changing the array then the comma here it says we're going to a new parameter we want the delete count so it's basically saying we want to start at a specific index and now we need to specify how many items of that array are we going to delete starting at that index now if we don't provide this because it is entirely optional it says if this is omitted or if its value is equal to or larger than the array.length minus the start we don't need to get into that then all the elements from start to the end of the array will be deleted so let's say that we you know specified the start as index of one then it's going to basically delete everything except for that first element so the next part that we have are these interesting brackets where we just have item one item two and then the dot dot dot this is basically saying that you can have an infinite number of parameters that you can replace and it looks like if we come down here to item one two dot dot dot it says the elements to add to the array beginning from start if you do not specify any elements splice will only remove elements from the array so this kind of changes the behavior of the entire method we go from removing elements to adding elements to the array so you can see how reading the documentation can be very helpful in our case we're just going to go through a basic example so we'll say that our array is going to be equal to 1 2 3 and then we'll put in some string that doesn't belong there 4 5 6. all right so what if we wanted to well hold on i cleared the screen but did not refresh and i tried to redeclare that array variable so let's try again so one two three four five six and what if we wanted to remove that string from there so all we'd have to do is array dot splice and then we want to specify that we want to start at the index of three so this is 0 1 2 and then 3 so we want to start at the index of 3 we want to remove a single element so we just want to put in a 1 right there and then at the end we can put in um let's say what we want to replace it with so in this case we want to remove this string and replace it with something so maybe something in between we'll put 3.5 or something all right so it returns the item that was removed and now our new array has the replacement value in there so at this point i think it's a good time to just briefly mention um the fact that we are mutating this array right here now when we looked at string methods none of those methods actually modified the existing string and that is because an array is not a primitive value now a string is a primitive value and for that reason it is immutable and you cannot change it now an array is mutable so that means that these methods that we're using are actually editing the array itself and you don't have to basically create copies you could if you use the slice method though all right so the next one that we're dealing with is going to be the find index and index of method so here's the find index it returns the index of the first element so only one that status satisfies the provided testing function all right so this is where we start getting into the callback functions that i talked about so if you don't understand callback functions be sure to go to that prior part of the lesson here and really wrap your head around those because from here moving forward we're going to be dealing with them in pretty much every array method anyways it says if there's no match so if your testing function does not match an element you're going to get a value of negative 1. so we know that if this method returns us negative 1 then what we're trying to match does not exist in the array otherwise we get the index to grab it from now this is kind of similar so the find index is kind of similar to the index of method so if we come down here and grab the index of method it's just going to return the first index where the given element is found there's no such thing as this you know callback function that matches or anything we're just searching for a specific string and again this will return negative 1 if that is not present now this is a little bit less powerful and like i said callback functions enable reusability in this case we just search for a raw string with the find index we can specify an entire testing function so in this case if you can grasp your head around these callback functions the find index is going to be a little bit more powerful for you so let's see if we can come up with a good example for this we'll use what we got for the index of method so again array is equal to this right here and we could just say array index of and maybe we want to look for that value of 3.5 and it's going to give us an index of 3. so what we could do here is we could basically say okay i want to get that value here so we can say array then we can use the bracket notation we obviously have to pass in an index and then we can find this index it's kind of circular here but i'm just trying to show you how this might be used and then we pass in there and now we can actually get the value so previously we just looked at for the index right here but now we're actually getting the value returned because we used that index and grabbed it from the array all right so that's how you use index of now let's talk about how you might use the find index method for a little bit more powerful search now what if you had a more complex array um like this this is obviously got a lot more to it it's got objects that have two properties first name and last name now if you tried to use the index of method so more complex array and then you used index of and then you started putting an object in here and typing out first name equals bob so on and so forth you're not going to get a match it will never match because you can't do that kind of equality comparison so all we have to do is use the find index so this is going to return us the index of the first match that we have identified within this array so let's say that we want to identify any or we want to identify the first item in this array where the last name does not equal smith so that's obviously the last value and that would be the zero one to third index so we expect an index of three to be returned and what this expects is an argument if we go to the find index again you'll see that we need to return a call back and that callback is going to have a couple of arguments that we can use here so let's look at this real quick the callback has an element so that's the current element being processed in the array that's not new to us we have the index of the array we don't probably don't need that and then we have the original array that we're looking at and we probably don't need that as well and then finally we have another argument after the callback and it says optional object to use as this when executing the callback we haven't talked about this keyword yet so we're not going to worry about that all right so we need to pass in a callback let's go ahead and populate that with the array item and that's all we're going to use here we could have passed in the index and the original array but we don't need to and then in this case since we're searching for anything that does not equal smith we can just say where array item dot last name does not equal smith all right so what this is going to do is return us a value of 3 which is the index of the first element that is not smith which happens to be this last object down here john doe so you can see how powerful this is as compared to the index of method there's a lot more things you can do with it but as i said this is just an overview of each method the next method we're looking at is the map method now we've already looked at this in the documentation a little bit earlier in this lesson let's look at it once more this was the method we kind of used to prove out the reusability thing but basically if we look at the parameters that we can put in here it says that we have a callback function and then similar to the um the previous method we just looked at we have the this arg which we haven't covered anything about that so we're not going to get into it so basically if you remember this is just a transformation function and it's going to loop through an array and perform a transformation according to your callback function on each element in the array so let's consider this array right here where we have blog posts from a database and in this case all of them are uncategorized so what if we wanted to return a new array but in the new array the category is going to be set to something like web development well all we'd have to do is use the map method now of course you could you know type it out and say blog post from database and then goes at 0 and then type in the category and set it equal to something but that's going to get really clunky and slow so what we need to do is say blog post from database dot map now this is where we pass in our map function so we're going to pass in the array item so as we go through each item the first thing that we need to do is we need to modify that actual property and then we need to return the item in full so we'll say array item dot category equals web development all right so there we have modified this individual array items category property and now we're going to return the array item which has been modified all right so we'll press enter and now you can see this map function returns the following array and in this case we now have the category updated for all of these properties so this would be a really useful function to do in the real world and i've actually used something very similar to this example in some of my apps so it's super realistic and super helpful it will save you a lot of time next up in our lineup is the for each method and until this point we've been looking at arrays and looping through arrays in one specific way so this is how we've been doing it if we have an array that has a couple values in it now we write our for loop so we initialize i to 0 we go until i is less than the array.length and then we increment i then we drop down into the for loop and we're going to do something here so maybe we're just going to print the element of the array that we're at so there you go prints it and we can do all sorts of stuff within that code block now there's another way to write a for loop that we haven't talked about yet and that is the for each method on an array so here's how we would do that we would say array.4 each and now what's going to happen is we have a callback function that gives us the array item and then we drop down into the function and we can once again console.log the array item all right so that's just a simple way to replicate that array up there you can also get the index so if we looked at the documentation of the for each method it's going to say here's our callback that we're looking at and it's going to pass us the current value and it's going to pass us the index in the original array so again this is this signature right here the the function signature is going to be very similar if not the same for pretty much all of these more advanced array methods so in this case if we wanted to take this uh for loop so let's clear the screen once more here's our array that we're looking at and if we wanted to do the for loop we use four each we want the array item and then we can grab the index so in this case we have two things to work with and we'll console.log say the array item and then we will enter down in console.log the index and you can see that we are printing the value of one which has a zero index the value of two which has a one index and the value of three which has number two index so this is really useful um a little bit quicker than our other for loop now there are a couple differences but nothing that we need to talk about right now you'll find those out as you start programming and i'll try to point them out within this series if we stumble upon them the next method that we're talking about is the includes method so this is going to help you find a single element and determine whether it exists within an array so there are multiple ways to do this and i'll show you a couple different ways and kind of prove out why the includes method might be the best in a lot of cases so let's say that you have the following array you have my colors you want to figure out whether the color orange exists within this array using some code now there are a couple ways that you can do this but one way that we can use is what we're basically accustomed to seeing and we're going to initialize this variable right here we'll say that it's false and then if when we're looping through this array if any of the colors equals orange then we set that variable to true and now we can figure out if orange exists which should be true so that's one way to do it but there is a simpler way to do it with the for each method so we could say something like this so we've already got the orange exist variable and all we'd have to do is loop through it that way set it to true and we can say orange exist and that is going to be equal to true now there is a even simpler way and it's using the includes method so all we would have to say is my colors that includes and then type orange and it's going to tell us true that does exist in the array so as you can see it's a lot easier to use a method like this than go through all of those for loops and you know store the value in a variable and stuff like that now just to connect the dots here and show you that there's a variety of ways to solve problems within coding you could have also used the index of method so if we would have said my colors and then you say index of and then you type orange and you return that it's going to give you an index of 3. so basically all you would have to say is that statement right there and you say if that does not equal negative 1 then our item exists with the in the array because if it returns negative 1 then it didn't find it and we know that it doesn't exist so that's just another way to solve it and hopefully you've seen that there are in this case there are four different ways that i went through something like that of checking whether something exists in an array the next method that we're going to use is filter and this is probably the most frequented method for me this is the one that i use the most probably i don't know for sure but this one and map are probably the two that i would use the most within my code now the filter method does exactly what you think it would do it filters out different results so let's say that you go on amazon and you go to your orders page it might look something like this you know this is fictitious it's not actually what their data model is going to look like but you can see that maybe we have some different properties on our orders array and this is going to represent all of the orders but you know when you go on amazon you can click on buttons and filter those orders by date by whether they're open open whether they're cancelled or whether they're digital and we can do this with the filter method which is basically going to return us an array that has been filtered based on a callback function that we provide to it so with this array you can see that we have this is digital property so we could find all of the digital products we could save that to an array by saying digital orders and then we'll set that equal to all orders dot filter and now this is where we pass in our callback function which again receives the array item and then we will pass that in to our function and return anything that is digital so we'll say array item dot is digital which is a boolean so if it's true then we're going to return a true value and it's going to be included in our resultant array so now our digital orders if we print that out you'll see that we have two of them and it says is digital true so we know that we match those correctly now there's a couple other filters that we could do we could paste in this and say okay we want all of the digital and cancelled orders so we're returning a condition where it says we want to check if it is digital and it is canceled so in this case we can print this array digital cancelled orders and we only have one of those where you can see is digital is true and is cancelled is also true so this is a super useful method and it's often used on the front end where you're like i said on your orders page on amazon and you click the button and it might execute this filter method and just show the user whatever is filtered so this is really useful i use it all the time on the front end and you can even use it on the back end a little bit as well now i saved the hardest for last of course because i didn't anticipate the fact that i'd be talking for such a long period of time but we're going to go ahead and try this anyways this is the reduce method so if we look at the reduce method within the documentation it's going to give us a very interesting callback function so instead of the array item and then the index and then the original array that we get passed into our callback it's going to give us some interesting parameters so it gives us the accumulator it gives us the current value and then it gives us some optional parameters here index and then the original array and then finally we pass in in addition to the callback an initial value that we want to initialize this accumulator to so i'm not going to go through all of these i'm going to put this into terms that i think we can all kind of recognize so let's go ahead and paste this in and this is the code that you're probably used to seeing so if we wanted to find the sum of this array so we wanted to add all of these numbers together this is how we would do it and this is how we did it in the 25 coding challenges that we practiced for the previous lesson so in this case we initialize a sum variable to equal zero so we start the count at zero then we loop through our array and for each element in the array we add it to the sum using the plus minus or plus equals operator and then at the end we console log the sum which is going to equal 99 because that's the sum of all of these values in the array now what i want to point out is the fact that this sum variable is essentially accumulating values so it's basically called our accumulator so if we go back to the documentation it shows that the first argument of this callback is the accumulator so you can think of this as our sum variable that we just wrote right here so what i'm going to do is right below here i'm going to rewrite all this code but using the reduce method so the first thing i'll do is write our reducer callback function and what this callback function is going to do is it's going to accumulate the value so if you look at the documentation it's going to first take this accumulator parameter which is basically going to act as this sum variable right here so up top we defined it like outside of the for loop but here we can just pass it in as a parameter to the callback and then we have an array item so that's going to be another parameter and we have others but we don't need them so let's go ahead and just use this and now what we're going to do is we're going to return a value so we always return a value from the callback for each array item and in this case this sum variable is going to be accumulated over the course of looping over this array so the reduce method automatically takes care of that and make sure that that accumulates a value so all we have to do is say sum plus or equal array item and what this is going to do is exactly the same thing that we're doing up here but we're adding the array item and we're doing it all within a callback function so let's define that and now we can say that we want our result to be equal to array.reduce so we're using the reduce method and we need to pass in our reducer callback and then we need to pass in one more thing so if you go over to the syntax you'll see that we have the callback which is encompassed uh in the highlight right here but then you'll see after that highlight we have a comma and then another parameter called initial value and if you come down and read about the initial value it's basically saying that this will represent the starting value of this sum variable that gets passed in for every iteration so we want to start that at 0 just like we started the sum up here at 0. and when we press enter and print the result we're going to get 99 which has done the same thing as our code above so while it looks very confusing it is very confusing you can kind of anchor yourself to this more uh simplistic code that we've already written and already understand so that is the reduce method and the last thing i want to show you about it is how you can use this to basically get the average of numbers in an array so we went through some practice problems in the 25 practice problem video previous to this lesson and we had to calculate an average of an array i believe and of course there is no average method on the array object i'm not sure why doesn't make a whole lot of sense to me but you can use this reduce method to actually calculate your own average of the array in just a single line here's how you're going to do that all right so we're going to say array.reduce and then we're going to of course pass in the sum and then the array item so we're writing the same thing that we have written already we're just going to do this in one line so we're going to take the sum plus or equal the array item all right so what this should give us right here is going to be equal to of course after we pass in our initial value that's going to give us the sum of the array and then all you have to do is divide that total by the array.the length and now you have retrieved the average of this array so that's just a way that this can be really useful and you might take a second to look at this code and see what it's doing because it's a little bit confusing as a one-liner like this but anyways that's just a really practical example something that i've used before in uh writing actual code all right so that is the end of the most common built-in array methods now you might be wondering well what happens if i find an operation that doesn't have a method already built for it and the answer to that is you probably will be using some sort of external library to do that and the most common one for us javascript developers is going to be the lowdash library so if you type in lodash and you click here this is a modern javascript utility library delivering modularity performance and extras if you go to the documentation you'll see on the left that at the top we have our array data type and you'll see that we have a bunch of different methods that we can do all sorts of really interesting things with so we have a limited set of built-in javascript objects but the developers of lodash have taken it a step further and basically created some additional methods so if you remember something that was tough for us is comparing the equality of two objects so if we go in here and create two objects so we'll say object one and we'll set that equal to prop one and some value all right and then we have object 2 and then we want to just set that perfectly equal to this all right so we put that in there and now if we try to compare them it's going to say false and that's going to stay false even if we use double equals and that's because we can't compare the equality of two objects and if you wanted to write a function that actually compares the equality of two objects it's going to take you hundreds of lines of code but if you use the lowdash library you can actually look at something let's go down here a little bit and we have this method called is equal and this is equal can actually take two objects as you see here so these are basically the example we just came up with and it checks if they're equal and it returns true and behind the scenes this library is running hundreds of lines of code to make this comparison and you can see how we can extend our functionality by using external libraries so let's go ahead and look if we go to the documentation on mdn go to javascript go to built-in objects and go down to the math object you're going to see that there's a lot of built-in math functions and as a developer you might be saying well hey i'm not writing a bunch of finance applications i probably don't need this well i'll show you in just a few seconds why it might be useful for you all right so if we go down here this is a little bit different than we're used to seeing so if we come into the console and talk about like dates or something we know that we can make a date by saying new date all right so that's the syntax that we're used to it's called the constructor syntax because we are constructing a i guess copy of this date template or the object i mean these are all terms that i'm kind of using in quotations because they're not perfectly true but you kind of get the point all right so this is the way of constructing an object now with the math library you'll see that all of the methods we're just taking the actual object we're so we're saying math and then we're just executing the method against it so instead of saying let's say my date equals new date and then we say my date dot get date or something like that we are first constructing the object and then using a method on it and this is how we've done it with all of these so you might say well why don't we say math equals new math and you press enter and it's going to say math is not a constructor well this gets us into the concept of object-oriented programming the math library actually has rather than instance methods so with the previous ones with the date object and the string in the array we have to first construct the actual object and then we call methods on top of that object which are called instance because we have an instance of that object and we call methods on top of it now with the math library we're just using static methods so we just call it directly from the object and it's pretty simple so let's just go through a couple of quick ones that you might you know find useful so let's see what my list shows right here so let's clear the screen and here are some of the common ones so we have math.pi that's going to give you the value of pi we have math.e so this is i think euler's constant or something like that that might be something that you use um and then we start getting into the methods so we have the absolute method and we can actually um pass in a negative number and get a positive or absolute value of that we also have the math.ceiling method which is going to round up to the nearest integer so let's say that we pass in math.pi we're going to round 3.14 up to the nearest integer of 4. likewise we have the floor method so we take math.pi again and it rounds it down to 3. and then finally we have the math.round method and again if we put in math.pi it's going to round it to 3 because 3.14 is closer to 3 than it is 2 4. all right so those are some quick methods that you can use you can also use the min and the max method so if we had min you can pass in a couple numbers and this is going to give you the smallest number and then of course max will do the opposite so same numbers but we get the highest number all right and then the final one is math.random so this is going to give you a random number a random decimal between 0 and 1. now this has implications that you probably don't understand but we've actually used this to get a random index of an array for previous videos and i'll show you exactly how we can do that alright so let's decompose this in two steps so first let's get a random number and set that equal to math.random all right so this is going to equal something between 0 and 1. let's just see what it equals right now all right so it equals 0.97 all right so we know that if we multiply something between 0 and 1 by another number it's going to give us a whole number between 0 and whatever number we're multiplying it by so if we multiply random number times five we're going to get a number between zero and five so we can use that to say that we want a larger number we'll just call it that and we'll say that will be equal to random number um times a you know we'll say a hundred so now we get zero to a hundred and then finally this is going to be a decimal when we look at it but if we wanted to bring this down to a whole number we just say math.floor and then pass in the larger number and now we have 97. all right so we can combine this with our knowledge of the length of an array to get um a range of random numbers that are within the bounds of an array so let's just define an array real quick and we'll just say some values blah blah all right so we got four values in there the length of this array is four so here's how we get a random index of our array we'll say random index and we'll be setting that equal to all right so we will say math.floor because we know we need a whole number now we want to pass in math.random so a number between 0 and 1 and then we want to multiply that by the array dot length so that we can get from 0 to the length of the array which is always going to give us a valid index all right so we will set that equal press enter and now we can just take our array and pass in our random index and it's going to give us a random value of that array next up we have to talk about air types within javascript and we're going to combine that with some of the data types that we haven't quite talked about up to this point all right so error handling in javascript is something that a beginner is not going to find the value in most likely i know i didn't when i first started out i could not understand why we need to handle errors within our code but there are some really important implications to this so if you have an external library that you're calling or maybe you're fetching data from an external database or something like that you don't know what the behavior of that external code is going to be so it could give you an error or for that matter let's say that you're building an app that's similar to say instagram and your user who maybe is got some spotty internet at the time is about to press post on their instagram photo and as they're pressing post their internet goes out alright so clearly the code that's going to be run is going to fail and what we want to do is handle that correctly so when you try to post and you got no internet you're probably going to see a message on the screen that says something like no internet please try again when you're connected now that is proper error handling because we have detected that something went wrong and given the user a meaningful message for that now if we didn't handle errors and we just you know ran our code and just you know threw caution to the wind and said hope it works well in that case when that user posts the photo and the internet goes out the whole instagram app is going to crash so we obviously don't want that to happen that's a terrible user experience and that is the value of error handling now in javascript there are three main types of errors there are actually more so if we go to the documentation javascript built-in objects and then we go to air now this is going to be what we call the base class this is the like you know parent air type and within this air type there are several subtypes of errors that we might see now there are three of them that i consider the most common and you'll probably see the most often and those are the ones that we're going to talk about here in this video so the three we're going to talk about are going to be the reference error the syntax error and the type air and i'm just going to walk you through a couple situations where you might encounter these and just explain like what they are and then finally we're going to talk about how to actually handle an error in javascript so the first one is going to be a reference error so let's click on that and this reference error it says is going to represent an error where there is a non-existent variable that is trying to be referenced basically all right so a good example of that is if we don't have a variable defined but we try to maybe print it to the screen or use it so let's just define some variable we'll set it equal to 20 and when we try to reference that by just typing it into the repple or the console it's going to give us a value but if we were to type in another variable and press enter we're going to say it's an uncaught uncaught reference error because another variable is not defined so when we think of referencing we're basically trying to find a reference in the computer's memory of that variable and if we haven't declared it yet then it obviously doesn't exist and we have a reference or a lookup error so that's the basics of a reference error now a syntax error is the next one so let's look at that we'll go back to the main error page go to syntaxair this is where you're writing invalid javascript so this one's not very hard to replicate all you have to do is write invalid javascript so let's say that we write an object and we set it equal to this right here so we'll say prop1 some value and put a semicolon at the end enter down and another value and put another semicolon semicolon at the end and it's going to give us a syntax error because these semicolons right here don't need to be there those those should be commas not semicolons so we wrote invalid javascript and we got a syntax error because of it so this one's pretty easy now the last one the type error this one is a little bit more confusing so let's look at it real quick go back to the main documentation go to type air now it's going to say a type error represents an error when the operation could not be performed because you're doing it on you know a variable or a data type that doesn't have that operation so something that you might think is going to throw this kind of air is i don't know adding two objects together so let's say that we have object one and we're gonna set that equal to uh i don't know say 20 here and then we'll come down and do another object this is just totally arbitrary i'm just making some examples now what if we said object one plus object two you would think that instead of returning this string with like meaningless object notation we would actually get a type error because we're trying to do something that can't be done we can't add two objects together like this they're not uh integers or or numbers when you're going to get a type error is when you're trying to pretty much call a method on the wrong data type so let's say that you have a number all right so we'll set that equal to 20 and on that number you're going to say to uppercase now we know from earlier in this video that two uppercase is a method attached to the string object not the number object so in this case we get a type error because it's not a function now this might also happen we have these objects up here and our property on the object is not a function but if we tried to call it like we call a function it's going to give us a type error because prop 1 is not a function so that's what a type error is all about now the last thing that we have to talk about is actual error handling and as we talked about it's important because you want to give a user the correct message when they you know run into an air and you also don't want your application to completely crash every time an air happens because it happens all the time with code so to do this we use something called a try catch block so the first part we put the word try and then we put these two brackets so we enter down in whatever code goes in these brackets is going to be checked for errors and if there's an error then it's going to throw that error and send it to the catch block so let's write some code that actually throws an error so let's say that same example of we have a number and then we try to make that number uppercase which is obviously invalid so we know that's going to throw an error but because it's within this try block we're going to pass that air it's going to be thrown and then this air is going to be populated with that new air object so this could be anything we could make it e we could make it air or er or we could make it air it doesn't matter it's just a parameter like we've seen before so let's enter down into the catch block and now we can actually check to see what this error is now i'm not going to just console log it because then you might get confused like okay what happened so let's go ahead and actually first let's let's just cheat real quick i'm going to put a var here so that we can use this code block again so let's press enter and you'll see that nothing no errors were thrown here and the reason is we had some invalid code but it got caught here and then we didn't run any code in the catch block so if we try this again no pun intended here we can actually print some details about this error so let's console.log something where we say air is an instance of we'll say type error and in this case it is a type error because we're trying to call a method that doesn't exist on this data type and this instance of operator is basically going to look at whatever is to the left of it which is that newly populated error object and it's going to say is this an instance of or kind of like a copy of the template that we call type error now that's going to hopefully return true if it does what we expected and then an error actually has a name attached to it or a message so we can say air dot message and print out whatever that error message is so you can see that if you looked at the instance properties we have dot message so that's how i knew how to access that alright so we press enter and now we get the value of true because this evaluates to true and then this sentence right here is going to be the error message so you can see that we have handled an error what we call gracefully we have not crashed our application and this is going to come in handy when we're using external libraries that we don't know how they're going to behave and finally kind of going along with these air objects we didn't talk about those much we also have three more data types that we have not touched on so let's clear the screen and look at what we're talking about the first one is going to be n a n for not a number so that's actually a data type then we have undefined or actually let's go with null we'll talk talk about null first and then we have undefined and you can see if we go to the documentation we go to the main javascript documentation with the built-in objects you will see that we have not a number right here so you can see a little bit about that you can also see if we look at the built-in objects that we have undefined somewhere so here's undefined and then finally we have null value right here all right so these are all you know built-in objects and in like when we're looking at this the null and undefined i believe are primitives so let me just check that make sure i'm talking about that correctly so yes the the undefined and null are going to be primitives as we talked about a little bit earlier so let's start with the least common one which is going to be not a number so this is kind of something that it it's not really used all that often you probably won't even see it all that often it's just nice to know about because you may get this response um at some point so basically not a number is meaning exactly what you would think it would mean it's saying that something is not a number so if we had a string and we set that equal to some string and then we tried to coerce we've done this before if you surround a number or like a string value so let's say that we had this number constructor or whatever and then we pass in a string but that string is equal to a number value this is going to work fine but if we tried to pass in my string you're going to get not a number because you can't possibly convert a string like that into a number so that's where you might get not a number you also might get it if you're trying to you know add uh that string with a number that doesn't work either and then you also will get it if you say my string multiplied by 2 divided by 2 or minus 2. you're going to get not a number in all of those cases again you're not going to see this very often so i'm just going to move on from here the next one is going to be null so unlike not a number you're going to encounter null values pretty much all the time and what this represents how i think of it is going to be the intentional absence of a value or a placeholder that you put in to a variable so if you were to define a variable so we'll say let play or no not placeholder we'll just say let my number and then we'll set it equal to null it says we're redeclaring it because we've already defined my numbers so we'll just say num equals null and we've redeclared this so i've been typing into this console too much and using the same variables variables so let's go ahead and refresh the whole page all right so this should work better we'll say num does not equal 20 it'll equal null so this is a placeholder value and this evaluates to a falsy value so if we say um num triple equals false then it's going to say false because it's not perfectly equal to a boolean but we say if num and then we get into a conditional block well console.log this will not be reached because this will evaluate to false so this block will be reached and when we press enter you'll see this will be reached because we said if num and num equals null so it's a falsy value so this does not get reached so that's just something nice to know as you're using conditionals and then finally we're talking about undefined which is very similar to null but instead of being an intentional assignment of value so kind of a placeholder this is when you just declare a variable so let's say we declare a variable called myvar and we don't assign it to a value now you've probably been seeing this undefined happen within the console but that's not actually representative of what we're talking about here so if you were to type console.log myvar you're going to get undefined and you're going to get it twice because it always prints undefined within the browser console but this actually does equal undefined and if you were to pull up this conditional statement again and you put in my var it's still going to print this else statement because you didn't reach this because this evaluates to a false value all right so that's it for this lesson number six it was a very long one obviously split up into multiple videos but if you made it here congrats and i look forward to talking about some more complex subjects we're going to try to get a little bit away from javascript but in the meantime i do have some practice problems for you and you can continue on to practice some of the things that we have learned within this lesson we've done practice problems in previous lessons we did 25 of them that took over two hours to do and you can check out this video to go to those those will be a little bit easier for you but now in the previous lesson or lesson six um i guess it's this lesson we went through a lot of the built-in objects and then the associated functions for those objects within javascript so we have a much broader understanding of what's available to us and therefore will be a lot better at solving these challenges so i'm going to walk through each of them i recommend that you try them out first on your own and then you can go through my solution we'll kind of look at some community members solutions as well now in the previous code challenge video where i did 25 of them i didn't zoom in so i made sure to have a nice big screen that you can see that was a little bit annoying to actually edit that video so hopefully this will work out better but anyways go to the link in the description to get to the collection we're going to be working on so this is my profile on code wars um and if you go to my profile or no this is my profile if you go to my profile here you can come down and you can see the collections and you can see that for lesson five we did 25 problems and then for lesson six we have some challenges and once you get here you can click on the train now button which is going to take you into all of these problems that i've laid out for you here on the right and there's 10 of them so we'll go ahead and click train now which is going to basically put us in this list and keep us there when we press you know next kata or whatever that's called i think it's something that has to do with jiu jitsu i didn't know in the previous video but i think i learned since then again leave it in the comments if you know totally what it means anyways let's get started with our first code challenge which is going to be list filtering now if you remember from the previous uh couple of videos we went through the built-in methods and one of those was the filter method on an array so in this case i would guess that we're probably going to be using that to solve this problem now i haven't seen any of these um or i've seen them i picked them but i haven't tried to solve any of these so we're going to be going in real time but i think the filter method's probably going to be what we want here so it says in this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out so this is a perfect use of our filter method we'll go here and as we always do let's change our parameter name so it's more descriptive so we'll call this so num list or even better num array and what we have to do is basically return num array and then we want to add the filter method on the end of that and the filter method as you might remember is going to basically take a callback function and whatever that callback function returns if it returns true on the individual element that we're looping through then it's going to keep that in the array if it returns false then it's going to throw it out and not include it in the resultant array so just for the sake of clarity i'm going to actually break this out into a separate function and then we'll refactor it so that it's a little bit more concise so we'll say our callback function is going to take an array item so that's how all of these methods on the arrays work they pass in the array item to the callback and then we're going to return a condition so in this case we want to look and see if we have a non-negative integer so any negative number or string is something that we need to filter out so we'll say first off if the type of array item which is going to return us with the type of the the variable if that is not equal to a number which we actually have to put in quotes so let's go to our our little playground here and if we have a number something like this and we say type of 20 it's going to give us number in quotes and then if we put in some sort of string it's going to give us string so we're using that knowledge and we're saying if the array item does not equal number then we want to filter it out and then we also want to add a condition we can say if it's not a number or with these two pipes or if the array item is negative so how do we figure out if a number is negative well we could just multiply it by negative 1 and if that turns out to be positive that's the case but we could also do something else so in our callback function we need to return either true or false which will determine whether this individual array item that we're looping over will be returned in that final array so in this case we have two conditions so we'll put this in a comment we'll say our first condition is that it is it is a number our second condition is it is positive so all we have to say is if the array item is greater or equal to zero um and the type of the array item is equal to number then we want to return true otherwise we return false here and exclude it so this little type of operator we've looked at it before a couple of times just to show you what i'm talking about i have up here if we say type of string or put a string in there it's going to return the value of string but if we put in say a number it's going to give us number so that's what i'm doing here i'm saying if the type of the item is equal to number then we want to keep it all right so there's our callback function this should work and all we would have to do is say return num array dot filter and then we pass in our callback function and let's see if that works when we test it so it does work but there's a couple ways we can refactor this the first way is that we don't need this conditional all we have to do really is return this expression right here which if this expression evaluates to true we want to keep it and if it evaluates to false we want to throw it away so if we just return the expression it should work just the same now another way we can refactor this is by taking this entire callback function and simplifying it so instead of passing it in as a declaration or whatever we can just define it right here so we can say array item and then use an arrow function to actually return this so let's break down onto the new line and we will return this expression right here and then we can get rid of this second function test it again and we should pass it now the last thing that we can do we don't even need these little brackets here um we can get rid of those and get rid of the return statement because if we put it all on one line it should work just fine so let's go ahead and test that looks like we got some sort of syntax error uh yeah we have this little semicolon at the end which is not valid so let's go ahead and test it now and we're passing it so that's how you might take this and refactor it to work correctly let's attempt the solution and we passed them all so we solved this one just okay so we'll submit it and see what other people have used to solve this solution all right let's go down here and um let's see this one did something very similar to us looks like they're saying if this is equal to number so i don't know if they accounted for negative numbers but maybe there's something about this that does um down here let's see well maybe i misread this actually so i wonder what the instructions were maybe go back and and see i guess maybe we didn't need to look for non-negative numbers i don't know all right let's move on it's not super important you can go back and kind of look at that on your own all right our next challenge is array.diff so probably getting the difference of an array of some sort so let's go into the instructions it says your goal in this kata is to implement a difference function which subtracts one list from another and returns the result it should remove all values from list a which are present in list b okay so looking at this we see that 1 is in both of these arrays so it's going to remove that and leave us with an array of 2. in this one we have two present in both arrays so we're going to remove that two and we're left with one and three which gives us our result so that's basically how this is working and hopefully we can use one of our built-in methods to solve this so just a fun fact if you go to the lodash library um this is something i talked about in a prior lesson or not a prior lesson in this lesson in a prior video you can go to the documentation and there's actually a method here so we can go to the intersection the intersection method is going to find what is alike within the arrays but we're trying to find the difference so we can go to the difference method right here so ultimately our challenge is to implement this method which we can use in lowdash it's very just kind of an interesting thing to know and if you start using the lowdash library like we might do kind of later in this series it will come in handy for this reason so what we're trying to do is find the difference and i think the way that we can do this is by using the array.includes method let me pull up the mdn documentation so we'll go to the web docs go to javascript go to built-in objects click array and now we can go down to the includes method which we talked about in a prior video in this lesson so this is basically going to figure out if something is included in another array or if a value is included in another array so if we were to loop through one of these arrays and then ask is each value included in the other and if so just remove it then that might help us solve this problem the first thing i want to do is check for the edge cases so if you see in the test down here there's a couple times where the first array might be uh nothing or the second array is nothing and in those cases we just have to return whatever is in the other array so let's say that if a dot length is equal to 0 then we want to return b so we want to return the other array and then we'll say if b dot length is 0 then we want to return a and let's go ahead and test that to see if we got a couple of them right and you see that we passed one of the tests because it passed in as an argument in empty array so now we have to actually solve the question of how do we get that intersection and figure out what is actually different between the two arrays so i'm just going to open this up here to full screen and then maybe we can put in an example just that we're anchored to something so let's put in something like three five six six 5 and 2 and then the second array will be something like 3 and 6. so we know that we want to have in the final array we want 5 5 and two so let's see that how we can actually solve that so what i think we should do is loop through this one of the arrays so we'll say a dot four each and then we have to pass in our callback function which will do something on each iteration so we'll say array item and then we also probably want to grab the array index and i'll show you why in just a second so let's drop down into our function here and this needs to be stored in a result we could also just return it but we'll store that in a result and then we'll return the result at the end of the function so every time we iterate through this we're going to actually use each item check if it exists in the other array and if it does exist we're going to remove that item from both of the arrays in each iteration of the loop in order to do that we need something that removes an item at a specific index of an array so let's go on to the documentation and just refresh ourselves on this method called splice now this is something we talked about in a prior video of this lesson but this is going to change the contents of an array by removing or replacing existing elements so we can remove an element at a start index and then we can that's kind of how we're going to solve this so we'll say that if the array item or actually let's say we're iterating through array called a so if b so the other array includes the array item if that includes the array item then we want to remove it so i'm going to put some comments here we have found this element in uh both arrays so we know that we need to remove that so how do we do that well we have to probably get a new array for each of these and then now we're going to be working with that as that uh you know most recent array so let's make a variable up here for we'll say current array um a and we'll just set that equal to i don't know we'll put a placeholder in there called null and then we'll say let current array b equal null so that will be the placeholder that we're going to assign these if we have mutated the array so now what we want to do is create two separate arrays so we'll say new a and then we'll come down here i'm not done with this yet but we'll say new b so we're going to grab two arrays that have these values removed from them and the way that we do that is basically by saying a dot filter so we're going to use the filter method and then i'm just going to pass in the name of the item as item because we don't want to conflict with this right here that we've used in kind of the parent callback that we're using and then let's go ahead and return this so long as this item that we're looping through does not equal the array item that we want to remove so as long as item does not equal array item we want to put that into the new array and we can actually use the same syntax down here we just need to replace a with b so this will be the new array for each of them and all we have to do here is replace this with our placeholder so current array a and i'm actually going to just call this new a just for simplicity and then new b and we'll change that here so new a and new b and so now each time we basically what we're doing so far here is we're looping through every element within the a array and then we're going to basically create a new array for both of them with the only the elements that have not been matched across both of them so let's see what this is returning i'll exit out of here let's just test it looks like we've got something going on here so let's go ahead and print what the result is before we actually return it so let's print the result and see what we're getting so a big mistake that i made that i just realized is that i'm actually modifying the original arrays and we don't want to do that because that's basically saying okay we're looping through both of the or through this array but within the loop we're actually modifying it at the same time and we definitely do not want to do that so let's open up the code block once more and now what we need to do is we need to get a copy of each of them so we'll say a copy is going to be equal to a dot slice which is just going to create a copy of that array and then b copy is b dot slice so that's just going to give us two copies of the array and now what we're going to say is instead of a dot filter and b dot filter we're going to do the a copy and the b copy and that's how we're going to sort through these let's go ahead and test that and see if it gets us an answer here so looks like we're getting undefined for our result and i wonder why that's happening so let's see oh that's because okay i'm just not thinking here so we're not going to assign the for loop to a value because it doesn't return an array it just loops through it doesn't really return anything so returning the result is going to not work so what we need to do is just return either one of these i believe so let's see what happens if we console log new a and then maybe return it okay so we're passing more of them now let's see it looks like this sample test it says if a is an empty array then we want to return an empty array so i think i messed that up here okay so here we have the first one empty so we want the result to be empty so if a dot length equals 0 then we just want to return an empty array rather than the b array so let's test that looks like we passed all of them let's go ahead and attempt it i'm not sure if this is going to pass it looks like we got some problems if we have negative numbers maybe let's see what we're dealing with here okay i think something that i'm doing wrong here is i'm taking a copy of the original array each time but we probably want to take a copy of the new array that we've created so each time it loops through it's going to remove you know one of the elements but if we keep making a copy of the original array we're going to keep adding back those ones that we removed already so let's go ahead and instead of making a copy here i'll expand this out we'll make a copy of new a and new b so this should represent the updated array so how this is working i just want to take a second to explain this let's say that we have the following example so we have one two two and three and then maybe we have two and three or something like that so what's going to happen is we start with array a and we're going to loop through it with this for each loop so we're saying okay we're going down into our code new a is going to be equal to null to start and we're going to be taking a copy of that which is going to give us null and then we're going to say if b includes the array item so in this case the array item is going to be 1 so we're saying if this array b includes the value of one then we're going to drop into this code right here and if that's the case then we're going to take new a and we're going to filter it and set it equal to or we're going to filter the copy and set it equal to new a so we probably need to initialize this to be equal to the original array so we'll say we'll start it with a and then we'll start it with b there so now we're starting with the original array and each time we get down into the loop we're going to take a copy of whatever has been updated and each time in the loop we're going to check okay is one included in this second array yes it is so now let's set new a which was the original array at the beginning but now we're going to filter out this item that we found in both of them so now as we go through the second time it's going to be removing elements so in the first iteration of this loop 1 is not found here so we don't do anything the second time that we come around we look at 2 and we see that 2 is included in both of these arrays so we are going to drop down into this if statement and we're going to reassign these placeholder arrays with our filtered array that does not include the 2 anymore so it's going to just have 1 and 3 and then 2 and 3. and then finally we get to the last one after a couple iterations we see it's in both we remove that and then we assign it to new a and new b so let's see if this works a little bit better looks like we passed the easy ones and then we passed all of them so this was a tough question like if you look at the difficulty of it 6 kyu and i once once again have not gone through this beforehand like i don't know i didn't know how to solve this problem before i looked at it so um hopefully that gives you a good idea of how thinking through this works like it takes a lot of iterations it's not something where i'm just like all right we're going in and solving it i know exactly how to do it you have to try some things you have to test it you know you realize oh i didn't account for this didn't account for that and eventually you will get to the answer so this was a really tough one i would definitely not call this a beginner javascript problem but hopefully you can see in our big solution here how we have used a bunch of different methods so we use slice we used filter we used includes and we used four each we used all of these in the same problem and that's how these built-in methods of javascript can really come in handy and it looks like after solving that since it was a tougher problem looks like i have leveled up here i started this code wars thing uh to make these videos so that's why i'm not a little bit higher up all right enough notifications here let's go to the next problem looks like we have another tough one 6 kyu so let's see if we can solve this a little bit quicker than the first one that we did here okay so let's look at some of the potential inputs and at the bottom it says note all the hashes are pre-validated and will only contain uppercase and lowercase letters a little dash and looks like a period so that's all that we would expect to see um and then looking at the examples you see we have a list of objects where each object has a property called name and as we scroll over okay that's all that we're working with there so in this case bart lisa and maggie so it looks like we have to put a comma after the first one probably a comma after the second one if there's more than three but in this case there's three so you put the and instead of the comma okay and then in this case where there is nothing in the array so it's empty we just return an empty string so that's a really easy one to start with i'm going to actually zoom out this is a little bit zoomed in i wanted people to be able to see everything but in this case we're running out of space so let's go if the names dot length equals zero then we want to return an empty string and if we test this we should pass to pass at least one of them for that empty array so we did that we got that test case covered now we have to actually go through each of these so let's see how that how we can do that we'll probably be iterating through the array with a for each loop but let's see okay let's try that so we'll say names.for each and then we can say name will be the argument that we're dealing with let's put some parentheses in here because we're actually going to need need the array index which is the second argument of the for each callback and again if you wanted to see that if you wanted to actually figure this out on your own if you go to the for each method so i go to technologies javascript built-in objects array and then i go to the for each method and then you can read all the syntax and it says the callback is going to have these parameters right here so it first passes in the current value then the index of the array that we're looping through and then the original array so that's how i knew to add in the name and then the array index and you could have just called this the array item to be a little bit more clear so let's break this down onto a new line all right so we're looping through and that's not going to return anything so we'll probably need to store this in our final string which will be equal to nothing to start all right so we'll loop through um and actually if this array has a length of 0 then this for each loop is not going to do anything so we probably could just return the final string and instead of having this if statement up there we'll just have this so let's test that make sure it works with that empty array which it does all right so back to here we're basically going to be grabbing the name property so we'll say name is going to be equal to the arrayitem.name because we know that the array item is going to be an object like right here and it has a property called name alright so we have our name and we just want to add that to the final string so let's see we need to first check if the array index does not equal the total names array dot length so base so basically what we're saying is that if we're not on the last index of the array then we want to do something so if we're not on the last index of the array we want to take our final string and we want to add the current array item name which is stored in the name variable that we put right here so that's going to just add the names and then of course we need the comma so we want to add the name so let's just remove this equal sign and then we'll say final string equals final string plus name and then plus a comma and then a space all right so that's going to basically reassign that final string and add the names with the commas so let's uh console.log the final string as we have it it's definitely not going to be right but we'll see what we're getting okay so we have bart comma lisa comma maggie comma homer comma and marge comma all right so that's kind of what we're expecting here but now we have to add in the and symbol and you can see in the last one right here we've put in a comma so let's see what this one actually was passing in as a test okay so one mistake that i made if the array index is not equal to the names.length remember the length of the array is going to be literally how many elements are in the array but the array is zero indexed so we'll actually never reach the index that is equal to the length of the array so we have to say the names.length minus 1. and if we test this we should not see this output here we should basically skip that one so you see that we skipped marge this time so that is actually correct now we'll put an else condition in here so otherwise if it is kind of the last element of the array so in here we'll put the final string will be equal to final string plus the and symbol and then plus the name itself all right so i don't think this is going to work perfectly but let's test it yeah so we're seeing and marge without a space here so all we need to do is add a space in there and test it alright so this looks a lot closer to what we're expecting so what we're missing here why it's failing is because we still have this comma here and when we have the and symbol we don't want the comma at the end so basically um this condition is not going to be correct so we want to actually identify two different situations we want to identify when we are on the second to last and then when we're on the last one so instead of saying not equal to let's say equal to so if the index that we're working with let's go ahead and expand this a little bit so the index that we're working with is equal to the last one that's when we're actually going to paste in this right here so we want to put and name okay otherwise so else if the array index equals names.length minus two so the i guess third to last we want to say the final string will be equal to final string plus the name but we don't want any of the commas going on here and then otherwise we just want to return the final string will be equal to uh final string plus the name plus a comma and a space so let's try this out and see what we're getting as output you can see that we're getting a lot closer so we have everything correct except we need a space right before the and symbol so let's just add that real quick here's where we're doing and let's just put a space there and test it again and now you can see that we're passing most of the test cases now we have one down here it says wrong output for a single name so if we have a single name we probably need to just return the name itself okay so we can just cover that in a separate test case so we can say if the names dot length is equal to one we're going to just return the names zero dot name so that should cover when we have a single name so we passed all of them let's attempt it and we passed all of them so this was another really tough one um probably not a beginner problem necessarily but hopefully again you see how we're iterating through this and and trying out different things until we get to the final solution all right so let's submit this one and go to the next as you can see someone solved it with the reduce method which we learned about in the prior video of this lesson but i didn't think of that so that's a that's a useful one and basically anytime that we're using like an accumulator so in this case we had the variable called final string so go back in the video to see that if you have a accumulator variable followed by a for loop that modifies that that's always a good use case for the reduce method i didn't think about it in this particular scenario but that's that's always a good use case this one's very interesting i'm not going to go through all these but be sure to look at them because people come up with some very clever solutions all right so let's look at this one this again is a pretty tough one so i guess we're getting all the tough ones in a row and then it's going to get a little bit easier here so in this problem it's basically the name is take a 10 minute walk and it's saying you live in this city blah blah blah where the roads are laid out in a perfect grid and you arrive 10 minutes early to an appointment you want to go take a walk you've got an app that generates a little route and you can go either north south east or west and each unit is going to be a single block all right so we need to figure out if a given array of directions is going to put us back at our original destination and it's going to take exactly 10 minutes and it says that each block is going to take you one minute so you takes you one minute to traverse one city block so we're basically returning true if it does false otherwise and then it says note you'll always receive a valid array containing a random assortment of directional letters and you're not going to receive anything other than north south east or west so in this case i think i have an idea of how we might solve this just initially so let's open this up and say the first condition we know that if there's not exactly 10 elements in the array it's going to take us more or less than 10 minutes so we'll say if walk.length does not equal 10 we're going to return false all right so if it doesn't equal 10 we're returning false and not going to the rest of the code if it does equal 10 then we'll go into the rest of the code and see if it gets us back in 10 minutes to our original destination now in this case i think what we're going to do is initiate or initialize four different variables so we'll say north offset equals zero uh west offset equals zero and we're going to do this for all of the directions so east offset and south offset all right so now we're going to loop through the array so we're going to use the for each method and we're going to pass in instead of array item let's start learning how to actually name our parameters to be a little bit more descriptive so we'll say direction which will represent the direction that we're going to receive and then we'll go down into the for loop and for each direction we're going to set our variables offsets so we'll use a switch case statement so we're going to put in the direction in the switch statement and then we're going to say case if it equals n then it's going to be north alright so we're going to put a break statement in there if it is west all right and then we'll just put the cases for all of the other ones we don't need that last break statement because it'll get there no matter what okay so we're going through all the different cases and in each one we're going to change the offset so if it is north we're going to say the north offset is going to be plus or equal 10 all right so that's just going to say or not 10 we'll just say 1. so we've gone north one block if the case is north and we're going to do that for all of these different cases so we'll say west offset will be plus or equal one and then let's see east offset plus or equal one and then in the last case we will say south offset plus or equal one so if we reach our destination like if we get put back to our destination then i think the sum of all of the offsets should net to zero or not not the sum you have to subtract them so the north offset will offset the south one and then west and then east so basically if north minus south equals zero and west minus east equals zero then we know that we have returned to the original destination so if we go down to the for loop here we'll say if north offset minus south offset equals zero and west offset minus east offset equals zero then we will return true otherwise we return false now in all these cases you're going to start catching on that when we just return true in a conditional oftentimes we can just return the expression itself so let's copy that and instead of this complicated block let's just return the expression itself all right so let's break out of here and test it and see what happens and we passed all of them so my assumption was correct you just calculate the offsets they should net to zero if you subtract them and if the you know left and right offset is zero and the top and bottom offset is zero then you've returned to your office or your appointment so let's attempt it see if we covered all the cases and we did so awesome let's submit it now this one i think was a little easier than the last two that we did but still required a little bit of thinking and i'm sure someone solved it in a much more efficient way so when it loads we'll see all right so the first solution here pretty much the same thing that we did so we did pretty good on this one all right once again we have a 6 kyu so might be a little bit tough but let's jump in and see it says write a function called persistence right here that takes in a positive parameter num and returns its multiplicative persistence which is the number of times you must multiply the digits in num until you reach a single digit so i don't understand this fully at first but let's look at the examples alright so if we pass in 39 it says we do 3 times 9 which is going to be 27 right here and then you do 2 times 7 will equal 14 and then 1 times 4 will equal 4 which is a single digit okay now does it always do positive numbers okay it says positive parameter okay so we know it's going to be a positive number so we know how to identify whether the solution is a single digit we basically say you know if it is less than 10 it's a single digit okay so we're probably going to go in some sort of loop here to iterate over each new solution and we might actually use something called recursion i'm not sure about that yet but we'll see so let's see if this works so we'll say um how do we get the individual pieces of this number though so let's go i can't actually remember if you can do this but let's take a number like 299 and see if we can access individual pieces of it looks like you cannot so we have to figure out how to get the first digit of each of the numbers so in this case i probably should know this but i don't and in the real world this is what i would do i would just open up a new tab and say find or maybe not find we'll say get first digit of a number in javascript so this will work get the second digit of a number okay so looks like we're turning it into a string grabbing the first digit and then converting it to a number okay so let's put a num equal to 299 and then say num to string okay it gives us 299 and then we can grab the first piece of that and then we will convert that to a number now there's a quick way to do this you could just put it wrap it around with this number object and it'll turn it back to a number or what you can do this is a shortcut you can just put a plus sign at the beginning and it automatically coerces it into a number so this is the pattern that we're going to use we're going to grab each of the digits of that number and then we can see you know multiply them together basically so the thing i'm curious about is whether we have okay so if we have three digits here we have to multiply all three of them okay so that's good to know so it could be up to maybe you know four or five or six digits so we have to do you know this times that times that times that so that's something we have to take into account here now if it's already a one digit number it says to return zero so let's just say that first we'll say if num is less than 10 which means it's a one digit number we're going to return zero and if we test that we should get at least one of these correct so if if it passes in a one digit number we got that correct okay so the next part let's see so what i think we should do here is we should convert the number to a string and then maybe instead of doing what i was showing you over here converting it back to a number maybe what we can do is take the string and put it into an array so let's go ahead and do that string version is going to be num dot 2 string and then from the string what we know we can do is actually convert that to an array so we'll say array version is going to be equal to the string version and then let's see i actually can't remember exactly what we used there so let's go to the documentation and go to the string methods is what we're looking for the reason we're looking for a string method is because we're trying to convert this string here into an array so let's go to built-in objects string down here and then let's look through the methods i know that there's a way that you can turn this into an array okay so it should be the split method yep that's what we need i don't know why i couldn't think of that but if we take a string so let me just use an example so you remember we can actually write a string literal so some string example and then we can take that literal string and chain a method on top of it because it wraps it in the actual string object that has methods automatically so what we can do is say split and then if we just pass in a empty string it's going to split it by each of the values here and that's exactly what we want to do so let's go ahead and see how that would work so if we say num dot to string so we first convert it to a string and then we say dot split and we pass in an empty string it's going to give us each of those numbers in string form all right and then from here we can basically convert those to numbers and multiply them together a good way that we can do that is by using the reduce method so if we chain on one more thing here we can say reduce and then we can say the the [Music] accumulator which is going to be accumulating that multiplication operation but we have to pass in a callback function so we'll put the accumulator and then the actual array item or the number so maybe we'll just pass in num for number and then now what we want to do is accumulate so multiply equal so it's going to take a q like whatever that's equal to now and it'll multiply that by the new number and store that in the same variable called acc for accumulator so we'll just do that multiplied by a number and we get the answer of 162. all right so that's our that's our solution here so if we do 2 times 9 times 9 we get 162 which is our answer here so this is basically what we're going to do and we can do it in one step like we've done here but we can also see it a little bit more clearly if i break it out into steps so let's go ahead and do that we've already converted it to a string now we want it to be in an array so we'll say split by an empty string and then finally we want to get the multiplication [Music] value is going to be equal to arrayversion.reduce and then we of course have our accumulator and our number and then what we want to return from that callback function is the accumulator multiplied not added sorry multiplied by that number okay so that's our callback function multiplication value should get the multiplication of all of them and then we'll say if multiplication value is less than 10 then we want to return true so you can already see where this one is going we can just return multiplication value is less than 10. so if it is less then we return true otherwise we return false now the problem with this is we have to figure out how many times um we have to iterate through this before we get something that is less than 10. so we're not quite done with the solution yet so this i believe is where the recursion comes in and recursion in coding languages is probably the hardest concept that you could learn so this one don't beat yourself up if you don't understand what this is doing fully um this is something that took me a long long couple hours early on in my journey to fully understand like how it worked so don't beat yourself up but i'm going to show you how i'm going to do this and hopefully it will actually work so i'm not talking a bunch of nonsense here all right so for this solution i had to turn off the camera i didn't realize that we'd be dealing with recursion here until i looked at this problem but a trick that you can use for recursion and just to kind of explain this code just at a high level and again you don't need to understand exactly how it's working because recursion is a very tough subject but basically what we're doing is we you know go through this code which i explained a little bit earlier and this is going to give us our multiplication value and then for recursion where we're you know calling the function multiple times on itself you need a stop condition okay so the stop condition is when multiplication value is not greater or equal to 10. so basically in this else return and what we're doing here when we say return 1 plus the persistence what it's doing is it's this is just a trick of recursion and if you read up on how recursive functions work in javascript you'll see this notation right here but basically what we're doing is we know that if the stop condition is met then we return the value of one otherwise we're going to keep calling the persistence function until we reach that so if we add 1 to the return value of each iteration or recursion then we're going to accumulate a total value of how many times we went through this so again this is a little bit confusing it was really confusing to me i had to brush up on recursion a little bit to solve this problem but basically this is i think the best solution that you're going to find for this problem so let's go ahead and attempt it we passed all of them so let's see how other people solved it maybe there's a way to do this without recursion but nevertheless this is probably the most efficient solution okay so actually the first solution might be more simple than um what we used here so this doesn't use any sort of recursion at all this is simply using the reduce method kind of as we had used so we we did this exact thing but it's looping through while the the num num counter is greater than one so and then it uses this times variable to kind of collect the number of times that we've gone through this this one right here this is like this is as concise of a solution as you could possibly get this is basically what we did it's a little bit syntactically compressed so you have a lot of little javascript tricks going in here but you can see the where we do one plus the persistence function so we're recursing over it so that would be the most clever solution as you can see here but once again recursion is a very tough subject i wouldn't expect any beginner that's watching this to really understand how to do that but of course you could have solved it this way which didn't require recursion like i used okay so we're on a 7 kyu problem which should be a little bit easier so let's see if that's the case it says atm machines allow 4 or 6 digit pin codes and pin codes cannot contain anything but exactly four digits or exactly six digits if the function is past a valid pin string return true else return false so this one looks pretty easy we're going to be using regular expressions here so this is exactly four digits this is four digits but it's alphanumeric so there's a letter in here so it's not valid so this one should be pretty easy we can go over here to actually look at these regular expressions a little bit so let's say that we have a string and the string is four digits here and then we want to match that against a regular expression of some sort so if we want to do that we're looking for numbers between 0 or 9 and we want either 4 or 6 so the way we can do that is we can basically i think i think we can just wrap this in a parenthesis and then repeat it with an or and then instead of 4 we pass in 6 here so let's see if that works if we add in one more it should not match it but if we add in two more it should um no so that's not working we could just separate this into multiple regular expressions because i can't remember exactly how to do this for both of them so in this case we're looking for any number so this will exclude letters automatically in a quantity of exactly four and we could be even more specific by saying the start of the line and end of the line with the dollar sign and basically if we're matching this then we're good so if we come up with anything here now if we put in uh five of them we're going to get an answer of null so we know that if we get an answer of null then it's not correct so let's go ahead and just write two regular expressions so we'll say four digits regular expression is going to be equal to what we just wrote here so let's just paste that in and then we can come down here and say six digits regular expression and just change this right here to a six so we're either matching all numbers uh in a quantity of four or all numbers in a quantity of six so we can basically return you know if both of these are null that means that we didn't match either of them so the answer is false but if either of them are true so they have a you know they're not null then we can return true so let's say we want to return um four digits right regex um and we have we actually have to match this up against the pin code so let's do that real quick so we'll say result one equals and then if you remember you can take the string pin number um i'm not sure if it is a string okay so it's passed a string value so we're good so we'll say uh pin dot match and then we'll pass in the four digits regular expression and then we might want to look for result number two and pass in the six six digits regular expression so here's what we can say we can say return result one dot length is greater than zero or no because if it's null then we don't have an array that we can check that on so basically we can say if result 1 equals null and result 2 equals null then we want to return false otherwise we want to return true because we matched one of them so null values default to falsy values so we can actually remove this and say if not result and not result 2. and you can see that it passed now we can simplify this even further so if we say let's see if we just return this expression can't spell so if we're saying if both of these are equal to null then we return false so let's see if that works it may actually be the opposite yeah so it's it's going to be the opposite let's see so all we have to do is reverse this by wrapping it in parentheses kind of reversing the result and this time it should pass so this is a decent solution but i guarantee you there's going to be solutions that are a little simpler than this where we don't have two different regular expressions there's definitely going to be a solution where we have a single regular expression that checks for both of them and i'll point that out when we get there okay so this one would be the best one the backslash d represents the digits so that's the same thing as our brackets and then zero to nine and you can see that it's doing the same thing that i was kind of trying to do i must have just messed up part of the syntax there but it's just testing against this regular expression so um that's basically um the best solution and this is the one i'd recommend using if you had the time to actually figure out how to write the regular expression that covers both cases now this is another regular expression this one should be even easier because it's 8 kyu so it says your task is simply to count the total number of lowercase letters in a string okay so let's take one of these complicated ones here and just take it into our little sandbox that we've been using so i'll paste that into the string and we'll start over with our regular expression so in this case we're counting the number of results that we have so we probably want to pass in the global flag to our regular expression so put that at the end and then we want to look for a through z lowercase letters and that's basically going to give us our answer i believe so if we just pass in a through z and then we pass in this global so if we take this off it's just going to match the first lowercase letter but if we put it back on it will give us all of them so that's pretty much all we're having to do here and then we just need to count the length of the array so let's do that we will just say return string.match and then we'll pass in lowercase a to lowercase z we'll pass in the global option and then we'll return the length of that array looks like we failed one of them so cannot read property length of null so in this case there's probably no array that we're getting right so if there's nothing if there's no lowercase letters we're not going to get a valid array and you can see that here if we were to take out all of these lowercase letters you're going to get a value of null so we need to handle that instead of just tacking on the length we have to first check whether it is a truthy value so let's see how we can do this kind of simply so we can use a ternary operator to do this so if we if we um basically say if this is a valid expression so as long as this does not equal null then we're going to get put into this part of the ternary operator so let's go ahead and save the result so the result is going to be equal to our regular expression evaluation and then we're going to say if the result is a truthy value then we're going to return result.length and otherwise we're going to return false or something like that let's see what it says in the instructions okay so if there's nothing we want to return zero so sorry about that it shouldn't be false it should be zero all right so we passed it this time let's just clean that up and attempt it cool so this one was rather simple a little bit easier than our previous regular expression and again as i said in the lesson you don't need to necessarily know like all of the details about writing regular expressions it's just good to be able to read them and that's why i wanted to go through this as an example so the next one we're doing is going to look at some basic dates and this one is asking is the date equal to today so it says write a simple function that takes a parameter as a date object or takes as a parameter a date object and returns a boolean value representing whether the date is today or not okay so this last part is basically just telling us like hey there is um going to be you know the 10th of january and the 10th of february so you can't just look at the day of the month which um we will look at but not exclusively so let's just make a bunch of variables here so our check day so this will be the date that we're passed we can just use the get date method that's going to give us the day of the month all right so check month is going to be date dot get month i believe we can go to the date methods so if we go to the mdn documentation go to built in objects and go to date you'll see that we have the get month method the get full year method and the get date method now you might say get day but this is the day of the week not the day of the month so we need get date instead so this will be day or this will be month of year and then this is zero indexed so that's important to remember so january is going to be zero and then the check year is going to be the date dot get full year all right and then our date today is going to be very similar so let's just copy all this code down here and we'll say today instead of check and then we'll replace all this with um the actual current date so today is going to be equal to a new date that's how we initialize it and then we can pass this in here in place of the date so then we just return check day equals today day and all the other ones match so let's just write that out all right so it's a little bit of a long expression but it should work all right so we passed it now the quickest way to do this it looks like is this solution so what are they doing here so they're creating a new date converting it to a date string and checking if that is equal to the date that was supplied to date string i actually didn't know that you could do that because this will give you different times but i guess it matches if you have uh different times of the day so let's just try that real quick in our console just to see if it's true so we'll set a to a date and then we'll set b to a date and then see if b equals a which is false so if you go obviously those are not going to be equal because the value of the date is milliseconds since whatever january 1st 1970 at midnight but if we convert it to string so tostring and then try it with b.2 string that's going to give you a false value too so i'm not sure what's going on here oh to a date string okay that's what that's where we went wrong we're using two date string so that should work now okay so that equals true so let's take a look at that method on our documentation so it says two string which is not some not one of them that we covered in our lessons which is why we didn't use it here but it says this returns the date portion of the date object so that's basically going to strip off the time and check if it's the same day so that's a good solution didn't think of that but that's a lot more concise that than what we did this is what we did we just checked it up against all the different methods that we had covered in our lessons all right so let's go to the next challenge and this is alternate capitalization given a string capitalize the letters that occupy even indexes and odd indexes separately and return as shown below index 0 will be considered even okay so we're going to return basically two strings for each argument here the input's always going to be lowercase with no spaces okay okay to solve this we first need to get this into an array so we need string to be into an array and then we'll capitalize them separately and then we'll combine them back into a string and assign them to different array values so how that's going to look let's see we will say array is equal to string dot split and we're going to split it by an empty string which will just separate all of the characters by a comma all right so if we just did that and console.logged it here's what we're going to get here's our string right here so we've got all of each letter broken out and we know that we have to take this array and we need to capitalize it in two different ways so we'll just say array one is going to be string.split and then we will also have array two and then we're going to loop through these arrays so array one dot four each and we'll say for each item then we also probably want the index because we got to figure out if it's even or odd and then we'll say array 2 but let's not do that quite yet we'll just figure this one out first so for array 1 we want to check if the index is we'll say even so this will be even capitalization can't spell here all right so if it's even that would include zeros so how do we find out if a number is even well we could use the modulus 2 operator so if we came to our little playground here in the console and we said okay 0 modulus 2 is going to be 0. 1 modulus 2 is going to be 1. 2 modulus 2 will be 0 and 3 modulus 2 will be 1. so basically anything modulus 2 that returns us with 0 is going to be even everything else is going to be odd so that's all we have to do here we'll say if the index modulus 2 equals 0 then it's even and for that case we'll say array 1 at the index that we're looking at and then we want to set that to uppercase okay so we've got array 1 completed and let's go ahead and console.log that and see if we did it correctly and we didn't do this correctly i don't know what i was actually thinking here we already have the array item so we just need to take the item and make it uppercase i think okay so we have a problem here we're trying to modify the actual item which is not going to modify the original array i just realized that so if you go to the array methods and go to javascript built-in objects and go to array you're going to see if we look at the for each method which is right here the callback function takes the current value the index and the array so we want to actually grab this third parameter and modify it within our problem here so we'll pass in the item index and then the original array and in this case we'll take the original array we'll pass in the index of that and then we'll set that equal to the item.2 upper case so i believe this one should work now we'll see and you'll see that we did capitalize the first so that's even lower case on the second upper case on the third so that should be working and i believe we did that right but let's check one time so capulet capitalize the letters that occupy even indexes index 0 will be considered even ok so we did that correctly so array 1 is done and then we can basically just do this again for odd capitalization change this to array two and then instead of saying when it's equal to zero we'll say when it's not equal to zero and then we just need to return a final array and we pass an array 1 and array 2 and i think that should get us to an answer maybe we did this in the wrong order let's see oh i'm sorry i'm not doing this correctly so our arrays are um split out into individual letters so we have to convert those back to strings so let me do that real quick um array one string is going to be array one um and then join by an empty string which will actually yeah i believe by an empty string which will join them all together and then we can do the same thing for array 2 string pass in array 2 and then we will return those like this and see what we get all right so we passed them this time let's just open this up and review what we did so as you see we're going to first take these strings and turn them into an array so if we go back here if we have some sort of string and then we say split and we use a empty character it's going to put it all into an array then we go and we loop through that existing array with the for each method and we know that the callback function that we're using is going to pass the item that we're looking at the index of that item and then the original array that we can actually modify within that loop and that's we knew this because we looked at the documentation here otherwise you probably would have to do just a regular for loop which acts a little bit different than a four each so anyways we're going through and we're asking if the index is modulus 2 equals 0 which indicates it's an even number and when it equals an even number we take our original array we pass in the index into the brackets to grab the item and we set that equal to the item itself in uppercase form and then for the odd capitalization we just basically do the opposite if modulus 2 does not equal 0 then we want to make it uppercase then finally we join those back together so um if we go to this array here so we split them into an array and then if we add join with an empty character it just joins them back together to the original string but we've obviously capitalized a few of them in the middle so we join them back together and then we just place them in a new array and return that array and that should give us the answer okay i think this is getting to the end one of our last ones here if not the last one it says given a number return the maximum number that could be formed from the digits of the number given only natural numbers pass to the function numbers contain digits 0 through 9 inclusive digit duplications could occur so also consider it when forming the largest okay so that's a good hint for us to remember here's an example so three goes first two goes second and one goes third that makes sense that's the maximum number that we could form okay so basically we have to take our number and we have to order it from greatest to least so i think a good way to do that is we might have to convert it into an array possibly so basically what this is asking us to do if we think about it you know all these numbers are going to be in the descending order so you have the largest one first and so basically what this is asking us to do is sort an array because we know that we can take this number convert it to a string and then separate that out into an array and if we can just sort that array then we got the answer but the sort method does not do it correctly when we have numbers here so let's just type it in to google this is just an algorithm so sort array numerically in javascript and we'll see what the answers are so it says how to sort an array of integers correctly and let's see okay so we can just use the sort function we actually can use it we have to pass in a callback um that will compare the two numbers so um let's see the sort method takes a compare function okay so i was just reading this incorrectly so we can take a call back and look at the compare function and return the greatest number so let's go ahead and do that uh just practice real quick so let's say that we have an array of numbers so 2 6 3 1 and we want to sort that so we will pass in our single num and then we want to say let's see what does it pass into this callback so let's look the first element and the second element is what it's going to pass in there so if we go to our little playground here it's not just one number but we'll have the first and second element and so if the second element is greater than the first then we'll return true i believe so let's see how it did it in this stack overflow answer it says return a minus b so actually it's the other way around so if a is greater than b we return that as true and then that's going to actually sort our array for us so let's test that out real quick we just want to return first minus second and now we have it sorted in ascending order i wonder if we take it second minus first okay so that's going to do it in descending order so we want to do second minus first for our problem here all right so the first thing we need to do is get this into a string so we're just going to try to do this all in one line because it'll help us kind of visualize chaining the methods together so we're going to return n to string okay so that converts it to a string and then we want to split it into an array now we've got it into an array of numbers here and then we want to sort it and pass in the first number first number and second number and then we want to return second minus first okay and then that's going to give us a sorted array of numbers so maybe we won't do this um let's just store this in a result for now and then console.log that result and see what we have so far so we'll test that and you'll see that it prints the console a descending order of that array now we know that we can take an array of numbers and we can actually just combine those together with join so maybe that's all we need to do we just at the end here so hold on one second let's return this again or no let's not do that let's keep it in the result and then at the end here i'm going to break out for a second and we can actually put these down onto separate lines so we'll say dot split dot sort and then the last thing that we want to do is we've got it in the descending array so then we can join it back together as a string and that's going to give us one big number and then we need to just convert that to a number and all we have to do is add a plus sign at the beginning to do that so now let's see what our result equals looks like we're getting the correct number here so all we have to do is return this result now so we don't even need to store it in a variable we can just return it and let's see how it works looks like we passed everything and again this is just some indentation it doesn't really matter how you do this we're just chaining all these methods together and you could certainly have broken that out into separate steps so let's attempt it looks like we passed everything and see what others use to solve this problem okay so this person did not pass in a sort callback they just passed in no no function whatsoever and that's going to default to ascending order as you saw up here so it says the default sort order is ascending so here they're going to split it into an array then they're going to sort it to ascending then they're going to use the reverse array method to make it descending and then they're going to join it together and then wrap it in a number which we did by adding the plus at the beginning of the entire expression so you can see i believe yeah this one right here is the exact solution that we used so pretty good i'm pretty happy with that one and with that i think we are done with these problems hopefully you learned a little bit i know some of these were more complicated and challenging than you would expect for a beginner and certainly when we talked about recursion there for a little bit that's getting a little bit over the pay grade of someone just starting out but it is an important topic to explore and kind of understand it definitely will come up in some sort of coding interview if you're doing those but yeah hopefully you enjoyed this if you care to drop a like on this video i'd really appreciate it subscribe to the channel for future lessons um and hit me up on twitter i'm at zg underscore dev uh tell me what you thought of this video and um how the challenges went for you all right so i will see you in the next lesson i believe that is lesson seven this is an html crash course and i'm really excited to walk you through it and i want to show you exactly what we're going to build over the next couple of videos so here is our ultimate challenge that we're trying to create and what this does is you've got a bunch of buttons here on the right where we can click them and a random meme is going to be shown to the screen or you can have a joke displayed on the screen a quote or a riddle where you're going to get the riddle and then you have to reveal the answer here so this is going to be a really fun project but before we get into it we have to learn a little bit about html now you'll notice that it's styled pretty nicely and this actually has some css in it but we're not going to be covering that in this lesson number seven of the series in the prior lessons of this series we've been talking a lot about javascript and basically how to program in general now we're going to jump into html and then in the next lesson series we're going to jump into css and we're ultimately going to complete our introduction to front-end web development before we dive into the details of html i want to start with a little bit of an analogy that will help us kind of going forward in this crash course and then also the css one in the next lesson so that analogy is building a house if you are building a house you basically have three steps first you have to build the structure so that would be the wood the concrete the brick the roofing all that kind of stuff then you have to set up the function of the house so that would be the plumbing the electric the water lines all of that kind of stuff has to go into the house to make it functional and then finally the third step is to make it look good you obviously don't want to build a house that doesn't look good you want to paint it correctly and you want to do some landscaping around it put some siding on it all of that stuff is very important for the market value of the house so if our house was a website the structure would be the html now the function the electric the plumbing all that kind of stuff would be the javascript and then the styling so the the paint and the landscaping that would be the css now you could live in your house with just the structure or the html you wouldn't have any function so you wouldn't have running water you wouldn't have heat or anything that makes a house livable but you certainly could live in that house and you'd have a roof over your head now if you add in the function you could live with just that too so you know add in the electric and the plumbing and now you've got a house that is functioning but one that is not worth that much if you went to try and sell it because you haven't painted it you haven't put landscaping around it and you haven't made it look attractive to another buyer so that's where the css comes in that's where the styling comes in so ultimately the goal of this analogy is to say that html is the only true requirement of a web page but if you want it to do anything useful you got to add some javascript and if you want to make it presentable to other people your users you have to add in the css so that's just a nice analogy to remember how everything relates to each other and how html css and javascript play nicely together to create a full user experience on the web while these all work together we're going to be skipping the css part in this lesson so like i said we've learned a lot about javascript in the prior couple of lessons of this full stack roadmap series and now we're going to learn about html so we're basically going to be focusing only on those two how do they interact and just as a hint it's going to be a big conversation about something called the document object model or the dom and that's basically the model that represents the html and allows us to plug our javascript into that html and make it functional but at this point we're getting too detailed we'll get there in a second first we need to understand what is html how does it work and how do we write it the first thing that we have to ask is what is html and it stands for hypertext markup language and despite the last word language it's generally not considered a full-on programming language like something like javascript that we've been working with before it's something that allows you to structure your text and all different components that you're going to put on a web page but it's not a full-on programming language you can't add in a whole lot of logic behind the scenes and make things dynamic without adding in that javascript now there's a ton to talk about with html but similar to how i've been handling other concepts within this series i'm not trying to get into absolutely everything and do a super deep dive into it my goal here is to get you exposed and learning the most essential concepts of html those things that you absolutely have to know to build web applications i think a great place to start is to understand the overall structure of an html document so something you have to understand is that every browser tab that you have open is going to have one and only one html document is what we call it within it so that one document follows a standard you know boilerplate is what we call it structure and that structure is what i'm going to put on the screen right here this is going to be the most basic way to write an html document now everything within those html tags we call these tags um is going to define the whole document and then within that document you have a head tag which has a site title so if you look up into the browser tab you know the the name that you see within that browser tab is defined by that title within the head and then there's other things that go in the head of the html document some things like different style sheets uh meta tags all that kind of stuff that we're not going to get too much in detail with in this lesson now what we're going to be focusing on is everything within that body tag so that's going to represent the bulk of your html document and it's going to represent the stuff that you're actually seeing on that web page well the thing that i have up on the screen right now is going to represent the most basic form of an html document there is a more standard boilerplate that you're probably going to see so i'm going to put that up right now so this boilerplate html document pretty much has everything that you need to learn in html css and javascript i'm not going to go through absolutely everything that you're seeing here but i do want to cover a few things that are very important we're not going to actually get into writing these but if you'll notice you see within the head you see this link tag and you'll see that it references something called a style sheet and then it gives a file that is passed in so this is actually called a css document and that's going to be fed into the html to actually style it if you look further down in this document you're going to see a script tag which represents the javascript that is connected to the html and gives it function now in our case we're going to be working in an environment called codepen so what i've got up on the screen is a basic codepen document i suggest that you create an account it's free and you'll be using it throughout this specific lesson on html so if you look on the left side you see html css and javascript now going back to that boilerplate that i just showed on the screen this is basically a beginner-friendly way to write that so codepen has already taken care of connecting everything up so the css and the javascript are automatically connected to the html and within this html area this is actually going to be what we're filling in to the body so you know how i talked about there's the the whole html document and then you have the head and the body well this represents the body and you can just assume that css and javascript are all connected up and ready to go with this i'm going to be using this codepen playground because it's going to be a very user-friendly way to explain what html is all about later in the series we'll talk about writing our html css and javascript in our own local environment but as we've been doing with the javascript earlier in the series using the browser devtools console this is kind of the same deal we're going to just get into an environment that's easy to use right off the bat like i said html represents hypertext markup language and it only has a few key things that you have to understand about it what we're going to be focusing on a lot in this html crash course are two things so number one we are going to be talking about html tags and number two we're going to be talking about html attributes so what do i mean by those well let's take a look at this code pen and you'll see that we can write a basic html tag by using these little brackets here so this is this happens to be a paragraph tag um represented by p and within it you have this these little brackets here so the the tag itself is enclosed in the brackets and then between it this is where we put some text that we want to display in the browser and you'll see that once it renders we have some text displaying within the code codepen browser alright so the end of this is going to be what we call the closing tag and you'll notice that we have this forward slash before the tag itself so i'm going to put up on the screen a basic diagram that gives you the anatomy of an html element so if we're breaking it down you basically have an opening tag then you have one or more attributes that you have defined for that specific element then you have the content that you put within the element which can be just text or it can be other html elements in certain cases we'll talk about that in a second and then finally you close it off with a closing html tag so you might be wondering you know what is this whole attribute thing about you'll see you know in that diagram that i put up we've got a class attribute which just happens to be one of many attributes that we can define for an html element in this case the attribute we're talking about is a class attribute we're going to talk more about the different types you can use but in this case a class attribute directly relates to kind of identifying the element with css now we're not going to be talking a whole lot about css in this lesson but i do want to show you just the basics of what this attribute is doing so we might say some class defined within this class attribute and then we can come down into the css and define that and then we can maybe give it a different color and now the text up here is going to be red so that's what we're doing with this attribute but that doesn't mean that all attributes relate to css we also can have attributes that are totally unrelated to css so for example if we wanted to add an image tag here and we're going to talk about this a little bit more later so don't worry about the syntax here yet we can define something called a source attribute so this source attribute is going to actually give us the image url that we want to display within that image tag so let me get an image to put in there real quick and i'll show you what i mean all right so i'm going to put a dog image in here just as i normally do so we'll paste that in there and now you should see once it reloads a very large picture here so we're going to use another attribute that i just know from experience about we can put a width property on here and maybe define it to about 300 pixels so that it's much smaller and we can see the image all right so again you should not understand you know the the p tag and the image tag you don't need to understand that quite yet we're going to cover it in a second but what i want to uh demonstrate here is that these attributes can you know serve a lot of different purposes and they're ultimately the same thing like you can think about them just like javascript variables so just like we're defining some sort of you know variable here or attribute and then assigning it some value you can do the same thing in javascript so you could define an attribute and give it some value here and that's the variable the javascript variable that we are so used to working with from the previous lessons so it's kind of a similar concept but these you know what we call variables or attributes here are specific to these html elements that we are creating all right before we get any further into these details that you're not understanding quite yet i want to talk about the difference between an element so an html element and an html tag so when i think of an element i think of it as a complete unit so it not only has an html tag but it also might have some attributes or maybe even some event listeners we're going to talk about that a little bit later too so let me write out a full element real quick just like we saw a second ago so we'll say a paragraph element and then we'll close it off with our closing brackets so i think of this as a complete unit and a full html element and we can also add on some attributes so maybe an id is going to be p1 or something and then we can add a class attribute now it doesn't matter what attributes you're putting here i'm just trying to demonstrate this is a full html element now if we're talking about tags i like to think of those as the actual indicator for what type of element we're creating so in this case we're going to put a p tag indicated by the letter p right here so as we move forward think of the tag as the function or like what the element is supposed to be doing and then the whole element as a general structure of that html the next thing i want to talk about is the types of html tags that we can use so far we just looked at a paragraph tag and an image tag but there are obviously plenty more that we can use within our html documents so the first type of tag at least how i categorize it there's plenty more classifications that you'll find with official documentation but just for our brains to understand it i think we can think about this in terms of normal html elements and self-closing html elements or maybe tags would be a better way to say that so let me paste in a few of them here these are all you know different tags that we can use so the p tag the h1 tag div span and strong and all of these are what we consider kind of normal html tags because they have an opening tag right here and then a closing tag and then you put something in the middle of them to actually create the entire element so these are what we consider normal now there are some other html tags that have some different behaviors where they're actually called self closing and i'm going to paste a few of those within our code pen here so let's get a rid of these and now we're looking at self self-closing tags and this would be image input meta tags these go in the head of the html document link tags this is how we link up the style sheets to our html something that's going on behind the scenes here so these are self-closing which basically means we have everything we need within the single tag and we put this forward slash at the end of it so rather than doing this let me just give you a very specific example here so rather than saying image and then having a closing tag and then putting something between them that's not valid we just have to create one tag right here called image and then put the forward slash at the end of it to close it so these are the two types of tags that we're dealing with you'll start to recognize them as you get more experience writing html i'll be sure to point them out as we go now the reason that i talk about the difference between you know normal and self-closing tags is they have different behaviors so if you wanted to embed some content within a normal element so let's take a div element which i just think of as kind of a general purpose container that we can use in html and i can put some text in here it's going to work just fine as you see on the screen i can also enter down and put in another html element maybe a paragraph tag here and i can embed html within that div tag all right so that's a normal html element that has an opening and closing tag but if we tried to do this with something like an image tag all right so this is a valid way to write an image and we of course need a source attribute so let me see if i can get another image here okay so let me get an actual image in here uh so that this is a valid image tag all right so it's working on the right but what i want to show you is that if you try to put something within this tag so let's come right here and try to put maybe a p element in here and we'll put some text right there you're going to see that this is not going to work all that well so you'll see these weird brackets here at the end and while the overall image is working and you still see some text printed to the screen this is not valid html to write this is you know it'll render but it's not valid so just remember as we write these self-closing html elements you can't put anything within them now a more subtle difference that i want to point out you know within these elements that we call normal or the tags that we call normal so for example a paragraph tag we've got some text that we can put in there now we can also put stuff within this but does it make sense to do so and the answer is probably not so if we have a paragraph tag and we put some text in here and then we also try to put maybe another paragraph tag so we'll put some more text in there this does not make a lot of sense to me and it doesn't make sense in general when you're writing html because in general a tag such as the paragraph tag is not meant to have elements um embedded between it other than just plain text so there are some html elements that have these kind of container functionalities and then there are some elements that just are meant to store text and only text so while we don't want to put you know elements within a paragraph tag if we just change this to a div that's going to be more appropriate so we can put you know as many other elements within a div as we want to because it's kind of structured and meant to do something like that it's kind of meant to be a more of a container type html tag all right so we've gone through some of the nuances of you know what type of html tags you're going to be looking at now in this next section i want to actually walk through some of the most common ones that you'll see and you'll use not only that but i want to show you how to actually inspect your html in the browser and also how do we look these different tags up in our documentation you know if you don't know what something is or you don't know what it's supposed to be used for you can go to the documentation to figure it out like i said there are a ton of html tags that you can use and i'm not going to be covering you know even half of them here that's not the goal the goal here is to get you the most essential ones and help you understand how to find um and use different tags without having prior knowledge of them as we talked about earlier html is not styled it's all about structure so if you look at this page i've got pulled up on the screen you can see a bunch of really ugly web elements and we're going to walk through each of these very briefly just so that you can kind of get the gist of them and i'll show you you know for a couple of them how we actually look them up in the documentation and see what they're for so just as a preview we've got some content elements up here we've got headings paragraph tags then we come down into some more content elements so we have span elements which allow you to kind of custom style this this actually has some css within it which we're not covering but you can see how you can do things with the paragraph tags and make it a little bit more customized using span and em and strong elements and then we can also use uh images and links so a would be a link image and then buttons here so we got buttons and then we have some list and then finally tables like i said we're not going to be covering that a whole lot but this is just the landscape of most of the elements that you'll be using on a daily basis as a web developer but before we jump into that i want to show you exactly how we inspect these now you'll notice i jumped over to google chrome here i'm using that for a very specific reason that we'll kind of talk about a little bit later in this lesson but it's very similar to firefox all you have to do is right-click and click inspect and then from here if you rearrange this you'll see that the elements panel is where you can actually see the html within this document so if we click down on the body you can see all of these elements and every time we hover over them it will actually you know highlight it in the browser itself alright so you can scroll down and just basically see the source of this html document which is a great way to understand it now i also have this this exact document linked in a codepen pen you can find that in the materials in the description of this video so you can just look through this on your own time if i go through something a little bit too fast here all right the first ones we're going to be talking about the first html tags are heading tags so that is h1 through h6 now if we go to the documentation this is i'm going to just go through this exercise with you so that you can get used to doing this but we basically go to mdn we go to technologies and click html and then we can go down to the references and click the html elements from here we can obviously go through and look for all these different tags that we can use and in this case we want the headings so i'll click h1 and you can see that it just has one page for h1 through h6 because they're all so similar types of elements when you're on a documentation page it's good to read this first sentence here just glance at the demo but if you come down to usage notes this is where you're going to learn like what is this tag actually for you know as we talked about html is the structure of the web and therefore every tag that we're using has a very specific purpose so this one says heading information can be used by user agents to construct a table of contents for a document automatically alright so basically what that's saying for us is that we can use the h1 through h6 to organize our web pages so let's start with an h1 element all right and we can give it a page title all right so that's kind of what h1 is used for when you go onto a web page there's always going to be a title of that you know blog post article or just the page in general and usually it's going to be h1 that's what google searches for with the search engines so a lot of seos will you know recommend that you have this uh semantically laid out so that the h1 only occurs once on the page and then you have everything else as h2 or even smaller so we'll make some h2s here this will be a secondary heading and like i said this is going to lay out your document semantically based on the order of importance of your text so i think you probably get the point of this you can do this for you know h3 all the way to h6 not all that complicated so we're going to just stop it right here but you kind of get the point play around with this and and basically all i'll say about this is that h1 through h3 is probably the furthest you're going to go you might use an h4 every once in a while but generally those first three are sufficient to build a web page next up we have the paragraph tag which we've already used a little bit so if we come to the mdn documentation and we go back to our well let me just go through the full process so we get used to it so technologies html html elements and then we can scroll down and look for the relevant tag so we'll come all the way down here to the p tag so it says the html p element represents paragraph paragraphs are usually represented in visual media as blocks of text separated from adjacent blocks blah blah blah a bunch of jargon here that we don't really need to read too much about this is basically what you're going to see everywhere on the web so when you read an article blog post most of the text is going to be a paragraph element and it's very simple to write as we saw we can just put some text between the opening and closing tags and you've got yourself a paragraph element now while we're here i want to highlight something kind of important with html and there are actually two things so number one html is not case sensitive so if we wanted to capitalize one of these tags and then have lowercase tags over here on the end it's still going to work just fine because html doesn't care about capitalization now it also does not care about line breaks so if we had a couple elements here let's just make a couple paragraph tags and we put a bunch of spaces in here and we entered it down a bunch let's make some enter down and some more spaces you'll see that the output is going to be the same no matter what and that's because the spaces between the tags doesn't matter whatsoever basically when it's rendered in the web uh or in the browser it's just looking for the opening and closing tag and then it automatically knows how to lay that out on the page so it's not case sensitive and these line breaks and spaces do not matter between these elements all right with that said we're moving on to the next elements and that would be some other content elements that allow you to basically style your paragraphs but i will say that as we talk about semantic html the whole goal of the new html5 standard which is the latest spec similar to when we talked about javascript in previous lessons how there's the ecmascript standard in es6 all that kind of stuff well there's also an html spec that is supposed to be followed and in the latest one the goal is to basically use html for structure and function only so instead of using html to actually style elements you're supposed to use only css to do that that said you will see these tags i'm about to show you uh quite often in old code and people still use them so you need to know what they are and what they do so let's write a paragraph tag right here some text that i want to emphasize and make bold and styled alright so these words are going to be manipulated by other html elements and as i said earlier a p tag is generally not something you want to embed other html elements within so like i said you probably do not want to put another p tag within a p tag and put text in there but there are a couple exceptions and they relate to the strong emphasis and span elements so the first one it's going to we'll look at is going to be span so let's go ahead go back to the documentation go back to this reference and look for the span tag okay so we'll click span and it says this element is a generic inline container for phrasing content which does not inherently represent anything it can be used to group elements for styling purposes or because they share attribute values so on and so forth so basically what that is saying is if you want something an html element that's not going to break down to a new line it's just going to be in line with the rest of the text but you want it to look a little bit different or function a little bit different than the rest of it you might use a span tag so an example of that we'll wrap this last word styled within a span tag alright and as of right now you see that it is red the reason this is red is because behind the scenes i didn't tell you this but i wrote some css that will take any span element and turn it the color red using css so if i open this up you'll see that i said for every span element i want the color to be red now i don't expect you to understand what that css is saying yet we're going to cover that in the next lesson but basically that's the point of a span is you can take a single you know piece of text within a paragraph tag and you can emphasize it in whatever way you want using css similar to a span we also have the strong and em or emphasize tags so let's take a look at those in the documentation let's go to the em tag this is it marks text that has emphasis so basically italics is what it's saying and then if we go to the strong tag down here this indicates that its contents have strong importance seriousness or urgency which is another way of saying bold so like i said this in the new html standard you probably should not use these tags but i'm showing you them because you'll see them often people still use them and they are valid html so if i wanted to emphasize this word or in other words make it italic i can surround it with these with this em tag and you'll see that this one word is now italicized now we can also make a word bold by wrapping it with the strong tag and now this will be a bold word so as you can see we can use these different html elements to actually style our html but again not recommended i would go ahead and use the best practices and style these with css instead and i know you're probably wondering what the heck that means and basically all that means is instead of using the em tag we might put in a span here for the emphasis all right and now that this is um wrapped in a span we can change the css so right now it's just making everything the color red but if we wanted to put an attribute in each of these so we'll put a class and we'll say red color so for for this span it's going to be a red color let me make this a little bit bigger and then for this span we'll put an attribute in there or a class called italics all right and now we can come down in the css let me delete all of this so down here in the css i can basically look at elements that have these specific classes again i'm sorry for getting too much into this but i really want to show you what this what i mean when i say you should style it with css rather than the em and strong tags so what we can do is we can select that class in css and then we can basically say the font style should be italic all right and once we do that you'll see that this word is emphasized with italics again but this time instead of using the em element we're using a span with some css styles now we can also come down and select the red color class and just give it a color equal to red and now you can see that we're back to the original style that we had so again we're not covering css too much in this lesson but i do want to show you how it's working with the html in unison all right so the next elements that we got to talk about i think a big one is going to be the div element now this is going to be one that you use all the time and it's very important and i consider it to be just a general container element but let's look it up in the documentation and there's our div tag all right so this is called an html content division element and it's a generic container for flow content so like i said it's just like a generic container that we can basically separate out pieces of our html and you know the the most important thing about it is we can take each of these individual units that are within a specific div and then we can style it so very similar to that span element where we're taking a individual piece of text and we're making it look different than the others we can also use some css magic along with this html structure to separate out our content so let me show you what i mean by that so let's say that we have two div containers all right and then within these we have some elements in container one all right and we'll just make a bunch of those and then we'll do the same thing for elements within container two so right now on the screen these don't look separated at all you know you basically look at this and say like oh it's all just one big piece together but since we wrapped these within div elements we can actually put some styles on here to separate them out and like i said you're not going to understand how these styles are working quite yet we'll do that in the next lesson but i do want to show you how the div is able to separate out our content so the first one we need to identify by div one just an arbitrary class name that we're giving it and then div two all right so what we're going to do is we're going to make some boxes around these so all we have to do is select both of them and give them a border all right and from here you can see that now we're getting some separation of our html elements and we can even put some margin around there and now it's going to be very apparent that we have two very separate sections of our html document and that is the power of a div and you'll be using these div elements to section out your content all the time the next element we're going to talk about or not element but tag is going to be the a tag which represents a link in html let's take a look in the documentation again just as a recap technologies html html elements and then you can click a so this is actually called the anchor element that's what what a stands for and it says with its href attribute creates a hyperlink to web pages files email addresses locations on the same page where anything else a url can address all right so an anchor tag or link tag is going to require this single attribute called the href attribute now with html tags most of them do not have required attributes but with a couple of them there are some required attributes this happens to be one of them so if we were to come into our html as we have it here and maybe we wanted to make a link around some of these text so what i'm going to do is make an a tag surrounded by this first container one text and right now you'll see that nothing is happening over here when i click it there's nothing there but once i put in that href attribute and i put in a link so we'll say www.google.com now you'll see that we have this link styling and if we click this it should take us to the appropriate link now one weird thing about codepen since it's kind of like an integrated environment that we can write html css and javascript when you have link tags within a code pen you can't click on them and actually go outside of codepen so i've actually pulled up let's let's actually look at the local file that i have so this is what i was showing you how to navigate around the different elements and if we go down to where i have the link example right here so let's go ahead and click this button up here to select a specific element and then i can click on that and it takes me there on the right side and you can see that i have this a or anchor tag with the href attribute right here representing my my personal website all right so when i click this it's going to take me directly to my website and that's how an anchor element is going to work now there's one additional thing that we can add to an anchor element to affect the behavior of the link so if we were to go in here and we can actually edit html directly within the dev tools and if we put a attribute called target and then we give it an underscore blank value so if we were to go to the documentation to see this you'll see that there's a couple attributes available here href being one of them but if you go down you can see there's a target attribute and it gives you the possible values that you can put in there and it says where to display the linked url as a name for a browsing context so basically it's saying where do i need to open this link and if we go to blank it says usually a new tab and that's exactly what's going to happen once i put in this target equals blank and i click this link it's opening a brand new tab rather than taking me to that link within the current tab like i said there's other attributes on an anchor element but those are probably the most common two ones that you'll need and the href attribute is actually required to make it function as a link at all the next one we're covering is the input element and this one is loaded with functionality especially when you start putting it within a form element alright so we're not going to talk a whole lot about this because this comes later in this series and we're going to talk a lot about it in the context of a front-end um web framework like react or angular vue.js but i just want to introduce it to you so we'll go to input tag we got to go back on the documentation it actually has an entire section just for input types and like i said there's a lot going on here there's a lot to do with forms within the web so let's open up the normal html reference and go down and find the input all right so we'll click input and it says it's used to create interactive controls for web-based forms in order to accept data from a user and that's pretty much where we're going to stop because that's all that we need to know for now i'm just going to show you a couple basic attributes that you can use with this and just give you the general concept of what it is so let's go to the page we're working with here and let me just clear out everything and put an input tag now this is going to be a self closing tag we talked about that earlier just like an image tag is and within this input you'll see that we already have something showing up here on the right that we can type some text into so right off the bat it's usable but if we just left it like this and we tried to hook up some javascript to it it'd be very difficult to select it and grab the data that the user's typing in from it so something that we'll often do is give an input a name and this is very important when you put this within a form so it can identify which input it needs to grab data from so we'll just say this will be input 1 and then another thing that you can put another attribute you can put is going to be a placeholder and this placeholder is going to be whatever shows up in kind of a grayed out text to indicate to the user what they need to type so we can say enter email address here and you'll see that this placeholder gets put in there and then once you start typing it goes away so that's a very common element or attribute that you can use for the input tag now you can also put a predefined value in there so instead of a placeholder if we were to put value it's going to replace that placeholder with an actual value so you can see how there's text already in here and if you wanted to change it you'd have to delete it and then type some new text so those are some of the common attributes that you can use for inputs and again i'm not going to cover inputs in very much detail at all right now because we're going to talk about them a lot later in the series next up we're going to be talking about image tags and we've already looked at it a little bit so let's go to our html elements reference and then look for the image tag there it is all right so the image element embeds an image into the document all right and there's going to be a couple common attributes that you'll use for this and there's one that is required so the source attribute is required as you can see in the documentation and then the alt attribute is highly recommended so when we're talking about web accessibility and we're talking about you know how do we know what this image represents if it doesn't render correctly that's going to be what the alt attribute does so i'll show you exactly what what i'm talking about here let's write our image tag which is self closing and then from here we're not going to see anything because we don't have our required source attribute now let's go back to this dog photo we'll put in the source attribute and paste in the link to that photo and it's of course going to be very large here because the image is much larger than our page here but we can also add in an additional attribute and a quick tip with html you can actually enter these down so it doesn't have to you know all be on one line we can separate out the different attributes from the tags so we'll go with source first and then we'll put in another common one which will be with so you can also specify this within css but this would be a quick way to make the width of this image 300 pixels all right so that gives us a more manageable image to work with and then finally we have the alt attribute so we'll say a dog image or something like that to describe what our image is all about and the reason we use this is because if our link is broken so if i just break the link real quick you're going to see when it reloads that it's going to give this little image icon and then whatever we put in the alt attribute it'll say a dog image so if the link ever breaks the user will know like oh this is supposed to be a dog image and i kind of know what the context of this image is and then finally one of my favorite content elements is going to be button alright so this is what we get to click and do fun things with so how we write this is you we just basically write out button it's a normal you know opening and closing tag type of element let's take a look in the documentation real quick go to html elements and we'll find the button element it says it represents clickable button used to submit forms or anywhere in a document for accessible standard button functionality alright so we click buttons all the time when we use web apps and it basically brings our web apps to life it allows the user to know like okay i got to click this to do something so within our button we can give it some text so we might say click me in all caps and you'll see that now we have a button that we can click now it's not going to do anything right off the bat because we haven't hooked up any event listeners or stuff like that to it now there's really not any common attributes that you'll see on a button like this but there is one common thing that you'll have to have for a button for it to really do anything and that is a event listener now we're not quite ready to talk about events within html but i do want to quickly show you what i'm talking about with buttons so if i wanted to do something every time someone clicked on this button i have to assign it an on click event now this is built in this is actually not an attribute this is an event listener so in here it expects a specific function to be called every time we click this button all right so if i go down to the javascript section of this code pen and i define a basic function that says alert me alright so something like this where every time we click the button it's going to bring up an alert in the browser and it's going to say hi alright so very simple and now if we put that into the button right here and we click the button it's going to make an alert pop up and say hi every time we click it all right so that's just the basics of event handling within html and we're going to talk about that in a little bit more detail when we get into the concept of the document object model or the dom the next element i want to talk about is another content element and it's a list so with a list we can basically have bullet points or we can have numbered list all right so if we wanted to have a bullet point list we're going to use some tag called ul alright so what ul stands for is unordered list and if we go to the documentation here we should be able to see that somewhere so let's go down to html elements and find ul all right so there is the ul and it's going to say this represents an unordered list of items typically rendered as a bulleted list all right so that's basically what that stands for and within it we have to put some li tags so you can see if we click li this element is used to represent an item in a list all right so we can put that here so we'll say li and we'll say item one and then we can copy this down a couple of times and give it item two and three and now you can see on the right side we have a bulleted list of content all right and if we wanted to change this to a numbered list all we have to do is change the ul kind of container to an ol container you can see it changed here on the bottom automatically and now you can see that it is numbered and if we go to the documentation we should be able to click on ol to see that this represents an ordered list of items all right so those are the two types of lists and these will be very helpful to us when we're laying out content and often times you'll see these lists corresponding to a loop so if we talked about in javascript earlier how loops work and we can take an array of elements and we can loop through them well it makes sense that we could basically loop through an array of elements and display them in a list like this so just keep that in mind as we move forward in the series and then the last thing i want to cover is tables and i know i said at the beginning we're not going to be talking about this all that often but i want to just glance over it very quickly and i recommend that you look at this on your own if you want more details about it so if we go to the documentation once again we go down to html elements and table alright so we'll click table and it says represents tab tabular data and then it shows you the basic structure of a table you can see that we have the table as the main container and then we have the t head tag will be the header tags and then the t body will be whatever data we're putting within our table within that body we have tr tags which are going to be the rows and then within the tr tags we have table data for which we'll put td tags in there all right so as you can see it's a pretty complex structure and like i said i recommend just using a table generator you can just search for it on google it's much easier than trying to remember this structure now of course you can look it up and just copy in this example and get started that way too anyways let's move on to the next part of our lesson and that's going to be talking about html attributes in a little bit more detail in this next part of the html crash course we're going to be talking about the html attributes now i know it's probably been a little bit confusing so far we've gone through a bunch of different html tags that we can be using we briefly covered attributes and how they work but we didn't really talk about when do you use them how do you know when to use them you know what's fair game here so we're going to start by talking about the difference between a global attribute and an element specific attribute in short a global attribute is something that's available to any html element you know no matter what it is an element specific attribute is an attribute that only does something to elements that it applies to so a good example of this can be the image tag so let's go ahead and look at that on the screen real quick so if we make an image tag and we give it a source property and then we give it a clasp or not property but attribute you can see the difference between a global and an element specific one so again let's go ahead and put the source in there and then let's make the width equal to 300 like we did earlier and what you can see on this image tag and then the total html element that we call an image you can see that we have the source attribute which is specific to this image tag we also have the width attribute which is also specific to the image tag now the class attribute is a global attribute which means it can be used on any element or any tag and to show that let's go ahead and make a paragraph tag below we'll put some text in there and then we'll give it a class all right so we've given it a class here and you can see that it's not doing anything right now but if we were to actually style these two different elements we would use these class values as a way to select them within our css so as i kind of shown you showed you earlier you can actually select these different classes and basically put some styles on here so what if i wanted to put a border on this image we can do it like that we can make that border a little bit larger and you can see how we can style that or we can come down and select the other one so some class value and we can give this text a different color so maybe we'll call it green and so now we can see that the text is green but this is not a css tutorial i just wanted to show you that that is a global attribute that we can use to connect up our html and our css but if i came down here and put something like a width on the p element and i gave it maybe 20 px or something like that it's not going to actually do anything because this is not applicable to the paragraph tag and likewise if we put a source on here and then we copied in the image url it's not going to do anything either because this is a paragraph tag it doesn't have a source attribute so it will still render in the browser okay it just won't do anything and that's the difference between a global and an element specific attribute if you wanted to connect the dots with the previous lessons we've gone through you can think of these element specific attributes just like we have javascript methods so if you remember if we have a string like this there are a certain number of methods that are available to this string data type in certain methods that are not so for example if we wanted to call a method like string dot 2 upper case all right something like that that's going to work just fine and i'm actually going to come over to a console that we can use to show this so we got a string all right right here and then if we say string to uppercase it's going to work just fine it's going to make all the letters uppercase but if we said string.reverse it's going to say type error because reverse is a method that's only available to an array data type so we would need an array like this and then use array.reverse to actually use that method and the same thing applies to html attributes there are just certain attributes that only apply to certain tag types all right at this point we have covered quite a lot about html and i want to have you actually go through a practice exercise and a challenge to reinforce some of these concepts so that when we talk about some how we actually connect javascript to the html you're not still trying to figure out like how does html work so i'm going to give you this challenge if we go to my code pen let's see if we can open this up here all right so as you can see um this is the challenge that we're going to be working with and what i've done here is i've put a couple different html elements in your goal is to basically recreate this but put in your own personal details of course and something that you can share maybe on twitter use the 100 days of code maybe hit me up zg underscore dev but anyways we're going to be building something like this but personalized and i've given you a starter template so this will be linked to in the description below so be sure to check that out and you'll just open this starter challenge up and you'll see that within the html i've kind of laid out a basic template for you and it says your code goes here now within codepen it actually this html part as i talked about a little bit earlier represents the body part so really all we have to do is just start typing right here so i'll leave you with that and you can just start typing some html so you can put some text in there and you're gonna see that it automatically shows up on the screen and furthermore it's going to be styled just like the one that i showed you because i've already written some css styles that will basically apply to your html you know no matter how you use it so i recommend that you just browse through these styles but don't worry about how they work just focus on writing the html that is necessary to create something that looks like this so just a few hints as we go through this looks like some sort of heading we've got some paragraphs in here we've got a link we've got a list and then we've got an image down here so that's basically all you need to do so go ahead and try that challenge out and be sure to share your results on twitter i think this is a rather basic challenge not saying it's going to be easy it will not be easy starting out but it's basic enough i'm not going to go through it here on video but if you'd like to go down in the description and i also have linked to this code pen where i've solved it so you can see the code that i wrote and how i wrote it to compare against yourself [Music] the next part of this video we are talking about something called the dom and that stands for document object model and it's very closely related to html which we talked about in the previous part of this lesson now i will say if you have not completed the challenge that i gave in the prior video go ahead and complete that and i would recommend that you have at least a basic understanding of html and kind of understand how to put something together within you know a code pen document or something like that because if you don't have at least a basic level of understanding of html this stuff is not going to make any sense before we get into this i want to show you a little bit about the codepen environment so that we can understand like how do we actually debug javascript within the browser and you know take those concepts we've been looking at in the prior videos where we've you know pretty much right clicked so let's take a look at the screen here if we right click and inspect and then we come to the console then we can write you know basic javascript within the console so we've been working in the console quite a bit but when we go to codepen since it's kind of it's in its own environment we have to do a few things to make sure that that console is working the same so if we come down to the js section of the code pen this is a little bit different than what we've been working with in the console now if we were to just type something like you know console.log hello now this is going to be printed to the console but it's going to do it over and over again every time the page loads because this javascript that we're working with here is directly connected to the html document above so it's not really like a freeform console that we can use to test out different javascript now codepen does have a console down here at the bottom left and if you open it up you'll see that we have that hello that we printed by writing that javascript code but we can also do something like that down here in the console and this is supposed to represent the same type of thing that we've been working with um when we right click and inspect and go to the dev tools console but the problem is this is not a really nice and easy console to work with like if you started to you know define a variable with that's like a string or something and then you type a and you hit enter it's not going to say it's not going to like be defined and that's a problem because we want to be able to experiment around and work with our javascript as we're building it so to solve that problem we're not going to be using the codepen console what we're going to do is right click and inspect within the codepen document so it still opens up this console and i will note i'm in google chrome not firefox this does not work quite as well in firefox unfortunately so i've switched to chrome for that reason and if we go to the console you'll see some of the the hellos that we put there from earlier but if you go to this little drop down here when we first enter the document you might see something like top but in order to get the right context to work in you have to drop this down and scroll down to codepen now we can actually select elements from the codepen document that we've written here and we don't have to um or we're basically going to have some responsiveness with our javascript just to quickly show you this and we're going to cover all of this stuff in a little bit but let me put a basic paragraph tag in our codepen document and you can see it showing up there and if we have this context the javascript context set to top and we try to type document.queryselector then grab all the paragraph tags it's not going to grab us the right ones so this is a paragraph tag but it's not the one that we're looking for here so we have to go down to the right context so select codepen and now when we run this it should give us the correct element that we're looking for so that's just really important if you're trying to follow along with this like code along as i'm doing it just make sure that you're in google chrome you right-click inspect element and make sure that the context is the right one it's under the codepen context alright now we got that covered just want to make sure that you're able to follow along let's jump into what the document object model or the dom is all about now how i would describe the dom is a bridge between html and javascript prior to these lessons we've been going through how to write javascript code we've gone through all the basics of it so you have at least a basic level of understanding of how to write javascript we also just learned about the basics of html which as you saw is a lot easier to learn than javascript now what we're going to do is we're going to take these two concepts and we're going to combine them together to make our html come alive so remember with the house analogy html is the structure css is the styling and javascript is the function so in other words with the house analogy html is the bricks and the concrete in the wood the css is the paint on the house and the javascript is going to be the plumbing and the electric and all that kind of stuff now the first question you're going to ask is why are we looking at the dom in the first place how do we know that this is important well the dom is basically at the root of any front end framework that you're ever going to work in it's the only way that we can connect our javascript to html and while you may not see it directly it may kind of you know be doing a bunch of stuff behind the scenes when you're working with a framework like angular reactor vue.js it's still there it's still using the dom api we're going to talk about that in a second so it's important to know that it exists and know the basics of how it works and what it's trying to accomplish so what is the dom now we know it's the document object model which gives us a little bit of a clue what we're talking about here but it's a little bit more complex than just that now i'll start with what it is not so the dom is not a programming language it's also not natively part of javascript so it only exists within the browser and there's different browser implementations whether you're on firefox or chrome or safari they all have slightly different implementations of the dom for our intents and purposes we don't really care about those differences we just want to know you know what it is and how it works so the best way to think about the dom is an api for an html document now you may not know what an api is but luckily i have done a video on just that so go ahead and watch that if you're unclear of what an api is but just as a you know 10 000 foot view what i consider an api and this is my personal definition of it that that makes sense in my head is a software api can be thought of as a user manual that allows developers to do something useful with a third-party software or hardware without knowing the inner workings of that software or hardware so basically the api that we call the dom is going to allow us to manipulate html without knowing you know how is html actually you know manipulated within the browser all we have to know is how to write the html and then how to use the api that we call the dom to manipulate it i know your head's probably spinning at this point so let's jump into a simple example to understand what i'm talking about so what i'm going to do here is paste in some html all right so this is going to be a basic html document i've got an h2 tag a paragraph tag and a button now if we click this button and we kind of talked about this a little bit earlier when we click this button it's not going to do anything and the reason is we don't have any javascript connected up to it in order to connect it up all we have to do is first identify the element that we're wanting to make interactive and then once we identify it we need to instruct it to do something via javascript so just as an example we can identify this button using pure javascript and all we have to do is type document.queryselector and then we type in button because that's the tag that we're looking for now of course i need to select the correct context to work in so we want the codepen context and then from here when we press enter we're going to see the button that is within our codepen output all right so that's how we would select that button now from there we can actually store that within a variable so if we were to rewrite that command and then just store it in a basic variable so btn is our variable now we can reuse it and we can do things with it but at this point you probably don't have any clue what all these commands are like what is document here what's query selector all that kind of stuff doesn't make any sense yet because we haven't talked about the dom so to better understand it i want to bring in yet another analogy for us now when we think about an html document just a static html document that we've been working with so far so this one right here on the screen that is kind of the equivalent to a paper map so with a paper map there's only so many things you can do with it you can write on it of course but you're not going to be able to you know click into it and see some more details about it but we can do that with a something like google maps we can click in and go to different places and do a bunch of other uh functions with it so the analogy here is that html alone is like a paper map but when we add the dom api with that html then it becomes something like google maps where we have a lot of interactivity that we can put into it so to really talk about the document object model let's take a look from 10 000 feet all right so we know some things about javascript that are pretty familiar to us so for example this is a string in javascript we know that all right and we also know that this is going to be a number all right but what about that button that we just defined here so we saved it into the button variable so if we look at that variable what type of data is that well we don't actually have a good indication of that all we have to do is say type of button and it's going to say object well clearly there's got to be something more specific than just object and there is when we start using the dom or document object model terminology from the dom's perspective this element right here or this variable right here is considered an element so if we were to come to the document object model documentation here you're going to see all sorts of interfaces and stuff it's it's a pretty complex api that we're not going to even cover half of but if we click in and look at the element this is the most general base class from which all element objects in a document inherit all right so that would be what we are looking at when we have saved that selected element into a variable called btn and once we know that this is of this element type we can come here in the documentation we can see there's all sorts of properties that we can look at and there's all sorts of methods that we can use on this specific variable so we have a lot of functionality available to us but we have to first understand you know the the whole 10 000 foot view so what i'm going to do in order for us to understand it better is show you a picture that's going to represent what the dom is really trying to do so at its highest level the dom has two built-in objects that are probably going to be used more than any other objects that you look at so at the top level the very top level you have the window object and what you can think of that as is literally the browser window that we're working in more specifically it's kind of referring to the individual tab browser tab that we're working in but in some cases it can cross over between tabs so you can actually program using that one object you can do all sorts of things within a browser so as a general user of a web browser there are several things that you do every day that you don't realize are programmable so one of those things is scrolling down all right scrolling down or scrolling up every time you scroll you can actually do that programmatically with javascript now another thing you can do is you can resize the browser window so if you were to resize it like this you can actually do that with javascript alright and finally if you refresh the page that's also something you can do with javascript in the way that you do it is by connecting into the dom via that top level object that we call window all right so i'm going to quickly show you how we can programmatically do these things by using that object but first let's take a look at this in the mdn documentation so we'll search for window and here's what we're looking for and it says the window interface represents a window containing a dom document document property points to the dom document loaded in that window so basically it's saying that the windows the browser itself or the tab and then there's an html document represented by the document object within it all right so that picture that i just showed you kind of uh shows that relationship how we have a html document that we are putting in that browser window now from here this window object like i said has all sorts of properties and methods that we can use to do things that we commonly do just by clicking around in the browser on a normal basis so you can see down this left hand side these are all of the different you know things we can do with it so for example we have this alert method so let's go ahead and inspect element so we're in the browser console and if we type window dot alert and then we say i love programming or something like that and press enter it's going to open up an alert window and you can see how we're already starting to program this browser window using that object here's another thing we can do we can open up new tabs so if we say window.open and maybe go to google alright when we press enter it's going to open a new tab and put us at google.com so that's pretty cool let's see another thing that we can do is we can scroll so you have to obviously be on a page that has a scroll bar which this one does and from here we just say window scroll by and then we give it a bunch of options here these are all documented within this method so if you were to um go down to the methods of window let's see if we can find them all right so we found the methods here and let's see if we can find this scroll by method all right so we have scroll by we can click on that and it just tells us okay you can pass in some options here and those options that we're passing in are scroll to options all right and then from here we have the top left and behavior so we're going to define we want to scroll from the top we want to scroll window dot inner height which is going to be a built-in property of that window object which basically says here's how tall this entire html document is all right so from the top we want to scroll down the entire length of the page essentially we're going to the bottom of this page all right and then from the left we want zero we don't want to scroll left and right and the behavior that we want is going to be smooth we don't want to just jolt down the page we want to smoothly scroll all right so when we do this and close out this method and press enter you got to watch the screen now it's going to scroll all the way down to the bottom of this you know page or whatever and i guess it didn't go all the way down because i actually explained that wrong so window.inner height if we look at that inner height is only going to define how large the visible window is so it's not going to define how large the entire document is so obviously this is a much larger document so it's only going to scroll down one entire page and then finally we are able to actually reload the page so we can tap into the window.location property and then that location property has a method called reload and you could have found this in the documentation if you looked hard enough but i don't want to right now just for sake of time so let's press enter and you can see you may not have seen it but it reloaded the entire page programmatically so you can see how that top level window object is basically our control over the browser and the way that this is happening is via the dom api so the dom again is just an api to connect in with our browser in html document now another thing to mention about this is it's a global object which means we don't have to actually specify it every time we use it so i showed you before that we could do window.alert and then type in something to get an alert all right so we can also do this by dropping window like we don't even need that because it's global and we can go ahead and get the same results there alright so that's important to keep in mind with the window object and the document object these are both global so we don't need to specify them before calling their methods or properties so a couple common properties on window we're not going to go through all of the methods and properties for all these objects but this is just such a high level one an important one that i want to so window has a few common properties that you might want to tap into one of those is going to be the inner height and i spelled that wrong so it's not going to give me anything so we've got inner height is going to give us the height in pixels of the space that displays the html so the visible area that's displaying that html now if we said outer height that's going to give us the pixel height of the entire browser window so if you look at this value it's 1040 but if you make this browser window smaller and you say outer height it's going to give us a different value 894 the same thing applies for inner width and outer width so inner width is going to give you the width of this space that's displaying the html so if you resized this you're going to get a different inner width all right so i don't want to spend too much time on the properties but you can kind of get the gist of what that's showing you now another common property that we might look at here with window is going to be local storage all right so this is going to be a big one that will come up as we start getting into more advanced web development concepts and basically what this allows you to do is store data temporarily in the browser so if i were to go to the application here in chrome it's called a little bit different thing in firefox i actually talked about that in like the third lesson of the series but if you click here you can actually see key value data pairs that can be set and retrieved through the local storage object so if we wanted to set an item we'll just say test and then we want to set it equal to 20. we can do that and then we come to application and you can see test has a value of 20 here so it's a great way to store temporary values and that is accessed through the local storage object that sits on the window object all right and then finally i want to talk about the location because this is going to basically represent an object that has details about where you're visiting right now so right now we're on the mdn web docs and if we were to look at the window.location you're going to get all sorts of details about that location we have in the address bar so these are some of the common properties just one more two more that i want to show you here before we move on window has a scroll x property which is going to indicate how much we have scrolled from the left side of the page or i guess right and left horizontal scrolling and then we have window dot scroll y which is going to indicate how much we've scrolled up and down so you see the scroll bar here this is going to change the value of this property every time we scroll a little bit so these are all going to come in handy as we work with the dom later on and we want to you know programmatically do things within this browser window now i've briefly mentioned it before but we can have these things called events happening within the dom now this is going to be a little bit complicated but i want to just show you a basic example of this something that you can do on the top level window object so we can say window on scroll so basically we're saying we want to tap into the on scroll event which basically every time i move that browser scroll bar it's going to fire this event and what i want to do every time i scroll i want to execute a function so right here i'm using an anonymous function and i'm just going to console.log hi alright so something very simple and now we have set an event listener on this the entire window all right so now when i touch the scroll bar we're going to get this printing hi to the console for a lot of different iterations here so that's just a basic event now i just want you to know that that is available and it's something that you can do but we're not going to really dig into the the super nitty gritty details of it in this lesson all right so now that we've talked about the window object which represents the entire you know tab that we have open or i guess the browser that we're working in let's talk about the document object so let's put that back up on the screen that 10 000 foot view of the dom api we've got at the top we've got the window object and then a property of the window object is going to be document and this is where the real fun begins because document is basically a representation of your entire html document that you've written so let's take a look at this i'm going to refresh the browser so we don't have that on scroll event firing every time but we have window at the top and then we have this property called document all right so we press enter on this and you now see that we have this big object that has basically the html document defined within it and while we can expand these different things visually within the console there's actually a lot of things that we can do programmatically using this document object we can add or remove html elements from the existing page we can modify existing html elements and we can even set events on specific html elements we saw how we set an event on the entire window but we could set an event like we talked about earlier in the prior video something like that where we have a button and we want to wait for a click event and we want to fire some sort of function that does something every time that button is clicked so this document object is very powerful and there's a lot of things that we have to cover to understand what it actually does so let me show you a basic example of this we'll go back over to our code pen which we have a basic html document here and i'm going to type document and let's take a look at what we're dealing with so right here we have this top level html wrapper which we talked about is going to happen for every single html document in codepen you know you'll notice over here we don't have that written but codepen is automatically wrapping our html within the html wrapper all right so we expand this down we have a head code pen takes care of this and a body and then from here this is where the actual html and javascript that we're writing is going to be placed so you'll see that we have our h2 element our paragraph element and our button element and then you also see some scripts down here at the bottom and basically these scripts are what are represented here in the javascript so any javascript we type here is connected up to our html document via these script tags so again don't have to understand how that all works we'll get into that in future lessons but i just wanted to show you the anatomy of this document object now if you've been following this series all the way through and you went through the javascript series you know how to write basic javascript and you also know that if we have a javascript object we can access properties of it so let's define a basic object with a property all right something like prop 1 and prop 2. nothing fancy here but if we were to actually let's make a nested object as well we'll say nested right here and then we will end this alright so object.prop2.nested is going to equal our value right here because we use the dot notation to access the different embedded properties and values within this object now you would think that we could do the same thing with the document object but we can't all right so if we try to do the same sort of access let's clear the screen and if we try document and then let's go ahead and look at it one one quick second so we obviously have document now we have html then we have body so if we typed document dot html dot body it's going to say we cannot read the property of body because it's undefined so we clearly cannot just access these different html elements with dot notation we have to use um the document object model api to do so and it's a little bit different it takes a little bit of getting used to but hopefully we can walk through some of the more common ways to use it here in the remainder of this video so i'm going to put on a new html document here with some additional parts to it so that we can really visualize what this dom is all about all right so we can think of the document object model as a tree and furthermore we can think of this tree as kind of like we think about ancestry trees or whatever where you have grandparents and then you have parents and you have children and stuff like that so what you're seeing on the screen right now is this html document that we just created in this code pen represented as the dom tree all right so you can see that we have the document at the top then we go down to html and then from there we have the head in the body and underneath the body specifically we have all of this html that we have written here and within that html we have a lot of siblings but we also have some children so for example let's look at this main div right here for the main body so the main body div is going to be the parent to this h1 tag these two p tags and this ul or unordered list tag all right so that's going to be the parent to these four now if we look at the list items its parent is going to be the unordered list and its grandparent is going to be the main body div so you can see how these relationships start to build and furthermore we have you know the concept of siblings so all of these list items these are all going to be siblings within the document object model and this unordered list the p tags and the h1 they're all siblings so what do we call all of these elements well in the document object model we call them all nodes now this is very different you should not confuse this with node.js we'll talk about that later in the series but that's basically the back end server side version of writing javascript this is a totally different concept all of these elements are considered nodes within the dom tree now of course they're all different so they're all different types of nodes and i'm going to put the image back on the screen that we looked at in the prior part of this lesson when we talked about the basics of html so within this element that we have defined it's a total unit we have different types of nodes here so at the simplest level we have something called an element node and that's going to be represented by the p tag all right and then we have an attribute on that uh html element and we call that an attribute node and then finally we have a text node which is going to be the content that we're putting between the starting and closing paragraph tags now to prove this out to you i'm going to actually take this element and create it in javascript and then insert it into the page that we're looking at right here all right so the first thing we have to do is define each of those three different types of nodes all right so the first one is going to be an element node and let me let me actually zoom out a little bit here so we've got an element node and we have to use the document object and then we use create element and before we do that i keep forgetting we have to change the context to codepen so that we're working with the document that we're seeing over here rather than the whole document all right so sorry about that and what we're going to do is create our element node which is going to be a p tag all right so we're just creating that element we just put on the screen so let's press enter and we have created our first element node and if we look at that it's just going to be an mtp tag there's nothing in it yet all right so now we have to create our text node alright so we're going to type document dot create text node and then here we're going to put in the word content which is the stuff that was between the p tags alright so we press enter and right now this element node is still empty all right so we still have an empty p tag and the reason is we haven't assigned the text node to that element yet before we do that let's create our last node which is going to be an attribute attribute node so we'll create an attribute and we want to call this a class attribute because that's the type of attribute we had on this html element all right so at this point let's look at what we have here so we have element node which is a mtp tag then we have the text node which is going to be content as a string or more specifically a dom string and then finally we have our attribute node which is going to be class equals to empty so basically we haven't assigned a value to this attribute yet but we've initialized it the first thing i want to do is i want to take our text node and i want to put it inside our p tag or element node so all we have to do is say element node dot append child and then we pass in the text node press enter and now when we print out the element node it's going to be a p tag with that content placed between it now we are starting to build out this element the next thing that we have to do is give a value to our attribute so if you saw before we have class equal to an empty string all we have to do to do that is we say attribute node and then we say dot value equals sum class all right so now we have this attribute node if we print that it's going to say class equals sum class so we're getting closer the only thing that we are missing is this attribute is not connected up with our paragraph element so to do that all we have to do is take our element node and we say set attribute and then we pass in the attribute node and it says it failed to execute because there are two oh okay i messed it up so not set attribute we have to set attribute node all right so we press enter there and now the element node is going to be our full html element that we saw in the model before the last thing we have to do is insert it into the document and the way that we do that is we use the document object so we type document and then we use the append child method we haven't talked about this yet but i just want to show how it works and now we just pass in element node and if you look at the bottom of our code pen you're going to see this element inserted into the document and it's going to say it failed to execute because i messed up once again i forgot i need to add one more piece of code in here i need to say document.body so instead of just inserting it at the end of the document which represents the whole html document i need to specify that i want to look at the body specifically so if we go over to elements and we look at the entire well i guess this is not a good example because codepen is adding a bunch of other stuff but basically we need to put in the body element so let's press enter and you can now see that our content has been inserted into our existing html document so you may not have recognized it but we're actually tapping into the dom's api to do everything we just did we created a new html element with javascript alone and you can see that we use things like set set attribute node and append child and we've used create attribute create text node create element these are all methods that are defined within the document object model all right so we are actually using the dom as we speak here now you might ask what's the point of this every time we refresh the page so if we refresh this page reload it we're not going to have those same elements inserted into our document anymore and i'm not sure what i'm doing here okay so html i actually need to copy in some stuff here it's because i didn't save the actual code pen but whatever the case that content at the bottom is going to be temporary and you might ask well what's the point of inserting stuff with javascript and the answer to that is for user experience so when a user creates a calendar event on google calendar you'll see a calendar event pop up immediately on that calendar and that was done via javascript you know tapping into the dom all right another example is a user adds a to do item to their to-do app or a user creates a post on facebook all of these things are using javascript via the dom so the dom's api to insert elements and modify elements that are existing within the html document but they're only temporary and it's because we want a better user experience so we want to immediately change that you know web page and then later we kind of save that to the database and reload it with new data later all right so at this point we have gone through a lot of different angles to think about the dom with but we haven't actually you know buckled down and tried to understand you know how do we actually use the dom here what are some of the methods available to us um what exactly is the dom like you know we've been working with the window object in the document object but there's so much more to it we can also work with individual elements and with each element we can do certain things with that just like we talked about with html and javascript before the purpose of this series is not to make you an expert in any one thing it's to take you from xero to launching your first full stack application and deploying it so we've got to cover a lot of ground so i'm going to walk you through an example of using the dom we're going to cover some of the common uh methods and you know properties that we can use within the dom but we're not going to cover everything and quite frankly most developers don't know everything about the dom it's very complicated and there's a lot of stuff that you will never end up using within your career anyways let's get started let's go ahead and put some new html in our code pen all right and this one says welcome to my html tutorial click the button below to begin and this button what we want to do with this is we want every time we click it we want to add a new paragraph element to the bottom of this page so how do we do that well it's three steps number one we have to find a way to identify the button element so that we can start working with it number two we have to register a click event on this button which is part of the dom it's part of the dom api and then number three we need to write a function that will insert a new paragraph element in the web page every time we click it so let's take a look at our button here we have a global attribute called id every html element is able to have this id attribute and we also have a global attribute called class every html element is eligible to have a class on it so we have multiple ways to identify this button so let me take you through a couple of those ways to better explain some of the methods we have on the dom here's the first way we can do it and this is probably the most generic we can say document.queryselector and then we can just type in button because this is going to be button is going to represent the tag that we're looking for so what this is going to do right now is it's going to look for any button on the entire page here including codepen's you know interface and the reason is we don't have the right context so let's go down to codepen again and try this again so we're going to select button and what this is going to do is it's going to look through the html document that we're working with and it's going to find the first occurrence of a button tag and if it doesn't find it it's just going to return null now the question is how did i know to use this document.queryselector all right the reason i knew that is because if you go to the documentation you go to apis dom then you go down to dom and you find that document object so this is that document object that we talked about it represents the html document itself this has certain properties and it has certain methods that are available for us to use so if we scroll down all the way to the methods you will see a couple of different query methods so we have query command enabled you won't use that much a couple others but you can see query selector it's actually listed twice i'm not sure if that's meant to do not meant to be that way i don't think it is actually but we can go to query selector and this will tell us basically how to use it so we can put in selectors it says a dom string containing one or more selectors to match the string must be valid css selector string so we don't know a whole lot about css quite yet but basically what that means is if we pass in to this query selector if we pass in something with a hashtag at the beginning that represents an id so if you remember we've got this id equal to btn-1 so if we put the hashtag and type bt btn-1 we should be able to find this element again of course we got to go back to the codepen context it's very annoying that i have to do that but i promise it'll get a little bit easier in the future so we'll go btn-1 and we can find that element that way now we can also use a class selector which is a period right so you can see our class is my-btn so if we do period my-btn we should be able to find the same button that way as well and these are just more specific ways to find a single element within the dom now of course as with anything in programming there's lots of ways to solve the same problem and if we go back to this documentation let's go back to the main document object and if we look at some of the methods here we've got these query selectors which are kind of generic query selectors that i basically it's all i use to find elements within the html document but you can also get an element by id all right so that's another way to locate an element so if we were to replace this so we say get element by id and then this time all we have to do is pass in the id you don't even need a hashtag at the end oh i messed this up here all right let's try that again so get element by id and button one and we should be able to find that button once again by using a different method so as you can see there's many ways to skin a cat here and with the document object model you can get very creative with your code now the reason that selecting it by an id is the most specific way to do it and probably the best way to do this is because you cannot have two elements with the same id so if i tried to copy this button down here and it's got the same exact id as the other button it's going to have a conflict so this is not really valid html it'll still render but it will cause you problems if you're trying to use any sort of front-end framework or even locate things with the dom so what i'm going to do here is paste in all of the different ways we can identify this one button let me get rid of the button here the second one and then it's going to kind of refresh put me back in the wrong context so i'm going to go back and now if i paste in all of these different methods so you can see there's quite a lot of different ways to identify this button but these are all valid ways to do it and this only executed it looks like one of them but if you were to type each of these in individually it's going to locate it each time but obviously there are some that are more specific than others and we always prefer to be as specific as possible when identifying elements within the dom all right so now that we know how to identify this element let's go ahead and write that in our code so so far we haven't used this javascript section quite yet but what i'm going to do is i'm going to say add paragraph and we're going to make a function that's going to be actually called every time we click that button so from here all we have to do is locate the button and assign it to a variable so we're going to use one of those methods i just will choose the query selector and we're going to pass in the id of that button in order to identify it alright so this button variable should represent our button and from here we should be able to do something with it every time it is clicked and i'm actually going to not put this within the function this will just be in the global scope of our javascript document here i'm going to close out this console so that we have some more room to write just for a second all right so now we have this button being identified and then we have a function that's going to add a paragraph all right and the last thing that we have to do is we have to add an event listener so this is how we do that we just say add event listener and then we specify the event that we're trying to listen to and then the function or the callback that we're going to pass in to execute on that event happening now this is the first way to register an event on a specific html element this is one way to do it another way to do it is if we take our element button and then we say on click and then we set it equal to add paragraph then that's going to also register that event on the button and then a third way we can do it is in the html itself and this is what we looked at previously in this tutorial if we come here to the button and we say on click and then we set that equal to add paragraph and then we actually execute it within those parentheses then that's going to register that click event as well so there's three different ways that we can register a click event but in this case i want to do it directly with javascript so we'll go ahead and go with this one this is a pretty simple one but you might be asking like how do we know that that's a event that's valid and the reason we know that is if we go to the event reference within the mdn web docs this is going to give us all of the different events that we have for elements on the web all right so not all of these are going to work with like say a button but a lot of these will just work for various components within a web page so here we have mouse events and you can see the click event and this is going to tell us that the event handler property is the on click which is what we put in right here so you'll get used to on you know you'll basically pick up on all of the common events with due time there's only a few of them that are used on a frequent basis and the on click method is actually one of the most common events that you'll use in web programming so at this point we have our event wired up and this add paragraph function is going to fire every time we click the button so let's say the button is working all right and if we go ahead and open back up that console so right click inspect go to the console and we're in the correct context it looks like and we can just say or click the button and it's going to say the button is working and it's going to do it over and over again every time we click it so the goal here is to insert a new paragraph element every time we click that button right now it's just console logging some a string to the console but we can also construct a new element just like we did before and this time around i'm going to show you a few shortcuts that's going to make it a little bit easier to create this paragraph element now we want to have some random data to be printed to each new paragraph element so i'll just generate a random number we'll say randomnum is going to be equal to math.floor this is a trick that we learned in a previous lesson and we'll just do between 0 and 100. something like that all right and then we're going to say the p content this is what we want to actually add within that paragraph element we're going to combine this with the random number we're just going to say the random number is and then we're going to add via string concatenation that random number all right so now the p content is the random string and let's go ahead and just test that out real quick by printing our random string to the console every time it is clicked alright so we will click me and it's going to say the random number is eight all right so that's done now we need to actually create our paragraph element the way that we do that is going to be we're going to define a new element variable and we're going to use the document.create new element method all right so if we go to the mdn docs and we go to technologies apis dom and then we go down to the document object right here and then we go down to the methods all right so we're going down to the methods here in the documentation and we should see a create element right here so it's not create new element i don't know why i said that here we're just going to do create element and then we need to pass in the type of element that we're looking for so if we look at the create element documentation it's going to say that our tag name is the only required property or parameter of this method so any tag name works in this case we're going to pass in p for that tag name and now we have a new element assigned to a variable the next thing that we'll do is we're just going to instead of creating a new text node like we did in that first example we're just going to take the new element and we're going to use a property called text content which exists on it and we're going to set that equal to the p content variable that we created prior all right so if we go to the documentation here and we go to let's see the element reference so this is going to be of type element and then the text content is going to be part of that so really the fastest way to do this is just type text content and it's going to be part of the node interface so if you remember the dom tree is just constructed of a bunch of nodes and no matter what type of node we're working with it's always going to have the properties and methods of this node interface so you can see in the properties if we scroll down to text content you can see that we can actually set this to new content directly so we'll do that here and now we just need to basically add this to the document at the very end so the way that we do that is we come down here at the bottom we say document dot body dot append child and then we put in the new element which has our random text content we'll get rid of this console.log here all right so here's the whole function that we're dealing with so we have add paragraph we get a random number we generate random content from that number we create a paragraph element we assign the random content as the text content of that element and then we append it to the end of the html body which is basically going to be after this button all alright so let's go ahead and test this out so we press click me and we're going to get a new paragraph element with a random number every time we click it so it's pretty cool like this is where programming starts to get kind of fun is when we actually can start building things that are interactive and we can click buttons and get results so hopefully this brings you a little bit of excitement obviously it's not something useful but we will soon get there in future videos as you can see with some of the examples we've gone through there's really only a few methods and properties that you kind of have to remember with the dom we got the query selectors so how do we identify the element we want and then the methods where we can create new elements and modify both their content their attributes we haven't talked too much about that yet but we can do a bunch of stuff with these elements so the next thing we have to talk about is going to be how do we navigate an html document now i talked about the dom tree and how there's you know siblings and parents grandparents child all that kind of stuff and that's going to come into play here how do we actually navigate through a complex html document to do this i'm going to copy in a predefined html document here that you'll see we've looked at this before and we're going to basically figure out how this dom um all the dom interfaces actually work here when i talk about you know dom nodes and you know elements and how elements kind of inherit from a dom node like what does that all mean and that's going to become more apparent when we work through the navigation of this html document so let's put that diagram back up on the screen where we can see this document right here visualized as a tree and from this this visual you can see that these list items are going to be siblings and they're going to be children of the unordered list so how do we select this unordered list and then get all of the children of it with javascript well this is actually simpler than you might think so we've already been able to identify elements from the dom so we use document.queryselector and then we pass in either a tag or an id or a class name something like that to identify it since we only have one unordered list in this entire document we can just put in ul as the tag name and that's going to when we change the context here and try it again that's going to select this list right here now let's go ahead and assign this to a variable so that we have something to work with so we'll say our list is equal to document.queryselector and then we're grabbing the actual html tag so we press enter now we have a list and that's going to be showing us the html here now obviously this is just a visual representation of the object but if we wanted to figure out you know what is this variable representing you know what does the list variable actually represent we have a way of doing that if you're ever confused what type of variable you're working with you can use this handy property that every node in the dom tree has all right so if we say list dot node type it's going to return us a number all right and we wonder what is that number well if we go to the documentation and we type in node dot node type so if you were to just type in node type you can find this and once you click on it and scroll down to the constants so these type constants this will tell you what type of element you're working with so in this case we got the value of one and that tells us that we're working with an element node and from here we can click element and we can see okay this is the type of node within the dom that we're working with and here are the properties and methods that are available to us so if we were to scroll down and look at the properties of the element we see this property called children all right so if we click on that it's going to say the parent node property children is a read-only property that returns a live html collection which contains all of the child elements of the node upon which it is called all right so let's go back to our document here and we'll say list dot children and you can see that we're getting returned this html collection now from here html collection is of course another thing that we don't understand but we can once again go back to the documentation and click on html collection to see what we're dealing with and from here we can once again go over to the methods and properties and there's this one method called item on the html collection which is going to return us the element at a specific index of that html collection so basically like an array that we've worked with in javascript before but it's not quite an array you can't just come over here and pass in brackets and put in a zero or a one at least i don't think maybe you can um i'm not sure okay maybe you actually can do that but if we put in list.children.item and we select one this is going to select the second item of the list so this is the way that i've always done it in the way that the documentation kind of presents it but it looks like at least in the browser i'm working in the bracket notation works as well from here though we can identify specific elements within our list and then we can set those equal to variables just like we did the list itself so if we come over to the left and we say list item is equal to the second one in the list and we press enter now we have that that entire html element stored in a variable and we can use different properties on it to modify it so if we came over here and we said list item which is representing that second item and we said text content all right this is list item number two but we can set it equal to some new content all right so we did this already but watch what happens in the html document when i press enter it changes the content so this is just kind of showing you how you can you know look at the whole dom tree and you can say okay here's the parent and then i can get children and then i can select one of the child elements of that you know set of children and then from there i have a standalone html element that i can do all sorts of things with i can set event listeners on it i can change the text content and i can do all sorts of other things that are defined by the documentation that i'm working with now once again maybe we don't know what this list item represents what is the node type of it well all we have to do is say node type and we get the answer of 1 once again which means it is an element and from there we just go to the documentation we can just go back up here and just search element and then from here we have all the properties all of the methods all of the event listeners that are available to this specific element that we have stored in a variable all right the next thing i want to do is i want to dig a little bit deeper into this concept of nodes so i've been beating around the bush here a little bit talking about how everything in the dom tree is considered a node but we've also been looking at elements and html collections and stuff like that and it can get very confusing like all of these different interfaces what's going on here like these are all different data types they all have different properties and methods how do i know where to look what has what available to it so on and so forth well before i get into this this is a object-oriented programming concept and that's not something that we've covered yet we may cover it in future videos but this gets into things like classes and inheritance and class methods and static methods and all that kind of stuff and the thing that i want you to recognize right now is that we have all these different interfaces defined by the dom all right so at the very top we have this thing called a node so let's go back to to the node and what i want you to remember is that everything in the dom inherits from this node interface so everything below it so an element um or an html element interface these all inherit up to this node interface which basically means these elements have all of their own properties and methods but they also have access to this you know master node interface as well so if we were to go look at some of the things we have available on this node you can see this append child method that we've been using quite frequently and let's see if we can find a couple other ones um it looks like that might be the only one that we've been using but we also have text content so we've been using that to modify the text within an html element and we have node type we've used that one before let's see what else looks like that's pretty much it but you can see that this node has a bunch of properties and methods that are available to anything below it so in other words if we come back to our document here our html document and we know that we have this unordered list stored in the variable called list all right and we know that list if we say list.nodetype it is of type element which inherits from that you know node interface that basically defines every element from here all we have to do is we say list dot append child and that's going to be a valid method because it's part of the node interface as we just saw and from here we can insert an html element and actually add it to the end of this list but first we have to actually create that element and this will be a good opportunity to show another method that we've already seen but didn't really dig into yet so what i'm going to do here is i'm going to take list dot append child and i'm going to say document dot new or no create element and we're going to make it a list item and this is going to be completely empty no content within it and you can see that we have a new list item showing up at the end of this list and all we have to do now is we say list.children and we select the final element um or no we have to say item and then we select the final element and then we say text content and we can put in some content here and now we have added a new item to the end of the list and modified it so you can see how we can start parsing through and navigating through this hierarchy pretty easily once we get the hang of it now what i want to introduce is something a little bit tricky and this is where we really have to think when we're navigating through the dom because in this html document you can see that we have two p elements that don't have an id and they don't have a class and that basically means we have no way of identifying these in an easy specific way so the first way that we can you know think about identifying one of these elements is by the query selector method and what we're going to try to do here is basically insert another element between these two paragraph tags and if you go to the documentation and you go to the i believe it's the node let's see yeah so the node interface has this method called insert before and what that does is it basically inserts a html node or element that inherits from node before a specific reference element so basically in our case we're going to use this method and we're going to have to figure out how do we identify the second p tag right here now the the way that we would think about doing this is we say document dot query selector and then we grab anything that has a p tag all right and in this case when we press enter we're gonna get the wrong one because the query selector method only gets the first occurrence now what we can do is we can change this to query selector all and what this is going to return us is a node list so unlike an html collection which we got when we looked at the children of that unordered list now we have just a node list which is a slightly different data type if we didn't know what this meant we can just go back to the documentation we can type in node list and let's see what this does all right so node list objects are collection of nodes usually returned by properties such as node.child nodes and methods such as document.queryselectorall which is what we used and let's look at a few more things we have some properties we have the length of it and then we have methods so we have this method called item similar to html collection where we can select something with an index so we can go ahead and do that and we can actually just chain it to the end of this so we'll say item and then maybe we'll pass in item number one and that's going to get the first index so remember it's zero index so the first p element will be zero then the next one will be one so now we have selected the second element and what we want to do is we want to assign that to a variable so we'll say uh p element is equal to that and now we want to use the p element um let's first say node type and we're going to get a type of one which means it's an element type and we know that element is kind of like a child or it inherits from that node type which is the base class so we're getting the node type and so we know that the p element or p lm is going to have this insert before method so let's go ahead and learn how do we use that so if we go back to what we were looking at previously so we've got node.insert before now let's look at the syntax we learned about this in prior lessons how to read this documentation and what we're seeing here is that we have um the parent node right here is going to be the parent of the newly inserted node so we have to ask ourselves what is the parent to the node that we're trying to insert let's go back to our html document and maybe we can even pop up the diagram on the screen and see that what is the parent to an element that we insert between these two paragraph tags in other words what is the parent of one of these paragraph tags well the answer is going to be the main body div alright so we need to pass in a reference to this main body div as that first parent node right here so we know that we have to select that and then we have to put in a new node which is going to represent a new element that we create and then finally a reference node so the reference node as we can see down here is going to be the the node or the element that our new one is going to be inserted before um or we're going to insert it before this element all right so if this is null then the new node is inserted at the end but we don't care about that because it's not so let's go back and the first thing that we need to do is identify that parent so we'll say parent is equal to document.queryselector and then this time since we have a class to select it by we're going to select it with the class notation so we have parent we'll just check that that we're getting the right one and now we just have to say parent dot insert before all right and then we have to make a new node so the new node is going to be a p element so let's go ahead and create that real quick so actually no we yeah we need to create a new element so new p tag is going to be document.create element of type p and then we're going to assign some text content we'll say programmatically added between all right so now we have our new element and finally we're going to take the parent we're going to insert before we're going to pass in the new element that we're putting there and then we're going to reference the element that we want to insert it before so that would be p ln all right so we did that up there before so let's see if this works and you can see that we have program programmatically added this paragraph tag between these two paragraph tags right here so this was a little bit of a tricky example and we kind of ventured out into a method that we definitely had to read the documentation for before we knew how to use it but this just goes to show how we can kind of navigate around the dom by visualizing it as a tree and really understanding what's the parent what's the child what's the sibling and you know what is the interface that i'm working with within the dom all right if you are still watching this video um that's awesome most people are not going to make it through this kind of conversation about the document object model but i will say that if you're at this point you're very confused that is totally okay the purpose of this video in this crash course especially this section about the document object model is not to make you an expert at it the purpose is to expose you to what it is what are some of the things we can do with it and just get you um started and thinking independently of okay if i don't know something how do i go figure it out you know we went through a bunch of examples where i didn't necessarily know how to use these different methods when i didn't know what type of data we're working with but you go to the documentation you search for it and you figure it out so if you're confused that's totally okay keep moving forward we're not going to actually be using the dom all that much throughout the rest of the series but as i said in the beginning it's super important that you understand what it is because it is at the root of every front-end framework that you're going to work with you know if you start working in react you're going to hear something called the virtual dom and if you don't know what the real dom is it's going to be very hard to understand what the virtual dom is and furthermore if you're working with like angular or view vue.js you know these all connect in with this dom api to um basically expose their own api so you're gonna you know have some easier ways of working with this html once we get into front-end frameworks but underneath the surface those frameworks are just connecting in with the same api that we just used in this video so to do a huge recap of html i think of it in two different parts first is going to be the static html and that's going to be just the html like the stuff you see on the page here this is static html and no matter what you do it's not going to change all right now the next part is dynamic html and that is what we see when we start working with the dom and we start manipulating and adding removing changing modifying those kind of things with the existing static html so there are two different parts the first one's pretty simple and i think you probably caught you know how to use it pretty easily but the second one is very confusing and even once you work with this for years it's still going to be something that you're learning over and over and over again and perfecting along the way but what i really want you to take away is that we can model an html document in a predictable way and then we can write a specification called the document object model and allow javascript to connect in with that api to actually modify the static html now the very last thing before we get into our lesson code challenge is going to be semantic html and i'm not going to spend a whole lot of time here but when we first went through it at the beginning of this crash course we were talking about the most simple elements possible so we've got divs we've got paragraph tags images inputs lists all of those kind of things with the new html5 spec the the new thinking on html is that you know consistent with the concept that we want to use it for structure we also want to use it for intent so what do we intend to use these different elements for are we trying to section off a part of the web page are we trying to make a nav bar are we trying to make a footer you know normally you would just put all of this into uh div elements so you can make a header and a nav bar in a you know blog section and a sidebar and a footer you can make all of those things with just divs but in the new html5 spec it's you know you're basically supposed to plug into some of the new tags that they have available and in this case instead of using divs for all of these different um features of your web page you should be using a nav bar um so the nav tag and for your section so like the blog section that should use a section tag as well as the um the sidebar and stuff like that and then you have this tag called main which represents the main content of the page and then you have an article tag which might represent a single blog post and then finally you have a footer tag which obviously represents the footer so all of these give more semantic more meaning to the intent of how you're trying to use your html and it's not super important for you starting out but i want you to understand that it is out there and when you see other people writing html and people that are really paying attention to best practices you'll see those different elements pop up but remember you can always look at the documentation if you ever have a question about a certain type of html tag go to the documentation and type it into the search bar and read about it that's the only way and the best way to learn as you're reading through other people's code all right this has been a pretty long html crash course but unlike some like shorter crash courses i really wanted to walk you through my line of thinking and help you be more independent when thinking about html in the document object model so hopefully this was informative and now i have a challenge for you that's going to really test you this is a tough one but i'll go ahead and solve it with you on video after you've tried it yourself and hopefully by doing so you're going to learn a lot about you know how do we reason through some of these different concepts and how do we take our static html and make it dynamic and bring something to life so what you see on the screen i showed it to you before but this is what we're going to create where we basically have a random generator for memes and jokes and random quotes and then riddles so we can reveal the answer and this will be a really cool project that i think you'll be excited to share with your network and be sure to tag me on twitter with it 100 days of code is the hashtag that you want to use but i'm really excited to dive into this so in the next video we're going to be solving it but i i recommend that you first try it on your own and see how far you can get so the link to the code pen starter pack is in the description and what you're going to be working with is basically uh this right here so let me go back to the starter portion all right so we have some code already written for you so i've already put some buttons in here and i've already linked up some of the events that are going to be called and then i've uh made some template functions for you that are going to correspond to those click events and then finally i have all this random data down here so a bunch of random quotes and uh meme urls and stuff like that and then then i have a helper function called get random data where in each of the methods that you're going to implement yourself it's already grabbing a random piece of data that you just need to figure out how to put into the html document and display it in the right spot once again i'm leaving a link into the in the description so you can go through and see like what are the user stories so user stories are basically like what should a user be able to do when they're looking at this application and so i've got those written out i also have a few hints to help you out um just point you in the right direction on this and then once you've tried it watch the next video of this series and we will solve it together welcome to the lesson 7 code challenge and this is going to be basically an html plus javascript challenge where we're going to use the dom to basically loop in our javascript into our html and bring it alive into a dynamic web page that's going to basically generate random memes jokes quotes and riddles so if you haven't seen this before just you can click these buttons on the right and it should generate something random every time we click it and we can do jokes and quotes and riddles and you have to reveal the riddle answer by clicking this button right here so this is going to be a really cool challenge you can obviously see that there are some styles already applied to it and since we haven't covered css yet in this full stack roadmap series that's going to be the next lesson i've done this for you so i've already wired up all the css styles and all you have to do is write the html and the javascript to make this thing come to life so let's go ahead and take a look at some of the user stories so this is from the blog post that i wrote here for this lesson and just as a recap a user story is something that we use quite often in programming to come up with requirements a good example of a common user story is every time a user enters his email and password and clicks login it the user should be you know dropped into their home page within the app you know that's a basic user story that just says okay when the user does this this happens and we have some user stories defined for this code challenge the first one is a user should be able to click buttons on the right side of the screen to generate random memes jokes quotes or riddles so as we saw we can click these buttons and get something random obviously going in the correct location only one piece of content so this is the second one only one piece of content should show at a time so for example if the user clicks show a meme and there's a quote already showing the application should remove the quote and show a random meme so as you see every time i click a new button this content right here is going to disappear and it's going to replace it with nothing and then generate a different type of content so right now we have a riddle if we replace it with a joke that goes away down here and our joke shows up up here so that's another requirement the third one content should be displayed under the appropriate heading so in other words if i click tell me a joke the joke needs to show up under tell me something funny rather than i want to riddle or something like that and then when showing a random riddle the answer should never be revealed before the user explicitly clicks to reveal the answer so when i click riddle you're going to see that i get the riddle but i don't get the answer and i have to click this reveal riddle answer in order to get the answer itself the next one is if a user clicks reveal riddle answer and there is no riddle an alert should show letting them know so in other words there's no riddle populated down here and if i click reveal riddle answer i get an alert up here at the top of the screen saying there's no riddle forward to show an answer for and then likewise if a user clicks reveal riddle answer and the answer is already revealed an alert should let them know that it's already there so let's go to a riddle let's reveal it and then let's click this again and the riddle is going to say or it's going to say the riddle answer is already exposed all right so those are the requirements and of course i gave some hints here the div element within with a unique id or class name can be a great way to establish a container for future content so i'll show you how that works um we also need to use the html element dot hidden property so that has to do with hiding and showing that riddle answer it says there's no perfect solution there's obviously a hundred solutions to this and then we got some startup code all right so here is the starter code that we're working with and we have to basically take this which only has some buttons that don't do anything yet and we have to turn it into this a working content generator so as we solve this i'm going to try to walk you through my thought process a little bit but i'm not going to spend uh in overly detailed amount of time on each of the components like the main purpose of this is to give you the solution um not to basically build an entire app and think through every last detail of it so let's go to our starter and we'll keep this one up so that we have a template to look at of what we're trying to do and then from the starter let's go ahead and get acquainted with what is here already so we'll start in the html section you can see in this html section we have let's zoom out just a little bit we have some instructions so notice how we've used semantic html here so at the end of the previous video we talked about semantic html how html should show intent and what we've done here is we've created a main tag which is going to say here's the main content area and then we have uh one section which is going to be the content this is the empty area right here and then we have another section called sidebar which is where all the actions are okay so that's what we mean by semantic html and it looks like everything has been implemented for us so we have our actions our buttons and our buttons are connected up to click events that are down in the javascript section so our content what we're gonna do is add it right here in the section with a class called content all right so then of course we have css this is just styling the whole web page for us but this is not required since we haven't really talked about how to use css yet so this should just automatically work for us and there's no action required by us uh in that and then finally we have javascript all right so this one's where some of the tricky part is going to come in where we're going to be working with the document object model as you can see we have some templated functions and we have our to-do items so with each of these functions we need to show a random you know piece of content in the correct location and never show more than one at a time so similar to our user stories this is just reiterating that so we have a show meme function which is going to be connected up if we look at our html it's connected up with this show meme button all right so if we were to go down here at the bottom of this function and just console.log is working all right so we've got that connected up and now we just click show meme and if we open up the console so down at the bottom left and we see is working we know that this has been wired up correctly so lucky for us we've got this wired up already no work to be done there all right so we've got a show meme then we have show joke which is um wired up to this button and then that does the same thing for quote and riddle for the next two buttons and then finally the reveal answers function is going to be wired up to this reveal riddle answer button all right and obviously this is styled a little bit differently but it's still a button html tag and then at the bottom you can see a bunch of code that i wrote already for you which is just a bunch of content that we can use for randomly generated content and then it has a couple of helper methods so it has a data object it gets a random number and then it uses those two things in this method right here to get random data so basically what we do is we pass in we have a parameter called type and if we pass in memes we'll get a random meme and if we pass in riddles we'll get a random riddle and you can see that i've already hooked that up to these uh functions that are wired up to the buttons so as you can see we have for the meme one we have already assigned a variable to this random meme url and if we were to just console.log what we're getting here and then we press show a meme um hold on so show a meme let's go to the console and you'll see that we get a link to an image which is going to represent the meme all right so that's the overview of what we're working with all we have to do is write some html and then finish these functions that have already been templated out for us i think the first thing that we'll do is probably the easiest so in our template you can see that we have these headings here so this output looks like an h2 heading and these ones down here look like maybe h3 headings or something like that so let's go ahead and um put those in there so where it says your html code goes here let's make an h2 so all you have to do in codepen it's pretty easy is type the um type the tag that you want to make and then press tab and it creates that element for you so we'll go ahead and type output and see what we get looks like we're getting the correct element there and now we have to go to the headings so we want in the next one we want an h3 and we want programmer memes so far so good we're looking okay tell me something funny so this will be jokes so another trick you can use in codepen and also visual studio code once we start working with it um if you go to the end of a line and press ctrl c and then press ctrl v you'll get the same line copied down so let's do that one more time and then replace the text within it so give me some wisdom and i want a riddle actually we need one more so we've got programmer memes oh we missed the tell me something funny so let's do that real quick all right so we've got our output and all of our sections defined now if you remember in the user or in the hints if we go back here and look at the hints it says the div element with a unique id or class name can be a great way to establish a container for future content so what i mean by that is if we go here and we look at these headings we obviously have to insert some sort of html element probably a paragraph tag after each of these now how do we you know how do we know where to put it like how do we select each of these headings and have a container to put it after them well something that i think is smart to do is create a div element which is going to act as that container for all of our future paragraph tags which will have or paragraph or image tags which will have the content itself so let's see what we're going to do here so i think what we want to do is after each of these headings instead of trying to dynamically figure out where to put these different pieces of content let's be explicit about it so let's make a div so we'll type div and we want to give it a class or an id because we want to be able to identify this individual div as the container that we want to put the content in so for this one let's just give it a class and we'll call it meme content all right so we'll enter down and basically what we want to accomplish with the javascript is we want to identify this div and then we want to insert our content within that container that's already sitting there ready and waiting for us now we could programmatically generate a div like this but it just would be a lot of extra work unnecessary work and it would require a lot more code so let's do this with all of the different sections so we'll go down here and now let's just replace these classes because we want unique identifiers for our javascript to basically be able to query so this one will be joke's content or joke content we'll stay consistent here and then quote content and finally riddle content all right so these should be able to be selected by javascript very easily all we would need to do let me save this real quick all we need to do is open up the console let me see if i can get this all right so the console looks like we're in the right javascript context and all we need to do is document.queryselector and then since we have classes here as the attribute we need to use the class selection syntax which is the dot preceding the name of the class so if we wanted to select this container right here called meme content we can just type in dot meme content and we should get that div all right so from there all we have to do if we want to insert something in it is we save it in a variable so we'll maybe say a container and then from there we say container dot append child and then this is where we pass in our generated html element so in this case let's just go ahead and create something for that this is just practice to understand how we're solving this problem we're not actually doing anything yet so we'll say new element is equal to document.createelement and then maybe we want to put in um let's let's go ahead and practice this with the meme container so we need to put in an image so to do that we type in image tag all right so we're going to create that element and then we're going to use something called the set attribute property all right so in order to create an image element we know how to do that so let's go ahead and practice that here if we say image it's going to give us a source attribute and an alt attribute we're going to ignore this alt attribute because it's not like totally necessary and then from here we can pass in a url to an image and that will display it within the browser here so let's just go ahead and go down to our data here and we've got this array of memes so let's just grab one of the urls just to test it for a second and paste that into the source attribute of our image and you can see that our image is coming in there nicely and i've actually written some css if you were to look in the css portion i've actually selected all images here and i've said that the max width is going to be 350 pixels so it's already going to come in nicely for you you don't have to worry about doing that so this is basically what we want to insert into that specific programmer memes area and if we wanted to do that we've already got our new image element and all we have to do here is say new element dot set attribute all right so if we were to if we didn't know anything about this this is how we'd figure it out so first we say new element dot uh node type so we talked about this in the prior video looks like we lost the context so we have to go back to the codepen context let's see if that still has this okay we got to redefine this real quick so first let's select the container so we just got to redo this because it lost the context that we were working in so now we have the container which is the meme content and then we have to create the new element alright so we created an image and now we just say new element dot node type and it's going to return us one now in the mdn documentation let's go to the mdn documentation real quick and let's type in node type this is a method we learned about prior in prior videos and you can see in the node constant or the node type constants a value of 1 represents an element so let's click on that element and then we come down and we can see that within the element there are methods and one of those should be if i'm remembering this correctly is set attribute so if we click on set attribute it says that we give it the name of the attribute we're wanting to set and then the value we want to set so in this case if we were trying to set the source attribute of an image we just have to say new element dot set attribute source is the name of the attribute we're setting and then you know maybe we'll paste in that link that we had earlier so that will be the value of the source so we press enter and now what we can do is basically take this new element that we have and we take the container and we append child we use the append child method to pass in that new element and boom we have a new image showing up in that section that we want it to show up in so like i said we haven't actually done anything this is just practicing and figuring out in the live console like okay how do we actually reason through this what methods are we going to use all that kind of stuff now we should be able to go down into the javascript and implement some of these functions to do exactly this all right so let's close this out here we don't need this for for a little bit and then let's let's refresh the page because we want a clean slate here um it looks like actually not sure why this is still there um let's see oh that's right because we had put this in here already okay let's get rid of that we don't want anything to start off we want this to be empty and now let's come down to our templated methods and in this case we want to show a mean right now when we click the button and we open up the console it's going to print a random meme url and we know that this url should be set equal to the source attribute of an image tag or image html element so we also know how to create that so let's go ahead and start writing this method the first thing we need to do is grab the container that we want to put it in so we will say container will be equal to document.queryselector and then we need to pass in meme content all right so we should have the container selected you could obviously test that if you wanted to i think if we just pass it into console.log we should be able to do that and show a meme and we get the correct container so we're doing good so far from here we need to say new image is going to be equal to document.create it's a create element or new element i can't remember okay so it is create element and we want an image element now we just need to set the attribute so we want to set the source attribute and what do we want to set that equal to well we want to set it equal to a url and we happen to have that already so we just drop in the random meme url and that should set the attribute for us and then finally we come down here at the bottom and we say container that append child and we pass in our new image okay so let's go ahead and save our our pen here and when we click this show meme we should get an image populating in the right section and we do all right so if we click it again it should put another image in there after this and it does and then another one and we could keep going with that but obviously the instructions one of the user stories is saying if there's already something there delete it and replace it with the new one so we have to figure out how to do that and a good way to do that is by using an if statement so if we come down to our method here of show meme we need to ask before we insert that into the container we need to say if the if there's an existing image there so how do we do that one way that i can think of is if we take is we'll just say set a variable is image equal to container dot query selector and then we'll look for an image so basically what this is doing is it's saying this container which represents this div right here if we grab that container and within it we look for an image element and if we find something then this value is image is going to represent the actual html element if we don't find anything then it's going to return null so we're going to get nothing so basically what we have to do is we say if is image so if that's a truthy value which should it should be truthy as long as it comes back with an image then we need to clear the contents of this container so a way that we can do that is by using the inner html property and if we go to the mdn documentation i believe this exists on the node interface so let's take a look looks like it's not here so let's search for that so inner html okay so it's on element so on each html element there is this property called inner html and it says the element property inner html gets or sets the html markup contained within the element so a quick trick this is something that i just have learned over time you wouldn't necessarily know it from this documentation is you can pass in an empty string to this property to clear all the html from a specific container so if there's an image already there we're going to say the containers inner html is equal to an empty string okay so basically what we're going to do is every time we click this button we're going to generate a random meme url then we're going to create a new image we're going to check if there's an existing image and if so clear it and then we're going to insert the new image so basically this is a another way of saying let's replace whatever's there so let's try it out so we got our first image here and if we click this again it should replace it excellent so we're replacing it correctly we've got this in the right spot and for the rest of the methods we pretty much should be able to implement the same type of logic and implement it for all the rest of the buttons so let's get rid of this to do because we have done it we have implemented that correctly now one thing i want to do before we get any further is implement this clear all method now the reason i want to do this is because it's basically since we see that we have buttons that kind of do the same thing we don't really need to write this logic right here every single time all we need to do is we need to write one method that looks at all of the containers here and it clears all of the containers before we replace it with new content so in order to do that let's go ahead and take some of this code here so we'll take that out of there and put it in the clear all so let's rename this variable and we'll say meme all right and let's go ahead and grab this container so actually this is not valid code we need to type document.queryselector and we need to grab the meme content container so we'll say meme container is equal to that and this should represent this div right here now we can do the same thing for the other divs so the joke content and the quote content and the riddle content so i'll show you what we're gonna do here like this will make sense in a second so just bear with me so we're going to copy this down four times we're going to say this one will be a joke container this one will be a quote container and this one will be a riddle container and then we of course have to replace this with joke and quote and riddle all right so in this javascript that we just wrote we are basically selecting each of these containers that should have the content that we're putting in and from here all we have to do is set the inner html to an empty string and no matter if there's something there or there's nothing there it doesn't matter we're just going to clear it no matter what so we'll say meme container dot inner html equals an empty string and we'll do that four times over and just replace these variables with the correct one all right so this clear all method is going to look in all of the different sections and it's going to clear them no matter what's there so all we have to do down in this show meme thing is before we append a new image we just have to call the clear all method so that should clear everything out so let's try it out let's show a meme and then click it again and it looks like it's clearing it and putting in the new one and that's going to happen for all of these because we're just going to pass this clear all method into each of these template functions okay so the next one is show a joke all right so we've already got a random joke text but let's go ahead in console.log the random joke text just to see what we're working with so let's click the button make sure it's working and let's open up the console all right it looks like we're getting a string that represents the joke so in this case we want to insert a paragraph element and put this text content in it alright so the this should be rather simple so we'll say new paragraph is equal to document.createelement and we'll put in a p tag and then we say p dot text content is equal to the random joke text so i'm taking the variable here which should be equal to a random joke and i'm assigning it to the new p element that i just created and then finally i need to clear all because i need to clear anything that's there already and then i need to insert it into the container so before we wrote out a different variable that selects the container and then we did it in two steps but we can basically just do this in one step so we can say query selector in this case we want to get the joke content i can't type and then at the end we can just chain on this append child and pass in the new paragraph element let's see if that works by clicking tell me a joke and it's saying p is not defined that's because i for some reason uh just said p instead of new p so that should work better this time all right so we're getting um some content here now this will be the real test if i click show a meme does this content get erased and a meme show up that's what should happen so let's show a meme and we see that in the jokes section there's nothing there which is great so let's tell another joke and you can see that we have a joke but no meme so far everything's working great so let's go to the next method that we need to implement so this will be a quote all right so let's go ahead like we've been doing will console.log the random quote and we'll click the button and you can see in the console we get an object with two keys or two properties we have the author property and the quote property both which are strings so what we want to do here is we want to display a p or a paragraph element and then another paragraph element right below it with the author so let's do that so we'll say the quote itself is going to be document.createelement and that will be a paragraph element and then we'll say the author is going to be document.createelement and that will also be a p element now for the text content the quote will be pretty straightforward we just say quote.textcontent equals the random quote and then we need to actually get the quote property which is the quote itself now for the author text content we need to grab the random quote again and grab the author property but we also want to put that little dash before it so all we have to do is make a string with a dash and a space and then we concatenate that with the um the author name all right so we've got our text content or let's see if we did this right so we got our quote and then our author stored in 2p elements so now all we should have to do is first clear all so we clear everything out and then we come down here and we say document.queryselector we want to grab the quote content container and then we append child and we put in the first we want to put in the quote and then the next thing we want to do is grab the same container and put in the author so let's go ahead and run this looks like we got a random quote working just fine but this is kind of messy i want to refactor this a little bit we'd never want to write the same thing two lines in a row that's just bad practice so what we want to do is we want to store the container like we did before in a variable so let's put this right here so now we have the container in that container variable and now we just have to call container.appendchild for both of these and this should work still awesome so now we can do jokes we can do quotes and we can do memes and they all kind of clear each other out when a new piece of content is generated now the last one is going to be the hardest one and that's because we only want to show the riddle itself but we don't want to show the answer to it until this reveal riddle answer is clicked so let's mark off our to-do's here we are done with the show quote we are done with the show joke and we're done with the clear all method so we're we're doing pretty good here and finally we have to show the riddle so i think what we're going to do is let's first see what we're getting it already shows us i already wrote in a comment that says this is what you should get but let's just verify it always good practice so console.log random riddle and let's click the button we get our random riddle with a answer prop and question prop so let's let's go ahead and um be explicit about this and i'm going to show you a little es6 or the latest spec of javascript i'll show you a trick on how to destructure i believe that's the correct term for it but we're going to destructure properties off of this object so all we have to do is we say const and then we put these little brackets here and then we pass in the properties that we're looking for so we want a question and we want an answer and that will be set equal to the random riddle that we're getting so now we should be able to console.log a variable called question as well as a variable called answer right here so let's see if that is true all right so we got these printing out okay and basically what i did here this right here is the equivalent so let me comment it out for a second what i just wrote there is the es6 or the latest spec of javascript equivalent syntax to doing this so we say question is equal to random riddle dot question and then answer is equal to random riddle dot answer so these two lines of code can be simplified into this single line of code right here by putting these little brackets around there and selecting the property that you want so that's just a little trick thought i'd show you as we're solving this all right so we've got our question we've got our answer now we have to create our paragraph elements so i think we're going to show the question and the answer both in a paragraph element just like we did with the quote but we obviously have to hide the answer up front so how we'll do that is this we'll say the question element will be equal to document.createelement we'll put a paragraph tag in there and then we'll set the text content equal to the question itself and then let's go ahead and take since we're inserting twice let's go ahead and get a single variable for the container so document.queryselector and we'll grab the riddle we need a dot at the beginning to select a class we want the riddle content div so that will be this riddle content div and that should work and now we want to say container.appendchild and we'll put in the question element let's see if that works okay awesome we have this paragraph element um representing the riddle and now what we have to do is we have to insert the answer but we have to make it hidden all right so i think the way that we're going to do this if you remember from the hints it says you may need to use the htmlelement.hidden property so let's take a look at what that is so the html element property hidden is a boolean which is true if the element's hidden otherwise it's false and you can actually set this equal to true or false to navigate between something that shows and something that hides so that's good to know we should be able to use that on any element that we're working with here all right so what we're going to do is we're going to create a similar to this question element let's just copy this down and this time it's going to be the answer so we'll do answer element and then that will be set equal to the answer all right now there's one additional thing that we have to do so we've got this button over here called reveal the riddle answer and that's going to call this function down here called reveal answers now if we're anticipating in the future this is something that you'd probably figure out after you implemented it and you're like oh shoot i realized this now but i've already built this i know how it works so what i'm saying is that in the future we're going to have to click this button and we're going to have to figure out whether there's a riddle answer available and whether that riddle answer is hidden or not hidden so in order to do that in order to make our jobs easier we should probably set an id attribute on this paragraph tag we're calling answer so we've already set the text content so let's let's go ahead and append this to the html so just so that you can see what's going on here so we're appending the answer element so now when we say riddle me we get the riddle and the answer which is obviously not what we want to do but what we can do here is after this we say answer element dot set attribute and what i want to set is an id and then what i want to do with that id is set it to riddle answer all right so what this is basically going to do is down here in the riddle content it's going to create a p tag that has an id equal to riddle answer okay and then the answer to the riddle will be sitting right there so later on when we click the reveal riddle answer button it's just going to say okay go find me a p tag with an id of riddle answer and if that's there then if it's there and it's hidden i want to unhide it if it's not there i want to alert the user that it's not there okay so that's basically what we're doing with that code down here and then of course we need to call the clear all method as we did with all the other functions and then the last thing we need to do is we need to hide this so we need to grab the hidden property and set it to true on this answer element so when i click riddle me we should see just the question and the answer should be hidden so let's click riddle me and we get exactly what we're looking for here so that's good now the last thing we have to do so we have completed this to do i believe let's click let's just test it by clicking out or clicking on some other ones okay looks good so far and now we have to implement this reveal answers so let's go back to our requirements here our user stories say when showing a random riddle the answer should never be revealed until we click that button so we got that if the user clicks reveal and there is no riddle an alert should let the user know that there's no riddle there if it's already there and it's already revealed another alert should be popped up so what we need to do here is we need to first check let's see what do we need to check so what we need to check is if there is a riddle answer or not or not a real answer but a riddle in general so the way that we do that is we first grab the riddle container which will be equal to document.queryselector riddle content okay so that should give us this div right here and then from there we can say the riddle is going to be equal to the riddle container dot query selector and in this case we want to search for a paragraph tag so if there's any paragraph tag sitting within that div we know that there's a riddle there and if we know that there's a riddle there then we know that you know we either need to reveal the riddle answer or we need to alert the user that it's already revealed okay so now we're going to do an if else if statement so we'll say if there is a riddle if there's a riddle revealed and then we actually we have one more step to do so we need to say the answer is equal to document.queryselector and the answer is going to be what we named it above right here so riddleanswer is the id that we're looking for so we just put this hashtag to search for an id rather than a class and we type in riddle answer okay so there's our answer and what we're going to say is if there's a riddle and there is an answer then i want to alert the user there is already an answer revealed actually no because even if there's an answer if there's an revie hidden it's still going to be registered under this variable so let's go ahead and test that out real quick let's comment this code right now and then we'll console.log answer okay so what we would expect is we've already created in this previous method we've created this element with an id called riddle answer but we've hidden it so it's there it's just hidden so what we want to check is when we search for this by its id if it is hidden what value do we get so we're going to console log that answer i'm first going to click riddle me and then i'm going to click reveal answers i'm going to look at the console and you can see that even though it's hidden we still found the element within the dom using this query selector so in other words we have to check whether it's hidden so we gotta print out answer.hidden instead of just answer so let's generate a riddle and then reveal it and we're gonna see that it that the hidden property is set to true so now what we need to do is uncomment this code and we're going to say if riddle and the answer is hidden then we need to reveal it okay so we need to say answer dot hidden equals false all right so that line of code right there should pretty much complete our web page here with the exception of those alerts so let's go ahead and generate a riddle and click reveal the answer and you can see that it reveals the answer right there okay and maybe we even want to come up to this method and instead of just setting the text content to answer we can put the answer is and then the answer so let's try that one it says the answer is an echo so perfect now the last thing that we have to do is handle those other cases so else if so if the riddle is true so if there's a riddle there and the answer is hidden we're going to reveal it if the riddle is there and the answer is not hidden so right here um so we basically this is redundant so if we make it to this else if block that means that the that either the riddle was not there or the answer was not hidden so if we get here and we say if the riddle is there that's all we need to check so if the riddle is there and we're trying to um we're trying to unhide it again then we're going to say alert the answer is already revealed okay so let's see if that works so we'll click the riddle we're going to reveal it and we're going to click the reveal button again and it says the answer is already revealed all right and then the last piece is just the else statement which basically means that we don't have a riddle at all but we're trying to reveal the answer so we'll say there is no riddle to reveal the answer to sorry the video cut out here i do not know exactly where i was at but basically we were just completing the last part here where if you click on the riddle and you reveal the answer it reveals if you reveal it again it's already revealed or if you have a random quote and you try to reveal the riddle answer it's going to say there's no riddle to reveal the answer to so we've pretty much completed this challenge hopefully it was fun for you a quick way that you can share this is click the share button down here and share it on twitter be sure to tag me at zg underscore dev and put in the hashtag hundred days of code that's where you can get support from the tech community the webdev community on twitter and hopefully um we'll see this popping up a couple times i think this is a fun little web page to create with some interactive javascript and if you want to customize it a little bit you can go down to the js section and replace some of these quotes and some of these memes and jokes with your own favorites in this video in subsequent videos we're going to be going through a css crash course so this is all about styling your web pages now before we get into a lot of the details i want to show you exactly what we're going to be able to build by the end of this and we're going to use a tool called front-end mentor because this is basically a bunch of professional looking web designs that have been implemented for us but they have not been coded so what you do is you click on a challenge and you can go in and get the design and then you code up the design and submit it and get feedback on it so the three that i've chosen based on what i want to cover are going to be this little single price grid component this will be the first one that we do right after getting through the crash course and it's a nice little introductory challenge you see that we'll make a desktop version and then a mobile version and then we will move on after we've gone through a couple other tutorials to this testimonials grid section which is going to help us get through the skills of flexbox and css grid which are going to be super important to you as a developer before i get into the topics i just want to say that the prerequisites are basically you got to know how to write html the basics of html is all you need we're also going to be working in the codepen environment so this is just an example right here where we can type out html css and javascript so get yourself comfortable with that and then optionally like i said we're going to be combining all of our skills for some of the code challenges so watching the previous parts of this full stack developer series is going to be important now in this lesson on css i want to go through what we're going to actually learn but more importantly what we're not going to learn i'll be completely honest when i put this together i got a little carried away i thought this was going to be a simple crash course in css but was humbly reminded that css has a lot of things going on there's a whole lot of things that we can cover and i tried to keep it honed in on the most important concepts per how i've been trying to do it with this series but there's just a lot to cover so as you can see on the left we're going to be talking through what is css and the basics of it we're going to be walking through the box model which is basically defining how much space an element is going to take up on your web page that's going to get us into some discussions on block versus inline elements box sizing and layouts and then we're going to cover a bunch of other things and i'm pretty excited to share a four-step system that i use when i create html and css basically take a design and put it into code because thinking through that was really hard for me when i first started and i want to share that with you so that's the main core lessons we also have some supplementary or optional lessons which includes responsive design so what happens when your web page gets put and opened up on a mobile device like an iphone does it look the same is it going to have a pleasant experience for the user in today's age that's required if you want to make a web app so we're going to talk about that in a separate video and then we're also going to have separate videos on flexbox and css grid which is going to allow us to layout our elements a little bit and like i said we have this project right here which gives us a great opportunity to apply those concepts and put this into a nice looking grid now what we're not going to be covering in this crash course is going to be graphic design and wireframing now there's a good reason for that that we're using front-end mentor because they already have professional looking designs built out and implemented for us and i don't want to waste a lot of time on going into design principles that i'm not really an expert at so we're gonna use and leverage existing designs and learn how to turn those into code on a web page don't get me wrong these are super important concepts as a full stack developer you always start with your wireframing and your design and your requirements and all that kind of stuff but that kind of comes later down the road i really want to focus on the core concepts of how do we actually turn things into code we're also not going to cover css preprocessors i'm not going to talk too much about it but basically it's a more convenient way to write css and it basically allows you to write a little bit less code and it gets pre-processed or i guess transpiled down into vanilla css through a bunch of code that does that so sas and less are popular preprocessors and you'll often find them on much larger projects where trying to organize your css is really important but we're not going to be working with that we don't have huge projects going on and it's not essential to our journey what we're also not going to be talking about is css frameworks so the last thing that i want to do in this full stack web developer series is lock you into a path to something that is really popular right now but may not be in two years and that's kind of the deal with css frameworks when i was first learning bootstrap was huge every tutorial was talking about bootstrap and now tailwind is a pretty popular css framework and all these frameworks are doing is taking vanilla css what we're going to be learning and writing some abstractions on top of that and just making it more convenient to you know lay out elements on the page and style them more quickly and efficiently i think it's really important to understand the bare bones basics of writing vanilla css and therefore we're not going to be looking a whole lot at these all right at this point i think we are finally ready to get into the css crash course as always apologies for the long intro but i really want to make sure that we're clear on what we're actually going through in this lesson the very first thing that i want to dive into is the basic syntax of css so we already learned about html and you might actually recognize this html diagram up here from the last lesson now css is relatively simple and all we're trying to do is target a specific html element and apply some sort of style to it or some some sort of layout to it in this example here you can see that we have defined an html element with a class called sum dash class and then down here at the bottom where we look at our css we're using something called a css selector and we're targeting a class of some class so we're basically saying with this rule down here i want to find any html element that has a class of this and then i want to assign this specific css property to that specific html element or elements if there are multiple elements that have the same class now this is just one of many examples that we can go through so in this case we're targeting a specific class we could also target ids or html tags and we can do a bunch of combinations of the three so we're going to be walking through how that works and then we're also going to be talking about these properties down here so similar to what we talked about with javascript when we're looking at javascript objects where we have you know some sort of property and then a value at the other side of that property same thing applies here with css we have a bunch of predefined properties that we're able to use to achieve different types of objectives whether that be laying out you know html elements on a page so do i want to put something side by side with each other or do i want to just style it so do i want to make the background green or do i want to make the text red or something like that we are of course going to get into all of the most common properties and selectors and all that kind of stuff but as with all the other lessons that i've taken you through i want to basically introduce this documentation so this is the mdn web docs we have visited this many times in the prior lessons of this series but now if we go to technologies we can go to css and then we go to the css reference and then we scroll down and you should find this section called index and this is going to list out all of the possible css properties that we can use to achieve these layout and style objectives for html elements so as we go through this if you ever get lost you can always look up these properties that we're using on this documentation page so we've got the basic syntax of css and we'll go into that even a little bit more but what is the actual purpose here like what are we trying to do well there are two things in my head that css is really good for and that is going to be layout and style so how are the elements the html elements laid out on the page are they on top of each other are they next to each other are they in a grid a table all that kind of stuff and then furthermore we can actually apply styles so what color is the text what color is the background do you have a background image what kind of font are you you know working with on the page so what i did was i put together a really basic example of this this is nothing fancy but it just shows you don't worry about the css quite yet here but it basically shows you how we can combine the two so we've got a container on the left and a container on the right and then furthermore we have colored borders and a little bit of colored text within this along with you know some headings and paragraph text so this is kind of the ultimate goal of css we can not only you know do style but we can also do layouts but if you zoom out even further the real goal of css is to target a specific html element or elements and apply some sort of styling to that element or elements there are three primary ways that we can select and style html elements with css we can do it by html tag we can do it by a class which is the the syntax example that we looked at a little bit ago then we can also do it by id so if we look at this on the page here let's go ahead and just make a simple paragraph tag and put some text in there so the first thing we can do is we can just target that specific html tag and give it a different text color and you'll see that this turns red up here we can also add in a class so we can say paragraph you know element or something like that and we can replace this by adding a period before which indicates i want to target a class and then you type in the class that you're trying to target and then you give it some sort of style and again we can see the red text there and then finally we can also have an id on an element so let's just call this one abc or something like that and we can come down here and use a hashtag in the front and that's going to say i want to try to target some sort of id on the page and then you type the id and then once again give it some sort of css property as you can see we've styled this red once again if you've been following along with this series these look familiar to you because we talked about them with javascript already so if we were to come down to the javascript section and you know we go through our basic document dot query selector and then we try to target a specific html element on the page we use the same selector syntax so if we were trying to target this element by its html tag we just drop in the letter p which targets all or targets the first paragraph element that it finds on the page we can also target it by class so once again we put a dot before and then we target the class that we're trying to go after and that's going to find the first html element with that class on it and then finally we can do the same thing with these ids so we can just type in hashtag and then the id and we've selected it with javascript so this is actually a pretty familiar concept if you've been following along with this series and it's relatively easy to kind of grasp and hold on to but what is not so simple is understanding what happens when you have multiple css selectors in a single style sheet and they're all targeting the same element what happens there which css rule takes precedence over the other for example if i put in this css style sheet if i targeted the id right here and gave it a color of green let's see if i can write that correctly and then i also come down and target the class and give it a color of red you have to ask yourself which one's going to apply we're going to cover that here in more detail in just a second but i want to go through a couple other ways that we can actually target html elements when we have some more complex html in our web page aside from selecting html elements with tags classes and ids we also have something called combinators that we can do to parse through all the html and target specific elements and to help us with this i'm bringing back a diagram that we looked at in the prior lesson on the dom or the document object model and what the dom describes is the structure of your web page and how the html is laid out and in this diagram we talked about how there's grandparents and there's parents and there's children children and siblings based on the level that an html element is at in the document so you can see that coming down here we've got you know an h1 a p tag another p tag in an unordered list and all four of these are considered siblings so that's just a quick review and what we can do with this is we can come back to our playground here and actually select different html elements based on these relationships the first combinator that i want to talk about is selecting descendants in html so if we go back to our our visual dom tree you can see that this div right here has several descendants so it has direct children which are these four elements but then it has grandchildren which the children and the grandchildren are all considered descendants of that specific div so what we can do within css is we can target all descendants with a single selector now i'm going to do it the wrong way first so that we can see why this matters in the first place let's say that i wanted to style these list items right here one way that i could do it is i could target the html tag li and then i can give it some sort of style so maybe i want the text color to be green i'm sticking with this color property because we haven't talked about all the css properties yet and it's pretty simple and easy to kind of intuitively understand so with this example i tried to select and style these list items and did so successfully but by targeting it just like this we run into problems if we change the html structure in the future and let's say that we put in some sort of footer so this isn't going to look great but let's go ahead and make a div and then an unordered list and then you know footer link number one and then we'll put three of those in there so number two and number three so this list right here imagine this being down low in the footer and what we would want to do with that is we want to actually make those align horizontally and we probably want to style them with a different text color and we probably want to give them a link and you see what's happening when we just select the li elements all descendants we're actually picking up these footer links right here and we don't want to be so there's a better way to actually target specific types of descendants within a document to allow us to have more specificity with targeting our html elements to make this example a little more clear let's go ahead and add some class attributes here so this will be the we'll call it the main container and then the second div down here is going to be called the footer now this is not semantic html like we talked about previously but it'll do the job here so what we're trying to do is we want to style just these list items so the the ones within the main container div but we don't want to target the ones that are sitting in the footer because those are going to be styled a little bit differently the way that we can do that rather than just targeting the li tag what we can do is we can actually combine selectors so combinators is what we're calling this and i can say i want to target the main container class and then i want to look for any list item that is a descendant of that main container class so the first div that we talked about here on the left that's the only div that has this class and so therefore we're only going to be styling the ones that we're targeting at this point we can also add additional list items to our main container div and it's going to get that style so if we were to come down right after this list make another div another on an ordered list and then put in some text here you're going to see that that will turn green because it still is a descendant of that main container but once again we have our footer links you know avoiding that style because we've been specific in selecting only descendants of that main div now i want to stop for just a second and look at this syntax that we're working with so you can see that we have a class selector with this period right here that determines that and then we have a tag selector separated by a single space so what we call this first one is going to be a pre-selector and then the li is the actual selector so in this css rule that we've defined we're not really targeting the main container we're targeting list items so the one that is furthest to the right is always going to be the html element that we actually want to style if we were just targeting the main container here so if we drop this down into a separate rule and gave a color of blue per se you're going to see that not only list items but everything else there is going to get that style so it's very different saying i want to target this main container class and saying i want to target all of the list items that are descendants of this pre-selector main container class all right i'm going to clear all these styles out and also clear the html that we're working with and let's go ahead and drop in some different html because the second one that we're going to talk about the second css combinator is how to select children of a specific element with the descendant selector which is just a space separating two different selectors we're grabbing everything below a certain html element but with the children selector we're only grabbing the first children so with this div right here that we're calling an id of main we only have three or we have four different children so we have the first three paragraph tags and then we have this div element which are all going to be considered children so if we wanted to style just those elements then we can use a slightly different syntax so remember we're targeting an id right here so instead of using a period we're going to use a hashtag then we're going to pass in that id drop down into our css rule and now what we're going to do is give a color of we'll call it green again now this is just going to target everything because we're just targeting everything in the div but if we added a child selector combinator we can target just those first three paragraph elements and how we do that is we use this little carrot right here and we type in paragraph tag right after that so what this is saying is i want to look at the div with an id of main and then i want to grab all of the children the direct children that are p tags below that so the only three that qualify for that rule are these first three paragraphs and therefore we style them to have a text color of green so we know how to select descendants we know how to select direct children now there's also a css combinator or combinators that we can use to select sibling elements now i will make the disclaimer that i don't use these ever i don't know if i've ever actually used these sibling selectors and the reason being is because whenever you change your html structure it's going to affect the css rules so it's a little bit brittle when you're putting these together but the reason i'm walking you through them is because you may see them in code somewhere and it's just good to know that they exist and how they work to do this i'm going to copy in a little bit different html because we need a different structure to work with and let me zoom out just a little bit so we can see more so within this html you can see that we have another you know main container and then we have paragraph elements and list items uh separated throughout the html i'm going to get rid of this css rule right here and now we're going to select some sibling elements let's say for some odd reason you want to style every paragraph element that directly follows a list group so in this case it would be this first paragraph here and the fourth paragraph here because they directly followed these list groups in order to do that we can use a special css combinator that selects only the first sibling of an element what we call this is the adjacent sibling selector so what we'll do is we will grab let's see how do we do this we need to grab all unordered list so ul is what we're going to target and then what we do is we add a plus sign and then we target all p elements that are going to be adjacent siblings to those unordered lists and from here we can just assign we'll call it blue this time and what you'll see is that only the first paragraph and the fourth paragraph have been targeted now what i was saying just a little bit earlier is that if you change your html structure at all these rules break so if we were to come in here and for you know some reason this is actually pretty common we wanted to maybe wrap this last unordered list in a div so let's copy this in another div and then you're gonna see that our rule that we wrote has broken down so this fourth paragraph here is no longer styled with blue text and that's simply because we changed the structure of that html so that's why i'm not a huge fan of these selectors they're pretty fragile in that sense now the last sibling selector we looked at the adjacent one which just grabs the first sibling next to an element but we can also grab all siblings next to an element so let's say that we wanted to style all of these paragraphs that are direct siblings to an unordered list the way that we do that is we open up our css and instead of a plus sign we're going to use this little tilde sign right here and what that's going to do is it's going to look for unordered list and it's going to look for direct siblings that have a you know a type paragraph and give it this style so once again pretty fragile i don't use these often but they're good to know you know how they work and you might see them in code at some point and hopefully you would fix them and make them a little bit more robust over the last few minutes i've talked about how fragile these selectors are but didn't really offer you a better solution so that's what we're going to do right now if i had a situation where let me go ahead and remove these css rules if i had a situation where i wanted to target this first paragraph in this fourth paragraph what i'm going to do is just give them a class so i'm going to come to the html and on the first paragraph i'm going to say blue text color is going to be my class name and then i'm going to copy that same class down to the fourth paragraph and now i'm just going to select that class with our class selector and then i'm going to give it a color of blue and now we've achieved the same exact result but now if we change the html structure this css rule is not going to break so that would be the preferred method at least in my head of doing that now believe it or not there are other ways to select html elements other than these the main three which is tag class id and then also the combinators we have some other ways to do it so let's say that we had this specific html right here so we have a p element and a div element and they both have the class called regular text let's say for some odd reason we want to style all elements with a class regular text a certain way but we don't want those styles to apply to a div element we only want them to apply to a p element well one way that we could do this is we could come down to our css and write this funky syntax where we actually specify the class that we want to target along with the tags so we're kind of grouping them all together so what this is saying is i want to style only elements that are paragraph elements that also have this class so it's kind of like an and statement and you come down here give it a color of blue we'll say and now we have selected only one of these as opposed to if we just were to select the class and then give it a color blue it's going to apply to both of them so that's a kind of obnoxious way to solve this problem right here it's totally unnecessary but i just want to show you that there are other ways that you can do this and i mainly would say if you're going down that route i would use it on a case-by-case basis you don't need to go out trying to learn all the different ways because in the end the three main ways tag class and id are going to be totally sufficient for writing good web applications by this point hopefully you're at least somewhat comfortable with writing a css rule that targets a specific html element we also kind of talked about how you might target multiple elements i'm going to write out a couple css properties that we have not covered yet so i wouldn't expect you to understand how they work but just try to follow along intuitively for the moment and we'll get into them later so we've got this html right here where we have a div with a class of box one and then another div with a class of box 2. and what we want to do with this is we want to apply some styles that are going to be shared across these boxes and then some styles which are going to be unique this is very similar to the example we looked at a little bit earlier where we have two boxes right here and some of the styles are the same so these are the same sized boxes they both have borders but the borders and some of the text are going to be different between the two so how do we go about you know writing the least amount of code and achieving that same result here is going to be the most inefficient way to do it but it's still going to work okay so what we're going to do is we're going to target box 1 by class and then we'll also come down in target box number 2 by class now at this point we can use some css properties again we haven't learned all of these quite yet but maybe we want to give a border to both of them so i want to give box 1 a green border and then i want to come down and give box 2 a blue border so now we have borders around these elements and let's also add some padding to both of them so we'll give them 20 pixels of padding we'll talk about these units a little bit later in this crash course and then finally let's go ahead and write some text in here we've got box box 1 and box 2 and i want to give these different colors so this box will have green text and this one is going to have blue text so far in this example everything's been a little bit different but let's say that we wanted to um make these the same width and height so let's say that we want box one to have a width of 200 pixels and then a height of 200 pixels and then we come down here with box two and we give it a width of 200 again and a height of 200 again so in this case we've got two boxes that have some different properties but some similar properties so one way that we can actually simplify our code a little bit and share some properties is by this special css syntax where we just leave a comma between the two classes that we're trying to select or ids or tags or whatever you're trying to select both at once so what this css rule is doing here um this box one and box two is it's saying i want to select both uh elements with a class of box one and elements with a class of box 2. so what we can do here is we can take these width and height properties and take them out of these individual styles and go ahead and put them right here so we've basically eliminated two whole lines of code and we've followed the dry principle which is do not repeat yourself and that's a pretty important one that you're gonna you know come across as you develop you know bigger and bigger software as with everything in code there's multiple ways to do this so we could write this rule right here where we're targeting two elements at once and applying shared styles now we can also achieve the same exact result by instead of targeting it in our css we can actually give multiple classes to a single element so instead of having just box one and box two maybe we have each of these have their own classes but then they also have a shared class which is called box so instead of coming down into the css and targeting it like this all we have to do is get rid of these and give them that shared class and now we're going to get the same exact result but in a slightly different way now honestly this is probably my preferred way of doing it it just is a little bit more straightforward and simple in my head but both of them are valid ways to you know provide shared styles to your html all right for the most part i think we've covered a lot of the selector stuff with css but while we're here we do have to talk about the pseudo selectors now we're not going to get into a whole lot of detail here because i don't think it's extremely important at this point but i do want to mention it i want to show you probably one of the most common ways of using something called a pseudo selector so let me get rid of all this stuff here and paste in a simple button so you can see that we have a button right now and you know click it it doesn't do anything it's not styled nicely or anything like that but what we can do with this button is we can apply something called i believe it's a pseudo class and that pseudo class will allow us to style this button differently based on different states of the button before we get into this there is a page on the documentation if you just type in pseudo classes to the mdn documentation you can read up on it but let's go ahead and get back to this example and i'll show you exactly what this does so what we're going to do is first target the button so maybe we want to apply a couple styles to this button again we haven't gone through the properties quite yet we're going to get there but let's just say that we want to give a background color of navy and then we want to give we'll say text color of white and you're gonna see that we now have a slightly better looking button not really a really nice looking button so maybe we get rid of the border so that it looks a little bit cleaner and then add some padding in here all things we're going to learn a little bit later in this course so now we've got a button that looks a little bit better not great and what we want to do with this is we want to attach this hover sudo class which basically says i want to target the button html element and when it is in its hover state so basically when i hover my mouse over it i want to apply these styles so the first thing that i want to do is give it a different cursor so when you hover over this you'll see that the cursor stays the same but i want it to be that little clicking cursor so all we need to do is come down and give it the cursor property and say we want it to be the pointer so now if we hover over this you'll see that the cursor changes to that hover pointer another thing we could do is we can change the opacity of this so maybe we want to give the background color uh 80 opacity so now we're going to get a different cursor and then it's also going to change color to kind of indicate to the user that they can click this and it'll do something so that is all made possible by this hover sudo class and then if you were to go to the documentation there's all sorts of different pseudo classes that you can use you know here are a couple of these but we're not going to get into a whole lot of detail on that right now now the last thing i want to talk about while we're on the topic of css selectors is kind of differentiate between two that we've already talked about so we've already seen these multiple selectors so let me go back we've got this box example but let me let me bring this back to the multiple selectors where we have box 1 and box 2 being selected and let's go ahead and compare this to something like where we go with box one and then we pass in a p tag or something like that now these are they look very similar but they're very different in this case we're saying i want to target box one and box two and then in the second rule we're saying i want to target any p element so the the actual selector that exists as a descendant of the box one class element so they're very different try to keep those separate in your head as we go through this tutorial now that we've covered how to select html elements with css rules let's talk about this whole cascading concept so css stands for cascading style sheets and what that term refers to is the concept of when you've got multiple style sheets which is possible and multiple css rules within those style sheets that all compete against each other to style the same html element it's asking which one is going to win which one's going to take the most precedence and actually style the element before i start writing a bunch of things i want to show you a tool that we've actually looked at before but we have not looked at yet with css so i know i've been working in firefox previously we're in google chrome just because i've got this google slides up here but we can do the same thing we can inspect elements with the dev tools and when we come to the elements tab which is the first one that you're going to see in all browsers you can parse through the html and you know click through and drill down and figure out which html element that you want to look at and then in this right hand pane over here this is where all of the styles that are applying to that element are going to be housed and in many cases you're going to see multiple styles so let's go ahead and just click on one of these here so i don't know it doesn't really matter but what you're going to see on the right side is a bunch of styles and in some cases you're going to see that they're crossed out and what that basically means is that they have been um canceled out by another style sheet or css rule that takes more precedence over the one that was written so that's what you're going to be seeing all the time with your web pages and i want to talk about how we determine which one takes that precedence let's throw some html on the page here one that has plenty of things to select so we have a div with a paragraph element within it and the div has a class the paragraph element has a class and an id so we've got plenty of ways that we can select this html element and we're going to do so so the first one pretty easy let's go ahead and just use a html tag selector and give it the color red now let's come down and select a class so we're going to select the p element by its class and we're going to give it a color of blue so far we've changed what we're looking at here each time that we've written a rule let's come down and be even a little bit more specific so let's select this by its id by putting a hashtag and typing in the id and then this time we're going to style it a color green so in the first case we tried to style it red then we tried to style it blue and then finally green and as you can see within this document this last rule right here is currently winning it's it has the most precedence and we're going to find out exactly why that's the case in just a second but let's keep going there's actually multiple ways that we can you know select this single element and i'm actually going to paste them all in because i don't want to bore you and waste your time by typing them all out but let's go ahead and paste all of the different ways that we can style this same paragraph element it's pretty pretty wild how many different ways that we can do this so we've got the three that we wrote and then we've got all of these different ways that are all targeting that same element and the question is when we look at all of these how do we know which one is going to end up you know winning and which one's going to apply now i'm sure the question that you're asking is why would this ever happen why would you ever write 10 different rules that are going to style the same element well the answer to that is you probably would not but what could happen is you could have external style sheets that are going to come into your project and they're going to have their opinions on how you know certain elements need to be styled and then you've got your own style sheets locally within the project and those have styles so it's pretty common to see multiple style sheets coming into a single web application and it's really important to understand you know which one's going to win so there are two ways that we can determine this or there's two factors in this equation so let's go ahead and just write this out two factors for determining css precedence all right so the first way is going to be or let's go ahead and make a an ordered list because we're just practicing here so the first way is going to be the location of the css rule in the file and the second one is going to be the specificity of it so the specificity of the css rule now we don't really understand either of these quite yet but the first one is pretty simple and the second one you'll catch on because we talked about it so much earlier in this lesson so let's start with the location piece so location of the css rule in the file this is really what we're referring to when we say cascading style sheets so if we came down here to the css and we targeted list items so these two list items right here is what we're targeting and we want to turn them a different color now i'm going to write this rule right here and then i'm going to take the same exact css rule but just change the color to blue and you'll see that it actually changes the color to blue but it's not quite apparent why the bottom one is going to take precedence over the top one well that's just how css works you always read from the top to bottom and basically the way that i think about it is the the css interpreter that's reading through and figuring out what to apply is going to just read from the top and it's going to say okay i want to style all my list items with a text color of green and then it's going to say okay as long as i don't see anything else in the rest of the style sheet or any other style sheet that comes after it then that's going to be the style that i apply but if i do see something which i do here then i'm going to replace that previous style with the most recent one that i saw when i read it from top to bottom so that's what we're talking about when we say cascading style sheets and that's one way that we can determine precedence with css selectors now i know it seems a little weird because when we worked with javascript you definitely cannot declare the same variables and stuff like that so it seems a little odd that we could put the same exact css selector in one css style sheet but this is totally valid that rule does not apply to css like it does javascript it's a totally different type of you know language that we're dealing with here so we know that the last selector in the file is always going to win unless we have a selector that's higher up but has more specificity so that is the second piece of this equation and we're going to break this out real quick to see how it works so let's say that we have these list items right here and let's go ahead and add a class to both both of them so we'll just say list item something very generic here and we know that now that we have the class we can select it in two different ways so the question i have for you is if we try to select this with its tag so just use the li tag to color the text and then we also added a rule that selected it by class which one is going to win over and then furthermore does it matter which order we put them in in the file before i type this in just pause the video and see if you can kind of reason through that for a second all right so let's try this out first we're going to select it by the tag and we're going to give it a color of green then we're going to select it by its class and we're going to give it a color of blue and you're going to see that it changes to blue but we don't quite know yet why this is the case because in the first rule that we talked about we said okay we read top to bottom and we're always going to take the last rule and apply it so that would make sense here because list items the last rule in the file and it's got a color of blue so that's why it's styled that way well that's actually the wrong answer the reason that it's styled blue is because the second rule is more specific than the first so if we were to take this and put it before this other selector we're still going to see blue text because even though this is last in the file this first one is more specific so the question is how do we know what is more specific than another to figure this out we just need a little visual which says that um on a continuum from the left to right so least important to most important or in other words least specific to most specific we have tags then classes and then ids so you might have picked this up by now but if we were to put an id so let's say list item number one and we try to style this um let's go ahead and put at the top of the file to make sure that we rule out this whole you know first to last uh stuff that applies and now we're going to select it by its id and we're going to give it a color of orange in this case you're going to see that this specific one it's not applying to both because you can't assign an id to two different html elements but when we're looking at this first element we're selecting it uh one time right here another time right here and then a third time right here and this top one despite being at the top of the file is going to be the most specific because of this continuum right here and therefore it's going to be the one that wins out and applies the style to that html element if you're following along intently you're probably asking yet another question and that is what about these combinators that we talked about earlier in the video what happens if i get rid of all these css rules and i come in here and let's say that i select it by just its id and i give it a color of green and then i also come in and i say i want to select let's see i want to go ahead and look in all the ordered list and i want to target all the list items that have an id of list item number one so this is going to be the same exact element that we're selecting so it's going to be the first one right here but let's go ahead and see if we can turn this a different color with this selector right here and we can and even if we take this and we put it at the top of the file we're still going to get the same result it's still going to be blue because this rule right here is more specific than this one right here despite the fact that we're using the most specific selector according to this little continuum so as you can see it gets a little bit tricky here when we start using combinators but there is two ways that we can identify pretty reliably which one's going to win out the first one is probably my favorite which is to just try it out and in this case it's really easy we're using codepen and we can just try it out and see what works but that's not going to give you a really good understanding of why it's working that way so there's another method that we can use where we can actually calculate with numbers the level of specificity of a css rule let me lay out the steps here we'll go ahead and practice some html for a second so let's go after this first list item actually the second list item because we're talking about specificity and let's make another ordered list so the first step that we're going to do is we're going to count the number of id selectors in the css rule so that's step number one then we're going to come down we're going to count the number of class selectors and then finally we'll count the number of tag selectors all right so if we're looking at these rules down here let's go ahead and apply this math and we'll first count the number of id selectors in the css rule so both of these have a single id so they have one id now if we go down to the next one count the number of class selectors they both have zero and then finally the number of tag selectors the first one has one but the second one has zero so the way that this is going to work out we can write some css comments right here and i might as well just go right above the actual rule so if we use this little comment syntax which is a forward slash and a star and then a star and a forward slash to close it out and we can count this up so we'll say id selectors this is going to be one we'll say class selectors it's going to be zero and then tag selectors will be one so if we add these together we get a number of 100 and one if we do the same exact little math problem here let's copy it down to this next rule we can say that the id selectors we have one we have zero class selectors and zero tag selectors so our number is going to be 100. so in this case the second rule has a specificity of 100 and this first one has a specificity of 101 which means it is one point or whatever you want to call it more specific and therefore it will be the one that applies to this element to really nail this concept i'm going to paste in some code so we don't have to type it all out and i'm just going to walk you through a few more calculations all right so we've got a little bit different html but all it is is a paragraph tag with a bunch of ids and classes to give us flexibility on how we select them so in the css i've written several rules that are going to be at different levels of specificity and let's just walk through how each of them are calculated so our first example we have two classes so we get 0 to 0 and our total is 20. come down here we have two ids so this is going to be uh actually should be oh no we're in the wrong spot here so we've got two ids zero classes and zero tag selectors so our specificity is 200 come down to the next one we've got a tag and we've got an id so we have 1 0 1 for 101 and then you get the point we just go down and do the same thing for the rest so the most specific one is the one with two id selectors for a specificity of 200 and you can see that the color orange is what applies to this paragraph element in the web page hopefully at this point it's making sense we know how to select things we know how to determine the order of precedence but there are a few other little gotchas that we need to walk through and one of those is going to be inline styles so if we were to write a paragraph element with some text we can come down to the css and we can style it of course but we also have the option if we give it this style attribute which is just an html attribute as we talked about in a previous lesson we can pass in css style rules into this these little quotations so i can give it a color of blue and then maybe i can give it some padding of 10 pixels and we separate this by these semicolons and we can write as many rules as we want to now the interesting thing about these inline styles is they take precedence over everything else so even if we were to come in here and let's say that we give it an id of you know paragraph or something like that so let's go ahead and select our id and let's give it a text color of orange you would think that because of this css importance diagram that this is going to be the most important rule and the color is going to be orange but obviously it's not it's blue and that's because any inline style is going to take the utmost precedence over all styles now there's one more thing that even makes it a little bit more confusing and that is the important keyword so if we came down into this style right here and we put a space an exclamation mark and we typed important what that's going to do is it's going to override any style from any style sheet and it's basically saying like this rule right here is very important and i don't want to look at any of the precedence rules that we have defined within the css language so even though we've got these inline styles this is still going to apply and even if we came down here and changed this from an id selector to a paragraph selector same deal we're still going to get this orange color because we've defined important on that css property now we can use this important keyword on any property that we want i've been looking at the color property mainly just to stay consistent and avoid confusion before we actually dive into all the different properties but this can be applied to anything else so if you wanted to override the padding style and say 50px or something like that if we put it in just like this not going to change anything but if we put an important at the end you're going to see that this text moves a lot because we've added some padding so that's the whole deal with important but i would recommend that with both of these types of selectors so the the inline selectors and the important selectors use them at a minimum in reality you almost never want to use these inline styles because it just clutters up your html now i've of course used it all the time because it's very quick it's a great way to just quickly put a style on an element and there's actually several css frameworks that do inline css only so you don't even have separate style sheets you just all you write it all in the html document so it's kind of a contentious topic among different camps but in general you probably want to stay away from these inline styles for now just because it's going to clutter up your code likewise with the important style i think this is a more unanimous viewpoint you should not be using this important keyword unless you absolutely have to and the only um example of this that i guess i've experienced that's a really good reason to use this important keyword is if you've got multiple style sheets so maybe you're using a css framework or a ui kit library that's got a bunch of like buttons and modals and stuff like that those are going to have predefined css styles and if you want to override them often you have to use the important keyword otherwise there's no other way to select them with enough specificity to override the existing styles so once again use the important keyword only when you have to pretty good rule of thumb to stick with for the last hour or so or however long i've been talking here we haven't really talked about css properties yet we've been using the color property among a couple others but there's hundreds of them that we can possibly use to both position the elements on the page and style them so remember css is all about layout and style and there's different properties that allow us to do those two different things so to start us off let's just see a super basic example something that you might have to do pretty often and that is let's go ahead and take a a div put some text in it and then style that as a box with specific dimensions maybe a background color and a border or something like that so here's how i'd approach that with css so first you have to make the div then you have to put some text in there so some text and i guess what we want to do is first get that background on there so i'll just select the div since there's only a couple elements here and we'll give it a background color of i don't know we'll call it navy we'll give the text color white we want to give that div a width of say 200 pixels and a height of also 200 pixels so we've got us ourselves a square with some text in it but maybe we want to put that text in the middle of that square so we can do that that would be kind of a layout or a positioning type property so one way we could do this pretty straightforward is going to be add a line height and set that equal to 200 pixels which is the same height as the square and then maybe we want to just set the text alignment to center so that's going to bring it to the middle of the square and then we can furthermore put on maybe a box shadow something like that so let's just write something out really quickly and you'll see what happens as we get a little shadow showing on this box and then maybe we could even i don't know we could put a border on it or something so we could say border one pixel solid i don't know something like green it's not gonna look great but you see that we have maybe we can put five pixels instead of one to make it more visible and now you can see that we have a green border on there so this is just a super basic example but you can see how we use some style properties and we use some positioning properties and this is actually not perfectly centered anymore and we'll talk about exactly why that's happening when we get into the concept of the box model but let's get rid of this and ask the question that you're probably wondering and that is how do we know what css properties to actually use i just went through that example really quickly and i had these on the top of my head because i've just been doing it for a while but when i first started i would have no idea how to do what i just did and i have to look up every single property that i was using and furthermore i'd have to look up like if i wanted to do something i'd have to type into google how do i center a paragraph element within a div or something like that so it's really difficult without experience to know what properties to use and when and there's really no great solution to that other than practicing and really you know just applying these concepts that we're learning in this crash course in my opinion the best way to understand the box model better is to just look at it interactively and a great tool to use to do this is the firefox dev tools so we've talked about this in prior lessons but we're coming back here and as you can see i've got this html document here in the browser and then what i've done is i've selected the content element that i've just made a single div and i've put some text in that div and what i've done is i've applied these five uh styles that are pretty directly related to the box model to see how these play out you know on an actual web page so as you can see if we go to the layout section of the dev tools so we're up in inspector we go to layout and then we can scroll down to see the box model and this gives us an incredibly interactive way to see what each of these represent so if you go through and you want to see how you know why does the content and how tall is it you come into these little properties and you can hover over them and you see on the left side of the screen it gives you an outline of exactly what that is applying to furthermore we can see that we've got some padding in here and again it's highlighted up on the screen it gives you a really nice outline of where that padding sits furthermore we've got a border it outlines that really nicely for us and then even some margin on the outside now there are other tools that help you visualize this like in google chrome so if we came back to google chrome you can see that if we're just looking at it i don't know let's go ahead and just select one of these elements here then you can come down to the box model and you can kind of see what's going on but in my opinion it's just not quite as good as firefox so that's why we're working in firefox in the first place but tools aside what actually is the box model well the box model represents these pieces that we're looking at but more specifically you have the content itself which is going to be you know text or just some space that we've defined by a width and a height and then outside of that content we then have some sort of padding or like it doesn't have to be set it could be zero padding but in this case i defined 20 pixels of padding and then outside of that we have a border and then outside of that we have a margin and within this box model i mentioned that it tells us how much space an html element represents within the web page and the properties that play into that are going to be the width the height the padding and the border now the margin is a little bit different because while it does take up space it doesn't actually define the space taken up by that individual element it kind of adds like some space around the element um so that if you have two elements and they're squashed together you can just kind of space them out a little bit so those are the components of the box model let's go ahead and dive in with some i actually made a code pen that's going to allow us to interactively play around with this box model so on my screen i've got a code pen that's i've got some javascript written up in here you can drop down these uh select elements and select different css properties to apply to this orange paragraph tag up here and why i created this is because when we're talking about the box model sure we can get into width height padding border margin all that kind of stuff but what's really confusing about the box model in css in general is that based on the values that you're passing to certain properties the rules of the game completely change and to highlight some of those rules i thought it would be helpful to put together this example and just visually walk you through like if we switch a certain css property what is that going to do to our html element that we're targeting the first property that i want to really touch on and dig into is the display pop property so this is a related property to the box model and once again the box model is not just one thing but this is highly related to what we're talking about here and if we scroll down to our documentation i've just gone to mdn technologies css the reference and scroll down to the index and we can find the display property somewhere in here so let's scroll down see if we can find it here's the d section and we'll go ahead and click display so it says this property sets whether an element is treated as a block or inline element in the layout used for its children such as flow layout grid or flex so there is a lot packed into this single property and honestly it's probably the most probably the most complex property that you'll deal with when you're writing css but we're going to dig into each piece of it and you're not going to have to read all this documentation i'm just going to walk you through it so you can see that there's a couple of or there's there's a lot of different things that you can pass to this property but the main ones are going to be block inline inline block flex and grid so if we came back to our box model example if we came down to the display type right here and did the drop down you can see that we have all of these different possible values excluding flex and grid and the reason we're excluding those is because flexbox and css grid are going to be totally separate videos that are going to require a lot more explanation so for now forget about those we're just going to focus on these ones right here so by default whenever you're dealing with most html elements you're going to have a display property of block but at this point doesn't mean anything to us like what does block mean what does inline mean since flexbox and css grid are kind of out of the equation here we're just focusing on block inline and inline block display types and based on what you set your elements they're going to interact with the page and flow through the web page a lot differently so it kind of changes the rules of the game now by default each element is going to have a certain type of display now most elements are going to have a display type of block and you can see in this example that the default display type of a p element is block but if you were to look at another type of element so if you come up to the html that we've got here it's a little bit messy but this span element which is represented by this green text that's actually going to be by default an inline element so the question is how do we know which elements are going to be by default a block and which ones are going to be by default inline and then furthermore what do they actually do what what's the difference between a block and inline element to understand this we've got this example laid out so watch what happens when i switch a block element to an inline element so i'll drop this down i'll say inline and you'll see that that orange div is going to jump up to the line before it and it's going to share that same space with the span element now that's the behavior of an inline html element so what you'll see here is that since a div element has a natural display type of block what it's going to do is occupy a hundred percent of its container space and in this case the container is 100 percent of the body html tag so it's occupying all of that space and furthermore the height if we don't give it a height specify it explicitly it's going to automatically take up the height of the content within it so however large this text is is how big of a height this element's going to occupy the second thing about block elements is that these elements occupy their own space they don't share lines with other elements so as you can see here our div in orange with the orange background occupying all the space and it's also on a different line than this green span element that's right above it and the span element actually has a default value of inline rather than block so different rules up there but since this one is a block then no other element can occupy the same space as it the next thing about a block element is that it respects its width and height properties so that doesn't make a whole lot of sense yet but you will see what i'm talking about in just a second so what i'm going to do is i'm going to drop down the content width and this is just going to apply a 400 pixel width to the orange element so you can see that it no longer takes the entire width of its container because we've explicitly given it a width and then furthermore we can give it a height of 200px or pixels so now it's occupying a space of 400 by 200. so what happens if i were to do the same thing with an inline element let's go ahead and see so i'm going to reset all of the properties so we're going back to the beginning and now what i'm going to do is come down here and set our div to be a display type of inline and you can see when i click that it comes up onto the previous line because inline elements actually can share space they don't break to a new line by default you'll also notice that they don't occupy all of the space they just occupy the size of the content within them so it's just occupying the text that's within this div because we've set it to that display type of inline now the last interesting thing is that unlike a block element that obeys or adheres to its width and height properties the inline element is not going to so even if we set the content width to 400 pixels not going to change at all we can also try that with the height set it to 200 pixels not going to work so in other words if you set it to inline width and height css properties are not going to work on it so as you can see depending on you know one property's value you have different rules of css which can get pretty confusing sometimes and it's why i'm spending so much time on it here now the last one that we have not talked about is a combination between the two so just to review a block element it's going to take all the space of its container it's going to take a height of the content within it and it's going to break to a new line and respect its width and height properties an inline element is going to just be the width and height of the content within it it's not going to break to a new line and it's also not going to respect the width and height properties if you try to set them now an inline block kind of adopts a couple attributes of both so the the main difference if we were to switch this inline to inline block then the only difference is now it's going to respect its width and height properties right now we don't have those or i guess we do have those set we have tried to set the content with in the content height but because we have an inline element right now it's not respecting that those dimensions but the second we change this to inline block we're going to get those width and height properties back just like we saw with the block but in this case we can still make it inline so although it's broken to a new line here that just happens to be the case because it's too large to fit on that line above so inline block just basically takes all the properties of inline but instead of ignoring width and height it now respects those properties and takes that space up this concept may not seem super important to you right now but i promise you as you go through knowing whether you're dealing with a block element or inline or inline block is going to be super important and then further down the road when we start talking about flexbox and css grid it becomes even more important to understand this kind of stuff let's go ahead and reset everything here and the next thing that we need to talk about are box model variations so to this point we just looked at the firefox dev tools and you can visualize the box model pretty well but there's actually another way of you know conceptualizing this box model and using it just like the display property and all other css properties have their default values the box type down here is going to have a default value of content box so that's what we're seeing right here but we can switch this to be something called border box which is basically going to change how all of those properties so width height padding border and margin it's going to change how those relate to each other within the box model to understand this let's go back to our firefox dev tools and let's go ahead and calculate the total size or space that this particular element is taking up so the way that we would do this is by adding things together because the default box type is content box so basically what we have to do is if if we wanted to find the total height of this element over here we could of course just hover over it and we can see exactly you know what the height is but if we wanted to calculate this we just come down into the box model and we say okay what is the height of the content well the height of the content is going to be a hundred pixels so we say a hundred and then we add on whatever the padding is on both sides so we add the top padding and the bottom padding which on both sides is going to be 20 pixels so we go 20 plus 20 is 40 plus the content height of a 100 is 140 and then on top of that we add the border dimensions which is going to be 5 pixels on the top in the bottom so in total the total height or space and height that this occupies on the web page is going to be 100 plus 20 plus 20 plus 5 plus five so that equals i guess 140 150 in total and then the margin the additional 20 and 20 or 40 pixels that's not actually going to play into the box model that's kind of separate from it it doesn't actually indicate how the total height of this element so let's go ahead and see if we can hover over this and you'll see that if we hover over the entire element it's showing in pretty small text that the height is 150 just like we calculated so again 100 plus 20 20 5 and 5 is our total height and then likewise we can do that with the width so we grab our content width which is 400 we add both of the paddings on the left and the right which is 40 so we got 440 and then we add the uh border which is 5 and 5 so we got 440 plus 10 is 450 and that's when we hover over it is what the width is going to be so that calculation that we did seems awfully complicated and it kind of is because we're having to add all these different components and consider okay what's the left padding what's the right padding what's the left and right border you know it would be a lot easier if we could just define i want this element to be 400 pixels wide and 200 high and be done with it and then have all the rest of the padding and border and stuff just be within it and that is what the border box value of the box type property allows us to do so all we have to do is come to the styles of the element and then we say box type and we say we want border box now if we apply that it's not going to work because let me see why invalid property name oh i must be blanking on what we're actually using here let me let me see what we're talking about here so box type so i was totally blanking out it's not it's not box type it is box sizing that box type is just what i named it in our code pen but if you say box sizing equals border box you'll see that this changes just a little bit so let me go ahead and uncheck this and watch the content up in the left top left you'll see that something is going on here and it's really hard to tell what that something is when we just have one element on the page but what i've done is put together another code pen that really helps us visualize the difference between the border box and the content box now stick with me here because i know it seems a little bit boring and why would we need to worry about box sizing but when we start making grids and we start you know putting elements within other elements it's going to become really important to know you know how much width do we need to give to children element to fit within the parent container so on and so forth so let's go ahead and dig into that example that i created and you'll see exactly what i mean so as i mentioned when we flip that box sizing from a content box to border box it basically takes the calculation of the space that html is occupying and it changes it so now instead of taking the content height plus the padding plus the border to get our total height we just look at the height property and that is our total height and it's a lot easier to kind of reconcile in our brains and furthermore it makes it a lot easier to put elements within other elements so as you see on the screen here what we're looking at is a couple of divs and i've got a child div and a parent div and what i'm trying to do is i'm trying to fit the child div within the parent and not have it break out of its space and if you see on the right side where we've got the output of this you can see that the content box we've got the parent container which has just a that's just a picture background that's not css um but that's the space that it's occupying and you can see that the child with that border on it because border you know has an impact on the total size of a content box you can see that by adding that border we're breaking out of that parent container and that's really going to be a problem when we're trying to arrange elements on the page so furthermore if we were to add padding to this so basically what this button is going to do when we click it is it's going to take the child element which is the one with the border on it and it's going to add some padding to it so when we click this you'll see that it makes it break out of our parent container even more and that's because when we add padding or a border to something that has a box sizing of content box it's going to add space to that element it's it's always going to be additive now when we come down to a border box type element it's not going to so what's going to happen is the the content width and height is going to be adjusted based on how much border and padding there is so you can see from the start we're already fitting the child element which is got the black border right on top of the parent container and if we add some padding instead of getting larger it's just going to make the content within it a little bit smaller so let's toggle the padding and you'll see that this text right here is going to come inwards a little bit but we're not going to see the the border break out of that parent container so i understand this is a little bit confusing especially if you're just starting out in css but i really wanted to introduce this concept because you will stumble upon this down the road and you'll probably see like i said something like a css reset so let me let me go ahead and grab that code that i'm talking about and show you exactly what i mean oftentimes you'll see in a css style sheet the following thing and this is going to change the stuff over on the right a little bit but that's not the point what you'll see is that on the html element or the root element they're going to change the box sizing to border box so it's going to take the root style and make it you know this nice little handy border box where you know the width is the width and the height is the height and there's no math going on there and then these next couple of rules similar to what we talked about where if you put a comma there it will select multiple you know classes or ids or tags or whatever that's what these are doing and this is a wild card character that's basically saying i want to target every element in this entire document and then what it's doing is it's saying i want to set the box sizing property to inherit which is basically basically going to look up and say okay what is the root element set to whatever that's set to inherit that down to all of the different elements on this web page so in effect what we call this is a css reset and it's going to change the default box sizing to this more easy to understand border box you know box sizing all right i know we've covered a lot we've come a long way in this crash course so far we're not done yet but i want to do a quick little review on a few things so we've learned about how do we select things how do we select html elements we've learned about what takes precedence when we have multiple css rules and we've learned about the box model and what i want to review here is the box model just go through a few things that you should keep on the top of your mind so first off the box model describes how much space an html element occupies the space is heavily influenced by that box sizing property that we just talked about whether that's content box or border box so content box you have to add the content width and height to the padding in the border well with the border box you just take the width and the height and that is the total space that your html element occupies so the space is also or space and layout is also heavily influenced by the display property so whether we have a block element an inline element or an inline block element is going to influence where those elements are sitting on the page so those two properties box sizing and display are going to really play together deciding how our web page is laid out and furthermore that display property has two additional values that we have not talked about but will so flexbox and css grid which is going to allow us to make grids and it's going to allow us to space elements out really well so that's going to be super useful to us in the future and finally the most important components of the box model are height width padding border and margin margin doesn't play into the total space it occupies but it does control the space around it and then finally all other css properties are going to be kind of supplemental to these core concepts so whether we're styling the text color or we're doing a background image or making a different font those are all going to be just kind of supplementary to these core concepts what i would recommend is that you come to this uh little example that i put together it's in the description and just click through all of these uh attributes and or not attributes but css properties and see what happens with different combinations so change the display type to inline and then try to set you know different margins and paddings and stuff like that and you'll see like okay the margin doesn't really do anything when it's in line but the padding does and then if we set this to block and then change the margin then it is going to do something aside from the display property and the box sizing and a couple other things we just talked about there's one other css property that you'll use pretty often and it will affect the flow of the elements on the page and that's going to be the position property so this position property has i think about four or five different possibilities so let's go ahead and write those out we need to make an unordered list and then put some list items in let me get my notes here so we can have a value of static we can have a value of absolute we can have a value of relative and then we can have fixed and the last one is going to be sticky so when i'm talking about this position property all i'm saying is that if we came down and targeted this unordered list and then we gave it a position we can say we want it to be fixed but what does this actually mean well this position property is going to be really handy when we have to lay out things on the page and like make things like nav bars so that's what i'm going to go ahead and show you is an example of how we might make a nav bar and show how that will stay at the top of the page rather than scrolling down when we have content that exceeds the total height of the viewport or the web page that we're looking at a few notes i want to make this first one the static is going to be the default value so if we came down here and defined the position of this unordered list to be static that's kind of redundant because it's already going to be that by default and then the other node i want to make is the sticky property value is going to be a little bit newer it's unsupported in some browsers and i'm not really going to touch on it because we can achieve very similar things with this fixed property all right so those are the possible values let's walk through each of them and see what they actually do for our html so the first thing i want to do is create a nav element so we talked about semantic html in the prior lesson how we want to use what the intent of that element is and if we want to make a nav bar we should use the nav html tag so within that nav we can make some sort of list which would be kind of a list of different links or something but just to keep it simple i'm just going to put in some sort of heading so we'll just put in an h2 element and we'll say this is the nav bar so you're going to see that pop up on the page and it's nothing special at the moment and actually let's go ahead let's make this an h4 just to be a little bit smaller and what we want to do in the css is we want to target that nav element we want to give it a background color of blue we're going to talk about those background styling properties so bear with me here well we don't know about them and then i want to make the height of this equal to we'll call it 60 pixels so you can see we have some sort of nav bar sitting up on the page and i actually don't like this blue let's go ahead and do navy and then change the color of the text to white just to make it look a little bit better and what you'll see is there's some space going on around this we would you know ideally want this to be at the top of the page you know have no white space around it and that's happening for two reasons by default this h4 element is going to have some sort of margin around it it's just a browser default that is set on this element we didn't set it but it's already there likewise codepen has a default that puts some either margin or padding within the body element so anything that we write in this html is going to have a little bit of padding in there so that you know we have this white space so all we have to do is just cancel that out by writing a css rule so we'll start with h4 and we'll just say we want zero margin on there see what that does remove some of the margin and then we want to come down and say on the body we want to have zero margin i don't yeah i guess that's all we needed we just need to zero out the margin and now there's no space around this so looking good there and if we wanted to center this text within the nav bar we can use a little trick um we can use the line height property to basically say okay i want this text to be you know x amount of pixels tall and in this case we know exactly how tall the nav bar is 60 pixels so what we need to do with this h4 is make the line height equal to 60 pixels and now we're going to see that go right in the middle and the last thing we'll do is on the left side of that h4 element let's just put some padding on the left of say 10 pixels and that's going to push it out a little bit so i know we haven't talked about a lot of these properties we will in a second but you can see how this is the snav bar is coming together but the problem is right now if we were to scroll down on this page the navbar is going to kind of go away and we don't want that since there's no other elements when i scroll you're not going to see anything so let's go ahead and add a div that has a bunch of boxes in here so we'll say we want to div with a class of box and then let's replicate that several times down and then we'll come down here and style all the classes of box and what we're going to do is we're going to give it a width of 200 pixels and a height of 200 pixels and let's go ahead and give it a background color of i don't know green is fine for now you're going to see just big one big blob because they're all kind of smooshed together but all we have to do is add some margin around it and those are going to be separated out so the reason that i created those is just to give us more than enough content so that we can actually see some scrolling action so now you'll see the scroll bar here on the right because we've got too much content for the visible screen and when we scroll down what happens we're not going to see that nav bar anymore and as you're probably used to with most sites the navbar needs to stay up top and the way that we do that is by using the position property and we can just come into the nav bar right here and we can say that we want the position to be fixed and what that's going to do is a lot of things that we don't expect and it's going to look a little bit intimidating for a second until we figure out exactly what's going on so when we put it to fixed it actually takes that element out of the the flow of the html document so previously we talked about how we have block elements and inline elements and if we change those you know it kind of breaks them out of the flow of the content likewise with the position fixed property now what's going to happen is we have to explicitly set the width of this so let's go ahead and set the width to be 100 of its container and that should span it across the entire web page here another way that we could do that is a property called 100 viewport width so vw and that is actually the same thing if we just came over to let's come over to our devtools in firefox and if you remember we could say window dot inner height and get some sort of value here and this means that the height of the visible window here is 966 pixels we can also get the inner width right here which is 773 and if we were to you know change this right here and run it again it's going to be a different value because the visible width of this viewport changed so these properties we know how to get them in javascript through the dom but we can also get them within css by using either 100 viewport width or 100 viewport height so vh so that is just a really useful thing to know i use it all the time especially when i'm making full page layouts and nav bars and stuff like that now the last thing about a position fixed element is that you have to specify the offset that you have to its parent container now in this case the parent container of our nav is going to be the body element and that's going to be basically this visible screen right here so all we have to do is come down and say from the top i want it to be zero pixels and that's gonna pull it up to the top and then since we have it as a hundred percent or a hundred percent width we don't really need to define this other one but let's go ahead and say we want it to be zero pixels from the left so at this point what we can see is that as we scroll down our nav bar stays at the top and it's considered fixed positioning the last thing that we would probably want to do is address this issue right here our boxes or our main content has now spilled under our fixed nav bar and the reason is because this fixed nav bar doesn't take up any space so unlike a block element or even an inline element that takes up some sort of space to the html document this nav bar occupies zero space whatsoever so to make sure that the main content is below it at all times we have to give a margin on the top of this boxes div class so we come down to the css give it target that class and then we say we want to give the margin top probably should be a little more than 60 pixels so let's just go ahead and say 80 pixels and that should bring all of our boxes down and now it's working just fine so that is the fixed positioning and it's going to be really useful when you're trying to make a nav bar at the top even at the side on the right side or even a footer at the bottom of the page that's a really good use case for this the last couple position uh property values that we can use so absolute and relative positioning again we're not talking about static because that's the default and sticky because that's kind of out of the scope here but when we're talking about a fixed positioning what we just looked at that's going to set the element it's going to remove it from the flow of the document and take up zero space and it's going to be positioned relative to the viewport which is always going to be the edges of the browser window so as you saw we said we want it to be zero pixels from the top so at the very top of the viewport and zero pixels from the left so the very left of the viewport now with relative and absolute positioning it's somewhat similar we're still pulling things out of the flow or at least with absolute positioning we're pulling them out of the flow of the document but we're positioning them relative to other elements so let's play around with relative positioning first so let me target this first box right here and what i want to do is i want to give it another class called rel position just to give it some indication of what it's doing and then within this i want to make another div and i want to say this will be absolute position we're not going to do anything quite yet with that but i'll show you what it does in just a second so let's focus in on this relatively position box which is this first green box that we're targeting we'll come down into our css and all we're going to do is give this relative position class a relative position and what you're going to see here is nothing has happened yet but what happens when we scroll is now this first box goes on top of our nav bar so only the first box though the rest of them go behind it and that is a direct result of setting that relative positioning on the element because what that does like i said with a lot of css properties setting one value will kind of change the rules of the game it's no different here what this does is enables a new property called z index which basically says okay we've got all these elements on the page what is the z index you know three-dimensional rather than two-dimensional so which one sits on top of another and when you put relative positioning or absolute positioning on an element it now has the z index so now what we have to do is define what needs to be on top of another so we need to come up to the nav bar and give it a z index of 1 to put it on top of this element once again so that's something really important to know about the relative positioning but what relative positioning really does is it as you saw it didn't really change the actual position of that box but now what we can do is define these top left right and bottom properties on this element and it will change its position relative to itself so that's kind of a confusing topic but basically the way you can think of it is if that green box is sitting right there right now if we give it a top value and we say 20 pixels it's going to go down 20 pixels and confusingly enough since our i believe our margin was 20 pixels exactly it's going to blend it into the other boxes so let's let's go ahead and make this 10 so that we see some separation still but it basically takes the original position and if we add the top property it will go 10 pixels below where it was to begin with so it's a little bit confusing but it is a useful type of positioning especially when you have some stubborn elements in your html that you just need to get to move just a little bit but you can't quite do it with normal css properties and finally if you set the rel the position to relative on a parent element then you can set absolute positioning on the element within that parent element so what we can do is we can look at this div that we put within the relatively positioned div and we can take this class and target it with our css so we'll target the absolutely positioned element which is not showing up yet but let's go ahead and give it a width of 20 pixels and a height of 20 pixels and give it a background color of say orange so we're going to have all sorts of nasty colors here on the screen and what you'll see is that by default it's going to sit up there in the top left corner of its parent and that's because we haven't positioned it yet so let's give it the absolute positioning and what that's going to do is now we have those top uh bottom right and left offsets enabled on this specific element but instead of being an offset based on the viewport so with this fixed nav bar whatever we pass to those properties is going to be in relation to the edges of your viewport now we're going to be setting these top right bottom left offsets based on the position of the next highest element that has relative or absolute positioning and if there's nothing above it that has those positionings then it's going to use the html element or the root element so in this case we have something that is relatively positioned as the direct parent which is this green box so if we were to come down to the absolute position and say we want to put it 20 pixels from the top it's just moving it down 20 pixels relative to the edges of this parent container and then we can put it i don't know we can say from the right we want it to be 30 pixels so it's going to pull it over to the right and offset it 30 pixels off of that right edge so that's what the absolute positioning does and it has to be used in relation with that relative positioning once again if you don't have an apparent element that is relatively positioned so let's go ahead and remove this position relative and now it completely disappears and you probably are asking yourself where did this just go and the answer is it's i'm thinking it's probably behind the nav bar so let's go ahead and give it a z index of 2 which is greater than the navbar which has an index of 1 and now you can see it pop up up here in the nav bar and the reason it's up there is because since we don't have any relatively positioned elements as its parent or you know grandparent it's positioning itself relative to the viewport just like we saw with the fixed positioning so now what we can say if we look at this absolutely positioned element if we change the top to be zero and the right to be also zero it's going to be perfectly up in the top right corner as you can see here in the screen as you can see these position properties can really help you control where the elements sit on the page and they're really powerful layout properties obviously you're not going to be a master of them after just watching this brief tutorial or demo but hopefully it exposed you to what's possible and when you stumble upon it in the future or in the code challenges you'll know exactly what's going on if i'm being honest we've made it through the hardest part of css in the first part of this course the remainder of this video and this crash course is going to be a little bit more fun a little bit more straightforward and you're going to really start to see the magic of css coming into play so in the remainder we're going to talk a little bit about css units so we've looked at them already but pixels em rem units percentages maybe a couple others that actually that's probably it and then we're also going to look at color codes and different color stylings so we've been using the color property to change the text color throughout this tutorial but we're going to get into you know how do we define different colors and you know how do we do color themes and stuff like that and then we'll dig into font properties so how do we change the font that's on the page and different font families and import fonts and stuff like that and then finally we will talk about background properties so how do we make an image as the background of your web page and then at the end i'm going to talk about writing shorthand css so that's going to bring a few things together that you may catch on to as we write it but that'll be pretty interesting and then to cap it off i'm going to give you my kind of four-step system of how i think through putting together html and then styling that with css effectively so it's basically like get the layout first then put the you know styles on and you know iterate over that process before we get into the remainder of the css properties some common ones you'll use we have to talk about measurement units within css so you can see them on the screen here pixels pretty straightforward and common em and rem those are font units uh like measuring how large the font on the screen is and then percentages that's going to really play into some responsive design that we'll talk about in a later video so we'll start with pixels and it's you've probably already looked it up seen me using it it's relatively intuitive but your computer screen is made up of pixels and the higher resolution your computer screen is the more pixels you can cram into it so there's it gets into a pretty complex discussion when you actually start thinking about it too much when you're looking at a web page and trying to calculate how many pixels exist on that web page but if you just look at it as a way to explicitly set the dimensions of html elements it's pretty simple so for example you saw that we made this little orange box in the top right corner we set it equal to a width and height of 20 pixels so that's obviously somewhat small and something that i wondered when i was first starting out is how do i get a good grasp of how many pixels to assign to certain types of elements and it's kind of like riding a bike like as you go through and write more and more css you'll get a feel for what are kind of the average pixel units that you'll be assigning to different types of things so for example i know that whenever i set padding or margin it's usually going to be in the range of 10 to 20 pixels when i set a border radius it's going to be in the range of 4 to 7 pixels and so on and so forth you kind of get a feel for how large um a pixel is after you've written css for a while next up i'm gonna skip down away from those font uh units so talking about em and rem i'm gonna skip those for a second and talk about percentages now percentages are kind of the preferred way in a lot of cases to define the width and height of certain elements on the page because when you go from a desktop to a mobile device you're going to need relative dimensions rather than just saying oh i want this element to be 500 pixels so a good example of this let me just take what i've got here on the screen and i'll go ahead and restyle this first box so it's the relatively positioned box i'll leave it how it is and i'm just going to say that i want the width to be 600 pixels and what it's going to do is it's going to be wider now and the problem here is that when i resize this screen and we go to a mobile device it's going to get cut off so as i make this smaller and smaller you're going to see that that box just goes off the screen completely and the problem with that is you know you want to have responsive designs so a better way to do this is to say i want the width to be i don't know we'll call it 80 percent of the container so as we resize this you're going to see that that always stays 80 percent of the container that it's within and that's a great way to write responsive web pages and again we're going to be talking a lot more about that responsive design in a separate video but that's why percentages matter in the first place now something about percentages that can get a little bit confusing is it's all relative to its container so let's go ahead and apply a few concepts we've learned so the question is how do we get this little orange box back in the green box so that we can play around with this well all we have to do is set the position back to relative so this orange box is going to now be relative to the green box so when i uncomment this property right here it's going to put this orange box in the top right corner of the green box so at least i think that's what's going to happen and that's exactly what happened so we now have that there and now what i'm going to do is go ahead and reposition this so instead of being in the top right we're going to put it in the top left so we'll put in the top left and now what we're going to do is give it a width in terms of percentages rather than pixels so what happens if i give it a width of 100 well now it spans the entire width of its parent element not the entire web page because it exists within that parent element so it's going to occupy the width of the parent element in other words if we were to set i'm just going to do this to make the math a little bit more simple but let's go ahead and set this green box equal to say 500 pixels so the calculation for this orange element is going to be 500 pixels times 100 is equal to 500 pixels and if we were to change this positioning to call it 80 now it's going to be 80 of 500 which is i guess 4 400 pixels yeah about 400 pixels is what this is now so you can see it's relative to its container not relative to the entire web page when we define percentages we've talked about pixels and percentages now let's talk about em and rem units so these are specific to font sizes and we haven't really talked about fonts yet but we have looked at text on the screen so why don't i go ahead and put some text within our absolutely positioned div so now we've got some text here and we can change the size of that text using the font size property we'll talk about that a little bit later but if we came down into our absolutely positioned element and we give it a font size we can say it can be 16 pixels which is generally the standard font size of a paragraph element so you won't see it change because that's probably the default that it already is but if we went and changed that to say 18 pixels you'll see that the text gets a little bit larger now when we're talking about em in rem units that's kind of the more preferred way to define text sizes within a web page and the reason being is if you're going around hunting and pecking for you know trying to find all of the different css rules that you've defined font sizes in it gets pretty difficult you want to define it at one level so at the top level of the document and then everything else needs to be relative to that assignment so what you'll generally see with font sizes is you'll see the html element so if we target the root element itself and we give it a font size you'll generally see it be set to about 16 pixels from there you will then define elements below that root element in relation to that 16 pixels so if i came down to the absolute positioned element so this text right here and i said that i want the font size to be one rem that means that it's going to be 100 of the size of the text in the root element or rem so in this case the root element is html and the font size is 16 so if we define it as one rem then that means that we're doing 100 of 16 pixels or in other words 16 pixels and if we change this to say 1.2 rem that would be 120 of 16 pixels which i don't think i can do that in my head right now so you figure that out with the calculator on your own but basically it makes it larger than the default font size likewise we can also use em units and that basically means relative to its parent element so if we have this relatively positioned element which is the green box right here and we set the font size right here to 16 or no we'll go ahead and say something a lot larger so that we can distinguish this so the relatively positioned element is going to have a font size of 24 pixels and then if we came down and we said we want one e m in the child element it's gonna set it exactly equal to this 24 pixels but if we change this back to rem units now it's going to go back to the 16 pixels because now it's relative to the html declaration which at the top is a font size of 16 pixels so as you can see the benefit of doing this and using these em and rem units is you can define it one time and then everything else is relative to that so let's say that you have your web app and you would originally define it to 14 pixels and then you start using the app and your users start complaining oh everything's too small instead of going and changing every css rule all you have to do is go up to the root element change that and then everything else is relative to that so that would be the benefit of using em in rem units speaking of fonts let's go ahead and see how we can set custom fonts on our html so i'm going to get rid of all this because all these colors are driving me nuts they're just so ugly i can't can't deal with them anymore so let's get a clean slate so let's go ahead and put a heading element and some paragraph text and then maybe one more um we'll call this the original style text and what we'll do here is this will be the original id and what we're going to style this as is we'll we'll put these two as a new font style and then we'll keep this one as the original just as a reference point when we're dealing with fonts in my head at least there's about four properties that are highly relevant that you have to know the first font property that we're dealing with is the font family which is going to define what all of the text on your web page in your web app what that's going to inherit and especially if you define it on the body or even the html so the the whole document you can define the font family and that's going to apply to everything within the document unless something is of course overwritten with a font family on a you know more specific element but let's go to the documentation i just searched for the font family property you could also go to technology css reference come down to the index and then somewhere in the f here we can find the font family looks like it might be at the very bottom though okay so here's the font family so that's how you get there and as you can see we are giving two values to this or you don't have to but in most cases you're going to give two values to this the first one is going to be the font family that you're wanting to specify and there's a bunch of built-in font families that you can use let me just see what we can find default font families there's probably a resource i think this is the one i clicked on before so w3.org it looks like this defines a lot of those default families that are available in all your browsers so this first one is going to represent the family and then the second one or the second argument we will call it to this property is going to be the fallback font so there's only a couple of valid fallback fonts and they're literally just like the type of font so everyone knows like a serif and a sans-serif font and then like there's a mono space font which is more like a typewriter type thing so those will be fallbacks for if the browser doesn't support the first one that you define but let's just go ahead and look in at an example in most cases you're just going to put them in quotes that first one because it'll be two words so let's copy this one in that's a good example so we'll put that in there and you'll see how this changes and i can comment it out you'll see it change and then uncomment it and you'll see the change of this font now the question is what happens if we want something more custom what if we want like a really nice looking professional font well there's a couple ways that you can do this in css and a couple of them i'm not going to walk through but the easiest way is to go to google fonts so let's open up google fonts and go here and then from from this page we can search through all different categories of fonts so let's say that we want only serif fonts and then we can go down to oh i guess different languages that they're available in and different font properties so let's scroll down and just pick something um see if i can find one that might be a little bit popular i believe you can sort by trending or most popular so maybe we look at roboto rubato slab all right so that's a good one and from here you just go down to all of these different font weights and styles and you click select this style so in this in pretty much every case you're going to select the regular so we'll click select that style and what you'll notice is that in this link that we're going to use in just a second it looks like it's just pulling in a default link but the second we add in a different weight so let's say that we want the boldest version of this font and we click select style now you're going to see that there's this little parameter in here that defines that we want the weight of 400 and 900 and each additional weight that you add is going to add a little bit more size to this import so you have to be careful with that but for the most part grab a couple and you should be good so in this case we have a regular font and we have a very bold font and all we have to do is take this link tag which is a valid html tag that we had talked about earlier and that's actually how you import style sheets to an html document and we'll come back to our codepen playground and come up to settings um i believe html and then in this section here stuff for the head we can just paste in those link tags and now those fonts have been imported into our document and we can access them with css now this is a little bit different than how you would do it if you were writing outside of the code pen environment so just for a second i'm going to comment out all of this html and show you exactly how you would do this on your own so you're going to need an html a body and then of course the head so this is our basic structure of an html document and then you'll of course have a link to your styles.css or something like that so this will be representing your stylesheet not here but in a different document it would and then you can also copy in those link tags that we imported from google fonts so this is how you would basically import a font to a custom html document that's outside of this you know nicely programmed codepen environment so anyways let's get rid of that we don't need that we'll uncomment or not uncomment okay so we're back to the start and now all we have to do is replace these two lines with the imported font if you go back to roboto or roboto slab you're going to see that the css rules to specify this family is right here so we can just copy that come back here and replace this whole line and now you watch the text change to this new font so that's how you would import a custom font it's pretty much how i always do it for a lot of my web pages and apps is use google fonts it's free and very easy to use so that is the first and most important css property when we're dealing with fonts we'll go ahead and give a font size of 16 pixels because we're on the root html element and that is a very standard value to be giving an entire web page now what gets interesting is the font weight property so font weight determines the thickness of that font but the interesting thing about this one is you can only use the weights that you've imported if you're using something like google fonts so if you remember when we were looking at this we only imported a weight of 400 and 900 which means if we give the um let's go ahead and target a specific element so in this case uh let's let's say this will be thick font and target that by its id and then give it a font weight of 900 and you're going to see that this goes bold because we're specifying it to be 900 but since we only imported that one weight if we change this to say you know 700 it's not going to change anything on the page and the reason is we didn't import it but if we were to change this back down to 400 then it's going to go back to normal because we did import the 400 or regular font size so that's that's how the font weight property is related to those imports that you will do the last thing we'll talk about is line height so let me put in a paragraph tag and here let's go ahead and copy some lorem ipsum font which is basically just generic text that we can use and what you'll see on the page is very normal looking text now if we change the line height so if we target the paragraph elements and give it a line height of call it 30 pixels you're going to see that it spaces this out a little bit more than the original and i believe the default line height is going to be the same as the font size but i could be wrong on that i would have to check but basically let's let's see we can actually check that so we'll go one rem and it looks like no it's not the same as the font size because it definitely got smushed a little bit more but anyways line height is basically going to give you the space between the lines of text when there's multiple lines of text we are getting to the end here we have a couple more topics to cover and one of those is colors so there's not a whole lot to know about colors but i do want to walk through some of the basics so far we've been talking about just basic you know color so if we came into our paragraph element here we give it a color of say red now red is pretty easy to understand it's the color red but that is a built-in css property and i'm wondering if there's a resource that has all of those potential properties so if you go to the mdn documentation go to the color property so the color css property and then i believe there should be some sort of list of all of the possible colors that you can use let's see if we can find it here so it looks like here's a few of the color keywords that you can use this looks like a list i don't know if this is exhaustive of all of the possible ones but it sure looks like it so you can just go to color value is where that is on the documentation but that's what we've been looking at so far and there are also other ways that you can define colors so you can define it as a hex or hexadecimal you can define it as rgb or you can define it as rgba so we'll walk through all of these and i'll show you exactly what they do to help us out a little bit we're going to use this built-in color picker just type in rgb to hex or vice versa and usually comes up on google there's other tools that you can use as well let me zoom in so that we can really see what we're dealing with here and you'll see that in this color picker we can drag to look for different colors we'll just stop here on this green and what you'll see is all these different codes so we could just type in to our css we could just say we want green or we want something like light green those are both valid values but if we wanted to be more specific and go with a color theme we're going to have to use something a little bit more complex and these color codes allow us to get to a very finite amount of detail with our colors and what you need to understand is that each color is built with red green and blue which is defined by this rgb value so the first number is going to be red then green and then blue now these numbers go from 0 to 255 i believe that's one byte is how much space that takes up in memory and each different combination of those bite values are going to give you a different color based on the blending of those red green and blue values and if you were to take these three values and convert them to hexadecimal notation which is just a numbering system you get this code right here so the 128 would be represented by 80 in hexadecimal 235 would be represented by e and b in hexadecimal and 52 uh oh just drag something around there 52 will be represented by 34 in hexadecimal so these are the same exact thing just expressed with different notation now there's one other thing that we can do and that is add an rgb a value so let's go ahead and copy this hex code in so we'll copy that into our our color for text color and you'll see that it changed a little bit and that works we can also come back and grab the rgb values and then in our css all we need to do is type rgb put some parentheses and separate those values from 0 to 255 by commas now there's one trick that you can use if you add an a at the end of the rgb you can define one more value within this i guess array or whatever we're calling this and this represents the opacity that we're going to define so if we wanted 100 opacity basically the same color we give it one and that's going to give us the same color but if we wanted 50 opacity you give it 0.5 and you can see how this has been kind of grayed out we can actually use our our new skills of absolutely positioning an element let's go ahead and write a div in here and we'll say uh overlay and come down and style this so we'll say overlay is gonna be a width of 200 pixels a height of 200 pixels and then we're going to give it the same exact color right here so and then maybe the p color will just be uh black by default so that we can see it really well and then this overlay is not showing up and why is it not showing up hold on oh that is because we've set the color we need to set the background color to actually get something to show up now this is not over the text yet but all we have to do is say position absolute and then give it 0 from the top and there we go so we've now got this box sitting on top of this text and you can see that we can actually see the text through it because we've given it this 50 opacity value if we flip that to one it's going to disappear because it's 100 percent opacity and it's covering all of the text so that's what the rgba css value is able to do with colors now speaking of colors i recommend going to adobe color which is a great place to kind of explore different color themes and it will show you like all the different complementary colors to certain codes you can even go to explore or go to trends and you can look through different color themes that people have put together for different you know niches so graphic design illustration architecture game design and you can go in and click on these palettes and it will give you all the hex codes that you need and you can set those up in your global css values and then your whole web page is going to be themed in a uniform way all right let's get rid of all this stuff because we're ready for the uh i think the last category of css properties that you'll probably need to be aware of and that is background properties so far we have talked about background colors and you've seen me do this with these boxes where i'm just giving them background colors so that we can see them on the screen but you can also do background images and with background images there are a couple of properties just like we talked about before certain properties set to certain values kind of change the rules of the game for the rest of the the page and that is the same deal with background images so i'm going to walk you through a couple use cases with backgrounds and how you might see them you know in in real life when you actually start putting together web pages the first one is simple like i said we've already talked about it and let me just go ahead and make some simple text on the page just just so we have something but what we're going to target is the body element so that's the whole web page and i'm going to give the body element a background color of green and you're going to see on the screen that it turns completely green so that is the easiest property the next thing that you can do rather than just making a solid color background is you can put a background image on your web page it's actually pretty common for like a home page here's my golf website you'll see that there's a background image there and this background image i've actually pulled this out into its own link so that we can use it within the codepen environment so let's copy this link so that we have it ready and now what i'm going to do is put a div up here and we're going to call it the main content and then we're going to put the text in there just that we have something to see and from here we will grab the main content and we're going to say background rather than background color equal to something like blue we're going to say the background image is equal to a url and i guess i copied in the wrong thing here so let me grab that link again and now we have a url so the problem here is it's only taking up this small amount of space and that is because we have not defined the size of this main content so all we need to do is say the width of the content is going to be a hundred percent of the viewport width so again that's the whole visible screen is 100 of that width and what you'll see is there's still some gaps here and that's because the code pen environment automatically adds some margin around the body so what we need to do there is reset that so we'll go to the body and give it a margin of zero so that should bring it in line there and then we have this p element which has some sort of margin on it too which is adding this top margin so we'll make the p margin equal to zero as well and now we have this at the top of the screen so if we come down here and give it a height of percent of the view viewport height now we're gonna see this image across the whole page now obviously this image is not showing what it is like this is who knows what this image is and we need to position it a little bit differently so the way that we do that is we'll use a property called background size and we'll go ahead and set that equal to cover so that's going to cover the whole container that we have it as a background for and then we want to make sure that we specify the background repeat to be no repeat just in case a screen size you know is so big that it has to repeat and then finally i think we need to add in the background position which is going to take two values it's gonna say i think you can just say like bottom right or something like that so let's go ahead and look that up if we go to the documentation and type in background position so it says the background position sets the initial position for each background image it's relative to the position layer okay and you can see that there's multiple different values that you can give it you can give it values relative to different parts of the screen or you can just say position it at the top left or center that kind of thing so what i will do is just position it to the right bottom right works but we can just position it to the right because our golfer here is on the right side and we need to pull it over so that you can see him in the background so this is our basic background image and then from here you can put text on the screen so what if we went up here and said on the body we want the color of the text to be white so that we can see it and instead of paragraph text maybe we have like an h1 welcome to my site or something like that and then of course we need to remove that margin because it's messing it up and we'll come down here give it some margin on the top which would be 50 pixels actually no we don't want to do that we want to do some padding on the top so that it doesn't bring the whole image down and then maybe we'll say on the body that we want the text alignment to be center so that's centered on the screen so we have a welcome message says welcome to my site and you've got a nice background image there's obviously a lot more that you can do here i didn't even scratch the surface on all the different things you have to think about when you're putting together a background image and trying to actually get elements like this piece of text on top of it so that gets a little bit more complicated but hopefully this is just a nice overview of those background properties to see what's possible all right so the last thing i'm going to say about css properties is that there is a short shorthand way to write them in some cases so to this point you've seen me just writing these properties and you know not really explaining okay i'm i'm giving it a margin of zero right here but what does that represent you know is the zero margin gonna be a margin on the left the right top bottom all four of them and the answer is it's gonna be all four of them because this margin property is a shorthand css property and what you can do let me let me go ahead and get us a better example here because this is not a good example so let's make a div and target that div give it a width of 200 pixels a height of 200 pixels and a background color of green just that we have something on the screen and then from here um actually let's let's make a couple of these so that we see two of them and you can see that they're combined together because there's no margin but if we were to put some margin on this we can say the margin is just 20 pixels and be done with it or we could say that the margin top is going to be 20 pixels so that it doesn't space it from the left side of the screen so this margin property when it's defined just on its own is a shorthand property you can give it one value which makes the margin on all sides equal or you can come in here and you can give it four different values so let me make sure i got this right but the first value should be the top margin then you have the looks like the right margin then the bottom margin and finally the left margin so you can define different values for you know top right bottom left instead of writing it all out like this so we could say this this line right here i'll comment it out and that's going to be equivalent to this so we'll say margin top is 20 margin right is 5 pixels margin bottom is 10 pixels and margin left is 20 pixels so these four properties combined together is the same exact thing as this one shorthand property so you'll see this very commonly in css where another great example of this is border you could say border is one pixel solid black and you'll see that border pop up or not very well let's make it 5 pixels so you can see it better and this is a shorthand property so what we could have done is comment this out and say border size is 5 pixels the border color is black and then the border i believe style is solid and it looks like i got something wrong here because the size is all messed up but let's let's look it up in the documentation so we'll type in border and it says that it sets the values border width border style and border color so that's what i missed it's not border size it would be border width and now you'll see the same exact result as you saw let's go ahead and comment this out just so that i can prove this out so we'll comment out those three properties and comment this one back in and the shorthand property has the same exact effect so shorthand properties are really quick and easy way to write css you'll kind of get used to them and just wanted to let you know that that was going on because i know it's kind of confusing when you see all these different properties being written and nobody explains oh what we can define the margin right using like three different ways of doing it congratulations if you've made it this far i know it's been probably a couple hours at this point of me talking about css and properties and concepts box models all that kind of stuff but hopefully that was a good introduction for you and at this point we're going to kind of prep ourselves for the html and css challenges that we looked at at the beginning of the video so i'll be walking you through those on video in separate videos not this one but before we get there i want to introduce the four step method that i go through when i'm writing html and css because it was not offered to me when i was first starting out or i didn't find it on my own and i think it would be really helpful to you because it took me a while to figure this out and really get into a good routine so this is a systematic way to write css in html and the first step is to figure out what you want what you want to put on the page and oftentimes that comes in the form of a design now we talked about we're not going to go through wireframing or graphic design in this course we're just going to use we're leveraging existing designs that have already been built but that is a huge huge concept here you never want to go into it where you have no idea how you want to design the visual elements on the page and try to write the code and design it all at the same time it's really confusing and i've done it too many times and i would not recommend it now once you have the design or at least some sort of wireframe that gives you a sense of where you want to put the elements you have to think of step two which is how do i want to arrange those on the page with html now this is a highly intuitive step you're going to get a lot better at this with experience and it's much easier after you've written a couple of apps to understand oh i need to put you know a div container here and then i need three div containers within that main div container and then within each of those containers i probably need some text and then maybe another container so you'll get a lot better at realizing you know how to use divs and different semantic html elements on the page as you go along the third step is how can i use css to achieve that outline or layout and this is where all the stuff we talked about in this crash course come into play if you don't know some of these properties and you know how they work and how they you know disrupt the flow of the elements on the page then you're not going to be able to do this very well so like step number two you'll get a lot better with practice on this one and then finally step four i really think this is probably the easier step of this process and that is how do i want to style these elements so once you've got them laid out on the page styling them is rather trivial you're just putting different colors and some padding maybe some margin that kind of stuff on them maybe some text decoration or you know font sizes that kind of stuff and it's pretty trivial compared to getting the layout correct in the first place so to go through this process i'm going to give you a short example here at the end of this video and then make sure to keep it in mind as we go into the challenge videos and i'll be sure to kind of adhere to this process as we're building those front-end mentor code challenges so here's what we're going to build with this four-step method this is we can think of this as the design so let me go ahead and change the view so that it's very clear so we're going to go to full page view and you'll see the design that we're trying to make it's nothing fancy but just looking at that visual design how can we lay that out in html so we have to decide you know looking at this do we want some containers built out you know it looks like they're next to each other how do we accomplish that with html so let's go back over to our playground and start writing this out i'm gonna actually put these side by side so that we can see them a little bit better when i look at this design the first thing that i see is two div elements so i'm thinking i'm definitely gonna have two divs and usually when i put something together with html i have a container div so kind of like the overarching parent to all the elements so what i'll do here is start us out with the template we'll say div dot container and press tab to make a div with the class of container and then from here i want to make a div with a class of box 1 and then i want to make another div with a class of box 2. so we've seen this before so if you remember you might be able to kind of figure this out but this is the basic structure that i think i want to lay that out and of course there's two pieces of text so in each of these divs i'm going to put some sort of h2 we'll call that box one and then we will say we want some text in there and then we can just copy this down into box two as you can see on the screen on the left this looks nothing like the one on the right and the reason i knew to put all these html elements in this fashion is because i have experience doing it and i just know that we have a overarching container each div is going to represent each box and then there's content within each of these divs that we can you know center and style a little bit later so let's go back to our our four-step pop process here we already asked how we want to arrange them that's the design itself so what we're looking at over on the right we said how does this arrangement look in the form of html we already wrote that out it doesn't look anything like it yet but it will and now we have to say how do we use css to imagine that layout that we see on the right so from here we have to take css and get these boxes i need to label this one correctly we need to get these two boxes and put them next to each other now what i'm most comfortable with is flexbox i know we haven't learned it and that's going to be a separate video but i'm going to go ahead and use it here just to give an example and how you use that is you basically take the main container and you display that as a flex item and what that's going to do is it's going to put the boxes right next to each other now what you have to do is for each of the boxes we're going to give it a width or no we're not going to do that we're going to give it a flex basis of 50 percent and we'll give that also to box number two so flex is going to be 50 that's basically going to make them equal to each other and then we need to give them a border so that we can see what is going on and i think it's about two pixels is what the border we're looking for is so we've got the border here on box number one we can copy this down to box number two we want to make that one blue okay and the next thing that we want to do is probably give some margin around it because currently they're right next to each other so we'll give it a margin of say 20 pixels and down here we will give it a margin of 20 pixels as well looks a little bit big so let's reduce that to 10 on both of them okay that's looking a little bit better and then with the box one and box two we probably also need to do text align center so that the text is all centered we're going to refactor this in just a second so now we're centered and at this point we're pretty much spot on with our layout so we need to go to the last step which is how should each element be styled we already have a border so that's kind of part of the process but the only thing that we need to do really is give it a color of blue for the text on the right and then give it a color of green for the text on the left and there you go um we have a pretty similar layout i think partially why it doesn't look exactly the same as because it's smaller but maybe we gave this some different dimensions but i'm happy with this we accomplished kind of what we wanted here the only thing that i would do is refactor the css because currently you can see we've set flex equal to 50 on both of these we've also set the text line and the margin so we can combine those into one selector so put a comma there select both boxes and then get all of these common properties that apply to both of them in here so we'll grab the margin and the flex basis and then we go ahead and delete that from these properties or from these selectors and it should look the same now so this is how you would go through that four step process to solve an html and css challenge let's jump in this is what we're going to be building here it's going to be a nice way to practice a lot of those basic css and html skills that we've just learned there's nothing crazy going on here but it will give us that good opportunity it looks like we've got a few different font styles font weights a little bit of color in here and then even a button down here that we can style and maybe add a pseudo class hover effect to so it'll be a great comprehensive introduction to writing html and css this code challenge comes from the frontend mentor website which is a great resource that you can scroll down and see a bunch of professional web designs for different things that will challenge you to use different skills in html css and javascript the one that i picked here is rather simple it's kind of on the the easier side of this but we are going to walk through a couple others so as you see this grid and the crowdfunding product page those two will be coming up in future video challenges for this video i've purposely not looked at this yet and figured out how i'm going to do it so this is completely raw and uncut i really want to just show you my thought process we talked about this four step system for writing html and css in the actual css crash course and i really want to apply this and walk you through my thought process of how i take a design such as this one and think about how do i lay it out how do i style it and what resources am i using to do so i'm not the best at writing html and css in the world far from it there are plenty of people that could come in and critique what i'm about to do in many many ways but my goal here is to show you just a realistic look at how we go about building something like this consistent with what we've been doing in the prior videos we're going to be using the codepen environment so you can start a blank code pen document and just follow along as we go through this challenge i think our first step is going to be going over to the front-end mentor website we don't need this page anymore but if we go ahead and click the visit challenge hub for the single price grid component which i'm going to leave a link in the description to this so that you don't have to go searching for it you'll find some starter files a design file that we don't need this is more on the side of you know if you're using figma or sketch or even photoshop or something like that to mock up the design it might be useful to see what are the components and layers going on there but like i said in the crash course this is not about graphic design or wireframing we're solely trying to take a pre-existing design and turn it into code scrolling down you can see that this is the newbie difficulty level so it should be relatively straightforward to solve almost 10 000 people have tried it and about 2 000 have finished and i fully intend to be in this bottom category as should you and the brief that we're given is pretty simple so it says your challenge is to build out this pricing component getting it looking like the design okay you can use any tools that you'd like we're going to use vanilla html and css we're not bringing in anything additional except maybe a google font or something like that it says your users should be able to and what i want to call out right here is this is called a user story and we've touched on it previously within this crash course when we built the html challenge but a user story is something that you will write down in the requirements gathering phase of building a web page or web app and it's basically saying okay let's come up with a list of things that the user should be able to do if this is implemented correctly so the first requirement is that we should be able to view the optimal layout for the component depending on the device's screen size we have not yet talked about responsive design that's i believe going to be the next video in this series so we may leave this part out i'm not quite sure yet we'll see how long it takes to build the first part but basically what that's saying is as we resize the screen or if we look at it on desktop or our mobile device it should you know look good to the user the second one is you should see a hover state on desktop for the sign up call to action so in other words what they're talking about is this button down here when we hover over it we should see some sort of interactive thing happening whether that be the button gets larger or changes colors opacity something like that so we can get a little bit creative with what we're doing there it says download the starter code go to the readme file this will give you further details the style guide will give you the colors and the fonts that we need to use so i'm going to go ahead and open this up you'll see that we have some folders and some files if we open up the readme it's written in markdown which is kind of hard to read here so what i'm going to do is open all of these files in a tool called visual studio code which is the code editor that we will eventually be using within this series but for now like i said we're going to be using this codepen environment because it's a lot more interactive and you can see the changes happening in real time so let me get that opened and then we'll look at some of these files i've got all the project files open in visual studio code which again you'll get very used to as we go throughout this course but basically we're just using it to see the the files for now and if we open up readme you'll see that it's written in markdown if you don't know what markdown is just go google it real quick to understand it basically takes these little two hashtags for example and this would become an h2 element within html so i've got it open in the preview it's a little bit easier to read than you know the stuff going on over here most of this stuff is not new the one thing that i'll talk about here is down here in the deployment section so they recommend that you use get version control to host this and push it to your repository and deploy it with for sell now we're going to just put this up on github pages and we're not going to actually be using the git utility because we haven't covered that in the series yet but i do want to you know give you a way to see it live on the internet and github pages is a really simple way to do that i'll show you exactly how that works but it will not be the focus of this tutorial the style guide we don't really need to go through this all that much right now but we'll be referring back to it when we have to figure out okay what font are we going to use how large is this card going to be and what colors are we putting in there so this will be a good reference so let's close that out and i think everything else looks like we have design these are just uh preview images to reference and then images we just have a favicon which is going to be kind of up in the browser tab and finally we have this git ignore file which has to do with the get version control utility we're not going to talk about that and then finally in index.html which has some predefined html boilerplate to start with but once again we're going to be using codepen instead of doing that just because it's a little bit more interactive for us all right let's get started uh coding this thing so once again we'll go back to this four-step system for writing html and css and for this i use this four step system on a component by component basis so in our case we're just building this simple card so we can pretty much use it one time through but if we had a much larger project we'd kind of iterate and do this process multiple times so the first thing is how do i want to arrange these html elements on the page that is basically talking about this design right here so how do i want to design it we've already have that done for us so we can go to step two how do i make this arrangement look using html so the way that we can figure that out is primarily by experience just knowing how you can write html that will be turned into something like this but i'm going to walk you through my thought process and just kind of start to give you that experience so just taking a look at this design the first thing that i'm thinking about is how do i lay this out with html so what are the container elements that i need to use and as we talked about a div element is a great you know container type element so what i'm thinking about is there's probably going to be four main containers that we're looking at from a high level the first one is going to be the entire design itself so that will be one div then we'll have something for this white box right here the bottom left box and then the bottom right box so really what we're going to be looking at is something like this and excuse my poor drawing i'm using a mouse so this is kind of tough but if we break it out like this and then we draw something around it this top one is going to be a div and then we'll have another div here and we'll have a div here and a div here so we're going to have four in total and that's going to represent our entire design as we move down to this this bottom left section which will be contained in a div it's going to be a little bit trickier than this top one because of this part right here so you see how we have some text and some more text styled right next to each other horizontally and as we know elements are block elements by default usually so we're going to have to find a way to lay that out so that they're both on the same line and they have two different styles so that'll be interesting to see how we do that and then finally at the bottom we've got a button which we'll spend a little bit of extra time on because we need to have a hover effect on it and then in the bottom right we have a pretty straightforward design where we just have a heading and then it looks like an unstyled list so it's not necessarily paragraph text it looks like this is a list without bullet points so i'll show you exactly how to do that and then finally we have some different colors throughout but that's not really related to this step so we've gone through steps one in steps two so what i'm going to do is actually take step number two and lay this out with html as we just talked about so i've got my design here on the right and my code pen here on the left so let's go ahead and start with this the first thing that i'm going to do is make a main div that we're going to call the container or more descriptively we'll call it the card because this is what we call a pricing card card is just terminology for something that's like all contained within a box then furthermore i said that we're going to have three more divs so let's lay that out and we're going to give i don't know we'll call it an id so maybe let's be more specific with everything here because we're only making one card in a real css project you might want to make a card design and then apply it to one class that you can you know use with multiple elements but for here we can just use ids and be super specific so we have a card then we have the top section and then we'll come down make yet another div this is going to be the bottom left section i'm just being super descriptive here and then furthermore we're going to have a bottom right section so let me expand this so that we can really see what we're dealing with it's not going to show anything on the screen yet because we haven't put anything in these divs but one thing that i just noticed that we probably should be doing is this bottom right section here we probably should wrap that in another div just because they're horizontally next to each other so really what we can visualize this as is we've got one section up here and then we have one section down here and then within that we have two more sections like that so over here in our html we're going to wrap these two in a div so let me take the bottom two sections and put those in a div as well and make sure that we have some good indentation so that we can see exactly what's going on pretty easily let's give this one an id so we'll just say bottom section so if you look at this on the right my extremely messy design we have this top section which is this big box and then at the bottom we have the bottom section which is the big box surrounding these two little boxes and then the two boxes down here are represented by the bottom left section in the bottom right section and then everything is encompassed this is getting extremely messy with a card right here all right so let me go ahead and disable this that we don't have all that drawing there but you can kind of see how this layout is going to map over to this design at this point let's put something in here so that we can start seeing this come to life at the moment we have an empty web page because we've got a bunch of div elements or containers that we've laid out but there's nothing in them so what goes in these sections let's start with the top section so right here contain contained within this single div and what we're going to put in there is it looks like a heading so that looks to me somewhat like we'll call it an h2 heading and it says join our community so that will be our first heading right there and of course this is not going to look anything like the card at the beginning we're going to use css styles to make this a lot prettier in just a second we'll come down here and this looks like maybe an h4 heading that says 30 day hassle free money back guarantee all right so that is the second part and then the third part is going to be some paragraph text so gain access to our full library and i'm going to stop talking so we can speed through this all right so we've got our top section already created and once again it looks nothing like the top section over here but we will get there in due time now we'll come down to these other sections here so we need to finish the bottom left section which we're going to put in this div right here and then the bottom right will be going in this div so with the bottom left section it looks like we have an h4 tag so similar to how we use for the 30 day hassle free money back guarantee we'll go ahead and use that same one for the monthly subscription because it looks close we can always change it later so we'll put in monthly subscription and then like i said once we get down to this little area so if we circle these two pieces right here this is going to be what i think as another container div so let's go ahead and make another div and then within that div we want to have we'll say um i guess we can just make another div that says 29 and then one more div that is going to say per month so this is going to this total container is going to wrap that pricing component and then we're going to take these two divs right here and make them go horizontal to each other as you can see they're you know block elements so they're breaking to new lines but you'll see how this comes in handy in a little bit so let's go to the next part get rid of these lines here and you can see that after the pricing it gives us a small text down here so we'll we'll put a paragraph and we'll say full access for less than one dollar a day and then finally at the very bottom let's space this out once more we're going to need a button that says sign up and you'll see that this button comes on the page here and then finally this is where it gets a little bit confusing since we haven't added any css yet but this container on the far right is going to be next to the container that we just created but for the time being since everything is a block element by default it's going to go right under here so let me go ahead and write this out it looks like we're using the same kind of header here so we'll stick with the h4 and say why us and then down here as i said you could interpret this as paragraph text you can make a bunch of paragraph text that breaks to new lines or we could just make a list and then take the bullet points out later with css so that's what i'm going to do i'm going to make an unordered list and then each list item is going to be what we're seeing on each line okay we've got it all written out we've got all the text and elements that we're probably going to need for this entire card but as you can see this looks nothing like the card that we have on the right and believe it or not we're going to use just vanilla css to take this ugly looking thing and turn it into the card that we see on the right so in reality if i was building this myself and i wanted to do it as quick as possible um what i would do is probably not lay it out like this where i've got all of the text on the page at once i would probably do it in pieces but i wanted to lay this out to just show you how we can visualize a design of course you need a little bit of experience to do this but we can visualize a design and turn it into html elements and we can take what looks like nothing special and turn it into this card on the right but let me go ahead and walk you through what i would do in real life so i've basically created a clone of this and what we're going to do here is we're going to delete all that text that we just put in here so we'll get right back to where we started and as you can see we have just the layout that we had created earlier where we have the whole card we have the top section bottom section and then the sub sections within those and realistically what i would do first off just to get everything laid out on the page correctly is start giving some dimensions and some borders on this so that we can visualize it and then i would put in the actual text elements so let's go ahead and do that real quick and you'll see exactly what i mean so if we were to come back to this four-step process this step number two can involve some css as i'm about to show you so we'll open up the css pane and what we want to first do is target the overall card so to do that with an id you use that hashtag and then supply the id and now what i'm going to do is just give it some sort of width and height and also tack on a border so that we can see it on the page so let's go ahead and give it a width of 200 pixels and a height of 200 pixels and then a border will be one pixel solid red so now you're starting to see and i'm gonna actually make this three pixels so we can see it better you'll see just a basic container on the page now the next thing i want to do is go see what the specifications for the size of this main card is because this outer container we are calling card should be the total dimensions so if we go back to visual studio code and see our specifications so we're in the style guide right now it says that the designs were created to the following widths so like i said we're not going to be getting into responsive design in this tutorial so if you were to look at the desktop design it looks like this and that's what we're creating but there's also a mobile design which you can see has a slightly different layout and in real life what we would be doing is we'd be designing on desktop to look like that and then when we resized the page so as we you know resize the page in our codepen environment we would expect this design to change but for this one we're staying simple since we haven't learned about responsive design yet and we're just creating this one right here so the specification for that desktop is going to be 1440 pixels but if we came to codepen and we gave it a width of say 1440 pixels it's going to go completely off the page so we're going to take a little bit of liberty here and make our own design based on the size of our code pen editor which i'm guessing is going to be around we'll call it 700 pixels maybe um that's a pretty good size we're probably going to want to give ourselves some some width here to work with so let's make it 600 pixels which is going to give us all of this space right here and then for the height we're just going to eyeball it because they haven't specified exactly what that height should be so let's look at the desktop design again you can generally grasp the aspect ratio here and you can tell that we're off by a little bit so let's maybe throw this up to 350 pixels and that looks like generally the aspect ratio that we're dealing with right here so nothing nothing perfect but it's going to get us close now the next step since we have our main container laid out and we just see it visibly we're going to do the same thing with each of the sections so you can see that we have our top section which we can come down into our css and target and for this section we're going to give it a width of 100 percent because we want it to be filling up the entire space of its main container and then we're going to give it a height if i can spell that right we'll give it a height of 50 percent because well maybe not 50 it looks like the top section is call it 40 and the bottom section is about 60 so maybe we'll go in that range where we give it a height of 40 percent the width of a hundred and of course we can't see anything yet because we haven't given it a background or a border so let's throw in a 2 pixel solid green border so you'll see that pop up in the top section here and that that looks pretty good so far now there's one problem that we're seeing already that i want to fix right off the bat before we get any further and that is this box sizing so as you can see when we put the border on this inner container you're seeing that it's kind of breaking out of the parent container on the right side here and the reason that's happening is because our box sizing is set to the default content box so what we're going to do is a basic css reset that's going to make every element on the page abide by the border box box sizing and we talked about that in the first of three lessons in our css crash course before so go back and review that if you have to to apply the border box box sizing to every element in this html document we'll just come to the top of our css file and paste in a predefined html or css reset so we're saying on the total document we want the box sizing to be border box which will as you see it will make this box go within its container and then the second rule is just basically going to say i want to grab every other element and apply or inherit that you know root level box sizing so by just adding these two rules you can see that our boxes are fitting nicely within each other if we comment them out it's going to be breaking out of the box again if we comment them back it goes or it should update i'm not sure why it's not updating let's uh refresh the page there we go so it is within the box with that fixed we can go to the bottom section um so let's let's go back to our design here for a second we've got our top section bordered out now we have this overall bottom section so we will write a css rule to target the bottom section div which is right here so you can see the bottom section which has the two sub sections within it so let's come down here and just paste in these properties because that's all we need but it's obviously going to be the wrong heights and widths for now so we do want a 100 width we want it taking up all of that but the height that we want is going to be 60 rather than 40. because as you see with 40 it doesn't take up the full uh height of the entire container but now that we've made it to 60 and then we'll turn this to call it blue you'll see that we now have a outer container in red our top container in green and our bottom container in blue now the last thing that we have to do to get this layout um perfect is going to be adding these two containers in the bottom section that will be side by side i'm going to add them one at a time because it's going to require us to do something a little bit tricky because they go next to each other so let's target the bottom left section first so we'll copy in this id into our css put that hashtag in front to target it and here we're going to copy in the same properties again just to get our border now this is going to be incorrect at the moment because we're giving it a width of 100 but really what we want is a width of 50 percent because we want that to take up the first half and then the height that we want is going to be 100 percent now i'm using these percentages because we have these already broken out within their containers so let's put a different color on here um maybe we'll put on what's contrasting to blue we'll say orange so that we can see that pretty nicely within that container and the reason that this works is because the width the width of 50 percent is going to be relative to its parent container so if we look and ask ourselves okay what is the parent container of this bottom left section well that's going to be the bottom section and the bottom section is outlined in blue right here so rather than saying 50 width of the entire document we're just saying 50 width of the bottom section which is already outlined for us and you can see that it's occupying perfectly 50 percent of that now if we did the same thing with the bottom right section so let's change this id to the bottom right section and then i'm running out of colors here but maybe we'll just give it a 2 pixel solid black border now we're running into problems here because now that we have a bottom right section it's breaking down onto a new line because all of these divs are by default block elements and they occupy their own line so in order for this to work we actually have to make both of these you know subsections inline block elements so we don't want to make them inline because inline elements don't respect the height and width properties which we want them to respect but we do want them to be up on the same line so what we're going to do is come to both of them and say display inline block and then once again display inline block now at this point you're probably wondering why are these boxes not lining up next to each other we've given one of them fifty percent with the other one 50 with and we've turned them into inline blocks which basically means that they both can sit on the same line unlike a block element now the reason that this is happening is probably going to make you pretty mad um because it made me mad when i found it out for the first time and a lot of other people and it's kind of just a quirky thing about css so when you make an inline block that respects the white space within the document and believe it or not if you come up to this html where we have the bottom left section in the bottom right section there's actually some white space between these two elements so this this space right here is considered white space for inline block elements so if we were to just backspace and bring this right next to each other it's going to bring this box up within that container and it's going to fit perfectly now i know what you're thinking like i said this is kind of infuriating that this is the reason that that was doing that and that's kind of you know coding in general sometimes you just find things that are completely ridiculous and what i will say is that there is some hope so in the next couple of videos we're going to be talking about css grid in flexbox and both of those will be a much more optimal way to make this layout like we've done here and you won't run into these weird quirky problems that just exist as a part of kind of the older method of writing css all right so just keep that in mind we have taken the divs that were nicely uh laid out in our html above like this and we've just removed the little white space between them and now it's going to work just fine so that's hopefully the only little hack that we're going to have to do throughout this project and like i said it's just it's a little bit frustrating but we have to do it here and until we know css grid and flexbox at least but this would be kind of your old way of lining these things up now the last thing that we have to do here because we've got our layout so if we come back to visual studio code you can see that we've got our top section bottom left and bottom right section all outlined with these ugly colors and and whatnot and at this point we can start putting html elements within them so let me go ahead and grab some of the text that we created over here just in this ugly space and i'm just going to copy the exact html and put it into each of the sections that it belongs in so we're going to see some html go up here and of course it's not going to not going to fully fit at the beginning we will fix that i promise and then let's come down here grab this put this in the bottom left section and you'll see that we have some some spacing that we're going to have to address here and that is partially due to the the default margin that is given that is being given to h2 and h4 elements and paragraph elements and then finally the bottom right section will copy that and paste it into the bottom right section okay so we've got all of our elements in here and now we have to fix a few things one of those things is the default margin that we're getting on these elements so i'm going to actually inspect the whole page it's going to look very weird for a second but if we hover over some of these elements you'll see that we have this orange margin on top and on bottom of those h4 elements and that is because it's just a browser default it's something that just it you know happens by default and if we go over to this page you can see that all of our text is is spaced out pretty nicely and that is because of those browser defaults but obviously over here we don't want them to be in effect because we want our document to have no spacing around we want to add our own spacing so all we have to do is come up to the top and to our html or sorry css reset where we're just you know resetting default properties i'm just going to grab the h2 element and the h4 element that we have as well as the paragraph element and give it a margin of zero so hopefully that will fix a few things you can see that we have no spacing around here all right the last problem we're dealing with here it looks like we've got all of our text without default spacing so that's nice and the last thing that we're worrying about is this orange box which is oddly just sitting there floating in space and if you were to right click and inspect this and you start clicking around looking at this there's not going to be any you know obvious reason why this is sitting down here there's no margin or padding or anything going on here so you kind of have to ask yourself what is going on and from experience i just know that when you have an inline block element it follows different rules than a block element and there are different properties that will allow you to modify how those rules are applying to your html now that's kind of similar to all css so when you change display properties and box sizing there's just different rules of the game so if we were to come over to the mdn documentation and type in vertical align this is going to show us a property and it says the vertical line css property sets vertical alignment of an inline inline block or table cell box so this does not apply at all to block elements which is the default type of html element for pretty much everything but what it does is it basically says this is a pretty good visual right here if you have two inline block elements that are sitting right next to each other on the same line they can be aligned at different vertical positions and by default i'm not sure exactly what the default is here probably the baseline or something like that but anyways we want to make sure that this gets the top alignment because if we come back to our challenge here we want these two inline blocks to basically lock up to the top border of their container so if we go into each of the sections and for the bottom left section that's behaving oddly if we type in vertical align and then say top it's going to bring it right back up and snap it to the top edge of its container now the bottom right section just so happens to snap up there naturally but let's go ahead and add the vertical line top for consistency's sake and this will also snap that to the top so at this point it looks like we have a nicely laid out card and although it is pretty ugly we've got everything that we need all of the content in there and we've got the layout that we need so at this point it's all about styling so in our four step process we looked at number one that was just looking at the design in some cases you'd actually do the designing in step two we asked ourselves how do we lay that out with html so we templated everything out now in step three we have to ask how can i use css to achieve the layout that i want and that's exactly what we did here we just used a few basic css properties to put some borders and really visualize the card component that we're trying to create and now finally how should we style each element and this is kind of the fun part where our ugly layout example is going to turn into what we see in this example a nice styled card so i think the first thing that i want to do let me go back to my example here since this is just called layout example let's let's copy all the html here into our original code pen and of course all of the css we'll copy in there too so this should get us back to where we were there and i can go ahead and close this out so we've just got this one example and i think what the next step here is going to be is to get these background colors uh put in there so we're going to start to get something that doesn't look so ugly so just looking at the colors let's just drop these into google because i'm really not sure just they're saying cyan bright yellow light gray in a grayish blue and to be completely honest i don't know what that refers to here it looks like this might be the yellow um not sure where the gray is maybe this text right here let's let's go ahead and type those into google real quick so we'll go rgb and there should be something rgb to hex maybe okay this is what we're looking for just the color picker on google and all we have to do is go back to our style guide and let's see what this cyan color is so we'll we'll paste in these values right here i'm not sure why they're giving us hsl values because that i really haven't used those all that much in web development you usually stick to hex and rgb but they are so let's go ahead and convert those to something a little bit easier to use so we'll grab a hex color here and let me just replace this just so we have a reference and then let's paste in the hsl for this yellow color that we're working with which to me looks more green maybe i'm colorblind who knows go for those hex values at the end here just for reference and then we'll do the same thing for the remainder because i like working in hex and i've never really worked with uh hsl so we'll go back and put these in here now to me when i look at this these bottom bottom left and bottom right boxes they they look like different colors to me this one looks like just a lighter version of the left and in the style guide i'm not so sure that they've given us two of those options so if we came in and looked at the cyan color so let me paste that in here it looks like that would be the bottom left color that we're dealing with or maybe even the bottom right color so what i'm going to do is just drag this that we have different colors maybe i'm missing something here but let me just drag this so that we get a little bit lighter color that we can work with and then i'm going to grab that hex value and paste it into the style guide so we'll put put that in there as darker and then lighter and at this point we can grab these color codes and start using them so we'll grab the darker color and we'll come back to our code pen and just put in a background color property so for the bottom left section we know that we can use the background color property and paste that in there and we should be getting that darker cyan color alright so at this point we can probably remove that border and that looks a little bit nicer for us and i'm also going to remove the bottom section blue border okay so we're looking a little bit better here and now i want to fill in the background color for this right section which as i just got a slightly lighter hex value so we'll go to the bottom right section get rid of the border and replace that with a background color and paste that in and it's looking like it's it's a little bit lighter but compared to the example it's there's more of a contrast here so let's go ahead let's play with that a little bit let's make it even a little bit more light grab that hex value and see what that looks like okay so that's that's a little bit better i like that contrast and you could even come over to adobe color and we'll go to this this adobe color tool and what i could do is i could grab the bottom left section hex code which is the darker cyan throw this in the middle here and then i want some shades of this color so we could grab maybe this shade over here which ensures that it's gonna you know nicely go together and let's put that one in the bottom right section just to get a little bit more complementary uh colors here and then i'll go back here and and replace this just so we have a reference of it okay so we've got these two um the top is going to be white and then at this point i think we can remove all of the borders just so that we don't have those ugly colors going on so let's let's go up here remove the top section green border everything looks good and then the red border around everything and boom we have ourselves a nicer looking layout now as you'll see in the example design it is kind of standing out against the background and you'll also notice that there are some little corners that are rounded so i want to do that just so that we have something as a reference here and i also want to kind of make this come out from the top left corner of the page so all we'd have to do for that is just add some padding to the body so if i go here and just add some padding of 20 pixels to the body you'll see that that comes in a little bit so remove it add it back and you can see our card just shifts inward so that we can kind of visualize it a little bit better and then finally for the card because that is the main container around everything what i'm going to do is add a box shadow and this property is going to take i believe five different arguments and they're all kind of referencing where the shadow is going to go so i can open up a tool called box shadow generator for css and there's a a couple tools here i don't know which one's the best they all should work but as you see this css box has a box shadow around it and you can you know drag these sliders to get different sorts of box shadows applying to this so this is a good visual tool that we can use to to see what we want and in our case we probably want a a much smaller box shadow so let's let's go ahead and actually no we want to stay close to zero so we'll go around four pixels and maybe three pixels or something like that and make the blur radius a little bit smaller and the opacity that we want let's make it down to like 0.2 or around that area maybe a little bit more i think that looks pretty good so the codes that we need it's showing us that we need to paste in all of these these webkit and moz box shadows are just for like other browser support we don't necessarily need them so i'm just going to copy in this rule right here and we'll paste it in to the cart and now you're going to see around the edges we're starting to get a defined edge to our card so let's go back to our design and you can see that we're getting a little bit closer here what i want to do is round the corners now so all we have to do is say on the main card we say we want to border radius of call it five pixels all right so we've got our border radius on here but what you'll notice is that the bottom right corner and the bottom left corner do not have a border radius and you might ask why because we've given a border radius of 5 pixels which should apply all around that container element but the problem is because we have these two inner container elements that are perfect you know squares and they occupy a hundred percent of the space within that main container so as we make a border radius we're actually those inner boxes are actually breaking out of the main container now and what you're seeing there is those those uh child containers there's a couple ways that we could fix this but a cool little trick that you can use and i know this is not necessarily obvious but if you do enough googling you would find this out if you set the overflow property to hidden on the main container element you'll see that those borders go rounded and basically what that's saying is we want to take those perfect square children containers and since those are breaking out of the main container we want to say to the main container css style okay if you have anything that's breaking out of your borders just hide it and so that's what allows those bottom border radiuses to apply so just a cool little trick it's also one of those frustrating things about css where it's doing things that you would not expect and that is literally the name of the game with css is figuring out how to get around the things that you're not expecting to happen okay so we're getting a little bit closer here the next thing that we can do that will just add a really nice touch and make this look a little bit cleaner is some padding i think so let's jump back to our design and you can see that all around the content here so around every bit of content there is this space this nice padding in here and what we could do is we could come in to our code pen and we could just say on the total card we want to add some padding of say 20 pixels but that's going to mess us up a little bit because although it adds nice padding around everything now these are not occupying 100 width and also we want some padding within these sub little containers so what i'm going to do instead of adding it on the total card to get padding like that i'm going to add them on each individual section so we'll call it a padding of about 20 pixels that's a pretty standard amount of padding to add and we can just look at this little space between the edge and the text on the top and the left to get an idea of how large that padding is and that looks pretty in line with our design so i'm happy with that and then let's copy that rule down into the other sections so not the bottom section because that represents everything here but i want to do it in the bottom left section so we get that nice padding and then i also want to do it in the bottom right section which will make the padding nice there too so at this point we've gone from a completely ugly layout design to something that actually makes sense visually we obviously have a lot to do with the text styles and colors in that button but we're getting a lot closer let's go ahead and change the text color you can see that everything in the bottom is a text color of white and then the top has varying text colors so we'll start with the top and you can see that the the heading is the same color as the bottom left or at least it appears to be so let me grab that bottom left color and we'll go ahead and apply that to our join our community text block so let me collapse the js here and we'll go up to the join our community and what you could do is you could target everything in the top section that's in h2 and since we're not planning on changing the html structure anytime soon i'm pretty happy with that you could also throw an id you know that says title and directly style it but in this case let's go ahead and practice our combinator skills our css combinators to style this so we'll come down here and at this point you'll see the css is getting pretty busy you might want to rearrange it and organize it by section but for now let's just keep going and what we'll do is we'll say the top section is the pre-selector that we want and then we want to grab every h2 element which is simply that first title and what we want to do is style it a color of that bottom left so now we're going to get the correct color let's go back to our visual studio code and see what else we have so we have this kind of lime green yellowish color as the next one and that is going to be this hex code the bright yellow so going back to our code pen you can see that this is the only h4 that is sitting in our top section so we can grab a similar rule as we just did except this time we're grabbing h4s and giving it a color of yellow so there is our yellow color and then finally we should have some sort of gray color and let me get to our visual studio code this this paragraph text is gray and in the style guide i believe that is the light gray that we're looking for so let's copy that hex code and then make all of the paragraph elements in the top section equal to this light gray color okay so that is that is very light i'm not sure if that's what we were supposed to do it appears pretty pretty good here but it's pretty darn light when we're looking at it here so i'm going to go ahead and make this a little bit stronger of a gray maybe it's the grayish blue actually let's try that instead ah okay so i think that's what they meant for this style yeah i think that's what we're looking for here so the grayish blue looks a little bit better we'll stay with that now we just have to add some some spacing between these so if you remember at the very top of our html we did a reset that said for all h2 h4 and p elements we want to add a margin of zero so that removed all the default spacing and now we can do it custom and since we've got all three of these elements selected with these combinators we can just drop into these rules and do so let's take a look at our design it looks like there's a good amount of space just by experience that looks about 15 pixels between the top and the next one let's go ahead and try that by adding a margin bottom of 15 pixels and you can see that's a nice little spacing there um what i'm starting to see is that our top section now that we've got this laid out is a little bit small from a height perspective so i think i'm going to adjust our our total dimensions here um previously we had said the bottom section has a height of 60 in the top 40 percent i'm going to change that to 45 and 55. and you'll see now we have a little bit more realistic uh breakout between the two sections i'm pretty happy with that so the last thing we need to do now that we made that change is add some spacing between this these two text elements and then we've already got some padding at the bottom so we'll go down to our rule of the h4 and add a margin on the bottom of 10 pixels which seems about right so comparing that up i think we're getting pretty close and and now that i'm looking at it we might actually need a little bit more padding around this uh than we've given it so let's go to the top section wherever we did that and let's increase that padding to say 25 pixels that that looks a little bit better for us and then we'll go ahead and update that to 25 pixels in the other two spots that we had added that okay so we're doing pretty good there and it looks like our total card size is it's starting to break out but i'm gonna wait on that before i try to restyle because the text is going to get a little bit smaller when we set the font size and change the font style so the last thing we'll do is come down and since everything in the bottom it looks like is a color of white we can just go to the selector that we used for the the entire bottom section so rather than trying to style the text in the left section and the right section we can just go to the entire bottom section and give it a color of white that looks pretty good i think that should work and then you can see the button color is not being styled because that is a separate element there so what i can do is actually target all buttons because there's only one button on the entire page so we'll we'll style the button and we'll give it a color of white and that's going to give it a text color over white we'll make that show up a little bit better once we add a background color which looks like that lime green but i'm going to wait on that for just a second the next major change that i want to make is getting this text to be the right font family right now we're just using the browser default whatever that is i don't actually know off the top of my head but we want to make this a different font completely and if you look at the style guide it's going to say the font family that we're looking at is carla and if we go to this link we can see all of the different styles that we need now the question is what what font weights and you know styles do we need well we can go and look at the spec it says we need weights of 400 and 700 so let me take the font weight of 400 and select that style and then the font weight of 700 and select that as well we will grab the link tags that we need we'll go back to our code pen over here go up to the settings html and in the stuff for the head element so since codepen gives us our own little environment we don't actually have to make the head element or boilerplate so we can just paste it here and by doing this and saving and closing we now have access to that new font but it hasn't changed anything because we have to actually select it within our document so up at the root element we want to give a font family of carla and you can see that it changed our design completely and it says that the overall font size is going to be 16 pixels so let's go ahead and say that the font size in the root element is going to be 16 pixels which is just going to apply to those paragraph elements as i'm flipping back and forth between these two designs looks like we still need a little more space after this so let me do that real quick before i get into anything else we'll add 20 pixels to there okay and then i'm i'm thinking that we're running out of space vertically and since we came up with an arbitrary height at the beginning i don't mind changing that because they didn't specify what the height should be so let's go ahead and turn that to 400 to give us some more space and i think this looks a lot closer to the card that we're working with here so just adjusting that gives us a pretty good look and now we've got our font family styled here i think the next thing we have to do is figure out of that font family they had us download the font weight of 400 and 700 which means that some of these should have a font weight of 700 or bold so let's look at our design and see which ones that would apply to it looks like to me this 29 uh call out is the only one where the font weight is you know extra bold so let's come down and um because the default font weight is going to be 400 or you could also specify it in the root so we'll say 400 but that's once again going to be the default so it's not going to change but if we came down to our price right here we're going to have to actually uh put some identifiers on here to enable us uh or it let us select it with css so i'm gonna say price um price container and then this will be an id of price and then this will have an id of price frequency or something like that so now we have ids to basically select each of these little components here and if we come down to our css we need to set the price to a font weight of 700. so we'll come down to the bottom we'll set our price to a font weight of 700 and you'll see that this gets a little bit bolder so if i uncomment it it's not bold if i comment it back it's going to be bold and then while we're here just give us a ballpark uh font size let's go ahead and say 1.4 rem which is basically going to be 140 of the root elements font size which we know is 16 pixels so do 140 times 16 pixels and that's how many pixels we just made that font size too all right looking back we're getting closer we're not we're not too far off i think the next obvious thing that's going on that we're missing is this button because our button looks terrible in comparison so let's go ahead and make a button that is about the same size here and also has that background color we've already selected the button so that we could give it white text but now we can just go ahead and grab this color code that we used for the h4 up here and apply it as the background color to the button so we'll give the background color that light green you can see that there's by default this border that looks pretty terrible so we can set the border to be equal to none that makes that look a little bit better and then there's more spacing than we have here around this text so that's a good use for padding so all we need to do is come in and add we'll say 10 pixels of padding that looks roughly correct and then finally you can see that this has a border radius so the button has curves around the edges and it's equal to kind of the size of that container so remember we put some padding around the edges of this container so the new size that we're going to have is we should be able to just say a hundred percent i believe so let's say the width is going to be a hundred percent and yes that's going to get it get us equal to that parent container so at this point we just need to add a border radius of call it 5 pixels and i think our button is looking pretty good so it looks like we're a little bit wider than this right here but i'm not too worried about that we have the perspective pretty good and the most important part is that it's a hundred percent of the container so i think at this point we have all of the colors in place let's just check that yes we do so we have all the colors that we need and our font family is correct now we just need to do two more things we need to to get the font sizes and font weights a little bit better and then we also need to get everything spaced out in this price and the frequency horizontal to each other i'm going to go ahead and say that this top section is completely done it's not you know perfectly aligned with this design but it's very close um and part of this is we're looking at a much larger version of this so um we're never going to get it like exactly perfectly it's just not worth our time but down here let's go ahead and start working on this bottom left box so this monthly subscription that looks okay maybe a little bit heavy in font but i'm not too worried about it but there needs to be some space between it and the price so let's target this right here and put a margin at the bottom of it and the way we can do that is go find in the html the tag that we want so the bottom left section and like we did in the top section we can just target individual elements so we'll target that and then we want to use a combinator to say that we want to target the h4 potentially let's see what we have here so we have the monthly subscription as an h4 and that's the only h4 in this entire section so we're pretty safe to target it with the combinator and now we want to give it a margin on the bottom of call it 20 pixels i think that's pretty good maybe a little much so maybe we'll go with something like not 5 but 15 pixels okay i like that and now the next part is we're going to take this per month and drag it up onto the line that the 29 dollars is on so to do that we once again target the bottom left section and actually no we're not going to target that we now have the this individual price container so that we can work specifically with this layout and what i want to do is grab the price frequency right here so we'll target the price frequency and i want to display this as an inline block because i want it to come up on that line uh next to the 29 dollars and at the moment it's not doing it because the 29 dollars is not an inline block so we can target both of those we can say price frequency um and the actual price to both be inline blocks and now they're coming onto the same line and since we have both of those elements within this div container the price container we can just say on the entire price container that we want a margin on the bottom of call it 20 or 10 pixels what you'll see here is that the per month is kind of aligned to the bottom of this 29 dollars in a quick way to do this you know if we were using flexbox this would be super easy but a kind of a quick hack to change this is set the position of this element to relative which then allows us to basically if you remember from the first video of the css crash course we talked about how relatively positioned elements you can set them basically relative to themselves so if we give it the top property and then we want to bring it upwards so we basically want to do a negative amount of pixels and we say we want to bring it up maybe three pixels you'll see that that per month kind of goes up a little bit so watch the per month and i'm going to uncomment or i'm going to comment this out it's going to go back to the bottom and then i'm going to put it back in there and it goes up to the middle so i kind of like how that's sitting and then furthermore on that price frequency the font size should be a little bit smaller so we'll give it 0.8 rem so that's a little bit closer and then you'll also see that it has a little bit more space between the 29 dollars and it's also a little bit lighter so it's not quite as punchy of a white color so all we would have to do here is maybe give it a trans or not transparency but we could give it an opacity of maybe 0.8 and that makes it a little bit lighter for us maybe even 0.7 okay i like that and then finally a margin to the left of it of maybe five pixels and you can see that spaces it over to the right so i'm pretty happy with how we've got that spaced out that's relatively relatively close in style and position and i like the size of the 29 dollars so i think we're done with that little sub component of the top of the bottom left container next up are full access for less than a dollar a day that's a little bit large in my opinion um because it looks like this text is even a little bit smaller than this paragraph text up here so we probably want to make this a little bit smaller in relation to the paragraph text the way that we'll do that is this is the only paragraph element within the bottom left section so once again we will grab the bottom left section and maybe even just copy this rule right below here and target all p elements which will be a single element and give it a font size of maybe 0.9 rem units and that gives us a nice size maybe we'll make it even a little bit smaller so 80 of the the original text size of 16 pixels and then finally we just have to bring this button all the way down to the bottom so we have a couple ways of doing this but one of those ways would be you know taking this p element and just putting a margin on the bottom of maybe 40 pixels and that should bring the button down now flipping between these two i'm seeing that uh this these text right here is kind of in the middle of the top in the button so i'm going to change a few of these alignments here i think we need a little bit more margin below the monthly subscription so let's add maybe 25 pixels instead of 15. and that gives us a nice little layout there so it's not perfect not exactly the same but it is fairly close and we've gotten all of the functional and layouts pretty good here so at this point i think we're done with the top we're done with the bottom left and now we just have the bottom right which should be relatively simple to do so the why us piece is already pretty good i like how that's sitting it's in alignment with monthly subscription same size as it so so that's good and now we just need to take the bullet points off of our list item and align those two uh to the left of each other so let's see that how we would do this so to really show you what's going on here let's open up the chrome dev tools and try to just rearrange this so that we can see everything it's a little bit weird debugging html and css in a codepen environment because you've got all these different windows open but really what this right side elements is doing is looking at this html page so if we were to click this selector up here and come down and grab the entire list that we're looking at you'll immediately see that there is some sort of you know padding and margin around this list by default so the the margin is fine that doesn't bother me at all but that green little box on the left side of all the text is some default padding that is added to list because generally you want those lists to kind of be indented from the main text so that's what you would want but here we don't want that so if we go over into the style right here and we basically say padding is equal to zero you're going to see that everything goes to the left and it lines up with the text like we want it now the last thing is there's bullet points that we don't want there and you can actually set the list style property and set that to none and by doing that all of the bullet points go away and so those are the only two changes that we'll have to make to get this to work so let me go ahead and refresh the page so we get this reset because those changes we made were actually temporary and then we'll come down to the very bottom and since there's only one list in our entire html document we'll just target it by tag and we'll say the padding is equal to zero and the list style is none so we've removed the list styling by using this attribute or property and remove the padding on the left so it's all lining up just like our design here but what we are missing is this looks a little bit smaller and it also isn't so such an intense white color so it looks like the opacity of it might be a little bit lower similar to this per month piece of text so all we need to do there is say font size and we'll bring it down to what did we say this one was 0.8 rem so 80 of the root element size or 80 times 16 pixels and then we can bring the opacity down to 0.8 as well and i think that looks pretty pretty good there it's not exact but it is it's pretty much in line with uh with everything at this point i think we're pretty much done the only remaining item that i can think of is this button right here so the button is currently not doing anything when we hover over it and i want to change that especially because that was part of the requirements so here's our button styles in this block right here and in order to style it on hover we need to give it the pseudo class of hover and once we do that this was something that we talked about and i think the first lesson of the crash course and the first thing that we always want to do on hover is change the cursor to a pointer so you can see how the cursor changes as i hover over this button and that's just to indicate to the user that hey you can click on this so from here we also probably want to change the color when we hover over it because the cursor is not obvious enough so we could just take this uh background color right here that's already on the button and make it a little bit lighter or a little bit darker so let's grab that hex code and bring it into our color wheel of adobe color and let's just see if we can find like a shade of this that would work i think something like maybe this one on the right would be okay and if we bring that in and we change the background color to the hover state to that little bit darker color it's going to just have an effect where it's showing us oh hey you can click here and it's going to do something and to make that even more pronounced there's a property that you can use called transform and there's plenty of values that you can pass here but one of those is going to be the scale function and we can just say like 1.1 or something like that so we can scale it 110 and you'll see that it you know gets a little bit larger when we hover over it and that's that's even a little extreme maybe we'll just do 1.02 or 102 and i like that that's a nice little subtle indication to the user that when they hover over and click this it will do something now right now this button doesn't do anything but just for fun just to add something in there we can come up and use what we've learned in the prior lessons of this full stack road map and come up to the button and add ourselves an on click listener and we'll pass the function alert user which we have not created yet but we'll come down to the js section and say alert user and then we will pass an alert that says hey you clicked the button so let's finish that one up and now when we click this button it's going to alert the user that they clicked it so obviously not you know useful but it's a nice little addition to make this a little bit more interactive now the final thing that we could do here is make this mobile responsive and what i mean by that is if we drag this right side over to the left you'll see that it just disappears right here and it just flows off of the page which is a terrible user experience if you're on mobile so this would be about the size of a mobile device so if your user you know looked at this design this is what they're going to see which is completely useless to them so what you would want to do is make it a mobile design and as you see in the front-end mentor challenge it tells you that you need to design something like this now we haven't talked about mobile and responsive design quite yet and i'm going to cover that in a completely separate video i think that might be the next video after this one so for now we're going to keep it just desktop only but in future challenges future front-end mentor challenges we will make that we will make this mobile responsive now there's one more thing that i want to do because it's quick it's easy and it will give you at least a little bit of a win here in terms of being able to share this and show it off so if we come to github let me open up a new tab and go to github if you followed along with this full stack roadmap from the very beginning i had asked you to create a github account because you're going to need it as a developer no matter what it's just kind of expected of you to have a github profile so if you've already got one great if not go ahead and create one and let's go ahead and click create new repository right here and we'll say pricing card frontend mentor challenge is what we're going to name this repository and we're going to call it an implementation of the front-end mentor code challenge and we're going to make it public and you don't really need to initialize any of these but let's go ahead and add a readme i'll just show you what that is and you click create repository so we're going to go through version control and how to actually get this within the terminal a little bit later in the series but for right now we're just going to use github's ui or user interface to post our code and then deploy it to github pages and what github pages is going to do is it's going to allow you to host this challenge you just created on the web and share it as a url so here's our initialized repository every repo or repository is going to have a readme or at least it should and this should give an introduction to what this repository is all about and oftentimes when you're building a web app the readme will give instructions on how to run it locally and how to develop it and you know you get the point so within the repository we obviously could make files on our local computer and then use version control to push the files up to this interface but for right now we're just going to add a file and we're going to click create new file and this one is going to be called index.html so this will basically be all of the html that we just created and index.html is the default html document that github pages is going to look for when it's trying to serve it on you know the live url so it needs to be named this and from here if you were to go back to our html and copy it all and paste it in here we have the body section implemented but since codepen already implements the head tag and the um body tag that's not going to be here so we need to add those pieces in order for this to work so the first thing that i'm going to do or actually let's let's come back and write out that structure so basically we'll come to the top and we'll say that we need an html tag and we'll copy this one and put it at the very bottom and then furthermore let me zoom in a little bit so you can see what i'm working on and then after this we will need the head tag and we'll just we'll call it uh my front end or no we'll say pricing card will be the title which is going to show up in this little tab when we're viewing it live and then we'll close out the head tag we'll come back to this in just a second to add those fonts that we put in and then we've got the body tag which is going to be closed all the way down here at the bottom and then lastly let's just indent everything to a proper level and now you can see that we've wrapped everything together in this html the last thing we're going to need to do here is link up all of the style sheets and the fonts so if we go back to our pricing card go to settings html we're going to need all of that stuff that we had in the head and we're going to put that right after the title so you can see we have these two link tags and then furthermore we're going to have to link up our css stylesheet that we'll put in in just a second so the way that we do that is another link tag and i believe the href is going to be style.css which we're going to create in just a second and this will be a stylesheet so we'll close that off and what this means is basically i want you to pull in the file called style.css in the directory that is basically hosting this index.html file you'll see what that means in just a second so for now let's go ahead and commit this new file and go down to add another file and this one is going to be style.css which is what i just talked about how we linked up to that within our html and from here we should be able to just paste in all of this css right here that we had created so nothing nothing else to do here we can just go ahead and commit that file and then finally we need to add our javascript file so we added just a few lines of javascript down here but in order for that to work we need to actually connect it up to our html and the way that we're going to do that is go back to our html file click edit and then come down to the bottom of the body and this is where we need to have our script tag to do that we just type out a script tag which is not self-closing there's actually two pieces to it and we need to give it a source and we're going to say um let's say just index.js is what we're going to call our javascript document so commit those changes i believe that should work so long as we go to the main file here or the main directory and add that javascript file so we need to name this index index.js as we just referred to in our html and then we should be able to paste in that function that we created and commit the file from here this is a complete repository i believe unless i missed something but we'll find out in a second now the only thing that we need to do is add another branch now this is a little bit complex we're getting into version control branching but basically the rule here within github is if you make a branch called gh dash pages or github pages it's automatically going to look for you know the index.html file on that different branch and it's going to deploy it automatically for you so it does a lot of the heavy lifting and you can for now kind of just think of it as behind the scenes magic but we will later learn how this all works so we're going to create a branch called github pages and we're going to create it from the main branch so it's going to copy all the files over okay so at this point we've got all of our files in github pages and if i did everything correctly which usually does not happen in coding it should show up down on this github pages link so let's see if we click view deployment awesome it looks like our card actually did deploy correctly with the styles because it's all styled and if we click sign up hopefully we'll get the alert so awesome we have um our deploy link which you can just copy and paste this link up here and share it with anyone and they can see the project that you just worked on you could also submit this to front-end mentor if you wanted to to get feedback on your project so awesome we've got something deployed and shareable [Music] as you saw in the intro our goal as web developers is to avoid our users hurting their eyes after they look at our web application on a desktop device a tablet a mobile device or whatever they're viewing it on let's take a look at some of the prerequisites it's basically just html and css which we covered for the most part in the prior lessons of this series now let's talk about this for just a second on what we're going to be covering here because this is a massive topic that i cannot do justice to if i'm not defining exactly what we're going to be learning i'm going to start on the right side of this screen and as i said i am not the best designer in the world graphic design is generally a painful experience for myself i've put together a few decent designs over the years but nothing that you know i could call myself a designer in any form or fashion but the good news is as a full stack web developer you do not need to be a great graphic designer for that reason i'm not going to teach you the design side of things and as we've talked about previously in the series we're going to be going through and making some of these challenges with html css and even javascript so this is frontend mentor and this is one of the designs that we're going to create after we learn about css grid and flexbox over the next couple videos and you can see that there's a responsive design here we're going to talk more about that in just a second likewise we have this challenge right here which is just another looks like crowdfunding page so this will be a fun one to do and what you'll notice here is that as we're going through our course here we're leveraging existing designs we're not trying to do this on our own and that is the point of this video is to teach you how to leverage those existing designs and make sure that everything is responsive and looks good on all devices that you might see it on so here are the lessons that we're going to learn in this video first off what is responsive design now you probably have a general idea of that but we'll dive a little deeper into it next we'll be talking about mobile first versus desktop first designs and honestly it took me years to figure out what this actually meant which looking back is is kind of silly because it's not that hard but we're going to talk about that so mobile first versus desktop first next up we'll talk about css breakpoints how they relate to media queries in how we actually write media queries within our web apps and then finally i'm going to leave you with a responsive design checklist this is not going to be super detailed just a high level checklist that you can go through to make sure you're covering your bases and a little project what we're going to do is take our design from a previous video so it was the previous video i'll link to it up here where we designed this pricing card and what you'll see with this pricing card is as we resize the screen it just gets cut off so i'm going to teach you in the challenge at the end of the video how we can take this situation and turn it into this one where as we resize it gets close gets close and then boom it breaks and it looks a lot better on a mobile device something that your users will not be angry about let's go ahead and dive into that question of what is responsive web design and the answer to that at least in my head i like to keep it simple so that we can remember it if your users hurt their eyes looking at your web page on a desktop device or a mobile device or a tablet or whatever they're viewing it on then you have not designed it responsibly so that is a good little way to remember what responsive design is all about now here's the thing it's 2021 and there's a lot of website builders there's you know squarespace there's wix wordpress a bunch of other website builders that kind of take care of this responsiveness for you and for that reason most websites nowadays are responsive and they look pretty good so i had a hard time actually finding a bad example of responsive web design for that reason i created one that's not going to be anything crazy but let me close out a few tabs here and what i created was this you know very basic example of bad web design but a few things that you'll notice here is as i make it larger and larger so this is a pretty big screen i'm working on you'll notice a few things so first off as my eyes read across this text i have to cover a lot of real estate on the screen to actually get through one single line of text and that's kind of a no-no when it comes to responsive web design you want that text to be you know generally less than we'll call it 700 pixels um so it needs to be you know in the middle of the screen and needs to be narrow enough for your eyes to not have to basically move great distances to read it so that's one piece of this another piece is you see this nav bar up here we've got a couple links on the left and a couple on the right and they are very spaced out to the point where it's it's almost too spaced out so those are a couple issues going on with this on a very wide screen now if we make this more narrow you're going to see something that is just going to hurt your eyes which is not the point of responsive design so as we get smaller this could be a mobile device of some sort you'll see that we have two columns here and that's not going to work on a mobile device you want to have most content to be you know full width on a mobile screen and then also you have all of these links up at the top and the contact link is almost getting cut off to the point where you can't even click it or see it or anything so that's a big problem if we were viewing this on a mobile device now what i did was i went ahead and improved it a little bit so this is the improved version of this design where you'll see on a wide screen the article text is only a certain width i think i made it something like 650 pixels wide as a maximum and you can see that we can skim this text without you know moving our heads from left to right and then also you'll see that the navbar has has been brought in so these links are not you know at the very edges of our page and then as we resize this you're going to see that everything changes so right around here i believe somewhere right there so you'll see at this break point this column goes under the main text which is going to be a little bit easier to see and then also you'll see the links at the top are going to turn into a navigation menu that it doesn't do anything because i didn't want to spend the time to do that but this would be like a drop down so that you could see the links there and then i left the home link so that you can still navigate that way so even on a really small device you can see that you know most of this looks pretty good now obviously this is not the most visually pleasing design in the world it's not meant to be it's meant to show you generally what responsive design is to get a better grasp of what this is all about let's take a look at really good examples of responsive design and i'll point out a few things that are good about them the two portfolio sites we're going to be looking at are cassie evans this is just a really nice looking site and jason lanksdorf i think i said that right i don't know either of these people but i have found their portfolio sites and they're just really visually pleasing they've done a really good job at responding to different screen sizes so as we look at this let's go ahead and break this this window out so that we can kind of resize it but let's just take a look at how these websites react to different screen sizes so here is cassie's website and this is the home page you'll see that as we resize this to a more mobile device that big text is going to get broken out to the main portion we're going to get rid of that image right there so it's a little nicer to see on a mobile device and then as you scroll down you'll see everything is kind of full width if we were to bring this back to a desktop now it's got a nice little layout so you kind of get the point this is just a really nice example of good responsive design right here likewise jason's site is pretty good in this respect so you'll see there's this picture here on the left on a desktop device and as we resize it's going to get put on top the button gets a little bigger so it's easier to click on a mobile device and as we go smaller and smaller you're going to see that it's really easy to navigate around if we click the blog you'll see it's the same thing on a mobile device it looks really nice and everything is easy to click on a desktop it's also really nice and you can see that it's in the middle of the screen this was what i was talking about earlier you don't want your text to be the full length of a desktop device because some of them are going to be huge screens and you'll have to move your head to read the text in the first place next up we have to talk about mobile first versus desktop first web designs when i was first learning i'll be honest i could not figure out what the difference between these were everyone was talking about it and saying oh you want to do mobile first mobile first but i never understood what they were talking about and it wasn't until i realized that these concepts of mobile or desktop first design has nothing really to do with the design phase of a project and it has everything to do with when we start writing our css so it's really important when you're you know learning all these concepts to really separate the design phase from the coding phase of a project you don't want to get into the situation where you're trying to design something and code it at the exact same time because after all designing something is very right brained and coding something is very left brain so you're going to get to the point where you're just in complete confusion if you're trying to do both at the same time so my rule for myself at least is before i write any code whatsoever i need to have at least a basic visual representation of what i'm trying to create but if you're a very mediocre designer like myself or if you just have no design skills whatsoever what do you do you know do we actually try to design something or do we just hire someone to do it that's a question that i was asking when i first started in my opinion you really have three options when it comes to you know getting that first visual representation of your web page in front of you number one you can hire a designer and have them design it for you but in most cases especially if you're watching this you don't have the time to go figure out what designer you want to hire and then furthermore you know you don't need to pay anyone to learn how to code so that's kind of out of the equation that's for later down the road when you're trying to actually build a product and sell it and you need to have a really professional design build the second option is to browse the internet for free designs and that's what we've done with the front end mentor challenges this is a great way to kind of outsource that step for free and what you can see is we've got these designs that we're going to be creating already built for us and we don't have to think about them at all as we scroll through this you'll see that there are desktop versions here and then there's also mobile versions so that you can see how do we want this to look on both sides of screen if you're not super worried about what you know your end product is going to look like but you want to still make it mobile responsive you can put together a wireframe and you can use a tool such as figma so this is a really popular place to put together a wireframe or even a mock-up which is a complete representation of what you want to build but usually pencil and paper is good enough for a basic wireframe and your goal with a wireframe is to just get a general idea of where the elements that you have on your page should be arranged based on the size of the screen so i'll put something up on the screen just a basic wireframe that i created for we'll just call it like a blog where you have a sidebar and you have some regular content and you can see a desktop version and a mobile version and this is entirely sufficient if you're just trying to get a basic understanding of what you're creating now like i said if you're trying to build out a product or something or if you're a great designer you can use a tool like figma which is going to allow you to put on put together full-on mock-ups and the difference between say a wireframe and a mock-up is a wireframe doesn't really care about you know the colors and the fonts all that detail it just cares about kind of the layout of the elements on the page now a mock-up is a full-on representation of what you're trying to build so a tool like figma or adobe xd or sketch or a couple other different good tools for that can allow you to create full-on mock-ups like the one that we're looking at here with front-end mentor so this has not been coded yet but it's a perfect visual representation of what we do want to code you can do that with those tools so coming back to the original question what's the difference between mobile first and desktop first design you see with all these mock-ups we have a version of the desktop and mobile on both so does it really matter which one you create first well the answer to that is you're asking the wrong question now this took me a long time to realize but mobile first and desktop first has nothing to do with the actual design phase it has everything to do with when you start writing that css and when you put together your break points so in order to understand these better we have to dive into something called a media query with css and along with that concept comes the concept of breakpoints to better understand what i mean when i say breakpoint let's look at a code pen that i put together to explain it now if i said i want a breakpoint at 600 pixels what i mean by that is as the screen gets smaller or bigger than a width of 600 pixels i want to have different css rules for both of those so in other words if i'm on a mobile device or a desktop device i want different css let's do a quick review so let me come to a page that i'm i'm not really using here so let me close out a few of these this is just the web docs for css we're going to right click and inspect and then go to the console and if we were to type window.enter with it's going to give us a number and that number 714 is the visible width of the screen so as we resize this so we're kind of pulling it to the left and right and resizing it we can run this again and you're going to see we get a different number so now it's only 356 pixels wide so that's what we talk about when we say the the viewport width so what i've done is i've written a little bit of javascript here to make this slightly bigger for us to see and what i've done here in the bottom is basically said when it resizes the screen i want to change the content of this up here so i've got a span an html span that has a number in it and that's going to be populated by the window.enter with property every time the screen resizes so in effect you're going to see as i drag this this number is going to update live and also what i've done and we're going to learn more about what this is is i've written a break point at a minimum width of 600 pixels and i've included some different styles within that break point so one of those is going to be the h1 tag is going to have a color of red on screens that are bigger than 600 pixels or equal to and then when it is not so a mobile device we're going to have a color of green so you'll see as i resize this we get closer to 600 so we're about to hit it and when i hit 600 it turns green so we'll go back up turns red back down it turns green so that is the basics of a css breakpoint now of course changing the color of some text based on the screen is not really that useful and you probably won't see it all that often but what you will see is css that affects the layout of your html changing on different screen sizes let me go ahead and uncomment um some of the code here that i've created and what i've put in here is a couple of pictures these are just placeholder pictures from the placeholder dot com and then we come down into the css and you can see that when we have a screen size that's a mobile screen size so when we say mobile first it's all these rules at the top we have a width of 75 percent and as we come down into this break point of 600 pixels you're going to see that the image width is going to be auto which basically means it's going to fill its container so in effect our break point at 600 pixels is going to say okay if it's 600 or greater i want to have these image images sitting next to each other otherwise i want to put them a hundred percent width of its container and they'll stack on top of each other so as we resize this we get closer and closer to 600 it's going to change so watch what happens when we get below 600 right there you'll see that the images went to 100 percent width or not 100 percent width 75 percent width and they're going to stack on top of each other because they no longer fit next to each other on the screen so that's a good example of a basic css breakpoint and how we can use it to rearrange the layout of our page so you have a basic idea of what a breakpoint is in css but the question becomes what break points do i actually use and there's not really a simple answer to that let's take a look at this web page right here this is screen sizes dot es there that's pretty clever you're going to see that there are a lot of different devices that we could possibly build our web page for and there's no way that we can go through all of these there's hundreds and hundreds of different devices there's no way that we can optimize for all of them so we have to do our best at guessing you know which screens do we want to set our breakpoints on and design them a little bit differently in general mobile devices are going to be somewhere in the range of 320 to call it 480 pixels wide you'll have tablets between 600 and 800 pixels you have you know regular sized laptops around the you know 1024 to 1440 pixel range and then bigger desktop devices are going to have a width of call it 2500 pixels so those are just generalizations but as you can see on the screen there's all sorts of different numbers and i don't want to get into the discussion of things like pixel density and what is the difference between the actual width of a device and the adapted pixel interpretation for us as web developers it's a pretty complicated topic if you can just remember those guidelines that i just mentioned you should be good so as you're deciding what breakpoints you need to use for your web page or your web app you have to ask three questions in my opinion so the first one is going to be will the web page even be used on a mobile device and is that your intention there are plenty of apps that are not meant to be used on a mobile device so think about budgeting apps or you know big spreadsheets things that just don't look very good on a mobile device you probably don't want to waste your time energy and maybe even money to create a pleasing mobile experience you're just going to say to your users hey this is not meant for a mobile device you should be using our application on a desktop another consideration would be are you offering a mobile application if you are you know a good example might be something like youtube so the youtube application on a mobile device is really nice to scroll through and use but their web version so if you were to open up the browser and then type in youtube.com rather than opening up the mobile app it'll still be a semi nice experience but it definitely won't be as great of an experience as opening the mobile app so if you're creating something and you plan on making a mobile application you may not have to put quite as much time and thought into how it looks on different screens the second question you might ask is is it actually important to have a different design for each screen if you've just got a basic website that just has text on it and you don't have a lot of images or anything like that then it may not be that important to have a bunch of break points you may be able to get away with one design for all devices and it'll work just fine and the final question you have to ask is are there standards for this you know why can't i just use the industry standard for what everyone is using for break points and unfortunately given the fact there are so many different screen sizes and screen sizes are constantly changing as new physical devices are released to the world there's not really an industry standard that we can go off of well there are no industry standards per se there are these things called css frameworks and as i talked about in the prior videos we're not going to be covering in depth any one css framework given the fact that i don't want to lock you into any one thing i want to teach you the fundamentals of css rather than trying to teach you a framework but having a framework to piggyback off of when it comes to break points is a very smart decision at least in my opinion these guys that create the frameworks are thinking about you know break points all the time they have to incorporate those break points into their own frameworks and therefore if it's good enough for them then it's good enough for me and hopefully everyone watching this given the fact that break points are not an exact science and these guys think about it a lot more than you know someone like myself would one of the most popular frameworks or at least long-standing css frameworks is bootstrap now bootstrap you'll find it in several tutorials you can kind of read up on it it's basically you know a css framework is all about making your job writing css easier so a lot of times these css frameworks are going to create grid systems in layout systems so that you can just have predefined classes that you can pass in to make your sites mobile responsive and stuff like that so with that they have their standard set of break points and if you scroll down to um on bootstrap's documentation it has a list of these break points and you can see that there are about five of them so by default bootstrap is what we call mobile first and you're going to start to catch on to what this means and when i say mobile first it means that we are first designing our css for a mobile device and then what we're saying is anything greater than 576 pixels will have these styles and then anything greater than 768 will have these different styles so basically you're starting with the mobile device you're writing your css and then you're writing little blocks of code with these break points to define oh what happens when we have a bigger device so that's kind of what we mean when we say mobile first so here are the break points got 576 768 992 1200 and 1400 for small medium large extra large and xxl so those are the standards for bootstrap and they're going to be our standards but the question is do we have to use all of them and the answer to that is no we don't need to use every single breakpoint here we just need to use enough breakpoints to make our designs look suitable on all devices and based on what you're creating that may be one or two break points or it may be all of these break points that you have right here if we're looking at an example like this pricing card that we created i'm not sure really what happened here but anyways we have this pricing card and for this we really only need one break point now if you're incorporating this into a application you might have a couple more but really we just have two states that we have to create we've got this full on state which can be on basically tablets and above and then we have this mobile state where everything's stacked on top of each other so in this case we only had to come down here and write one single break point so we have the min width being 576 which i got from right here so anything greater than a small device or a mobile device is going to have x styles all right so we've been in theory land here this whole video and we've been talking about the the design aspects and break points and how do we choose them but we haven't actually written any code yet and i want to do that now i want to show you exactly how to write a css breakpoint and how that relates to this thing called media queries so i'm going to come to my code pen and make a new one so that we can just have a playground to work in and then down here in the css this is where we're going to write our breakpoints but we first need to have some html so we'll say responsive web design example and from here let's just put in an h1 welcome to the tutorial all right so we have some html to work with and now down in the css section we can write a breakpoint and the basic syntax of the css breakpoint is this so what i copied in here is a what we actually call a media query and the breakpoint is the number that we pass in as a minimum width to that media query now we have a little bit more to learn about the syntax because we have to ask ourselves what does this at media actually mean and if we go over to the mdn documentation let me close out of here this is what we've been using so far in this series and you can come down to technology css and then if you come to the css reference so in the css reference the first paragraph has all sorts of links that we can go to and it says use this reference to browse alphabetical index of all the properties pseudo classes pseudo elements data types functional notations and at rules and this last one the at rules is what we want to click on so what these are they're css statements that instruct css how to behave so that would be fitting into our categorization of media queries perfectly so if we scroll down you'll see down here in the index these are all the different at rules or whatever tells the css how to behave and if we click on media that's going to take us to the media query page so this is an at rule that can be used to apply part of a style sheet based on the result of one or more media queries all right so then we come down to the syntax and you're going to see some pretty complicated stuff being written but this is the basics of a media query and if we come back to our example here you'll see the basic syntax just has at media and then in parentheses we write whatever rule we have and then down here this is where the css is going to go so if we wanted to specify some css for our h1 we're going to put it there so any time the screen is greater than 756 pixels the text will be red so if we shrink this it goes back to black and then we go to red so that's the very basic syntax but in this rule we're actually selecting every type of media device so if we go back to the documentation and let me drag this over so we're not getting so confused with our tabs but if we come to the media rule and scroll down to the description you'll see that there's this concept of media types so we have all print screen and speech and you can actually define within your media query which device types you're actually trying to target now in our case when we wrote this one we're just saying we want all devices now you can also specify in that rule that you only want to target certain devices now we're going to be working with screens mostly that's like a mobile like an iphone or android a tablet or a desktop screen so that's kind of our forte as web developers but you also have print and speech which are some more specific use cases that you can target so let's see how we could change our rule to target those devices so if we said media all and this is going to say that we want to target all media types so this is the equivalent of this those are the same thing but if we come in here and we say media print and this is only going to apply to print as basically like a print preview view on a screen so as you can see our rule down here of the color red no longer applies to this text because we're only targeting print media devices likewise we could put in speech and that's also not going to work because this is not a speech media type but if we put in the word screen it's going to now apply because this is a screen and this rule should apply to it now with these media rules we can add some logic in here i'm not going to get too deep into this but i want to at least show you what it is so let's say that we wanted to target only screens at a width between 400 and 600 pixels now we could do that in one media rule and the way that we would do that is by writing some logical operators within css so if you remember with javascript we came down here into the javascript section and we just typed in some basic code we can use conditionals so we can say we can create an if statement that says if my var is greater than 10 and my var is less than 100 then we'll do something within this code so you can see that this little and symbol is the logical operator that we're using now css actually has logical operators just like this so we don't need this javascript code anymore but we can rearrange this rule to have those logical operators and target multiple conditions so let's go ahead and do that let me get rid of this comment the first thing that we're going to do when we're having these logical operators we have to put this operator called only so only when it's a screen is when we're going to use this rule and then we can drop down onto some new lines just to separate these things out we'll put our brackets down here and now we can add another one here so we can say and max width is something like 800 pixels so let's just change this we'll go what did i say we wanted we wanted something between 400 pixels and 600 pixels and now what we're saying with this rule is so at media so we want an at rule for css we only want to target screen media types which is what we're looking at here and we want to target something with a minimum width of 400 pixels and we want to target one some something with a max width of 600 pixels so basically what we're saying here is only screens between 400 and 600 pixels should this h1 css rule apply so if we make the screen really big that's not within those bounds but as we go down and you can down here right at the bottom you'll see a little pixel indicator as i scroll but as we get between 400 and 600 we should see it turn red and then we go back to black as it gets smaller than 400 so you can see how we can use css logical operators with our media queries to target different types of conditions now you can also bring in other css at rules so if we go back to our documentation we'll click back this is our our at rules page and you can see we have media queries but we also have things like import and supports so if we click on supports this basically is an app rule that lets you check to see if a specific css property is supported in the browser that the user is using so a good example of this is flexbox and css grid which we'll learn about in the next few videos those are not yet supported by all browsers now most of them do but things like internet explorer i don't think do and maybe a couple others with css grid so you can see in this example we can use this to basically check whether the display supports display grid property and then if so we will use that property within our css so as you can see media queries and css at rules can give us a whole lot of power when designing our pages for different devices media types and different browsers so we're not going to get too far into it because that's not the point of this video but i did want to show you the basics of it just so that you were aware now at this point i want to take you through we'll call it the 80 20 guide of media queries and css breakpoints i'm going to show you how to take these standard breakpoints that we have from the bootstrap framework and incorporate it into an actual css document to do that let me put some images on our page so we'll say welcome to the tutorial and then we'll have a bunch of just placeholder images with on the screen so what we want to do with these images is make them responsive based on the screen size so something that i can think of is maybe on mobile devices we want it to be you know 100 percent with just like we're seeing here where they're vertically stacked then maybe on tablet devices we can have two images per row and then on a desktop device that is much larger we could have uh we'll call it four images in one row across the page as i just described we want to have three different states of our web page we want a mobile display a tablet display so two images and a desktop display four images so we have three states and that requires two css break points to achieve now you'll remember bootstrap has you know five of them we only need two of these because we're not making a super complex design and that is a good demonstration of how you have to look at you know what your purpose is and then decide how many break points you don't want to just throw in a bunch of break points just to have them there the first thing we need to do is put in a basic css reset now we talked about this in prior videos of this series so be sure to watch those if you haven't but it's just changing the box sizing so that we can fit these images reliably next to each other without any unwanted behavior the next thing we'll do is we'll add in our break point templates if we go to bootstrap and look at what we need here i think what we're going to do is grab this one right here so i just want to mention here this top one this is not actually a break point that we need this is kind of just the default so as we talked about this is a mobile first approach where we want to design the regular css or the default css to apply to a mobile screen and then we will conditionally style any screens that are larger than a mobile device with these breakpoints so we're going to grab the greater than or equal to 576 so that's basically saying what happens on anything greater than a mobile device and then we also will probably grab the 992 which is basically saying everything greater than say a tablet so mobile tablet and desktop is all we need to cover so we'll say 576 and 992. let me just jot this down here so 576 and 992 so i don't forget and now we can write our media queries so like i said you can just use this basic syntax you don't have to put any of those logical operators in there and we're going to say min width is 5 76 pixels and then put some brackets to break it down we will copy this put it right below and change this number to 992. we can get rid of the comments here and we should be set up to have our css breakpoints now now right now the pictures on the screen no matter what size screen we have are not going to be responsive now you'll see that they come next to each other on this larger screen and that is just because naturally an image is i believe an inline block element which means it doesn't have to have its own line and these are i believe 500 pixel uh images yeah we put in 500 pixels for these images using the placehold placeholder service and therefore if the screen is bigger than say a thousand pixels so 500 plus 500 those inline blocks are going to wrap up onto the line so this is nothing of css that we've written it's just the natural flow of html so in our mobile first approach let's go down to a mobile device which is going to be right here and what we want to do within this css we'll go right after the css reset and what we will do is we'll target all images on the page and we'll give them a width of 100 and what that's going to do is it's going to resize all of the images to be 100 of the viewport and you'll notice there's a little bit of padding that's just natural a default padding and margin of this browser in the codepen environment but we have these as a hundred percent width now as we resize this you're going to see that it maintains that 100 width all the way up to these larger screens and of course this is a way too large of an image to have on the screen so we want to wrap these on you know next to each other on the lines so what we'll first do is we'll come into this media query which is basically going to say anything greater than a mobile device is what we want here so we'll target images and what this is going to say is if the screen is greater than 576 pixels we want to override the original image style and put it to something else so in this case we'll give it a width of we wanted 2 per screen so we'll say 50 percent and now we should see these wrapping um no they're not wrapping let's change it to 49 percent okay there we go now the reason we have to do 49 this is not going to be a problem once we learn flexbox and css grid but it's just uh white space and a couple other things that are causing this so um don't mind the fact that it's 49 it should technically be 50. so as as you see here on a mobile device we have 100 width and they stack on top then as we resize to be greater than 576 they will break onto two or two per row and then now we want to make it four per once we get above 992 pixels so right now it's going to stay as 2 per row no matter how big we get but if we come down here and add an image style and give it a width of 24 percent you'll see that as we get larger it's going to put 4 per row so now we see we have one per row here two per on a tablet and four per row on a desktop device so we've successfully written a mobile first approach to css and we've designed for three different screen sizes now i want to walk through how the css interpreter is actually working here because sure we can write these break points but we really have to understand what's going on otherwise we're going to make a silly mistake that i'm about to show you so looking at the css we have to ask ourselves how do we actually read this or how would the the browser be interpreting this so by default we've got all these images here and when css is red we'll we'll first read the css reset we'll make the border box um different and then as we come down we'll say okay i have an image css selector giving it a width of 100 so the css interpreter is going to say okay great the width of all images is now 100 now as we come um further down you'll see the media queries and it'll the browser will basically say so is my total width of the viewport greater than 576 pixels let's go ahead and make that true so now we're at the biggest size and so as the browser's reading this it says okay yeah it is bigger than 576. so let's go ahead and apply this rule so it says all right now images are not 100 we're going to replace that with 49 and then it reads even further down and it sees this next media query and it asked it it basically asks itself okay is it greater than 992 pixels the viewport width of the browser the answer to that in this case is yes so it's going to say okay now my images need to be 24 width and we're going to replace these previous two rules up here so what we're doing is we're walking through that cascading concept of css where it's read from top to bottom and the last rule so long as that last rule has the same specificity as rules above it is going to be the one that applies so where this is important is the order of the breakpoints that you're adding or the media queries so if i were to take this last media query and place it above this one things are going to change for us and that is because of how the css is read from top to bottom so in this case what you'll notice is on a mobile device we look good it's working fine but as we go up we're only going to get two images per row no matter how far or how wide the screen gets and you might look at your css and say well why is that we've defined that anything greater than 992 should get a width of 24 percent but the way that css is read is top to bottom and what it's doing here is it's saying okay first we have this rule here that sets it to 100 then we're going to ask ourselves is the screen bigger than 992 the answer is yes so now we're going to set it to 24 then it reads down once again and it says is it greater than 576 and the answer is still yes because this is inclusive of all screen sizes so now it's going to replace that image width with 49 and override the 24 right here so the lesson here is that your media queries the order of them is very important and you're going to need to do two things so first you need to add your media queries at the bottom of your css style sheet and the reason is you've got all these rules for the default style which would be the mobile screens and you need these at the end so that they can override those styles and then second if you're going with the mobile first approach which is basically using min widths rather than max width then you need the greatest width to be at the bottom so you need to go from you know smallest to greatest otherwise what we just saw is going to happen and nothing's going to behave as you'd expect so at this point i want to revisit this topic of mobile first and desktop first design i've alluded to it over the last couple of minutes what this actually is but i think we're ready to fully understand the difference between the two like i said earlier it has nothing to do with the design phase of your project when you're just getting the graphic design laid out in your wireframes or your mock-ups doesn't really matter because you're going to design multiple screen size designs no matter what but when we come to write the css the difference is going to be whether we're using media queries with a min width or we're putting in media queries with a max width because that's going to change how we think about our css entirely in this case we used min with which is basically the equivalent to mobile first design because all of the default styles up here are going to automatically apply to screens and since we're using min widths that's going to apply to mobile devices now as we go up in size that's when we're going to start replacing css properties according to those bigger screen sizes so what you see here is mobile first and now i'm going to reverse it and we're going to actually do a desktop first approach which is not what i recommend but i want to show you the compare and contrast so if we wanted to go desktop first then we would just have to change these two to max width all right so let's change both min widths to max width and you'll see that everything is working a little bit differently it's basically the exact opposite so as we get bigger we go to that 100 width and as we go smaller we go into the grid which is obviously not what we want and the reason is because we thought of it from a mobile first perspective when we built it so basically when you're doing desktop first all of these default rules are going to apply to your desktop devices so anything that is greater than this the greatest break point that you've assigned is going to get all of these default styles so we're basically starting from the widest and then as we go down we're replacing rules so if we wanted to work this the same as we had before we're just going to have to change the widths so all of these rules are for you know desktop devices so we want to replace this hundred percent with 24 that should get us back to the point where we have the four four pictures per row now as we come down we're going to change the max width of 576 so this is basically referring to all mobile devices we want to make this 100 so that on a mobile device we see them stacked on top of each other and it's not working let me go ahead and refresh the page to make sure we got this oh actually this is a good lesson so when we go from desk or from mobile first to desktop first the order of the media queries is going to change so when we had min widths we said we want the smallest to largest going from top to bottom but in this case it's going down this style sheet and it's saying okay is the screen less than 576 so let's make it a mobile it's saying is it less than 576 the answer is yes so let's apply an image width of 100 percent then it goes down and it reads is the max width or is it less than 992 in this case that is also true so it's replacing the width to 24 so that's why we're still seeing the grid there so if we wanted to make that work we need to put for a desktop first approach we need the largest media queries first going to the smallest and now we have our intended behavior of 100 with on mobile and then we should get let's go ahead and replace this last one here to be 49 and now we should have a correct design so 100 on mobile we'll have two images on a tablet and then we'll have four images on the desktop so as you can see mobile first versus desktop first is all about how we're writing our media queries and whether we're using mid widths or max widths in general you're going to find that the consensus among the development community is to use a mobile first approach and the reason behind it is as you're getting into more complex designs it is just easier to use the natural flow of html on a mobile device so no media queries required and then tweak it as you get larger because in many cases a desktop first design is probably not going to look very good on a mobile device but the inverse of that so a mobile first design it will probably look okay on a desktop device without any modifications so in summary you're basically going to have to write less code in most cases all right so we've covered the basics now the next question is what do i actually put within these media queries and the answer to that is i can't possibly teach it within one lesson you're going to learn you know how to resize things based on different devices through experience and time i'm about to show you a basic example of just that but first i want to leave you with a responsive design checklist so let's jump over here and go to the checklist so you can just you know take a screenshot of this write it down whatever you want to do but this is what i go through when i'm looking at my design and my web page and testing it on different devices i want to make sure that i check all of these boxes and if you can check all these boxes you've covered 95 percent of what you need to and the rest is going to be you know just subtle little changes and highly dependent on how many requirements you have for your project first up we want fluid layouts this is basically saying you know what we just looked at where we have you know single images on a mobile device we have a row of two images on a tablet in a row of four images on a desktop so that's what we mean when we say fluid layouts we want to make the content you know just look good in general now images we want to make sure that we have the right aspect ratios and we're not stretching images to be you know too wide too tall too big on certain devices we also want to do the same thing with text we don't want to have huge text on a mobile device where you only have you know four or five words per line that's a really poor user experience the next thing up is overflow so that's basically you don't want to have any text or images or whatever cut off on the side of the screen so you've probably seen a mobile design before where you know half the image is off the screen you want to avoid that and then similar to that we want full width mobile content so in general this doesn't apply to everything but in general everything that you look at is going to be stacked on top of each other rather than having columns you never really want columns on a mobile device and then finally just general usability so this is not too hard just get a friend someone who will give you honest feedback have them play around with your web page on multiple devices and anyone is going to be able to tell okay this doesn't feel right this one's kind of clunky it's hard to use this on a mobile device you know i'm squinting on the desktop to see this you should get pretty good feedback and just using a common sense approach is usually pretty good for this all right at this point we're going to apply these skills we just learned and we're going to take this pricing card challenge where you can see that everything's getting cut off and it's not responsive and we're gonna make it responsive so the answer to this is actually relatively simple and we can do it in one of two approaches so the first way we could do it is a desktop first approach which like i said i don't recommend that as your default way of approaching css but in this case we did design this with a desktop first mindset we didn't really think about mobile design when we were doing this and therefore in the css and html that has been reflected so in this case you can see that the width of this card that we've created we decided that as we were creating the card and you can see it right here so we've assigned a width of 600 pixels to this and for that reason i think we can assign a media query or a break point around that 600 pixel mark now if we did it exactly at 600 pixels then you're going to see a design where you know we're at 635 pixels here and looks like about 600 right here and obviously there's something cut off so you want to put a little bit of a little bit of wiggle room there so we might want to write our media query to be somewhere around 650 pixels so what we'll do is we'll come down to the very bottom of the css as we're supposed to with media queries we'll write at media and then we'll say max with because we're solving this in the desktop first approach because this will be the simplest solution and we'll say 650 pixels so dropping down into this css rule all we really have to do is make these bottom the bottom left and the bottom right containers that we created we want to make them 100 width so that they naturally stack on top of each other so i believe all we have to do if i'm thinking about this correctly is target those two and give them a 100 width when the screen is less than 650 pixels so the ids for this would be bottom left section so let's grab that throw it in our media query and paste that in and then we want to put a comma because we want to also do the same thing for the bottom right section so let's grab that and put a hashtag to target it and then we'll make a css rule so right now in this media query we're saying anything any screen that's less than 650 pixels i want to target the bottom left section in the bottom right section by their ids and then i want to give them a width of a hundred percent all right so at this point our screen is greater than 650 but as we resize you'll see that it's done something for us but now we have to figure out what's going on here because we've completely lost the bottom right section it's pretty much disappeared and the reason that that is happening is because we've set explicit heights on this card if you scroll up to the the top of the css you'll see that we gave the card a width of 600 pixels and a height of 400 pixels and then we also set the overflow to be hidden which was what allowed us to make those rounded corners at the bottom so what's happening here when i resize this and we're basically losing this right side entirely is because now when we stack them on top of each other it's greater than that height of 400 pixels and we're hiding that overflow content so it's not being shown here so if we were to come down to our media query and set the height to be auto it's going to change something for us i believe no that's not going to change because we don't want to assign that to the the sections we want that to be to the main card so we need to target the card itself so down in our media query we're going to target the card and now we're going to set the height to be auto which is going to take away that explicit height and give it its automatic height and you can see that now we are resizing correctly but the last problem is you're going to see it's still cut off because for this card component we still do not have an automatic width so we've only assigned a 100 percent width to the bottom left and bottom right section so if we were to assign the width here to auto or even a hundred percent we should see that it resizes now appropriately so i think what we can do is we can actually take since we've duplicated this with we can take this height auto and we can put this up into this rule and then we can just include the the main card within that and delete these and i think it should work just fine so let's resize it and you can see that it works just fine and you can see that all we had to do was add this little block of code to make it mobily responsive but i don't want to leave you hanging on this one i said throughout the whole video we want to go mobile first mobile first so i want to stay true to that and show you how to make this mobile responsive from a mobile first perspective or strategy this desktop first approach worked pretty well because we originally created this from that approach but ideally we would have created the original card from a mobile first perspective so i'm going to show you how to convert that so let's delete our media query that we just created you'll see that it goes back to being cut off and not being mobily responsive and now what i want to do is create that mobile first design so instead of a max width we're going to write a media query where we have a mid width which is going to be 576 pixels we'll just go with that so anything greater than a mobile device is going to have um whatever styles we put in here now the cool thing about you know media queries and break points is that you don't have to duplicate all of your css a lot of styles are going to be applied similarly across devices so for example the color of this button in this text in these backgrounds that's not going to change based on whether we're on a mobile or desktop device so we don't need to rewrite those likewise our text is going to stay the same we're going to have the same font style no matter what so really the only things that you have to alter between break points is going to be layout related properties so what i want to start with is the overall card so if you remember in the top part of the css we have a rule for card and right now we're giving it an explicit width and an explicit height now if you remember from a mobile first perspective everything that's not within the media query itself is going to apply to a mobile device so naturally when we're on a mobile device we do not want these explicit widths and heights so let me go ahead and comment those out for a second and what you're going to see is that we've already done a lot better so the the overall card is the right width and height now but we've got some problems going on within the bottom left and bottom right sections now we don't want to get rid of these widths and heights altogether so let's go ahead and take those out of the the main card and then scroll down to our media query and we'll target the card element and paste in those rules again so now we want to give it a width of 600 and a height of 400 on any device greater than a mobile device or 576 pixels so let's go ahead and resize this and it should break somewhere around here so right there you saw it so that was where our break point was and you'll see that based on the explicit width and height we've given it it's not really fitting correctly so we can actually change this it should work just fine so maybe we'll say something like i don't know 550 pixels so that gets it fitting on the screen and then the height can go ahead and stay at 400 pixels so now as we have our mobile device it's somewhat responsive and then as we go to anything greater than a mobile device it stays as this card the next thing that we want to look at is the bottom left in the bottom right sections so here are the styles that we have for those and you'll see that we've given them widths of 50 each so in this case when we're on a mobile device we do not want these widths to be 50 we want them to be 100 percent so let's go ahead and comment this out and when we do that it's going to have the default width of auto which is going to be 100 so by default it's going to be 100 and if we comment out both of these i think we should start to see something that makes a little bit more sense so you'll see that we have some auto actually no i said that wrong we cannot just use it as auto we need the the text to span 100 width of its container so in these styles we need to i guess go ahead and set a width of a hundred percent so that fixes this section and then we'll come down here and set a width of 100 and that should pull this one to the right all right so we're looking good there and i think for the most part we have our mobile design complete so i think this is exactly what we want so our mobile design is complete we just need to make this desktop design look a little bit better because as of right now it obviously is missing an entire section so really we can just let's go ahead and delete the width of 50 percent even the commented out section here and then we'll go down to our query at the bottom that says anything greater than a mobile device and we're going to target the bottom left section and the bottom right section and here we're going to give it a width of 50 percent just like we had before and now you'll see that it fits just perfectly and as we resize it it goes it resizes perfectly on a mobile device so that is the difference it's very subtle especially because our project is very simple there's not you know hundreds of styles but the difference between a mobile first and a desktop first design is really important and you really have to internalize that as a full stack web developer in this video i'm going to teach you how flexbox works we'll start off by going through what's available to you what it can actually do and then we'll spend the rest of the video which will be the the majority of it diving really deep into how flexbox works how do we calculate different things and use all of the properties my goal for this video is to be the most in-depth flexbox tutorial on youtube and the reason i'm making it so in-depth is because it's important i think that not only knowing flexbox allows you to eliminate a lot of the frustration when it comes to layouts in css but it'll also set us up for our next lesson which is going to be on css grid so let's get started this is a flexbox crash course and the prerequisites for it is going to be a basic understanding of html and css i've covered all of this in prior lessons of this series so go check those out if you're unfamiliar here's what we're going to be covering in this video we're going to learn what flexbox is how can help you out and make your life easier we're going to go through all the basic properties of flexbox and we're going to get pretty detailed in those so that you really understand what's going on there and then finally we're going to look at a front-end mentor project this testimonial uh grid over here which we're going to be creating with flexbox in the next video a few of the things that we're not going to be covering shorthand flexbox properties so there's a couple properties within this css i guess module or framework or whatever you want to call it that allow you to write less css to achieve the same thing and that's not our goal here we're trying to learn flexbox you can apply those much later down the road when you're more comfortable with flexbox and we're also not going to be getting very advanced with this i have written advanced flexbox properties but we're pretty much covering all the properties i guess probably the better way to say that is we're not going to be going into advanced flexbox methods and you know different layouts and all that kind of stuff my main goal is to get you understanding how the system works and get you very comfortable with using it the obvious starting question is what is flexbox well if you have been following along you might have realized that as we're writing our css there's really two main components you have layout related properties and then you have style related properties so a style property would be like a color or a font or something like that it doesn't really affect how things are laid out on the page but layout properties do that's how you create you know grids and you know nav bars and footers and sidebars all of that kind of stuff so far we have not been using flexbox and our job has been a little bit difficult because we've had to use uh you know hacky ways of getting layouts to work flexbox is going to come in and really help you out with that layout side of things and per the mdn web docs flexbox is called the css flexible box layout module and it's a model for displaying items in a single dimension so either a row or a column in future videos we're going to get into css grid which is a two-dimensional layout module within css and some you know might say that css grid is better than flexbox or vice versa but they really work together and we're going to cover how those work together within our css whatever the case flexbox and then subsequently css grid which again is a future video these are kind of the future of css layouts so in the past like i talked about you had to use like floats and clear fixes and inline blocks and specific widths and if you watched one of the previous videos we had to almost you know hack our way to getting items to fit within a container by removing white space from our html these are not good solutions they're not sustainable and so flexbox along with css grid come in to provide a better model for doing layouts but enough talk let me show you exactly what we're dealing with here so what i've got here is some basic html and i really want you to start understanding this container child relationship within html so if we look at this we can see that we have a div element with a class of container and then we have three div elements all having the same class of item this relationship is very important the container is the parent to all three of these you know children elements to show you how this relationship is important let's put some width and height dimensions we'll go with 100 pixels for the width and height of the items within this container and we'll give them a background color of say orange and then maybe we'll give some margin of 5 pixels so they're kind of separate a little bit so here you can see the three div elements these are the children elements to that container and the reason this parent-child relationship is so important is because the second you enable flexbox on a container all of the child elements become what we call flex items so this is really important and first off let me show you what happens when we have the normal display property so right now by default each item here and the container are all going to be a display type of block so they're block elements and you'll see as i type these in it's not going to do anything because we're basically just explicitly writing in what was already the default now with block elements as we talked about in prior css videos of the series these are going to have certain characteristics and flex items and flex containers are also going to have certain display characteristics so if we remove this display block which again these are just defaults for div elements and we come up to the container and we type display flex you're going to notice that these items change and the reason they change is because by adding the single property on the container element or the parent we are basically making a flex container which holds a bunch of flex items the container and the items have specific um characteristics that you know normal block or inline elements do not have in just a minute i'm going to explain exactly why these flex items are going in a horizontal direction rather than vertical but for now i really want to drill home this point about flex containers and flex items now you might ask well if i set display flex on this container and all of these are going to be flex items then can i also you know make the flex item a flex container and the answer is yes you can actually have an element be both a flex item and a flex container at the same time and that's why it's important to always keep these relationships clear in your head let me show you what i mean so if we come down to the final div element which currently is just a flex item and then we put in a few more divs so let's put in a class of sub item and then we're going to copy this down three times and right now this doesn't have any dimensions but if we copy this css selector of item down and then we put in sub item and give it some different dimensions so we'll give it something very small like maybe 15 pixels wide and tall and then we'll make the background color blue and a margin of 1 pixel something like that and what you'll see is that these line up within that third container because they're children of this third flex item but at the moment these are not flex items themselves they are block elements by default this brings up a very important point so if you look at our css we have put the display flex property on this main container and when we do that it only turns that container into a flex item and then the direct children not all descendants but only the direct children will become the flex items so in this case all of these divs are going to be flex items but the sub items are not going to be flex items these will be the default block elements now what i can do is i can come in and turn this final div into a flex container which will then subsequently turn these sub items into flex items as well just so that i have a unique selector on here let me turn this into item three we'll put another class on there and then all we have to do is come down here on item three and write display flex and what you'll see is these three blue boxes right here are going to go in a row just like the orange boxes are as we go throughout this tutorial i want you to keep this clear in your head this exact model that we just laid out here so let me just walk you through it one more time at the top level we have a div called with a class of container this is a flex container and the reason it's a flex container is because we put the display flex property on it now all of the items these three items right here these are flex items and the reason they're flex items is because their direct parent is a flex container now these sub items right here these are also flex items but it's not because we have display flex on the main container it's because we've put display flex on this third item right here i know this is confusing right now it's going to get a lot more clear as we go through the tutorial but what's happening here is this third div within the container is both a flex container and it's a flex item so it can actually serve as both types at the same time in relation to the main container it is a flex item in relation to these sub items it is a flex container with that level of understanding between the container and flex item and how we can combine those here are the flex container and flex item properties that you can use we're going to go through each of these but you have to keep them very clear in your head when you're going through because you have to first identify am i dealing with a container or an item and as you saw in some cases you can be dealing with both so you can actually put a combination of these properties on a single html element sometimes this gets a little bit confusing and something that i had done when i was first learning flexbox is i had this cheat sheet that i found here it's a really great resource i'll be sure to link to it in the description but i just keep this open whenever i'm working with flexbox because it's very interactive you can you know click on different things and and see how different flex properties work and it covers pretty much everything now as i just mentioned some properties apply to the container and some to the items this resource does not really separate that out on the left side right here but for each property you'll come down and you can see what it applies to so this one applies to flex containers so i'd use this resource keep it open as you're learning flexbox it's a really good you know little cheat sheet to have in addition as we've been going through in this course you can always look it up on mdn web docs so as you can see we're on flexbox and here's the property reference for all of the valid properties that we can use and you can click on each of them and kind of get an overview of what what's available and what i'll point out is this formal definition down here for each property is going to tell you what the initial value is and sometimes that's important to understand what the default values you're dealing with are the reason understanding these default values is so important is because when we have um you know flex items and flex containers like this putting this single rule on a container is going to set a bunch of properties by default that you're not seeing you know in front of you in reality when we put display flex on this container it's setting all of the properties that i just pasted in here so it's setting a flex direction justify content align content items in flex wrap and these are all of the default values so you can see that nothing changed over here on the left because they were already applied by default likewise flex items have default properties so i can come down to the item itself which is going to be a flex item and paste in all of these properties right here and the reason nothing changed on the left again is because these are the defaults that we you know that flexbox comes packed with what we're going to do in this tutorial is learn how we can modify the default values on all of these uh properties right here to get the intended result that we want the last thing i want to point out before we jump into our first property is just a quick note on shorthand flex properties so if we came down to the flex item so this is targeting one of our flex items here and we put in something called flex and we gave it a value of uh let's see 0 1 and auto this right here is what we call a shorthand flex property and it is the exact equivalent of writing flex grow zero flex shrink one and flex basis of auto so these three properties written out longhand is the same thing as this single property written out shorthand and for this tutorial like i said we're not going to be looking at these shorthand properties because we have enough to deal with with the longhand properties just understanding how they work so just wanted to clarify that because you might see that in the flex cheat sheet so if you came down here flex flow is a shorthand property i'm sure flex is somewhere on here but whatever the case just know that those exist but we're not going to be covering them i think at this point i've covered all of these you know prerequisite pieces of knowledge to really jump into flexbox let's go ahead and get started we're not going to use this because it's pretty ugly and i actually put together a little demo here so you can check out the javascript for this it's basically just toggling some of the values here and what this demo is meant to explain explain is the main tenet of flexbox and that is the fact that we have one dimensional one-dimensional model where we can align items on either the horizontal or vertical axis as you'll see in the html we've got some some html up here none of this is really important what we're really focused on here is going to be this piece right here so we've got our main container which is going to be our flex container and then each of these divs are going to be the actual flex items and i've actually styled these a little bit just for you know visual purposes but it's not important to this tutorial so if we're looking at this html the way that we create a flex container of this div is by selecting it with css and writing display flex on it so by default that's going to create a flex container and then all of the direct children are going to be the flex items so all three of these divs right here now the next property that we have to understand and this is a really important part of flexbox is the flex direction property and this is by default going to be set to row what this property is defining is what we're going to be using as the main axis and what we're going to be using as the cross axis i've written this out with html and css so you can see that this is the main axis horizontally and this is the cross axis vertically but what happens is if you change this flex direction property you're going to change the flow of the items within the flex container and when you do that that's also going to change the main and the cross axis so by default this is set to row which means everything is going to go horizontal and the main axis is going to be horizontal but if we were to toggle this flex direction which is going to via javascript change the flex direction to column then what's happening is the main axis is going to be vertical and the cross axis is going to be horizontal now so now we're looking vertically well previously when it was set to row we're looking horizontally so as we learn new properties keep two things in mind first off as we talked about you got to know which html element is the flex container and which are the flex items number two you have to know which direction the flex items are going to flow in so if you've got this property flex flex direction set to row they're going to go horizontal and the main axis is going to be horizontal and if you set it to column it's going to flow um vertically and the main axis is going to be vertical as you can see in this example our flex container is has a certain height and width and the reason is because we set it down here so if we find the container that we set it on we set the width and height to 300 and 200 pixels now if we comment these out we get rid of the dimensions on the container it's going to act similar to a block element where it kind of spans the width of its parent container and then the height is going to be based on the content of the items within it to keep things simple we're going to keep this width and height set on our container just so that we can have a nice visual to look at as we explore some of the different flex properties for the remainder of the tutorial we're going to be working here in this workspace and i want to just kind of derive how i got here just so you're not confused with pre-existing styles that i put on here so what i have is basically four div elements within a main container and as of right now what i'm showing you there's no flex box enabled here except on these items right here but that's not what we're trying to cover so these are basic divs and as of right now they're displaying as block elements and there's no you know dimensions on the container so let me kind of derive how we get to our workspace first off i'm going to set a width and a height on our main flex container and i'm going to put a border on it so just uncommented those properties out and what you'll see here is a black border that is 4 pixels in dimension and then the width itself is going to be 408 pixels on the whole container the reason that it's 408 is because if you add the left border of 4 pixels in the right border of four pixels that gets you eight pixels and therefore what i wanted to do was basically make the total area within the container uh 400 pixels exactly so that we have a better time doing you know basic calculations so that's why i made it 408 pixels now we have to uncomment the display flex to make these flex items so now you're seeing that they're sitting you know on the horizontal axis and that's actually because of the flex direction row default setting here and this is how we get to our basic workspace and then finally i will uncomment these two lines of html which are going to be styled somewhere down here so that we can see the cross in the main axis at all times and as we go through the rest of the tutorial we're always going to be working with a flex direction of row so therefore the main axis is always going to be horizontal and the cross axis is always going to be vertical if you want you can switch this and call it flex direction column and you can see how all of these rules that we're about to go through work out from a vertical axis perspective the next few properties we're talking about are alignment properties which allow you to align and space out these flex items within the container the first one is going to be justify content so if we come to mdn here's the formal definition and we'll come down to the formal definition here and see that the initial value is normal if we read about what normal is it says the items are packed in their default position as if no justified content value was set so that's kind of what you're seeing here it's all just kind of by default at the start of that axis now similarly if we set the justify content property to start this is going to achieve the same thing so we're just putting it at the start so for all intents and purposes you can think of the default property for justify content to be start if we wanted all of the flex items to go to the end of the main axis rather than the start we can just type that into this property so if we replace this with end you'll see that all of these jump to the right side and you'll see that as we're looking on the main axis that's how we're deciding where these items are going to go we're flowing horizontally so the start is the left side and the end is the right side we can also change this to center which is going to put them right in the middle and then we have some additional property values that will actually space the items out a little bit the first one is going to be space between so if we type space between these are going to be spaced out with equal amount of white space between them within the container we also have a property called space around so if we change this to that you'll see that there is now some space surrounding each of these items on the main axis now what i'll have you notice here is that the amount of space between the items so this little area here and here those are all going to be equal in dimension but the edges so to the right of this fourth element into the left of the first that's a little bit smaller than the space between the elements so that's kind of the how we distinguish the space around property value with the space evenly property value so when i type space evenly you'll see that these change just a little bit and now what's happening is every piece of white space between the elements are going to be exactly equal so the justify content property tells us how the items are spaced out and aligned on the main axis let's go ahead and set this back to start because i want to have the default values so that now we can talk about the next property which is the align items property let's go ahead and look this up in mdn we'll type in align items and you'll see that this is the property that we use when we want to align flex items on the cross axis so remember we set this example to a flex direction of row by default and that means that the main axis is horizontal and cross is vertical so the align items always applies to the cross axis and is going to decide how these items are aligned vertically looking at the mdn docs you'll see that the initial value is again normal and if we go to normal wherever that is i guess it's at the top here it says the effect of this keyword is dependent on the layout mode we are in as you can see in the third bullet point it says for flex items the keyword behaves as stretch and if we come down to stretch it says the flex items are stretched such that the cross size of the item's margin box the same as the line while respecting width and height constraints that's an overly confusing way to say that by default the align items if we set it to normal is going to stretch the height of these items to the total height of the flex container unless you have a specific height set on those items already so this brings us to a brief discussion on the width and height properties of flex items right now we don't have any width or height set on either or any of these flex items right here so they're getting i guess what you call the defaults and with flexbox a flex item is by default going to get the width of its inner content and the height is going to stretch to be equal to the total height of the flex container as you can see the inner content is going to be the numbers that we've put in here and i know there's a little space there but it's still considered just the inner content and then the height is of course the height of the full flex container now if we come down to flex item number one and we give it a width of say 100 pixels it's no longer going to have that default width of the content it's going to respect that width that we've set on it likewise if we give it a height of say 50 pixels it's going to respect that height as well so if you put explicit width and height properties on your flex items by default they're going to get those so long as there's not you know overflow within the flex container or you've set different properties that we're going to talk about in just a bit just as a quick review the items here the flex items do not have explicit width or height properties and therefore the default height that a flex item is going to get is going to be the total size of the cross axis so in this case the main axis is row cross is column so total height of that column is going to be the natural height this is no longer the case when we start putting in different values for the align items so once we put in something like flex start you're going to see that all of these items pull up to the top of the cross axis and they no longer have that total height of the cross axis and what's happening here is they just have the width and height of the inner content so if we change that inner content then it's going to adjust the size so one way we could do that let's say that we have this first flex item we'll come all the way to the bottom and i'm going to uncomment this little rule that i wrote that made the font size for this first item much larger and you can see that as we make the content bigger or smaller the flex item itself gets bigger or smaller because we don't have those explicit width or height properties now i'm going to leave that there because it's going to bring up a very good point here in a second so right now we have flex start we can also bring these down on the cross axis to flex end so these are at the end now and then we can center them with center and this is actually something that's really hard to do without flexbox so really cool uh part of this flexbox model what you'll see with the center is that there's like an imaginary horizontal line going through the middle of the content in these flex items but we also have this property value called baseline and what's going to happen here is we actually draw that horizontal line kind of at the at the bottom of each piece of content within the flex item so because this first one has much bigger inner content our flex baseline is going to be in a different spot so they're not going to be perfectly aligned flush with the top of that cross axis so far we've learned a couple of properties justify content and align items so we can play around with these let's just do a few examples so that we can kind of review and recap so let's go ahead and say justify content will be center and align items will also be center this is how you can take content flex items and put them in the middle of a container and this was very difficult to do before flexbox so this is a really cool advantage of having flexbox and then we can of course change all of these so we can put the justify content to end and put that there we can also put align items to flex end i don't know why you'd want to do this but you can put in the bottom right corner of the container so very flexible no pun intended of what we can do here with these alignment properties let me reset everything let's just put these back to the defaults so all you have to do is delete them and they're back to their defaults and then we will come down here and comment out this last rule that changes the font size of that first item so now everything is equal and what i want to do is actually make each of these items a specific width and height we'll go with 50 pixels by 50 pixels one way that we can do that since we have two classes set here and each flex item has this fi class we can just target that one class give it a width of 50 pixels and a height of 50 pixels and now we have these perfect little squares sitting as our flex items by default if you don't change any properties flexbox will respect the flex item width and height properties so we set those to 50 pixels by 50 pixels and that's what we're getting right here and if we came down to the flex container again and we set that align items property to say stretch or something like that you'll see that they're not actually going to stretch down to the total height because we have those explicit properties set already but the next thing i want to explore is what happens if you have too many or too little flex items within your flex container in this example if you remember from a few minutes ago when we talked about the actually it's at the top here we set the width of the flex container to 408 and the height to 200 pixels because the main axis is horizontal as a result of the flex direction equal to row property the width is really all that we care about right here because everything is going in the width kind of direction so we set this to 408 exactly for the example i'm about to show you if you've got a container 408 pixels wide in that container has a four pixel border on the left and the right then the total white space that is available for these flex items is exactly 400 pixels if each of our boxes are 50 pixels in width that means that we can fit a total of eight flex items perfectly within this container let's go ahead and do that just to see what i'm talking about so all we have to do is copy this down four more times and you should see a bunch of items pop up here let me just go ahead and change the labels so that we can see all our flex items a little bit better and now you can see that we have eight items perfectly fitting within our flex container but what happens if we take all of these flex items and we double it so let's say instead of 8 flex items of 50 pixels each we have 16 so we double it what happens then well let me go ahead and do that real quick i'll just copy all of these items and paste them below and what you're going to see as we extend this is that these items are overflowing outside of the flex container there's a couple weird things going on as well if you'll notice it doesn't look like these items are 50 pixels wide anymore now of course if you were wondering how wide these elements are you don't have to guess you can right click inspect element and you're dropped into the firefox dev tools and the reason i'm in firefox i know i've been switching back and forth between chrome and firefox throughout this series but i'm in firefox because the devtools have an awesome flexbox little module here that you can inspect elements with and really see what's going on so all we have to do is come up to this inspector here we click that so that we can select items on the page and let's first start with our overall flex container when we click that flex container the layout tab is going to drop us down into this section called flex container if you collapse that you'll see that there's also grid in the box model so these are different you know models that we can look at but if we look at flex container and then we toggle this little button you can see all of the different flex items outlined for you as well as some overflow space and other elements of the flexbox we'll turn this off for now but we can also go down to the flex items and you can see they're being highlighted here on the left as i hover over them so you can click them with this little inspector here so maybe we click on number eight and we can look here within the inspector of everything that's going on with this individual flex item we're going to talk more about all of this stuff in just a bit we're not quite ready yet the thing that i want to draw your attention to is the fact that if we go down to the box model which we learned about in previous lessons we know all about how this works and you look at the total size of a single element so we'll click on number four you'll see that the dimensions are 33 and a half by 50 which is not what we set them to we set them to 50 pixels wide and 50 pixels tall so the question is why is it shrinking the width like we didn't tell it to do that why is that happening furthermore if these are overflowing outside the container what do we do about that we obviously don't want to show this to our users we don't want our flex items just going indefinitely off the page and especially if you're making say a grid of images where there's hundreds of images and you're using flexbox to display them you don't want them going on a single axis you know indefinitely to the right or indefinitely down in this situation you have two options the first option is kind of the standard default way of handling overflow in css and this does not this is not specific to flexbox itself you can use this with any type of css display type so if i came to the flex container and i set the overflow property i can set this to a couple of values but usually you might want to set it to auto and if you set it to auto what's going to happen is these elements are no longer going to break outside of that container they're just going to scroll so you see the scroll bar at the bottom of our flex container now and we can see all of our items they're still shrunk a little bit but we can see them all without you know breaking out of the container but like i said with that grid of images example if we could imagine that these are images we don't want our users to have to scroll left and right to see all the images we want them to just wrap down to the next line and just have like a nice little grid of images so the second option that you have here instead of setting the overflow property which is just a generic solution we can set this flex wrap property and if we set it to wrap you'll see what happens is these elements go back to their you know original size of 50 by 50 as we intended and they're going to perfectly wrap onto the next line now you see some white space in here here in the middle and you might say okay i don't really want that i want those to be flush to each other so naturally what you're going to do is come down to the flex container you're going to type in a line items like we had said earlier and you'll type in flex start something like that and you won't see anything happen and the reason is because the rules of the game just changed when we put in this flex wrap property i know you're probably getting tired of hearing me say that but as we go through css certain properties set to certain values changed the whole game just like when we set flexbox to the container you know it changes the behavior of all the children elements within it this flex wrap property changes how we align items on the cross axis so instead of align items we now have to use this property called align content so if flex wrap is set to anything other than the default which is going to be no wrap you'll see it overflows again if it's set to anything except for this no wrap then we're going to use instead of align items we're going to use align content which is going to have the same possible values for the most part as align items but it's going to tell flexbox how to arrange this new like group of wrapped items within the flex container so now we can set flex start or we could wrap them at flex end or even put them right in the center of everything so that's how we wrap items to the next line if they're overflowing at this point we've actually covered all of the flex container properties that we can use just to do a very quick review i'll get rid of all this extra stuff here so get rid of these last eight flex items and then i'll get rid of these last two properties here so just to review we have of course the display flex which is going to activate flexbox we have the flex direction which we can set to either row or column so if we set it to column it's going to go vertical if we set it to row it's going to go horizontal we have justify content which aligns flex items on the main axis in this case the horizontal axis we can set that to center which is not going to change anything because they're it's as wide as the container itself but if we just commented out this last flex item you'll see how this works it kind of centers all of the content in the middle and then we have align items we can also put center on that to put those in the middle next up we're going to talk about how do we influence the size and alignment of individual flex items so if you take a look at what i'm about to do i can say something like a line self and i can say flex start and you're going to see this first item drops out of the flow of the rest of the flex items and we can do this with each individual flex item for a lot of different combinations here speaking of this alignment let's go ahead and start with that because i think it's the easier part of this discussion so i will let me just go ahead and comment out the last four items here in our container just so that we have four and then within our rules here i'm going to remove these justify content align items so that everything is top left default settings now if we came down to individual items which i have css selectors here for on the right what we can use is this align self property which is essentially the same exact thing as a line items except it only applies to a single flex item so in this case we've set this to flex start which is the default so it all looks normal but if i take this first flex item so the first box here and i set this to flex end you'll see that this drops to the bottom while the rest stay at the top and i can do this to flex item number four so align self flex end and you'll see this one dropped to the bottom and then maybe i wanted number three in the middle so align self center so this one goes in the middle you can see how we can individually pick and choose where our flex items are going to go and of course our container level properties still apply so if i came back up to the container and set justify content to center this whole group will go to the middle so these work together it's not one or the other you use them in tandem like i said the align self property is pretty straightforward once you know align items on the main container now the last part we're going to talk about is the sizing of flex items and this is probably the hardest part of flexbox in my opinion to understand how all of these work and what we're dealing with here is the flex grow flex shrink and flex basis properties we're going to start with flex grow and this is going to apply to flex items when they don't naturally fill the entire main axis so in this case the main axis is horizontal because of the flex direction row property and in this case we have 50 pixel wide flex items and we have four of them so this should only fill about half of the total width of the container earlier we talked about how the total white space the total width for these flex items in this container is exactly 400 pixels if each item is 50 pixels wide then 50 plus 50 plus 50 plus 50 is going to be 200 pixels and therefore the occupied space is going to be 200 pixels and the unoccupied space is going to be 200 pixels when there is unoccupied space or empty space along the main axis of a flex container the flex grow property is going to come into play by default this property on each and every single item is going to be set to zero and what that means is that each item by default is going to take up zero percent of the empty space within the container that is why what we're looking at here is not stretching out to fill the container right now is because all of these items by default are set to not take up that extra space but if i come down to the first flex item and i change that default property to a one you're going to see that the first item fills up all of that remaining space so it's going to take up the original width it had of 50 pixels and then we also know we had an empty space of 200 pixels so the total width of this first item or first flex item now is going to be 50 plus 200 or 250 pixels now of course with my tutorials i don't want you to just take my word for it so let's go ahead and verify what i just said 50 pixels plus 200 is 250. so if we click the selector up here select that element and go down to the box model you'll see it's 250 and a half pixels and the reason it's a half of a pixel more than what we calculated i'm not sure that i can fully explain it with the utmost confidence but i will attempt to so if we set this back to flex grow of 0 and refresh the page because it's being stubborn and then if we inspect the element we'll go to this first element and what you'll see in the box model is that the inner content of this so the number itself is 28 and a half pixels so if we hover over this you'll see that the box gets drawn around that number now if we pulled in the same exact html in css but in google chrome so i'm going to pull in google chrome right here and we're going to select this first box and scroll down to the box model you'll see that the inner content is 28 not 28 and a half so browser differences are of course a big part of web development but i wouldn't worry about that extra half that we saw when we're trying to make the calculations just make sure that you have the understanding of how these are calculated in programming being off by one is sometimes a catastrophic error but when we're dealing with uh css it's not quite as big of a deal so we will we will stick with this and as we go throughout you're probably going to notice that my calculations are going to be off by just a couple decimals but just bear with me so let's set this back to a flex grow of 1. so we have 200 pixels of empty space now we don't because 200 pixels got allocated to flex item number one now if we came down to flex item number two which by default has a flex flex grow value of zero and if we set that to one here's what's going to happen if we came down here to flex item number two uh the default value for flex grow is going to be zero no changes as you see but if we were to set this to one you'll see that number 2 element gets part of that empty space as well so now 1 and 2 are going to be completely equal now how do we get to this you know what kind of calculation is happening here i'm going to try to draw it out for you so if we come to right over flex item number one just make a comment what we're going to do is take all of the flex items within the container and we're going to add up their flex grow values so by default remember they are are all set to zero so flex item number one has a value of one as we set it right here let me close this comment so we can see it correctly so we have a value of one on flex item one a value of one on flex item two and then by default the last two are set to zero and zero so the total of all that is going to equal two and what you can think of this as is two units of empty space so we'll say two units and you might ask okay what is what unit are we measuring this against well if you remember there is 200 pixels of empty space and if we divide that by two allocated units we get 100 pixels per unit so each unit that we allocate is going to be 100 pixels but if we were to come down to flex item number three and set a flex grow value of 1 that changes it because now our total sum we're going to add a 1 in there and now we have a total of three units we still have 200 pixels of empty space but now we have to divide that by three units and that should get us to i'm not sure if i can do that math i think that's a decimal actually so 200 divided by 3 is going to be 66.666 pixels but if we come down to flex item number 3 we set a flex grow factor of one and then flex item number four and set a flex grow factor of one now everything changes so we have to update these values in our sum so now it's one plus one plus one plus one so we have four units and we still have 200 pixels of empty space to allocate because if you have four items each 50 pixels wide that's 200 pixels empty space is 200 pixels in a 400 pixel container so still have 200 pixels of empty space to allocate over four total units so we'll define divide that by four and that's going to get us to 50 pixels each per unit so what's happening here is for each item in this container we have an original size of 50 pixels and then we allocate 50 pixels to that of empty space so they're each going to be 100 pixels equal across the board and you can see that if you click on here it's 100.133 pixels forget about the decimals here but they're all equal so that's how that works now it doesn't have to be a flex grow value of one you can also give it two or three or four it's all just dependent on how many units you want to allocate to each flex item let's say that we gave flex item number two a value of we'll say four or maybe maybe not four let's go with three so that we have an even number so now our calculation is one plus three plus one plus one is going to be 6 units 200 pixels of empty space divided by 6 units i'm going to need a calculator for that one so 200 divided by 6 is 33 pixels per unit so if we are giving the second flex item three units of empty space in each unit of empty space is 33.33 that means that we're giving it an extra you know 99.9999 or 100 pixels so we take the original width of 50 we add about a hundred so we get 150 pixels wide for the second item let's check that see if we're in the ballpark and you see that it's 150.25 so that calculation worked perfectly hopefully that explanation made a little bit of sense just have to think about it in allocated space empty space and you can calculate the two pieces by getting the total width of your flex container and then the total width of the items within the flex container and then breaking it out into units based on the amount of empty space that we need to allocate back out to the items now the opposite applies when we have too many items in our flex container let me get rid of all these flex grow values so that we can get it back to normal and then let me also uncomment these divs right here so that we have eight total items we know that that fits perfectly but what if we put in nine so instead of eight we have nine which means we've added an extra one in there which means that we have 50 pixels of overflow so remember each one's 50 pixels wide 50 pixels times 8 is going to be 400 pixels which is equal to the total available space within our flex container so by adding a ninth we are making the total amount of flex items 450 pixels wide versus the 400 available space so we have 50 extra pixels of overflow similar to how we allocate out empty space to flex items we can also reduce the size of flex items by the amount of overflow that we have so if you look at this container you're going to see that by default they all fit within the container when we add that ninth item and the reason is because all of them actually shrunk a little bit when we put put that ninth item in there the reason that they shrunk is because by default every flex item is going to have a flex shrink value of one so if i put that in for flex item number one you will not see any change because that's already the default it's already shrinking by one unit so we know how to calculate those units you basically take the overflow just like we took the empty space with the flex grow so if we have 50 pixels of overflow let's let's change this right here to 50 pixels of overflow and then we have to ask ourselves how many units we're allocating that across by default each flex item is going to have a flex shrink value of one so that means that each flex item should be allocated equal overflow in other words we have to do one plus one plus one all the way to nine so we have nine total units i'm just going to get rid of this because you get the point here so we have nine total units with 50 pixels of overflow so we divide 50 pixels by nine and that's going to give us i believe 5.33 pixels per unit let me check that real quick so 50 divided by 9 equals no it's not 5.33 it's 5.55 pixels per unit in other words we're going to take the original width of these items which was 50 pixels and we're going to subtract off 5.55 pixels from that 50. so in theory each item should be 44.45 pixels wide so let's inspect the element we'll go to any of these here and you're going to see 44.5 pixels wide pretty much exactly what we just calculated now this is where the the firefox devtools comes in handy so if you click on one of these items it's going to pop up in the layout section exactly how this shrink operation worked so you can see the flexibility has a flex shrink of one and that's going to say minus five and a half pixels which is what we just calculated so it's going to take the 28.5 pixels which is the inner content width uh subtract off five and a half pixels to get down to 23 pixels and then you add back in the natural padding and border that um is set on these items to get your total width of what we calculated as 44 and a half pixels now this brings up a good point because if you look at this model right here you're going to see that each item has a minimum content width so if you try to shrink something too much then it's going to make it so that it will kind of get capped out it won't shrink past the minimum value of its content i'll show you exactly how this works all we have to do is add in a few more a few more flex items so let's add one more for good measure and what we see here is that they're clearly breaking out of that container now just like we saw earlier when we looked at this and we tried to wrap them onto the next line now from the flex shrink perspective these are breaking out of the container because while they have you know calculated the total overflow space and then allocated that out to each of the flex items the total amount of overflow is going to be greater than the um basically it's going to make them shrink smaller than their minimum values which is not possible let's see if we can calculate this just for funds we have 1 through 9 10 11 12 so we have 13 total flex items which are all supposedly 50 pixels wide so if you do 13 times 50 let's see what that is so that's 650 pixels and if you subtract the 400 pixels that the container width is you get 250 pixels so our overflow that we're dealing with now is going to be 250 pixels and we have 13 units to allocate it over so we need to divide that by 13 and what that's going to give us is let's see 19.23 pixels per unit so we're going to have to subtract we're going to have to basically say let me break down onto the next line just so we're clear we're going to take the original width minus 19.23 pixels and what that's going to equal is let me see 30. pixels so theoretically we would just take all that overflow and subtract it from the original sizes to get us to a total width of 30.77 pixels but if we inspect element and we click on any of these here you're going to see that this little min property popped up so it says the minimum size of this element is going to be 12 pixels so that the minimum inner content is 12 pixels and when you add on the padding and the border of each of these elements that's going to break you over that value that we had calculated here so it's basically going to be uh the the content itself is going to be too big to shrink that much hopefully i haven't lost you here um try to lay out this in a logical manner um and i know there's a lot of detail it's definitely not totally necessary but i really think knowing this kind of stuff and how this is calculated is is that extra step that you have to take as a developer to really um understand what you're doing and be able to debug things and understand okay why is this breaking out of the flex container why won't this shrink enough you know those kind of questions let's do one more um practice problem here i guess you could call it so let's go back to nine elements which are gonna naturally fit within this but at the moment they're each being allocated what was it 5.55 pixels so 50 minus 5.55 to get it shrunk down to fit now if we wanted to allocate it only to certain items we can change the defaults so let's go ahead and say that we want the first three items of this flex container to keep their 50 pixel width now the way that we would do that is we'd go to item 1 and we'd set a flex shrink value of 0 which is not the default and we'll copy that down to two and three and now you'll see that each of these get wider and if we inspect them they're each going to be 50 pixels wide while the remainder are going to absorb that overflow space by shrinking so just a quick review if the total width of the items the flex items in the flex container do not equal the total flex container so if there's not enough items flex grow is going to be what applies and that empty space is going to be allocated based on the flex grow values if the overflow happens so if there's more items that can fit in the flex container then the flex shrink will apply and based on the flex shrink values of each item determines how that overflow space is going to be allocated out the last property with all this sizing that we have to talk about is flex basis let me get our container back to a spot where we only have four so let me i'll just comment out those just in case we need them later so now we have four items which are explicitly set to 50 pixels wide each so we set each of them to 50 pixels wide 50 pixels tall now if we wanted to change those dimensions we can actually do that using a flex item property let's go to the first flex item and set the flex basis to equal 40 what you'll see is that this first item gets a lot wider and it's not because it's trying to fill the empty space it's because it's respecting this flex basis property over the width property that we set earlier so by default this flex basis is actually set to auto and when it's set to auto what that is basically saying is set to flexbox is go ahead and look for the width and height properties and if you find them respect those properties as the width and height of the flex items if you don't find them go ahead and size the flex items based on the inner content and that's the default for flex basis but you can also assign pixel units and percentage units to the flex bases to override the width and height of your flex items this unlocks a lot of possibilities and depending on how you use them with flex grow and flex shrink you can get a bunch of different combinations so if i was looking at this flex box right here i have the four items each 50 pixels wide and let's say that i wanted to make them even widths and span them across the whole container one way that i could do that is by coming to each of the flex items and setting the flex grow to be equal to one and what that's going to do is equally allocate the empty space as we just talked about so that's one way to do it now we could also backtrack a little bit and instead of doing that we can set the flex basis to 25 on each of the items and that's going to equally give width to each of the items and give you the same exact result now if we come in and try to set the you know flex grow property on one of the items it's not going to change because the flex basis is going to take precedence over that but in some cases maybe you don't set the flex basis property on all of the items to equal 100 percent so in this case we've given each of them 25 which adds up to 100 so it's going to fill the whole container but let's say that we for the first item we give it 10 percent so we're going to be missing 15 percent of the container and let me let me delete that for a second so now we just have flex basis properties and they sum up to what 85 if i'm calculating that correctly so there's some empty space here on the right it doesn't fully fill the container but now if i put on the flex grow property and set it to 1 that first element is going to take the remaining space and we're back where we started so essentially setting uh flex bases of 25 on the last three and 10 plus a flex grow of one is the same as just doing flex basis of 25 on all of them so many ways to get to the same answer the last thing we'll talk about is the order of the items along the main axis in our flex container so if you wanted to change uh swap these out so maybe you wanted to put number two in number one's place you could do that all you have to do is come up to the html and put number two before number one and that should just flip them but if in flexbox you don't even have to do that you can actually flip those with css so if i came down to the css and i looked at flex item number one and i gave it an order of two and then i gave flex item two in order of one now you're going to see that these are flipped and hold on i think the reason that this is not working is because you have to set them on all so we'll set the order on three as three in the order on four as four so the three and four have their natural order but then two and one are going to be flipped so if you set order on all of the flex items you can determine you know the order that they come in now you might ask why is this useful well in many cases it's really not useful you might as well just change the order of your html and be done with it but if you are designing responsively which we talked about in a prior video i think was just the previous video as you're resizing your browser you may want different elements on the page to go in different orders so say you have a nav bar at the top you've got main content a sidebar and a footer maybe you want to order those differently whether you're on a desktop or a mobile device and this would be how you would do that is you'd set a media query so you might come down and say at media and we'd say like min width of 600 px or something like that and then in here you could probably set the flex item number four to have an order of one and then we'll say a flex item 1 will be in order of 4 something like that so as we are getting bigger than 600 px that's how that's going to show but as we shrink it you'll see that those flip so we can resize and order them differently based on different screens at this point you've learned pretty much all of the main properties of flexbox now of course there's some shorthand properties that can make your life easier so instead of you know setting the flex basis and the grow and the shrink values what you can do is come in and just say flex and then the first value is going to be the flex grow so maybe you want to set that to one and then the second one's flex shrink you might set that to one and then the third one is going to be flex basis which is set to auto so this is a shorthand way of writing flex grow flex shrink and flex basis all in one line so that's just a shorthand property but not necessary i wouldn't even recommend using those until you're very comfortable with flexbox well this is actually a brand new day same hat same hoodie whatever but i forgot to add the last part to this video which is actually taking this previous challenge that we had solved so as you're following along with the full stack roadmap we solved this pricing card challenge as a part of the original html and css tutorials and then also made it responsive in the prior video on responsive web design what i want to do to wrap up this video is take this design and make it with flexbox instead of how we did it before so if i'll remind you just for a moment what we had to do to get these bottom left and bottom right sections sitting next to each other with a 50 width with something a little bit hacky um if we come up to the html um you'll see right here so this line doesn't look you know all that spectacular but what you'll see is we've got a closing div right here that corresponds to the bottom left section div and then the starting div for the bottom right section is sitting right next to it which when you're looking at html that's generally not very good syntax and that was kind of a hack that we used to remove the white space and allow for the fact that we had 50 widths on both of them so if i were to enter this down one line just for formatting sake you'll see that the bottom right uh piece of the card actually disappears and that's because it's going underneath that bottom left card and then we're hiding the overflow as you'll see in the css for the main card so we've got overflow hidden so if we take that off you'll see it pop up below here so that's what's going on and we can solve this very easily using flexbox and get rid of all of our hacky solutions that we had before now to make this a little bit simpler for us to see exactly how this is working let's go back to our layout example this is what we had used to originally build the card so that we could see what we were dealing with and we didn't have to confuse ourselves with all the different styling while we're just trying to focus on layout it isn't pretty but it will work for our layout purposes at least and what you'll see is i've come down and added a media query this is from the prior video if you haven't watched that we talk about responsive design and media queries so basically what we're saying is in this block we want to apply certain styles to any uh device that is greater than 500 or greater than or equal to 576 pixels as you'll see when we shrink this it changes the layout and then when we go above 576 then these two style selectors will apply and you'll see that the width and the height of the card gets applied and then the bottom left and bottom right section are going to get a 50 width each now with flexbox this is relatively simple all we have to do is if we're looking at this entire card here it's composed of a top section and a bottom section with two left and right sub bottom sections and what we want to do is on the desktop device so this what we're looking at right here we want to have those bottom sections in one row and then when we shrink it to mobile we want it to come into a column so if you remember from just a few minutes ago we talked about the flex direction where we can take the main axis and change it based on that flex direction so what i'm thinking is that for a desktop device we want flex direction equal to row so that these two go right next to each other and then on a mobile device we want it to be in a column so the way that we do that since we're designing mobile first here is we come up to all the main styles here and we go to the bottom section because that's what we want to turn into a flex container we don't necessarily want to turn the whole card into a flex container because the top the top section is actually fine and it would be overkill to put a flexbox container on it but the bottom section is not so let's go into the bottom section and type display flex and what you'll see is i believe nothing will happen at the moment because we're in the desktop view but let's go ahead and go to the mobile view and now we've got some things messed up because we're designing mobile first so right now we're working on the mobile device so these are still sitting next to each other and all we have to do to change that is set the flex direction to column and you'll see that they jump right above each other that's pretty much it when it comes to the mobile design of this let's see if we can find anything that we can remove now um i believe we can remove this vertical line okay we're going to get to this we'll actually get to this let's get the design right first and then we will worry about that so at the moment we set the flex direction to column and because of that because we're designing mobile first that is also going to apply to the desktop design unless we change it in our media query so let's do that we'll say bottom section and we'll give it a flex direction of row again so the default so now as we resize we get a very similar result and we're actually pretty much done but we have some redundant styles that we can actually remove now that we're using flexbox and this is where the nice parts of flexbox come in you don't have to write as much css so we can remove the bottom left and bottom right section widths and i believe that may screw us up a bit after we change a few things hold on i think we need to change uh let's remove the display inline block on the bottom left section in the bottom right uh we will remove the 100 width and we'll remove the vertical align top so we don't need those anymore but what you'll notice is now we've got this screwed up and that is by design that's not surprising let's make sure our mobile is still working so we're good here mobile's still working but desktop has a little bit of work and if you remember the bottom section is our flex container which means based on the html here if we're looking at it the bottom section has two direct children it has the bottom left section and the bottom right section and we can actually enter this down on to its new line now that we're using flexbox we don't have to have that hack but anyways the bottom left and right section are the children so therefore these two style selectors are going to be the flex items in this desktop view we have the flex direction set to row which means that those that bottom section has a main axis that is going horizontal and if you remember from a few minutes ago the way that we resize the flex items along the main axis is going to be one of two ways we can use the flex grow property or we can use the flex basis so let's come in here and just try both of them just so i can show you we'll start with the flex basis it may be a little more straightforward so if you put in 50 on the bottom left section and then you say flex basis 50 on the bottom right section we have completed the card it looks great and as we resize it it's still going to work so that's one solution another solution is we could come in and we could say flex grow set that equal to one and flex grow equal to one on the bottom right section and this should work just fine too now going directly from flex basis to flex grow will not really illustrate that point let's get rid of the flex grows for just a second so that we can conceptualize by default these two sub containers down here have a natural width because we haven't given them we haven't given them an explicit width so they're going to take the content with the content sitting within them as you can see is filling the entire width of their container now these do not fill up the entire section we've got this empty space here on the right and the flex grow property is what applies when we have that empty space and so basically what we're going to do when we enable the flex grow one properties like we talked about is we're going to split up that space just give it one unit to the bottom left section in one unit to the bottom right and therefore it's going to be i believe perfectly equal or it actually may not be perfectly equal it may just be a coincidence that the content is a similar width let me make sure that that's correct so let's just enable these and we can check in the dev tools so let's inspect and let's see what we're dealing with so this one is 263 pixels wide this is 276 pixels wide so glad we looked at this i actually was not thinking about this correctly the correct solution here is probably going to be the flex basis of 50 it just looks like they're equal when we use the flex grow because they're similar widths by default like naturally the content has a similar width in both sections but if i close out the dev tools and let's say i came up here and just added a bunch of content to one of the list items in the bottom right you'll see that now that flex grow is not working quite as well because we're not giving them equal widths we're growing them based on their natural size so i stand corrected that is not the correct way to do it flex grow at least i think flex basis is by far the most reliable way to split these into equal containers so we'll go with that and call it a day i think we have solved this to my liking so this is the basic layout and then as we shrink it goes down to mobile so as we come over to here back to our card and let me let me bring this guy back up so that we can see everything working okay so there's our card now we got to turn this into a flex box and we'll do exactly what we just did in the layout example so if you remember we came down we didn't need any of that we did need a bottom section we wanted the flex direction to be row on a desktop device and then in the bottom section we need to display flex set the flex direction to column and let's see what we got here looks good now we just wanted to remove on the bottom left and bottom right sections i know there's a lot more styles here we don't need this guy we don't need the vertical lines we don't need the width all we have to do is add a flex basis of 50 percent on each of these so we're looking good there and i believe we have solved it i don't think there's anything that we did otherwise now there's just one more thing that i want to show you with this that we can use flexbox for as you see this 29 per month these are sitting next to each other and whenever you have horizontal content it's usually a decent use case for flexbox let's look at the html that we set this up with so you'll see that we have a price container that would be the overall parent and then that parent has two children items so if we turned price container into a flex container then the price and the price frequency would therefore become the flex items let's find that in our css so here's the price container we've just set a margin on it so that's probably going to stay the same even with the flex container but then we also have price and price frequency which looks like we have styled right above it so let me let me just change the order of this because it makes a little more sense to have the container on top and what we're going to do is set display flex by default it's going to be set to row so it will put them next to each other let's get rid of all the styles that we have within these i don't think that we need any of them except for maybe the font size and the opacity because those are not layout related properties so here's our starting point and you can see that by default even after we removed all those styles that display flex making that whole container a flex container is going to still put them next to each other now let's just back up and remember exactly what's going on here we have some html where we have a price container and then we have two items within it so we have a flex container and two flex items by default we've set this to display flex which makes the main axis on the horizontal so in a row and therefore if we want to align things along the main axis we use justify content if we want to align them on the cross axis we use align items as you can see the per month is a little bit misaligned with that 29 dollars an easy way to fix that is to come on to the price container set the align items and set those to center you'll see that that perfectly centers those two right next to each other and that's a much better solution than what we did before where we set the position to relative and then we set it negative three pixels to just pull it up a little bit the last thing we have to do as you'll notice there's no spacing between these and there's no real solution here with the flexbox i mean we could come in here and justify content and say something like space between but you'll see that that's going to space it out way too much so that's not really going to help us so really the best way to do this is just come on to the [Music] either the price frequency or the price item and set a margin so we don't even need this style anymore since this is already written the price frequency we can just use this and all we have to say because this is the um per month piece all we have to say is margin left and give it something like five pixels and that spaces it out nicely very easy and straightforward so there you have it that is how we solve this with a mobile first fully responsive solution with flexbox and it shrinks and gets bigger and it looks great on all devices in this video we're going to be building a testimonials grid and we're going to be doing it with a mobile first responsive design solution and we're also going to be using flexbox my goal in this video is to not only solve this challenge on front-end mentor but also to apply those skills that we've learned in previous lessons so that you can really start to use them in the real world lastly i'm going to be adding in a few extra pieces to the puzzle so throughout this series we have been using codepen which is just a nice environment where we have html css and javascript all in one place all hooked up and ready to go since this challenge is relatively straightforward and you'll see as we walk through it i want to also add in the concept of a code editor so we're going to be downloading and using visual studio code and then we're also going to be doing a little bit of version control and i'm not going to get super detailed into it uh quite yet but i'm gonna show you the commands that you have to run to get your design pushed up to github and deployed to the github pages so that you can you know share a url with others of what you built but don't worry the only prerequisites that you really need are going to be html css and make sure you've watched my flexbox in responsive design videos let's get started if we come up to front end mentor this is the challenge that we are going to be creating and i've already started it so i don't need to click here so let me go to my challenges or i guess profile my challenges and then testimonials grid section so from here you can download the starter files so we'll do that it's going to pop up on another screen for me so once you've downloaded your files make sure that you have visual studio code downloaded it's just a code editor and i actually made a whole video on code editors you can check it out up here of which ones i recommend and why so obviously visual studio code would be one of those so go ahead and download that and once you've downloaded it open up a brand new window from here you're going to want to click file um open a folder and then go find the testimonial challenge starter pack that you just downloaded so i've got it selected and this is going to open me up into the workspace as we start looking at the starter files if you open up index html which is what we're going to actually be working on you can see that there's all sorts of text already in here and luckily we have a lot of the testimonials that we will need they're not really implemented in html elements but it's good to just have this text ready to go now just to relate this back to what we've been using with codepen this index.html is basically the same as this html box here in codepen it's just um on our local computer it's in our local file system rather than up in the cloud now your first question is probably well how do we actually view this html document in the browser now remember html and css they are only displayed in the browser therefore we need a web browser to look at this the way that we do that is you actually have a couple options i'll show you the most bare bones option first and then we'll go into what we're actually going to be using to solve this challenge to view an html document in the browser all you have to do is go to your file explorer i'm on windows but this works the same on mac or linux and just go to the folder that you have it saved in and then find the index.html file you'll notice that it does not say index dot html as you see over here and that is because the file type over here is chrome html document so it doesn't display it here in the name if you right click and go down to open with you can choose a browser to open it in so i might just say google chrome and when we click that it should have opened it up if i go back to my browser it should open it up in google chrome and there's a lot of text here and that's because we have to actually implement this design but this is the html document that we're looking at and you can see up in the um url bar we're actually looking at our local c drive um now on mac and linux this is going to look a little bit different but it's the same concept it's just the local path to that file displayed in the browser now i'm going to point out a few things here that might be interesting to you the first thing you see this little icon up here in the top left portion of the tab this is a little front-end minor logo or favicon you might be wondering well how did that get there well the way that we put that there is if we come back to our files and we look at this link tag right here this is actually putting in that favicon up into the title here's some metadata about it some sizing but here's the really important attribute which is the href and what we've passed in is a relative path we'll learn a lot more about absolute and relative paths coming up when we start working in the terminal a little bit more but all this is saying is i want to look relative to the current directory into the images folder over here and then drop that down and grab me the favicon 32x32 png and we can open that up and see the little icon that we have and that matches the icon that we see up in the tab furthermore in the index.html we've got the doctype that's important for just html boilerplate and then i'm actually going to indent this for us so we can see it a little bit better we have the head tag which is pretty standard for an html document here are some meta tags that help with displaying the site in the browser properly these are just pretty standard defaults that you might put in and then of course the link we just talked about now the title tag this is where we actually name the tab that it's going to open in so we can change this and just say instead of front-end mentor we can just say testimonial challenge and when we click save open the browser up and click refresh you'll see that it changed to testimonial challenge right here we have some styles we haven't really been looking at this but if you come back to our browser and go to codepen the css right here there's a couple ways that you can link that up you can actually use a link tag to link up styles or you can just put them all in a style html tag right here now we're not going to do that because that's not very good practice for a good amount of css you want to break it out into a separate file which is what we're going to do so we'll delete all of that we don't need it and then finally we have all of this text which is basically if we look at the challenge itself i wish there's a better way to see this oh yeah we actually have the design here so if we go to the desktop design you'll see that we have all of the text and all this text is right here i'm going to cut this out and then i just want to make a new file so i just right clicked over here in the file pane click new file and we'll just say placeholder text dot txt so we give it the dot txt file extension it's just a plain text document we can paste that in and save it just that we have it but it doesn't clutter up our html this last part the attribution this is just as we look at it in the browser it's just this little piece down here so you can if you want to you can go ahead and fill this out and link to whatever you want to your you know twitter account your linkedin account your github account whatever you want to do i'm going to delete it here i don't really want to clutter up anything i want to just focus on the challenge itself so at this point we have a basic html boiler plate and i'll actually show you a shortcut to get to pretty much exactly this with just a few keystrokes let's save this i'll right click create a new file and we're going to call this test.html because we're just going to be testing a few things within this document i'm going to show you how to use emmett and what emmett is if we open it up this is built into visual studio code and what you basically can do with it is type shorthand you know characters in click tab and it will extend it out into a code snippet of what you want already so let me just give you a brief example if we wanted in this html document you can see visual studio code has identified in the bottom right that this is an html document so if i type h2 it's going to pull up this box that says emit abbreviation and you can click it to see what it's going to abbreviate to but really all you have to do is click tab and it's going to extend it out into the element that you want and you can just start typing similarly we can actually get this entire html boiler plate in just a few keystrokes so if you type html and we have a couple options here and you can go through and see exactly what it's going to create so basic html will just give you the the main tag we we want something a little better than that oops i already did it on accident the html5 let's let's go ahead and see what html5 gives us it's going to give us pretty much everything we want here just like we saw earlier and then finally html xml this is not something that you're probably going to use very often so if we just click html5 or scroll to it with our arrow keys and press tab it's going to break us out into this boilerplate which is very similar to what we were set up with when we downloaded the starter files for our front end mentor challenge so that's just a handy way to create this and then of course you can change the title i don't think there's a link tag for the favicon setup by default but you've got all the meta tags that are necessary and you're all set as we go through this challenge i'm going to be using those emmett snippets and i'll try to mention it as we go along but just know that that's going on for now i'll delete the test file we don't need that any longer and now let me show you our workflow so what we could do is just write some html so h2 tab and then sum title that's just basic html and click save and then we can alt tab or switch back to the browser open up the tab that we have it on and click refresh and you'll see the new html document this would be very cumbersome and it's not a great way to make everything fast so what we're going to do is we're going to split the screens here and i'm going to put the visual studio code on the left and the testimonial challenge on the right now obviously we don't have a lot of room here so you can always click this right here to collapse that might make this a little bit smaller for us and then when we're working with our dev tools we'll click inspect we're in chrome right now you can use firefox as well if you want since we've got these side by side i'm going to actually re-dock all of this stuff to be at the bottom so i can do that let me remember where i can do that let's click there we go you click these three little dots and click this dock to bottom so now we have a much wider screen to work with and as you resize this in the top right corner you can see what screen size you have or you can click the toggle device toolbar and now we can see it more explicitly where we can resize the screen up here so this is probably going to be the better option for us and we can change the zoom to let's say be 75 percent so that we can see it a little bit better and let me zoom out just a tiny bit we don't need the javascript console because we're not working with javascript so we can close that drawer and now all we have is the elements here on the left so the html and then the styles on the right or the css and if we click this select an element button and we select that h2 we can locate it within the html document and then see the relevant styles down here so that's going to be our workflow and as we go through this we can you know just add new elements some text click save come over here and click refresh and now you see it's been added to the document now there's just one more thing that i want to do and it's just going to make this whole workflow just a little bit nicer we're going to come into the extensions piece let me just expand this out for us we'll go to the extensions tab within visual studio code and let's type in live server now live server is a way that we can get automatic updates every time we click save on our html and css documents so let's install this it's very easy to install extensions in visual studio code so you can see it's installed i think that's all we need to do so this is just another tab that's been opened up let's close it we don't need it we'll close this out as well and then all we have to do to get this live server working is come down to the bottom right of our screen and click the go live and it's going to pop up a security box you need to allow access and now what you'll see is that we've got a different address this is actually we'll get into this a lot more later in the series but 127.0.0.1 that's our localhost and then 5501 is the port that we're serving it on and we're serving the index.html document now if we put these next to each other close this out and add another you know piece of text so more text and once we click save you'll see that it immediately gets updated here in the browser that's going to be incredibly handy for us we don't have to go back and forth clicking refresh every time and it's also really nice because instead of you know going to your file folder and right clicking and opening with a browser you can just do it directly from visual studio code i know that was a very long kind of introduction to get us started working on this project but um as i've been trying to do within this full stack series is just take you through every single step so we're not leaving anything out we're going through absolutely everything and that's why i wanted to show you the extensions the emmett abbreviations the favicons the meta tags all of these little details because as i was learning i always wondered about them and oftentimes tutorials would just skip right over them but we are finally ready to start the actual challenge and start building that testimonials grid very first thing i'm going to do and this is what you should always do is link up your css document if i open the file explorer you'll see that there is no css available to us yet because we haven't created it so go ahead and right click create a new file call it styles.css or whatever you want it doesn't really matter this is just a convention once you have styles.css let's just go ahead and put in a basic style so we'll use the wildcard that selects all elements and we'll give it a color of red the reason we do this is just to you know verify that everything is working correctly when we click save you'll notice nothing happens here and the reason nothing happens is not because our live server is not working it's because we haven't told the index.html document that we need to grab that stylesheet so let's use an emmet abbreviation we'll type link and you can see all sorts of different links that you can use and if we go down to css click tab you'll see that we have a css link set up for us and since we are in the same directory so index.html and styles.css are in the same directory we can just use this exactly so it's saying style.css and just to stay with convention i i actually think it should be styled not styles let's just rename it to style.css over here and then save the html document you'll see that immediately the text color updated to be read so we know that our link to our style.css is working if you put in you know something like styles.css and click save not going to work anymore so that's that's what's going on there with that link tag and for those of you who have been following along with the series if we come to codepen what we just did there was we hooked up the css in the html by default when you're in codepen you can just type html and type css and they work together but when you're not working in codepen which honestly most of the time as a developer you won't be you have to actually come in and link them up with the link tag all right so let's get started here on the layout we'll close the file pane for just a second and if you look at the desktop design you'll see that we'll have to kind of flip back and forth or what we can do is we can split these editors so i believe we can split down and now we can close this top one and we have the index.html at the top and our desktop design at the bottom it's kind of small but it will at least give us a general feeling for what kind of layout we need if you're like me and get kind of annoyed with all this text flowing off the screen just click view toggle word wrap and then everything wraps to our view i kind of like that so we'll go with that and to start us off let's see what we're dealing with here with the grid so the first step to solving any html and css challenge or just building a web app in general is to get the layout correct and we saw that in previous lessons and challenges that we did where we just put a border on each of our div containers and then we did the layout and then we filled in all the content and styles so we're going to do the exact same thing here and you'll see it's not going to be all that complicated now the most obvious place to start if we make this little bigger we have one two three four five containers and they all have slightly different styles it looks like the two white containers here may have the exact same style but for now let's just give them all a unique id so that we can select them with css a little bit easier so to do that with emit abbreviations you can actually just type the hashtag and then whatever id you want to give it i'm just going to number these so we'll say testimonial dash 1 and then click tab and you see what it does is it gives an id attribute on a div so that's what by default if you use the emit abbreviations to put a hashtag and then type something or if you put a dot and then type something and press tab it will give you a class so that's just a quick shortcut so we'll go through and we'll do the same thing here so we'll say testimonial dash two and then another useful thing in visual studio code if you go to the end of a line click control c to copy and then control v it will just paste them down so there's our five and then we can come in here and give this a three a four and a five so we now have all of the containers that we need when we click save everything's going to disappear up here because we just have empty div containers that's okay for now the next thing that i'm looking at here normally i just put a border on everything and make a layout but this is already kind of a grid and we might as well start filling in the colors as backgrounds so that we can see what we're dealing with and then additionally we probably need to start looking at the mobile design because we're doing this mobile first so i know it's going to clutter the screen up a little but let's go ahead and open up the mobile design in the bottom part here close this back out and here you'll just see that every card is going to be stacked directly on top of each other but they're going to look relatively similar so here's the desktop here's the mobile it looks like on the mobile design the cards themselves for the the grid items or testimonials are going to be the height of the content so the items with more content are going to be taller than the ones with less so probably the best way to lay this out at the beginning is to just grab some of this content and put a background on there and just start getting the structure of everything so let me pull this down i know that's really small but we'll work with it and here in testimonial number one i'm just going to put a small amount of content because i don't want our html document getting super busy with a bunch of text yet because that'll kind of that'll take away from the concepts we're trying to learn here so let's go to the placeholder text and maybe we'll just grab this really short line of text and we'll put that in a paragraph tag so we'll just copy it there and click save we still have red text i don't really want that so let's replace this all selector instead of color red we'll do box sizing border box because that's our basic css reset that we want to do we want to set all box sizing to border box so now we got the right text and all of our box sizing is good let's resize the screen so that we actually have the browser on the right mimicking a mobile device so this isn't perfect i don't know you know this is probably not exactly a mobile device let's make it even a little bit smaller it gives us more room to type anyways but this is close enough and this is what we're going to build our mobile design off of as we talked about in the responsive design video 576 pixels is kind of our standard um mobile device so anything 576 or below is going to be considered mobile so that's roughly what we're looking at here and if you right click and inspect and then you click this little icon right here to get the full screen you should be able to see exactly how wide it is so if we just typed window dot inner with we're at 584 so if we wanted to hit that 576 number or a little bit lower it'd be right about here so we'll keep the screen size right here for now and then as we uh go up to the desktop design we'll make it a little bit wider i'm going to copy this same text just so that we have something into each of these additional testimonial divs and now we're going to have some text to work with and then let's look at these colors here so how do we know what colors to use well we have some starter files and if we go to the style guide dot md it's going to give you layouts and then colors right here so this is where we're going to find the colors that we need so if we come down to the colors we see all of them listed in hsl format and you can use that in css but i just it's not my preferred way of doing it and honestly i haven't seen a whole lot of developers that use hsl so what i'm going to do is i'm going to convert these to hex codes and that way we can work with what i'm comfortable with and probably what you'll see more often just as a reminder this is how i'm doing it i just typed in rgb to hex and then you get this tool that google has and you can just basically come in here grab an hsl value paste it into this box here and then copy the hex code and replace it here so that's all i'm doing alright i've got these all replaced here so we've got hex values a little bit more comfortable for most people to be using and we've got a couple notes on the text colors but i think these are mostly related to the text itself so we'll get there a little bit later i'm going to drag the style guide down to this bottom portion of our visual studio code screen just so we can have it as a reference now it looks like the background of the entire screen is going to be a light gray so we'll start with that if we go to our style.css in addition to the box sizing we also want to set margin 0 and padding 0 on everything just so we don't have any unintended side effects so when we click save you'll see that everything gets smooshed together because we just excluded the margin in padding that's by default set on a paragraph element in the browser and now we'll come down here and we'll say on the body we want the background color to be i believe the light gray or the light grayish blue we'll see which one looks more similar so there's the light gray that is definitely too dark and there is the light grayish blue which i think that does match if my eyes are not playing tricks on me i think the background here uh matches pretty well so i think we got that right so we got the background covered and now we can start filling out our containers themselves so each of them again has an individual id let's template those out so testimonial one and then we can copy this 5 times we may not need all of these but we'll just have them out here for the start or actually of course we'll need all of these they're all different colors so the first one is this purple the purple card here so we need to grab that color from the style guide i believe the moderate violet is what we're looking for so we'll come up to testimonial one and give it a background color of that and now you'll when we click save you should see that background color now the next one that we're looking for is it looks like kind of a darkish gray so very dark grayish blue i think that's the one that we want so we'll come to testimonial 2 and do the same thing and then for the rest of these i don't want to waste your time i'm just going to go and kind of guess and check until i get the background colors correct on each one of these pieces to this grid but the one thing i will say before i speed through this is the numbering of these so we've got them numbered one through five but you may be wondering which is one and which is five um four reasons that you will find out later as we design this with flexbox um this top one will be one then two three four and then the one on the right is going to be the fifth one so just know that as i'm going through and styling the backgrounds on each of them alright i've got all the background colors filled in here and as you'll see in this mobile design that we're trying to make there's some room between the edge of the screen and these items within the grid and what we're going to do to create that is we're going to actually wrap all of these items in one big container and then we're going to set some padding on that container to bring all of them inwards a little bit so we'll go to the index.html to do that and right now all of these divs are just sitting within the body but we're going to say we want to create a id of testimonial grid container we'll just be very specific with it grab the closing div down here and paste it in so now we want to select this testimonial grid container and put this in our styles so hashtag grab the id and now we want to set some padding you can either set it in percentage or pixels for this since we're going to be making it responsive i'm going to start with a percentage so usually about two percent works pretty well you can see that you get a little bit of padding there but of course when we look at the the mobile design and make this bigger there's a little more padding on the top than there is on the sides so we can actually use the shorthand padding property to say we want 2 percent and then maybe five percent and we'll save that and actually got that wrong so the top and bottom is going to be the first argument and then the left and right will be the second let's bump that up to three see what we got this looks pretty good so i'll stay with this i'm i'm fairly satisfied with the spacing that we have here something that is really bothering me right now is the text color on these darker backgrounds so before i get too mad let's just go ahead and fill that out so for the first one we'll need a white text color so we'll throw that in there and then we also will just copy this down for testimonial two we're good on three good on five but we'll also need it on four and now you'll see that the text is popping out a little bit better but it's all crammed together so we need to space things out just a little bit as you see in the mobile design there's not only a little bit of space around the inner content of each grid component but there's also some space between each grid component on a mobile device like this a simple way to add the spacing between them is to just set a margin bottom on each of these and this looks roughly around 20 pixels give or take so instead of trying to set that on each of these let's create a class selector that grabs all of them and what we could do is we could come down here and grab each of them one at a time separated by commas but that's going to get very tedious the better way to do it is to come to the html and instead of trying to grab them all by ids let's assign a class to each of the grid items so we'll just say grid item is going to be the class name and then we can copy that down into each of these divs and here's a little shortcut within visual studio code if you find the spot that you want to go in and you hold down the alt key it's different on mac but on windows would be the alt key and then you click in a different place you'll see there's two cursors so we'll click again and now there's three cursors and then finally four and what you can do is now we can type all at once and paste in those class the those classes that we just created so there we go we've got class of grid item in each of these grid items and now we can come down and just select that and set a margin bottom of 20 pixels so when we click save we've got a nice amount of spacing between the items furthermore another similar thing between all the grid items if we look at our styles here is that the padding around the content is the same on each of them and it looks like i'm not exactly sure but it might be somewhere around 20 pixels as well so we'll start with that and we can tweak it as we go so we'll set padding of 20 pixels click save and now our boxes are looking a whole lot better than they did a few minutes ago so we are far from done here with the mobile design but right around this point once i'm like happy with the general layout obviously the text isn't right in here we're missing pieces but once i'm satisfied and it doesn't look like completely terrible then that's when i go to the desktop design and start making it responsive because i want to get the layout correct before i start you know adding a bunch of other things in so what we will do here is we're going to shrink this visual studio code window a little bit close the editor so we have some more space and then make this browser window larger so that we can use the chrome dev tools and when we bring this down here i'm going to click this button right here so we can toggle the device toolbar and now we have the ability to see this at different sizes really quickly without resizing the entire screen so that'll be helpful as we you know we can actually see the the width up here updating live so that'll be really helpful as we resize on different devices and design for that so here's about our mobile design if you just click this bar you can get a small mobile device you can click here for medium and here for a large mobile device so here's here's the large mobile device and the reason it shrunk is because we're at 40 percent so let's go to maybe 75 you can see it a little bit better and then as we resize this we're getting into the larger desktop devices i know i've mentioned um about 576 pixels for the break point we're going to use and again that is because earlier in this course when we talked about responsive design i just said let's go ahead and use the bootstrap css framework breakpoints they're right here so they go from 576 768 992 in our case we've got two designs only so we're only going to need a single break point for this now we just have to decide at what point do we break so if you just play around with this design and you just look at the text right around so we're at 708 pixels wide right now and this is getting a little bit wide for my liking with this mobile design over here so i think 576 right about here is going to be just about the widest we'll go for that mobile design and that's going to be our break point then to set that break point we come over to our css we write at media for a media query and since we're doing mobile first design we'll set a min width of 576 pixels so now we open the brackets up and any styles we put here are going to apply to anything bigger than 576 so all of this right here and before we get too far let's go ahead and test it this is really important as you're coding whether it's html css javascript or anything else do it incrementally don't just write a bunch of code and then test it out all at the end so let's test it out by targeting a p tag and giving it a color of red something obnoxious and we'll click save right now we're at 444 pixels so we would not expect this style to apply but as we go above 576 we would expect all the text to turn red so let's see if it does and it does so that means that our media query is working great remember always put these media queries at the bottom of your css otherwise they may get overwritten by other styles so our goal here before we get any further is once we hit above 576 so when this is turning red and the reason it's doing that is because i haven't saved yet but once we get above here we would expect this layout to look like this desktop design over here so the question is how do we do that and the way that we're going to solve this is with flexbox i'm just going to expand this vs code so that we can really look at the design here so as i look at this just by experience alone i'm thinking to myself that using flexbox we're probably going to need to split these into sub containers now there's a lot of different ways that you can do it as always there's more than one way to solve the problem but i'm going to choose the most straightforward and beginner friendly way of doing this i'm thinking that there is probably a way that we could take all four of these items on the left side wrap those in a container and then control the overflow of the container with flex wrap and then size them accordingly but i'm thinking that might get a little bit complex to try to implement and by adding just one extra container we can avoid all of that so what i'm thinking is that we split the overall layout into two containers so the first container is going to be all four of these items right here and the second container will be this item on the right by doing this we can take container one on the left and container two on the right and we can display flex and they will go side by side next to each other but that doesn't solve how we lay out these items on the left so what i would want to do within those containers so so remember we have one container on the left one on the right and then the left container we can further divide into two other containers so we'll have a top and a bottom container and in each of those we will display flex so that they sit next to each other and then we can use flex basis to make the dimensions of each of these a little bit different i know this is a little abstract so let me just show you exactly what i mean so we'll keep that down here and this all happens within the html itself so to take these four items on the left and wrap them in a container we just need to find them so here are the first four and all we have to do is go at the top of here and set a container now we have to decide what to name it so that we can target it with css naming things is always difficult i'm not going to over complicate it here we're just going to call it main left and make that div and then within main left like i said we're going to have a top part and a bottom part there's a couple ways i could do this but once again i'll go very simple and i'll just say top and then we'll name the other one bottom so at this point i've got everything laid out correctly so we want to grab items one and two so this one and this one and put it in that uh top container right here so we'll do that we'll just cut these away here put them there and then in the bottom we will take items three and four and put them there now finally we have testimonial five and i said we're going to make a separate container for that but lucky enough we've already wrapped our entire grid within the testimonial grid container and if you think about this from a flexbox perspective we always have to think container and direct children doesn't matter the you know grandchildren or great-grandchildren those don't apply in flexbox so all we have to know is what is the parent and what direct children does that parent have this testimonial grid container has exactly two direct children it has the main left and then it has the testimonial five which is that you know this tall card here on the right so now that we've got this laid out let's save it and i will now bring this back to the left side of the screen so we can look at it again and you'll see that on the mobile device you know nothing has changed here we've put all these containers but since we haven't selected them with css there's really no change to the overall layout quite yet but now this is where the real magic is going to happen and what we could do is um since we're devi we're now designing for the desktop we did mobile first with all these styles now we're doing desktop within the media query but again remember you don't have to put everything within that media query there can be some shared css properties between mobile and desktop designs so the first style i'll add is going to be the testimonial grid container we want to do work from the outside in so we'll go to style.css and we'll put that into our media query and the only thing we need to do here for the moment is write display flex and when we click save you'll see nothing happens here because we're still looking at the mobile device but as we make this larger now you see that this the left the main left container with all four of those items go left and then the last item testimonial five will be pushed off to the right because we've displayed them on the main axis which is horizontal now if you look at this design obviously this testimonial 5 has much less width than the rest so we can set that with flex and i suppose it's probably about 25 it looks like 20 to 25 percent of the total width so let's come to our style.css and on each item we can set the flex basis so in this case we're going to want to grab testimonial 5 and target that with our id selector and then we'll say the flex basis for that we'll start with 20 and see what that looks like okay that's a little bit small i'm thinking that we'll need maybe 25 i like that a little bit better and we can always tweak it as we go the next thing we have to do is look at these four items on the left and they obviously the top two go next to each other and the bottom two go next to each other as well therefore we need to make the top a flex container in the bottom a flex container so you'll see the id is top and bottom so all we need to do is come over here and say top display flex and then bottom display flex but we have a little bit of a problem here because they're not taking up the whole height of that main container so if we go to the index.html we have the main left container and we want this right testimonial 5 to be equal in height to these other testimonials to do this we need to also turn the main left container into a flex container so we'll go right here and display flex and this is not going to look right for a second because when we do that it puts everything in a row but if we change the flex direction of the main left to be column now it's going to go back and look the same but now we have control using flexbox of how those are distributed so if we look at this main left container which its direct children are going to be the top and the bottom which are also flex containers we can distribute those using the justify content since we set the flex direction to column the main axis is going to be going vertical and therefore justify content allows us to align on the main axis we also have the ability to say how large we want these using flex bases so while top and bottom are flex containers in relation to their children html elements they are flex items in relation to the main left container so if we come down and set the flex basis to um we'll start with 50 on each of them and then i'll show you why that's not going to work perfectly so we'll save that and now you see that we have these filling up the total height of that main left container and i was actually wrong the flex basis of 50 works just fine because in the mobile version we set the padding or we set the margin bottom of 20 pixels so there's already some space between we'll obviously have to space out horizontally but we'll do that in just a bit now the next question we have to ask is how do we get these proportions correct with these four containers to do that we can work directly with the top and bottom and we can use the flex basis of their children items to do that so all we have to do is grab let's see exactly how we need to do this it looks like these are a complete mirror of each other so this first one and the fourth one are about equal proportion and the second and the third one are equal proportion so we know that our percentages are gonna work pretty much the same so to do this we're gonna need to grab testimonial one through four and put them down in our media query which is our desktop design so if we put these at the bottom remember in our desktop design these are considered flex items we can go ahead and get rid of these two properties because they're already defined in our mobile version which also applies to the desktop as long as we don't override them so we can get rid of all those but now we can set a flex basis so the first one this purple one looks like it's got about 75 percent of the width so we'll go with 75 percent and then that means testimonial 2 is going to have a flex basis of 25 because we want to fill up the whole width of that container and testimonial 3 is going to have a flex basis of also 25 and then a flex basis of 75 percent for that last one so when we click save you should see the proportions update uh pretty nicely here as we compare this design right here to our desktop design picture you'll probably notice that it looks a little bit wider overall and you'll see that there's more room around the entire container so let's just fix that so that we are not thrown off by these proportions in the way that we'll do that i believe we can just add some we can add a max width to the entire container so let's look at our index.html and our testimonial grid container is probably what we're going to want to select and we've already got that selected in the media query so on a larger device maybe we set the max width of let's say [Music] looks like this might be like 600 pixels that's just a rough guess so that's a little bit narrow so maybe we'll set the max width to say 700 maybe that looks a little bit better let's go up to 750. i think that's kind of in the ballpark and then to center this we could use like flexbox but honestly the easier way is this little trick where you set a margin of zero pixels top and bottom and then left and right you give it auto so it's gonna automatically give the margin to center it so when we click save you'll see it all goes to the middle and now we're starting to see a little bit more proportioned grid here now at this point i think this is a little bit small the proportion we've given to that right one so let's change the flex basis of testimonial five to be maybe something like 35 percent and then of course we need to set the left one to be the same so we need to set the flex basis on the main left which is all four of these items together we'll set this to about 70 percent i think and then update this testimonial 5 to be 30 and i think that looks just a little bit better for us once we add some spacing here it should look a lot a lot better so to finish this off here the layout um it looks like there's some spacing between each of these items you know horizontally and there's a few ways that we could do this actually i'm now thinking we might even go like 35 65 here and now we have these ones which are squished a little bit so maybe we need to update this container give it a bigger max width then we'll change the 75 25 at this point i'm just like adjusting things until we get generally the right fit so let's try maybe like a 70 30. that looks slightly better but i still think we can go a little bit more so 65 35 there we go i think this looks relatively good and then now that we've adjusted this this looks a little bit too big so we'll claw this one back to 70 and 30. and i think we're looking good here i think the proportions between this and our design is relatively good and then of course this bottom one we need to match the top so we'll just come down and do that real quick so 65 35 so this one needs to be updated to 35 and 65 and now our grid is looking really good now to get the spacing horizontally you'll see that there's these little gutters here um so right now we don't have any any spacing horizontally we could do this with margin um as we kind of did vertically with that margin but i'm going to try to apply our flexbox skills to make this happen which is a totally valid way to do it as well so the first one we have to take care of is the main left container which surrounds all four of these and then testimonial five here on the right right now we have a flex basis of thirty percent on this far right and seventy percent on the main left container so it's split in two but if we reduced those by a couple percentages and then we aligned them with space between them we could add that little vertical gutter in there so what i'll do is i'll take the main left container and just bring it down to say 68 so now we have about two percent empty space within the container so let's save that and you'll see there's no there's no space because it's kind of added at the end it's kind of invisible here on the right but if we come up to the testimonial grid container and what we do is we justify content because remember the main left with all four of those items and the testimonial five on the right are aligned on a main axis which goes horizontal because the flex direction is set to row by default so justify content allows us to align those within there and all we have to say is space between and when we do that you'll see that there's a little space added there because we added some extra empty space and then we spaced them out between we can do the same exact thing with these flex items here too so the top flex container the flex basis is 50 or actually i'm not doing this right so the top and bottom are going to be the flex containers so let's just set the justify content on here to be [Music] space between to start off all right and we'll save that and we won't see any changes because currently the flex basis equals a hundred percent so there's no space to add between but if we reduce we'll just reduce testimonial one to sixty three percent so we have two percent left over once again and we'll save it and now we have that little space of two percent added in there as well now of course two percent on a large container versus two percent on a much smaller container is going to equal a different pixel width so as you see in the design the little gutter here between this testimonial five on the right and all four of these on the left that vertical gutter is a little bit wider than the vertical gutters that we just added to these containers right here so you see how to do it with flexbox if you're okay with that fact then you can just leave it as we have it but what i'm going to do just for demonstration purposes is i'm going to undo everything that we just did so we'll set everything back to their original percentages we'll remove the justify contents and save it and now we're back to this place where we have no space between and a really simple way that we could do this is we could just look at all of the flex items or all of the grid items here on the left we can just add a margin to the right of say 20 pixels and it will be equal across the board so if we look at our index.html we have all of these individual items with a class of grid item but we don't want to add the margin right to this last testimonial so one way that we could do that is we could look at the main left container which has these four items and then we can select all descendants with a class of grid item so it only selects those four so i'll show you what i mean here um i guess we'll we'll put this up here right under main left and we'll say main left is the parent that we want to look at and then we want to grab all grid item descendants of that and now what we can do let me just show you what we're selecting first so we'll set a color of red so that the text turns red and we'll click save and now you can see all of these left 4 grid items have red text but we've excluded the right one with the css selector that's exactly what we want so we'll add a margin to the right of 20 pixels and you're going to see that this just gets spaced out really nicely automatically and that is it's a little little much maybe let's go with 15 pixels and that is exactly how we would add that spacing on the right naturally and now it's equal between all of them at this point the overall structure of our grid looks very good and now that i'm looking at it one more time it seems that this right one is still a little bit too wide and i apologize for going back and forth here trying to teach and uh and see how to do this at the same time so it's a little bit tough so let's let's bring this back to 25 on testimonial 5 and 75 on the main left container i think this looks good and i really hope i don't change my mind on that again i might but as of right now this looks very similar and the cool thing is if we resize this down to mobile so once we hit 576 pixels it should break and boom we have our mobile design so we don't have to update anything on the mobile side of things we just made this desktop design and it's pretty much responsive now the one thing you'll notice um is as we shrink it these get shrunk a little bit so it may not look perfect say on like a tablet but it should work okay you could always add an additional breakpoint if you wanted to to adjust those flex basis numbers that it looks better on tablet but i'm not going to worry about that here because i think it would just be distracting from the main lessons all right so we have got our layout done it is responsive and now we should be able to just go in and look at this entire design here and start adding in all these components and styling them out before we do that though i'd like to clean up our css a little bit because at the moment we are duplicating a lot of our css rules so this is what we would call kind of refactoring so you write the code out you don't really worry about doing it perfectly the first time but then you kind of edit and you you know iterate on it and make it a little bit cleaner so what you'll see is we've written this rule of display flex several different times so what we can do is we can take all of these ids where we're writing that display flex and put it into one single css rule some might say this is not worth doing but i think it is it's nice to nice to break out our code a little bit so we want to select our testimonial grid container and display flex let me just write the rule first and then of course we want to select more than just the testimonial grid so what else uses flex we have the main left so we'll copy that in there so main left top and bottom so those are all going to display flex and now we can get rid of all of those individual rules on each of them click save it should look the same down here we have these four flex items all set with flex basis percentages that are very similar so let's group this together let's take testimonial three combine it with that rule and delete all of this and we'll take testimonial four combine it with that rule and delete this last one save it should get the same thing top and bottom now both have a flex basis of fifty percent and that's the only rule so we don't need them broken out separately we can just bring those together and delete that rule click save should be the same and then i'm going to take all of our flex rules and just kind of group them together here so let me let me just put those here okay so now we have all of our css grouped up a little bit better and we can start working on adding the content i'm going to shrink this screen and we'll go ahead and close the dev tools and we'll just work on this first on the mobile design so we have some more screen real estate and then we'll check it on desktop to make sure everything is working so we'll open up our file explorer and index.html along with our placeholder text because this is what we're going to actually be putting in to all of these containers and i'm just going to look at the desktop design i know we're doing mobile over here but it's just bigger so we can see it better i'll go through the first one with you and then the rest of them are going to be pretty much exactly the same so i'll let you do those on your own and i'll just speed through it so with this first card right here now we have this big quotation we're going to worry about that totally last we're not going to worry about that yet the content itself it looks like we have a picture we have a name we have this says verified graduate so they're the status of that person and then we have the main testimonial and the secondary testimonial so just looking at this immediately we need an image tag we need a paragraph tag another paragraph tag looks like maybe an h4 tag and then another paragraph tag so let's get the picture first this first one is daniel clifford so we're going to need that image and that's been provided to us if we go to images folder you should see imagedaniel.jpg so we can finally start to to replace this so we're on testimonial one right here and we're first going to put in an image so we'll use the emit abbreviation click tab and the source that we want is going to be the relative path and then we'll say images and then we want image dash daniel and then the alt attribute this is for accessibility you should always put this in here so daniel daniel profile picture just a description of what that is click save you'll see this image come in to our first grid item we now need to write in his name and verified graduate so let's do that we'll just use a p tag so daniel clifford verified graduate there we go we've got those added in there now we have to go find the text so it says i received a job offer mid-course okay so there's our daniel so here's our first piece of text we'll put that in the p tag right here so there's that text and then finally we'll just want one more p tag and actually no this will not be p tag let's just set that as an h4 i think that's roughly what we would want here that looks okay we can always switch it later then finally we have this last piece of text so this is the longer testimonial that we'll put in the p tag right here all sorts of formatting issues so i'm just gonna get this all on one line here it doesn't really matter but i'm gonna just do this anyway okay so we've got our first card done and like i said you can go through and figure out how to do the rest of them it's the same exact pattern for each of the different testimonial cards so what i'm going to do is i'm going to just take this entire structure and i'm going to come down and replace each item with it when we click save you'll see that it's all looking the same and then now we can just switch the individual parts so the second testimonial if we open up the desktop design it's going to be jonathan so based on the image names here we have image jonathan so we really can just come into the find the second one here so testimonial to replace the link here click save now we have jonathan in here and then you just update the rest of the text so i'm going to speed through this and i'll see you when i'm done all right we are done and you can see that i've got all of the text and images in here now it's just a matter of styling these if you look at the main design they're all about the same so we have the image in a circle on the left with the name and the verified graduate right under it and then the actual testimonial below so it makes no sense to try to target each of these individually we'll just set a common class among all of them and then style it one time let's start with the image and the name to the right of the image the first thing that we need to do because how i see this is we've got the image on the left and then the name and verified graduate stacked on top of each other so to get those next to each other a great way to do it is flexbox so the image will be flex item number one and then we'll make a container around the name in the verified graduate which will be flex item number two to do that we just come to the html we'll go to the first one here and we'll come right above the image and what should we name this we'll just call it a class of header and then within header we're going to have the image and then another container with the other text so i'm going to call that container header text okay so now all we have to do is drop in the image up top and then these two p tags in this container down here i'm going to split our style.css to the right here in visual studio code so we can see both and as we look at the header in the header text we can come down since this applies to both mobile and desktop we'll just write it up in the mobile first section where it applies to pretty much any everything and we'll just put at the bottom we want to target header which is going to be display flex automatically when we click save you're going to see it jumps on the to the correct line and then these are lined up so if you look at this the picture and the text they're like on the same horizontal axis so the way that we get that to happen is align items so we're aligning items on the cross axis to be center so now that's going to work and then of course we need to grab the image so from the header container we want to grab all images and we want the border radius to be it's just a trick to make circular pictures we want that to be 50 percent so you see now we have a circle picture and then we want the margin right to be let's call it 5 pixels actually that's not enough so let's do 10 pixels now you can see there's a little space between those lastly we need some space under this header container so we'll come back up to header set a margin bottom of maybe 20 pixels and that will space us out from this top header container to the text under it now of course this has not applied to the rest of the testimonials but we will follow the same exact process as long as we keep the same class on them these css styles should apply just fine so i'm going to speed through this so you don't have to watch me do it um completely i'm just going to go and create this exact structure uh below for each of them and probably should have done this the first time around but whatever we'll we'll get through it okay so i got that done and that's kind of a good lesson there you usually want to finish like one iteration so one card um get it all correct and then apply the styles to the rest i didn't do that so i had to pay for it with some extra time the next thing i'm going to do is space out these two pieces of text so the first one is in h4 the second one is a paragraph tag so we can select that with our our styles pretty easily all we have to do is look at any grid item class and select the h4s and add a margin bottom so we'll say grid item h4 and we'll set a margin bottom of 20 pixels maybe i think that looks okay so 20 pixels yeah it looks pretty good we're getting close to the finish line here the next thing you'll notice is there's rounded corners on each of these cards that'll be pretty easy to apply so let's go find our grid item style which selects all the grid items and we'll set a border radius of around 5 pixels maybe that'll make those rounded corners so that looks good there's also a little bit of a drop shadow on each of these or at least the white ones but we'll just go ahead and put it on each of them just for simplicity so any grid item is going to get a box shadow you can go to a box shadow tool online this is just the standard that i always put in there is a black color with 20 opacity and then i kind of tweak these values right here until i get roughly what i want so you can see there's a little box shadow there i might spread this out a little bit more it's not exactly what we're looking for but it is it's pretty close and i'm not too concerned about getting this verbatim to the design the next big piece to this is the font so in the style guide if we go here to the style guide it's going to tell you what font you need to use so for typography the body copy is 13 pixels we haven't set that yet um actually let's let's do that really quick so for grid items you could set this at multiple levels but for grid items we'll set the font size to be 13 pixels makes it a lot smaller here for us and then for grid item h4s we'll set the font size back up so we'll say maybe 1.2 rem so that's the root element is going to be [Music] the html which i believe is set default to 16 pixels so that will make it a much bigger text and i think that looks relatively good decent proportions there so i'm okay with that and now we have to actually change the font itself so in the style guide it says that we need the barlow semi-condensed font from google fonts i happen to have this open already let me expand the screen and all you have to do is select the styles that you want so we will do that and it looks like we need 500 and 600 font weights so we will go grab medium which is 500 and 600 which is semi bold now it's saying that we just need to put this link in our html document so we'll copy that should be good on this so let me resize the screen real quick and now all we need to do with this is put it into our html so we put this in the head and we can put it right by our style sheet link so i think usually you're supposed to put this after your stylesheet so that this applies i'm not entirely certain if that is that matters a ton let's save it by default it's not going to be using it because we haven't enabled it so we'll have to come back to our styles.css and in the body this might be where we put the font family and this is where we'll go to the rules here and we can just paste in the css rule actually so we'll be using barlow semicondensed with a fallback of sans serif save that and you can see all the font is is looking a lot closer to our demo here you'll notice that these white cards have a gray text rather than the black text we're giving it so let's update that i believe we have a rule for this already so we can actually condense testimonial five and three into one because they have the same styles and then the font color that we need if we go to the style guide is probably this light gray i'm thinking nope that's a little too light so we'll try the dark grayish blue and i think that's a little bit closer may not be exact but i'm i'm thinking it's close enough it's it's better than the black that we had before now the smaller text has a little bit less opacity than the main text and then we also need to send the set the font weight so let's look at the style guide it says for the typography or note for the text colors the verified graduate okay we're not ready for that yet review paragraphs inside quotes have the same color but are 70 percent opacity so in our design here we have a couple different p tags so we have the name verified graduate and then the testimonial down here so if we just set the font weight of a p tag and the opacity of a p tag for all of them that will apply to mostly what we want because we want this testimonial to have 70 opacity and probably verified graduate as well and then we can specifically override that opacity style to be a hundred percent for the name uh later so let's let's go ahead and do that so we'll see if we have something for this already so we have grid item so we'll just set the grid item p tags so any any p tag within the grid item we want to set the opacity to 70 percent and then for the name let's just add a class so we can use the alt key to come into all of the p tags with the name just holding down alt and we can give it a class name and then we can come down into the name selector and give it an opacity of 100 maybe not the most effective solution but it works and we probably need to select it with header to be more specific than our grid item p because header and name that's two class selectors is more specific than grid item p which is a class and a tag selector so this one takes precedence and we'll override it now i think we just need to tweak a few things with the text here not a whole lot we might want to make the name just slightly larger than that default 13 pixels so we can come to that rule that we just created so the name and we'll say the font size is going to be [Music] 1.1 rem actually that's going to be way too large um how about one rem so 16 pixels that looks better if you open this up you might see a little border around these pictures too so while we're here we can look at the header image and we can give it a border of one pixel solid maybe purple i think some of them are different but let's just go with purple on all of them you can see that little border that we have around there you can tweak that how you like it lastly i think the font weights look okay but they did tell us to import weights of 500 and 600 so we should probably explicitly set those so on the grid grid item h4s will give a font weight of 600 looks about the same so not a huge change for the most part i think i'm happy with the design we have it's not absolutely perfect but it's pretty close and more importantly we've gone through a lot of the concepts we've covered in the previous videos which is always fun to apply those leave a comment if i missed anything i think the only thing remaining is this big quote up here and there's a couple ways you could approach this i do not believe there's any assets for this like this is not an image actually it is so this is an svg and we could use this svg i don't want to get too far into css and svgs it's like a whole other topic but i'll go ahead and just quickly put it in and show you how to do it so what we can do is this background pattern quotation svg if we click on it it's just a bunch of numbers but we can actually put this into an image i believe so if we come to our index.html let me get this css collapsed a little bit and it's obviously on the main card the purple card here so number one we can pretty much put it wherever we would like here within this main card so let's make an image and then the source is going to be images and then background pattern quotation so now you see that quote is already in there that's nice now we just need to absolutely position this because it's in that weird spot so it's like kind of randomly up there in the top right corner let's give it an id so we'll give it the quote id and then we come over to our css and we will select quote and we'll say position absolute and what that is going to do is position it relative to i believe the viewport by default um let's see how that works so from the right we want it to be 20 pixels roughly okay yeah so it's relative to the viewport we want it relative to this container itself so to do that we need to set the testimonial one container to be position relative and once we do that that quote will come in correctly come down to quote it should be probably more like 30 pixels from the right and then from the top it is pretty much flush with the top so we'll do zero you can see that gets us a little bit closer um and as we resize this it's obviously going to change that a little bit so maybe from the right we go like three percent i'm just going to experiment around with this until we get it right maybe like seven percent that's closer let's just ramp it up to 15. okay i like that i think 15 is pretty good and like i said it's flush with the top so that's good and then finally as we resize this i know this doesn't look amazing we're just kind of in that middle state where it's like this is like bigger than a mobile device but smaller than a tablet so you're really not going to see this on any device for the most part so like here is about where you're starting you're going to start seeing it and it looks like the quote is like a little bit above that text and in our actual design here it's showing it as behind the text like overlapping that's partially because of the spacing that we've given it so i'm not overly concerned about it but as we resize it and it does kind of overlap text we want that to go behind everything so to do that we need to go to the styles we need to set the z index equal to like negative one actually let's let's set this the index to one and then on the [Music] testimonial one we will set the z index to be uh zero so it's above the testimonial card but then we want it to be below the testimonial text so we'll set the z index of the header to three i think that might get us there maybe not there we go we just need to set it to position relative so that the z index applies and now it should be going behind any text and stuff like that so should be good there and i think our design is pretty much complete so as you can see like we could spend the next hour or two hours trying to get this to look exactly like the example here just if you're curious i took a second to try to figure this out fully exactly how we can get it to look like this and didn't come all the way there but i did get it somewhat close so i put a max width of 1440 according to the style guide here where it says the desktop is 1440 and then i set the testimonial five and main left containers to eighty and twenty percent respectively so this looks okay i think there's it's not exact like i said but it's a little bit closer let me know in the comments if i missed anything but as we resize this it should be responsive we have this like middle state where it's a little bit weird but honestly trying to perfect that is just going to be a lot of tedious tweaks that you can kind of do on your own if you want but the main goal here has been accomplished on desktop it looks like this and on mobile it looks like this where we have everything stacked on top of each other so now at this point we're going to take this local host that nobody can see and we're going to deploy this to github pages so that you can pass it off as a link and show off your project so the way that we'll do that let's close out everything here so we have a clean workspace so what we can do is we can open up source control this is embedded within uh visual studio code and what we can do is initialize a repository and this can be done on the command line too but i think we should you know since we haven't learned git yet and i don't expect you to understand any of this quite yet might as well use the visual interface so from here it's we initialize the repository and it says control enter to commit on master so we can write a message just say first commit and then control enter we're going to stage all our changes and commit them directly yes and i'm i'm actually set up with github already so it may ask you to go through some authorization where you have to sign in to github so go ahead and do that if you have to now what we have to do is create a new repository so on our github so if you don't have an account sign up for github you'll need it uh for the rest of your developer life click new repository and we'll just say testimonial grid front end manner challenge solution very long repository name you don't need to make it that long and we'll just create the repository make it public not too worried about initializing all this stuff now from here you're just going to copy the https link come back to visual studio code click these three dots click remote add remote and then we're going to put in that url from there we should be good i think we want to push it to the main branch so let me see what we're working with here yep so we need to push to main but this is just the name we're giving it so origin we're just linking up our code here to the github repository again you don't need to understand this we're just trying to get it deployed here so um now we're going to click push okay then let's check it out here we'll refresh and you'll see that we have all of these uh files that we just created from visual studio code here on our repository right now we don't have it deployed to github pages the way that we can do that we can do it here or we can do it within visual studio code but quite honestly doing it within the repository is is relatively straightforward since we already have index.html in the main root of the directory so we'll go to master find or create a branch and type github pages and it says create branch github pages from master which is exactly what we want to do this is master what we're looking at right now so we'll create that branch and now let's go back to our main repository and i believe it should publish this to github pages in the next minute or two it doesn't happen like instantaneously because what it's actually doing is taking our code files and putting them on github pages which is just another server and then serving them over our github url let's refresh the page looks like our environments has been set up now so we can click on the main repository we can click github pages and then view the deployment and you can see that what we just created is now live in this link up here you can copy and share with someone else and as we resize it it of course is going to resize to that mobile mobilely responsive width and then desktop responsive all right that is the end of the front end web development foundations course it was a long one i applaud anyone who got through every minute of it hope that you enjoyed it be sure to give free code camp a like on this video and subscribe to their channel once again there is a link in the description with some additional resources for each of the topics that i covered so that if you want to take it to the next level you can while this is a long course 20 plus hours it's only the beginning if you want to be good at front end web development or even extend yourself and get into full stack development you're going to need to practice there's going to be a lot more learning to come but hopefully this course gets you on the right foot it gets you started in the right direction on your career to becoming a web developer i wish you the best of luck in your journey to stay connected with me i'm pretty active on twitter at zg underscore dev dev so feel free to connect with me there i've also got a youtube channel of my own would really appreciate you hop over there and check out what i've got on that channel so long and happy coding