Lets build 4 javascript projects for beginners | chai aur #javascript — Transcript
Full transcript
- 0:00Yes, how are you all? Welcome to Chai
- 0:03Aur Code, and we're running our series,
- 0:05which we've renamed Chai Aur JavaScript
- 0:08. Look, I don't think there could be a
- 0:09better name than this. Let me know in
- 0:12the comments how you liked this new
- 0:13name, Chai Aur JavaScript. Similarly,
- 0:15we've already done Chai Aur HTML, and
- 0:17we'll be doing more soon. But for now,
- 0:20our main focus of this video is your
- 0:22favorite projects. Yes, I know a lot of
- 0:25people are excited about projects. But
- 0:28it's not like we can do high-level,
- 0:30high-end projects right away. Because
- 0:32we've only studied the basics. We've
- 0:34finished the basics, and we've finished
- 0:35intermediate. We've studied a little
- 0:37bit. But as I always say, you won't be
- 0:38able to do full-fledged projects after
- 0:40studying just this much. But that's not
- 0:42the case. In this video, I'll share a
- 0:44very exciting project: projects you've
- 0:46never done before—in a course, a paid
- 0:49course, or a boot camp. You've likely
- 0:51never had an experience like the one
- 0:53I'm going to give you for free on this
- 0:55channel, with projects. Yes, I have
- 0:57some expertise, having written code for
- 0:5910-12 years, so I'll have to share that
- 1:01experience. That's it. Look, we're less
- 1:04noisy here and more practical. So we
- 1:07won't make a big fuss about something
- 1:10terrible. But yes, we've done a great
- 1:12job, which will make your project
- 1:13journey much better. Your project
- 1:15practice will increase, and you'll be
- 1:17able to practice multiple times without
- 1:19worry. Just open your browser and
- 1:21you'll be able to do many projects. Now
- 1:23, we won't be able to do many projects
- 1:25because we haven't discussed events yet
- 1:27. We'll need one or two long videos
- 1:30about events, in which we'll understand
- 1:32events: how events work in JavaScript,
- 1:35what they do, what types of events
- 1:37exist, how to use them, and how to
- 1:39delve deeper into them. And without
- 1:41events, it's actually impossible to do
- 1:43many projects without DOM. In fact,
- 1:45it's impossible to do any project at
- 1:46all. So, during this project, this long
- 1:49video, I'll be giving you some tips and
- 1:52hints about how events are happening.
- 1:54But please assume that if anyone asks
- 1:56in the comments that I didn't explain
- 1:57this or that, please tell them, "Oh, I
- 1:59don't have the patience. How can I
- 2:01become a programmer?" Right now, we're
- 2:02just studying the basics of events.
- 2:04Just enough to do the projects. But
- 2:07after that, we'll delve into events in
- 2:09depth. Okay? So now let's talk about
- 2:12our projects. How many projects are we
- 2:13going to do? We're going to do four
- 2:15projects in this one video. And with
- 2:17great difficulty, I've set up projects
- 2:19that gradually increase in complexity
- 2:21so that you gain confidence, because
- 2:23this series has one main motive: to
- 2:25build confidence that you can write
- 2:27code. Okay? So now I'll show you how
- 2:29much hard work we've put in here. Not
- 2:31only that, but I've done it myself,
- 2:33because I don't have a team on this
- 2:34channel. Everything has to be done by
- 2:36myself. So let me tell you what I
- 2:39actually did here. First, I went to
- 2:41Code Sandbox. If you're unfamiliar with
- 2:43Code Sandbox, it's a great website.
- 2:45What happens here? You can create
- 2:46templates for JavaScript and other
- 2:48similar things so people can practice
- 2:49them in the browser, or you can write
- 2:51the entire code in the browser. As
- 2:53we've done throughout the series, I was
- 2:54writing the entire code here inside
- 2:56GetUp. I was writing it in the browser.
- 2:58What I did now was use Code Sandbox,
- 3:00which I always do. I have a lot of old
- 3:02projects that I give to people for
- 3:03practice. I'll give them to you too.
- 3:05Don't worry. I encountered a few minor
- 3:07problems there. The platform is good.
- 3:09No problems. But I encountered some
- 3:11minor issues there, and I thought I'd
- 3:13be able to work through them with
- 3:15practice. So, I didn't complete the
- 3:17project. I then moved the project to
- 3:20another website called Stock Blades.
- 3:22Now, what's inside the Stock Blades
- 3:23website? I created these DOM projects,
- 3:25and this is how you'll see them when
- 3:27you open them. Now, since I've created
- 3:30an account and created all the code
- 3:32here, any changes I make will be saved.
- 3:35I don't want to save those changes. I'm
- 3:37not going to give you the code file.
- 3:40But what I want is an experience where
- 3:42you don't have to worry about creating
- 3:44any installation files. You can simply
- 3:47go ahead and write JavaScript code,
- 3:48practice the code, and complete the
- 3:50project. This is the experience I
- 3:52wanted everyone to have. Now, it took
- 3:54some time to create this experience, so
- 3:56I've made you wait a little while. But
- 3:58now, since I've created the project
- 4:00here, I can share this project with you
- 4:03. And I'll share it with you. I want
- 4:06your index file to be there by default,
- 4:08and I'll copy it here. I'm sharing this
- 4:10so you can see behind the scenes of how
- 4:12much work was done here and how you're
- 4:13getting the project. If I gave you a
- 4:16direct, magical link, you'd say, "Yes,
- 4:17everything is working." But you should
- 4:19also know the behind-the-scenes of how
- 4:20the labs were created. What services
- 4:22did I use, how did I get help, what all
- 4:24happened? So I copied the URL. Okay?
- 4:28Now, one thing you should keep in mind
- 4:29here. What some people do is open a new
- 4:31incognito tab and paste it there. You
- 4:34can do that. No problem. The FIR will
- 4:35be created there too. But what happens
- 4:37there? You won't see any code preview
- 4:40on the right-hand side. Okay? So please
- 4:43don't open it in incognito mode. Open
- 4:45it in your regular browser because I'm
- 4:47already logged in. Anything I save will
- 4:49be saved. Then how will you practice?
- 4:52So what's the point? I'm opening a
- 4:53Safari browser here. You can use any
- 4:56browser, Chrome, anything; it won't
- 4:58matter. The problem is because I'm
- 5:00locked in. Now, you won't have my code
- 5:02files, so you're not locked in, it
- 5:04won't matter. So, I opened this same
- 5:07URL here. Now, what's the point of this
- 5:10URL? Suppose I wrote it here. I wrote
- 5:13it all. Okay? Now, I wrote it, but
- 5:15suppose I want to go to Project One, so
- 5:17I'll click here and say, okay, this is
- 5:19Project One. I want to create this
- 5:21color switcher, and I can go back to
- 5:22home. The link to the YouTube channel
- 5:24is there, obviously. But suppose, for
- 5:26some reason or by mistake, you deleted
- 5:28all the links. So, here they are, the
- 5:30links are deleted. So what happens now?
- 5:33No problem. If all the links are
- 5:35deleted, just refresh this page and say
- 5:37, "Yes, leave this page." Refresh it,
- 5:40and you'll have the exact code, what we
- 5:43call starter code, so you can focus on
- 5:45it without any worries. But if you want
- 5:49to save any changes you're making, like
- 5:51whatever JavaScript I'm writing, then
- 5:53click on the "difference" button. What
- 5:56will that do? The entire project will
- 5:57be uploaded to your Stag Blitz. Yes,
- 5:59you'll have to create a Stuckets
- 6:00account. I don't have any sponsorship
- 6:02from Stuckets or Code Sandbox. In fact,
- 6:05I purchased Code Sandbox, so I didn't
- 6:07take any sponsorship from my own money.
- 6:09So, if you want, click "fork" here, and
- 6:11it will say that the project has been
- 6:13forked. Now, any changes you make will
- 6:15be saved, but you'll need to sign in.
- 6:17It won't save without signing in. Okay?
- 6:20So, the second thing is, where will you
- 6:22get this link? You'll find this link in
- 6:24your code file. So, what do we do here?
- 6:28Simply create a Markdown file. And
- 6:30through that Markdown file, I'll take
- 6:32you all the links here. So, let's
- 6:34create a new folder and name it 07 "and
- 6:39" projects. Here, inside this 07
- 6:42projects, we'll create a new file
- 6:45called projects. A set "one .dot" md.
- 6:53An md is a Markdown file. We won't
- 6:56discuss much about the Markdown file
- 7:00right now. Yes, we won't discuss much
- 7:03about our Markdown file, what it does,
- 7:05how it works, why it works. But you'll
- 7:08see a little button above. This button
- 7:10opens a preview for your Markdown. By
- 7:13the way, I've made a nice detailed
- 7:14video about Markdown on my English
- 7:16channel. If you request it, please
- 7:17leave a request in the comments section
- 7:18. I should get a lot of requests. Then
- 7:21I'll upload a Hindi Markdown video here
- 7:23as well. So, we've opened the preview
- 7:24of this Markdown. Now, what can we do
- 7:26here? Just put a hash and say "Projects
- 7:31Related to Markdown." Okay? Now, I need
- 7:37to write the project link here. So,
- 7:38I'll write the project link here as
- 7:40well. How do I place the project link
- 7:42and the project link here? Here we go.
- 7:47This is our project link. And let's
- 7:50name it "Click Here." So, anyone who
- 7:56sees the preview just needs to click
- 7:57here, and anyone who doesn't want to do
- 7:59it can copy and paste it from the code
- 8:00file here. Now, you have the project
- 8:02file. Now, not just that, what else can
- 8:05we do here? I'll also give you the code
- 8:07for the projects a little bit more so
- 8:08you can see the final solution code. So
- 8:11, here I'll write "Solution Code" and
- 8:16here comes "Solution Code." So, there
- 8:20you have the solution code. And here
- 8:22we'll write "Project One." Here we go,
- 8:27Project One. Now, here I have to write
- 8:29JavaScript code. So, I'll put three
- 8:31backticks, then I'll write "j vascript
- 8:34javascript" and these three backticks
- 8:37to end. Now, whatever code I write here
- 8:40, for example, let's say I write "
- 8:42console.log" here, and here we write "
- 8:45Hitesh." Here you go. So, you'll see
- 8:48that code here. If you look at the
- 8:50Markdown preview, we can copy and paste
- 8:52it from here. So, whatever solution we
- 8:54create for Project One, we can easily
- 8:56find it here. Okay? Got it? I'll even
- 9:00push it so you can all see it. So,
- 9:02we'll repeat the same steps: solution
- 9:04code for Project One, solution code for
- 9:06Project Two, and so on. But the best
- 9:08part is that we need to go to Safari
- 9:10now. Here's our Safari, and we just
- 9:12need to focus on completing these
- 9:14projects. The video will obviously be a
- 9:16little longer because we're working on
- 9:18four projects simultaneously, with the
- 9:19final one requiring a lot of work. So,
- 9:21please maintain some patience. Since
- 9:22you've been requesting me to make long
- 9:24videos for so many series, I think
- 9:25you've developed patience. You'll have
- 9:27a lot of fun, and you'll find it very
- 9:29interesting. Okay? So, let's close this
- 9:32. You can also go into Zen mode. See,
- 9:34when you go into Zen mode, you'll only
- 9:36see the code. I really like it when
- 9:37it's visible. I'll exit it now because
- 9:39I also need to insert myself into the
- 9:41video, so a small little bit at the
- 9:43bottom looks fine. Okay? So, what do we
- 9:46do now? First, let's delve into our
- 9:48color changer. Let's discuss this
- 9:50project. So, look, the color changer is
- 9:52a theme switcher, a project called
- 9:54Color Switcher Curry. It's created in
- 9:56many places. It's used in almost every
- 9:58course. So, we'll do it too. Obviously,
- 10:01the goal is simply to click on this,
- 10:03click on this, click on this, click on
- 10:05this. The entire background color
- 10:07should change. That's all it takes. Now
- 10:10, look, we know how to change the color
- 10:11because inside the DOM, we'll add a
- 10:13style and color inside the body. Okay?
- 10:16No problem. But how to solve it here is
- 10:18the biggest challenge. Because in a
- 10:20project, only what you've learned will
- 10:22appear. Remember, this isn't your 12th
- 10:23grade education, where only what you've
- 10:25learned will appear. A little more
- 10:26detail is also involved here. We also
- 10:28go into a little more detail in the
- 10:29project. So, look, these are our
- 10:31buttons. Okay, sir? Now, what happens
- 10:33with this? It seems to be an event. We
- 10:34haven't talked much about events yet,
- 10:35and we won't do that either. We'll talk
- 10:37about it in a separate video. Now,
- 10:39let's try to solve this project so you
- 10:42can understand it. So, what's the
- 10:43project? First, open the HTML. So,
- 10:45here's the HTML. Zoom in a little bit.
- 10:49Oh, and then I'll open the settings
- 10:53here. My settings are open. Ah, project
- 10:58settings here, and workspace settings.
- 11:03Here, I'll search for fonts. Font size.
- 11:10Yes, sir. Here's the font size. So,
- 11:12I'll make it a little bigger so you can
- 11:14see it better. Let's make it 20. Yes,
- 11:17sir. Now you'll see the font clearly.
- 11:19Let's save the settings. Since I'm in
- 11:22Safari, you won't see any changes I'm
- 11:24making. But I'll make the changes
- 11:26inside it so you can see the font
- 11:27clearly. Okay? So, let's go back inside
- 11:29. Now let's look at the index. You
- 11:31should be able to see it clearly now.
- 11:33We'll move it to the side because it's
- 11:34not needed that much. Even if it's
- 11:35smaller, it'll do. It's not that
- 11:37problematic. I was writing some styling
- 11:39and such. So I'll clean up this code.
- 11:41Don't worry about that. I've cleaned it
- 11:43up further. Now let's come back here.
- 11:45Okay? So the main thing right now is
- 11:46that we have this color switcher. This
- 11:48is a span. Inside it, we have these
- 11:50buttons, classes, and each one has an
- 11:52ID. Gray, White, Blue, Yellow. So,
- 11:55these are the four IDs we have. We've
- 11:57specifically assigned IDs accordingly
- 11:59so we can change the color based on
- 12:00them. If you want to add more, you can
- 12:02add more. Along with that, we also have
- 12:04CSS, which you don't need to worry
- 12:06about. We're not focusing on CSS right
- 12:08now. And we have a script called tea
- 12:09and code. This will be the script, and
- 12:11which one will it be? So if you look at
- 12:13the code, we don't have anything right
- 12:14now. It's completely empty. So how do
- 12:17we achieve this goal? First, we need to
- 12:21select the elements. Which elements? We
- 12:23need to select all the buttons. Okay?
- 12:26What do we need to do next? We need to
- 12:27select the body. Obviously, our goal is
- 12:29to change the color of the body. So, we
- 12:31see two elements here. One is the body.
- 12:33So how do we select the body?
- 12:35document.query.sector.body. Okay? The
- 12:37body is selected here. Or you can also
- 12:39select the document body. That's fine
- 12:40too. And we need to select all these
- 12:43buttons. Now, you can select by class
- 12:45or select by ID and hold down each
- 12:46button one by one. So, it's better to
- 12:49select them all at once. Why? So I can
- 12:52loop through this later. Obviously, we
- 12:54need to do it the easy way. So, using a
- 12:56loop seems easier to me. Okay? So,
- 12:58let's move on to our code. So, what do
- 13:01we do first? Let's get the buttons. Now
- 13:02, how do we get the buttons? To get the
- 13:04buttons, I first need a document, and
- 13:06inside it, I can write "dot query
- 13:08sector." Should I use "query sector" or
- 13:10"query sector all"? We need to select
- 13:12buttons. We need to select all. So,
- 13:13here's "query sector all." Now, what do
- 13:16we need to write inside it? We need to
- 13:17write "all buttons." So, dot buttons.
- 13:20Okay? Are these buttons or buttons?
- 13:21Let's check that once. Look, here we
- 13:24have buttons. Okay? That's all done.
- 13:26Now we have all the buttons. So, let's
- 13:29hold it in a variable. So, take this
- 13:31cost and call it "buttons." Here you go
- 13:35. If you want, you can even lock the
- 13:37console, saying, "Let's lock the
- 13:39console." A shortcut won't work here.
- 13:42console.log and tell it to print these
- 13:46buttons. Here, you'll see a small
- 13:49console at the bottom. So, look, we
- 13:51have a node list. We've also studied
- 13:53the node list prototype. It's not shown
- 13:55in as much detail here, but we've
- 13:56already studied it. So, we know how it
- 13:58works now. Okay? So, that's the basics.
- 14:01We don't need this console lock anymore
- 14:02. Let's remove it. Now, what do we need
- 14:05? We also need a body. Okay? So, we
- 14:07wrote const body because we need to
- 14:09hold it in a variable. How do we get
- 14:11the body? There are many ways to select
- 14:13the body. What will we do? Let's use
- 14:15the document.query field. And directly
- 14:17inside the query field, we'll provide
- 14:18the tag name. What tag name do we have?
- 14:20Body. Okay? So, we have the body, and
- 14:23we have the buttons. We've learned this
- 14:25much about the DOM. Okay? But now, can
- 14:28I also loop over the buttons? Yes, I
- 14:30certainly can. But we haven't studied
- 14:33events much. We'll briefly explore how
- 14:35events work. So, what are events? There
- 14:37are many events within the browser.
- 14:39Moving the cursor in the browser is an
- 14:41event. Closing a tab is also an event.
- 14:44Clicking somewhere is also an event. So
- 14:46, there are many events. We'll learn
- 14:48more about events. For now, our goal is
- 14:50simply to capture the event you click
- 14:53and perform certain actions with it. As
- 14:56we'll see, you'll start to understand
- 14:57once you do a project. It's not that
- 15:00complicated, and since I'm here, it's
- 15:01bound to be easy. Okay? So, we've got
- 15:03buttons, okay. Here are the buttons.
- 15:06Now, we know that we've got a node list
- 15:07. We've got a node list, not an HTML
- 15:09collection, so I know I can use a for
- 15:11each with it. Okay? You already know
- 15:13that much. It's even suggested that you
- 15:15could put it on a list. Okay, sir? I've
- 15:17put this for each. Now, inside the for
- 15:19each, I know I can write a callback.
- 15:21You can write an arrow function, a
- 15:23regular function, whatever you want.
- 15:26Here, I've written the function. And
- 15:28what's inside this function? Each one,
- 15:30because it's a for each loop, you'll
- 15:32find each button there. So, here, I'll
- 15:35call it button. Okay? Now, I've got
- 15:37this button, and I'm going to console
- 15:40lock it first, just for clarity.
- 15:42console.log. And let's see what we
- 15:45actually get inside the button. Here,
- 15:48we've printed the button. Let's look at
- 15:50the console log. So, you have the
- 15:51entire button, which is a complete HTML
- 15:53span, and you've got all the attributes
- 15:55inside it. You've got the tag name.
- 15:57You've got the inner HTML. I've got all
- 15:59the attributes, what are they? I've got
- 16:01the class. I've got the ID. So, at
- 16:03least I get all the elements of that
- 16:05button here. Okay? That much is clear.
- 16:08Now what do we do here? We'll do a
- 16:11little work here. We'll add an event
- 16:13listener to each of these buttons. Now,
- 16:15why would we add an event listener?
- 16:17Because, we know events happen inside
- 16:18the browser, but we also need someone
- 16:20to listen to them. Imagine if someone
- 16:22is standing alone and speaking
- 16:23something, and no one is listening to
- 16:25him, how long will he keep speaking?
- 16:27For example, if I'm teaching you on
- 16:29this channel, and no one is watching or
- 16:31sharing the videos, or subscribing, how
- 16:33long will I keep speaking? I'll think,
- 16:35okay, no one is listening, so stop this
- 16:37video series. Focus elsewhere, on our
- 16:39main channel, where 800,000 people are
- 16:41listening. But no, I know you're
- 16:43listening, you're subscribing. So, we
- 16:45need to look at events here too. So,
- 16:48how do we add events? You can add
- 16:50events anywhere. How do we do it?
- 16:51Suppose this is an individual button.
- 16:53If you loop it, it will be applied to
- 16:54all of them. You can simply add this
- 16:57method to it to add an event listener.
- 16:59Okay? Did you see? It's very simple,
- 17:01you add an event listener. Event
- 17:03listener means I'll listen to every
- 17:05event that happens here. But I don't
- 17:06want to listen to all events. The mouse
- 17:08hovers over it, the page refreshes, the
- 17:10page loads. There are many types of
- 17:12events. We'll discuss them later. So, I
- 17:14don't want to listen to all events. I
- 17:15want to listen to the click event. Okay
- 17:17? You can also listen to the mouse over
- 17:18if you want. So, you write click here.
- 17:21Okay? You've heard the click event. But
- 17:23what if there's a click event? Yes,
- 17:25that's great. So what do you do? Write
- 17:27a callback here. Here's the function,
- 17:30and here's the callback. Okay? So the
- 17:32event is done. Now we've called it. But
- 17:35what happens next? Whenever an event
- 17:37occurs, you have to carry that event
- 17:38along. That event object contains a lot
- 17:41of details. I'll explain what you find
- 17:44in those details later. For now, just
- 17:47name it 'e'. Now, look, this is just a
- 17:49name. You name it 'e'. Some people call
- 17:50it 'event'. Some people might call it '
- 17:53superman'. Some people might call it '
- 17:55tea'. Name it whatever you want. It's
- 17:58actually the event object itself. Okay?
- 18:00So, don't worry about variable names.
- 18:01So what do I do here? I'll name it 'e'.
- 18:05It'll be short, it'll be easy. Okay,
- 18:07sir. I've named it 'e'. Now, what do we
- 18:09do next? First, let's do a console log.
- 18:12Okay? console log console dot log,
- 18:16because only the console log will tell
- 18:17you what to do. Let's create this event
- 18:20and another one, e dot target. Okay? I
- 18:23want to create it on both of them so I
- 18:24can actually tell you what the event is
- 18:25. An event is a very large object. We
- 18:27won't go into too much detail about it
- 18:28right now. So, look, I clicked here.
- 18:30All the elements are displayed. Now I
- 18:32click on this button. Look, here comes
- 18:35the mouse event because e is a mouse
- 18:37event, but it has many values. Is
- 18:40trusted true, and within the prototype,
- 18:41you get many more values. As we saw in
- 18:43the node list, we'll inspect that too
- 18:45and read about it completely. And then
- 18:48what happens is, you also have this
- 18:50span list element because event dot
- 18:51target, which target this event came
- 18:53from. So, inside that, you've got an
- 18:55HTML span element, and you've got these
- 18:58attributes. So, right now you have gray
- 19:00. Now, if I click on the yellow one,
- 19:02look, let's zoom in on the HTML span a
- 19:04little bit for you. So look, you've got
- 19:06an HTML span here, and here you've got
- 19:08attributes, and this time you've got a
- 19:10yellow button. Earlier, you had a gray
- 19:12button. So, by typing "event.dot.target
- 19:14," you know where this event is coming
- 19:16from. We'll talk about this in more
- 19:18detail. So, now that I know there's an
- 19:20event.dot.target, I can extract the ID
- 19:22from within it. How do I extract the ID
- 19:24? By adding a dot.dot.id, I'll get
- 19:26whatever the gray/yellow is. So, what
- 19:28do I do now? Now that we've discussed
- 19:30this much, everything else is easy. You
- 19:32can use an if else statement. You can
- 19:33use a switch case, just like I do with
- 19:35an if else statement. Okay? So, I've
- 19:38simply used an if statement. Inside the
- 19:40if statement, I'll check the value of
- 19:43the event.dot.target and the ID we're
- 19:46getting inside it. So, what do we have
- 19:49first? It's gray. So, I'll select gray.
- 19:52Now, I'll have to see if I've written "
- 19:55GREY" or "A." Ah, yes, sir. I've
- 19:58written "GREY" here. Okay? The tea will
- 20:02continue to run. See, if the code works
- 20:04, the tea will also work. Please ignore
- 20:06that. Okay? So what am I going to do
- 20:09here? I already have a body. You know
- 20:11that. Look, I've given you so much
- 20:14practice in the DOM that if you go in
- 20:16this far, you can change the color of a
- 20:18body. Okay? Body dot, here, take the
- 20:23style. Now what? Background color.
- 20:26Background color. Okay, sir. Now what
- 20:28do you want to do with this background
- 20:30color? If you want, you can also
- 20:31hardcode a value here. Gray. Or if you
- 20:34want to get a little more programming
- 20:36practice, what do you actually do?
- 20:38Instead of hardcoding a value, write e
- 20:41dot target dot ID, because the value
- 20:44inside it is gray too. So you can write
- 20:47it like this. Okay? So there you have
- 20:50it. Okay, sir. So now let's minimize it
- 20:53and see, click on gray. The body has
- 20:55turned gray. Very easy. You can refresh
- 20:58it again. Click this and it's gray. Now
- 21:01what can I do in the same way? I can
- 21:02change more values. So, okay? It's gray
- 21:05. This will turn white. This will turn
- 21:07blue. This will turn yellow. Okay? I
- 21:08just copied and pasted it, what else
- 21:09have I done? So what do I have to do?
- 21:11Just come here and write, "Look, this
- 21:13is white. This is blue. And this is
- 21:17yellow." If you want to add more
- 21:19buttons, add more. What's the worry?
- 21:21What's the worry? That's all I had to
- 21:24do. So, look, this is gray. This is
- 21:25white. This is blue. This is yellow.
- 21:27That's all I had to do. Now, what's
- 21:30even better than this? I can go into
- 21:32the index HTML here and tell it, "Hey,
- 21:34do one more thing, duplicate it. And
- 21:37this time, what's the ID for it? Make
- 21:38it purple." Let's make it purple. Here,
- 21:42purple. So, you've done this purple.
- 21:44But obviously, the class is button,
- 21:46with an ID of purple. So, I don't have
- 21:48purple in my CSS right now. But okay,
- 21:50it doesn't say purple in the CSS, it'll
- 21:52only show white; but I know it's purple
- 21:54inside. So what will change in the code
- 21:55? Nothing will change in the code. What
- 21:57do you have to do? Check that its ID is
- 21:59purple, so you set its color to the
- 22:02same purple as the target color. So, I
- 22:05saved it. Now click here. Obviously,
- 22:07it's set to purple. Obviously, I didn't
- 22:09denotate it as purple in the CSS, but
- 22:11you can understand this much; now
- 22:12you're advanced. You're starting to
- 22:14understand things. Okay? I just did
- 22:16this to show you a demo. So, if you
- 22:18need this code, you already have it. So
- 22:20, it's obvious you'll say, "Sir, give
- 22:21me the code," because if I can't do it,
- 22:23at least I can see the reference. Yes,
- 22:25absolutely. So we'll copy this and go
- 22:27to the top of our browser and here's
- 22:30the console log. Hitesh, I'll leave it
- 22:32there and write all the code below it.
- 22:34So what does this do? I wrote this code
- 22:36. So all this code is available to you
- 22:38here. Okay? Okay? So I'll push it to
- 22:40the top to show that we've completed
- 22:42Project One. So this is our Project One
- 22:47complete. You can't call it complete.
- 22:51Let's write the solution code. Oh, I
- 22:54don't know what the keyboard is doing.
- 22:56Solution code. Okay, sir. Let's commit
- 23:00it. Okay, so how do we commit? We
- 23:02haven't even staged it before. Yes.
- 23:04Should we make a video on the gate? We
- 23:06will. We will. We will very quickly. So
- 23:07let's commit this and push it so you
- 23:10can easily get the code. Okay, sir. Now
- 23:15let's go here and refresh the code to
- 23:17see if it's loaded or not. So this is
- 23:20our Project Seven. Okay, sir. We have
- 23:22its Markdown file. There you go, it
- 23:24looks great just by clicking. You don't
- 23:26have to do anything. You've got a copy
- 23:27automatically because it's marked down.
- 23:29Just click. See how easy it was to copy
- 23:31all the code? Okay? So, you've learned
- 23:34quite a bit about how to use events,
- 23:36how to use them, and how to manipulate
- 23:39documents. Now, let's tell you the best
- 23:42part: you said, "Sir, you've done it."
- 23:45Now, how do I practice here? Your
- 23:47practice is very easy. Go to this URL.
- 23:50Now, we're on another URL. We'll go
- 23:52back here and copy the URL link from
- 23:54here. Copy the link address here and
- 23:58open a new tab. Where do we open a tab
- 24:01in Safari? I rarely use Safari. So,
- 24:04okay. Leave this page. We're back on
- 24:07the same page. Open it in Safari, or in
- 24:10Chrome, wherever you want. You've got
- 24:13the project. Now you go here and say, "
- 24:15This is what I want to do, a color, a
- 24:18background changer, and then go into
- 24:20the code. See, there's no code involved
- 24:22. Practice as many times as you want.
- 24:24Do it in different ways. Do it with if
- 24:26else, do it with switches. Whatever you
- 24:28want to do, if you want to use another
- 24:30approach, use that approach. So look
- 24:32what a great playground we've created
- 24:33for you. So, this gets you a like and a
- 24:36share. Okay? So you get it. Now let's
- 24:40move on to our second project, which is
- 24:44much easier than comparison. This one
- 24:46requires a small formula. We won't go
- 24:49into the formula much. You can look it
- 24:50up whenever you want. Simply Google it
- 24:55and type in" How to find a BMI index. "
- 25:02Here you go. So, I need the BMI index
- 25:06formula. So, you'll get the formula:
- 25:09weight in kilograms divided by height
- 25:11in meters squared. And it's also listed
- 25:14here, saying," You can square this by
- 25:15multiplying your height by your weight.
- 25:17"So, we won't delve too much into the
- 25:18formula, as it's a mathematical term.
- 25:20You can use it at any time. The basic
- 25:22idea is how we'll handle this project.
- 25:24So, click back. We're starting the
- 25:26project from here. Please enter the
- 25:28time stamp. You can do this at least.
- 25:30So, we'll add the BMI generator here.
- 25:32Look, we need your height in
- 25:33centimeters. Enter your weight in
- 25:35kilograms. And as soon as you click"
- 25:37Calculate, "you'll see a calculation.
- 25:40And BMI is a guide to what's
- 25:42underweight? So, if your ratio is like
- 25:44this," normal "is like this,"
- 25:46overweight "is like this, then you get
- 25:48the values. So, how will we handle this
- 25:50project? First, we'll look at what this
- 25:52project does and what its HTML looks
- 25:54like. So, here's its HTML structure.
- 25:56Now, because I obviously made some
- 25:58changes here, I'll have to change the
- 26:00workspace settings again so you can see
- 26:02the code. Let me do one thing: I'll
- 26:05save the changes right in front of you,
- 26:07so you can see it. So, I'll permanently
- 26:10change the workspace settings to make
- 26:12it a little easier. So, let's change
- 26:15the font size back here. So, let's make
- 26:20it 20. You're also so small, oh, what
- 26:23happened? 20. Yes, sir. It's fine now.
- 26:26So, I'll save it here. Now, when I save
- 26:28something here, what actually happens?
- 26:30You get all the saved files there by
- 26:33default. So, I don't have to do
- 26:34anything. I just reload it once, and
- 26:37now I'll have all those settings on by
- 26:41default. Yes, once I propagate it here,
- 26:44everything automatically comes in.
- 26:45Sometimes it takes a little time, a
- 26:46couple of minutes, but that's fine.
- 26:49Let's refresh it one more time. Here we
- 26:53go. Yes, sir. Right now we're seeing a
- 26:56bit of code. Okay? That's why I'm not
- 26:58doing it in my own, because if I save
- 26:59anything there, everyone will see it. I
- 27:01want us to play in the Playground. Okay
- 27:03? Okay? So here we are in the HTML. So,
- 27:05inside the HTML, we have a basic
- 27:07navigation bar at the top. Here we have
- 27:10a full div, which is a container.
- 27:12Lastly, our script is loading the
- 27:14JavaScript key code. This is our BMI
- 27:16calculator. Okay, sir? Here's the BMI
- 27:18calculator. Now here we have a form.
- 27:21Now, look, when events occur on a form,
- 27:23many other concepts arise. We won't
- 27:25discuss that right now. There's also
- 27:26the concept of event bubbling. There's
- 27:27a lot of other things. We won't discuss
- 27:29that right now. We'll discuss it in an
- 27:30extra dedicated video. So, don't go
- 27:32into that right now. So, look, here you
- 27:34have a label. Next, you have an input
- 27:36and a height from the input. Now, the
- 27:38first problem we'll solve is how to
- 27:41extract values from the input. Right?
- 27:43That's done through events. Then,
- 27:46there's another label with an ID called
- 27:47" weight, "so we need to extract the
- 27:49value from there. Okay, so once we have
- 27:52the value, I can simply calculate it.
- 27:54Whether I multiply it or divide it,
- 27:55whatever I'm doing isn't a big problem.
- 27:57Second, here's our result. Now, what's
- 28:00the result? If you type" Hitesh, "you
- 28:03get" Hitesh. "This is the result. By
- 28:06default, there's nothing inside it. But
- 28:08we'll replace it here. So, we've
- 28:10studied how we can take inner html and
- 28:12inner text, and inject them into the
- 28:14DOM. Okay? It's very easy. We'll do
- 28:16that too. And whether this guide is
- 28:18just for you or for the user who uses
- 28:20it isn't something we care about. So,
- 28:22you see, it looked quite complex. But
- 28:24once you analyzed it a little, you saw
- 28:26that there wasn't anything quite that
- 28:27big in it. So, okay, let's move on. Now
- 28:29, when I saved it here, the project
- 28:31changed. But actually, there's no
- 28:32change because I'm not signed in yet,
- 28:34so there's no problem with it. So,
- 28:36let's move on. So, what do we do first?
- 28:39We need to select this form. So, why do
- 28:42we need to select the form? Because,
- 28:44look, we need to select the entire form
- 28:46because you have a submit button within
- 28:48that form. As soon as you go here, the
- 28:50calculate BMI button is actually the
- 28:52submit button. When submitting the form
- 28:55, we'll talk about events here. We'll
- 28:57use a submit type event. There's no
- 28:59click event here because it's a form.
- 29:01That's why I was telling you to be
- 29:02patient. For example, if we study
- 29:04events, it will be more convenient for
- 29:05you. But that's okay, we can learn some
- 29:07things on the go. Okay? So what should
- 29:09we do first? Here we have the document.
- 29:12Next, the query field. What's inside
- 29:14the query field? Just provide it
- 29:16directly. There's no need for a class
- 29:17ID. Here you go, form. Hold it in a
- 29:19variable. There you go, that's it. Okay
- 29:22? So we have the form. Okay, sir. That
- 29:24much is clear to us. Here we have the
- 29:27complete form. This form is completely
- 29:29in our hands. Okay? Now what is a form?
- 29:32Whenever a form is submitted, you've
- 29:34probably read that a form is submitted
- 29:36using either a post type or a get type.
- 29:38But whenever a form is submitted, its
- 29:40values are sent to the URL or to the
- 29:42server. We need to stop that, because
- 29:44we're not sending it to the server. So
- 29:46stop the default action of the form
- 29:48right here. Okay? What do we need to do
- 29:51to stop that? First, you get a method
- 29:53directly on events. Yes, it will work.
- 29:58Look, if there's code, it will work. So
- 30:00, look, there's a form, and you have
- 30:02the same add event listener on it. Yes,
- 30:04it's the same add event listener
- 30:05everywhere. So, which event are you
- 30:07looking at this time? Not our click,
- 30:09not that click event. This time we're
- 30:11looking at the submit event. So, where
- 30:12can we find a list of these events? I
- 30:14just told you we need to be patient.
- 30:16We'll have to look at it when we talk
- 30:18about events. Okay? Again, you get a
- 30:20callback. So, here we go, callback.
- 30:22What do you get inside the callback? E
- 30:24event, Chai, Superman, whatever you
- 30:26want to call it. Okay? So, what's the
- 30:28first thing we do? With E, we tell it
- 30:30to prevent default. That means,
- 30:32whatever your default action is, stop
- 30:34it for 1 minute. Don't vomit submit.
- 30:37Okay? Now, what do we need? What values
- 30:41do we need? It's very easy to see. We
- 30:43need this height, and we need this
- 30:45weight. Okay? Now, what we do is select
- 30:49those values every time an event occurs
- 30:51in the form. What do some people do?
- 30:53They select them right here. Well,
- 30:54there's a big difference. First, I'll
- 30:55explain how to select them here. Then
- 30:57we'll discuss the difference. So, what
- 31:00will we do? Simply put, add a query
- 31:02variable to this document. What do we
- 31:05need after adding a query variable? We
- 31:07have the ID. So, we'll give you the ID.
- 31:09Here you go. We'll give you the ID
- 31:11first. What height? Give the weight.
- 31:12Give the height. Here we go. We've
- 31:14given you the height. Okay, what do you
- 31:15do after giving the height? Just type
- 31:17in the dot value and you'll get the
- 31:18value. It's a very easy process. So,
- 31:20this is how you get the input values.
- 31:23Okay? But how do you get the values? In
- 31:25a string. And remember? Back when you
- 31:27thought the series was boring. Yes,
- 31:29back then we taught you how to pass a
- 31:32string to an int? You can pass it to a
- 31:35float, by the way. So, here we are, we
- 31:37wrapped it. And told it what to do? Ah,
- 31:41simply parse it to an int. So now
- 31:43whatever value you got in the string
- 31:45has been parsed to an integer. Okay? We
- 31:47haven't checked here yet whether it can
- 31:49be parsed or not. Did someone write" a
- 31:51"or" what "? Let's not worry about that
- 31:52right now. We'll worry about it when we
- 31:54make a more complex project. Let's hold
- 31:56on to this one. Here, let's call it"
- 31:58height. "Here, okay? Let's move it a
- 32:02little to the side so you can see it
- 32:03easily, because the tech isn't what
- 32:05we're concerned with. As for the UI,
- 32:06the UI is whatever. Let's take another
- 32:08copy of it. Let's call it" here. "What
- 32:12did I write here? What all did it open?
- 32:16Okay. Here, I wrote" weight. "And
- 32:19obviously, its ID is also written here,
- 32:22weight. So, what's the difference
- 32:25between writing this height here versus
- 32:27writing this height outside the form?
- 32:30Look, what's the actual difference?
- 32:31You're trying to get those values right
- 32:34when you click. You're trying to get
- 32:36those values during that event. Because
- 32:38what will happen? What will happen if
- 32:39you write those values outside here?
- 32:41Your script runs as soon as the page
- 32:43loads, and that empty value will be
- 32:45stored inside this height. So, do you
- 32:48want an empty value or some value
- 32:50inside it? The use case depends on you.
- 32:51Our use case right now is that we want
- 32:54to extract whatever values are inside
- 32:56that values after submitting. We don't
- 32:58want empty values. So, if you write it
- 33:00here, it will be wrong. So, what will
- 33:03happen? This use case will give you
- 33:09empty values. I've seen a lot of people
- 33:12, teaching for various companies, in
- 33:14various courses. People there are
- 33:16constantly worried about how their
- 33:18values are coming up empty, because the
- 33:21code after this is so simple. There's
- 33:23nothing after that, actually. Okay? Let
- 33:25me tell you a little about our code,
- 33:27which calculates the height and weight
- 33:29we just took. It could be completely
- 33:30broken. So, let's do a few checks. I
- 33:33won't discuss many checks right now,
- 33:35because we're not at that stage of such
- 33:37complex code, but let's talk about a
- 33:39few positions. So, what do we do?
- 33:43Actually, if you look closely, we have
- 33:45a result. Right? We haven't taken the
- 33:47result yet. We'll take that too. We'll
- 33:49actually take it right here. If you
- 33:51look carefully, we've talked about this
- 33:53a little bit. This is the results ID.
- 33:55Let's take this as well so we can get
- 33:57the results. We also get the results
- 33:59here. So let's call it results or
- 34:02results. They are actually results. Yes
- 34:04, they are results. So we'll call this
- 34:06results as well. And we get them
- 34:07through the query vector. There's no
- 34:09need for dot value. So here you go, dot
- 34:11results, and there's no need for
- 34:14percent because it's just one element.
- 34:17So here you go. So here you go. Okay?
- 34:21Now what are we going to do? We'll do a
- 34:22little check. If all those checks are
- 34:24correct, what will we do? We'll add
- 34:25values inside results. So, to add
- 34:27values, we know innerHTML is very easy.
- 34:30If you want, we've already discussed
- 34:31innerText. We won't do it again. So
- 34:33what are we going to do here? Please
- 34:37give a valid height because we're
- 34:42checking for height. As it is, we'll
- 34:44copy this and then. So, what other
- 34:46checks can we apply? So, look, the
- 34:47height has been parsed into an int. So,
- 34:50what's the first thing? It shouldn't be
- 34:52empty. Okay? And the second condition,
- 34:55since I'm talking about it, add another
- 34:58value. Here, what else would you like
- 35:01to check? So, what other values are
- 35:04there? What else do you want? If it's
- 35:06less than zero, I don't want it. Okay?
- 35:09And if it's not a number, that is, not
- 35:11a number at all, then what's the point?
- 35:12So, what's the point here? There's an
- 35:14old technique and a new technique. Many
- 35:15people check if height is equal to or
- 35:19not equal to, like checking if height
- 35:22is not equal to N. This is actually an
- 35:25old technique. Don't use it. I'll tell
- 35:28you a better technique that's still in
- 35:29use today. And actually, the updated
- 35:31JavaScript technique is recommended in
- 35:33the documentation. What they say is
- 35:35that you use this NN method and you
- 35:38pass in the height. So if it's 'true',
- 35:42then the 'true' height. If it's 'true',
- 35:44meaning it's convertible to a number,
- 35:45not a number, it's convertible to a
- 35:47number, so you'll get the value 'true',
- 35:48so there's no problem. And if it's '
- 35:51false', then we don't want to proceed,
- 35:52okay? So you'll get it, basically, okay
- 35:54. So now we'll save it, because we've
- 35:56already done all this work, so we'll
- 35:57check it first. So look, now we've
- 35:58passed in 'calculate', so it's saying,"
- 36:00Please give a valid height. "Okay, it's
- 36:02scrolling now, that's fine, no problem.
- 36:04And what can we see here next? If I
- 36:06give height an 'a', which can't be
- 36:09converted, and give it 'calculate', it
- 36:11will say" please give a valid one. "And
- 36:13if I give it a 'three' here, and then '
- 36:16calculate', it won't say anything. We
- 36:18haven't reset it yet. Okay? That's it.
- 36:21But if I want, I can also give a height
- 36:22value here. So if you want, we can
- 36:24convert it back to text so you can see
- 36:27that the value is actually changing. So
- 36:29here we are, I've given it the dollar
- 36:31value, and whatever the height value is
- 36:33, we'll also give it the height value
- 36:35here. Okay? When I save it, suppose it
- 36:37gives a. When I calculate, it says,"
- 36:39please give a valid height. "Not a
- 36:41number, that's great. And if I give it
- 36:44three here, then calculate, it says"
- 36:46please give a valid height. "A,
- 36:47actually, we're changing it because if
- 36:50this condition is met, then it will
- 36:52only run, and this value will only be
- 36:54set when it runs. Okay? You understand
- 36:56the code logic. Otherwise, what should
- 36:57we do? Set it to height. Okay, sir? So
- 37:00just to see. So let's save it. So here
- 37:03we go to four. When we calculate, we
- 37:05see that four is set. But if we
- 37:07calculate 4A, that also gets converted
- 37:09to four. But if you calculate AA, it's
- 37:12not a number. I had you discuss this
- 37:14whole thing in the beginning. I hope
- 37:17you remember. Okay, sir? So, you
- 37:19understand. Now we'll do the exact same
- 37:21thing in weight. I did this just to
- 37:23explain it to you. Let's copy and paste
- 37:26this. Here. Now check weight in the
- 37:28same way. Weight has the same checks.
- 37:31That the weight should not be empty.
- 37:33Even zero. Okay, we need to check the
- 37:34weight here as well. Let's copy this so
- 37:37I don't make any spelling mistakes
- 37:40everywhere. Here we go. Okay? So we
- 37:46have the weight. Okay. So we'll write
- 37:49the weight here as well. Okay, sir. So
- 37:52we've done that much work now. Perfect,
- 37:55now we have both the height and the
- 37:57weight. Okay, sir. Now what do we do if
- 38:00everything is working fine here, that
- 38:01is, if we have the height, what do we
- 38:03do here? We'll actually put an if
- 38:05because the height will be checked, and
- 38:06the weight will also be checked.
- 38:08There's nothing wrong with this code.
- 38:10But okay, for better syntax, we'll put
- 38:13an if and move it to the same line. So,
- 38:16else if the weight has been checked as
- 38:17well. Okay. If all this is checked,
- 38:20then what do we do in the else case
- 38:22here? Okay, if everything is fine, what
- 38:25do we do? Calculate the BOMI. Now, to
- 38:26calculate BMI, we need a full value.
- 38:29Look, I won't bother you too much with
- 38:30the full value here. Because, look, you
- 38:32can write it however you want. I'll
- 38:34give it to you directly. Here, I've
- 38:36written it down in my notes. So, this
- 38:38is the full value we have. Okay? What
- 38:40the full value says is, as we just saw
- 38:42in How to Find a BMI Index: weight in
- 38:45kilograms divided by height in meters
- 38:47squared. So, it's the exact same value.
- 38:50We have the weight, and then we
- 38:52multiply the height by it because we
- 38:54need it in that exact value. And height
- 38:57squared is height*height. So, okay,
- 38:59we've taken that and used 'to fixed to
- 39:01() because after multiplying and
- 39:02dividing, the number often ends up
- 39:04being very long. We don't need that
- 39:06much after the decimal point, so we've
- 39:07added' to fixed 'here to show up to two
- 39:09decimal values; I don't need anything
- 39:10beyond that. So, that's our formula.
- 39:13Now, look, you can create your
- 39:14temperature in the same way, and you
- 39:15can add the full value. That's the
- 39:17mathematical part, not really a coding
- 39:19part. So, let's take this and call it a
- 39:22() const, and call it bmi (). So, there
- 39:25we have bmi. This is bmi calculated,
- 39:27that's it. Now, what do we do next? We
- 39:29need to display this bmi in the results
- 39:31. So, show the results. Okay? Now, how
- 39:38do we show the results? It's very
- 39:40simple. Here we have our results. Okay?
- 39:43And what do we do inside that? Add
- 39:44inner h tm l. Inner HTML. So what do we
- 39:47do with inner HTML this time? We take
- 39:49the back text and add it to the span.
- 39:50Because it will be useful later. Yes,
- 39:51there's a project coming up where we'll
- 39:53write values like this. It's better to
- 39:55get some practice in now. Okay? Here
- 39:59you go. We wrote this in a span and
- 40:01injected the values inside the span. So
- 40:04here we go, take this with the dollar
- 40:06sign, and say, here you go, your BAMI
- 40:08is injected. Okay, sir. That's all we
- 40:10had to do. So we saved it and said,
- 40:13okay, let's say someone's height is 180
- 40:16cm, and 65 is the fictitious value.
- 40:19Calculate BAMI, and it calculated it.
- 40:22If you enter' a 'instead of 65,
- 40:23calculate it, then it said," Please
- 40:25give a valid height. "Okay, didn't I
- 40:28change the message? Look, I didn't
- 40:30change the message. So it gives the
- 40:32weight again, okay? So, 160 cm, and
- 40:35here it gives' q '. So it said," Hey,
- 40:38give a valid weight. "Okay, okay! Sorry
- 40:40, I made a mistake. So okay, let's give
- 40:42someone a valid weight of 88, so we've
- 40:44calculated 34. Now what can you do?
- 40:47This is your task, your assignment,
- 40:49based on the range I've given you,
- 40:51right? So, based on this, you have to
- 40:54print another message here, after a
- 40:56line break inside this inner HTML file,
- 40:58stating whether your value is actually
- 41:00underweight, in the normal range, or
- 41:02overweight. Okay? So, you've done this
- 41:05little assignment. Now you'll need this
- 41:07code as well. Yes, you'll need the
- 41:09entire code. Okay? So let's come back
- 41:12here and go to our Chrome and here this
- 41:16Project 2. Okay, so this Project 2 here
- 41:20, let's add another one. Let's call it
- 41:26Project 2 Solution. I'm writing this
- 41:30triple back text j script so that it's
- 41:32easy for you to copy and paste a little
- 41:34bit easily when you look at the get up
- 41:37there. So here I am, I've pasted it.
- 41:39Look how well it looks. Project
- 41:41Solution Solution One Project Two
- 41:43Solution. Okay? The assignment is also
- 41:46nicely included here. So please take
- 41:48care of that. Let's save it and push
- 41:50the code right in front of you. Here,
- 41:54Project Two Solution, let's commit it
- 42:01and push it too. Okay? It's a bit hot
- 42:06today. So the AC keeps going off and on
- 42:09. So, that's all the work we have.
- 42:11Let's go back to our Safari. So, that's
- 42:14our project. Let's close it. Let's
- 42:16refresh. You know, all the values will
- 42:18disappear. Now let's go back to Home.
- 42:21Okay? We're back to Home. So our BMI
- 42:23calculator is done. Now let's do a
- 42:25simple project. Very simple. You might
- 42:27not have done a project easier than
- 42:28this. But actually, this timing game
- 42:30within JavaScript is quite complex.
- 42:32It's very interesting. We'll talk about
- 42:33this sometime, discuss it. Now let's
- 42:35come to our digital clock. So this is
- 42:37our digital clock. What do we need to
- 42:39do? You need to display the local time
- 42:40here. Okay, sir? It's very easy. So
- 42:42let's dig into our code. Here's three,
- 42:45this digital clock, and this code. Okay
- 42:49, first, the index. So let's read it
- 42:51here. Now, what's inside the index?
- 42:54This is the CSS I wrote. Okay? We have
- 42:56a center div, I've placed a flex box,
- 42:59etc. here. And this is our banner for
- 43:01the local time, and we've given it an
- 43:03ID for the clock, which is displayed in
- 43:04orange. So that's it. Display the time.
- 43:08Okay, sir. So, here's the code. Now,
- 43:10how do we display the time? First, we
- 43:12need something to display the time.
- 43:14Where do I display it? So, what do we
- 43:15do? Take the clock as a reference and
- 43:17display it here. Okay, now how do I get
- 43:19the clock? Let's take it as a reference
- 43:21, document.a.query. Okay? This time
- 43:24we're using something new: Get Element
- 43:25by ID. Okay? Just for fun. Use whatever
- 43:27you want. There's no problem. Okay?
- 43:30We've got it. Get Element by ID. And
- 43:32what about the ID? So, we'll put the
- 43:34clock here. Okay? If anyone's having
- 43:36trouble, what can they do? That
- 43:38document.query.sector. I'm going to
- 43:41explain it to you in different ways
- 43:43because the code you read will be read
- 43:45differently. So, here's the query. What
- 43:48do you do in the query.sector? Nothing,
- 43:50just give the ID. Okay? I've given you
- 43:55the clock. Now, take whatever you want.
- 43:57We can even store it in a variable. So,
- 43:59let's call it clock. Take this. If you
- 44:02don't want one, take this one. Don't
- 44:05take two. Just comment out one. I'll
- 44:08use the first one for you. Right now,
- 44:09both are going to return the same value
- 44:11. We already know that much. Now, it's
- 44:12not a big deal. So, what we need to do
- 44:14now is display the clock. Well, we
- 44:16don't need to display a clock like this
- 44:17. It's a digital clock. So, it should
- 44:19change every second. Look, if you want
- 44:21a clock, you can get it very easily.
- 44:23There's nothing to do with it. What do
- 44:24you need to get a late date or a date?
- 44:26Simply get a date. New Date. Date is a
- 44:29direct object. Okay? We have the date.
- 44:33So, if you want to display this date,
- 44:34what can you do? So, you can go to
- 44:38console. console.log and say," This
- 44:43date has a method called' to local
- 44:47string '. There's also' date string ';
- 44:50we'll use' time string 'and this method
- 44:52. That's it, you're done. So, the
- 44:54problem is that it hasn't appeared here
- 44:56yet. When I open the console, I have
- 44:58the time. Every time I save it, I'll
- 45:02get the time. Okay, every time the page
- 45:05refreshes. "I'll keep getting the
- 45:07updated time every time. But what do I
- 45:09want? This date should run every time.
- 45:11It should run repeatedly, and it should
- 45:13keep updating here. Now, if I knew of a
- 45:15method in JavaScript that would run
- 45:18every moment, or I could control
- 45:20whether I want it to run after 1 second
- 45:22or 2 seconds. If I want it to run every
- 45:24time, how would I do that? Some people
- 45:26will come here very intelligent and say
- 45:28," Set up a cron job. "Hey, what are
- 45:30you talking about? You've become so
- 45:32advanced that you've reached the top of
- 45:34the server that you'll set up a cron
- 45:35job. This is where you fail the
- 45:37interview. Because you're asking for
- 45:39advice, it's a small matter, like a
- 45:40needle. You're drawing your sword. How
- 45:43will that work? So, what do you need to
- 45:45do here? You need to know a simple
- 45:47method that controls JavaScript events.
- 45:49That's your setInterval method. It lets
- 45:52you give me any method and specify the
- 45:54interval after which I'll run it
- 45:56continuously. It'll run for as long as
- 46:00your program runs or as long as the
- 46:02script is open. We'll discuss this
- 46:04further because it causes many
- 46:05JavaScript interviews to fail. Yes,
- 46:07we'll discuss this too, but now you
- 46:09know. So, there are two parameters:
- 46:11this is the function, and this is the
- 46:13function's body, and the second
- 46:14parameter, separated by a comma, is the
- 46:16time. It's in milliseconds. Give it
- 46:181000. For 1 second, give it 2000.
- 46:20Always write it this way. If you don't
- 46:23write it this way, you're sure to make
- 46:25mistakes in syntax, and you won't get
- 46:27code highlighting, which is a feature
- 46:29of many interviews. Yes, there are many
- 46:31interviews where you don't get code
- 46:33highlighting or color highlighting. So,
- 46:36remember to write it this way. What do
- 46:38you have to do? Write this set interval
- 46:39. After that, there are two parameters.
- 46:41First, write the first parameter. This
- 46:42is how we wrote the function. We wrote
- 46:44the body of the function. After that,
- 46:46we put a comma and put 1000 here. If we
- 46:48want to give 2000, then for 2 seconds,
- 46:50as the interviewer is asking you,
- 46:52whatever your task is. Okay, now what
- 46:53do we have to do? We don't have to do
- 46:55anything. This is what we had to do. So
- 46:57, let's cut it and give it to him. Now,
- 47:00these values will come to us every
- 47:03second. So, let's save it and see. So,
- 47:05let's look in the console. Now, see,
- 47:06these values are coming to me every
- 47:08second. 26 27 28 So, that's all we've
- 47:11got. But I never actually had to run it
- 47:13in the console. So let's stop it for 1
- 47:16second. So, I never had to run it in
- 47:18the console. So, let's comment it out.
- 47:20Where did I have to run it? I had to
- 47:22update this document. Its node had to
- 47:24be updated every second. Okay? Let's do
- 47:27that. What's the big deal? So here's
- 47:28the clock. What do you do with it?
- 47:29Inner HTML, inner text, whatever you
- 47:31want, I'll take the inner HTML. What do
- 47:33you do with it? Here's what I wrote
- 47:35above, date.com. There are a lot of
- 47:37methods inside it. So I'll use to
- 47:39localize a timestring. And here we go,
- 47:42that's all I needed to do. So now we
- 47:45have a date that's updating every
- 47:47second. Why is it updating? Because
- 47:49this method is running every second.
- 47:51Okay? So that was a small project, but
- 47:53it was quite interesting. Now let's
- 47:56move on to our bigger project. So let's
- 47:58have some tea first, and then we'll
- 48:00move on to the bigger project. Yes, sir
- 48:03. After a sip of tea, we're here. I
- 48:06haven't given you this code yet. Don't
- 48:07worry. I haven't forgotten. I'll give
- 48:09it to you. Don't worry. So let's copy
- 48:11it and run it on our browser. And here
- 48:14we have another one. Okay, sir? I'll
- 48:17give you another one. This is our
- 48:21Project 3 solution code. Here's the
- 48:25back-tech, and the same JavaScript. Yes
- 48:27, by the way, if you ever need to use
- 48:29it for your own use, instead of
- 48:30JavaScript, if you want to write Python
- 48:32code, you can also write Python code.
- 48:34That code is highlighted here. You just
- 48:35have to enter the language. For example
- 48:37, suppose print. Yes, I write a lot of
- 48:39code in Python. I have to write a lot
- 48:41for college right now. So, Hitesh, take
- 48:43this. Look, Python code is also
- 48:44available, and when you look at the
- 48:46get-up, that code can be copied and
- 48:47pasted. Java and Python are available
- 48:48for everyone. Right now, we're focusing
- 48:49on JavaScript. Sometimes we just feel
- 48:51like giving you a little extra
- 48:52knowledge, so we provide it there. Here
- 48:55you go. Now you have the solution code
- 48:57for Project 3. It was pretty easy, but
- 48:58there was no need to provide it. So,
- 49:00okay? But let's provide it. So, write
- 49:04the Project Solution 3 solution
- 49:08properly. Solution 3 code and then
- 49:12we'll do it. The stage is ready. Let's
- 49:15commit and push it. Here, let's push it
- 49:19. Check it once so I know whether
- 49:22you're getting the code or not. So,
- 49:24here you go, and here you have Solution
- 49:28Three. Okay, sir. That's all done. Now
- 49:31let's move on to our larger project.
- 49:33This project will be a little larger.
- 49:34It will be a little time-consuming. But
- 49:38it's not a complex project. Look, after
- 49:40writing so much code, you must have
- 49:41understood. We add logic one by one.
- 49:44It's not so much that we have a problem
- 49:46. There's a little too much fan. Let's
- 49:49reduce it. Wait a second. Yes, sir. So,
- 49:53in this Project Four, we have a lot of
- 49:56code, but it's not complex. Not complex
- 49:59. We'll just add a very small if and
- 50:01else, and it's done. Yes, we'll have to
- 50:03select a lot of values within the
- 50:04documents. But look, select one value,
- 50:07select two values, or select 20 values.
- 50:09The same method needs to be applied
- 50:10within the document. The same query
- 50:11field. So, it's not a complicated task.
- 50:14It's lengthy, I admit. It will take
- 50:15time. So, okay. What is this? Now,
- 50:17guess the number. It's a very simple
- 50:18game. You might have played it too. So,
- 50:19this is guess the number. What it
- 50:21involves is that you try and guess the
- 50:23number between one and 100. Okay? You
- 50:25have 10 attempts to show the numbers.
- 50:27Okay? And guess the correct number. So,
- 50:29you have to guess the number. Here, you
- 50:31have to enter a number. As soon as you
- 50:32submit, that number will be evaluated.
- 50:35And you have to check whether the
- 50:37number is actually correct or not. So,
- 50:39you'll also have to generate a random
- 50:40number. Okay. And what are we doing
- 50:42with the previous guesses? We're
- 50:44creating an array where we'll store all
- 50:46the user's previous guesses and display
- 50:48them to the user. We'll also need to
- 50:54reduce the remaining guesses. With each
- 50:55click, we'll reduce the number of
- 50:57guesses until you have nine attempts
- 50:59left, five attempts left. So, this is
- 51:01how it works. Okay? So, there's a lot
- 51:03involved, but it's not complicated. I'm
- 51:05saying it again, it's not complicated.
- 51:06Let's study it one by one, because it's
- 51:08a great project. You'll have a lot of
- 51:10fun. So, there's a lot of code. Don't
- 51:12be intimidated by the code. So, look,
- 51:14here we have a form. Okay? And here we
- 51:17have a result. Okay? There's no problem
- 51:19at all. Now, within the form, I know I
- 51:22have a guess field, which says" Guess a
- 51:24number. "Okay? After that, there's an
- 51:26input, which is what? There's a" guess
- 51:28"field, and there's an input there,
- 51:30because I can extract values from there
- 51:31. Okay, I'll extract that too. And then
- 51:33what do I have? Input type" submit. "
- 51:36Now, this input type" submit "is
- 51:38actually a button. Yes, you don't
- 51:41always have to write" submit "for the
- 51:43button in the form. You can also write"
- 51:45submit "for the input. Okay? So, this
- 51:47button looks like this. Now, scenarios
- 51:49like this arise, so here's the scenario
- 51:51. And this div below, we have" previous
- 51:55guesses. "Okay? So, we can easily get
- 51:57those as well, called" guesses. "And we
- 51:59can easily get the last result as well,
- 52:02from within 10. Okay? So, it's not that
- 52:04complicated. So, we can get the values
- 52:06here. Okay? Okay? Now, since we have an
- 52:09ID here, where did I actually go?
- 52:12Here's" submit. "So, you know, we've
- 52:14written this down. So, what exactly is
- 52:16this? This is our major click event.
- 52:18Here, we'll have to listen to the event
- 52:20. Okay? I'll show you some more. You'll
- 52:23get the idea. Okay? So, first of all,
- 52:26our challenge is to generate a random
- 52:28number between 1 and 100. So, how is a
- 52:30random number generated? To generate a
- 52:33random number, we use the Math library
- 52:35and use math.random. Okay, sir. Now,
- 52:37what do we do with math.random? We lock
- 52:40the console so you can keep track of
- 52:42what we're actually doing. What actions
- 52:44are happening here. So, here we go.
- 52:47Let's open the console. Here we go. Now
- 52:50, look, every time I save this, every
- 52:52time I refresh the page, I get a new
- 52:55number. But if you look carefully, what
- 52:57is this number? 0.5484. Okay? So now,
- 53:02since all the values are coming after
- 53:04the decimal, if I want from one to 100,
- 53:07suppose I want up to 10, what do I do
- 53:09to jump from the decimal to here?
- 53:11Nothing, just multiply by 10. Hey,
- 53:13basic mathematics, multiply by 10, and
- 53:15one number jumps over here. Okay? Now,
- 53:18every time you refresh this, one number
- 53:19will jump over here. Okay? Now, if you
- 53:23jumped one number over here, it's like
- 53:24this. Now, if you wanted 100, jump two
- 53:26numbers over here. Because you only
- 53:28need up to 99. Okay? Now, there's one
- 53:30more special thing about this. It will
- 53:32give you all the numbers, but sometimes
- 53:34it might also give you zero, because it
- 53:36will give you 0 to 99. Now, I don't
- 53:37need zero. So what I do is I simply
- 53:39come here and tell it to add 1. So here
- 53:42we go, I added 1. Now it's guaranteed
- 53:44that your number can be 100, but it
- 53:46won't be zero. Okay? So you can refresh
- 53:49as much as you want. Now, see, getting
- 53:51the exact zero isn't possible right now
- 53:52in this randomness. But okay. Not only
- 53:55that, I want this decimal that's coming
- 53:57out, I don't want a decimal. I want an
- 53:59integer. So, here we go, wrap it here,
- 54:02and do the same thing we just did in
- 54:05the last video. And simply tell it to
- 54:08parse to an int. So, when we parse to
- 54:10an int, obviously, you know what value
- 54:12we got? So, this is 91, so we took it
- 54:15from one to 100. Okay? So, more values
- 54:19will come out as well. So, one problem
- 54:21has been solved: how to generate a
- 54:23random number. So, why not write this
- 54:24random number to the console? Let's
- 54:26hold it in a value. So, let's call it
- 54:31simply a cost random number. Here you
- 54:35go. Okay? The random number is there.
- 54:38One problem has been solved. But what's
- 54:40the next problem? We need to take a lot
- 54:42of values in the document. So, the
- 54:44document.query field. In the query
- 54:47field, we just read what we have.
- 54:50Actually, let's open this one too.
- 54:52First, we take this one, which is
- 54:54called submit, because many events will
- 54:57be triggered on it. So, here we go. Now
- 55:00, what do we do with it? Let's call it
- 55:02submit button or submit. We can give it
- 55:06good variable names here. If we don't
- 55:07give it in the HTML, that's fine. Okay?
- 55:09What else do we need to take? We need
- 55:10to take user input as well. So, where
- 55:12will we take user input from? We'll
- 55:14take it from the guess field. Obviously
- 55:15, we need its value. So let's copy and
- 55:19duplicate it. We need the query sector,
- 55:22this guess field. And what is this?
- 55:25User input. User input. Now, obviously,
- 55:30what we need here is that this won't
- 55:33suffice. I need to actually get its
- 55:35value. So, you can get it by dot value
- 55:37here, or you can also get it by letter
- 55:39on. Whatever you like. Ah, for now,
- 55:42I'll keep it here because I'm writing
- 55:43all the sectors. Actually, I'm not
- 55:45passing the exact value because
- 55:47wherever I need the value, I'll write
- 55:49it there by dot value. Now, it's up to
- 55:51you which approach you want to use.
- 55:53I'll use this approach for now. Then
- 55:56we'll see what approach we use next. So
- 56:00, let's move on to more values. Okay?
- 56:03If this seems a little problematic to
- 56:04you, we've just tried the dot value in
- 56:06our project. So, you won't be too
- 56:07bothered. Okay? So let's take more
- 56:09values. Okay? Here we are, we have user
- 56:12input. Now, after the user input, let's
- 56:15move on. Here we take the guess slot.
- 56:20So, we'll take the user's guesses, the
- 56:21previous guesses here. So let's call it
- 56:23that too. So, this one we have with a
- 56:26dot is a class. So, these guesses are
- 56:30called guess slots. Because we'll keep
- 56:34filling these slots. We'll have arrays.
- 56:36So, what else do we need? We also need
- 56:38to show the user's last result, what
- 56:40did you provide here, so we'll actually
- 56:42take that as well. The last result
- 56:46value will come to us from last result.
- 56:49This is also a class. So, okay. Let's
- 56:50take this as well. It's not a problem
- 56:52at all. Let's tell it to take this last
- 56:54result from here. And let's take the
- 56:56same name. Its remaining last slot also
- 56:59means how much remaining is left. The
- 57:03last result. Yes, sir. It's the same.
- 57:06Let's save it. Okay. So, is there a
- 57:09problem here? I think it's because it's
- 57:11not loading. So I think we did
- 57:13something. Let's save the remaining.
- 57:16Let's refresh it once. Yes, sir. There
- 57:18is a problem. Give me a second. Let me
- 57:20just select this and see what's causing
- 57:22the problem. Let's debug it. Yes, sir.
- 57:28So after checking, I had to refresh it.
- 57:31Keep in mind when refreshing, if you
- 57:33haven't saved or locked all the code,
- 57:35it will all be lost. Similarly, this is
- 57:37the sandbox design. This is the
- 57:38playground design. So I selected all of
- 57:40this and copied and pasted it back. So,
- 57:41there are no problems right now. So,
- 57:43honestly, I didn't think there was an
- 57:44issue. But it's okay. Okay, we have
- 57:47some more values here. Let's take those
- 57:50as well. So look, we also have this
- 57:52paragraph. A low and a high. We'll use
- 57:53this as well. So we'll copy the query
- 57:55section of this as well. That way we'll
- 57:58tell the values whether this is a low
- 57:59value or a high value. So we'll take
- 58:02this and call it low and high. Low and
- 58:07high. Okay? That's it. Okay, we'll take
- 58:10one more thing here. Where is it
- 58:15actually? Here it is. These result
- 58:17paragraphs, we'll actually show all
- 58:19these values to the user only when we
- 58:21have, or what if we'll probably hide
- 58:24these values here. We'll do all this.
- 58:26So we're going to do all this work now.
- 58:28If the user has taken all the guesses,
- 58:29let's say, then we won't show them the
- 58:31previous guesses, the remaining guesses
- 58:33, etc. We'll tell them to start over.
- 58:34So let's hold on to this for once. Then
- 58:36we'll discuss what to do with it. So
- 58:39here we go, and this is the class we
- 58:42created. Okay. Here we have the class.
- 58:45And we'll hold this in a variable for
- 58:49the start-over. Okay? We'll provide the
- 58:51start-over messages here. Okay. And
- 58:54we'll insert some values. So, actually,
- 58:56we'll take that too. Let's take a
- 58:59paragraph because we're going to inject
- 59:01it in several places later. Then I'll
- 59:03tell you the strategy we're following.
- 59:05document.createElement. Which element
- 59:08do we create? Nothing, just create a
- 59:10paragraph. Okay. Now let's talk about
- 59:12the strategy: what exactly do we need?
- 59:16So, let's take those variables. So,
- 59:19first of all, we'll need a variable.
- 59:21What will it be? It will be an array.
- 59:22Okay? Why would it be an array? Because
- 59:24what will we do inside it? Whatever
- 59:26values the user submits, we'll store
- 59:28those values and show the entire array
- 59:31to the user so they know not to guess
- 59:34the same guest value again. Okay? For
- 59:36that, we've taken one. So, what we'll
- 59:38need to track next is how many attempts
- 59:41they've made. So, their number of
- 59:43guesses, or let's call it num-guesses.
- 59:46Number of guesses. We'll take that too.
- 59:48We'll start with one, meaning this is
- 59:49your first guess. Okay? We'll start
- 59:51with one, and as soon as it reaches 10,
- 59:53we'll block them, saying they can't
- 59:55submit anymore, so we'll disable the
- 59:57submit. Okay? We'll add a property to
- 59:59that. Okay? And we'll also take a
- 1:00:02variable here called let's play the
- 1:00:05game. And what will we set to play the
- 1:00:07game? true. Well, whenever you design
- 1:00:09games like this, there's always a
- 1:00:11variable called" playGame "that allows
- 1:00:13you to play the game. Now, suppose your
- 1:00:15events have completed, your coins have
- 1:00:17been depleted, or something like that.
- 1:00:19Nothing progresses without checking
- 1:00:21this condition. This is how games are
- 1:00:23designed. Okay? So, that's our basic
- 1:00:26idea: we need these values, and that's
- 1:00:28all we had to do, was collect and
- 1:00:29prepare things. We haven't written any
- 1:00:35of the code's logic yet. Okay? So now
- 1:00:36we'll write a few functions and define
- 1:00:39how they work. So, the first function I
- 1:00:43need is" validateGuess. "So, what's
- 1:00:46inside this" validateGuess "function?
- 1:00:48Give me a guess, and I'll validate it.
- 1:00:51Okay? So, why is this there? This is
- 1:00:53because I first need to see if he
- 1:00:55guessed a valid number or not? What if
- 1:00:57he guessed A B C? Obviously. Next,
- 1:00:59we'll check if the value is less than
- 1:01:02one. Is he giving negative values? Or
- 1:01:05because we created the game from one to
- 1:01:07100. If he's giving a value of 500,
- 1:01:08that's still not right. So, we'll do
- 1:01:10all this validation of the guesses
- 1:01:12within this method. Okay? Okay, sir? So
- 1:01:15, this work is done within this method.
- 1:01:18Now, what do we do next? After that, we
- 1:01:22need another method called" CheckGuess.
- 1:01:26"Here you go. Okay, you'll have to
- 1:01:28enter your guesses within this"
- 1:01:29CheckGuess "as well. Now you'll say,"
- 1:01:30Well, it's validated. Why do we need to
- 1:01:32check it now? "Look, the validation has
- 1:01:35been done, but from there you only get
- 1:01:36validation that the values are above or
- 1:01:38below. We haven't been able to get it
- 1:01:40to print any message. So what do we do?
- 1:01:42We want to print a message to confirm
- 1:01:44whether your value or the guest value
- 1:01:46is correct, low, or high. So, the check
- 1:01:49will happen here. Validation and check
- 1:01:51are almost the same. You can give it
- 1:01:53any other name if you want. This name
- 1:01:54seems fine to me right now. So, what
- 1:01:57else can we do? Because you're printing
- 1:01:58that message in check guesses. So, if
- 1:02:00you want, you can do it directly within
- 1:02:01that. But what will I do? I'll create
- 1:02:04another method here, which I'll call
- 1:02:07displayMessage. So what will happen is
- 1:02:11that as soon as there's a display
- 1:02:12message in this method, you just pass a
- 1:02:14message or your guess inside it, and
- 1:02:16I'll send you the message directly.
- 1:02:18What will this method do? It will
- 1:02:20interact directly with the DOM. Okay?
- 1:02:22So, we'll display the message. It's
- 1:02:23very easy. We won't do anything else.
- 1:02:25We'll empty the user's input value
- 1:02:28again. Because obviously, he needs to
- 1:02:30guess next. And what else will we do
- 1:02:32with that? We'll add our guess to our
- 1:02:35inner HTML. Okay? What else will we do?
- 1:02:38We'll reduce your numbers. These
- 1:02:41numbers mean all the manipulation of
- 1:02:43the HTML DOM will be done within the
- 1:02:44display message. Okay? So, this is our
- 1:02:48display message. Okay? Okay, there's no
- 1:02:50guess inside it. We'll actually take a
- 1:02:52message from the user. Okay? Did I put
- 1:02:55too many yeses? I put too many. Okay?
- 1:02:59So, we'll take a message for the user
- 1:03:01system and print whatever the message
- 1:03:03is there. Okay, okay. That's enough
- 1:03:07display messages, so let's create
- 1:03:09another method that will do all the
- 1:03:10rest. We'll tell it to display the
- 1:03:12guess. We'll give it the display guess
- 1:03:18method. And here we'll see there are
- 1:03:20more methods like this. Okay? So, let's
- 1:03:22summarize for now. In case you're a
- 1:03:24little confused. What's the validate/
- 1:03:26guess method supposed to do? It won't
- 1:03:28do much. It simply guesses and
- 1:03:30validates whether a value is between 1
- 1:03:32and 100, and whether the value actually
- 1:03:35exists. We'll use all of these methods
- 1:03:37here, but that's the main role for now.
- 1:03:39After that, we'll have a check/guess
- 1:03:40method. The check/guess method will
- 1:03:43check if the value is equal to the
- 1:03:45random number. If it's equal, use this
- 1:03:47display message to tell the player
- 1:03:49you've won the game. If it's low, say
- 1:03:51it's low. If it's high, say it's high.
- 1:03:53And what happens within display/guess
- 1:03:55is that this display/guess method won't
- 1:03:56do anything. It will clean up your
- 1:03:59values. Okay? Because you need to input
- 1:04:01the next value as well. You don't need
- 1:04:02to take the same value. What else will
- 1:04:04it do besides update the arrays
- 1:04:06containing your guesses. It will also
- 1:04:08update your remaining guesses. And what
- 1:04:10will the display message do? There
- 1:04:11won't be much here. In fact, we've
- 1:04:12taken the low and high values here.
- 1:04:14We'll pass a message into this query
- 1:04:16field and print that message as it is.
- 1:04:19Okay? You've got it. Now we need a
- 1:04:22method to determine what we want to do.
- 1:04:24We want to end the game and start a new
- 1:04:26one. Okay? So, here's the function.
- 1:04:29What do you do first? Here's the new
- 1:04:31game. We've got a new game. We've
- 1:04:33started this new game. And here's a
- 1:04:36function. This is called end game. So,
- 1:04:39you've ended the game. Okay, sir.
- 1:04:42That's a lot of methods. I told you
- 1:04:43projects are a bit complex. And you
- 1:04:45also wanted some longer videos. So,
- 1:04:47okay. Okay, now we're done. This new
- 1:04:50game, end game, these are very easy
- 1:04:52methods. You can place it anywhere you
- 1:04:54want. Let's do one thing: first, we'll
- 1:04:56place the end game so you can
- 1:04:57understand the flow easily. In fact, it
- 1:04:59doesn't matter where you place the
- 1:05:00method. But for the sake of
- 1:05:01understanding, we can do the end game
- 1:05:03and then start a new game. Okay? Now
- 1:05:06let's talk about how these methods are
- 1:05:08designed. First, check if you're even
- 1:05:11available to play the game. Okay? How
- 1:05:14do you check that? First, we'll check
- 1:05:16the if condition. Here you have your if
- 1:05:18method. Now, what's inside it? If it's
- 1:05:20play, it will automatically evaluate to
- 1:05:22true or false. If it is, play the game,
- 1:05:24otherwise don't. So, if you can play
- 1:05:27the game, what do you do? So, take this
- 1:05:30submit we've taken. Remember this
- 1:05:32submit here? I took this submit. So, I
- 1:05:34need to listen to it. So, I need to add
- 1:05:37an event listener here. Do I need to
- 1:05:40add an event listener? It's very easy.
- 1:05:42I need to add a click. And here's your
- 1:05:45function. And here it is. Okay? Now,
- 1:05:47inside this function, you know that you
- 1:05:49get an event. Yes, we discussed it, so
- 1:05:52here you have the event here. Okay? Now
- 1:05:54, if you get an event, this is a form,
- 1:05:56so what do you do with the form? The
- 1:05:58values will be stored on the server, or
- 1:05:59wherever someone is calling it. So, the
- 1:06:01first thing you need to do is prevent
- 1:06:03the event by default, saying," No, no,
- 1:06:04don't take the values anywhere. Stop it
- 1:06:05here, I'll take them here. "Okay? Now,
- 1:06:08the second thing you need is this guess
- 1:06:10number. Now, when we designed our
- 1:06:14sectors, we took user input. We didn't
- 1:06:17actually take that user input from the
- 1:06:18user, right? We just selected it. So,
- 1:06:22now we have to select its values. So,
- 1:06:23it's very simple. What can we do? You
- 1:06:25can write the user input dot value
- 1:06:26there if you want. There's no problem
- 1:06:28at all. But this value also needs to be
- 1:06:30parsed as an int. Yes, you probably
- 1:06:33forgot. Here, I've parsed it as an int.
- 1:06:36Now what do you do with this value?
- 1:06:37Hold it in a variable. Here. Here, I've
- 1:06:42held it. Okay, is the game complete yet
- 1:06:44? Look, I don't have to do anything. I
- 1:06:46just have to pass it as it is to the
- 1:06:48next one. Here, validate the guess. And
- 1:06:51here, I've passed you the number. That
- 1:06:53was all I had to do. Design in small
- 1:06:55functions. So, my work is done. So,
- 1:06:58this play if condition is done. Look, I
- 1:07:00checked that the user was allowed to
- 1:07:02play the game. After that, he entered a
- 1:07:05number. I picked the number from here
- 1:07:07and displayed it here. And if you want,
- 1:07:10I can also lock the console to see if
- 1:07:12the number is coming in correctly. So,
- 1:07:15I've written this guess inside the
- 1:07:16console lock. Okay? So let's try this
- 1:07:18once and see if it's working. Suppose I
- 1:07:21typed six here. I pressed submit and
- 1:07:24guessed six. But obviously, I'm not
- 1:07:25checking it right now. I've already
- 1:07:28taught you how to select if it's empty
- 1:07:30or not a number. I've already seen all
- 1:07:31of that. So, repeating the same thing
- 1:07:3310 times doesn't make any sense. So,
- 1:07:36right now, I'm guessing whatever
- 1:07:36numbers are there. So, my event
- 1:07:39listener method is working fine for me.
- 1:07:40Okay? That's all clear. Now, within
- 1:07:42this validate guess, we'll check
- 1:07:44whether the number is actually entered
- 1:07:46correctly. You could have checked here
- 1:07:48as well if you wanted. But since a
- 1:07:49validate guess method has already been
- 1:07:51described, let's apply validation. Look
- 1:07:52, let me tell you one more thing.
- 1:07:54You're going to write about these
- 1:07:55validation methods a lot. Because what
- 1:07:57happens? When you write about the
- 1:07:58backend of any application, suppose a
- 1:08:00username or an email address appears.
- 1:08:02There are also specially created
- 1:08:04methods to validate whether the email
- 1:08:06contains an@, or whether there's a word
- 1:08:09after the@? Is it already in the
- 1:08:12database? Is the username a valid email
- 1:08:13address? Is it a unique email address?
- 1:08:15So, you'll be doing a lot of such
- 1:08:16validations. Okay, so now we'll check.
- 1:08:19So, first, let's check by applying an'
- 1:08:20if condition '. Here you go. So, what's
- 1:08:21inside the' if condition '? I told you,
- 1:08:23' is not a number 'is a method nowadays
- 1:08:25that checks whether it's a number or
- 1:08:27not. So, since you've already got the'
- 1:08:29guess 'here, here you go, you've got
- 1:08:31the' guess 'and we'll set an alert.
- 1:08:33Although I prefer that only a text
- 1:08:34message goes there, okay, I don't want
- 1:08:36to proceed further and let's make it a
- 1:08:38little easier. So, let's give him an
- 1:08:40alert message here: Please add and
- 1:08:41please enter a valid number. Now, we're
- 1:08:48going to use this method, this message,
- 1:08:50in many places. Okay? What else would
- 1:08:53you like to check? Let's do one more
- 1:08:55check. Let's check one more time: is
- 1:08:58our guess less than one? What if it's a
- 1:09:04one-digit number? Copy this math here.
- 1:09:06Okay, sir. There you go. That's it.
- 1:09:09Okay, what can we do now? Duplicate it.
- 1:09:12Here, copy it and paste it. What do you
- 1:09:15want to check now? Is it greater than
- 1:09:16100? Obviously, you'll check this too.
- 1:09:18If it's greater than 100, we'll do it.
- 1:09:20Please, we need a number. Please enter
- 1:09:24a number. Let's send a good message.
- 1:09:26Let's say less than 100 and write here,
- 1:09:32please add, please enter a number a
- 1:09:36more than one. Okay? It was very basic.
- 1:09:40Okay, sir. That's it. So, if you've
- 1:09:42checked all this, what would you like
- 1:09:44to do next? So, you'll be in the else
- 1:09:45case. Obviously, what would you like
- 1:09:48first in the else case? First, I'd like
- 1:09:50to see your number somewhere. After
- 1:09:54that, let's push it into the array.
- 1:09:57Okay? We gave you previous guesses. So,
- 1:10:00let's push it inside the previous
- 1:10:01guesses. So, we have previous guesses.
- 1:10:05How do we push it inside the array?
- 1:10:06Here, dot push. Here, and your guess is
- 1:10:09done. Since he's still playing, you can
- 1:10:11add his number. Well, at least you've
- 1:10:14done the push. Now, that might be his
- 1:10:16last attempt. This might just be his
- 1:10:1910th attempt. I don't want to let him
- 1:10:21do it after that. So, after validation,
- 1:10:23I'll first have to check if his game is
- 1:10:27over. So, how can I check if this
- 1:10:30number is over? If you remember, we
- 1:10:34used the number of passes," Number of
- 1:10:35Gags. "I can check it through this. Yes
- 1:10:37, of course, we can. If this number of
- 1:10:40passes is incremented, because I've put
- 1:10:42it in the global scope. Remember, we're
- 1:10:44using scoping. I've put it in the
- 1:10:46global scope. So, if his number of
- 1:10:48passes is his 11th, we don't have to do
- 1:10:51anything. You can also check for less
- 1:10:54than or greater than 10. Greater than
- 1:10:56equal to 10, whatever you want. It's
- 1:10:58the same thing. Okay? So what do we do?
- 1:11:01So, if it's equal to that, we'll have a
- 1:11:04message. This display message first
- 1:11:07makes the display guess once, ensuring
- 1:11:09that whatever it displayed is actually
- 1:11:11in its possession. So, we'll give the
- 1:11:13display guess method. Now, how it
- 1:11:15displays it isn't my concern right now.
- 1:11:17Just get it displayed. After that,
- 1:11:19we'll also get a display message. The
- 1:11:20display message needs a message. So,
- 1:11:22okay, yes. Let's give it a message as
- 1:11:23well. So, we send the display message a
- 1:11:25message with a back text saying," Game
- 1:11:27Over. "Now, how it displays it is its
- 1:11:30concern. Okay? And we'll also tell the
- 1:11:32user the random number, because the
- 1:11:34game is over now. Okay? So, let's tell
- 1:11:37it. The random number was already there
- 1:11:39, because it was already over. So, here
- 1:11:44it is, and since it's in the global
- 1:11:45scope, I can access the random number.
- 1:11:46So, let's take this and move it aside
- 1:11:48so you can easily see it. Okay? If it
- 1:11:50was over, then we've checked another
- 1:11:53condition: that we pushed that number.
- 1:11:55Now his game is over, so display the
- 1:11:57message to him and make him do this too
- 1:11:59. What else can you do? End the game.
- 1:12:02So, here's the end game. I don't know
- 1:12:04how to end it. Not end, but end game.
- 1:12:08End game, yes, here's the end game. So,
- 1:12:10we've called end game. We'll see how it
- 1:12:12ends later, we don't have that headache
- 1:12:14right now. Okay, sir, that's enough
- 1:12:16work. But if you're considering the
- 1:12:18else case, meaning it's not at 11 yet,
- 1:12:21or equal to 11, what can we do in this
- 1:12:23else case? Obviously, now that it's
- 1:12:25here, it's less than 11, because we're
- 1:12:26not allowing it to be greater, we've
- 1:12:28already checked that. If you want, you
- 1:12:30can also check the range base to see if
- 1:12:32it's between 1 and 10. You can also use
- 1:12:33that, it's not a bad thing in this
- 1:12:35logic. So, what should we do first? It
- 1:12:37needs to display a guess. It needs to
- 1:12:40display the guess under all
- 1:12:40circumstances. It needs to be told that
- 1:12:43you set this number and it's in your
- 1:12:44guess list. Okay? So what do we do here
- 1:12:47? Just give the guess. Look, the number
- 1:12:48is already going into the guess list,
- 1:12:50into an array within the list. But it
- 1:12:52also needs to be displayed. Okay?
- 1:12:54That's done. Now, what do we do next?
- 1:12:55We need to check whether your guess was
- 1:12:58correct. So what do we do? We pass that
- 1:13:01method to the check-guess method. Now,
- 1:13:03we won't use this display message right
- 1:13:06now, because we were sure it would
- 1:13:08display a message. But if we get into
- 1:13:10the check-guess method, we don't need
- 1:13:12to display the message right now. What
- 1:13:14we need to do is display a message
- 1:13:16inside the check-guess method, whether
- 1:13:18the value you entered is actually
- 1:13:19higher or lower. So, we'll decide that
- 1:13:21there. Okay? So that's it for our
- 1:13:24validated guesses. Okay, sir. Now let's
- 1:13:26go to check guess. What do we do inside
- 1:13:28check guess? Actually, we need to check
- 1:13:30whether it's equal to the random value,
- 1:13:32lower, or higher. Based on that, we're
- 1:13:33going to display messages. Okay, sir?
- 1:13:36So let's check. Let's enter our if else
- 1:13:38case. So, here we have our if else.
- 1:13:40Okay, sir? Now what do we do inside if?
- 1:13:42First, what do we do? Inside if, we
- 1:13:48check the guess. Yes, sir. You're
- 1:13:50giving us the guess. So, is that guess
- 1:13:52equal to our random number? Okay? If
- 1:13:55it's equal to the random number, then
- 1:13:56fine. That's it. It's the end of the
- 1:13:58game. You guessed it all in one go. So
- 1:14:01what do we do in that case? Call
- 1:14:02display message and give it a message.
- 1:14:06What message will it give saying," Did
- 1:14:09you guess it right? "You guessed it
- 1:14:12right? Okay, sir. You guessed it right.
- 1:14:15And then play the end game, because now
- 1:14:18he's understood, so what more can we do
- 1:14:20? Okay? So play the end game. Okay.
- 1:14:23What if it's not equal to the random
- 1:14:25number? It's smaller than that. Okay,
- 1:14:27sir. So let's check that too. If the
- 1:14:30guess you gave us is less than the
- 1:14:32random number, we'll still have to
- 1:14:34display a message. So let's take this
- 1:14:36display message. Copy and paste it. So
- 1:14:40what should we call it? Number is low.
- 1:14:43So what will we actually do? Instead of
- 1:14:45saying" Number is low, "we'll say"
- 1:14:48Number is too low. "We'll also create a
- 1:14:50little confusion in the game. We won't
- 1:14:51make it that easy. Just write" low "as
- 1:14:53soon as it's less. Say" too low "so he
- 1:14:55can try again. Okay. Okay, that's it.
- 1:14:59Let's copy and paste it back. If it's
- 1:15:02greater than the random number, then"
- 1:15:06Number is too high. "Okay, sir. So, our
- 1:15:10check validation is complete, and the
- 1:15:12check guess is complete. Okay? And
- 1:15:14we've also printed the message. Okay,
- 1:15:16okay. Now, the actual thing we need to
- 1:15:19do is display guess. Now, look, we've
- 1:15:21used this display message in many
- 1:15:23places, but what exactly is this
- 1:15:24display guess doing? How do we want it
- 1:15:26to display to the user? So, look, the
- 1:15:28first thing we need to do within the
- 1:15:30display guess is what will we do with
- 1:15:32this display guess? We'll update the
- 1:15:34value of the user input we've taken.
- 1:15:37Now, what will we update it with? With
- 1:15:39an empty string, so once you've
- 1:15:41submitted the value, something is
- 1:15:42definitely happening to the value after
- 1:15:44submission. So, look, display guess is
- 1:15:45running here as well. Display guess is
- 1:15:47running here as well. So, we actually
- 1:15:48need to clear the value. So, we have to
- 1:15:50do this. So, we can also call it a
- 1:15:52cleanup. We could also call this the
- 1:15:53cleanup method instead of displaying
- 1:15:54the guesses. Okay? That's done. But
- 1:15:57what do we do now? We need to add an
- 1:16:01inner HTML to this guess slot that we
- 1:16:03took. Now, if you remember this guess
- 1:16:07slot that we took, actually, just a
- 1:16:11second. Actually, we need to look here
- 1:16:13first. Let's show you this guess slot
- 1:16:15that we took a second. What is this?
- 1:16:18These are our gases. Okay? Now, where
- 1:16:20are these gases coming from? These are
- 1:16:22our gases. So, we want to add our inner
- 1:16:25HTML to this. So, we've selected this
- 1:16:27span that we took. Now, I want to add
- 1:16:29some values to this span. Okay? That's
- 1:16:32what I want to do. So, let's come here.
- 1:16:34So, I took this guess slot. I took its
- 1:16:35inner HTML. So, what do we do inside
- 1:16:38this inner HTML? We'll keep adding. So,
- 1:16:40plus equals, what do we need to add?
- 1:16:43What are we going to do? We're going to
- 1:16:44add a variable. So, take this dollar,
- 1:16:47and inside it, let's take these values
- 1:16:52and call it gas. So, whatever gas
- 1:16:54values are there, it's not updating
- 1:16:56them. It's just pushing them. Okay?
- 1:16:59What else do we need to do? Besides,
- 1:17:00we're already here. So, let's add our
- 1:17:03number of gases to the number of gases.
- 1:17:06This has to increase, right? Because if
- 1:17:08you look carefully, this number of
- 1:17:10gases that we have, we started at one.
- 1:17:13But it should increase. When will it
- 1:17:15increase? When you're cleaning up the
- 1:17:17value that the user has given, its
- 1:17:18value is gone, so clean up the value
- 1:17:20and show that value here, add that
- 1:17:22value, update it. But there's one more
- 1:17:25thing to do. We change the values of
- 1:17:26these remaining gases as well. Now, how
- 1:17:28have we done renaming remaining gases?
- 1:17:31So look, here we've written the
- 1:17:32remaining gases as a separate text. But
- 1:17:34inside this span, we've given the last
- 1:17:36result here the value of 10. So
- 1:17:38updating this is very easy. Once a
- 1:17:40value has been updated, what do we do
- 1:17:42simply? Take this remaining. What do we
- 1:17:44do inside it? Let's take this inner h
- 1:17:46TM l and calculate the values inside
- 1:17:48this inner h TM l. So with the back
- 1:17:50text. Okay, let's calculate the values
- 1:17:52each time. Okay, let's calculate them
- 1:17:53right here. So JavaScript allows us to
- 1:17:55do that. So let's do that. Here's the
- 1:17:57dollar sign. So, how do we calculate
- 1:17:59this? Let's subtract all your gases
- 1:18:03from 11. So, wet gases are obvious. Now
- 1:18:07, you don't have to go from 11 to 0, so
- 1:18:10what we'll do is subtract 11. Once
- 1:18:12you've taken one, you're 10, so now
- 1:18:14it's nine. We'll allow one value
- 1:18:16directly, then you can also multiply it
- 1:18:18by 10, as you see fit. 11 seems fine to
- 1:18:20me, so if you're confused, just use 10.
- 1:18:23It's no big deal. We'll check this
- 1:18:25further. I'll put 11 in now, so 11
- 1:18:28minus the wet gases, whatever value
- 1:18:30comes out will be printed here. Okay?
- 1:18:33That's it. If you want, you can also
- 1:18:35call it' Clean Up Gas 'instead of'
- 1:18:36Display Gas '. You might find that more
- 1:18:38comfortable. So that's all done. Now
- 1:18:40let's move on to our new method. Okay,
- 1:18:42sir? What's the new method? This is the
- 1:18:44display message. So, what's the display
- 1:18:46message? If you remember, we took a
- 1:18:48value here, low and high. What was it
- 1:18:50doing? This low and high are simply a
- 1:18:52paragraph with nothing inside. So,
- 1:18:54simply, you need to print the message
- 1:18:55inside this, and the message is already
- 1:18:57being passed to you here. It's very
- 1:18:59easy. What do you do? Take this, and
- 1:19:01then take this, and say we want to add
- 1:19:03a value to your inner HTML. What value
- 1:19:05do you want to add? So, take this. Now,
- 1:19:07what do you do inside this? Take the
- 1:19:09background text and an H2 or H3,
- 1:19:11whichever you prefer. Let's take an H3.
- 1:19:15Let's take an H2. Let's take an H2.
- 1:19:19Take this, and then print the values
- 1:19:20inside this. So, what value do you want
- 1:19:21to print? Everyone has already created
- 1:19:22their message well. So, pass the
- 1:19:24message as it is. There you go. You're
- 1:19:27done. Okay? Now comes the end game and
- 1:19:29the new game. So, we'll have to do the
- 1:19:31end game as well. So, let's try this
- 1:19:33once. Okay? So, hopefully, we've
- 1:19:35refreshed it. So, suppose I enter three
- 1:19:38here. Submit, guess. So it's saying the
- 1:19:40number is too low, and our input has
- 1:19:42been cleaned up. If I enter it here,
- 1:19:45say, 99, and submit it, it's checking
- 1:19:47it's 99 again. What's happening now?
- 1:19:49Actually, there's a slight problem here
- 1:19:51. So, you entered three first, then 99.
- 1:19:53So, there's actually no spaces between
- 1:19:55them. Okay? So, we've actually got this
- 1:19:59plus equals guess under display guess
- 1:20:01here, and we'll have to adjust it a
- 1:20:04little bit. So what do we do here?
- 1:20:06We'll add a little spacing after the
- 1:20:08plus equals guess. You can also add a
- 1:20:09comma if you want. But you'll do this
- 1:20:11much debugging anyway. Okay? So, let's
- 1:20:13try it again. Let's refresh it once. So
- 1:20:15, let's enter three once and submit. So
- 1:20:18, this three appears here. Now let's
- 1:20:20give it 66. It's submitted. So, look,
- 1:20:22now there's a little spacing. If you
- 1:20:24don't like it, you can add commas or
- 1:20:26anything else. I'm just using spaces
- 1:20:28now because they seem to fit. If you
- 1:20:30want, add more spaces. Okay? It'll be
- 1:20:33easy. I've added four, and then I've
- 1:20:35added 33, so this 33 is also coming up,
- 1:20:38and I've added 22, and this is also
- 1:20:40coming up. So, look, you can see the
- 1:20:42entire list here. If you want to do
- 1:20:44something else within this and within
- 1:20:46the inner HTML, display it in a
- 1:20:47paragraph, or display it with commas,
- 1:20:48you can do that too. It's not a big
- 1:20:50deal. Because plus equals, it will
- 1:20:52never change. If you don't like it, you
- 1:20:56want to do this with it, so save it and
- 1:20:58try it too. I've added five. I submit
- 1:21:00it and it shows five. Then I add six.
- 1:21:03This is what you want. You want comma
- 1:21:06values. I like commas better. It's up
- 1:21:08to you. Look, these are small things.
- 1:21:10There's no need to take up so much load
- 1:21:12. Okay? So that's the basics. Okay. Now
- 1:21:16let's focus on the end game and the new
- 1:21:17game, because the user will keep
- 1:21:18playing. So, we'll have to do the end
- 1:21:20game, right? You already wrote here,"
- 1:21:22Make it an end game, "but we've never
- 1:21:24done an end game. So, let's discuss the
- 1:21:26end game as well, how will the end game
- 1:21:28be? So, what will it take to do the end
- 1:21:30game? First, you'll have to create a
- 1:21:33button. Okay? Now, what is a button?
- 1:21:35We'll have a button. Come here a second
- 1:21:39. So what will we do? Let's create a
- 1:21:42new button here. Right now, it's just"
- 1:21:44Submit. "But actually, we'll need a new
- 1:21:46button on which we can create all of
- 1:21:48this. If we're doing the end game,
- 1:21:49we'll also have to give it the option
- 1:21:50for a new game. So, let's do that first
- 1:21:52. Yes, sir. So, what do we do now?
- 1:21:56First, let's focus on our new game. On
- 1:21:58the end game, sorry, not on the new
- 1:22:00game. So, how do you do the endgame? So
- 1:22:02, the first thing you should do is
- 1:22:05clear all the input fields. If you're
- 1:22:07doing the endgame, you'll have to clear
- 1:22:08them. So, take the value in this user
- 1:22:11input, and this is the value cleared.
- 1:22:15That's it. There wasn't much difficulty
- 1:22:16in this. Okay? Okay, the value is
- 1:22:18cleared. Now, what should you do to
- 1:22:20prevent users from adding more values
- 1:22:23to this user input? So, how do you do
- 1:22:25that? So, what should you do with this
- 1:22:28user input? Set an attribute. So, we
- 1:22:31know how to set attributes. Here, set
- 1:22:32attribute, and we've discussed a lot
- 1:22:34about set attribute. Which attribute
- 1:22:36would you like to set within it? I'll
- 1:22:38set only one attribute within it, which
- 1:22:41will be disabled. Now, you can't set
- 1:22:44disabled alone. Actually, key values
- 1:22:47are set in pairs. Normally, if you type
- 1:22:49a disabled word in the input, it
- 1:22:51becomes disabled. But this is where you
- 1:22:52might run into problems. So what should
- 1:22:54we do? Don't add anything to that
- 1:22:56second parameter. So, it's a key-value
- 1:22:58pair. You're done. Okay? That's it. So,
- 1:23:02this is our endgame. Now, we need to
- 1:23:05add a start button as well. So what
- 1:23:08should we do? Let's add a class' List '
- 1:23:11to that paragraph we took at the
- 1:23:13beginning. classList.add. Okay? What
- 1:23:18class' List 'are we adding inside it?
- 1:23:20Look, that class has no use. So, let's
- 1:23:22add a button inside it. Okay? Then it
- 1:23:25will start behaving like a button.
- 1:23:27Because you've written the classes in
- 1:23:28such a way that they behave like
- 1:23:29buttons. Okay? That hasn't worked yet.
- 1:23:31Now, we need to add an inner h TM l
- 1:23:33inside that paragraph as well. Okay? So
- 1:23:36take a paragraph and tell it to add
- 1:23:39inner HTML. Okay? What inner HTML
- 1:23:42should you add? You can add a paragraph
- 1:23:45, a div, a span. Whatever you add, it's
- 1:23:48not that big of a deal. So, what I want
- 1:23:51to do inside it is select an H1. Here,
- 1:23:54let's select an H1 or H2. The H1 will
- 1:23:57be quite large. Here, here, I've taken
- 1:24:00the H2. Now what do I need to do inside
- 1:24:03this H2? Let me add an ID. Here, whose
- 1:24:08ID should I use? New Game. Because
- 1:24:10we'll use the same method for this
- 1:24:12button in New Game as well. We don't
- 1:24:14need to add four buttons. Okay? Let's
- 1:24:16tell it to start new game. Since End
- 1:24:18Game is done, it should get this button
- 1:24:20as well. Right now, all these buttons
- 1:24:21are just hanging in the air. They don't
- 1:24:23have much significance right now. Okay?
- 1:24:25Inside that paragraph, you added a
- 1:24:28button and an inner HTML, okay? So,
- 1:24:31that's it. Okay, sir. We're done with
- 1:24:34that. Now, what do we do? If you
- 1:24:35remember, we took a startover. Remember
- 1:24:39this startover, which was the result
- 1:24:40paragraph? So, we've taken the entire
- 1:24:44result paragraph. Now, what do we do
- 1:24:47inside this result paragraph? We'll
- 1:24:48change it again within its inner HTML.
- 1:24:50Actually, we'll append it. At first, I
- 1:24:52was thinking of removing all the values
- 1:24:54and doing that. But then I thought,
- 1:24:56let's do something. Let's use
- 1:24:58appendChild to add a new child inside
- 1:25:00it. Okay? Which new child should we add
- 1:25:02? Here, we've added the paragraph. Okay
- 1:25:05, sir? Now the paragraph is easily
- 1:25:06available inside it. Remember, the
- 1:25:08paragraph was in global scope, so this
- 1:25:09is the paragraph. So, we created an
- 1:25:11element, but nothing was placed inside
- 1:25:12it. Coming here, we placed it inside,
- 1:25:15adding a class, a button, and a new
- 1:25:17game ID. We also added it. Okay, sir.
- 1:25:20That's all done. What do we do now?
- 1:25:22This is the end game. And tell it to
- 1:25:25start the new game after this. Okay,
- 1:25:29fine. Now when will the new game start?
- 1:25:32What do we do now? Okay, we forgot one
- 1:25:33more thing here. Remember, nothing will
- 1:25:35happen without this. This play game
- 1:25:37also needs to be changed, because this
- 1:25:39is what actually ends the game. So what
- 1:25:41do we do with play game? Make it false.
- 1:25:43Yes, sir. We forgot this. It was very
- 1:25:45important. Okay? Now, whatever work is
- 1:25:47left, run the new method. See how it
- 1:25:49runs the new method. We're not worried
- 1:25:51about this. Okay, sir. Now let's save
- 1:25:53it. What do we do for once? Let's enter
- 1:25:56the values here. Hopefully, we won't
- 1:25:58guess the values. So we'll only give
- 1:25:59small values. Submit this two. Submit
- 1:26:02two again. Submit three. Submit four.
- 1:26:07Five. So 1 2 3 4, that's five values.
- 1:26:09Let's quickly give five more. Submit
- 1:26:11eight. Submit seven. Here, let's give
- 1:26:14seven as well. 1 2 3 4 5 6 7 8, so
- 1:26:17let's give nine. And here, 10. Submit
- 1:26:22this guess. Now your remaining guess is
- 1:26:24zero. So zero means one, so let's give
- 1:26:26one. Submit. So see what happened now?
- 1:26:28Now I can't click here. It's not here.
- 1:26:30All the values have come to you. The
- 1:26:32remaining guess has reached -1. So
- 1:26:34actually, I should have stopped at zero
- 1:26:35. But that's fine. You're also getting
- 1:26:37random values like it was 22 and start
- 1:26:39a new game. Now how do I start a new
- 1:26:41game? It's not possible to click here.
- 1:26:43So here's the click event listener. Now
- 1:26:44, you don't have to create an event
- 1:26:45listener just on the button. You can
- 1:26:47also add it to the H1. Event listeners.
- 1:26:49So, the event listener. You have to do
- 1:26:50the New Game thing. So far, our game is
- 1:26:52running fine. Okay? We've come a long
- 1:26:54way. So, what do we do first? Let's get
- 1:26:56a reference to this button. Okay? So,
- 1:26:59just do the same simple document.query.
- 1:27:03What do we do inside the query sector?
- 1:27:05We added an ID. You could also get it
- 1:27:07from a class. But okay, let's get it
- 1:27:08from the ID. So, what do we do here? We
- 1:27:11have an ID. Which ID is it? New Game.
- 1:27:14Okay, so there it is. Now, let's hold
- 1:27:16it in a variable. So, let's call it a
- 1:27:20New Game button. Now, look, a button
- 1:27:22doesn't always have to look like a
- 1:27:24button. It's possible that clicking on
- 1:27:25H1 will look like a button. You can
- 1:27:27change that in CSS if you want. Okay,
- 1:27:29so what do we do now? You'll have to
- 1:27:31listen to this" New Game "button that's
- 1:27:33appeared. So, it's a simple story: add
- 1:27:35an event listener. Okay, sir. Which
- 1:27:37event should you listen to? Here,
- 1:27:38you've already listened to the" click.
- 1:27:40"What do you need to do? Let me explain
- 1:27:42. Now, write the function. Here, take
- 1:27:45this function. Here, take it. Okay?
- 1:27:48What happens inside the function?
- 1:27:49You'll get the event anyway. Okay? We
- 1:27:51already have the event. Okay? Now, you
- 1:27:53can use this event if you want. If you
- 1:27:55don't, don't use it. This event doesn't
- 1:27:57really matter because as soon as this
- 1:27:59event happens, meaning the click, we
- 1:28:01don't care about this event. We need to
- 1:28:03reset our variables. Yes, sir. The main
- 1:28:05story starts there. So, what should we
- 1:28:07do first? What should we do first? Turn
- 1:28:08on" Play Game. "So, set" Play Game "to
- 1:28:11true. Your game starts there. But don't
- 1:28:14use" Play Game "too quickly because
- 1:28:16you'll actually reset all the variables
- 1:28:18first. After that, you'll allow the
- 1:28:21user. So what do you need to do? First,
- 1:28:23you'll need to generate a new random
- 1:28:25number. So, get a random number. Why do
- 1:28:29you need to code it all over again?
- 1:28:30Since we already know the random number
- 1:28:31was generated this way, it would
- 1:28:33actually be better if we changed it,
- 1:28:35because re-creating the random number
- 1:28:37would cause const problems. So, let's
- 1:28:39change it now, and the logic will
- 1:28:41remain the same. So, let's copy it and
- 1:28:44move it here. So, random number equals
- 1:28:47random number. Here, the random number
- 1:28:49is generated. You don't need to remove
- 1:28:52the previous guesses one by one. Just
- 1:28:55reset it, it's done. Now, whatever
- 1:28:58values you entered in the previous
- 1:28:59guesses are empty. It's reset again.
- 1:29:02Okay, sir. What do you do next? Start
- 1:29:05your guessing numbers, that's it. Right
- 1:29:07now, you're taking your first guess.
- 1:29:09And what else can you do? The inner
- 1:29:12HTML of the guess slot has also been
- 1:29:15emptied. That disabled field will also
- 1:29:18need to be removed. Remaining will also
- 1:29:19need to be removed. Yes, sir. So, what
- 1:29:21should we do with this remaining inner
- 1:29:26HTML? Now, we can use the same logic we
- 1:29:31used here. Yes, we can actually use the
- 1:29:32entire remaining. Copy it, because the
- 1:29:36upper value is already selected. So,
- 1:29:39here it is, it's calculated again. Why
- 1:29:41do we need to do it again? Yes, we need
- 1:29:43to remove that from user input. We can
- 1:29:46use the remove attribute here. Not
- 1:29:48remove attribute ns, but remove
- 1:29:50attribute. So, remove attribute, here
- 1:29:55it is. Which attribute do we need to
- 1:29:58remove? There's only one. Disabled.
- 1:30:02We've removed this attribute as well.
- 1:30:05What else do we need to do? If you
- 1:30:07remember, we also had a start over.
- 1:30:09Remember? Start over? No, remember? It
- 1:30:10was start over. This query result pass,
- 1:30:13the result pass we took, contains the
- 1:30:15entire values. We appended a child here
- 1:30:18. Yes, that's exactly what we need to
- 1:30:19remove, so we'll have to remove one
- 1:30:20child from here as well. Now, yes,
- 1:30:22we're not just adding. So, which child
- 1:30:26do we need to remove? We only have one.
- 1:30:30Remove this paragraph. Okay? It will
- 1:30:32get the reference to the paragraph
- 1:30:33above. And then set" play game "to true
- 1:30:36. Hopefully, your game will start. Okay
- 1:30:39, sir. It was quite lengthy, but
- 1:30:40interesting. We didn't do anything
- 1:30:42complicated. We just went step by step.
- 1:30:44Okay, sir. Let's play it one more time.
- 1:30:46So, let's submit two. Then submit three
- 1:30:49. Then submit 11, then submit 11 again.
- 1:30:56Submit 22. Submit 33. Submit 44. And
- 1:31:02submit 55. And too high. Okay? Okay? We
- 1:31:07want to go high. We're not even looking
- 1:31:08. We don't have to go that far. 77 and
- 1:31:11another value, 88. Okay? That's all
- 1:31:15done. The random number was 53. The
- 1:31:17remaining value is negative one. You
- 1:31:18can change that. That's all the
- 1:31:20assignment for you. Let's start over.
- 1:31:22So all the values are there again. I
- 1:31:24can enter them again here. So, I saw, I
- 1:31:26enjoyed the projects. They're quite
- 1:31:28lengthy, but they're quite interesting
- 1:31:29and fun. Let's copy this and move it to
- 1:31:32our browser so we can give you this
- 1:31:34code too. Let's come here. Hey, we're
- 1:31:37done with the code. So let's start it
- 1:31:42again. I hope you enjoyed it. There are
- 1:31:46a lot of good projects in it. A lot of
- 1:31:47projects, so I won't add any more right
- 1:31:49now because I want to talk a little
- 1:31:50about event listeners and a little bit
- 1:31:51of JavaScript. I can't do any projects
- 1:31:53based on the classes right now because
- 1:31:54I haven't taught you the classes. I
- 1:31:56can't discuss higher-order functions. I
- 1:31:59can't discuss prototypes. I can't
- 1:32:01discuss advanced topics. But obviously,
- 1:32:03I'll take my time, but I'll teach
- 1:32:05JavaScript in a way I never have to see
- 1:32:07again. Okay, sir. So, just wait a
- 1:32:09second, I'll give you the final code
- 1:32:11and then I'll wrap up the video. In the
- 1:32:13meantime, please subscribe to your
- 1:32:14subscriptions and other things. And if
- 1:32:16the video isn't getting shares on
- 1:32:18Lindin even after so much hard work, it
- 1:32:20means you're not enjoying the projects,
- 1:32:22and I should just teach. I should do
- 1:32:24fewer projects. Okay? So, this is our
- 1:32:27Project 4 solution. Yes, please do it.
- 1:32:30Subscribe in the meantime. If you say
- 1:32:33you're leaving the video right now,
- 1:32:34next time I'll leave the video right
- 1:32:36here, and you won't find this code.
- 1:32:39Tell me, are you done yet? Here you go,
- 1:32:41your solution is ready. The Project 4
- 1:32:43session is up, okay. So, let's have
- 1:32:46some fun. Let's save it and commit it,
- 1:32:51Project 4. Here we go, commit it, and
- 1:32:57push it, so you get the source code.
- 1:33:04And there we go, and refresh it and
- 1:33:07check it. So, Project 1, 2, 3, 4, yes,
- 1:33:10you have all the source code. So, go to
- 1:33:13this code sandbox, Stack Blitz, and see
- 1:33:16what numbers you have, what values you
- 1:33:19have. Now, the best part is that I can
- 1:33:23refresh it whenever I want, and all
- 1:33:26this will disappear. So, here's our
- 1:33:29code. Well, it's there for now, because
- 1:33:32I have to open it in a new tab. But,
- 1:33:34okay, your URL is saved here. You can
- 1:33:36take a look at it if you want. The good
- 1:33:38thing is that I can also see how many
- 1:33:40of you are practicing this. I mean, it
- 1:33:42doesn't come to the individual exactly.
- 1:33:44But yes, if I tell you here, then these
- 1:33:46projects of Dom, then here I can see
- 1:33:48all these things like project views
- 1:33:50etc. If we go back to this, then how
- 1:33:52many projects are there here? How many
- 1:33:54views are there on that? All this is
- 1:33:55visible. So how many fors have there
- 1:33:57been? How many views have there been?
- 1:33:58So all this is also visible. Well, it
- 1:33:59felt good. I hope that the effort that
- 1:34:01we have put in, the hard work that we
- 1:34:02have done, will be very useful for you
- 1:34:04too. If you want more such videos which
- 1:34:06are purely project based, then tell me
- 1:34:07in the comment section, we will bring
- 1:34:09more videos. Well, it has been quite
- 1:34:10late. See you in the next video.
About this transcript
This page contains the full transcript of Lets build 4 javascript projects for beginners | chai aur #javascript by Chai aur Code, generated from the public captions YouTube serves with the video. The transcript has 18,181 words across 2,503 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.