Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:02,080
welcome to the ultimate javascript
2
00:00:02,080 --> 00:00:03,679
course where you're gonna master
3
00:00:03,679 --> 00:00:07,440
javascript in under 12 hours by building
4
00:00:07,440 --> 00:00:09,440
projects you're gonna start off with the
5
00:00:09,440 --> 00:00:11,679
basic fundamentals of javascript and
6
00:00:11,679 --> 00:00:13,679
then you're gonna move on to building
7
00:00:13,679 --> 00:00:15,759
smaller to bigger projects so for
8
00:00:15,759 --> 00:00:17,680
example you're going to start off first
9
00:00:17,680 --> 00:00:19,920
by doing lots of exercises so you can
10
00:00:19,920 --> 00:00:22,640
get good with how arrays work objects
11
00:00:22,640 --> 00:00:25,119
variables etc then you're going to build
12
00:00:25,119 --> 00:00:27,359
little projects like a tip calculator
13
00:00:27,359 --> 00:00:30,160
then i'll show you how to learn apis and
14
00:00:30,160 --> 00:00:32,479
make an app that gets a random dog every
15
00:00:32,479 --> 00:00:33,920
single time you click it you'll learn
16
00:00:33,920 --> 00:00:36,000
how to build your own superhero apps if
17
00:00:36,000 --> 00:00:38,800
you search up batman or thanos you can
18
00:00:38,800 --> 00:00:40,480
actually find your superhero and you can
19
00:00:40,480 --> 00:00:41,680
switch it up pretty cool pretty
20
00:00:41,680 --> 00:00:42,879
impressive and you can show it off to
21
00:00:42,879 --> 00:00:44,160
your friends i'll also show you how to
22
00:00:44,160 --> 00:00:46,000
deploy these apps so they're online
23
00:00:46,000 --> 00:00:47,760
anybody can use them at any time you'll
24
00:00:47,760 --> 00:00:50,160
learn how to build your own weather app
25
00:00:50,160 --> 00:00:51,920
with javascript i'll help you master
26
00:00:51,920 --> 00:00:54,079
concepts like promises and async and
27
00:00:54,079 --> 00:00:56,399
await some advanced concepts as well
28
00:00:56,399 --> 00:00:58,640
right don't worry this is for complete
29
00:00:58,640 --> 00:01:00,480
beginners okay so if you're a complete
30
00:01:00,480 --> 00:01:01,760
beginner you're in the right place and
31
00:01:01,760 --> 00:01:03,120
for even more intermediate there's going
32
00:01:03,120 --> 00:01:04,159
to be a couple of things for you to
33
00:01:04,159 --> 00:01:06,080
learn you're also going to build a
34
00:01:06,080 --> 00:01:07,439
fighting game which is going to be
35
00:01:07,439 --> 00:01:08,880
really cool it's going to be like street
36
00:01:08,880 --> 00:01:10,400
fighter but you get to build your own
37
00:01:10,400 --> 00:01:12,080
where your characters will have attack
38
00:01:12,080 --> 00:01:13,520
and heal and there's going to be sounds
39
00:01:13,520 --> 00:01:15,119
and graphics and you'll see how to build
40
00:01:15,119 --> 00:01:17,200
that with javascript and you'll be
41
00:01:17,200 --> 00:01:21,200
building the netflix app with javascript
42
00:01:21,200 --> 00:01:23,520
so how freaking exciting is that i hope
43
00:01:23,520 --> 00:01:25,439
that this is the most exciting
44
00:01:25,439 --> 00:01:27,040
javascript course that you've ever come
45
00:01:27,040 --> 00:01:28,880
across and i think you're gonna have a
46
00:01:28,880 --> 00:01:30,960
lot of fun so stick around this is gonna
47
00:01:30,960 --> 00:01:35,040
be a fun ride now before we just jump
48
00:01:35,040 --> 00:01:37,200
into it what i want you to do is smash
49
00:01:37,200 --> 00:01:39,759
that like button so this course goes out
50
00:01:39,759 --> 00:01:41,920
to as many people as possible and as a
51
00:01:41,920 --> 00:01:43,759
way to thank you here's a picture of a
52
00:01:43,759 --> 00:01:45,759
koala bear alright and a baby and a
53
00:01:45,759 --> 00:01:47,280
monkey there you go hopefully i earned
54
00:01:47,280 --> 00:01:49,360
that like now make sure to click the
55
00:01:49,360 --> 00:01:53,159
link that says join.replay.com
56
00:01:53,280 --> 00:01:56,560
cp dash javascript we're popping it up
57
00:01:56,560 --> 00:01:58,399
on the screen right now this is the most
58
00:01:58,399 --> 00:01:59,920
important part of this video so click
59
00:01:59,920 --> 00:02:01,680
that and make sure that you fork this
60
00:02:01,680 --> 00:02:04,399
rupple it's going to bring you to this
61
00:02:04,399 --> 00:02:06,719
screen right here and then what i want
62
00:02:06,719 --> 00:02:08,160
you to do is you should see javascript
63
00:02:08,160 --> 00:02:10,560
course by clever programmer right and
64
00:02:10,560 --> 00:02:12,000
then what i want you to do is make sure
65
00:02:12,000 --> 00:02:14,400
to hit fork this is very important okay
66
00:02:14,400 --> 00:02:17,360
hit fork rupple and once you do that you
67
00:02:17,360 --> 00:02:20,000
can actually get started working on this
68
00:02:20,000 --> 00:02:22,080
course but this is the most important
69
00:02:22,080 --> 00:02:23,840
step so click the link below in the
70
00:02:23,840 --> 00:02:25,599
youtube description or somewhere on the
71
00:02:25,599 --> 00:02:28,319
screen and then fork ruppel this is a
72
00:02:28,319 --> 00:02:30,000
rebel that you need to fork and then
73
00:02:30,000 --> 00:02:31,840
you'll be able to get started now once
74
00:02:31,840 --> 00:02:34,319
you fork it it should bring you to this
75
00:02:34,319 --> 00:02:36,400
screen over here now
76
00:02:36,400 --> 00:02:38,160
there's a bunch of code already here so
77
00:02:38,160 --> 00:02:39,519
that might intimidate you but don't let
78
00:02:39,519 --> 00:02:41,519
that intimidate you okay you can pretty
79
00:02:41,519 --> 00:02:43,519
much just ignore everything and when i'm
80
00:02:43,519 --> 00:02:45,519
gonna be coding along in the tutorial
81
00:02:45,519 --> 00:02:47,599
you can just follow along in this file
82
00:02:47,599 --> 00:02:49,680
your playground.js
83
00:02:49,680 --> 00:02:51,840
and you'll be completely fine you don't
84
00:02:51,840 --> 00:02:53,599
have to worry about anything that's
85
00:02:53,599 --> 00:02:55,440
happening in here right now
86
00:02:55,440 --> 00:02:57,920
these are only resources available for
87
00:02:57,920 --> 00:02:59,599
you as lessons
88
00:02:59,599 --> 00:03:01,760
notes exercises and projects that you
89
00:03:01,760 --> 00:03:04,879
could refer to later for now
90
00:03:04,879 --> 00:03:06,319
this is pretty much the your
91
00:03:06,319 --> 00:03:08,879
playground.js file is the one that you
92
00:03:08,879 --> 00:03:10,159
should be using
93
00:03:10,159 --> 00:03:11,760
in the video you'll see me use
94
00:03:11,760 --> 00:03:14,000
playground.js but that's mine so don't
95
00:03:14,000 --> 00:03:16,800
you you can go inside of it but don't
96
00:03:16,800 --> 00:03:21,519
use it okay only use your playground.js
97
00:03:21,519 --> 00:03:24,640
and write all of your code in here and
98
00:03:24,640 --> 00:03:26,480
you'll understand everything that's
99
00:03:26,480 --> 00:03:29,360
happening now just to explain to you
100
00:03:29,360 --> 00:03:31,519
what this is over here and what is
101
00:03:31,519 --> 00:03:33,280
happening is
102
00:03:33,280 --> 00:03:36,720
there's some in here we have the
103
00:03:36,720 --> 00:03:39,840
repo the github repo for this exact
104
00:03:39,840 --> 00:03:42,480
project so you can go ahead and you can
105
00:03:42,480 --> 00:03:44,959
fork that repo okay or not fork but like
106
00:03:44,959 --> 00:03:46,560
you can star it all right you could
107
00:03:46,560 --> 00:03:48,239
start that repo
108
00:03:48,239 --> 00:03:50,319
um just so on your github you could find
109
00:03:50,319 --> 00:03:52,080
it easily and plus it's nice for me to
110
00:03:52,080 --> 00:03:54,480
have stars right for these repos and go
111
00:03:54,480 --> 00:03:56,239
up and other developers can find it and
112
00:03:56,239 --> 00:03:57,680
helps them
113
00:03:57,680 --> 00:04:00,959
all right and then you have lessons so
114
00:04:00,959 --> 00:04:03,120
in lessons for example
115
00:04:03,120 --> 00:04:05,439
you can go to one of these lessons where
116
00:04:05,439 --> 00:04:07,599
maybe i have this little app that shows
117
00:04:07,599 --> 00:04:09,519
you how to click in with javascript and
118
00:04:09,519 --> 00:04:11,280
get these squares right and clear the
119
00:04:11,280 --> 00:04:13,360
game and you can go play it and then you
120
00:04:13,360 --> 00:04:15,360
can go and look at its code so for
121
00:04:15,360 --> 00:04:17,120
example for that red yellow green
122
00:04:17,120 --> 00:04:19,759
squares app that i have right here which
123
00:04:19,759 --> 00:04:21,358
you're going to build inside of this
124
00:04:21,358 --> 00:04:24,240
tutorial on the left hand side i'm going
125
00:04:24,240 --> 00:04:25,520
to have
126
00:04:25,520 --> 00:04:28,000
its code available for you so inside of
127
00:04:28,000 --> 00:04:31,199
dom i have this thing called red yellow
128
00:04:31,199 --> 00:04:33,360
green right and
129
00:04:33,360 --> 00:04:34,960
that's gonna be that's gonna have the
130
00:04:34,960 --> 00:04:36,560
code to this entire app so you're
131
00:04:36,560 --> 00:04:38,960
actually able to even click into it boom
132
00:04:38,960 --> 00:04:41,680
and see all of the code okay but again
133
00:04:41,680 --> 00:04:43,919
right now i wouldn't worry about looking
134
00:04:43,919 --> 00:04:46,720
at that right now just stay put all the
135
00:04:46,720 --> 00:04:48,960
project solutions are here so for
136
00:04:48,960 --> 00:04:50,479
example there's going to be project
137
00:04:50,479 --> 00:04:52,000
exercises and then you're going to see
138
00:04:52,000 --> 00:04:54,240
project solutions so what are these if
139
00:04:54,240 --> 00:04:55,919
you go into a project solution like
140
00:04:55,919 --> 00:04:56,960
let's say
141
00:04:56,960 --> 00:05:00,240
uh fighting game or weather app or
142
00:05:00,240 --> 00:05:02,400
create netflix these are the final
143
00:05:02,400 --> 00:05:04,320
completed versions of the app so if i
144
00:05:04,320 --> 00:05:06,479
type in las vegas and hit search boom my
145
00:05:06,479 --> 00:05:10,080
app shows up but if i go inside of
146
00:05:10,080 --> 00:05:12,080
project exercises weather app these are
147
00:05:12,080 --> 00:05:14,240
the uncompleted versions so when i hit
148
00:05:14,240 --> 00:05:16,160
them you'll notice that the javascript
149
00:05:16,160 --> 00:05:18,160
part is not working that's going to be
150
00:05:18,160 --> 00:05:21,199
your job to do and
151
00:05:21,199 --> 00:05:22,479
by the time you're going through this
152
00:05:22,479 --> 00:05:24,240
course you'll learn how to do all of
153
00:05:24,240 --> 00:05:26,880
this by yourself okay but for now don't
154
00:05:26,880 --> 00:05:29,360
try to do this yourself i'm just showing
155
00:05:29,360 --> 00:05:32,000
you kind of what's happening here what
156
00:05:32,000 --> 00:05:34,160
is all of this and how the course is
157
00:05:34,160 --> 00:05:36,639
structured but simply put for the rest
158
00:05:36,639 --> 00:05:39,120
of the video all you really need to do
159
00:05:39,120 --> 00:05:42,560
is just relax and follow along over here
160
00:05:42,560 --> 00:05:44,639
in your playground.js
161
00:05:44,639 --> 00:05:46,639
so whatever you see me do in the video
162
00:05:46,639 --> 00:05:49,360
just replicate it here and we'll start
163
00:05:49,360 --> 00:05:52,080
from the very first line of code hello
164
00:05:52,080 --> 00:05:54,960
world what i've decided to do is package
165
00:05:54,960 --> 00:05:57,360
all of this up into a course so you
166
00:05:57,360 --> 00:06:00,000
could binge this like netflix and it
167
00:06:00,000 --> 00:06:01,520
could actually keep track of your
168
00:06:01,520 --> 00:06:03,280
progress and everything will be much
169
00:06:03,280 --> 00:06:06,080
more organized and in one place in one
170
00:06:06,080 --> 00:06:08,080
course instead of you having to watch
171
00:06:08,080 --> 00:06:10,160
this on youtube so i've done this for
172
00:06:10,160 --> 00:06:12,639
your convenience to turn the ultimate
173
00:06:12,639 --> 00:06:15,120
javascript course for beginners into a
174
00:06:15,120 --> 00:06:18,560
packaged resource for you and it's also
175
00:06:18,560 --> 00:06:20,800
completely free and all you have to do
176
00:06:20,800 --> 00:06:22,479
is just go to the link below in the
177
00:06:22,479 --> 00:06:23,880
description that says
178
00:06:23,880 --> 00:06:27,199
cleverprogrammer.com slash js dash
179
00:06:27,199 --> 00:06:29,759
tutorial just go to that link click in
180
00:06:29,759 --> 00:06:31,360
the description below i'll take you
181
00:06:31,360 --> 00:06:34,479
directly to this completely free course
182
00:06:34,479 --> 00:06:36,560
all you have to do is just sign up okay
183
00:06:36,560 --> 00:06:37,840
it's gonna ask for your email or
184
00:06:37,840 --> 00:06:39,360
whatever to create an account so i could
185
00:06:39,360 --> 00:06:40,720
keep track of everything that you're
186
00:06:40,720 --> 00:06:42,479
doing and then boom you will have
187
00:06:42,479 --> 00:06:44,880
complete access to this course it'll be
188
00:06:44,880 --> 00:06:46,440
in your library you'll be able to go to
189
00:06:46,440 --> 00:06:48,319
cleverprogrammer.com and access it and
190
00:06:48,319 --> 00:06:50,479
it'll track your progress it'll track
191
00:06:50,479 --> 00:06:52,880
every single thing that you're doing so
192
00:06:52,880 --> 00:06:54,720
it's very important i highly recommend
193
00:06:54,720 --> 00:06:56,000
that you do that because it's going to
194
00:06:56,000 --> 00:06:58,000
enhance your experience of discourse
195
00:06:58,000 --> 00:07:00,560
with that said let's jump right into the
196
00:07:00,560 --> 00:07:02,720
video all right let's get started with
197
00:07:02,720 --> 00:07:05,520
the very basics of javascript so i'm
198
00:07:05,520 --> 00:07:07,360
gonna assume you're a complete beginner
199
00:07:07,360 --> 00:07:08,880
then we're gonna work our way up and
200
00:07:08,880 --> 00:07:11,599
make it more complex okay so let's start
201
00:07:11,599 --> 00:07:13,680
with something really really simple and
202
00:07:13,680 --> 00:07:15,360
we're just gonna start with the hello
203
00:07:15,360 --> 00:07:17,440
world and i know that's what everyone
204
00:07:17,440 --> 00:07:19,280
talks about but let's do it anyway okay
205
00:07:19,280 --> 00:07:21,360
so let's just do hello world and you can
206
00:07:21,360 --> 00:07:23,680
hit command s or control s if you're on
207
00:07:23,680 --> 00:07:25,840
windows and as soon as you save you'll
208
00:07:25,840 --> 00:07:28,080
notice that it runs right over here okay
209
00:07:28,080 --> 00:07:29,680
or you can just hit that play button at
210
00:07:29,680 --> 00:07:31,199
the top and as soon as you hit that
211
00:07:31,199 --> 00:07:33,759
you'll see hello world here will pop up
212
00:07:33,759 --> 00:07:36,000
so perfect that's exactly what we want
213
00:07:36,000 --> 00:07:38,319
now you can console log anything and
214
00:07:38,319 --> 00:07:39,919
they'll show up there okay so try
215
00:07:39,919 --> 00:07:43,039
console logging your name okay so try
216
00:07:43,039 --> 00:07:44,720
console logging that and if you do that
217
00:07:44,720 --> 00:07:46,560
correctly it should show up right over
218
00:07:46,560 --> 00:07:49,039
here and don't forget these apostrophes
219
00:07:49,039 --> 00:07:51,199
right here so let's give that a try i'm
220
00:07:51,199 --> 00:07:52,319
going to put my name here i'm going to
221
00:07:52,319 --> 00:07:55,280
hit run and boom at the bottom you see
222
00:07:55,280 --> 00:07:58,400
rafa kazi so good job so far you're
223
00:07:58,400 --> 00:08:00,160
making great progress and you most
224
00:08:00,160 --> 00:08:02,639
likely got this right as well so good
225
00:08:02,639 --> 00:08:04,560
job if you didn't that's okay don't
226
00:08:04,560 --> 00:08:06,160
worry we'll you'll have plenty of
227
00:08:06,160 --> 00:08:09,199
chances now let's do some programming
228
00:08:09,199 --> 00:08:11,919
and let's make it simple and get started
229
00:08:11,919 --> 00:08:13,680
with it okay so
230
00:08:13,680 --> 00:08:16,240
what can i do as a programmer right what
231
00:08:16,240 --> 00:08:18,160
are the things that i could do with it
232
00:08:18,160 --> 00:08:20,639
so let's say we have a variable and we
233
00:08:20,639 --> 00:08:23,199
start storing stuff in there okay so for
234
00:08:23,199 --> 00:08:26,000
example i can say name and i can say
235
00:08:26,000 --> 00:08:28,560
name is rafikaze and now if you hit run
236
00:08:28,560 --> 00:08:30,639
you'll notice that this won't really do
237
00:08:30,639 --> 00:08:33,679
anything this didn't do anything and i'm
238
00:08:33,679 --> 00:08:36,240
going to or actually let's say name is
239
00:08:36,240 --> 00:08:38,559
peter pan so this could my name can be
240
00:08:38,559 --> 00:08:40,320
different from the name that i gave here
241
00:08:40,320 --> 00:08:42,640
and you'll notice i don't see peter pan
242
00:08:42,640 --> 00:08:44,880
anywhere so what's going on why am i not
243
00:08:44,880 --> 00:08:46,880
seeing peter pan well what i need to do
244
00:08:46,880 --> 00:08:49,120
is remember i need to console log it so
245
00:08:49,120 --> 00:08:52,320
i can actually do this here and run it
246
00:08:52,320 --> 00:08:54,880
and boom all of a sudden notice that i'm
247
00:08:54,880 --> 00:08:58,000
seeing peter pan so that in javascript
248
00:08:58,000 --> 00:09:00,560
are called variables so you can store
249
00:09:00,560 --> 00:09:02,720
values in them and then you can refer to
250
00:09:02,720 --> 00:09:04,399
those values even if those values are
251
00:09:04,399 --> 00:09:06,720
really long so for example see i have a
252
00:09:06,720 --> 00:09:09,760
sentence here and my sentence is how are
253
00:09:09,760 --> 00:09:13,600
you doing today nice to see you hope you
254
00:09:13,600 --> 00:09:16,720
have a great day whatever my sentence is
255
00:09:16,720 --> 00:09:19,760
yeah and now if i do console.log and i
256
00:09:19,760 --> 00:09:22,959
do sentence and we run this look the
257
00:09:22,959 --> 00:09:25,760
entire sentence gets printed out but if
258
00:09:25,760 --> 00:09:29,200
i remove this console log boom you see
259
00:09:29,200 --> 00:09:31,440
that at the bottom right there no
260
00:09:31,440 --> 00:09:34,560
sentence shows up so this is something
261
00:09:34,560 --> 00:09:37,120
nice to kind of note and to pick up on
262
00:09:37,120 --> 00:09:38,480
what you just learned about are
263
00:09:38,480 --> 00:09:40,240
variables and if you ever see me doing
264
00:09:40,240 --> 00:09:42,080
these two slashes this just tells the
265
00:09:42,080 --> 00:09:45,120
computer hey this is for humans so don't
266
00:09:45,120 --> 00:09:47,360
actually try to run this because if i
267
00:09:47,360 --> 00:09:49,680
remove this and i try to run this i'm
268
00:09:49,680 --> 00:09:51,279
going to get some type of error that's
269
00:09:51,279 --> 00:09:52,480
going to say hey you haven't defined
270
00:09:52,480 --> 00:09:54,959
variables which just means i never told
271
00:09:54,959 --> 00:09:56,720
the computer what variables is i never
272
00:09:56,720 --> 00:09:58,399
did something like variables equal to 5
273
00:09:58,399 --> 00:10:00,880
or whatever but this line wasn't meant
274
00:10:00,880 --> 00:10:02,720
for computer anyway it was meant for a
275
00:10:02,720 --> 00:10:05,519
human being so these are called comments
276
00:10:05,519 --> 00:10:08,959
okay comments and they are meant to just
277
00:10:08,959 --> 00:10:11,440
be read by human beings and the computer
278
00:10:11,440 --> 00:10:14,160
decides to completely ignore them now
279
00:10:14,160 --> 00:10:17,920
let's do something where we have numbers
280
00:10:17,920 --> 00:10:19,360
and other things okay so i want to show
281
00:10:19,360 --> 00:10:20,959
you other things that you could do with
282
00:10:20,959 --> 00:10:23,519
javascript so let's say we have food and
283
00:10:23,519 --> 00:10:25,760
i set food to you know let's say you
284
00:10:25,760 --> 00:10:29,519
paid maybe 20 for your food okay great
285
00:10:29,519 --> 00:10:31,120
well i'll give you a little exercise
286
00:10:31,120 --> 00:10:33,279
here i want you to go ahead and console
287
00:10:33,279 --> 00:10:36,480
lock that can you make food pop up here
288
00:10:36,480 --> 00:10:39,279
give that a try i'll give you five four
289
00:10:39,279 --> 00:10:42,320
three two one let's go so we're gonna do
290
00:10:42,320 --> 00:10:44,640
console log and i'm gonna say food just
291
00:10:44,640 --> 00:10:46,640
like this and now when we hit it you'll
292
00:10:46,640 --> 00:10:48,800
see at the bottom i see 20. okay
293
00:10:48,800 --> 00:10:50,000
hopefully you're seeing that on your
294
00:10:50,000 --> 00:10:52,399
screen now i'm going to remove this line
295
00:10:52,399 --> 00:10:54,240
let me show you how you can do other
296
00:10:54,240 --> 00:10:56,079
things with programming okay so for
297
00:10:56,079 --> 00:10:58,320
example there's certain things called
298
00:10:58,320 --> 00:11:00,640
operators so we want to tip let's just
299
00:11:00,640 --> 00:11:03,839
say 20 okay now with that food and that
300
00:11:03,839 --> 00:11:06,160
tip what we need to figure out is how
301
00:11:06,160 --> 00:11:08,800
much is the tip because yes this is 20
302
00:11:08,800 --> 00:11:10,640
but what's the tip amount so let's
303
00:11:10,640 --> 00:11:13,120
calculate the tip amount and we're going
304
00:11:13,120 --> 00:11:16,240
to say it well that's simply food right
305
00:11:16,240 --> 00:11:18,720
multiplied by the tip so this maybe we
306
00:11:18,720 --> 00:11:21,040
can call it tip percentage so we should
307
00:11:21,040 --> 00:11:23,680
give better variable names and that way
308
00:11:23,680 --> 00:11:26,320
things are much more easy to read now
309
00:11:26,320 --> 00:11:28,720
before we run this remember this tip
310
00:11:28,720 --> 00:11:31,519
here we updated it to tip percent so
311
00:11:31,519 --> 00:11:33,839
make sure you copy paste that there so
312
00:11:33,839 --> 00:11:37,120
use food multiplied by tip percentage
313
00:11:37,120 --> 00:11:39,440
now you have your tip amount okay so
314
00:11:39,440 --> 00:11:41,600
let's now print that out so i'm going to
315
00:11:41,600 --> 00:11:44,160
say tip amount and let's try running
316
00:11:44,160 --> 00:11:47,760
this and we see that it was four dollars
317
00:11:47,760 --> 00:11:49,760
okay very good that's exactly what we
318
00:11:49,760 --> 00:11:51,600
wanted now let's say we want to make it
319
00:11:51,600 --> 00:11:54,240
a little bit more interactive and we
320
00:11:54,240 --> 00:11:56,480
want to make this a kind of like an app
321
00:11:56,480 --> 00:11:58,720
experience so right now i hope you're
322
00:11:58,720 --> 00:11:59,600
excited because you're going to be
323
00:11:59,600 --> 00:12:02,720
building your first app with javascript
324
00:12:02,720 --> 00:12:04,160
that's the power of javascript you can
325
00:12:04,160 --> 00:12:06,480
build things with it so freaking fast
326
00:12:06,480 --> 00:12:08,240
and you're going to be building an app
327
00:12:08,240 --> 00:12:09,839
right now so i'm going to teach you
328
00:12:09,839 --> 00:12:11,600
something called you know basically how
329
00:12:11,600 --> 00:12:14,000
to get inputs from user that's how you
330
00:12:14,000 --> 00:12:15,760
make apps because right now we've hard
331
00:12:15,760 --> 00:12:17,680
coded food to b20
332
00:12:17,680 --> 00:12:20,079
but let's say that if the food is a
333
00:12:20,079 --> 00:12:21,440
different amount well you'll need to
334
00:12:21,440 --> 00:12:23,040
come to this app that you made and
335
00:12:23,040 --> 00:12:25,279
you're going to need to put 40 here and
336
00:12:25,279 --> 00:12:26,720
you're going to need to put a different
337
00:12:26,720 --> 00:12:29,440
percentage you need to calculate this
338
00:12:29,440 --> 00:12:31,279
differently every single time it's going
339
00:12:31,279 --> 00:12:33,839
to be kind of annoying now here's how
340
00:12:33,839 --> 00:12:36,160
you can use user input okay so instead
341
00:12:36,160 --> 00:12:38,880
of this food saying 20 here kind of hard
342
00:12:38,880 --> 00:12:40,800
coded here what i'm going to instead do
343
00:12:40,800 --> 00:12:42,959
is i'm going to say prompt
344
00:12:42,959 --> 00:12:44,399
okay and
345
00:12:44,399 --> 00:12:46,240
i'll show you actually how prompt works
346
00:12:46,240 --> 00:12:48,079
so let's go ahead and try that first so
347
00:12:48,079 --> 00:12:49,519
i'm just going to say we're going to
348
00:12:49,519 --> 00:12:52,399
create a variable called banana and
349
00:12:52,399 --> 00:12:54,160
we're just going to store something in
350
00:12:54,160 --> 00:12:56,160
there and i'm going to say prompt or
351
00:12:56,160 --> 00:12:58,000
let's say fruit okay and we're going to
352
00:12:58,000 --> 00:13:00,240
say what is your favorite
353
00:13:00,240 --> 00:13:02,639
fruit we need to console log this so i'm
354
00:13:02,639 --> 00:13:05,360
going to say console.log we'll say
355
00:13:05,360 --> 00:13:07,440
all right and i'll say
356
00:13:07,440 --> 00:13:10,240
my favorite fruit is mango this time
357
00:13:10,240 --> 00:13:12,320
right and boom you actually see mango at
358
00:13:12,320 --> 00:13:14,959
the bottom right that's exactly the
359
00:13:14,959 --> 00:13:17,440
experience we wanted for this app so
360
00:13:17,440 --> 00:13:20,639
just very simple but we need to make
361
00:13:20,639 --> 00:13:22,720
this happen and we need to make our food
362
00:13:22,720 --> 00:13:25,519
calculator thing that we made more um
363
00:13:25,519 --> 00:13:27,440
dynamic because right now it's hard
364
00:13:27,440 --> 00:13:29,279
coded so we need to make it where it
365
00:13:29,279 --> 00:13:31,120
takes an input from the user instead of
366
00:13:31,120 --> 00:13:33,120
saying 20 here all right and then it
367
00:13:33,120 --> 00:13:35,040
calculates the tip amount based on that
368
00:13:35,040 --> 00:13:36,959
so what can we do i want you to take
369
00:13:36,959 --> 00:13:39,920
about 30 seconds to a minute and see how
370
00:13:39,920 --> 00:13:42,480
do i make this food thing over here
371
00:13:42,480 --> 00:13:44,560
interactive if you haven't figured out
372
00:13:44,560 --> 00:13:46,399
so all good let's do this together so
373
00:13:46,399 --> 00:13:49,360
i'm going to say prompt and in this
374
00:13:49,360 --> 00:13:52,720
prompt i'm simply going to say how much
375
00:13:52,720 --> 00:13:55,839
was the food and we don't we don't want
376
00:13:55,839 --> 00:13:58,639
this fruit line to run so i'm going to
377
00:13:58,639 --> 00:14:00,399
comment this out by putting a slash
378
00:14:00,399 --> 00:14:02,959
slash here and i'm also going to comment
379
00:14:02,959 --> 00:14:04,800
this one out this console log by putting
380
00:14:04,800 --> 00:14:06,240
a slash slash here because i don't want
381
00:14:06,240 --> 00:14:08,959
those to run i just want this here to
382
00:14:08,959 --> 00:14:11,519
run so what's going to happen now is
383
00:14:11,519 --> 00:14:13,040
food is going to run it's going to ask
384
00:14:13,040 --> 00:14:15,440
for what how much was your food once you
385
00:14:15,440 --> 00:14:16,959
put in how much the food was it's going
386
00:14:16,959 --> 00:14:19,040
to store that inside of the food and
387
00:14:19,040 --> 00:14:22,079
then our tip percentage is going to run
388
00:14:22,079 --> 00:14:23,839
and then our tip amount is going to be
389
00:14:23,839 --> 00:14:25,680
calculated based on whatever you put in
390
00:14:25,680 --> 00:14:28,959
the food multiplied by 20 tip and then
391
00:14:28,959 --> 00:14:31,680
we will output the tip amount perfect so
392
00:14:31,680 --> 00:14:33,760
how much was the food we will say was
393
00:14:33,760 --> 00:14:36,480
let's say forty dollars i'll hit run
394
00:14:36,480 --> 00:14:38,240
let's try it again and let's say it was
395
00:14:38,240 --> 00:14:40,720
twenty dollars so twenty percent of
396
00:14:40,720 --> 00:14:43,440
twenty should be four let's try it boom
397
00:14:43,440 --> 00:14:46,320
so that was great and there we go so our
398
00:14:46,320 --> 00:14:49,440
food was calculated properly we do have
399
00:14:49,440 --> 00:14:52,240
four here very good now what if you
400
00:14:52,240 --> 00:14:55,440
wanted to make tip percentage
401
00:14:55,440 --> 00:14:57,680
user input as well where this is not
402
00:14:57,680 --> 00:14:59,600
hard coded this is the data the user
403
00:14:59,600 --> 00:15:01,760
puts in because the user can decide hey
404
00:15:01,760 --> 00:15:03,760
how much tip do i want to add do i want
405
00:15:03,760 --> 00:15:08,480
to give 10 tip 20 tip 1 tip 100 tip that
406
00:15:08,480 --> 00:15:10,720
should be up to the person and what we
407
00:15:10,720 --> 00:15:12,480
want to do is the user should be able to
408
00:15:12,480 --> 00:15:14,480
put in let's say they want to give a 20
409
00:15:14,480 --> 00:15:16,560
tip so this should be able to put in 20.
410
00:15:16,560 --> 00:15:19,040
the user shouldn't have to say 0.2 right
411
00:15:19,040 --> 00:15:22,160
so they should be able to go 20. now
412
00:15:22,160 --> 00:15:24,240
what do we have to do to get 20 down to
413
00:15:24,240 --> 00:15:27,920
0.2 well 20 divided by 100 gets that to
414
00:15:27,920 --> 00:15:30,880
0.2 so it's whatever the user input is
415
00:15:30,880 --> 00:15:33,759
we divide that by 100 so let's replace
416
00:15:33,759 --> 00:15:36,399
this by user input let's say for example
417
00:15:36,399 --> 00:15:38,800
but in this case you know what we can do
418
00:15:38,800 --> 00:15:41,920
is we can just replace this with prompt
419
00:15:41,920 --> 00:15:43,759
okay and we can say
420
00:15:43,759 --> 00:15:46,240
let's just say tip percentage okay so
421
00:15:46,240 --> 00:15:48,000
that's all the prompt will say and then
422
00:15:48,000 --> 00:15:49,839
whatever tip that they put in we will
423
00:15:49,839 --> 00:15:53,199
divide that by 100. all right so here's
424
00:15:53,199 --> 00:15:55,040
one cool thing that we can do on the
425
00:15:55,040 --> 00:15:56,720
bottom right here click right in front
426
00:15:56,720 --> 00:15:58,480
of that arrow and it'll allow you to
427
00:15:58,480 --> 00:16:00,320
write code that will run right away so
428
00:16:00,320 --> 00:16:01,600
you could see what's happening so let's
429
00:16:01,600 --> 00:16:04,320
say that i have 20 and if i divide that
430
00:16:04,320 --> 00:16:06,160
by 100 and then i want you to hit
431
00:16:06,160 --> 00:16:08,800
execute at the bottom right and uh
432
00:16:08,800 --> 00:16:10,480
scroll all the way down and you can see
433
00:16:10,480 --> 00:16:12,639
20 divided by 100
434
00:16:12,639 --> 00:16:16,560
is 0.2 right so that's exactly what we
435
00:16:16,560 --> 00:16:18,399
want so that's how that division sign
436
00:16:18,399 --> 00:16:19,759
works and i know i haven't shown you
437
00:16:19,759 --> 00:16:21,360
division but
438
00:16:21,360 --> 00:16:22,560
there are a bunch of things that we
439
00:16:22,560 --> 00:16:24,560
could use in javascript so there's
440
00:16:24,560 --> 00:16:26,800
multiplication there's division there's
441
00:16:26,800 --> 00:16:29,279
exponents there are a bunch of other
442
00:16:29,279 --> 00:16:32,000
things but like plus minus right all the
443
00:16:32,000 --> 00:16:34,880
basic math stuff works so let's try and
444
00:16:34,880 --> 00:16:36,959
see if this is working this food thing
445
00:16:36,959 --> 00:16:38,800
that we're making here okay so
446
00:16:38,800 --> 00:16:39,920
whatever the tip is we're going to
447
00:16:39,920 --> 00:16:41,600
divide that by 100 and then we're going
448
00:16:41,600 --> 00:16:43,360
to multiply food with that tip
449
00:16:43,360 --> 00:16:45,440
percentage all right so let's give that
450
00:16:45,440 --> 00:16:48,480
a try over here i'm going to hit run and
451
00:16:48,480 --> 00:16:50,560
let's see what happens so how much was
452
00:16:50,560 --> 00:16:52,639
the food so let's say i paid a hundred
453
00:16:52,639 --> 00:16:54,959
bucks for the food the tip is going to
454
00:16:54,959 --> 00:16:57,360
calculate okay it's a tip percentage all
455
00:16:57,360 --> 00:17:00,240
right let's say we want to give 10 as a
456
00:17:00,240 --> 00:17:02,000
tip so that would be 10
457
00:17:02,000 --> 00:17:04,240
and boom you can see that it actually
458
00:17:04,240 --> 00:17:06,880
says ten dollars right there now if you
459
00:17:06,880 --> 00:17:08,480
don't even want this to come in the
460
00:17:08,480 --> 00:17:10,240
console and you want this to come like
461
00:17:10,240 --> 00:17:11,439
where the user could see it you could
462
00:17:11,439 --> 00:17:13,839
even use alert and you could alert the
463
00:17:13,839 --> 00:17:15,679
user with what the tip amount is gonna
464
00:17:15,679 --> 00:17:17,199
be so let's try this whole thing again
465
00:17:17,199 --> 00:17:18,799
and watch what's gonna happen it was a
466
00:17:18,799 --> 00:17:21,039
hundred dollars um how much was the tip
467
00:17:21,039 --> 00:17:23,520
it was let's say 10
468
00:17:23,520 --> 00:17:26,000
and boom it just says right there you're
469
00:17:26,000 --> 00:17:27,839
looking at 10. i'm just going to comment
470
00:17:27,839 --> 00:17:30,400
this out normally in a normal app you'll
471
00:17:30,400 --> 00:17:33,120
never use prompt to get user input
472
00:17:33,120 --> 00:17:35,200
you'll use something called forms in
473
00:17:35,200 --> 00:17:38,240
javascript or you know html forms you
474
00:17:38,240 --> 00:17:40,720
use forms and users like we'll put in
475
00:17:40,720 --> 00:17:43,760
the data there and then that'll populate
476
00:17:43,760 --> 00:17:45,600
in here but for now we're just going to
477
00:17:45,600 --> 00:17:47,600
stick to using prompt because it's the
478
00:17:47,600 --> 00:17:49,360
easiest way without having to do too
479
00:17:49,360 --> 00:17:51,679
much html and all this other crap all
480
00:17:51,679 --> 00:17:54,240
right so all right now what if we want
481
00:17:54,240 --> 00:17:56,080
to do something more because right now
482
00:17:56,080 --> 00:17:57,919
this is pretty good right but what if we
483
00:17:57,919 --> 00:18:00,880
want to show the tip amount and like the
484
00:18:00,880 --> 00:18:02,640
total amount right what do we want to
485
00:18:02,640 --> 00:18:04,960
show how much the food was how much the
486
00:18:04,960 --> 00:18:07,120
tip was and then what's the total amount
487
00:18:07,120 --> 00:18:09,039
what if we want to show all of those
488
00:18:09,039 --> 00:18:11,840
things so let's go ahead and do that as
489
00:18:11,840 --> 00:18:13,679
well so we're going to say total okay
490
00:18:13,679 --> 00:18:15,840
let's create a variable called total and
491
00:18:15,840 --> 00:18:19,440
total will be whatever food was plus the
492
00:18:19,440 --> 00:18:21,760
tip amount so let's go ahead and do that
493
00:18:21,760 --> 00:18:23,760
i want you to take a try and do that
494
00:18:23,760 --> 00:18:26,000
yourself and then console log it see if
495
00:18:26,000 --> 00:18:28,240
you nail it so take five to ten seconds
496
00:18:28,240 --> 00:18:31,360
to do that right now count down five
497
00:18:31,360 --> 00:18:32,480
four
498
00:18:32,480 --> 00:18:33,679
three
499
00:18:33,679 --> 00:18:34,720
two
500
00:18:34,720 --> 00:18:35,679
one
501
00:18:35,679 --> 00:18:37,520
and now let's go ahead and let's do it
502
00:18:37,520 --> 00:18:39,679
together all right i put myself up here
503
00:18:39,679 --> 00:18:41,520
so i'm nice and out of your way so let's
504
00:18:41,520 --> 00:18:43,360
go ahead and do this together now so
505
00:18:43,360 --> 00:18:46,559
total is gonna be food plus tip amount
506
00:18:46,559 --> 00:18:48,400
and then let's go ahead and let's make
507
00:18:48,400 --> 00:18:51,280
sure the console log and i'm gonna say
508
00:18:51,280 --> 00:18:53,360
total and then let's print it how much
509
00:18:53,360 --> 00:18:55,440
was the food okay so let's go the food
510
00:18:55,440 --> 00:18:57,280
was 100
511
00:18:57,280 --> 00:18:59,360
how much you want to tip let's say 20
512
00:18:59,360 --> 00:19:02,240
so that should be 20 so it should be 100
513
00:19:02,240 --> 00:19:05,280
plus 20 tip that should put you at 120.
514
00:19:05,280 --> 00:19:08,480
so let's see and we got this weird thing
515
00:19:08,480 --> 00:19:10,720
so what's happening here is if you take
516
00:19:10,720 --> 00:19:13,520
a thing like this 100 and you try to add
517
00:19:13,520 --> 00:19:15,840
20 to this this is not going to do what
518
00:19:15,840 --> 00:19:17,440
you think it's going to do okay because
519
00:19:17,440 --> 00:19:19,120
this is a different data type and we're
520
00:19:19,120 --> 00:19:20,559
going to talk about different data types
521
00:19:20,559 --> 00:19:22,320
so i'll write this here for now data
522
00:19:22,320 --> 00:19:24,000
types and there are different data types
523
00:19:24,000 --> 00:19:26,320
like strings and numbers and then other
524
00:19:26,320 --> 00:19:28,240
ones anything that has these quotes
525
00:19:28,240 --> 00:19:30,880
around it is a string and so if you try
526
00:19:30,880 --> 00:19:32,400
to add two strings watch what's going to
527
00:19:32,400 --> 00:19:34,799
happen when i hit execute it's going to
528
00:19:34,799 --> 00:19:37,600
add a hundred plus the word 20. so it's
529
00:19:37,600 --> 00:19:39,600
almost like you're adding words together
530
00:19:39,600 --> 00:19:41,120
okay it doesn't see these as numbers
531
00:19:41,120 --> 00:19:42,720
it's like looking at it like a word this
532
00:19:42,720 --> 00:19:45,120
is a word you're spelling 100 and when
533
00:19:45,120 --> 00:19:46,880
you try to add those two words together
534
00:19:46,880 --> 00:19:49,039
it makes them one word and so that's not
535
00:19:49,039 --> 00:19:51,200
what we want but if you take a number
536
00:19:51,200 --> 00:19:53,520
100 and then you add 20 to it and if you
537
00:19:53,520 --> 00:19:55,760
hit execute you can see that that
538
00:19:55,760 --> 00:19:58,559
actually gives you 120 basically what
539
00:19:58,559 --> 00:20:00,320
you're looking for so what we need to do
540
00:20:00,320 --> 00:20:01,919
is we're getting this data from the
541
00:20:01,919 --> 00:20:04,960
users and it's coming back as a string
542
00:20:04,960 --> 00:20:07,919
you guys so for example i console log
543
00:20:07,919 --> 00:20:11,440
this food right here and let's just like
544
00:20:11,440 --> 00:20:13,440
run through this and let's just say 100
545
00:20:13,440 --> 00:20:16,480
and we'll say 20 and run this that first
546
00:20:16,480 --> 00:20:19,120
100 even though you can't see it it's
547
00:20:19,120 --> 00:20:21,440
actually a string so this is not a
548
00:20:21,440 --> 00:20:23,200
number it's actually a string so what we
549
00:20:23,200 --> 00:20:24,640
want to do is we want to force it to be
550
00:20:24,640 --> 00:20:26,159
kind of a number so let's say i have a
551
00:20:26,159 --> 00:20:28,320
100 here right if i want to force it to
552
00:20:28,320 --> 00:20:30,960
become a number i can call this i can
553
00:20:30,960 --> 00:20:32,799
wrap this in something called number so
554
00:20:32,799 --> 00:20:34,559
number is a function and it will take
555
00:20:34,559 --> 00:20:38,480
this 100 and turn it into an a regular
556
00:20:38,480 --> 00:20:40,880
100 without the quotes so we'll talk
557
00:20:40,880 --> 00:20:43,200
more about functions later so we'll make
558
00:20:43,200 --> 00:20:44,559
a note of this we'll talk more about
559
00:20:44,559 --> 00:20:47,039
functions later things like that and you
560
00:20:47,039 --> 00:20:49,039
know maybe even type casting but those
561
00:20:49,039 --> 00:20:50,720
are a little bit more advanced right now
562
00:20:50,720 --> 00:20:52,000
i don't want you to worry about any of
563
00:20:52,000 --> 00:20:54,000
that i just want you to focus on i'm
564
00:20:54,000 --> 00:20:56,240
just wrapping this thing in something
565
00:20:56,240 --> 00:20:58,000
called number and it automatically takes
566
00:20:58,000 --> 00:20:59,600
it from a word and turns it into a
567
00:20:59,600 --> 00:21:01,760
number and so i'm gonna take here and
568
00:21:01,760 --> 00:21:03,600
i'm going to just wrap this in a
569
00:21:03,600 --> 00:21:05,520
function called number like this so when
570
00:21:05,520 --> 00:21:07,440
you get food amount that should be a
571
00:21:07,440 --> 00:21:09,679
number and this tip percentage here that
572
00:21:09,679 --> 00:21:11,039
should be a number as well so i'm going
573
00:21:11,039 --> 00:21:13,679
to wrap this into a number as well just
574
00:21:13,679 --> 00:21:15,520
like this and we're going to move this
575
00:21:15,520 --> 00:21:17,679
here so you can see this very clearly so
576
00:21:17,679 --> 00:21:19,440
i've wrapped this inside of a number and
577
00:21:19,440 --> 00:21:22,320
i've wrapped this inside wrap this
578
00:21:22,320 --> 00:21:24,480
inside of a number as well make sure you
579
00:21:24,480 --> 00:21:26,080
have the right amount of parentheses
580
00:21:26,080 --> 00:21:27,600
going on so now we're not going to get
581
00:21:27,600 --> 00:21:29,600
that weird error anymore let's go ahead
582
00:21:29,600 --> 00:21:31,200
and run this again we're going to say
583
00:21:31,200 --> 00:21:34,159
the food was 100 tip is 20 percent and
584
00:21:34,159 --> 00:21:36,559
it calculates it correctly over here and
585
00:21:36,559 --> 00:21:40,159
you do see that it says 120 okay that's
586
00:21:40,159 --> 00:21:42,080
exactly what we were looking for
587
00:21:42,080 --> 00:21:44,400
perfect now one other thing that's kind
588
00:21:44,400 --> 00:21:45,679
of confusing when we're looking at our
589
00:21:45,679 --> 00:21:48,559
console log is like ah this says 20 here
590
00:21:48,559 --> 00:21:51,200
and it says 120 and
591
00:21:51,200 --> 00:21:52,799
i would want to label it right so i
592
00:21:52,799 --> 00:21:55,039
would want to want it to say tip amount
593
00:21:55,039 --> 00:21:57,840
here and then 20 and then total here and
594
00:21:57,840 --> 00:21:59,840
then 120. in other words like basically
595
00:21:59,840 --> 00:22:01,360
i wanted to say
596
00:22:01,360 --> 00:22:03,679
tip like that here and i would want this
597
00:22:03,679 --> 00:22:05,919
to have total here just like that right
598
00:22:05,919 --> 00:22:08,000
that's that's the the thing that i want
599
00:22:08,000 --> 00:22:10,320
so let's do that here so in console log
600
00:22:10,320 --> 00:22:13,600
i can actually say tip amount and then
601
00:22:13,600 --> 00:22:16,559
here i can say total so now watch what's
602
00:22:16,559 --> 00:22:18,559
going to happen when i run this and we
603
00:22:18,559 --> 00:22:21,600
say 100 and we said 20 is it tip amount
604
00:22:21,600 --> 00:22:24,240
20 total 120. okay so now it's kind of
605
00:22:24,240 --> 00:22:26,960
nice our app is working in a better way
606
00:22:26,960 --> 00:22:29,679
a nicer way so everything looks cleaner
607
00:22:29,679 --> 00:22:31,120
and better and it's just showing you
608
00:22:31,120 --> 00:22:32,320
different things you could kind of do
609
00:22:32,320 --> 00:22:34,240
with the console log so i hope for now
610
00:22:34,240 --> 00:22:35,919
this was really good we're gonna keep on
611
00:22:35,919 --> 00:22:37,760
going there's a lot more to cover i hope
612
00:22:37,760 --> 00:22:40,240
you're excited i'm excited and i'll see
613
00:22:40,240 --> 00:22:42,720
you shortly alright let's get back to
614
00:22:42,720 --> 00:22:44,720
coding and now you're gonna learn about
615
00:22:44,720 --> 00:22:46,640
some really cool stuff and we're gonna
616
00:22:46,640 --> 00:22:48,159
build some more fun stuff okay so let's
617
00:22:48,159 --> 00:22:49,919
go ahead and do it together but one
618
00:22:49,919 --> 00:22:52,400
thing i want to tell you is if you want
619
00:22:52,400 --> 00:22:54,640
to code yourself you're going to be
620
00:22:54,640 --> 00:22:56,840
coding in a file called your
621
00:22:56,840 --> 00:22:59,360
playground.js okay so that's the file i
622
00:22:59,360 --> 00:23:01,840
want you to be using when you're coding
623
00:23:01,840 --> 00:23:03,919
right now because i'm coding in a file
624
00:23:03,919 --> 00:23:06,000
called playground.js okay so by the time
625
00:23:06,000 --> 00:23:08,559
you're seeing this video this file will
626
00:23:08,559 --> 00:23:11,360
have like all the code that i have
627
00:23:11,360 --> 00:23:13,360
written you know so far so this is going
628
00:23:13,360 --> 00:23:15,039
to have like pretty much everything that
629
00:23:15,039 --> 00:23:16,960
we have done together but in your
630
00:23:16,960 --> 00:23:18,720
playground it's like your scratch file
631
00:23:18,720 --> 00:23:20,080
and you're able to kind of code along
632
00:23:20,080 --> 00:23:22,080
with me all right let's keep going let's
633
00:23:22,080 --> 00:23:23,840
talk about a couple more things okay so
634
00:23:23,840 --> 00:23:25,520
one of the things i want to talk to you
635
00:23:25,520 --> 00:23:28,880
about are like different kind of data
636
00:23:28,880 --> 00:23:31,520
types that we have all right so we got
637
00:23:31,520 --> 00:23:33,679
numbers all right and the examples of
638
00:23:33,679 --> 00:23:37,280
them are like 1 5 10 100 you know
639
00:23:37,280 --> 00:23:38,880
whatever you could have decimal numbers
640
00:23:38,880 --> 00:23:41,360
too like 2.5 you have other things like
641
00:23:41,360 --> 00:23:42,960
what we call strings and these are
642
00:23:42,960 --> 00:23:44,480
anything that have quotes around them so
643
00:23:44,480 --> 00:23:46,960
it's like if i say hello or if i say
644
00:23:46,960 --> 00:23:50,400
what's what is up okay those are all
645
00:23:50,400 --> 00:23:52,320
strings and now strings could have
646
00:23:52,320 --> 00:23:54,640
single quotes around them or they could
647
00:23:54,640 --> 00:23:56,960
also actually have double quotes around
648
00:23:56,960 --> 00:23:59,279
them okay so like this you know as long
649
00:23:59,279 --> 00:24:01,200
as they're wrapped up and
650
00:24:01,200 --> 00:24:03,279
it's even on both sides okay single
651
00:24:03,279 --> 00:24:06,240
versus double they both work so those
652
00:24:06,240 --> 00:24:07,760
are strings so something we were talking
653
00:24:07,760 --> 00:24:09,679
about up here anything in quotes is
654
00:24:09,679 --> 00:24:11,840
going to kind of print out exactly like
655
00:24:11,840 --> 00:24:14,240
how you want it to okay other data types
656
00:24:14,240 --> 00:24:17,120
might include things like arrays and
657
00:24:17,120 --> 00:24:19,039
we'll talk about them later i'm just
658
00:24:19,039 --> 00:24:20,480
going to kind of leave a note here for
659
00:24:20,480 --> 00:24:23,200
now for this we also have objects
660
00:24:23,200 --> 00:24:24,880
a different completely different data
661
00:24:24,880 --> 00:24:27,679
type another data type is boolean so
662
00:24:27,679 --> 00:24:30,320
this is either something is true or
663
00:24:30,320 --> 00:24:33,679
false okay so true slash false this is
664
00:24:33,679 --> 00:24:36,720
an example of a boolean and why i'm
665
00:24:36,720 --> 00:24:38,159
talking about this and i know right now
666
00:24:38,159 --> 00:24:39,679
it might be like why are we talking
667
00:24:39,679 --> 00:24:41,440
about data types i mean this seems kind
668
00:24:41,440 --> 00:24:43,200
of boring but this is going to become
669
00:24:43,200 --> 00:24:44,559
really important because imagine if
670
00:24:44,559 --> 00:24:46,559
you're building an app where you know
671
00:24:46,559 --> 00:24:48,320
let's say that it's a simple app and it
672
00:24:48,320 --> 00:24:50,559
says hey if raining outside tell the
673
00:24:50,559 --> 00:24:53,600
person to get an umbrella or if it's not
674
00:24:53,600 --> 00:24:55,279
raining outside then tell a person to
675
00:24:55,279 --> 00:24:57,200
get a sunglasses well you're going to
676
00:24:57,200 --> 00:24:58,960
need to understand how boolean works
677
00:24:58,960 --> 00:25:00,000
otherwise you're not going to be able to
678
00:25:00,000 --> 00:25:02,080
make an app like that work taking it
679
00:25:02,080 --> 00:25:04,480
into real life well banks work off of
680
00:25:04,480 --> 00:25:05,679
booleans right because if you think
681
00:25:05,679 --> 00:25:08,240
about a bank can you take out if you
682
00:25:08,240 --> 00:25:10,080
have zero dollars left in the bank can
683
00:25:10,080 --> 00:25:13,840
you take out uh 10 000 or 1000 dollars
684
00:25:13,840 --> 00:25:16,559
you can't so the bank has a line of code
685
00:25:16,559 --> 00:25:19,039
that's sitting there that goes if user
686
00:25:19,039 --> 00:25:20,960
has this amount of money available so
687
00:25:20,960 --> 00:25:23,039
let's say you ask for five thousand
688
00:25:23,039 --> 00:25:25,440
dollars it says does the user have five
689
00:25:25,440 --> 00:25:27,120
thousand dollars available and if the
690
00:25:27,120 --> 00:25:29,600
user does then great let them transact
691
00:25:29,600 --> 00:25:32,400
it then basically if they do say true
692
00:25:32,400 --> 00:25:35,840
but if they don't say false okay and so
693
00:25:35,840 --> 00:25:38,880
using booleans you're able to do
694
00:25:38,880 --> 00:25:41,440
something we'll talk about later you can
695
00:25:41,440 --> 00:25:43,919
do conditionals and then you can do if
696
00:25:43,919 --> 00:25:46,159
else type of statements or if then and
697
00:25:46,159 --> 00:25:47,760
that's going to be important another
698
00:25:47,760 --> 00:25:50,480
thing i want you to learn about is well
699
00:25:50,480 --> 00:25:52,240
you know we have data types but one
700
00:25:52,240 --> 00:25:54,000
other thing we need to worry about is
701
00:25:54,000 --> 00:25:57,120
like basic math stuff okay and it's
702
00:25:57,120 --> 00:25:58,640
gonna be really simple like i'm gonna
703
00:25:58,640 --> 00:26:01,279
say like first grader level math stuff
704
00:26:01,279 --> 00:26:03,279
okay or third grader level like
705
00:26:03,279 --> 00:26:05,279
something really really simple so let's
706
00:26:05,279 --> 00:26:08,080
go and just talk about kind of math like
707
00:26:08,080 --> 00:26:10,240
operations that we have okay and you
708
00:26:10,240 --> 00:26:11,679
should know about these so for example
709
00:26:11,679 --> 00:26:13,760
we have multiplication so you can
710
00:26:13,760 --> 00:26:16,880
multiply using this you can divide using
711
00:26:16,880 --> 00:26:20,640
a slash you can uh do other ones like x
712
00:26:20,640 --> 00:26:22,880
exponents okay and these are like with
713
00:26:22,880 --> 00:26:25,679
two asterisks like this and then
714
00:26:25,679 --> 00:26:28,720
you know we have modulo which is like
715
00:26:28,720 --> 00:26:30,640
this or you know in other words is like
716
00:26:30,640 --> 00:26:32,880
a remainder okay so for example if you
717
00:26:32,880 --> 00:26:35,840
did five modulo two what would you get
718
00:26:35,840 --> 00:26:37,200
well that's a great question let's
719
00:26:37,200 --> 00:26:39,360
actually ask our javascript here so i'll
720
00:26:39,360 --> 00:26:42,640
do five modular two and hit execute okay
721
00:26:42,640 --> 00:26:44,080
and you can see that one is the
722
00:26:44,080 --> 00:26:46,000
remainder here right so you'd get back
723
00:26:46,000 --> 00:26:48,720
one if you did this so that's how modulo
724
00:26:48,720 --> 00:26:50,559
and remainders work and then of course
725
00:26:50,559 --> 00:26:52,960
you have add which is a plus sign which
726
00:26:52,960 --> 00:26:54,320
is pretty simple and i don't need to
727
00:26:54,320 --> 00:26:56,320
probably give you an example of that and
728
00:26:56,320 --> 00:26:58,480
then you have minus okay so these are
729
00:26:58,480 --> 00:27:00,480
kind of the majority of the math
730
00:27:00,480 --> 00:27:03,520
operations that you will do when you're
731
00:27:03,520 --> 00:27:05,440
coding so these are helpful for you to
732
00:27:05,440 --> 00:27:07,279
know because when we're we're going to
733
00:27:07,279 --> 00:27:08,480
be going along and we're going to be
734
00:27:08,480 --> 00:27:10,559
building things together and some of
735
00:27:10,559 --> 00:27:11,600
this stuff you're going to kind of need
736
00:27:11,600 --> 00:27:13,440
to know so we're going to just put it
737
00:27:13,440 --> 00:27:15,120
out there let's go ahead and call it
738
00:27:15,120 --> 00:27:17,440
math operators because they're like
739
00:27:17,440 --> 00:27:20,320
operators okay and then i'm just gonna
740
00:27:20,320 --> 00:27:23,360
show you a couple of like
741
00:27:23,360 --> 00:27:24,320
math
742
00:27:24,320 --> 00:27:26,640
methods okay that are gonna be helpful
743
00:27:26,640 --> 00:27:29,039
for you so and one of them we can
744
00:27:29,039 --> 00:27:31,520
actually even use right now so floor and
745
00:27:31,520 --> 00:27:35,440
seal the floor it just rounds down okay
746
00:27:35,440 --> 00:27:37,279
and this just you know if you can guess
747
00:27:37,279 --> 00:27:40,000
it by the name it rounds it up if you're
748
00:27:40,000 --> 00:27:41,919
getting a tip well that's that's going
749
00:27:41,919 --> 00:27:43,360
to be really weird right because like
750
00:27:43,360 --> 00:27:45,520
let's say you haven't placed an order
751
00:27:45,520 --> 00:27:46,840
and it was for
752
00:27:46,840 --> 00:27:49,520
110.25 cents or something and then
753
00:27:49,520 --> 00:27:51,440
you're trying to calculate a tip on that
754
00:27:51,440 --> 00:27:52,640
20
755
00:27:52,640 --> 00:27:54,240
and then you get these like decimal
756
00:27:54,240 --> 00:27:55,760
numbers now sometimes that could be
757
00:27:55,760 --> 00:27:57,360
confusing and maybe you don't want it to
758
00:27:57,360 --> 00:27:59,120
a decimal number you want it to be an
759
00:27:59,120 --> 00:28:01,200
exact dollar amount okay let's just say
760
00:28:01,200 --> 00:28:03,440
well what you could do in that case is
761
00:28:03,440 --> 00:28:06,919
you could take a number like 22.5 or
762
00:28:06,919 --> 00:28:10,000
110.25 cents and you could call floor on
763
00:28:10,000 --> 00:28:11,440
this okay so we're going to do
764
00:28:11,440 --> 00:28:13,679
math.floor and we're going to wrap this
765
00:28:13,679 --> 00:28:15,679
in and i'm going to hit execute and you
766
00:28:15,679 --> 00:28:18,000
can see that it just floored it so it
767
00:28:18,000 --> 00:28:20,240
just rounded it down that's all it does
768
00:28:20,240 --> 00:28:22,080
so if you did the same thing let's say
769
00:28:22,080 --> 00:28:25,039
seal and i said 110.25
770
00:28:25,039 --> 00:28:27,360
and i hit execute it would round that up
771
00:28:27,360 --> 00:28:31,279
to 111 okay when we're actually building
772
00:28:31,279 --> 00:28:32,880
our we're going to be building some
773
00:28:32,880 --> 00:28:34,880
games in this tutorial as well so for
774
00:28:34,880 --> 00:28:37,200
example later when we go and let's say
775
00:28:37,200 --> 00:28:39,679
we build rock paper scissors or we build
776
00:28:39,679 --> 00:28:41,200
any of these games you're going to need
777
00:28:41,200 --> 00:28:43,440
to know some of these things okay so you
778
00:28:43,440 --> 00:28:46,880
can round down and round up um and
779
00:28:46,880 --> 00:28:49,039
you'll see why so you need to know this
780
00:28:49,039 --> 00:28:50,559
if you want to build those games later
781
00:28:50,559 --> 00:28:52,960
and then there's random okay all right
782
00:28:52,960 --> 00:28:56,000
so here's how random works okay this
783
00:28:56,000 --> 00:28:59,039
gives you a random number okay okay so
784
00:28:59,039 --> 00:29:01,440
this is how you actually use math.random
785
00:29:01,440 --> 00:29:03,360
okay so let's say i use math.random
786
00:29:03,360 --> 00:29:05,200
don't put anything inside of it and just
787
00:29:05,200 --> 00:29:06,799
run it so it'll always give you a number
788
00:29:06,799 --> 00:29:09,120
between 0 and 1. so if i keep pasting
789
00:29:09,120 --> 00:29:11,279
math.random you'll always get a number
790
00:29:11,279 --> 00:29:13,360
between 0 and 1. now if you want to try
791
00:29:13,360 --> 00:29:16,399
to get a number between 0 and 3 just
792
00:29:16,399 --> 00:29:18,720
multiply this by 3 like this and this
793
00:29:18,720 --> 00:29:20,000
will always make sure that you get a
794
00:29:20,000 --> 00:29:22,080
number between 0 and 3. and so if i keep
795
00:29:22,080 --> 00:29:23,520
running this you can see i got
796
00:29:23,520 --> 00:29:25,679
math.random multiplied by 3 and there
797
00:29:25,679 --> 00:29:28,159
you go i got a 2.4 let's try it again
798
00:29:28,159 --> 00:29:31,679
right i get a 0.42 and now you know when
799
00:29:31,679 --> 00:29:33,440
you're playing rock paper scissors you
800
00:29:33,440 --> 00:29:36,559
want rock to be zero you want paper to
801
00:29:36,559 --> 00:29:38,240
be one and let's say you want scissors
802
00:29:38,240 --> 00:29:40,720
to be two so if you want those numbers
803
00:29:40,720 --> 00:29:43,840
to map to them like that you need to
804
00:29:43,840 --> 00:29:45,520
pick between zero one and two okay
805
00:29:45,520 --> 00:29:46,799
that's what you need to do that's why
806
00:29:46,799 --> 00:29:48,720
you need to run this function and i'm
807
00:29:48,720 --> 00:29:50,480
gonna you're gonna learn later why
808
00:29:50,480 --> 00:29:52,880
that's the case okay but that's why you
809
00:29:52,880 --> 00:29:55,120
need to get between zero one and two so
810
00:29:55,120 --> 00:29:57,360
if we keep going with this well let me
811
00:29:57,360 --> 00:29:59,120
ask you this right right now you're
812
00:29:59,120 --> 00:30:01,679
doing this and you get this long number
813
00:30:01,679 --> 00:30:04,000
two point seven two blah blah but you
814
00:30:04,000 --> 00:30:06,080
just wanna get either zero one and two
815
00:30:06,080 --> 00:30:07,919
so in other words how could you turn
816
00:30:07,919 --> 00:30:11,039
this long number into just two okay take
817
00:30:11,039 --> 00:30:12,720
a couple of seconds it's one of the
818
00:30:12,720 --> 00:30:14,799
things i've already taught you okay hint
819
00:30:14,799 --> 00:30:16,320
hint it's one of the methods you have to
820
00:30:16,320 --> 00:30:19,760
use here okay so see if you can guess
821
00:30:19,760 --> 00:30:22,960
this i'll give you five seconds five
822
00:30:22,960 --> 00:30:25,520
four three two
823
00:30:25,520 --> 00:30:26,960
one
824
00:30:26,960 --> 00:30:30,080
all right let's call math.floor on this
825
00:30:30,080 --> 00:30:32,080
and wrap this in and hit execute and
826
00:30:32,080 --> 00:30:34,159
you'll see boom i just got two so then
827
00:30:34,159 --> 00:30:35,679
in other words what i have to do is i
828
00:30:35,679 --> 00:30:38,320
have to copy this math.random multiplied
829
00:30:38,320 --> 00:30:41,039
by three paste it here and
830
00:30:41,039 --> 00:30:41,799
call
831
00:30:41,799 --> 00:30:44,640
math.floor on this entire thing so now
832
00:30:44,640 --> 00:30:46,559
when i do that what will happen is this
833
00:30:46,559 --> 00:30:48,720
will eval run and evaluate and give me
834
00:30:48,720 --> 00:30:50,559
something like two point seven two point
835
00:30:50,559 --> 00:30:52,559
seven eight nine five five five four
836
00:30:52,559 --> 00:30:54,399
four four some long number right two
837
00:30:54,399 --> 00:30:56,799
point seven nine blah blah blah and then
838
00:30:56,799 --> 00:30:59,840
on that it'll call math.floor and it
839
00:30:59,840 --> 00:31:02,080
will turn this into just two and you'll
840
00:31:02,080 --> 00:31:04,399
end up with just two okay so let's try
841
00:31:04,399 --> 00:31:06,799
running this boom i got zero perfect
842
00:31:06,799 --> 00:31:08,880
let's run it again boom i got one let's
843
00:31:08,880 --> 00:31:10,960
run it again boom i got zero let's run
844
00:31:10,960 --> 00:31:13,279
it again boom i got two perfect so i'll
845
00:31:13,279 --> 00:31:15,120
always get between zero one and two
846
00:31:15,120 --> 00:31:16,799
those are the three different choices
847
00:31:16,799 --> 00:31:18,720
that'll map to my rock or paper or
848
00:31:18,720 --> 00:31:20,640
scissors later on when you're coding
849
00:31:20,640 --> 00:31:22,880
that up so that's just an example of how
850
00:31:22,880 --> 00:31:25,279
these methods work we'll practice more
851
00:31:25,279 --> 00:31:27,360
with them later but right now you should
852
00:31:27,360 --> 00:31:29,039
just know them and have them in your
853
00:31:29,039 --> 00:31:30,880
notes all right guys and let's do this
854
00:31:30,880 --> 00:31:32,960
thing right now so let's just go up and
855
00:31:32,960 --> 00:31:35,840
comment out like some of this stuff here
856
00:31:35,840 --> 00:31:37,200
so like we don't want to run this
857
00:31:37,200 --> 00:31:38,720
anymore so what i'm going to do is i'm
858
00:31:38,720 --> 00:31:40,480
just going to go ahead and highlight all
859
00:31:40,480 --> 00:31:42,799
of this okay because i don't care about
860
00:31:42,799 --> 00:31:44,480
running this anymore because otherwise
861
00:31:44,480 --> 00:31:45,679
every time you hit run it's going to
862
00:31:45,679 --> 00:31:47,039
keep prompting you and we don't want
863
00:31:47,039 --> 00:31:48,799
that anymore and just hold command and
864
00:31:48,799 --> 00:31:50,960
then hit slash or if you're on windows
865
00:31:50,960 --> 00:31:53,600
probably hit control and then hit slash
866
00:31:53,600 --> 00:31:55,200
okay so let's do that and it should
867
00:31:55,200 --> 00:31:58,000
comment out all of this okay with
868
00:31:58,000 --> 00:31:59,360
exactly how i did on my computer if it
869
00:31:59,360 --> 00:32:01,039
doesn't do that for you just turn it
870
00:32:01,039 --> 00:32:02,880
into a comment block but just make sure
871
00:32:02,880 --> 00:32:04,880
to close the comment block okay because
872
00:32:04,880 --> 00:32:06,480
if you don't close it then everything
873
00:32:06,480 --> 00:32:08,240
will turn green and that's not what you
874
00:32:08,240 --> 00:32:11,200
want either so i'm going to hit undo and
875
00:32:11,200 --> 00:32:14,159
i will just leave it as let's go back up
876
00:32:14,159 --> 00:32:16,000
here and i will just leave it as a
877
00:32:16,000 --> 00:32:18,399
common block like this okay and now
878
00:32:18,399 --> 00:32:20,559
let's go back down to where we were at
879
00:32:20,559 --> 00:32:22,000
all right so one of the things i'll do
880
00:32:22,000 --> 00:32:24,880
now is let's go over to our
881
00:32:24,880 --> 00:32:28,480
console here okay so let's go to boom
882
00:32:28,480 --> 00:32:30,399
let's go to this guy over here okay and
883
00:32:30,399 --> 00:32:33,519
i'm gonna drag hit settings and there's
884
00:32:33,519 --> 00:32:35,360
display size and i'll make the display
885
00:32:35,360 --> 00:32:37,679
size bigger let's go all the way back
886
00:32:37,679 --> 00:32:39,760
hit console perfect so now we have some
887
00:32:39,760 --> 00:32:42,240
more room to see everything here easier
888
00:32:42,240 --> 00:32:43,760
fantastic
889
00:32:43,760 --> 00:32:45,519
all right so
890
00:32:45,519 --> 00:32:48,480
we covered the math methods we covered
891
00:32:48,480 --> 00:32:50,399
math operators we covered some of this
892
00:32:50,399 --> 00:32:52,720
data type stuff right so let's go back
893
00:32:52,720 --> 00:32:54,640
up to our variables here okay so in our
894
00:32:54,640 --> 00:32:57,840
variables introduce to this so you have
895
00:32:57,840 --> 00:32:58,640
var
896
00:32:58,640 --> 00:33:00,799
const and let these are three very
897
00:33:00,799 --> 00:33:02,640
important things i want you to know and
898
00:33:02,640 --> 00:33:04,320
normally when you make a variable you
899
00:33:04,320 --> 00:33:06,240
never just make it naked like this you
900
00:33:06,240 --> 00:33:08,240
always either call it like you say let
901
00:33:08,240 --> 00:33:10,559
sentence equal blah blah blah let so you
902
00:33:10,559 --> 00:33:12,480
always just put a let here so right now
903
00:33:12,480 --> 00:33:14,480
i will just tell you never use var okay
904
00:33:14,480 --> 00:33:16,320
i'll make your life simple you can use
905
00:33:16,320 --> 00:33:19,039
const or let only use const if you know
906
00:33:19,039 --> 00:33:20,399
that thing that you made you're never
907
00:33:20,399 --> 00:33:21,760
gonna change it again but if you're
908
00:33:21,760 --> 00:33:23,600
gonna change it use let so most of the
909
00:33:23,600 --> 00:33:25,919
times for now at your level i will just
910
00:33:25,919 --> 00:33:28,000
say use let okay that way you won't run
911
00:33:28,000 --> 00:33:30,320
into any problems so for simplicity use
912
00:33:30,320 --> 00:33:31,760
let but if you want something to be
913
00:33:31,760 --> 00:33:33,360
constantly true then you're gonna use
914
00:33:33,360 --> 00:33:36,240
const so all our variables just put a
915
00:33:36,240 --> 00:33:39,200
let there and for more advanced users
916
00:33:39,200 --> 00:33:40,960
we're learning javascript what this does
917
00:33:40,960 --> 00:33:43,360
is it makes sure that you don't have a
918
00:33:43,360 --> 00:33:45,760
global scope for your variable names
919
00:33:45,760 --> 00:33:48,080
okay it gives you kind of get scopes it
920
00:33:48,080 --> 00:33:50,640
down so let's go down here now let's go
921
00:33:50,640 --> 00:33:52,799
ahead and like let's do something
922
00:33:52,799 --> 00:33:54,320
simple okay so i'm going to show you
923
00:33:54,320 --> 00:33:55,919
programming in a fun way as if you were
924
00:33:55,919 --> 00:33:57,600
sitting right next to me and we're just
925
00:33:57,600 --> 00:33:59,360
coding up something together okay and
926
00:33:59,360 --> 00:34:01,360
i'm like yo let's walk through this
927
00:34:01,360 --> 00:34:02,559
let's say
928
00:34:02,559 --> 00:34:05,120
we make like a tiny little weather app
929
00:34:05,120 --> 00:34:07,120
okay so let's go ahead and let's make a
930
00:34:07,120 --> 00:34:09,760
baby weather app and uh what we're going
931
00:34:09,760 --> 00:34:12,159
to do is we're going to say if there's
932
00:34:12,159 --> 00:34:14,000
rain then what we want to do is we want
933
00:34:14,000 --> 00:34:17,839
to say grab your umbrella or
934
00:34:17,839 --> 00:34:20,399
just print the umbrella emoji
935
00:34:20,399 --> 00:34:21,760
and if you don't have an emoji that's
936
00:34:21,760 --> 00:34:23,359
fine you don't need an emoji okay this
937
00:34:23,359 --> 00:34:26,079
is just for fun if it's not raining
938
00:34:26,079 --> 00:34:28,719
we're just gonna say wear
939
00:34:28,719 --> 00:34:31,359
your sunglasses okay so it's gonna be a
940
00:34:31,359 --> 00:34:33,918
simple app where the user is going to be
941
00:34:33,918 --> 00:34:36,320
asked if it's raining or what the
942
00:34:36,320 --> 00:34:38,079
weather is and if the user says it's
943
00:34:38,079 --> 00:34:40,159
rain right now then you tell them grab
944
00:34:40,159 --> 00:34:42,560
your umbrella and if the user says
945
00:34:42,560 --> 00:34:44,719
anything else like it's not raining or
946
00:34:44,719 --> 00:34:46,960
it's sunny outside then it says hey wear
947
00:34:46,960 --> 00:34:49,599
your sunglasses okay very very simplest
948
00:34:49,599 --> 00:34:52,719
overly simplistic baby level app but
949
00:34:52,719 --> 00:34:54,800
just to kind of gives you the ropes of
950
00:34:54,800 --> 00:34:56,719
programming okay so let's go ahead and
951
00:34:56,719 --> 00:34:58,480
build this out now and we're going to
952
00:34:58,480 --> 00:35:01,200
introduce you to a concept called
953
00:35:01,200 --> 00:35:03,760
conditionals they're your friend they're
954
00:35:03,760 --> 00:35:05,680
so freaking awesome and they allow you
955
00:35:05,680 --> 00:35:08,000
to build amazing apps and amazing things
956
00:35:08,000 --> 00:35:09,440
all right so i'm going to say let
957
00:35:09,440 --> 00:35:10,320
weather so i'm going to create a
958
00:35:10,320 --> 00:35:12,160
variable called weather so whether it
959
00:35:12,160 --> 00:35:14,480
could be rain it could be
960
00:35:14,480 --> 00:35:16,800
sunny whatever so we'll just say if
961
00:35:16,800 --> 00:35:19,760
weather weather rain then i want you to
962
00:35:19,760 --> 00:35:22,400
console.log grab your umbrella just like
963
00:35:22,400 --> 00:35:23,839
this i'm going to copy this and i'm
964
00:35:23,839 --> 00:35:26,480
going to paste it else we're going to
965
00:35:26,480 --> 00:35:29,119
console.log and then the sentence is
966
00:35:29,119 --> 00:35:30,720
going to be where your sunglasses and
967
00:35:30,720 --> 00:35:32,079
i'm going to paste that right here so
968
00:35:32,079 --> 00:35:34,160
make sure that you copy this correctly
969
00:35:34,160 --> 00:35:36,640
so here you have if weather is equal to
970
00:35:36,640 --> 00:35:38,640
rain with the two equal signs very
971
00:35:38,640 --> 00:35:41,040
important okay and i'll explain that to
972
00:35:41,040 --> 00:35:43,520
you later why as well okay so this is
973
00:35:43,520 --> 00:35:45,839
something called you have conditional
974
00:35:45,839 --> 00:35:49,119
operators and those include conditional
975
00:35:49,119 --> 00:35:51,520
operators include things like equal
976
00:35:51,520 --> 00:35:54,880
equal sign or equal equal equal sign or
977
00:35:54,880 --> 00:35:57,839
greater than sign or less than sign or
978
00:35:57,839 --> 00:36:00,079
less than equal to or greater than or
979
00:36:00,079 --> 00:36:03,839
equal to not equal to not equal equal to
980
00:36:03,839 --> 00:36:05,760
and those are the main ones that you're
981
00:36:05,760 --> 00:36:07,599
going to use okay i'm just leaving this
982
00:36:07,599 --> 00:36:10,160
here for you to know we're using this so
983
00:36:10,160 --> 00:36:11,680
if the weather is rain this is going to
984
00:36:11,680 --> 00:36:14,720
become true if this weather here is rain
985
00:36:14,720 --> 00:36:16,640
so this weather here is rain this is
986
00:36:16,640 --> 00:36:18,160
basically going to say hey is it rain
987
00:36:18,160 --> 00:36:20,079
equal to rain let's test it let's see
988
00:36:20,079 --> 00:36:23,040
what it says so is rain equal equal to
989
00:36:23,040 --> 00:36:24,880
rain and let's hit execute and let's
990
00:36:24,880 --> 00:36:26,800
watch what happens and boom look at that
991
00:36:26,800 --> 00:36:30,560
it says it's true now let's say is sun
992
00:36:30,560 --> 00:36:32,640
equal equal to rain it's going to say
993
00:36:32,640 --> 00:36:35,280
false okay let's try a couple of others
994
00:36:35,280 --> 00:36:37,280
5 greater than 2
995
00:36:37,280 --> 00:36:39,280
is that true or false
996
00:36:39,280 --> 00:36:40,480
all right hopefully you guessed it
997
00:36:40,480 --> 00:36:42,560
correct let's hit it and you can see it
998
00:36:42,560 --> 00:36:46,880
says true okay is 5 greater than or
999
00:36:46,880 --> 00:36:49,839
equal to 5 is this statement true
1000
00:36:49,839 --> 00:36:53,200
greater than or equal to 5 yes it's true
1001
00:36:53,200 --> 00:36:56,240
because 5 is equal to 5 okay greater
1002
00:36:56,240 --> 00:36:58,320
than or equal to but if you do 5 is
1003
00:36:58,320 --> 00:37:01,839
greater than 5 this will give you false
1004
00:37:01,839 --> 00:37:03,839
because 5 is not greater than 5. so
1005
00:37:03,839 --> 00:37:05,200
these are a couple of ones that you
1006
00:37:05,200 --> 00:37:07,520
should know and learn about because this
1007
00:37:07,520 --> 00:37:08,960
is going to be really important for any
1008
00:37:08,960 --> 00:37:10,400
app that you're essentially building
1009
00:37:10,400 --> 00:37:12,560
okay so if the weather is equal to rain
1010
00:37:12,560 --> 00:37:15,520
so if this is rain because this weather
1011
00:37:15,520 --> 00:37:17,680
here is rain if these two are equal
1012
00:37:17,680 --> 00:37:19,200
what's really going to happen is this
1013
00:37:19,200 --> 00:37:21,040
entire thing here is going to evaluate
1014
00:37:21,040 --> 00:37:23,839
to a true statement so this entire thing
1015
00:37:23,839 --> 00:37:25,920
is going to become true and if something
1016
00:37:25,920 --> 00:37:28,560
becomes true an if then statement it
1017
00:37:28,560 --> 00:37:31,040
will run this line right here but let's
1018
00:37:31,040 --> 00:37:33,119
say that you put false here well then
1019
00:37:33,119 --> 00:37:35,040
it's going to run this line right here
1020
00:37:35,040 --> 00:37:37,520
and it's not going to run that line okay
1021
00:37:37,520 --> 00:37:40,320
so that's how if else statements work
1022
00:37:40,320 --> 00:37:41,440
just make sure you have the right
1023
00:37:41,440 --> 00:37:43,520
squigglies so this squiggly opens here
1024
00:37:43,520 --> 00:37:46,240
and closes here that's where your
1025
00:37:46,240 --> 00:37:49,440
true statement goes the l squiggly opens
1026
00:37:49,440 --> 00:37:51,440
here closes here and that's where your
1027
00:37:51,440 --> 00:37:53,520
console log this is where your false
1028
00:37:53,520 --> 00:37:56,079
statement goes so let's hit undo undo
1029
00:37:56,079 --> 00:37:58,800
undo and go weather is equal to rain so
1030
00:37:58,800 --> 00:38:00,560
now what i'm going to do is we're going
1031
00:38:00,560 --> 00:38:02,560
to hit run and let's see if this runs
1032
00:38:02,560 --> 00:38:04,560
how we want it to it says grab your
1033
00:38:04,560 --> 00:38:07,200
umbrella beautiful so because weather is
1034
00:38:07,200 --> 00:38:10,240
rain well what if the weather is sunny
1035
00:38:10,240 --> 00:38:11,839
before i hit run
1036
00:38:11,839 --> 00:38:14,960
do you think it's going to run line 57
1037
00:38:14,960 --> 00:38:17,599
or do you think it's going to run line
1038
00:38:17,599 --> 00:38:20,480
59 which one do you think it's going to
1039
00:38:20,480 --> 00:38:22,160
be take a second
1040
00:38:22,160 --> 00:38:24,560
and let's hit run and let's see what
1041
00:38:24,560 --> 00:38:27,440
happens and boom it will run this line
1042
00:38:27,440 --> 00:38:29,839
because this statement it found it was
1043
00:38:29,839 --> 00:38:34,480
false because sunny is not equal to rain
1044
00:38:34,480 --> 00:38:37,280
so this becomes false so then it won't
1045
00:38:37,280 --> 00:38:39,839
run the true statement it will go ahead
1046
00:38:39,839 --> 00:38:42,560
and run this false statement okay
1047
00:38:42,560 --> 00:38:43,839
under the else
1048
00:38:43,839 --> 00:38:46,320
so if we want to make it a app that a
1049
00:38:46,320 --> 00:38:47,839
user can use
1050
00:38:47,839 --> 00:38:49,680
this input here we will make it
1051
00:38:49,680 --> 00:38:52,000
something the user puts in so remember
1052
00:38:52,000 --> 00:38:54,400
how do we ask a user what they want to
1053
00:38:54,400 --> 00:38:56,079
put in do you remember it starts with a
1054
00:38:56,079 --> 00:38:59,839
p and then goes p r o
1055
00:38:59,839 --> 00:39:01,280
hopefully you remember that was the word
1056
00:39:01,280 --> 00:39:03,040
prompt and we're going to prompt the
1057
00:39:03,040 --> 00:39:05,920
user and we we're going to going to ask
1058
00:39:05,920 --> 00:39:07,599
how is the
1059
00:39:07,599 --> 00:39:10,480
weather you know so if it runs
1060
00:39:10,480 --> 00:39:12,480
boom how's the weather and if we say
1061
00:39:12,480 --> 00:39:13,280
it's
1062
00:39:13,280 --> 00:39:14,480
rain
1063
00:39:14,480 --> 00:39:16,000
you're going to see that it says grab
1064
00:39:16,000 --> 00:39:17,440
your umbrella
1065
00:39:17,440 --> 00:39:20,079
if we say something else let's say it's
1066
00:39:20,079 --> 00:39:22,240
sunny it's going to say wear your
1067
00:39:22,240 --> 00:39:24,480
sunglasses so it's working exactly how
1068
00:39:24,480 --> 00:39:26,800
we want now if the weather is equal to
1069
00:39:26,800 --> 00:39:28,320
rain that doesn't really make sense
1070
00:39:28,320 --> 00:39:30,079
right let's make let's put a y here so
1071
00:39:30,079 --> 00:39:31,760
it checks for for weather is equal to
1072
00:39:31,760 --> 00:39:34,240
rainy so let's run this again
1073
00:39:34,240 --> 00:39:36,560
and now i'll have to put in rainy here
1074
00:39:36,560 --> 00:39:39,280
so let's put rainy and now it will say
1075
00:39:39,280 --> 00:39:41,760
rainy equals rainy this is true and then
1076
00:39:41,760 --> 00:39:44,000
a little console log
1077
00:39:44,000 --> 00:39:46,400
grab your umbrella so we just made a
1078
00:39:46,400 --> 00:39:48,800
mini app that can
1079
00:39:48,800 --> 00:39:51,119
you know fake help you
1080
00:39:51,119 --> 00:39:53,680
with uh what you should do
1081
00:39:53,680 --> 00:39:55,599
based on the weather okay if you wanted
1082
00:39:55,599 --> 00:39:57,440
to make it a fancier app or a more real
1083
00:39:57,440 --> 00:39:59,440
world app you'll be put at pulling the
1084
00:39:59,440 --> 00:40:01,920
weather data from a real app so you'll
1085
00:40:01,920 --> 00:40:03,839
be pulling it from some real app using
1086
00:40:03,839 --> 00:40:05,040
an api
1087
00:40:05,040 --> 00:40:07,440
and then you can detect what the weather
1088
00:40:07,440 --> 00:40:09,520
is automatically and then if it's
1089
00:40:09,520 --> 00:40:10,400
raining
1090
00:40:10,400 --> 00:40:12,000
instead of console log grab your
1091
00:40:12,000 --> 00:40:13,760
umbrella instead of this you could have
1092
00:40:13,760 --> 00:40:15,680
it send you a text message on your phone
1093
00:40:15,680 --> 00:40:18,079
saying hey it's going to rain today take
1094
00:40:18,079 --> 00:40:19,760
your jacket with you so you could use
1095
00:40:19,760 --> 00:40:22,400
twilio to send you literally a text
1096
00:40:22,400 --> 00:40:25,760
message sms on your phone so that's
1097
00:40:25,760 --> 00:40:27,920
freaking mind-blowing right so that's
1098
00:40:27,920 --> 00:40:30,480
really the benefit of uh learning this
1099
00:40:30,480 --> 00:40:32,720
and right now i know these are like baby
1100
00:40:32,720 --> 00:40:34,079
applications we're making and you're
1101
00:40:34,079 --> 00:40:35,680
look oh i just want to make something
1102
00:40:35,680 --> 00:40:37,839
bigger please hold on we're going to get
1103
00:40:37,839 --> 00:40:38,960
to it we're going to build a lot of
1104
00:40:38,960 --> 00:40:41,760
amazing applications but let's start off
1105
00:40:41,760 --> 00:40:43,920
and keep it simple and so far you've
1106
00:40:43,920 --> 00:40:46,400
done a fantastic job let's keep this
1107
00:40:46,400 --> 00:40:48,480
going all right welcome back so we're
1108
00:40:48,480 --> 00:40:50,720
going to talk about functions now you we
1109
00:40:50,720 --> 00:40:52,960
just got done making this baby app what
1110
00:40:52,960 --> 00:40:56,000
we're gonna do now is i'm going to just
1111
00:40:56,000 --> 00:40:58,720
essentially comment out this entire app
1112
00:40:58,720 --> 00:41:00,160
okay because we don't need it all right
1113
00:41:00,160 --> 00:41:01,839
now that it's commented out i do want to
1114
00:41:01,839 --> 00:41:03,599
let you know that on the left hand side
1115
00:41:03,599 --> 00:41:05,760
when you're using this rupple there will
1116
00:41:05,760 --> 00:41:07,839
be something here and right now this
1117
00:41:07,839 --> 00:41:09,680
folder is called notes it might change
1118
00:41:09,680 --> 00:41:11,280
later but what you'll be able to do is
1119
00:41:11,280 --> 00:41:13,760
you'll be able to go to this folder and
1120
00:41:13,760 --> 00:41:15,680
pretty much everything that we cover in
1121
00:41:15,680 --> 00:41:18,079
our playground will be kind of organized
1122
00:41:18,079 --> 00:41:20,560
here as notes so even though it's like
1123
00:41:20,560 --> 00:41:22,800
commented out here and kind of hard to
1124
00:41:22,800 --> 00:41:24,000
see because this file is going to get
1125
00:41:24,000 --> 00:41:25,839
really messy as we start to code and
1126
00:41:25,839 --> 00:41:27,520
build more and more things here you can
1127
00:41:27,520 --> 00:41:29,760
go here miniweatherapp.js which
1128
00:41:29,760 --> 00:41:32,000
essentially has that exact app that we
1129
00:41:32,000 --> 00:41:33,440
created and it's just kind of sitting
1130
00:41:33,440 --> 00:41:36,400
here for your use okay so this code will
1131
00:41:36,400 --> 00:41:39,520
always be there and available for you
1132
00:41:39,520 --> 00:41:42,079
i'm gonna go back to my playground here
1133
00:41:42,079 --> 00:41:43,760
but if you're trying to code along
1134
00:41:43,760 --> 00:41:45,920
you'll be going to your playground okay
1135
00:41:45,920 --> 00:41:48,560
this is going to be your playground so
1136
00:41:48,560 --> 00:41:50,480
make sure that you go to this all right
1137
00:41:50,480 --> 00:41:53,119
so let's talk about functions because
1138
00:41:53,119 --> 00:41:55,680
functions are a pretty important part of
1139
00:41:55,680 --> 00:41:57,839
any programming language and for
1140
00:41:57,839 --> 00:42:01,440
javascript they are massive so
1141
00:42:01,440 --> 00:42:03,119
let's go ahead and learn a little bit
1142
00:42:03,119 --> 00:42:05,520
about functions how do they work well
1143
00:42:05,520 --> 00:42:08,079
functions are designed to basically run
1144
00:42:08,079 --> 00:42:10,640
a recipe so they can run multiple things
1145
00:42:10,640 --> 00:42:14,319
for you at once and it also organizes
1146
00:42:14,319 --> 00:42:16,800
your code in a nice little way okay and
1147
00:42:16,800 --> 00:42:19,200
so functions make your life a lot easier
1148
00:42:19,200 --> 00:42:21,280
so let's go back here so you see how
1149
00:42:21,280 --> 00:42:23,440
when you create a variable like a
1150
00:42:23,440 --> 00:42:26,079
variable it can only store
1151
00:42:26,079 --> 00:42:28,079
like a name or something along those
1152
00:42:28,079 --> 00:42:30,480
lines what's cool about a function is a
1153
00:42:30,480 --> 00:42:32,880
function can store
1154
00:42:32,880 --> 00:42:34,640
blocks of code
1155
00:42:34,640 --> 00:42:37,680
so for example remember this food app
1156
00:42:37,680 --> 00:42:39,119
that we made earlier i'm just going to
1157
00:42:39,119 --> 00:42:41,599
go ahead and comment this out this app
1158
00:42:41,599 --> 00:42:43,440
that we made earlier i mean this is a
1159
00:42:43,440 --> 00:42:45,040
lot of lines of code and if i ever want
1160
00:42:45,040 --> 00:42:47,680
to run this app i have to always
1161
00:42:47,680 --> 00:42:50,160
uncomment these lines of code or
1162
00:42:50,160 --> 00:42:52,319
basically write all of these lines of
1163
00:42:52,319 --> 00:42:54,640
code every single time i want to run it
1164
00:42:54,640 --> 00:42:55,760
whereas
1165
00:42:55,760 --> 00:42:58,160
imagine that it was in a function well
1166
00:42:58,160 --> 00:43:01,440
then in order to run this piece of code
1167
00:43:01,440 --> 00:43:02,880
all i would have to say is i wouldn't
1168
00:43:02,880 --> 00:43:04,560
have to uncomment all of those lines up
1169
00:43:04,560 --> 00:43:07,359
there all i would have to say instead is
1170
00:43:07,359 --> 00:43:10,240
i could just go run a function that you
1171
00:43:10,240 --> 00:43:11,280
know says
1172
00:43:11,280 --> 00:43:14,480
calculate tip amount and then i would
1173
00:43:14,480 --> 00:43:17,280
just give it its tip because okay so
1174
00:43:17,280 --> 00:43:19,760
let's say that there was a tip or you
1175
00:43:19,760 --> 00:43:21,280
know maybe the amount of food was a
1176
00:43:21,280 --> 00:43:23,599
hundred dollars i just gave it that and
1177
00:43:23,599 --> 00:43:26,079
then the entire app automatically runs
1178
00:43:26,079 --> 00:43:28,480
just by me typing in this instead of
1179
00:43:28,480 --> 00:43:30,640
that entire chunk of code so that's
1180
00:43:30,640 --> 00:43:32,079
where function comes in so let's go
1181
00:43:32,079 --> 00:43:34,319
ahead comment out this entire block
1182
00:43:34,319 --> 00:43:36,720
again with command slash or control
1183
00:43:36,720 --> 00:43:39,040
slash and i'm going to go down here i'm
1184
00:43:39,040 --> 00:43:40,960
gonna remove this line i'm gonna start
1185
00:43:40,960 --> 00:43:42,880
writing our first function
1186
00:43:42,880 --> 00:43:44,880
so i'll write a first function what this
1187
00:43:44,880 --> 00:43:49,520
will do is it will make a greeting okay
1188
00:43:49,520 --> 00:43:51,920
so or say my name so let's do a say my
1189
00:43:51,920 --> 00:43:54,079
name function okay so let's do say my
1190
00:43:54,079 --> 00:43:54,880
name
1191
00:43:54,880 --> 00:43:58,440
and say my name function it will do
1192
00:43:58,440 --> 00:44:01,200
console.log and it'll just say qazi
1193
00:44:01,200 --> 00:44:03,760
because my name is qazi okay so that's
1194
00:44:03,760 --> 00:44:05,599
all it's going to say well let's go down
1195
00:44:05,599 --> 00:44:07,839
here and i'm going to call and i'm going
1196
00:44:07,839 --> 00:44:10,720
to hit run and you're going to see that
1197
00:44:10,720 --> 00:44:13,200
nothing really happened right it didn't
1198
00:44:13,200 --> 00:44:15,520
actually do anything and to make things
1199
00:44:15,520 --> 00:44:18,000
less confusing let's also comment out
1200
00:44:18,000 --> 00:44:19,839
everything that we have here so we just
1201
00:44:19,839 --> 00:44:21,839
have everything commented out i'll run
1202
00:44:21,839 --> 00:44:24,880
perfect so we see nothing in our console
1203
00:44:24,880 --> 00:44:26,400
nothing happened well why didn't it do
1204
00:44:26,400 --> 00:44:28,800
this so even though i made this function
1205
00:44:28,800 --> 00:44:32,240
i have to call this function like this
1206
00:44:32,240 --> 00:44:33,839
and now when i hit run watch what
1207
00:44:33,839 --> 00:44:37,119
happens boom you can see that it says
1208
00:44:37,119 --> 00:44:40,160
qazi right over here awesome now let's
1209
00:44:40,160 --> 00:44:42,640
go on and you know we've done a pretty
1210
00:44:42,640 --> 00:44:44,640
good job with this function say my name
1211
00:44:44,640 --> 00:44:46,480
and look at what it does though before
1212
00:44:46,480 --> 00:44:48,720
we continue and move on this is how you
1213
00:44:48,720 --> 00:44:51,359
call a function so unlike a variable
1214
00:44:51,359 --> 00:44:53,359
when you call a function you put open
1215
00:44:53,359 --> 00:44:56,640
and close parens in front of it okay now
1216
00:44:56,640 --> 00:44:58,000
this function
1217
00:44:58,000 --> 00:45:01,280
what it the name of it is say my name or
1218
00:45:01,280 --> 00:45:03,920
the variable of this function right is
1219
00:45:03,920 --> 00:45:06,400
say my name this is the name of it and
1220
00:45:06,400 --> 00:45:08,560
when you call it it runs all the
1221
00:45:08,560 --> 00:45:10,800
instructions that are inside of that
1222
00:45:10,800 --> 00:45:13,040
block okay so for example what does that
1223
00:45:13,040 --> 00:45:15,920
mean what if i had like console.log
1224
00:45:15,920 --> 00:45:18,640
seller name and say brian
1225
00:45:18,640 --> 00:45:21,359
and let's also have it say banana
1226
00:45:21,359 --> 00:45:23,520
whatever right if i run this you're
1227
00:45:23,520 --> 00:45:25,839
gonna notice that by just calling the
1228
00:45:25,839 --> 00:45:27,920
function it is running all these four
1229
00:45:27,920 --> 00:45:30,560
lines of code and it runs them all right
1230
00:45:30,560 --> 00:45:32,319
see that and then watch what happens if
1231
00:45:32,319 --> 00:45:34,000
i just comment this out and i run it
1232
00:45:34,000 --> 00:45:35,280
boom nothing
1233
00:45:35,280 --> 00:45:38,000
so now all we have to do to run our code
1234
00:45:38,000 --> 00:45:39,440
is we don't have to comment or uncomment
1235
00:45:39,440 --> 00:45:41,680
the entire thing we can actually just
1236
00:45:41,680 --> 00:45:43,920
comment or uncomment that call of the
1237
00:45:43,920 --> 00:45:45,599
function okay this is called
1238
00:45:45,599 --> 00:45:47,920
calling your function so whenever i say
1239
00:45:47,920 --> 00:45:49,839
call your function that basically means
1240
00:45:49,839 --> 00:45:51,599
you write the name of the function with
1241
00:45:51,599 --> 00:45:53,680
open close parens cool
1242
00:45:53,680 --> 00:45:54,640
now
1243
00:45:54,640 --> 00:45:56,880
let's say that we want this function to
1244
00:45:56,880 --> 00:45:58,800
be more dynamic because right now it's
1245
00:45:58,800 --> 00:46:00,720
just saying quasi-raphael brian banana
1246
00:46:00,720 --> 00:46:03,839
so it will never actually say your name
1247
00:46:03,839 --> 00:46:04,640
right
1248
00:46:04,640 --> 00:46:07,520
so what how can we handle that how we
1249
00:46:07,520 --> 00:46:09,119
can handle that is actually let's remove
1250
00:46:09,119 --> 00:46:10,880
all of these lines right
1251
00:46:10,880 --> 00:46:11,920
and
1252
00:46:11,920 --> 00:46:13,680
we're gonna say hey we want you to say
1253
00:46:13,680 --> 00:46:15,920
like a dynamic name that the user gives
1254
00:46:15,920 --> 00:46:18,240
you okay well it's gonna go hey i don't
1255
00:46:18,240 --> 00:46:19,920
know what name is in this case what
1256
00:46:19,920 --> 00:46:21,119
we're gonna do is we're gonna also put
1257
00:46:21,119 --> 00:46:24,720
that here so this function say my name
1258
00:46:24,720 --> 00:46:26,880
okay has an argument
1259
00:46:26,880 --> 00:46:30,400
called name and it's logging that
1260
00:46:30,400 --> 00:46:33,359
that argument out okay so then if i say
1261
00:46:33,359 --> 00:46:35,680
quasi here and i run this
1262
00:46:35,680 --> 00:46:37,440
it's going to print out quasi it's going
1263
00:46:37,440 --> 00:46:40,160
to log out quasi but what if i say lance
1264
00:46:40,160 --> 00:46:42,640
here what is what is it going to do
1265
00:46:42,640 --> 00:46:44,079
let's try it
1266
00:46:44,079 --> 00:46:46,319
and boom lance because i call this
1267
00:46:46,319 --> 00:46:48,000
function okay
1268
00:46:48,000 --> 00:46:50,560
and here's how it works it goes here and
1269
00:46:50,560 --> 00:46:52,640
it puts in the name lance
1270
00:46:52,640 --> 00:46:54,720
and then it goes here and puts in the
1271
00:46:54,720 --> 00:46:57,200
name lance and logs it out to the
1272
00:46:57,200 --> 00:46:59,119
console that's how
1273
00:46:59,119 --> 00:47:00,400
a function
1274
00:47:00,400 --> 00:47:03,200
works okay this is a function with one
1275
00:47:03,200 --> 00:47:05,200
argument so let's if i take you back to
1276
00:47:05,200 --> 00:47:07,599
our older function that function has no
1277
00:47:07,599 --> 00:47:10,319
arguments okay let's make let's copy
1278
00:47:10,319 --> 00:47:13,040
this and let's paste this here okay and
1279
00:47:13,040 --> 00:47:15,920
then let's make this the dynamic one
1280
00:47:15,920 --> 00:47:16,800
all right
1281
00:47:16,800 --> 00:47:19,599
say my name yeah so this is say my name
1282
00:47:19,599 --> 00:47:21,520
with no arguments this is say my name
1283
00:47:21,520 --> 00:47:23,119
with an argument okay let's give them
1284
00:47:23,119 --> 00:47:25,520
different names maybe so this is say my
1285
00:47:25,520 --> 00:47:27,599
name maybe let's just call it say my
1286
00:47:27,599 --> 00:47:30,079
name two for now okay to call this
1287
00:47:30,079 --> 00:47:31,680
function what are you gonna have to do
1288
00:47:31,680 --> 00:47:33,520
so if you wanna call this function right
1289
00:47:33,520 --> 00:47:35,440
here say my name to
1290
00:47:35,440 --> 00:47:36,800
how will you
1291
00:47:36,800 --> 00:47:39,599
call it okay take five seconds and see
1292
00:47:39,599 --> 00:47:41,359
if you could call it okay and i'll give
1293
00:47:41,359 --> 00:47:43,200
you a hint it's what you did on line
1294
00:47:43,200 --> 00:47:44,400
seventy
1295
00:47:44,400 --> 00:47:45,440
five
1296
00:47:45,440 --> 00:47:48,480
four three two one
1297
00:47:48,480 --> 00:47:50,000
all right let's do it so i'm gonna say
1298
00:47:50,000 --> 00:47:51,520
say my name
1299
00:47:51,520 --> 00:47:52,400
to
1300
00:47:52,400 --> 00:47:54,000
and in this i'm actually going to give
1301
00:47:54,000 --> 00:47:55,760
it a name i'm going to give it an
1302
00:47:55,760 --> 00:47:57,839
argument
1303
00:47:57,839 --> 00:48:00,240
and you can see that now it will print
1304
00:48:00,240 --> 00:48:02,720
out kazur if i say kevin
1305
00:48:02,720 --> 00:48:04,480
and run it you'll see that it'll
1306
00:48:04,480 --> 00:48:06,880
actually say kevin right here okay and
1307
00:48:06,880 --> 00:48:09,200
we'll comment outline 70 where it says
1308
00:48:09,200 --> 00:48:11,520
say my name because we're just running
1309
00:48:11,520 --> 00:48:13,520
the function that we wrote on line 72
1310
00:48:13,520 --> 00:48:15,520
and there's a nice little toggle that
1311
00:48:15,520 --> 00:48:17,359
you could toggle to
1312
00:48:17,359 --> 00:48:19,119
and it'll basically just
1313
00:48:19,119 --> 00:48:21,200
collapse everything that you have inside
1314
00:48:21,200 --> 00:48:22,960
of the function okay
1315
00:48:22,960 --> 00:48:25,520
so that's kind of a nice way to do
1316
00:48:25,520 --> 00:48:27,359
things so moving forward pretty much
1317
00:48:27,359 --> 00:48:29,200
everything that we do
1318
00:48:29,200 --> 00:48:31,520
will majority of the times do it in a
1319
00:48:31,520 --> 00:48:34,079
function it keeps your code clean
1320
00:48:34,079 --> 00:48:36,800
concise compact and it's just a better
1321
00:48:36,800 --> 00:48:38,960
way to do things and we'll we're now
1322
00:48:38,960 --> 00:48:40,319
going to step it up
1323
00:48:40,319 --> 00:48:42,960
and make things that are a little bit
1324
00:48:42,960 --> 00:48:44,960
more advanced and do them with the
1325
00:48:44,960 --> 00:48:46,880
function okay so let's do that next so
1326
00:48:46,880 --> 00:48:48,880
i'm gonna comment out this line here say
1327
00:48:48,880 --> 00:48:49,920
my name
1328
00:48:49,920 --> 00:48:51,760
two okay i'm gonna comment that out
1329
00:48:51,760 --> 00:48:53,119
let's go down let's make another
1330
00:48:53,119 --> 00:48:54,000
function
1331
00:48:54,000 --> 00:48:56,559
and we'll call this function greeting
1332
00:48:56,559 --> 00:48:58,640
okay so greeting
1333
00:48:58,640 --> 00:49:00,480
will take in
1334
00:49:00,480 --> 00:49:01,520
um
1335
00:49:01,520 --> 00:49:02,640
your name
1336
00:49:02,640 --> 00:49:03,839
okay
1337
00:49:03,839 --> 00:49:06,240
it'll ask the user it'll basically need
1338
00:49:06,240 --> 00:49:08,000
the name of the user and then what
1339
00:49:08,000 --> 00:49:10,000
greeting will do is it will say well
1340
00:49:10,000 --> 00:49:12,559
console.log it okay and it'll basically
1341
00:49:12,559 --> 00:49:15,359
say hi
1342
00:49:15,359 --> 00:49:16,559
name
1343
00:49:16,559 --> 00:49:19,200
nice to meet you okay
1344
00:49:19,200 --> 00:49:21,760
and if i run greeting so watch what's
1345
00:49:21,760 --> 00:49:22,960
going to happen so let's say i run
1346
00:49:22,960 --> 00:49:24,480
greeting and i give it my name and i say
1347
00:49:24,480 --> 00:49:26,640
my name is quasi and let's run it and
1348
00:49:26,640 --> 00:49:30,240
let's watch what happens okay it says hi
1349
00:49:30,240 --> 00:49:32,160
nice to meet you
1350
00:49:32,160 --> 00:49:33,839
hi name nice to meet you but that's
1351
00:49:33,839 --> 00:49:36,720
weird it says name here and it
1352
00:49:36,720 --> 00:49:38,240
says exactly name that's not what i
1353
00:49:38,240 --> 00:49:40,000
wanted to say i wanted to say qazi so
1354
00:49:40,000 --> 00:49:41,599
how do i do that
1355
00:49:41,599 --> 00:49:43,119
um
1356
00:49:43,119 --> 00:49:45,359
what we're going to say is
1357
00:49:45,359 --> 00:49:46,800
hi
1358
00:49:46,800 --> 00:49:48,319
plus
1359
00:49:48,319 --> 00:49:49,280
name
1360
00:49:49,280 --> 00:49:50,800
okay and watch what happens so that
1361
00:49:50,800 --> 00:49:52,640
allows you to concatenate strings
1362
00:49:52,640 --> 00:49:54,400
together the plus sign so watch if i
1363
00:49:54,400 --> 00:49:55,839
have hi
1364
00:49:55,839 --> 00:49:58,079
and then i put a plus sign here and i
1365
00:49:58,079 --> 00:49:58,800
say
1366
00:49:58,800 --> 00:49:59,839
kazi
1367
00:49:59,839 --> 00:50:01,760
and if i hit execute you can see it says
1368
00:50:01,760 --> 00:50:03,119
hi kazi
1369
00:50:03,119 --> 00:50:04,720
but it's saying it right next to each
1370
00:50:04,720 --> 00:50:06,240
other so we want to have a little space
1371
00:50:06,240 --> 00:50:08,720
in there so i'm going to say hi space
1372
00:50:08,720 --> 00:50:11,920
plus kazi like this okay so this is high
1373
00:50:11,920 --> 00:50:13,920
and then one empty letter empty space
1374
00:50:13,920 --> 00:50:16,400
here hit execute now it says high quasi
1375
00:50:16,400 --> 00:50:18,000
with the space
1376
00:50:18,000 --> 00:50:19,200
in the middle
1377
00:50:19,200 --> 00:50:20,079
okay
1378
00:50:20,079 --> 00:50:21,119
perfect
1379
00:50:21,119 --> 00:50:22,000
so
1380
00:50:22,000 --> 00:50:23,680
we're gonna go hi
1381
00:50:23,680 --> 00:50:26,960
space plus name plus
1382
00:50:26,960 --> 00:50:28,880
all of this stuff that we wrote here
1383
00:50:28,880 --> 00:50:31,520
comma nice to meet you so let's put that
1384
00:50:31,520 --> 00:50:34,000
there okay this will concatenate and get
1385
00:50:34,000 --> 00:50:35,920
our greeting ready so we'll say greeting
1386
00:50:35,920 --> 00:50:37,280
is equal to
1387
00:50:37,280 --> 00:50:40,960
or we'll just say greet is equal to that
1388
00:50:40,960 --> 00:50:43,599
okay and then in console log we'll just
1389
00:50:43,599 --> 00:50:45,920
print out whatever greed is so let's hit
1390
00:50:45,920 --> 00:50:48,480
run and you can say hi kazi nice to meet
1391
00:50:48,480 --> 00:50:50,000
you well what if i change my name to
1392
00:50:50,000 --> 00:50:51,040
lance
1393
00:50:51,040 --> 00:50:52,720
it will go
1394
00:50:52,720 --> 00:50:55,440
high lance nice to meet you so it's
1395
00:50:55,440 --> 00:50:58,160
working as expected that's really good
1396
00:50:58,160 --> 00:51:00,800
but it still doesn't look super
1397
00:51:00,800 --> 00:51:02,720
nice how this is with the plus sign and
1398
00:51:02,720 --> 00:51:04,000
it's kind of annoying you have to like
1399
00:51:04,000 --> 00:51:06,319
put this space there and it just feels
1400
00:51:06,319 --> 00:51:07,680
unnatural so
1401
00:51:07,680 --> 00:51:09,760
this is why i want to show you template
1402
00:51:09,760 --> 00:51:11,119
literals because they make your life a
1403
00:51:11,119 --> 00:51:14,319
lot easier so instead of this code
1404
00:51:14,319 --> 00:51:15,839
saying greedy is equal to blah blah
1405
00:51:15,839 --> 00:51:17,760
let's write it in template template
1406
00:51:17,760 --> 00:51:20,640
literals okay so let's go greet equals
1407
00:51:20,640 --> 00:51:22,559
and when you do template literals you
1408
00:51:22,559 --> 00:51:24,559
always have to do these backticks on
1409
00:51:24,559 --> 00:51:26,400
your computer okay so you always start
1410
00:51:26,400 --> 00:51:28,480
off with the backticks okay so let's put
1411
00:51:28,480 --> 00:51:30,800
a backtick here so template literals
1412
00:51:30,800 --> 00:51:32,640
now what i'm going to say is
1413
00:51:32,640 --> 00:51:33,680
hi
1414
00:51:33,680 --> 00:51:37,680
name comma nice to meet you and we can
1415
00:51:37,680 --> 00:51:39,440
make this name dynamic and how we're
1416
00:51:39,440 --> 00:51:40,720
going to do that we're going to say this
1417
00:51:40,720 --> 00:51:42,880
name is a javascript part it's not a
1418
00:51:42,880 --> 00:51:44,559
literal string so how we're going to do
1419
00:51:44,559 --> 00:51:46,800
that is we're going to wrap it in the
1420
00:51:46,800 --> 00:51:48,480
squigglies and we're going to put a
1421
00:51:48,480 --> 00:51:50,960
dollar sign whatever is wrapped inside
1422
00:51:50,960 --> 00:51:52,720
of the squigglies followed by a dollar
1423
00:51:52,720 --> 00:51:55,280
sign it will understand as actual
1424
00:51:55,280 --> 00:51:58,319
javascript okay meaning that now it's
1425
00:51:58,319 --> 00:52:00,079
going to put this whole thing together
1426
00:52:00,079 --> 00:52:01,839
kind of like how we were doing here but
1427
00:52:01,839 --> 00:52:04,079
it this just does it in a much nicer way
1428
00:52:04,079 --> 00:52:05,839
so now let's do console log great and
1429
00:52:05,839 --> 00:52:06,960
watch what happens so i'm going to hit
1430
00:52:06,960 --> 00:52:09,920
run hi lance nice to meet you what if i
1431
00:52:09,920 --> 00:52:11,119
say
1432
00:52:11,119 --> 00:52:13,440
amber heard haha
1433
00:52:13,440 --> 00:52:14,960
you guys saw what has happened right the
1434
00:52:14,960 --> 00:52:18,800
news is out or johnny depp okay let's go
1435
00:52:18,800 --> 00:52:20,960
here by the way which side were you on
1436
00:52:20,960 --> 00:52:22,640
were you on amber heard side or johnny
1437
00:52:22,640 --> 00:52:24,960
depp site comment below let me know i'm
1438
00:52:24,960 --> 00:52:26,960
curious to know your thoughts i think
1439
00:52:26,960 --> 00:52:27,839
that
1440
00:52:27,839 --> 00:52:30,640
she kind of deserved to lose and um
1441
00:52:30,640 --> 00:52:33,040
you know it's nice that johnny got
1442
00:52:33,040 --> 00:52:34,800
he was awarded 10 million dollars she
1443
00:52:34,800 --> 00:52:36,880
was awarded 2 million
1444
00:52:36,880 --> 00:52:40,480
so basically she owes johnny depp 8
1445
00:52:40,480 --> 00:52:42,880
million dollars which is more than her
1446
00:52:42,880 --> 00:52:44,720
net worth so
1447
00:52:44,720 --> 00:52:46,400
she's not gonna have a lot of fun paying
1448
00:52:46,400 --> 00:52:49,040
that off uh but regardless that's
1449
00:52:49,040 --> 00:52:51,119
neither here nor there
1450
00:52:51,119 --> 00:52:53,280
let's get back to what you came here for
1451
00:52:53,280 --> 00:52:55,280
so going back to this okay i got my
1452
00:52:55,280 --> 00:52:57,200
greeting i can do johnny depp so it's
1453
00:52:57,200 --> 00:52:59,440
like hi johnny depp nice to meet you so
1454
00:52:59,440 --> 00:53:01,359
hopefully you understand how this
1455
00:53:01,359 --> 00:53:03,520
function is working so let's go to the
1456
00:53:03,520 --> 00:53:06,160
semi name function right this is a
1457
00:53:06,160 --> 00:53:10,480
function called say my name and it has
1458
00:53:10,480 --> 00:53:13,359
no arguments okay in other words it has
1459
00:53:13,359 --> 00:53:15,680
zero arguments so i want you to also
1460
00:53:15,680 --> 00:53:17,599
learn how to speak the lingo because
1461
00:53:17,599 --> 00:53:19,599
you're gonna start hearing this a lot uh
1462
00:53:19,599 --> 00:53:22,640
you know write a function call blah blah
1463
00:53:22,640 --> 00:53:24,880
and it should have x arguments so this
1464
00:53:24,880 --> 00:53:26,559
is it's a function called say my name
1465
00:53:26,559 --> 00:53:28,400
and has zero arguments okay
1466
00:53:28,400 --> 00:53:30,160
so i want you to write out the sentence
1467
00:53:30,160 --> 00:53:32,480
here for this one say my name too so
1468
00:53:32,480 --> 00:53:34,559
write out a sentence exactly in this
1469
00:53:34,559 --> 00:53:35,599
format
1470
00:53:35,599 --> 00:53:37,680
that talks about this and you could even
1471
00:53:37,680 --> 00:53:39,920
take this a step further and say uh like
1472
00:53:39,920 --> 00:53:41,280
what does this function do and you can
1473
00:53:41,280 --> 00:53:42,960
say it
1474
00:53:42,960 --> 00:53:44,480
prints out
1475
00:53:44,480 --> 00:53:48,400
your name to the console or it logs out
1476
00:53:48,400 --> 00:53:50,160
i'm just from the python world a lot of
1477
00:53:50,160 --> 00:53:52,480
the a lot of my life and so you say
1478
00:53:52,480 --> 00:53:54,319
print there a lot so it logs out your
1479
00:53:54,319 --> 00:53:56,319
name to the console okay so that's it
1480
00:53:56,319 --> 00:53:59,280
okay console.log quasi and so this is
1481
00:53:59,280 --> 00:54:01,520
like what the function does okay so it
1482
00:54:01,520 --> 00:54:03,760
does this is what it does
1483
00:54:03,760 --> 00:54:06,640
and you could write for semi name two in
1484
00:54:06,640 --> 00:54:08,559
the exact thing okay what it is and what
1485
00:54:08,559 --> 00:54:10,720
does it do actually try to pause my
1486
00:54:10,720 --> 00:54:12,079
video so you can write it in english
1487
00:54:12,079 --> 00:54:13,839
what you think this does the lingo is
1488
00:54:13,839 --> 00:54:15,520
important to understand okay so in chess
1489
00:54:15,520 --> 00:54:16,880
you have to learn something called chest
1490
00:54:16,880 --> 00:54:18,640
notation this is kind of like that but
1491
00:54:18,640 --> 00:54:20,480
for coding so then we have a language
1492
00:54:20,480 --> 00:54:22,160
that we can build upon and get better
1493
00:54:22,160 --> 00:54:23,680
every single time so take about five to
1494
00:54:23,680 --> 00:54:25,680
ten seconds i'll count down now or just
1495
00:54:25,680 --> 00:54:29,599
pause the video five four three two one
1496
00:54:29,599 --> 00:54:31,280
cool so here's what we're going to do
1497
00:54:31,280 --> 00:54:33,680
here this is a function
1498
00:54:33,680 --> 00:54:36,400
called okay the name of this function is
1499
00:54:36,400 --> 00:54:37,359
say
1500
00:54:37,359 --> 00:54:40,079
my name to it has
1501
00:54:40,079 --> 00:54:42,640
one argument and if you want to be extra
1502
00:54:42,640 --> 00:54:44,319
special and so you could say it has one
1503
00:54:44,319 --> 00:54:46,559
argument called name and then what does
1504
00:54:46,559 --> 00:54:47,680
it do
1505
00:54:47,680 --> 00:54:51,839
it logs out your name to console so it's
1506
00:54:51,839 --> 00:54:53,680
a good habit to get in
1507
00:54:53,680 --> 00:54:56,640
in later in life this habit if you keep
1508
00:54:56,640 --> 00:54:59,119
doing this right now will translate to
1509
00:54:59,119 --> 00:55:01,520
you writing documentation for your code
1510
00:55:01,520 --> 00:55:03,440
so one is gonna help you develop just a
1511
00:55:03,440 --> 00:55:05,760
better skill and two it's gonna help you
1512
00:55:05,760 --> 00:55:07,440
understand and develop the skill of
1513
00:55:07,440 --> 00:55:09,760
documentation and reading and
1514
00:55:09,760 --> 00:55:11,440
understanding the documentation which is
1515
00:55:11,440 --> 00:55:12,960
gonna be really valuable when you get a
1516
00:55:12,960 --> 00:55:14,799
job as a developer because you're gonna
1517
00:55:14,799 --> 00:55:16,160
need to know these things and that makes
1518
00:55:16,160 --> 00:55:18,000
you a lot more valuable that's what we
1519
00:55:18,000 --> 00:55:19,599
did for greeting okay this is what we
1520
00:55:19,599 --> 00:55:21,359
did for this greeting we introduced you
1521
00:55:21,359 --> 00:55:23,760
to the concept of template literals and
1522
00:55:23,760 --> 00:55:25,440
this is going to be very helpful for you
1523
00:55:25,440 --> 00:55:26,799
especially when you're making a couple
1524
00:55:26,799 --> 00:55:28,720
of games javascript games that we're
1525
00:55:28,720 --> 00:55:30,400
going to show you later like for example
1526
00:55:30,400 --> 00:55:32,720
rock paper scissors all right so let's
1527
00:55:32,720 --> 00:55:35,440
go ahead and move forward now all right
1528
00:55:35,440 --> 00:55:36,799
now we're going to create another
1529
00:55:36,799 --> 00:55:39,200
function okay together so because i want
1530
00:55:39,200 --> 00:55:40,880
you to constantly get a lot of practice
1531
00:55:40,880 --> 00:55:43,760
with functions okay and this function is
1532
00:55:43,760 --> 00:55:45,280
going to have multiple arguments so you
1533
00:55:45,280 --> 00:55:46,960
can actually have more arguments than
1534
00:55:46,960 --> 00:55:49,040
one okay so you can have one argument or
1535
00:55:49,040 --> 00:55:52,079
two arguments um or 10 arguments or 20
1536
00:55:52,079 --> 00:55:53,520
however many you want but generally
1537
00:55:53,520 --> 00:55:56,480
between one to five is like pretty you
1538
00:55:56,480 --> 00:55:57,200
know
1539
00:55:57,200 --> 00:55:59,119
reasonable and you don't really want to
1540
00:55:59,119 --> 00:56:01,119
add more arguments just for the sake of
1541
00:56:01,119 --> 00:56:02,880
it so let's go function and we're going
1542
00:56:02,880 --> 00:56:04,559
to create a function called sum and i
1543
00:56:04,559 --> 00:56:06,400
want you to try this out so it's like if
1544
00:56:06,400 --> 00:56:08,880
i want to have a function sum and it
1545
00:56:08,880 --> 00:56:11,440
will have two numbers that it'll take in
1546
00:56:11,440 --> 00:56:13,520
as an input okay so it'll have two
1547
00:56:13,520 --> 00:56:16,240
numbers that'll taken as an input a and
1548
00:56:16,240 --> 00:56:18,880
b and what this function should do is
1549
00:56:18,880 --> 00:56:21,119
return the sum of these two numbers so
1550
00:56:21,119 --> 00:56:22,960
you'll also be introduced to the concept
1551
00:56:22,960 --> 00:56:25,200
of the keyword called return okay
1552
00:56:25,200 --> 00:56:27,359
there's a special keyword in javascript
1553
00:56:27,359 --> 00:56:29,520
and generally with your functions you
1554
00:56:29,520 --> 00:56:31,119
never you don't really want them to
1555
00:56:31,119 --> 00:56:32,799
console log things you want them to
1556
00:56:32,799 --> 00:56:34,960
return things so you can do something
1557
00:56:34,960 --> 00:56:37,440
with them later so we're gonna start
1558
00:56:37,440 --> 00:56:39,359
getting in the habit of return and then
1559
00:56:39,359 --> 00:56:41,200
eventually you'll start seeing why
1560
00:56:41,200 --> 00:56:42,880
returns are actually helpful and why
1561
00:56:42,880 --> 00:56:44,960
they matter all right so let's have this
1562
00:56:44,960 --> 00:56:48,160
return a and b the sum of them okay so
1563
00:56:48,160 --> 00:56:51,760
we're gonna do return a plus b and i'll
1564
00:56:51,760 --> 00:56:53,680
i'll show you why we're using the word
1565
00:56:53,680 --> 00:56:55,520
return instead of something like
1566
00:56:55,520 --> 00:56:57,839
console.log and let me do that with a
1567
00:56:57,839 --> 00:57:00,720
simple example here so first let's just
1568
00:57:00,720 --> 00:57:02,799
run this function so when i do sum and
1569
00:57:02,799 --> 00:57:05,119
if i give it one and two it should take
1570
00:57:05,119 --> 00:57:06,880
one and two so it should replace a with
1571
00:57:06,880 --> 00:57:09,760
one b with two so it should do return
1572
00:57:09,760 --> 00:57:11,920
one plus two that's effectively what
1573
00:57:11,920 --> 00:57:14,000
happens so it should return can you
1574
00:57:14,000 --> 00:57:14,799
guess
1575
00:57:14,799 --> 00:57:16,880
three okay that's right so that's what
1576
00:57:16,880 --> 00:57:18,480
it should return but watch what happens
1577
00:57:18,480 --> 00:57:20,400
when i run this okay and let's um
1578
00:57:20,400 --> 00:57:21,760
comment out this greeting because we
1579
00:57:21,760 --> 00:57:23,280
don't need to run that anymore and let's
1580
00:57:23,280 --> 00:57:25,839
run this function well nothing went to
1581
00:57:25,839 --> 00:57:27,599
the console so it
1582
00:57:27,599 --> 00:57:30,319
ran something happened but nothing ever
1583
00:57:30,319 --> 00:57:32,799
went to the console interesting
1584
00:57:32,799 --> 00:57:35,599
well it's not being shown so let's go
1585
00:57:35,599 --> 00:57:36,799
ahead and show it so let's do
1586
00:57:36,799 --> 00:57:39,440
console.log and run it and now you can
1587
00:57:39,440 --> 00:57:41,359
see that it says three now watch what
1588
00:57:41,359 --> 00:57:44,240
happens if i do num1 and i want to store
1589
00:57:44,240 --> 00:57:46,319
this result somewhere so i want to do
1590
00:57:46,319 --> 00:57:49,280
one comma two okay so i want to store
1591
00:57:49,280 --> 00:57:51,359
the sum of one and two in a variable
1592
00:57:51,359 --> 00:57:53,680
called num1 okay so let's delete this
1593
00:57:53,680 --> 00:57:56,559
line now and let's run and it got stored
1594
00:57:56,559 --> 00:57:58,559
and now we'll we'll log it out so the
1595
00:57:58,559 --> 00:58:01,200
console.log and i'll say num1 okay let's
1596
00:58:01,200 --> 00:58:04,160
run it and it got the sum three okay it
1597
00:58:04,160 --> 00:58:06,240
stored three into this variable called
1598
00:58:06,240 --> 00:58:08,480
num1 and then we console log it out and
1599
00:58:08,480 --> 00:58:10,640
boom we see it right there now to prove
1600
00:58:10,640 --> 00:58:14,079
my point if we hadn't done return a plus
1601
00:58:14,079 --> 00:58:16,799
b instead imagine we just did console
1602
00:58:16,799 --> 00:58:19,440
log a plus b watch what what's gonna
1603
00:58:19,440 --> 00:58:22,000
happen let's run this and we get an
1604
00:58:22,000 --> 00:58:25,599
error and this error says undefined so
1605
00:58:25,599 --> 00:58:27,920
this prints out the console log right
1606
00:58:27,920 --> 00:58:29,680
but when we run
1607
00:58:29,680 --> 00:58:32,319
so this line goes and it goes 3 but then
1608
00:58:32,319 --> 00:58:34,480
this over here says undefined because
1609
00:58:34,480 --> 00:58:37,200
what's being stored in num1 is undefined
1610
00:58:37,200 --> 00:58:40,000
we don't have a number like three that's
1611
00:58:40,000 --> 00:58:42,240
actually being stored here we're console
1612
00:58:42,240 --> 00:58:43,440
logging something and we're trying to
1613
00:58:43,440 --> 00:58:45,680
store console.log here and you can't
1614
00:58:45,680 --> 00:58:48,160
store that so that's why what you want
1615
00:58:48,160 --> 00:58:51,040
to do here is return a plus b if you
1616
00:58:51,040 --> 00:58:53,040
don't do a return then this function is
1617
00:58:53,040 --> 00:58:55,680
essentially useless and it can't be used
1618
00:58:55,680 --> 00:58:58,400
in other places and do actual helpful
1619
00:58:58,400 --> 00:59:00,319
things okay so that's why what we need
1620
00:59:00,319 --> 00:59:03,119
to do is actually change this to a
1621
00:59:03,119 --> 00:59:05,040
return a plus b
1622
00:59:05,040 --> 00:59:07,760
just like this okay now when we do num1
1623
00:59:07,760 --> 00:59:10,240
it actually does get used okay let's
1624
00:59:10,240 --> 00:59:12,160
comment out both of these lines now and
1625
00:59:12,160 --> 00:59:13,119
what we're going to do is we're going to
1626
00:59:13,119 --> 00:59:15,760
go back all the way up to when we made
1627
00:59:15,760 --> 00:59:18,400
that little baby tip calculator thing so
1628
00:59:18,400 --> 00:59:20,000
i'm going to copy this entire thing and
1629
00:59:20,000 --> 00:59:21,599
we're going to turn this into a function
1630
00:59:21,599 --> 00:59:24,720
so i copied it okay from line 17 to line
1631
00:59:24,720 --> 00:59:27,200
24 and we're going to paste it here
1632
00:59:27,200 --> 00:59:29,119
right there let's hit run so nothing
1633
00:59:29,119 --> 00:59:30,880
should come out to the console so that's
1634
00:59:30,880 --> 00:59:32,799
perfect that's exactly kind of where you
1635
00:59:32,799 --> 00:59:35,680
should be right now now i'll go and
1636
00:59:35,680 --> 00:59:38,000
uncomment this and um
1637
00:59:38,000 --> 00:59:40,559
we won't be using these prompts anymore
1638
00:59:40,559 --> 00:59:42,400
because that's kind of uh you know when
1639
00:59:42,400 --> 00:59:44,160
you're using functions and prompts is
1640
00:59:44,160 --> 00:59:46,079
like rarely ever used it's kind of an
1641
00:59:46,079 --> 00:59:48,240
easy way to give a front end to the app
1642
00:59:48,240 --> 00:59:49,839
you know just kind of for you to play
1643
00:59:49,839 --> 00:59:51,599
around with but majority of the times
1644
00:59:51,599 --> 00:59:54,240
you wouldn't really use prompt that much
1645
00:59:54,240 --> 00:59:56,000
but first let's just focus on turning
1646
00:59:56,000 --> 00:59:57,599
this into a function so we're going to
1647
00:59:57,599 --> 00:59:59,280
say is going to be a function and we're
1648
00:59:59,280 --> 01:00:03,760
gonna call it calculate uh tip and food
1649
01:00:03,760 --> 01:00:06,079
amount something like that okay or let's
1650
01:00:06,079 --> 01:00:09,119
just call it calculate total food total
1651
01:00:09,119 --> 01:00:11,040
okay so this is gonna calculate the food
1652
01:00:11,040 --> 01:00:14,240
total with the food amount plus however
1653
01:00:14,240 --> 01:00:16,240
much the tip was okay we won't have the
1654
01:00:16,240 --> 01:00:18,079
alert statement anymore so let's remove
1655
01:00:18,079 --> 01:00:20,240
that and this function is dependent on
1656
01:00:20,240 --> 01:00:22,079
two things how much you paid for the
1657
01:00:22,079 --> 01:00:24,400
food and the second thing it's dependent
1658
01:00:24,400 --> 01:00:27,359
on is how much you want to give as a tip
1659
01:00:27,359 --> 01:00:29,359
so these two are going to be arguments
1660
01:00:29,359 --> 01:00:30,559
that we're now going to pass and so i'm
1661
01:00:30,559 --> 01:00:33,839
going to say food and tip percentage all
1662
01:00:33,839 --> 01:00:35,520
right so basically here's what we're
1663
01:00:35,520 --> 01:00:37,280
going to do right so tip whatever the
1664
01:00:37,280 --> 01:00:39,119
user gives we're going to take that tip
1665
01:00:39,119 --> 01:00:41,200
we're going to divide that by
1666
01:00:41,200 --> 01:00:45,200
100 okay so if the user says hits 20
1667
01:00:45,200 --> 01:00:46,799
then we're going to take that tip
1668
01:00:46,799 --> 01:00:49,040
divided by 100 and turn it into an
1669
01:00:49,040 --> 01:00:52,240
actual percentage which would be 0.2
1670
01:00:52,240 --> 01:00:54,000
and then we'll multiply this now we're
1671
01:00:54,000 --> 01:00:55,440
going to do the same thing for food
1672
01:00:55,440 --> 01:00:56,720
we're not gonna have this number
1673
01:00:56,720 --> 01:00:58,160
prompting anymore okay we're gonna
1674
01:00:58,160 --> 01:01:00,319
remove that we're gonna say whatever
1675
01:01:00,319 --> 01:01:02,400
food is gonna be whatever food is here
1676
01:01:02,400 --> 01:01:04,319
so in other words we actually don't even
1677
01:01:04,319 --> 01:01:06,319
need food because the user is just
1678
01:01:06,319 --> 01:01:08,799
passing that information down so
1679
01:01:08,799 --> 01:01:10,480
food is going to be a given tip is going
1680
01:01:10,480 --> 01:01:12,079
to be a given we're going to calculate
1681
01:01:12,079 --> 01:01:13,520
the tip percentage based off of the
1682
01:01:13,520 --> 01:01:14,960
given tip and then we're going to do the
1683
01:01:14,960 --> 01:01:17,040
same thing so take the food multiply by
1684
01:01:17,040 --> 01:01:18,640
the tip percentage that'll give us the
1685
01:01:18,640 --> 01:01:20,480
tip amount and then the total will be
1686
01:01:20,480 --> 01:01:22,720
the food plus the two amount and then we
1687
01:01:22,720 --> 01:01:25,280
can console log it out if we want to and
1688
01:01:25,280 --> 01:01:26,880
in this case since we're being adults
1689
01:01:26,880 --> 01:01:28,720
now not a little children anymore we're
1690
01:01:28,720 --> 01:01:31,440
going to do returns total
1691
01:01:31,440 --> 01:01:33,440
let's go here and let's indent this so
1692
01:01:33,440 --> 01:01:35,839
you can grab this and just hit tab
1693
01:01:35,839 --> 01:01:37,599
and make sure that this function has a
1694
01:01:37,599 --> 01:01:40,000
squiggly that opens it and a squiggly
1695
01:01:40,000 --> 01:01:42,079
that closes it okay that's pretty
1696
01:01:42,079 --> 01:01:43,680
important whenever you're writing
1697
01:01:43,680 --> 01:01:46,000
functions so i'm just hitting back or
1698
01:01:46,000 --> 01:01:47,760
you can just hit format at the top and
1699
01:01:47,760 --> 01:01:50,160
they'll automatically format your code
1700
01:01:50,160 --> 01:01:52,319
for you if you hit this and formatting
1701
01:01:52,319 --> 01:01:54,000
just means it makes it look nice and
1702
01:01:54,000 --> 01:01:56,000
pretty okay so i have calculate food
1703
01:01:56,000 --> 01:01:58,319
total it takes in food and it takes in a
1704
01:01:58,319 --> 01:02:00,480
tip so let's actually try this last
1705
01:02:00,480 --> 01:02:03,280
console log and we'll call the function
1706
01:02:03,280 --> 01:02:05,760
calculate food total and we'll say the
1707
01:02:05,760 --> 01:02:10,000
food was 100 and the tip was let's say
1708
01:02:10,000 --> 01:02:11,119
20
1709
01:02:11,119 --> 01:02:13,119
let's go ahead and hit run now let's see
1710
01:02:13,119 --> 01:02:15,760
what we get and we get back 120 so now
1711
01:02:15,760 --> 01:02:17,119
you can start to see the power of
1712
01:02:17,119 --> 01:02:18,640
functions that
1713
01:02:18,640 --> 01:02:21,280
they can do a whole bunch of things very
1714
01:02:21,280 --> 01:02:22,799
quickly and so you just call it in a
1715
01:02:22,799 --> 01:02:24,799
very easy way give it two inputs and
1716
01:02:24,799 --> 01:02:28,240
boom your function is up and running now
1717
01:02:28,240 --> 01:02:31,280
let's take this a step even further so
1718
01:02:31,280 --> 01:02:33,920
remember when we created a sum function
1719
01:02:33,920 --> 01:02:37,839
c if you can replace this line 104 okay
1720
01:02:37,839 --> 01:02:39,680
so you can leave the total here but see
1721
01:02:39,680 --> 01:02:42,480
if we can replace this and this instead
1722
01:02:42,480 --> 01:02:44,559
of using this plus sign here and see if
1723
01:02:44,559 --> 01:02:46,640
you can use a sum function to calculate
1724
01:02:46,640 --> 01:02:48,720
the total so i'll give you five seconds
1725
01:02:48,720 --> 01:02:50,720
you can pause the video and try to do it
1726
01:02:50,720 --> 01:02:53,200
yourself okay so let's go five
1727
01:02:53,200 --> 01:02:54,079
four
1728
01:02:54,079 --> 01:02:55,119
three
1729
01:02:55,119 --> 01:02:56,000
two
1730
01:02:56,000 --> 01:02:56,960
one
1731
01:02:56,960 --> 01:02:58,799
all right let's give this a try so let's
1732
01:02:58,799 --> 01:03:00,640
go here and instead of using a plus sign
1733
01:03:00,640 --> 01:03:01,440
what i'm going to say is i'm going to
1734
01:03:01,440 --> 01:03:04,720
say sum and we're going to give it food
1735
01:03:04,720 --> 01:03:05,920
all right so it's going to be the food
1736
01:03:05,920 --> 01:03:07,680
amount and then we're going to give it
1737
01:03:07,680 --> 01:03:10,079
the tip amount so now we're using the
1738
01:03:10,079 --> 01:03:12,559
function that we created ourselves
1739
01:03:12,559 --> 01:03:14,000
earlier and then we're going to store
1740
01:03:14,000 --> 01:03:15,760
the result of that in a variable called
1741
01:03:15,760 --> 01:03:17,760
total and then we're going to return
1742
01:03:17,760 --> 01:03:20,319
that total so let's go ahead and run
1743
01:03:20,319 --> 01:03:22,319
this function and we should still get
1744
01:03:22,319 --> 01:03:25,760
back 120. perfect very good let's change
1745
01:03:25,760 --> 01:03:28,400
the price of this 300 let's try it again
1746
01:03:28,400 --> 01:03:31,920
you can see that our our tip is 360
1747
01:03:31,920 --> 01:03:34,079
dollars now okay and it's working
1748
01:03:34,079 --> 01:03:36,720
perfectly now i do have to show you the
1749
01:03:36,720 --> 01:03:38,559
modern way of writing a function because
1750
01:03:38,559 --> 01:03:40,160
you're gonna start seeing that in a lot
1751
01:03:40,160 --> 01:03:41,920
of different places with javascript and
1752
01:03:41,920 --> 01:03:45,680
so this is the es6 way and this is these
1753
01:03:45,680 --> 01:03:47,680
are called arrow functions
1754
01:03:47,680 --> 01:03:48,480
so
1755
01:03:48,480 --> 01:03:51,280
um and also the variables let's let's uh
1756
01:03:51,280 --> 01:03:53,039
change them okay so let's go ahead and
1757
01:03:53,039 --> 01:03:55,039
do these variables in a way you know we
1758
01:03:55,039 --> 01:03:57,200
you we're using the var let cons so i
1759
01:03:57,200 --> 01:03:58,720
know that we didn't use it for a couple
1760
01:03:58,720 --> 01:04:00,480
of these other things but that's again
1761
01:04:00,480 --> 01:04:02,640
just so you can start understanding how
1762
01:04:02,640 --> 01:04:04,319
all of this stuff works in an easy way
1763
01:04:04,319 --> 01:04:06,079
but we're going to do const tip
1764
01:04:06,079 --> 01:04:09,119
percentage all right we'll do const tip
1765
01:04:09,119 --> 01:04:12,000
amount you could do cons or let either
1766
01:04:12,000 --> 01:04:13,200
should be fine okay then we're gonna do
1767
01:04:13,200 --> 01:04:14,960
const total three variables that we're
1768
01:04:14,960 --> 01:04:16,960
declaring okay so constant let and you
1769
01:04:16,960 --> 01:04:18,640
should you should be fine either way
1770
01:04:18,640 --> 01:04:20,640
remember if you do something const that
1771
01:04:20,640 --> 01:04:22,160
means you're not changing that variable
1772
01:04:22,160 --> 01:04:24,240
later and if you do
1773
01:04:24,240 --> 01:04:26,480
let that that's basically saying hey i
1774
01:04:26,480 --> 01:04:28,160
know i'm making this variable that i
1775
01:04:28,160 --> 01:04:29,680
know i'm going to change at a later
1776
01:04:29,680 --> 01:04:31,359
point i'm going to change its definition
1777
01:04:31,359 --> 01:04:32,720
there we go let's run this function
1778
01:04:32,720 --> 01:04:34,000
again and let's just make sure that it
1779
01:04:34,000 --> 01:04:35,680
works all right so now with arrow
1780
01:04:35,680 --> 01:04:37,200
functions how they work is they kind of
1781
01:04:37,200 --> 01:04:39,520
look like this okay so
1782
01:04:39,520 --> 01:04:42,240
um i'll make we'll we'll make this
1783
01:04:42,240 --> 01:04:44,400
function sum but we'll do it with an
1784
01:04:44,400 --> 01:04:46,720
arrow function okay so let's do some i'm
1785
01:04:46,720 --> 01:04:48,799
going to say const i'm going to say some
1786
01:04:48,799 --> 01:04:50,640
arrow is equal to
1787
01:04:50,640 --> 01:04:52,000
like that
1788
01:04:52,000 --> 01:04:54,400
so it's equal to and then you put in the
1789
01:04:54,400 --> 01:04:56,799
arguments all right so this is think of
1790
01:04:56,799 --> 01:04:58,640
this like a variable and then you put
1791
01:04:58,640 --> 01:05:01,200
the arguments here so here i'm going to
1792
01:05:01,200 --> 01:05:03,760
say a comma b and then this arrow here
1793
01:05:03,760 --> 01:05:06,160
so which is equal and greater than sign
1794
01:05:06,160 --> 01:05:08,480
open close squigglies and then inside of
1795
01:05:08,480 --> 01:05:10,400
there i'm just going to say return a
1796
01:05:10,400 --> 01:05:13,359
plus b so that's how you can write an
1797
01:05:13,359 --> 01:05:14,720
arrow function
1798
01:05:14,720 --> 01:05:16,799
and basically write the same thing but
1799
01:05:16,799 --> 01:05:18,319
an error function now here's the cool
1800
01:05:18,319 --> 01:05:19,680
part about the error function okay
1801
01:05:19,680 --> 01:05:20,799
because right now you're probably like
1802
01:05:20,799 --> 01:05:22,640
wait what's the benefit of this why am i
1803
01:05:22,640 --> 01:05:25,280
writing this uh when i can clearly write
1804
01:05:25,280 --> 01:05:27,359
it this way and this looks like easier
1805
01:05:27,359 --> 01:05:28,640
to kind of see and understand because it
1806
01:05:28,640 --> 01:05:30,640
says function error functions can look
1807
01:05:30,640 --> 01:05:32,480
really nice so here's what i mean by
1808
01:05:32,480 --> 01:05:34,799
that they have a superpower called
1809
01:05:34,799 --> 01:05:37,039
implicit returns where if you remove
1810
01:05:37,039 --> 01:05:38,880
these squigglies you don't even have to
1811
01:05:38,880 --> 01:05:40,640
write the keyword return it will
1812
01:05:40,640 --> 01:05:42,799
automatically return
1813
01:05:42,799 --> 01:05:44,799
that and so that
1814
01:05:44,799 --> 01:05:46,559
sum function that you wrote can
1815
01:05:46,559 --> 01:05:49,599
essentially be on the same line that's
1816
01:05:49,599 --> 01:05:51,280
some function that you wrote so it's so
1817
01:05:51,280 --> 01:05:52,880
nice and easy because your entire
1818
01:05:52,880 --> 01:05:54,480
function just gets written out like this
1819
01:05:54,480 --> 01:05:56,319
so you know it'll be like right i have a
1820
01:05:56,319 --> 01:05:57,760
function called sum it takes in two
1821
01:05:57,760 --> 01:06:00,559
arguments a and b and it returns their
1822
01:06:00,559 --> 01:06:02,839
sum okay so this is an example of an
1823
01:06:02,839 --> 01:06:06,559
implicit return so arrow function
1824
01:06:06,559 --> 01:06:08,720
with implicit return okay that's an
1825
01:06:08,720 --> 01:06:10,640
example of that and again i'll just call
1826
01:06:10,640 --> 01:06:13,359
this function some arrow so it's clear
1827
01:06:13,359 --> 01:06:14,319
for you
1828
01:06:14,319 --> 01:06:16,079
and then this is
1829
01:06:16,079 --> 01:06:19,920
arrow function with explicit return okay
1830
01:06:19,920 --> 01:06:23,520
let's put it here important for implicit
1831
01:06:23,520 --> 01:06:25,440
return remove
1832
01:06:25,440 --> 01:06:27,280
squigglies
1833
01:06:27,280 --> 01:06:30,000
okay so squigglies are these these curly
1834
01:06:30,000 --> 01:06:32,960
braces or i can say curly braces all
1835
01:06:32,960 --> 01:06:34,319
right there we go
1836
01:06:34,319 --> 01:06:36,640
so that's important to do if you want to
1837
01:06:36,640 --> 01:06:39,039
have implicit return where you don't
1838
01:06:39,039 --> 01:06:41,039
want to say that return so let's try
1839
01:06:41,039 --> 01:06:42,559
this some arrow function that we created
1840
01:06:42,559 --> 01:06:44,720
and see if it works we'll do console.log
1841
01:06:44,720 --> 01:06:47,359
and i'll say sum arrow and we'll give it
1842
01:06:47,359 --> 01:06:51,119
10 comma 40 50. and we should get back a
1843
01:06:51,119 --> 01:06:53,280
60. let's see if we get that back it
1844
01:06:53,280 --> 01:06:54,480
says it's been declared because i'm
1845
01:06:54,480 --> 01:06:56,640
defining it twice and remember i called
1846
01:06:56,640 --> 01:06:59,119
it const so if i'm calling it a const
1847
01:06:59,119 --> 01:07:01,520
here it's not going to let me redefine
1848
01:07:01,520 --> 01:07:03,839
this function later with the same
1849
01:07:03,839 --> 01:07:05,680
variable so i'll just call it some arrow
1850
01:07:05,680 --> 01:07:07,839
2 and call the function some arrow 2
1851
01:07:07,839 --> 01:07:10,079
here and we'll hit save command s or
1852
01:07:10,079 --> 01:07:12,720
just hit run and boom you see the sum of
1853
01:07:12,720 --> 01:07:15,839
these two numbers is indeed 60. so the
1854
01:07:15,839 --> 01:07:17,520
sum function is working so it's a really
1855
01:07:17,520 --> 01:07:19,440
nice way of creating your function so
1856
01:07:19,440 --> 01:07:22,079
for bonus points and for being fancy i
1857
01:07:22,079 --> 01:07:24,079
want you to always try to use the arrow
1858
01:07:24,079 --> 01:07:25,599
function because that's the modern way
1859
01:07:25,599 --> 01:07:28,160
of doing javascript and you'll see that
1860
01:07:28,160 --> 01:07:29,920
in majority of the code basis so i'd
1861
01:07:29,920 --> 01:07:31,119
rather you just get in the habit of
1862
01:07:31,119 --> 01:07:34,240
doing that now to wrap this exercise up
1863
01:07:34,240 --> 01:07:36,240
i'm going to leave you off with an
1864
01:07:36,240 --> 01:07:38,799
exercise okay to wrap up really the sum
1865
01:07:38,799 --> 01:07:41,119
functions and i want you to practice
1866
01:07:41,119 --> 01:07:42,880
with functions now here's what i'm going
1867
01:07:42,880 --> 01:07:44,799
to have you do so in files on the left
1868
01:07:44,799 --> 01:07:46,880
hand side okay i want you to go inside
1869
01:07:46,880 --> 01:07:50,000
of functions and go inside of something
1870
01:07:50,000 --> 01:07:51,920
called sum and then there's going to be
1871
01:07:51,920 --> 01:07:53,440
an exercise folder in here and a
1872
01:07:53,440 --> 01:07:55,440
solution folder so don't go into the
1873
01:07:55,440 --> 01:07:57,839
solution for this exercise yet make sure
1874
01:07:57,839 --> 01:08:00,240
to go into the exercise if you're
1875
01:08:00,240 --> 01:08:02,480
stuck and you can't make progress and
1876
01:08:02,480 --> 01:08:04,880
you can't really you're stuck then and
1877
01:08:04,880 --> 01:08:06,880
only then go in solution but don't go in
1878
01:08:06,880 --> 01:08:09,200
there as the first thing and in this
1879
01:08:09,200 --> 01:08:13,039
exercise i'll go and go to script.js
1880
01:08:13,039 --> 01:08:13,839
okay
1881
01:08:13,839 --> 01:08:15,039
in here
1882
01:08:15,039 --> 01:08:17,040
you will see that you have these
1883
01:08:17,040 --> 01:08:19,040
exercises like make a function called
1884
01:08:19,040 --> 01:08:21,198
add which is the same as sum so you
1885
01:08:21,198 --> 01:08:22,719
should have no problem making this
1886
01:08:22,719 --> 01:08:25,839
function make a function called subtract
1887
01:08:25,839 --> 01:08:27,920
make a function that divides two numbers
1888
01:08:27,920 --> 01:08:29,439
and make a function that multiplies two
1889
01:08:29,439 --> 01:08:31,279
numbers okay so all of these should take
1890
01:08:31,279 --> 01:08:34,238
in two arguments sub add
1891
01:08:34,238 --> 01:08:35,600
div and mul
1892
01:08:35,600 --> 01:08:38,479
they should do what they say here and so
1893
01:08:38,479 --> 01:08:40,158
we have some notes for you and what you
1894
01:08:40,158 --> 01:08:41,920
got to do so the to-do's are written out
1895
01:08:41,920 --> 01:08:44,640
for you here and this is the normal way
1896
01:08:44,640 --> 01:08:46,238
of doing it and for bonus points if you
1897
01:08:46,238 --> 01:08:49,600
want to be fancy use the es6 syntax for
1898
01:08:49,600 --> 01:08:51,520
error functions to maybe even convert
1899
01:08:51,520 --> 01:08:53,198
these into error functions if you want
1900
01:08:53,198 --> 01:08:55,759
to be a real boss and then at the bottom
1901
01:08:55,759 --> 01:08:58,640
make sure you also console log all of
1902
01:08:58,640 --> 01:08:59,920
these so
1903
01:08:59,920 --> 01:09:02,319
meaning you print out the result of all
1904
01:09:02,319 --> 01:09:04,719
of these okay or do some kind of call
1905
01:09:04,719 --> 01:09:05,920
now where you're going to have
1906
01:09:05,920 --> 01:09:08,319
difficulty is running this file so
1907
01:09:08,319 --> 01:09:09,600
you're going to be like how do i run
1908
01:09:09,600 --> 01:09:12,399
this file let me show you okay so over
1909
01:09:12,399 --> 01:09:14,238
here we have something for you can
1910
01:09:14,238 --> 01:09:16,080
exercises and if you want to see this in
1911
01:09:16,080 --> 01:09:17,839
full screen you can always hit this
1912
01:09:17,839 --> 01:09:19,839
button open in a new tab that'll open
1913
01:09:19,839 --> 01:09:21,439
this entire thing in a new tab that way
1914
01:09:21,439 --> 01:09:23,679
you can like guaranteed see it right
1915
01:09:23,679 --> 01:09:24,960
just in case some certain things are
1916
01:09:24,960 --> 01:09:27,679
hidden and what's cool about repple is
1917
01:09:27,679 --> 01:09:29,600
your code is already deployed on the
1918
01:09:29,600 --> 01:09:31,679
internet meaning like this is not just
1919
01:09:31,679 --> 01:09:33,600
on your local computer you could run
1920
01:09:33,600 --> 01:09:36,399
this anywhere like you could send this
1921
01:09:36,399 --> 01:09:38,640
link to your buddy and they could just
1922
01:09:38,640 --> 01:09:39,759
run this
1923
01:09:39,759 --> 01:09:41,759
at their place right like because it's
1924
01:09:41,759 --> 01:09:43,600
online this is online so they can run it
1925
01:09:43,600 --> 01:09:44,960
and like look at your code and so if you
1926
01:09:44,960 --> 01:09:46,399
made something you could be really proud
1927
01:09:46,399 --> 01:09:48,479
of it and show it to them so we have
1928
01:09:48,479 --> 01:09:50,238
this project solutions folder right so
1929
01:09:50,238 --> 01:09:52,319
this is where the solutions will be but
1930
01:09:52,319 --> 01:09:54,320
for functions here's the sum right so
1931
01:09:54,320 --> 01:09:56,239
you click the sum now if you do
1932
01:09:56,239 --> 01:09:57,920
console.log
1933
01:09:57,920 --> 01:10:01,280
hello from the sum file
1934
01:10:01,280 --> 01:10:03,199
okay or some exercise or whatever let's
1935
01:10:03,199 --> 01:10:05,679
just say that here and i'll hit save
1936
01:10:05,679 --> 01:10:07,360
if you click sum
1937
01:10:07,360 --> 01:10:09,280
it will say hello from the sum exercise
1938
01:10:09,280 --> 01:10:12,400
so meaning anything you console log here
1939
01:10:12,400 --> 01:10:14,400
will show up just make sure if you want
1940
01:10:14,400 --> 01:10:16,080
to run this file again you're going to
1941
01:10:16,080 --> 01:10:17,679
have to click
1942
01:10:17,679 --> 01:10:20,000
this sum link over here and that will
1943
01:10:20,000 --> 01:10:22,239
run your exercise okay
1944
01:10:22,239 --> 01:10:24,560
and also another thing i want to show
1945
01:10:24,560 --> 01:10:27,280
you is this is just html css javascript
1946
01:10:27,280 --> 01:10:29,440
so what's nice about this is you could
1947
01:10:29,440 --> 01:10:32,800
just download this folder as zip okay
1948
01:10:32,800 --> 01:10:34,719
and let's go ahead and open it
1949
01:10:34,719 --> 01:10:36,640
so i'm going to go ahead and open this
1950
01:10:36,640 --> 01:10:38,080
and go inside of here and you could
1951
01:10:38,080 --> 01:10:40,800
click this index.html and just run it
1952
01:10:40,800 --> 01:10:43,600
and it will run on your computer locally
1953
01:10:43,600 --> 01:10:45,920
so just so you understand that this is
1954
01:10:45,920 --> 01:10:47,440
real world right just because we're
1955
01:10:47,440 --> 01:10:49,040
doing it on online website doesn't mean
1956
01:10:49,040 --> 01:10:51,040
you can't use it in real life you could
1957
01:10:51,040 --> 01:10:53,440
just download that folder and run it and
1958
01:10:53,440 --> 01:10:55,440
now it's on your local computer so
1959
01:10:55,440 --> 01:10:57,840
that's just to kind of show you that all
1960
01:10:57,840 --> 01:10:59,280
your options and everything that you
1961
01:10:59,280 --> 01:11:01,840
need is available at your disposal all
1962
01:11:01,840 --> 01:11:04,159
right so but we're just coding here for
1963
01:11:04,159 --> 01:11:05,840
the sake of simplicity and replica just
1964
01:11:05,840 --> 01:11:08,239
makes everything really really easy okay
1965
01:11:08,239 --> 01:11:10,960
so that's it go ahead now and do this
1966
01:11:10,960 --> 01:11:13,520
exercise and once you return from doing
1967
01:11:13,520 --> 01:11:15,120
this exercise we're going to move on to
1968
01:11:15,120 --> 01:11:16,800
starting to talk about different types
1969
01:11:16,800 --> 01:11:19,360
of data data types like arrays objects
1970
01:11:19,360 --> 01:11:21,600
things of that nature okay so good luck
1971
01:11:21,600 --> 01:11:22,640
have fun
1972
01:11:22,640 --> 01:11:25,280
and go and crush this and i'll see you
1973
01:11:25,280 --> 01:11:26,640
in a minute
1974
01:11:26,640 --> 01:11:28,960
all right so now let's jump in and start
1975
01:11:28,960 --> 01:11:30,560
talking about arrays and right before we
1976
01:11:30,560 --> 01:11:32,560
do that let's go ahead and comment out
1977
01:11:32,560 --> 01:11:34,080
every single line
1978
01:11:34,080 --> 01:11:36,239
that is still running so line 108 for me
1979
01:11:36,239 --> 01:11:38,239
i'm going to comment out and i'm also
1980
01:11:38,239 --> 01:11:40,560
going to comment out line 121 and so
1981
01:11:40,560 --> 01:11:42,640
when i hit run nothing should be coming
1982
01:11:42,640 --> 01:11:45,199
out to my console perfect my console is
1983
01:11:45,199 --> 01:11:47,679
looking crispy clean that's exactly what
1984
01:11:47,679 --> 01:11:48,960
we want
1985
01:11:48,960 --> 01:11:49,760
so
1986
01:11:49,760 --> 01:11:52,719
let's go ahead and start working on
1987
01:11:52,719 --> 01:11:54,320
arrays i'm going to go here i'm going to
1988
01:11:54,320 --> 01:11:56,480
say arrays
1989
01:11:56,480 --> 01:11:57,360
okay
1990
01:11:57,360 --> 01:11:59,280
and why do you need a raise well what if
1991
01:11:59,280 --> 01:12:00,800
you want to hold multiple different
1992
01:12:00,800 --> 01:12:02,159
things how are you going to do that so
1993
01:12:02,159 --> 01:12:04,000
if you have groceries
1994
01:12:04,000 --> 01:12:05,520
right in a variable you could only have
1995
01:12:05,520 --> 01:12:07,920
one grocery like a fruit but arrays
1996
01:12:07,920 --> 01:12:09,760
allow you to have multiple things so for
1997
01:12:09,760 --> 01:12:11,600
example i could have an array that has a
1998
01:12:11,600 --> 01:12:12,960
banana
1999
01:12:12,960 --> 01:12:15,760
it also has
2000
01:12:15,760 --> 01:12:17,040
apple
2001
01:12:17,040 --> 01:12:20,239
okay i can also have orange
2002
01:12:20,239 --> 01:12:21,120
okay
2003
01:12:21,120 --> 01:12:23,840
we can also have a pair
2004
01:12:23,840 --> 01:12:25,120
all right i could have groceries like
2005
01:12:25,120 --> 01:12:26,960
this and you could write the words too
2006
01:12:26,960 --> 01:12:28,640
so this is just a string and i decided
2007
01:12:28,640 --> 01:12:30,480
to put an emoji in it but you could
2008
01:12:30,480 --> 01:12:32,960
decide to put like the actual word pair
2009
01:12:32,960 --> 01:12:34,560
here if you want but i just think that
2010
01:12:34,560 --> 01:12:35,760
this looks
2011
01:12:35,760 --> 01:12:37,840
prettier right so that's why i'm having
2012
01:12:37,840 --> 01:12:39,760
it like this
2013
01:12:39,760 --> 01:12:41,840
now if i'm doing
2014
01:12:41,840 --> 01:12:43,440
all right and remember to just put a
2015
01:12:43,440 --> 01:12:44,800
const here okay so we're just going to
2016
01:12:44,800 --> 01:12:48,239
say const groceries and here we go we
2017
01:12:48,239 --> 01:12:50,800
have all of these you know we got a
2018
01:12:50,800 --> 01:12:53,199
banana we got an apple we got all this
2019
01:12:53,199 --> 01:12:54,960
awesome stuff in our groceries right now
2020
01:12:54,960 --> 01:12:57,440
let's console log groceries
2021
01:12:57,440 --> 01:12:58,239
okay
2022
01:12:58,239 --> 01:13:00,400
and let's hit save and let's see what
2023
01:13:00,400 --> 01:13:02,480
happens i'm going to move this here and
2024
01:13:02,480 --> 01:13:04,000
boom you can actually see that it
2025
01:13:04,000 --> 01:13:06,800
printed out all of the groceries okay
2026
01:13:06,800 --> 01:13:10,080
now how do i get just a banana if we if
2027
01:13:10,080 --> 01:13:12,239
i want right so if you click it it shows
2028
01:13:12,239 --> 01:13:14,000
you the zeroth element is a banana the
2029
01:13:14,000 --> 01:13:15,520
first is the
2030
01:13:15,520 --> 01:13:18,159
apple the second is orange and the third
2031
01:13:18,159 --> 01:13:20,719
is pear okay and remember everything in
2032
01:13:20,719 --> 01:13:22,159
coding when you start counting it
2033
01:13:22,159 --> 01:13:24,480
usually starts from zero
2034
01:13:24,480 --> 01:13:26,719
so it goes zero one two three
2035
01:13:26,719 --> 01:13:28,719
and the length of this is four okay
2036
01:13:28,719 --> 01:13:30,400
because there are four items in here
2037
01:13:30,400 --> 01:13:33,760
four grocery items now
2038
01:13:33,760 --> 01:13:36,239
how can we get just a banana if we want
2039
01:13:36,239 --> 01:13:38,719
just a banana how do we do that well
2040
01:13:38,719 --> 01:13:40,719
here's what we do we go console.log
2041
01:13:40,719 --> 01:13:43,360
groceries we put this bracket and we do
2042
01:13:43,360 --> 01:13:46,480
zero okay and let me hit save and let's
2043
01:13:46,480 --> 01:13:49,280
see boom we just got the banana okay
2044
01:13:49,280 --> 01:13:51,840
well what if i want an apple what do i
2045
01:13:51,840 --> 01:13:53,520
do can you take a guess i'll give you
2046
01:13:53,520 --> 01:13:55,280
five seconds see if you could write that
2047
01:13:55,280 --> 01:13:57,199
yourself five
2048
01:13:57,199 --> 01:13:58,080
four
2049
01:13:58,080 --> 01:13:59,040
three
2050
01:13:59,040 --> 01:14:01,199
two one
2051
01:14:01,199 --> 01:14:03,440
all right let's do it so you would have
2052
01:14:03,440 --> 01:14:05,520
to put in a one here okay so boom let's
2053
01:14:05,520 --> 01:14:08,080
do that let's do a one let's hit save
2054
01:14:08,080 --> 01:14:11,199
and boom you see we got an apple now how
2055
01:14:11,199 --> 01:14:14,880
would i get not the orange but the pair
2056
01:14:14,880 --> 01:14:16,480
so how would i get the pair make sure to
2057
01:14:16,480 --> 01:14:19,440
count correctly zero one two three right
2058
01:14:19,440 --> 01:14:20,800
how would i get the pair what do i need
2059
01:14:20,800 --> 01:14:22,800
to put in these brackets to get that
2060
01:14:22,800 --> 01:14:24,719
pair i'll give you five seconds here
2061
01:14:24,719 --> 01:14:26,480
five four
2062
01:14:26,480 --> 01:14:30,960
three two one all right let's do it
2063
01:14:30,960 --> 01:14:32,640
we would do three
2064
01:14:32,640 --> 01:14:33,600
okay
2065
01:14:33,600 --> 01:14:36,159
and you can see that we got the pair
2066
01:14:36,159 --> 01:14:36,960
okay
2067
01:14:36,960 --> 01:14:40,159
so again this is zero this is one this
2068
01:14:40,159 --> 01:14:42,880
is two and this is three okay if we want
2069
01:14:42,880 --> 01:14:45,280
to get the orange we would simply
2070
01:14:45,280 --> 01:14:48,080
pop in a two here okay let's give that a
2071
01:14:48,080 --> 01:14:50,239
try make sure you're coding along with
2072
01:14:50,239 --> 01:14:51,840
me and you're just exercising with me
2073
01:14:51,840 --> 01:14:53,760
okay make sure you're not just passively
2074
01:14:53,760 --> 01:14:55,440
watching this make sure you're coding
2075
01:14:55,440 --> 01:14:57,840
along with me every single step of the
2076
01:14:57,840 --> 01:14:58,880
way that's how you're going to get
2077
01:14:58,880 --> 01:15:01,679
better so this is called indexing okay
2078
01:15:01,679 --> 01:15:04,000
this is like you grab one of the indexes
2079
01:15:04,000 --> 01:15:06,960
okay so here i'm getting grabbing
2080
01:15:06,960 --> 01:15:09,520
grab the second
2081
01:15:09,520 --> 01:15:11,600
or like the third index in this case
2082
01:15:11,600 --> 01:15:13,120
okay um
2083
01:15:13,120 --> 01:15:15,360
or second index okay grab the second
2084
01:15:15,360 --> 01:15:16,560
index
2085
01:15:16,560 --> 01:15:18,320
that's basically what we're doing here
2086
01:15:18,320 --> 01:15:20,480
or grab the second item if you want to
2087
01:15:20,480 --> 01:15:22,640
say it in a more human language but in
2088
01:15:22,640 --> 01:15:24,960
programming this is a zeroth index this
2089
01:15:24,960 --> 01:15:27,120
is the first second third so if you ever
2090
01:15:27,120 --> 01:15:28,960
hear like hey it's in the first index or
2091
01:15:28,960 --> 01:15:31,920
the third index that just means where
2092
01:15:31,920 --> 01:15:34,960
its position is in an array okay and
2093
01:15:34,960 --> 01:15:37,920
arrays are really really cool and i'll
2094
01:15:37,920 --> 01:15:40,400
show you a lot more about them soon all
2095
01:15:40,400 --> 01:15:42,239
right so take a look at that right at
2096
01:15:42,239 --> 01:15:44,239
the bottom you can see
2097
01:15:44,239 --> 01:15:46,880
that and let me remove let me cons let
2098
01:15:46,880 --> 01:15:49,120
me comment out this line here okay and
2099
01:15:49,120 --> 01:15:50,480
let's run it again
2100
01:15:50,480 --> 01:15:53,120
and i'll move this up over here it's
2101
01:15:53,120 --> 01:15:55,199
easier to see but take a look at this
2102
01:15:55,199 --> 01:15:57,760
right over here
2103
01:15:57,760 --> 01:16:00,960
before we add the cookie and we console
2104
01:16:00,960 --> 01:16:02,719
log this out notice that there's no
2105
01:16:02,719 --> 01:16:05,600
cookie in here right but after we do the
2106
01:16:05,600 --> 01:16:07,920
push and we console log it out watch
2107
01:16:07,920 --> 01:16:09,040
what happens
2108
01:16:09,040 --> 01:16:11,199
now we have a cookie that's added that
2109
01:16:11,199 --> 01:16:13,199
wasn't there before so that's what push
2110
01:16:13,199 --> 01:16:16,239
does push adds things on to the end of
2111
01:16:16,239 --> 01:16:17,040
the
2112
01:16:17,040 --> 01:16:21,040
array okay so try adding something else
2113
01:16:21,040 --> 01:16:24,080
try adding a chocolate try adding
2114
01:16:24,080 --> 01:16:26,960
uh a blueberry try adding something and
2115
01:16:26,960 --> 01:16:28,320
see what happens and see if you could do
2116
01:16:28,320 --> 01:16:30,080
it yourself okay so add something more
2117
01:16:30,080 --> 01:16:31,920
here let's you give this a try so i'll
2118
01:16:31,920 --> 01:16:34,400
give you five seconds five four
2119
01:16:34,400 --> 01:16:37,600
three two one all right now let's try
2120
01:16:37,600 --> 01:16:38,960
adding something together so i'm gonna
2121
01:16:38,960 --> 01:16:41,600
say groceries dot push okay and we're
2122
01:16:41,600 --> 01:16:42,719
gonna say
2123
01:16:42,719 --> 01:16:45,760
i'm gonna do let's do blueberries
2124
01:16:45,760 --> 01:16:46,640
okay
2125
01:16:46,640 --> 01:16:49,840
and then i'll console.log here paste it
2126
01:16:49,840 --> 01:16:52,239
here hit save and run
2127
01:16:52,239 --> 01:16:54,960
and check it out we got
2128
01:16:54,960 --> 01:16:56,719
we got one
2129
01:16:56,719 --> 01:16:58,000
the regular
2130
01:16:58,000 --> 01:17:00,400
uh groceries array
2131
01:17:00,400 --> 01:17:02,960
then we got this with the cookie this
2132
01:17:02,960 --> 01:17:05,679
time okay and then we push the blueberry
2133
01:17:05,679 --> 01:17:07,280
and you can see there's a blueberry at
2134
01:17:07,280 --> 01:17:09,120
the end there
2135
01:17:09,120 --> 01:17:11,760
so that's how the push method works okay
2136
01:17:11,760 --> 01:17:14,080
great so let's um
2137
01:17:14,080 --> 01:17:15,679
comment out
2138
01:17:15,679 --> 01:17:18,000
all of these for now all the console
2139
01:17:18,000 --> 01:17:20,239
logs so if you run your code you should
2140
01:17:20,239 --> 01:17:22,159
see basically nothing okay it should be
2141
01:17:22,159 --> 01:17:23,840
empty just like this
2142
01:17:23,840 --> 01:17:26,800
perfect now what we want to do is we
2143
01:17:26,800 --> 01:17:27,920
want to
2144
01:17:27,920 --> 01:17:30,320
try grabbing like what if i only wanted
2145
01:17:30,320 --> 01:17:32,800
the banana
2146
01:17:32,800 --> 01:17:35,440
okay what if i only wanted the banana
2147
01:17:35,440 --> 01:17:36,880
and the apple
2148
01:17:36,880 --> 01:17:38,719
okay those are the only two things or
2149
01:17:38,719 --> 01:17:40,560
the orange and the pear and that's all i
2150
01:17:40,560 --> 01:17:42,080
wanted okay
2151
01:17:42,080 --> 01:17:45,280
well for this there is a method
2152
01:17:45,280 --> 01:17:46,239
called
2153
01:17:46,239 --> 01:17:47,440
slice
2154
01:17:47,440 --> 01:17:49,360
okay so we're going to do that so we're
2155
01:17:49,360 --> 01:17:51,280
going to says groceries
2156
01:17:51,280 --> 01:17:53,040
and i'm going to say slice and i'm going
2157
01:17:53,040 --> 01:17:55,679
to say 0 to 2 and let's see what happens
2158
01:17:55,679 --> 01:17:58,560
okay and let's make sure we do console
2159
01:17:58,560 --> 01:18:00,560
wrap this in console.log because
2160
01:18:00,560 --> 01:18:02,239
otherwise it won't show it to the screen
2161
01:18:02,239 --> 01:18:04,800
let's hit command enter or ctrl enter
2162
01:18:04,800 --> 01:18:06,640
and boom look at that we're getting
2163
01:18:06,640 --> 01:18:10,480
banana and apple so it's starting from
2164
01:18:10,480 --> 01:18:13,920
zero including zero okay so basically
2165
01:18:13,920 --> 01:18:16,880
what we're saying is start from zero
2166
01:18:16,880 --> 01:18:19,440
inclusive
2167
01:18:19,440 --> 01:18:20,320
and
2168
01:18:20,320 --> 01:18:21,520
up to
2169
01:18:21,520 --> 01:18:25,280
two okay but but not including two
2170
01:18:25,280 --> 01:18:27,440
okay so basically it's giving you the
2171
01:18:27,440 --> 01:18:29,360
the index this is giving you is either
2172
01:18:29,360 --> 01:18:31,600
zero or one that's the index it's giving
2173
01:18:31,600 --> 01:18:33,679
you so it's either giving you this
2174
01:18:33,679 --> 01:18:35,840
or that okay so if you want to get the
2175
01:18:35,840 --> 01:18:37,520
first two elements you can do it like
2176
01:18:37,520 --> 01:18:39,280
this okay up to two if you want to get
2177
01:18:39,280 --> 01:18:41,040
three elements you do this
2178
01:18:41,040 --> 01:18:43,280
okay so if i run this it's gonna give me
2179
01:18:43,280 --> 01:18:46,239
three elements if i do four and run it
2180
01:18:46,239 --> 01:18:47,920
watch what happens it's gonna give me
2181
01:18:47,920 --> 01:18:49,760
pretty much all of the elements right if
2182
01:18:49,760 --> 01:18:51,840
i hit five it's gonna give me the cookie
2183
01:18:51,840 --> 01:18:55,040
as well that that we added and if i do
2184
01:18:55,040 --> 01:18:57,120
six it's going to give me all the way up
2185
01:18:57,120 --> 01:18:58,560
until the blueberries that we've added
2186
01:18:58,560 --> 01:19:00,320
because we still have the push grocer's
2187
01:19:00,320 --> 01:19:01,840
not pushed there
2188
01:19:01,840 --> 01:19:03,920
boom so you can see the blueberries so
2189
01:19:03,920 --> 01:19:06,080
when i do zero to six it's gonna go from
2190
01:19:06,080 --> 01:19:08,880
zero to actually the fifth index and
2191
01:19:08,880 --> 01:19:10,320
give me
2192
01:19:10,320 --> 01:19:12,480
up to that point cool all right now
2193
01:19:12,480 --> 01:19:15,120
question for you what if i wanted not
2194
01:19:15,120 --> 01:19:18,400
from zero but if i want it from
2195
01:19:18,400 --> 01:19:20,480
right here the pair
2196
01:19:20,480 --> 01:19:22,400
until the blueberry so what if i wanted
2197
01:19:22,400 --> 01:19:25,040
this part of
2198
01:19:25,040 --> 01:19:27,280
the array can you splice that and get
2199
01:19:27,280 --> 01:19:29,360
that out of the array for me please take
2200
01:19:29,360 --> 01:19:31,760
a few seconds take five seconds
2201
01:19:31,760 --> 01:19:36,800
five four three two one and let's work
2202
01:19:36,800 --> 01:19:39,679
on it together okay so i'm going to try
2203
01:19:39,679 --> 01:19:42,719
and i'm going to do okay let's do up to
2204
01:19:42,719 --> 01:19:44,880
six
2205
01:19:44,880 --> 01:19:46,880
one two three four five
2206
01:19:46,880 --> 01:19:48,719
okay like that
2207
01:19:48,719 --> 01:19:50,560
this is just an example
2208
01:19:50,560 --> 01:19:52,800
so let's do console.log
2209
01:19:52,800 --> 01:19:54,880
and we're going to do groceries dot
2210
01:19:54,880 --> 01:19:57,840
slice and i'm going to say
2211
01:19:57,840 --> 01:20:00,080
so let's actually count together okay so
2212
01:20:00,080 --> 01:20:01,920
we have
2213
01:20:01,920 --> 01:20:02,840
0
2214
01:20:02,840 --> 01:20:05,920
1 thank you baby appreciate it
2215
01:20:05,920 --> 01:20:06,719
2
2216
01:20:06,719 --> 01:20:09,040
3 okay so basically we're going to say
2217
01:20:09,040 --> 01:20:11,199
give us from 3
2218
01:20:11,199 --> 01:20:12,639
2
2219
01:20:12,639 --> 01:20:14,719
4
2220
01:20:14,719 --> 01:20:15,840
5
2221
01:20:15,840 --> 01:20:17,199
and 6
2222
01:20:17,199 --> 01:20:20,719
but it's going to go up until five
2223
01:20:20,719 --> 01:20:21,679
okay
2224
01:20:21,679 --> 01:20:24,159
perfect so let's go ahead and do that so
2225
01:20:24,159 --> 01:20:25,600
i'm gonna go
2226
01:20:25,600 --> 01:20:26,639
zero
2227
01:20:26,639 --> 01:20:29,760
sorry three two six and let's see if
2228
01:20:29,760 --> 01:20:31,280
this gives us what we want so i'm going
2229
01:20:31,280 --> 01:20:34,400
to hit run and boom it gives us from a
2230
01:20:34,400 --> 01:20:35,440
pear
2231
01:20:35,440 --> 01:20:37,840
to a blueberry let's try another example
2232
01:20:37,840 --> 01:20:39,520
okay keep doing this with me don't get
2233
01:20:39,520 --> 01:20:41,520
lazy on this keep trying this with me
2234
01:20:41,520 --> 01:20:43,840
while i take a sip of my water that
2235
01:20:43,840 --> 01:20:45,679
my girlfriend brought me but try getting
2236
01:20:45,679 --> 01:20:49,600
me apple to a pair okay try getting me
2237
01:20:49,600 --> 01:20:50,560
this
2238
01:20:50,560 --> 01:20:54,880
right over here okay take a second
2239
01:20:55,040 --> 01:20:58,159
um and see if you can get me those three
2240
01:20:58,159 --> 01:21:00,000
right here okay so take about five
2241
01:21:00,000 --> 01:21:01,120
seconds
2242
01:21:01,120 --> 01:21:02,320
all right
2243
01:21:02,320 --> 01:21:03,760
five
2244
01:21:03,760 --> 01:21:06,000
four or take take a little bit longer
2245
01:21:06,000 --> 01:21:08,000
while i sip this water
2246
01:21:08,000 --> 01:21:11,000
okay
2247
01:21:14,400 --> 01:21:16,480
so let's give that a try
2248
01:21:16,480 --> 01:21:18,239
i'm gonna go ahead and do i'm gonna
2249
01:21:18,239 --> 01:21:20,320
change this to a
2250
01:21:20,320 --> 01:21:22,960
one because one will get me the apple
2251
01:21:22,960 --> 01:21:24,560
and it'll be zero
2252
01:21:24,560 --> 01:21:27,760
one two three and four one ahead of the
2253
01:21:27,760 --> 01:21:30,000
one that we need okay and let's go four
2254
01:21:30,000 --> 01:21:32,000
let's hit run
2255
01:21:32,000 --> 01:21:34,639
and i get apple to appear exactly like i
2256
01:21:34,639 --> 01:21:37,360
wanted right it was that
2257
01:21:37,360 --> 01:21:39,840
that's what i wanted
2258
01:21:39,840 --> 01:21:42,960
and that's what we got
2259
01:21:42,960 --> 01:21:44,560
perfect
2260
01:21:44,560 --> 01:21:47,520
very very good very good okay let's keep
2261
01:21:47,520 --> 01:21:49,440
making progress now all right so now
2262
01:21:49,440 --> 01:21:53,840
before we graduate from uh slice
2263
01:21:53,840 --> 01:21:55,600
and if you notice that i changed my
2264
01:21:55,600 --> 01:21:58,000
shirt i just had to do an interview
2265
01:21:58,000 --> 01:21:59,679
because they were because my girlfriend
2266
01:21:59,679 --> 01:22:02,960
is a wow superhero she's candy crush
2267
01:22:02,960 --> 01:22:04,960
and um they're doing a documentary on
2268
01:22:04,960 --> 01:22:06,800
her so i had to go do the interview and
2269
01:22:06,800 --> 01:22:07,840
i had to change my shirt because i
2270
01:22:07,840 --> 01:22:09,760
wanted something black and my previous
2271
01:22:09,760 --> 01:22:11,679
shirt was yellow and nice and poppy and
2272
01:22:11,679 --> 01:22:13,440
so i don't know why i'm telling you that
2273
01:22:13,440 --> 01:22:14,239
but
2274
01:22:14,239 --> 01:22:16,320
i thought you should know let's go back
2275
01:22:16,320 --> 01:22:18,400
all right
2276
01:22:18,400 --> 01:22:20,480
um so here
2277
01:22:20,480 --> 01:22:22,480
groceries slice
2278
01:22:22,480 --> 01:22:25,280
so this is good but before we graduate
2279
01:22:25,280 --> 01:22:26,400
i'm going to teach you about two
2280
01:22:26,400 --> 01:22:28,080
different array methods okay so there's
2281
01:22:28,080 --> 01:22:29,440
a couple of array methods that i want
2282
01:22:29,440 --> 01:22:30,639
you to know so
2283
01:22:30,639 --> 01:22:33,360
we went over slice we went over push
2284
01:22:33,360 --> 01:22:37,360
i want you to learn index of and i want
2285
01:22:37,360 --> 01:22:39,040
you to learn
2286
01:22:39,040 --> 01:22:41,360
uh length okay
2287
01:22:41,360 --> 01:22:44,080
this is probably the most like one of
2288
01:22:44,080 --> 01:22:46,320
the most common ones length uh so let's
2289
01:22:46,320 --> 01:22:48,560
just go through it together real quick
2290
01:22:48,560 --> 01:22:50,480
so now i'm going to say console.log and
2291
01:22:50,480 --> 01:22:53,280
we'll do groceries okay
2292
01:22:53,280 --> 01:22:56,400
and i'm gonna do index of and what index
2293
01:22:56,400 --> 01:22:59,760
of does it it will give me the index of
2294
01:22:59,760 --> 01:23:01,360
these fruits okay
2295
01:23:01,360 --> 01:23:03,840
so let me just comment out these i
2296
01:23:03,840 --> 01:23:06,400
comment these uh slices out here so i'm
2297
01:23:06,400 --> 01:23:08,080
going to hit run
2298
01:23:08,080 --> 01:23:09,600
let's just do
2299
01:23:09,600 --> 01:23:11,280
let's just run that one so we can see
2300
01:23:11,280 --> 01:23:12,480
all of our
2301
01:23:12,480 --> 01:23:14,719
groceries or fruits
2302
01:23:14,719 --> 01:23:16,560
perfect and so index of what it's going
2303
01:23:16,560 --> 01:23:18,000
to do is if i ask for the index of
2304
01:23:18,000 --> 01:23:19,360
orange it's going to literally give me
2305
01:23:19,360 --> 01:23:20,960
what the index of that is so can you
2306
01:23:20,960 --> 01:23:22,719
tell me what the index of the orange is
2307
01:23:22,719 --> 01:23:24,960
for example banana is zero apple is one
2308
01:23:24,960 --> 01:23:26,320
so orange is
2309
01:23:26,320 --> 01:23:28,239
you guessed it too
2310
01:23:28,239 --> 01:23:30,320
so let's see what it says
2311
01:23:30,320 --> 01:23:32,880
all right so let's uncomment these lines
2312
01:23:32,880 --> 01:23:34,960
and let's go here and we're going to pop
2313
01:23:34,960 --> 01:23:36,639
in orange here
2314
01:23:36,639 --> 01:23:38,080
okay we're going to say hey what's the
2315
01:23:38,080 --> 01:23:40,880
index of this orange let's run it
2316
01:23:40,880 --> 01:23:42,800
and i just gave back 2.
2317
01:23:42,800 --> 01:23:45,520
all right let's ask it about the pair
2318
01:23:45,520 --> 01:23:47,679
and let's run it and it gave back three
2319
01:23:47,679 --> 01:23:49,199
because this is three okay so that's
2320
01:23:49,199 --> 01:23:51,760
what index of does helpful to kind of
2321
01:23:51,760 --> 01:23:53,760
know you won't be using it too often i
2322
01:23:53,760 --> 01:23:55,760
mean honestly when i'm coding majority
2323
01:23:55,760 --> 01:23:57,120
of the times i don't use it but i just
2324
01:23:57,120 --> 01:23:58,960
thought since this is the array section
2325
01:23:58,960 --> 01:24:00,560
i cover these methods because they're
2326
01:24:00,560 --> 01:24:02,239
going to be helpful for you all right
2327
01:24:02,239 --> 01:24:04,080
i'll comment this out for now so index
2328
01:24:04,080 --> 01:24:05,920
of and then the other method you need to
2329
01:24:05,920 --> 01:24:08,320
know is dot length okay so that one is
2330
01:24:08,320 --> 01:24:09,679
going to be important so let's go ahead
2331
01:24:09,679 --> 01:24:12,000
and do console.log
2332
01:24:12,000 --> 01:24:13,920
groceries and can you guess what length
2333
01:24:13,920 --> 01:24:15,280
is going to tell you
2334
01:24:15,280 --> 01:24:16,480
dot length
2335
01:24:16,480 --> 01:24:19,280
length starts from 1
2336
01:24:19,280 --> 01:24:21,199
and so it will take the overall length
2337
01:24:21,199 --> 01:24:22,960
of this so however many items are in
2338
01:24:22,960 --> 01:24:23,760
here
2339
01:24:23,760 --> 01:24:25,679
is going to give you that back so let's
2340
01:24:25,679 --> 01:24:29,679
count one two three four five
2341
01:24:29,679 --> 01:24:30,560
six
2342
01:24:30,560 --> 01:24:33,679
so length is going to give you back
2343
01:24:33,679 --> 01:24:34,560
six
2344
01:24:34,560 --> 01:24:36,000
all right let's give it a try and let's
2345
01:24:36,000 --> 01:24:38,639
run it boom there we go see that six
2346
01:24:38,639 --> 01:24:40,719
right there
2347
01:24:40,719 --> 01:24:42,960
that's it that's that's what uh that's
2348
01:24:42,960 --> 01:24:44,400
what length does
2349
01:24:44,400 --> 01:24:47,440
okay perfect so now we can basically say
2350
01:24:47,440 --> 01:24:50,880
you've graduated from arrays yay good
2351
01:24:50,880 --> 01:24:53,760
job there's a bunch more to it but this
2352
01:24:53,760 --> 01:24:56,480
is just over simplified you know what
2353
01:24:56,480 --> 01:24:59,360
arrays can do you'll learn a lot more
2354
01:24:59,360 --> 01:25:02,080
about arrays a lot better once we get to
2355
01:25:02,080 --> 01:25:03,760
the loops because there's a lot of cool
2356
01:25:03,760 --> 01:25:05,520
things you could do with arrays they're
2357
01:25:05,520 --> 01:25:08,159
like my favorite thing in coding i think
2358
01:25:08,159 --> 01:25:10,080
arrays are literally my favorite thing
2359
01:25:10,080 --> 01:25:11,840
and next up we're gonna start learning
2360
01:25:11,840 --> 01:25:14,000
about objects okay so let's go ahead and
2361
01:25:14,000 --> 01:25:15,920
start learning about them now all right
2362
01:25:15,920 --> 01:25:17,920
now let's talk about objects so here's
2363
01:25:17,920 --> 01:25:19,040
the first thing we're gonna do we're
2364
01:25:19,040 --> 01:25:22,639
gonna comment out line 144 and 135 so
2365
01:25:22,639 --> 01:25:24,960
there's nothing being printed okay let's
2366
01:25:24,960 --> 01:25:26,719
go ahead and hit run and look at that we
2367
01:25:26,719 --> 01:25:28,639
have nothing coming out to the console
2368
01:25:28,639 --> 01:25:30,719
log that's perfect all right now let's
2369
01:25:30,719 --> 01:25:33,679
go ahead and let's talk about objects
2370
01:25:33,679 --> 01:25:35,600
objects are really really important and
2371
01:25:35,600 --> 01:25:37,679
they're really awesome so in just one
2372
01:25:37,679 --> 01:25:39,840
second we're gonna work on right when i
2373
01:25:39,840 --> 01:25:41,440
snap my fingers we're gonna work on a
2374
01:25:41,440 --> 01:25:43,760
new object all right this is the object
2375
01:25:43,760 --> 01:25:45,920
we're gonna be working on so as you can
2376
01:25:45,920 --> 01:25:50,080
see here we have mr leonardo dicaprio he
2377
01:25:50,080 --> 01:25:53,120
is our he's our object in this case and
2378
01:25:53,120 --> 01:25:54,880
he has a couple of properties so his
2379
01:25:54,880 --> 01:25:57,760
properties are he has a name so if you
2380
01:25:57,760 --> 01:26:00,159
do person.name you know that should we
2381
01:26:00,159 --> 01:26:02,480
need to set that to leonardo
2382
01:26:02,480 --> 01:26:06,159
and then his shirt here will be white
2383
01:26:06,159 --> 01:26:07,920
okay because if you can actually see his
2384
01:26:07,920 --> 01:26:09,199
shirt is
2385
01:26:09,199 --> 01:26:11,679
white back there so we need to create an
2386
01:26:11,679 --> 01:26:13,840
object that actually represents that so
2387
01:26:13,840 --> 01:26:15,920
let's go ahead and do that now so i'm
2388
01:26:15,920 --> 01:26:17,360
going to put him
2389
01:26:17,360 --> 01:26:19,440
over here kind of in the corner so you
2390
01:26:19,440 --> 01:26:21,440
can see him now let's create an object
2391
01:26:21,440 --> 01:26:22,880
to represent this so we're going to say
2392
01:26:22,880 --> 01:26:25,360
cons person and object is created by
2393
01:26:25,360 --> 01:26:27,280
doing these squigglies okay so these
2394
01:26:27,280 --> 01:26:30,320
squigglies represent an object so so far
2395
01:26:30,320 --> 01:26:32,719
right here we have created essentially
2396
01:26:32,719 --> 01:26:33,760
an
2397
01:26:33,760 --> 01:26:35,360
empty object
2398
01:26:35,360 --> 01:26:36,800
now what we're going to do is we're
2399
01:26:36,800 --> 01:26:39,520
going to add some properties
2400
01:26:39,520 --> 01:26:40,639
to that
2401
01:26:40,639 --> 01:26:43,360
person object okay so we're going to add
2402
01:26:43,360 --> 01:26:46,159
a name so the name of this person in
2403
01:26:46,159 --> 01:26:50,320
this case will be leonardo and shirt
2404
01:26:50,320 --> 01:26:51,679
will be white
2405
01:26:51,679 --> 01:26:54,080
this is an object if i format this you
2406
01:26:54,080 --> 01:26:56,080
see this is how an object looks if you
2407
01:26:56,080 --> 01:26:57,600
want to make it look a little bit like
2408
01:26:57,600 --> 01:27:00,239
prettier and kind of how you know pros
2409
01:27:00,239 --> 01:27:02,400
write prose would probably make it look
2410
01:27:02,400 --> 01:27:04,480
like that okay kind of how it looks like
2411
01:27:04,480 --> 01:27:06,960
here so open squiggly close quickly it
2412
01:27:06,960 --> 01:27:08,159
doesn't matter if you have it on the
2413
01:27:08,159 --> 01:27:10,080
same line or you don't okay so it
2414
01:27:10,080 --> 01:27:12,080
doesn't really matter which way you have
2415
01:27:12,080 --> 01:27:12,880
it
2416
01:27:12,880 --> 01:27:15,360
both ways will work all right now this
2417
01:27:15,360 --> 01:27:18,239
here pretty fairly represents what we
2418
01:27:18,239 --> 01:27:21,520
have over here you have a person object
2419
01:27:21,520 --> 01:27:23,600
the name is leonardo shirt is white now
2420
01:27:23,600 --> 01:27:25,120
let's go ahead and console.log and
2421
01:27:25,120 --> 01:27:27,440
access some of the properties of this
2422
01:27:27,440 --> 01:27:29,040
object that we created so we're going to
2423
01:27:29,040 --> 01:27:31,520
do person.name and when i console this
2424
01:27:31,520 --> 01:27:33,280
you're gonna see that the person name
2425
01:27:33,280 --> 01:27:35,520
and i'm gonna move this up here okay
2426
01:27:35,520 --> 01:27:37,840
when i when i run this you can see that
2427
01:27:37,840 --> 01:27:40,400
the person name is indeed leonardo kind
2428
01:27:40,400 --> 01:27:42,320
of just like what we wanted right and
2429
01:27:42,320 --> 01:27:45,040
the start and can you guess what person
2430
01:27:45,040 --> 01:27:46,639
that shirt is going to give me when i
2431
01:27:46,639 --> 01:27:49,360
type that in and when i hit run take a
2432
01:27:49,360 --> 01:27:51,120
guess
2433
01:27:51,120 --> 01:27:54,000
exactly you're going to get white okay
2434
01:27:54,000 --> 01:27:56,400
just like we had expected here because
2435
01:27:56,400 --> 01:27:57,920
those are the properties and that's how
2436
01:27:57,920 --> 01:28:01,280
you access them so that's how objects
2437
01:28:01,280 --> 01:28:03,199
work so first let's learn different ways
2438
01:28:03,199 --> 01:28:05,440
of accessing this object okay dot
2439
01:28:05,440 --> 01:28:06,719
notation
2440
01:28:06,719 --> 01:28:09,840
versus bracket notation these both of
2441
01:28:09,840 --> 01:28:11,760
these you should know okay so how do you
2442
01:28:11,760 --> 01:28:13,120
access objects so you're right now
2443
01:28:13,120 --> 01:28:15,360
you're using a dot notation now here's
2444
01:28:15,360 --> 01:28:18,239
an example of a bracket notation let's
2445
01:28:18,239 --> 01:28:19,840
do console.log and we're going to say
2446
01:28:19,840 --> 01:28:22,000
person and we're going to do name like
2447
01:28:22,000 --> 01:28:23,040
this
2448
01:28:23,040 --> 01:28:26,320
and let's run it and boom you also get
2449
01:28:26,320 --> 01:28:28,480
leonardo right over here
2450
01:28:28,480 --> 01:28:30,960
well give it a try try doing the shirt
2451
01:28:30,960 --> 01:28:32,560
but with the bracket notation try
2452
01:28:32,560 --> 01:28:34,000
accessing it i'll give you five seconds
2453
01:28:34,000 --> 01:28:38,880
five four three two one all right let's
2454
01:28:38,880 --> 01:28:40,800
see if you got it so i just triple
2455
01:28:40,800 --> 01:28:42,800
clicked to copy and then i did command c
2456
01:28:42,800 --> 01:28:44,480
to copy and then i went down and command
2457
01:28:44,480 --> 01:28:46,800
v to paste you could do ctrl c or ctrl v
2458
01:28:46,800 --> 01:28:48,960
to copy and paste and then double click
2459
01:28:48,960 --> 01:28:50,800
the name inside here change that to
2460
01:28:50,800 --> 01:28:53,280
shirt hit command s or ctrl s if you're
2461
01:28:53,280 --> 01:28:55,520
on windows to save and automatically run
2462
01:28:55,520 --> 01:28:56,719
apparently it's not automatically
2463
01:28:56,719 --> 01:28:58,960
running so let's run click the button
2464
01:28:58,960 --> 01:29:01,440
here or command enter and boom you can
2465
01:29:01,440 --> 01:29:03,600
see that the shirt is coming out white
2466
01:29:03,600 --> 01:29:07,120
right so we got different ways access an
2467
01:29:07,120 --> 01:29:08,960
object that's really good now there are
2468
01:29:08,960 --> 01:29:11,120
different ways to assign
2469
01:29:11,120 --> 01:29:12,639
things to an object as well so for
2470
01:29:12,639 --> 01:29:14,719
example i could give him a phone number
2471
01:29:14,719 --> 01:29:17,679
okay so i could actually do person.phone
2472
01:29:17,679 --> 01:29:19,520
so notice we don't actually have phone
2473
01:29:19,520 --> 01:29:21,920
in here and i could do phone
2474
01:29:21,920 --> 01:29:25,440
and we could give him a number
2475
01:29:25,440 --> 01:29:27,280
like that okay
2476
01:29:27,280 --> 01:29:29,440
person not phone and then we're going to
2477
01:29:29,440 --> 01:29:32,000
go ahead and do console.log
2478
01:29:32,000 --> 01:29:35,440
and we'll say person. let's go ahead and
2479
01:29:35,440 --> 01:29:37,920
try it let's see what happens and boom
2480
01:29:37,920 --> 01:29:39,679
you can see that
2481
01:29:39,679 --> 01:29:41,600
we actually get that phone number right
2482
01:29:41,600 --> 01:29:44,320
there but also you can actually console
2483
01:29:44,320 --> 01:29:45,920
log the entire person as well so you can
2484
01:29:45,920 --> 01:29:47,280
see what that looks like so let's go
2485
01:29:47,280 --> 01:29:49,840
console log person let's go ahead let's
2486
01:29:49,840 --> 01:29:50,960
hit run
2487
01:29:50,960 --> 01:29:54,239
and you can see you can see this object
2488
01:29:54,239 --> 01:29:55,600
right over here
2489
01:29:55,600 --> 01:29:58,880
name shirt phone so now this object has
2490
01:29:58,880 --> 01:30:00,880
all of the things and it didn't have
2491
01:30:00,880 --> 01:30:04,159
that before so it's kind of like how in
2492
01:30:04,159 --> 01:30:06,560
arrays we were almost doing like push
2493
01:30:06,560 --> 01:30:08,000
it's kind of like that but like it's
2494
01:30:08,000 --> 01:30:09,760
just you're creating a new key on the
2495
01:30:09,760 --> 01:30:12,639
fly and giving it a property another way
2496
01:30:12,639 --> 01:30:14,239
you could actually create that property
2497
01:30:14,239 --> 01:30:16,320
is by putting this in bracket notations
2498
01:30:16,320 --> 01:30:18,159
as well because you could go like this
2499
01:30:18,159 --> 01:30:20,159
bracket notation and say hey person
2500
01:30:20,159 --> 01:30:22,639
phone is actually equal to this hit run
2501
01:30:22,639 --> 01:30:24,719
and watch what happens boom you got the
2502
01:30:24,719 --> 01:30:27,360
same thing and that also works so both
2503
01:30:27,360 --> 01:30:29,120
work okay the reason why we're putting
2504
01:30:29,120 --> 01:30:31,120
the phone number in quotes by the way as
2505
01:30:31,120 --> 01:30:33,360
in we're making it a string is because
2506
01:30:33,360 --> 01:30:35,600
if you didn't do that it would try to
2507
01:30:35,600 --> 01:30:38,800
subtract it will do 1 minus 2 2 2 minus
2508
01:30:38,800 --> 01:30:42,800
3 3 3 minus 4 4 4 and evaluate to a
2509
01:30:42,800 --> 01:30:44,639
big negative number
2510
01:30:44,639 --> 01:30:46,239
that's not what we want right so put
2511
01:30:46,239 --> 01:30:49,120
that in quotes because we want the exact
2512
01:30:49,120 --> 01:30:50,400
phone number and phone numbers or
2513
01:30:50,400 --> 01:30:52,320
strings all right so now let's say you
2514
01:30:52,320 --> 01:30:54,960
want to create another person okay so
2515
01:30:54,960 --> 01:30:56,880
i'll give you an exercise for you to do
2516
01:30:56,880 --> 01:30:58,800
and let's go ahead and do this
2517
01:30:58,800 --> 01:30:59,840
right now so i'm going to give you a
2518
01:30:59,840 --> 01:31:02,000
different person with different name and
2519
01:31:02,000 --> 01:31:03,280
different shirt and i'm going to need
2520
01:31:03,280 --> 01:31:04,960
you to create an object
2521
01:31:04,960 --> 01:31:07,360
a person object that represents that
2522
01:31:07,360 --> 01:31:10,159
okay so you can call that object person2
2523
01:31:10,159 --> 01:31:11,679
alright so let's go ahead and do that
2524
01:31:11,679 --> 01:31:13,520
here you're going to call this object
2525
01:31:13,520 --> 01:31:15,280
person2
2526
01:31:15,280 --> 01:31:16,800
all right so now instead of leonardo
2527
01:31:16,800 --> 01:31:18,159
dicaprio you're going to get somebody
2528
01:31:18,159 --> 01:31:20,480
better yours truly
2529
01:31:20,480 --> 01:31:23,440
and you have the object right over here
2530
01:31:23,440 --> 01:31:25,760
okay so this is the object that you have
2531
01:31:25,760 --> 01:31:27,360
and these are the properties that this
2532
01:31:27,360 --> 01:31:30,800
object have okay so try to see if you
2533
01:31:30,800 --> 01:31:33,679
can make this object by yourself either
2534
01:31:33,679 --> 01:31:35,040
pause my video
2535
01:31:35,040 --> 01:31:37,120
or take a couple of seconds right now to
2536
01:31:37,120 --> 01:31:39,040
do this but i'm gonna start doing this
2537
01:31:39,040 --> 01:31:43,520
in five four three two one okay let's do
2538
01:31:43,520 --> 01:31:45,520
it so i'm going to basically there's a
2539
01:31:45,520 --> 01:31:47,199
cons person two
2540
01:31:47,199 --> 01:31:50,880
and i'm going to say name is causey
2541
01:31:50,880 --> 01:31:52,880
right and make sure to put a comma so
2542
01:31:52,880 --> 01:31:54,719
after every property right so let's go
2543
01:31:54,719 --> 01:31:57,520
through an object so we have squiggly
2544
01:31:57,520 --> 01:32:00,400
bass squigglies in the start and closing
2545
01:32:00,400 --> 01:32:02,800
okay instead of brackets
2546
01:32:02,800 --> 01:32:05,840
object uh arrays have brackets
2547
01:32:05,840 --> 01:32:08,320
and then you have a key and a value all
2548
01:32:08,320 --> 01:32:13,040
right so objects are key value pairs
2549
01:32:13,040 --> 01:32:15,040
all right so you have a key here and a
2550
01:32:15,040 --> 01:32:17,760
value here a key here and a value here
2551
01:32:17,760 --> 01:32:18,719
okay
2552
01:32:18,719 --> 01:32:19,600
so
2553
01:32:19,600 --> 01:32:21,440
a key you can also sometimes call it a
2554
01:32:21,440 --> 01:32:24,480
property okay so here the key is name
2555
01:32:24,480 --> 01:32:27,360
and the key is shirt okay and name and
2556
01:32:27,360 --> 01:32:28,800
shirt is also the property so you could
2557
01:32:28,800 --> 01:32:31,280
say kind of both ways
2558
01:32:31,280 --> 01:32:34,080
so you put in the key right person name
2559
01:32:34,080 --> 01:32:35,840
and you get back the value which is
2560
01:32:35,840 --> 01:32:37,440
actually the name
2561
01:32:37,440 --> 01:32:38,800
right you do the same thing here with
2562
01:32:38,800 --> 01:32:39,920
the phone
2563
01:32:39,920 --> 01:32:42,080
so you take your object
2564
01:32:42,080 --> 01:32:43,760
you put in the key
2565
01:32:43,760 --> 01:32:46,080
right and then that key unlocks and we
2566
01:32:46,080 --> 01:32:48,639
set this value for that key
2567
01:32:48,639 --> 01:32:50,800
again we do person.phone so we did the
2568
01:32:50,800 --> 01:32:53,360
object and the key and then the key
2569
01:32:53,360 --> 01:32:54,960
gives us the value
2570
01:32:54,960 --> 01:32:56,480
all right kind of think of it like
2571
01:32:56,480 --> 01:32:59,120
opening a lock all right so name here so
2572
01:32:59,120 --> 01:33:00,880
this is the first key value the second
2573
01:33:00,880 --> 01:33:03,360
key value we're gonna set is shirt
2574
01:33:03,360 --> 01:33:04,719
is black
2575
01:33:04,719 --> 01:33:07,840
okay and uh that's it that's the way you
2576
01:33:07,840 --> 01:33:09,520
represent this object right so if we do
2577
01:33:09,520 --> 01:33:12,560
console log person2
2578
01:33:12,560 --> 01:33:14,639
and run it
2579
01:33:14,639 --> 01:33:17,520
boom you can actually see that it's
2580
01:33:17,520 --> 01:33:19,760
showing you quasi
2581
01:33:19,760 --> 01:33:22,880
uh and shirt is black okay that's super
2582
01:33:22,880 --> 01:33:25,679
cool and uh if i change this to you know
2583
01:33:25,679 --> 01:33:28,000
what if i want quasi's name what if i
2584
01:33:28,000 --> 01:33:29,920
only want the name can you try that for
2585
01:33:29,920 --> 01:33:32,560
me if i only want the name
2586
01:33:32,560 --> 01:33:34,560
boom hopefully you got this correct okay
2587
01:33:34,560 --> 01:33:36,639
let's run it and now we're getting the
2588
01:33:36,639 --> 01:33:38,800
name well what if i want the shirt as
2589
01:33:38,800 --> 01:33:39,920
well
2590
01:33:39,920 --> 01:33:42,239
same idea right if i run this we're
2591
01:33:42,239 --> 01:33:44,239
gonna also now get the shirt so shirt
2592
01:33:44,239 --> 01:33:45,199
color
2593
01:33:45,199 --> 01:33:47,120
and we got it as black let's turn this
2594
01:33:47,120 --> 01:33:49,040
into a function that then
2595
01:33:49,040 --> 01:33:53,120
says hi my name is kazee and my shirt is
2596
01:33:53,120 --> 01:33:55,760
black okay so let's go ahead and create
2597
01:33:55,760 --> 01:33:57,920
a function okay so remember how we
2598
01:33:57,920 --> 01:33:58,960
create functions right there are
2599
01:33:58,960 --> 01:34:01,199
different ways you could do function
2600
01:34:01,199 --> 01:34:02,880
or you could do the
2601
01:34:02,880 --> 01:34:06,080
fancy way like this
2602
01:34:06,080 --> 01:34:08,239
we'll call this function introducer
2603
01:34:08,239 --> 01:34:10,000
because it introduces you to other
2604
01:34:10,000 --> 01:34:11,360
people okay
2605
01:34:11,360 --> 01:34:13,840
and this is going to take in
2606
01:34:13,840 --> 01:34:16,719
let's say we can give it a
2607
01:34:16,719 --> 01:34:17,760
shirt
2608
01:34:17,760 --> 01:34:20,800
and uh it will also take in a name so
2609
01:34:20,800 --> 01:34:23,360
let's go name and shirt okay
2610
01:34:23,360 --> 01:34:24,719
and if we want to turn this whole thing
2611
01:34:24,719 --> 01:34:26,639
into oh sorry whoops and we're going to
2612
01:34:26,639 --> 01:34:27,840
do
2613
01:34:27,840 --> 01:34:29,199
if you want to do error functions this
2614
01:34:29,199 --> 01:34:30,480
is how we're going to do them okay so
2615
01:34:30,480 --> 01:34:32,960
we're going to go ahead and say
2616
01:34:32,960 --> 01:34:35,600
name comma shirt and we're going to do
2617
01:34:35,600 --> 01:34:38,400
arrows like this i'm going to go here
2618
01:34:38,400 --> 01:34:40,719
all right now the final sentence that
2619
01:34:40,719 --> 01:34:42,320
we're going to return
2620
01:34:42,320 --> 01:34:44,159
right is going to basically be remember
2621
01:34:44,159 --> 01:34:46,159
template literals all right so let's use
2622
01:34:46,159 --> 01:34:48,000
the template literals again and we're
2623
01:34:48,000 --> 01:34:49,119
gonna say
2624
01:34:49,119 --> 01:34:50,639
hi my
2625
01:34:50,639 --> 01:34:52,719
name is name
2626
01:34:52,719 --> 01:34:54,719
okay and we're just gonna say name like
2627
01:34:54,719 --> 01:34:55,920
this for now
2628
01:34:55,920 --> 01:34:59,199
my name is name and the color of my
2629
01:34:59,199 --> 01:35:01,440
shirt is whatever the color of your
2630
01:35:01,440 --> 01:35:03,280
shirt is okay so in this case it should
2631
01:35:03,280 --> 01:35:04,400
be like black
2632
01:35:04,400 --> 01:35:06,000
so here let's say if we had it
2633
01:35:06,000 --> 01:35:07,760
hard-coded it would be like this but we
2634
01:35:07,760 --> 01:35:10,960
don't want it hardco um we don't want it
2635
01:35:10,960 --> 01:35:13,440
hard-coded right so we're gonna do here
2636
01:35:13,440 --> 01:35:15,760
is name we're going to make it dynamic
2637
01:35:15,760 --> 01:35:17,440
we're going to send name
2638
01:35:17,440 --> 01:35:19,360
and remember and see if you can make
2639
01:35:19,360 --> 01:35:22,400
this shirt dynamic as well boom we're
2640
01:35:22,400 --> 01:35:23,679
going to say sure
2641
01:35:23,679 --> 01:35:25,280
and we're going to save this into a
2642
01:35:25,280 --> 01:35:28,320
variable called intro the const intro is
2643
01:35:28,320 --> 01:35:29,760
equal to that
2644
01:35:29,760 --> 01:35:32,000
but we're going to make an object first
2645
01:35:32,000 --> 01:35:34,159
so just so you can keep practicing an
2646
01:35:34,159 --> 01:35:35,920
object okay so we're basically going to
2647
01:35:35,920 --> 01:35:39,119
say we're going to create a cons person
2648
01:35:39,119 --> 01:35:42,239
object and this object is going to have
2649
01:35:42,239 --> 01:35:45,520
a key name and the value of that key is
2650
01:35:45,520 --> 01:35:46,639
also going to be named because that's
2651
01:35:46,639 --> 01:35:48,000
what we're passing it i'm just going to
2652
01:35:48,000 --> 01:35:49,760
say name right here and it's also going
2653
01:35:49,760 --> 01:35:51,440
to have a shirt and that's just going to
2654
01:35:51,440 --> 01:35:53,520
be the shirt that we pass it okay so
2655
01:35:53,520 --> 01:35:54,480
these are
2656
01:35:54,480 --> 01:35:56,960
values that we're giving it these are
2657
01:35:56,960 --> 01:35:59,679
the actual keys that are hard coded okay
2658
01:35:59,679 --> 01:36:01,199
so these will stay the same these will
2659
01:36:01,199 --> 01:36:03,520
be exactly name and exactly shirt and
2660
01:36:03,520 --> 01:36:05,360
this part will be dynamic these are the
2661
01:36:05,360 --> 01:36:07,600
values that we give it and so then we're
2662
01:36:07,600 --> 01:36:11,440
gonna in order to get this person's name
2663
01:36:11,440 --> 01:36:13,119
remember what we have to do right it's
2664
01:36:13,119 --> 01:36:14,960
person dot
2665
01:36:14,960 --> 01:36:17,119
name perfect and now how are we gonna
2666
01:36:17,119 --> 01:36:19,199
get this shirt we're gonna do person dot
2667
01:36:19,199 --> 01:36:20,000
what
2668
01:36:20,000 --> 01:36:21,679
person dot
2669
01:36:21,679 --> 01:36:24,480
shirt perfect good job if you kept up
2670
01:36:24,480 --> 01:36:26,719
with that okay so we're creating an
2671
01:36:26,719 --> 01:36:29,199
object right here and we're creating the
2672
01:36:29,199 --> 01:36:30,800
intro and now we're just gonna say
2673
01:36:30,800 --> 01:36:33,360
return the intro all right
2674
01:36:33,360 --> 01:36:35,440
that's our function and let's give it a
2675
01:36:35,440 --> 01:36:39,199
try so let's go console.log let's call
2676
01:36:39,199 --> 01:36:41,440
our function introducer and let's give
2677
01:36:41,440 --> 01:36:44,639
it a name qazi and let's give it a shirt
2678
01:36:44,639 --> 01:36:47,280
color and we'll say black and let's run
2679
01:36:47,280 --> 01:36:49,920
this and look at that right we're
2680
01:36:49,920 --> 01:36:52,560
getting hi my name is kazi and the color
2681
01:36:52,560 --> 01:36:55,280
of my shirt is black beautiful so we're
2682
01:36:55,280 --> 01:36:57,199
using template little i mean this is
2683
01:36:57,199 --> 01:36:59,679
advanced this is awesome if you're doing
2684
01:36:59,679 --> 01:37:01,440
this good job for doing this this is
2685
01:37:01,440 --> 01:37:03,280
using this is a
2686
01:37:03,280 --> 01:37:06,639
es6 error function right this function
2687
01:37:06,639 --> 01:37:07,520
has
2688
01:37:07,520 --> 01:37:09,119
two arguments
2689
01:37:09,119 --> 01:37:12,159
and you created an object in here
2690
01:37:12,159 --> 01:37:15,280
and then you're using template literals
2691
01:37:15,280 --> 01:37:17,119
so there are a lot of concepts in here
2692
01:37:17,119 --> 01:37:19,679
that you're doing so great job on
2693
01:37:19,679 --> 01:37:21,280
following along and doing this with me
2694
01:37:21,280 --> 01:37:23,920
even if you didn't get it it's good that
2695
01:37:23,920 --> 01:37:25,920
you're you know you just followed along
2696
01:37:25,920 --> 01:37:27,840
and like did it and hopefully you got it
2697
01:37:27,840 --> 01:37:29,440
working on your computer like that's
2698
01:37:29,440 --> 01:37:32,000
what's really important so here i have
2699
01:37:32,000 --> 01:37:34,320
this and if i if i show you right hi my
2700
01:37:34,320 --> 01:37:35,679
name is kazi and the color of my shirt
2701
01:37:35,679 --> 01:37:37,920
is black now let's go ahead and comment
2702
01:37:37,920 --> 01:37:39,760
out all the other stuff that we had here
2703
01:37:39,760 --> 01:37:42,080
right so i'm going to comment this out
2704
01:37:42,080 --> 01:37:44,080
um you can leave this or comment it out
2705
01:37:44,080 --> 01:37:45,920
i'll just comment it out
2706
01:37:45,920 --> 01:37:48,639
you can comment out pretty much
2707
01:37:48,639 --> 01:37:50,320
all of this
2708
01:37:50,320 --> 01:37:52,719
okay and i'll comment out this person
2709
01:37:52,719 --> 01:37:55,119
right here too and we'll just hit run
2710
01:37:55,119 --> 01:37:56,400
and it should just say hi my name is
2711
01:37:56,400 --> 01:37:58,560
kazi and the color of my shirt is black
2712
01:37:58,560 --> 01:37:59,440
okay so
2713
01:37:59,440 --> 01:38:00,880
the only console log that should be
2714
01:38:00,880 --> 01:38:02,800
happening is the one coming from this
2715
01:38:02,800 --> 01:38:04,880
function this function is super cool
2716
01:38:04,880 --> 01:38:06,320
because this function is like creating
2717
01:38:06,320 --> 01:38:08,560
an object and then doing the template
2718
01:38:08,560 --> 01:38:11,360
literals accessing data from that object
2719
01:38:11,360 --> 01:38:14,560
very nice okay this is a very very nice
2720
01:38:14,560 --> 01:38:17,040
well-written function so good job if you
2721
01:38:17,040 --> 01:38:19,199
got here so far now try
2722
01:38:19,199 --> 01:38:22,159
see if you can now do this call the same
2723
01:38:22,159 --> 01:38:24,320
function but see if you can call it on
2724
01:38:24,320 --> 01:38:26,880
leonardo and with the shirt being white
2725
01:38:26,880 --> 01:38:28,480
so give that a try i'll give you five
2726
01:38:28,480 --> 01:38:33,040
seconds five four three two one all
2727
01:38:33,040 --> 01:38:34,239
right let's do it together so i'm gonna
2728
01:38:34,239 --> 01:38:36,159
say console.log we're gonna say
2729
01:38:36,159 --> 01:38:37,920
introducer
2730
01:38:37,920 --> 01:38:40,560
and we're gonna see leonardo here
2731
01:38:40,560 --> 01:38:44,320
right and the shirt color is gonna be
2732
01:38:44,320 --> 01:38:45,119
white
2733
01:38:45,119 --> 01:38:47,600
these arguments have to match right so
2734
01:38:47,600 --> 01:38:49,679
this matches that
2735
01:38:49,679 --> 01:38:52,080
and then this matches that okay so these
2736
01:38:52,080 --> 01:38:55,600
arguments the order here matters okay
2737
01:38:55,600 --> 01:38:58,080
and uh let's run this and let's see if
2738
01:38:58,080 --> 01:39:00,320
it does the leonardo one as well and it
2739
01:39:00,320 --> 01:39:02,800
does hi my name is leonardo and the
2740
01:39:02,800 --> 01:39:05,920
color of my shirt is shirt okay so we
2741
01:39:05,920 --> 01:39:07,840
probably gotta say white here hi my name
2742
01:39:07,840 --> 01:39:09,760
is leonardo and the color of my shirt is
2743
01:39:09,760 --> 01:39:10,840
white
2744
01:39:10,840 --> 01:39:13,199
beautiful this is doing exactly what we
2745
01:39:13,199 --> 01:39:14,480
want we're going to go ahead and comment
2746
01:39:14,480 --> 01:39:15,760
this out
2747
01:39:15,760 --> 01:39:18,000
and so far you're doing absolutely
2748
01:39:18,000 --> 01:39:19,679
fantastic
2749
01:39:19,679 --> 01:39:21,920
all right now let's take this even a
2750
01:39:21,920 --> 01:39:24,400
step further let's say that we want to
2751
01:39:24,400 --> 01:39:26,080
have this person but we also want to
2752
01:39:26,080 --> 01:39:28,960
calculate and have their net worth okay
2753
01:39:28,960 --> 01:39:30,400
so i'm going to go ahead and refresh
2754
01:39:30,400 --> 01:39:33,040
this page and let's just uncomment the
2755
01:39:33,040 --> 01:39:35,679
quasi in black here perfect my name is
2756
01:39:35,679 --> 01:39:37,280
kazi and the color my shirt is black
2757
01:39:37,280 --> 01:39:40,480
blah blah perfect we want to say this
2758
01:39:40,480 --> 01:39:44,000
person has certain assets okay so assets
2759
01:39:44,000 --> 01:39:45,360
they have is like let's say a hundred
2760
01:39:45,360 --> 01:39:48,159
thousand dollars all right and the debt
2761
01:39:48,159 --> 01:39:51,520
they have is fifty thousand dollars so
2762
01:39:51,520 --> 01:39:52,960
if you wanted to calculate the net worth
2763
01:39:52,960 --> 01:39:55,360
is really easy just assets minus debt or
2764
01:39:55,360 --> 01:39:57,119
assets minus you know you could say
2765
01:39:57,119 --> 01:39:58,800
let's say liability
2766
01:39:58,800 --> 01:39:59,760
all right
2767
01:39:59,760 --> 01:40:00,639
so
2768
01:40:00,639 --> 01:40:02,080
it's how much basically money do you
2769
01:40:02,080 --> 01:40:03,280
have and then how much debt do you have
2770
01:40:03,280 --> 01:40:04,719
it's very simple for an average person
2771
01:40:04,719 --> 01:40:05,840
if you want to calculate your net worth
2772
01:40:05,840 --> 01:40:07,119
you could do that right now like it's
2773
01:40:07,119 --> 01:40:08,719
very easy you could have a negative net
2774
01:40:08,719 --> 01:40:10,080
worth or a zero net worth and if you
2775
01:40:10,080 --> 01:40:11,360
have a positive net worth that's
2776
01:40:11,360 --> 01:40:14,000
incredible so here you would have a net
2777
01:40:14,000 --> 01:40:15,600
worth of fifty thousand right hundred
2778
01:40:15,600 --> 01:40:17,440
thousand minus fifty thousand we could
2779
01:40:17,440 --> 01:40:19,280
do that right and so we could say and my
2780
01:40:19,280 --> 01:40:22,159
net worth is and i could do this and i
2781
01:40:22,159 --> 01:40:25,520
could basically say a person.assets
2782
01:40:25,520 --> 01:40:29,360
minus person dot liability like
2783
01:40:29,360 --> 01:40:30,960
abilities
2784
01:40:30,960 --> 01:40:33,360
and paste that there okay
2785
01:40:33,360 --> 01:40:35,520
so let's run this and let's see what we
2786
01:40:35,520 --> 01:40:37,520
get hi my name is kazi and the color of
2787
01:40:37,520 --> 01:40:39,199
my shirt is black and my net worth is
2788
01:40:39,199 --> 01:40:40,960
fifty thousand dollars we could put a
2789
01:40:40,960 --> 01:40:44,000
dollar sign here right and we could say
2790
01:40:44,000 --> 01:40:46,080
we could say we you you don't need to
2791
01:40:46,080 --> 01:40:48,000
say usd but you could if you want and
2792
01:40:48,000 --> 01:40:49,760
says my name is blah blah my net worth
2793
01:40:49,760 --> 01:40:53,679
is 50 000 usd so this dollar sign is uh
2794
01:40:53,679 --> 01:40:56,080
pretty much to actually put a dollar
2795
01:40:56,080 --> 01:40:57,679
sign and then the next dollar sign and
2796
01:40:57,679 --> 01:40:59,600
squigglies are to basically say hey i'm
2797
01:40:59,600 --> 01:41:01,440
writing javascript so let's make this
2798
01:41:01,440 --> 01:41:03,679
even nicer i'm going to teach you how to
2799
01:41:03,679 --> 01:41:05,040
create methods
2800
01:41:05,040 --> 01:41:07,520
inside of an object okay and anytime
2801
01:41:07,520 --> 01:41:10,159
you've seen dot met something that's
2802
01:41:10,159 --> 01:41:13,119
usually a method okay when you're
2803
01:41:13,119 --> 01:41:16,159
calling it so like math.floor remember
2804
01:41:16,159 --> 01:41:19,360
when you saw math.floor
2805
01:41:19,360 --> 01:41:22,320
this is a method if you didn't have this
2806
01:41:22,320 --> 01:41:23,760
math part here and it was just like this
2807
01:41:23,760 --> 01:41:25,920
this would be a function and if this
2808
01:41:25,920 --> 01:41:27,520
open closed parenthesis weren't here
2809
01:41:27,520 --> 01:41:29,440
this would be a property so i know it
2810
01:41:29,440 --> 01:41:31,280
can be confusing but here's a great
2811
01:41:31,280 --> 01:41:33,520
example all right you have an object
2812
01:41:33,520 --> 01:41:35,920
right here properties are just with dot
2813
01:41:35,920 --> 01:41:38,159
to car.name card.model those are all
2814
01:41:38,159 --> 01:41:40,080
properties right but
2815
01:41:40,080 --> 01:41:42,400
methods are car.start with the open
2816
01:41:42,400 --> 01:41:44,320
close print card.drive open close print
2817
01:41:44,320 --> 01:41:46,480
car.break open close paren so methods
2818
01:41:46,480 --> 01:41:48,400
look like this all right so just kind of
2819
01:41:48,400 --> 01:41:50,639
take a mental screenshot of this for now
2820
01:41:50,639 --> 01:41:51,760
all right so here's what we're going to
2821
01:41:51,760 --> 01:41:54,239
do here's a really cool thing in kind of
2822
01:41:54,239 --> 01:41:55,520
advanced things you could do with
2823
01:41:55,520 --> 01:41:57,760
objects so i can actually create a net
2824
01:41:57,760 --> 01:41:59,840
worth field and what this will do is
2825
01:41:59,840 --> 01:42:01,119
what i essentially want to do is want to
2826
01:42:01,119 --> 01:42:04,639
say hey assets minus liabilities but if
2827
01:42:04,639 --> 01:42:06,239
i do this i'm going to run into a
2828
01:42:06,239 --> 01:42:08,719
problem okay so let's try that so let's
2829
01:42:08,719 --> 01:42:11,719
remove all of this and let's paste just
2830
01:42:11,719 --> 01:42:13,840
netperson.net worth and let's see what
2831
01:42:13,840 --> 01:42:16,159
happens if we run this and it says
2832
01:42:16,159 --> 01:42:18,080
assets that's not defined and and you're
2833
01:42:18,080 --> 01:42:19,840
probably thinking wait assets is defined
2834
01:42:19,840 --> 01:42:21,840
right here so why isn't it just using
2835
01:42:21,840 --> 01:42:24,080
assets this is that's not how it works
2836
01:42:24,080 --> 01:42:25,920
so in when you're defining an object
2837
01:42:25,920 --> 01:42:27,600
because all of this is happening at the
2838
01:42:27,600 --> 01:42:31,280
same exact time this isn't like created
2839
01:42:31,280 --> 01:42:33,600
yet and so it doesn't know so what you
2840
01:42:33,600 --> 01:42:34,800
want to be able to do is you want to be
2841
01:42:34,800 --> 01:42:37,679
able to say person dot assets but it
2842
01:42:37,679 --> 01:42:40,239
also won't really know that so here's a
2843
01:42:40,239 --> 01:42:42,159
hack here's a way that we do this okay
2844
01:42:42,159 --> 01:42:44,159
so we're going to create a function and
2845
01:42:44,159 --> 01:42:46,239
yes you can actually store functions
2846
01:42:46,239 --> 01:42:48,560
inside of objects which is kind of crazy
2847
01:42:48,560 --> 01:42:50,320
open close paren all right we're just
2848
01:42:50,320 --> 01:42:51,760
going to do this this is going to be
2849
01:42:51,760 --> 01:42:53,199
your net worth function and what it's
2850
01:42:53,199 --> 01:42:55,840
going to do is it's going to return and
2851
01:42:55,840 --> 01:42:59,440
we're going to say this dot assets minus
2852
01:42:59,440 --> 01:43:02,320
this dot liabilities this thing is kind
2853
01:43:02,320 --> 01:43:04,159
of weird but what this is referring to
2854
01:43:04,159 --> 01:43:06,400
is this specific person object that
2855
01:43:06,400 --> 01:43:07,920
you've created so it's literally like
2856
01:43:07,920 --> 01:43:10,679
imagine plugging in person.assets and
2857
01:43:10,679 --> 01:43:12,639
person.liabilities okay but you're going
2858
01:43:12,639 --> 01:43:14,800
to use this this whole thing here is
2859
01:43:14,800 --> 01:43:16,880
going to become 100 000. and this entire
2860
01:43:16,880 --> 01:43:18,960
thing is going to evaluate to 50 000 and
2861
01:43:18,960 --> 01:43:20,480
then this thing is going to evaluate to
2862
01:43:20,480 --> 01:43:22,480
50 000 like this and then once you
2863
01:43:22,480 --> 01:43:24,400
return this it's going to pop up in your
2864
01:43:24,400 --> 01:43:27,440
console so let's go back boom this
2865
01:43:27,440 --> 01:43:29,760
assets sold liabilities and net worth is
2866
01:43:29,760 --> 01:43:32,000
a method so even though we've defined as
2867
01:43:32,000 --> 01:43:33,840
a function it is a method and i'll show
2868
01:43:33,840 --> 01:43:36,000
you why because now in order to call it
2869
01:43:36,000 --> 01:43:38,239
if you do person.net worth watch what's
2870
01:43:38,239 --> 01:43:39,840
gonna happen it's not gonna work let's
2871
01:43:39,840 --> 01:43:42,320
run it and we just got
2872
01:43:42,320 --> 01:43:44,000
hi my name is kazi and the color of my
2873
01:43:44,000 --> 01:43:45,840
shirt is black and my net worth is
2874
01:43:45,840 --> 01:43:48,000
function return assets so just like just
2875
01:43:48,000 --> 01:43:50,560
like spit out this entire function
2876
01:43:50,560 --> 01:43:52,159
that's not what we want we want the
2877
01:43:52,159 --> 01:43:54,800
actual result so we need to call this
2878
01:43:54,800 --> 01:43:57,360
method and this is how you call it okay
2879
01:43:57,360 --> 01:43:59,119
boom you call it just like a function
2880
01:43:59,119 --> 01:44:00,639
but because it has a dot here it's
2881
01:44:00,639 --> 01:44:03,520
called a method so the person has a net
2882
01:44:03,520 --> 01:44:04,880
worth method
2883
01:44:04,880 --> 01:44:07,040
now let's run it and watch what happens
2884
01:44:07,040 --> 01:44:08,080
boom
2885
01:44:08,080 --> 01:44:09,679
right my name is causing the color of my
2886
01:44:09,679 --> 01:44:12,000
shirt is black and my net worth is 50
2887
01:44:12,000 --> 01:44:13,040
000
2888
01:44:13,040 --> 01:44:14,560
and now to make this code even more
2889
01:44:14,560 --> 01:44:17,920
dynamic assets and liabilities should be
2890
01:44:17,920 --> 01:44:20,239
passed by the user okay so that could be
2891
01:44:20,239 --> 01:44:22,159
like another thing that that might be
2892
01:44:22,159 --> 01:44:23,520
nice right now it's hard coded but
2893
01:44:23,520 --> 01:44:24,960
that's okay we can just kind of leave it
2894
01:44:24,960 --> 01:44:26,960
as is just to kind of show you that you
2895
01:44:26,960 --> 01:44:28,880
could do some more advanced stuff with
2896
01:44:28,880 --> 01:44:31,440
objects all right so let's run this okay
2897
01:44:31,440 --> 01:44:33,040
and if you change the name it'll also
2898
01:44:33,040 --> 01:44:34,880
work change the assets liabilities
2899
01:44:34,880 --> 01:44:36,320
you'll get different numbers all right
2900
01:44:36,320 --> 01:44:39,119
so if you got this far great job uh
2901
01:44:39,119 --> 01:44:41,040
we're gonna hit run on this to just make
2902
01:44:41,040 --> 01:44:43,600
sure we we're gucci but if you got this
2903
01:44:43,600 --> 01:44:44,480
far
2904
01:44:44,480 --> 01:44:46,560
you're almost an object
2905
01:44:46,560 --> 01:44:49,440
master so good job okay
2906
01:44:49,440 --> 01:44:50,880
you probably don't know like everything
2907
01:44:50,880 --> 01:44:52,560
there is to know about objects okay but
2908
01:44:52,560 --> 01:44:55,760
like you know more than 98 of the
2909
01:44:55,760 --> 01:44:57,920
population so you should be proud of
2910
01:44:57,920 --> 01:45:00,480
yourself give yourself a pat on the back
2911
01:45:00,480 --> 01:45:02,239
right about now go ahead and do that for
2912
01:45:02,239 --> 01:45:04,400
me and and you're beautiful and you
2913
01:45:04,400 --> 01:45:05,280
matter
2914
01:45:05,280 --> 01:45:07,119
and i'm happy that you're here so thank
2915
01:45:07,119 --> 01:45:09,520
you warms my heart all right with that
2916
01:45:09,520 --> 01:45:11,600
said let's continue all right now we're
2917
01:45:11,600 --> 01:45:13,679
going to be doing my favorite part
2918
01:45:13,679 --> 01:45:15,840
without this part you pretty much don't
2919
01:45:15,840 --> 01:45:17,760
have programming so this concept that
2920
01:45:17,760 --> 01:45:19,280
you're going to learn right now is going
2921
01:45:19,280 --> 01:45:21,199
to put everything together and it's
2922
01:45:21,199 --> 01:45:23,440
going to make everything do you do so
2923
01:45:23,440 --> 01:45:24,800
useful and it's not just going to be
2924
01:45:24,800 --> 01:45:26,560
like why can't i just do this in excel
2925
01:45:26,560 --> 01:45:29,040
why can't i just blah blah this is the
2926
01:45:29,040 --> 01:45:31,119
most important thing let's get into it
2927
01:45:31,119 --> 01:45:34,159
and this is called for loops loops
2928
01:45:34,159 --> 01:45:37,520
are the heart blood and soul of computer
2929
01:45:37,520 --> 01:45:39,440
programming and i'm just so excited that
2930
01:45:39,440 --> 01:45:40,880
you get to learn it right here with me
2931
01:45:40,880 --> 01:45:42,159
so if you're enjoying this so far smash
2932
01:45:42,159 --> 01:45:43,760
that like button so this video goes out
2933
01:45:43,760 --> 01:45:45,520
to more people helps more people become
2934
01:45:45,520 --> 01:45:47,920
developer and it's free so do it right
2935
01:45:47,920 --> 01:45:49,840
now and let's keep on rocking and
2936
01:45:49,840 --> 01:45:52,880
rolling so we're going to use loops okay
2937
01:45:52,880 --> 01:45:55,119
and let's go ahead and grab our array
2938
01:45:55,119 --> 01:45:56,880
that we made earlier with groceries okay
2939
01:45:56,880 --> 01:45:58,719
so i'm gonna just go ahead and copy it
2940
01:45:58,719 --> 01:46:01,520
so let's go boom grab this
2941
01:46:01,520 --> 01:46:04,159
const groceries okay i've defined it
2942
01:46:04,159 --> 01:46:05,440
above so what i'm gonna do is i'm just
2943
01:46:05,440 --> 01:46:07,600
gonna comment all of this out because if
2944
01:46:07,600 --> 01:46:09,520
i've defined a const above i can't
2945
01:46:09,520 --> 01:46:11,199
redefine it below so i'm gonna make sure
2946
01:46:11,199 --> 01:46:12,560
it's commented out and then i'm gonna
2947
01:46:12,560 --> 01:46:14,400
come down here and i'm gonna pay his
2948
01:46:14,400 --> 01:46:16,080
groceries okay and let's call it fruits
2949
01:46:16,080 --> 01:46:18,560
cause let's be let's be real that's what
2950
01:46:18,560 --> 01:46:21,040
it is right so now i have fruits well
2951
01:46:21,040 --> 01:46:23,040
what i wanna do is i wanna like print
2952
01:46:23,040 --> 01:46:25,199
out every single one of them so how do i
2953
01:46:25,199 --> 01:46:27,280
do that well here's a way you could do
2954
01:46:27,280 --> 01:46:29,280
it right console log we'll do fruits of
2955
01:46:29,280 --> 01:46:31,679
zero and then we'll just keep repeating
2956
01:46:31,679 --> 01:46:32,639
that
2957
01:46:32,639 --> 01:46:34,960
pattern right one two
2958
01:46:34,960 --> 01:46:36,560
three and then what will happen
2959
01:46:36,560 --> 01:46:38,400
essentially is this will get you this
2960
01:46:38,400 --> 01:46:41,199
one one will get you that one two will
2961
01:46:41,199 --> 01:46:42,880
get you that one
2962
01:46:42,880 --> 01:46:44,320
three will get you that one right
2963
01:46:44,320 --> 01:46:46,480
hopefully that makes sense so far so
2964
01:46:46,480 --> 01:46:48,320
let's go ahead and run this
2965
01:46:48,320 --> 01:46:50,080
and you get all four of these right
2966
01:46:50,080 --> 01:46:51,679
that's perfect but what if there were
2967
01:46:51,679 --> 01:46:53,760
like many many more like so if i just
2968
01:46:53,760 --> 01:46:56,960
copy this and i just keep let's say i
2969
01:46:56,960 --> 01:46:58,800
copy this and i just keep pasting it
2970
01:46:58,800 --> 01:47:01,360
right well that's a lot how are you
2971
01:47:01,360 --> 01:47:03,840
going to like print out all of those
2972
01:47:03,840 --> 01:47:05,119
it's going to be a lot of work are you
2973
01:47:05,119 --> 01:47:07,119
really going to do this right and then
2974
01:47:07,119 --> 01:47:08,480
what if they're more than that because
2975
01:47:08,480 --> 01:47:11,199
netflix has a what you know let's go to
2976
01:47:11,199 --> 01:47:13,520
netflix right now so here's an example
2977
01:47:13,520 --> 01:47:16,080
of an array this right here is an array
2978
01:47:16,080 --> 01:47:17,920
all right that's how you see these five
2979
01:47:17,920 --> 01:47:20,639
profiles one two three four five that's
2980
01:47:20,639 --> 01:47:23,280
an array when i go inside of this
2981
01:47:23,280 --> 01:47:24,960
everything that's showing up here is an
2982
01:47:24,960 --> 01:47:28,560
array so here's an array of new releases
2983
01:47:28,560 --> 01:47:30,080
here's the array of true crime
2984
01:47:30,080 --> 01:47:32,960
documentaries here's an array of
2985
01:47:32,960 --> 01:47:35,520
trending now these are just arrays all
2986
01:47:35,520 --> 01:47:38,239
right so if i have this many movies i
2987
01:47:38,239 --> 01:47:40,560
can't just like do them manually i need
2988
01:47:40,560 --> 01:47:42,560
something that can automatically find
2989
01:47:42,560 --> 01:47:43,920
out how many movies there are and then
2990
01:47:43,920 --> 01:47:47,520
like print them out okay or show them so
2991
01:47:47,520 --> 01:47:49,679
that's an example of you know why you
2992
01:47:49,679 --> 01:47:52,080
need to learn something called
2993
01:47:52,080 --> 01:47:54,320
looping okay or for loops so let's go
2994
01:47:54,320 --> 01:47:56,080
ahead and do that now all right so
2995
01:47:56,080 --> 01:47:57,280
here's how we're going to write our for
2996
01:47:57,280 --> 01:47:59,040
loop we're going to say 4 and we're
2997
01:47:59,040 --> 01:48:01,360
going to say let i equal 0 and then put
2998
01:48:01,360 --> 01:48:03,840
a semicolon and we're going to say while
2999
01:48:03,840 --> 01:48:05,679
i is less than and i know this is going
3000
01:48:05,679 --> 01:48:07,679
to look super weird but just trust me
3001
01:48:07,679 --> 01:48:09,280
hang in there with me okay fruit's not
3002
01:48:09,280 --> 01:48:11,360
length and we're going to do i plus plus
3003
01:48:11,360 --> 01:48:12,560
like everything here you're going to be
3004
01:48:12,560 --> 01:48:14,639
like what the hell am i looking at and
3005
01:48:14,639 --> 01:48:15,840
we're going to break it down in a second
3006
01:48:15,840 --> 01:48:17,520
but first let's just run it and see if
3007
01:48:17,520 --> 01:48:19,199
we can get it to do what we want it to
3008
01:48:19,199 --> 01:48:22,239
do and i'm going to say fruits and get
3009
01:48:22,239 --> 01:48:23,679
me the
3010
01:48:23,679 --> 01:48:26,560
get me the iath fruit okay let's run it
3011
01:48:26,560 --> 01:48:28,480
and let's actually console log this
3012
01:48:28,480 --> 01:48:30,800
console log and let's comment out all of
3013
01:48:30,800 --> 01:48:32,800
these four lines right here okay let's
3014
01:48:32,800 --> 01:48:36,159
run this and you see it it printed out
3015
01:48:36,159 --> 01:48:38,880
every single fruit that we had right all
3016
01:48:38,880 --> 01:48:41,199
of these fruits every single one of them
3017
01:48:41,199 --> 01:48:42,719
and how i can prove that too that i
3018
01:48:42,719 --> 01:48:44,080
print every single one out what we could
3019
01:48:44,080 --> 01:48:46,639
do is like put i there and then do this
3020
01:48:46,639 --> 01:48:48,239
and i'll actually even count it for you
3021
01:48:48,239 --> 01:48:51,679
so this is zeroth one first second third
3022
01:48:51,679 --> 01:48:53,440
fourth fifth sixth
3023
01:48:53,440 --> 01:48:55,199
all the way up to nineteenth okay so
3024
01:48:55,199 --> 01:48:56,320
there's like
3025
01:48:56,320 --> 01:48:59,920
20 total items in this array so this
3026
01:48:59,920 --> 01:49:02,320
loop is doing that this for loop right
3027
01:49:02,320 --> 01:49:04,480
here it's looping through automatically
3028
01:49:04,480 --> 01:49:06,400
auto magically figuring this out but
3029
01:49:06,400 --> 01:49:08,400
let's break it down what is happening
3030
01:49:08,400 --> 01:49:11,119
really so we're saying this loop we want
3031
01:49:11,119 --> 01:49:13,360
an index why do we want an index well
3032
01:49:13,360 --> 01:49:15,679
imagine if i instead of i imagine i put
3033
01:49:15,679 --> 01:49:18,480
a zero here well it's gonna run but it's
3034
01:49:18,480 --> 01:49:20,000
just gonna keep on getting the banana
3035
01:49:20,000 --> 01:49:21,440
every single time but that's not what we
3036
01:49:21,440 --> 01:49:24,239
want so what needs to happen is we're
3037
01:49:24,239 --> 01:49:26,080
essentially trying to mimic this process
3038
01:49:26,080 --> 01:49:27,840
we want fruits to be zero the first time
3039
01:49:27,840 --> 01:49:29,599
fruits to be one the next time fruits to
3040
01:49:29,599 --> 01:49:31,760
be two the third time fruits to be uh
3041
01:49:31,760 --> 01:49:33,520
this the fourth time
3042
01:49:33,520 --> 01:49:35,760
right so this is what what we're doing
3043
01:49:35,760 --> 01:49:38,719
here so the start i is set to zero and
3044
01:49:38,719 --> 01:49:40,560
all a loop does is it just repeats the
3045
01:49:40,560 --> 01:49:42,320
same line of code again and again and
3046
01:49:42,320 --> 01:49:44,960
again and again and again fruits.length
3047
01:49:44,960 --> 01:49:46,960
all i'm saying here is hey run the same
3048
01:49:46,960 --> 01:49:49,280
amount of times there are items in this
3049
01:49:49,280 --> 01:49:51,679
array so meaning in other words we want
3050
01:49:51,679 --> 01:49:54,800
you to run 20 times so while i is less
3051
01:49:54,800 --> 01:49:58,239
than 20. so run 20 times that's what
3052
01:49:58,239 --> 01:50:00,320
this means right here okay and as soon
3053
01:50:00,320 --> 01:50:03,119
as it runs 20 times and it tries to run
3054
01:50:03,119 --> 01:50:05,199
the 21st time it just stops and it
3055
01:50:05,199 --> 01:50:07,679
doesn't run again and i plus plus means
3056
01:50:07,679 --> 01:50:09,280
every single time you're done running
3057
01:50:09,280 --> 01:50:11,760
just increment i by one so first time it
3058
01:50:11,760 --> 01:50:14,719
runs i is zero okay so we put a zero
3059
01:50:14,719 --> 01:50:17,520
here and there's a zero here and you
3060
01:50:17,520 --> 01:50:20,000
will get your first item which is gonna
3061
01:50:20,000 --> 01:50:20,880
be
3062
01:50:20,880 --> 01:50:23,440
this item right here then it's going to
3063
01:50:23,440 --> 01:50:24,880
run again and here's what's going to
3064
01:50:24,880 --> 01:50:27,520
happen i is going to become 1 next time
3065
01:50:27,520 --> 01:50:29,040
the loop is going to run let's start
3066
01:50:29,040 --> 01:50:31,760
from the scratch right so it goes 0
3067
01:50:31,760 --> 01:50:34,960
less than 20 is that true or false i'm
3068
01:50:34,960 --> 01:50:36,000
asking you
3069
01:50:36,000 --> 01:50:39,520
that's true 0 is less than 20. so then
3070
01:50:39,520 --> 01:50:42,400
this line of this line of code here runs
3071
01:50:42,400 --> 01:50:44,800
and it prints this out pops it right
3072
01:50:44,800 --> 01:50:47,599
there perfect then once we're done
3073
01:50:47,599 --> 01:50:50,159
running it increments i by one so now i
3074
01:50:50,159 --> 01:50:53,840
is one is 1 less than 20 yes or no yes
3075
01:50:53,840 --> 01:50:57,440
it is okay 1 is less than 20. so then it
3076
01:50:57,440 --> 01:51:00,480
runs but next time because there is an i
3077
01:51:00,480 --> 01:51:03,679
here fruits becomes 1 because it's
3078
01:51:03,679 --> 01:51:06,960
dynamic like that so then it gets you
3079
01:51:06,960 --> 01:51:09,199
this guy right over here then it runs
3080
01:51:09,199 --> 01:51:11,520
again and it does the same thing too so
3081
01:51:11,520 --> 01:51:14,480
it goes to 2 less than 20 goes yes pops
3082
01:51:14,480 --> 01:51:16,560
in a 2 gets you the orange because
3083
01:51:16,560 --> 01:51:18,800
that's in position 2 right here then it
3084
01:51:18,800 --> 01:51:21,520
increments i by one i becomes three is
3085
01:51:21,520 --> 01:51:23,280
three less than twenty is that true or
3086
01:51:23,280 --> 01:51:26,159
false yes it's true and it prints out
3087
01:51:26,159 --> 01:51:28,800
fruit of three so then you get this pair
3088
01:51:28,800 --> 01:51:31,440
then it goes again and increments this
3089
01:51:31,440 --> 01:51:33,840
by one so this becomes what
3090
01:51:33,840 --> 01:51:35,280
4 good
3091
01:51:35,280 --> 01:51:37,199
and then it prints it out
3092
01:51:37,199 --> 01:51:38,239
all right
3093
01:51:38,239 --> 01:51:40,480
i'm going to ask you this time what is i
3094
01:51:40,480 --> 01:51:42,800
now if you guess 5 good job because
3095
01:51:42,800 --> 01:51:45,119
after running this we increment i by 1
3096
01:51:45,119 --> 01:51:48,560
so i is 5 now it's 5 less than 20
3097
01:51:48,560 --> 01:51:51,199
okay this is 5 and this is 20 and this
3098
01:51:51,199 --> 01:51:53,280
thing then becomes true and then once
3099
01:51:53,280 --> 01:51:55,599
this becomes true this part of the loop
3100
01:51:55,599 --> 01:51:58,080
runs all right so is this less than 20
3101
01:51:58,080 --> 01:52:00,800
yes it is then this becomes 5 and then
3102
01:52:00,800 --> 01:52:03,440
this prints this out so on and so forth
3103
01:52:03,440 --> 01:52:05,599
so this is why we just leave a i here
3104
01:52:05,599 --> 01:52:08,000
okay so that's how loops work they're
3105
01:52:08,000 --> 01:52:09,440
really awesome
3106
01:52:09,440 --> 01:52:12,000
real really nice and uh they make our
3107
01:52:12,000 --> 01:52:14,000
life easy now
3108
01:52:14,000 --> 01:52:16,239
now now now now here's something
3109
01:52:16,239 --> 01:52:18,000
exciting i'm going to show you
3110
01:52:18,000 --> 01:52:20,639
you pretty much never
3111
01:52:20,639 --> 01:52:21,679
ever
3112
01:52:21,679 --> 01:52:25,280
ever have to use this ugly looking loop
3113
01:52:25,280 --> 01:52:27,199
ever again this is really the only time
3114
01:52:27,199 --> 01:52:28,639
you're going to be confused thanks to
3115
01:52:28,639 --> 01:52:30,480
the new javascript
3116
01:52:30,480 --> 01:52:32,480
ways of doing it and i'll show you them
3117
01:52:32,480 --> 01:52:35,360
and after that pretty much never write
3118
01:52:35,360 --> 01:52:37,679
this ugly loop i never write it i don't
3119
01:52:37,679 --> 01:52:39,599
deal with these weird indexes and
3120
01:52:39,599 --> 01:52:41,280
honestly i even forgot how to write this
3121
01:52:41,280 --> 01:52:44,239
ugly looking loop should be eradicated
3122
01:52:44,239 --> 01:52:46,400
from most of what you're doing in your
3123
01:52:46,400 --> 01:52:48,639
day-to-day i'll show you a much easier
3124
01:52:48,639 --> 01:52:50,800
way of writing all right so now let's go
3125
01:52:50,800 --> 01:52:53,199
ahead and comment out this entire for
3126
01:52:53,199 --> 01:52:54,719
loop that we wrote and instead what
3127
01:52:54,719 --> 01:52:55,679
we're going to write is we're going to
3128
01:52:55,679 --> 01:52:57,760
say 4 and watch this we're gonna set
3129
01:52:57,760 --> 01:53:00,719
kant's fruit of fruits okay and we're
3130
01:53:00,719 --> 01:53:02,480
just gonna say console log fruit and
3131
01:53:02,480 --> 01:53:04,400
watch what happens i'm gonna
3132
01:53:04,400 --> 01:53:07,520
run this and it does the exact same
3133
01:53:07,520 --> 01:53:11,199
thing and you do not have to worry about
3134
01:53:11,199 --> 01:53:14,239
indices at all so this is so nice so
3135
01:53:14,239 --> 01:53:16,800
here's how this is working fruits this
3136
01:53:16,800 --> 01:53:19,760
variable here fruits is important this
3137
01:53:19,760 --> 01:53:21,520
is this right here this is the fruits
3138
01:53:21,520 --> 01:53:23,679
array this variable here right here
3139
01:53:23,679 --> 01:53:25,280
fruit i'm making it up on the fly i
3140
01:53:25,280 --> 01:53:28,080
could call it whatever i want i can call
3141
01:53:28,080 --> 01:53:30,239
it john but i just got to make sure that
3142
01:53:30,239 --> 01:53:32,560
i put john here and john here and boom
3143
01:53:32,560 --> 01:53:34,400
we run it the only point of this
3144
01:53:34,400 --> 01:53:37,520
variable right here is fruits in this
3145
01:53:37,520 --> 01:53:40,000
case will be the name of you know it
3146
01:53:40,000 --> 01:53:42,159
refers to this which is this entire
3147
01:53:42,159 --> 01:53:44,880
array right here this entire array right
3148
01:53:44,880 --> 01:53:46,880
here okay that's what fruit says but
3149
01:53:46,880 --> 01:53:49,599
fruit when you're looping the first time
3150
01:53:49,599 --> 01:53:52,320
it's this banana and when you're looping
3151
01:53:52,320 --> 01:53:54,400
the second time it's this apple and when
3152
01:53:54,400 --> 01:53:56,400
you loop again is this orange and when
3153
01:53:56,400 --> 01:53:58,239
you loop again it's this so you don't
3154
01:53:58,239 --> 01:54:00,639
have to even worry about indices this is
3155
01:54:00,639 --> 01:54:02,960
the fruit and then in the next loop this
3156
01:54:02,960 --> 01:54:05,119
is the fruit and then in the next loop
3157
01:54:05,119 --> 01:54:06,719
right this is the fruit this is the
3158
01:54:06,719 --> 01:54:08,560
first loop and the second loop and the
3159
01:54:08,560 --> 01:54:10,880
third loop the fourth loop and then you
3160
01:54:10,880 --> 01:54:13,760
just print that out so like i don't know
3161
01:54:13,760 --> 01:54:15,920
if you if you look at these two side by
3162
01:54:15,920 --> 01:54:17,760
side right this looks like an eyesore
3163
01:54:17,760 --> 01:54:20,560
and hurts your eyes and you will get eye
3164
01:54:20,560 --> 01:54:22,320
aids by looking at this and then you
3165
01:54:22,320 --> 01:54:23,840
have this one
3166
01:54:23,840 --> 01:54:25,119
just
3167
01:54:25,119 --> 01:54:26,880
this is beautiful this is pure gold i
3168
01:54:26,880 --> 01:54:28,880
mean this is how this has meant to be
3169
01:54:28,880 --> 01:54:30,239
right this is how it should things
3170
01:54:30,239 --> 01:54:31,920
should look and feel this is very
3171
01:54:31,920 --> 01:54:33,840
pythonic python you write loops like
3172
01:54:33,840 --> 01:54:36,239
this this is really really nice so we
3173
01:54:36,239 --> 01:54:38,719
like this one much more than this one
3174
01:54:38,719 --> 01:54:40,560
okay so stick with this one and then
3175
01:54:40,560 --> 01:54:42,080
i'll show you a couple of other things
3176
01:54:42,080 --> 01:54:43,679
you can do as well all right so now
3177
01:54:43,679 --> 01:54:45,599
let's do something else with arrays okay
3178
01:54:45,599 --> 01:54:48,159
so we're going to say const numbers and
3179
01:54:48,159 --> 01:54:49,760
we're going to create a array and it's
3180
01:54:49,760 --> 01:54:51,280
going to keep our number so one two
3181
01:54:51,280 --> 01:54:52,960
three four five six
3182
01:54:52,960 --> 01:54:53,840
well
3183
01:54:53,840 --> 01:54:57,119
what if we want to um so we'll do a
3184
01:54:57,119 --> 01:54:58,400
couple of things right i'll show you how
3185
01:54:58,400 --> 01:54:59,920
to sum up
3186
01:54:59,920 --> 01:55:02,639
all numbers in an array because that
3187
01:55:02,639 --> 01:55:04,320
will be something helpful for you and
3188
01:55:04,320 --> 01:55:06,320
important for you to learn but we want
3189
01:55:06,320 --> 01:55:09,280
to do now try this write both of these
3190
01:55:09,280 --> 01:55:11,599
loops for this numbers and print out
3191
01:55:11,599 --> 01:55:12,960
every single number how we're printing
3192
01:55:12,960 --> 01:55:14,639
out our fruits okay and comment out
3193
01:55:14,639 --> 01:55:16,239
everything related to the fruit okay so
3194
01:55:16,239 --> 01:55:18,560
hit run and you should see empty and
3195
01:55:18,560 --> 01:55:20,480
loop through so then all the numbers get
3196
01:55:20,480 --> 01:55:22,320
printed out from one through six try
3197
01:55:22,320 --> 01:55:25,040
that i'll give you five seconds five
3198
01:55:25,040 --> 01:55:28,239
four three two one
3199
01:55:28,239 --> 01:55:30,239
all right let's write it together so
3200
01:55:30,239 --> 01:55:34,719
we're gonna say 4 let i equal 0 okay i
3201
01:55:34,719 --> 01:55:36,080
is less than
3202
01:55:36,080 --> 01:55:38,239
numbers dot length all right you kind of
3203
01:55:38,239 --> 01:55:40,159
get used to writing this for loop once
3204
01:55:40,159 --> 01:55:41,760
you write it a couple of times but again
3205
01:55:41,760 --> 01:55:42,960
we're not even going to be using this
3206
01:55:42,960 --> 01:55:44,880
one that much anymore so get ready to
3207
01:55:44,880 --> 01:55:46,719
say goodbye to this one all right let's
3208
01:55:46,719 --> 01:55:48,480
run this and let's see if it works boom
3209
01:55:48,480 --> 01:55:50,400
one two three four five six so this one
3210
01:55:50,400 --> 01:55:52,480
worked exactly how we expected it to
3211
01:55:52,480 --> 01:55:55,760
fantastic okay let's go ahead and let's
3212
01:55:55,760 --> 01:55:58,320
comment this out let's comment this out
3213
01:55:58,320 --> 01:56:00,480
and now we're going to say we're going
3214
01:56:00,480 --> 01:56:01,599
to write the other loop so we're going
3215
01:56:01,599 --> 01:56:06,159
to say 4 const number of numbers and
3216
01:56:06,159 --> 01:56:07,840
we're simply going to just say console
3217
01:56:07,840 --> 01:56:10,719
log number and let's hit run and boom
3218
01:56:10,719 --> 01:56:12,639
one two three four five six don't make
3219
01:56:12,639 --> 01:56:15,440
the mistake of printing out numbers
3220
01:56:15,440 --> 01:56:16,960
because what you're doing here you're
3221
01:56:16,960 --> 01:56:18,639
just going to print out the array
3222
01:56:18,639 --> 01:56:20,560
five five times you don't want to do
3223
01:56:20,560 --> 01:56:22,960
that let's just say number boom there we
3224
01:56:22,960 --> 01:56:25,360
go so that should that printed out
3225
01:56:25,360 --> 01:56:27,040
everything hopefully you got this
3226
01:56:27,040 --> 01:56:28,560
correct and if you didn't it's okay
3227
01:56:28,560 --> 01:56:30,400
don't stress we'll have some additional
3228
01:56:30,400 --> 01:56:33,280
exercises here in our files for you to
3229
01:56:33,280 --> 01:56:34,000
do
3230
01:56:34,000 --> 01:56:35,840
with arrays okay because arrays are very
3231
01:56:35,840 --> 01:56:38,000
very important so we got this six now
3232
01:56:38,000 --> 01:56:40,080
what if we wanted to double every single
3233
01:56:40,080 --> 01:56:42,080
number right over okay so let's go ahead
3234
01:56:42,080 --> 01:56:44,639
and try that how can we do that how can
3235
01:56:44,639 --> 01:56:47,520
we print out 2 for this 4 for this 6 for
3236
01:56:47,520 --> 01:56:50,400
this 8 for this 10 for this and 12 for
3237
01:56:50,400 --> 01:56:53,040
this okay how do we do that so what we
3238
01:56:53,040 --> 01:56:54,719
want to do here is you actually you
3239
01:56:54,719 --> 01:56:56,400
could actually do it right here give
3240
01:56:56,400 --> 01:56:58,000
that a try see if you could figure that
3241
01:56:58,000 --> 01:57:00,560
out you have enough tools to be able to
3242
01:57:00,560 --> 01:57:02,719
figure this one out right now and i will
3243
01:57:02,719 --> 01:57:04,159
tell you this you don't even have to
3244
01:57:04,159 --> 01:57:06,639
write an additional line of code that's
3245
01:57:06,639 --> 01:57:08,960
the hint i'll give you you can fit that
3246
01:57:08,960 --> 01:57:11,920
in on line 221 and basically have the
3247
01:57:11,920 --> 01:57:14,000
answer to this problem so either take
3248
01:57:14,000 --> 01:57:15,840
five seconds to do it or pause the video
3249
01:57:15,840 --> 01:57:18,239
i'll give you five seconds now five four
3250
01:57:18,239 --> 01:57:20,639
three two one
3251
01:57:20,639 --> 01:57:23,199
all right so we're gonna go number times
3252
01:57:23,199 --> 01:57:24,719
2. that's it
3253
01:57:24,719 --> 01:57:28,400
let's run and now we get 2 4 6 8 10 12.
3254
01:57:28,400 --> 01:57:31,040
everything is doubled you guys that's
3255
01:57:31,040 --> 01:57:33,760
awesome but what if we wanted to do it
3256
01:57:33,760 --> 01:57:36,480
so that this numbers are right here is
3257
01:57:36,480 --> 01:57:39,199
doubled so or like we get a new array
3258
01:57:39,199 --> 01:57:41,440
that's like all of these numbers doubled
3259
01:57:41,440 --> 01:57:43,520
how could we do that double numbers how
3260
01:57:43,520 --> 01:57:45,360
could we double the numbers and
3261
01:57:45,360 --> 01:57:48,560
basically at the end get back an array
3262
01:57:48,560 --> 01:57:53,520
that says 2 4 6 8 10 12. how could we
3263
01:57:53,520 --> 01:57:56,639
put this in and get this out so let's
3264
01:57:56,639 --> 01:57:58,639
give that a try all right so here's what
3265
01:57:58,639 --> 01:58:00,400
we're gonna do we're gonna pretty much
3266
01:58:00,400 --> 01:58:02,719
have this same loop that we have here
3267
01:58:02,719 --> 01:58:04,400
but we're gonna need to create an array
3268
01:58:04,400 --> 01:58:06,320
that like stores that results right
3269
01:58:06,320 --> 01:58:08,880
we're going to say let result is equal
3270
01:58:08,880 --> 01:58:10,960
to and we'll just make it an array like
3271
01:58:10,960 --> 01:58:12,480
this all right so we're just going to
3272
01:58:12,480 --> 01:58:13,840
have this empty array it's going to be
3273
01:58:13,840 --> 01:58:16,080
empty at the start and the trick that
3274
01:58:16,080 --> 01:58:18,320
we're going to use is remember we did
3275
01:58:18,320 --> 01:58:21,360
push so again you have enough to figure
3276
01:58:21,360 --> 01:58:23,440
this out if you want to try but inside
3277
01:58:23,440 --> 01:58:26,000
of this for loop you're gonna push to
3278
01:58:26,000 --> 01:58:28,800
this results array double this amount so
3279
01:58:28,800 --> 01:58:30,719
see if you could figure that out and
3280
01:58:30,719 --> 01:58:32,880
then console.log result and get that by
3281
01:58:32,880 --> 01:58:34,719
yourself you have enough to figure out
3282
01:58:34,719 --> 01:58:36,239
it might be a little bit challenging
3283
01:58:36,239 --> 01:58:37,520
though right now i'll give you five
3284
01:58:37,520 --> 01:58:40,080
seconds you can pause the video five
3285
01:58:40,080 --> 01:58:43,280
four three two one and now we're gonna
3286
01:58:43,280 --> 01:58:45,679
solve it together so all we wanna do
3287
01:58:45,679 --> 01:58:49,280
here is i'm gonna do result dot push
3288
01:58:49,280 --> 01:58:51,920
number times two all right so i'm gonna
3289
01:58:51,920 --> 01:58:54,400
loop through and then push so then i'm
3290
01:58:54,400 --> 01:58:56,400
gonna have two then i'm gonna loop again
3291
01:58:56,400 --> 01:58:58,560
and number is going to be
3292
01:58:58,560 --> 01:59:01,599
two and we're gonna come down here
3293
01:59:01,599 --> 01:59:04,560
2 times 2 is going to become 4 right so
3294
01:59:04,560 --> 01:59:06,400
we're going to result that pushed off 4
3295
01:59:06,400 --> 01:59:08,719
that's going to add a 4 right here and
3296
01:59:08,719 --> 01:59:10,080
then we're just going to keep looping
3297
01:59:10,080 --> 01:59:11,599
through this array and just keep
3298
01:59:11,599 --> 01:59:13,920
doubling it every single time and then
3299
01:59:13,920 --> 01:59:16,719
pushing the double result in here in
3300
01:59:16,719 --> 01:59:18,880
that result array okay so let's remove
3301
01:59:18,880 --> 01:59:19,679
that
3302
01:59:19,679 --> 01:59:22,080
and then once that entire loop is done
3303
01:59:22,080 --> 01:59:24,239
so make sure to put the console log of
3304
01:59:24,239 --> 01:59:26,960
result outside of this for loop okay
3305
01:59:26,960 --> 01:59:28,159
because we don't want that repeating
3306
01:59:28,159 --> 01:59:30,239
again and again let's comment that line
3307
01:59:30,239 --> 01:59:32,320
out or we can actually just even delete
3308
01:59:32,320 --> 01:59:33,920
it here we don't need that anymore let's
3309
01:59:33,920 --> 01:59:36,560
try running and watch what happens boom
3310
01:59:36,560 --> 01:59:41,040
we have an array with 2 4 6 8 10 12. so
3311
01:59:41,040 --> 01:59:44,400
that array doubles all of the numbers
3312
01:59:44,400 --> 01:59:47,280
so this is an example of how you could
3313
01:59:47,280 --> 01:59:49,360
do something like this all right now
3314
01:59:49,360 --> 01:59:51,280
what we would want to do is turn it into
3315
01:59:51,280 --> 01:59:52,960
a function okay so we're going to
3316
01:59:52,960 --> 01:59:54,320
basically go
3317
01:59:54,320 --> 01:59:55,440
const
3318
01:59:55,440 --> 01:59:56,719
double okay we're going to call this
3319
01:59:56,719 --> 01:59:59,760
function double and uh this function is
3320
01:59:59,760 --> 02:00:01,360
gonna you can you could call it you
3321
02:00:01,360 --> 02:00:02,880
could either call it function or you
3322
02:00:02,880 --> 02:00:05,440
could use this arrow syntax to highlight
3323
02:00:05,440 --> 02:00:06,880
that this is a function right it's the
3324
02:00:06,880 --> 02:00:09,679
es6 function and what we're gonna do is
3325
02:00:09,679 --> 02:00:11,679
what this function does is takes in
3326
02:00:11,679 --> 02:00:14,320
numbers and we'll have the same loop so
3327
02:00:14,320 --> 02:00:17,360
we'll say const number of numbers
3328
02:00:17,360 --> 02:00:19,040
grab this result right here we're going
3329
02:00:19,040 --> 02:00:20,800
to paste that in so it's going to create
3330
02:00:20,800 --> 02:00:22,880
a temporary recall result it's going to
3331
02:00:22,880 --> 02:00:24,400
loop through the numbers that you gave
3332
02:00:24,400 --> 02:00:26,639
it it's going to keep pushing double of
3333
02:00:26,639 --> 02:00:29,280
that result into result array and then
3334
02:00:29,280 --> 02:00:30,880
at the end we're just going to return
3335
02:00:30,880 --> 02:00:32,960
result just like that all right and we
3336
02:00:32,960 --> 02:00:34,880
can get rid of all of this right here
3337
02:00:34,880 --> 02:00:36,880
and let's try running double so let's do
3338
02:00:36,880 --> 02:00:39,440
console.log we'll say double and let's
3339
02:00:39,440 --> 02:00:42,080
give an array to double so one two
3340
02:00:42,080 --> 02:00:45,040
three four five six and let's run this
3341
02:00:45,040 --> 02:00:47,440
and let's see what it outputs two four
3342
02:00:47,440 --> 02:00:51,440
six eight ten twelve that is perfect you
3343
02:00:51,440 --> 02:00:54,159
guys that's exactly right that's exactly
3344
02:00:54,159 --> 02:00:56,000
what we were looking for so that is
3345
02:00:56,000 --> 02:00:59,280
beautiful nice job if you got this far
3346
02:00:59,280 --> 02:01:00,960
question for you what if we wanted to
3347
02:01:00,960 --> 02:01:03,360
turn this function into the square
3348
02:01:03,360 --> 02:01:05,360
function meaning we square all these
3349
02:01:05,360 --> 02:01:07,280
numbers how could we do that so think
3350
02:01:07,280 --> 02:01:09,880
about that squaring means using
3351
02:01:09,880 --> 02:01:12,239
exponentiation instead of multiplication
3352
02:01:12,239 --> 02:01:14,080
okay so how could we turn this function
3353
02:01:14,080 --> 02:01:16,880
into something that squares numbers so
3354
02:01:16,880 --> 02:01:19,360
meaning 1 squared would be 1 2 squared
3355
02:01:19,360 --> 02:01:22,080
would be 4 3 squared would be 9 4
3356
02:01:22,080 --> 02:01:24,480
squared would be 16 5 square would be 25
3357
02:01:24,480 --> 02:01:26,480
6 squared would be 36 right so how do we
3358
02:01:26,480 --> 02:01:28,560
get that array back well it's simple
3359
02:01:28,560 --> 02:01:31,760
just put another asterisk right here hit
3360
02:01:31,760 --> 02:01:34,560
run and there you go it's all
3361
02:01:34,560 --> 02:01:37,679
perfectly squared so let's go back boom
3362
02:01:37,679 --> 02:01:39,440
i'm gonna just turn back into a double
3363
02:01:39,440 --> 02:01:41,599
all right so that's how easy that is now
3364
02:01:41,599 --> 02:01:43,199
imagine if the size of this array
3365
02:01:43,199 --> 02:01:48,320
changes 7 8 9 20 40 and watch right when
3366
02:01:48,320 --> 02:01:50,159
i run it boom it just works instantly
3367
02:01:50,159 --> 02:01:52,159
right this is the crazy part about like
3368
02:01:52,159 --> 02:01:53,920
when you write good code and i'm just
3369
02:01:53,920 --> 02:01:55,920
gonna like fill it up with crazy numbers
3370
02:01:55,920 --> 02:01:57,520
here right i'm gonna make this really
3371
02:01:57,520 --> 02:01:59,760
really big you're getting a big data
3372
02:01:59,760 --> 02:02:01,920
file in excel or something and watch it
3373
02:02:01,920 --> 02:02:04,960
just it just works on the whole thing
3374
02:02:04,960 --> 02:02:07,840
insane right and it says 177 more items
3375
02:02:07,840 --> 02:02:10,159
this is the beautiful thing about code
3376
02:02:10,159 --> 02:02:12,159
so when again when you go look at
3377
02:02:12,159 --> 02:02:13,920
netflix right so let's go let's go to
3378
02:02:13,920 --> 02:02:16,000
netflix again and let's take a look at
3379
02:02:16,000 --> 02:02:18,000
this this is an array and what we're
3380
02:02:18,000 --> 02:02:19,920
doing to that array every single time so
3381
02:02:19,920 --> 02:02:22,000
like we have some data in that array and
3382
02:02:22,000 --> 02:02:24,719
we style that data every single time we
3383
02:02:24,719 --> 02:02:27,599
loop through so we we write a for loop
3384
02:02:27,599 --> 02:02:29,599
and we loop through and we say take this
3385
02:02:29,599 --> 02:02:33,360
data the billion dollar code text the
3386
02:02:33,360 --> 02:02:35,360
image url that's how you're seeing the
3387
02:02:35,360 --> 02:02:37,920
image and turn it into this card and
3388
02:02:37,920 --> 02:02:39,920
then do that again with the next movie
3389
02:02:39,920 --> 02:02:41,840
so almost imagine that there's a loop
3390
02:02:41,840 --> 02:02:46,000
for right or const movie of movies and
3391
02:02:46,000 --> 02:02:48,239
what you do here and we close the
3392
02:02:48,239 --> 02:02:52,080
squiggly right there and we say you know
3393
02:02:52,080 --> 02:02:53,280
return
3394
02:02:53,280 --> 02:02:55,520
movie and let's call this function card
3395
02:02:55,520 --> 02:02:56,960
and let's just say this function card
3396
02:02:56,960 --> 02:02:59,040
turns it into this nice
3397
02:02:59,040 --> 02:03:00,719
neat card okay we don't let's say we
3398
02:03:00,719 --> 02:03:02,960
don't know the details of this function
3399
02:03:02,960 --> 02:03:05,440
but what this card function does is if
3400
02:03:05,440 --> 02:03:07,760
you give it a data of a movie like the
3401
02:03:07,760 --> 02:03:09,280
name of the movie and the image of the
3402
02:03:09,280 --> 02:03:11,440
movie it turns it into this nice little
3403
02:03:11,440 --> 02:03:13,520
card over here then you loop again and
3404
02:03:13,520 --> 02:03:15,280
then you do it again you loop again you
3405
02:03:15,280 --> 02:03:16,800
do it again you loop again you do it
3406
02:03:16,800 --> 02:03:18,719
again you loop again you do it again i
3407
02:03:18,719 --> 02:03:20,159
mean god damn i should be called
3408
02:03:20,159 --> 02:03:21,920
freaking senior loopy because that
3409
02:03:21,920 --> 02:03:23,679
there's a ton of loops there that's the
3410
02:03:23,679 --> 02:03:26,239
crazy part you guys this is real world
3411
02:03:26,239 --> 02:03:28,880
stuff okay it's not just like me telling
3412
02:03:28,880 --> 02:03:31,199
you random stuff it has a very real
3413
02:03:31,199 --> 02:03:32,800
application even though we're doing like
3414
02:03:32,800 --> 02:03:34,719
beginner exercises right now just so you
3415
02:03:34,719 --> 02:03:36,239
could get better at it because if you
3416
02:03:36,239 --> 02:03:37,920
don't know how arrays work then if i
3417
02:03:37,920 --> 02:03:39,440
give you something more complicated with
3418
02:03:39,440 --> 02:03:41,920
like cards and images in html and css
3419
02:03:41,920 --> 02:03:43,679
you're going to be so confused so you
3420
02:03:43,679 --> 02:03:45,840
have to learn these by themselves like a
3421
02:03:45,840 --> 02:03:47,760
martial art you got to learn the rear
3422
02:03:47,760 --> 02:03:49,599
naked choke by itself and you got to
3423
02:03:49,599 --> 02:03:51,360
learn the kimura by itself and then you
3424
02:03:51,360 --> 02:03:53,840
got to learn the positions and then you
3425
02:03:53,840 --> 02:03:55,360
learn these esoteric positions in jiu
3426
02:03:55,360 --> 02:03:56,560
jitsu and you're like what the heck is
3427
02:03:56,560 --> 02:03:58,560
the point of this and then one day you
3428
02:03:58,560 --> 02:04:00,960
just it to hit that triangle you like
3429
02:04:00,960 --> 02:04:03,040
rotate your hip you angle your leg the
3430
02:04:03,040 --> 02:04:04,800
right way you grab the neck the right
3431
02:04:04,800 --> 02:04:07,520
way you twist the right way you pop your
3432
02:04:07,520 --> 02:04:09,599
right leg and you pop the left leg and
3433
02:04:09,599 --> 02:04:11,760
you get that triangle just boom
3434
02:04:11,760 --> 02:04:13,679
everything and it all happens at once
3435
02:04:13,679 --> 02:04:15,199
and it's so smooth and it's beautiful
3436
02:04:15,199 --> 02:04:16,719
but when you're learning it you learn it
3437
02:04:16,719 --> 02:04:18,400
like step by step and same thing with
3438
02:04:18,400 --> 02:04:20,880
chess or anything else in life soccer so
3439
02:04:20,880 --> 02:04:22,239
we're just learning the components and
3440
02:04:22,239 --> 02:04:24,800
the pieces by themselves and then later
3441
02:04:24,800 --> 02:04:26,560
you'll be able to put them together with
3442
02:04:26,560 --> 02:04:28,239
more impressive stuff okay when we get
3443
02:04:28,239 --> 02:04:30,480
to the project portion all right now
3444
02:04:30,480 --> 02:04:32,239
let's keep having more fun with the
3445
02:04:32,239 --> 02:04:34,079
arrays because the more practice you get
3446
02:04:34,079 --> 02:04:36,880
with arrays and loops the better of a
3447
02:04:36,880 --> 02:04:38,000
programmer you're going to become
3448
02:04:38,000 --> 02:04:40,639
because honestly that's like 90
3449
02:04:40,639 --> 02:04:42,079
of what you're doing in programming
3450
02:04:42,079 --> 02:04:45,040
you're usually getting max a lot of data
3451
02:04:45,040 --> 02:04:46,320
and you're looping through it and you're
3452
02:04:46,320 --> 02:04:48,239
doing something to it okay that's like
3453
02:04:48,239 --> 02:04:51,199
super common and so let's just get a
3454
02:04:51,199 --> 02:04:53,360
bunch of different practices in all
3455
02:04:53,360 --> 02:04:54,800
right so
3456
02:04:54,800 --> 02:04:57,119
we're going to go ahead and make another
3457
02:04:57,119 --> 02:04:58,960
function and this function we're going
3458
02:04:58,960 --> 02:05:00,639
to make is going to be called letter
3459
02:05:00,639 --> 02:05:03,199
counter okay so let's go letter
3460
02:05:03,199 --> 02:05:05,520
counter you can also call it counter
3461
02:05:05,520 --> 02:05:07,679
whatever you want and
3462
02:05:07,679 --> 02:05:09,920
we're going to use it making an error
3463
02:05:09,920 --> 02:05:10,880
function
3464
02:05:10,880 --> 02:05:12,800
all right and basically what this
3465
02:05:12,800 --> 02:05:14,320
function is going to do
3466
02:05:14,320 --> 02:05:16,079
is it's going to take a look and see how
3467
02:05:16,079 --> 02:05:17,520
many
3468
02:05:17,520 --> 02:05:20,639
letters are in a given phrase okay so if
3469
02:05:20,639 --> 02:05:22,320
i gave it a phrase
3470
02:05:22,320 --> 02:05:25,280
let's say khan's phrase is equal to uh
3471
02:05:25,280 --> 02:05:26,960
hey can you
3472
02:05:26,960 --> 02:05:29,679
go to grocery
3473
02:05:29,679 --> 02:05:31,679
store
3474
02:05:31,679 --> 02:05:32,960
okay whatever
3475
02:05:32,960 --> 02:05:34,880
all right this will go through this
3476
02:05:34,880 --> 02:05:37,040
whole thing and it will basically just
3477
02:05:37,040 --> 02:05:39,280
just count how many letters there are
3478
02:05:39,280 --> 02:05:42,960
okay let's call it how many letters
3479
02:05:42,960 --> 02:05:44,480
and so you're just gonna go through and
3480
02:05:44,480 --> 02:05:46,000
this is gonna be a one there's gonna be
3481
02:05:46,000 --> 02:05:49,440
a two this is gonna be a three a four a
3482
02:05:49,440 --> 02:05:52,960
five for the empty space six seven eight
3483
02:05:52,960 --> 02:05:54,880
nine so you can do this you can count an
3484
02:05:54,880 --> 02:05:56,480
empty space as a letter that's totally
3485
02:05:56,480 --> 02:05:58,079
fine so think about how you're gonna
3486
02:05:58,079 --> 02:05:59,920
write a function like this right think
3487
02:05:59,920 --> 02:06:01,920
about what are you gonna need to do well
3488
02:06:01,920 --> 02:06:03,599
you're gonna need to loop through this
3489
02:06:03,599 --> 02:06:05,520
and the cool part is you can actually
3490
02:06:05,520 --> 02:06:07,840
loop through these types of phrases you
3491
02:06:07,840 --> 02:06:09,280
can loop through strings as well first
3492
02:06:09,280 --> 02:06:12,159
let's comment out um anything we kind of
3493
02:06:12,159 --> 02:06:14,960
have up here okay perfect and now in
3494
02:06:14,960 --> 02:06:16,719
here i'm going to go ahead and i'm going
3495
02:06:16,719 --> 02:06:19,440
to say let's let's write a loop okay i'm
3496
02:06:19,440 --> 02:06:21,199
going to say 4
3497
02:06:21,199 --> 02:06:23,520
letter in phrase and let's see if this
3498
02:06:23,520 --> 02:06:25,440
works and let's do console log and we'll
3499
02:06:25,440 --> 02:06:27,840
say letter okay and let's hit run and
3500
02:06:27,840 --> 02:06:30,000
let's see what happens okay it's running
3501
02:06:30,000 --> 02:06:32,079
okay so what we need to do in order to
3502
02:06:32,079 --> 02:06:33,760
make sure that this actually runs is we
3503
02:06:33,760 --> 02:06:35,520
need to call this function that we just
3504
02:06:35,520 --> 02:06:37,280
made right so let's go right here and
3505
02:06:37,280 --> 02:06:39,199
i'm going to say console.log and we're
3506
02:06:39,199 --> 02:06:40,880
going to say how
3507
02:06:40,880 --> 02:06:42,000
many
3508
02:06:42,000 --> 02:06:43,760
letters okay and let's just call that
3509
02:06:43,760 --> 02:06:45,520
function
3510
02:06:45,520 --> 02:06:47,920
and there we go okay one two three four
3511
02:06:47,920 --> 02:06:48,800
five
3512
02:06:48,800 --> 02:06:52,719
and then it ends with um undefined okay
3513
02:06:52,719 --> 02:06:54,880
all good so this keeps looping through
3514
02:06:54,880 --> 02:06:56,719
and then it just prints out the letter
3515
02:06:56,719 --> 02:06:58,239
that's what we're looking for right and
3516
02:06:58,239 --> 02:06:59,599
we actually don't even need to call
3517
02:06:59,599 --> 02:07:01,599
console.log here because we're printing
3518
02:07:01,599 --> 02:07:02,960
out from the function so let's try
3519
02:07:02,960 --> 02:07:05,119
running this and now you see there's no
3520
02:07:05,119 --> 02:07:08,239
undefined right and we we go from 0 to
3521
02:07:08,239 --> 02:07:12,239
40. but it needs to start from a one and
3522
02:07:12,239 --> 02:07:14,320
if you think you're getting this or you
3523
02:07:14,320 --> 02:07:16,400
know how to kind of solve this or if you
3524
02:07:16,400 --> 02:07:18,800
even want to try i highly recommend you
3525
02:07:18,800 --> 02:07:20,320
just pause my video right here and
3526
02:07:20,320 --> 02:07:22,560
actually give this a try okay go ahead
3527
02:07:22,560 --> 02:07:24,400
and try to like do this on your own and
3528
02:07:24,400 --> 02:07:26,079
then you can come back to this but i
3529
02:07:26,079 --> 02:07:28,480
want you to get a ton of practice with
3530
02:07:28,480 --> 02:07:30,079
this stuff because that's how you're
3531
02:07:30,079 --> 02:07:32,159
going to get better not just by watching
3532
02:07:32,159 --> 02:07:34,159
me but by doing it with me that's how
3533
02:07:34,159 --> 02:07:36,800
you're going to get better
3534
02:07:37,040 --> 02:07:40,159
so if we want to count the the letters
3535
02:07:40,159 --> 02:07:42,560
in here right or this this how many are
3536
02:07:42,560 --> 02:07:44,480
in there we can't start counting from
3537
02:07:44,480 --> 02:07:46,880
zero okay
3538
02:07:46,880 --> 02:07:48,480
so what i'm going to do
3539
02:07:48,480 --> 02:07:50,960
is we can just make it where if i'm
3540
02:07:50,960 --> 02:07:51,840
getting
3541
02:07:51,840 --> 02:07:52,880
uh
3542
02:07:52,880 --> 02:07:55,119
a letter here right oh and if you were a
3543
02:07:55,119 --> 02:07:57,599
little bit confused like wait how are we
3544
02:07:57,599 --> 02:07:59,360
when we run this
3545
02:07:59,360 --> 02:08:01,599
how are we getting the numbers
3546
02:08:01,599 --> 02:08:02,880
with this four
3547
02:08:02,880 --> 02:08:05,280
loop right so you can also say four cons
3548
02:08:05,280 --> 02:08:07,760
letter and if you change this to of
3549
02:08:07,760 --> 02:08:09,679
because remember i taught you like i
3550
02:08:09,679 --> 02:08:13,360
taught you four of loops earlier
3551
02:08:13,360 --> 02:08:15,599
so if you do letter of phrase you're
3552
02:08:15,599 --> 02:08:16,880
actually gonna and you run this you're
3553
02:08:16,880 --> 02:08:20,239
gonna get letters the actual letters
3554
02:08:20,239 --> 02:08:23,360
all right if you run um
3555
02:08:23,360 --> 02:08:25,679
letter in phrase it's gonna get you the
3556
02:08:25,679 --> 02:08:28,320
index at each one of those
3557
02:08:28,320 --> 02:08:31,040
right so if you want to get 41 well what
3558
02:08:31,040 --> 02:08:33,599
we want to do is
3559
02:08:33,599 --> 02:08:37,320
uh just do
3560
02:08:37,520 --> 02:08:40,079
letter plus one
3561
02:08:40,079 --> 02:08:42,320
okay so this will just increase it by
3562
02:08:42,320 --> 02:08:44,159
one whatever it is
3563
02:08:44,159 --> 02:08:46,000
and if
3564
02:08:46,000 --> 02:08:47,840
and for whatever reason this letter
3565
02:08:47,840 --> 02:08:49,760
thinks it's a string so it was doing
3566
02:08:49,760 --> 02:08:52,960
that weird thing where it's attaching
3567
02:08:52,960 --> 02:08:53,840
you know
3568
02:08:53,840 --> 02:08:57,199
zero plus one is zero one and one
3569
02:08:57,199 --> 02:08:59,119
plus one is one one it's attaching
3570
02:08:59,119 --> 02:09:01,119
itself so it thinks letter is a string
3571
02:09:01,119 --> 02:09:04,000
so just type cast it into a number so
3572
02:09:04,000 --> 02:09:05,760
force it to be a number
3573
02:09:05,760 --> 02:09:07,280
and then
3574
02:09:07,280 --> 02:09:09,599
you get this okay so this is like
3575
02:09:09,599 --> 02:09:11,360
one very silly way of doing this and you
3576
02:09:11,360 --> 02:09:13,679
get oh i get 41 at the end so that's it
3577
02:09:13,679 --> 02:09:15,599
that's how i'm getting my
3578
02:09:15,599 --> 02:09:17,360
this is how i'm counting my letters but
3579
02:09:17,360 --> 02:09:18,560
there are better ways i'm going to show
3580
02:09:18,560 --> 02:09:20,000
you in just a second
3581
02:09:20,000 --> 02:09:21,520
all right so now let's talk about better
3582
02:09:21,520 --> 02:09:23,599
ways of doing this yeah so what we want
3583
02:09:23,599 --> 02:09:26,400
to do is
3584
02:09:26,400 --> 02:09:28,800
we want to have we want to set a
3585
02:09:28,800 --> 02:09:31,840
variable here that let's say is so we're
3586
02:09:31,840 --> 02:09:34,159
going to say let
3587
02:09:34,159 --> 02:09:38,400
uh result equals zero okay
3588
02:09:38,400 --> 02:09:40,800
let's go here and we're gonna go we're
3589
02:09:40,800 --> 02:09:43,679
basically gonna say
3590
02:09:43,840 --> 02:09:46,560
result is equal to
3591
02:09:46,560 --> 02:09:48,159
number and we're going to call index and
3592
02:09:48,159 --> 02:09:50,159
we're going to say plus 1. because all
3593
02:09:50,159 --> 02:09:51,440
we're doing here
3594
02:09:51,440 --> 02:09:53,920
is the loop already allows us to get up
3595
02:09:53,920 --> 02:09:55,920
up to 41 right so we know that this is
3596
02:09:55,920 --> 02:09:58,560
the correct answer all we want to do
3597
02:09:58,560 --> 02:10:01,920
is store that correct 41 in result
3598
02:10:01,920 --> 02:10:04,159
and then at the end of this loop
3599
02:10:04,159 --> 02:10:06,639
we want to just return that result at
3600
02:10:06,639 --> 02:10:06,960
the end
3601
02:10:06,960 --> 02:10:08,320
[Music]
3602
02:10:08,320 --> 02:10:10,560
that's all we're looking to do
3603
02:10:10,560 --> 02:10:12,159
okay so
3604
02:10:12,159 --> 02:10:14,320
because okay so i'll give you an example
3605
02:10:14,320 --> 02:10:16,400
okay let's just do
3606
02:10:16,400 --> 02:10:18,480
result is index
3607
02:10:18,480 --> 02:10:21,920
and here let's just return result
3608
02:10:21,920 --> 02:10:23,920
let's remove this line
3609
02:10:23,920 --> 02:10:25,679
well we can keep that line so you can
3610
02:10:25,679 --> 02:10:28,400
just see how the loop looks
3611
02:10:28,400 --> 02:10:30,880
and we're gonna run this function but
3612
02:10:30,880 --> 02:10:32,960
wrap it in console.log all right
3613
02:10:32,960 --> 02:10:35,599
actually we can do a trick here to make
3614
02:10:35,599 --> 02:10:37,760
this look really nice so here we have
3615
02:10:37,760 --> 02:10:40,239
return result right in order to make it
3616
02:10:40,239 --> 02:10:41,920
we want to make it look like this result
3617
02:10:41,920 --> 02:10:44,400
is 40. so there's a nice trick we can do
3618
02:10:44,400 --> 02:10:45,679
in our return
3619
02:10:45,679 --> 02:10:48,480
and we can basically say we can return
3620
02:10:48,480 --> 02:10:51,440
it as an object so when you print it out
3621
02:10:51,440 --> 02:10:53,280
the object is going to look really nice
3622
02:10:53,280 --> 02:10:55,360
like this
3623
02:10:55,360 --> 02:10:58,560
right um and then there's a even nicer
3624
02:10:58,560 --> 02:11:02,239
shortcut where if you just add in
3625
02:11:02,239 --> 02:11:04,880
one thing and wrap it up in an object
3626
02:11:04,880 --> 02:11:06,800
it's going to automatically set this as
3627
02:11:06,800 --> 02:11:08,800
the key
3628
02:11:08,800 --> 02:11:09,840
and
3629
02:11:09,840 --> 02:11:12,159
use this as a variable for value so it's
3630
02:11:12,159 --> 02:11:14,880
going to set the word result as the key
3631
02:11:14,880 --> 02:11:18,480
and use the value 44 from this
3632
02:11:18,480 --> 02:11:21,360
so it's a super cool little hack
3633
02:11:21,360 --> 02:11:23,440
and you can use this pretty often so
3634
02:11:23,440 --> 02:11:24,639
let's run this
3635
02:11:24,639 --> 02:11:27,040
and boom you can see that result in this
3636
02:11:27,040 --> 02:11:29,119
case is 40. so now you can see that
3637
02:11:29,119 --> 02:11:31,280
result is returning
3638
02:11:31,280 --> 02:11:34,320
pretty much the last value in this
3639
02:11:34,320 --> 02:11:36,960
entire thing that we get right when we
3640
02:11:36,960 --> 02:11:39,760
print out our uh indices
3641
02:11:39,760 --> 02:11:42,159
so these are this is you know this is
3642
02:11:42,159 --> 02:11:42,880
like
3643
02:11:42,880 --> 02:11:45,360
basically saying this is zero this is
3644
02:11:45,360 --> 02:11:48,079
one this is two but we just wanna start
3645
02:11:48,079 --> 02:11:50,719
it off with the one so
3646
02:11:50,719 --> 02:11:53,760
um let's go ahead and all we need to do
3647
02:11:53,760 --> 02:11:56,239
here to index is
3648
02:11:56,239 --> 02:11:58,560
we just need to add a 1 to it and also
3649
02:11:58,560 --> 02:12:01,040
remember we need to wrap the index in a
3650
02:12:01,040 --> 02:12:03,840
string just like this oh sorry not in a
3651
02:12:03,840 --> 02:12:06,480
string i'm sorry wrap index in a number
3652
02:12:06,480 --> 02:12:08,800
because it is a string okay so we need
3653
02:12:08,800 --> 02:12:10,400
to typecast it
3654
02:12:10,400 --> 02:12:12,960
let's run this and boom you get result
3655
02:12:12,960 --> 02:12:14,560
is 41.
3656
02:12:14,560 --> 02:12:16,800
right so
3657
02:12:16,800 --> 02:12:18,239
now
3658
02:12:18,239 --> 02:12:20,639
phrase we can change the phrase right
3659
02:12:20,639 --> 02:12:22,239
and again
3660
02:12:22,239 --> 02:12:23,679
how many letters is a function that
3661
02:12:23,679 --> 02:12:26,159
takes in the phrase so let's make this
3662
02:12:26,159 --> 02:12:29,520
let's extract this argument out now
3663
02:12:29,520 --> 02:12:33,360
and instead we'll put phrase over here
3664
02:12:33,360 --> 02:12:36,239
and we'll pass it in the phrase okay so
3665
02:12:36,239 --> 02:12:38,639
the function itself shouldn't have to
3666
02:12:38,639 --> 02:12:40,560
worry about the phrase it should just
3667
02:12:40,560 --> 02:12:42,480
wait for what the user is going to give
3668
02:12:42,480 --> 02:12:44,719
it as a phrase and it says result is 41.
3669
02:12:44,719 --> 02:12:47,360
now imagine i change this and i go
3670
02:12:47,360 --> 02:12:49,119
prompt the user
3671
02:12:49,119 --> 02:12:50,000
for
3672
02:12:50,000 --> 02:12:50,960
you know
3673
02:12:50,960 --> 02:12:53,119
write your phrase
3674
02:12:53,119 --> 02:12:55,840
okay and now let's run this and now this
3675
02:12:55,840 --> 02:12:58,320
pop-up happened at the top so i'm going
3676
02:12:58,320 --> 02:13:00,239
to write my phrase
3677
02:13:00,239 --> 02:13:03,760
i am wearing a nice green
3678
02:13:03,760 --> 02:13:06,159
shirt
3679
02:13:06,480 --> 02:13:09,119
right so when we do that
3680
02:13:09,119 --> 02:13:12,159
uh let's go ahead and run this
3681
02:13:12,159 --> 02:13:15,040
and i hit okay
3682
02:13:15,199 --> 02:13:18,400
and let's see what happens here
3683
02:13:18,400 --> 02:13:19,760
let's hit okay
3684
02:13:19,760 --> 02:13:21,760
and right when i hit okay boom we got
3685
02:13:21,760 --> 02:13:23,840
31.
3686
02:13:23,840 --> 02:13:25,119
all right so that
3687
02:13:25,119 --> 02:13:28,960
that phrase has 31 characters
3688
02:13:28,960 --> 02:13:30,880
now here's something sad i'm gonna tell
3689
02:13:30,880 --> 02:13:31,599
you
3690
02:13:31,599 --> 02:13:33,760
you can actually replace there is a
3691
02:13:33,760 --> 02:13:35,440
method for strings
3692
02:13:35,440 --> 02:13:37,440
that allows you to calculate the total
3693
02:13:37,440 --> 02:13:39,679
of uh you know see the length of a
3694
02:13:39,679 --> 02:13:40,719
string
3695
02:13:40,719 --> 02:13:42,560
and that is just
3696
02:13:42,560 --> 02:13:44,480
dot length okay
3697
02:13:44,480 --> 02:13:47,199
so for example if i well we don't open
3698
02:13:47,199 --> 02:13:49,119
up a shell let's just stay here so say i
3699
02:13:49,119 --> 02:13:50,960
have a string here
3700
02:13:50,960 --> 02:13:52,800
okay let's zoom in so you can see that
3701
02:13:52,800 --> 02:13:54,159
better
3702
02:13:54,159 --> 02:13:56,079
all right so say i have a string here
3703
02:13:56,079 --> 02:13:57,760
that is
3704
02:13:57,760 --> 02:14:00,480
banana if i just call dot length on this
3705
02:14:00,480 --> 02:14:03,360
and run this you notice it'll give me a
3706
02:14:03,360 --> 02:14:04,560
six
3707
02:14:04,560 --> 02:14:06,880
okay
3708
02:14:07,119 --> 02:14:11,520
and uh let's call let's say
3709
02:14:11,520 --> 02:14:14,400
banana wants to know
3710
02:14:14,400 --> 02:14:16,239
your slogan
3711
02:14:16,239 --> 02:14:19,760
dot length it'll give me back 32 so a
3712
02:14:19,760 --> 02:14:23,360
dot length is a property
3713
02:14:23,360 --> 02:14:24,320
um
3714
02:14:24,320 --> 02:14:26,960
on anything that's of type string you
3715
02:14:26,960 --> 02:14:28,560
can call anything that's a string you
3716
02:14:28,560 --> 02:14:32,639
can call dot length on it okay
3717
02:14:32,719 --> 02:14:34,800
and that returns the final answer
3718
02:14:34,800 --> 02:14:37,280
so let me zoom out here which means
3719
02:14:37,280 --> 02:14:39,440
which means i could actually remove my
3720
02:14:39,440 --> 02:14:40,880
entire code
3721
02:14:40,880 --> 02:14:42,560
right
3722
02:14:42,560 --> 02:14:45,520
and basically just say
3723
02:14:45,520 --> 02:14:49,280
the result is phrase dot length
3724
02:14:49,280 --> 02:14:51,599
change this whole thing with one line of
3725
02:14:51,599 --> 02:14:55,360
code hit run
3726
02:14:55,360 --> 02:14:57,840
write your phrase
3727
02:14:57,840 --> 02:14:59,520
it's going to say something crazy and
3728
02:14:59,520 --> 02:15:01,360
let's see if it can calculate it fast
3729
02:15:01,360 --> 02:15:04,800
and it did and it goes result is 107
3730
02:15:04,800 --> 02:15:05,840
okay
3731
02:15:05,840 --> 02:15:08,719
but let's write it in our way because
3732
02:15:08,719 --> 02:15:10,480
remember that's the way you learn how to
3733
02:15:10,480 --> 02:15:12,400
do it and right now it's important for
3734
02:15:12,400 --> 02:15:15,599
you to manually learn to do these things
3735
02:15:15,599 --> 02:15:17,840
because i want you to develop and focus
3736
02:15:17,840 --> 02:15:19,520
on your skill set then once you get
3737
02:15:19,520 --> 02:15:20,400
better
3738
02:15:20,400 --> 02:15:22,960
normally what you would do
3739
02:15:22,960 --> 02:15:24,320
is
3740
02:15:24,320 --> 02:15:25,360
you know
3741
02:15:25,360 --> 02:15:28,000
a javascript string
3742
02:15:28,000 --> 02:15:30,800
um length you will just like google this
3743
02:15:30,800 --> 02:15:32,880
okay and see if that exists and you'll
3744
02:15:32,880 --> 02:15:35,440
google this and you'll go somewhere and
3745
02:15:35,440 --> 02:15:37,920
it will say that let's see
3746
02:15:37,920 --> 02:15:39,920
w3schools is usually a really good
3747
02:15:39,920 --> 02:15:41,360
resource
3748
02:15:41,360 --> 02:15:43,679
and it's just showing you text make a
3749
02:15:43,679 --> 02:15:46,400
text let it equal to hello world
3750
02:15:46,400 --> 02:15:49,040
let length be text.length and it also
3751
02:15:49,040 --> 02:15:50,639
allows you to try it yourself so you can
3752
02:15:50,639 --> 02:15:53,360
always click that and you could hit run
3753
02:15:53,360 --> 02:15:55,280
right and boom it says 12 here and what
3754
02:15:55,280 --> 02:15:57,360
if i change this to something random and
3755
02:15:57,360 --> 02:15:58,480
let's hit run
3756
02:15:58,480 --> 02:16:01,360
and you can see it says 38
3757
02:16:01,360 --> 02:16:03,199
right over there yeah
3758
02:16:03,199 --> 02:16:04,960
so
3759
02:16:04,960 --> 02:16:07,040
uh this is how you would normally do it
3760
02:16:07,040 --> 02:16:08,560
in real life
3761
02:16:08,560 --> 02:16:10,239
you just google these things you have to
3762
02:16:10,239 --> 02:16:12,560
memorize anything okay
3763
02:16:12,560 --> 02:16:14,239
but right now i'm just kind of getting
3764
02:16:14,239 --> 02:16:16,480
used to having you get used to exercises
3765
02:16:16,480 --> 02:16:18,719
do them yourself okay now let's build it
3766
02:16:18,719 --> 02:16:21,599
up and take it up a notch okay so let's
3767
02:16:21,599 --> 02:16:23,760
let's make a function
3768
02:16:23,760 --> 02:16:25,840
that sums things okay that's also going
3769
02:16:25,840 --> 02:16:27,440
to require loops
3770
02:16:27,440 --> 02:16:28,719
and arrays
3771
02:16:28,719 --> 02:16:31,679
so i'm going to go ahead and we're gonna
3772
02:16:31,679 --> 02:16:33,439
comment this out and we're gonna comment
3773
02:16:33,439 --> 02:16:37,200
this out so we're gonna go const uh sum
3774
02:16:37,200 --> 02:16:38,959
array
3775
02:16:38,959 --> 02:16:41,840
it's gonna take in some numbers
3776
02:16:41,840 --> 02:16:43,280
and
3777
02:16:43,280 --> 02:16:45,599
we're going to have a result we're going
3778
02:16:45,599 --> 02:16:47,439
to say
3779
02:16:47,439 --> 02:16:49,840
let result equal zero
3780
02:16:49,840 --> 02:16:50,799
okay
3781
02:16:50,799 --> 02:16:52,000
and then we're going to have some kind
3782
02:16:52,000 --> 02:16:54,718
of for loop action going on here and
3783
02:16:54,718 --> 02:16:56,398
then we're gonna
3784
02:16:56,398 --> 02:16:57,760
return the
3785
02:16:57,760 --> 02:17:00,398
result and let's just do this trick like
3786
02:17:00,398 --> 02:17:02,799
that we did last time and
3787
02:17:02,799 --> 02:17:04,638
something is going to go in the for loop
3788
02:17:04,638 --> 02:17:06,000
okay something is going to go inside of
3789
02:17:06,000 --> 02:17:08,318
the for loop oh sorry we forgot to put
3790
02:17:08,318 --> 02:17:10,558
the arrow functions not bad arrows there
3791
02:17:10,558 --> 02:17:12,318
we go and what's going to go inside of
3792
02:17:12,318 --> 02:17:14,398
this for loop is basically going to be
3793
02:17:14,398 --> 02:17:16,000
imagine this right imagine how we would
3794
02:17:16,000 --> 02:17:19,920
do this manually so say we have
3795
02:17:20,240 --> 02:17:22,160
these numbers or let's say we have these
3796
02:17:22,160 --> 02:17:24,160
numbers okay this we know that this
3797
02:17:24,160 --> 02:17:27,120
should if we ever have 1 2 3 4 we know
3798
02:17:27,120 --> 02:17:30,479
that this should equal to 10.
3799
02:17:30,558 --> 02:17:32,638
now how are we going to actually sum
3800
02:17:32,638 --> 02:17:35,040
this up in normal life what would our
3801
02:17:35,040 --> 02:17:37,840
pattern be well i probably have some
3802
02:17:37,840 --> 02:17:40,318
variable let's just call it result and i
3803
02:17:40,318 --> 02:17:41,120
would
3804
02:17:41,120 --> 02:17:42,638
set it to
3805
02:17:42,638 --> 02:17:46,000
zero in the start or i might set it to
3806
02:17:46,000 --> 02:17:48,558
whatever this number is right over here
3807
02:17:48,558 --> 02:17:51,120
okay so you could you could do either or
3808
02:17:51,120 --> 02:17:52,478
but let's just start it with being a
3809
02:17:52,478 --> 02:17:54,799
zero okay so it starts off being a zero
3810
02:17:54,799 --> 02:17:57,519
then what we do is we loop through and
3811
02:17:57,519 --> 02:18:00,879
we make the result becomes one then we
3812
02:18:00,879 --> 02:18:03,599
loop through again and we do one plus
3813
02:18:03,599 --> 02:18:06,398
two and then result becomes two sorry
3814
02:18:06,398 --> 02:18:09,920
three then we loop through again and we
3815
02:18:09,920 --> 02:18:13,120
already have this 3 here so we add 3 to
3816
02:18:13,120 --> 02:18:14,799
this 3 right here
3817
02:18:14,799 --> 02:18:16,879
so we add this to that
3818
02:18:16,879 --> 02:18:19,040
and we get result is equal to 6 and then
3819
02:18:19,040 --> 02:18:21,679
we add 4 to that and we get 10. so
3820
02:18:21,679 --> 02:18:23,200
that's how we would do it kind of as a
3821
02:18:23,200 --> 02:18:24,799
human being if we were to do it like
3822
02:18:24,799 --> 02:18:26,000
manually
3823
02:18:26,000 --> 02:18:27,599
and do it with each step and that's how
3824
02:18:27,599 --> 02:18:29,359
we're going to do it with our computer
3825
02:18:29,359 --> 02:18:30,638
we're just going to have it loop through
3826
02:18:30,638 --> 02:18:32,718
and keep adding these numbers
3827
02:18:32,718 --> 02:18:34,318
so let's go ahead and now write that
3828
02:18:34,318 --> 02:18:36,718
loop so again try to give this a try and
3829
02:18:36,718 --> 02:18:38,718
see if you can do this yourself okay
3830
02:18:38,718 --> 02:18:40,478
because what you've learned so far you
3831
02:18:40,478 --> 02:18:42,240
should be able to come very close to
3832
02:18:42,240 --> 02:18:44,558
solving this so take five seconds to do
3833
02:18:44,558 --> 02:18:46,959
it and also pause your pause my video in
3834
02:18:46,959 --> 02:18:48,799
these five seconds to try it yourself so
3835
02:18:48,799 --> 02:18:51,200
five four three
3836
02:18:51,200 --> 02:18:53,200
two one
3837
02:18:53,200 --> 02:18:54,959
all right let's get back to it so here
3838
02:18:54,959 --> 02:18:55,920
we go
3839
02:18:55,920 --> 02:18:57,599
i'm gonna say
3840
02:18:57,599 --> 02:18:58,638
for
3841
02:18:58,638 --> 02:18:59,920
const
3842
02:18:59,920 --> 02:19:02,558
number of numbers
3843
02:19:02,558 --> 02:19:04,718
all right and let's just call console
3844
02:19:04,718 --> 02:19:06,879
log number
3845
02:19:06,879 --> 02:19:10,558
and uh let's call our function console
3846
02:19:10,558 --> 02:19:12,398
sum array
3847
02:19:12,398 --> 02:19:13,920
okay and
3848
02:19:13,920 --> 02:19:15,840
let's give it an array so we're going to
3849
02:19:15,840 --> 02:19:17,359
have we're going to give it this array
3850
02:19:17,359 --> 02:19:19,599
of no it's going to be called nums okay
3851
02:19:19,599 --> 02:19:21,519
let's remove
3852
02:19:21,519 --> 02:19:23,120
let's comment out all of this because we
3853
02:19:23,120 --> 02:19:26,718
don't need that and let's pass our
3854
02:19:26,718 --> 02:19:30,000
summary nums and let's just run this
3855
02:19:30,000 --> 02:19:32,240
okay so we get back one two three four
3856
02:19:32,240 --> 02:19:33,679
because it's looping through and
3857
02:19:33,679 --> 02:19:35,920
printing number out each time
3858
02:19:35,920 --> 02:19:37,599
so if i give it an array of one two
3859
02:19:37,599 --> 02:19:40,160
three four it goes okay number is one
3860
02:19:40,160 --> 02:19:42,398
the first time print it out number is
3861
02:19:42,398 --> 02:19:44,398
two the second time print it out numbers
3862
02:19:44,398 --> 02:19:46,478
three the third time print it out so on
3863
02:19:46,478 --> 02:19:49,600
and so forth all right so it's printing
3864
02:19:49,600 --> 02:19:51,840
this out result is still saying zero so
3865
02:19:51,840 --> 02:19:53,359
now what we need to do is we need to
3866
02:19:53,359 --> 02:19:56,319
increment our result so let's go ahead
3867
02:19:56,319 --> 02:19:58,800
and do that now okay so again try to
3868
02:19:58,800 --> 02:20:00,880
give this a try and now we're gonna do
3869
02:20:00,880 --> 02:20:02,560
this so all i'm gonna say is i'm gonna
3870
02:20:02,560 --> 02:20:05,120
say result and i'm gonna say result
3871
02:20:05,120 --> 02:20:06,640
plus
3872
02:20:06,640 --> 02:20:09,200
and we're going to do
3873
02:20:09,200 --> 02:20:10,720
whatever is the new number that i'm
3874
02:20:10,720 --> 02:20:12,399
getting okay
3875
02:20:12,399 --> 02:20:15,280
and let's just see if this works and i'm
3876
02:20:15,280 --> 02:20:16,960
going to hit run
3877
02:20:16,960 --> 02:20:19,280
and it says result is 10. interesting
3878
02:20:19,280 --> 02:20:20,960
did that really work well let's try it
3879
02:20:20,960 --> 02:20:23,520
with and let's add 5 to this array and
3880
02:20:23,520 --> 02:20:25,840
let's see if that works and i'm going to
3881
02:20:25,840 --> 02:20:28,240
hit run and we should get 15
3882
02:20:28,240 --> 02:20:31,600
and boom result is 15. beautiful okay so
3883
02:20:31,600 --> 02:20:32,960
let's see
3884
02:20:32,960 --> 02:20:34,000
um
3885
02:20:34,000 --> 02:20:36,479
how this is happening what is going on
3886
02:20:36,479 --> 02:20:38,160
all right so let's do something to
3887
02:20:38,160 --> 02:20:41,680
visualize this code and see exactly how
3888
02:20:41,680 --> 02:20:43,520
this works i'm going to copy this let's
3889
02:20:43,520 --> 02:20:44,920
go over to
3890
02:20:44,920 --> 02:20:46,960
pythontutor.com all right and then hit
3891
02:20:46,960 --> 02:20:49,840
enter once you're in pythontutor.com
3892
02:20:49,840 --> 02:20:51,680
you're going to go to related services
3893
02:20:51,680 --> 02:20:54,240
where it says javascript tutor okay and
3894
02:20:54,240 --> 02:20:57,359
click that this website is awesome so
3895
02:20:57,359 --> 02:20:58,880
paste your code
3896
02:20:58,880 --> 02:21:00,880
and uh just for simplicity sake i'm
3897
02:21:00,880 --> 02:21:02,800
gonna remove any console log because we
3898
02:21:02,800 --> 02:21:04,000
don't really need that it's gonna help
3899
02:21:04,000 --> 02:21:05,600
us visualize so
3900
02:21:05,600 --> 02:21:07,600
there's gonna be no need for that and we
3901
02:21:07,600 --> 02:21:08,960
don't need any comments either it's
3902
02:21:08,960 --> 02:21:10,479
unnecessary for you to remove it but i'm
3903
02:21:10,479 --> 02:21:12,240
just gonna remove that for simple
3904
02:21:12,240 --> 02:21:14,000
simplicity sake
3905
02:21:14,000 --> 02:21:15,680
then hit this big button here that says
3906
02:21:15,680 --> 02:21:18,640
visualize execution so let's click it
3907
02:21:18,640 --> 02:21:20,640
okay perfect
3908
02:21:20,640 --> 02:21:21,760
all right
3909
02:21:21,760 --> 02:21:22,640
now
3910
02:21:22,640 --> 02:21:25,120
on the left hand side is going to be our
3911
02:21:25,120 --> 02:21:27,120
code on the right hand side is going to
3912
02:21:27,120 --> 02:21:29,359
be visualizing exactly how this code is
3913
02:21:29,359 --> 02:21:30,240
running
3914
02:21:30,240 --> 02:21:32,319
very important to notice
3915
02:21:32,319 --> 02:21:33,680
also
3916
02:21:33,680 --> 02:21:35,280
this red
3917
02:21:35,280 --> 02:21:36,240
arrow
3918
02:21:36,240 --> 02:21:39,760
means the next line to execute so which
3919
02:21:39,760 --> 02:21:41,200
is the next line of code is going to
3920
02:21:41,200 --> 02:21:43,280
execute okay so it's already telling us
3921
02:21:43,280 --> 02:21:44,319
that this is
3922
02:21:44,319 --> 02:21:46,560
going to be when i hit run or when i hit
3923
02:21:46,560 --> 02:21:48,640
next to step through this
3924
02:21:48,640 --> 02:21:50,240
this is the line of code that the
3925
02:21:50,240 --> 02:21:51,840
computer is going to run
3926
02:21:51,840 --> 02:21:53,600
and we're just going to go through step
3927
02:21:53,600 --> 02:21:56,560
by step and see exactly which line runs
3928
02:21:56,560 --> 02:21:57,920
exactly when
3929
02:21:57,920 --> 02:22:00,399
and green arrow will mean
3930
02:22:00,399 --> 02:22:02,960
the line that was just previously
3931
02:22:02,960 --> 02:22:04,560
executed
3932
02:22:04,560 --> 02:22:05,359
cool
3933
02:22:05,359 --> 02:22:07,200
so this is our function let's walk
3934
02:22:07,200 --> 02:22:08,640
through this all right so let's go ahead
3935
02:22:08,640 --> 02:22:10,640
and hit next okay
3936
02:22:10,640 --> 02:22:12,800
so now it sees we have a function called
3937
02:22:12,800 --> 02:22:15,280
summary and
3938
02:22:15,280 --> 02:22:17,680
boom it's like okay i got you there it
3939
02:22:17,680 --> 02:22:20,160
is there it is i see it let's hit next
3940
02:22:20,160 --> 02:22:21,040
again
3941
02:22:21,040 --> 02:22:23,520
now it's on const
3942
02:22:23,520 --> 02:22:25,120
const nums as you can see right here
3943
02:22:25,120 --> 02:22:27,040
right so this is a line that's about to
3944
02:22:27,040 --> 02:22:29,600
execute next so let's hit next and it
3945
02:22:29,600 --> 02:22:32,000
goes okay i see that there is a variable
3946
02:22:32,000 --> 02:22:34,640
called nums and it's inside of this
3947
02:22:34,640 --> 02:22:36,960
array okay or it's this array right here
3948
02:22:36,960 --> 02:22:39,280
it refers to this array and this is a
3949
02:22:39,280 --> 02:22:41,439
global frame because this is
3950
02:22:41,439 --> 02:22:43,200
uh these are all variables that are
3951
02:22:43,200 --> 02:22:45,840
outside of this function so these are
3952
02:22:45,840 --> 02:22:47,680
accessible from anywhere and from
3953
02:22:47,680 --> 02:22:49,520
everywhere okay and we'll talk more
3954
02:22:49,520 --> 02:22:51,920
about scope in the upcoming videos now
3955
02:22:51,920 --> 02:22:53,760
next line is going to execute is it's
3956
02:22:53,760 --> 02:22:56,080
going to call this function summary with
3957
02:22:56,080 --> 02:22:58,960
the argument nums okay and then our
3958
02:22:58,960 --> 02:23:00,080
entire function is going to start
3959
02:23:00,080 --> 02:23:01,600
running so let's hit next
3960
02:23:01,600 --> 02:23:02,560
cool
3961
02:23:02,560 --> 02:23:04,960
so now we're inside of our function and
3962
02:23:04,960 --> 02:23:07,200
the first line of our function is just
3963
02:23:07,200 --> 02:23:09,280
about to run
3964
02:23:09,280 --> 02:23:10,399
so the first line is going to be what
3965
02:23:10,399 --> 02:23:11,520
result is
3966
02:23:11,520 --> 02:23:12,319
cool
3967
02:23:12,319 --> 02:23:14,160
so let's uh right now result is
3968
02:23:14,160 --> 02:23:16,720
undefined okay but once this line runs
3969
02:23:16,720 --> 02:23:18,640
here result is going to have a value so
3970
02:23:18,640 --> 02:23:20,000
let's hit next
3971
02:23:20,000 --> 02:23:23,280
and boom result is now set to zero
3972
02:23:23,280 --> 02:23:24,560
now
3973
02:23:24,560 --> 02:23:26,160
okay so this green is just showing you
3974
02:23:26,160 --> 02:23:28,160
the last line that ran
3975
02:23:28,160 --> 02:23:30,000
now the red is showing you that we're
3976
02:23:30,000 --> 02:23:32,479
about to start running our for loop and
3977
02:23:32,479 --> 02:23:34,240
we're just gonna be teeter-tottering in
3978
02:23:34,240 --> 02:23:35,200
between
3979
02:23:35,200 --> 02:23:37,439
this line the for loop line line four
3980
02:23:37,439 --> 02:23:38,880
and line five and line four and line
3981
02:23:38,880 --> 02:23:40,479
five and line four and line five until
3982
02:23:40,479 --> 02:23:42,720
the entire loop is done then we're gonna
3983
02:23:42,720 --> 02:23:44,560
go to this line here and return the
3984
02:23:44,560 --> 02:23:47,359
result at the end okay which and again
3985
02:23:47,359 --> 02:23:49,840
remember uh you know we're gonna touch
3986
02:23:49,840 --> 02:23:51,359
base on scope
3987
02:23:51,359 --> 02:23:53,280
but if you put this if you put things in
3988
02:23:53,280 --> 02:23:54,720
the wrong scope wrong things will happen
3989
02:23:54,720 --> 02:23:56,399
for example if you put this result
3990
02:23:56,399 --> 02:23:58,960
inside of this these squigglies here
3991
02:23:58,960 --> 02:24:00,160
right this is just going to keep
3992
02:24:00,160 --> 02:24:02,000
returning every single result but that's
3993
02:24:02,000 --> 02:24:03,600
not what we want we want to return the
3994
02:24:03,600 --> 02:24:06,319
results after this entire for loop is
3995
02:24:06,319 --> 02:24:08,800
done right and then we want to return
3996
02:24:08,800 --> 02:24:10,319
this result okay
3997
02:24:10,319 --> 02:24:12,000
so let's run through this and see what
3998
02:24:12,000 --> 02:24:13,520
happens okay so right now it doesn't
3999
02:24:13,520 --> 02:24:15,600
know what number is either
4000
02:24:15,600 --> 02:24:18,160
okay but it does know it results as zero
4001
02:24:18,160 --> 02:24:21,280
and it also knows that numbers
4002
02:24:21,280 --> 02:24:23,920
refers to this array right here okay
4003
02:24:23,920 --> 02:24:25,280
let's hit next
4004
02:24:25,280 --> 02:24:26,560
okay
4005
02:24:26,560 --> 02:24:27,600
boom
4006
02:24:27,600 --> 02:24:30,240
boom boom all right there we go
4007
02:24:30,240 --> 02:24:33,680
and now it says number is one perfect
4008
02:24:33,680 --> 02:24:35,359
that's exactly what we wanted right
4009
02:24:35,359 --> 02:24:37,359
number is one because the first time
4010
02:24:37,359 --> 02:24:39,120
we're going through the array right it's
4011
02:24:39,120 --> 02:24:40,880
a one that's what that's where the
4012
02:24:40,880 --> 02:24:43,040
number is coming from now let's see what
4013
02:24:43,040 --> 02:24:44,319
it's going to do the result so watch
4014
02:24:44,319 --> 02:24:45,920
what's going to happen it's going to do
4015
02:24:45,920 --> 02:24:47,439
result is going to be 1 this time so
4016
02:24:47,439 --> 02:24:49,920
it's going to do 1 plus or sorry result
4017
02:24:49,920 --> 02:24:52,319
is 0 right now so we're going to do 0
4018
02:24:52,319 --> 02:24:54,319
plus the number 1.
4019
02:24:54,319 --> 02:24:56,080
okay let's try that and now you can see
4020
02:24:56,080 --> 02:24:58,720
result is one let's do it again now
4021
02:24:58,720 --> 02:25:02,240
number is two so two plus one result is
4022
02:25:02,240 --> 02:25:04,560
going to be three
4023
02:25:04,560 --> 02:25:05,760
okay
4024
02:25:05,760 --> 02:25:07,760
three plus three result is going to be
4025
02:25:07,760 --> 02:25:08,880
six
4026
02:25:08,880 --> 02:25:11,840
six plus four result is going to be
4027
02:25:11,840 --> 02:25:14,880
10. okay and notice how it was just like
4028
02:25:14,880 --> 02:25:16,720
boom boom boom boom it's just these
4029
02:25:16,720 --> 02:25:19,520
lines running over and over again right
4030
02:25:19,520 --> 02:25:22,399
and now the result is 15 now the loop
4031
02:25:22,399 --> 02:25:24,720
ends because are no more numbers to loop
4032
02:25:24,720 --> 02:25:25,840
through
4033
02:25:25,840 --> 02:25:27,840
and we go to result
4034
02:25:27,840 --> 02:25:30,080
result is 15
4035
02:25:30,080 --> 02:25:32,319
and then we return the result at the end
4036
02:25:32,319 --> 02:25:33,840
return value
4037
02:25:33,840 --> 02:25:35,760
right and the return value here in this
4038
02:25:35,760 --> 02:25:39,120
case is
4039
02:25:39,120 --> 02:25:42,080
this object it was of type object
4040
02:25:42,080 --> 02:25:46,240
and it says result is equal to 15.
4041
02:25:46,240 --> 02:25:47,520
okay
4042
02:25:47,520 --> 02:25:50,000
so there you have it a full
4043
02:25:50,000 --> 02:25:53,359
visualization of a for loop exactly how
4044
02:25:53,359 --> 02:25:55,359
it works and what is happening and
4045
02:25:55,359 --> 02:25:57,920
hopefully that clarifies things for you
4046
02:25:57,920 --> 02:25:59,760
a lot further
4047
02:25:59,760 --> 02:26:01,840
so i want you to try something
4048
02:26:01,840 --> 02:26:04,840
try writing this function some
4049
02:26:04,840 --> 02:26:07,920
array by yourself from scratch
4050
02:26:07,920 --> 02:26:10,720
and i know we just did the answer
4051
02:26:10,720 --> 02:26:12,960
but i want you to give it a try without
4052
02:26:12,960 --> 02:26:14,960
referring to this and see if you could
4053
02:26:14,960 --> 02:26:17,439
create this function by yourself and if
4054
02:26:17,439 --> 02:26:19,439
you could that would be fantastic and
4055
02:26:19,439 --> 02:26:21,120
just give it a give it give it a shot
4056
02:26:21,120 --> 02:26:23,280
right and if you could get this working
4057
02:26:23,280 --> 02:26:25,359
see you know that would be absolutely
4058
02:26:25,359 --> 02:26:26,880
amazing and then check back with this
4059
02:26:26,880 --> 02:26:28,960
and see how far you got all right let's
4060
02:26:28,960 --> 02:26:30,080
do some
4061
02:26:30,080 --> 02:26:33,200
more exercises with loops
4062
02:26:33,200 --> 02:26:35,200
arrays objects whatever right because
4063
02:26:35,200 --> 02:26:36,800
this is going to make you so much better
4064
02:26:36,800 --> 02:26:37,920
so
4065
02:26:37,920 --> 02:26:40,800
let's make a function called max so
4066
02:26:40,800 --> 02:26:43,840
let's do const let's say find max okay
4067
02:26:43,840 --> 02:26:46,240
or you could call it max as well
4068
02:26:46,240 --> 02:26:49,200
and it's going to take in numbers and
4069
02:26:49,200 --> 02:26:52,880
it's going to have a let's say
4070
02:26:52,880 --> 02:26:55,520
cons result and then
4071
02:26:55,520 --> 02:26:58,319
it's going to return the result
4072
02:26:58,319 --> 02:27:01,680
okay and then we're going to have
4073
02:27:01,680 --> 02:27:04,000
something some loop stuff happen here
4074
02:27:04,000 --> 02:27:06,560
okay some loop stuff and here's how the
4075
02:27:06,560 --> 02:27:09,120
algorithm is gonna work okay imagine we
4076
02:27:09,120 --> 02:27:11,920
have an array of you know let's just say
4077
02:27:11,920 --> 02:27:13,120
one
4078
02:27:13,120 --> 02:27:14,640
two
4079
02:27:14,640 --> 02:27:18,000
three and four and five okay imagine we
4080
02:27:18,000 --> 02:27:19,600
have this array right here and let's
4081
02:27:19,600 --> 02:27:21,359
just put commas here
4082
02:27:21,359 --> 02:27:23,120
because that's how the array would look
4083
02:27:23,120 --> 02:27:25,439
i have these commas and what we're gonna
4084
02:27:25,439 --> 02:27:27,760
do is we're initially gonna set result
4085
02:27:27,760 --> 02:27:29,359
to one and then we're gonna loop through
4086
02:27:29,359 --> 02:27:30,960
and we're going to say hey it's 2
4087
02:27:30,960 --> 02:27:33,200
greater than 1.
4088
02:27:33,200 --> 02:27:34,960
check these
4089
02:27:34,960 --> 02:27:37,920
if it is then result set it to 2.
4090
02:27:37,920 --> 02:27:41,359
okay then loop again
4091
02:27:41,359 --> 02:27:44,960
all right is 3 greater than two
4092
02:27:44,960 --> 02:27:47,760
if it is then set result to this then
4093
02:27:47,760 --> 02:27:49,600
check between these two numbers if four
4094
02:27:49,600 --> 02:27:52,000
is greater set four if five is greater
4095
02:27:52,000 --> 02:27:53,359
set five
4096
02:27:53,359 --> 02:27:55,200
and then once the loop is done it's
4097
02:27:55,200 --> 02:27:57,359
going to return the biggest number
4098
02:27:57,359 --> 02:28:01,680
okay and now imagine if you know in in
4099
02:28:01,680 --> 02:28:03,600
let's say there was a case where we had
4100
02:28:03,600 --> 02:28:04,640
a
4101
02:28:04,640 --> 02:28:05,840
one here
4102
02:28:05,840 --> 02:28:08,000
okay let's see if it would work if that
4103
02:28:08,000 --> 02:28:10,080
same algorithm would work there
4104
02:28:10,080 --> 02:28:13,760
okay set one two the biggest set one
4105
02:28:13,760 --> 02:28:16,080
to result okay great
4106
02:28:16,080 --> 02:28:17,760
now check between these two's two
4107
02:28:17,760 --> 02:28:18,880
greater
4108
02:28:18,880 --> 02:28:21,920
yes then make two the result okay then
4109
02:28:21,920 --> 02:28:23,600
check between these
4110
02:28:23,600 --> 02:28:25,439
is three greater okay make three the
4111
02:28:25,439 --> 02:28:27,840
result check between these
4112
02:28:27,840 --> 02:28:29,520
is four greater okay make four the
4113
02:28:29,520 --> 02:28:30,560
result
4114
02:28:30,560 --> 02:28:33,120
check between these is one greater
4115
02:28:33,120 --> 02:28:34,399
no
4116
02:28:34,399 --> 02:28:37,359
then don't make one the result keep for
4117
02:28:37,359 --> 02:28:39,359
the result right and and then that's how
4118
02:28:39,359 --> 02:28:42,080
we're we end up with four so that's kind
4119
02:28:42,080 --> 02:28:44,399
of the algorithm that we would do okay
4120
02:28:44,399 --> 02:28:46,880
now we just have to code it up so we're
4121
02:28:46,880 --> 02:28:49,600
basically gonna say set result not to
4122
02:28:49,600 --> 02:28:52,240
zero okay because imagine
4123
02:28:52,240 --> 02:28:54,399
what if you had an array with only
4124
02:28:54,399 --> 02:28:56,319
negative numbers in it well then zero
4125
02:28:56,319 --> 02:28:58,000
would be the biggest number so that's a
4126
02:28:58,000 --> 02:29:00,560
common trap do not set result to zero
4127
02:29:00,560 --> 02:29:03,280
we're gonna instead say
4128
02:29:03,280 --> 02:29:06,880
results is the first item of this array
4129
02:29:06,880 --> 02:29:08,160
okay we're assuming we're getting back
4130
02:29:08,160 --> 02:29:10,560
an array or we're getting an array here
4131
02:29:10,560 --> 02:29:11,600
okay
4132
02:29:11,600 --> 02:29:14,080
and then we're gonna say is loop so
4133
02:29:14,080 --> 02:29:15,840
we're gonna say we're gonna write a four
4134
02:29:15,840 --> 02:29:18,640
of loop const number of numbers
4135
02:29:18,640 --> 02:29:21,680
and basically we're gonna say if
4136
02:29:21,680 --> 02:29:23,920
number is
4137
02:29:23,920 --> 02:29:26,240
greater than result
4138
02:29:26,240 --> 02:29:28,560
in that case what do we want to do
4139
02:29:28,560 --> 02:29:30,479
we want to set result
4140
02:29:30,479 --> 02:29:32,399
to that number that we just found
4141
02:29:32,399 --> 02:29:34,240
and at the end
4142
02:29:34,240 --> 02:29:36,399
when we're done with this loop we just
4143
02:29:36,399 --> 02:29:37,359
simply
4144
02:29:37,359 --> 02:29:39,520
return the result okay so let's go ahead
4145
02:29:39,520 --> 02:29:41,359
and give this a try
4146
02:29:41,359 --> 02:29:43,200
and let's comment out anything else that
4147
02:29:43,200 --> 02:29:45,120
we might have running okay we don't have
4148
02:29:45,120 --> 02:29:47,520
anything else running so we're oh we do
4149
02:29:47,520 --> 02:29:48,640
so let's
4150
02:29:48,640 --> 02:29:51,120
comment out these lines right here and
4151
02:29:51,120 --> 02:29:52,880
let's just go console.log and i'm going
4152
02:29:52,880 --> 02:29:55,280
to say max and let's give it numbers
4153
02:29:55,280 --> 02:29:57,280
like one two three four
4154
02:29:57,280 --> 02:29:58,160
five
4155
02:29:58,160 --> 02:30:00,319
and let's see what it returns as the max
4156
02:30:00,319 --> 02:30:02,720
aha yeah so this is not a const let's
4157
02:30:02,720 --> 02:30:04,240
make this a let because we are going to
4158
02:30:04,240 --> 02:30:06,960
be changing result okay so this error is
4159
02:30:06,960 --> 02:30:09,280
saying assignment to constant variable
4160
02:30:09,280 --> 02:30:11,439
so all it's saying is hey you cannot
4161
02:30:11,439 --> 02:30:14,160
redefine a constant so that's why i'm
4162
02:30:14,160 --> 02:30:16,160
changing it to a let now you can
4163
02:30:16,160 --> 02:30:17,840
redefine it right here and we should be
4164
02:30:17,840 --> 02:30:21,200
good and let's do that trick again let's
4165
02:30:21,200 --> 02:30:23,520
return result as an object so it looks
4166
02:30:23,520 --> 02:30:24,880
nice when we print it and you can see
4167
02:30:24,880 --> 02:30:28,240
result is five now imagine we have one
4168
02:30:28,240 --> 02:30:30,479
comma two one two
4169
02:30:30,479 --> 02:30:33,120
so the max in this array is what
4170
02:30:33,120 --> 02:30:34,560
it is a
4171
02:30:34,560 --> 02:30:36,960
it's the 4. so let's
4172
02:30:36,960 --> 02:30:38,800
see if it gives us a 4.
4173
02:30:38,800 --> 02:30:41,280
yes it does beautiful look at that let's
4174
02:30:41,280 --> 02:30:43,200
go 20 now
4175
02:30:43,200 --> 02:30:44,399
and 10.
4176
02:30:44,399 --> 02:30:46,560
and let's see if that works beautiful
4177
02:30:46,560 --> 02:30:48,479
and now let's make this array
4178
02:30:48,479 --> 02:30:50,319
now let's make this massive right like
4179
02:30:50,319 --> 02:30:51,840
let's just
4180
02:30:51,840 --> 02:30:54,080
uh
4181
02:30:54,640 --> 02:30:56,960
yeah let's let's take this right here
4182
02:30:56,960 --> 02:30:58,960
and let's just paste a couple of times
4183
02:30:58,960 --> 02:31:00,800
and run it and look at that it says
4184
02:31:00,800 --> 02:31:03,760
result is 20 and if i make this
4185
02:31:03,760 --> 02:31:06,720
a 3 over here and run it
4186
02:31:06,720 --> 02:31:09,040
boom result is 30. i mean it will find
4187
02:31:09,040 --> 02:31:11,439
it wherever the max is it will find that
4188
02:31:11,439 --> 02:31:14,399
max right so this is a really nice
4189
02:31:14,399 --> 02:31:17,120
function that we have created now let's
4190
02:31:17,120 --> 02:31:19,200
keep building on this okay so this is
4191
02:31:19,200 --> 02:31:20,800
all going to be making sense and trust
4192
02:31:20,800 --> 02:31:22,240
me it's going to be so important for you
4193
02:31:22,240 --> 02:31:24,319
once you start getting to
4194
02:31:24,319 --> 02:31:26,640
building projects and doing those types
4195
02:31:26,640 --> 02:31:28,640
of exercises you're going to wish that
4196
02:31:28,640 --> 02:31:30,960
you spent the time to learn this because
4197
02:31:30,960 --> 02:31:32,560
this is going to be a complete game
4198
02:31:32,560 --> 02:31:34,319
changer for you all right so our max
4199
02:31:34,319 --> 02:31:35,680
function is done
4200
02:31:35,680 --> 02:31:38,319
let's work on a different function now
4201
02:31:38,319 --> 02:31:41,120
and what this function will do is
4202
02:31:41,120 --> 02:31:42,720
it will
4203
02:31:42,720 --> 02:31:45,280
calculate frequency okay so let's go
4204
02:31:45,280 --> 02:31:46,560
ahead and try that so we're going to
4205
02:31:46,560 --> 02:31:47,680
const
4206
02:31:47,680 --> 02:31:50,399
frequency
4207
02:31:50,640 --> 02:31:52,640
okay it's going to take in
4208
02:31:52,640 --> 02:31:55,439
uh let's say a phrase
4209
02:31:55,439 --> 02:31:57,840
that somebody will say and i'll tell you
4210
02:31:57,840 --> 02:31:59,520
how many times
4211
02:31:59,520 --> 02:32:02,399
uh that phrase or that or sorry that
4212
02:32:02,399 --> 02:32:04,240
that letter occurs okay
4213
02:32:04,240 --> 02:32:05,840
so let's just say
4214
02:32:05,840 --> 02:32:08,319
letter frequency
4215
02:32:08,319 --> 02:32:10,800
okay so what does that mean so say i
4216
02:32:10,800 --> 02:32:11,680
said
4217
02:32:11,680 --> 02:32:12,720
haha
4218
02:32:12,720 --> 02:32:14,720
here's what it should return
4219
02:32:14,720 --> 02:32:16,720
okay so if i call the function letter
4220
02:32:16,720 --> 02:32:19,600
frequency on the word haha here's what
4221
02:32:19,600 --> 02:32:21,840
it should return it should return
4222
02:32:21,840 --> 02:32:25,520
an object and the object should say h
4223
02:32:25,520 --> 02:32:27,439
occurs twice
4224
02:32:27,439 --> 02:32:28,800
and a
4225
02:32:28,800 --> 02:32:30,720
occurs twice
4226
02:32:30,720 --> 02:32:33,680
that's what it should return so
4227
02:32:33,680 --> 02:32:35,359
how do we
4228
02:32:35,359 --> 02:32:37,840
create this function so it does that if
4229
02:32:37,840 --> 02:32:40,240
you want to give it a try you can this
4230
02:32:40,240 --> 02:32:42,160
will be hard because you'll need to use
4231
02:32:42,160 --> 02:32:44,240
your object knowledge as well that we
4232
02:32:44,240 --> 02:32:46,080
put together a while ago
4233
02:32:46,080 --> 02:32:48,479
but this will be using you know summing
4234
02:32:48,479 --> 02:32:50,479
up numbers looping through things
4235
02:32:50,479 --> 02:32:52,319
iterating and all kinds of stuff like
4236
02:32:52,319 --> 02:32:53,600
that and so
4237
02:32:53,600 --> 02:32:55,439
uh incrementing variables is going to be
4238
02:32:55,439 --> 02:32:56,800
a tough one if you try to solve it
4239
02:32:56,800 --> 02:32:59,040
yourself but you should give this a try
4240
02:32:59,040 --> 02:33:00,880
right about now and we'll continue in
4241
02:33:00,880 --> 02:33:05,120
five seconds five four three two one
4242
02:33:05,120 --> 02:33:06,479
let's continue
4243
02:33:06,479 --> 02:33:09,040
so i have letter frequency how do i
4244
02:33:09,040 --> 02:33:10,640
determine this
4245
02:33:10,640 --> 02:33:13,040
okay let's figure it out together
4246
02:33:13,040 --> 02:33:15,280
first let's just go ahead and call this
4247
02:33:15,280 --> 02:33:16,720
function so we're just going to say
4248
02:33:16,720 --> 02:33:17,600
letter
4249
02:33:17,600 --> 02:33:19,120
frequency and we're going to give it
4250
02:33:19,120 --> 02:33:22,640
that phrase haha like this
4251
02:33:22,640 --> 02:33:26,160
and uh let's console log that phrase
4252
02:33:26,160 --> 02:33:27,840
let's delete that line and let's just
4253
02:33:27,840 --> 02:33:29,840
make sure that this letter frequency is
4254
02:33:29,840 --> 02:33:32,160
running okay haha showing up perfect now
4255
02:33:32,160 --> 02:33:33,680
what do we want to do
4256
02:33:33,680 --> 02:33:35,920
we're we're getting haha
4257
02:33:35,920 --> 02:33:38,399
well we need to actually start writing a
4258
02:33:38,399 --> 02:33:40,479
for loop that will start going through
4259
02:33:40,479 --> 02:33:41,840
all of those letters because that's
4260
02:33:41,840 --> 02:33:43,840
going to be important
4261
02:33:43,840 --> 02:33:44,880
okay
4262
02:33:44,880 --> 02:33:46,720
so let's go ahead and write a for loop
4263
02:33:46,720 --> 02:33:48,319
so i'm going to say for
4264
02:33:48,319 --> 02:33:50,800
letter const letter
4265
02:33:50,800 --> 02:33:53,920
of phrase and let's just go console log
4266
02:33:53,920 --> 02:33:55,600
and we're just going to console log each
4267
02:33:55,600 --> 02:33:57,520
letter and let's see what we get
4268
02:33:57,520 --> 02:33:59,760
we get h-a-h-a
4269
02:33:59,760 --> 02:34:02,880
okay great now this is where objects are
4270
02:34:02,880 --> 02:34:04,880
going to start becoming very very handy
4271
02:34:04,880 --> 02:34:08,080
because you know key value pairs are
4272
02:34:08,080 --> 02:34:11,439
going to be absolutely clutch for this
4273
02:34:11,439 --> 02:34:13,439
um arrays are not a good solution for
4274
02:34:13,439 --> 02:34:16,000
this okay but like objects are objects
4275
02:34:16,000 --> 02:34:18,240
are also great for storing you know
4276
02:34:18,240 --> 02:34:20,640
information in your phone book like on
4277
02:34:20,640 --> 02:34:22,080
my iphone
4278
02:34:22,080 --> 02:34:24,000
when i'm going through that is being
4279
02:34:24,000 --> 02:34:26,720
stored in an object okay that's why i
4280
02:34:26,720 --> 02:34:28,319
have you know my name
4281
02:34:28,319 --> 02:34:29,920
my you know i could look up somebody by
4282
02:34:29,920 --> 02:34:32,160
their name their phone number etc and
4283
02:34:32,160 --> 02:34:34,399
objects are great for that so we're
4284
02:34:34,399 --> 02:34:37,280
gonna go and look at something in the
4285
02:34:37,280 --> 02:34:38,720
console okay and that's gonna make our
4286
02:34:38,720 --> 02:34:41,120
lives a lot easier so say we have
4287
02:34:41,120 --> 02:34:42,640
something called
4288
02:34:42,640 --> 02:34:46,160
word and this word has let's actually
4289
02:34:46,160 --> 02:34:48,240
give it let's actually call it frequency
4290
02:34:48,240 --> 02:34:49,840
right or freak
4291
02:34:49,840 --> 02:34:50,960
um
4292
02:34:50,960 --> 02:34:53,520
and let's say this frequency has
4293
02:34:53,520 --> 02:34:56,399
that the h is
4294
02:34:56,399 --> 02:34:57,359
once
4295
02:34:57,359 --> 02:34:59,280
and that's it okay so it has one h in
4296
02:34:59,280 --> 02:35:02,560
there now if we have a word like haha or
4297
02:35:02,560 --> 02:35:04,240
whatever right or if we want to just
4298
02:35:04,240 --> 02:35:05,359
check
4299
02:35:05,359 --> 02:35:09,760
yo is h inside of the frequency
4300
02:35:10,000 --> 02:35:11,680
we're actually able to check that and it
4301
02:35:11,680 --> 02:35:14,240
goes yes h actually exists because it
4302
02:35:14,240 --> 02:35:16,479
can if you do h and frequency what it
4303
02:35:16,479 --> 02:35:18,240
does is it looks for
4304
02:35:18,240 --> 02:35:20,080
h as a key
4305
02:35:20,080 --> 02:35:22,720
ins and searches all the keys inside of
4306
02:35:22,720 --> 02:35:27,040
an object okay so it goes yes h exists
4307
02:35:27,040 --> 02:35:29,200
and uh if i go
4308
02:35:29,200 --> 02:35:31,280
you know because of the word haha does a
4309
02:35:31,280 --> 02:35:33,040
exist in frequency and when i look it
4310
02:35:33,040 --> 02:35:34,640
says no
4311
02:35:34,640 --> 02:35:36,720
that a does not exist that's actually
4312
02:35:36,720 --> 02:35:37,680
false
4313
02:35:37,680 --> 02:35:40,160
so using this we're actually able to see
4314
02:35:40,160 --> 02:35:42,560
if we've already stored something in our
4315
02:35:42,560 --> 02:35:44,960
object or not because the way that we
4316
02:35:44,960 --> 02:35:47,120
want to do this is we want to go and say
4317
02:35:47,120 --> 02:35:50,640
okay so say we have the word haha then i
4318
02:35:50,640 --> 02:35:53,280
want to have a object
4319
02:35:53,280 --> 02:35:54,960
i'll just write it out here and it
4320
02:35:54,960 --> 02:35:56,479
should loop through
4321
02:35:56,479 --> 02:35:58,720
and in the first loop it should notice
4322
02:35:58,720 --> 02:36:00,960
that there's a letter h so as soon as i
4323
02:36:00,960 --> 02:36:04,640
notice the letter h i make a key for h
4324
02:36:04,640 --> 02:36:06,960
all right and because i know it showed
4325
02:36:06,960 --> 02:36:10,640
up once i set it to one so i go that key
4326
02:36:10,640 --> 02:36:13,680
is equal and it's it has a value of one
4327
02:36:13,680 --> 02:36:14,560
now
4328
02:36:14,560 --> 02:36:17,359
then i loop through that
4329
02:36:17,359 --> 02:36:19,920
other word again and this time i see
4330
02:36:19,920 --> 02:36:22,720
that we have a okay perfect
4331
02:36:22,720 --> 02:36:25,439
well have i seen a before
4332
02:36:25,439 --> 02:36:28,560
no i haven't so then i set a to 1
4333
02:36:28,560 --> 02:36:30,560
and so far so good
4334
02:36:30,560 --> 02:36:33,040
okay then we loop through the third time
4335
02:36:33,040 --> 02:36:35,680
and i see h again well i've already seen
4336
02:36:35,680 --> 02:36:36,960
this before
4337
02:36:36,960 --> 02:36:39,920
so what do i do if i've seen this before
4338
02:36:39,920 --> 02:36:42,640
am i gonna go ahead and make another h
4339
02:36:42,640 --> 02:36:44,720
and then set that to one you can't do
4340
02:36:44,720 --> 02:36:47,200
that because in objects you can't have
4341
02:36:47,200 --> 02:36:49,200
the same key repeating twice so we have
4342
02:36:49,200 --> 02:36:51,439
this key here and that this key here so
4343
02:36:51,439 --> 02:36:54,240
what will happen instead is we will end
4344
02:36:54,240 --> 02:36:56,800
up overwriting this key
4345
02:36:56,800 --> 02:36:59,439
and just do h as one and that's what it
4346
02:36:59,439 --> 02:37:00,960
was in the first place but that's not
4347
02:37:00,960 --> 02:37:03,520
the result we want we actually want to
4348
02:37:03,520 --> 02:37:06,240
we actually want to increment this key
4349
02:37:06,240 --> 02:37:07,280
right here
4350
02:37:07,280 --> 02:37:09,520
this h1 right so we want to increment
4351
02:37:09,520 --> 02:37:11,359
this one
4352
02:37:11,359 --> 02:37:14,080
by doing plus one and making it a two so
4353
02:37:14,080 --> 02:37:17,040
how do we do that what we do is we go
4354
02:37:17,040 --> 02:37:19,520
hey does h already exist and we do this
4355
02:37:19,520 --> 02:37:21,840
little checker here right there
4356
02:37:21,840 --> 02:37:24,560
and if it says yes h already exists then
4357
02:37:24,560 --> 02:37:26,560
we go go to h
4358
02:37:26,560 --> 02:37:27,439
and
4359
02:37:27,439 --> 02:37:30,720
and add one to its value so it goes okay
4360
02:37:30,720 --> 02:37:33,680
i'm to go ahead and i'm going to do that
4361
02:37:33,680 --> 02:37:37,120
so we go to this and this becomes a 2
4362
02:37:37,120 --> 02:37:39,200
right and then we go to the fourth
4363
02:37:39,200 --> 02:37:41,280
letter in haha
4364
02:37:41,280 --> 02:37:43,280
and we do the same thing and we go hey
4365
02:37:43,280 --> 02:37:46,160
does a actually exist in frequency and
4366
02:37:46,160 --> 02:37:48,479
in this case it's supposed to be a true
4367
02:37:48,479 --> 02:37:51,439
and it goes yes a does exist and so if
4368
02:37:51,439 --> 02:37:52,880
that's true
4369
02:37:52,880 --> 02:37:55,600
we then go okay find the existing a find
4370
02:37:55,600 --> 02:37:58,479
its value and increment its value by one
4371
02:37:58,479 --> 02:38:00,479
and then we get 2.
4372
02:38:00,479 --> 02:38:03,200
okay so that's how that works that's how
4373
02:38:03,200 --> 02:38:04,399
that works
4374
02:38:04,399 --> 02:38:06,880
so let's go ahead and basically say that
4375
02:38:06,880 --> 02:38:08,479
algorithm
4376
02:38:08,479 --> 02:38:10,479
but in code this time
4377
02:38:10,479 --> 02:38:13,680
so first we're going to make a frequency
4378
02:38:13,680 --> 02:38:16,640
make a frequency object
4379
02:38:16,640 --> 02:38:18,960
and that's going to store all the memory
4380
02:38:18,960 --> 02:38:21,600
slash all the data of what it has seen
4381
02:38:21,600 --> 02:38:22,880
so far
4382
02:38:22,880 --> 02:38:24,880
okay so this is so make a frequency
4383
02:38:24,880 --> 02:38:26,840
object okay it's going to be called
4384
02:38:26,840 --> 02:38:29,439
frequency kind of like we did here okay
4385
02:38:29,439 --> 02:38:30,960
but in the start it's just going to be
4386
02:38:30,960 --> 02:38:34,319
empty it's going to be an empty object
4387
02:38:34,319 --> 02:38:36,319
then we obviously loop through all the
4388
02:38:36,319 --> 02:38:38,240
letters of the phrase
4389
02:38:38,240 --> 02:38:41,200
and we check if that letter that we
4390
02:38:41,200 --> 02:38:43,120
found in phrase does that already exist
4391
02:38:43,120 --> 02:38:44,399
in
4392
02:38:44,399 --> 02:38:46,880
frequency so
4393
02:38:46,880 --> 02:38:52,319
check if letter exists in frequency
4394
02:38:52,720 --> 02:38:54,960
so you make sure to you know we're kind
4395
02:38:54,960 --> 02:38:56,800
of writing pseudo code right now because
4396
02:38:56,800 --> 02:38:58,960
we're just writing it kind of like code
4397
02:38:58,960 --> 02:39:00,720
but in english and then later you just
4398
02:39:00,720 --> 02:39:02,560
turn it into
4399
02:39:02,560 --> 02:39:04,720
code and then you're done so if we check
4400
02:39:04,720 --> 02:39:07,760
if the letter exists in frequency okay
4401
02:39:07,760 --> 02:39:09,760
what next
4402
02:39:09,760 --> 02:39:13,280
well if it exists remember if it exists
4403
02:39:13,280 --> 02:39:16,720
we're gonna increment by one
4404
02:39:16,720 --> 02:39:19,760
otherwise we set it to one
4405
02:39:19,760 --> 02:39:21,920
so let's write that check if letter
4406
02:39:21,920 --> 02:39:24,080
exists in frequency
4407
02:39:24,080 --> 02:39:26,000
if it exists so this is going to be
4408
02:39:26,000 --> 02:39:28,080
indented because this is going to be
4409
02:39:28,080 --> 02:39:30,160
nested inside of this if condition and
4410
02:39:30,160 --> 02:39:31,840
we're going to say if it exists we'll
4411
02:39:31,840 --> 02:39:34,000
then
4412
02:39:34,240 --> 02:39:36,800
increment
4413
02:39:36,800 --> 02:39:38,560
the value
4414
02:39:38,560 --> 02:39:39,760
by one
4415
02:39:39,760 --> 02:39:42,640
otherwise
4416
02:39:45,920 --> 02:39:48,960
set it set
4417
02:39:51,040 --> 02:39:54,240
the value to one
4418
02:39:54,240 --> 02:39:57,120
okay that's essentially all we're doing
4419
02:39:57,120 --> 02:39:59,680
and so now take take some time to see if
4420
02:39:59,680 --> 02:40:01,840
you can actually write this as code and
4421
02:40:01,840 --> 02:40:03,840
then obviously at the end we're just
4422
02:40:03,840 --> 02:40:05,840
going to return that frequency that we
4423
02:40:05,840 --> 02:40:07,680
created okay so we're going to return
4424
02:40:07,680 --> 02:40:11,200
that frequency so const or let's go let
4425
02:40:11,200 --> 02:40:13,520
frequency
4426
02:40:13,520 --> 02:40:16,640
equal an empty object
4427
02:40:16,640 --> 02:40:18,640
so see if you could code up the rest of
4428
02:40:18,640 --> 02:40:20,560
this okay take maybe
4429
02:40:20,560 --> 02:40:22,800
a minute or five minutes or whatever and
4430
02:40:22,800 --> 02:40:24,080
even if it takes you 20 minutes it's
4431
02:40:24,080 --> 02:40:26,399
totally fine because this is normal okay
4432
02:40:26,399 --> 02:40:28,080
you're not supposed to get it this fast
4433
02:40:28,080 --> 02:40:29,439
this is supposed to take you a lot of
4434
02:40:29,439 --> 02:40:30,399
time
4435
02:40:30,399 --> 02:40:31,840
but take some time i'll give you five
4436
02:40:31,840 --> 02:40:33,359
seconds to pause the video and try it on
4437
02:40:33,359 --> 02:40:37,200
your own five four three two one
4438
02:40:37,200 --> 02:40:39,520
all right let's do it together now
4439
02:40:39,520 --> 02:40:41,120
check if the letter exists i'm going to
4440
02:40:41,120 --> 02:40:41,920
say
4441
02:40:41,920 --> 02:40:43,520
how do i check well i just checked by
4442
02:40:43,520 --> 02:40:46,000
doing letter
4443
02:40:46,000 --> 02:40:48,080
in frequency okay does that letter exist
4444
02:40:48,080 --> 02:40:50,880
in frequency this will evaluate to true
4445
02:40:50,880 --> 02:40:53,279
or if false that's it i'm going to say
4446
02:40:53,279 --> 02:40:54,880
if
4447
02:40:54,880 --> 02:40:57,439
letter is in frequency
4448
02:40:57,439 --> 02:40:58,319
then
4449
02:40:58,319 --> 02:41:00,000
do the following
4450
02:41:00,000 --> 02:41:01,359
and then we're going to say increment
4451
02:41:01,359 --> 02:41:03,520
the value by plus one so how do we do
4452
02:41:03,520 --> 02:41:05,680
that let's do that right now
4453
02:41:05,680 --> 02:41:09,439
we're just going to say frequency
4454
02:41:11,359 --> 02:41:13,120
of that letter so we're creating a new
4455
02:41:13,120 --> 02:41:14,960
key on the fly
4456
02:41:14,960 --> 02:41:18,640
and we're gonna say equal to
4457
02:41:21,680 --> 02:41:25,120
whatever it was before
4458
02:41:25,200 --> 02:41:27,040
plus one
4459
02:41:27,040 --> 02:41:29,520
okay so it's like whatever frequency
4460
02:41:29,520 --> 02:41:33,040
of the letter previously was just like
4461
02:41:33,040 --> 02:41:35,279
incremented by one okay if it doesn't
4462
02:41:35,279 --> 02:41:38,960
exist which is the only other scenario
4463
02:41:38,960 --> 02:41:41,120
then what we can actually say
4464
02:41:41,120 --> 02:41:43,920
is we can go and say hey set the
4465
02:41:43,920 --> 02:41:45,760
frequency
4466
02:41:45,760 --> 02:41:47,200
of that letter
4467
02:41:47,200 --> 02:41:48,240
to one
4468
02:41:48,240 --> 02:41:49,840
that's it
4469
02:41:49,840 --> 02:41:52,000
and uh then we just return that
4470
02:41:52,000 --> 02:41:54,560
frequency okay so let's try it on this
4471
02:41:54,560 --> 02:41:55,680
word
4472
02:41:55,680 --> 02:41:59,040
haha and see what we get
4473
02:41:59,040 --> 02:42:02,720
and look at that we get h is 2 and a is
4474
02:42:02,720 --> 02:42:03,840
2.
4475
02:42:03,840 --> 02:42:06,319
that's crazy right like we just coded
4476
02:42:06,319 --> 02:42:08,560
that out from scratch and it works so
4477
02:42:08,560 --> 02:42:12,399
nicely and let's try
4478
02:42:12,399 --> 02:42:15,279
lol what are you
4479
02:42:15,279 --> 02:42:16,800
doing
4480
02:42:16,800 --> 02:42:19,279
later tonight lol
4481
02:42:19,279 --> 02:42:21,439
haha
4482
02:42:21,439 --> 02:42:22,479
okay
4483
02:42:22,479 --> 02:42:24,960
let's run this
4484
02:42:24,960 --> 02:42:26,800
and it just runs through every single
4485
02:42:26,800 --> 02:42:29,040
letter right here
4486
02:42:29,040 --> 02:42:32,720
and it says l happens about five times
4487
02:42:32,720 --> 02:42:34,399
is that true let's see
4488
02:42:34,399 --> 02:42:35,279
l
4489
02:42:35,279 --> 02:42:38,399
two so one two
4490
02:42:38,399 --> 02:42:41,200
and then we got
4491
02:42:41,200 --> 02:42:42,479
three
4492
02:42:42,479 --> 02:42:44,560
four five yep so that looks like it
4493
02:42:44,560 --> 02:42:46,479
happens five times and it says how many
4494
02:42:46,479 --> 02:42:48,640
times o happens and it says how many
4495
02:42:48,640 --> 02:42:50,720
times a comma happens how many spaces
4496
02:42:50,720 --> 02:42:52,000
there are
4497
02:42:52,000 --> 02:42:53,840
and it just shows you the whole freaking
4498
02:42:53,840 --> 02:42:55,600
thing right
4499
02:42:55,600 --> 02:42:56,479
so
4500
02:42:56,479 --> 02:42:59,359
frequency is working as expected now one
4501
02:42:59,359 --> 02:43:01,680
thing i'm going to show you is called
4502
02:43:01,680 --> 02:43:04,880
incr these are incremental operators
4503
02:43:04,880 --> 02:43:06,960
okay at least that's what i call them
4504
02:43:06,960 --> 02:43:10,160
and they are plus plus
4505
02:43:10,160 --> 02:43:12,160
minus minus
4506
02:43:12,160 --> 02:43:14,720
plus equals
4507
02:43:14,720 --> 02:43:16,080
okay
4508
02:43:16,080 --> 02:43:19,520
um plus equals one and so these are
4509
02:43:19,520 --> 02:43:21,279
incremental operators they're really
4510
02:43:21,279 --> 02:43:23,520
really helpful and make everything
4511
02:43:23,520 --> 02:43:26,640
shorter so for example this over here
4512
02:43:26,640 --> 02:43:29,279
you won't ever see it written like this
4513
02:43:29,279 --> 02:43:30,880
if somebody's coding this up they're
4514
02:43:30,880 --> 02:43:33,680
gonna write this like this
4515
02:43:33,680 --> 02:43:36,399
okay so it's gonna say plus equal 1. so
4516
02:43:36,399 --> 02:43:38,479
plus equal 1 is the same thing as
4517
02:43:38,479 --> 02:43:40,080
frequency letter
4518
02:43:40,080 --> 02:43:42,399
is equal to frequency letter plus 1. so
4519
02:43:42,399 --> 02:43:45,279
these two are equal and i just want you
4520
02:43:45,279 --> 02:43:47,600
to kind of get used to seeing them okay
4521
02:43:47,600 --> 02:43:49,680
and we can go and change
4522
02:43:49,680 --> 02:43:51,600
some of our stuff
4523
02:43:51,600 --> 02:43:53,439
above as well so like where it says
4524
02:43:53,439 --> 02:43:56,319
result equals result plus number
4525
02:43:56,319 --> 02:43:57,359
right
4526
02:43:57,359 --> 02:43:59,439
how could we change this here
4527
02:43:59,439 --> 02:44:01,200
try it on your own take about five
4528
02:44:01,200 --> 02:44:02,560
seconds and this you should be able to
4529
02:44:02,560 --> 02:44:04,399
do in five seconds five
4530
02:44:04,399 --> 02:44:06,880
four three two one
4531
02:44:06,880 --> 02:44:09,760
you could change this with a plus equal
4532
02:44:09,760 --> 02:44:10,880
all right
4533
02:44:10,880 --> 02:44:13,600
incrementing it like that okay
4534
02:44:13,600 --> 02:44:16,080
and if you're incrementing something by
4535
02:44:16,080 --> 02:44:18,880
one here's the cool thing you could also
4536
02:44:18,880 --> 02:44:19,920
just do
4537
02:44:19,920 --> 02:44:22,479
frequency plus plus
4538
02:44:22,479 --> 02:44:24,240
and that just this just means that
4539
02:44:24,240 --> 02:44:27,120
you're incrementing frequency by one so
4540
02:44:27,120 --> 02:44:28,800
let me try hitting run
4541
02:44:28,800 --> 02:44:31,279
you can see that this works right
4542
02:44:31,279 --> 02:44:33,840
you can hit one run on this and you can
4543
02:44:33,840 --> 02:44:35,439
see that this also works and gets the
4544
02:44:35,439 --> 02:44:36,720
same result
4545
02:44:36,720 --> 02:44:39,200
and we can you know
4546
02:44:39,200 --> 02:44:40,800
or you can go back to the oldest one and
4547
02:44:40,800 --> 02:44:42,720
see we get the same results so it's a
4548
02:44:42,720 --> 02:44:44,800
matter of preference you never want to
4549
02:44:44,800 --> 02:44:46,880
write it the long way
4550
02:44:46,880 --> 02:44:48,720
you won't ever see it written the long
4551
02:44:48,720 --> 02:44:50,479
way and you could write it in between
4552
02:44:50,479 --> 02:44:52,000
these two ways so you could go plus
4553
02:44:52,000 --> 02:44:53,760
equals one or you could go plus plus
4554
02:44:53,760 --> 02:44:54,960
either work
4555
02:44:54,960 --> 02:44:56,960
okay so i just thought you should know
4556
02:44:56,960 --> 02:45:00,000
those you could also go minus minus and
4557
02:45:00,000 --> 02:45:02,560
that will like decrement it
4558
02:45:02,560 --> 02:45:04,720
okay and um
4559
02:45:04,720 --> 02:45:05,680
you know
4560
02:45:05,680 --> 02:45:08,800
so i'll just leave this as plus equals
4561
02:45:08,800 --> 02:45:10,720
one for you just so you can wrap your
4562
02:45:10,720 --> 02:45:12,240
mind around this
4563
02:45:12,240 --> 02:45:14,640
and get used to seeing it okay so these
4564
02:45:14,640 --> 02:45:16,560
are incremental operators they're very
4565
02:45:16,560 --> 02:45:18,720
very useful and
4566
02:45:18,720 --> 02:45:21,200
very commonly used
4567
02:45:21,200 --> 02:45:22,479
now i'm going to remove all these
4568
02:45:22,479 --> 02:45:24,960
comments or i can just leave them here
4569
02:45:24,960 --> 02:45:27,040
if because you know this might
4570
02:45:27,040 --> 02:45:28,399
this might kind of
4571
02:45:28,399 --> 02:45:30,000
hurt your brain a little bit how's this
4572
02:45:30,000 --> 02:45:32,080
frequency thing working so i'm going to
4573
02:45:32,080 --> 02:45:34,080
leave that here for you all right now
4574
02:45:34,080 --> 02:45:35,840
let's just keep building on this and
4575
02:45:35,840 --> 02:45:38,000
let's keep taking it a step further so
4576
02:45:38,000 --> 02:45:40,720
now instead of letter frequency
4577
02:45:40,720 --> 02:45:41,680
let's
4578
02:45:41,680 --> 02:45:44,640
improve our our skills and and do
4579
02:45:44,640 --> 02:45:46,800
something called word frequency okay so
4580
02:45:46,800 --> 02:45:48,399
we're going to build a new one i'm going
4581
02:45:48,399 --> 02:45:50,000
to say const
4582
02:45:50,000 --> 02:45:51,600
word frequency
4583
02:45:51,600 --> 02:45:53,279
and see if you can try to challenge
4584
02:45:53,279 --> 02:45:57,439
yourself to take a stab at this phrase
4585
02:45:57,439 --> 02:45:59,600
and then we're gonna run this function
4586
02:45:59,600 --> 02:46:01,120
okay
4587
02:46:01,120 --> 02:46:03,840
and we're gonna do the similar similar
4588
02:46:03,840 --> 02:46:05,279
type of approach we're gonna create
4589
02:46:05,279 --> 02:46:07,920
frequency here as an empty object and
4590
02:46:07,920 --> 02:46:10,720
we're gonna return frequency at the end
4591
02:46:10,720 --> 02:46:12,240
all right
4592
02:46:12,240 --> 02:46:14,080
um and
4593
02:46:14,080 --> 02:46:16,640
inside of here we're gonna do our like
4594
02:46:16,640 --> 02:46:18,479
coding okay this is where we're gonna be
4595
02:46:18,479 --> 02:46:20,479
doing our logic now
4596
02:46:20,479 --> 02:46:23,200
here's the one interesting part here
4597
02:46:23,200 --> 02:46:25,200
i'm gonna comment this out
4598
02:46:25,200 --> 02:46:27,439
obviously let's comment that out let's
4599
02:46:27,439 --> 02:46:30,160
go ahead and console.log word frequency
4600
02:46:30,160 --> 02:46:31,920
to make sure it's running
4601
02:46:31,920 --> 02:46:34,160
and let's give it a phrase the phrase is
4602
02:46:34,160 --> 02:46:35,040
lol
4603
02:46:35,040 --> 02:46:37,439
what is going on
4604
02:46:37,439 --> 02:46:40,080
lol what
4605
02:46:40,080 --> 02:46:42,319
law let's just say that okay lol what
4606
02:46:42,319 --> 02:46:43,359
lol
4607
02:46:43,359 --> 02:46:45,680
so we know that if we run that here's
4608
02:46:45,680 --> 02:46:48,560
what we should get so
4609
02:46:48,560 --> 02:46:50,560
if i run this function so let's have an
4610
02:46:50,560 --> 02:46:52,479
example and this is also really good for
4611
02:46:52,479 --> 02:46:54,800
documentation because
4612
02:46:54,800 --> 02:46:57,200
by looking at this people can know
4613
02:46:57,200 --> 02:46:59,279
how to call your function and then what
4614
02:46:59,279 --> 02:47:01,279
your function actually returns at the
4615
02:47:01,279 --> 02:47:03,920
end so i'm going to say word frequency
4616
02:47:03,920 --> 02:47:05,680
and if i call this function with this
4617
02:47:05,680 --> 02:47:08,640
phrase law what law
4618
02:47:08,640 --> 02:47:11,920
it will return the following it'll say
4619
02:47:11,920 --> 02:47:14,399
lol
4620
02:47:14,880 --> 02:47:17,359
occurs twice
4621
02:47:17,359 --> 02:47:18,840
and
4622
02:47:18,840 --> 02:47:22,080
what occurs one time
4623
02:47:22,080 --> 02:47:25,040
okay just like that
4624
02:47:25,040 --> 02:47:28,240
and um this is what it should return
4625
02:47:28,240 --> 02:47:30,560
so how do we make it so it does that
4626
02:47:30,560 --> 02:47:32,240
well i wanna i have to teach you a
4627
02:47:32,240 --> 02:47:34,880
couple of things for this so say i have
4628
02:47:34,880 --> 02:47:36,240
a phrase
4629
02:47:36,240 --> 02:47:40,080
and let's go phrase is equal to lol what
4630
02:47:40,080 --> 02:47:41,200
lol
4631
02:47:41,200 --> 02:47:44,640
so what we would really love
4632
02:47:44,640 --> 02:47:48,319
for to happen is imagine we had an array
4633
02:47:48,319 --> 02:47:49,200
and
4634
02:47:49,200 --> 02:47:53,520
it had a it had things like this
4635
02:47:54,319 --> 02:47:56,800
that'd be really nice because if we loop
4636
02:47:56,800 --> 02:47:58,399
through something like that right in an
4637
02:47:58,399 --> 02:48:00,160
array it would just
4638
02:48:00,160 --> 02:48:02,240
loop through the words and we want to
4639
02:48:02,240 --> 02:48:04,080
loop through the words instead of
4640
02:48:04,080 --> 02:48:06,000
letters that's the only difference here
4641
02:48:06,000 --> 02:48:09,200
between last exercise and this exercise
4642
02:48:09,200 --> 02:48:10,640
okay so how
4643
02:48:10,640 --> 02:48:12,080
then the question is how do we get it
4644
02:48:12,080 --> 02:48:13,439
into an array so we can loop through it
4645
02:48:13,439 --> 02:48:14,240
because
4646
02:48:14,240 --> 02:48:15,600
if you try to loop through it in a
4647
02:48:15,600 --> 02:48:17,920
string watch what's gonna happen okay so
4648
02:48:17,920 --> 02:48:19,840
if you try to loop through in a string
4649
02:48:19,840 --> 02:48:21,920
it's gonna loop through and print out
4650
02:48:21,920 --> 02:48:24,080
every single letter from here
4651
02:48:24,080 --> 02:48:26,319
so let's try to write a loop and loop
4652
02:48:26,319 --> 02:48:28,840
through this so i'm gonna say
4653
02:48:28,840 --> 02:48:30,479
four
4654
02:48:30,479 --> 02:48:32,240
letter
4655
02:48:32,240 --> 02:48:34,960
in phrase
4656
02:48:37,040 --> 02:48:39,760
console.log letter
4657
02:48:39,760 --> 02:48:41,359
okay and we're gonna hit enter we're
4658
02:48:41,359 --> 02:48:43,680
gonna close the parentheses and run this
4659
02:48:43,680 --> 02:48:46,080
right and it's just gonna oh sorry not
4660
02:48:46,080 --> 02:48:48,960
not in let's run that loop again and say
4661
02:48:48,960 --> 02:48:51,359
of
4662
02:48:54,399 --> 02:48:56,720
of
4663
02:48:56,720 --> 02:48:58,240
and it's just going to print out every
4664
02:48:58,240 --> 02:48:59,439
single letter well that's going to be
4665
02:48:59,439 --> 02:49:01,120
confusing because it's going to
4666
02:49:01,120 --> 02:49:02,960
count letters for us and instead what we
4667
02:49:02,960 --> 02:49:04,640
want to do is we want to count words
4668
02:49:04,640 --> 02:49:07,200
entire words right and words in this
4669
02:49:07,200 --> 02:49:09,520
case are separated by what what's a
4670
02:49:09,520 --> 02:49:10,960
common thing all of these words are
4671
02:49:10,960 --> 02:49:13,920
separated by take i'll give you a hint
4672
02:49:13,920 --> 02:49:16,080
they're separated by
4673
02:49:16,080 --> 02:49:17,680
these spaces
4674
02:49:17,680 --> 02:49:18,560
okay
4675
02:49:18,560 --> 02:49:20,240
so we can do something cool in
4676
02:49:20,240 --> 02:49:22,080
javascript we can turn this into an
4677
02:49:22,080 --> 02:49:23,359
array
4678
02:49:23,359 --> 02:49:24,160
um
4679
02:49:24,160 --> 02:49:25,920
and if we turn this into an array so
4680
02:49:25,920 --> 02:49:27,680
let's say i give you phrase and if i
4681
02:49:27,680 --> 02:49:30,000
call dot this method got split on it
4682
02:49:30,000 --> 02:49:32,640
it's a string method and i say split it
4683
02:49:32,640 --> 02:49:34,640
by spaces
4684
02:49:34,640 --> 02:49:36,240
and i hit enter watch it what it's going
4685
02:49:36,240 --> 02:49:37,680
to do it's going to turn that into an
4686
02:49:37,680 --> 02:49:40,160
array and now that array very clearly
4687
02:49:40,160 --> 02:49:42,319
has three items and if i loop through
4688
02:49:42,319 --> 02:49:43,840
through these items watch what's going
4689
02:49:43,840 --> 02:49:45,840
to happen so
4690
02:49:45,840 --> 02:49:48,080
i'm just going to call it array
4691
02:49:48,080 --> 02:49:49,680
okay and then let's just loop through
4692
02:49:49,680 --> 02:49:51,359
there's a 4
4693
02:49:51,359 --> 02:49:53,680
word in array
4694
02:49:53,680 --> 02:49:55,200
console.log
4695
02:49:55,200 --> 02:49:56,319
word
4696
02:49:56,319 --> 02:49:58,319
i'm sorry i'm just used to python so
4697
02:49:58,319 --> 02:50:00,479
it's for of of
4698
02:50:00,479 --> 02:50:01,920
of of
4699
02:50:01,920 --> 02:50:04,240
so for word of array
4700
02:50:04,240 --> 02:50:06,640
yeah law what law so look at that i'm
4701
02:50:06,640 --> 02:50:09,279
actually looping through
4702
02:50:09,279 --> 02:50:12,319
the words in that array and i can now in
4703
02:50:12,319 --> 02:50:15,840
my object see if this word exists and if
4704
02:50:15,840 --> 02:50:18,560
it does exist then i can increment it by
4705
02:50:18,560 --> 02:50:21,200
one and if it doesn't exist then i can
4706
02:50:21,200 --> 02:50:23,520
just set it to one so it's the same
4707
02:50:23,520 --> 02:50:26,080
exact thing but just with words this
4708
02:50:26,080 --> 02:50:28,399
time and all you have to do is turn it
4709
02:50:28,399 --> 02:50:30,960
into an array of words and then you're
4710
02:50:30,960 --> 02:50:33,279
good so let's give it a try now
4711
02:50:33,279 --> 02:50:35,200
is what we know so we're gonna once we
4712
02:50:35,200 --> 02:50:36,800
get our phrase
4713
02:50:36,800 --> 02:50:38,240
okay what's the first thing we're gonna
4714
02:50:38,240 --> 02:50:39,920
do to it what's the first thing we're
4715
02:50:39,920 --> 02:50:41,760
gonna do to that phrase to turn it into
4716
02:50:41,760 --> 02:50:43,120
an array
4717
02:50:43,120 --> 02:50:46,640
we're gonna do phrase dot split
4718
02:50:46,640 --> 02:50:48,800
yeah this is going to turn it into an
4719
02:50:48,800 --> 02:50:52,000
array but we need it to be separated by
4720
02:50:52,000 --> 02:50:54,560
what it's separated by spaces in this
4721
02:50:54,560 --> 02:50:57,120
case okay
4722
02:50:57,120 --> 02:50:58,319
so boom
4723
02:50:58,319 --> 02:50:59,920
this is going to give us what we need so
4724
02:50:59,920 --> 02:51:01,120
now let's
4725
02:51:01,120 --> 02:51:04,240
let's go ahead and um
4726
02:51:04,240 --> 02:51:05,520
i'll do
4727
02:51:05,520 --> 02:51:07,920
let's just call this words
4728
02:51:07,920 --> 02:51:09,520
okay
4729
02:51:09,520 --> 02:51:10,880
and um
4730
02:51:10,880 --> 02:51:12,160
now
4731
02:51:12,160 --> 02:51:15,279
let's console.log
4732
02:51:15,439 --> 02:51:16,840
these
4733
02:51:16,840 --> 02:51:19,920
words and hit run and watch what happens
4734
02:51:19,920 --> 02:51:22,880
now i hit run and watch i got lol what
4735
02:51:22,880 --> 02:51:24,319
lol okay
4736
02:51:24,319 --> 02:51:27,359
if i have hello here and i run it
4737
02:51:27,359 --> 02:51:30,160
lol what lol hello yeah so now it's
4738
02:51:30,160 --> 02:51:32,720
doing what we want it to do now the
4739
02:51:32,720 --> 02:51:34,960
algorithm is going to be pretty much the
4740
02:51:34,960 --> 02:51:37,840
same as letter frequency
4741
02:51:37,840 --> 02:51:40,399
very very similar to that so we're going
4742
02:51:40,399 --> 02:51:43,520
to basically use like a similar
4743
02:51:43,520 --> 02:51:47,760
similar formula okay that we did here
4744
02:51:47,760 --> 02:51:49,600
so we're gonna say
4745
02:51:49,600 --> 02:51:51,840
we're already creating that frequency
4746
02:51:51,840 --> 02:51:53,359
object there yeah
4747
02:51:53,359 --> 02:51:56,080
and then we are returning it that's good
4748
02:51:56,080 --> 02:51:57,680
so we're doing that here too we're
4749
02:51:57,680 --> 02:51:59,279
creating it and we're returning it so
4750
02:51:59,279 --> 02:52:00,399
that's good
4751
02:52:00,399 --> 02:52:02,720
and we're gonna do the same thing so
4752
02:52:02,720 --> 02:52:05,840
we're gonna say for const word
4753
02:52:05,840 --> 02:52:08,399
of words
4754
02:52:08,399 --> 02:52:10,640
okay let's just console log the words
4755
02:52:10,640 --> 02:52:13,359
for now and let's just see
4756
02:52:13,359 --> 02:52:15,200
what it's console logging so let's run
4757
02:52:15,200 --> 02:52:16,080
it
4758
02:52:16,080 --> 02:52:17,680
and you can see that it goes lola what
4759
02:52:17,680 --> 02:52:19,279
law perfect is
4760
02:52:19,279 --> 02:52:21,439
every word and we're gonna do the same
4761
02:52:21,439 --> 02:52:23,120
thing check if it exists in the
4762
02:52:23,120 --> 02:52:25,040
frequency if
4763
02:52:25,040 --> 02:52:27,600
word and frequency
4764
02:52:27,600 --> 02:52:29,040
then
4765
02:52:29,040 --> 02:52:31,200
we're going to say
4766
02:52:31,200 --> 02:52:33,920
we're going to increment it by one
4767
02:52:33,920 --> 02:52:35,760
so we're going to go
4768
02:52:35,760 --> 02:52:37,439
frequency
4769
02:52:37,439 --> 02:52:38,560
word
4770
02:52:38,560 --> 02:52:41,279
plus equals one
4771
02:52:41,279 --> 02:52:43,359
else
4772
02:52:43,359 --> 02:52:44,960
frequency
4773
02:52:44,960 --> 02:52:46,560
word
4774
02:52:46,560 --> 02:52:48,479
set it to one
4775
02:52:48,479 --> 02:52:51,439
and at the end return frequency so let's
4776
02:52:51,439 --> 02:52:52,560
try this
4777
02:52:52,560 --> 02:52:55,359
and boom look at that lol is twice what
4778
02:52:55,359 --> 02:52:58,000
is one time and it's already working
4779
02:52:58,000 --> 02:53:00,399
right that's crazy so let's go
4780
02:53:00,399 --> 02:53:03,439
yo yo yo and let's run it and here we're
4781
02:53:03,439 --> 02:53:05,439
gonna see that yo is coming out three
4782
02:53:05,439 --> 02:53:07,279
times lol is coming out two times and
4783
02:53:07,279 --> 02:53:10,800
one is what is coming out one time so
4784
02:53:10,800 --> 02:53:12,720
i mean this is so cool we just made it
4785
02:53:12,720 --> 02:53:13,680
but
4786
02:53:13,680 --> 02:53:16,080
there's something we can do to make it
4787
02:53:16,080 --> 02:53:17,520
even better
4788
02:53:17,520 --> 02:53:19,439
i want you to notice the similarities
4789
02:53:19,439 --> 02:53:21,439
between this
4790
02:53:21,439 --> 02:53:22,840
and the letter
4791
02:53:22,840 --> 02:53:25,600
frequency thing that we wrote yeah and
4792
02:53:25,600 --> 02:53:27,680
instead of calling it letter frequency
4793
02:53:27,680 --> 02:53:29,680
let's just call it i mean actually let's
4794
02:53:29,680 --> 02:53:32,720
leave it as letter frequency that's fine
4795
02:53:32,720 --> 02:53:34,720
but they're very similar right the code
4796
02:53:34,720 --> 02:53:37,359
here is very very similar to the code we
4797
02:53:37,359 --> 02:53:39,920
have here for example
4798
02:53:39,920 --> 02:53:42,160
we're creating a frequency object we're
4799
02:53:42,160 --> 02:53:44,240
creating a frequency object
4800
02:53:44,240 --> 02:53:46,720
we're returning frequency
4801
02:53:46,720 --> 02:53:48,960
we're returning frequency
4802
02:53:48,960 --> 02:53:49,840
right
4803
02:53:49,840 --> 02:53:50,800
we're
4804
02:53:50,800 --> 02:53:52,880
we have a loop that we go through we
4805
02:53:52,880 --> 02:53:56,160
have a loop that we go through
4806
02:53:56,160 --> 02:53:58,160
if it's detected in the frequency
4807
02:53:58,160 --> 02:53:59,920
incremented by one
4808
02:53:59,920 --> 02:54:01,520
if it's detected in frequency
4809
02:54:01,520 --> 02:54:03,920
incremented by one
4810
02:54:03,920 --> 02:54:06,240
if it's not detected set it to one if
4811
02:54:06,240 --> 02:54:08,800
it's not detected set it to one i mean
4812
02:54:08,800 --> 02:54:11,200
my gosh there must be something we could
4813
02:54:11,200 --> 02:54:12,479
do because
4814
02:54:12,479 --> 02:54:14,720
it looks pretty much like the same exact
4815
02:54:14,720 --> 02:54:15,840
thing
4816
02:54:15,840 --> 02:54:17,680
and watch there's something brilliant
4817
02:54:17,680 --> 02:54:20,080
that we can do so we can actually delete
4818
02:54:20,080 --> 02:54:23,279
this entire for loop
4819
02:54:23,279 --> 02:54:25,760
let's get rid of this console log
4820
02:54:25,760 --> 02:54:27,760
we can delete this
4821
02:54:27,760 --> 02:54:31,120
let's get rid of this return here
4822
02:54:31,120 --> 02:54:35,040
let's get rid of this frequency here
4823
02:54:35,040 --> 02:54:37,520
and the only new thing we need in this
4824
02:54:37,520 --> 02:54:39,120
is we just need to
4825
02:54:39,120 --> 02:54:41,120
split it by
4826
02:54:41,120 --> 02:54:44,160
uh spaces to break it into words
4827
02:54:44,160 --> 02:54:46,960
right and let's go const words
4828
02:54:46,960 --> 02:54:48,399
sorry i keep forgetting the cons
4829
02:54:48,399 --> 02:54:50,880
sometimes
4830
02:54:51,439 --> 02:54:53,279
and all we do here
4831
02:54:53,279 --> 02:54:56,560
okay this is going to be so beautiful
4832
02:54:56,560 --> 02:54:58,319
is
4833
02:54:58,319 --> 02:55:00,479
we call our letter frequency function
4834
02:55:00,479 --> 02:55:02,720
that we created earlier and we just pass
4835
02:55:02,720 --> 02:55:04,080
it words
4836
02:55:04,080 --> 02:55:07,120
and then ill know what to do with it
4837
02:55:07,120 --> 02:55:08,880
so it goes in here
4838
02:55:08,880 --> 02:55:10,560
and then it has instructions for what we
4839
02:55:10,560 --> 02:55:11,920
need to do
4840
02:55:11,920 --> 02:55:14,160
right and all we need to do here is just
4841
02:55:14,160 --> 02:55:16,160
say return
4842
02:55:16,160 --> 02:55:18,240
so let's try running this
4843
02:55:18,240 --> 02:55:21,279
and look at that you guys we are getting
4844
02:55:21,279 --> 02:55:23,680
the same exact answer and it is
4845
02:55:23,680 --> 02:55:26,720
completely correct yo is three times
4846
02:55:26,720 --> 02:55:30,800
what is once lol is two times okay
4847
02:55:30,800 --> 02:55:31,840
now
4848
02:55:31,840 --> 02:55:34,560
this is if this is not blowing your mind
4849
02:55:34,560 --> 02:55:36,160
think about it like this when you go to
4850
02:55:36,160 --> 02:55:39,920
google right and you type in kanye west
4851
02:55:39,920 --> 02:55:42,479
and it starts auto completing it's using
4852
02:55:42,479 --> 02:55:45,279
the same thing because it knows
4853
02:55:45,279 --> 02:55:47,120
what are the things that are being
4854
02:55:47,120 --> 02:55:49,359
searched frequently
4855
02:55:49,359 --> 02:55:51,040
and because it knows their frequency
4856
02:55:51,040 --> 02:55:53,200
it's showing them here so for example
4857
02:55:53,200 --> 02:55:55,600
maybe this search over here
4858
02:55:55,600 --> 02:55:57,760
right this kanye west runway
4859
02:55:57,760 --> 02:56:00,800
that might be done let's say 10 times
4860
02:56:00,800 --> 02:56:04,240
kanye west net worth
4861
02:56:04,240 --> 02:56:06,240
might be done five times
4862
02:56:06,240 --> 02:56:08,160
kanye west's girlfriend might be
4863
02:56:08,160 --> 02:56:09,920
searched three times
4864
02:56:09,920 --> 02:56:13,279
okay it's just an object and then google
4865
02:56:13,279 --> 02:56:15,680
is sorting it and showing you based on
4866
02:56:15,680 --> 02:56:18,560
the ones that were the most frequent
4867
02:56:18,560 --> 02:56:20,800
that's the really cool part and this is
4868
02:56:20,800 --> 02:56:21,680
why
4869
02:56:21,680 --> 02:56:23,840
you know once you learn this here all
4870
02:56:23,840 --> 02:56:25,359
you need to do is be able to attach a
4871
02:56:25,359 --> 02:56:27,359
front end like this and boom your apps
4872
02:56:27,359 --> 02:56:29,040
are going to be absolutely amazing and
4873
02:56:29,040 --> 02:56:31,760
mind-blowing so let's try this now okay
4874
02:56:31,760 --> 02:56:33,680
let's try our own function with
4875
02:56:33,680 --> 02:56:36,240
different inputs we're going to say
4876
02:56:36,240 --> 02:56:40,960
const user input is prompt
4877
02:56:40,960 --> 02:56:43,279
uh write your
4878
02:56:43,279 --> 02:56:45,279
sentence
4879
02:56:45,279 --> 02:56:48,479
right and we're gonna replace this with
4880
02:56:48,479 --> 02:56:50,240
whatever the user types in so we're
4881
02:56:50,240 --> 02:56:52,560
gonna say user input here
4882
02:56:52,560 --> 02:56:54,720
and call this and um
4883
02:56:54,720 --> 02:56:56,399
yeah let's go ahead and run it write
4884
02:56:56,399 --> 02:56:59,920
your sentence and we're just gonna say
4885
02:57:00,000 --> 02:57:00,430
um
4886
02:57:00,430 --> 02:57:01,920
[Music]
4887
02:57:01,920 --> 02:57:03,760
kanye
4888
02:57:03,760 --> 02:57:05,920
west
4889
02:57:05,920 --> 02:57:09,800
has great music
4890
02:57:10,640 --> 02:57:12,479
but west side
4891
02:57:12,479 --> 02:57:14,720
is
4892
02:57:15,120 --> 02:57:16,880
greater
4893
02:57:16,880 --> 02:57:18,080
than
4894
02:57:18,080 --> 02:57:19,120
greater
4895
02:57:19,120 --> 02:57:22,160
than east side yeah because i live on
4896
02:57:22,160 --> 02:57:23,760
the west coast in california and then
4897
02:57:23,760 --> 02:57:26,479
you got all these people in new york and
4898
02:57:26,479 --> 02:57:29,120
i like that my time gets back
4899
02:57:29,120 --> 02:57:31,520
i get my three hours back when i come to
4900
02:57:31,520 --> 02:57:33,760
california instead of losing my three
4901
02:57:33,760 --> 02:57:36,560
hours when i go to new york so
4902
02:57:36,560 --> 02:57:38,880
if you're in new york
4903
02:57:38,880 --> 02:57:41,120
and you want to earn three hours
4904
02:57:41,120 --> 02:57:43,359
back of your life forever come to
4905
02:57:43,359 --> 02:57:45,840
california
4906
02:57:45,840 --> 02:57:48,080
so let's try this
4907
02:57:48,080 --> 02:57:50,319
yeah and um
4908
02:57:50,319 --> 02:57:52,080
probably the only thing that occurs
4909
02:57:52,080 --> 02:57:53,840
twice is
4910
02:57:53,840 --> 02:57:56,160
west i imagine okay
4911
02:57:56,160 --> 02:57:59,600
and then let's just write kanye is also
4912
02:57:59,600 --> 02:58:01,040
the goat
4913
02:58:01,040 --> 02:58:03,200
the goat
4914
02:58:03,200 --> 02:58:05,520
that's not gonna work the goat okay ella
4915
02:58:05,520 --> 02:58:07,359
said the goat
4916
02:58:07,359 --> 02:58:10,720
and let's hit okay and run it and uh it
4917
02:58:10,720 --> 02:58:12,080
came back
4918
02:58:12,080 --> 02:58:14,240
all right and it broke it down
4919
02:58:14,240 --> 02:58:16,160
and you can see it did a full breakdown
4920
02:58:16,160 --> 02:58:18,800
of every single word kanye is two west
4921
02:58:18,800 --> 02:58:19,840
is two
4922
02:58:19,840 --> 02:58:22,720
has is one great is one music is one but
4923
02:58:22,720 --> 02:58:25,359
is one side is one is two
4924
02:58:25,359 --> 02:58:28,479
and the goat is two thus two and it did
4925
02:58:28,479 --> 02:58:31,359
it all so fast and so quick
4926
02:58:31,359 --> 02:58:34,479
all right so we did a lot of
4927
02:58:34,479 --> 02:58:36,960
challenges and exercises and if you want
4928
02:58:36,960 --> 02:58:40,000
to deep dive and do like a full on boot
4929
02:58:40,000 --> 02:58:42,640
camp and master arrays
4930
02:58:42,640 --> 02:58:45,359
and objects then i recommend you
4931
02:58:45,359 --> 02:58:48,960
deep dive into exercises okay where
4932
02:58:48,960 --> 02:58:50,080
we'll have
4933
02:58:50,080 --> 02:58:53,600
additional exercises for you and lance
4934
02:58:53,600 --> 02:58:56,080
he is going to be your boot camp
4935
02:58:56,080 --> 02:58:57,680
instructor for those okay so if you want
4936
02:58:57,680 --> 02:59:00,399
to become a like a legend he's going to
4937
02:59:00,399 --> 02:59:02,479
help you turn into a black belt my job
4938
02:59:02,479 --> 02:59:04,080
is to take you from white belt turning
4939
02:59:04,080 --> 02:59:06,479
into a blue belt he will do that so if
4940
02:59:06,479 --> 02:59:08,479
you want to for example you know because
4941
02:59:08,479 --> 02:59:10,800
i'm going to move on to different topics
4942
02:59:10,800 --> 02:59:12,560
now but if you want to just keep
4943
02:59:12,560 --> 02:59:14,399
exercising this
4944
02:59:14,399 --> 02:59:15,760
you can do that
4945
02:59:15,760 --> 02:59:17,840
you can do that by going into say this
4946
02:59:17,840 --> 02:59:20,560
exercises folder on the left hand side
4947
02:59:20,560 --> 02:59:21,760
by the time you're watching this
4948
02:59:21,760 --> 02:59:23,359
tutorial we'll have bunch of more
4949
02:59:23,359 --> 02:59:25,120
exercises right now we have this one
4950
02:59:25,120 --> 02:59:27,200
here that says convert convert hours to
4951
02:59:27,200 --> 02:59:30,240
seconds you click that okay and then
4952
02:59:30,240 --> 02:59:33,040
lance has this exercise prepared for you
4953
02:59:33,040 --> 02:59:35,439
there's a rebel that you'll have to copy
4954
02:59:35,439 --> 02:59:38,640
okay and paste in your urls and you'll
4955
02:59:38,640 --> 02:59:41,120
be able to fork this and be able to code
4956
02:59:41,120 --> 02:59:42,800
it on your own so this is the rupple
4957
02:59:42,800 --> 02:59:44,720
that lance has created and if you want
4958
02:59:44,720 --> 02:59:46,720
to be able to do it on your own hit fork
4959
02:59:46,720 --> 02:59:48,560
at the top okay
4960
02:59:48,560 --> 02:59:52,319
and then you can actually run this
4961
02:59:52,319 --> 02:59:54,399
uh rebel
4962
02:59:54,399 --> 02:59:55,359
okay
4963
02:59:55,359 --> 02:59:57,200
and uh you can write your code here and
4964
02:59:57,200 --> 02:59:58,880
you could write your solution here okay
4965
02:59:58,880 --> 03:00:00,000
and
4966
03:00:00,000 --> 03:00:01,840
what lance will also be doing with some
4967
03:00:01,840 --> 03:00:03,600
of these exercises he'll also have a
4968
03:00:03,600 --> 03:00:05,840
link at the top of the loom video where
4969
03:00:05,840 --> 03:00:07,439
you'll be able to click and watch his
4970
03:00:07,439 --> 03:00:09,680
video explaining that exercise
4971
03:00:09,680 --> 03:00:11,680
explaining that project in detail as
4972
03:00:11,680 --> 03:00:13,600
well okay so
4973
03:00:13,600 --> 03:00:15,840
think of him as a
4974
03:00:15,840 --> 03:00:17,040
solid
4975
03:00:17,040 --> 03:00:18,640
um you know
4976
03:00:18,640 --> 03:00:20,640
boot camp instructor that just like kind
4977
03:00:20,640 --> 03:00:22,800
of dives deep in with you all right so
4978
03:00:22,800 --> 03:00:24,800
that those are going to be the extra
4979
03:00:24,800 --> 03:00:27,120
resources available to you with that
4980
03:00:27,120 --> 03:00:28,080
said
4981
03:00:28,080 --> 03:00:30,000
with that said
4982
03:00:30,000 --> 03:00:32,319
you're doing amazing
4983
03:00:32,319 --> 03:00:34,399
pat yourself on the back i mean wow
4984
03:00:34,399 --> 03:00:37,040
you're doing an excellent job so far by
4985
03:00:37,040 --> 03:00:38,880
being here you're putting in the work
4986
03:00:38,880 --> 03:00:40,240
you're putting in the effort i mean you
4987
03:00:40,240 --> 03:00:41,520
could be doing so many things you could
4988
03:00:41,520 --> 03:00:43,359
be playing with your kids right now you
4989
03:00:43,359 --> 03:00:44,240
could be
4990
03:00:44,240 --> 03:00:47,040
outside hanging out with your friends
4991
03:00:47,040 --> 03:00:48,000
you know
4992
03:00:48,000 --> 03:00:49,920
you probably got off of work and you're
4993
03:00:49,920 --> 03:00:51,520
putting in that time to learn this i
4994
03:00:51,520 --> 03:00:53,439
mean that's huge dedication you're
4995
03:00:53,439 --> 03:00:55,600
taking time away from all this other
4996
03:00:55,600 --> 03:00:56,960
stuff you could be doing and doing this
4997
03:00:56,960 --> 03:00:59,920
so now granted coding is a lot of fun
4998
03:00:59,920 --> 03:01:01,200
but if you're in this stage i know
4999
03:01:01,200 --> 03:01:04,160
you're doing it for learning and i tip
5000
03:01:04,160 --> 03:01:04,960
my
5001
03:01:04,960 --> 03:01:06,880
proverbial hat to you
5002
03:01:06,880 --> 03:01:10,560
um huge props for making it this far
5003
03:01:10,560 --> 03:01:12,880
with that said let's keep on rocking and
5004
03:01:12,880 --> 03:01:14,880
rolling all right now we're gonna move
5005
03:01:14,880 --> 03:01:16,640
on to something really fun they're
5006
03:01:16,640 --> 03:01:20,160
called higher order functions don't let
5007
03:01:20,160 --> 03:01:22,960
the name confuse you or freak you out
5008
03:01:22,960 --> 03:01:24,880
they're very very easy i swear once you
5009
03:01:24,880 --> 03:01:26,240
see them a couple of times pattern
5010
03:01:26,240 --> 03:01:28,880
recognition maybe and then it's so fast
5011
03:01:28,880 --> 03:01:31,279
it's so easy so let's get to it so there
5012
03:01:31,279 --> 03:01:33,200
are a couple of higher order functions
5013
03:01:33,200 --> 03:01:34,479
i'm going to show you okay so higher
5014
03:01:34,479 --> 03:01:36,399
order functions and i'm going to show
5015
03:01:36,399 --> 03:01:39,279
you map and i will show you filter map
5016
03:01:39,279 --> 03:01:41,920
is like one of the most important things
5017
03:01:41,920 --> 03:01:43,439
that you should use
5018
03:01:43,439 --> 03:01:47,279
uh i'll also show you reduce okay and uh
5019
03:01:47,279 --> 03:01:49,840
map will make your life so much easier
5020
03:01:49,840 --> 03:01:51,920
like you'll be using map so much and
5021
03:01:51,920 --> 03:01:53,680
then you'll also use filter a lot when
5022
03:01:53,680 --> 03:01:55,520
you're writing javascript code so let's
5023
03:01:55,520 --> 03:01:57,600
go ahead and comment out these lines
5024
03:01:57,600 --> 03:01:59,200
here because we don't need them right
5025
03:01:59,200 --> 03:02:03,200
now and let's just get started okay so
5026
03:02:03,200 --> 03:02:06,399
we want to use let's start with doing
5027
03:02:06,399 --> 03:02:08,319
some of these functions again but
5028
03:02:08,319 --> 03:02:11,120
instead we're actually going to use map
5029
03:02:11,120 --> 03:02:12,720
so let's say we want to double our
5030
03:02:12,720 --> 03:02:14,880
numbers right any number that we give it
5031
03:02:14,880 --> 03:02:17,040
watch how easy it is so say i have an
5032
03:02:17,040 --> 03:02:19,040
array i can
5033
03:02:19,040 --> 03:02:20,319
loop through this
5034
03:02:20,319 --> 03:02:22,880
using dot map and i can say map and give
5035
03:02:22,880 --> 03:02:25,279
me a number and for each number i'm just
5036
03:02:25,279 --> 03:02:27,040
going to console.log it so watch what
5037
03:02:27,040 --> 03:02:29,600
happens let's run this and boom there i
5038
03:02:29,600 --> 03:02:31,840
have my loop like look at how clean it
5039
03:02:31,840 --> 03:02:34,640
is it's just one line right no blocks of
5040
03:02:34,640 --> 03:02:36,399
code no weird squigglies or anything
5041
03:02:36,399 --> 03:02:38,479
like that and what i'm what i'm saying
5042
03:02:38,479 --> 03:02:39,439
is
5043
03:02:39,439 --> 03:02:41,520
map through this i mean literally think
5044
03:02:41,520 --> 03:02:43,279
of it like loop through this like it's
5045
03:02:43,279 --> 03:02:46,319
literally like dot loop and then each
5046
03:02:46,319 --> 03:02:48,160
variable let's we'll just call it number
5047
03:02:48,160 --> 03:02:49,920
so like this will be a variable called
5048
03:02:49,920 --> 03:02:51,680
number then this will be number and this
5049
03:02:51,680 --> 03:02:53,279
will be number and each time what i want
5050
03:02:53,279 --> 03:02:56,000
you to do is just print it out so
5051
03:02:56,000 --> 03:02:58,560
i'm returning this right away but if you
5052
03:02:58,560 --> 03:03:00,399
wanted to write a full function here you
5053
03:03:00,399 --> 03:03:02,399
could just do this put open close
5054
03:03:02,399 --> 03:03:04,640
squigglies and like write whatever you
5055
03:03:04,640 --> 03:03:06,640
want here okay you could write multiple
5056
03:03:06,640 --> 03:03:08,640
lines here and when i run this you'd see
5057
03:03:08,640 --> 03:03:11,200
it's like one haha two haha three haha
5058
03:03:11,200 --> 03:03:13,120
for haha like so on and so forth so
5059
03:03:13,120 --> 03:03:15,120
let's remove this now we're doing an
5060
03:03:15,120 --> 03:03:18,800
implicit return implicit return by not
5061
03:03:18,800 --> 03:03:20,880
having a squiggly braces here we don't
5062
03:03:20,880 --> 03:03:22,080
even need to say return it's
5063
03:03:22,080 --> 03:03:25,200
automatically returning it okay now
5064
03:03:25,200 --> 03:03:27,840
i want you to take a look at this how
5065
03:03:27,840 --> 03:03:30,000
could we double all of these numbers
5066
03:03:30,000 --> 03:03:32,160
here watch how easy it is i could just
5067
03:03:32,160 --> 03:03:34,960
say number times two and watch what
5068
03:03:34,960 --> 03:03:37,200
happens so let's just go
5069
03:03:37,200 --> 03:03:39,680
let result is equal to that and we'll
5070
03:03:39,680 --> 03:03:42,640
console log result
5071
03:03:42,640 --> 03:03:44,319
okay let's run this
5072
03:03:44,319 --> 03:03:46,960
and look at that you guys like that is
5073
03:03:46,960 --> 03:03:50,640
so clean that is so nice i mean it is
5074
03:03:50,640 --> 03:03:52,560
the easiest thing right in the world and
5075
03:03:52,560 --> 03:03:54,960
it just boom two four six eight and all
5076
03:03:54,960 --> 03:03:56,720
i did is i just said number and multiply
5077
03:03:56,720 --> 03:03:58,880
that by two now here's also what's cool
5078
03:03:58,880 --> 03:04:00,720
about map so whenever you have to write
5079
03:04:00,720 --> 03:04:02,399
loops pretty much just forget about them
5080
03:04:02,399 --> 03:04:04,240
and you'll be using maps like most of
5081
03:04:04,240 --> 03:04:06,000
the times in javascript most of the
5082
03:04:06,000 --> 03:04:08,240
times and what's cool about map is it's
5083
03:04:08,240 --> 03:04:11,359
also returning to you an array okay so
5084
03:04:11,359 --> 03:04:12,399
map
5085
03:04:12,399 --> 03:04:14,000
returns
5086
03:04:14,000 --> 03:04:16,880
so it loops and returns
5087
03:04:16,880 --> 03:04:18,960
an array so remember when you have to
5088
03:04:18,960 --> 03:04:21,359
like make this result thing
5089
03:04:21,359 --> 03:04:23,920
so for example when we were doubling our
5090
03:04:23,920 --> 03:04:25,600
numbers right we had to make this result
5091
03:04:25,600 --> 03:04:28,000
and this temporary array then we had to
5092
03:04:28,000 --> 03:04:30,240
loop through and push to that array and
5093
03:04:30,240 --> 03:04:32,319
then return that array well what's
5094
03:04:32,319 --> 03:04:33,840
amazing about
5095
03:04:33,840 --> 03:04:35,200
map is
5096
03:04:35,200 --> 03:04:37,439
it's already doing that it just goes
5097
03:04:37,439 --> 03:04:38,640
through and doubles it it's
5098
03:04:38,640 --> 03:04:41,040
automatically pushing it to an array
5099
03:04:41,040 --> 03:04:42,880
right and then it's returning that array
5100
03:04:42,880 --> 03:04:44,000
at the end
5101
03:04:44,000 --> 03:04:46,160
so that's what makes map so freaking
5102
03:04:46,160 --> 03:04:48,880
awesome and all of our like code that we
5103
03:04:48,880 --> 03:04:50,479
have written before we could just like
5104
03:04:50,479 --> 03:04:52,880
replace it right so let's let's rewrite
5105
03:04:52,880 --> 03:04:54,800
our double functions let's say const
5106
03:04:54,800 --> 03:04:56,160
double map
5107
03:04:56,160 --> 03:04:57,120
and
5108
03:04:57,120 --> 03:04:59,200
it takes in numbers
5109
03:04:59,200 --> 03:05:01,920
okay and what this does
5110
03:05:01,920 --> 03:05:05,200
is it will take numbers so we'll do
5111
03:05:05,200 --> 03:05:08,080
return numbers dot
5112
03:05:08,080 --> 03:05:09,279
map
5113
03:05:09,279 --> 03:05:12,000
and for each number just multiply it by
5114
03:05:12,000 --> 03:05:12,880
two
5115
03:05:12,880 --> 03:05:15,040
okay so that's it
5116
03:05:15,040 --> 03:05:16,960
that's our entire
5117
03:05:16,960 --> 03:05:19,680
function so compare this double here
5118
03:05:19,680 --> 03:05:21,520
with how many lines of code to this
5119
03:05:21,520 --> 03:05:23,439
double here with one line of code this
5120
03:05:23,439 --> 03:05:26,160
is so much cleaner and so let's delete
5121
03:05:26,160 --> 03:05:27,120
that
5122
03:05:27,120 --> 03:05:29,120
and let's try running
5123
03:05:29,120 --> 03:05:31,040
double map
5124
03:05:31,040 --> 03:05:33,520
and give it an array we'll say one two
5125
03:05:33,520 --> 03:05:35,760
three and we should get
5126
03:05:35,760 --> 03:05:37,600
two four six
5127
03:05:37,600 --> 03:05:39,760
boom look at that we got an array of two
5128
03:05:39,760 --> 03:05:42,000
four six so this is a much nicer way of
5129
03:05:42,000 --> 03:05:43,439
writing it so i
5130
03:05:43,439 --> 03:05:46,319
try turning some of these okay for
5131
03:05:46,319 --> 03:05:47,520
example
5132
03:05:47,520 --> 03:05:50,399
you know some of these functions into
5133
03:05:50,399 --> 03:05:52,000
using map
5134
03:05:52,000 --> 03:05:53,200
or
5135
03:05:53,200 --> 03:05:55,120
using filter and see if you could do
5136
03:05:55,120 --> 03:05:57,359
that all right now what i'm going to do
5137
03:05:57,359 --> 03:05:59,359
is before we get into the exercises and
5138
03:05:59,359 --> 03:06:01,120
remember for exercises if you need
5139
03:06:01,120 --> 03:06:03,040
exercise there'll be an exercise folder
5140
03:06:03,040 --> 03:06:04,000
for you
5141
03:06:04,000 --> 03:06:05,840
me or lance will put together these
5142
03:06:05,840 --> 03:06:07,439
exercises for you and you can always
5143
03:06:07,439 --> 03:06:09,680
kind of access them from here and
5144
03:06:09,680 --> 03:06:11,439
exercise okay and by the time you look
5145
03:06:11,439 --> 03:06:14,080
at this it'll be much more fleshed out
5146
03:06:14,080 --> 03:06:15,120
so
5147
03:06:15,120 --> 03:06:17,600
i showed you an example of how to use
5148
03:06:17,600 --> 03:06:18,479
map
5149
03:06:18,479 --> 03:06:20,800
so this is
5150
03:06:20,800 --> 03:06:22,240
map
5151
03:06:22,240 --> 03:06:24,000
all right now i'm going to show you how
5152
03:06:24,000 --> 03:06:26,160
to use filter okay that's another
5153
03:06:26,160 --> 03:06:29,680
important one so say you have numbers
5154
03:06:29,680 --> 03:06:30,640
like
5155
03:06:30,640 --> 03:06:33,520
one two three four five six
5156
03:06:33,520 --> 03:06:35,920
well in order to be able to go through
5157
03:06:35,920 --> 03:06:37,680
these numbers i mean
5158
03:06:37,680 --> 03:06:39,680
and and let's say i want you to only
5159
03:06:39,680 --> 03:06:42,560
give me numbers that are greater than
5160
03:06:42,560 --> 03:06:44,240
three how are you gonna do that right
5161
03:06:44,240 --> 03:06:45,040
now
5162
03:06:45,040 --> 03:06:46,880
let's create our own filters we're gonna
5163
03:06:46,880 --> 03:06:51,359
say const filter and it takes in numbers
5164
03:06:51,359 --> 03:06:54,319
and it takes in a filter number as well
5165
03:06:54,319 --> 03:06:55,359
so like
5166
03:06:55,359 --> 03:06:57,680
should be greater than or something like
5167
03:06:57,680 --> 03:06:59,520
this okay so we have to give it a few
5168
03:06:59,520 --> 03:07:01,279
things we need to give it numbers and we
5169
03:07:01,279 --> 03:07:02,399
need to give it like it should be
5170
03:07:02,399 --> 03:07:04,160
greater than 4 or greater than 5 or
5171
03:07:04,160 --> 03:07:06,240
greater than 3 something like that and
5172
03:07:06,240 --> 03:07:08,800
then how will it work well
5173
03:07:08,800 --> 03:07:10,720
we will need to have an empty array to
5174
03:07:10,720 --> 03:07:12,720
start off with and then what we need to
5175
03:07:12,720 --> 03:07:14,560
do is go hey is
5176
03:07:14,560 --> 03:07:17,200
one greater than this number here
5177
03:07:17,200 --> 03:07:18,560
and
5178
03:07:18,560 --> 03:07:20,319
if one
5179
03:07:20,319 --> 03:07:22,319
is greater than this
5180
03:07:22,319 --> 03:07:24,160
then throw it in that bucket but if it's
5181
03:07:24,160 --> 03:07:26,160
not let's say we have we chose 3 to be
5182
03:07:26,160 --> 03:07:28,720
our number here so 1 is not greater than
5183
03:07:28,720 --> 03:07:30,880
3 right so then it shouldn't do anything
5184
03:07:30,880 --> 03:07:32,800
with it and cross it off
5185
03:07:32,800 --> 03:07:35,439
then we go here is 2 greater than 3 nope
5186
03:07:35,439 --> 03:07:38,160
is 3 greater than 3 nope is four greater
5187
03:07:38,160 --> 03:07:40,720
than three yes so then you get four it's
5188
03:07:40,720 --> 03:07:43,120
five greater than three then you get yes
5189
03:07:43,120 --> 03:07:45,680
is six greater than three yes
5190
03:07:45,680 --> 03:07:48,160
right so you pop those numbers in there
5191
03:07:48,160 --> 03:07:49,680
but besides those you don't so then you
5192
03:07:49,680 --> 03:07:52,720
get this filtered array okay so if you
5193
03:07:52,720 --> 03:07:54,240
run filter
5194
03:07:54,240 --> 03:07:56,479
and let's say we give it
5195
03:07:56,479 --> 03:07:59,279
uh numbers let's say we give it a
5196
03:07:59,279 --> 03:08:01,279
you know one two
5197
03:08:01,279 --> 03:08:02,399
three
5198
03:08:02,399 --> 03:08:03,920
four five six
5199
03:08:03,920 --> 03:08:05,760
and then we say it has to be greater
5200
03:08:05,760 --> 03:08:06,960
than three
5201
03:08:06,960 --> 03:08:09,439
well then what should filter return
5202
03:08:09,439 --> 03:08:11,120
it should return
5203
03:08:11,120 --> 03:08:12,800
four five six that's what it should
5204
03:08:12,800 --> 03:08:14,800
return so see if you could
5205
03:08:14,800 --> 03:08:17,359
write this function on your own okay so
5206
03:08:17,359 --> 03:08:19,120
i'm gonna give you five seconds to pause
5207
03:08:19,120 --> 03:08:21,359
the video and try giving this a shot on
5208
03:08:21,359 --> 03:08:26,000
your own five four three two one
5209
03:08:26,000 --> 03:08:27,840
all right let's do this function
5210
03:08:27,840 --> 03:08:29,760
together
5211
03:08:29,760 --> 03:08:31,760
so i have filter here
5212
03:08:31,760 --> 03:08:32,560
now
5213
03:08:32,560 --> 03:08:36,160
what we can do is um we have our numbers
5214
03:08:36,160 --> 03:08:38,399
we have what what it needs to be greater
5215
03:08:38,399 --> 03:08:39,279
than
5216
03:08:39,279 --> 03:08:40,560
so we're going to start off with an
5217
03:08:40,560 --> 03:08:42,319
empty array so we're going to say result
5218
03:08:42,319 --> 03:08:44,319
equal that empty array let's actually
5219
03:08:44,319 --> 03:08:46,240
write our for loop first so let's return
5220
03:08:46,240 --> 03:08:48,160
the result and let's write our for loop
5221
03:08:48,160 --> 03:08:50,080
so we're going to say 4
5222
03:08:50,080 --> 03:08:51,600
number of
5223
03:08:51,600 --> 03:08:54,000
const number of numbers
5224
03:08:54,000 --> 03:08:56,479
i want you to do the following
5225
03:08:56,479 --> 03:08:57,920
if
5226
03:08:57,920 --> 03:08:59,120
number
5227
03:08:59,120 --> 03:09:01,200
is greater than
5228
03:09:01,200 --> 03:09:03,040
this number
5229
03:09:03,040 --> 03:09:03,920
then
5230
03:09:03,920 --> 03:09:06,319
we're going to do result.push
5231
03:09:06,319 --> 03:09:08,399
number and then we're going to return
5232
03:09:08,399 --> 03:09:10,560
that array at the end all right so let's
5233
03:09:10,560 --> 03:09:12,240
give that a try now so we're going to do
5234
03:09:12,240 --> 03:09:15,040
console.log
5235
03:09:15,040 --> 03:09:17,040
and we're going to call our function
5236
03:09:17,040 --> 03:09:18,080
filter
5237
03:09:18,080 --> 03:09:19,439
we're going to give it an array of one
5238
03:09:19,439 --> 03:09:20,880
two three four
5239
03:09:20,880 --> 03:09:22,880
five six
5240
03:09:22,880 --> 03:09:25,439
right and then put a comma here and
5241
03:09:25,439 --> 03:09:27,359
let's give it another argument and the
5242
03:09:27,359 --> 03:09:29,359
greater than number will be three so
5243
03:09:29,359 --> 03:09:31,920
let's format our code and let's run it
5244
03:09:31,920 --> 03:09:33,760
all right and then filter work so it
5245
03:09:33,760 --> 03:09:36,960
gave us greater than four five and six
5246
03:09:36,960 --> 03:09:39,760
so here we've kind of created our own
5247
03:09:39,760 --> 03:09:42,000
filter this is like a manual way of
5248
03:09:42,000 --> 03:09:43,680
making a filter and it's all right it
5249
03:09:43,680 --> 03:09:46,319
does okay job if we need you know for
5250
03:09:46,319 --> 03:09:48,880
example let's say we give it a two well
5251
03:09:48,880 --> 03:09:50,720
then it should give us number three four
5252
03:09:50,720 --> 03:09:52,960
five six let's run it so now it's only
5253
03:09:52,960 --> 03:09:54,800
looking for numbers greater than two so
5254
03:09:54,800 --> 03:09:56,479
boom it it works
5255
03:09:56,479 --> 03:09:57,279
but
5256
03:09:57,279 --> 03:10:00,800
let's use the dot filter method and dot
5257
03:10:00,800 --> 03:10:02,399
filter is an array method that we can
5258
03:10:02,399 --> 03:10:04,640
actually use and it makes our life a lot
5259
03:10:04,640 --> 03:10:08,000
easier okay so watch this what we can do
5260
03:10:08,000 --> 03:10:10,640
is let's comment this out right now and
5261
03:10:10,640 --> 03:10:13,200
i can actually go and create nums okay
5262
03:10:13,200 --> 03:10:15,520
so let's say we create nums one two
5263
03:10:15,520 --> 03:10:18,080
three four five six okay and let's
5264
03:10:18,080 --> 03:10:19,439
filter through this so let's say
5265
03:10:19,439 --> 03:10:22,560
nums.filter and i'm gonna say for each
5266
03:10:22,560 --> 03:10:25,040
num return to me
5267
03:10:25,040 --> 03:10:28,160
make sure num is greater than
5268
03:10:28,160 --> 03:10:30,720
six all right let's console log this and
5269
03:10:30,720 --> 03:10:32,720
here's the cool thing about filter okay
5270
03:10:32,720 --> 03:10:35,920
filter returns an array all right that's
5271
03:10:35,920 --> 03:10:37,680
the cool thing about filters so if i go
5272
03:10:37,680 --> 03:10:40,319
back to filter here it also it loops and
5273
03:10:40,319 --> 03:10:43,520
returns an array with matching
5274
03:10:43,520 --> 03:10:44,800
conditions
5275
03:10:44,800 --> 03:10:46,720
so that's the difference between map and
5276
03:10:46,720 --> 03:10:49,200
filter map will return the exact thing
5277
03:10:49,200 --> 03:10:52,319
and filter will loop and return an array
5278
03:10:52,319 --> 03:10:54,560
with matching conditions all right so
5279
03:10:54,560 --> 03:10:56,560
let's give this a try and let's console
5280
03:10:56,560 --> 03:10:57,680
log this
5281
03:10:57,680 --> 03:10:59,760
and let's run it nothing is greater than
5282
03:10:59,760 --> 03:11:01,600
six so that's why we got nothing back
5283
03:11:01,600 --> 03:11:04,160
let's try three boom four five six let's
5284
03:11:04,160 --> 03:11:06,880
try uh let's try greater than or equal
5285
03:11:06,880 --> 03:11:08,880
to this so if we do greater than or
5286
03:11:08,880 --> 03:11:11,680
equal to then how many items should we
5287
03:11:11,680 --> 03:11:13,680
get back take a guess
5288
03:11:13,680 --> 03:11:16,160
all right if you said four good job
5289
03:11:16,160 --> 03:11:18,560
that's right it is four great work on
5290
03:11:18,560 --> 03:11:20,319
that but it will return four because
5291
03:11:20,319 --> 03:11:21,279
it'll be
5292
03:11:21,279 --> 03:11:22,560
four five and six because they're
5293
03:11:22,560 --> 03:11:24,880
greater and this is equal
5294
03:11:24,880 --> 03:11:28,000
greater than or equal to so let's run it
5295
03:11:28,000 --> 03:11:30,960
one other thing with conditionals that
5296
03:11:30,960 --> 03:11:34,399
um you should know about is uh operation
5297
03:11:34,399 --> 03:11:36,640
called and and all right so
5298
03:11:36,640 --> 03:11:38,720
the and logic is really important for
5299
03:11:38,720 --> 03:11:40,399
you to learn so let's go ahead and look
5300
03:11:40,399 --> 03:11:43,040
let's just practice this here but if you
5301
03:11:43,040 --> 03:11:47,040
have true or false watch what happens if
5302
03:11:47,040 --> 03:11:48,960
you have these statements like this
5303
03:11:48,960 --> 03:11:50,800
right if you have true statement or a
5304
03:11:50,800 --> 03:11:52,160
false statement
5305
03:11:52,160 --> 03:11:54,000
you can have so
5306
03:11:54,000 --> 03:11:56,880
if you have this this will return a true
5307
03:11:56,880 --> 03:11:58,800
and how you write an or
5308
03:11:58,800 --> 03:12:00,720
is like this in javascript if you do
5309
03:12:00,720 --> 03:12:02,000
true or false it's going to give you a
5310
03:12:02,000 --> 03:12:04,720
true if you do true
5311
03:12:04,720 --> 03:12:06,239
and and
5312
03:12:06,239 --> 03:12:08,319
and false you're going to get back or
5313
03:12:08,319 --> 03:12:10,000
false all right so these things are
5314
03:12:10,000 --> 03:12:11,600
important for you to know they're logic
5315
03:12:11,600 --> 03:12:14,000
tables and i think it's much better for
5316
03:12:14,000 --> 03:12:15,439
you to just understand them by example
5317
03:12:15,439 --> 03:12:16,720
because if i tell you logic tables
5318
03:12:16,720 --> 03:12:18,080
you're going gonna be like oh my god
5319
03:12:18,080 --> 03:12:20,720
logic tables what is that well it's a
5320
03:12:20,720 --> 03:12:22,720
lot simpler than you think so for
5321
03:12:22,720 --> 03:12:27,040
example say i want numbers greater than
5322
03:12:27,040 --> 03:12:27,920
four
5323
03:12:27,920 --> 03:12:30,080
so i want numbers greater than four but
5324
03:12:30,080 --> 03:12:32,399
i also want numbers
5325
03:12:32,399 --> 03:12:35,279
uh that are less than three or less than
5326
03:12:35,279 --> 03:12:37,359
two let's say how can i say that
5327
03:12:37,359 --> 03:12:39,359
statement well i can say it like this
5328
03:12:39,359 --> 03:12:42,319
let's go or and now if i run it it's
5329
03:12:42,319 --> 03:12:43,920
gonna notice that it gave me a five and
5330
03:12:43,920 --> 03:12:46,399
a six so those match the conditions and
5331
03:12:46,399 --> 03:12:48,640
it gave me a one because one also
5332
03:12:48,640 --> 03:12:50,640
matched the condition so
5333
03:12:50,640 --> 03:12:52,800
anything that's a or statement here's
5334
03:12:52,800 --> 03:12:54,720
the cool thing about or it needs to
5335
03:12:54,720 --> 03:12:55,920
match this
5336
03:12:55,920 --> 03:12:58,080
or this it just needs to match one of
5337
03:12:58,080 --> 03:13:00,239
them and if it's the case then it'll
5338
03:13:00,239 --> 03:13:02,080
automatically be true
5339
03:13:02,080 --> 03:13:03,760
just needs to match one of them but if
5340
03:13:03,760 --> 03:13:05,520
you said and
5341
03:13:05,520 --> 03:13:07,920
right if you put a and here logical and
5342
03:13:07,920 --> 03:13:10,640
like this it won't show anything you
5343
03:13:10,640 --> 03:13:13,120
know why because nothing matches that
5344
03:13:13,120 --> 03:13:15,200
nothing is greater there's no number
5345
03:13:15,200 --> 03:13:18,720
that's greater than five and six and
5346
03:13:18,720 --> 03:13:20,720
less than two at the same time this
5347
03:13:20,720 --> 03:13:24,800
wants both to be true at the same exact
5348
03:13:24,800 --> 03:13:28,399
time so here we wanna put an or examples
5349
03:13:28,399 --> 03:13:31,200
of these could be like let's say a user
5350
03:13:31,200 --> 03:13:34,640
is authenticated right in an app
5351
03:13:34,640 --> 03:13:36,160
and then
5352
03:13:36,160 --> 03:13:37,439
user
5353
03:13:37,439 --> 03:13:38,479
is
5354
03:13:38,479 --> 03:13:39,520
a
5355
03:13:39,520 --> 03:13:40,800
premium
5356
03:13:40,800 --> 03:13:43,680
member you know or like
5357
03:13:43,680 --> 03:13:44,720
and
5358
03:13:44,720 --> 03:13:47,359
user is paying member
5359
03:13:47,359 --> 03:13:48,319
so
5360
03:13:48,319 --> 03:13:50,319
then you want something to happen right
5361
03:13:50,319 --> 03:13:52,800
so you can go if then you want to run
5362
03:13:52,800 --> 03:13:55,120
some like piece of code that that let's
5363
03:13:55,120 --> 03:13:57,760
just say does something okay so if user
5364
03:13:57,760 --> 03:14:01,120
is out and then like code goes here so
5365
03:14:01,120 --> 03:14:03,920
if user is authenticated and user is a
5366
03:14:03,920 --> 03:14:06,720
paying member then do the following so
5367
03:14:06,720 --> 03:14:08,880
this is an example so if this evaluates
5368
03:14:08,880 --> 03:14:11,359
to true and this evaluates to true
5369
03:14:11,359 --> 03:14:13,200
that's the only time this code is going
5370
03:14:13,200 --> 03:14:16,319
to run but let's say the user is not a
5371
03:14:16,319 --> 03:14:18,479
paying member and this is false
5372
03:14:18,479 --> 03:14:21,840
then you know for example let's let's
5373
03:14:21,840 --> 03:14:23,680
make a let's make an example here let's
5374
03:14:23,680 --> 03:14:26,479
say you're a youtube user right and you
5375
03:14:26,479 --> 03:14:28,720
have youtube premium so if you have
5376
03:14:28,720 --> 03:14:31,760
youtube premium then i show you
5377
03:14:31,760 --> 03:14:32,960
no ads
5378
03:14:32,960 --> 03:14:35,680
so if you're authenticated logged in and
5379
03:14:35,680 --> 03:14:37,279
you're a paying member then i show you
5380
03:14:37,279 --> 03:14:40,960
no ads otherwise what i do
5381
03:14:40,960 --> 03:14:43,279
show you ads so that's the nice part
5382
03:14:43,279 --> 03:14:45,840
about this and and thing
5383
03:14:45,840 --> 03:14:48,080
so if i can even make it like this right
5384
03:14:48,080 --> 03:14:50,960
equal true and this is equal to true so
5385
03:14:50,960 --> 03:14:52,640
that would be like more verbose way of
5386
03:14:52,640 --> 03:14:55,279
saying it but this is why this chaining
5387
03:14:55,279 --> 03:14:57,840
kind of comes in but then like doing the
5388
03:14:57,840 --> 03:15:00,000
same youtube example maybe let's just
5389
03:15:00,000 --> 03:15:02,880
say that let's remove the ads like let's
5390
03:15:02,880 --> 03:15:05,040
say the youtube app deciding when to
5391
03:15:05,040 --> 03:15:06,880
show you the videos so if i go to
5392
03:15:06,880 --> 03:15:09,120
youtube oh i can't go to youtube right
5393
03:15:09,120 --> 03:15:11,279
now because i'm in focus mode so you're
5394
03:15:11,279 --> 03:15:13,439
just gonna have to imagine that we're in
5395
03:15:13,439 --> 03:15:15,120
youtube because i'm a human being right
5396
03:15:15,120 --> 03:15:16,960
like i'm sometimes making these courses
5397
03:15:16,960 --> 03:15:18,880
for you and these exercises for you and
5398
03:15:18,880 --> 03:15:21,040
then i get distracted spend a bunch of
5399
03:15:21,040 --> 03:15:23,680
time watching youtube and it's just it's
5400
03:15:23,680 --> 03:15:26,160
just a waste of time right and i end up
5401
03:15:26,160 --> 03:15:28,000
not making this course material for you
5402
03:15:28,000 --> 03:15:30,560
so i have to stop myself and block my
5403
03:15:30,560 --> 03:15:32,479
own youtube so there you go that's what
5404
03:15:32,479 --> 03:15:34,720
you saw i've blocked my own youtube and
5405
03:15:34,720 --> 03:15:37,920
this is what my current focus is so
5406
03:15:37,920 --> 03:15:39,359
let's just go back because i don't want
5407
03:15:39,359 --> 03:15:41,120
to fall down the youtube rabbit hole but
5408
03:15:41,120 --> 03:15:43,040
just imagine in your mind's eye right
5409
03:15:43,040 --> 03:15:46,000
what happens if you're authenticated you
5410
03:15:46,000 --> 03:15:48,160
be white we might want to relax this
5411
03:15:48,160 --> 03:15:50,720
statement and put an or here why you
5412
03:15:50,720 --> 03:15:54,640
know show youtube videos so
5413
03:15:54,640 --> 03:15:56,880
whether the youtube user is
5414
03:15:56,880 --> 03:15:59,520
authenticated or whether the
5415
03:15:59,520 --> 03:16:02,399
user is a paying member in either case
5416
03:16:02,399 --> 03:16:04,960
they still get to see youtube videos
5417
03:16:04,960 --> 03:16:07,359
regardless but let's just say you might
5418
03:16:07,359 --> 03:16:09,520
have to be logged in so that's why the
5419
03:16:09,520 --> 03:16:12,399
or so the aura creates relaxing
5420
03:16:12,399 --> 03:16:15,439
conditions easier to match and creates
5421
03:16:15,439 --> 03:16:17,920
these strict conditions that are like
5422
03:16:17,920 --> 03:16:21,600
district disciplinarian father okay so
5423
03:16:21,600 --> 03:16:23,760
use them accordingly
5424
03:16:23,760 --> 03:16:26,800
they both have their own use cases uh
5425
03:16:26,800 --> 03:16:28,560
but they're very important to understand
5426
03:16:28,560 --> 03:16:30,800
like the and and the ors okay and they
5427
03:16:30,800 --> 03:16:34,399
come up quite often so here i can using
5428
03:16:34,399 --> 03:16:35,279
this
5429
03:16:35,279 --> 03:16:37,520
i can find numbers below a certain thing
5430
03:16:37,520 --> 03:16:40,080
and numbers less than okay all right so
5431
03:16:40,080 --> 03:16:42,960
let's make this example a little bit
5432
03:16:42,960 --> 03:16:47,040
better okay oops did not mean to do that
5433
03:16:47,040 --> 03:16:49,279
cool let's go back down all right let's
5434
03:16:49,279 --> 03:16:50,800
make this example a little bit better so
5435
03:16:50,800 --> 03:16:52,800
what can we do with this okay so say we
5436
03:16:52,800 --> 03:16:55,840
create a array of objects so this is
5437
03:16:55,840 --> 03:16:57,200
where it's going to start getting really
5438
03:16:57,200 --> 03:16:59,840
cool because in reality you're always
5439
03:16:59,840 --> 03:17:01,680
dealing with or mo a lot of the times
5440
03:17:01,680 --> 03:17:03,680
you're dealing with array of objects
5441
03:17:03,680 --> 03:17:05,359
when you're using an api or whatever and
5442
03:17:05,359 --> 03:17:06,640
i'll show you what it looks like so say
5443
03:17:06,640 --> 03:17:09,040
you have actors right and you want to
5444
03:17:09,040 --> 03:17:11,439
have actors with their net worths and
5445
03:17:11,439 --> 03:17:12,640
then you want to
5446
03:17:12,640 --> 03:17:14,880
filter based on their net worth so only
5447
03:17:14,880 --> 03:17:17,520
show you like the richest actors versus
5448
03:17:17,520 --> 03:17:19,439
like the actors that might not be that
5449
03:17:19,439 --> 03:17:21,680
rich yeah so let's say we create an
5450
03:17:21,680 --> 03:17:23,600
array yes first thing we're going to do
5451
03:17:23,600 --> 03:17:25,279
is we're going to create this array and
5452
03:17:25,279 --> 03:17:27,439
then inside of this array we're going to
5453
03:17:27,439 --> 03:17:30,399
have we're going to create an object and
5454
03:17:30,399 --> 03:17:32,000
we're going to create a name and let's
5455
03:17:32,000 --> 03:17:34,560
say johnny short for johnny tepp and
5456
03:17:34,560 --> 03:17:36,239
then we're going to give johnny depp a
5457
03:17:36,239 --> 03:17:38,880
net worth of whatever that is let's say
5458
03:17:38,880 --> 03:17:42,720
amber uh net worth of 10 and then uh
5459
03:17:42,720 --> 03:17:45,040
let's create create leonardo
5460
03:17:45,040 --> 03:17:46,960
with a huge net worth so whatever that
5461
03:17:46,960 --> 03:17:50,000
is 100 million cool so we have an array
5462
03:17:50,000 --> 03:17:52,479
of objects okay so when you have an
5463
03:17:52,479 --> 03:17:54,479
array of objects what you can do through
5464
03:17:54,479 --> 03:17:57,840
it is you can loop through it um kind of
5465
03:17:57,840 --> 03:18:00,080
in like a normal way if you wanted to
5466
03:18:00,080 --> 03:18:01,520
but i'm just going to go and show you
5467
03:18:01,520 --> 03:18:02,960
how to make that filter example that
5468
03:18:02,960 --> 03:18:04,560
we're talking about okay so what i'm
5469
03:18:04,560 --> 03:18:05,399
going to do is i'm going to say
5470
03:18:05,399 --> 03:18:07,680
actors.filter and i'm going to filter
5471
03:18:07,680 --> 03:18:09,520
them and i'm going to grab each actor
5472
03:18:09,520 --> 03:18:11,120
and i'm going to filter them based on
5473
03:18:11,120 --> 03:18:13,040
their net worth anywhere where their net
5474
03:18:13,040 --> 03:18:14,720
worth is greater than 10. so if it's
5475
03:18:14,720 --> 03:18:17,680
greater than 10 show me those actors so
5476
03:18:17,680 --> 03:18:20,080
guess which actors it's going to show me
5477
03:18:20,080 --> 03:18:21,760
can you take a guess i'll give you five
5478
03:18:21,760 --> 03:18:26,239
seconds five four three two one
5479
03:18:26,239 --> 03:18:28,399
you're absolutely right it's only going
5480
03:18:28,399 --> 03:18:30,560
to show me johnny and leonardo it's not
5481
03:18:30,560 --> 03:18:32,160
gonna show me amber because after that
5482
03:18:32,160 --> 03:18:34,399
lawsuit she ain't got no money left
5483
03:18:34,399 --> 03:18:36,880
right so let's hit run on this and we
5484
03:18:36,880 --> 03:18:40,239
see leonardo and we see johnny and we do
5485
03:18:40,239 --> 03:18:42,960
not see amber right let's go ahead and
5486
03:18:42,960 --> 03:18:45,279
comment this out and i'll also delete
5487
03:18:45,279 --> 03:18:46,640
this line here because we don't need
5488
03:18:46,640 --> 03:18:48,720
that anymore so let's run this again and
5489
03:18:48,720 --> 03:18:51,359
you can see that i have johnny and
5490
03:18:51,359 --> 03:18:53,680
leonardo right here so you can see how
5491
03:18:53,680 --> 03:18:56,880
powerful filtering is right like it is
5492
03:18:56,880 --> 03:18:59,359
able to go through an array of objects
5493
03:18:59,359 --> 03:19:01,680
and it's able to filter based on the net
5494
03:19:01,680 --> 03:19:04,080
worth and then based on that it's only
5495
03:19:04,080 --> 03:19:06,080
returning it's creating a new array of
5496
03:19:06,080 --> 03:19:07,680
objects and it's only returning the
5497
03:19:07,680 --> 03:19:09,040
objects
5498
03:19:09,040 --> 03:19:10,160
that
5499
03:19:10,160 --> 03:19:13,040
have a net worth of greater than 10. and
5500
03:19:13,040 --> 03:19:15,279
we can keep taking this further now what
5501
03:19:15,279 --> 03:19:16,800
i want to show you is
5502
03:19:16,800 --> 03:19:18,800
we can not just keep everything in
5503
03:19:18,800 --> 03:19:21,279
console logs right but we can also put
5504
03:19:21,279 --> 03:19:23,359
things to the dom so we're going to get
5505
03:19:23,359 --> 03:19:25,520
a lot more into dom elements later but
5506
03:19:25,520 --> 03:19:27,520
right now let's just do a little sneak
5507
03:19:27,520 --> 03:19:29,200
preview so what i'm going to do is i'm
5508
03:19:29,200 --> 03:19:33,279
going to say playground dot inner html
5509
03:19:33,279 --> 03:19:36,479
and um we're gonna now output this stuff
5510
03:19:36,479 --> 03:19:38,479
to an html okay so i'm gonna say
5511
03:19:38,479 --> 03:19:40,880
internet html and i'm just gonna say
5512
03:19:40,880 --> 03:19:42,880
leonardo okay let's start with this and
5513
03:19:42,880 --> 03:19:46,399
end the h1 tag perfect now let's run it
5514
03:19:46,399 --> 03:19:47,760
and you can see
5515
03:19:47,760 --> 03:19:49,760
it runs it right over here the next
5516
03:19:49,760 --> 03:19:51,359
thing i want to do is right now it's
5517
03:19:51,359 --> 03:19:53,359
hard coded i'm get how i'm getting his
5518
03:19:53,359 --> 03:19:56,239
name so let's actually write a piece of
5519
03:19:56,239 --> 03:19:59,120
code where we can dynamically get this
5520
03:19:59,120 --> 03:20:00,880
name over here okay so let's go ahead
5521
03:20:00,880 --> 03:20:02,399
and instead of console logging this
5522
03:20:02,399 --> 03:20:04,399
result i'm going to store this so we're
5523
03:20:04,399 --> 03:20:06,479
going to say const result okay or let's
5524
03:20:06,479 --> 03:20:08,080
just say let result and we're going to
5525
03:20:08,080 --> 03:20:10,960
store it in here and then here
5526
03:20:10,960 --> 03:20:13,279
let's make this a template literal where
5527
03:20:13,279 --> 03:20:15,279
it says hard-coded leonardo we're gonna
5528
03:20:15,279 --> 03:20:17,760
make it javascript i'm gonna say result
5529
03:20:17,760 --> 03:20:19,439
get me the zeroth element from the
5530
03:20:19,439 --> 03:20:21,680
result so that's gonna get me the
5531
03:20:21,680 --> 03:20:24,160
first thing all right and uh then i'm
5532
03:20:24,160 --> 03:20:25,200
gonna say
5533
03:20:25,200 --> 03:20:26,239
dot
5534
03:20:26,239 --> 03:20:27,920
actually i don't need to do yeah yeah i
5535
03:20:27,920 --> 03:20:30,000
can do dot name okay result zero dot
5536
03:20:30,000 --> 03:20:31,680
name so that'll probably get me johnny
5537
03:20:31,680 --> 03:20:34,800
depp let's run this okay sometimes if it
5538
03:20:34,800 --> 03:20:36,479
breaks like this just refresh and
5539
03:20:36,479 --> 03:20:37,760
everything should be fine so i'm gonna
5540
03:20:37,760 --> 03:20:39,439
go ahead and refresh
5541
03:20:39,439 --> 03:20:41,840
okay perfect so now you can see that
5542
03:20:41,840 --> 03:20:43,520
it's actually getting me
5543
03:20:43,520 --> 03:20:46,000
johnny kind of how i expected it would
5544
03:20:46,000 --> 03:20:47,680
let's go console.log and we'll see a
5545
03:20:47,680 --> 03:20:50,399
result here as well so you can see all
5546
03:20:50,399 --> 03:20:54,640
right and johnny net worth is 200 to
5547
03:20:54,640 --> 03:20:57,439
whatever two million right and then
5548
03:20:57,439 --> 03:20:59,359
leonardo dicaprio net worth is 10
5549
03:20:59,359 --> 03:21:02,960
million and uh i'm able to get johnny
5550
03:21:02,960 --> 03:21:04,960
right here by doing result zero dot name
5551
03:21:04,960 --> 03:21:08,000
dynamically if i wanted to basically see
5552
03:21:08,000 --> 03:21:09,680
let me actually run this again if i
5553
03:21:09,680 --> 03:21:11,680
wanted to see the entire object like
5554
03:21:11,680 --> 03:21:14,319
this entire thing but output it onto the
5555
03:21:14,319 --> 03:21:15,120
dom
5556
03:21:15,120 --> 03:21:17,920
i could stringify it so i can actually
5557
03:21:17,920 --> 03:21:18,720
go
5558
03:21:18,720 --> 03:21:22,880
hey get me the result and then we'll say
5559
03:21:22,880 --> 03:21:27,840
stringify json. let's go json.stringify
5560
03:21:27,840 --> 03:21:30,560
and then we'll pass in result okay and
5561
03:21:30,560 --> 03:21:32,479
let's run this and it'll just kind of
5562
03:21:32,479 --> 03:21:34,640
like output it kind of like that which
5563
03:21:34,640 --> 03:21:37,120
looks pretty hideous so we want it to
5564
03:21:37,120 --> 03:21:38,399
look a little bit better right let's
5565
03:21:38,399 --> 03:21:40,640
start off with just showing the names of
5566
03:21:40,640 --> 03:21:42,319
the people that have a higher net worth
5567
03:21:42,319 --> 03:21:43,600
than let's say
5568
03:21:43,600 --> 03:21:45,760
two million so basically what i'm gonna
5569
03:21:45,760 --> 03:21:48,800
do here is i'm gonna do the following
5570
03:21:48,800 --> 03:21:51,040
okay so let's let's go ahead and run
5571
03:21:51,040 --> 03:21:53,439
this so what i'm going to do is i want
5572
03:21:53,439 --> 03:21:56,800
to make it so it just gives me the names
5573
03:21:56,800 --> 03:21:59,760
of each person okay and how can i do
5574
03:21:59,760 --> 03:22:01,920
that so say i have an array like this
5575
03:22:01,920 --> 03:22:04,399
let's store that in results i'm going to
5576
03:22:04,399 --> 03:22:06,560
paste that here so let's whenever i type
5577
03:22:06,560 --> 03:22:08,319
in result i should get that well how
5578
03:22:08,319 --> 03:22:09,200
will i
5579
03:22:09,200 --> 03:22:10,399
join them
5580
03:22:10,399 --> 03:22:13,359
what i can do is i can actually map
5581
03:22:13,359 --> 03:22:15,520
through this because map will return an
5582
03:22:15,520 --> 03:22:17,920
array and so i'm going to map and first
5583
03:22:17,920 --> 03:22:19,200
i'm going to get this element and i'm
5584
03:22:19,200 --> 03:22:20,640
going to get this element and all i'm
5585
03:22:20,640 --> 03:22:21,760
going to do is i'm just going to pull
5586
03:22:21,760 --> 03:22:23,040
out the name so what i'm going to do is
5587
03:22:23,040 --> 03:22:24,560
i'm going to say result.map and i'm
5588
03:22:24,560 --> 03:22:26,720
going to say for each actor in there i
5589
03:22:26,720 --> 03:22:28,960
want you to just return to me actor.name
5590
03:22:28,960 --> 03:22:30,880
so this is just going to return this
5591
03:22:30,880 --> 03:22:32,560
code here saying return an array with
5592
03:22:32,560 --> 03:22:34,800
just actor names that's it so return an
5593
03:22:34,800 --> 03:22:37,520
array map is saying return an array
5594
03:22:37,520 --> 03:22:38,960
that's just filled with actor names
5595
03:22:38,960 --> 03:22:40,800
that's it that's all i'm doing so let's
5596
03:22:40,800 --> 03:22:42,479
run it and boom i'm just getting this
5597
03:22:42,479 --> 03:22:45,359
back even if i do that that should get
5598
03:22:45,359 --> 03:22:47,279
me kind of the right result now what i
5599
03:22:47,279 --> 03:22:49,600
can do on top of this is when you get
5600
03:22:49,600 --> 03:22:52,479
this array back you can actually call a
5601
03:22:52,479 --> 03:22:55,680
method like join on it and if you join
5602
03:22:55,680 --> 03:22:57,600
it it'll actually join it and turn into
5603
03:22:57,600 --> 03:22:59,439
a string so it's the opposite of splice
5604
03:22:59,439 --> 03:23:02,080
splice turns it into an array the join
5605
03:23:02,080 --> 03:23:04,720
will turn it into a string so i'll
5606
03:23:04,720 --> 03:23:07,040
actually say the join
5607
03:23:07,040 --> 03:23:09,680
and then join them by either spaces or i
5608
03:23:09,680 --> 03:23:12,560
can say join them by a comma and a space
5609
03:23:12,560 --> 03:23:14,160
right so now it's going to be johnny and
5610
03:23:14,160 --> 03:23:16,239
leonardo and no matter how many actors i
5611
03:23:16,239 --> 03:23:18,880
had it would kind of join them by name
5612
03:23:18,880 --> 03:23:21,120
with a comma and a space in there so all
5613
03:23:21,120 --> 03:23:23,439
i need to do to this thing that we had
5614
03:23:23,439 --> 03:23:25,279
written earlier and i was just pressing
5615
03:23:25,279 --> 03:23:27,120
up so i could find that and i'm just
5616
03:23:27,120 --> 03:23:28,880
going to say dot join and we're going
5617
03:23:28,880 --> 03:23:30,960
gonna put this here and put a comma
5618
03:23:30,960 --> 03:23:33,120
right there i'm gonna hit enter and now
5619
03:23:33,120 --> 03:23:35,439
just doing the whole thing all in one go
5620
03:23:35,439 --> 03:23:36,319
so
5621
03:23:36,319 --> 03:23:39,680
let's copy that line and we're gonna do
5622
03:23:39,680 --> 03:23:41,840
it right here okay
5623
03:23:41,840 --> 03:23:44,640
let names and i'm going to say names are
5624
03:23:44,640 --> 03:23:46,399
you know whatever result was we're going
5625
03:23:46,399 --> 03:23:48,960
to map through it for each actor just
5626
03:23:48,960 --> 03:23:50,720
get me the actor name
5627
03:23:50,720 --> 03:23:51,920
so again
5628
03:23:51,920 --> 03:23:54,319
get me an array with actor names that's
5629
03:23:54,319 --> 03:23:57,359
all i'm saying here and then join it
5630
03:23:57,359 --> 03:24:00,080
by a comma and a space and then those
5631
03:24:00,080 --> 03:24:01,120
names
5632
03:24:01,120 --> 03:24:02,800
i want them
5633
03:24:02,800 --> 03:24:04,479
outputted on my
5634
03:24:04,479 --> 03:24:06,479
dom so let's hit run
5635
03:24:06,479 --> 03:24:08,160
and you can see that i'm getting those
5636
03:24:08,160 --> 03:24:10,239
names out put it on my dom okay let's
5637
03:24:10,239 --> 03:24:12,720
add in a couple of more actors here
5638
03:24:12,720 --> 03:24:14,479
so boom boom
5639
03:24:14,479 --> 03:24:15,600
boom
5640
03:24:15,600 --> 03:24:19,520
so let's look up matt damon networth
5641
03:24:19,520 --> 03:24:21,680
all right celebritynetworth.com perfect
5642
03:24:21,680 --> 03:24:26,160
so 170 million all right 170 million
5643
03:24:26,160 --> 03:24:27,720
let's type that in
5644
03:24:27,720 --> 03:24:31,120
170 million let's type it in so we can
5645
03:24:31,120 --> 03:24:33,200
copy the right amount of zeros here
5646
03:24:33,200 --> 03:24:36,000
let's copy that let's move it back and
5647
03:24:36,000 --> 03:24:37,760
i'm going to say matt
5648
03:24:37,760 --> 03:24:39,600
we'll put that right there so his net
5649
03:24:39,600 --> 03:24:43,200
worth is 170 million let's uh grab
5650
03:24:43,200 --> 03:24:45,840
another actor let's go
5651
03:24:45,840 --> 03:24:47,680
brad pitt
5652
03:24:47,680 --> 03:24:49,040
net worth
5653
03:24:49,040 --> 03:24:52,880
okay and we got 300 million here perfect
5654
03:24:52,880 --> 03:24:57,359
so let's go 300 million and
5655
03:24:57,359 --> 03:24:58,640
we should get
5656
03:24:58,640 --> 03:25:01,040
calculator.name nice
5657
03:25:01,040 --> 03:25:03,600
good website good website and i'm going
5658
03:25:03,600 --> 03:25:05,520
to copy this right here
5659
03:25:05,520 --> 03:25:07,680
and we'll paste it right there just
5660
03:25:07,680 --> 03:25:09,760
remove the commas of course
5661
03:25:09,760 --> 03:25:13,520
all right so we'll name this one bread
5662
03:25:13,520 --> 03:25:17,120
red okay that's it and let's remove
5663
03:25:17,120 --> 03:25:18,640
this one because that's a duplicate so
5664
03:25:18,640 --> 03:25:20,960
this is this is good for now
5665
03:25:20,960 --> 03:25:22,399
and um
5666
03:25:22,399 --> 03:25:24,960
let's let's just run this yeah let's run
5667
03:25:24,960 --> 03:25:26,880
this and see if it works
5668
03:25:26,880 --> 03:25:28,960
and so any actors that have a greater
5669
03:25:28,960 --> 03:25:30,880
than ten dollars net worth you got
5670
03:25:30,880 --> 03:25:33,600
johnny matt brad pitt and leonardo
5671
03:25:33,600 --> 03:25:35,359
dicaprio
5672
03:25:35,359 --> 03:25:37,279
and you know the whole thing works
5673
03:25:37,279 --> 03:25:39,359
perfectly and what's cool is like if you
5674
03:25:39,359 --> 03:25:41,359
wanted to show images for them you could
5675
03:25:41,359 --> 03:25:43,840
also show images you just need to store
5676
03:25:43,840 --> 03:25:46,239
image url
5677
03:25:46,239 --> 03:25:48,800
all right so i think these exercises
5678
03:25:48,800 --> 03:25:51,120
kind of pushed you
5679
03:25:51,120 --> 03:25:52,960
a little bit when it comes to filter
5680
03:25:52,960 --> 03:25:56,000
like if you had to write this code
5681
03:25:56,000 --> 03:25:58,000
without using filter or map oh my god
5682
03:25:58,000 --> 03:26:00,479
like good luck try it and it will be so
5683
03:26:00,479 --> 03:26:02,000
many lines of code there will be so many
5684
03:26:02,000 --> 03:26:03,760
functions
5685
03:26:03,760 --> 03:26:06,160
with filter and map i mean they just
5686
03:26:06,160 --> 03:26:08,880
make it so easy to kind of just like
5687
03:26:08,880 --> 03:26:10,640
go through this so i'm going to comment
5688
03:26:10,640 --> 03:26:12,479
out all of this comment out this going
5689
03:26:12,479 --> 03:26:15,520
to comment out all of this right here
5690
03:26:15,520 --> 03:26:17,200
so when i run it nothing really should
5691
03:26:17,200 --> 03:26:19,680
be coming up perfect
5692
03:26:19,680 --> 03:26:21,120
so now what i want to show you i want to
5693
03:26:21,120 --> 03:26:23,840
show you reduce okay this is the another
5694
03:26:23,840 --> 03:26:26,399
one that's really really important
5695
03:26:26,399 --> 03:26:28,479
so when is reduced used
5696
03:26:28,479 --> 03:26:31,120
let's say you want to combine all of
5697
03:26:31,120 --> 03:26:34,239
if you want to sum all of the net worths
5698
03:26:34,239 --> 03:26:36,160
okay so generally whenever you're
5699
03:26:36,160 --> 03:26:37,840
thinking of summing
5700
03:26:37,840 --> 03:26:41,040
it's a amazing use case for summing like
5701
03:26:41,040 --> 03:26:43,680
whenever you think some
5702
03:26:43,680 --> 03:26:44,840
think
5703
03:26:44,840 --> 03:26:48,160
reduce okay and i'll just try to teach
5704
03:26:48,160 --> 03:26:49,840
you with examples because that'll just
5705
03:26:49,840 --> 03:26:52,239
make it so much easier for you to learn
5706
03:26:52,239 --> 03:26:54,640
so say we want we have that function
5707
03:26:54,640 --> 03:26:56,720
where we sum right so let's go ahead and
5708
03:26:56,720 --> 03:26:58,640
find our sum function
5709
03:26:58,640 --> 03:27:01,439
so where do we have it
5710
03:27:01,439 --> 03:27:02,720
some array
5711
03:27:02,720 --> 03:27:04,560
all right so
5712
03:27:04,560 --> 03:27:06,319
we have this function some array look at
5713
03:27:06,319 --> 03:27:08,560
how many lines of code this is right now
5714
03:27:08,560 --> 03:27:10,640
watch what is going to happen when i use
5715
03:27:10,640 --> 03:27:11,600
reduce
5716
03:27:11,600 --> 03:27:13,359
okay when you're summing an accumulator
5717
03:27:13,359 --> 03:27:15,359
like this that's when you use reduce
5718
03:27:15,359 --> 03:27:17,520
when you are
5719
03:27:17,520 --> 03:27:19,120
doing something else where you need to
5720
03:27:19,120 --> 03:27:22,239
return an array you will use
5721
03:27:22,239 --> 03:27:26,560
map okay so push gets replaced by an app
5722
03:27:26,560 --> 03:27:28,319
all right so let's see if we can turn
5723
03:27:28,319 --> 03:27:30,960
this into a very small lines of code
5724
03:27:30,960 --> 03:27:33,279
like summing an array of numbers
5725
03:27:33,279 --> 03:27:36,720
here's how this will look okay some
5726
03:27:36,720 --> 03:27:39,040
nums okay some
5727
03:27:39,040 --> 03:27:41,439
with reduce okay so i know it's a big
5728
03:27:41,439 --> 03:27:43,520
name but this is still a set the you
5729
03:27:43,520 --> 03:27:45,040
know simple function and i'll show you
5730
03:27:45,040 --> 03:27:46,720
how it works
5731
03:27:46,720 --> 03:27:48,960
and here's what we're gonna say
5732
03:27:48,960 --> 03:27:51,200
okay so we're gonna get numbers
5733
03:27:51,200 --> 03:27:52,800
and we'll do
5734
03:27:52,800 --> 03:27:54,479
we'll have it do
5735
03:27:54,479 --> 03:27:57,279
numbers dot reduce
5736
03:27:57,279 --> 03:27:59,760
all right so to keep things simple let's
5737
03:27:59,760 --> 03:28:01,760
uh not even have it be a function okay
5738
03:28:01,760 --> 03:28:03,439
let's just start actually let's do it
5739
03:28:03,439 --> 03:28:05,520
right here on the right hand side first
5740
03:28:05,520 --> 03:28:08,560
so say i have one two three
5741
03:28:08,560 --> 03:28:11,200
okay how do i sum them up
5742
03:28:11,200 --> 03:28:14,160
so here's how we're gonna do it
5743
03:28:14,160 --> 03:28:17,359
with this okay so let's say i have my
5744
03:28:17,359 --> 03:28:20,319
nums okay so let's say i go nums are 1 2
5745
03:28:20,319 --> 03:28:21,200
3
5746
03:28:21,200 --> 03:28:23,760
and then i'm going to say reduce
5747
03:28:23,760 --> 03:28:27,840
okay and reduce takes in a function
5748
03:28:27,840 --> 03:28:31,439
reduce takes in a function as an
5749
03:28:31,439 --> 03:28:32,560
argument
5750
03:28:32,560 --> 03:28:35,600
that's why it's called a higher order
5751
03:28:35,600 --> 03:28:36,720
function
5752
03:28:36,720 --> 03:28:37,840
because
5753
03:28:37,840 --> 03:28:39,359
even if you look at filter i don't know
5754
03:28:39,359 --> 03:28:41,439
if you noticed but what you're really
5755
03:28:41,439 --> 03:28:43,200
giving filter here
5756
03:28:43,200 --> 03:28:45,200
so
5757
03:28:45,200 --> 03:28:46,960
uh let's just go back you know just in
5758
03:28:46,960 --> 03:28:49,040
case some of this wasn't clear
5759
03:28:49,040 --> 03:28:51,680
number number times two this is just a
5760
03:28:51,680 --> 03:28:54,880
fancy way of writing function that takes
5761
03:28:54,880 --> 03:28:56,319
in a number
5762
03:28:56,319 --> 03:29:00,880
and then you go return number times two
5763
03:29:00,880 --> 03:29:02,880
so you're you're creating a function on
5764
03:29:02,880 --> 03:29:04,720
the fly here okay you're creating like
5765
03:29:04,720 --> 03:29:06,560
an anonymous function with no name on
5766
03:29:06,560 --> 03:29:08,000
the fly here
5767
03:29:08,000 --> 03:29:10,239
and or lambda function and you're just
5768
03:29:10,239 --> 03:29:11,760
passing it that function okay this is
5769
03:29:11,760 --> 03:29:12,960
what's called a higher order function
5770
03:29:12,960 --> 03:29:14,640
you're passing it a function this right
5771
03:29:14,640 --> 03:29:16,960
here is a function like this right here
5772
03:29:16,960 --> 03:29:18,479
is a function
5773
03:29:18,479 --> 03:29:20,479
this right here
5774
03:29:20,479 --> 03:29:22,880
uh right here is a function
5775
03:29:22,880 --> 03:29:24,800
and so we're gonna create a function
5776
03:29:24,800 --> 03:29:25,600
here
5777
03:29:25,600 --> 03:29:27,520
function
5778
03:29:27,520 --> 03:29:29,520
and um
5779
03:29:29,520 --> 03:29:32,640
let's let's give this a try okay so
5780
03:29:32,640 --> 03:29:34,720
what does reduce do
5781
03:29:34,720 --> 03:29:36,880
reduce loops
5782
03:29:36,880 --> 03:29:40,960
and gives you back the accumulator
5783
03:29:40,960 --> 03:29:42,239
all right so
5784
03:29:42,239 --> 03:29:45,120
this is going to be my previous value
5785
03:29:45,120 --> 03:29:47,040
this is going to be my next value i'm
5786
03:29:47,040 --> 03:29:49,600
just going to call it any x for now and
5787
03:29:49,600 --> 03:29:52,239
let's just go ahead and console.log
5788
03:29:52,239 --> 03:29:54,399
previous and next
5789
03:29:54,399 --> 03:29:56,160
okay and let's run this and let's see
5790
03:29:56,160 --> 03:29:57,520
what happens
5791
03:29:57,520 --> 03:29:59,520
okay boom i got one two
5792
03:29:59,520 --> 03:30:02,080
so what's previous and next in this case
5793
03:30:02,080 --> 03:30:05,120
right this is our accumulator so it's
5794
03:30:05,120 --> 03:30:06,720
it's basically going to be like this is
5795
03:30:06,720 --> 03:30:08,560
the previous value this is the next
5796
03:30:08,560 --> 03:30:12,000
value so if you do previous plus next
5797
03:30:12,000 --> 03:30:14,000
it's going to do one plus two
5798
03:30:14,000 --> 03:30:15,439
and then
5799
03:30:15,439 --> 03:30:18,160
previous is going to become three
5800
03:30:18,160 --> 03:30:20,479
okay preview is gonna become three
5801
03:30:20,479 --> 03:30:22,319
all right and then you're gonna go
5802
03:30:22,319 --> 03:30:23,760
through this
5803
03:30:23,760 --> 03:30:26,319
and do three plus three and you're gonna
5804
03:30:26,319 --> 03:30:27,840
turn it into six
5805
03:30:27,840 --> 03:30:29,680
now what i'm gonna do is i'm just going
5806
03:30:29,680 --> 03:30:30,479
to go
5807
03:30:30,479 --> 03:30:31,920
return
5808
03:30:31,920 --> 03:30:33,600
previous plus
5809
03:30:33,600 --> 03:30:36,800
next okay or previous plus
5810
03:30:36,800 --> 03:30:38,479
current that's usually what they call it
5811
03:30:38,479 --> 03:30:40,880
okay current so let's do previous plus
5812
03:30:40,880 --> 03:30:41,760
current
5813
03:30:41,760 --> 03:30:42,640
and
5814
03:30:42,640 --> 03:30:44,319
i'm going gonna store the result of this
5815
03:30:44,319 --> 03:30:46,960
in results so let's go const result is
5816
03:30:46,960 --> 03:30:48,160
equal to that
5817
03:30:48,160 --> 03:30:50,479
and we're gonna go console.log and let's
5818
03:30:50,479 --> 03:30:52,080
get the result and let's take a look at
5819
03:30:52,080 --> 03:30:54,399
what it is and boom you see that we just
5820
03:30:54,399 --> 03:30:57,040
got back six okay so
5821
03:30:57,040 --> 03:31:00,800
this function here with the result um
5822
03:31:00,800 --> 03:31:01,520
with
5823
03:31:01,520 --> 03:31:04,080
using reduce it made our life really
5824
03:31:04,080 --> 03:31:05,920
easy right being able to go through this
5825
03:31:05,920 --> 03:31:08,960
entire thing and sum it so quickly with
5826
03:31:08,960 --> 03:31:11,040
essentially one line of code that's a
5827
03:31:11,040 --> 03:31:13,439
total game changer now let's see if we
5828
03:31:13,439 --> 03:31:16,160
can even make it simpler so we're going
5829
03:31:16,160 --> 03:31:18,960
to use es6 notation and turn this
5830
03:31:18,960 --> 03:31:20,960
function here into an es6 function so
5831
03:31:20,960 --> 03:31:23,439
watch what happens so this function part
5832
03:31:23,439 --> 03:31:25,680
is going to lose this right here and we
5833
03:31:25,680 --> 03:31:27,040
don't need that we don't just say
5834
03:31:27,040 --> 03:31:29,279
function anymore we're going to put a
5835
03:31:29,279 --> 03:31:30,800
greater than
5836
03:31:30,800 --> 03:31:32,239
equal and
5837
03:31:32,239 --> 03:31:34,560
equal and a greater than sign and then
5838
03:31:34,560 --> 03:31:37,200
this part here we we want to do implicit
5839
03:31:37,200 --> 03:31:38,560
return so we're going to remove the
5840
03:31:38,560 --> 03:31:40,319
return but in order to have implicit
5841
03:31:40,319 --> 03:31:42,080
return you also need to remove this
5842
03:31:42,080 --> 03:31:43,760
squiggly like curly braces we're going
5843
03:31:43,760 --> 03:31:45,200
to remove them
5844
03:31:45,200 --> 03:31:47,760
we're going to format it okay
5845
03:31:47,760 --> 03:31:50,479
and you can even if you want try to get
5846
03:31:50,479 --> 03:31:51,920
this on
5847
03:31:51,920 --> 03:31:53,040
one line
5848
03:31:53,040 --> 03:31:55,520
so now that entire solution is just one
5849
03:31:55,520 --> 03:31:56,399
line
5850
03:31:56,399 --> 03:31:58,319
so you're using reduce
5851
03:31:58,319 --> 03:31:59,840
and you're summing up all these numbers
5852
03:31:59,840 --> 03:32:01,359
so when i hit run watch what happens i
5853
03:32:01,359 --> 03:32:02,560
got a six
5854
03:32:02,560 --> 03:32:04,319
and guess what
5855
03:32:04,319 --> 03:32:06,160
can you remember where we've made a
5856
03:32:06,160 --> 03:32:08,239
function that takes two numbers and
5857
03:32:08,239 --> 03:32:09,760
returns their sum because that's all
5858
03:32:09,760 --> 03:32:11,120
you're doing here you're creating an
5859
03:32:11,120 --> 03:32:12,640
anonymous function
5860
03:32:12,640 --> 03:32:14,800
that takes in a number
5861
03:32:14,800 --> 03:32:16,720
and then just returns
5862
03:32:16,720 --> 03:32:18,640
and just sums both of them right that's
5863
03:32:18,640 --> 03:32:20,640
all this is doing so can you remember
5864
03:32:20,640 --> 03:32:23,040
where we've created a function like this
5865
03:32:23,040 --> 03:32:24,640
okay i'll give you a couple of seconds
5866
03:32:24,640 --> 03:32:28,239
five four three two one to find it
5867
03:32:28,239 --> 03:32:31,439
and let's see if i can find it as well
5868
03:32:31,439 --> 03:32:33,200
so if you look
5869
03:32:33,200 --> 03:32:35,439
we created a long time ago
5870
03:32:35,439 --> 03:32:37,600
we created this function sum right here
5871
03:32:37,600 --> 03:32:39,520
okay and we even have its short version
5872
03:32:39,520 --> 03:32:42,160
right here some arrow two some arrow and
5873
03:32:42,160 --> 03:32:44,239
then we also have some
5874
03:32:44,239 --> 03:32:45,840
so let's see
5875
03:32:45,840 --> 03:32:49,200
if we can feed it that function okay so
5876
03:32:49,200 --> 03:32:50,640
now you're going to start to understand
5877
03:32:50,640 --> 03:32:52,399
why it's called a higher order function
5878
03:32:52,399 --> 03:32:54,560
so let's bring it down let's bring some
5879
03:32:54,560 --> 03:32:57,439
down so i'm going to copy this
5880
03:32:57,439 --> 03:32:59,120
and i'll bring it down just so you can
5881
03:32:59,120 --> 03:33:00,479
see it easily
5882
03:33:00,479 --> 03:33:02,560
and what i'm going to do here is instead
5883
03:33:02,560 --> 03:33:04,239
of even writing all of this first we're
5884
03:33:04,239 --> 03:33:06,640
going to turn it into one line of code
5885
03:33:06,640 --> 03:33:08,880
how we had earlier so we've done that
5886
03:33:08,880 --> 03:33:09,600
right
5887
03:33:09,600 --> 03:33:10,560
we said
5888
03:33:10,560 --> 03:33:13,040
previous current
5889
03:33:13,040 --> 03:33:15,120
right and then return previous plus
5890
03:33:15,120 --> 03:33:17,600
current so sum up those two numbers we
5891
03:33:17,600 --> 03:33:20,319
could also say a comma b
5892
03:33:20,319 --> 03:33:21,359
return
5893
03:33:21,359 --> 03:33:23,920
this so if i run this this should still
5894
03:33:23,920 --> 03:33:26,960
give me a six as expected
5895
03:33:26,960 --> 03:33:29,600
and here's the cool part i can actually
5896
03:33:29,600 --> 03:33:31,279
just feed it that entire function just
5897
03:33:31,279 --> 03:33:34,160
like this so i can just say hey
5898
03:33:34,160 --> 03:33:36,399
reduce it to one number
5899
03:33:36,399 --> 03:33:37,680
and then just
5900
03:33:37,680 --> 03:33:39,279
sum it so that's why it's called reduce
5901
03:33:39,279 --> 03:33:40,800
because it's an array and you're
5902
03:33:40,800 --> 03:33:43,040
reducing it down to just one number
5903
03:33:43,040 --> 03:33:45,040
right it's gonna return one number here
5904
03:33:45,040 --> 03:33:46,800
that's why it's called reduce and then
5905
03:33:46,800 --> 03:33:48,560
i'm giving it the sum function and what
5906
03:33:48,560 --> 03:33:50,239
the sum function does
5907
03:33:50,239 --> 03:33:51,200
is
5908
03:33:51,200 --> 03:33:53,040
it just takes in
5909
03:33:53,040 --> 03:33:55,439
things and it sums them up so let's run
5910
03:33:55,439 --> 03:33:58,479
this and this still gives me a six let's
5911
03:33:58,479 --> 03:34:01,040
say i wanted to multi take the
5912
03:34:01,040 --> 03:34:03,520
multiplicative product of all of these
5913
03:34:03,520 --> 03:34:05,760
numbers right so i would want to do 1
5914
03:34:05,760 --> 03:34:07,920
times 2 times 3 times 4. what would i
5915
03:34:07,920 --> 03:34:10,239
need to do in order to get 1 times 2
5916
03:34:10,239 --> 03:34:12,960
times 3 times 4. see if you can do that
5917
03:34:12,960 --> 03:34:15,680
with reduce okay so take a few minutes
5918
03:34:15,680 --> 03:34:17,279
and try it on your own i'll give you
5919
03:34:17,279 --> 03:34:21,840
five seconds five four three two one all
5920
03:34:21,840 --> 03:34:24,000
right let's get let's go all you would
5921
03:34:24,000 --> 03:34:26,000
have to do is you just have to copy this
5922
03:34:26,000 --> 03:34:28,800
create a new function call it multiply
5923
03:34:28,800 --> 03:34:31,439
and you just change this a plus b to a
5924
03:34:31,439 --> 03:34:32,560
times b
5925
03:34:32,560 --> 03:34:34,880
and just feed it multiply
5926
03:34:34,880 --> 03:34:36,960
and now it'll just keep going through
5927
03:34:36,960 --> 03:34:38,880
multiplying those number saving their
5928
03:34:38,880 --> 03:34:41,520
accumulator and spit it out so
5929
03:34:41,520 --> 03:34:42,960
you feed it
5930
03:34:42,960 --> 03:34:45,680
sum you get some you feed it multiply
5931
03:34:45,680 --> 03:34:47,439
you get the multiplication
5932
03:34:47,439 --> 03:34:48,960
multiplicative
5933
03:34:48,960 --> 03:34:50,479
product at the end
5934
03:34:50,479 --> 03:34:52,479
so anytime you're
5935
03:34:52,479 --> 03:34:54,640
using reduce you could create your own
5936
03:34:54,640 --> 03:34:57,120
function or just on the fly just go a
5937
03:34:57,120 --> 03:34:58,560
comma b
5938
03:34:58,560 --> 03:35:01,359
a plus b this is a pattern i use very
5939
03:35:01,359 --> 03:35:04,160
very very very very often
5940
03:35:04,160 --> 03:35:06,800
and i hope that you use this don't
5941
03:35:06,800 --> 03:35:08,800
anytime you're thinking some just copy
5942
03:35:08,800 --> 03:35:10,560
this for now literally you can copy and
5943
03:35:10,560 --> 03:35:12,080
paste this most of the times whenever
5944
03:35:12,080 --> 03:35:13,600
you think some this is usually going to
5945
03:35:13,600 --> 03:35:16,160
be the answer and then you know you can
5946
03:35:16,160 --> 03:35:19,200
stack overflow it later and just go hey
5947
03:35:19,200 --> 03:35:21,359
how do i sum up something with reduce
5948
03:35:21,359 --> 03:35:23,359
how do i do this with reduce and you're
5949
03:35:23,359 --> 03:35:24,399
going to start
5950
03:35:24,399 --> 03:35:26,319
learning this reduces something pretty
5951
03:35:26,319 --> 03:35:28,000
complicated and so i don't want you to
5952
03:35:28,000 --> 03:35:29,840
go fully down the rabbit hole but just
5953
03:35:29,840 --> 03:35:31,279
learn it with pattern recognition
5954
03:35:31,279 --> 03:35:33,439
anytime you have to sum up numbers use
5955
03:35:33,439 --> 03:35:36,160
this right a good exercise for you to
5956
03:35:36,160 --> 03:35:37,439
try would be
5957
03:35:37,439 --> 03:35:39,760
how can you use reduce to sum up all of
5958
03:35:39,760 --> 03:35:41,840
these net worths okay so get the total
5959
03:35:41,840 --> 03:35:44,479
net words so try giving that a you know
5960
03:35:44,479 --> 03:35:46,239
see if you can give that a try i'll give
5961
03:35:46,239 --> 03:35:47,680
you five seconds and then we'll try to
5962
03:35:47,680 --> 03:35:49,520
solve that together okay so i'll give
5963
03:35:49,520 --> 03:35:50,560
five
5964
03:35:50,560 --> 03:35:51,359
four
5965
03:35:51,359 --> 03:35:53,200
three two
5966
03:35:53,200 --> 03:35:54,000
one
5967
03:35:54,000 --> 03:35:56,160
all right let's try to solve that one
5968
03:35:56,160 --> 03:35:58,399
together all right so i got all these
5969
03:35:58,399 --> 03:36:00,399
net words over here
5970
03:36:00,399 --> 03:36:02,880
and i want to sum them up how do i do
5971
03:36:02,880 --> 03:36:05,840
that by the way just a pro tip in
5972
03:36:05,840 --> 03:36:07,520
replied there's something really sick
5973
03:36:07,520 --> 03:36:10,160
that allows you to explain code so let's
5974
03:36:10,160 --> 03:36:11,600
say you're not understanding what this
5975
03:36:11,600 --> 03:36:13,840
is you can highlight this right click
5976
03:36:13,840 --> 03:36:15,760
and click this button that says explain
5977
03:36:15,760 --> 03:36:18,800
code and this does a pretty freaking
5978
03:36:18,800 --> 03:36:21,600
good job this code takes the array nums
5979
03:36:21,600 --> 03:36:24,080
and reduces it to a single value by
5980
03:36:24,080 --> 03:36:26,399
adding each value together
5981
03:36:26,399 --> 03:36:29,040
that is a really good explanation so you
5982
03:36:29,040 --> 03:36:30,640
could hit yes and then you can also hit
5983
03:36:30,640 --> 03:36:32,560
regenerate and it generates a new
5984
03:36:32,560 --> 03:36:34,880
explanation for you so this code takes
5985
03:36:34,880 --> 03:36:36,239
an array reduce it to single number
5986
03:36:36,239 --> 03:36:38,239
adding all the numbers together
5987
03:36:38,239 --> 03:36:39,120
so
5988
03:36:39,120 --> 03:36:41,600
that's a really freaking awesome feature
5989
03:36:41,600 --> 03:36:44,560
of replica make sure to keep using that
5990
03:36:44,560 --> 03:36:46,720
anywhere you're kind of confused and you
5991
03:36:46,720 --> 03:36:48,319
don't know why we're doing a certain
5992
03:36:48,319 --> 03:36:50,800
thing i mean i'm kind of blown away by
5993
03:36:50,800 --> 03:36:53,840
it it's really it's really good okay so
5994
03:36:53,840 --> 03:36:55,840
like for example look at line 356 right
5995
03:36:55,840 --> 03:36:56,800
now right
5996
03:36:56,800 --> 03:36:58,800
if you highlight this and right click
5997
03:36:58,800 --> 03:37:00,479
and hit explain code
5998
03:37:00,479 --> 03:37:03,120
it will tell you watch this code is an
5999
03:37:03,120 --> 03:37:05,680
example of template literals
6000
03:37:05,680 --> 03:37:07,680
brilliant it is used to display the
6001
03:37:07,680 --> 03:37:10,160
value of variable called names inside an
6002
03:37:10,160 --> 03:37:11,760
h1 tag
6003
03:37:11,760 --> 03:37:14,080
this is brilliant this is explaining it
6004
03:37:14,080 --> 03:37:16,160
better than me let's see what it says
6005
03:37:16,160 --> 03:37:18,640
about this filter one let's see how good
6006
03:37:18,640 --> 03:37:20,640
of a job it does here so let's explain
6007
03:37:20,640 --> 03:37:21,760
code
6008
03:37:21,760 --> 03:37:23,920
this code filters the actors array for
6009
03:37:23,920 --> 03:37:25,600
all actors that have a net worth greater
6010
03:37:25,600 --> 03:37:29,960
than 10. oh my god
6011
03:37:30,640 --> 03:37:32,319
oh my god
6012
03:37:32,319 --> 03:37:35,120
so now let's play with this um actors
6013
03:37:35,120 --> 03:37:36,720
array that we were working with earlier
6014
03:37:36,720 --> 03:37:38,640
right so i'm going to comment out all of
6015
03:37:38,640 --> 03:37:40,960
this stuff here for a second
6016
03:37:40,960 --> 03:37:43,120
and i'm going to go back to this actor
6017
03:37:43,120 --> 03:37:46,239
stuff and let's go ahead and we don't
6018
03:37:46,239 --> 03:37:48,160
need to uncomment anything let's just go
6019
03:37:48,160 --> 03:37:50,160
down here and i'm just going to say
6020
03:37:50,160 --> 03:37:52,239
actors.reduce
6021
03:37:52,239 --> 03:37:54,000
and i'm just going to write what i had
6022
03:37:54,000 --> 03:37:56,239
written before and then i'll then i'll
6023
03:37:56,239 --> 03:37:59,040
start using my brain okay and so there's
6024
03:37:59,040 --> 03:38:01,439
gonna be an accumulator oh one of the
6025
03:38:01,439 --> 03:38:03,200
things
6026
03:38:03,200 --> 03:38:05,680
that is kind of useful to know that i'll
6027
03:38:05,680 --> 03:38:07,520
kind of tell you in a second
6028
03:38:07,520 --> 03:38:09,120
is this okay let me actually tell it to
6029
03:38:09,120 --> 03:38:11,200
you right now so you're not confused
6030
03:38:11,200 --> 03:38:12,880
later okay so let's go ahead and comment
6031
03:38:12,880 --> 03:38:14,080
all of this
6032
03:38:14,080 --> 03:38:15,120
right here
6033
03:38:15,120 --> 03:38:17,359
you could pass in so it this reduce
6034
03:38:17,359 --> 03:38:19,680
takes in two arguments one argument is a
6035
03:38:19,680 --> 03:38:21,760
function and the other argument it takes
6036
03:38:21,760 --> 03:38:25,040
in is like some kind of number okay so
6037
03:38:25,040 --> 03:38:26,960
it takes in two arguments
6038
03:38:26,960 --> 03:38:29,199
so we have already given it a function
6039
03:38:29,199 --> 03:38:30,640
and then we can give it another argument
6040
03:38:30,640 --> 03:38:32,319
like a zero so watch what happens when i
6041
03:38:32,319 --> 03:38:34,319
run it right now i get 10
6042
03:38:34,319 --> 03:38:36,720
but i could also start the accumulator
6043
03:38:36,720 --> 03:38:38,319
from negative 10
6044
03:38:38,319 --> 03:38:40,960
and then it will count up to zero right
6045
03:38:40,960 --> 03:38:43,520
or i could start it at already ten and
6046
03:38:43,520 --> 03:38:45,359
when i started from there it will count
6047
03:38:45,359 --> 03:38:48,560
up to twenty so this secondary argument
6048
03:38:48,560 --> 03:38:51,359
by default usually it starts at zero but
6049
03:38:51,359 --> 03:38:54,560
you can also specify this one so helpful
6050
03:38:54,560 --> 03:38:56,000
for you to know if you don't need that
6051
03:38:56,000 --> 03:38:57,600
you could just write it like this okay
6052
03:38:57,600 --> 03:38:59,600
but i'll just leave that here so you
6053
03:38:59,600 --> 03:39:00,560
know
6054
03:39:00,560 --> 03:39:02,319
that there's different options for you
6055
03:39:02,319 --> 03:39:04,160
available okay let's go back to this
6056
03:39:04,160 --> 03:39:06,399
part here where we were writing actors
6057
03:39:06,399 --> 03:39:08,319
reduce so this is going to be my
6058
03:39:08,319 --> 03:39:11,680
previous value okay and my accumulator
6059
03:39:11,680 --> 03:39:14,000
is going to be zero and then what i'm
6060
03:39:14,000 --> 03:39:15,760
going to do is
6061
03:39:15,760 --> 03:39:18,880
for my current value okay so this is
6062
03:39:18,880 --> 03:39:21,520
previous this is current and i can just
6063
03:39:21,520 --> 03:39:24,080
say dot net worth
6064
03:39:24,080 --> 03:39:25,840
and let's see if this does what we want
6065
03:39:25,840 --> 03:39:28,239
it to do let's console log the result of
6066
03:39:28,239 --> 03:39:29,040
this
6067
03:39:29,040 --> 03:39:30,640
and if it doesn't then we'll try to fix
6068
03:39:30,640 --> 03:39:32,800
it and looks like it does it combines
6069
03:39:32,800 --> 03:39:35,920
all of those net words already okay so
6070
03:39:35,920 --> 03:39:38,000
this is previous
6071
03:39:38,000 --> 03:39:40,080
right if i had to write it in a verbose
6072
03:39:40,080 --> 03:39:40,960
way
6073
03:39:40,960 --> 03:39:43,359
let's write here previous
6074
03:39:43,359 --> 03:39:45,279
this is current
6075
03:39:45,279 --> 03:39:47,520
as you just say current not net worth
6076
03:39:47,520 --> 03:39:49,840
and then previous is that so previous is
6077
03:39:49,840 --> 03:39:52,160
like in the start previous is zero
6078
03:39:52,160 --> 03:39:54,319
then previous is like
6079
03:39:54,319 --> 03:39:56,720
uh whatever the result of this was
6080
03:39:56,720 --> 03:39:58,560
so it's kind of like recursive
6081
03:39:58,560 --> 03:40:01,359
why is it yeah so okay so think about it
6082
03:40:01,359 --> 03:40:03,199
like this right we're going through it
6083
03:40:03,199 --> 03:40:04,640
the first time
6084
03:40:04,640 --> 03:40:06,720
first loop first
6085
03:40:06,720 --> 03:40:09,359
previous is zero so zero plus 20 million
6086
03:40:09,359 --> 03:40:11,040
or whatever is 20 million so then
6087
03:40:11,040 --> 03:40:13,359
previous becomes this next time then 20
6088
03:40:13,359 --> 03:40:15,520
million plus 10
6089
03:40:15,520 --> 03:40:18,720
then that becomes you know that like 20
6090
03:40:18,720 --> 03:40:21,040
million 10 or something like that right
6091
03:40:21,040 --> 03:40:22,160
and then
6092
03:40:22,160 --> 03:40:24,800
uh you add it again and then previous
6093
03:40:24,800 --> 03:40:26,720
becomes the sum of
6094
03:40:26,720 --> 03:40:29,520
20 million plus 170 million so this
6095
03:40:29,520 --> 03:40:30,720
becomes like
6096
03:40:30,720 --> 03:40:33,600
190 million or something like that okay
6097
03:40:33,600 --> 03:40:36,880
and so that's how previous works do this
6098
03:40:36,880 --> 03:40:39,600
we'll say console.log
6099
03:40:39,600 --> 03:40:42,160
and i'll run this and if you remove the
6100
03:40:42,160 --> 03:40:43,840
zero this should still work because by
6101
03:40:43,840 --> 03:40:46,479
default it's uh zero oh no actually in
6102
03:40:46,479 --> 03:40:48,960
this case it won't work c so we have to
6103
03:40:48,960 --> 03:40:50,399
actually pass in a zero so that's why
6104
03:40:50,399 --> 03:40:52,720
that's important uh because something
6105
03:40:52,720 --> 03:40:54,239
because it was trying to like add the
6106
03:40:54,239 --> 03:40:56,080
objects on top of each other so it
6107
03:40:56,080 --> 03:40:58,479
becomes weird so just pass in that zero
6108
03:40:58,479 --> 03:41:00,479
if you need to and then this way you can
6109
03:41:00,479 --> 03:41:02,560
sum it up so see reduce allows you a
6110
03:41:02,560 --> 03:41:04,160
nice little way to sum up this whole
6111
03:41:04,160 --> 03:41:04,960
thing
6112
03:41:04,960 --> 03:41:06,880
let's comment this out all right let's
6113
03:41:06,880 --> 03:41:08,720
run and hopefully nothing prints out
6114
03:41:08,720 --> 03:41:11,359
beautiful so just to recap we talked
6115
03:41:11,359 --> 03:41:12,399
about
6116
03:41:12,399 --> 03:41:14,880
map function and how you can use map to
6117
03:41:14,880 --> 03:41:16,239
double numbers
6118
03:41:16,239 --> 03:41:18,800
we talked about filter and how you could
6119
03:41:18,800 --> 03:41:21,840
filter for specific net worths we talked
6120
03:41:21,840 --> 03:41:22,960
about
6121
03:41:22,960 --> 03:41:24,160
reduce
6122
03:41:24,160 --> 03:41:26,399
and how you could reduce things to a
6123
03:41:26,399 --> 03:41:29,120
single value and sum them up together or
6124
03:41:29,120 --> 03:41:31,040
essentially how you could sum up things
6125
03:41:31,040 --> 03:41:32,560
without having to
6126
03:41:32,560 --> 03:41:34,080
you know loop through and do it all
6127
03:41:34,080 --> 03:41:36,399
manually reduce allows you to do it kind
6128
03:41:36,399 --> 03:41:39,199
of in one go so you know and we talked
6129
03:41:39,199 --> 03:41:41,680
about higher order functions how higher
6130
03:41:41,680 --> 03:41:42,960
order functions
6131
03:41:42,960 --> 03:41:45,359
work together meaning it's a function
6132
03:41:45,359 --> 03:41:46,960
that takes in another function as an
6133
03:41:46,960 --> 03:41:48,479
argument that's why it's called a higher
6134
03:41:48,479 --> 03:41:50,479
order function so we talked about those
6135
03:41:50,479 --> 03:41:53,840
there's a lot that we covered and this
6136
03:41:53,840 --> 03:41:55,520
is kind of like the part where it gets
6137
03:41:55,520 --> 03:41:57,439
kind of complex and it's like what is
6138
03:41:57,439 --> 03:41:59,199
happening things will keep getting
6139
03:41:59,199 --> 03:42:00,479
harder so i don't want to promise you
6140
03:42:00,479 --> 03:42:02,720
things will get easier but this is one
6141
03:42:02,720 --> 03:42:04,080
of the pretty challenging parts to
6142
03:42:04,080 --> 03:42:05,680
understand if you don't really grasp it
6143
03:42:05,680 --> 03:42:07,520
right away don't beat yourself up it's
6144
03:42:07,520 --> 03:42:09,439
totally normal it's okay all right
6145
03:42:09,439 --> 03:42:11,040
you're in the right place
6146
03:42:11,040 --> 03:42:12,800
with that said we're going to keep on
6147
03:42:12,800 --> 03:42:14,880
rocking and rolling and learning new
6148
03:42:14,880 --> 03:42:15,760
things
6149
03:42:15,760 --> 03:42:17,920
all right boys and girls let's get back
6150
03:42:17,920 --> 03:42:20,880
to it before we're very close to getting
6151
03:42:20,880 --> 03:42:23,600
to that rock paper scissors game for you
6152
03:42:23,600 --> 03:42:25,520
and that's going to be really exciting
6153
03:42:25,520 --> 03:42:27,279
but first things first
6154
03:42:27,279 --> 03:42:29,920
we have to work on
6155
03:42:29,920 --> 03:42:31,520
getting your
6156
03:42:31,520 --> 03:42:34,800
dom skills up okay so let's go ahead and
6157
03:42:34,800 --> 03:42:36,000
talk about
6158
03:42:36,000 --> 03:42:39,120
dom manipulation and no i'm not talking
6159
03:42:39,120 --> 03:42:41,439
about a dominatrix get your head out of
6160
03:42:41,439 --> 03:42:43,760
the gutter okay i'm talking about
6161
03:42:43,760 --> 03:42:46,560
this dom right over here all right so
6162
03:42:46,560 --> 03:42:50,239
when you see stuff in this console log
6163
03:42:50,239 --> 03:42:51,359
right here
6164
03:42:51,359 --> 03:42:54,160
that usually has to do with
6165
03:42:54,160 --> 03:42:55,359
um
6166
03:42:55,359 --> 03:42:56,960
that's just whatever is in your console
6167
03:42:56,960 --> 03:42:58,560
but when you see stuff here that's your
6168
03:42:58,560 --> 03:43:01,359
dom okay it stands for
6169
03:43:01,359 --> 03:43:04,560
document object model which
6170
03:43:04,560 --> 03:43:06,239
you never have to remember again okay
6171
03:43:06,239 --> 03:43:08,399
it's just like nerdy knowledge that
6172
03:43:08,399 --> 03:43:10,239
won't really be super useful for you in
6173
03:43:10,239 --> 03:43:11,920
the real world you'll just refer to it
6174
03:43:11,920 --> 03:43:14,399
as dom most of the times dom and it's
6175
03:43:14,399 --> 03:43:15,840
just hey this is the stuff that's
6176
03:43:15,840 --> 03:43:18,160
showing up here okay so how do i change
6177
03:43:18,160 --> 03:43:20,640
this exercise to say
6178
03:43:20,640 --> 03:43:23,760
exercise how do i say change
6179
03:43:23,760 --> 03:43:26,560
this projects here from being
6180
03:43:26,560 --> 03:43:30,080
written in black to be written in red
6181
03:43:30,080 --> 03:43:31,840
all right all of that requires you to
6182
03:43:31,840 --> 03:43:34,160
manipulate the dom
6183
03:43:34,160 --> 03:43:35,760
when we're playing rock paper scissors
6184
03:43:35,760 --> 03:43:38,000
right how come when i
6185
03:43:38,000 --> 03:43:40,560
click this button watch
6186
03:43:40,560 --> 03:43:42,319
it's going to say rock versus scissors
6187
03:43:42,319 --> 03:43:45,040
you win when i click this hand
6188
03:43:45,040 --> 03:43:47,600
let me move myself here when i click
6189
03:43:47,600 --> 03:43:50,239
this hand watch what happens
6190
03:43:50,239 --> 03:43:52,800
boom boom boom boom boom boom boom boom
6191
03:43:52,800 --> 03:43:55,760
right all of this right here my friend
6192
03:43:55,760 --> 03:43:58,640
is just dom manipulation boom dom
6193
03:43:58,640 --> 03:44:00,000
manipulation
6194
03:44:00,000 --> 03:44:02,640
this changing here when i hit this red
6195
03:44:02,640 --> 03:44:03,840
button right here watch what's gonna
6196
03:44:03,840 --> 03:44:06,880
happen boom everything got cleared up
6197
03:44:06,880 --> 03:44:09,840
that right there dom manipulation
6198
03:44:09,840 --> 03:44:11,840
okay so let's go back
6199
03:44:11,840 --> 03:44:13,680
so that's what we're gonna work on okay
6200
03:44:13,680 --> 03:44:15,680
how do we actually make stuff happen on
6201
03:44:15,680 --> 03:44:17,600
the screen how do we change stuff on the
6202
03:44:17,600 --> 03:44:19,760
screen with javascript how do we mess
6203
03:44:19,760 --> 03:44:21,840
around with the html
6204
03:44:21,840 --> 03:44:23,520
with javascript
6205
03:44:23,520 --> 03:44:25,040
and we're going to play around with that
6206
03:44:25,040 --> 03:44:26,399
a little bit
6207
03:44:26,399 --> 03:44:29,199
so let's go ahead to our index.html and
6208
03:44:29,199 --> 03:44:31,840
i'm going to go ahead and add in a div
6209
03:44:31,840 --> 03:44:33,040
okay and
6210
03:44:33,040 --> 03:44:34,800
we can add in a div
6211
03:44:34,800 --> 03:44:36,640
actually let's do this to do some dom
6212
03:44:36,640 --> 03:44:38,880
manipulation some of this stuff let's do
6213
03:44:38,880 --> 03:44:39,680
it
6214
03:44:39,680 --> 03:44:42,000
also outside of this replace stuff that
6215
03:44:42,000 --> 03:44:43,279
we're doing here so you can start to
6216
03:44:43,279 --> 03:44:44,960
learn how to do things from scratch
6217
03:44:44,960 --> 03:44:47,359
yourself as well okay so just do me a
6218
03:44:47,359 --> 03:44:49,760
favor hit command t or control t on your
6219
03:44:49,760 --> 03:44:52,000
computer open a new tab and let's go to
6220
03:44:52,000 --> 03:44:54,080
replic.com
6221
03:44:54,080 --> 03:44:54,960
okay
6222
03:44:54,960 --> 03:44:57,520
and now here just open a new repl
6223
03:44:57,520 --> 03:45:00,160
okay so for example there's a multiple
6224
03:45:00,160 --> 03:45:01,920
repels let's go back
6225
03:45:01,920 --> 03:45:04,239
multiple rebels that show up here let's
6226
03:45:04,239 --> 03:45:06,560
just choose the html css and javascript
6227
03:45:06,560 --> 03:45:08,800
one okay depending on when you're
6228
03:45:08,800 --> 03:45:10,399
watching this maybe if they updated the
6229
03:45:10,399 --> 03:45:12,560
site click a plus button and then choose
6230
03:45:12,560 --> 03:45:15,520
html css and javascript okay whichever
6231
03:45:15,520 --> 03:45:16,880
way you want to do this so i'll just
6232
03:45:16,880 --> 03:45:18,239
click here
6233
03:45:18,239 --> 03:45:19,920
and what do we want to call this i'm
6234
03:45:19,920 --> 03:45:22,239
just going to say dom manipulation
6235
03:45:22,239 --> 03:45:23,439
lesson
6236
03:45:23,439 --> 03:45:24,399
all right
6237
03:45:24,399 --> 03:45:26,880
and then go ahead hit create
6238
03:45:26,880 --> 03:45:28,880
perfect it'll bring you to here
6239
03:45:28,880 --> 03:45:30,880
okay and if you hit run
6240
03:45:30,880 --> 03:45:33,120
uh right now you should see hello world
6241
03:45:33,120 --> 03:45:35,520
on the screen how are you seeing this so
6242
03:45:35,520 --> 03:45:37,040
you came with three files here
6243
03:45:37,040 --> 03:45:40,399
index.html script.js style.css now to
6244
03:45:40,399 --> 03:45:42,960
style.css don't worry about it just for
6245
03:45:42,960 --> 03:45:44,000
styling
6246
03:45:44,000 --> 03:45:45,199
the main ones that we're going to be
6247
03:45:45,199 --> 03:45:47,439
worrying about here is the html and the
6248
03:45:47,439 --> 03:45:48,560
script
6249
03:45:48,560 --> 03:45:49,680
so
6250
03:45:49,680 --> 03:45:52,880
this is pretty much standard html that
6251
03:45:52,880 --> 03:45:55,600
when you just like make any javascript
6252
03:45:55,600 --> 03:45:56,479
app
6253
03:45:56,479 --> 03:45:59,120
you you you know you you kind of start
6254
03:45:59,120 --> 03:46:00,880
off with but to make it even less
6255
03:46:00,880 --> 03:46:02,800
intimidating for you to start off let's
6256
03:46:02,800 --> 03:46:04,960
just remove almost everything we're just
6257
03:46:04,960 --> 03:46:07,359
going to leave a script tag here
6258
03:46:07,359 --> 03:46:09,520
and let's say if we can say hello world
6259
03:46:09,520 --> 03:46:11,840
in this h2 and let's hit run let's see
6260
03:46:11,840 --> 03:46:14,000
if this works so it looks like hello
6261
03:46:14,000 --> 03:46:16,399
world shows up here so this is literally
6262
03:46:16,399 --> 03:46:18,800
two lines of code okay
6263
03:46:18,800 --> 03:46:21,040
and let's even write this line of code
6264
03:46:21,040 --> 03:46:22,560
here so you understand how to link a
6265
03:46:22,560 --> 03:46:25,920
style sheet to your javascript sheet
6266
03:46:25,920 --> 03:46:27,680
okay so hello world
6267
03:46:27,680 --> 03:46:28,560
and
6268
03:46:28,560 --> 03:46:30,960
now let's go to our script.js file and
6269
03:46:30,960 --> 03:46:32,880
we'll write console.log
6270
03:46:32,880 --> 03:46:35,520
and i'll say hello and i'll hit run and
6271
03:46:35,520 --> 03:46:37,840
notice the problem that we ran into
6272
03:46:37,840 --> 03:46:38,800
right
6273
03:46:38,800 --> 03:46:41,199
where is our hello hello why isn't this
6274
03:46:41,199 --> 03:46:43,279
showing up in the console what we need
6275
03:46:43,279 --> 03:46:46,000
to do is link this style sheet
6276
03:46:46,000 --> 03:46:48,239
to this script sheet okay so we need to
6277
03:46:48,239 --> 03:46:50,720
go ahead and do that let me zoom in
6278
03:46:50,720 --> 03:46:53,199
a little bit so you can see everything a
6279
03:46:53,199 --> 03:46:55,680
lot more clearly so let's go ahead and
6280
03:46:55,680 --> 03:46:57,680
do that and i'm going to link this
6281
03:46:57,680 --> 03:47:00,000
okay move this over here
6282
03:47:00,000 --> 03:47:02,880
so we're going to say script hit tab
6283
03:47:02,880 --> 03:47:04,640
and it'll create these
6284
03:47:04,640 --> 03:47:06,160
tabs for you right
6285
03:47:06,160 --> 03:47:08,399
and then we're going to say script
6286
03:47:08,399 --> 03:47:10,720
source is and for source i'm going to
6287
03:47:10,720 --> 03:47:12,800
say script.js because this is the name
6288
03:47:12,800 --> 03:47:14,160
of this file
6289
03:47:14,160 --> 03:47:16,239
you could call this whatever you want as
6290
03:47:16,239 --> 03:47:17,920
long as you change the name here for
6291
03:47:17,920 --> 03:47:18,880
script
6292
03:47:18,880 --> 03:47:20,560
let's hit run
6293
03:47:20,560 --> 03:47:20,840
and
6294
03:47:20,840 --> 03:47:22,560
[Music]
6295
03:47:22,560 --> 03:47:25,359
now you see hello over here is showing
6296
03:47:25,359 --> 03:47:26,160
up
6297
03:47:26,160 --> 03:47:27,760
okay
6298
03:47:27,760 --> 03:47:29,120
perfect
6299
03:47:29,120 --> 03:47:32,000
so we have linked our index.html with
6300
03:47:32,000 --> 03:47:34,040
our
6301
03:47:34,040 --> 03:47:35,920
script.js
6302
03:47:35,920 --> 03:47:38,319
okay now let's do basic dom manipulation
6303
03:47:38,319 --> 03:47:40,080
okay how are we going to do this well
6304
03:47:40,080 --> 03:47:42,880
what i want to do is this h2 i'm going
6305
03:47:42,880 --> 03:47:45,040
to give it an id and we're just going to
6306
03:47:45,040 --> 03:47:46,880
call it title and then we're going to
6307
03:47:46,880 --> 03:47:49,600
come here and i'm going to say let
6308
03:47:49,600 --> 03:47:50,880
title
6309
03:47:50,880 --> 03:47:51,840
equal
6310
03:47:51,840 --> 03:47:52,960
and
6311
03:47:52,960 --> 03:47:54,720
what's cool about javascript is you can
6312
03:47:54,720 --> 03:47:55,520
do something called
6313
03:47:55,520 --> 03:47:58,520
document.getelementbyid
6314
03:47:58,880 --> 03:48:00,720
so we're gonna pull
6315
03:48:00,720 --> 03:48:02,720
this specific element right over here
6316
03:48:02,720 --> 03:48:06,239
this h2 right here by putting in the id
6317
03:48:06,239 --> 03:48:08,239
so what's the id
6318
03:48:08,239 --> 03:48:09,920
can you guess
6319
03:48:09,920 --> 03:48:11,760
right it's title
6320
03:48:11,760 --> 03:48:12,560
okay
6321
03:48:12,560 --> 03:48:15,600
so i'm gonna get this id and then let's
6322
03:48:15,600 --> 03:48:18,160
go ahead and console.log this and let's
6323
03:48:18,160 --> 03:48:19,840
see what it shows us on the screen so
6324
03:48:19,840 --> 03:48:21,920
i'm gonna say console.log.title
6325
03:48:21,920 --> 03:48:23,439
let's run this
6326
03:48:23,439 --> 03:48:25,600
um sometimes if your app kind of breaks
6327
03:48:25,600 --> 03:48:27,279
out like this just hit
6328
03:48:27,279 --> 03:48:28,960
just go ahead and hit refresh so i'm
6329
03:48:28,960 --> 03:48:32,080
going to refresh here not a big problem
6330
03:48:32,080 --> 03:48:33,760
all right let's run it again
6331
03:48:33,760 --> 03:48:36,160
and you can see that it says hello and
6332
03:48:36,160 --> 03:48:38,880
then it says html heading element so
6333
03:48:38,880 --> 03:48:40,880
it's it's showing us something that's a
6334
03:48:40,880 --> 03:48:43,040
really good sign now what i want to do
6335
03:48:43,040 --> 03:48:45,600
is i'm going to say take the title
6336
03:48:45,600 --> 03:48:48,399
okay and we're going to see if we can
6337
03:48:48,399 --> 03:48:50,800
grab its title dot inner text so let's
6338
03:48:50,800 --> 03:48:53,120
try this let's see if we can do this
6339
03:48:53,120 --> 03:48:55,600
ah so i'm actually able to pull the text
6340
03:48:55,600 --> 03:48:57,199
of this title so now let's go to our
6341
03:48:57,199 --> 03:49:00,239
html and change this hello world to
6342
03:49:00,239 --> 03:49:01,600
hello world
6343
03:49:01,600 --> 03:49:02,720
hello
6344
03:49:02,720 --> 03:49:04,000
knight
6345
03:49:04,000 --> 03:49:05,760
and let's run
6346
03:49:05,760 --> 03:49:07,760
and and you can see that we're actually
6347
03:49:07,760 --> 03:49:10,560
pulling hello knight from here
6348
03:49:10,560 --> 03:49:12,560
right from html
6349
03:49:12,560 --> 03:49:14,319
we're pulling that and reading it in our
6350
03:49:14,319 --> 03:49:16,880
javascript so that's a really good
6351
03:49:16,880 --> 03:49:18,319
sign means
6352
03:49:18,319 --> 03:49:20,720
we can actually now do stuff with it now
6353
03:49:20,720 --> 03:49:22,720
here's the cool part how can we go the
6354
03:49:22,720 --> 03:49:26,720
other way how can we update our html
6355
03:49:26,720 --> 03:49:28,800
from our javascript and then it'll show
6356
03:49:28,800 --> 03:49:30,800
up here so let's go ahead and do that
6357
03:49:30,800 --> 03:49:33,920
now i'm going to say title dot inner
6358
03:49:33,920 --> 03:49:35,040
text
6359
03:49:35,040 --> 03:49:36,880
and let's just change it to whatever we
6360
03:49:36,880 --> 03:49:39,199
want okay so let's say we want our inner
6361
03:49:39,199 --> 03:49:41,040
text to say
6362
03:49:41,040 --> 03:49:43,520
goodbye
6363
03:49:43,520 --> 03:49:46,239
so let's say we wanted to say goodbye
6364
03:49:46,239 --> 03:49:47,920
my lover
6365
03:49:47,920 --> 03:49:52,399
goodbye my lover goodbye my friend you
6366
03:49:52,399 --> 03:49:54,479
have been the one
6367
03:49:54,479 --> 03:49:56,800
there we go so look at that right now
6368
03:49:56,800 --> 03:49:58,880
i'm gonna i'm gonna let's um
6369
03:49:58,880 --> 03:50:00,239
let's run this
6370
03:50:00,239 --> 03:50:02,560
and you see for a fraction of a second
6371
03:50:02,560 --> 03:50:04,960
it'll say hello
6372
03:50:04,960 --> 03:50:07,199
night and then it says goodbye my lover
6373
03:50:07,199 --> 03:50:09,520
so let's run this
6374
03:50:09,520 --> 03:50:11,199
fraction of a second and then it goes to
6375
03:50:11,199 --> 03:50:12,720
go down my lover
6376
03:50:12,720 --> 03:50:14,319
so
6377
03:50:14,319 --> 03:50:16,640
we're printing out the text before
6378
03:50:16,640 --> 03:50:18,640
changing it let's also print it out
6379
03:50:18,640 --> 03:50:20,880
after changing it so we're gonna say
6380
03:50:20,880 --> 03:50:23,279
okay and in our console log here
6381
03:50:23,279 --> 03:50:25,120
we're gonna say before
6382
03:50:25,120 --> 03:50:28,560
like that and then here we'll say after
6383
03:50:28,560 --> 03:50:30,640
like that
6384
03:50:30,640 --> 03:50:31,760
and
6385
03:50:31,760 --> 03:50:34,720
let's run this now and you can see
6386
03:50:34,720 --> 03:50:36,479
before it was hello at night and then
6387
03:50:36,479 --> 03:50:39,439
after it became good by my lover so that
6388
03:50:39,439 --> 03:50:40,319
is
6389
03:50:40,319 --> 03:50:43,600
the heart and soul of dom manipulation
6390
03:50:43,600 --> 03:50:46,160
okay this is this is what it is now if
6391
03:50:46,160 --> 03:50:48,640
you want to change the html you can do
6392
03:50:48,640 --> 03:50:50,319
that as well so watch this i can
6393
03:50:50,319 --> 03:50:52,080
actually go ahead and do
6394
03:50:52,080 --> 03:50:54,239
title.inner html so i can actually
6395
03:50:54,239 --> 03:50:57,279
change its html
6396
03:50:57,279 --> 03:50:59,040
okay so
6397
03:50:59,040 --> 03:51:01,680
it was an h2 right i could actually say
6398
03:51:01,680 --> 03:51:03,760
put it in p tags
6399
03:51:03,760 --> 03:51:05,840
and change it so let's say
6400
03:51:05,840 --> 03:51:08,239
we'll make a message so we'll say co let
6401
03:51:08,239 --> 03:51:10,000
message
6402
03:51:10,000 --> 03:51:13,279
equal and here i'll put goodbye my lover
6403
03:51:13,279 --> 03:51:15,040
that'll be the message and then we'll
6404
03:51:15,040 --> 03:51:18,319
say it's whatever the message is right
6405
03:51:18,319 --> 03:51:19,520
boom
6406
03:51:19,520 --> 03:51:20,960
so
6407
03:51:20,960 --> 03:51:22,720
let's comment out this last line right
6408
03:51:22,720 --> 03:51:25,359
now and let's go ahead and run this and
6409
03:51:25,359 --> 03:51:26,800
watch what happens
6410
03:51:26,800 --> 03:51:28,399
okay so i should get pretty much the
6411
03:51:28,399 --> 03:51:30,160
same exact thing all i've done is create
6412
03:51:30,160 --> 03:51:32,720
a variable message putting it in here
6413
03:51:32,720 --> 03:51:34,640
now watch what i can do
6414
03:51:34,640 --> 03:51:37,520
i can put that same message within these
6415
03:51:37,520 --> 03:51:40,479
p tags okay so first let's just start
6416
03:51:40,479 --> 03:51:42,640
off by doing hello just so we can get a
6417
03:51:42,640 --> 03:51:44,640
sense of how this is working
6418
03:51:44,640 --> 03:51:46,080
and you can see that we've changed our
6419
03:51:46,080 --> 03:51:48,960
inner html to this all right so notice
6420
03:51:48,960 --> 03:51:50,640
the problem we're running into right
6421
03:51:50,640 --> 03:51:52,880
when i do p tags it's still showing
6422
03:51:52,880 --> 03:51:56,720
title tags it's because it's inner html
6423
03:51:56,720 --> 03:51:59,040
okay so if i go back to this tag here
6424
03:51:59,040 --> 03:52:01,120
this doesn't have any other inner
6425
03:52:01,120 --> 03:52:02,479
elements
6426
03:52:02,479 --> 03:52:04,160
if it had other inner elements it'd be
6427
03:52:04,160 --> 03:52:05,680
able to change them
6428
03:52:05,680 --> 03:52:08,479
so there's nothing for it to change here
6429
03:52:08,479 --> 03:52:09,600
okay so here's what we want to do
6430
03:52:09,600 --> 03:52:10,640
instead
6431
03:52:10,640 --> 03:52:13,439
we want to actually wrap this entire h2
6432
03:52:13,439 --> 03:52:14,880
in a div
6433
03:52:14,880 --> 03:52:16,560
all right so let's go ahead and copy
6434
03:52:16,560 --> 03:52:19,359
pick um cut that paste it here so now
6435
03:52:19,359 --> 03:52:21,359
here's what it should look like
6436
03:52:21,359 --> 03:52:23,840
you should have one div
6437
03:52:23,840 --> 03:52:26,399
and that div should have a h2 inside of
6438
03:52:26,399 --> 03:52:28,720
it that's it okay one last thing we need
6439
03:52:28,720 --> 03:52:32,080
to do is we need to give the id not to
6440
03:52:32,080 --> 03:52:33,600
the h2
6441
03:52:33,600 --> 03:52:35,600
but instead we need to give the id to
6442
03:52:35,600 --> 03:52:37,520
the div okay so the div should have the
6443
03:52:37,520 --> 03:52:40,479
id not the h2 let's go ahead and try
6444
03:52:40,479 --> 03:52:43,439
again so now i'm grabbing this one when
6445
03:52:43,439 --> 03:52:45,520
i say get me
6446
03:52:45,520 --> 03:52:48,160
um when i said get me title
6447
03:52:48,160 --> 03:52:50,720
it's getting me this div right over here
6448
03:52:50,720 --> 03:52:51,840
and then when i say change the inner
6449
03:52:51,840 --> 03:52:53,680
html it changes this
6450
03:52:53,680 --> 03:52:55,760
and to be even more clear what i can
6451
03:52:55,760 --> 03:52:57,680
right click and rename all occurrences
6452
03:52:57,680 --> 03:52:59,760
and we can call it title div
6453
03:52:59,760 --> 03:53:01,520
okay so what i'm doing is i'm grabbing
6454
03:53:01,520 --> 03:53:04,080
the title div and then changing its
6455
03:53:04,080 --> 03:53:07,600
inner text and its inner html so let's
6456
03:53:07,600 --> 03:53:09,760
go ahead and give that a try now
6457
03:53:09,760 --> 03:53:12,479
and you can see that i get back hello
6458
03:53:12,479 --> 03:53:14,479
now in order to make this hello instead
6459
03:53:14,479 --> 03:53:16,479
of hello this message show up that we
6460
03:53:16,479 --> 03:53:18,000
wrote remember let's use template
6461
03:53:18,000 --> 03:53:19,520
literal so see if you could replace this
6462
03:53:19,520 --> 03:53:22,479
hard-coded hello with this message right
6463
03:53:22,479 --> 03:53:24,800
over here so take five seconds okay
6464
03:53:24,800 --> 03:53:26,319
it'll be a good practice and using
6465
03:53:26,319 --> 03:53:28,160
template literals remember to start with
6466
03:53:28,160 --> 03:53:29,359
backticks
6467
03:53:29,359 --> 03:53:31,760
and uh pause the video try to yourself
6468
03:53:31,760 --> 03:53:36,880
five four three two one and let's go
6469
03:53:36,880 --> 03:53:39,040
ahead and do this now so i'm gonna
6470
03:53:39,040 --> 03:53:41,279
replace this with backticks
6471
03:53:41,279 --> 03:53:44,720
back text right here okay and then
6472
03:53:44,720 --> 03:53:47,600
this here is gonna become a javascript
6473
03:53:47,600 --> 03:53:50,399
variable so we're gonna go like that and
6474
03:53:50,399 --> 03:53:52,479
i'm just gonna put message in here okay
6475
03:53:52,479 --> 03:53:55,040
let's run it now and you can see it says
6476
03:53:55,040 --> 03:53:57,199
goodbye my lover and then there are
6477
03:53:57,199 --> 03:53:58,880
other things i can do as well right so
6478
03:53:58,880 --> 03:54:01,760
for example i can actually grab this div
6479
03:54:01,760 --> 03:54:04,239
right here and change its style property
6480
03:54:04,239 --> 03:54:06,720
and make it red or green or blue or
6481
03:54:06,720 --> 03:54:08,720
whatever i want here's how we can do
6482
03:54:08,720 --> 03:54:10,800
that let's go ahead and do that from raw
6483
03:54:10,800 --> 03:54:14,239
html file and here for this div i'm just
6484
03:54:14,239 --> 03:54:16,239
gonna pass in a style attribute i'm
6485
03:54:16,239 --> 03:54:18,960
gonna say style and we're going to put a
6486
03:54:18,960 --> 03:54:20,399
string here and i'm going to say color
6487
03:54:20,399 --> 03:54:23,120
make the color blue so let's run this
6488
03:54:23,120 --> 03:54:25,840
color is blue and put a semicolon here
6489
03:54:25,840 --> 03:54:27,840
and run it don't put blue as a string
6490
03:54:27,840 --> 03:54:29,760
because it's already a string
6491
03:54:29,760 --> 03:54:32,479
and there we go goodbye my lover okay if
6492
03:54:32,479 --> 03:54:34,720
i wanted this to be the background
6493
03:54:34,720 --> 03:54:37,439
change the background color instead
6494
03:54:37,439 --> 03:54:39,520
this is how i could do it
6495
03:54:39,520 --> 03:54:40,960
there we go so we could do background
6496
03:54:40,960 --> 03:54:43,279
color like this okay so let's go back
6497
03:54:43,279 --> 03:54:45,520
to how we had this right here
6498
03:54:45,520 --> 03:54:47,439
okay so now in order to do that i'm
6499
03:54:47,439 --> 03:54:49,199
going to remove this entire thing let's
6500
03:54:49,199 --> 03:54:51,920
do it from our javascript file instead
6501
03:54:51,920 --> 03:54:54,479
so i'll go to my script.js and if you
6502
03:54:54,479 --> 03:54:55,600
want to move around in a really
6503
03:54:55,600 --> 03:54:56,880
comfortable way you could actually do
6504
03:54:56,880 --> 03:54:59,120
command p and then do script.js and
6505
03:54:59,120 --> 03:55:00,560
command p
6506
03:55:00,560 --> 03:55:02,239
so this allows you to kind of move
6507
03:55:02,239 --> 03:55:04,960
through without even having to use this
6508
03:55:04,960 --> 03:55:07,120
um you know navigate through files like
6509
03:55:07,120 --> 03:55:09,359
that
6510
03:55:09,359 --> 03:55:12,479
so here i'm going to do title div dot
6511
03:55:12,479 --> 03:55:15,199
style dot color okay and this actually
6512
03:55:15,199 --> 03:55:17,040
will get me the
6513
03:55:17,040 --> 03:55:19,840
uh color property
6514
03:55:19,840 --> 03:55:22,720
of uh that div okay whatever color it is
6515
03:55:22,720 --> 03:55:24,640
or whatever and what i can do is change
6516
03:55:24,640 --> 03:55:26,560
it so watch this actually
6517
03:55:26,560 --> 03:55:29,359
so i could say color and let's go red
6518
03:55:29,359 --> 03:55:31,760
let's run this and i just made the color
6519
03:55:31,760 --> 03:55:34,399
of this div red okay
6520
03:55:34,399 --> 03:55:36,080
now another thing i could do is i could
6521
03:55:36,080 --> 03:55:37,840
change the background color and how we
6522
03:55:37,840 --> 03:55:39,680
could do that it says we could we can't
6523
03:55:39,680 --> 03:55:40,560
put
6524
03:55:40,560 --> 03:55:42,880
dashes inside of a variable name
6525
03:55:42,880 --> 03:55:45,279
i could do background color like this
6526
03:55:45,279 --> 03:55:47,040
and then we could hit run
6527
03:55:47,040 --> 03:55:48,640
and now it changes the background color
6528
03:55:48,640 --> 03:55:50,000
of it to red
6529
03:55:50,000 --> 03:55:51,760
okay so there's a lot of stuff that we
6530
03:55:51,760 --> 03:55:54,000
could actually do
6531
03:55:54,000 --> 03:55:56,399
but i think the few key things that i
6532
03:55:56,399 --> 03:55:58,479
want to show you more is actually like
6533
03:55:58,479 --> 03:56:00,960
on click all right and how that works so
6534
03:56:00,960 --> 03:56:02,080
that's going to be another really
6535
03:56:02,080 --> 03:56:03,359
important one and you're definitely
6536
03:56:03,359 --> 03:56:05,359
going to need to know that for
6537
03:56:05,359 --> 03:56:07,600
tic tac to uh sorry rock paper scissors
6538
03:56:07,600 --> 03:56:09,600
so let's make sure we have
6539
03:56:09,600 --> 03:56:11,520
we have this figured out
6540
03:56:11,520 --> 03:56:15,120
in order to do to to do this exercise
6541
03:56:15,120 --> 03:56:17,040
i want to give you context so here's the
6542
03:56:17,040 --> 03:56:18,239
context
6543
03:56:18,239 --> 03:56:21,279
in our rock paper scissors when you go
6544
03:56:21,279 --> 03:56:22,399
here
6545
03:56:22,399 --> 03:56:23,279
okay
6546
03:56:23,279 --> 03:56:25,199
and you run this
6547
03:56:25,199 --> 03:56:26,560
app
6548
03:56:26,560 --> 03:56:29,199
okay let's go ahead and open this up
6549
03:56:29,199 --> 03:56:32,000
actually in its own browser
6550
03:56:32,000 --> 03:56:33,680
let's go right here rock paper scissors
6551
03:56:33,680 --> 03:56:36,319
right watch what happens
6552
03:56:36,319 --> 03:56:39,040
when i click here it detects that i've
6553
03:56:39,040 --> 03:56:40,960
actually clicked rock
6554
03:56:40,960 --> 03:56:42,800
when i click here it detects that i've
6555
03:56:42,800 --> 03:56:45,199
actually clicked a hand and when i click
6556
03:56:45,199 --> 03:56:47,760
here it can actually detect that i've
6557
03:56:47,760 --> 03:56:48,800
clicked
6558
03:56:48,800 --> 03:56:49,920
scissors
6559
03:56:49,920 --> 03:56:51,120
so we're going to
6560
03:56:51,120 --> 03:56:53,359
do like a baby exercise that's gonna
6561
03:56:53,359 --> 03:56:55,040
highlight this okay so we're gonna make
6562
03:56:55,040 --> 03:56:56,080
a
6563
03:56:56,080 --> 03:56:58,399
we're gonna make three squares
6564
03:56:58,399 --> 03:57:00,800
and each square is gonna have a
6565
03:57:00,800 --> 03:57:02,080
different color
6566
03:57:02,080 --> 03:57:04,720
all right so we're gonna have a
6567
03:57:04,720 --> 03:57:08,319
red square a green square okay a green
6568
03:57:08,319 --> 03:57:11,359
square and a yellow
6569
03:57:11,359 --> 03:57:12,640
square
6570
03:57:12,640 --> 03:57:15,040
okay you could do it in whichever order
6571
03:57:15,040 --> 03:57:17,199
and what we would want to do is when the
6572
03:57:17,199 --> 03:57:19,439
user clicks
6573
03:57:19,439 --> 03:57:21,199
when the user clicks on one of these
6574
03:57:21,199 --> 03:57:22,399
squares
6575
03:57:22,399 --> 03:57:24,720
we just want to console.log
6576
03:57:24,720 --> 03:57:26,960
that property value so we want to read
6577
03:57:26,960 --> 03:57:29,199
it we want to be able to we want we want
6578
03:57:29,199 --> 03:57:31,520
it to say in the console log red
6579
03:57:31,520 --> 03:57:33,840
right when we click yellow it should say
6580
03:57:33,840 --> 03:57:35,439
yellow at the bottom right when we click
6581
03:57:35,439 --> 03:57:37,439
green it should say green at the bottom
6582
03:57:37,439 --> 03:57:39,520
it should be able to detect
6583
03:57:39,520 --> 03:57:42,800
which square the user clicked on and be
6584
03:57:42,800 --> 03:57:45,680
able to pull out that square's value so
6585
03:57:45,680 --> 03:57:46,560
this is going to be an important
6586
03:57:46,560 --> 03:57:48,399
exercise because once you can do this
6587
03:57:48,399 --> 03:57:50,720
you'll be able to attach and do more
6588
03:57:50,720 --> 03:57:52,080
functionality with the rock paper
6589
03:57:52,080 --> 03:57:53,279
scissors button because remember when
6590
03:57:53,279 --> 03:57:55,840
you click this a bunch of stuff happens
6591
03:57:55,840 --> 03:57:57,279
but in order to make that stuff happen
6592
03:57:57,279 --> 03:57:59,359
you first need to detect
6593
03:57:59,359 --> 03:58:01,359
that a button was clicked and you have
6594
03:58:01,359 --> 03:58:03,279
to detect which button was clicked was
6595
03:58:03,279 --> 03:58:05,359
it the rock one was it the paper one or
6596
03:58:05,359 --> 03:58:07,439
was it the scissors one so let's go
6597
03:58:07,439 --> 03:58:09,279
ahead and do this
6598
03:58:09,279 --> 03:58:12,000
uh red yellow green exercise
6599
03:58:12,000 --> 03:58:13,920
so in order to do this we're gonna go
6600
03:58:13,920 --> 03:58:16,479
back to our exercise page that we had
6601
03:58:16,479 --> 03:58:17,439
open
6602
03:58:17,439 --> 03:58:19,560
on this exercise we're gonna go to our
6603
03:58:19,560 --> 03:58:21,520
index.html and we're gonna make three
6604
03:58:21,520 --> 03:58:24,160
dips let's go div one
6605
03:58:24,160 --> 03:58:26,880
we're gonna make a div
6606
03:58:26,880 --> 03:58:29,600
two and we're gonna make div three
6607
03:58:29,600 --> 03:58:32,800
and this is going to have an id of red
6608
03:58:32,800 --> 03:58:35,279
this is going to have an id of
6609
03:58:35,279 --> 03:58:36,399
green
6610
03:58:36,399 --> 03:58:39,040
or sorry yellow yellow
6611
03:58:39,040 --> 03:58:42,000
and this is going to have an id of
6612
03:58:42,000 --> 03:58:42,880
green
6613
03:58:42,880 --> 03:58:44,800
now we want to give this a width and a
6614
03:58:44,800 --> 03:58:45,600
height
6615
03:58:45,600 --> 03:58:47,040
okay so we want to give each of these
6616
03:58:47,040 --> 03:58:49,359
with uh divs a width and a height so for
6617
03:58:49,359 --> 03:58:50,479
width
6618
03:58:50,479 --> 03:58:54,399
we'll say 100 for height we'll say 100
6619
03:58:54,399 --> 03:58:56,000
and then we want to give it a color and
6620
03:58:56,000 --> 03:58:58,160
so overall says style and we'll say
6621
03:58:58,160 --> 03:59:00,560
color is
6622
03:59:00,560 --> 03:59:03,439
red okay because this is a red one
6623
03:59:03,439 --> 03:59:06,000
and put a semicolon here and let's run
6624
03:59:06,000 --> 03:59:08,239
this and let's see if something happens
6625
03:59:08,239 --> 03:59:09,279
okay
6626
03:59:09,279 --> 03:59:12,399
so nothing has happened yet all right so
6627
03:59:12,399 --> 03:59:14,640
now let's make this red square over here
6628
03:59:14,640 --> 03:59:16,239
okay how are we going to do this so if i
6629
03:59:16,239 --> 03:59:18,160
hit refresh obviously that shouldn't be
6630
03:59:18,160 --> 03:59:19,199
there
6631
03:59:19,199 --> 03:59:20,720
i just made it to show you that it works
6632
03:59:20,720 --> 03:59:22,399
so let's go ahead and style this so what
6633
03:59:22,399 --> 03:59:23,680
are we going to do so i'm going to
6634
03:59:23,680 --> 03:59:25,439
create a style tag
6635
03:59:25,439 --> 03:59:27,680
and then we're going to give it a height
6636
03:59:27,680 --> 03:59:30,160
of 100 pixels
6637
03:59:30,160 --> 03:59:32,239
put a semicolon here we're going to give
6638
03:59:32,239 --> 03:59:34,080
it a width
6639
03:59:34,080 --> 03:59:37,199
of 100 pixels semicolon there and we're
6640
03:59:37,199 --> 03:59:39,199
going to give it a
6641
03:59:39,199 --> 03:59:41,040
background color
6642
03:59:41,040 --> 03:59:42,239
of
6643
03:59:42,239 --> 03:59:44,560
red i don't need to put a string here
6644
03:59:44,560 --> 03:59:46,319
because it's all a string
6645
03:59:46,319 --> 03:59:48,000
let's run this
6646
03:59:48,000 --> 03:59:50,000
and we have our red square so i'm just
6647
03:59:50,000 --> 03:59:51,840
going to copy this
6648
03:59:51,840 --> 03:59:54,560
paste it over this and paste it over
6649
03:59:54,560 --> 03:59:56,000
this so now i should have three red
6650
03:59:56,000 --> 03:59:57,600
squares when i run it
6651
03:59:57,600 --> 03:59:59,359
okay perfect now of course we're just
6652
03:59:59,359 --> 04:00:01,199
going to make it called yellow this one
6653
04:00:01,199 --> 04:00:03,359
we're going to make it green
6654
04:00:03,359 --> 04:00:06,000
okay and id and then we also need to
6655
04:00:06,000 --> 04:00:07,680
make the color yellow
6656
04:00:07,680 --> 04:00:09,199
and then we need to make the background
6657
04:00:09,199 --> 04:00:10,800
color green here
6658
04:00:10,800 --> 04:00:12,160
let's run
6659
04:00:12,160 --> 04:00:13,920
and you can see i have these three
6660
04:00:13,920 --> 04:00:15,359
squares
6661
04:00:15,359 --> 04:00:17,439
now what we want to do is we want these
6662
04:00:17,439 --> 04:00:19,680
to show up side by side instead of up
6663
04:00:19,680 --> 04:00:21,279
and down like this so this is a little
6664
04:00:21,279 --> 04:00:22,720
bit of styling stuff but we're going to
6665
04:00:22,720 --> 04:00:24,479
be really gross and we're going to style
6666
04:00:24,479 --> 04:00:26,640
everything in line styling
6667
04:00:26,640 --> 04:00:29,120
don't ever style like this okay but just
6668
04:00:29,120 --> 04:00:31,040
for now we're just going to style like
6669
04:00:31,040 --> 04:00:32,800
this so let's put all of these three
6670
04:00:32,800 --> 04:00:36,000
boxes inside of another big box okay so
6671
04:00:36,000 --> 04:00:37,600
let's put them inside of a container so
6672
04:00:37,600 --> 04:00:40,000
just wrap all three of those
6673
04:00:40,000 --> 04:00:42,080
um let's see if i could show it to you
6674
04:00:42,080 --> 04:00:43,760
in a clear way
6675
04:00:43,760 --> 04:00:44,640
cool
6676
04:00:44,640 --> 04:00:46,160
so
6677
04:00:46,160 --> 04:00:48,000
here you have your red div here you have
6678
04:00:48,000 --> 04:00:49,680
your yellow div here you have a green
6679
04:00:49,680 --> 04:00:50,960
div right
6680
04:00:50,960 --> 04:00:53,600
we're gonna put all three of those like
6681
04:00:53,600 --> 04:00:55,439
boxes that you created inside of this
6682
04:00:55,439 --> 04:00:56,960
big div right here
6683
04:00:56,960 --> 04:00:58,720
and then we're gonna style this one and
6684
04:00:58,720 --> 04:01:00,960
we're gonna say display
6685
04:01:00,960 --> 04:01:02,000
flex
6686
04:01:02,000 --> 04:01:03,040
run it
6687
04:01:03,040 --> 04:01:05,120
and now it should show it to put it put
6688
04:01:05,120 --> 04:01:06,800
it in a flexbox or show it to you like
6689
04:01:06,800 --> 04:01:09,359
this okay now what i want you to do is
6690
04:01:09,359 --> 04:01:11,920
when i click this red one it should say
6691
04:01:11,920 --> 04:01:13,439
red when i click this yellow one it
6692
04:01:13,439 --> 04:01:15,600
should say yellow and i click this green
6693
04:01:15,600 --> 04:01:17,520
one it should say green so let's work on
6694
04:01:17,520 --> 04:01:19,600
that now how are we going to do this
6695
04:01:19,600 --> 04:01:22,399
so here's the cool thing so first let's
6696
04:01:22,399 --> 04:01:23,760
go ahead
6697
04:01:23,760 --> 04:01:26,720
let's go ahead and grab our divs so this
6698
04:01:26,720 --> 04:01:28,160
we're going to need this red div we're
6699
04:01:28,160 --> 04:01:30,319
going to need this yellow and this green
6700
04:01:30,319 --> 04:01:32,399
so what i'm going to do here
6701
04:01:32,399 --> 04:01:34,160
is see if you can do this
6702
04:01:34,160 --> 04:01:37,120
document.getelementbyid
6703
04:01:37,120 --> 04:01:38,160
and
6704
04:01:38,160 --> 04:01:40,800
use this technique here to get the red
6705
04:01:40,800 --> 04:01:43,760
div okay i'll give you five seconds here
6706
04:01:43,760 --> 04:01:44,640
five
6707
04:01:44,640 --> 04:01:48,080
four three two one okay let's do it
6708
04:01:48,080 --> 04:01:49,920
together now so i'm going to say a red
6709
04:01:49,920 --> 04:01:54,160
div or let's say cons red div
6710
04:01:54,160 --> 04:01:56,000
actually let's say let because we might
6711
04:01:56,000 --> 04:01:58,160
change this later
6712
04:01:58,160 --> 04:02:00,479
red div is
6713
04:02:00,479 --> 04:02:01,800
we're going to do
6714
04:02:01,800 --> 04:02:03,359
document.getelementbyid so let's go
6715
04:02:03,359 --> 04:02:05,439
document.getelement
6716
04:02:05,439 --> 04:02:06,800
by id
6717
04:02:06,800 --> 04:02:09,120
and we're going to pass in red here
6718
04:02:09,120 --> 04:02:11,760
let's copy this and paste and paste
6719
04:02:11,760 --> 04:02:13,760
and if you're new don't copy and paste
6720
04:02:13,760 --> 04:02:15,520
just write it all down because it's
6721
04:02:15,520 --> 04:02:18,080
going to build your pattern recognition
6722
04:02:18,080 --> 04:02:19,120
all right
6723
04:02:19,120 --> 04:02:21,680
so i'm going to grab yellow here paste
6724
04:02:21,680 --> 04:02:24,640
it here green here paste it here so now
6725
04:02:24,640 --> 04:02:26,640
we have we're grabbing the red div the
6726
04:02:26,640 --> 04:02:29,680
yellow div and the green div
6727
04:02:29,680 --> 04:02:31,199
okay and here's what we're gonna do
6728
04:02:31,199 --> 04:02:33,359
there's something called dot on click
6729
04:02:33,359 --> 04:02:34,960
okay so i'll show that to you right now
6730
04:02:34,960 --> 04:02:36,399
so let's go
6731
04:02:36,399 --> 04:02:38,000
red
6732
04:02:38,000 --> 04:02:41,439
red div dot on click okay so this will
6733
04:02:41,439 --> 04:02:44,080
detect any event that's happening when a
6734
04:02:44,080 --> 04:02:45,439
user clicks
6735
04:02:45,439 --> 04:02:47,279
and we're going to set this equal to and
6736
04:02:47,279 --> 04:02:48,479
we're going to create an anonymous
6737
04:02:48,479 --> 04:02:50,000
function here
6738
04:02:50,000 --> 04:02:53,520
and we're just going to say console log
6739
04:02:53,520 --> 04:02:55,120
and in this console log we're just going
6740
04:02:55,120 --> 04:02:57,680
to say console.log you clicked red you
6741
04:02:57,680 --> 04:02:59,600
clicked red because we already know the
6742
04:02:59,600 --> 04:03:01,279
click read right because we've grabbed
6743
04:03:01,279 --> 04:03:03,199
this entire red div
6744
04:03:03,199 --> 04:03:05,840
we grabbed this entire red div here and
6745
04:03:05,840 --> 04:03:07,840
we stored it in this variable right
6746
04:03:07,840 --> 04:03:08,640
there
6747
04:03:08,640 --> 04:03:11,439
okay so let's hit run
6748
04:03:11,439 --> 04:03:13,680
and let's click red and it says you
6749
04:03:13,680 --> 04:03:15,439
click red okay
6750
04:03:15,439 --> 04:03:17,680
and now we can just we've been removed
6751
04:03:17,680 --> 04:03:19,439
you clicked and so when i click it it
6752
04:03:19,439 --> 04:03:20,880
should say red
6753
04:03:20,880 --> 04:03:22,080
now watch what happens when i click
6754
04:03:22,080 --> 04:03:24,080
yellow nothing happens right when i
6755
04:03:24,080 --> 04:03:27,359
click green nothing happens so red knows
6756
04:03:27,359 --> 04:03:29,359
its own bounds and it can detect when
6757
04:03:29,359 --> 04:03:31,040
it's being clicked on thanks to this dot
6758
04:03:31,040 --> 04:03:32,239
on click
6759
04:03:32,239 --> 04:03:33,760
and then we just pass this anonymous
6760
04:03:33,760 --> 04:03:35,279
function and then we can do stuff here
6761
04:03:35,279 --> 04:03:36,239
right so
6762
04:03:36,239 --> 04:03:37,760
if you wanted to be able to write
6763
04:03:37,760 --> 04:03:39,760
multiple lines of code here you could do
6764
04:03:39,760 --> 04:03:40,560
this
6765
04:03:40,560 --> 04:03:43,600
you know console log red say
6766
04:03:43,600 --> 04:03:45,760
hi whatever right so then you could run
6767
04:03:45,760 --> 04:03:47,520
multiple lines of code like this and you
6768
04:03:47,520 --> 04:03:49,120
could run them
6769
04:03:49,120 --> 04:03:50,960
so that says read high every single time
6770
04:03:50,960 --> 04:03:53,040
i click this
6771
04:03:53,040 --> 04:03:55,760
the other way of writing this here
6772
04:03:55,760 --> 04:03:57,520
that we're writing the short that we're
6773
04:03:57,520 --> 04:03:59,359
writing here the other way of writing
6774
04:03:59,359 --> 04:04:01,359
this would be to say
6775
04:04:01,359 --> 04:04:03,600
give me a function here
6776
04:04:03,600 --> 04:04:05,600
with no arguments and then that just
6777
04:04:05,600 --> 04:04:08,319
console logs it says red
6778
04:04:08,319 --> 04:04:09,600
okay so that would be another way of
6779
04:04:09,600 --> 04:04:11,359
doing this so when i click red now it
6780
04:04:11,359 --> 04:04:13,600
still says red okay this is kind of like
6781
04:04:13,600 --> 04:04:15,520
the old school way of writing functions
6782
04:04:15,520 --> 04:04:17,120
and you could still do that
6783
04:04:17,120 --> 04:04:19,520
but arrow functions are
6784
04:04:19,520 --> 04:04:22,000
just make things look cleaner
6785
04:04:22,000 --> 04:04:23,040
there we go
6786
04:04:23,040 --> 04:04:25,359
try see if you can try to do the same
6787
04:04:25,359 --> 04:04:26,960
thing for yellow div so when i click
6788
04:04:26,960 --> 04:04:28,000
yellow
6789
04:04:28,000 --> 04:04:30,080
it detects that i'm clicking yellow and
6790
04:04:30,080 --> 04:04:31,439
see if you can do the same thing for
6791
04:04:31,439 --> 04:04:33,199
green so when i click green it detects
6792
04:04:33,199 --> 04:04:34,720
that i'm clicking green i'll give you
6793
04:04:34,720 --> 04:04:35,920
five seconds
6794
04:04:35,920 --> 04:04:37,040
five
6795
04:04:37,040 --> 04:04:38,000
four
6796
04:04:38,000 --> 04:04:39,120
three
6797
04:04:39,120 --> 04:04:40,000
two
6798
04:04:40,000 --> 04:04:41,120
one
6799
04:04:41,120 --> 04:04:43,040
all right let's try it together so you
6800
04:04:43,040 --> 04:04:44,239
know try pausing the video if you want
6801
04:04:44,239 --> 04:04:45,760
to try it yourself or let's go ahead and
6802
04:04:45,760 --> 04:04:48,479
do it together now so i'm just gonna
6803
04:04:48,479 --> 04:04:51,680
go copy here paste paste
6804
04:04:51,680 --> 04:04:53,840
and then this red i'm gonna change it
6805
04:04:53,840 --> 04:04:56,800
with yellow and then i'm gonna copy that
6806
04:04:56,800 --> 04:04:59,439
yellow right there and paste it here and
6807
04:04:59,439 --> 04:05:01,439
this red here i'm gonna change it with
6808
04:05:01,439 --> 04:05:02,960
green
6809
04:05:02,960 --> 04:05:04,880
and i'm going to copy that green and
6810
04:05:04,880 --> 04:05:06,479
paste it here
6811
04:05:06,479 --> 04:05:09,520
okay so let's try running this and watch
6812
04:05:09,520 --> 04:05:12,000
what happens so let's go run this now
6813
04:05:12,000 --> 04:05:14,319
when i click red it detects red
6814
04:05:14,319 --> 04:05:16,479
beautiful when i click yellow it detects
6815
04:05:16,479 --> 04:05:18,399
yellow and look at how sensitive is
6816
04:05:18,399 --> 04:05:19,279
right
6817
04:05:19,279 --> 04:05:21,359
even when i click right
6818
04:05:21,359 --> 04:05:24,399
right here so close it detects it like
6819
04:05:24,399 --> 04:05:25,600
look at that
6820
04:05:25,600 --> 04:05:27,359
when i click when i move just a little
6821
04:05:27,359 --> 04:05:29,600
bit over it detects red when i move just
6822
04:05:29,600 --> 04:05:31,279
a little bit over it detects the yellow
6823
04:05:31,279 --> 04:05:33,040
so it's really good and now let's try
6824
04:05:33,040 --> 04:05:35,520
green that's what we want okay because
6825
04:05:35,520 --> 04:05:37,439
what what you guys are gonna do is
6826
04:05:37,439 --> 04:05:40,239
imagine instead of red this was a rock
6827
04:05:40,239 --> 04:05:42,720
so what you're gonna say is
6828
04:05:42,720 --> 04:05:44,960
well
6829
04:05:44,960 --> 04:05:46,160
you know
6830
04:05:46,160 --> 04:05:47,920
let's say
6831
04:05:47,920 --> 04:05:49,439
uh
6832
04:05:49,439 --> 04:05:50,399
you say
6833
04:05:50,399 --> 04:05:51,760
user
6834
04:05:51,760 --> 04:05:53,760
chose
6835
04:05:53,760 --> 04:05:55,359
rock
6836
04:05:55,359 --> 04:05:58,319
okay so that so when you click red let's
6837
04:05:58,319 --> 04:06:00,399
run this first you click red it says
6838
04:06:00,399 --> 04:06:02,000
user chose rock
6839
04:06:02,000 --> 04:06:04,160
and then maybe
6840
04:06:04,160 --> 04:06:07,680
what happens next is you calculate
6841
04:06:07,680 --> 04:06:08,880
um
6842
04:06:08,880 --> 04:06:10,560
you know random
6843
04:06:10,560 --> 04:06:12,960
you get random computer choice
6844
04:06:12,960 --> 04:06:15,040
so maybe random computer choice says the
6845
04:06:15,040 --> 04:06:16,880
computer selected
6846
04:06:16,880 --> 04:06:19,840
you know maybe paper okay and then what
6847
04:06:19,840 --> 04:06:22,399
do you do you compare these two against
6848
04:06:22,399 --> 04:06:24,840
each other so you compare user
6849
04:06:24,840 --> 04:06:27,520
choice uh versus
6850
04:06:27,520 --> 04:06:30,479
computer choice um and then you figure
6851
04:06:30,479 --> 04:06:33,199
out a player choice versus computer
6852
04:06:33,199 --> 04:06:34,479
choice and then you figure out like
6853
04:06:34,479 --> 04:06:36,560
which one is better so then you put them
6854
04:06:36,560 --> 04:06:38,319
against each other and you
6855
04:06:38,319 --> 04:06:40,800
you find out like what the result is
6856
04:06:40,800 --> 04:06:42,080
right
6857
04:06:42,080 --> 04:06:43,040
maybe
6858
04:06:43,040 --> 04:06:44,399
you know if you lost you should get
6859
04:06:44,399 --> 04:06:46,560
negative one if it was a draw you get a
6860
04:06:46,560 --> 04:06:49,520
zero or you get a one if you won
6861
04:06:49,520 --> 04:06:52,239
and then you show all of those on the
6862
04:06:52,239 --> 04:06:54,960
screen so you know it shows the results
6863
04:06:54,960 --> 04:06:57,040
on the screen like it says you win but
6864
04:06:57,040 --> 04:06:59,760
on the dom right on the dom so another
6865
04:06:59,760 --> 04:07:01,439
order of saying screen is really just
6866
04:07:01,439 --> 04:07:03,840
dom so then it shows it on the dom
6867
04:07:03,840 --> 04:07:05,680
document object model where the users
6868
04:07:05,680 --> 04:07:08,640
can see it so that's that's really like
6869
04:07:08,640 --> 04:07:11,760
the bare bones of how you're gonna start
6870
04:07:11,760 --> 04:07:13,359
being able to do rock paper scissors
6871
04:07:13,359 --> 04:07:14,800
okay so you really need to understand
6872
04:07:14,800 --> 04:07:16,640
document object model
6873
04:07:16,640 --> 04:07:18,720
and how dom manipulation works is the
6874
04:07:18,720 --> 04:07:20,640
most basic version of it but honestly
6875
04:07:20,640 --> 04:07:22,720
this is really all you need and you'd be
6876
04:07:22,720 --> 04:07:25,040
able to do that rock paper scissors
6877
04:07:25,040 --> 04:07:26,720
uh no problem
6878
04:07:26,720 --> 04:07:28,560
so hopefully you enjoyed learning about
6879
04:07:28,560 --> 04:07:29,359
dom
6880
04:07:29,359 --> 04:07:31,760
and with that said we're now going to go
6881
04:07:31,760 --> 04:07:34,239
ahead and start covering more stuff
6882
04:07:34,239 --> 04:07:35,840
let's actually do one additional
6883
04:07:35,840 --> 04:07:37,600
exercise on this because as you've
6884
04:07:37,600 --> 04:07:38,640
noticed
6885
04:07:38,640 --> 04:07:41,439
you had you had multiple things
6886
04:07:41,439 --> 04:07:42,800
and you wrote
6887
04:07:42,800 --> 04:07:45,199
the same code but multiple times here
6888
04:07:45,199 --> 04:07:46,960
you know pretty much the same code
6889
04:07:46,960 --> 04:07:49,600
multiple times here so one way i want to
6890
04:07:49,600 --> 04:07:52,239
show you is you actually could even like
6891
04:07:52,239 --> 04:07:55,199
loop through all of these okay and then
6892
04:07:55,199 --> 04:07:56,479
actually like
6893
04:07:56,479 --> 04:07:57,840
kind of
6894
04:07:57,840 --> 04:07:59,279
you know do the thing that you want to
6895
04:07:59,279 --> 04:08:00,960
do with them all at once so i'll show
6896
04:08:00,960 --> 04:08:02,000
you now
6897
04:08:02,000 --> 04:08:05,279
so what i could do is um
6898
04:08:05,279 --> 04:08:06,800
at this level
6899
04:08:06,800 --> 04:08:09,520
let's go ahead and give a
6900
04:08:09,520 --> 04:08:11,120
class to all of these so i'm going to
6901
04:08:11,120 --> 04:08:13,199
basically say
6902
04:08:13,199 --> 04:08:15,040
class is
6903
04:08:15,040 --> 04:08:16,720
color button
6904
04:08:16,720 --> 04:08:19,600
all right and let's end the quote and
6905
04:08:19,600 --> 04:08:21,840
let's do that for all of the buttons i'm
6906
04:08:21,840 --> 04:08:23,680
going to go here
6907
04:08:23,680 --> 04:08:26,239
and we're going to give it a class
6908
04:08:26,239 --> 04:08:29,760
oh sorry let's copy this let's paste it
6909
04:08:29,760 --> 04:08:32,239
and let's go ahead and do it here as
6910
04:08:32,239 --> 04:08:35,600
well okay so now what you should have is
6911
04:08:35,600 --> 04:08:38,560
three like squares or whatever right
6912
04:08:38,560 --> 04:08:41,040
and uh the class is just color
6913
04:08:41,040 --> 04:08:43,199
you could call it color square
6914
04:08:43,199 --> 04:08:44,800
that probably is a better class name so
6915
04:08:44,800 --> 04:08:47,359
color square color square color square
6916
04:08:47,359 --> 04:08:49,920
okay now if we want to get
6917
04:08:49,920 --> 04:08:53,040
all of these elements all at once what i
6918
04:08:53,040 --> 04:08:56,479
can actually do is i can say console.
6919
04:08:56,479 --> 04:08:58,840
actually let's do it like this i say con
6920
04:08:58,840 --> 04:09:02,080
squares and i can say get
6921
04:09:02,080 --> 04:09:03,840
sorry document
6922
04:09:03,840 --> 04:09:05,359
dot
6923
04:09:05,359 --> 04:09:06,800
get
6924
04:09:06,800 --> 04:09:09,040
query selector
6925
04:09:09,040 --> 04:09:10,080
all
6926
04:09:10,080 --> 04:09:11,760
and then i can pass in the class that
6927
04:09:11,760 --> 04:09:14,080
i'm actually looking for so in this case
6928
04:09:14,080 --> 04:09:16,399
i'm looking for the class
6929
04:09:16,399 --> 04:09:17,520
color
6930
04:09:17,520 --> 04:09:18,880
square
6931
04:09:18,880 --> 04:09:20,479
let's also
6932
04:09:20,479 --> 04:09:22,880
console log this and let's run it all
6933
04:09:22,880 --> 04:09:24,880
right and i get back a node list so
6934
04:09:24,880 --> 04:09:26,560
that's exactly what i was looking for
6935
04:09:26,560 --> 04:09:29,840
okay now let's see if i can loop through
6936
04:09:29,840 --> 04:09:31,439
these
6937
04:09:31,439 --> 04:09:33,359
before we loop actually what we need to
6938
04:09:33,359 --> 04:09:35,359
do is let's run this
6939
04:09:35,359 --> 04:09:37,120
and node lists right now is empty so
6940
04:09:37,120 --> 04:09:38,560
that's not what we want we want to put a
6941
04:09:38,560 --> 04:09:40,880
period here period color square
6942
04:09:40,880 --> 04:09:42,960
basically says hey i want to look for
6943
04:09:42,960 --> 04:09:44,880
the class them color square okay so this
6944
04:09:44,880 --> 04:09:46,239
is classes
6945
04:09:46,239 --> 04:09:47,920
and i'm going to select all the classes
6946
04:09:47,920 --> 04:09:49,439
i'm going to return them to you as like
6947
04:09:49,439 --> 04:09:51,680
an array or a node list
6948
04:09:51,680 --> 04:09:53,760
perfect so now i have all these elements
6949
04:09:53,760 --> 04:09:55,840
right over here all right so let's just
6950
04:09:55,840 --> 04:09:57,439
make sure we're getting all of our color
6951
04:09:57,439 --> 04:09:59,680
squares and console log this one is
6952
04:09:59,680 --> 04:10:01,040
console log
6953
04:10:01,040 --> 04:10:03,520
squares let's run this
6954
04:10:03,520 --> 04:10:06,239
and boom you can see we're getting
6955
04:10:06,239 --> 04:10:08,399
all of our squares right here because
6956
04:10:08,399 --> 04:10:10,800
we're getting three html developments
6957
04:10:10,800 --> 04:10:12,000
perfect
6958
04:10:12,000 --> 04:10:14,080
now here's what one thing we want to do
6959
04:10:14,080 --> 04:10:15,040
okay
6960
04:10:15,040 --> 04:10:16,960
in order to do this go ahead and change
6961
04:10:16,960 --> 04:10:18,319
all these two buttons because in rock
6962
04:10:18,319 --> 04:10:20,080
paper scissors you're not gonna these
6963
04:10:20,080 --> 04:10:21,439
aren't gonna be divs these are gonna be
6964
04:10:21,439 --> 04:10:22,720
butt tens
6965
04:10:22,720 --> 04:10:25,760
okay because if i go to here these are
6966
04:10:25,760 --> 04:10:27,439
buttons okay these are buttons buttons
6967
04:10:27,439 --> 04:10:28,319
buttons
6968
04:10:28,319 --> 04:10:30,239
cool
6969
04:10:30,239 --> 04:10:32,479
so let's go back here and we're going to
6970
04:10:32,479 --> 04:10:36,080
change these divs to buttons so button
6971
04:10:36,080 --> 04:10:38,159
end it with the button this is going to
6972
04:10:38,159 --> 04:10:39,279
be
6973
04:10:39,279 --> 04:10:41,120
a button as well
6974
04:10:41,120 --> 04:10:43,439
button here and button there
6975
04:10:43,439 --> 04:10:47,359
cool so now that this one div here
6976
04:10:47,359 --> 04:10:50,239
has three buttons
6977
04:10:50,239 --> 04:10:51,279
perfect
6978
04:10:51,279 --> 04:10:52,800
so let's hit run and let's make sure
6979
04:10:52,800 --> 04:10:54,880
everything is working as expected red
6980
04:10:54,880 --> 04:10:56,960
red red yellow yellow green green green
6981
04:10:56,960 --> 04:10:58,880
perfect
6982
04:10:58,880 --> 04:11:00,399
now what we're going to do is we're
6983
04:11:00,399 --> 04:11:01,680
going to give each of these buttons a
6984
04:11:01,680 --> 04:11:03,760
value okay so we're going to give this
6985
04:11:03,760 --> 04:11:05,359
button a
6986
04:11:05,359 --> 04:11:06,960
value of red
6987
04:11:06,960 --> 04:11:08,960
we're going to give this button a value
6988
04:11:08,960 --> 04:11:11,120
of yellow we're going to give this
6989
04:11:11,120 --> 04:11:13,359
button a value
6990
04:11:13,359 --> 04:11:14,720
of green
6991
04:11:14,720 --> 04:11:16,960
and let's go here and we're going to now
6992
04:11:16,960 --> 04:11:18,399
say now that we're getting back our
6993
04:11:18,399 --> 04:11:23,359
squares let's go console.log square
6994
04:11:23,680 --> 04:11:26,080
let's grab the zero square and let's do
6995
04:11:26,080 --> 04:11:27,600
value
6996
04:11:27,600 --> 04:11:29,760
okay now when i run this code you're
6997
04:11:29,760 --> 04:11:31,439
going to notice
6998
04:11:31,439 --> 04:11:35,279
oops square is not defined ah squares of
6999
04:11:35,279 --> 04:11:38,159
zero dot value let's do that
7000
04:11:38,159 --> 04:11:40,399
okay and let's uh let's comment this out
7001
04:11:40,399 --> 04:11:41,760
so we don't have to worry about this
7002
04:11:41,760 --> 04:11:43,600
anymore comment this out
7003
04:11:43,600 --> 04:11:45,359
and you can see we're extracting the
7004
04:11:45,359 --> 04:11:48,239
value here and we're getting back red
7005
04:11:48,239 --> 04:11:50,880
okay so that's a really good sign
7006
04:11:50,880 --> 04:11:54,239
now if i do this again
7007
04:11:54,239 --> 04:11:56,159
i'm going to get back
7008
04:11:56,159 --> 04:11:58,479
yellow but if i do this again i'm going
7009
04:11:58,479 --> 04:12:00,000
to get back a 2.
7010
04:12:00,000 --> 04:12:01,840
so instead of repeating this code again
7011
04:12:01,840 --> 04:12:04,319
and again here's a great point to write
7012
04:12:04,319 --> 04:12:05,199
uh
7013
04:12:05,199 --> 04:12:06,640
what do we write when we have to repeat
7014
04:12:06,640 --> 04:12:08,800
a ton of code over and over and over and
7015
04:12:08,800 --> 04:12:10,720
over and over and over again
7016
04:12:10,720 --> 04:12:13,439
we use a loop and specifically let's use
7017
04:12:13,439 --> 04:12:16,159
a for loop and let's use it for each
7018
04:12:16,159 --> 04:12:19,120
loop okay so we're going to say
7019
04:12:19,120 --> 04:12:20,800
and for each is one of the best loops so
7020
04:12:20,800 --> 04:12:22,399
if i haven't shown you that
7021
04:12:22,399 --> 04:12:24,000
up until now i'm going to show it to you
7022
04:12:24,000 --> 04:12:26,159
now okay so four each is my favorite
7023
04:12:26,159 --> 04:12:30,159
that one is even better than a um
7024
04:12:30,159 --> 04:12:32,479
four of loop okay so let's go ahead and
7025
04:12:32,479 --> 04:12:34,800
use a for each loop so i'm going to say
7026
04:12:34,800 --> 04:12:38,319
grab squares and then for each square
7027
04:12:38,319 --> 04:12:41,439
okay let's just console log so grab each
7028
04:12:41,439 --> 04:12:43,600
for each square
7029
04:12:43,600 --> 04:12:47,600
i'm going to console.log square.value
7030
04:12:47,600 --> 04:12:49,359
let's run this
7031
04:12:49,359 --> 04:12:51,520
and now you can see and we're going to
7032
04:12:51,520 --> 04:12:54,000
comment this out and let's run
7033
04:12:54,000 --> 04:12:57,600
and you can see i get red yellow green
7034
04:12:57,600 --> 04:12:58,560
okay
7035
04:12:58,560 --> 04:13:00,560
now what i can do is i can loop through
7036
04:13:00,560 --> 04:13:01,840
this
7037
04:13:01,840 --> 04:13:04,560
and basically make this function right
7038
04:13:04,560 --> 04:13:07,199
over here multiple times okay so i can
7039
04:13:07,199 --> 04:13:08,720
actually say
7040
04:13:08,720 --> 04:13:10,800
hey
7041
04:13:10,800 --> 04:13:12,560
for each square what i want you to do is
7042
04:13:12,560 --> 04:13:15,680
actually update its on click property
7043
04:13:15,680 --> 04:13:18,080
and set it to this function so now we
7044
04:13:18,080 --> 04:13:19,439
can actually just go ahead and do that
7045
04:13:19,439 --> 04:13:21,920
so i can say square dot unclick
7046
04:13:21,920 --> 04:13:23,359
and we're going to set that to
7047
04:13:23,359 --> 04:13:25,600
console.log
7048
04:13:25,600 --> 04:13:27,199
that square's
7049
04:13:27,199 --> 04:13:30,319
name okay and we're gonna take all three
7050
04:13:30,319 --> 04:13:32,000
of these lines and actually comment them
7051
04:13:32,000 --> 04:13:33,680
out because we don't need them anymore
7052
04:13:33,680 --> 04:13:36,080
so let's go ahead and run
7053
04:13:36,080 --> 04:13:37,680
and check it out
7054
04:13:37,680 --> 04:13:39,359
red red red yellow yellow yellow green
7055
04:13:39,359 --> 04:13:40,960
green green whoops we're getting some
7056
04:13:40,960 --> 04:13:43,279
weird error let's run it again
7057
04:13:43,279 --> 04:13:45,199
oh because that name is not a thing i'm
7058
04:13:45,199 --> 04:13:47,439
sorry let's do dot value let's run it
7059
04:13:47,439 --> 04:13:48,399
again
7060
04:13:48,399 --> 04:13:51,439
and value is being pulled from this html
7061
04:13:51,439 --> 04:13:52,960
attribute that we created right here
7062
04:13:52,960 --> 04:13:54,800
value that's where it's coming from so
7063
04:13:54,800 --> 04:13:56,720
let's go back let's hit run
7064
04:13:56,720 --> 04:14:00,399
click red yellow green yellow green red
7065
04:14:00,399 --> 04:14:03,040
right so look at how nice this became
7066
04:14:03,040 --> 04:14:05,120
now we don't even need
7067
04:14:05,120 --> 04:14:08,319
this big chunk of code anymore
7068
04:14:08,319 --> 04:14:10,560
right we just
7069
04:14:10,560 --> 04:14:12,319
have this here
7070
04:14:12,319 --> 04:14:14,319
okay so let's i'll leave the comments
7071
04:14:14,319 --> 04:14:16,479
there for you let's run
7072
04:14:16,479 --> 04:14:18,560
click click click click click click
7073
04:14:18,560 --> 04:14:20,960
click click click right so this is super
7074
04:14:20,960 --> 04:14:23,840
nice and uh this pattern you're
7075
04:14:23,840 --> 04:14:25,920
definitely gonna need it for when we're
7076
04:14:25,920 --> 04:14:28,399
making our rock paper scissors game so
7077
04:14:28,399 --> 04:14:30,399
keep this pattern in mind this is an
7078
04:14:30,399 --> 04:14:33,120
example you're gonna be definitely using
7079
04:14:33,120 --> 04:14:35,600
all right let's um
7080
04:14:35,600 --> 04:14:38,239
dude times clicked okay so let's take
7081
04:14:38,239 --> 04:14:40,000
our app um
7082
04:14:40,000 --> 04:14:42,720
up by one level so this dom
7083
04:14:42,720 --> 04:14:44,399
red yellow green squares app that we
7084
04:14:44,399 --> 04:14:46,479
made okay so i'm going to click on this
7085
04:14:46,479 --> 04:14:47,920
here
7086
04:14:47,920 --> 04:14:48,960
all right
7087
04:14:48,960 --> 04:14:51,600
and um and we're going to take it up up
7088
04:14:51,600 --> 04:14:53,760
a notch so we want it to be where when i
7089
04:14:53,760 --> 04:14:56,000
click it it actually says the number of
7090
04:14:56,000 --> 04:14:58,880
the times clicked here zero if i click
7091
04:14:58,880 --> 04:15:01,199
red one it should give me one if i click
7092
04:15:01,199 --> 04:15:03,040
yellow four times it should give me four
7093
04:15:03,040 --> 04:15:04,880
here and if i click green three it gives
7094
04:15:04,880 --> 04:15:06,560
me three so how are we gonna do that
7095
04:15:06,560 --> 04:15:08,399
we're gonna start combining a lot of the
7096
04:15:08,399 --> 04:15:10,479
things that we were learning okay so see
7097
04:15:10,479 --> 04:15:12,720
if you could attempt it
7098
04:15:12,720 --> 04:15:14,319
but let's do this together so you can
7099
04:15:14,319 --> 04:15:17,040
just get some more practice here so i'm
7100
04:15:17,040 --> 04:15:19,760
going to create a object called times
7101
04:15:19,760 --> 04:15:21,520
clicked it's going to keep in store how
7102
04:15:21,520 --> 04:15:22,960
many times any of these colors is
7103
04:15:22,960 --> 04:15:25,279
clicked so we're going to have red
7104
04:15:25,279 --> 04:15:26,640
is clicked
7105
04:15:26,640 --> 04:15:29,600
uh red just click zero times
7106
04:15:29,600 --> 04:15:30,840
we'll have
7107
04:15:30,840 --> 04:15:32,720
yellow
7108
04:15:32,720 --> 04:15:34,960
zero times because start off everything
7109
04:15:34,960 --> 04:15:36,960
is zero right green
7110
04:15:36,960 --> 04:15:38,319
is going to be
7111
04:15:38,319 --> 04:15:40,239
zero
7112
04:15:40,239 --> 04:15:42,640
we just have it like this for now
7113
04:15:42,640 --> 04:15:44,159
and then what i'm going to do is just on
7114
04:15:44,159 --> 04:15:45,600
click here
7115
04:15:45,600 --> 04:15:47,680
i'm going to turn this into a full on
7116
04:15:47,680 --> 04:15:49,600
full function yeah like a function but
7117
04:15:49,600 --> 04:15:52,399
not just that returns right away
7118
04:15:52,399 --> 04:15:54,640
so we can write stuff here
7119
04:15:54,640 --> 04:15:56,880
i'll comment out the square value part
7120
04:15:56,880 --> 04:15:58,000
for now
7121
04:15:58,000 --> 04:15:59,920
and what i'm going to basically say here
7122
04:15:59,920 --> 04:16:01,600
is um
7123
04:16:01,600 --> 04:16:05,040
hey basically what i want you to do is
7124
04:16:05,040 --> 04:16:07,600
grab the times clicked object
7125
04:16:07,600 --> 04:16:11,120
and put this in as the key so
7126
04:16:11,120 --> 04:16:13,600
uh and and basically grab its key okay
7127
04:16:13,600 --> 04:16:16,080
and we're going to say dot value
7128
04:16:16,080 --> 04:16:18,319
so dot value will be whichever one i
7129
04:16:18,319 --> 04:16:20,080
click remember so the dot if i click
7130
04:16:20,080 --> 04:16:22,720
this the value is going to be red
7131
04:16:22,720 --> 04:16:24,800
and what i'm going to say is increment
7132
04:16:24,800 --> 04:16:27,439
that by 1. so if i click here
7133
04:16:27,439 --> 04:16:30,319
the value is going to be red
7134
04:16:30,319 --> 04:16:33,040
and then my object is going to go find
7135
04:16:33,040 --> 04:16:35,359
the key where it says red it's going to
7136
04:16:35,359 --> 04:16:37,199
find this
7137
04:16:37,199 --> 04:16:40,159
and then i'm going to say plus equals 1
7138
04:16:40,159 --> 04:16:42,000
its value so it's gonna increment this
7139
04:16:42,000 --> 04:16:43,040
by one
7140
04:16:43,040 --> 04:16:46,880
and uh we also want this to show up
7141
04:16:46,880 --> 04:16:47,840
like
7142
04:16:47,840 --> 04:16:50,159
here we want it to show up right
7143
04:16:50,159 --> 04:16:51,439
so now what i'm going to say is i'm
7144
04:16:51,439 --> 04:16:53,920
going to say
7145
04:16:54,239 --> 04:16:56,800
that square inner text
7146
04:16:56,800 --> 04:16:58,580
we're going to change it to
7147
04:16:58,580 --> 04:17:01,040
[Music]
7148
04:17:01,040 --> 04:17:03,359
times clicked
7149
04:17:03,359 --> 04:17:06,399
squared up value
7150
04:17:06,560 --> 04:17:08,399
all right so let's go ahead and run this
7151
04:17:08,399 --> 04:17:11,439
and i'll click this click red
7152
04:17:11,439 --> 04:17:12,479
boom
7153
04:17:12,479 --> 04:17:14,000
boom
7154
04:17:14,000 --> 04:17:15,120
boom
7155
04:17:15,120 --> 04:17:16,640
perfect
7156
04:17:16,640 --> 04:17:18,720
okay and if you want the most updated
7157
04:17:18,720 --> 04:17:21,040
version of this app i'm i'm imagining
7158
04:17:21,040 --> 04:17:23,040
you're coding it up in your own rupple
7159
04:17:23,040 --> 04:17:24,800
and that's great if you're coding it up
7160
04:17:24,800 --> 04:17:26,560
here in your own replit but if you want
7161
04:17:26,560 --> 04:17:28,399
to merge if you want to see the finished
7162
04:17:28,399 --> 04:17:30,560
version you know it's going to be in
7163
04:17:30,560 --> 04:17:32,960
here in this dom folder red yellow green
7164
04:17:32,960 --> 04:17:34,880
exercise
7165
04:17:34,880 --> 04:17:36,640
and then you just can come here this has
7166
04:17:36,640 --> 04:17:38,720
a solution there so you can just
7167
04:17:38,720 --> 04:17:40,800
navigate here pick the solution and then
7168
04:17:40,800 --> 04:17:42,319
see it okay this is the most updated one
7169
04:17:42,319 --> 04:17:44,479
this is the one i'm doing right now
7170
04:17:44,479 --> 04:17:46,239
and that's how it shows up and i click
7171
04:17:46,239 --> 04:17:48,399
here boom it shows up right so look at
7172
04:17:48,399 --> 04:17:50,479
that right how nice is that if i click
7173
04:17:50,479 --> 04:17:52,640
yellow 10 times it shows it to me click
7174
04:17:52,640 --> 04:17:54,080
read 10 times
7175
04:17:54,080 --> 04:17:56,399
whatever i'm doing it's it's keeping
7176
04:17:56,399 --> 04:17:58,720
track of everything so this is where it
7177
04:17:58,720 --> 04:17:59,680
it's
7178
04:17:59,680 --> 04:18:02,399
nice to work with objects and things of
7179
04:18:02,399 --> 04:18:04,159
that nature this is why we practice all
7180
04:18:04,159 --> 04:18:06,319
these things so now there's a lot that's
7181
04:18:06,319 --> 04:18:08,319
going on here
7182
04:18:08,319 --> 04:18:10,960
there's a loop going on there's an event
7183
04:18:10,960 --> 04:18:13,840
listener called on click happening
7184
04:18:13,840 --> 04:18:16,319
we're going into this times clicked
7185
04:18:16,319 --> 04:18:19,439
object and updating its key values and
7186
04:18:19,439 --> 04:18:21,359
then we are manipulating the dom and
7187
04:18:21,359 --> 04:18:22,880
showing that result
7188
04:18:22,880 --> 04:18:24,560
on here right so there's a lot going on
7189
04:18:24,560 --> 04:18:26,880
here and with just these few lines of
7190
04:18:26,880 --> 04:18:27,920
code
7191
04:18:27,920 --> 04:18:29,520
and so
7192
04:18:29,520 --> 04:18:31,920
you know it's a lot but that's why we
7193
04:18:31,920 --> 04:18:34,000
built it all up step by step so
7194
04:18:34,000 --> 04:18:36,560
hopefully you enjoyed this exercise
7195
04:18:36,560 --> 04:18:38,399
for you know furthering your dom
7196
04:18:38,399 --> 04:18:40,000
manipulation skills
7197
04:18:40,000 --> 04:18:42,080
now one thing one more thing i want to
7198
04:18:42,080 --> 04:18:44,239
show you here is how to clear this so
7199
04:18:44,239 --> 04:18:45,840
basically to start a new game because
7200
04:18:45,840 --> 04:18:47,680
for rock paper scissors you're gonna
7201
04:18:47,680 --> 04:18:49,279
need to know how to do that right so for
7202
04:18:49,279 --> 04:18:51,520
example we play a game we keep track of
7203
04:18:51,520 --> 04:18:53,520
the score right so that's why you need
7204
04:18:53,520 --> 04:18:55,199
to learn how to keep track of a score
7205
04:18:55,199 --> 04:18:57,520
and then when we hit this red
7206
04:18:57,520 --> 04:18:59,439
it ends the game
7207
04:18:59,439 --> 04:19:01,120
right so it clears the game and then you
7208
04:19:01,120 --> 04:19:03,760
start from scratch all over again so if
7209
04:19:03,760 --> 04:19:05,840
we go here we want to be able to add
7210
04:19:05,840 --> 04:19:08,000
that functionality so let's go ahead and
7211
04:19:08,000 --> 04:19:09,920
try to create this together okay so i'm
7212
04:19:09,920 --> 04:19:11,600
going to say const
7213
04:19:11,600 --> 04:19:12,640
clear
7214
04:19:12,640 --> 04:19:13,920
scores
7215
04:19:13,920 --> 04:19:15,600
okay or you could call it function as
7216
04:19:15,600 --> 04:19:18,080
well whatever is easier for you honestly
7217
04:19:18,080 --> 04:19:19,760
it doesn't really matter
7218
04:19:19,760 --> 04:19:22,720
and what clear scores is going to do
7219
04:19:22,720 --> 04:19:24,560
is it's going to take
7220
04:19:24,560 --> 04:19:28,239
you know all of these squares here
7221
04:19:28,239 --> 04:19:31,760
and it's basically just gonna clear up
7222
04:19:31,760 --> 04:19:33,359
their inner text
7223
04:19:33,359 --> 04:19:36,080
okay so i'm gonna say squares
7224
04:19:36,080 --> 04:19:39,279
dot for each square
7225
04:19:39,279 --> 04:19:43,199
i'm going to grab square.value
7226
04:19:43,199 --> 04:19:45,120
and we're just going to set that to
7227
04:19:45,120 --> 04:19:46,960
empty so we're just going to set it to
7228
04:19:46,960 --> 04:19:48,880
nothing so we're going to loop through
7229
04:19:48,880 --> 04:19:51,359
take each square set its value to empty
7230
04:19:51,359 --> 04:19:53,040
and then that's kind of it okay so this
7231
04:19:53,040 --> 04:19:54,960
is going to be my function that clears
7232
04:19:54,960 --> 04:19:57,279
everything so this looks good to me now
7233
04:19:57,279 --> 04:19:58,800
we just need to make a button that can
7234
04:19:58,800 --> 04:20:01,359
detect that that button is being clicked
7235
04:20:01,359 --> 04:20:02,960
okay so
7236
04:20:02,960 --> 04:20:04,640
we will go
7237
04:20:04,640 --> 04:20:06,800
here
7238
04:20:06,800 --> 04:20:09,920
in our index.html file alright and we
7239
04:20:09,920 --> 04:20:11,680
want to add in a button
7240
04:20:11,680 --> 04:20:14,000
that says clear game
7241
04:20:14,000 --> 04:20:15,760
or something like that so it's obvious
7242
04:20:15,760 --> 04:20:17,279
so if i go back
7243
04:20:17,279 --> 04:20:19,199
i see clear game button is right there
7244
04:20:19,199 --> 04:20:20,640
so perfect
7245
04:20:20,640 --> 04:20:22,319
oh sorry actually we're going to give
7246
04:20:22,319 --> 04:20:24,399
this button we need to give it a id or
7247
04:20:24,399 --> 04:20:26,640
something right so we can grab it so
7248
04:20:26,640 --> 04:20:29,840
let's call this clear clear dash game
7249
04:20:29,840 --> 04:20:32,239
that's fine let's go back into our that
7250
04:20:32,239 --> 04:20:33,279
script
7251
04:20:33,279 --> 04:20:35,439
and then i'm going to basically say
7252
04:20:35,439 --> 04:20:37,199
once clear game
7253
04:20:37,199 --> 04:20:38,399
div
7254
04:20:38,399 --> 04:20:39,760
is
7255
04:20:39,760 --> 04:20:43,359
do document dot get elements by id and
7256
04:20:43,359 --> 04:20:46,000
the id in this case is clear game so i
7257
04:20:46,000 --> 04:20:48,159
have my clear game div
7258
04:20:48,159 --> 04:20:50,640
and um what i'm going to say is clear
7259
04:20:50,640 --> 04:20:53,680
game div dot on click and i'm going to
7260
04:20:53,680 --> 04:20:55,920
set its on click to this function right
7261
04:20:55,920 --> 04:20:57,760
over here so i'm just going to clear
7262
04:20:57,760 --> 04:21:00,720
scores just basically when i click on
7263
04:21:00,720 --> 04:21:01,680
click
7264
04:21:01,680 --> 04:21:04,080
run this function right over here
7265
04:21:04,080 --> 04:21:05,680
okay so i'll put this function right
7266
04:21:05,680 --> 04:21:06,960
over here
7267
04:21:06,960 --> 04:21:09,199
and let's try it so basically right when
7268
04:21:09,199 --> 04:21:10,399
i click
7269
04:21:10,399 --> 04:21:12,080
when i click this
7270
04:21:12,080 --> 04:21:13,120
button
7271
04:21:13,120 --> 04:21:15,040
okay or maybe let's call the clear game
7272
04:21:15,040 --> 04:21:16,000
button
7273
04:21:16,000 --> 04:21:18,000
clear game button
7274
04:21:18,000 --> 04:21:21,120
let's do btn so our code looks
7275
04:21:21,120 --> 04:21:23,359
nice and clean on the same line
7276
04:21:23,359 --> 04:21:25,040
and now when i click clear game button
7277
04:21:25,040 --> 04:21:26,560
it should clear all the scores so let's
7278
04:21:26,560 --> 04:21:28,960
give that a try oh it's not doing that
7279
04:21:28,960 --> 04:21:31,600
so let's see if we can debug it and see
7280
04:21:31,600 --> 04:21:34,159
what's happening okay i'm trying to
7281
04:21:34,159 --> 04:21:37,040
change its value but instead of value
7282
04:21:37,040 --> 04:21:38,960
what i'm meaning to change is the inner
7283
04:21:38,960 --> 04:21:42,080
text so let's go enter text
7284
04:21:42,080 --> 04:21:45,920
and we're going to set this text to
7285
04:21:45,920 --> 04:21:49,520
zero and let's try now
7286
04:21:49,680 --> 04:21:51,199
let's run this
7287
04:21:51,199 --> 04:21:54,319
let's go back in the game
7288
04:21:54,319 --> 04:21:55,760
there we go
7289
04:21:55,760 --> 04:21:59,279
right there we go now
7290
04:21:59,279 --> 04:22:01,279
it's doing it correctly
7291
04:22:01,279 --> 04:22:04,720
clear game it starts from scratch
7292
04:22:04,720 --> 04:22:07,600
what happens if we set the text to empty
7293
04:22:07,600 --> 04:22:09,359
text so let's see what happens does this
7294
04:22:09,359 --> 04:22:11,279
give us an error no
7295
04:22:11,279 --> 04:22:12,960
so this this might actually be even
7296
04:22:12,960 --> 04:22:15,600
better just set it to an empty string
7297
04:22:15,600 --> 04:22:17,439
or a zero
7298
04:22:17,439 --> 04:22:19,600
and you're good to go here
7299
04:22:19,600 --> 04:22:21,680
so now this app is
7300
04:22:21,680 --> 04:22:23,760
really coming out nice right this app is
7301
04:22:23,760 --> 04:22:25,199
doing a really
7302
04:22:25,199 --> 04:22:27,520
really good job and
7303
04:22:27,520 --> 04:22:29,760
i mean yeah this is a great app looks
7304
04:22:29,760 --> 04:22:31,520
really really nice so there you go like
7305
04:22:31,520 --> 04:22:33,120
this here
7306
04:22:33,120 --> 04:22:36,159
should give you a very clear idea of dom
7307
04:22:36,159 --> 04:22:38,000
manipulation how to make things work
7308
04:22:38,000 --> 04:22:40,560
like if you have this example to work
7309
04:22:40,560 --> 04:22:43,120
off of i mean you're pretty set to go
7310
04:22:43,120 --> 04:22:45,199
with dom manipulation so great job if
7311
04:22:45,199 --> 04:22:47,520
you got this far nice work all right
7312
04:22:47,520 --> 04:22:49,279
you're making amazing progress so far
7313
04:22:49,279 --> 04:22:50,560
but here's what we're going to do now
7314
04:22:50,560 --> 04:22:52,319
we're going to step it up we're going to
7315
04:22:52,319 --> 04:22:56,080
go and start building a tip calculator
7316
04:22:56,080 --> 04:22:57,680
app so check it out i'm going to go over
7317
04:22:57,680 --> 04:23:00,479
here and we're going to do the following
7318
04:23:00,479 --> 04:23:02,800
i'm going to open this up so hit that
7319
04:23:02,800 --> 04:23:04,880
button right behind my face the top
7320
04:23:04,880 --> 04:23:07,760
right and it's going to open up this
7321
04:23:07,760 --> 04:23:09,439
this right here and we're going to go
7322
04:23:09,439 --> 04:23:12,319
over to tip calculator and hit click on
7323
04:23:12,319 --> 04:23:13,520
it okay
7324
04:23:13,520 --> 04:23:16,560
and here if you type in let's say
7325
04:23:16,560 --> 04:23:18,800
the bill total is a hundred dollars the
7326
04:23:18,800 --> 04:23:21,439
tip amount is ten percent and you can
7327
04:23:21,439 --> 04:23:23,920
see it already calculated it correctly
7328
04:23:23,920 --> 04:23:25,359
right because if the bill is a hundred
7329
04:23:25,359 --> 04:23:27,120
dollars and the tip is ten percent well
7330
04:23:27,120 --> 04:23:29,680
that's the tip would be ten dollars so
7331
04:23:29,680 --> 04:23:31,279
the total would be a hundred and ten
7332
04:23:31,279 --> 04:23:34,080
dollars per person but what if we had
7333
04:23:34,080 --> 04:23:36,479
more people what if we had two people so
7334
04:23:36,479 --> 04:23:38,399
watch what happens when i hit plus
7335
04:23:38,399 --> 04:23:40,399
boom the bill just got divided between
7336
04:23:40,399 --> 04:23:42,399
two people and so it automatically tells
7337
04:23:42,399 --> 04:23:45,439
hey john you need to pay 55 kazi you
7338
04:23:45,439 --> 04:23:47,840
need to pay 55 so both people know
7339
04:23:47,840 --> 04:23:49,600
exactly how much they need to pay what
7340
04:23:49,600 --> 04:23:52,080
if i keep increasing this number well
7341
04:23:52,080 --> 04:23:54,080
you see it keeps dividing
7342
04:23:54,080 --> 04:23:56,080
between all of those people so if the
7343
04:23:56,080 --> 04:23:58,319
bill was 110 dollars and you divide that
7344
04:23:58,319 --> 04:24:00,960
by 10 people you get 11 dollars per
7345
04:24:00,960 --> 04:24:01,920
person
7346
04:24:01,920 --> 04:24:03,600
now watch what happens if i keep
7347
04:24:03,600 --> 04:24:04,960
subtracting it's subtracting and
7348
04:24:04,960 --> 04:24:06,720
subtracting and subtracting it if i go
7349
04:24:06,720 --> 04:24:09,120
here and now subtract it watch it's not
7350
04:24:09,120 --> 04:24:10,720
gonna let me go down to zero right i'll
7351
04:24:10,720 --> 04:24:12,159
try a couple i'll try a couple more
7352
04:24:12,159 --> 04:24:14,080
times boom boom boom boom boom boom boom
7353
04:24:14,080 --> 04:24:17,680
boom nope does not let me go below one
7354
04:24:17,680 --> 04:24:20,159
so these are some of the functionalities
7355
04:24:20,159 --> 04:24:22,159
of this app that you'll have to keep in
7356
04:24:22,159 --> 04:24:24,159
mind so for example what will you need
7357
04:24:24,159 --> 04:24:25,680
to know how to do
7358
04:24:25,680 --> 04:24:27,680
well you'll actually need to be able to
7359
04:24:27,680 --> 04:24:30,000
get the user input
7360
04:24:30,000 --> 04:24:32,399
from this field right here
7361
04:24:32,399 --> 04:24:34,560
okay and this field let's say it's
7362
04:24:34,560 --> 04:24:36,960
called our this one over here let's just
7363
04:24:36,960 --> 04:24:41,840
say it's called bill total input okay
7364
04:24:41,840 --> 04:24:44,239
then you're going to have to get
7365
04:24:44,239 --> 04:24:48,640
data from this tip as well
7366
04:24:48,720 --> 04:24:51,120
and let's say this one is called tip
7367
04:24:51,120 --> 04:24:54,319
input okay
7368
04:24:54,319 --> 04:24:56,319
also you're going to be able to you're
7369
04:24:56,319 --> 04:24:57,840
going to need to be able to pull the
7370
04:24:57,840 --> 04:25:01,199
number of people at any given time
7371
04:25:01,199 --> 04:25:03,760
this one will probably be called let's
7372
04:25:03,760 --> 04:25:06,239
say number of people okay so maybe these
7373
04:25:06,239 --> 04:25:07,600
are all divs
7374
04:25:07,600 --> 04:25:09,520
or inputs that you can have you know
7375
04:25:09,520 --> 04:25:11,199
these are ids that you might be able to
7376
04:25:11,199 --> 04:25:14,080
have access to okay and then this over
7377
04:25:14,080 --> 04:25:14,960
here
7378
04:25:14,960 --> 04:25:17,120
this right over here
7379
04:25:17,120 --> 04:25:20,800
right this will be called per
7380
04:25:20,800 --> 04:25:21,920
person
7381
04:25:21,920 --> 04:25:24,960
total okay and all of these will be div
7382
04:25:24,960 --> 04:25:27,600
ids okay which means these are special
7383
04:25:27,600 --> 04:25:29,680
things inside of the html and you can
7384
04:25:29,680 --> 04:25:31,680
get access to all of this so then if you
7385
04:25:31,680 --> 04:25:33,760
want to get access to
7386
04:25:33,760 --> 04:25:35,279
this hundred and ten dollars you'll be
7387
04:25:35,279 --> 04:25:38,159
able to do something like per person
7388
04:25:38,159 --> 04:25:39,760
total
7389
04:25:39,760 --> 04:25:41,199
uh you know
7390
04:25:41,199 --> 04:25:42,880
div dot
7391
04:25:42,880 --> 04:25:44,399
inner text
7392
04:25:44,399 --> 04:25:45,840
or whatever right and that'll get you
7393
04:25:45,840 --> 04:25:48,399
access to what you're looking for
7394
04:25:48,399 --> 04:25:50,159
and that's just to show you how the
7395
04:25:50,159 --> 04:25:52,800
javascript portion will interface with
7396
04:25:52,800 --> 04:25:55,520
this app okay and then based on that
7397
04:25:55,520 --> 04:25:57,199
you're going to need to do things right
7398
04:25:57,199 --> 04:25:59,439
so for example you're going to need to
7399
04:25:59,439 --> 04:26:01,680
do some calculations that
7400
04:26:01,680 --> 04:26:04,479
you know maybe take the
7401
04:26:04,479 --> 04:26:06,000
bill
7402
04:26:06,000 --> 04:26:07,680
plus the tip
7403
04:26:07,680 --> 04:26:09,600
and then it divides that by the number
7404
04:26:09,600 --> 04:26:11,279
of people and then when you do that you
7405
04:26:11,279 --> 04:26:12,560
get this
7406
04:26:12,560 --> 04:26:14,159
so there's going to be a bunch of things
7407
04:26:14,159 --> 04:26:15,600
that you're going to need to understand
7408
04:26:15,600 --> 04:26:18,080
but take a screenshot of this actually
7409
04:26:18,080 --> 04:26:20,000
right now on your computer or with your
7410
04:26:20,000 --> 04:26:21,359
phone
7411
04:26:21,359 --> 04:26:23,600
because this will help you
7412
04:26:23,600 --> 04:26:26,640
constantly refer to certain key elements
7413
04:26:26,640 --> 04:26:29,279
on how this app is going to work
7414
04:26:29,279 --> 04:26:31,520
so i'm going to now explain and kind of
7415
04:26:31,520 --> 04:26:33,439
walk through this app and we're also
7416
04:26:33,439 --> 04:26:35,279
going to have you set it up on a new
7417
04:26:35,279 --> 04:26:38,399
repel so let's go ahead and do that now
7418
04:26:38,399 --> 04:26:39,199
so
7419
04:26:39,199 --> 04:26:41,600
this is going to be your tip calculator
7420
04:26:41,600 --> 04:26:43,279
exercise so how you're going to get
7421
04:26:43,279 --> 04:26:45,359
access to it so i'm going to zoom in
7422
04:26:45,359 --> 04:26:48,239
here so you can see everything
7423
04:26:48,239 --> 04:26:50,720
a lot more clearly
7424
04:26:50,720 --> 04:26:51,840
okay
7425
04:26:51,840 --> 04:26:53,439
and then we're going to kind of walk
7426
04:26:53,439 --> 04:26:54,960
through every single thing so on the
7427
04:26:54,960 --> 04:26:57,760
left hand side right you see projects
7428
04:26:57,760 --> 04:26:59,840
make click into projects all right now
7429
04:26:59,840 --> 04:27:01,439
when you click into projects you're
7430
04:27:01,439 --> 04:27:03,279
going to see
7431
04:27:03,279 --> 04:27:06,080
tip calculator so go ahead and click on
7432
04:27:06,080 --> 04:27:08,239
tip calculator and now you're going to
7433
04:27:08,239 --> 04:27:10,800
see exercise now don't go into the
7434
04:27:10,800 --> 04:27:12,800
solution because once again if i show
7435
04:27:12,800 --> 04:27:15,439
you how this works over here right let's
7436
04:27:15,439 --> 04:27:16,880
run this
7437
04:27:16,880 --> 04:27:18,800
and over here you'll see that we have
7438
04:27:18,800 --> 04:27:21,439
project exercises project solutions so
7439
04:27:21,439 --> 04:27:23,920
here's the tip calculator solution this
7440
04:27:23,920 --> 04:27:26,399
will be the fully working app so you'll
7441
04:27:26,399 --> 04:27:29,439
be able to type in 100 here and 10 and
7442
04:27:29,439 --> 04:27:31,120
it'll calculate everything correctly
7443
04:27:31,120 --> 04:27:32,880
you'll be able to hit plus minus minus
7444
04:27:32,880 --> 04:27:34,159
minus
7445
04:27:34,159 --> 04:27:37,040
so this right now is the working version
7446
04:27:37,040 --> 04:27:39,120
okay so if you need to see the solution
7447
04:27:39,120 --> 04:27:41,600
anytime don't go to it right now
7448
04:27:41,600 --> 04:27:43,840
do not do it but this is where the
7449
04:27:43,840 --> 04:27:46,479
solution will be and the main file that
7450
04:27:46,479 --> 04:27:48,080
you need to write code for is the
7451
04:27:48,080 --> 04:27:51,120
script.js file the javascript file
7452
04:27:51,120 --> 04:27:54,159
the index.html and the style files are
7453
04:27:54,159 --> 04:27:55,840
already completed for you so you don't
7454
04:27:55,840 --> 04:27:57,279
need to touch them if you make the
7455
04:27:57,279 --> 04:27:59,760
javascript work this entire app will
7456
04:27:59,760 --> 04:28:02,319
work okay so that's where your solution
7457
04:28:02,319 --> 04:28:05,199
is going to be but if you go to tip
7458
04:28:05,199 --> 04:28:07,439
calculator from project exercises and
7459
04:28:07,439 --> 04:28:08,439
you do
7460
04:28:08,439 --> 04:28:10,000
110
7461
04:28:10,000 --> 04:28:10,800
and
7462
04:28:10,800 --> 04:28:12,319
see nothing happens you can't change the
7463
04:28:12,319 --> 04:28:14,080
number of people it doesn't update your
7464
04:28:14,080 --> 04:28:15,120
score
7465
04:28:15,120 --> 04:28:16,880
there's no calculations being done here
7466
04:28:16,880 --> 04:28:19,920
so the javascript here is not working
7467
04:28:19,920 --> 04:28:22,479
okay so this is where your javascript is
7468
04:28:22,479 --> 04:28:24,800
not working
7469
04:28:24,800 --> 04:28:25,840
so
7470
04:28:25,840 --> 04:28:29,120
uh this is your exercise file okay now
7471
04:28:29,120 --> 04:28:31,680
what i recommend highly highly highly
7472
04:28:31,680 --> 04:28:34,159
highly recommend you don't just start
7473
04:28:34,159 --> 04:28:36,319
coding in this script.js what i
7474
04:28:36,319 --> 04:28:38,800
recommend is you create a new rupple
7475
04:28:38,800 --> 04:28:40,880
with these three files
7476
04:28:40,880 --> 04:28:44,080
right and then get started okay so let's
7477
04:28:44,080 --> 04:28:46,479
go ahead and do that now so i'm going to
7478
04:28:46,479 --> 04:28:48,479
create a new tab here
7479
04:28:48,479 --> 04:28:50,800
and we're going to go to replic.com all
7480
04:28:50,800 --> 04:28:53,760
right now we're going to choose html css
7481
04:28:53,760 --> 04:28:56,479
and javascript rupple and here i'm
7482
04:28:56,479 --> 04:28:58,560
basically going to say
7483
04:28:58,560 --> 04:29:03,600
let's go and say tip calculator exercise
7484
04:29:03,600 --> 04:29:06,319
and i will hit create rupple
7485
04:29:06,319 --> 04:29:09,359
okay now that this rupple is created
7486
04:29:09,359 --> 04:29:12,399
what i recommend you do is go back
7487
04:29:12,399 --> 04:29:14,159
go to your index
7488
04:29:14,159 --> 04:29:16,800
copy all of this so command a command c
7489
04:29:16,800 --> 04:29:20,239
or control a control c okay cop select
7490
04:29:20,239 --> 04:29:22,080
all right in here
7491
04:29:22,080 --> 04:29:23,279
copy it
7492
04:29:23,279 --> 04:29:25,279
go here and
7493
04:29:25,279 --> 04:29:27,840
delete everything in this index.html and
7494
04:29:27,840 --> 04:29:28,880
paste
7495
04:29:28,880 --> 04:29:31,040
what you just copied okay so once you
7496
04:29:31,040 --> 04:29:33,840
paste it you should see an ugly looking
7497
04:29:33,840 --> 04:29:34,880
bill
7498
04:29:34,880 --> 04:29:36,239
app okay
7499
04:29:36,239 --> 04:29:37,760
now the next thing we're gonna do is
7500
04:29:37,760 --> 04:29:39,920
we're gonna grab the style file so let's
7501
04:29:39,920 --> 04:29:43,359
go ahead and grab the style file copy it
7502
04:29:43,359 --> 04:29:46,399
come back go to your style.css delete
7503
04:29:46,399 --> 04:29:49,680
everything that's in here hit paste
7504
04:29:49,680 --> 04:29:51,520
hit run
7505
04:29:51,520 --> 04:29:54,080
and now the styling is complete so now
7506
04:29:54,080 --> 04:29:56,080
our app is looking good okay so this is
7507
04:29:56,080 --> 04:30:00,560
what style files stylesheet is for now
7508
04:30:00,560 --> 04:30:02,080
the last thing we're actually going to
7509
04:30:02,080 --> 04:30:03,279
do here
7510
04:30:03,279 --> 04:30:04,800
is
7511
04:30:04,800 --> 04:30:06,960
you see if you go to your script
7512
04:30:06,960 --> 04:30:08,560
there it's empty so we're going to go
7513
04:30:08,560 --> 04:30:10,399
ahead to our exercise
7514
04:30:10,399 --> 04:30:12,399
copy the whole thing okay command a
7515
04:30:12,399 --> 04:30:16,319
command c come over here paste it so now
7516
04:30:16,319 --> 04:30:18,239
when you look here
7517
04:30:18,239 --> 04:30:20,720
there isn't any real javascript code
7518
04:30:20,720 --> 04:30:22,800
written up okay and what i'm going to do
7519
04:30:22,800 --> 04:30:24,239
is i'm just going to collapse all of
7520
04:30:24,239 --> 04:30:26,080
these functions
7521
04:30:26,080 --> 04:30:28,159
and now what we're going to do is we're
7522
04:30:28,159 --> 04:30:30,479
actually just going to walk through
7523
04:30:30,479 --> 04:30:33,840
walk through like all of the um all of
7524
04:30:33,840 --> 04:30:35,120
the things that you need to know in the
7525
04:30:35,120 --> 04:30:36,800
gotchas right
7526
04:30:36,800 --> 04:30:39,120
and then you'll be able to just get
7527
04:30:39,120 --> 04:30:41,439
started and code it up yourself okay but
7528
04:30:41,439 --> 04:30:43,520
just let's walk through it together all
7529
04:30:43,520 --> 04:30:45,199
right so you're gonna be building the
7530
04:30:45,199 --> 04:30:47,279
tip calculator app now there are a
7531
04:30:47,279 --> 04:30:48,960
couple of things that you're gonna need
7532
04:30:48,960 --> 04:30:50,159
to know
7533
04:30:50,159 --> 04:30:52,319
all right otherwise this pro this
7534
04:30:52,319 --> 04:30:54,000
project is gonna become way harder for
7535
04:30:54,000 --> 04:30:55,840
you and i don't want that to happen okay
7536
04:30:55,840 --> 04:30:57,760
so i want it to be something that you're
7537
04:30:57,760 --> 04:30:59,439
able to kind of grasp
7538
04:30:59,439 --> 04:31:00,800
so there are a couple of functions
7539
04:31:00,800 --> 04:31:02,560
you're going to need to know their names
7540
04:31:02,560 --> 04:31:04,640
all right and you have access to them so
7541
04:31:04,640 --> 04:31:07,279
you have access to calculate bill
7542
04:31:07,279 --> 04:31:08,960
increase people decrease people these
7543
04:31:08,960 --> 04:31:10,720
are the functions you're going to create
7544
04:31:10,720 --> 04:31:12,159
these are the only three functions
7545
04:31:12,159 --> 04:31:13,680
you'll need and make sure to give them
7546
04:31:13,680 --> 04:31:16,239
this exact name they're already made for
7547
04:31:16,239 --> 04:31:18,159
you you cannot change their names if you
7548
04:31:18,159 --> 04:31:19,760
change their name your app will break
7549
04:31:19,760 --> 04:31:21,760
i'll explain why
7550
04:31:21,760 --> 04:31:24,479
these functions are hard coded inside of
7551
04:31:24,479 --> 04:31:28,080
our index.html okay so for example
7552
04:31:28,080 --> 04:31:30,880
when you are typing over here
7553
04:31:30,880 --> 04:31:33,760
typing over here every time you type on
7554
04:31:33,760 --> 04:31:34,960
key up
7555
04:31:34,960 --> 04:31:36,640
we're calling the calculate build
7556
04:31:36,640 --> 04:31:37,680
function
7557
04:31:37,680 --> 04:31:40,080
so that function is running literally
7558
04:31:40,080 --> 04:31:42,080
every single time you type here
7559
04:31:42,080 --> 04:31:44,159
that's why that function is there you
7560
04:31:44,159 --> 04:31:46,399
don't need to change that name the other
7561
04:31:46,399 --> 04:31:49,199
function i told you about for example
7562
04:31:49,199 --> 04:31:51,040
increase people and decrease people
7563
04:31:51,040 --> 04:31:53,600
function those are also written here
7564
04:31:53,600 --> 04:31:56,960
so for example you can see here increase
7565
04:31:56,960 --> 04:31:58,880
uh there's a plus sign
7566
04:31:58,880 --> 04:32:00,159
right there
7567
04:32:00,159 --> 04:32:02,399
right and then there is a minus sign
7568
04:32:02,399 --> 04:32:03,520
right there
7569
04:32:03,520 --> 04:32:06,399
and then there is a number right over
7570
04:32:06,399 --> 04:32:07,760
here
7571
04:32:07,760 --> 04:32:10,560
every single time you click this plus
7572
04:32:10,560 --> 04:32:12,479
button right over here
7573
04:32:12,479 --> 04:32:14,479
every time you click this plus button
7574
04:32:14,479 --> 04:32:17,840
this function increase people will run
7575
04:32:17,840 --> 04:32:19,520
every time you click this minus button
7576
04:32:19,520 --> 04:32:20,399
over here
7577
04:32:20,399 --> 04:32:23,760
this function decrease people will run
7578
04:32:23,760 --> 04:32:25,439
that's what's happening so that's why
7579
04:32:25,439 --> 04:32:26,800
these functions have special names do
7580
04:32:26,800 --> 04:32:28,319
not change them
7581
04:32:28,319 --> 04:32:30,159
unless you change them here and then
7582
04:32:30,159 --> 04:32:32,239
you're fine okay but i just don't want
7583
04:32:32,239 --> 04:32:33,040
you to
7584
04:32:33,040 --> 04:32:34,800
fall prey
7585
04:32:34,800 --> 04:32:36,239
the next thing
7586
04:32:36,239 --> 04:32:37,760
every time you
7587
04:32:37,760 --> 04:32:40,080
clickety clack and type anything in here
7588
04:32:40,080 --> 04:32:42,000
or here anything anything you type in
7589
04:32:42,000 --> 04:32:43,279
here here
7590
04:32:43,279 --> 04:32:45,279
you're gonna run this calculate bill
7591
04:32:45,279 --> 04:32:46,800
function
7592
04:32:46,800 --> 04:32:49,359
okay for your tip
7593
04:32:49,359 --> 04:32:51,600
um and your build total okay so
7594
04:32:51,600 --> 04:32:53,600
calculate bill
7595
04:32:53,600 --> 04:32:55,120
and then you also have calculate bill
7596
04:32:55,120 --> 04:32:57,040
twice you can see that
7597
04:32:57,040 --> 04:33:00,159
so this calculate bill is for this one
7598
04:33:00,159 --> 04:33:02,159
and this calculate bill here is for this
7599
04:33:02,159 --> 04:33:04,160
one okay so every time you type in any
7600
04:33:04,160 --> 04:33:06,080
of those fields calculate bill
7601
04:33:06,080 --> 04:33:08,320
automatically fires and runs
7602
04:33:08,320 --> 04:33:10,561
going um and then the other things
7603
04:33:10,561 --> 04:33:12,320
you'll need to know are a couple of
7604
04:33:12,320 --> 04:33:13,840
things like
7605
04:33:13,840 --> 04:33:14,719
you know
7606
04:33:14,719 --> 04:33:17,919
you have this bill input and you have a
7607
04:33:17,919 --> 04:33:20,080
tip input right so
7608
04:33:20,080 --> 04:33:23,438
you have ids like build total input
7609
04:33:23,438 --> 04:33:25,919
and you have other ids
7610
04:33:25,919 --> 04:33:27,919
like tip input so let's look for temp
7611
04:33:27,919 --> 04:33:30,958
input you have tip input okay now
7612
04:33:30,958 --> 04:33:32,480
because i'm nice
7613
04:33:32,480 --> 04:33:33,840
all right not everybody's gonna be this
7614
04:33:33,840 --> 04:33:35,680
nice but what i've done for you is i
7615
04:33:35,680 --> 04:33:38,400
have put it all into the javascript file
7616
04:33:38,400 --> 04:33:39,840
everything you need to know so you don't
7617
04:33:39,840 --> 04:33:41,160
have to go to your
7618
04:33:41,160 --> 04:33:43,919
index.html file pretty much at all okay
7619
04:33:43,919 --> 04:33:45,199
you don't have to like go and look at
7620
04:33:45,199 --> 04:33:46,799
anything
7621
04:33:46,799 --> 04:33:48,799
because what i've done here for you is
7622
04:33:48,799 --> 04:33:50,400
i've already just told you hey here are
7623
04:33:50,400 --> 04:33:52,639
the three functions you need to know
7624
04:33:52,639 --> 04:33:54,639
calculate bill increase people decrease
7625
04:33:54,639 --> 04:33:56,000
people
7626
04:33:56,000 --> 04:33:57,359
i'm already letting you know these
7627
04:33:57,359 --> 04:33:59,359
functions are hard coded in the html so
7628
04:33:59,359 --> 04:34:01,359
you cannot change their names
7629
04:34:01,359 --> 04:34:02,879
and then i'm already letting you know
7630
04:34:02,879 --> 04:34:04,160
these are all the div ids you're going
7631
04:34:04,160 --> 04:34:05,520
to need access to so you're going to
7632
04:34:05,520 --> 04:34:08,000
need access to build total input
7633
04:34:08,000 --> 04:34:09,759
this is going to be
7634
04:34:09,759 --> 04:34:13,118
this div or this input field
7635
04:34:13,118 --> 04:34:15,039
so that's going to be that input field
7636
04:34:15,039 --> 04:34:17,199
you're going to need tip input
7637
04:34:17,199 --> 04:34:18,719
okay this is going to be the user input
7638
04:34:18,719 --> 04:34:19,759
for tip
7639
04:34:19,759 --> 04:34:23,199
you're going to need access to
7640
04:34:23,199 --> 04:34:26,320
you're going to need access to so let's
7641
04:34:26,320 --> 04:34:28,799
just draw these again that's right there
7642
04:34:28,799 --> 04:34:31,118
tip input is right there you're gonna
7643
04:34:31,118 --> 04:34:33,359
need access to number of people which is
7644
04:34:33,359 --> 04:34:34,480
right there
7645
04:34:34,480 --> 04:34:37,118
this is a div okay so this is a div id
7646
04:34:37,118 --> 04:34:38,400
so once you get the div you're gonna
7647
04:34:38,400 --> 04:34:41,118
need to do dot inner text to get
7648
04:34:41,118 --> 04:34:43,278
to get these okay and then you're going
7649
04:34:43,278 --> 04:34:45,680
to need access to so you're going to
7650
04:34:45,680 --> 04:34:47,359
either for this i think you might need
7651
04:34:47,359 --> 04:34:49,520
to do dot value
7652
04:34:49,520 --> 04:34:51,359
one of those dot value or dot inner text
7653
04:34:51,359 --> 04:34:53,520
okay try both of them and it should work
7654
04:34:53,520 --> 04:34:55,118
one of them should work and then you
7655
04:34:55,118 --> 04:34:57,359
have per person total which is right
7656
04:34:57,359 --> 04:34:58,400
there
7657
04:34:58,400 --> 04:35:00,240
so those are the div ids you're going to
7658
04:35:00,240 --> 04:35:02,160
need access to i'm already telling you
7659
04:35:02,160 --> 04:35:04,400
their exact names so you don't have to
7660
04:35:04,400 --> 04:35:07,118
look in your html file
7661
04:35:07,118 --> 04:35:09,199
right over here on line 20 i'm telling
7662
04:35:09,199 --> 04:35:12,000
you hey get global access to all of
7663
04:35:12,000 --> 04:35:13,359
these inputs
7664
04:35:13,359 --> 04:35:15,039
okay meaning just get access to all of
7665
04:35:15,039 --> 04:35:17,599
these inputs right here before you start
7666
04:35:17,599 --> 04:35:19,199
creating your functions because your
7667
04:35:19,199 --> 04:35:20,799
functions are going to need access to
7668
04:35:20,799 --> 04:35:21,680
them
7669
04:35:21,680 --> 04:35:22,719
so
7670
04:35:22,719 --> 04:35:24,561
you know if i were you i'd do something
7671
04:35:24,561 --> 04:35:26,080
like const
7672
04:35:26,080 --> 04:35:28,879
let's say bill total input div or
7673
04:35:28,879 --> 04:35:31,118
something right and then or build total
7674
04:35:31,118 --> 04:35:32,000
input
7675
04:35:32,000 --> 04:35:34,639
and you can do something like document
7676
04:35:34,639 --> 04:35:35,680
dot
7677
04:35:35,680 --> 04:35:38,799
get element by id and then pass in bill
7678
04:35:38,799 --> 04:35:40,400
total input
7679
04:35:40,400 --> 04:35:42,879
okay this is just an example of how you
7680
04:35:42,879 --> 04:35:44,958
would get this specific one right over
7681
04:35:44,958 --> 04:35:46,000
here
7682
04:35:46,000 --> 04:35:48,320
all right so what i'd recommend is you
7683
04:35:48,320 --> 04:35:50,958
grab not just this one but all of them
7684
04:35:50,958 --> 04:35:53,599
so you have all four of these divs and
7685
04:35:53,599 --> 04:35:55,039
have access to them that's what i would
7686
04:35:55,039 --> 04:35:56,639
recommend you do here
7687
04:35:56,639 --> 04:35:58,480
going underneath here
7688
04:35:58,480 --> 04:36:01,359
i would i recommend that on line 24 line
7689
04:36:01,359 --> 04:36:03,599
25 get number of people from number of
7690
04:36:03,599 --> 04:36:04,799
people div
7691
04:36:04,799 --> 04:36:06,639
so once you get access
7692
04:36:06,639 --> 04:36:08,799
to these four divs bill input tip input
7693
04:36:08,799 --> 04:36:10,879
number of people div and per person
7694
04:36:10,879 --> 04:36:12,080
total div
7695
04:36:12,080 --> 04:36:13,919
you can use the number of people div and
7696
04:36:13,919 --> 04:36:16,240
from there you can actually get access
7697
04:36:16,240 --> 04:36:18,240
to the number of people by pulling the
7698
04:36:18,240 --> 04:36:19,759
dot text okay
7699
04:36:19,759 --> 04:36:22,639
and remember to type cast so anytime you
7700
04:36:22,639 --> 04:36:24,561
get a thing so let's say
7701
04:36:24,561 --> 04:36:25,680
thing
7702
04:36:25,680 --> 04:36:28,160
input and then you either do dot value
7703
04:36:28,160 --> 04:36:29,759
or dot inner text
7704
04:36:29,759 --> 04:36:31,359
but both of them are going to return to
7705
04:36:31,359 --> 04:36:33,199
you a string that's going to look like
7706
04:36:33,199 --> 04:36:34,240
this
7707
04:36:34,240 --> 04:36:36,080
and then you call the number function on
7708
04:36:36,080 --> 04:36:37,840
it and then that will turn it into an
7709
04:36:37,840 --> 04:36:40,320
actual 10 okay and then you'll be able
7710
04:36:40,320 --> 04:36:42,480
to do math on that because remember if
7711
04:36:42,480 --> 04:36:44,240
you take 10
7712
04:36:44,240 --> 04:36:46,561
string and you add another 10 string to
7713
04:36:46,561 --> 04:36:47,840
this
7714
04:36:47,840 --> 04:36:49,680
this is actually going to give you not
7715
04:36:49,680 --> 04:36:50,719
20
7716
04:36:50,719 --> 04:36:51,759
but
7717
04:36:51,759 --> 04:36:53,118
10 10 10
7718
04:36:53,118 --> 04:36:55,599
10 10 okay which is going to be silly
7719
04:36:55,599 --> 04:36:56,400
so
7720
04:36:56,400 --> 04:36:58,080
you want to do you want to do it the
7721
04:36:58,080 --> 04:36:59,599
right way okay so make sure you type
7722
04:36:59,599 --> 04:37:02,160
cast it use that number function
7723
04:37:02,160 --> 04:37:02,958
okay
7724
04:37:02,958 --> 04:37:04,400
so this is where you're gonna set up all
7725
04:37:04,400 --> 04:37:06,320
your globals and then you're gonna be
7726
04:37:06,320 --> 04:37:08,958
good to go now let's go into each of
7727
04:37:08,958 --> 04:37:10,799
these functions at a high level
7728
04:37:10,799 --> 04:37:12,480
so you're gonna create three functions
7729
04:37:12,480 --> 04:37:14,160
here
7730
04:37:14,160 --> 04:37:15,919
all right
7731
04:37:15,919 --> 04:37:17,199
and you're gonna define these three
7732
04:37:17,199 --> 04:37:20,719
functions so calculate bill
7733
04:37:20,719 --> 04:37:23,759
calculate bill will pretty much run
7734
04:37:23,759 --> 04:37:26,240
all the time like you should calculate
7735
04:37:26,240 --> 04:37:27,438
bill
7736
04:37:27,438 --> 04:37:30,160
when a user types it here automatically
7737
04:37:30,160 --> 04:37:32,080
you should calculate bill when the user
7738
04:37:32,080 --> 04:37:34,320
updates the tip you should calculate
7739
04:37:34,320 --> 04:37:36,480
bill when the user hits this plus sign
7740
04:37:36,480 --> 04:37:38,000
you should calculate the bill when the
7741
04:37:38,000 --> 04:37:39,919
user hits this minus sign
7742
04:37:39,919 --> 04:37:42,000
and ultimately what calculate bill is
7743
04:37:42,000 --> 04:37:44,561
doing is it's updating this number over
7744
04:37:44,561 --> 04:37:46,958
here the total per person owed
7745
04:37:46,958 --> 04:37:49,520
okay that's the ultimate job of
7746
04:37:49,520 --> 04:37:51,919
calculate bill okay you can see it says
7747
04:37:51,919 --> 04:37:54,000
calculate the total bill per person
7748
04:37:54,000 --> 04:37:55,919
that's what it does and then if you go
7749
04:37:55,919 --> 04:37:59,118
here there are ample ample ample amounts
7750
04:37:59,118 --> 04:38:01,278
of tips for you on how to actually do
7751
04:38:01,278 --> 04:38:03,278
this okay so i have tons of
7752
04:38:03,278 --> 04:38:04,561
notes for you
7753
04:38:04,561 --> 04:38:06,480
as you go and code this along so i hope
7754
04:38:06,480 --> 04:38:09,118
you don't have too much problems there
7755
04:38:09,118 --> 04:38:11,359
okay i'll guide you through everything
7756
04:38:11,359 --> 04:38:12,480
hey
7757
04:38:12,480 --> 04:38:14,320
get the user input and convert it into a
7758
04:38:14,320 --> 04:38:16,320
number so i'm reminding you to type cast
7759
04:38:16,320 --> 04:38:17,438
here
7760
04:38:17,438 --> 04:38:19,278
hey get the tip from user and convert it
7761
04:38:19,278 --> 04:38:21,840
into a percentage hey get the total tip
7762
04:38:21,840 --> 04:38:24,240
amount calculate the total amount and
7763
04:38:24,240 --> 04:38:26,000
then update it on the dom and show it to
7764
04:38:26,000 --> 04:38:28,240
the user right so updated on the dom
7765
04:38:28,240 --> 04:38:31,039
means update this right here
7766
04:38:31,039 --> 04:38:33,759
so calculate bill hopefully
7767
04:38:33,759 --> 04:38:36,160
that kind of makes sense at high level
7768
04:38:36,160 --> 04:38:37,919
increase people what this will do is
7769
04:38:37,919 --> 04:38:40,561
splits the bill between more people
7770
04:38:40,561 --> 04:38:41,520
all right
7771
04:38:41,520 --> 04:38:43,680
so what should happen here well it walks
7772
04:38:43,680 --> 04:38:45,759
you through it should increment the
7773
04:38:45,759 --> 04:38:48,639
amount of people
7774
04:38:48,639 --> 04:38:51,759
amount of i'll just say amount of people
7775
04:38:51,759 --> 04:38:53,359
then you update the dom with the new
7776
04:38:53,359 --> 04:38:54,879
number of people
7777
04:38:54,879 --> 04:38:56,561
all right so what does that mean
7778
04:38:56,561 --> 04:38:59,278
so you increment the amount of people
7779
04:38:59,278 --> 04:39:01,840
and then once you increment it you make
7780
04:39:01,840 --> 04:39:04,080
sure that you write that to the dom like
7781
04:39:04,080 --> 04:39:07,199
you change this one to a two for example
7782
04:39:07,199 --> 04:39:09,919
and this function runs increase people
7783
04:39:09,919 --> 04:39:12,000
increase people function runs every time
7784
04:39:12,000 --> 04:39:14,000
you hit this plus button so when you hit
7785
04:39:14,000 --> 04:39:15,359
this plus button
7786
04:39:15,359 --> 04:39:16,879
that function runs when you hit the
7787
04:39:16,879 --> 04:39:18,320
minus button
7788
04:39:18,320 --> 04:39:20,879
decrease people function runs okay
7789
04:39:20,879 --> 04:39:22,000
hopefully those are kind of
7790
04:39:22,000 --> 04:39:23,840
self-explanatory
7791
04:39:23,840 --> 04:39:25,359
uh but
7792
04:39:25,359 --> 04:39:26,879
hopefully that makes sense
7793
04:39:26,879 --> 04:39:28,480
and then at the end you want to
7794
04:39:28,480 --> 04:39:30,561
calculate the bill based on the number
7795
04:39:30,561 --> 04:39:32,160
of people so
7796
04:39:32,160 --> 04:39:34,320
i'm already giving you a hint you're
7797
04:39:34,320 --> 04:39:35,680
going to be reusing some of the
7798
04:39:35,680 --> 04:39:38,560
functions you've created previously okay
7799
04:39:38,560 --> 04:39:40,400
perfect so i'm going to close this
7800
04:39:40,400 --> 04:39:43,040
go here decrease people it splits the
7801
04:39:43,040 --> 04:39:45,040
build between fewer
7802
04:39:45,040 --> 04:39:48,560
people okay so there i'm gonna go over
7803
04:39:48,560 --> 04:39:50,480
the similarities between increase and
7804
04:39:50,480 --> 04:39:52,320
decrease people right if you look at
7805
04:39:52,320 --> 04:39:53,200
this
7806
04:39:53,200 --> 04:39:55,760
increment update the dom calculate the
7807
04:39:55,760 --> 04:39:56,638
bill
7808
04:39:56,638 --> 04:39:59,200
decrease people decrement
7809
04:39:59,200 --> 04:40:01,120
amount of people
7810
04:40:01,120 --> 04:40:02,798
update the dom with the new number of
7811
04:40:02,798 --> 04:40:03,680
people
7812
04:40:03,680 --> 04:40:05,360
and then calculate the bill based on the
7813
04:40:05,360 --> 04:40:07,680
new number of people so they're pretty
7814
04:40:07,680 --> 04:40:09,600
similar and how they work so once you
7815
04:40:09,600 --> 04:40:11,680
figure out one of them the next one will
7816
04:40:11,680 --> 04:40:13,120
be really easy
7817
04:40:13,120 --> 04:40:15,200
the only difference between decreased
7818
04:40:15,200 --> 04:40:17,120
people is you
7819
04:40:17,120 --> 04:40:19,120
when you hit plus you can go as high as
7820
04:40:19,120 --> 04:40:20,798
you want but when you hit minus you
7821
04:40:20,798 --> 04:40:22,638
shouldn't be able to go below one
7822
04:40:22,638 --> 04:40:24,400
because it wouldn't make sense right you
7823
04:40:24,400 --> 04:40:26,160
have the bill but then how come it has
7824
04:40:26,160 --> 04:40:28,000
zero people that owe it that doesn't
7825
04:40:28,000 --> 04:40:30,320
make sense so it shouldn't have zero or
7826
04:40:30,320 --> 04:40:32,638
you can't have negative people right so
7827
04:40:32,638 --> 04:40:34,160
you can't decrease the number of people
7828
04:40:34,160 --> 04:40:36,000
to zero or negative
7829
04:40:36,000 --> 04:40:38,160
so here i recommend you write a guard
7830
04:40:38,160 --> 04:40:39,920
clause i haven't shown you what a guard
7831
04:40:39,920 --> 04:40:42,480
clause is but basically it's just an if
7832
04:40:42,480 --> 04:40:43,200
here
7833
04:40:43,200 --> 04:40:44,878
it's a fancy name for just an if
7834
04:40:44,878 --> 04:40:46,400
statement and if statement is just
7835
04:40:46,400 --> 04:40:48,878
something along the lines of this okay
7836
04:40:48,878 --> 04:40:50,878
if something
7837
04:40:50,878 --> 04:40:53,440
something is not true i'm just going to
7838
04:40:53,440 --> 04:40:54,560
write it in english
7839
04:40:54,560 --> 04:40:55,760
just return
7840
04:40:55,760 --> 04:40:57,440
and what that basically means is this
7841
04:40:57,440 --> 04:40:59,040
will end the function so if you say
7842
04:40:59,040 --> 04:41:00,080
return
7843
04:41:00,080 --> 04:41:02,240
it just won't activate any lines that
7844
04:41:02,240 --> 04:41:04,160
are below it so let's say you were gonna
7845
04:41:04,160 --> 04:41:05,600
decrement the number decrement the
7846
04:41:05,600 --> 04:41:07,040
number of people here
7847
04:41:07,040 --> 04:41:09,520
but here you check for the condition is
7848
04:41:09,520 --> 04:41:12,400
people equal to one one or less you go
7849
04:41:12,400 --> 04:41:13,920
hey is people
7850
04:41:13,920 --> 04:41:16,160
less than or equal to one
7851
04:41:16,160 --> 04:41:18,798
if people is less than or equal to one
7852
04:41:18,798 --> 04:41:20,958
then don't run any of these lines of
7853
04:41:20,958 --> 04:41:22,000
code
7854
04:41:22,000 --> 04:41:23,440
right like just
7855
04:41:23,440 --> 04:41:25,840
stop the return here is just like hey
7856
04:41:25,840 --> 04:41:28,240
stop the function end it right here
7857
04:41:28,240 --> 04:41:30,240
okay and and you're not even giving it
7858
04:41:30,240 --> 04:41:32,400
anything to return just return stop
7859
04:41:32,400 --> 04:41:34,878
right here so that's an example of how
7860
04:41:34,878 --> 04:41:36,718
that would work okay so i'm gonna get
7861
04:41:36,718 --> 04:41:38,638
rid of all of that now
7862
04:41:38,638 --> 04:41:40,560
so that would be your decreased people
7863
04:41:40,560 --> 04:41:42,240
function
7864
04:41:42,240 --> 04:41:43,120
okay
7865
04:41:43,120 --> 04:41:45,760
so i hope that this exercise kind of
7866
04:41:45,760 --> 04:41:48,000
gives you everything you need to know
7867
04:41:48,000 --> 04:41:49,680
and now you should be able to get
7868
04:41:49,680 --> 04:41:51,840
started working on this project i wish
7869
04:41:51,840 --> 04:41:54,798
you the best of luck and
7870
04:41:54,798 --> 04:41:57,120
give it your best shot try as hard as
7871
04:41:57,120 --> 04:41:59,440
you possibly can i really believe you're
7872
04:41:59,440 --> 04:42:01,440
gonna make a lot of progress and i think
7873
04:42:01,440 --> 04:42:03,200
that if you really push yourself you
7874
04:42:03,200 --> 04:42:05,360
should be able to do this if you don't
7875
04:42:05,360 --> 04:42:06,560
get this done
7876
04:42:06,560 --> 04:42:08,878
don't get discouraged it's okay i'm
7877
04:42:08,878 --> 04:42:10,878
right here right we'll code this
7878
04:42:10,878 --> 04:42:13,200
together i'll show you the solution
7879
04:42:13,200 --> 04:42:14,160
next
7880
04:42:14,160 --> 04:42:16,560
um and yeah we'll just walk through this
7881
04:42:16,560 --> 04:42:19,360
together okay so pause the video now
7882
04:42:19,360 --> 04:42:21,600
give this a shot and try to nail this
7883
04:42:21,600 --> 04:42:23,200
and get this done and then what i
7884
04:42:23,200 --> 04:42:25,600
recommend is you come back and walk
7885
04:42:25,600 --> 04:42:27,200
through the solution because our
7886
04:42:27,200 --> 04:42:28,718
solutions might be different and you
7887
04:42:28,718 --> 04:42:30,878
might be able to pick up you know a lot
7888
04:42:30,878 --> 04:42:32,878
of things from how
7889
04:42:32,878 --> 04:42:34,718
maybe i solved the problem versus maybe
7890
04:42:34,718 --> 04:42:36,400
how you solve the problem okay and
7891
04:42:36,400 --> 04:42:37,840
remember in coding there always can be
7892
04:42:37,840 --> 04:42:39,600
multiple different ways and one way
7893
04:42:39,600 --> 04:42:42,638
isn't necessarily wrong okay it's kind
7894
04:42:42,638 --> 04:42:44,320
of an art form so we're gonna have
7895
04:42:44,320 --> 04:42:45,840
different ways where we get to something
7896
04:42:45,840 --> 04:42:48,240
so that's it um i hope you enjoyed this
7897
04:42:48,240 --> 04:42:50,480
and uh get to working and i'll see you
7898
04:42:50,480 --> 04:42:53,120
soon all right so i hope that you gave
7899
04:42:53,120 --> 04:42:56,080
it a try and if you got it good job for
7900
04:42:56,080 --> 04:42:58,400
solving the tip calculator if you didn't
7901
04:42:58,400 --> 04:43:01,280
it's okay we're gonna do it now let's do
7902
04:43:01,280 --> 04:43:03,200
it together so let's build a tip
7903
04:43:03,200 --> 04:43:06,320
calculator so here we're going to walk
7904
04:43:06,320 --> 04:43:07,840
through right and let's pay attention to
7905
04:43:07,840 --> 04:43:09,600
the instructions the first thing is
7906
04:43:09,600 --> 04:43:12,080
we're going to get access to all of our
7907
04:43:12,080 --> 04:43:14,240
inputs and divs okay so our bill input
7908
04:43:14,240 --> 04:43:16,160
tip input number of people div and per
7909
04:43:16,160 --> 04:43:18,080
person total diff so let's get access to
7910
04:43:18,080 --> 04:43:19,520
all of these now
7911
04:43:19,520 --> 04:43:21,920
so i'm basically going to say cons bill
7912
04:43:21,920 --> 04:43:23,600
input and we're going to say
7913
04:43:23,600 --> 04:43:26,400
document.getelementbyid
7914
04:43:26,400 --> 04:43:29,120
and we're going to say bill input total
7915
04:43:29,120 --> 04:43:31,520
build total input and then we're going
7916
04:43:31,520 --> 04:43:33,440
to get access to our tip input so we're
7917
04:43:33,440 --> 04:43:34,680
going to say
7918
04:43:34,680 --> 04:43:37,040
document.getelements by id
7919
04:43:37,040 --> 04:43:38,878
and i'm going to paste in tip input
7920
04:43:38,878 --> 04:43:40,480
because these are the specific ids i
7921
04:43:40,480 --> 04:43:43,440
need then let's get access to our number
7922
04:43:43,440 --> 04:43:45,600
of people div
7923
04:43:45,600 --> 04:43:49,520
and we're gonna do get element by id
7924
04:43:49,520 --> 04:43:51,360
and then we're gonna paste that in right
7925
04:43:51,360 --> 04:43:53,280
there number of people
7926
04:43:53,280 --> 04:43:54,798
and we're gonna do
7927
04:43:54,798 --> 04:43:58,080
get access to our per person total okay
7928
04:43:58,080 --> 04:44:01,760
and this will be per person total so
7929
04:44:01,760 --> 04:44:03,520
we're going to get access to all of this
7930
04:44:03,520 --> 04:44:05,600
and what global access means is because
7931
04:44:05,600 --> 04:44:07,600
we're defining it all the way up here
7932
04:44:07,600 --> 04:44:11,120
all of our functions will have access to
7933
04:44:11,120 --> 04:44:13,520
all of these divs
7934
04:44:13,520 --> 04:44:14,638
all right another thing we're going to
7935
04:44:14,638 --> 04:44:16,080
need is we're going to need the number
7936
04:44:16,080 --> 04:44:17,360
of people
7937
04:44:17,360 --> 04:44:19,600
from number of people div so now i'm
7938
04:44:19,600 --> 04:44:22,160
going to say let number be why i'm
7939
04:44:22,160 --> 04:44:23,760
saying let here is because the number of
7940
04:44:23,760 --> 04:44:25,520
people is going to change so sometimes
7941
04:44:25,520 --> 04:44:26,878
it's going to be 1 sometimes going to be
7942
04:44:26,878 --> 04:44:28,638
2 three four five
7943
04:44:28,638 --> 04:44:30,480
right so number of people is gonna
7944
04:44:30,480 --> 04:44:32,000
change so we're gonna say
7945
04:44:32,000 --> 04:44:34,718
let number of people equal
7946
04:44:34,718 --> 04:44:36,240
we're gonna go to our number of people
7947
04:44:36,240 --> 04:44:37,280
div
7948
04:44:37,280 --> 04:44:40,000
right and then we'll try and see if we
7949
04:44:40,000 --> 04:44:42,958
can do inner text and let's type cast it
7950
04:44:42,958 --> 04:44:45,680
into a number now let's see if any of
7951
04:44:45,680 --> 04:44:48,160
this works okay so in my calculate bill
7952
04:44:48,160 --> 04:44:49,600
i'm just going to try to do a console
7953
04:44:49,600 --> 04:44:51,200
log and i'm just going to say hey show
7954
04:44:51,200 --> 04:44:52,718
me number of people and let's just see
7955
04:44:52,718 --> 04:44:53,600
if like
7956
04:44:53,600 --> 04:44:56,080
any of this is even working right so if
7957
04:44:56,080 --> 04:44:57,760
i type in here
7958
04:44:57,760 --> 04:44:59,280
every time i
7959
04:44:59,280 --> 04:45:01,840
type it runs my calculate bill function
7960
04:45:01,840 --> 04:45:04,320
and every time it runs that it actually
7961
04:45:04,320 --> 04:45:05,520
shows me
7962
04:45:05,520 --> 04:45:07,440
my number of people
7963
04:45:07,440 --> 04:45:08,878
right
7964
04:45:08,878 --> 04:45:10,400
my number of people is one so that's why
7965
04:45:10,400 --> 04:45:12,400
i just keep saying one one one one one
7966
04:45:12,400 --> 04:45:15,760
okay now i can do console log and we can
7967
04:45:15,760 --> 04:45:18,480
also do bill input right and i can get
7968
04:45:18,480 --> 04:45:21,040
the bill input value and we can console
7969
04:45:21,040 --> 04:45:23,200
log that too so i can go here and say
7970
04:45:23,200 --> 04:45:25,760
one two three and you can see we got one
7971
04:45:25,760 --> 04:45:26,878
two three
7972
04:45:26,878 --> 04:45:28,240
at the bottom
7973
04:45:28,240 --> 04:45:30,000
right and the console log
7974
04:45:30,000 --> 04:45:33,680
abc and i got abc at the bottom
7975
04:45:33,680 --> 04:45:35,600
hopefully you can see that it's right
7976
04:45:35,600 --> 04:45:36,560
there
7977
04:45:36,560 --> 04:45:38,400
perfect so
7978
04:45:38,400 --> 04:45:41,200
it looks like i'm able to uh get
7979
04:45:41,200 --> 04:45:43,520
information from from these divs and
7980
04:45:43,520 --> 04:45:45,440
things so that's working so i'm going to
7981
04:45:45,440 --> 04:45:47,520
now remove these console logs because
7982
04:45:47,520 --> 04:45:49,520
i've verified what i needed to verify
7983
04:45:49,520 --> 04:45:50,560
now i'm just going to follow the
7984
04:45:50,560 --> 04:45:52,240
instructions that i have written here so
7985
04:45:52,240 --> 04:45:53,600
get bill
7986
04:45:53,600 --> 04:45:55,520
from user input and convert it into a
7987
04:45:55,520 --> 04:45:56,400
number
7988
04:45:56,400 --> 04:45:58,320
so i'm gonna basically say hey bill
7989
04:45:58,320 --> 04:46:00,080
input and
7990
04:46:00,080 --> 04:46:02,320
what i'm gonna say dot value should
7991
04:46:02,320 --> 04:46:04,798
honestly get me the actual
7992
04:46:04,798 --> 04:46:06,958
bill now i should just have to type
7993
04:46:06,958 --> 04:46:09,280
cassette to a number and this should get
7994
04:46:09,280 --> 04:46:10,718
get me the actual bill and now let's
7995
04:46:10,718 --> 04:46:12,160
just save it to a variable and we'll
7996
04:46:12,160 --> 04:46:15,040
call that bill let's console log bill
7997
04:46:15,040 --> 04:46:17,360
and uh let's see what happens okay so
7998
04:46:17,360 --> 04:46:21,040
let's run our app and one two three
7999
04:46:21,040 --> 04:46:23,040
right and our bill is
8000
04:46:23,040 --> 04:46:24,638
constantly being printed out to the
8001
04:46:24,638 --> 04:46:26,080
screen and it's a number so that's
8002
04:46:26,080 --> 04:46:27,440
that's good so i'm gonna remove this
8003
04:46:27,440 --> 04:46:29,680
console log don't need that anymore now
8004
04:46:29,680 --> 04:46:31,680
we're gonna get the tip from user and
8005
04:46:31,680 --> 04:46:33,760
convert it into a percentage so we're
8006
04:46:33,760 --> 04:46:35,520
going to say const tip
8007
04:46:35,520 --> 04:46:37,120
is equal to
8008
04:46:37,120 --> 04:46:38,480
we're going to do the same thing and
8009
04:46:38,480 --> 04:46:40,560
we're going to say tip input dot value
8010
04:46:40,560 --> 04:46:42,878
this time and we're going to actually
8011
04:46:42,878 --> 04:46:44,638
convert it into a percentage right
8012
04:46:44,638 --> 04:46:46,080
because that's what it says convert into
8013
04:46:46,080 --> 04:46:48,240
a percentage divided by 100 so we're
8014
04:46:48,240 --> 04:46:50,160
going to divide it by 100. now let's
8015
04:46:50,160 --> 04:46:53,360
console log the tip percentage
8016
04:46:53,360 --> 04:46:56,320
let's run this and i'm gonna do
8017
04:46:56,320 --> 04:46:59,520
10 and notice it gave me 0.1 because 10
8018
04:46:59,520 --> 04:47:03,600
divided by 100 is 0.1 so that's perfect
8019
04:47:03,600 --> 04:47:05,200
right we're getting our tip percentage
8020
04:47:05,200 --> 04:47:07,280
that means it's 10 great so i can get
8021
04:47:07,280 --> 04:47:09,040
rid of this console log don't need that
8022
04:47:09,040 --> 04:47:10,638
anymore all right now we're going to get
8023
04:47:10,638 --> 04:47:13,040
the total tip amount and how we're going
8024
04:47:13,040 --> 04:47:15,280
to get this is we're just basically
8025
04:47:15,280 --> 04:47:17,680
going to say you know it's going to be
8026
04:47:17,680 --> 04:47:21,680
bill multiplied by tip percentage right
8027
04:47:21,680 --> 04:47:22,560
so
8028
04:47:22,560 --> 04:47:24,718
now let's go ahead and console log this
8029
04:47:24,718 --> 04:47:26,878
and we're going to say tip amount tip
8030
04:47:26,878 --> 04:47:28,000
amount
8031
04:47:28,000 --> 04:47:29,280
okay and i'm going to just put it as an
8032
04:47:29,280 --> 04:47:31,600
object so it prints out kind of nicely
8033
04:47:31,600 --> 04:47:34,000
so let's go ahead and let's do
8034
04:47:34,000 --> 04:47:37,600
writing this is the same as writing this
8035
04:47:37,600 --> 04:47:39,360
both are the same so this is just like a
8036
04:47:39,360 --> 04:47:40,958
pro little pro hack all right so i'm
8037
04:47:40,958 --> 04:47:42,240
gonna say 10
8038
04:47:42,240 --> 04:47:45,040
and tip amounts did not get updated
8039
04:47:45,040 --> 04:47:46,718
interesting so let's go ahead and fix
8040
04:47:46,718 --> 04:47:49,600
that let's run this again let's say 10
8041
04:47:49,600 --> 04:47:52,000
oh because i'm not putting in a bill
8042
04:47:52,000 --> 04:47:55,440
total so let's go ahead and do 100 10 on
8043
04:47:55,440 --> 04:47:58,000
that tip amount is 10. uh what if it was
8044
04:47:58,000 --> 04:48:01,280
200.7 would be 20 dollars let's do 300
8045
04:48:01,280 --> 04:48:03,600
and you can see that tip amount is
8046
04:48:03,600 --> 04:48:06,718
calculating correctly right it says 30.
8047
04:48:06,718 --> 04:48:09,440
uh that's perfect so it looks like this
8048
04:48:09,440 --> 04:48:10,958
is working so i'm going to go ahead and
8049
04:48:10,958 --> 04:48:12,798
remove this console log don't need that
8050
04:48:12,798 --> 04:48:14,798
anymore all right now next up we need to
8051
04:48:14,798 --> 04:48:17,360
calculate the total so the total is
8052
04:48:17,360 --> 04:48:19,760
going to be tip amount plus bill so
8053
04:48:19,760 --> 04:48:21,840
let's go ahead and do const total and
8054
04:48:21,840 --> 04:48:24,718
we're going to say tip amount plus bill
8055
04:48:24,718 --> 04:48:27,600
okay let's go ahead and console.log that
8056
04:48:27,600 --> 04:48:31,440
now and let's run and when i type in 100
8057
04:48:31,440 --> 04:48:34,480
and i type in so uh 10
8058
04:48:34,480 --> 04:48:36,320
so right now tip is assuming zero
8059
04:48:36,320 --> 04:48:38,400
percent so now we have a hundred oh by
8060
04:48:38,400 --> 04:48:40,400
the way let's do that little trick we'll
8061
04:48:40,400 --> 04:48:41,600
do this
8062
04:48:41,600 --> 04:48:43,680
wrap it in an object let's try this
8063
04:48:43,680 --> 04:48:46,160
again and we're going to say 100 and
8064
04:48:46,160 --> 04:48:48,000
we're going to give a tip of 10 and you
8065
04:48:48,000 --> 04:48:50,798
can see total is 110. right let's give a
8066
04:48:50,798 --> 04:48:52,240
tip of 20
8067
04:48:52,240 --> 04:48:54,480
and total is 120.
8068
04:48:54,480 --> 04:48:56,480
so this is working exactly like how we
8069
04:48:56,480 --> 04:48:59,120
wanted to let's get rid of this now
8070
04:48:59,120 --> 04:49:00,560
now we're going to calculate the per
8071
04:49:00,560 --> 04:49:02,400
person total where we're going to divide
8072
04:49:02,400 --> 04:49:04,240
by the number of people
8073
04:49:04,240 --> 04:49:07,360
so that should be easy to do per person
8074
04:49:07,360 --> 04:49:10,240
total and we're gonna do total divided
8075
04:49:10,240 --> 04:49:12,958
by and we already have access to number
8076
04:49:12,958 --> 04:49:15,120
of people right here right this is
8077
04:49:15,120 --> 04:49:17,200
pulling number of people is pulling
8078
04:49:17,200 --> 04:49:20,400
straight from here okay so it's one and
8079
04:49:20,400 --> 04:49:22,000
we're type casting it into a number so
8080
04:49:22,000 --> 04:49:23,760
it's going to not be a string of 1 but
8081
04:49:23,760 --> 04:49:26,160
it's a actual number of one right so
8082
04:49:26,160 --> 04:49:27,840
we're going to do that divided by number
8083
04:49:27,840 --> 04:49:30,638
of people and now let's go ahead and
8084
04:49:30,638 --> 04:49:33,280
let's show that so we're going to do per
8085
04:49:33,280 --> 04:49:37,440
person total let's run this and we have
8086
04:49:37,440 --> 04:49:38,878
100
8087
04:49:38,878 --> 04:49:40,400
tip is 10
8088
04:49:40,400 --> 04:49:43,920
and per person total is 110. it's
8089
04:49:43,920 --> 04:49:45,600
because right now we're not able to
8090
04:49:45,600 --> 04:49:47,040
increase the
8091
04:49:47,040 --> 04:49:49,520
number of people right but let's say i
8092
04:49:49,520 --> 04:49:52,638
went to my html file and i just hard
8093
04:49:52,638 --> 04:49:54,560
coded that one to a two let's see what
8094
04:49:54,560 --> 04:49:56,240
happens i'm gonna hard code that to a
8095
04:49:56,240 --> 04:49:58,320
two let's run our app and let's see if
8096
04:49:58,320 --> 04:50:01,200
this works so i'm gonna say bill is
8097
04:50:01,200 --> 04:50:05,520
you know bill is 100 and then tip is 10
8098
04:50:05,520 --> 04:50:07,920
and you can see that it says 55 dollars
8099
04:50:07,920 --> 04:50:10,480
per person right which which makes ample
8100
04:50:10,480 --> 04:50:13,760
amount of cents 55 per person why
8101
04:50:13,760 --> 04:50:16,718
because it's 110 110 divided by two
8102
04:50:16,718 --> 04:50:19,840
people is 55. so now let's go and change
8103
04:50:19,840 --> 04:50:22,480
that hard-coded to back to a one let's
8104
04:50:22,480 --> 04:50:24,000
run our app again
8105
04:50:24,000 --> 04:50:26,000
and uh because we want to change that
8106
04:50:26,000 --> 04:50:28,240
dynamically right we don't want to like
8107
04:50:28,240 --> 04:50:30,320
hard code it in so make sure that this
8108
04:50:30,320 --> 04:50:32,400
is back to a one when you run your app
8109
04:50:32,400 --> 04:50:36,400
perfect so what's up next let's go down
8110
04:50:36,400 --> 04:50:37,920
and let's see what's up next and now
8111
04:50:37,920 --> 04:50:40,480
update the per person total on dom and
8112
04:50:40,480 --> 04:50:43,040
show it to user okay cool so let's go do
8113
04:50:43,040 --> 04:50:44,080
that
8114
04:50:44,080 --> 04:50:47,440
so i'm gonna grab per person
8115
04:50:47,440 --> 04:50:49,920
per person total div because i have
8116
04:50:49,920 --> 04:50:52,958
access to it from up here oh let's just
8117
04:50:52,958 --> 04:50:55,920
call it per person total diff okay so
8118
04:50:55,920 --> 04:50:57,920
per person total div we have access to
8119
04:50:57,920 --> 04:51:00,480
it up there let's p yeah per person
8120
04:51:00,480 --> 04:51:02,560
total div and we're going to change its
8121
04:51:02,560 --> 04:51:04,638
inner text to
8122
04:51:04,638 --> 04:51:07,360
uh per person total right over here okay
8123
04:51:07,360 --> 04:51:09,040
so i'm going to say per
8124
04:51:09,040 --> 04:51:10,560
person total
8125
04:51:10,560 --> 04:51:13,680
oh i'm sorry okay i'm uh yeah i'm just
8126
04:51:13,680 --> 04:51:16,560
kind of misunderstanding this but like
8127
04:51:16,560 --> 04:51:18,480
what i mean is
8128
04:51:18,480 --> 04:51:20,480
this is going to be total per person
8129
04:51:20,480 --> 04:51:21,280
okay
8130
04:51:21,280 --> 04:51:22,958
so that's
8131
04:51:22,958 --> 04:51:25,680
that's this div right here per person
8132
04:51:25,680 --> 04:51:29,280
total so we are updating it correctly
8133
04:51:29,280 --> 04:51:31,840
okay so let's just see what happens now
8134
04:51:31,840 --> 04:51:33,360
let's run this
8135
04:51:33,360 --> 04:51:36,080
let's go ahead and type in 100
8136
04:51:36,080 --> 04:51:38,480
let's type in 10 percent
8137
04:51:38,480 --> 04:51:40,000
and let's see what happens we're getting
8138
04:51:40,000 --> 04:51:42,958
back 110 so that's good but we want it
8139
04:51:42,958 --> 04:51:46,160
to be in dollars so let's go ahead and
8140
04:51:46,160 --> 04:51:48,798
do string template literals
8141
04:51:48,798 --> 04:51:50,560
and we're gonna
8142
04:51:50,560 --> 04:51:52,878
wrap it in this and just put
8143
04:51:52,878 --> 04:51:55,200
another dollar sign so
8144
04:51:55,200 --> 04:51:57,120
this dollar sign will just make sure
8145
04:51:57,120 --> 04:51:58,958
this is javascript and then this dollar
8146
04:51:58,958 --> 04:52:01,120
sign will actually put an actual dollar
8147
04:52:01,120 --> 04:52:03,360
sign so i'll say 100
8148
04:52:03,360 --> 04:52:04,798
and we'll do 10
8149
04:52:04,798 --> 04:52:06,878
and when we go here you can see that it
8150
04:52:06,878 --> 04:52:09,120
says 110
8151
04:52:09,120 --> 04:52:11,200
dollars okay
8152
04:52:11,200 --> 04:52:12,240
great
8153
04:52:12,240 --> 04:52:15,040
now what happens if we have a bill
8154
04:52:15,040 --> 04:52:17,560
that's something like this
8155
04:52:17,560 --> 04:52:20,000
148.5 what happens if we have a bill
8156
04:52:20,000 --> 04:52:21,360
that's like
8157
04:52:21,360 --> 04:52:22,638
decimals
8158
04:52:22,638 --> 04:52:25,440
you know or has maybe
8159
04:52:25,440 --> 04:52:28,560
weird decimals and now you can see
8160
04:52:28,560 --> 04:52:31,200
that it started showing like
8161
04:52:31,200 --> 04:52:33,600
lots of decimal places right like so for
8162
04:52:33,600 --> 04:52:38,958
135 dollars and 52 cents it says 149.00
8163
04:52:38,958 --> 04:52:40,718
like how are you going to pay that in
8164
04:52:40,718 --> 04:52:42,958
cents that doesn't really make sense so
8165
04:52:42,958 --> 04:52:44,798
what we want to do here is we want to do
8166
04:52:44,798 --> 04:52:47,280
a dot 2 fixed
8167
04:52:47,280 --> 04:52:49,440
so we're going to do that now so
8168
04:52:49,440 --> 04:52:51,360
you can actually
8169
04:52:51,360 --> 04:52:54,000
do dot to fix
8170
04:52:54,000 --> 04:52:56,400
two and pass in two right there and it's
8171
04:52:56,400 --> 04:52:58,240
gonna make sure that it doesn't go
8172
04:52:58,240 --> 04:53:01,280
beyond two decimals so let's try some
8173
04:53:01,280 --> 04:53:04,320
number again like that and you'll notice
8174
04:53:04,320 --> 04:53:06,080
that now it actually just goes to two
8175
04:53:06,080 --> 04:53:09,600
decimal places now what if the bill is
8176
04:53:09,600 --> 04:53:10,878
more like
8177
04:53:10,878 --> 04:53:13,840
3 000 or something like that it gives
8178
04:53:13,840 --> 04:53:16,240
you 3 300 but it doesn't you know it'd
8179
04:53:16,240 --> 04:53:18,160
be nicer if it kind of put a comma in
8180
04:53:18,160 --> 04:53:20,958
there right so if you want to kind of do
8181
04:53:20,958 --> 04:53:23,120
that what we can actually do and this is
8182
04:53:23,120 --> 04:53:24,480
a cool
8183
04:53:24,480 --> 04:53:26,798
uh trick that i learned that i use a lot
8184
04:53:26,798 --> 04:53:28,080
actually so that's why i kind of want to
8185
04:53:28,080 --> 04:53:31,280
show it off here you can do dot locale
8186
04:53:31,280 --> 04:53:34,638
string and just pass in um
8187
04:53:34,638 --> 04:53:35,680
en
8188
04:53:35,680 --> 04:53:37,440
us so it's just gonna
8189
04:53:37,440 --> 04:53:40,638
format it to us currency like a dollar
8190
04:53:40,638 --> 04:53:43,360
currency or whatever right and uh now if
8191
04:53:43,360 --> 04:53:44,560
you try
8192
04:53:44,560 --> 04:53:46,080
some bill
8193
04:53:46,080 --> 04:53:47,680
right you notice that it's actually
8194
04:53:47,680 --> 04:53:48,878
putting that
8195
04:53:48,878 --> 04:53:51,600
dollar sign and that comma or sorry not
8196
04:53:51,600 --> 04:53:53,120
the dollar sign but the comma which is
8197
04:53:53,120 --> 04:53:55,360
nice so like if you get some like i
8198
04:53:55,360 --> 04:53:56,638
don't know you're never going to have a
8199
04:53:56,638 --> 04:53:58,240
bill this much when you're eating food
8200
04:53:58,240 --> 04:54:00,320
right but all the commas are there and
8201
04:54:00,320 --> 04:54:02,320
if you get up to 25 million the commas
8202
04:54:02,320 --> 04:54:04,638
are still there so this is good now we
8203
04:54:04,638 --> 04:54:06,160
just want to make sure that we still do
8204
04:54:06,160 --> 04:54:08,480
that dot to fix thing so let's do two
8205
04:54:08,480 --> 04:54:13,040
fixed and pass it to two places right so
8206
04:54:13,040 --> 04:54:14,878
fix it to do decimals first and then
8207
04:54:14,878 --> 04:54:17,440
turn it into with commas let's try it so
8208
04:54:17,440 --> 04:54:19,520
let's try some big number
8209
04:54:19,520 --> 04:54:22,718
and let's do decimal places like crazy
8210
04:54:22,718 --> 04:54:24,718
and notice that the decimal places here
8211
04:54:24,718 --> 04:54:26,958
don't change okay
8212
04:54:26,958 --> 04:54:28,160
so great
8213
04:54:28,160 --> 04:54:29,840
all right in this case
8214
04:54:29,840 --> 04:54:31,520
all right lesson learned so it seems
8215
04:54:31,520 --> 04:54:33,920
like if you try to use dot two fixed and
8216
04:54:33,920 --> 04:54:35,920
locale string it doesn't work so just
8217
04:54:35,920 --> 04:54:37,920
pick one in this case we're just gonna
8218
04:54:37,920 --> 04:54:40,560
stick to dot to fix for now okay
8219
04:54:40,560 --> 04:54:43,680
so without making it much more complex
8220
04:54:43,680 --> 04:54:45,200
and that'll just take care of the
8221
04:54:45,200 --> 04:54:47,280
decimal places which is perfectly fine
8222
04:54:47,280 --> 04:54:49,280
actually but yeah our decimal places
8223
04:54:49,280 --> 04:54:50,638
will be fine
8224
04:54:50,638 --> 04:54:51,840
and then you can give whatever
8225
04:54:51,840 --> 04:54:53,920
percentage tip and everything will work
8226
04:54:53,920 --> 04:54:56,240
out as expected
8227
04:54:56,240 --> 04:54:58,638
so let's get rid of this console log we
8228
04:54:58,638 --> 04:55:00,878
don't need this anymore so i just got
8229
04:55:00,878 --> 04:55:02,080
rid of it
8230
04:55:02,080 --> 04:55:03,760
perfect so now it seems like our
8231
04:55:03,760 --> 04:55:06,240
calculate bill is doing
8232
04:55:06,240 --> 04:55:08,400
exactly what it needs to do write it it
8233
04:55:08,400 --> 04:55:10,160
calculates the bill in a really
8234
04:55:10,160 --> 04:55:12,400
intelligent way so now all we got to do
8235
04:55:12,400 --> 04:55:14,320
is increase and increase let's work on
8236
04:55:14,320 --> 04:55:16,080
increased people so here it says
8237
04:55:16,080 --> 04:55:18,160
increment the amount of people well i
8238
04:55:18,160 --> 04:55:20,080
already have number of people number of
8239
04:55:20,080 --> 04:55:21,920
people right here in line 29 so what i'm
8240
04:55:21,920 --> 04:55:23,200
going to do is i'm just going to copy
8241
04:55:23,200 --> 04:55:24,000
this
8242
04:55:24,000 --> 04:55:25,840
and paste it here and i'm just gonna do
8243
04:55:25,840 --> 04:55:27,920
plus equals one so we're just gonna
8244
04:55:27,920 --> 04:55:30,240
increment the number of people by one
8245
04:55:30,240 --> 04:55:31,760
and then we're gonna update it on the
8246
04:55:31,760 --> 04:55:32,878
dom
8247
04:55:32,878 --> 04:55:35,760
so we're gonna grab number of people
8248
04:55:35,760 --> 04:55:38,080
div which is gonna be
8249
04:55:38,080 --> 04:55:40,878
this div right here okay and we're just
8250
04:55:40,878 --> 04:55:42,560
going to pop in the number of people
8251
04:55:42,560 --> 04:55:44,480
that we now have so we're going to say
8252
04:55:44,480 --> 04:55:46,000
number of people
8253
04:55:46,000 --> 04:55:47,600
is equal to
8254
04:55:47,600 --> 04:55:50,560
number of people dot inner text
8255
04:55:50,560 --> 04:55:53,440
is equal to number of people okay
8256
04:55:53,440 --> 04:55:57,040
so now let's run this and let's hit plus
8257
04:55:57,040 --> 04:55:58,718
and you can see that every time i hit
8258
04:55:58,718 --> 04:56:00,798
plus it actually increments the number
8259
04:56:00,798 --> 04:56:02,560
of people and then it shows it and
8260
04:56:02,560 --> 04:56:04,718
that's why you can see that it's showing
8261
04:56:04,718 --> 04:56:07,280
it exactly how we want it to right and
8262
04:56:07,280 --> 04:56:09,440
right now minus doesn't work okay so
8263
04:56:09,440 --> 04:56:11,360
that's fine we'll do that in a second
8264
04:56:11,360 --> 04:56:14,240
but watch what happens when i do this
8265
04:56:14,240 --> 04:56:16,958
the bill doesn't calculate the right way
8266
04:56:16,958 --> 04:56:18,638
i have 10 people
8267
04:56:18,638 --> 04:56:20,718
okay so let's run this again
8268
04:56:20,718 --> 04:56:24,080
let's go a hundred dollars tip is ten
8269
04:56:24,080 --> 04:56:26,878
percent okay and let's run this
8270
04:56:26,878 --> 04:56:29,520
and you can see it's saying everybody
8271
04:56:29,520 --> 04:56:31,040
owes a hundred and ten dollars per
8272
04:56:31,040 --> 04:56:32,878
person that doesn't make sense because
8273
04:56:32,878 --> 04:56:35,440
110 dollars that's eleven hundred
8274
04:56:35,440 --> 04:56:36,798
dollars so how come the bill started
8275
04:56:36,798 --> 04:56:39,958
with 110 dollars and now we owe 100
8276
04:56:39,958 --> 04:56:43,280
110 or something's wrong so what we need
8277
04:56:43,280 --> 04:56:44,320
to do
8278
04:56:44,320 --> 04:56:46,760
is once we do this we need to
8279
04:56:46,760 --> 04:56:49,200
recalculate the bill based on the new
8280
04:56:49,200 --> 04:56:51,760
number of people which is super easy all
8281
04:56:51,760 --> 04:56:54,480
we have to do here is reuse the function
8282
04:56:54,480 --> 04:56:56,718
we just created so just call this
8283
04:56:56,718 --> 04:56:58,480
function and that should handle
8284
04:56:58,480 --> 04:57:01,920
everything so let's go ahead run this
8285
04:57:01,920 --> 04:57:03,520
i'm going to say we have a hundred
8286
04:57:03,520 --> 04:57:05,920
dollar bill i'm gonna say we're tipping
8287
04:57:05,920 --> 04:57:07,680
let's say 10
8288
04:57:07,680 --> 04:57:09,920
number of people is two and boom look at
8289
04:57:09,920 --> 04:57:13,200
that it's 55. why does it work because
8290
04:57:13,200 --> 04:57:14,160
remember
8291
04:57:14,160 --> 04:57:16,560
per person total is calculated total
8292
04:57:16,560 --> 04:57:18,878
divided by number of people so when you
8293
04:57:18,878 --> 04:57:20,798
update number of people here
8294
04:57:20,798 --> 04:57:22,560
this number of people updates and that's
8295
04:57:22,560 --> 04:57:23,920
the number of people
8296
04:57:23,920 --> 04:57:26,798
calculate bill function has access to so
8297
04:57:26,798 --> 04:57:29,040
when you run it again it just
8298
04:57:29,040 --> 04:57:31,040
recalculates this with a new number of
8299
04:57:31,040 --> 04:57:32,718
people after you change them pretty
8300
04:57:32,718 --> 04:57:35,120
beautiful right so very very simple
8301
04:57:35,120 --> 04:57:37,840
let's go ahead and do the same thing so
8302
04:57:37,840 --> 04:57:39,840
this code is going to be really really
8303
04:57:39,840 --> 04:57:42,480
really similar for decrease so decrement
8304
04:57:42,480 --> 04:57:44,958
the amount of people instead of plus one
8305
04:57:44,958 --> 04:57:46,480
we're gonna do minus one
8306
04:57:46,480 --> 04:57:48,878
number of people div we're gonna change
8307
04:57:48,878 --> 04:57:51,200
to update that and then we're gonna
8308
04:57:51,200 --> 04:57:53,040
recalculate the bill so it's gonna be
8309
04:57:53,040 --> 04:57:55,120
very similar to increase so watch what
8310
04:57:55,120 --> 04:57:57,840
happens when i have a hundred dollars i
8311
04:57:57,840 --> 04:58:00,718
have a ten percent tip i can go one way
8312
04:58:00,718 --> 04:58:03,040
and i can go backwards look at that how
8313
04:58:03,040 --> 04:58:06,240
nice but here is the problem i can go
8314
04:58:06,240 --> 04:58:09,280
more than i can go less than one watch i
8315
04:58:09,280 --> 04:58:11,600
went to negative one what is that i'm
8316
04:58:11,600 --> 04:58:13,680
going to negative numbers what is
8317
04:58:13,680 --> 04:58:16,560
happening how can i have so what the
8318
04:58:16,560 --> 04:58:19,280
restaurant now owes me for eating there
8319
04:58:19,280 --> 04:58:21,040
like they should you know what i mean
8320
04:58:21,040 --> 04:58:23,920
like the royalties there to eat
8321
04:58:23,920 --> 04:58:26,480
you are special you are a royalty so if
8322
04:58:26,480 --> 04:58:28,160
you went and the restaurant owed you
8323
04:58:28,160 --> 04:58:30,400
money i would understand okay fair
8324
04:58:30,400 --> 04:58:31,680
enough because you're here watching this
8325
04:58:31,680 --> 04:58:34,080
tutorial you're top of the food chain
8326
04:58:34,080 --> 04:58:36,080
all right while everybody struggles out
8327
04:58:36,080 --> 04:58:38,320
there you're here grinding it out so
8328
04:58:38,320 --> 04:58:39,600
then you can
8329
04:58:39,600 --> 04:58:41,120
level up your career provide for your
8330
04:58:41,120 --> 04:58:42,638
family provide for your kids you're a
8331
04:58:42,638 --> 04:58:44,240
champ tap yourself on the bat pat
8332
04:58:44,240 --> 04:58:47,280
yourself on the back do both tap and pat
8333
04:58:47,280 --> 04:58:50,320
do them both all right so we don't want
8334
04:58:50,320 --> 04:58:52,878
to go below one person so we're gonna
8335
04:58:52,878 --> 04:58:55,760
add what i call a guard clause here
8336
04:58:55,760 --> 04:58:58,240
basically going to say if
8337
04:58:58,240 --> 04:59:00,240
number of people is less than or equal
8338
04:59:00,240 --> 04:59:01,760
to one homie
8339
04:59:01,760 --> 04:59:03,920
just stop just stop
8340
04:59:03,920 --> 04:59:05,040
stop
8341
04:59:05,040 --> 04:59:06,240
don't run
8342
04:59:06,240 --> 04:59:08,718
this simply here means end the function
8343
04:59:08,718 --> 04:59:09,760
right here
8344
04:59:09,760 --> 04:59:11,600
and the decreased pupil function on line
8345
04:59:11,600 --> 04:59:14,160
70 and don't run any of this
8346
04:59:14,160 --> 04:59:15,040
so
8347
04:59:15,040 --> 04:59:17,280
let's try this again and i'll explain
8348
04:59:17,280 --> 04:59:18,878
110
8349
04:59:18,878 --> 04:59:20,958
now when i hit minus watch what happens
8350
04:59:20,958 --> 04:59:24,200
hit minus
8351
04:59:24,560 --> 04:59:26,718
nothing happens when i hit plus it works
8352
04:59:26,718 --> 04:59:28,638
when i hit minus it works but when i hit
8353
04:59:28,638 --> 04:59:30,718
minus now it doesn't work so what is
8354
04:59:30,718 --> 04:59:33,280
happening when it's one or less than one
8355
04:59:33,280 --> 04:59:36,000
it just says hey i noticed that this
8356
04:59:36,000 --> 04:59:38,240
function is running but i want you to
8357
04:59:38,240 --> 04:59:40,320
only run here and when this condition is
8358
04:59:40,320 --> 04:59:42,400
true because number of people is one
8359
04:59:42,400 --> 04:59:44,400
right just one less than or equal to one
8360
04:59:44,400 --> 04:59:46,320
yes it's true so then the code just
8361
04:59:46,320 --> 04:59:48,240
stops and doesn't end up running any of
8362
04:59:48,240 --> 04:59:50,878
this now in the other case scenario
8363
04:59:50,878 --> 04:59:52,240
where a number of people is let's say
8364
04:59:52,240 --> 04:59:53,840
two it goes as two less than or equal to
8365
04:59:53,840 --> 04:59:56,320
one and it goes false so then it doesn't
8366
04:59:56,320 --> 04:59:58,560
run this but it ends up running all of
8367
04:59:58,560 --> 05:00:00,320
these here okay so that's why it's a
8368
05:00:00,320 --> 05:00:02,160
guard clause right so we're just
8369
05:00:02,160 --> 05:00:04,718
guarding the entire code base and we
8370
05:00:04,718 --> 05:00:07,360
stop it okay to prove to you i'm just
8371
05:00:07,360 --> 05:00:11,120
going to say it's a long number of
8372
05:00:11,120 --> 05:00:12,400
people
8373
05:00:12,400 --> 05:00:14,638
so
8374
05:00:14,798 --> 05:00:18,480
i'm going to stop okay so let's go ahead
8375
05:00:18,480 --> 05:00:21,200
and uh run this and watch what happens
8376
05:00:21,200 --> 05:00:22,638
when i hit minus
8377
05:00:22,638 --> 05:00:25,840
it detects one is one or less than one
8378
05:00:25,840 --> 05:00:28,080
so i'm going to stop right when i go
8379
05:00:28,080 --> 05:00:30,400
here and try to run minus see it doesn't
8380
05:00:30,400 --> 05:00:32,160
happen it doesn't happen the console.log
8381
05:00:32,160 --> 05:00:34,320
statement doesn't happen doesn't happen
8382
05:00:34,320 --> 05:00:35,680
even here it doesn't happen even here
8383
05:00:35,680 --> 05:00:38,798
but when i go one more below boom one is
8384
05:00:38,798 --> 05:00:40,878
one or less than one so i'm gonna stop
8385
05:00:40,878 --> 05:00:42,718
one is one or less than one so i'm gonna
8386
05:00:42,718 --> 05:00:43,600
stop
8387
05:00:43,600 --> 05:00:45,840
so it's detecting it right every single
8388
05:00:45,840 --> 05:00:48,480
time every single time okay so we can
8389
05:00:48,480 --> 05:00:50,080
even like leave a little message for the
8390
05:00:50,080 --> 05:00:52,240
user and say you know
8391
05:00:52,240 --> 05:00:54,400
hey you can't
8392
05:00:54,400 --> 05:00:58,240
go you cannot less than one person
8393
05:00:58,240 --> 05:01:00,798
right so now like the user can see this
8394
05:01:00,798 --> 05:01:02,560
message and
8395
05:01:02,560 --> 05:01:05,280
no okay i can't do that right so
8396
05:01:05,280 --> 05:01:07,680
plus plus plus minus minus minus
8397
05:01:07,680 --> 05:01:10,080
hey you cannot have less than one person
8398
05:01:10,080 --> 05:01:11,360
and if you want to make it more
8399
05:01:11,360 --> 05:01:13,680
intrusive and more visible to the user
8400
05:01:13,680 --> 05:01:15,760
you can even do something like an alert
8401
05:01:15,760 --> 05:01:18,240
so now we go 100
8402
05:01:18,240 --> 05:01:20,958
10 percent tip plus plus plus minus
8403
05:01:20,958 --> 05:01:22,718
minus minus minus
8404
05:01:22,718 --> 05:01:24,958
says hey right
8405
05:01:24,958 --> 05:01:27,120
you cannot have less than one person so
8406
05:01:27,120 --> 05:01:28,958
this lets us know
8407
05:01:28,958 --> 05:01:30,080
right there that that's not a
8408
05:01:30,080 --> 05:01:32,480
possibility so you can do it whatever
8409
05:01:32,480 --> 05:01:34,638
way you want okay you could throw an
8410
05:01:34,638 --> 05:01:36,400
error if you want and like that'll kind
8411
05:01:36,400 --> 05:01:38,320
of crash the app and if you want to be
8412
05:01:38,320 --> 05:01:40,160
really pro i can give you one like kind
8413
05:01:40,160 --> 05:01:42,080
of pro javascript tip
8414
05:01:42,080 --> 05:01:45,040
is uh what you can do is you can throw
8415
05:01:45,040 --> 05:01:47,040
an error and show the user they're doing
8416
05:01:47,040 --> 05:01:48,400
something wrong but like kind of in a
8417
05:01:48,400 --> 05:01:51,920
sneaky way so here you can say you can
8418
05:01:51,920 --> 05:01:53,600
you can replace this
8419
05:01:53,600 --> 05:01:55,440
alert with something called a throw and
8420
05:01:55,440 --> 05:01:58,160
they'll throw an error so you do throw
8421
05:01:58,160 --> 05:01:59,440
and then what's nice about throw is you
8422
05:01:59,440 --> 05:02:01,520
won't even need that return like or just
8423
05:02:01,520 --> 05:02:03,040
leave the return here but like you won't
8424
05:02:03,040 --> 05:02:04,878
really need it okay so you can do that
8425
05:02:04,878 --> 05:02:07,040
return pattern i'm leaving this return
8426
05:02:07,040 --> 05:02:08,798
here to show you use a return pattern
8427
05:02:08,798 --> 05:02:10,480
again and again because that's normally
8428
05:02:10,480 --> 05:02:12,400
what i do but here if you want to throw
8429
05:02:12,400 --> 05:02:13,920
an error and really let the user know
8430
05:02:13,920 --> 05:02:15,600
you could do that right so for example
8431
05:02:15,600 --> 05:02:18,160
boom 100 10
8432
05:02:18,160 --> 05:02:20,560
i try to hit minus and it throws like a
8433
05:02:20,560 --> 05:02:22,480
red little error at the bottom says hey
8434
05:02:22,480 --> 05:02:24,320
you cannot have less than one person so
8435
05:02:24,320 --> 05:02:26,958
it's very clear that the user is trying
8436
05:02:26,958 --> 05:02:28,638
to do something that they're not allowed
8437
05:02:28,638 --> 05:02:31,760
to do and like you can make it visible
8438
05:02:31,760 --> 05:02:34,480
all right so there you go let's see if
8439
05:02:34,480 --> 05:02:36,160
this entire thing is working or not
8440
05:02:36,160 --> 05:02:38,320
let's open it up here and let's check
8441
05:02:38,320 --> 05:02:39,280
right
8442
05:02:39,280 --> 05:02:41,840
we just built this and we're excited
8443
05:02:41,840 --> 05:02:42,798
right
8444
05:02:42,798 --> 05:02:45,360
let's see if it works so we got a bill
8445
05:02:45,360 --> 05:02:47,040
we just went and ate at a nice
8446
05:02:47,040 --> 05:02:49,040
restaurant let's say you went to
8447
05:02:49,040 --> 05:02:51,600
cheesecake factory you took your family
8448
05:02:51,600 --> 05:02:54,320
out i mean it's a nice night out right
8449
05:02:54,320 --> 05:02:56,798
they gave you a big fat bill for 375
8450
05:02:56,798 --> 05:02:58,400
dollars maybe you maybe you brought some
8451
05:02:58,400 --> 05:03:00,160
friends with you and they're like hey
8452
05:03:00,160 --> 05:03:02,400
mandatory tip is 20 so you all start
8453
05:03:02,400 --> 05:03:04,080
freaking out who's gonna calculate it
8454
05:03:04,080 --> 05:03:05,760
what is it gonna be what's happening
8455
05:03:05,760 --> 05:03:09,200
right so you go okay we got the tip is
8456
05:03:09,200 --> 05:03:10,080
you know
8457
05:03:10,080 --> 05:03:12,480
10 or we're gonna give a 20 tip so
8458
05:03:12,480 --> 05:03:13,360
that's
8459
05:03:13,360 --> 05:03:15,840
oh that's 450
8460
05:03:15,840 --> 05:03:17,920
right now but you only have one you only
8461
05:03:17,920 --> 05:03:20,240
have selected as one person but luckily
8462
05:03:20,240 --> 05:03:22,718
you brought your friends with you right
8463
05:03:22,718 --> 05:03:25,600
you brought the entire gang hopefully
8464
05:03:25,600 --> 05:03:27,680
so you know you got
8465
05:03:27,680 --> 05:03:29,120
you got uh
8466
05:03:29,120 --> 05:03:32,400
juanes you got daniel you got frankie
8467
05:03:32,400 --> 05:03:35,280
you got lance you got nas
8468
05:03:35,280 --> 05:03:37,520
i mean you got the whole gang up in
8469
05:03:37,520 --> 05:03:39,760
there right so you got all these six
8470
05:03:39,760 --> 05:03:40,718
people
8471
05:03:40,718 --> 05:03:42,160
look at that
8472
05:03:42,160 --> 05:03:45,360
your bill is only 75 dollars
8473
05:03:45,360 --> 05:03:47,680
that's not too bad right that's not too
8474
05:03:47,680 --> 05:03:50,160
bad that's that's easy maybe maybe you
8475
05:03:50,160 --> 05:03:52,480
bring in two other people from somewhere
8476
05:03:52,480 --> 05:03:54,080
you go to them and you grab them and
8477
05:03:54,080 --> 05:03:55,520
you're like hey
8478
05:03:55,520 --> 05:03:57,520
come join our party and split the bill
8479
05:03:57,520 --> 05:03:59,760
with them too now the more the merrier
8480
05:03:59,760 --> 05:04:03,680
am i right you got 56.25
8481
05:04:03,680 --> 05:04:05,600
i mean this is at this point the
8482
05:04:05,600 --> 05:04:07,520
restaurant should be paying you right
8483
05:04:07,520 --> 05:04:08,878
this is nothing
8484
05:04:08,878 --> 05:04:10,878
cheesecake factory nice night out with
8485
05:04:10,878 --> 05:04:13,280
the family and friends i mean come on
8486
05:04:13,280 --> 05:04:14,878
that's a bargain
8487
05:04:14,878 --> 05:04:16,958
the 20 tip right
8488
05:04:16,958 --> 05:04:20,480
so this is this is really nice right
8489
05:04:20,480 --> 05:04:22,400
this is really nice app that we could
8490
05:04:22,400 --> 05:04:24,400
actually use and what's amazing is it's
8491
05:04:24,400 --> 05:04:26,878
already hosted online thanks to repple
8492
05:04:26,878 --> 05:04:30,480
yeah it's deployed on the interwebs so
8493
05:04:30,480 --> 05:04:31,760
you could put it on your portfolio you
8494
05:04:31,760 --> 05:04:33,440
could do whatever you want with it's
8495
05:04:33,440 --> 05:04:35,520
live ready to go
8496
05:04:35,520 --> 05:04:38,480
okay ignore that sound that was i'm
8497
05:04:38,480 --> 05:04:40,160
having a focus session and it makes this
8498
05:04:40,160 --> 05:04:43,200
little bang so now what i want you to do
8499
05:04:43,200 --> 05:04:45,680
is you're going to grab all of this
8500
05:04:45,680 --> 05:04:48,000
javascript code that you wrote
8501
05:04:48,000 --> 05:04:50,718
go back to this exercise
8502
05:04:50,718 --> 05:04:52,798
file okay
8503
05:04:52,798 --> 05:04:55,840
and go to tip calculator go to exercise
8504
05:04:55,840 --> 05:04:58,000
go to the script js okay
8505
05:04:58,000 --> 05:04:59,040
and then
8506
05:04:59,040 --> 05:05:00,400
delete everything that's in this
8507
05:05:00,400 --> 05:05:03,360
exercise file okay and then paste your
8508
05:05:03,360 --> 05:05:04,718
code in here
8509
05:05:04,718 --> 05:05:07,040
all right and give it a try and see if
8510
05:05:07,040 --> 05:05:10,320
it works so go and run it so go to your
8511
05:05:10,320 --> 05:05:12,000
now project exercises you should be able
8512
05:05:12,000 --> 05:05:14,480
to go to tip calculator
8513
05:05:14,480 --> 05:05:16,638
you should be able to
8514
05:05:16,638 --> 05:05:18,320
type in things like
8515
05:05:18,320 --> 05:05:20,240
100 10
8516
05:05:20,240 --> 05:05:22,480
plus plus plus plus minus minus minus it
8517
05:05:22,480 --> 05:05:24,000
should all
8518
05:05:24,000 --> 05:05:27,040
work smoothly okay so make sure you go
8519
05:05:27,040 --> 05:05:29,280
ahead and do that update your exercise
8520
05:05:29,280 --> 05:05:31,040
as exercise as you go so you have the
8521
05:05:31,040 --> 05:05:32,798
most up-to-date
8522
05:05:32,798 --> 05:05:34,240
version with all of your projects
8523
05:05:34,240 --> 05:05:36,240
completed all in one place nice and
8524
05:05:36,240 --> 05:05:37,680
ready for you to use
8525
05:05:37,680 --> 05:05:38,638
all right
8526
05:05:38,638 --> 05:05:40,560
that was the tip calculator i hope you
8527
05:05:40,560 --> 05:05:42,798
had fun i had a ton of fun actually
8528
05:05:42,798 --> 05:05:46,240
sharing it it's 11 53 p.m i was supposed
8529
05:05:46,240 --> 05:05:47,600
to have a date night with my girlfriend
8530
05:05:47,600 --> 05:05:49,200
we're gonna watch the boys the new
8531
05:05:49,200 --> 05:05:51,280
season of the boys is out i mean god
8532
05:05:51,280 --> 05:05:53,840
that show is good season three
8533
05:05:53,840 --> 05:05:55,200
started watching this indian movie
8534
05:05:55,200 --> 05:05:57,920
called rrr phenomenal
8535
05:05:57,920 --> 05:05:59,200
and um
8536
05:05:59,200 --> 05:06:01,040
about to move soon you know leaving
8537
05:06:01,040 --> 05:06:03,840
marina del rey man a lot's going on you
8538
05:06:03,840 --> 05:06:05,600
know a lot's going on
8539
05:06:05,600 --> 05:06:09,520
and instead of doing any of those things
8540
05:06:09,520 --> 05:06:12,718
you know i decided to be here and uh
8541
05:06:12,718 --> 05:06:14,560
build this app with you and do it
8542
05:06:14,560 --> 05:06:16,560
together so it's gonna be awesome
8543
05:06:16,560 --> 05:06:18,718
so i hope you enjoyed it i hope it was
8544
05:06:18,718 --> 05:06:20,560
awesome let's continue let's learn more
8545
05:06:20,560 --> 05:06:22,160
javascript i hope you're excited i love
8546
05:06:22,160 --> 05:06:23,840
your beautiful face and i'll see you
8547
05:06:23,840 --> 05:06:26,160
soon let's go
8548
05:06:26,160 --> 05:06:27,760
welcome back
8549
05:06:27,760 --> 05:06:30,000
now i want you to start getting ready
8550
05:06:30,000 --> 05:06:32,320
for rock paper scissors and by the way
8551
05:06:32,320 --> 05:06:34,080
just a sneak peek
8552
05:06:34,080 --> 05:06:36,240
you're going to be doing something with
8553
05:06:36,240 --> 05:06:37,600
netflix
8554
05:06:37,600 --> 05:06:39,440
soon so one of your projects is going to
8555
05:06:39,440 --> 05:06:42,080
be netflix okay so i hope you're excited
8556
05:06:42,080 --> 05:06:42,958
about that this is going to be with
8557
05:06:42,958 --> 05:06:44,560
vanilla javascript so this is just a
8558
05:06:44,560 --> 05:06:46,160
little sneak peek
8559
05:06:46,160 --> 05:06:48,000
now what we're going to be doing is i
8560
05:06:48,000 --> 05:06:49,520
want you to start getting ready for rock
8561
05:06:49,520 --> 05:06:50,718
paper scissors because we're about to
8562
05:06:50,718 --> 05:06:52,638
start building it soon but there are
8563
05:06:52,638 --> 05:06:54,080
just a few things that you need to
8564
05:06:54,080 --> 05:06:55,360
understand for this
8565
05:06:55,360 --> 05:06:57,520
all right so for rock paper scissors one
8566
05:06:57,520 --> 05:07:00,160
of the dom manipulation concepts
8567
05:07:00,160 --> 05:07:01,440
you're already good with like for
8568
05:07:01,440 --> 05:07:03,360
example when you click a button right so
8569
05:07:03,360 --> 05:07:05,280
for example if i go to rocket processor
8570
05:07:05,280 --> 05:07:06,878
solutions over here and when i click
8571
05:07:06,878 --> 05:07:08,320
this
8572
05:07:08,320 --> 05:07:09,920
to click and detect the button you've
8573
05:07:09,920 --> 05:07:11,680
already learned this concept so that's
8574
05:07:11,680 --> 05:07:12,480
good
8575
05:07:12,480 --> 05:07:14,878
um when i click the hand you've already
8576
05:07:14,878 --> 05:07:16,480
learned that concept so that's you're
8577
05:07:16,480 --> 05:07:18,240
good there
8578
05:07:18,240 --> 05:07:20,480
you learn the concept of being able to
8579
05:07:20,480 --> 05:07:22,160
hit this red button and then it clears
8580
05:07:22,160 --> 05:07:24,400
everything up right so dom manipulation
8581
05:07:24,400 --> 05:07:26,080
concepts you've learned so i'm happy
8582
05:07:26,080 --> 05:07:27,200
about that
8583
05:07:27,200 --> 05:07:28,718
i think the things that you still need
8584
05:07:28,718 --> 05:07:31,600
to know are how to select an item from
8585
05:07:31,600 --> 05:07:33,360
an array so let's go ahead and just do a
8586
05:07:33,360 --> 05:07:35,760
practice exercise together so remember
8587
05:07:35,760 --> 05:07:37,120
you're going to be coding in your
8588
05:07:37,120 --> 05:07:39,200
playground.js and i'm going to be coding
8589
05:07:39,200 --> 05:07:42,160
in just playground.js okay so this one
8590
05:07:42,160 --> 05:07:44,798
is for muah qazi
8591
05:07:44,798 --> 05:07:46,878
this other one is for you okay so this
8592
05:07:46,878 --> 05:07:48,400
one is going to be with all the finish
8593
05:07:48,400 --> 05:07:50,480
titles so
8594
05:07:50,480 --> 05:07:53,120
how do you select an um item out of an
8595
05:07:53,120 --> 05:07:55,600
array so let's practice that so here
8596
05:07:55,600 --> 05:07:57,600
let's say we create a new variable
8597
05:07:57,600 --> 05:07:59,760
called uh fruits okay i'll just do let
8598
05:07:59,760 --> 05:08:02,000
fruits
8599
05:08:02,000 --> 05:08:03,520
and let's see
8600
05:08:03,520 --> 05:08:05,840
did we make it earlier yes we did okay
8601
05:08:05,840 --> 05:08:06,718
so
8602
05:08:06,718 --> 05:08:08,798
i don't have to i'll just change this to
8603
05:08:08,798 --> 05:08:10,480
a let
8604
05:08:10,480 --> 05:08:11,360
okay
8605
05:08:11,360 --> 05:08:13,240
i'll bring this down
8606
05:08:13,240 --> 05:08:14,560
[Music]
8607
05:08:14,560 --> 05:08:16,000
you have to paste the whole thing in
8608
05:08:16,000 --> 05:08:18,480
we'll just do this
8609
05:08:18,480 --> 05:08:21,040
and i'll do it up until here
8610
05:08:21,040 --> 05:08:24,080
okay so fruits is only going to be
8611
05:08:24,080 --> 05:08:25,680
a couple of items
8612
05:08:25,680 --> 05:08:27,520
now let's go ahead and make sure that
8613
05:08:27,520 --> 05:08:29,920
fruits is we're able to console it so
8614
05:08:29,920 --> 05:08:33,400
i'm going to say console.log
8615
05:08:33,600 --> 05:08:35,600
fruits of
8616
05:08:35,600 --> 05:08:38,958
zero let's run this
8617
05:08:38,958 --> 05:08:40,798
okay one second it says it's already
8618
05:08:40,798 --> 05:08:42,400
been declared so maybe i don't need to
8619
05:08:42,400 --> 05:08:45,040
even say let here let's run this
8620
05:08:45,040 --> 05:08:48,560
okay cool so i have this fruit banana
8621
05:08:48,560 --> 05:08:51,120
showing up
8622
05:08:51,120 --> 05:08:54,878
now how would i randomly select
8623
05:08:54,878 --> 05:08:57,040
something from this array
8624
05:08:57,040 --> 05:08:59,120
okay so let's make a function we're
8625
05:08:59,120 --> 05:09:01,760
going to call it
8626
05:09:05,360 --> 05:09:08,958
okay random selection from array
8627
05:09:08,958 --> 05:09:10,718
this is the function in here we're going
8628
05:09:10,718 --> 05:09:13,600
to put our fruits and now since this is
8629
05:09:13,600 --> 05:09:15,440
inside of a function you can say kant's
8630
05:09:15,440 --> 05:09:17,120
fruits and you can create a new fruits
8631
05:09:17,120 --> 05:09:18,480
here okay
8632
05:09:18,480 --> 05:09:20,240
and the scope of this fruits is only
8633
05:09:20,240 --> 05:09:21,680
going to remain within the function and
8634
05:09:21,680 --> 05:09:23,120
outside of the function this variable
8635
05:09:23,120 --> 05:09:25,680
will be unknown okay
8636
05:09:25,680 --> 05:09:28,320
and let's move this down here
8637
05:09:28,320 --> 05:09:29,920
and let's just run this function for now
8638
05:09:29,920 --> 05:09:33,520
so i'm just going to say console.log
8639
05:09:33,520 --> 05:09:38,320
random select from gray or let's say
8640
05:09:38,320 --> 05:09:41,600
random fruit
8641
05:09:41,600 --> 05:09:46,080
okay and let's say that this takes in
8642
05:09:46,240 --> 05:09:48,718
items
8643
05:09:49,920 --> 05:09:51,280
let's say it takes in fruits and it
8644
05:09:51,280 --> 05:09:55,320
randomly returns them okay
8645
05:09:55,360 --> 05:09:57,760
let's uh take this array out i know i'm
8646
05:09:57,760 --> 05:09:59,600
making go back and forth yeah yeah yeah
8647
05:09:59,600 --> 05:10:01,600
whatever just just
8648
05:10:01,600 --> 05:10:03,760
it's okay all right i know i'm making go
8649
05:10:03,760 --> 05:10:06,000
back and forth but it's all good
8650
05:10:06,000 --> 05:10:07,120
um
8651
05:10:07,120 --> 05:10:08,560
it's not supposed to be inside of this
8652
05:10:08,560 --> 05:10:10,080
function let's put it outside of the
8653
05:10:10,080 --> 05:10:11,200
function okay because i'm making a
8654
05:10:11,200 --> 05:10:14,400
mistake so it's my fault i apologize
8655
05:10:14,400 --> 05:10:17,280
for this confusion
8656
05:10:17,280 --> 05:10:19,680
and what we want to do is
8657
05:10:19,680 --> 05:10:21,040
you want to pass it a fruit and it
8658
05:10:21,040 --> 05:10:23,280
should randomly return the fruit so an
8659
05:10:23,280 --> 05:10:25,840
example of this if you call random fruit
8660
05:10:25,840 --> 05:10:27,680
and you pass it in
8661
05:10:27,680 --> 05:10:30,000
you know let's just say
8662
05:10:30,000 --> 05:10:32,240
or whatever you can pass it numbers as
8663
05:10:32,240 --> 05:10:34,400
well so if you pass it in one and two
8664
05:10:34,400 --> 05:10:37,280
it may be randomly returns two okay and
8665
05:10:37,280 --> 05:10:39,200
if you run it again it might randomly
8666
05:10:39,200 --> 05:10:40,560
return one
8667
05:10:40,560 --> 05:10:44,320
so that's what this array here does
8668
05:10:44,798 --> 05:10:47,040
all right so it's going to get fruits
8669
05:10:47,040 --> 05:10:48,480
and it's going to randomly select now
8670
05:10:48,480 --> 05:10:50,400
how do we randomly select so one thing
8671
05:10:50,400 --> 05:10:51,798
you have to remember is we have
8672
05:10:51,798 --> 05:10:54,638
math.random and this will give us a
8673
05:10:54,638 --> 05:10:56,480
random number okay so let's try it
8674
05:10:56,480 --> 05:10:58,638
math.random
8675
05:10:58,638 --> 05:11:00,160
and i'll get a random number now if i
8676
05:11:00,160 --> 05:11:02,400
multiply it by 3
8677
05:11:02,400 --> 05:11:03,920
i'm always going to get a number that's
8678
05:11:03,920 --> 05:11:06,958
going to be less than 3.
8679
05:11:07,280 --> 05:11:09,760
now to make these numbers useful because
8680
05:11:09,760 --> 05:11:10,958
right now there are these decimal
8681
05:11:10,958 --> 05:11:13,200
numbers which are kind of useless
8682
05:11:13,200 --> 05:11:15,600
we're going to floor
8683
05:11:15,600 --> 05:11:17,680
all of this we're going to call a floor
8684
05:11:17,680 --> 05:11:19,120
on this
8685
05:11:19,120 --> 05:11:21,120
all right so now when we press up and
8686
05:11:21,120 --> 05:11:22,958
keep running it press up and enter up
8687
05:11:22,958 --> 05:11:25,280
and enter now notice i'm only going to
8688
05:11:25,280 --> 05:11:28,400
get numbers between 0 1 and 2.
8689
05:11:28,400 --> 05:11:29,360
okay
8690
05:11:29,360 --> 05:11:32,480
so i want to be able to get
8691
05:11:32,480 --> 05:11:36,560
0 1 2 and 3. so for this the range
8692
05:11:36,560 --> 05:11:39,760
should be because this is 0 1 2 3
8693
05:11:39,760 --> 05:11:41,680
i should be able to select
8694
05:11:41,680 --> 05:11:42,878
randomly
8695
05:11:42,878 --> 05:11:45,360
between these four numbers right here
8696
05:11:45,360 --> 05:11:48,080
okay so in this case i might have to do
8697
05:11:48,080 --> 05:11:49,760
math
8698
05:11:49,760 --> 05:11:52,320
multiplied by four so then i get a
8699
05:11:52,320 --> 05:11:54,160
number between zero one and two and
8700
05:11:54,160 --> 05:11:56,080
three okay so i'll always get that so
8701
05:11:56,080 --> 05:11:58,638
keep running it i'll never get four i'll
8702
05:11:58,638 --> 05:12:00,560
only get zero one and two and three
8703
05:12:00,560 --> 05:12:01,360
sorry
8704
05:12:01,360 --> 05:12:04,080
zero to three
8705
05:12:04,638 --> 05:12:07,120
okay so that's the trick that's how
8706
05:12:07,120 --> 05:12:08,958
we're gonna get that now
8707
05:12:08,958 --> 05:12:12,160
this is this over here is the max right
8708
05:12:12,160 --> 05:12:14,160
for multi where i'm multiplying it by
8709
05:12:14,160 --> 05:12:15,280
four
8710
05:12:15,280 --> 05:12:17,120
that is the
8711
05:12:17,120 --> 05:12:18,798
that's the length
8712
05:12:18,798 --> 05:12:20,560
that i want it to be
8713
05:12:20,560 --> 05:12:23,280
okay so let's go
8714
05:12:23,280 --> 05:12:25,440
create a variable called max
8715
05:12:25,440 --> 05:12:27,840
length is going to
8716
05:12:27,840 --> 05:12:31,120
it's gonna be four or yeah four
8717
05:12:31,120 --> 05:12:32,240
okay and now i'm gonna write this
8718
05:12:32,240 --> 05:12:33,600
function i'm just gonna replace it with
8719
05:12:33,600 --> 05:12:35,520
max length
8720
05:12:35,520 --> 05:12:37,360
so now when i change the max length to
8721
05:12:37,360 --> 05:12:39,760
let's say i wanted to give me between
8722
05:12:39,760 --> 05:12:41,520
three items i just change it to a three
8723
05:12:41,520 --> 05:12:43,360
i run it and now i get between zero one
8724
05:12:43,360 --> 05:12:44,480
and two
8725
05:12:44,480 --> 05:12:46,320
so this is going to be dynamically
8726
05:12:46,320 --> 05:12:48,798
changing so in order to account for that
8727
05:12:48,798 --> 05:12:51,920
we need to pull from fruit's length
8728
05:12:51,920 --> 05:12:53,840
okay so we're gonna say math.random
8729
05:12:53,840 --> 05:12:56,718
multiplied by fruits.length this will
8730
05:12:56,718 --> 05:12:59,600
give us a dynamic array
8731
05:12:59,600 --> 05:13:02,560
and then we want to
8732
05:13:02,560 --> 05:13:05,840
wrap this in math.floor
8733
05:13:05,840 --> 05:13:08,320
okay and we'll say
8734
05:13:08,320 --> 05:13:10,400
random number
8735
05:13:10,400 --> 05:13:11,520
and then what we're going to do is we're
8736
05:13:11,520 --> 05:13:13,280
just going to return
8737
05:13:13,280 --> 05:13:14,878
fruits
8738
05:13:14,878 --> 05:13:16,958
whatever that random number is so it's
8739
05:13:16,958 --> 05:13:19,040
going to be 0 1 2 3 and 4 or whatever
8740
05:13:19,040 --> 05:13:20,798
whichever one right
8741
05:13:20,798 --> 05:13:22,638
now
8742
05:13:22,638 --> 05:13:24,480
let's run our random fruit function and
8743
05:13:24,480 --> 05:13:27,200
pass it our fruits that we just created
8744
05:13:27,200 --> 05:13:29,680
right over here
8745
05:13:29,680 --> 05:13:31,200
let's run it and let's see if it does
8746
05:13:31,200 --> 05:13:34,080
what we want it to do
8747
05:13:34,240 --> 05:13:35,920
okay so i don't need to say let over
8748
05:13:35,920 --> 05:13:39,040
here let's just hit run cool so i got a
8749
05:13:39,040 --> 05:13:41,680
pear let's try it again i got a banana
8750
05:13:41,680 --> 05:13:43,760
let's try it again i got an orange let's
8751
05:13:43,760 --> 05:13:45,840
try it again orange let's try it again
8752
05:13:45,840 --> 05:13:48,878
orange let's try it again orange banana
8753
05:13:48,878 --> 05:13:50,320
pear
8754
05:13:50,320 --> 05:13:52,480
pear banana
8755
05:13:52,480 --> 05:13:54,080
right so just randomly picking and it
8756
05:13:54,080 --> 05:13:55,680
never breaks
8757
05:13:55,680 --> 05:13:56,480
okay
8758
05:13:56,480 --> 05:13:58,320
so you can see how
8759
05:13:58,320 --> 05:14:00,080
this is working right every single time
8760
05:14:00,080 --> 05:14:02,400
i run it
8761
05:14:02,400 --> 05:14:03,200
run
8762
05:14:03,200 --> 05:14:04,000
run
8763
05:14:04,000 --> 05:14:07,120
run i get back a different fruit
8764
05:14:07,120 --> 05:14:09,440
okay and so let's console log what
8765
05:14:09,440 --> 05:14:11,280
number is every single time random
8766
05:14:11,280 --> 05:14:13,680
number
8767
05:14:14,080 --> 05:14:15,760
okay let's run it
8768
05:14:15,760 --> 05:14:17,360
boom you can see the random number is
8769
05:14:17,360 --> 05:14:18,160
two
8770
05:14:18,160 --> 05:14:20,400
so it gets zero one and two so i get the
8771
05:14:20,400 --> 05:14:22,560
orange random number is one so i get the
8772
05:14:22,560 --> 05:14:25,840
apple one apple zero banana
8773
05:14:25,840 --> 05:14:28,160
okay so that's how this random fruit
8774
05:14:28,160 --> 05:14:29,600
function
8775
05:14:29,600 --> 05:14:30,480
uh
8776
05:14:30,480 --> 05:14:33,360
works so this is definitely one that you
8777
05:14:33,360 --> 05:14:35,520
needed to know because
8778
05:14:35,520 --> 05:14:37,360
in order to be able to do what you're
8779
05:14:37,360 --> 05:14:39,200
going to do in
8780
05:14:39,200 --> 05:14:41,040
rock paper scissors you're going to need
8781
05:14:41,040 --> 05:14:43,440
to know how this works
8782
05:14:43,440 --> 05:14:45,120
more specifically speaking there's going
8783
05:14:45,120 --> 05:14:46,718
to be a function in rock paper scissors
8784
05:14:46,718 --> 05:14:47,600
that you're going to need to implement
8785
05:14:47,600 --> 05:14:49,360
which is going to get a random computer
8786
05:14:49,360 --> 05:14:50,240
choice
8787
05:14:50,240 --> 05:14:51,680
and the array you're going to have is
8788
05:14:51,680 --> 05:14:53,280
going to be rock
8789
05:14:53,280 --> 05:14:54,560
paper
8790
05:14:54,560 --> 05:14:56,560
and scissors
8791
05:14:56,560 --> 05:14:57,840
and you're going to use need to
8792
05:14:57,840 --> 05:14:59,680
essentially lose the same logic to
8793
05:14:59,680 --> 05:15:01,600
randomly pull a computer choice out of
8794
05:15:01,600 --> 05:15:04,080
this array of three elements
8795
05:15:04,080 --> 05:15:06,560
rock paper or scissors
8796
05:15:06,560 --> 05:15:08,160
okay so you're gonna have to figure that
8797
05:15:08,160 --> 05:15:09,200
one out
8798
05:15:09,200 --> 05:15:10,798
one more thing you're gonna need to know
8799
05:15:10,798 --> 05:15:12,958
before we get into rock paper scissors
8800
05:15:12,958 --> 05:15:14,000
is
8801
05:15:14,000 --> 05:15:18,160
so this shows you select a random
8802
05:15:18,160 --> 05:15:21,280
element from an array that's what this
8803
05:15:21,280 --> 05:15:23,360
piece of code shows you
8804
05:15:23,360 --> 05:15:24,878
the next thing you're actually going to
8805
05:15:24,878 --> 05:15:26,240
need to know
8806
05:15:26,240 --> 05:15:28,000
is
8807
05:15:28,000 --> 05:15:30,000
some more advanced conditional
8808
05:15:30,000 --> 05:15:32,560
statements meaning how do you chain if
8809
05:15:32,560 --> 05:15:35,120
with an else if with an else
8810
05:15:35,120 --> 05:15:36,798
so you're going to need to know that
8811
05:15:36,798 --> 05:15:37,920
okay because that's how you're going to
8812
05:15:37,920 --> 05:15:39,680
be able to implement your rock paper
8813
05:15:39,680 --> 05:15:41,360
scissors game
8814
05:15:41,360 --> 05:15:44,638
so i'll give you an exercise
8815
05:15:44,638 --> 05:15:47,120
there are three different weathers
8816
05:15:47,120 --> 05:15:48,400
rainy
8817
05:15:48,400 --> 05:15:50,480
sunny and overcast
8818
05:15:50,480 --> 05:15:51,280
okay
8819
05:15:51,280 --> 05:15:53,440
and what we want to do is if the weather
8820
05:15:53,440 --> 05:15:56,638
is rainy i love rain so we should give
8821
05:15:56,638 --> 05:16:00,000
that a plus one score or a one score
8822
05:16:00,000 --> 05:16:01,840
uh we don't need to put plus one here
8823
05:16:01,840 --> 05:16:03,360
right
8824
05:16:03,360 --> 05:16:05,360
uh if it's sunny then we give that a
8825
05:16:05,360 --> 05:16:07,680
score of negative one because i don't
8826
05:16:07,680 --> 05:16:09,440
like sunny weather
8827
05:16:09,440 --> 05:16:10,840
okay
8828
05:16:10,840 --> 05:16:13,920
so i freaking love rain man like i love
8829
05:16:13,920 --> 05:16:16,320
rain rain is so awesome
8830
05:16:16,320 --> 05:16:17,280
sunny
8831
05:16:17,280 --> 05:16:19,440
um it's all right
8832
05:16:19,440 --> 05:16:22,798
and then overcast would be zero
8833
05:16:22,798 --> 05:16:26,000
i also i mean like for me i also love
8834
05:16:26,000 --> 05:16:27,760
overcast so
8835
05:16:27,760 --> 05:16:30,000
it is what it is you know um i would
8836
05:16:30,000 --> 05:16:32,480
want to give this a score of one but
8837
05:16:32,480 --> 05:16:34,160
since for this example we're just gonna
8838
05:16:34,160 --> 05:16:36,080
do this okay because i like rain more
8839
05:16:36,080 --> 05:16:37,840
than i like overcast so this is how
8840
05:16:37,840 --> 05:16:41,560
we're gonna do this app
8841
05:16:42,958 --> 05:16:45,200
weather scorer this is gonna be our
8842
05:16:45,200 --> 05:16:46,638
function
8843
05:16:46,638 --> 05:16:48,878
and how are we gonna do this so see if
8844
05:16:48,878 --> 05:16:50,638
you could give this a try and write
8845
05:16:50,638 --> 05:16:52,000
you're gonna need to write essentially
8846
05:16:52,000 --> 05:16:55,840
three if else if else conditions okay
8847
05:16:55,840 --> 05:16:57,280
and they're gonna need to follow this
8848
05:16:57,280 --> 05:16:58,958
pattern let's go ahead and do it
8849
05:16:58,958 --> 05:17:00,000
together if you can't figure out
8850
05:17:00,000 --> 05:17:01,360
yourself that's totally fine okay i'm
8851
05:17:01,360 --> 05:17:03,840
gonna give you five seconds five four
8852
05:17:03,840 --> 05:17:06,878
three two one all right let's do this
8853
05:17:06,878 --> 05:17:08,878
together now so i'm going to basically
8854
05:17:08,878 --> 05:17:10,638
say
8855
05:17:10,638 --> 05:17:11,680
if
8856
05:17:11,680 --> 05:17:13,520
so the the weather score right it's
8857
05:17:13,520 --> 05:17:15,680
going to take in an input from the user
8858
05:17:15,680 --> 05:17:16,958
so it's going to take in what the
8859
05:17:16,958 --> 05:17:19,200
weather is right now so we're going to
8860
05:17:19,200 --> 05:17:21,280
say if the weather
8861
05:17:21,280 --> 05:17:23,600
is rainy and you can use three equal
8862
05:17:23,600 --> 05:17:25,680
signs here as well or two equal signs
8863
05:17:25,680 --> 05:17:28,560
both will work in this case
8864
05:17:28,560 --> 05:17:30,160
if weather is rainy
8865
05:17:30,160 --> 05:17:32,000
and we'll also create a variable called
8866
05:17:32,000 --> 05:17:33,920
score that'll keep track of the overall
8867
05:17:33,920 --> 05:17:35,120
score
8868
05:17:35,120 --> 05:17:38,240
i'm going to say if weather is rainy
8869
05:17:38,240 --> 05:17:41,520
then we're going to do score equals one
8870
05:17:41,520 --> 05:17:43,280
okay
8871
05:17:43,280 --> 05:17:46,798
and now we're going to say elsif
8872
05:17:47,680 --> 05:17:49,040
weather
8873
05:17:49,040 --> 05:17:52,240
is sunny
8874
05:17:52,240 --> 05:17:54,160
we're going to say
8875
05:17:54,160 --> 05:17:55,200
score
8876
05:17:55,200 --> 05:17:57,040
is equal to
8877
05:17:57,040 --> 05:17:59,200
negative one
8878
05:17:59,200 --> 05:18:01,040
and now you could do an else because
8879
05:18:01,040 --> 05:18:02,560
we're just going to assume it's overcast
8880
05:18:02,560 --> 05:18:04,000
because those are three choices so you
8881
05:18:04,000 --> 05:18:06,480
could do else or you could also do an
8882
05:18:06,480 --> 05:18:08,798
else if okay because you can chain as
8883
05:18:08,798 --> 05:18:11,440
many elsifs as you want but you can only
8884
05:18:11,440 --> 05:18:13,840
have one else and then we're gonna say
8885
05:18:13,840 --> 05:18:15,200
else
8886
05:18:15,200 --> 05:18:16,160
score
8887
05:18:16,160 --> 05:18:17,520
is
8888
05:18:17,520 --> 05:18:19,520
zero
8889
05:18:19,520 --> 05:18:21,440
and then at the end we want to return
8890
05:18:21,440 --> 05:18:23,520
score
8891
05:18:23,520 --> 05:18:27,040
okay so hopefully you either got this or
8892
05:18:27,040 --> 05:18:29,760
try it now without like removing your
8893
05:18:29,760 --> 05:18:31,120
code from scratch and then writing it
8894
05:18:31,120 --> 05:18:32,400
again from scratch and you can try it
8895
05:18:32,400 --> 05:18:34,878
that way too and see if you can get it
8896
05:18:34,878 --> 05:18:36,638
and now we're gonna call
8897
05:18:36,638 --> 05:18:38,958
console.log our weather
8898
05:18:38,958 --> 05:18:41,520
scorer app that we just made
8899
05:18:41,520 --> 05:18:45,120
and we're gonna give our weather score
8900
05:18:45,120 --> 05:18:47,040
rainy and let's see what score it
8901
05:18:47,040 --> 05:18:48,400
returns
8902
05:18:48,400 --> 05:18:50,638
wait let's uh comment out our allure
8903
05:18:50,638 --> 05:18:53,040
fruit stuff up here
8904
05:18:53,040 --> 05:18:54,878
let's run it
8905
05:18:54,878 --> 05:18:56,718
and you can see we got back one because
8906
05:18:56,718 --> 05:19:01,080
it's rainy now let's try sunny
8907
05:19:01,600 --> 05:19:03,840
now that's a negative one
8908
05:19:03,840 --> 05:19:04,878
okay
8909
05:19:04,878 --> 05:19:08,320
and then we have
8910
05:19:08,798 --> 05:19:11,520
overcast
8911
05:19:13,200 --> 05:19:14,638
there we go
8912
05:19:14,638 --> 05:19:16,958
now if you want to take this to a next
8913
05:19:16,958 --> 05:19:18,958
level you could even chain these so for
8914
05:19:18,958 --> 05:19:23,040
example we could even chain these and go
8915
05:19:23,440 --> 05:19:26,638
uh if weather equals equals rainy and
8916
05:19:26,638 --> 05:19:29,440
maybe you can have two types of weathers
8917
05:19:29,440 --> 05:19:30,958
you can give you can have somebody can
8918
05:19:30,958 --> 05:19:34,080
give you multiple like um
8919
05:19:34,080 --> 05:19:35,600
weathers or something right like you
8920
05:19:35,600 --> 05:19:36,958
could get
8921
05:19:36,958 --> 05:19:39,120
they could give you it's rainy but then
8922
05:19:39,120 --> 05:19:41,280
there's also overcast
8923
05:19:41,280 --> 05:19:43,600
or you know it's a mixture of two so
8924
05:19:43,600 --> 05:19:45,680
then you can chain them together like
8925
05:19:45,680 --> 05:19:48,480
that so you could go
8926
05:19:48,560 --> 05:19:50,718
if weather is rainy let's just say i'm
8927
05:19:50,718 --> 05:19:52,240
giving i'm just gonna give a very rough
8928
05:19:52,240 --> 05:19:54,400
example so we're gonna say weather
8929
05:19:54,400 --> 05:19:56,878
and then we're gonna say whether two
8930
05:19:56,878 --> 05:19:57,540
and whether
8931
05:19:57,540 --> 05:20:00,320
[Music]
8932
05:20:00,320 --> 05:20:02,400
two is
8933
05:20:02,400 --> 05:20:04,160
overcast
8934
05:20:04,160 --> 05:20:06,000
then maybe this gets a score of two
8935
05:20:06,000 --> 05:20:07,680
because it's two of my favorite
8936
05:20:07,680 --> 05:20:11,040
weather's in one where there is rain
8937
05:20:11,040 --> 05:20:13,120
and there's overcast
8938
05:20:13,120 --> 05:20:15,360
all right so here's an example of doing
8939
05:20:15,360 --> 05:20:17,680
that yeah i have two other conditions
8940
05:20:17,680 --> 05:20:19,840
here and i'm checking if both are true
8941
05:20:19,840 --> 05:20:21,440
then score is one
8942
05:20:21,440 --> 05:20:22,480
now
8943
05:20:22,480 --> 05:20:24,400
let's go and pass both of these
8944
05:20:24,400 --> 05:20:26,480
conditions so i'll say
8945
05:20:26,480 --> 05:20:28,958
uh first weather is rainy and then the
8946
05:20:28,958 --> 05:20:31,680
next weather is
8947
05:20:31,680 --> 05:20:34,160
overcast right in this order oh wait
8948
05:20:34,160 --> 05:20:34,958
sorry
8949
05:20:34,958 --> 05:20:37,360
overcast let's put overcast here and
8950
05:20:37,360 --> 05:20:39,680
we'll say the score would be two points
8951
05:20:39,680 --> 05:20:41,040
in that case
8952
05:20:41,040 --> 05:20:43,040
so if whether one is rainy let's give
8953
05:20:43,040 --> 05:20:45,440
that a try and boom we got a score of
8954
05:20:45,440 --> 05:20:46,878
two
8955
05:20:46,878 --> 05:20:47,840
okay
8956
05:20:47,840 --> 05:20:50,000
so that way you can actually chain
8957
05:20:50,000 --> 05:20:52,080
conditions okay and then here let's see
8958
05:20:52,080 --> 05:20:54,160
if i can do this right so let's go else
8959
05:20:54,160 --> 05:20:55,040
if
8960
05:20:55,040 --> 05:20:56,638
and now we'll do another else if right
8961
05:20:56,638 --> 05:20:57,920
here
8962
05:20:57,920 --> 05:21:01,040
and then in this elsif i'm going to say
8963
05:21:01,040 --> 05:21:04,240
check if just the first weather is
8964
05:21:04,240 --> 05:21:08,480
rainy then give it a score of one let's
8965
05:21:08,480 --> 05:21:09,760
remove that
8966
05:21:09,760 --> 05:21:11,600
boom let's bring this all together so
8967
05:21:11,600 --> 05:21:15,280
now let's see if i pass just rainy
8968
05:21:15,280 --> 05:21:17,120
and maybe i don't even pass a second
8969
05:21:17,120 --> 05:21:19,440
argument let's watch what happens
8970
05:21:19,440 --> 05:21:22,080
boom i get a score of one
8971
05:21:22,080 --> 05:21:23,600
okay let's try
8972
05:21:23,600 --> 05:21:26,080
i pass sunny here and let's run this
8973
05:21:26,080 --> 05:21:28,320
i still get a score of one so watch
8974
05:21:28,320 --> 05:21:29,840
what's happening
8975
05:21:29,840 --> 05:21:31,920
this condition ran over here and it said
8976
05:21:31,920 --> 05:21:34,718
hey check if the weather is rainy and
8977
05:21:34,718 --> 05:21:37,120
weather two is overcast
8978
05:21:37,120 --> 05:21:38,080
okay
8979
05:21:38,080 --> 05:21:40,798
so we ran this and we saw actually you
8980
05:21:40,798 --> 05:21:43,760
know what this is true but this one over
8981
05:21:43,760 --> 05:21:46,638
here is false
8982
05:21:47,120 --> 05:21:49,440
because of this and statement here both
8983
05:21:49,440 --> 05:21:51,840
of this becomes false so the entire
8984
05:21:51,840 --> 05:21:53,600
things the entire thing becomes false so
8985
05:21:53,600 --> 05:21:55,760
then we don't run this check then we go
8986
05:21:55,760 --> 05:21:57,520
to this else if here
8987
05:21:57,520 --> 05:21:59,120
then we check
8988
05:21:59,120 --> 05:22:00,878
is weather equal to rainy it's the first
8989
05:22:00,878 --> 05:22:02,638
weather equal to rainy and we go yes it
8990
05:22:02,638 --> 05:22:05,680
is and then it gives it a score of one
8991
05:22:05,680 --> 05:22:08,240
okay and the order here matters because
8992
05:22:08,240 --> 05:22:11,760
if you put this statement above
8993
05:22:11,760 --> 05:22:12,798
well then
8994
05:22:12,798 --> 05:22:15,280
if the weather was rainy and overcast it
8995
05:22:15,280 --> 05:22:18,080
would still only get a score of one
8996
05:22:18,080 --> 05:22:20,080
because it would run this statement and
8997
05:22:20,080 --> 05:22:21,600
then just end
8998
05:22:21,600 --> 05:22:23,760
because it's chained with an else if not
8999
05:22:23,760 --> 05:22:25,200
an if
9000
05:22:25,200 --> 05:22:28,400
so else if meaning if this if runs then
9001
05:22:28,400 --> 05:22:30,480
this if will not run
9002
05:22:30,480 --> 05:22:32,160
but if you remove that with just an if
9003
05:22:32,160 --> 05:22:34,480
now both of these will run regardless
9004
05:22:34,480 --> 05:22:36,958
okay so that's why this statement elsif
9005
05:22:36,958 --> 05:22:38,638
is like an important one to keep in mind
9006
05:22:38,638 --> 05:22:40,798
and also you can check multiple things
9007
05:22:40,798 --> 05:22:42,958
why this will matter is in rock paper
9008
05:22:42,958 --> 05:22:45,120
scissors you'll have computer choice
9009
05:22:45,120 --> 05:22:46,798
here
9010
05:22:46,798 --> 05:22:49,200
and you'll have player choice here and
9011
05:22:49,200 --> 05:22:51,200
you'll need to compare if
9012
05:22:51,200 --> 05:22:53,440
you know rock
9013
05:22:53,440 --> 05:22:55,760
and maybe the computer picked or sorry
9014
05:22:55,760 --> 05:22:57,520
let's it's yeah so let's say computer
9015
05:22:57,520 --> 05:22:58,480
picked
9016
05:22:58,480 --> 05:22:59,360
rock
9017
05:22:59,360 --> 05:23:01,360
and you picked
9018
05:23:01,360 --> 05:23:03,040
um
9019
05:23:03,040 --> 05:23:06,080
let's say scissors or paper
9020
05:23:06,080 --> 05:23:08,240
you'll have to give yourself a score of
9021
05:23:08,240 --> 05:23:11,600
one whether you won or not
9022
05:23:11,600 --> 05:23:13,120
okay so if you want in this case you
9023
05:23:13,120 --> 05:23:14,480
have to give a score of one otherwise
9024
05:23:14,480 --> 05:23:17,360
zero otherwise negative one if you lost
9025
05:23:17,360 --> 05:23:19,200
so this will come in handy when you're
9026
05:23:19,200 --> 05:23:20,718
building that game okay so this is just
9027
05:23:20,718 --> 05:23:22,480
kind of foreshadowing that so this was a
9028
05:23:22,480 --> 05:23:24,718
little contrived example
9029
05:23:24,718 --> 05:23:26,718
but if you hung in there and you did it
9030
05:23:26,718 --> 05:23:28,638
great job pat on the back
9031
05:23:28,638 --> 05:23:31,440
and with that said i mean you're
9032
05:23:31,440 --> 05:23:33,280
you know at this point you're pretty
9033
05:23:33,280 --> 05:23:36,320
much ready to actually start building
9034
05:23:36,320 --> 05:23:38,240
out rock paper scissors i hope you're
9035
05:23:38,240 --> 05:23:40,558
excited all right i hope you're excited
9036
05:23:40,558 --> 05:23:42,638
we're now going to work on the rock
9037
05:23:42,638 --> 05:23:45,680
paper scissors app so let's go back here
9038
05:23:45,680 --> 05:23:48,160
now where you're going to find this app
9039
05:23:48,160 --> 05:23:50,160
is going to be in your projects over
9040
05:23:50,160 --> 05:23:53,120
here okay so let me go back to this here
9041
05:23:53,120 --> 05:23:55,360
so i i can get you a little familiar
9042
05:23:55,360 --> 05:23:56,958
with this if you haven't really been
9043
05:23:56,958 --> 05:23:59,680
familiarized with this yet so what this
9044
05:23:59,680 --> 05:24:02,160
is on this right hand side is you have
9045
05:24:02,160 --> 05:24:04,160
some lessons you have project exercises
9046
05:24:04,160 --> 05:24:07,040
and project solutions okay so these are
9047
05:24:07,040 --> 05:24:09,360
fully working apps
9048
05:24:09,360 --> 05:24:12,558
got it these over here are exercises
9049
05:24:12,558 --> 05:24:14,638
meaning when you go to rock paper
9050
05:24:14,638 --> 05:24:16,718
scissors in under solutions you're going
9051
05:24:16,718 --> 05:24:19,520
to notice that the entire app works but
9052
05:24:19,520 --> 05:24:21,760
if you hit refresh here and you go into
9053
05:24:21,760 --> 05:24:23,920
project exercises again and you go rock
9054
05:24:23,920 --> 05:24:26,480
paper scissors under project exercises
9055
05:24:26,480 --> 05:24:28,638
you will click and nothing will work
9056
05:24:28,638 --> 05:24:31,440
meaning that the entire app is made but
9057
05:24:31,440 --> 05:24:33,200
it's missing the javascript
9058
05:24:33,200 --> 05:24:35,760
functionality okay so
9059
05:24:35,760 --> 05:24:37,520
nothing dynamic is happening with this
9060
05:24:37,520 --> 05:24:39,920
app as a result of that so that's where
9061
05:24:39,920 --> 05:24:43,200
exercises will show up now to find where
9062
05:24:43,200 --> 05:24:44,798
to go and code these exercises on the
9063
05:24:44,798 --> 05:24:46,240
left hand side you have this folder
9064
05:24:46,240 --> 05:24:48,080
called projects so you're going to click
9065
05:24:48,080 --> 05:24:49,120
projects
9066
05:24:49,120 --> 05:24:51,040
inside here you're going to have rock
9067
05:24:51,040 --> 05:24:53,200
paper scissors i'm going to click rock
9068
05:24:53,200 --> 05:24:55,120
paper scissors so we're going to go to
9069
05:24:55,120 --> 05:24:56,878
rock paper scissors and here you're
9070
05:24:56,878 --> 05:24:58,320
going to see that this project has an
9071
05:24:58,320 --> 05:25:00,320
exercise and a solution and every
9072
05:25:00,320 --> 05:25:02,718
project will have its own exercise and
9073
05:25:02,718 --> 05:25:05,520
will have its own solution folder okay
9074
05:25:05,520 --> 05:25:08,000
so if you look at them exercise solution
9075
05:25:08,000 --> 05:25:11,760
folder uh the exercise folder has
9076
05:25:11,760 --> 05:25:14,558
your html your javascript and your style
9077
05:25:14,558 --> 05:25:15,440
sheet
9078
05:25:15,440 --> 05:25:17,760
solution has the same thing okay the
9079
05:25:17,760 --> 05:25:20,000
only difference between the solution and
9080
05:25:20,000 --> 05:25:22,080
the exercise folders is going to be the
9081
05:25:22,080 --> 05:25:24,480
script this is the only thing that's
9082
05:25:24,480 --> 05:25:27,120
different between the two okay these two
9083
05:25:27,120 --> 05:25:30,400
are not the same you will need to update
9084
05:25:30,400 --> 05:25:32,638
this inside of exercise
9085
05:25:32,638 --> 05:25:35,120
i would highly recommend do not look at
9086
05:25:35,120 --> 05:25:38,160
the solution sheet until you give this
9087
05:25:38,160 --> 05:25:41,280
project a try on your own okay that's
9088
05:25:41,280 --> 05:25:42,798
gonna be that's not good you're not
9089
05:25:42,798 --> 05:25:43,920
gonna improve like that okay so
9090
05:25:43,920 --> 05:25:45,200
seriously just like don't do it it's
9091
05:25:45,200 --> 05:25:46,718
gonna be a waste of your time waste of
9092
05:25:46,718 --> 05:25:48,080
your effort like otherwise why are you
9093
05:25:48,080 --> 05:25:49,520
watching this tutorial just don't watch
9094
05:25:49,520 --> 05:25:51,520
it go do something else right so if
9095
05:25:51,520 --> 05:25:53,840
you're gonna do it then do it right make
9096
05:25:53,840 --> 05:25:56,400
sure you do the exercise first and give
9097
05:25:56,400 --> 05:25:58,160
it your best attempt even if it takes
9098
05:25:58,160 --> 05:26:00,638
you hours or days it doesn't matter
9099
05:26:00,638 --> 05:26:02,320
do that because you're gonna get better
9100
05:26:02,320 --> 05:26:05,280
so let's go into you know this file
9101
05:26:05,280 --> 05:26:07,760
right over here uh let's so what we're
9102
05:26:07,760 --> 05:26:09,440
gonna do is we're gonna familiarize
9103
05:26:09,440 --> 05:26:12,840
ourselves with what's inside of here
9104
05:26:12,840 --> 05:26:16,240
okay um and then we're probably gonna
9105
05:26:16,240 --> 05:26:18,958
move over into a new rebel okay so i
9106
05:26:18,958 --> 05:26:20,638
want you to actually do this exercise
9107
05:26:20,638 --> 05:26:22,160
but outside of here because if you try
9108
05:26:22,160 --> 05:26:23,440
to do this here
9109
05:26:23,440 --> 05:26:25,680
it's not gonna be the best experience
9110
05:26:25,680 --> 05:26:27,360
plus i want you to become a better
9111
05:26:27,360 --> 05:26:29,760
developer so i want you to actually
9112
05:26:29,760 --> 05:26:32,080
create a new rupple with all three of
9113
05:26:32,080 --> 05:26:33,760
these files in there and then try to
9114
05:26:33,760 --> 05:26:35,760
code it up so you can see how to do this
9115
05:26:35,760 --> 05:26:38,400
from scratch okay so as a matter of fact
9116
05:26:38,400 --> 05:26:39,840
why don't we actually go ahead and set
9117
05:26:39,840 --> 05:26:42,160
up the new repel now and then we'll walk
9118
05:26:42,160 --> 05:26:43,760
through the code together okay so let's
9119
05:26:43,760 --> 05:26:46,558
go ahead and do that so hit open up your
9120
05:26:46,558 --> 05:26:48,080
open up a new tab
9121
05:26:48,080 --> 05:26:50,240
and then just go to replit.com just like
9122
05:26:50,240 --> 05:26:51,400
this
9123
05:26:51,400 --> 05:26:53,760
replit.com we're going to select the
9124
05:26:53,760 --> 05:26:57,040
html css javascript
9125
05:26:57,040 --> 05:26:59,040
okay click here
9126
05:26:59,040 --> 05:27:01,920
and then just choose uh great rapple but
9127
05:27:01,920 --> 05:27:05,200
we're gonna in this case say rock paper
9128
05:27:05,200 --> 05:27:08,080
scissors and hit create rubble perfect
9129
05:27:08,080 --> 05:27:10,798
now inside of here we just have index
9130
05:27:10,798 --> 05:27:12,558
script and style okay so we have these
9131
05:27:12,558 --> 05:27:14,400
three files now here's all we're going
9132
05:27:14,400 --> 05:27:16,080
to do we're going to go back we're going
9133
05:27:16,080 --> 05:27:16,958
to
9134
05:27:16,958 --> 05:27:18,558
copy so what i'm going to do is i'm
9135
05:27:18,558 --> 05:27:20,638
going to do command a or control a to
9136
05:27:20,638 --> 05:27:22,480
copy this entire everything that's
9137
05:27:22,480 --> 05:27:24,160
inside of this index.html i'm going to
9138
05:27:24,160 --> 05:27:27,280
copy it right click and copy
9139
05:27:27,280 --> 05:27:29,120
let's go back here
9140
05:27:29,120 --> 05:27:30,718
let's delete everything that's inside of
9141
05:27:30,718 --> 05:27:31,600
here
9142
05:27:31,600 --> 05:27:33,470
right click and i'm going to
9143
05:27:33,470 --> 05:27:34,638
[Music]
9144
05:27:34,638 --> 05:27:36,958
paste
9145
05:27:37,120 --> 05:27:39,280
boom just pasted that code okay so now
9146
05:27:39,280 --> 05:27:42,320
my html is pasted if i run it you're
9147
05:27:42,320 --> 05:27:44,240
gonna see it doesn't look very good but
9148
05:27:44,240 --> 05:27:45,760
it's there okay
9149
05:27:45,760 --> 05:27:47,520
then we're gonna go ahead and grab the
9150
05:27:47,520 --> 05:27:49,920
style sheet i'm gonna copy it
9151
05:27:49,920 --> 05:27:52,558
and we're gonna go back and we're going
9152
05:27:52,558 --> 05:27:54,000
to delete everything that's inside of
9153
05:27:54,000 --> 05:27:55,680
here i'm going to right click and paste
9154
05:27:55,680 --> 05:27:56,798
this
9155
05:27:56,798 --> 05:27:58,000
and i don't want you to worry about the
9156
05:27:58,000 --> 05:27:59,280
styling and everything i just want you
9157
05:27:59,280 --> 05:28:00,798
to worry about the javascript portion so
9158
05:28:00,798 --> 05:28:03,200
you can see that with the styling boom
9159
05:28:03,200 --> 05:28:04,878
everything looks perfect right
9160
05:28:04,878 --> 05:28:06,638
everything looks super clean now in our
9161
05:28:06,638 --> 05:28:08,958
script let's go ahead and copy our
9162
05:28:08,958 --> 05:28:10,638
script and don't worry this is not the
9163
05:28:10,638 --> 05:28:12,798
solution okay so just copy everything
9164
05:28:12,798 --> 05:28:15,040
that's inside of here so either control
9165
05:28:15,040 --> 05:28:17,600
a command a to select everything
9166
05:28:17,600 --> 05:28:20,080
and then you're going to hit copy then
9167
05:28:20,080 --> 05:28:22,798
come into this script and paste it okay
9168
05:28:22,798 --> 05:28:24,080
and hit run
9169
05:28:24,080 --> 05:28:25,920
and if you click nothing should be
9170
05:28:25,920 --> 05:28:28,958
working yet perfect okay this is
9171
05:28:28,958 --> 05:28:31,040
you starting from scratch and now let's
9172
05:28:31,040 --> 05:28:32,320
just walk through kind of what
9173
05:28:32,320 --> 05:28:33,958
everything is so let's start off with
9174
05:28:33,958 --> 05:28:36,400
index.html what's going on in here it's
9175
05:28:36,400 --> 05:28:38,638
a very simple file with only a few lines
9176
05:28:38,638 --> 05:28:40,958
of actual code all right some of the
9177
05:28:40,958 --> 05:28:42,400
code is
9178
05:28:42,400 --> 05:28:45,120
you know just doctype html html like
9179
05:28:45,120 --> 05:28:46,400
whatever right that's
9180
05:28:46,400 --> 05:28:47,520
default
9181
05:28:47,520 --> 05:28:49,680
html files come with that a couple of
9182
05:28:49,680 --> 05:28:51,600
lines of code here
9183
05:28:51,600 --> 05:28:54,160
and you're going to need to know the
9184
05:28:54,160 --> 05:28:56,240
player score certain div ids because
9185
05:28:56,240 --> 05:28:57,600
you're going to need to access them so
9186
05:28:57,600 --> 05:28:59,680
for example this button has a value
9187
05:28:59,680 --> 05:29:01,760
remember when we did red green and
9188
05:29:01,760 --> 05:29:03,840
yellow when we did that color app our
9189
05:29:03,840 --> 05:29:06,480
buttons had a value red it had a value
9190
05:29:06,480 --> 05:29:08,558
green it had a value whatever so then
9191
05:29:08,558 --> 05:29:09,680
when you clicked it you're able to
9192
05:29:09,680 --> 05:29:11,520
retrieve its value
9193
05:29:11,520 --> 05:29:12,878
that's why that exercise is going to be
9194
05:29:12,878 --> 05:29:15,600
val helpful to you here no pun intended
9195
05:29:15,600 --> 05:29:17,280
valuable for you
9196
05:29:17,280 --> 05:29:18,558
so
9197
05:29:18,558 --> 05:29:20,878
this is uh that's those buttons you're
9198
05:29:20,878 --> 05:29:22,520
going to need to know these classes so
9199
05:29:22,520 --> 05:29:24,400
familiarize yourself with this
9200
05:29:24,400 --> 05:29:26,958
index.html because from the javascript
9201
05:29:26,958 --> 05:29:29,040
side you're going to need to detect when
9202
05:29:29,040 --> 05:29:31,040
somebody clicks this button and you're
9203
05:29:31,040 --> 05:29:33,200
going to need to be able to refer to
9204
05:29:33,200 --> 05:29:35,120
these specific buttons you're going to
9205
05:29:35,120 --> 05:29:36,798
need to be able to refer
9206
05:29:36,798 --> 05:29:39,840
to you know just specific things and
9207
05:29:39,840 --> 05:29:41,840
then write on there
9208
05:29:41,840 --> 05:29:44,718
okay so for example when the player's
9209
05:29:44,718 --> 05:29:46,558
score shows up maybe that's the player's
9210
05:29:46,558 --> 05:29:48,080
score div when
9211
05:29:48,080 --> 05:29:49,840
the hands show up meaning the computer
9212
05:29:49,840 --> 05:29:51,760
hands or the user hands they should show
9213
05:29:51,760 --> 05:29:53,600
up here and by hand it's like computer
9214
05:29:53,600 --> 05:29:55,600
choice or player choice the result of
9215
05:29:55,600 --> 05:29:57,360
the game whether you won or lost that
9216
05:29:57,360 --> 05:29:59,760
should show up here as well okay um
9217
05:29:59,760 --> 05:30:01,840
obviously above that
9218
05:30:01,840 --> 05:30:03,680
red button right of course it should
9219
05:30:03,680 --> 05:30:05,360
show like above there so it should show
9220
05:30:05,360 --> 05:30:06,400
like here
9221
05:30:06,400 --> 05:30:07,680
end game button you're going to need
9222
05:30:07,680 --> 05:30:10,160
this id as well and this script here
9223
05:30:10,160 --> 05:30:11,520
just means that
9224
05:30:11,520 --> 05:30:13,920
this index.html
9225
05:30:13,920 --> 05:30:14,958
here
9226
05:30:14,958 --> 05:30:17,840
is linked with this uh
9227
05:30:17,840 --> 05:30:20,638
javascript sheet okay meaning if you
9228
05:30:20,638 --> 05:30:22,558
ever want if you ever change the name
9229
05:30:22,558 --> 05:30:24,798
here make sure you go and also change
9230
05:30:24,798 --> 05:30:26,240
the name here and make sure they both
9231
05:30:26,240 --> 05:30:27,280
match
9232
05:30:27,280 --> 05:30:29,360
okay cool but i would recommend you
9233
05:30:29,360 --> 05:30:31,360
don't change anything because once
9234
05:30:31,360 --> 05:30:33,680
you're done with your entire code you
9235
05:30:33,680 --> 05:30:36,080
should copy your working code and paste
9236
05:30:36,080 --> 05:30:39,200
it back inside of the script okay your
9237
05:30:39,200 --> 05:30:41,520
and the only file you need to change is
9238
05:30:41,520 --> 05:30:43,840
a script file okay awesome
9239
05:30:43,840 --> 05:30:46,718
now let's walk through this project step
9240
05:30:46,718 --> 05:30:48,480
by step we're not going to solve it
9241
05:30:48,480 --> 05:30:50,160
right now but i'm going to give it to
9242
05:30:50,160 --> 05:30:51,840
you as an exercise but we need to just
9243
05:30:51,840 --> 05:30:55,120
kind of go over some stuff okay
9244
05:30:55,120 --> 05:30:55,920
so
9245
05:30:55,920 --> 05:30:57,600
this project will have a couple of
9246
05:30:57,600 --> 05:30:59,520
functions that you need to implement all
9247
05:30:59,520 --> 05:31:01,760
right here we go
9248
05:31:01,760 --> 05:31:04,878
so if you look at this there's only one
9249
05:31:04,878 --> 05:31:05,840
two
9250
05:31:05,840 --> 05:31:06,878
three
9251
05:31:06,878 --> 05:31:08,638
four and five
9252
05:31:08,638 --> 05:31:10,480
there are only five functions that you
9253
05:31:10,480 --> 05:31:12,400
need to implement the code shouldn't be
9254
05:31:12,400 --> 05:31:14,080
like longer than 100 lines it should be
9255
05:31:14,080 --> 05:31:16,400
less than that probably around 70ish
9256
05:31:16,400 --> 05:31:18,400
lines without comments
9257
05:31:18,400 --> 05:31:19,280
and
9258
05:31:19,280 --> 05:31:20,638
that's what you need to work on okay
9259
05:31:20,638 --> 05:31:24,080
it's five functions one two three four
9260
05:31:24,080 --> 05:31:26,160
five six actually sorry six functions
9261
05:31:26,160 --> 05:31:28,718
you need to work on six functions now
9262
05:31:28,718 --> 05:31:30,718
with each function there's going to be
9263
05:31:30,718 --> 05:31:33,200
some documentation telling you what this
9264
05:31:33,200 --> 05:31:35,440
function should do how to run this
9265
05:31:35,440 --> 05:31:38,160
function and what it should return okay
9266
05:31:38,160 --> 05:31:39,520
and then there's going to be some other
9267
05:31:39,520 --> 05:31:41,600
notes sometimes if and you know within
9268
05:31:41,600 --> 05:31:43,200
the function as well so for example
9269
05:31:43,200 --> 05:31:45,520
let's walk through computer choice
9270
05:31:45,520 --> 05:31:48,000
well this function computer choice get
9271
05:31:48,000 --> 05:31:50,240
computer choice randomly selects between
9272
05:31:50,240 --> 05:31:52,080
rock paper scissors and returns that
9273
05:31:52,080 --> 05:31:53,840
string so that should be pretty
9274
05:31:53,840 --> 05:31:55,600
self-explanatory what that function
9275
05:31:55,600 --> 05:31:57,680
should do every time i call it here's an
9276
05:31:57,680 --> 05:31:59,600
example of me calling it get computer
9277
05:31:59,600 --> 05:32:01,040
choice and here's an example of it
9278
05:32:01,040 --> 05:32:03,600
returning a random string rock here's
9279
05:32:03,600 --> 05:32:05,440
another example of me calling it and it
9280
05:32:05,440 --> 05:32:08,000
returning a random string scissors
9281
05:32:08,000 --> 05:32:10,240
this function has no
9282
05:32:10,240 --> 05:32:13,840
arguments okay zero arguments nada so
9283
05:32:13,840 --> 05:32:15,920
you won't need to add in or take away
9284
05:32:15,920 --> 05:32:18,320
arguments the arguments are already
9285
05:32:18,320 --> 05:32:19,200
there
9286
05:32:19,200 --> 05:32:21,040
for you to help you out here's another
9287
05:32:21,040 --> 05:32:21,840
function you're going to need to
9288
05:32:21,840 --> 05:32:24,558
implement the get result function
9289
05:32:24,558 --> 05:32:27,200
okay this is going to compare player
9290
05:32:27,200 --> 05:32:29,040
choice and computer choice and it will
9291
05:32:29,040 --> 05:32:32,000
return the score accordingly meaning
9292
05:32:32,000 --> 05:32:33,920
here's an example where the human wins
9293
05:32:33,920 --> 05:32:36,320
aka you win against the computer when
9294
05:32:36,320 --> 05:32:38,400
you call get result and you pass it rock
9295
05:32:38,400 --> 05:32:40,718
and scissors so this is the player
9296
05:32:40,718 --> 05:32:42,558
choice and this is a computer choice so
9297
05:32:42,558 --> 05:32:45,280
when you give it rock and scissors
9298
05:32:45,280 --> 05:32:48,160
who wins is it the human or
9299
05:32:48,160 --> 05:32:50,480
the bot that wins this case the human
9300
05:32:50,480 --> 05:32:52,400
wins so then it should return a score of
9301
05:32:52,400 --> 05:32:54,480
one here's an example of a human losing
9302
05:32:54,480 --> 05:32:56,080
you call the get result function you
9303
05:32:56,080 --> 05:32:58,878
pass a scissors four player and you pass
9304
05:32:58,878 --> 05:33:00,878
it rock for the computer
9305
05:33:00,878 --> 05:33:03,040
and it returns negative one because rock
9306
05:33:03,040 --> 05:33:04,638
beats scissors and then here's another
9307
05:33:04,638 --> 05:33:07,200
example of human draws
9308
05:33:07,200 --> 05:33:08,558
where
9309
05:33:08,558 --> 05:33:11,040
you pick rock for the human and rock for
9310
05:33:11,040 --> 05:33:13,120
the computer and the score should be
9311
05:33:13,120 --> 05:33:15,840
zero okay so those are examples of how
9312
05:33:15,840 --> 05:33:17,440
to call this function what it should
9313
05:33:17,440 --> 05:33:19,840
return should return a number
9314
05:33:19,840 --> 05:33:22,160
and the two arguments that it has okay
9315
05:33:22,160 --> 05:33:23,680
this function is probably going to be
9316
05:33:23,680 --> 05:33:25,040
one of the most difficult ones for you
9317
05:33:25,040 --> 05:33:29,040
to implement and uh here i i have even
9318
05:33:29,040 --> 05:33:31,040
other notes for you to make it easier so
9319
05:33:31,040 --> 05:33:33,120
for example when you're implementing
9320
05:33:33,120 --> 05:33:34,480
this you're gonna need to have you're
9321
05:33:34,480 --> 05:33:36,878
gonna need to do a lot of if else if
9322
05:33:36,878 --> 05:33:38,798
else conditions okay because you're
9323
05:33:38,798 --> 05:33:41,120
gonna need to check if the
9324
05:33:41,120 --> 05:33:43,920
player picks rock and the computer picks
9325
05:33:43,920 --> 05:33:46,558
rock then what happens okay
9326
05:33:46,558 --> 05:33:48,240
so you're gonna need to write the code
9327
05:33:48,240 --> 05:33:50,958
here it says all situations where human
9328
05:33:50,958 --> 05:33:53,680
draws set score to zero so in order to
9329
05:33:53,680 --> 05:33:55,440
write this well this should be pretty
9330
05:33:55,440 --> 05:33:56,400
easy because
9331
05:33:56,400 --> 05:33:59,280
when's the only situation it's a draw so
9332
05:33:59,280 --> 05:34:01,680
you could say if a rock goes against a
9333
05:34:01,680 --> 05:34:03,680
rock that's a draw if a scissor goes
9334
05:34:03,680 --> 05:34:05,920
against a scissor that's a draw if a
9335
05:34:05,920 --> 05:34:08,160
that will take you forever or you could
9336
05:34:08,160 --> 05:34:09,680
just say hey if player choice and
9337
05:34:09,680 --> 05:34:12,240
computer joints match that's a draw so
9338
05:34:12,240 --> 05:34:15,120
you could do it like that all right
9339
05:34:15,120 --> 05:34:16,798
so that should take care of all of your
9340
05:34:16,798 --> 05:34:19,280
draw conditions then here are your other
9341
05:34:19,280 --> 05:34:20,798
conditions so
9342
05:34:20,798 --> 05:34:22,958
this is where all situations where human
9343
05:34:22,958 --> 05:34:25,360
wins you should set the score to one so
9344
05:34:25,360 --> 05:34:27,280
you're going to use a bunch of else if's
9345
05:34:27,280 --> 05:34:29,760
here and this is a situation where you
9346
05:34:29,760 --> 05:34:31,680
will define all situations where a human
9347
05:34:31,680 --> 05:34:34,320
wins so a human will win if
9348
05:34:34,320 --> 05:34:35,520
human pigs
9349
05:34:35,520 --> 05:34:36,638
you know
9350
05:34:36,638 --> 05:34:38,878
rock against scissors a human will win
9351
05:34:38,878 --> 05:34:41,360
if the human picks scissors against
9352
05:34:41,360 --> 05:34:44,400
paper a human will win if a human picks
9353
05:34:44,400 --> 05:34:46,240
paper against rock
9354
05:34:46,240 --> 05:34:48,320
so those are all the winning conditions
9355
05:34:48,320 --> 05:34:50,400
go here and then all the losing
9356
05:34:50,400 --> 05:34:52,160
conditions you can be pretty clever
9357
05:34:52,160 --> 05:34:53,360
about it and you don't even actually
9358
05:34:53,360 --> 05:34:54,798
have to define them because you could
9359
05:34:54,798 --> 05:34:57,920
just say else human loses okay so these
9360
05:34:57,920 --> 05:34:59,600
comments are there to help you kind of
9361
05:34:59,600 --> 05:35:01,440
simplify the logic for you a little bit
9362
05:35:01,440 --> 05:35:03,200
and then just return the score make sure
9363
05:35:03,200 --> 05:35:05,120
you don't forget to return the score i'm
9364
05:35:05,120 --> 05:35:06,718
going to toggle this function and close
9365
05:35:06,718 --> 05:35:08,878
it then you have the show result
9366
05:35:08,878 --> 05:35:10,958
function okay this function is also
9367
05:35:10,958 --> 05:35:12,558
going to be a little bit advanced and
9368
05:35:12,558 --> 05:35:14,080
this one
9369
05:35:14,080 --> 05:35:16,558
this function here get result is heavy
9370
05:35:16,558 --> 05:35:19,040
on if else conditions show result is
9371
05:35:19,040 --> 05:35:20,878
going to be heavy on dom manipulation
9372
05:35:20,878 --> 05:35:22,558
okay so show result is how you're going
9373
05:35:22,558 --> 05:35:24,718
to draw on screen and show stuff for
9374
05:35:24,718 --> 05:35:27,840
example if i go to the solution version
9375
05:35:27,840 --> 05:35:29,520
when i click here
9376
05:35:29,520 --> 05:35:32,000
show result is a function that's doing
9377
05:35:32,000 --> 05:35:34,400
you know human chose rock computer chose
9378
05:35:34,400 --> 05:35:35,360
paper
9379
05:35:35,360 --> 05:35:36,558
you lose
9380
05:35:36,558 --> 05:35:38,080
and then giving a score of negative one
9381
05:35:38,080 --> 05:35:40,718
and then drawing drawing it out here
9382
05:35:40,718 --> 05:35:42,638
okay so that's the show result function
9383
05:35:42,638 --> 05:35:45,360
so get result will calculate who won
9384
05:35:45,360 --> 05:35:48,080
show result will then output that the
9385
05:35:48,080 --> 05:35:50,080
human computer choice the human choice
9386
05:35:50,080 --> 05:35:52,400
the current score all of that stuff onto
9387
05:35:52,400 --> 05:35:54,400
the screen so show result updates the
9388
05:35:54,400 --> 05:35:57,040
dom to you win or you lose or it's a
9389
05:35:57,040 --> 05:35:58,718
draw based on the score also shows
9390
05:35:58,718 --> 05:36:01,280
player choice versus computer choice
9391
05:36:01,280 --> 05:36:02,798
okay
9392
05:36:02,798 --> 05:36:04,000
now
9393
05:36:04,000 --> 05:36:06,080
here
9394
05:36:06,080 --> 05:36:07,200
um
9395
05:36:07,200 --> 05:36:08,958
you can see it takes in three arguments
9396
05:36:08,958 --> 05:36:11,200
okay the score player choice computer
9397
05:36:11,200 --> 05:36:13,120
choice so it needs that in three pieces
9398
05:36:13,120 --> 05:36:15,680
of information and uh hint on a negative
9399
05:36:15,680 --> 05:36:18,160
on a score of negative one you should do
9400
05:36:18,160 --> 05:36:21,840
result.inner text you lose why because
9401
05:36:21,840 --> 05:36:24,798
oops if i go back here i have a div
9402
05:36:24,798 --> 05:36:27,360
called result right so make sure you get
9403
05:36:27,360 --> 05:36:30,280
this div by using element
9404
05:36:30,280 --> 05:36:32,638
document.getelementbyid get this div
9405
05:36:32,638 --> 05:36:35,280
right store in a variable called result
9406
05:36:35,280 --> 05:36:37,200
and then you could do result.intertext
9407
05:36:37,200 --> 05:36:40,320
and change its value to you lose so
9408
05:36:40,320 --> 05:36:41,680
that's what i said don't forget to grab
9409
05:36:41,680 --> 05:36:43,200
the div with the result id so this is
9410
05:36:43,200 --> 05:36:45,680
just a hint on how to like do one of
9411
05:36:45,680 --> 05:36:47,440
them you're going to need to do multiple
9412
05:36:47,440 --> 05:36:50,958
divs here then you have on click rock
9413
05:36:50,958 --> 05:36:53,360
paper scissors so this is where
9414
05:36:53,360 --> 05:36:56,240
what should happen when the user clicks
9415
05:36:56,240 --> 05:36:58,080
well right when the user clicks you need
9416
05:36:58,080 --> 05:36:59,680
to think about what happens so this
9417
05:36:59,680 --> 05:37:01,520
function should calculate who won and
9418
05:37:01,520 --> 05:37:03,840
show it on the screen so to give you a
9419
05:37:03,840 --> 05:37:06,878
hint this function should call upon our
9420
05:37:06,878 --> 05:37:09,520
get result function and our show result
9421
05:37:09,520 --> 05:37:10,480
function
9422
05:37:10,480 --> 05:37:12,878
that's kind of it okay that's what this
9423
05:37:12,878 --> 05:37:14,320
function will do
9424
05:37:14,320 --> 05:37:16,718
all right and then we have our play game
9425
05:37:16,718 --> 05:37:17,840
function this is going to be a really
9426
05:37:17,840 --> 05:37:19,200
important function
9427
05:37:19,200 --> 05:37:21,040
the name is a little misleading and i'll
9428
05:37:21,040 --> 05:37:22,958
kind of explain why
9429
05:37:22,958 --> 05:37:24,798
but here's what it should do
9430
05:37:24,798 --> 05:37:26,958
make the rps rps means rock paper
9431
05:37:26,958 --> 05:37:29,280
scissors so anywhere you see rps it
9432
05:37:29,280 --> 05:37:31,680
means rocket processors so make the rps
9433
05:37:31,680 --> 05:37:33,680
buttons actively listen for a click and
9434
05:37:33,680 --> 05:37:36,240
do something once a click is detected so
9435
05:37:36,240 --> 05:37:39,280
that's what the function of play game is
9436
05:37:39,280 --> 05:37:41,280
so all it does is just listening here
9437
05:37:41,280 --> 05:37:44,320
okay it attaches listeners to these
9438
05:37:44,320 --> 05:37:47,520
and once you click here it should run
9439
05:37:47,520 --> 05:37:49,840
this function right there when i click
9440
05:37:49,840 --> 05:37:51,760
here it should run that function right
9441
05:37:51,760 --> 05:37:53,520
there and of course we pass in the
9442
05:37:53,520 --> 05:37:55,200
player choice so
9443
05:37:55,200 --> 05:37:58,000
if i let's say choose rock
9444
05:37:58,000 --> 05:37:58,958
yeah
9445
05:37:58,958 --> 05:38:02,240
and um i click that then it calls on
9446
05:38:02,240 --> 05:38:05,360
click rps and then it passes in rock as
9447
05:38:05,360 --> 05:38:07,040
the player choice
9448
05:38:07,040 --> 05:38:09,600
it and then vice versa if i choose any
9449
05:38:09,600 --> 05:38:11,440
of these other ones and then on click
9450
05:38:11,440 --> 05:38:14,080
rps knows what to do so i have all the
9451
05:38:14,080 --> 05:38:16,240
notes here for you right so you make
9452
05:38:16,240 --> 05:38:18,080
sure to use query selectors select all
9453
05:38:18,080 --> 05:38:19,680
rps buttons
9454
05:38:19,680 --> 05:38:21,920
right and then i have even step by step
9455
05:38:21,920 --> 05:38:23,760
on how you can do it uh loop through the
9456
05:38:23,760 --> 05:38:25,520
buttons using a for each loop add a
9457
05:38:25,520 --> 05:38:28,160
click event listener to each button
9458
05:38:28,160 --> 05:38:30,080
and uh this is
9459
05:38:30,080 --> 05:38:33,120
you know um we can say add an on click
9460
05:38:33,120 --> 05:38:34,400
and this is something that we did
9461
05:38:34,400 --> 05:38:37,280
already with our color app okay remember
9462
05:38:37,280 --> 05:38:39,440
we had to add event listeners on click
9463
05:38:39,440 --> 05:38:42,080
to each update each button's on click
9464
05:38:42,080 --> 05:38:44,480
properties to basically listen for a
9465
05:38:44,480 --> 05:38:47,120
click and then call another function so
9466
05:38:47,120 --> 05:38:48,558
refer to that
9467
05:38:48,558 --> 05:38:50,958
red green yellow square app that we did
9468
05:38:50,958 --> 05:38:52,878
okay cool so that's what play game is
9469
05:38:52,878 --> 05:38:56,160
gonna be and then um you know and then
9470
05:38:56,160 --> 05:38:58,480
make sure that in there in play game you
9471
05:38:58,480 --> 05:39:01,040
also add a listener to this button too
9472
05:39:01,040 --> 05:39:02,638
so basically in play game all you're
9473
05:39:02,638 --> 05:39:04,480
really doing is you're just updating the
9474
05:39:04,480 --> 05:39:06,638
on click properties of each of these
9475
05:39:06,638 --> 05:39:08,718
okay that's kind of kind of what happens
9476
05:39:08,718 --> 05:39:11,200
there and last
9477
05:39:11,200 --> 05:39:13,920
end game end game is just gonna be a
9478
05:39:13,920 --> 05:39:16,080
function that clears all the text on the
9479
05:39:16,080 --> 05:39:18,718
dom so if i go here and i hit this
9480
05:39:18,718 --> 05:39:20,558
it just clears all the text so it takes
9481
05:39:20,558 --> 05:39:22,160
all the values that you had initially
9482
05:39:22,160 --> 05:39:23,040
set
9483
05:39:23,040 --> 05:39:25,280
sets them to empty string or empties
9484
05:39:25,280 --> 05:39:29,040
them out okay so i hope that you know i
9485
05:39:29,040 --> 05:39:31,280
really believe that you'll be able to do
9486
05:39:31,280 --> 05:39:32,958
this we've done
9487
05:39:32,958 --> 05:39:34,958
every single every exercise everything
9488
05:39:34,958 --> 05:39:36,400
that we've done up until this point
9489
05:39:36,400 --> 05:39:38,638
preps you for this project there
9490
05:39:38,638 --> 05:39:40,080
shouldn't be any
9491
05:39:40,080 --> 05:39:42,320
concepts that are new while putting them
9492
05:39:42,320 --> 05:39:44,240
together is gonna be harder and then
9493
05:39:44,240 --> 05:39:45,680
there's gonna be certain things that are
9494
05:39:45,680 --> 05:39:48,558
for sure going to be challenging so do
9495
05:39:48,558 --> 05:39:51,120
don't be upset or frustrated if you
9496
05:39:51,120 --> 05:39:53,040
don't get this honestly i don't expect
9497
05:39:53,040 --> 05:39:54,558
you to be able to get it you'd have to
9498
05:39:54,558 --> 05:39:56,558
be really really freaking hyper smart to
9499
05:39:56,558 --> 05:39:58,798
get this on your first try when i used
9500
05:39:58,798 --> 05:40:01,440
to do these projects i never got this
9501
05:40:01,440 --> 05:40:03,360
these types of projects on my own like
9502
05:40:03,360 --> 05:40:05,680
right away in the start i would give it
9503
05:40:05,680 --> 05:40:07,680
my best i would come close but it
9504
05:40:07,680 --> 05:40:08,878
wouldn't be working and then i would
9505
05:40:08,878 --> 05:40:10,480
actually look at the real code and then
9506
05:40:10,480 --> 05:40:12,480
i would go ah okay then i would try it
9507
05:40:12,480 --> 05:40:15,440
again but i would give it an honest try
9508
05:40:15,440 --> 05:40:17,120
so make sure that you do give it an
9509
05:40:17,120 --> 05:40:19,440
honest try and try your hardest before
9510
05:40:19,440 --> 05:40:21,440
you come and look at the solutions and
9511
05:40:21,440 --> 05:40:23,040
the concepts you know they'll be covered
9512
05:40:23,040 --> 05:40:25,440
are like for loops dom manipulation
9513
05:40:25,440 --> 05:40:27,440
variables conditionals template literals
9514
05:40:27,440 --> 05:40:29,440
event listeners higher order functions
9515
05:40:29,440 --> 05:40:32,718
and math.random stuff okay so
9516
05:40:32,718 --> 05:40:33,600
um
9517
05:40:33,600 --> 05:40:34,638
i think you're going to have a lot of
9518
05:40:34,638 --> 05:40:37,440
fun with this and really give it a try
9519
05:40:37,440 --> 05:40:39,120
and then pretty soon we're going to walk
9520
05:40:39,120 --> 05:40:41,440
through and build this together okay so
9521
05:40:41,440 --> 05:40:44,160
the next uh step will be us actually
9522
05:40:44,160 --> 05:40:45,440
walking through the entire solution
9523
05:40:45,440 --> 05:40:47,200
together but for now you should stop
9524
05:40:47,200 --> 05:40:48,558
take a break and try to build this
9525
05:40:48,558 --> 05:40:50,878
together by yourself
9526
05:40:50,878 --> 05:40:53,120
all right see you soon all right let's
9527
05:40:53,120 --> 05:40:56,240
go through and code up rock paper
9528
05:40:56,240 --> 05:40:59,120
scissors together let's go
9529
05:40:59,120 --> 05:40:59,840
so
9530
05:40:59,840 --> 05:41:02,240
let's go and do this step by step all
9531
05:41:02,240 --> 05:41:03,920
right and we're gonna walk through and
9532
05:41:03,920 --> 05:41:06,400
do every single thing together so this
9533
05:41:06,400 --> 05:41:08,638
get computer choice function over here
9534
05:41:08,638 --> 05:41:10,480
remember we made this function that
9535
05:41:10,480 --> 05:41:12,718
allowed us to get a random item from an
9536
05:41:12,718 --> 05:41:15,440
array right fruit item we're gonna use
9537
05:41:15,440 --> 05:41:18,480
very similar logic to that so uh let's
9538
05:41:18,480 --> 05:41:19,920
go and say
9539
05:41:19,920 --> 05:41:21,840
and i'm coding this live together with
9540
05:41:21,840 --> 05:41:23,280
you so if i get stuck in any of these
9541
05:41:23,280 --> 05:41:24,718
parts it's live
9542
05:41:24,718 --> 05:41:26,638
yes i've coded it up before but now
9543
05:41:26,638 --> 05:41:28,320
we're doing it from scratch so we're
9544
05:41:28,320 --> 05:41:31,280
gonna get so here are the rps choices
9545
05:41:31,280 --> 05:41:34,240
okay so we have rock we have paper
9546
05:41:34,240 --> 05:41:36,878
and we have scissors
9547
05:41:36,878 --> 05:41:39,280
these are the three choices that that
9548
05:41:39,280 --> 05:41:42,400
are universally available to us right
9549
05:41:42,400 --> 05:41:45,040
and now i'm going to just say
9550
05:41:45,040 --> 05:41:48,320
and let's capitalize this r
9551
05:41:48,320 --> 05:41:49,920
p
9552
05:41:49,920 --> 05:41:50,638
s
9553
05:41:50,638 --> 05:41:53,680
okay and now we're gonna say we're gonna
9554
05:41:53,680 --> 05:41:57,520
randomly select one so random
9555
05:41:57,680 --> 05:41:58,798
choice
9556
05:41:58,798 --> 05:42:02,520
okay and then here i'm going to do
9557
05:42:02,520 --> 05:42:05,520
math.random right and
9558
05:42:05,520 --> 05:42:07,520
we're gonna multiply that by three just
9559
05:42:07,520 --> 05:42:08,878
hard code three in there because we only
9560
05:42:08,878 --> 05:42:10,558
have three choices so this will make
9561
05:42:10,558 --> 05:42:12,080
sure that it gets the number between
9562
05:42:12,080 --> 05:42:13,360
zero one
9563
05:42:13,360 --> 05:42:14,638
zero and three
9564
05:42:14,638 --> 05:42:15,680
okay
9565
05:42:15,680 --> 05:42:17,840
and then we're gonna call mata floor on
9566
05:42:17,840 --> 05:42:20,480
this so remember because math.random is
9567
05:42:20,480 --> 05:42:23,280
always going to give you some
9568
05:42:23,280 --> 05:42:24,240
like
9569
05:42:24,240 --> 05:42:26,400
crazy numbers zero point eight four four
9570
05:42:26,400 --> 05:42:28,160
four four four something right but when
9571
05:42:28,160 --> 05:42:30,240
you floor it it'll just
9572
05:42:30,240 --> 05:42:32,080
remove all the decimal places and then
9573
05:42:32,080 --> 05:42:34,000
round it down so now this should give us
9574
05:42:34,000 --> 05:42:36,000
zero one and two perfect that's exactly
9575
05:42:36,000 --> 05:42:37,920
what we're looking for and then all we
9576
05:42:37,920 --> 05:42:39,520
wanna do
9577
05:42:39,520 --> 05:42:41,120
um is
9578
05:42:41,120 --> 05:42:43,360
let's let's actually call this random
9579
05:42:43,360 --> 05:42:47,200
number and then we're going to do uh rps
9580
05:42:47,200 --> 05:42:48,638
choice
9581
05:42:48,638 --> 05:42:50,320
pass in the random number and then
9582
05:42:50,320 --> 05:42:52,080
that's it we should just return that
9583
05:42:52,080 --> 05:42:54,080
okay so return
9584
05:42:54,080 --> 05:42:56,638
random number so if this function let's
9585
05:42:56,638 --> 05:42:58,160
see if this function is good to go and
9586
05:42:58,160 --> 05:43:00,000
if it's working okay so i'm gonna go
9587
05:43:00,000 --> 05:43:02,160
ahead and call this here we're going to
9588
05:43:02,160 --> 05:43:04,480
say console.log actually here's even a
9589
05:43:04,480 --> 05:43:06,400
cooler way of testing this
9590
05:43:06,400 --> 05:43:08,400
so let's just copy this function right
9591
05:43:08,400 --> 05:43:10,718
over here copy paste it in
9592
05:43:10,718 --> 05:43:12,240
hit enter
9593
05:43:12,240 --> 05:43:14,000
alright so basically what all we've done
9594
05:43:14,000 --> 05:43:16,000
is we took this function and pasted the
9595
05:43:16,000 --> 05:43:18,558
definition inside of our console and now
9596
05:43:18,558 --> 05:43:21,360
let's just call computer choice
9597
05:43:21,360 --> 05:43:23,280
let's call it again let's call it again
9598
05:43:23,280 --> 05:43:25,440
let's call it again and you can see that
9599
05:43:25,440 --> 05:43:28,160
every time it gives us a random choice
9600
05:43:28,160 --> 05:43:31,280
rock paper scissors everything is random
9601
05:43:31,280 --> 05:43:32,080
okay
9602
05:43:32,080 --> 05:43:35,040
so this function is working correctly
9603
05:43:35,040 --> 05:43:36,638
okay so i think that's great now let's
9604
05:43:36,638 --> 05:43:38,878
move on to the next function
9605
05:43:38,878 --> 05:43:40,000
this is going to be a little bit of a
9606
05:43:40,000 --> 05:43:41,360
harder function
9607
05:43:41,360 --> 05:43:43,360
right this is the get result and it's
9608
05:43:43,360 --> 05:43:44,958
going to compute the score so based on
9609
05:43:44,958 --> 05:43:47,360
the score that we give it it's going to
9610
05:43:47,360 --> 05:43:49,920
say whether you won or you lost so now
9611
05:43:49,920 --> 05:43:51,920
what are all the situations where a
9612
05:43:51,920 --> 05:43:55,120
human draws this one over here is simple
9613
05:43:55,120 --> 05:43:58,000
we're going to say if player choice is
9614
05:43:58,000 --> 05:44:00,878
you know equal to computer choice
9615
05:44:00,878 --> 05:44:02,400
well in that case we're just going to
9616
05:44:02,400 --> 05:44:05,920
return a score of actually
9617
05:44:05,920 --> 05:44:08,080
let's have a variable here here called
9618
05:44:08,080 --> 05:44:09,280
score
9619
05:44:09,280 --> 05:44:11,280
and we're going to basically just say
9620
05:44:11,280 --> 05:44:12,958
score equals
9621
05:44:12,958 --> 05:44:14,958
zero these are all of our draws
9622
05:44:14,958 --> 05:44:16,798
situation so now all of our drawing
9623
05:44:16,798 --> 05:44:18,558
situations are handled
9624
05:44:18,558 --> 05:44:20,798
now we have our else ifs
9625
05:44:20,798 --> 05:44:22,798
that we're gonna start okay so else if
9626
05:44:22,798 --> 05:44:26,000
actually let's do it this way
9627
05:44:26,000 --> 05:44:28,320
uh we're gonna say elsif
9628
05:44:28,320 --> 05:44:29,520
so now we're gonna have a bunch of
9629
05:44:29,520 --> 05:44:31,760
elsifs
9630
05:44:31,760 --> 05:44:33,760
so what are the situations where a human
9631
05:44:33,760 --> 05:44:35,120
will win so
9632
05:44:35,120 --> 05:44:36,958
let's pick a situation where a human
9633
05:44:36,958 --> 05:44:39,680
will win if a human chooses rock
9634
05:44:39,680 --> 05:44:41,040
then let's pick a situation where a
9635
05:44:41,040 --> 05:44:43,760
human will win where they pick paper and
9636
05:44:43,760 --> 05:44:45,280
then let's pick a situation where a
9637
05:44:45,280 --> 05:44:47,840
human will win if a human picks scissors
9638
05:44:47,840 --> 05:44:49,200
okay so those are the three different
9639
05:44:49,200 --> 05:44:51,520
situations so let's give it a try
9640
05:44:51,520 --> 05:44:53,280
so we're going to say
9641
05:44:53,280 --> 05:44:55,920
if player choice
9642
05:44:55,920 --> 05:44:56,718
is
9643
05:44:56,718 --> 05:44:58,558
rock
9644
05:44:58,558 --> 05:45:00,000
and
9645
05:45:00,000 --> 05:45:02,558
and uh make sure to always have
9646
05:45:02,558 --> 05:45:04,558
everything in title case like this
9647
05:45:04,558 --> 05:45:06,638
because if you use lower r somewhere for
9648
05:45:06,638 --> 05:45:08,798
rock and capital r somewhere else your
9649
05:45:08,798 --> 05:45:11,600
entire app is going to break okay so
9650
05:45:11,600 --> 05:45:14,718
just be consistent with uh this
9651
05:45:14,718 --> 05:45:16,878
spelling here everywhere all right let's
9652
05:45:16,878 --> 05:45:19,200
go here and let's give it a try so
9653
05:45:19,200 --> 05:45:20,480
computer
9654
05:45:20,480 --> 05:45:25,600
choice so rock will beat scissors
9655
05:45:25,600 --> 05:45:28,000
so this is situation one where a human
9656
05:45:28,000 --> 05:45:30,080
wins now let's try another let's see
9657
05:45:30,080 --> 05:45:31,840
another situation here
9658
05:45:31,840 --> 05:45:34,798
okay so i'm gonna copy this
9659
05:45:34,798 --> 05:45:36,798
and paste this here right
9660
05:45:36,798 --> 05:45:38,240
so these are now we're making all the
9661
05:45:38,240 --> 05:45:40,480
situations where a human wins
9662
05:45:40,480 --> 05:45:42,798
and we're going to set the score to one
9663
05:45:42,798 --> 05:45:44,160
in this case
9664
05:45:44,160 --> 05:45:46,160
now we're going to have another elsif so
9665
05:45:46,160 --> 05:45:47,760
i'm just going to copy this and paste
9666
05:45:47,760 --> 05:45:49,360
that
9667
05:45:49,360 --> 05:45:52,160
okay and uh let's also let's actually
9668
05:45:52,160 --> 05:45:54,000
copy i'm gonna copy
9669
05:45:54,000 --> 05:45:56,160
this entire thing and paste it again
9670
05:45:56,160 --> 05:45:57,520
copy this entire thing and paste it
9671
05:45:57,520 --> 05:46:00,080
again so in the situation of rock and
9672
05:46:00,080 --> 05:46:01,600
then we need to handle situation of
9673
05:46:01,600 --> 05:46:02,878
paper and then we need to handle
9674
05:46:02,878 --> 05:46:05,680
situation of scissors so paper
9675
05:46:05,680 --> 05:46:08,000
if a human chooses paper well what would
9676
05:46:08,000 --> 05:46:10,080
the computer need to choose in order for
9677
05:46:10,080 --> 05:46:12,000
the human to win
9678
05:46:12,000 --> 05:46:14,320
let's think that think about that right
9679
05:46:14,320 --> 05:46:18,480
so if i chose paper well paper beats
9680
05:46:18,480 --> 05:46:20,558
rock
9681
05:46:20,558 --> 05:46:23,120
so then we're going to pick rock here
9682
05:46:23,120 --> 05:46:24,958
and then scissors beats paper so we're
9683
05:46:24,958 --> 05:46:27,040
going to put paper here
9684
05:46:27,040 --> 05:46:29,040
so now we've defined all of the
9685
05:46:29,040 --> 05:46:31,360
conditions where a human wins so we're
9686
05:46:31,360 --> 05:46:33,200
done with that
9687
05:46:33,200 --> 05:46:34,798
and then the last thing we actually just
9688
05:46:34,798 --> 05:46:35,840
need to do
9689
05:46:35,840 --> 05:46:38,000
is define a situation where a human
9690
05:46:38,000 --> 05:46:39,200
loses so
9691
05:46:39,200 --> 05:46:41,520
what we need to do here
9692
05:46:41,520 --> 05:46:44,240
is just say else
9693
05:46:44,240 --> 05:46:47,280
score is uh negative one
9694
05:46:47,280 --> 05:46:49,680
okay so now everything else is a
9695
05:46:49,680 --> 05:46:51,680
situation where it's reversed where the
9696
05:46:51,680 --> 05:46:53,120
computer wins
9697
05:46:53,120 --> 05:46:56,240
but we can we can be really smart and
9698
05:46:56,240 --> 05:46:58,878
cheeky and just say else here okay
9699
05:46:58,878 --> 05:47:00,558
otherwise you would have to do a bunch
9700
05:47:00,558 --> 05:47:02,080
of elsifs and then you would have to
9701
05:47:02,080 --> 05:47:04,558
basically reverse this
9702
05:47:04,558 --> 05:47:06,320
and put all the choices where computer
9703
05:47:06,320 --> 05:47:08,878
wins and manually define each one of
9704
05:47:08,878 --> 05:47:11,120
them but if you do it this way you don't
9705
05:47:11,120 --> 05:47:13,600
have to define each one of them right
9706
05:47:13,600 --> 05:47:16,080
and then at the end all we need to do
9707
05:47:16,080 --> 05:47:18,160
thanks to this note this note is pretty
9708
05:47:18,160 --> 05:47:19,840
much giving away the exact line of code
9709
05:47:19,840 --> 05:47:21,200
you need to write
9710
05:47:21,200 --> 05:47:24,558
make sure to return the score
9711
05:47:24,558 --> 05:47:25,600
okay
9712
05:47:25,600 --> 05:47:27,600
return the score now let's take this
9713
05:47:27,600 --> 05:47:29,680
function and see if we can copy this and
9714
05:47:29,680 --> 05:47:31,440
let's see if we can paste this function
9715
05:47:31,440 --> 05:47:32,798
in the
9716
05:47:32,798 --> 05:47:35,600
command line we pasted it and let's call
9717
05:47:35,600 --> 05:47:37,600
our function get result
9718
05:47:37,600 --> 05:47:40,558
and i'm going to give it a player choice
9719
05:47:40,558 --> 05:47:42,718
of rock and then the computer will be
9720
05:47:42,718 --> 05:47:44,798
scissors and let's see what happens
9721
05:47:44,798 --> 05:47:46,878
and we get one is that true does rock
9722
05:47:46,878 --> 05:47:48,958
beat scissors yes it does so we get back
9723
05:47:48,958 --> 05:47:52,958
one what happens if both are rock
9724
05:47:52,958 --> 05:47:55,360
zero so that's perfect
9725
05:47:55,360 --> 05:47:57,600
let's try let's try all these test cases
9726
05:47:57,600 --> 05:47:59,360
right to rock scissors
9727
05:47:59,360 --> 05:48:01,120
we tried rock rock now let's try
9728
05:48:01,120 --> 05:48:04,798
scissors rock and we should lose
9729
05:48:06,000 --> 05:48:07,520
scissors
9730
05:48:07,520 --> 05:48:08,878
comma
9731
05:48:08,878 --> 05:48:10,638
rock put make sure to put those quotes
9732
05:48:10,638 --> 05:48:11,920
and let's run it
9733
05:48:11,920 --> 05:48:15,040
and indeed we do lose so all of these
9734
05:48:15,040 --> 05:48:17,120
test cases that we have written here
9735
05:48:17,120 --> 05:48:19,520
right check out what we have written out
9736
05:48:19,520 --> 05:48:20,480
here
9737
05:48:20,480 --> 05:48:23,920
so this function is gucci this function
9738
05:48:23,920 --> 05:48:25,680
is doing what it's supposed to be doing
9739
05:48:25,680 --> 05:48:28,160
so we're good on the get result function
9740
05:48:28,160 --> 05:48:29,440
okay so i think
9741
05:48:29,440 --> 05:48:31,520
we're good with that so we finished get
9742
05:48:31,520 --> 05:48:33,040
computer choice
9743
05:48:33,040 --> 05:48:36,240
we finished get result okay now let's go
9744
05:48:36,240 --> 05:48:38,798
through some of our
9745
05:48:38,798 --> 05:48:41,520
other functions that we're going to be
9746
05:48:41,520 --> 05:48:44,958
doing as well so let's let's go to our
9747
05:48:44,958 --> 05:48:47,280
next function show result
9748
05:48:47,280 --> 05:48:49,920
well before we do this
9749
05:48:49,920 --> 05:48:52,878
before we just like do show result kind
9750
05:48:52,878 --> 05:48:54,400
of in one go
9751
05:48:54,400 --> 05:48:56,878
let's just attach our listeners first
9752
05:48:56,878 --> 05:48:58,080
because that's going to be the most
9753
05:48:58,080 --> 05:48:59,760
important thing because if if i was
9754
05:48:59,760 --> 05:49:01,040
making this app the first thing i would
9755
05:49:01,040 --> 05:49:02,638
do is add listeners because if i don't
9756
05:49:02,638 --> 05:49:04,080
add listeners
9757
05:49:04,080 --> 05:49:05,760
i'm never going to be able to see really
9758
05:49:05,760 --> 05:49:07,040
anything happen
9759
05:49:07,040 --> 05:49:08,798
so let's go ahead and add listeners so
9760
05:49:08,798 --> 05:49:10,320
loop through the buttons using a for
9761
05:49:10,320 --> 05:49:12,480
each loop okay so let's do that but
9762
05:49:12,480 --> 05:49:14,000
first what i need to do use query
9763
05:49:14,000 --> 05:49:16,798
selector to select all rps buttons
9764
05:49:16,798 --> 05:49:18,718
so let's do it
9765
05:49:18,718 --> 05:49:21,600
rps buttons i'm going to set document
9766
05:49:21,600 --> 05:49:23,040
dot
9767
05:49:23,040 --> 05:49:24,240
query
9768
05:49:24,240 --> 05:49:26,320
selector all
9769
05:49:26,320 --> 05:49:28,798
and we're going to pass it
9770
05:49:28,798 --> 05:49:33,200
rps buttons let's go to our html file
9771
05:49:33,200 --> 05:49:36,320
and if you see here we have rps button
9772
05:49:36,320 --> 05:49:38,878
class right class rps button class rps
9773
05:49:38,878 --> 05:49:41,840
button so it's not plural it's singular
9774
05:49:41,840 --> 05:49:44,240
and let's just see if we can actually
9775
05:49:44,240 --> 05:49:46,160
console log this out so let's go ahead
9776
05:49:46,160 --> 05:49:50,558
and console log rps button
9777
05:49:50,558 --> 05:49:51,440
okay
9778
05:49:51,440 --> 05:49:53,840
and uh here we we are calling the play
9779
05:49:53,840 --> 05:49:56,000
game function so if i hit run everything
9780
05:49:56,000 --> 05:49:58,000
should run
9781
05:49:58,000 --> 05:49:59,920
and there we go we're seeing that we're
9782
05:49:59,920 --> 05:50:02,480
getting three html button elements which
9783
05:50:02,480 --> 05:50:04,558
is exactly what we expected
9784
05:50:04,558 --> 05:50:05,760
right
9785
05:50:05,760 --> 05:50:08,400
and now you can either manually attach a
9786
05:50:08,400 --> 05:50:09,760
listener to each one of them so you
9787
05:50:09,760 --> 05:50:12,080
could do rps buttons
9788
05:50:12,080 --> 05:50:13,520
you know zero
9789
05:50:13,520 --> 05:50:17,760
dot on click and we could say
9790
05:50:17,760 --> 05:50:20,320
um you know just
9791
05:50:20,320 --> 05:50:23,200
console log the
9792
05:50:23,200 --> 05:50:26,958
uh the value we could do that
9793
05:50:27,360 --> 05:50:28,840
rps button
9794
05:50:28,840 --> 05:50:31,600
zero right and then you can just do dot
9795
05:50:31,600 --> 05:50:33,120
value so this is like this would be a
9796
05:50:33,120 --> 05:50:34,878
way of hard coding it but that's not
9797
05:50:34,878 --> 05:50:36,320
what we want to do so when we click rock
9798
05:50:36,320 --> 05:50:38,320
it does detect that we're clicking rock
9799
05:50:38,320 --> 05:50:40,000
when i click paper it doesn't work when
9800
05:50:40,000 --> 05:50:42,240
i click scissors nothing works right so
9801
05:50:42,240 --> 05:50:44,958
let's go ahead and now do what the
9802
05:50:44,958 --> 05:50:46,958
instructions say here so it says use a
9803
05:50:46,958 --> 05:50:48,160
loop to
9804
05:50:48,160 --> 05:50:50,000
go through them so i'm going to say rps
9805
05:50:50,000 --> 05:50:51,360
buttons dot
9806
05:50:51,360 --> 05:50:53,920
for each so that will allow me to loop
9807
05:50:53,920 --> 05:50:56,718
and for each button
9808
05:50:56,718 --> 05:50:59,840
okay rps button
9809
05:51:00,878 --> 05:51:02,878
i'm going to do something so for each
9810
05:51:02,878 --> 05:51:05,280
rps button we're going to add an event
9811
05:51:05,280 --> 05:51:06,958
listener so let's go ahead and do that
9812
05:51:06,958 --> 05:51:09,200
now
9813
05:51:09,360 --> 05:51:10,958
for each button
9814
05:51:10,958 --> 05:51:12,798
on click
9815
05:51:12,798 --> 05:51:14,878
we're basically going to say hey every
9816
05:51:14,878 --> 05:51:16,718
time i click you
9817
05:51:16,718 --> 05:51:19,760
i want you to call the on click rps
9818
05:51:19,760 --> 05:51:21,040
function
9819
05:51:21,040 --> 05:51:22,080
okay
9820
05:51:22,080 --> 05:51:23,840
and then step four is make sure to pass
9821
05:51:23,840 --> 05:51:25,760
the currently selected rps button as an
9822
05:51:25,760 --> 05:51:27,040
argument
9823
05:51:27,040 --> 05:51:28,080
okay
9824
05:51:28,080 --> 05:51:30,638
so or the currently selected
9825
05:51:30,638 --> 05:51:33,200
so basically if i click this
9826
05:51:33,200 --> 05:51:36,558
then i need to pass this to on click rps
9827
05:51:36,558 --> 05:51:39,200
okay so let's go ahead and do that
9828
05:51:39,200 --> 05:51:40,878
so i'm going to basically pass it the
9829
05:51:40,878 --> 05:51:42,558
value of that so we're going to say rps
9830
05:51:42,558 --> 05:51:45,280
button dot value so loop through the
9831
05:51:45,280 --> 05:51:46,480
buttons
9832
05:51:46,480 --> 05:51:49,520
go through them and then for each button
9833
05:51:49,520 --> 05:51:52,240
give it this function
9834
05:51:52,240 --> 05:51:53,120
okay
9835
05:51:53,120 --> 05:51:55,840
and now if i hit so if i hit run here
9836
05:51:55,840 --> 05:51:57,600
and i click nothing is going to happen
9837
05:51:57,600 --> 05:52:00,160
right now but let's go to our on click
9838
05:52:00,160 --> 05:52:01,840
function and here i'm just going to say
9839
05:52:01,840 --> 05:52:03,840
console.log
9840
05:52:03,840 --> 05:52:05,680
and we're just going to say console.log
9841
05:52:05,680 --> 05:52:08,558
playerchoice
9842
05:52:08,558 --> 05:52:10,080
so let's hit run
9843
05:52:10,080 --> 05:52:11,200
let's hit
9844
05:52:11,200 --> 05:52:13,520
and now you can see every time i click
9845
05:52:13,520 --> 05:52:15,680
rock it prints it out
9846
05:52:15,680 --> 05:52:17,680
when i click paper it prints it out when
9847
05:52:17,680 --> 05:52:20,840
i click scissors it prints it out as
9848
05:52:20,840 --> 05:52:25,120
well so now the rps button f loop is
9849
05:52:25,120 --> 05:52:27,680
doing what i wanted to do
9850
05:52:27,680 --> 05:52:29,280
and i'm going to just remove all of
9851
05:52:29,280 --> 05:52:30,878
these notes because i don't need them
9852
05:52:30,878 --> 05:52:31,920
anymore
9853
05:52:31,920 --> 05:52:34,320
because we just essentially did all of
9854
05:52:34,320 --> 05:52:37,280
this that it was asking
9855
05:52:37,280 --> 05:52:38,400
okay
9856
05:52:38,400 --> 05:52:39,920
and um
9857
05:52:39,920 --> 05:52:42,480
yeah so play game button is done
9858
05:52:42,480 --> 05:52:45,360
on click is detecting the click so that
9859
05:52:45,360 --> 05:52:47,520
is good now what i can do with the on
9860
05:52:47,520 --> 05:52:48,558
click
9861
05:52:48,558 --> 05:52:49,520
is
9862
05:52:49,520 --> 05:52:51,440
we have the computer choice already
9863
05:52:51,440 --> 05:52:52,558
ready to go
9864
05:52:52,558 --> 05:52:54,160
right and we already have the get
9865
05:52:54,160 --> 05:52:56,000
results function so we can pretty much
9866
05:52:56,000 --> 05:52:57,760
play rock paper scissors at least in the
9867
05:52:57,760 --> 05:52:59,200
command line like we're we're pretty
9868
05:52:59,200 --> 05:53:01,440
much done with rock paper scissors here
9869
05:53:01,440 --> 05:53:02,160
so
9870
05:53:02,160 --> 05:53:05,360
i'm gonna basically say con let's get
9871
05:53:05,360 --> 05:53:06,878
computer choice
9872
05:53:06,878 --> 05:53:09,040
get comp choice
9873
05:53:09,040 --> 05:53:10,080
or
9874
05:53:10,080 --> 05:53:12,240
computer
9875
05:53:12,240 --> 05:53:14,080
choice and we're gonna call that
9876
05:53:14,080 --> 05:53:17,040
computer choice function so let's try it
9877
05:53:17,040 --> 05:53:19,440
get computer
9878
05:53:19,440 --> 05:53:20,638
choice
9879
05:53:20,638 --> 05:53:23,280
and let's just console.log our computer
9880
05:53:23,280 --> 05:53:24,878
choice
9881
05:53:24,878 --> 05:53:25,920
okay
9882
05:53:25,920 --> 05:53:28,638
so now we should have and uh what we'll
9883
05:53:28,638 --> 05:53:29,760
do is
9884
05:53:29,760 --> 05:53:31,600
put it as an object so then it'll tell
9885
05:53:31,600 --> 05:53:33,280
us like what's the computer choice and
9886
05:53:33,280 --> 05:53:35,200
what's the player choice let's run this
9887
05:53:35,200 --> 05:53:37,360
and click and you can see player choice
9888
05:53:37,360 --> 05:53:39,600
is rock and computer choice is scissors
9889
05:53:39,600 --> 05:53:41,360
i'll click again
9890
05:53:41,360 --> 05:53:43,200
this time player choices rock computer
9891
05:53:43,200 --> 05:53:47,920
choice is paper i'll click here
9892
05:53:47,920 --> 05:53:49,840
i click this one right now so it says
9893
05:53:49,840 --> 05:53:51,680
our choice was paper computer choice was
9894
05:53:51,680 --> 05:53:55,040
rock so our get computer choice function
9895
05:53:55,040 --> 05:53:58,558
is working correctly that's really good
9896
05:53:58,558 --> 05:53:59,680
okay
9897
05:53:59,680 --> 05:54:00,718
well
9898
05:54:00,718 --> 05:54:02,878
now since we have player choice and
9899
05:54:02,878 --> 05:54:05,760
computer choice guess what we don't even
9900
05:54:05,760 --> 05:54:07,840
need to see how this get result function
9901
05:54:07,840 --> 05:54:08,878
works
9902
05:54:08,878 --> 05:54:10,878
we just need to call it now baby and
9903
05:54:10,878 --> 05:54:13,680
have it return the score that's it
9904
05:54:13,680 --> 05:54:14,558
so
9905
05:54:14,558 --> 05:54:16,878
let's go and do con score and we're
9906
05:54:16,878 --> 05:54:19,200
going to just say get result
9907
05:54:19,200 --> 05:54:20,798
and we're going to pass it the player
9908
05:54:20,798 --> 05:54:21,920
choice
9909
05:54:21,920 --> 05:54:23,360
but make sure you pass it in the right
9910
05:54:23,360 --> 05:54:24,798
order and we're going to pass it the
9911
05:54:24,798 --> 05:54:27,520
computer choice
9912
05:54:27,520 --> 05:54:28,718
all right
9913
05:54:28,718 --> 05:54:31,440
and then we're just gonna console log
9914
05:54:31,440 --> 05:54:33,760
the score here
9915
05:54:33,760 --> 05:54:35,200
okay and uh
9916
05:54:35,200 --> 05:54:37,120
make sure to just put it as an object so
9917
05:54:37,120 --> 05:54:39,440
it just looks nice let's hit run boom
9918
05:54:39,440 --> 05:54:41,120
look at that player choices rock
9919
05:54:41,120 --> 05:54:43,200
computer choices paper and the score is
9920
05:54:43,200 --> 05:54:45,040
negative one meaning we lost right
9921
05:54:45,040 --> 05:54:47,920
because paper beats rock
9922
05:54:47,920 --> 05:54:50,080
let's run it again we chose paper
9923
05:54:50,080 --> 05:54:52,558
computer chose rock paper beats rock so
9924
05:54:52,558 --> 05:54:55,440
we won this time let's choose scissors
9925
05:54:55,440 --> 05:54:58,080
and we lost because we chose scissors
9926
05:54:58,080 --> 05:55:00,320
and the computer chose rock and we lost
9927
05:55:00,320 --> 05:55:02,480
let's try scissors again and we won
9928
05:55:02,480 --> 05:55:04,400
because we chose scissors the computer
9929
05:55:04,400 --> 05:55:07,360
chose paper and the score is positive
9930
05:55:07,360 --> 05:55:08,160
one
9931
05:55:08,160 --> 05:55:10,480
meaning that we won
9932
05:55:10,480 --> 05:55:14,160
okay fantastic so guys the whole
9933
05:55:14,160 --> 05:55:16,718
thing is working you guys this is really
9934
05:55:16,718 --> 05:55:19,280
really exciting now the only things that
9935
05:55:19,280 --> 05:55:22,320
are left are we need to keep track of
9936
05:55:22,320 --> 05:55:24,958
the overall score of the game so every
9937
05:55:24,958 --> 05:55:27,120
time the score updates we need to
9938
05:55:27,120 --> 05:55:28,958
actually keep track of that score and
9939
05:55:28,958 --> 05:55:30,240
increment it
9940
05:55:30,240 --> 05:55:31,760
now what we could do is we could
9941
05:55:31,760 --> 05:55:34,558
actually create a object to actually
9942
05:55:34,558 --> 05:55:36,480
keep track of both of those scores and
9943
05:55:36,480 --> 05:55:38,080
we could put that object all the way at
9944
05:55:38,080 --> 05:55:39,600
the top why are we putting it all the
9945
05:55:39,600 --> 05:55:40,958
way at the top
9946
05:55:40,958 --> 05:55:44,080
so every single function can have access
9947
05:55:44,080 --> 05:55:46,320
to it because it's like a database okay
9948
05:55:46,320 --> 05:55:47,440
so
9949
05:55:47,440 --> 05:55:49,520
it will have all our scores we'll say
9950
05:55:49,520 --> 05:55:51,600
const
9951
05:55:51,600 --> 05:55:55,440
scores okay and then this will have
9952
05:55:55,440 --> 05:55:57,680
a computer score
9953
05:55:57,680 --> 05:55:59,520
okay and it'll start off with zero and
9954
05:55:59,520 --> 05:56:01,520
then it'll have player score and that
9955
05:56:01,520 --> 05:56:03,520
will start off with zero let's give it a
9956
05:56:03,520 --> 05:56:05,600
more descriptive name and let's call it
9957
05:56:05,600 --> 05:56:07,040
total score
9958
05:56:07,040 --> 05:56:07,840
okay
9959
05:56:07,840 --> 05:56:10,400
so this will be the total and we will
9960
05:56:10,400 --> 05:56:13,520
update it depending on you know whether
9961
05:56:13,520 --> 05:56:16,000
the computer wins or the player wins
9962
05:56:16,000 --> 05:56:19,040
right or the human wins
9963
05:56:19,040 --> 05:56:21,040
and where will we do this well what we
9964
05:56:21,040 --> 05:56:23,840
can actually do is we can do this inside
9965
05:56:23,840 --> 05:56:26,320
of our get result function
9966
05:56:26,320 --> 05:56:27,120
okay
9967
05:56:27,120 --> 05:56:29,840
so we can actually update this object
9968
05:56:29,840 --> 05:56:32,080
right over here directly
9969
05:56:32,080 --> 05:56:36,160
so inside of our uh get result function
9970
05:56:36,160 --> 05:56:38,240
you could pass it
9971
05:56:38,240 --> 05:56:40,240
the total score
9972
05:56:40,240 --> 05:56:42,240
right and then we could actually do that
9973
05:56:42,240 --> 05:56:44,000
update that so i'm gonna give it i'm
9974
05:56:44,000 --> 05:56:46,878
gonna say total score
9975
05:56:46,878 --> 05:56:49,840
let's pass it in
9976
05:56:52,160 --> 05:56:53,840
i changed my mind let's not do it in
9977
05:56:53,840 --> 05:56:56,080
there because this function is very pure
9978
05:56:56,080 --> 05:56:58,000
let's not pollute it
9979
05:56:58,000 --> 05:56:59,200
with other
9980
05:56:59,200 --> 05:57:00,878
things and this happens very commonly
9981
05:57:00,878 --> 05:57:02,558
you know as a developer you go back and
9982
05:57:02,558 --> 05:57:04,480
forth a lot because it's normal to kind
9983
05:57:04,480 --> 05:57:06,958
of go back and forth and then decide you
9984
05:57:06,958 --> 05:57:08,480
know what i'm going to keep my leave my
9985
05:57:08,480 --> 05:57:09,840
code clean because then otherwise this
9986
05:57:09,840 --> 05:57:10,718
function is going to have too many
9987
05:57:10,718 --> 05:57:12,000
dependencies and it's going to be kind
9988
05:57:12,000 --> 05:57:14,958
of annoying so let's leave that as is
9989
05:57:14,958 --> 05:57:17,520
we're gonna update this total score
9990
05:57:17,520 --> 05:57:20,558
after we get it right so let's say that
9991
05:57:20,558 --> 05:57:23,680
uh i i on click i get the computer
9992
05:57:23,680 --> 05:57:26,000
choice i get the score
9993
05:57:26,000 --> 05:57:28,240
well then i update the score based off
9994
05:57:28,240 --> 05:57:29,440
of that
9995
05:57:29,440 --> 05:57:31,200
okay so
9996
05:57:31,200 --> 05:57:33,600
i'm gonna basically go and say
9997
05:57:33,600 --> 05:57:36,400
get the total score
9998
05:57:36,400 --> 05:57:39,360
and we're gonna grab the
9999
05:57:39,360 --> 05:57:42,840
uh we're gonna grab the player
10000
05:57:42,840 --> 05:57:46,718
score and we're going to increment it by
10001
05:57:46,718 --> 05:57:48,638
whatever score
10002
05:57:48,638 --> 05:57:51,120
whatever whatever we get as a score okay
10003
05:57:51,120 --> 05:57:53,440
so this way for now we'll just keep
10004
05:57:53,440 --> 05:57:55,200
track of the player score you don't
10005
05:57:55,200 --> 05:57:56,480
really need to keep track of the
10006
05:57:56,480 --> 05:57:58,400
computer score
10007
05:57:58,400 --> 05:58:01,760
i will leave that to you to implement as
10008
05:58:01,760 --> 05:58:02,638
a
10009
05:58:02,638 --> 05:58:05,120
challenge okay but for now
10010
05:58:05,120 --> 05:58:07,280
we'll just keep track of the player
10011
05:58:07,280 --> 05:58:10,400
score and increment the player score
10012
05:58:10,400 --> 05:58:11,200
okay
10013
05:58:11,200 --> 05:58:13,280
and then let's console log
10014
05:58:13,280 --> 05:58:14,878
total score
10015
05:58:14,878 --> 05:58:17,280
and now let's play this game
10016
05:58:17,280 --> 05:58:18,878
so rock
10017
05:58:18,878 --> 05:58:20,718
so player score is negative one let's
10018
05:58:20,718 --> 05:58:23,680
play rock again so now this time we won
10019
05:58:23,680 --> 05:58:26,080
right so last game we lost the player
10020
05:58:26,080 --> 05:58:27,840
score became negative one
10021
05:58:27,840 --> 05:58:30,480
this game we won all right now all
10022
05:58:30,480 --> 05:58:32,000
that's left to do
10023
05:58:32,000 --> 05:58:34,798
is updating the dom to actually show you
10024
05:58:34,798 --> 05:58:37,280
one or you lost or you drew
10025
05:58:37,280 --> 05:58:39,760
so let's go to our show result function
10026
05:58:39,760 --> 05:58:40,958
this is where we're going to be doing
10027
05:58:40,958 --> 05:58:42,878
dom manipulation right
10028
05:58:42,878 --> 05:58:44,480
is we have a couple of different
10029
05:58:44,480 --> 05:58:47,680
scenarios we have a scenario where
10030
05:58:47,680 --> 05:58:51,840
we could say we lost right so
10031
05:58:52,400 --> 05:58:53,440
if
10032
05:58:53,440 --> 05:58:55,360
score
10033
05:58:55,360 --> 05:58:56,798
is
10034
05:58:56,798 --> 05:58:59,600
negative one
10035
05:59:00,160 --> 05:59:02,000
then do something
10036
05:59:02,000 --> 05:59:03,520
else if
10037
05:59:03,520 --> 05:59:05,920
score is
10038
05:59:05,920 --> 05:59:07,040
zero
10039
05:59:07,040 --> 05:59:09,280
then do something right
10040
05:59:09,280 --> 05:59:10,718
else if
10041
05:59:10,718 --> 05:59:11,920
score
10042
05:59:11,920 --> 05:59:13,200
is 1
10043
05:59:13,200 --> 05:59:16,558
or we could just say else
10044
05:59:16,798 --> 05:59:20,400
do something right so in this case
10045
05:59:20,400 --> 05:59:21,360
if we
10046
05:59:21,360 --> 05:59:23,920
want then what we want to say is
10047
05:59:23,920 --> 05:59:26,958
you or sorry you lose
10048
05:59:26,958 --> 05:59:29,760
okay in this situation we would want to
10049
05:59:29,760 --> 05:59:31,680
say you drew
10050
05:59:31,680 --> 05:59:33,280
or
10051
05:59:33,280 --> 05:59:35,520
it's a tie or something right so let's
10052
05:59:35,520 --> 05:59:37,200
put double quotes and now we'll say it's
10053
05:59:37,200 --> 05:59:39,760
a tie
10054
05:59:40,160 --> 05:59:44,000
and here we can say
10055
05:59:44,000 --> 05:59:45,600
u
10056
05:59:45,600 --> 05:59:47,680
1.
10057
05:59:47,680 --> 05:59:49,200
now we need to do something with these
10058
05:59:49,200 --> 05:59:50,400
strings because right now we're not
10059
05:59:50,400 --> 05:59:52,080
telling the computer to do anything
10060
05:59:52,080 --> 05:59:55,040
right so we're going to create
10061
05:59:55,040 --> 05:59:57,600
uh we're going to get the divs so let's
10062
05:59:57,600 --> 06:00:00,400
get the resultive
10063
06:00:00,400 --> 06:00:02,320
and this is going to be we're going to
10064
06:00:02,320 --> 06:00:05,200
get this by doing
10065
06:00:05,200 --> 06:00:08,650
document dot get element by id
10066
06:00:08,650 --> 06:00:10,080
[Music]
10067
06:00:10,080 --> 06:00:12,240
and i believe the id is called result
10068
06:00:12,240 --> 06:00:14,638
let's go take a look and we have result
10069
06:00:14,638 --> 06:00:15,600
here
10070
06:00:15,600 --> 06:00:17,200
okay we have result
10071
06:00:17,200 --> 06:00:19,280
we have player hands and then we have
10072
06:00:19,280 --> 06:00:21,680
player score
10073
06:00:21,680 --> 06:00:25,680
okay hands and then player score
10074
06:00:25,680 --> 06:00:28,638
okay let's go back
10075
06:00:31,360 --> 06:00:33,040
hands div is going to be
10076
06:00:33,040 --> 06:00:35,680
document.getelementbyid
10077
06:00:35,680 --> 06:00:38,320
and we're going to do hands and i
10078
06:00:38,320 --> 06:00:39,760
believe
10079
06:00:39,760 --> 06:00:41,520
this div player score we're also going
10080
06:00:41,520 --> 06:00:45,120
to need that so let's go const
10081
06:00:45,120 --> 06:00:48,320
player score div
10082
06:00:48,320 --> 06:00:51,120
document i get element
10083
06:00:51,120 --> 06:00:53,280
id
10084
06:00:53,280 --> 06:00:56,480
player score okay just like that in the
10085
06:00:56,480 --> 06:00:59,280
in a case where we lose right what we
10086
06:00:59,280 --> 06:01:00,638
actually want to do is we want to say
10087
06:01:00,638 --> 06:01:03,760
result div dot inner
10088
06:01:03,760 --> 06:01:04,878
text
10089
06:01:04,878 --> 06:01:07,200
i want to set that to you lose
10090
06:01:07,200 --> 06:01:08,400
and then we're going to do the same
10091
06:01:08,400 --> 06:01:12,240
thing here and the same thing here
10092
06:01:12,240 --> 06:01:13,600
okay
10093
06:01:13,600 --> 06:01:15,520
so let's see
10094
06:01:15,520 --> 06:01:17,200
what happens
10095
06:01:17,200 --> 06:01:21,520
now let's give it a try so i'll run this
10096
06:01:21,520 --> 06:01:23,200
we win
10097
06:01:23,200 --> 06:01:25,680
and i'm not seeing anything yet so let's
10098
06:01:25,680 --> 06:01:28,558
go and debug what the problem is so
10099
06:01:28,558 --> 06:01:30,240
resultive let's make sure that this
10100
06:01:30,240 --> 06:01:31,840
result div is
10101
06:01:31,840 --> 06:01:33,360
the actual div that we want so i'm going
10102
06:01:33,360 --> 06:01:35,600
to console.log or
10103
06:01:35,600 --> 06:01:37,840
actually we i don't think we're calling
10104
06:01:37,840 --> 06:01:41,040
the function show result anywhere
10105
06:01:41,040 --> 06:01:44,160
so let's go ahead and work on that now
10106
06:01:44,160 --> 06:01:46,000
so remember
10107
06:01:46,000 --> 06:01:47,600
on click rps what it should do is
10108
06:01:47,600 --> 06:01:50,400
calculate who won and show it on screen
10109
06:01:50,400 --> 06:01:51,760
right so
10110
06:01:51,760 --> 06:01:54,400
line 74 calculates who won
10111
06:01:54,400 --> 06:01:56,638
right then we update the leader the
10112
06:01:56,638 --> 06:01:59,280
total score
10113
06:01:59,600 --> 06:02:01,920
and then we want to show on the screen
10114
06:02:01,920 --> 06:02:04,080
who won so let's go ahead and do that
10115
06:02:04,080 --> 06:02:05,600
now
10116
06:02:05,600 --> 06:02:07,760
i'll do it at the bottom here
10117
06:02:07,760 --> 06:02:10,320
and we'll call our show result function
10118
06:02:10,320 --> 06:02:12,160
and it has a couple of arguments and you
10119
06:02:12,160 --> 06:02:13,680
can see it's telling us it has the
10120
06:02:13,680 --> 06:02:16,558
argument of
10121
06:02:18,718 --> 06:02:20,718
score
10122
06:02:20,718 --> 06:02:22,840
what other arguments do we have let's go
10123
06:02:22,840 --> 06:02:25,360
check let's go check player choice and
10124
06:02:25,360 --> 06:02:27,280
computer choice
10125
06:02:27,280 --> 06:02:28,718
so we're going to give it the player
10126
06:02:28,718 --> 06:02:30,320
choice
10127
06:02:30,320 --> 06:02:31,280
and then we're going to give it the
10128
06:02:31,280 --> 06:02:34,240
computer choice
10129
06:02:36,000 --> 06:02:38,480
there we go player choice computer
10130
06:02:38,480 --> 06:02:40,798
choice
10131
06:02:41,120 --> 06:02:42,240
okay
10132
06:02:42,240 --> 06:02:43,240
that's um
10133
06:02:43,240 --> 06:02:44,480
[Music]
10134
06:02:44,480 --> 06:02:46,320
that's effectively it that's all what we
10135
06:02:46,320 --> 06:02:47,840
need to do here so let's give it a try
10136
06:02:47,840 --> 06:02:49,600
now run it
10137
06:02:49,600 --> 06:02:52,080
boom and it says you lose and it that's
10138
06:02:52,080 --> 06:02:54,400
is that true that we lost right we got a
10139
06:02:54,400 --> 06:02:56,320
score of negative one let's try again
10140
06:02:56,320 --> 06:02:57,520
you won
10141
06:02:57,520 --> 06:02:59,520
and now look it says
10142
06:02:59,520 --> 06:03:02,160
um the score is one so i win and then
10143
06:03:02,160 --> 06:03:04,558
the total overall game score right the
10144
06:03:04,558 --> 06:03:08,558
total score it says is zero player score
10145
06:03:08,558 --> 06:03:10,558
is zero now because we won
10146
06:03:10,558 --> 06:03:12,798
and now it's a tie so everything remains
10147
06:03:12,798 --> 06:03:15,920
zero zero zero let's run again i lost it
10148
06:03:15,920 --> 06:03:19,360
goes back down to negative one
10149
06:03:19,520 --> 06:03:23,120
all right now one a bunch of times
10150
06:03:25,040 --> 06:03:26,958
dang my score is just getting worse and
10151
06:03:26,958 --> 06:03:27,920
worse
10152
06:03:27,920 --> 06:03:30,798
okay all good so our score is
10153
06:03:30,798 --> 06:03:32,718
working the way that we want it to work
10154
06:03:32,718 --> 06:03:34,400
right
10155
06:03:34,400 --> 06:03:37,760
so what we want to do next
10156
06:03:37,760 --> 06:03:40,878
is at the end we also want to update the
10157
06:03:40,878 --> 06:03:43,360
scores
10158
06:03:43,360 --> 06:03:44,480
okay
10159
06:03:44,480 --> 06:03:46,718
so let's go ahead and do that so let's
10160
06:03:46,718 --> 06:03:48,958
update the score div now so or actually
10161
06:03:48,958 --> 06:03:50,400
let's do hands first because that's
10162
06:03:50,400 --> 06:03:52,718
really important
10163
06:03:52,718 --> 06:03:56,240
so we're going to say hands div
10164
06:03:56,240 --> 06:03:57,920
dot inner
10165
06:03:57,920 --> 06:04:00,240
text
10166
06:04:00,240 --> 06:04:02,480
right and it's whatever the choices are
10167
06:04:02,480 --> 06:04:04,000
that the player choice and computer
10168
06:04:04,000 --> 06:04:06,480
choice so i'm going to say
10169
06:04:06,480 --> 06:04:08,958
player choice
10170
06:04:08,958 --> 06:04:11,600
versus
10171
06:04:12,558 --> 06:04:15,440
computer choice
10172
06:04:15,440 --> 06:04:17,200
okay let's run this and let's see what
10173
06:04:17,200 --> 06:04:19,680
happens rock versus scissors
10174
06:04:19,680 --> 06:04:21,680
paper versus rock
10175
06:04:21,680 --> 06:04:23,520
scissors versus rock
10176
06:04:23,520 --> 06:04:25,360
okay so that's working but it's not
10177
06:04:25,360 --> 06:04:26,718
really easy to tell which one is the
10178
06:04:26,718 --> 06:04:28,558
human choice and which one is a bot
10179
06:04:28,558 --> 06:04:29,600
choice what i'm going to do is i'm just
10180
06:04:29,600 --> 06:04:31,600
going to put an emoji here and an emoji
10181
06:04:31,600 --> 06:04:32,878
there
10182
06:04:32,878 --> 06:04:35,040
so here i'm just going to make a person
10183
06:04:35,040 --> 06:04:36,480
emoji
10184
06:04:36,480 --> 06:04:38,480
and then for this i'm just going to make
10185
06:04:38,480 --> 06:04:40,000
a
10186
06:04:40,000 --> 06:04:41,680
robot emoji
10187
06:04:41,680 --> 06:04:42,840
let's run
10188
06:04:42,840 --> 06:04:45,280
this now when i click you can see that
10189
06:04:45,280 --> 06:04:47,840
it shows a robot emoji a human emoji and
10190
06:04:47,840 --> 06:04:50,558
a robot emoji
10191
06:04:50,558 --> 06:04:51,840
right
10192
06:04:51,840 --> 06:04:53,760
and then all we need to do is just make
10193
06:04:53,760 --> 06:04:55,600
sure that the score
10194
06:04:55,600 --> 06:04:57,520
shows up
10195
06:04:57,520 --> 06:04:58,718
like
10196
06:04:58,718 --> 06:05:01,360
here right score score should show up
10197
06:05:01,360 --> 06:05:02,558
there
10198
06:05:02,558 --> 06:05:04,878
so let's go ahead and do that now
10199
06:05:04,878 --> 06:05:05,540
and i'm gonna
10200
06:05:05,540 --> 06:05:07,200
[Music]
10201
06:05:07,200 --> 06:05:09,280
access the player
10202
06:05:09,280 --> 06:05:10,840
score
10203
06:05:10,840 --> 06:05:15,520
div and grab its inner text
10204
06:05:16,878 --> 06:05:19,280
right and then we're just gonna get the
10205
06:05:19,280 --> 06:05:22,558
total score
10206
06:05:24,240 --> 06:05:26,798
and we're gonna set that total score
10207
06:05:26,798 --> 06:05:28,958
to
10208
06:05:31,600 --> 06:05:33,360
yeah that's actually it i don't even
10209
06:05:33,360 --> 06:05:35,120
think we have to do anything the total
10210
06:05:35,120 --> 06:05:37,040
score should just be this and we should
10211
06:05:37,040 --> 06:05:41,200
be good so let's run this now this
10212
06:05:41,200 --> 06:05:43,120
so it's bringing the total score as an
10213
06:05:43,120 --> 06:05:44,878
object because it is an object so we
10214
06:05:44,878 --> 06:05:47,280
actually just want the player choice
10215
06:05:47,280 --> 06:05:50,160
score so player score
10216
06:05:50,160 --> 06:05:51,920
let's run it
10217
06:05:51,920 --> 06:05:55,120
oops undefined so why is that undefined
10218
06:05:55,120 --> 06:05:57,440
let's see player ah player score not
10219
06:05:57,440 --> 06:05:59,760
player choice
10220
06:05:59,760 --> 06:06:01,680
run
10221
06:06:01,680 --> 06:06:06,400
boom negative one boom negative two
10222
06:06:06,958 --> 06:06:09,200
right if you want to make this
10223
06:06:09,200 --> 06:06:12,160
fancier or whatever you could put this
10224
06:06:12,160 --> 06:06:13,680
inside of template literals and you
10225
06:06:13,680 --> 06:06:16,000
could say your score or something
10226
06:06:16,000 --> 06:06:17,280
something along those lines if you
10227
06:06:17,280 --> 06:06:20,160
wanted to right you could say your score
10228
06:06:20,160 --> 06:06:24,080
say one two zero one
10229
06:06:24,080 --> 06:06:25,040
whatever
10230
06:06:25,040 --> 06:06:26,480
and there you go
10231
06:06:26,480 --> 06:06:28,878
now let's open this up in a
10232
06:06:28,878 --> 06:06:31,120
full tab
10233
06:06:31,120 --> 06:06:32,320
right and let's take a look at this
10234
06:06:32,320 --> 06:06:34,840
let's play this game
10235
06:06:34,840 --> 06:06:38,240
click rock versus rocket to tie click
10236
06:06:38,240 --> 06:06:41,360
again rock versus scissors you won
10237
06:06:41,360 --> 06:06:43,680
rock versus rock it's a tie
10238
06:06:43,680 --> 06:06:46,000
i picked rock computer picked paper i
10239
06:06:46,000 --> 06:06:48,638
lost right and my score is being updated
10240
06:06:48,638 --> 06:06:50,240
automatically let's just play a ton of
10241
06:06:50,240 --> 06:06:52,160
games and let's see what happens
10242
06:06:52,160 --> 06:06:53,920
all right we're getting on a
10243
06:06:53,920 --> 06:06:57,840
oh we lost a lot of games there
10244
06:06:58,558 --> 06:07:00,798
right so there we go so the computer is
10245
06:07:00,798 --> 06:07:03,680
doing a good job and uh but but we beat
10246
06:07:03,680 --> 06:07:05,440
the computer right that's what matters
10247
06:07:05,440 --> 06:07:08,240
we want at the end
10248
06:07:08,240 --> 06:07:09,840
so let's go back
10249
06:07:09,840 --> 06:07:12,958
and now if i hit the red nothing happens
10250
06:07:12,958 --> 06:07:14,638
so we just have to make the red button
10251
06:07:14,638 --> 06:07:15,520
work
10252
06:07:15,520 --> 06:07:17,120
so first
10253
06:07:17,120 --> 06:07:19,120
let's do the following so we're going to
10254
06:07:19,120 --> 06:07:21,360
say
10255
06:07:21,760 --> 06:07:23,520
const
10256
06:07:23,520 --> 06:07:25,680
end game button
10257
06:07:25,680 --> 06:07:26,480
and we're going to say
10258
06:07:26,480 --> 06:07:29,480
document.getelementbyid
10259
06:07:30,240 --> 06:07:32,320
and i think it's end game button if i'm
10260
06:07:32,320 --> 06:07:34,638
not mistaken so let's go back
10261
06:07:34,638 --> 06:07:35,680
let's look
10262
06:07:35,680 --> 06:07:37,440
and i have a button i gave it id of
10263
06:07:37,440 --> 06:07:39,680
endgame button so let's go back here and
10264
06:07:39,680 --> 06:07:41,280
we're going to say endgame button just
10265
06:07:41,280 --> 06:07:43,760
like this
10266
06:07:43,760 --> 06:07:45,760
all right and we just need to
10267
06:07:45,760 --> 06:07:48,150
connect it so we gotta go
10268
06:07:48,150 --> 06:07:50,160
[Music]
10269
06:07:50,160 --> 06:07:52,480
endgame button
10270
06:07:52,480 --> 06:07:54,160
dot on click
10271
06:07:54,160 --> 06:07:57,440
and on click we just wanna run our end
10272
06:07:57,440 --> 06:08:00,638
game function that's it
10273
06:08:00,638 --> 06:08:02,558
okay and in our endgame function what
10274
06:08:02,558 --> 06:08:04,400
needs to happen is it should just clear
10275
06:08:04,400 --> 06:08:06,958
all the score so in our endgame function
10276
06:08:06,958 --> 06:08:10,558
we're gonna grab our total score
10277
06:08:10,558 --> 06:08:13,600
okay let's actually pass a total score
10278
06:08:13,600 --> 06:08:15,920
total score right here
10279
06:08:15,920 --> 06:08:17,520
that's just to signify that we're
10280
06:08:17,520 --> 06:08:19,280
passing it the total score okay so we're
10281
06:08:19,280 --> 06:08:20,878
signifying that we're passing this
10282
06:08:20,878 --> 06:08:23,360
that's why we're making a dependency
10283
06:08:23,360 --> 06:08:25,200
and we're getting the total score
10284
06:08:25,200 --> 06:08:27,280
because the global variable that the
10285
06:08:27,280 --> 06:08:29,600
whole our entire app has access to this
10286
06:08:29,600 --> 06:08:31,360
total score
10287
06:08:31,360 --> 06:08:32,320
okay
10288
06:08:32,320 --> 06:08:33,760
so this function is going to take total
10289
06:08:33,760 --> 06:08:34,958
score
10290
06:08:34,958 --> 06:08:38,558
and it's going to set the total score of
10291
06:08:38,558 --> 06:08:42,120
the player score
10292
06:08:43,200 --> 06:08:45,280
to zero
10293
06:08:45,280 --> 06:08:46,958
and then we're also going to set the
10294
06:08:46,958 --> 06:08:51,360
computer score to zero here
10295
06:08:52,480 --> 06:08:55,440
and we're also going to just erase
10296
06:08:55,440 --> 06:08:57,760
everything that we see on the dom
10297
06:08:57,760 --> 06:09:01,040
okay so let's go ahead and grab our
10298
06:09:01,040 --> 06:09:02,558
all of our divs i'm going to copy them
10299
06:09:02,558 --> 06:09:04,000
from here
10300
06:09:04,000 --> 06:09:07,520
i'm going to paste these divs here
10301
06:09:07,840 --> 06:09:10,240
and then we're gonna grab their inner
10302
06:09:10,240 --> 06:09:13,520
text and set it to
10303
06:09:15,280 --> 06:09:16,878
empty
10304
06:09:16,878 --> 06:09:18,958
right and then we're gonna do that for
10305
06:09:18,958 --> 06:09:22,160
both of the other divs so paste paste
10306
06:09:22,160 --> 06:09:24,000
double click copy double click paste
10307
06:09:24,000 --> 06:09:27,440
double click copy double click paste
10308
06:09:27,440 --> 06:09:29,040
and that should make everything empty so
10309
06:09:29,040 --> 06:09:31,360
i think this should be good for the
10310
06:09:31,360 --> 06:09:33,200
endgame function so let's go ahead and
10311
06:09:33,200 --> 06:09:35,280
try this run it
10312
06:09:35,280 --> 06:09:37,440
play play play play hit the red button
10313
06:09:37,440 --> 06:09:39,600
and everything clears up run run run run
10314
06:09:39,600 --> 06:09:42,160
hit end
10315
06:09:42,400 --> 06:09:44,320
and you can see that everything is
10316
06:09:44,320 --> 06:09:46,718
working
10317
06:09:47,840 --> 06:09:49,280
okay
10318
06:09:49,280 --> 06:09:52,320
perfect so there you go this is your
10319
06:09:52,320 --> 06:09:55,520
entire app right let's go and try it
10320
06:09:55,520 --> 06:09:57,760
the entire app is working hit the red
10321
06:09:57,760 --> 06:10:01,040
button it stops starts again
10322
06:10:01,040 --> 06:10:03,200
okay so the challenge for you is see if
10323
06:10:03,200 --> 06:10:04,320
you could add
10324
06:10:04,320 --> 06:10:06,798
player score
10325
06:10:06,798 --> 06:10:09,520
player score and then computer score
10326
06:10:09,520 --> 06:10:11,520
and see if you can see if both of those
10327
06:10:11,520 --> 06:10:14,320
get updated as you play the game
10328
06:10:14,320 --> 06:10:16,080
right that would probably be the best
10329
06:10:16,080 --> 06:10:18,240
version of this app if you can make that
10330
06:10:18,240 --> 06:10:21,840
work that'd be really nice
10331
06:10:22,000 --> 06:10:23,840
and with that said we're essentially
10332
06:10:23,840 --> 06:10:25,840
done with this so all you got to do now
10333
06:10:25,840 --> 06:10:28,638
is try copying all of the code here so
10334
06:10:28,638 --> 06:10:32,320
command a command c or ctrl a and ctrl c
10335
06:10:32,320 --> 06:10:35,360
go back to the exercises that you know
10336
06:10:35,360 --> 06:10:36,160
the
10337
06:10:36,160 --> 06:10:37,200
where you were working on this
10338
06:10:37,200 --> 06:10:39,200
javascript course rebel so go back to
10339
06:10:39,200 --> 06:10:40,320
that
10340
06:10:40,320 --> 06:10:42,718
and then go to this exercise
10341
06:10:42,718 --> 06:10:45,680
folder go to script.js right
10342
06:10:45,680 --> 06:10:48,160
and see what happens if you paste your
10343
06:10:48,160 --> 06:10:50,000
entire code over this
10344
06:10:50,000 --> 06:10:52,878
so if you paste your code over this
10345
06:10:52,878 --> 06:10:54,558
and you hit run
10346
06:10:54,558 --> 06:10:56,160
and you go to
10347
06:10:56,160 --> 06:10:59,440
rock paper scissors project exercises
10348
06:10:59,440 --> 06:11:03,320
your game should run
10349
06:11:04,320 --> 06:11:06,000
just like that i'm going to command z
10350
06:11:06,000 --> 06:11:09,120
and undo all of this
10351
06:11:10,000 --> 06:11:11,440
all right
10352
06:11:11,440 --> 06:11:13,920
there we go
10353
06:11:14,160 --> 06:11:15,760
so you should be able to paste it in
10354
06:11:15,760 --> 06:11:17,760
here and i recommend that you do so then
10355
06:11:17,760 --> 06:11:19,200
you have this
10356
06:11:19,200 --> 06:11:21,440
fully working
10357
06:11:21,440 --> 06:11:23,280
okay and then if you want to delete all
10358
06:11:23,280 --> 06:11:25,520
the comments you can from this to kind
10359
06:11:25,520 --> 06:11:28,160
of make it make it a lot cleaner
10360
06:11:28,160 --> 06:11:29,200
but
10361
06:11:29,200 --> 06:11:32,080
at this point right the whole thing is
10362
06:11:32,080 --> 06:11:33,600
you you've done pretty much the whole
10363
06:11:33,600 --> 06:11:34,718
thing i'm going to remove all these
10364
06:11:34,718 --> 06:11:36,240
console logs
10365
06:11:36,240 --> 06:11:38,000
from here just so you could see this in
10366
06:11:38,000 --> 06:11:39,840
an easy
10367
06:11:39,840 --> 06:11:42,160
easy to see way
10368
06:11:42,160 --> 06:11:44,160
like this on click function you'll
10369
06:11:44,160 --> 06:11:47,120
realize is the main function right it's
10370
06:11:47,120 --> 06:11:48,400
the entire
10371
06:11:48,400 --> 06:11:51,120
kind of game logic that happens
10372
06:11:51,120 --> 06:11:53,920
that happens here
10373
06:11:53,920 --> 06:11:56,320
and um
10374
06:11:56,320 --> 06:11:59,760
we get the computer choice
10375
06:11:59,760 --> 06:12:02,320
we already have the player choice
10376
06:12:02,320 --> 06:12:04,240
we compute the result based on player
10377
06:12:04,240 --> 06:12:06,400
choice and computer choice
10378
06:12:06,400 --> 06:12:10,160
we update the scoreboard
10379
06:12:10,160 --> 06:12:12,400
and then we show that score
10380
06:12:12,400 --> 06:12:15,200
on the screen
10381
06:12:15,280 --> 06:12:17,680
so this is like the main logic of the
10382
06:12:17,680 --> 06:12:19,680
game right this is where your main main
10383
06:12:19,680 --> 06:12:22,400
main logic is happening
10384
06:12:22,400 --> 06:12:24,718
so i hope that this totally made sense
10385
06:12:24,718 --> 06:12:27,200
you understood every single step by step
10386
06:12:27,200 --> 06:12:29,280
parts of this
10387
06:12:29,280 --> 06:12:31,600
and this was a lot of fun for me to show
10388
06:12:31,600 --> 06:12:33,520
you i hope it was fun for you to
10389
06:12:33,520 --> 06:12:35,920
actually learn this and you enjoyed it
10390
06:12:35,920 --> 06:12:37,840
but that said i love your beautiful face
10391
06:12:37,840 --> 06:12:39,280
and i'll see you
10392
06:12:39,280 --> 06:12:40,638
shortly
10393
06:12:40,638 --> 06:12:43,520
time to learn apis or how to get
10394
06:12:43,520 --> 06:12:45,920
pictures of random dogs every single
10395
06:12:45,920 --> 06:12:49,360
time i'll refresh refresh again refresh
10396
06:12:49,360 --> 06:12:52,558
again and different dog different times
10397
06:12:52,558 --> 06:12:54,558
do you want to make this well guess what
10398
06:12:54,558 --> 06:12:56,400
you're going to need to learn how apis
10399
06:12:56,400 --> 06:12:58,320
work and don't worry we're going to keep
10400
06:12:58,320 --> 06:13:00,798
it bare bones very simple easy to get
10401
06:13:00,798 --> 06:13:02,638
started so
10402
06:13:02,638 --> 06:13:04,480
that way you could learn and not be
10403
06:13:04,480 --> 06:13:06,000
confused on
10404
06:13:06,000 --> 06:13:07,280
what is going on
10405
06:13:07,280 --> 06:13:09,200
now in order to learn about this let's
10406
06:13:09,200 --> 06:13:11,280
go ahead and code this up together from
10407
06:13:11,280 --> 06:13:13,520
scratch so hit command t and let's go to
10408
06:13:13,520 --> 06:13:16,320
replic.com all right so this is a
10409
06:13:16,320 --> 06:13:18,160
platform where you can code super easily
10410
06:13:18,160 --> 06:13:19,600
right if you've been coding along thus
10411
06:13:19,600 --> 06:13:22,878
far just choose html css javascript and
10412
06:13:22,878 --> 06:13:24,400
the app will be up and running right
10413
06:13:24,400 --> 06:13:26,878
away okay let's go ahead and do that and
10414
06:13:26,878 --> 06:13:29,120
for the name we're just gonna say dog
10415
06:13:29,120 --> 06:13:31,760
api playground okay this is just for fun
10416
06:13:31,760 --> 06:13:34,480
and we're just gonna test this out okay
10417
06:13:34,480 --> 06:13:35,280
now
10418
06:13:35,280 --> 06:13:36,638
it's going to give me a random
10419
06:13:36,638 --> 06:13:39,440
index.html file a script file and style
10420
06:13:39,440 --> 06:13:41,120
and script is already connected with
10421
06:13:41,120 --> 06:13:44,400
index for example if i do console.log
10422
06:13:44,400 --> 06:13:47,360
and i say hello it's going to show that
10423
06:13:47,360 --> 06:13:49,120
hello right there all we're going to do
10424
06:13:49,120 --> 06:13:51,680
here is let's add in a random div i'm
10425
06:13:51,680 --> 06:13:53,520
just going to say div and we're going to
10426
06:13:53,520 --> 06:13:56,718
say id for id maybe i'm just going to
10427
06:13:56,718 --> 06:13:57,600
say
10428
06:13:57,600 --> 06:14:00,240
dog image or something like that and
10429
06:14:00,240 --> 06:14:01,440
then we're going to give it an inner
10430
06:14:01,440 --> 06:14:04,480
html of image like this okay you'll see
10431
06:14:04,480 --> 06:14:06,080
why this is important because we're
10432
06:14:06,080 --> 06:14:08,958
going to manipulate this from our script
10433
06:14:08,958 --> 06:14:13,040
file okay so let's go grab dog image now
10434
06:14:13,040 --> 06:14:15,360
let's just play with
10435
06:14:15,360 --> 06:14:16,480
the
10436
06:14:16,480 --> 06:14:18,638
apis first okay so the url that we're
10437
06:14:18,638 --> 06:14:20,080
gonna need for this
10438
06:14:20,080 --> 06:14:22,558
is this one right over here okay
10439
06:14:22,558 --> 06:14:26,558
https colon slash slash dog dot ceo
10440
06:14:26,558 --> 06:14:29,600
slash api slash breed slash image slash
10441
06:14:29,600 --> 06:14:31,040
random now
10442
06:14:31,040 --> 06:14:33,280
for you what i'm gonna do is i'm gonna
10443
06:14:33,280 --> 06:14:37,680
go ahead and add this to the playground
10444
06:14:37,680 --> 06:14:39,680
so you'll have access to this okay so
10445
06:14:39,680 --> 06:14:42,558
i'm gonna create uh api section over
10446
06:14:42,558 --> 06:14:45,040
here okay we're gonna say apis
10447
06:14:45,040 --> 06:14:46,558
so if you're watching this javascript
10448
06:14:46,558 --> 06:14:49,120
tutorial you'll be able to
10449
06:14:49,120 --> 06:14:51,440
get access to this url okay so this is
10450
06:14:51,440 --> 06:14:54,400
for the dog api right i'll just say dog
10451
06:14:54,400 --> 06:14:57,040
api cool so you can go ahead inside of
10452
06:14:57,040 --> 06:14:59,760
here and and copy this okay let's go to
10453
06:14:59,760 --> 06:15:02,558
our brand new repo that we had created
10454
06:15:02,558 --> 06:15:05,760
and in here we're just gonna
10455
06:15:05,760 --> 06:15:08,160
get access to this okay so what do i
10456
06:15:08,160 --> 06:15:10,480
want to do i want to
10457
06:15:10,480 --> 06:15:12,160
fetch this so we're simply just gonna
10458
06:15:12,160 --> 06:15:14,080
say fetch and we're gonna pop in that
10459
06:15:14,080 --> 06:15:17,360
url that we got so copy this url paste
10460
06:15:17,360 --> 06:15:20,480
it in here now there are certain new
10461
06:15:20,480 --> 06:15:21,760
concepts that i'm gonna teach you but
10462
06:15:21,760 --> 06:15:23,440
let's just go one step at a time okay
10463
06:15:23,440 --> 06:15:25,120
right now i kind of want you to just
10464
06:15:25,120 --> 06:15:28,000
follow along with me and we'll code this
10465
06:15:28,000 --> 06:15:30,480
up and then worry about what it actually
10466
06:15:30,480 --> 06:15:31,840
does in just a second so i'm going to
10467
06:15:31,840 --> 06:15:34,638
say dot then all right and this dot i'll
10468
06:15:34,638 --> 06:15:37,120
put it here dot then but this is the
10469
06:15:37,120 --> 06:15:39,600
same as if you wrote it all on the same
10470
06:15:39,600 --> 06:15:42,000
line by the way so eight being on line
10471
06:15:42,000 --> 06:15:44,000
five here or here is actually the same
10472
06:15:44,000 --> 06:15:46,080
thing but it's just that i don't want
10473
06:15:46,080 --> 06:15:48,958
this long to run in line to run
10474
06:15:48,958 --> 06:15:50,878
infinitely long so i'm just going to hit
10475
06:15:50,878 --> 06:15:52,718
enter here so it breaks it off into
10476
06:15:52,718 --> 06:15:54,480
separate lines now we're going to get
10477
06:15:54,480 --> 06:15:56,320
back a response and i'm going to say hey
10478
06:15:56,320 --> 06:15:58,480
return to a response.json i know this is
10479
06:15:58,480 --> 06:16:00,000
confusing right now but we'll walk
10480
06:16:00,000 --> 06:16:02,798
through it in a second and then
10481
06:16:02,798 --> 06:16:04,240
i'm going to say
10482
06:16:04,240 --> 06:16:07,600
hey get me that json okay and i'm just
10483
06:16:07,600 --> 06:16:08,840
going to have you
10484
06:16:08,840 --> 06:16:11,760
console.log.json all right so let's run
10485
06:16:11,760 --> 06:16:13,840
this and let's see what happens if
10486
06:16:13,840 --> 06:16:16,000
anything at all and would you look at
10487
06:16:16,000 --> 06:16:18,638
that what my code is doing is going to
10488
06:16:18,638 --> 06:16:21,360
this api meaning there you know what is
10489
06:16:21,360 --> 06:16:23,360
an api there's some data somewhere in
10490
06:16:23,360 --> 06:16:25,600
the cloud somebody else is hosting it
10491
06:16:25,600 --> 06:16:27,280
and we're going grabbing access to it
10492
06:16:27,280 --> 06:16:29,440
and bringing into our app this way we
10493
06:16:29,440 --> 06:16:30,958
can be lazy and we don't need our own
10494
06:16:30,958 --> 06:16:32,558
database and we don't need to figure out
10495
06:16:32,558 --> 06:16:35,360
how to get random dog images this
10496
06:16:35,360 --> 06:16:37,040
as long as you have access this link or
10497
06:16:37,040 --> 06:16:39,440
this api it will automatically get you a
10498
06:16:39,440 --> 06:16:41,760
different dog image every single time so
10499
06:16:41,760 --> 06:16:43,440
now i'm gonna and you know there's
10500
06:16:43,440 --> 06:16:46,000
examples of lots of different uh apps
10501
06:16:46,000 --> 06:16:47,600
that you know you could use your api so
10502
06:16:47,600 --> 06:16:49,360
you could use a movie api and then also
10503
06:16:49,360 --> 06:16:50,480
you can have you could build like a
10504
06:16:50,480 --> 06:16:53,520
movie app without needing you know
10505
06:16:53,520 --> 06:16:56,240
every single movie which has millions of
10506
06:16:56,240 --> 06:16:58,160
them right you don't need access to them
10507
06:16:58,160 --> 06:16:59,680
you can actually
10508
06:16:59,680 --> 06:17:02,400
get access to them through a database
10509
06:17:02,400 --> 06:17:04,400
that allows you to do that
10510
06:17:04,400 --> 06:17:07,920
all right so right now i am actually
10511
06:17:07,920 --> 06:17:10,080
getting a random dog image every single
10512
06:17:10,080 --> 06:17:12,240
time over here okay i work console
10513
06:17:12,240 --> 06:17:14,080
logging it out so let's go ahead and
10514
06:17:14,080 --> 06:17:16,718
copy it to prove it i'm gonna hit
10515
06:17:16,718 --> 06:17:18,878
command t command v and i'm gonna paste
10516
06:17:18,878 --> 06:17:20,958
it in the browser and you can see that i
10517
06:17:20,958 --> 06:17:23,520
actually got a real real image right
10518
06:17:23,520 --> 06:17:24,958
it's a cute puppy
10519
06:17:24,958 --> 06:17:27,360
i have an english bulldog as well so
10520
06:17:27,360 --> 06:17:29,840
you know it's kind of cute i like dogs
10521
06:17:29,840 --> 06:17:31,360
so here
10522
06:17:31,360 --> 06:17:33,680
i'm fetching this data and then once you
10523
06:17:33,680 --> 06:17:35,600
fetch it you have to do something to it
10524
06:17:35,600 --> 06:17:38,320
right away so this is where this dot
10525
06:17:38,320 --> 06:17:40,000
then thing comes in so whenever you see
10526
06:17:40,000 --> 06:17:43,120
this dot then thing this is talking
10527
06:17:43,120 --> 06:17:45,120
about promises
10528
06:17:45,120 --> 06:17:47,200
okay so promises is this like
10529
06:17:47,200 --> 06:17:50,798
weird confusing concept in javascript
10530
06:17:50,798 --> 06:17:52,718
because this
10531
06:17:52,718 --> 06:17:55,680
leads you into asynchronous programming
10532
06:17:55,680 --> 06:17:58,080
and all that basically means is up until
10533
06:17:58,080 --> 06:18:01,040
now you were writing you know uh regular
10534
06:18:01,040 --> 06:18:04,080
scripts you had this like list of
10535
06:18:04,080 --> 06:18:05,440
instructions that you would have the
10536
06:18:05,440 --> 06:18:07,520
computer do like you would say hey
10537
06:18:07,520 --> 06:18:09,520
you know run this for loop then go to
10538
06:18:09,520 --> 06:18:11,440
this array and grab this then do this
10539
06:18:11,440 --> 06:18:13,600
with that array then filter that johnny
10540
06:18:13,600 --> 06:18:15,440
depp net worth and show me that
10541
06:18:15,440 --> 06:18:17,680
different actors that was
10542
06:18:17,680 --> 06:18:20,240
like synchronous code meaning it runs
10543
06:18:20,240 --> 06:18:22,240
line by line when you're dealing with
10544
06:18:22,240 --> 06:18:24,320
getting data from somewhere else like
10545
06:18:24,320 --> 06:18:26,160
from a third party website all of a
10546
06:18:26,160 --> 06:18:28,240
sudden your code isn't synchronous
10547
06:18:28,240 --> 06:18:30,480
anymore meaning that your code doesn't
10548
06:18:30,480 --> 06:18:31,280
learn
10549
06:18:31,280 --> 06:18:33,680
run linearly anymore so here is an
10550
06:18:33,680 --> 06:18:35,600
example of this okay let me see if i
10551
06:18:35,600 --> 06:18:37,920
need to zoom in more that's good for now
10552
06:18:37,920 --> 06:18:39,280
so i'm going to go ahead i'm going to
10553
06:18:39,280 --> 06:18:42,000
say console.log say yo
10554
06:18:42,000 --> 06:18:43,520
and i'm going to paste that right
10555
06:18:43,520 --> 06:18:44,878
underneath this
10556
06:18:44,878 --> 06:18:46,558
and we're gonna run this
10557
06:18:46,558 --> 06:18:49,600
and you can see that this did not run
10558
06:18:49,600 --> 06:18:52,080
linearly right so
10559
06:18:52,080 --> 06:18:54,718
here let's prove this right so i'm gonna
10560
06:18:54,718 --> 06:18:56,638
delete that hello at the top
10561
06:18:56,638 --> 06:18:59,200
so run
10562
06:18:59,200 --> 06:19:00,160
first
10563
06:19:00,160 --> 06:19:02,000
and then this should run
10564
06:19:02,000 --> 06:19:04,718
third right because what should happen
10565
06:19:04,718 --> 06:19:05,680
is
10566
06:19:05,680 --> 06:19:07,680
and you would imagine that this console
10567
06:19:07,680 --> 06:19:09,200
log here would run seconds i'm just
10568
06:19:09,200 --> 06:19:12,080
going to say comma or actually let's do
10569
06:19:12,080 --> 06:19:14,558
it here and i'm just gonna say run
10570
06:19:14,558 --> 06:19:17,040
second all right so what should happen
10571
06:19:17,040 --> 06:19:19,200
is this console log should run first
10572
06:19:19,200 --> 06:19:21,360
this over here should run this console
10573
06:19:21,360 --> 06:19:23,360
long should run second and then this
10574
06:19:23,360 --> 06:19:25,840
console log should run third but when i
10575
06:19:25,840 --> 06:19:28,240
hit run watch what happens right run
10576
06:19:28,240 --> 06:19:32,160
first and run third ran first and then
10577
06:19:32,160 --> 06:19:34,400
run second showed up
10578
06:19:34,400 --> 06:19:37,040
so what is happening well we make a
10579
06:19:37,040 --> 06:19:39,360
request to this url so that's the same
10580
06:19:39,360 --> 06:19:41,360
as if i com pasted this right you saw
10581
06:19:41,360 --> 06:19:43,760
how this takes a second to actually show
10582
06:19:43,760 --> 06:19:44,478
you
10583
06:19:44,478 --> 06:19:47,200
a new dog image link right it takes a
10584
06:19:47,200 --> 06:19:50,558
few seconds one two one two
10585
06:19:50,558 --> 06:19:53,600
one two one two so it takes about one to
10586
06:19:53,600 --> 06:19:56,478
two seconds whereas this code right here
10587
06:19:56,478 --> 06:19:59,840
runs almost instantly okay so what
10588
06:19:59,840 --> 06:20:01,920
javascript decides to do is it says hey
10589
06:20:01,920 --> 06:20:04,160
i'm gonna run everything that i already
10590
06:20:04,160 --> 06:20:07,120
know so it already knows this it runs it
10591
06:20:07,120 --> 06:20:09,440
it already knows this it runs it but
10592
06:20:09,440 --> 06:20:12,478
this information over here right this
10593
06:20:12,478 --> 06:20:14,558
takes maybe two seconds so let's write
10594
06:20:14,558 --> 06:20:16,798
two seconds right here that takes two
10595
06:20:16,798 --> 06:20:17,840
seconds
10596
06:20:17,840 --> 06:20:19,920
once that takes two seconds and gets
10597
06:20:19,920 --> 06:20:23,200
resolved right this returns to us as a
10598
06:20:23,200 --> 06:20:25,920
javascript promise so
10599
06:20:25,920 --> 06:20:28,160
we just get back we're like okay we got
10600
06:20:28,160 --> 06:20:29,520
the data back
10601
06:20:29,520 --> 06:20:31,840
these dot dens what they do is we go
10602
06:20:31,840 --> 06:20:34,240
okay once we get that data back
10603
06:20:34,240 --> 06:20:37,200
then i want to turn that data into json
10604
06:20:37,200 --> 06:20:39,920
okay so we'll talk about what json
10605
06:20:39,920 --> 06:20:42,400
actually means so that's this is step
10606
06:20:42,400 --> 06:20:45,040
one right and then what we do with this
10607
06:20:45,040 --> 06:20:48,080
is we console log that json so this is
10608
06:20:48,080 --> 06:20:50,080
step two and i know this is really weird
10609
06:20:50,080 --> 06:20:52,320
notation but just.dense but you'll get
10610
06:20:52,320 --> 06:20:54,320
used to it after some point when you
10611
06:20:54,320 --> 06:20:55,920
start learning more and more about like
10612
06:20:55,920 --> 06:20:58,638
apis and asynchronous programming or for
10613
06:20:58,638 --> 06:21:00,718
now just like kind of memorize it that
10614
06:21:00,718 --> 06:21:02,638
whenever you have to do fetch you do
10615
06:21:02,638 --> 06:21:05,280
these two dot dens every single time and
10616
06:21:05,280 --> 06:21:06,958
you pretty much can copy paste this
10617
06:21:06,958 --> 06:21:09,680
exact type of code um every single time
10618
06:21:09,680 --> 06:21:12,638
you do a api request okay that's what's
10619
06:21:12,638 --> 06:21:14,080
happening okay and these are functions
10620
06:21:14,080 --> 06:21:15,360
so i'm just basically saying hey dot
10621
06:21:15,360 --> 06:21:18,240
then you know grab the response and turn
10622
06:21:18,240 --> 06:21:21,120
it into a json and what json does is
10623
06:21:21,120 --> 06:21:23,280
json turns the data essentially to this
10624
06:21:23,280 --> 06:21:26,080
object type of format that you see here
10625
06:21:26,080 --> 06:21:27,840
right so you have message and then you
10626
06:21:27,840 --> 06:21:30,000
have the url
10627
06:21:30,000 --> 06:21:32,558
and then there's another key so we have
10628
06:21:32,558 --> 06:21:34,160
one key with the message which is the
10629
06:21:34,160 --> 06:21:36,878
actual image for the dog and then we
10630
06:21:36,878 --> 06:21:38,718
have another key which is status which
10631
06:21:38,718 --> 06:21:40,160
tells you whether it was successful or
10632
06:21:40,160 --> 06:21:42,958
not and then that object ends right so
10633
06:21:42,958 --> 06:21:45,600
that json is really just an object with
10634
06:21:45,600 --> 06:21:49,440
two keys inside of it message and status
10635
06:21:49,440 --> 06:21:51,760
that's what this is
10636
06:21:51,760 --> 06:21:53,360
you know getting back and then once we
10637
06:21:53,360 --> 06:21:55,680
get that back we go ahead and console
10638
06:21:55,680 --> 06:21:57,120
log it out
10639
06:21:57,120 --> 06:21:58,840
this dot then
10640
06:21:58,840 --> 06:22:02,320
runs whenever this promise here resolves
10641
06:22:02,320 --> 06:22:04,958
okay now watch what happens if i let's
10642
06:22:04,958 --> 06:22:06,080
say
10643
06:22:06,080 --> 06:22:07,840
cut this right i'd remove those lines
10644
06:22:07,840 --> 06:22:10,400
and i just say console.log and we run
10645
06:22:10,400 --> 06:22:11,200
this
10646
06:22:11,200 --> 06:22:13,360
let's run this and you notice it just
10647
06:22:13,360 --> 06:22:16,080
says promise what the heck is that right
10648
06:22:16,080 --> 06:22:17,600
i can't really do anything with the
10649
06:22:17,600 --> 06:22:20,000
promise promise is a really weird thing
10650
06:22:20,000 --> 06:22:22,240
but when i bring that back
10651
06:22:22,240 --> 06:22:24,160
now something happens
10652
06:22:24,160 --> 06:22:27,120
so whenever you're dealing with promises
10653
06:22:27,120 --> 06:22:29,040
if you just call this it's going to give
10654
06:22:29,040 --> 06:22:31,440
you a promise but you have to do a dot
10655
06:22:31,440 --> 06:22:32,558
then
10656
06:22:32,558 --> 06:22:34,798
and this other dot then these two dot
10657
06:22:34,798 --> 06:22:37,680
dents chained together to pull that data
10658
06:22:37,680 --> 06:22:38,558
out
10659
06:22:38,558 --> 06:22:40,638
after your code runs and waits for two
10660
06:22:40,638 --> 06:22:43,600
seconds so this is an example of like
10661
06:22:43,600 --> 06:22:45,520
asynchronous programming and it's a
10662
06:22:45,520 --> 06:22:47,440
complicated way of just saying like
10663
06:22:47,440 --> 06:22:49,440
stuff you have to wait for
10664
06:22:49,440 --> 06:22:52,160
this over here is stuff you don't have
10665
06:22:52,160 --> 06:22:53,520
to wait for
10666
06:22:53,520 --> 06:22:55,760
right this is literally that's literally
10667
06:22:55,760 --> 06:22:58,080
all that means okay stuff you gotta wait
10668
06:22:58,080 --> 06:23:00,080
stuff you don't have to wait for stuff
10669
06:23:00,080 --> 06:23:02,000
you have to wait for and when you have
10670
06:23:02,000 --> 06:23:03,680
stuff you have to wait for
10671
06:23:03,680 --> 06:23:05,920
you have to do these dot dents
10672
06:23:05,920 --> 06:23:07,840
that's when you use them okay and then
10673
06:23:07,840 --> 06:23:10,080
they just call them these like fancy
10674
06:23:10,080 --> 06:23:11,840
names that confuse you like promises
10675
06:23:11,840 --> 06:23:14,160
asynchronous programming in your head
10676
06:23:14,160 --> 06:23:15,680
honestly between you and i don't use
10677
06:23:15,680 --> 06:23:17,440
those words okay in your in your head
10678
06:23:17,440 --> 06:23:18,400
just like
10679
06:23:18,400 --> 06:23:20,798
use simpler words so you can understand
10680
06:23:20,798 --> 06:23:22,478
it but like learn these words because
10681
06:23:22,478 --> 06:23:24,240
they're going to be helpful for you when
10682
06:23:24,240 --> 06:23:25,760
you're googling things right how do
10683
06:23:25,760 --> 06:23:27,600
promises work how how do you do
10684
06:23:27,600 --> 06:23:29,040
asynchronous programming with javascript
10685
06:23:29,040 --> 06:23:31,120
but like for yourself just go
10686
06:23:31,120 --> 06:23:33,280
is this stuff do i need to wait for
10687
06:23:33,280 --> 06:23:35,520
is this like a url or a database thing
10688
06:23:35,520 --> 06:23:38,080
that i'm doing yes i probably need to
10689
06:23:38,080 --> 06:23:41,120
wait a few seconds to get that data yes
10690
06:23:41,120 --> 06:23:42,638
okay then i just know that i'm going to
10691
06:23:42,638 --> 06:23:45,760
need to do dense google how to call how
10692
06:23:45,760 --> 06:23:48,638
to do a fetch api request google
10693
06:23:48,638 --> 06:23:51,360
how do i deal with promises google how
10694
06:23:51,360 --> 06:23:54,160
to get a json file from a promise
10695
06:23:54,160 --> 06:23:55,920
right so that's where your google skills
10696
06:23:55,920 --> 06:23:58,878
come in and stack overflow you go
10697
06:23:58,878 --> 06:24:00,718
there and you get the information that
10698
06:24:00,718 --> 06:24:02,080
you actually need
10699
06:24:02,080 --> 06:24:04,320
all right so hopefully
10700
06:24:04,320 --> 06:24:06,320
this part over here makes sense to you
10701
06:24:06,320 --> 06:24:09,360
and we're retrieving the data now we
10702
06:24:09,360 --> 06:24:10,718
want to take this
10703
06:24:10,718 --> 06:24:13,040
further okay so what we want to do now
10704
06:24:13,040 --> 06:24:13,920
is
10705
06:24:13,920 --> 06:24:15,680
we want to do this
10706
06:24:15,680 --> 06:24:17,280
right and then we want to actually
10707
06:24:17,280 --> 06:24:19,040
update our
10708
06:24:19,040 --> 06:24:21,680
we want to update our um
10709
06:24:21,680 --> 06:24:24,000
this right over here okay we want to we
10710
06:24:24,000 --> 06:24:26,240
want to update this right over here and
10711
06:24:26,240 --> 06:24:28,638
we want to like put the dog image here
10712
06:24:28,638 --> 06:24:30,400
so the new dog image that we're getting
10713
06:24:30,400 --> 06:24:31,920
all right so let's do this once we get
10714
06:24:31,920 --> 06:24:34,878
that json back right here then what we
10715
06:24:34,878 --> 06:24:36,320
can do is
10716
06:24:36,320 --> 06:24:38,478
once we get it back then we go ahead and
10717
06:24:38,478 --> 06:24:39,680
update
10718
06:24:39,680 --> 06:24:41,360
this dom right over here right and put
10719
06:24:41,360 --> 06:24:42,718
the dog image
10720
06:24:42,718 --> 06:24:44,558
so let's go ahead and do that now so i'm
10721
06:24:44,558 --> 06:24:47,040
gonna i'm gonna remove this console log
10722
06:24:47,040 --> 06:24:49,200
here and we're going to go right there
10723
06:24:49,200 --> 06:24:51,200
i'm just going to say console.log
10724
06:24:51,200 --> 06:24:52,400
json
10725
06:24:52,400 --> 06:24:54,400
right and let's run this
10726
06:24:54,400 --> 06:24:56,080
and this is the json that i'm getting
10727
06:24:56,080 --> 06:24:58,160
and i can see that this json is just an
10728
06:24:58,160 --> 06:25:00,840
object so if i actually did
10729
06:25:00,840 --> 06:25:03,120
json.message i should get
10730
06:25:03,120 --> 06:25:04,958
the contents of that message so i should
10731
06:25:04,958 --> 06:25:07,520
get this url right over here
10732
06:25:07,520 --> 06:25:09,200
all right so let's go ahead and try that
10733
06:25:09,200 --> 06:25:11,520
so i'm going to say json message and you
10734
06:25:11,520 --> 06:25:13,600
can see that i'm getting that that like
10735
06:25:13,600 --> 06:25:16,080
specific url that we need yeah all right
10736
06:25:16,080 --> 06:25:17,760
now that i know i'm pulling this data
10737
06:25:17,760 --> 06:25:20,320
all i have to do is just update
10738
06:25:20,320 --> 06:25:23,280
this dog image div and change its inner
10739
06:25:23,280 --> 06:25:26,478
html to a different image okay so let's
10740
06:25:26,478 --> 06:25:28,240
go ahead and get access to this i'm
10741
06:25:28,240 --> 06:25:29,600
going to go back
10742
06:25:29,600 --> 06:25:30,798
boom
10743
06:25:30,798 --> 06:25:33,280
and i'm just going to say
10744
06:25:33,280 --> 06:25:35,280
you know const
10745
06:25:35,280 --> 06:25:38,080
get or dog image
10746
06:25:38,080 --> 06:25:40,878
and it's going to say
10747
06:25:40,920 --> 06:25:42,478
document.getelementbyid and i'm going to
10748
06:25:42,478 --> 06:25:44,478
pass in this id
10749
06:25:44,478 --> 06:25:46,000
right and then all we're going to do
10750
06:25:46,000 --> 06:25:49,200
here is dot inner html
10751
06:25:49,200 --> 06:25:51,680
and let's do template literals here and
10752
06:25:51,680 --> 06:25:53,120
i'm going to say image
10753
06:25:53,120 --> 06:25:55,920
set set the source for the image
10754
06:25:55,920 --> 06:25:59,120
and the source for the image is going to
10755
06:25:59,120 --> 06:26:00,320
be
10756
06:26:00,320 --> 06:26:02,240
json.message
10757
06:26:02,240 --> 06:26:04,000
all right and then we'll just end this
10758
06:26:04,000 --> 06:26:06,080
image tag right there
10759
06:26:06,080 --> 06:26:08,478
so this should do the trick let's try it
10760
06:26:08,478 --> 06:26:10,638
i have a broken image so interesting
10761
06:26:10,638 --> 06:26:12,160
what's happening here i need to put
10762
06:26:12,160 --> 06:26:15,360
quotes around this let's give that a try
10763
06:26:15,360 --> 06:26:16,400
okay
10764
06:26:16,400 --> 06:26:19,040
boom there you go so now you can see our
10765
06:26:19,040 --> 06:26:21,680
dog images oh that's a cute dog look at
10766
06:26:21,680 --> 06:26:22,638
that
10767
06:26:22,638 --> 06:26:25,840
and i i have a i love pomeranians i just
10768
06:26:25,840 --> 06:26:28,638
think they're so cute oh that is cute
10769
06:26:28,638 --> 06:26:30,080
look at these guys
10770
06:26:30,080 --> 06:26:31,520
just playing
10771
06:26:31,520 --> 06:26:33,280
run right every single time you run it
10772
06:26:33,280 --> 06:26:35,520
you should now just be getting a new dog
10773
06:26:35,520 --> 06:26:36,878
image right so
10774
06:26:36,878 --> 06:26:38,478
this is working
10775
06:26:38,478 --> 06:26:40,400
now their sizes are kind of different
10776
06:26:40,400 --> 06:26:42,160
and a little bit all over the place so
10777
06:26:42,160 --> 06:26:44,000
in order to make sure that we have a
10778
06:26:44,000 --> 06:26:46,080
size that we actually like what we can
10779
06:26:46,080 --> 06:26:48,958
just say is i can pass the
10780
06:26:48,958 --> 06:26:50,718
uh height
10781
06:26:50,718 --> 06:26:52,878
and width properties okay so we can say
10782
06:26:52,878 --> 06:26:56,878
height is 100 and width is 100 let's try
10783
06:26:56,878 --> 06:26:58,240
running it now
10784
06:26:58,240 --> 06:26:59,920
and you'll see that we'll always get an
10785
06:26:59,920 --> 06:27:01,920
image of this exact size every single
10786
06:27:01,920 --> 06:27:02,878
time
10787
06:27:02,878 --> 06:27:06,160
but maybe we can make it 300
10788
06:27:06,160 --> 06:27:07,600
and 300
10789
06:27:07,600 --> 06:27:09,680
that way it'll be big but it won't like
10790
06:27:09,680 --> 06:27:11,680
completely blow out a proportion right
10791
06:27:11,680 --> 06:27:14,400
so there we go so 300 by 300 is not a
10792
06:27:14,400 --> 06:27:15,440
bad one
10793
06:27:15,440 --> 06:27:18,958
as you could do that okay so this is
10794
06:27:18,958 --> 06:27:21,760
just like a really bare-bones starting
10795
06:27:21,760 --> 06:27:25,600
example of apis and like that quickly
10796
06:27:25,600 --> 06:27:27,920
you have a up and running app
10797
06:27:27,920 --> 06:27:29,840
you know this is nice like if you had to
10798
06:27:29,840 --> 06:27:31,120
do this by yourself you know how much
10799
06:27:31,120 --> 06:27:32,558
work that would take you would have to
10800
06:27:32,558 --> 06:27:35,120
collect infinite dog photos manually
10801
06:27:35,120 --> 06:27:37,440
you'll have to download all of them then
10802
06:27:37,440 --> 06:27:40,160
you'll need like a file with like
10803
06:27:40,160 --> 06:27:42,638
hundreds of dog photos and then your
10804
06:27:42,638 --> 06:27:44,798
code would need to like go through that
10805
06:27:44,798 --> 06:27:47,440
file manually and like randomly select
10806
06:27:47,440 --> 06:27:49,360
the dog and then show that dog and you
10807
06:27:49,360 --> 06:27:51,280
have to keep that dog thing updated and
10808
06:27:51,280 --> 06:27:52,160
it's like
10809
06:27:52,160 --> 06:27:55,280
so much work but here somebody's doing
10810
06:27:55,280 --> 06:27:57,520
that for you with the random dog images
10811
06:27:57,520 --> 06:27:59,840
that they have collected and all you get
10812
06:27:59,840 --> 06:28:02,080
to do is just hit the run button every
10813
06:28:02,080 --> 06:28:05,200
single time and get a different freaking
10814
06:28:05,200 --> 06:28:06,240
dog
10815
06:28:06,240 --> 06:28:08,478
that's nice now what we could do is
10816
06:28:08,478 --> 06:28:10,478
instead of making this app where every
10817
06:28:10,478 --> 06:28:12,478
single time you have to like refresh
10818
06:28:12,478 --> 06:28:14,400
this app and get a new dog image that's
10819
06:28:14,400 --> 06:28:17,040
kind of like lame let's make it so we
10820
06:28:17,040 --> 06:28:19,120
have a button that says like get new dog
10821
06:28:19,120 --> 06:28:21,360
or get dog or something like that and
10822
06:28:21,360 --> 06:28:24,240
then every single time we click that we
10823
06:28:24,240 --> 06:28:28,320
get a new dog okay so you you have
10824
06:28:28,320 --> 06:28:30,160
enough information to be able to kind of
10825
06:28:30,160 --> 06:28:31,920
do this yourself
10826
06:28:31,920 --> 06:28:34,400
so if you want to challenge yourself you
10827
06:28:34,400 --> 06:28:36,718
could try it but if you don't and you
10828
06:28:36,718 --> 06:28:38,400
just kind of want to follow along that's
10829
06:28:38,400 --> 06:28:39,680
all right too
10830
06:28:39,680 --> 06:28:41,360
because we will have some exercises
10831
06:28:41,360 --> 06:28:43,200
coming up for you okay so don't have to
10832
06:28:43,200 --> 06:28:45,120
kill yourself here all right so first
10833
06:28:45,120 --> 06:28:46,080
thing i'm going to do is i'm going to
10834
06:28:46,080 --> 06:28:47,920
turn this into a function okay in my
10835
06:28:47,920 --> 06:28:49,840
mind i'm thinking about it like this i
10836
06:28:49,840 --> 06:28:51,600
want to turn this piece of code into a
10837
06:28:51,600 --> 06:28:54,080
function and what i want to happen is i
10838
06:28:54,080 --> 06:28:56,320
want this piece of code to run every
10839
06:28:56,320 --> 06:28:59,920
only when i click the button and
10840
06:28:59,920 --> 06:29:01,920
every time i click the button this piece
10841
06:29:01,920 --> 06:29:04,718
of code should run all right so let's go
10842
06:29:04,718 --> 06:29:07,600
ahead and add a button right underneath
10843
06:29:07,600 --> 06:29:10,160
this okay so this button can be added by
10844
06:29:10,160 --> 06:29:13,040
going into html hitting index.html right
10845
06:29:13,040 --> 06:29:15,280
here and right underneath here we can
10846
06:29:15,280 --> 06:29:16,400
say button
10847
06:29:16,400 --> 06:29:18,080
get new dog
10848
06:29:18,080 --> 06:29:19,440
let's
10849
06:29:19,440 --> 06:29:22,958
yeah let's give it an id of dog bun
10850
06:29:22,958 --> 06:29:25,360
okay let's run and now we should have a
10851
06:29:25,360 --> 06:29:27,760
get new dog button okay cool
10852
06:29:27,760 --> 06:29:28,478
now
10853
06:29:28,478 --> 06:29:30,638
what we want to do is i just want to say
10854
06:29:30,638 --> 06:29:32,400
hey every time i click that button so
10855
06:29:32,400 --> 06:29:35,120
let's go let's go grab that dog button
10856
06:29:35,120 --> 06:29:37,040
because i'm going to say const
10857
06:29:37,040 --> 06:29:38,798
dog button
10858
06:29:38,798 --> 06:29:40,320
go to document
10859
06:29:40,320 --> 06:29:43,360
get elements by id
10860
06:29:43,360 --> 06:29:46,160
there's a dog button okay and here i'm
10861
06:29:46,160 --> 06:29:48,240
going to say dog button dot
10862
06:29:48,240 --> 06:29:49,680
on click
10863
06:29:49,680 --> 06:29:52,718
and we're going to set its on click to
10864
06:29:52,718 --> 06:29:54,638
just say you
10865
06:29:54,638 --> 06:29:56,798
clicked get new dog okay so it's just
10866
06:29:56,798 --> 06:29:58,478
gonna say like hey i noticed that you
10867
06:29:58,478 --> 06:30:01,280
click get new dog okay so click it click
10868
06:30:01,280 --> 06:30:02,958
it click it click it every single time i
10869
06:30:02,958 --> 06:30:03,840
click it
10870
06:30:03,840 --> 06:30:05,840
you'll notice that at the bottom right
10871
06:30:05,840 --> 06:30:06,798
here
10872
06:30:06,798 --> 06:30:09,120
it says get new dog right so
10873
06:30:09,120 --> 06:30:10,878
it is working click click click click
10874
06:30:10,878 --> 06:30:11,920
click
10875
06:30:11,920 --> 06:30:13,840
okay perfect so it's doing
10876
06:30:13,840 --> 06:30:15,520
what it's supposed to do it's detecting
10877
06:30:15,520 --> 06:30:17,040
the click now what i want to do is i
10878
06:30:17,040 --> 06:30:18,240
want to run
10879
06:30:18,240 --> 06:30:20,638
this fetch request on click so let's
10880
06:30:20,638 --> 06:30:22,798
wrap this bad boy inside of a function
10881
06:30:22,798 --> 06:30:25,200
so i'm going to say const get
10882
06:30:25,200 --> 06:30:27,200
new dog okay and we're going to turn
10883
06:30:27,200 --> 06:30:29,040
this into a function and we'll do it
10884
06:30:29,040 --> 06:30:30,798
like this and i'll take this code and
10885
06:30:30,798 --> 06:30:32,638
i'll paste it in here let's format it so
10886
06:30:32,638 --> 06:30:34,000
it looks beautiful
10887
06:30:34,000 --> 06:30:34,958
nice
10888
06:30:34,958 --> 06:30:37,280
so now what this is doing it get there's
10889
06:30:37,280 --> 06:30:39,200
a function called get new dog when we
10890
06:30:39,200 --> 06:30:42,718
call it it fetches this data it fetches
10891
06:30:42,718 --> 06:30:44,558
this url
10892
06:30:44,558 --> 06:30:47,120
uh gets a response
10893
06:30:47,120 --> 06:30:49,440
like console logs the response right and
10894
06:30:49,440 --> 06:30:51,200
then it like updates the image now we
10895
06:30:51,200 --> 06:30:52,718
don't even need this console log here so
10896
06:30:52,718 --> 06:30:54,478
i'll just remove it
10897
06:30:54,478 --> 06:30:56,160
so all this does is
10898
06:30:56,160 --> 06:30:59,280
gets the url and then updates the image
10899
06:30:59,280 --> 06:31:00,958
with the new dog
10900
06:31:00,958 --> 06:31:01,760
thing
10901
06:31:01,760 --> 06:31:03,200
all right and
10902
06:31:03,200 --> 06:31:05,120
here what we're gonna do
10903
06:31:05,120 --> 06:31:07,680
is we're just going to go ahead and call
10904
06:31:07,680 --> 06:31:09,120
that function so i'm just going to say
10905
06:31:09,120 --> 06:31:10,798
get new dog
10906
06:31:10,798 --> 06:31:12,638
like this and we're going to call this
10907
06:31:12,638 --> 06:31:13,600
function
10908
06:31:13,600 --> 06:31:16,160
okay let's run it
10909
06:31:16,160 --> 06:31:17,840
run our app and now what we're going to
10910
06:31:17,840 --> 06:31:20,080
do is we're going to say get new dog and
10911
06:31:20,080 --> 06:31:21,840
boom it just got us a new dog let's hit
10912
06:31:21,840 --> 06:31:24,240
it again get new dog boom got us a new
10913
06:31:24,240 --> 06:31:27,040
dog get new dog getting you right that
10914
06:31:27,040 --> 06:31:30,080
is so nice and now that dog button every
10915
06:31:30,080 --> 06:31:32,240
time i click it that button is doing the
10916
06:31:32,240 --> 06:31:34,798
work for us and we don't have to like
10917
06:31:34,798 --> 06:31:37,600
kind of do anything right that's nice
10918
06:31:37,600 --> 06:31:39,360
uh if we want we could put that button
10919
06:31:39,360 --> 06:31:41,280
at the top as well so what we could do
10920
06:31:41,280 --> 06:31:43,760
is let's go to index.html i'll take this
10921
06:31:43,760 --> 06:31:46,558
button from here i'll kind of move it at
10922
06:31:46,558 --> 06:31:49,200
the top right there and then this hello
10923
06:31:49,200 --> 06:31:51,600
world i'll just completely remove it so
10924
06:31:51,600 --> 06:31:54,240
if we go to our app we have get new dog
10925
06:31:54,240 --> 06:31:55,520
right at the top
10926
06:31:55,520 --> 06:31:57,840
you click it you get a new dog you click
10927
06:31:57,840 --> 06:32:00,878
it again you get a new dog again
10928
06:32:00,878 --> 06:32:02,320
if you want you could give everything
10929
06:32:02,320 --> 06:32:03,840
kind of a margin
10930
06:32:03,840 --> 06:32:05,520
but we don't really need to do that for
10931
06:32:05,520 --> 06:32:07,920
this we can just keep this up oh that is
10932
06:32:07,920 --> 06:32:11,280
cute um i have english bulldog that very
10933
06:32:11,280 --> 06:32:12,400
similar
10934
06:32:12,400 --> 06:32:14,400
to this
10935
06:32:14,400 --> 06:32:16,718
yeah she's adorable she has huge paws
10936
06:32:16,718 --> 06:32:17,760
man
10937
06:32:17,760 --> 06:32:19,840
so yeah so this get new dog thing is
10938
06:32:19,840 --> 06:32:21,120
working
10939
06:32:21,120 --> 06:32:23,440
kind of honestly how we wanted to and
10940
06:32:23,440 --> 06:32:25,280
it's really
10941
06:32:25,280 --> 06:32:27,920
kind of straightforward so
10942
06:32:27,920 --> 06:32:30,638
not too complicated honestly like
10943
06:32:30,638 --> 06:32:33,280
it works it makes sense and what i'll do
10944
06:32:33,280 --> 06:32:35,760
for you is i'll take this lesson right
10945
06:32:35,760 --> 06:32:38,160
and turn it into a lesson in our main
10946
06:32:38,160 --> 06:32:40,400
like javascript app that we were working
10947
06:32:40,400 --> 06:32:43,280
on and i'll put it in here for you so
10948
06:32:43,280 --> 06:32:45,120
then like you can actually go through
10949
06:32:45,120 --> 06:32:46,160
this
10950
06:32:46,160 --> 06:32:47,920
api's lessons right so okay i'll make a
10951
06:32:47,920 --> 06:32:49,920
new folder that way it's like clear it's
10952
06:32:49,920 --> 06:32:52,558
like dog related right and then in here
10953
06:32:52,558 --> 06:32:54,558
i'm just gonna add
10954
06:32:54,558 --> 06:32:57,200
an index.html file
10955
06:32:57,200 --> 06:32:59,920
i'll also add a
10956
06:32:59,920 --> 06:33:02,878
script.js file
10957
06:33:02,878 --> 06:33:04,160
and then you don't really need a style
10958
06:33:04,160 --> 06:33:05,520
sheet for this because there's not much
10959
06:33:05,520 --> 06:33:08,080
style so we'll paste the javascript code
10960
06:33:08,080 --> 06:33:09,360
that we wrote
10961
06:33:09,360 --> 06:33:12,558
we'll go back to this app and i will
10962
06:33:12,558 --> 06:33:16,240
grab the index.html i'll copy this
10963
06:33:16,240 --> 06:33:19,680
and we'll go ahead and paste that
10964
06:33:19,680 --> 06:33:22,160
index.html
10965
06:33:22,160 --> 06:33:24,798
code in here for dog random
10966
06:33:24,798 --> 06:33:27,840
so what's nice about this now is
10967
06:33:27,840 --> 06:33:30,000
right over here on the right hand side
10968
06:33:30,000 --> 06:33:32,798
right you'll have under lessons you'll
10969
06:33:32,798 --> 06:33:35,040
have api lesson for get random dog
10970
06:33:35,040 --> 06:33:36,638
you'll be able to click it other people
10971
06:33:36,638 --> 06:33:38,000
will be able to see that you made this
10972
06:33:38,000 --> 06:33:40,080
too and you just hit get new dog and
10973
06:33:40,080 --> 06:33:42,718
you'll get a new dog every single time
10974
06:33:42,718 --> 06:33:44,878
and then in order to
10975
06:33:44,878 --> 06:33:48,000
like actually see the code for this app
10976
06:33:48,000 --> 06:33:50,320
you can go ahead on the left hand side
10977
06:33:50,320 --> 06:33:51,920
go into api
10978
06:33:51,920 --> 06:33:54,400
go into dog random
10979
06:33:54,400 --> 06:33:56,320
and boom you'll see it right here
10980
06:33:56,320 --> 06:33:58,478
so this code is like the solution is
10981
06:33:58,478 --> 06:34:00,400
there for you if you need it this is
10982
06:34:00,400 --> 06:34:01,920
going to be the
10983
06:34:01,920 --> 06:34:04,798
uh little project that we just made okay
10984
06:34:04,798 --> 06:34:06,718
and then we're going to like do more
10985
06:34:06,718 --> 06:34:08,718
practice stuff with apis okay so we're
10986
06:34:08,718 --> 06:34:11,280
gonna get to it right now but hopefully
10987
06:34:11,280 --> 06:34:12,160
like
10988
06:34:12,160 --> 06:34:14,878
this app makes sense for you so far
10989
06:34:14,878 --> 06:34:17,360
all right now we're gonna build another
10990
06:34:17,360 --> 06:34:20,478
app with apis and this is going to be a
10991
06:34:20,478 --> 06:34:22,798
superhero one and you should try
10992
06:34:22,798 --> 06:34:24,320
to kind of make a little bit of it
10993
06:34:24,320 --> 06:34:25,760
yourself as we go along but they're
10994
06:34:25,760 --> 06:34:27,040
going to be certain concepts in here
10995
06:34:27,040 --> 06:34:28,718
that might be new for example like
10996
06:34:28,718 --> 06:34:31,760
access tokens but this superhero api app
10997
06:34:31,760 --> 06:34:33,600
is going to do the following every time
10998
06:34:33,600 --> 06:34:36,320
you click get new hero is going to
10999
06:34:36,320 --> 06:34:38,558
randomly select the hero show you that
11000
06:34:38,558 --> 06:34:40,958
heroes intelligence or durability or
11001
06:34:40,958 --> 06:34:42,958
attack power or speed or something along
11002
06:34:42,958 --> 06:34:45,760
those lines like stats you know so for
11003
06:34:45,760 --> 06:34:49,200
example if you typed in get random hero
11004
06:34:49,200 --> 06:34:51,280
it might bring a batman or if you typed
11005
06:34:51,280 --> 06:34:53,120
in another hero it might bring up let's
11006
06:34:53,120 --> 06:34:55,440
say spider-man and so you should be able
11007
06:34:55,440 --> 06:34:57,840
to do that with this app now if we want
11008
06:34:57,840 --> 06:34:59,200
to really take this app to the next
11009
06:34:59,200 --> 06:35:00,080
level
11010
06:35:00,080 --> 06:35:01,920
you can try making it where you can type
11011
06:35:01,920 --> 06:35:04,000
in the name of the hero like batman and
11012
06:35:04,000 --> 06:35:06,320
hit enter and it should automatically
11013
06:35:06,320 --> 06:35:08,958
pop up batman's image and it should show
11014
06:35:08,958 --> 06:35:11,920
batman's stats or maybe his biography
11015
06:35:11,920 --> 06:35:13,920
and then every single time you type in a
11016
06:35:13,920 --> 06:35:16,638
new person and say get hero it
11017
06:35:16,638 --> 06:35:18,400
automatically does that every single
11018
06:35:18,400 --> 06:35:20,080
time okay so this is what we're gonna
11019
06:35:20,080 --> 06:35:21,840
code up let's go ahead and do this
11020
06:35:21,840 --> 06:35:24,080
together step number one go to
11021
06:35:24,080 --> 06:35:26,160
superheroapi.com
11022
06:35:26,160 --> 06:35:27,600
it should bring you to this page where
11023
06:35:27,600 --> 06:35:29,280
it says get all superheroes and villains
11024
06:35:29,280 --> 06:35:31,360
data from all universes under a single
11025
06:35:31,360 --> 06:35:32,478
api
11026
06:35:32,478 --> 06:35:33,520
all right
11027
06:35:33,520 --> 06:35:36,000
so now here's what you need for this
11028
06:35:36,000 --> 06:35:37,840
you're going to need it's going to
11029
06:35:37,840 --> 06:35:39,760
generate an access token for you so
11030
06:35:39,760 --> 06:35:41,040
you're going to need to connect it with
11031
06:35:41,040 --> 06:35:42,798
your facebook if you don't have a
11032
06:35:42,798 --> 06:35:44,718
facebook go ahead and create one it's
11033
06:35:44,718 --> 06:35:47,360
free make a dummy one come here connect
11034
06:35:47,360 --> 06:35:49,440
it okay so do that
11035
06:35:49,440 --> 06:35:51,360
and then you'll have a
11036
06:35:51,360 --> 06:35:53,280
account access token okay so once you
11037
06:35:53,280 --> 06:35:55,200
log in with facebook this token is gonna
11038
06:35:55,200 --> 06:35:56,638
be important because without this token
11039
06:35:56,638 --> 06:35:58,878
this api will not work so make sure you
11040
06:35:58,878 --> 06:36:00,240
have that and then what you're going to
11041
06:36:00,240 --> 06:36:03,440
need is this url right here okay this is
11042
06:36:03,440 --> 06:36:04,878
going to be your base
11043
06:36:04,878 --> 06:36:07,520
url then for this api there are a couple
11044
06:36:07,520 --> 06:36:09,520
of other things that that are helpful to
11045
06:36:09,520 --> 06:36:11,600
know for you is
11046
06:36:11,600 --> 06:36:13,600
how this api will work is you'll need to
11047
06:36:13,600 --> 06:36:16,240
either pass in the id of the hero or
11048
06:36:16,240 --> 06:36:18,240
you're going to need to search that hero
11049
06:36:18,240 --> 06:36:20,400
okay so if i for example go here you can
11050
06:36:20,400 --> 06:36:22,878
find the character ids here
11051
06:36:22,878 --> 06:36:24,638
you'll be able to go here and see all
11052
06:36:24,638 --> 06:36:26,718
the heroes right so you have ethan hunt
11053
06:36:26,718 --> 06:36:29,120
you have evil deadpool
11054
06:36:29,120 --> 06:36:30,958
you have spiderman
11055
06:36:30,958 --> 06:36:34,798
right over here right you have batman
11056
06:36:34,798 --> 06:36:37,920
hawkeye you have thanos so you have all
11057
06:36:37,920 --> 06:36:40,718
these heroes as either you pass in their
11058
06:36:40,718 --> 06:36:43,120
id so for example you saw that the id
11059
06:36:43,120 --> 06:36:46,080
was two four five so what that basically
11060
06:36:46,080 --> 06:36:48,160
means is what your url will look like is
11061
06:36:48,160 --> 06:36:51,120
the following so you'll have https call
11062
06:36:51,120 --> 06:36:53,440
superhero api.com
11063
06:36:53,440 --> 06:36:57,440
slash api slash whatever your token is
11064
06:36:57,440 --> 06:36:58,958
so i'm going to go ahead and copy my
11065
06:36:58,958 --> 06:37:01,760
token and then we're going to go ahead
11066
06:37:01,760 --> 06:37:03,520
and paste this
11067
06:37:03,520 --> 06:37:06,558
oh sorry let's go ahead and do this
11068
06:37:06,558 --> 06:37:07,280
so
11069
06:37:07,280 --> 06:37:08,320
right so
11070
06:37:08,320 --> 06:37:11,200
api.com api slash
11071
06:37:11,200 --> 06:37:12,958
whatever just imagine that that says
11072
06:37:12,958 --> 06:37:14,958
your access token right here okay your
11073
06:37:14,958 --> 06:37:16,320
access token
11074
06:37:16,320 --> 06:37:19,280
slash you're gonna type in id
11075
06:37:19,280 --> 06:37:21,440
slash whatever hero that you want so
11076
06:37:21,440 --> 06:37:24,958
let's say 245 or whatever it is okay now
11077
06:37:24,958 --> 06:37:27,760
if i try to go to this url let's see
11078
06:37:27,760 --> 06:37:29,440
what happens and let's make sure i bring
11079
06:37:29,440 --> 06:37:30,320
my
11080
06:37:30,320 --> 06:37:31,760
access token and repl i'm going to
11081
06:37:31,760 --> 06:37:34,160
replace this part with my access token
11082
06:37:34,160 --> 06:37:36,240
let's hit enter and let's see if we get
11083
06:37:36,240 --> 06:37:38,320
anything if it works at all right you
11084
06:37:38,320 --> 06:37:40,000
actually just say
11085
06:37:40,000 --> 06:37:42,478
245 here just like that okay so let's
11086
06:37:42,478 --> 06:37:43,680
try that
11087
06:37:43,680 --> 06:37:44,958
and you can see that i'm actually
11088
06:37:44,958 --> 06:37:48,240
getting the data back on ethan hunt
11089
06:37:48,240 --> 06:37:50,240
and i have power stats on him so his
11090
06:37:50,240 --> 06:37:51,760
intelligence is
11091
06:37:51,760 --> 06:37:54,160
strength is pretty low 11
11092
06:37:54,160 --> 06:37:56,558
speed is kind of low
11093
06:37:56,558 --> 06:37:58,958
uh durability is low but then like
11094
06:37:58,958 --> 06:38:00,878
combat is pretty high
11095
06:38:00,878 --> 06:38:02,638
right ethan hunt
11096
06:38:02,638 --> 06:38:04,400
and then he has a place of birth which
11097
06:38:04,400 --> 06:38:08,080
is wisconsin i did not know that okay
11098
06:38:08,080 --> 06:38:10,240
his height is five six
11099
06:38:10,240 --> 06:38:12,400
uh what's his occupation he's an agent
11100
06:38:12,400 --> 06:38:14,558
he's a training officer
11101
06:38:14,558 --> 06:38:17,200
and um yeah and then here's his image so
11102
06:38:17,200 --> 06:38:19,280
i can actually click that image
11103
06:38:19,280 --> 06:38:21,040
and boom it shows me
11104
06:38:21,040 --> 06:38:22,718
okay apparently
11105
06:38:22,718 --> 06:38:25,360
it's uh tom cruise okay that's what i
11106
06:38:25,360 --> 06:38:26,478
expected
11107
06:38:26,478 --> 06:38:28,240
so let's go back here
11108
06:38:28,240 --> 06:38:31,600
so this is the basic this is a call like
11109
06:38:31,600 --> 06:38:34,000
a sample call reference okay so this is
11110
06:38:34,000 --> 06:38:35,840
going to be useful for you
11111
06:38:35,840 --> 06:38:38,240
and now we can actually just start
11112
06:38:38,240 --> 06:38:40,080
coding this up so let's go ahead open a
11113
06:38:40,080 --> 06:38:43,120
new repl so let's go to replic.com
11114
06:38:43,120 --> 06:38:44,798
replay.com
11115
06:38:44,798 --> 06:38:47,120
okay and then just go ahead and choose
11116
06:38:47,120 --> 06:38:49,680
that html css javascript one that's the
11117
06:38:49,680 --> 06:38:52,958
one we want and we're gonna say
11118
06:38:52,958 --> 06:38:56,958
superhero api okay let's go
11119
06:38:56,958 --> 06:38:58,080
cool
11120
06:38:58,080 --> 06:38:59,840
and in here we're gonna do kind of a
11121
06:38:59,840 --> 06:39:01,600
similar thing but this time we're going
11122
06:39:01,600 --> 06:39:03,120
to say get super hero we're just going
11123
06:39:03,120 --> 06:39:04,478
to create a function right from the
11124
06:39:04,478 --> 06:39:05,520
start
11125
06:39:05,520 --> 06:39:07,910
all right and then we'll just say hello
11126
06:39:07,910 --> 06:39:09,200
[Music]
11127
06:39:09,200 --> 06:39:11,040
and we'll just call this function let's
11128
06:39:11,040 --> 06:39:13,200
just to make sure that this function is
11129
06:39:13,200 --> 06:39:14,558
working
11130
06:39:14,558 --> 06:39:16,878
and make sure to put an equal sign here
11131
06:39:16,878 --> 06:39:18,718
and run this
11132
06:39:18,718 --> 06:39:20,878
okay now you should it should say hello
11133
06:39:20,878 --> 06:39:22,240
at the bottom right there and you don't
11134
06:39:22,240 --> 06:39:23,920
actually even need to do console log
11135
06:39:23,920 --> 06:39:26,160
here just run this because it already
11136
06:39:26,160 --> 06:39:27,760
has a console log
11137
06:39:27,760 --> 06:39:29,680
and you can see that we're getting back
11138
06:39:29,680 --> 06:39:30,798
hello
11139
06:39:30,798 --> 06:39:33,200
perfect so seems like our get superhero
11140
06:39:33,200 --> 06:39:35,120
function we created it we are able to
11141
06:39:35,120 --> 06:39:37,040
call it now
11142
06:39:37,040 --> 06:39:38,478
inside of this function what i actually
11143
06:39:38,478 --> 06:39:39,600
want to do
11144
06:39:39,600 --> 06:39:42,080
is i want to make that fetch request so
11145
06:39:42,080 --> 06:39:43,440
remember that
11146
06:39:43,440 --> 06:39:46,240
fetch requests we made earlier you can
11147
06:39:46,240 --> 06:39:48,080
pretty much go ahead
11148
06:39:48,080 --> 06:39:49,840
for a dog random
11149
06:39:49,840 --> 06:39:52,558
and look at it and your fetch request is
11150
06:39:52,558 --> 06:39:55,360
going to be very similar to that
11151
06:39:55,360 --> 06:39:57,360
all right so remember it's fetch you
11152
06:39:57,360 --> 06:39:59,840
pass in the url and then you do these
11153
06:39:59,840 --> 06:40:02,160
dot thens
11154
06:40:02,160 --> 06:40:04,320
so let's go ahead and
11155
06:40:04,320 --> 06:40:06,000
do that now okay so i'm going to
11156
06:40:06,000 --> 06:40:08,000
basically say fetch
11157
06:40:08,000 --> 06:40:10,240
and let's put that
11158
06:40:10,240 --> 06:40:12,798
reference url there so we have we can
11159
06:40:12,798 --> 06:40:14,558
see it at all times
11160
06:40:14,558 --> 06:40:16,718
and now i'm going to
11161
06:40:16,718 --> 06:40:18,160
basically
11162
06:40:18,160 --> 06:40:21,040
uh use the url right so
11163
06:40:21,040 --> 06:40:22,798
let's go ahead and do that i'm going to
11164
06:40:22,798 --> 06:40:23,760
paste
11165
06:40:23,760 --> 06:40:27,440
and character id here is going to be
11166
06:40:27,440 --> 06:40:29,760
let's just say 245 because that's ethan
11167
06:40:29,760 --> 06:40:32,240
hunt and for access token i'm going to
11168
06:40:32,240 --> 06:40:33,520
go back
11169
06:40:33,520 --> 06:40:37,920
and copy the access token so copy
11170
06:40:37,920 --> 06:40:38,958
and
11171
06:40:38,958 --> 06:40:40,958
let's paste it in
11172
06:40:40,958 --> 06:40:42,798
in place of that where it says access
11173
06:40:42,798 --> 06:40:44,958
token
11174
06:40:44,958 --> 06:40:46,958
okay replace that with just whatever
11175
06:40:46,958 --> 06:40:49,920
your token is and uh this is what you
11176
06:40:49,920 --> 06:40:53,120
should have now one really really key
11177
06:40:53,120 --> 06:40:54,718
hack that i'm going to show you here is
11178
06:40:54,718 --> 06:40:57,360
make sure to put php here if you don't
11179
06:40:57,360 --> 06:41:00,080
put that this will not work i know it's
11180
06:41:00,080 --> 06:41:02,478
pretty weird but this is a weird hack
11181
06:41:02,478 --> 06:41:05,760
that i had to kind of find around it
11182
06:41:05,760 --> 06:41:08,400
and it's just for the superhero api okay
11183
06:41:08,400 --> 06:41:11,600
you don't ever like need to do api.php
11184
06:41:11,600 --> 06:41:13,440
like that okay
11185
06:41:13,440 --> 06:41:15,760
so there we go this should get the ethan
11186
06:41:15,760 --> 06:41:18,240
hunt guy and then we just need to do the
11187
06:41:18,240 --> 06:41:19,920
two dot dens that we've talked about so
11188
06:41:19,920 --> 06:41:21,680
i'm going to say response
11189
06:41:21,680 --> 06:41:25,200
get the response as a json
11190
06:41:25,200 --> 06:41:26,798
and then we need to do something with
11191
06:41:26,798 --> 06:41:29,360
that response we're going to say then
11192
06:41:29,360 --> 06:41:31,200
once you get that
11193
06:41:31,200 --> 06:41:32,718
we're going to say json and we're going
11194
06:41:32,718 --> 06:41:33,760
to say
11195
06:41:33,760 --> 06:41:35,600
console
11196
06:41:35,600 --> 06:41:37,760
log that json
11197
06:41:37,760 --> 06:41:39,600
okay so
11198
06:41:39,600 --> 06:41:42,718
with fetch requests you can pretty much
11199
06:41:42,718 --> 06:41:43,840
like
11200
06:41:43,840 --> 06:41:45,680
copy paste these lines almost every
11201
06:41:45,680 --> 06:41:47,040
single time
11202
06:41:47,040 --> 06:41:49,120
and then fetch of course is just going
11203
06:41:49,120 --> 06:41:50,320
to fetch that's not going to change the
11204
06:41:50,320 --> 06:41:51,600
only thing that's really going to change
11205
06:41:51,600 --> 06:41:53,280
is what goes in here
11206
06:41:53,280 --> 06:41:54,080
okay
11207
06:41:54,080 --> 06:41:55,600
so i'm going to hit run on this and
11208
06:41:55,600 --> 06:41:58,160
let's see if anything works and boom
11209
06:41:58,160 --> 06:42:02,798
look at that we are getting ethan hunt
11210
06:42:02,798 --> 06:42:04,798
we're getting that data
11211
06:42:04,798 --> 06:42:06,478
right over here
11212
06:42:06,478 --> 06:42:09,920
uh just as we want it to
11213
06:42:09,920 --> 06:42:12,718
okay so we get response success id is
11214
06:42:12,718 --> 06:42:14,160
245
11215
06:42:14,160 --> 06:42:16,160
name is ethan hunts and then we're
11216
06:42:16,160 --> 06:42:18,320
getting all the power stats that's
11217
06:42:18,320 --> 06:42:20,558
perfect right now if i wanted to get any
11218
06:42:20,558 --> 06:42:22,320
one of these specific things i could do
11219
06:42:22,320 --> 06:42:24,798
like let's say the dot id or dot name
11220
06:42:24,798 --> 06:42:26,478
let's say i just did dot name and let's
11221
06:42:26,478 --> 06:42:27,760
run this
11222
06:42:27,760 --> 06:42:30,000
i should just get back i should just get
11223
06:42:30,000 --> 06:42:32,718
back ethan hunt okay
11224
06:42:32,718 --> 06:42:34,240
if i
11225
06:42:34,240 --> 06:42:36,400
ran and i looked at this and i wanted
11226
06:42:36,400 --> 06:42:38,240
power stats right i could do
11227
06:42:38,240 --> 06:42:40,958
json.powerstats
11228
06:42:40,958 --> 06:42:42,958
and that should get me back
11229
06:42:42,958 --> 06:42:44,878
just the power stats
11230
06:42:44,878 --> 06:42:47,040
and then i believe image should get you
11231
06:42:47,040 --> 06:42:48,160
the image
11232
06:42:48,160 --> 06:42:49,120
yup
11233
06:42:49,120 --> 06:42:51,360
right and then you will have to do dot
11234
06:42:51,360 --> 06:42:54,798
url to get the actual url out of that
11235
06:42:54,798 --> 06:42:56,878
because it's an object within an object
11236
06:42:56,878 --> 06:43:00,160
okay and now we have access to the url
11237
06:43:00,160 --> 06:43:02,400
and if you use an image tag you can
11238
06:43:02,400 --> 06:43:04,558
actually then pop that up on the screen
11239
06:43:04,558 --> 06:43:05,920
there
11240
06:43:05,920 --> 06:43:08,478
so what we'll do in order to attach that
11241
06:43:08,478 --> 06:43:09,520
image
11242
06:43:09,520 --> 06:43:11,680
to like basically your app
11243
06:43:11,680 --> 06:43:13,760
is i'm gonna go ahead and i'm just going
11244
06:43:13,760 --> 06:43:16,718
to say hey get the document
11245
06:43:16,718 --> 06:43:19,200
dot like query selector all right and
11246
06:43:19,200 --> 06:43:21,200
we're going to query select and grab the
11247
06:43:21,200 --> 06:43:23,600
body tag so that's
11248
06:43:23,600 --> 06:43:25,760
this tag right here
11249
06:43:25,760 --> 06:43:28,080
all right i'm going to grab the body tag
11250
06:43:28,080 --> 06:43:31,200
and then to the body tag let's uh let's
11251
06:43:31,200 --> 06:43:32,400
add something so i'm just going to say
11252
06:43:32,400 --> 06:43:35,200
plus equals okay and we're just going to
11253
06:43:35,200 --> 06:43:36,798
write like
11254
06:43:36,798 --> 06:43:38,638
i don't know hi
11255
06:43:38,638 --> 06:43:41,120
or goodbye something like that let's run
11256
06:43:41,120 --> 06:43:43,920
this oh we want to make sure we do dot
11257
06:43:43,920 --> 06:43:46,878
inner html here so let's do dot inner
11258
06:43:46,878 --> 06:43:48,400
html
11259
06:43:48,400 --> 06:43:49,920
let's run this
11260
06:43:49,920 --> 06:43:52,000
and you can see that all we did here is
11261
06:43:52,000 --> 06:43:54,558
we just appended goodbye right there
11262
06:43:54,558 --> 06:43:56,080
okay
11263
06:43:56,080 --> 06:43:57,840
if you don't do plus equals and you just
11264
06:43:57,840 --> 06:43:59,440
do this it will replace everything
11265
06:43:59,440 --> 06:44:00,958
that's inside of the body and replace it
11266
06:44:00,958 --> 06:44:02,638
with goodbye and that's not what we want
11267
06:44:02,638 --> 06:44:05,280
right we just want to like attach that
11268
06:44:05,280 --> 06:44:08,160
so watch this right so this ethan hunt
11269
06:44:08,160 --> 06:44:10,478
that we're getting we have the image
11270
06:44:10,478 --> 06:44:13,360
here so just go ahead and copy that okay
11271
06:44:13,360 --> 06:44:16,000
uh right click copy that
11272
06:44:16,000 --> 06:44:18,798
and if i go const
11273
06:44:18,798 --> 06:44:20,958
image i'll just paste that there it's
11274
06:44:20,958 --> 06:44:22,718
that long url
11275
06:44:22,718 --> 06:44:23,600
okay
11276
06:44:23,600 --> 06:44:25,760
so now what i can do is i can just like
11277
06:44:25,760 --> 06:44:28,958
attach that image to the html body okay
11278
06:44:28,958 --> 06:44:31,280
so what i can say is i can just go and
11279
06:44:31,280 --> 06:44:33,440
go
11280
06:44:34,160 --> 06:44:36,478
i have
11281
06:44:36,478 --> 06:44:39,280
image tag right and then this image tag
11282
06:44:39,280 --> 06:44:40,958
will have a source
11283
06:44:40,958 --> 06:44:43,200
and this source
11284
06:44:43,200 --> 06:44:45,520
is uh gonna be
11285
06:44:45,520 --> 06:44:47,760
the image that we have
11286
06:44:47,760 --> 06:44:50,798
okay let's uh let's make this a template
11287
06:44:50,798 --> 06:44:53,360
literal so we can then write javascript
11288
06:44:53,360 --> 06:44:54,240
in there
11289
06:44:54,240 --> 06:44:55,840
all right boom
11290
06:44:55,840 --> 06:44:58,160
boom boom there we go
11291
06:44:58,160 --> 06:45:00,798
so we have this so image and then just
11292
06:45:00,798 --> 06:45:03,120
like in this like that
11293
06:45:03,120 --> 06:45:06,400
cool so image we gave it a source
11294
06:45:06,400 --> 06:45:08,240
and this is going to be the ethan hunt
11295
06:45:08,240 --> 06:45:10,878
image and it's kind of hard coded here
11296
06:45:10,878 --> 06:45:12,958
all right let's put this maybe in
11297
06:45:12,958 --> 06:45:15,680
strings is that what that wants yeah
11298
06:45:15,680 --> 06:45:17,520
that's kind of weird huh like it wants
11299
06:45:17,520 --> 06:45:20,080
that in strings even though it already
11300
06:45:20,080 --> 06:45:22,798
is in spring so kind of weird but
11301
06:45:22,798 --> 06:45:24,000
whatever
11302
06:45:24,000 --> 06:45:25,360
we can we're going to go ahead and do
11303
06:45:25,360 --> 06:45:26,160
that
11304
06:45:26,160 --> 06:45:29,920
and now you can see that this image is
11305
06:45:29,920 --> 06:45:31,520
popping out and it's actually not ethan
11306
06:45:31,520 --> 06:45:34,478
hunt i apologize it's amazo right but if
11307
06:45:34,478 --> 06:45:36,080
i change this
11308
06:45:36,080 --> 06:45:38,878
uh hero here you know then i could get a
11309
06:45:38,878 --> 06:45:41,120
different image but yeah so if i run
11310
06:45:41,120 --> 06:45:42,558
this i'm just going to get that amazon
11311
06:45:42,558 --> 06:45:44,240
image every single time okay so this is
11312
06:45:44,240 --> 06:45:46,400
apparently what amazo looks like i mean
11313
06:45:46,400 --> 06:45:48,400
i didn't even know that right
11314
06:45:48,400 --> 06:45:50,160
and we're going to give this a height
11315
06:45:50,160 --> 06:45:53,360
and width too so let's go ahead and do
11316
06:45:53,360 --> 06:45:54,798
height is
11317
06:45:54,798 --> 06:45:57,120
200 and then we're going to say width is
11318
06:45:57,120 --> 06:46:00,240
200 now in order to make this dynamic i
11319
06:46:00,240 --> 06:46:02,400
want you to try it because right now
11320
06:46:02,400 --> 06:46:04,320
every time i run this and even if i pass
11321
06:46:04,320 --> 06:46:05,840
in a different hero let's say i put in
11322
06:46:05,840 --> 06:46:08,080
245 and i'm actually putting in ethan
11323
06:46:08,080 --> 06:46:08,878
hunt
11324
06:46:08,878 --> 06:46:10,638
notice that it's still going to give me
11325
06:46:10,638 --> 06:46:13,520
a mazzo so see if you could
11326
06:46:13,520 --> 06:46:16,638
change something here in this function
11327
06:46:16,638 --> 06:46:18,638
where it could update this image
11328
06:46:18,638 --> 06:46:21,040
dynamically okay so give that a try i'll
11329
06:46:21,040 --> 06:46:22,638
give you five seconds to pause the video
11330
06:46:22,638 --> 06:46:24,718
and try it on your own five
11331
06:46:24,718 --> 06:46:28,400
four three two one
11332
06:46:28,400 --> 06:46:30,798
all right let's go ahead and now give
11333
06:46:30,798 --> 06:46:32,798
this a try together
11334
06:46:32,798 --> 06:46:33,760
so
11335
06:46:33,760 --> 06:46:36,798
i am getting back this data but i'm just
11336
06:46:36,798 --> 06:46:38,798
console logging it so instead of console
11337
06:46:38,798 --> 06:46:40,878
logging it what i want to do
11338
06:46:40,878 --> 06:46:43,440
is uh i want to grab the body and i want
11339
06:46:43,440 --> 06:46:45,600
to add this all of this to the body okay
11340
06:46:45,600 --> 06:46:46,958
so this is what i want to do so i'm
11341
06:46:46,958 --> 06:46:48,798
going to cut this because i don't need
11342
06:46:48,798 --> 06:46:50,718
that there i'm going to remove this
11343
06:46:50,718 --> 06:46:51,840
console
11344
06:46:51,840 --> 06:46:53,520
well you can keep that console log for
11345
06:46:53,520 --> 06:46:56,080
now i'll keep that console log
11346
06:46:56,080 --> 06:46:57,120
and
11347
06:46:57,120 --> 06:46:59,360
i'm just gonna say hey uh get me the
11348
06:46:59,360 --> 06:47:02,878
json and then just add that image now
11349
06:47:02,878 --> 06:47:04,638
instead of using this hard-coded image
11350
06:47:04,638 --> 06:47:06,638
what we're basically gonna do
11351
06:47:06,638 --> 06:47:08,638
is uh we're gonna grab
11352
06:47:08,638 --> 06:47:11,120
this image right over here so remember
11353
06:47:11,120 --> 06:47:12,878
we're gonna need to do
11354
06:47:12,878 --> 06:47:14,718
json.image
11355
06:47:14,718 --> 06:47:16,240
and then that'll give us this object
11356
06:47:16,240 --> 06:47:17,440
right here and then we're gonna need to
11357
06:47:17,440 --> 06:47:20,718
do dot url to get access to then this
11358
06:47:20,718 --> 06:47:24,160
url right here that key okay so let's go
11359
06:47:24,160 --> 06:47:29,160
here and we're gonna say json.image.url
11360
06:47:29,440 --> 06:47:31,280
just like this
11361
06:47:31,280 --> 06:47:33,920
so now i can remove that hard-coded
11362
06:47:33,920 --> 06:47:36,240
image that i had let's run this and
11363
06:47:36,240 --> 06:47:39,920
let's see if that works and boom i got
11364
06:47:39,920 --> 06:47:41,920
i got ethan hunt let's run it again
11365
06:47:41,920 --> 06:47:44,718
let's run it again let's go here
11366
06:47:44,718 --> 06:47:46,798
to our html and i don't want to say
11367
06:47:46,798 --> 06:47:48,320
hello world here anymore i want to
11368
06:47:48,320 --> 06:47:50,160
actually say button that just says get
11369
06:47:50,160 --> 06:47:52,478
new hero and we'll make that work later
11370
06:47:52,478 --> 06:47:54,478
and for bonus points you should try to
11371
06:47:54,478 --> 06:47:56,718
make that work yourself okay before we
11372
06:47:56,718 --> 06:47:58,558
even do anything else perfect and i'll
11373
06:47:58,558 --> 06:48:00,878
just add a new break line okay this just
11374
06:48:00,878 --> 06:48:02,240
like breaks
11375
06:48:02,240 --> 06:48:04,400
things on to a new line and then that
11376
06:48:04,400 --> 06:48:06,240
way the get new hero button always stays
11377
06:48:06,240 --> 06:48:09,280
at the top so let's get back to our
11378
06:48:09,280 --> 06:48:10,478
script
11379
06:48:10,478 --> 06:48:13,120
and um watch what happens if i pass in
11380
06:48:13,120 --> 06:48:15,440
20 and run it now it's going to get me
11381
06:48:15,440 --> 06:48:16,878
amaros
11382
06:48:16,878 --> 06:48:19,200
image if i run 40 and run it it's going
11383
06:48:19,200 --> 06:48:20,558
to get me
11384
06:48:20,558 --> 06:48:23,040
this guy whoever that is
11385
06:48:23,040 --> 06:48:25,520
uh let's hit 60
11386
06:48:25,520 --> 06:48:27,760
and it's running me that looks like bane
11387
06:48:27,760 --> 06:48:29,760
is that is that bane
11388
06:48:29,760 --> 06:48:31,600
he looks epic
11389
06:48:31,600 --> 06:48:34,160
yeah that's bane that's sick
11390
06:48:34,160 --> 06:48:36,240
and his relative is apparently king
11391
06:48:36,240 --> 06:48:39,040
snake who's his father yo i didn't know
11392
06:48:39,040 --> 06:48:40,718
that
11393
06:48:40,718 --> 06:48:43,680
that's lit that is super cool
11394
06:48:43,680 --> 06:48:45,920
man ben is a badass
11395
06:48:45,920 --> 06:48:46,718
now
11396
06:48:46,718 --> 06:48:48,718
see if you can make this get new hero
11397
06:48:48,718 --> 06:48:51,200
button works every single time i click
11398
06:48:51,200 --> 06:48:53,600
it i want to be able to get a new hero
11399
06:48:53,600 --> 06:48:55,440
every single time so i want you to pause
11400
06:48:55,440 --> 06:48:58,320
this video give this a try on your own
11401
06:48:58,320 --> 06:49:00,958
five four three
11402
06:49:00,958 --> 06:49:01,760
two
11403
06:49:01,760 --> 06:49:02,798
one
11404
06:49:02,798 --> 06:49:05,120
all right let's go ahead and try to do
11405
06:49:05,120 --> 06:49:07,200
this together it's all right if you
11406
06:49:07,200 --> 06:49:09,600
didn't get this i'm proud of you for
11407
06:49:09,600 --> 06:49:12,080
trying and i hope you tried
11408
06:49:12,080 --> 06:49:13,760
um because you'd only be cheating
11409
06:49:13,760 --> 06:49:15,360
yourself if you didn't
11410
06:49:15,360 --> 06:49:17,600
all right so
11411
06:49:17,600 --> 06:49:18,798
uh let's
11412
06:49:18,798 --> 06:49:20,400
let's do basically what we did for that
11413
06:49:20,400 --> 06:49:23,360
dog app right so we have our button
11414
06:49:23,360 --> 06:49:25,760
and what we want to do is
11415
06:49:25,760 --> 06:49:28,400
let's go give that button an id well we
11416
06:49:28,400 --> 06:49:30,400
only have one button so i can be kind of
11417
06:49:30,400 --> 06:49:32,240
lazy here and i can just select this
11418
06:49:32,240 --> 06:49:33,280
button
11419
06:49:33,280 --> 06:49:35,440
i can query select this button but is
11420
06:49:35,440 --> 06:49:38,478
that a good way of doing it not really
11421
06:49:38,478 --> 06:49:40,160
right like that's
11422
06:49:40,160 --> 06:49:41,280
no i don't want to give you guys bad
11423
06:49:41,280 --> 06:49:43,280
habits let's just say id
11424
06:49:43,280 --> 06:49:44,638
new hero
11425
06:49:44,638 --> 06:49:46,638
button let's just say that
11426
06:49:46,638 --> 06:49:48,798
let's go into our script
11427
06:49:48,798 --> 06:49:50,240
and we're going to get access button
11428
06:49:50,240 --> 06:49:52,878
kind of up top here const
11429
06:49:52,878 --> 06:49:54,958
hero button
11430
06:49:54,958 --> 06:49:57,360
new hero button
11431
06:49:57,360 --> 06:49:59,200
and i'm going to say document dot
11432
06:49:59,200 --> 06:50:02,478
elements by id paste in that new hero
11433
06:50:02,478 --> 06:50:04,718
button and so whenever that button runs
11434
06:50:04,718 --> 06:50:06,638
i want to run this function here so i'm
11435
06:50:06,638 --> 06:50:09,040
going to say console.log
11436
06:50:09,040 --> 06:50:11,040
new hero button
11437
06:50:11,040 --> 06:50:14,878
and i'm going to say on click run me
11438
06:50:14,878 --> 06:50:17,200
this thing gets superhero
11439
06:50:17,200 --> 06:50:19,600
and that's it so now when i run this app
11440
06:50:19,600 --> 06:50:21,120
everything should be empty when i hit
11441
06:50:21,120 --> 06:50:23,200
get new hero it should run but every
11442
06:50:23,200 --> 06:50:25,040
single time i run it it's going to keep
11443
06:50:25,040 --> 06:50:26,638
making a new request but it's going to
11444
06:50:26,638 --> 06:50:28,958
get me the same hero every single time
11445
06:50:28,958 --> 06:50:30,638
so getting your hero get new hero get
11446
06:50:30,638 --> 06:50:32,558
new hero get new hero it's getting the
11447
06:50:32,558 --> 06:50:35,840
same exact hero but every single time
11448
06:50:35,840 --> 06:50:37,360
and that's not what we want we want a
11449
06:50:37,360 --> 06:50:39,600
different hero every single time and so
11450
06:50:39,600 --> 06:50:41,200
now i'm going to give you another piece
11451
06:50:41,200 --> 06:50:42,798
of this exercise okay
11452
06:50:42,798 --> 06:50:45,280
try this on your own so see if you can
11453
06:50:45,280 --> 06:50:46,478
make it
11454
06:50:46,478 --> 06:50:47,760
where
11455
06:50:47,760 --> 06:50:48,520
it
11456
06:50:48,520 --> 06:50:51,920
randomly picks a hero okay remember that
11457
06:50:51,920 --> 06:50:53,520
array exercise that we did where we
11458
06:50:53,520 --> 06:50:56,320
picked random remember the when we were
11459
06:50:56,320 --> 06:51:00,000
making that color app right uh remember
11460
06:51:00,000 --> 06:51:01,680
or sorry fruits app and we picked a
11461
06:51:01,680 --> 06:51:03,600
random fruit remember when we did rock
11462
06:51:03,600 --> 06:51:06,160
paper scissors and the computer chooses
11463
06:51:06,160 --> 06:51:08,798
rock paper scissors randomly all right
11464
06:51:08,798 --> 06:51:09,520
so
11465
06:51:09,520 --> 06:51:11,520
you're gonna need to use some of that
11466
06:51:11,520 --> 06:51:13,680
knowledge to pull this off this one's
11467
06:51:13,680 --> 06:51:15,760
gonna be a little tricky but i think if
11468
06:51:15,760 --> 06:51:18,400
you really tried you could make this
11469
06:51:18,400 --> 06:51:20,400
number where it's random now if you need
11470
06:51:20,400 --> 06:51:21,600
a hint if you don't need a hint you
11471
06:51:21,600 --> 06:51:23,040
could pause this video try it on your
11472
06:51:23,040 --> 06:51:24,878
right now if you need a hint what i'll
11473
06:51:24,878 --> 06:51:27,600
tell you is this number is not going to
11474
06:51:27,600 --> 06:51:29,600
be between three like it is for rock
11475
06:51:29,600 --> 06:51:32,000
papers and scissors this number is going
11476
06:51:32,000 --> 06:51:34,000
to be between one
11477
06:51:34,000 --> 06:51:35,280
and
11478
06:51:35,280 --> 06:51:38,878
all the way till 731 because that's how
11479
06:51:38,878 --> 06:51:40,878
many heroes they have so you're going to
11480
06:51:40,878 --> 06:51:44,240
need to put a number between 1 and 731
11481
06:51:44,240 --> 06:51:47,280
uh in here so it should either go 1 or
11482
06:51:47,280 --> 06:51:51,360
731 or some number in between but never
11483
06:51:51,360 --> 06:51:54,718
lower than one and never higher than 731
11484
06:51:54,718 --> 06:51:57,280
okay so you need to make a function that
11485
06:51:57,280 --> 06:51:58,558
can do that
11486
06:51:58,558 --> 06:52:00,160
all right so take five seconds right now
11487
06:52:00,160 --> 06:52:03,920
i'll give you five four three two
11488
06:52:03,920 --> 06:52:07,200
one all right so we're gonna do this now
11489
06:52:07,200 --> 06:52:08,478
const
11490
06:52:08,478 --> 06:52:09,280
uh
11491
06:52:09,280 --> 06:52:10,798
random
11492
06:52:10,798 --> 06:52:12,558
uh i'm gonna create a function called
11493
06:52:12,558 --> 06:52:13,920
random hero
11494
06:52:13,920 --> 06:52:15,600
and basically what this function is
11495
06:52:15,600 --> 06:52:16,958
going to do
11496
06:52:16,958 --> 06:52:18,080
is
11497
06:52:18,080 --> 06:52:22,240
we know that there are 731 heroes okay
11498
06:52:22,240 --> 06:52:25,958
so khan's number of heroes is
11499
06:52:25,958 --> 06:52:29,200
731 and what i'll basically want to do
11500
06:52:29,200 --> 06:52:32,400
is i want to do math.random okay so
11501
06:52:32,400 --> 06:52:34,478
let's also open this up in the shell or
11502
06:52:34,478 --> 06:52:37,200
sorry in our console and give this a try
11503
06:52:37,200 --> 06:52:39,200
all right so let's go here and actually
11504
06:52:39,200 --> 06:52:41,920
i'll clear this up so you can see this
11505
06:52:41,920 --> 06:52:43,600
nice and at the top
11506
06:52:43,600 --> 06:52:44,718
so
11507
06:52:44,718 --> 06:52:46,000
let's say
11508
06:52:46,000 --> 06:52:48,160
i do math.random
11509
06:52:48,160 --> 06:52:51,200
and we multiply this by 731. now notice
11510
06:52:51,200 --> 06:52:54,000
that i always get a number between 0 and
11511
06:52:54,000 --> 06:52:56,718
731 but it'll never actually go up to
11512
06:52:56,718 --> 06:53:01,120
731 right so that's maybe not not what
11513
06:53:01,120 --> 06:53:03,680
we want we want it to actually go up to
11514
06:53:03,680 --> 06:53:06,400
731 so there are a few hacks you could
11515
06:53:06,400 --> 06:53:07,600
do here
11516
06:53:07,600 --> 06:53:09,280
so if we want to do so first of all
11517
06:53:09,280 --> 06:53:11,120
let's do math.floor
11518
06:53:11,120 --> 06:53:14,080
so math.floor will and if i hit up and
11519
06:53:14,080 --> 06:53:15,840
enter up and enter it's going to keep
11520
06:53:15,840 --> 06:53:18,638
going but it'll never actually pick 731
11521
06:53:18,638 --> 06:53:20,400
and it it's not going to be easy for me
11522
06:53:20,400 --> 06:53:23,360
to prove that to you here but it's not
11523
06:53:23,360 --> 06:53:26,638
and it's also able to pick zero and
11524
06:53:26,638 --> 06:53:28,798
that's not what we want in order and to
11525
06:53:28,798 --> 06:53:30,478
prove this to you guys what i can do is
11526
06:53:30,478 --> 06:53:32,638
like copy this piece of code
11527
06:53:32,638 --> 06:53:34,798
and just change it to a smaller like
11528
06:53:34,798 --> 06:53:36,798
number so you can kind of see this
11529
06:53:36,798 --> 06:53:38,400
so see how it goes zero and one but
11530
06:53:38,400 --> 06:53:40,320
it'll never pick two so it's basically
11531
06:53:40,320 --> 06:53:42,478
picking the lower bounds we want to
11532
06:53:42,478 --> 06:53:45,440
actually pick the upper bounds one start
11533
06:53:45,440 --> 06:53:46,558
with one
11534
06:53:46,558 --> 06:53:49,360
and end at 7 31. so
11535
06:53:49,360 --> 06:53:51,200
the way that you could do this is you
11536
06:53:51,200 --> 06:53:53,600
could actually just add a plus one to
11537
06:53:53,600 --> 06:53:55,600
all of these so this way when i run it
11538
06:53:55,600 --> 06:53:57,760
every single time i'm gonna get a one or
11539
06:53:57,760 --> 06:53:59,520
a two every single time so you could
11540
06:53:59,520 --> 06:54:02,400
just add a one or instead of math.floor
11541
06:54:02,400 --> 06:54:05,120
you could actually call math.seal so
11542
06:54:05,120 --> 06:54:07,200
either of those ways work so we're gonna
11543
06:54:07,200 --> 06:54:09,600
do the same thing with our function with
11544
06:54:09,600 --> 06:54:13,040
731 and just add a one at the end and
11545
06:54:13,040 --> 06:54:14,958
this is always gonna ensure that you're
11546
06:54:14,958 --> 06:54:16,878
getting a number that's greater than
11547
06:54:16,878 --> 06:54:19,120
zero and it doesn't end at seven thirty
11548
06:54:19,120 --> 06:54:21,360
it would unit actually seven thirty one
11549
06:54:21,360 --> 06:54:23,440
okay so this will get us
11550
06:54:23,440 --> 06:54:24,798
that so i'm gonna
11551
06:54:24,798 --> 06:54:28,478
paste this here okay now the 731 is
11552
06:54:28,478 --> 06:54:31,200
actually just you know number of heroes
11553
06:54:31,200 --> 06:54:33,120
number of heroes and then we're gonna
11554
06:54:33,120 --> 06:54:35,280
plus one this cool
11555
06:54:35,280 --> 06:54:37,920
and we're gonna say
11556
06:54:37,920 --> 06:54:40,400
and actually we can just return this
11557
06:54:40,400 --> 06:54:41,680
that's it
11558
06:54:41,680 --> 06:54:43,280
okay and then we can just call the
11559
06:54:43,280 --> 06:54:45,440
random hero function
11560
06:54:45,440 --> 06:54:48,558
here so i can just say a random hero
11561
06:54:48,558 --> 06:54:51,440
like that so get superhero and we're
11562
06:54:51,440 --> 06:54:53,600
gonna get a random
11563
06:54:53,600 --> 06:54:56,400
random hero okay you could because this
11564
06:54:56,400 --> 06:54:58,638
function here is just going to return a
11565
06:54:58,638 --> 06:55:01,680
number so for example if i
11566
06:55:01,680 --> 06:55:04,958
do this and we do random hero and
11567
06:55:04,958 --> 06:55:07,120
press up and enter it says run random
11568
06:55:07,120 --> 06:55:09,200
here is not defined interesting
11569
06:55:09,200 --> 06:55:11,280
but i just defined it
11570
06:55:11,280 --> 06:55:14,000
so let's try random hero now boom there
11571
06:55:14,000 --> 06:55:16,478
we go so every time i run random hero
11572
06:55:16,478 --> 06:55:18,320
you can see what it does right it gets
11573
06:55:18,320 --> 06:55:19,120
me
11574
06:55:19,120 --> 06:55:22,558
a different number every single time and
11575
06:55:22,558 --> 06:55:23,920
does a pretty good job right of
11576
06:55:23,920 --> 06:55:26,240
generating a random hero so that's why
11577
06:55:26,240 --> 06:55:27,840
we're just
11578
06:55:27,840 --> 06:55:30,160
running this random hero in here they'll
11579
06:55:30,160 --> 06:55:31,680
generate a random hero and then we'll
11580
06:55:31,680 --> 06:55:33,920
get that specific superhero and then
11581
06:55:33,920 --> 06:55:36,000
we'll get that dude's image so let's hit
11582
06:55:36,000 --> 06:55:38,240
run at the top let's go ahead and click
11583
06:55:38,240 --> 06:55:42,080
get new hero get new hero and
11584
06:55:42,080 --> 06:55:44,240
all right so we've had a little bit of
11585
06:55:44,240 --> 06:55:45,360
hacky
11586
06:55:45,360 --> 06:55:47,280
html here that might be causing some
11587
06:55:47,280 --> 06:55:49,200
problems so let's go to our html file
11588
06:55:49,200 --> 06:55:52,558
real quick and let's just
11589
06:55:52,558 --> 06:55:54,400
uh update this
11590
06:55:54,400 --> 06:55:57,120
real quick okay so what i'm gonna say
11591
06:55:57,120 --> 06:56:00,160
is um i'm gonna put a div here
11592
06:56:00,160 --> 06:56:03,680
and we're just gonna say id is hero
11593
06:56:03,680 --> 06:56:04,558
image
11594
06:56:04,558 --> 06:56:07,280
and then inside of here we'll just have
11595
06:56:07,280 --> 06:56:09,200
like an empty image thing okay and then
11596
06:56:09,200 --> 06:56:10,320
we'll just
11597
06:56:10,320 --> 06:56:13,600
update that instead so here we're gonna
11598
06:56:13,600 --> 06:56:14,958
say
11599
06:56:14,958 --> 06:56:16,080
const
11600
06:56:16,080 --> 06:56:18,878
hero image div
11601
06:56:18,878 --> 06:56:20,160
all right and then i'm going to say
11602
06:56:20,160 --> 06:56:23,920
document dot get element by id
11603
06:56:23,920 --> 06:56:26,160
and i think it's called hero image right
11604
06:56:26,160 --> 06:56:27,840
let's just double check
11605
06:56:27,840 --> 06:56:29,360
hero image
11606
06:56:29,360 --> 06:56:31,280
copy come back
11607
06:56:31,280 --> 06:56:32,478
paste it
11608
06:56:32,478 --> 06:56:33,760
perfect
11609
06:56:33,760 --> 06:56:36,000
and then we're just going to do
11610
06:56:36,000 --> 06:56:37,760
uh we're going to replace this part
11611
06:56:37,760 --> 06:56:39,040
right here
11612
06:56:39,040 --> 06:56:40,798
with hero image div and we're going to
11613
06:56:40,798 --> 06:56:42,878
change that html to this image so now
11614
06:56:42,878 --> 06:56:44,400
let's give this a try and let's see if
11615
06:56:44,400 --> 06:56:46,798
this is still working hit run at the top
11616
06:56:46,798 --> 06:56:48,400
hit get new hero
11617
06:56:48,400 --> 06:56:51,760
and let's try it and it works okay so
11618
06:56:51,760 --> 06:56:53,520
i'm getting new heroes
11619
06:56:53,520 --> 06:56:55,200
and look at what's happening their
11620
06:56:55,200 --> 06:56:56,718
images are actually showing up at the
11621
06:56:56,718 --> 06:56:58,958
bottom hit get new hero again
11622
06:56:58,958 --> 06:57:01,440
and so every time i hit get new hero it
11623
06:57:01,440 --> 06:57:04,080
actually like gives me a new hero so
11624
06:57:04,080 --> 06:57:06,958
that's great and why is this happening
11625
06:57:06,958 --> 06:57:08,878
so let's go ahead and open this app up
11626
06:57:08,878 --> 06:57:10,478
and let's try to figure out why is this
11627
06:57:10,478 --> 06:57:12,240
happening why is this behaving instead
11628
06:57:12,240 --> 06:57:14,558
of replacing the hero it keeps adding
11629
06:57:14,558 --> 06:57:16,080
them so get new hero
11630
06:57:16,080 --> 06:57:17,440
get new hero
11631
06:57:17,440 --> 06:57:18,958
i mean this is kind of a cool
11632
06:57:18,958 --> 06:57:20,558
functionality too actually i kind of
11633
06:57:20,558 --> 06:57:21,920
like this
11634
06:57:21,920 --> 06:57:24,400
uh i kind of like this uh version of the
11635
06:57:24,400 --> 06:57:26,400
app right every time you hit get new
11636
06:57:26,400 --> 06:57:29,440
hero you keep getting back
11637
06:57:29,440 --> 06:57:31,920
somebody somebody new so it's actually
11638
06:57:31,920 --> 06:57:34,638
super super super cool i actually
11639
06:57:34,638 --> 06:57:36,958
i really like this functionality but
11640
06:57:36,958 --> 06:57:38,878
let's say you want to change this
11641
06:57:38,878 --> 06:57:40,958
let's say you only want one hero well
11642
06:57:40,958 --> 06:57:42,478
what you could do instead of this plus
11643
06:57:42,478 --> 06:57:45,120
equals here which appends you could just
11644
06:57:45,120 --> 06:57:47,520
say equal and that's only just going to
11645
06:57:47,520 --> 06:57:49,440
replace so run this again hit get new
11646
06:57:49,440 --> 06:57:51,520
hero he'd get new hero and you're going
11647
06:57:51,520 --> 06:57:52,500
to get a new hero
11648
06:57:52,500 --> 06:57:53,600
[Music]
11649
06:57:53,600 --> 06:57:54,958
every single time
11650
06:57:54,958 --> 06:57:57,680
okay or you know not every single time
11651
06:57:57,680 --> 06:58:00,320
there is a one out of 731 chance that
11652
06:58:00,320 --> 06:58:01,680
you're going to get the same person
11653
06:58:01,680 --> 06:58:03,680
again probably lower than that
11654
06:58:03,680 --> 06:58:06,080
but yeah it's a pretty low chance this
11655
06:58:06,080 --> 06:58:07,920
is going to repeat itself all right now
11656
06:58:07,920 --> 06:58:10,478
what we want to do is we want to have
11657
06:58:10,478 --> 06:58:12,798
a search field here
11658
06:58:12,798 --> 06:58:13,760
and
11659
06:58:13,760 --> 06:58:17,120
you should type in there and then
11660
06:58:17,120 --> 06:58:19,120
um you should have a get new hero button
11661
06:58:19,120 --> 06:58:20,558
right next to it so you should be able
11662
06:58:20,558 --> 06:58:22,160
to type in what hero you're looking for
11663
06:58:22,160 --> 06:58:24,478
and then say get hero and it should
11664
06:58:24,478 --> 06:58:27,280
surge that hero okay so we should have
11665
06:58:27,280 --> 06:58:30,320
like kind of two buttons so one button
11666
06:58:30,320 --> 06:58:33,120
gets a random hero okay and i guess
11667
06:58:33,120 --> 06:58:34,878
let's just give this a name of random
11668
06:58:34,878 --> 06:58:36,240
hero so
11669
06:58:36,240 --> 06:58:38,718
uh let's go to our button and here i'm
11670
06:58:38,718 --> 06:58:40,558
just gonna say like
11671
06:58:40,558 --> 06:58:42,638
random and so when you click it it's
11672
06:58:42,638 --> 06:58:43,840
very clear that this is gonna get a
11673
06:58:43,840 --> 06:58:46,320
random hero okay and then what we're
11674
06:58:46,320 --> 06:58:47,840
gonna do is we're gonna have an input
11675
06:58:47,840 --> 06:58:49,680
which you you know is gonna be your
11676
06:58:49,680 --> 06:58:51,200
search button all right so let's go
11677
06:58:51,200 --> 06:58:52,638
ahead and create that input now i'm
11678
06:58:52,638 --> 06:58:54,000
gonna say input
11679
06:58:54,000 --> 06:58:56,400
and let's run our app and we should have
11680
06:58:56,400 --> 06:58:58,160
an input right here okay and then we're
11681
06:58:58,160 --> 06:58:59,200
gonna have a
11682
06:58:59,200 --> 06:59:01,280
button kind of like in front another
11683
06:59:01,280 --> 06:59:02,798
button in front of that input and this
11684
06:59:02,798 --> 06:59:04,638
is going to be search
11685
06:59:04,638 --> 06:59:06,798
okay let's run this so now our app is
11686
06:59:06,798 --> 06:59:09,200
going to have two things so random hero
11687
06:59:09,200 --> 06:59:10,638
will get you a random here every single
11688
06:59:10,638 --> 06:59:11,520
time
11689
06:59:11,520 --> 06:59:14,000
or you could type in a name of some hero
11690
06:59:14,000 --> 06:59:16,558
like batman and hit search
11691
06:59:16,558 --> 06:59:18,718
great so let's go back
11692
06:59:18,718 --> 06:59:22,000
to our app let's go to script.js
11693
06:59:22,000 --> 06:59:25,600
and let's work on this search
11694
06:59:25,600 --> 06:59:27,520
get you know basically let's get a hero
11695
06:59:27,520 --> 06:59:30,000
that was actually just searched
11696
06:59:30,000 --> 06:59:31,520
so i'm going to create a new function
11697
06:59:31,520 --> 06:59:33,520
well do i need to create a new function
11698
06:59:33,520 --> 06:59:35,558
let me actually think about it because
11699
06:59:35,558 --> 06:59:37,120
getsuperhero a bunch of the
11700
06:59:37,120 --> 06:59:39,280
functionality is going to be same just
11701
06:59:39,280 --> 06:59:40,160
this
11702
06:59:40,160 --> 06:59:41,920
uh this is going to change right so for
11703
06:59:41,920 --> 06:59:44,080
example if you search a hero
11704
06:59:44,080 --> 06:59:45,520
then it'll go
11705
06:59:45,520 --> 06:59:47,520
base url instead of id we're going to
11706
06:59:47,520 --> 06:59:51,360
actually do search.name of the hero
11707
06:59:51,360 --> 06:59:54,160
and if you pass in an id
11708
06:59:54,160 --> 06:59:56,958
okay so if they pass in a
11709
06:59:56,958 --> 06:59:58,000
name
11710
06:59:58,000 --> 07:00:00,400
then this is going to be the url
11711
07:00:00,400 --> 07:00:01,280
like
11712
07:00:01,280 --> 07:00:02,718
schematic or whatever right this is what
11713
07:00:02,718 --> 07:00:04,400
the url is going to look like and to
11714
07:00:04,400 --> 07:00:05,920
pass in an id
11715
07:00:05,920 --> 07:00:08,400
the url is going to look like base url
11716
07:00:08,400 --> 07:00:12,558
slash id okay and let's uh try that url
11717
07:00:12,558 --> 07:00:14,000
so let's go
11718
07:00:14,000 --> 07:00:18,400
grab that url real quick so i'll go here
11719
07:00:18,400 --> 07:00:21,440
grab this whole thing copy it hit paste
11720
07:00:21,440 --> 07:00:25,040
grab your token go back to that
11721
07:00:25,040 --> 07:00:26,478
paste this
11722
07:00:26,478 --> 07:00:28,478
token right here
11723
07:00:28,478 --> 07:00:31,440
and now we're gonna do slash search
11724
07:00:31,440 --> 07:00:33,920
slash batman and let's see if this works
11725
07:00:33,920 --> 07:00:37,200
as i expected or if it's gonna break
11726
07:00:37,200 --> 07:00:38,958
and it actually does work wow look at
11727
07:00:38,958 --> 07:00:41,760
that let's try spider
11728
07:00:41,760 --> 07:00:44,240
and yeah spider shows up let's try
11729
07:00:44,240 --> 07:00:46,558
spiderman let's see if that shows up
11730
07:00:46,558 --> 07:00:48,080
that's the wrong name so let's try
11731
07:00:48,080 --> 07:00:50,080
spider dash man
11732
07:00:50,080 --> 07:00:53,440
and spider dash man indeed shows up now
11733
07:00:53,440 --> 07:00:55,680
one thing if you have noticed is you're
11734
07:00:55,680 --> 07:00:58,000
gonna get back of basically the same
11735
07:00:58,000 --> 07:01:00,400
response regardless of
11736
07:01:00,400 --> 07:01:03,440
so you know whether you searched the you
11737
07:01:03,440 --> 07:01:05,760
you searched it up like
11738
07:01:05,760 --> 07:01:07,920
with the search spider-man
11739
07:01:07,920 --> 07:01:10,638
or whether you typed in the id of
11740
07:01:10,638 --> 07:01:13,120
spider-man which is i can see here it
11741
07:01:13,120 --> 07:01:16,080
says 620 so let's go ahead and replace
11742
07:01:16,080 --> 07:01:17,680
this here with
11743
07:01:17,680 --> 07:01:19,680
uh 620.
11744
07:01:19,680 --> 07:01:21,440
both of them are actually going to
11745
07:01:21,440 --> 07:01:23,200
return to you
11746
07:01:23,200 --> 07:01:27,280
pretty much the same exact
11747
07:01:27,280 --> 07:01:29,440
response right like the response is very
11748
07:01:29,440 --> 07:01:31,200
very close
11749
07:01:31,200 --> 07:01:33,920
the only thing i see change here though
11750
07:01:33,920 --> 07:01:36,718
is we have a results array like we have
11751
07:01:36,718 --> 07:01:39,360
a results uh key here
11752
07:01:39,360 --> 07:01:41,920
right and then the value of that is an
11753
07:01:41,920 --> 07:01:43,120
array
11754
07:01:43,120 --> 07:01:45,200
so basically it seems like it gets you
11755
07:01:45,200 --> 07:01:47,680
multiple results for spider-man
11756
07:01:47,680 --> 07:01:50,400
um one or multiple results and so
11757
07:01:50,400 --> 07:01:52,080
basically it seems like we would want to
11758
07:01:52,080 --> 07:01:53,040
pull
11759
07:01:53,040 --> 07:01:54,798
basically i think we would be safe just
11760
07:01:54,798 --> 07:01:56,958
pulling the first result that's the
11761
07:01:56,958 --> 07:01:58,478
difference between this and if i
11762
07:01:58,478 --> 07:02:00,478
actually just type in 620 because that
11763
07:02:00,478 --> 07:02:02,878
is getting me that specific spider-man
11764
07:02:02,878 --> 07:02:04,878
that i was looking for
11765
07:02:04,878 --> 07:02:06,718
whereas for this
11766
07:02:06,718 --> 07:02:08,798
it's getting me a array of results and
11767
07:02:08,798 --> 07:02:10,958
then the first result has the specific
11768
07:02:10,958 --> 07:02:12,878
spider-man that you might be looking for
11769
07:02:12,878 --> 07:02:14,320
okay so we're just gonna get like the
11770
07:02:14,320 --> 07:02:15,200
first
11771
07:02:15,200 --> 07:02:17,280
item out of that array that's how we're
11772
07:02:17,280 --> 07:02:19,680
gonna do it so let's go back so some of
11773
07:02:19,680 --> 07:02:22,320
our code is going to change not all of
11774
07:02:22,320 --> 07:02:25,280
it but just a little amount of our code
11775
07:02:25,280 --> 07:02:26,718
meaning
11776
07:02:26,718 --> 07:02:28,718
in one of the scenarios
11777
07:02:28,718 --> 07:02:31,040
so normally we just do json and we're
11778
07:02:31,040 --> 07:02:33,520
good but in this case we might have to
11779
07:02:33,520 --> 07:02:35,360
do
11780
07:02:35,360 --> 07:02:37,000
dot json.rt
11781
07:02:37,440 --> 07:02:39,760
get me the zeroth result
11782
07:02:39,760 --> 07:02:40,718
right
11783
07:02:40,718 --> 07:02:42,320
and
11784
07:02:42,320 --> 07:02:43,680
then
11785
07:02:43,680 --> 07:02:47,760
then i could do dot image dot url
11786
07:02:47,760 --> 07:02:49,920
unlike how it is currently where you
11787
07:02:49,920 --> 07:02:53,558
could just do json.image.url
11788
07:02:54,080 --> 07:02:56,240
okay so let's maybe let's put these
11789
07:02:56,240 --> 07:02:59,440
examples kind of this so you can see
11790
07:02:59,440 --> 07:03:00,718
right so
11791
07:03:00,718 --> 07:03:03,360
with this method i can just go like this
11792
07:03:03,360 --> 07:03:05,120
because i already know exactly which
11793
07:03:05,120 --> 07:03:07,200
json that i'm getting here i'm getting
11794
07:03:07,200 --> 07:03:08,958
multiple results so i want to pull the
11795
07:03:08,958 --> 07:03:11,840
first result get that results image url
11796
07:03:11,840 --> 07:03:12,638
so
11797
07:03:12,638 --> 07:03:15,440
a slight change is going to happen
11798
07:03:15,440 --> 07:03:17,600
whether is going to happen
11799
07:03:17,600 --> 07:03:22,478
whether we're given um a id or a name
11800
07:03:22,478 --> 07:03:25,200
so in that case for simplicity's sake i
11801
07:03:25,200 --> 07:03:27,040
think what i'm going to decide to do for
11802
07:03:27,040 --> 07:03:29,840
now is just create a different function
11803
07:03:29,840 --> 07:03:31,920
that way we won't have to do all this if
11804
07:03:31,920 --> 07:03:34,320
else conditioned stuff we can just have
11805
07:03:34,320 --> 07:03:37,040
a different function okay so this
11806
07:03:37,040 --> 07:03:39,760
function here let's rename it and this
11807
07:03:39,760 --> 07:03:42,558
should be get random superhero
11808
07:03:42,558 --> 07:03:44,638
that function we run and it essentially
11809
07:03:44,638 --> 07:03:47,360
will get a random superhero
11810
07:03:47,360 --> 07:03:48,878
and um
11811
07:03:48,878 --> 07:03:50,320
yeah and if it's gonna get a random
11812
07:03:50,320 --> 07:03:52,240
superhero i mean
11813
07:03:52,240 --> 07:03:54,798
you you really don't even need to give
11814
07:03:54,798 --> 07:03:57,120
it an id so
11815
07:03:57,120 --> 07:03:59,120
yeah so yeah you could call it get
11816
07:03:59,120 --> 07:04:00,798
random super get superhero i'll just
11817
07:04:00,798 --> 07:04:02,160
leave it as
11818
07:04:02,160 --> 07:04:05,040
uh let's go ahead command z this
11819
07:04:05,040 --> 07:04:06,878
let's just leave it as get superhero for
11820
07:04:06,878 --> 07:04:08,320
now just because i don't want you to
11821
07:04:08,320 --> 07:04:09,840
like have to change it everywhere like
11822
07:04:09,840 --> 07:04:11,120
for now you should just have a working
11823
07:04:11,120 --> 07:04:13,760
version so let's leave that as is so
11824
07:04:13,760 --> 07:04:15,280
this is one function now let's make
11825
07:04:15,280 --> 07:04:16,958
another function and this is going to be
11826
07:04:16,958 --> 07:04:18,000
called
11827
07:04:18,000 --> 07:04:21,280
get searched get search superhero okay
11828
07:04:21,280 --> 07:04:24,160
so this is whatever the user searched up
11829
07:04:24,160 --> 07:04:25,840
and this is gonna it's gonna probably
11830
07:04:25,840 --> 07:04:27,920
you know whatever the name the user is
11831
07:04:27,920 --> 07:04:29,360
searching for right
11832
07:04:29,360 --> 07:04:31,280
now we're gonna be able to copy a bunch
11833
07:04:31,280 --> 07:04:33,040
of stuff from here
11834
07:04:33,040 --> 07:04:34,878
okay so let's go back
11835
07:04:34,878 --> 07:04:37,040
for fetch request here
11836
07:04:37,040 --> 07:04:38,878
we're gonna have a
11837
07:04:38,878 --> 07:04:41,040
base url of course
11838
07:04:41,040 --> 07:04:42,638
but on the top of the base rail we're
11839
07:04:42,638 --> 07:04:44,080
going to say search
11840
07:04:44,080 --> 07:04:46,160
and we're going to pass in the name
11841
07:04:46,160 --> 07:04:49,200
so this is going to get us the
11842
07:04:49,200 --> 07:04:51,280
you know like put in batman or whatever
11843
07:04:51,280 --> 07:04:52,160
here
11844
07:04:52,160 --> 07:04:53,638
and then we're going to do our
11845
07:04:53,638 --> 07:04:56,638
dens.dense like how we did earlier
11846
07:04:56,638 --> 07:04:58,000
and
11847
07:04:58,000 --> 07:04:59,200
let's just
11848
07:04:59,200 --> 07:05:02,320
go right here and paste them format this
11849
07:05:02,320 --> 07:05:03,280
cool
11850
07:05:03,280 --> 07:05:07,520
you do our dense.dense and for json here
11851
07:05:07,520 --> 07:05:10,400
we're actually gonna grab the
11852
07:05:10,400 --> 07:05:14,160
we're gonna say const super uh hero
11853
07:05:14,160 --> 07:05:17,680
is equal to json.results
11854
07:05:17,680 --> 07:05:19,040
of zero
11855
07:05:19,040 --> 07:05:20,958
this is gonna be the specific hero and
11856
07:05:20,958 --> 07:05:21,840
then
11857
07:05:21,840 --> 07:05:25,040
we can replace that with hero.image.url
11858
07:05:25,040 --> 07:05:28,558
and uh instead of console.logging json
11859
07:05:28,558 --> 07:05:32,718
we're going to console.log our hero
11860
07:05:32,718 --> 07:05:35,440
and this search superhero function is
11861
07:05:35,440 --> 07:05:36,878
going to run
11862
07:05:36,878 --> 07:05:39,120
whenever we click the search button okay
11863
07:05:39,120 --> 07:05:41,360
so let's go ahead and let's see the
11864
07:05:41,360 --> 07:05:43,280
search button dude we give it an id
11865
07:05:43,280 --> 07:05:46,400
let's give it an id of search button
11866
07:05:46,400 --> 07:05:48,718
let's go back into our code
11867
07:05:48,718 --> 07:05:50,160
and uh
11868
07:05:50,160 --> 07:05:51,680
let's say
11869
07:05:51,680 --> 07:05:52,638
search
11870
07:05:52,638 --> 07:05:55,320
button is going to be
11871
07:05:55,320 --> 07:05:56,878
document.getelementbyid and we're going
11872
07:05:56,878 --> 07:05:59,680
to say search button and uh then we're
11873
07:05:59,680 --> 07:06:01,520
just simply just going to attach
11874
07:06:01,520 --> 07:06:02,798
a listener to this and we're going to
11875
07:06:02,798 --> 07:06:04,080
say
11876
07:06:04,080 --> 07:06:05,280
on click
11877
07:06:05,280 --> 07:06:07,040
get search
11878
07:06:07,040 --> 07:06:10,400
get search superhero and for now
11879
07:06:10,400 --> 07:06:12,878
let's just hard code batman so it should
11880
07:06:12,878 --> 07:06:14,638
just get us batman
11881
07:06:14,638 --> 07:06:15,920
and then
11882
07:06:15,920 --> 07:06:17,920
put him out so let's hit search
11883
07:06:17,920 --> 07:06:20,240
oh sorry let's hit run at the top first
11884
07:06:20,240 --> 07:06:22,478
then let's hit search and look at that
11885
07:06:22,478 --> 07:06:24,798
it's working perfectly exactly how we
11886
07:06:24,798 --> 07:06:26,878
wanted it to work and when i hit search
11887
07:06:26,878 --> 07:06:30,320
i am getting batman exactly like like i
11888
07:06:30,320 --> 07:06:31,600
expected
11889
07:06:31,600 --> 07:06:34,878
okay let's replace batman with thanos
11890
07:06:34,878 --> 07:06:38,000
and let's hit run and let's hit search
11891
07:06:38,000 --> 07:06:39,440
and you can see that i'm getting back
11892
07:06:39,440 --> 07:06:40,558
thanos
11893
07:06:40,558 --> 07:06:42,718
so this is good this is doing this is
11894
07:06:42,718 --> 07:06:44,638
doing what i needed to do all right now
11895
07:06:44,638 --> 07:06:46,958
what we need to do is we want to be able
11896
07:06:46,958 --> 07:06:49,920
to put in stuff in here and get access
11897
07:06:49,920 --> 07:06:52,080
to that so i'm gonna go
11898
07:06:52,080 --> 07:06:54,000
inside of this input
11899
07:06:54,000 --> 07:06:56,000
and to grab its value we're gonna give
11900
07:06:56,000 --> 07:06:59,680
it a name and we're gonna say the value
11901
07:06:59,680 --> 07:07:01,440
of this is
11902
07:07:01,440 --> 07:07:03,200
search input
11903
07:07:03,200 --> 07:07:04,718
so let's see if
11904
07:07:04,718 --> 07:07:06,718
that will allow us to do what we want to
11905
07:07:06,718 --> 07:07:07,520
do
11906
07:07:07,520 --> 07:07:09,120
so i'm going to go ahead and i'm going
11907
07:07:09,120 --> 07:07:11,280
to say const
11908
07:07:11,280 --> 07:07:12,478
search
11909
07:07:12,478 --> 07:07:13,520
input
11910
07:07:13,520 --> 07:07:16,718
i'm going to say document dot get
11911
07:07:16,718 --> 07:07:19,040
element by id
11912
07:07:19,040 --> 07:07:22,240
actually we have not given this an id
11913
07:07:22,240 --> 07:07:23,120
let's
11914
07:07:23,120 --> 07:07:25,200
take out the value
11915
07:07:25,200 --> 07:07:28,478
and we're just gonna do id for now id is
11916
07:07:28,478 --> 07:07:30,558
gonna be search
11917
07:07:30,558 --> 07:07:33,280
input okay let's let's do the id there
11918
07:07:33,280 --> 07:07:35,280
all right now i'm going to say
11919
07:07:35,280 --> 07:07:36,718
search input
11920
07:07:36,718 --> 07:07:40,478
and we'll try to get its inner text so
11921
07:07:40,478 --> 07:07:43,680
search input and let me let's see if we
11922
07:07:43,680 --> 07:07:46,958
do dot inner text if it actually gets us
11923
07:07:46,958 --> 07:07:49,920
what we want all right enter
11924
07:07:49,920 --> 07:07:51,360
enter text
11925
07:07:51,360 --> 07:07:54,478
and um let's console log it here so i'm
11926
07:07:54,478 --> 07:07:56,478
going to say console log
11927
07:07:56,478 --> 07:07:59,760
search input so every time i run i type
11928
07:07:59,760 --> 07:08:01,760
in that button so let's search
11929
07:08:01,760 --> 07:08:04,000
and it doesn't look like i got anything
11930
07:08:04,000 --> 07:08:06,320
let's type in hello
11931
07:08:06,320 --> 07:08:07,680
or let's not type in that let's say
11932
07:08:07,680 --> 07:08:09,440
cookie monster so it's like visible
11933
07:08:09,440 --> 07:08:11,200
let's put a cookie emoji in there and
11934
07:08:11,200 --> 07:08:12,478
let's hit search
11935
07:08:12,478 --> 07:08:14,718
and let's see if that did anything and
11936
07:08:14,718 --> 07:08:16,798
got us anything we're looking for
11937
07:08:16,798 --> 07:08:19,680
nope i don't see anything let's silence
11938
07:08:19,680 --> 07:08:22,160
our console logs here so i'm going to
11939
07:08:22,160 --> 07:08:24,240
silence that console log let's silence
11940
07:08:24,240 --> 07:08:27,680
this console log here and then
11941
07:08:27,680 --> 07:08:29,520
this one in here as well so no more
11942
07:08:29,520 --> 07:08:32,160
console logs let's hit run
11943
07:08:32,160 --> 07:08:34,478
let's hit search and something is
11944
07:08:34,478 --> 07:08:37,440
happening but we're not getting what we
11945
07:08:37,440 --> 07:08:40,080
want let's type it in search is still
11946
07:08:40,080 --> 07:08:42,160
empty all right so all we need to do
11947
07:08:42,160 --> 07:08:44,400
let's just hit run here okay all we need
11948
07:08:44,400 --> 07:08:46,558
to do is make sure search input is
11949
07:08:46,558 --> 07:08:49,280
documented element by d search input
11950
07:08:49,280 --> 07:08:51,600
right and then every single time we run
11951
07:08:51,600 --> 07:08:52,878
this function
11952
07:08:52,878 --> 07:08:55,200
search we're going to console.log
11953
07:08:55,200 --> 07:08:57,840
whatever value is in here okay so we're
11954
07:08:57,840 --> 07:08:58,958
going to say
11955
07:08:58,958 --> 07:09:00,718
search input
11956
07:09:00,718 --> 07:09:03,280
dot value and make sure the console log
11957
07:09:03,280 --> 07:09:05,360
this so now they'll extract the value
11958
07:09:05,360 --> 07:09:07,680
from that field so let's say i write in
11959
07:09:07,680 --> 07:09:08,718
uh
11960
07:09:08,718 --> 07:09:10,080
you know
11961
07:09:10,080 --> 07:09:11,200
cookie
11962
07:09:11,200 --> 07:09:14,080
monster and hit search and you can see
11963
07:09:14,080 --> 07:09:16,080
we're extracting cookie monster right
11964
07:09:16,080 --> 07:09:18,878
over here exactly how we want to right
11965
07:09:18,878 --> 07:09:20,958
so this is going to be the search that
11966
07:09:20,958 --> 07:09:23,280
we're now gonna pass in
11967
07:09:23,280 --> 07:09:25,760
okay so this is the search input.value
11968
07:09:25,760 --> 07:09:27,520
is essentially gonna be the name
11969
07:09:27,520 --> 07:09:31,360
so this getsearch superhero function
11970
07:09:31,360 --> 07:09:33,040
what we're gonna do is we're gonna copy
11971
07:09:33,040 --> 07:09:35,120
this search input.value and every time
11972
07:09:35,120 --> 07:09:37,440
we call this function instead of passing
11973
07:09:37,440 --> 07:09:39,120
hard coded thanos
11974
07:09:39,120 --> 07:09:41,360
instead we're just going to pass in the
11975
07:09:41,360 --> 07:09:43,120
user's
11976
07:09:43,120 --> 07:09:45,360
search input value that they put in so
11977
07:09:45,360 --> 07:09:47,680
let's try running this again okay let's
11978
07:09:47,680 --> 07:09:49,200
try running this again
11979
07:09:49,200 --> 07:09:52,080
and i will say thanos
11980
07:09:52,080 --> 07:09:54,798
and let's hit search and boom let's hit
11981
07:09:54,798 --> 07:09:58,160
batman and hit search and boom let's hit
11982
07:09:58,160 --> 07:09:59,440
spider
11983
07:09:59,440 --> 07:10:01,920
dash man let's go search
11984
07:10:01,920 --> 07:10:05,760
uh ethan maybe oh wow that worked i'm
11985
07:10:05,760 --> 07:10:08,558
surprised and that was fast too look at
11986
07:10:08,558 --> 07:10:10,558
how fast it is man
11987
07:10:10,558 --> 07:10:13,200
this is nice
11988
07:10:13,200 --> 07:10:14,160
right
11989
07:10:14,160 --> 07:10:16,718
um this app is doing what it's supposed
11990
07:10:16,718 --> 07:10:18,958
to be doing so when you search it gets
11991
07:10:18,958 --> 07:10:20,878
you a hero when you click get random
11992
07:10:20,878 --> 07:10:23,760
hero it gets you here as well now what
11993
07:10:23,760 --> 07:10:26,400
we want to do is above the hero it
11994
07:10:26,400 --> 07:10:28,320
should say their name okay and
11995
07:10:28,320 --> 07:10:30,160
underneath it just say maybe some of
11996
07:10:30,160 --> 07:10:31,520
their stats
11997
07:10:31,520 --> 07:10:34,478
so let's go ahead you should try see if
11998
07:10:34,478 --> 07:10:36,240
you could try that on your own
11999
07:10:36,240 --> 07:10:37,280
um
12000
07:10:37,280 --> 07:10:39,440
but we'll walk through it together if
12001
07:10:39,440 --> 07:10:41,040
you don't get it okay so i'll give you
12002
07:10:41,040 --> 07:10:42,558
five seconds to try to pause and try to
12003
07:10:42,558 --> 07:10:44,240
do that on your own
12004
07:10:44,240 --> 07:10:45,280
five
12005
07:10:45,280 --> 07:10:47,360
four three
12006
07:10:47,360 --> 07:10:48,400
two
12007
07:10:48,400 --> 07:10:50,160
one
12008
07:10:50,160 --> 07:10:53,760
cool so what we'll do is we'll
12009
07:10:53,760 --> 07:10:56,798
get the hero name i kind of showed that
12010
07:10:56,798 --> 07:10:58,400
at the top okay so let's see where we're
12011
07:10:58,400 --> 07:11:03,440
doing this we're saying okay the hero
12012
07:11:03,440 --> 07:11:04,400
image
12013
07:11:04,400 --> 07:11:06,798
i want you to replace it with an image
12014
07:11:06,798 --> 07:11:08,000
and i want you to replace it with the
12015
07:11:08,000 --> 07:11:09,760
following so let's go back
12016
07:11:09,760 --> 07:11:11,760
to where we're doing that
12017
07:11:11,760 --> 07:11:13,520
so we're getting the hero
12018
07:11:13,520 --> 07:11:15,440
let's just go to our random
12019
07:11:15,440 --> 07:11:17,040
hero function first because that one is
12020
07:11:17,040 --> 07:11:18,558
easier to understand
12021
07:11:18,558 --> 07:11:21,760
and here we're creating this
12022
07:11:21,760 --> 07:11:23,520
you know we're basically saying hey the
12023
07:11:23,520 --> 07:11:26,000
html is the inner html is this we're
12024
07:11:26,000 --> 07:11:28,400
creating this image show that image
12025
07:11:28,400 --> 07:11:30,718
well above that image we could actually
12026
07:11:30,718 --> 07:11:32,958
put the um
12027
07:11:32,958 --> 07:11:35,040
we could put their
12028
07:11:35,040 --> 07:11:36,080
name
12029
07:11:36,080 --> 07:11:39,680
okay so for example i could say
12030
07:11:39,680 --> 07:11:41,680
kant's
12031
07:11:41,680 --> 07:11:45,520
is uh we're gonna go grab the
12032
07:11:45,520 --> 07:11:46,798
json
12033
07:11:46,798 --> 07:11:48,478
and we're gonna say dot name
12034
07:11:48,478 --> 07:11:51,280
so json.name but we need to actually
12035
07:11:51,280 --> 07:11:52,160
like
12036
07:11:52,160 --> 07:11:55,280
make the actual h2 or whatever so let's
12037
07:11:55,280 --> 07:11:58,718
turn it into an h2 i'm going to say h2
12038
07:11:58,718 --> 07:12:01,200
wrap that in an h2 okay
12039
07:12:01,200 --> 07:12:03,280
and we'll
12040
07:12:03,280 --> 07:12:04,718
wrap this
12041
07:12:04,718 --> 07:12:06,798
this so json.name
12042
07:12:06,798 --> 07:12:08,240
and now if you want to like kind of
12043
07:12:08,240 --> 07:12:09,920
concatenate these together you could
12044
07:12:09,920 --> 07:12:11,280
actually go i mean this is kind of
12045
07:12:11,280 --> 07:12:12,798
getting really crazy
12046
07:12:12,798 --> 07:12:14,718
but what this is doing here is you're
12047
07:12:14,718 --> 07:12:17,840
inserting the name html and then you're
12048
07:12:17,840 --> 07:12:19,840
inserting the image right after that so
12049
07:12:19,840 --> 07:12:21,840
let's see if we can try this and if this
12050
07:12:21,840 --> 07:12:23,120
works
12051
07:12:23,120 --> 07:12:25,040
let's go random hero
12052
07:12:25,040 --> 07:12:27,280
right and now we get robin v
12053
07:12:27,280 --> 07:12:29,200
and the image let's try again we get
12054
07:12:29,200 --> 07:12:31,280
vision 2 and
12055
07:12:31,280 --> 07:12:32,240
boom
12056
07:12:32,240 --> 07:12:34,160
right we get the name
12057
07:12:34,160 --> 07:12:36,400
and the hero as well
12058
07:12:36,400 --> 07:12:38,878
and we can get their stats too so let's
12059
07:12:38,878 --> 07:12:41,360
try console logging the json
12060
07:12:41,360 --> 07:12:43,520
and we're gonna do dot power
12061
07:12:43,520 --> 07:12:44,718
stats
12062
07:12:44,718 --> 07:12:46,878
let's run random hero and you can see we
12063
07:12:46,878 --> 07:12:49,200
get back intelligence strength speed
12064
07:12:49,200 --> 07:12:51,600
durability power combat so you can
12065
07:12:51,600 --> 07:12:53,440
actually show all of those stats right
12066
07:12:53,440 --> 07:12:54,878
underneath here
12067
07:12:54,878 --> 07:12:56,558
see if you could do that by yourself
12068
07:12:56,558 --> 07:12:58,638
okay so here i would highly recommend
12069
07:12:58,638 --> 07:13:00,160
you take about five seconds see if you
12070
07:13:00,160 --> 07:13:02,798
could add that yourself so i'll give you
12071
07:13:02,798 --> 07:13:04,558
five seconds five
12072
07:13:04,558 --> 07:13:05,520
four
12073
07:13:05,520 --> 07:13:06,558
three
12074
07:13:06,558 --> 07:13:07,520
two
12075
07:13:07,520 --> 07:13:08,718
one
12076
07:13:08,718 --> 07:13:10,478
all right so let's go ahead and do this
12077
07:13:10,478 --> 07:13:12,798
together so i'm gonna say const
12078
07:13:12,798 --> 07:13:16,000
power stats um is going to be
12079
07:13:16,000 --> 07:13:18,160
you know not h2 but maybe it's gonna be
12080
07:13:18,160 --> 07:13:18,878
like
12081
07:13:18,878 --> 07:13:20,558
p tags
12082
07:13:20,558 --> 07:13:22,558
so we could actually either do all power
12083
07:13:22,558 --> 07:13:24,400
stats or what we can do is just do one
12084
07:13:24,400 --> 07:13:26,000
stat at a time so let's just try
12085
07:13:26,000 --> 07:13:28,000
intelligence and we're gonna put in a p
12086
07:13:28,000 --> 07:13:29,120
tag here
12087
07:13:29,120 --> 07:13:32,320
and i'm gonna say
12088
07:13:33,120 --> 07:13:33,958
dot json.powerstats.intel
12089
07:13:35,280 --> 07:13:37,760
and wrap that close that p tag right
12090
07:13:37,760 --> 07:13:38,718
there
12091
07:13:38,718 --> 07:13:39,680
so
12092
07:13:39,680 --> 07:13:42,400
so now what i can do is i can go and
12093
07:13:42,400 --> 07:13:44,878
take this intelligence and just show it
12094
07:13:44,878 --> 07:13:46,878
at the bottom right so
12095
07:13:46,878 --> 07:13:48,478
we can
12096
07:13:48,478 --> 07:13:50,160
add that right here
12097
07:13:50,160 --> 07:13:51,520
intelligence
12098
07:13:51,520 --> 07:13:54,638
hit enter and let's go right random hero
12099
07:13:54,638 --> 07:13:56,958
right ellie bishop intelligence is
12100
07:13:56,958 --> 07:13:58,958
undefined interesting let's see why
12101
07:13:58,958 --> 07:14:01,200
that's happening oh i misspelled
12102
07:14:01,200 --> 07:14:03,280
intelligence right here okay so careful
12103
07:14:03,280 --> 07:14:05,440
with the so that's why copy pasting is
12104
07:14:05,440 --> 07:14:07,680
oftentimes better better for programming
12105
07:14:07,680 --> 07:14:09,760
so you don't make these errors let's go
12106
07:14:09,760 --> 07:14:13,040
ahead random hero we got sauron
12107
07:14:13,040 --> 07:14:16,160
and then the and it's 88 over here okay
12108
07:14:16,160 --> 07:14:17,040
so
12109
07:14:17,040 --> 07:14:18,718
if you want to make that better you can
12110
07:14:18,718 --> 07:14:21,600
actually say intelligence
12111
07:14:21,600 --> 07:14:23,200
and then
12112
07:14:23,200 --> 07:14:25,280
it'll look cleaner that way so now let's
12113
07:14:25,280 --> 07:14:26,798
hit random hero
12114
07:14:26,798 --> 07:14:29,360
tundra intelligence is 38
12115
07:14:29,360 --> 07:14:30,798
and
12116
07:14:30,798 --> 07:14:32,400
try doing this
12117
07:14:32,400 --> 07:14:34,798
right let's make another one for another
12118
07:14:34,798 --> 07:14:36,478
stat so we're going to do instead of
12119
07:14:36,478 --> 07:14:38,478
intelligence maybe this one will do
12120
07:14:38,478 --> 07:14:39,520
strength
12121
07:14:39,520 --> 07:14:41,760
and we'll say strength
12122
07:14:41,760 --> 07:14:43,200
and we'll
12123
07:14:43,200 --> 07:14:45,200
paste strength right there
12124
07:14:45,200 --> 07:14:48,240
and then we'll attach strength onto this
12125
07:14:48,240 --> 07:14:50,638
so we're just making this this long html
12126
07:14:50,638 --> 07:14:51,920
right over here
12127
07:14:51,920 --> 07:14:54,320
all right let's run this
12128
07:14:54,320 --> 07:14:56,878
get random hero and you can see now we
12129
07:14:56,878 --> 07:14:59,680
have the cape we have intelligence and
12130
07:14:59,680 --> 07:15:00,958
strength
12131
07:15:00,958 --> 07:15:02,958
now if you want to make it like
12132
07:15:02,958 --> 07:15:05,600
kind of more enticing you for for
12133
07:15:05,600 --> 07:15:07,840
intelligence let's put a brain emoji for
12134
07:15:07,840 --> 07:15:10,080
this let's put a muscle emoji
12135
07:15:10,080 --> 07:15:13,040
and now let's run this get random hero
12136
07:15:13,040 --> 07:15:15,200
and you can see intelligence is 50 and
12137
07:15:15,200 --> 07:15:17,920
strength is 10. right random hero
12138
07:15:17,920 --> 07:15:20,240
intelligence is null so maybe this hero
12139
07:15:20,240 --> 07:15:22,878
does not have an intelligence right
12140
07:15:22,878 --> 07:15:26,080
here intelligence is 88 strength is 10.
12141
07:15:26,080 --> 07:15:28,320
now we can see the pattern here right
12142
07:15:28,320 --> 07:15:30,638
basically for all the stats we're going
12143
07:15:30,638 --> 07:15:32,400
to keep needing to repeat this pattern
12144
07:15:32,400 --> 07:15:34,400
again and again and again meaning this
12145
07:15:34,400 --> 07:15:36,400
it will basically you'll need to keep
12146
07:15:36,400 --> 07:15:39,040
copy pasting p tags together so at the
12147
07:15:39,040 --> 07:15:40,798
end of the day what it actually looks
12148
07:15:40,798 --> 07:15:42,718
like is p tag
12149
07:15:42,718 --> 07:15:44,878
right and then you're gonna have this
12150
07:15:44,878 --> 07:15:46,718
so like actually it'll actually look
12151
07:15:46,718 --> 07:15:49,360
like this so you have this p tag right
12152
07:15:49,360 --> 07:15:50,558
here
12153
07:15:50,558 --> 07:15:54,638
um combined with this p tag back to back
12154
07:15:54,638 --> 07:15:57,120
combined with probably another p tag
12155
07:15:57,120 --> 07:16:00,000
that talks about combat
12156
07:16:00,000 --> 07:16:01,840
right combined with another p tag that
12157
07:16:01,840 --> 07:16:04,558
talks about maybe strength or sorry not
12158
07:16:04,558 --> 07:16:06,558
strength but like speed followed by
12159
07:16:06,558 --> 07:16:09,040
another p tag that says durability
12160
07:16:09,040 --> 07:16:11,120
so we're seeing this like pattern here
12161
07:16:11,120 --> 07:16:12,878
and we want to actually create this big
12162
07:16:12,878 --> 07:16:14,958
long string and then we want to just
12163
07:16:14,958 --> 07:16:16,638
attach that string
12164
07:16:16,638 --> 07:16:18,958
right here so you know
12165
07:16:18,958 --> 07:16:20,798
it's a pattern so what we can actually
12166
07:16:20,798 --> 07:16:22,798
do is let's just create a function that
12167
07:16:22,798 --> 07:16:24,000
actually
12168
07:16:24,000 --> 07:16:25,840
you know gets
12169
07:16:25,840 --> 07:16:27,360
stats
12170
07:16:27,360 --> 07:16:30,558
get and show stats or actually let's say
12171
07:16:30,558 --> 07:16:32,000
get stats
12172
07:16:32,000 --> 07:16:34,558
html so it will
12173
07:16:34,558 --> 07:16:36,638
get the stats right but then convert
12174
07:16:36,638 --> 07:16:37,920
them into
12175
07:16:37,920 --> 07:16:41,200
a usable html so
12176
07:16:41,200 --> 07:16:43,200
we're going to create this function
12177
07:16:43,200 --> 07:16:45,200
and we're going to expect that we're
12178
07:16:45,200 --> 07:16:47,600
going to get a character okay so just
12179
07:16:47,600 --> 07:16:48,798
like we're going to we're going to
12180
07:16:48,798 --> 07:16:51,040
expect that we get this like json-like
12181
07:16:51,040 --> 07:16:53,920
data of that specific character and then
12182
07:16:53,920 --> 07:16:55,200
what we're gonna do
12183
07:16:55,200 --> 07:16:59,760
is we can do map here or we can do a for
12184
07:16:59,760 --> 07:17:02,558
each but but since we're just trying to
12185
07:17:02,558 --> 07:17:05,840
do a string concatenation it might make
12186
07:17:05,840 --> 07:17:08,000
more sense to do
12187
07:17:08,000 --> 07:17:09,680
a four
12188
07:17:09,680 --> 07:17:12,478
each you could also do a dot map and
12189
07:17:12,478 --> 07:17:14,798
kind of join everything together so
12190
07:17:14,798 --> 07:17:17,200
we could definitely do it
12191
07:17:17,200 --> 07:17:19,520
that way as well okay so how that might
12192
07:17:19,520 --> 07:17:21,840
look like is say you have an array
12193
07:17:21,840 --> 07:17:23,680
let me clear this
12194
07:17:23,680 --> 07:17:27,600
so say you have an array that says hello
12195
07:17:27,600 --> 07:17:28,718
by
12196
07:17:28,718 --> 07:17:30,638
cookie whatever
12197
07:17:30,638 --> 07:17:34,240
and let's give this so if i want to join
12198
07:17:34,240 --> 07:17:36,240
all of these i can actually just do this
12199
07:17:36,240 --> 07:17:37,840
and i can just say hey just join them
12200
07:17:37,840 --> 07:17:39,120
all together
12201
07:17:39,120 --> 07:17:40,878
so if i have an array
12202
07:17:40,878 --> 07:17:43,360
of like p tags
12203
07:17:43,360 --> 07:17:45,280
and i have
12204
07:17:45,280 --> 07:17:47,600
intelligence here
12205
07:17:47,600 --> 07:17:50,160
intelligence and then i have
12206
07:17:50,160 --> 07:17:51,920
another p tag
12207
07:17:51,920 --> 07:17:53,680
that says
12208
07:17:53,680 --> 07:17:55,360
speed
12209
07:17:55,360 --> 07:17:59,360
right i could actually do dot join here
12210
07:17:59,360 --> 07:18:00,558
and do this
12211
07:18:00,558 --> 07:18:03,200
and i just get back one big string
12212
07:18:03,200 --> 07:18:04,638
so this is kind of what we want to do
12213
07:18:04,638 --> 07:18:06,958
here so i'm going to say
12214
07:18:06,958 --> 07:18:08,320
character
12215
07:18:08,320 --> 07:18:11,440
okay dot map so map basically says
12216
07:18:11,440 --> 07:18:13,440
return an array
12217
07:18:13,440 --> 07:18:14,638
and
12218
07:18:14,638 --> 07:18:16,240
we're actually we're going to do power
12219
07:18:16,240 --> 07:18:18,120
stats
12220
07:18:18,120 --> 07:18:20,000
character.powerstats.map okay so we're
12221
07:18:20,000 --> 07:18:21,360
going to loop through all the power
12222
07:18:21,360 --> 07:18:22,398
stats
12223
07:18:22,398 --> 07:18:23,360
we're going to loop through all the
12224
07:18:23,360 --> 07:18:24,798
power stats
12225
07:18:24,798 --> 07:18:26,398
and for each stat
12226
07:18:26,398 --> 07:18:28,878
what i'm going to say
12227
07:18:28,878 --> 07:18:30,798
is
12228
07:18:30,798 --> 07:18:32,558
make this html
12229
07:18:32,558 --> 07:18:34,798
like looking thing here all right so
12230
07:18:34,798 --> 07:18:36,240
since what we're getting back is
12231
07:18:36,240 --> 07:18:38,240
actually an object it's not going to be
12232
07:18:38,240 --> 07:18:40,398
super simple to loop through this so
12233
07:18:40,398 --> 07:18:42,478
what we're going to say is
12234
07:18:42,478 --> 07:18:44,558
character.powerstats
12235
07:18:44,558 --> 07:18:46,958
is not an array okay this is actually
12236
07:18:46,958 --> 07:18:48,958
going to be an object that you're
12237
07:18:48,958 --> 07:18:50,240
getting back
12238
07:18:50,240 --> 07:18:52,398
so what we want to do here is we want to
12239
07:18:52,398 --> 07:18:55,040
loop kind of this way and i'm going to
12240
07:18:55,040 --> 07:18:58,520
say for stat in
12241
07:18:58,520 --> 07:19:00,080
character.powerstats and we're gonna
12242
07:19:00,080 --> 07:19:01,120
loop through this and let's just
12243
07:19:01,120 --> 07:19:03,600
console.log each stat
12244
07:19:03,600 --> 07:19:05,200
and let's call this function
12245
07:19:05,200 --> 07:19:07,520
getstats.html
12246
07:19:07,520 --> 07:19:08,558
uh
12247
07:19:08,558 --> 07:19:10,718
let's just say maybe
12248
07:19:10,718 --> 07:19:12,320
and i'll call this function and we'll
12249
07:19:12,320 --> 07:19:14,718
pass it the character which is just json
12250
07:19:14,718 --> 07:19:16,160
in this case
12251
07:19:16,160 --> 07:19:19,520
uh but this really is the character okay
12252
07:19:19,520 --> 07:19:20,320
so
12253
07:19:20,320 --> 07:19:22,958
get stats html pass it to character and
12254
07:19:22,958 --> 07:19:24,638
then it should just run
12255
07:19:24,638 --> 07:19:26,718
and then console log all the power stats
12256
07:19:26,718 --> 07:19:28,798
let's run this let's get a random hero
12257
07:19:28,798 --> 07:19:30,240
and you can see it just ran through and
12258
07:19:30,240 --> 07:19:32,160
says intelligence strength speed
12259
07:19:32,160 --> 07:19:34,878
durability power combat perfect
12260
07:19:34,878 --> 07:19:37,200
so now what we want to do now that we're
12261
07:19:37,200 --> 07:19:39,680
actually able to loop through this
12262
07:19:39,680 --> 07:19:41,520
now another way that we can actually
12263
07:19:41,520 --> 07:19:43,200
loop through this and let me show this
12264
07:19:43,200 --> 07:19:45,120
to you because this is kind of cool
12265
07:19:45,120 --> 07:19:48,000
so if you have an object
12266
07:19:48,000 --> 07:19:49,840
right so let's say we have things and
12267
07:19:49,840 --> 07:19:52,558
things is this object right over here
12268
07:19:52,558 --> 07:19:54,958
which has strength and cookie well i can
12269
07:19:54,958 --> 07:19:57,280
actually go and i can say
12270
07:19:57,280 --> 07:19:59,440
object.keys
12271
07:19:59,440 --> 07:20:01,520
and pass in things to it and it's going
12272
07:20:01,520 --> 07:20:03,840
to just turn that turn all of its keys
12273
07:20:03,840 --> 07:20:06,240
into an array and then i can actually
12274
07:20:06,240 --> 07:20:08,798
loop through those keys so
12275
07:20:08,798 --> 07:20:11,040
that's a really
12276
07:20:11,040 --> 07:20:13,680
cool way to actually
12277
07:20:13,680 --> 07:20:16,000
to actually like iterate so instead of
12278
07:20:16,000 --> 07:20:19,200
saying for stat and power.stats what i
12279
07:20:19,200 --> 07:20:21,600
could actually do is i could say object
12280
07:20:21,600 --> 07:20:23,760
dot keys
12281
07:20:23,760 --> 07:20:28,320
and um we could ask for the character
12282
07:20:28,320 --> 07:20:30,958
character the power stats
12283
07:20:30,958 --> 07:20:34,478
and this will turn the whole thing into
12284
07:20:34,478 --> 07:20:35,920
let's actually just
12285
07:20:35,920 --> 07:20:38,160
console log this so it's not super
12286
07:20:38,160 --> 07:20:39,680
confusing
12287
07:20:39,680 --> 07:20:41,440
i'll console log this here in the same
12288
07:20:41,440 --> 07:20:44,000
line all right let's run this and let's
12289
07:20:44,000 --> 07:20:45,760
get random hero
12290
07:20:45,760 --> 07:20:47,600
and you can see that i have this array
12291
07:20:47,600 --> 07:20:49,360
here intelligence strength speed
12292
07:20:49,360 --> 07:20:51,520
durability power combat so i just turned
12293
07:20:51,520 --> 07:20:53,040
this into
12294
07:20:53,040 --> 07:20:55,680
an array of all of the keys
12295
07:20:55,680 --> 07:20:57,920
so now what's cool about this is because
12296
07:20:57,920 --> 07:21:00,080
it is an array i can loop through it
12297
07:21:00,080 --> 07:21:02,798
with a dot map and then a dot map will
12298
07:21:02,798 --> 07:21:05,840
loop through this array and also return
12299
07:21:05,840 --> 07:21:07,600
an array
12300
07:21:07,600 --> 07:21:10,160
so now what i can say is for
12301
07:21:10,160 --> 07:21:12,798
each stat i want you to do the following
12302
07:21:12,798 --> 07:21:14,000
okay so
12303
07:21:14,000 --> 07:21:16,798
it's going to go through each stat so
12304
07:21:16,798 --> 07:21:19,120
intelligence strength speed durability
12305
07:21:19,120 --> 07:21:21,440
power combat blah blah
12306
07:21:21,440 --> 07:21:24,080
and here's what i want you to do you're
12307
07:21:24,080 --> 07:21:25,840
going to
12308
07:21:25,840 --> 07:21:27,840
uh create this string
12309
07:21:27,840 --> 07:21:30,398
okay we're going to say p tag and then
12310
07:21:30,398 --> 07:21:31,360
this p
12311
07:21:31,360 --> 07:21:32,718
make sure it's a template literal with
12312
07:21:32,718 --> 07:21:35,440
backticks it's going to be a p tag
12313
07:21:35,440 --> 07:21:38,160
it's going to have um that
12314
07:21:38,160 --> 07:21:40,160
the name of the key
12315
07:21:40,160 --> 07:21:42,718
that you're passing so here i'm just
12316
07:21:42,718 --> 07:21:46,080
going to say it's going to be the stat
12317
07:21:46,080 --> 07:21:48,240
okay whatever stat that is so it's so
12318
07:21:48,240 --> 07:21:49,520
this is going to turn into their
12319
07:21:49,520 --> 07:21:51,200
strength or whatever
12320
07:21:51,200 --> 07:21:53,120
uh or like
12321
07:21:53,120 --> 07:21:54,878
intelligence
12322
07:21:54,878 --> 07:21:58,718
right and then we can say
12323
07:21:58,718 --> 07:22:00,478
i'll put a colon here
12324
07:22:00,478 --> 07:22:02,320
right and then after that colon
12325
07:22:02,320 --> 07:22:04,478
we want to do is actually put the value
12326
07:22:04,478 --> 07:22:06,240
so i'm going to say
12327
07:22:06,240 --> 07:22:09,440
i'm going to say character dot power
12328
07:22:09,440 --> 07:22:10,798
stats
12329
07:22:10,798 --> 07:22:12,878
right and then pass it
12330
07:22:12,878 --> 07:22:15,360
that specific stat we're looking for so
12331
07:22:15,360 --> 07:22:17,040
i can just say stat or i could just do
12332
07:22:17,040 --> 07:22:18,638
it like this and i can say stat like
12333
07:22:18,638 --> 07:22:19,680
this
12334
07:22:19,680 --> 07:22:20,878
okay so
12335
07:22:20,878 --> 07:22:22,478
it's going to go to that object and it's
12336
07:22:22,478 --> 07:22:25,120
going to pull that specific stat so all
12337
07:22:25,120 --> 07:22:27,600
this means is let's say that this object
12338
07:22:27,600 --> 07:22:29,120
was called
12339
07:22:29,120 --> 07:22:31,120
we had something called character
12340
07:22:31,120 --> 07:22:33,280
dot power stats and that was equal to
12341
07:22:33,280 --> 07:22:35,520
this array right there whatever right or
12342
07:22:35,520 --> 07:22:38,160
that object let's copy that object and
12343
07:22:38,160 --> 07:22:40,240
let's say we have power stats is equal
12344
07:22:40,240 --> 07:22:43,440
to this object right here and so if i do
12345
07:22:43,440 --> 07:22:45,040
power stats
12346
07:22:45,040 --> 07:22:48,000
and i pass in strength oops it's going
12347
07:22:48,000 --> 07:22:50,478
to get me the value
12348
07:22:50,478 --> 07:22:53,520
of strength so it's going to get me 8
12349
07:22:53,520 --> 07:22:54,478
right there
12350
07:22:54,478 --> 07:22:56,160
so that's all we're doing here i'm
12351
07:22:56,160 --> 07:22:58,958
basically going into that object
12352
07:22:58,958 --> 07:23:02,240
and i'm just pulling out its value
12353
07:23:02,240 --> 07:23:04,080
and then we're just going to end this p
12354
07:23:04,080 --> 07:23:05,360
tag
12355
07:23:05,360 --> 07:23:06,958
and this p tag
12356
07:23:06,958 --> 07:23:08,558
all right so it's gonna loop through and
12357
07:23:08,558 --> 07:23:10,718
do that and then let's see if we need to
12358
07:23:10,718 --> 07:23:14,320
do anything else for this one
12359
07:23:14,320 --> 07:23:16,080
and honestly it doesn't really look like
12360
07:23:16,080 --> 07:23:17,920
it so we can actually just return this
12361
07:23:17,920 --> 07:23:20,718
array and it's gonna create a full array
12362
07:23:20,718 --> 07:23:22,798
okay and so i'm going to call this
12363
07:23:22,798 --> 07:23:24,000
const
12364
07:23:24,000 --> 07:23:26,240
let's say stats
12365
07:23:26,240 --> 07:23:28,718
and then we'll console log
12366
07:23:28,718 --> 07:23:30,718
stats
12367
07:23:30,718 --> 07:23:32,240
okay so let's run this let's do get
12368
07:23:32,240 --> 07:23:35,840
random hero and you can see now i have
12369
07:23:35,840 --> 07:23:38,000
intelligence and a p tag i have strength
12370
07:23:38,000 --> 07:23:40,878
and a p tag i have speed and a p tag
12371
07:23:40,878 --> 07:23:43,680
i have durability in a p tag power and
12372
07:23:43,680 --> 07:23:46,240
combat
12373
07:23:46,240 --> 07:23:48,958
and if we want to give each of these an
12374
07:23:48,958 --> 07:23:51,280
emoji we could do that as well and we
12375
07:23:51,280 --> 07:23:53,760
can kind of create a map for the emoji
12376
07:23:53,760 --> 07:23:54,958
and then
12377
07:23:54,958 --> 07:23:58,558
kind of like map the
12378
07:23:58,718 --> 07:24:00,398
map the stat
12379
07:24:00,398 --> 07:24:03,680
stat to emoji map we could create that
12380
07:24:03,680 --> 07:24:06,398
and then we could even use that here
12381
07:24:06,398 --> 07:24:08,398
and get the appropriate emoji so for
12382
07:24:08,398 --> 07:24:09,600
example
12383
07:24:09,600 --> 07:24:11,200
we can we can create that right here
12384
07:24:11,200 --> 07:24:12,798
right so we could say
12385
07:24:12,798 --> 07:24:15,120
stat to emoji
12386
07:24:15,120 --> 07:24:16,878
right and then we could say hey if the
12387
07:24:16,878 --> 07:24:20,320
stat is let's say intelligence
12388
07:24:20,320 --> 07:24:21,760
and you don't need to even put it into
12389
07:24:21,760 --> 07:24:23,040
string here
12390
07:24:23,040 --> 07:24:25,920
so let's go intelligence
12391
07:24:25,920 --> 07:24:28,398
is going to be this
12392
07:24:28,398 --> 07:24:30,000
brain
12393
07:24:30,000 --> 07:24:31,440
comma
12394
07:24:31,440 --> 07:24:32,798
and
12395
07:24:32,798 --> 07:24:35,440
strength is going to be muscle
12396
07:24:35,440 --> 07:24:38,478
and then we have speed
12397
07:24:38,478 --> 07:24:40,320
let's paste this and then we have
12398
07:24:40,320 --> 07:24:42,360
durability
12399
07:24:42,360 --> 07:24:44,000
durability
12400
07:24:44,000 --> 07:24:46,878
and then we have power
12401
07:24:46,878 --> 07:24:49,840
and then we have combat so strength is
12402
07:24:49,840 --> 07:24:52,240
that speed is a zap
12403
07:24:52,240 --> 07:24:55,040
durability is let's just say like maybe
12404
07:24:55,040 --> 07:24:57,040
a tire emoji or something we'd
12405
07:24:57,040 --> 07:24:59,520
apparently there's no tire so
12406
07:24:59,520 --> 07:25:02,000
say a rope
12407
07:25:02,000 --> 07:25:04,080
um
12408
07:25:04,080 --> 07:25:06,398
workout
12409
07:25:06,398 --> 07:25:09,600
lift there we go that's durability
12410
07:25:09,600 --> 07:25:12,000
and then power
12411
07:25:12,000 --> 07:25:14,240
i don't know that will be power okay and
12412
07:25:14,240 --> 07:25:16,798
then combat will be sword
12413
07:25:16,798 --> 07:25:19,440
okay stat to emoji
12414
07:25:19,440 --> 07:25:22,240
so that's emoji so here if we want to
12415
07:25:22,240 --> 07:25:24,320
actually like map that we could actually
12416
07:25:24,320 --> 07:25:25,280
just
12417
07:25:25,280 --> 07:25:27,840
right before this
12418
07:25:27,840 --> 07:25:30,160
stat to emoji and then give it the value
12419
07:25:30,160 --> 07:25:31,920
of the stat
12420
07:25:31,920 --> 07:25:34,240
and uh this is going to get us the emoji
12421
07:25:34,240 --> 07:25:35,840
that we want so let's run this let's hit
12422
07:25:35,840 --> 07:25:37,440
get random hero
12423
07:25:37,440 --> 07:25:38,878
and you can see like we have
12424
07:25:38,878 --> 07:25:42,320
intelligence as a brain strength is that
12425
07:25:42,320 --> 07:25:43,600
zap
12426
07:25:43,600 --> 07:25:45,120
boom
12427
07:25:45,120 --> 07:25:47,840
okay get random here another random hero
12428
07:25:47,840 --> 07:25:49,840
and you can see we got
12429
07:25:49,840 --> 07:25:52,080
boom right
12430
07:25:52,080 --> 07:25:53,840
power combat
12431
07:25:53,840 --> 07:25:55,600
our emojis are working kind of as
12432
07:25:55,600 --> 07:25:58,080
expected so this is actually making all
12433
07:25:58,080 --> 07:26:00,878
the stats for us and getting them
12434
07:26:00,878 --> 07:26:01,760
right
12435
07:26:01,760 --> 07:26:02,878
and
12436
07:26:02,878 --> 07:26:06,240
instead of having all of this crap right
12437
07:26:06,240 --> 07:26:08,638
here which is kind of like
12438
07:26:08,638 --> 07:26:12,240
weird and annoying i could actually just
12439
07:26:12,240 --> 07:26:13,840
remove this
12440
07:26:13,840 --> 07:26:16,000
right and um
12441
07:26:16,000 --> 07:26:18,478
and here we could just say con stats is
12442
07:26:18,478 --> 07:26:20,958
get stats html
12443
07:26:20,958 --> 07:26:22,878
right and then we could just replace all
12444
07:26:22,878 --> 07:26:24,558
of this
12445
07:26:24,558 --> 07:26:26,000
and paste
12446
07:26:26,000 --> 07:26:27,280
stats
12447
07:26:27,280 --> 07:26:29,120
and now when we run this and we get a
12448
07:26:29,120 --> 07:26:30,638
random hero
12449
07:26:30,638 --> 07:26:33,040
okay let's do a random hero
12450
07:26:33,040 --> 07:26:34,718
okay let's see it's all oh yeah it's
12451
07:26:34,718 --> 07:26:36,478
doing undefined right now and we know
12452
07:26:36,478 --> 07:26:37,360
why
12453
07:26:37,360 --> 07:26:39,120
because we haven't turned it into a
12454
07:26:39,120 --> 07:26:41,120
string yet we're like
12455
07:26:41,120 --> 07:26:43,520
this is still an array so remember at
12456
07:26:43,520 --> 07:26:45,600
the end of and we also need to return
12457
07:26:45,600 --> 07:26:47,520
we're not returning anything so we want
12458
07:26:47,520 --> 07:26:49,760
to do is we want to say
12459
07:26:49,760 --> 07:26:53,040
dot join like this okay and we want to
12460
07:26:53,040 --> 07:26:54,440
do return
12461
07:26:54,440 --> 07:26:56,080
stats.join
12462
07:26:56,080 --> 07:26:57,520
and like that
12463
07:26:57,520 --> 07:27:00,080
okay so let's run this and now when i do
12464
07:27:00,080 --> 07:27:02,000
random hero you can see that it's just
12465
07:27:02,000 --> 07:27:04,638
giving me one long string
12466
07:27:04,638 --> 07:27:07,360
of p tags intelligence is a p tag
12467
07:27:07,360 --> 07:27:09,520
this strength right here is a p tag
12468
07:27:09,520 --> 07:27:11,520
speed right here is a p tag and now when
12469
07:27:11,520 --> 07:27:12,558
i look
12470
07:27:12,558 --> 07:27:14,320
you can see that it's actually showing
12471
07:27:14,320 --> 07:27:17,920
up really nicely right so professor zoom
12472
07:27:17,920 --> 07:27:21,120
let's get random hero and you can see
12473
07:27:21,120 --> 07:27:23,280
all of this right here run this again
12474
07:27:23,280 --> 07:27:24,798
random hero
12475
07:27:24,798 --> 07:27:27,040
boom
12476
07:27:28,240 --> 07:27:30,558
mr freeze isis
12477
07:27:30,558 --> 07:27:32,478
run run run and then we're just getting
12478
07:27:32,478 --> 07:27:34,638
the intelligence strength speed
12479
07:27:34,638 --> 07:27:37,760
durability all of that right here
12480
07:27:37,760 --> 07:27:39,680
now it would be nice to have this
12481
07:27:39,680 --> 07:27:41,920
intelligence i capitalized as
12482
07:27:41,920 --> 07:27:43,520
capitalized as well
12483
07:27:43,520 --> 07:27:48,320
so let's go ahead and maybe add a um
12484
07:27:48,320 --> 07:27:52,240
dot title case 2.2 title case okay which
12485
07:27:52,240 --> 07:27:53,760
is kind of a nice little thing that you
12486
07:27:53,760 --> 07:27:55,200
could do
12487
07:27:55,200 --> 07:27:59,040
and we could actually pass that in
12488
07:27:59,040 --> 07:28:02,320
right over here and i could say that two
12489
07:28:02,320 --> 07:28:05,040
title case and let's see if
12490
07:28:05,040 --> 07:28:08,320
title case string method
12491
07:28:08,320 --> 07:28:11,280
javascript okay so you can always like
12492
07:28:11,280 --> 07:28:12,798
google this stuff too and i'll kind of
12493
07:28:12,798 --> 07:28:14,478
show you examples
12494
07:28:14,478 --> 07:28:16,798
and let's see if there's a method that
12495
07:28:16,798 --> 07:28:18,638
does title casing
12496
07:28:18,638 --> 07:28:21,120
do we have to oh dot two upper okay so
12497
07:28:21,120 --> 07:28:22,398
we could actually just do that to
12498
07:28:22,398 --> 07:28:24,958
uppercase and we should be
12499
07:28:24,958 --> 07:28:26,718
we should be fine that shouldn't be too
12500
07:28:26,718 --> 07:28:28,798
bad or maybe that just uppercases
12501
07:28:28,798 --> 07:28:30,320
everything that's not what we want
12502
07:28:30,320 --> 07:28:31,120
either
12503
07:28:31,120 --> 07:28:33,680
actually we can just do two uppercase
12504
07:28:33,680 --> 07:28:35,680
and yes it's not title casing because
12505
07:28:35,680 --> 07:28:38,638
title casing is like pretty complex but
12506
07:28:38,638 --> 07:28:40,638
dot to uppercase is a method that comes
12507
07:28:40,638 --> 07:28:42,558
built into javascript and you can see
12508
07:28:42,558 --> 07:28:44,080
what it does and it's really nice right
12509
07:28:44,080 --> 07:28:46,000
it just capitalizes
12510
07:28:46,000 --> 07:28:48,478
everything which is nice like we're
12511
07:28:48,478 --> 07:28:52,320
capitalizing the stat right here um and
12512
07:28:52,320 --> 07:28:53,920
you know that makes that makes the whole
12513
07:28:53,920 --> 07:28:56,478
thing just look a lot better so the
12514
07:28:56,478 --> 07:28:58,638
stats html function yeah it's getting
12515
07:28:58,638 --> 07:29:00,958
definitely pretty intense i can feel
12516
07:29:00,958 --> 07:29:02,878
that but uh you know if you're doing
12517
07:29:02,878 --> 07:29:04,398
this and you
12518
07:29:04,398 --> 07:29:05,920
you know you got up to this part like
12519
07:29:05,920 --> 07:29:08,080
you're doing a good job so just hang in
12520
07:29:08,080 --> 07:29:10,638
there and honestly what we could do is
12521
07:29:10,638 --> 07:29:12,718
we could probably turn this function get
12522
07:29:12,718 --> 07:29:15,040
stats html into like
12523
07:29:15,040 --> 07:29:16,240
get
12524
07:29:16,240 --> 07:29:17,920
hero info
12525
07:29:17,920 --> 07:29:20,240
you know get hero info html or something
12526
07:29:20,240 --> 07:29:22,558
because we can just create the whole
12527
07:29:22,558 --> 07:29:24,558
the entire html in that function which
12528
07:29:24,558 --> 07:29:26,718
will be really nice we just hand over
12529
07:29:26,718 --> 07:29:28,958
all the html elements so it's like the
12530
07:29:28,958 --> 07:29:30,558
hero name right
12531
07:29:30,558 --> 07:29:32,638
well that's just character.name so for
12532
07:29:32,638 --> 07:29:34,878
example if i copied this and i brought
12533
07:29:34,878 --> 07:29:36,240
it down here
12534
07:29:36,240 --> 07:29:39,440
this is this would just become character
12535
07:29:39,440 --> 07:29:41,360
dot name so that should take care of
12536
07:29:41,360 --> 07:29:42,240
that
12537
07:29:42,240 --> 07:29:45,280
and then we actually update the div at
12538
07:29:45,280 --> 07:29:47,520
the end and we create we do this like
12539
07:29:47,520 --> 07:29:49,680
image thing right here
12540
07:29:49,680 --> 07:29:51,120
so this image thing that we're doing
12541
07:29:51,120 --> 07:29:51,920
here
12542
07:29:51,920 --> 07:29:53,120
well that would be really nice to
12543
07:29:53,120 --> 07:29:56,000
actually do it handle that here as well
12544
07:29:56,000 --> 07:29:58,398
okay so let's try handling that here i'm
12545
07:29:58,398 --> 07:30:01,680
gonna say character.json
12546
07:30:01,680 --> 07:30:05,120
image url okay this will be a template
12547
07:30:05,120 --> 07:30:06,798
literal like this
12548
07:30:06,798 --> 07:30:08,958
just a const
12549
07:30:08,958 --> 07:30:12,478
image so this will be the image uh html
12550
07:30:12,478 --> 07:30:14,718
okay and then we have this final html
12551
07:30:14,718 --> 07:30:17,600
that like yeah and then here we go ahead
12552
07:30:17,600 --> 07:30:19,200
and we um
12553
07:30:19,200 --> 07:30:22,398
update this so yeah get stats
12554
07:30:22,398 --> 07:30:24,558
so we can actually rename this function
12555
07:30:24,558 --> 07:30:26,558
as to show stats because really what
12556
07:30:26,558 --> 07:30:28,240
this function is doing is showing the
12557
07:30:28,240 --> 07:30:30,638
stats on the screen
12558
07:30:30,638 --> 07:30:33,360
um or showing the
12559
07:30:33,360 --> 07:30:36,718
hero info on the screen so we could
12560
07:30:36,718 --> 07:30:38,478
right click this and rename all
12561
07:30:38,478 --> 07:30:40,718
occurrences we could say show
12562
07:30:40,718 --> 07:30:42,478
hero info
12563
07:30:42,478 --> 07:30:44,320
it's a show hero info so this is
12564
07:30:44,320 --> 07:30:45,760
basically the function that's actually
12565
07:30:45,760 --> 07:30:48,878
going to show this here the info
12566
07:30:48,878 --> 07:30:50,638
so i think that's like probably a much
12567
07:30:50,638 --> 07:30:52,638
better name for it and then it would
12568
07:30:52,638 --> 07:30:55,120
make sense if that's the function that
12569
07:30:55,120 --> 07:30:57,200
actually updates
12570
07:30:57,200 --> 07:30:59,200
uh the entire app as well so i'm going
12571
07:30:59,200 --> 07:31:00,958
to copy this for now we're going to come
12572
07:31:00,958 --> 07:31:03,360
down here and i'm going to paste this
12573
07:31:03,360 --> 07:31:04,558
right over here
12574
07:31:04,558 --> 07:31:05,840
so this function doesn't even need to
12575
07:31:05,840 --> 07:31:08,638
actually return this return anything
12576
07:31:08,638 --> 07:31:10,398
stats
12577
07:31:10,398 --> 07:31:11,360
boom
12578
07:31:11,360 --> 07:31:14,398
and in so for stats we can actually just
12579
07:31:14,398 --> 07:31:16,558
do the dot join right here so i can
12580
07:31:16,558 --> 07:31:19,360
actually just say that join so then it
12581
07:31:19,360 --> 07:31:21,200
it just turns this entire thing into
12582
07:31:21,200 --> 07:31:23,360
that long string instead of keeping it
12583
07:31:23,360 --> 07:31:24,798
as an array
12584
07:31:24,798 --> 07:31:26,958
and then this hero image div dot inner
12585
07:31:26,958 --> 07:31:29,360
html well this just becomes something
12586
07:31:29,360 --> 07:31:30,958
simple
12587
07:31:30,958 --> 07:31:33,840
we can actually even remove this guy now
12588
07:31:33,840 --> 07:31:35,520
and actually just say
12589
07:31:35,520 --> 07:31:36,478
image
12590
07:31:36,478 --> 07:31:39,440
so this just becomes this nice little
12591
07:31:39,440 --> 07:31:42,718
html so we get we make the name html we
12592
07:31:42,718 --> 07:31:44,478
make the image html
12593
07:31:44,478 --> 07:31:47,440
and then we make the stats.html
12594
07:31:47,440 --> 07:31:49,280
and then we just concatenate all of
12595
07:31:49,280 --> 07:31:52,160
those together and update the hero image
12596
07:31:52,160 --> 07:31:54,000
we don't need to we don't need this
12597
07:31:54,000 --> 07:31:56,240
function to return anything right show
12598
07:31:56,240 --> 07:31:57,840
hero info
12599
07:31:57,840 --> 07:32:02,798
and we basically get rid of all of this
12600
07:32:02,798 --> 07:32:04,478
all of this here
12601
07:32:04,478 --> 07:32:07,040
and we can just simply call the show
12602
07:32:07,040 --> 07:32:09,600
hero info function so now this function
12603
07:32:09,600 --> 07:32:12,000
just became really easy to look at right
12604
07:32:12,000 --> 07:32:13,600
get superhero
12605
07:32:13,600 --> 07:32:15,600
once you get the superhero
12606
07:32:15,600 --> 07:32:18,320
you just pass that superhero
12607
07:32:18,320 --> 07:32:22,320
jace uh con superhero is equal to json
12608
07:32:22,320 --> 07:32:23,840
you just pass that
12609
07:32:23,840 --> 07:32:26,320
hero or that character or whatever
12610
07:32:26,320 --> 07:32:28,000
and that this just shows it on the
12611
07:32:28,000 --> 07:32:29,680
screen so let's actually try this let's
12612
07:32:29,680 --> 07:32:32,160
run this let's say random hero all right
12613
07:32:32,160 --> 07:32:33,840
so it didn't it's not coming up on the
12614
07:32:33,840 --> 07:32:36,320
screen let's figure that out real quick
12615
07:32:36,320 --> 07:32:37,440
so what i'm going to do is i'm just
12616
07:32:37,440 --> 07:32:40,558
going to say console.log
12617
07:32:40,558 --> 07:32:43,680
and let's see if uh name is working as
12618
07:32:43,680 --> 07:32:46,080
expected so let's try name
12619
07:32:46,080 --> 07:32:48,478
let's run random hero yep oh actually
12620
07:32:48,478 --> 07:32:50,320
it's working i just had to like hit the
12621
07:32:50,320 --> 07:32:52,638
run i just think i never hit run
12622
07:32:52,638 --> 07:32:54,958
now let's hit random hero and look at
12623
07:32:54,958 --> 07:32:57,280
that random here is showing up
12624
07:32:57,280 --> 07:32:59,440
right and then intelligence strength
12625
07:32:59,440 --> 07:33:01,680
speed durability all of that showing up
12626
07:33:01,680 --> 07:33:04,878
exactly how we want let me remove
12627
07:33:04,878 --> 07:33:06,638
all of this here
12628
07:33:06,638 --> 07:33:09,040
so now it's really easy to read get the
12629
07:33:09,040 --> 07:33:10,558
superhero
12630
07:33:10,558 --> 07:33:13,440
show the superhero that's it now this
12631
07:33:13,440 --> 07:33:16,240
function is reusable and i'm gonna give
12632
07:33:16,240 --> 07:33:18,398
you that as an exercise so try that
12633
07:33:18,398 --> 07:33:19,520
right now
12634
07:33:19,520 --> 07:33:22,160
see if you could take this
12635
07:33:22,160 --> 07:33:24,798
what we did right here and add that to
12636
07:33:24,798 --> 07:33:26,558
search functionality so when i type in
12637
07:33:26,558 --> 07:33:28,558
batman and i hit search
12638
07:33:28,558 --> 07:33:30,398
make it so it actually shows me the
12639
07:33:30,398 --> 07:33:33,520
batman name it shows me the batman image
12640
07:33:33,520 --> 07:33:36,398
and it shows me the batman stats so see
12641
07:33:36,398 --> 07:33:38,478
if you could do that on your own
12642
07:33:38,478 --> 07:33:40,320
i'll give you a hint it's you're pretty
12643
07:33:40,320 --> 07:33:41,920
much only going to be writing it's going
12644
07:33:41,920 --> 07:33:43,840
to be just a few lines of code and it'll
12645
07:33:43,840 --> 07:33:46,080
do the entire thing for you so i'll give
12646
07:33:46,080 --> 07:33:47,760
you about five seconds see if you could
12647
07:33:47,760 --> 07:33:49,520
try to figure out i mean pause the video
12648
07:33:49,520 --> 07:33:50,718
and see if you could try to figure it
12649
07:33:50,718 --> 07:33:53,040
out five four
12650
07:33:53,040 --> 07:33:54,000
three
12651
07:33:54,000 --> 07:33:54,878
two
12652
07:33:54,878 --> 07:33:56,320
one
12653
07:33:56,320 --> 07:33:58,638
okay let's go ahead and do this together
12654
07:33:58,638 --> 07:34:01,440
now so this is going to be really really
12655
07:34:01,440 --> 07:34:03,920
really really nice okay because watch
12656
07:34:03,920 --> 07:34:05,600
what we're gonna do
12657
07:34:05,600 --> 07:34:07,520
let's go to our search superhero
12658
07:34:07,520 --> 07:34:08,878
function
12659
07:34:08,878 --> 07:34:11,120
and i'm gonna delete everything from
12660
07:34:11,120 --> 07:34:12,000
here
12661
07:34:12,000 --> 07:34:14,798
okay and remove everything
12662
07:34:14,798 --> 07:34:18,000
right and we already have our hero here
12663
07:34:18,000 --> 07:34:20,478
which is the first item
12664
07:34:20,478 --> 07:34:24,080
i'm just gonna call our show hero info
12665
07:34:24,080 --> 07:34:26,240
and i'm gonna pass in the hero that we
12666
07:34:26,240 --> 07:34:27,440
have
12667
07:34:27,440 --> 07:34:29,360
okay and let's give it a try now so
12668
07:34:29,360 --> 07:34:31,520
let's search
12669
07:34:31,520 --> 07:34:33,200
uh batman
12670
07:34:33,200 --> 07:34:34,798
and let's hit search
12671
07:34:34,798 --> 07:34:37,040
and look at that it's already working
12672
07:34:37,040 --> 07:34:38,558
right this is kind of
12673
07:34:38,558 --> 07:34:40,558
pretty unbelievable but if you actually
12674
07:34:40,558 --> 07:34:42,798
open this app up
12675
07:34:42,798 --> 07:34:46,160
and you go batman and you hit search
12676
07:34:46,160 --> 07:34:47,760
boom
12677
07:34:47,760 --> 07:34:50,320
let's go spider-man
12678
07:34:50,320 --> 07:34:53,520
boom let's go thanos
12679
07:34:53,520 --> 07:34:54,180
boom
12680
07:34:54,180 --> 07:34:56,398
[Music]
12681
07:34:56,398 --> 07:34:58,240
i mean this is amazing or you could get
12682
07:34:58,240 --> 07:35:00,958
a random hero and both them both of them
12683
07:35:00,958 --> 07:35:03,440
work so just to explain a little bit
12684
07:35:03,440 --> 07:35:05,280
about how we were able to make it work
12685
07:35:05,280 --> 07:35:07,760
that easily is look we already have
12686
07:35:07,760 --> 07:35:10,160
access to the hero right here so then
12687
07:35:10,160 --> 07:35:12,558
you just pass that hero to show hero
12688
07:35:12,558 --> 07:35:15,600
info and it knows what to do already
12689
07:35:15,600 --> 07:35:16,798
it knows that it's going to get a
12690
07:35:16,798 --> 07:35:18,080
character it knows it can do
12691
07:35:18,080 --> 07:35:19,760
character.name it knows how to get the
12692
07:35:19,760 --> 07:35:22,240
image url it knows how to like
12693
07:35:22,240 --> 07:35:24,558
turn the thing into
12694
07:35:24,558 --> 07:35:28,160
object.keys map through it get the stats
12695
07:35:28,160 --> 07:35:30,320
you know get all of these nice emojis
12696
07:35:30,320 --> 07:35:32,320
that we have here it knows how to do all
12697
07:35:32,320 --> 07:35:33,680
of that
12698
07:35:33,680 --> 07:35:35,280
and so then it just kind of
12699
07:35:35,280 --> 07:35:38,000
does all of that for you and then you're
12700
07:35:38,000 --> 07:35:39,760
you're good you're just essentially
12701
07:35:39,760 --> 07:35:42,000
reusing a function that you've created
12702
07:35:42,000 --> 07:35:43,920
that's the nice thing about reusing
12703
07:35:43,920 --> 07:35:45,520
functions right
12704
07:35:45,520 --> 07:35:46,718
um
12705
07:35:46,718 --> 07:35:49,200
it it like really is a game changer when
12706
07:35:49,200 --> 07:35:50,878
you can reuse like your existing
12707
07:35:50,878 --> 07:35:52,478
functions
12708
07:35:52,478 --> 07:35:54,798
all right cool so now just to kind of
12709
07:35:54,798 --> 07:35:59,360
make our app pretty i'll just say like
12710
07:35:59,360 --> 07:36:02,160
um at the top
12711
07:36:02,160 --> 07:36:04,958
kind of like right here and i'll say
12712
07:36:04,958 --> 07:36:05,370
um
12713
07:36:05,370 --> 07:36:07,120
[Music]
12714
07:36:07,120 --> 07:36:07,540
you know
12715
07:36:07,540 --> 07:36:10,160
[Music]
12716
07:36:10,160 --> 07:36:13,200
superhero app
12717
07:36:13,200 --> 07:36:14,718
and then
12718
07:36:14,718 --> 07:36:18,638
for instructions we'll just say like
12719
07:36:18,878 --> 07:36:23,200
you can search for a superhero like
12720
07:36:23,200 --> 07:36:25,280
batman
12721
07:36:25,280 --> 07:36:28,320
or thanos
12722
07:36:28,320 --> 07:36:29,520
or
12723
07:36:29,520 --> 07:36:30,878
simply
12724
07:36:30,878 --> 07:36:32,638
hit
12725
07:36:32,638 --> 07:36:34,398
all right so superhero app you can
12726
07:36:34,398 --> 07:36:36,080
search for a superhero like batman or
12727
07:36:36,080 --> 07:36:37,760
thanos or you can generate a random hero
12728
07:36:37,760 --> 07:36:40,718
so now the user goes to this app they
12729
07:36:40,718 --> 07:36:42,478
understand kind of how it works and what
12730
07:36:42,478 --> 07:36:45,120
it means so hit random hero
12731
07:36:45,120 --> 07:36:47,600
all right let's open this up here
12732
07:36:47,600 --> 07:36:49,040
superhero app you can search for a
12733
07:36:49,040 --> 07:36:50,320
superhero
12734
07:36:50,320 --> 07:36:52,080
or you can generate a random hero so
12735
07:36:52,080 --> 07:36:54,398
let's try random hero
12736
07:36:54,398 --> 07:36:56,320
right boom we're pulling random heroes
12737
07:36:56,320 --> 07:36:58,320
and this is like going to an api pulling
12738
07:36:58,320 --> 07:37:00,398
the data from there or i can actually
12739
07:37:00,398 --> 07:37:02,798
say you know bane let's see if that
12740
07:37:02,798 --> 07:37:03,760
works
12741
07:37:03,760 --> 07:37:06,160
you know thanos i know that works ooh
12742
07:37:06,160 --> 07:37:08,718
bane has nice combat
12743
07:37:08,718 --> 07:37:11,360
why is this power like only 51 do we
12744
07:37:11,360 --> 07:37:13,440
have superman here
12745
07:37:13,440 --> 07:37:16,558
oh cyborg superman that is badass
12746
07:37:16,558 --> 07:37:19,040
durability 100
12747
07:37:19,040 --> 07:37:22,558
power 100 speed 93 strength
12748
07:37:22,558 --> 07:37:23,840
i mean yeah but i don't really like
12749
07:37:23,840 --> 07:37:25,600
superman because he's just overpowered
12750
07:37:25,600 --> 07:37:27,600
it's like oh he just has like literally
12751
07:37:27,600 --> 07:37:29,600
every power on the planet it's like
12752
07:37:29,600 --> 07:37:32,080
how's that a fun superhero
12753
07:37:32,080 --> 07:37:34,080
i need my superheroes to kind of be like
12754
07:37:34,080 --> 07:37:36,320
weak but then use intelligence and
12755
07:37:36,320 --> 07:37:39,040
that's pretty cool
12756
07:37:39,520 --> 07:37:41,280
um or maybe that's how i was because i
12757
07:37:41,280 --> 07:37:43,440
went to chess club
12758
07:37:43,440 --> 07:37:45,760
and i resonate with those heroes more so
12759
07:37:45,760 --> 07:37:48,478
there you go we got this app it's fully
12760
07:37:48,478 --> 07:37:49,840
working
12761
07:37:49,840 --> 07:37:51,200
it's doing everything that it needs to
12762
07:37:51,200 --> 07:37:52,958
do now what i'm going to do is i'm going
12763
07:37:52,958 --> 07:37:55,440
to move it over for you inside of our
12764
07:37:55,440 --> 07:37:58,398
javascript course so you have access to
12765
07:37:58,398 --> 07:38:00,080
this under api
12766
07:38:00,080 --> 07:38:01,760
i'm going to create a new folder and
12767
07:38:01,760 --> 07:38:04,718
this is going to be called superhero
12768
07:38:04,718 --> 07:38:06,958
superhero app or whatever
12769
07:38:06,958 --> 07:38:09,718
and in here we're going to have a
12770
07:38:09,718 --> 07:38:11,440
index.html
12771
07:38:11,440 --> 07:38:13,440
and you don't need to follow along on
12772
07:38:13,440 --> 07:38:15,520
this one i'm just creating this for you
12773
07:38:15,520 --> 07:38:18,080
so you you know that this is available
12774
07:38:18,080 --> 07:38:20,760
to you and so we're gonna paste the
12775
07:38:20,760 --> 07:38:24,000
index.html in here we'll go here copy
12776
07:38:24,000 --> 07:38:26,160
the script so command a command c or
12777
07:38:26,160 --> 07:38:28,558
ctrl a control c
12778
07:38:28,558 --> 07:38:30,798
and you're gonna paste that in here
12779
07:38:30,798 --> 07:38:32,320
and then i'm just going to link it all
12780
07:38:32,320 --> 07:38:34,398
right so now you should actually be able
12781
07:38:34,398 --> 07:38:37,040
to go to the home page here
12782
07:38:37,040 --> 07:38:39,360
and you're actually going to see
12783
07:38:39,360 --> 07:38:42,240
get super heroes right over here
12784
07:38:42,240 --> 07:38:43,920
right and you'll be able to click in to
12785
07:38:43,920 --> 07:38:45,680
get superheroes
12786
07:38:45,680 --> 07:38:47,520
and then the app should show up here so
12787
07:38:47,520 --> 07:38:48,878
you could do random here and it'll
12788
07:38:48,878 --> 07:38:50,718
generate a random hero
12789
07:38:50,718 --> 07:38:52,798
or you could ask for
12790
07:38:52,798 --> 07:38:54,798
thanos
12791
07:38:54,798 --> 07:38:56,080
and search
12792
07:38:56,080 --> 07:38:59,280
and it'll bring up thanos for you okay
12793
07:38:59,280 --> 07:39:00,958
and if you want to make this app a
12794
07:39:00,958 --> 07:39:03,600
little bit more robust meaning like
12795
07:39:03,600 --> 07:39:06,478
well actually i think if you tried
12796
07:39:06,478 --> 07:39:08,718
thanos let's see if that works
12797
07:39:08,718 --> 07:39:10,718
search oh that still works so that's
12798
07:39:10,718 --> 07:39:11,840
pretty cool
12799
07:39:11,840 --> 07:39:13,520
spider
12800
07:39:13,520 --> 07:39:14,478
man
12801
07:39:14,478 --> 07:39:15,760
let's try
12802
07:39:15,760 --> 07:39:17,920
okay so the app is actually smart it can
12803
07:39:17,920 --> 07:39:19,600
it can detect that normally what you do
12804
07:39:19,600 --> 07:39:23,360
is you call dot to lower case on this so
12805
07:39:23,360 --> 07:39:24,958
then like whatever user types it just
12806
07:39:24,958 --> 07:39:26,638
lowercases it and then you do whatever
12807
07:39:26,638 --> 07:39:28,718
you want with it but this is fine
12808
07:39:28,718 --> 07:39:30,398
so there you go your app should be
12809
07:39:30,398 --> 07:39:32,878
working here and should be available to
12810
07:39:32,878 --> 07:39:33,760
you
12811
07:39:33,760 --> 07:39:35,760
in your javascript course you'll be
12812
07:39:35,760 --> 07:39:37,520
you'll be able to click it and see how
12813
07:39:37,520 --> 07:39:40,080
it functions and uh this will be the
12814
07:39:40,080 --> 07:39:42,080
solution file for you okay so the
12815
07:39:42,080 --> 07:39:44,638
solution will be available for you here
12816
07:39:44,638 --> 07:39:46,160
and you can just kind of go and play
12817
07:39:46,160 --> 07:39:48,878
around with it and use it or for bonus
12818
07:39:48,878 --> 07:39:51,120
points you could replace this with your
12819
07:39:51,120 --> 07:39:53,680
own code okay so if you want to be real
12820
07:39:53,680 --> 07:39:56,320
pro go ahead and copy paste your own
12821
07:39:56,320 --> 07:39:57,920
code that you just did in the other
12822
07:39:57,920 --> 07:40:02,000
repel right paste it over it and
12823
07:40:02,000 --> 07:40:04,320
see if this still works and if it does
12824
07:40:04,320 --> 07:40:06,160
you're a real champ because i want you
12825
07:40:06,160 --> 07:40:08,798
to have this entire javascript course
12826
07:40:08,798 --> 07:40:11,200
should essentially turn into all your
12827
07:40:11,200 --> 07:40:14,240
code everywhere here okay so we've
12828
07:40:14,240 --> 07:40:15,920
covered a lot
12829
07:40:15,920 --> 07:40:18,638
i hope that you've enjoyed it so far i
12830
07:40:18,638 --> 07:40:20,878
mean this was a lot of fun covering apis
12831
07:40:20,878 --> 07:40:23,200
in so much detail and building a mini
12832
07:40:23,200 --> 07:40:24,478
project
12833
07:40:24,478 --> 07:40:25,760
i'll see you soon
12834
07:40:25,760 --> 07:40:27,360
i promise
12835
07:40:27,360 --> 07:40:30,398
you will be great i promise you're going
12836
07:40:30,398 --> 07:40:32,638
to be a champion developer one day and i
12837
07:40:32,638 --> 07:40:35,040
promise i'm going to teach you promises
12838
07:40:35,040 --> 07:40:37,440
let's get right into it
12839
07:40:37,440 --> 07:40:39,520
so promises are
12840
07:40:39,520 --> 07:40:41,760
kind of tricky to understand it throws
12841
07:40:41,760 --> 07:40:43,600
people off a lot but you and i are going
12842
07:40:43,600 --> 07:40:44,798
to learn it together just go to
12843
07:40:44,798 --> 07:40:46,398
replay.com make sure you're here
12844
07:40:46,398 --> 07:40:49,200
replay.com this is the url and then
12845
07:40:49,200 --> 07:40:51,280
we're just going to open up a new rupple
12846
07:40:51,280 --> 07:40:54,638
with html css javascript and let's just
12847
07:40:54,638 --> 07:40:56,080
say learning
12848
07:40:56,080 --> 07:40:59,360
promises okay and hit create rubble
12849
07:40:59,360 --> 07:41:00,478
so we're just going to kind of start
12850
07:41:00,478 --> 07:41:01,920
from scratch
12851
07:41:01,920 --> 07:41:02,878
and
12852
07:41:02,878 --> 07:41:05,520
we're going to walk through kind of
12853
07:41:05,520 --> 07:41:07,920
real real basics of promises okay so
12854
07:41:07,920 --> 07:41:10,160
promises introduces the concept of
12855
07:41:10,160 --> 07:41:12,320
asynchronous programming all right it's
12856
07:41:12,320 --> 07:41:14,080
a complicated word
12857
07:41:14,080 --> 07:41:15,520
and i know we've talked a little bit
12858
07:41:15,520 --> 07:41:17,600
about promises already but now we're
12859
07:41:17,600 --> 07:41:20,240
gonna start deep diving into promises
12860
07:41:20,240 --> 07:41:22,878
and looking at it at like bare bones
12861
07:41:22,878 --> 07:41:25,040
basics and starting from there so
12862
07:41:25,040 --> 07:41:26,878
asynchronous programming is some piece
12863
07:41:26,878 --> 07:41:29,600
of code that's going to run out of order
12864
07:41:29,600 --> 07:41:30,958
okay it's going to happen at a random
12865
07:41:30,958 --> 07:41:32,478
time so the way that i want you to kind
12866
07:41:32,478 --> 07:41:34,878
of think about it is like this like
12867
07:41:34,878 --> 07:41:38,320
imagine you're at a restaurant and you
12868
07:41:38,320 --> 07:41:41,040
order some food at the restaurant now
12869
07:41:41,040 --> 07:41:42,958
when you order that food you're still
12870
07:41:42,958 --> 07:41:44,798
continuing your conversation with
12871
07:41:44,798 --> 07:41:46,958
whoever you're sitting and eating food
12872
07:41:46,958 --> 07:41:49,840
with while that food is being prepared
12873
07:41:49,840 --> 07:41:52,240
you're still talking once the food is
12874
07:41:52,240 --> 07:41:54,160
prepared the server will bring it up to
12875
07:41:54,160 --> 07:41:56,798
you or the server will say hey no soup
12876
07:41:56,798 --> 07:41:59,600
today right that's what they'll tell you
12877
07:41:59,600 --> 07:42:03,680
so let's go ahead and mimic that type of
12878
07:42:03,680 --> 07:42:05,280
that type of
12879
07:42:05,280 --> 07:42:07,520
thing but inside of code
12880
07:42:07,520 --> 07:42:09,440
okay so i'm going to go ahead and zoom
12881
07:42:09,440 --> 07:42:11,680
in here so you can actually see a lot
12882
07:42:11,680 --> 07:42:14,638
more clearly let's do it like this
12883
07:42:14,638 --> 07:42:16,958
all right and let's say that you're the
12884
07:42:16,958 --> 07:42:19,200
server or let's say that there is a
12885
07:42:19,200 --> 07:42:20,160
server
12886
07:42:20,160 --> 07:42:21,920
and you're going to
12887
07:42:21,920 --> 07:42:23,920
you order
12888
07:42:23,920 --> 07:42:26,878
let's say tomato soup tomato soup
12889
07:42:26,878 --> 07:42:28,240
meanwhile
12890
07:42:28,240 --> 07:42:32,240
you continue your converse convo with a
12891
07:42:32,240 --> 07:42:33,680
friend
12892
07:42:33,680 --> 07:42:35,840
so your life basically continues so this
12893
07:42:35,840 --> 07:42:38,080
is your other code running whichever
12894
07:42:38,080 --> 07:42:40,080
code is ready to kind of go
12895
07:42:40,080 --> 07:42:42,000
and then once this uh
12896
07:42:42,000 --> 07:42:44,398
your tomato soup is done
12897
07:42:44,398 --> 07:42:46,320
your server
12898
07:42:46,320 --> 07:42:48,160
brings it to you
12899
07:42:48,160 --> 07:42:49,920
in this case we'll just say
12900
07:42:49,920 --> 07:42:53,040
if your problem is resolved okay the the
12901
07:42:53,040 --> 07:42:54,958
order that you requested if your order
12902
07:42:54,958 --> 07:42:57,840
is resolved then your server will bring
12903
07:42:57,840 --> 07:43:00,878
soup your server brings you
12904
07:43:00,878 --> 07:43:02,398
soup
12905
07:43:02,398 --> 07:43:05,520
okay if it is not whoops if it's not
12906
07:43:05,520 --> 07:43:06,638
resolved
12907
07:43:06,638 --> 07:43:07,600
paste
12908
07:43:07,600 --> 07:43:09,520
what's going to happen
12909
07:43:09,520 --> 07:43:11,040
is
12910
07:43:11,040 --> 07:43:13,440
rejected okay so this is other situation
12911
07:43:13,440 --> 07:43:15,520
that can happen and your server will
12912
07:43:15,520 --> 07:43:16,878
tell you like
12913
07:43:16,878 --> 07:43:19,680
no soup today okay so maybe like these
12914
07:43:19,680 --> 07:43:21,040
are the two things that can happen
12915
07:43:21,040 --> 07:43:23,680
either you get soup or your server comes
12916
07:43:23,680 --> 07:43:25,440
in to you and tells you hey
12917
07:43:25,440 --> 07:43:28,160
no soup today so these are the two
12918
07:43:28,160 --> 07:43:30,080
different type of scenarios that can
12919
07:43:30,080 --> 07:43:32,638
happen now in code
12920
07:43:32,638 --> 07:43:34,558
we're going to try to go ahead and mimic
12921
07:43:34,558 --> 07:43:36,080
that and kind of write it but first
12922
07:43:36,080 --> 07:43:38,320
things first i want to teach you how to
12923
07:43:38,320 --> 07:43:40,398
write asynchronous code right so
12924
07:43:40,398 --> 07:43:42,320
if you've ever done a fetch request or
12925
07:43:42,320 --> 07:43:44,000
api request that's an example of
12926
07:43:44,000 --> 07:43:46,798
asynchronous code the way to mimic that
12927
07:43:46,798 --> 07:43:48,638
is usually you're waiting for something
12928
07:43:48,638 --> 07:43:50,320
to come back to you so the way we can
12929
07:43:50,320 --> 07:43:52,320
actually mimic that is
12930
07:43:52,320 --> 07:43:55,040
we can write a set timeout function and
12931
07:43:55,040 --> 07:43:57,520
what this does is it takes in a function
12932
07:43:57,520 --> 07:43:58,958
so let's say
12933
07:43:58,958 --> 07:44:02,160
whatever soup okay and just console
12934
07:44:02,160 --> 07:44:03,920
let's just console log soup for now
12935
07:44:03,920 --> 07:44:08,718
console log soup and we'll pass it 2000
12936
07:44:08,718 --> 07:44:11,040
milliseconds now what this means is
12937
07:44:11,040 --> 07:44:12,958
this function is gonna
12938
07:44:12,958 --> 07:44:14,958
wait for two seconds and then it's gonna
12939
07:44:14,958 --> 07:44:17,200
say soup so let's go one
12940
07:44:17,200 --> 07:44:18,160
two
12941
07:44:18,160 --> 07:44:20,798
and boom we got soup okay let's run it
12942
07:44:20,798 --> 07:44:22,798
again one
12943
07:44:22,798 --> 07:44:23,920
two
12944
07:44:23,920 --> 07:44:24,958
soup
12945
07:44:24,958 --> 07:44:27,200
so every single time two seconds happen
12946
07:44:27,200 --> 07:44:29,520
you get soup so two thousand
12947
07:44:29,520 --> 07:44:32,240
milliseconds is one second
12948
07:44:32,240 --> 07:44:34,798
one second
12949
07:44:34,840 --> 07:44:38,478
okay and so in other words what you can
12950
07:44:38,478 --> 07:44:41,520
say is if you have a function
12951
07:44:41,520 --> 07:44:43,040
okay so let's just say you have some
12952
07:44:43,040 --> 07:44:44,000
funk
12953
07:44:44,000 --> 07:44:46,398
and this function that you have it like
12954
07:44:46,398 --> 07:44:48,638
kind of prints out soup or whatever
12955
07:44:48,638 --> 07:44:51,200
you can just pass in your funk like this
12956
07:44:51,200 --> 07:44:54,478
let's call it waiting for soup paste it
12957
07:44:54,478 --> 07:44:55,440
in here
12958
07:44:55,440 --> 07:44:58,398
run it and this code is basically saying
12959
07:44:58,398 --> 07:45:00,240
hey set timeout
12960
07:45:00,240 --> 07:45:02,320
you're gonna you're gonna run this piece
12961
07:45:02,320 --> 07:45:05,520
of code aka this over here and i want
12962
07:45:05,520 --> 07:45:08,320
you to wait two seconds oh sorry this is
12963
07:45:08,320 --> 07:45:09,440
two seconds sorry two thousand
12964
07:45:09,440 --> 07:45:11,360
milliseconds is two seconds
12965
07:45:11,360 --> 07:45:13,440
one thousand milliseconds is one second
12966
07:45:13,440 --> 07:45:14,320
okay
12967
07:45:14,320 --> 07:45:16,320
uh my apologies on that
12968
07:45:16,320 --> 07:45:18,320
so you say hey i'm gonna wait for some
12969
07:45:18,320 --> 07:45:20,958
soup and two seconds i want you to print
12970
07:45:20,958 --> 07:45:23,200
that out so run i'm gonna wait one
12971
07:45:23,200 --> 07:45:25,840
second two seconds and then boom i get
12972
07:45:25,840 --> 07:45:28,398
soup let's run it again one
12973
07:45:28,398 --> 07:45:29,200
two
12974
07:45:29,200 --> 07:45:30,840
and i get soup
12975
07:45:30,840 --> 07:45:32,878
one two
12976
07:45:32,878 --> 07:45:34,958
and i get soup
12977
07:45:34,958 --> 07:45:36,958
now let's say you have other bits of
12978
07:45:36,958 --> 07:45:39,440
code so let's say you have some code
12979
07:45:39,440 --> 07:45:41,760
which is console log
12980
07:45:41,760 --> 07:45:43,840
you know you you get to the restaurant
12981
07:45:43,840 --> 07:45:46,000
with your date
12982
07:45:46,000 --> 07:45:48,638
and so you
12983
07:45:48,638 --> 07:45:51,440
start the convo with your
12984
07:45:51,440 --> 07:45:53,680
date right whoever that is you start the
12985
07:45:53,680 --> 07:45:54,798
convo
12986
07:45:54,798 --> 07:45:56,718
and then
12987
07:45:56,718 --> 07:45:59,440
you continue the convo right but now
12988
07:45:59,440 --> 07:46:01,120
this is the interesting that happens
12989
07:46:01,120 --> 07:46:03,440
right so watch this for example
12990
07:46:03,440 --> 07:46:05,360
and i'm going to take this
12991
07:46:05,360 --> 07:46:06,958
and paste it here
12992
07:46:06,958 --> 07:46:08,240
so
12993
07:46:08,240 --> 07:46:11,760
on line 10 you start the convo and line
12994
07:46:11,760 --> 07:46:13,440
12
12995
07:46:13,440 --> 07:46:16,080
is you it's gonna say still speaking so
12996
07:46:16,080 --> 07:46:18,080
what should happen well what you would
12997
07:46:18,080 --> 07:46:21,040
imagine to happen is i'm going to
12998
07:46:21,040 --> 07:46:22,798
what what i'm what you should imagine to
12999
07:46:22,798 --> 07:46:25,680
happen is this line runs first then this
13000
07:46:25,680 --> 07:46:27,920
line runs second and then this line
13001
07:46:27,920 --> 07:46:29,520
should run third
13002
07:46:29,520 --> 07:46:31,520
but watch what's gonna happen you start
13003
07:46:31,520 --> 07:46:33,600
the convo with your girlfriend or your
13004
07:46:33,600 --> 07:46:35,120
boyfriend whoever
13005
07:46:35,120 --> 07:46:37,760
and it says still speaking and then you
13006
07:46:37,760 --> 07:46:40,160
get soup that's really weird right so if
13007
07:46:40,160 --> 07:46:42,638
i just go and do this and make it really
13008
07:46:42,638 --> 07:46:44,240
clear this is one
13009
07:46:44,240 --> 07:46:46,320
this will print out two because this
13010
07:46:46,320 --> 07:46:48,160
goes in right here and this should be
13011
07:46:48,160 --> 07:46:50,638
three so you should you should expect to
13012
07:46:50,638 --> 07:46:52,398
see one two three but watch what happens
13013
07:46:52,398 --> 07:46:54,878
one and three run first and then two
13014
07:46:54,878 --> 07:46:58,000
comes out so it seems like if i had to
13015
07:46:58,000 --> 07:46:59,200
order this
13016
07:46:59,200 --> 07:47:02,000
it seems like this line of code seems
13017
07:47:02,000 --> 07:47:03,840
like this line of code
13018
07:47:03,840 --> 07:47:06,558
runs first and then this line of code
13019
07:47:06,558 --> 07:47:09,840
runs second and this line of code runs
13020
07:47:09,840 --> 07:47:12,240
third because this we're waiting for
13021
07:47:12,240 --> 07:47:14,080
this all right this is like kind of
13022
07:47:14,080 --> 07:47:16,718
running in the background now let's
13023
07:47:16,718 --> 07:47:19,760
model this in a more realistic way so in
13024
07:47:19,760 --> 07:47:22,398
the start you start your convo right you
13025
07:47:22,398 --> 07:47:24,398
sit down and start your convo and then
13026
07:47:24,398 --> 07:47:26,878
what we'll actually say is let's call it
13027
07:47:26,878 --> 07:47:28,878
order soup
13028
07:47:28,878 --> 07:47:30,718
so you sit down you start the
13029
07:47:30,718 --> 07:47:32,000
conversation
13030
07:47:32,000 --> 07:47:33,360
with your partner
13031
07:47:33,360 --> 07:47:35,920
line 11 you order your soup and then
13032
07:47:35,920 --> 07:47:38,240
you're still speaking right you continue
13033
07:47:38,240 --> 07:47:40,558
speaking so let's run this
13034
07:47:40,558 --> 07:47:42,638
you sit down you order your soup then
13035
07:47:42,638 --> 07:47:44,000
you wait a couple of seconds and then
13036
07:47:44,000 --> 07:47:45,680
soup is ready in reality of course the
13037
07:47:45,680 --> 07:47:47,200
soup is going to take longer than two
13038
07:47:47,200 --> 07:47:49,440
seconds to get ready but this is just to
13039
07:47:49,440 --> 07:47:53,280
show you that you can start a request
13040
07:47:53,280 --> 07:47:57,280
right right on this line is where you
13041
07:47:57,280 --> 07:48:00,718
basically start your request
13042
07:48:00,718 --> 07:48:02,958
and then
13043
07:48:02,958 --> 07:48:06,558
your request is ready at a point some at
13044
07:48:06,558 --> 07:48:08,558
some point later right you get the data
13045
07:48:08,558 --> 07:48:09,360
back
13046
07:48:09,360 --> 07:48:10,878
now when you're dealing with stuff like
13047
07:48:10,878 --> 07:48:15,280
this with timeout yes you will get back
13048
07:48:15,280 --> 07:48:16,558
you know kind of what you're looking for
13049
07:48:16,558 --> 07:48:18,320
but timeout is also not really good it's
13050
07:48:18,320 --> 07:48:21,200
called it's thread blocking meaning like
13051
07:48:21,200 --> 07:48:23,280
it will kind of stop you from often
13052
07:48:23,280 --> 07:48:25,280
times doing other things
13053
07:48:25,280 --> 07:48:27,200
so when you're dealing with real world
13054
07:48:27,200 --> 07:48:29,600
data like an api request it's going to
13055
07:48:29,600 --> 07:48:31,920
return it to you as a promise what
13056
07:48:31,920 --> 07:48:33,760
you're not going to get back
13057
07:48:33,760 --> 07:48:36,080
is this timeout thing okay
13058
07:48:36,080 --> 07:48:39,200
so let's go ahead and kind of model that
13059
07:48:39,200 --> 07:48:42,718
so let's create our first promise
13060
07:48:42,718 --> 07:48:44,398
so i'm going to basically go ahead and
13061
07:48:44,398 --> 07:48:46,718
say
13062
07:48:46,878 --> 07:48:49,360
const promise one
13063
07:48:49,360 --> 07:48:51,840
and it's gonna be a new promise okay and
13064
07:48:51,840 --> 07:48:55,200
this is a promise class we'll talk a lot
13065
07:48:55,200 --> 07:48:57,280
about classes don't worry if we haven't
13066
07:48:57,280 --> 07:49:00,000
already you know in in the javascript
13067
07:49:00,000 --> 07:49:01,440
course if you're watching it in the
13068
07:49:01,440 --> 07:49:03,040
javascript course there's gonna be a
13069
07:49:03,040 --> 07:49:05,440
timestamp for promises or if i have it
13070
07:49:05,440 --> 07:49:07,520
as a video on our youtube channel or
13071
07:49:07,520 --> 07:49:09,360
sorry not promises with classes go ahead
13072
07:49:09,360 --> 07:49:11,440
and watch that watch the part with the
13073
07:49:11,440 --> 07:49:13,840
classes because you'll then really get
13074
07:49:13,840 --> 07:49:14,798
how
13075
07:49:14,798 --> 07:49:16,798
like a promise works under the hood
13076
07:49:16,798 --> 07:49:18,240
because whenever you do new you're
13077
07:49:18,240 --> 07:49:19,840
actually creating um
13078
07:49:19,840 --> 07:49:20,878
like a
13079
07:49:20,878 --> 07:49:22,558
object from a class
13080
07:49:22,558 --> 07:49:24,080
okay so anyway
13081
07:49:24,080 --> 07:49:26,558
so promise takes in a function and that
13082
07:49:26,558 --> 07:49:28,398
function takes in two things it either
13083
07:49:28,398 --> 07:49:30,638
takes in a or a reject
13084
07:49:30,638 --> 07:49:32,878
and then it wants you to do something
13085
07:49:32,878 --> 07:49:34,478
with it okay so now what i'm going to do
13086
07:49:34,478 --> 07:49:36,958
is i'm going to say
13087
07:49:36,958 --> 07:49:38,638
set timeout
13088
07:49:38,638 --> 07:49:40,478
set timeout will take a function and
13089
07:49:40,478 --> 07:49:43,680
let's just say i'm going to
13090
07:49:43,680 --> 07:49:44,878
do this
13091
07:49:44,878 --> 07:49:46,958
and it says soup is ready
13092
07:49:46,958 --> 07:49:48,878
all right and i'll do the same thing
13093
07:49:48,878 --> 07:49:50,878
i'll wait two seconds
13094
07:49:50,878 --> 07:49:52,240
and then
13095
07:49:52,240 --> 07:49:54,240
what this code will do
13096
07:49:54,240 --> 07:49:57,600
is write actually inside of this timeout
13097
07:49:57,600 --> 07:49:59,120
let's go ahead and do this right inside
13098
07:49:59,120 --> 07:50:00,478
of this timeout so i'm going to do the
13099
07:50:00,478 --> 07:50:01,760
following so we're going to have a
13100
07:50:01,760 --> 07:50:03,120
timeout function
13101
07:50:03,120 --> 07:50:04,638
and inside of this timeout we're going
13102
07:50:04,638 --> 07:50:07,760
to say is ready and for now we'll set is
13103
07:50:07,760 --> 07:50:09,120
ready to true
13104
07:50:09,120 --> 07:50:11,040
so we're going to say hey if the soup is
13105
07:50:11,040 --> 07:50:13,920
ready and this is how you do ternary
13106
07:50:13,920 --> 07:50:16,160
operators by the way so i'll show you an
13107
07:50:16,160 --> 07:50:18,080
example of a ternary operator so if
13108
07:50:18,080 --> 07:50:19,760
let's say you have a
13109
07:50:19,760 --> 07:50:21,520
if
13110
07:50:21,520 --> 07:50:24,840
is ready equal equal true
13111
07:50:24,840 --> 07:50:28,638
then you know run this
13112
07:50:28,638 --> 07:50:31,040
um else
13113
07:50:31,040 --> 07:50:32,398
do this
13114
07:50:32,398 --> 07:50:34,320
all right so if you have this line of
13115
07:50:34,320 --> 07:50:36,320
code you could write it in different
13116
07:50:36,320 --> 07:50:37,600
ways the first thing you can do is
13117
07:50:37,600 --> 07:50:41,200
actually you can remove this because
13118
07:50:41,200 --> 07:50:43,920
if this is already true then you don't
13119
07:50:43,920 --> 07:50:46,240
even need to say equal equal true but if
13120
07:50:46,240 --> 07:50:47,840
you want to leave it here you can that's
13121
07:50:47,840 --> 07:50:50,000
not really going to change anything
13122
07:50:50,000 --> 07:50:51,520
and then what you could do is you could
13123
07:50:51,520 --> 07:50:53,840
turn it into a ternary operator as well
13124
07:50:53,840 --> 07:50:55,360
so you could say hey you could remove
13125
07:50:55,360 --> 07:50:56,798
this
13126
07:50:56,798 --> 07:50:59,360
and you could do if this is true do this
13127
07:50:59,360 --> 07:51:01,600
if it's false do this so that's what
13128
07:51:01,600 --> 07:51:03,360
we're doing here on line 20. we're
13129
07:51:03,360 --> 07:51:05,360
basically going to say hey if
13130
07:51:05,360 --> 07:51:07,920
is ready is true then i want you to do
13131
07:51:07,920 --> 07:51:08,958
hit
13132
07:51:08,958 --> 07:51:10,718
you know soup is ready
13133
07:51:10,718 --> 07:51:12,878
otherwise what i want you to do
13134
07:51:12,878 --> 07:51:15,440
this is where our function ends
13135
07:51:15,440 --> 07:51:16,718
right and then let's give it two
13136
07:51:16,718 --> 07:51:18,638
thousand second millisecond wait right
13137
07:51:18,638 --> 07:51:22,398
here otherwise i'm gonna say no soup
13138
07:51:22,398 --> 07:51:24,320
today okay so let's just say
13139
07:51:24,320 --> 07:51:25,840
no soup today
13140
07:51:25,840 --> 07:51:27,440
something like that
13141
07:51:27,440 --> 07:51:29,840
and i'll put a little check mark here
13142
07:51:29,840 --> 07:51:31,520
if you don't have emojis by the way you
13143
07:51:31,520 --> 07:51:33,520
can get this app called rocket on your
13144
07:51:33,520 --> 07:51:34,558
macbook
13145
07:51:34,558 --> 07:51:36,080
all right so now what this is going to
13146
07:51:36,080 --> 07:51:38,958
do is it's going to run and what i want
13147
07:51:38,958 --> 07:51:41,600
to do is instead of console.log for
13148
07:51:41,600 --> 07:51:43,360
promises what we do is we actually will
13149
07:51:43,360 --> 07:51:44,958
do resolve
13150
07:51:44,958 --> 07:51:47,920
or reject
13151
07:51:48,958 --> 07:51:52,320
all right so if uh like if it's a
13152
07:51:52,320 --> 07:51:54,478
right scenario and you want and it did
13153
07:51:54,478 --> 07:51:55,680
it correctly then you want to say
13154
07:51:55,680 --> 07:51:57,920
resolve like so if you did if you made a
13155
07:51:57,920 --> 07:52:00,000
let's say a api request and you got data
13156
07:52:00,000 --> 07:52:02,320
back you want to resolve that and return
13157
07:52:02,320 --> 07:52:03,040
it
13158
07:52:03,040 --> 07:52:05,680
if you didn't get data back for whatever
13159
07:52:05,680 --> 07:52:07,200
reason maybe there's a server error
13160
07:52:07,200 --> 07:52:08,958
maybe the data wasn't ready then you
13161
07:52:08,958 --> 07:52:11,440
want to reject it so in this case we're
13162
07:52:11,440 --> 07:52:14,240
going to say go ahead and reject no sub
13163
07:52:14,240 --> 07:52:16,638
today okay
13164
07:52:16,638 --> 07:52:21,040
so do this and this is going to be our
13165
07:52:21,040 --> 07:52:22,398
like first
13166
07:52:22,398 --> 07:52:25,440
promise that we have uh wrote
13167
07:52:25,440 --> 07:52:28,958
and i'll remove all of this
13168
07:52:28,958 --> 07:52:30,398
okay so remember
13169
07:52:30,398 --> 07:52:32,798
this is to just kind of visually explain
13170
07:52:32,798 --> 07:52:35,120
it to you how ternary operators work is
13171
07:52:35,120 --> 07:52:36,958
like this
13172
07:52:36,958 --> 07:52:40,558
okay so it's right here right
13173
07:52:40,558 --> 07:52:44,160
this part right here
13174
07:52:44,240 --> 07:52:47,798
is the condition
13175
07:52:48,398 --> 07:52:52,000
this part right here runs
13176
07:52:52,000 --> 07:52:56,398
if you have a true runs if true
13177
07:52:56,478 --> 07:52:59,280
and this part right here
13178
07:52:59,280 --> 07:53:02,320
runs if false
13179
07:53:02,320 --> 07:53:04,478
okay that's how
13180
07:53:04,478 --> 07:53:06,478
that's how ternary operators work okay
13181
07:53:06,478 --> 07:53:08,878
so if i put a box around this
13182
07:53:08,878 --> 07:53:10,878
the term for this
13183
07:53:10,878 --> 07:53:12,958
if you haven't seen this yet it's called
13184
07:53:12,958 --> 07:53:14,558
ternary
13185
07:53:14,558 --> 07:53:16,558
operators
13186
07:53:16,558 --> 07:53:18,798
so instead of if else condition you'll
13187
07:53:18,798 --> 07:53:20,718
have this like
13188
07:53:20,718 --> 07:53:22,958
more specifically it's that like
13189
07:53:22,958 --> 07:53:24,558
question mark part
13190
07:53:24,558 --> 07:53:26,718
and the colon here
13191
07:53:26,718 --> 07:53:30,638
okay cool so i have this promise here
13192
07:53:30,638 --> 07:53:32,320
and now let's see what happens when a
13193
07:53:32,320 --> 07:53:34,478
console log this promise so let's see
13194
07:53:34,478 --> 07:53:35,520
what happens
13195
07:53:35,520 --> 07:53:37,040
promise one
13196
07:53:37,040 --> 07:53:40,320
and i'm gonna go ahead and run this
13197
07:53:40,320 --> 07:53:42,320
and notice what i get back so i'll
13198
07:53:42,320 --> 07:53:44,718
comment out all of these lines here
13199
07:53:44,718 --> 07:53:46,958
up at the top
13200
07:53:46,958 --> 07:53:49,280
let's run this and you notice i actually
13201
07:53:49,280 --> 07:53:52,718
get back uh something called a promise
13202
07:53:52,718 --> 07:53:55,360
so wait how is that useful
13203
07:53:55,360 --> 07:53:57,680
i did this and i'm just getting back
13204
07:53:57,680 --> 07:53:58,958
something that says promise what the
13205
07:53:58,958 --> 07:54:00,160
heck does that mean
13206
07:54:00,160 --> 07:54:02,398
all that's saying is hey you have a
13207
07:54:02,398 --> 07:54:04,240
promise but that promise has not been
13208
07:54:04,240 --> 07:54:07,200
fulfilled yet it just started so it
13209
07:54:07,200 --> 07:54:08,638
started and like i'm just letting you
13210
07:54:08,638 --> 07:54:11,120
know that it started now i need to tell
13211
07:54:11,120 --> 07:54:12,878
it to do something once it's resolved so
13212
07:54:12,878 --> 07:54:14,478
remember it takes two seconds to resolve
13213
07:54:14,478 --> 07:54:16,398
but when i run this it instantly does
13214
07:54:16,398 --> 07:54:18,080
promise right
13215
07:54:18,080 --> 07:54:19,680
so what i want to do is i want to do a
13216
07:54:19,680 --> 07:54:22,320
dot then
13217
07:54:22,320 --> 07:54:24,558
and dot then is the special things that
13218
07:54:24,558 --> 07:54:25,920
a promise has
13219
07:54:25,920 --> 07:54:28,080
and what that does is if the promise
13220
07:54:28,080 --> 07:54:29,280
resolves
13221
07:54:29,280 --> 07:54:31,120
then you can call a dot then and get its
13222
07:54:31,120 --> 07:54:33,760
value so we can say
13223
07:54:33,760 --> 07:54:37,440
console log value like this okay and
13224
07:54:37,440 --> 07:54:39,040
let's run this and you're gonna see
13225
07:54:39,040 --> 07:54:41,440
first it says promise and then after two
13226
07:54:41,440 --> 07:54:43,680
seconds it says soup is ready if i make
13227
07:54:43,680 --> 07:54:45,520
this five seconds so it waits that'll
13228
07:54:45,520 --> 07:54:47,520
make it even more obvious so it just
13229
07:54:47,520 --> 07:54:49,120
started and it said promise now after
13230
07:54:49,120 --> 07:54:52,000
five seconds one two three four five
13231
07:54:52,000 --> 07:54:52,878
boom
13232
07:54:52,878 --> 07:54:56,320
soup is ready and it shows up
13233
07:54:56,320 --> 07:54:58,558
so you can see that this is working kind
13234
07:54:58,558 --> 07:55:01,520
of the way that we you know expected but
13235
07:55:01,520 --> 07:55:03,600
this dot then notation for you might be
13236
07:55:03,600 --> 07:55:05,520
weird but if you have done api requests
13237
07:55:05,520 --> 07:55:06,958
you'll recall
13238
07:55:06,958 --> 07:55:07,840
that
13239
07:55:07,840 --> 07:55:09,680
hey this is that's actually very similar
13240
07:55:09,680 --> 07:55:12,080
to api requests because that's where you
13241
07:55:12,080 --> 07:55:14,000
use the dot then thing and that's how
13242
07:55:14,000 --> 07:55:17,040
you actually pull the value out
13243
07:55:17,040 --> 07:55:18,320
now
13244
07:55:18,320 --> 07:55:20,160
what happens if the soup isn't ready so
13245
07:55:20,160 --> 07:55:21,840
i'm going to go on line 19 and i'm going
13246
07:55:21,840 --> 07:55:23,920
to change that to false what happens
13247
07:55:23,920 --> 07:55:26,240
when the soup is not ready
13248
07:55:26,240 --> 07:55:28,000
well watch what happens we're going to
13249
07:55:28,000 --> 07:55:32,000
wait five seconds one two three four
13250
07:55:32,000 --> 07:55:33,200
five
13251
07:55:33,200 --> 07:55:35,760
and nothing happened
13252
07:55:35,760 --> 07:55:38,558
what i don't see anything why did that
13253
07:55:38,558 --> 07:55:40,718
happen that is weird
13254
07:55:40,718 --> 07:55:42,638
it's because we're not actually
13255
07:55:42,638 --> 07:55:45,520
catching that mistake okay so let's go
13256
07:55:45,520 --> 07:55:47,760
right here
13257
07:55:47,760 --> 07:55:49,680
this is all gonna be one line but i'm
13258
07:55:49,680 --> 07:55:51,440
breaking it up into multiple different
13259
07:55:51,440 --> 07:55:53,760
lines so you can clearly see like what's
13260
07:55:53,760 --> 07:55:55,200
actually happening
13261
07:55:55,200 --> 07:55:56,558
okay
13262
07:55:56,558 --> 07:56:00,398
um and so i'm gonna say hey
13263
07:56:00,398 --> 07:56:03,760
get me the value on success so the dot
13264
07:56:03,760 --> 07:56:06,240
then is when success happens
13265
07:56:06,240 --> 07:56:08,558
okay and then there's a dot catch
13266
07:56:08,558 --> 07:56:11,120
so this is when an error happens
13267
07:56:11,120 --> 07:56:13,600
okay and so i'm gonna say
13268
07:56:13,600 --> 07:56:17,360
get me the value in this case
13269
07:56:17,360 --> 07:56:18,878
and let's run it
13270
07:56:18,878 --> 07:56:20,000
so
13271
07:56:20,000 --> 07:56:22,478
watch what happens in five seconds one
13272
07:56:22,478 --> 07:56:26,240
two three four five and boom no soup
13273
07:56:26,240 --> 07:56:29,440
today and that's happening because our
13274
07:56:29,440 --> 07:56:32,320
on catch is actually catching
13275
07:56:32,320 --> 07:56:35,760
the reject so whenever you have a reject
13276
07:56:35,760 --> 07:56:37,440
like this
13277
07:56:37,440 --> 07:56:39,760
that goes into catch
13278
07:56:39,760 --> 07:56:41,600
and you can actually pull that from the
13279
07:56:41,600 --> 07:56:43,600
value
13280
07:56:43,600 --> 07:56:44,558
so
13281
07:56:44,558 --> 07:56:47,200
let's go ahead and do
13282
07:56:47,200 --> 07:56:50,080
i'll say error
13283
07:56:50,798 --> 07:56:51,920
okay
13284
07:56:51,920 --> 07:56:54,638
and we'll just make it an object
13285
07:56:54,638 --> 07:56:56,478
error
13286
07:56:56,478 --> 07:56:59,040
error
13287
07:56:59,040 --> 07:57:02,240
and here we'll say success message and
13288
07:57:02,240 --> 07:57:05,120
i'll say success and an object like this
13289
07:57:05,120 --> 07:57:07,120
so we'll run this and now you'll see
13290
07:57:07,120 --> 07:57:09,360
this promise will run and then in five
13291
07:57:09,360 --> 07:57:11,840
seconds it will get get you
13292
07:57:11,840 --> 07:57:13,920
okay so in this case here it said error
13293
07:57:13,920 --> 07:57:16,320
no soup today okay
13294
07:57:16,320 --> 07:57:17,760
and what we'll do is we'll make this
13295
07:57:17,760 --> 07:57:19,680
function two seconds and what i want to
13296
07:57:19,680 --> 07:57:21,920
do here is instead of false or true i
13297
07:57:21,920 --> 07:57:24,398
want it to randomly select true or false
13298
07:57:24,398 --> 07:57:26,000
so i'm going to put true or false i'm
13299
07:57:26,000 --> 07:57:27,440
going to make an array and then we're
13300
07:57:27,440 --> 07:57:29,520
going to index that array if we index it
13301
07:57:29,520 --> 07:57:31,120
by zero we're going to get false if we
13302
07:57:31,120 --> 07:57:32,798
index it by one then we're going to get
13303
07:57:32,798 --> 07:57:35,040
true what i actually want to do here is
13304
07:57:35,040 --> 07:57:37,520
i want to index it
13305
07:57:37,520 --> 07:57:39,200
by something random right so i'm going
13306
07:57:39,200 --> 07:57:41,280
to say math.random
13307
07:57:41,280 --> 07:57:45,040
and then i'm going to
13308
07:57:45,040 --> 07:57:46,798
let's actually call math.floor
13309
07:57:46,798 --> 07:57:48,558
math.floor
13310
07:57:48,558 --> 07:57:52,000
and in that i'm going to say math.random
13311
07:57:52,000 --> 07:57:55,360
and we're going to multiply it by 2.
13312
07:57:55,360 --> 07:57:56,160
okay
13313
07:57:56,160 --> 07:57:57,440
so
13314
07:57:57,440 --> 07:58:00,240
this piece of code here will always make
13315
07:58:00,240 --> 07:58:03,280
sure i either get a 0 or a 1. that's all
13316
07:58:03,280 --> 07:58:05,840
that piece of code is doing
13317
07:58:05,840 --> 07:58:08,000
so this way i'll pick a random true or
13318
07:58:08,000 --> 07:58:10,000
false and so that way it'll randomly
13319
07:58:10,000 --> 07:58:10,798
either
13320
07:58:10,798 --> 07:58:12,478
resolve or reject
13321
07:58:12,478 --> 07:58:15,040
and i'm kind of mimicking a real
13322
07:58:15,040 --> 07:58:16,398
situation here
13323
07:58:16,398 --> 07:58:17,840
fifty percent chance it'll work fifty
13324
07:58:17,840 --> 07:58:19,680
percent chance it won't but that's not
13325
07:58:19,680 --> 07:58:23,080
how good apis are right good apis work
13326
07:58:23,080 --> 07:58:25,840
99.999 percent of the time and there is
13327
07:58:25,840 --> 07:58:28,478
this tiny bit of time where they break
13328
07:58:28,478 --> 07:58:30,638
or return some kind of error so
13329
07:58:30,638 --> 07:58:32,398
that would be a terrible api if it
13330
07:58:32,398 --> 07:58:35,040
worked only 50 of the time
13331
07:58:35,040 --> 07:58:36,558
so let's run it
13332
07:58:36,558 --> 07:58:39,360
and boom on success we got soup is ready
13333
07:58:39,360 --> 07:58:41,360
okay let's run it again
13334
07:58:41,360 --> 07:58:43,520
we'll wait two seconds
13335
07:58:43,520 --> 07:58:46,160
and we got nothing so what happened here
13336
07:58:46,160 --> 07:58:48,080
we might we must have had
13337
07:58:48,080 --> 07:58:51,520
a weird error oh no it just took longer
13338
07:58:51,520 --> 07:58:53,440
than two seconds apparently and then
13339
07:58:53,440 --> 07:58:56,558
boom error no soup today
13340
07:58:56,558 --> 07:58:58,478
right and i can keep running this and
13341
07:58:58,478 --> 07:59:00,638
it'll randomly keep selecting and then
13342
07:59:00,638 --> 07:59:02,160
it'll just check hey if it's ready is
13343
07:59:02,160 --> 07:59:04,878
true run this and if it's false
13344
07:59:04,878 --> 07:59:07,360
it will run the next statement right
13345
07:59:07,360 --> 07:59:08,878
because all it's doing here is putting
13346
07:59:08,878 --> 07:59:10,718
either a true right if i put a true here
13347
07:59:10,718 --> 07:59:11,840
and i run
13348
07:59:11,840 --> 07:59:13,680
you'll see that i'll always get and
13349
07:59:13,680 --> 07:59:15,040
let's make the wait one second so we
13350
07:59:15,040 --> 07:59:17,280
don't have to wait that much
13351
07:59:17,280 --> 07:59:18,878
so if i say true it's always going to
13352
07:59:18,878 --> 07:59:20,798
give me back success
13353
07:59:20,798 --> 07:59:23,280
and i can run it a million times and if
13354
07:59:23,280 --> 07:59:25,440
i hardcode false here you'll see that
13355
07:59:25,440 --> 07:59:28,398
it'll always give me back error
13356
07:59:28,398 --> 07:59:30,080
so we're just going to put israeli here
13357
07:59:30,080 --> 07:59:31,840
because israeli is either going to be
13358
07:59:31,840 --> 07:59:34,478
true or false
13359
07:59:34,878 --> 07:59:36,320
okay great
13360
07:59:36,320 --> 07:59:38,878
so now you can see
13361
07:59:38,878 --> 07:59:41,520
how our promise is working
13362
07:59:41,520 --> 07:59:45,600
and this promise is extremely similar
13363
07:59:45,600 --> 07:59:48,718
to the promise that
13364
07:59:48,718 --> 07:59:50,798
we had in our
13365
07:59:50,798 --> 07:59:52,638
you know the dog application we made and
13366
07:59:52,638 --> 07:59:54,718
the superhero application we made and
13367
07:59:54,718 --> 07:59:56,320
actually let's go ahead and reference
13368
07:59:56,320 --> 07:59:57,120
that
13369
07:59:57,120 --> 07:59:58,320
so
13370
07:59:58,320 --> 08:00:00,160
so go let's go back to our javascript
13371
08:00:00,160 --> 08:00:03,520
course and if we go to
13372
08:00:03,520 --> 08:00:07,600
uh dog random and we look at the script
13373
08:00:07,600 --> 08:00:08,798
right
13374
08:00:08,798 --> 08:00:10,080
notice
13375
08:00:10,080 --> 08:00:12,080
that we're doing something very similar
13376
08:00:12,080 --> 08:00:14,878
here right we have this fetch request
13377
08:00:14,878 --> 08:00:16,798
and let's actually bring that in here so
13378
08:00:16,798 --> 08:00:20,000
we can actually play with it
13379
08:00:20,160 --> 08:00:23,120
so i'll run this and watch what happens
13380
08:00:23,120 --> 08:00:25,360
okay
13381
08:00:25,360 --> 08:00:27,600
and i'll console log this for you
13382
08:00:27,600 --> 08:00:29,200
console.log and you'll see that'll
13383
08:00:29,200 --> 08:00:31,360
actually say it's a promise
13384
08:00:31,360 --> 08:00:33,360
and to make it visible that i'm console
13385
08:00:33,360 --> 08:00:37,520
logging this i'm just going to say fetch
13386
08:00:38,320 --> 08:00:40,000
like that
13387
08:00:40,000 --> 08:00:41,840
do it run
13388
08:00:41,840 --> 08:00:43,920
and you can see that it's telling me
13389
08:00:43,920 --> 08:00:46,478
that hey the fetch promise has began it
13390
08:00:46,478 --> 08:00:48,958
has started but now i actually need to
13391
08:00:48,958 --> 08:00:50,878
do something with it so what i actually
13392
08:00:50,878 --> 08:00:55,798
need to do in this case is i want to do
13393
08:00:56,240 --> 08:00:57,600
let's do
13394
08:00:57,600 --> 08:00:59,440
let's do this
13395
08:00:59,440 --> 08:01:02,000
okay so i'm going to do fetch
13396
08:01:02,000 --> 08:01:04,080
and then on another line so like
13397
08:01:04,080 --> 08:01:05,520
normally i want to do this but you see
13398
08:01:05,520 --> 08:01:07,440
how the line is running so long that
13399
08:01:07,440 --> 08:01:09,520
it's going to be too long so i'm going
13400
08:01:09,520 --> 08:01:11,040
to break it up onto a new line so i'll
13401
08:01:11,040 --> 08:01:12,958
hit enter
13402
08:01:12,958 --> 08:01:15,520
so we're going to say fetch dot
13403
08:01:15,520 --> 08:01:17,200
then
13404
08:01:17,200 --> 08:01:21,200
and i'm going to get the response
13405
08:01:21,200 --> 08:01:23,520
and we're going to get the json response
13406
08:01:23,520 --> 08:01:26,320
back now here's the interesting part if
13407
08:01:26,320 --> 08:01:28,478
i console log
13408
08:01:28,478 --> 08:01:30,160
this part here you're going to notice
13409
08:01:30,160 --> 08:01:32,160
that this actually
13410
08:01:32,160 --> 08:01:35,040
is also a promise so let's console log
13411
08:01:35,040 --> 08:01:37,440
this part and i'll just say
13412
08:01:37,440 --> 08:01:39,840
response
13413
08:01:39,920 --> 08:01:42,718
and we'll run it
13414
08:01:42,718 --> 08:01:45,440
and notice that response is a promise
13415
08:01:45,440 --> 08:01:48,638
response json is a promise too
13416
08:01:48,638 --> 08:01:50,878
so when you run this call majority of
13417
08:01:50,878 --> 08:01:53,520
the times you're sending another request
13418
08:01:53,520 --> 08:01:55,360
and saying hey can you please turn it
13419
08:01:55,360 --> 08:01:57,760
into a json and then that is also an
13420
08:01:57,760 --> 08:01:59,920
asynchronous task that could take
13421
08:01:59,920 --> 08:02:02,638
multiple seconds before returning so
13422
08:02:02,638 --> 08:02:04,638
it's kind of like this invisible coding
13423
08:02:04,638 --> 08:02:06,558
that you're doing here right so
13424
08:02:06,558 --> 08:02:09,120
we have the fetch promise once it's once
13425
08:02:09,120 --> 08:02:10,638
it resolves
13426
08:02:10,638 --> 08:02:13,760
once the fetch promise resolves
13427
08:02:13,760 --> 08:02:16,478
i want you to
13428
08:02:16,478 --> 08:02:20,160
run the response promise with the json
13429
08:02:20,160 --> 08:02:22,240
and guess what guess what you're gonna
13430
08:02:22,240 --> 08:02:25,360
have to do in order to pull a value from
13431
08:02:25,360 --> 08:02:27,200
that
13432
08:02:27,200 --> 08:02:28,398
promise
13433
08:02:28,398 --> 08:02:30,080
you're gonna have to chain it with
13434
08:02:30,080 --> 08:02:32,638
another dot then okay so we're gonna
13435
08:02:32,638 --> 08:02:34,478
have to go and chain it with another dot
13436
08:02:34,478 --> 08:02:36,638
dense i'm gonna go down here
13437
08:02:36,638 --> 08:02:38,718
and we're gonna say another dot then
13438
08:02:38,718 --> 08:02:41,360
and we're gonna say data or json or
13439
08:02:41,360 --> 08:02:42,240
whatever
13440
08:02:42,240 --> 08:02:44,160
you're gonna say console log
13441
08:02:44,160 --> 08:02:46,398
data okay this this variable here does
13442
08:02:46,398 --> 08:02:47,680
not matter you can call it whatever you
13443
08:02:47,680 --> 08:02:49,840
want and if you ever hear that that's
13444
08:02:49,840 --> 08:02:52,320
just me using a focus app to keep me
13445
08:02:52,320 --> 08:02:53,920
focused to keep shooting this so if you
13446
08:02:53,920 --> 08:02:55,840
ever hear that i apologize it'll happen
13447
08:02:55,840 --> 08:02:56,878
every
13448
08:02:56,878 --> 08:02:58,798
20 30 minutes or something like that
13449
08:02:58,798 --> 08:03:00,558
just ignore it
13450
08:03:00,558 --> 08:03:02,398
all right let's run this
13451
08:03:02,398 --> 08:03:04,958
okay so what did we get
13452
08:03:04,958 --> 08:03:06,718
we got oh yeah
13453
08:03:06,718 --> 08:03:10,240
we gotta do not console log
13454
08:03:10,240 --> 08:03:11,760
all right so make sure to remove that
13455
08:03:11,760 --> 08:03:14,558
console log that we had remove this part
13456
08:03:14,558 --> 08:03:15,760
right here
13457
08:03:15,760 --> 08:03:19,120
and just return response.json like this
13458
08:03:19,120 --> 08:03:21,840
and then do data and call data on this
13459
08:03:21,840 --> 08:03:24,240
so let's run that
13460
08:03:24,240 --> 08:03:26,320
and now you can see that we actually got
13461
08:03:26,320 --> 08:03:27,440
back
13462
08:03:27,440 --> 08:03:29,520
the random dog image that we're looking
13463
08:03:29,520 --> 08:03:32,240
for okay so when you're doing fetch
13464
08:03:32,240 --> 08:03:33,600
remember
13465
08:03:33,600 --> 08:03:35,440
fetch is a promise
13466
08:03:35,440 --> 08:03:38,000
and then you do the dot then
13467
08:03:38,000 --> 08:03:41,040
response and then that's also a promise
13468
08:03:41,040 --> 08:03:44,160
okay so this is a promise
13469
08:03:44,160 --> 08:03:46,478
that's a promise okay so there's like
13470
08:03:46,478 --> 08:03:48,160
everything is a promise
13471
08:03:48,160 --> 08:03:51,200
all right and then within here within
13472
08:03:51,200 --> 08:03:53,040
right here
13473
08:03:53,040 --> 08:03:55,920
you you have access to the actual data
13474
08:03:55,920 --> 08:03:57,360
right there you actually get the real
13475
08:03:57,360 --> 08:03:59,520
real real data and you can do something
13476
08:03:59,520 --> 08:04:02,718
with it okay so that's why
13477
08:04:02,718 --> 08:04:04,080
that's like a little bit of a funny
13478
08:04:04,080 --> 08:04:05,760
reason why that happens but but
13479
08:04:05,760 --> 08:04:08,958
underneath the hood it's a promise
13480
08:04:08,958 --> 08:04:11,040
now i'm going to teach you something
13481
08:04:11,040 --> 08:04:12,798
that's going to make your life a lot
13482
08:04:12,798 --> 08:04:14,798
easier moving forward
13483
08:04:14,798 --> 08:04:17,920
and it'll also help you avoid having to
13484
08:04:17,920 --> 08:04:21,600
do dot dance and dot catches because
13485
08:04:21,600 --> 08:04:23,360
they get pretty annoying as you can see
13486
08:04:23,360 --> 08:04:25,840
you'll have to keep chaining and this is
13487
08:04:25,840 --> 08:04:28,478
essentially one line of code right if i
13488
08:04:28,478 --> 08:04:30,718
actually remove the wrapping or whatever
13489
08:04:30,718 --> 08:04:32,638
like this is not how you want to be
13490
08:04:32,638 --> 08:04:34,240
writing your code honestly with like
13491
08:04:34,240 --> 08:04:37,120
that many.then.then.then.catch
13492
08:04:37,120 --> 08:04:38,478
because then you'll have to attach dot
13493
08:04:38,478 --> 08:04:40,080
catch they'll catch any errors that
13494
08:04:40,080 --> 08:04:41,440
happen
13495
08:04:41,440 --> 08:04:44,000
and honestly it becomes kind of a
13496
08:04:44,000 --> 08:04:46,160
nightmare and so
13497
08:04:46,160 --> 08:04:47,680
what you want to do
13498
08:04:47,680 --> 08:04:51,120
is you want to avoid using this syntax
13499
08:04:51,120 --> 08:04:53,200
and you want to use the new syntactical
13500
08:04:53,200 --> 08:04:56,160
sugar that like es6 and beyond provided
13501
08:04:56,160 --> 08:04:58,878
for us and that will make our life
13502
08:04:58,878 --> 08:05:01,840
incredibly incredibly easier
13503
08:05:01,840 --> 08:05:03,920
and so i want to introduce you to you
13504
08:05:03,920 --> 08:05:06,240
the concept of
13505
08:05:06,240 --> 08:05:10,320
async await okay it's incredibly easy
13506
08:05:10,320 --> 08:05:11,840
it's not hard
13507
08:05:11,840 --> 08:05:12,878
it
13508
08:05:12,878 --> 08:05:15,360
will make this part a lot easier it'll
13509
08:05:15,360 --> 08:05:17,120
seem a little foreign and weird the
13510
08:05:17,120 --> 08:05:18,478
first time you look at it but once you
13511
08:05:18,478 --> 08:05:20,478
get used to it that's the way to go and
13512
08:05:20,478 --> 08:05:21,920
your code and your logic is going to
13513
08:05:21,920 --> 08:05:24,240
become a lot easier okay
13514
08:05:24,240 --> 08:05:25,280
so
13515
08:05:25,280 --> 08:05:27,200
this fetch request that we're doing here
13516
08:05:27,200 --> 08:05:28,000
right
13517
08:05:28,000 --> 08:05:30,718
we want to avoid all these dot dens and
13518
08:05:30,718 --> 08:05:32,798
make it simpler so let's create a new
13519
08:05:32,798 --> 08:05:33,920
function
13520
08:05:33,920 --> 08:05:37,200
and we're going to say get get dog
13521
08:05:37,200 --> 08:05:40,478
whatever and now what we're going to say
13522
08:05:40,478 --> 08:05:42,240
this is the most important part is
13523
08:05:42,240 --> 08:05:43,760
whenever you want to use
13524
08:05:43,760 --> 08:05:44,878
a weight
13525
08:05:44,878 --> 08:05:47,520
you're going to need to use async
13526
08:05:47,520 --> 08:05:49,280
okay so make sure so what we're going to
13527
08:05:49,280 --> 08:05:50,718
do is
13528
08:05:50,718 --> 08:05:52,718
just pass in the async keyword here
13529
08:05:52,718 --> 08:05:53,840
which just says hey this is an
13530
08:05:53,840 --> 08:05:57,360
asynchronous function so this function
13531
08:05:57,360 --> 08:05:59,680
uh will run out of order if this
13532
08:05:59,680 --> 08:06:01,200
function will need to communicate to the
13533
08:06:01,200 --> 08:06:03,520
internet or get some type of data or
13534
08:06:03,520 --> 08:06:05,760
we'll need to wait uh half a second a
13535
08:06:05,760 --> 08:06:07,600
millisecond a couple of seconds to get
13536
08:06:07,600 --> 08:06:09,760
this data async just means whenever you
13537
08:06:09,760 --> 08:06:11,040
have to like
13538
08:06:11,040 --> 08:06:13,600
speak to some website or some database
13539
08:06:13,600 --> 08:06:14,798
or something
13540
08:06:14,798 --> 08:06:16,718
you're using an asynchronous function
13541
08:06:16,718 --> 08:06:20,000
there like 99.9 okay so
13542
08:06:20,000 --> 08:06:22,000
whenever you think api requests think
13543
08:06:22,000 --> 08:06:23,920
async whenever you think i'm going to do
13544
08:06:23,920 --> 08:06:26,080
a fetch request think async functions
13545
08:06:26,080 --> 08:06:28,320
whenever you have a post request
13546
08:06:28,320 --> 08:06:29,840
async function
13547
08:06:29,840 --> 08:06:32,798
all right so i have this async function
13548
08:06:32,798 --> 08:06:34,000
over here
13549
08:06:34,000 --> 08:06:36,558
now with this async function um what i
13550
08:06:36,558 --> 08:06:39,200
want to do is i want to say
13551
08:06:39,200 --> 08:06:40,558
fetch
13552
08:06:40,558 --> 08:06:42,878
response is
13553
08:06:42,878 --> 08:06:46,798
and i'm going to fetch this image okay
13554
08:06:46,798 --> 08:06:48,398
the only special thing that we're
13555
08:06:48,398 --> 08:06:50,240
actually going to do here
13556
08:06:50,240 --> 08:06:52,558
is since this url run real
13557
08:06:52,558 --> 08:06:54,638
url runs really long i'm just going to
13558
08:06:54,638 --> 08:06:56,958
say const url and pass that in so we
13559
08:06:56,958 --> 08:06:58,878
could just actually say url
13560
08:06:58,878 --> 08:07:00,878
and then i don't need to make this this
13561
08:07:00,878 --> 08:07:03,440
huge we can make this much smaller i can
13562
08:07:03,440 --> 08:07:05,200
make this bigger here
13563
08:07:05,200 --> 08:07:07,920
so i have my url and i'm just going to
13564
08:07:07,920 --> 08:07:10,320
fetch that url and then what i'm going
13565
08:07:10,320 --> 08:07:11,360
to do
13566
08:07:11,360 --> 08:07:13,760
is i'm going to
13567
08:07:13,760 --> 08:07:15,040
get the data
13568
08:07:15,040 --> 08:07:17,040
and here here's the interesting part so
13569
08:07:17,040 --> 08:07:18,398
for this what you want to do is you want
13570
08:07:18,398 --> 08:07:20,798
to pass in this keyword a weight all
13571
08:07:20,798 --> 08:07:23,440
right this is very important a weight a
13572
08:07:23,440 --> 08:07:25,760
weight is the same thing
13573
08:07:25,760 --> 08:07:27,600
as you
13574
08:07:27,600 --> 08:07:28,718
doing
13575
08:07:28,718 --> 08:07:30,080
dot then
13576
08:07:30,080 --> 08:07:33,680
and then saying respond something okay a
13577
08:07:33,680 --> 08:07:36,080
weight will make your life a lot easier
13578
08:07:36,080 --> 08:07:38,320
so we're going to say oh wait then next
13579
08:07:38,320 --> 08:07:39,680
line
13580
08:07:39,680 --> 08:07:42,080
i'm going to say const data and i'm
13581
08:07:42,080 --> 08:07:43,520
gonna say
13582
08:07:43,520 --> 08:07:45,718
await
13583
08:07:45,718 --> 08:07:47,440
response.json
13584
08:07:47,440 --> 08:07:50,000
so look at that we have our fetch
13585
08:07:50,000 --> 08:07:51,840
requests and then we have our
13586
08:07:51,840 --> 08:07:53,760
response.json
13587
08:07:53,760 --> 08:07:56,638
and that's being stored inside of data
13588
08:07:56,638 --> 08:07:58,878
and now you can use the data variable
13589
08:07:58,878 --> 08:08:00,718
kind of anywhere you want inside of this
13590
08:08:00,718 --> 08:08:02,718
function it'll work
13591
08:08:02,718 --> 08:08:04,160
everywhere and it'll work normally and
13592
08:08:04,160 --> 08:08:05,840
then you don't have to like you're not
13593
08:08:05,840 --> 08:08:08,718
forced you're not forced to like code
13594
08:08:08,718 --> 08:08:11,200
inside of this then
13595
08:08:11,200 --> 08:08:12,798
method like that's kind of ugly and
13596
08:08:12,798 --> 08:08:14,000
annoying
13597
08:08:14,000 --> 08:08:15,920
and now you could use
13598
08:08:15,920 --> 08:08:17,600
console log data
13599
08:08:17,600 --> 08:08:19,760
so let's try this and run it okay and
13600
08:08:19,760 --> 08:08:21,840
you can see that
13601
08:08:21,840 --> 08:08:24,558
um i got back my oh actually sorry we
13602
08:08:24,558 --> 08:08:25,920
didn't call this function yet that's why
13603
08:08:25,920 --> 08:08:27,120
i was like this was something weird is
13604
08:08:27,120 --> 08:08:29,120
happening all right let's run this
13605
08:08:29,120 --> 08:08:31,840
and you can see that we're now getting
13606
08:08:31,840 --> 08:08:34,240
our our random dog
13607
08:08:34,240 --> 08:08:36,878
right over here okay i'll comment out
13608
08:08:36,878 --> 08:08:38,638
this fetch right here
13609
08:08:38,638 --> 08:08:40,718
let's run it again and you can see that
13610
08:08:40,718 --> 08:08:43,280
i'm actually getting back my dog so this
13611
08:08:43,280 --> 08:08:45,280
syntax is
13612
08:08:45,280 --> 08:08:47,760
really really really clean it's a much
13613
08:08:47,760 --> 08:08:50,878
much much better syntax so in order to
13614
08:08:50,878 --> 08:08:53,280
use this you have to create a function
13615
08:08:53,280 --> 08:08:56,798
you cannot use so here are the rules
13616
08:08:56,798 --> 08:08:58,878
rules for using
13617
08:08:58,878 --> 08:09:00,558
async
13618
08:09:00,558 --> 08:09:02,558
await
13619
08:09:02,558 --> 08:09:04,080
one
13620
08:09:04,080 --> 08:09:06,080
you must
13621
08:09:06,080 --> 08:09:08,160
create a function
13622
08:09:08,160 --> 08:09:11,520
okay you cannot use async outside of a
13623
08:09:11,520 --> 08:09:14,478
function it won't work so you can't like
13624
08:09:14,478 --> 08:09:17,120
do this here or something like that that
13625
08:09:17,120 --> 08:09:19,120
it won't work like that so you need to
13626
08:09:19,120 --> 08:09:20,718
have a function
13627
08:09:20,718 --> 08:09:22,638
all right you can't like use a weight
13628
08:09:22,638 --> 08:09:24,000
like this
13629
08:09:24,000 --> 08:09:26,000
you need a function that's called async
13630
08:09:26,000 --> 08:09:27,600
and then you can do all your jiggery
13631
08:09:27,600 --> 08:09:30,240
pokery all right that's step one
13632
08:09:30,240 --> 08:09:32,080
two
13633
08:09:32,080 --> 08:09:32,840
you
13634
08:09:32,840 --> 08:09:34,398
must
13635
08:09:34,398 --> 08:09:36,240
use
13636
08:09:36,240 --> 08:09:37,760
must create
13637
08:09:37,760 --> 08:09:39,840
um let's use
13638
08:09:39,840 --> 08:09:41,200
keyword
13639
08:09:41,200 --> 08:09:42,478
async
13640
08:09:42,478 --> 08:09:44,398
use the word
13641
08:09:44,398 --> 08:09:45,600
await
13642
08:09:45,600 --> 08:09:47,440
so anything you're waiting for you just
13643
08:09:47,440 --> 08:09:50,718
await it right so here we're gonna await
13644
08:09:50,718 --> 08:09:52,558
this promise and then we're gonna await
13645
08:09:52,558 --> 08:09:54,160
the second promise
13646
08:09:54,160 --> 08:09:55,600
and that's it those are the only two
13647
08:09:55,600 --> 08:09:58,000
promises right this is a promise
13648
08:09:58,000 --> 08:10:00,320
and then this is a promise and then once
13649
08:10:00,320 --> 08:10:02,558
you are done waiting for them
13650
08:10:02,558 --> 08:10:05,040
you just get back the actual data and
13651
08:10:05,040 --> 08:10:06,240
then you can do whatever you want with
13652
08:10:06,240 --> 08:10:09,120
it right so that data gets stored here
13653
08:10:09,120 --> 08:10:11,440
it's a much much much nicer way to write
13654
08:10:11,440 --> 08:10:13,680
it so try to do it like that now
13655
08:10:13,680 --> 08:10:15,600
i'll give you a little exercise
13656
08:10:15,600 --> 08:10:17,760
see if you could do this soup example
13657
08:10:17,760 --> 08:10:19,360
that we just did see if you could do
13658
08:10:19,360 --> 08:10:20,320
this
13659
08:10:20,320 --> 08:10:23,440
but using async await for this promise
13660
08:10:23,440 --> 08:10:26,398
right here rather than the dot dance
13661
08:10:26,398 --> 08:10:28,558
okay so see if you create a function
13662
08:10:28,558 --> 08:10:30,240
let's you can call it whatever you want
13663
08:10:30,240 --> 08:10:33,440
like get soup or something right and
13664
08:10:33,440 --> 08:10:35,520
then see if you could turn this piece of
13665
08:10:35,520 --> 08:10:36,798
code
13666
08:10:36,798 --> 08:10:37,920
into
13667
08:10:37,920 --> 08:10:39,120
this
13668
08:10:39,120 --> 08:10:41,120
all right so take like five seconds give
13669
08:10:41,120 --> 08:10:43,520
that a try so i'll give you five seconds
13670
08:10:43,520 --> 08:10:44,878
five
13671
08:10:44,878 --> 08:10:46,080
four
13672
08:10:46,080 --> 08:10:47,360
three
13673
08:10:47,360 --> 08:10:48,478
two
13674
08:10:48,478 --> 08:10:50,240
one
13675
08:10:50,240 --> 08:10:52,558
all right if you haven't paused it
13676
08:10:52,558 --> 08:10:54,638
or you pause it whichever one hopefully
13677
08:10:54,638 --> 08:10:56,638
you gave it a try let's go ahead and now
13678
08:10:56,638 --> 08:10:58,638
do this together okay so i'm going to
13679
08:10:58,638 --> 08:11:00,398
say const get
13680
08:11:00,398 --> 08:11:01,360
soup
13681
08:11:01,360 --> 08:11:03,840
and we'll do this way and then here i'm
13682
08:11:03,840 --> 08:11:05,680
just going to say
13683
08:11:05,680 --> 08:11:06,878
const
13684
08:11:06,878 --> 08:11:09,600
soup is a weight
13685
08:11:09,600 --> 08:11:11,280
promise
13686
08:11:11,280 --> 08:11:13,360
and then we can probably console log
13687
08:11:13,360 --> 08:11:16,080
soup and let's see if this works okay so
13688
08:11:16,080 --> 08:11:19,040
get soup and i'm gonna run this
13689
08:11:19,040 --> 08:11:21,040
see i ran into that problem myself i
13690
08:11:21,040 --> 08:11:22,958
forgot to put i mean and what's nice is
13691
08:11:22,958 --> 08:11:25,840
that it actually gives us a nice error a
13692
08:11:25,840 --> 08:11:28,160
weight is only valid in async functions
13693
08:11:28,160 --> 08:11:30,160
and at the top level body of modules so
13694
08:11:30,160 --> 08:11:32,160
let's go ahead and put async
13695
08:11:32,160 --> 08:11:35,040
and run this so boom there we go error
13696
08:11:35,040 --> 08:11:37,520
no soup today right if i remove a weight
13697
08:11:37,520 --> 08:11:38,798
watch what's going to happen and if i
13698
08:11:38,798 --> 08:11:41,920
run this it's just going to say promise
13699
08:11:41,920 --> 08:11:43,360
right here i'm not going to get anything
13700
08:11:43,360 --> 08:11:45,600
else i'm going to comment at every other
13701
08:11:45,600 --> 08:11:47,440
console log statement so we don't get
13702
08:11:47,440 --> 08:11:48,558
confused
13703
08:11:48,558 --> 08:11:50,478
that's it it just says promise and then
13704
08:11:50,478 --> 08:11:52,878
that's kind of it but the only way to
13705
08:11:52,878 --> 08:11:54,878
get the data out of it
13706
08:11:54,878 --> 08:11:57,520
is you just say oh wait
13707
08:11:57,520 --> 08:11:58,958
and now that same thing that was
13708
08:11:58,958 --> 08:12:01,120
initially saying promise
13709
08:12:01,120 --> 08:12:03,680
now we'll actually return you
13710
08:12:03,680 --> 08:12:04,798
data
13711
08:12:04,798 --> 08:12:06,160
okay i don't know why it's taking that
13712
08:12:06,160 --> 08:12:08,160
long why is it taking forever okay there
13713
08:12:08,160 --> 08:12:10,478
we go soup is ready perfect let's run it
13714
08:12:10,478 --> 08:12:12,240
again and let's see what it says this
13715
08:12:12,240 --> 08:12:14,958
time soup is ready let's run it again
13716
08:12:14,958 --> 08:12:15,840
mmm
13717
08:12:15,840 --> 08:12:18,638
so that's weird how come sometimes it
13718
08:12:18,638 --> 08:12:21,360
says when the soup is if i run it if
13719
08:12:21,360 --> 08:12:22,798
soup is ready
13720
08:12:22,798 --> 08:12:24,878
you know it says it correctly but if
13721
08:12:24,878 --> 08:12:27,360
soup is not ready it returns nothing
13722
08:12:27,360 --> 08:12:30,478
isn't that a little sus what's going on
13723
08:12:30,478 --> 08:12:33,040
so what's going on is now you have to
13724
08:12:33,040 --> 08:12:34,558
try catch this
13725
08:12:34,558 --> 08:12:35,680
so
13726
08:12:35,680 --> 08:12:37,440
if you get an error
13727
08:12:37,440 --> 08:12:38,958
you're not doing it you're not able to
13728
08:12:38,958 --> 08:12:42,000
do a dot catch like how you were before
13729
08:12:42,000 --> 08:12:43,280
so that's what's happening we're getting
13730
08:12:43,280 --> 08:12:46,160
in a reject reject and reject is not
13731
08:12:46,160 --> 08:12:47,920
going to get stored in here so what you
13732
08:12:47,920 --> 08:12:50,398
want to do is you want to say try and
13733
08:12:50,398 --> 08:12:52,558
you want to wrap this in a try catch so
13734
08:12:52,558 --> 08:12:55,120
what i can do is i can say try
13735
08:12:55,120 --> 08:12:58,878
and we try this and we console log
13736
08:12:58,878 --> 08:13:01,680
and then we do catch
13737
08:13:01,680 --> 08:13:04,638
okay and we just let's just say console
13738
08:13:04,638 --> 08:13:07,440
log soup and let's see what happens soup
13739
08:13:07,440 --> 08:13:09,520
is ready let's run it again
13740
08:13:09,520 --> 08:13:11,920
soup is ready okay so see we got
13741
08:13:11,920 --> 08:13:13,680
something that was an error interesting
13742
08:13:13,680 --> 08:13:16,320
so maybe let's pass in
13743
08:13:16,320 --> 08:13:18,160
let's catch the error here and let's see
13744
08:13:18,160 --> 08:13:20,080
if we can print the error out so let's
13745
08:13:20,080 --> 08:13:22,160
run it and let's hope we run into an
13746
08:13:22,160 --> 08:13:24,478
error and let's see if it actually
13747
08:13:24,478 --> 08:13:26,878
prints out our error or not okay soup is
13748
08:13:26,878 --> 08:13:28,478
ready soup is ready
13749
08:13:28,478 --> 08:13:31,360
boom there we go no soup today so if you
13750
08:13:31,360 --> 08:13:32,878
want to catch
13751
08:13:32,878 --> 08:13:35,920
this is how you catch with async awaits
13752
08:13:35,920 --> 08:13:37,200
in tax
13753
08:13:37,200 --> 08:13:40,080
so the catching is not the prettiest
13754
08:13:40,080 --> 08:13:42,160
but i will just promise you this that if
13755
08:13:42,160 --> 08:13:44,000
you start using async await a weight
13756
08:13:44,000 --> 08:13:45,440
will make your life
13757
08:13:45,440 --> 08:13:47,760
a lot simpler and they'll simplify your
13758
08:13:47,760 --> 08:13:49,520
code a lot and then
13759
08:13:49,520 --> 08:13:51,360
it'll be much easier for you to deal
13760
08:13:51,360 --> 08:13:52,958
with things when you're doing
13761
08:13:52,958 --> 08:13:55,440
asynchronous programming so a weight is
13762
08:13:55,440 --> 08:13:56,398
really
13763
08:13:56,398 --> 08:13:58,320
like the way to go
13764
08:13:58,320 --> 08:13:59,680
all right now what if we want to take
13765
08:13:59,680 --> 08:14:02,160
this example further meaning
13766
08:14:02,160 --> 08:14:05,200
if you promise the waiter or the waiter
13767
08:14:05,200 --> 08:14:07,360
promises you that they're going to bring
13768
08:14:07,360 --> 08:14:09,840
you soup well if they bring you soup
13769
08:14:09,840 --> 08:14:12,718
then that's good now what how can we
13770
08:14:12,718 --> 08:14:14,958
take this even further if the waiter
13771
08:14:14,958 --> 08:14:18,718
brings you soup then maybe you also
13772
08:14:18,718 --> 08:14:20,878
pay the waiter okay so let's let's write
13773
08:14:20,878 --> 08:14:22,320
these up here
13774
08:14:22,320 --> 08:14:25,360
so if waiter brings you soup
13775
08:14:25,360 --> 08:14:27,680
maybe then you tip the waiter
13776
08:14:27,680 --> 08:14:29,040
and you
13777
08:14:29,040 --> 08:14:31,920
you pay for soup
13778
08:14:31,920 --> 08:14:34,080
on the other hand if you don't get your
13779
08:14:34,080 --> 08:14:36,160
soup so this is like what happens if the
13780
08:14:36,160 --> 08:14:38,080
situation is resolved
13781
08:14:38,080 --> 08:14:41,040
but if it's rejected and it doesn't go
13782
08:14:41,040 --> 08:14:43,040
through and your soup is declined for
13783
08:14:43,040 --> 08:14:45,280
whatever reason or they didn't have soup
13784
08:14:45,280 --> 08:14:46,878
then maybe you
13785
08:14:46,878 --> 08:14:48,798
you know you're you're a crappy customer
13786
08:14:48,798 --> 08:14:51,760
and you leave a bad review
13787
08:14:51,760 --> 08:14:55,040
and uh you give no tip all right so how
13788
08:14:55,040 --> 08:14:56,478
are you going to actually
13789
08:14:56,478 --> 08:14:57,840
you know
13790
08:14:57,840 --> 08:15:00,320
write that in code so see if you could
13791
08:15:00,320 --> 08:15:02,320
give that a try and see if you could
13792
08:15:02,320 --> 08:15:03,600
take this
13793
08:15:03,600 --> 08:15:06,638
or the dot down one farther
13794
08:15:06,638 --> 08:15:08,878
and see if you can kind of um
13795
08:15:08,878 --> 08:15:12,000
add in all of this sauce like in terms
13796
08:15:12,000 --> 08:15:12,878
of
13797
08:15:12,878 --> 08:15:14,878
adding in a bunch of these things okay
13798
08:15:14,878 --> 08:15:16,478
and just you could create arbitrary
13799
08:15:16,478 --> 08:15:18,080
variables like
13800
08:15:18,080 --> 08:15:20,318
uh tip for the waiter so you could
13801
08:15:20,318 --> 08:15:23,280
create a variable called tip you could
13802
08:15:23,280 --> 08:15:25,280
create a variable called pay and you can
13803
08:15:25,280 --> 08:15:27,280
just kind of set what happens to that
13804
08:15:27,280 --> 08:15:28,878
but the main thing i'm looking for is
13805
08:15:28,878 --> 08:15:30,080
you know
13806
08:15:30,080 --> 08:15:33,120
where to write those lines of code okay
13807
08:15:33,120 --> 08:15:35,520
that's mainly what i'm looking for
13808
08:15:35,520 --> 08:15:38,398
so give that a give that a try and then
13809
08:15:38,398 --> 08:15:39,680
we're going to do this together so i'll
13810
08:15:39,680 --> 08:15:42,718
give you about five seconds so go five
13811
08:15:42,718 --> 08:15:43,760
four
13812
08:15:43,760 --> 08:15:44,878
three
13813
08:15:44,878 --> 08:15:45,920
two
13814
08:15:45,920 --> 08:15:46,798
one
13815
08:15:46,798 --> 08:15:50,000
all right let's do this together now
13816
08:15:50,000 --> 08:15:51,760
so i have this
13817
08:15:51,760 --> 08:15:53,440
you know so we have either whether it's
13818
08:15:53,440 --> 08:15:56,558
resolved or rejected now everything
13819
08:15:56,558 --> 08:15:58,398
that's resolved in my mind is going to
13820
08:15:58,398 --> 08:16:00,798
go inside of this try block and
13821
08:16:00,798 --> 08:16:02,318
everything that's rejected is going to
13822
08:16:02,318 --> 08:16:05,520
go inside of this catch block right so
13823
08:16:05,520 --> 08:16:06,878
say we have
13824
08:16:06,878 --> 08:16:08,638
you know cons and and maybe we can give
13825
08:16:08,638 --> 08:16:11,360
a ra rating you know why not so let's
13826
08:16:11,360 --> 08:16:12,558
say we have
13827
08:16:12,558 --> 08:16:14,160
let rating
13828
08:16:14,160 --> 08:16:16,398
we define another variable
13829
08:16:16,398 --> 08:16:18,958
let tip and we define another variable
13830
08:16:18,958 --> 08:16:21,440
let pay okay
13831
08:16:21,440 --> 08:16:23,040
and if
13832
08:16:23,040 --> 08:16:26,878
this if we got our soup as promised
13833
08:16:26,878 --> 08:16:28,398
well then what i'm going to do is i'm
13834
08:16:28,398 --> 08:16:30,718
going to say i'm going to increase the
13835
08:16:30,718 --> 08:16:32,398
rating to say i'm going to say hit i'm
13836
08:16:32,398 --> 08:16:34,638
going to rate this place five stars
13837
08:16:34,638 --> 08:16:37,200
and i'm going to
13838
08:16:37,200 --> 08:16:39,280
say the tip is
13839
08:16:39,280 --> 08:16:42,000
you know maybe 20
13840
08:16:42,000 --> 08:16:44,160
and i'm gonna pay
13841
08:16:44,160 --> 08:16:47,040
you know ten dollars or whatever for the
13842
08:16:47,040 --> 08:16:50,318
food and then on the other hand
13843
08:16:50,318 --> 08:16:52,798
if the and then for review maybe i give
13844
08:16:52,798 --> 08:16:54,958
a five star review here right
13845
08:16:54,958 --> 08:16:56,878
but on the other hand if i don't get
13846
08:16:56,878 --> 08:16:58,318
what i want
13847
08:16:58,318 --> 08:17:01,520
then i'm going to leave a rating of one
13848
08:17:01,520 --> 08:17:02,398
star
13849
08:17:02,398 --> 08:17:04,958
i'm going to leave no tip
13850
08:17:04,958 --> 08:17:06,398
i'm not going to pay anything because
13851
08:17:06,398 --> 08:17:08,318
while i didn't get anything and then
13852
08:17:08,318 --> 08:17:11,120
review i'm going to leave one star
13853
08:17:11,120 --> 08:17:13,440
okay so we can kind of do that
13854
08:17:13,440 --> 08:17:15,520
and what we could do is actually we can
13855
08:17:15,520 --> 08:17:17,520
even turn this into an object i'm
13856
08:17:17,520 --> 08:17:19,680
thinking something let's do that
13857
08:17:19,680 --> 08:17:21,040
actually
13858
08:17:21,040 --> 08:17:23,280
uh const
13859
08:17:23,280 --> 08:17:25,440
data okay let's just say data because
13860
08:17:25,440 --> 08:17:27,200
that's how usually
13861
08:17:27,200 --> 08:17:28,958
you get data
13862
08:17:28,958 --> 08:17:34,000
rating is zero and tip is zero and p is
13863
08:17:34,000 --> 08:17:35,760
i'm just gonna say zero i mean it should
13864
08:17:35,760 --> 08:17:38,160
be kind of null okay because those are
13865
08:17:38,160 --> 08:17:40,878
data types as well null and undefined
13866
08:17:40,878 --> 08:17:41,680
so
13867
08:17:41,680 --> 08:17:43,680
you could do null or undefined if you
13868
08:17:43,680 --> 08:17:45,200
want to be like really accurate because
13869
08:17:45,200 --> 08:17:47,920
these aren't defined yet actually
13870
08:17:47,920 --> 08:17:50,160
all right so pay is zero and review is
13871
08:17:50,160 --> 08:17:52,318
zero and let's actually use those data
13872
08:17:52,318 --> 08:17:54,000
types afterwards and i'm gonna remove
13873
08:17:54,000 --> 08:17:54,958
this
13874
08:17:54,958 --> 08:17:56,398
and then
13875
08:17:56,398 --> 08:17:58,760
we're just gonna say
13876
08:17:58,760 --> 08:18:02,638
data.rating data.tip data.pay
13877
08:18:02,638 --> 08:18:04,318
data.review and then we're going to do
13878
08:18:04,318 --> 08:18:05,840
the same thing here
13879
08:18:05,840 --> 08:18:07,200
so now it's actually even looking kind
13880
08:18:07,200 --> 08:18:09,120
of like a real api
13881
08:18:09,120 --> 08:18:11,040
which is interesting
13882
08:18:11,040 --> 08:18:14,878
and then at the end of all of this
13883
08:18:14,878 --> 08:18:18,160
whichever one we're going to return data
13884
08:18:18,160 --> 08:18:21,120
and return data
13885
08:18:21,120 --> 08:18:23,520
okay the try catch block
13886
08:18:23,520 --> 08:18:27,060
so now if i do soup and i'll do console
13887
08:18:27,060 --> 08:18:28,318
[Music]
13888
08:18:28,318 --> 08:18:29,440
log
13889
08:18:29,440 --> 08:18:30,878
now let's run it
13890
08:18:30,878 --> 08:18:32,398
and watch what's going to happen so
13891
08:18:32,398 --> 08:18:34,958
promise is running soup is ready
13892
08:18:34,958 --> 08:18:37,360
all right why didn't we get our data
13893
08:18:37,360 --> 08:18:39,280
why did we not get
13894
08:18:39,280 --> 08:18:42,318
our data let's see every console logging
13895
08:18:42,318 --> 08:18:44,318
it we are
13896
08:18:44,318 --> 08:18:46,000
are we running yeah we're running at
13897
08:18:46,000 --> 08:18:47,840
soup interesting
13898
08:18:47,840 --> 08:18:51,040
yes this is the magic of
13899
08:18:51,040 --> 08:18:53,440
uh asynchronous information right that's
13900
08:18:53,440 --> 08:18:55,520
the problem with it so i can get caught
13901
08:18:55,520 --> 08:18:57,760
up but i can get caught off guard with
13902
08:18:57,760 --> 08:18:59,040
it too even though i've been programming
13903
08:18:59,040 --> 08:19:01,680
for years so it is complicated
13904
08:19:01,680 --> 08:19:04,398
see here if i actually run this it's not
13905
08:19:04,398 --> 08:19:06,240
going to work because this function runs
13906
08:19:06,240 --> 08:19:07,680
instantly
13907
08:19:07,680 --> 08:19:09,200
and then i'm sitting there and waiting
13908
08:19:09,200 --> 08:19:11,840
for the data so i actually have to await
13909
08:19:11,840 --> 08:19:14,798
this data and get the results back
13910
08:19:14,798 --> 08:19:16,240
because it's a promise and i need to
13911
08:19:16,240 --> 08:19:18,878
retrieve the data but i can't just await
13912
08:19:18,878 --> 08:19:21,440
it right so i have to actually
13913
08:19:21,440 --> 08:19:23,280
so what i need to do is this
13914
08:19:23,280 --> 08:19:25,520
say i want to remove this console log on
13915
08:19:25,520 --> 08:19:27,680
line 47 and i still want it to actually
13916
08:19:27,680 --> 08:19:30,000
read the console what i want to what i
13917
08:19:30,000 --> 08:19:32,878
want to do is get soup right i want to
13918
08:19:32,878 --> 08:19:34,240
actually call
13919
08:19:34,240 --> 08:19:38,240
this is a promise getsu is a promise
13920
08:19:38,240 --> 08:19:40,240
right and it returns when we run
13921
08:19:40,240 --> 08:19:41,760
asynchronous function it's going to
13922
08:19:41,760 --> 08:19:43,040
return
13923
08:19:43,040 --> 08:19:46,878
uh return a promise okay so in order to
13924
08:19:46,878 --> 08:19:50,000
get this function we can't just call it
13925
08:19:50,000 --> 08:19:51,440
right when you just try to call it it's
13926
08:19:51,440 --> 08:19:52,958
not really going to do anything so what
13927
08:19:52,958 --> 08:19:54,638
i want to do instead
13928
08:19:54,638 --> 08:19:57,520
is i want to do dot then and i'm going
13929
08:19:57,520 --> 08:19:58,718
to say
13930
08:19:58,718 --> 08:20:01,680
value and we'll console log the value
13931
08:20:01,680 --> 08:20:03,440
that we get out of it
13932
08:20:03,440 --> 08:20:05,760
let's run this now and now you can see
13933
08:20:05,760 --> 08:20:08,240
the soup is ready and we got this data
13934
08:20:08,240 --> 08:20:10,318
so we're go we're actually got the data
13935
08:20:10,318 --> 08:20:12,080
back that we were looking for
13936
08:20:12,080 --> 08:20:14,718
right and then here i can actually see
13937
08:20:14,718 --> 08:20:16,638
that the rating is five tip is twenty
13938
08:20:16,638 --> 08:20:18,718
percent pay is ten dollars reviews five
13939
08:20:18,718 --> 08:20:21,520
stars if i run this again
13940
08:20:21,520 --> 08:20:23,920
and oh no soup today well guess what
13941
08:20:23,920 --> 08:20:26,398
rating is one tip is zero pay is zero
13942
08:20:26,398 --> 08:20:28,478
and review is one star
13943
08:20:28,478 --> 08:20:30,558
so that's how
13944
08:20:30,558 --> 08:20:33,120
that's how this works hopefully like
13945
08:20:33,120 --> 08:20:35,360
this makes sense for you on
13946
08:20:35,360 --> 08:20:36,718
you know kind of
13947
08:20:36,718 --> 08:20:38,000
how this
13948
08:20:38,000 --> 08:20:39,840
how you actually do this how all of this
13949
08:20:39,840 --> 08:20:40,718
works
13950
08:20:40,718 --> 08:20:42,638
so here's a simple trick for you to
13951
08:20:42,638 --> 08:20:44,080
remember if you're struggling with
13952
08:20:44,080 --> 08:20:46,318
promises or you've ever struggled with
13953
08:20:46,318 --> 08:20:48,798
promises or i promise you will struggle
13954
08:20:48,798 --> 08:20:52,000
with promises you know in the future
13955
08:20:52,000 --> 08:20:54,558
if you ever so let's just do this okay
13956
08:20:54,558 --> 08:20:55,280
so
13957
08:20:55,280 --> 08:20:57,520
see say if you console log get soup and
13958
08:20:57,520 --> 08:20:59,280
you're like how the hell do i get the
13959
08:20:59,280 --> 08:21:01,120
value out of this why can't i extract
13960
08:21:01,120 --> 08:21:02,638
the value out of it
13961
08:21:02,638 --> 08:21:04,958
if you ever read that something says
13962
08:21:04,958 --> 08:21:07,520
promise there's only
13963
08:21:07,520 --> 08:21:11,200
two ways to ever extract the actual
13964
08:21:11,200 --> 08:21:14,240
value out of this it's only two ways
13965
08:21:14,240 --> 08:21:16,160
either you await it
13966
08:21:16,160 --> 08:21:18,080
which you're going to need to wrap that
13967
08:21:18,080 --> 08:21:20,398
in a function that has async
13968
08:21:20,398 --> 08:21:22,798
you're either going to need to await
13969
08:21:22,798 --> 08:21:23,840
this
13970
08:21:23,840 --> 08:21:24,798
or
13971
08:21:24,798 --> 08:21:26,558
you're going to need to
13972
08:21:26,558 --> 08:21:28,798
dodge then this all right and then just
13973
08:21:28,798 --> 08:21:29,600
do
13974
08:21:29,600 --> 08:21:31,520
value
13975
08:21:31,520 --> 08:21:33,680
value right return the value
13976
08:21:33,680 --> 08:21:35,680
so those are the only two ways you
13977
08:21:35,680 --> 08:21:38,638
actually get data back from an async
13978
08:21:38,638 --> 08:21:40,160
function
13979
08:21:40,160 --> 08:21:42,798
or any type of asynchronous task or
13980
08:21:42,798 --> 08:21:45,200
asynchronous promise even if you make a
13981
08:21:45,200 --> 08:21:47,040
sum function and let's say you make a
13982
08:21:47,040 --> 08:21:48,878
async sum function
13983
08:21:48,878 --> 08:21:51,680
that takes in two values a comma b and
13984
08:21:51,680 --> 08:21:54,160
returns their sum a plus b
13985
08:21:54,160 --> 08:21:56,878
if you call this function right watch
13986
08:21:56,878 --> 08:21:58,240
what's gonna happen
13987
08:21:58,240 --> 08:22:00,080
so if i call this function i pass it one
13988
08:22:00,080 --> 08:22:02,638
comma two and then let's just say sum
13989
08:22:02,638 --> 08:22:04,318
here and i'll run this
13990
08:22:04,318 --> 08:22:05,760
even if it's something simple like this
13991
08:22:05,760 --> 08:22:07,520
watch what's gonna happen when i run it
13992
08:22:07,520 --> 08:22:10,718
i ain't got back the results son what
13993
08:22:10,718 --> 08:22:13,440
the hell why not get back the result i
13994
08:22:13,440 --> 08:22:15,360
clearly defined this there isn't even
13995
08:22:15,360 --> 08:22:17,200
any asynchronous parts to this i'm just
13996
08:22:17,200 --> 08:22:20,000
clearly saying sum up a plus b why are
13997
08:22:20,000 --> 08:22:22,080
you playing games with me
13998
08:22:22,080 --> 08:22:24,398
but because we have the async keyword
13999
08:22:24,398 --> 08:22:26,160
it's an asynchronous function which
14000
08:22:26,160 --> 08:22:29,680
means by default it returns as a promise
14001
08:22:29,680 --> 08:22:31,680
so the only way to extract the value out
14002
08:22:31,680 --> 08:22:33,600
of this you give it a try i'll give you
14003
08:22:33,600 --> 08:22:36,000
five seconds five and you can pause the
14004
08:22:36,000 --> 08:22:40,160
video five four three two one
14005
08:22:40,160 --> 08:22:42,558
only two ways either you call a weight
14006
08:22:42,558 --> 08:22:44,160
or
14007
08:22:44,160 --> 08:22:47,120
async my friend so
14008
08:22:47,120 --> 08:22:49,920
let's go ahead and do async here
14009
08:22:49,920 --> 08:22:52,718
uh or sorry either await or dot then so
14010
08:22:52,718 --> 08:22:55,760
let's do then and we'll say console.log
14011
08:22:55,760 --> 08:22:57,958
or sorry we'll say value
14012
08:22:57,958 --> 08:22:59,520
console.log
14013
08:22:59,520 --> 08:23:02,318
value just like this and now let's run
14014
08:23:02,318 --> 08:23:04,318
it and you're gonna see that we got back
14015
08:23:04,318 --> 08:23:05,520
to three
14016
08:23:05,520 --> 08:23:09,360
that three we extracted right away so
14017
08:23:09,360 --> 08:23:12,638
that's how async functions work
14018
08:23:12,638 --> 08:23:13,600
and
14019
08:23:13,600 --> 08:23:16,080
any type of async stuff that you have if
14020
08:23:16,080 --> 08:23:17,440
you want to extract its value you're
14021
08:23:17,440 --> 08:23:19,120
gonna have to start using
14022
08:23:19,120 --> 08:23:21,840
dot thens to retrieve value for them
14023
08:23:21,840 --> 08:23:24,240
especially if they return data like this
14024
08:23:24,240 --> 08:23:26,638
all right so hopefully
14025
08:23:26,638 --> 08:23:29,680
that gave you some examples of how this
14026
08:23:29,680 --> 08:23:30,798
works
14027
08:23:30,798 --> 08:23:32,558
all right
14028
08:23:32,558 --> 08:23:34,798
this probably isn't going to solve
14029
08:23:34,798 --> 08:23:36,160
everything for you but it's going to
14030
08:23:36,160 --> 08:23:38,318
give you examples and what we're going
14031
08:23:38,318 --> 08:23:40,878
to do is in notes here in your
14032
08:23:40,878 --> 08:23:42,318
javascript course that you're going
14033
08:23:42,318 --> 08:23:43,600
through right now
14034
08:23:43,600 --> 08:23:46,398
in notes i'm going to create a file and
14035
08:23:46,398 --> 08:23:49,120
i'm just going to call it async
14036
08:23:49,120 --> 08:23:53,680
let's call it promises async awaits it
14037
08:23:53,680 --> 08:23:56,798
apis whatever okay and uh let's uh
14038
08:23:56,798 --> 08:24:00,318
let me call it dot json dot js
14039
08:24:00,318 --> 08:24:02,160
and i'll paste in all these kind of
14040
08:24:02,160 --> 08:24:04,240
notes for you as reference point
14041
08:24:04,240 --> 08:24:05,920
so you don't lose them so if you want to
14042
08:24:05,920 --> 08:24:08,318
go in notes you can look at it okay
14043
08:24:08,318 --> 08:24:11,600
and then here's another note for you
14044
08:24:11,600 --> 08:24:13,600
that lance has created these are really
14045
08:24:13,600 --> 08:24:15,440
nice and really helpful it kind of goes
14046
08:24:15,440 --> 08:24:17,440
through some of what we've talked about
14047
08:24:17,440 --> 08:24:19,600
and plus it asks you lots of questions
14048
08:24:19,600 --> 08:24:21,840
and gives you lots of written notes for
14049
08:24:21,840 --> 08:24:24,160
promises to practice them more and to
14050
08:24:24,160 --> 08:24:26,318
really understand them more right so he
14051
08:24:26,318 --> 08:24:28,240
walks you through hey how do you get the
14052
08:24:28,240 --> 08:24:30,718
promise variable how do you increment
14053
08:24:30,718 --> 08:24:32,798
the waiter money
14054
08:24:32,798 --> 08:24:33,760
how do you
14055
08:24:33,760 --> 08:24:36,398
you know how do you do the async await
14056
08:24:36,398 --> 08:24:38,160
plus try catch
14057
08:24:38,160 --> 08:24:40,638
he has an example that's like an actual
14058
08:24:40,638 --> 08:24:42,160
working example and if you want to run
14059
08:24:42,160 --> 08:24:43,760
any of these examples open up a new
14060
08:24:43,760 --> 08:24:46,000
rupple copy paste copy this code paste
14061
08:24:46,000 --> 08:24:48,718
it in there and it'll work
14062
08:24:48,718 --> 08:24:51,520
and how to actually get this data okay
14063
08:24:51,520 --> 08:24:54,160
so here we're doing return user and just
14064
08:24:54,160 --> 08:24:56,160
remember since this is an async function
14065
08:24:56,160 --> 08:24:57,760
you're gonna have to actually extract
14066
08:24:57,760 --> 08:25:00,240
its value out of it
14067
08:25:00,240 --> 08:25:01,760
so we have those
14068
08:25:01,760 --> 08:25:04,160
exercises those notes
14069
08:25:04,160 --> 08:25:06,478
here for you if you want to get more
14070
08:25:06,478 --> 08:25:08,718
practice with the javascript course if
14071
08:25:08,718 --> 08:25:10,000
you're not in this javascript course or
14072
08:25:10,000 --> 08:25:12,000
whatever make sure you're watching this
14073
08:25:12,000 --> 08:25:14,638
on youtube just type in javascript
14074
08:25:14,638 --> 08:25:16,160
and
14075
08:25:16,160 --> 08:25:18,160
you should find this all right so with
14076
08:25:18,160 --> 08:25:20,718
that said i hope you enjoyed this i hope
14077
08:25:20,718 --> 08:25:22,318
you learned something about
14078
08:25:22,318 --> 08:25:23,680
promises
14079
08:25:23,680 --> 08:25:26,878
and i know it's confusing but if you
14080
08:25:26,878 --> 08:25:27,920
really
14081
08:25:27,920 --> 08:25:30,718
stick to it and keep practicing you'll
14082
08:25:30,718 --> 08:25:32,718
keep getting better with proc
14083
08:25:32,718 --> 08:25:34,398
with promises
14084
08:25:34,398 --> 08:25:36,080
and
14085
08:25:36,080 --> 08:25:38,160
if you can just really work hard and
14086
08:25:38,160 --> 08:25:41,600
understand this concept of promises now
14087
08:25:41,600 --> 08:25:43,920
your journey to becoming a developer
14088
08:25:43,920 --> 08:25:45,040
will become
14089
08:25:45,040 --> 08:25:48,160
much much much easier because there are
14090
08:25:48,160 --> 08:25:49,280
so many
14091
08:25:49,280 --> 08:25:51,200
concepts that are asynchronous when it
14092
08:25:51,200 --> 08:25:52,958
comes to web development
14093
08:25:52,958 --> 08:25:55,280
and non-understanding promises will
14094
08:25:55,280 --> 08:25:57,920
always get you in a lot of trouble
14095
08:25:57,920 --> 08:25:59,840
all right with that said i hope you
14096
08:25:59,840 --> 08:26:02,318
enjoyed this and one last thing i i said
14097
08:26:02,318 --> 08:26:03,440
i was going to talk about null and
14098
08:26:03,440 --> 08:26:06,318
undefined you could make these like
14099
08:26:06,318 --> 08:26:09,360
you know null types because
14100
08:26:09,360 --> 08:26:11,600
if you didn't give a review it should
14101
08:26:11,600 --> 08:26:13,360
really be type null
14102
08:26:13,360 --> 08:26:14,318
right
14103
08:26:14,318 --> 08:26:16,160
and uh let's just say we don't define
14104
08:26:16,160 --> 08:26:17,840
any values there and if i run this i
14105
08:26:17,840 --> 08:26:20,000
should get back like null types
14106
08:26:20,000 --> 08:26:22,558
if uh you know my soup is order right no
14107
08:26:22,558 --> 08:26:24,080
no no no
14108
08:26:24,080 --> 08:26:27,280
okay which which makes sense because by
14109
08:26:27,280 --> 08:26:28,318
default
14110
08:26:28,318 --> 08:26:30,318
why would you give a rating of zero by
14111
08:26:30,318 --> 08:26:31,920
default right that doesn't make sense
14112
08:26:31,920 --> 08:26:33,600
that would mean you literally go to a
14113
08:26:33,600 --> 08:26:37,200
restaurant rated zero you actually paid
14114
08:26:37,200 --> 08:26:39,280
zero but you haven't been to that
14115
08:26:39,280 --> 08:26:40,718
restaurant so it's actually null it's
14116
08:26:40,718 --> 08:26:43,760
not it's non-existent or undefined
14117
08:26:43,760 --> 08:26:44,638
either
14118
08:26:44,638 --> 08:26:46,240
you can use either one of those and
14119
08:26:46,240 --> 08:26:47,760
that'll represent a similar thing in
14120
08:26:47,760 --> 08:26:50,240
this scenario so null is a special value
14121
08:26:50,240 --> 08:26:53,040
and undefined is usually also a special
14122
08:26:53,040 --> 08:26:54,080
value
14123
08:26:54,080 --> 08:26:55,120
all right with that said i hope you
14124
08:26:55,120 --> 08:26:57,120
enjoyed this let's keep on going let's
14125
08:26:57,120 --> 08:26:59,200
keep on learning
14126
08:26:59,200 --> 08:27:01,600
all right let's take your api skills up
14127
08:27:01,600 --> 08:27:03,760
a notch we're going to build this
14128
08:27:03,760 --> 08:27:06,558
weather app right over here i can type
14129
08:27:06,558 --> 08:27:08,240
in arizona
14130
08:27:08,240 --> 08:27:09,520
hit search
14131
08:27:09,520 --> 08:27:13,680
temperature is 90 degrees max is 95.
14132
08:27:13,680 --> 08:27:16,638
i can type in chicago search
14133
08:27:16,638 --> 08:27:20,240
91 max is 94 and min is 88 and it's
14134
08:27:20,240 --> 08:27:22,000
clear right now
14135
08:27:22,000 --> 08:27:24,558
or i can type in
14136
08:27:24,558 --> 08:27:26,958
las vegas where i actually just was at
14137
08:27:26,958 --> 08:27:29,280
and i got very very sick playing a chess
14138
08:27:29,280 --> 08:27:30,958
tournament wasn't because of the chess
14139
08:27:30,958 --> 08:27:33,040
tournament it's just vegas as a cesspool
14140
08:27:33,040 --> 08:27:36,558
of crazy humans doing wild things and so
14141
08:27:36,558 --> 08:27:38,398
got a crazy sinus infection but now i'm
14142
08:27:38,398 --> 08:27:40,398
recovering so
14143
08:27:40,398 --> 08:27:42,398
anyway it was hot it was 120 degrees
14144
08:27:42,398 --> 08:27:45,520
there when i was there so there you go
14145
08:27:45,520 --> 08:27:47,840
this is the app this is what you're
14146
08:27:47,840 --> 08:27:49,280
going to be building right now with the
14147
08:27:49,280 --> 08:27:51,360
api knowledge that you've learned so
14148
08:27:51,360 --> 08:27:52,638
let's go ahead
14149
08:27:52,638 --> 08:27:54,638
and set some ground rules for this and
14150
08:27:54,638 --> 08:27:56,558
let me actually also show you
14151
08:27:56,558 --> 08:27:58,240
what you actually need to know in order
14152
08:27:58,240 --> 08:28:00,160
to be able to build this okay so first
14153
08:28:00,160 --> 08:28:01,760
thing we're gonna do is let's open up a
14154
08:28:01,760 --> 08:28:04,240
new tab and let's go to replay.com we're
14155
08:28:04,240 --> 08:28:08,160
gonna just open up one with html css
14156
08:28:08,160 --> 08:28:10,478
javascript okay this one right here and
14157
08:28:10,478 --> 08:28:12,240
let's just say
14158
08:28:12,240 --> 08:28:15,440
api weather okay and hit create replica
14159
08:28:15,440 --> 08:28:17,200
cool so this should just be empty right
14160
08:28:17,200 --> 08:28:18,080
now
14161
08:28:18,080 --> 08:28:20,398
and then let's go over and open a new
14162
08:28:20,398 --> 08:28:21,360
tab
14163
08:28:21,360 --> 08:28:22,920
and we're going to go to
14164
08:28:22,920 --> 08:28:26,160
rapidapi.com just type in rapidapi.com
14165
08:28:26,160 --> 08:28:28,958
now once you're on this website
14166
08:28:28,958 --> 08:28:31,840
sign up i am currently logged in okay so
14167
08:28:31,840 --> 08:28:33,440
i can log out but like sign up with
14168
08:28:33,440 --> 08:28:36,240
either github or google or whatever it's
14169
08:28:36,240 --> 08:28:38,398
free just sign up
14170
08:28:38,398 --> 08:28:40,840
and then it'll bring you to like
14171
08:28:40,840 --> 08:28:42,478
rapidapi.com
14172
08:28:42,478 --> 08:28:44,798
all right on here just search for api so
14173
08:28:44,798 --> 08:28:46,638
we're going to search for a weather api
14174
08:28:46,638 --> 08:28:48,638
so i'm going to just type in weather and
14175
08:28:48,638 --> 08:28:51,040
hit enter and you'll see over here that
14176
08:28:51,040 --> 08:28:53,920
it shows open weather map this is the
14177
08:28:53,920 --> 08:28:55,360
one that we're going to be using this is
14178
08:28:55,360 --> 08:28:57,360
the easiest one to use and why we're
14179
08:28:57,360 --> 08:28:59,360
using rapid api is because they make
14180
08:28:59,360 --> 08:29:02,478
using apis really really really easy
14181
08:29:02,478 --> 08:29:03,520
so
14182
08:29:03,520 --> 08:29:05,200
let's use them okay they're freaking
14183
08:29:05,200 --> 08:29:07,520
awesome so let's click on open weather
14184
08:29:07,520 --> 08:29:10,398
map api and i'm going to zoom out just a
14185
08:29:10,398 --> 08:29:12,080
little bit so you can see
14186
08:29:12,080 --> 08:29:14,080
you can see a little bit better here
14187
08:29:14,080 --> 08:29:15,520
let's drag this
14188
08:29:15,520 --> 08:29:16,478
back
14189
08:29:16,478 --> 08:29:18,878
now you can see our api request is
14190
08:29:18,878 --> 08:29:21,040
getting set up i'm going to hover over
14191
08:29:21,040 --> 08:29:22,798
this where it says node
14192
08:29:22,798 --> 08:29:24,798
axios whatever we're going to switch
14193
08:29:24,798 --> 08:29:26,878
that to just javascript fetch because we
14194
08:29:26,878 --> 08:29:28,558
want to see how to do
14195
08:29:28,558 --> 08:29:30,638
that request in fetch
14196
08:29:30,638 --> 08:29:33,120
and this is already kind of showing us
14197
08:29:33,120 --> 08:29:36,240
how to retrieve the data from this open
14198
08:29:36,240 --> 08:29:38,398
weather api okay
14199
08:29:38,398 --> 08:29:40,638
it's putting in a key
14200
08:29:40,638 --> 08:29:43,840
it's making a request to the open map
14201
08:29:43,840 --> 08:29:46,080
open weather map api and it's trying to
14202
08:29:46,080 --> 08:29:47,840
pull some data from there now there's
14203
08:29:47,840 --> 08:29:49,120
just a couple of things we're going to
14204
08:29:49,120 --> 08:29:50,958
need to do so in order to even use the
14205
08:29:50,958 --> 08:29:52,478
free one just we're gonna need to
14206
08:29:52,478 --> 08:29:54,638
subscribe to test it doesn't take in a
14207
08:29:54,638 --> 08:29:56,080
credit card or anything like that so
14208
08:29:56,080 --> 08:29:58,000
just hit subscribe to test
14209
08:29:58,000 --> 08:29:59,680
and then choose the one that's a zero
14210
08:29:59,680 --> 08:30:02,080
dollars okay this is not gonna ask for a
14211
08:30:02,080 --> 08:30:03,760
card it's just free just hit the
14212
08:30:03,760 --> 08:30:05,200
subscribe button so we're just going to
14213
08:30:05,200 --> 08:30:06,638
hit subscribe
14214
08:30:06,638 --> 08:30:09,280
click back on api documentation all
14215
08:30:09,280 --> 08:30:10,798
right perfect let's go back to the
14216
08:30:10,798 --> 08:30:13,920
javascript fetch mode all right and
14217
08:30:13,920 --> 08:30:16,240
just clean up some of these things over
14218
08:30:16,240 --> 08:30:18,478
here so for example this over here i'm
14219
08:30:18,478 --> 08:30:20,958
going to replace that with chicago so
14220
08:30:20,958 --> 08:30:22,478
you're going to notice that it actually
14221
08:30:22,478 --> 08:30:25,280
updated that dynamically in the url that
14222
08:30:25,280 --> 08:30:27,760
we had remove this all the option
14223
08:30:27,760 --> 08:30:29,520
optional ones just remove them double
14224
08:30:29,520 --> 08:30:31,280
click and remove double click and remove
14225
08:30:31,280 --> 08:30:33,840
double click and remove imperial you can
14226
08:30:33,840 --> 08:30:36,558
leave that and then xml double click and
14227
08:30:36,558 --> 08:30:39,520
remove so your your url should look
14228
08:30:39,520 --> 08:30:41,600
pretty simple fetch url should look
14229
08:30:41,600 --> 08:30:43,840
pretty simple right now okay so if i
14230
08:30:43,840 --> 08:30:45,200
zoom into this
14231
08:30:45,200 --> 08:30:47,440
should basically just say
14232
08:30:47,440 --> 08:30:50,000
community open weather map
14233
08:30:50,000 --> 08:30:52,718
blah blah blah and then
14234
08:30:52,718 --> 08:30:55,920
units imperial okay and the city should
14235
08:30:55,920 --> 08:30:58,478
be chicago for now let's see what
14236
08:30:58,478 --> 08:31:01,360
happens if i actually try to run this so
14237
08:31:01,360 --> 08:31:03,520
let's go up at the top let's hit
14238
08:31:03,520 --> 08:31:05,280
test endpoint
14239
08:31:05,280 --> 08:31:08,160
run and boom you see that it's actually
14240
08:31:08,160 --> 08:31:09,360
already
14241
08:31:09,360 --> 08:31:11,920
giving us back a result
14242
08:31:11,920 --> 08:31:13,600
okay so let's scroll down here a little
14243
08:31:13,600 --> 08:31:14,398
bit
14244
08:31:14,398 --> 08:31:16,398
and you can see we have a result here so
14245
08:31:16,398 --> 08:31:19,920
we got 13 items back and i'm gonna just
14246
08:31:19,920 --> 08:31:22,160
close all of these
14247
08:31:22,160 --> 08:31:24,798
items that it sent and it sent us back a
14248
08:31:24,798 --> 08:31:26,798
couple of you know it's an object with
14249
08:31:26,798 --> 08:31:28,798
like a bunch of different properties
14250
08:31:28,798 --> 08:31:32,240
right so we have a cod a name which is
14251
08:31:32,240 --> 08:31:35,440
chicago the id of this specific place
14252
08:31:35,440 --> 08:31:37,840
what time zone it is in all right
14253
08:31:37,840 --> 08:31:40,000
whatever this dt thing is
14254
08:31:40,000 --> 08:31:42,958
clouds wind visibility the stuff that
14255
08:31:42,958 --> 08:31:44,398
we're going to need
14256
08:31:44,398 --> 08:31:46,318
is we're definitely going to need to
14257
08:31:46,318 --> 08:31:48,478
pull from weather so we can pull things
14258
08:31:48,478 --> 08:31:49,600
like main
14259
08:31:49,600 --> 08:31:51,840
it's like clear okay because if we go
14260
08:31:51,840 --> 08:31:54,398
back to our app our weather app you can
14261
08:31:54,398 --> 08:31:56,318
see that we have this clear here so
14262
08:31:56,318 --> 08:31:57,920
we're going to need to pull this and
14263
08:31:57,920 --> 08:31:59,440
then after that we just need to pull
14264
08:31:59,440 --> 08:32:01,520
these different three different ones so
14265
08:32:01,520 --> 08:32:04,558
temp min temp and max temp so if i go
14266
08:32:04,558 --> 08:32:07,680
back to rapid api where is min temp max
14267
08:32:07,680 --> 08:32:08,718
temp
14268
08:32:08,718 --> 08:32:10,478
let's take a look at that so let's see
14269
08:32:10,478 --> 08:32:13,280
if we can find the mint max temp all of
14270
08:32:13,280 --> 08:32:16,080
that so i'm gonna look inside of main
14271
08:32:16,080 --> 08:32:20,240
and here we have temp min temp max and
14272
08:32:20,240 --> 08:32:22,240
temp so we have all of the things that
14273
08:32:22,240 --> 08:32:24,080
we need so it seems like we get them
14274
08:32:24,080 --> 08:32:26,798
back in this object and then the key
14275
08:32:26,798 --> 08:32:29,760
that it's in is main okay so for example
14276
08:32:29,760 --> 08:32:31,840
if i was to imagine what i will be doing
14277
08:32:31,840 --> 08:32:34,478
is i'll get back some something like
14278
08:32:34,478 --> 08:32:36,638
data and then from data i'm gonna have
14279
08:32:36,638 --> 08:32:39,200
to get access to maybe the first data
14280
08:32:39,200 --> 08:32:41,120
point or whatever and then i'm going to
14281
08:32:41,120 --> 08:32:44,080
need to get access to main and then
14282
08:32:44,080 --> 08:32:46,958
within main so main right here and then
14283
08:32:46,958 --> 08:32:48,958
within main i'll probably need to get
14284
08:32:48,958 --> 08:32:50,878
access to temp
14285
08:32:50,878 --> 08:32:53,680
just like that so that's probably how
14286
08:32:53,680 --> 08:32:55,120
i'm going to
14287
08:32:55,120 --> 08:32:56,878
get that specific temp that we're
14288
08:32:56,878 --> 08:32:59,040
looking for okay so let's go ahead and
14289
08:32:59,040 --> 08:33:01,760
try that out let's copy that code
14290
08:33:01,760 --> 08:33:04,318
snippet so i'm going to basically copy
14291
08:33:04,318 --> 08:33:06,080
this entire thing the other way you can
14292
08:33:06,080 --> 08:33:08,878
copy it just hit copy code go over you
14293
08:33:08,878 --> 08:33:11,280
don't need to spam it like me being just
14294
08:33:11,280 --> 08:33:15,040
an idiot go back here and let's go ahead
14295
08:33:15,040 --> 08:33:17,920
and paste that all of that okay so this
14296
08:33:17,920 --> 08:33:19,680
is going to be like a bunch of code that
14297
08:33:19,680 --> 08:33:21,520
got pasted but
14298
08:33:21,520 --> 08:33:24,000
options here is really what you you so
14299
08:33:24,000 --> 08:33:25,920
when you're using fetch
14300
08:33:25,920 --> 08:33:28,558
you can pass it it can take in a url and
14301
08:33:28,558 --> 08:33:30,318
then it can take in options
14302
08:33:30,318 --> 08:33:32,318
and then inside of options you can pass
14303
08:33:32,318 --> 08:33:34,558
it things like you can pass at things
14304
08:33:34,558 --> 08:33:35,440
like
14305
08:33:35,440 --> 08:33:37,040
what's your method so this is a get
14306
08:33:37,040 --> 08:33:39,760
request and then you can also pass it in
14307
08:33:39,760 --> 08:33:42,080
headers and you could pass it in like
14308
08:33:42,080 --> 08:33:44,638
the specific key that you need like an
14309
08:33:44,638 --> 08:33:46,080
access token because you're gonna need
14310
08:33:46,080 --> 08:33:46,878
that
14311
08:33:46,878 --> 08:33:48,558
and then who you're sending the request
14312
08:33:48,558 --> 08:33:50,478
to all right so that's what options
14313
08:33:50,478 --> 08:33:51,840
really is
14314
08:33:51,840 --> 08:33:53,840
and if i try to run this let's see if we
14315
08:33:53,840 --> 08:33:56,478
actually get back any data
14316
08:33:56,478 --> 08:33:58,318
and boom you can actually see on the
14317
08:33:58,318 --> 08:34:01,040
bottom right i just get got back all the
14318
08:34:01,040 --> 08:34:01,920
data
14319
08:34:01,920 --> 08:34:05,360
so dot then console log response now
14320
08:34:05,360 --> 08:34:06,798
let's see if
14321
08:34:06,798 --> 08:34:08,958
let's see if we can get from that
14322
08:34:08,958 --> 08:34:11,760
response right so this is really data
14323
08:34:11,760 --> 08:34:14,718
data and let's see if i can do data dot
14324
08:34:14,718 --> 08:34:15,760
main
14325
08:34:15,760 --> 08:34:17,360
and let's see if that does what we
14326
08:34:17,360 --> 08:34:18,558
wanted to do
14327
08:34:18,558 --> 08:34:21,040
boom it actually just got us back what
14328
08:34:21,040 --> 08:34:22,878
we were looking for after i did data dot
14329
08:34:22,878 --> 08:34:24,798
main i didn't even have to do data zero
14330
08:34:24,798 --> 08:34:26,080
dot main
14331
08:34:26,080 --> 08:34:27,120
and
14332
08:34:27,120 --> 08:34:30,080
i'm getting back my temp my temp min and
14333
08:34:30,080 --> 08:34:32,718
temp max and those are the things i need
14334
08:34:32,718 --> 08:34:35,360
right what was the other thing i need
14335
08:34:35,360 --> 08:34:37,200
well i needed that thing where it said
14336
08:34:37,200 --> 08:34:38,318
clear
14337
08:34:38,318 --> 08:34:41,280
where did it say clear it said clear
14338
08:34:41,280 --> 08:34:43,440
under i believe
14339
08:34:43,440 --> 08:34:46,638
weather so let's go check again
14340
08:34:46,638 --> 08:34:50,638
so when i ran the results i have weather
14341
08:34:50,638 --> 08:34:52,478
weather has an array so i have to
14342
08:34:52,478 --> 08:34:54,240
probably like
14343
08:34:54,240 --> 08:34:56,160
grab the zeroth item from weather so i'm
14344
08:34:56,160 --> 08:34:57,600
imagining i gotta do something like this
14345
08:34:57,600 --> 08:34:58,958
data
14346
08:34:58,958 --> 08:35:01,520
dot weather
14347
08:35:01,520 --> 08:35:03,200
of xero
14348
08:35:03,200 --> 08:35:04,638
dot
14349
08:35:04,638 --> 08:35:07,040
main i imagine i gotta do something like
14350
08:35:07,040 --> 08:35:07,840
that
14351
08:35:07,840 --> 08:35:10,638
and then that'll get me access to
14352
08:35:10,638 --> 08:35:12,000
that'll get me access to what i need
14353
08:35:12,000 --> 08:35:13,600
because data will get me access to this
14354
08:35:13,600 --> 08:35:14,638
whole thing
14355
08:35:14,638 --> 08:35:17,040
whether dot weather will get me access
14356
08:35:17,040 --> 08:35:18,000
to
14357
08:35:18,000 --> 08:35:19,440
this right here
14358
08:35:19,440 --> 08:35:21,920
whether xero will get me access to the
14359
08:35:21,920 --> 08:35:23,280
specific
14360
08:35:23,280 --> 08:35:26,160
array right there and then dot main
14361
08:35:26,160 --> 08:35:28,718
within that will get me access i believe
14362
08:35:28,718 --> 08:35:32,000
to this key right here uh this value
14363
08:35:32,000 --> 08:35:33,600
right here clear
14364
08:35:33,600 --> 08:35:37,040
so let's give that a try and see if that
14365
08:35:37,040 --> 08:35:40,958
gets us what we need dot weather of xero
14366
08:35:40,958 --> 08:35:44,240
dot what do we say
14367
08:35:44,240 --> 08:35:45,600
dot main
14368
08:35:45,600 --> 08:35:47,440
clear boom
14369
08:35:47,440 --> 08:35:50,160
so you can see exactly how we can
14370
08:35:50,160 --> 08:35:51,680
actually get back
14371
08:35:51,680 --> 08:35:53,680
uh how we can get access to the things
14372
08:35:53,680 --> 08:35:55,920
that we need this is how you can make
14373
08:35:55,920 --> 08:35:57,360
that request
14374
08:35:57,360 --> 08:35:59,760
so hopefully this sets you kind of in
14375
08:35:59,760 --> 08:36:02,000
the right place to get started with this
14376
08:36:02,000 --> 08:36:04,080
exercise and i'll show you
14377
08:36:04,080 --> 08:36:05,680
now the project that you're going to be
14378
08:36:05,680 --> 08:36:07,520
building okay so then you have all the
14379
08:36:07,520 --> 08:36:09,280
tools that you need to be able to
14380
08:36:09,280 --> 08:36:11,040
complete it so if you go to your
14381
08:36:11,040 --> 08:36:13,200
javascript course main
14382
08:36:13,200 --> 08:36:16,000
folder right main file right over here
14383
08:36:16,000 --> 08:36:17,680
here's where you will find your weather
14384
08:36:17,680 --> 08:36:19,440
apps if you actually
14385
08:36:19,440 --> 08:36:20,638
go down
14386
08:36:20,638 --> 08:36:22,958
you'll see it'll say weather app
14387
08:36:22,958 --> 08:36:25,040
right over here
14388
08:36:25,040 --> 08:36:25,920
okay
14389
08:36:25,920 --> 08:36:27,360
click on that
14390
08:36:27,360 --> 08:36:28,718
and this is your weather app that you
14391
08:36:28,718 --> 08:36:30,478
can use
14392
08:36:30,478 --> 08:36:32,878
and if you go inside of the exercises
14393
08:36:32,878 --> 08:36:35,440
for this weather app so let's go to
14394
08:36:35,440 --> 08:36:36,718
projects
14395
08:36:36,718 --> 08:36:38,798
let's go to weather app let's go to
14396
08:36:38,798 --> 08:36:40,478
exercise
14397
08:36:40,478 --> 08:36:43,600
and this exercise here
14398
08:36:43,600 --> 08:36:45,760
okay we'll walk you through
14399
08:36:45,760 --> 08:36:48,080
exactly how you're gonna
14400
08:36:48,080 --> 08:36:50,638
do everything inside of this weather app
14401
08:36:50,638 --> 08:36:53,280
now you can use this url
14402
08:36:53,280 --> 08:36:54,478
here
14403
08:36:54,478 --> 08:36:57,120
or you can use the url that
14404
08:36:57,120 --> 08:36:59,280
the way that i showed you on rapid api
14405
08:36:59,280 --> 08:37:00,958
for bonus points try to just use the
14406
08:37:00,958 --> 08:37:02,718
rapid api way
14407
08:37:02,718 --> 08:37:04,798
um i think it's easier so then you don't
14408
08:37:04,798 --> 08:37:06,878
even have to go to open weather app
14409
08:37:06,878 --> 08:37:08,398
weather map
14410
08:37:08,398 --> 08:37:10,638
and create your own api but whichever
14411
08:37:10,638 --> 08:37:12,958
one is simpler for you do it that way
14412
08:37:12,958 --> 08:37:15,120
and here it'll walk you through exactly
14413
08:37:15,120 --> 08:37:16,478
how this project
14414
08:37:16,478 --> 08:37:17,360
works
14415
08:37:17,360 --> 08:37:19,120
okay so it's like hey
14416
08:37:19,120 --> 08:37:21,280
you know get the weather data so create
14417
08:37:21,280 --> 08:37:22,958
a function that essentially runs and
14418
08:37:22,958 --> 08:37:24,718
gets the weather data
14419
08:37:24,718 --> 08:37:26,638
um you're gonna need to write something
14420
08:37:26,638 --> 08:37:29,120
so if we open up the app on this right
14421
08:37:29,120 --> 08:37:31,040
hand side let's go to the weather app
14422
08:37:31,040 --> 08:37:32,398
you're gonna need to have the search
14423
08:37:32,398 --> 08:37:34,240
input field where you're gonna need to
14424
08:37:34,240 --> 08:37:36,478
extract the user
14425
08:37:36,478 --> 08:37:38,798
city from and then pop that into your
14426
08:37:38,798 --> 08:37:40,798
url and then search it
14427
08:37:40,798 --> 08:37:42,398
right so then when they hit search it
14428
08:37:42,398 --> 08:37:44,718
pulls all the data this
14429
08:37:44,718 --> 08:37:47,920
weather maps api problem is pretty
14430
08:37:47,920 --> 08:37:50,000
similar to the superhero app that we
14431
08:37:50,000 --> 08:37:51,840
made so i don't think you should have
14432
08:37:51,840 --> 08:37:53,840
too much trouble honestly doing this
14433
08:37:53,840 --> 08:37:55,760
this is not shouldn't be too challenging
14434
08:37:55,760 --> 08:37:56,798
for you
14435
08:37:56,798 --> 08:37:58,478
if it is it's all good we'll just walk
14436
08:37:58,478 --> 08:38:00,398
through this together and then you have
14437
08:38:00,398 --> 08:38:02,080
two functions here okay you have the
14438
08:38:02,080 --> 08:38:04,000
search city function all right and then
14439
08:38:04,000 --> 08:38:06,318
you have this show weather data
14440
08:38:06,318 --> 08:38:07,600
function
14441
08:38:07,600 --> 08:38:08,638
and
14442
08:38:08,638 --> 08:38:10,318
search city what this is going to do is
14443
08:38:10,318 --> 08:38:12,000
based on whatever the user has put in
14444
08:38:12,000 --> 08:38:14,398
here it's going to extract the value
14445
08:38:14,398 --> 08:38:16,398
that's what search city will do and then
14446
08:38:16,398 --> 08:38:18,160
show weather data
14447
08:38:18,160 --> 08:38:21,200
will update data on the dom
14448
08:38:21,200 --> 08:38:22,558
and
14449
08:38:22,558 --> 08:38:25,520
you will have access to
14450
08:38:25,520 --> 08:38:28,318
what those variables are as well by
14451
08:38:28,318 --> 08:38:30,240
those variables i mean the dom you're
14452
08:38:30,240 --> 08:38:31,920
going to need to manipulate the dom so
14453
08:38:31,920 --> 08:38:34,080
check out the html on where you actually
14454
08:38:34,080 --> 08:38:36,080
need to go and manipulate the
14455
08:38:36,080 --> 08:38:37,680
dom
14456
08:38:37,680 --> 08:38:41,360
also to make your lives easier i have
14457
08:38:41,360 --> 08:38:42,798
put
14458
08:38:42,798 --> 08:38:44,958
all the divs you'll need access to all
14459
08:38:44,958 --> 08:38:47,760
right so your city name weather type
14460
08:38:47,760 --> 08:38:50,718
temp the these are the div
14461
08:38:50,718 --> 08:38:53,680
ids okay so these are the actual div ids
14462
08:38:53,680 --> 08:38:56,878
if you go into the index.html file
14463
08:38:56,878 --> 08:38:59,360
you'll be able to see those specific ids
14464
08:38:59,360 --> 08:39:01,520
if you look for them like city name
14465
08:39:01,520 --> 08:39:04,240
weather type men temp max temp right and
14466
08:39:04,240 --> 08:39:06,718
that's where it goes
14467
08:39:06,718 --> 08:39:08,240
those go right there when you change
14468
08:39:08,240 --> 08:39:10,398
their inner text so all you'll need to
14469
08:39:10,398 --> 08:39:13,680
do is change their inner text
14470
08:39:13,680 --> 08:39:15,680
now to get ready for this project what
14471
08:39:15,680 --> 08:39:19,520
you need to do is this so for your
14472
08:39:19,520 --> 08:39:20,718
file
14473
08:39:20,718 --> 08:39:23,760
do the following in your index.html go
14474
08:39:23,760 --> 08:39:24,958
back
14475
08:39:24,958 --> 08:39:27,600
copy the index.html from
14476
08:39:27,600 --> 08:39:30,080
from this exercise file right from the
14477
08:39:30,080 --> 08:39:32,240
javascript course copy the entire
14478
08:39:32,240 --> 08:39:34,958
index.html file go
14479
08:39:34,958 --> 08:39:37,200
and replace it paste it over into the
14480
08:39:37,200 --> 08:39:39,600
new replit that you created
14481
08:39:39,600 --> 08:39:44,558
do the same thing by going to style.css
14482
08:39:44,558 --> 08:39:47,600
and copying this entire thing and going
14483
08:39:47,600 --> 08:39:50,160
ahead and pasting this so your styles
14484
08:39:50,160 --> 08:39:52,558
should showing be showing up and then
14485
08:39:52,558 --> 08:39:55,920
for your script well what you could do
14486
08:39:55,920 --> 08:39:59,040
is you could copy pretty much everything
14487
08:39:59,040 --> 08:40:00,638
that's here
14488
08:40:00,638 --> 08:40:02,478
and then maybe just paste it underneath
14489
08:40:02,478 --> 08:40:03,920
for now
14490
08:40:03,920 --> 08:40:05,840
okay so that way you have
14491
08:40:05,840 --> 08:40:08,000
kind of the piece of code that you and i
14492
08:40:08,000 --> 08:40:10,000
worked on together so you have that
14493
08:40:10,000 --> 08:40:10,958
already
14494
08:40:10,958 --> 08:40:12,558
okay that's just to show you how to pull
14495
08:40:12,558 --> 08:40:14,558
data and then the rest is up to you
14496
08:40:14,558 --> 08:40:16,398
because this should already show you
14497
08:40:16,398 --> 08:40:19,040
enough of what you need to do and really
14498
08:40:19,040 --> 08:40:21,040
put you on the right track and then it's
14499
08:40:21,040 --> 08:40:22,958
your job to like go okay how do i
14500
08:40:22,958 --> 08:40:25,440
retrieve this data and how do i actually
14501
08:40:25,440 --> 08:40:28,958
like have this data show up here right
14502
08:40:28,958 --> 08:40:30,000
show up
14503
08:40:30,000 --> 08:40:32,718
into the right places because this piece
14504
08:40:32,718 --> 08:40:33,920
of code
14505
08:40:33,920 --> 08:40:37,280
already does that for you so have fun
14506
08:40:37,280 --> 08:40:39,280
give this project a try
14507
08:40:39,280 --> 08:40:40,240
and
14508
08:40:40,240 --> 08:40:43,360
see how much of it you can get done
14509
08:40:43,360 --> 08:40:44,718
all right if you get this whole thing
14510
08:40:44,718 --> 08:40:45,520
done
14511
08:40:45,520 --> 08:40:47,840
huge props if you don't don't stress too
14512
08:40:47,840 --> 08:40:50,000
much we'll walk through it together all
14513
08:40:50,000 --> 08:40:51,920
right with that said i'll see you
14514
08:40:51,920 --> 08:40:53,120
shortly
14515
08:40:53,120 --> 08:40:54,958
all right now let's get to work and
14516
08:40:54,958 --> 08:40:57,120
let's solve this together let's build
14517
08:40:57,120 --> 08:40:59,360
this weather app now we're just gonna do
14518
08:40:59,360 --> 08:41:00,478
it from
14519
08:41:00,478 --> 08:41:03,120
scratch this is the only piece of code
14520
08:41:03,120 --> 08:41:05,440
that we had right and i got that from
14521
08:41:05,440 --> 08:41:09,200
rapid api i went to rapid api signed up
14522
08:41:09,200 --> 08:41:12,318
for open weather map and then basically
14523
08:41:12,318 --> 08:41:14,398
i was able to get my data points and
14524
08:41:14,398 --> 08:41:16,240
then it also just makes a code snippet
14525
08:41:16,240 --> 08:41:17,760
for you which is really nice just make
14526
08:41:17,760 --> 08:41:20,000
sure to choose javascript fetch and then
14527
08:41:20,000 --> 08:41:21,840
you can copy and use that exact code
14528
08:41:21,840 --> 08:41:23,920
snippet so it makes your life a little
14529
08:41:23,920 --> 08:41:25,760
bit easier now i don't even need this
14530
08:41:25,760 --> 08:41:28,000
api key right over here because i'm
14531
08:41:28,000 --> 08:41:28,878
using
14532
08:41:28,878 --> 08:41:33,040
a rapid api one so that makes life even
14533
08:41:33,040 --> 08:41:34,080
easier
14534
08:41:34,080 --> 08:41:34,958
so
14535
08:41:34,958 --> 08:41:36,878
what do we need to do here we need to
14536
08:41:36,878 --> 08:41:38,878
get the data based on the city that the
14537
08:41:38,878 --> 08:41:40,398
user types well
14538
08:41:40,398 --> 08:41:43,040
we're already able to fetch based on the
14539
08:41:43,040 --> 08:41:45,040
city right so if i see my fetch request
14540
08:41:45,040 --> 08:41:46,718
here i can see that i'm passing in
14541
08:41:46,718 --> 08:41:49,520
chicago so i get chicago data if i run
14542
08:41:49,520 --> 08:41:52,958
i get clear okay let's remove this
14543
08:41:52,958 --> 08:41:54,558
here and i'm just going to leave it to
14544
08:41:54,558 --> 08:41:57,360
data let's run this and you can see that
14545
08:41:57,360 --> 08:41:59,200
i get back
14546
08:41:59,200 --> 08:42:00,798
all of the data
14547
08:42:00,798 --> 08:42:02,798
related to
14548
08:42:02,798 --> 08:42:04,718
related to chicago right and i can even
14549
08:42:04,718 --> 08:42:07,520
see name here so let's just put
14550
08:42:07,520 --> 08:42:09,120
data dot
14551
08:42:09,120 --> 08:42:11,040
name and let's run it and you can see
14552
08:42:11,040 --> 08:42:13,680
chicago well let's say i change this to
14553
08:42:13,680 --> 08:42:17,600
marina del rey and let's run this okay i
14554
08:42:17,600 --> 08:42:20,318
got some errors right there
14555
08:42:20,318 --> 08:42:22,558
marina del rey let's put percent signs
14556
08:42:22,558 --> 08:42:24,878
let's see what happens okay no problem
14557
08:42:24,878 --> 08:42:27,360
no problem let's go back to open weather
14558
08:42:27,360 --> 08:42:30,318
map api and i just want to see
14559
08:42:30,318 --> 08:42:31,440
um
14560
08:42:31,440 --> 08:42:33,280
when you type in marina del rey how does
14561
08:42:33,280 --> 08:42:35,360
it write that in strings
14562
08:42:35,360 --> 08:42:39,520
randall right ah percent 20 20 20 just
14563
08:42:39,520 --> 08:42:41,360
like that so that's how you represent a
14564
08:42:41,360 --> 08:42:42,398
space
14565
08:42:42,398 --> 08:42:45,760
in urls as percent 20. so that's marina
14566
08:42:45,760 --> 08:42:47,040
percent
14567
08:42:47,040 --> 08:42:48,478
20 del rey
14568
08:42:48,478 --> 08:42:50,318
cool that's a city
14569
08:42:50,318 --> 08:42:52,958
yep so now it's pulling it's searching
14570
08:42:52,958 --> 08:42:55,760
at marina del rey and i can also look up
14571
08:42:55,760 --> 08:42:57,760
hollywood or whatever i want right and
14572
08:42:57,760 --> 08:42:59,120
let's hit run
14573
08:42:59,120 --> 08:43:01,520
and it'll pull hollywood and
14574
08:43:01,520 --> 08:43:03,280
retrieve its data
14575
08:43:03,280 --> 08:43:05,520
so we already know that whatever we want
14576
08:43:05,520 --> 08:43:07,680
to put in here it needs to go
14577
08:43:07,680 --> 08:43:09,840
right here so this is where like you
14578
08:43:09,840 --> 08:43:12,718
know in my mind if i'm looking at this i
14579
08:43:12,718 --> 08:43:14,558
will immediately convert this into a
14580
08:43:14,558 --> 08:43:16,000
template literal
14581
08:43:16,000 --> 08:43:18,318
and right here
14582
08:43:18,318 --> 08:43:21,680
okay so const let's just say let city
14583
08:43:21,680 --> 08:43:22,840
equals
14584
08:43:22,840 --> 08:43:24,558
whatever
14585
08:43:24,558 --> 08:43:26,398
city equals whatever
14586
08:43:26,398 --> 08:43:28,318
in my mind this is where the user city
14587
08:43:28,318 --> 08:43:31,440
is going to go so i'm going to go
14588
08:43:31,440 --> 08:43:33,600
user city
14589
08:43:33,600 --> 08:43:35,440
because i know that it's something that
14590
08:43:35,440 --> 08:43:36,958
the user is going to give me it's not
14591
08:43:36,958 --> 08:43:38,878
going to be actually hard coded into my
14592
08:43:38,878 --> 08:43:41,920
url so now if i give
14593
08:43:41,920 --> 08:43:43,920
let's say hollywood
14594
08:43:43,920 --> 08:43:46,000
then when i search it'll say hollywood
14595
08:43:46,000 --> 08:43:47,120
right here
14596
08:43:47,120 --> 08:43:50,478
if i give detroit or whatever
14597
08:43:50,478 --> 08:43:53,920
it will get me detroit yeah
14598
08:43:53,920 --> 08:43:55,440
so now
14599
08:43:55,440 --> 08:43:58,318
this is this is kind of doing what i
14600
08:43:58,318 --> 08:44:01,040
wanted to do we have yet to retrieve the
14601
08:44:01,040 --> 08:44:02,718
data what we want to do is obviously
14602
08:44:02,718 --> 08:44:05,200
pull the data dynamically from
14603
08:44:05,200 --> 08:44:08,240
this user input box when the user types
14604
08:44:08,240 --> 08:44:09,440
in the data
14605
08:44:09,440 --> 08:44:11,600
right so if we look here we have city
14606
08:44:11,600 --> 08:44:13,600
name we have weather type
14607
08:44:13,600 --> 08:44:16,080
city name is probably where i imagine
14608
08:44:16,080 --> 08:44:18,718
you know we're gonna pull the
14609
08:44:18,718 --> 08:44:21,920
the value from that's where the value is
14610
08:44:21,920 --> 08:44:24,000
it's in this text box so you want to
14611
08:44:24,000 --> 08:44:25,520
pull the value out of there so let's
14612
08:44:25,520 --> 08:44:26,478
just say
14613
08:44:26,478 --> 08:44:27,920
las vegas
14614
08:44:27,920 --> 08:44:29,600
and then we want to pull the value when
14615
08:44:29,600 --> 08:44:32,160
the user hits the search button
14616
08:44:32,160 --> 08:44:35,120
so we want to make our function
14617
08:44:35,120 --> 08:44:38,160
where when the user hits
14618
08:44:38,160 --> 08:44:39,760
search
14619
08:44:39,760 --> 08:44:41,600
we can extract this
14620
08:44:41,600 --> 08:44:44,878
now if we go to our index.html you can
14621
08:44:44,878 --> 08:44:47,360
actually see that on the on click method
14622
08:44:47,360 --> 08:44:50,478
for this search button we already have
14623
08:44:50,478 --> 08:44:52,080
search city
14624
08:44:52,080 --> 08:44:54,398
function that gets called so every time
14625
08:44:54,398 --> 08:44:56,318
i click this button the search city
14626
08:44:56,318 --> 08:44:58,398
function gets called so let's see where
14627
08:44:58,398 --> 08:45:00,160
our search city function is it's right
14628
08:45:00,160 --> 08:45:02,638
here okay so now let's go ahead
14629
08:45:02,638 --> 08:45:04,718
console.log city
14630
08:45:04,718 --> 08:45:07,280
and it seems like we're already
14631
08:45:07,280 --> 08:45:09,280
pulling the value out of it right here
14632
08:45:09,280 --> 08:45:10,878
and we should be able to see the value
14633
08:45:10,878 --> 08:45:12,558
so let's type in las vegas let's hit
14634
08:45:12,558 --> 08:45:13,520
search
14635
08:45:13,520 --> 08:45:16,558
let's hit run i guess let's type in las
14636
08:45:16,558 --> 08:45:18,878
vegas and let's hit search and you can
14637
08:45:18,878 --> 08:45:21,120
see that we pull las vegas
14638
08:45:21,120 --> 08:45:23,280
so by searching city we're already able
14639
08:45:23,280 --> 08:45:24,878
to pull the value
14640
08:45:24,878 --> 08:45:26,558
now we have this other function which is
14641
08:45:26,558 --> 08:45:28,478
called get weather data so i'm just
14642
08:45:28,478 --> 08:45:30,398
going to put a const right here
14643
08:45:30,398 --> 08:45:32,718
this is really the
14644
08:45:32,718 --> 08:45:34,718
you know this piece of code that we
14645
08:45:34,718 --> 08:45:36,398
wrote up at the top
14646
08:45:36,398 --> 08:45:38,638
okay it's basically that because that's
14647
08:45:38,638 --> 08:45:40,638
where we're really making our api
14648
08:45:40,638 --> 08:45:43,360
requests i'm just going to first cut my
14649
08:45:43,360 --> 08:45:45,120
options and i'm going to pop in my
14650
08:45:45,120 --> 08:45:47,120
options right here
14651
08:45:47,120 --> 08:45:48,878
because those are like my keys and all
14652
08:45:48,878 --> 08:45:50,718
of that stuff and then i'm going to cut
14653
08:45:50,718 --> 08:45:52,718
my fetch request
14654
08:45:52,718 --> 08:45:54,638
and we can delete this user city because
14655
08:45:54,638 --> 08:45:56,558
we don't need that anymore
14656
08:45:56,558 --> 08:46:00,080
and boom we can put our fetch request
14657
08:46:00,080 --> 08:46:02,000
right get weather data will take in a
14658
08:46:02,000 --> 08:46:02,878
city
14659
08:46:02,878 --> 08:46:05,200
so now we'll just call this city right
14660
08:46:05,200 --> 08:46:06,958
over here because we're already grabbing
14661
08:46:06,958 --> 08:46:08,318
that from the
14662
08:46:08,318 --> 08:46:10,638
argument right there i don't need this
14663
08:46:10,638 --> 08:46:13,040
url right here anymore because we're
14664
08:46:13,040 --> 08:46:14,878
using rapid api
14665
08:46:14,878 --> 08:46:17,440
cool all right so fetch
14666
08:46:17,440 --> 08:46:18,638
fetch will be the city that we're
14667
08:46:18,638 --> 08:46:20,000
fetching
14668
08:46:20,000 --> 08:46:21,760
and then it will
14669
08:46:21,760 --> 08:46:24,958
return that data okay so right now we're
14670
08:46:24,958 --> 08:46:27,120
console logging the city names i'm just
14671
08:46:27,120 --> 08:46:30,240
going to say console log data
14672
08:46:30,240 --> 08:46:32,398
so search city is going to run and then
14673
08:46:32,398 --> 08:46:34,638
what we can do is
14674
08:46:34,638 --> 08:46:36,318
we can
14675
08:46:36,318 --> 08:46:39,280
call our get weather function and pass
14676
08:46:39,280 --> 08:46:41,280
the city that we got so let's go ahead
14677
08:46:41,280 --> 08:46:43,520
and give it the city okay so let's see
14678
08:46:43,520 --> 08:46:46,080
what happens now let's say i hit detroit
14679
08:46:46,080 --> 08:46:47,760
and let's hit search
14680
08:46:47,760 --> 08:46:50,000
and you can see i just got all this data
14681
08:46:50,000 --> 08:46:51,360
on detroit
14682
08:46:51,360 --> 08:46:53,120
right and it i can even see that the
14683
08:46:53,120 --> 08:46:54,558
name here
14684
08:46:54,558 --> 08:46:56,240
says detroit
14685
08:46:56,240 --> 08:47:00,318
now let's type in las vegas
14686
08:47:00,318 --> 08:47:01,280
boom
14687
08:47:01,280 --> 08:47:03,120
name las vegas
14688
08:47:03,120 --> 08:47:05,360
and i'm getting all this data back so it
14689
08:47:05,360 --> 08:47:07,520
seems like my button is connect my
14690
08:47:07,520 --> 08:47:09,840
typing being able to type is connected
14691
08:47:09,840 --> 08:47:12,398
when i hit search it's extracting the
14692
08:47:12,398 --> 08:47:14,638
data it's sending that over to get
14693
08:47:14,638 --> 08:47:16,398
weather data and then that knows how to
14694
08:47:16,398 --> 08:47:18,558
make a fetch request
14695
08:47:18,558 --> 08:47:20,638
and retrieve the data back
14696
08:47:20,638 --> 08:47:22,080
and the last thing really that we need
14697
08:47:22,080 --> 08:47:24,318
to do is
14698
08:47:24,318 --> 08:47:26,718
really grab that data
14699
08:47:26,718 --> 08:47:27,840
and
14700
08:47:27,840 --> 08:47:30,478
throw it onto our dom right like
14701
08:47:30,478 --> 08:47:32,718
actually show it onto our screen to show
14702
08:47:32,718 --> 08:47:34,478
weather data
14703
08:47:34,478 --> 08:47:36,638
so this is in this method in this
14704
08:47:36,638 --> 08:47:37,920
function right over here show weather
14705
08:47:37,920 --> 08:47:39,680
data we just need to update the dom
14706
08:47:39,680 --> 08:47:42,080
elements so basically
14707
08:47:42,080 --> 08:47:44,160
what i want to do here is
14708
08:47:44,160 --> 08:47:46,240
i'm going to take these guys
14709
08:47:46,240 --> 08:47:47,920
and update them
14710
08:47:47,920 --> 08:47:53,120
okay so for example what do we have temp
14711
08:47:53,120 --> 08:47:55,760
right so let's say document
14712
08:47:55,760 --> 08:47:59,200
dot get element by id
14713
08:47:59,200 --> 08:48:02,638
and temp and we'll set its inner text to
14714
08:48:02,638 --> 08:48:06,638
let's just hard code it to 86.6
14715
08:48:06,638 --> 08:48:08,160
and then i'm just going to call show
14716
08:48:08,160 --> 08:48:10,958
weather data right underneath here
14717
08:48:10,958 --> 08:48:13,200
and i'm going to give it
14718
08:48:13,200 --> 08:48:14,638
i'm going to just give it some dummy
14719
08:48:14,638 --> 08:48:16,398
data right now okay so right now we're
14720
08:48:16,398 --> 08:48:17,840
not really going to pass it anything
14721
08:48:17,840 --> 08:48:19,280
useful
14722
08:48:19,280 --> 08:48:21,920
and let's go las vegas search
14723
08:48:21,920 --> 08:48:23,200
boom
14724
08:48:23,200 --> 08:48:26,958
and you can see that my temp was 86.6
14725
08:48:26,958 --> 08:48:28,558
okay
14726
08:48:28,558 --> 08:48:29,920
so
14727
08:48:29,920 --> 08:48:31,760
i'm just hard coding this right now but
14728
08:48:31,760 --> 08:48:33,440
we're going to need to dynamically pull
14729
08:48:33,440 --> 08:48:35,680
the temperature and then pass that
14730
08:48:35,680 --> 08:48:37,440
temperature
14731
08:48:37,440 --> 08:48:40,798
right actually past the data
14732
08:48:40,798 --> 08:48:42,478
and not just
14733
08:48:42,478 --> 08:48:44,558
kind of the silly thing we're doing here
14734
08:48:44,558 --> 08:48:47,280
so now what i want to do here is
14735
08:48:47,280 --> 08:48:50,318
i want to return this data so let's just
14736
08:48:50,318 --> 08:48:52,000
do this
14737
08:48:52,000 --> 08:48:54,958
so this way i'm returning data
14738
08:48:54,958 --> 08:48:56,638
and the next thing i can do is i can
14739
08:48:56,638 --> 08:48:58,240
make this function
14740
08:48:58,240 --> 08:48:59,360
async
14741
08:48:59,360 --> 08:49:01,920
and why i want to do that is i'm about
14742
08:49:01,920 --> 08:49:04,398
to run asynchronous code
14743
08:49:04,398 --> 08:49:06,398
and i need it to run in the right order
14744
08:49:06,398 --> 08:49:08,558
because i want to get the data first and
14745
08:49:08,558 --> 08:49:11,200
then i want to show the data but if i
14746
08:49:11,200 --> 08:49:13,440
try to run them both at the same time
14747
08:49:13,440 --> 08:49:14,958
without doing this what's going to
14748
08:49:14,958 --> 08:49:16,638
happen is
14749
08:49:16,638 --> 08:49:19,440
this here is going to run first because
14750
08:49:19,440 --> 08:49:22,240
this is like synchronous code and then
14751
08:49:22,240 --> 08:49:24,718
this here is going to run after because
14752
08:49:24,718 --> 08:49:26,718
this is asynchronous that's not what we
14753
08:49:26,718 --> 08:49:29,120
want we want this to finish first
14754
08:49:29,120 --> 08:49:31,760
and then we want this one to run okay so
14755
08:49:31,760 --> 08:49:33,920
i'm going to say async
14756
08:49:33,920 --> 08:49:36,398
and we're going to say const
14757
08:49:36,398 --> 08:49:38,718
data and i'm going to say await
14758
08:49:38,718 --> 08:49:42,000
okay and we can pass this data now to
14759
08:49:42,000 --> 08:49:44,318
our show weather data and then we'll
14760
08:49:44,318 --> 08:49:46,878
console log the weather data and see if
14761
08:49:46,878 --> 08:49:48,718
we're actually getting back anything and
14762
08:49:48,718 --> 08:49:50,398
i'll just say
14763
08:49:50,398 --> 08:49:52,558
a cookie here so we can see that it's
14764
08:49:52,558 --> 08:49:54,718
the actual thing that we're getting
14765
08:49:54,718 --> 08:49:57,440
so let's run this
14766
08:49:57,440 --> 08:50:00,000
let's type in las vegas hit search
14767
08:50:00,000 --> 08:50:03,280
i'm getting back undefined interesting
14768
08:50:03,280 --> 08:50:05,200
oh because we're not returning anything
14769
08:50:05,200 --> 08:50:08,000
so actually we can put a weight here
14770
08:50:08,000 --> 08:50:10,160
and we just got to make sure we return
14771
08:50:10,160 --> 08:50:12,798
this fetch
14772
08:50:12,798 --> 08:50:14,080
and now
14773
08:50:14,080 --> 08:50:16,260
a wait will grab the
14774
08:50:16,260 --> 08:50:17,360
[Music]
14775
08:50:17,360 --> 08:50:19,520
grab the data and then we'll store that
14776
08:50:19,520 --> 08:50:21,200
data and then we'll pass that data so
14777
08:50:21,200 --> 08:50:23,520
let's try running this now let's see
14778
08:50:23,520 --> 08:50:24,958
what happens
14779
08:50:24,958 --> 08:50:28,000
okay las vegas boom now it works and we
14780
08:50:28,000 --> 08:50:30,798
get our cookie emoji perfect
14781
08:50:30,798 --> 08:50:33,200
so we grab that data
14782
08:50:33,200 --> 08:50:34,878
and then we show the weather data okay
14783
08:50:34,878 --> 08:50:37,280
so we get weather data and then we show
14784
08:50:37,280 --> 08:50:39,040
weather data this is the right way to do
14785
08:50:39,040 --> 08:50:40,240
it
14786
08:50:40,240 --> 08:50:43,360
and now that we have access to that data
14787
08:50:43,360 --> 08:50:45,360
remember how we want to get access to
14788
08:50:45,360 --> 08:50:46,718
our temps right so let's go ahead
14789
08:50:46,718 --> 08:50:48,638
console log and we're going to say
14790
08:50:48,638 --> 08:50:49,920
weather
14791
08:50:49,920 --> 08:50:52,080
data dot
14792
08:50:52,080 --> 08:50:54,718
i think if you do main
14793
08:50:54,718 --> 08:50:56,878
all right let's see what main gets us
14794
08:50:56,878 --> 08:50:58,878
let's go las vegas again and main gets
14795
08:50:58,878 --> 08:51:00,638
us all the stuff that we need so it gets
14796
08:51:00,638 --> 08:51:03,600
us the temp so we could do main dot temp
14797
08:51:03,600 --> 08:51:05,280
here
14798
08:51:05,280 --> 08:51:06,958
and
14799
08:51:06,958 --> 08:51:09,200
i can just copy this and i can paste
14800
08:51:09,200 --> 08:51:12,080
that right there let's hit run
14801
08:51:12,080 --> 08:51:14,240
and now you can see that we have 106
14802
08:51:14,240 --> 08:51:15,600
degrees
14803
08:51:15,600 --> 08:51:18,160
temperature automatically being updated
14804
08:51:18,160 --> 08:51:20,398
right there now we just need to do min
14805
08:51:20,398 --> 08:51:22,718
temp and max temp and
14806
08:51:22,718 --> 08:51:25,360
city name needs to go here as well
14807
08:51:25,360 --> 08:51:27,478
so we're just going to have a bunch of
14808
08:51:27,478 --> 08:51:30,240
document.getelements by ids
14809
08:51:30,240 --> 08:51:31,920
now so let's go ahead i'm just going to
14810
08:51:31,920 --> 08:51:33,520
paste this line
14811
08:51:33,520 --> 08:51:36,478
five times because i have one two
14812
08:51:36,478 --> 08:51:38,478
three four five
14813
08:51:38,478 --> 08:51:40,000
dom elements that i'm going to need to
14814
08:51:40,000 --> 08:51:41,840
update
14815
08:51:41,840 --> 08:51:43,760
so one of them is obviously going to be
14816
08:51:43,760 --> 08:51:44,638
temp
14817
08:51:44,638 --> 08:51:46,958
so let's just say we're good with temp
14818
08:51:46,958 --> 08:51:49,680
the next one i'm gonna do is a city name
14819
08:51:49,680 --> 08:51:51,840
okay so we're gonna go and see
14820
08:51:51,840 --> 08:51:54,398
i think it's literally called city
14821
08:51:54,398 --> 08:51:56,160
dash
14822
08:51:56,160 --> 08:51:58,318
i could be wrong
14823
08:51:58,318 --> 08:52:00,080
city name
14824
08:52:00,080 --> 08:52:03,520
yeah i think that should be fine
14825
08:52:03,520 --> 08:52:04,958
i think so
14826
08:52:04,958 --> 08:52:06,478
we'll see
14827
08:52:06,478 --> 08:52:08,398
see if this does what we wanted to do
14828
08:52:08,398 --> 08:52:11,040
and let's just do weatherdata.name
14829
08:52:11,040 --> 08:52:12,638
so let's try this
14830
08:52:12,638 --> 08:52:14,240
let's go ahead las vegas let's hit
14831
08:52:14,240 --> 08:52:16,398
search and boom las vegas perfect so
14832
08:52:16,398 --> 08:52:17,920
that worked
14833
08:52:17,920 --> 08:52:20,558
next one is this right over here how did
14834
08:52:20,558 --> 08:52:22,000
we get
14835
08:52:22,000 --> 08:52:24,798
clear again let's go check
14836
08:52:24,798 --> 08:52:27,040
so we're going to do
14837
08:52:27,040 --> 08:52:30,478
dot weather of xero dot main and that'll
14838
08:52:30,478 --> 08:52:32,718
get us what we need
14839
08:52:32,718 --> 08:52:35,600
okay so we're gonna do dot weather of
14840
08:52:35,600 --> 08:52:36,240
xero
14841
08:52:36,240 --> 08:52:37,680
[Music]
14842
08:52:37,680 --> 08:52:40,080
dot main
14843
08:52:40,080 --> 08:52:43,760
and this is going to get us our
14844
08:52:43,760 --> 08:52:46,160
this is going to get us
14845
08:52:46,160 --> 08:52:48,718
what's the div called div is going to be
14846
08:52:48,718 --> 08:52:51,200
called weather type
14847
08:52:51,200 --> 08:52:52,478
weather
14848
08:52:52,478 --> 08:52:54,798
dash type
14849
08:52:54,798 --> 08:52:57,840
and then we have temp
14850
08:52:57,840 --> 08:53:00,958
min dash temp and max dash temp so let's
14851
08:53:00,958 --> 08:53:04,318
just do both of those min dash temp
14852
08:53:04,318 --> 08:53:07,120
max dash temp
14853
08:53:07,120 --> 08:53:09,440
okay and then in order to get those i'm
14854
08:53:09,440 --> 08:53:11,600
gonna just need to do
14855
08:53:11,600 --> 08:53:13,520
temp
14856
08:53:13,520 --> 08:53:14,878
underscore
14857
08:53:14,878 --> 08:53:16,558
min
14858
08:53:16,558 --> 08:53:19,600
temp underscore max i'm just looking at
14859
08:53:19,600 --> 08:53:22,558
this right here temp max
14860
08:53:22,558 --> 08:53:24,840
and temp
14861
08:53:24,840 --> 08:53:26,798
min okay
14862
08:53:26,798 --> 08:53:28,638
and now let's give this a try and i
14863
08:53:28,638 --> 08:53:30,718
think the entire thing should be working
14864
08:53:30,718 --> 08:53:34,240
so let's go las vegas hit search
14865
08:53:34,240 --> 08:53:37,120
and we can see las vegas clear
14866
08:53:37,120 --> 08:53:40,718
temp men temp maxstamp boom
14867
08:53:40,718 --> 08:53:43,440
that was slick
14868
08:53:43,440 --> 08:53:45,120
can you believe we just did that right
14869
08:53:45,120 --> 08:53:47,680
now like we were speeding through that i
14870
08:53:47,680 --> 08:53:49,440
was like what 14 minutes with
14871
08:53:49,440 --> 08:53:52,398
explanation we're going fast so let's go
14872
08:53:52,398 --> 08:53:54,718
chicago hit search
14873
08:53:54,718 --> 08:53:59,680
clear chicago temp mint max temp
14874
08:53:59,680 --> 08:54:01,600
right let's type in i don't know that
14875
08:54:01,600 --> 08:54:03,680
should probably break oh crap that's
14876
08:54:03,680 --> 08:54:05,680
working so i guess you could type in
14877
08:54:05,680 --> 08:54:07,840
cities or states
14878
08:54:07,840 --> 08:54:10,000
maybe i'll try illinois
14879
08:54:10,000 --> 08:54:12,798
beautiful so it works kind of all around
14880
08:54:12,798 --> 08:54:15,920
let's check california and just general
14881
08:54:15,920 --> 08:54:17,200
nice
14882
08:54:17,200 --> 08:54:19,280
so look at that right it's
14883
08:54:19,280 --> 08:54:21,200
working really really well it's able to
14884
08:54:21,200 --> 08:54:23,280
pull the data fast it's able to show the
14885
08:54:23,280 --> 08:54:24,398
data
14886
08:54:24,398 --> 08:54:27,760
and if you want to move all of your code
14887
08:54:27,760 --> 08:54:30,080
from this into the
14888
08:54:30,080 --> 08:54:32,798
javascript exercise page you could do
14889
08:54:32,798 --> 08:54:35,120
that as well just make sure you
14890
08:54:35,120 --> 08:54:37,680
you know command it copy this and go all
14891
08:54:37,680 --> 08:54:39,120
the way back
14892
08:54:39,120 --> 08:54:42,318
and then just go into this exercise
14893
08:54:42,318 --> 08:54:43,680
one right
14894
08:54:43,680 --> 08:54:45,920
and then select it and paste everything
14895
08:54:45,920 --> 08:54:47,200
over it
14896
08:54:47,200 --> 08:54:49,440
and you should be good to go
14897
08:54:49,440 --> 08:54:51,440
right and then your exercise
14898
08:54:51,440 --> 08:54:53,360
file should be working so you should be
14899
08:54:53,360 --> 08:54:54,798
able to go to your
14900
08:54:54,798 --> 08:54:56,638
project exercises you should be able to
14901
08:54:56,638 --> 08:54:58,160
click weather app
14902
08:54:58,160 --> 08:55:00,398
and then it should be
14903
08:55:00,398 --> 08:55:02,638
kind of working in there for you
14904
08:55:02,638 --> 08:55:04,558
okay even if right now it's
14905
08:55:04,558 --> 08:55:06,000
it might be broken
14906
08:55:06,000 --> 08:55:07,760
there you go so now if you go to weather
14907
08:55:07,760 --> 08:55:10,000
app right over here it should pull up
14908
08:55:10,000 --> 08:55:12,478
and once you put your javascript code
14909
08:55:12,478 --> 08:55:14,798
the exercise one should be working
14910
08:55:14,798 --> 08:55:16,638
and then the solution one is of course
14911
08:55:16,638 --> 08:55:19,040
the final version so that's
14912
08:55:19,040 --> 08:55:21,120
you know that's always gonna be
14913
08:55:21,120 --> 08:55:22,318
up and running
14914
08:55:22,318 --> 08:55:24,798
so there you go that's how you make your
14915
08:55:24,798 --> 08:55:26,718
own weather app hopefully that gave you
14916
08:55:26,718 --> 08:55:28,000
a clear understanding and everything
14917
08:55:28,000 --> 08:55:29,760
makes sense and hopefully you enjoyed it
14918
08:55:29,760 --> 08:55:32,080
and i'll see you soon all right i hope
14919
08:55:32,080 --> 08:55:34,318
you're having as much fun as i'm having
14920
08:55:34,318 --> 08:55:38,638
now let's move on and talk about classes
14921
08:55:38,638 --> 08:55:40,958
so how do classes work in javascript
14922
08:55:40,958 --> 08:55:44,798
open a new tab let's go to replic.com
14923
08:55:44,798 --> 08:55:46,478
all right replit
14924
08:55:46,478 --> 08:55:48,638
rep lit.com
14925
08:55:48,638 --> 08:55:51,920
open up html css javascript file and
14926
08:55:51,920 --> 08:55:54,318
we're going to say a lesson
14927
08:55:54,318 --> 08:55:56,558
and we'll talk about
14928
08:55:56,558 --> 08:55:58,478
classes
14929
08:55:58,478 --> 08:56:00,878
okay
14930
08:56:00,878 --> 08:56:01,840
all right
14931
08:56:01,840 --> 08:56:03,280
so
14932
08:56:03,280 --> 08:56:05,280
how what the heck are classes how do
14933
08:56:05,280 --> 08:56:08,318
they work so classes are you know any
14934
08:56:08,318 --> 08:56:10,000
time you've seen okay
14935
08:56:10,000 --> 08:56:11,920
for example
14936
08:56:11,920 --> 08:56:14,798
uh anytime you've done a dot method on
14937
08:56:14,798 --> 08:56:16,718
something right that requires you know
14938
08:56:16,718 --> 08:56:18,080
about classes
14939
08:56:18,080 --> 08:56:20,958
so let's go into our script.js here so
14940
08:56:20,958 --> 08:56:22,478
i'll give you examples of where you're
14941
08:56:22,478 --> 08:56:24,398
using classes
14942
08:56:24,398 --> 08:56:26,160
where you didn't even realize that you
14943
08:56:26,160 --> 08:56:27,120
were
14944
08:56:27,120 --> 08:56:28,638
all right so
14945
08:56:28,638 --> 08:56:30,798
when you say new promise when you say
14946
08:56:30,798 --> 08:56:32,080
new promise
14947
08:56:32,080 --> 08:56:34,718
you're using classes here okay where
14948
08:56:34,718 --> 08:56:36,638
else are you using classes
14949
08:56:36,638 --> 08:56:39,520
remember when we can have a
14950
08:56:39,520 --> 08:56:42,080
array of nums
14951
08:56:42,080 --> 08:56:45,040
and then we could do numstot push
14952
08:56:45,040 --> 08:56:47,200
well guess what
14953
08:56:47,200 --> 08:56:49,600
you're actually working with the class
14954
08:56:49,600 --> 08:56:51,360
you're working with the array class and
14955
08:56:51,360 --> 08:56:53,200
that happens to have a method
14956
08:56:53,200 --> 08:56:56,000
called.push and we'll talk about
14957
08:56:56,000 --> 08:56:59,360
everything too we'll talk about methods
14958
08:56:59,360 --> 08:57:02,798
okay we'll talk about properties
14959
08:57:02,798 --> 08:57:04,240
properties
14960
08:57:04,240 --> 08:57:06,000
we'll talk about
14961
08:57:06,000 --> 08:57:07,600
classes
14962
08:57:07,600 --> 08:57:08,478
okay
14963
08:57:08,478 --> 08:57:10,718
that's the main topic right main topic
14964
08:57:10,718 --> 08:57:13,200
is classes and there's all these things
14965
08:57:13,200 --> 08:57:14,958
all right so we're going to be building
14966
08:57:14,958 --> 08:57:18,398
this ferrari right over here now
14967
08:57:18,398 --> 08:57:20,240
let's if you look at this ferrari here
14968
08:57:20,240 --> 08:57:22,240
right what do we have
14969
08:57:22,240 --> 08:57:24,000
we have and excuse my terrible
14970
08:57:24,000 --> 08:57:25,600
photoshopping job but i'm not a
14971
08:57:25,600 --> 08:57:27,920
photoshop expert i'm a developer all
14972
08:57:27,920 --> 08:57:28,878
right
14973
08:57:28,878 --> 08:57:29,760
um
14974
08:57:29,760 --> 08:57:30,878
that's what i'm
14975
08:57:30,878 --> 08:57:33,200
that's my skill not photoshopping so we
14976
08:57:33,200 --> 08:57:35,840
have this ferrari here not fiat because
14977
08:57:35,840 --> 08:57:37,520
that car sucks so
14978
08:57:37,520 --> 08:57:40,398
here we have our ferrari and it will
14979
08:57:40,398 --> 08:57:42,718
have this these these are the properties
14980
08:57:42,718 --> 08:57:43,920
that it'll have okay so you'll be able
14981
08:57:43,920 --> 08:57:47,120
to do car.name car.model car.wait and
14982
08:57:47,120 --> 08:57:49,520
classes are very similar to objects if
14983
08:57:49,520 --> 08:57:52,080
you learn objects
14984
08:57:52,080 --> 08:57:54,638
you know you you you're kind of in a
14985
08:57:54,638 --> 08:57:56,240
you're in a pretty good place like
14986
08:57:56,240 --> 08:57:58,160
you're not going to be too confused to
14987
08:57:58,160 --> 08:58:01,040
as to what is happening over here now
14988
08:58:01,040 --> 08:58:03,760
uh we have this now what are the other
14989
08:58:03,760 --> 08:58:04,878
things that we actually need to know
14990
08:58:04,878 --> 08:58:06,638
about this right we have all these
14991
08:58:06,638 --> 08:58:09,360
properties if you look on the methods
14992
08:58:09,360 --> 08:58:10,798
well look at what kind of methods we
14993
08:58:10,798 --> 08:58:13,760
have we have car.start we have car.drive
14994
08:58:13,760 --> 08:58:16,798
we have car.break and we have car.stop
14995
08:58:16,798 --> 08:58:18,798
now those are all the anything with the
14996
08:58:18,798 --> 08:58:21,280
dot start and the open close parens like
14997
08:58:21,280 --> 08:58:24,478
that those are called methods okay
14998
08:58:24,478 --> 08:58:27,120
and those are basically like functions
14999
08:58:27,120 --> 08:58:30,240
but anytime you have to do car dot start
15000
08:58:30,240 --> 08:58:31,360
then it's just going to be called a
15001
08:58:31,360 --> 08:58:34,160
method okay any any function that needs
15002
08:58:34,160 --> 08:58:36,718
a dot is a method okay that's another
15003
08:58:36,718 --> 08:58:38,318
kind of way to remember it and then
15004
08:58:38,318 --> 08:58:40,160
properties are where you're not calling
15005
08:58:40,160 --> 08:58:41,920
the function with open close parentheses
15006
08:58:41,920 --> 08:58:45,280
so just car.name is ferrari car.model is
15007
08:58:45,280 --> 08:58:48,718
whatever 500 car.weight is 850 kg
15008
08:58:48,718 --> 08:58:51,840
car.color was supposed to be red and i
15009
08:58:51,840 --> 08:58:53,840
photoshopped over it and i made it white
15010
08:58:53,840 --> 08:58:56,318
so whatever color is red just imagine
15011
08:58:56,318 --> 08:58:58,878
it's a red okay so we're gonna go ahead
15012
08:58:58,878 --> 08:59:00,240
and build
15013
08:59:00,240 --> 08:59:02,398
this thing and we're going to do it in a
15014
08:59:02,398 --> 08:59:04,798
class-based way so we can actually
15015
08:59:04,798 --> 08:59:07,280
uh model model this okay and then you'll
15016
08:59:07,280 --> 08:59:09,040
be able to create multiple car objects
15017
08:59:09,040 --> 08:59:11,120
from it so let's go ahead and do that
15018
08:59:11,120 --> 08:59:12,798
now all right so there we go so i'm just
15019
08:59:12,798 --> 08:59:14,798
going to leave this over here so you can
15020
08:59:14,798 --> 08:59:17,360
see it the entire time okay
15021
08:59:17,360 --> 08:59:19,520
all right so
15022
08:59:19,520 --> 08:59:21,600
i'm going to go ahead and do class and
15023
08:59:21,600 --> 08:59:24,160
we're going to do class car and we're
15024
08:59:24,160 --> 08:59:25,680
just going to do this
15025
08:59:25,680 --> 08:59:27,120
all right now
15026
08:59:27,120 --> 08:59:28,638
we're going to need to create a
15027
08:59:28,638 --> 08:59:32,080
constructor and a constructor is really
15028
08:59:32,080 --> 08:59:34,080
going to have like the
15029
08:59:34,080 --> 08:59:35,040
you know
15030
08:59:35,040 --> 08:59:35,840
like
15031
08:59:35,840 --> 08:59:38,318
what are the initial type of arguments
15032
08:59:38,318 --> 08:59:39,840
and things like that we're going to have
15033
08:59:39,840 --> 08:59:41,040
so
15034
08:59:41,040 --> 08:59:43,200
you'll also learn about a new keyword
15035
08:59:43,200 --> 08:59:45,200
and it'll be called this alright so i'll
15036
08:59:45,200 --> 08:59:47,840
show you now so we're going to have
15037
08:59:47,840 --> 08:59:50,718
this dot name
15038
08:59:50,718 --> 08:59:54,160
okay so the car will take in the name
15039
08:59:54,160 --> 08:59:58,000
it will take in let's say a color
15040
08:59:58,000 --> 08:59:59,120
and
15041
08:59:59,120 --> 09:00:00,558
honestly that's kind of it i don't want
15042
09:00:00,558 --> 09:00:03,520
to deal with model and weight yet so
15043
09:00:03,520 --> 09:00:05,280
i'll just take in a name
15044
09:00:05,280 --> 09:00:08,318
and a color okay for now or maybe it
15045
09:00:08,318 --> 09:00:09,100
takes in
15046
09:00:09,100 --> 09:00:11,200
[Music]
15047
09:00:11,200 --> 09:00:14,638
top speed or something like that
15048
09:00:14,638 --> 09:00:16,398
speed
15049
09:00:16,398 --> 09:00:17,600
all right
15050
09:00:17,600 --> 09:00:20,398
and then we so this this dot name will
15051
09:00:20,398 --> 09:00:21,600
be name
15052
09:00:21,600 --> 09:00:23,760
this dot color
15053
09:00:23,760 --> 09:00:25,360
will be color
15054
09:00:25,360 --> 09:00:26,798
and this will make more sense once we
15055
09:00:26,798 --> 09:00:29,200
actually call this object okay and
15056
09:00:29,200 --> 09:00:31,600
this.speed is going to be whatever speed
15057
09:00:31,600 --> 09:00:32,718
you pass in
15058
09:00:32,718 --> 09:00:34,638
so now let's create a new object with
15059
09:00:34,638 --> 09:00:36,798
this class so i'm going to say
15060
09:00:36,798 --> 09:00:38,080
const
15061
09:00:38,080 --> 09:00:38,878
car
15062
09:00:38,878 --> 09:00:41,840
or ferrari
15063
09:00:41,840 --> 09:00:42,958
is
15064
09:00:42,958 --> 09:00:45,200
and we're going to say new car
15065
09:00:45,200 --> 09:00:47,920
okay so note this new car how we're
15066
09:00:47,920 --> 09:00:49,840
doing this new so other things you're
15067
09:00:49,840 --> 09:00:52,718
going to see new with is new date
15068
09:00:52,718 --> 09:00:55,120
okay so data is a class you're going to
15069
09:00:55,120 --> 09:00:57,360
do new promise
15070
09:00:57,360 --> 09:00:59,920
promise as a class okay
15071
09:00:59,920 --> 09:01:02,638
and we're going to pass in our arguments
15072
09:01:02,638 --> 09:01:04,160
here so for name i'm going to say
15073
09:01:04,160 --> 09:01:05,600
ferrari
15074
09:01:05,600 --> 09:01:07,920
for color we're going to say red and for
15075
09:01:07,920 --> 09:01:10,160
speed i don't know i'll say 250 top
15076
09:01:10,160 --> 09:01:12,558
speed and i'm just going to console.log
15077
09:01:12,558 --> 09:01:14,638
ferrari and let's look in the console
15078
09:01:14,638 --> 09:01:16,000
and see what we get
15079
09:01:16,000 --> 09:01:18,798
and boom we just got a car object right
15080
09:01:18,798 --> 09:01:19,920
there
15081
09:01:19,920 --> 09:01:22,638
and it says the name is ferrari the
15082
09:01:22,638 --> 09:01:26,558
color is red and the speed is 250
15083
09:01:26,558 --> 09:01:28,798
right
15084
09:01:28,798 --> 09:01:30,958
beautiful so this is how you will like
15085
09:01:30,958 --> 09:01:32,878
create a class and create a constructor
15086
09:01:32,878 --> 09:01:34,558
that will like initialize things
15087
09:01:34,558 --> 09:01:37,760
instantly now this part this is the
15088
09:01:37,760 --> 09:01:39,920
keyword that throws a lot of people off
15089
09:01:39,920 --> 09:01:42,318
like what is this
15090
09:01:42,318 --> 09:01:45,440
what this refers to guys is
15091
09:01:45,440 --> 09:01:47,360
you know how we do
15092
09:01:47,360 --> 09:01:48,080
uh
15093
09:01:48,080 --> 09:01:49,360
the the name
15094
09:01:49,360 --> 09:01:50,798
well
15095
09:01:50,798 --> 09:01:53,280
this refers to
15096
09:01:53,280 --> 09:01:56,080
ferrari so now when i do so watch this
15097
09:01:56,080 --> 09:01:58,558
right so if i do ferrari
15098
09:01:58,558 --> 09:02:01,360
dot name and run it
15099
09:02:01,360 --> 09:02:03,520
i'll actually get back the name ferrari
15100
09:02:03,520 --> 09:02:05,280
okay so let me remove the other console
15101
09:02:05,280 --> 09:02:08,080
log just so we can see ferrari now if i
15102
09:02:08,080 --> 09:02:11,440
remove this dot name watch what happens
15103
09:02:11,440 --> 09:02:13,200
it says undefined
15104
09:02:13,200 --> 09:02:14,240
okay
15105
09:02:14,240 --> 09:02:16,478
so this here in this case becomes
15106
09:02:16,478 --> 09:02:17,700
ferrari ferrari.name
15107
09:02:17,700 --> 09:02:19,040
[Music]
15108
09:02:19,040 --> 09:02:21,200
and we set its name to the whatever name
15109
09:02:21,200 --> 09:02:23,600
was passed in so say it we had passed in
15110
09:02:23,600 --> 09:02:25,120
ferrari
15111
09:02:25,120 --> 09:02:28,160
so then it becomes ferrari like this so
15112
09:02:28,160 --> 09:02:30,558
it goes ferrari.name and sets it to
15113
09:02:30,558 --> 09:02:32,638
ferrari okay
15114
09:02:32,638 --> 09:02:34,878
so
15115
09:02:34,958 --> 09:02:38,638
in other words this argument
15116
09:02:38,638 --> 09:02:41,040
gets passed into name
15117
09:02:41,040 --> 09:02:42,718
and name is the variable that goes right
15118
09:02:42,718 --> 09:02:44,958
here so that's going to be ferrari
15119
09:02:44,958 --> 09:02:47,040
and this specific ferrari object that we
15120
09:02:47,040 --> 09:02:49,120
just created
15121
09:02:49,120 --> 09:02:51,200
that is referred to by this so
15122
09:02:51,200 --> 09:02:52,878
everywhere we said this
15123
09:02:52,878 --> 09:02:54,160
it's being replaced with the word
15124
09:02:54,160 --> 09:02:55,520
ferrari
15125
09:02:55,520 --> 09:02:56,398
all right
15126
09:02:56,398 --> 09:02:59,440
that's what this does
15127
09:02:59,440 --> 09:03:02,478
all right so if i now go this.color and
15128
09:03:02,478 --> 09:03:04,958
run this
15129
09:03:05,200 --> 09:03:09,440
let's go console.log ferrari dot name
15130
09:03:09,440 --> 09:03:11,680
and then we'll do
15131
09:03:11,680 --> 09:03:16,000
speed run this and we get ferrari red
15132
09:03:16,000 --> 09:03:17,760
250.
15133
09:03:17,760 --> 09:03:20,240
okay so you can see we've already
15134
09:03:20,240 --> 09:03:23,920
created three properties that this has
15135
09:03:23,920 --> 09:03:25,440
okay so these are
15136
09:03:25,440 --> 09:03:26,840
these are
15137
09:03:26,840 --> 09:03:29,600
properties properties
15138
09:03:29,600 --> 09:03:30,478
okay
15139
09:03:30,478 --> 09:03:31,760
now we're going to go ahead and create
15140
09:03:31,760 --> 09:03:33,840
certain methods for example let's create
15141
09:03:33,840 --> 09:03:36,160
a method that's um
15142
09:03:36,160 --> 09:03:37,160
you know
15143
09:03:37,160 --> 09:03:40,478
car.start or car.drive
15144
09:03:40,478 --> 09:03:41,440
okay
15145
09:03:41,440 --> 09:03:42,558
um
15146
09:03:42,558 --> 09:03:45,680
car dot break so let's say we have
15147
09:03:45,680 --> 09:03:47,840
let's say we do car
15148
09:03:47,840 --> 09:03:50,878
let's say we do drive
15149
09:03:51,360 --> 09:03:56,120
all right and let's just say console log
15150
09:03:56,160 --> 09:03:58,000
just drove
15151
09:03:58,000 --> 09:03:59,760
two miles
15152
09:03:59,760 --> 09:04:00,638
okay
15153
09:04:00,638 --> 09:04:02,000
now what we can do is we can go
15154
09:04:02,000 --> 09:04:05,600
console.log and i can say ferrari
15155
09:04:05,600 --> 09:04:06,798
in this case i don't even need to
15156
09:04:06,798 --> 09:04:08,718
console.log it because console.log is
15157
09:04:08,718 --> 09:04:10,638
inside of the drive method so we can
15158
09:04:10,638 --> 09:04:13,920
actually just say ferrari dot drive
15159
09:04:13,920 --> 09:04:16,638
open close parens
15160
09:04:16,638 --> 09:04:18,160
and when i run this watch what's gonna
15161
09:04:18,160 --> 09:04:19,520
happen
15162
09:04:19,520 --> 09:04:23,120
boom it says just drove two miles
15163
09:04:23,120 --> 09:04:25,680
okay so that's the drive method
15164
09:04:25,680 --> 09:04:28,638
all right let's do car. let's do break
15165
09:04:28,638 --> 09:04:30,080
method right
15166
09:04:30,080 --> 09:04:30,798
so
15167
09:04:30,798 --> 09:04:32,558
how are we going to do break let's say
15168
09:04:32,558 --> 09:04:34,478
for break we just want a console log and
15169
09:04:34,478 --> 09:04:36,240
we want to say came to a screeching halt
15170
09:04:36,240 --> 09:04:38,558
so you try that and see if you could
15171
09:04:38,558 --> 09:04:40,638
call dot break
15172
09:04:40,638 --> 09:04:42,160
take a few seconds this should take you
15173
09:04:42,160 --> 09:04:43,600
only five or seven seconds to do
15174
09:04:43,600 --> 09:04:45,760
yourself it's going to be only one line
15175
09:04:45,760 --> 09:04:48,000
of code just like on line 18 and then
15176
09:04:48,000 --> 09:04:49,600
you should be able to refer to ferrari
15177
09:04:49,600 --> 09:04:51,520
dot break so give that a try make sure
15178
09:04:51,520 --> 09:04:53,120
you're practicing with me the entire
15179
09:04:53,120 --> 09:04:55,040
time so i'll give you five seconds
15180
09:04:55,040 --> 09:04:56,160
five
15181
09:04:56,160 --> 09:04:57,040
four
15182
09:04:57,040 --> 09:04:58,000
three
15183
09:04:58,000 --> 09:04:58,878
two
15184
09:04:58,878 --> 09:05:01,520
one okay let's go
15185
09:05:01,520 --> 09:05:03,520
so i'm going to go ahead here and say
15186
09:05:03,520 --> 09:05:05,680
console.log
15187
09:05:05,680 --> 09:05:07,840
say
15188
09:05:07,840 --> 09:05:10,558
uh coming to a
15189
09:05:10,558 --> 09:05:12,798
breaking come breaking
15190
09:05:12,798 --> 09:05:15,200
coming to a screeching
15191
09:05:15,200 --> 09:05:16,080
halt
15192
09:05:16,080 --> 09:05:18,160
or something like that right let's hit
15193
09:05:18,160 --> 09:05:19,040
run
15194
09:05:19,040 --> 09:05:22,080
and let's do ferrari
15195
09:05:22,080 --> 09:05:24,958
dot break
15196
09:05:25,440 --> 09:05:28,080
breaking coming to a screeching halt
15197
09:05:28,080 --> 09:05:29,600
there you go
15198
09:05:29,600 --> 09:05:32,000
okay and uh
15199
09:05:32,000 --> 09:05:33,840
what are other methods right so we have
15200
09:05:33,840 --> 09:05:36,240
methods like stop so that is probably
15201
09:05:36,240 --> 09:05:38,160
like car the car coming to a complete
15202
09:05:38,160 --> 09:05:39,280
stop
15203
09:05:39,280 --> 09:05:40,638
so
15204
09:05:40,638 --> 09:05:43,360
let's say that the car starts off with
15205
09:05:43,360 --> 09:05:45,520
like a speed of uh
15206
09:05:45,520 --> 09:05:47,120
okay so let's say we have a current
15207
09:05:47,120 --> 09:05:49,440
speed as well so we'll go this dot in
15208
09:05:49,440 --> 09:05:51,760
everywhere we have speed let's say this
15209
09:05:51,760 --> 09:05:54,878
is we we change this with the top speed
15210
09:05:54,878 --> 09:05:57,200
okay so i'm going to right click rename
15211
09:05:57,200 --> 09:05:58,878
all the occurrences i'm going to say top
15212
09:05:58,878 --> 09:06:00,558
speed so maybe this is passing in the
15213
09:06:00,558 --> 09:06:02,798
top speed just to make that very clear
15214
09:06:02,798 --> 09:06:04,398
now we'll have
15215
09:06:04,398 --> 09:06:06,558
this dot current speed
15216
09:06:06,558 --> 09:06:08,800
is going to be zero and start all right
15217
09:06:08,800 --> 09:06:11,278
so whenever a new car is created well it
15218
09:06:11,278 --> 09:06:13,680
isn't driving right now so it'll be zero
15219
09:06:13,680 --> 09:06:15,278
but then let's say we
15220
09:06:15,278 --> 09:06:17,680
start driving the car
15221
09:06:17,680 --> 09:06:20,160
then maybe the speed goes up by 10 mile
15222
09:06:20,160 --> 09:06:22,480
uh 10 miles an hour or something
15223
09:06:22,480 --> 09:06:25,040
yeah so how we could actually do that is
15224
09:06:25,040 --> 09:06:27,598
in our drive method i can just increment
15225
09:06:27,598 --> 09:06:32,078
this dot current speed current speed by
15226
09:06:32,078 --> 09:06:33,598
10
15227
09:06:33,598 --> 09:06:35,917
okay and that just increase the speed by
15228
09:06:35,917 --> 09:06:37,278
10
15229
09:06:37,278 --> 09:06:39,758
and we'll say console log
15230
09:06:39,758 --> 09:06:43,160
and i'll say
15231
09:06:44,800 --> 09:06:46,960
driving
15232
09:06:46,960 --> 09:06:49,840
driving at
15233
09:06:50,480 --> 09:06:52,398
and we'll put
15234
09:06:52,398 --> 09:06:56,680
this dot current speed
15235
09:06:59,680 --> 09:07:00,557
okay
15236
09:07:00,557 --> 09:07:03,597
so let's hit run
15237
09:07:04,000 --> 09:07:05,917
and now you can see it says driving
15238
09:07:05,917 --> 09:07:09,040
speed at 10 miles an hour so now we have
15239
09:07:09,040 --> 09:07:11,122
a new property called desktop current
15240
09:07:11,122 --> 09:07:14,078
speed so i can actually go ferrari
15241
09:07:14,078 --> 09:07:15,680
dot
15242
09:07:15,680 --> 09:07:17,360
current speed
15243
09:07:17,360 --> 09:07:19,598
and we can see that
15244
09:07:19,598 --> 09:07:21,278
oh no sorry it's not a function so let's
15245
09:07:21,278 --> 09:07:23,758
run this again it's not a method
15246
09:07:23,758 --> 09:07:25,680
so current speed right now is you can
15247
09:07:25,680 --> 09:07:27,360
see undefined
15248
09:07:27,360 --> 09:07:29,598
oh i'm sorry the
15249
09:07:29,598 --> 09:07:31,758
and if the speed here is undefined
15250
09:07:31,758 --> 09:07:33,758
because this is called top speed my
15251
09:07:33,758 --> 09:07:35,839
apologies
15252
09:07:35,839 --> 09:07:38,000
but if you if you console log dot
15253
09:07:38,000 --> 09:07:40,000
current speed you can actually see that
15254
09:07:40,000 --> 09:07:42,718
it says it's currently 10 miles an hour
15255
09:07:42,718 --> 09:07:44,877
right so let's run this
15256
09:07:44,877 --> 09:07:47,519
and you can see it says 10. okay i'm
15257
09:07:47,519 --> 09:07:49,598
gonna comment out all the other console
15258
09:07:49,598 --> 09:07:51,680
logs so it doesn't get like super
15259
09:07:51,680 --> 09:07:54,800
confusing as to what's happening and
15260
09:07:54,800 --> 09:07:56,800
i'll console log this just drove two
15261
09:07:56,800 --> 09:07:58,800
miles an hour as well
15262
09:07:58,800 --> 09:08:00,877
right so it says driving speed at 10
15263
09:08:00,877 --> 09:08:02,718
miles an hour
15264
09:08:02,718 --> 09:08:05,438
and let's console log before we started
15265
09:08:05,438 --> 09:08:09,320
driving the car as well
15266
09:08:09,758 --> 09:08:11,519
so in the start you can see the speed is
15267
09:08:11,519 --> 09:08:12,960
zero
15268
09:08:12,960 --> 09:08:14,877
right here
15269
09:08:14,877 --> 09:08:16,877
right if you look
15270
09:08:16,877 --> 09:08:18,480
we see that the speed is zero right
15271
09:08:18,480 --> 09:08:20,718
there
15272
09:08:20,718 --> 09:08:23,917
right then after we drive
15273
09:08:23,917 --> 09:08:25,758
you can see that the speed is 10 miles
15274
09:08:25,758 --> 09:08:27,122
an hour
15275
09:08:27,122 --> 09:08:29,598
well what if we
15276
09:08:29,598 --> 09:08:31,680
do the drive method again what will our
15277
09:08:31,680 --> 09:08:33,278
speed be can you guess
15278
09:08:33,278 --> 09:08:34,877
take five seconds and try to guess this
15279
09:08:34,877 --> 09:08:38,078
five four three two one
15280
09:08:38,078 --> 09:08:39,839
okay let's see if you guessed it right
15281
09:08:39,839 --> 09:08:42,800
beautiful okay nice if you guessed 20
15282
09:08:42,800 --> 09:08:45,519
because it increments it by 10 and if
15283
09:08:45,519 --> 09:08:48,960
you do drive multiple if i do drive two
15284
09:08:48,960 --> 09:08:51,199
more times what is current speed going
15285
09:08:51,199 --> 09:08:53,040
to be at this point take a guess i'll
15286
09:08:53,040 --> 09:08:54,960
give you five seconds five four three
15287
09:08:54,960 --> 09:08:56,557
two one
15288
09:08:56,557 --> 09:08:58,800
and it's gonna be 40 miles an hour okay
15289
09:08:58,800 --> 09:09:00,398
driving speed at 30 miles an hour
15290
09:09:00,398 --> 09:09:03,598
driving speed at 40 miles an hour
15291
09:09:03,598 --> 09:09:05,758
so the car is getting like faster and
15292
09:09:05,758 --> 09:09:06,800
faster
15293
09:09:06,800 --> 09:09:08,160
now what we could do is we could kind of
15294
09:09:08,160 --> 09:09:10,877
get fancy and we could do a set timeout
15295
09:09:10,877 --> 09:09:14,000
where basically if we drive for one
15296
09:09:14,000 --> 09:09:15,040
second
15297
09:09:15,040 --> 09:09:17,519
uh it gets up to a certain certain speed
15298
09:09:17,519 --> 09:09:19,278
if we drive for two seconds maybe it
15299
09:09:19,278 --> 09:09:21,078
gets up to like
15300
09:09:21,078 --> 09:09:23,438
35 miles an hour or something and then
15301
09:09:23,438 --> 09:09:26,480
when we get to 2.8 or 2.9 seconds
15302
09:09:26,480 --> 09:09:29,598
maybe it's it does 60 miles an hour
15303
09:09:29,598 --> 09:09:32,877
right so what is that actually um
15304
09:09:32,877 --> 09:09:34,480
ferrari
15305
09:09:34,480 --> 09:09:38,078
let's see 0 to 60
15306
09:09:38,078 --> 09:09:42,238
how long does it do it in about 3 point
15307
09:09:42,640 --> 09:09:44,480
how fast is a ferrari
15308
09:09:44,480 --> 09:09:47,199
so let's see their seconds right so
15309
09:09:47,199 --> 09:09:50,718
three seconds 2.9 seconds 2.5 seconds
15310
09:09:50,718 --> 09:09:52,960
what's crazy is that rivian which is one
15311
09:09:52,960 --> 09:09:55,199
of these trucks that is like my absolute
15312
09:09:55,199 --> 09:09:56,480
favorite right now
15313
09:09:56,480 --> 09:09:58,800
dad that is like faster than a ferrari
15314
09:09:58,800 --> 09:10:01,519
and it's a truck and it does it in three
15315
09:10:01,519 --> 09:10:02,557
seconds
15316
09:10:02,557 --> 09:10:05,199
so some ferraris we have go
15317
09:10:05,199 --> 09:10:07,040
and do it in 2.9 seconds whatever we'll
15318
09:10:07,040 --> 09:10:09,839
just do 2.9 seconds right
15319
09:10:09,839 --> 09:10:11,278
so
15320
09:10:11,278 --> 09:10:12,960
what we can do is we can even like get
15321
09:10:12,960 --> 09:10:16,398
fancy with it and we can create a method
15322
09:10:16,398 --> 09:10:19,360
that like takes us to 60 seconds 60
15323
09:10:19,360 --> 09:10:20,877
miles an hour
15324
09:10:20,877 --> 09:10:23,278
right so let's call it zero
15325
09:10:23,278 --> 09:10:26,278
260.
15326
09:10:26,398 --> 09:10:30,000
and what this does is maybe it runs a
15327
09:10:30,000 --> 09:10:32,800
set timeout
15328
09:10:33,199 --> 09:10:36,877
and then it console logs
15329
09:10:40,960 --> 09:10:42,640
that was fast
15330
09:10:42,640 --> 09:10:44,078
or something right
15331
09:10:44,078 --> 09:10:46,877
and let's make let's put some squigglies
15332
09:10:46,877 --> 09:10:48,640
around this actually
15333
09:10:48,640 --> 09:10:50,718
because we want this to be like a
15334
09:10:50,718 --> 09:10:51,917
function
15335
09:10:51,917 --> 09:10:53,438
and we want to write probably more
15336
09:10:53,438 --> 09:10:54,557
things in it
15337
09:10:54,557 --> 09:10:56,480
and then we want to set the current
15338
09:10:56,480 --> 09:10:58,960
speed
15339
09:10:59,839 --> 09:11:01,278
to 60
15340
09:11:01,278 --> 09:11:02,398
because i'm going to set the current
15341
09:11:02,398 --> 09:11:03,839
speed to 60.
15342
09:11:03,839 --> 09:11:06,398
so let's see if this works kind of as
15343
09:11:06,398 --> 09:11:08,640
expected so we're going to do console
15344
09:11:08,640 --> 09:11:09,598
log
15345
09:11:09,598 --> 09:11:11,122
we don't need to console.log we can
15346
09:11:11,122 --> 09:11:13,519
actually just do ferrari
15347
09:11:13,519 --> 09:11:14,480
dot
15348
09:11:14,480 --> 09:11:16,480
0 to 60
15349
09:11:16,480 --> 09:11:18,640
and run this
15350
09:11:18,640 --> 09:11:20,078
and
15351
09:11:20,078 --> 09:11:22,237
let's see
15352
09:11:22,237 --> 09:11:26,160
that was fast okay and now if we check
15353
09:11:26,160 --> 09:11:28,398
so like if i comment out all these other
15354
09:11:28,398 --> 09:11:30,800
lines
15355
09:11:30,800 --> 09:11:33,839
and uh let's comment out this line as
15356
09:11:33,839 --> 09:11:35,122
well
15357
09:11:35,122 --> 09:11:36,480
and this one
15358
09:11:36,480 --> 09:11:38,398
and now let's just check the speed so
15359
09:11:38,398 --> 09:11:40,557
i'm just gonna get this console log one
15360
09:11:40,557 --> 09:11:42,160
and put it right here and run it that
15361
09:11:42,160 --> 09:11:45,199
was fast so because it's asynchronous
15362
09:11:45,199 --> 09:11:46,877
notice what happened
15363
09:11:46,877 --> 09:11:50,480
it ran and told me what its speed was
15364
09:11:50,480 --> 09:11:53,360
actually before that code finished
15365
09:11:53,360 --> 09:11:57,122
running so we what we want to do
15366
09:11:57,122 --> 09:11:59,360
is we could turn this actually into a
15367
09:11:59,360 --> 09:12:01,360
promise if we wanted to get kind of
15368
09:12:01,360 --> 09:12:03,680
advanced with it and then get this
15369
09:12:03,680 --> 09:12:05,438
promise and then once it resolves we
15370
09:12:05,438 --> 09:12:07,438
could do something with it if we want so
15371
09:12:07,438 --> 09:12:09,040
we could do
15372
09:12:09,040 --> 09:12:11,598
return new
15373
09:12:11,598 --> 09:12:13,519
promise
15374
09:12:13,519 --> 09:12:16,800
and then we can like
15375
09:12:16,800 --> 09:12:18,960
resolve
15376
09:12:18,960 --> 09:12:21,438
resolve comma reject
15377
09:12:21,438 --> 09:12:23,598
and kind of get fancy with it
15378
09:12:23,598 --> 09:12:26,320
but we can try doing a simpler way first
15379
09:12:26,320 --> 09:12:27,040
because
15380
09:12:27,040 --> 09:12:29,438
simpler ways are usually you know we
15381
09:12:29,438 --> 09:12:30,557
like them
15382
09:12:30,557 --> 09:12:32,398
and we can actually just
15383
09:12:32,398 --> 09:12:35,680
grab the current speed like this okay so
15384
09:12:35,680 --> 09:12:38,320
we can grab the current speed from here
15385
09:12:38,320 --> 09:12:40,398
after it changes it to 60. so let's try
15386
09:12:40,398 --> 09:12:41,598
running it now
15387
09:12:41,598 --> 09:12:44,398
and you can see that the current speed
15388
09:12:44,398 --> 09:12:47,438
uh at this point has been
15389
09:12:47,438 --> 09:12:50,640
you know turned into 60.
15390
09:12:50,640 --> 09:12:53,360
so if we do it like this
15391
09:12:53,360 --> 09:12:55,278
it'll work and it'll it'll do what we
15392
09:12:55,278 --> 09:12:56,718
expect it to do
15393
09:12:56,718 --> 09:12:58,877
now for break what we want to do is see
15394
09:12:58,877 --> 09:13:01,040
if you can make it where break brings
15395
09:13:01,040 --> 09:13:02,480
the speed
15396
09:13:02,480 --> 09:13:05,040
down by 10. okay so if drive brings the
15397
09:13:05,040 --> 09:13:07,598
speed up by 10 see if you can make it
15398
09:13:07,598 --> 09:13:08,320
where
15399
09:13:08,320 --> 09:13:10,320
brake brings the speed down by 10 okay
15400
09:13:10,320 --> 09:13:12,078
so give it a try pause the video i'll
15401
09:13:12,078 --> 09:13:13,598
give you five seconds to pause the video
15402
09:13:13,598 --> 09:13:15,040
and try it on your own i think you'll be
15403
09:13:15,040 --> 09:13:17,278
able to do it yourself okay so
15404
09:13:17,278 --> 09:13:19,360
let's do a countdown five
15405
09:13:19,360 --> 09:13:21,917
four three two
15406
09:13:21,917 --> 09:13:22,960
one
15407
09:13:22,960 --> 09:13:25,839
okay let's give it a try now
15408
09:13:25,839 --> 09:13:27,758
so let's do it together if you hadn't
15409
09:13:27,758 --> 09:13:30,078
already and if you did great job okay
15410
09:13:30,078 --> 09:13:31,598
and if you didn't it's okay we'll figure
15411
09:13:31,598 --> 09:13:33,040
it out together
15412
09:13:33,040 --> 09:13:35,680
so for break what i want to do is i
15413
09:13:35,680 --> 09:13:37,519
basically want to say
15414
09:13:37,519 --> 09:13:40,000
hey um i can remove this console log for
15415
09:13:40,000 --> 09:13:42,640
now don't care about it and i will
15416
09:13:42,640 --> 09:13:46,877
simply say set the current speed
15417
09:13:46,877 --> 09:13:49,121
to
15418
09:13:49,360 --> 09:13:51,360
um
15419
09:13:51,360 --> 09:13:53,040
minus equals
15420
09:13:53,040 --> 09:13:54,237
10.
15421
09:13:54,237 --> 09:13:56,398
okay so this will just decrement the
15422
09:13:56,398 --> 09:13:58,640
speed by 10.
15423
09:13:58,640 --> 09:14:00,000
and we'll
15424
09:14:00,000 --> 09:14:01,839
we'll put a console log here and we'll
15425
09:14:01,839 --> 09:14:03,917
say
15426
09:14:03,917 --> 09:14:05,122
breaking
15427
09:14:05,122 --> 09:14:06,800
so
15428
09:14:06,800 --> 09:14:08,960
let's comment out all of these lines
15429
09:14:08,960 --> 09:14:10,000
here
15430
09:14:10,000 --> 09:14:11,360
okay
15431
09:14:11,360 --> 09:14:13,680
and let's comment down this one and i'll
15432
09:14:13,680 --> 09:14:15,278
just go right here
15433
09:14:15,278 --> 09:14:18,800
so we got the ferrari
15434
09:14:18,800 --> 09:14:20,320
right we have the ferrari now what do we
15435
09:14:20,320 --> 09:14:23,199
want to do i do ferrari dot drive so
15436
09:14:23,199 --> 09:14:25,040
this should bring it up by 10 miles an
15437
09:14:25,040 --> 09:14:27,199
hour and let's print out the speed so
15438
09:14:27,199 --> 09:14:29,839
i'll console log
15439
09:14:29,839 --> 09:14:30,800
speed
15440
09:14:30,800 --> 09:14:32,960
or sorry ferrari
15441
09:14:32,960 --> 09:14:35,519
dot current speed
15442
09:14:35,519 --> 09:14:37,680
so let's run this and the current speed
15443
09:14:37,680 --> 09:14:39,199
is 10 miles an hour
15444
09:14:39,199 --> 09:14:43,278
okay now let's do ferrari dot break
15445
09:14:43,278 --> 09:14:45,519
and then we'll console log
15446
09:14:45,519 --> 09:14:47,598
the speed
15447
09:14:47,598 --> 09:14:50,800
and you can see that when i did drive
15448
09:14:50,800 --> 09:14:52,877
it increased it by 10 miles an hour and
15449
09:14:52,877 --> 09:14:54,877
then breaking it
15450
09:14:54,877 --> 09:14:56,960
decreased it by 10 and so now we're at
15451
09:14:56,960 --> 09:14:59,199
zero miles an hour okay
15452
09:14:59,199 --> 09:15:02,718
now what if we wanted to make stop where
15453
09:15:02,718 --> 09:15:04,557
stop essentially just brings the speed
15454
09:15:04,557 --> 09:15:06,237
down to zero regardless of where you're
15455
09:15:06,237 --> 09:15:09,598
at so to emphasize that first let's do
15456
09:15:09,598 --> 09:15:12,320
drive multiple times here
15457
09:15:12,320 --> 09:15:14,640
and see if before i continue the video
15458
09:15:14,640 --> 09:15:16,320
if you can guess what the speed is going
15459
09:15:16,320 --> 09:15:17,199
to be
15460
09:15:17,199 --> 09:15:19,360
on line 46 so i'll give you five seconds
15461
09:15:19,360 --> 09:15:20,718
to figure that out
15462
09:15:20,718 --> 09:15:21,680
5
15463
09:15:21,680 --> 09:15:22,480
four
15464
09:15:22,480 --> 09:15:23,360
three
15465
09:15:23,360 --> 09:15:24,237
two
15466
09:15:24,237 --> 09:15:25,839
one
15467
09:15:25,839 --> 09:15:27,598
all right so how i'm going to figure
15468
09:15:27,598 --> 09:15:31,519
this out is one two three four five five
15469
09:15:31,519 --> 09:15:34,078
multiplied by 10 is 50 so this should
15470
09:15:34,078 --> 09:15:36,237
get you to 50 miles an hour they should
15471
09:15:36,237 --> 09:15:37,839
decrement by 10
15472
09:15:37,839 --> 09:15:39,680
this should get us 40 miles an hour
15473
09:15:39,680 --> 09:15:42,800
let's run this and we get our speed at
15474
09:15:42,800 --> 09:15:45,122
40 miles an hour okay
15475
09:15:45,122 --> 09:15:46,640
so now what we want to do is we want to
15476
09:15:46,640 --> 09:15:47,839
implement the
15477
09:15:47,839 --> 09:15:50,480
stop method okay so try to implement it
15478
09:15:50,480 --> 09:15:52,960
yourself i'll give you some time to do
15479
09:15:52,960 --> 09:15:56,078
it you should be able to do it okay and
15480
09:15:56,078 --> 09:15:57,598
if you're not going to pause the video
15481
09:15:57,598 --> 09:15:59,360
or if you have paused the video and
15482
09:15:59,360 --> 09:16:00,640
you're back
15483
09:16:00,640 --> 09:16:02,960
let's figure it out together now so i'm
15484
09:16:02,960 --> 09:16:05,840
just going to say
15485
09:16:05,917 --> 09:16:07,758
now i guess it would make more sense to
15486
09:16:07,758 --> 09:16:09,278
say coming
15487
09:16:09,278 --> 09:16:11,278
to a screeching
15488
09:16:11,278 --> 09:16:12,877
halt
15489
09:16:12,877 --> 09:16:15,122
right and then we can actually say this
15490
09:16:15,122 --> 09:16:16,877
dot current speed and we can actually
15491
09:16:16,877 --> 09:16:19,680
set it to just equal to zero okay so
15492
09:16:19,680 --> 09:16:21,278
like basically we're going to reset it
15493
09:16:21,278 --> 09:16:23,680
to zero
15494
09:16:23,680 --> 09:16:26,237
so let's try
15495
09:16:26,237 --> 09:16:28,160
ferrari dot
15496
09:16:28,160 --> 09:16:29,519
stop
15497
09:16:29,519 --> 09:16:31,758
and now can you guess what line 52 is
15498
09:16:31,758 --> 09:16:32,877
gonna say
15499
09:16:32,877 --> 09:16:35,839
okay take a guess there and let's run
15500
09:16:35,839 --> 09:16:37,758
and boom you can see that now we're at
15501
09:16:37,758 --> 09:16:40,237
zero miles an hour all right so we were
15502
09:16:40,237 --> 09:16:43,122
driving at 10 driving 20 driving speed
15503
09:16:43,122 --> 09:16:44,960
30 40 50
15504
09:16:44,960 --> 09:16:47,680
50 and then we were breaking and then we
15505
09:16:47,680 --> 09:16:49,519
came to a screeching halt and now we're
15506
09:16:49,519 --> 09:16:51,519
at zero miles an hour and the car has
15507
09:16:51,519 --> 09:16:52,960
effectively
15508
09:16:52,960 --> 09:16:56,000
come to a full stop
15509
09:16:56,000 --> 09:16:58,320
all right now let's grab a different car
15510
09:16:58,320 --> 09:17:01,598
let's get a porsche right right here and
15511
09:17:01,598 --> 09:17:05,278
let's see if you can create
15512
09:17:05,278 --> 09:17:08,398
uh a new object with a porsche so give
15513
09:17:08,398 --> 09:17:12,199
that a try and let's say its speed is
15514
09:17:12,199 --> 09:17:15,278
250 miles an hour as well okay or 240
15515
09:17:15,278 --> 09:17:16,640
miles an hour
15516
09:17:16,640 --> 09:17:18,879
so you have a porsche
15517
09:17:18,879 --> 09:17:20,078
[Music]
15518
09:17:20,078 --> 09:17:22,398
porsche
15519
09:17:22,398 --> 09:17:23,758
okay
15520
09:17:23,758 --> 09:17:26,718
color is yellow right and then speed of
15521
09:17:26,718 --> 09:17:29,519
this is let's say 250 miles an hour so
15522
09:17:29,519 --> 09:17:31,839
make a new object with this and then see
15523
09:17:31,839 --> 09:17:33,917
if you could do things like drive it or
15524
09:17:33,917 --> 09:17:35,438
get its current speed and things of that
15525
09:17:35,438 --> 09:17:38,398
nature give that a try
15526
09:17:38,398 --> 09:17:40,877
give that a try
15527
09:17:40,877 --> 09:17:43,680
okay
15528
09:17:43,680 --> 09:17:46,398
and while you give that a try
15529
09:17:46,398 --> 09:17:49,519
one thing we can do is um let's go down
15530
09:17:49,519 --> 09:17:51,680
here okay give that a try pause the
15531
09:17:51,680 --> 09:17:53,040
video and see if you can actually try
15532
09:17:53,040 --> 09:17:54,640
this on your own so you take five
15533
09:17:54,640 --> 09:17:56,480
seconds five four
15534
09:17:56,480 --> 09:17:58,640
three two one
15535
09:17:58,640 --> 09:17:59,519
okay
15536
09:17:59,519 --> 09:18:00,640
now
15537
09:18:00,640 --> 09:18:02,640
here's how you do it okay let's do it
15538
09:18:02,640 --> 09:18:04,237
together so
15539
09:18:04,237 --> 09:18:05,758
i'm going to make
15540
09:18:05,758 --> 09:18:07,040
porsche
15541
09:18:07,040 --> 09:18:07,917
right
15542
09:18:07,917 --> 09:18:10,000
and it's gonna be a new car and i'm
15543
09:18:10,000 --> 09:18:11,040
gonna say
15544
09:18:11,040 --> 09:18:13,360
porsche
15545
09:18:13,360 --> 09:18:16,320
and color is yellow
15546
09:18:16,320 --> 09:18:19,040
and top speed is let's say 250 miles an
15547
09:18:19,040 --> 09:18:20,480
hour
15548
09:18:20,480 --> 09:18:22,480
okay
15549
09:18:22,480 --> 09:18:28,000
and uh let's go ahead and do porsche
15550
09:18:28,000 --> 09:18:30,160
dot name
15551
09:18:30,160 --> 09:18:33,122
and we'll console log it
15552
09:18:33,122 --> 09:18:35,839
and we'll do dot color
15553
09:18:35,839 --> 09:18:37,360
and we'll do
15554
09:18:37,360 --> 09:18:38,800
top speed
15555
09:18:38,800 --> 09:18:40,398
and let's run this and see if we
15556
09:18:40,398 --> 09:18:42,800
actually get it yep we get porsche
15557
09:18:42,800 --> 09:18:47,278
it's yellow and the top speed is 250.
15558
09:18:47,278 --> 09:18:49,122
can we drive it let's try it
15559
09:18:49,122 --> 09:18:51,758
porsche.drive
15560
09:18:51,758 --> 09:18:53,360
and let's do let's drive it like
15561
09:18:53,360 --> 09:18:56,000
multiple times
15562
09:18:56,000 --> 09:18:57,438
all right
15563
09:18:57,438 --> 09:19:00,078
and you can see boom the top speed uh
15564
09:19:00,078 --> 09:19:02,000
now we're at 50 miles an hour if you
15565
09:19:02,000 --> 09:19:03,839
want you could also turn it into a loop
15566
09:19:03,839 --> 09:19:05,758
right so we could basically say
15567
09:19:05,758 --> 09:19:07,758
you could run it for
15568
09:19:07,758 --> 09:19:09,360
five times
15569
09:19:09,360 --> 09:19:11,839
or more if you want so let's say we have
15570
09:19:11,839 --> 09:19:14,237
an array of one two three four five
15571
09:19:14,237 --> 09:19:16,800
and i can say for each
15572
09:19:16,800 --> 09:19:20,000
and i can say
15573
09:19:20,000 --> 09:19:22,960
i don't care about the value
15574
09:19:22,960 --> 09:19:25,200
i don't need to use it and i can simply
15575
09:19:25,200 --> 09:19:26,400
say
15576
09:19:26,400 --> 09:19:26,940
porsche
15577
09:19:26,940 --> 09:19:30,279
[Music]
15578
09:19:31,040 --> 09:19:32,480
dot drive
15579
09:19:32,480 --> 09:19:34,640
so all this function is doing is i don't
15580
09:19:34,640 --> 09:19:36,720
care about these
15581
09:19:36,720 --> 09:19:39,520
and i'm just saying hey increment
15582
09:19:39,520 --> 09:19:42,080
porsche.draw uh just do porsha.drive
15583
09:19:42,080 --> 09:19:44,240
essentially five times so this gives me
15584
09:19:44,240 --> 09:19:46,080
a loop that lets me run this piece of
15585
09:19:46,080 --> 09:19:48,400
code five times let's try it
15586
09:19:48,400 --> 09:19:49,756
okay
15587
09:19:49,756 --> 09:19:52,960
i cannot read properties of undefined on
15588
09:19:52,960 --> 09:19:54,560
line five
15589
09:19:54,560 --> 09:19:56,160
interesting
15590
09:19:56,160 --> 09:19:58,320
line five
15591
09:19:58,320 --> 09:20:02,436
what's happening on line five
15592
09:20:03,276 --> 09:20:06,480
okay let's comment this out i guess
15593
09:20:06,480 --> 09:20:09,200
let's try it
15594
09:20:09,200 --> 09:20:11,680
okay so actually let's do this because
15595
09:20:11,680 --> 09:20:15,120
nums is also of class type array
15596
09:20:15,120 --> 09:20:17,680
or sorry array arrays are
15597
09:20:17,680 --> 09:20:20,080
class type array so you have to
15598
09:20:20,080 --> 09:20:22,880
instantiate them first and then you can
15599
09:20:22,880 --> 09:20:24,880
actually do a dot for each so now i can
15600
09:20:24,880 --> 09:20:27,436
do nums.4 each
15601
09:20:27,436 --> 09:20:30,640
and now it will actually work okay so
15602
09:20:30,640 --> 09:20:33,596
now i can actually try
15603
09:20:33,596 --> 09:20:34,480
run
15604
09:20:34,480 --> 09:20:36,400
and you can see that we just looped
15605
09:20:36,400 --> 09:20:39,276
through five times and
15606
09:20:39,276 --> 09:20:42,400
you know really floored it and gave you
15607
09:20:42,400 --> 09:20:44,400
know ran our porsche
15608
09:20:44,400 --> 09:20:46,400
now let's see if we can do
15609
09:20:46,400 --> 09:20:51,160
0 to 60 for our porsche so porsche.0260
15610
09:20:51,916 --> 09:20:53,840
and let's run this and let's see if that
15611
09:20:53,840 --> 09:20:55,596
runs
15612
09:20:55,596 --> 09:20:59,200
okay and it goes phew that was fast now
15613
09:20:59,200 --> 09:21:00,880
for our timeout we want to make sure
15614
09:21:00,880 --> 09:21:02,800
that we give it like a couple of seconds
15615
09:21:02,800 --> 09:21:05,520
right so we want to say 3000
15616
09:21:05,520 --> 09:21:07,840
so this will wait for
15617
09:21:07,840 --> 09:21:10,320
three seconds and then it'll say that
15618
09:21:10,320 --> 09:21:11,520
was fast
15619
09:21:11,520 --> 09:21:13,040
and then print out the current speed of
15620
09:21:13,040 --> 09:21:16,000
the car etc so let's try it now
15621
09:21:16,000 --> 09:21:17,200
and
15622
09:21:17,200 --> 09:21:19,436
boom that happened three seconds later
15623
09:21:19,436 --> 09:21:21,520
it goes phew that was fast
15624
09:21:21,520 --> 09:21:23,520
okay so we have zero to 60 on this as
15625
09:21:23,520 --> 09:21:24,560
well
15626
09:21:24,560 --> 09:21:28,160
and then can we do porsche dot stop
15627
09:21:28,160 --> 09:21:30,880
and then check porsche
15628
09:21:30,880 --> 09:21:32,480
dot current
15629
09:21:32,480 --> 09:21:34,560
speed
15630
09:21:34,560 --> 09:21:37,840
and we'll console
15631
09:21:37,840 --> 09:21:40,960
log this and run this now
15632
09:21:40,960 --> 09:21:45,040
and then boom we have speed is zero
15633
09:21:45,040 --> 09:21:46,880
okay there you go
15634
09:21:46,880 --> 09:21:49,436
oftentimes you'll see these get and set
15635
09:21:49,436 --> 09:21:51,360
methods a lot so i'll show you an
15636
09:21:51,360 --> 09:21:53,756
example of that and you can try to make
15637
09:21:53,756 --> 09:21:54,880
this
15638
09:21:54,880 --> 09:21:57,680
yourself as well so i'll do
15639
09:21:57,680 --> 09:21:58,800
get
15640
09:21:58,800 --> 09:22:01,520
current speed you can already kind of
15641
09:22:01,520 --> 09:22:04,880
see what this method is going to do okay
15642
09:22:04,880 --> 09:22:06,240
and
15643
09:22:06,240 --> 09:22:07,756
see if you could write this yourself
15644
09:22:07,756 --> 09:22:09,360
it's going to be very very very very
15645
09:22:09,360 --> 09:22:10,960
easy okay it's just going to do one line
15646
09:22:10,960 --> 09:22:12,320
here
15647
09:22:12,320 --> 09:22:14,320
um literally take like four to five
15648
09:22:14,320 --> 09:22:16,080
seconds try to write it on your own as
15649
09:22:16,080 --> 09:22:17,040
well
15650
09:22:17,040 --> 09:22:21,436
and five four three two one okay
15651
09:22:21,436 --> 09:22:23,200
so get current speed and you can just
15652
09:22:23,200 --> 09:22:26,000
say return current speed okay there
15653
09:22:26,000 --> 09:22:28,560
isn't a huge amazing use case for this
15654
09:22:28,560 --> 09:22:30,000
here but i'm just showing you this
15655
09:22:30,000 --> 09:22:32,400
because a lot of classes will have these
15656
09:22:32,400 --> 09:22:34,840
getters what they call
15657
09:22:34,840 --> 09:22:37,596
getters setters
15658
09:22:37,596 --> 09:22:39,840
so i i want you to get used to this so
15659
09:22:39,840 --> 09:22:41,596
they'll have ton of like get methods and
15660
09:22:41,596 --> 09:22:43,680
then they'll have ton of set methods
15661
09:22:43,680 --> 09:22:46,080
that like set the current speed
15662
09:22:46,080 --> 09:22:48,560
right so basically this drive method is
15663
09:22:48,560 --> 09:22:51,360
really like set the speed or like update
15664
09:22:51,360 --> 09:22:54,400
the speed back by a certain thing
15665
09:22:54,400 --> 09:22:55,360
um
15666
09:22:55,360 --> 09:22:57,520
we could even give it like uh we could
15667
09:22:57,520 --> 09:22:59,916
even give it a method like target speed
15668
09:22:59,916 --> 09:23:03,436
where you as a user can actually pass in
15669
09:23:03,436 --> 09:23:05,120
the speech actually let's do that
15670
09:23:05,120 --> 09:23:07,360
because you've seen how to call methods
15671
09:23:07,360 --> 09:23:08,800
but you haven't seen how to pass
15672
09:23:08,800 --> 09:23:11,916
arguments to methods so how about
15673
09:23:11,916 --> 09:23:14,800
you could actually call the method drive
15674
09:23:14,800 --> 09:23:17,276
and put in a
15675
09:23:17,276 --> 09:23:19,916
like a number here like a speed number
15676
09:23:19,916 --> 09:23:22,080
and instead of incremented incrementing
15677
09:23:22,080 --> 09:23:24,400
this by plus 10
15678
09:23:24,400 --> 09:23:26,960
it'll increment it by the speed that you
15679
09:23:26,960 --> 09:23:28,640
give it
15680
09:23:28,640 --> 09:23:32,480
okay so now when we call that drive now
15681
09:23:32,480 --> 09:23:34,880
drive needs something okay and uh i can
15682
09:23:34,880 --> 09:23:36,640
also show you default arguments in
15683
09:23:36,640 --> 09:23:38,960
javascript which are really nice so if
15684
09:23:38,960 --> 09:23:40,720
you don't pass in an argument it'll just
15685
09:23:40,720 --> 09:23:42,560
give you 10. for example if i try to run
15686
09:23:42,560 --> 09:23:45,520
now my code is going to kind of like
15687
09:23:45,520 --> 09:23:46,480
not work
15688
09:23:46,480 --> 09:23:48,560
you see how we have these nands
15689
09:23:48,560 --> 09:23:51,840
everywhere like nan
15690
09:23:51,840 --> 09:23:53,200
because
15691
09:23:53,200 --> 09:23:55,436
we're not passing in a speed
15692
09:23:55,436 --> 09:23:57,520
when we call our
15693
09:23:57,520 --> 09:23:59,840
drive method so if you see our drive
15694
09:23:59,840 --> 09:24:01,596
method here we're not passing in a speed
15695
09:24:01,596 --> 09:24:02,880
here right
15696
09:24:02,880 --> 09:24:05,436
so if we actually just give it a
15697
09:24:05,436 --> 09:24:08,560
equals 10 that's a default argument so
15698
09:24:08,560 --> 09:24:10,400
if i don't pass in a speed it'll set
15699
09:24:10,400 --> 09:24:12,800
that to just be 10. so let's give it a
15700
09:24:12,800 --> 09:24:14,640
try now and see if that works
15701
09:24:14,640 --> 09:24:16,720
yes it does right
15702
09:24:16,720 --> 09:24:19,200
but let's go ahead and give some speed
15703
09:24:19,200 --> 09:24:21,840
so for example for the porsche what i'll
15704
09:24:21,840 --> 09:24:23,840
do is
15705
09:24:23,840 --> 09:24:26,080
i will comment out this line where i
15706
09:24:26,080 --> 09:24:27,360
gave it a
15707
09:24:27,360 --> 09:24:28,480
like a
15708
09:24:28,480 --> 09:24:30,560
drove it five times here
15709
09:24:30,560 --> 09:24:33,120
what i'll do instead
15710
09:24:33,120 --> 09:24:35,756
and i'll comment out this nums line too
15711
09:24:35,756 --> 09:24:40,080
what i'll do instead is i'll say porsche
15712
09:24:40,080 --> 09:24:40,840
dot
15713
09:24:40,840 --> 09:24:45,436
drive and let's say i say 40
15714
09:24:45,436 --> 09:24:46,320
okay
15715
09:24:46,320 --> 09:24:48,320
and then we console log and we get the
15716
09:24:48,320 --> 09:24:50,400
current speed so let's go ahead and
15717
09:24:50,400 --> 09:24:52,560
console log here and run this
15718
09:24:52,560 --> 09:24:54,800
and you can see that it instantly just
15719
09:24:54,800 --> 09:24:57,680
went it shot up to 40.
15720
09:24:57,680 --> 09:25:01,756
right it instantly shot up to 40 here
15721
09:25:02,080 --> 09:25:02,960
let's
15722
09:25:02,960 --> 09:25:03,756
do
15723
09:25:03,756 --> 09:25:05,360
80.
15724
09:25:05,360 --> 09:25:07,276
okay let's do 40 here
15725
09:25:07,276 --> 09:25:09,120
and let's do
15726
09:25:09,120 --> 09:25:11,680
maybe another 80.
15727
09:25:11,680 --> 09:25:13,520
now what's gonna happen so i did 40 here
15728
09:25:13,520 --> 09:25:15,360
for drive and i did 80 here for drive
15729
09:25:15,360 --> 09:25:17,120
what do you think line 69 is going to
15730
09:25:17,120 --> 09:25:18,240
get us
15731
09:25:18,240 --> 09:25:23,276
what's the speed going to be 5 4 3 2 1.
15732
09:25:23,276 --> 09:25:25,756
speed's going to be 40 plus 80 right
15733
09:25:25,756 --> 09:25:27,436
it's all incrementing on top of each
15734
09:25:27,436 --> 09:25:29,160
other so it's going to be
15735
09:25:29,160 --> 09:25:31,276
120 let's run it
15736
09:25:31,276 --> 09:25:32,480
and boom
15737
09:25:32,480 --> 09:25:35,800
we got 120
15738
09:25:36,080 --> 09:25:37,520
okay so
15739
09:25:37,520 --> 09:25:40,640
there you go this is how you make
15740
09:25:40,640 --> 09:25:43,756
basics of classes and this is an example
15741
09:25:43,756 --> 09:25:46,720
with cars if you have cars how can you
15742
09:25:46,720 --> 09:25:48,720
make different classes and give cars
15743
09:25:48,720 --> 09:25:50,240
different methods and hopefully this was
15744
09:25:50,240 --> 09:25:52,880
like a really visual example so it kind
15745
09:25:52,880 --> 09:25:54,000
of
15746
09:25:54,000 --> 09:25:56,560
got certain concepts through for you so
15747
09:25:56,560 --> 09:25:58,880
that's what's happening with classes
15748
09:25:58,880 --> 09:26:01,520
okay so to give you some other real
15749
09:26:01,520 --> 09:26:03,596
world example of this
15750
09:26:03,596 --> 09:26:05,680
and you know i want i do want to talk
15751
09:26:05,680 --> 09:26:08,160
about nums and stuff like that and i'll
15752
09:26:08,160 --> 09:26:10,720
kind of foreshadow it a little bit
15753
09:26:10,720 --> 09:26:14,080
but here's a real world example
15754
09:26:14,080 --> 09:26:17,276
so let's say we have const numbers
15755
09:26:17,276 --> 09:26:20,480
and we have this array right here
15756
09:26:20,480 --> 09:26:23,916
and we do numbers dot push and i say 4
15757
09:26:23,916 --> 09:26:26,640
and i do console log numbers
15758
09:26:26,640 --> 09:26:28,720
what is numbers going to say
15759
09:26:28,720 --> 09:26:30,000
is numbers going to give me back an
15760
09:26:30,000 --> 09:26:31,756
array 1 2 3
15761
09:26:31,756 --> 09:26:33,436
or is it going to give me back an array
15762
09:26:33,436 --> 09:26:35,520
of one two three four which one is it
15763
09:26:35,520 --> 09:26:36,880
gonna be
15764
09:26:36,880 --> 09:26:38,160
if you guess that it's gonna give me
15765
09:26:38,160 --> 09:26:39,916
back an array with one two three four
15766
09:26:39,916 --> 09:26:42,480
good job that's exactly right
15767
09:26:42,480 --> 09:26:44,000
why is that happening
15768
09:26:44,000 --> 09:26:45,840
because we're doing this dot push so
15769
09:26:45,840 --> 09:26:48,560
what is dot push is dot push let me ask
15770
09:26:48,560 --> 09:26:51,276
you is dot push a property
15771
09:26:51,276 --> 09:26:52,720
is dot push
15772
09:26:52,720 --> 09:26:57,756
a function or is dot push a method
15773
09:26:57,756 --> 09:27:00,400
uh or is it something else
15774
09:27:00,400 --> 09:27:03,840
what's not push
15775
09:27:06,960 --> 09:27:10,800
if you guess that dot push is a method
15776
09:27:10,800 --> 09:27:13,436
or a property that's not right because
15777
09:27:13,436 --> 09:27:15,596
property is if i didn't have any
15778
09:27:15,596 --> 09:27:17,596
function call here okay so that would be
15779
09:27:17,596 --> 09:27:20,240
a property it's not a property
15780
09:27:20,240 --> 09:27:22,640
if you said function that would not be
15781
09:27:22,640 --> 09:27:24,320
right because
15782
09:27:24,320 --> 09:27:26,720
a function is if you had to call push
15783
09:27:26,720 --> 09:27:28,840
like this because you have to do dot
15784
09:27:28,840 --> 09:27:31,916
push it's not a function
15785
09:27:31,916 --> 09:27:34,240
if you said method
15786
09:27:34,240 --> 09:27:36,960
you are completely correct good job okay
15787
09:27:36,960 --> 09:27:40,640
so this is an example of a method
15788
09:27:40,640 --> 09:27:43,040
method so if it's a method well then
15789
09:27:43,040 --> 09:27:45,040
that means whenever methods are involved
15790
09:27:45,040 --> 09:27:47,436
there must be classes
15791
09:27:47,436 --> 09:27:50,240
because you can only have methods inside
15792
09:27:50,240 --> 09:27:52,320
of classes i'll write it as a note you
15793
09:27:52,320 --> 09:27:54,480
can only have methods
15794
09:27:54,480 --> 09:27:56,320
inside of classes
15795
09:27:56,320 --> 09:27:59,200
so you must have a class here well that
15796
09:27:59,200 --> 09:28:01,596
basically means that numbers which is of
15797
09:28:01,596 --> 09:28:03,276
type array
15798
09:28:03,276 --> 09:28:06,240
that that means that that's a class
15799
09:28:06,240 --> 09:28:08,960
okay it's its own class and that class
15800
09:28:08,960 --> 09:28:11,276
has methods like dot push
15801
09:28:11,276 --> 09:28:12,960
and there has there's something i
15802
09:28:12,960 --> 09:28:14,480
haven't shown you yet but it's called
15803
09:28:14,480 --> 09:28:17,840
type of so if i actually have an array
15804
09:28:17,840 --> 09:28:19,756
so let me clear this console and let me
15805
09:28:19,756 --> 09:28:21,840
zoom in here so you can see better so if
15806
09:28:21,840 --> 09:28:24,800
i do type of and i say
15807
09:28:24,800 --> 09:28:27,360
one two three array
15808
09:28:27,360 --> 09:28:28,160
okay
15809
09:28:28,160 --> 09:28:30,000
here it's giving me okay what's the type
15810
09:28:30,000 --> 09:28:33,200
of this and it says object right if i do
15811
09:28:33,200 --> 09:28:35,276
type of
15812
09:28:35,276 --> 09:28:36,800
and let's say i do
15813
09:28:36,800 --> 09:28:39,276
two it says type of number if i say
15814
09:28:39,276 --> 09:28:40,800
hello
15815
09:28:40,800 --> 09:28:42,640
right it tells me string
15816
09:28:42,640 --> 09:28:45,520
so this is a type of either object but
15817
09:28:45,520 --> 09:28:48,800
specifically this is of array class okay
15818
09:28:48,800 --> 09:28:50,960
so sometimes javascript is weird it ends
15819
09:28:50,960 --> 09:28:52,640
up calling a lot of things objects but
15820
09:28:52,640 --> 09:28:55,120
this is really like an array class
15821
09:28:55,120 --> 09:28:57,680
and classes are objects sometimes that's
15822
09:28:57,680 --> 09:28:58,720
okay
15823
09:28:58,720 --> 09:29:00,160
then you have number then you have
15824
09:29:00,160 --> 09:29:02,160
string so that's why like when you have
15825
09:29:02,160 --> 09:29:04,880
a string it has its own methods like dot
15826
09:29:04,880 --> 09:29:06,000
split
15827
09:29:06,000 --> 09:29:08,960
right where it turns that string into an
15828
09:29:08,960 --> 09:29:10,240
array
15829
09:29:10,240 --> 09:29:13,276
um or you could have an array with one
15830
09:29:13,276 --> 09:29:15,040
two three
15831
09:29:15,040 --> 09:29:17,436
and you could say dot join
15832
09:29:17,436 --> 09:29:20,080
join this with strawberries
15833
09:29:20,080 --> 09:29:22,160
right and then when i do this it joins
15834
09:29:22,160 --> 09:29:25,040
one two three with strawberries
15835
09:29:25,040 --> 09:29:25,916
so
15836
09:29:25,916 --> 09:29:27,916
there are classes which have their own
15837
09:29:27,916 --> 09:29:30,240
method so if you were to visualize this
15838
09:29:30,240 --> 09:29:32,960
so for example when i have arrays right
15839
09:29:32,960 --> 09:29:34,640
here
15840
09:29:34,640 --> 09:29:37,596
like arrays right what is array so
15841
09:29:37,596 --> 09:29:41,756
imagine it like this array is of class
15842
09:29:41,756 --> 09:29:44,720
uh or it's a class array and then you
15843
09:29:44,720 --> 09:29:46,480
might have different methods like you
15844
09:29:46,480 --> 09:29:48,320
might have a join method
15845
09:29:48,320 --> 09:29:51,200
and you might have the
15846
09:29:51,200 --> 09:29:53,436
push method
15847
09:29:53,436 --> 09:29:54,240
right
15848
09:29:54,240 --> 09:29:56,240
and then we can actually even try
15849
09:29:56,240 --> 09:29:59,360
implementing our own push okay and we we
15850
09:29:59,360 --> 09:30:01,596
could create our own push and see if
15851
09:30:01,596 --> 09:30:05,040
that works exactly like push does and so
15852
09:30:05,040 --> 09:30:06,720
let's give that a try but first let's
15853
09:30:06,720 --> 09:30:08,880
just see how
15854
09:30:08,880 --> 09:30:10,960
certain array methods work okay so for
15855
09:30:10,960 --> 09:30:12,800
example i'm gonna do
15856
09:30:12,800 --> 09:30:14,800
so i have one two three
15857
09:30:14,800 --> 09:30:18,000
this is an array right if i do dot push
15858
09:30:18,000 --> 09:30:20,400
and i say four you can see that i've
15859
09:30:20,400 --> 09:30:22,160
added a four to that array right so
15860
09:30:22,160 --> 09:30:24,800
let's just go to one two three again
15861
09:30:24,800 --> 09:30:26,640
perfect okay so we have this one two
15862
09:30:26,640 --> 09:30:28,480
three array now
15863
09:30:28,480 --> 09:30:30,000
one thing you could do
15864
09:30:30,000 --> 09:30:33,436
to an array is i could actually get the
15865
09:30:33,436 --> 09:30:35,276
zeroth property of an array and i can
15866
09:30:35,276 --> 09:30:37,596
change it
15867
09:30:37,596 --> 09:30:39,276
sorry about that
15868
09:30:39,276 --> 09:30:41,276
um i have a chess tournament coming up
15869
09:30:41,276 --> 09:30:43,520
so my friend is calling me for that it's
15870
09:30:43,520 --> 09:30:46,800
tomorrow in las vegas
15871
09:30:46,800 --> 09:30:49,520
and it's like a hundred and four degrees
15872
09:30:49,520 --> 09:30:51,840
severe heat warnings there but i'm
15873
09:30:51,840 --> 09:30:54,320
excited and i'm gonna go play it there
15874
09:30:54,320 --> 09:30:56,160
it's the national open chess tournament
15875
09:30:56,160 --> 09:30:57,596
and they have a hundred thousand dollar
15876
09:30:57,596 --> 09:30:59,680
prize guaranteed
15877
09:30:59,680 --> 09:31:01,040
i'm not looking to win i'm just looking
15878
09:31:01,040 --> 09:31:02,640
to have a good time it's going to be fun
15879
09:31:02,640 --> 09:31:04,960
playing on the u 2100 if any of you guys
15880
09:31:04,960 --> 09:31:06,800
are chess nerds
15881
09:31:06,800 --> 09:31:08,400
let's get back to this
15882
09:31:08,400 --> 09:31:11,040
so let's say i have this or so let's say
15883
09:31:11,040 --> 09:31:13,360
i crit let me create an array
15884
09:31:13,360 --> 09:31:16,880
so x equals we'll say 1 2 3. okay so i
15885
09:31:16,880 --> 09:31:19,436
have this array right here 1 2 3.
15886
09:31:19,436 --> 09:31:21,680
now i can actually
15887
09:31:21,680 --> 09:31:24,960
change some of the properties
15888
09:31:24,960 --> 09:31:27,360
in here so i could actually do
15889
09:31:27,360 --> 09:31:28,880
x of 0
15890
09:31:28,880 --> 09:31:32,080
and i can change that to
15891
09:31:32,080 --> 09:31:33,840
i don't know 10.
15892
09:31:33,840 --> 09:31:36,320
and now when you look at x notice that
15893
09:31:36,320 --> 09:31:39,596
i've changed at that 0th position and
15894
09:31:39,596 --> 09:31:42,320
now you have a 10. okay well what if i
15895
09:31:42,320 --> 09:31:46,160
change at position 2 and i do x of 2
15896
09:31:46,160 --> 09:31:48,240
is now
15897
09:31:48,240 --> 09:31:51,200
whatever x of 2 was plus 7. what do you
15898
09:31:51,200 --> 09:31:53,120
think is going to happen
15899
09:31:53,120 --> 09:31:54,240
what do you think is going to happen
15900
09:31:54,240 --> 09:31:55,680
here
15901
09:31:55,680 --> 09:31:59,916
so x of 2 will get you access to the
15902
09:31:59,916 --> 09:32:02,240
the third index right so this or the
15903
09:32:02,240 --> 09:32:04,640
second index zero index right this is a
15904
09:32:04,640 --> 09:32:07,916
zero this is a one and this is a two
15905
09:32:07,916 --> 09:32:10,000
and i'm setting x of 2 to whatever x of
15906
09:32:10,000 --> 09:32:11,840
2 was plus 7. what do you think is going
15907
09:32:11,840 --> 09:32:14,000
to happen there well this 3 is going to
15908
09:32:14,000 --> 09:32:16,240
actually turn into a plus 7. so let's
15909
09:32:16,240 --> 09:32:17,436
try it
15910
09:32:17,436 --> 09:32:20,000
right and let's run x and you can see
15911
09:32:20,000 --> 09:32:22,400
that now i have 10 here and 10 there so
15912
09:32:22,400 --> 09:32:25,120
you could mutate an array right
15913
09:32:25,120 --> 09:32:26,240
now
15914
09:32:26,240 --> 09:32:29,276
by doing this what if we access an
15915
09:32:29,276 --> 09:32:32,000
a location that we haven't we don't even
15916
09:32:32,000 --> 09:32:34,240
see there so let's give that a try so
15917
09:32:34,240 --> 09:32:36,240
let's see if i can do
15918
09:32:36,240 --> 09:32:38,400
x of 3
15919
09:32:38,400 --> 09:32:40,480
right which doesn't exist
15920
09:32:40,480 --> 09:32:42,560
and say i say
15921
09:32:42,560 --> 09:32:44,400
uh apple
15922
09:32:44,400 --> 09:32:46,800
and now we do x and you can actually see
15923
09:32:46,800 --> 09:32:50,160
i have 10 to 10 apple
15924
09:32:50,160 --> 09:32:53,520
so i could actually add things to an
15925
09:32:53,520 --> 09:32:54,560
array
15926
09:32:54,560 --> 09:32:57,040
by getting like the last index of the
15927
09:32:57,040 --> 09:32:58,960
array and adding things there
15928
09:32:58,960 --> 09:33:00,000
whoa
15929
09:33:00,000 --> 09:33:02,320
that's pretty bizarre huh
15930
09:33:02,320 --> 09:33:05,756
so using this knowledge we can actually
15931
09:33:05,756 --> 09:33:08,240
implement our own
15932
09:33:08,240 --> 09:33:09,680
push method
15933
09:33:09,680 --> 09:33:11,756
okay and i'll show you how you can
15934
09:33:11,756 --> 09:33:14,080
actually add methods to
15935
09:33:14,080 --> 09:33:16,720
the array data type so javascript allows
15936
09:33:16,720 --> 09:33:19,520
you to do array.prototype and this
15937
09:33:19,520 --> 09:33:21,596
allows you to just get access to the
15938
09:33:21,596 --> 09:33:23,916
array like class
15939
09:33:23,916 --> 09:33:26,720
and we can actually now create our own
15940
09:33:26,720 --> 09:33:28,720
method here and say
15941
09:33:28,720 --> 09:33:31,436
my push okay so this is going to be our
15942
09:33:31,436 --> 09:33:34,880
push method that we're creating
15943
09:33:34,880 --> 09:33:36,240
and we're going to say it's going to be
15944
09:33:36,240 --> 09:33:37,756
a function
15945
09:33:37,756 --> 09:33:40,000
okay and what this function is going to
15946
09:33:40,000 --> 09:33:40,960
do
15947
09:33:40,960 --> 09:33:42,640
is let's for now let's just say it's
15948
09:33:42,640 --> 09:33:45,756
going to get this dot length okay this
15949
09:33:45,756 --> 09:33:47,596
this will refer to the array you're
15950
09:33:47,596 --> 09:33:50,240
calling the dot whatever on so let's
15951
09:33:50,240 --> 09:33:52,720
create const fruits
15952
09:33:52,720 --> 09:33:55,200
and i'm going to say
15953
09:33:55,200 --> 09:33:57,756
banana
15954
09:33:58,800 --> 09:34:00,840
let's say
15955
09:34:00,840 --> 09:34:04,800
blackberry that's not blackberry
15956
09:34:04,800 --> 09:34:07,276
uh berry is fine oh we don't have to
15957
09:34:07,276 --> 09:34:09,276
apparently what happened to it
15958
09:34:09,276 --> 09:34:10,800
strawberry
15959
09:34:10,800 --> 09:34:13,200
and then we'll have a cookie okay so
15960
09:34:13,200 --> 09:34:15,276
apparently cookie's a fruit but
15961
09:34:15,276 --> 09:34:17,916
hey let's just go with it
15962
09:34:17,916 --> 09:34:19,320
and now let's do
15963
09:34:19,320 --> 09:34:21,276
console.log and i'm going to say
15964
09:34:21,276 --> 09:34:24,080
fruits.mypush
15965
09:34:24,080 --> 09:34:26,960
and call it as a method okay fruit's not
15966
09:34:26,960 --> 09:34:28,800
my push let's call it the method let's
15967
09:34:28,800 --> 09:34:33,200
hit run and let's see what happens here
15968
09:34:33,200 --> 09:34:36,000
okay we're getting an error somewhere
15969
09:34:36,000 --> 09:34:37,360
let's see where we're getting that error
15970
09:34:37,360 --> 09:34:40,720
it says line 97.
15971
09:34:40,720 --> 09:34:43,360
oh because i'm not like calling typeof
15972
09:34:43,360 --> 09:34:44,880
on anything so let's just comment that
15973
09:34:44,880 --> 09:34:45,840
out
15974
09:34:45,840 --> 09:34:47,680
let's run this now
15975
09:34:47,680 --> 09:34:49,840
and you can see i have one two three
15976
09:34:49,840 --> 09:34:50,880
four
15977
09:34:50,880 --> 09:34:52,000
okay
15978
09:34:52,000 --> 09:34:54,160
and right now it says undefined because
15979
09:34:54,160 --> 09:34:55,756
i'm not having it return anything so
15980
09:34:55,756 --> 09:34:58,160
let's say return dot length
15981
09:34:58,160 --> 09:35:00,880
so it should return length of three and
15982
09:35:00,880 --> 09:35:02,800
we're we're getting that three we were
15983
09:35:02,800 --> 09:35:03,916
looking for
15984
09:35:03,916 --> 09:35:04,800
okay
15985
09:35:04,800 --> 09:35:07,596
uh let's get our cars to
15986
09:35:07,596 --> 09:35:09,360
get our cars to kind of calm down a bit
15987
09:35:09,360 --> 09:35:13,120
so i'll just comment all of that out
15988
09:35:13,120 --> 09:35:14,000
all right
15989
09:35:14,000 --> 09:35:16,240
so and then let's comment out our array
15990
09:35:16,240 --> 09:35:17,756
stuff here as well
15991
09:35:17,756 --> 09:35:19,840
so we should just have fruits now if i
15992
09:35:19,840 --> 09:35:22,080
add an additional fruit in here
15993
09:35:22,080 --> 09:35:24,400
let's say i add a pear
15994
09:35:24,400 --> 09:35:27,040
or an apple what do you think the length
15995
09:35:27,040 --> 09:35:28,000
is going to say do you think it's going
15996
09:35:28,000 --> 09:35:29,596
to say 3 do you think it's going to say
15997
09:35:29,596 --> 09:35:30,880
5 you think it's going to say 10 what do
15998
09:35:30,880 --> 09:35:32,160
you think is going to say
15999
09:35:32,160 --> 09:35:34,560
let's try running it boom 5 because i
16000
09:35:34,560 --> 09:35:37,200
have 5 items in here
16001
09:35:37,200 --> 09:35:40,400
so in order to do the thing right if i
16002
09:35:40,400 --> 09:35:43,436
have an array with five items then all i
16003
09:35:43,436 --> 09:35:44,880
have to do
16004
09:35:44,880 --> 09:35:48,720
is say something along the lines of
16005
09:35:48,720 --> 09:35:51,520
my array
16006
09:35:51,520 --> 09:35:52,560
five
16007
09:35:52,560 --> 09:35:54,560
and in the fifth position
16008
09:35:54,560 --> 09:35:56,160
i can put in
16009
09:35:56,160 --> 09:36:01,360
whatever item i pass so my item
16010
09:36:01,360 --> 09:36:02,240
okay
16011
09:36:02,240 --> 09:36:04,080
so in this function
16012
09:36:04,080 --> 09:36:06,800
we'll pass in item
16013
09:36:06,800 --> 09:36:07,756
okay
16014
09:36:07,756 --> 09:36:09,520
and then we can actually just pull the
16015
09:36:09,520 --> 09:36:11,040
item from here
16016
09:36:11,040 --> 09:36:13,756
and let's actually just see first if we
16017
09:36:13,756 --> 09:36:16,000
can actually get access to
16018
09:36:16,000 --> 09:36:17,680
our item through that right so let's
16019
09:36:17,680 --> 09:36:19,596
just say return item
16020
09:36:19,596 --> 09:36:23,680
and i'll pass in cookie
16021
09:36:23,680 --> 09:36:26,160
the word cookie and let's run this
16022
09:36:26,160 --> 09:36:27,436
and you can see
16023
09:36:27,436 --> 09:36:30,880
that my push function like meth run my
16024
09:36:30,880 --> 09:36:32,800
my push method runs
16025
09:36:32,800 --> 09:36:34,320
it calls this
16026
09:36:34,320 --> 09:36:36,320
and i can actually call it on type array
16027
09:36:36,320 --> 09:36:37,840
right i'm creating my own methods for
16028
09:36:37,840 --> 09:36:38,880
the array
16029
09:36:38,880 --> 09:36:40,880
and then it's actually returning the
16030
09:36:40,880 --> 09:36:43,200
cookie right here
16031
09:36:43,200 --> 09:36:45,916
right so in order to do this now i have
16032
09:36:45,916 --> 09:36:50,720
all the pieces of the puzzle i can do
16033
09:36:50,720 --> 09:36:53,120
grab this array okay so this will get
16034
09:36:53,120 --> 09:36:55,360
you the actual array that you're passing
16035
09:36:55,360 --> 09:36:57,520
it so let's run this just so you can see
16036
09:36:57,520 --> 09:36:59,596
and you can see this refers to the
16037
09:36:59,596 --> 09:37:02,160
actual array that was passed in okay so
16038
09:37:02,160 --> 09:37:06,000
hopefully that demystifies the word this
16039
09:37:06,000 --> 09:37:08,080
a lot for you okay because that's what
16040
09:37:08,080 --> 09:37:10,080
this is referring to
16041
09:37:10,080 --> 09:37:12,400
and now we're gonna say
16042
09:37:12,400 --> 09:37:14,160
it's whatever you're calling this dot
16043
09:37:14,160 --> 09:37:17,040
thing on is usually like this okay so
16044
09:37:17,040 --> 09:37:18,800
it's referring to like fruits in this
16045
09:37:18,800 --> 09:37:21,436
case now if i do another dot
16046
09:37:21,436 --> 09:37:23,596
that dot will be referring to whatever
16047
09:37:23,596 --> 09:37:24,880
this
16048
09:37:24,880 --> 09:37:26,880
would be
16049
09:37:26,880 --> 09:37:27,840
okay
16050
09:37:27,840 --> 09:37:30,080
so now i'm gonna say
16051
09:37:30,080 --> 09:37:32,720
this it's i know it's an array
16052
09:37:32,720 --> 09:37:34,320
and
16053
09:37:34,320 --> 09:37:36,720
i know what i want to do
16054
09:37:36,720 --> 09:37:38,400
is
16055
09:37:38,400 --> 09:37:41,520
i want to return the array
16056
09:37:41,520 --> 09:37:42,880
okay
16057
09:37:42,880 --> 09:37:43,916
and
16058
09:37:43,916 --> 09:37:46,720
on its fifth element around his fifth
16059
09:37:46,720 --> 09:37:48,720
position zero because
16060
09:37:48,720 --> 09:37:50,240
let's just go through this this is zero
16061
09:37:50,240 --> 09:37:52,480
this is one this is two this is three
16062
09:37:52,480 --> 09:37:54,880
this is four so in its fifth position
16063
09:37:54,880 --> 09:37:58,840
right over here i wanna add in a
16064
09:37:58,840 --> 09:38:00,400
um
16065
09:38:00,400 --> 09:38:01,200
a
16066
09:38:01,200 --> 09:38:02,880
what other fruit is left that i haven't
16067
09:38:02,880 --> 09:38:04,720
covered yet
16068
09:38:04,720 --> 09:38:07,840
a kiwi do we have a kiwi in here
16069
09:38:07,840 --> 09:38:09,840
apparently we do nice
16070
09:38:09,840 --> 09:38:10,640
so
16071
09:38:10,640 --> 09:38:13,916
on that position i want to set that to a
16072
09:38:13,916 --> 09:38:14,960
kiwi
16073
09:38:14,960 --> 09:38:16,320
okay
16074
09:38:16,320 --> 09:38:18,560
and then we return this so let's go
16075
09:38:18,560 --> 09:38:20,240
ahead and try that
16076
09:38:20,240 --> 09:38:24,320
and you can see boom we actually now
16077
09:38:24,320 --> 09:38:26,320
have a kiwi
16078
09:38:26,320 --> 09:38:29,040
right over here okay
16079
09:38:29,040 --> 09:38:29,840
so
16080
09:38:29,840 --> 09:38:32,400
that's super cool but this five is hard
16081
09:38:32,400 --> 09:38:34,080
coded how could we get this five in a
16082
09:38:34,080 --> 09:38:35,596
dynamic way
16083
09:38:35,596 --> 09:38:38,080
well if we have access to this can we do
16084
09:38:38,080 --> 09:38:41,120
this dot length and get access
16085
09:38:41,120 --> 09:38:42,880
and then we're just going to set that to
16086
09:38:42,880 --> 09:38:46,240
a kiwi but in instead of a kiwi
16087
09:38:46,240 --> 09:38:48,720
we can just set it to an item whatever
16088
09:38:48,720 --> 09:38:51,276
item we pass it so now we can pass the
16089
09:38:51,276 --> 09:38:52,960
kiwi from here
16090
09:38:52,960 --> 09:38:55,120
and let's run this
16091
09:38:55,120 --> 09:38:57,436
and you can see that we pass in the kiwi
16092
09:38:57,436 --> 09:38:58,720
went here
16093
09:38:58,720 --> 09:39:00,720
and it plugged it in
16094
09:39:00,720 --> 09:39:01,680
boom
16095
09:39:01,680 --> 09:39:04,640
and so now we have our own push method
16096
09:39:04,640 --> 09:39:06,240
right we can actually just use our own
16097
09:39:06,240 --> 09:39:07,756
push to push whatever we want so let's
16098
09:39:07,756 --> 09:39:10,240
try pushing a couple of things and let's
16099
09:39:10,240 --> 09:39:15,680
then print out our our array okay so
16100
09:39:15,840 --> 09:39:17,916
ignore that
16101
09:39:17,916 --> 09:39:20,320
so let's go ahead and and push a bunch
16102
09:39:20,320 --> 09:39:23,120
of stuff so i'm going to push
16103
09:39:23,120 --> 09:39:24,720
more cookies
16104
09:39:24,720 --> 09:39:26,160
i'm gonna push
16105
09:39:26,160 --> 09:39:28,000
more strawberries
16106
09:39:28,000 --> 09:39:30,240
okay i'm gonna push
16107
09:39:30,240 --> 09:39:31,840
another
16108
09:39:31,840 --> 09:39:33,680
i'm gonna push an orange
16109
09:39:33,680 --> 09:39:34,560
okay
16110
09:39:34,560 --> 09:39:37,840
and then we're gonna console
16111
09:39:37,840 --> 09:39:40,720
fruits and let's see what happens
16112
09:39:40,720 --> 09:39:42,800
and there you go all of the things that
16113
09:39:42,800 --> 09:39:44,880
we actually pushed
16114
09:39:44,880 --> 09:39:47,040
right got added
16115
09:39:47,040 --> 09:39:48,800
all of these guys that we pushed here
16116
09:39:48,800 --> 09:39:50,400
four new fruits
16117
09:39:50,400 --> 09:39:53,916
you can see that we now have those four
16118
09:39:53,916 --> 09:39:55,276
new
16119
09:39:55,276 --> 09:39:57,520
fruits
16120
09:39:57,520 --> 09:40:00,080
right added in
16121
09:40:00,080 --> 09:40:02,080
right there
16122
09:40:02,080 --> 09:40:03,040
okay
16123
09:40:03,040 --> 09:40:06,080
so you can extend
16124
09:40:06,080 --> 09:40:08,400
classes you can extend
16125
09:40:08,400 --> 09:40:10,400
uh array data types like this you could
16126
09:40:10,400 --> 09:40:12,480
extend other data types like this so you
16127
09:40:12,480 --> 09:40:13,596
could extend
16128
09:40:13,596 --> 09:40:16,080
so many things and then you know now
16129
09:40:16,080 --> 09:40:18,800
hopefully other things to you
16130
09:40:18,800 --> 09:40:21,596
might make sense too for example
16131
09:40:21,596 --> 09:40:24,000
if you have an object right so let's say
16132
09:40:24,000 --> 09:40:26,960
i create an object called things
16133
09:40:26,960 --> 09:40:28,800
equal or let's say i create an object
16134
09:40:28,800 --> 09:40:30,080
called actor
16135
09:40:30,080 --> 09:40:31,120
and
16136
09:40:31,120 --> 09:40:33,120
name
16137
09:40:33,120 --> 09:40:35,756
is uh
16138
09:40:35,756 --> 09:40:38,160
jamie
16139
09:40:38,240 --> 09:40:39,680
foxx
16140
09:40:39,680 --> 09:40:45,596
and age is i don't know how old he is 37
16141
09:40:45,680 --> 09:40:46,840
right
16142
09:40:46,840 --> 09:40:49,596
and um
16143
09:40:49,596 --> 09:40:51,276
money is
16144
09:40:51,276 --> 09:40:52,960
i don't know whatever something random
16145
09:40:52,960 --> 09:40:55,436
right so now i have an actor how do i
16146
09:40:55,436 --> 09:40:57,596
get the keys of this actor i got to use
16147
09:40:57,596 --> 09:41:00,240
a method on the actor
16148
09:41:00,240 --> 09:41:03,680
you know object so object is also like
16149
09:41:03,680 --> 09:41:06,000
its own class and it has its own method
16150
09:41:06,000 --> 09:41:07,840
so if you were to imagine it you should
16151
09:41:07,840 --> 09:41:09,916
imagine it like this i have a class
16152
09:41:09,916 --> 09:41:12,640
object and it has different methods for
16153
09:41:12,640 --> 09:41:14,960
example it has a method called
16154
09:41:14,960 --> 09:41:18,320
keys all right so now watch what happens
16155
09:41:18,320 --> 09:41:19,840
when i do
16156
09:41:19,840 --> 09:41:20,960
object
16157
09:41:20,960 --> 09:41:23,520
dot keys and i pass it actor
16158
09:41:23,520 --> 09:41:24,400
okay
16159
09:41:24,400 --> 09:41:26,880
so i can actually pass it actor and boom
16160
09:41:26,880 --> 09:41:29,596
i get name age money
16161
09:41:29,596 --> 09:41:32,320
all right well actually object keys is a
16162
09:41:32,320 --> 09:41:34,640
little bit different it's it is on the
16163
09:41:34,640 --> 09:41:36,720
object class so you could do object.keys
16164
09:41:36,720 --> 09:41:39,276
and pass past the actors so that's
16165
09:41:39,276 --> 09:41:41,596
fair enough that's still like that's
16166
09:41:41,596 --> 09:41:46,240
still a method dot keys okay so
16167
09:41:46,240 --> 09:41:50,240
there are a lot of things that are um
16168
09:41:50,240 --> 09:41:51,756
all right and you can also do this you
16169
09:41:51,756 --> 09:41:54,640
can also do dot values
16170
09:41:54,640 --> 09:41:58,160
and get all the values back
16171
09:41:58,160 --> 09:42:00,480
right so you could do dot keys get the
16172
09:42:00,480 --> 09:42:02,400
get the keys you could do dot values
16173
09:42:02,400 --> 09:42:04,240
it'll give you all the values of that
16174
09:42:04,240 --> 09:42:05,360
object
16175
09:42:05,360 --> 09:42:07,040
so
16176
09:42:07,040 --> 09:42:09,436
there are so many use cases of this
16177
09:42:09,436 --> 09:42:11,360
knowledge and this information and if
16178
09:42:11,360 --> 09:42:13,916
you deep dive into it now it'll save you
16179
09:42:13,916 --> 09:42:16,000
so much pain
16180
09:42:16,000 --> 09:42:18,560
in the future because it gets so much
16181
09:42:18,560 --> 09:42:19,596
harder
16182
09:42:19,596 --> 09:42:21,120
if you have like
16183
09:42:21,120 --> 09:42:23,840
shaky foundations and shaky basics and
16184
09:42:23,840 --> 09:42:26,160
then you try to go and build you know
16185
09:42:26,160 --> 09:42:28,560
learn more complex stuff on top of it
16186
09:42:28,560 --> 09:42:30,320
you're just gonna have so much trouble
16187
09:42:30,320 --> 09:42:32,800
but if you learn this now and you can
16188
09:42:32,800 --> 09:42:34,800
extend these classes and add your
16189
09:42:34,800 --> 09:42:36,400
methods and do a whole bunch of stuff
16190
09:42:36,400 --> 09:42:38,000
like this
16191
09:42:38,000 --> 09:42:39,360
you're gonna be
16192
09:42:39,360 --> 09:42:41,120
just such a badass developer in the
16193
09:42:41,120 --> 09:42:42,320
future and you're gonna be so much
16194
09:42:42,320 --> 09:42:43,916
stronger than most people and where
16195
09:42:43,916 --> 09:42:45,916
other people get stuck you're gonna be
16196
09:42:45,916 --> 09:42:47,276
flying past
16197
09:42:47,276 --> 09:42:49,360
so i hope this made sense
16198
09:42:49,360 --> 09:42:52,080
i do i would like to do some more kind
16199
09:42:52,080 --> 09:42:55,680
of exercises with classes because
16200
09:42:55,680 --> 09:42:57,276
i think you should get very used to
16201
09:42:57,276 --> 09:42:59,680
seeing them like everything under the
16202
09:42:59,680 --> 09:43:01,840
hood is going to be some type of class
16203
09:43:01,840 --> 09:43:03,840
some type of object everything under the
16204
09:43:03,840 --> 09:43:05,120
hood is going to be
16205
09:43:05,120 --> 09:43:07,200
methods and all that type of stuff going
16206
09:43:07,200 --> 09:43:09,756
on so if you start getting used to
16207
09:43:09,756 --> 09:43:11,756
seeing all of this now i think it's
16208
09:43:11,756 --> 09:43:13,360
going to be good for you so we're going
16209
09:43:13,360 --> 09:43:16,320
to do a few more examples together
16210
09:43:16,320 --> 09:43:18,880
so all of these notes here i'm going to
16211
09:43:18,880 --> 09:43:21,040
leave them for you
16212
09:43:21,040 --> 09:43:23,200
so you have access to them kind of
16213
09:43:23,200 --> 09:43:25,680
whenever you want
16214
09:43:25,680 --> 09:43:27,596
okay let's go let's make sure that
16215
09:43:27,596 --> 09:43:30,080
they're running
16216
09:43:30,080 --> 09:43:31,520
and if you ever want to run them you can
16217
09:43:31,520 --> 09:43:33,680
just copy them into a new rupple and run
16218
09:43:33,680 --> 09:43:36,160
it okay and all of these notes i'm going
16219
09:43:36,160 --> 09:43:38,000
to take them we're going to go back to
16220
09:43:38,000 --> 09:43:39,840
our javascript course
16221
09:43:39,840 --> 09:43:43,756
and then inside of classes i'm going to
16222
09:43:43,756 --> 09:43:45,680
create a new
16223
09:43:45,680 --> 09:43:46,960
folder
16224
09:43:46,960 --> 09:43:49,596
okay and in this folder
16225
09:43:49,596 --> 09:43:51,840
i'm just gonna call it or actually this
16226
09:43:51,840 --> 09:43:54,400
could work really well as a
16227
09:43:54,400 --> 09:43:56,160
yeah so that's good actually so let's go
16228
09:43:56,160 --> 09:43:57,916
inside of here we're gonna create a new
16229
09:43:57,916 --> 09:44:01,596
folder and i'm gonna call it
16230
09:44:01,596 --> 09:44:03,120
car
16231
09:44:03,120 --> 09:44:04,960
class okay
16232
09:44:04,960 --> 09:44:06,560
and then inside of here we're going to
16233
09:44:06,560 --> 09:44:09,200
make a new file we're just going to say
16234
09:44:09,200 --> 09:44:10,800
how did we name them we should just call
16235
09:44:10,800 --> 09:44:12,800
them script.js everywhere for fighting
16236
09:44:12,800 --> 09:44:14,560
game would it be yeah we just call
16237
09:44:14,560 --> 09:44:16,320
script.js that's fine
16238
09:44:16,320 --> 09:44:18,640
and then all of this is gonna be in here
16239
09:44:18,640 --> 09:44:20,800
for you okay so you're gonna be able to
16240
09:44:20,800 --> 09:44:22,480
see
16241
09:44:22,480 --> 09:44:24,640
how do you actually work with uh
16242
09:44:24,640 --> 09:44:26,240
you know how did we create the car class
16243
09:44:26,240 --> 09:44:27,756
and that lesson is gonna be there so you
16244
09:44:27,756 --> 09:44:30,080
can just go and take a look at it at any
16245
09:44:30,080 --> 09:44:32,480
given time that you want all right to
16246
09:44:32,480 --> 09:44:34,960
finish this off i'm gonna leave you with
16247
09:44:34,960 --> 09:44:37,840
an exercise
16248
09:44:37,840 --> 09:44:41,276
try creating a new replit a new rupple
16249
09:44:41,276 --> 09:44:42,960
okay on replit
16250
09:44:42,960 --> 09:44:45,436
with just html css javascript and try
16251
09:44:45,436 --> 09:44:47,916
creating a bank class
16252
09:44:47,916 --> 09:44:51,840
okay so create a bank class
16253
09:44:51,840 --> 09:44:55,360
and then it should have methods such as
16254
09:44:55,360 --> 09:44:56,880
withdraw
16255
09:44:56,880 --> 09:44:58,480
and it should have methods such as
16256
09:44:58,480 --> 09:45:00,560
deposit okay
16257
09:45:00,560 --> 09:45:03,360
and it should have properties
16258
09:45:03,360 --> 09:45:05,120
such as
16259
09:45:05,120 --> 09:45:07,120
balance okay
16260
09:45:07,120 --> 09:45:10,220
so for example i should be able to do
16261
09:45:10,220 --> 09:45:11,916
[Music]
16262
09:45:11,916 --> 09:45:13,200
um
16263
09:45:13,200 --> 09:45:15,436
you know kazi accounts
16264
09:45:15,436 --> 09:45:17,840
and i should be able to do new bank
16265
09:45:17,840 --> 09:45:19,680
account or whatever
16266
09:45:19,680 --> 09:45:21,200
account
16267
09:45:21,200 --> 09:45:23,756
um and you could call the class bank
16268
09:45:23,756 --> 09:45:26,480
account or bank i don't care whatever
16269
09:45:26,480 --> 09:45:29,120
and then maybe you could uh start it off
16270
09:45:29,120 --> 09:45:31,840
with the initial balance okay so and
16271
09:45:31,840 --> 09:45:35,040
then if you ever do self dot or
16272
09:45:35,040 --> 09:45:37,360
sorry python world desktop balance it
16273
09:45:37,360 --> 09:45:39,200
should get me the current balance in
16274
09:45:39,200 --> 09:45:42,480
this case it should get me back you know
16275
09:45:42,480 --> 09:45:45,200
100.
16276
09:45:45,200 --> 09:45:46,800
if i do
16277
09:45:46,800 --> 09:45:49,480
this dot
16278
09:45:49,480 --> 09:45:52,320
deposits 100
16279
09:45:52,320 --> 09:45:54,640
the balance should get incremented so
16280
09:45:54,640 --> 09:45:57,276
what should happen is give me one second
16281
09:45:57,276 --> 09:45:59,756
and i'll show you what should happen
16282
09:45:59,756 --> 09:46:03,436
okay let's move this up here
16283
09:46:03,436 --> 09:46:03,940
boom
16284
09:46:03,940 --> 09:46:05,840
[Music]
16285
09:46:05,840 --> 09:46:08,240
all right if i do that then
16286
09:46:08,240 --> 09:46:10,720
after this if i actually take a look at
16287
09:46:10,720 --> 09:46:12,320
this dot balance
16288
09:46:12,320 --> 09:46:14,720
that should become 200.
16289
09:46:14,720 --> 09:46:18,560
if i do this dot withdraw
16290
09:46:18,560 --> 09:46:20,400
100
16291
09:46:20,400 --> 09:46:22,800
then when i look at this.balance
16292
09:46:22,800 --> 09:46:25,276
that should become 100 okay because i
16293
09:46:25,276 --> 09:46:27,360
just subtracted it
16294
09:46:27,360 --> 09:46:28,160
so
16295
09:46:28,160 --> 09:46:31,276
i want you to give this a shot
16296
09:46:31,276 --> 09:46:33,276
see how far
16297
09:46:33,276 --> 09:46:34,880
you can take it and how much of it you
16298
09:46:34,880 --> 09:46:36,480
can complete i really believe you should
16299
09:46:36,480 --> 09:46:38,880
be able to do this entire thing
16300
09:46:38,880 --> 09:46:40,480
and you should be able to call these
16301
09:46:40,480 --> 09:46:41,680
methods
16302
09:46:41,680 --> 09:46:43,360
okay and then
16303
09:46:43,360 --> 09:46:45,120
do them right so it should be this that
16304
09:46:45,120 --> 09:46:48,240
deposit yep 100 like this it's a
16305
09:46:48,240 --> 09:46:50,080
method right and then it takes in a
16306
09:46:50,080 --> 09:46:52,640
function sorry it's a method and it has
16307
09:46:52,640 --> 09:46:54,880
a parameter that you have to pass it so
16308
09:46:54,880 --> 09:46:56,560
in this case i'm going to pass it 100
16309
09:46:56,560 --> 09:46:58,720
and then increments it by 100 if i do
16310
09:46:58,720 --> 09:47:01,276
5000 or 10 000 it should increment by
16311
09:47:01,276 --> 09:47:03,520
that amount
16312
09:47:03,520 --> 09:47:06,080
on the other edge cases
16313
09:47:06,080 --> 09:47:07,916
let me ask you this if you have zero
16314
09:47:07,916 --> 09:47:09,200
dollars in the bank should you be able
16315
09:47:09,200 --> 09:47:11,200
to withdraw a hundred dollars so let's
16316
09:47:11,200 --> 09:47:13,520
say that this dot balance
16317
09:47:13,520 --> 09:47:15,756
or let's say you try to withdraw in this
16318
09:47:15,756 --> 09:47:17,040
case
16319
09:47:17,040 --> 09:47:18,720
two hundred dollars when you only have a
16320
09:47:18,720 --> 09:47:20,320
hundred dollars left should you be able
16321
09:47:20,320 --> 09:47:22,080
to do that
16322
09:47:22,080 --> 09:47:23,840
no you should not
16323
09:47:23,840 --> 09:47:24,560
so
16324
09:47:24,560 --> 09:47:27,200
it should catch that
16325
09:47:27,200 --> 09:47:30,800
it should stop that from happening
16326
09:47:30,800 --> 09:47:31,756
and
16327
09:47:31,756 --> 09:47:34,480
it should print out an error message
16328
09:47:34,480 --> 09:47:38,800
telling the user hey you cannot withdraw
16329
09:47:39,360 --> 09:47:41,360
more than what you don't have
16330
09:47:41,360 --> 09:47:42,960
in order and if you want to make it
16331
09:47:42,960 --> 09:47:45,520
really jacked and really amazing try
16332
09:47:45,520 --> 09:47:47,840
adding a ui to this project i don't care
16333
09:47:47,840 --> 09:47:50,240
if it's ugly it doesn't matter
16334
09:47:50,240 --> 09:47:52,480
but see if you can add a ui to this as
16335
09:47:52,480 --> 09:47:54,400
well so it has like buttons i could
16336
09:47:54,400 --> 09:47:56,080
press for withdraw i could click them
16337
09:47:56,080 --> 09:47:58,240
and they can help me withdraw
16338
09:47:58,240 --> 09:47:59,840
and then i click a button it helps me
16339
09:47:59,840 --> 09:48:03,360
deposits deposit
16340
09:48:03,360 --> 09:48:04,480
so maybe
16341
09:48:04,480 --> 09:48:06,240
so let's actually draw this out right so
16342
09:48:06,240 --> 09:48:09,120
i have a
16343
09:48:09,120 --> 09:48:11,436
button here
16344
09:48:11,436 --> 09:48:13,276
for withdraw
16345
09:48:13,276 --> 09:48:16,276
draw
16346
09:48:16,720 --> 09:48:19,200
i have another button
16347
09:48:19,200 --> 09:48:20,320
okay
16348
09:48:20,320 --> 09:48:23,520
i have another button
16349
09:48:23,520 --> 09:48:26,720
right here or wherever it doesn't matter
16350
09:48:26,720 --> 09:48:28,960
for
16351
09:48:29,200 --> 09:48:31,520
deposit i don't care about the ui as
16352
09:48:31,520 --> 09:48:34,800
long as you get get it done
16353
09:48:34,800 --> 09:48:38,240
and then we have another
16354
09:48:38,240 --> 09:48:40,000
button here
16355
09:48:40,000 --> 09:48:41,840
or not a button but like rather maybe
16356
09:48:41,840 --> 09:48:43,360
you have a
16357
09:48:43,360 --> 09:48:45,360
you know maybe here it actually says
16358
09:48:45,360 --> 09:48:48,080
what the balance is so it just says like
16359
09:48:48,080 --> 09:48:49,680
hit a balance right now
16360
09:48:49,680 --> 09:48:52,080
is 100 or whatever so it actually
16361
09:48:52,080 --> 09:48:53,840
literally has the balance written out
16362
09:48:53,840 --> 09:48:55,360
and then the balance increases and you
16363
09:48:55,360 --> 09:48:57,520
can see it on the screen
16364
09:48:57,520 --> 09:48:59,520
right so you hit withdraw you hit
16365
09:48:59,520 --> 09:49:01,436
deposit it either decrements or
16366
09:49:01,436 --> 09:49:03,916
increments
16367
09:49:03,916 --> 09:49:05,840
and you know and then if there are any
16368
09:49:05,840 --> 09:49:07,596
other methods or anything like that you
16369
09:49:07,596 --> 09:49:10,240
could think about adding them uh maybe
16370
09:49:10,240 --> 09:49:12,320
there is like you want to close your
16371
09:49:12,320 --> 09:49:13,916
checking account or something like that
16372
09:49:13,916 --> 09:49:16,560
and then just clears up everything
16373
09:49:16,560 --> 09:49:17,680
you know
16374
09:49:17,680 --> 09:49:18,640
but
16375
09:49:18,640 --> 09:49:21,840
this is like the main crux of it so
16376
09:49:21,840 --> 09:49:26,720
try doing this see if you can
16377
09:49:26,720 --> 09:49:28,400
nail this one you should definitely able
16378
09:49:28,400 --> 09:49:30,480
to nail this exercise
16379
09:49:30,480 --> 09:49:33,360
and then once you give it a full solid
16380
09:49:33,360 --> 09:49:34,400
attempt
16381
09:49:34,400 --> 09:49:36,480
then we'll work together and code
16382
09:49:36,480 --> 09:49:38,080
together and
16383
09:49:38,080 --> 09:49:39,680
make the finished version
16384
09:49:39,680 --> 09:49:42,240
got it all right i hope you're excited
16385
09:49:42,240 --> 09:49:45,120
go give it a try and then come back and
16386
09:49:45,120 --> 09:49:47,040
we'll do this together all right now
16387
09:49:47,040 --> 09:49:49,840
let's work and create that bank class
16388
09:49:49,840 --> 09:49:51,596
that we were talking about
16389
09:49:51,596 --> 09:49:54,480
so let's go ahead and do this so i have
16390
09:49:54,480 --> 09:49:56,560
this bang class the first thing that
16391
09:49:56,560 --> 09:49:58,720
comes to my mind before i even start
16392
09:49:58,720 --> 09:50:01,360
thinking about anything is bank class
16393
09:50:01,360 --> 09:50:02,880
bank just like this
16394
09:50:02,880 --> 09:50:03,756
okay
16395
09:50:03,756 --> 09:50:05,276
um and
16396
09:50:05,276 --> 09:50:07,276
you know i'm like okay cool that's
16397
09:50:07,276 --> 09:50:09,276
that's that's a good start
16398
09:50:09,276 --> 09:50:10,640
what's next
16399
09:50:10,640 --> 09:50:12,560
well what's next is i have certain
16400
09:50:12,560 --> 09:50:14,640
methods i have a draw method so i'm just
16401
09:50:14,640 --> 09:50:16,160
going to go ahead and kind of create
16402
09:50:16,160 --> 09:50:17,596
that
16403
09:50:17,596 --> 09:50:20,320
and then i have a deposit method so i'm
16404
09:50:20,320 --> 09:50:22,640
going to go ahead and create that
16405
09:50:22,640 --> 09:50:23,840
okay
16406
09:50:23,840 --> 09:50:24,720
the
16407
09:50:24,720 --> 09:50:26,640
the balance is going to be something
16408
09:50:26,640 --> 09:50:28,960
that's going to be instantiated so it's
16409
09:50:28,960 --> 09:50:30,880
going to be like right when the class is
16410
09:50:30,880 --> 09:50:32,160
created the balance is going to be
16411
09:50:32,160 --> 09:50:33,916
created right at the exact same time so
16412
09:50:33,916 --> 09:50:36,320
for that i'm going to need constructor
16413
09:50:36,320 --> 09:50:38,480
and the construction is going to just be
16414
09:50:38,480 --> 09:50:40,960
kind of empty like this and i'm going to
16415
09:50:40,960 --> 09:50:41,756
say
16416
09:50:41,756 --> 09:50:43,200
i'm going to grab balance and we're
16417
09:50:43,200 --> 09:50:46,400
going to say this stop balance is
16418
09:50:46,400 --> 09:50:49,276
whatever the balance was passed in then
16419
09:50:49,276 --> 09:50:51,360
all withdrawal does in my mind is
16420
09:50:51,360 --> 09:50:54,080
withdraw
16421
09:50:54,720 --> 09:50:58,640
right this this function right over here
16422
09:50:58,640 --> 09:51:00,640
withdraw
16423
09:51:00,640 --> 09:51:03,200
what this does is it should just take
16424
09:51:03,200 --> 09:51:07,276
this dot balance and increment it by
16425
09:51:07,276 --> 09:51:10,640
whatever amount the user wants
16426
09:51:10,640 --> 09:51:12,960
and we should have pretty much the same
16427
09:51:12,960 --> 09:51:15,120
thing here for deposit but we decrement
16428
09:51:15,120 --> 09:51:17,276
it oh sorry the other way the other way
16429
09:51:17,276 --> 09:51:19,596
the other way decrement on withdrawal
16430
09:51:19,596 --> 09:51:21,680
and increment on deposit right because
16431
09:51:21,680 --> 09:51:23,916
we're depositing money and we're taking
16432
09:51:23,916 --> 09:51:25,916
out money
16433
09:51:25,916 --> 09:51:29,520
so so far looks pretty good to me looks
16434
09:51:29,520 --> 09:51:31,436
like we're almost done honestly with
16435
09:51:31,436 --> 09:51:34,480
this let's go ahead and
16436
09:51:34,480 --> 09:51:37,200
do const causey
16437
09:51:37,200 --> 09:51:41,360
checking and we'll say cla new bank
16438
09:51:41,360 --> 09:51:42,960
and uh we're going to put in we're going
16439
09:51:42,960 --> 09:51:45,276
to start it off with a deposit of 100
16440
09:51:45,276 --> 09:51:48,480
okay let's go ahead and console
16441
09:51:48,480 --> 09:51:49,360
log
16442
09:51:49,360 --> 09:51:51,276
causey checking
16443
09:51:51,276 --> 09:51:53,360
dot balance
16444
09:51:53,360 --> 09:51:54,320
okay
16445
09:51:54,320 --> 09:51:57,040
and then we're simply going to i'm gonna
16446
09:51:57,040 --> 09:51:58,960
just move it a little bit out of the way
16447
09:51:58,960 --> 09:52:01,916
here and we're gonna say run
16448
09:52:01,916 --> 09:52:03,520
okay and you can see that i'm getting
16449
09:52:03,520 --> 09:52:07,520
back uh 100 here which is great
16450
09:52:07,520 --> 09:52:09,200
you know that's exactly what i had
16451
09:52:09,200 --> 09:52:11,680
expected when we made this
16452
09:52:11,680 --> 09:52:14,320
now let's try adding so i'm going to say
16453
09:52:14,320 --> 09:52:17,200
causeychecking.deposit
16454
09:52:17,200 --> 09:52:19,276
and we're going to add in 100 and then
16455
09:52:19,276 --> 09:52:21,596
let's print out the balance again
16456
09:52:21,596 --> 09:52:24,560
okay and now the balance is 200
16457
09:52:24,560 --> 09:52:26,480
and what we can do is inside of our
16458
09:52:26,480 --> 09:52:29,596
withdraw function i can just add an or
16459
09:52:29,596 --> 09:52:31,596
end deposit i can add a console log
16460
09:52:31,596 --> 09:52:32,800
right here
16461
09:52:32,800 --> 09:52:35,520
and i can say
16462
09:52:36,400 --> 09:52:38,000
so it just shows us the balance right
16463
09:52:38,000 --> 09:52:39,220
away okay so
16464
09:52:39,220 --> 09:52:40,960
[Music]
16465
09:52:40,960 --> 09:52:43,120
that won't work so i'll just do balance
16466
09:52:43,120 --> 09:52:46,480
call in this dot balance like that okay
16467
09:52:46,480 --> 09:52:47,756
and you could call this whatever you
16468
09:52:47,756 --> 09:52:50,720
want that doesn't matter and this in
16469
09:52:50,720 --> 09:52:52,240
objects is the same thing as if you did
16470
09:52:52,240 --> 09:52:53,520
balance
16471
09:52:53,520 --> 09:52:54,320
okay
16472
09:52:54,320 --> 09:52:55,840
so both are the same so i'm just going
16473
09:52:55,840 --> 09:52:57,200
to copy this
16474
09:52:57,200 --> 09:53:00,320
and i'll also paste it over here
16475
09:53:00,320 --> 09:53:02,160
so we have
16476
09:53:02,160 --> 09:53:03,596
um
16477
09:53:03,596 --> 09:53:05,840
so basically as soon as it updates the
16478
09:53:05,840 --> 09:53:08,080
balance amount it should just show it
16479
09:53:08,080 --> 09:53:10,240
okay so then that way we don't have to
16480
09:53:10,240 --> 09:53:12,000
do all these
16481
09:53:12,000 --> 09:53:14,800
a ton of console logs
16482
09:53:14,800 --> 09:53:15,840
okay
16483
09:53:15,840 --> 09:53:18,080
and maybe let's add another console log
16484
09:53:18,080 --> 09:53:19,360
that says
16485
09:53:19,360 --> 09:53:21,680
console.log
16486
09:53:21,680 --> 09:53:23,436
deposited
16487
09:53:23,436 --> 09:53:26,640
and the amount
16488
09:53:26,800 --> 09:53:27,680
okay
16489
09:53:27,680 --> 09:53:29,200
and then we'll do the same thing for
16490
09:53:29,200 --> 09:53:31,680
withdraw and we'll say with
16491
09:53:31,680 --> 09:53:32,720
drew
16492
09:53:32,720 --> 09:53:34,960
the amount so let's run it
16493
09:53:34,960 --> 09:53:36,720
and now you can see it says deposit it
16494
09:53:36,720 --> 09:53:38,480
100 right
16495
09:53:38,480 --> 09:53:41,840
and we can put in
16496
09:53:43,520 --> 09:53:46,400
so i just put a dollar sign here
16497
09:53:46,400 --> 09:53:48,320
and then we can put amount like that so
16498
09:53:48,320 --> 09:53:50,000
it'll show it with the dollar sign so
16499
09:53:50,000 --> 09:53:52,400
it's a deposit 100 so balance is now
16500
09:53:52,400 --> 09:53:54,560
200.
16501
09:53:54,560 --> 09:53:56,000
okay
16502
09:53:56,000 --> 09:53:58,960
and we can we can do this same trick
16503
09:53:58,960 --> 09:54:01,120
that we did here for with
16504
09:54:01,120 --> 09:54:03,520
for withdrawing okay
16505
09:54:03,520 --> 09:54:04,560
so
16506
09:54:04,560 --> 09:54:08,560
let's try with draw
16507
09:54:10,080 --> 09:54:12,560
so after withdrawing balance is 100
16508
09:54:12,560 --> 09:54:14,880
let's withdraw one more time balance is
16509
09:54:14,880 --> 09:54:18,240
100 0 right now here's going to be the
16510
09:54:18,240 --> 09:54:19,200
problem
16511
09:54:19,200 --> 09:54:21,040
if i try to withdraw one more time do
16512
09:54:21,040 --> 09:54:22,720
you think it's gonna let me withdraw
16513
09:54:22,720 --> 09:54:24,080
i'll give you five seconds to figure
16514
09:54:24,080 --> 09:54:28,720
that out five four three two one
16515
09:54:28,800 --> 09:54:30,880
yes it's gonna let me withdraw right
16516
09:54:30,880 --> 09:54:33,596
watch what happens boom negative 100
16517
09:54:33,596 --> 09:54:35,756
should i be able to withdraw allowed to
16518
09:54:35,756 --> 09:54:37,596
withdraw money that i don't have the
16519
09:54:37,596 --> 09:54:39,840
answer is resounding
16520
09:54:39,840 --> 09:54:42,400
no right heck no i should not be allowed
16521
09:54:42,400 --> 09:54:45,360
to do that that is crazy otherwise
16522
09:54:45,360 --> 09:54:47,120
would be super rich right all of us
16523
09:54:47,120 --> 09:54:49,040
would be insanely rich so what we want
16524
09:54:49,040 --> 09:54:51,840
to do in that case is
16525
09:54:51,840 --> 09:54:54,320
add a guard clause inside of our
16526
09:54:54,320 --> 09:54:55,520
withdrawal
16527
09:54:55,520 --> 09:54:58,400
okay so we're going to say guard clause
16528
09:54:58,400 --> 09:55:00,160
so protect against this and i'm just
16529
09:55:00,160 --> 09:55:03,436
going to say if and hopefully you tried
16530
09:55:03,436 --> 09:55:05,360
this and you got it if you did good job
16531
09:55:05,360 --> 09:55:07,200
and if you didn't that's okay but like
16532
09:55:07,200 --> 09:55:08,480
if you didn't get it and you want to
16533
09:55:08,480 --> 09:55:11,200
attempt it now try pausing it now
16534
09:55:11,200 --> 09:55:12,640
and basically what i'm gonna say is i'm
16535
09:55:12,640 --> 09:55:14,320
gonna say
16536
09:55:14,320 --> 09:55:15,276
if
16537
09:55:15,276 --> 09:55:19,120
this dot balance is less than
16538
09:55:19,120 --> 09:55:20,320
or equal
16539
09:55:20,320 --> 09:55:23,840
to zero then just return like meaning
16540
09:55:23,840 --> 09:55:26,080
don't don't run
16541
09:55:26,080 --> 09:55:28,000
don't run this function method just stop
16542
09:55:28,000 --> 09:55:31,596
it right here okay so let's try running
16543
09:55:31,596 --> 09:55:34,240
let's try this again and let's see what
16544
09:55:34,240 --> 09:55:35,596
happens
16545
09:55:35,596 --> 09:55:38,480
okay and maybe we can even say this and
16546
09:55:38,480 --> 09:55:40,480
and say um
16547
09:55:40,480 --> 09:55:41,840
you know or maybe we can we can like
16548
09:55:41,840 --> 09:55:44,160
throw an error here too or a console log
16549
09:55:44,160 --> 09:55:46,640
and just say
16550
09:55:46,720 --> 09:55:48,960
can't
16551
09:55:48,960 --> 09:55:51,120
not
16552
09:55:51,120 --> 09:55:53,756
withdraw
16553
09:55:56,800 --> 09:55:58,400
you cannot withdraw
16554
09:55:58,400 --> 09:56:00,240
more than
16555
09:56:00,240 --> 09:56:03,040
what you have so if we're already at
16556
09:56:03,040 --> 09:56:05,596
zero this will stop us this statement
16557
09:56:05,596 --> 09:56:07,840
will stop us with drawing if we're
16558
09:56:07,840 --> 09:56:10,560
already at zero okay so for example if i
16559
09:56:10,560 --> 09:56:12,480
try running this now
16560
09:56:12,480 --> 09:56:14,160
it says you cannot withdraw more than
16561
09:56:14,160 --> 09:56:17,120
what you have so this is really good for
16562
09:56:17,120 --> 09:56:20,160
zero but the the problem still remains
16563
09:56:20,160 --> 09:56:24,080
for example if my balance was at 100 i
16564
09:56:24,080 --> 09:56:26,480
could still withdraw 200 right that
16565
09:56:26,480 --> 09:56:28,560
won't we won't be stopped by that let's
16566
09:56:28,560 --> 09:56:29,520
say
16567
09:56:29,520 --> 09:56:32,640
i try 200 and run this you can see now
16568
09:56:32,640 --> 09:56:35,596
my balance is negative 100 right and
16569
09:56:35,596 --> 09:56:38,080
then it stops me after that
16570
09:56:38,080 --> 09:56:40,000
but that's not what should happen we
16571
09:56:40,000 --> 09:56:42,800
should be able to withdraw before that
16572
09:56:42,800 --> 09:56:44,480
so maybe what we want to check is we
16573
09:56:44,480 --> 09:56:47,840
want to say if this dot balance minus
16574
09:56:47,840 --> 09:56:49,360
the amount
16575
09:56:49,360 --> 09:56:52,080
that we're being passed in
16576
09:56:52,080 --> 09:56:53,916
if that's less than or equal to zero
16577
09:56:53,916 --> 09:56:55,756
then just stop so let's try running it
16578
09:56:55,756 --> 09:56:58,560
now and you'll see that it doesn't let
16579
09:56:58,560 --> 09:57:00,800
us withdraw less than you know once
16580
09:57:00,800 --> 09:57:02,720
we're at 100 it doesn't let us withdraw
16581
09:57:02,720 --> 09:57:05,436
and it stops us twice and stops us both
16582
09:57:05,436 --> 09:57:08,080
times so we'll not be able to withdraw
16583
09:57:08,080 --> 09:57:09,916
any more than we have right so for
16584
09:57:09,916 --> 09:57:12,240
example if i try to withdraw let's say a
16585
09:57:12,240 --> 09:57:14,960
thousand dollars it'll just say sorry
16586
09:57:14,960 --> 09:57:17,360
can now withdraw it all right and the
16587
09:57:17,360 --> 09:57:20,560
our balance will still be at at 200 okay
16588
09:57:20,560 --> 09:57:23,200
so maybe we can i can do this it's like
16589
09:57:23,200 --> 09:57:25,596
i can print out the balance as well here
16590
09:57:25,596 --> 09:57:27,040
right so you can now withdraw more than
16591
09:57:27,040 --> 09:57:29,200
what you have and then boom shows the
16592
09:57:29,200 --> 09:57:32,320
balance right there so that stops it and
16593
09:57:32,320 --> 09:57:34,960
now you can you can try
16594
09:57:34,960 --> 09:57:36,800
depositing you know as much as you want
16595
09:57:36,800 --> 09:57:38,720
you can deposit ten thousand ten
16596
09:57:38,720 --> 09:57:40,560
thousand ten thousand if you look at
16597
09:57:40,560 --> 09:57:42,640
your balance it'll be twenty nine
16598
09:57:42,640 --> 09:57:44,480
thousand one hundred now let's say you
16599
09:57:44,480 --> 09:57:47,276
start with drawing twenty thousand and
16600
09:57:47,276 --> 09:57:50,640
then you try with drawing 5 000 and then
16601
09:57:50,640 --> 09:57:53,276
another 5 000. let's see what happens so
16602
09:57:53,276 --> 09:57:56,000
a bunch of your deposits deposited 1000
16603
09:57:56,000 --> 09:57:57,520
withdrew
16604
09:57:57,520 --> 09:58:00,400
so deposited one 10 000 deposited 10 000
16605
09:58:00,400 --> 09:58:01,916
and then you tried to withdraw so it's
16606
09:58:01,916 --> 09:58:05,040
like withdrew 1000 withdrew 20 000 so
16607
09:58:05,040 --> 09:58:08,960
balance is not 90 100 from 29 000 that
16608
09:58:08,960 --> 09:58:11,200
makes sense
16609
09:58:11,200 --> 09:58:14,240
but when your balance got to 4 100 right
16610
09:58:14,240 --> 09:58:17,200
and you try to withdraw 5 000 well you
16611
09:58:17,200 --> 09:58:19,840
ca it says you can't you can't withdraw
16612
09:58:19,840 --> 09:58:22,160
five thousand dollars so
16613
09:58:22,160 --> 09:58:24,720
that's how those methods are working so
16614
09:58:24,720 --> 09:58:27,040
that's really really good now the only
16615
09:58:27,040 --> 09:58:29,520
thing that was left is if you could kind
16616
09:58:29,520 --> 09:58:32,080
of create a ui for this right so let's
16617
09:58:32,080 --> 09:58:34,080
go ahead and try to create a ui for this
16618
09:58:34,080 --> 09:58:35,680
that shouldn't be that hard so the first
16619
09:58:35,680 --> 09:58:37,360
things i want to do in order to create a
16620
09:58:37,360 --> 09:58:38,480
ui is
16621
09:58:38,480 --> 09:58:40,480
i need some buttons
16622
09:58:40,480 --> 09:58:41,756
all right i'll create another button
16623
09:58:41,756 --> 09:58:42,560
here
16624
09:58:42,560 --> 09:58:45,436
and for this button we'll call it
16625
09:58:45,436 --> 09:58:49,840
we'll give this button an id of deposit
16626
09:58:49,840 --> 09:58:54,240
deposits we'll give this button id of
16627
09:58:54,240 --> 09:58:57,276
give this button an id of withdrawal
16628
09:58:57,276 --> 09:58:59,360
okay so we got two buttons here
16629
09:58:59,360 --> 09:59:01,840
and it'll say deposit this button will
16630
09:59:01,840 --> 09:59:02,720
say
16631
09:59:02,720 --> 09:59:05,120
withdraw so a little plus sign to show
16632
09:59:05,120 --> 09:59:07,040
that that adds money this withdraws
16633
09:59:07,040 --> 09:59:09,436
minus sign that that withdraws money
16634
09:59:09,436 --> 09:59:11,756
we'll remove this hello world from there
16635
09:59:11,756 --> 09:59:13,840
okay so now we have deposit and withdraw
16636
09:59:13,840 --> 09:59:17,040
um we'll have the text for
16637
09:59:17,040 --> 09:59:19,276
your balance right there so
16638
09:59:19,276 --> 09:59:21,756
i will have a div here
16639
09:59:21,756 --> 09:59:25,200
and then inside of this div i will have
16640
09:59:25,200 --> 09:59:26,640
a p tag
16641
09:59:26,640 --> 09:59:29,200
and this p tag will say
16642
09:59:29,200 --> 09:59:30,880
uh
16643
09:59:30,880 --> 09:59:31,916
balance
16644
09:59:31,916 --> 09:59:34,160
right and then balance will be zero for
16645
09:59:34,160 --> 09:59:36,800
now and then we'll give this an id
16646
09:59:36,800 --> 09:59:39,276
of balance this div because then we're
16647
09:59:39,276 --> 09:59:40,400
going to be able to change its inner
16648
09:59:40,400 --> 09:59:42,400
attacks or it's inner html
16649
09:59:42,400 --> 09:59:43,916
right
16650
09:59:43,916 --> 09:59:46,480
perfect so we got balance right there
16651
09:59:46,480 --> 09:59:47,360
cool
16652
09:59:47,360 --> 09:59:48,080
so
16653
09:59:48,080 --> 09:59:50,160
we have our balance we have deposit
16654
09:59:50,160 --> 09:59:51,200
withdraw
16655
09:59:51,200 --> 09:59:53,040
and now what we want to do is when we
16656
09:59:53,040 --> 09:59:54,080
click
16657
09:59:54,080 --> 09:59:57,120
we want certain functions to run so what
16658
09:59:57,120 --> 09:59:58,880
i can do and there's this nice little
16659
09:59:58,880 --> 10:00:01,120
trick since i have this deposit id right
16660
10:00:01,120 --> 10:00:02,880
here i could actually just
16661
10:00:02,880 --> 10:00:05,520
say i believe deposit and should run
16662
10:00:05,520 --> 10:00:07,520
this i think so
16663
10:00:07,520 --> 10:00:09,120
but
16664
10:00:09,120 --> 10:00:12,240
let's uh let's not be that hacky let's
16665
10:00:12,240 --> 10:00:15,360
try to avoid being that that degenerate
16666
10:00:15,360 --> 10:00:17,520
so i'm just gonna say const
16667
10:00:17,520 --> 10:00:20,300
deposit button and we're gonna
16668
10:00:20,300 --> 10:00:22,840
[Music]
16669
10:00:22,840 --> 10:00:24,720
documents
16670
10:00:24,720 --> 10:00:27,680
dot get elements by id and we're gonna
16671
10:00:27,680 --> 10:00:29,840
say deposit
16672
10:00:29,840 --> 10:00:31,040
okay
16673
10:00:31,040 --> 10:00:35,360
and then i'm gonna do the same thing
16674
10:00:35,360 --> 10:00:38,640
and we'll get the
16675
10:00:38,640 --> 10:00:40,240
withdrawal
16676
10:00:40,240 --> 10:00:42,160
button and all we're going to do is
16677
10:00:42,160 --> 10:00:43,840
we're just going to attach listeners to
16678
10:00:43,840 --> 10:00:46,080
this i'm going to say deposit button dot
16679
10:00:46,080 --> 10:00:50,400
on click and every time you do on click
16680
10:00:50,400 --> 10:00:52,960
we're going to do you know quasi
16681
10:00:52,960 --> 10:00:54,640
checking and this we'll just do cause
16682
10:00:54,640 --> 10:00:56,160
you checking that withdraw that's fine
16683
10:00:56,160 --> 10:00:58,400
cause you checking that
16684
10:00:58,400 --> 10:00:59,680
withdraw
16685
10:00:59,680 --> 10:01:02,160
and um oh okay that's a good that's a
16686
10:01:02,160 --> 10:01:04,080
good point we'll also have to do a input
16687
10:01:04,080 --> 10:01:06,880
field for like how much do we want to
16688
10:01:06,880 --> 10:01:08,720
deposit or withdraw right so let's do
16689
10:01:08,720 --> 10:01:13,276
input and we'll give this an id of
16690
10:01:13,360 --> 10:01:16,480
amount so let's go back and i will
16691
10:01:16,480 --> 10:01:18,160
simply
16692
10:01:18,160 --> 10:01:19,276
go
16693
10:01:19,276 --> 10:01:23,436
amount input and then we will say amount
16694
10:01:23,436 --> 10:01:25,840
so if i hit run again you'll see i have
16695
10:01:25,840 --> 10:01:27,756
a field above here because that will
16696
10:01:27,756 --> 10:01:30,640
basically tell us how much do we want to
16697
10:01:30,640 --> 10:01:33,120
uh withdraw or deposit so i'm going to
16698
10:01:33,120 --> 10:01:34,560
say withdraw
16699
10:01:34,560 --> 10:01:38,240
whatever the amount input dot value is
16700
10:01:38,240 --> 10:01:39,680
okay
16701
10:01:39,680 --> 10:01:42,480
and let's see if that actually
16702
10:01:42,480 --> 10:01:45,276
does anything right so i'm gonna
16703
10:01:45,276 --> 10:01:47,436
i'm just gonna console.log
16704
10:01:47,436 --> 10:01:50,640
and i will say amount
16705
10:01:50,640 --> 10:01:52,030
input dot value
16706
10:01:52,030 --> 10:01:53,200
[Music]
16707
10:01:53,200 --> 10:01:54,560
and let's see if that does what we
16708
10:01:54,560 --> 10:01:56,720
wanted to do so i'll say 23 and i'll run
16709
10:01:56,720 --> 10:01:57,596
this
16710
10:01:57,596 --> 10:02:00,160
sorry 23 and then i'll hit
16711
10:02:00,160 --> 10:02:02,080
i actually got to attach it to something
16712
10:02:02,080 --> 10:02:04,960
right so for now i'll remove this line
16713
10:02:04,960 --> 10:02:06,480
and i'll just say
16714
10:02:06,480 --> 10:02:09,916
amount input dot value and just console
16715
10:02:09,916 --> 10:02:12,240
log it for me please
16716
10:02:12,240 --> 10:02:14,240
let's run this and let's hit deposit
16717
10:02:14,240 --> 10:02:18,000
deposit deposit 23 deposit 25 deposit
16718
10:02:18,000 --> 10:02:20,320
deposit that does not do what we want it
16719
10:02:20,320 --> 10:02:22,880
to do let's just console log anything
16720
10:02:22,880 --> 10:02:25,040
for now just to see if things are
16721
10:02:25,040 --> 10:02:27,040
attached the right way
16722
10:02:27,040 --> 10:02:29,040
let's run this let's deposit deposit
16723
10:02:29,040 --> 10:02:31,596
deposit did it this is still not doing
16724
10:02:31,596 --> 10:02:33,756
what we wanted to do okay so let's go
16725
10:02:33,756 --> 10:02:36,800
back to our deposit button it has the
16726
10:02:36,800 --> 10:02:38,240
correct id
16727
10:02:38,240 --> 10:02:39,436
of
16728
10:02:39,436 --> 10:02:41,756
deposit right
16729
10:02:41,756 --> 10:02:46,400
so the id is correct let's go back
16730
10:02:50,240 --> 10:02:52,880
pause it this is going to be withdrawal
16731
10:02:52,880 --> 10:02:55,756
and so i'm saying deposit button
16732
10:02:55,756 --> 10:03:00,480
dot on click that let's try that
16733
10:03:00,640 --> 10:03:02,240
ah interesting
16734
10:03:02,240 --> 10:03:03,840
okay let's open this up let's just see
16735
10:03:03,840 --> 10:03:06,000
if it's not a bug
16736
10:03:06,000 --> 10:03:08,080
hit inspect
16737
10:03:08,080 --> 10:03:12,916
console and let's hit draw
16738
10:03:13,680 --> 10:03:16,240
it is not a bug interesting okay we're
16739
10:03:16,240 --> 10:03:17,840
doing something wrong we'll figure it
16740
10:03:17,840 --> 10:03:18,800
out
16741
10:03:18,800 --> 10:03:22,560
oh yes i forgot on click takes in a
16742
10:03:22,560 --> 10:03:24,720
uh anonymous function right so let's do
16743
10:03:24,720 --> 10:03:27,040
this
16744
10:03:27,040 --> 10:03:29,680
and now let's hit deposit yes so it runs
16745
10:03:29,680 --> 10:03:31,276
every single time so now the button
16746
10:03:31,276 --> 10:03:33,596
click is being detected correctly that's
16747
10:03:33,596 --> 10:03:34,400
good
16748
10:03:34,400 --> 10:03:38,480
now let's do amount input dot value
16749
10:03:38,480 --> 10:03:40,480
and let's run that
16750
10:03:40,480 --> 10:03:42,560
oops let's run this
16751
10:03:42,560 --> 10:03:46,160
let's do 23 deposit 25 4 deposit 25
16752
10:03:46,160 --> 10:03:49,276
deposit now this part is running
16753
10:03:49,276 --> 10:03:52,080
correctly as well so if i do 26
16754
10:03:52,080 --> 10:03:53,520
deposit
16755
10:03:53,520 --> 10:03:55,200
right works
16756
10:03:55,200 --> 10:03:58,560
works correctly good okay let's move it
16757
10:03:58,560 --> 10:04:00,320
to a new
16758
10:04:00,320 --> 10:04:03,276
line so things don't get that squished
16759
10:04:03,276 --> 10:04:05,840
okay so i have attached a listener on
16760
10:04:05,840 --> 10:04:07,436
this on click
16761
10:04:07,436 --> 10:04:10,640
now let's try doing that withdraw thing
16762
10:04:10,640 --> 10:04:12,560
that we were doing with
16763
10:04:12,560 --> 10:04:13,420
draw
16764
10:04:13,420 --> 10:04:14,560
[Music]
16765
10:04:14,560 --> 10:04:16,560
right kazi checking out withdraw amount
16766
10:04:16,560 --> 10:04:19,120
dot input value okay
16767
10:04:19,120 --> 10:04:20,160
and
16768
10:04:20,160 --> 10:04:22,560
let's try doing that so i'll do ten
16769
10:04:22,560 --> 10:04:25,360
dollars uh oh sorry not withdraw right
16770
10:04:25,360 --> 10:04:26,720
deposit
16771
10:04:26,720 --> 10:04:28,720
because this is a deposit button so
16772
10:04:28,720 --> 10:04:30,880
let's run the code again let's hit ten
16773
10:04:30,880 --> 10:04:33,680
dollars deposit boom the deposit just
16774
10:04:33,680 --> 10:04:36,000
went up by ten but it's attaching the
16775
10:04:36,000 --> 10:04:38,960
string so now it doesn't say 4
16776
10:04:38,960 --> 10:04:43,120
100 now it says like 410 000
16777
10:04:43,120 --> 10:04:45,756
so what we want to do is we want this to
16778
10:04:45,756 --> 10:04:49,680
be a number and yes okay so we want we
16779
10:04:49,680 --> 10:04:51,120
want that to be a number and what we
16780
10:04:51,120 --> 10:04:53,596
could do is we could just call dot value
16781
10:04:53,596 --> 10:04:56,800
on here and we could just call number on
16782
10:04:56,800 --> 10:04:59,200
this so now and then amount we don't
16783
10:04:59,200 --> 10:05:00,560
need it to be an input because it's
16784
10:05:00,560 --> 10:05:02,720
actually an amount now and we just pass
16785
10:05:02,720 --> 10:05:05,040
this amount around now so now i could
16786
10:05:05,040 --> 10:05:07,200
set deposit amount
16787
10:05:07,200 --> 10:05:11,840
okay so if i said 10 here hit deposit
16788
10:05:11,840 --> 10:05:14,480
amount input is not defined
16789
10:05:14,480 --> 10:05:17,360
so this is just the amount
16790
10:05:17,360 --> 10:05:19,120
and i don't even need that line anymore
16791
10:05:19,120 --> 10:05:20,640
let's run it again
16792
10:05:20,640 --> 10:05:23,200
let's do ten dollars deposit
16793
10:05:23,200 --> 10:05:28,000
okay let's do twenty dollars deposit
16794
10:05:28,000 --> 10:05:28,880
oh
16795
10:05:28,880 --> 10:05:31,756
interesting we actually can't do it like
16796
10:05:31,756 --> 10:05:34,000
that so let's go back
16797
10:05:34,000 --> 10:05:36,240
let's keep that amount input there
16798
10:05:36,240 --> 10:05:38,400
once we get this query selector then we
16799
10:05:38,400 --> 10:05:39,680
have to do dot value because if we just
16800
10:05:39,680 --> 10:05:41,520
do dot value it's going to just get the
16801
10:05:41,520 --> 10:05:44,160
value that it initially had when the
16802
10:05:44,160 --> 10:05:46,240
script loaded and that's it and then
16803
10:05:46,240 --> 10:05:47,360
every time you click it it's just going
16804
10:05:47,360 --> 10:05:49,840
to get you back that value but if you
16805
10:05:49,840 --> 10:05:52,080
keep it like this dom element
16806
10:05:52,080 --> 10:05:53,840
and then at the time of the click pull
16807
10:05:53,840 --> 10:05:55,596
the value then you're actually going to
16808
10:05:55,596 --> 10:05:57,840
get the correct value
16809
10:05:57,840 --> 10:05:59,520
so i apologize but you're going to have
16810
10:05:59,520 --> 10:06:01,840
to keep doing that value here and then
16811
10:06:01,840 --> 10:06:03,040
you're going to have to
16812
10:06:03,040 --> 10:06:06,160
type cast that to a number
16813
10:06:06,160 --> 10:06:08,400
like this okay so this you'll kind of
16814
10:06:08,400 --> 10:06:09,756
have to do
16815
10:06:09,756 --> 10:06:12,160
each time okay so let's run this
16816
10:06:12,160 --> 10:06:13,680
and
16817
10:06:13,680 --> 10:06:16,080
see this i don't need this here anymore
16818
10:06:16,080 --> 10:06:17,520
let's run it again
16819
10:06:17,520 --> 10:06:18,880
all right i'm going to deposit ten
16820
10:06:18,880 --> 10:06:21,040
dollars and now you can see that the ten
16821
10:06:21,040 --> 10:06:23,756
dollars are deposited correctly
16822
10:06:23,756 --> 10:06:24,560
okay
16823
10:06:24,560 --> 10:06:28,080
let's try depositing a thousand dollars
16824
10:06:28,080 --> 10:06:31,520
let's try depositing 20 000
16825
10:06:31,520 --> 10:06:32,960
right
16826
10:06:32,960 --> 10:06:35,200
and you can see
16827
10:06:35,200 --> 10:06:38,480
at the bottom right there
16828
10:06:39,436 --> 10:06:41,840
now it's doing it correctly right we're
16829
10:06:41,840 --> 10:06:43,840
depositing the correct amount
16830
10:06:43,840 --> 10:06:44,720
now
16831
10:06:44,720 --> 10:06:46,160
let's try doing the same thing for the
16832
10:06:46,160 --> 10:06:47,680
withdrawal button so if you haven't done
16833
10:06:47,680 --> 10:06:50,000
it yet try doing the withdrawal button
16834
10:06:50,000 --> 10:06:51,756
yourself and see if you can get it i'll
16835
10:06:51,756 --> 10:06:54,080
give you five seconds five
16836
10:06:54,080 --> 10:06:56,640
four three two
16837
10:06:56,640 --> 10:06:57,436
one
16838
10:06:57,436 --> 10:06:59,520
all right let's try the withdrawal
16839
10:06:59,520 --> 10:07:02,080
button together so i'm gonna paste this
16840
10:07:02,080 --> 10:07:05,756
i'm gonna replace this with withdraw
16841
10:07:05,756 --> 10:07:07,360
and then
16842
10:07:07,360 --> 10:07:10,720
we're gonna use the withdraw method here
16843
10:07:10,720 --> 10:07:12,400
okay and we're just gonna do number
16844
10:07:12,400 --> 10:07:15,756
amount input.value same way
16845
10:07:15,756 --> 10:07:16,560
let's
16846
10:07:16,560 --> 10:07:18,720
run this and i'm gonna say
16847
10:07:18,720 --> 10:07:22,000
100 deposit so balance is 42
16848
10:07:22,000 --> 10:07:25,276
and now withdraw balance 4 100. so now
16849
10:07:25,276 --> 10:07:27,520
deposit and withdraw are working all i
16850
10:07:27,520 --> 10:07:30,960
need to do is update the dom after
16851
10:07:30,960 --> 10:07:33,436
we do this deposit right
16852
10:07:33,436 --> 10:07:35,360
so as soon as that happens we're going
16853
10:07:35,360 --> 10:07:36,800
to update the dom
16854
10:07:36,800 --> 10:07:39,360
and how what i'm going to do is i have
16855
10:07:39,360 --> 10:07:40,320
the
16856
10:07:40,320 --> 10:07:42,640
amount
16857
10:07:42,640 --> 10:07:45,120
not the amount input i need the
16858
10:07:45,120 --> 10:07:47,916
i need the balance right so i need this
16859
10:07:47,916 --> 10:07:49,276
balance div
16860
10:07:49,276 --> 10:07:52,240
so let's go back and we're going to do
16861
10:07:52,240 --> 10:07:55,040
const balance
16862
10:07:55,040 --> 10:07:56,240
div
16863
10:07:56,240 --> 10:07:58,560
document.getelementbyd
16864
10:07:58,560 --> 10:08:00,880
and we're going to say balance
16865
10:08:00,880 --> 10:08:03,040
okay so it's going to get us to div
16866
10:08:03,040 --> 10:08:04,640
and i'm going to say
16867
10:08:04,640 --> 10:08:06,800
inner text
16868
10:08:06,800 --> 10:08:08,800
and we're going to set that to and it's
16869
10:08:08,800 --> 10:08:11,756
going to say balanced
16870
10:08:12,000 --> 10:08:13,756
um
16871
10:08:13,756 --> 10:08:16,800
and we're going to pass in
16872
10:08:16,800 --> 10:08:18,840
this right
16873
10:08:18,840 --> 10:08:22,000
there okay
16874
10:08:24,480 --> 10:08:26,240
you could also just do this so we can
16875
10:08:26,240 --> 10:08:27,916
create a new variable
16876
10:08:27,916 --> 10:08:30,240
use that here
16877
10:08:30,240 --> 10:08:32,560
and use that here okay and then do the
16878
10:08:32,560 --> 10:08:37,520
same thing for the withdrawal function
16879
10:08:37,756 --> 10:08:39,840
and then just replace it with amount so
16880
10:08:39,840 --> 10:08:41,200
then it's cleaner
16881
10:08:41,200 --> 10:08:42,480
okay
16882
10:08:42,480 --> 10:08:44,080
so there we go quasi checking that
16883
10:08:44,080 --> 10:08:46,160
deposit amount
16884
10:08:46,160 --> 10:08:47,680
and then the balance is going to read
16885
10:08:47,680 --> 10:08:49,756
that amount and then we're going to do
16886
10:08:49,756 --> 10:08:51,840
the same thing right here balance div
16887
10:08:51,840 --> 10:08:54,160
enter texas amount let's run this let's
16888
10:08:54,160 --> 10:08:55,596
hit 10
16889
10:08:55,596 --> 10:08:58,400
boom balance is 10. let's do
16890
10:08:58,400 --> 10:08:59,520
30.
16891
10:08:59,520 --> 10:09:02,960
now you can see balance is 30. right but
16892
10:09:02,960 --> 10:09:05,120
the problem here is that it's only
16893
10:09:05,120 --> 10:09:07,360
putting in the amount it's not actually
16894
10:09:07,360 --> 10:09:08,560
putting
16895
10:09:08,560 --> 10:09:11,840
the actual balance so instead of amount
16896
10:09:11,840 --> 10:09:14,840
what should we put here take a
16897
10:09:14,840 --> 10:09:17,360
guess what we should actually put in
16898
10:09:17,360 --> 10:09:19,276
here is we should do
16899
10:09:19,276 --> 10:09:22,080
causeychecking.balance
16900
10:09:22,080 --> 10:09:24,080
okay that's what we want to do here
16901
10:09:24,080 --> 10:09:27,596
let's go ahead and do that
16902
10:09:27,596 --> 10:09:29,680
here as well so it's reading the balance
16903
10:09:29,680 --> 10:09:32,800
the entire time in that case
16904
10:09:32,800 --> 10:09:35,276
all right let's run it
16905
10:09:35,276 --> 10:09:37,756
and let's go 100
16906
10:09:37,756 --> 10:09:39,596
deposit
16907
10:09:39,596 --> 10:09:40,960
1000
16908
10:09:40,960 --> 10:09:43,596
deposit 5200
16909
10:09:43,596 --> 10:09:49,840
20 000 deposit 25 000 withdrawal 1000
16910
10:09:49,840 --> 10:09:52,720
or sorry withdrawal right
16911
10:09:52,720 --> 10:09:55,680
withdrawal let's say
16912
10:09:55,680 --> 10:09:58,720
19 000 it would draw boom the new
16913
10:09:58,720 --> 10:10:01,916
balance is 2200.
16914
10:10:01,916 --> 10:10:03,040
so
16915
10:10:03,040 --> 10:10:05,200
hopefully this should
16916
10:10:05,200 --> 10:10:08,560
so hopefully this tells you a lot about
16917
10:10:08,560 --> 10:10:11,040
how classes work
16918
10:10:11,040 --> 10:10:13,276
you know what you could do with them how
16919
10:10:13,276 --> 10:10:16,000
to then connect them to a ui and all of
16920
10:10:16,000 --> 10:10:18,400
those things because they make your life
16921
10:10:18,400 --> 10:10:19,756
honestly
16922
10:10:19,756 --> 10:10:22,080
a lot easier right and then you can use
16923
10:10:22,080 --> 10:10:27,276
them in a really nice easy way um
16924
10:10:27,276 --> 10:10:29,040
and they're just really useful and
16925
10:10:29,040 --> 10:10:30,320
you're gonna see them everywhere and
16926
10:10:30,320 --> 10:10:33,040
everything is a class like balance div
16927
10:10:33,040 --> 10:10:35,200
this is a class that's why you're doing
16928
10:10:35,200 --> 10:10:37,436
dot inner text
16929
10:10:37,436 --> 10:10:40,400
documents is probably like of a you you
16930
10:10:40,400 --> 10:10:42,160
get this document object model which is
16931
10:10:42,160 --> 10:10:44,080
probably like this class or this object
16932
10:10:44,080 --> 10:10:46,480
and you can do dot get element by id let
16933
10:10:46,480 --> 10:10:48,800
me ask you what's dot get element by id
16934
10:10:48,800 --> 10:10:50,800
is it a property is it a method or is it
16935
10:10:50,800 --> 10:10:52,560
a function which one is it
16936
10:10:52,560 --> 10:10:54,000
you should be able to guess this in like
16937
10:10:54,000 --> 10:10:56,880
three seconds three two one
16938
10:10:56,880 --> 10:10:59,040
if you guess this is a method good job
16939
10:10:59,040 --> 10:11:00,720
this is a method right because it looks
16940
10:11:00,720 --> 10:11:02,640
like a function but with a dot
16941
10:11:02,640 --> 10:11:05,276
that's a method so this document is a
16942
10:11:05,276 --> 10:11:06,880
class and then we're doing this dot
16943
10:11:06,880 --> 10:11:09,520
whatever method
16944
10:11:09,520 --> 10:11:12,720
um so everything just everything has to
16945
10:11:12,720 --> 10:11:15,520
do with classes and objects and
16946
10:11:15,520 --> 10:11:17,360
all this stuff and so this is why this
16947
10:11:17,360 --> 10:11:20,000
is so important for you to learn and
16948
10:11:20,000 --> 10:11:23,120
you know this this keyword is so
16949
10:11:23,120 --> 10:11:24,320
important to learn because this will
16950
10:11:24,320 --> 10:11:26,000
come so many places it will be so
16951
10:11:26,000 --> 10:11:27,756
confused what it was and you'll be
16952
10:11:27,756 --> 10:11:29,840
thankful that you did so many
16953
10:11:29,840 --> 10:11:32,560
class-based exercises and will still do
16954
10:11:32,560 --> 10:11:35,200
more because
16955
10:11:35,200 --> 10:11:37,276
like just doing one example is not
16956
10:11:37,276 --> 10:11:39,520
enough you have to do so many different
16957
10:11:39,520 --> 10:11:41,436
examples so many different exercises so
16958
10:11:41,436 --> 10:11:43,360
many different projects and then the
16959
10:11:43,360 --> 10:11:45,360
more you do the more it starts clicking
16960
10:11:45,360 --> 10:11:46,880
and the more you start understanding so
16961
10:11:46,880 --> 10:11:49,040
pattern recognition and programming is
16962
10:11:49,040 --> 10:11:51,680
one of the most key and underrated and
16963
10:11:51,680 --> 10:11:53,360
understated things that you could do
16964
10:11:53,360 --> 10:11:55,276
that will just like skyrocket your skill
16965
10:11:55,276 --> 10:11:58,240
set okay like intelligence is one thing
16966
10:11:58,240 --> 10:12:00,320
yes but pattern recognition is even
16967
10:12:00,320 --> 10:12:02,240
beyond that when it comes to con you
16968
10:12:02,240 --> 10:12:05,360
know programming even things like chess
16969
10:12:05,360 --> 10:12:06,800
and then you can build on top of it and
16970
10:12:06,800 --> 10:12:08,080
take it further
16971
10:12:08,080 --> 10:12:09,756
now the last thing i'll do for you is
16972
10:12:09,756 --> 10:12:12,400
we'll take this bank's class we're going
16973
10:12:12,400 --> 10:12:14,240
to go to our
16974
10:12:14,240 --> 10:12:15,916
javascript course
16975
10:12:15,916 --> 10:12:17,840
and then inside of here inside of
16976
10:12:17,840 --> 10:12:19,756
classes i'm going to create a new folder
16977
10:12:19,756 --> 10:12:22,080
and we'll call it
16978
10:12:22,080 --> 10:12:23,436
banks
16979
10:12:23,436 --> 10:12:26,080
class okay
16980
10:12:26,080 --> 10:12:28,240
and then in here i'm going to create a
16981
10:12:28,240 --> 10:12:31,360
new file it'll just be called script.js
16982
10:12:31,360 --> 10:12:34,960
and this file will have the entire code
16983
10:12:34,960 --> 10:12:38,560
okay and then i'll grab the index.html
16984
10:12:38,560 --> 10:12:41,520
so you have that to refer to as well and
16985
10:12:41,520 --> 10:12:43,360
you can always see it
16986
10:12:43,360 --> 10:12:44,880
go back here
16987
10:12:44,880 --> 10:12:46,560
and we'll add in
16988
10:12:46,560 --> 10:12:50,720
whoops oh jesus what did we do
16989
10:12:51,680 --> 10:12:54,080
what did i do okay i moved banks out of
16990
10:12:54,080 --> 10:12:55,916
the banks class out of the classes okay
16991
10:12:55,916 --> 10:12:57,520
cool now we're good
16992
10:12:57,520 --> 10:13:00,320
let's add in a new file here and we'll
16993
10:13:00,320 --> 10:13:03,640
say index.html
16994
10:13:03,680 --> 10:13:06,480
and we'll paste that html right here
16995
10:13:06,480 --> 10:13:07,756
okay
16996
10:13:07,756 --> 10:13:09,436
all right so now what you'll be able to
16997
10:13:09,436 --> 10:13:10,880
do is you'll be able to go to the home
16998
10:13:10,880 --> 10:13:13,200
page right and right on the home page
16999
10:13:13,200 --> 10:13:14,400
right here
17000
10:13:14,400 --> 10:13:17,436
you'll see lessons and in lessons you'll
17001
10:13:17,436 --> 10:13:19,120
see classes
17002
10:13:19,120 --> 10:13:21,596
it'll say bank with deposit and withdraw
17003
10:13:21,596 --> 10:13:23,756
methods and so this lesson will be
17004
10:13:23,756 --> 10:13:26,000
available for you you can click it
17005
10:13:26,000 --> 10:13:28,640
and play around with it right here okay
17006
10:13:28,640 --> 10:13:30,720
and deposit so you'll have the fully
17007
10:13:30,720 --> 10:13:32,880
working app with the final solution okay
17008
10:13:32,880 --> 10:13:35,200
and then you'll be able to hit withdraw
17009
10:13:35,200 --> 10:13:38,160
and take out any of the monies right hit
17010
10:13:38,160 --> 10:13:41,120
withdraw let's do 300.
17011
10:13:41,120 --> 10:13:42,400
boom
17012
10:13:42,400 --> 10:13:44,720
okay
17013
10:13:45,040 --> 10:13:46,560
boom
17014
10:13:46,560 --> 10:13:47,916
99
17015
10:13:47,916 --> 10:13:50,080
okay you'll be able to withdraw whatever
17016
10:13:50,080 --> 10:13:52,720
you want deposit whatever you want so
17017
10:13:52,720 --> 10:13:54,880
this app is there for you now and then
17018
10:13:54,880 --> 10:13:56,240
if you want to look at the code
17019
10:13:56,240 --> 10:13:58,800
obviously the code is going to be inside
17020
10:13:58,800 --> 10:14:01,040
of folder called classes it's going to
17021
10:14:01,040 --> 10:14:03,520
have banks class in there and inside of
17022
10:14:03,520 --> 10:14:05,276
there you're going to have this code and
17023
10:14:05,276 --> 10:14:07,276
then the script file
17024
10:14:07,276 --> 10:14:09,360
is going to be the main one where the
17025
10:14:09,360 --> 10:14:11,596
entire javascript is sitting there
17026
10:14:11,596 --> 10:14:14,160
waiting for you okay and you can always
17027
10:14:14,160 --> 10:14:17,120
use this to refer to your notes
17028
10:14:17,120 --> 10:14:19,916
run the app make changes and do whatever
17029
10:14:19,916 --> 10:14:22,240
you want okay hopefully that was
17030
10:14:22,240 --> 10:14:24,160
insightful and gave you some more
17031
10:14:24,160 --> 10:14:27,680
insight into classes and we'll be still
17032
10:14:27,680 --> 10:14:29,360
doing more stuff so i hope you're
17033
10:14:29,360 --> 10:14:31,596
prepared and excited all right now that
17034
10:14:31,596 --> 10:14:34,080
you got your classes skills up to a
17035
10:14:34,080 --> 10:14:36,080
certain level and we worked on how to
17036
10:14:36,080 --> 10:14:38,640
make a bank we also worked on how to
17037
10:14:38,640 --> 10:14:40,480
make a car class
17038
10:14:40,480 --> 10:14:42,400
now what i want you to do is you're
17039
10:14:42,400 --> 10:14:44,000
going to be taking it up a notch and
17040
10:14:44,000 --> 10:14:45,756
you're going to be building a street
17041
10:14:45,756 --> 10:14:47,840
fighter game and this is going to be a
17042
10:14:47,840 --> 10:14:49,596
really fancy game it's going to have
17043
10:14:49,596 --> 10:14:50,880
sounds and it's going to have the
17044
10:14:50,880 --> 10:14:52,960
ability to attack and heal and you're
17045
10:14:52,960 --> 10:14:55,040
gonna have ken and ryu and two fighters
17046
10:14:55,040 --> 10:14:56,480
fighting you're gonna be able to create
17047
10:14:56,480 --> 10:14:59,040
a player class a game class and you'll
17048
10:14:59,040 --> 10:15:00,960
be able to play the game win the game
17049
10:15:00,960 --> 10:15:03,596
and you'll be able to hit q a to attack
17050
10:15:03,596 --> 10:15:05,520
and heal from player one and you'll be
17051
10:15:05,520 --> 10:15:07,200
able to hit p and l
17052
10:15:07,200 --> 10:15:09,756
to attack and heal with player two and
17053
10:15:09,756 --> 10:15:11,200
there's gonna be a whole lot of stuff
17054
10:15:11,200 --> 10:15:13,680
you can do with classes and this game is
17055
10:15:13,680 --> 10:15:15,916
gonna push your javascript javascript
17056
10:15:15,916 --> 10:15:17,520
skills to the next level now for this
17057
10:15:17,520 --> 10:15:20,000
game your instructor is gonna be lance
17058
10:15:20,000 --> 10:15:22,000
so i hope you're excited stay put he's
17059
10:15:22,000 --> 10:15:24,240
an awesome instructor i love him to
17060
10:15:24,240 --> 10:15:26,800
death you're gonna enjoy it and i'm
17061
10:15:26,800 --> 10:15:28,720
gonna send it over to lance to show you
17062
10:15:28,720 --> 10:15:31,520
how to make the fighting street fighter
17063
10:15:31,520 --> 10:15:34,480
game let's go lance what is up guys it's
17064
10:15:34,480 --> 10:15:35,916
lance here and like kazi said i'm going
17065
10:15:35,916 --> 10:15:38,160
to be taking over this specific section
17066
10:15:38,160 --> 10:15:39,916
of the javascript course i hope you guys
17067
10:15:39,916 --> 10:15:41,916
are ready you guys are crushing it so
17068
10:15:41,916 --> 10:15:43,916
far and as you can see we're on a
17069
10:15:43,916 --> 10:15:46,640
separate repel from the course right
17070
10:15:46,640 --> 10:15:48,080
these are the topics that i want to go
17071
10:15:48,080 --> 10:15:49,756
over so the first one we have up here is
17072
10:15:49,756 --> 10:15:51,360
something called while loops you may
17073
10:15:51,360 --> 10:15:53,120
have heard of this when kazi was
17074
10:15:53,120 --> 10:15:54,800
explaining loops the ones that i know
17075
10:15:54,800 --> 10:15:57,596
that you guys know are the for loops so
17076
10:15:57,596 --> 10:16:00,560
take a look so with four loops my key if
17077
10:16:00,560 --> 10:16:01,520
you want to know the difference between
17078
10:16:01,520 --> 10:16:04,160
these two is you use four loops right
17079
10:16:04,160 --> 10:16:04,960
when
17080
10:16:04,960 --> 10:16:05,680
you
17081
10:16:05,680 --> 10:16:08,480
know how many times
17082
10:16:08,480 --> 10:16:10,720
you want to loop right
17083
10:16:10,720 --> 10:16:12,560
and for a while loop
17084
10:16:12,560 --> 10:16:14,080
right they do the same thing they both
17085
10:16:14,080 --> 10:16:18,000
loop but you use this when you are
17086
10:16:18,000 --> 10:16:22,160
not sure how many times
17087
10:16:22,320 --> 10:16:25,040
you want to loop right think about when
17088
10:16:25,040 --> 10:16:27,120
we use a for loop i use a for loop to go
17089
10:16:27,120 --> 10:16:29,276
through an array and loop however long
17090
10:16:29,276 --> 10:16:31,120
that array is we know that right but the
17091
10:16:31,120 --> 10:16:34,480
while loop we want to keep looping until
17092
10:16:34,480 --> 10:16:35,840
a condition
17093
10:16:35,840 --> 10:16:38,160
is not true all right so let me show you
17094
10:16:38,160 --> 10:16:41,200
what it looks like enough talk all right
17095
10:16:41,200 --> 10:16:42,480
for a while loop all you have to do is
17096
10:16:42,480 --> 10:16:46,000
set up is just this right the key thing
17097
10:16:46,000 --> 10:16:48,640
that makes this engine run is this right
17098
10:16:48,640 --> 10:16:51,360
here this is called your condition right
17099
10:16:51,360 --> 10:16:53,756
whatever you put here is how long you're
17100
10:16:53,756 --> 10:16:54,880
going to loop to so let's set up an
17101
10:16:54,880 --> 10:16:58,000
example all right let's say let num
17102
10:16:58,000 --> 10:16:59,756
is equal to zero right if i were to
17103
10:16:59,756 --> 10:17:01,596
console log num it would just show me
17104
10:17:01,596 --> 10:17:02,640
it's zero
17105
10:17:02,640 --> 10:17:05,360
but what if i say keep looping
17106
10:17:05,360 --> 10:17:07,520
while num
17107
10:17:07,520 --> 10:17:09,360
is less than
17108
10:17:09,360 --> 10:17:10,320
five
17109
10:17:10,320 --> 10:17:11,276
right
17110
10:17:11,276 --> 10:17:12,560
so that's how you can read this it's
17111
10:17:12,560 --> 10:17:13,756
actually really easy to read with while
17112
10:17:13,756 --> 10:17:16,080
loops you can say while num is less than
17113
10:17:16,080 --> 10:17:18,240
five keep looping so if i ask you guys
17114
10:17:18,240 --> 10:17:20,240
right now right here
17115
10:17:20,240 --> 10:17:22,960
is num less than five yes
17116
10:17:22,960 --> 10:17:24,480
right so if i were to run this piece of
17117
10:17:24,480 --> 10:17:26,480
code it would actually run forever
17118
10:17:26,480 --> 10:17:29,436
because when will num ever equal five
17119
10:17:29,436 --> 10:17:30,560
see
17120
10:17:30,560 --> 10:17:32,400
there we go so then we can say something
17121
10:17:32,400 --> 10:17:34,800
like this right while num is less than
17122
10:17:34,800 --> 10:17:37,756
five you wanna say
17123
10:17:37,756 --> 10:17:39,840
consolag looping
17124
10:17:39,840 --> 10:17:41,840
right and then after we say console log
17125
10:17:41,840 --> 10:17:45,120
looping we can also do a num
17126
10:17:45,120 --> 10:17:46,320
plus plus
17127
10:17:46,320 --> 10:17:47,436
all right do you guys remember what plus
17128
10:17:47,436 --> 10:17:50,240
plus does plus plus will increment add
17129
10:17:50,240 --> 10:17:52,960
one to the value of num so the first
17130
10:17:52,960 --> 10:17:54,800
time it's zero right then the second
17131
10:17:54,800 --> 10:17:57,360
time it's one two right and so what we
17132
10:17:57,360 --> 10:17:59,120
could also do here is put a comma and
17133
10:17:59,120 --> 10:18:00,320
write num
17134
10:18:00,320 --> 10:18:02,000
so if i were to run this code what do
17135
10:18:02,000 --> 10:18:03,756
you think we're gonna see in the console
17136
10:18:03,756 --> 10:18:06,160
over here give you guys five seconds
17137
10:18:06,160 --> 10:18:07,200
one
17138
10:18:07,200 --> 10:18:09,120
two three
17139
10:18:09,120 --> 10:18:10,000
four
17140
10:18:10,000 --> 10:18:12,640
five did you alright let's hit run and
17141
10:18:12,640 --> 10:18:14,720
we can see looping zero looping one
17142
10:18:14,720 --> 10:18:16,640
looping three looping four so how many
17143
10:18:16,640 --> 10:18:19,840
times did this run this run five times
17144
10:18:19,840 --> 10:18:20,880
right
17145
10:18:20,880 --> 10:18:22,400
so now we can switch it up
17146
10:18:22,400 --> 10:18:24,080
all right let's say
17147
10:18:24,080 --> 10:18:26,160
let's say let
17148
10:18:26,160 --> 10:18:27,120
num
17149
10:18:27,120 --> 10:18:30,000
2 is equal to 10 right but we didn't
17150
10:18:30,000 --> 10:18:31,360
know let's pretend this is a hidden
17151
10:18:31,360 --> 10:18:33,040
number that we didn't know right as long
17152
10:18:33,040 --> 10:18:36,560
as num is less than num2
17153
10:18:36,560 --> 10:18:38,240
right keep looping what if we didn't
17154
10:18:38,240 --> 10:18:40,800
know what num2 was will it still stop
17155
10:18:40,800 --> 10:18:42,800
yeah because once it reaches 10 right
17156
10:18:42,800 --> 10:18:45,120
the condition is no longer true so let's
17157
10:18:45,120 --> 10:18:47,596
hit run there we go 0 through 9. so
17158
10:18:47,596 --> 10:18:49,840
that's kind of how the while loop works
17159
10:18:49,840 --> 10:18:52,960
while the condition is true keep
17160
10:18:52,960 --> 10:18:54,720
looping so here's a here's another
17161
10:18:54,720 --> 10:18:56,640
example not as useful all right you can
17162
10:18:56,640 --> 10:18:57,520
say
17163
10:18:57,520 --> 10:18:58,400
let
17164
10:18:58,400 --> 10:18:59,916
and this will be closer to the activity
17165
10:18:59,916 --> 10:19:02,840
that we're doing let
17166
10:19:02,840 --> 10:19:06,480
finished equals false i'll say equal is
17167
10:19:06,480 --> 10:19:08,000
true
17168
10:19:08,000 --> 10:19:09,596
right so
17169
10:19:09,596 --> 10:19:12,400
if i said in this while loop finished
17170
10:19:12,400 --> 10:19:15,040
is equal equal to true
17171
10:19:15,040 --> 10:19:17,360
right
17172
10:19:17,436 --> 10:19:19,040
how many times would this loop if i were
17173
10:19:19,040 --> 10:19:20,720
to run it take a guess
17174
10:19:20,720 --> 10:19:23,436
if you said it ran infinitely you'd be
17175
10:19:23,436 --> 10:19:25,040
correct why is it infinite though answer
17176
10:19:25,040 --> 10:19:26,800
my question what do you think that's
17177
10:19:26,800 --> 10:19:28,960
because we're saying while finish is
17178
10:19:28,960 --> 10:19:31,040
equal equally true keep looping but at
17179
10:19:31,040 --> 10:19:33,120
any point did finish change no but if i
17180
10:19:33,120 --> 10:19:35,040
were to say in this while loop finish is
17181
10:19:35,040 --> 10:19:36,880
now equal to false
17182
10:19:36,880 --> 10:19:37,916
right
17183
10:19:37,916 --> 10:19:40,000
then we could say
17184
10:19:40,000 --> 10:19:41,200
run
17185
10:19:41,200 --> 10:19:42,800
and we'll just see one looping right
17186
10:19:42,800 --> 10:19:44,400
because it's going to console.log switch
17187
10:19:44,400 --> 10:19:47,040
it to false and then exit the wild
17188
10:19:47,040 --> 10:19:48,320
there we go so we looked at the while
17189
10:19:48,320 --> 10:19:50,560
loop that's pretty much it for that so
17190
10:19:50,560 --> 10:19:52,960
the next thing we have is add event
17191
10:19:52,960 --> 10:19:54,400
listener right here and if we were going
17192
10:19:54,400 --> 10:19:56,160
to talk about ad event listeners you
17193
10:19:56,160 --> 10:19:58,480
guys need to remember what the dom is so
17194
10:19:58,480 --> 10:20:00,400
what is the dom well
17195
10:20:00,400 --> 10:20:01,756
the dom
17196
10:20:01,756 --> 10:20:02,640
right
17197
10:20:02,640 --> 10:20:04,800
dom stands for what
17198
10:20:04,800 --> 10:20:06,240
document
17199
10:20:06,240 --> 10:20:07,840
object
17200
10:20:07,840 --> 10:20:12,000
model or model right basically the html
17201
10:20:12,000 --> 10:20:15,520
itself right so here doctype html and we
17202
10:20:15,520 --> 10:20:18,800
can select with javascript any of these
17203
10:20:18,800 --> 10:20:20,720
elements here so as you can see we have
17204
10:20:20,720 --> 10:20:23,436
our basic hello world text up there how
17205
10:20:23,436 --> 10:20:24,880
would i grab
17206
10:20:24,880 --> 10:20:26,400
this div
17207
10:20:26,400 --> 10:20:27,916
right with javascript all right can you
17208
10:20:27,916 --> 10:20:30,080
guys see it you guys remember
17209
10:20:30,080 --> 10:20:31,756
so give me five seconds
17210
10:20:31,756 --> 10:20:32,640
one
17211
10:20:32,640 --> 10:20:33,596
two
17212
10:20:33,596 --> 10:20:34,480
three
17213
10:20:34,480 --> 10:20:35,436
four
17214
10:20:35,436 --> 10:20:37,756
five awesome so
17215
10:20:37,756 --> 10:20:39,276
if you notice there's a little id here
17216
10:20:39,276 --> 10:20:41,276
that says play alright so what we could
17217
10:20:41,276 --> 10:20:43,840
do right is set up a variable you can
17218
10:20:43,840 --> 10:20:44,800
say
17219
10:20:44,800 --> 10:20:46,000
let
17220
10:20:46,000 --> 10:20:48,400
play is equal to and then how do you
17221
10:20:48,400 --> 10:20:51,360
grab uh an id from the dom well i can
17222
10:20:51,360 --> 10:20:54,560
say document dot get element by id
17223
10:20:54,560 --> 10:20:56,960
and just over here just say
17224
10:20:56,960 --> 10:20:59,840
play all right and if i were to console
17225
10:20:59,840 --> 10:21:00,880
log
17226
10:21:00,880 --> 10:21:02,960
play what are we going to see
17227
10:21:02,960 --> 10:21:04,640
exactly html
17228
10:21:04,640 --> 10:21:07,276
div element right if i wanted to get the
17229
10:21:07,276 --> 10:21:08,880
inner text
17230
10:21:08,880 --> 10:21:10,640
right
17231
10:21:10,640 --> 10:21:12,240
could do something like this
17232
10:21:12,240 --> 10:21:14,560
enter text hello world all right that's
17233
10:21:14,560 --> 10:21:16,400
what it says inside that div cool and
17234
10:21:16,400 --> 10:21:18,320
this is how we use the dom to get what
17235
10:21:18,320 --> 10:21:21,120
we want from our html right so
17236
10:21:21,120 --> 10:21:23,680
what if i wanted to add an on click to
17237
10:21:23,680 --> 10:21:25,040
this what did it cause it teach you you
17238
10:21:25,040 --> 10:21:27,436
guys remember and for simplifying and
17239
10:21:27,436 --> 10:21:29,120
for simplifying purposes we're actually
17240
10:21:29,120 --> 10:21:30,880
going to call this play div so you can
17241
10:21:30,880 --> 10:21:32,080
make a difference
17242
10:21:32,080 --> 10:21:34,240
okay cool so now
17243
10:21:34,240 --> 10:21:37,360
our variable that's holding our dom
17244
10:21:37,360 --> 10:21:38,880
element
17245
10:21:38,880 --> 10:21:42,240
called play is hello world so how do we
17246
10:21:42,240 --> 10:21:43,916
add an event listener the way that qazi
17247
10:21:43,916 --> 10:21:45,756
taught you right play div would be
17248
10:21:45,756 --> 10:21:47,680
something like this dot
17249
10:21:47,680 --> 10:21:50,480
on click right every element has an on
17250
10:21:50,480 --> 10:21:52,880
click attribute that we can access here
17251
10:21:52,880 --> 10:21:56,240
and we can say onclick is equal to
17252
10:21:56,240 --> 10:21:58,960
an anonymous function which just means
17253
10:21:58,960 --> 10:22:00,720
you know just means a function without a
17254
10:22:00,720 --> 10:22:04,160
name so if i say when i click this div
17255
10:22:04,160 --> 10:22:06,480
on the when i click on click of this div
17256
10:22:06,480 --> 10:22:09,120
we'll console.log
17257
10:22:09,120 --> 10:22:12,960
yoyo right so let's hit run
17258
10:22:12,960 --> 10:22:16,080
let's click hello world we should see
17259
10:22:16,080 --> 10:22:17,120
yoyo
17260
10:22:17,120 --> 10:22:19,276
oh i see it's because i did capital c
17261
10:22:19,276 --> 10:22:21,200
just to be lower case c
17262
10:22:21,200 --> 10:22:22,640
let's see now
17263
10:22:22,640 --> 10:22:23,596
run
17264
10:22:23,596 --> 10:22:25,120
yo yo yo
17265
10:22:25,120 --> 10:22:27,596
yo yo you got it cool so that's pretty
17266
10:22:27,596 --> 10:22:29,596
simple that's perfectly fine and i'm
17267
10:22:29,596 --> 10:22:31,360
just going to show you guys an alternate
17268
10:22:31,360 --> 10:22:34,080
way to do the same exact thing right so
17269
10:22:34,080 --> 10:22:36,400
this is the on click way right but
17270
10:22:36,400 --> 10:22:37,680
there's also
17271
10:22:37,680 --> 10:22:40,400
something called add event listener so
17272
10:22:40,400 --> 10:22:41,520
play div
17273
10:22:41,520 --> 10:22:45,200
so you could do is say play div dot
17274
10:22:45,200 --> 10:22:47,520
add event listener
17275
10:22:47,520 --> 10:22:50,480
right and then you can say
17276
10:22:50,480 --> 10:22:52,000
two things right so let's say add event
17277
10:22:52,000 --> 10:22:53,840
listener and you notice these
17278
10:22:53,840 --> 10:22:56,560
parentheses down here let me zoom in
17279
10:22:56,560 --> 10:22:58,560
cool these parentheses
17280
10:22:58,560 --> 10:23:01,520
comes from the add eventlistener method
17281
10:23:01,520 --> 10:23:04,560
so this is a method right
17282
10:23:04,560 --> 10:23:06,400
and it takes in two arguments what are
17283
10:23:06,400 --> 10:23:08,800
the arguments it's one
17284
10:23:08,800 --> 10:23:09,596
what
17285
10:23:09,596 --> 10:23:11,436
are you listening for here
17286
10:23:11,436 --> 10:23:13,276
well i'm listening for a click
17287
10:23:13,276 --> 10:23:14,720
then you can put a comma and then you're
17288
10:23:14,720 --> 10:23:15,680
going to put
17289
10:23:15,680 --> 10:23:16,400
a
17290
10:23:16,400 --> 10:23:17,360
function
17291
10:23:17,360 --> 10:23:18,960
like so
17292
10:23:18,960 --> 10:23:19,756
okay
17293
10:23:19,756 --> 10:23:21,520
so you do this es5 where you can say
17294
10:23:21,520 --> 10:23:23,756
function
17295
10:23:23,756 --> 10:23:25,040
and you basically it's what are you
17296
10:23:25,040 --> 10:23:26,320
listening for
17297
10:23:26,320 --> 10:23:29,680
and what do you want to do
17298
10:23:29,680 --> 10:23:31,040
when you click
17299
10:23:31,040 --> 10:23:33,040
all right so this time i want to say
17300
10:23:33,040 --> 10:23:35,916
console log
17301
10:23:36,240 --> 10:23:37,436
hey yo
17302
10:23:37,436 --> 10:23:39,596
or hey yo poo apparently all right so if
17303
10:23:39,596 --> 10:23:42,080
i hit run here and i click on this hey
17304
10:23:42,080 --> 10:23:43,120
yo
17305
10:23:43,120 --> 10:23:45,360
right so how is that different than this
17306
10:23:45,360 --> 10:23:48,240
it's just a longer way to add events
17307
10:23:48,240 --> 10:23:50,000
right the
17308
10:23:50,000 --> 10:23:52,000
like it's a long way than on click but
17309
10:23:52,000 --> 10:23:53,756
the benefit would be
17310
10:23:53,756 --> 10:23:56,880
instead of running a click i can write
17311
10:23:56,880 --> 10:23:58,960
other ones like key down key up you just
17312
10:23:58,960 --> 10:24:01,200
have to change what the event is
17313
10:24:01,200 --> 10:24:02,560
listening for
17314
10:24:02,560 --> 10:24:04,480
cool right so that's just another thing
17315
10:24:04,480 --> 10:24:05,916
a little side thing that i want to teach
17316
10:24:05,916 --> 10:24:07,916
you guys the main reason i wanted to
17317
10:24:07,916 --> 10:24:09,680
talk about event listeners is because we
17318
10:24:09,680 --> 10:24:12,320
can do something like this all right we
17319
10:24:12,320 --> 10:24:13,200
can say
17320
10:24:13,200 --> 10:24:14,720
document
17321
10:24:14,720 --> 10:24:17,360
dot add event listing right so what i
17322
10:24:17,360 --> 10:24:19,436
what am i adding an event listener to if
17323
10:24:19,436 --> 10:24:20,960
i ask you guys that question
17324
10:24:20,960 --> 10:24:22,480
okay take a look
17325
10:24:22,480 --> 10:24:24,160
so this one i added event listener to
17326
10:24:24,160 --> 10:24:26,400
the play div but this one i'm adding an
17327
10:24:26,400 --> 10:24:28,080
event lesson to
17328
10:24:28,080 --> 10:24:30,080
the document right so the document is
17329
10:24:30,080 --> 10:24:33,040
essentially the dom or the whole uh page
17330
10:24:33,040 --> 10:24:35,916
itself right so basically
17331
10:24:35,916 --> 10:24:37,840
if i say
17332
10:24:37,840 --> 10:24:40,560
right something like this
17333
10:24:40,560 --> 10:24:42,480
and write key down that is an event that
17334
10:24:42,480 --> 10:24:43,840
we can listen for
17335
10:24:43,840 --> 10:24:47,276
and then here i can say function
17336
10:24:47,276 --> 10:24:49,040
right
17337
10:24:49,040 --> 10:24:50,320
e
17338
10:24:50,320 --> 10:24:51,200
right
17339
10:24:51,200 --> 10:24:54,596
and then we'll console.log
17340
10:24:55,916 --> 10:24:57,040
hi
17341
10:24:57,040 --> 10:24:58,880
okay cool so
17342
10:24:58,880 --> 10:25:00,400
let's take a look and analyze this for a
17343
10:25:00,400 --> 10:25:02,160
second i'm saying document.add event
17344
10:25:02,160 --> 10:25:04,080
listener i'm saying listen for a key
17345
10:25:04,080 --> 10:25:06,080
down which is me pressing down on the
17346
10:25:06,080 --> 10:25:07,916
keyboard and then this function
17347
10:25:07,916 --> 10:25:09,520
represents what i want to do when i
17348
10:25:09,520 --> 10:25:11,840
press key down right i want to say
17349
10:25:11,840 --> 10:25:14,720
console.log high so let's hit run
17350
10:25:14,720 --> 10:25:16,480
and if i were to press let's say the
17351
10:25:16,480 --> 10:25:18,320
queue on my keyboard what's going to
17352
10:25:18,320 --> 10:25:19,276
happen
17353
10:25:19,276 --> 10:25:20,880
oh it's going to put q if i click that
17354
10:25:20,880 --> 10:25:22,640
let's click the app itself
17355
10:25:22,640 --> 10:25:25,120
hi all right i'm just going to click l
17356
10:25:25,120 --> 10:25:25,840
hi
17357
10:25:25,840 --> 10:25:27,520
i'm going to hit space bar
17358
10:25:27,520 --> 10:25:30,640
hi right whatever key i press down
17359
10:25:30,640 --> 10:25:32,720
it's going to run this function so
17360
10:25:32,720 --> 10:25:34,720
that's how event listeners work why is
17361
10:25:34,720 --> 10:25:36,240
this useful answer why am i telling you
17362
10:25:36,240 --> 10:25:38,320
well the main thing is e right here you
17363
10:25:38,320 --> 10:25:41,120
don't need to put e here but e
17364
10:25:41,120 --> 10:25:42,720
is essentially what's going to represent
17365
10:25:42,720 --> 10:25:45,596
our event or keyboard event right
17366
10:25:45,596 --> 10:25:47,040
so
17367
10:25:47,040 --> 10:25:49,680
if i say something like this if
17368
10:25:49,680 --> 10:25:52,800
e dot key right because each e has a key
17369
10:25:52,800 --> 10:25:54,480
attribute that we can call
17370
10:25:54,480 --> 10:25:58,000
e dot key equals
17371
10:25:58,000 --> 10:25:59,360
q
17372
10:25:59,360 --> 10:26:00,160
right
17373
10:26:00,160 --> 10:26:03,276
then i want to say console of
17374
10:26:03,276 --> 10:26:04,400
you
17375
10:26:04,400 --> 10:26:05,596
pressed
17376
10:26:05,596 --> 10:26:07,680
q
17377
10:26:07,680 --> 10:26:09,040
else
17378
10:26:09,040 --> 10:26:12,320
right i'll say console log
17379
10:26:12,320 --> 10:26:13,680
not q
17380
10:26:13,680 --> 10:26:16,400
okay cool so let's hit run again
17381
10:26:16,400 --> 10:26:18,240
if i were to press the spacebar once
17382
10:26:18,240 --> 10:26:21,200
again oops not there on the app
17383
10:26:21,200 --> 10:26:23,200
not q obviously it's not cute but if i
17384
10:26:23,200 --> 10:26:24,320
hit p
17385
10:26:24,320 --> 10:26:27,276
knock you what if i hit q
17386
10:26:27,276 --> 10:26:30,720
q so if you ever wanted to make your app
17387
10:26:30,720 --> 10:26:33,276
respond to your key presses we can use
17388
10:26:33,276 --> 10:26:35,680
document.ad event listener like how sick
17389
10:26:35,680 --> 10:26:37,360
is that right this is going to come into
17390
10:26:37,360 --> 10:26:40,160
play when me and you are going to make a
17391
10:26:40,160 --> 10:26:41,436
fighting game together where we can
17392
10:26:41,436 --> 10:26:43,916
press q do a nice punch we can press a
17393
10:26:43,916 --> 10:26:46,160
we can heal up so i'm getting excited
17394
10:26:46,160 --> 10:26:49,160
right but we're almost there that is
17395
10:26:49,160 --> 10:26:51,276
document.ad event listener right and you
17396
10:26:51,276 --> 10:26:53,360
can set conditions within that function
17397
10:26:53,360 --> 10:26:55,436
too so i like this because it kind of
17398
10:26:55,436 --> 10:26:56,640
like ties
17399
10:26:56,640 --> 10:26:59,200
everything together so
17400
10:26:59,200 --> 10:27:00,720
awesome so i hope you guys enjoyed that
17401
10:27:00,720 --> 10:27:02,320
one the next thing i want to talk about
17402
10:27:02,320 --> 10:27:05,040
is audio tags so let me get a second to
17403
10:27:05,040 --> 10:27:06,720
set up some sounds here for you guys so
17404
10:27:06,720 --> 10:27:08,640
that we can mess around with it okay so
17405
10:27:08,640 --> 10:27:10,240
i took a moment to grab some sounds for
17406
10:27:10,240 --> 10:27:13,040
you guys oops not packages sounds as you
17407
10:27:13,040 --> 10:27:15,276
can see we got fast heel quick heel
17408
10:27:15,276 --> 10:27:17,360
quick hit victory fast these are gonna
17409
10:27:17,360 --> 10:27:18,400
these are things that we're going to be
17410
10:27:18,400 --> 10:27:20,400
using for the fighting game activity
17411
10:27:20,400 --> 10:27:21,520
later on
17412
10:27:21,520 --> 10:27:23,756
but if you wanted to learn how to add
17413
10:27:23,756 --> 10:27:26,000
sounds to your javascript project we're
17414
10:27:26,000 --> 10:27:28,240
going to be using something called the
17415
10:27:28,240 --> 10:27:32,000
audio tag with our html so we're going
17416
10:27:32,000 --> 10:27:34,160
to need to go to our html for this one
17417
10:27:34,160 --> 10:27:37,200
and down below here we can go right
17418
10:27:37,200 --> 10:27:39,436
before the body or actually we can do it
17419
10:27:39,436 --> 10:27:41,596
outside of the body even and we can say
17420
10:27:41,596 --> 10:27:43,436
audio
17421
10:27:43,436 --> 10:27:44,640
like this
17422
10:27:44,640 --> 10:27:46,000
okay
17423
10:27:46,000 --> 10:27:48,720
all we have to do it's similar to
17424
10:27:48,720 --> 10:27:52,480
the script tags or your image tags right
17425
10:27:52,480 --> 10:27:53,916
we're going to add a source sorry we're
17426
10:27:53,916 --> 10:27:55,360
going to say source
17427
10:27:55,360 --> 10:27:57,840
is equal to sounds
17428
10:27:57,840 --> 10:27:59,520
all right and then we can just pick one
17429
10:27:59,520 --> 10:28:02,400
so let's do quick hit all right we can
17430
10:28:02,400 --> 10:28:04,960
say quick
17431
10:28:05,040 --> 10:28:06,560
quick
17432
10:28:06,560 --> 10:28:08,960
hit
17433
10:28:09,520 --> 10:28:10,640
dot
17434
10:28:10,640 --> 10:28:12,160
left
17435
10:28:12,160 --> 10:28:15,360
or actually mp3 doesn't really matter
17436
10:28:15,360 --> 10:28:16,320
cool
17437
10:28:16,320 --> 10:28:18,240
so that's the source and after that we
17438
10:28:18,240 --> 10:28:21,436
can had something called controls so i'm
17439
10:28:21,436 --> 10:28:25,240
just going to format it real quick
17440
10:28:26,640 --> 10:28:30,480
so we have source we can also do
17441
10:28:31,436 --> 10:28:33,916
style
17442
10:28:34,640 --> 10:28:36,080
yeah so for the style i just want to say
17443
10:28:36,080 --> 10:28:37,596
display none because i don't want to see
17444
10:28:37,596 --> 10:28:40,160
this audio tag
17445
10:28:40,160 --> 10:28:42,160
and then for
17446
10:28:42,160 --> 10:28:44,480
type
17447
10:28:44,480 --> 10:28:47,436
let's do an audio
17448
10:28:47,436 --> 10:28:49,756
mpeg
17449
10:28:49,756 --> 10:28:50,720
awesome
17450
10:28:50,720 --> 10:28:52,960
okay and then after that we can also
17451
10:28:52,960 --> 10:28:55,120
give this an id
17452
10:28:55,120 --> 10:28:56,720
so i'll just call this punch because
17453
10:28:56,720 --> 10:28:57,916
it's going to use that punch sound
17454
10:28:57,916 --> 10:29:00,000
effect and now that i'm looking at it
17455
10:29:00,000 --> 10:29:02,240
quick hit that web
17456
10:29:02,240 --> 10:29:06,080
is what should be here wav not mp3 so
17457
10:29:06,080 --> 10:29:08,240
make sure it's the same as the source
17458
10:29:08,240 --> 10:29:10,080
we'll hit run really quickly all right
17459
10:29:10,080 --> 10:29:11,916
don't see the auto tag good
17460
10:29:11,916 --> 10:29:13,916
and next what we want to do is head over
17461
10:29:13,916 --> 10:29:15,680
to your script.js
17462
10:29:15,680 --> 10:29:17,200
and let's think about it right so we
17463
10:29:17,200 --> 10:29:18,720
have this quick hit sound and kind of
17464
10:29:18,720 --> 10:29:21,276
goes like poo poo right but when do you
17465
10:29:21,276 --> 10:29:23,040
want to hear that sound right so that's
17466
10:29:23,040 --> 10:29:25,040
where the programming starts to come
17467
10:29:25,040 --> 10:29:27,436
into play i want to hear that sound
17468
10:29:27,436 --> 10:29:29,200
when i press
17469
10:29:29,200 --> 10:29:30,400
q
17470
10:29:30,400 --> 10:29:33,120
right so if i press q
17471
10:29:33,120 --> 10:29:35,120
after our console log
17472
10:29:35,120 --> 10:29:37,520
i want to play the sound so how can we
17473
10:29:37,520 --> 10:29:39,276
play the sound remember we gave this an
17474
10:29:39,276 --> 10:29:41,520
id right so if we give us an id how do
17475
10:29:41,520 --> 10:29:43,040
we grab
17476
10:29:43,040 --> 10:29:45,276
this element ah
17477
10:29:45,276 --> 10:29:47,436
now it's coming together so
17478
10:29:47,436 --> 10:29:49,756
if i want to grab an element you can say
17479
10:29:49,756 --> 10:29:51,040
document
17480
10:29:51,040 --> 10:29:53,520
dot get element by id
17481
10:29:53,520 --> 10:29:56,000
and just put in the name of the id which
17482
10:29:56,000 --> 10:29:58,000
i believe is just punch all right so we
17483
10:29:58,000 --> 10:30:02,320
got the element now but now we grabbed
17484
10:30:02,320 --> 10:30:04,320
this audio tag
17485
10:30:04,320 --> 10:30:06,480
how do i use it
17486
10:30:06,480 --> 10:30:07,680
well
17487
10:30:07,680 --> 10:30:09,916
to play audio we can just do this method
17488
10:30:09,916 --> 10:30:11,756
it's just called dot play
17489
10:30:11,756 --> 10:30:13,436
and if i hit run again
17490
10:30:13,436 --> 10:30:15,040
i press p
17491
10:30:15,040 --> 10:30:17,596
well not there i press p
17492
10:30:17,596 --> 10:30:20,000
not q right but if i press q
17493
10:30:20,000 --> 10:30:22,560
you heard that
17494
10:30:22,960 --> 10:30:24,720
all right it's going to play as long as
17495
10:30:24,720 --> 10:30:27,360
you press q
17496
10:30:27,756 --> 10:30:29,360
and a little thump sound that's the
17497
10:30:29,360 --> 10:30:30,960
little quick hit all right that's the
17498
10:30:30,960 --> 10:30:32,640
one i'm going to use for the project
17499
10:30:32,640 --> 10:30:34,560
itself so
17500
10:30:34,560 --> 10:30:37,360
you can set up conditions based on the
17501
10:30:37,360 --> 10:30:39,756
key that you key down that you press and
17502
10:30:39,756 --> 10:30:42,160
based on that you can play a punch or a
17503
10:30:42,160 --> 10:30:43,916
sound right so that's going to unlock a
17504
10:30:43,916 --> 10:30:45,840
world of possibilities for you guys we
17505
10:30:45,840 --> 10:30:47,916
could also set it so
17506
10:30:47,916 --> 10:30:49,680
let's say you want to add another sound
17507
10:30:49,680 --> 10:30:52,000
let's say if i don't press q i want to
17508
10:30:52,000 --> 10:30:54,080
hear something else so if you want to
17509
10:30:54,080 --> 10:30:56,840
add another sound i'll do is copy all
17510
10:30:56,840 --> 10:30:58,960
this right
17511
10:30:58,960 --> 10:31:01,200
place in that audio tag once again
17512
10:31:01,200 --> 10:31:02,080
help
17513
10:31:02,080 --> 10:31:05,040
don't forget to close it
17514
10:31:06,480 --> 10:31:08,240
there we go so that's the close this is
17515
10:31:08,240 --> 10:31:10,720
the closing audio tag for this one this
17516
10:31:10,720 --> 10:31:12,480
is the closing audio tag for this one
17517
10:31:12,480 --> 10:31:14,800
for this one make sure that closes but
17518
10:31:14,800 --> 10:31:17,040
instead we're going to say
17519
10:31:17,040 --> 10:31:20,320
victory instead of quick hit it is
17520
10:31:20,320 --> 10:31:25,120
victory and this is not wav this is mp3
17521
10:31:25,120 --> 10:31:27,120
so we got mp3 on the line let's hit
17522
10:31:27,120 --> 10:31:29,520
script js and when do i want that
17523
10:31:29,520 --> 10:31:32,000
victory sound to play it's when
17524
10:31:32,000 --> 10:31:33,916
i don't hit q so let's say
17525
10:31:33,916 --> 10:31:35,680
document.element
17526
10:31:35,680 --> 10:31:36,880
by id
17527
10:31:36,880 --> 10:31:39,200
and put in victory
17528
10:31:39,200 --> 10:31:40,320
vic
17529
10:31:40,320 --> 10:31:41,276
tori
17530
10:31:41,276 --> 10:31:44,480
and we'll hit dot play
17531
10:31:44,480 --> 10:31:46,320
this should work and let me just double
17532
10:31:46,320 --> 10:31:47,596
check that's what i called it yep
17533
10:31:47,596 --> 10:31:48,720
victory
17534
10:31:48,720 --> 10:31:51,200
awesome so let's see if this works
17535
10:31:51,200 --> 10:31:53,520
run i press q to punch you
17536
10:31:53,520 --> 10:31:55,120
you heard that and you hit p because
17537
10:31:55,120 --> 10:31:57,680
that's gonna be something else
17538
10:31:57,680 --> 10:31:59,520
hey so that's gonna be the victory music
17539
10:31:59,520 --> 10:32:00,800
when you win in the fighting game as
17540
10:32:00,800 --> 10:32:04,640
well but as long as i hit not q
17541
10:32:04,640 --> 10:32:05,596
i'll hit
17542
10:32:05,596 --> 10:32:07,040
i'll play that victory sound so if i hit
17543
10:32:07,040 --> 10:32:09,360
spacebar
17544
10:32:09,360 --> 10:32:10,800
all right i hope you guys can hear that
17545
10:32:10,800 --> 10:32:12,480
but this is super cool when i found out
17546
10:32:12,480 --> 10:32:14,960
about javascript sounds i was like yo we
17547
10:32:14,960 --> 10:32:17,436
could do a lot of different things now
17548
10:32:17,436 --> 10:32:18,720
right
17549
10:32:18,720 --> 10:32:21,276
cool so that is audio tag so i didn't
17550
10:32:21,276 --> 10:32:23,200
really write anything here but we are
17551
10:32:23,200 --> 10:32:25,916
using them mostly in our html
17552
10:32:25,916 --> 10:32:28,160
calling them with our javascript and
17553
10:32:28,160 --> 10:32:29,680
timing when do you want to hear that
17554
10:32:29,680 --> 10:32:32,000
sound so that's the very basics of all
17555
10:32:32,000 --> 10:32:33,520
of these things
17556
10:32:33,520 --> 10:32:35,840
we need all these three concepts plus
17557
10:32:35,840 --> 10:32:37,360
everything you learned previously to
17558
10:32:37,360 --> 10:32:39,520
make this fighting game app so
17559
10:32:39,520 --> 10:32:40,880
without further ado let's move on to the
17560
10:32:40,880 --> 10:32:43,200
next part alright i think you guys are
17561
10:32:43,200 --> 10:32:45,276
ready we can finally getting started on
17562
10:32:45,276 --> 10:32:47,916
your next project the fighting game i'm
17563
10:32:47,916 --> 10:32:49,276
super excited about this because there's
17564
10:32:49,276 --> 10:32:50,960
a lot that goes into this this is
17565
10:32:50,960 --> 10:32:52,640
definitely going to be a lot harder for
17566
10:32:52,640 --> 10:32:54,640
a project than the previous ones but i
17567
10:32:54,640 --> 10:32:56,080
think you guys are ready all right it's
17568
10:32:56,080 --> 10:32:57,436
going to go over all the topics that
17569
10:32:57,436 --> 10:33:00,480
we've done before especially focusing on
17570
10:33:00,480 --> 10:33:03,520
classes as you can see here so
17571
10:33:03,520 --> 10:33:05,436
before i explain what's going on i want
17572
10:33:05,436 --> 10:33:07,040
to demo the app a little bit as you can
17573
10:33:07,040 --> 10:33:07,840
see
17574
10:33:07,840 --> 10:33:10,160
this is our very minimal front end with
17575
10:33:10,160 --> 10:33:12,960
two players lance for player one kazi
17576
10:33:12,960 --> 10:33:15,200
for player two right it's pretty obvious
17577
10:33:15,200 --> 10:33:17,360
how this works if i were to press q
17578
10:33:17,360 --> 10:33:19,520
lands attacks if kanzi presses q i mean
17579
10:33:19,520 --> 10:33:21,360
p he attacks as well alright so let's
17580
10:33:21,360 --> 10:33:23,436
see what happens if i press q right now
17581
10:33:23,436 --> 10:33:24,800
oops not there
17582
10:33:24,800 --> 10:33:27,276
on the click on the app thank you
17583
10:33:27,276 --> 10:33:28,480
there you go you hear that little sound
17584
10:33:28,480 --> 10:33:29,916
effect
17585
10:33:29,916 --> 10:33:32,240
and it hits and does damage to qazi and
17586
10:33:32,240 --> 10:33:34,960
it updates the dom live which is sick
17587
10:33:34,960 --> 10:33:37,200
all right what if kazi replies with by
17588
10:33:37,200 --> 10:33:38,640
pressing p
17589
10:33:38,640 --> 10:33:40,080
there you go i did a little bit more
17590
10:33:40,080 --> 10:33:42,000
damage than him but we'll see we'll see
17591
10:33:42,000 --> 10:33:43,596
all right let's say kazi wants to heal
17592
10:33:43,596 --> 10:33:44,880
up
17593
10:33:44,880 --> 10:33:47,680
heard that there we go i can heal up as
17594
10:33:47,680 --> 10:33:48,480
well
17595
10:33:48,480 --> 10:33:49,596
and that's pretty much the game we can
17596
10:33:49,596 --> 10:33:51,276
kind of just go at each other one person
17597
10:33:51,276 --> 10:33:53,040
on the right side keyboard one person on
17598
10:33:53,040 --> 10:33:54,400
the left or you can just play by
17599
10:33:54,400 --> 10:33:56,030
yourself like this
17600
10:33:56,030 --> 10:33:58,720
[Music]
17601
10:33:58,720 --> 10:34:00,880
hey so it looks like i win this round as
17602
10:34:00,880 --> 10:34:02,400
you can see lance wins right here and if
17603
10:34:02,400 --> 10:34:04,560
you wanted to reset just click this nice
17604
10:34:04,560 --> 10:34:07,040
button over here boom so we're back to
17605
10:34:07,040 --> 10:34:08,900
100 health we could go again
17606
10:34:08,900 --> 10:34:11,840
[Music]
17607
10:34:11,840 --> 10:34:14,080
now cozzy wins this time so that's
17608
10:34:14,080 --> 10:34:15,916
pretty much the app all right it looks
17609
10:34:15,916 --> 10:34:18,400
like pretty simple but there's a lot of
17610
10:34:18,400 --> 10:34:19,916
things we actually need to keep in mind
17611
10:34:19,916 --> 10:34:22,320
so i hope you guys are ready right as an
17612
10:34:22,320 --> 10:34:24,560
added feature we can hit the simulate
17613
10:34:24,560 --> 10:34:26,000
button
17614
10:34:26,000 --> 10:34:27,276
and look at that kazi wins the
17615
10:34:27,276 --> 10:34:30,080
simulation right it just basically runs
17616
10:34:30,080 --> 10:34:32,240
through all the commands and once
17617
10:34:32,240 --> 10:34:34,480
there's a winner it'll get print out who
17618
10:34:34,480 --> 10:34:35,596
wins
17619
10:34:35,596 --> 10:34:38,000
so that's pretty much the demo
17620
10:34:38,000 --> 10:34:40,160
now it's time to get through the
17621
10:34:40,160 --> 10:34:42,480
exercise you guys ready awesome
17622
10:34:42,480 --> 10:34:44,720
so to get to the exercise when you hit
17623
10:34:44,720 --> 10:34:46,800
run the javascript course you just have
17624
10:34:46,800 --> 10:34:48,720
to click fighting game right here the
17625
10:34:48,720 --> 10:34:50,080
exercise
17626
10:34:50,080 --> 10:34:51,916
and you should see the same layout if i
17627
10:34:51,916 --> 10:34:53,756
were to press q or anything like that
17628
10:34:53,756 --> 10:34:55,120
it's not going to work it'd simulate
17629
10:34:55,120 --> 10:34:57,120
nothing all right we're going to add
17630
10:34:57,120 --> 10:35:00,880
those features ourselves and if i go to
17631
10:35:00,880 --> 10:35:02,640
this rupple here
17632
10:35:02,640 --> 10:35:04,560
right this is the same thing this is
17633
10:35:04,560 --> 10:35:06,960
basically the exercise just
17634
10:35:06,960 --> 10:35:08,720
ported onto an independent ripple for
17635
10:35:08,720 --> 10:35:10,960
your guys's testing purposes so
17636
10:35:10,960 --> 10:35:12,640
i'm going to link it to you guys but hop
17637
10:35:12,640 --> 10:35:14,720
on a separate rebel to get started on
17638
10:35:14,720 --> 10:35:17,120
the exercise i'm going to be on here
17639
10:35:17,120 --> 10:35:18,880
explaining to you guys how it works you
17640
10:35:18,880 --> 10:35:21,120
ready let's go alright so you guys
17641
10:35:21,120 --> 10:35:23,360
finally made it here to the fighting
17642
10:35:23,360 --> 10:35:25,916
game app and now i wanted to explain to
17643
10:35:25,916 --> 10:35:27,840
you guys how it works give you guys a
17644
10:35:27,840 --> 10:35:29,360
little bit of a tour just so you
17645
10:35:29,360 --> 10:35:30,720
understand what the hell am i going to
17646
10:35:30,720 --> 10:35:33,436
do to make this work so we saw a couple
17647
10:35:33,436 --> 10:35:35,360
things right you saw me press q to
17648
10:35:35,360 --> 10:35:37,916
attack right you saw me press reset
17649
10:35:37,916 --> 10:35:39,276
right you could probably think in your
17650
10:35:39,276 --> 10:35:41,200
head how you're probably going to do
17651
10:35:41,200 --> 10:35:44,320
this but luckily for you in the script
17652
10:35:44,320 --> 10:35:45,276
js
17653
10:35:45,276 --> 10:35:47,596
right you'll see all the instructions so
17654
10:35:47,596 --> 10:35:49,840
i walk you guys through don't worry
17655
10:35:49,840 --> 10:35:52,480
so again make sure you fork this
17656
10:35:52,480 --> 10:35:55,596
specific rebel fighting game project
17657
10:35:55,596 --> 10:35:57,360
right so that you can test your code
17658
10:35:57,360 --> 10:35:59,040
here and not mess with the javascript
17659
10:35:59,040 --> 10:36:01,120
course if you forked that right and then
17660
10:36:01,120 --> 10:36:02,640
when you're done you're just going to
17661
10:36:02,640 --> 10:36:05,276
copy paste your code to the repo so
17662
10:36:05,276 --> 10:36:08,320
we'll meet up back there make sense cool
17663
10:36:08,320 --> 10:36:11,040
so this is the fighting game app right
17664
10:36:11,040 --> 10:36:13,680
this is to create an update game
17665
10:36:13,680 --> 10:36:16,400
function that will update the dom with
17666
10:36:16,400 --> 10:36:18,720
the state of the game what that means is
17667
10:36:18,720 --> 10:36:21,756
when i punch qazi he loses health we
17668
10:36:21,756 --> 10:36:24,720
should update the html now there's two
17669
10:36:24,720 --> 10:36:27,360
classes we have to create a player
17670
10:36:27,360 --> 10:36:29,840
and a game right you guys made classes
17671
10:36:29,840 --> 10:36:32,240
with qazi before remember so that was
17672
10:36:32,240 --> 10:36:35,596
like the car right and the car can drive
17673
10:36:35,596 --> 10:36:37,120
it can honk
17674
10:36:37,120 --> 10:36:39,276
right what is this class this class
17675
10:36:39,276 --> 10:36:41,276
he can strike or heal and that makes
17676
10:36:41,276 --> 10:36:43,120
sense for the player right but then
17677
10:36:43,120 --> 10:36:45,200
there's a class for game
17678
10:36:45,200 --> 10:36:47,596
the game is going to represent the whole
17679
10:36:47,596 --> 10:36:50,240
game itself so what can the game do
17680
10:36:50,240 --> 10:36:51,200
play
17681
10:36:51,200 --> 10:36:53,360
check if it's over declare the winner
17682
10:36:53,360 --> 10:36:55,596
and reset so i hope that makes sense to
17683
10:36:55,596 --> 10:36:57,916
you these functions are hard-coded into
17684
10:36:57,916 --> 10:36:59,680
html so don't change their names keep it
17685
10:36:59,680 --> 10:37:00,800
as it is
17686
10:37:00,800 --> 10:37:02,800
and here you're going to find all the
17687
10:37:02,800 --> 10:37:04,720
div ids that we're going to be using
17688
10:37:04,720 --> 10:37:06,400
there's a couple all right let's play
17689
10:37:06,400 --> 10:37:09,436
result the names and the health so let's
17690
10:37:09,436 --> 10:37:11,360
take a look at the h the html just for a
17691
10:37:11,360 --> 10:37:13,596
second take a look around as you can see
17692
10:37:13,596 --> 10:37:15,840
very bare bones right this is the image
17693
10:37:15,840 --> 10:37:18,640
of ryu here here is a div called player
17694
10:37:18,640 --> 10:37:20,880
controls so let's why did i go here it's
17695
10:37:20,880 --> 10:37:23,040
because of this id so there's an id
17696
10:37:23,040 --> 10:37:23,916
called
17697
10:37:23,916 --> 10:37:26,080
result right and that's going to hold
17698
10:37:26,080 --> 10:37:28,160
the winner of the match right so
17699
10:37:28,160 --> 10:37:29,680
remember that you saw that lance wins
17700
10:37:29,680 --> 10:37:30,800
over here
17701
10:37:30,800 --> 10:37:33,120
that's gonna be with the result div so
17702
10:37:33,120 --> 10:37:37,040
if you look scroll down to the result
17703
10:37:37,040 --> 10:37:39,276
this is the div that's gonna hold that
17704
10:37:39,276 --> 10:37:40,400
name
17705
10:37:40,400 --> 10:37:41,756
okay so if you're ever wondering what
17706
10:37:41,756 --> 10:37:43,680
the hell is this lance take a look at
17707
10:37:43,680 --> 10:37:46,240
html and see if you can find that same
17708
10:37:46,240 --> 10:37:47,360
div
17709
10:37:47,360 --> 10:37:50,240
cool but lucky for you and the exercise
17710
10:37:50,240 --> 10:37:53,276
we grab all the dips for you right so
17711
10:37:53,276 --> 10:37:55,040
the names should make sense it's pretty
17712
10:37:55,040 --> 10:37:56,960
easy to read the play button is a
17713
10:37:56,960 --> 10:37:58,960
variable that holds the play div
17714
10:37:58,960 --> 10:38:01,436
right the name div holds the player one
17715
10:38:01,436 --> 10:38:03,756
name uh-huh cool
17716
10:38:03,756 --> 10:38:05,916
awesome so now for the meet right the
17717
10:38:05,916 --> 10:38:09,276
actual factual functionality of the app
17718
10:38:09,276 --> 10:38:10,160
right
17719
10:38:10,160 --> 10:38:12,960
this is called update game right the
17720
10:38:12,960 --> 10:38:14,880
point of update game is exactly what it
17721
10:38:14,880 --> 10:38:17,120
sounds like update the dom with the
17722
10:38:17,120 --> 10:38:18,320
names
17723
10:38:18,320 --> 10:38:21,120
and the latest health of the players
17724
10:38:21,120 --> 10:38:24,160
right so right now it's defaulted to 100
17725
10:38:24,160 --> 10:38:26,480
and defaulted to player 1 and player 2.
17726
10:38:26,480 --> 10:38:29,120
that's just based off the html so if i
17727
10:38:29,120 --> 10:38:30,640
go to
17728
10:38:30,640 --> 10:38:32,400
player 1
17729
10:38:32,400 --> 10:38:33,436
right here
17730
10:38:33,436 --> 10:38:35,200
how do you like take a second to think
17731
10:38:35,200 --> 10:38:36,560
about this
17732
10:38:36,560 --> 10:38:39,436
how would you guys grab this div
17733
10:38:39,436 --> 10:38:42,000
and change the text of it
17734
10:38:42,000 --> 10:38:44,080
to the player
17735
10:38:44,080 --> 10:38:46,160
ah right it's getting really complex now
17736
10:38:46,160 --> 10:38:48,240
but it's not as complex as you think
17737
10:38:48,240 --> 10:38:49,436
right
17738
10:38:49,436 --> 10:38:52,560
so we want to be updating this to lens
17739
10:38:52,560 --> 10:38:55,200
and this takazi or even your name and
17740
10:38:55,200 --> 10:38:57,916
then within update game we're also going
17741
10:38:57,916 --> 10:38:59,596
to think about
17742
10:38:59,596 --> 10:39:02,080
when do we want to stop updating the
17743
10:39:02,080 --> 10:39:03,916
game and just declare a winner right
17744
10:39:03,916 --> 10:39:06,800
what condition will we need to do that
17745
10:39:06,800 --> 10:39:08,960
right so when we update the game
17746
10:39:08,960 --> 10:39:11,520
we're gonna need a condition to check
17747
10:39:11,520 --> 10:39:13,360
if the player health is less than or
17748
10:39:13,360 --> 10:39:15,436
equal to zero because if it's less than
17749
10:39:15,436 --> 10:39:17,276
or equal to zero
17750
10:39:17,276 --> 10:39:19,596
should we keep playing the game no we're
17751
10:39:19,596 --> 10:39:21,360
done so right
17752
10:39:21,360 --> 10:39:22,960
so if this hits zero or is less than
17753
10:39:22,960 --> 10:39:25,756
zero we can declare a winner and declare
17754
10:39:25,756 --> 10:39:28,240
winner is actually another method all
17755
10:39:28,240 --> 10:39:29,680
right if you scroll up you'll see all
17756
10:39:29,680 --> 10:39:32,320
the methods that we need to make
17757
10:39:32,320 --> 10:39:33,360
cool
17758
10:39:33,360 --> 10:39:36,800
so the very first thing besides update
17759
10:39:36,800 --> 10:39:38,720
game once you have that working we need
17760
10:39:38,720 --> 10:39:41,120
to start creating our classes so what is
17761
10:39:41,120 --> 10:39:44,000
a class guys can you remember a class i
17762
10:39:44,000 --> 10:39:46,880
like to call it a template for creating
17763
10:39:46,880 --> 10:39:49,120
an object right so this is the blueprint
17764
10:39:49,120 --> 10:39:51,360
of that object what kind of object are
17765
10:39:51,360 --> 10:39:52,640
we making here
17766
10:39:52,640 --> 10:39:54,800
exactly a player object so
17767
10:39:54,800 --> 10:39:56,560
in the constructor
17768
10:39:56,560 --> 10:39:58,560
we have to think about what are the
17769
10:39:58,560 --> 10:40:00,880
attributes of a player object so what do
17770
10:40:00,880 --> 10:40:02,640
we need we need a name
17771
10:40:02,640 --> 10:40:05,120
we need a health and we're going to need
17772
10:40:05,120 --> 10:40:08,240
attack damage right that attack damage
17773
10:40:08,240 --> 10:40:09,916
if you guys don't play any video games
17774
10:40:09,916 --> 10:40:12,240
or unfamiliar that just means how much
17775
10:40:12,240 --> 10:40:15,120
damage will my punch do to you
17776
10:40:15,120 --> 10:40:16,560
all right
17777
10:40:16,560 --> 10:40:19,360
so we can leave that as attack damage
17778
10:40:19,360 --> 10:40:20,320
right
17779
10:40:20,320 --> 10:40:22,320
and make sure that this is all passed in
17780
10:40:22,320 --> 10:40:24,000
the constructor because we are going to
17781
10:40:24,000 --> 10:40:26,080
do that ourselves later
17782
10:40:26,080 --> 10:40:28,880
cool so for the strike
17783
10:40:28,880 --> 10:40:31,680
this is a function within the class and
17784
10:40:31,680 --> 10:40:33,596
remember what that's called exactly it's
17785
10:40:33,596 --> 10:40:35,840
called a method right so this is the
17786
10:40:35,840 --> 10:40:37,200
strike method
17787
10:40:37,200 --> 10:40:40,480
and keep mo keep note of what it takes
17788
10:40:40,480 --> 10:40:42,640
in takes in a player
17789
10:40:42,640 --> 10:40:45,276
an enemy or attack damage right
17790
10:40:45,276 --> 10:40:47,276
before you get all worried
17791
10:40:47,276 --> 10:40:49,360
what do you think the strike function
17792
10:40:49,360 --> 10:40:50,720
does
17793
10:40:50,720 --> 10:40:51,916
exactly what it sounds like you're going
17794
10:40:51,916 --> 10:40:53,916
to strike somebody
17795
10:40:53,916 --> 10:40:55,040
right
17796
10:40:55,040 --> 10:40:55,840
so
17797
10:40:55,840 --> 10:40:57,436
when i strike
17798
10:40:57,436 --> 10:40:58,640
kazi
17799
10:40:58,640 --> 10:41:01,200
we can make him lose five damage points
17800
10:41:01,200 --> 10:41:02,480
or a health point so this should be like
17801
10:41:02,480 --> 10:41:04,400
95 right but we want to make it random
17802
10:41:04,400 --> 10:41:06,240
you guys know how to make random damage
17803
10:41:06,240 --> 10:41:08,240
right so that's why the first
17804
10:41:08,240 --> 10:41:10,880
instruction here is get a random number
17805
10:41:10,880 --> 10:41:12,720
between 1 and 10
17806
10:41:12,720 --> 10:41:14,400
and that is the damage amount so think
17807
10:41:14,400 --> 10:41:16,480
about that how do i get a random number
17808
10:41:16,480 --> 10:41:17,436
hmm
17809
10:41:17,436 --> 10:41:18,320
right
17810
10:41:18,320 --> 10:41:20,080
think way back right
17811
10:41:20,080 --> 10:41:21,520
actually not even that back you guys did
17812
10:41:21,520 --> 10:41:23,916
that earlier in another project
17813
10:41:23,916 --> 10:41:26,000
so get a random number between 1 and 10
17814
10:41:26,000 --> 10:41:28,400
that is your damage amount
17815
10:41:28,400 --> 10:41:30,000
so now that we have a random number
17816
10:41:30,000 --> 10:41:31,756
that's the damage amount
17817
10:41:31,756 --> 10:41:33,916
what should we do with that amount
17818
10:41:33,916 --> 10:41:35,916
exactly we should subtract
17819
10:41:35,916 --> 10:41:38,320
qazi's health with that amount right so
17820
10:41:38,320 --> 10:41:40,880
subtract the enemy health with the
17821
10:41:40,880 --> 10:41:43,116
damage amount right so how could we you
17822
10:41:43,116 --> 10:41:45,756
know change the enemy health
17823
10:41:45,756 --> 10:41:46,640
by
17824
10:41:46,640 --> 10:41:48,720
subtracting the damage amount
17825
10:41:48,720 --> 10:41:50,080
right so play around with it that's why
17826
10:41:50,080 --> 10:41:51,116
that's why i want you guys going
17827
10:41:51,116 --> 10:41:53,276
separate rebel it's easier to just play
17828
10:41:53,276 --> 10:41:55,116
around and test it right i don't expect
17829
10:41:55,116 --> 10:41:57,360
you guys to be like i got this lance
17830
10:41:57,360 --> 10:41:59,436
right for me it's going to be
17831
10:41:59,436 --> 10:42:01,840
incremental you got to test stuff out
17832
10:42:01,840 --> 10:42:03,840
all right feel it up so subtract the
17833
10:42:03,840 --> 10:42:05,680
enemy health for the damage amount and
17834
10:42:05,680 --> 10:42:08,400
after we do that you
17835
10:42:08,400 --> 10:42:10,400
you technically you did damage right
17836
10:42:10,400 --> 10:42:12,800
that's correct but i want to see it on
17837
10:42:12,800 --> 10:42:15,596
the html itself on the front end right
17838
10:42:15,596 --> 10:42:17,276
so we need to update
17839
10:42:17,276 --> 10:42:19,520
the dom somehow so
17840
10:42:19,520 --> 10:42:21,436
guess what then you're going to call
17841
10:42:21,436 --> 10:42:22,960
update game which is going to update
17842
10:42:22,960 --> 10:42:25,360
everything with the latest stats
17843
10:42:25,360 --> 10:42:27,116
so that's why
17844
10:42:27,116 --> 10:42:28,560
a lot of the functionality comes from
17845
10:42:28,560 --> 10:42:30,640
you making sure the updating game works
17846
10:42:30,640 --> 10:42:31,840
properly
17847
10:42:31,840 --> 10:42:34,240
cool so after you do some damage after
17848
10:42:34,240 --> 10:42:36,560
you strike kazi
17849
10:42:36,560 --> 10:42:38,880
you subtract the health you update the
17850
10:42:38,880 --> 10:42:40,960
game and then just return a message
17851
10:42:40,960 --> 10:42:41,840
saying
17852
10:42:41,840 --> 10:42:46,240
uh lance attacks qazi for this amount
17853
10:42:46,240 --> 10:42:47,840
right not too bad
17854
10:42:47,840 --> 10:42:48,880
right
17855
10:42:48,880 --> 10:42:50,000
now
17856
10:42:50,000 --> 10:42:52,240
if you understood that the heel is going
17857
10:42:52,240 --> 10:42:55,200
to be very very similar
17858
10:42:55,200 --> 10:42:57,200
right so remember a heel
17859
10:42:57,200 --> 10:42:59,680
is going to restore some health back
17860
10:42:59,680 --> 10:43:01,520
right so we don't want to make heal
17861
10:43:01,520 --> 10:43:04,000
overpowered so instead of 1 through 10
17862
10:43:04,000 --> 10:43:07,360
heal is a method that takes in who is
17863
10:43:07,360 --> 10:43:08,400
healing
17864
10:43:08,400 --> 10:43:10,560
right so if i put in let me put an
17865
10:43:10,560 --> 10:43:12,320
example here
17866
10:43:12,320 --> 10:43:13,436
heal
17867
10:43:13,436 --> 10:43:16,320
uh player one who's healing player two
17868
10:43:16,320 --> 10:43:17,436
or player one
17869
10:43:17,436 --> 10:43:20,320
player one you guys are killing it so if
17870
10:43:20,320 --> 10:43:22,400
you said player one we're good to go how
17871
10:43:22,400 --> 10:43:24,000
does heel work well the first thing
17872
10:43:24,000 --> 10:43:26,480
we're going to do is get a random number
17873
10:43:26,480 --> 10:43:31,040
from one two five exactly so we take
17874
10:43:31,040 --> 10:43:32,960
that number one to five and store it in
17875
10:43:32,960 --> 10:43:35,680
a variable we can call it hp amount so
17876
10:43:35,680 --> 10:43:37,360
we have amount what should we do with
17877
10:43:37,360 --> 10:43:39,840
that amount
17878
10:43:39,916 --> 10:43:42,160
add it to the player's health right so
17879
10:43:42,160 --> 10:43:44,960
if i got my num random number was 3
17880
10:43:44,960 --> 10:43:47,360
i need to add 3 to the player's health
17881
10:43:47,360 --> 10:43:48,640
so something i want you to start
17882
10:43:48,640 --> 10:43:52,000
thinking about is right
17883
10:43:52,560 --> 10:43:56,080
how do we get the player health lance
17884
10:43:56,080 --> 10:43:56,960
right
17885
10:43:56,960 --> 10:43:58,640
well you got to think back on when you
17886
10:43:58,640 --> 10:44:01,680
make a object from a player class
17887
10:44:01,680 --> 10:44:03,520
how do you get that health
17888
10:44:03,520 --> 10:44:05,436
and then how do you want to pass it down
17889
10:44:05,436 --> 10:44:06,960
right because it takes in a player so
17890
10:44:06,960 --> 10:44:08,320
how do we get that player and how do we
17891
10:44:08,320 --> 10:44:10,000
get the health
17892
10:44:10,000 --> 10:44:12,080
cool so after you added the health to
17893
10:44:12,080 --> 10:44:13,276
the player
17894
10:44:13,276 --> 10:44:15,436
guess what we got to update the game to
17895
10:44:15,436 --> 10:44:18,480
make sure that our html is up to date
17896
10:44:18,480 --> 10:44:20,800
okay and when it's done just return me a
17897
10:44:20,800 --> 10:44:23,680
nice message that says lance heals for
17898
10:44:23,680 --> 10:44:24,800
two hp
17899
10:44:24,800 --> 10:44:26,240
do good so far
17900
10:44:26,240 --> 10:44:27,680
great because you're done with the
17901
10:44:27,680 --> 10:44:30,000
player class that's it only strike and
17902
10:44:30,000 --> 10:44:33,040
heal as you can see
17903
10:44:33,040 --> 10:44:34,160
awesome
17904
10:44:34,160 --> 10:44:35,116
so
17905
10:44:35,116 --> 10:44:37,916
with the player class done there's also
17906
10:44:37,916 --> 10:44:40,960
the game class right so if again class
17907
10:44:40,960 --> 10:44:43,040
template or blueprint for creating an
17908
10:44:43,040 --> 10:44:45,916
object what object are we creating in
17909
10:44:45,916 --> 10:44:48,400
here we're creating the game and all the
17910
10:44:48,400 --> 10:44:50,080
functionality for it so in the
17911
10:44:50,080 --> 10:44:53,360
constructure constructor the constructor
17912
10:44:53,360 --> 10:44:55,520
right the attribute that it needs is
17913
10:44:55,520 --> 10:44:57,360
just is over so if i ask guys what do
17914
10:44:57,360 --> 10:44:58,880
you think is over
17915
10:44:58,880 --> 10:45:00,000
means
17916
10:45:00,000 --> 10:45:02,080
right it looks like it's defaulted to
17917
10:45:02,080 --> 10:45:02,960
false
17918
10:45:02,960 --> 10:45:04,880
alright what do you think it means
17919
10:45:04,880 --> 10:45:06,240
all right take a second
17920
10:45:06,240 --> 10:45:07,916
one two
17921
10:45:07,916 --> 10:45:10,560
three four five
17922
10:45:10,560 --> 10:45:13,520
is over represents if the game is over
17923
10:45:13,520 --> 10:45:16,320
so if the game if so if is over is false
17924
10:45:16,320 --> 10:45:17,756
is the game over
17925
10:45:17,756 --> 10:45:19,680
nah it's not over yet if it's true
17926
10:45:19,680 --> 10:45:21,436
though the game is over
17927
10:45:21,436 --> 10:45:22,320
okay
17928
10:45:22,320 --> 10:45:23,916
cool that's all you need to know by
17929
10:45:23,916 --> 10:45:25,916
default it's going to be false because
17930
10:45:25,916 --> 10:45:27,360
if it's true that means we finish the
17931
10:45:27,360 --> 10:45:29,680
round and if i ask guys when do you
17932
10:45:29,680 --> 10:45:32,400
think the game would be over here
17933
10:45:32,400 --> 10:45:35,436
yes this would be if one of us
17934
10:45:35,436 --> 10:45:36,560
health
17935
10:45:36,560 --> 10:45:39,596
is below zero or equal to zero so as
17936
10:45:39,596 --> 10:45:42,560
long as it's not zero or equal to zero
17937
10:45:42,560 --> 10:45:44,800
keep playing cool so if you got that
17938
10:45:44,800 --> 10:45:46,560
let's talk about our first method here
17939
10:45:46,560 --> 10:45:49,040
this is called declare the winner or
17940
10:45:49,040 --> 10:45:51,360
declare winner notice what it takes in
17941
10:45:51,360 --> 10:45:53,916
it takes in is over that's interesting
17942
10:45:53,916 --> 10:45:56,320
okay it takes in player one
17943
10:45:56,320 --> 10:45:58,640
and player two right if you look at the
17944
10:45:58,640 --> 10:46:00,640
description if the game is over
17945
10:46:00,640 --> 10:46:03,436
and a player has zero health we need to
17946
10:46:03,436 --> 10:46:05,360
declare who the winner is and that's
17947
10:46:05,360 --> 10:46:06,960
what this function does
17948
10:46:06,960 --> 10:46:09,116
cool so
17949
10:46:09,116 --> 10:46:10,720
all i want you to do is just create a
17950
10:46:10,720 --> 10:46:12,080
variable that's going to hold the
17951
10:46:12,080 --> 10:46:13,360
message
17952
10:46:13,360 --> 10:46:14,480
and
17953
10:46:14,480 --> 10:46:17,040
if is over is true why do i want to
17954
10:46:17,040 --> 10:46:19,756
check for that because if if if is over
17955
10:46:19,756 --> 10:46:22,640
is true then that means the game is over
17956
10:46:22,640 --> 10:46:24,880
but we don't want it to just be that
17957
10:46:24,880 --> 10:46:27,040
we also want it to be and
17958
10:46:27,040 --> 10:46:28,880
so what's and in programming that's
17959
10:46:28,880 --> 10:46:29,916
going to be something you want to think
17960
10:46:29,916 --> 10:46:31,200
about
17961
10:46:31,200 --> 10:46:32,240
and
17962
10:46:32,240 --> 10:46:34,320
player 1 health is less than or equal to
17963
10:46:34,320 --> 10:46:35,116
0
17964
10:46:35,116 --> 10:46:37,520
then update the message
17965
10:46:37,520 --> 10:46:41,276
to player one wins or lance wins
17966
10:46:41,276 --> 10:46:43,596
then we're gonna need an else if if is
17967
10:46:43,596 --> 10:46:45,116
over is true
17968
10:46:45,116 --> 10:46:46,480
and
17969
10:46:46,480 --> 10:46:48,960
p2 health is less than or equal to zero
17970
10:46:48,960 --> 10:46:52,080
then update the message to kazuwins or
17971
10:46:52,080 --> 10:46:53,596
p2 wins
17972
10:46:53,596 --> 10:46:55,756
and just for fun here if you look
17973
10:46:55,756 --> 10:46:58,240
underneath we need to play the victory
17974
10:46:58,240 --> 10:47:00,080
sound which i totally forgot for the
17975
10:47:00,080 --> 10:47:01,680
strike we also want to play a little
17976
10:47:01,680 --> 10:47:03,276
sound too right but you don't have to
17977
10:47:03,276 --> 10:47:05,040
worry about that just yet
17978
10:47:05,040 --> 10:47:06,240
that's gonna happen when you press
17979
10:47:06,240 --> 10:47:07,116
buttons
17980
10:47:07,116 --> 10:47:09,040
but when we declare the winner
17981
10:47:09,040 --> 10:47:11,436
there should be a nice victory sound
17982
10:47:11,436 --> 10:47:13,040
okay and after that just return message
17983
10:47:13,040 --> 10:47:14,080
variable
17984
10:47:14,080 --> 10:47:15,680
that's the clear one
17985
10:47:15,680 --> 10:47:18,400
keeping up so far feel free to rewind a
17986
10:47:18,400 --> 10:47:20,480
part of the video just to hear me
17987
10:47:20,480 --> 10:47:21,680
explain it one more time sometimes you
17988
10:47:21,680 --> 10:47:22,800
just need it more than once and it
17989
10:47:22,800 --> 10:47:24,240
clicks all right
17990
10:47:24,240 --> 10:47:25,680
and don't worry too if you're totally
17991
10:47:25,680 --> 10:47:26,560
lost
17992
10:47:26,560 --> 10:47:27,916
there's going to be a solution where i
17993
10:47:27,916 --> 10:47:30,640
explain how the code works
17994
10:47:30,640 --> 10:47:31,680
awesome
17995
10:47:31,680 --> 10:47:32,480
so
17996
10:47:32,480 --> 10:47:35,200
here we have reset
17997
10:47:35,200 --> 10:47:36,880
reset is exactly what it sounds like
17998
10:47:36,880 --> 10:47:38,560
let's say we finish the game and it's
17999
10:47:38,560 --> 10:47:39,436
over
18000
10:47:39,436 --> 10:47:41,756
we can hit reset to put back the hp to
18001
10:47:41,756 --> 10:47:45,840
zero not zero sorry 100 right so reset
18002
10:47:45,840 --> 10:47:48,400
is going to set the health back to 100
18003
10:47:48,400 --> 10:47:50,640
and what else should we do
18004
10:47:50,640 --> 10:47:51,680
exactly
18005
10:47:51,680 --> 10:47:55,116
we should set is over from true to false
18006
10:47:55,116 --> 10:47:57,116
or just make sure it's false because
18007
10:47:57,116 --> 10:47:59,116
then when we reset the game is ready to
18008
10:47:59,116 --> 10:48:00,560
go yeah
18009
10:48:00,560 --> 10:48:01,520
cool
18010
10:48:01,520 --> 10:48:03,756
another bonus thing is usually there's
18011
10:48:03,756 --> 10:48:05,276
going to be a text here that says lance
18012
10:48:05,276 --> 10:48:06,960
or causey wins
18013
10:48:06,960 --> 10:48:09,756
we want to clear that resolve div in our
18014
10:48:09,756 --> 10:48:11,680
text so that doesn't show there's a
18015
10:48:11,680 --> 10:48:12,720
winner right because it shouldn't be
18016
10:48:12,720 --> 10:48:14,160
wondering if we just started or reset
18017
10:48:14,160 --> 10:48:15,520
yeah
18018
10:48:15,520 --> 10:48:16,880
cool
18019
10:48:16,880 --> 10:48:20,640
awesome then there is this play feature
18020
10:48:20,640 --> 10:48:21,436
right
18021
10:48:21,436 --> 10:48:22,960
that's the one where we press simulate
18022
10:48:22,960 --> 10:48:25,040
and it's gonna just run through
18023
10:48:25,040 --> 10:48:26,880
all the commands and just generates a
18024
10:48:26,880 --> 10:48:29,116
winner randomly not randomly but based
18025
10:48:29,116 --> 10:48:31,520
off the commands that you put
18026
10:48:31,520 --> 10:48:32,640
so
18027
10:48:32,640 --> 10:48:34,320
this is where that while loop comes in
18028
10:48:34,320 --> 10:48:36,800
and notice something where it says while
18029
10:48:36,800 --> 10:48:38,640
exclamation point
18030
10:48:38,640 --> 10:48:40,560
this is over right so what does the
18031
10:48:40,560 --> 10:48:42,640
exclamation point mean if you don't know
18032
10:48:42,640 --> 10:48:44,160
what the exclamation point mean that
18033
10:48:44,160 --> 10:48:46,240
just means not right
18034
10:48:46,240 --> 10:48:46,960
so
18035
10:48:46,960 --> 10:48:51,040
when not this is over or in other words
18036
10:48:51,040 --> 10:48:54,640
if this is over sorry in other words
18037
10:48:54,640 --> 10:48:58,400
as long as this over this is over is
18038
10:48:58,400 --> 10:49:00,640
false keep running
18039
10:49:00,640 --> 10:49:01,520
right
18040
10:49:01,520 --> 10:49:03,916
if it's true then it's going to stop
18041
10:49:03,916 --> 10:49:06,640
so basically once is over is true
18042
10:49:06,640 --> 10:49:08,000
no more while loop
18043
10:49:08,000 --> 10:49:11,040
right keep looping until is over is true
18044
10:49:11,040 --> 10:49:12,880
if this is confusing you you can still
18045
10:49:12,880 --> 10:49:15,596
write it as that as well you can say
18046
10:49:15,596 --> 10:49:17,360
false like so and it would still work
18047
10:49:17,360 --> 10:49:19,756
the same right this is just another way
18048
10:49:19,756 --> 10:49:21,916
to do the same thing
18049
10:49:21,916 --> 10:49:23,040
amazing
18050
10:49:23,040 --> 10:49:26,080
so like i said once is over is true the
18051
10:49:26,080 --> 10:49:27,756
while loop is broken
18052
10:49:27,756 --> 10:49:30,320
then we declare the winner right this is
18053
10:49:30,320 --> 10:49:31,916
the simulation part right that's nothing
18054
10:49:31,916 --> 10:49:33,520
to do with us pressing or clicking the
18055
10:49:33,520 --> 10:49:35,756
buttons this is if you got lazy you just
18056
10:49:35,756 --> 10:49:38,240
want to see who would win click simulate
18057
10:49:38,240 --> 10:49:40,640
awesome so once that while loop is done
18058
10:49:40,640 --> 10:49:42,960
run that declare winner method
18059
10:49:42,960 --> 10:49:45,756
after that here i want you to set up the
18060
10:49:45,756 --> 10:49:48,480
players or you can say let player 1
18061
10:49:48,480 --> 10:49:51,680
equal 2 and then how do you set up a new
18062
10:49:51,680 --> 10:49:54,400
player object from the class you tell me
18063
10:49:54,400 --> 10:49:57,040
all right so put that over here then we
18064
10:49:57,040 --> 10:49:59,360
want to save that original player data
18065
10:49:59,360 --> 10:50:01,200
right so if i said uh let player one
18066
10:50:01,200 --> 10:50:03,360
equals you can say let p1 equal to
18067
10:50:03,360 --> 10:50:05,596
player one i just want to set a copy so
18068
10:50:05,596 --> 10:50:07,436
that we can reset without
18069
10:50:07,436 --> 10:50:09,276
um worrying about
18070
10:50:09,276 --> 10:50:10,240
you know
18071
10:50:10,240 --> 10:50:12,400
the original health
18072
10:50:12,400 --> 10:50:13,680
so there's that
18073
10:50:13,680 --> 10:50:15,916
next create the game object from the
18074
10:50:15,916 --> 10:50:17,916
game class it's the same as this one all
18075
10:50:17,916 --> 10:50:20,080
right how do we make a new instance of
18076
10:50:20,080 --> 10:50:21,916
the game class
18077
10:50:21,916 --> 10:50:24,720
and then we can initialize the game by
18078
10:50:24,720 --> 10:50:27,200
up saying update game right because when
18079
10:50:27,200 --> 10:50:29,520
you put in lance and qazi as the players
18080
10:50:29,520 --> 10:50:31,360
it's not going to say lance and qazi
18081
10:50:31,360 --> 10:50:33,360
here unless you call
18082
10:50:33,360 --> 10:50:34,880
update game
18083
10:50:34,880 --> 10:50:36,960
right
18084
10:50:36,960 --> 10:50:38,560
so make sure you call update game over
18085
10:50:38,560 --> 10:50:40,320
here
18086
10:50:40,320 --> 10:50:41,840
awesome
18087
10:50:41,840 --> 10:50:43,276
over here it's the same thing like we're
18088
10:50:43,276 --> 10:50:44,080
doing
18089
10:50:44,080 --> 10:50:45,916
up at the top here except we're just
18090
10:50:45,916 --> 10:50:48,720
saving the state of is over
18091
10:50:48,720 --> 10:50:50,240
okay
18092
10:50:50,240 --> 10:50:53,200
now this right here line 148
18093
10:50:53,200 --> 10:50:56,160
we're going to add a on click
18094
10:50:56,160 --> 10:50:58,720
right listener that's going to run the
18095
10:50:58,720 --> 10:51:00,160
play method
18096
10:51:00,160 --> 10:51:01,596
all right so think about
18097
10:51:01,596 --> 10:51:04,560
how do we set up an on click
18098
10:51:04,560 --> 10:51:07,916
what uh div element are we gonna use
18099
10:51:07,916 --> 10:51:09,436
from here
18100
10:51:09,436 --> 10:51:10,880
to hit the play button or it's the
18101
10:51:10,880 --> 10:51:12,560
simulate button
18102
10:51:12,560 --> 10:51:15,756
and make sure that it runs it right
18103
10:51:15,756 --> 10:51:18,240
that's all you need to do for 148
18104
10:51:18,240 --> 10:51:20,800
and for 151 if you're still listening
18105
10:51:20,800 --> 10:51:23,520
right you're going to add functionality
18106
10:51:23,520 --> 10:51:25,436
where we can press
18107
10:51:25,436 --> 10:51:27,840
a or q to do different things so if you
18108
10:51:27,840 --> 10:51:29,276
remember the prep section that we had
18109
10:51:29,276 --> 10:51:31,200
earlier this is where it all comes to
18110
10:51:31,200 --> 10:51:33,596
play so here this should look familiar
18111
10:51:33,596 --> 10:51:35,916
add event listener key down function e
18112
10:51:35,916 --> 10:51:36,960
right
18113
10:51:36,960 --> 10:51:39,360
here's the trick if you press q
18114
10:51:39,360 --> 10:51:41,520
and the enemy health is greater than
18115
10:51:41,520 --> 10:51:44,880
zero and is over is still false then
18116
10:51:44,880 --> 10:51:48,480
call the strike function or method sorry
18117
10:51:48,480 --> 10:51:51,680
strike method and after striking
18118
10:51:51,680 --> 10:51:54,400
play the attack sound so here's the
18119
10:51:54,400 --> 10:51:56,880
audio tag section coming up again and
18120
10:51:56,880 --> 10:51:58,320
you might be asking lance
18121
10:51:58,320 --> 10:51:59,916
there's audio here well if you look at
18122
10:51:59,916 --> 10:52:01,436
the index
18123
10:52:01,436 --> 10:52:02,400
right
18124
10:52:02,400 --> 10:52:04,560
scroll all the way to the bottom you can
18125
10:52:04,560 --> 10:52:07,116
see i already set up the audio for you
18126
10:52:07,116 --> 10:52:08,640
you just have to play it so how do we
18127
10:52:08,640 --> 10:52:10,560
play audio sounds think about how we did
18128
10:52:10,560 --> 10:52:11,596
that
18129
10:52:11,596 --> 10:52:13,436
all right grab the ids
18130
10:52:13,436 --> 10:52:15,520
and you hit the play method to play the
18131
10:52:15,520 --> 10:52:18,320
sound it's up to you guys to tell me
18132
10:52:18,320 --> 10:52:21,040
when you want to play each sound so map
18133
10:52:21,040 --> 10:52:22,880
out the sounds to what you want
18134
10:52:22,880 --> 10:52:24,400
make sure you get the heel in there this
18135
10:52:24,400 --> 10:52:26,880
is for keel and this is the player two
18136
10:52:26,880 --> 10:52:28,080
controls
18137
10:52:28,080 --> 10:52:30,400
and once you have that you should have a
18138
10:52:30,400 --> 10:52:33,040
working fighting game so this is
18139
10:52:33,040 --> 10:52:34,560
definitely a challenge i'm not gonna lie
18140
10:52:34,560 --> 10:52:37,116
to you guys but if you do this you can
18141
10:52:37,116 --> 10:52:39,276
pretty much make whatever game you want
18142
10:52:39,276 --> 10:52:40,320
and i think you guys are up for the
18143
10:52:40,320 --> 10:52:41,520
challenge so
18144
10:52:41,520 --> 10:52:43,520
take your time with this rewind what you
18145
10:52:43,520 --> 10:52:46,080
need to and if anything we can look at
18146
10:52:46,080 --> 10:52:47,756
the solution and do this together so are
18147
10:52:47,756 --> 10:52:50,320
you guys ready i know i am let's get
18148
10:52:50,320 --> 10:52:52,720
started
18149
10:52:53,756 --> 10:52:56,880
all right so this is the solution let's
18150
10:52:56,880 --> 10:52:58,880
walk through how to make this fighting
18151
10:52:58,880 --> 10:53:01,116
game app together so are you guys ready
18152
10:53:01,116 --> 10:53:02,800
and at any point it's starting to all
18153
10:53:02,800 --> 10:53:03,680
click
18154
10:53:03,680 --> 10:53:06,080
feel free to pause the video and see how
18155
10:53:06,080 --> 10:53:08,400
far you get before i catch up to you so
18156
10:53:08,400 --> 10:53:09,756
this is it this is the fighting game at
18157
10:53:09,756 --> 10:53:11,276
the moment nothing works i'm pressing
18158
10:53:11,276 --> 10:53:14,400
buttons and nothing is happening so as
18159
10:53:14,400 --> 10:53:15,840
you can see this is the fighting game
18160
10:53:15,840 --> 10:53:18,320
and if as a reminder the main
18161
10:53:18,320 --> 10:53:21,040
functionality of this game visually is
18162
10:53:21,040 --> 10:53:22,800
from update game right it does exactly
18163
10:53:22,800 --> 10:53:24,560
what it sounds like it's going to update
18164
10:53:24,560 --> 10:53:27,436
the html or the dom with whatever's
18165
10:53:27,436 --> 10:53:29,276
happening all right so we can see it
18166
10:53:29,276 --> 10:53:31,200
the main core functionality is going to
18167
10:53:31,200 --> 10:53:33,520
be based on these two classes that we'll
18168
10:53:33,520 --> 10:53:35,276
make so think back to when you made a
18169
10:53:35,276 --> 10:53:36,960
bank think back to when you made a car
18170
10:53:36,960 --> 10:53:38,720
with qazi right we're going to make a
18171
10:53:38,720 --> 10:53:40,640
player now and we're going to make a
18172
10:53:40,640 --> 10:53:42,320
game all right so
18173
10:53:42,320 --> 10:53:43,436
what i'm going to do we're going to be
18174
10:53:43,436 --> 10:53:44,800
unconventional we're going to start with
18175
10:53:44,800 --> 10:53:47,200
the classes first so that update game
18176
10:53:47,200 --> 10:53:49,116
might make sense right we're going to
18177
10:53:49,116 --> 10:53:52,320
start with by making the classes
18178
10:53:52,320 --> 10:53:54,960
cool so let's scroll down to the class
18179
10:53:54,960 --> 10:53:56,560
player right this is the part that you
18180
10:53:56,560 --> 10:53:58,720
guys should be familiar with when i make
18181
10:53:58,720 --> 10:54:00,560
a new player it should look something
18182
10:54:00,560 --> 10:54:01,596
like this
18183
10:54:01,596 --> 10:54:04,480
and we have to pass in qazi the health
18184
10:54:04,480 --> 10:54:05,520
and the
18185
10:54:05,520 --> 10:54:07,360
attack damage which is just how much
18186
10:54:07,360 --> 10:54:09,360
damage will cause this punch do that's
18187
10:54:09,360 --> 10:54:11,436
that's what seven is standing for right
18188
10:54:11,436 --> 10:54:12,240
cool
18189
10:54:12,240 --> 10:54:14,240
so as you can see this is where the name
18190
10:54:14,240 --> 10:54:15,916
goes the health goes and the attack
18191
10:54:15,916 --> 10:54:18,400
damage goes and then from here we put
18192
10:54:18,400 --> 10:54:20,080
that for each
18193
10:54:20,080 --> 10:54:22,320
property of an object right so how do i
18194
10:54:22,320 --> 10:54:25,040
get if after i create a new player how
18195
10:54:25,040 --> 10:54:27,116
do i get kazi's name you're going to do
18196
10:54:27,116 --> 10:54:31,116
kazi.net or kazi right so
18197
10:54:31,116 --> 10:54:32,480
let's not we don't have to make any of
18198
10:54:32,480 --> 10:54:34,080
the functions i just want to showcase
18199
10:54:34,080 --> 10:54:36,640
you how this works so if we scroll down
18200
10:54:36,640 --> 10:54:38,080
let's find where we have to make the
18201
10:54:38,080 --> 10:54:38,960
players
18202
10:54:38,960 --> 10:54:40,320
who will create two players using the
18203
10:54:40,320 --> 10:54:41,756
player class right
18204
10:54:41,756 --> 10:54:42,720
so
18205
10:54:42,720 --> 10:54:44,720
what we have to do i'll do is say let
18206
10:54:44,720 --> 10:54:46,960
player 1 right you can call this
18207
10:54:46,960 --> 10:54:48,640
whatever but i want to call player 1
18208
10:54:48,640 --> 10:54:49,840
equal to
18209
10:54:49,840 --> 10:54:52,240
how do we make a new instance of an
18210
10:54:52,240 --> 10:54:54,560
object we can say new
18211
10:54:54,560 --> 10:54:57,596
the class name right so capita capital p
18212
10:54:57,596 --> 10:55:00,000
parentheses and then whatever we pass in
18213
10:55:00,000 --> 10:55:03,916
so a reminder what do we pass in we need
18214
10:55:03,916 --> 10:55:08,720
name health and attack damage okay so
18215
10:55:08,720 --> 10:55:10,960
let's see so we're gonna need name which
18216
10:55:10,960 --> 10:55:12,960
will be player one will be this time
18217
10:55:12,960 --> 10:55:15,040
we'll make it quasi right
18218
10:55:15,040 --> 10:55:16,640
and then the next parameter is health
18219
10:55:16,640 --> 10:55:19,040
we'll say 100 and for attack damage
18220
10:55:19,040 --> 10:55:22,080
let's just say 10
18221
10:55:22,080 --> 10:55:24,480
so now that that's there this is player
18222
10:55:24,480 --> 10:55:26,560
1. if i were to console
18223
10:55:26,560 --> 10:55:28,400
log player one what am i going to see
18224
10:55:28,400 --> 10:55:29,916
guys
18225
10:55:29,916 --> 10:55:31,200
and we see
18226
10:55:31,200 --> 10:55:33,520
there you go there is our player object
18227
10:55:33,520 --> 10:55:36,916
with name health and tactic so if i say
18228
10:55:36,916 --> 10:55:38,480
player1.health
18229
10:55:38,480 --> 10:55:40,640
right we should see
18230
10:55:40,640 --> 10:55:43,680
100. cool if i wanted the player one's
18231
10:55:43,680 --> 10:55:44,560
name
18232
10:55:44,560 --> 10:55:47,360
that name right so that's why
18233
10:55:47,360 --> 10:55:49,520
in the example we have something like
18234
10:55:49,520 --> 10:55:51,040
this
18235
10:55:51,040 --> 10:55:53,116
let's see
18236
10:55:53,116 --> 10:55:54,400
over here yeah that's why i said player
18237
10:55:54,400 --> 10:55:55,756
one a name
18238
10:55:55,756 --> 10:55:57,360
is the name or kazi because that's the
18239
10:55:57,360 --> 10:55:59,276
name of the variable
18240
10:55:59,276 --> 10:56:01,276
beautiful so at this point you guys
18241
10:56:01,276 --> 10:56:02,640
should be able to make player two so i
18242
10:56:02,640 --> 10:56:04,720
don't make player two same thing let
18243
10:56:04,720 --> 10:56:06,080
player two
18244
10:56:06,080 --> 10:56:10,560
is equal to new player
18245
10:56:10,560 --> 10:56:12,560
lance
18246
10:56:12,560 --> 10:56:14,480
we'll say 100 as well and we'll also
18247
10:56:14,480 --> 10:56:16,720
make 15 to make it fair well sorry not
18248
10:56:16,720 --> 10:56:18,160
fair that's not fair at all we'll make
18249
10:56:18,160 --> 10:56:20,000
it 10. sorry
18250
10:56:20,000 --> 10:56:22,080
beautiful all right so with that said
18251
10:56:22,080 --> 10:56:25,520
those are our two player classes right
18252
10:56:25,520 --> 10:56:27,680
so with coding everything's a pattern
18253
10:56:27,680 --> 10:56:29,436
and once you understand the pattern we
18254
10:56:29,436 --> 10:56:31,436
can replicate and do what we need to so
18255
10:56:31,436 --> 10:56:33,040
this is how you create two players from
18256
10:56:33,040 --> 10:56:35,596
the player class how do we create a game
18257
10:56:35,596 --> 10:56:38,640
object from the game class
18258
10:56:38,640 --> 10:56:39,680
okay
18259
10:56:39,680 --> 10:56:41,276
cool so
18260
10:56:41,276 --> 10:56:44,160
if you were saying like game equals new
18261
10:56:44,160 --> 10:56:45,436
game
18262
10:56:45,436 --> 10:56:46,560
right
18263
10:56:46,560 --> 10:56:47,756
is that it
18264
10:56:47,756 --> 10:56:49,116
right well if you're one they're not
18265
10:56:49,116 --> 10:56:51,276
sure if this is it we're gonna look up
18266
10:56:51,276 --> 10:56:52,960
and see if we need to pass anything in
18267
10:56:52,960 --> 10:56:54,320
the constructor
18268
10:56:54,320 --> 10:56:55,680
nothing so
18269
10:56:55,680 --> 10:56:57,040
this is it for the game class right and
18270
10:56:57,040 --> 10:56:59,756
if i were to console log the game
18271
10:56:59,756 --> 10:57:01,276
right
18272
10:57:01,276 --> 10:57:02,240
let's see
18273
10:57:02,240 --> 10:57:04,800
what we find out we say the object and
18274
10:57:04,800 --> 10:57:07,840
only attribute that it has is is over is
18275
10:57:07,840 --> 10:57:10,960
false right as a reminder what is is
18276
10:57:10,960 --> 10:57:14,800
over mean right basically if is over
18277
10:57:14,800 --> 10:57:16,160
equals true
18278
10:57:16,160 --> 10:57:17,840
the game is over if it's false i can
18279
10:57:17,840 --> 10:57:19,596
still punch quasi he can still punch me
18280
10:57:19,596 --> 10:57:21,520
back alright we can keep playing so
18281
10:57:21,520 --> 10:57:22,960
that's what the game is so if i were to
18282
10:57:22,960 --> 10:57:25,360
if i wanted to see the state of the game
18283
10:57:25,360 --> 10:57:28,640
i can just say console log game dot
18284
10:57:28,640 --> 10:57:31,040
is over and it's false so is the game
18285
10:57:31,040 --> 10:57:33,520
over false it's not over
18286
10:57:33,520 --> 10:57:34,480
awesome
18287
10:57:34,480 --> 10:57:36,400
so that's the basics i initialized it
18288
10:57:36,400 --> 10:57:38,080
like this we're going to come back to
18289
10:57:38,080 --> 10:57:39,840
this but this is my review for the
18290
10:57:39,840 --> 10:57:41,200
classes
18291
10:57:41,200 --> 10:57:44,480
cool so now when i say p1 dot health or
18292
10:57:44,480 --> 10:57:48,000
p1 that name or game dot is over you
18293
10:57:48,000 --> 10:57:49,756
should know what i mean
18294
10:57:49,756 --> 10:57:52,000
cool
18295
10:57:53,436 --> 10:57:55,200
all righty so now that you've got your
18296
10:57:55,200 --> 10:57:57,520
refresher on how the classes work we're
18297
10:57:57,520 --> 10:58:00,000
going to make the core function that's
18298
10:58:00,000 --> 10:58:02,400
not in any class it's called update game
18299
10:58:02,400 --> 10:58:04,400
and again it's going to update the html
18300
10:58:04,400 --> 10:58:06,320
to the game right so if you look at the
18301
10:58:06,320 --> 10:58:07,520
comment here
18302
10:58:07,520 --> 10:58:09,596
we're going to update the dom with the
18303
10:58:09,596 --> 10:58:12,000
names and the latest health of the
18304
10:58:12,000 --> 10:58:13,436
players what do i mean by that let's
18305
10:58:13,436 --> 10:58:16,800
break it down by the names so with names
18306
10:58:16,800 --> 10:58:18,320
where are the names on the html do you
18307
10:58:18,320 --> 10:58:20,320
see it it's player one
18308
10:58:20,320 --> 10:58:23,276
and player two right
18309
10:58:23,276 --> 10:58:25,040
i wanna change this to the names that i
18310
10:58:25,040 --> 10:58:27,756
put in so if you look all the way down
18311
10:58:27,756 --> 10:58:29,916
player one is kazi so this should be
18312
10:58:29,916 --> 10:58:32,080
changed to kazi alright so how do i
18313
10:58:32,080 --> 10:58:33,916
change that to cause
18314
10:58:33,916 --> 10:58:35,680
right to break it down step by step
18315
10:58:35,680 --> 10:58:37,436
first i need to grab
18316
10:58:37,436 --> 10:58:40,160
this dom element all right so how do i
18317
10:58:40,160 --> 10:58:43,840
grab the player one dom element
18318
10:58:43,840 --> 10:58:45,276
so lucky for you
18319
10:58:45,276 --> 10:58:47,200
all the elements that you need are up
18320
10:58:47,200 --> 10:58:48,480
here you're just going to have to
18321
10:58:48,480 --> 10:58:50,560
examine each one and think about what
18322
10:58:50,560 --> 10:58:52,240
you need and they're pretty easy to read
18323
10:58:52,240 --> 10:58:54,320
in my opinion so which of these
18324
10:58:54,320 --> 10:58:57,040
variables contains the name
18325
10:58:57,040 --> 10:58:58,960
dom element
18326
10:58:58,960 --> 10:59:00,320
all right i know you guys see it it's
18327
10:59:00,320 --> 10:59:04,480
number 38 p1 named it is equal to p1
18328
10:59:04,480 --> 10:59:06,160
name right if i were to console.log it
18329
10:59:06,160 --> 10:59:08,640
just to prove it to you guys
18330
10:59:08,640 --> 10:59:12,560
p1 name div hit run
18331
10:59:12,560 --> 10:59:14,560
boom there it is html div element and
18332
10:59:14,560 --> 10:59:17,200
then how do i get the player one text in
18333
10:59:17,200 --> 10:59:19,520
there right do you remember
18334
10:59:19,520 --> 10:59:20,960
think back to what kali taught you he
18335
10:59:20,960 --> 10:59:23,436
said dot inner text
18336
10:59:23,436 --> 10:59:26,560
and i can see player one so again where
18337
10:59:26,560 --> 10:59:28,480
is this coming from we are grabbing it
18338
10:59:28,480 --> 10:59:30,560
from the dom which is right here and you
18339
10:59:30,560 --> 10:59:33,200
can see it highlighted p1 name
18340
10:59:33,200 --> 10:59:35,116
the inner text of this div
18341
10:59:35,116 --> 10:59:38,720
is player one okay cool so now that we
18342
10:59:38,720 --> 10:59:42,240
know how to get this player 1 we need to
18343
10:59:42,240 --> 10:59:44,880
determine what we want to change it to
18344
10:59:44,880 --> 10:59:46,160
right so
18345
10:59:46,160 --> 10:59:49,756
in this function line 44 we can write
18346
10:59:49,756 --> 10:59:52,320
p1 name div
18347
10:59:52,320 --> 10:59:54,320
right and what do i want to change about
18348
10:59:54,320 --> 10:59:55,680
this div do i want to change the whole
18349
10:59:55,680 --> 10:59:58,240
entire div
18350
10:59:58,880 --> 11:00:01,116
no just the inner text so to change the
18351
11:00:01,116 --> 11:00:02,560
inner text you just do it enter text
18352
11:00:02,560 --> 11:00:03,360
again
18353
11:00:03,360 --> 11:00:06,320
and to actually update it or change it
18354
11:00:06,320 --> 11:00:09,200
you say equals and then if i say
18355
11:00:09,200 --> 11:00:11,916
something like dog this is going to say
18356
11:00:11,916 --> 11:00:13,596
dog right but it's i don't want to say
18357
11:00:13,596 --> 11:00:16,080
dog i want to say kazi
18358
11:00:16,080 --> 11:00:18,720
and i could just hard code this to kazi
18359
11:00:18,720 --> 11:00:21,276
this is still technically correct right
18360
11:00:21,276 --> 11:00:23,520
but what would be more accurate
18361
11:00:23,520 --> 11:00:25,200
is if i get it
18362
11:00:25,200 --> 11:00:28,800
from this object player one object so
18363
11:00:28,800 --> 11:00:31,040
how do i get the name of player one
18364
11:00:31,040 --> 11:00:32,400
right so you see how
18365
11:00:32,400 --> 11:00:34,240
the code can be written in a way that's
18366
11:00:34,240 --> 11:00:37,276
readable so if i want player 1 right
18367
11:00:37,276 --> 11:00:39,040
i could write player 1 here that would
18368
11:00:39,040 --> 11:00:40,960
still work but
18369
11:00:40,960 --> 11:00:42,560
this is the key here this is the
18370
11:00:42,560 --> 11:00:45,680
parentheses in here this is the argument
18371
11:00:45,680 --> 11:00:48,080
i'm passing player 1
18372
11:00:48,080 --> 11:00:51,840
as p1 so what i'm basically saying is
18373
11:00:51,840 --> 11:00:53,436
player 1
18374
11:00:53,436 --> 11:00:54,320
is
18375
11:00:54,320 --> 11:00:55,916
the same as
18376
11:00:55,916 --> 11:00:58,240
p1 so whenever i say p1 i'm talking
18377
11:00:58,240 --> 11:01:02,240
about player 1. same thing with p2 okay
18378
11:01:02,240 --> 11:01:04,880
so in the inner text here if i say p1
18379
11:01:04,880 --> 11:01:08,000
this is what is the data type of p1 is
18380
11:01:08,000 --> 11:01:09,040
it a
18381
11:01:09,040 --> 11:01:12,240
string a number or an object
18382
11:01:12,240 --> 11:01:14,960
i'm gonna give you guys five seconds one
18383
11:01:14,960 --> 11:01:15,916
two
18384
11:01:15,916 --> 11:01:16,880
three
18385
11:01:16,880 --> 11:01:17,680
four
18386
11:01:17,680 --> 11:01:20,000
five if you said object you are correct
18387
11:01:20,000 --> 11:01:21,916
it's the same as the player
18388
11:01:21,916 --> 11:01:23,596
object that we created before so if this
18389
11:01:23,596 --> 11:01:26,240
is the object what properties does this
18390
11:01:26,240 --> 11:01:28,640
object have and which one do i need
18391
11:01:28,640 --> 11:01:29,436
right
18392
11:01:29,436 --> 11:01:30,560
i need
18393
11:01:30,560 --> 11:01:34,400
the name so i'll just say p1 dot name
18394
11:01:34,400 --> 11:01:36,080
and there we go
18395
11:01:36,080 --> 11:01:37,916
all right and i'm just underneath this
18396
11:01:37,916 --> 11:01:39,680
i'll call update game right here i'm
18397
11:01:39,680 --> 11:01:41,360
going to move it later but if i say
18398
11:01:41,360 --> 11:01:44,080
update game and i say pass in oops
18399
11:01:44,080 --> 11:01:46,800
actually no i can't do that and you go
18400
11:01:46,800 --> 11:01:48,640
back
18401
11:01:48,640 --> 11:01:50,880
cool if i were to go down here to prove
18402
11:01:50,880 --> 11:01:51,916
it to you
18403
11:01:51,916 --> 11:01:53,756
let's see
18404
11:01:53,756 --> 11:01:56,160
you want to be calling
18405
11:01:56,160 --> 11:01:58,320
update game here so in line 143 this is
18406
11:01:58,320 --> 11:02:00,720
where we're going to call update game
18407
11:02:00,720 --> 11:02:02,480
and we're gonna pass in
18408
11:02:02,480 --> 11:02:05,680
player one
18409
11:02:05,840 --> 11:02:07,360
or actually no we're gonna do this sorry
18410
11:02:07,360 --> 11:02:08,960
let me start over
18411
11:02:08,960 --> 11:02:11,916
so we're gonna come down to 143
18412
11:02:11,916 --> 11:02:15,200
and over here in 137 we're gonna save
18413
11:02:15,200 --> 11:02:17,756
that player data so p1 is equal to
18414
11:02:17,756 --> 11:02:18,880
player one
18415
11:02:18,880 --> 11:02:22,000
and p2 is equal to player two
18416
11:02:22,000 --> 11:02:24,240
okay we're doing this so that we have
18417
11:02:24,240 --> 11:02:26,560
the original player data
18418
11:02:26,560 --> 11:02:27,916
all right so when we reset it's going to
18419
11:02:27,916 --> 11:02:29,360
be easier so
18420
11:02:29,360 --> 11:02:31,436
now like i said before player 1 is the
18421
11:02:31,436 --> 11:02:33,360
same as p1
18422
11:02:33,360 --> 11:02:36,160
cool so we want to start the game by
18423
11:02:36,160 --> 11:02:37,840
updating it
18424
11:02:37,840 --> 11:02:39,840
and we can put p1
18425
11:02:39,840 --> 11:02:40,960
p2
18426
11:02:40,960 --> 11:02:43,040
and we can say
18427
11:02:43,040 --> 11:02:44,400
the game
18428
11:02:44,400 --> 11:02:47,916
i think was the third variable
18429
11:02:47,916 --> 11:02:50,960
game state oh yeah game
18430
11:02:50,960 --> 11:02:52,960
dot is over
18431
11:02:52,960 --> 11:02:54,320
perfect
18432
11:02:54,320 --> 11:02:57,276
okay now hit run here look at that quasi
18433
11:02:57,276 --> 11:02:59,596
it's kazi now right how is it kazi did i
18434
11:02:59,596 --> 11:03:03,436
write just kazi there nope because i am
18435
11:03:03,436 --> 11:03:05,596
saying an update game change that inner
18436
11:03:05,596 --> 11:03:07,680
text to player one's name all right like
18437
11:03:07,680 --> 11:03:09,276
i said before we could do dog here and
18438
11:03:09,276 --> 11:03:10,480
if i hit run
18439
11:03:10,480 --> 11:03:11,840
it's going to be dog
18440
11:03:11,840 --> 11:03:12,800
cool
18441
11:03:12,800 --> 11:03:14,400
and again like i said
18442
11:03:14,400 --> 11:03:16,800
kazi would still work in this scenario
18443
11:03:16,800 --> 11:03:19,360
right but it's not what we call dynamic
18444
11:03:19,360 --> 11:03:21,680
right it's hard-coded quasi right
18445
11:03:21,680 --> 11:03:23,680
i want it to be whatever i put so if i
18446
11:03:23,680 --> 11:03:25,756
put p1 that name here
18447
11:03:25,756 --> 11:03:27,040
it's qazi
18448
11:03:27,040 --> 11:03:30,000
but if i go back to
18449
11:03:30,000 --> 11:03:33,916
this instance in 134 if i change this to
18450
11:03:33,916 --> 11:03:34,960
kevin
18451
11:03:34,960 --> 11:03:36,560
hit run
18452
11:03:36,560 --> 11:03:38,160
now it's kevin see how it's dynamic
18453
11:03:38,160 --> 11:03:39,840
depending on what it's like kind of like
18454
11:03:39,840 --> 11:03:41,360
when you start a video game and it says
18455
11:03:41,360 --> 11:03:43,520
enter your name here the game will be
18456
11:03:43,520 --> 11:03:45,840
updated to say that name all right so
18457
11:03:45,840 --> 11:03:47,436
that's what we're doing all right so hit
18458
11:03:47,436 --> 11:03:48,400
run
18459
11:03:48,400 --> 11:03:50,880
cool so if i ask you guys how can we
18460
11:03:50,880 --> 11:03:54,160
put in player two's name
18461
11:03:54,160 --> 11:03:56,080
how can we put player two's name what
18462
11:03:56,080 --> 11:03:58,640
would you guys do
18463
11:03:58,800 --> 11:04:00,480
you guys got the pattern it's gonna be
18464
11:04:00,480 --> 11:04:04,560
p2 name div dot inner text is equal to
18465
11:04:04,560 --> 11:04:06,640
p2 dot name
18466
11:04:06,640 --> 11:04:08,960
boom that's it
18467
11:04:08,960 --> 11:04:11,276
awesome so what about the health well
18468
11:04:11,276 --> 11:04:12,320
the health
18469
11:04:12,320 --> 11:04:14,800
right we're gonna what we need to start
18470
11:04:14,800 --> 11:04:17,040
thinking about what div
18471
11:04:17,040 --> 11:04:19,116
holds this health right let's see if
18472
11:04:19,116 --> 11:04:20,800
there's anything that can help us here
18473
11:04:20,800 --> 11:04:22,240
all right if you're not sure we can also
18474
11:04:22,240 --> 11:04:25,436
look here so in the health
18475
11:04:25,436 --> 11:04:27,596
it's going to be the p1 health right so
18476
11:04:27,596 --> 11:04:30,000
we have to grab that element by its id
18477
11:04:30,000 --> 11:04:32,240
and we already did over here
18478
11:04:32,240 --> 11:04:34,960
right p1 health dip so
18479
11:04:34,960 --> 11:04:36,560
following the same pattern we can just
18480
11:04:36,560 --> 11:04:40,160
say p1 health div dot inner text is
18481
11:04:40,160 --> 11:04:42,720
equal to
18482
11:04:43,520 --> 11:04:46,720
what we're going to put is the p1 dot
18483
11:04:46,720 --> 11:04:47,756
health
18484
11:04:47,756 --> 11:04:50,400
all right same thing here p2healthdiv
18485
11:04:50,400 --> 11:04:52,560
dot in our text
18486
11:04:52,560 --> 11:04:53,596
oops
18487
11:04:53,596 --> 11:04:55,916
dot enter text
18488
11:04:55,916 --> 11:04:57,116
is equal to
18489
11:04:57,116 --> 11:04:59,276
p2 dot l
18490
11:04:59,276 --> 11:05:01,040
awesome so this isn't so bad because
18491
11:05:01,040 --> 11:05:03,200
this is it this is what's actually
18492
11:05:03,200 --> 11:05:05,040
making it so we can see what's going on
18493
11:05:05,040 --> 11:05:06,960
after i punch quasi or he punches me
18494
11:05:06,960 --> 11:05:08,480
back
18495
11:05:08,480 --> 11:05:09,436
cool
18496
11:05:09,436 --> 11:05:10,640
so
18497
11:05:10,640 --> 11:05:13,040
now the next thing is
18498
11:05:13,040 --> 11:05:15,360
if i punch qazi his health will drop to
18499
11:05:15,360 --> 11:05:17,436
like 90 or whatever
18500
11:05:17,436 --> 11:05:19,436
okay so if it's 90
18501
11:05:19,436 --> 11:05:22,080
are we still playing yes or no
18502
11:05:22,080 --> 11:05:24,240
yes so if we're still playing right that
18503
11:05:24,240 --> 11:05:27,200
means we can keep going at it but if
18504
11:05:27,200 --> 11:05:28,480
my health
18505
11:05:28,480 --> 11:05:32,240
is zero can qazi still punch me no he
18506
11:05:32,240 --> 11:05:33,596
shouldn't be able to i'm already done
18507
11:05:33,596 --> 11:05:35,436
right so
18508
11:05:35,436 --> 11:05:37,200
we can set a condition
18509
11:05:37,200 --> 11:05:39,436
to make it so it stops
18510
11:05:39,436 --> 11:05:41,596
updating the game and we can declare a
18511
11:05:41,596 --> 11:05:43,116
winner right because that means the game
18512
11:05:43,116 --> 11:05:45,200
is over so
18513
11:05:45,200 --> 11:05:47,200
we can clear a condition like i said so
18514
11:05:47,200 --> 11:05:48,000
if
18515
11:05:48,000 --> 11:05:49,756
the player health is less than or equal
18516
11:05:49,756 --> 11:05:53,680
to zero then set is over to true
18517
11:05:53,680 --> 11:05:55,360
right so basically told you what's right
18518
11:05:55,360 --> 11:05:56,960
for the condition
18519
11:05:56,960 --> 11:06:00,480
so how do i get the player health again
18520
11:06:00,480 --> 11:06:02,640
p1 dot health
18521
11:06:02,640 --> 11:06:04,960
okay you're right
18522
11:06:04,960 --> 11:06:06,160
health
18523
11:06:06,160 --> 11:06:07,596
right and what do we want to check about
18524
11:06:07,596 --> 11:06:10,240
the health we just want to say p1.health
18525
11:06:10,240 --> 11:06:12,400
right what we want to say is if
18526
11:06:12,400 --> 11:06:15,756
p1 that health if kazi's health is less
18527
11:06:15,756 --> 11:06:17,840
than or equal to zero
18528
11:06:17,840 --> 11:06:20,560
right then the game should be over
18529
11:06:20,560 --> 11:06:22,320
right but what about my health if my
18530
11:06:22,320 --> 11:06:24,240
health is less than or equal to zero
18531
11:06:24,240 --> 11:06:26,720
right can we keep playing no
18532
11:06:26,720 --> 11:06:29,040
so what am i really saying then um what
18533
11:06:29,040 --> 11:06:31,596
i'm saying is if either player's health
18534
11:06:31,596 --> 11:06:34,240
is equal to zero right
18535
11:06:34,240 --> 11:06:36,800
so we can say this or right here this
18536
11:06:36,800 --> 11:06:37,596
means
18537
11:06:37,596 --> 11:06:38,960
or
18538
11:06:38,960 --> 11:06:41,520
p2.health is less than or equal to zero
18539
11:06:41,520 --> 11:06:43,916
so let's read this out fully together so
18540
11:06:43,916 --> 11:06:46,640
this is saying if kazi's health is less
18541
11:06:46,640 --> 11:06:49,200
than zero or equal to zero
18542
11:06:49,200 --> 11:06:51,756
or if lance's health is less than or
18543
11:06:51,756 --> 11:06:53,680
equal to zero
18544
11:06:53,680 --> 11:06:56,400
that means the game is over but is it
18545
11:06:56,400 --> 11:06:58,560
over by just writing this if statement
18546
11:06:58,560 --> 11:07:01,040
not really right what we need to do is
18547
11:07:01,040 --> 11:07:04,080
take the game state right
18548
11:07:04,080 --> 11:07:07,360
or in other words take game dot is over
18549
11:07:07,360 --> 11:07:10,080
right and set that to true and then we
18550
11:07:10,080 --> 11:07:12,880
can update the game state our game state
18551
11:07:12,880 --> 11:07:15,040
is what we passed in here
18552
11:07:15,040 --> 11:07:18,000
game state to equal
18553
11:07:18,000 --> 11:07:18,880
game
18554
11:07:18,880 --> 11:07:21,360
dot is over
18555
11:07:21,360 --> 11:07:22,880
right so it's like it's called
18556
11:07:22,880 --> 11:07:24,400
transitive property i'm saying this is
18557
11:07:24,400 --> 11:07:26,960
now true and now game state is also
18558
11:07:26,960 --> 11:07:29,276
equal to true right
18559
11:07:29,276 --> 11:07:30,880
cool
18560
11:07:30,880 --> 11:07:32,000
now
18561
11:07:32,000 --> 11:07:34,240
if you remember the solution and the
18562
11:07:34,240 --> 11:07:35,276
demo
18563
11:07:35,276 --> 11:07:37,436
when kazi beats me right it's going to
18564
11:07:37,436 --> 11:07:39,840
say kazi's wins over here in this area
18565
11:07:39,840 --> 11:07:42,960
right here where my mouse is right
18566
11:07:42,960 --> 11:07:45,040
so what we need to do
18567
11:07:45,040 --> 11:07:47,520
to make it so that messages displays is
18568
11:07:47,520 --> 11:07:49,520
find out which of these holds that
18569
11:07:49,520 --> 11:07:54,480
resultive right if i go to the html
18570
11:07:54,480 --> 11:07:56,800
let's look at the html where are you
18571
11:07:56,800 --> 11:08:00,400
over here 989 it's empty right but we
18572
11:08:00,400 --> 11:08:02,320
want to populate it with a message that
18573
11:08:02,320 --> 11:08:04,800
says kazi wins right
18574
11:08:04,800 --> 11:08:06,320
so
18575
11:08:06,320 --> 11:08:08,240
that's what this update game is going to
18576
11:08:08,240 --> 11:08:09,680
return
18577
11:08:09,680 --> 11:08:11,116
right so we're going to say
18578
11:08:11,116 --> 11:08:13,916
result dot inner text
18579
11:08:13,916 --> 11:08:15,680
right where am i getting result from i'm
18580
11:08:15,680 --> 11:08:17,520
getting it from over here
18581
11:08:17,520 --> 11:08:19,200
all right it's actually called result
18582
11:08:19,200 --> 11:08:20,960
div sorry
18583
11:08:20,960 --> 11:08:23,276
and how do i change it to the message
18584
11:08:23,276 --> 11:08:26,880
well the message is can be like kazi
18585
11:08:26,880 --> 11:08:28,080
wins
18586
11:08:28,080 --> 11:08:30,720
but that's not dynamic guys
18587
11:08:30,720 --> 11:08:32,880
if i put kazi wins here
18588
11:08:32,880 --> 11:08:34,720
and i beat qazi
18589
11:08:34,720 --> 11:08:36,000
it's gonna still say kazi wins and
18590
11:08:36,000 --> 11:08:37,916
that's not right so
18591
11:08:37,916 --> 11:08:39,756
what we need to do is make a dynamic by
18592
11:08:39,756 --> 11:08:42,080
saying is equal to
18593
11:08:42,080 --> 11:08:43,436
game
18594
11:08:43,436 --> 11:08:48,000
dot declare winner right and pass in the
18595
11:08:48,000 --> 11:08:50,240
arguments let's pause them
18596
11:08:50,240 --> 11:08:51,680
you might be wondering lance what the
18597
11:08:51,680 --> 11:08:54,320
hell is declare winner well don't worry
18598
11:08:54,320 --> 11:08:56,400
declare winner if you remember up at the
18599
11:08:56,400 --> 11:08:57,520
top here
18600
11:08:57,520 --> 11:08:59,596
it's a function or method that comes
18601
11:08:59,596 --> 11:09:01,916
from the game class and we haven't made
18602
11:09:01,916 --> 11:09:04,240
declare winner yet so don't worry about
18603
11:09:04,240 --> 11:09:06,160
it just know that we have to call it
18604
11:09:06,160 --> 11:09:07,916
right here
18605
11:09:07,916 --> 11:09:09,916
okay so declare winner and if you look
18606
11:09:09,916 --> 11:09:11,680
at declare winner all right sorry for
18607
11:09:11,680 --> 11:09:13,596
moving around it's going to take three
18608
11:09:13,596 --> 11:09:15,756
properties it's gonna take the game
18609
11:09:15,756 --> 11:09:18,320
state which is game is over right it's
18610
11:09:18,320 --> 11:09:19,276
gonna take
18611
11:09:19,276 --> 11:09:23,040
p1 and p2 right kind of like
18612
11:09:23,040 --> 11:09:25,200
transferring this over it's like uh this
18613
11:09:25,200 --> 11:09:27,040
guy was doing stuff and then now pass it
18614
11:09:27,040 --> 11:09:29,916
on to the next task right
18615
11:09:29,916 --> 11:09:32,960
declare winner his job all it is is to
18616
11:09:32,960 --> 11:09:35,520
let us know that handles who the winner
18617
11:09:35,520 --> 11:09:37,200
is right
18618
11:09:37,200 --> 11:09:39,436
update game is just updating the game
18619
11:09:39,436 --> 11:09:42,240
declare winner is telling us who
18620
11:09:42,240 --> 11:09:44,320
actually won between the two
18621
11:09:44,320 --> 11:09:46,480
does that make sense perfect and the
18622
11:09:46,480 --> 11:09:48,000
last thing we want to do in this if
18623
11:09:48,000 --> 11:09:50,640
statement we can just say return the
18624
11:09:50,640 --> 11:09:52,880
game state
18625
11:09:52,880 --> 11:09:54,080
perfect
18626
11:09:54,080 --> 11:09:56,160
okay that should be good then that's
18627
11:09:56,160 --> 11:09:58,160
update game for you
18628
11:09:58,160 --> 11:09:59,840
so let's hit run
18629
11:09:59,840 --> 11:10:01,840
perfect so now we got quasi lens right
18630
11:10:01,840 --> 11:10:05,040
we're making progress on our app already
18631
11:10:05,040 --> 11:10:06,880
cool
18632
11:10:06,880 --> 11:10:08,840
awesome so now we need to do
18633
11:10:08,840 --> 11:10:11,680
strike and heal and strike and heal is
18634
11:10:11,680 --> 11:10:13,436
from the player class right so remember
18635
11:10:13,436 --> 11:10:16,240
this is the player object and methods
18636
11:10:16,240 --> 11:10:18,720
are functions within the class right so
18637
11:10:18,720 --> 11:10:21,040
when i never think about methods methods
18638
11:10:21,040 --> 11:10:22,800
are just what that object is supposed to
18639
11:10:22,800 --> 11:10:24,320
do so what is the player object supposed
18640
11:10:24,320 --> 11:10:26,800
to do it's supposed to strike an attack
18641
11:10:26,800 --> 11:10:29,520
right now it can also heal up so that's
18642
11:10:29,520 --> 11:10:31,916
why these two methods are strike
18643
11:10:31,916 --> 11:10:33,916
and heal all right you guys are doing
18644
11:10:33,916 --> 11:10:35,840
great so far right that first function
18645
11:10:35,840 --> 11:10:38,000
wasn't bad right this thing should be
18646
11:10:38,000 --> 11:10:40,560
more familiar this this method
18647
11:10:40,560 --> 11:10:41,520
okay
18648
11:10:41,520 --> 11:10:43,040
it's kind of like look at this as like a
18649
11:10:43,040 --> 11:10:44,720
big review of everything you learned so
18650
11:10:44,720 --> 11:10:46,720
far and don't worry if it doesn't make
18651
11:10:46,720 --> 11:10:49,360
sense take your time pause the video
18652
11:10:49,360 --> 11:10:52,560
take a break take a walk right the cool
18653
11:10:52,560 --> 11:10:54,240
part about this being a youtube video is
18654
11:10:54,240 --> 11:10:56,560
that you can take it at a pace that you
18655
11:10:56,560 --> 11:10:58,000
want
18656
11:10:58,000 --> 11:10:59,040
so
18657
11:10:59,040 --> 11:11:01,840
with let damage amount right so this is
18658
11:11:01,840 --> 11:11:04,320
the comment here strike is attacking an
18659
11:11:04,320 --> 11:11:07,116
enemy from a random number between zero
18660
11:11:07,116 --> 11:11:11,436
and your attack damage bonus okay
18661
11:11:11,436 --> 11:11:13,276
so what we're going to need to do
18662
11:11:13,276 --> 11:11:15,520
is
18663
11:11:15,520 --> 11:11:17,840
get a random number between 1 and 10 and
18664
11:11:17,840 --> 11:11:20,240
that is the damage amount right so let's
18665
11:11:20,240 --> 11:11:21,840
do that so how do we get a random number
18666
11:11:21,840 --> 11:11:23,520
guys do you remember
18667
11:11:23,520 --> 11:11:25,916
to get a random number we're gonna use
18668
11:11:25,916 --> 11:11:27,680
math.random
18669
11:11:27,680 --> 11:11:30,240
right and what i wanna what i wanna do
18670
11:11:30,240 --> 11:11:32,720
is store that in a variable so let's
18671
11:11:32,720 --> 11:11:34,400
make a variable let's call it damage
18672
11:11:34,400 --> 11:11:36,320
amount all right and we'll set this
18673
11:11:36,320 --> 11:11:37,880
equal to
18674
11:11:37,880 --> 11:11:39,520
math.random
18675
11:11:39,520 --> 11:11:41,116
right
18676
11:11:41,116 --> 11:11:44,000
and then if i do times
18677
11:11:44,000 --> 11:11:45,520
10 that's going to give me a random
18678
11:11:45,520 --> 11:11:47,436
number between 1 and 10.
18679
11:11:47,436 --> 11:11:48,960
right
18680
11:11:48,960 --> 11:11:51,200
and more specifically every single
18681
11:11:51,200 --> 11:11:52,480
number between one and then so that
18682
11:11:52,480 --> 11:11:58,240
means 1.5 1.6 1.7 1.10
18683
11:11:58,240 --> 11:11:59,680
do you want to do
18684
11:11:59,680 --> 11:12:03,596
0.5 damage to causey or lance no so
18685
11:12:03,596 --> 11:12:05,116
there's two ways we could go about it we
18686
11:12:05,116 --> 11:12:06,320
could do
18687
11:12:06,320 --> 11:12:10,240
math.ceiling to round down or math.floor
18688
11:12:10,240 --> 11:12:11,916
sorry math.ceiling to round up and map
18689
11:12:11,916 --> 11:12:14,480
that floor to round down right and if i
18690
11:12:14,480 --> 11:12:16,960
want to make it to 10 right let's use
18691
11:12:16,960 --> 11:12:20,240
math.seal so math.seal is how you use it
18692
11:12:20,240 --> 11:12:22,240
put that in parentheses
18693
11:12:22,240 --> 11:12:24,160
and there we go so
18694
11:12:24,160 --> 11:12:27,200
if i asked you guys what does line 75 do
18695
11:12:27,200 --> 11:12:29,756
it gets a random number between one and
18696
11:12:29,756 --> 11:12:30,720
ten
18697
11:12:30,720 --> 11:12:32,720
cool and we store that random number
18698
11:12:32,720 --> 11:12:34,960
let's say it's seven and put it in
18699
11:12:34,960 --> 11:12:37,116
damage amount
18700
11:12:37,116 --> 11:12:38,160
all right
18701
11:12:38,160 --> 11:12:41,276
so how can we take that damage amount
18702
11:12:41,276 --> 11:12:42,320
okay
18703
11:12:42,320 --> 11:12:45,756
and subtract the enemy's health by it
18704
11:12:45,756 --> 11:12:47,756
right so let's say i'm quasi
18705
11:12:47,756 --> 11:12:51,116
i'm punching or striking lance myself
18706
11:12:51,116 --> 11:12:53,756
and for seven damage right
18707
11:12:53,756 --> 11:12:56,080
how can i reflect that on
18708
11:12:56,080 --> 11:12:58,480
lance's health that i lost seven damage
18709
11:12:58,480 --> 11:13:00,000
well we have to subtract it with the
18710
11:13:00,000 --> 11:13:02,160
damage amount so to get the how do i get
18711
11:13:02,160 --> 11:13:03,596
the enemy health then that's probably a
18712
11:13:03,596 --> 11:13:04,880
question you have
18713
11:13:04,880 --> 11:13:06,800
well again you have to ask yourself do i
18714
11:13:06,800 --> 11:13:08,720
have access right that's kind of like
18715
11:13:08,720 --> 11:13:10,640
the key here do i have access to the
18716
11:13:10,640 --> 11:13:13,040
enemy health if i look at the
18717
11:13:13,040 --> 11:13:14,560
parentheses here
18718
11:13:14,560 --> 11:13:16,880
yes i do it's an under enemy so if i
18719
11:13:16,880 --> 11:13:18,720
were to console log enemy and test this
18720
11:13:18,720 --> 11:13:20,720
function out right that's going to be
18721
11:13:20,720 --> 11:13:22,240
the player
18722
11:13:22,240 --> 11:13:24,080
object right or the one who's getting
18723
11:13:24,080 --> 11:13:25,680
attacked
18724
11:13:25,680 --> 11:13:27,756
in this case me
18725
11:13:27,756 --> 11:13:30,880
so what i could do is say enemy right
18726
11:13:30,880 --> 11:13:33,276
and how do i get the health of a player
18727
11:13:33,276 --> 11:13:35,680
exactly.health right this is not new you
18728
11:13:35,680 --> 11:13:38,240
got it guys that health right and how do
18729
11:13:38,240 --> 11:13:41,116
i subtract damage amount by it well
18730
11:13:41,116 --> 11:13:43,680
it's going to be minus equals damage
18731
11:13:43,680 --> 11:13:46,480
amount right subtract enemy health by
18732
11:13:46,480 --> 11:13:48,080
the damage amount
18733
11:13:48,080 --> 11:13:50,080
not too bad right cool
18734
11:13:50,080 --> 11:13:52,080
all right so then
18735
11:13:52,080 --> 11:13:54,320
if i did the damage already what should
18736
11:13:54,320 --> 11:13:56,160
i do next
18737
11:13:56,160 --> 11:13:58,240
i should update the game because kazi
18738
11:13:58,240 --> 11:13:59,840
can punch me
18739
11:13:59,840 --> 11:14:02,160
but it's not gonna get reflected on here
18740
11:14:02,160 --> 11:14:03,520
unless i
18741
11:14:03,520 --> 11:14:06,880
update the game so let's update the game
18742
11:14:06,880 --> 11:14:09,596
by saying
18743
11:14:09,596 --> 11:14:11,916
by saying um what's it called
18744
11:14:11,916 --> 11:14:13,040
player
18745
11:14:13,040 --> 11:14:14,400
not player
18746
11:14:14,400 --> 11:14:15,756
then we could say
18747
11:14:15,756 --> 11:14:18,080
oops
18748
11:14:18,960 --> 11:14:20,000
player
18749
11:14:20,000 --> 11:14:22,560
then we could say enemy
18750
11:14:22,560 --> 11:14:24,560
and then we could say
18751
11:14:24,560 --> 11:14:26,080
the state of the game which is just
18752
11:14:26,080 --> 11:14:28,880
going to be game state right or in other
18753
11:14:28,880 --> 11:14:32,320
words game dot is over
18754
11:14:32,320 --> 11:14:34,720
perfect
18755
11:14:34,720 --> 11:14:36,240
all right and the last thing to need to
18756
11:14:36,240 --> 11:14:38,640
do is return
18757
11:14:38,640 --> 11:14:39,916
a message
18758
11:14:39,916 --> 11:14:41,200
right and basically the message is going
18759
11:14:41,200 --> 11:14:43,756
to say like qazi attacks lands for this
18760
11:14:43,756 --> 11:14:46,320
much damage right and if you remember
18761
11:14:46,320 --> 11:14:47,520
there's something called template
18762
11:14:47,520 --> 11:14:49,360
literals where we can make this dynamic
18763
11:14:49,360 --> 11:14:52,080
we could say player dot name which is
18764
11:14:52,080 --> 11:14:55,040
going to be kazi attacks
18765
11:14:55,040 --> 11:14:56,640
template literal
18766
11:14:56,640 --> 11:14:59,360
lance and get the name
18767
11:14:59,360 --> 11:15:01,840
for and how do i get the amount of
18768
11:15:01,840 --> 11:15:04,320
damage well luckily thanks to the
18769
11:15:04,320 --> 11:15:06,080
variable name you can just say damage
18770
11:15:06,080 --> 11:15:07,040
amount
18771
11:15:07,040 --> 11:15:08,720
perfect
18772
11:15:08,720 --> 11:15:09,596
cool
18773
11:15:09,596 --> 11:15:11,276
so we should have strike and i know
18774
11:15:11,276 --> 11:15:12,800
you're gonna say attack damage is not
18775
11:15:12,800 --> 11:15:14,800
used but i'm gonna come back to it if
18776
11:15:14,800 --> 11:15:16,560
this makes sense to you guys
18777
11:15:16,560 --> 11:15:19,756
okay cool
18778
11:15:19,756 --> 11:15:21,200
all right so let's test the function out
18779
11:15:21,200 --> 11:15:22,480
and see if it works
18780
11:15:22,480 --> 11:15:24,480
so we can hard code that function and
18781
11:15:24,480 --> 11:15:26,560
call it right here remember p1 is player
18782
11:15:26,560 --> 11:15:29,916
one so how do i use the strike method
18783
11:15:29,916 --> 11:15:32,720
of this player right if you remember a
18784
11:15:32,720 --> 11:15:35,520
method it's just a function inside of an
18785
11:15:35,520 --> 11:15:37,756
object so we could just say dodge strike
18786
11:15:37,756 --> 11:15:39,276
and remember i can't just write dot
18787
11:15:39,276 --> 11:15:41,756
strike i have to pass in what
18788
11:15:41,756 --> 11:15:43,276
i have to pass in
18789
11:15:43,276 --> 11:15:44,840
dun
18790
11:15:44,840 --> 11:15:48,560
dun the player the enemy and how much
18791
11:15:48,560 --> 11:15:49,596
damage
18792
11:15:49,596 --> 11:15:50,720
right
18793
11:15:50,720 --> 11:15:52,160
so
18794
11:15:52,160 --> 11:15:54,480
what i'll put here is
18795
11:15:54,480 --> 11:15:56,720
player one right that's the player the
18796
11:15:56,720 --> 11:15:59,276
enemy is gonna be player two and the
18797
11:15:59,276 --> 11:16:01,520
attack damage is going to be 10.
18798
11:16:01,520 --> 11:16:03,596
remember i'm not using attack damage yet
18799
11:16:03,596 --> 11:16:05,200
right
18800
11:16:05,200 --> 11:16:07,680
so let's run this function and see what
18801
11:16:07,680 --> 11:16:11,360
happens boom 91 right so how much damage
18802
11:16:11,360 --> 11:16:13,756
did kazi do to me he did nine damage if
18803
11:16:13,756 --> 11:16:14,800
i run
18804
11:16:14,800 --> 11:16:17,276
he did one damage to me right see how
18805
11:16:17,276 --> 11:16:19,200
it's a random number each time the
18806
11:16:19,200 --> 11:16:23,680
strike is working perfectly right so
18807
11:16:23,680 --> 11:16:25,200
we can do console log two we can
18808
11:16:25,200 --> 11:16:26,960
console.log p1 strike to see what it
18809
11:16:26,960 --> 11:16:28,640
returns to us
18810
11:16:28,640 --> 11:16:29,840
is it run
18811
11:16:29,840 --> 11:16:32,880
92 and it says qazi attacks lance for
18812
11:16:32,880 --> 11:16:34,240
eight all right so we are missing
18813
11:16:34,240 --> 11:16:35,520
something
18814
11:16:35,520 --> 11:16:37,200
that's why you always want a console log
18815
11:16:37,200 --> 11:16:38,800
to make sure you're not missing anything
18816
11:16:38,800 --> 11:16:40,840
so let's see for
18817
11:16:40,840 --> 11:16:43,680
eight didn't say damage all right so
18818
11:16:43,680 --> 11:16:44,960
that looks more
18819
11:16:44,960 --> 11:16:47,116
uh like an actual game message so it's a
18820
11:16:47,116 --> 11:16:47,916
run
18821
11:16:47,916 --> 11:16:50,560
kazi attacks lands for nine damage right
18822
11:16:50,560 --> 11:16:52,960
so this is perfect this is the strike
18823
11:16:52,960 --> 11:16:56,800
function already set up right cool so
18824
11:16:56,800 --> 11:16:58,720
now let's talk about uh the attack
18825
11:16:58,720 --> 11:17:01,520
damage argument
18826
11:17:01,520 --> 11:17:03,840
so remember i'm not using this
18827
11:17:03,840 --> 11:17:05,200
right
18828
11:17:05,200 --> 11:17:09,596
but this has the same name as a player
18829
11:17:09,596 --> 11:17:11,520
as the player attack damage right so at
18830
11:17:11,520 --> 11:17:13,116
the moment we're getting a random number
18831
11:17:13,116 --> 11:17:14,800
between 1 and 10
18832
11:17:14,800 --> 11:17:16,480
right but you guys should be able to
18833
11:17:16,480 --> 11:17:18,800
understand this if i remove this 10 and
18834
11:17:18,800 --> 11:17:20,480
said attack
18835
11:17:20,480 --> 11:17:23,200
damage right what did i just do
18836
11:17:23,200 --> 11:17:25,520
what's going to be the random number
18837
11:17:25,520 --> 11:17:27,680
if you said i don't know right
18838
11:17:27,680 --> 11:17:29,116
that's going to be true because it's
18839
11:17:29,116 --> 11:17:32,160
dynamic right why is it dynamic because
18840
11:17:32,160 --> 11:17:35,040
if i put
18841
11:17:35,436 --> 11:17:36,800
let's say i put
18842
11:17:36,800 --> 11:17:40,800
25 here right as attack damage
18843
11:17:40,800 --> 11:17:42,800
what that means is
18844
11:17:42,800 --> 11:17:44,800
if i go over here
18845
11:17:44,800 --> 11:17:47,200
i'm now getting a random number between
18846
11:17:47,200 --> 11:17:48,160
1
18847
11:17:48,160 --> 11:17:49,276
and
18848
11:17:49,276 --> 11:17:51,916
25 or whatever i put as attack damage
18849
11:17:51,916 --> 11:17:53,680
let's see if that's right let's hit run
18850
11:17:53,680 --> 11:17:56,080
11 damage right that's big boy damage
18851
11:17:56,080 --> 11:17:59,360
that's five all right 13 right nine all
18852
11:17:59,360 --> 11:18:01,596
right two right 13. there's a lot of
18853
11:18:01,596 --> 11:18:03,116
ways to get a number but it can go as
18854
11:18:03,116 --> 11:18:05,040
high as 25 if you put it there there you
18855
11:18:05,040 --> 11:18:06,400
go that's a big one
18856
11:18:06,400 --> 11:18:07,840
23 damage
18857
11:18:07,840 --> 11:18:08,640
right
18858
11:18:08,640 --> 11:18:09,520
so
18859
11:18:09,520 --> 11:18:12,560
what i'm saying is
18860
11:18:13,040 --> 11:18:15,360
if i based it off now make it even more
18861
11:18:15,360 --> 11:18:17,596
dynamic instead of just writing 25
18862
11:18:17,596 --> 11:18:19,596
i want to say it's going to be based off
18863
11:18:19,596 --> 11:18:20,880
p1
18864
11:18:20,880 --> 11:18:21,756
dot
18865
11:18:21,756 --> 11:18:23,916
attack damage right
18866
11:18:23,916 --> 11:18:26,640
so now what is the random number between
18867
11:18:26,640 --> 11:18:29,040
all right take a look at player one the
18868
11:18:29,040 --> 11:18:32,000
random number now is between one and ten
18869
11:18:32,000 --> 11:18:32,960
again
18870
11:18:32,960 --> 11:18:35,040
right so this is the place where you're
18871
11:18:35,040 --> 11:18:36,800
going to input your character stats
18872
11:18:36,800 --> 11:18:39,520
right so in the video game you put the
18873
11:18:39,520 --> 11:18:41,756
player's name you put the health and
18874
11:18:41,756 --> 11:18:44,960
then you could say qazi can do uh 50
18875
11:18:44,960 --> 11:18:46,840
damage right let's hit
18876
11:18:46,840 --> 11:18:49,596
run run 14
18877
11:18:49,596 --> 11:18:54,880
19 23 48 damage right so as you can see
18878
11:18:54,880 --> 11:18:56,720
this is what controls how much den so
18879
11:18:56,720 --> 11:18:58,560
there's a lot of variation now that we
18880
11:18:58,560 --> 11:19:00,000
added to the game right you could give
18881
11:19:00,000 --> 11:19:01,756
someone with a thousand health and
18882
11:19:01,756 --> 11:19:03,756
someone else a thousand damage if you
18883
11:19:03,756 --> 11:19:06,240
wanted to right but for simplicity's
18884
11:19:06,240 --> 11:19:08,480
sake i'm going to keep it at 100 and i'm
18885
11:19:08,480 --> 11:19:10,880
going to keep this at 10. okay so let's
18886
11:19:10,880 --> 11:19:12,960
hit run and now it's back to 1 through
18887
11:19:12,960 --> 11:19:14,240
10.
18888
11:19:14,240 --> 11:19:15,116
cool
18889
11:19:15,116 --> 11:19:16,880
and you also know that this piece of
18890
11:19:16,880 --> 11:19:19,276
code is what makes me or makes quasi do
18891
11:19:19,276 --> 11:19:20,640
the punch all right if you wanted to
18892
11:19:20,640 --> 11:19:23,116
change it around to me doing the punch
18893
11:19:23,116 --> 11:19:25,916
back to kazi you say p2.strike
18894
11:19:25,916 --> 11:19:28,960
change this player to p2 change this to
18895
11:19:28,960 --> 11:19:31,200
p1 and take in
18896
11:19:31,200 --> 11:19:33,436
player 2's damage
18897
11:19:33,436 --> 11:19:34,640
yeah
18898
11:19:34,640 --> 11:19:36,960
cool so the way i would like to
18899
11:19:36,960 --> 11:19:39,916
understand this app is remember the bits
18900
11:19:39,916 --> 11:19:42,000
and pieces of this app and figure out
18901
11:19:42,000 --> 11:19:43,916
ways to we can put it together and when
18902
11:19:43,916 --> 11:19:46,880
we want to put together right so
18903
11:19:46,880 --> 11:19:48,400
we don't we can erase this because we
18904
11:19:48,400 --> 11:19:50,640
get how it works now if i hit run right
18905
11:19:50,640 --> 11:19:51,916
nothing happens
18906
11:19:51,916 --> 11:19:54,720
right because i only want to call this
18907
11:19:54,720 --> 11:19:56,880
strike function at a certain point but
18908
11:19:56,880 --> 11:19:58,800
we'll get back to that later so that's
18909
11:19:58,800 --> 11:20:00,480
the strike
18910
11:20:00,480 --> 11:20:02,160
right i'm gonna move a little bit faster
18911
11:20:02,160 --> 11:20:04,720
because the heel
18912
11:20:04,720 --> 11:20:07,200
is actually the exact same thing except
18913
11:20:07,200 --> 11:20:09,520
no one takes one argument just who the
18914
11:20:09,520 --> 11:20:12,480
player that's gonna heal right so if
18915
11:20:12,480 --> 11:20:14,000
cause is healing
18916
11:20:14,000 --> 11:20:15,916
we're going to put causes information in
18917
11:20:15,916 --> 11:20:16,800
here
18918
11:20:16,800 --> 11:20:19,116
okay so the first thing is get a random
18919
11:20:19,116 --> 11:20:21,756
number between one and five let's do it
18920
11:20:21,756 --> 11:20:23,596
so let's call it hp amount and let's get
18921
11:20:23,596 --> 11:20:25,520
a random number between one and five
18922
11:20:25,520 --> 11:20:27,276
well that's going to equal
18923
11:20:27,276 --> 11:20:28,400
right
18924
11:20:28,400 --> 11:20:31,400
math.random
18925
11:20:31,680 --> 11:20:32,560
times
18926
11:20:32,560 --> 11:20:36,560
5 right but this is going to be 0
18927
11:20:36,560 --> 11:20:38,320
through 5 and it's going to be the
18928
11:20:38,320 --> 11:20:39,680
decimal places so we want to say
18929
11:20:39,680 --> 11:20:41,680
math.seal
18930
11:20:41,680 --> 11:20:43,916
right
18931
11:20:44,400 --> 11:20:46,160
and there we go
18932
11:20:46,160 --> 11:20:47,840
all right that's the hp amount how do we
18933
11:20:47,840 --> 11:20:49,840
add the hp amount to the player's health
18934
11:20:49,840 --> 11:20:52,000
well we can just say player right if you
18935
11:20:52,000 --> 11:20:53,040
remember
18936
11:20:53,040 --> 11:20:54,480
this up here
18937
11:20:54,480 --> 11:20:57,116
it's going to be plus equals
18938
11:20:57,116 --> 11:20:58,080
player
18939
11:20:58,080 --> 11:21:00,240
dot health well not player health sorry
18940
11:21:00,240 --> 11:21:03,116
player dot health sorry is going to be
18941
11:21:03,116 --> 11:21:06,880
plus equals the hp amount
18942
11:21:06,880 --> 11:21:10,320
okie dokie so we got player.health plus
18943
11:21:10,320 --> 11:21:11,916
equals hpm
18944
11:21:11,916 --> 11:21:12,800
cool
18945
11:21:12,800 --> 11:21:14,000
so after we
18946
11:21:14,000 --> 11:21:16,720
updated the health in the background
18947
11:21:16,720 --> 11:21:18,800
right we need to update the front end
18948
11:21:18,800 --> 11:21:20,000
with it so
18949
11:21:20,000 --> 11:21:21,436
our phone was only a way that we could
18950
11:21:21,436 --> 11:21:23,840
do that it's with update game so update
18951
11:21:23,840 --> 11:21:25,840
game it's going to take in
18952
11:21:25,840 --> 11:21:28,640
the player right
18953
11:21:28,640 --> 11:21:29,916
and also
18954
11:21:29,916 --> 11:21:31,520
p2
18955
11:21:31,520 --> 11:21:34,480
and also game state
18956
11:21:34,480 --> 11:21:36,240
all right which is also just game that
18957
11:21:36,240 --> 11:21:38,480
is over
18958
11:21:38,480 --> 11:21:40,960
perfect
18959
11:21:41,840 --> 11:21:43,200
okay and then we're gonna return a nice
18960
11:21:43,200 --> 11:21:44,720
message so that we could see it which
18961
11:21:44,720 --> 11:21:46,800
can be similar to before which is
18962
11:21:46,800 --> 11:21:48,080
backticks
18963
11:21:48,080 --> 11:21:48,880
right
18964
11:21:48,880 --> 11:21:51,040
it's going to read qazi
18965
11:21:51,040 --> 11:21:52,560
heals for
18966
11:21:52,560 --> 11:21:55,360
5 hp for example so to get kazzy's name
18967
11:21:55,360 --> 11:21:58,400
we say player.name and then we can say
18968
11:21:58,400 --> 11:22:01,520
heals for
18969
11:22:01,596 --> 11:22:05,400
the hp amount
18970
11:22:05,756 --> 11:22:07,680
and then you could say hp exclamation
18971
11:22:07,680 --> 11:22:10,000
point perfect now let's test if this
18972
11:22:10,000 --> 11:22:12,320
works
18973
11:22:12,880 --> 11:22:14,560
okay so
18974
11:22:14,560 --> 11:22:17,200
to test if it works we're gonna get p1
18975
11:22:17,200 --> 11:22:19,200
how do i get the heal method
18976
11:22:19,200 --> 11:22:20,560
dot heal
18977
11:22:20,560 --> 11:22:22,560
what arguments does it take
18978
11:22:22,560 --> 11:22:25,116
it's gonna take in who's healing
18979
11:22:25,116 --> 11:22:27,360
right and i hit run
18980
11:22:27,360 --> 11:22:29,040
i healed for 103. let's see if our
18981
11:22:29,040 --> 11:22:32,320
message works
18982
11:22:32,320 --> 11:22:35,840
so let's console log the return
18983
11:22:36,640 --> 11:22:38,800
kazi heals for three hp that's perfect
18984
11:22:38,800 --> 11:22:43,360
all right 101 104 101 right so this
18985
11:22:43,360 --> 11:22:44,960
piece of code
18986
11:22:44,960 --> 11:22:47,916
is how a player can heal right
18987
11:22:47,916 --> 11:22:49,680
so there you go and with that if you
18988
11:22:49,680 --> 11:22:51,436
wanted to make player two heal just say
18989
11:22:51,436 --> 11:22:55,200
p2.heal and p2 right
18990
11:22:55,200 --> 11:22:58,320
the benefit of making something dynamic
18991
11:22:58,320 --> 11:23:00,720
is that you can heal like this
18992
11:23:00,720 --> 11:23:02,960
okay
18993
11:23:04,720 --> 11:23:07,680
oh i see i see
18994
11:23:07,680 --> 11:23:09,040
speaking of making a dynamic i made it a
18995
11:23:09,040 --> 11:23:11,040
little too dynamic it's a good thing we
18996
11:23:11,040 --> 11:23:12,240
tested this
18997
11:23:12,240 --> 11:23:16,640
so go back to the heel
18998
11:23:16,640 --> 11:23:18,720
yes this could just be p1 p2 right
18999
11:23:18,720 --> 11:23:20,160
because those are the variables that
19000
11:23:20,160 --> 11:23:22,320
hold the player names so now i've hit
19001
11:23:22,320 --> 11:23:25,040
run there you go 100 101. now let's only
19002
11:23:25,040 --> 11:23:28,756
lance heals for four
19003
11:23:28,880 --> 11:23:30,480
so remember this is how a player can
19004
11:23:30,480 --> 11:23:32,240
heal depending on who wants to heal you
19005
11:23:32,240 --> 11:23:34,800
just change it from p1 to p2
19006
11:23:34,800 --> 11:23:37,756
so erase that
19007
11:23:37,756 --> 11:23:40,240
and guess what guys
19008
11:23:40,240 --> 11:23:43,680
we finish the player class let's go so
19009
11:23:43,680 --> 11:23:46,160
the player class is now done and let's
19010
11:23:46,160 --> 11:23:48,080
say the game is pretty much half done
19011
11:23:48,080 --> 11:23:50,640
too right we already can strike we can
19012
11:23:50,640 --> 11:23:52,000
also heal
19013
11:23:52,000 --> 11:23:53,116
cool
19014
11:23:53,116 --> 11:23:54,320
so now
19015
11:23:54,320 --> 11:23:58,560
let's work on the game class
19016
11:23:58,800 --> 11:24:00,960
all right so remember refresh on the
19017
11:24:00,960 --> 11:24:03,916
game class the game object represents
19018
11:24:03,916 --> 11:24:06,160
the whole entire game and within that
19019
11:24:06,160 --> 11:24:08,800
game right we are using strike and heal
19020
11:24:08,800 --> 11:24:10,240
right but the only thing that we care
19021
11:24:10,240 --> 11:24:12,800
about with the game is checking if it's
19022
11:24:12,800 --> 11:24:13,756
over
19023
11:24:13,756 --> 11:24:15,200
right which it should be false at the
19024
11:24:15,200 --> 11:24:16,240
beginning
19025
11:24:16,240 --> 11:24:18,880
uh declaring the winner right after we
19026
11:24:18,880 --> 11:24:20,480
update the game we have to say like oh
19027
11:24:20,480 --> 11:24:24,320
qazi wins or oh lance wins right we also
19028
11:24:24,320 --> 11:24:25,916
want to be able to reset that's what
19029
11:24:25,916 --> 11:24:27,360
this button is for
19030
11:24:27,360 --> 11:24:29,436
but let's say we're all at zero hp if i
19031
11:24:29,436 --> 11:24:31,840
hit reset we should be able to restart
19032
11:24:31,840 --> 11:24:33,520
our health back to normal
19033
11:24:33,520 --> 11:24:35,200
set is over back to false and then we
19034
11:24:35,200 --> 11:24:36,720
can fight again
19035
11:24:36,720 --> 11:24:37,436
right
19036
11:24:37,436 --> 11:24:39,040
then there's also this bonus function
19037
11:24:39,040 --> 11:24:42,480
called play right and play
19038
11:24:42,480 --> 11:24:45,116
is gonna make it so that we can simulate
19039
11:24:45,116 --> 11:24:48,640
the whole match okay
19040
11:24:48,640 --> 11:24:50,000
so you don't even have to play the game
19041
11:24:50,000 --> 11:24:51,520
you can kind of just simulate what would
19042
11:24:51,520 --> 11:24:53,116
happen right by pressing the simulate
19043
11:24:53,116 --> 11:24:54,080
button
19044
11:24:54,080 --> 11:24:54,960
okay
19045
11:24:54,960 --> 11:24:56,880
so let's work on that and if we do that
19046
11:24:56,880 --> 11:24:58,720
the game is pretty much done
19047
11:24:58,720 --> 11:25:00,080
right we just need to add like little
19048
11:25:00,080 --> 11:25:02,720
details like sound effects so
19049
11:25:02,720 --> 11:25:04,640
starting with declare winner if the game
19050
11:25:04,640 --> 11:25:06,800
is over and a player health has zero
19051
11:25:06,800 --> 11:25:10,080
health declare the winner right so let's
19052
11:25:10,080 --> 11:25:13,520
do that let's create a message variable
19053
11:25:13,520 --> 11:25:14,880
that's the first thing right here so
19054
11:25:14,880 --> 11:25:15,916
let's say
19055
11:25:15,916 --> 11:25:16,960
let
19056
11:25:16,960 --> 11:25:18,080
message
19057
11:25:18,080 --> 11:25:19,520
and that's it
19058
11:25:19,520 --> 11:25:20,480
okay
19059
11:25:20,480 --> 11:25:22,800
it's going to hold a different message
19060
11:25:22,800 --> 11:25:24,800
based on the condition right so that
19061
11:25:24,800 --> 11:25:25,680
means
19062
11:25:25,680 --> 11:25:27,680
if you look at this it says if is over
19063
11:25:27,680 --> 11:25:28,720
is true
19064
11:25:28,720 --> 11:25:29,596
and
19065
11:25:29,596 --> 11:25:32,400
qazi health is less than equal to zero
19066
11:25:32,400 --> 11:25:36,080
then update the message variable to qazi
19067
11:25:36,080 --> 11:25:37,276
wins
19068
11:25:37,276 --> 11:25:38,480
right so
19069
11:25:38,480 --> 11:25:40,160
there i kind of tell you guys how to
19070
11:25:40,160 --> 11:25:43,360
write this exact thing you say if
19071
11:25:43,360 --> 11:25:44,320
right
19072
11:25:44,320 --> 11:25:46,560
code block
19073
11:25:46,560 --> 11:25:48,960
and read this and translate it to code
19074
11:25:48,960 --> 11:25:50,160
well if
19075
11:25:50,160 --> 11:25:52,240
is over
19076
11:25:52,240 --> 11:25:53,436
right
19077
11:25:53,436 --> 11:25:55,116
is over
19078
11:25:55,116 --> 11:25:57,200
equals equals true all right and how
19079
11:25:57,200 --> 11:25:59,116
come i don't have to write game dot is
19080
11:25:59,116 --> 11:26:00,400
over
19081
11:26:00,400 --> 11:26:01,360
right
19082
11:26:01,360 --> 11:26:03,276
well take a look do i have access to
19083
11:26:03,276 --> 11:26:06,560
just is over yes i do right over here it
19084
11:26:06,560 --> 11:26:09,756
takes in is over p1 p2 so i can just
19085
11:26:09,756 --> 11:26:11,916
call it freely like so
19086
11:26:11,916 --> 11:26:14,640
cool so if is over is true
19087
11:26:14,640 --> 11:26:15,520
right
19088
11:26:15,520 --> 11:26:17,436
and
19089
11:26:17,436 --> 11:26:20,000
p1 dot health is
19090
11:26:20,000 --> 11:26:22,080
less than or equal to zero right in
19091
11:26:22,080 --> 11:26:24,320
other words if the game is over
19092
11:26:24,320 --> 11:26:26,800
and causes health to zero right
19093
11:26:26,800 --> 11:26:28,960
then i want to make the message right
19094
11:26:28,960 --> 11:26:32,080
update the message to say
19095
11:26:32,800 --> 11:26:35,360
backticks
19096
11:26:37,360 --> 11:26:38,800
p
19097
11:26:38,800 --> 11:26:41,040
2 dot name
19098
11:26:41,040 --> 11:26:45,840
wins why do i want to say p2.name wins
19099
11:26:45,916 --> 11:26:47,756
all right think about it for a second
19100
11:26:47,756 --> 11:26:50,880
if the player wants health is zero
19101
11:26:50,880 --> 11:26:52,160
who actually won
19102
11:26:52,160 --> 11:26:53,916
player two all right kazi's health is
19103
11:26:53,916 --> 11:26:57,276
zero then i win lance wins all right
19104
11:26:57,276 --> 11:26:59,840
next is the elsif right i'm going to
19105
11:26:59,840 --> 11:27:01,596
write it just right over here
19106
11:27:01,596 --> 11:27:04,400
else if
19107
11:27:04,880 --> 11:27:06,160
which is the other what's the other
19108
11:27:06,160 --> 11:27:08,080
scenario in this case well the other
19109
11:27:08,080 --> 11:27:10,960
scenario is is over
19110
11:27:10,960 --> 11:27:12,880
equal equal true
19111
11:27:12,880 --> 11:27:15,596
and right and means both conditions have
19112
11:27:15,596 --> 11:27:17,480
to be true
19113
11:27:17,480 --> 11:27:19,276
p2.health
19114
11:27:19,276 --> 11:27:23,276
is equal less than or equal to zero
19115
11:27:23,520 --> 11:27:25,756
okay and if that's true right
19116
11:27:25,756 --> 11:27:28,480
we want it so that the message
19117
11:27:28,480 --> 11:27:29,520
says
19118
11:27:29,520 --> 11:27:31,200
p1 wins
19119
11:27:31,200 --> 11:27:34,400
yeah so the message is gonna say p
19120
11:27:34,400 --> 11:27:35,916
one
19121
11:27:35,916 --> 11:27:37,596
dot name
19122
11:27:37,596 --> 11:27:38,880
wins
19123
11:27:38,880 --> 11:27:41,360
perfect okay and then if i want i could
19124
11:27:41,360 --> 11:27:44,880
just take this and put it over here
19125
11:27:45,200 --> 11:27:47,596
perfect
19126
11:27:47,596 --> 11:27:49,756
cool and after that we also have a
19127
11:27:49,756 --> 11:27:52,240
victory sound that i want to play so
19128
11:27:52,240 --> 11:27:54,560
if you look in the html
19129
11:27:54,560 --> 11:27:56,320
and you remember the prep course that we
19130
11:27:56,320 --> 11:27:58,640
did together all the sounds are set up
19131
11:27:58,640 --> 11:28:00,800
right here right we have p1 attack p1
19132
11:28:00,800 --> 11:28:04,560
heal p2 attack p2 heal and victory so
19133
11:28:04,560 --> 11:28:06,480
and all the sounds are loaded up on here
19134
11:28:06,480 --> 11:28:09,116
as well so we want to play is that
19135
11:28:09,116 --> 11:28:11,360
victory sound over here
19136
11:28:11,360 --> 11:28:12,080
so
19137
11:28:12,080 --> 11:28:14,640
let's play that victory sound
19138
11:28:14,640 --> 11:28:16,160
and remember to play the victory sound
19139
11:28:16,160 --> 11:28:18,160
we just do document
19140
11:28:18,160 --> 11:28:19,116
get the
19141
11:28:19,116 --> 11:28:21,436
element by id
19142
11:28:21,436 --> 11:28:23,276
which is just going to be
19143
11:28:23,276 --> 11:28:26,000
victory
19144
11:28:26,480 --> 11:28:28,960
and we're going to hit dot play
19145
11:28:28,960 --> 11:28:29,756
right
19146
11:28:29,756 --> 11:28:31,040
and then
19147
11:28:31,040 --> 11:28:32,560
since we have the message variable we
19148
11:28:32,560 --> 11:28:34,000
can just say message
19149
11:28:34,000 --> 11:28:36,960
all right make that really simple cool
19150
11:28:36,960 --> 11:28:39,680
and now we can declare the winner right
19151
11:28:39,680 --> 11:28:42,080
this function is now up and running
19152
11:28:42,080 --> 11:28:44,160
let's see what's what's going on here a
19153
11:28:44,160 --> 11:28:45,916
message okay good this function is now
19154
11:28:45,916 --> 11:28:47,116
up and running
19155
11:28:47,116 --> 11:28:51,840
and if you remember update game function
19156
11:28:51,840 --> 11:28:54,400
let's see where is it perfect in update
19157
11:28:54,400 --> 11:28:56,800
game function
19158
11:28:56,800 --> 11:28:58,560
i said game
19159
11:28:58,560 --> 11:29:00,080
dot declare winner because that's the
19160
11:29:00,080 --> 11:29:01,756
method of the game
19161
11:29:01,756 --> 11:29:04,240
and we passed it in and that's going to
19162
11:29:04,240 --> 11:29:06,640
hold the message so what is going to
19163
11:29:06,640 --> 11:29:08,160
show up in the inner text of this
19164
11:29:08,160 --> 11:29:09,840
resultive
19165
11:29:09,840 --> 11:29:12,880
exactly it's going to show lance wins or
19166
11:29:12,880 --> 11:29:14,560
kazi wins
19167
11:29:14,560 --> 11:29:15,520
okay
19168
11:29:15,520 --> 11:29:16,640
cool
19169
11:29:16,640 --> 11:29:19,916
and we can't test it yet until until um
19170
11:29:19,916 --> 11:29:22,800
you know we get the actual attacking
19171
11:29:22,800 --> 11:29:24,720
functionality up and running
19172
11:29:24,720 --> 11:29:26,640
so why don't we do that
19173
11:29:26,640 --> 11:29:28,160
so before we get through the reset and
19174
11:29:28,160 --> 11:29:30,000
play we're gonna pause this for a second
19175
11:29:30,000 --> 11:29:31,360
and we're gonna make it so that we can
19176
11:29:31,360 --> 11:29:32,400
actually punch
19177
11:29:32,400 --> 11:29:35,200
and heal right so let's scroll
19178
11:29:35,200 --> 11:29:37,276
all the way down to the bottom
19179
11:29:37,276 --> 11:29:40,320
right to the player one controls this is
19180
11:29:40,320 --> 11:29:42,880
the meat of the project right so if you
19181
11:29:42,880 --> 11:29:45,840
remember the prep i'm using document.add
19182
11:29:45,840 --> 11:29:47,436
event listener key down right it's
19183
11:29:47,436 --> 11:29:49,596
listening for me pressing down on my
19184
11:29:49,596 --> 11:29:50,720
keyboard
19185
11:29:50,720 --> 11:29:53,756
uh the specific thing is if i press the
19186
11:29:53,756 --> 11:29:55,200
q button
19187
11:29:55,200 --> 11:29:57,756
then i want to be able to attack right
19188
11:29:57,756 --> 11:30:00,160
so let's set that up
19189
11:30:00,160 --> 11:30:04,400
so if right and how do i get the key
19190
11:30:04,400 --> 11:30:06,240
for q right it's going to come through
19191
11:30:06,240 --> 11:30:07,840
this e parameter right here which is
19192
11:30:07,840 --> 11:30:10,000
like your keyboard event so if my
19193
11:30:10,000 --> 11:30:12,160
keyboard
19194
11:30:12,160 --> 11:30:16,080
dot key right is equal equal to q that's
19195
11:30:16,080 --> 11:30:17,840
basically what i want to say here so
19196
11:30:17,840 --> 11:30:19,360
i'll say if
19197
11:30:19,360 --> 11:30:20,640
equal to
19198
11:30:20,640 --> 11:30:21,596
q
19199
11:30:21,596 --> 11:30:23,520
or if i press q
19200
11:30:23,520 --> 11:30:26,720
and right p2 dot health
19201
11:30:26,720 --> 11:30:30,160
is greater than
19202
11:30:30,640 --> 11:30:32,240
zero
19203
11:30:32,240 --> 11:30:33,520
and
19204
11:30:33,520 --> 11:30:36,000
the game dot
19205
11:30:36,000 --> 11:30:38,720
is over is equal equal to false right
19206
11:30:38,720 --> 11:30:40,400
there's three conditions here why do
19207
11:30:40,400 --> 11:30:42,240
they matter because i want to be able to
19208
11:30:42,240 --> 11:30:43,596
press q
19209
11:30:43,596 --> 11:30:46,240
but i can only press this q
19210
11:30:46,240 --> 11:30:48,000
if
19211
11:30:48,000 --> 11:30:50,480
you know player 2 is still alive and the
19212
11:30:50,480 --> 11:30:52,000
game is not over otherwise i'm just
19213
11:30:52,000 --> 11:30:53,276
going to keep beating up a dead horse
19214
11:30:53,276 --> 11:30:55,276
right we don't want to do that so
19215
11:30:55,276 --> 11:30:57,840
let's put the code block in here and
19216
11:30:57,840 --> 11:31:00,720
after striking right play the attack
19217
11:31:00,720 --> 11:31:02,320
sound that's the comment that should go
19218
11:31:02,320 --> 11:31:03,276
in here
19219
11:31:03,276 --> 11:31:05,360
right after striking then play the
19220
11:31:05,360 --> 11:31:07,756
attack sound well how do i strike well i
19221
11:31:07,756 --> 11:31:09,680
showed you guys how to strike to strike
19222
11:31:09,680 --> 11:31:11,596
you just have to say the person that's
19223
11:31:11,596 --> 11:31:13,596
striking which is player one
19224
11:31:13,596 --> 11:31:14,800
right
19225
11:31:14,800 --> 11:31:18,000
uh put in who's striking who's the enemy
19226
11:31:18,000 --> 11:31:20,640
and we wanna make sure that takes in the
19227
11:31:20,640 --> 11:31:22,080
attack damage right we're making a
19228
11:31:22,080 --> 11:31:23,680
dynamic here
19229
11:31:23,680 --> 11:31:25,360
cool
19230
11:31:25,360 --> 11:31:26,880
so let's hit run
19231
11:31:26,880 --> 11:31:28,240
if i press q
19232
11:31:28,240 --> 11:31:30,240
oops no click on the app press q
19233
11:31:30,240 --> 11:31:31,916
hey look at that
19234
11:31:31,916 --> 11:31:33,276
we're doing some damage here so if i hit
19235
11:31:33,276 --> 11:31:35,360
run again to reset it for now right i
19236
11:31:35,360 --> 11:31:36,800
also want to make a sound you know just
19237
11:31:36,800 --> 11:31:38,560
to put some impact
19238
11:31:38,560 --> 11:31:39,596
right so
19239
11:31:39,596 --> 11:31:41,360
if you look over here which one is the
19240
11:31:41,360 --> 11:31:47,360
player one attack it is p1 attack right
19241
11:31:47,756 --> 11:31:50,080
awesome i made the names nice and easy
19242
11:31:50,080 --> 11:31:52,080
for you guys so it's not too confusing i
19243
11:31:52,080 --> 11:31:53,840
know it's a lot right but you guys if
19244
11:31:53,840 --> 11:31:54,800
you guys made it to this point you're
19245
11:31:54,800 --> 11:31:56,880
doing great so far
19246
11:31:56,880 --> 11:31:59,040
so how do i grab that element you can
19247
11:31:59,040 --> 11:32:01,040
say document
19248
11:32:01,040 --> 11:32:03,360
dot get element by id
19249
11:32:03,360 --> 11:32:05,116
you can say
19250
11:32:05,116 --> 11:32:06,640
p1
19251
11:32:06,640 --> 11:32:07,916
attack
19252
11:32:07,916 --> 11:32:08,800
right
19253
11:32:08,800 --> 11:32:11,116
and you can just say dot play
19254
11:32:11,116 --> 11:32:13,200
perfect
19255
11:32:13,200 --> 11:32:15,436
cool so now let's hit run
19256
11:32:15,436 --> 11:32:19,000
and now we punch
19257
11:32:21,360 --> 11:32:22,960
all right there you go so now it works i
19258
11:32:22,960 --> 11:32:25,520
figured out i named i gave the wrong
19259
11:32:25,520 --> 11:32:28,160
audio files in here it was a dot wav
19260
11:32:28,160 --> 11:32:31,756
file so i changed it to mp3 so if you
19261
11:32:31,756 --> 11:32:33,756
fork this rebel at the time watching
19262
11:32:33,756 --> 11:32:36,160
this video it should say mp3 and it
19263
11:32:36,160 --> 11:32:37,596
should be good to go like you don't do
19264
11:32:37,596 --> 11:32:39,520
anything different code wise so let's
19265
11:32:39,520 --> 11:32:41,916
test it out hit run
19266
11:32:41,916 --> 11:32:44,160
hey there you go a nice crisp punching
19267
11:32:44,160 --> 11:32:46,320
sound i'm like literally punching myself
19268
11:32:46,320 --> 11:32:48,080
and that's the funny part about this so
19269
11:32:48,080 --> 11:32:49,840
cause he's punching lance doing random
19270
11:32:49,840 --> 11:32:51,040
damage
19271
11:32:51,040 --> 11:32:52,240
nice
19272
11:32:52,240 --> 11:32:55,520
so the cue button works all right
19273
11:32:55,520 --> 11:32:57,276
the next thing you want to do is set up
19274
11:32:57,276 --> 11:32:58,640
the e button
19275
11:32:58,640 --> 11:32:59,436
right
19276
11:32:59,436 --> 11:33:01,360
same conditions
19277
11:33:01,360 --> 11:33:03,680
but this time it's for heal right this
19278
11:33:03,680 --> 11:33:04,720
should say
19279
11:33:04,720 --> 11:33:07,116
then heal
19280
11:33:07,116 --> 11:33:08,880
perfect okay
19281
11:33:08,880 --> 11:33:11,276
so after that's done you set up the same
19282
11:33:11,276 --> 11:33:13,360
condition if
19283
11:33:13,360 --> 11:33:16,640
e dot key is equal to where do we want
19284
11:33:16,640 --> 11:33:18,240
to make the heal button to for player
19285
11:33:18,240 --> 11:33:19,680
one
19286
11:33:19,680 --> 11:33:21,360
it's gonna be a right you could set this
19287
11:33:21,360 --> 11:33:23,756
to whatever you want right the cool
19288
11:33:23,756 --> 11:33:25,916
thing i love about this is that you can
19289
11:33:25,916 --> 11:33:28,320
add this to your own game your own
19290
11:33:28,320 --> 11:33:30,640
project right so if you understand this
19291
11:33:30,640 --> 11:33:32,400
we can reuse those pieces again and
19292
11:33:32,400 --> 11:33:34,480
again so a
19293
11:33:34,480 --> 11:33:37,116
and right b
19294
11:33:37,116 --> 11:33:39,800
one right well p2 we could also put
19295
11:33:39,800 --> 11:33:41,680
p2.health here
19296
11:33:41,680 --> 11:33:46,240
mostly because if i'm kazi and my health
19297
11:33:46,240 --> 11:33:48,560
is zero he shouldn't be healing while
19298
11:33:48,560 --> 11:33:49,756
i'm dead right we should just not be
19299
11:33:49,756 --> 11:33:53,200
able to play so p2.health is greater
19300
11:33:53,200 --> 11:33:54,320
than zero
19301
11:33:54,320 --> 11:33:55,756
then p
19302
11:33:55,756 --> 11:33:57,520
and
19303
11:33:57,520 --> 11:33:59,596
right
19304
11:33:59,596 --> 11:34:03,840
and game dot is over is equal equal to
19305
11:34:03,840 --> 11:34:06,000
false right then we shouldn't be able to
19306
11:34:06,000 --> 11:34:08,240
heal
19307
11:34:11,116 --> 11:34:13,040
all right actually
19308
11:34:13,040 --> 11:34:15,116
no no no if it is true then we should be
19309
11:34:15,116 --> 11:34:17,520
able to heal yeah yeah there you go
19310
11:34:17,520 --> 11:34:19,680
so after healing play the heel sound so
19311
11:34:19,680 --> 11:34:21,916
let's heal so to heal you guys want to
19312
11:34:21,916 --> 11:34:25,680
do this p1.heal parentheses p1
19313
11:34:25,680 --> 11:34:27,200
and then we could say
19314
11:34:27,200 --> 11:34:28,720
something like
19315
11:34:28,720 --> 11:34:30,640
uh document
19316
11:34:30,640 --> 11:34:32,480
element
19317
11:34:32,480 --> 11:34:34,880
by id
19318
11:34:34,880 --> 11:34:37,040
and i will save you guys to look it's
19319
11:34:37,040 --> 11:34:40,480
just going to be p1 here
19320
11:34:40,480 --> 11:34:42,320
save you guys some time there and how do
19321
11:34:42,320 --> 11:34:44,640
i play the sound at that dom element
19322
11:34:44,640 --> 11:34:46,880
it's going to be play so run
19323
11:34:46,880 --> 11:34:48,000
hit attack
19324
11:34:48,000 --> 11:34:49,680
that's it heal
19325
11:34:49,680 --> 11:34:50,880
oh
19326
11:34:50,880 --> 11:34:53,116
it's not playing
19327
11:34:53,116 --> 11:34:55,276
you need to double check
19328
11:34:55,276 --> 11:34:57,276
run
19329
11:34:57,276 --> 11:34:59,360
oh now it's not loading
19330
11:34:59,360 --> 11:35:03,520
okay let me try refreshing
19331
11:35:03,520 --> 11:35:07,720
oh i don't want to lose this progress
19332
11:35:14,400 --> 11:35:16,720
okay
19333
11:35:16,880 --> 11:35:19,436
let's check our progress
19334
11:35:19,436 --> 11:35:21,596
ah there it is we lost it
19335
11:35:21,596 --> 11:35:24,240
so let's put this back on here
19336
11:35:24,240 --> 11:35:26,160
the sounds are correct yep yeah see how
19337
11:35:26,160 --> 11:35:28,000
it says mp3 now before if you looked at
19338
11:35:28,000 --> 11:35:29,200
the video it says
19339
11:35:29,200 --> 11:35:33,360
wav it should be mp3
19340
11:35:33,360 --> 11:35:35,040
there we go
19341
11:35:35,040 --> 11:35:37,840
so if i press q
19342
11:35:39,360 --> 11:35:41,840
p one attack play
19343
11:35:41,840 --> 11:35:44,320
what the hell
19344
11:35:45,200 --> 11:35:47,436
okay i see what's wrong this if
19345
11:35:47,436 --> 11:35:49,040
statement
19346
11:35:49,040 --> 11:35:52,080
needs his curly brackets
19347
11:35:52,080 --> 11:35:55,200
okay cool so now we can bring this back
19348
11:35:55,200 --> 11:35:58,240
uh we can bring back this sound
19349
11:35:58,240 --> 11:36:02,000
okay cool so the strike is the same
19350
11:36:02,160 --> 11:36:05,680
the heel is what's new i took out the is
19351
11:36:05,680 --> 11:36:07,596
over false right that should actually be
19352
11:36:07,596 --> 11:36:09,360
covered over here
19353
11:36:09,360 --> 11:36:12,800
let's hit run and now it was a q
19354
11:36:12,800 --> 11:36:14,560
okay it shouldn't be playing that heel
19355
11:36:14,560 --> 11:36:17,560
sound
19356
11:36:18,080 --> 11:36:19,040
okay
19357
11:36:19,040 --> 11:36:20,320
how about heal
19358
11:36:20,320 --> 11:36:22,800
all right heel's working fine
19359
11:36:22,800 --> 11:36:25,276
now it's this guy why are you playing
19360
11:36:25,276 --> 11:36:27,840
both sounds
19361
11:36:30,400 --> 11:36:31,680
and healing
19362
11:36:31,680 --> 11:36:34,160
let's see
19363
11:36:34,240 --> 11:36:35,436
ah
19364
11:36:35,436 --> 11:36:38,080
okay i see now all right so you tell me
19365
11:36:38,080 --> 11:36:40,240
guys if you see the error and the error
19366
11:36:40,240 --> 11:36:42,560
is actually in the heel
19367
11:36:42,560 --> 11:36:43,360
all right
19368
11:36:43,360 --> 11:36:45,040
what is the difference between one equal
19369
11:36:45,040 --> 11:36:47,436
side and two equals sign so i totally
19370
11:36:47,436 --> 11:36:49,520
forgot this should have two otherwise
19371
11:36:49,520 --> 11:36:52,960
it's recognizing every key as the the
19372
11:36:52,960 --> 11:36:54,720
key press for heal all right even if i
19373
11:36:54,720 --> 11:36:55,840
press q
19374
11:36:55,840 --> 11:36:58,560
so there you go that's kazi punching
19375
11:36:58,560 --> 11:37:01,680
now he heals
19376
11:37:01,680 --> 11:37:02,880
perfect
19377
11:37:02,880 --> 11:37:04,400
right so now it's starting to get fun
19378
11:37:04,400 --> 11:37:06,800
right well fun for qazi not really for
19379
11:37:06,800 --> 11:37:08,640
me because i'm getting beat up here
19380
11:37:08,640 --> 11:37:11,916
all right so let's add my controls and
19381
11:37:11,916 --> 11:37:14,480
guess what the controls is the same
19382
11:37:14,480 --> 11:37:16,480
exact thing so let's do the if statement
19383
11:37:16,480 --> 11:37:19,436
all right if e dot key is equal equal i
19384
11:37:19,436 --> 11:37:21,040
won't forget this time
19385
11:37:21,040 --> 11:37:24,080
and the punch for lance or me is going
19386
11:37:24,080 --> 11:37:25,436
to be p
19387
11:37:25,436 --> 11:37:27,436
and we do the and
19388
11:37:27,436 --> 11:37:29,756
i want to make sure if kazi's health is
19389
11:37:29,756 --> 11:37:31,520
greater than zero i should still be able
19390
11:37:31,520 --> 11:37:33,276
to punch him right
19391
11:37:33,276 --> 11:37:35,200
and then and
19392
11:37:35,200 --> 11:37:37,116
uh game dot
19393
11:37:37,116 --> 11:37:41,360
is over is equal equal to false perfect
19394
11:37:41,360 --> 11:37:44,000
now after striking then play the attack
19395
11:37:44,000 --> 11:37:45,520
sound so let's strike
19396
11:37:45,520 --> 11:37:48,320
p2 dot strike
19397
11:37:48,320 --> 11:37:50,560
what is the arguments that strike takes
19398
11:37:50,560 --> 11:37:52,800
it's going to take in the player and i
19399
11:37:52,800 --> 11:37:55,276
love that replit shows you you know like
19400
11:37:55,276 --> 11:37:57,116
the little comments of what arguments
19401
11:37:57,116 --> 11:37:58,720
does it take takes in the player the
19402
11:37:58,720 --> 11:38:01,520
enemy and the damage so the enemy is
19403
11:38:01,520 --> 11:38:04,160
player one or kazi and the attack damage
19404
11:38:04,160 --> 11:38:05,756
is my
19405
11:38:05,756 --> 11:38:08,080
attack damage
19406
11:38:08,080 --> 11:38:11,200
and it's like that okay
19407
11:38:11,200 --> 11:38:14,000
and after we strike we need to play the
19408
11:38:14,000 --> 11:38:16,400
sound and to save you guys some time
19409
11:38:16,400 --> 11:38:18,480
it's basically
19410
11:38:18,480 --> 11:38:20,560
document
19411
11:38:20,560 --> 11:38:23,116
dot get element dot get
19412
11:38:23,116 --> 11:38:25,200
element by
19413
11:38:25,200 --> 11:38:27,596
id
19414
11:38:27,596 --> 11:38:30,720
and i believe it's p2 attack right i
19415
11:38:30,720 --> 11:38:32,640
have to make sure you can just look over
19416
11:38:32,640 --> 11:38:34,960
here it is p2 attack
19417
11:38:34,960 --> 11:38:37,276
that's how you get that id then you hit
19418
11:38:37,276 --> 11:38:39,596
play
19419
11:38:39,596 --> 11:38:40,800
okay cool
19420
11:38:40,800 --> 11:38:42,960
let's hit run and hp
19421
11:38:42,960 --> 11:38:44,080
oh
19422
11:38:44,080 --> 11:38:46,400
i attacked myself
19423
11:38:46,400 --> 11:38:47,520
uh
19424
11:38:47,520 --> 11:38:50,080
that's not good
19425
11:38:50,080 --> 11:38:51,916
there you go for some reason when i the
19426
11:38:51,916 --> 11:38:53,276
names were switched around and that was
19427
11:38:53,276 --> 11:38:55,916
really confusing so i hit refresh
19428
11:38:55,916 --> 11:38:58,080
and now let's see if cube
19429
11:38:58,080 --> 11:39:01,200
okay that works fine
19430
11:39:01,276 --> 11:39:04,400
if e dot key
19431
11:39:04,960 --> 11:39:08,880
oh can't forget the curly brackets
19432
11:39:11,360 --> 11:39:13,596
okay
19433
11:39:13,756 --> 11:39:16,000
i hear two sounds
19434
11:39:16,000 --> 11:39:17,916
there you go that's one now i'm going to
19435
11:39:17,916 --> 11:39:18,620
punch quasi
19436
11:39:18,620 --> 11:39:19,680
[Music]
19437
11:39:19,680 --> 11:39:21,360
and they switch places
19438
11:39:21,360 --> 11:39:24,160
that's interesting
19439
11:39:26,080 --> 11:39:28,480
oh that's a really funny bug it's like i
19440
11:39:28,480 --> 11:39:30,320
punch
19441
11:39:30,320 --> 11:39:32,880
and then they switch
19442
11:39:32,880 --> 11:39:34,880
oh and i think it's because of the
19443
11:39:34,880 --> 11:39:36,400
strike
19444
11:39:36,400 --> 11:39:38,080
okay
19445
11:39:38,080 --> 11:39:40,080
so going back to the top guys
19446
11:39:40,080 --> 11:39:42,720
let's go to the strike
19447
11:39:42,720 --> 11:39:44,800
and where it says update game this is
19448
11:39:44,800 --> 11:39:46,480
going to be different so this is too
19449
11:39:46,480 --> 11:39:48,160
dynamic so we have to put back this to
19450
11:39:48,160 --> 11:39:50,640
player one and player two and i actually
19451
11:39:50,640 --> 11:39:52,400
had that before
19452
11:39:52,400 --> 11:39:54,080
so now
19453
11:39:54,080 --> 11:39:55,520
we're not gonna get that weird switching
19454
11:39:55,520 --> 11:39:57,840
names i'm when i put player and enemy
19455
11:39:57,840 --> 11:40:00,320
there i'm saying that they're switching
19456
11:40:00,320 --> 11:40:02,400
roles so that's why you see kazia lance
19457
11:40:02,400 --> 11:40:04,640
switch and now that's why i got confused
19458
11:40:04,640 --> 11:40:06,480
but now
19459
11:40:06,480 --> 11:40:08,160
there we go
19460
11:40:08,160 --> 11:40:09,596
all right now we can actually fight the
19461
11:40:09,596 --> 11:40:11,596
game i can heal
19462
11:40:11,596 --> 11:40:13,040
well kazuki can you actually can't heal
19463
11:40:13,040 --> 11:40:14,640
yet
19464
11:40:14,640 --> 11:40:16,320
now there's like a back and forth here
19465
11:40:16,320 --> 11:40:19,360
it's making it a lot more fun
19466
11:40:19,360 --> 11:40:21,596
there you go so that cleared it up just
19467
11:40:21,596 --> 11:40:23,756
to recap make sure in your strike
19468
11:40:23,756 --> 11:40:26,080
function when you update the game it
19469
11:40:26,080 --> 11:40:27,276
shouldn't say
19470
11:40:27,276 --> 11:40:28,960
player enemy
19471
11:40:28,960 --> 11:40:31,360
it just should say p1 p2 so go to line
19472
11:40:31,360 --> 11:40:32,640
79
19473
11:40:32,640 --> 11:40:34,960
change this from player to p1 change
19474
11:40:34,960 --> 11:40:36,400
this from p2 to enemy because it should
19475
11:40:36,400 --> 11:40:37,840
be the same it shouldn't be able to be
19476
11:40:37,840 --> 11:40:38,960
switched around
19477
11:40:38,960 --> 11:40:41,916
like that shouldn't be dynamic
19478
11:40:41,916 --> 11:40:43,436
in this sense
19479
11:40:43,436 --> 11:40:45,680
so cool that's strike
19480
11:40:45,680 --> 11:40:48,320
so now let's do the if statement for the
19481
11:40:48,320 --> 11:40:51,276
heal which is pretty simple the key is
19482
11:40:51,276 --> 11:40:52,720
going to be
19483
11:40:52,720 --> 11:40:53,520
l
19484
11:40:53,520 --> 11:40:54,560
for here
19485
11:40:54,560 --> 11:40:57,756
right not capital l lower case and if
19486
11:40:57,756 --> 11:41:01,360
you know p1 that health is greater than
19487
11:41:01,360 --> 11:41:03,200
zero
19488
11:41:03,200 --> 11:41:05,680
then we should be able to heal still
19489
11:41:05,680 --> 11:41:08,160
and i can't forget the curly brackets
19490
11:41:08,160 --> 11:41:11,116
so inside the curly brackets
19491
11:41:11,116 --> 11:41:12,960
it says after healing
19492
11:41:12,960 --> 11:41:14,640
play heal sound
19493
11:41:14,640 --> 11:41:17,276
so let's how do we heal as player two
19494
11:41:17,276 --> 11:41:21,276
just use the method and pass it in
19495
11:41:21,276 --> 11:41:23,116
then how do we play the sound we get the
19496
11:41:23,116 --> 11:41:24,640
dom element
19497
11:41:24,640 --> 11:41:26,640
which should just be
19498
11:41:26,640 --> 11:41:28,000
p2
19499
11:41:28,000 --> 11:41:29,916
heal
19500
11:41:29,916 --> 11:41:31,916
and then do dot
19501
11:41:31,916 --> 11:41:34,240
play
19502
11:41:35,840 --> 11:41:38,000
awesome
19503
11:41:38,000 --> 11:41:40,160
and that should be it so let's hit run
19504
11:41:40,160 --> 11:41:42,720
and let's see if we have a game
19505
11:41:42,720 --> 11:41:44,720
oh i love the heel sound effect for my
19506
11:41:44,720 --> 11:41:46,880
my heel it's like whoop
19507
11:41:46,880 --> 11:41:48,720
whoop and then kazi says
19508
11:41:48,720 --> 11:41:50,000
[Music]
19509
11:41:50,000 --> 11:41:53,000
right
19510
11:41:55,276 --> 11:41:57,116
and
19511
11:41:57,116 --> 11:41:58,240
hey
19512
11:41:58,240 --> 11:42:00,640
lance wins did you hear that
19513
11:42:00,640 --> 11:42:04,560
that is the victory sound from declare
19514
11:42:04,560 --> 11:42:06,960
winner
19515
11:42:07,276 --> 11:42:08,960
let's see well i passed it declare
19516
11:42:08,960 --> 11:42:09,916
winner
19517
11:42:09,916 --> 11:42:11,360
drink the man right here play the
19518
11:42:11,360 --> 11:42:12,640
victory sound
19519
11:42:12,640 --> 11:42:14,800
right now the only problem is after we
19520
11:42:14,800 --> 11:42:16,000
play the game
19521
11:42:16,000 --> 11:42:17,436
it's like a one-time use we can't play
19522
11:42:17,436 --> 11:42:18,720
anymore because this reset button
19523
11:42:18,720 --> 11:42:19,916
doesn't work
19524
11:42:19,916 --> 11:42:21,200
so
19525
11:42:21,200 --> 11:42:22,480
we're almost done guys we just need to
19526
11:42:22,480 --> 11:42:24,400
set up this reset button
19527
11:42:24,400 --> 11:42:26,720
right and the reset button is pretty
19528
11:42:26,720 --> 11:42:28,640
simple we just kind of set all the
19529
11:42:28,640 --> 11:42:32,400
values back to its original state right
19530
11:42:32,400 --> 11:42:34,720
so what i mean by values i mean p1
19531
11:42:34,720 --> 11:42:37,276
health causes health and my health
19532
11:42:37,276 --> 11:42:39,520
sending it back to 100.
19533
11:42:39,520 --> 11:42:42,560
so all you have to do is just say
19534
11:42:42,560 --> 11:42:45,520
p1 do i have access to p1 yes i do
19535
11:42:45,520 --> 11:42:47,276
i say health
19536
11:42:47,276 --> 11:42:51,276
is equal to oops 100
19537
11:42:51,276 --> 11:42:54,960
all right p2.health is equal to 100
19538
11:42:54,960 --> 11:42:57,596
right and then we also want to set the
19539
11:42:57,596 --> 11:43:00,160
game is over to what do you want is over
19540
11:43:00,160 --> 11:43:03,040
to be false again or true again
19541
11:43:03,040 --> 11:43:04,400
false because
19542
11:43:04,400 --> 11:43:07,040
if this dot is over
19543
11:43:07,040 --> 11:43:08,720
is equal to
19544
11:43:08,720 --> 11:43:11,116
false that means we can keep playing
19545
11:43:11,116 --> 11:43:12,000
and
19546
11:43:12,000 --> 11:43:13,596
do we want to see lance wins right i
19547
11:43:13,596 --> 11:43:15,276
don't think kazi wants to see my name
19548
11:43:15,276 --> 11:43:17,916
and for resetting right my name winning
19549
11:43:17,916 --> 11:43:19,916
and for resetting so we're going to get
19550
11:43:19,916 --> 11:43:22,320
that result div again
19551
11:43:22,320 --> 11:43:24,080
get the inner text
19552
11:43:24,080 --> 11:43:26,240
and how do we make it so that it doesn't
19553
11:43:26,240 --> 11:43:28,560
show lance wins well we can just say
19554
11:43:28,560 --> 11:43:29,756
empty quotes that's going to make it
19555
11:43:29,756 --> 11:43:31,756
blank that's how you clear your inputs
19556
11:43:31,756 --> 11:43:33,840
that's how you clear your dips
19557
11:43:33,840 --> 11:43:34,720
and
19558
11:43:34,720 --> 11:43:36,960
when we're set we should also
19559
11:43:36,960 --> 11:43:38,880
update the game
19560
11:43:38,880 --> 11:43:40,160
to p1
19561
11:43:40,160 --> 11:43:41,200
p2
19562
11:43:41,200 --> 11:43:42,160
and
19563
11:43:42,160 --> 11:43:44,800
set the game state to whatever this dot
19564
11:43:44,800 --> 11:43:48,240
is over is perfect so if i hit run here
19565
11:43:48,240 --> 11:43:49,610
let's uh play the game
19566
11:43:49,610 --> 11:43:52,650
[Music]
19567
11:43:53,840 --> 11:43:56,480
qazi wins let's hit reset
19568
11:43:56,480 --> 11:43:58,640
ay and then we can just play again
19569
11:43:58,640 --> 11:44:01,840
let's see who wins this time
19570
11:44:02,480 --> 11:44:04,480
i win this time right this is getting
19571
11:44:04,480 --> 11:44:06,480
this is really fun i had a lot of fun
19572
11:44:06,480 --> 11:44:08,720
making this guys with that said that's
19573
11:44:08,720 --> 11:44:12,960
pretty much the fighting game app right
19574
11:44:12,960 --> 11:44:14,880
so it wasn't too bad it looks like a lot
19575
11:44:14,880 --> 11:44:16,720
of code but if you really break it down
19576
11:44:16,720 --> 11:44:19,596
it's just understanding each piece like
19577
11:44:19,596 --> 11:44:21,200
all right you want to reset just get the
19578
11:44:21,200 --> 11:44:22,720
player health all right what do you want
19579
11:44:22,720 --> 11:44:24,800
to set the health to 100 all right how
19580
11:44:24,800 --> 11:44:26,320
do you make sure the game is playable
19581
11:44:26,320 --> 11:44:27,360
false
19582
11:44:27,360 --> 11:44:29,916
right so if it helps to write stuff down
19583
11:44:29,916 --> 11:44:31,756
and look it over again feel free to do
19584
11:44:31,756 --> 11:44:35,200
so right the last function here is the
19585
11:44:35,200 --> 11:44:36,560
play button this is going to be the
19586
11:44:36,560 --> 11:44:39,520
bonus feature it's the simulation right
19587
11:44:39,520 --> 11:44:41,200
if i hit reset i should be able to
19588
11:44:41,200 --> 11:44:42,800
simulate the game and just see what
19589
11:44:42,800 --> 11:44:44,560
would happen right
19590
11:44:44,560 --> 11:44:46,480
so to play
19591
11:44:46,480 --> 11:44:48,640
i usually like to reset to make sure the
19592
11:44:48,640 --> 11:44:50,720
player's health is back to full before
19593
11:44:50,720 --> 11:44:52,800
starting right
19594
11:44:52,800 --> 11:44:55,436
so how do we reset the game
19595
11:44:55,436 --> 11:44:58,880
well reset is a method within game so if
19596
11:44:58,880 --> 11:45:00,480
it's within game and you're calling
19597
11:45:00,480 --> 11:45:03,436
something within itself you can say this
19598
11:45:03,436 --> 11:45:05,916
right this represent rob is pointing to
19599
11:45:05,916 --> 11:45:07,200
itself
19600
11:45:07,200 --> 11:45:08,640
so that just means the game it's like
19601
11:45:08,640 --> 11:45:10,480
saying game dot reset
19602
11:45:10,480 --> 11:45:13,040
right game.reset takes two parameters p1
19603
11:45:13,040 --> 11:45:14,640
and p2
19604
11:45:14,640 --> 11:45:15,596
right
19605
11:45:15,596 --> 11:45:17,040
and that's it
19606
11:45:17,040 --> 11:45:18,400
okay cool
19607
11:45:18,400 --> 11:45:20,240
now we have the while loop so we went
19608
11:45:20,240 --> 11:45:21,916
over while loops in that prep video that
19609
11:45:21,916 --> 11:45:24,480
i had with you guys
19610
11:45:24,480 --> 11:45:25,840
that prep video that i have with you
19611
11:45:25,840 --> 11:45:29,040
guys and what we want to do is make sure
19612
11:45:29,040 --> 11:45:30,800
both players get to strike so that means
19613
11:45:30,800 --> 11:45:33,596
kazi goes once and then heals and then i
19614
11:45:33,596 --> 11:45:36,160
go once heals and then it just keeps
19615
11:45:36,160 --> 11:45:38,640
looping till this
19616
11:45:38,640 --> 11:45:40,160
dot is over
19617
11:45:40,160 --> 11:45:40,960
is
19618
11:45:40,960 --> 11:45:43,436
true because the second this turns true
19619
11:45:43,436 --> 11:45:44,880
the match is over
19620
11:45:44,880 --> 11:45:46,720
right so
19621
11:45:46,720 --> 11:45:48,320
all you have to do is just call the each
19622
11:45:48,320 --> 11:45:50,480
command right that's it so that's just
19623
11:45:50,480 --> 11:45:53,596
p1 dot strike
19624
11:45:53,596 --> 11:46:00,000
p1 p2 and p1 dot attack damage that's it
19625
11:46:00,000 --> 11:46:02,800
so kazi got to strike in the simulation
19626
11:46:02,800 --> 11:46:05,840
now what we should do next is p2.heal
19627
11:46:05,840 --> 11:46:07,200
right this could be any order that you
19628
11:46:07,200 --> 11:46:10,480
want so now kazi strikes i heal right
19629
11:46:10,480 --> 11:46:12,640
this is kind of the script
19630
11:46:12,640 --> 11:46:15,116
so in this script i heal and then now i
19631
11:46:15,116 --> 11:46:17,596
strike
19632
11:46:17,680 --> 11:46:20,320
so it's going to say p2 is the player
19633
11:46:20,320 --> 11:46:22,640
the enemy is p1 and we're going to take
19634
11:46:22,640 --> 11:46:24,080
in my
19635
11:46:24,080 --> 11:46:26,160
attack damage
19636
11:46:26,160 --> 11:46:27,910
okay cool
19637
11:46:27,910 --> 11:46:30,949
[Music]
19638
11:46:31,276 --> 11:46:33,276
right
19639
11:46:33,276 --> 11:46:34,720
and then there's also
19640
11:46:34,720 --> 11:46:38,560
p1 dot heal
19641
11:46:38,560 --> 11:46:40,960
all right p1 so i so this is the script
19642
11:46:40,960 --> 11:46:44,720
guys cause he strikes i heal i strike
19643
11:46:44,720 --> 11:46:47,520
cause heals all right cause he strikes i
19644
11:46:47,520 --> 11:46:49,360
heal i strike i was going to keep doing
19645
11:46:49,360 --> 11:46:51,276
that to one of us dies which will happen
19646
11:46:51,276 --> 11:46:52,960
eventually and this is the way to make
19647
11:46:52,960 --> 11:46:55,436
it most fair right in a while loop so i
19648
11:46:55,436 --> 11:46:58,720
don't have to do any work at all
19649
11:46:58,720 --> 11:47:01,436
right so once is over is true which
19650
11:47:01,436 --> 11:47:03,596
means we're out of the while loop
19651
11:47:03,596 --> 11:47:05,520
right we're going to declare the winner
19652
11:47:05,520 --> 11:47:07,200
so we can just say return
19653
11:47:07,200 --> 11:47:09,360
this dot declare winner
19654
11:47:09,360 --> 11:47:10,560
and we can say
19655
11:47:10,560 --> 11:47:12,960
this dot is over
19656
11:47:12,960 --> 11:47:16,800
p1 and p2 right because remember declare
19657
11:47:16,800 --> 11:47:17,756
winner
19658
11:47:17,756 --> 11:47:19,596
takes in the game state
19659
11:47:19,596 --> 11:47:21,116
p1 p2
19660
11:47:21,116 --> 11:47:23,596
perfect
19661
11:47:23,680 --> 11:47:25,116
cool and then
19662
11:47:25,116 --> 11:47:26,960
uh there is a glitch where it says
19663
11:47:26,960 --> 11:47:28,720
undefined so let's just make the message
19664
11:47:28,720 --> 11:47:32,800
say tie right it's a tie
19665
11:47:32,800 --> 11:47:36,160
when i can't determine who wins
19666
11:47:36,320 --> 11:47:38,640
okay
19667
11:47:38,880 --> 11:47:40,320
cool
19668
11:47:40,320 --> 11:47:43,200
so now that we have that functionality
19669
11:47:43,200 --> 11:47:45,680
just double check everything
19670
11:47:45,680 --> 11:47:48,400
time test it out
19671
11:47:48,400 --> 11:47:50,960
now to actually run the code
19672
11:47:50,960 --> 11:47:54,160
i had a little to do here which is add a
19673
11:47:54,160 --> 11:47:59,840
click listener to the play button right
19674
11:47:59,840 --> 11:48:00,800
so
19675
11:48:00,800 --> 11:48:03,360
we need to find the div that holds the
19676
11:48:03,360 --> 11:48:05,200
simulate button so if you look at the
19677
11:48:05,200 --> 11:48:06,320
top here
19678
11:48:06,320 --> 11:48:10,240
going up the top there is a
19679
11:48:10,240 --> 11:48:12,480
play button right here play button right
19680
11:48:12,480 --> 11:48:14,400
that's going to hold play so all you
19681
11:48:14,400 --> 11:48:15,220
need to do
19682
11:48:15,220 --> 11:48:17,520
[Music]
19683
11:48:17,520 --> 11:48:20,000
is come down below here
19684
11:48:20,000 --> 11:48:21,840
and say play
19685
11:48:21,840 --> 11:48:23,680
button dot on
19686
11:48:23,680 --> 11:48:25,360
click
19687
11:48:25,360 --> 11:48:26,560
all right it's lowercase because it's
19688
11:48:26,560 --> 11:48:29,116
not react
19689
11:48:30,720 --> 11:48:34,400
is equal to the function where
19690
11:48:34,400 --> 11:48:35,680
result
19691
11:48:35,680 --> 11:48:37,840
dot inner text
19692
11:48:37,840 --> 11:48:39,436
is equal to
19693
11:48:39,436 --> 11:48:42,720
game dot play and then you put in
19694
11:48:42,720 --> 11:48:43,756
p1
19695
11:48:43,756 --> 11:48:45,520
p2
19696
11:48:45,520 --> 11:48:46,560
perfect
19697
11:48:46,560 --> 11:48:48,960
let's hit run
19698
11:48:48,960 --> 11:48:50,640
okay so now that you have that we can
19699
11:48:50,640 --> 11:48:52,840
hit the simulate button it should work
19700
11:48:52,840 --> 11:48:57,520
now let's go lance wins right
19701
11:48:57,520 --> 11:48:59,520
how's the tie
19702
11:48:59,520 --> 11:49:02,320
kazi wins there we go cool so now the
19703
11:49:02,320 --> 11:49:04,080
simulate button works and we're free to
19704
11:49:04,080 --> 11:49:06,240
just simulate or we could reset and we
19705
11:49:06,240 --> 11:49:09,436
could just start playing
19706
11:49:09,436 --> 11:49:11,276
and you can make this fighting game even
19707
11:49:11,276 --> 11:49:13,360
more advanced you can add like a kick
19708
11:49:13,360 --> 11:49:15,520
option that does more damage you can add
19709
11:49:15,520 --> 11:49:17,840
like a i don't know a special move like
19710
11:49:17,840 --> 11:49:19,756
hadouken and it does even more damage
19711
11:49:19,756 --> 11:49:21,520
right all you have to do is go to the
19712
11:49:21,520 --> 11:49:23,200
player class
19713
11:49:23,200 --> 11:49:26,320
right and add that method in here right
19714
11:49:26,320 --> 11:49:28,320
this is what you need the essential
19715
11:49:28,320 --> 11:49:30,400
tools to create something like this and
19716
11:49:30,400 --> 11:49:32,240
i hope you guys enjoyed if you made it
19717
11:49:32,240 --> 11:49:34,640
this far in the video congratulations
19718
11:49:34,640 --> 11:49:36,800
this is the fighting game
19719
11:49:36,800 --> 11:49:37,756
so
19720
11:49:37,756 --> 11:49:39,680
sami show me how i did share with us how
19721
11:49:39,680 --> 11:49:41,436
you did the fighting game and make sure
19722
11:49:41,436 --> 11:49:44,960
to fork this ripple and place it back
19723
11:49:44,960 --> 11:49:46,800
into the main course so let me show you
19724
11:49:46,800 --> 11:49:48,720
guys how to do that
19725
11:49:48,720 --> 11:49:51,360
okay so let's say your code is working
19726
11:49:51,360 --> 11:49:53,756
and it's up to date all you guys need to
19727
11:49:53,756 --> 11:49:57,200
do is go to the main replit again
19728
11:49:57,200 --> 11:49:58,000
right
19729
11:49:58,000 --> 11:50:00,320
and you can go to the solution and
19730
11:50:00,320 --> 11:50:02,960
compare it to how i did in the solution
19731
11:50:02,960 --> 11:50:06,000
and what you could do is put your code
19732
11:50:06,000 --> 11:50:08,880
in the exercise folder so if this is
19733
11:50:08,880 --> 11:50:11,276
your index right you want to take
19734
11:50:11,276 --> 11:50:13,040
your index
19735
11:50:13,040 --> 11:50:14,480
click ctrl a
19736
11:50:14,480 --> 11:50:15,840
ctrl c
19737
11:50:15,840 --> 11:50:18,480
and then paste it on here
19738
11:50:18,480 --> 11:50:20,240
right so that it's up to date and has
19739
11:50:20,240 --> 11:50:21,436
your code
19740
11:50:21,436 --> 11:50:22,400
okay
19741
11:50:22,400 --> 11:50:24,240
so once you do that your reply is
19742
11:50:24,240 --> 11:50:25,916
complete right we moved over here so you
19743
11:50:25,916 --> 11:50:28,000
could test stuff a lot easier
19744
11:50:28,000 --> 11:50:29,916
perfect so that's it that's the fighting
19745
11:50:29,916 --> 11:50:31,360
game guys i'm glad you guys walked
19746
11:50:31,360 --> 11:50:32,400
through it with me
19747
11:50:32,400 --> 11:50:34,880
thanks for watching this part
19748
11:50:34,880 --> 11:50:36,320
all right so you've made it to the
19749
11:50:36,320 --> 11:50:38,320
netflix clone i'm excited about this one
19750
11:50:38,320 --> 11:50:39,840
because there's a little bit of a story
19751
11:50:39,840 --> 11:50:41,756
that goes along with this so let's let's
19752
11:50:41,756 --> 11:50:44,000
think about a scenario right netflix is
19753
11:50:44,000 --> 11:50:47,040
hiring you as a developer to fix their
19754
11:50:47,040 --> 11:50:48,880
app alright if you click on this button
19755
11:50:48,880 --> 11:50:50,880
right here open in a new tab
19756
11:50:50,880 --> 11:50:52,800
you're gonna see that looks pretty empty
19757
11:50:52,800 --> 11:50:55,200
that's because the movies aren't being
19758
11:50:55,200 --> 11:50:58,160
loaded in properly so it's your job as a
19759
11:50:58,160 --> 11:51:01,436
developer to make the api call and pull
19760
11:51:01,436 --> 11:51:02,880
the movies that we need it should look
19761
11:51:02,880 --> 11:51:04,480
something like this so if you go to your
19762
11:51:04,480 --> 11:51:07,756
javascript course rebel go to open
19763
11:51:07,756 --> 11:51:10,160
in the new tab for that you should see
19764
11:51:10,160 --> 11:51:13,520
this nice page create netflix and bam
19765
11:51:13,520 --> 11:51:15,596
this is what netflix is supposed to use
19766
11:51:15,596 --> 11:51:17,360
and this is what netflix is supposed to
19767
11:51:17,360 --> 11:51:19,200
look like and as you can see all the
19768
11:51:19,200 --> 11:51:20,800
netflix originals are here stranger
19769
11:51:20,800 --> 11:51:22,080
things season four if you guys are
19770
11:51:22,080 --> 11:51:23,360
watching let me know how you guys think
19771
11:51:23,360 --> 11:51:25,680
about it and then there's trending now
19772
11:51:25,680 --> 11:51:29,840
and there's also top rated movies right
19773
11:51:29,840 --> 11:51:32,000
so if you didn't know in the background
19774
11:51:32,000 --> 11:51:35,436
this is using an api from moviesdb so
19775
11:51:35,436 --> 11:51:36,560
we're going to be working with a real
19776
11:51:36,560 --> 11:51:39,520
api and if you get it working netflix is
19777
11:51:39,520 --> 11:51:41,520
up and running again so this is what it
19778
11:51:41,520 --> 11:51:43,200
looks like what you're going to need to
19779
11:51:43,200 --> 11:51:44,000
do
19780
11:51:44,000 --> 11:51:46,960
is hex to exit this out hop over back
19781
11:51:46,960 --> 11:51:50,000
into the exercise folder and take these
19782
11:51:50,000 --> 11:51:52,560
files you're going to notice a link to
19783
11:51:52,560 --> 11:51:53,720
this
19784
11:51:53,720 --> 11:51:56,400
replicate netflix project and this is
19785
11:51:56,400 --> 11:51:58,320
where we're going to be doing most of
19786
11:51:58,320 --> 11:51:59,436
our coding
19787
11:51:59,436 --> 11:52:01,360
so let's get started and let's take a
19788
11:52:01,360 --> 11:52:04,240
look at the assignment so here's our app
19789
11:52:04,240 --> 11:52:06,080
make netflix
19790
11:52:06,080 --> 11:52:08,000
here's the scenario where we have to
19791
11:52:08,000 --> 11:52:10,400
make sure that fetch movies is working
19792
11:52:10,400 --> 11:52:13,520
so the core functionality of this app is
19793
11:52:13,520 --> 11:52:16,240
to get fetch movies working
19794
11:52:16,240 --> 11:52:18,640
right and fetch movies is going to take
19795
11:52:18,640 --> 11:52:20,080
in a url
19796
11:52:20,080 --> 11:52:21,436
a div id
19797
11:52:21,436 --> 11:52:23,200
or a class
19798
11:52:23,200 --> 11:52:25,200
right so fetch movies is over here as
19799
11:52:25,200 --> 11:52:28,880
you can see the api url the dom element
19800
11:52:28,880 --> 11:52:30,640
right what do i mean by dom element what
19801
11:52:30,640 --> 11:52:33,360
do you guys think what is a dom element
19802
11:52:33,360 --> 11:52:35,276
right so with dom element it's going to
19803
11:52:35,276 --> 11:52:36,880
be one of these it's going to be either
19804
11:52:36,880 --> 11:52:39,436
original movies which is a class
19805
11:52:39,436 --> 11:52:42,080
trending or top rated right why would i
19806
11:52:42,080 --> 11:52:43,200
want to put
19807
11:52:43,200 --> 11:52:45,840
dom element as an argument
19808
11:52:45,840 --> 11:52:47,756
right because
19809
11:52:47,756 --> 11:52:49,360
this is actually
19810
11:52:49,360 --> 11:52:50,960
a dynamic
19811
11:52:50,960 --> 11:52:52,800
function right why is it dynamic if you
19812
11:52:52,800 --> 11:52:55,040
remember what dynamic is it can be
19813
11:52:55,040 --> 11:52:56,800
inputted with any
19814
11:52:56,800 --> 11:52:58,800
you know parameter or argument and still
19815
11:52:58,800 --> 11:53:00,720
get you the right itself right result
19816
11:53:00,720 --> 11:53:03,200
it's not hard-coded right so
19817
11:53:03,200 --> 11:53:05,840
later on i need to make an api call a
19818
11:53:05,840 --> 11:53:08,560
separate one to get the trending one but
19819
11:53:08,560 --> 11:53:10,080
where do i want to put it well that's
19820
11:53:10,080 --> 11:53:12,400
where dom element comes in
19821
11:53:12,400 --> 11:53:14,080
dom element is just who doesn't
19822
11:53:14,080 --> 11:53:16,000
represent one of these guys right so if
19823
11:53:16,000 --> 11:53:17,680
i want to put it in trending
19824
11:53:17,680 --> 11:53:18,560
right
19825
11:53:18,560 --> 11:53:19,520
i need
19826
11:53:19,520 --> 11:53:22,000
this dom element the trending div
19827
11:53:22,000 --> 11:53:23,520
because that's where i want to put it
19828
11:53:23,520 --> 11:53:24,960
and then if you notice there's something
19829
11:53:24,960 --> 11:53:26,400
called path type
19830
11:53:26,400 --> 11:53:27,916
that one you might not know but when you
19831
11:53:27,916 --> 11:53:30,080
look at the data there's two types in
19832
11:53:30,080 --> 11:53:32,480
here there is let me see let's bring
19833
11:53:32,480 --> 11:53:34,430
back let's bring that back in here
19834
11:53:34,430 --> 11:53:35,756
[Music]
19835
11:53:35,756 --> 11:53:38,160
there's two types of paths what i mean
19836
11:53:38,160 --> 11:53:40,240
by paths is this if i could create
19837
11:53:40,240 --> 11:53:41,916
netflix do you notice the difference
19838
11:53:41,916 --> 11:53:45,436
between these movies and these movies
19839
11:53:45,436 --> 11:53:48,240
well this is called poster path and this
19840
11:53:48,240 --> 11:53:50,560
is called backdrop path it's the wide
19841
11:53:50,560 --> 11:53:53,200
one so you have to define
19842
11:53:53,200 --> 11:53:55,680
on your api data which path is going to
19843
11:53:55,680 --> 11:53:57,680
do you want a posterior path or a
19844
11:53:57,680 --> 11:53:59,436
backdrop path all right that's what
19845
11:53:59,436 --> 11:54:01,276
that's here for
19846
11:54:01,276 --> 11:54:03,276
cool so that's what path type is and
19847
11:54:03,276 --> 11:54:05,840
again here we're going to use the fetch
19848
11:54:05,840 --> 11:54:08,880
method right to call the api
19849
11:54:08,880 --> 11:54:10,960
and then we're going to use fetch to get
19850
11:54:10,960 --> 11:54:12,160
the response
19851
11:54:12,160 --> 11:54:14,560
and then call something called show
19852
11:54:14,560 --> 11:54:16,560
movies that's another function
19853
11:54:16,560 --> 11:54:19,840
right so this is a function
19854
11:54:19,840 --> 11:54:21,520
that displays the movies to the dom
19855
11:54:21,520 --> 11:54:22,960
right so
19856
11:54:22,960 --> 11:54:25,916
fetch movies is just fetching the movies
19857
11:54:25,916 --> 11:54:28,320
show movies is another function that's
19858
11:54:28,320 --> 11:54:31,276
gonna show the movies over here right so
19859
11:54:31,276 --> 11:54:34,400
we're gonna need to take that object and
19860
11:54:34,400 --> 11:54:36,000
place it into the down all right nothing
19861
11:54:36,000 --> 11:54:37,596
new right i'll walk you guys through
19862
11:54:37,596 --> 11:54:38,880
that eventually but i think you guys
19863
11:54:38,880 --> 11:54:40,880
know how that's done right
19864
11:54:40,880 --> 11:54:43,756
next we have a function that fetches
19865
11:54:43,756 --> 11:54:47,116
netflix originals right so these three
19866
11:54:47,116 --> 11:54:50,800
functions are gonna need some api calls
19867
11:54:50,800 --> 11:54:53,680
so what i did is x tap the tab over here
19868
11:54:53,680 --> 11:54:55,680
and let me get you the api
19869
11:54:55,680 --> 11:54:57,200
that you're gonna need so the url for
19870
11:54:57,200 --> 11:54:58,960
this one all right let's do a comment
19871
11:54:58,960 --> 11:55:00,080
here
19872
11:55:00,080 --> 11:55:03,680
a url to get originals is going to be
19873
11:55:03,680 --> 11:55:05,520
this api
19874
11:55:05,520 --> 11:55:06,560
and the cool part is you don't have to
19875
11:55:06,560 --> 11:55:08,080
worry about getting an api key i already
19876
11:55:08,080 --> 11:55:10,080
got it for you it's this one right here
19877
11:55:10,080 --> 11:55:12,640
all right then we can also
19878
11:55:12,640 --> 11:55:15,040
get the url for
19879
11:55:15,040 --> 11:55:19,040
sorry this is the url for trending
19880
11:55:19,040 --> 11:55:20,390
we need a url
19881
11:55:20,390 --> 11:55:21,436
[Music]
19882
11:55:21,436 --> 11:55:23,200
for
19883
11:55:23,200 --> 11:55:25,520
original movies
19884
11:55:25,520 --> 11:55:28,720
which is just going to be this
19885
11:55:29,916 --> 11:55:31,200
yep and then we can do something like
19886
11:55:31,200 --> 11:55:32,720
this oops
19887
11:55:32,720 --> 11:55:34,240
like that
19888
11:55:34,240 --> 11:55:36,720
and see the top rated url i have for you
19889
11:55:36,720 --> 11:55:38,560
guys it's going to be
19890
11:55:38,560 --> 11:55:41,200
right
19891
11:55:41,200 --> 11:55:42,960
url
19892
11:55:42,960 --> 11:55:44,960
pull in and boom there you go
19893
11:55:44,960 --> 11:55:46,640
so these are the urls that you want to
19894
11:55:46,640 --> 11:55:48,960
use and you're going to use these and
19895
11:55:48,960 --> 11:55:52,480
put them into fetch movies right so
19896
11:55:52,480 --> 11:55:54,640
after you get the url
19897
11:55:54,640 --> 11:55:56,320
you're gonna call
19898
11:55:56,320 --> 11:55:58,000
call fetch
19899
11:55:58,000 --> 11:55:59,200
movies
19900
11:55:59,200 --> 11:56:01,040
all right so as you can see fetch movies
19901
11:56:01,040 --> 11:56:03,276
is the key here so i'll put this three
19902
11:56:03,276 --> 11:56:04,720
times so after this
19903
11:56:04,720 --> 11:56:07,276
call fetch movies and after this
19904
11:56:07,276 --> 11:56:09,276
call pledge movies
19905
11:56:09,276 --> 11:56:10,800
all right why am i calling fetch movies
19906
11:56:10,800 --> 11:56:13,360
because fetch movies again is already
19907
11:56:13,360 --> 11:56:15,916
making the api call right it's already
19908
11:56:15,916 --> 11:56:18,240
making an api call i'm taking a dynamic
19909
11:56:18,240 --> 11:56:20,560
one and then you're gonna choose where
19910
11:56:20,560 --> 11:56:22,160
it goes right so it's already being
19911
11:56:22,160 --> 11:56:23,916
handled right it's a helper function
19912
11:56:23,916 --> 11:56:25,680
right it's called a helper function
19913
11:56:25,680 --> 11:56:28,640
because it's helping get originals fetch
19914
11:56:28,640 --> 11:56:31,200
the movies it's helping trending fetch
19915
11:56:31,200 --> 11:56:33,436
the trending movies right or using it
19916
11:56:33,436 --> 11:56:35,116
dynamically alright so that's pretty
19917
11:56:35,116 --> 11:56:37,360
much it for the app what you need to do
19918
11:56:37,360 --> 11:56:39,116
is figure out how it comes together and
19919
11:56:39,116 --> 11:56:40,560
i would start with
19920
11:56:40,560 --> 11:56:43,360
this function on line 45 okay so best of
19921
11:56:43,360 --> 11:56:45,360
luck this is definitely a little bit
19922
11:56:45,360 --> 11:56:46,960
less complex than the fighting game that
19923
11:56:46,960 --> 11:56:49,116
you guys have done but i think you guys
19924
11:56:49,116 --> 11:56:51,116
can do it and if you get this working
19925
11:56:51,116 --> 11:56:52,560
you're free to do whatever you want with
19926
11:56:52,560 --> 11:56:54,640
this ui because you can use this to make
19927
11:56:54,640 --> 11:56:56,800
your own netflix change out the code to
19928
11:56:56,800 --> 11:56:58,720
make i don't know disney plus
19929
11:56:58,720 --> 11:57:00,640
however you guys want to move it
19930
11:57:00,640 --> 11:57:02,960
however you guys want to do it so
19931
11:57:02,960 --> 11:57:04,480
good luck i'll see you guys in the
19932
11:57:04,480 --> 11:57:06,080
solution
19933
11:57:06,080 --> 11:57:08,240
awesome so how'd you guys do were you
19934
11:57:08,240 --> 11:57:10,000
able to get it working if you were close
19935
11:57:10,000 --> 11:57:13,040
or you got to finish congratulations you
19936
11:57:13,040 --> 11:57:15,840
have netflix right if you didn't this is
19937
11:57:15,840 --> 11:57:18,160
what the solution part is for and maybe
19938
11:57:18,160 --> 11:57:19,756
you can see maybe that i coded it a
19939
11:57:19,756 --> 11:57:20,960
little bit differently or maybe you
19940
11:57:20,960 --> 11:57:22,720
coded it better than me right hats off
19941
11:57:22,720 --> 11:57:25,680
to you if you did but let's walk through
19942
11:57:25,680 --> 11:57:28,240
the solution right so we have the make
19943
11:57:28,240 --> 11:57:30,640
netflix app we have fetch movies and the
19944
11:57:30,640 --> 11:57:32,160
three main functions and i just realized
19945
11:57:32,160 --> 11:57:33,756
the url is already on here so i didn't
19946
11:57:33,756 --> 11:57:36,480
need to paste it down below that's okay
19947
11:57:36,480 --> 11:57:38,480
we'll keep it moving we're gonna see the
19948
11:57:38,480 --> 11:57:40,160
great originals trending now top rated
19949
11:57:40,160 --> 11:57:42,320
right so let's break this down
19950
11:57:42,320 --> 11:57:44,400
here are the divs that we need to know
19951
11:57:44,400 --> 11:57:46,400
original movies has the netflix
19952
11:57:46,400 --> 11:57:48,880
originals trending has trending yada
19953
11:57:48,880 --> 11:57:51,116
yada you guys know that right here's an
19954
11:57:51,116 --> 11:57:53,360
interesting one window
19955
11:57:53,360 --> 11:57:55,276
dot unload
19956
11:57:55,276 --> 11:57:56,080
right
19957
11:57:56,080 --> 11:57:58,400
it's exactly what it sounds like it's
19958
11:57:58,400 --> 11:58:01,276
basically a method right because it's
19959
11:58:01,276 --> 11:58:03,040
coming from window
19960
11:58:03,040 --> 11:58:05,840
and when the window loads
19961
11:58:05,840 --> 11:58:09,680
run these three functions so
19962
11:58:09,680 --> 11:58:11,200
it's going to run those three functions
19963
11:58:11,200 --> 11:58:14,160
and then display it on our netflix app
19964
11:58:14,160 --> 11:58:15,840
right but at the moment you're not going
19965
11:58:15,840 --> 11:58:17,040
to see anything
19966
11:58:17,040 --> 11:58:18,640
right because
19967
11:58:18,640 --> 11:58:20,080
those functions don't exist or they
19968
11:58:20,080 --> 11:58:22,560
don't do anything right they're blank
19969
11:58:22,560 --> 11:58:24,720
all right so let's start with the core
19970
11:58:24,720 --> 11:58:26,240
of our functionality and that's going to
19971
11:58:26,240 --> 11:58:28,880
be on fetch movies so let's hop over to
19972
11:58:28,880 --> 11:58:31,360
fetch movies that's gonna be on line 45
19973
11:58:31,360 --> 11:58:33,360
i believe yep it's right there
19974
11:58:33,360 --> 11:58:35,916
so now that you're on fetch movies
19975
11:58:35,916 --> 11:58:38,240
we're gonna need to do is take a look at
19976
11:58:38,240 --> 11:58:40,320
url dom element and path type i
19977
11:58:40,320 --> 11:58:42,560
explained this in the last video so to
19978
11:58:42,560 --> 11:58:44,080
get started using it you just want to
19979
11:58:44,080 --> 11:58:46,480
say fetch right
19980
11:58:46,480 --> 11:58:48,560
and then how do i fetch
19981
11:58:48,560 --> 11:58:50,400
you know api data
19982
11:58:50,400 --> 11:58:52,480
well you just put the api url right so
19983
11:58:52,480 --> 11:58:54,160
that could look two ways it could look
19984
11:58:54,160 --> 11:58:55,596
like this
19985
11:58:55,596 --> 11:58:57,916
fetch this whole thing
19986
11:58:57,916 --> 11:58:59,040
or
19987
11:58:59,040 --> 11:59:00,640
and this would work you could totally do
19988
11:59:00,640 --> 11:59:02,880
this but this is only getting top rated
19989
11:59:02,880 --> 11:59:04,160
as you can see
19990
11:59:04,160 --> 11:59:06,080
but i want to make fetch movies dynamic
19991
11:59:06,080 --> 11:59:08,240
so how do i make this dynamic
19992
11:59:08,240 --> 11:59:10,560
just type in url
19993
11:59:10,560 --> 11:59:12,720
how come that's allowed well i'm passing
19994
11:59:12,720 --> 11:59:14,720
that as an argument right i'll put that
19995
11:59:14,720 --> 11:59:17,116
long string when i call the function
19996
11:59:17,116 --> 11:59:21,360
later cool so after that we can do a dot
19997
11:59:21,360 --> 11:59:22,960
then all right and again with the cool
19998
11:59:22,960 --> 11:59:24,160
thing with that then is you can just
19999
11:59:24,160 --> 11:59:26,400
write it like here
20000
11:59:26,400 --> 11:59:28,320
parentheses and
20001
11:59:28,320 --> 11:59:31,276
remember dot then is handling what you
20002
11:59:31,276 --> 11:59:32,400
want to do
20003
11:59:32,400 --> 11:59:35,520
after the promise is resolved right so
20004
11:59:35,520 --> 11:59:38,400
if i say uh that then here and i say
20005
11:59:38,400 --> 11:59:39,840
response
20006
11:59:39,840 --> 11:59:42,480
and then say arrow and then say
20007
11:59:42,480 --> 11:59:44,240
console.log
20008
11:59:44,240 --> 11:59:46,480
response
20009
11:59:46,480 --> 11:59:49,360
okay cool so if i hit run here boom
20010
11:59:49,360 --> 11:59:50,960
nothing really happens and you can even
20011
11:59:50,960 --> 11:59:53,040
check it on inspect element over here
20012
11:59:53,040 --> 11:59:55,200
nothing all right reason being we didn't
20013
11:59:55,200 --> 11:59:57,040
really call fetch movie so let's do that
20014
11:59:57,040 --> 11:59:58,480
right here
20015
11:59:58,480 --> 12:00:00,240
all right so coming down below here
20016
12:00:00,240 --> 12:00:01,756
we're just going to call fetch movies to
20017
12:00:01,756 --> 12:00:03,680
test it out and
20018
12:00:03,680 --> 12:00:05,436
here we need to pass in a url dom
20019
12:00:05,436 --> 12:00:07,200
element and a path type
20020
12:00:07,200 --> 12:00:09,116
but are we using dom element or path
20021
12:00:09,116 --> 12:00:11,276
type just yet nah we're not using it
20022
12:00:11,276 --> 12:00:13,360
just yet so we're gonna just pass in a
20023
12:00:13,360 --> 12:00:15,680
url and i can't just write url here
20024
12:00:15,680 --> 12:00:17,520
because then it'll be undefined fetching
20025
12:00:17,520 --> 12:00:20,400
undefined right so let's pass in a real
20026
12:00:20,400 --> 12:00:23,840
api url which was this one up the top
20027
12:00:23,840 --> 12:00:25,916
get originals
20028
12:00:25,916 --> 12:00:27,756
and we'll paste it right here and let's
20029
12:00:27,756 --> 12:00:29,040
put some more space so you guys don't
20030
12:00:29,040 --> 12:00:30,560
get confused all right this is a
20031
12:00:30,560 --> 12:00:31,840
function call
20032
12:00:31,840 --> 12:00:34,080
passing an api url we can also hit
20033
12:00:34,080 --> 12:00:35,436
refresh here
20034
12:00:35,436 --> 12:00:36,880
because it's going to run
20035
12:00:36,880 --> 12:00:39,596
then console and look at that response
20036
12:00:39,596 --> 12:00:41,520
right look at this we got a response
20037
12:00:41,520 --> 12:00:43,436
here's everything we need the headers
20038
12:00:43,436 --> 12:00:46,240
the status status 200 means it's good
20039
12:00:46,240 --> 12:00:49,040
which means we're good to go right so
20040
12:00:49,040 --> 12:00:51,596
now that we have the response though
20041
12:00:51,596 --> 12:00:53,756
we need to do something else
20042
12:00:53,756 --> 12:00:55,840
and something else with the response
20043
12:00:55,840 --> 12:00:56,880
right
20044
12:00:56,880 --> 12:00:59,360
well in here
20045
12:00:59,360 --> 12:01:01,436
if
20046
12:01:01,436 --> 12:01:02,960
dot okay
20047
12:01:02,960 --> 12:01:05,116
right if it's successful or if we got
20048
12:01:05,116 --> 12:01:08,320
200 we're gonna return
20049
12:01:08,320 --> 12:01:09,200
right
20050
12:01:09,200 --> 12:01:11,840
response
20051
12:01:11,840 --> 12:01:14,480
dot json right
20052
12:01:14,480 --> 12:01:15,756
cool
20053
12:01:15,756 --> 12:01:17,680
then we can add an else
20054
12:01:17,680 --> 12:01:19,680
all right and then we can just throw
20055
12:01:19,680 --> 12:01:22,160
an error so we can say throw a new
20056
12:01:22,160 --> 12:01:25,040
instance of an error right the classes
20057
12:01:25,040 --> 12:01:28,880
are coming back here right something
20058
12:01:28,880 --> 12:01:30,480
went wrong just in case something went
20059
12:01:30,480 --> 12:01:32,400
wrong along the way we can have this
20060
12:01:32,400 --> 12:01:34,000
else statement
20061
12:01:34,000 --> 12:01:35,520
cool but
20062
12:01:35,520 --> 12:01:37,436
as you see we got the response object
20063
12:01:37,436 --> 12:01:39,436
but we didn't get any data right and the
20064
12:01:39,436 --> 12:01:41,520
okay is true so that's good
20065
12:01:41,520 --> 12:01:43,436
we didn't get any data all right what we
20066
12:01:43,436 --> 12:01:46,080
need to get the data is go over to line
20067
12:01:46,080 --> 12:01:47,360
54
20068
12:01:47,360 --> 12:01:48,880
where this parentheses and you're just
20069
12:01:48,880 --> 12:01:50,720
gonna have to type in dot then remember
20070
12:01:50,720 --> 12:01:53,916
dot then is handling what we get back
20071
12:01:53,916 --> 12:01:55,520
from this right
20072
12:01:55,520 --> 12:01:56,800
so
20073
12:01:56,800 --> 12:01:58,960
basically after that then and you can
20074
12:01:58,960 --> 12:02:00,560
change multiple dot then you can do that
20075
12:02:00,560 --> 12:02:02,160
then that then not then this is the
20076
12:02:02,160 --> 12:02:03,520
second dot then
20077
12:02:03,520 --> 12:02:06,400
and this parameter we'll call data
20078
12:02:06,400 --> 12:02:09,520
right and then we will console.log the
20079
12:02:09,520 --> 12:02:11,116
data right
20080
12:02:11,116 --> 12:02:13,680
so there we go let's hit refresh
20081
12:02:13,680 --> 12:02:15,596
and look when i contact data this is
20082
12:02:15,596 --> 12:02:18,240
what we get back we get an object right
20083
12:02:18,240 --> 12:02:20,400
most of the time when you make an api
20084
12:02:20,400 --> 12:02:22,960
call you're going to get an array of
20085
12:02:22,960 --> 12:02:24,080
objects
20086
12:02:24,080 --> 12:02:26,320
or just an object itself so this time we
20087
12:02:26,320 --> 12:02:28,880
got one object right and if we open it
20088
12:02:28,880 --> 12:02:32,320
up we got results which is what
20089
12:02:32,320 --> 12:02:34,160
right an array
20090
12:02:34,160 --> 12:02:36,160
right and inside that array what's
20091
12:02:36,160 --> 12:02:37,680
inside that array let's open it up
20092
12:02:37,680 --> 12:02:38,880
objects
20093
12:02:38,880 --> 12:02:40,400
right and notice something
20094
12:02:40,400 --> 12:02:43,520
there is a backdrop path
20095
12:02:43,520 --> 12:02:46,640
the back drop path is a character is a
20096
12:02:46,640 --> 12:02:49,116
what's it called a property
20097
12:02:49,116 --> 12:02:51,520
with a value
20098
12:02:51,520 --> 12:02:53,596
of the image remember backdrop is the
20099
12:02:53,596 --> 12:02:56,560
wide one right so let's open up zero
20100
12:02:56,560 --> 12:02:57,520
again
20101
12:02:57,520 --> 12:02:59,916
so there is the backdrop path and here
20102
12:02:59,916 --> 12:03:02,480
we have the poster path right so i
20103
12:03:02,480 --> 12:03:04,640
didn't just make this up this is how we
20104
12:03:04,640 --> 12:03:06,640
can determine whether we want the path
20105
12:03:06,640 --> 12:03:09,200
type to be the backdrop or the path type
20106
12:03:09,200 --> 12:03:12,720
to be the poster right so there's fetch
20107
12:03:12,720 --> 12:03:16,400
movies and now it's pretty much set up
20108
12:03:16,400 --> 12:03:18,320
right the only thing that we could add
20109
12:03:18,320 --> 12:03:22,240
here is a dot catch all right.catch is
20110
12:03:22,240 --> 12:03:24,560
just here because if there's any error
20111
12:03:24,560 --> 12:03:27,680
we can say error
20112
12:03:27,680 --> 12:03:31,916
and then arrow here and say console.log
20113
12:03:31,916 --> 12:03:33,116
error
20114
12:03:33,116 --> 12:03:34,160
right
20115
12:03:34,160 --> 12:03:35,596
okay cool we're actually pretty much
20116
12:03:35,596 --> 12:03:37,200
done with the fetch movies already we're
20117
12:03:37,200 --> 12:03:39,200
fetching the data right
20118
12:03:39,200 --> 12:03:42,720
but then we want to take this whole data
20119
12:03:42,720 --> 12:03:45,916
and show it onto our app
20120
12:03:45,916 --> 12:03:49,116
right so how do we show movies right
20121
12:03:49,116 --> 12:03:50,640
luckily we have a function for that show
20122
12:03:50,640 --> 12:03:52,080
movies all right so what i'm going to do
20123
12:03:52,080 --> 12:03:53,360
is i'm going to erase this because we
20124
12:03:53,360 --> 12:03:55,360
don't want to call fetch movies here all
20125
12:03:55,360 --> 12:03:56,240
right we're going to call it somewhere
20126
12:03:56,240 --> 12:03:58,160
else what we want to care about now is
20127
12:03:58,160 --> 12:04:00,000
show movies right
20128
12:04:00,000 --> 12:04:01,360
show movies
20129
12:04:01,360 --> 12:04:03,520
is exactly what it sounds like and its
20130
12:04:03,520 --> 12:04:05,680
job is to display that movies
20131
12:04:05,680 --> 12:04:07,436
that object that we got back all right
20132
12:04:07,436 --> 12:04:08,480
this thing
20133
12:04:08,480 --> 12:04:10,240
this results
20134
12:04:10,240 --> 12:04:11,276
array
20135
12:04:11,276 --> 12:04:14,080
and display it like all of these 20 and
20136
12:04:14,080 --> 12:04:17,200
display it into our dom over here right
20137
12:04:17,200 --> 12:04:18,480
so that means we're going to use the dom
20138
12:04:18,480 --> 12:04:21,916
and how do we select things from the dom
20139
12:04:21,916 --> 12:04:22,800
all right
20140
12:04:22,800 --> 12:04:24,400
so one of the very first things that
20141
12:04:24,400 --> 12:04:26,640
we're going to do is create a variable
20142
12:04:26,640 --> 12:04:29,756
that grabs the id or class
20143
12:04:29,756 --> 12:04:32,480
right why do i say or class right
20144
12:04:32,480 --> 12:04:34,640
that's because
20145
12:04:34,640 --> 12:04:36,080
we have a class that's going to hold
20146
12:04:36,080 --> 12:04:38,800
original movies and the rest are ids
20147
12:04:38,800 --> 12:04:39,680
right
20148
12:04:39,680 --> 12:04:40,560
so
20149
12:04:40,560 --> 12:04:41,840
what we're going to do is make a
20150
12:04:41,840 --> 12:04:43,200
variable var
20151
12:04:43,200 --> 12:04:44,720
movies
20152
12:04:44,720 --> 12:04:45,756
right
20153
12:04:45,756 --> 12:04:48,480
element right because it's a dom element
20154
12:04:48,480 --> 12:04:50,160
is equal to
20155
12:04:50,160 --> 12:04:52,880
so if i wanted to get a class or an id
20156
12:04:52,880 --> 12:04:54,640
right i'm not sure what it is
20157
12:04:54,640 --> 12:04:58,160
can i use document.getelementbyid
20158
12:04:58,160 --> 12:05:00,800
not get element by id right this is only
20159
12:05:00,800 --> 12:05:02,080
going to grab an id it's not going to
20160
12:05:02,080 --> 12:05:03,200
get a class
20161
12:05:03,200 --> 12:05:04,160
how about
20162
12:05:04,160 --> 12:05:06,160
by class name right this could work too
20163
12:05:06,160 --> 12:05:08,160
but that's only going to be class
20164
12:05:08,160 --> 12:05:11,520
so what i want to do is grab query
20165
12:05:11,520 --> 12:05:15,000
selector document.we're
20166
12:05:15,276 --> 12:05:16,520
right
20167
12:05:16,520 --> 12:05:18,400
document.queryselector if you don't know
20168
12:05:18,400 --> 12:05:20,000
it's just going to grab
20169
12:05:20,000 --> 12:05:24,080
the first instance of the class or id
20170
12:05:24,080 --> 12:05:25,520
right what do i mean
20171
12:05:25,520 --> 12:05:28,960
let's say we have four boxes right and
20172
12:05:28,960 --> 12:05:31,360
they're all classes called box it's
20173
12:05:31,360 --> 12:05:33,116
gonna grab the first box if you have
20174
12:05:33,116 --> 12:05:35,680
four ids it's gonna grab the four id
20175
12:05:35,680 --> 12:05:36,800
luckily
20176
12:05:36,800 --> 12:05:38,320
right we only have one of these one of
20177
12:05:38,320 --> 12:05:40,320
these one of these so
20178
12:05:40,320 --> 12:05:42,640
it's always gonna pick the right one
20179
12:05:42,640 --> 12:05:44,880
so query selector and how do i make it
20180
12:05:44,880 --> 12:05:47,116
so it's dynamic right i want to grab the
20181
12:05:47,116 --> 12:05:49,436
trending one so i could just write you
20182
12:05:49,436 --> 12:05:51,840
know trending here
20183
12:05:51,840 --> 12:05:53,436
and it would only grab trending right
20184
12:05:53,436 --> 12:05:56,640
it's static but to make it dynamic right
20185
12:05:56,640 --> 12:05:58,640
we have access to
20186
12:05:58,640 --> 12:06:00,800
dom underscore element
20187
12:06:00,800 --> 12:06:02,480
and where does dom element come from it
20188
12:06:02,480 --> 12:06:04,560
comes from this argument here and that's
20189
12:06:04,560 --> 12:06:06,560
what we're passing in
20190
12:06:06,560 --> 12:06:09,200
okay cool
20191
12:06:09,200 --> 12:06:11,200
next thing all right we're going to loop
20192
12:06:11,200 --> 12:06:13,040
through
20193
12:06:13,040 --> 12:06:15,040
movies object
20194
12:06:15,040 --> 12:06:15,840
so
20195
12:06:15,840 --> 12:06:18,000
remember the part on the javascript
20196
12:06:18,000 --> 12:06:20,320
course about objects there's a way to
20197
12:06:20,320 --> 12:06:22,320
loop through them and you can use
20198
12:06:22,320 --> 12:06:24,800
something called the four of loop right
20199
12:06:24,800 --> 12:06:26,720
so to use a four of loop you say four
20200
12:06:26,720 --> 12:06:29,360
just like we do with a for loop right
20201
12:06:29,360 --> 12:06:32,640
but to use four of you're just gonna say
20202
12:06:32,640 --> 12:06:34,160
four
20203
12:06:34,160 --> 12:06:35,040
let
20204
12:06:35,040 --> 12:06:36,560
movie
20205
12:06:36,560 --> 12:06:39,680
of the object name so what is the object
20206
12:06:39,680 --> 12:06:40,880
name here
20207
12:06:40,880 --> 12:06:42,640
right
20208
12:06:42,640 --> 12:06:45,276
it's going to be called movies right
20209
12:06:45,276 --> 12:06:48,160
but let's think about this
20210
12:06:48,160 --> 12:06:49,756
what am i looping through if i just say
20211
12:06:49,756 --> 12:06:51,360
movies
20212
12:06:51,360 --> 12:06:53,596
if i'm looking through movies i'm
20213
12:06:53,596 --> 12:06:56,480
looping through this whole object
20214
12:06:56,480 --> 12:06:58,720
right but where in this object does the
20215
12:06:58,720 --> 12:07:00,000
movies live
20216
12:07:00,000 --> 12:07:01,916
it lives in the results
20217
12:07:01,916 --> 12:07:03,116
right so i want to be looping through
20218
12:07:03,116 --> 12:07:04,800
the array
20219
12:07:04,800 --> 12:07:06,800
right of objects so
20220
12:07:06,800 --> 12:07:08,560
how can i if this is if movies is an
20221
12:07:08,560 --> 12:07:10,480
object how do i get the results from
20222
12:07:10,480 --> 12:07:11,840
that object
20223
12:07:11,840 --> 12:07:12,960
aha
20224
12:07:12,960 --> 12:07:15,436
right you guys got it results
20225
12:07:15,436 --> 12:07:20,880
so let movie of movies dot results okay
20226
12:07:20,880 --> 12:07:24,080
so from here we can console.log
20227
12:07:24,080 --> 12:07:25,276
right
20228
12:07:25,276 --> 12:07:27,436
uh movie let's start let's stop there
20229
12:07:27,436 --> 12:07:29,756
just to make sense of it all right
20230
12:07:29,756 --> 12:07:32,400
and instead of console logging the data
20231
12:07:32,400 --> 12:07:34,640
in line 55 i don't want to print it i
20232
12:07:34,640 --> 12:07:36,560
want to call show
20233
12:07:36,560 --> 12:07:38,320
movies right
20234
12:07:38,320 --> 12:07:41,436
and for now we're going to say url right
20235
12:07:41,436 --> 12:07:44,240
that's what we need to pass here url
20236
12:07:44,240 --> 12:07:45,840
actually not url we should pass data
20237
12:07:45,840 --> 12:07:48,000
because that's the object right the data
20238
12:07:48,000 --> 12:07:49,680
is the movies
20239
12:07:49,680 --> 12:07:52,000
the dom element right we can keep that
20240
12:07:52,000 --> 12:07:54,160
the same and the path type we keep that
20241
12:07:54,160 --> 12:07:55,276
the same
20242
12:07:55,276 --> 12:07:56,320
cool
20243
12:07:56,320 --> 12:07:58,320
so to get this up and running right show
20244
12:07:58,320 --> 12:08:00,240
movies is gonna get called when i call
20245
12:08:00,240 --> 12:08:03,116
fetch movies so what we should do is
20246
12:08:03,116 --> 12:08:04,480
call fetch movies let's call fetch
20247
12:08:04,480 --> 12:08:06,080
movies again
20248
12:08:06,080 --> 12:08:08,400
test it out see if i have it pasted
20249
12:08:08,400 --> 12:08:11,116
right nice i still have the api
20250
12:08:11,116 --> 12:08:12,640
right then we're going to say the dom
20251
12:08:12,640 --> 12:08:16,160
element is the original and the original
20252
12:08:16,160 --> 12:08:17,680
is like this
20253
12:08:17,680 --> 12:08:21,916
all right original movies
20254
12:08:22,160 --> 12:08:24,560
yep okay so original movie just copy
20255
12:08:24,560 --> 12:08:29,880
that that's the class name comma
20256
12:08:30,240 --> 12:08:32,240
and it should be dot original movie
20257
12:08:32,240 --> 12:08:33,756
since it's a class
20258
12:08:33,756 --> 12:08:36,240
and in the comma we want
20259
12:08:36,240 --> 12:08:39,520
poster path poster underscore path
20260
12:08:39,520 --> 12:08:40,960
all right so let's hit
20261
12:08:40,960 --> 12:08:42,640
refresh on this
20262
12:08:42,640 --> 12:08:44,000
and you see this console log where is
20263
12:08:44,000 --> 12:08:46,160
this console coming from it's coming
20264
12:08:46,160 --> 12:08:47,200
from
20265
12:08:47,200 --> 12:08:49,436
this in line 70. so we're consolidating
20266
12:08:49,436 --> 12:08:50,800
what a movie is
20267
12:08:50,800 --> 12:08:52,640
so what is a movie then what does this
20268
12:08:52,640 --> 12:08:53,840
tell you
20269
12:08:53,840 --> 12:08:55,040
right
20270
12:08:55,040 --> 12:08:56,560
it tells me
20271
12:08:56,560 --> 12:08:59,360
that these are all the objects
20272
12:08:59,360 --> 12:09:03,040
inside of the movies array all right if
20273
12:09:03,040 --> 12:09:04,560
our console log
20274
12:09:04,560 --> 12:09:08,040
up top here console.log
20275
12:09:08,040 --> 12:09:11,276
movies.results right
20276
12:09:11,276 --> 12:09:13,360
let's hit refresh on this
20277
12:09:13,360 --> 12:09:16,560
at the top here see this array of 20.
20278
12:09:16,560 --> 12:09:19,360
that is movies.results
20279
12:09:19,360 --> 12:09:20,640
we're we're looping through
20280
12:09:20,640 --> 12:09:23,360
movies.results so movie
20281
12:09:23,360 --> 12:09:25,680
is each individual object so this is the
20282
12:09:25,680 --> 12:09:27,200
first one which is stranger things this
20283
12:09:27,200 --> 12:09:29,360
was all of us are dead lucifer squid
20284
12:09:29,360 --> 12:09:31,916
game first kill right it's exactly what
20285
12:09:31,916 --> 12:09:34,000
it sounds like every movie
20286
12:09:34,000 --> 12:09:36,640
in movie results
20287
12:09:36,640 --> 12:09:38,800
okay so when i say movie i'm talking
20288
12:09:38,800 --> 12:09:41,276
about each movie cool
20289
12:09:41,276 --> 12:09:42,880
so what do we want to do with this loop
20290
12:09:42,880 --> 12:09:44,400
then all right what do we want to do
20291
12:09:44,400 --> 12:09:45,916
with each movie
20292
12:09:45,916 --> 12:09:47,040
well
20293
12:09:47,040 --> 12:09:48,880
with each movie
20294
12:09:48,880 --> 12:09:49,756
right
20295
12:09:49,756 --> 12:09:53,116
i want to create an image element right
20296
12:09:53,116 --> 12:09:54,560
there should be an image element if you
20297
12:09:54,560 --> 12:09:56,480
remember the finished app
20298
12:09:56,480 --> 12:09:57,276
right
20299
12:09:57,276 --> 12:09:58,880
let's uh bring it back again and keep
20300
12:09:58,880 --> 12:10:01,840
bringing it back i should just keep it
20301
12:10:01,840 --> 12:10:03,360
let's go here
20302
12:10:03,360 --> 12:10:04,800
open here
20303
12:10:04,800 --> 12:10:06,560
and let's look
20304
12:10:06,560 --> 12:10:07,360
oop
20305
12:10:07,360 --> 12:10:09,680
let's look at create netflix
20306
12:10:09,680 --> 12:10:12,720
each of these is an image tag with the
20307
12:10:12,720 --> 12:10:14,960
data of that movie right so what we need
20308
12:10:14,960 --> 12:10:17,520
to do is for every movie and movie
20309
12:10:17,520 --> 12:10:20,480
result let's create an image element
20310
12:10:20,480 --> 12:10:22,000
right so we can actually create that by
20311
12:10:22,000 --> 12:10:24,400
making a variable so we can say i've
20312
12:10:24,400 --> 12:10:26,000
been saying var let's just say let to
20313
12:10:26,000 --> 12:10:28,160
keep it consistent let
20314
12:10:28,160 --> 12:10:31,436
and let so let's say let image element
20315
12:10:31,436 --> 12:10:33,200
right
20316
12:10:33,200 --> 12:10:36,400
and set this equal to document
20317
12:10:36,400 --> 12:10:37,520
dot
20318
12:10:37,520 --> 12:10:39,040
create
20319
12:10:39,040 --> 12:10:40,880
element all right this is how you create
20320
12:10:40,880 --> 12:10:42,720
an element using the dom
20321
12:10:42,720 --> 12:10:45,840
img this is making an image tag right so
20322
12:10:45,840 --> 12:10:48,160
this is this comment right here
20323
12:10:48,160 --> 12:10:51,596
within loop create image element
20324
12:10:51,596 --> 12:10:54,360
all right let's get these also in here
20325
12:10:54,360 --> 12:10:55,520
[Music]
20326
12:10:55,520 --> 12:10:58,400
set attribute
20327
12:10:59,116 --> 12:11:04,276
set source i can actually copy all these
20328
12:11:06,240 --> 12:11:08,480
awesome all right so now we need to set
20329
12:11:08,480 --> 12:11:10,960
an attribute right
20330
12:11:10,960 --> 12:11:14,240
so the attribute of this
20331
12:11:14,240 --> 12:11:15,520
is going to be
20332
12:11:15,520 --> 12:11:16,480
image
20333
12:11:16,480 --> 12:11:18,080
element right that's the variable
20334
12:11:18,080 --> 12:11:20,800
holding the element and we're gonna make
20335
12:11:20,800 --> 12:11:23,680
an image tag for every single movie
20336
12:11:23,680 --> 12:11:24,880
all right
20337
12:11:24,880 --> 12:11:28,960
image element dot set attribute right
20338
12:11:28,960 --> 12:11:31,040
is data
20339
12:11:31,040 --> 12:11:32,720
dash id
20340
12:11:32,720 --> 12:11:34,560
and then you're going to comma
20341
12:11:34,560 --> 12:11:36,480
movie right what's movie
20342
12:11:36,480 --> 12:11:38,800
movie is the object all right and how do
20343
12:11:38,800 --> 12:11:40,960
i get the id
20344
12:11:40,960 --> 12:11:42,880
right right here id
20345
12:11:42,880 --> 12:11:44,480
so how do i get an id of the movie
20346
12:11:44,480 --> 12:11:47,040
object you say movie.id
20347
12:11:47,040 --> 12:11:49,680
i'm saying attribute as data id okay
20348
12:11:49,680 --> 12:11:52,880
cool so now when you see set source here
20349
12:11:52,880 --> 12:11:54,880
that is the actual image itself right
20350
12:11:54,880 --> 12:11:56,640
this is creating the image tag this is
20351
12:11:56,640 --> 12:11:59,596
giving it an attribute of the id this is
20352
12:11:59,596 --> 12:12:01,596
putting the picture in the element make
20353
12:12:01,596 --> 12:12:02,640
sense
20354
12:12:02,640 --> 12:12:03,520
cool
20355
12:12:03,520 --> 12:12:06,320
so we can do an image element dot source
20356
12:12:06,320 --> 12:12:07,756
right that's how you change it it's
20357
12:12:07,756 --> 12:12:09,040
equal to
20358
12:12:09,040 --> 12:12:11,596
and then we have to have the link
20359
12:12:11,596 --> 12:12:14,560
so the api link for this is universal
20360
12:12:14,560 --> 12:12:15,276
right
20361
12:12:15,276 --> 12:12:19,360
it's going to be something like this
20362
12:12:20,320 --> 12:12:21,916
boom
20363
12:12:21,916 --> 12:12:22,800
image
20364
12:12:22,800 --> 12:12:25,436
element.source is equal to
20365
12:12:25,436 --> 12:12:26,240
this
20366
12:12:26,240 --> 12:12:28,160
image path right where am i getting this
20367
12:12:28,160 --> 12:12:30,160
from well if you look
20368
12:12:30,160 --> 12:12:32,960
that is the url of the image and over
20369
12:12:32,960 --> 12:12:35,596
here it just needs the path to add on to
20370
12:12:35,596 --> 12:12:36,320
it
20371
12:12:36,320 --> 12:12:38,320
right so we put this
20372
12:12:38,320 --> 12:12:39,756
after
20373
12:12:39,756 --> 12:12:41,040
original
20374
12:12:41,040 --> 12:12:43,360
then you get the backdrop but if you put
20375
12:12:43,360 --> 12:12:45,200
oops if you put
20376
12:12:45,200 --> 12:12:48,080
this poster path in front of the url you
20377
12:12:48,080 --> 12:12:49,756
get the poster
20378
12:12:49,756 --> 12:12:51,276
that's how this works
20379
12:12:51,276 --> 12:12:53,436
right but now what is this what is this
20380
12:12:53,436 --> 12:12:54,320
uh
20381
12:12:54,320 --> 12:12:57,116
dollar sign template movie bracket path
20382
12:12:57,116 --> 12:12:58,000
type
20383
12:12:58,000 --> 12:12:59,116
well
20384
12:12:59,116 --> 12:13:00,720
this is going to get you
20385
12:13:00,720 --> 12:13:03,116
it's a dynamic way to go into the movie
20386
12:13:03,116 --> 12:13:04,080
object
20387
12:13:04,080 --> 12:13:04,800
and
20388
12:13:04,800 --> 12:13:06,480
path type remember it's dynamic it could
20389
12:13:06,480 --> 12:13:08,720
be backdrop or poster
20390
12:13:08,720 --> 12:13:10,880
if this says if this were to say
20391
12:13:10,880 --> 12:13:12,880
something like um
20392
12:13:12,880 --> 12:13:13,840
back
20393
12:13:13,840 --> 12:13:15,436
drop
20394
12:13:15,436 --> 12:13:17,040
path then it's going to get only the
20395
12:13:17,040 --> 12:13:19,840
backdrops it's static if i said poster
20396
12:13:19,840 --> 12:13:21,200
path it's only going to get the posters
20397
12:13:21,200 --> 12:13:23,040
but if i say
20398
12:13:23,040 --> 12:13:25,840
path type
20399
12:13:27,916 --> 12:13:30,160
path type then it's going to get
20400
12:13:30,160 --> 12:13:34,000
whatever i passed in so now it's dynamic
20401
12:13:34,000 --> 12:13:35,436
cool
20402
12:13:35,436 --> 12:13:38,720
oh i copied it over
20403
12:13:38,720 --> 12:13:41,200
there we go
20404
12:13:41,360 --> 12:13:44,480
now i can go back like so perfect
20405
12:13:44,480 --> 12:13:46,480
so the next thing we need to do is
20406
12:13:46,480 --> 12:13:49,276
uh we actually don't need to add these
20407
12:13:49,276 --> 12:13:51,436
an event listener i'm gonna take that
20408
12:13:51,436 --> 12:13:54,560
out but we can add this
20409
12:13:54,560 --> 12:13:57,200
append the image element to the dom
20410
12:13:57,200 --> 12:13:58,800
element selected right what does that
20411
12:13:58,800 --> 12:14:01,360
mean right what it means is let's say i
20412
12:14:01,360 --> 12:14:03,360
want to add original movies or that's
20413
12:14:03,360 --> 12:14:05,436
what this data is it should go
20414
12:14:05,436 --> 12:14:08,480
underneath netflix originals right so
20415
12:14:08,480 --> 12:14:10,320
how do i put those image tags right
20416
12:14:10,320 --> 12:14:11,840
that's the step we
20417
12:14:11,840 --> 12:14:13,276
fetched the data
20418
12:14:13,276 --> 12:14:14,480
we
20419
12:14:14,480 --> 12:14:16,640
got each individual movie
20420
12:14:16,640 --> 12:14:19,596
we created an image tag we put the
20421
12:14:19,596 --> 12:14:21,116
picture in there
20422
12:14:21,116 --> 12:14:22,480
where do we put them that's what the
20423
12:14:22,480 --> 12:14:25,276
step is where do we put those images
20424
12:14:25,276 --> 12:14:27,436
right and where we put them is going to
20425
12:14:27,436 --> 12:14:30,400
depend on what we put as the dom element
20426
12:14:30,400 --> 12:14:32,720
so if you look under
20427
12:14:32,720 --> 12:14:34,640
the original movies
20428
12:14:34,640 --> 12:14:36,240
this is what we need we need to put it
20429
12:14:36,240 --> 12:14:38,560
in this div right so how do we put stuff
20430
12:14:38,560 --> 12:14:39,756
in this div
20431
12:14:39,756 --> 12:14:41,916
well we're going to use the append
20432
12:14:41,916 --> 12:14:42,880
method
20433
12:14:42,880 --> 12:14:44,160
right
20434
12:14:44,160 --> 12:14:46,720
so let's come down below right here
20435
12:14:46,720 --> 12:14:49,520
and we can say movies
20436
12:14:49,520 --> 12:14:50,880
element
20437
12:14:50,880 --> 12:14:54,400
right dot append
20438
12:14:54,400 --> 12:14:57,916
child right so child right is whatever's
20439
12:14:57,916 --> 12:15:00,080
inside the div right so
20440
12:15:00,080 --> 12:15:02,160
basically
20441
12:15:02,160 --> 12:15:03,276
add
20442
12:15:03,276 --> 12:15:06,400
image element
20443
12:15:06,400 --> 12:15:08,880
to the movie element right so now we're
20444
12:15:08,880 --> 12:15:11,680
combining it together so l
20445
12:15:11,680 --> 12:15:13,040
element
20446
12:15:13,040 --> 12:15:15,596
oh element there you go so now we
20447
12:15:15,596 --> 12:15:17,436
appended the child to the dom element
20448
12:15:17,436 --> 12:15:18,720
selected
20449
12:15:18,720 --> 12:15:21,116
yep so we're selecting the dom with dom
20450
12:15:21,116 --> 12:15:23,116
element and it's movies l and when we
20451
12:15:23,116 --> 12:15:25,756
say movies l add an image element to
20452
12:15:25,756 --> 12:15:27,360
that movies right so what is that
20453
12:15:27,360 --> 12:15:28,640
happening visually basically add
20454
12:15:28,640 --> 12:15:30,400
stranger things to that div add this
20455
12:15:30,400 --> 12:15:32,640
movie to that div add lucifer to that
20456
12:15:32,640 --> 12:15:35,116
div it's going to do this one by one
20457
12:15:35,116 --> 12:15:36,880
because of the loop
20458
12:15:36,880 --> 12:15:39,436
for every movie in movie result create
20459
12:15:39,436 --> 12:15:42,000
an image tag set the attribute put the
20460
12:15:42,000 --> 12:15:44,800
source in and add it to the div and then
20461
12:15:44,800 --> 12:15:47,116
again create the image tag create the
20462
12:15:47,116 --> 12:15:49,596
source add it to the div until
20463
12:15:49,596 --> 12:15:51,276
all 20 is done
20464
12:15:51,276 --> 12:15:53,596
makes sense and that's guess what that's
20465
12:15:53,596 --> 12:15:56,000
it that's show movies so if i were to
20466
12:15:56,000 --> 12:15:57,680
let's hit refresh
20467
12:15:57,680 --> 12:15:59,200
on this guy
20468
12:15:59,200 --> 12:16:01,116
see what happens
20469
12:16:01,116 --> 12:16:03,680
hey look at that exactly how i said it
20470
12:16:03,680 --> 12:16:05,436
it appended
20471
12:16:05,436 --> 12:16:07,116
a movie element
20472
12:16:07,116 --> 12:16:09,116
onto netflix original so the first part
20473
12:16:09,116 --> 12:16:11,040
is done right
20474
12:16:11,040 --> 12:16:12,800
that's sick right but now it's gonna go
20475
12:16:12,800 --> 12:16:14,320
away because i'm gonna erase fetch
20476
12:16:14,320 --> 12:16:16,880
movies right because this is a static
20477
12:16:16,880 --> 12:16:18,800
way of doing that like the point of this
20478
12:16:18,800 --> 12:16:21,436
exercise is to show you dynamic api
20479
12:16:21,436 --> 12:16:24,160
calls right so i erased it and now if i
20480
12:16:24,160 --> 12:16:25,360
refresh
20481
12:16:25,360 --> 12:16:26,800
we lost the progress but you guys know
20482
12:16:26,800 --> 12:16:28,240
how it works now right
20483
12:16:28,240 --> 12:16:30,240
so with that said
20484
12:16:30,240 --> 12:16:31,360
right
20485
12:16:31,360 --> 12:16:34,640
show movies is done fetch movies is done
20486
12:16:34,640 --> 12:16:37,276
now let's make the dynamic calls right
20487
12:16:37,276 --> 12:16:39,200
notice how i have the api already for
20488
12:16:39,200 --> 12:16:42,880
you what you need to do is just say let
20489
12:16:42,880 --> 12:16:44,000
url
20490
12:16:44,000 --> 12:16:45,040
right
20491
12:16:45,040 --> 12:16:47,596
let url equal to this
20492
12:16:47,596 --> 12:16:49,596
right and then we call fetch movies
20493
12:16:49,596 --> 12:16:51,360
remember the thing that we just erased
20494
12:16:51,360 --> 12:16:53,360
right now we can add it back in but this
20495
12:16:53,360 --> 12:16:55,680
is forget originals so
20496
12:16:55,680 --> 12:16:57,916
d you could type in this whole string
20497
12:16:57,916 --> 12:16:59,916
here but it would look very long
20498
12:16:59,916 --> 12:17:00,720
right
20499
12:17:00,720 --> 12:17:01,840
you could
20500
12:17:01,840 --> 12:17:03,596
just type in url
20501
12:17:03,596 --> 12:17:06,400
why that's because we have url right
20502
12:17:06,400 --> 12:17:08,640
here all right holding this long string
20503
12:17:08,640 --> 12:17:10,240
that's another way another trick for you
20504
12:17:10,240 --> 12:17:12,800
guys if you have a really long api call
20505
12:17:12,800 --> 12:17:14,720
and you don't want to you know fill up
20506
12:17:14,720 --> 12:17:16,080
your function calls with that long
20507
12:17:16,080 --> 12:17:17,360
string you can just store it in a
20508
12:17:17,360 --> 12:17:19,436
shorter variable all right making use of
20509
12:17:19,436 --> 12:17:20,720
variables here
20510
12:17:20,720 --> 12:17:21,680
and then
20511
12:17:21,680 --> 12:17:23,916
put the same exact parameters dot
20512
12:17:23,916 --> 12:17:27,916
original underscore underscore movies
20513
12:17:27,916 --> 12:17:28,880
comma
20514
12:17:28,880 --> 12:17:30,240
poster
20515
12:17:30,240 --> 12:17:31,520
oops
20516
12:17:31,520 --> 12:17:34,480
the poster path
20517
12:17:34,960 --> 12:17:36,880
awesome so this is get originals and if
20518
12:17:36,880 --> 12:17:39,040
i were to hit refresh on here
20519
12:17:39,040 --> 12:17:41,680
it works again right it works again
20520
12:17:41,680 --> 12:17:42,800
because
20521
12:17:42,800 --> 12:17:44,480
why why is this working even though i
20522
12:17:44,480 --> 12:17:47,116
didn't call get originals well if you go
20523
12:17:47,116 --> 12:17:49,116
at the top remember that windows unload
20524
12:17:49,116 --> 12:17:50,720
it's already calling it
20525
12:17:50,720 --> 12:17:52,880
right so we are one third of the way
20526
12:17:52,880 --> 12:17:56,160
there the rest is simple
20527
12:17:56,160 --> 12:17:58,000
so call movies
20528
12:17:58,000 --> 12:17:59,916
let's figure out that so we can just
20529
12:17:59,916 --> 12:18:01,520
call fetch movies here
20530
12:18:01,520 --> 12:18:03,360
we can take this comment that i made for
20531
12:18:03,360 --> 12:18:04,880
you guys and do
20532
12:18:04,880 --> 12:18:06,640
let
20533
12:18:06,640 --> 12:18:09,680
url equal to
20534
12:18:09,680 --> 12:18:12,160
this right
20535
12:18:12,160 --> 12:18:14,560
and pretty much do the same thing
20536
12:18:14,560 --> 12:18:17,520
i can say url
20537
12:18:17,520 --> 12:18:20,080
and this time right it's different we
20538
12:18:20,080 --> 12:18:21,520
want to say
20539
12:18:21,520 --> 12:18:23,680
hashtag trending why are we saying
20540
12:18:23,680 --> 12:18:25,276
hashtag trending
20541
12:18:25,276 --> 12:18:28,000
it's hashtag because this is an id you
20542
12:18:28,000 --> 12:18:28,960
got it
20543
12:18:28,960 --> 12:18:30,320
so trending
20544
12:18:30,320 --> 12:18:32,480
and we want the
20545
12:18:32,480 --> 12:18:36,640
backdrop right the wide one backdrop
20546
12:18:36,640 --> 12:18:37,680
okay
20547
12:18:37,680 --> 12:18:39,680
i love coding because once you know the
20548
12:18:39,680 --> 12:18:41,360
individual piece
20549
12:18:41,360 --> 12:18:42,800
it starts to get a lot simpler because
20550
12:18:42,800 --> 12:18:44,840
it's the same pattern over and over
20551
12:18:44,840 --> 12:18:48,160
again okay so now that that's done if i
20552
12:18:48,160 --> 12:18:50,080
hit refresh here
20553
12:18:50,080 --> 12:18:52,400
hey trending is available
20554
12:18:52,400 --> 12:18:54,320
awesome
20555
12:18:54,320 --> 12:18:57,916
okay top rated same deal let's hit let
20556
12:18:57,916 --> 12:19:00,800
url equal boom
20557
12:19:00,800 --> 12:19:03,200
call fetch movies
20558
12:19:03,200 --> 12:19:06,080
right we can say url here
20559
12:19:06,080 --> 12:19:07,756
comma
20560
12:19:07,756 --> 12:19:10,000
the next thing is going to be top rated
20561
12:19:10,000 --> 12:19:12,800
and is that a class or an id
20562
12:19:12,800 --> 12:19:13,916
it is
20563
12:19:13,916 --> 12:19:16,800
an id top rated
20564
12:19:16,800 --> 12:19:18,320
all right so this isn't too bad right
20565
12:19:18,320 --> 12:19:22,560
guys if i do back drop underscore path
20566
12:19:22,560 --> 12:19:23,680
hit run
20567
12:19:23,680 --> 12:19:25,756
right at refresh
20568
12:19:25,756 --> 12:19:29,756
let's frickin go and with that said guys
20569
12:19:29,756 --> 12:19:32,560
this is your netflix app where you can
20570
12:19:32,560 --> 12:19:34,080
go big and small
20571
12:19:34,080 --> 12:19:36,400
look at that
20572
12:19:36,400 --> 12:19:38,560
awesome so look at that you made netflix
20573
12:19:38,560 --> 12:19:41,436
you've got it working your boss is happy
20574
12:19:41,436 --> 12:19:44,560
right feel free to take this project and
20575
12:19:44,560 --> 12:19:46,240
do whatever you want with it right you
20576
12:19:46,240 --> 12:19:48,560
can make your own netflix you can make
20577
12:19:48,560 --> 12:19:49,916
disney plus
20578
12:19:49,916 --> 12:19:52,000
right but this is how it works you guys
20579
12:19:52,000 --> 12:19:54,000
learn how to make apis and they're
20580
12:19:54,000 --> 12:19:56,800
dynamic so congratulations this is it
20581
12:19:56,800 --> 12:19:58,080
for the project
20582
12:19:58,080 --> 12:20:00,160
give yourself a round of applause and
20583
12:20:00,160 --> 12:20:02,000
this has been awesome
20584
12:20:02,000 --> 12:20:05,276
wow that was a lot i mean that was over
20585
12:20:05,276 --> 12:20:08,080
10 hours of content if you got to hear
20586
12:20:08,080 --> 12:20:10,320
start to finish first of all i love you
20587
12:20:10,320 --> 12:20:12,960
my adsense loves you thank you so much
20588
12:20:12,960 --> 12:20:15,680
and i think your skills javascript
20589
12:20:15,680 --> 12:20:17,360
skills went
20590
12:20:17,360 --> 12:20:19,436
way higher now you're still not a pro
20591
12:20:19,436 --> 12:20:21,756
but this is way more than you might have
20592
12:20:21,756 --> 12:20:24,480
ever done okay and you definitely have
20593
12:20:24,480 --> 12:20:25,756
increased your skill set so give
20594
12:20:25,756 --> 12:20:27,680
yourself a huge pat on the back i'm
20595
12:20:27,680 --> 12:20:29,596
really proud of you for getting to here
20596
12:20:29,596 --> 12:20:32,560
i hope this was exciting riveting fun
20597
12:20:32,560 --> 12:20:35,200
engaging insightful interesting and more
20598
12:20:35,200 --> 12:20:37,040
than anything else just curiosity
20599
12:20:37,040 --> 12:20:39,040
inducing i hope that you want to
20600
12:20:39,040 --> 12:20:40,960
endeavor on this journey and on this
20601
12:20:40,960 --> 12:20:43,116
path to like learn more javascript and
20602
12:20:43,116 --> 12:20:45,436
just improve your coding skills i had a
20603
12:20:45,436 --> 12:20:47,360
lot of fun making this for you our team
20604
12:20:47,360 --> 12:20:49,840
had clever programmer me and lance david
20605
12:20:49,840 --> 12:20:51,680
everybody had so much fun putting this
20606
12:20:51,680 --> 12:20:53,200
course together for you i hope you
20607
12:20:53,200 --> 12:20:55,916
enjoyed it but let this be just a start
20608
12:20:55,916 --> 12:20:58,000
in your journey there's so much more for
20609
12:20:58,000 --> 12:21:00,480
you to do i can't wait to see what types
20610
12:21:00,480 --> 12:21:01,756
of projects you build because that's
20611
12:21:01,756 --> 12:21:03,276
what you need to do now okay it's all
20612
12:21:03,276 --> 12:21:04,880
about building projects with this and
20613
12:21:04,880 --> 12:21:07,116
taking this to a whole another level so
20614
12:21:07,116 --> 12:21:08,800
if you enjoyed this smash that like
20615
12:21:08,800 --> 12:21:10,400
button subscribe to the channel because
20616
12:21:10,400 --> 12:21:11,916
this is the channel where you will
20617
12:21:11,916 --> 12:21:13,916
become the best developer on planet
20618
12:21:13,916 --> 12:21:17,116
earth and look out for more content okay
20619
12:21:17,116 --> 12:21:18,880
so thanks so much for watching and as
20620
12:21:18,880 --> 12:21:21,520
always this is qazi and i'll see you in
20621
12:21:21,520 --> 12:21:24,480
the next video
1386932
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.