Become a Fullstack Developer from Scratch – Full Beginner’s Tutorial
This massive course will prepare you to
be a full stack developer. It's taught
by many different instructors and put
together by Scribba. Here's Pere
Borggan, the CEO of Scribba, introducing
the course. Hi there and welcome to this
massive full stack developer path. These
modules that you are about to watch have
helped countless students over on
scribble.com break into the tech
industry as developers and not in the
boring theoretical way. No, in a fun way
where you build games, clone Google and
Instagram, build AI investing apps, even
Chrome extensions. And when I say you, I
really mean you. Because the only way to
learn this is to type the code out
yourself. Throughout this course, the
teachers will throw challenges your way,
actually over 500 times. And the starter
code for these challenges can be found
on GitHub or over on scrimba.com in the
interactive expanded version of this
course. We're going to start out with
the basics of HTML, CSS, JavaScript, and
so forth, and then move on to the
frameworks and technologies that are
used in the industry like React, Node,
Next, SQL, TypeScript, testing, and even
AI engineering. So, this really is your
one-stop shop towards becoming a
professional fullstack developer. Now, I
would love to follow you on this
journey. So, be sure to send me a
LinkedIn connection request so that I
can share for you from the sidelines.
With that, let's dive in.
>> The French philosopher Renee Deart once
said three clever words,
ergo, sum, which translates in English
to I code, therefore I am. So let us
prove to ourselves that we actually do
exist by coding this out in HTML. And
the way we do that is actually not too
different from how you would write this
out in a regular text editor. as there
you would simply write out the sentence.
And then if you wanted it a bit more
prominent, you'd probably highlight it
and then click on normal text here and
choose another content type for your
text. You can see heading one, heading
two, heading three. Let's hover over
heading one and then click on it. And
there you can see that the text became a
lot more prominent. It became a heading
one. Now if we want to do this in HTML,
we'd simply create an HTML file which
you can see here. It's called
index.html. HTML as that is a common
name to give your HTML files. And here I
would write I code therefore I am. Now
if we open up a browser and point this
browser to our HTML file and then run it
then we can see that the text indeed
appears in the browser. However, it's
pretty small. So if we wanted to turn it
into a heading one instead what we do is
wrap the text in HTML tags. So before
the text, I'll do this two angle
brackets with the text H1 inside of
them. That's short for heading one. And
then at the very end of the text, I
would do the same H1. But since this is
a closing tag and not an opening tag, I
would also have to give it a slash
before the text. And now if I rerun the
code, there you go. We have made our
text a lot more prominent. Now I think
our website should give proper credit
here. So let's write reneart underneath
the H1 like that. If we now rerun the
code, you can see that the text is just
normal. It's not an H1. And that is
because it is only the text which is
within the opening H1 and closing H1
that gets formatted as a proper heading
in the browser. In my view, this line
right here should be a bit bigger. I
want it to instead look something like
this where the name is pretty thick and
big but not as big as the heading one.
And that brings me to your first
challenge. I'm going to paste it in
here. So, what you are going to do is
try to make our web page look like the
design in the provided slide. And if you
are struggling to think of what kind of
HTML tag you'll have to wrap this name
in, well, I've got a little hint for
you. What comes after one? So, pause
this scrim, jump into the code, and try
to solve this challenge right now. And
then when you return back to me, I will
of course show you the solution as well.
Good luck.
Okay, hopefully that went well. The way
you solve this is by instead of using
the H1, using its smaller sibling, the
H2. Like that. It has the exact same
so-called syntax as the H1. So instead
of a one you use a two. If we now run
this, there you go. We have achieved the
design we wanted. So great job. Let's
move on.
Before we move on to the next challenge,
there's one thing I want to make sure
that you are aware of. And that is that
if you have an HTML file locally on your
computer and you have a browser, what
you can do is simply drag and drop that
file into the browser and then it'll be
rendered. So that is actually more or
less the same thing that's happening
here on Scribba. Though here we have a
mini browser and the text editor inside
of well your browser because Scribba
itself is browser based. So a little bit
meta but it's essentially the same
thing. Okay. Now, it's time for you to
get some more muscle memory on how to
write HTML tags and also get to know two
new tags because I want you to create a
news article and more specifically this
news article right here which has a
title that says humans have reached
Mars. It has a subheading right here and
also some regular text. And the HTML
tags are H1 for the title, H3 for this
one. You haven't used an H3 for, but I
think you know how to do it. And then
there's finally a P tag for the text at
the bottom. P is short for paragraph and
I have provided a little syntax example
for you here. However, if you've
understood the logic of how to write
these tags by now, it shouldn't come as
a surprise how you do that. Okay, here
is also the text so that you don't have
to write all of that out. You can just
copy paste it instead. So now starting
here on line 13, write the HTML we need
in order to create this news article.
Okay, hopefully that went well. Let's
start with the H1. I'm going to create
the open and closing tag and then paste
in the text like that. Running the code.
Yes, it works. Moving on to the H3.
Copying the text.
Then rerunning the browser. And there we
go. Finally, there is the paragraph
like this.
rerunning the code and there we go. We
have achieved this exact layout. Great
job. Let's move on.
Now you are going to learn about images
because I really think our Mars article
needs an image. The way to do that is by
using the img tag. So we start with our
angle brackets and then write simply img
inside of those. And what's unique about
this tag is that you don't need a
closing tag. So there is no closing img
as there is a closing a run or closing
paragraph. This is simply not needed. So
it's often called a self-closing tag and
some people use the slash at the end of
the opening tag like this just to
signify that it is closing itself.
However, it's not really needed. So we
can skip it. And I think in general that
less code is better code. So I am just
going to skip it. However, if you prefer
to have it like this, that's totally
fine as well. Okay, enough syntax. How
do we get this image to work? Because
right now, if we rerender this page,
nothing has happened. And that is
because we haven't told the image tag
which image we want to render. And the
way to do that is by writing src, which
is short for source. And then we write
equals, and then two quotes, because
inside of these two quotes, we're going
to specify the image we want to render.
And here on the left hand side you can
see I have mars.jpeg.
It is in the same directory as our
index.html file. Meaning I can simply do
mars.jpg.
Then run the code. And there we go. We
have our image. That is super cool.
However, you can see that this image
behaves a little bit differently from
our paragraph for example because where
is the paragraph take up the entire
width? the image just take up whatever
width it needs, leaving us with white
space on the right hand side if the
browser is too wide or cutting off the
image if the browser is too narrow. And
I actually want this image to behave
like our other paragraphs. So I'm going
to show you a little hack to do that
because normally you'd use CSS for doing
this. But there is a little trick we can
use by simply utilizing the width
attribute. So that is an attribute just
like source is an attribute and it uses
the same syntax. We'll do with equals
and then two quotes and then inside of
the quotes we can write for example
100%.
If we now rerun this you can see yes our
image is now scaling with the size of
our browser looking super neat. Now as I
said this is a hack. So, if you show
this around to people, you might get
this annoying guy saying, "Well,
actually, that's not valid HTML
according to the specification." But you
know what? Let's not care about that. At
this point, you should not give a darn
about specifications, validity, and aka
correct HTML. You should instead look at
yourself like a hacker who grabs
whatever tool you need in order to get
the job done. So now we're going to use
this even though we know it's a hack
because later down the line you're going
to learn the proper way of doing this
using CSS, the language we use for
styling websites. So with that out of
the way, I want to show you one more
thing before I give you a challenge.
Because one thing that's super cool is
that you can actually borrow images from
wherever you want online. So if you find
an image you like, you can borrow it and
use it in your project. Let me show you
how. So if we Google Mars for example
and click on the images tab and then
click on whatever image we think is
cool. For example, this one right here.
And then in this image view on the right
hand side here, you can rightclick and
choose copy image address. Once you've
copied that, you can simply paste it
into the source attribute. So I'm going
to remove Mars.jpeg and paste in the
image address which I just copied. If we
now run the code, you can see there we
go. We have borrowed this super cool
image from another website online.
So, it's time for a challenge. What I
want you to do now is add a second image
to our article. I want you to find one
via Google and then place it under the
H1 element but above the H3 element. So,
that is right here. And finally,
remember to use the width attribute to
make the image as wide as the other
elements. I'm going to leave you here
with the marks image again and wish you
good luck. go ahead and solve this
challenge right now.
Okay, hopefully that went well. What you
needed to do was write the angle
brackets img. Then set the source to
whatever image you found online. I'm
going to reuse the one I found earlier
like that. If we're rendering this, this
is a really wide image by default.
That's not what we want. We want it to
scale with our browser. So, we need the
width attribute. width equals a 100%
like that. And now we have two super
cool images in our article, both scaling
up and down with the size of the
browser. Great job. Let's move on.
Okay, now you're going to learn about
something called nesting, which is the
act of nesting HTML elements inside of
other HTML elements. So, let me show it
to you in practice. Let's say that we
want to group all of these elements here
together. What we then do is above all
of the HTML tags, write another HTML
tag. And in this case, we use the
so-called div, which is short for
divider. However, I don't think that is
a very good name. I would rather call
this, for example, a container, though
div is what it's called. So, we're just
going to have to stick with that. Now,
in order to nest elements inside of this
div, we'd write the closing tag below
all of the HTML tags in our article. And
then for readability purposes, we'd
indent all of the tags in our article.
So now our H1, image, H3, P, and image
here are all children of this div tag
right here. This doesn't change our
layout at all. So this div tag here is
invisible. However, it has now given us
the ability to target the entire article
just by targeting this div right here.
And that is useful if we, for example,
want to add some styling to our entire
article using CSS or somehow manipulated
using JavaScript, which is the
programming language that you use in
browsers. And very often, you'll
actually see a ton of divs on a page. So
for example, you'd see another div
around this image element here, which
then would have been indented yet
another time in order to signal that
this is indeed a child of this div,
which again is a child element of this
div. Now if we were to visualize an HTML
document, it would look something like
this. You'd have, for example, a div up
here and it would have two children
which could be their own divs like this.
And then this lefth hand div here would
for example have an image tag as its
first child and also an H3 as its second
child. The other div on the right hand
side could have an H1 tag as its child,
P tag as its child, maybe even another
div as its child. And then if we go yet
another level deep, this paragraph tag
might have its own child, which could be
for example an A tag, which you haven't
learned about yet, but which you'll
learn about soon, as that is the
so-called anchor tag that allows you to
create links on websites. And looking at
this visualization, you might think,
well, this kind of looks like a family
tree. And yes, it does. And that is
exactly why we call this structure the
HTML document tree because we are inside
of an HTML document. and how they are
nested inside of each other becomes kind
of like a tree where you have a trunk
and then branches going out from that
and then yet again new branches going
out from those branches and so on and so
on. So now that you know that let's move
on to the next scrimm.
Okay, now we're going to use your newly
acquired internet superpowers in order
to perform a little practical joke
because you might have a friend who is
far too obsessed with a sports team. For
example, I myself am a Manchester United
fan and one of the worst things I can
imagine is if some rich billionaire buys
the club and changes its name. That
would make me truly outrageous. So, what
we're going to do now is change the news
on BBC Sports so that you then can show
this page to a Man United fan and thus
make them really upset while you get a
good laugh. So, let's go ahead and do
this here on the BBC Sports page on this
top article here. I'm going to write
click on the text and then you can see
it gives me the option to choose
inspect. Now, note that this is on a
Chrome browser as that is what I'm
using. If you're in a different browser,
there might be slightly different steps
here or a different wording, but overall
in general, the steps are the same. So,
I'm going to click on inspect. And then
you can see that it opens up this panel
on the bottom of the browser. This is
called the developer tools. And here you
can see we are in the elements tab. And
that shows us the HTML elements that are
on the page. And here the one we have
chosen now which happens to light up
this area on the BBC site contains the
title of our article and it is an A tag
which I've just briefly mentioned and
that you're going to learn more about
later. Now we can't see any text here.
So I'm going to click on this age tag
and there you can see it contains an
age3 tag nested inside of it. If we
click on that one as well you can see
that there we see the text that is
rendered in the browser. So here we can
double click on that text and change it
for example to something as awful as
Jeff Bezos to buy Man United and rename
the club after himself. Oh my god that
is about the worst piece of news any
United fan can read. So if we now hit
enter there you can see boom we have
changed the text on BBC. Now let's click
on this paragraph here as well open it
up because there we can also change the
text that is written underneath the
title. Let's paste in the US billionaire
says he doesn't follow the Premier
League but wants to own the club to grow
his personal brand. This just gets worse
and worse. So now if we hit enter that
is also rendered in the browser. Now I'm
going to scroll a little bit up in the
dev tools here and hover over this div
tag here because as you can see that
highlights our image. So it seems to be
that our image is nested inside of a div
tag. Let's open it up by clicking on
this triangle. And there we can see
another div tag. Well, okay. Still
highlighting the image. Let's open up
that one as well. Oh, wow. There we have
yet another div tag. A div inside of a
div inside of a div. This is web
development in a nutshell. Let's open it
up again. And there, yes, we can see our
image tag. I'll scroll a bit down. And
you can see there are tons of different
attributes here. We're going to ignore
almost all of these and then instead
direct our attention towards the src
attribute. That one we know because that
is the one we need to change in order to
change this image. So now let's open up
a new window. Search for Jeff Bezos.
Click on an image we like. For example,
this one right here. Right click on it
and choose copy image address. Then can
do now is double click on this source
and then remove the existing source
inside of the quotes like that and then
paste in our image of Jeff Bezos.
However, if we now hit enter, you can
see nothing changed. And that is because
BBC is a little bit fancy here. They use
another attribute called src set, which
helps the browser render different sized
images on different types of screens
depending on what kind of image is
appropriate for the given screen the
website is rendered on. So, what we're
going to do here is just double click on
the src set so that everything's
highlighted and just delete all of it
and then hit enter. And boom, there you
go. We have Jeff Bezos looking pretty
ready to acquire this football club and
rename it Bezos United. Finally, I'm
going to close this panel by clicking on
the X in the right corner here. And
there you go. We have our article. So
now the final step is just to take this
site, show it to someone who is a
Manchester United fan, and get a good
laugh. Now, a final notice. If you
refresh this page, you will lose your
changes because you only made your
changes locally. And when you refresh
it, the entire page gets the fresh data
from the database of BBC and not from
whatever local changes you did. And
since it is local, that also, of course,
implies that your changes only happen on
your computer. You don't change BBC in
production, of course. So, go ahead and
play around with some sites online. See
if you can come up with a fun little
practical joke for a friend. Have fun
and I'll see you in the next Scrim.
Now, we're going to get you on boarded
to the Scriber community because this
career path is so much more than just a
course. It's also a social experience
where you are able to get help, meet
like-minded people, network, and find
inspiration. Our Discord server is at
the heart of our community and thousands
of coding students just like you are
online at any given time. We pride
ourselves on keeping our Discord an
ultra supportive and friendly space and
we welcome coders of all abilities from
complete beginner to professional and we
have a ton of channels for all kinds of
coding and workrelated subjects. So in a
moment click on this link, this
screenshot or any of the screenshots you
see in this scrim. That's right.
Remember, a scrim is not a video. So,
the link that you can see right here,
that is a live link. You can click it.
This is going to take you through to
Discord, and you can just follow the
steps to get registered and onboarded
into our Discord server. And that will
no doubt involve proving that you're
human by identifying elks or something.
Once you're done, why not make your
first stop on our Discord server, the
introduce yourself channel. Let us know
who you are, where you're from, what
your aims are, what you like to do when
you're not coding. This is your first
opportunity to interact with the
community. And then why not browse the
channels? And I'm just going to point
out a few highlights. We have the
announcements channel for news and
updates. There's a stay accountable
section with today I will and today I
did. These channels can help you set
goals and brag about your success.
Career chat, as the name suggests, is
where you can get and give help on
career related matters. The I built this
channel is a great place to show off the
amazing projects you've been building.
And if we just scroll down a little bit
more, we can see that we've got the
general help channels here for
JavaScript, design, HTML, React. And
then here we've got specific channels
for each module of this career path. So
you can always go there if you get stuck
and you need a bit of help or if you
want to pay it forward and help out some
other students. Now, if we go right back
up to the top, we have event. And this
will show you up and cominging events
for the Scriber community. Every Tuesday
we host the Scriber town hall on
Discord. And if you click into events,
you'll see that the next town hall is
already scheduled and it will show you
the time in your local time zone. So for
me here in the UK, it's at 5:00 p.m. on
Tuesdays. The town hall is a live event
especially for the Scriber community
with 45 minutes to an hour of chat on
all things Scrimber, learning to code,
and getting jobs in tech. Each week is a
bit different. Sometimes we interview
students who recently got hired to hear
about the process for example or we
review some code or give feedback on a
CV. But we always have time to answer
your questions. So do come along and
join us. It's an interactive experience.
You can chat with us and it would be
great to see you there. It's a great
chance for you to interact with the
Scriber team and have your say. You'll
find the town hall channel right here in
the sidebar. Also check out the
Scrimbert podcast. There is a new
episode every week and it's packed with
motivational advice and job hunting
strategies from developers who've been
exactly where you are now. You can click
this slide or find it wherever you
usually get your podcasts. I'm going to
leave you with these two links. We've
got the Scriber podcast and our Discord
server. Once you've checked them out,
come back here and you're ready to get
started coding your very first web app.
Now you're going to learn about your
very first interactive HTML element and
that is the button tag because buttons
are all over the web. So to create a
button, simply start with your angle
bracket and a button opening tag and
then a button closing tag like that. And
in between the two tags, you write
whatever text you want inside of your
button. Now, as you can see, it's styled
with a background color that also has a
border around it. And you can even click
it. Now, this button doesn't do
anything. In order to hook this up with
some kind of feature, for example,
trigger a purchase. Then you'd have to
learn JavaScript, and that is outside
the scope of this course, but something
you'll learn later in the front-end
developer career path. So, for now, our
button will just be there and pretend to
do something while actually being pretty
useless. Now that you've seen this once,
I want to put you on the spot and give
you a challenge. I want you to create a
layout according to the design in this
slide. So imagine that you are building
a website where people are to sign up.
Then it would be very natural to have
something like this where you have an H1
at the top welcoming them, a paragraph
with a little message and then a sign up
button. So I want you to create this
layout. You don't have to write the text
because I've provided it here. Just make
sure that you set up the HTML tags
correctly so that it renders properly on
the page. Good luck.
Okay, hopefully that went well. Let's
now do it together. I'll start with the
welcome H1 tag like that. Paste that in.
And boom, there we go. Next up, there's
the paragraph, opening tag, closing tag,
and then pasting the text in between.
Running this code. It still looks
correct. Finally, the button like that
with sign up written inside of it. And
boom, there we go. We have our
completely useless signup form that
doesn't do anything. But in the next
scrim, we're going to add on to this. So
stay tuned and then I'll see you there.
The signup section we have here is
actually pretty useless because there's
no way for our users to add, for
example, their username or a password.
So in this scrim, we're going to fix
that. Which leads us to the input tag.
The input tag is just like images, a
self-closing tag. So we only write the
opening tag and not the closing tag like
we do with buttons for example. However,
we have to specify what kind of type our
input should be. So it has this type
attribute just as the image has a source
attribute and the value of the type can
be a range of different options, but
we're going to start off with the most
common one and that is text. If we run
this, you can see we get this nice
little text box in the browser. Another
handy attribute is the placeholder.
That's where we can give the user a hint
about what kind of data we're asking
for. So, for example, enter username
like that. If we run this, you can see
it says enter username. Okay, let's do a
mini challenge. We of course want them
to enter their password as well. So, I
want you to create a new input field on
line four, which is identical to the one
on line three, though it should say
enter password in the placeholder as
opposed to enter username. And don't
just copy this line and change this
word. I want you to get some muscle
memory here. So, go ahead and do that
right now.
Okay, hopefully you did like this. Input
type equals text placeholder enter
password like that. And there we go. Now
you can enter a username and a password.
However, in the real world, passwords
are never exposed like that. The
characters are always masked. And the
way we achieve that is simply by
changing the type from text to password
like that. If we run the code now, you
can see the username is shown but the
password is masked. Really nice. And
speaking of this type attribute here, it
can take a range of different values
which turn into really interesting
interactive elements on the page. For
example, you can do type equals date.
Let's say you want the user to add their
birth date during sign up. And then you
get this nice little date picker. Or you
can do type equals time. For example, if
you want the user to be able to
configure when they should get some kind
of notification, that gives the user a
nice time picker. Or let's say you want
to give the user a way to configure the
theme of their profile with colors. Then
you can do type equals color. And then
you get this powerful color picker where
the user can add basically any color
there is through moving around on these
dots. So, the input tag is a really
versatile tool in your toolbox. Okay,
let's move on to the main challenge
because I want our users to have profile
pictures, which means that you need to
add an HTML input field that lets them
upload image files. And I'm not going to
tell you what kind of type that input
field has. I'm just going to show you
how it looks, which is like this. You've
probably seen it online before. It's
basically a button. And when you click
it, it lets you choose an image from
your local file system. And the reason I
want you to figure out what the type
attributes should be here is because
being good at Googling is actually huge
part of being a good programmer. And the
two are actually not that easy to
separate from each other. So this gives
you a way to practice those Googling
skills. And I'm going to give you a hint
that the keywords you should use are
found in this last sentence right here.
So go ahead and solve this challenge
right now and then I will of course show
you the solution when you return back to
me.
Okay, hopefully that went well. Let's do
this. The keywords I think are important
here are HTML input because we know
we're looking for an HTML input tag. And
then what is our intent? Well, it is to
upload image files. And I think Google
can handle this just by combining HTML
input upload image files. If we just
paste that into Google, let's see what
we get. And as you can see, when we
search for that, we get a first hit from
developer.mmazilla.org.
Let's click into that. And here you can
see the title is input type equals file.
And we get this little code snippet
here. And it also says input elements
with type file let the user choose one
or more files from their device storage.
Sounds pretty good so far. Let's zoom in
on the code here. And there you can see
that they are actually using the profile
picture example themselves. Choose a
profile picture with this button which
looks suspiciously similar to what I
just showed you a couple of minutes ago.
So I think we've found it. What we're
looking for is type equals file. Let's
add this up above the signup button.
put a equals file like that. If we run
this code, yes, there we go. We get this
button which looks like exactly what
we're looking for.
Okay, now we're going to move on from
using Google to building Google. I mean,
how cool is that? You've learned HTML
for just about 30 minutes and now you're
going to build the most visited website
in the world. That is just amazing. As
you can see here on the similar web
overview, Google is at the very top
amongst the most visited websites in the
world, beating sites like YouTube,
Facebook, Instagram, Wikipedia, and all
the way at 10th, a pornography site, of
course. And by the way, it's a little
bit sad to see that this adult site has
almost three times the average visit
ruration of Wikipedia. So, a pretty deep
insight about humankind right there.
Anyway, we are going to build google.com
and it'll look like this. However, now
you're probably wondering, how can we
make a page look like this? This doesn't
look like regular HTML. And that is
because I've cheated a little bit and
added this line up here, which is a link
tag to styles.css. And if you look at
the file menu in the left hand side, you
can see that actually we have a file
called styles.css, CSS which contains
exactly that cascading stylesheets which
is the language for styling websites and
which you're going to learn all about a
little bit later in this course.
However, for now, I want you to
completely ignore this. What you should
do instead is just create the four HTML
tags we need in order to create a
google.com clone. And then this CSS will
be pulled in and take care of styling
the elements as they should be. So that
it will look like this. And to give you
a hint, this is an image file pointing
to google.png, the file I've added here.
And PNG is just a file format just like
JPEG. You can use it in the exact same
way as we used JPEGs earlier on. So you
should know how to do that. And this is
an input field. You have to figure out
what kind of type it is. I think you
might have a clue. This is a button on
the left hand side. And this is also a
button. So basically it is these four
tags you need to create in order to
build a clone of the world's biggest
website. So go ahead and do that. Start
down here and ignore the line up here.
And then when you return back to me, I
will of course show you the solution as
well. Good luck.
Okay, hopefully that went well. If not,
no worries. Let's do it together. So,
starting with the image tag for the
Google logo, I'm going to do img like
that. And it's a self-closing one. So, I
don't need a closing tag, but I need a
source attribute. And it should point to
the Google.png file. So, I'll just do
Google.png
like that. And let's run this. And ooh,
there we go. We got the logo. Okay,
moving on. Let's do the input field.
Also, a self-closing tag.
running the code and there we go. We
have a input field looking pretty good.
Okay, next up there's the two buttons.
One saying Google search and one saying
I'm feeling lucky. So I'm going to add
both of these button Google search and
then since I'm lazy I'm going to copy it
and do I'm feeling lucky like that.
Let's run this. And there we go. We have
a Google clone with just four HTML
elements. So, if you did this, give
yourself a pat on the back. It is
fantastically well done. You should
really be proud of yourself. And if you
didn't manage to do it, just try it
again and I think you'll make it. At
this point, it might be a good idea to
take a little break and digest
everything you've learned. And then when
you are ready to continue, I will see
you in the next scrim.
Okay. Now that you've built google.com,
you probably want to showcase your
project on some kind of portfolio page.
So, let's pretend we're creating exactly
that. Here we have the index.html page,
the main page of our portfolio. And as
you can see, it says Pier Harold
Borggan, developer, teacher,
procrastinator. So, a really good
description of myself. Though, what it
lacks is some kind of page or section
which describes my projects. though I
have actually created the underlying
page for exactly that over here on
work.html.
As you can see, this is a few tags that
describes my google.com project.
However, there's no way for the users to
know this. The only way they can get to
this page now is by actually typing
work.html up in the browser here and
then it is rendered. We don't want that.
Instead, we want some kind of link from
the index page that is this one that
points towards the work.html. HTML. And
in order for us to achieve that, you
have to learn about anchor tags, aka
links. An anchor tag is simply an HTML
tag with an A. So, let's open and close
an anchor tag and write, for example,
check out my projects
here. Like that. If we run this,
it seemingly looks just like our
paragraph. And we can't click it either.
And that is because for anchor tags, you
also have to provide it with an href
attribute. That's short for hypertext
reference. Really fancy, scary looking
word right there. Don't worry about it.
All this attribute does is decide where
the link should point to. So if we do
equal and then two quotes, we can inside
the quotes simply write work.html.
If we now run this, you can see that it
renders as a link. And when we click it,
boom, we're taken to the work.html HTML
page. Really nice. Now, another thing I
want to show you with with anchor tags
is that you can nest them inside of
other tags and actually inline in a text
block. So, let's say that we want to
link to google.com from this site. Then,
we'll create a paragraph, which can say
something like check out the live demo
here. And we only want the here word to
be a clickable link. Well, then we'll
simply wrap the anchor tag around here
as well and we'll close it before we
close the paragraph. And then we'll do
the href paste in the URL to google.com.
Now, this is a link that points to an
external website, not to any of our
pages. So, if we run this, you can see
that the link is now only over the
hairword. However, if I click this link
now, the Scribba mini browser will try
to head over to google.com and we only
allow the mini browser to render Scribba
projects at the moment. So, that won't
work. Instead, what I'm going to do is
make this link open in a new tab. The
way we do that is through a new
attribute. So, first I'm just going to
break this into a new line just to
improve the readability a little bit.
And then here I'm going to write target
equals
blank. Now, that is a cryptic way of
saying that you want the link to open up
in a new tab. I know. But if we now run
this, if you click on this link, what
you'll see is that in your main browser,
a new tab will be opened at the
Google.com homepage. So, I'm not going
to go deeper into why it's called target
equals blank, but this is the way to do
it. I of course wish that HTML had a
better solution for this. for example,
new tab equals yes, that would have made
a lot more sense to me. But this is how
it is. You just have to accept it. Okay,
that was a lot of talking for me. Now,
it's time for a challenge. I want you to
create an anchor tag that links back to
the index.html page because if a user
ends up on this page, they also need a
way to get back to the main page. So,
create that link here however you want
and then I will show you the solution
when you return back to me.
Okay, hopefully that went well. Let's
create this anchor tag. I'll wrap it
inside of a paragraph and write turn to
homepage and then I'll simply wrap the
homepage word in an anchor tag like that
and point that anchor tag to index.html.
If we run this, you can see return to
homepage. And if we click that, we are
back at the homepage. Great job. Let's
move on.
Okay, we're back at our Google example
here. And now I want you to add a anchor
tag to the page because sometimes you'll
see that Google adds a little text
underneath the buttons. For example,
something about privacy like we protect
your privacy then with a learn how link
that you can click on to read more about
that. So that is exactly what I want you
to do. I want you to add an anchor tag
that points to blog.html.
As you can see, I've added a blog.html
file up here. And I want you to wrap the
link inside of a paragraph tag. So, just
as it is here, you can see it's an
entire paragraph here, but the anchor
tag is only wrapping the second
sentence. So, go ahead and do that right
now.
Okay. Hopefully that went well. Let's do
it. We'll start with the paragraph
opening and closing tag. and then do we
protect your privacy like that. And here
we're going to add the anchor tag in
line. So I'll do a open and close and do
an href as well. Point it to blog.html
and the text should be learn how. Let's
run this. And boom, there we go. We have
added a nice little link here. If we
want to learn more about Google privacy,
then we can click this link. But not
much to see here. Turns out that privacy
probably isn't as important for a
company who makes money by showing ads
to users. Anyway, we can click on this
link, return back to Google, and there
we are. Great job solving this
challenge. Let's move on.
Okay, now we're going to talk about how
to structure HTML documents because the
structure we have here isn't actually
correct. It's a very simplified
structure compared to how it should be.
Though it does work. So, I didn't want
to give you all of these theoretical
boring structure things up front. I
wanted you to build stuff from the get-
go without the added complexity of the
proper HTML structure as my philosophy
is first make it work and then make it
better. However, now we're at the point
where we should make it better. So,
let's have a look at how to structure
HTML documents properly. And HTML is
actually quite like us humans. It has a
head and a body. And what do you place
inside of these elements? Well, in the
body, that's where you place the tags
you see on the page. And that is these
tags here. So, our image, our input,
button, paragraph, and our anchor tag
because these are visible on the page.
Now, of course, the CSS styling up here
also results in a different layout, but
the link tag in itself is not something
that is rendered on the page. So, it
should not be in the body tag. So, let's
just add the body right here. We'll add
the starting tag and then the closing
tag down here on line nine. And then
we'll indent everything inside of the
body like that. So, moving on to the
head. That's where we add metadata. And
what do I mean about that? Well, it can
be many things. For example, styles. So,
this link right here should be inside of
the head. We'll do head and then close
it off here like that. Other things
we'll have in the head is the title tag.
So let's have a look at what that is. If
we navigate over to the BBC football
page and zoom in on the top, you can see
up in the tab, it has a icon. It says
football-b
icon at the beginning here and this
sentence is controlled by elements we
place inside of the heads tag. So if we
want to add a title text in a tab,
simply write title and then for example
google.com would be natural to write
here. Though we haven't implemented tabs
in the mini browser here on screen so
you won't see that. But had we done that
it would have looked like this though of
course would have said google.com
instead. So we don't need that. So let's
just remove it. Another thing that is
controlled by tags inside of the head
tag is how articles are shown on social
media. So if we zoom out a little bit
here and click in on this article at the
top here and if we now were to take this
URL and share it for example on Twitter,
what we then would get is a different
heading on the Twitter card than what
the article natively has and also a
different image than the main image on
the article. So this is metadata that
has told Twitter that if this article is
shared on Twitter, we want you to use
this heading instead of the heading that
is inside of the article. and also this
image instead of this image right here.
So that was a lot about the head tag.
We're not going to use the head that
much, but I wanted to give you an
understanding about why it is important.
Now there's one more HTML tag we need
and that is the HTML tag in itself which
should wrap all other HTML tags. So we
need to up here at the very top write
HTML and at the very bottom close it off
and also then indent everything inside
of it like that. And then there's one
more thing which is to me a little bit
annoying that we need that is the dock
type declaration because you have to
tell the browser that we're actually
using HTML 5 and not HTML 4. That's just
a version of the HTML specification and
that is done by this line up here. You
can see it's blue and it starts with an
exclamation mark. So, it's actually not
an HTML tag. It's just metadata for the
browser. Because if you don't do this,
the browser will assume that you're not
using HTML 5, but an earlier version of
HTML, and that might result in some
quirks on the page. Though, in 99% of
cases, there will be no difference
between adding this and not having it.
So, at times, you'll probably see
tutorials on Scribba where I've
forgotten to add this because for the
purposes of our example, it won't make
any difference. But if you have
something that you want to deploy to the
world and have in production, then you
just have to accept that you need this
sign up at the top. And sometimes you'll
see people write it in capital letters
as well. Doesn't really matter. I prefer
having lowercase letters. Okay, that was
a lot of theory. It's time to get your
hands on the keyboard. And luckily
though, we have a blog.html file which
doesn't have the proper HTML structure.
So your challenge is simply to add the
proper HTML document structure to this
page. And I don't want you to copy paste
my tags, but write them out on your own
instead so that you build up that ever
so important muscle memory. Go ahead and
give this one a shot right now and then
I will of course do it myself when you
return back to me.
Okay, let's do this. I'll start with the
body as that should wrap our visible
elements on the page like that. Then
I'll do the head. Though we haven't
linked to the styles on this page. So
the head will actually just be empty
like this. So I'll keep it on one line.
Moving on. It is the HTML document. And
that should wrap everything. So I'll do
like that. And at the very end, I'll add
the dock type HTML to tell the browser
that we are working with HTML 5, not
HTML 4. So if we now navigate over to
the blog.html site, you can see that
nothing has seemingly changed. And as
goes for the main page itself. This
structure we've added doesn't result in
any visible change. We just have a more
correct underlying code file to work
with. Great job. Let's move on.
Now you're going to learn about lists
because let's say that you for example
want to create some kind of tribute
site. It could be for whatever. For
example, uh I don't know the Beijing
Winter Olympics. If that's one of your
interests, you might want to create a
tribute site where you list out the top
nations who competed or something like
that. And if so, you would have to use
an HTML list. So, let's first just
figure out which nation actually won the
most medals in the Beijing Olympics. So,
I'm going to click on the medals tab
here on the Olympics site. And here we
got it. And zoom in a little bit. And
oh, wow. It turns out that actually
Norway, my country, completely
obliterated
the other countries with 16 gold medals
over other nations like Germany, China,
and the US.
That is really astonishing, especially
consider the fact that the size of the
Norwegian population is this compared to
the other nations here. I mean, way to
go Norway. We got to create this list in
HTML. Okay. So, how do we do that? Well,
we have to use two different tags. One
tag for wrapping the entire list and
another tag for each list item. The
wrapper tag is one called O. That's
short for ordered list. And then inside
of the ordered list, we place the list
items, the li.
So when we do this and write Norway here
render this you can see that the browser
gives this a number as it is the first
item in the list and also indents it a
little bit from the left hand side. So
adding another one here it then gets the
number two and is stacked below the
first one. So let's add the final ones
like that. Looking pretty good. We have
a list just like on the Olympic site.
Now, if we wanted to be a little bit
more diplomatic here and not brag too
much about this insane victory by
Norway, we could change this O to a UL.
That is an unordered list. And as you
can see, then the numbers are replaced
with dots. And now you might ask
yourself, well, why do we need these
unordered list? They look like kind of
boring elements you just use in Word
documents or something like that. Well,
once you learn about CSS, you'll be able
to style your list however you want and
thus make it much more usable. If we,
for example, head over to BBC Sports
here and rightclick on the navbar here
and click inspect, then we'll see that
actually this entire navbar is an
unordered list item with each of the
categories being its own list items. So,
lists are all over the web and important
to learn. Okay, now I'm going to remove
all of this and give you a quick little
challenge because I want you to create
an ordered list inside of the body tag
and then fill it with your top three
favorite foods.
So, go ahead and give this a shot right
now.
Okay, hopefully that went well. Let's do
it. So, we start out with an O tag that
we're opening up and closing. And as for
the list items, even though Norway is a
great Winter Olympics country, it is a
truly mediocre country when it comes to
food. So, my favorite food is definitely
from other places than where I'm from. I
love Italian pasta. I love Japanese
ramen soup.
And lately, I've been really into pai.
So, there we have it. Rendered out on
the page. Really good job learning about
HTML lists. Let's keep up the pace and
move on.
Okay, now you have arrived to the main
project of this section. You are going
to build a website about yourself and
we're going to personalize it as well
plus deploy it to the world so that
anyone around the world can visit this
website. It is going to be so much fun.
And here is the website itself. As you
can see, it's pretty simple with hardly
any styling by default. Just a little
bit on the button and the input field
here. as with the border around this
image. And building this lightly styled
design is the first part, part one of
the challenge. And then in part two of
the challenge, we are going to go crazy
on the styling with just a few
parameters that you are to tweak and
it's going to look so cool. But let's
not get ahead of ourselves here. Let's
first just run through what kind of tags
you need in order to build this website.
So, first off, there is an image tag and
the image you should use should be added
to the file system on the left hand side
here. As you can see, there is a
pair.jpeg image here. And the way to add
an image to this file system is simply
by drag and dropping it over this
section. Next up, there's an H1 here as
well with a title. Of course, you should
write your own name, not my name. And
then there's an H2 heading as we need
some fun facts about you. The fun facts
itself are built using an unordered list
plus list items inside of the ul
element. And then there is a paragraph
with a link nested inside of it that
should go to your LinkedIn profile. So
the point here obviously is to replace
all of this information with fun facts
about you with your name with your image
your LinkedIn profile so that when you
share this with the Scribba community
other people will be able to get to know
you better. And then finally, we have an
input field here and a button that won't
really do anything cuz we're not hooking
this up with a real newsletter. We are
just pretending that you have a
newsletter that people can sign up to.
So once you've done this, you've
completed part one of the challenge and
you've also filled up this body element
here with HTML tags. Next, you'll move
on to part two of the challenge. What
you'll do there is take your existing
design and modify it. So, if we head
over to the index.javaScript file, you
can see that there are a bunch of
so-called JavaScript functions, which
probably look really scary at this
point, but don't worry. The only thing
you're going to do here is take the word
that you see inside of the quotes right
here, and replace them with one of the
words that are commented out in the line
above. because this will give you the
opportunity to change the styling on the
page according to what word you change
this argument to. So what you are to do
here is look at the name of this
function. It says favorite movie genre
and what is that? Do you like space
movies, scary movies, military movies,
romantic movies, cowboy movies and so
on. And then just take your preference
and change it like this. So if you
choose cowboy for example, what then
will happen is that the font on the
website suddenly became like a western
movie. That's pretty cool. And moving on
here you are to answer what is your
favorite fruit? Is it watermelon,
tomato, banana? If it for example is
banana, then you should replace regular
with banana. And what will then happen?
If you run the code, then suddenly your
page is banana colored. Really nice. So
now you are a cowboy banana. That's
pretty cool. Finally, there's two more
parameters you can tweak. First, tell me
is your favorite mode on your computer.
Light mode or dark mode. If you choose
dark and run the code again, then you
can see the colors have flipped and now
it is primarily dark with the brown
background and the light yellow color as
opposed to how it was previously with
the yellow background and brown text
color. And then at the very end, you are
to choose what kind of style you like
your edges to be. Sharp, soft, or round.
Let's say you like round. Well, then
you'll simply replace sharp with round.
And there we go. We have a nice round
layout where the image and the input and
button now have a much more round
feeling to it. So, what happens here is
that these are JavaScript functions that
execute code, which you can see further
down in the index.js js file and change
the layout accordingly. Though, you
should ignore everything below this
line. Don't even try to understand it.
It's far outside the scope of this
course and you're going to learn
JavaScript later in the front-end
developer path anyway. So, it's nothing
for you to worry about right now. Now,
I'm going to reset all of these. Set
these two to regular and these two to
light and sharp as that is the default
styling that we should have. And then
I'll leave you with this image right
here as it now is your turn to build
this layout, personalize it with the
JavaScript functions so that you have
your very own completely unique website.
So go ahead and do this right now and
then I will see you when you return back
to me.
Okay, hopefully that went well. Let's
now do this together. First, we need the
image tag. So I'm going to do img. The
source is the pier.jpeg
which I'd already added. Yes, there we
can see it is rendering properly. Next
up, there is an H1.
I my name is Pear like that. Very good.
Moving on, we have the H2
fun facts about me like that. And then
there's the unordered list and the list
items. So, I'll do ul close that ul
because the list items need to be inside
of the ul and the first one is 36 years
old. I'm going to copy this one
like that. And I live in Norway and I
sing in a choir.
And there we go. Looking pretty good.
Next up, there is the paragraph with the
inline anchor tag. Here's the paragraph.
connect with me on LinkedIn like that.
Oops, it should say me on LinkedIn here.
And then we are to wrap LinkedIn here
into an href like that. And then I'm
going to paste in the URL to my LinkedIn
right here. And if you actually want to
connect with me on LinkedIn, feel free
to click on this link as soon as I've
added the target equals
blank so that it opens up in a new tab.
Okay. Finally, we we need to add the
input and the button.
Input type text like that and button
with join newsletter as the text.
Yes, that worked. And we finally just
need the placeholder in the input.
And here we're going to write enter
email. Enter email. Like that. And that
actually
looks pretty good to me. That is what we
were looking for. Yes. Okay. Next step.
We are going to personalize it. This
will be easy for me. I really love
movies about space. So I'm going to
choose space here as my favorite movie
genre. It gives me this really neat
sci-fiish font. And I am a huge avocado
fan. So, got to do that. Woo! Looking
pretty good. I tend to prefer both light
mode and dark mode, but let's try dark.
Yeah, not bad. And as for favorite edge
style, I'm not going to do completely
round, not completely sharp, but
something in between, as I think the
answer often is something in between.
And yes, there we go. Pretty cool
website. Now, the final thing I want you
to do is head back to your note where
you created your website and then click
on the bottom right icon, the cog wheel,
and choose download as zip because
that'll download your project to your
local machine. And we need that in order
to deploy this project over the next few
scrims. So, what I'd recommend you to do
is on your computer, create a dev folder
and move that zip file you just
downloaded to this dev folder. Double
click on it to open it. That'll give it
this long obscure ID. So if I were you,
I would change that to something
understandable, for example, personal
site and just save it so that you have
your project neatly placed with a good
name on your local computer so that we
are ready to share it with the world
over the next few scrims. It is going to
be epic. So I look forward to see you in
the next scrim after you've done all of
this. I'll see you there.
In order to get your personal site live
on the worldwide web, we are going to
use a service called Netlifi as it is
both free, easy to use, and powerful as
it's used by tons of professional
developers. So, if you're following this
course on scrimma.com, just click on
this slide right here and you'll be
taken to Netlify. Or if you're following
this course on YouTube, just head over
to netify.com and you'll get to the
exact same page. Once you're there, I
want you to click at the sign up button
as that'll take you to an authentication
screen where you can choose a bunch of
different options. I am going to go with
the email option. So, I'll click on that
and fill in my email and also choose a
password. Then I'll click sign up, which
again will trigger Netify to send a
verification email to my inbox. So, I'll
head over to my inbox and click on
verify email. That'll take me to the
Netlefy onboarding page where I have to
tell them a little bit about myself. So
here I'm going to say that I'm using
Netlefi for personal reasons. And
opening up this select box here, I'm
going to classify myself as a hobby
developer. And my first project will be
a personal/portfolio
site. Then I'll scroll a bit down and
give my team a name. I am just going to
call my team pair as it's common to use
your first name, but you can choose
whatever name you want on your team.
Once you're done with all of that, click
on the setup and continue. Then you are
taken to the page where you are to
deploy your first project. You can
either import an existing project, start
from a template, or deploy manually. And
that's exactly what we are going to do.
So, what I want you to do here is return
back to the folder you created in the
previous scrim called personal site. And
then mark all of the files and drag and
drop them into this rectangle. Once
you've done that, your browser might ask
you if you actually want to upload the
files to the site. Just confirm that by
clicking on upload and then Netlify
starts its magic. At this point, they
are working under the hood deploying
your site to the web. So here you might
have to wait for a couple of minutes
until you see this icon right here turn
into a screenshot of your personal site.
Now if that doesn't happen within a
couple of minutes, just try to refresh
the site itself as that'll probably do
the trick. Then you can simply click on
this URL right here and boom, you have
deployed your site so that anyone in the
world can access it. That is a
phenomenally great job and you should
truly be proud of yourself. The final
step I think you should do now is to
share your creation with the world. I
would recommend you to use either
LinkedIn or Twitter as both of those
have huge communities of developers. So
what you should do is basically what
Jolene and Jess are doing here. Share a
link to your personal site. Write a
couple of sentences about it. And also
feel free to mention anyone else who've
been involved in this project and add a
relevant hashtag like Jess is doing
here. For example, 100 days of code.
That that is a great hashtag to follow
and to use as it puts you in touch with
a bunch of other people online who are
going through the same learning journey
as you are. So I am looking forward to
see your personal site either on
LinkedIn or Twitter. And I'm also
looking forward to see you in the next
scrim.
Guess who is my favorite person in the
world right now? You are because you
have just completed the HTML section of
this course and that is a huge
milestone. So now let's have a look at
all of the awesome things you've
learned. Starting out you learned about
the core HTML tags that is the heading
H1 H2 and so on and also the paragraph
tag. And then you learned about nesting,
which is how some tags are inside of
other tags, which again are inside of
yet other tags, eventually creating a
tree structure of HTML tags. Then you
learned about buttons and input fields,
which are the elements that make your
website interactive. And then we talked
about anchor tags, a super important
concept as it allows you to create links
between different websites or web pages
on the web. And then of course we had to
learn about proper HTML document
structure as that is important with the
doc type declaration the HTML tag head
tag and also the body tag. From there,
we moved on to lists. Both unordered
lists like this one right here and
ordered lists, which is super cool and
almost as cool as what you followed up
with, which was to deploy your personal
web page to the web so that anyone
around the world can navigate to it and
check out your creation. This was a lot
of learning. I completely understand if
you're a little bit overwhelmed right
now. And it's totally okay to now take a
break, relax a little bit as in the next
section you are going to learn about CSS
aka cascading stylesheets. That is how
we design our web pages so that they go
from the dull default HTML styling into
being beautifully designed, polished
products that people enjoy looking at.
So super exciting times ahead of us. So
when you are ready, I will see you in
the CSS section.
Welcome to the second section of this
course where you are going to graduate
from HTML into the world of CSS or
cascading stylesheets as it stands for.
This is super exciting because now
you'll be able to make your sites look
beautiful as well. So the main project
of this section is actually the
google.com homepage because this time
around you are going to write the CSS as
well. And this probably looks a little
bit frightening right now, but don't
worry. I'm going to take you through it
step by step. And actually, before we
get going with Google, we're going to
get you familiar with the syntax here
and have some fun destroying
vikipedia.org.
So, it's going to be a lot of fun and we
have no time to lose. Let's just get
started.
Okay, so let's get you writing your
first lines of CSS straight out of the
gate. As you can see here, we have an
HTML file which has a body that simply
has please accept our cookies as text
directly in the body tag and that is
rendered here in the mini browser.
However, as you'll notice, the style
doesn't exactly look like default
browser styling. And that is because
we've added this line here which says
link meaning it's linking to something.
And what kind of relation does this link
has to our HTML? Well, it's real
stylesheet. It is a sheet that contains
styles. And where does this sheet live?
Well, that is dictated by the href
attribute, which you probably recognize
from anchor tags. And it's pointing to
styles.css.
And lo and behold, in our file system,
we have a styles.css file. And there we
can see a few lines of scary looking
code you've never seen before, perhaps.
However, don't worry. We're going to go
through this. So what happens here is
that this CSS code is being pulled into
the HTML file by this line three here
and then it is applied to the website
when it is rendered. So if we were to
comment out this line three here and
rerun the example, it'll look very
boring like this. So let's undo that.
Doesn't exactly look great now either,
but it's at least some styling. Although
this cookie widget is a little bit
weirdly designed in my opinion, which
leads me to your challenge. You are
going to redesign this cookie widget by
changing the values inside of this body
selector. And now you might ask, well,
what is a selector? Okay, let's go
through that because that is the basic
CSS syntax you need to know in order to
solve this challenge. So CSS works by
selecting certain elements on the page
that is HTML elements and then styling
them however you want. And the element
you select is what you start with when
writing a CSS rule. Here we are
selecting the body. That is the exact
same thing we're doing here. And what's
our body? Well, it is this tag right
here. Then in order to apply some
specific styling to the body tag, we
write an opening curly bracket and a
close curly bracket. And then inside of
those brackets, we have something called
a property, which in this case is color,
and a value, which in this case is red.
So here we're saying that the color,
which is actually text color, that's
what color means here. The text color in
the body should be red. So if I now
change the color property from blue to
red, you can see in the main browser we
have changed the text inside of our body
to red because if we head over to the
HTML, this text is indeed inside of our
body.
Now as you might have noticed as well,
the property is followed by colon and
the value is followed by semicolon. And
you have to have that notation,
otherwise you'll break the example.
So, what I want you to do now is
redesign this cookie widget. Make sure
you change all of the listed CSS
properties. That's background color,
font size, font weight, text align, and
margin top. And what you'll change it to
is up to you, but I've given you the
hints of what kind of values the
properties expect. Here in the comments
on the right hand side you can see
comments in CSS is done with this star
and slash character. So the gradeout
text as in with the HTML is not
interpreted by the browser. It's just
help text for you in order to solve this
challenge. And I'm intentionally not
giving you any more information about
what each of these properties mean
because I think they're pretty
self-explanatory. So, I want you to
experiment with changing them and draw
your own conclusions as to what, for
example, the font size does to the
styling on our page. And then once
you've done this, once you've redesigned
the cookie widget, move on to the next
scrim. And then I will show you how I
would do it as well. Good luck.
So, really good job writing your first
lines of CSS. That is no small
accomplishment. Now, I'm going to show
you how I would design my cookie widget.
So, I want my widget to have cookie
colors. So, for my background property,
which is what sets the background color
of the body, which fills up the entire
browser at this point, I'm going to
choose a color called saddle brown. Like
that. And you might wonder, well, how
did I know that saddle brown is a color
that CSS supports? Well, you might have
noticed as you wrote CSS values that VS
Code, our editor, starts suggesting
values for you as you start typing. So
that is how I found Saddle Brown. This
suggestion box here isn't recorded by
Scribba itself. So you won't see it in
recording, but you will see it as you
interact with the code yourself. So we
have the background. Now let's move on
to the next property, which is color.
And that dictates the color of the text.
It is set to red. Though let's choose
white here. That looks pretty good. The
next one, font size dictates well the
size of the font. And here you can see
we are using px values which is short
for pixels. And you can choose whatever
value you want. Let's see what happens
if we increase it up to 200. Oh wow,
that is far too big. So instead, let's
try something like 25. Yeah, that's
pretty good. Next, there's the font
weight. You can choose between lighter,
normal, or bold. And if we change this
to bold, you'll see there that our text
suddenly got a bit thicker, which is
exactly what this property does. It
changes the thickness of the font. Okay,
text align. It is set to right, but we
can also set it to left or center. Let's
see what happens if we do left. And that
bumps the text over to the left hand
side. I think we should have our cookie
widget with centered text like that.
Finally, margin top. What does that do?
Well, if we try to change this to, for
example, 50, you can see that. Oh, wow.
That created more space here at the top.
So it adds margin aka spacing at the
top. And here I think maybe 50 was a bit
too much. Let's do 30. Yeah, that is
just right. Now you probably ended at a
completely different styling than this.
That is okay. The point here was to
experiment. Before you leave, I'll give
you a sneak peek at another tool you can
experiment with, and that is a different
way of describing colors in CSS called
hex codes. So here I'll paste in this
weird looking code starting with a
hashtag. And as you can see it created
the exact same color. So this here is
actually a recipe for the saddle brown
color. And it's actually a description
of three different things. How much red,
green, and blue this saddle brown color
consists of. So if I change this eight
here to one instead, you can see that
completely changes the color. Now if
that is confusing, don't worry. You'll
learn more about that later. I just
wanted to give you a sneak peek on it.
As with all of these other properties,
you are going to get more comfortable
with all of this. So, it's not a problem
at all if you completely forget about
all of these properties and values over
time as you do it again and again. It'll
become a part of your muscle memory. So,
with that, let's carry on.
Now that you know enough CSS to be
dangerous, let's use your newly acquired
superpower in order to destroy
Wikipedia. And for that, we need a
Wikipedia page. And about the coolest
one there is is this list of helicopter
prison escapes. I mean, oh my god, how
insane is that? So, click this image and
you'll be taken to this article and then
I'll show you step by step how we can
destroy it with CSS. So the first thing
you need to do is to right click as
that'll open this menu where you can
choose inspect and once you click that
the dev tools will open. Then choose the
icon in the far left as that allows you
to use your cursor to select elements on
the page. So that when you hover over an
element like this one, you'll see that
the dev tools selects it for you. So if
you move your cursor over to the logo,
you'll see that one selected. And if you
move your cursor over to the main part
of the screen here, you can see it
selects the entire article. Once you're
on that selection, I want you to click
because that'll select the specific HTML
element here in the elements tab. And
don't worry about all of this. You do
not need to recognize the tags and
attributes and everything you see here.
What I instead want you to do is head
over to the right hand side where you
see element.style. Click there and
you'll be able to actually apply CSS
directly to this page. So you can, for
example, write background red and boom,
there we have really ruined this
article. Then if you select another
element on the page from this body
content element here, you can do the
exact same thing. Click on the element
style and now for example write font
size and 30 pixels. And there you go.
Now you can click on the X icon in the
far right corner here and then you can
read about these helicopter prison
escapes with a really bad background and
huge text if that's what you prefer. So
feel free to spend an hour in this
article and then I will see you in the
next scrim.
Okay, so now it's time for you to yet
again build the google.com homepage, but
this time around you're also going to
write the CSS so that you'll understand
everything that goes into building this
elegantly designed search engine. As you
can see, I've set up the basic skeleton
here in the HTML file for you. In the
body tag, we have an image which is
pointing to the Google.png PNG image
which lies in the same directory as our
index html folder up here. And we have
an input type text which is our search
field. And we're only going to start
with these two elements and then later
move on to the buttons. Now, as you can
see in the mini browser here, this looks
horrible. It has no styling. So the logo
is far too large. The input field
doesn't look very good. So in short,
there's a lot of work to do here. But
before we can start writing CSS, you
need to link to the CSS file. You can
see I have a style CSS here which just
sets the background of the body to red
as a way of testing whether it is
actually a link to the HTML. Currently,
it's not. You are going to have to link
to the CSS file on your own. And if you
remember a couple of scrims back, I
showed you how to do that. Though,
please don't look back at that scrim in
order to solve this challenge. Instead,
Google it because I do that almost every
single time I have to link from an HTML
file to a CSS file. So, you should not
be ashamed for googling such a quote
unquote simple thing. Instead, just do
that right now because it's important
practice and then when you come back, I
will show you how I would do it as well.
Okay, hopefully that went well. Now,
let's have a look at how we can solve
this. So, I would simply type how to
link to CSS into Google and click at the
top result. This happens to be a W3
schools article. Here you can see that
there's a few different ways you can
import CSS into the HTML file, but we're
only going to worry about the first one
here called external CSS. As you can
see, if we scroll a bit down, what we do
is we type link re equals stylesheet and
href with a pointer to the CSS file
itself. So we'll do link file equals
stylesheet
href equals styles.css
and then closing it. And if we now
refresh the browser, boom, there we go.
It is red looking horrible. So we can
remove that so that it looks white
again. But now we know that we have
linked the CSS and the HTML together. So
we are ready to move on and start
styling our page.
The first thing we want to fix in our
example here are the widths of the
elements. Because if we compare it to
the finished design, you can see that
the input field is meant to be wider
than the logo. However, in our example,
that's not the case. Quite the opposite
actually. The logo is much wider than
the input field. So, we need to shrink
the logo and then make the input field
wider. So, I'm going to start and do one
of these and then you will do the other.
I happen to know that the width we want
to have on this input field is 400
pixels. What I'm then going to do is
target the input field using the element
name and create a CSS selector just
writing input then open and close curly
bracket. Then simply write with and 400
px and boom there you can see suddenly
the input field widened quite a lot. And
as you might have noticed, we're using
this pixels value here again, which
we've used a couple times already,
actually. And if you're a little bit
confused as to what a pixel is, you can
think of it as the smallest unit of
space that our screens can render. So a
pixel is just a tiny square. So tiny
that we actually need 400 of them
stacked up besides each other in order
to get a width of this size. So with
that, I want you to fix the width of the
Google logo. Select the image tag and
set the width of the logo to 300 pixels.
Go ahead and do that right now and then
I will show you how to do it as well
when you return back to this scrim.
Okay, let's do this. I'm going to break
this down into a few lines so I get some
space above the input field and then
target the img element, the image
element. open up the curly brackets and
do with 300 pixels. Like that. Oo, we're
not quite yet at the finished design,
but the ratio between the logo and the
input field is now a lot better. Great
job. Let's move on.
Okay, I've discovered a problem with our
Google site, which is that if we make
the browser really wide, you can see
that suddenly the items stack up besides
each other on the same line. And that's
obviously not the kind of behavior we
want. We want them to always be stacked
on top of each other. And in order to
fix this, you have to learn about inline
elements versus block elements. And
that's best understood through the lens
of an example. So let's say that you,
for example, have a collection of things
you want to showcase. You could be like
George Francen, for example, and collect
dinosaur poo. He actually has the
world's largest collection of fossilized
feces, and naturally, he wants to
exhibit them in a museum. And if he were
to use display inline on his images,
they would stack up besides each other
like this. So kind of like in a line
horizontally. However, had he used
display block on his images, then they
would stack up on top of each other. So
rule of thumb is display block stacks on
top. Another way to look at this is that
the block level element blocks out the
rest of the available horizontal space
so that nothing else can be placed
there. And that is regardless of how
wide this image is, the content itself,
display block requires control over all
the horizontal space in its given
container. So when this block image is
placed here and you add a new image, it
would have to be placed underneath the
previous one. And if it is display block
as well, it'll block out the rest of the
available horizontal space and the next
image have to be placed below that one
again and so on. So the solution is
obviously to turn these elements into
blocks and I'm just going to give you
the syntax here. This is how you write
it. Display colon block. Go ahead and do
that for both of these and observe what
happens to the example when you make the
browser wide. Good luck.
Okay, let's do this. So I'll do display
block up here and the same
here. If we look at the browser,
seemingly nothing has happened. However,
if we now make it wide, you can see yes,
no matter how wide the browser is, these
will always stack up on top of each
other. So, great job. Let's move on.
The next thing we need to fix is the
lack of white space above our Google
logo. Because if we take a look at the
design here, we want quite a lot of
space in between the top of the browser
and the logo. And I'm not going to tell
you how to add that, but if you can
think back to the very first scrim in
this section, we added some space above
the text in our cookie widget. And that
specific space is called margin or
margin top to be specific. So your job
is to add some margin on top of our
image right here. You can start on line
seven so that our example here gets some
breathing room, some white space above
the logo and the input field. go ahead
and give this one a shot and then I will
of course show you the solution when you
return back to me.
Okay, hopefully that went well. If not,
no worries. I really pushed you out on
thin ice here. So, it's completely okay
if you didn't remember that the property
is called margin top. And what you add
there is a pixel value. So, for example,
100 px. And there we go. We've now added
a margin on the top of our image. And
while we're still quite far off from our
provided design here, we are getting
closer. Great job. Let's move on.
Now, while it totally works to add the
margin top directly on the image here,
you'd most likely not see professional
developers do this because what they do
instead is wrap these two elements in
some kind of container and then control
the positioning of these elements
indirectly via that container. And in
order to do this, you have to learn
about the div. I'm going to create an
opening div and then indent these two
and then a closing div below here. Now,
if we run this code, seemingly nothing
has happened because the div actually
does very little in itself. Though it is
indispensable for web developers. Just
to show you how important it is, let's
take a look at Facebook and my timeline.
Here I've opened up dev tools and
inspected one of the timeline items. And
you can see it is actually a div inside
of a div inside of another div inside of
yet another div inside of a div inside
of a div.
Divs really do rule the world. So it's
important that you learn about them. And
the way we'll use this div now is by
removing the margin top styling from the
image and then add it to the div itself.
And the reason this is important is
let's say that for example Google
decides to create a video here instead
or a interactive doodle or some kind of
game. They often play around with the
Google logo. Well, if so, it's a really
bad idea that it is the image itself
that controls the margin from the top of
the browser and down to the content
because that image might be removed. So,
it is a solution that will get you into
trouble in the future. So what we'll do
here is we'll simply instead target the
div and then remove the margin top from
the image and boom it has popped up. Now
we lost all of our white space but if we
add it back to our div we are back at
the design we started out with seemingly
nothing has changed but as you know we
have a better HTML structure. So we have
definitely made good progress here. So
with that, let's move on to the next
scrim.
The reason you need to learn about
classes now is because our current setup
where we simply have the elements like
this and then selecting the element
using the element selector in CSS will
give us bugs down the line because when
we add the two buttons beneath the input
field, we'll actually wrap these two in
a div as well. And let's see what
happens at this point. What I'll do now
is simply add the HTML for these buttons
down here beneath the input field. So it
is a div with two buttons inside of it.
And then you can see we are closing that
div, the inner div, and then we are
closing the outer div. Let's run this
code. And oh wow, what you can see is
that the spacing between the input field
and the buttons is totally off. And that
is because our CSS element selector here
selects any div it can find in the HTML
document and applies the margin top to
it. So it doesn't just select the first
one, but it can select thousands of
divs. So this is a very generic solution
and likely not something we want to use
as we want to have multiple divs with
different types of styling in our HTML
element. So let's now instead fix this
by doing the following. I'll write class
equals then for example main this here
is an attribute called class so it's
built into HTML and this here is the
name I'm giving my class I want to call
this div my main element and now the way
I select that in the CSS is by instead
of just writing the element name simply
doing dot main like that now if I rerun
this code you can see now the margin top
is only applied to the outer div, our
main class, and not to the div which
wraps the two buttons. So that is
exactly what we needed. Now I'm going to
remove these two buttons here because we
don't want to deal with them just yet.
And then I'm going to give you a
challenge.
First, you're going to add a class to
each of these two elements. That is the
image and the input. You have to decide
what you will call the two classes, but
make sure to give them different names.
And then you're going to in the CSS
replace the element selectors with class
selectors. So that is the img selector
here and the input selector here. And
obviously the CSS class selectors you're
going to replace them with is the ones
you have added to the elements right
here. So go ahead and do this right now.
Okay. Hopefully that went well. Let's
now do it together. So for the image
tag, I'm simply going to do class
equals. Then I'll call this logo dash
img. So here I'm using a convention
where I'm first giving my class a
descriptive name and then using a dash
and then specifying the element as well.
This is an img element, but it could
have been for example an SVG or
something else. Now I don't expect you
to have followed the same convention.
There are plenty of CSS conventions and
it doesn't matter at all what you used
in your example. You can call your
classes whatever you want. Okay, for the
input we'll do class equals search dash
input like that. Okay, let's move
through the CSS. Then I'll replace this
with dot. Now you can see it broke the
layout because this styling now is not
applied to any element. So we need to do
logo-ash img like that. And here do dot
search dash input. And there we have the
exact same example. Didn't change at
all, but we now have a more flexible
solution that scales better. So great
job solving this challenge. Let's move
on.
Okay, now it's time to take a closer
look at margins. And you've already
dipped your toes into this as you have
used margin top. So, you might have some
intuition about what margins do.
However, now you're going to learn it
properly through creating flags with CSS
because flags are just awesome. I mean,
take a look at this flag here, for
example, from the Republic of Venice, a
state which existed for over a thousand
years in Europe. And it contains, of
course, a flying lion holding a book. I
mean, what's more natural than that? Or
this one right here for the aisle of
man. I mean, have you ever seen a more
creepy creature than this three-legged
thing right here? Imagine having that
thing running after you?
That would not be fun. And this flag
would be really hard to create in CSS,
actually. So, we're not going to create
this one. We're going to start off at a
much easier level with just some very
simple shapes. So, let's have a look at
those. Actually, here is what we're
going to work with. As you can see,
there are simply three divs inside of
the body tag. And each div has a class
red, blue or green. If we check out the
styles.css, you can see that these three
classes apply a background color to each
of the elements and sets the height to
100 pixels. And here we have the result.
Three rectangular boxes that are stacked
on top of each other. And that tells us
that these are display block because
display block stacks on top. There's one
thing that bothers me here, and that is
that we have a little bit of white space
around the three items. Why is that?
Well, actually, it's because our browser
is trying to help us here. It's assuming
that we want a little bit of space
around the body tag so that these three
don't touch the edges of the browser. So
what's actually going on here in code is
that the browser
targets the body and sets a margin of 8
pixels. If you notice when I added this,
nothing changed. So if I remove this,
it's still the same. Meaning that this
style here is applied automatically. So
that means that margin is actually the
space around an element because now if
we change this to zero, boom, there you
can see suddenly our divs touch the
edges of the browser. Now here at the
bottom, they stop after 300 pixels since
we've told them to. So this margin zero
here seemingly didn't change anything at
the bottom because the bottom contains
white space anyway if we make our
browser tall enough. But anyway, that's
a digression. Let's get it back so that
the browser is completely filled up with
these three items. And now let's play a
little bit around with margins. So what
do you think will happen if I add a
margin top of 10 pixels on the red hair?
Take a guess.
Okay. Margin top 10 pixels. Boom. It
added 10 pixels on the top. What do you
think will happen if I add margin left
10 pixels as well? Let's try it.
Okay, so it added 10 pixels on the left
hand side, but it's only on the red
rectangle, not the blue or green
rectangle. Likewise, if I do margin
width 10 pixels, it adds 10 pixels to
the right hand side as well. If I do
100, it eats up almost a third of our
red rectangle. Okay, so now it's your
turn.
I want you to add 100 pixels of margin
on the bottom of the red box. So, go
ahead and give that a shot right now.
Okay, if you did margin bottom 100 px
like that, I want to congratulate you
because you just created the flag of
Monaco. Really good job creating your
first CSS flag. And now, let's do that
yet again. Here is your challenge. You
are going to create the flag of the
Gambia. And here we have that flag.
However, there's a catch. You can only
do this by applying styles to the blue
box. So, by adding margin to this CSS
rule right here. So, go ahead and give
that a shot right now.
Okay. Hopefully that went well. The way
to do it is by doing margin top. Let's
try 10 pixels and see. Actually, I think
we want our white space to be a little
bit more than 10 pixels. So, let's do 20
like that. And then margin bottom as
well. 20 pixels. And there. I think we
have a pretty good flag for the Gambia.
Now, before we move on to our Google
project, there's one little weird thing
I want to tell you about margin top and
bottom. Because what do you think
happens if I add for example margin
bottom of 15 to this red element here?
Well, according to my logic, the margin
right here would be 35 pixels because we
added 20 pixels on top of the blue. And
if we add 15 at the bottom of the red
one, that should be 35 pixels in total.
However, now that I added this, nothing
happened. And that is because margin top
and margin bottom will in many
situations collapse into each other. So
that as long as this margin is smaller
than this, this will take precedence.
And it's only when this one becomes
larger, for example, 25, that it will
kick in. So let's take it back to 15.
And to make things even worse, there's
actually scenarios where this doesn't
happen. However, digging into that is
outside of the scope of this lecture. I
just want you to be aware of it because
if you're suddenly in a situation where
your margins don't seem to add up, it is
probably because of so-called collapsing
margins. Okay, with that, let's move on.
Okay, we're back at our Google project.
I want to give you a quick challenge. I
want you to add some space between the
logo and the search field because as you
can see here, there's some spacing in
between the elements in our design.
However, in our project now, they are
crammed together. There are two ways you
can do this using margins and I want you
to think about which two ways that is
and choose one of them. Go ahead and do
this right now.
Okay, hopefully that went well. Let's do
it. So, you can either add a margin
bottom to the logo or you can add a
margin top to the search field. I'm
going to go with adding margin bottom to
the logo as I think either way is as
good as the other at this stage. Margin
bottom. And how much do we want to add?
Well, let's try 20 px. And yeah, as you
can see, we've got some spacing. The
bottom of the G is not touching the top
of the input field. And if we compare
that to the example, I think it looks
more or less the same. So, great job.
Let's move on.
Okay. Now you are going to learn about
centering elements in CSS and that is a
scary topic for a lot of developers. So
scary that this stack overflow question
about how to horizontally center an
element has been viewed 4.5 million
times. So if you think this is a little
bit confusing, you are in very good
company. So don't worry about it. So
with that in mind, let's move over to
our example. You are going to center
this dinosaur poo. As you can see in the
CSS, it has just one property set at
this point which is a width of 200
pixels. That is because the image is so
wide originally that if I don't set that
width, it'll just take up more space
than we have available. And setting the
width of it is actually an important
thing in order to center it because
there are three things that need to be
in place when we are to center elements
with the technique you are about to
learn aka centering with margins. And
these three components are first the
element needs to be display blocked
because how is an element supposed to
center itself if it doesn't command full
control over all the horizontal space.
If it's display inline and allows other
elements to stack themselves besides the
element, well then it won't be able to
center itself because that space now is
busy with other things. So in order to
center this poo, it needs to control all
the horizontal space.
So it needs this
block like that. Now that didn't change
anything, but under the hood, the poo
now controls the full horizontal space.
Okay, the second part is that it must
have a width
and that is what we've done here already
because how is this item supposed to be
centered inside the browser if it is
wider than the browser itself? That's
often what happens with either large
elements or with some HTML elements who
just naturally actually expand to take
up all the available space in the block.
So we have to make sure that our element
has a width. And finally we set the
margin on the left hand side and the
right hand side to the value auto. Let's
just do that. Margin left auto. Oh wow.
Now it pushed the pool all the way to
the right hand side because auto here
means take up all the available space.
And since this is a block element, the
available space is all the space that's
left after the pooh has taken up its 200
pixels. So it just ate up all of the
space and turned it into margin.
However, at this point, margin right
enters the party. Margin right and says
I want the available space as well. And
at that point, the margin left and
margin right goes into discussion and
decides, well, actually, since there are
two of us here pushing on each of the
side of the element, trying to get all
of the available white space, let's just
divide it evenly amongst both of us. And
the result, well, it is that the item is
centered. So, that was a long
explanation of centering with margins.
If you feel confused, don't worry about
it right now. All you need to do is
remember these three conditions and just
be aware of the 4.5 million other
developers who are in or have been in
the same boat as you. Let's move on to
the next scrim.
Okay, we are back at our Google project.
Now your challenge is to center the logo
and the search field using the technique
you just learned. So go ahead and give
that a shot right now and then I will of
course show you the solution afterwards
as well.
Okay, as you hopefully remembered, the
way to center an element which is
display block and has a width is by
adding margin left auto and margin right
auto. So let's do that. Margin
left auto. And as you can see, that
pushes the logo all the way to the right
hand side because the margin left is set
to auto and it takes up as much space as
it can. We do the same thing with the
right hand side. Do margin right auto.
Then the right hand side starts pushing
back again. And when they're both taking
up equally as much space, the margin
left and margin right kind of decides,
yeah, let's not argue anymore. Now we
have a nice balance. And woo, lo and
behold, our element is centered. So now
what we can do is be super lazy and copy
that code and paste it in down here. And
boom, our input field is centered as
well. And our project is starting to
look a little bit like our finished
design. So this is super cool. Let's
just keep up the pace and move on.
we need to take a little step back and
learn a new CSS concept which is the
close relative of margins called
padding. And we're of course going to
learn that through the lens of an
example. So here I have just a simple
div which contains a little bit of text
or specifically the two first sentences
in the white paper behind Bitcoin. And
let's say we want to turn this into a
nice looking card that people can read.
If so, we'd probably create a class call
it card. Then of course in the style
CSS, we'd select that card and apply the
styling. As for now, it just has one
property set, which is the color that
we've set to a shade of black that isn't
pure black, but a little bit softer, so
to speak. That is because by default,
the browser uses pure black as the text
color and pure white as the background
color. And as a rule of thumb, you never
want to have pure black on pure white.
That's not a pleasant experience for the
eyes. So instead, we went for this. But
that was a digression. Let's move on
with turning this into a nicel looking
card. So a card needs a background
color. So let's just do background light
gray for example, like that. And we also
don't want our card to be full width
because as you remember divs are block
by default and take up the full width if
you don't specify any width on it. So
we're going to do width for example 300
pixels like that. And then we also want
some space above the card. That is of
course margin top. Let's push it 50
pixels down from the top. And finally it
should be centered. So, let's do margin
left, auto, and margin right like that.
Okay. So, now we have something that
looks at least a little bit like a card.
But there's one problem here. You can
see the text is crammed all the way into
the edges of the div, which doesn't look
particularly good. And that is where
padding comes into play. So now I'm just
going to apply the padding by doing
padding and for example 20 pixels like
that. And as you can see, that created a
little bit of space. Basically, some
breathing room in between the text and
the edge of our div container here. And
if I do 50, you can see it's even more
breathing room. So, where's margin adds
space on the outside of our div, the
card which we've selected here, padding
will add space on the inside in between
the content and the edges of the
container or the div. So margin kind of
lives up here and down here if we are to
visualize this in our HTML whereas
padding lives on the inside of our
element that is here and here.
And now you might wonder well why did I
just write padding whereas for margin
we're specifying each of the sides. And
well you can do exactly that on padding
as well. this padding 20 pixels right
here is the exact same thing as writing
the following padding top 20, bottom 20,
left 20, and right 20. So with these
four lines, we can actually delete this
one up here. And then we have the same
convention as we're using with margins.
It's just that you can use padding 20
pixels like that if all of the values
are the same. It's basically a shortand.
And now that we have all of them written
out, we can play around a little bit
with it. perhaps add a 100 pixels on the
left hand side looks really weird and
100 pixels on the top as well. And
suddenly our card does not look very
good. But let's get it back. And now I
want to give you a challenge. I want you
to add padding to make our card look
like the provided design. And the
provided design is right here. You can
see it's a completely broken card as
we've been ruthless about our padding.
And I've actually removed all of the
padding rules from the CSS as I want you
to build some muscle memory writing
padding. So go ahead and make this card
look like it does in the slide. And then
return back to me once you've done it.
Okay, hopefully that went well. If not,
no worries. Let's do it together. As you
can see here, it's completely crammed
into the bottom left corner, which means
that it doesn't have any padding on the
left hand side or any padding on the
bottom, but a ton of padding on the top
and a ton of padding on the right. So,
I'm just going to do padding top 100
pixels, something like that. Yeah, seems
pretty much what we have in the design
as well. And then do the same padding
right 100 pixels like that. And to me
that looks actually pretty much like our
design. I don't need to write padding
bottom or padding left now. Though I
could do it. I could do padding bottom
zero pixels or just zero and padding
left zero as well. Though that didn't
change anything. So it's actually not
needed because by default the padding of
a div is zero. So however you solved
this, great job. Let's move on.
So now you've learned about controlling
the space on the outside of the element
with the margin and on the inside of the
element with the padding. But wouldn't
it be nice if we could also control the
border in between the margin and the
padding? And it turns out we can through
you guessed it the border property cuz
if I write border here and then specify
for example two pixels thickness solid
in shape and then color red you can see
then we get a border which is on the
outside of the padding but on the inside
of the margin. And if we change any of
these three values here it'll update
accordingly. So let's try 8 pixels width
instead. You can see it's much thicker
now. Instead of solid, we can do a like
that. Pretty weird. And instead of red,
we can do, for example, fff white in
hexodimal or just write white like that
or just blue so it's a little bit
clearer. Now, there are many types of
borders, but in 99.9% of the cases, you
want to use solid like that. And also,
very often, you'd want to soften up
these corners.
And you can do that with the border
radius property. And here you specify a
pixel value, for example, 30 pixels. And
boom, there you can see we have this
much softer corners. If we crank this up
to 100 or a,000, it stops at some point
because then the edges have simply
turned into half circles. So at some
point, there's no use going any higher.
Now, this looks pretty bad. So, what I
want you to do now is fix it up so that
it looks like this. And I'm not going to
give you any of the values I've used
here. Just a little hint. I took an
already existing color in our box and
used it for the border. That's a common
web design trick to reuse colors and not
have too many different colors. So, if
you think this is pure black, mm- you
are wrong. So, go ahead and change the
values of the border and border radius
so that our example here matches the
design. I've provided in the slide. Good
luck.
Okay. Hopefully that went well. So,
let's start with the border. 8 pixels
here. Looks a bit too thick for me. So,
let's try four instead. Like that. I
think that matches better. Yes.
And the color I gave you a hint was just
the blackish color we have for our text.
Like that. And the border radius is much
smaller. Let's try 10.
It's This still looks a little bit
rounder. So, let's try 20. Yes, there. I
think we got it. Great job. Now, a cool
thing to do in order to inspect the
margin, border, padding, and size of the
element in general is to open up the dev
tools, which can be done by
right-clicking on the navbar here and
then clicking inspect and then this dev
tools panel will open here, clicking on
the select icon on the left hand side
and then hovering over the element.
Finally clicking at it. There you will
see that we have selected the element in
the elements tab and under the styles
tab you can see the styling we have
applied using CSS. So here you can
actually modify them if you want but
we're not going to do that right now.
We're going to scroll down and verify
that our margin border and padding are
correct. So if I hover over the margin
here you can see up here in the mini
browser it is marked in orange and the
values are provided down in the box
here. If I move over to the border, we
can see that border is now selected in
the browser and it is indeed four pixels
as we coded up a minute ago. If I move
over to the padding, you can see that is
also indeed 20 on each side, just as we
want it to be. And finally, if I hover
over the center here, the content is
highlighted. And the content of this div
is just the text. And as you can see, it
is 300 pixels wide, just as we set it
as. So, it's working as expected. And
the height is dictated by how many lines
of text are two sentences required. So
using this technique of inspecting an
element and its sizing properties like
this is a really good way of debugging
if something for example looks wrong
because it gives you an ability to
verify if the numbers here match up with
what you've tried to write in your CSS.
So now that you've learned all of that,
I want to tell you the name of this and
it's actually called the box model. And
to be honest, I've never really liked
this word. It sounds more complex than
it is in my opinion. It's just spacing
around the element, a border, spacing on
the inside of an element and the content
itself, but in web development, we call
this the box model. So, you'll probably
hear this name come up from time to
time, but you shouldn't fear it. You
know exactly what it is, and you've used
all of the properties in the box model.
So, don't worry about it. Actually,
let's give you a new box model practice
in the next scrim. I'll see you there.
Okay. And now you are going to create a
really neat looking button, namely the
following button on Twitter. Let's zoom
in a little bit. And here you can see
it. This is the button you see if you
already are following someone. And we
zoom out again. This is the Scribba
Twitter profile. You should definitely
just click this image and follow us
because we share a ton of useful tips
and tricks and career advice for
aspiring coders here. So, it's
definitely worthwhile. Anyway, now you
are going to use the things you've
learned about padding, border, border
radius, and so forth in order to make
this button right here with the ugly
default styling look exactly like this.
Or not exactly, as we're not going to
worry about the font. It is a little bit
different, but other than that, you
should try to make it look identical.
I've given you some hints here. For
example, the border color is this hex
color right here. And the text color is
this one right here. And also, you need
to remember to adjust the font weight.
And I don't think you've done that ever
since the very first scrim where you
wrote your first lines of CSS. So, you
might have to look back so that you
figure out how to adjust the font
weight. Or you can Google it. Up to you.
Anyway, just go ahead and give this your
best shot. And if you at some point get
stuck or don't know how to do it, no
worries. We are going to do it together
afterwards as well. Good luck.
Okay, hopefully that went well. Let's do
it together. I'm going to target the
button just using an element selector
like this. And now let's pick the
lowhanging fruit, namely the ones we've
gotten here already. For example, by
doing color and then copying this color
right here. Okay, that didn't change
much, at least not visibly. Then let's
do border. And I'm going to start with
one pixel solid. And then the border
color right here. Like that. Okay. So it
looks a little bit different now, but
not quite this. One thing we can clearly
see is that this image has a white
background whereas ours is still a
little bit gray. So let's do that as
well. Background white. Okay. Yes. But
our button is much more crammed. There's
too little padding. Whereas here you can
see we have a little bit of padding on
the top and the bottom and actually
quite a lot of padding on the left and
right hand side. And that's actually a
little design tip right there. What
you'll often see is that buttons have
more padding on the left hand side and
the right hand side and they have above
and below. So let's try padding top five
pixels for example. Padding bottom five
pixels. And then we'll try left 10
pixels. and right 10 pixels as well. How
does that look? Not too bad. But
actually, it looks like this button has
even more. So, we've added half as much
on the top and bottom as we have on the
sides. So, let's crank up both of these
a little bit. I'm going to select both
of these and do eight top and below. And
select both of these and do 16 left and
right. Yeah, I think that's more like
it. Now, one thing that definitely is
remaining is the rounded edges here. So
that is the border radius and add it up
here with the border. Let's try 10
pixels. It's a little bit round now, but
not as round as the Twitter button. So
let's do 16. Yeah, more like it. Can we
go higher? Yeah, I think we can go to
18. That looks quite good to me if we
compare to this one. And then finally,
we needed to remember the font weight,
which I can do font weight
bold like that. That looks a lot better.
I think the final adjustment I want to
make is adding even a little bit more
padding around it because now that I
look at it, it feels like this is a bit
more spacious. So, here we are really in
the nitpicking area. But let's try 10 on
both of the top and bottom. And let's
try 20 on the left and right. And what's
interesting now is that since the box
now has grown in size because it's
affected by the padding, the border
radius can also be cranked up a little
bit. I think actually we can do 20 now.
Yes, because the bigger a box is, the
more pixels you need to add in order to
get this fully soft looking button which
essentially has two half circles on each
of its sides. So looking at the example
and comparing it with ours, I think we
have reached our goal. And if your
values are different than mine, that
does not matter at all. You're actually
creating the button of a huge public
company here. You are just learning
basic CSS. So, it's totally not expected
that you create buttons as neatly as
Twitter does, as they probably have a
team of developers and designers working
just on this profile page. So, however
it went, be proud of yourself and get
ready to move on with our Google
project. I'll see you in the next
scrimm.
Okay, we are back at our Google project
and now you are going to fix the design
of our search field and you're actually
going to get a design spec and a proper
walkthrough of how I want it to look. So
here I've opened up the dev tools
actually on the right hand side as I
want to inspect the box model of this
element with you. And if you wonder how
I managed to stick the dev tools to the
right hand side here, just click these
three icons if you're in Chrome. Then
you can choose the doc side. So let's
move on down here to the box model of
the input field. And if I hover over the
content in the box model here, you can
see that it says 400 pixels * 24 pixels.
And you can see the content is marked in
blue in the mini browser here. So that
means that our width is 400 pixels. We
know that because we set it ourselves
here. And the height of the content
should be 24 pixels. Okay, let's move on
to the padding. If I hover over that,
you can see it is green and it's
highlighted in green in the mini
browser. The padding at the top and the
bottom are 10 pixels as you can see down
here. And the left hand side and right
hand side have 30 pixels each. And then
if we move over to the border, you can
see that it's simply one pixel all
around. So here is your design spec
given to you by your hipster designer
down in the bottom right corner here.
Height of content 24 pixels. But do
remember that that is actually the
height of the inner box here not the
height of the total input field as that
also contains the padding and the
border. But the height property should
be 24 pixels. The padding above and
beyond as said 10 pixels right and left
hand side 30 pixels. Thickness of border
1 pixel. The color of border is this.
And then finally also the border radius
is also a value that our designer has
been kind enough to give to you. So take
this design spec and implement it using
CSS.
Okay. Hopefully that went well. If not,
no worries. We'll do this together right
now. So starting with the height of 24
there. I'm going to just write height 24
pixels like that. Okay. So it became a
little bit taller. Now next step is the
padding top and bottom was 10 pixels. So
padding top 10 px
bottom was also mpx like that. Okay. The
padding left and right hand side were 30
pixels according to the spec. So let's
implement that like that. And then we'll
move on to the border. One pixel and
this hex code right here. So I'll do
border and here we have to provide it
with three values. So one pixel it is
going to be solid and the color is d15
like that. Looking pretty good. The
final thing then is the border radius.
And that should be 24 pixels according
to the design. And look at this. It is
starting to look really professional. So
really good job if you did this
challenge. And if you did a couple of
mistakes, no worries at all. Actually, I
also did a little mistake here according
to best practices when setting heights
for input fields. Because actually, I
want to show you that this height of 24
pixels here is actually not the way we
want to do it. Because if I Google set
height of input field CSS and then click
the first link, that is a Stack Overflow
question. And here you can see someone
who's been having problems with this.
So, they've asked a question about it.
If we scroll a little bit down, you can
see that people are saying try with
padding and line height. Hm. Line
height. That's not something we've used.
If we scroll a little bit down, you can
see that some browsers will honor a CSS
height rule, some not. So, it seems to
be a little bit difficult to use height
for the input field. And this person as
well suggests line height. And likewise
with this third person here. So, I'm
going to trust the wisdom of the crowds
here and use line height instead.
like that. It didn't change anything,
but we are using a property that more
browsers apparently will honor. And line
height is actually just the height of
one line of text. And since that is
everything we have inside of an input
field, it's just one line of text.
That'll be the exact same thing as
setting the height of the inner content
of the input field. So, if that was a
little bit confusing and you prefer to
just use height, that's totally fine as
well. It's more important that you are
using what you are comfortable with than
that you are doing it the 100% correct
way according to conventions. So just
use whatever you want. I just wanted you
to be aware of the fact that this is the
recommended way according to Stack
Overflow. With that, let's move on.
Okay, now we're going to add the first
button to our Google project. So, I'm
going to head over to the HTML and here
beneath the input field, I'm just going
to type button like that. And this will
be the Google search button. So, there
we have it. Doesn't exactly look good,
but don't worry about that. Now, I want
to give this a class as well. And a
class name you'll often see used for
buttons is simply btn. Like that. Okay.
Now that we have that, I want you to
center the button and add some space
above it. So go ahead and use the class
selector to select this button and solve
this challenge. Good luck.
Okay. So what I'll do is do btn to
select the button here. You might have
been tempted to simply do margin left
auto and then margin right auto.
However, that doesn't seem to do
anything. Why is that? Well, if you
remember the criteria you needed in
order to use this technique to center
elements, one of them was that you need
your element to be display block.
Whereas buttons are actually display
inline by default. So if we do display
block, boom, there we go. The button is
centered. And now you might ask, well,
what about the width? We also set the
width on the input field and the logo.
Well, we could do that as well with this
one. Let's do width 200 pixels for
example. However, it's not needed
because if we remove the width, you can
see that it still has a width which is
certainly not full width across the
entire page. So, it kind of has an
implicit width without us setting the
width. So, even though it is a criteria
that your element needs a width in order
to use this technique here in order to
center it, you don't necessarily have to
set the width yourself if the element
already has some kind of width. So it
works perfectly fine just doing display
block and margin left right auto in this
example.
Finally we are going to add some space
above it. So let's do that. I'll do
margin top and do for example 30 pixels.
Yeah looking pretty good. Great job
solving this challenge. Let's move on.
Now we are going to style the button in
our Google project because as you can
see this is how our button currently
looks. But this is how we want it to
look. So, we got to do a bunch of
changes. However, there's one thing I
want to change actually on this
google.com homepage because notice how
low the contrast is between the
background color of the button and the
background color of the entire website.
It's hardly possible to see that there's
any difference. And that's not good
because it reduces the accessibility of
the site and makes it hard for people
who are visually impaired to use it. And
actually to prove this, let's take these
two colors, the light gray here and the
white, and put them into a contrast
checker online. For example, this one
right here on userway.org.
They have this nice little contrast
checker where you can paste in the
foreground color and the background
color and then get a contrast ratio. And
this F8 F9 F8 hex color is the one that
Google is using. And if we scroll a bit
down, you'll see that Google indeed
fails miserably on this test. Complete
failure. The ratio is 1.05, though it
should have been 4.5 or more. So, not
good Google. So, what we will do instead
is change the background from this to
something like this. Here you can see
it's much easier to see that it is
indeed a button. So even though the real
background color is this one right here,
we are going to use this one right here.
So with that in mind, I want you to
style the button according to the
provided design you now have. And the
only hint I'll give you is that these
are the properties you are going to use.
However, I'm not giving you the values
of any of them because I want you to
practice looking at an example and
making your best guess as to what the
values for the CSS properties should be
as that's how you'll work out in the
real world when a designer, for example,
gives you a design file. So, go ahead
and do this right now and then I will
show you the solution afterwards as
well.
Okay, let's do this. I'm going to bring
up the example here. Have it on the side
so that we can see how it develops and
then start with the background color as
we have the value for that one down
here.
Round like that. Okay. Next, I would try
to see what is the most apparent
difference between this button and this
one right here. Well, the border is
certainly different. This button does
not have a border at all, whereas this
one has got has a border. So, let's just
remove it. Border none. Like that. Okay,
good. Now, we can see that the text is
much more crammed into this box than it
is on this one right here. So, that is
the padding. And also, you can notice
that there's more padding on the left
and right hand side than on the top and
bottom. So, I'm going to try to add, for
example, padding top five pixels.
Uh, it actually looks like this one here
has more than five pixels because this
space is still pretty crammed. So, I'm
going to do, for example, eight.
Yeah, I think that is more like it. I'm
going to give that on the bottom as
well.
Like that. On the sides, I'm just going
to try the double padding left 16
pixels. Padding right 16 pixels. And you
know what? I think that button and our
button looks pretty similar in size.
So, next up is the border radius because
you can see this one is totally
rectangular whereas this one has softer
corners. So, I'm going to do border
radius something like 4 pixels. So, that
is pretty good. The final minor
adjustments I'm going to make is reduce
the font size ever so little from what
it is here down to what it is here. I
mean, it's almost identical, but I
happen to know that this one is indeed
14 pixels. So, I'm going to do font size
14 pixels like that. And there we
actually have the buttons, this one and
this one identical. Now, if you chose
any other values than the ones I've
chosen, don't worry about it at all. The
point was not for you to reach the exact
same values as me, but just in the same
range, basically. So regardless of how
your example ended up, good job reaching
this far. Let's just keep up the pace
and move on.
Now we are going to add the second
button to our Google project. This I'm
feeling lucky button right here. And I
want you to do that on your own first.
So that's your challenge. Add the next
button. And do remember to give that
button a btn class so that it's styled
as our search button. And once you've
done that and executed the code, I want
you to take a look in the mini browser
and ask yourself, why aren't our buttons
behaving as we want them to? Because, as
you'll see, there's something wrong with
the layout, at least when comparing it
to this layout right here. So, go ahead
and do that right now and try to think
of why our layout is wrong. And then I
will do it as well when you return back
to me and explain what's going on.
Okay, let's do this. So, I'm lazy, so
I'm just going to copy this one like
that and write um feeling lucky. Run the
code. And as you can see,
the buttons are now stacked on top of
each other. Hm. What we can do in order
to figure out what's going on here is
actually use the built-in dev tools in
the browser, which you've seen a few
times now. So, let me show you this
debug session using slides. I'm going to
rightclick in the navbar and then choose
inspect. Open up the elements tab and
then click on the selector icon here so
that I can select this button by
hovering over it and then down in the
styles tab, I can scroll down until I
see the box model for our button. And
what you'll notice is that our button is
taking up all the available space on the
left hand side and the right hand side.
And if we move down to the second
button, you can see that that button is
also doing that. That is because they
are display block.
Exactly this line right here. If you
remember from previous lectures, display
block kind of takes up all of the
available space on the horizontal axis.
So as we said earlier, display block
stacks on top. We aren't able to get the
layout we want if our buttons are
display block. And then you might be
tempted to do well let's just remove the
display block. However, if we do they
will be on the same line. Yes, they are
now inline elements but they will just
be crammed all the way to the left hand
side because margin left right auto
doesn't work. If you remember from the
lecture about centering, you learned
that display block and margin left right
auto works together in order to center
something. You can't have just one of
them. Which means that in order to go
from this layout to this layout, we have
to use a different technique for
centering the elements. And there's
actually two ways we could do this.
There's the simple and inflexible way
using text align. And then there's the
slightly harder but super flexible way
using flexbox. And since you're going to
use flexbox several times throughout
this course, I want you to get familiar
with it right away. So in the next
scrim, we're going to talk about
flexbox. But before we do that, I just
want to show you the simple way as well
so that you've seen it. Because as you
might remember from one of the very
first scrims in this section, we used
something called text align in order to
align our text either to the left,
right, or center. Now the text align
property actually works on all inline
elements, not just on text. So what we
can do is remove the display block from
our buttons. As you know that turns them
into inline elements and they are
crammed to the left and as inline
elements only take up the available
space they need according to their
content. There is no available margin to
use. So auto here which basically means
take up whatever available margin there
is serves no purpose. So we can remove
both of these two as well. As you can
see it made no difference. And then we
can move up to the main the parent. So
we'll do text align
center like that and boom both buttons
are now centered. So here we have told
the container the main element all
inline elements inside of it should
align to the center. So that was the
easy but inflexible way of doing this.
Let's instead of using this technique
move on to do it how the pros do it
using flexbox.
Flexbox is everywhere. Let's just have a
look at Twitter for example. Here the
menu on the lefth hand side is created
with flexbox as with the tweet options
menu and the timeline itself. And guess
what these trends for you and who to
follow is created with? You guessed it,
flexbox. So this is something you need
to learn. Let's imagine then that you
have a e-commerce website which you can
see here that sells shoes, hoodies, and
t-shirts. Well, then you'd probably have
a navigation menu like this one where
users can click and then probably open
up a submen or something like that. As
you can see, the items stack on top of
each other. That is to be expected as
these are divs and divs are display
block. They want to take up all the
available space in the width. And if
you're wondering how I styled these, we
can head over to the styles.css and just
have a quick look at that. You can see
I've aligned the text to the center
inside of all the items. Hadn't I done
that, it would have been aligned to the
left. Also, there's a border. As you can
see, there's some padding on the inside,
and there's some margin in between the
elements. Now, the question is, what if
I instead of this vertical layout wanted
a horizontal layout, which is more
common for so-called navigation bars?
So, something that looks like this.
Well, this is where flexbox comes to the
rescue. And in order to turn this into a
flexbox layout, I would first have to
wrap these three items in a container
because a flexbox layout always consists
of a container and its direct children.
So let's wrap it in a div and give this
a class of for example nav dash wrapper
like that. If I run this now, nothing
happens because divs as you know are
invisible unless you give them any
styling or content. So let's head over
to the styles.css
and now target the nav wrapper like that
in order to turn this into a flexbox
container. All I have to do is write
display flex and boom. Suddenly we have
forced our three divs into one row. Now
this layout flows from left to right
along the horizontal axis and that is
pretty significant given that these
items were display block by default. But
flexbox, as powerful as it is, overrides
that and forces them onto one line.
However, we haven't got quite the layout
we want yet. As you can see, it's
centered and there's also some space in
between the items. Now, let's start with
the centering because flexbox now has
control over these items and can decide
where along the main axis they should be
positioned. So, we can do justify
content. That is a little bit of a weird
name for a property, but that's just how
it is. and then set that to center. Like
that. Now all of the items are centered.
And the final step is just to get some
spacing in between these items. And that
is done with our good old friend margin.
So we'll do margin left. Let's do 10
pixels. And let's add the same thing on
the margin right
like that. And there we have the layout
we wanted. Now a final cool thing I want
to show you is that we can do for
example end here as well. That'll push
it all the way to the right hand side or
start at the beginning or alternatively
do space dash around. That's pretty
cool. Now we've distributed all the
white space around each of the items.
And another one is space between. Now
all of the available space is between
the items. So they're spread out like
this. Now we are getting ahead of
ourselves here. I just wanted to
demonstrate this to show how powerful
flexbox is. What you are going to work
with is the center here. So with that,
let's return back to our Google clone.
Okay, we are back at our Google project.
And now your job is to center these two
buttons besides each other. Meaning
getting to this layout right here. And
the way to do that is of course using
flexbox. So you'll wrap the two buttons
in a flexbox div and then center them.
And then also I want you to give the div
a class of btn-wrapper.
So you'll need to modify both the HTML
and the CSS here. Now before you start,
I'm going to remove these three lines
here because they are solely there in
order to center our buttons using this
display block and margin left right auto
technique. However, now we're going to
use the flexbox technique. And we don't
want to use both techniques at the same
time because that'll just be confusing
as they will affect each other. So I
will remove these lines. And now you're
left with this left aligned layout here.
So please take over here, do this
challenge, and then return back to me
when you've done it or when you feel you
need some guidance. Good luck.
Okay, hopefully that went well. If not,
no worries. We'll do it now together. So
I'm going to start with this flexbox div
that we want in our HTML. It should wrap
our buttons and only our buttons. So we
want to start it up here. If class of
btn dash wrapper at I'm going to indent
my buttons and then end the div down
here. So now it is simply wrapping our
two buttons. If I run this code,
seemingly nothing has happened. But
under the hood, we have the button
wrapper which we can target using
btn-wrapper
like that. And then we can do display
flex. That's turned our div into a
flexbox. And now we can use the justify
content and do center like that. There
you can see both our buttons are
centered. However, they are totally
crammed into each other now. There's no
margin in between them. So, in order to
fix that, we want to add a little bit of
margin left and right to these buttons.
So, I'm going to do margin left, perhaps
four pixels, and margin right four
pixels as well. Like that.
That's looking pretty good to me. So, if
you didn't do this, no worries at all.
And also, if you are a little bit
confused about flexbox, that is totally
okay as well. We're going to work with
that several times throughout this
course, so you'll get more opportunities
to practice this. Just feel proud that
you've at least dipped your toes into
Flexbox as that is a super important
tool for any front-end developer. So,
great job. Let's move on.
Now, there's one final thing I want you
to know, and that is that you are
amazing because you have taken your very
first step into the world of CSS, and
that is more than most people ever do.
So, take this moment to feel great about
yourself. And also, let's take this
moment to recap what you've learned.
Starting out, you learned about the CSS
syntax. That is how you select elements
and also use properties and values. And
then you learned the key concept about
inline elements versus block elements.
So block elements stacking on top of
each other like these two and inline
elements standing kind of in a
horizontal line like the buttons here.
Moving on, you learned about margins,
which is how you create white space
outside of elements, for example, above
the Google logo here. And you learned
about its close cousin, the padding,
which is how you create space on the
inside of elements, for example, in
between the text and the edges of these
buttons. Also borders is what is between
padding and margin and we use that on
the input field here. We decided its
thickness, its type and color and also
the radius of the corners. So we didn't
want sharp edges but rather soft
circular edges which we achieved with
this border radius property. What we
also did was take a closer look at the
div tag which you saw briefly in the
HTML section. This time around though
you actually created two divs. one to
wrap the entire project and another to
wrap the buttons. And we had to separate
those from each other. So you had to
learn about classes. That is the
attribute you set like this. And you
select by using the dot notation in the
CSS. Also a big thing is centering in
CSS. You learned three different ways of
doing this. And the final way you
centered elements was through the use of
flexbox. a super powerful tool that all
front-end developers need to be
comfortable with. So, you have learned a
lot. Now, I think you should take a
little break. You have to digest these
things, recharge, and then I will see
you back here when you are ready in the
next section.
Welcome to the second CSS section of
this course where you are going to build
a business card for yourself. And it's
going to look more or less like this.
Though you're going to personalize it,
make it your own, not only with your own
information, but also with a beautiful
color palette of your choice and a font
that you like. So you are going to level
up your design skills as well as your
CSS skills, meaning that this is going
to be a lot of fun. So let's just get
started with it right away.
Okay, let's start out by having a quick
look at the basic skeleton for our app.
As you can see in the file system here,
we have an index html file, also a
styles.css file, and finally we have an
images folder which contains my avatar.
It's a png file called pair.png.
Now, starting with the HTML, it's just a
very simple setup with the HTML tag and
then the head tag which links to the
styles.css.
As for the body, it has one direct child
which is a div which will work like kind
of the container for our business card.
And inside of that div, we have an image
tag. We have an H3 with my name. We have
a paragraph with the title and an H4
with the location.
Moving over to the CSS, we only have one
rule here. We're setting the margin to
20 pixels on the body. And if you
remember how this works, when we're not
specifying either margin top, margin
bottom, left, or right, just setting the
margin in general, these 20 pixels will
be added on all four sides. on the top,
on the left, bottom, and right. And
that's simply because I want to avoid
that these elements touch the edges of
the mini browser. So, there's a problem
though. As you can see, the image isn't
rendering properly. It's just an icon.
That is, as far as I can remember, not
how my face looks like. And the problem
is in the image tag. So, I want you to
try to fix that. And I'm not going to
give you any more hints. Rather than if
you are stuck, try to Google HTML file
paths. That should help you. So go ahead
and give this one a shot and then I will
show you how to solve it as well when
you return back to me.
Okay, let's do this. Let's take this
string right here and paste it into
Google. There you can see that the top
result is a W3 schools article. Let's
head into that one. Here you can see
file path examples. Let's zoom in on
this one. And if we have image source
equals picture.jpeg, JPEG, which will be
the same as we have here, although our
image is called pair.png. That means
that the file is located in the same
folder as the current page. Well, is
that the case? Now, looking at our
current page, index.html, you can see
that the image is not located in the
same folder as the index html because
the image lives inside of the images
folder. So, what we then might need is
something like this where we do images
slash and then the picture name. here.
It means that the picture is located in
the images folder in the current folder.
And that looks correct. The images
folder is in the same folder as
index.html. And the headshot of me is
located inside of that images folder.
That means that we can try to do images
like that. Slash run the code. Oh, there
we go. We have my avatar imported and
are ready to move on to the next trim.
I'll see you there.
Hi there, my name is Bob Zroll. I'm
another teacher here at Scribba and I
wanted to expound a little bit on the
lesson that you just learned with pair.
This is a very similar example to what
you just saw with the Google homepage.
In this example, we have an image that
is referencing cat.jpeg, but as it turns
out, relative to where our index.html
file is, that cat.jpeg file is only
inside of the images folder. So, as you
just saw in the example with pair, I
will need to prefix this with images
slash. When I begin my string this way,
it's saying look in the directory that
the current file or the index.html file
is in for another directory called
images. The directory that the
index.html file is in is called the
project root folder. And that's
everything that you see here under this
explorer title here in Scribba. And by
correctly referencing the path to where
this cat.jpeg JPEG image is found. I can
hit save and our image of this cat is
giant because our CSS file is commented
out, but we'll fix that in just a
minute. Okay, so this is simple enough
to understand, but there are some other
points of syntax that I think are really
helpful to know. First of all, and
probably the easiest one to understand
is that if you prefix the path with a
dot slash, it's a way to tell in your
code that you are going to be starting
from the current directory. That's
exactly what we're doing here already.
So adding a dot slash does exactly the
same thing. As far as I know, there's
functionally no difference. Now on its
face, it might seem like it's just
extraneous information. However,
personally, I do prefer to include the
dot slash at the beginning. I think it's
a little bit more explicit and kind of a
visual reminder that I'm referencing a
local file inside of my project. But
that's going to change from one person
to the next and one team to the next. So
I mostly wanted to include it here so
that when you saw it, it wasn't
confusing. We can see I can hit save and
we're still getting our cat image
showing up correctly. This leads us into
another way that you can use relative
path syntax to reference other files in
your tree and that's by prefixing your
path with a dot dot slash. Using the two
dots is a way to start looking at the
parent directory first. Okay, when would
this be useful? Well, let's go into our
source directory and then inside of our
CSS directory and open up our index.css.
And here you'll notice that we're
selecting an image div class and giving
it a background image with this URL.
This URL has a relative path that goes
up two directories first before going
down into the images directory. Because
our index.css CSS file lives inside of
the CSS folder, which is inside of this
source folder, which is a sibling, I
guess you could say, folder to the
images folder. We need to go up two
levels from CSS to source and from
source to the project route before we
can then drill back down into the images
directory. Now, why isn't this dog.jpeg
showing up in our mini browser over
here? That's because in our index.html,
HTML. We commented out this stylesheet
and we don't have the correct href here.
So, take a quick second as a mini
challenge. Pause now and fix this href
so that first of all, our cat image will
be more reasonably sized and second of
all, the dog image will show up. Pause
now and see if you can get that to work.
To do this, we can start with a dot
slash if we want. go into the source
folder, then into the CSS folder, and
then into index.css.
We'll hit save. And there we go.
Everything is working. Really quickly,
we can also see an example of this dot
dot slash. If I were to move my
index.html inside of, let's say, my
source folder, I'll need to make a quick
change to the mini browser here to begin
it with /source.index.html.
But you can see that my sleeping cat is
broken and so is the link to my CSS
page. Because my index.html page is now
nested inside of my source folder, I'll
need to make a change both to my link
here and to my image here. And by I'll
need to make that change, I mean you
need to make that change. Let's make
this into a quick challenge. Pause now
and fix both of these paths so that
everything goes back to the way it was.
Okay. Well, relative to the index page,
which is now inside of the source
directory, I no longer need to reference
/source. Instead, I can just simply go
into the CSS directory because the
current directory is the source
directory. With our cat image, I need to
go up a directory first in order to get
to the project route where I can then
drill down into the images directory.
So, I'll hit save. And sure enough,
everything's working the way it was. not
feeling great about putting the index
page inside of my source directory. So,
I'm going to move that out. I'm gonna
change these back and fix my mini
browser so that it's not looking at the
source anymore. Okay, perfect. The last
syntax that I'll talk about is just
using a slash at the beginning. Having a
slash as the very first character of
your relative path is a way to start
directly from the project's root
directory. For example, if I didn't want
to do this whole thing where I'm going
up two directories before then going
down to my images directory, it's a
little bit more concise for me to begin
this with a slash that says just start
at the top root directory of this
project rather than worrying about going
up so many levels and then back down so
many levels. So that's what the slash at
the beginning of your path does. Now, I
have one more challenge for you and
that's to fix our script source. We
haven't talked too much about
JavaScript, so don't be too concerned
about anything dealing with JavaScript.
All we need to do is make sure that the
source to the JavaScript file is
correct. And when it is correct, you
should see a message pop up in the
console. That's all I'm going to say
about that for now. I want you to figure
out the rest. So, pause here and fix the
source for our script.
Our index.html is at the root of our
project. So, I have a bunch of different
ways that I could do this. I could just
start with a slash to indicate start
from the root. Then I would go into
source scripts. Let's open this up.
Scripts and then main.js. So,
slashmain.js.
We'll hit save. And we get that I'm the
main.js file console log just like we
would expect when it's working. Because
this file is at the root, I don't
necessarily need to specify a slash at
the beginning. I could just say src or
as I mentioned my preference would be to
start with a dot / src and hitting save
shows that log coming up again and
everything also working that way. I know
this is a really simple topic but I'm
hoping this can eliminate any confusion
that you might have as you're looking at
other people's code other code here on
Scribba or maybe help you avoid a really
tricky bug sometime about having a path
that's incorrect somewhere. Okay, thanks
for letting me talk about relative paths
for just a little bit. I'm going to pass
you on back to P.
Okay, now we're going to do a little bit
of housekeeping here because this image
element actually has a serious flaw
because it lacks an important ingredient
that all image tags should have which is
called the alt text or the alternative
text and that is simply an attribute
that is written like this alt equals and
then inside of quotes you are to write a
description of the image. So here it
could be for example Pier Borggan
smiling like that. However, if we run
this code now, you can see that
seemingly nothing has changed. So why do
we at all write these old texts? Well,
first of all, it makes your site
accessible because imagine that a person
who is visually impaired or who is blind
is trying to visit your website. In that
case, they would use a screen reader
which goes through the HTML and reads it
out loud to the user. And as for the H3
tag, it would be simple for the screen
reader to read out loud what that says
because that is text. However, for an
image like this one, you need to add an
alt tag to describe the image so that
the screen reader has something to read
out loud. And secondly, this also works
as a fallback if your image doesn't
render properly cuz let's say you have a
spelling mistake in the sorts attribute,
for example, an extra R. Well, in that
case, the image won't render, but the
alt tag provides as a backup so that the
user seeing this can kind of understand
what was at least supposed to be there.
And finally, writing alt text will help
your site rank higher on Google, meaning
you get more search traffic. And that is
because the alt tag most likely provide
important metadata about the image so
that Google knows how to use it in a
search results page. And if your site is
not accessible, it might be punished in
the form of ranking lower. So with that
in mind, let's look at how to write
these alt texts. And the first thing you
need to know is that you should not
include for example image of because
that's simply not necessary. The screen
reader knows that it is an image because
it is an image tag.
Secondly, should be concise meaning less
than 125 characters because a lot of
screen readers will cut off after this
limit has been reached. And finally, a
trick you should use in order to come up
with a good old text is imagine that you
are describing this image to a person
over the phone. So let's try to do that
with an example. Here we have an iconic
image of Muhammad Ali in a boxing match.
And one way to write an old text for
this could be just a boxing match, but
it would actually be a pretty poor way
of describing it because there's so much
more going on here. You have plenty more
characters you can use in order to
describe it. So, a better one would be
two boxers in the ring, one lying down
and the other standing up. That's a good
factual explanation, but we're not quite
there yet because it isn't actually
describing this iconic moment when
Muhammad Ali knocked out Sunonny Listen.
So, if you were to describe this over
the phone, you would probably understand
that this description is pretty shallow.
So what I would rather write as an old
text for this image would be Muhammad
Ali in the boxing ring screaming at
Sunonni listen after knocking him out.
That in my opinion properly describes
the intent of this image. What the
person who has decided to show this
image on a website is trying to convey.
So with that example in mind, I want you
to write what you think is an
appropriate alt text for this image. go
ahead and do that right now and then I
will show you what I would write when
you return back to me.
Okay, let's do this. We'll do alt equals
quotes and here I would use my name pair
Harold Borggan. And you can see I'm
smiling and I'm looking at the camera. I
think that's important. So I'd write
smiling at the camera. Finally, the
background is really playful and
colorful.
So, I want to include, for example, with
a colorful
background like that. So, if yours was a
bit simpler or shorter, don't worry
about it. The most important thing is
that you add an alt text and do as best
as you can. And actually, plenty of
developers never add this. So, just by
adding it, you are actually separating
yourself from most other beginners. So,
with that, let's just move on.
The next thing we're going to do is
shrink this image to a suitable size for
our business card because as it is now
it is far too big. So I want you to head
over to the styles.css and solve this
challenge. You are to set the width of
the image to 150 pixels and you need to
use a class to select the image element.
So go ahead and give this a shot right
now.
Okay, hopefully that went well. Let's
now do it together. So, first we need to
actually create the class that we are
going to select from the CSS. So, let's
head over to the HTML file here. We'll
just do class equals. And I'm going to
call this one an avatar like that.
Actually, now that we have three of
these on the image element, I think for
readability purposes, it works well to
break them into three lines.
I think this is a little bit easier to
read. Then I'll head over to the
styles.css and target the class avatar
and just set the width to 150 pixels.
Now rerun the code. There we go. So
great job solving this challenge. Let's
move on.
Okay, now you're soon going to start
building the layout of this card.
However, right now the card kind of
blends into the background. There's no
separation between the card and the
background. So, I want to add a little
helper style in order for us to more
clearly separate the card from the
background. And for that, we need to
grab hold of the outer element of the
card. And can you take a guess what kind
of HTML element do you think should wrap
the entire card?
If you guessed a div, that is correct.
And heading over to the HTML, you can
see we are actually already wrapping the
entire thing in a div. So here we have
our container div or card div or
whatever we want to call it. So what I
want you to do now is to first give that
div a class and actually I think you
should call it card and then I want you
to select the card class in CSS and give
it a dotted blue border. This is our
helper styles. It'll give us a little
blue border around the element so that
we can see how our card div kind of
behaves. And also, as you can see from
the design here, none of the elements
touch the edge of the border. So, it
seems like there's some padding in play
here. Let's just add 20 pixels of
padding on all sides of the card as
well. So, go ahead, solve these three
challenges, and then I'll of course show
you the solution as well.
Okay, let's do this. So, first we need
to give the card div a class.
I'll head over here and do class equals
card.
I'll run this code so that we have this
class in the DOM.
The next step is to select the class by
doing card
and then adding a border. If you
remember from previously that is done
through the border property which takes
three values. The first one being the
thickness of the border. I'm just going
to set it to one pixel. The second being
the quality for example solid or dotted
or dashed or a few others. We're going
to use the dotted one and the color will
be blue. And there you can see boom we
have a border around our entire card.
And this will make it a lot easier for
us to work with the card now that we can
see it clearly on the page. Finally, we
want to add 20 pixels of padding because
as you can see here, the elements touch
the border on these sides and we don't
want that. We want some space. So, we'll
do padding
20 pixels like that. And there we go. We
have solved the challenge. With this in
place, I think we're ready to start
moving on with fixing the layout. And as
you can see on the finished design here,
there's a big difference between this
one and our current one. And that is
that the current one is vertical whereas
this one is more horizontal. So they
stack up differently. We want the image
on the left hand side and the text on
the right hand side, not everything on
top of each other. So basically we want
two columns, one for the image and one
for the text. And as you might remember
from previously, flexbox helps us create
columns. So let's now just experiment by
adding display flex to the card and
let's see what happens. Oh wow. Okay. So
what happened there? It actually turned
every single element into a column in
itself. That's not exactly what we want
here. So display flex isn't the entire
solution. So, what we're going to do now
is go out on a tangent, learn a little
bit more about this flexbox concept in
CSS, and then return back to this
business card project once we're ready
to build a layout like this. So, let's
move on and learn more about flexbox.
Okay, so let's say that you've built
some kind of software as a service and
now you want to sell it. Then you might
have a page like this where people can
upgrade to pro and they can choose the
individual plan which gives them 1 GB
and no support or the enterprise plan
which gives them 10 GB and priority
support. Now I've purposely not given
any design to this example as I wanted
to make it as easy as possible to grasp.
But hopefully if you are to create a
pricing page you make it look a lot
better than what we have here. So you
can see the HTML right here for the
entire pricing layout. I've wrapped it
in a div and I'm using an H2 unordered
list, a button and so on. Now let's say
that you instead of having this one
column layout here where everything
stacks on top of each other, you want
this two column layout where you have
the individual plan at the left hand
side and the enterprise plan at the
right hand side. Well, if so, we know
that flexbox will help us create
columns. But if we try turning our
container is the div with a class of
pricing into a flexbox,
you'll see that
it'll break the layout just as we saw in
our business card example. Because
what's going on here is that flexbox
takes every single item that is every
single direct child and turns it into a
column in itself. So we get a column for
the H2, for the unordered list, for the
buy button, for the H2 again, and so on.
And this is obviously not what we want.
It's far from this two column layout
right here. However, there's one hint to
be drawn from this example. And that is
that if you check out the unordered list
here, you see that it has two children
of itself, LII elements. And those two
are not their own columns. they are in
the same column stacking on top of each
other. So it seems as if flexbox only
turns the direct children into their own
columns. So the direct children are the
h2 here, the ul, the button, but not the
list items. And once we know that, we
can use that knowledge in order to get
to this layout. Because this layout has
two columns, that means that our flexbox
container should have two direct
children, not six as it has now. And how
can we achieve that? Well, we want this
group to be inside of a direct child of
this container. And we want this group
of HTML elements to also be inside of
another direct child of the flexbox
container. So what we'll simply do is
we'll wrap both of these inside of a div
like that. And we'll indent it for
readability purposes. Let's try to run
this code having wrapped one of the two
groups into its own div. And there you
can see the four individuals plan now
stacks up just how we want it. But the
rest of the layout is still broken. So
we need to do the same thing down here
like that. Run the code and there we go.
Now our flexbox container actually only
contains two items. this div and this
div as it doesn't care about what's
inside of both of those children. It
only cares about its direct children.
And the result is that we get the layout
we wanted here, at least in terms of
having two columns. There's still a
problem with lacking space in between
these two children, but we're not going
to deal about that right now. For now,
I'm going to send you back to the
business card example so that you can
use this to improve our layout.
Okay, we are back at our business card
example and now I want you to use what
you've just learned in order to fix our
cards broken column layout cuz you are
going to make this card into a two
column instead of a four column which it
is now it has the image name title and
location at four different columns but
instead we want two columns one for the
image and another one for the text
elements. So this challenge is solved in
the HTML. As you know, we already have a
display flex on the card and the only
thing you need to do is one change
inside of our flexbox. So hopefully
you're able to do that. Go give it a
shot and then I will show you the
solution as well.
Okay, hopefully that went well. Let's do
it together. So the problem is that
flexbox treats each of these elements as
a single child and thus each of them get
its own column. However, we only want
two columns. So we want these three to
be governed by one flex child. So we
need to wrap them inside of a div. We'll
do like this. Open the div and then
close the div down here. And we'll
indent these so that we get some better
readability. Now you can see the flexbox
only has two direct children. This one
and this one. That means that we should
get two columns in our layout. And yes,
we do. Brilliant. Of course, there's
still a long way from this design to
this design. But we are making great
progress here. So, let's just keep up
the pace and move on to the next scrim.
Okay. I think looking at this example
now, I got to admit that I'm pretty
pleased with this border, one pixel
dotted blue here, I think it serves a
very nice purpose, which is to highlight
for us how our card is being laid out on
the page. So, wouldn't it be nice if we
could use that exact technique on other
elements on the page as well? For
example, the children of the flexbox
container, that is this image right here
and the div tag. That would have been
really neat. And we can do that by for
example just copying this one and adding
it for example to the avatar. See there
the avatar is got that blue border as
well. However, wouldn't it be neat if we
in our HTML could just quickly apply a
class to any element for example this
one and it would get the same thing as
well. So basically the argument I'm
making is that we should create a
dedicated class for this blue border.
And what that is is a utility class, aka
a class that only sets a single CSS
property. So let me show you how this
would work. Let's say that we for
example want a class to make our text
italic. Then we'll simply do class
equals itallic like that. If we run this
code, nothing has happened because the
class hasn't been created yet. But here
we can create the italic class and do
font style italic like that. And now you
can see the name is in italic. Now if we
want to reuse this, we can just do class
italic here as well. Run the code and
the location is also italic. And if we
want the entire card itallic, we can
even add the class here up on the
container element itself. Run the code.
And now this has been inherited down to
all of these elements. And if you're a
bit confused about what I'm doing here,
it is simply adding two CSS classes to a
single element. Do that simply by adding
a space in between the two classes.
Okay, let's get rid of these like that
and delete this one as well. Now, what I
want you to do is create a utility class
for the dotted blue border. This is the
styling we have here. And then give the
class to both flexbox children in the
HTML. that is the flexbox items you can
see here and here. And also I think you
should give the same class to the card
as well so that you can remove this line
10 here. So go ahead and give this one a
shot and then I will show you how to do
it afterwards as well.
Okay, hopefully that went well. Let's
start out by creating the class. I'll
write border blue here.
And then inside of it, I'll just cut
this away and paste it in here instead.
Now we have a utility class for blue
borders. I'll copy it and add it to this
flex item. And as a second class to the
image element, we run this code. You can
see now both of these have the blue
border around them. Let's also add the
border blue class to the flexbox
container. run the code and boom, there
we go. Now we have an even better
overview over how our elements are laid
out.
Okay, with these blue borders around the
flexbox children, it's pretty easy to
see that our layout is pretty far from
how we want it to be. Cuz here you can
see in the card that there's some space
in between the two elements. Whereas in
our example, they're completely crammed
together and also pushed all the way to
the left hand side. Whereas here they're
kind of spaced out. There's some space
on the left hand side in between the
element and also some space on the right
hand side. So they're kind of spaced out
in a nice way. And now I want you to add
that horizontal space around the flex
children. There's a flexbox property
that does this. And you've seen that
property before. However, you might have
forgotten it. If so, then Google justify
content and you might find the answer.
I'm not going to give you any more hints
than that because if you have forgotten
about it, it is a great practice to
Google it. Then once you figure out the
answer, go ahead and add the horizontal
space so that our example becomes a
little bit more like this layout. And
just so you know, you only need to add
one property for this, not multiple. So
give it your best shot and then I will
show you the solution afterwards as
well.
Okay, let's do this. So the property is
called justify content. A little bit of
a weird name, I agree, but that's how it
is. And here we can use a range of
different values. And the one we're
looking for is called space around like
that. And now you can see both elements
now have some space around them. And
also we could do space between then all
of the available space will be between
the two items. Or we could even do
center as you've seen before. then
there's no space in between them but
both of them are centered and end pushes
it all the way to the end and start
keeps it at the start. So this is a
super powerful property and we want the
space round value for it. As you can see
it's at least getting closer to the
layout we have in our finished design.
So great job. Let's move on.
Okay, time for a new challenge. You are
going to set a fixed width on the card
and then center it on the page because
its behavior is a little bit weird right
now. As you can see, if we enlarge the
screen, the card gets enlarged as well
and that makes the spacing between the
elements look super weird. The aspect
ratio becomes very different from a
business card. So, we don't want that.
Instead, we want the width to be 400
pixels. So, it should be a fixed width.
And then we also want the card to be
centered on the page. Now, one thing I
want you to notice when I enlarge this
is that the card is a display block even
though we've set it to display flex. So
that means that flexbox containers by
default are display block. And hopefully
you remember from previously that
display block takes up all the available
space in the horizontal axis. And it is
also one of the three ingredients you
need in order to center an element using
margins. So hopefully you remember from
the Google project how to center block
elements. So go ahead and give this one
your best shot and then I will show you
the solution as well when you return
back to me.
Okay, let's do this. I'll start by
setting the width to 400 pixels like
that. Now you can see if we enlarge the
screen, the card is stuck to the left
hand side of the website. So, in order
to fix that and instead center it, we'll
use margins. And hopefully you remember
that we'll do margin left and set that
to auto. That pushes it all the way to
the right hand side because all of the
margin is now on the left hand side. But
then in order to even it out and center
it, we'll do margin right as well and
set that to auto. And the result of
having an equal margin on the left hand
side and the right hand side is that the
card is centered. So, great job on
solving this challenge. Let's move on.
>> All right, let's take a minute to talk
about CSS inheritance. And you're
probably thinking, wait a minute, this
isn't the singongy Nordic accent of
Perorgan. And you'd be right. My name is
Bob Zurroll. I'm another teacher here at
Scribba. And I'm interjecting myself
here to make a small accessibility
update just in the way that the HTML was
written in the original version of the
screencast, but also as a way to
introduce myself. If you're here as part
of the front-end career path, you will
be taking another one of my courses,
either the APIs and asynchronous
JavaScript course or the React course
that's later on in the path. Okay, back
to the topic at hand. Let's learn about
inheritance and why it's important to
learn about inheritance in the first
place. First of all, it will help you
write, you could say, drier code. Dry
stands for don't repeat yourself. In
other words, you can take advantage of
this concept of CSS inheritance so that
you don't have to keep writing the same
CSS code over and over, but instead can
define it in one place and have that
change cascade throughout the rest of
your CSS. Inheritance is also something
that can help you avoid bugs in your
code and generally be more efficient. To
demonstrate this, we've pulled in an
article from Wikipedia here about the
dancing plague of 1518. This is a really
strange phenomenon that happened in
modern-day France where somewhere
between 50 and up to 400 people
inexplicably took to the streets and
started dancing as it would appear
against their will. It's a fascinating
story. Feel free to read through the
events. There's even an illustration
here. I have no idea what could cause
this, but there's some pretty good
theories here that include food
poisoning and so forth. So, feel free to
look through this article, but that's
just for fun. Let's not go too far down
this rabbit hole. Instead, let's talk
about inheritance in the context of this
article. Since we've pulled this into
our own page, maybe we've decided that
we would like to center all of the text
on this page. And so, I could go to my
styles.css and start centering each one
of these elements by itself. For
example, we have an H1, a paragraph, an
H2, more paragraphs. So, sure, I could
start by saying I want to select the H1
and do text align center. Okay, I've
centered my text there for the H1. Now,
let's go to our H2 and do the exact same
thing. So, we'll go ahead and center the
H2. Great. We're partway there. Then, we
have to also select the paragraphs. And
hopefully, you can see the point I'm
trying to make here. In this case, we
would not consider our code very dry
because we are repeating oursel instead
of following DRY. Don't repeat yourself.
So, what does this have to do with
inheritance? Well, let's look at our
HTML and we can see that our entire
article is conveniently placed inside of
an article element. The idea of an
article element in HTML is
self-contained content that if you were
to remove it and put it somewhere else,
it would still make sense. Of course,
the dancing plague of 1518 is hard to
make any sense of, but I think you get
my gist. In this case, if we really do
want everything in our article to be
text align center, we can take advantage
of CSS inheritance by instead of
repeating all of our rules here, we can
say everything inside of the article
should be text align center. And as you
can see, this single rule has been
inherited by all of its children
elements where text align center makes
sense. Now, I could have chosen to put
text align center up on the body
instead. And you can see we get the same
effect. Essentially, we can know that
this inheritance doesn't just happen
with the direct children of the element
that we're selecting in CSS, but will
work any number of layers down the
descendant tree of the element that
we're selecting. I personally prefer to
have it on the article because maybe
there's other elements that we don't
want to have text align center that are
outside of the article tag. So, let's
leave it here on our article. Now to
refine our understanding of inheritance,
it's important to know that text align
is an example of a CSS property that
does get inherited down the tree.
However, not all CSS properties will get
inherited. I have found that it's pretty
intuitive which ones will and which ones
won't. But like we should do anytime we
have a question as we're learning
something new, we can Google it. Here
you can see we've Googled which CSS
properties are inherited. And there's a
Stack Overflow question exactly about
this. If we click into it, we can see
down here somebody has answered it and
begun a list. If we scroll down, we get
all the way down to 41 different CSS
properties that are inherited. Now, let
me be honest, most of these are not CSS
properties that you're going to really
have to care too much about. In fact,
when I was looking at this, I think
probably over 50% of these properties
I've never even heard of before. Some of
them, however, like the text align that
we just used and some of these other
font related ones, like font family,
font size, font style, and so forth, are
really good ones to know. All right,
let's get your hands on the keyboard
with a challenge. I want you to navigate
to this Stack Overflow question to pick
a few of the inherited properties on
this list. You can just click the
screenshot here to go directly to the
article and try applying them directly
to the article element that we've
selected here and just see this working
in practice. I know it might seem a
little bit frivolous, but this kind of
hands-on work is what really will engage
your brain and help you remember the
stuff that we're learning. Now, as a
fair warning, some of these properties
are going to affect things like screen
readers more than anything you might see
visually. Properties like volume or
speech rate, but other ones should be
readily apparent in the visible output
that you see here. Then, once you're
done, I'm going to hand you back to pair
to continue on with the next scrim.
Okay. Now it's time for a challenge in
the business card project because you
are going to center the text inside of
the paragraph and the H4 elements and
that is the paragraph right here which
says frontend developer and the H4 right
here. Because as you can see, both of
those elements are now crammed all the
way to the left hand side. Whereas in
our finished design, they are aligned at
the center of this flex children
directly underneath the name Pier Harold
Borgen. And if you're wondering why
we're not caring about centering this
one, it's because it takes up the full
width of the container. So you can't
center or left or right align it because
it takes up the full width anyway. it is
the element that actually decides how
wide this container should be. So you
will only see the paragraph and the age
for change when you fix this. And the
way you should do it is by only adding
one property and then have inheritance
take care of applying that property to
both elements. So you're only going to
write one line of code. And I'm not
going to tell you where you should add
that line of code. You have to figure
that out on your own. And I'm also not
going to tell you what the property is
called or the value you'll give it, but
you have seen it before. So hopefully
it's not that foreign to you. So jump
into the code and try to solve this
challenge now. Then of course revert
back to me when you want to see my
solution.
Okay, hopefully that went well. Now
we're going to use the text align
property and set it to center. But
before we do that, we need to decide
which element we should target with that
property. Should we add it to the entire
body, for example? If so, it'll inherit
down from the body into the card and
then into our flex children right here.
And then finally to these elements as
well. That works perfectly fine. We can
actually try it. Text align center. And
boom. These two are now centered.
However, imagine that this card is a
part of a bigger website. If so, we've
now centered all text inside of the
body,
which is where all of the HTML, both now
and in the future, will live. Though,
this might have second order
consequences. Perhaps we want to have a
navbar with a title that isn't text
align center, but rather text aligned
right or left. So, actually, I think
it's better to scope this to, for
example, our card. We at least know now
that all of the text we currently have
in our card should be aligned to the
center. So, let's do that instead. Let's
just remove it from the body and paste
it in here. And now we're not causing
any unwanted second order effects
outside of the card. So this is in my
opinion a better solution. But if you
added it to the body, that is a
perfectly valid solution as well. So
don't feel that you did a mistake. In
coding, there's always plenty of ways to
solve things. And there's rarely one
right answer. There's just consequences
for whatever choice you make. So really
good job doing this. Let's move on.
All right. Now, the fun stuff remains,
which is adding colors according to the
specified design here. So, this is a
really rewarding little challenge
that'll make a ton of difference as
you'll go from this boring white layout
here to a nice colorful layout. So,
you're going to add these three colors.
As you can see, I've added the hexadimal
values for each of the colors, the
black, the blue, and the purple. The
purple is the border at the bottom here.
The blue is the background color, of
course. And then finally, you might
wonder, well, what's the black? Well,
that is actually the text color, as the
text here isn't pure black, as it uses
this shade of black instead. Now, you
might wonder, well, how do I create this
border at the bottom here? I've only
learned how to create borders around an
entire element previously. Well, the way
to solve any problem when coding is to
Google it. So, let's just write border
bottom CSS in Google. There you can see
a W3 schools article. Click in on it.
And here it has some code examples which
we can zoom in on set the style of the
bottom border for different elements.
And here it shows us how to set the
border bottom. So we do border
dashbottom and then for example 5px
solid red. Well that's exactly how we'd
set the border itself except that we
would omit this dashbottom in the
property name. So you'll use the exact
same technique you've used for the
entire border just with this extra word
added to the end. So hopefully you'll be
able to do that. So here is the design.
And a final thing I'm going to do before
I'll leave it to you is to remove these
blue borders here because we don't need
them anymore. So let's head into the
HTML. I'm going to mark the border blue
utility class we've added. Do command
plus D. That enables you to select
multiple instances of whatever you've
highlighted. And then boom. I'm going to
remove it like that. Then also we don't
need any class in the div for this flex
children anymore. The code our borders
are gone. I'll leave this for now in
case you want to add it back again. But
now it's your turn. Go ahead and try to
solve this challenge and then of course
I will show you the solution when you
return back to me.
Okay, hopefully that went well. Let's
now do it together. I'm going to start
with the blue background as that'll make
the biggest difference. So I'll do
background then copy this hex value like
that. Very good. Then I'll do the text
color that is color and copy the one
right here. Yes, not as visible as the
previous change, but something surely
happened. We're now using that shade of
black for the text. And finally, the
purple border bottom will be added by
doing border dash bottom. And then let's
try two pixels. And we want to use solid
than the purple color. Okay, we can see
a thin border at the bottom. Let's have
a look at the design. It looks like it
is definitely thicker here. So, let's
crank it up a little bit. For example, 6
pixels. Yes, that looks pretty good to
me, actually. So, really good job
reaching this far. You are very close to
the end right now. So, let's just keep
up this great pace and finish up this
project over the next few scrims.
Okay, now I want to talk a little bit
about fonts because fonts are actually
really important. Let's say that you for
example have created a computer
specialists company called Click Lovers.
Well, you might want to think about
which font you choose in that case. Or
if you're the owner of the Megaflex
shop, you should also think carefully
about which font you choose. Finally, if
you are the head of the wig and pen
association, make sure that your font
spaces out words correctly. Now, these
are of course extreme cases and they're
all on prints. You'll rarely see cases
as bad as this on the web, but fonts
really matter online as well. Let's take
Wikipedia for example. Here is a random
article about people who have died from
laughter. As you can see, Wikipedia has
this very serious nice font. But if we
change that to something like Comic
Sandance, you can see the text gives a
whole different type of association. It
went from being very serious like this
to being more like a comic book. So when
you are building projects, it is
important that you choose a font that
kind of suits your project. So let's
move on to look at how it works
technically under the hood. Because when
you visit a website, for example,
scribble.com, you visit it via a client.
That could be your mobile, tablet,
desktop, or whatever. What happens is
that when you type www.scrimble.com
scrimba.com into the URL bar. It sends
off a request to Scribba's server and
then the server says, "Yeah, okay. You
want to visit our website? Yeah, of
course. Here you go." And it takes
everything you need in order to run the
Scribby in the browser. That is the
HTML, the CSS, the JavaScript, the
assets, which can be images and videos
and so forth. Then also the recipe for
the font and then it sends that back to
the client. However, for some websites,
they won't send you the recipe for the
font over the network because they will
instead on their website use a font that
most likely is already pre-installed in
your client. That is also called a web
safe font, meaning that the recipe for
rendering that font exists in most
clients. Now, of course, there might be
some clients that don't have that
specific font, but in almost all cases,
it's safe to use one of the web fonts
without sending the recipe for it over
the network. So, what are these websafe
fonts? Well, here is an article on V3
schools where you can see all of the web
fonts for HTML and CSS. So, when you're
using these, you don't have to send the
recipe for the font over the network.
Now, for our digital business card,
we're only going to use Web Safe fonts,
but later down the line, you'll also
learn how to pull in external fonts so
that you can go outside of these 10
fonts as they don't really give you a
lot of flexibility. So, with that in
place, let's look at how you actually
use a font in CSS. So, in order to set a
font, you first target normally the body
and then type out font family and then
the name of the font. And here you can
see we've typed V and that triggers VS
Code which is the editor powering
Scribba to come up with suggestions for
you. You can see it doesn't only suggest
Verdonna which is the font we want. It
also adds a bunch of other fonts after
it all separated by a comma. So if we
hit enter now, boom, then we've added
what's called a font stack. And what
this means is that if the browser finds
the Verdonna font installed, it finds
the recipe for that font, well then it
will use it. However, if it can't find
it, if Verdonna isn't installed in the
client's computer, then it'll try to
find Geneva, which is a pretty similar
font. If it can't find Geneva, it'll
jump on and try to. And if it can't even
find Tacoma, it says, well, give me any
Sans serif font. And sans serif is a
category of fonts. So, here it's just
saying, give me any font from this
category. And there are mainly two
categories, sans serif and serif. Here
you can see an example of both of them.
Serif fonts contain these decorative
strokes at the end of the letters. You
can see it here, here, here. Whereas
sans serif is without those decorative
endings. So sans then means without or
not or something like that. You get the
point. Okay, that was a lot of theory.
Now it is your turn. You are going to
set the font here to Verdonna. And also
make sure to remember to get the entire
font stack so that we have backups in
case the client doesn't include the
Verdonna font. So go ahead and do that
right now.
Okay. Hopefully that went well. What
you'd want to do is font family colon
start typing and then the editor
suggests the entire font stack. Though
you won't see that now because the
scrimbot DOM recorder doesn't record it.
However, I can see it. So, I'm just
going to hit enter and boom, the editor
suggested this entire font stack with
me. We have changed the font. Let's
verify that by removing it. Boom. Yeah,
that's different from this. So, really
good job. We've actually now completed
the design for our business card.
However, there's a little issue with our
code because this is how noobs write
their margins. And I want to teach you
how the pros do it. So, let's move on to
the next scrim where you'll learn
exactly that.
Okay, now it's time to learn about the
shorthands for margin and padding. And
you've actually been exposed to one of
them already, which is that if you have
the same value on the margin top, right,
bottom, and left, you can reduce those
four lines into one margin 10px. Then
the browser will take this 10px and
apply them to all sides. So these four
lines are identical to this one down
here. Now the question becomes what
happens when you have four different
values for example 10 px 20 px 30 and
40. Well if so you have to do like this
margin and then 10 px 20 30 40. So the
first one is margin top the second one
is right the third one is bottom and the
fourth one is left. So now you're
probably thinking well how can I
possibly remember this order? And the
trick is to think of the clock. It
starts at the top then goes to the right
then goes to the bottom and then goes to
the left. So with that in mind, I want
you to improve this code here which
creates the Japanese flag in CSS because
as you can see this red circle here
which is a div has a few styles applied
up here but down here we've used the
nonshorthand way to set the margins.
Setting some margin on the top so that
the circle is pushed down. nothing on
the right and the bottom but 100 pixels
on the left so that it's pushed to more
or less the center of a screen given
that the screen is as wide as it is
right now. So I want you to translate
this into using the margin shortand
instead. Go ahead and do that right now.
Okay, hopefully that went well. I'm
going to actually comment these lines
out. Now you can see the red circle is
up in the left corner. And then I'm
going to do margin starting at the top
should be 40 pixels. Then the right side
0 pixels. Then the bottom should also
have zero pixels. And then the left side
which should have 100 pixels. And there
we go. We are back at the exact same
layout. So great job if you manage to
solve this. Now there's another scenario
I want to teach you about as well which
is if we have top and bottom as one
value and right and left as another
value. If so you can actually reduce it
to margin and then only two values. Here
you do 10px and auto because 10px is top
and bottom. Auto is left and right. So
the way to think about this is that the
first value controls the vertical line,
the yaxis, and the second value controls
the horizontal line, also known as the
x-axis. So with that in mind, let me
change this a little bit. I'm going to
go back to the funky way of writing
this. And I'm actually going to improve
it a little bit because if we want to
center the circle, we shouldn't hardcode
a margin left because that'll only work
on this screen size. When we do like
this, it's not centered anymore. So
let's instead use the better technique
of using auto on the left hand side and
the right hand side. And also just to
make sure that no elements are allowed
to appear directly underneath our
circle, I want to add 40 pixels to the
margin bottom. So now I'll make it a
little bit easier for you and separate
these two. With this in mind, can you
reduce these four lines into two? Go
ahead and give that a shot right now.
Okay, let's do this. I'll comment out
these. And if you wondered what shortcut
I used for that, I'm using command + K +
C, but that is on a Mac keyboard. I'll
comment this out. Then I'll do margin
first. We'll set the vertical axis. I
should have 40 pixels at the top and the
bottom. So that is 40 px. And then we'll
set the horizontal axis, which is auto
like that. And boom, there we go. We
have it centered. at least horizontally
regardless of how wide the screen is and
we have reduced four lines into one. So
great job. Let's move on.
Okay. Now you're going to take what
you've learned about the margin
shortorthhand and use that in order to
reduce these two lines into one. So
you're going to replace the two margin
properties with a single margin
shortand. And of course after you've
done that the card should still be
centered as it is now. So go ahead and
give this one your best shot.
Okay, let's do this. So I'm going to
start by removing both of these. Now you
can see the card is aligned on the left
side. So in order to fix this, we'll do
margin. And then the first property
we'll set will be the vertical line,
meaning margin top and margin bottom
because we're only going to set two
values. And when we do that, each of the
value dictates two sides either top
bottom or left right. So we'll set zero
as the first value. That means margin
top zero and margin bottom zero. And
then for the second value which dictates
the horizontal margin meaning left hand
side and right hand side we'll set of
course auto. And there you see now the
card is still centered and we have
reduced two lines into one. So great job
solving this challenge.
You have reached the final step of the
business card project. That is
phenomenally well done. Give yourself a
pat on the back. And now only the fun
stuff remains because now you're going
to personalize this card. So first I
want you to add your own information to
this card. As for the image, you can
just drag and drop an image over the
file system here and then link to it
from the source. And once you've done
that, head over to the CSS because here
you're going to do a few cool
challenges. First off, I want you to
find a color palette you like on
coolers.co. And this is coolers. It's a
super neat site for finding nicel
lookinging colors and color
combinations. So what you can do is
simply click this explore trending
palettes. Then you'll come to this
overview which has a ton of different
palettes. And you can also search up in
the right corner here. Let's say you're
a big fan of roses. Well, then you can
write that and then boom, you'll find a
ton of pallets with rose looking colors.
And when you hover over one of the
colors, you'll see the hex value of that
color. And also, if you have an account
on coolers, you can click on these three
dots here and then choose save palette.
Then you can give it a name and save it.
When you then later visit your profile,
which you can do by clicking up here in
the top right corner, then you'll see
all of your saved pallets. So go ahead
and find a palette you like on coolers
and then change these values here so
that your business card becomes
completely unique. Also once that's done
I want you to find a web save font you
like and add it to your card as well
because you shouldn't use Verdana. You
should pick one of the other web fonts
and if you have to Google a little bit
to find the web save fonts well that is
just important practice as well. And
then if you want to go above and beyond
go crazy I've created some stretch goals
for you here. I want you to simply find
other ways to personalize the design of
your business card. For example, you can
change the border or borders. You can
add a border radius. You can also
shuffle the layout like should the image
be here or maybe centered at the top or
at the right hand side or just in a
corner. I don't know. And if you want to
do something mind-blowing, you can add
shadows. For example, I've not taught
you anything about shadows, so that's
far outside the scope of this course. Or
if you want to go even more crazy,
double mind blown, create some hover
effects so that when you hover with a
cursor over some of the elements, they
change. That is business card 2.0 in my
opinion. And if you want to go triple
mind blown, create some CSS animations.
Now, please don't stress about doing any
of these. It is not expected that you do
any of them. I just wanted to add it
there for those of you who can't help
going above and beyond. Now, once you're
done personalizing your business card, I
have one more task for you, and that is
to download the code to your local
computer and then place it in a folder
called business card because we're going
to use this folder when we deploy your
business card in the next scrim. So, go
ahead and do all of this and I will see
you when you're done.
Last time we deployed a project, we
first added our code to Netlfy, which
then again took care of deploying our
project to the web. Now, while this
works perfectly fine, the way the pros
do it is to go via an intermediate
service and step called GitHub and then
let GitHub take care of sending off the
code to Netlifi so that it's deployed to
the web. And the reason you want this
kind of architecture is because GitHub
allows you to collaborate on your code
with other developers. So now we are
going to deploy your business card using
this technique. The first thing you need
to do is head over to GitHub. If you're
watching this course on Scribba, you can
just click on this slide right here. But
if you're following us on YouTube, just
head over to github.com and you'll get
to the exact same page. Once you're
there, I want you to click at the sign
up button. That'll open up a form where
you have to add your email, create a
password, and also choose a username.
Once you've done all of that, click the
continue button, which then sends you to
this onboarding screen where you can add
a little bit of information about
yourself or just click on the skip
personalization option if you are
impatient. Once you've done this, you'll
see a dashboard. Yours is probably not
as filled with information as mine,
though that doesn't matter. What we're
going to do now is click on the plus
icon in the top right corner. There it
opens up a menu where you are to choose
new repository. The repository is the
backbone of any software project. This
is where you'll find all the project
files and GitHub along with the git
protocol takes care of tracking all the
changes that has been done to the
repository from its birth. Though we're
getting ahead of ourselves right now. We
are just going to give it a name which
you type in here in the repository name
field like that. I'm going to call it
business card. And then I'm not going to
touch any of the options here. I'm just
going to scroll down and click create
repository. And there we go. Now, we
have to upload our files. We can do that
in many different ways. Right now, we're
just going to do the very simplest one.
So, what I want you to do is click on
the uploading an existing file link
right here as that'll take you to a drag
and drop field quite similar to what we
saw at Netlefi earlier. Here, I want you
to open up the business card folder
which you created on your local computer
in the previous scrim. Mark all of the
files and then just drag and drop them
into the rectangle at GitHub. Once
you've done that, you'll see all of your
files listed. Just scroll a little bit
down and then we have to do something
called commit changes, which might seem
a little bit weird and complicated, but
don't worry about that right now. I'm
just going to go with the recommended
text, add files via upload, and then hit
commit changes. And boom, there we go.
We have a repository on GitHub which
includes all the code we need for our
business card. Now, if you were to
collaborate with other developers on
this project, they would push their code
up to this specific repository. And you
would also probably create issues for
tasks you want to do and work with
something called pull requests, which is
when you want to merge someone's changes
into the codebase. And there's also a
ton of other things you can do here, but
we're not going to worry about any of
that right now because the only thing we
want to use this repository for right
now is as an intermediate step before
our deployment. So let's head over to
Netlefi and make sure you're logged in
so that you see the previous project we
deployed. And right besides it, you can
see this button called add new site.
Click on it and choose import an
existing project. Here you're able to
choose GitHub. Click on that and it'll
open this pop-up menu where GitHub asks
where you want to install Netlefi
because that's what you're doing now.
We're installing Netlefi on our GitHub
profile. Click on your GitHub username.
In my case, Pier Borgan. And here I'm
just going to leave all the settings as
they are. Scroll down and hit install.
And there we go. Now you can see that
Netlefi has pulled in your GitHub
profile and has your repositories
listed. I have a bunch of repositories,
so I'll have to search for the business
card. I'll write business. And there we
can see it. When I click on this
repository, it is imported into Netlefi.
And here I can adjust some settings, but
I'm not going to do any of that as
usual. Just scroll down to the bottom
and hit deploy site. And there we go.
The deployment is now in progress. So
we'll have to wait a little bit. And
then once this image turns into a
screenshot of your site, it is deployed.
So clicking on this link will open up
our business card. Really good job
though. I hope your business card looks
a lot cooler than this as you were to
personalize it in the previous scrim.
Actually, to prove to me that you did
personalize this, I want you to head
over to Twitter and share your deployed
business card. Do as Michael the Rocket
does here and mention me if you want to
alongside a link to your project and
perhaps even a screenshot of it. That
would be super cool. So, go ahead and do
that right now and then I'll see you in
the next scrimm.
Hi there, my name is Bob Zerroll and I'm
another teacher here at Scribba. You
just learned a little bit about how to
use GitHub to deploy your project to
Netlefi. I thought this might be a good
opportunity for us to dive a little bit
deeper into the differences between Git
and GitHub. This is going to be a little
bit more surface level just so that we
have a solid understanding of the
difference between the two. So, we're
not going to be really diving into the
weeds with either of these things quite
yet. To better understand them, let's
look at the differences between the two.
First, we'll start with Git. Git is
what's called a version control system.
We'll talk pretty soon about what
exactly that means, but it's important
to know that Git is something that runs
locally on your own machine. If your
machine didn't come with Git already
pre-installed, then it is something that
you would have to download and install
on your machine in order to use. And its
main task is to help us keep a history
of the changes that we've made to our
projects. In contrast, GitHub is an
online platform that stores something
called Git repositories. The term
repository is just kind of a fancy way
to say a folder of your code that you
have been tracking the changes of in
Git. In contrast with Git, which runs
locally on your own machine, GitHub runs
online in the cloud. And where Git's
main task is to keep track of a history
of the changes you've made to your
project, GitHub is a way for one to
share your code and that history of the
changes from Git with other people.
Maybe an imperfect but illustrative
analogy is Git is kind of like taking
photos on your phone or your camera,
which happens locally on your own
device. And GitHub is kind of like an
online photo sharing platform like
Instagram. It's not a perfect example,
but it does highlight some of the main
differences between Git and GitHub.
Okay, we talked about version control.
So, what exactly is version control? The
main thing that version control provides
for us is a way for us to create save
points, which in Git are called commits
of your code so that you can revert back
to them if needed. These save points or
commits aren't something that actually
alters the content of your code itself.
This information is saved in kind of a
separate hidden folder inside of your
project that is using git, but it
provides you a way that if let's say you
were to write some kind of bug into your
code or accidentally delete everything
and then close your editor that you
couldn't just undo those changes with a
git commit, we can revert back to that
commit and have everything restored the
way that it was at that save point. This
reminds me of the most difficult level
of any video game I've ever played in my
life. If you know, you know. And I
probably would have enjoyed this game a
lot more if I had the ability to create
safe points along the way that I could
go to every time I died or failed.
Another major benefit of version control
is that it manages changes made by other
people that are also working on the same
codebase by giving us a means to merge
multiple different versions of the same
codebase together and fix any of the
resulting conflicts. If you've ever
worked on a group paper and well, you're
old enough, you might be familiar with
this phenomenon. As different people are
working on their own parts of the paper,
you would end up emailing the paper back
and forth and you had to come up with
unique names. And then you'd have to
have someone in the group who was an
editor and could take all of these
different versions and merge them
together into one version that became
the final paper the group would turn in.
In the end, it just becomes a huge pain.
And this is something that version
control helps us mitigate. Now, this
isn't to say that you won't have two
people that make, let's say, a change to
the same line of code, but in different
ways, but rather git as a version
control system gives us a way to
highlight those changes and decide which
one of those changes we want to include
in the final codebase. In the end,
version control generally allows us to
be less worried about making any
mistakes or having our code conflict
with other people that are working on
our same project. Okay, so this is
version control with Git. Let's now
switch over and start talking about
GitHub. GitHub is a cloud-based SAS
product. As a SAS product, a few years
ago, it was purchased by Microsoft. And
its main feature is that it gives us a
way to share our code and the changes
that were made to our code with those
commits in Git with other people. As the
name would imply, it assumes that we're
using Git as our version control system.
As a side note, there are other version
control systems, but you'll see Git used
far far more commonly than any other
version control system out there. As a
feature of GitHub, they have integrated
a number of Git-based capabilities right
into the website. Things like
committing, merging, branching, and so
forth. You don't need to know what those
operations are per se, but it does
contribute a little bit to the blurring
of lines between Git, the version
control system, and GitHub, the
cloud-based SAS product, which contains
Git repositories. Personally, I think
it's really important for beginners to
become familiar with and start using Git
and GitHub from an early point in their
learning. But I do think there is a more
approachable way to do this and that's
by using a product from GitHub called
GitHub Desktop. This is a native desktop
application that you can download from
the GitHub website and it allows you to
perform git operations on Git
repositories on your local machine and
in a pretty seamless way push those
changes up to GitHub where they can be
shared with other people. If you're
interested in learning more now about
GitHub Desktop, you can click the
screenshot you see here. That will take
you to another scrim that's in a future
module here on Scribba, which is taught
by Treasure. Using GitHub Desktop might
not be as hardcore as a senior developer
who's using Git from the command line.
But with how powerful and therefore
complex Git is, the learning curve can
be a little bit steep. And I think
GitHub Desktop is a great approachable
way to begin using and understanding
Git. Either way, congratulations on
deploying your business card. That
really is a huge feat and as P said,
it's something that should be shared
with the world. All right, that's it
from me for now, but I'm sure we'll run
into each other in another lesson here
on Scribba. Bye for now.
No matter how much you ended up
personalizing your business card, I want
to say well done. You reached the end of
this section, so give yourself a pat on
the back. You deserve it. And let's now
have a look at what you learned at the
beginning of this section. You learned
about alt text on images, which is
really important for accessibility. And
then you learned about child containers
in flexbox because we created this card
as a flexbox container and made sure
that it only had two direct childrens.
So that these two got their own columns
and then inside of the second column you
could add things on rows again, which is
what we did here. That concept of only
the direct children being the flex items
and then turning these children into
containers again is a critical thing to
understand in order to create
sophisticated flexbox layouts. Moving
on, you learned more about inheritance.
This is something you had been exposed
to before. But hopefully you have a more
conscious relationship to this right now
as you understand how setting the text
align on the card div that is this one
right here makes that property applied
to these elements as well as they
inherited the text align center from the
grandparent that is the div up here. You
also learned about shorthands and more
specifically the margin and padding
shorthands. Here we're setting the
margin to zero at the top and bottom and
auto at left rightight. And it is a lot
more compact than writing out margin
top, left, right, and bottom all on
their own lines. Another thing you
learned about was web safe fonts, which
we used up here in the body where we
used Verdonna, which is one of those
fonts you can use without worrying about
whether or not it's installed on your
users computers because most likely it
is. And then a really important concept
in web design is color palettes. We took
a look at that and hopefully you found
your own palette so that you
personalized your business card because
colors really do make or break a design.
So that was quite a lot of concepts. You
should be really proud of yourself
reaching this far. Now it's time for you
to take a break so that these concepts
mature in your mind and do whatever you
need to recharge your batteries and then
I'll see you back here in the next
section.
Welcome to the section where you are
going to build a space exploration site.
Here is the example you're going to
build. As you can see, it has this super
neat background image that animates and
that really breathes life into the site.
We're also using Google Fonts to pull in
this custom spaceish font right here.
And we're doing a lot of interesting
things with some subtle text shadows,
this underline, and more. So, there will
be plenty of new things to learn in
addition to reinforcing HTML and CSS
concepts you've previously learned. So,
let's launch into this section.
Okay, let's get started building our
space exploration site. As you can see,
I've set up a basic HTML structure for
you here. It is very simple as the body
simply contains an H1 title and a
button. As for the CSS, there is no as
we are going to add that now. So, the
first thing we're going to do is fix the
background here because we don't want
the white background. We want to have an
image of the universe in the background.
Later down the line, we're going to
change that to an animated image so that
it looks like a video playing in the
background, which is even cooler. But,
as for now, we're just going to do a
standard background image as we're going
to work on this for a while and I don't
want to have an animated image looping
in the background as we work as I think
that's a little bit annoying. So, let's
start out with just a static background
image. And just so you know, background
images is something different than how
you've used the images up until now
where you've done img, src, some kind of
JPEG like that. Instead, background
images are not in the HTML. They are
added in the CSS file. Enough talk about
that. Let's just find a cool background
image and then deal with how to
implement it into our app. And the
service I prefer to use for finding cool
images for your web design is Unsplash
because the quality of images here are
really good. So they will take your web
design to new heights and you can use
them freely however you want. So if I
search here for universe, we'll get a
ton of nice images. Let's scroll a bit
down. As you can see, there's plenty of
good candidates here, but I think this
one is particularly nice. So I'm going
to click in on it. It is created by
Gilmo Furla and you can see it's really
popular have over 12 million views
already. Now if we want to download this
you can click on the icon up here and
download it range of different sizes and
I've actually done that and placed it
here in the images folder. So here you
can see universe.jpeg.
So let's see how we can add this to our
app. What I'm going to do is target the
body because we want the image to span
across the entirety of our page. So we
need to add the background image to the
body. We'll use the background image
property. And here we'll utilize
something called a CSS function. Don't
worry about why it's called a CSS
function. Just remember that the way to
do it is by writing URL open parenthesis
and then inside of the parenthesis you
refer to the image source just as you do
in the image with the img tag. It is
using quotes. So inside of the quotes,
we'll write the path to this exact
image. And the way we do that is first
by navigating to the images folder. So
images then slash going into the folder
choosing the universe
JPEG. And boom, there we go. Suddenly
our background changed. However, there's
a couple of problems here. Our H1 is now
almost invisible since by default the
text is black and the background is very
black as well. So I'm going to change
the color to white. Like that. Much
better. Also, this image is thousands of
pixels wide, whereas our browser is not
that wide. So, notice that if we make it
super wide, we still only see the edge
of the galaxy more or less the center of
the image. So, what we need to do is
shrink our browser down to like a couple
of hundred pixels wide, which is what we
want. And then make our background image
shrink so that it's squeezed into the
width of this browser. The way we do
that is background size
and the value cover. Boom. There we go.
Now the width of the background image is
exactly as the width of its container
which again is exactly as wide as the
browser itself. Meaning that the browser
and the background image has the exact
same width. And it looks pretty good.
I'm not going to give you a challenge on
how to do this right now as you'll get
plenty of practice using background
images later in this course. So, with
that, let's just move on to the next
scrim.
Okay. In this two-part challenge, you're
going to improve the design of our app.
First, I want you to center the text and
button and then style the button
according to the provided design. And
that is found here in the slide. As you
can see, the button is slightly
different from how it is right now. Not
too different, but certainly it has some
CSS properties applied. And I'm not
going to tell you which because this is
a good way for you to train your design
eye. When you design the button, I want
you to use a class in order to select
it. So, not just using an element
selector like we do here. So, go ahead
and give this a shot and then I will
show you the solution as well when you
return back to me.
Okay, let's do this. First, we got to
center the text on the button. And here
you can use a range of different
options, either using margins or flexbox
or text align. You've learned plenty of
ways to center elements. Now I'm going
to choose the easiest one which is to
apply a text align center in the body
element because as you know this centers
not only the H1 but also buttons. Okay,
with that out of the way, let's move on
to the button. First I want to give it a
class and I think btn is a good class
name. So let's add it to the HTML as
well like that and run the code. So we
have it applied in the DOM. Now let's
have a look at this design. The first
thing that is apparent to me is that
this button has more space around the
text. As you can see here, it's a bit
more crammed. So that is the padding
property.
Let's try 10 pixels just on all four
sides of the padding. Having a look at
this button right now, you can see that
this one is a bit more rectangular
whereas this one is a bit more squared.
Now, cuz in this design here, there's
more space on the left and right hand
side than the top and bottom. And if you
remember the short hand, we can do this
by, for example, starting with five
pixels on the top and bottom and 10
pixels on the left and right. And that
looks much better. Maybe it's a little
bit higher. So, let's do six and 12.
That looks pretty good to me. Now, the
next thing I want to change is the
background color. It's not totally
apparent, but actually, this one has a
white background color, whereas this one
has the default button background color,
which is a bit grayer. It's not that
easy to see because of the background
here. It looks pretty white compared to
the black background, but it is actually
not entirely white. something you'd know
if you've worked with buttons in HTML
for a while as they always have a
default grayish background color
background white like that. Okay, now it
suddenly became apparent that it also
has a border in this default styling
whereas that's not the case in our
provided design. So let's do border. We
can do none like that. And with that, it
looks like we have completed our design
as these two look identical as far as I
can see. So, really good job. Let's move
on.
Okay. Now, you're going to get a new
superpower in your tool belt called
Google Fonts. And that is a service that
gives you access to over a thousand
different highquality fonts that you can
use in your projects. And the reason you
need to learn about this now is because
the font we have here isn't good enough
in my opinion. It is a little bit boring
for a space project. What we want
instead is something like this, which
looks a lot more sci-fiish. However,
this font isn't one of the so-called
websafe fonts you learned about earlier,
which is already installed in most
operating systems. This is a very custom
font. So, we have to use Google fonts in
order to bring it into our project. And
here you can see the Google fonts
interface. It's located at
fonts.google.com.
And if we scroll a bit down, you can see
it contains a range of very different
fonts. Now, it has a search feature up
in the top left corner, but it's
actually not that good to be honest.
It's better to just use regular Google
search when searching for Google fonts.
So, let's do that instead. The way I
found the font we're going to use now is
just by googling sci-fi and then Google
font. And then Google suggested this
Orbitron font. So if we click into it,
you can see here it is. If we scroll a
bit down, you can see we have to choose
specifically which styles we want. So
I'm going to choose this 400 regular
style by clicking on select this style
like that. And you can see the panel on
the right hand side. And it is clear
that we have chosen the orbitron font
and the regular 400 font weight. Also,
let's click on this extra bold in case
we want to bold some text on our site.
And there we have that one as well. Now,
if we want to use this on the web, we
can embed it by simply copying a piece
of code into the head of our HTML. And
that is the code you want to copy and
then paste it in exactly here. Next up,
we also have to specify it in our CSS.
That's done by pasting this line into
our CSS, for example, in the body tag.
I'm not going to do this for you,
though. Scribba is all about you doing
things on your own, getting your hands
on the keyboard. So, now I want you to
head over to Google Fonts, fetch the
Orbitron font in these two styles, and
then do whatever you need to do in order
to make the Orbitron font render
properly on our site. I'm going to leave
you here with this screenshot because
make sure to compare this design with
what you get in the mini browser because
there might be something you need to do
down in the btn selector. And hopefully
you're able to spot that. If not, no
worries cuz I'll be here showing you the
solution when you return back to me.
Good luck.
Okay, let's do this. So, first we're
going to take this snippet of code right
here and then paste it into our HTML
like that. Then need to run the code so
that it is imported into our project.
And then next up, it is the styles which
we're going to set here. Let's copy that
line of code. And then simply paste it
in right here. And boom. Join the
exploration now looks like our project
right here. However, this one also has
the Orbitron font applied to the button,
but we don't have that. We still use
that boring old font that we had
previously. Why is that? Well, that's
actually because buttons don't inherit
font families. So, here the H1 has
inherited the font property from body,
but the button has not. So, what we need
to do here is specifically tell the
button to have this font family. And we
can do that in two ways. One way is
through using a CSS value you haven't
learned yet, and that is the inherit
keyword. As you can see, now it does
inherit from the body, but only because
we've specifically told the button to do
exactly that. What I'm assuming you did
instead, which works perfectly fine as
well, is simply to paste in the font
family orbitron on the btn class. As you
can see, that also works well. So,
hopefully you were able to spot this. If
not, no worries. It was just a little
gotcha I left here. Let's move on to the
next scrim.
Before we move on from the subject of
fonts, there's one more technique I want
to teach you. Because in some cases,
Google fonts might actually not be
enough. And that's typically the case if
you want very specific fonts. For
example, let's say that you are building
a Godfather website in honor of the
classic film. Well, in that case, you'd
probably want to get a hold of the real
Godfather font. though Google Fonts
doesn't host that one because it is
simply too specific for them to care
about. So you'd most likely have to find
this font somewhere on the worldwide
web. And there are great services for
this. For example, a 101font.com
there. If you search for Godfather, they
actually have it. So you can click into
it, scroll a little bit down and click
download. So what you then did is kind
of downloaded the recipe for the font.
And if you open up the folder you got,
you'll see a TTF file. That's a file
format for fonts. What you then can do
is simply drag and drop that file into
your file system like I have done right
here. You can see I have the
cordoneion.ttf
file. Now what I can do is use the at
font face rule in CSS. So simply writing
an at and then font-face and then open
up the curly brackets and write two
properties. The source which should use
the URL technique pointing to the TTF
file which I'm doing right here. And
also I need to tell CSS what name I want
to give this font in my stylesheets. And
I'm just going to choose Corleona for
now. But I could have chosen whatever
name I wanted. Now I'm able to target
the H1 and use this font family. So I'll
do font family
or Leona. And there you can see I have
the Godfather font. So I'm off to a
great start on my tribute page. So now
what I think you should do is actually
try and do this yourself. Find a random
font online, download it so that you get
a hold of the TTF file, drag and drop
that TTF file into the file system here
on Scribba and use the add font face
property to make that font available in
your stylesheets. Once you've done that,
I will see you in the next scrim.
Now you are going to learn about the
span tag because in the next scrim you
are going to create an underline under
the exploration word and in order to
achieve that you need the span tag. So
let's take a little step back from our
space exploration site and learn all
about this awesome tag. The example
we're going to use in order to learn the
span tag is of banner ads. You've
probably seen these before. You've
browsed the web, for example, on news
sites. And many of these are actually
so-called HTML 5 ads where they are
built using HTML and CSS. And it's
actually a little bit sad how many
clever developers in this world who are
spending all of their time in their best
years and getting more people to click
on these ads through making them more
invasive, more eye-catching, and more
targeted. But that's the name of the
game. So, we just have to accept it. And
you are now going to do exactly that.
you are going to make this ad more
eye-catching so that hopefully more
people click on it and visit our scammy
casino. So, how do we do this? Well,
looking at the HTML, you can see the div
wrapper has a class of ad which sets the
background color, border, and the text
alignment inside of this green
rectangle. And there are two elements
inside of the ad. It is the H1 and the
H3. And it says click here. So, what we
want to do is make this hair word pop
out more. So how do we do that? We can't
target the H3 itself because that'll
style the entire sentence. We only want
to style this word. And this is where
spans come in. Here we can write span
like that. Open the tag before the word
and close it directly afterward. If we
now run this code, seemingly nothing has
happened because spans are invisible
unless you give them styling. So they're
kind of like the div as divs neither
provide any styling unless you give it
to it specifically. Although the
difference is that spans are in line
whereas divs are block based. So to
demonstrate that if we change this to a
div. What would happen if we run the
code is that it would break the hairword
onto a new line because divs are display
block. They take up all the available
space in the width. Where spans do not
do that. Only take up the space they
need in order to make room for this
content. What we now can do is give this
span a class. I'm going to call it CTA.
That's short for call to action as
that's normally what these kinds of
elements are called. And in the CSS,
we'll do CTA. Let's just see that it
works by doing color red. Yes, there we
can see we have now only targeted the
here word. This doesn't look
particularly good though. So, let's
instead do something that makes it look
a bit more like a button. For that, we'd
need a border in my opinion. For
example, 2 pixels. And we want it to be
solid. And here, let's steal the color
from the border around the ad so that we
reuse it a little bit. I think that can
look nice. Yeah. Okay. Here you can see
that it it's added a border around the
word. It is very crammed on the left and
right hand side. So, let's try to add
some padding. Let's just do five pixels
or something. Actually, I think we can
do five pixels on the top and 10 pixels
on the left and right. Yeah, that looks
much better. and then to make it pop out
even more so that it doesn't blend into
the overall background we have on the ad
itself. So I'm going to do background.
Now I happen to have a nice brown color
here which I think looks decent with the
other colors here. So now it is a lot
more easier to see that you are going to
click here on this thing that looks like
a button as it stands out more. And
hopefully we have increased the
click-through rate with 0.02% 02% and
thus made our boss really happy. So,
great job. Now that you've learned this,
let's return back to the space
exploration site and put your new skills
to the test.
Okay, now you're going to create this
neat little underline under the
expiration word. And I want you to do
that using a span and a class. as for
the specific design of the underline and
what kind of CSS property you are to
use, that is something you'll have to
figure out on your own. So, go ahead and
give this a shot. Good luck and I'll see
you on the other side.
Okay, hopefully that went well. Let's do
it. I'm going to start with adding this
span to the HTML. It should wrap the
exploration word. So, span like that.
And then I'm going to give it a class.
I'm going to call this class underline
because it'll only have one job and that
is to give the expiration word the
underline. So this class is something
we've talked about earlier. It's a
utility class. It only has one job and
that is setting a single CSS property.
So if we run the code now, nothing has
seemingly happened. But we do have this
in our DOM, meaning we can select it in
the CSS. We'll do dot underline like
that. Let's just try and set the color
to red to see that it works. And yes, it
does indeed work. But that's not the
effect we want. We want to set the
underline. And what is this underline?
Well, it's actually just a border
bottom. We do border bottom like that. 2
pixels solid
white. You can see yes, we indeed get a
border under it. However, this border
looks a little bit thicker. So, I don't
think 2 pixels is the correct thickness.
I think we want to have something like
four. Yes, that looks pretty good to me.
So, we have solved this challenge and we
are ready to move on.
Okay, now we are going to add this
SpaceX logo at the top here. And while
you're adding it, you're going to learn
a whole new concept in HTML and CSS,
namely a close cousin of the class
attribute, which is called the ID
attribute. And the way to use it is
simply by writing ID instead of class.
But now you're probably wondering why
would we use this ID? If we now try to
rerun this code, you can see that our
underline is gone. And that is because
when you are using an ID like this, you
have to refer to it in the CSS not by
using a dot but instead by using a
hashtag. There you can see now our
border bottom is again applied to our
example. So now you're probably
wondering, well, why would we want to
use this? It just further complicates
things by having both ids and classes to
worry about. And the thing is they serve
two different scenarios. You should use
an ID if you know that your element is
unique and you'll only use it once. So
ID of underline should only be be
applied once in this entire document.
Whereas class is something you can reuse
again and again. It's meant for reusage.
So if you for example wanted a button
here that also said for example FAQ's
then a class is what you use in order to
make sure that this button has the same
styling as this button. But if you did
that with IDs and turned the btn here
into an ID, this would be invalid HTML.
Browsers are very forgiving. So it
probably would work technically. The
styling would be applied but you just
shouldn't do it. Now, as for this idea
of underline here, I actually don't
think it makes sense to use an ID
because this underline here could
perfectly fine be a part of our design
system and also be applied at multiple
other sections and paragraphs throughout
our entire page as we build out this
space exploration site. So, I want to
have the freedom to be able to reuse
this later as well, even though I'm only
using it once now. So, I'm going to turn
this into a class again. Rerun the
example. Now, it's not applied.
and then go from the ID selector to the
class selector in CSS by replacing the
hashtag with a dot. And there it is
applied.
So that was a long intro. The reason for
that was that I want you to render this
SpaceX logo. You can see I've added the
SpaceX.png
image file up here. And then I want you
to give it ID because this SpaceX logo
at the top here is most likely unique.
We probably don't want to have multiple
centered SpaceX logos at this size. So
this is kind of our main logo and you
can give it that ID main logo.
Then in the CSS you need to select the
logo using whatever ID you have given
it. Then as you'll see you'll have to
reduce its size. That's why you need
this CSS rule right here because it is
far too big this image. Finally I'm
going to remove this FAQ button so that
we're back to our original design. Now
it's your turn. Take over and try to
solve this challenge.
Okay, hopefully that went well. Let's do
it. We're going to render it using an
img tag and the source will be first
images and then slash
spacex.png
like that. If we run this, you can see,
oh wow,
that is not exactly what we were looking
for. It's far too big this image. So we
have to reduce its size. And to target
it, we're going to use an ID. And I'm
going to call this main logo like that.
Now if we head to the styles.css,
we'll do hashtag main- logo. And here we
have to reduce its width. So let's try
with for example
h perhaps this is 200 pixels. I don't
know. Let's try 200 px like that. That
was a bit too large in my opinion. So,
let's reduce it further to 100 pixels.
And yes, that is much more like our
design in my opinion. Looking pretty
good. So, with that, we have solved the
challenge. Great job.
Now, we're going to breathe some more
life into our example here because it
works fine with this static JPEG as the
background image, but wouldn't it be a
lot cooler if it was animated? So, you
could see the stars flying around. And
in our images folder, we now just happen
to have a file called galaxy.webp.
Hm. What's that? Let's just try to
replace this universe JPEG here with the
galaxy webp file.
So I'll do galaxy.
Webp and boom. The galaxy webp is an
animated image in which you see a bunch
of stars around the galaxy. And to me
this looks a lot cooler than what we
just had.
Now you might wonder what this web p
here is. And that is just an image
format pretty similar to so-called GIFs,
GIF. And GIF is what you recognize from
all over the web in animated images like
this one right here, which you've
probably seen before. So WEBP is used
exactly as GIFs are on the web. However,
it has a better compression. So it's
more compact and doesn't use as much
data as GIFs use when they're sent from
a server to a client. So we want to use
WEBP where we can. And now you might
wonder, well, where did you find this
galaxy.p image? That is on a site called
Gy. Giphy. That is a search engine for
GIFs. So, I just navigated to it,
searched for galaxy, scrolled a little
bit down, and found this neat looking
galaxy GIF here, created by the European
Space Agency. Now, this is a slideshow,
so you can't see that this GIF is now
being animated, but it actually is
because I just saved this image to my
local computer and then uploaded it to
the images folder right here. Now, if
you want, you can also click on the
share button here and click copy GIF
link. And then you can simply paste that
link into this URL. And then that'll
give, as you see, the exact same result.
But I prefer to rather use the version
which we've imported into our project
right here in case the gy file might
change at some point or something.
However, for experimentation purposes, I
think you should head over to GY, search
for a few images, and just paste them in
right here and see how that affects our
example. Just so you've got familiar
with using gy and pasting external URLs
into the background image property in
CSS. So, go ahead and do that and then I
will see you in the next scrim.
One thing I like to do when working with
images as backgrounds is to fetch colors
from the image and use it in the design
because that can often look pretty cool.
So, let me show you exactly how to do
that. We can use coolers, the service
you're already familiar with because if
you click on the more option in the
navbar, you can choose pick plet from
photo. That'll take you to this image
picker where you can choose an image by
clicking on the browse an image and
either upload the image or paste a URL
to it. Once you've done that, you're
taken to this nice tool where you can
either drag and drop this picker around
on the image until you find a color you
like or you can use this range input
here as that'll automatically try to
find interesting colors in the image for
you. And then it generates a palette as
you can see right here. Now let's say we
like this pette. Then we'll click export
and save PLET. And we have it in our
dashboard. Now, it only has three
colors, but that doesn't matter. I'm
only going to use one of these. That is
the blue one cuz I think it would be
cool to use this blue color as the text
color on our site. So, I want the H1 and
the button text to have that bluish
color. And the way I'm going to do that
is change the color of the body here
because that is where the H1 gets its
color from. It is the only element
inheriting from the body at this point.
So I'm going to do like that. This is
the hexodimal value I got from coolers.
And as you can see, I think it looks
pretty cool. And also let's keep the
underline white because I think it makes
sense to have that in a different color.
Now moving on to the button. We'll do
color
and paste it in again. And there we go.
Now here I think actually this apply
text could be a bit clearer because the
contrast with the background is just
okay at this point because with this
blue color I think the thin letters
became a bit too thin. Now a final thing
I want to do with this apply text here
is make it a little bit bolder make it
stand out a bit more. And luckily we
imported not only the regular font
weight with the Orbitron font family but
also the 800 font weight the extra bold.
So what we can do now is actually do
font weight
and then just bold. And there you can
see it actually bolded the text. Or we
could use the value directly which is
800. that'll give us the exact same
result as the font weight property can
both take numerical values and named
values. So the numerical values range
from 100, 200, 300, 400 and so forth up
until 900. And the named values are for
example normal, bold and so on. So we
imported 800. So let's just write 800
here. With that I think our example
looks pretty good. So let's move on.
Going to space is definitely a risky
affair. So, I want to add this terms and
conditions apply text underneath the
main section of our site. However, I
don't want to add this. This is Scrimba,
so you're going to do it, of course.
But, I'm going to give you a set of
detailed instructions so that you know
how to go about doing this. So, you're
going to start with wrapping the hero
section in a div. And the hero section
is the one you can see on top here. It's
very often called a hero section in web
design and it's probably called that
because kind of serves as the user's
first glimpse of the company and the
offering and has this kind of prominent
place towards the top and it usually
extends full width. So it is kind of the
hero of the website. So in our case it
is these three elements right here the
logo title and the call to action button
and those should all be wrapped inside
of a div. And then I want you to move
the background image that is the
animated galaxy image from the body and
over into this div. Now as you do that
you'll notice that you'll have to add
some padding to this hero div as it'll
look really crammed if you don't add
that. Then I want you to add the terms
and conditions apply in an H3 below the
hero section. And then finally, a
problem you'll probably come across is
that the body tag has some default
margins, which right now doesn't bother
us, but which will become apparent once
you change this layout. So, go ahead and
give this one your best shot, and when
you return back to me, I'll show you how
to do it as well.
Okay, let's go. I'm going to start with
the hero section div. I'm going to add
it right here. I'll give it an ID of
hero because it is unique. So it's okay
to use ID
and then I'm going to indent this in and
run the code. And seemingly nothing has
happened because as you remember from
previously divs don't have any default
styling to them. So what we need to do
is target the hero and then move the
background properties from the body to
the hero. And there we go. And as you
can see, it doesn't look particularly
good. Now, as I said, we will be forced
to deal with the padding in the hero
because you can see here the logo is
really crammed all the way up to the top
and there's no space underneath the
button. So, let's add padding. And on
the top, maybe we want 10 pixels like
that. Yes. On the right hand side, we
don't need anything for now. But on the
bottom, we want a lot of padding. Maybe
we'll do 40 pixels like that. Then on
the left hand side, don't need anything
either. So here we have the padding.
Now let's add the terms and conditions.
Apply H3 below the hero. That's just
like this. Adding an H3. Terms and
conditions. Apply like that. Run the
code. And there we go. It has already
the color we want since we have set the
blue color as the text color on the
body. So, it inherits the color property
from the body. However, as you can see,
are still not quite done because
something has happened with the margins
on the site or or at least the margins
of the body has become more apparent
because now the entire body isn't filled
with the background image anymore. So,
we're suddenly noticing that the body
has some default margins on all sides.
We don't want that. So, we're going to
neutralize that by doing margin zero.
And boom, there we go. We have completed
the challenge. Now there is one more
thing which is a little bit problematic
which is that if you watch this on a
mobile for example so that the
exploration word is located directly
over the galaxy the readability sinks
quite a bit because the text color
blends in with the background and the
way we'll fix that is by using a shadow
technique on this text. So now you are
going to learn exactly that.
Text shadow is a really nice tool to
have in your web design toolkit as it
can make your designs much better. So
here I've created a very simple
imitation of the Netflix logo with the
red background and white text. As you
can see, I've pulled in a font which
kind of looks like the Netflix font.
It's not really the same, but it's close
enough. And it is an H1, and I've set
the font size to 80 pixels. Now, what if
I want to have a black shadow like this?
as that makes it look even more like the
classical Netflix logo. I can achieve
that by on the H1 which is the element
that includes the Netflix text as you
can see here. I can add a property
called text dash shadow which must
contain three values. So just bear with
me here as I'll write all three of them
out and then I'll explain it. I'll do 0
pixels 0 pixels black and seemingly
nothing happened. That's because I first
wanted to show you the third value here,
black. That's pretty simple. That's just
the color of the shadow. And as you can
see here, it is black. The two other
values dictate the offset of the shadow
on the horizontal line and the vertical
line. So if I add, for example, 5px
here, you can see that now the shadow
appeared. It's as if it's sticking out
to the right of the letters 5 pixels. If
I do the opposite, minus 5 pixels, it'll
stick out five pixels on the left hand
side. As you can see, there's no shadow
below and above because that's dictated
by the second value. So, if I turn this
back to five and on the vertical one, do
for example 30, then you can see that
the shadow got pushed far too much
downwards. If I do minus 30, it's pushed
upwards. If I do five pixels, it's
pushed only five pixels downwards, which
looks pretty good actually. It is indeed
the design we were looking for. So
summarize here, the first value is how
many pixels you want to push the shadow
to the right. The second value is how
many pixels you want to push the shadow
downwards. And the third is the color.
So what I want you to do now is try to
make the shadow look like this. As you
can see, now it's on the left hand side
and below like as if the sun is shining
from the top right corner. So take a
guess at how you need to change this
line 11 here in order to get that
effect. Go ahead and do that right now.
Okay, hopefully that went well. What you
need to do is basically put a minus in
front of the first value like that. That
shifted the shadow from being on the
right hand side of the letters to the
left hand side. Okay, changing it back
again. Now I want to teach you about a
fourth value you can add to the text
shadow property and that is the third
pixel value. So here you can also write
for example 5 px. And what happened now?
If we compare what we have here right
now with how it was before we added that
value, you can see that previously the
edges of the shadow were sharp. There
was no blur. Whereas here, they're kind
of fading over into the red color behind
it. And this is called blur. And if we
reduce this one to zero again, the edge
is sharp again. Now, if we do something
crazy like 50, you can see that it is so
blurry that it just looks a little bit
like background is a little bit dirty.
So, let's get it back to zero again
here. What I want you to do now is try
to achieve this shadow. Go ahead and
give that a shot right now.
Okay, hopefully that went well. Let's
have a look. You can see the shadow is
above and to the right of the letters.
Let's fix that first. To have it on the
right of the letters, we need plus value
on the first one. We want to push it to
the right and we want to have it above.
We don't want to push it downwards as
we're doing with this value. So we want
to minus it pushing it upwards like
that. Now the only difference is that
this is a lot more blurry whereas this
is completely sharp. So let's try five
pixels here as well. And boom, there we
go. That looks pretty correct to me. So
great job doing this challenge. Before
we move on, I have just a final trick I
want to show you, which is a little bit
of a hack, but super useful because
let's say that, for example, the
background here was white smoke instead
of the red color. And white smoke is a
very white color with just a little bit
of gray in it. As you can see, it looks
really bad. Now, we can hardly see the
letters, but we can clearly see the
shadows. If we now set zero pixels on
the two first values, but keep the blur,
well, then suddenly there's no shadow,
but there's just a glowing effect around
the letters, making them visible, even
though they are in almost the same color
as the background. And if we make this
smaller, for example, one, you can see
the line around the letters look a
little bit sharper since it's very
little blur now. And if you do like 20,
each letter gets a lot of blur around it
and thus making them visible. So that is
a little trick to use normally with
small values on the blur in order to
make your text stand out a little bit
more in the situation where your text
color and your background color is a
little bit too similar. And this is very
relevant if you have a background image
in which the contrast between that image
and the text color is a little bit too
weak. So then you'd use this hack to
make your text stand out despite the
similarities between your text color and
the background image. So that was quite
a lot about text shadows, but now you
know it. So we are ready to move on to
the next scrim.
Okay, now we are going to fix this
problem of the text blending too much
into the background when it is broken
into two lines and thus are located
exactly over the galaxy in the
background image here because the
readability is a bit too low. Now, in my
opinion, I want you to fix that by
adding a blurred border around the H1
tag using the technique you just
learned. You should set the blur to four
pixels and the color to black. So go
ahead and do this right now. Then I will
show you the solution as well when you
return back to me.
Okay, let's do this. So first we need to
select the H1. And we've been really
good at using IDs and classes here. So
instead of just selecting the element,
I'm actually going to give this one an
ID of title because we probably only
have one title on the page. So running
this and navigating back into the CSS
file, I can select the title and then I
need to use the text shadow property. We
want both the horizontal offset and
vertical offset to be zero pixels. We're
not actually doing creating a shadow
here, the effect we want is more of an
outline. So then without any offset, but
four pixels blur, it'll blur a black
shadowish effect around the entirety of
all the characters. So if you look at it
now and look closely, there is a slight
blurred black border around the letters.
It's not that easy to see, but if you
now look at this while I comment out
that CSS rule, you'll see the
difference. So keep looking at the
exploration word and I will comment it
out now. Now I'll comment it back again
there.
Comment it out. Comment it back in
again. So, so hopefully you saw that it
changed here a little bit. So, now the
readability is a little bit better.
Great job.
Hey, it's Gil. I'm a teacher at Scribba.
Before we wrap up this section, I want
to drop in and teach you about a small
yet important part of your HTML
document, and that is setting the
document language using the HTML lang
attribute. And this is important for
several reasons. First, screen readers
use the lang attribute to determine
which language to use when reading the
content. And this is essential for users
who rely on assisted technologies.
Search engines also use the lang
attribute to understand the language of
your content. And it also ensures
consistency in language specific
features like date formats and specific
character sets. Setting the language is
quite straightforward. In your HTML
file, you'll use the lang attribute like
this in your opening HTML tag. And the
value of this attribute should be a
standard language code. For example, to
set the language of your document to
English, you would use en. And for
Spanish, it would be E S for espanol.
These language codes are standardized
and usually consist of two or three
letters. You can easily find the correct
code for your language with a quick
online search. Sometimes your page might
contain content in multiple language. In
that case, you'll set the primary
language in the HTML tag and use the
lang attribute on elements with
different languages. For example, in the
H3 tag, add a lang attribute and set it
to Spanish. Setting the language of your
page might seem like a small step, but
it does play a big role in accessibility
and helps your website reach the right
audience. To learn more about the ling
attribute and all the different language
codes, be sure to review the MDN docs
reference listed here.
Now, I'm not sure if you're aware of
this, but you're the best because you've
completed this section and that is very
well done. Plenty of people who have
started this section has never completed
it. You're not like them. So, I am
really proud of you. And let's take a
quick look at what you've learned.
Starting out, you learned about
background images. More specifically,
you learned about the background image
property and the URL function and also
the background size property in CSS and
setting that to cover. We also then used
a webp format in order to get this
animated image in our hero section,
which is super cool and really breathes
life into our website. And then you
learned about Google fonts. We imported
them up here in the head section and
then you set the font family like this.
Fonts do so much for the design of
websites. So, this is an important thing
to know as a web developer. And you also
learned a new HTML tag, which is the
span tag, which you can see right here.
It is what allowed us to create this
underline under the exploration word,
which it did in combination with a
utility class. The underline class,
which only has one job, and that is to
set the underline whatever element it's
applied to. And then also we learned
about ids which is another attribute in
HTML that you target in the CSS using a
hashtag. And at the very end we did a
really cool trick with the text shadow
property in order to make the
readability of our title here a little
bit better. So you've learned some key
concepts in this section. Now is the
time for you to get up, stretch, maybe
get a cup of coffee or whatever you
prefer to recharge because be sure to do
exactly that. And then I will see you in
the next section.
Welcome to this section where you are
going to build a birthday gift site. And
it is just so cool because this site is
something you can use for whenever a
friend or family member of yours has a
birthday. So what you do is basically
add their information to the header
here. And then we have these gifts. So
for example, this one saying here's how
happy I am for you today. And when the
birthday boy or girl hovers over the
image, they will see a nice gift that
makes them feel truly appreciated. Going
down, we have other gifts. How people
react when you enter the room. Ooh, this
is meant to make them feel really
good-looking. And here, if I had to
describe you with one word, genius, so
that you make them feel really smart.
Next up, the only person as badass as
you is, of course, James Bond. And then
rounding it up with a little toast. So
hopefully you have someone special to
send this site to like Nick Air for
example who despite being a
computerenerated image created by an AI
also is one of my best friends. So I am
looking forward to make Nick happy on
his birthday. Let's just get started
with this section.
Okay, it's time for your first
challenge. What you're going to do is
create this basic design here for the
header of our app. and it's pretty far
from what we got right now. As you can
see in the styles.css, we have no
styling at the moment. So, our site
looks pretty dull. You can see
everything is left aligned and the image
of Nick is far too large. Plus, we're
lacking the appropriate font. Taking a
look at the HTML here. It's just a very
simple structure with an H1, an image
tag, an H2, and an H4 inside of the
body. So, nothing special there. The
font that you will use is called Happy
Monkey and it is a Google font. So
you're going to have to head over to
Google Font, fetch it, and then embed it
into our project. As you can see in the
provided design, the image of Nick
should be rounded and not squared as it
is right now. So you will have to figure
out how to make an image round. And yet
again, you'll find the answer if you
Google it. Finally, I want you to
consider how you should select the
image. So, should you use a class
selector, an ID selector, or an element
selector? You can use any of them, but I
want you to think about it and try to
figure out what you think makes most
sense. Go ahead and do this right now,
and then I of course will show you the
solution when you return back to me.
Okay, hopefully that went well. Let's do
it together. I want to start with
centering everything. And for that, I'm
going to do
body and then set the text align to
center like that. As you can see, that
indeed centered our elements. Next up, I
want to fix the image. It is here. And I
think it makes sense to use an ID
selector here because this is kind of
the main image of our birthday boy. And
I don't think we'll duplicate it across
the page. So, it is unique, meaning we
can use an ID. So, I'll do ID equals and
then BFF for best friend forever because
Nick is my BFF. Please don't make fun of
me because my BFF is a computerenerated
image. But that is just how it is. And
then I'll do dash img just to make it a
little bit easier to understand what it
is based upon the ID name. Then I'll run
the code so that that is applied to the
DOM. And heading over to the styles,
I'll do #bfff-
img like that. And first I want to fix
the width of it. It's far too wide. This
looks more like 100 200 pixels. So,
let's try width of 150 pixels. Like
that. Yes, that is pretty much what
we're looking for. And hopefully you
figured out how to make an image round.
And the way to do it is to use the
border radius property, which you've
seen a couple of times by now. And the
maximum border radius an element can
have is 50%. And boom, there it is
completely round. All right. The final
part is to add the Google font, the
happy monkey font. That is a very
birthdayish font. Actually, I found it
just by googling Google fonts birthday.
Then happy monkey was the top result.
Clicking into that, we'll select it by
clicking select this style and then
we'll copy the links here so that we can
embed it into our project. I'll paste
that into the head of our project. Then
copy the font family rule so that I can
paste that directly in as well. Now I'll
have to rerun so that the font is
embedded. And yes, there we go. That is,
as far as I can see, exactly the design
we were looking for. So great job. Let's
move on.
Okay, your next challenge is to colorize
the app because I want it to look like
this with a nice blue background, pink
border, and white text. And as you can
see, I've provided the hexadesimal
values for the pink, blue, and white
colors here. So your job is simply to
figure out where you need to add these
and what kind of CSS properties you're
going to use. Go ahead and do this right
now.
Okay, hopefully that went well. Let's do
it together. I'm going to start here in
the body taking the blue hexodimal value
and setting it as the background of the
body. Boom. There we go. Next up, we
want to set the color property here to
white. Just six Fs after each other
like that. Now that is inherited down
from the body into all of these. Okay.
Final step is the image. As you can see,
it should have a border around Nick and
it should be pink. So here we'll use the
border property and let's try six pixels
because it looks pretty thick. It should
be solid. And then we'll copy the hex
value from pink and paste in right
there. And actually that looks about
right to me. So great job. Let's move
on.
There is one problem with our site at
the moment, which is that if we run
these two colors through a contrast
checker, for example, userway.org,
you'll see that our accessibility
actually fails because the contrast
ratio is too small. So, what can we do
to improve this? Given that we want to
keep the colors we have, what we can do
is use the trick you learned in the
space exploration section. Because pay
attention to what happens to this text
if we add a thin black border around the
text using the text shadow property. So
this is without the text shadow property
and this is with it. As you can see the
text now stands out more as the black
contrasts well with both the white and
the blue background. And it is also
pretty subtle. So the text still looks
white but it's just a little bit more
visible. And in my opinion I think the
readability here is pretty good. So what
I want you to do now is write the CSS
for this exact technique. So add a thin
blurred border around the H1, H2, and H4
elements using the text shadow technique
you learned in the space exploration
section. You're going to set the blur to
one pixel and the color to black. Okay,
hopefully you remember how to do that.
Give it your best shot and then I will
of course show you the solution as well
when you return back to me.
Okay, let's do this. So, let's start
with the H1 here. And here I'm just
going to use an element selector as
we'll have a lot of H2s and H4s
throughout the site. And I think we want
all of them to have this style. So,
there's no point separating it into a
class. So, text shadow
setting the first two values to 0 pixels
as we don't want any offset vertically
or horizontally. But the third value,
the blur should be set to one pixel. And
then the color is black. And boom, there
we go. We have that thin outline which
improves the readability. And now you
probably did something like this. Added
a whole new rule for the H2 and yet
another one for the H4.
And if you did, that is completely fine.
That is how I expected you to solve this
challenge. But I do want to show you a
little trick which allows us to reduce
all of these into one line. And that is
simply by using a group CSS selector
where you simply add multiple selectors
on one line. So what you do is instead
of just writing H1, you write H1,
H2. Now we can delete the H2 because
this rule is applied to both the H1 and
the H2. And likewise we can just add the
H4 here as well. Like that. Now we can
remove all of this. As you can see, the
thin border is applied to all of the
elements. Now, it is really important
that you actually use a comma here and
not just add it like this. This means
something entirely different and we're
not going to learn about that right now
as it's outside of the scope of this
section. So, make sure you have the
commas and then you can use this
technique. And I happen to know that we
also want this text shadow to be applied
to our H3 elements down the line and
also our paragraphs down the line. So,
I'm just going to add them here so that
we don't have to think about this as we
add new elements to our site later down
the line. So, great job. Let's move on.
Now, we want to take this pink
background that we have around the image
and apply it behind the H2 and the H4
element so that it looks like this. So,
let's try the most intuitive approach
and see how that works. That would be to
target the H2 and the H4. I'm going to
use this comma selector here to target
both simultaneously as we've done up
here. And then we'll set the background
to this pink color right here. However,
as you can see, that did not give us
what we wanted because heading elements
like H2 and H4 are by default display
block. So they take up all the available
horizontal space. So let's then try to
turn them into display inline and see
what happens then.
Ah, that did not work well either
because when they're in line, they're
actually narrow enough to be on the same
line and actually jump up to the line
above with the image, which also is
display inline. So, let's remove the
display inline as well. And the way to
actually solve this is to use a
technique you haven't learned yet. Now,
I'm just going to show it to you fast,
and then we're going to take a closer
look at how it works in the next script.
Because what we can do is head into the
body, which is the container element for
these two and for all of our elements
actually, and do bisplay flex. That
stacks things up on a horizontal line,
turning every element into its own
column. But we can change that by doing
flex direction, which now is set to row
by default. And that might sound a
little bit confusing since I've always
said that flexbox turns every element
into its own column. And yes, it does
that. However, what it also does is make
sure that all of the elements are in the
same row. So that is why it's called
flex direction row because rows go from
left to right. And you can see there's
only one row from left to right here,
which becomes even clearer if you do
like this. And all the elements are
inside of the same row. Now, in order to
flip this, we can simply change row from
column like that. Now all of the
elements are stacked on top of each
other again in one column because a
column goes from top to bottom. So this
is just one column. Now with that in
place, we can use another CSS property
in that flexbox gave us which is called
align as items and set that to center.
And boom, there we go. Now, this was
probably pretty confusing and you were
not meant to to understand this right
now, which is why we're going to take a
little digression here and look closer
at how flexbox works and then return
back to this example where you will code
it up yourself and properly understand
it.
If you are to become a flexbox master,
you will need to know align items.
Luckily though, it's not that hard to
learn because it is a close cousin of a
property you already know, which is
justify content. So, let's pretend that
the two of us, for example, work in a
lab together. Well, if so, we'd have our
lab equipment, which you can see here,
inside of our lab, which is this green
rectangle right here. And what we're
going to do now is use flexbox along
with the justify content and line items
in order to organize our lab equipment
in different ways in our lab. So let's
have a look at the code. The HTML is
very simple. It's just a container which
has three divs and each div hold exactly
one piece of lab equipment visualized
via an emoji. If we head over to the
CSS, you can see that the container has
a border and a fixed height. So that is
why we get this green rectangle. The
items have a width and a height. There's
some margin in between them and some
padding on the inside. I've set the font
size and also added a border. And
finally, something you haven't learned
yet, but I've added a gradient
background so that the color slightly
fades from a darker shade of green to a
lighter shade of green in the
background. Now, you are going to learn
about the linear gradient later on. So,
don't worry a second about this right
now. Just accept that it is how it is.
And let's get going with the organizing.
So as you can see by default the lab
equipment the divs that is are stacked
on top of each other because as you
remember divs are display block. However
if we turn it into a flexbox layout
each of the item gets its own column and
as a consequence they stack up on the
horizontal line. So far so good. And if
we set the justify content property
it'll arrange these in different ways
along this horizontal axis. So if we do
center for example they are centered. If
we do end they are pushed all the way to
the end and yeah you know the deal. So
what align items does is instead of
controlling the horizontal line which I
from now will call the main axis it
controls the crossaxis which is the
vertical one going from top to bottom.
So let's turn this back to start and do
align items.
Enter. And boom, there you can see now
it pushed all of the three items down to
the center so that they are centered
vertically meaning they are centered
along the crossaxis. And then I can
combine this with the main axis which is
justify content that controls the
horizontal one going from left to right.
For example, do end here and boom then
we are still vertically aligned. So
we're in the middle but we are pushed
all the way to the right hand side along
the main horizontal axis. So the values
you are going to use for line items to
begin with are start, center and end.
Whereas with justify content as you know
you can use start, center and end but
also these space around space evenly and
space between properties. Okay, let's
move on to the challenge. What I want
you to do is follow my orders on where
we should leave our lab equipment
because I want you to do it in very
specific ways. Starting with this
position here where they're all squeezed
down in the bottom left corner. So go
ahead and modify justify content and
align items to achieve that layout right
now.
So to achieve this layout you can see it
is at the bottom of the crossaxis the
vertical one. So we'll do end on align
items but it is at the start of the main
axis because it is pushed all the way to
the left meaning that we should do start
up here. And there we go. We have solved
it. Okay. New challenge. I want you to
achieve this layout. Go ahead and do
this right now.
So here we can see we are actually
centered both along the main axis
horizontally and the crossaxis
vertically. So I'll try center. Yes. And
center again. And there we go. Okay.
Final one. I want you to organize the
equipment like this. Go ahead and do it
right now.
Okay, hopefully that went well. The
first thing I'll notice here is that all
of the items are squeezed to the top.
So, align items should be start. And
there's some space around the elements.
So, we need to try one of these. For
example, space around, space evenly, or
space between. So, let's try for example
space between.
Hm, that didn't exactly achieve what we
wanted because there should be some
white space on the left side and the
right hand side here where these two are
squeezed all the way to the end. So
maybe we can do space around instead.
Yes, that looks exactly like what we
want. So really good job solving these
three challenges. Before we move on,
there's just a small thing I want to
tell you. And that is something you
might see from time to time, which is
that if I now remove the height of our
items, see, then they just became a
little bit higher because the content
inside of it wants a little more height.
But that was besides the point. What I
wanted to do is also remove the align
items. Let's see what happens. Oh wow.
It turns out that flexbox in the
crossaxis actually stretches the content
like this. And the reason it didn't
happen previously was because our height
property overrode that stretching. So
what this means is that align items by
default has the value stretch. When we
do for example start, it's no longer
stretched and it's pushed to the start.
So this might feel very confusing and
the only reason I wanted to tell you
about it now is that when you are in a
flexbox layout and you notice that
something is stretching in a way you
don't want it to stretch, the prime
suspect you should look at is align
items and experiment with that. So be
aware of that. But don't worry too much
if you found this a little bit
confusing. It's totally okay. Let's just
keep up the pace and move on to the next
scrim.
Now you're going to learn about flex
direction because there is one problem
with our lab system now and that is that
regardless of how we change around on
the align items and justify content.
Let's center and end for example our lab
equipment will always stack up besides
each other. They'll be on the same row
and there's no way to get them to stack
on top of each other. But maybe that's
how I prefer to leave my lab equipment
for the night. But regardless of how we
change to justify content and align
items, that is now impossible. Now we
could of course remove the display flex
and then these divs will stack on top of
each other by default. So by doing this
now they stack on top of each other.
However, justify content and line items
no longer work. So if I try changing
these,
nothing happens because these properties
work inside of a flexbox, not inside of
a random div that by default is display
block.
So we need the display flex like that in
order to make these work. However, what
we can do is change the flex direction.
So first let me just both of these two
back to start and then I'll do flex dash
direction
and it by default is a row because as I
said these stack up alongside of each
other on a row but if we change this to
column boom there you can see now they
suddenly stack on top of each other as
if they are in the same column as
columns go from top to bottom. So now we
are back to the original layout we had
before we made this into a flex
container. But justify content and align
items now work. However, they work in a
little bit of a different way than you
might expect because as you remember I
previously talked about the main axis
going from left to right and being the
one controlled by justify content
whereas the crossaxis going from top to
bottom and also being controlled by
align items. Well, when we change the
flex direction and our items stack up
differently on top of each other, we
also change the main axis. So, this is
no longer the crossaxis, it is the main
axis and it's controlled by justify
content. Whereas the horizontal one is
no longer the main, it is the cross and
it's controlled by align items. So, we
basically flipped align items and
justify content. So if I now do justify
content end, they'll jump downwards
whereas previously they would have
jumped to the right. And if I now do
align items and they'll jump to the
right whereas previously align items
control the vertical position. So this
is probably a little bit confusing now.
You should not feel bad if this feels
overwhelming. This will take time to
mature in your mind and it's totally
okay that in the beginning now you're
just experimenting with these values in
order to get the layout you want and not
really remembering exactly why it works
as it works. So let's now give you some
practice. I want to clean up my lab
equipment and I want it to be like this.
So go ahead and change around on these
two values in order to achieve that
layout.
Okay. What we need to do is on justify
content I want to center it so that it's
vertically centered but then we want to
align it to the start on the left hand
side. Yes, that is correct. Okay, new
one. Achieve this layout.
Okay, here we can see some space in
between the items. So I think we need to
use space between on justify compent.
Yes, looking good. But we are aligning
to the left. We want to align it to the
right instead. So align items should
probably be end instead of start. And
boom, that is correct. Final layout.
This one right here. Give it a shot
right now.
Okay, we can see that they are all
squeezed towards the top. So I think
we'll do justify content start like
that. Yes, this is good. But we want to
center it as well. So, we'll do align
items center. And there we go. So, great
job doing this. Let's just move on.
Okay. Now, we're going to put your
freshly acquired flexbox skills to the
test because you are going to turn this
layout here into this layout. So first I
want you to wrap the HTML elements in a
div with an ID called header. So that is
all of these items right here. And then
you are to select this header and then
use flexbox to achieve the layout from
the design slide. And as you might have
guessed, you will need to use flex
direction and align items to solve this.
So go ahead and give this your best
shot.
Okay. So, we'll start in the HTML.
Create that div opening tag and close it
off down here. Indent this, run it. No
change seen in the mini browser. That is
to be expected because we'll have to
target it and style it. So, I'm going to
do header and then first display flex.
that forces all of the elements into one
row because it is flex direction row by
default. However, we don't want that. We
want everything in one column. So, we'll
do flex direction column like that. Now,
they stack on top of each other, which
is just what we want. But there's a
problem. The two elements here are
stretching and the image of Nick is left
aligned. It's not centered. So, I think
we need to change the align items. align
items into center like that. And there
we have the layout we wanted. So great
job. Let's move on.
Okay. Now you're going to fix up the
design for this 24 years old element and
the date element right here because I
want it to look like this. Here you can
see that there's some space between the
content of the element and the edge of
the element. And there's not as much
space between the elements. And also the
edges of the rectangles are a bit
softer. But before you start doing this,
there's a couple of things I want to do.
Because here the H2 and the H4 have no
IDs or classes. They are just selected
using the element selector and also
using a selector list separated by
commas. And I don't think we should do
either of those. First of all, we
probably don't want all of our H2 and
H4s on the page to be styled according
to how it looks here up in the header.
So, we actually want to give these two
ids. I'm going to call this one Bday age
and this one Bday date. Running this
code so that we got it applied. And then
in the CSS, instead of using element
selectors, we'll use ID selectors. Eday
age and e day date. And then finally,
let's avoid applying styles to both of
these simultaneously. Let's work with
one or the other because this is really
an optimization and writing code which
is so-called dry, short for don't repeat
yourself. And while that is a good
thing, I want you to focus on the core
CSS now because this will introduce a
little bit more complexity. So let us
instead just keep it simple. So I'll
break this into a new line. So, we got
the birthday date rule here and then
copy the exact same thing to the bday
age like that. Okay, now it's time for
your challenge. You are going to make
the bday elements look like the provided
design slide like this. So, go ahead and
do this right now.
Okay, hopefully that went well. Let's
start with the age element. As you can
see, there is some space around it. That
is padding most likely. So, let's add
padding, for example, 10 pixels, and see
how that works. If we compare that with
what we have in the design, it looks
pretty good, but I think we shouldn't
have 10 pixels on each side. I think the
padding on the left and right side is
meant to be slightly larger than the top
and bottom, as we've often seen with
buttons and layouts in general. So, for
the top and bottom, we'll do 5 pixels
and and left and right 10 pixels. That
is what we are looking for as far as I
can see. Okay, next up there is some
border radius here as well. Not that
easy to see, but it definitely is there.
So, we'll do border radius 5 pixels for
example. That looks pretty good to me.
And now let's just try to copy this down
to our birthday date and see how that
works. Here you can see except for the
fact that there's much less space
between the elements here. They look
pretty similar as far as I can see. So,
we need to move on to the margin. Let's
try first and remove all the margin from
the birthday date. Margin zero like
that. Now it's quite a lot closer and
whatever margin comes from the age two
element and it probably has a bit too
much margin bottom. So let's rather
control that by doing margin bottom. For
example, 10 pixels and yes that looks
pretty good to me. Let's also look at
the margin top here. It is too much
space between the age element and the
image. So if we do margin top by 10
pixels there, that looks pretty good,
but it's perhaps still a little bit too
much. Let's do five instead. Yeah,
actually I think we got it. So the final
step then is to convert this into a
shorthand because we know how to use
short hands, don't we? We want five on
the top and 10 at the bottom. Then we'll
do margin 5 px for the top, zero for the
right hand side, 10 px for the bottom,
and zero for the left hand side. And
there we go. Now, if your solution is
slightly different from mine, perhaps
you used slightly different margins,
border radius, padding, that doesn't
really matter. Give yourself a pat on
the back regardless of how you solve
this and let's move on.
Okay. Now, you are going to create the
first gift section of our site. So, as
you can see, it contains a title, a
little hint about hovering over the
gift, and then there's the image itself.
Now, the hover effect is not a part of
this challenge. You're only going to
work with this hard-coded gift cover at
this point. And that gift cover has been
added up here in a images folder, which
I've just created. As we now have two
images, so I wanted to gather them
together in this images folder. That
means that I've also changed the image
of Nick or the source that is referring
to the image of Nick in the HTML. It now
contains the correct path into the
images folder. So, the challenge is
described in detail here. I want you to
start by creating a gift section div
that will wrap all of the elements for
the first gift. And then I want you to
add the H2, H3, and img elements. So
that is the title which is an H2, the
hint which is an H3 and finally the
image itself. And I want you to give
them all their own classes so that you
can use the classes to select the
elements when you are to style them
according to the design. And as a final
hint, the image should be 400 pixels
wide. So go ahead and do this right now.
Okay, hopefully that went well. So I'll
start by heading over to the HTML and
create the markup. So we wanted a div to
begin with. I'm going to create that and
then give it a class of gift section
like that. And inside of it, we wanted
first an H2 for the title, which should
contain the following text. Let's run
this. And yeah. Ah, nice. We can see
that the text already has the correct
font and also the slight shadow around
it. Okay, very nice. Let's then give it
a class as that was something we should
do with all elements.
I'll call this gift a title like that.
And there was the H3 with the following
text. And I'll give that a class of gift
hint. So you can see I'm using gift and
then a description. I think that makes
it easy to understand that these are all
somewhat related to each other. Finally,
the image element to have a source of
images/gift
cover.jpeg
and also a class of gift img. All right,
let's run this code. And as you can see,
everything is being rendered, though the
image is far too large. So we need to
get going with the styling. All right,
I'll start with the image. So, dot
imgd and give it a width of 400 pixels.
Yes, that is much better. And it is
centered because of our text align
center up here. Very nice. Okay, let's
just carry on with this one. You can see
it has a white border. So, let's do
border. Looks to be something like 6
pixels or something. And solid white.
Yep, pretty good. And finally, a border
radius because the corners here are a
little bit rounded off. So, let's try
border radius at 10 pixels for example.
And yeah, that looks pretty much like
the design for the image. Nice. Okay,
next up is the spacing. As you can see,
we have far too little space from the
gift section itself to the header
section above. And I could add that to
the top element here to the title for
example, but it makes a lot more sense
to add it to the container element
itself because it should control how far
it is between this container and the
container above. So let's do div section
margin top and for example 50 pixels.
That is much better and looks pretty
much like what we have here. Now you can
see that the space between the title and
the hint is very limited. It's not that
much space. Whereas here the default
margins for the H2 and H3 elements
create quite a lot of space in between
them. So now we probably have to work
with both the default margin that the H2
has at its bottom and the default margin
which the H3 has at its top. It doesn't
really matter what we start with. Let's
for example just start with the gift
hint and see what happens if we set the
margin top. Actually let's bring it up
on the side here. Now we've not done
anything with the margin top at all.
Let's set it to zero. You can see, hm,
nothing happened. Okay, that must be
because the margin top here and the
margin bottom under the H2 collapsed
into each other. And the margin bottom
for the H2 was probably larger than the
margin top for the H3. So removing it
served no purpose. That means that we
also need to try and remove the gift
titles margin at its bottom. Set it to
zero, for example. And there we go. Now
they're really crammed into each other.
Actually too much. They should have a
little bit of spacing in between them.
So let's try to instead of reducing that
to zero, we would set it to, for
example, 10 pixels. That looks much more
like the design we have here. Very good.
So you probably had different names on
your classes and different margins and
perhaps different borders and radiuses.
That doesn't matter. The important thing
is that you gave it a shot and got some
kind of result hopefully resembling
this. So, no matter what you did, great
job. Let's carry on with this course.
Okay. Now, we're going to prepare our
project so that we later can add the
hover feature. So, we're not going to
add the hover feature right now. We're
just going to set the stage for it. And
in order to do that, I'm going to first
mess up this project a little bit and
then it'll be your job to fix it because
we are not going to use the image tag
here in order to display this gift.
Instead, we are going to use a div tag.
So, I'm changing this into a div, which
means I also need to close it like that
because divs are not self-closing as img
tags are. And then next up, divs don't
have a source attribute. So we can't use
this in order to display the image. If I
now run this, you can see boom, suddenly
our image is gone. This src attribute
has no place here because instead we are
going to render this image through the
CSS background image property. So I'm
going to remove this one. If we run the
code again, you can see nothing else has
happened. And moving over to the CSS,
now it is your job to turn this weird
line here into something that looks like
this, which is exactly what we had
before. I messed up the code here. So,
you are to fix the gift img div because
I kept the class right here
so that it looks like the provided
design. And a little bit of a hint, it
needs a height. And if you wonder how
much, I want it to be a square, so equal
width and height. It also needs a
background image, which is how you'll
render this gift cover. It also should
be centered again because as you can see
here, this looks very much left aligned.
So basically what's happened here is
that the div has zero height because the
div has no content inside of it. So the
only thing that's visible is the border
which spans 400 pixels starting from the
left hand side and over towards the
right hand side. So go ahead and give
this a shot and then I will show you the
solution when you return back to me.
Okay, let's do this. First, it needs a
height, and it should be a square,
meaning equal width and height. So,
let's just do eight and set it to 400 px
like that. As you can see, we now have a
empty div on our page. Next up, we're
going to set the background image. So,
how do we do that? Well, you've seen
that one before. Background image URL
and then point it to the images folder
and the gift cover. Ape. And there you
go. But what is happening here? It seems
like we only kind of see the top left
corner of the image. That is because the
image itself is huge, much larger than
this 400x 400 pixel container. So, we
need to use our good old trend
background size and set that to cover.
And that squeezes our large image into
the current width we have available
here. Now, finally, why isn't this
element centered anymore? Well, the
centering of the image used to happen
because the text align is set to center.
But text align will only center inline
elements inside of its container. And as
you hopefully remember divs, they are
not in line. They are display block. And
and you might have tried to do display
inline like that. Then you can see that
doesn't really work. Then our width
property and height property gets this
weird underline underneath them because
they are simply ignored by CSS because
you can't force the width and height of
an inline element as they should only be
as large as the content inside of them
are. And this div doesn't have any
content. So we can't really use display
inline. What we rather can use is the
margin technique you learned earlier.
the one where you set the left and right
margin to auto because that works on
elements that are display block and
which have a set width. So let's do
margin zero on the top and bottom and
auto on the left and right. And there we
go. We have it centered. We are back to
our original design and we've set the
stage for our hover effect which Nick is
going to be so glad to experience. So,
it's really cool that you've made it
this far and let's just keep up the pace
and move on to the next scrim.
Hey there, it's Bob Zroll again and I'm
coming to you now with a quick update to
the lesson that used to be here. We're
going to have a quick aside to learn
about the hover pseudo class. To see
this, we're just going to use this
simple little app that we have here
where we have a restaurant menu. And the
user should be, of course, able to click
on any of these, which would
theoretically take you to a different
page. In the HTML, you can see we have
this semantic nav element, which
indicates that it will be a section of
navigation. And we kept it simple here
with three anchor tags to link to other
parts of our site. Now, currently, these
are just going to this hash mark, which
is sort of a placeholder. It won't
actually direct us anywhere different.
We can head over to the CSS and we can
see there's just a few rules here to
make these anchors look a little bit
more like buttons. We have the text
aligned to the center. The color of the
font is white. There's some margin to
separate it from other elements and some
padding inside. The background is the
red color that you see. We've increased
the font size. And because anchor
elements are typically display inline,
we changed that to block and then got
rid of the underline by using text
decoration none. And that's referring to
the underline that is typically there
when you have a link. Now, what would be
really nice with this menu is if we gave
some kind of visual feedback to the user
when they were hovering over the
different menu items here. I'm sure
you're not a stranger to this concept.
It's something that many websites do
when you're hovering over the
navigation. There's some kind of hover
effect that happens. And we can target
our CSS specifically to an element that
is currently being hovered by using the
hover pseudo class. The way we can do
this is by selecting our anchor just
like we did up above, but this time
putting a colon hover. This way any CSS
properties that we include here will
only be applied when the element is
currently being hovered. Or in other
words, whenever the mouse has entered
the perimeter of that element. So for
example, one thing we could do is when
the element is being hovered, we could
change the background color to this
hexodimal value here, which is a little
bit cryptic. doesn't really tell you
much about it, but just know that it's a
slightly darker shade of the red
background that we have when the item is
not being hovered. And you can see that
just by putting my mouse inside the
perimeter of that element, we get our
hover styles or this change in
background color applied and it's a much
more interactive experience. So, what's
happening here is we have added an
additional level of specificity or in
other words, we are being more specific
about this background color rule than we
were about the background that's up
here. And actually maybe just to have an
apples to apples, we could change this
to background color. That way we can see
that our anchor colon hover is being
more specific and therefore this
background color is being applied when
we hover. To be clear, this isn't
happening because we have written this
below our other anchor. I can move this
up above and see that the styles will
still apply just like they were before.
But rather it has to do with how
specific our rules are and the way CSS
is working under the hood, which we
don't have to dive into too deeply at
this point. But it's enough to know that
CSS is doing the intuitive thing just as
you would expect. Of course, background
color is not the only thing that we
could change here. We could change the
font size, for example, to let's call it
40 pixels and see that we can make as
many different changes as we want. And
now the hovered menu item is really
jumping out at you. I think that might
be a little bit much. So, I'm going to
get rid of that rule. On that vein of
being able to change other properties,
one cool trick that we can do, and this
will help us in the challenge that's
coming up in the next scrim is we could
do something like changing the
background image and we could set this
to a URL. We'll use this CSS URL
function as you are familiar with and
let's just point it to eyes.jpg
which we have inside of our files up
here. All right, let's see what happens.
I'm going to go to my menu and we'll see
that we get our friend Nick's eyes kind
of creepily looking at us through the
background of these menu items. Again,
this is a little creepy and probably the
strangest menu items you've ever seen on
a website, but hopefully this is a
perfect primer for the challenge that's
coming up on our birthday gift site as
we are just about ready to help Nick
unwrap his birthday presents. So, that's
what we'll be jumping into next.
Okay, now we are getting into the fun
stuff here because now we're going to
create this epic feature where Nick when
he hovers over the gift paper gets
revealed an underlying gift that makes
him feel happy and appreciated on his
birthday. So the challenge is described
here. You are to swap the gift paper
image out with the happy gift when you
hover over the gift paper. So the happy
gift is added here in the images folder.
I'm not going to show it to you. I want
you to solve this challenge in order to
see this gift. Now, the gift paper image
you are to swap out is this div right
here. As you remember, we gave it a
background image, which is the gift
cover.jpeg file, which also is in our
images folder. So, go ahead and solve
this challenge right now, and then I
will show you the solution when you
return back to me.
Okay, hopefully that went well. What
we'll do is first target this gift img
here as we want to create a hover effect
on it. So I'll do gift img colon hover.
Now whatever CSS properties and values
we are setting inside of these curly
brackets will be applied when the user
hovers over this gift image. And what
kind of properties do we want to change?
Well, it's actually only one, just the
background image here. So, I'm going to
copy that one, paste it in, and we don't
want it to be the gift cover.jpeg. We
want it to be the happy.gif.
Like that. Let's see how this works.
I'll move the cursor down towards the
image. And when it crosses over, we see
Phoebe and Rachel from Friends being
super happy for Nick on his birthday.
And I don't know about you, but if
someone told me that they would be this
happy for me on my birthday, it would
make me feel so grateful and
appreciated. So, we are really doing a
good deed creating this website. And you
know what? You've done a great deed for
yourself in terms of getting this far in
the course. You have crushed through so
many difficult CSS concepts. So, Phoebe
and Rachel are also cheering for you.
Take a moment to appreciate that and
then I will see you again in the next
scrim.
Okay, now that we've made sure that Nick
feels appreciated on his birthday
through this Phoebe and Rachel gift,
let's make sure he also feels really
good-looking because now we're going to
create this gift right here, which says
how people react when you enter the
room. And the GIF will show when he
hovers over this element is this hot GIF
up here. I'm not going to show it to you
now. You're going to have to solve the
challenge to see it. And speaking of
that, let's have a look at the challenge
text. So you are to create the next gift
which uses the hot GIF and its HTML
structure should be like the previous
one but without the gift hint element
because as you can see here we have this
hint written in parenthesis. However,
that's not needed for this one. I think
Nick will probably get the point by the
time he scroll down to this one. So
let's just skip it. But other than that
you can simply copy paste this section
and then of course change the text as
well. However, you will need to do some
more things as well because now that you
have multiple gifts on the page, you
cannot use the gift image class to set
the background image on hover. That is
this one right here. Because if you have
multiple gift image elements on the
page, well then every single one of them
will just show this happy gift. And
that's obviously not what we want. So
instead, each of the gifts will need a
unique ID that controls which background
image that should be shown when the user
hovers over the element. Okay, that was
probably a lot to parse for you. But if
you didn't quite catch it, just go ahead
and try to solve the challenge because I
think you will see this problem sooner
or later anyway. You definitely will,
actually. So make it your best shot and
then of course I will show you the
solution when you return back to me.
Okay, let's do this. So, starting in the
HTML by simply copying this one right
here and pasting it in down here,
removing the gift hint because we don't
need that. And then updating the title.
Let's run the code now. And here we can
see the first element with Phoebe and
Rachel. Let's scroll a bit down. And
there we have our second element, though
it still shows Phoebe and Rachel. So
that is a problem and that is because as
I said this gift image class just sets
the background image on hover to the
happy gift on both of these elements
right now. So we can't use a class for
this. We need a unique ID. But we're not
going to remove the class of gift img
because it actually gives a lot of
styling actually all the styling to the
element. So it's only the hover pseudo
class that needs to be applied to
something else. So let's instead now on
the first one here add an ID that we'll
call gif- img-happy.
We'll run the code so that it's applied
and then instead of referring to the
generic class here refer to this gift
img happy ID. Now, if we run the code,
you can see that it works on the first
gift, though the second gift now lacks a
hover effect. And that is because the
only hover effect we've applied now is
on this specific ID here on the first
gift. Previously, we used the generic
gift img class. So, it was applied to
all gift images, but now the hover only
works on the first one. So, what we need
to do is use this exact technique, but
on this section as well. So I'll create
an ID on this section and call it gift
bash img bash hot. I'll run the code so
that it's applied.
And then since I'm lazy, I'll copy this.
Gift img happy should be changed to gift
img hot because that is our new ID that
targets that element. And we don't want
to show the happy gift. We want to show
the hot gift. So let's see if this
works. Here we are at the section. When
we hover over the image, we see someone
looking pretty interested in Nick. So, I
think he will feel really goodlooking
and hot on his birthday when he sees
this gift. So, great job solving this
challenge. Let's move on.
As you can see over in the file system,
I've added a genius GIF and a badass
GIF. So, your job is now to create the
genius and badass gift sections. They
should look exactly as the hot section.
So, I'm not going to give you any more
instructions other than giving you the
titles so that you can just copy them
from here instead of writing them out on
your own. So, go ahead and create these
two sections right now and then I will
show you the solution when you return
back to me. Good luck.
Okay, let's do this. So, I'm going to
start in the HTML file because we're
simply going to copy paste this one
two times.
Then I'm going to add the titles. I'll
copy this one and paste it in here. And
copy this one and paste it in here.
Okay, let's run this
now. You can see we have the hot gif and
then the genius section which still has
the hot gif underneath it. That's to be
expected. And there we have the badass
section which also still has the same
GIF. So now we have the sections
rendered on the page at least. But we
need to fix up the CSS. So lazy as I am,
I'll copy this one here. It says gift
image hot. The first one will be the
genius one. I'll do genius instead. That
should refer to the genius gif. Now that
we have this in place, we can just give
this class to the respective image like
that. Running the code. And we have the
happy gif, we have the hot gif, and then
the genius gift. That is pretty cool. I
surely think Nick will appreciate this
compliment. Okay, the final one here.
The keyword is badass. So I'll change
the ID to badass now that we are in the
HTML
and moving on to the CSS. Copy this and
change from genius to badass because
that's the ID we just created. And the
GIF we want to fetch out from our images
folder is the badass.gif.
Let's open up our example again. Scroll
down to who is the only person as badass
as you. It is of course Chuck Norris.
All right, we are done with this
challenge. Great job. Let's move on.
Now, it's finally time to add the very
last gift, and that is the chairs gift.
So, you're going to add this section,
but there's a little difference between
this section and the previous ones
because, as you can see here, it is not
squared, but rather more rectangular. So
what I want you to do is give the gift
section a height of 200 pixels which is
different from the original height that
we controlled up here on the gift img
class where we set it to 400 pixels. So
the way you are to change this or
override it is by selecting the gift img
chairs ID which you are going to create
just like you've created ids for all the
other gift images as well. And then
you're going to set the height on that
element which essentially means to
override the previous height. And
finally here is the text as well along
with the chair skiff up in the file
system here. And I really think you'll
enjoy this one. So go ahead and give it
your best shot and then I will show you
the solution when you return back to me.
Okay, let's do this. I'll start in the
HTML by again copy pasting the preview
section and here I need to change the
text first. So I'm going to do
like that
and then we're not going to have that
ask but rather it shares.
So running this we can see we have a
squared image in place. That's not
exactly what we want. So we need to do
the next step which is to use this ID
which we just created and override the
height. I'll do height 200 pixels like
that. And there we can see it is now
just as we want it. The final step then
is to use this hover pseudo class in
order to change the background image to
chairs. Oh, copy this one and do chairs
instead. And now if we hover over it, we
see Leonardo DiCaprio in a smoking
giving us a toast. Really nice.
And one thing I want to mention here is
that you might be a little bit confused
as to why this height property takes
precedence over this height property.
And it is actually not because this is
written further down in the CSS file.
Actually, if we remove it to even
further up in the CSS file, even though
CSS reads from the top and then
downwards, this still takes precedence
over this. And that is because of
something called specificity. Which
means that the more specific a CSS
selector is, the higher it will be
prioritized. And this ID selector is a
more specific selector than the class
because as you know only one element can
have an ID whereas multiple elements can
share the same class. So ids are more
specific than classes. Now I don't want
to get more into specificity. We're
going to learn more about that later in
the front end developer career path. For
now I just wanted to mention it in case
you were a little bit confused. So, we
have now done all of the gifts and we
have a super cool birthday page we can
send to Nick. There's just a few things
left and then we are completely done
with this epic birthday site. So, I'll
see you in the next scrimm.
Now, you are going to create the footer
for our site and it should have a
paragraph that says, "Want to create
your own birthday gift site? take this
HTML and CSS course with a link to well
this exact HTML and CSS course and I
want you to configure the anchor tag so
that it opens up the link in a new tab
and if you've forgotten how to do that
well then you simply have to Google it.
I've provided the link right here and
also the text so that you don't have to
write that out on your own. However,
outside of that, I'm not going to give
you any more hints, but I want you to
notice that there is some spacing around
this paragraph, both above it and
beneath it. You can see it doesn't touch
the bottom of the site. And also, it is
centered. It's about as wide as the
images themselves, and it's also in
italic. So, you're going to have to
figure out how this translates into CSS.
I think you can do it. Go ahead and give
it your best shot, and then I will see
you again when you return back to me.
Okay, let's do this. So, I'll head over
to the HTML and add a paragraph which
I'll give an ID of footer. And let's
paste in the text
like that. And also the link
else we need to create an anchor tag and
it should be around this HTML and CSS
course. So we'll start the anchor tag
here. Do an href
and then in order to open the anchor tag
in a new tab, we'll do target equals
underscore blank. And then we'll close
it off at the end here.
Let's run this. Okay, so we have now
something that looks not too far from
our design. It's decent, but still a lot
of things lacking. First of all, this
text is far too wide. It should be more
or less the same width as the images.
So, let's do
footer.
Set the width to 400 pixels. Okay. What
happened now? Well, we do have text line
center, but now that this is 400 pixels
wide and it's still a display block
element as paragraph is that the text
line center won't center the content
here inside of its container. So, we
need to center it using the margin
technique. Meaning that we'll do margin
zero at top and bottom and then auto at
left and right. Yes, there it is
centered. But actually, we do want some
margin at the top and bottom because, as
you can see right here, it has some
spacing above and a little bit more
spacing beneath it. So, let's try 40
pixels on top. Yeah, that's pretty good.
And now 40 pixels at the bottom. That's
a bit too much in my opinion here. It
looks more like 20 or something. So,
let's rather than do 20 pixels like
that. Now, we have 40 at top, auto at
left, right, and 20 at bottom. And that
looks pretty good to me. However, one
obvious thing we need to fix is that
this anchor tag should also be white.
So, I'm going to target the element
itself. New color white like that. Now,
I think the final step we have is to
make it itallic. And that's done up here
where we can do font style
italic.
And there we go. That looks identical in
my eyes. So, really good job. I'll see
you in the next scrimm.
Okay, now we're going to talk about
gradients. And that's something you've
probably seen many times in nature,
though not really thought much about
because, for example, because the sky is
full of gradients. Here you can see a
sky that kind of fades from a light blue
color into a whitish color. And this
fade is basically a gradient. Here's
another example where it starts almost
black at the top and then slowly fades
over into purple and then again into a
orangeish or light brown or I don't know
what you want to call this color, but
you get the point. It's fading from one
color to another. And this is very often
used in web design and you can and you
can do it with CSS pretty easily. Let's
for example remove this background image
of the gift card from our gifts. Now you
can see it's just transparent.
and then instead we'll write background
and linear dash gradient
and then two parenthesis. So this looks
a little bit like the URL keyword we
write when we want to refer to images.
Meaning that this is also a so-called
CSS function. Now inside of the
parenthesis we simply write the two
colors we want to fade from and to. So
let's do from red to blue. And boom,
there you go. Now you can see that all
of our gifts fade from red down to blue.
And by default, it starts with the first
color at the top and then the second
color at the bottom. So let's try to
mimic this image here in CSS. I happen
to know that there's a CSS color called
light sky blue. So if we start with that
one and go down to white and have a look
at this gradient here. Actually to me
it's kind of similar to this one we have
here. Not quite, but you get the point.
Okay, now I'm going to remove this, give
us our gift cover back, and then give
you a challenge because now I want you
to change our site's background into a
gradient that goes from blue at the top
to pink at the bottom. And let me show
it to you. Let me and and let me here
show you how it will look. I'm going to
do some magic now. I'm going to knit my
fingers and then the gradient and then
the gradient will be applied
automatically.
pretty nice. But now you can see the
bottom here is pink and if we scroll up
it slowly fades over into blue and at
the top it's totally blue. So this makes
the site slightly more fancy which I
think is cool and matches the birthday
team. So now I'm going to knit again and
the gradient will be gone like that. Now
our site is blue all the way from the
bottom to the top and it's your job now
to get the gradient back. So go ahead
and do that right now.
Okay, let's do it. So, the property we
need to fix is the background on the
body because we need here a linear
gradient
going from the blue color it used to
have to the pink color which we've been
using as a border of our image of Nick.
So then I'll simply paste that one in
right here. Now we should be fading from
blue to pink.
Let's see. We can see the blue. And yes,
it looks like it is slowly fading
towards pink. Really good job. Now
you've learned a super cool CSS
technique. I'll see you in the next
scrim.
Okay, now that we have completed Nick's
birthday site, I want you to make it
your own. So, first try to think of
someone whose birthday is upcoming. It
could be a friend, a family member, a
celebrity, yourself, or even me. I would
love to get tons of these sites sent my
way on my birthday on January 26th. And
then once you figured out who to send
this to, make this site about them,
meaning that I think you should choose
your own gifts. You can just drag and
drop a GIF file into the images folder
here and then refer to it in the code.
Though, be aware that Scribba sets
certain size limitations on GIFs. So, if
it doesn't work to upload it, you might
instead just want to grab the URL for a
GIF you can find online, copy it, and
paste it in right here, so that Scribba
isn't hosting it, but rather the website
where you originally found the GIF.
Another thing you can do is change
around on the text for the gifts. And of
course, also, if you really want to go
crazy, feel free to change around on the
design. Perhaps you want to use other
colors, other fonts, and in general, be
creative and make it your own. Finally,
you have to deploy this to the web.
That's the whole point with this site
because the person who has a birthday
needs to see it. So, you know how to do
that as we've done that already earlier
in this course. And I would love to see
your site in I built this. So, feel free
to drop the link to your deployed site
in this channel if you are up for it. So
hopefully I'll see tons of these in the
I Built This channel and I'll also see
you in the next scrim.
It is not just Nick who deserves a
congratulation on this day. You too, my
friend, deserve exactly that. Congrats
on completing this section. Let's now
take a look at what you've learned.
Early on, we took a deeper dive into
Flexbox and learned about align items.
this property here which aligns the
items in a flexbox container along the
crossaxis. And speaking of axis, you
learned how to flip the main axis of a
flexbox using the flex direction
property. We set it to column here,
which is the opposite of the default row
value. So then flipping the direction
from being horizontal to being vertical.
You also learned about a super cool
pseudo class called hover which allowed
us to create this epic feature where
Nick hovers over the image and sees the
underlying GIF. And you learned about
another cool CSS feature which is how to
create gradient so that the background
of this website slowly fades from blue
to pink. And we achieved that using the
linear gradient function which we added
to the background of the body. A final
thing we touched briefly upon was the
grouping selector, which allowed us to
make our CSS syntax much more compact as
we could target all of these HTML tags
instead of having to recreate this text
shadow rule again and again on new
lines. So, you have made a ton of
learnings. Make sure you take a break
and get some rest. And then I will see
you back here in the next section when
you are ready.
Welcome to the final section of this
course. And here it is time for you to
go solo. And what do I mean about that?
Well, you know how I've kind of been
peeking over your shoulder, helping you
step by step throughout all of the
projects we've built? Well, that time is
over. Now, I'm going to remove the
training wheels and you will have to
build a full project on your own because
that's actually the only way you can
know whether or not you've truly learned
HTML and CSS. So, you're going to build
a so-called solo project. And the
project is a homepage for your hometown.
You can see it here on the right hand
side. This version right here happens to
be about my hometown or the place I grew
up in Oslo, Norway. It's a part of the
city called Torchov. However, the point
with this is not for you to build
exactly this, but rather build a website
about a place, area, city, or country
that you care about and you have a
connection to. Because when you do that
and then share it in the Discord
community, it'll give an opportunity for
other students on Scribba to get to know
you better. And hopefully you'll enjoy
building it as well because it is about
the place you care about. Now, I'm going
to go deeper into the content of the
solo project in the next Scrim. But for
now, there's one thing you need to do,
and that is to head over to figma.com.
Because Figma is the design tool in
which you'll get the design for your
project. It is the most popular design
tool these days. It can kind of be seen
as the GitHub equivalent for designers.
So I want you to click on this image
right here and then you'll be taken to
figma.com. There, click on sign up and
create an account. And I also want you
to click on this image right here. Is
that'll take you to a quick 10-minute
Figma tutorial which Bob Zerole, our
teacher here at Scribba created
specifically for students who need to
learn a little bit of Figma in order to
do solo project here on Scribba. So you
can either watch this right now or you
can wait until you've started the solo
project when you feel the need for some
guidance through the Figma interface.
The reason this is at YouTube and not
here on Scribba is because learning a
browserbased tool like this is actually
better to do via a regular video than
via slides here on Scribba. So with
that, let's move on and take a closer
look at the actual solo project.
Now it is time for you to solve your
very first solo project, the hometown
homepage. And here it is. As you can
see, it is a website that consists of
three sections. The hero here has a
background image and a heading and
subheading. There's the activities
section, which has a title and three
columns, each one containing an image, a
heading, and a paragraph. And then
there's finally this card at the bottom
which is meant to introduce you to a
tourist guide for this area. And you
might look at this and think, how on
earth am I supposed to create all of
this? Well, take a look at each of the
elements and think back at what we've
done in this course. For example, this
hero section here with a background
image and another section beneath it.
Well, that's pretty similar to what we
did in the space exploration site. And
these two headings right here with the
background colors. Well, that's what we
did in the birthday gift card with the
year element and the date element. And
as for the card down here, I think that
resembles our business card quite a lot.
Finally, the three columns we have in
the activity section, well, that looks
like a job for flexbox if you ask me.
And we've used flexbox multiple times.
Even the way we use it here where
actually all of the three flex children,
the items are containers in themselves
which contain an image, a heading, and a
paragraph. So I think you got this.
Let's then look at the specific
requirements. First of all, you're going
to build it from scratch or I'm saying
that with quotes because I've actually
added a little bit of a skeleton here
for you. So you can see what we got.
basically a version with hardly any
styling but with some of the elements in
place. You can see there's the hero
section here. There's the activity
section. It doesn't have all the data
but at least the three images. And then
there's the guide section at the bottom
here. Obviously though it looks terrible
because the only styling I've applied is
some margin zero on the body and I've
aligned the text to the center and also
limited the width of the images to 100
pixels. So you can use this as a
starting point if you want or you can
wipe it out and do it completely from
scratch. That's up to you. Next I want
you to make sure that you do use classes
flexbox the background image property
and also the color plet that I'm
providing. As you can see in the CSS
here are the hex values for the color
pallet I'm using. And also I have a link
to coolers here. You can click this
image and you'll be taken to the color
palette. And then once you're done, if
you are up for it, there are some
stretch goals. First, make it about your
own hometown, country, or whatever place
you like. I hope everyone wants to do
this because when you share it in the
Scribba community, we will get to know
you a little bit better. Secondly, if
you use a different color palette, your
site will be even more unique. And if
you add a Google font, you can really
level up the design because I've not
done that. And as a result, the font
here is a little bit boring. Also, if
you want to go crazy, find a way to use
the hover pseudo class on any of these
elements. That would be super cool. And
finally, you could add an entire new
sections. That would be so cool.
However, do remember that these stretch
coils are optional. You don't have to do
them, but I would encourage you to give
it a shot if you have the energy. So,
moving on. To get to the actual Figma
file, click on this image right here.
And if you get confused, do remember the
Figma tutorial I talked about in the
previous scrim. You can click on this
link right here to get to it. Now, if
you're watching this on YouTube, these
links have also been added to the
description below the video and they are
in the corresponding GitHub repo for
this course. Finally, you can do this
solo project either locally on your
computer or here at Scribba. If you do
it here at Scribba, I would recommend
you to click on the three dots in the
bottom right corner and choose fork to
new scrim as that'll give you your very
own scrim to work on instead of just
working on a note on the timeline of
this specific lesson. So, I will leave
you with these requirements so that you
can look back at them when you need to.
With that, I wish you the best of luck.
You got this. Happy coding.
Oh, wow. Did you just complete a
fivehour long module here on Scribba?
Wow, that is amazingly well done.
Because you know if this is the number
of people who start learning HTML and
CSS, this is the number of people who
actually learn it and you belong in this
group. So you are truly something
unique. So actually I think you should
brag about this as much as you can. For
example, in the today I did channel on
Discord or in one of the module channels
or to whomever is in the same room as
you. Make sure that you communicate to
the world that you rock because you are
the kind of person that completes online
courses. A rare breed in today's day and
age. So feel free to rest a little bit
as well and then I will see you when you
are ready to tackle JavaScript in module
3.
Hi there, it's Pier from Scrimby here
again. You are doing a phenomenal job
plowing through this path and I feel
super privileged to be able to follow
you into the exciting world of
JavaScript as well. We are going to
build a bunch of cool projects together.
Starting out with a very simple
passenger counter until we up the game
and build a blackjack game. You are also
going to practice your skills through
building a password generator, a unit
converter, and even a Chrome extension
that serves as a tool for salespeople in
order to track their leads. So, it's
going to be a lot of fun. And I can
promise you that learning your very
first proper programming language really
changes how you think. You're going to
feel like you've become somewhat of a
genius at the end of this path, having
unlocked a whole new level of logical
thinking in your brain. So, let's just
dive in and learn some JavaScript.
Your very first exposure to JavaScript
will come through building a passenger
counter app. Now, you might ask
yourself, why on earth should we build a
passenger counter app? What's the point
of that? Well, you'd be surprised
because when I was 19, right around this
time, my full-time job was to count
people who entered the subway. So every
day I would stand outside in the cold
and be like, "Oh, so there's 1 2 3 4 5
six people entering the subway this
morning. How interesting." And then I
would bring up my pen and paper and note
down the number six, which at times was
really cold. So it would have been so
much better if I had a little people
counter app on my Nokia phone so that I
could just tap these buttons while
having the phone in my pocket. But this
was long before I learned to code. So
instead we are going to build this
passenger counter app together so that
you will learn the basics of JavaScript
and so that you are prepared in case you
one time in the future have to stand
outside in the cold counting people who
enter the subway.
Welcome to this lesson where we are
going to take a look at the setup for
our people counter app. Here you can see
we have a basic HTML structure which
probably looks familiar to you. You can
see we have the HTML tag, the head tag,
and inside of the body, we only have two
elements, the H1 and an H2. That is the
title up here, and our counter element.
We also have some styling in our
index.css.
You can check it out here. This is just
very basic styling that we're not going
to go through because you want to get
into the JavaScript as soon as possible.
So, the first thing we're going to do
actually is to simply inside of our body
tag create a so-called script tag like
that. Looks just like any other HTML
tag. It just says script. And inside of
here, you can actually write JavaScript.
So, inside of this tag, I'm going to
write a pretty long JavaScript
expression that you probably find a
little bit frightening, but don't worry
about that. You're not meant to really
understand it in this lesson. I just
want to demonstrate that this works. So
I'm going to start by writing document
that refers to the HTML document itself.
And then I'll do dot and get element
by id and then open and close
parenthesis.
Then inside of the parenthesis I'm going
to copy the ID of our H2 element. So
here we're saying hey HTML document I
want to get an element and I want to get
it by its ID and the ID is this count L.
So what do you think we get as a reply?
Well, we grab a hold of this element
right here. And then we can do dot inner
text set that equal to, for example,
five. If I now run this code, you'll see
that boom, we have changed the text on
the inside of the H2 element, this one
right here, from zero to five using
JavaScript. And that opens up a whole
world of opportunities. It essentially
gives us the power to breathe life into
our application and turn it into a
realworld product that solves problems
for people. That's why this is so cool
to learn. However, we are not going to
write our JavaScript this way here. It's
kind of an amateurish way to do it. What
we rather want to do is create an
index.js file like that. Here is our
index.js file. It's currently empty. But
what I'm going to do is copy this line
here.
Then paste it into the index.js file.
And now
add an attribute to my script tag called
src, which is short for source. And then
inside of here, I'm just going to write
index.js.
We are in the HTML file and the
JavaScript file. It is in the same
folder. So I don't need to navigate into
any folder or type in a complicated path
here. Just write the name of the file.
If we now run this code again, you can
see that yes, it still works. So now we
have a proper setup for our app and we
are ready to get started building our
people counter. Let's move on.
So today is actually a really important
day in your life because you're going to
write your first lines of JavaScript.
And that is just such a powerful thing
to do because you take the first step
into a world of opportunities and it
might even change your life. So in a
year you might want to celebrate this
day as the anniversary the day you met
JavaScript and changed your life for the
better. Anyway, the line you're going to
write is not this long one right here.
It's a little bit too difficult at the
moment. I'm actually going to add a
double slash in front of it like that.
And you can see that grade it out. That
means that JavaScript will ignore this
since this is now a code comment, not
real code. So if we run the code here in
the browser, you can see that now we are
not modifying the H2 tag anymore. So we
are ready here down at line three to
write our first lines of code. Now let's
think a little bit. What do we need
actually to build this subway passenger
counter app? Well, the first thing we
need is some way to store the count. And
that's a natural job for JavaScript
because it's kind of like the data we
need to store. And when you need to
store data, you need to create so-called
variables to store the data in. And I'm
now going to create a variable called
count. And the way we do that is we
write let pounds equal zero. Now we have
a variable called count. and that holds
a tiny piece of data, namely the number
zero. The way you can read this in
English is actually let count be zero.
So pretty natural. And let's actually
try and verify that actually our count
variable does indeed equal zero. Well,
we can do that by using a tool that
every single developer uses every single
day. It's called console.log.
Then open and close parenthesis. And
inside of these parenthesis, you can put
almost whatever you want. And in our
case, we are going to put this count
variable here. If we now run the code,
you can see that down in the console at
the bottom, we printed out the number
zero. And the console is something that
the browser provides us with. It's there
mainly for debugging purposes, for
checking our code like we are doing
here, verifying that actually, yeah, I
want to take a look at at count. I want
to print it out to the control and see
with my own eyes that it is what I think
it is. And indeed, it was. And this
console you see here is Grimbus
implementation of the console. If you
want to check out the real world console
in a Chrome browser, you can for example
head over to google.com or any page
basically and then rightclick and choose
the inspect option. When you click that,
it'll open up this so-called developer
tools which shows you a bunch of HTML.
As for now, we're not going to worry
about that. Instead, just click on the
console option and that'll open up a
blank console where you actually in
addition to printing out stuff which we
did down here in the scribba console.
You can write JavaScript as well. So if
you want to write let count equals 5 and
then hit enter and then write console
log and then pass in count in the
parenthesis and then hit enter, you'll
see that it'll log out five as well. So
the console is available to you in all
browsers and it's a tool that developers
use every single day. So, I'm really
happy that you've learned about it this
early on. Now, I actually have a
challenge for you. I want you to do two
things. First, you're going to create a
variable called my age and then set its
value to your age and then log the my
age variable to the console. So, go
ahead and do this right now and then I
will show you how to do it as well when
you come back.
Okay, hopefully that went well. Let's do
it together. Do let my age. I happen to
be 35 at the moment. Then to console log
out my age, I'll do console log in close
parenthesis my age. If we run this code,
you see now we are printing out both
account and also my age. So we have
verified that our variables indeed work
as expected. Before we move on, there's
one thing I want to show you. and that
is to take this line console logging of
my age and place it above the my age
variable declaration. So now we try to
console log my age before we define it
since JavaScript starts reading at the
top and works its way downwards. So what
do you think then will happen if I now
remove these other control logs and only
keep the my age console log? Let's take
a guess. Well, what happens is that we
get a reference error. cannot access my
age before initialization. So,
JavaScript actually understands what
we're trying to do that we're actually
referencing a value before it has been
initialized and that simply doesn't
work. So, now that you are aware of
that, let's move on.
Okay, so let's return back to our count
variable and learn some more advanced
concepts, which is that you can actually
use JavaScript kind of as a calculator.
So instead of just writing let count
equals 5, you can do let count= 5 + 7
for example. And when you run that code,
you'll see 12 in the console. You can do
minus of course that'll result in minus2
in the console. Or you can even divide
0.71 bunch of numbers after that. And of
course multiply gives us 35. So there's
a lot of mathematical operations built
into JavaScript. And you're not going to
learn about all of them now because now
instead of just hard- coding raw
numbers, let's try to use variables here
instead because often times you won't
see this kind of operation. You'll
normally see numbers represented as
variables in real world code bases. So
removing this right here and for example
do let
first batch equals 5. So this was the
batch of people that first entered the
subway wagon. And instead of hard coding
five here yet again, just going to do
count equals first batch. Well, what do
you think will be logged out here then?
Quite correctly, it's five because first
batch is five and count is set to the
same as five. And perhaps there was
another batch as well. Let second batch,
a group of people that entered just
before the doors closed. Maybe that was
seven people. Well, then we can do
count equals first batch plus second
batch. Yes, this works as well. So, this
is a much more common thing to see in
JavaScript that a variable is set to the
value of, for example, two other
variables added together. Now, I want to
comment out this like that. And if you
wondered how I did that magic of
commenting out multiple lines at once, I
did simply command + k + c or replacing
this with control if you're on a windows
machine because what I want to do now, I
want to gather this code here
and then I want to give you a three-part
challenge where you are going to convert
your age into dog ears. So first create
two variables my age and human dog
ratio. That is the ratio between human
years and dog years. And for the sake of
simplicity, we're going to say that one
human year is about seven dog years.
Now, the calculation is actually a
little bit more complicated than that,
but that's not that important. The
important thing here is the learning.
So, set then the human dog ratio to
seven and then multiply the two
variables together and then store the
result in my dog age. Finally, log my
dog age to the control. This is great
practice. It's important to build up
your muscle memory. So, go ahead and do
this right now and then return back to
the screencast when you want to see how
I solve it as well.
Okay, hopefully that went well. Now,
let's do it together. Move the console
so we can get some more space here. And
we'll start with my age, which still is
35. And we'll do human
ratio. That should be seven. Finally,
the my dog age should be my age
multiplied with human aug ratio. Let's
verify that this works by logging out my
dog age.
The code and yes, we can see in the
console we get 245.
My god, I would be an old dog if I was a
dog. I'm really glad I'm a human
actually. So, let's move on.
Okay, so let's return back to our count
example. Now you've learned how to
initialize a variable and even how to do
mathematical operations on it, both with
hard-coded numbers and with other
variables if we need that. And that's
all great. However, up until now, we've
just been working on the initialization
or the so-called assignment of the
variable. What if you want to reassign
it or in plain English, you want to
change the variable? Well, then you can
do that, for example, down here on line
five and just do count equals 3. And if
we now run the code, you see that we log
out three as opposed to five because
JavaScript starts from the top and runs
down and uses the latest value it can
find for any variable defined with the
let keyword. Now, there are other types
of variables that you can't reassign
like this, but we'll get to that later.
As for now, we're just going to work
with the let. And you can do this as
many times as you want. If I want to
change it even further down down to one,
you can see now that JavaScript is only
taking into account the line seven
reassignment of the count variable. So
this is all good, but it doesn't really
help us if we want to increment our
people entered count with one every time
we for example click a button. What we
then want to do is something like count
plus one because we want to take the
current count, keep track of how many
we've counted up until now and then just
increment with one. The way to do that
is actually by simply doing like this.
Now this might look a little bit weird
since we have a count on both sides of
the equal mark and probably you've
learned at school that the left and
right hand side of the equal mark should
be completely equal in mathematics.
However, that's not how it works in
JavaScript. Here we are assigning this
variable to take on this value. So first
you want to calculate the value and the
value is this point 5 + 1. Well then
count now will be equal to six. Let's
run this code and see that it works.
Yes. And let's say we wanted to
increment it by two. Then we do two and
now it's seven because 5 + 2 is 7. Do
the same thing again. This time we can
increment it with 10. Now we're up at 17
and minus that with two again and then
we're down at 15. I think you get the
point. Now I have a challenge for you.
Okay. So you are to create a variable
bonus points. So this could for example
be a game where your bonus points goes
up and down. And I want you to
initialize it as 50 and then increase it
to 100, decrease it down to 25 and then
finally increase it up to 70 again. And
you are to use the same technique that
we used here with the count where you
reassign the variable to the old value
of the variable and then adding or
subtracting a value. And I want you to
console log the value after each step.
So go ahead and do this right now and
then I will show you the solution as
well.
Okay, hopefully this went well. Let's do
this together. We'll start with let
bonus points be 50. Console logging it
out. And then we're going to do bonus
points equals bonus points plus 50
because we want to get to 100. Again,
console logging it. Next up, bonus
points equals bonus points
minus 75 because we want to get down to
25. Logging it out again. Finally, bonus
points equals bonus points. Now we need
to increase it with 45
70. Let's comment out this code up here
and run the code. Bring up the console
and yes 50 100 25 70. Great job. Let's
move on.
Okay, now you know enough JavaScript for
us to move on to the HTML and build the
interface for our app. And it's not a
lot of interface. It's h to begin with
just a button and later down the line
we're going to create another button but
it has to be written and you are going
to do that. So I want you to follow
these instructions. You are to create an
increment button that is the text of the
button with the ID of increment-b.
So go ahead and do this right now.
Okay. Hopefully that went well. Now
let's do it together button. Then I'll
create the closing tag inside of here.
I'll write increment. Could have written
count here. But I think increment is
more of a technically descriptive word.
So let's use that. And ID
increment dashbtn like that. If we run
this code, you'll see yes, we indeed
have this button and it is already
styled. How convenient. Let's check out
the CSS for that. We have a general
styling for buttons which are reused
across both of our buttons since they're
using the button selector. And then our
increment button and our save button has
a specific background each of them.
They're dark red and dark green. Great
job doing this. Now the question is how
do we make this button interactive?
Because right now nothing happens if I
click it. Well, in the next lecture
we'll go through exactly how to do that.
Okay. Okay, so now we're going to put
the different pieces in this puzzle
together and also add a whole new piece.
And in order to free up some space in
our JavaScript file here, let's just
remove this previous exercise and
actually write out in pseudo code the
procedures that we need to do in order
to make this work. So first we know that
we need to store the count somewhere
and we know that we want to initialize
it as zero. And then as I said in the
previous lesson, we need to kind of
listen for clicks on this button here.
When it's clicked, we need to increment
our count.
Then finally, which we aren't going to
do just yet, but I just want to write
that procedure out as well so you know
what to expect,
we need to change the countell,
this paragraph right here in the HTML to
reflect the new count. So we know how to
initialize the count as zero and we know
how to increment a count variable. So to
achieve these first three we need to do
this task that is listening for clicks
on the increment button and that can be
done in a multitude of ways. The easiest
one is simply in the HTML defining an on
click event listener. You simply write
on click equals and then something in
between quotes. So this is an attribute
in the HTML just like the ID is an
attribute as well. And inside of here
you write the function that you want to
be triggered when the button is clicked.
So I'm going to write increment and open
and close parenthesis here. Now that
probably looks really weird for you.
That's totally okay if you're confused
by what's going on here because we've
seen nothing of the word increment in
our JavaScript yet. So rightfully so. We
need to create that function. I'll do
that down here. And I'll use the keyword
function, then the name, increment, open
and close parenthesis, and then open and
close brackets. So, pretty funky syntax
right there. And the first time you type
them, it feels weird, but over time it
becomes second nature. What you write
inside of these curly brackets is the
so-called body of the function. And that
piece of code that is in the body will
be executed every time this function is
called or invoked. That's the same
thing. So just to see it in action going
to write console log button was clicked
like that. If we run the code and I
click the button you can see that in the
console we see the button was clicked.
If I do it again I again and again and
again and again. So every single time
the button is clicked this piece of code
runs. And it runs because we in the
HTMLs are saying, "Hey, JavaScript, when
this button is clicked on the on click
event, then I want you to run a function
called increment."
And that's what's happening here. And
then JavaScript is like, "Yeah, for
sure." So it sits ready and listens for
that specific click event. And
JavaScript can do that. It pays
attention to what the user does on the
screen. And when it sees someone
clicking on this button, it fires off
this piece of code.
This was probably a lot of information
to parse in one go. So before we move on
and continue on turning our pseudo code
into real JavaScript, let's take a
closer look at functions over the next
few casts so that you get to write some
functions as well and build up your
understanding and muscle memory.
Okay. So let's pretend that we are
building a race car game. So here I have
some comments which are meant as pseudo
code for the functionalities of the game
and the only real functional part of it
is these five console logs which are
counting down from 5 to 1. So we start
the game by setting up the race and then
we do 5 4 3 2 1. If I run the code, you
can see that is logged to the console.
That means that it's go. Players are to
start racing and they would be running
the race and then the race is finished
and a winner is declared. And then of
course we are getting ready for a new
race. However, with the current
knowledge we have about JavaScript, the
way we would count down for a new race
would have to be to copy all of these
lines and paste them in down here. So
that now when we run it, we get 5 4 3 2
1 printed two times to the console.
However, this isn't good. Anytime you
see these kinds of repetitions in your
code, you should try to think is there
any way we can extract this into a
function and that is exactly what we are
going to do. We are going to refactor
this so that it instead uses a function.
Another word for function could be
command. So we are asking JavaScript to
kind of remember a command so that we
can use it later on as well. And that
command can be as comprehensive as you
want. In our case, it's going to be to
countdown from 5 to 1. So let's just do
this to see it in action. To create a
function, as I talked about in the
previous cast, we use the function
keyword and then the name of the
function. I'm going to call this
function countdown, then open and close
parenthesis, and then open and close
curly brackets. And all of the action
happens inside of the curly brackets. So
I'm going to break that into a new line.
And I'm going to copy all of these
control logs and then paste them inside
here like that. And here I'm tabbing it
out just to highlight that this code is
inside of that function. This is not
something JavaScript cares about. It's
to make our code easy to read for
humans. So now I have declared our
function. However, if we remove this
down here and now try to run the code,
nothing happens because it has only been
declared. it hasn't been what we called
invoked or also called as this code
inside of here will only run if we call
the function. So if JavaScript was a dog
and we've taught our dog to roll around,
then in order to get our dog to actually
roll around, we have to say roll around.
Same thing here. We have taught
JavaScript to countdown, but in order
for it to actually do the counting, we
have to say countdown. And we do that
like this. Simply writing countdown with
open and close parenthesis. So now you
can see when I run the code, it indeed
does the counting. But if I comment this
out, it does nothing. So what I'm going
to do now is I'm going to take this
countdown and I'm going to put it all
the way here at the top. And then I'm
going to call it when we are ready to
start the first race and then also call
it when we are ready to start the second
race. Now if I run the code, you can see
that we indeed countdown two times
before the first race and before the
second race. And this is much better
than typing out all five console logs
both here on line 11 and on line 19. So
we have compressed our code a lot. So
with this knowledge fresh in mind, let's
move on to the next cast where you are
to write your first function.
Okay, now it's your turn. You are going
to create a function that logs out the
number 42 to the console. And you can
decide the name of the function. And
when you've done that, make sure you
call or invoke the function as well. So
go ahead and do this right now.
Okay, let's do this. I'm going to call
it my
logger. Open and close brackets. And
then inside of the body do console.log
42. Now we need to call it my logger.
Like that. We run the code. And yes, you
can see we are logging 42 to the
console. Great job.
Okay, time for a new challenge. So in
our race car game, we have certain laps
and of course we track how much time our
players use to race through the laps.
And here we have three lap times in the
variable lap 1, lap 2, and lap three.
And the thing is, if you create a
function down here on line six, it will
be able to access these three variables
from within the function. So what I want
you to do now is create a function that
simply logs out the sum of all the three
laps. So that'll be the total racing
time for a player. So you decide what
name you want to give it. Just write
that function that logs out the total
lap time and then run the function as
well.
Okay, hopefully that went well. Let's do
this loop function. I'm going to call
this function log lap time. Open and
close parenthesis. And here I can either
first create a variable. Let total time
for example do lap 1 + lap 2 + lap 3 and
then console log out total lap time like
that and then log lap time invoke the
function down here on line 11. If we now
run the code we can see yes we are
logging out 103. This is one way of
doing it. Alternatively, I could have
skipped creating this variable in here
and simply added the variables inside of
the console log. It will give me, as you
can see, the exact same result. Now the
reason I wanted to create this variable
here though is to just show you one
thing which is that while this function
could access these variables from the
so-called global scope the outer scope
which is not on the inside of the
function but on the outside of it. This
doesn't work the other way around. So if
I now just try to console log total time
from the outer scope if we run this you
can say total time is not defined. That
is because when you define a let
variable inside of this scope it only
exists inside of this block of code.
This is the scope in which the function
lives. And that's why we call let
variables block scoped. So that might be
a little bit confusing in that the lap
could go from the outside of the
function and to the inside of the
function, but total time couldn't go
from the inside of the function to the
outside of the function. But don't worry
too much about this. JavaScript will
tell you when something isn't defined,
and you can be sure that you will
misstep at times, but that's just a part
of the process. Let's now move on to the
next challenge.
Okay, so now we have to combine some of
the things you've learned up until now
in order to build this lap counter
feature because in our racing game, we
want to keep track of how many laps a
player has completed. So I've created
this variable called laps completed and
initialized it to zero. So what I want
you to do now is create a function that
increments the laps completed variable
with one every time you run the
function. Then I want you to run or call
or invoke or whatever you want to call
it this function three times so that on
line 9 here the value that's logged out
will be three. Now if you're wondering
how to increment a variable in
JavaScript just look back a few casts
because we did that previously. So now
you have to combine that knowledge with
your freshly acquired knowledge about
writing functions. So go ahead and give
this your best try right now.
Okay, hopefully that went well. If not,
no worries. Let's do this together so
that you get to see how we do it. Do
function. And I'm going to call this
function increment lap like that. Open
and close parenthesis and then open and
close curly brackets. And here we have
to increment this variable just as we
did a few casts ago by doing laps
completed equals the old value of laps
completed plus one like that. If I now
run increment lap one time you'll see
that we're logging out one. Good stuff.
Let's
invoke it three times. And there we can
see we are logging out three to the
console. So great job doing this. If you
didn't manage it, just go back and try
again now that you've seen my solution
because it's really important that you
actually write the code here so that you
build up that important muscle memory.
Okay, we're back at the subway counter
app. And as you can see, I've deleted
the increment function because I want
you to write it instead. It's much
better than me writing everything. And
you want to do it as a part of turning
these first three lines of pseudo code
into real code. So you're going to
initialize the count as zero. You're
going to listen for clicks on the
increment button and thus you have to
create that function that's triggered
when the increment button is clicked and
then you are going to increment the
count. I'm going to add a final little
challenge here and that is to log it out
after the count variable has been
incremented. So go ahead and do this
right now and then afterwards I'll of
course show you the solution as well.
Okay, hopefully that went well. Let's do
this together. We'll do function
increment
open and close parenthesis and close
curly brackets. We have the button.
Let's verify that it works. Good thing
to do after every single step so that
you know if you at some point get a bug,
you will have a better understanding of
where it happened since you know which
steps you were able to get to without
triggering any bugs. So, I'll do
clicked. Run the code. Click the button.
Yes, that works. Okay. Now we need to
increment the count. So we're going to
do that inside here. And of course then
we need the count initialized to begin
with. So count equals zero here. As you
remember it's just as we're simply going
to do what we did up line for. We'll do
count equals count + 1. Now finally
console log count. I'm going to remove
this and run the code. Hit increment.
Yes, we see one and two and three and
four. Nice. So then we have done these
three parts and remove this helper code
right here. We have the final part left
and we can finally start understanding
what this weird piece of code here up on
line one that we worked with in the very
beginning of the course actually means.
Let's do that in the next lecture.
Okay, so we are ready to add the final
piece of the puzzle which is to change
the count L that is the H2 right here
which currently has zero
so that reflects the new count and the
new count is down here which we are
logging out. So we have to do something
down here at line 9 and what we're going
to do is take this line right here which
has just been lying here in the
background all along. Now in order to
properly understand this expression, I
think we should break it into two. Take
the first part where we're actually
asking for the HTML element and the
second part where we're actually
modifying the HTML element. So starting
with the first part, I'm going to store
this in a variable called count L like
that. then document
get element by id and close parenthesis
and passing in count l
this exact string right here. Notice
that we're using different naming
convention here in the JavaScript. This
is called camel case like that where you
capitalize the first letter of the
second word but not the first letter of
the first word. This is how you want to
write JavaScript variables. It's a very
normal convention. And also, if we were
to try to use the same convention as
we've been using in our HTML and CSS,
JavaScript would actually tell us, uh,
uh, uh, you can't do that. As you can
see, we're getting a red line under this
character, which is the JavaScript's way
of complaining to us because it's not a
valid variable name when you use dash.
So, we got to remove it like that. Now,
as always, what do you think we should
do now? Now that we've taken one step
into the unknown, well, of course, we're
going to use our good old friend console
log because we have to verify at every
single step that we're actually on
track. We haven't introduced any bugs.
So, I'll do console.log and logging out
count L. And if we bring up the console,
we can see actually we're getting what
looks like an HTML element in the
console. Hm, that's pretty interesting.
Now, it's actually not a real HTML
element. It's more of a representation.
It's a model of an HTML element and it
gives us a lot of other opportunities to
change it using JavaScript as well. But
for now it's it's perfectly fine to
think of this as just yeah that is the
element that we are seeing right here.
It's a JavaScript representation of that
element because remember we said hey
HTML document why don't you get me the
element that has the ID of countell and
the HTML was like yeah sure just take it
and then you store that in the countell
variable and now you as a JavaScript
developer have control over this element
and you can manipulate it which is
exactly what we're going to do down here
at line 14 because now we're going to
move on to the second part of this big
expression We're just modifying the
element itself.ext
equals 5. Or in our case, how would we
write this? Well, we've turned this big
thing here into this variable. So, we'll
use this variable and do dot inner text.
That is inner text like that. Equals.
And we're not going to set it to five
because that would make any sense. What
are we going to set it to? Think a
second about that.
Indeed, it is the count because we know
from console logging earlier on that the
count does indeed increment correctly.
And now we're just saying that we want
the inner text. That is the text on the
inside of the tags. We want it to be the
count. We'll run the code. Hit the
increment button. And yes, it works. Now
we are incrementing. We are counting
people. Really good job reaching this
point. and your JavaScript skills are
starting to become dangerous as you now
know how to create interactive apps. Now
before we move on, there's one thing I
want to mention which is that this get
element by ID here and this.log
both are functions in the same way our
increment function is a function. Now
they are hooked onto the document object
and the console object, but that's just
a nuance. There's still functions or
methods as we call functions that are
hooked onto so-called objects. If that
sounds confusing, it's because it should
be at this point because you haven't
learned about objects yet and methods on
objects. But don't worry about that.
Just think of them as functions. And
actually, the thing we've been doing
here of adding something in between the
parentheses of the function when we are
calling it is called to pass in
arguments. which means that we are
giving the function some data to work
with. Like here, we're specifying the ID
of the element we want to grab.
Otherwise, if we didn't do that, this
function would just be running in the
wild and not know what to look for.
Likewise, in this log here, we're
passing in which variable we want to log
to the console. If we didn't do this,
the log function wouldn't know what to
log. So speaking in analogies, if the
function is a chef that are to cook a
meal for you, then the arguments you
pass in are the ingredients the chef
uses to cook that meal. So hopefully
that made everything a bit more clear.
Let's clean up this code and move on.
Now before we move on, I want to talk a
little bit about what we're doing here
where we're saying document.getelement
get element by ID because you'll often
hear developers refer to this technique
as working with the DOM. And that's one
of those threeletter acronyms in web
development that sounds super
complicated and I was really confused
about this when I was learning to code
and I want you to avoid that confusion.
So, let's just unpack this expression.
It's short for document object model.
That sounds really fancy. However, it's
actually just how you use JavaScript to
modify a website. It's just that people
naming this wanted to give it a really
abstract name. But if we look at each of
the words separately, the name actually
does make sense. So, let's do that.
Starting with a document. Why that?
Well, that's because what you are
interacting with is an HTML document.
Object. Well, that's because the
document keyword in JavaScript is of the
data type object. meaning they've taken
the HTML document and shoved it into a
JavaScript object. Of course, that's a
little bit of a simplification, but
that's the essence. So, this word
document is of the data type object.
Now, you haven't learned about objects
yet, though you're going to do that a
little bit later. Finally, there's
model. Model is basically a
representation or a modelation, kind of
like, you know, there's the real Saturn
5 rocket that NASA built and then
there's the Lego model. And that's how
it is here as well. There's the real
HTML element which you've written in
your HTML file. Then there's the
JavaScript model, the representation you
have of this element in JavaScript.
So don't be frightened when people talk
about the DOM just in your head
translated to how to use JavaScript to
modify websites.
And with that out of the way, let's move
on with the coding.
Or actually before we move on, I have to
admit that I fooled you a little bit
because the problem with the previous
lecture was that I wrote the code and
that's just wrong. You are going to
write the code. That's how you learn. So
I have removed the two lines of code
that I wrote. Namely grabbing the
countell element and storing it in a
count variable and also setting the
count else inner text to the count. So
just do that and if you struggle just go
back and look at my solution. So, no
need for me to type this out for you yet
again. Just go ahead and give it your
best shot and refer to that if you need
help. And I will see you in the next
lecture.
Okay, it's time for you to build some
muscle memory cuz you are going to
create a save button and a save
function. You're going to start with a
function. Here is the instructions.
Create a function called save which logs
out the count when it's called. We're
going to modify this to do some other
stuff afterwards, but as for now, I
wanted to just log out the count.
Secondly, in the HTML, you are going to
create the save button. You are to set
the ID as save btn and have it call the
save function when it is clicked. So, go
ahead and do this right now and then
return back to the screencast when you
want to see my solution.
Okay, hopefully that went well. Let's do
this. We'll do function save open and
close parenthesis. Then the body of the
function will console log the count. If
we want to check that this actually
works now, we can call the save
function, run the code, and we can see
yes, that the count is indeed logged out
as a consequence of us calling the
function here on line 15. Okay, let's
remove that. Going over to the HTML,
we're going to create the save button
and we do open button tag.
Then we'll just close it right away.
I'll write save in between the tags and
give it the ID of save.btn.
Now we can actually run it to see the
dependency. Yes, it indeed does. But
nothing happens when we click it. And
that's because we have to do on click.
We want to run the save function. Here
we have to invoke it with the open and
close parenthesis otherwise it won't
call the function. So rerunning the
code, hitting save. Yes, there we are
logging out zero because of this line.
Now we can increment a couple of times
and see. Oo, now we're logging out six.
Pretty nice. Now we're starting to get
somewhere. It's only a matter of saving
the count to the website as well and
rendering it out under the buttons. But
before that, there's some new concepts
you need to learn. So I'll see you in
the next cast.
Okay, so now it's time for us to build
this little functionality here which
allows us to save the number of people
who have entered into the subway at each
of the departures. However, this
requires you to work with this full
paragraph which actually includes both
numbers which you can see but also like
regular text, previous entries and some
symbols as well. And once you move away
from just straight out numbers in
JavaScript, you're moving into the land
of so-called strings. And strings is a
data type in JavaScript. And in case you
wondered, yes, numbers is also a data
type. And if you think the word string
is a little bit weird, well, you can
also think of this as just text. The
strings you work with in JavaScript are
normally meant to be text that the user
reads on the page. Now, we have been
working with text here before. We've
hardcoded in some text here and there in
the HTML. And actually, as far as HTML
knows, everything is strings. Doesn't
even know about numbers. HTML can't do
calculations for you. So, when I'm
talking about strings and numbers, I'm
talking about it in the context of
JavaScript. And even though you're
probably not even aware of it, you've
actually written a string already up
here on line one. You've wrapped the
count d-l inside of two quotes. And that
is how you create a string in
JavaScript. But you've had far from
enough practice to start working with
strings in our people counter app. So in
the next lesson, we're going to go over
the basics of it and then you're going
to solve some challenges so that we
build up that muscle memory and make you
capable of using strings in our app.
I'll see you there.
So strings are used all over the place
in software. Let's say that you for
example are building an app where people
can authenticate. Well, then most likely
you'd have to store some kind of
username for your users. That would most
likely be stored as a string. For
example, pair wrapped in double quotes.
Then this string just like we did with
numbers will be stored in a variable. So
let username equals pair. Now we can
console log out username
and we get pair logged to the console.
Now, one thing I want to mention is that
I'm using double quotes here to create
this string. I could use single quotes
instead if I want to. You can see that
works. However, I cannot start with a
single quote and end with a double
quote. As you can see, now our editor
tells us that this syntax is off. And if
we try to run the code, we are getting a
syntax error. So, you got to stick with
one or the other. And I usually use
double quotes, but that's just a matter
of preference. If you prefer single
quotes, go ahead. Now, I want to give
you a challenge.
Here you are to create a variable
message that stores the string. You have
three new notifications.
Go ahead and do that right now.
Okay. Hopefully that went well. Let's do
this. Message equals
this string right here. Now, what we
want to do here is combine this string
with this string because this string
right here is probably the result of us
checking in the database and seeing that
oh, there's three new notifications that
pair hasn't seen. So, render out this
string right here. And then, of course,
we need to combine it with the name. So
what we want to do is message plus
username like that. When we add a plus
here we so-called concatenate the two
strings together. So let's have a look
at what happens when we run the code
becomes this slightly ugly string. We
have three new notifications pair with
no space in between the strings. So it
kind of becomes like this
isn't really what we wanted. What we
then also can do is add a third string
in between here which includes a comma
and a space. Now if we run it, you have
three new notifications pair. And maybe
we even want to add a exclamation mark
at the end like that. As you can see,
when we use the plus for strings, it
simply concatenates the characters
together. Okay. Now I have a new
challenge for you.
You are to create a variable message to
user that contains the message we have
logged. By that I mean the entire
message in here. This should be a new
variable and then afterwards I would log
it out just to make sure that you have
configured the message to user in the
correct way. Go ahead and do this right
now.
Okay. To do this you do let message to
user equals message plus comma space
plus username plus exclamation mark. Now
we can replace all of this with message
to user and running the code again we
get the exact same string which is just
what we wanted. Great job.
Okay, time for another challenge. You
are to create two variables, name and
greeting. And the name variable should
store your name. And the greeting should
store, for example, hi, my name is. Then
you are to create a third variable, my
greeting, that concatenates the two
strings. And finally, log the my
greeting variable to the console. So
this is just to give you some muscle
memory. Go ahead and do this right now.
Okay, let's do this. Maloo let name
equals pair Harold organ. Then let
greeting I my name is and then a space
at the end. And then finally let my
greeting equals greeting plus name. Then
to see how this went we do console log
my greeting.
Running the code. Yes, we get, "Hi, my
name is Pier Harold Borggan." Now,
notice that here I added the space
inside of one of the strings as opposed
to doing something like that. However,
doing that would work the exact same
way. So, great job solving this
challenge. Let's move on.
Okay, now let's mix it up a little bit.
Let's say that my name isn't Pier Harold
Borgen. It's actually 42. A strange
name, but anyway, what do you think will
happen when we have one number and one
string? What will the result be? Let's
do it. It's actually, hi, my name is 42.
So, the 42 here has actually been turned
into a string that has been added into
like this. And that's kind of what
happens in a wrestling game. Between a
string and a number, the string always
wins. Now, this was a little bit of a
weird example. So, let's take one that's
a bit more interesting. Let's try let
points equals four. And then let's say
bonus points. This is some kind of game
that is 10. Let total points equals
points plus bonus points. Total points.
Now, what do you think we'll log out
here? I'm going to comment out this one
right here. Take two seconds to think
about what the result will be.
Okay, let's run this. The result is 410.
That is weird because perhaps you
expected 14. No, as I said, the string
always wins. So when we say 4 + 10,
which is the values of these variables,
the four will be turned into a string.
And the result when you concatenate two
strings is just pretty easy to see by
just removing this fluff in between
them. 410. If the numbers won the
wrestling game would be like this. and
it would indeed log out 14, but it
didn't. So, I'm going to give you a
couple of challenges here. What do you
think these four control logs will log
out? Take a minute to think about that.
Potentially write your solutions in a
bold comment either above or perhaps
even after each of the lines and then
return back to the screencast and then
we'll have a look at it together.
Okay, here we have a number and a
number. So this will end up as a number
as well. 4 + 5 is nine as far as I can
remember. Here a string plus a string.
Even though they are number characters
inside of the strings, that doesn't
matter. JavaScript still sees this as a
string. And the result will be a string
of 24. Here wrestling match between a
string and a number. Sadly the number
always loses. So this will be 51. And
finally again wrestling match between a
number and a string. The result is
always that the string wins. So this
will be 100 100 like that. Let's run the
code. Yes, as you also can see from the
console, the way the scribble console
highlights the difference between
numbers and strings is with the color.
The strings are white whereas the
numbers are blue. So great job. Let's
move on.
Now you've had a chance to work with
strings. We need to talk about a common
problem that you might already have come
across. I've got a string here that
says, "I'd love to see the show The Lion
King." Let's just hit save. And we're
successfully logging that out. So, it
works. But what if I wanted to emphasize
the fact that The Lion King was the name
of the show? In that case, I might want
to put that in quotation marks. As soon
as I do that, you can see the red
squiggly line, and that tells us we're
about to get an error. Let's save. Yeah,
there we are. It says unexpected
identifier the Well, that tells us that
it's running into a problem right here.
And the reason this is happening is that
to JavaScript's rather basic brain, the
string is starting right here with this
quotation mark and it's finishing right
here with this quotation mark. So if
this is the string, then what on earth
is all of this? Well, at that point,
JavaScript gets confused and it throws a
tantrum, or rather it throws an error.
Now, we could potentially get around
that by changing these quotation marks
to inverted commas. But look, now we've
got loads of red squiggly line. Let's
just hit save. And now it says
unexpected identifier D. And the problem
that we've got is that JavaScript thinks
the string starts with this inverted
comma and actually finishes with this
apostrophe. Because to JavaScript, the
inverted comma and the apostrophe are
exactly the same thing. Now, if I
removed this apostrophe, well, look, the
red lines disappeared, and that actually
works. But that's not good enough. We
don't want to have to make a typo in our
string just to avoid an error. So, we
need another solution here. And the
solution is the escape character. Let me
just put that apostrophe back in. Now,
the escape character is the backslash.
And I put it right in there. And you'll
see the red line has all disappeared.
Let's hit save. And now I could do the
same thing if I went back to our
original quotation marks. The apostrophe
now doesn't need an escape character. So
I can delete that. But we do need an
escape character right here. And a
second one right here. Now let's hit
save. And we get the same result. It is
working. So what exactly are these
escape characters doing? Well, what the
escape character does is it tells
JavaScript that the next character
should not be seen as a string
delimiter. So the next character is not
starting or finishing a string. It's
just part of the string. So whenever you
run into the problem of having to use a
quotation mark or an apostrophe inside a
string and you get an error because it's
clashing with your string delimiters,
i.e. whatever you're using to open and
close the string. Well, you can use the
escape character to tell JavaScript that
the character that comes next is not to
be treated as a string delimiter. It's
not to be treated as a piece of code.
It's just part of the string. Now, as
you spend more time in coding, you will
come across escape characters more and
more. But that is just a really quick
introduction to them to solve that
really common problem. Okay, I just
wanted to bring that to your attention.
Now, let's get back to the main project.
Okay, it's time for a challenge. This
time you are going to create a feature
that we already have on Scrimma because
on the dashboard we welcome our users by
saying welcome back Pier Harold Borggan.
That's a normal thing a lot of apps do.
So it makes sense to try to implement it
and the way you want to do it is through
grabbing a hold of this paragraph here
which I've given an ID of welcome-L.
That is the first part of the challenge.
You are going to store that paragraph in
a variable called welcome L using this
camel case convention. And then you are
to create two variables name and
greeting that contains your name and the
greeting we want to render on the page.
In our case that is welcome back.
Finally you are going to render the
entire welcome message using welcome
liner text. So go ahead and give this
one a shot and then I will show you the
solution when you return back to this
cast.
Okay, let's do this. We'll start by
grabbing the welcome L paragraph. Do let
welcome L equals document.getelement
by id passing in the id we have here
like that. Now we need to create the two
variables. Let name my name is pair
harold.
Let greeting
clear welcome back and finally rendering
it out. That is done by doing welcome l
dot inner text
equals meeting plus name. Now if we run
this we can see we are rendering out
welcome back pier Harold Borggan but
there's no space between welcome back
and my name that can easily be solved by
just adding a space after welcome back
or we could of course hardcode it in in
between these two variables when we
concatenated the string as well but that
doesn't really matter that much in this
case. So let's just stick with this and
move on to the next lesson.
Okay. Now, there's one more thing we
want to do because personally, I think
this welcome message could be a little
bit more lively and fun. And what would
help there is to add an emoji. For
example, this emoji right here that will
look pretty nice. So, I want you to add
that to the welcome element inner text.
However, there's a catch. You are not
allowed to modify the code over line 9.
You have to write it below. So, the
question is, how can you add this emoji
to the end here in the browser? by
simply writing more code, not by
modifying any of the existing code. And
to give you a hint, you need to remember
back what you did when you incremented
numbers. This technique here, you can do
the same thing with welcomel.in text. So
go ahead and give this a shot.
Okay, hopefully that went well. If not,
no worries. We'll do this together. What
we're simply going to do here is instead
of using count, we're going to use
welcome liner text. And instead of the
one, we're going to use an emoji. So
here we'll do welcome liner
text equals the old welcome
else inner text plus this new emoji
right here. And an emoji is just a
string like all other characters. It's
just rendered out a little bit fancy.
So, if we run this, you can see yes, we
indeed get the emoji at the end. Now,
before we leave, I want to teach you a
little trick because this line here is
pretty clunky. Writing the welcome liner
text twice. It's not something you'll
see out in the while because there's a
shorter version of writing this and that
is simply using the plus equal operator.
What it does, it takes the current value
you have and then adds this one after
it. So it's the exact same thing that
you were doing here. Seem a bit weird to
do this plus equal sign here. Just try
and remember that it simply takes what
you have here and adds this to it. So
that after this line, welcome.ext
consists of the entire thing here.
Okay, with that in mind, let's go back
to our passenger counter app.
Okay, just a minor thing before we start
using strings in our app. I want you to
use the plus equals technique instead of
this slightly longer count equals count
plus one technique. Go ahead and do
that.
Okay, hopefully that went well by simply
doing like this. Count plus equals 1.
Run the code. Yes, it works. Let's move
on.
Okay, so now you are going to solve a
super cool challenge. You're going to
take everything you've learned about
JavaScript up until now and use it to
create this save feature. You can see it
says previous entries and then four, 5,
11 with dashes that separate them. And
these are, of course, added every time
you hit the save button. Now, there's a
dash after the three numbers as well.
Don't worry about that. It might look a
little bit weird, but it's the easiest
way to do this, and we want to do it the
easiest way now that you're building
stuff like this first time around. We
don't want to worry about details. Make
it work first, then make it better.
That's my philosophy. So, we're just
going to make this work. Okay. So, your
three challenges are listed as comments
here in the JavaScript. And in the HTML,
I'm going to write the paragraph for you
so that you have something to work with.
So, this is a P tag and it should say
previous entries like that. And I'm
going to give it an ID equals save L for
save element. And then you are going to
grab the save element paragraph and
store it in a variable called save l.
And then in the save function which is
triggered when the save button is
clicked on you are going to create a
variable that contains both the count
that is the number and the dash
separator. So it'll look something like
this like 12 space dash space. And then
you are to render this newly created
variable into the save element using
inner text. However, you must make sure
that you don't delete the existing
content from the save l paragraph
because you want to concatenate the
string, not wipe it out entirely and
replace the paragraph with the latest
entry. So, you got to remember your plus
equal, but luckily you know how to use
plus equals and that might be helpful in
this situation. Finally, one thing I
want to mention is that you might
struggle a little bit with the spaces
here and it might end up slightly
different than what you can see here and
that is perfectly okay. Don't worry
about that. In the next cast actually,
we are going to do some searching online
to figure out if there are some
alternative methods we can do this on
which will help us keep these spaces.
That's a critical part of web
development, searching for solutions
online. So, I've left a little gotcha
for you here so that we get a nice
little problem to solve using Google and
some online docs in the next cast. So,
don't worry about that. Just go ahead
and give this your best shot. And if you
need help, just revert to this
screencast and I will show you the
solution as well.
Okay, hopefully this went well. If not,
no worries. Let's do it together. First,
we are going to do let save L. That'll
be document
getelement by id and we are going to get
the save d-l like that. As always, I
like to console log just to see that I
have the correct element fetched. Let's
run this code. Then yes, bringing up the
console, we see that we have grabbed the
correct element. Then we can move on.
First, create a variable that contains
both the count and the dash separator.
So, what are we going to call this
variable? I intentionally didn't give
you a name for it because I wanted you
to think what could be a suitable name
for this string. And there's no right or
wrong answer, but one thing you could
call it would be count string or count
str like that. So, it is the count, but
we've turned it into a string. So a
count string kind of makes sense and the
value of it will be count plus and then
the string. I'll add a space a dash and
a space. All right. Three render the
variable using inner text. Okay. Let's
do it down here. We'll do save l dot
text. Then you might have been tempted
to do equal count r but that won't work.
Let's just try it. Run the code. We
increment and we hit save. Boom. Then we
suddenly remove the previous entries
text. We cleared it out, of course,
because we set all of the inner text to
be just this count string. That doesn't
work. We have to take into account what
was there previously as well. So we add
plus equals like that. Increment,
increment, increment. Save. Yes, that
works. All right. So it works really
well actually. However, this dash at the
end here is gone as is our space here in
the paragraph. And that is a bit weird
because we want to keep those spaces.
However, it's also a great opportunity
to do some searching online in order to
solve this problem which is a critical
skill for any developers, namely finding
answers online. We'll do that in the
next cast.
Okay. So, we have this little problem
here, which is that our spaces aren't
working in the way we want them to work.
Basically, this space here gets lost on
the way somewhere. I don't know where
but um it happens. How do we go about
figuring out what's wrong? Well, looking
at these two lines, which is where we
deal with this save element paragraph,
you can think what would be a suspect?
Which tool that we're using would be a
good suspect.
And I'm going to point to this one right
here cuz I happen to know that that is
actually the guilty one, the inner text.
And let's say you' guessed that as well.
How do you go about finding another tool
that doesn't work this way? Well, one
thing you can do is you can write inner
text alternative and then MDN. Put that
into Google and see what it returns. And
MDN is short for Mozilla Developer
Networks and it's a great resource. So
if we do that, type that into Google,
you can see the very first result is
node text content. Hm. Okay, that's
interesting. Let's click into that.
There we land on an interface you're
going to get very familiar with the MDN
web docs. And it says node.ext content.
Don't worry about this node here. It's
kind of fancy way of saying HTML element
sort of, not entirely, but just
translate it to HTML element right now
and you'll be good. And then you can
read the text content property of the
HTML element represents the text content
of the element. Okay, seems pretty
useful to me. And then we can scroll a
little bit down and then you see they've
actually listed up differences between
inner text and text content. And here if
we scan through this you can see that
inner text only shows human readable
elements. And yet again inner text is
aware of styling and won't return the
text of hidden elements. Okay. So it
seems to be that the inner text somehow
struggles a little bit to return things
that are hidden or not human readable.
And like yeah, a space is certainly not
that human readable as a character. So
maybe we can just change it then to text
content instead. Let's try that. Text
content
code increment. Increment increment.
Save. Oh. Oh yes indeed. Now we get the
spaces. And one thing I want to
highlight here is that while the Misilla
WebDocs is a great resource, it's a very
comprehensive, the language you'll see
in these kinds of documentation sites is
often very technical, which is why I
kind of skimmed through and found some
hints of human readable and hidden. And
I kind of didn't worry too much about
going into depth. And I think you as
well should think when you see a page
like this, don't feel that you need to
understand everything. And for the love
of God, don't try to learn to code by
just reading documentation. Some people
say that's a good idea. I think it's a
terrible idea. You want to slowly learn
how to get comfortable reading
documentation, but you also want to
ignore all of the complex stuff that
it's very often wrapped within. And
there are other sites as well that
explain things in various degrees of
difficulty. For example, there's also
something called W3 Schools, which is
very good. There's Stack Overflow and
many more. And you're going to see more
of this as you progress in the course as
we're going to come back to this
debugging concept later on as well. But
as for now, we've just had a little
sneak peek on the Mozilla developer
network. And actually, it's kind of
trash talking the text here and saying
that it's also computationally
expensive. So to me, I've gotten enough
hints that I actually want to use.ext
content instead of inner text. I'm going
to change this up here as well.
Like that.
It still works. Great. Now our app is
almost complete. There's only one more
thing we need to do and we'll do that in
the next cast.
Okay. So now we are going to put the
final pieces in our puzzle because we
need to fix the fact that if I increment
this and then hit save and then the
subway leaves the station and I'm going
to count a new group of people then with
the current solution I will be counting
further on from six like 7 8 9 and
that's not what we want. We want this
one to be set back to zero every time we
hit save. And to do this you need to
write two lines of code. And at this
point, I want to just throw you into the
deep end of the pool and see how you
swim. So, I'm not going to give you any
hints or write any pseudo code or tell
you where you're going to write these
two lines. Now, you really have to think
and think for yourself, what do you need
to do here in order to make this work?
And feel free to hack a little bit on
it. Try a solution, run the code, see
how it works, console log out the
values, and just hack away at it and
give it your best shot. And if you don't
manage to do it, no worries at all
because I will show you how to do it
again, of course, when you return back
to the screencast. So then you can just
try to do it again after you've seen my
solution. Anyway, enough talk. Jump into
the code here and give this a shot.
Okay, now let's solve this together. So,
what we need to do is write a couple of
lines in this save function because this
is where the magic should happen because
it's when we hit the save button that we
want this value to be set back to zero.
And how do we set this element to be
zero? Well, it is the count element
which we've worked with previously. And
setting it to zero, we know how to do
that. We can do count L dot text content
and set that equal to zero. Like that.
If I rerun the code, increment,
increment, increment, increment, hit
save, and yes, it's now zero. But if I
now try to hit the increment again,
you'll see, oh, five.
That's weird. Why is that? That's
because we have indeed set the text
content of the count element to zero.
So, this H2 right here. But in the
JavaScript, we're still keeping track of
the count. And that count has been
incremented this point five times. So
what we needed to do here when we set
the text content to zero is to also set
the count back to zero as well because
JavaScript remembers what we've done
previously unless we refresh the entire
browser. So even though JavaScript can
see that yeah you are setting the text
content of this element here to zero it
still keeps track of the count here and
JavaScript can't deduct that since you
want to set this here to zero you also
want this to zero. No, you got to give
JavaScript very specific instructions.
So you need to do here count equals zero
as well. Now if we run the code bam bam
bam save it's set to zero. And yes we
indeed start at zero again.
Oh hey it's Gil from Scribba. I bet you
didn't expect me here. P just wanted me
to pop in real quick to share something
important with you. Thanks Pere. So in
this section, we started off by using
the onclick event handler HTML attribute
to call and run functions like increment
and save when their corresponding
buttons get clicked. This is an
intuitive and straightforward way to
understand how event listeners can be
attached to HTML elements and grasp the
basics of interactivity. But in larger
realworld projects, mixing HTML
structure and JavaScript behavior like
this can lead to code that is harder to
maintain and manage. So keep this in
mind and real soon we'll teach you the
more recommended and maintainable
approach for registering event listeners
and executing JavaScript. And this app
is looking outstanding by the way. So
great job getting this far.
So you have built your very first
JavaScript app from scratch.
Congratulations. That is a huge
accomplishment. So give yourself a pat
on the back. You have really learned a
lot in this section. And to prove that
to you, we're actually going to do a
little recap so that you get to see in
front of you all of the new concepts
you've shoved into your brain at this
point. So we started out with the script
tag. If you remember, we had it here in
the index.html HTML file and we wrote
our JavaScript internally in this file.
However, we realized that that wasn't
the way we wanted to go. So, we instead
pointed this script tag to a source,
namely the index.js file, so that we
could write our JavaScript there
instead. Then, we moved on quickly and
learned about variables, namely the let
variable, and we set its value to a
number, which brings us to your very
first data type, numbers. You learned
how to work with numbers, do
mathematical operations, and even
increment numbers here on line six, and
also reassign the count variable to zero
here on line 14. So, a lot of work with
numbers in this app, but you also
learned about strings, which was your
second data type. Here you added a
string to build the save feature, and
also you console logged out a few
strings along the way with code that
we've deleted now. So I added this
console log here to remind you about it
because that is a truly important
concept in developing and you will use
it throughout your entire career. Also
another thing you will use constantly as
a developer is functions. You learned
how to create functions like this
increment and save function and also how
to call them aka invoke them from the
HTML file here on the button clicks. And
that brings us to the next concept which
you learned a ton about which is the DOM
or the document object model or what I
like to call it how to use JavaScript to
change the website.
So we interacted with the DOM at several
places. For example, up here where we
did document.getelement
by ID. That's one you are going to get
very familiar with throughout this
module. We passed in the ID as a string
so that the JavaScript was able to head
into the HTML and fetch out the content
you told it to look for. And another way
we interacted with the DOM was through
the inner text property. We used that
here instead of text content. But then
we Googled our way to the Mozilla
developer network realized that actually
text content is a better solution. So,
we learned about that and oh my god,
this is a lot of concepts to learn. I
can totally understand if you're a
little bit overwhelmed and if you feel
that you are on thin ice on a few of
these concepts, that's totally okay. We
are going to repeat all of these things
over and over again throughout the
module. So, don't worry about that.
Actually, we are going to jump directly
into a section where you are going to
get assignments that test you for these
skills just to give you that repetition
and muscle memory you need in order to
make these concepts become second
nature. So, let's go ahead and get some
repetition and then once we've done with
that, we'll move on to our next big
project, the Blackjack app, which is
going to be so much fun to build. So, I
can't wait to see you there. For now,
let's jump into a few quickfire
challenges.
Okay, in this challenge you are going to
repeat variables. So first you are going
to create two variables first name and
last name. And obviously I want you to
set their values to your first name and
your last name and then concatenate the
two variables into a third variable
called full name so that you have your
full name. Finally, log full name out to
the console. Go ahead and give this a
shot right now and then I will show you
my solution when you return back to this
screencast.
Okay, let's do this. I'll do first name
equals pair and then I'll do let last
name equals Borggan like that. Then we
need to concatenate the two together.
Let all name equals first name plus last
name. Like that. Finally, I want to log
it to the console. Console.log
full name. Now I can run the code. And
as you can see, we're logging Pier
Borggan to the console. But there's no
space in between the two names. So I
could achieve this in a multitude of
ways. either by adding a space here. Now
it's better or adding a space before
Borggan still good. However, neither of
these are optimal since I want the first
name and last name to be the name and
the name only, not an extra space. So
what I'll do instead is in between here,
I'll add an extra space like that.
Running the code. There we go. Now first
name and last name contain only the
characters of my name. But we still get
the space by adding this third string
here when we create full name. So no
matter how you did it, great job. Let's
move on.
In this challenge, you are going to
create a function that logs out hi there
Linda when called. And you have to use
the name and the greeting variable
inside of the body of your function. And
also there's a comma and an exclamation
mark you also have to include so that
you get this exact string log to the
console. So go ahead and solve this one
and then I will show you the solution
afterwards as well.
Okay, let's do this. Create a function.
I'll call it greet Linda. Like that here
we are going to console log first the
greeting. Then we have to plus the comma
and the space and then plus again then
the name and then plus again and then
the exclamation mark like that. Final
step to check our code we need to run
it. Greet Linda like that. Running the
code and there we see this exact string
in the console. Great job.
In this challenge you are going to
practice your incrementing and
decrementing skills. So let's say that
you are building a game and the player
is collecting points which currently are
at three and you need to create two
functions. One which is called add three
points and one which is called remove
one point and of course then have these
add and remove their given points from
or to the my points variable. So remove
one point should decrease my points with
one. Add three points should add three
points to the my points variable. And
your job is to create those two
functions and then call them however
many times you need to call them in
order for this line down here to log out
the number 10. So you are going to write
your code in between here. You can add
as many lines as you want. But when you
eventually run the code, my point should
be 10. And you should only have changed
my points via calling these two
functions a number of times. So go ahead
and give this one your best shot and
then I of course will show you the
solution afterwards as well.
Okay, hopefully that went well. Let's do
it together. We'll do function add three
points. What that'll do is take my point
and plus equal three
function remove one point
and that should do my point minus equal
one like that. Okay, now we have them
which means that we just need to call
them a couple of times. We are at three
and we need to get it up to 10. What I
will do then is I'll first do add three
points. Call that three times because
then we'll go up to 12.
My calculation is correct. Let's run
this. Yes, we are indeed logging 12.
That means that we can remove one point
two times. That should get us to 10.
Yes, we are logging out 10. So, we have
solved the challenge. Great job.
Okay, it's time for some strings and
numbers repetition. You are going to add
a bunch of strings and numbers together
and try to predict what the result is.
So here after each one of these lines,
I've added two slashes. That means that
you can write your prediction. Try to go
through all of these and then run the
code and see how you did it. And then I
will give it a shot afterwards as well.
Good luck.
Okay, starting on the number two, it is
a string plus a number. We know that
JavaScript then turns both of these into
strings. So, it becomes 22. Here, 11 +
7, that's just two numbers. We get 18
and as a number this time and not as a
string. 6 + 5. You might be fooled to
think that JavaScript would treat this
as a number calculation since the number
is first. But no, actually the string
wins and it becomes 65. Likewise with
this one here, it's very natural to
think that what you want to do is add
these two points together so that it's
my points is 14. But no, since you have
a string here, everything will be turned
into a string and the result will be my
points colon 59. 2 + 2 that's four just
a number and finally two strings of
course becomes a string 1114 like that
let's run this and indeed all of my
predictions were correct that was good
otherwise I would lose all my integrity
so I'm really happy about that let's
move on
okay let's imagine that you are working
on an e-commerce website and this
website sells shoes so here you can see
a Nike shoe with a purchase button. The
HTML is located here, of course, with an
image tag, the paragraph or the Nike
shoe, the button, but then there's also
an empty error paragraph. And this is
pretty common because in case there's an
error going happening, for example, the
purchase didn't go through or some kind
of bug caused the operation to fail or
whatever it is, we want to render
something inside of this paragraph. So
that is your job in this challenge. You
are going to set up the JavaScript so
that when the user clicks on the
purchase button, you are to render out
something went wrong. Please try again
in the paragraph that has the ID of
error. That's of course this one. And
the button they will click is of course
that one. You can see there is some CSS
here, but you're not going to worry
about that because this challenge is all
about the JavaScript and of course also
hooking up the onclick event listener on
this button. You should know how to do
that by now and you should also know how
to render text inside of a paragraph. So
go ahead and give this one a shot and
then of course I will show you the
solution afterwards as well.
So there are two parts to this
challenge. First there's registering
that the user clicks on the button and
then there is rendering out the text in
the DOM. So let's take first things
first the button. So I'll head over to
the button and do an on click event
listener. And here we need to specify
which function we want to run. We
haven't written any function yet. So
let's do that. We'll do function. I'll
call it purchase because that's what the
user is trying to do. Open and close
curly brackets. Now I can take this
function and specify that when the user
clicks the button, we want to call this
function. Good stuff. Let's just console
log out button clicked.
Run this code. Try to click the button.
And yes, we are rendering out button
clicked. Good. The next step is to grab
a hold of the paragraph that we are
going to render text inside of. So we'll
do let
error paragraph and document.getelement
by id passing in error like that. Yet
again, we can do console log just to
verify that we have grabbed a hold of
the right paragraph. We run the code and
bring up the console and yes, we can see
that we have the paragraph.
Then inside of the purchase function,
this is where we want to trigger the
rendering of the error text. I'll do
error paragraph dot text content
equals
this string right here.
Now, if we rerun the code, click the
purchase button. Yes, something went
wrong. Please try again. And as you can
see, it's rendered out in red text,
which is very common because we want to
grab the user's attention and tell them
that something wrong has happened.
That's of course something we have
specified in the CSS, but this challenge
is not about CSS. It was about
JavaScript. So, great job solving this.
Let's move on.
Okay. In this challenge, you are going
to create a very basic calculator. As
you can see in the mini browser here,
I've set up the UI for you. Here we have
two buttons. Here we have the numbers 8
and two. And then there's four buttons.
Add, subtract, divide, and multiply. If
I try to click one of these now, nothing
happens. But what you then of course
want to happen is that for example, if
you hit subtract, you are to take 8
minus 2, which is six, and then render
that out in the sum here. so that it'll
say sum colon 6. Taking a look at the
HTML, we can see that the numbers at the
top num 1 L and num 2 L that's the 8 and
two are simply spans. However, they are
empty. So, how come we have content in
them when they are rendered? Well, that
is because I'm doing that in the
JavaScript. First defining two let
variables num one and num two and
setting them to 8 and two. And then
doing document get element by id
fetching out num 1 L and num 2 L and
setting each of their text contents to
num one and num two. And that's why
we're rendering out these numbers.
Moving on to the buttons, you can see
that they are very simple. Only the tag
and the text. I have not created the
event listeners because that's your job.
And then finally the span with the id of
suml is where you are going to render
out the result of the calculation.
So summing this up you are to create
four functions here in the JavaScript
file. Add, subtract, divide and
multiply. And then you need to call the
correct function when the user clicks on
one of the buttons. So obviously when
the user clicks the add button you are
to call the add function and vice versa
with all the others. Then inside of the
functions you are to perform the given
calculation. So in add you need to add
num one and num two together and
subtract you will take num one minus num
two and divide and so on. Finally render
the result of the calculation in the
paragraph that has the ID of sum.
So go ahead and give this one a shot and
then of course return back to the
screencast when you want to see my
solution.
Okay, let's do this. I'm going to start
by creating here an on click
event handler and call this function add
because when the user clicks the add
button we want to invoke a function
which are to be named add and then we
need to create the function function add
here what do we want to do
first we just want to check that it
works
the code add yes we are logging out now
then we need to grab a hold of the sum
paragraph. So I'll do let sum l equals
document getelement by id and the ID is
sum-l like that. Here I'm just going to
see if it works by doing sum l.ext
content equals
num 1 + num 2.
Rerunning the code. And yes, there we
can see we're rendering out 10. However,
we wanted to render out some colon
equals 10. So, let's try adding that sum
colon plus num one plus num two.
Rerunning the code, hitting add. And now
we have come into a situation where
actually JavaScript is turning all of
our three values into strings. As you
remember, when you add strings and
numbers together, they're all turned
into strings. What we need to do here is
first perform the calculation and then
concatenate that with the string. So
we'll do let result equals num 1 + num 2
because here we're only adding numbers
together and JavaScript will will treat
them as numbers and thus perform a
mathematical calculation and not a
string concatenation. So then we can
replace this expression here with
result. Rerunning the code, clicking
add. And yes, there we go.
Great stuff. Now, I'm a little bit lazy,
so I'm just going to copy this, paste it
in, and do subtract, and simply replace
this with a minus.
Add the on click equals subtract
like that. Running the code, clicking
subtract. A sum is six. Brilliant.
Now, we'll just
copy paste again.
divide
should get this. Use the division symbol
and multiply should use the
multiplication symbol. Now we only need
to add the last two event handlers.
Divide is the name of that function.
A space here. And multiply is the name
of the final function. Running the code.
Trying add gives 10. Brilliant. Subtract
gives six. That's correct. Divide
rightfully gives four. And multiply 16.
We have made a calculator. Good job. You
have all reason to be proud of yourself
now. And if you didn't manage to do this
one, if you found it a bit too hard, now
that you've seen the solution, just go
back and try it again. That's great
practice and gives you the muscle memory
you need. And when you're ready, I'll
see you in the next cast.
In the next scrim, you will be given a
solo project. That means a project that
you are going to build all on your own.
So I will not be there to help you
unfortunately. Now before you jump into
the solo project, there's one thing you
need to know about and that is the
service called Figma. It is a design
tool that is heavily used in the
industry these days. So it has almost
become a must-have skill for any
front-end developer working in a team
with designers. You will be given the
solo project design in a Figma file. So
click on this image right here and
create a Figma account. And then once
you're ready, you can click on this
image as Bob Zol here at Scribba has
created a quick walkthrough of Figma for
students who are doing solo projects
here on Scribba. So if you get confused
about the Figma interface, just revert
back to this video and you'll be all
good. Now, a final thing I want to
mention is that the solo project is a
pro offering. So, if you haven't
upgraded to Scribba Pro, now is a great
time to do that. With that, I wish you
good luck.
Okay, it is time to remove the training
wheels and have you build your very own
project from scratch. And the one you're
going to build is this basketball
scorecard. You can see on the right hand
side here. It is a very common app for
not only basketball but many sports
because you need a way to keep track of
the scores between the different teams.
And the requirements are that you are to
build this from scratch or I write that
in quotes because I've given you a
little bit of a skeleton to start from.
I'll show you that in a bit. And also
you are to follow the design. You will
get a Figma file so that you can inspect
this design right here. And of course,
we're going to make all of these six
buttons work according to the expected
behavior. And that is that if you click
on any of these three down here under
the home column, then the home score
will be incremented with the number that
is on the button. So if you click on two
here, the home score should be
incremented from 12 to 14. Likewise, if
you hit on the plus one in the guest
column, then in this case, the guest
score should increase from five to six.
So these are all the requirements.
However, we also have some stretch goals
that you can do if you want to go above
and beyond. But I want to warn you
because in this JavaScript module, I
haven't really taught you the concepts
you need to do in order to complete
these stretch goals. So, these are only
meant for students who are actually
ahead of the class and who've been doing
studying on their own. And actually,
it's only for those of you who find
these requirements to be a bit boring.
So, don't do these stretch goals if you
are struggling. But if you want to go
above and beyond, you can, for example,
add a new game button that sets both of
these scores to zero so that you can
start adding scores. Again, you can also
highlight which team is leading. I'm not
sure how you would want to do that, but
you'll probably find a nice way to
showcase that. And also, you can add
more counters, for example, which period
the game is in, how many fouls each team
has had, and even a countdown timer.
Though, this is really complex. It's far
beyond the scope of the JavaScript I've
been teaching you so far. So, I don't
expect to see a lot of solutions with
this timer implemented. And also, a good
way to practice your overall product and
design skills is to change the design.
Make it your own. You could even turn it
into a counter for a completely
different type of sport and then
accommodate the buttons to whatever
point system that sport has. But, as I
said, the stretch goals are only stretch
goals. They are not a part of the core
requirements. Now, as I said, there's a
Figma file you can use in order to look
at the design more closely. Just click
on this image right here and you'll be
taken to that Figma file. Now, when you
get there, you'll probably see other
people interacting with the file live.
That's how Figma work. It's a
multiplayer app. So, what I want you to
do here is click on the down arrow at
the top here and choose this duplicate
option as that'll give you your very own
Figma file to work with. And if you are
a little bit confused with Figma, you
can click on this link right here and
then that'll take you to a quick Figma
tutorial on YouTube. Now, if you ever
get stuck and don't know what to do
next, we have created a help section for
you where we'll give you hints that'll
hopefully get you unstuck. So, I'll
leave you here with the requirements and
the design as well. You can click on
this design to get to the Figma file and
also here is a link to the help section
I just mentioned. Now, as I said, I have
a little bit of a skeleton for you just
to make sure that you hit the gate
running. So as you can see in the HTML
I've created a div with a class of
container that contains two direct
children which both also are divs that
then again contain their own children
which are H3 elements with home and
guest as the text. And in the mini
browser, you can see that these are set
up side by side as columns, which might
seem a little bit weird given that divs
are block level. But if you head over to
the CSS file, you will understand why
because I've given this container a
display flex, which turns the children
into columns and then set some space
around each of these two columns using
the justify content property. So what
you can do then is start filling out
each of these two columns and slowly
inch your way towards this design and
also towards making it feature complete.
Of course you will have to deal with
JavaScript to make the buttons work and
there I haven't given you any hints. It
is a completely blank file but do
remember that there is a huge community
here at Scribba of amazing students who
love to help each other out. So good
luck and happy coding.
Hello students. My name is Treasure. I'm
a teacher here at Scribba. And first of
all, I want to congratulate you on
completing yet another solo project.
This is a huge accomplishment. So, I
hope that you're proud of yourself. But
our work is not done because now we have
to get this project up on the web to
share with the world. Earlier in your
coursework, you learned a quick way to
share a project on GitHub by creating a
repository or basically a folder that
holds your code using this uploading an
existing file link and manually dragging
your project files over into GitHub. And
this is a perfectly fine way to get
started, but I'd like to take things one
step further with you today and show you
a real world way to manage your coding
projects. So imagine you made this
scoreboard solo project for a client or
an employer. The employer loves your
work but wants a change. They want the
headers to read home and away instead of
home and guest. So, we could make these
changes in a scrim and then download the
files again, copy them over to GitHub
again. But instead, let's learn a less
manual, more professional way by setting
up what's called a local development
environment. What's a local development
environment? Well, a simplified answer
for our purposes is that it's all the
tools that you use to do web development
on your local machine. And that's going
to include a text editor, which is a
tool to edit code on your computer, and
version control, a tool to keep changes
updated on GitHub. Now, don't be scared
by the term version control. It
basically just means keeping track of
the changes you make to your coding
projects. The tool that we're going to
use for version control is called GitHub
Desktop. GitHub Desktop is a tool made
by GitHub that offers a really nice
interface that allows you to connect and
sync a repository. So remember that a
repository is basically a folder
containing all the files and folders
that your project requires. So you'll
have a repo that lives on your computer
and a repo that lives on the internet
and GitHub Desktop helps you keep them
in sync. For a code editor, we're going
to install and use Visual Studio Code.
VS Code is super popular and widely used
and has a ton of features. In its most
basic form, it's not actually that much
different than coding in a scrim. In
fact, Scribba uses VS Code under the
hood, so it should look a little
familiar to you. Most of VS Code's
features are beyond the scope of what
we're trying to accomplish today, but we
will use it to open our code locally and
make some changes. Before we get
started, I'd like to quickly remind you
how to download your project files from
a scrim. So from the scrim where you've
completed the scoreboard project, click
on the gear and then click on download
as zip. That downloads the files to your
computer and we will do something with
these files shortly. Please join me in
the next scrim where I'll walk you
through how to create a local repo for
your project, connect it to GitHub
Desktop, and push it to GitHub. I'll see
you there.
Let's get started by downloading GitHub
Desktop. So you can click on this link
or on the image to go to
desktop.github.com.
And once you're there, we can click on
download. That will download a zip file
to your computer. And you can follow the
installation process and then sign into
GitHub desktop with your GitHub login.
And after you do that, your screen
should look something like this with
some options on the left and any
existing repositories that you might
have on GitHub on the right. After this
is all set up, here is your first
challenge. You will use GitHub Desktop
to create a new local repo. Unzip the
project files you downloaded earlier and
move those files into the new repo.
Here's a couple of hints. On the getting
started screen of GitHub Desktop, there
is a button called create new
repository. I highly suggest that you
use that. And your second hint is that
you can use all default options. So if
you're not sure what an option should
be, leave it how it is. Go ahead and
pause the scrim and give this a try and
I will be right back to walk you through
it.
All right, we're back. Hopefully, you
have a shiny new repository. And here we
go. I'm going to start by clicking on
the create a new repository on your hard
drive button. I can then give my
repository a name and a description, as
well as choose where I want to put this
repository on my computer. Now might be
a good time to create a special folder
where you'll keep all of your present
and future coding projects. So, I've
created a folder called projects, and
that's where I've saved my project zip
file. That's also where I'm going to
save my new repo by clicking open. And I
can go ahead and leave these options how
they are, and click create repository.
So, now I have a new repository called
scoreboard, but there are no uncommitted
changes because there's nothing in it. I
still need to move my project files into
the repo folder that I've just created.
So, GitHub Desktop, you'll find, is
super helpful. Here are some friendly
suggestions. it says for what to do
next. And we have this option that says
view the files of your repository in
Finder. So I'm going to click this show
and finder button. And that will take me
to the location on my computer where I
have stored my repo. So here I have my
repository and my project zip file. So I
will click on the zip file to unzip and
then copy and paste these files into my
scoreboard repo. So, when I return to
GitHub desktop, you'll see that it has
detected that I have added three files
to this repository. Before I can put
this repository up on GitHub, I'm going
to need to make what's called a commit.
Making a commit means that you're making
a record of the changes that you've made
to your project so that you or future
devs you're collaborating with will know
what you've done. So, as you can see in
green are the changes that I've added.
And if I were to delete anything, and
we'll see this later, that will show up
in red. To commit these files, I will
need to write a commit message. And that
is just a brief summary of the changes
that I'm including in my commit. Once
I've written my commit message, I can
click on commit to main. Now, if you
don't understand what main refers to,
don't worry about it. It's something
called a branch, and you'll learn more
about it later. So, I'm going to click
on this. And now that we've committed
our changes, we are ready to publish to
GitHub. As you can see, GitHub Desktop
has very helpfully highlighted this
option that says, "This repository is
currently only available on your local
machine. By publishing it to GitHub, you
can share it and collaborate with
others." So, that's exactly what we want
to do. Let's click on publish
repository. This will pop up a window
where I can give my remote repository,
meaning the repository that's up on
GitHub, a name and a description. So,
I'll go ahead and write those to match
my local repository. And then I
willclick this box that says keep this
code private because we don't want to
keep it private. We want to share it
with everybody. Then I can go ahead and
click publish repository. So now we've
created a repo for our project and we
have published that repo to GitHub. We
now have the option to view on GitHub.
So let's take a look. We now have this
lovely new repo up on GitHub and GitHub
Desktop is set up to track any changes
we make locally so that we can easily
update this repo with those changes. So
in the next scrim, we'll use Visual
Studio Code to edit project files and
put those changes on GitHub. I will see
you there.
Let's begin by downloading and
installing Visual Studio Code. You can
click on the link or the image to go to
code.visisualstudio.com.
Click on the arrow and choose the stable
version for your operating system. And
that will download the files and take
you to this getting started page where
you can read or watch some introductory
videos if that's what you would like to
do. So going back to the scenario that
we discussed earlier, I have another
challenge for you. Let's imagine that a
client or an employer would love to use
your scoreboard project, but instead of
the headers reading home and guest, they
want the headers to read home and away.
Keeping that in mind, here's what I'd
like you to do. Open your project in VS
Code. Change the guest header so that it
reads away instead. And then in GitHub
Desktop, write a commit message and
commit and publish your change to
GitHub. Again, I have a couple of hints.
GitHub Desktop provides another helpful
button to open your project in VS Code.
So, strongly suggest that you start
there. And your second hint is don't
forget to save your changes in VS Code.
This is something that I forget to do
all the time and then I waste a lot of
time trying to figure out what's going
on. So, don't be like me. Remember to
save your changes. Okay, go ahead and
pause this scrim and give this a try.
And I will be right back to walk you
through it.
Okay, we're back. Hopefully that went
well, but if you found this confusing,
please don't worry about it. Especially
if you haven't had a lot of experience
with GitHub or GitHub desktop or version
control. This is a lot of new stuff and
a lot of new terminology to wrap your
brain around. And it's also not super
important to understand all of this
conceptually quite yet. It's totally
okay this early in the game to see this
process as just a series of steps to
accomplish a task. So again, try not to
worry about it. You'll learn much more
later. So I'm going to go to GitHub
desktop and click on open in Visual
Studio Code. The first time that you
open files in Visual Studio Code, it
might ask you if you trust the authors.
I'm going to say yes, I trust the
authors. And then I can go to my
index.html file and make that change.
Now remembering to save, I will return
to GitHub desktop. And you can see that
it has automatically detected the change
that I made. You can see that the line
we deleted is recorded in red and the
line that we added is recorded in green.
Before I can publish this change to
GitHub, I will need to make a commit. So
I'm going to come down here and write a
nice descriptive message explaining the
change that we've made. So we've updated
the score header and now I can commit to
main. GitHub Desktop has very helpfully
highlighted our next step. It says you
have one local commit waiting to be
pushed to GitHub. So you may be
wondering what push origin means and
what the difference is between a commit
and a push. So while GitHub desktop will
automatically detect when you make a
change to a repo, in order to push it to
GitHub, you must say to GitHub, please
make an official record of these
changes. And the record of these changes
is called a commit. Push essentially
means publish the commits I've made to
GitHub. And don't worry too much about
what origin means. It's basically
another word for your remote repo. So I
will go ahead and push origin. And now
we have no local changes. There's
nothing left to do. We have pushed our
commit up to GitHub. And I would like to
quickly point out that if you click on
the history tab here, you can see a
record of all of the commits that I've
made so far. Now going back to the
changes tab, I can click on view on
GitHub. And if I click on our index.html
file, you see those changes are
reflected in our remote repo on GitHub.
So, awesome work today. You've learned a
ton about setting up a local environment
to manage your coding projects outside
of a scrim on your computer. There's
much more to learn about version control
and GitHub, and that will come later.
But for now, I'd encourage you to
celebrate your newfound knowledge by
posting a link to your scoreboard repo
in the I built this channel of the
Scribba Discord. I'll see you later, and
happy coding.
We've used GitHub Desktop to push our
project to GitHub. Now let's use Netlefi
to deploy our project to the web. Once
you log into Netlefi from your Netlefi
dashboard, you can click on add new site
and then import an existing project. Now
this might look a little familiar. We're
going to connect a Git provider and the
Git provider we're using is GitHub. Then
I will search for my repo and click on
it. And we come to a site setting page.
We can leave everything as it is. Scroll
down and click deploy site. Now Netlefi
will begin to deploy our project. And
once it's done, we can click on the link
and view our project up on the web. Now,
mine is not very pretty because I'm just
using example files. Yours should look a
lot better. Now, let me show you how
Netlefi will actually automatically
deploy whenever you commit a change to
your remote GitHub repo. I'm going to go
back to GitHub Desktop and open my
project in VS Code. And then I will go
into my index.html and change these
headers again to something else.
Returning to GitHub desktop, I can write
a descriptive commit message. Commit to
main. And once I have pushed that up to
GitHub, I can return to my Netlefi
dashboard, click on my project and its
link to see that it has changed. Pretty
awesome. Now that you've built and
deployed your project, I really
recommend that you share it. If you look
up in the header here, you will see a
share solution button. All you have to
do is click it to go to Twitter where
we've already created the message for
you. Just don't forget to copy and paste
your Netlefi URL here. Great work today.
Happy coding and I'll see you next time.
Hey, and welcome to building blackjack.
I'm so excited about this one because
you are going to learn JavaScript while
building one of the most iconic casino
games ever. Here is a sneak peek of the
app we are going to build. And while
it's not a full-blown blackjack app, of
course, it does share a lot of the
functionalities with blackjack apps you
can play on online casinos. Speaking of
that, that is actually a huge market.
People spend over $20 billion per year
gambling on online casinos. And
personally, I love playing poker and
blackjack. I think my life actually
peaked when I won €100 on a casino in
Prague in 2016. That made me feel like
such a gangster. I spent the rest of the
weekend squatting in front of fast cars
like this Mercedes, this Hummer, this
BMW, and this Bentley. So, my goal with
this section, of course, is to make you
feel as cool as I did in 2016.
And with that out of the way, let's just
get started.
Okay, so we're going to kick this off
with a challenge. You are going to
create two variables, the first card and
the second card. Then you're going to
set their values to a random number
between 2 and 11 as that's the values
you can draw from the deck of cards in
blackjack. Secondly, you are going to
create a variable sum and set it to the
sum of the two cards. So go ahead and do
this right now.
Okay, let's do this. Start with the
first card. I'm going to set it to six.
And the second card up to nine. Then the
sum of course equals first card plus
second card like that. Let control log
out the sum. Run the code and yes indeed
we get 15. Great job. Let's move on.
Okay. So we are ready to build the logic
of our blackjack game. Now let's have a
look at how blackjack works before we
try to formulate this in JavaScript. So,
the point with blackjack is to get a
hand that sums up to exactly 21. And if
you don't get 21, at least you want to
get as close to 21 as possible, but
never above 21 because if so, you're out
of the game. So, here are three
scenarios. One where you have two cards
on your hand, that's your starting hand,
and they amount to 15. So, you're still
in the game. No big reason to either cry
or celebrate. Then, there's the other
case where you have a king and an ace.
And since the ace here equals 11 and the
king equals 10, you've got blackjack.
So, woohoo. And the third scenario, you
started out with a 10 and a six and then
you ask for a new card, but damn, you
ended up at 22. So, you're over 21 and
sadly you've lost your money. So, now
the question is, how do we translate
these things into JavaScript? Sadly,
JavaScript isn't as smart as you are who
can understand this by just hearing me
talk about it. JavaScript is kind of
stupid and it needs you to describe the
logic 100% correct. So let's just do
that so you get to see it in action. We
start out with writing if and then open
and close parenthesis and then it's the
sum of our cards that we are going to
check. So we say if sum is less than 21
just like we were doing here. So this
crocodile mouth is something you might
recognize from your maths class at
school. And it kind of makes sense
because here on the right hand side you
can see that's where the gap between the
teeth of the alligator is the largest
whereas at the left hand side here the
gap between the teeth is the smallest.
So small value here large value here. If
this condition is true meaning that sum
is less than 21. Well then we add two
curly brackets and tell JavaScript that
we want to execute the piece of code
inside of those curly brackets. So we'll
do control log. Do you want to draw a
new
card? Since that's what you can do in
this scenario. You are still in the game
and you can draw another card if you
want to. But there are cases where this
isn't true and we need to account for
those as well. We do that by writing
else if and open and close parenthesis.
And here we write sum equal equal equal
21. This is else if sum equals 21 then
we want this code to be executed. And
this triple equal operator is different
from this one right here. Here we are
saying that we want the sum to become
first card plus second card. But here
we're asking is the sum strictly equal
to 21. And you say strictly equal when
you see three equal marks. And don't
worry about why it's three and not
necessarily two. We're going to get back
to that later. For now on just use three
equal marks when you write else if
statements where you are checking if a
variable is exactly some kind of value.
So if the sum is 21 what is the case
then? Well woohoo the player has
blackjack. So we'll do slog.
Woohoo you've got blackjack like that.
And then the third scenario we'll do
else if yet again sum. And what do you
think we'll use here? Of course, we'll
use this one right here because if the
sum is larger than 21, then the user has
lost. Sum is greater than 21. Open and
closed curly brackets console log.
You're out of the game. Like that.
Actually, let's add these emojis as
well. There we go. Now, one little thing
I want you to notice here is that here I
have to use double quotes to wrap this
as I'm using single quotes to create
this you've and your words. Cuz if I was
using single quotes here, it would be
completely off because JavaScript would
have thought that I was intending to
close off this string right here when
I'm not intending that. I was just
writing you've instead of you have. So,
just a little digression there. Now
let's run this code and see how it
works. We'll hit run and yes, do you
want to draw a new card? We triggered
this which seems correct since 9 + 6 is
15 and that is indeed less than 21.
Let's try to do 10 and 11. So this is
black deck. Yes, JavaScript triggered
line 8, which is what we wanted. And
finally, let's say that we got 17 on the
first two cards and we really wanted a
new card. If we wanted to take the
chant, got to be bummed out by getting
six. If so, our sum would be 23. And
what do you think will happen? Yes, this
line of code should be run. I'm going to
run the code. And boom, you're out of
the game. Now, a final thing I want to
tell you about before we move on is that
could actually skip all of this code if
we wanted to and do like that. Because
if sum is not less than 21 and it's not
exactly 21 either meaning we've jumped
to the third clause in our if else
clause then it must be over 21. I mean
there's no other alternative in the
entire universe. So there's no point
specifying that sum here should be over
21 for this block of code to be
executed. meaning we can just skip it.
So that we have if sum is less than 21
execute this. If sum is exactly 21
execute that and in all other cases
execute this. This will work as you see
by running the code in the exact same
way. Okay. Now that you know that let's
move on.
Now it is your turn to write an if else
statement. You are to check if a person
is old enough to enter the nightclub
which has a lower age limit at 21. And
then you are to log a suitable message
to the console in both cases. And by
that I mean if the user is less than 21
or if the user is 21 or older. So I've
defined the age variable here. And also
written pseudo code if less than 21 then
a suitable message would be for example
you cannot enter the club. else meaning
all other cases because if they're not
less than 21, well, they have to be 21
or above, then you are to log something
like welcome. Go ahead and give this a
shot. And if you have forgotten how to
write an if else statement, just go back
and rewatch the previous lesson. And
notice here that I don't have an else if
statement in between here. I only have
the first if and the final else, not the
else if which we had in the previous
lesson. And that is because I want you
to think for yourself and try to figure
out well how do you write this with only
an if and an else and not the if else in
between. Just give it a shot and I will
show you the solution as well when you
return back to this screencast.
Okay, hopefully that went well. Let's do
this. If less than 21, we have to do age
is less. Remember this alligator mouth
hair and 21 like that. This means the
person is maximum 20 years old.
Then we are to do following while
console.log
you cannot enter the club. And then we
simply do else and then the open and
close curly brackets. And here we can
console log welcome.
Running the code we get welcome. That's
correct. Trying to lower the age to 20.
We get you cannot enter the club. Now a
final thing I want to point out is that
you could also solve this by using
another comparison operator which is the
less than or equal to. That means a
slightly different thing though because
it checks that this value is less than
or equal to 21. So if we take this back
to 21 and run it, you can see we get you
cannot enter the club which obviously is
wrong. They should be able to enter the
club at this point. So if we're using
the smaller than or equal to operator,
we have to use 20 because then we
include 20. That is an upper range that
is inclusive of that number.
So now it works. Whether you prefer
using less than or equal to or just the
less than is up to you. You just need to
keep in mind how that changes the entire
expression. With that, let's move on.
Let's practice if else statements a bit
more. Because in Norway, we have this
super cool tradition where when you
reach 100 years old, you get a personal
birthday card from the king. This is the
king, King Harold V of Norway. And in
this challenge, we are going to code the
logic for this. So here it is in pseudo
code. If less than 100, and here I'm
talking about the age variable, of
course, then you are to log something
like not allegible to the console. Else
if the age variable is exactly 100, you
are to log something like here is your
birthday card from the king. And else,
in all other cases, meaning they're over
100 years old, you get not allegible.
You have already gotten one. because if
they're 105 years old, they got their
birthday card from the king 5 years ago.
Okay, go ahead and give this one a shot
and then I will show you the solution
afterwards as well.
Okay, let's do this. If age is less than
100,
we'll console log not legible and then
we have to do the else if and specify
the condition age is exactly 100. Then
we console log here is your birthday
card from the king. And else in all
other cases the person is over 100 years
old. They have already gotten their card
previous so not allegible. Let's run the
code. Yes, 100 you are getting the card.
At 45
you are not legible. At 145 you are not
allegible either. Finally, before you
leave, let's take a closer look at this
triple equals here because it might seem
like a little bit of an overkill to have
three equal signs after each other. And
that is a valid point. You could use
two. If we run the code, it still works
with 145. And actually with 100 as well,
it would still work. You are getting
your birthday card. However, the thing
with two equals, so-called double
equals, is that it's a little less
strict. So let's say that you by mistake
passed in a string here as well, which
has the same value of 100, but it's a
string and not a number. If we run the
code, you can see it still works.
However, with the triple equals running
the code, then it doesn't work. Now, it
says not alleible even though 100 is
here as a number and 100 is here as a
string. So, double equals ignores the
fact that this is a string and this is a
number. Basically converts this into a
number if it's a number on the left hand
side as well. So that this entire thing
would be true. However, that's not what
we want. We actually want to be strict
in our code, which is why I would
recommend you to only use triple equals.
Ignore double equals. This forces you as
a developer to be a bit more mindful
about how you are checking your
conditionals. You need to make sure that
if you're starting out with a number up
here, you're also comparing it with a
number down in the conditional. But
that's just good practice being
thoughtful as to what kind of data types
you are using in your code. just stick
with triple equals and you'll be just
fine. With that, let's move on.
Okay, so now we're back at our blackjack
game. And you might have noticed that
we've lost the code that we previously
had. Well, that's the point of Scribba.
You are going to write the code, not me.
So, I want you to rewrite the
conditional that I created previously,
but with a slightly different take. You
have to follow the logic that I lay out
in the pseudo code here. So you have to
use in the first if clause the less than
or equal to operator. So you have to set
it to less than or equal to 20. And then
in the else if clause you can use the
strictly equal to check if the sum is
exactly 21. And finally you're going to
use the else clause and not an else if
on the third conditional which I did
previously. So go ahead and do this. And
if you can't do it or want to see my
solution, just return back to this
screencast and I will show you how to do
it as well. Good luck.
Okay, let's do this. First, there's the
if sum is less than or equal to 20, then
we are console logging out this string
right here. And then we do else if the
same as we did last time. sum triple
equals 21.
That's the blackjack case. And here I'm
lazy. So I'll just copy paste this one
and then this one in here. And then
finally not doing else if and checking
for the condition but just doing the
else. So all other cases and then
pasting in this you are out of the game
string like that. Good stuff. Let's run
the code. And yes, we have 17 here. And
we rightfully so logged out. Do you want
to draw a new card? Good stuff. If we
try to hit one more time with another
10, we are out of the game. Good stuff.
And how about a four? Blackjack. Woohoo.
Great job. Let's move on.
You've already worked with some
different types of data in JavaScript.
You've seen numbers and you've seen
strings. Now, I want to add one more
data type to this list because often we
don't really need the almost infinite
possibilities of numbers and strings of
letters. We actually just need two
options like a light switch on or off,
yes or no, true or false. And that
brings us to the boolean. The boolean is
a value that can be either true or
false. Let's see one in action. Imagine
we're developing a site for an online
food retailer. We've collected some
information about our customers eating
habits so we know what food items to
show them. Oftent times when storing a
boolean, we will choose a name with a
verb. So let's come in here. I'm going
to delete all of this and I'm going to
set up a let with the name is vegan.
And I'm going to set that to true. So
the verb here is makes this read like a
question. Is the customer vegan? Yes,
that's true. And of course it could also
be false. Let's just log that out
quickly. And when we save down in the
console, we get false. Let's change this
to true. Okay, so that's working as
expected. And having a boolean that you
can flip to true or false is really,
really useful. And you're going to see
it a lot. Let's just dig a little bit
deeper. Our site could also allow users
to use a special code to get a discount
for their food order. Let's set up a
boolean to see if they've got a discount
code or not. And here the verb could be
has.
So the question here is, has the user
got a discount code? And at the moment,
the answer is yes. That is set to true.
Now, let's start to see the real power
of a boolean by using this in a
function. And I'm just going to delete
our earlier example. And I'm going to
set up a function here called process
order.
In the body of the function, let's use
an if to check if the user has a
discount code. And if they do, let's
just log out that the discount has been
applied.
Okay, now if I call this function and we
hit save down in the console, we get
discount applied to food order. So this
is working because the if is checking if
this is true and we know that it is
true. We've got it set to true right
here. If we set this to false, we hit
save and nothing happens. This code does
not run. And we can see that a little
bit better if we add an else. So if this
is false, this code doesn't run. This
code will run. Let's just log out. No
discount applied
again. We'll save. Oh, and I've
misspelled console. There we are. No
discount applied. Okay. Now, let's do
something a little bit more ingenious.
So, if this is set to true, then we
process an order. The discount is
applied. Now, the discount code has been
used. So, we should set this boolean to
false. Now, I'm going to call this
function not once but twice. Let's see
what happens. Okay. So the first time we
call the function we get discount
applied to food order. So what's
happening there is that has discount
code is true. The process order function
checks that it finds it's true. So it
logs out discount applied to food order.
Then it flips that boolean to false. The
second time we run process order has
discount code is false. So this code
here does not run but this code does
run. So we log out. No discount applied.
So the boolean is working like a kind of
gate. It's letting you through when it's
true and not when it's false. It's a
really really useful data type and I
think you're going to see it again very
very soon. Now just to clarify, it might
be a little bit strange that all we need
to do is put the variable has discount
code in here. You might have expected
that we need to ask if has discount code
is triple equals to true. But actually
that is not necessary in JavaScript. The
if statement evaluates the expression
inside these parentheses, i.e. all of
this code right here, and coerces it to
a boolean value. What does that mean? It
means it converts it to a boolean value,
true or false. It does this part for you
under the hood. And that is why this
works. So, actually, all we need in here
is the variable. And if we were to come
in here and replace it with something
like an empty string, well, in
JavaScript, an empty string is a falsy
value. And that means that the if
statement will coersse this to false.
And this code right here will never run.
Let's hit save. And we see no discount
has been applied twice. So this code
never ran. So just remember that
JavaScript is going to do that part for
you. It's going to take has discount
code and it's going to evaluate it to
true or false without you needing to do
anything else. Okay, hopefully that has
tidied up any confusion. Now, let's get
back to the project where you're going
to see a few more booleans and get a
chance to practice with them.
Okay, so our game is coming along really
nicely here. However, there's a big
problem and that is let's say that you
are playing this game and you get
blackjack. So you get a 10 and an ace
and boom, you've got blackjack. Down
here at line 13, you of course want to
cash out your money. Going to get the
pot on the table. However, at this point
down on line 13, JavaScript has no way
of knowing which console log it logged
out. It doesn't remember that. So what
we need to do then is introduce a new
variable that keeps track of this state.
You can call it the state of the game.
like has the user gotten blackjack or
maybe has the user lost or whatever it
is. So what we then want to do is for
this scenario have a variable that
reflects whether or not the player has
blackjack. And when I say whether or not
yes or no is there a data type in
JavaScript that you think might be
suitable for describing such a state. If
you guessed a boolean a true or false
that is correct. So what we'll do is
we'll do let has blackjack like that and
set that initial value of course to
false because by default you don't have
blackjack.
And then here inside of the else if
clause that describes the blackjack
case, we're simply going to reassign the
has blackjack value to true instead.
Now down at line 16, if we log this out,
he has blackjack value and then run this
code. Boom. You can see that we're both
logging out on line 9 and we have
persisted the state so that line 16, for
example, can start writing the code for
taking the cash that's on the table that
now is rightfully yours and then deposit
it into your account. So now we have an
app that keeps track of the data of the
state in a much more sensible way. Now,
I want you, of course, to write this
kind of logic yourself. So, in the next
cast, I'm going to give you another
challenge so that we can keep track of
even more of the state of this game.
I'll see you there.
Okay, so now it's your turn and you are
going to make the state of our game a
little bit more robust because at this
point, we're only tracking if the player
actually gets blackjack. If the player
busts out and gets 22 or more, we're not
tracking it. And obviously we have to do
that. So I want you to create a variable
called is alive and start by assigning
it to true. And then you're going to
flip that value to false inside of the
appropriate code block. So there's three
code blocks, the if block, the else if
block, and the else block. And you of
course then have to pick the correct
code block and then write that line of
code that flips the value to false. And
finally log it out to check that you're
doing everything correctly. So good
luck. go ahead and do this and then I
will show you the solution afterwards as
well.
Okay, let's do this. Starting up here,
we're going to create a let variable
called is alive and assign that to true
because when you're starting out, the
player is still alive. And at which
point is the player no longer alive,
meaning out of the game. Well, that's
obviously here in the else block because
that's the case if the sum is 22 or
more. So here we'll do is alive equals
false. And then finally we'll console
log is alive like that. We got true
because actually we had blackjack and
rightfully so the player is not dead.
How about if we pretend that we add a
third card here. It's really stupid
idea. Pick a third card after you've
gotten blackjack. But let's just pretend
that. We'll run the code and boom.
False. No longer alive. Great job
solving this challenge. Let's keep up
the pace and move on.
Now that you've learned about booleans,
let's take a look back at the
conditional you wrote. Because one thing
you probably didn't think too much about
when you wrote your conditionals is that
the expressions inside of the
parenthesis would be translated by
JavaScript eventually into a boolean. So
it would be either true
or false. That's always the case with
expressions passed into conditionals
because here we are comparing two
numbers and one of them are either
larger than the other or they are two
equal numbers. So the claim you come
with has to be either true or false.
Let's try to play this out using an
example. For example, when you've gotten
two cards, a 10 and an 11, meaning you
have blackjack. In that case, sum will
be 21. And the claim you come with
inside of the first if statement,
obviously that is a lie. Meaning it gets
evaluated to false. In the second
condition though, you're saying that sum
is equal to 21. And we know we are going
to translate sum into 21 because that's
what it is. So here you're saying 21
triple equals 21. Well, yes, that is a
true statement. So that becomes the true
boolean. And here you can read through
it. If false, well then we're going to
skip the body of this statement. Else if
true. Yes, we like true. Then we are
going to execute that piece of code. So
this is essentially what happens when
you run the code with specific values
for these variables.
Rewinding it back. What I want to do now
is stress test your comparison and
boolean skills. So, I'm going to paste
in a bunch of control logs here and then
comment out our black game. Now, your
job is to look at each of these
expressions and predict whether true or
false is logged to the console. So,
write either true or false after these
two slashes on each of the lines from 1
to 7. And then, of course, when you've
given it a shot, run the code and see
how it went. And then, I will do the
same thing afterwards as well. Go ahead
and do this right now.
Okay, let's do this. First, four triple
equals 3. Obviously, that's wrong. It's
going to log out false. 5 is greater
than 2. That is true.
And 12 is greater than 12. No, 12 is not
greater than 12. It's exactly the same.
So, this will be a false statement.
3 is less than zero. Again, that's also
false.
3 is larger than or equal to 3. Yes,
that would be a true statement because
it's not larger than, but it is equal
to. So, true.
11 is less or equal to 11. Again, it's
not less than 11, but it is equal to 11.
So, it's true. And 3 is less or equal to
2. No, three is actually larger than
two, meaning that this is going to be
false. So, this is my prediction. Let's
run the code. Bring up the console. And
yes, phew, I was right. Very good. Let's
move on.
Welcome back, my friend. I hope you are
ready for a new challenge because that
is exactly what I have in store for you
now. Actually, it's a three-part
challenge because you are going to make
our program a little bit more robust in
the sense that it might be that these
console logs here that we're just
throwing out into the void, logging to
the console and forgetting all about can
be used later on down the line or that
we of some reason want to keep track of
them. Maybe we want to render it out at
multiple locations in the app. Maybe
there's a notification and there's text,
there's a log of messages you've gotten.
Maybe we want to repeat it. I don't
know. There's tons of reasons for
keeping this data and very few reasons
for just logging it out. We want to keep
it in a variable just like we've done
here with the has blackjack and is
alive. So your three-part challenge is
to first declare a variable called
message and then assign its value to an
empty string and then you're going to
reassign the message variable to the
string we're logging out. Notice here
that I'm using some fancy words like I'm
saying declare instead of create a
variable. I'm saying assign instead of
set its value. That's because I think
you now are ready to hear some of these
fancy JavaScript words because there are
subtle differences between these words.
Like here we are actually declaring the
variable and then we are assigning it
when we set the value to true. This is
after all called the assignment operator
in JavaScript. And then naturally so
when we change a variable or assign it
yet again we reassign it. So, I just
want to use the correct terminology
here, but I think it's important that
you don't get put off by all of these
words and think that you have to
remember them all. You do not. It's
perfectly fine to talk about this using
your own words. Create, change, set the
value, whatever. I just want you to also
be exposed to these technically correct
ways of expressing it. Anyway, enough
talk. As I said, step two, you are going
to reassign the message variable to the
string that we're logging out. So, I
don't want any more console logs inside
of these three code blocks because the
only code block is the one you're going
to create in part three of the challenge
when you are going to log out the
message variable. So, go ahead and give
this your best shot and then return back
to this screencast when you want to see
my solution.
Okay, hopefully that went well. If not,
no worries. Let's do this together.
First, we'll start by declaring this let
variable. We'll call it message and
we'll set it to an empty string. Then on
line 11, 13, and 16, we need to replace
the console log with a message equals
like that. And also remove this close
parenthesis. Do the same thing here.
Remove this one and the closed
parenthesis. And then write message
equals finally here as well. And then we
are ready to log it out down here at
line 21. So I'll log out the message,
run the code, and as you can see, we're
logging. You are out of the game. That
is correct because we had this really
stupid move where we got blackjack, but
we asked for a new card. So let's remove
this. Uh run the code. Yes, you have
blackjack. Try again with for example
14. And yes, I can now draw a new card.
So great job reaching this far. Let's
move on.
Okay, now it's time to fix our biggest
flaw in our blackjack game, which is
that it has no user interface. If we
want this to be a hugely successful
online blackjack game, it has to use
HTML and CSS so that users can play it
via a browser. And I've actually set up
the basics for you here. You can see an
HTML file with a head and a body. And
then inside of the body, we have an H1
with a text blackjack. And also we are
linking to the JavaScript file here on
line 8. And as you can see, if we bring
up the mini browser, this just renders
out the H1 tag. So your job now is to
link this HTML file to the CSS file. You
can see I've created just one CSS rule
in this file and it will be applied
changing the color of the title when you
manage to add a link to the CSS file
here on line five. So I almost every
single time have to search on Google for
how to link to CSS from HTML. I end up
at something like this. So I want you to
do the same thing. And one tip would be
to find this W3 schools right here as
they often pop up on Google and contain
the necessary information. So go ahead
and figure this out on your own and then
I will show you the solution afterwards
as well.
Okay. Hopefully you nailed that one. If
not, no worries. Let's first check out
Google. Then, if we click into the W3
Schools site, you'll see the following.
And it tells us that there are three
ways to insert CSS: external, internal,
and inline. And we are using an external
CSS file. So, we want to scroll down to
external CSS. If we do that, we see the
code here. And the line we're looking
for is this one. Link re equals
stylesheet href equals my style.css.
Okay, good. So let's do link real well
and that is short for relation. So what
is the relation between this HTML file
and the current file you're going to
point to now? Well, it is that the file
you are pointing to is a stylesheet cuz
as you remember CSS is short for
cascading stylesheets. And then it is
the href which in our case is index.css.
We are in the same directory so we don't
need to navigate into a folder or
anything like that. We'll just write
index.css CSS and the href attribute is
something you should be familiar with by
now as that's also what we use when we
are creating links. Now let's run this
code. Boom. You can see the color has
changed meaning we are indeed importing
this stylesheet. Great job.
Okay, now it's time for you to style
this app because as you can see it looks
kind of bad. I've added a few more
paragraphs here as you can see. one
which says want to play around and has
an ID of message shell and then just two
other paragraphs that will display the
cards and the sum eventually. And your
job now is to take this HTML and make it
look like this. If we head into the
index.css file, you can see it says
complete the CSS styling according to
the provided design. And here I've
created two new selectors for you, the
body and the message shell. And the
thing is you can create this entire
design by only using these two
selectors. So that's why I have them
here for you. I've also added a
table.png inside of an images folder.
That is for the casino background that
we are using here. And you've learned
all about how to do this styling in
previous modules of the career path. So
if you struggle, you can go back and
rewatch some of those lectures if you
want. Now, before you start solving this
challenge, I'm going to add the font
family for you because we're using the
trebushche font here and you haven't
really learned how to do that. So, I'm
going to do it for you. We do font
family up in the body and there I
specify trebushche. And as you noticed,
what happened now was that the editor
gave me a bunch of other fonts as well.
And this is called a font stack. And the
reason we use font stacks is because if
your operating system doesn't have
trebuchems installed, well then your
browser can't show that font unless
you've also imported from Google fonts
for example or included it in your
project, neither of which we have done
right now. So if your operating system
can't show trebushchems, then we're
telling it to look for Lucia Sans
Unicode then. And if you don't have that
either, well, look for Lucia Grande and
so on and so on. So it kind of ensures
that at least we'll find a somewhat
similar font as it moves downwards in
the stack. It at least find a sans serif
font here which is our final choice. But
let's stop with fonts there. I don't
want to get deeper into it. Now we have
the trebushche font visible and
hopefully it's visible on your end as
well because trebushche is one of those
fonts that are so-called web meaning
that almost all operating systems have
them installed so it's safe to use. Now,
I want you to take a look at this design
and and add whatever CSS you need to the
body and the message shell so that you
turn this boring page into what looks
much more like a real blackjack game.
So, go ahead and do this right now.
Okay, hopefully that went well. I'm
going to start with the background
image. So, I'm going to write background
image. And then to fetch this table.png
png. Here I can do URL open and close
parenthesis. Then navigate into the
images folder, grab a hold of the
table.png
like that. Now you can see we have the
nice background. However, if I drag this
wide, you can see that at some point we
reach the end of the image and CSS adds
the image yet again after that and we
get this ugly looking line which we
don't want. We instead want to use the
background size property and set that to
cover. Now, no matter how wide you make
it, the image still covers the full
width.
Okay, next step is to center this text
and make it white. So, we can do text
align center to center everything and
color white like that. So, with this we
are turning everything into white except
for the H1 which you can see here. I
have specifically specified that the H1
should be color golden rod and that
overwrites the color white on the body
element. Next up, you can see here the
text is a bit bolder. So, we want to do
that as well. We want to do font weight
bold like that.
Starting to look good.
There's just one final thing just that
you can see the message element which is
the message for the user is in italic
here. It's still just normal. So in the
message gel, we'll do font style and set
that to itallic like that. And there we
have a neat looking blackjack game. So
with that, let's move on.
Okay, so now we need to enable our user
to click a button to play the game. And
you are going to do this through a
challenge, of course. And I'm really
excited about this one because it
includes both HTML, CSS, and JavaScript.
So, this is the first time where you'll
use all three languages to solve a
problem. And the reason that this is so
cool is because this is how it works in
real life when you get a job as a
professional developer. You'll juggle
between HTML, CSS, and JavaScript all
the time. And this is an opportunity to
start practicing exactly that. So, it's
a three-part challenge starting here in
the HTML where you are going to create a
button that says start game and it
should call a start game function when
it's clicked. So, obviously then you
also need to create that start game
function. You'll do that in the
index.js. Here we are to create the
start game function and move the
conditional below line 11 to 20 that's
these lines into the body of the
function so that this code is run when
the button is clicked and then finally
you have to make the button look good
for the user. You are going to style it
according to the provided design and
that is exactly here you can see the
start button. So just click to open this
slide whenever you need to take another
look at it. And doing this translating a
design into CSS is something the front-
end developers do every single day. So
it's great practice. I've given you
three values for properties here.
There's the color of the text that
should have this hex value. The width
should be 150 pixels and the background
should be golden rod. Same as the text
color on the H1. This is a big one, so I
can totally understand if you'll
struggle a bit going through this one,
but give it your best shot and then
return to this screencast when you want
to see my solution. Good luck.
Okay, hopefully that went well. Now,
let's do this here. We'll do button like
that, opening tag. And then I'll do the
closing tag as well. And inside we'll
write start game like that. Let's run
the code. And it looks not particularly
good, but it's at least there. And it's
actually centered. The reason for that
is because here on the body, we are
centering all of our text. And that also
centers the button. Next up, we need to
call the start game function when it's
clicked. And if you remember how that
was, then you'd write on click equals
and then the start game with open and
close parenthesis inside of a string
right here. So when the button is
clicked, we're going to call or invoke
the start game function. Then we need to
create that function. And if you
remember how to do that, it is through
the function keyword function and start
game open and close parenthesis and then
curly brackets for the body of the
function. Inside of here, we are going
to take this code, copy it, paste it in.
I'm going to give it a tab so that it
looks better like that and remove the
empty lines. Now we have this. Let's
rerun the code and see if it works. Yes,
previously we got this console log just
by refreshing the page. But that doesn't
happen now. I have to actually click the
start game. Then we run this. Then we
console log out the message. Great job.
Now let's move on to the CSS. Okay, this
is how we want it to look. Let's take
the first three provided design hints
and see how close we get just by
implementing them. Text color that is
the color property
like that. hardly see it since the text
is so thin, but that's okay for now.
Let's just set the width. Width 150
pixels like that. Okay, looks a little
bit better. And the background
that should be golden rod. Okay, so hm
looking a little bit better, but not
quite this design here. As you can see,
it has some space above and below the
text, whereas this one is much more
crammed. So, let's add that. And if you
remember from the CSS section, you could
add that space by using the padding. So
that's padding top and padding bottom.
I'm going to set five pixels. Looks
about right. Padding bottom. And do five
pixels there as well. Yeah, not too bad.
However, as I mentioned, this text is
really thin. And here it's much thicker.
So let's actually do font weight bold.
Yes, that looks much better.
Now, the next thing I want to fix is the
border around the button. Because you
can see it's totally clean here. It has
no border, but the default styling that
our browser provides has this little
border. So, we want to do border none.
Okay. Now, it actually pretty similar.
They're almost identical, but only one
thing separates them, and that is that
you can see the edges are sharp on this
one, whereas the edges are a bit rounder
on our provided design. So that is the
border radius and pretty subtle. So I'm
going to try two pixels. And yes, that
looks, as far as I can see, identical.
So this was a huge challenge. and you
had to context switch between HTML, CSS,
and JavaScript. So, be really proud of
yourself for reaching this far. If you
struggled and didn't really manage to
get to the finish line on your own, I
think you should just go back and try it
again now that you've seen my solution.
That's a great thing to do to reinforce
what you've just learned. Whatever you
do, I will see you in the next lesson.
Okay, so now our player can click the
start game button like this. However,
we're only logging out the message.
Obviously, we don't want to do that. We
want to display that info on the page
along with, of course, information about
the cards and the sum. But starting with
the message, I want you to solve a
two-part challenge. First, you are going
to store the message L paragraph, that
is this one right here, in a variable
called message L using the camel case
naming convention. And then you're going
to display the message using message
l.ext content. Then of course you can
remove the console log right here since
we don't need it anymore as the message
will pop up here in our app instead. So
go ahead and give this a shot and return
back to me when you want to see my
solution.
Okay. So the way to do this is if you
remember first by creating a variable
with let message L and then using the
document keyword dot get element by id
passing in the ID which we can see from
the HTML and just copy from there
actually and paste then in here as
always control logging to see that we
are on the right track
running this code and yes there we can
see we are logging out that element
now it's just a matter of deleting this
line
doing message Lext
content and what do we want to set the
text content to we want to set it to
message like that if we now run the code
hit the start game button yes there we
got do you want to draw a new card job
now there's one thing I want to do
before we round off and that is actually
to remove these emojis because when they
are italicized, they don't look that
good. And while that isn't the most
complicated thing to fix, it's not
something I think you should learn right
now. I want to keep up the pace. And
also, when we see this text here
rendered on the page, it's kind of our
dealer speaking to us in italic text.
And I don't think our dealer should show
that much emotion. So, it makes sense to
remove these emojis like this. Also,
maybe make it a bit more neutral. The
dealer will just tell us that we have
blackjack. won't shout out. Woohoo. So,
makes more sense. Run your card. Yeah,
looks good. If we get blackjack. Yeah, I
think that looks good. So, with that,
let's move on.
Okay, so let's make our game a little
bit more useful because now you can
click the start game button and indeed
we get the message from the dealer, but
we have no idea what our sum and our
cards are. So, we obviously need to
render that out and we're going to start
with the sum. So, it's a three-part
challenge where you first have to head
into the HTML and there give the sum
paragraph that is this one an ID of
sum-l
then you are to store the sum paragraph
in a variable called sum L like this in
camel case and finally render the sum
out so here I'm talking about the actual
numeric sum on the page using this
format so sum colon and then the given
sum you have up here. So here you have
to keep this text which is already in
the DOM intact. But we have done that
before. So hopefully you'll manage it.
If not, no worries. You can just revert
back to this screencast and I will show
you how to do it as well. But go ahead
and give this your best shot right now.
Okay, let's do this. First, we'll give
this an ID of sum-l.
Moving over to the JavaScript, we'll do
let some l with the camel case this time
and document.getelement
by id open and close parenthesis and the
quotes passing in the ID that we just
added to the HTML element sum-l.
Now just to be certain let's just
console log the sum l run the code. Yes
indeed we are grabbing the element and
then we need to render out the string of
sum and then the actual sum which in
this case is 14 from this variable up
here into the element itself. And the
way I'll do that is I'll use sum L and
then text content which you should have
seen a few times by now and set that
equal to not only the sum because if we
do that let's rerun the code and start
the game. Then we simply just clear out
the existing text where we want to keep
the sum there. And we can do that in
multiple ways. But the way I'll do it is
simply just write sum colon a space and
then plus here. So that we just
reconstruct the default text as well in
addition to the sum. I think this is a
nice way of doing it. So if we run the
code, start the game. Yes, there we go.
Now, before we move on, I want to teach
you about another way of grabbing
elements from the DOM because we've used
get element by ID a few times now. So,
I'm assuming you're comfortable with
this technique and are ready to learn
another one. And that is something
called the query selector. And to learn
that, let's comment out this one and
copy it to a new line where we can
change it.
And here instead of get element by ID,
we'll do query
selector like that. Since this is a more
dynamic method, we have to be a bit more
specific in the value we pass into it.
So here we actually have to pass in a
hashtag in front of the sum to tell the
query selector that it's actually the
element with the ID of suml that we're
looking for. Because unlike the get
element by ID, we're not specifying that
it's an ID in the method name. It's just
called query selector which is kind of a
broader expression. So query, that's
another word for question and selector
that refers to CSS selectors. So we're
kind of asking for an element by its
selector. And then we specify the given
selector inside of the parenthesis. So
here if we run this start game, the
exact same thing happens. Now, if you've
forgotten what a CSS selector is, we can
head over to the CSS and it's these guys
right here. The body that's just
selecting the body element, this one.
And then there's the ID selector right
here. You can see we are using the
hashtag to grab the message L because
this paragraph has an ID of message-l.
Likewise, sum has an ID as well. So
we're using the hashtag to tell query
selector that this sum-l comes in the
form of an ID. Now this means that we
could change this to a class instead.
However, then we also have to change
this hashtag and replace it with a dot.
Hopefully you remember that the way to
select CSS classes is by using the dot.
If we run this, it still works. And
actually we could even pass in just the
element name as well. So we could do
body right here. However, it would break
our app since we're now okay just
clearing out the entire body when we are
running this piece of code down here on
line 11 because now suml actually refers
to this entire body.
So, as you probably understand, query
selecture is very powerful and you will
see it used in a lot of tutorials here
on Scribba. But as for right now, I just
wanted to show it to you so that you are
prepared once you see it in other
tutorials and out in the wild. In the
near future, I am going to stick with
the document.getelement by ID as that
does the job for us and it's also a very
simple method to understand. So, I'd
like to avoid the added complexity of
jumping between get element by ID and
query selector if we can.
With that, let's just move on.
Okay, let's continue building that
muscle memory of yours. You are going to
do almost the exact same thing as you
did with the sum, but this time with the
cards because obviously when you start
the game, you can see your sum now, but
you can't see your cards. That has to be
fixed. So, it's the same three-part
challenge starting in the HTML where you
are to give the cards paragraph an ID of
card cell. Then in the JavaScript, you
are to store the cards paragraph in a
variable called card cell with camel
case. And then finally render it out in
this format right here. So go ahead and
do this right now and then I will show
you how I would do it when you revert
back to the screencast as well.
Okay, let's do this. First giving this
an ID of
cards L.
Then storing the card paragraph in a
variable called also cardel. Let cards L
in camel case this time
document.getelement
by id passing in cards-l
like that. Now, I'm just going to jump
straight to this line here and do cards
L.ext
content equals constructing the entire
contents of the paragraph this time. So,
we're clearing out the text that's
already there and replacing it with a
new cards colon.
This will be actually cleared out and
replaced using JavaScript. and then plus
the first card and then I have to add a
space between the first card and the
second card and then the second card
like that. If I run this code, start the
game and there we go. We have both of
the cards rendered out. Great job. Let's
move on.
So, our game really starts taking shape
now. We hit the start game button and we
get the message, the cards, and the sum.
However, a critical part of blackjack is
the ability to draw a new card from the
deck. So, we need to start building that
feature and we are going to first begin
with creating the new card button that
you can see on the slide here. So, in
this three-part challenge, you are going
to first head into the HTML and create
that new card button and then make it
run a new card function when it's
clicked. And then in the JavaScript, you
have to create that new card function.
And for now, it should just log out
drawing a new card from the deck.
Then in the CSS, you need to make sure
that when these two buttons are rendered
out vertically like this, so above each
other, you'll need to make sure that
this small space between them is added
because it won't be added by default.
Now, if you enlarge this browser and
make it wider, they will render out
besides each other, but you are not to
worry about that layout. So, make sure
you keep this browser window a little
bit narrow so that they are rendered out
under each other. and then make sure
that you get this little gap here. So
this is again a very holistic challenge.
You are to write JavaScript, HTML, and
CSS just like it is out in the real
world. So go ahead and give this your
best shot right now.
Okay, hopefully that went well. If not,
no worries. Let's do this together.
In the HTML, we are starting with the
button
open and closing tag. New card like
that. I'll just render it out. Oo, there
we got it. Actually got them beside each
other. We want them vertically like
this. So there we can already see that
we are missing the gap that we wanted.
But that's the CSS part of the
challenge. Let's continue on with the
HTML and get that wrapped up. We'll do
on click equals and then run this new
card function which we haven't created
yet which we are going to create right
now down here function new card open and
close parenthesis and then the curly
brackets for the body of the function
inside here logging out this string we
got provided in the comment like that
I'll run this code hit new card and
indeed we are logging it
Now final step is to add this gap right
here. We need to head into the CSS and
there are multiple ways to do this. One
way is to just say that we want any
button to have a little bit of margin
below them. So this is about four
pixels. So we could do
margin bottom four pixels like that.
There we see that our button works.
Alternatively, let's say that we in some
case end up with a situation where
something comes too close to the button
on the top side of it. Well, maybe we
need some margin on top of it as well.
But if I now add margin bottom and
margin top or four pixels here, margin
top four pixels, what you'll see is that
the gap here became twice as wide
because that has the top margin that
pushes it down and also the start game
button that uses its margin bottom to
get the gap even larger. So what we then
instead can do is take two pixels on
each. Now we have something that looks
very much like this. And we have created
our buttons so that they always have
some space below and above themselves.
And I think that makes a lot of sense as
buttons usually want a little bit of
space around them. With that, let's move
on.
Okay, let's make our new card function a
bit more useful because now it's just
rendering out this string. What we
wanted to do is to actually draw a new
card and then add that new card to the
sum and then rerun the code that is
inside of the start game function so
that the sum is added to the page. And
actually later down the line, we also
want to show the new card of course, but
as for now, we're just going to worry
about the sum. So, this is a three-part
challenge where you are to first create
a card variable, and we're still just
working with hard-coded card variables.
You're just going to code it to a number
between 2 and 11. Then, you are going to
add this new card to the sum variable.
And the sum variable is the one up here,
of course, which currently is the first
card plus the second card. And in this
case, it'll be plus the third card as
well. So, you need to plus equal the sum
variable. And then finally you are going
to just call the start game function
because what does the start game
function actually do? Well, first it
renders out the two cards. In our case
now we'll have added the third card. So
this will still be wrong but line 13
will render out actually the new sum
because at this point sum will have
changed since we'll have modified it
down here. And then it'll show the
message based upon the new updated sum.
So actually it seems that we can use
this start game function even after
we've drawn a new card and thus we're
going to rename it a bit later. But as
for now let's just keep it how it is and
your job is just to call the start game
function here on line 33. So go ahead
and give this challenge your best shot
and then I will show you the solution as
well afterwards.
Okay, let's do this. So we do let card
here. We'll set that to a random number
between 2 and 11. I'll do seven. And
then we're going to add the new card to
the sum variable. If you remember how we
add numbers to an existing variable,
it's via the plus equal operator. So
we'll do sum plus equal card. And then
we'll call start game. So start game
like that. Let's rerun the code. First
do start game. Okay. Do you want to draw
a new card? I happen to want to do that.
So I'll hit new card. And boom. I drew
the seven card which then made the sum
reach 21 which then again made our
dealer tell us that we got blackjack. So
that is exactly what we want. Now let's
see what happens if we instead of a
seven got an eight. If I now rerun the
entire code, start game and then hit new
card. Bam. Then we're out of the game
since we have sum equals 22. And if we
got a six instead I'll rerun the code.
Start game. New card. Then we still get
the do you want to draw a new card text
since we're still below 21. Now I can
actually hit the new card yet again and
then we are out of the game. Now we're
at 26. So this works for multiple cards.
Of course it's only the sum that's
reflected right now, not the cards. So
we'll have to fix that in a bit. But for
now, let's just move on and first fix
this little problem that the start game
function name doesn't really make sense
anymore since we're calling start game
here at a point where the game has
already started. So, semantically it
doesn't really add up. So, let's move on
to the next cast.
So the problem now is that the start
game function is triggered both when we
hit the start game button, which makes a
lot of sense, but also when we click the
new card button because even though the
new card button triggers the new card
function, which is this one right here,
that function itself calls start game as
well. And while this makes sense because
we want to run this code, the fact that
start game still is called start game
doesn't really make sense because if
another developer comes in here and
looks at that code, he'll be like, um,
why are we starting the game all over
again when clicking a new card, that
doesn't semantically add up. So, what we
want to do here is actually just change
the name of the start game function, for
example, to render game because that's
essentially what we're doing. We're
rendering out the current state of the
game. So we can do render game just
rename this one and this one to that. So
far so good. But then the problem
becomes that we're actually calling
start game here. And we could do render
game like this. But actually I want to
keep this start game function intact
because down the line there might be
some other things you want to do in this
game initialization function. So I want
you now to create a function that uses
the name start game and then just calls
or invokes render game. That's all it
should do. So that this start game
button will work. Because now you can
see it doesn't work. When I click it,
we're actually getting a reference error
saying that start game is not defined
because we're trying to call a function
that no longer exists. So go ahead and
create the start game function here on
line 12 and make it just call this
render game function right here. Go
ahead and do this right now.
Okay, hopefully that went well. The way
to do it is through function and then
start game open and close parenthesis
open and close curly brackets and inside
here just call render game like that.
Let's rerun the project. Hit start game.
And yes, now it works again. Great job.
Okay, so now we need to fix up the
rendering of the cards because if I hit
start game and I get the two cards out
and then hit new card, indeed our sum
increases, but our cards paragraph are
displaying only two of the three cards
we've pulled from the deck cuz we have a
10 and a four and a six. whereas we're
only showing the 10 and the four in the
browser because that is hardcoded here
to only do that no matter how many cards
we pulled from the deck. This line 16
will always just render out the first
card and the second card. So this line
here in pseudo code says render out
first card and second card. But what we
instead need is something that says
render out
all the cards we have regardless of how
many cards. If we have five cards, it
should render out five cards. If we have
two cards, it should render out two
cards. But that's not possible with the
current implementation. And what we need
is a central way of storing all of our
cards. We need to store all of our cards
in one variable regardless of how many
cards it is. And the way to do this is
actually to use what's called an array,
which is essentially a list of items.
And I'm going to create an array here.
And it's totally okay if you now feel
confused. I just want to give you a
little sneak peek at it before we take a
deep dive and give you some practice on
it. I'm going to do let cards equals and
then open and closed square brackets.
That's the notation for creating arrays.
And inside of these square brackets, I'm
going to paste the first card, then a
comma, and then the second card. So,
this is now a cards array that includes
both cards, ordered by the first card
first and the second card second. You
can call this an ordered list of items.
Now, we down at what's line 18 now. and
replace the first card by referring to
the array instead and doing open and
close square bracket and zero. Don't
worry about that. You'll understand that
in a minute. Let's just now do the same
things for the second card. Then we'll
do cards open and closed square brackets
and one.
So this is referring to this one right
here. And one is referring to this item
right here. And that is because arrays
are so-called zero indexed. They start
at zero and one. And if we add a third
card here, that would be index number
two. But we don't have a third card. So
let's remove that.
Now, even though you might feel a little
bit confused now, let's just run the
code, hit the start game, and see yes,
that it still works. So this way of
doing it was the exact same as what we
had previously when we just hardcoded in
first card and second card like this.
Now, this opens the possibility later
down the line to render out all of our
cards in a dynamic way inside of the
cards element regardless of how many
cards we have inside of our cards array.
So, that was a lot of information. Now,
we're going to take a step back, work
with arrays, get your hands dirty. So,
don't worry if you didn't quite
understand this as you will surely do
that after the next few lessons.
Okay, so arrays are ordered lists of
items. However, that's a little bit
abstract. So, let's instead look at a
real world product and see if we can
recognize any components that most
likely use arrays as a data structure.
So, here we have a LinkedIn profile.
This is Justin. He's a previous Scribba
student. And looking at this, there's
immediately multiple components that
most likely use arrays to render stuff
out. You can see here on the right hand
side there are some promotions actually
three of them and that's basically a
list of items and while their order
might be a little bit random they still
have a specific order. Likewise with the
people also viewed here that's a list of
four people. Let's also scroll a bit
down here. We can see even in the main
section there's this component that says
featured. That is featured posts and
that's basically just a list of items.
So, LinkedIn probably uses an array to
describe this data in JavaScript. So,
actually, let's just create that array.
Lette featured posts like that and then
equals open and closed square brackets.
The first one is actually Justin sharing
the Netflix clone he built through a
course here on Scribba and he actually
deployed it as well. So, he's going
above and beyond. So, check out my
Netflix
clone like that. The second one is
actually a link Justin has shared to
source code he's pushed to GitHub, which
is also a great thing to do if you want
to track employers. So here's code for
my project.
I'm simplifying, of course, the data
here because there are multiple parts of
this. There's an image, there's text,
there's comments and so forth. But as
for now, we're just simplifying it into
strings. The third item here is actually
Justin sharing the portfolio he has
built through a scribble course as well.
So I've
relaunched my portfolio like that. There
we have an array with three items. Now
this line is pretty long. So sometimes
you'll see an array formatted like this
as it makes it easier to see that it is
indeed three items. And a key thing to
observe here is that we add commas after
the items but not after the final item.
So only between items basically. So
going back to LinkedIn, there are tons
of components here that's probably
utilized as arrays. For example, the
work experience, that's a list of
previous jobs or licenses and
certifications. That's a list of
licenses. You can see the front-end path
right here. And skills and endorsements.
So, a ton of examples. And now what I
want you to do is to create an array
that lists your experience or education
or licenses or skills or something
similar. You are to choose what you want
this array to contain. But try to think
how can you best describe your either
experience, education, skills or
whatever in a way that would make your
LinkedIn profile look tempting for
recruiters as that's a smart thing to
think a little bit about already even
though you're very early in your coding
journey. So go ahead and do that. And
remember that the items should just be
strings just as I've done up here. Go
ahead and do this right now.
Okay, hopefully that went well. Now I'm
going to do it as well. So I'll do let
and then experience. That's what I want
to create my array about, my work
experience. And I'm currently the CEO at
Scribba. Before that, I was a front- end
developer at Sinetta. That's a Oslo
based company for seaf freight analytics
and also as you probably remember from
our subway counter app I worked as a
people counter out in the cold a company
called Norstat. So that is my
experience. Now before you move on to
the next cast I want to highlight the
importance of actually being able to
describe your background and your skills
in a good way on LinkedIn. Justin here,
the person behind the LinkedIn profile
we looked at, he actually lost his job
in the petroleum industry due to the
coronavirus pandemic. And less than one
year later, he now works as a
professional software developer. And as
I said, he did that mostly through
Scribba and was a super engaged student
at our Discord channel. And what's so
cool about how he did it is that he sent
out over 100 job applications without
getting much luck. but then went into
optimizing his LinkedIn and then
suddenly he got two job offers and the
job he got actually came through a
recruiter that found his LinkedIn
profile as a result of keyword searches.
Actually, he ended up tweaking his
LinkedIn profile so that he attracted
the right recruiter who got him an
amazing job as a developer. So, I wanted
to share this with you. And if you click
on this image, you'll be taken to the
page for the podcast interview we did
with Justin so that you can listen to
his story if you want to. For example,
when you're riding the bus or out
walking or cleaning or whatever, I don't
want you to interrupt the course and
listen to this instead of coding. It's
more important that you code, but you
can keep it in mind for the next time
that you have half an hour or something
because I think you'll learn a ton about
Justin who at the age of 33 did his
career shift. So, with that little plug
out of the way, let's move on.
An important thing to know about arrays
is that they are so-called zero indexed.
And that means that you want to grab the
first item of the array, you do it
through using the zero index, not the
first index. And let me show you what
that means in practice. Featured posts,
that's our array. And I want to grab the
first item, the check out my Netflix
clone. I'll do that through open and
close square brackets and then passing
in the number zero. If I now console log
this
and we run the code, you can see yes
that we are indeed logging out that
string. So this isn't entirely intuitive
since we as humans normally start
counting at one. But in arrays you start
counting at zero. So if this is the
zeroth index,
which index does this item have? Take a
guess.
Well, it has the index of two even
though it is the third item. I run the
code to now see that passing in two logs
out. I've just relaunched my portfolio.
So, here is the zero index. This is the
first and here is the second. Now, with
this in mind, I want you to practice
this. So, you are to take my experience
array here and log out the following to
the console. and in this order. So you
are to start with the front- end
developer in Asanetta, then the people
counter and then CEO at Scribba. So
first this one, then this one, and then
the first one. So you have to write
three console logs and place them in
order here starting from line 18 so that
I'm sure that you actually understand
the concept of arrays being zero
indexed. I'm going to comment out this
one right here so it doesn't disturb
you. Now go ahead and solve this
challenge.
Okay, hopefully that went well. Let's do
this together.
I'm going to do control log. I want the
front-end developer at Seretta that is
not the first but the second item and in
terms of indexes, it's the zeroth and
then the first index. So then I need to
do experience
one. Okay, I'm going to copy this one,
paste it in again. Now I'm going to log
out the third item, which is the second
index. So that is experience 2. And
finally, I'm going to log out this item
right here that has the index of zero.
If I run this code, you can see in the
console that we are indeed logging out
the items in this order. Now, before we
move on, I'm going to confuse you a
little bit because let's actually delete
all of this this remove the console and
and just go back to this featured posts.
If I now do featured posts dot length
and then console.log
this out, which number do you think
we'll see printed to the console? Take a
guess. Well, the answer is three. And
that might be a little bit confusing
because I've just taught you that arrays
are zero index. So the first is zero,
the second is one, the third is two. So
it would be natural to believe that
thelength property of this feature posts
is two because the final index is two.
But whereas indexes are zero indexed,
the length is not zero indexed. We count
the length of an array in just the same
way you would count anything in life,
which is starting at the number one. So
that also means that the length property
of an array is always one larger than
the final index. So this is one of those
things that are a little bit unintuitive
and confusing when learning JavaScript
and you probably will mix it up a couple
times. That's not a problem. Just trust
me when I say that eventually this will
become second nature for you. So let's
move on.
Okay, up until now, our arrays have
either included only strings or only
numbers. However, they're not limited to
that. Actually, put any data type you
want into an array. And you can mix as
much as you want. So, you can think of
an arrays as kind of a composition of
multiple data types and their values.
So, this is why arrays are what we call
a composite data type. You can say it's
composed by its items if you needed to
make semantically sense. And you'll
often hear this also referred to as
complex data types. That is arrays and
also for example objects which you are
going to learn about later. They stand
in contrast to the primitive data types
you have worked with up until now. The
three primitives you have learned are
strings, numbers, and booleans because
they are more primitive than arrays.
They're not composed of many other data
types which arrays often are. So one
analogy you can use is that the
primitive data types are the Lego blocks
whereas the array the composite data
types is kind of like the full Lego
model. For example, after you've built a
little house with your Lego blocks. But
anyway, you shouldn't worry too much
about this if you find it a little bit
abstract and complex. As a developer,
you don't go around thinking about, oh,
is this a primitive data type or a
complex data type. it just becomes
second nature and you don't even think
about it. So you shouldn't be put off by
this even though it's a little bit
theoretical. Instead, I think you should
just practice. Now, I want you to create
an array that describes yourself and use
all the three primitive data types that
you've learned so far. So, it should
contain your name, which is a string,
your age, which is a number, and whether
or not you like pizza, which should be a
boolean. So, go ahead and do this right
now.
Okay, hopefully that went well. Now,
let's do it together. I'm going to
create an array called pair
and my name is Pear Harold Borggan. I am
35 years old and I do happen to like
pizza and I especially like pizza with
pineapple.
It's the best. Though, I know that a lot
of you don't agree with me. So, let's
not get into a discussion about that.
Let's just move on with the course.
One of the most common operations you'll
perform on a race is adding items to the
end of it. That's for example what you
want to do in our blackjack game where
you start with two cards and then you
want to draw a new card. That'll
naturally then be put at the end of the
array. For example, six. However, the
way to do that is not through actually
changing it directly which I did. Now
you use a method which is called push
which allows you to well push things
into the array. So you do cards dot push
and then open and close parenthesis. And
inside of these parenthesis you place
whatever you want to push. So for
example six. If we now console log the
cards underneath our push operation
you can see we have added six to the
array. So this here is actually called a
method which is just a fancy way of
saying a function on an object. And you
haven't really learned about objects
yet. So don't worry about if you think
this sounds a little bit abstract. But
this operation here is pretty similar as
the console.log
then passing in for example hello. Don't
you think? We have the first object
cards or console. Then we have the
methods which is either log or push.
Then we have whatever we pass into these
methods when we call them. So this here
cards push is conceptually very similar
to what you've been doing all along.
Let's now make sure that you build
muscle memory on this by solving a
challenge.
So I want you to imagine that you are
building a chat application for example
the one I showed you here on LinkedIn.
And the data structure we'll use is of
course arrays. And we'll pretend that
each of these messages is an item in the
array. So when someone types in a new
message and sends it, well, you want to
push that new message to the array. So
here you are to push the new message
variable, that's this one down here,
into the messages array, that's this one
here. And then you are to log out the
array to just verify that you actually
succeeded in pushing the item. Go ahead
and do that right now. and then I will
show you the solution afterwards as
well.
Okay, hopefully that went well. Now
let's do this. We'll do messages dot
push and inside of the parenthesis we'll
pass in new message like that referring
to the variable and not the hard-coded
value which we did up here when we
passed in six. So a little bit different
but still the same concept. Now let's
console.log log messages and then run
this code. If we bring up the console,
we have pushed the new message to the
array because we're logging out the
array with same here at the end. Now, a
little mini challenge before you leave.
Let's say that you wanted to regret
sending this message same here and you
wanted to remove it from the messages
array. How can you do that?
You haven't learned this yet, but this
is a golden opportunity for you to
Google it out and try to figure out how
can you remove the last item in an
array. So just see if you can find the
answer on Google and then try to remove
the last item of the messages array here
on line 18. Go ahead and do that right
now.
Okay, maybe that worked, maybe not. If
not, no worries at all. This is just a
bonus. What you do is do messages
dot pop and then open and close
parenthesis. Now
if we log the messages again and then
run the code, open up the console, you
can see we are logging two versions of
this array. one from line 15 where the
same hair string is included as the last
item and once again from line 19 where
the array is back to how it originally
was like this. So pop is kind of the
opposite of push and not as commonly
used but I just wanted to give you a
sneak peek at it as you might come
across it here and there in your
development journey. With that let's
move on.
Okay, now it's time for some repetition.
You are going to do the exact same thing
as I did before we had our little arrays
deep dive. So, you're first going to
create a new array cards that contain
the first card and the second card. And
then in the render game, you are going
to change line 18 so that it refers to
the cards array when rendering them out.
So, not referring to first card and
second card this way, but doing it via
our freshly created cards array. So go
ahead and do this right now.
Okay, let's do this together. First,
we'll do let cards equals open and
closed square brackets. And then we'll
set the first card as the first item in
the array and the second card as the
second item. And as you hopefully
remember, we can now refer to these
values by doing cards zero and cards
one. Like that. We'll run the code, hit
start game, and yes, indeed, it works.
Great job. Let's move on.
Now that we have the first card and the
second card in the cards array, we also
have to, of course, add the new card to
the cards array. So, quick little
challenge. After this card has been
created and it has been added to the
sum, we also need to push the card to
the cards array. So go ahead and do this
right now.
All right. The way to do this is by
writing cards dot push open and close
parenthesis and then pass in the newly
created card like that. And in order to
verify that this actually works, we can
on the next line console log out the
cards array like that. Let's run this
again. Start game. Hit new card. And
there we can see in the console now the
cards array has been logged out with all
the three cards 10, four, and six. So we
still aren't rendering out the third
card though. And to achieve that, you
have to learn how to count in
JavaScript. or strictly speaking, you
have to learn how to write for loops. So
over the next few casts, we are going to
take a deep dive into for loops and then
later return back to the blackjack game
so that we can fix it up. Let's move on.
Okay, so now you're going to learn how
to count with JavaScript. That might
sound really silly. I mean counting is
so easy. If you ask a human to count,
for example, if you say pier, count to
10. I would go, yeah, sure. 1 2 3 4 5 6
7 8 9 10. However, JavaScript is like
that annoying person who then replies
and said, "Well, where exactly do you
want me to start counting?" And by
counting to 10, do you mean including 10
or should I stop before 10? And also,
should I count every single number or do
you want me to, for example, only count
the odd numbers or the even numbers or
whatever? So, JavaScript is really like
that annoying person that takes
everything you say 100% literal. So when
you ask JavaScript to count, you have to
be super specific. We need to specify
where we start counting, where the
finish line is, and what the step size
is. Here, I've already written the code
for you. So let's just dissect it. We
start with the for keyword. That's
because this is a so-called for loop,
but don't worry about that. Let's now
look at the things that are inside of
the parenthesis here. Here you can see
our starting condition. Let count equals
1. So, we're going to start our count at
one. Then we have a semicolon. And then
we specify our finish condition where it
says count is less than 11. So, we want
count to always be less than 11. So,
maximum 10. In other words, we're never
going to count over this number. And
finally, the step size is that for each
counting, you're going to increment with
one. So, count plus equals 1. Now these
three conditions together describe to
JavaScript how many times we want to run
the code inside of the curly brackets
and what the value of count will be at
every single iteration. So here we are
going to run this code 10 times and the
variable count will change its value in
every single iteration starting at one
and going up to 10 jumping one number at
a time. So we will count to 10. Let's
run this code. And there you can see in
the console we get the numbers from 1
and up to 10. If we now change this here
to 2, what do you think will happen?
Well, then we get 1 3 5 7 9 because we
still start at one. We're going to end
before 11. So maximum 10 and we're going
to jump with two. So in the first
iteration, count equals to one. It's
what we said we wanted to start with.
But then in the second iteration, we've
told JavaScript that we want to increase
by two every single time. So then it's
three, and then it's five, and then it's
seven, and then it's nine, and then at
the last iteration, it's like, okay, now
I'm going to be 11. But uh that doesn't
work. That violates our finish condition
here. So JavaScript stops the loop
before that happens and then continues
on with whatever it has to do underneath
the loop. So let's take this back to
one. Can you modify this one so that it
counts from 1 to 20? How would you do
that?
Hopefully you identified that what you
want to change is the finish condition
and set that to less than 21. We run
this. You can see in the console that
we're logging out 1 to 20. Now, how
would we change this if we want to count
from 10 to 20? Go ahead and try that
right now.
Okay, hopefully you figured out that it
is the starting condition you want to
modify. You want to write 10 here so
that it starts at 10, ends before 21,
and counts one at a time. We run the
code. You can see we get from 10 to 20
logged out to the console. So hopefully
you've gained a little bit of intuition
on this. Now let's move on to the next
cast.
Okay, let's first walk through how to
create a loop from scratch. And then I
want you to create your own loop or at
least try according to the
specifications here on line three and
four. But to begin with, we'll create a
loop that counts from zero to five. So
to do that, we'll do four open and close
parenthesis. And then inside of the
parenthesis, we have to define our
starting point, our finish line, and how
big our steps will be. This is done
through a counter variable. So we could
do let count equals zero and then
semicolon to end the starting statement
because zero is where we want to start.
However, where's count is a very
semantically good word for this
variable, a common convention in
JavaScript is to use the letter I
instead.
We could have named it whatever we want.
And you can use count if you think
that's easier, but I'm going to use I
from now on. Then there's the finish
line which we also need to define. We
want to count to five. So I should be
less than six and then semicolon again.
So this defines that I should always be
less than six. And obviously then the
last point where it will stop when
counting upwards is five. And then
finally, we have to define how big our
step should be. And I'm going to do I
plus equals 1. And here we don't have to
use a semicolon to end because this is
the last condition. Actually, I can
space it out a little bit. We follow the
same structure for all of our
conditions. And then the open and closed
square brackets for the body of our for
loop. Then inside of here, we'll do
console.log
log I from the code and we can see 0 1 2
3 4 5. Good. Okay. Now that you've seen
this, I want you to try to create a for
loop that counts from 10 up to 100 in
steps of 10. So it should count 10 20 30
40 50 and so on including 100. And I
want you to use control log as I am
doing here to log out the count at each
step of the iteration. So go ahead and
do this right now.
Okay, hopefully that went well. If not,
no worries. Let's do this together. Loop
four. Let And here you might wonder if
well, we've already used the I variable.
Can we reuse it down here? And the
answer is yes. I is only defined inside
of the curly brackets of the for loop
when you do it like this. So outside of
it, for example, down here at line 8,
JavaScript won't remember this i
variable. So whatever you've configured
it to mean up here inside of the curly
brackets doesn't affect whatever you set
I to down here. So we can do let I again
equals 10. That's where we want to
start. I should be this time less than
not 100 because then it'll stop at 99.
It needs to be less than 101. And
finally I should not be plus= 1. Well,
should be plus equals 10 because at each
step in the iteration, you want to
increase I with 10. Console log I. I'm
going to comment this one out. Run the
code. And there we see 10, 20, 30, 40,
50, and so on all the way up to 100. So,
great job.
Okay, now you're going to experience how
powerful for loops are when combining
them with arrays because let's say that
we are building a chat application like
the one that LinkedIn has which I showed
you earlier and these messages might
just well be items in arrays. Let's just
pretend that each of these messages are
exactly that and ignore all the metadata
around the messages like the time and
the profile picture and name and stuff
like that and just pretend that we have
the data structure which contains a
bunch of messages inside of an array.
Basically just what we have here the
exact same array we've worked with
earlier as well. Now pretend that this
is a consolebased array. So everything
basically need to do is log out these
messages to the console. But in order to
do that, we can't just console log out
messages because then we'll just get a
long array and that's not what we want.
We want each of the messages on its own
line and not with the square bracket and
stuff like that around. So the hard way
to do this is by doing like this
messages one, messages two, messages
three, like to take the index from zero
to three and log all of them out. There
we go. Now we have what looks like a
chat in the console. But this is a
really bad way of doing it. We are
repeating ourselves again and again and
that's a general anti-attern. There's
something called dry for don't repeat
yourself. So don't write more code than
you need to and especially not if you're
just repeating the same code. And also
this doesn't scale because if I add now
at the end of the array and run the
code, I'm not logging out the last one.
I have to manually update my code. So
what we instead want to do is combine
this array with a for loop. Now let's
comment out this and write four. Let I
equals zero semicolon.
Then we want I to stop before it reaches
five. Then we want I to increment by one
at every single iteration. Now inside of
this for loop we log the value of i
see in the console that we get from zero
to four and that actually resembles
quite a lot what we did here. However,
we've even added the index of the latest
item as well. So, how about we instead
of just logging out I simply do messages
open square brackets close square
brackets
use the I as the index and then run the
code and boom there we go. We have all
of the items logged out in order one
after another. Brilliant. However,
there's still a little problem here that
if I add yet another message here, maybe
an emoji, and I run the code, the emoji
isn't logged to the console. Then I have
to change this to six again. So, it's
still not fully like dynamic. However, I
have a trick because how can I get a
hold of the number six in a dynamic way?
Well, you might remember that arrays has
a property which is called length and
that will refer to the actual length of
the array. If we use that, we can be
sure that our loop counts up to the
final index all the way up to the end of
the array. If we now run the code, yes,
there we have all of our messages
regardless of how long the messages
array is. So this was a lot of typing
from my end though. So let's move on to
a challenge so that you get to practice
this as well.
Okay. So now let's return back to the
example of having cards in an array like
we have in our blackjack game. So here
I've created an array that has three
cards of the values 7, 3, and 9. And I
want you to create a for loop that logs
out all of these cards to the console.
However, not by hard coding where the
loop should stop counting, but using the
cards.length property. So, go ahead and
do this right now and then I will of
course show you the solution as well
afterwards.
Okay, hopefully this went well. Let's do
it. We'll do four open and close
parenthesis and then let I equal zero
because we're going to start at the zero
index.
I should be less than cards dot and I
should be increased with one at every
step in the iteration. Now here for this
last condition, I actually want to show
you another trick because what you can
do here is actually do I ++ that'll do
the exact same thing as this. It's just
syntactical sugar that JavaScript
provides us with to make our code a
little bit neater.
So I ++ increment I with one. So now we
can do console log cards then passing in
the I run the code and there we can see
7 3 and 9 printed to the console. Great
job.
Okay, this is so cool because now you
are going to combine what you've learned
about arrays and for loops with what you
know about manipulating the DOM. So,
we're going to pull the strings together
and have you render out this sentence
inside of this greeting L. So, you can
see we have in our HTML just an empty
paragraph which has the ID of greeting
L. We have fetched that element from the
HTML and stored it in this greeting L
variable. And your job is to render the
sentence that is the array here. So, you
are to render all of these items inside
of the array in the greeting L
paragraph. And the way to do that is to
use a for loop to loop through it and
render each of them into the greeting L
using text content. So a lot of stuff
you have to keep track of here, but give
it your best shot and then I of course
will show you the solution when you
return back to this screencast.
Okay, hopefully that went well. Now
let's do this. We do four. Let I equals
zero because we want to start at the
zero index in our array. We'll do I is
less than sentence.
[Music]
And then we do I ++ because we want to
increment one time. Here we need to grab
a hold of the greeting L and then do
text content. And here you might have
been tempted to do equal sentence open
and close square brackets and passing in
the index. That will just end up with
rendering pair because first it renders
hello and then it clears out all of the
text content and renders the second
word. But then for every single time in
this loop it clears out all the text
content and we end up with just the
result after the last iteration and that
was the last word in the array. So what
we have to do we have to keep the
previous content intact inside of the
paragraph and as you might remember we
do that with plus equals. So running the
code again. Yes, now it works. Now
though I have a bonus challenge for you,
which is the following. How do you keep
the spaces between the words if I remove
these spaces from the array? That is if
I do like this, like this, like this,
and like this. And we render it out. You
can say, ah, we have no more spaces. It
looks like one long weird word. How can
we on this line seven modify it so that
we get spaces between the words? Go
ahead and try that.
Okay, what we will do is we'll simply at
the end here add a plus and concatenate
our given item in the array with a
space. So now we add a space after each
of the words. If we render this, you can
see yes, we get now the sentence exactly
how we want it. Great job. Let's move
on.
Okay, back at our blackjack game. It's
time for you to create a for loop that
renders out all the cards in the cards
array instead of just the two first ones
which we are doing up here. So you are
going to start your for loop down here
at line 19. I'm going to help you out a
little bit because we are going to keep
this first setting of the text content
here so that we have the default text
inside of the paragraph. And then you
are going to render out the cards after
this text inside of the paragraph. Go
ahead and create that for loop so that
our black deck game supports any number
of cards inside of the cards array and
will render all of them out regardless
of how many it is. So give it your best
shot and return back to me when you want
to see my solution.
Okay, hopefully that went well. If not,
no worries. We'll do it together. We'll
start with four and then open and close
parenthesis. Of course, inside of the
parenthesis, we need to define that we
want to start our incrementer at zero
and we want to stop it before it reaches
the length of the cards array. How do we
do that? Well, by I is less than cards.
And then a semicolon and we want to
increment i at every iteration. The body
of the for loop with curly brackets.
Here we are going to plus equal onto
this paragraph. So we'll do cards l.ext
content plus equals. Let's start by just
adding the cards and then passing in the
current value of I our iterator which
will start at zero and to begin with end
before two. So it'll be 0 1 and then run
the code start game and yes it works.
However, it looks like it says 104 here
because we haven't added a space between
our numbers. So the way to do that is
simply by concatenating
a space after vin card. Now if we run
it, start game. Yes, we've got it. And
if we hit new card, now we can see both
the sum and the cards are now correct
according to the state of our game.
Great job. Let's move on.
Okay, so our blackjack game is actually
turning out pretty good now. Can start
the game. We can hit a new card. 20. I
can even hit yet another card. Boom. Ah,
I'm out of the game. It's It's almost
fun to play, though, you'll get pretty
tired of it as you play it multiple
times since you every single time get
the exact same cards. So, not exactly an
exciting blackjack game. And of course,
that is because we have hard-coded the
variables for our first card and second
card here and also hard-coded value for
our third and fourth and every preceding
card here on line 40. So we'll always
get 10 4 6 66 66 66 66 66 66 66 66 66 66
666 and so forth. So what we need to do
is instead create a function get random
card that returns a random number so
that we instead of hard- coding the
number 10 up here on line one, we can do
get random card and call that function.
This function will then return a random
card that the first card variable will
be set to. And we can do the same with
the second card. And also down here in
the new card function, we can do get
random card there as well. This way we'd
get a functional blackjack game.
However, there's a few things you
haven't learned yet which you need to
learn in order to create this get random
card function. So let's rewind a little
bit here. Get back to the hard-coded
numbers. And over the next few casts,
you're going to learn how to return
values in functions and also how to
generate random numbers inside of a
function. So let's go ahead and do
exactly that.
Okay, let's return back to our racing
game example in order to learn about
returning values in functions. So here
we have two players who've raced through
the game and player one got the time 102
and player two got the time 107. And our
job is to create a function that returns
the fastest race time. So in order to do
that, we first have to do function. I'm
going to call this get fastest
base time. Quite a long word, but at
least it's descriptive. And inside of
the body of the function, we have to
first check which of these times were
fastest. So we'll do an if statement and
say if player one time is less than
player two time well then we are going
to return
player one time
because if it's less than player two
then it's the fastest race time and we
are going to return it. Now this return
keyword here might now seem a bit weird
and you probably don't understand yet
what it means but don't worry about that
right now. It'll be clearer as we move
on. And then we're going to do else if.
The next condition is if player two was
faster than player one. That is player
two time is less than player one time.
If so, we are going to return player two
time like that.
Then an else if actually these two are
equal. So none of these expressions
evaluate to true. Well, then the fastest
time is any player's time because they
had the exact same time. We either
return player one time or player two
time. It doesn't matter. I'm just going
to return player one time. Okay. Now,
what we can do is we can do let fast as
race. I'm going to call this variable
that. And now I'm going to define that
declare that variable as the result of
invoking this function. So I'm going to
do get fastest race time like that.
Now the value of fastest race time will
be whatever this function has returned.
And if we console log as this race time
we can see we get 102. That's correct
because this function compared player
one and player two and rightfully
recognized that player one was faster
than player two and thus returned player
one time. What you might think now is,
well, we're just console logging out the
value here. What's the point? Why
couldn't we just instead of these
returns just do console
log and do that directly inside of the
function? Do that first and run it. You
can see yes, we are now indeed running
this line six here. We're console
logging out the value. But if we down
here try to see what is the value of
fastest race at this point and run the
code, you can see that we're getting 102
first from this line, but then we're
getting undefined from line 16 because
now the fastest race variable simply
isn't defined because this function
didn't return anything. And it's
actually quite valuable to have stored
the fastest race time inside of this
value as opposed to just having console
logged it out up here cuz now we can use
this variable for something else. So
let's do like this and return instead
and move over and remove the parenthesis
as well. Wondering how I got the cursor
to be on multiple lines at the same
time. It's a trick where you first
select a word and then do command D or
if you're not on a Mac control + D.
Select the word command D one two times.
And now I have selected all of the
return keywords so that I can delete
them or retype them. That was just a
quick little tip. Now it's time for a
challenge.
So you are going to write a function
that returns the total race time. Then
you are going to call or invoke the
function and then store the returned
value in a new variable. You have to
decide for yourself what you want to
call that variable and what you're going
to call this function. Then finally you
are going to log your variable out just
to check that everything has worked. So
go ahead and do this right now.
Okay. Hopefully this went well. Let's do
this. audio function get total race time
and the way to do that is simply to
return player one time plus player two
time. Then we can create a new variable
called let
total time equals get total race time.
And now we can console log total time
like that. Running the code and indeed
we get 209 in the console which sounds
just about right. So great job. Let's
move on.
Okay, it's time to take what you've
learned about returning values and
functions and implement that technique
into our app. This is a three-part
challenge. First, you're going to create
a function get random card that always
returns the number five. And then you're
going to use get random card to set the
values of the first card and the second
card. And then use it to set the value
of the card variable down here in the
new card function as well. So you are
going to write your function get random
card here starting on line 14. And now
you might think, well, that doesn't make
sense because we've previously learned
that if you defined a variable down here
on line 14, we can't use it up on line
two and three. That's at least how it is
with variables you define using the let
keyword. And that is exactly right. But
when you create functions in the way we
do, there's an exception. So I want you
to just try this and see if it works.
And then I'm going to explain afterwards
why it works. Go ahead and give this
your best shot. and then I will show you
the solution as well afterwards.
Okay, hopefully that went well. Now
let's do this. We'll do function get
random card like that. Then inside of
the body of the function, we'll simply
return five. We need to use this up here
at line two
get random card and on line three get
random card. So what's happening here is
that we're calling this function it will
return five and thus second card and
first card will be initialized with a
value five as will our card down here.
If we do get random card and call it
these two really important otherwise it
won't work but with them card will now
have the value five. So let's run our
blackjack game. Start the game. Ooh we
got two fives. Ah yet another five.
What's the chance? Obviously, it is 100%
at the moment, but that doesn't matter
because we now have the setup for inside
of this function writing the logic for
getting an actual random card back. So,
that's great. Finally, I promised to
talk a little bit about the fact that
we're creating the function down here on
line 14 and then using it up on line
two. That seems to go against what we
learned about variables early on.
However, when you write functions like
this, which is called function
declarations, the function gets
so-called hoisted to the top, meaning
that it's accessible even at line one.
Regardless of how far down you define
the function, we could have written this
function on line 10,000. It would still
be accessible on line one. Now, there
are other ways of creating functions
that don't give the function this extra
benefit. But as for now, we're only
going to write functions in this way. So
don't worry about that because now you
have another thing to worry about and
that is how do we actually modify this
get random function so that it actually
returns a random card. So from the next
lesson on we are going to dive into
exactly how to do that. I'll see you
there.
So I remember when I was learning this
slightly clunky way of generating random
numbers in JavaScript and thinking well
why do I really need to generate random
numbers? Is that really such a common
thing to do for developers? And
actually, it is. Of course, there's our
use case where we're building a card
game. Obviously, we need to have some
kind of randomness in which card you
get, otherwise it would be meaningless.
But also, imagine all games that use
dices. A dice is basically a physical
randomness function that gives you
random number between 1 and six. But
randomness isn't only important in these
kind of casino-ish games. I would say in
all computer games, randomness plays
some kind of role. For example, in this
popular iPhone game, Crossy Road, where
you are to cross a road as a duck
without getting hit by a car. Surely,
there's some randomness underneath here,
which decides which cars appear on the
road and also when they appear. But it
doesn't stop there with games. Actually,
in cyber security, random numbers is a
key component in order to authenticate
yourself and generate keys as within
cryptocurrencies. So, for example,
Bitcoin uses random numbers as well. So
random numbers plays a role in the
crypto economy as well. And most
importantly, we used random numbers in
order to generate the suggested tweet
text in our JavaScript advent calendar.
So as you can understand, random numbers
are everywhere and you need to learn the
most basic way of generating that in
JavaScript and that is through something
that we call the math object. That's
something that's already provided to you
in JavaScript or by the browser to be
specific. And you can type math wherever
you want with a capital M and then do
dot random and then open and close
parenthesis so that you call this
function or method. And don't worry if
you don't quite understand what's going
on under the hood here. You're not meant
to do that right now. All you're meant
to know is that that will return a
number to you which I've now stored in a
variable called random number. And then
I'm logging out this random number. What
I want you to do now is pause this
screencast and run this code a few times
so that you get to see a few of the
values that our random number variable
can take and then try to as succinctly
and specifically as possible. Write your
answer at what you think math.random
does. So what does it return? Try to
formulate that and write it down and
then return back to the screencast when
you think you have the answer.
So the way I would write this is that it
generates
a random number between zero and one and
then parenthesis
not inclusive of one. So it generates a
random number including zero and one but
not including one. So from 0
0 to 0.9999999999
and a bunch of nines after that. So if
we were to visualize this slightly say
that math.random gives you a number from
0.0000 and a bunch of zeros after that.
I just didn't couldn't bother writing
all of them to 0.999999999.
Basically a bunch of nines as well. That
is the range you are given when you give
JavaScript this command. So if we run
this,
you can see we get 0.68 and a bunch of
numbers. 0.95 and a bunch of numbers.
0.88 0.001. Yeah, as you can see, we've
already been almost from very close to
zero to very close to one. Now before we
go, there's one thing I need to point
out, which is that the randomness here,
while it looks completely random for us
humans, it's not truly random. There is
some deterministic variables going on
under the hood here. And it's actually a
really hard problem in computer science
to generate truly random numbers. But
for our use cases, we're not going to
worry about that. As far as weak here,
these numbers that we generate with
math.random are more than random enough
for our use case. This is not a PhD in
cyber security but but rather getting
you started with web development. So
with that let's move on.
Okay. So now we know how to create a
random number between zero and up to but
not including one. However, it's not
entirely clear how we utilize
math.random in order to create something
useful like for example a dice function.
So let's take this one step at a time
towards creating such a function. And
the first step we're going to take is
simply by multiplying
math.random with six. Now I want you to
do the same thing that you did
previously which is to pause the
screencast, run this code a few times
and see what we log to the console and
then answer the following question. In
which range will our random number be
now? So from which number to which other
number? It used to be from 0 to 0.99999.
Now it's something else and you have to
figure that out. So go ahead and do that
right now.
Okay, the answer is that it still starts
at zero. It now goes all the way up to
5.99999
and a bunch of nines. I'm not going to
type all of them. So if math.random gave
us a number from 0 to 0.999, math.random
* 6 will give us a number from 0 because
0 * 6 is still 0 to 5.999
because 0 bunch of 9 * 6 is not entirely
six, but it's almost six. So it's 5 a
bunch of nines. So now we have kind of
expanded our range roughly speaking from
being between 0 and 1 to being between 0
and six. We did that by multiplying it
with six. Let's move on to the next
step.
Okay. So now we need to learn another
method on the math object and that is
math.f floor. floor does is it takes
some input a number and then does
something with that number and returns
it so that you can store that in a new
variable for example like floored number
right here that is the floored version
of this number so I want you to pause
the screencast run this code a few times
and answer the question what does math
floor do to positive numbers and the
reason I'm saying positive numbers is I
don't want you to add negative numbers
in here because the mental model is
slightly easier if you only think about
positive numbers and it's a bit easier
to write a concise answer to it in my
opinion. And finally, I've never used
math floor on negative numbers anyway.
It's usually used on positive numbers.
So try this. Feel free to add a few
different numbers in here and run the
code. And once you're ready, just type
out your personal explanation of what
you think math floor does. Go ahead and
do this right now.
Now the way I think of math floor is
that it simply removes
the decimals because if we run this you
can see it returns three. If we have
12.4
12 even 12.99999
still 12. Okay. So great job. With that
knowledge in mind let's move on to the
next lesson.
Okay. So just as a quick recap,
math.random gave us a number between 0
and almost 1. And math random * 6 then
gave us a number between 0 and almost
six. And now we are going to combine
this with math.f floor which you learned
in the previous lesson. I'm going to add
it here. I'm going to write math.f floor
and then wrap our entire previous
expression inside the parenthesis of
math floor. Now, what I want you to do
is run this code a few times and then
write down possible values that random
number now can hold. So, there's a
limited number of them and your job is
to figure out exactly which ones. So, go
ahead and do this right now.
Okay, hopefully that went well. Now,
let's run this together. You can see we
got four four 0 1 4 0 2 5 H. Okay. So it
seems we are getting the numbers between
0 and five. That actually makes a lot of
sense because if we take this range here
between 0 and almost six and shove it
into the math.f floor function then it
will basically remove all of the
decimals. And if we remove the decimals
from from this entire range, which
numbers are we then left with? That's
just the whole numbers from zero all the
way up to five. So the right answer was
0 1 2 3 4 5 like that. Okay, hopefully
you're still hanging on. Let's move on
to the final step of our dice function.
Okay, now we are going to complete our
dice feature because we know that with
this expression currently get a whole
number from 0 up to 5. So the question
then remains how do we turn this range
into rather a range between 1 and six
which is essentially the same as a dice.
And I want you to try this for yourself.
Try to modify the expression so that we
get a range from 1 to 6 instead of 0 to
5. Go ahead and give that a shot right
now.
Okay, hopefully that went well. To
understand what we need to do, we can
compare each of the numbers here. We can
see that the first number here is one
and the first number there is zero. And
how do we go from 0 to one? We add one.
And the same goes for all of the numbers
here. How do we go from 1 to two? Well,
we add one. And from 5 to 6, we add one.
So basically what we can do is just do
plus one at the very end. Then if we run
the code you see we get two we get six 3
5 6 2 1. Yes, we indeed get the entire
range of a dice. So really good job
reaching this far. Now I only have one
last challenge for you and that is to
create a function called roll dice that
returns a random number between 1 and
six. So basically, you're going to take
what we've done here and wrap it into a
function and then return the result. Go
ahead and do that.
Okay. To do this, I'll do function roll
dice open and close parenthesis open and
close brackets. And inside of here, I'm
just going to copy this line,
paste it in, and then return
random number like that.
Now I have to remove it from up here
because out here on line two random
number isn't even defined. So we can't
log it out. Random number is now only
defined inside of the roll dice function
and the value of it gets returned. So if
we console log and inside of the
parenthesis we call the roll dice
function like that should get yes a
random number between 1 and six. really
good job reaching this far. This was
complicated stuff. So, if you feel a
little bit shaky with this, that's
totally okay. You can go back and
rewatch it and retry all of the
challenges if you want to. And then when
you feel ready, we'll move on and use
this knowledge to make our blackjack
game much better.
Okay, we're back at our blackjack game
and we are going to fix up this get
random card function, but we'll do it
step by step. So to begin with I want
you to make it return a random number
between 1 and 13. So here you can
pretend that we are working with the
regular values that a deck of cards
usually has. So with the ace being the
one, the numbers from 2 to 10 of course
being their respective numbers and then
the jack being 11, queen being 12 and
king being 13. So from 1 to 13 like
this. Go ahead and give this your best
shot and then of course I will show you
how to do it afterwards as well.
Okay, now let's do this. We know that if
we do math random, we are going to
generate a number between 0.0000
and a bunch of decimals after that up
until 0.999 and a bunch of nines after
that as well. So the first thing we need
to do in order to get this range to look
more like this one is to multiply this
entire thing with 13. 0
* 13 is still 0
is still 0 and 0.999 * 13 is actually
just 12.99
or we have a bunch of nines after that
as well. So we're getting to something
remotely similar. It's just a matter of
how many nines we have after the dot.
that this is roughly the range we're
working with with this expression right
here. So, we want to remove the
decimals. And how do we do that? Well,
if you remember from previously, we can
use the math.f floor function and wrap
the entire thing inside of the
parenthesis of math.f floor. So, now we
take this expression here, this one, and
we then floor it, which gives you like
that and like that. So 0 to 12, that's
what we're returning here. Now the
question remains, how do we go from 0 to
12 to 1 to 13? Well, that's only a
matter of adding a 1 to any given
number. We just do like that. And that
should return a number between 1 and 13.
Let's try to start this game and pull a
bunch of cards to see if we actually get
the entire range. 7. Four. Looking good
so far. 10. Okay. 13. Yes. Five. A bunch
of other numbers. Getting way too many
numbers here. Let's get that one. Yes,
there we got it. A one. So, we have the
entire range from 13 to one and just
about everything in between. This was
not a normal blackjack round, but at
least prove the point that this
generates random number between 1 and
13. So, great job reaching this far, but
of course, we have to modify it because
in blackjack, we're not following the
usual value sequence for cards. So, we
need to modify our function a little
bit, but we'll do that in the next cast.
Now, we need to change our get random
card function because in blackjack, the
jack, queen, and king are all worth 10,
and the ace is worth 11. Or strictly
speaking, the ace can be worth both 1
and 11. You get to choose as a player.
What suits you best is the value you
use. However, for the sake of
simplicity, we are just going to treat
the ace as 11 in our case as having a
dual value for this card would add a lot
of complexity to our game and it's not
something I want to focus on. It's
rather something you can for example do
as a stretch goal later if you want to
modify the game at the end of this
course. So, for now, we're going to
treat our ace as 11. So, this means we
need to modify our get random card
function. If the number it returns is 1,
then we should change it to 11. And
likewise, if the number it returns is
either 11, 12, or 13, we should rather
return 10. So go ahead and modify this
function so that these exceptions are
followed.
You're going to have to define a
variable that this random number is
stored in and also have some kind of
conditional that checks for these
conditions and returns the appropriate
value. So go ahead and give this a shot
and then I will of course show you the
solution afterwards as well.
Okay, hopefully that went well. If not,
no worries. We'll do it together. First,
I'm going to instead of returning this
directly, turn it into our random number
like that. Then we need to write our
conditional to account for these special
cases. I'm going to start from the very
top and check if random number is more
than 10 meaning that card is either the
jack, queen or the king. So if random
number is more than 10, what are we
going to return then? Well then we are
going to return 10 because that is the
value of the jack, queen and king. Then
I'm going to do else if and now I'm
going to count for the ace itself and
for that I'm going to use the triple
equals. So I'll do random number triple
equals 1 and I'm going to return 11.
Then in all other cases just do else
like that. In all other cases it must
mean that we have drawn either 2 3 4 5 6
7 8 9 or 10. And if so we're just going
to return that value. So then we're just
going to return the random number
directly like that. Let's run the code.
Hit start game. Okay 55. I'm going to
just hit a bunch of cards to see that we
get the entire range. And indeed, it
seems that we does. We have a two here.
We have an 11 here. And have almost
every single word in between. I'm 100%
sure that if we continue hitting the new
card, in the long run, we'll just get
random numbers across our entire defined
range. So, great job. We're almost
reaching the end of our blackjack game.
You should really be proud of yourselves
for reaching this far. This is starting
to get advanced. There's even more to
learn. So, let's just keep up the pace
and move on.
Okay, there's one really weird thing
with our blackjack game, and that's
easily exposed. If we console log out
the cards here at line 12, and then
refresh the page, there we can see I got
two and seven. And the reason that's
weird is because I haven't even clicked
the start game button. I've kind of just
entered the casino and the casino has
already decided what cards I am to be
dealt. That's fishy. We don't want that.
I want the cards to be generated as I
click on the start game button. So the
first card and the second card should be
generated inside of the start game
function. And thus the cards and the sum
also needs to be reassigned inside of
the start game function. And also the is
alive shouldn't be set to true by
default before I've even clicked the
start game button. I'm not alive. I'm
not even playing the game. I'm going to
use that as an example and set it to
false first. Then inside of the start
game function, I'm going to do is alive
equals true. So now we have the correct
starting state before I've entered the
game up here on line six and the correct
value once I've started the game down on
line 26. So what I want you to do is
make it so that the same thing happens
with cards array because it should not
be set here and the first and second
card shouldn't even be defined up here
and the sum should be zero by default.
So this is how the state of the game is
before I've started playing the game.
Now, what you need to do down here at
line 23 and 24 is generate the two
random numbers, then reassign the cards
and some variables so that the game can
start. So, you might find this a bit
tricky, but do your best. There's no
shame in trying and failing cuz I will
of course also show you how to do it
when you return back to this cast. Good
luck.
Okay, let's do this.
We'll start by defining first card and
second card. And they should be defined
by running the get random card function.
That'll give them the get random card
function. Second card equals card as
well. Now we can set the cards array to
first card
and second card and the sum to first
card plus second card like that. And
also actually
get this console logged back in again
and rerun the browser. You can see now
it's empty. The casino hasn't decided
which cards I am to get. Very good. So,
we hit start game and there I got my
black sack. Oh my god, that's amazing.
And with that, I have nothing more to
say. I'll see you in the next cast.
The next thing I want to fix is our new
card button because it's kind of broken.
Let's see why. If I now start the game
and I get two cards and I draw a third
card and a fourth card and oh, I'm out
of the game. At this point, I shouldn't
be able to draw more cards. But
actually, I am. I can hit it. And I got
another 10. So, I'm at 34. Shouldn't be
possible to get to that number in
blackjack. So, we need to stop it when
the game is over. And also, if we
refresh the browser able to I shouldn't
be able to start with just one card
either, but I actually can. So, there's
two conditions we need to check. both if
I'm still in the game basically if the
is alive is true and also I can't have
blackjack either. Now this requires us
to learn something called logical
operators which lets you combine
multiple conditions inside of an if
statement. So that a piece of code
should only be run if multiple
conditions are satisfied. So let's take
a little detour and learn about logical
operators in the next casts.
Okay, let's learn about logical
operators. Here we have a little bit of
boilerplate code, namely two variables
and a function. And the imagined
scenario here is a course platform much
like Scrimbo. And what we're trying to
solve is to figure out whether or not
user should get a certificate. that is
whether or not we should call this get
certificate function because the deal is
we only want to call this function if
two conditions are true and that is has
completed course in other words the user
has completed 100% of the course and
secondly gives certificate that is
whether or not this is the type of
course makes the user elleible for a
certificate because you can imagine that
very short courses doesn't give you a
certificate the course has to be of a
certain device to award you with a
certificate. So what we need to do is
figure out how we can check if both of
these conditions are true because then
and only then do we want to call this
generate certificate function. You could
actually achieve this with the concepts
you've learned so far by simply nesting
if statements. For example, you could do
if has completed course equals to true
and then add the body of the if
statement and then inside of the body
you could nest another if statement. If
gives certificate equals true and then
so you get two ifs inside of each other
and then inside of the inner if you do
generate certificate.
Now if we run this code, you can see
yes, we are indeed generating the
certificate. And if we flip one of them
to false, we are not generating the
certificate because here this condition
was not fulfilled. So neither of the
code lines inside of the body was run.
JavaScript just jumped over the entire
if statement. However, this is a very
clunky way of doing it. Better way would
be to actually remove the inner if
statement. For now, let's just comment
it out. And then inside of the
parentheses here, we can add another
condition by writing a double amperand.
And then the next condition gives
certificate equals true. We now move
this line, this line, and uncomment this
one. Run this code. We see nothing
happens because this condition here is
false. And with the and operator, both
of the expressions before and after the
operator need to evaluate to true for
this entire expression to evaluate to
true. So if we flip this to true and run
this, now it works, but it's much better
to have it on one line. And a little
extra thing we can do is actually omit
these equals true. Because now
what does this and this evaluate to?
Well, it's simply true and
true and true equals well true. But as
for now, in order to be explicit about
the condition we are checking for, I
actually want to keep these triple
equals here as well.
So one way to read this in plain English
is if
this is true and this is true then run
this code. Hopefully that makes sense
because in the next cast I'm going to
give you a challenge so that you get
your hands dirty with this as well.
Okay, let's continue on with our course
platform example. So in this challenge,
you are to imagine that you are writing
the code that is to be run after the
student has struggled with the challenge
for a little while or maybe the student
has clicked a button and signaled to the
platform that that she is stuck. And we
first want to check has the student
solved the challenge she's trying to
solve and has the students any hints
left? Because if the student has any
hints left, she can go and open those
hints. But if she's used all of our
hints and she hasn't solved the
challenge yet, then we are to run this
show solution function and just show her
the solution. Obviously, it just logs
out a string, but you can imagine that
it would show the solution. So, your job
is to create an if statement that checks
that both variables are false. I mean,
these two variables and then if so, run
the show solution function. Go ahead and
give this a shot and then I will of
course show you the solution when you
return back to this screencast.
Okay, the way to solve this is almost
similar to what we did in the previous
cast. We'll do if and then open and
close parenthesis. And here we need to
use a logical and operator because we're
going to check if both has solved the
challenge. triple equals false and the
double amperand pass hints left is also
false.
Then if the user hasn't solved the
challenge and has no hints left, it's
time to give her some slack and show
this solution. So then we'll trigger
show solution like that. Let's run the
code. And indeed this triggers the code
because both of them are false. If we
try to flip one to true, then the code
is not triggered. We're not logging
anything to the console, meaning that
our logical operator that governs our if
statement actually works. Great job.
So, now that you've learned the AND
operator, let's take a look at its close
relative, the or operator. What does
that do? Well, it allows you to check if
either this condition is true or this
condition is true. And if so, the body
of the if statement will be executed.
So, if you're in a situation where we
only need one of two cases to be valid,
well, then you want to use the or
operator. As for this example, it
doesn't really make sense. It will now
run since actually both of these are
true. But if the give certificate is
false, it will also run. But that
doesn't make any sense because if the
course doesn't give any certificates at
all, you're not supposed to generate any
certificates regardless of the user
having completed the course. So let's
use another example instead. Imagine
here that you're building Netflix and
you've just launched this amazing new
documentary, The Fire Festival. I don't
know if you've seen this, but it's truly
amazing about a guy who creates a
festival that aims to be the celebrity
hotshot festival of the world, but just
becomes a disaster. It's really good.
After this has been launched, you
probably want to recommend it to a ton
of your users. And what you want to do
is you want to check if the user likes
documentaries or if the user likes
startups. They don't have to like both,
but if they like one of these themes,
then most likely they will enjoy the
fire festival documentary. So, I want
you now to try to use the or statement,
that is the two lines you can see here.
You can copy them if you want if you
don't know how to get them from your
keyboard. And you're going to use this
to call recommend movie if either of the
variables that is the likes documentary
or like startups are true. I'm cruing
you out on thin ice here. I haven't
written a complete or statement for you,
but it works in just the same way as the
and statement. So, I think you can do it
if you just give it a shot. And if you
don't, no worries. I will show you the
solution afterwards as well.
Okay, let's do this. We'll do let likes
documentaries.
Set that to true. And we'll do let likes
startups. Set that to false for example.
And if likes documentaries, if that is
true or if likes startups is true, well
then we want to recommend this movie.
Like that. Running the code, we can see
we are recommending the movie even
though this one is false. Flipping both
of them to false. Now if I run the code,
nothing happens because at least one of
them needs to be true. This or this. And
if we make both true
and run the code, then of course if both
are true, then one or the other is also
true. So that would satisfy the
condition. Great job doing this
challenge. Let's keep up the pace and
move on.
Now you are going to take what you've
learned about the logical and operator
and modify this new card function so
that you only allow the player to get a
new card if she is alive and does not
have blackjack. So those are the two
condition that need to be filled for you
to actually run this code. So give this
a shot and I will of course show you the
solution afterwards as well.
Okay, so the way to do this is if open
and close parenthesis and then I'm going
to actually just open the body of the
conditional and paste in this code. And
when are we going to run this code?
Well, she has to be alive. I'm going to
do is alive. And then if that equals to
true, that's the first condition. And
the second condition which we also want
to be true since we're using this
logical and operator should be that the
has blackjack
should equal to false like that. If is
alive is true and has blackjack is false
then the dealer should be willing to
give the player a new card. So let's try
this. We'll start the game. 15 new card
25. You're out of the game. Let's try to
ask the dealer if we can please get one
more card. Oh, nothing happens. It
works. Brilliant. Really good job doing
this challenge.
Okay, so our blackjack game is almost
complete. There's just a tiny little
feature I would like to add as well
because if you've ever played online
blackjack or poker, what you normally
see is your own name and your credits or
your chips somewhere on the screen. So,
what I mean by that is something along
the lines of this,
like that. So you see your name and how
many credits you have or chips or
whatever. And this is nice because then
it's possible to later implement the
concept of winning, of increasing your
chips and of losing as you are betting
or playing rounds are critical
components of any blackjack game.
However, I wouldn't want to hardcode it
in the HTML like this. We of course want
to represent this data in our JavaScript
as that gives us the ability to change
it. Now I'm going to just show you how
we could do this to begin with by first
giving this an ID of player dashl
and removing the contents of it.
And then in the JavaScript we could do
let player name equals pair and let
player chips
equals
145 for example. And then we could grab
the element player L equals
document.getelement
by ID passing in the ID we just created.
Then do the player.ext content which you
know by now
your name colon and a space and then a
dollar sign and then plus the player
tips like that. If we now run this you
can see yes that we render it in the
exact same way. However, now we've come
into a situation where two values that
are inherently connected together like
the player and his chips which currently
live in two different variables. What
would make more sense in this situation
would be to somehow combine this into a
single unity and that is where so-called
objects come into play. That is another
data type which resembles arrays a
little bit but they are also different
in some fundamental ways but they are
kind of also buckets where you store
data. So in our case we would perhaps
want to create a player object in which
we can store both the players name and
the chips. So I'm going to show you how
to do this. You might feel a little bit
confused as you see it but over the next
few cast you're going to practice
objects yourself. So what I'll do, I'll
write let player equals open and close
curly brackets. You've seen these
before. They're used often in
JavaScript, but when you see them in a
context like this after the assignment
operator, they mean that we are creating
an object. And inside of here, what I
essentially want to do is add this data
right here. However, you can see that
something is wrong here with all these
red lines. And that's because the syntax
for creating items inside of our object
is different from how we define
variables. First of all, we don't use
the let keyword in front of the names.
And secondly, we replace the assignment
with colon. And thirdly, just as we have
in arrays, we separate the items with
commas. But we don't do that on the last
item. Finally, here it would make sense
to omit the player as we've already used
that name as the name of the entire
object. So, we can just rather do name
and I'm not going to capitalize it and
chips like that. Now, we have taken our
two variables and group them together in
this little bucket which we are calling
player. And the way we now can refer to
these values pair and 145 is by using
the first the object name player and
then the given key that is the name as
this is called a key and its value. So
name is the key and pair is the value.
Also chips is a key and 145 is a value.
So to replace player chips with
reference to the chips inside of the
player object we'll instead do player.
chips. If I run this code, you can see
we get the exact same result. You're
probably a little bit confused now, so
don't worry if you think this was hard.
Over the next few casts, you are going
to practice objects for yourself so that
this becomes muscle memory and second
nature to you. Let's move on.
One way to think of objects is that they
give you the ability to store data in
depth. And with that in mind, we'll know
that objects are so-called complex data
types or also called composite data
types because we use numbers, strings,
booleans, even arrays in order to
compose objects. So they often store
quite a lot of data and they can even
store objects within objects. That kind
of nested data structure is not
something you're going to think about
now. For now, just think of it as key
value pairs. And let's look this through
the lens of a real world example. That's
always easier. So here on Scribba, we
represent our courses as objects cuz
here you can see the CSS grid course
actually the very first course we
launched back in the days and it has a
bunch of different data. For example, it
has the title, the number of lessons,
and the creator of the course, the
length of the course, and the level of
it. So let's actually translate this
course into JavaScript into how the data
structure actually looks. So, roulette
course equals open and closed curly
brackets. Start with a title like that.
Title colon learn CSS grid for free. Now
we have one key value pair. Title is the
key and the string here is the value.
Then the lessons that is natural to
represent as a number. So it was 16
lessons. the creator which again is a
string that and then we have the length
which while it says 1 hour here that's
actually not how we represent that data
in our database and thus in this object
break it down into minutes. So perhaps
this course is actually 63 minutes. But
as for the user interface, we don't want
to go into detail that much. We round it
to the closest hour. And finally,
there's the level. And here it says
intermediate. That's also one of those
cases where the data in the user
interface differs slightly from the data
in the database. So our levels are
represented from 0 to three where zero
is complete noobs and three are the
advanced courses. So the intermediate
course you can see here, we type out
intermediate here. If this value is set
to two, now you don't have to worry
about how our design and what we say to
the user differs from the underlying
data. It's just something I wanted to
give you a little heads up about because
it's very normal in real world code
bases.
Now, it looks like we're kind of done
here, but we're actually not because
there's a lot more data for a course
actually than what's represented here.
For example, whether or not it's free or
paid. That's a boolean. So, it's either
true or false like that is free. True.
And this course is indeed free. And we
also have tags for courses. And that's
an array because we want to have the
ability to add multiple tags to one
course. So, we need a complex data type
here as well as the value of this tags
key. So, for example, this CSS grid
course has the tags HTML and CSS. So,
this is a very real world example of an
object. Now if we want to access any of
these values here for example the length
we do that through something we call the
dot notation course dot length like that
console log out this and run the code
you can see that we log out 63 so
course.length length gives us back 63.
And you've used this dot notation quite
a lot actually up until now. And even
though you probably haven't been aware
of it, you have been using objects all
along. Like what are we doing here?
Console.log.
What kind of data type do you think the
console is? Well, it's an object. And
just to give you one more example, let's
say we want to log out this array here.
Well, then we simply do course tags on
the code. And when we open the control,
we can see that we are logging out an
array because this entire array is the
value that we get back when we ask for
the tags. Now, one thing I want to point
out before we move on that you can
replace this dot with an open square
bracket and then add a closing square
bracket and then turn the key name into
a string. This is called bracket
notation. If we run the code, we get the
exact same thing back. Now, there are
reasons for why you sometimes want to
use bracket notation, but we're not
going to get into that right now because
in the foreseeable future, you are not
going to end up in a situation where you
have to do that. So, I'm just going to
tell you now to use the dot notation.
It's simpler, it's shorter, less code to
read, less code to write. It's very
understandable, and it's actually what
most developers prefer to use if they
get to choose. So, now I've been talking
for far too long. I want you to write
code, of course. This is Scrimbot.
That's what we're all about. So, in the
next cast, you are going to write your
own object. I'll see you there.
One thing I like to do if I'm bored is
to search for epic places to live on
Airbnb. For example, castles. Like this
wish list I came across right here,
which contains a bunch of more or less
affordable castles to rent across
Europe. So, for example, this really
neat looking castle right here only cost
$190.
I mean, that's what people pay for like
10 square meters in a hotel room in
central London. So, pretty cool that you
can rent a castle for that same price.
But anyway, the point being that these
listings here on Airbnb most likely take
advantage of the object data structure
in order to store all of the data that
they need to present to the user. So,
this can also be a great exercise for
you so that we can build up that object
muscle memory of yours. I want you to
basically create an object that
represents an Airbnb castle listing. It
should contain at least one boolean, one
string, one number, and one array. It
can contain a bunch more if you want to
if you want to go above and beyond.
However, that's the minimum requirement.
And at the end, I want you to log out at
least two of the keys using the dot
notation. So, go ahead and do this right
now.
Okay, hopefully that went well. Let's do
this together. I'll do let h castle
equals open and close brackets. We can
use the title. Live like a king in my
castle like that. As for the number, I
think the price is very natural to use.
Price $190. And what about the boolean?
Which components here naturally lend
themselves well to be using a boolean
data type under the hood? Well, I think
the fact that two of these are
superhosts and the third is not points
to this being a suitable piece of data
for the boolean data type because you're
either a superhost or you're not. So, is
superhost
and this one happened to be true. And
then finally, there's the array. What
kind of data here would it make sense to
place inside of an array? Well, it could
be that these kind of bullet points here
are arrays that are pulled out of the
underlying data and are kind of the
featured selling points that Airbnb
wants to list out or actually you can
see that there's multiple images here.
If you see this carousel indicator so
the images is very natural to have
inside of an array and also the reviews
that's definitely a list of many items.
So, doesn't matter what you choose or if
you choose something completely
different. The point is just that I
wanted you to write an array inside of
an object. So I'm going to choose images
like that. Make that an array. And here
we probably have URLs to the specific
images. So it might be img/castl1.png
img/castl2.png
and so on and so on. I won't bother
writing anymore. Now let's log out to
this using the dot notation console log
castel.pric
console log castel
s superhost.
We'll run this and indeed we print out
190 and true to the console meaning we
have completed the challenge. Great jobs
holding this one. Let's move on.
Okay. Welcome back. Now, it's your time
to render out the player's name and his
or her chips on the page. So, it's a
four-part challenge. First, you are to
create a paragraph with an ID of player
L. Then, you are to move over to the
JavaScript and create the player object
here at the very top, and you are to
give it two keys, name and chips. Then
you are to move down to where you are
grabbing elements from the DOM and then
grab hold of the player L paragraph and
store it in a variable called player L.
Then you are to render the player's name
and chips inside of player L. So pretty
big challenge, but you've seen me do it
before and you've practiced how to work
with objects, so I think you're going to
nail this one. Give it your best shot
and then I will show you how to do it as
well afterwards.
Okay, hopefully that went well. If not,
no worries. Let's do it together. First,
we'll do a paragraph here and we'll give
it the ID of player-l
like that. Nothing happens because it's
empty.
Moving over to the JavaScript, we are to
create the player object. Let player
equals open and close curly brackets are
to define its name.
In my case, pair. Use a comma to
separate the items. And then chips.
Let's say I have $200 in chips right
now.
Three. Grab a hold of the player L
paragraph and store it in a variable
called player L. Okay, we know how to do
that. Let player L equals
document.getelement get element by id
passing in the player-l
which is what we created right here.
At this point, let's console log out
player L.
And indeed, we are fetching it from the
DOM. Finally, we are to render the
player's name and chips inside of player
L. We know how to do that as well.
Player L.ext text content equals and
here we have to concatenate a string and
dig into the object to find the correct
values. So we'll start with the player
doame plus going to have a colon and a
space and then the dollar sign in front
of the chips and then do plus again
player.ips.
Now this is all set up. Let's run the
project. And there we go. We have
rendered it out. Great job solving this
challenge.
Now that you've learned about objects, I
want to tell you about a specific coding
pattern that we've actually been using
quite a lot here, but that we aren't
going to implement from scratch
ourselves in this module. But since we
still are exposed to it, I want you to
understand what's going on under the
hood. And that pattern is to create
functions inside of objects. Something
which we also call methods. Because
let's say that for example we create
another key here which is say hello and
then a colon and then as the value we'll
actually write a function like this. We
won't give it a name. It'll just be an
anonymous function. And inside we'll
console log JSON. That's howdy in our
region. And now if we want to invoke
this function we'll do player do say
hello and then open and close
parenthesis. If we run the code now, you
can see that indeed we are logging
Heisson to the console. And now the
reason I wanted to show this to you is
because don't you think this syntax
looks a little bit familiar? Like first
a variable then a dot and then a
function that you are calling with the
parenthesis at the end because actually
that's the exact same thing we're doing
here when we're saying document dot
getelement by ID and the parenthesis.
And here we're also passing in a value,
but it's essentially the same thing. And
also here down at math dot random also
the same thing. Math is an object and
random is a function on that object also
called a method.
And even our good old friend console.log
is the exact same thing. Object with a
method. So this is not something we are
going to implement ourselves in this
module but I just wanted you to be aware
of that so that when I speak about
methods you know that they are simply
functions that are attached to objects.
So with that let's move on.
Hey Tom here and I'm just dropping by to
talk about AI. AI tools like chat GPT
have taken the world by storm. At the
peak of the hype, AI was going to
replace humans in many skilled jobs. And
there were even lurid clickbaity claims
that humans could disappear altogether
from the face of the earth. So certainly
no point in learning to be a web
developer then. Well, you're still
watching this, so you didn't believe
that any more than I did. But AI is here
to stay and it does have implications
for the industry and for you as a
learner. In this scrim, I want to
investigate what AI means to the coding
student because there are some good uses
of AI for you as a coder, but also some
potential missteps and problems. Take
this fairly typical Scriber challenge.
In fact, it's one you've already seen. I
could solve this myself, but I can't be
bothered. So, I'm going to copy it and
paste it into chat GPT. I'll hit the
magic button. And look, we've got an
answer. And it's given me some
explanation. And explanation is good. if
I read it. But the temptation is strong
just to take the code it's prepared for
me right here and paste it in to the
editor and solve the challenge. And when
I save that and we open up the console,
well, I can see that it is working. But
obviously if I were a new aspiring
developer that couldn't solve this
challenge in the first place in 5
minutes time I would have absolutely no
idea what this code does or how it works
because all I've done is glanced at the
bit of explanation and then copied and
pasted some code from chat GPT. I
haven't really progressed myself at all.
I'm no more knowledgeable and that is
pretty silly. So remember, when learning
to code, you need to understand what a
piece of code does. You need to know its
use cases. So what it's good for and
what its limitations are. And you need
to be building muscle memory. You'll
hear teachers and instructors talking a
lot about muscle memory. That only comes
with the practice of actually writing
code, not looking at it, not watching
somebody else write it on YouTube, and
of course, not watching AI churn it out.
And although theory and explanation are
great, with code you're not really
learning until you're doing. So going
back to our previous example, unless you
read the explanation thoroughly, write
this code out yourself, and perhaps
experiment with it a bit to verify that
what the AI told you is true, you're
actually doing yourself a disservice.
And yes, I said verify what the AI told
you is true. And that brings us to a key
fact and chat GPT is honest about it
right here in its footer. AI is not
always right. Chat GPT can make
mistakes. Check important info. And you
need to know that. Remember how AI is
trained. It learns in most cases from
code which is openly available on the
internet. And there is a lot of bad code
on the internet. If you blindly use AI
generated code without understanding
what it does, sooner or later things
will go horribly wrong. You will not
make it far as a professional developer
if you don't understand the code you're
writing. But if you choose to use AI to
generate code, there are several
takehomes I want you to know about. Make
sure you understand the code that's been
generated. Verify that it's true from
nonAI sources if at all possible. And
whenever you use code that came from AI
or indeed from Stack Overflow or any
other source, write out the code
yourself. This will build muscle memory.
And lastly, keep asking yourself why.
Why does this code work? Following these
rules will help you grow as a developer
much faster. And remember, AI should
enhance your skills and not replace
them. So my last bit of friendly advice
on this topic is that at this stage in
your learning where you are right now,
it's probably best to avoid using AI to
simply generate code for you. But I'm
not saying never use AI. So let's
examine some of the use cases for AI and
highlight some pitfalls to avoid. You
can use AI as a code explainer. If you
don't understand what some code does and
why, you can drop it into your AI
helper. This can certainly be very
useful, but it does come with a caveat.
And this is basically a continuation of
what I was just saying about accuracy.
AI always sounds confident in its reply,
but don't let that fool you. It can be
misleading or just plain wrong. This is
what is known as hallucinating and it
can sound very plausible. We all know
that person, right? Or perhaps a
politician we've seen interviewed.
They've got all of the confidence in the
world, but that doesn't mean that
they're not speaking rubbish. So, while
AI can be great for explaining code,
think of it as a friendly, helpful
colleague who potentially knows more
than you, but is not infallible, not
perfect. I also want to add that while
the more traditional ways of finding
answers are more timeconuming, they are
time well spent. Reading blog posts or
discussions on the problem or asking a
question on Scribb's Discord and forum
might well give you a more rounded
understanding of the problem than AI
does. And if you find a solution that
someone else has implemented for their
own problem, which you then have to
customize to apply to your code, you're
fine-tuning your coding skills even
more. So, while AI is a good explainer,
it should really be considered a last
resort. AI can be useful for debugging.
Again, if you're really stuck and if
you're working professionally and are
against the clock, then this is a great
time-saving tool. But this also comes
with a big caveat. As a new developer,
you should do everything you can to
debug the code yourself without relying
on AI. That means thinking for yourself,
re-watching scrims, searching online for
the meaning behind error codes. You do
have to be able to understand the
problem and the solution to consider it
a success. So don't get into the lazy
habit of relying on AI. Probably one of
the best uses is as a research tool. And
I'm thinking here more on the high
level. So you could ask it some pretty
general questions. For example, what are
the main considerations for making a
website accessible to users with visual
impairments? Or what is the best way to
empty an HTML element? Of course, you'll
still need to use background knowledge
and traditional research to weed out
responses that are inaccurate or wrong,
but it can really speed up the research
process and give you a ton of ideas. AI
can be a great timesaver. I want to
finish up with a word about ethics
because there are some downsides to AI
which don't get enough of a mention.
From an environmental perspective, AI is
a huge consumer of power. AI involves
crunching data and that means servers
are working hard using electricity and
needing to be cooled. We do need to be
cutting our carbon footprints and not
expanding them. Also, the source content
AI is trained on was made by real people
who have mostly not consented and are
not getting a cut of the profits. So,
these are two things to bear in mind,
not least because they will influence
the evolution of AI from here on in, and
you should definitely be keeping an eye
on this field. Okay, let's go back to
the course.
So, you have built your very first
JavaScript game. Congrats with that.
This is perhaps the first of many games
you're going to build. Perhaps the start
of a career as a game developer. I don't
know. Anyway, give yourself a pat on the
back. You have learned a ton. And
actually, let's do a little recap so
that we remind yourself about all the
awesome things you've learned throughout
this section. One thing which was key to
our app and is key to basically every
web app out there is the arrays. We
defined a card array and also you
reassign the array down in the start
game function where you defined it with
items and then all the way down at the
new card button you also pushed to the
array. So you are starting to get some
serious array skills. Also we worked
with objects up here. We created our
player object and the object as you
remember contains key value pairs and
it's also a data type that is used in
basically every single web app out
there.
The third data type you learned was
booleans namely the true and false for
the has blackjack and is alive
variables. We flip them around. So we
set is alive to true down here and has
blackjack set to true down here. And
thus creating kind of a state for our
game. This brings us to the next
important concept you learned which is
the if else statements that wrapped
these booleans and so many other things.
This is key. You cannot build apps
without these kinds of conditional
statements. Both the if, the else if and
the else are absolutely key in any
programming language. And in order to
make these if else statements work, we
used comparison operators. That's these
guys in here. Less than or equal to or
triple equal. They helped us define the
conditions as to when we wanted to
render the blocks of code in the if else
statement. Moving on, we also did
logical operators. The and operator
which checked that both this condition
and this condition was true in order for
us to run this code in the block of the
if statement. And something we also
learned was the for loops.
It was here in the render game function
where we used the for loop to loop
through our cards array and render out
each of the cards. Again, for loop is
just one of those foundational things in
programming because so much of
programming is evolved around looping
through data and doing things based upon
the data. Whether that's rendering it
out, making calculations, sending it off
from a server to a client, or just
summing it together, for example, it
doesn't matter. It's just something
you'll do again and again. So, I'm
really happy that you've learned it as
early as now. Another useful tool you've
learned is the math object that allowed
us to create this get random card
function by using math.random, which
gives us a number between 0 and one, and
math.f floor, which basically floors the
number to the nearest whole number
moving downwards.
And you built upon your further
knowledge of functions and learned how
to use return statements in functions
because in this get random card, we had
to return values. And that's something
you didn't know before this section.
There's still more things to learn about
functions. So, we'll definitely continue
on with functions in the Chrome
extension we're going to build a bit
later, but for now, just be proud about
the fact that you've at least come to
the point where you can return values in
functions. That's a big important step.
So, with all of these things learned, I
completely understand. If you feel that
you're a little bit on shaky ground here
and that all of these names basically
just confuse you even more, I don't
think you should worry about that. I
would have never remembered the
difference between comparison operators
and logical operators when I learned
JavaScript. What they are called doesn't
really matter. The important thing is
that you know how to compare two
variables, for example, or that you know
how to create a true or false variable.
Whether that you call them booleans or
true false isn't something you should be
too worried about right now. So even
though this might look scary, try to not
let it scare you. just focus on coding
as much as you can because over time
these concepts will mature in your mind
and become second nature. And actually
on that note, you are going to get some
practice right away because before we
jump on to the final project, the Chrome
extension, I'm going to throw a few
challenges on you just so that we're
sure that you really understand these
concepts and that you build out that
ever so important muscle memory. So
let's go ahead and do that right now.
Okay. In this challenge, you are going
to practice objects and functions. So,
first you are going to create a person
object that contains three keys, name,
age, and country. And the values of
those keys should be set by using
yourself as an example. So, your name,
your age, and your country. And then you
are to create a function called log data
that uses this person object. It just
grabs it from the global scope to then
create a string in the following format.
pair is 35 years old and lives in
Norway. Obviously, pair 35 and Norway
are pulled from the person object
directly, whereas the rest is a string
that is hard-coded inside of the
function. Finally, call log data in
order to verify that it works. So, go
ahead and do this right now and then I
will show you the solution as well when
you return back.
Okay, let's do this. We'll do let person
equals open and close curly brackets
name should be pair
age 35 and country
Norway like that and then the function
log data like that here we will
console.log
this string and you can see the first
value is actually pulled directly from
person.name name. So we'll do personname
plus and then s and then plus person do
age then plus again
years old and lives in plus
person dot
country like that. Now the final step is
to call the function log data
and run the code. And yes, we can see
Pier is 35 years old and lives in
Norway. Great job.
Okay. Now you are going to practice
conditional statements. So that is the
if, else if and else statements. And the
problem you are going to solve is a very
common one for train companies or a bus
service, which is that the price of the
ticket is based upon the passenger's
age. For example, if the passenger is
less than 6 years old, there's a free
pass. 6 to 17 gives a child discount. 18
to 26 gives a student discount. 27 and
up to 66, that's the age range that pays
the full price. And the people over 66
get get the senior citizen discount. So
your job is then to create a conditional
statement that accounts for all of these
scenarios and based upon the age
variable up here logs out which discount
the passenger will get. So go ahead and
give this a shot and return back to me
when you want to see my solution.
Okay, let's do this. I'll start with the
if and if h is less than six well then
console log
three else if next condition that is
that age is less than 18 because that
will in practice mean from 6 to 17 here
because it's less than 18. Yeah, that's
17 and below. However, we've already
accounted for up until five. So the code
here will be triggered from 6 to 17.
console.log
child discount.
Okay. Next one. Else if here we'll do
age is less than 27.
So log student discount because this one
kind of starts at 18 since this one
stopped right before 18 and it should be
up until including 26. that is in other
words less than 27.
Then we have the full price which is for
people now left in the group who are
less than 67 or in other words 266.
Control log full price. And finally then
we have the else. We don't need to
specify an if because in all other cases
the passenger should get the senior
citizen discount.
like that. Let's run the code. At 15,
yes, we're getting the child discount.
Correct. At 55, we're getting the full
price. Very good. 75, we are getting the
senior citizen discount. So, it seems to
be working for at least these three
scenarios, and I hope it works for the
rest. So, let's move on.
Okay, it's time to practice your for
loop skills and your geography skills
because I've created this large
countries array here which contains the
five largest countries in the world by
population size. So it's China, India,
USA, Indonesia and Pakistan. And what
you are to do is to use a for loop to
log out the following to the world.
First a sentence, the five largest
countries in the world, and then each of
the countries on a new line with a dash
and a space in front of the country
name. So you are to use a for loop and
loop through this array here. You can't
simply hardcode out all of the console
logs. That wouldn't serve any purpose.
So try to remember how to use a for
loop. If you can't remember it, you can
look back at previous casts as we surely
have gone through it. Or of course, I
will also show you the solution
afterwards as well.
Okay, let's do this. I'll start with the
for loop because that's the meat of this
challenge for let I equals zero. We'll
start at zero because as you remember
raise our zero index and we want to use
the index in order to fetch out each of
the items. So starting there and
stopping before I reaches large
countries dolength
length of this is five. So before it
reaches five that is four and indeed
Pakistan is the fourth index. So that
should be right and we want to increment
upwards like that. Here we'll do console
log and then first the dash and the
space like that concatenating this with
large countries passing in in the square
brackets the iterator variable I and now
let's run this code and bring up the
console and indeed it looks to be
working. Now we only need to control log
out
the five largest countries in the world
like this. running the code and yes, it
works. Great job.
Okay, now I need your help because I
have messed up our large countries
array. As you can see, in first place I
have Tuvalu and in fifth place I have
Monaco. However, neither of those
countries are large enough to deserve a
spot in this large country's array.
Actually, Tuvalu is a tiny island state
far out in the Pacific Ocean with just
over 10,000 inhabitants. And Monaco is a
tiny state in Europe that's smaller than
Central Park in New York. So surely it
does not belong in this array. The first
and fifth place belongs to China and
Pakistan. So I need you to help me fix
up this large countries array so that
China and Pakistan are added back into
their respective places. And to solve
this, you are going to use the four
following tools. First, push and pop,
which you've learned earlier in this
course, and then their counterparts,
unshift and shift, which I haven't
taught you about, but this is
intentional because I want you to Google
how to use unshift and shift. And what I
will disclose to you is that they are
kind of a mirror image of push and pop.
Because whereas push and pop will work
with the end of the array, unshift and
shift works with the beginning of the
array. So pop will remove an item at the
end like this. Whereas push will push an
item into the end like that. And
likewise shift will remove an item from
the beginning of the array like this.
and unshift will shove an item into the
array at the start like that. So with
that short explanation and Google, you
should be able to I want you to try to
solve this challenge and of course I
will show you the solution when you
return back to this screencast. Good
luck.
Okay, hopefully that went well. Let's do
this. I'll start at the end. So I want
to replace Monaco with Pakistan. Then
I'll do large countries pop in order to
pop away Monaco. And then large
countries push
passing in Pakistan.
Let's now console log
large countries to see if this works.
Running the code. And yes, there we go.
Now to remove this first item here, I'm
going to have to use shift. And here I
completely understand if you're confused
as to why shift is named shift and
unshift is named unshift. It's about the
two most unintuitive method names in
JavaScript in my opinion. And one way to
think of this is that unshift is a
longer word than shift. So unshift makes
the array longer. That's just as push is
a longer word than pop. So push makes
the array longer. Anyway, let's just do
this. Large countries dot shift. Let's
see if we've removed to value by running
the code now. Yes, to value is gone.
That means I need to do large countries
do unshift and pass in China like that.
Running the code. Yes, we have fixed up
the large countries array. Great job.
Now, it's time for a scary challenge
because you are going to check if it is
Friday the 13th. And if so, you are
going to log out this spooky face
because, as you might know, Friday the
13th is kind of a scary date. If you've
seen the Friday the 13th horror movies.
So, you have two variables. day of
month, which is meant to be from either
1 through 31, depending on which day of
the month it is, and then a weekday
variable, which is meant to be Friday or
Tuesday or Wednesday or any other days
in the week. And if it is both Friday
and the 13th, and only if both of those
conditions are satisfied, you are to log
out the spooky face. So just to remind
you, you have to use the logical and
operator which we learned is symbolized
with these two amperands. So go ahead
and give this a shot right now.
Okay, let's do this. We'll do if open
and close parenthesis and here we'll do
day of month triple equals 13. If it is
the 13th day and if it is the week day
of triple equals Friday
then we are going to log out the spooky
emoji like that. Running the code
nothing happens because it's Friday the
31st.
Flipping this around. Friday the 13th.
Yes, we are logging out the spooky face.
And if we change this to Tuesday,
nothing logs out because this time the
first condition was satisfied. It
evaluated to true. But the second one
was not satisfied. So it evaluated to
false and the and operator requires that
both the first and the second evaluates
to true in order to execute the code in
the body of the if statement. So great
job.
Okay. Now you are going to practice
returning random numbers. And the
problem you are to solve is the rock,
paper, scissor game. If you try that,
it's a game where two people choose with
their hands either a rock, a paper, or a
scissor. And the way it works is that
the rock will beat the scissor, but the
paper will wrap itself around the rock
so that the rock loses. And if the match
is between the scissor and the paper,
the scissor will cut the paper into
pieces. So if those rules were a bit
confusing, don't worry about it. That's
not the point here. Your only job is
actually to create a function that
returns one of the random items in this
array when it's called. So you need to
work with math.random in order to
achieve this. Go back and watch previous
casts if you've forgotten about that or
search for it on Google. That's even
better. And finally, when you have coded
up your solution and want to see mine,
just revert back to this screencast and
I will of course show you how to do it
as well.
Okay, let's do this.
We'll call the function get hand
and inside of it first need to generate
a random index that we're going to use
in order to fetch out rock, paper, or
scissor. So we'll do let random index
like that equals first I'll just do
math.random like that and then actually
I'm going to return random index and
then debug along the way just to be sure
that I understand each step I'm taking.
So here I'll console log the result of
running the get hand function like that.
So now if we run this we're logging out
0.05
or 0.1 or 0.07 07 or 0.97. So a value
between 0 and one not including one. So
0.9999 is the top value. Okay. First
then what we need to do is multiply this
with three. Now if we run it, you see
that I'm getting a value between 0 and
2.9999.
0 to 2.999.
That's my current range. Now the value I
want is either a zero, one or a two. So
I'm not too far from this. And the way I
can get that is by stripping away the
decimals by flooring it. In other words,
math dot floor
like that. Here you can see two 0 2 one.
Yes. Now we are generating random
numbers between zero and two. How can we
use that to get back a random hand
either rock paper or scissor? Well, that
is simply by doing hands and then using
the random index as the index. Now,
let's run this. We're getting scissor,
we're getting paper, and we're getting
rock. Brilliant. Great job.
If you, like me, grew up in the '9s, you
might remember this game, Street Fighter
2 on Super Nintendo. It was really cool.
So now I want to create a copy of this
game called Emoji Fighter. Or actually,
we won't do any fighting here. We'll
just set the stage for two emojis to
challenge each other. So the way it
works is that when the user clicks on
the pick fighters button, I want two
random emojis to appear side by side
with a VS between them for versus. As
you can see, I've coded up the skeleton
for you here by creating this fighters
array, which contains all of our
different fighters and also fetching
references to the stage and to the
button. And the stage is where I want
you to render out the emojis. And the
button is the one right here. As you can
see in the index.js, I've hooked it up
with an event listener so that we listen
for clicks, which we can verify by
inside of this callback function trigger
out click. So that now if we run the
code and click on the button you can see
it is logged to the console. So your job
then is to inside of this function write
the code needed in order to pick two
random emojis from this array and then
display them in the stage l. Now, as a
little hint, if you don't know how to
pick a random item from this array, do
remember that you know how to generate a
random number in JavaScript and then you
can use that random number as an index
which you again use to fetch out one of
these emojis as you hopefully know how
to get a hold of items within arrays
using the items index. So, just do that
twice and then render it out. And if you
struggle, don't worry. As always, I will
show you the solution when you return
back to me. Good luck.
Okay, let's do this. So now what I want
to do first is generate a number that is
between zero and whatever length this
fighters array is. So let's just get
started and do some trial and error. You
let random
index equals math random like that. If
we now console log that out and see that
when we click on the pick fighters we
get random number between zero and one.
So this range is too narrow at the
moment. We need something between zero
and the final index here. So what I'll
do is I'll multiply math random with
fighters dolength like that. Now if we
run the code and click pick fighters you
can see we get 12 point something 2
point something 4 point something 3
point something. Yeah, it actually looks
pretty promising. However, we don't want
these decimals. So, what I'll actually
do, I'll floor this entire expression
down to the closest whole number by
doing math. Floor
and wrapping this entire expression
within the parenthesis of the floor
method. If we now run this again and
choose P fighters, we get 8 9 3 5. Yes,
this is actually really promising. So
now if I instead of logging out just the
random index actually log out fighters
square bracket and then random index.
What do you think will happen then? As
you can see we are logging out a random
emoji. So now what we need to do is
duplicate this line here because we want
two random emojis. We'll do random index
one and random index two like that. And
then let's actually console log out the
first mode plus vs
space before and after and then plus
fighters square brackets random index 2.
We run the code and pick fighters. Then
we get an error. I've done a mistake. It
says random index is not defined. That
is because I forgot to change this
variable into random index one when I
renamed this one because I wanted two of
them. So, a little live debugging
session right there. Let's run it again.
Pig fighters. Yes, now it works.
Dinosaur against a seal. We get to the
console. Meaning that the final thing we
need to do is just replace the console
log with stageel lext
content and then set that equal to the
same expression that we just logged to
the console. If we run the code, click
pick fighters. Yes, there we go. Robot
against crab. Click again, we get a
spider against a dragon. And finally, a
robot against a dragon. So, great job
solving this challenge.
Now, I have a fun little challenge for
you. Because you can see this fruits
array here, which contains emoji fruits.
Basically, three apples and two oranges
mixed together. You are going to take
these fruits and sort them onto the
apple shelf or the orange shelf. The
apple shelf is the one at the top here,
and the orange shelf is the one at the
bottom. They are just simply divs, but
I've given a little bit of styling. So
you are to create a function that when
called puts the apples onto the apple
shelf and the oranges onto the orange
shelf. And you should use a for loop, a
conditional statement, and the text
content property to achieve this. Now,
one thing you might be wondering is how
do you work with emojis in JavaScript?
And actually, you can treat these as
normal strings. It's just as if it said
orange here instead of having the emoji.
So with that, I wish you good luck and
I'll see you later when I'll show you my
solution.
Okay, hopefully that went well. Let's do
this function. I'm going to call it sort
fruit like that. And then inside of the
function, we first need to loop through
all of the fruits for let i equals zero.
I equals less than fruit.
length and I should be incremented
upwards.
Now, inside of this, we need to check
every step in the iteration. Are we
dealing with an apple or are we dealing
with an orange? So, I'll do if fruit i.
That's the current fruit triple equals.
And then I can just copy this apple
here. If it's an apple, well, then we
are going to do apple shelf.ext
text content.
And here we're not going to do just
equal the apple. We're going to plus
equal because we want to gather all of
our apples here and not wipe them out
every time we run this code. Then I'm
going to do an else if root i triple
equals this time an orange. Well, then
we are going to head over to the orange
shelf and do text content
plus equals the orange. Now, it would
have worked perfectly fine if we just
omitted this as well like that because
in our array, if it isn't an apple, we
know that it's an orange, but we might
be getting other arrays which have
bananas in them as well. And if so, we
don't want to put any bananas in the
orange shelf. We want to check
explicitly that we are dealing with an
orange before we put the fruit up on the
shelf. So finally, we just need to sort
fruit.
We run the code. Let's do that. And
there we go. We got three apples and two
oranges neatly sorted on both of the
shelves. So great job.
Okay, now it's time for you to do a new
solo project. And this time you're going
to create a random password generator
because in today's day and age it is
critically important that you have
secure passwords for all of the services
you use. If not, it'll be easy for
people to hack your accounts. So let's
have a look at the project you're going
to build. It has only one piece of
interactivity and that is that user can
click this generate passwords button and
then two different passwords will be
generated in the fields below here. And
you are going to build this from
scratch. And I'm putting that in quotes
because I've given you the characters
array that you can use in order to
generate these passwords here at the top
of the index.js file because it's a
hassle for you to find this online. So I
wanted to give you this so that you can
hit the ground running. And as I said,
you are to generate two random passwords
when the user clicks the button. And
each password should be 15 characters
long. Now, if that is too easy for you,
I also have some stretch goals, but you
should only try these stretch goals if
the rest of this challenge is a breeze
for you. So, if you struggle, don't do
these stretch goals. The first one would
be the ability to set a password length.
That would probably have to be some kind
of input field where the user can say
how many characters they want to have in
their passwords. And also a copy on
click feature would be really nice so
that when the user clicks on any of
these passwords they will be
automatically copied to their
clipboards. Also you could give a couple
of toggle options here where the user
gets to modify whether or not they want
symbols and or numbers in their
passwords or not. So here you'd probably
have to split out the numbers which you
can see here and the symbols which you
can see here and then use them or not
depending on what the user wants. As
always you can find the design in a
Figma file. Just click on this image
right here and you'll be taken to that
file and there you will actually see
that we provide a dark mode version and
a light mode version. So feel free to
choose the one you like best. Now you
might see other students being inside
this exact Figma file. So what you want
to do is click on this downward arrow
here and choose duplicate as that'll
give you your very own Figma file to
work with without getting disturbed by
cursors from other students. Now if
Figma confuses you a bit, don't worry.
Just click this link right here and
you'll be taken to a short Figma
tutorial. And if you get stuck, click on
the help section link right here and
you'll get some hints. Okay, I'll leave
you with the requirements here on the
lefth hand side and the design here on
the right hand side which you of course
can click at if you want to get to Figma
and also a link to the help section
below. So that should be everything you
need in order to get started. I wish you
the best of luck and happy coding.
Hey, and welcome to this section where
we are going to build a Chrome
extension, which is just so freaking
cool because Chrome extensions, if you
haven't seen them, are programs that can
enhance your browser experience in
Chrome. This is an example called Honey.
It's really brilliant because what it
does is gather coupons from all over the
web and when you visit a page where you
want to buy something, it gives you
whatever coupons it can find online so
that you can save money. And it was
acquired by PayPal for $4 billion. So
building Chrome extensions is huge
because I mean everyone uses browsers
and and surely what comes out of the box
when you use Chrome surely hasn't
thought of every single problem people
might encounter when they are using
browsers and browser extensions is our
way as developers to build solutions
around these problems. Here's another
one. Grammarly I use it all the time
here. So when I write for so for so for
example when I write tweets it sits on
top of Chrome and checks my spelling
because it has access to what I do on
the page and then suggests how I should
improve my spelling and my writing in
general. And this one right here is a
lifesaver. It's motion and it saves me
hours every single day by stopping me
right before I try to enter for example
Facebook. And it does this with a ton of
other distracting sites as well so that
I can focus on what matters like for
example creating courses on Scrimbo or
writing code or doing productive stuff.
So so cool. If you want to check out
more extensions, you can head over to
chrome.google.com/webstore.
There you can search through all of
them. And also if you want to open your
extensions in Chrome, just click this
puzzle icon in the top right corner and
you'll see all of your extensions. And
here you can see leads tracker. What's
that? Well, that is the one we are going
to build in this section. It is based on
an insight I had when I worked as a
developer in a company called Senetta,
which is a provider of price information
for people who are shipping containers.
Because what I saw there was that the
sales team at that company spent
countless hours tracking down leads or
sourcing leads as they also call it. And
that is looking for people online who
might be interested in your product. So
what they do is like browse through a
bunch of pages and look at people and
like no this guy is probably not
interested in our product. This one
neither. Ah yes, here we have a
potential customer. Let's save this one
to our system. Moving on. This person
does not look like a potential customer
but this one does. Yeah. And this one
does as well. But this one does not look
like a potential customer. So what they
then had to do is take these three
potential customers and store them
somewhere so that they could reach out
to them in the next phase. And this is
something that and this is something
that basically every single sales
representative does because I mean you
need people to sell to. So you need to
find them somewhere. So our leads
tracker will be a tool for these kinds
of people. The way it works is that you
visit a website, for example, a profile
on LinkedIn and then you open up the
extensions, click on the leads tracker
and there you can see you get the
ability to save input. That is any URL
that you pass into this input field or
hit save tab. If you hit save tab, our
extension heads over to this tab, grabs
the URL and saves it in a list. Then you
can move on to another page, for
example, my LinkedIn page, and then hit
save tab again. And boom, there we have
the URL to my profile. And then moving
on, this is Synindra, our CTO in
Scribba. Yet again, we click save tab
and his profile URL is saved. And this
data is persisted. So you can close down
the app or Chrome, turn off your
computer, and when you come back, it's
still there. So that you as a sales
representative can then grab these leads
and do something else with it. So this
is actually a pretty useful tool for
sales representatives. And it's actually
deployed in the browser so that you can
use it in the real world. And the point
with this is to give you the powers to
do this to actually create Chrome
extension that has value and also give
you the skills you need to deploy it to
someone's browser. So once you've
learned this, I would really recommend
you to look for opportunities. Look for
problems you see out in the world. Maybe
there's a problem you have or or someone
you study with or someone you work with
or just a problem someone tells you
about. when you come across problems
people have, I want you to think, can I
build a Chrome extension to help solve
this problem? Because if you can do
that, you truly have a superpower and
that'll manifest itself in opportunities
that you will get throughout your career
if you're just willing to use that
superpower, which JavaScript and web
development is. So, this was a long
introduction. I hope you are as excited
as me. Let's just move on and build your
very first Chrome extension.
Okay, in this challenge, you are going
to create the basic markup we need in
order to start building our app. So, you
are to create an input element which is
of type text and has the ID of input-l.
And then you're going to create a save
input button. That is the one the user
will click in order to save the input.
And it should have the ID of input-bn.
And here save input should be the text
inside the button. So go ahead and do
this right now.
Okay, let's do this. I'll start with the
input and give it a type of text and ID
of input L. And the input element is a
so-called void or empty tag, meaning it
only has an opening tag. So you're not
supposed to create hosting tag because
you're not meant to write anything in
between input tags. It is the user that
is meant to add the input. So this is
how you create an input tag which has
mentioned often called void or empty
tags or you'll also hear them referred
to as self-closing tags. Anyway, let's
move on to the button. New button that
is not a void tag. It should have a
closing tag and an opening tag. And in
between we are going to write save
input. And the ID is set here as
input.btn.
If we now run this code, you see yes
indeed we have an input field and a
button. Great job.
Okay. Now you are going to style our app
according to this design.
So, in the CSS file, which is where
you're going to work, you'll see that
I've already styled the body tag a
little bit. Specifically, I've cleared
out the margin. Since browsers often add
a default margin to the body tag, I had
to set that to zero to neutralize that
because I don't want any margin on the
body. However, I've set the padding to
10 because I do want a little bit of
padding. You can see here on the body
element that there's some space between
the edges and where our elements start.
That is the padding that is provided in
here. I've also set a font of Ariel and
that is a web safe font. So that one
should work without for example using
Google fonts in our project as well. So
just click on the slide here when you
want to see the design and click outside
of the slide when you want to go back to
the code. And I've also added a little
helper text here which is the
hexadesimal code of the green color
which you can see on the button and on
the borders of the input field. Now you
can see on the bottom image here that
I've added some text to the input field.
The slightly bluish color in the
background here is not something you are
to configure. That's just default
styling that is added by the browser. So
you don't have to think about that. As
far as you know the background of the
input element will be white. Now, I'm
going to get you started here with
making the input element full width
because that's a little bit unintuitive
and I want to show it to you because as
you can see here on the input element
when we enter text into it, we have a
little gap between the text and the edge
of it. However, in our app now, if I
like that you will see that there is
basically no gap. So, we have to add a
little bit of padding on the left side.
Let's say 10 pixels. And there you can
see we got the gap. We want the same gap
on the right hand side as well. So I'll
do padding right of 10 pixels as well.
So now we have that. And you might
wonder why I started there. It's a
little bit weird. But let's say that we
now want to provide the styling for
making this input field be full width.
Well then I'll do width and then set it
to 100%. That means take up all the
available space I have inside of my
container. And for the input field, the
kind of container it lives inside of is
the body tag.
So that means that 100% should make the
input field take up the entire body. And
the body has a little bit of padding as
you know. So I would expect this to
actually stop at the right 10 pixels
before the end of our browser. But
actually it doesn't do that. It
continues on and actually goes past our
browser which is a little bit weird. The
reason is that the true width of this
element now is
100%
plus 10 pixels plus 10 pixels because it
includes the padding left and the
padding right and adds that on top of
this width. So that's a problem and it
results in our input field being wider
than our body tag and we don't want
that. So the trick I'll use to avoid
that is something called box sizing. And
here I say I want you to size the box
along its borders. I'll write border
box. And there you can see now full
width includes the paddings as well. The
100% now takes into account the paddings
and just make sure that the entire thing
spans across the entire body tag but not
overflowing it.
I wanted to show you that because it's
not entirely intuitive. Now you have
what you need. Your job is to use only
these two selectors, the input and the
button selector, and make it look like
the design you can see here on the
slide. So go ahead and give that a shot
right now.
Okay, hopefully that went well. Now
let's do this together. I'm going to
continue with our input element because
here we'll see that it's very crammed
above and below the text we have written
in the input field. So what I can do is
padding bottom also of 10 pixels and
padding top
also of 10 pixels. Now we have something
that looks much more like what we have
here. Next up, we also need the color on
the border. So, we'll do border
one pixel
solid and then use this green color here
like that. Think look good. Now, we can
actually continue with the button. The
first thing is the background color
which is green. We'll do background
and use this green color again like
that. Okay. Also the text is white color
white like that. You see there's also
padding here as well which we need to
add. There's a little bit more padding
on the left and right hand side. Let's
try padding top of 10 pixels and padding
bottom of 10 pixels and then padding
left of 20 pixels. Yeah, starting to
look good. Padding right of 20 pixels.
And now I actually want to clean up our
code a little bit because you can see
we're using this very verbose way of
writing paddings both here and here.
Whereas in the body as you have learned
previously in this career path you can
use the short hand. And if all of the
top bottom left right are of the same
value we can just use padding. So I can
remove all of these and do padding 10
pixels. Here it's a little bit more
complex but the top and bottom are
identical and the left and right are
identical. What we then can use is
adding and then 10 pixels for the top
and bottom and then 20 pixels for the
left and right. So the first value kind
of becomes the y-axis above and below
and the second value becomes the x-axis
to the left and to the right.
If I remove this, you now see that it
looks identical as how we had it
previously, but we've removed a ton of
lines, which is good. Now, uh, another
thing on the button is that it has these
default borders. We don't want that
according to our design. So, we'll do
border none like that.
Now, it's really starting to look
similar, but there's two things. We want
to bold the text and we need a little
gap between the input field and the
button. So, font weight
bold. Yes.
And the gap between the button and the
input can be added to either of the
elements. I'm going to add it to the
input field. Do use a margin
button and set it to for example four
pixels. There we go. Now we have
something that looks pretty much like
what we have and it's also responsive.
So we are ready to move on.
Okay. Now we are going to move from the
CSS over to the JavaScript because we
have to make our input button work. And
while you won't make it useful in this
cast, you will at least have it log out
button clicked when the user clicks it.
So you know how to do this, you're going
to have to modify both some HTML and
write a function in the JavaScript file.
So go ahead and do it so that when I
click the save input button, the console
will log out button clicked. And once
you are done, return back to the
screencast and I will show you the
solution as well. Good luck.
Okay, hopefully you remembered that the
way to do this is to create an on click
handler on the button. There you want to
type the name of the function you want
to trigger. Let's call this function
save lead for example open and close
parenthesis. Then we'll head over to the
index.js file and create that function
function save lead like that and inside
of it we'll do console log button
clicked like that. We'll run the code
click the button and indeed we are
logging out button clicked. Great job.
So, while this way of writing event
listeners where we define a function and
trigger it via the onclick attribute in
the HTML works perfectly fine out in the
wild, you'll most likely see
professional developers use a slightly
different technique. So, it's important
that you learn that one as well. And the
way we do that is by first grabbing a
hold of the ID of the element we want to
hook an event listener onto. So I'll do
let input btn equals document dot
getelement
by id pass asing in the input batch btn
and then what I'll do is input btn dot
addvent
listener like that and inside of the add
event listener function or method which
it's also called I'll first specify what
kind of event I want to listen for and
here it is
click events and then I'll write a comma
and do something which might look a
little bit weird because now we are
going to define what are we going to do
when someone clicks on this input
button. Well, we are going to invoke a
function and in order to invoke that
function, we first have to write it. So,
I'll do function open and close
parenthesis and then the curly brackets
and break it onto a new line. And inside
of this function, we can write whatever
code we want to be executed when the
user clicks on this input button. So
here we'll do console.log
button clicked from add event listener
like that. And let's change this to
button click from on click attribute.
Now if we run this code, hit the button,
you can see both this line and this line
is executed. However, now we can
actually remove this on click event
listener from our HTML. And some would
say that this results in a cleaner
separation of concerns where the HTML
doesn't worry about any event listeners
as that's all gathered in the
JavaScript. So of course then we should
also remove the save lead function and
we're left with this. Running the code,
clicking the button and only this line
triggers.
So in the next cast you are going to get
some practice writing this type of event
listeners. So let's dive into it.
So in this challenge you are going to
practice writing event listeners using
the technique you just learned. So it's
a three-part challenge where you first
are to grab the box from the DOM and
store it in a variable. Here you can see
the box. It's just a rectangle that says
open the box. You can imagine that this
is some kind of Easter egg on a
e-commerce website where if you open the
box, you get a coupon or something like
that. And as for HTML, it's just a very
simple div with an ID of box. And of
course, I've styled it a little bit in
the CSS file, but we're not going to
worry about that. So, when you have
grabbed the box from the DOM and stored
it in a variable, you are to add a click
event listener to it. And then finally,
log out. I want to open the box when the
box is clicked. So, if you struggle to
remember how to add an event listener
using JavaScript, you can go back and
rewatch how I explained that or even
better, Google it out and you should
find the solution as well. With that, go
ahead and solve this challenge right
now.
Okay, hopefully that went well. Let's do
this. So we'll do let box equals
document dot getelement
I id passing in ox and then to add the
event listener it's box dot addvent
listener passing in first the event we
want to listen for that is the click and
then what do we want to trigger when the
user clicks well it's this function
right here and inside of it we're going
to console log I want to open the box.
Like that. Let's run the code. Click the
box. And yes, it works. Great job.
Okay, we are back at our lead tracking
app. Now, of course, you are going to do
what I showed you before you learned
about ade event listener, which is to
refactor our app so that it uses the
addeventlister when you click on the
save input button. Cuz this is Grimba.
I'm not supposed to write the code. So
even though I've already done it, now
it's your turn. So go ahead and do this
right now.
Okay, hopefully that went well. Now I
will quickly do it as well. We need to
grab a hold of the button. I'm going to
grab the ID here and do let input btn
equals document.getelement get element
by id passing in the ID. Now that we
have that, we can do input btn dot add
event listener. We're going to listen
for clicks. And the code we want to run
when users click the button is inside of
the body of this function right here.
So I'm going to copy this console log,
paste it in there
that now I can remove the save lead
function and I can also remove the
onclick attribute in the HTML like that.
I'll rerun the code. Click save input
and indeed it works. Great job. Let's
move on.
Now we need to set up the infrastructure
for being able to actually save the
leads that we enter into our input
field. And for that we need two
variables. First a my leads variable
which should be assigned to an empty
array. Secondly the input L variable
which should be assigned to the text
input field. With that I mean the input
field right here. So go ahead and create
these two variables right now.
Okay, the way to do this is let my leads
equals an empty array like that. And for
the input L, you probably did let input
L equals document
element by ID passing in the input L.
However, what I'm going to do is to swap
out this let with const. Hm, that's
weird. Why am I doing that? You might
wonder. And that is because in modern
JavaScript developers use both let and
const to declare variables. And now I
think it's about time that you learn
both of these methods. So we're going to
set the input l as a const and also the
input btn. Now the main difference
between const and let is that const
cannot be reassigned
and the name kind of hints to that. It
says const which is short for constant.
So whatever you assign it to should be
constant meaning you can't reassign it.
And if you don't remember what
reassignment is, it's when you do input
I'll for example equals
hello.
This won't work. If I run this code, you
can actually see assignment to constant
variable. I tried to assign the input
lel. However, it's a constant. So,
JavaScript is saying uh uh not allowed.
Now, you might ask, why do we need this?
This seems to be something that just
makes it more difficult for us to code
because it gives us less freedom. Well,
that is a very good question. So in the
next cast, we'll dig into exactly that
so that you properly understand why you
want to use both let and const and
actually get some practice using both of
these variable types.
Okay, let's say that we are back at our
blackjack game and we have two variables
player name and credits. The first one
is a const whereas the second one is a
let. Now immediately upon seeing these
two variables, a developer would
understand that player name is not to be
reassigned. However, this developer
wouldn't only think that credits can be
reassigned, it would actually assume
that credits are to be reassigned
further down in the code. So the
developer would expect something like
credits equals dreads minus 10, for
example. And the reason for that is that
developers often use the following to
decide whether or not they should use
const or let. So if possible, default to
using const. If not, use let. If these
two lines follow this methodology, it
would mean that it simply wasn't
possible to write credits as a const
because it is going to be reassigned
later down in the code. And thus we
skipped the default of const and used
let instead. And this is actually the
biggest benefit with const. It gives us
a way to quickly signalize to anyone who
are reading the code what we want to do
with the variables. So in addition to
expecting that something like this here
would happen, the developer would also
be very suspicious if she saw something
like player name equals something else
down here because that would mean that
the person writing this code mixed up
their let and const
error. So with all of this knowledge and
following this methodology, I want you
to do a challenge. Which of the
variables below should be changed from
let to const? Here the situation is an
e-commerce shop where you have a base
price, a discount, a shipping cost, and
shipping time. At some point though, the
details of the shipping changes and then
you calculate the full price and finally
notify the customer. So go ahead and
change some of these variables to const
and then I will show you the solution
afterwards as well.
Okay, let's do this. I'm going to start
down here on where things are
reassigned. It's easy to see that these
are reassignments since we're starting a
new line with just the variable name,
not let in front of it. So here we are
reassigning shipping cost and shipping
time. Meaning these two can be let and
this is a new variable. So actually
here's the end of our code and base
price and discount they haven't changed.
So they could be const and it says here
if possible use const. So default to
const. So then I'll do const and const
like that. And the code works perfectly
fine. I haven't broken anything. Is
there anything else we can do? Yep.
Actually, it is here at full price. We
see that this variable is only assigned
once. It's not reassigned further down,
just used here in the console log.
Meaning that this one also should be a
const. So hopefully you now understand
why we want to use const and let. And if
it's still a bit confusing, don't worry
about it. We're going to continue using
these variables throughout this course.
So eventually it'll be completely
natural for you to switch between the
these two variable types. With that,
let's return back to our lead tracker
app.
Okay, we are back in our lead tracker
app. And now we need to make sure that
when the user hits the save input
button, a lead is pushed to the myle
leads array. For now, you're just going
to use a hard-coded lead. So,
www.awomelead.com,
for example, and that value should be
pushed to my array when the input button
is clicked. So go ahead and give this a
shot right now.
Okay, hopefully what you did was to here
inside of the body of this function
write my leads
push and then pass in www.
awesomele.com
like that. If we now also console log
out the my leads array like that, we
will actually verify that it works. I'll
hit the save input button and there you
can see we now have one lead in the my
leads array. If I hit it again, we have
two and three and four. However, it's
pretty useless because we're just
pushing the exact same thing to the
array. So, let's fix up that in the next
cast.
Okay. So now we are going to take
whatever the user enters into the input
field right here
and push that value into the my leads
array when the save input button is
clicked. So push the value from the
input into the my leads array instead of
the hard-coded www.awomeleads.com
value right here. However, I haven't
taught you how to actually get the value
from the input L. That is, for example,
this www.example
value right here. So, you need to Google
that. And that's great practice because
googling is, as you know, something
professional developers do every single
day.
So, here is what I recommend you to
Google. Try to find, for example, a
Stack Overflow question and see if you
can figure it out on your own. If you
don't, no worries. I will solve this for
you when you return back to this cast.
So, go ahead and do this right now.
Okay, hopefully this went well. Let's