Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,080 --> 00:00:01,920
hey guys in this video i'm gonna teach
2
00:00:01,920 --> 00:00:03,360
you everything you need to know to get
3
00:00:03,360 --> 00:00:06,080
started writing code in javascript so
4
00:00:06,080 --> 00:00:10,400
sit back relax and enjoy the show
5
00:00:10,800 --> 00:00:12,960
if you wouldn't mind please leave a like
6
00:00:12,960 --> 00:00:15,599
comment and subscribe one like equals
7
00:00:15,599 --> 00:00:18,080
one prayer for the youtube algorithm
8
00:00:18,080 --> 00:00:20,400
all right let's get started everybody
9
00:00:20,400 --> 00:00:22,800
what the heck is javascript javascript
10
00:00:22,800 --> 00:00:25,279
is a web-based interpreted programming
11
00:00:25,279 --> 00:00:27,279
language that's used to
12
00:00:27,279 --> 00:00:29,599
add interactive behavior to web pages
13
00:00:29,599 --> 00:00:31,840
build web and mobile applications to
14
00:00:31,840 --> 00:00:34,239
create command line tools and for game
15
00:00:34,239 --> 00:00:36,480
development i like games here are some
16
00:00:36,480 --> 00:00:38,239
important notes before we get started
17
00:00:38,239 --> 00:00:41,040
javascript is not the same as java even
18
00:00:41,040 --> 00:00:42,559
though they share the same name both
19
00:00:42,559 --> 00:00:44,480
languages were developed around the same
20
00:00:44,480 --> 00:00:48,079
time now knowing html and css is helpful
21
00:00:48,079 --> 00:00:50,480
but not necessary at least until we near
22
00:00:50,480 --> 00:00:52,320
the end of this series where we begin
23
00:00:52,320 --> 00:00:54,640
working extensively with the dom the
24
00:00:54,640 --> 00:00:56,480
document object model hey if you're
25
00:00:56,480 --> 00:00:58,399
interested i do have a one hour full
26
00:00:58,399 --> 00:01:00,239
course on both of those subjects what
27
00:01:00,239 --> 00:01:02,320
you'll need you'll need a web browser
28
00:01:02,320 --> 00:01:04,400
and a text editor you'll need a web
29
00:01:04,400 --> 00:01:07,200
browser well because javascript runs in
30
00:01:07,200 --> 00:01:08,880
a web browser i recommend using the
31
00:01:08,880 --> 00:01:12,080
latest version of chrome firefox safari
32
00:01:12,080 --> 00:01:14,400
opera whatever please do not use
33
00:01:14,400 --> 00:01:16,640
internet explorer i also recommend a
34
00:01:16,640 --> 00:01:19,200
text editor a few popular text editors
35
00:01:19,200 --> 00:01:21,680
include but are not limited to visual
36
00:01:21,680 --> 00:01:24,799
studio code sublime text replit which is
37
00:01:24,799 --> 00:01:27,119
an online text editor or notepad if you
38
00:01:27,119 --> 00:01:29,119
want to torture yourself in this series
39
00:01:29,119 --> 00:01:31,040
i'll be using google chrome as my web
40
00:01:31,040 --> 00:01:33,520
browser and visual studio code for my
41
00:01:33,520 --> 00:01:34,960
text editor if you would like to
42
00:01:34,960 --> 00:01:36,960
download visual studio code i'll show
43
00:01:36,960 --> 00:01:39,840
you how to download visual studio code
44
00:01:39,840 --> 00:01:42,159
head to this url
45
00:01:42,159 --> 00:01:45,159
code.visualstudio.com
46
00:01:45,360 --> 00:01:47,759
look for this blue drop down menu then
47
00:01:47,759 --> 00:01:49,920
download the latest stable version for
48
00:01:49,920 --> 00:01:52,000
your operating system i'm running
49
00:01:52,000 --> 00:01:54,479
windows i will install this for windows
50
00:01:54,479 --> 00:01:55,759
then your download should start
51
00:01:55,759 --> 00:01:57,520
automatically if it doesn't you can hit
52
00:01:57,520 --> 00:01:59,119
this big blue button
53
00:01:59,119 --> 00:02:00,560
then i'm just going to go ahead and open
54
00:02:00,560 --> 00:02:02,719
this
55
00:02:02,719 --> 00:02:04,640
accept the license agreement yes i
56
00:02:04,640 --> 00:02:07,040
actually did read it that fast
57
00:02:07,040 --> 00:02:08,239
if you would like you can create a
58
00:02:08,239 --> 00:02:10,160
desktop icon
59
00:02:10,160 --> 00:02:11,360
next
60
00:02:11,360 --> 00:02:13,840
install
61
00:02:15,599 --> 00:02:17,440
we might as well go ahead and launch vs
62
00:02:17,440 --> 00:02:19,920
code now
63
00:02:20,959 --> 00:02:22,720
within vs code the first thing i
64
00:02:22,720 --> 00:02:24,879
recommend doing is downloading the live
65
00:02:24,879 --> 00:02:26,800
server extension
66
00:02:26,800 --> 00:02:28,720
let's go over to the left navigation bar
67
00:02:28,720 --> 00:02:30,640
go to extensions
68
00:02:30,640 --> 00:02:32,000
then look up
69
00:02:32,000 --> 00:02:33,120
live
70
00:02:33,120 --> 00:02:36,120
server
71
00:02:36,480 --> 00:02:38,480
then we're gonna click on this
72
00:02:38,480 --> 00:02:40,000
let's pretend that this wasn't already
73
00:02:40,000 --> 00:02:41,760
installed
74
00:02:41,760 --> 00:02:44,160
then click install
75
00:02:44,160 --> 00:02:46,400
what live server does is that every time
76
00:02:46,400 --> 00:02:48,720
that you save your javascript file
77
00:02:48,720 --> 00:02:50,480
it will update your current web browser
78
00:02:50,480 --> 00:02:52,959
right away to reflect any changes it's a
79
00:02:52,959 --> 00:02:56,160
helpful development tool
80
00:02:56,160 --> 00:02:58,000
we're going to create a new folder to
81
00:02:58,000 --> 00:03:00,720
hold all of our files i'm going to go to
82
00:03:00,720 --> 00:03:02,480
open folder
83
00:03:02,480 --> 00:03:03,920
i'm going to create a new folder on my
84
00:03:03,920 --> 00:03:06,959
desktop i'm going to right click go to
85
00:03:06,959 --> 00:03:10,480
new folder i'll name this website click
86
00:03:10,480 --> 00:03:12,879
website select folder
87
00:03:12,879 --> 00:03:14,560
there we go now let's create a new
88
00:03:14,560 --> 00:03:17,599
javascript file go to new file i'll name
89
00:03:17,599 --> 00:03:19,239
this file
90
00:03:19,239 --> 00:03:21,760
index.js be sure to get that extension
91
00:03:21,760 --> 00:03:24,959
js because well that's a javascript file
92
00:03:24,959 --> 00:03:28,519
i'm also going to create an html file
93
00:03:28,519 --> 00:03:30,080
index.html
94
00:03:30,080 --> 00:03:31,760
on a website this would be the landing
95
00:03:31,760 --> 00:03:32,879
page
96
00:03:32,879 --> 00:03:35,840
then a css style sheet which i will name
97
00:03:35,840 --> 00:03:38,959
style dot css
98
00:03:38,959 --> 00:03:41,239
okay we have our three files
99
00:03:41,239 --> 00:03:44,400
index.javascript file our index.html
100
00:03:44,400 --> 00:03:45,360
file
101
00:03:45,360 --> 00:03:47,680
then our css stylesheet
102
00:03:47,680 --> 00:03:49,760
we'll need html skeleton code to work
103
00:03:49,760 --> 00:03:52,239
with so within your html file if you
104
00:03:52,239 --> 00:03:55,200
type exclamation point then hit tab
105
00:03:55,200 --> 00:03:57,200
that will generate some sample text to
106
00:03:57,200 --> 00:03:59,599
be used to create a webpage
107
00:03:59,599 --> 00:04:01,439
we'll want to take care of this early
108
00:04:01,439 --> 00:04:04,480
let's link our css style sheet within
109
00:04:04,480 --> 00:04:07,040
the head tags of my html document
110
00:04:07,040 --> 00:04:09,840
i'm going to create a link tag
111
00:04:09,840 --> 00:04:10,720
link
112
00:04:10,720 --> 00:04:14,640
take the rel attribute set the sql to
113
00:04:14,640 --> 00:04:16,320
style sheet
114
00:04:16,320 --> 00:04:18,798
the next attribute is href
115
00:04:18,798 --> 00:04:20,399
set the sql to
116
00:04:20,399 --> 00:04:23,479
your css stylesheet
117
00:04:23,479 --> 00:04:25,440
style.css
118
00:04:25,440 --> 00:04:27,280
so our stylesheet is now linked to our
119
00:04:27,280 --> 00:04:28,880
html file
120
00:04:28,880 --> 00:04:30,880
then we need to link our javascript file
121
00:04:30,880 --> 00:04:32,639
to our html file
122
00:04:32,639 --> 00:04:34,880
within the body of my html document i'm
123
00:04:34,880 --> 00:04:37,440
going to create a pair of script tags
124
00:04:37,440 --> 00:04:39,840
script that's the opening tag
125
00:04:39,840 --> 00:04:43,840
then forward slash script to close it
126
00:04:43,840 --> 00:04:46,160
within the opening script tag
127
00:04:46,160 --> 00:04:49,440
i will set the source attribute equal to
128
00:04:49,440 --> 00:04:52,080
the name of my javascript file mine is
129
00:04:52,080 --> 00:04:54,479
index.js
130
00:04:54,479 --> 00:04:56,720
then be sure to save everything to save
131
00:04:56,720 --> 00:04:58,720
myself from potential future headaches i
132
00:04:58,720 --> 00:05:00,720
tend to save all three files
133
00:05:00,720 --> 00:05:02,639
to display output from our javascript
134
00:05:02,639 --> 00:05:04,880
file we'll need to open up a web browser
135
00:05:04,880 --> 00:05:07,520
an easy way to do that would be to go to
136
00:05:07,520 --> 00:05:08,639
explore
137
00:05:08,639 --> 00:05:11,199
right click on your html file open with
138
00:05:11,199 --> 00:05:13,199
live server i now have this window to
139
00:05:13,199 --> 00:05:14,880
work with this window is a
140
00:05:14,880 --> 00:05:17,440
representation of my html file when
141
00:05:17,440 --> 00:05:19,360
coding with javascript i like to place
142
00:05:19,360 --> 00:05:21,520
my browser window and vs code right next
143
00:05:21,520 --> 00:05:23,280
to each other kind of like this to
144
00:05:23,280 --> 00:05:25,280
access our console which we can use to
145
00:05:25,280 --> 00:05:27,440
display output i'm going to right click
146
00:05:27,440 --> 00:05:31,520
within my window go to inspect
147
00:05:32,639 --> 00:05:35,520
click on these arrows then go to console
148
00:05:35,520 --> 00:05:37,680
and then just save
149
00:05:37,680 --> 00:05:39,840
any output within my javascript file
150
00:05:39,840 --> 00:05:42,000
will go to my console to display some
151
00:05:42,000 --> 00:05:44,160
output we can type
152
00:05:44,160 --> 00:05:46,800
console.log
153
00:05:46,800 --> 00:05:48,720
then a set of parentheses
154
00:05:48,720 --> 00:05:50,639
typically with programming you end a
155
00:05:50,639 --> 00:05:53,280
statement with a semicolon it's like the
156
00:05:53,280 --> 00:05:55,360
punctuation at the end of a sentence
157
00:05:55,360 --> 00:05:57,520
it's optional to add a semicolon to the
158
00:05:57,520 --> 00:05:59,520
end of a statement but in nearly all
159
00:05:59,520 --> 00:06:02,160
programming languages besides python to
160
00:06:02,160 --> 00:06:04,800
add a statement you need a semicolon i
161
00:06:04,800 --> 00:06:06,800
type it in just as a force of habit but
162
00:06:06,800 --> 00:06:08,880
it would still work regardless now let's
163
00:06:08,880 --> 00:06:10,880
type something let's type some output to
164
00:06:10,880 --> 00:06:12,960
the console window this can be either
165
00:06:12,960 --> 00:06:16,240
within single quotes or double quotes
166
00:06:16,240 --> 00:06:19,280
let's say something i like some food you
167
00:06:19,280 --> 00:06:21,360
like pizza
168
00:06:21,360 --> 00:06:23,520
i do have that live server extension set
169
00:06:23,520 --> 00:06:25,840
up when i save that will update my web
170
00:06:25,840 --> 00:06:28,479
browser within my console we have the
171
00:06:28,479 --> 00:06:31,600
statement that i made i like pizza if i
172
00:06:31,600 --> 00:06:33,440
would like to print another statement i
173
00:06:33,440 --> 00:06:34,400
can again
174
00:06:34,400 --> 00:06:35,280
type
175
00:06:35,280 --> 00:06:37,840
console.log
176
00:06:37,840 --> 00:06:39,759
then within either single quotes or
177
00:06:39,759 --> 00:06:43,120
double quotes i can say something else
178
00:06:43,120 --> 00:06:45,039
it's really
179
00:06:45,039 --> 00:06:46,800
good
180
00:06:46,800 --> 00:06:49,680
i like pizza it's really good you can
181
00:06:49,680 --> 00:06:52,160
also create an alert box by typing
182
00:06:52,160 --> 00:06:55,120
window dot alert
183
00:06:55,120 --> 00:06:56,720
within the parentheses
184
00:06:56,720 --> 00:06:58,800
within a set of quotes what would we
185
00:06:58,800 --> 00:07:00,319
like to say
186
00:07:00,319 --> 00:07:01,280
i
187
00:07:01,280 --> 00:07:03,919
really love pizza
188
00:07:03,919 --> 00:07:06,000
alert will create a pop-up
189
00:07:06,000 --> 00:07:08,479
i really love pizza
190
00:07:08,479 --> 00:07:10,240
there's also comments to create a
191
00:07:10,240 --> 00:07:12,960
comment you use two forward slashes
192
00:07:12,960 --> 00:07:16,080
comments are ignored by the interpreter
193
00:07:16,080 --> 00:07:17,919
this is a
194
00:07:17,919 --> 00:07:19,680
comment
195
00:07:19,680 --> 00:07:22,000
usually comments are used for notes for
196
00:07:22,000 --> 00:07:24,560
yourself or for other developers
197
00:07:24,560 --> 00:07:26,240
you could also write a multi-line
198
00:07:26,240 --> 00:07:29,680
comment by typing forward slash asterisk
199
00:07:29,680 --> 00:07:31,120
wherever you would like your comment to
200
00:07:31,120 --> 00:07:34,000
end you would type asterisk forward
201
00:07:34,000 --> 00:07:35,599
slash again
202
00:07:35,599 --> 00:07:36,720
this
203
00:07:36,720 --> 00:07:37,759
is
204
00:07:37,759 --> 00:07:39,360
a
205
00:07:39,360 --> 00:07:42,360
multi-line
206
00:07:42,840 --> 00:07:45,680
comment all right everybody that is a
207
00:07:45,680 --> 00:07:48,160
quick introduction to javascript and in
208
00:07:48,160 --> 00:07:50,639
the next topic we'll discuss variables
209
00:07:50,639 --> 00:07:52,160
all right everybody that's a quick
210
00:07:52,160 --> 00:07:54,080
introduction to javascript hey if you
211
00:07:54,080 --> 00:07:55,680
enjoyed this video let me know by
212
00:07:55,680 --> 00:07:57,520
smashing that like button leave a random
213
00:07:57,520 --> 00:07:59,360
comment down below and subscribe if
214
00:07:59,360 --> 00:08:02,800
you'd like to become a fellow bro
215
00:08:03,599 --> 00:08:05,840
all right everybody variables so a
216
00:08:05,840 --> 00:08:08,560
variable is a container for storing data
217
00:08:08,560 --> 00:08:10,879
it's a representation of some value
218
00:08:10,879 --> 00:08:12,720
think back to middle school algebra
219
00:08:12,720 --> 00:08:14,560
class where you had to solve an equation
220
00:08:14,560 --> 00:08:16,960
you have to solve for x and x is some
221
00:08:16,960 --> 00:08:19,199
representation of a value it's basically
222
00:08:19,199 --> 00:08:21,360
no different in programming a variable
223
00:08:21,360 --> 00:08:23,919
behaves as if it was the value that it
224
00:08:23,919 --> 00:08:25,919
contains there's two steps to creating a
225
00:08:25,919 --> 00:08:28,400
variable declaration and assignment to
226
00:08:28,400 --> 00:08:30,479
declare a variable we use one of three
227
00:08:30,479 --> 00:08:33,919
keywords either var let or const we'll
228
00:08:33,919 --> 00:08:36,000
discuss const in a future video but for
229
00:08:36,000 --> 00:08:38,640
now we can either use var or let but
230
00:08:38,640 --> 00:08:40,719
using let is best practice due to
231
00:08:40,719 --> 00:08:42,799
something called variable scope but that
232
00:08:42,799 --> 00:08:44,720
won't make too much sense until we reach
233
00:08:44,720 --> 00:08:46,959
the video on functions so for now for
234
00:08:46,959 --> 00:08:48,959
best practice to create a variable let's
235
00:08:48,959 --> 00:08:51,360
use the keyword let we precede the
236
00:08:51,360 --> 00:08:53,920
variable with let and now we need a
237
00:08:53,920 --> 00:08:56,399
unique descriptive name of this variable
238
00:08:56,399 --> 00:08:58,160
so if you're thinking of algebra you
239
00:08:58,160 --> 00:09:00,320
might think of a variable such as maybe
240
00:09:00,320 --> 00:09:03,680
x or y like they are some number but in
241
00:09:03,680 --> 00:09:05,360
programming you want a variable to be
242
00:09:05,360 --> 00:09:07,839
descriptive of what it contains let's
243
00:09:07,839 --> 00:09:10,160
assign a number to this variable maybe
244
00:09:10,160 --> 00:09:12,240
in this example we need to store a
245
00:09:12,240 --> 00:09:14,800
student's age so age would be a
246
00:09:14,800 --> 00:09:16,880
descriptive name for this variable and
247
00:09:16,880 --> 00:09:19,040
that is step one we have declared a
248
00:09:19,040 --> 00:09:21,519
unique variable named age but it doesn't
249
00:09:21,519 --> 00:09:22,959
yet contain a value that's where
250
00:09:22,959 --> 00:09:25,040
assignment comes in that's step two so
251
00:09:25,040 --> 00:09:27,600
let's try and use this variable
252
00:09:27,600 --> 00:09:29,519
console.log
253
00:09:29,519 --> 00:09:31,440
and i'm just going to display age and
254
00:09:31,440 --> 00:09:33,040
then let's see what happens if you
255
00:09:33,040 --> 00:09:34,880
declare a variable but do not assign it
256
00:09:34,880 --> 00:09:36,959
a value it's undefined so we haven't
257
00:09:36,959 --> 00:09:38,640
reached step two yet we've only declared
258
00:09:38,640 --> 00:09:40,560
a variable when you display a variable
259
00:09:40,560 --> 00:09:42,640
make sure it's not within quotes
260
00:09:42,640 --> 00:09:44,959
because then we're literally printing
261
00:09:44,959 --> 00:09:47,360
the word edge instead of the value
262
00:09:47,360 --> 00:09:49,600
contained within age the variable now
263
00:09:49,600 --> 00:09:51,920
step two is assignment we take the
264
00:09:51,920 --> 00:09:54,080
variable name and use the assignment
265
00:09:54,080 --> 00:09:56,800
operator which is an equal sign and set
266
00:09:56,800 --> 00:09:59,120
this equal to some value let's say that
267
00:09:59,120 --> 00:10:01,680
i'm 21 years old not anymore but i like
268
00:10:01,680 --> 00:10:03,360
to think that i still am now when i
269
00:10:03,360 --> 00:10:05,839
display this variable age it displays
270
00:10:05,839 --> 00:10:08,720
the number 21. so age is a variable it
271
00:10:08,720 --> 00:10:11,040
behaves as if it was the value that it
272
00:10:11,040 --> 00:10:13,120
contains now with creating a variable
273
00:10:13,120 --> 00:10:15,279
you can do this in two steps or you can
274
00:10:15,279 --> 00:10:17,360
combine them both together so instead of
275
00:10:17,360 --> 00:10:19,600
taking two lines of code we could
276
00:10:19,600 --> 00:10:21,519
combine them both together like this and
277
00:10:21,519 --> 00:10:23,200
this would do the same thing but there
278
00:10:23,200 --> 00:10:25,040
may be times when you need to declare a
279
00:10:25,040 --> 00:10:27,040
variable and then assign a value later
280
00:10:27,040 --> 00:10:28,959
on like maybe you're accepting some user
281
00:10:28,959 --> 00:10:31,040
input so in that case it would be better
282
00:10:31,040 --> 00:10:32,959
to do that in two steps different
283
00:10:32,959 --> 00:10:35,839
variables have different data types 21
284
00:10:35,839 --> 00:10:38,399
is a number data type we can use this
285
00:10:38,399 --> 00:10:40,800
variable in some arithmetic expressions
286
00:10:40,800 --> 00:10:42,959
maybe it's my birthday and i need to add
287
00:10:42,959 --> 00:10:45,279
one to my age i can easily change that
288
00:10:45,279 --> 00:10:49,360
by setting age equal to age plus one and
289
00:10:49,360 --> 00:10:52,720
my age is now 22 so this is a number
290
00:10:52,720 --> 00:10:55,680
data type another data type is a string
291
00:10:55,680 --> 00:10:58,640
a string is a series of characters let's
292
00:10:58,640 --> 00:11:00,720
create a variable to hold a student's
293
00:11:00,720 --> 00:11:01,760
name
294
00:11:01,760 --> 00:11:04,480
so we will use that let keyword
295
00:11:04,480 --> 00:11:07,120
and i'll create a variable name named
296
00:11:07,120 --> 00:11:10,240
first name then i could assign a value
297
00:11:10,240 --> 00:11:12,800
later or right away i'll do so right
298
00:11:12,800 --> 00:11:16,800
away so i will set first name equal to
299
00:11:16,800 --> 00:11:19,760
then to create a string we can use a set
300
00:11:19,760 --> 00:11:22,399
of quotes either double quotes or single
301
00:11:22,399 --> 00:11:23,360
quotes
302
00:11:23,360 --> 00:11:25,839
and i can add a series of characters so
303
00:11:25,839 --> 00:11:27,839
why don't you enter in your first name
304
00:11:27,839 --> 00:11:29,760
and now i can use this variable and it
305
00:11:29,760 --> 00:11:31,839
contains a series of characters let's
306
00:11:31,839 --> 00:11:33,519
display this within our console
307
00:11:33,519 --> 00:11:35,600
console.log
308
00:11:35,600 --> 00:11:36,480
first
309
00:11:36,480 --> 00:11:39,839
name so this displays bro 21 another
310
00:11:39,839 --> 00:11:42,720
data type is a boolean a boolean is only
311
00:11:42,720 --> 00:11:45,519
one of two values true or false
312
00:11:45,519 --> 00:11:47,839
this time let's say that we have a
313
00:11:47,839 --> 00:11:50,320
boolean variable named
314
00:11:50,320 --> 00:11:52,880
maybe student if a person is currently
315
00:11:52,880 --> 00:11:55,600
enrolled we could set student to equal
316
00:11:55,600 --> 00:11:57,920
true if they're not enrolled or maybe
317
00:11:57,920 --> 00:12:00,639
they graduated this could be false so a
318
00:12:00,639 --> 00:12:03,519
boolean is one of two values true or
319
00:12:03,519 --> 00:12:05,440
false then let's display this within our
320
00:12:05,440 --> 00:12:06,560
console
321
00:12:06,560 --> 00:12:08,160
console
322
00:12:08,160 --> 00:12:10,160
dot log
323
00:12:10,160 --> 00:12:11,839
student
324
00:12:11,839 --> 00:12:14,399
and this displays false or if i was a
325
00:12:14,399 --> 00:12:17,040
student this could be true so these are
326
00:12:17,040 --> 00:12:19,760
three common data types strings which
327
00:12:19,760 --> 00:12:22,839
are a series of characters
328
00:12:22,839 --> 00:12:24,399
numbers
329
00:12:24,399 --> 00:12:26,240
and booleans
330
00:12:26,240 --> 00:12:28,399
as a beginner it's actually really easy
331
00:12:28,399 --> 00:12:31,200
to mix up strings and numbers numbers we
332
00:12:31,200 --> 00:12:33,440
can use with arithmetic expressions
333
00:12:33,440 --> 00:12:35,360
strings we cannot let's use that
334
00:12:35,360 --> 00:12:37,839
previous example of incrementing my age
335
00:12:37,839 --> 00:12:40,480
by one because it's my birthday
336
00:12:40,480 --> 00:12:41,519
age
337
00:12:41,519 --> 00:12:44,399
equals age plus one we can use numbers
338
00:12:44,399 --> 00:12:46,959
in arithmetic expressions see my age is
339
00:12:46,959 --> 00:12:50,000
now 22 but if this was a string i'll put
340
00:12:50,000 --> 00:12:51,519
this within quotes
341
00:12:51,519 --> 00:12:52,639
string
342
00:12:52,639 --> 00:12:55,760
well when we add one to age it's going
343
00:12:55,760 --> 00:12:58,240
to concatenate one to the end of the
344
00:12:58,240 --> 00:13:01,440
string i am not 211 years old it's
345
00:13:01,440 --> 00:13:03,839
treating age as a series of characters
346
00:13:03,839 --> 00:13:06,079
and we're adding an additional character
347
00:13:06,079 --> 00:13:08,399
to age so as a beginner you really have
348
00:13:08,399 --> 00:13:10,720
to pay attention to data types it is
349
00:13:10,720 --> 00:13:12,560
really easy to put things within quotes
350
00:13:12,560 --> 00:13:14,399
when you don't mean to when you display
351
00:13:14,399 --> 00:13:17,040
a variable within the console or the dom
352
00:13:17,040 --> 00:13:18,880
you can display it along with some other
353
00:13:18,880 --> 00:13:21,120
text so along with my first name i'm
354
00:13:21,120 --> 00:13:24,079
going to display hello
355
00:13:24,079 --> 00:13:25,200
hello
356
00:13:25,200 --> 00:13:27,120
then after i finish the string i will
357
00:13:27,120 --> 00:13:30,959
add comma so hello bro with my second
358
00:13:30,959 --> 00:13:33,839
log method i'll print another message
359
00:13:33,839 --> 00:13:35,600
perhaps you
360
00:13:35,600 --> 00:13:37,519
are
361
00:13:37,519 --> 00:13:39,519
age
362
00:13:39,519 --> 00:13:41,920
years old
363
00:13:41,920 --> 00:13:45,199
hello bro you are 21 years old and what
364
00:13:45,199 --> 00:13:47,839
can we do with student um let's say
365
00:13:47,839 --> 00:13:50,320
maybe enrolled
366
00:13:50,320 --> 00:13:52,560
so this will display true if i'm
367
00:13:52,560 --> 00:13:54,000
currently enrolled
368
00:13:54,000 --> 00:13:56,560
or false if i'm no longer a student if
369
00:13:56,560 --> 00:13:58,480
you need to display a variable within
370
00:13:58,480 --> 00:14:00,959
your dom here's one thing that we can do
371
00:14:00,959 --> 00:14:03,519
so within our html document let's create
372
00:14:03,519 --> 00:14:06,160
maybe some paragraph tags i'll create
373
00:14:06,160 --> 00:14:07,040
three
374
00:14:07,040 --> 00:14:09,120
so we need an opening paragraph tag and
375
00:14:09,120 --> 00:14:11,120
a closing paragraph tag
376
00:14:11,120 --> 00:14:14,800
i will set a unique id of p1
377
00:14:14,800 --> 00:14:17,360
copy this paste it two times
378
00:14:17,360 --> 00:14:19,839
the second one will be p2 and the third
379
00:14:19,839 --> 00:14:22,720
will be p3 so within our dom we have
380
00:14:22,720 --> 00:14:25,279
three paragraph tags each with a unique
381
00:14:25,279 --> 00:14:28,480
id if i need to change the inner html of
382
00:14:28,480 --> 00:14:30,880
these paragraph tags i can do so using
383
00:14:30,880 --> 00:14:32,320
this statement
384
00:14:32,320 --> 00:14:33,920
document
385
00:14:33,920 --> 00:14:35,680
dot get
386
00:14:35,680 --> 00:14:37,120
element
387
00:14:37,120 --> 00:14:38,160
by
388
00:14:38,160 --> 00:14:39,519
id
389
00:14:39,519 --> 00:14:40,959
parentheses
390
00:14:40,959 --> 00:14:42,720
and then within a set of quotes i will
391
00:14:42,720 --> 00:14:45,519
list the id of the html tag i would like
392
00:14:45,519 --> 00:14:46,560
to change
393
00:14:46,560 --> 00:14:48,560
let's begin with p1 and then i will
394
00:14:48,560 --> 00:14:51,120
follow the statement with dot
395
00:14:51,120 --> 00:14:52,000
inner
396
00:14:52,000 --> 00:14:55,360
html and set the sequel to
397
00:14:55,360 --> 00:14:58,480
so let's display hello then to display a
398
00:14:58,480 --> 00:14:59,839
variable we will use some string
399
00:14:59,839 --> 00:15:03,600
concatenation so we will use plus
400
00:15:03,600 --> 00:15:06,320
a variable name let's say first name
401
00:15:06,320 --> 00:15:08,399
hello bro okay let's do the same thing
402
00:15:08,399 --> 00:15:09,760
with p2
403
00:15:09,760 --> 00:15:10,839
u
404
00:15:10,839 --> 00:15:14,079
r so when using string concatenation you
405
00:15:14,079 --> 00:15:15,839
do have to pay attention to spaces
406
00:15:15,839 --> 00:15:18,320
within strings
407
00:15:18,320 --> 00:15:20,480
you are age
408
00:15:20,480 --> 00:15:21,760
plus
409
00:15:21,760 --> 00:15:23,839
years old
410
00:15:23,839 --> 00:15:28,880
you are 21 years old and lastly p3
411
00:15:28,880 --> 00:15:31,199
uh let's say
412
00:15:31,199 --> 00:15:32,320
enrolled
413
00:15:32,320 --> 00:15:34,880
colon space
414
00:15:34,880 --> 00:15:36,639
student
415
00:15:36,639 --> 00:15:39,040
enrolled true and then you know you can
416
00:15:39,040 --> 00:15:41,199
change any one of these values
417
00:15:41,199 --> 00:15:43,680
well yeah everybody those are variables
418
00:15:43,680 --> 00:15:46,079
a variable is just a container for
419
00:15:46,079 --> 00:15:48,720
storing data it's a representation of
420
00:15:48,720 --> 00:15:51,360
some value a variable behaves as if it
421
00:15:51,360 --> 00:15:53,600
was the value that it contains there's
422
00:15:53,600 --> 00:15:56,880
two steps declaration and assignment you
423
00:15:56,880 --> 00:15:59,279
can do these both together or separate
424
00:15:59,279 --> 00:16:01,279
and variables have different data types
425
00:16:01,279 --> 00:16:03,440
three that we discussed are strings
426
00:16:03,440 --> 00:16:06,000
which are a series of characters
427
00:16:06,000 --> 00:16:08,560
numbers and booleans which are either
428
00:16:08,560 --> 00:16:11,120
true or false you can display variables
429
00:16:11,120 --> 00:16:13,279
along with text within your console or
430
00:16:13,279 --> 00:16:15,440
you could use string concatenation to
431
00:16:15,440 --> 00:16:17,920
display a variable's value along with
432
00:16:17,920 --> 00:16:20,079
some text in your dom so yeah those are
433
00:16:20,079 --> 00:16:21,839
variables everybody if you would like a
434
00:16:21,839 --> 00:16:23,680
copy of all this code i'll post this in
435
00:16:23,680 --> 00:16:25,360
the comments section down below if you
436
00:16:25,360 --> 00:16:26,880
enjoyed this video let me know by
437
00:16:26,880 --> 00:16:28,560
smashing that like button leave random
438
00:16:28,560 --> 00:16:30,399
comment down below and subscribe if
439
00:16:30,399 --> 00:16:33,920
you'd like to become a fellow bro
440
00:16:34,240 --> 00:16:36,240
hey everybody i have a super quick video
441
00:16:36,240 --> 00:16:38,639
on arithmetic expressions an arithmetic
442
00:16:38,639 --> 00:16:41,759
expression is a combination of operands
443
00:16:41,759 --> 00:16:44,720
and operators operands are values
444
00:16:44,720 --> 00:16:48,000
variables etc operators are well the
445
00:16:48,000 --> 00:16:50,639
addition sign subtraction multiplication
446
00:16:50,639 --> 00:16:52,800
division and modulus which we'll talk
447
00:16:52,800 --> 00:16:54,639
about a little bit later an arithmetic
448
00:16:54,639 --> 00:16:57,680
expression can be evaluated to a value
449
00:16:57,680 --> 00:17:01,199
for example y equals x plus five x and
450
00:17:01,199 --> 00:17:03,600
five would be operands and the addition
451
00:17:03,600 --> 00:17:05,839
sign would be the operator just really
452
00:17:05,839 --> 00:17:08,559
quick let's cover a few maybe we have a
453
00:17:08,559 --> 00:17:10,959
variable named students we're a teacher
454
00:17:10,959 --> 00:17:12,400
and we have a whole class of students to
455
00:17:12,400 --> 00:17:14,959
keep track of i need to add one student
456
00:17:14,959 --> 00:17:17,439
i could take our students variable and
457
00:17:17,439 --> 00:17:19,760
set the sequel to if i need to increment
458
00:17:19,760 --> 00:17:22,880
students by one i can set the sequel to
459
00:17:22,880 --> 00:17:25,679
students currently it's 20
460
00:17:25,679 --> 00:17:28,000
plus one and then let's display this
461
00:17:28,000 --> 00:17:30,559
console.log
462
00:17:30,559 --> 00:17:31,679
students
463
00:17:31,679 --> 00:17:34,320
and this should be 21. so we also have
464
00:17:34,320 --> 00:17:36,240
subtraction
465
00:17:36,240 --> 00:17:39,039
students equals students minus 1
466
00:17:39,039 --> 00:17:41,919
and now we have 19. for multiplication
467
00:17:41,919 --> 00:17:43,840
you use an asterisk maybe we're
468
00:17:43,840 --> 00:17:45,919
combining two classes students equal
469
00:17:45,919 --> 00:17:47,679
students times two
470
00:17:47,679 --> 00:17:49,840
and now there's forty students perhaps
471
00:17:49,840 --> 00:17:53,200
we're dividing a class in half
472
00:17:53,200 --> 00:17:55,360
students divided by two and you use a
473
00:17:55,360 --> 00:17:56,799
forward slash
474
00:17:56,799 --> 00:17:58,559
and now there's ten now this one's a
475
00:17:58,559 --> 00:18:00,640
little bit strange we will discuss
476
00:18:00,640 --> 00:18:03,200
modulus the modulus operator is a
477
00:18:03,200 --> 00:18:05,200
percent sign it gives you the remainder
478
00:18:05,200 --> 00:18:07,200
of any division we have a class of 20
479
00:18:07,200 --> 00:18:08,960
students and we need to divide this
480
00:18:08,960 --> 00:18:11,120
class into groups of three but 20
481
00:18:11,120 --> 00:18:13,200
doesn't divide by three evenly i can
482
00:18:13,200 --> 00:18:15,200
store the remainder within a separate
483
00:18:15,200 --> 00:18:16,400
variable
484
00:18:16,400 --> 00:18:19,440
i'll create a new variable called extra
485
00:18:19,440 --> 00:18:21,360
students
486
00:18:21,360 --> 00:18:23,840
and we will take students
487
00:18:23,840 --> 00:18:25,840
modulus 3
488
00:18:25,840 --> 00:18:28,000
and let's display the extra students
489
00:18:28,000 --> 00:18:30,320
that we have so we have two extra
490
00:18:30,320 --> 00:18:33,760
students but if this was 21 well 21
491
00:18:33,760 --> 00:18:36,160
divides by three evenly so the remainder
492
00:18:36,160 --> 00:18:38,720
is zero we have zero extra students so
493
00:18:38,720 --> 00:18:41,520
that is the modulus operator one popular
494
00:18:41,520 --> 00:18:43,360
use of the modulus operator is that you
495
00:18:43,360 --> 00:18:46,000
can find if a number is even or odd by
496
00:18:46,000 --> 00:18:49,520
taking a value or variable modulus 2. if
497
00:18:49,520 --> 00:18:52,240
extra students is 1 that means we have
498
00:18:52,240 --> 00:18:54,960
an odd number if it's 0 that means we
499
00:18:54,960 --> 00:18:57,039
have an even number so that is the
500
00:18:57,039 --> 00:18:59,200
modulus operator it can be somewhat
501
00:18:59,200 --> 00:19:01,039
tedious to write these long expressions
502
00:19:01,039 --> 00:19:03,200
out so there is a shortcut instead of
503
00:19:03,200 --> 00:19:05,280
saying students equal students plus one
504
00:19:05,280 --> 00:19:06,799
you could just write
505
00:19:06,799 --> 00:19:09,760
students plus equals one this is also
506
00:19:09,760 --> 00:19:11,840
known as an augmented assignment
507
00:19:11,840 --> 00:19:13,919
operator it's kind of like a shortcut so
508
00:19:13,919 --> 00:19:16,799
students is now 21. then we have
509
00:19:16,799 --> 00:19:19,919
students minus equals one students is
510
00:19:19,919 --> 00:19:21,039
19.
511
00:19:21,039 --> 00:19:22,240
students
512
00:19:22,240 --> 00:19:24,720
times equals 2.
513
00:19:24,720 --> 00:19:26,960
there are 40 students now
514
00:19:26,960 --> 00:19:28,080
students
515
00:19:28,080 --> 00:19:31,039
divide equals 2. and there are 10
516
00:19:31,039 --> 00:19:33,039
students with augmented assignment
517
00:19:33,039 --> 00:19:34,880
operators you can use these if you're
518
00:19:34,880 --> 00:19:37,120
reassigning the same variable like i
519
00:19:37,120 --> 00:19:39,280
couldn't say let
520
00:19:39,280 --> 00:19:41,600
extra students
521
00:19:41,600 --> 00:19:44,160
modulus equals three right
522
00:19:44,160 --> 00:19:46,320
and then if we were to display this we
523
00:19:46,320 --> 00:19:48,559
would have a syntax error so augmented
524
00:19:48,559 --> 00:19:50,320
assignment operators are kind of like a
525
00:19:50,320 --> 00:19:52,000
shortcut you can do if you're
526
00:19:52,000 --> 00:19:54,080
reassigning the same variable another
527
00:19:54,080 --> 00:19:55,600
thing to consider with arithmetic
528
00:19:55,600 --> 00:19:57,760
expressions is that there's operator
529
00:19:57,760 --> 00:20:00,480
precedence maybe we have this equation
530
00:20:00,480 --> 00:20:05,039
let result equals 1 plus 2
531
00:20:05,039 --> 00:20:06,799
times
532
00:20:06,799 --> 00:20:09,520
three plus four
533
00:20:09,520 --> 00:20:11,280
and then i'm just going to display
534
00:20:11,280 --> 00:20:14,000
whatever the result is
535
00:20:14,000 --> 00:20:16,640
so the result is 15. the order in which
536
00:20:16,640 --> 00:20:18,480
you solve this is that you begin at the
537
00:20:18,480 --> 00:20:20,320
left and work your way towards the right
538
00:20:20,320 --> 00:20:22,159
you solve anything within parentheses
539
00:20:22,159 --> 00:20:25,120
first then exponents then multiplication
540
00:20:25,120 --> 00:20:27,200
and division then lastly addition and
541
00:20:27,200 --> 00:20:29,200
subtraction starting on the left and
542
00:20:29,200 --> 00:20:31,039
working our way towards the right we
543
00:20:31,039 --> 00:20:32,880
would solve anything with parentheses
544
00:20:32,880 --> 00:20:36,080
first three plus four is seven then we
545
00:20:36,080 --> 00:20:38,720
solve any exponents there aren't any
546
00:20:38,720 --> 00:20:40,640
multiplication and division we can
547
00:20:40,640 --> 00:20:43,120
either resolve one plus two or two times
548
00:20:43,120 --> 00:20:45,520
seven so multiplication has precedence
549
00:20:45,520 --> 00:20:47,919
over addition we would solve this first
550
00:20:47,919 --> 00:20:50,799
then it's just 1 plus 14 which is 15.
551
00:20:50,799 --> 00:20:52,960
now by adding a set of parentheses you
552
00:20:52,960 --> 00:20:55,520
can force operator precedence this time
553
00:20:55,520 --> 00:20:58,000
i would like to resolve 1 plus 2 first
554
00:20:58,000 --> 00:21:00,159
and now we have a new result the result
555
00:21:00,159 --> 00:21:02,559
is 21. so yeah that's a super quick
556
00:21:02,559 --> 00:21:04,880
video on arithmetic expressions it's a
557
00:21:04,880 --> 00:21:06,480
little dull but it's kind of necessary
558
00:21:06,480 --> 00:21:08,000
to cover hey if this video helped you
559
00:21:08,000 --> 00:21:09,919
out help me out by smashing that like
560
00:21:09,919 --> 00:21:11,679
button leave a random comment down below
561
00:21:11,679 --> 00:21:13,200
and subscribe if you'd like to become a
562
00:21:13,200 --> 00:21:16,080
fellow bro
563
00:21:16,080 --> 00:21:17,840
hey everyone in this video i'm going to
564
00:21:17,840 --> 00:21:19,679
show you how we can accept some user
565
00:21:19,679 --> 00:21:22,000
input in javascript i'll demonstrate two
566
00:21:22,000 --> 00:21:24,799
ways the easy way with a simple window
567
00:21:24,799 --> 00:21:27,039
prompt and the difficult way with an
568
00:21:27,039 --> 00:21:30,080
html text box let's begin with the easy
569
00:21:30,080 --> 00:21:32,400
way i'm going to create a variable named
570
00:21:32,400 --> 00:21:34,559
username and the user will type in a
571
00:21:34,559 --> 00:21:37,679
name for themselves let username
572
00:21:37,679 --> 00:21:40,400
equals and to create a window prompt we
573
00:21:40,400 --> 00:21:42,000
type window
574
00:21:42,000 --> 00:21:42,960
dot
575
00:21:42,960 --> 00:21:45,840
prompt parentheses and within the
576
00:21:45,840 --> 00:21:48,640
parentheses we can add a message
577
00:21:48,640 --> 00:21:52,080
what's your name after a user types in
578
00:21:52,080 --> 00:21:54,240
some user input we will assign whatever
579
00:21:54,240 --> 00:21:56,880
they type into a variable so i'm going
580
00:21:56,880 --> 00:21:59,080
to display this
581
00:21:59,080 --> 00:22:03,280
console.log user name what's your name
582
00:22:03,280 --> 00:22:06,320
type in your name press ok and this will
583
00:22:06,320 --> 00:22:08,480
display the value that you typed in the
584
00:22:08,480 --> 00:22:10,080
difficult way although it's more
585
00:22:10,080 --> 00:22:12,960
practical is to use some html elements
586
00:22:12,960 --> 00:22:15,360
within our html file let's create a
587
00:22:15,360 --> 00:22:18,480
prompt a label
588
00:22:18,720 --> 00:22:21,919
enter your name and then maybe i'll add
589
00:22:21,919 --> 00:22:23,760
a line break
590
00:22:23,760 --> 00:22:26,559
and an input box
591
00:22:26,559 --> 00:22:27,840
input
592
00:22:27,840 --> 00:22:30,720
this is a self-closing tag
593
00:22:30,720 --> 00:22:33,600
a line break with my input tag let's set
594
00:22:33,600 --> 00:22:34,799
the type
595
00:22:34,799 --> 00:22:38,320
equal to text it's a text box and i'll
596
00:22:38,320 --> 00:22:40,720
give this an id of
597
00:22:40,720 --> 00:22:41,520
my
598
00:22:41,520 --> 00:22:44,799
text okay so save we have our label and
599
00:22:44,799 --> 00:22:48,480
a text box let's create a submit button
600
00:22:48,480 --> 00:22:51,120
we'll need a pair of button tags
601
00:22:51,120 --> 00:22:51,919
type
602
00:22:51,919 --> 00:22:57,120
will be button and the id will be my
603
00:22:57,120 --> 00:22:58,480
button
604
00:22:58,480 --> 00:23:00,000
you can add some text to the button
605
00:23:00,000 --> 00:23:01,440
between the button tags just write
606
00:23:01,440 --> 00:23:04,320
something submit okay make sure to save
607
00:23:04,320 --> 00:23:06,559
your html document then we'll head back
608
00:23:06,559 --> 00:23:08,880
to our javascript file what i'm about to
609
00:23:08,880 --> 00:23:10,640
show you is a little advanced but you
610
00:23:10,640 --> 00:23:12,159
can always just copy this and save it
611
00:23:12,159 --> 00:23:14,559
for the future after clicking our submit
612
00:23:14,559 --> 00:23:16,559
button whatever text is within our text
613
00:23:16,559 --> 00:23:18,960
box we'll assign it to a variable we'll
614
00:23:18,960 --> 00:23:20,799
type document
615
00:23:20,799 --> 00:23:21,600
dot
616
00:23:21,600 --> 00:23:22,559
get
617
00:23:22,559 --> 00:23:23,600
element
618
00:23:23,600 --> 00:23:26,559
by id
619
00:23:26,559 --> 00:23:28,400
then type my
620
00:23:28,400 --> 00:23:31,200
button follow this with dot
621
00:23:31,200 --> 00:23:32,080
on
622
00:23:32,080 --> 00:23:33,120
click
623
00:23:33,120 --> 00:23:36,480
and we will set this equal to function
624
00:23:36,480 --> 00:23:40,000
parenthesis then a set of curly braces
625
00:23:40,000 --> 00:23:42,000
at the top of my program i'm going to
626
00:23:42,000 --> 00:23:44,080
declare my variable but not yet assign
627
00:23:44,080 --> 00:23:46,559
it and within my function the set of
628
00:23:46,559 --> 00:23:50,640
parentheses i will take username and set
629
00:23:50,640 --> 00:23:52,880
this equal to
630
00:23:52,880 --> 00:23:54,240
document
631
00:23:54,240 --> 00:23:56,000
dot get
632
00:23:56,000 --> 00:23:57,200
element
633
00:23:57,200 --> 00:23:58,080
by
634
00:23:58,080 --> 00:23:59,120
id
635
00:23:59,120 --> 00:24:02,080
parentheses then within quotes
636
00:24:02,080 --> 00:24:06,000
my text that's the id of the text box
637
00:24:06,000 --> 00:24:07,600
dot value
638
00:24:07,600 --> 00:24:09,679
so when we click on this button take
639
00:24:09,679 --> 00:24:12,400
whatever text is within our text box and
640
00:24:12,400 --> 00:24:14,960
assign it to user name then afterwards
641
00:24:14,960 --> 00:24:17,039
let's display whatever our username is
642
00:24:17,039 --> 00:24:18,480
within our console
643
00:24:18,480 --> 00:24:20,559
or if you want we can change maybe this
644
00:24:20,559 --> 00:24:24,279
label console.log
645
00:24:24,480 --> 00:24:27,679
user name type in your first and last
646
00:24:27,679 --> 00:24:28,799
name
647
00:24:28,799 --> 00:24:30,159
press submit
648
00:24:30,159 --> 00:24:32,080
and this will display your name let's
649
00:24:32,080 --> 00:24:34,159
change this label but i'm going to give
650
00:24:34,159 --> 00:24:36,559
this label a unique id
651
00:24:36,559 --> 00:24:40,960
id equals my label so save go back to
652
00:24:40,960 --> 00:24:43,200
your javascript file i'm going to edit
653
00:24:43,200 --> 00:24:46,320
the inner html of this label document
654
00:24:46,320 --> 00:24:48,159
dot get
655
00:24:48,159 --> 00:24:51,600
element by id
656
00:24:51,600 --> 00:24:52,480
my
657
00:24:52,480 --> 00:24:53,760
label
658
00:24:53,760 --> 00:24:57,039
dot inner html
659
00:24:57,039 --> 00:25:00,400
set the sequel to user name okay so
660
00:25:00,400 --> 00:25:02,880
again type in your first and last name
661
00:25:02,880 --> 00:25:05,279
press submit and this should change that
662
00:25:05,279 --> 00:25:07,440
label maybe i'll add
663
00:25:07,440 --> 00:25:08,960
hello
664
00:25:08,960 --> 00:25:11,760
plus username this time
665
00:25:11,760 --> 00:25:14,799
hello bro code so yeah those are two
666
00:25:14,799 --> 00:25:17,120
different ways to accept user input you
667
00:25:17,120 --> 00:25:19,600
can go the easy way with a simple window
668
00:25:19,600 --> 00:25:21,840
prompt or you can take the difficult
669
00:25:21,840 --> 00:25:24,320
approach with an html text box although
670
00:25:24,320 --> 00:25:26,400
it's more practical but it's above our
671
00:25:26,400 --> 00:25:28,240
current level of understanding at this
672
00:25:28,240 --> 00:25:30,720
point in time so yeah those are two ways
673
00:25:30,720 --> 00:25:33,440
to accept user input in javascript if
674
00:25:33,440 --> 00:25:35,120
this video helped you out you can help
675
00:25:35,120 --> 00:25:37,039
me out by smashing that like button
676
00:25:37,039 --> 00:25:38,799
leave random comments down below and
677
00:25:38,799 --> 00:25:40,159
subscribe if you'd like to become a
678
00:25:40,159 --> 00:25:42,880
fellow bro
679
00:25:42,880 --> 00:25:44,480
hey guys in this video i'm going to
680
00:25:44,480 --> 00:25:46,960
explain type conversion type conversion
681
00:25:46,960 --> 00:25:49,360
is the ability to change the data type
682
00:25:49,360 --> 00:25:51,600
of a value to another and i'll explain
683
00:25:51,600 --> 00:25:53,679
how to do this with strings numbers and
684
00:25:53,679 --> 00:25:56,000
booleans here's one issue when accepting
685
00:25:56,000 --> 00:25:57,919
some user input let's say we have
686
00:25:57,919 --> 00:26:00,080
variable age and i'll create a window
687
00:26:00,080 --> 00:26:02,320
prompt to ask a user to enter in their
688
00:26:02,320 --> 00:26:05,760
age how old are you once we accept some
689
00:26:05,760 --> 00:26:08,400
user input i'm going to increment age by
690
00:26:08,400 --> 00:26:10,400
one because let's say it's the user's
691
00:26:10,400 --> 00:26:12,400
birthday then let's display the user's
692
00:26:12,400 --> 00:26:13,279
age
693
00:26:13,279 --> 00:26:16,159
console.log
694
00:26:16,159 --> 00:26:19,200
happy birthday
695
00:26:19,200 --> 00:26:20,080
u
696
00:26:20,080 --> 00:26:21,840
r
697
00:26:21,840 --> 00:26:24,240
our age variable
698
00:26:24,240 --> 00:26:25,520
years
699
00:26:25,520 --> 00:26:28,080
old how old are you let's say that i'm
700
00:26:28,080 --> 00:26:32,559
21 i press ok happy birthday you are 211
701
00:26:32,559 --> 00:26:35,120
years old when we accept user input it's
702
00:26:35,120 --> 00:26:37,600
of a string data type we can't normally
703
00:26:37,600 --> 00:26:39,600
use strings for any sort of arithmetic
704
00:26:39,600 --> 00:26:42,400
expressions what i intended was to add
705
00:26:42,400 --> 00:26:45,279
our edge 21 plus 1. that would give me
706
00:26:45,279 --> 00:26:47,760
22. if you add a number to a string you
707
00:26:47,760 --> 00:26:49,840
just concatenate that number to the end
708
00:26:49,840 --> 00:26:52,000
of the string because a string is just a
709
00:26:52,000 --> 00:26:54,000
series of characters if i need to take
710
00:26:54,000 --> 00:26:55,840
some user input and use it with some
711
00:26:55,840 --> 00:26:57,840
sort of arithmetic expression i'll want
712
00:26:57,840 --> 00:27:00,400
to convert that string to a number and
713
00:27:00,400 --> 00:27:02,640
one way i can do that is to use the
714
00:27:02,640 --> 00:27:05,039
number constructor so i'm going to set
715
00:27:05,039 --> 00:27:05,840
age
716
00:27:05,840 --> 00:27:08,320
equal to then type number
717
00:27:08,320 --> 00:27:10,080
make sure it's capital
718
00:27:10,080 --> 00:27:13,200
parentheses and then pass in age this
719
00:27:13,200 --> 00:27:15,600
will convert a string to a number how
720
00:27:15,600 --> 00:27:18,480
old are you 21 press ok happy birthday
721
00:27:18,480 --> 00:27:20,799
you are now 22 years old if you ever
722
00:27:20,799 --> 00:27:23,120
need to get the data type of a variable
723
00:27:23,120 --> 00:27:25,600
there is a type of keyword
724
00:27:25,600 --> 00:27:27,679
let's display it type
725
00:27:27,679 --> 00:27:28,640
of
726
00:27:28,640 --> 00:27:31,279
age so before we convert our age
727
00:27:31,279 --> 00:27:33,120
variable it's a string then let's
728
00:27:33,120 --> 00:27:35,279
display the data type of age after we
729
00:27:35,279 --> 00:27:36,799
convert it
730
00:27:36,799 --> 00:27:39,120
at first it's a string we convert it and
731
00:27:39,120 --> 00:27:40,960
now it's a number and then we can use it
732
00:27:40,960 --> 00:27:43,120
in arithmetic expressions here's a few
733
00:27:43,120 --> 00:27:46,000
other examples we have three variables x
734
00:27:46,000 --> 00:27:48,240
y and z
735
00:27:48,240 --> 00:27:51,279
i'm going to set x to equal and we will
736
00:27:51,279 --> 00:27:53,520
use the number constructor and we will
737
00:27:53,520 --> 00:27:58,799
convert the string 3.14 into a number
738
00:27:58,799 --> 00:28:00,559
and then i will display
739
00:28:00,559 --> 00:28:04,320
whatever x is as well as the data type
740
00:28:04,320 --> 00:28:07,760
type of x remember we're converting a
741
00:28:07,760 --> 00:28:10,640
string into a number
742
00:28:10,640 --> 00:28:14,080
so 3.14 is now a number let's convert a
743
00:28:14,080 --> 00:28:17,279
number into a string y equals and we can
744
00:28:17,279 --> 00:28:19,600
use the string constructor
745
00:28:19,600 --> 00:28:23,039
and we will convert the number 3.14 into
746
00:28:23,039 --> 00:28:24,960
a string
747
00:28:24,960 --> 00:28:27,200
console.log
748
00:28:27,200 --> 00:28:28,080
y
749
00:28:28,080 --> 00:28:32,640
and type of y 3.14 is now a string
750
00:28:32,640 --> 00:28:34,880
and to convert something to a boolean
751
00:28:34,880 --> 00:28:37,600
you can use the boolean constructor
752
00:28:37,600 --> 00:28:39,679
so if you pass in an empty string just a
753
00:28:39,679 --> 00:28:43,679
set of quotes this will give you false
754
00:28:43,679 --> 00:28:45,440
console.log
755
00:28:45,440 --> 00:28:48,159
variable z and the type of z
756
00:28:48,159 --> 00:28:50,320
so converting an empty string to a
757
00:28:50,320 --> 00:28:53,120
boolean will result in false and this is
758
00:28:53,120 --> 00:28:55,520
of the boolean data type but if you type
759
00:28:55,520 --> 00:28:58,240
in anything else it will give you true
760
00:28:58,240 --> 00:28:59,279
so maybe
761
00:28:59,279 --> 00:29:01,440
i try and convert the word pizza when
762
00:29:01,440 --> 00:29:03,520
you convert a string to a boolean that's
763
00:29:03,520 --> 00:29:06,000
not empty this will give you true if
764
00:29:06,000 --> 00:29:07,600
it's an empty string just a set of
765
00:29:07,600 --> 00:29:10,159
quotes it results in false this would be
766
00:29:10,159 --> 00:29:12,080
useful if you need to accept some user
767
00:29:12,080 --> 00:29:14,399
input or user types in their name if
768
00:29:14,399 --> 00:29:16,320
they skip that step and using an if
769
00:29:16,320 --> 00:29:18,080
statement which we'll talk about later
770
00:29:18,080 --> 00:29:20,159
you can check to see if a user typed in
771
00:29:20,159 --> 00:29:21,840
something or not based on if that
772
00:29:21,840 --> 00:29:24,240
boolean is true or false but we'll get
773
00:29:24,240 --> 00:29:26,320
into that later so yeah those are a few
774
00:29:26,320 --> 00:29:28,399
basic ways of type conversion you can
775
00:29:28,399 --> 00:29:30,559
change the data type of one value to
776
00:29:30,559 --> 00:29:32,559
another if you need to get the data type
777
00:29:32,559 --> 00:29:35,039
of a variable just precede that variable
778
00:29:35,039 --> 00:29:37,760
with the type of keyword and to convert
779
00:29:37,760 --> 00:29:39,919
a value or variable into another data
780
00:29:39,919 --> 00:29:42,240
type you can surround that value or
781
00:29:42,240 --> 00:29:44,720
variable with a given constructor the
782
00:29:44,720 --> 00:29:46,480
number constructor the string
783
00:29:46,480 --> 00:29:48,880
constructor or the boolean constructor
784
00:29:48,880 --> 00:29:50,799
oh one fun fact too if you try and
785
00:29:50,799 --> 00:29:52,720
convert something into a number that
786
00:29:52,720 --> 00:29:54,480
normally shouldn't be a number
787
00:29:54,480 --> 00:29:56,640
like the word pizza well then this will
788
00:29:56,640 --> 00:30:00,320
result in n-a-n not a number that's just
789
00:30:00,320 --> 00:30:02,720
a fun fact so yeah that is type
790
00:30:02,720 --> 00:30:05,039
conversion in javascript if this video
791
00:30:05,039 --> 00:30:06,880
helped you out you can help me out by
792
00:30:06,880 --> 00:30:08,399
smashing that like button leave a random
793
00:30:08,399 --> 00:30:10,159
comment down below and subscribe if
794
00:30:10,159 --> 00:30:13,200
you'd like to become a fellow bro
795
00:30:13,200 --> 00:30:15,039
hey everybody in this video i'm going to
796
00:30:15,039 --> 00:30:17,520
explain constants a constant is a
797
00:30:17,520 --> 00:30:19,760
variable that can't be changed it adds
798
00:30:19,760 --> 00:30:21,760
some security to our code to declare a
799
00:30:21,760 --> 00:30:24,240
constant you use the const keyword in
800
00:30:24,240 --> 00:30:26,640
place of var or let in this example
801
00:30:26,640 --> 00:30:28,559
we're going to create a sample program
802
00:30:28,559 --> 00:30:30,399
to calculate the circumference of a
803
00:30:30,399 --> 00:30:32,640
circle which has this formula
804
00:30:32,640 --> 00:30:35,360
circumference equals 2 times pi times
805
00:30:35,360 --> 00:30:36,960
radius let's declare all of the
806
00:30:36,960 --> 00:30:38,399
variables that we'll need we're going to
807
00:30:38,399 --> 00:30:40,559
write this program without constants and
808
00:30:40,559 --> 00:30:42,640
then later with constants and i can show
809
00:30:42,640 --> 00:30:44,480
you their usefulness we'll create three
810
00:30:44,480 --> 00:30:46,880
variables let pi
811
00:30:46,880 --> 00:30:48,360
equal
812
00:30:48,360 --> 00:30:52,000
3.14159 just the first few digits of pi
813
00:30:52,000 --> 00:30:55,960
let radius and let
814
00:30:55,960 --> 00:30:58,399
circumference we're going to accept some
815
00:30:58,399 --> 00:31:01,120
user input we will assign radius equal
816
00:31:01,120 --> 00:31:01,919
to
817
00:31:01,919 --> 00:31:03,360
window
818
00:31:03,360 --> 00:31:05,840
dot prompt
819
00:31:06,320 --> 00:31:09,039
enter the radius
820
00:31:09,039 --> 00:31:10,960
of a circle
821
00:31:10,960 --> 00:31:12,960
then when we accept user input it's a
822
00:31:12,960 --> 00:31:14,559
string we'll need to convert that to a
823
00:31:14,559 --> 00:31:17,120
number radius equals
824
00:31:17,120 --> 00:31:19,120
and use some type conversion and convert
825
00:31:19,120 --> 00:31:21,519
our radius into a number to calculate
826
00:31:21,519 --> 00:31:23,600
the circumference of a circle we will
827
00:31:23,600 --> 00:31:26,559
set circumference equal to and follow
828
00:31:26,559 --> 00:31:30,000
that formula 2 times pi
829
00:31:30,000 --> 00:31:32,799
times radius and with the radius the
830
00:31:32,799 --> 00:31:34,720
user will type that in
831
00:31:34,720 --> 00:31:36,240
at the end of our program we will
832
00:31:36,240 --> 00:31:38,880
display our result
833
00:31:38,880 --> 00:31:41,120
the circumference
834
00:31:41,120 --> 00:31:42,960
is
835
00:31:42,960 --> 00:31:44,240
circumference
836
00:31:44,240 --> 00:31:45,600
let's run this
837
00:31:45,600 --> 00:31:48,240
enter the radius of a circle if the
838
00:31:48,240 --> 00:31:51,440
radius is 9 i press ok the circumference
839
00:31:51,440 --> 00:31:54,640
is 56.54 now here's why constants are
840
00:31:54,640 --> 00:31:57,279
useful let's say that somebody changes
841
00:31:57,279 --> 00:31:58,799
our code
842
00:31:58,799 --> 00:32:00,480
pi now equals
843
00:32:00,480 --> 00:32:03,760
420 69. now when we run this program and
844
00:32:03,760 --> 00:32:06,240
we type in 9 for the radius
845
00:32:06,240 --> 00:32:08,840
while the circumference is now
846
00:32:08,840 --> 00:32:11,840
7572 we can prevent a variable from
847
00:32:11,840 --> 00:32:14,320
being changed if we assign it as a
848
00:32:14,320 --> 00:32:17,600
constant in place of let use the const
849
00:32:17,600 --> 00:32:20,240
keyword and our variable pi is now a
850
00:32:20,240 --> 00:32:22,720
constant and a common naming convention
851
00:32:22,720 --> 00:32:24,720
for constants is that you make all of
852
00:32:24,720 --> 00:32:27,279
the letters uppercase it's not necessary
853
00:32:27,279 --> 00:32:29,360
but it's good practice then we are going
854
00:32:29,360 --> 00:32:31,600
to change that here too
855
00:32:31,600 --> 00:32:34,000
enter the radius of a circle i'll type 9
856
00:32:34,000 --> 00:32:35,600
the same as before
857
00:32:35,600 --> 00:32:37,840
and we encountered a type error
858
00:32:37,840 --> 00:32:40,559
assignment to constant variable so once
859
00:32:40,559 --> 00:32:42,720
we declare a constant we can't change it
860
00:32:42,720 --> 00:32:44,720
so if i were to get rid of this line run
861
00:32:44,720 --> 00:32:48,880
this again 9 the circumference is 56.54
862
00:32:48,880 --> 00:32:50,799
you'll want to use constants as often as
863
00:32:50,799 --> 00:32:53,039
possible if you have a variable that you
864
00:32:53,039 --> 00:32:55,200
know will not be changed later we're not
865
00:32:55,200 --> 00:32:57,360
going to assign radius and circumference
866
00:32:57,360 --> 00:32:58,960
as constants because we'll need to
867
00:32:58,960 --> 00:33:01,039
update them later in our program so yeah
868
00:33:01,039 --> 00:33:02,799
those are constants it's basically just
869
00:33:02,799 --> 00:33:05,039
a variable that can't be changed it adds
870
00:33:05,039 --> 00:33:06,640
a little bit of data security if you
871
00:33:06,640 --> 00:33:08,320
found this video helpful you can help me
872
00:33:08,320 --> 00:33:10,000
out by smashing that like button leave a
873
00:33:10,000 --> 00:33:11,919
random comment down below and subscribe
874
00:33:11,919 --> 00:33:15,279
if you'd like to become a fellow bro
875
00:33:15,279 --> 00:33:16,960
all right everybody let's talk about
876
00:33:16,960 --> 00:33:19,600
javascript math math is an intrinsic
877
00:33:19,600 --> 00:33:22,320
object that provides basic mathematics
878
00:33:22,320 --> 00:33:24,799
functionality and constants there's a
879
00:33:24,799 --> 00:33:26,399
lot of functions i think that you would
880
00:33:26,399 --> 00:33:28,960
find useful in this example we have a
881
00:33:28,960 --> 00:33:33,279
variable x and x equals 3.14 feel free
882
00:33:33,279 --> 00:33:35,120
to choose any number that you want maybe
883
00:33:35,120 --> 00:33:36,880
i need to round this variable well
884
00:33:36,880 --> 00:33:38,960
there's a built-in function of math that
885
00:33:38,960 --> 00:33:42,320
can do that for us i could just type x
886
00:33:42,320 --> 00:33:44,960
equals then type math
887
00:33:44,960 --> 00:33:45,919
dot
888
00:33:45,919 --> 00:33:47,519
and then i have access to a bunch of
889
00:33:47,519 --> 00:33:49,600
different functions i would like to
890
00:33:49,600 --> 00:33:52,640
round x so add a set of parentheses and
891
00:33:52,640 --> 00:33:55,600
pass in a value or a variable that you
892
00:33:55,600 --> 00:33:57,840
would like to round and then if i was to
893
00:33:57,840 --> 00:34:01,760
display the value of x with console.log
894
00:34:01,760 --> 00:34:04,240
this would display my value or variable
895
00:34:04,240 --> 00:34:06,159
rounded whatever's within the set of
896
00:34:06,159 --> 00:34:08,399
parentheses of this function and x
897
00:34:08,399 --> 00:34:12,719
rounded is 3. let's cover a few more
898
00:34:12,719 --> 00:34:15,280
the floor function will always round a
899
00:34:15,280 --> 00:34:18,879
number down if this variable was 3.99
900
00:34:18,879 --> 00:34:20,960
the floor function will always round
901
00:34:20,960 --> 00:34:23,599
down x is still three if you need to
902
00:34:23,599 --> 00:34:26,399
round up there is the ceiling function
903
00:34:26,399 --> 00:34:30,000
which is shortened to c e i l
904
00:34:30,000 --> 00:34:32,320
if x is 3.14
905
00:34:32,320 --> 00:34:34,719
seal will round up
906
00:34:34,719 --> 00:34:37,679
x is now four you can raise a value to a
907
00:34:37,679 --> 00:34:39,040
given power
908
00:34:39,040 --> 00:34:42,719
x equals math dot pow in the parentheses
909
00:34:42,719 --> 00:34:45,040
you'll give a base and an exponent each
910
00:34:45,040 --> 00:34:47,040
is separated with a comma
911
00:34:47,040 --> 00:34:50,560
x to the power of 2 is 9.8
912
00:34:50,560 --> 00:34:52,399
x to the power of 3
913
00:34:52,399 --> 00:34:56,639
is 30.9 their square root x equals math
914
00:34:56,639 --> 00:34:58,480
dot square root
915
00:34:58,480 --> 00:35:00,720
sqrt for short
916
00:35:00,720 --> 00:35:02,960
the square root of x
917
00:35:02,960 --> 00:35:04,880
is 1.77
918
00:35:04,880 --> 00:35:07,040
to find an absolute value you can use
919
00:35:07,040 --> 00:35:10,200
the absolute value function x equals
920
00:35:10,200 --> 00:35:13,200
math.abs the absolute value is the
921
00:35:13,200 --> 00:35:15,839
distance away from zero if x was
922
00:35:15,839 --> 00:35:17,760
negative three point one four the
923
00:35:17,760 --> 00:35:19,839
distance away from zero would result in
924
00:35:19,839 --> 00:35:22,240
a positive number positive three point
925
00:35:22,240 --> 00:35:24,000
one four this time we have a couple
926
00:35:24,000 --> 00:35:25,440
variables and we need to find the
927
00:35:25,440 --> 00:35:27,520
maximum and the minimum we'll create two
928
00:35:27,520 --> 00:35:30,560
more variables let y equal
929
00:35:30,560 --> 00:35:33,280
i don't know what about five
930
00:35:33,280 --> 00:35:34,640
let z
931
00:35:34,640 --> 00:35:37,920
equal nine i'll declare a maximum and a
932
00:35:37,920 --> 00:35:39,040
minimum
933
00:35:39,040 --> 00:35:40,640
let maximum
934
00:35:40,640 --> 00:35:42,160
lat minimum
935
00:35:42,160 --> 00:35:46,040
i will assign maximum equal to
936
00:35:46,040 --> 00:35:48,000
math.max function
937
00:35:48,000 --> 00:35:51,520
and pass in x y and z
938
00:35:51,520 --> 00:35:53,839
this will return the maximum and assign
939
00:35:53,839 --> 00:35:54,560
it
940
00:35:54,560 --> 00:35:57,599
so the maximum between x y and z
941
00:35:57,599 --> 00:36:01,599
is nine then there's also minimum
942
00:36:01,599 --> 00:36:06,200
so minimum equals math.min
943
00:36:06,400 --> 00:36:11,200
the minimum between x y and z is x 3.14
944
00:36:11,200 --> 00:36:13,680
there's also some built-in constants too
945
00:36:13,680 --> 00:36:16,960
with x i assigned 3.14 i would like to
946
00:36:16,960 --> 00:36:20,000
assign pi with more digits well there is
947
00:36:20,000 --> 00:36:22,320
a built-in constant of math
948
00:36:22,320 --> 00:36:27,200
x equals math.pi and when i display x x
949
00:36:27,200 --> 00:36:29,359
is three point one four one five nine
950
00:36:29,359 --> 00:36:31,040
two six five three five eight nine seven
951
00:36:31,040 --> 00:36:33,680
nine three so math is an intrinsic
952
00:36:33,680 --> 00:36:36,240
object that provides basic mathematics
953
00:36:36,240 --> 00:36:38,640
functionality and constants there's a
954
00:36:38,640 --> 00:36:40,320
lot more functions and constants than
955
00:36:40,320 --> 00:36:42,079
what we covered but here's a few of the
956
00:36:42,079 --> 00:36:44,800
basics so yeah that is javascript math
957
00:36:44,800 --> 00:36:46,400
if you found this video helpful you can
958
00:36:46,400 --> 00:36:48,240
help me out by smashing that like button
959
00:36:48,240 --> 00:36:49,760
leave a random comment down below and
960
00:36:49,760 --> 00:36:51,040
subscribe if you'd like to become a
961
00:36:51,040 --> 00:36:53,520
fellow bro
962
00:36:53,520 --> 00:36:55,920
hey welcome back everybody in this video
963
00:36:55,920 --> 00:36:57,599
we're going to create a practice program
964
00:36:57,599 --> 00:37:00,320
to find the hypotenuse of a right angled
965
00:37:00,320 --> 00:37:02,400
triangle and the formula to solve that
966
00:37:02,400 --> 00:37:06,000
is c equals the square root of a squared
967
00:37:06,000 --> 00:37:07,760
plus b squared we'll create two
968
00:37:07,760 --> 00:37:09,920
variations of this program first we'll
969
00:37:09,920 --> 00:37:12,240
accept some user input via some prompts
970
00:37:12,240 --> 00:37:14,320
then later on we'll adjust this program
971
00:37:14,320 --> 00:37:16,240
and accept some user input through some
972
00:37:16,240 --> 00:37:19,040
html text boxes let's begin let's
973
00:37:19,040 --> 00:37:21,520
declare three variables each for side a
974
00:37:21,520 --> 00:37:22,880
b and c
975
00:37:22,880 --> 00:37:24,640
let a
976
00:37:24,640 --> 00:37:25,760
let b
977
00:37:25,760 --> 00:37:27,920
and let's see we'll accept some user
978
00:37:27,920 --> 00:37:30,560
input for sides a and b
979
00:37:30,560 --> 00:37:34,240
a equals window dot prompt
980
00:37:34,240 --> 00:37:38,240
and the prompt will be enter side a
981
00:37:38,240 --> 00:37:40,480
when we accept user input it's a string
982
00:37:40,480 --> 00:37:43,280
we'll need to convert that to a number a
983
00:37:43,280 --> 00:37:46,480
equals number pass in a
984
00:37:46,480 --> 00:37:48,960
let's do the same thing for side b
985
00:37:48,960 --> 00:37:50,880
let's copy and paste what we have but
986
00:37:50,880 --> 00:37:54,000
change a to b
987
00:37:54,160 --> 00:37:55,680
and the formula to calculate the
988
00:37:55,680 --> 00:37:57,520
hypotenuse goes a little something like
989
00:37:57,520 --> 00:37:58,400
this
990
00:37:58,400 --> 00:37:59,359
c
991
00:37:59,359 --> 00:38:02,480
equals we'll need to square sides a and
992
00:38:02,480 --> 00:38:04,960
b and add them together we can use
993
00:38:04,960 --> 00:38:07,920
javascript math dot
994
00:38:07,920 --> 00:38:09,359
power function
995
00:38:09,359 --> 00:38:12,079
a to the power of two
996
00:38:12,079 --> 00:38:13,520
plus
997
00:38:13,520 --> 00:38:15,599
copy this paste it
998
00:38:15,599 --> 00:38:18,160
b to the power of two then we need to
999
00:38:18,160 --> 00:38:20,720
square all of this and that equals c
1000
00:38:20,720 --> 00:38:23,440
so in the next step we could set c equal
1001
00:38:23,440 --> 00:38:24,560
to
1002
00:38:24,560 --> 00:38:28,000
math dot square root
1003
00:38:28,000 --> 00:38:29,839
and pass in c
1004
00:38:29,839 --> 00:38:31,119
if you would like to do this in less
1005
00:38:31,119 --> 00:38:34,240
steps you could copy all of this
1006
00:38:34,240 --> 00:38:35,599
cut this line
1007
00:38:35,599 --> 00:38:37,440
and paste what we just copied so this
1008
00:38:37,440 --> 00:38:39,599
would only take one line of code then at
1009
00:38:39,599 --> 00:38:42,320
the end we will display side c
1010
00:38:42,320 --> 00:38:45,119
side c
1011
00:38:45,119 --> 00:38:48,160
c okay let's run this
1012
00:38:48,160 --> 00:38:50,160
enter side a
1013
00:38:50,160 --> 00:38:52,079
a is three
1014
00:38:52,079 --> 00:38:55,920
b is four side c equals five okay now
1015
00:38:55,920 --> 00:38:57,520
let's make a more advanced version of
1016
00:38:57,520 --> 00:38:59,440
this program we'll accept some user
1017
00:38:59,440 --> 00:39:02,960
input via some html text boxes so let's
1018
00:39:02,960 --> 00:39:05,440
head to our html file and add a couple
1019
00:39:05,440 --> 00:39:09,200
elements we'll create three labels
1020
00:39:09,200 --> 00:39:12,160
label the first will have an id
1021
00:39:12,160 --> 00:39:14,400
equal to maybe a
1022
00:39:14,400 --> 00:39:17,280
label then close this tag and i'll add a
1023
00:39:17,280 --> 00:39:18,880
line break
1024
00:39:18,880 --> 00:39:21,119
then i'll copy this paste it
1025
00:39:21,119 --> 00:39:24,640
will have a b label and a c label i'll
1026
00:39:24,640 --> 00:39:29,839
change the text to side a colon
1027
00:39:29,920 --> 00:39:31,440
side b
1028
00:39:31,440 --> 00:39:33,520
and side c
1029
00:39:33,520 --> 00:39:35,359
okay and this is what we have so far
1030
00:39:35,359 --> 00:39:38,160
i'll add two text boxes right underneath
1031
00:39:38,160 --> 00:39:41,040
a and b and then a button later we'll
1032
00:39:41,040 --> 00:39:44,000
create a self-closing input tag
1033
00:39:44,000 --> 00:39:45,599
then i'll add a line break at the end
1034
00:39:45,599 --> 00:39:47,119
before i forget
1035
00:39:47,119 --> 00:39:48,640
set the type
1036
00:39:48,640 --> 00:39:50,320
equal to
1037
00:39:50,320 --> 00:39:52,800
text because it's a text box
1038
00:39:52,800 --> 00:39:56,400
and i will give this a unique id of a
1039
00:39:56,400 --> 00:39:59,119
text box
1040
00:39:59,119 --> 00:40:01,359
okay let's copy this
1041
00:40:01,359 --> 00:40:03,520
then underneath b label and give this
1042
00:40:03,520 --> 00:40:06,800
text box an id of b text box okay this
1043
00:40:06,800 --> 00:40:08,560
is what we have so far
1044
00:40:08,560 --> 00:40:10,560
then i'll create a button underneath
1045
00:40:10,560 --> 00:40:14,640
this second text box so right here
1046
00:40:14,720 --> 00:40:17,359
so we'll create a pair of button tags
1047
00:40:17,359 --> 00:40:20,640
and a line break i'll set the type equal
1048
00:40:20,640 --> 00:40:22,960
to button
1049
00:40:22,960 --> 00:40:27,040
and an id of what about submit
1050
00:40:27,040 --> 00:40:28,720
button then i should probably add some
1051
00:40:28,720 --> 00:40:29,680
text
1052
00:40:29,680 --> 00:40:31,760
so between the button tags
1053
00:40:31,760 --> 00:40:33,920
submit so let's save then heading back
1054
00:40:33,920 --> 00:40:36,079
to our javascript file this is what we
1055
00:40:36,079 --> 00:40:38,079
have to type this will be a little bit
1056
00:40:38,079 --> 00:40:39,599
advanced for us because we haven't
1057
00:40:39,599 --> 00:40:41,680
discussed functions yet when we click on
1058
00:40:41,680 --> 00:40:43,920
this button we need to do something to
1059
00:40:43,920 --> 00:40:46,800
select this button we will type document
1060
00:40:46,800 --> 00:40:47,599
dot
1061
00:40:47,599 --> 00:40:48,560
get
1062
00:40:48,560 --> 00:40:50,839
element by
1063
00:40:50,839 --> 00:40:54,240
id within the parentheses we will list a
1064
00:40:54,240 --> 00:40:56,560
unique id
1065
00:40:56,560 --> 00:41:00,240
i would like the id of my submit button
1066
00:41:00,240 --> 00:41:02,560
then follow this with dot
1067
00:41:02,560 --> 00:41:05,440
on click when we click on this button we
1068
00:41:05,440 --> 00:41:06,880
would like to do something we would like
1069
00:41:06,880 --> 00:41:09,599
to perform a function function
1070
00:41:09,599 --> 00:41:12,240
parentheses curly braces within the
1071
00:41:12,240 --> 00:41:14,800
curly braces we will execute some code
1072
00:41:14,800 --> 00:41:16,400
and actually we can copy a lot of what
1073
00:41:16,400 --> 00:41:18,880
we have here so let's copy our previous
1074
00:41:18,880 --> 00:41:21,280
code and paste it but we need to change
1075
00:41:21,280 --> 00:41:22,880
a couple things around we would not like
1076
00:41:22,880 --> 00:41:25,520
window prompts so let's change this line
1077
00:41:25,520 --> 00:41:26,880
to a
1078
00:41:26,880 --> 00:41:28,640
equals
1079
00:41:28,640 --> 00:41:30,560
document
1080
00:41:30,560 --> 00:41:32,000
dot get
1081
00:41:32,000 --> 00:41:35,359
element by id
1082
00:41:35,359 --> 00:41:37,680
and i would like to get the value of
1083
00:41:37,680 --> 00:41:41,280
this text box which has a unique id of a
1084
00:41:41,280 --> 00:41:44,000
text box
1085
00:41:44,079 --> 00:41:47,040
follow this with dot value we're taking
1086
00:41:47,040 --> 00:41:49,280
the value of this text box and assigning
1087
00:41:49,280 --> 00:41:51,599
it to variable a when we accept user
1088
00:41:51,599 --> 00:41:53,440
input it's normally a string we're going
1089
00:41:53,440 --> 00:41:55,599
to convert it to a number
1090
00:41:55,599 --> 00:41:58,160
let's do the same thing with b so copy
1091
00:41:58,160 --> 00:42:00,240
that line paste it
1092
00:42:00,240 --> 00:42:04,720
change a to b here and here as well
1093
00:42:04,720 --> 00:42:07,119
then convert it to a number
1094
00:42:07,119 --> 00:42:08,960
we can keep this line of code the logic
1095
00:42:08,960 --> 00:42:11,200
is still the same and lastly we can
1096
00:42:11,200 --> 00:42:13,119
update this label i'm going to change
1097
00:42:13,119 --> 00:42:15,040
this around real quick
1098
00:42:15,040 --> 00:42:16,480
i don't want to display anything but
1099
00:42:16,480 --> 00:42:18,319
we'll still keep the label i'm going to
1100
00:42:18,319 --> 00:42:21,520
change the inner html of this label c
1101
00:42:21,520 --> 00:42:23,920
label
1102
00:42:24,079 --> 00:42:26,720
document dot get
1103
00:42:26,720 --> 00:42:27,760
element
1104
00:42:27,760 --> 00:42:28,839
by
1105
00:42:28,839 --> 00:42:31,760
id we are selecting
1106
00:42:31,760 --> 00:42:34,160
c label and i'm going to change the
1107
00:42:34,160 --> 00:42:36,319
inner html
1108
00:42:36,319 --> 00:42:38,640
and set this equal to
1109
00:42:38,640 --> 00:42:40,400
side c
1110
00:42:40,400 --> 00:42:41,920
colon space
1111
00:42:41,920 --> 00:42:43,280
plus c
1112
00:42:43,280 --> 00:42:45,280
okay so make sure you save both your
1113
00:42:45,280 --> 00:42:47,760
html document and your javascript file
1114
00:42:47,760 --> 00:42:49,760
and let's run this
1115
00:42:49,760 --> 00:42:52,720
so side a is three side b is four let's
1116
00:42:52,720 --> 00:42:56,079
press submit and side c is five so yeah
1117
00:42:56,079 --> 00:42:58,319
everybody that is a practice program to
1118
00:42:58,319 --> 00:43:00,720
find the hypotenuse of a right angled
1119
00:43:00,720 --> 00:43:02,480
triangle if you would like a copy of all
1120
00:43:02,480 --> 00:43:04,240
this code i'll post this in the comments
1121
00:43:04,240 --> 00:43:06,079
section down below if this video helped
1122
00:43:06,079 --> 00:43:07,920
you out feel free to help me out by
1123
00:43:07,920 --> 00:43:09,680
smashing that like button leave random
1124
00:43:09,680 --> 00:43:11,280
comment down below and subscribe if
1125
00:43:11,280 --> 00:43:13,119
you'd like to become a fellow bro hey
1126
00:43:13,119 --> 00:43:15,040
yeah everybody in this video we're going
1127
00:43:15,040 --> 00:43:17,280
to create a simple counter program using
1128
00:43:17,280 --> 00:43:20,480
javascript so sit back relax and enjoy
1129
00:43:20,480 --> 00:43:22,079
the show
1130
00:43:22,079 --> 00:43:24,079
let's begin everybody so head to your
1131
00:43:24,079 --> 00:43:26,880
html document we will create one label
1132
00:43:26,880 --> 00:43:29,440
and three buttons
1133
00:43:29,440 --> 00:43:33,040
label use a closing tag add a line break
1134
00:43:33,040 --> 00:43:36,560
the id of this label will be count label
1135
00:43:36,560 --> 00:43:37,440
count
1136
00:43:37,440 --> 00:43:38,400
label
1137
00:43:38,400 --> 00:43:40,480
the text will set to be zero to begin
1138
00:43:40,480 --> 00:43:42,000
with
1139
00:43:42,000 --> 00:43:45,040
we'll need three buttons
1140
00:43:45,040 --> 00:43:47,119
button
1141
00:43:47,119 --> 00:43:49,280
close it the first button will have an
1142
00:43:49,280 --> 00:43:52,240
id of decrease button
1143
00:43:52,240 --> 00:43:54,079
decrease
1144
00:43:54,079 --> 00:43:55,119
button
1145
00:43:55,119 --> 00:43:58,880
and the text will be decrease
1146
00:43:59,040 --> 00:44:01,280
okay let's copy this button paste it two
1147
00:44:01,280 --> 00:44:03,599
additional times
1148
00:44:03,599 --> 00:44:06,160
the second button will be a reset button
1149
00:44:06,160 --> 00:44:09,280
text reset and the third button will be
1150
00:44:09,280 --> 00:44:10,400
increase
1151
00:44:10,400 --> 00:44:12,560
increase button
1152
00:44:12,560 --> 00:44:13,599
increase
1153
00:44:13,599 --> 00:44:15,760
i would like to change the font size as
1154
00:44:15,760 --> 00:44:17,760
well as the positioning of my count
1155
00:44:17,760 --> 00:44:20,480
label i'll link an external css style
1156
00:44:20,480 --> 00:44:21,280
sheet
1157
00:44:21,280 --> 00:44:23,680
so i will create a new file
1158
00:44:23,680 --> 00:44:26,000
style dot css
1159
00:44:26,000 --> 00:44:28,560
within the head of my html document i
1160
00:44:28,560 --> 00:44:31,119
need to link the style sheet use the
1161
00:44:31,119 --> 00:44:32,400
link tag
1162
00:44:32,400 --> 00:44:34,960
we'll set the relationship attribute set
1163
00:44:34,960 --> 00:44:38,400
the sequel to style sheet and set the
1164
00:44:38,400 --> 00:44:41,200
href attribute to the name of your style
1165
00:44:41,200 --> 00:44:44,400
sheet style dot css we'll need this
1166
00:44:44,400 --> 00:44:46,160
count label id
1167
00:44:46,160 --> 00:44:48,079
i would like to change the css
1168
00:44:48,079 --> 00:44:50,560
properties of my count label so add a
1169
00:44:50,560 --> 00:44:52,880
set of curly braces we'll center this
1170
00:44:52,880 --> 00:44:56,079
label and increase the font size
1171
00:44:56,079 --> 00:44:57,839
display
1172
00:44:57,839 --> 00:44:59,520
block
1173
00:44:59,520 --> 00:45:02,720
text align
1174
00:45:02,839 --> 00:45:07,200
center font size 50 should be fine 50
1175
00:45:07,200 --> 00:45:08,800
pixels
1176
00:45:08,800 --> 00:45:10,560
save your style sheet
1177
00:45:10,560 --> 00:45:12,800
save your html file
1178
00:45:12,800 --> 00:45:15,040
and head to your javascript file we'll
1179
00:45:15,040 --> 00:45:17,599
declare and assign a count variable let
1180
00:45:17,599 --> 00:45:20,400
count equal zero when we click on one of
1181
00:45:20,400 --> 00:45:21,920
these three buttons we would like to
1182
00:45:21,920 --> 00:45:24,480
perform some function
1183
00:45:24,480 --> 00:45:26,640
let's select the decrease button
1184
00:45:26,640 --> 00:45:29,640
document.getelementbyid
1185
00:45:31,520 --> 00:45:32,400
decrease
1186
00:45:32,400 --> 00:45:34,640
button
1187
00:45:34,640 --> 00:45:36,640
dot on
1188
00:45:36,640 --> 00:45:37,920
click
1189
00:45:37,920 --> 00:45:40,319
let me close out of this equals a
1190
00:45:40,319 --> 00:45:43,760
function parenthesis curly braces
1191
00:45:43,760 --> 00:45:45,440
so copy this
1192
00:45:45,440 --> 00:45:48,480
paste it two additional times
1193
00:45:48,480 --> 00:45:50,720
we have reset button
1194
00:45:50,720 --> 00:45:53,119
and increase button when i select the
1195
00:45:53,119 --> 00:45:55,520
decrease button i will take our count
1196
00:45:55,520 --> 00:45:57,920
variable minus equals
1197
00:45:57,920 --> 00:46:00,720
one and we will change the inner html of
1198
00:46:00,720 --> 00:46:02,560
this label
1199
00:46:02,560 --> 00:46:05,560
document.getelementbyid
1200
00:46:06,480 --> 00:46:07,440
count
1201
00:46:07,440 --> 00:46:09,359
label
1202
00:46:09,359 --> 00:46:10,880
dot inner
1203
00:46:10,880 --> 00:46:14,800
html set the sql to our count
1204
00:46:14,800 --> 00:46:18,000
copy these two lines of code
1205
00:46:18,000 --> 00:46:20,400
paste it within each of these functions
1206
00:46:20,400 --> 00:46:22,640
for the reset button we will set count
1207
00:46:22,640 --> 00:46:25,280
to equal zero and the increase button
1208
00:46:25,280 --> 00:46:29,359
count plus equals one let's save
1209
00:46:29,359 --> 00:46:31,599
when i click the increase button it
1210
00:46:31,599 --> 00:46:34,640
increases my label decrease decreases my
1211
00:46:34,640 --> 00:46:35,680
label
1212
00:46:35,680 --> 00:46:38,319
and the reset button resets my label so
1213
00:46:38,319 --> 00:46:40,640
yeah everybody that is a simple counter
1214
00:46:40,640 --> 00:46:43,040
program in javascript i'll post the code
1215
00:46:43,040 --> 00:46:44,880
for this program in the comment section
1216
00:46:44,880 --> 00:46:46,720
down below hey if this video helped you
1217
00:46:46,720 --> 00:46:48,640
out help me out by smashing that like
1218
00:46:48,640 --> 00:46:50,319
button leave random comments down below
1219
00:46:50,319 --> 00:46:51,920
and subscribe if you'd like to become a
1220
00:46:51,920 --> 00:46:54,079
fellow bro
1221
00:46:54,079 --> 00:46:56,319
hey everybody just so you know that this
1222
00:46:56,319 --> 00:46:58,400
topic is completely optional if you're
1223
00:46:58,400 --> 00:47:00,160
ever interested in making any sort of
1224
00:47:00,160 --> 00:47:02,400
games with javascript then this video is
1225
00:47:02,400 --> 00:47:03,920
a must let's begin to create a random
1226
00:47:03,920 --> 00:47:05,920
number let's declare a variable and we
1227
00:47:05,920 --> 00:47:09,280
will set the sequel to type math dot and
1228
00:47:09,280 --> 00:47:11,680
there is a random method then let's
1229
00:47:11,680 --> 00:47:14,640
display whatever x is console.log
1230
00:47:14,640 --> 00:47:17,119
x the random method of math will
1231
00:47:17,119 --> 00:47:19,520
generate a random decimal number between
1232
00:47:19,520 --> 00:47:22,160
0 and 1. but we can actually use that
1233
00:47:22,160 --> 00:47:23,920
maybe we're rolling a dice how do i
1234
00:47:23,920 --> 00:47:26,640
generate a random number between one and
1235
00:47:26,640 --> 00:47:29,839
six so to do this i'm going to take math
1236
00:47:29,839 --> 00:47:33,440
dot random and multiply this by six
1237
00:47:33,440 --> 00:47:35,680
this gives us a random decimal number
1238
00:47:35,680 --> 00:47:38,319
between zero and five technically to
1239
00:47:38,319 --> 00:47:40,079
truncate the decimal portion we can
1240
00:47:40,079 --> 00:47:41,760
round down i will surround this with a
1241
00:47:41,760 --> 00:47:43,200
set of parenthesis
1242
00:47:43,200 --> 00:47:46,960
and we will use math dot floor
1243
00:47:46,960 --> 00:47:49,280
and this will round down this generates
1244
00:47:49,280 --> 00:47:51,760
a random number between 0 and five
1245
00:47:51,760 --> 00:47:53,599
because computers always start with zero
1246
00:47:53,599 --> 00:47:55,760
if i need a one through six i can add an
1247
00:47:55,760 --> 00:47:57,920
offset i'll just add plus one this
1248
00:47:57,920 --> 00:48:00,079
generates a random number between one
1249
00:48:00,079 --> 00:48:02,640
and six as if we were rolling a dice now
1250
00:48:02,640 --> 00:48:04,000
i like to play a lot of dungeons and
1251
00:48:04,000 --> 00:48:06,640
dragons and there's different sized dice
1252
00:48:06,640 --> 00:48:08,800
there's eight sided dice 10 sided dice
1253
00:48:08,800 --> 00:48:11,200
20 sided dice etc if i'm simulating
1254
00:48:11,200 --> 00:48:14,119
rolling a 20-sided dice i would multiply
1255
00:48:14,119 --> 00:48:18,240
math.random method times 20 plus 1. so
1256
00:48:18,240 --> 00:48:20,720
this generates a random number between 1
1257
00:48:20,720 --> 00:48:22,720
and 20. maybe i need to roll a couple
1258
00:48:22,720 --> 00:48:25,200
dice i'll copy this line of code
1259
00:48:25,200 --> 00:48:28,880
we'll create variables y and z
1260
00:48:28,880 --> 00:48:31,280
then display them
1261
00:48:31,280 --> 00:48:35,680
console.log x y and z this would be as
1262
00:48:35,680 --> 00:48:38,000
if we're rolling three dice we get three
1263
00:48:38,000 --> 00:48:40,640
random numbers between one and six just
1264
00:48:40,640 --> 00:48:42,480
for fun let's create some labels and
1265
00:48:42,480 --> 00:48:44,160
generate some random numbers within our
1266
00:48:44,160 --> 00:48:46,880
dom so within the body of my html
1267
00:48:46,880 --> 00:48:49,040
document i'll create a couple labels
1268
00:48:49,040 --> 00:48:53,680
label id this will be x label then close
1269
00:48:53,680 --> 00:48:56,319
this label add a line break i'll add two
1270
00:48:56,319 --> 00:48:58,480
more
1271
00:48:58,480 --> 00:49:00,000
y label
1272
00:49:00,000 --> 00:49:03,520
and z label then i'll create a button
1273
00:49:03,520 --> 00:49:05,520
close the tag
1274
00:49:05,520 --> 00:49:08,480
type equals button
1275
00:49:08,480 --> 00:49:11,599
id equals roll
1276
00:49:11,599 --> 00:49:12,559
button
1277
00:49:12,559 --> 00:49:14,720
then i will add some text to the button
1278
00:49:14,720 --> 00:49:17,520
roll make sure to save heading back to
1279
00:49:17,520 --> 00:49:20,000
our javascript file
1280
00:49:20,000 --> 00:49:21,839
when we click on this button we will
1281
00:49:21,839 --> 00:49:23,680
execute a function
1282
00:49:23,680 --> 00:49:26,319
so we need to select our button document
1283
00:49:26,319 --> 00:49:28,240
dot get
1284
00:49:28,240 --> 00:49:32,319
element by id the name of our button is
1285
00:49:32,319 --> 00:49:35,520
roll button
1286
00:49:35,680 --> 00:49:37,760
dot on click
1287
00:49:37,760 --> 00:49:40,480
equals set the sql to a function
1288
00:49:40,480 --> 00:49:42,839
parentheses curly
1289
00:49:42,839 --> 00:49:47,760
braces let's copy these lines of code
1290
00:49:48,960 --> 00:49:51,359
we don't need to declare these again
1291
00:49:51,359 --> 00:49:53,440
let's get rid of this text
1292
00:49:53,440 --> 00:49:55,920
we'll declare the variables at the top
1293
00:49:55,920 --> 00:49:57,920
so after rolling three random numbers
1294
00:49:57,920 --> 00:50:00,800
between one and six i will update our
1295
00:50:00,800 --> 00:50:01,760
labels
1296
00:50:01,760 --> 00:50:04,480
x label y label and z label we need to
1297
00:50:04,480 --> 00:50:06,319
select each of these labels
1298
00:50:06,319 --> 00:50:09,319
document.getelementbyid
1299
00:50:11,440 --> 00:50:13,599
label dot inner
1300
00:50:13,599 --> 00:50:16,640
html set the sql to
1301
00:50:16,640 --> 00:50:18,160
x
1302
00:50:18,160 --> 00:50:21,440
then do the same thing with y and z
1303
00:50:21,440 --> 00:50:23,839
make sure to change the labels to y
1304
00:50:23,839 --> 00:50:24,880
label
1305
00:50:24,880 --> 00:50:26,720
equals y
1306
00:50:26,720 --> 00:50:29,359
z label equals z
1307
00:50:29,359 --> 00:50:31,920
so make sure to save both of your files
1308
00:50:31,920 --> 00:50:34,400
oh i'm forgetting z there okay
1309
00:50:34,400 --> 00:50:36,640
so now when i press this button we will
1310
00:50:36,640 --> 00:50:39,280
roll three dice and we end up with three
1311
00:50:39,280 --> 00:50:41,839
random numbers between one and six
1312
00:50:41,839 --> 00:50:43,520
so yeah that's how to generate some
1313
00:50:43,520 --> 00:50:46,000
random numbers in javascript if this
1314
00:50:46,000 --> 00:50:47,760
video helped you out you can help me out
1315
00:50:47,760 --> 00:50:49,359
by smashing that like button leave a
1316
00:50:49,359 --> 00:50:51,280
random comment down below and subscribe
1317
00:50:51,280 --> 00:50:54,880
if you'd like to become a fellow bro
1318
00:50:54,880 --> 00:50:56,960
hey welcome back everybody so let's talk
1319
00:50:56,960 --> 00:50:58,960
about some useful string properties and
1320
00:50:58,960 --> 00:51:02,400
methods i have a variable user name and
1321
00:51:02,400 --> 00:51:04,640
assign this whatever your first and last
1322
00:51:04,640 --> 00:51:07,280
name is if we type this variable and add
1323
00:51:07,280 --> 00:51:09,200
a dot we have access to a bunch of
1324
00:51:09,200 --> 00:51:11,040
different properties and methods of this
1325
00:51:11,040 --> 00:51:13,119
string one of which is the length
1326
00:51:13,119 --> 00:51:15,599
property this will give us the length of
1327
00:51:15,599 --> 00:51:17,599
a string how many characters are within
1328
00:51:17,599 --> 00:51:19,359
the string we could assign this to a
1329
00:51:19,359 --> 00:51:23,520
variable name length equal username dot
1330
00:51:23,520 --> 00:51:26,000
length or we could display it i'm going
1331
00:51:26,000 --> 00:51:28,400
to display this with a console.log
1332
00:51:28,400 --> 00:51:29,640
statement
1333
00:51:29,640 --> 00:51:32,720
console.log username.length the length
1334
00:51:32,720 --> 00:51:35,119
of my name is eight characters but yours
1335
00:51:35,119 --> 00:51:37,040
will likely be something different let's
1336
00:51:37,040 --> 00:51:39,119
cover a few more we can get the
1337
00:51:39,119 --> 00:51:42,079
character of a string at a given index
1338
00:51:42,079 --> 00:51:45,359
type the name of the string variable dot
1339
00:51:45,359 --> 00:51:46,319
char
1340
00:51:46,319 --> 00:51:49,119
at add a set of parentheses whatever
1341
00:51:49,119 --> 00:51:50,800
character you would like to return you
1342
00:51:50,800 --> 00:51:52,960
will add the index of that character
1343
00:51:52,960 --> 00:51:54,960
computers always start with zero so the
1344
00:51:54,960 --> 00:51:57,440
first character in the string would have
1345
00:51:57,440 --> 00:52:00,160
an index of zero so maybe i would like
1346
00:52:00,160 --> 00:52:02,640
to assign this to a variable let
1347
00:52:02,640 --> 00:52:08,000
first letter equal username dot char at
1348
00:52:08,000 --> 00:52:10,319
index zero or you know i could display
1349
00:52:10,319 --> 00:52:12,480
it with console.log
1350
00:52:12,480 --> 00:52:14,640
and then just add this variable and
1351
00:52:14,640 --> 00:52:16,640
method within the parentheses the
1352
00:52:16,640 --> 00:52:20,559
character at index 0 of my string is b
1353
00:52:20,559 --> 00:52:23,200
then index 1 would be r
1354
00:52:23,200 --> 00:52:24,240
2
1355
00:52:24,240 --> 00:52:27,440
would be o that is the char at method
1356
00:52:27,440 --> 00:52:29,359
you can find the index of the first
1357
00:52:29,359 --> 00:52:31,599
occurrence of a letter type the variable
1358
00:52:31,599 --> 00:52:35,599
name user name dot
1359
00:52:35,599 --> 00:52:37,200
index
1360
00:52:37,200 --> 00:52:39,520
of then pass in a character you would
1361
00:52:39,520 --> 00:52:41,839
like to find the first index of how
1362
00:52:41,839 --> 00:52:43,839
about o and then i will display this
1363
00:52:43,839 --> 00:52:44,839
with
1364
00:52:44,839 --> 00:52:47,119
console.log the index of the first
1365
00:52:47,119 --> 00:52:49,520
occurrence of the character o is at
1366
00:52:49,520 --> 00:52:50,880
index 2
1367
00:52:50,880 --> 00:52:56,400
0 1 2. there's also last index of
1368
00:52:56,800 --> 00:52:58,640
last
1369
00:52:58,640 --> 00:53:01,680
index of and we'll keep that as o
1370
00:53:01,680 --> 00:53:03,920
the last occurrence of o
1371
00:53:03,920 --> 00:53:05,839
has an index of five
1372
00:53:05,839 --> 00:53:07,760
zero one two
1373
00:53:07,760 --> 00:53:10,720
three four five we can also trim spaces
1374
00:53:10,720 --> 00:53:12,960
before and after a string
1375
00:53:12,960 --> 00:53:16,079
so i'm going to display this as it is
1376
00:53:16,079 --> 00:53:18,480
and i'll turn these into comments
1377
00:53:18,480 --> 00:53:22,319
okay console.log username
1378
00:53:22,319 --> 00:53:23,760
and we have all the space that we would
1379
00:53:23,760 --> 00:53:25,760
like to get rid of
1380
00:53:25,760 --> 00:53:28,640
so to do that we will reassign username
1381
00:53:28,640 --> 00:53:32,400
with user name dot trim
1382
00:53:32,400 --> 00:53:34,400
this gets rid of any empty spaces before
1383
00:53:34,400 --> 00:53:36,880
and after any other characters we could
1384
00:53:36,880 --> 00:53:39,839
make our string all uppercase username
1385
00:53:39,839 --> 00:53:45,200
equals username dot to uppercase
1386
00:53:45,280 --> 00:53:47,359
and my name is all uppercase there is
1387
00:53:47,359 --> 00:53:49,760
also lowercase
1388
00:53:49,760 --> 00:53:50,640
to
1389
00:53:50,640 --> 00:53:53,119
lowercase and all the characters are now
1390
00:53:53,119 --> 00:53:55,680
lowercase okay here's another replace
1391
00:53:55,680 --> 00:53:57,680
all but this time let's create a phone
1392
00:53:57,680 --> 00:53:59,040
number
1393
00:53:59,040 --> 00:54:01,440
let phone number
1394
00:54:01,440 --> 00:54:03,599
equal then within a set of quotes make
1395
00:54:03,599 --> 00:54:05,680
up some phone number with dashes
1396
00:54:05,680 --> 00:54:08,680
123-456-7890
1397
00:54:11,200 --> 00:54:14,000
okay using the replace all method i can
1398
00:54:14,000 --> 00:54:15,920
replace all given characters with
1399
00:54:15,920 --> 00:54:19,440
another one so phone number
1400
00:54:19,440 --> 00:54:20,960
equals
1401
00:54:20,960 --> 00:54:23,359
phone number
1402
00:54:23,359 --> 00:54:26,000
dot replace
1403
00:54:26,000 --> 00:54:27,599
all
1404
00:54:27,599 --> 00:54:30,160
so this has two arguments the character
1405
00:54:30,160 --> 00:54:32,559
we would like to replace
1406
00:54:32,559 --> 00:54:33,760
add a comma
1407
00:54:33,760 --> 00:54:35,599
the second character is what we'll be
1408
00:54:35,599 --> 00:54:38,079
replacing all of these dashes with how
1409
00:54:38,079 --> 00:54:40,400
about a forward slash and we will
1410
00:54:40,400 --> 00:54:42,400
display phone number
1411
00:54:42,400 --> 00:54:44,240
so here's my phone number
1412
00:54:44,240 --> 00:54:45,520
if you would like to eliminate these
1413
00:54:45,520 --> 00:54:48,799
dashes then just don't type in anything
1414
00:54:48,799 --> 00:54:51,200
and those dashes are gone so yeah those
1415
00:54:51,200 --> 00:54:53,520
are a few useful string properties and
1416
00:54:53,520 --> 00:54:55,599
methods if you have a string variable
1417
00:54:55,599 --> 00:54:58,079
add a dot and you have access to a bunch
1418
00:54:58,079 --> 00:54:59,839
of different properties and methods a
1419
00:54:59,839 --> 00:55:01,599
lot of which we didn't cover but these
1420
00:55:01,599 --> 00:55:03,680
are just a few of the basics so yeah
1421
00:55:03,680 --> 00:55:05,520
those are some useful string properties
1422
00:55:05,520 --> 00:55:07,680
and methods if this video helped you out
1423
00:55:07,680 --> 00:55:09,359
you can help me out by smashing that
1424
00:55:09,359 --> 00:55:10,799
like button leave a random comment down
1425
00:55:10,799 --> 00:55:12,400
below and subscribe if you'd like to
1426
00:55:12,400 --> 00:55:15,359
become a fellow bro
1427
00:55:15,359 --> 00:55:17,839
sup everybody let's talk about the slice
1428
00:55:17,839 --> 00:55:20,400
method the slice method extracts a
1429
00:55:20,400 --> 00:55:22,640
section of a string and returns it as a
1430
00:55:22,640 --> 00:55:24,720
new string without modifying the
1431
00:55:24,720 --> 00:55:27,280
original string how is this useful maybe
1432
00:55:27,280 --> 00:55:30,960
we have a variable full name set the
1433
00:55:30,960 --> 00:55:33,760
sequel to whatever your full name is
1434
00:55:33,760 --> 00:55:36,160
what i would like to do is extract
1435
00:55:36,160 --> 00:55:38,480
certain portions of this full name to
1436
00:55:38,480 --> 00:55:40,960
create a first name and a last name
1437
00:55:40,960 --> 00:55:42,880
without changing the full name i'll
1438
00:55:42,880 --> 00:55:44,799
declare two additional variables but not
1439
00:55:44,799 --> 00:55:46,319
assign them yet
1440
00:55:46,319 --> 00:55:50,160
first name as well as last name
1441
00:55:50,160 --> 00:55:52,880
i can create an entirely new string from
1442
00:55:52,880 --> 00:55:55,839
an existing string via the slice method
1443
00:55:55,839 --> 00:55:58,319
let's extract this last name and assign
1444
00:55:58,319 --> 00:56:01,920
it to this last name variable last name
1445
00:56:01,920 --> 00:56:03,280
equals
1446
00:56:03,280 --> 00:56:04,559
then type
1447
00:56:04,559 --> 00:56:08,720
the original string full name dot slice
1448
00:56:08,720 --> 00:56:10,960
within the parentheses there's up to two
1449
00:56:10,960 --> 00:56:13,040
values that we can add the starting
1450
00:56:13,040 --> 00:56:15,839
index and the ending index if i need my
1451
00:56:15,839 --> 00:56:20,319
last name that would be at index 0 1 2 3
1452
00:56:20,319 --> 00:56:23,040
4. so 4 would be the last value if you
1453
00:56:23,040 --> 00:56:25,920
don't add a second value it's assumed to
1454
00:56:25,920 --> 00:56:27,599
copy everything until the end of the
1455
00:56:27,599 --> 00:56:29,760
string i would like the entire rest of
1456
00:56:29,760 --> 00:56:32,400
the string after index 4 and then we'll
1457
00:56:32,400 --> 00:56:34,880
assign it to this new variable last name
1458
00:56:34,880 --> 00:56:36,839
let's display it to test it
1459
00:56:36,839 --> 00:56:38,480
console.log
1460
00:56:38,480 --> 00:56:40,960
last name and this should display my
1461
00:56:40,960 --> 00:56:43,200
last name yep what if i need the first
1462
00:56:43,200 --> 00:56:45,920
name you can place up to two indices so
1463
00:56:45,920 --> 00:56:48,799
first name equals
1464
00:56:48,799 --> 00:56:51,520
type the original string full name
1465
00:56:51,520 --> 00:56:54,960
dot slice the starting index will be
1466
00:56:54,960 --> 00:56:57,839
zero and the ending index will be in my
1467
00:56:57,839 --> 00:57:01,760
example 0 1 2 3. this doesn't include
1468
00:57:01,760 --> 00:57:03,760
the last character all right then let's
1469
00:57:03,760 --> 00:57:06,960
display my first name
1470
00:57:07,359 --> 00:57:09,040
and there's my first name even though
1471
00:57:09,040 --> 00:57:11,119
this does work it wouldn't be realistic
1472
00:57:11,119 --> 00:57:13,520
to do this by hand manually every time
1473
00:57:13,520 --> 00:57:15,040
we would like to create a first name and
1474
00:57:15,040 --> 00:57:17,040
a last name from a full name right so
1475
00:57:17,040 --> 00:57:19,280
why don't we use the string index of
1476
00:57:19,280 --> 00:57:21,359
method to search for any spaces and
1477
00:57:21,359 --> 00:57:23,599
return it index so let's try this again
1478
00:57:23,599 --> 00:57:25,520
let's begin with last name
1479
00:57:25,520 --> 00:57:29,119
last name equals full name
1480
00:57:29,119 --> 00:57:30,880
dot slice
1481
00:57:30,880 --> 00:57:33,280
and we only need one index we need the
1482
00:57:33,280 --> 00:57:35,440
starting position of where to begin so
1483
00:57:35,440 --> 00:57:37,200
we would like to begin where there's any
1484
00:57:37,200 --> 00:57:40,079
spaces plus one so we need to begin at
1485
00:57:40,079 --> 00:57:41,839
this character in my example but it
1486
00:57:41,839 --> 00:57:43,200
might be different depending on what
1487
00:57:43,200 --> 00:57:45,040
your full name is so i would like to
1488
00:57:45,040 --> 00:57:47,680
search for any spaces and i can do that
1489
00:57:47,680 --> 00:57:50,880
by taking my string full name dot and
1490
00:57:50,880 --> 00:57:54,160
use the index of operator and we will
1491
00:57:54,160 --> 00:57:56,799
search for the index of any spaces
1492
00:57:56,799 --> 00:57:59,200
so let me hide my first name here
1493
00:57:59,200 --> 00:58:02,079
oh pay attention to capitalization there
1494
00:58:02,079 --> 00:58:04,160
however i need to get rid of the space
1495
00:58:04,160 --> 00:58:07,119
because the first value is inclusive so
1496
00:58:07,119 --> 00:58:09,599
i will add plus one
1497
00:58:09,599 --> 00:58:11,839
everything after the first space
1498
00:58:11,839 --> 00:58:13,839
slice it and create a new string so
1499
00:58:13,839 --> 00:58:15,839
there's my last name then let's do this
1500
00:58:15,839 --> 00:58:17,200
with our first name
1501
00:58:17,200 --> 00:58:18,160
first
1502
00:58:18,160 --> 00:58:19,359
name
1503
00:58:19,359 --> 00:58:20,960
equals
1504
00:58:20,960 --> 00:58:22,720
full name
1505
00:58:22,720 --> 00:58:24,559
dot slice
1506
00:58:24,559 --> 00:58:26,880
then we'll need two indices
1507
00:58:26,880 --> 00:58:29,040
zero the beginning
1508
00:58:29,040 --> 00:58:33,599
and we will end wherever there's a space
1509
00:58:34,000 --> 00:58:36,160
so there's my first name and my last
1510
00:58:36,160 --> 00:58:37,920
name let's try a different name just to
1511
00:58:37,920 --> 00:58:39,680
be sure that it's working right how
1512
00:58:39,680 --> 00:58:42,960
about snoop dogg
1513
00:58:42,960 --> 00:58:46,240
yep snoop dogg all right everybody that
1514
00:58:46,240 --> 00:58:48,400
is the slice method it extracts a
1515
00:58:48,400 --> 00:58:50,880
section of a string and returns it as a
1516
00:58:50,880 --> 00:58:52,799
new string without modifying the
1517
00:58:52,799 --> 00:58:54,880
original string in our example we
1518
00:58:54,880 --> 00:58:57,119
created a first name and a last name
1519
00:58:57,119 --> 00:58:59,599
from a user's full name and that is the
1520
00:58:59,599 --> 00:59:01,440
string method if you found this video
1521
00:59:01,440 --> 00:59:03,440
helpful you can help me out by smashing
1522
00:59:03,440 --> 00:59:05,040
that like button leave random comment
1523
00:59:05,040 --> 00:59:06,720
down below and subscribe if you'd like
1524
00:59:06,720 --> 00:59:09,680
to become a fellow bro
1525
00:59:09,680 --> 00:59:11,680
hey everybody we're talking about method
1526
00:59:11,680 --> 00:59:13,520
chaining method chaining is a
1527
00:59:13,520 --> 00:59:15,680
programming technique where you call one
1528
00:59:15,680 --> 00:59:18,480
method after another in one continuous
1529
00:59:18,480 --> 00:59:20,720
line of code here's an example of where
1530
00:59:20,720 --> 00:59:22,960
it could be useful let's say we have a
1531
00:59:22,960 --> 00:59:25,040
username
1532
00:59:25,040 --> 00:59:26,720
and assign this whatever your first name
1533
00:59:26,720 --> 00:59:28,400
is but i'm going to make this all
1534
00:59:28,400 --> 00:59:30,720
lowercase in this example what i would
1535
00:59:30,720 --> 00:59:33,119
like to do is take the first character
1536
00:59:33,119 --> 00:59:35,599
of the string and make it uppercase
1537
00:59:35,599 --> 00:59:37,599
without using method chaining we could
1538
00:59:37,599 --> 00:59:39,119
do something like this
1539
00:59:39,119 --> 00:59:41,200
we'll create a temporary variable to
1540
00:59:41,200 --> 00:59:43,520
store a letter and i will return the
1541
00:59:43,520 --> 00:59:46,240
first letter in the string
1542
00:59:46,240 --> 00:59:50,400
user name dot and i will use the char at
1543
00:59:50,400 --> 00:59:51,680
method
1544
00:59:51,680 --> 00:59:53,599
and the first letter has an index of
1545
00:59:53,599 --> 00:59:56,319
zero letter is now b but now i would
1546
00:59:56,319 --> 00:59:58,480
like to make it uppercase i will need to
1547
00:59:58,480 --> 01:00:02,160
reassign my letter variable equals
1548
01:00:02,160 --> 01:00:03,839
letter dot
1549
01:00:03,839 --> 01:00:04,799
to
1550
01:00:04,799 --> 01:00:07,200
uppercase
1551
01:00:07,200 --> 01:00:10,319
and then we can display this
1552
01:00:10,960 --> 01:00:13,920
console.log letter this should display
1553
01:00:13,920 --> 01:00:16,480
the letter capital b to write this code
1554
01:00:16,480 --> 01:00:18,559
a little more elegantly we could use
1555
01:00:18,559 --> 01:00:21,119
method chaining after calling one method
1556
01:00:21,119 --> 01:00:23,599
we can call subsequent methods i'll
1557
01:00:23,599 --> 01:00:26,480
eliminate this line and follow the char
1558
01:00:26,480 --> 01:00:29,040
at method with
1559
01:00:29,040 --> 01:00:30,000
two
1560
01:00:30,000 --> 01:00:31,680
uppercase
1561
01:00:31,680 --> 01:00:33,920
then add that set of parentheses this
1562
01:00:33,920 --> 01:00:35,760
single line of code will do the exact
1563
01:00:35,760 --> 01:00:37,920
same thing and it's easier to read and
1564
01:00:37,920 --> 01:00:40,319
understand you can follow one method
1565
01:00:40,319 --> 01:00:42,400
call it with another if i would like to
1566
01:00:42,400 --> 01:00:44,799
invoke another method i don't know like
1567
01:00:44,799 --> 01:00:46,799
trim i could just add that to the end
1568
01:00:46,799 --> 01:00:48,960
you have the capability of calling one
1569
01:00:48,960 --> 01:00:51,440
method after another in one continuous
1570
01:00:51,440 --> 01:00:53,359
line of code it makes your code cleaner
1571
01:00:53,359 --> 01:00:55,359
and more readable so yeah that's method
1572
01:00:55,359 --> 01:00:56,960
chaining if you found this video helpful
1573
01:00:56,960 --> 01:00:58,720
feel free to help me out by smashing
1574
01:00:58,720 --> 01:01:00,160
that like button leave a random comment
1575
01:01:00,160 --> 01:01:01,680
down below and subscribe if you'd like
1576
01:01:01,680 --> 01:01:04,720
to become a fellow bro
1577
01:01:04,720 --> 01:01:06,480
sup guys in this video i'm going to
1578
01:01:06,480 --> 01:01:09,040
explain if statements an if statement is
1579
01:01:09,040 --> 01:01:11,920
a basic form of decision making if a
1580
01:01:11,920 --> 01:01:14,720
condition is true then we do something
1581
01:01:14,720 --> 01:01:17,760
we execute some subset of code if not
1582
01:01:17,760 --> 01:01:20,000
then we don't do it in this example i
1583
01:01:20,000 --> 01:01:22,160
have variable age and i'll set the
1584
01:01:22,160 --> 01:01:25,200
sequel to maybe 21. if somebody's age is
1585
01:01:25,200 --> 01:01:27,119
over 18 i would like to display a
1586
01:01:27,119 --> 01:01:29,440
message that says you're an adult if
1587
01:01:29,440 --> 01:01:31,119
they're under 18 it doesn't display
1588
01:01:31,119 --> 01:01:33,200
anything so to create an if statement
1589
01:01:33,200 --> 01:01:35,839
type if a set of parentheses for a
1590
01:01:35,839 --> 01:01:38,960
condition and then a set of curly braces
1591
01:01:38,960 --> 01:01:41,839
if some condition that we check is true
1592
01:01:41,839 --> 01:01:43,760
we will execute any code within these
1593
01:01:43,760 --> 01:01:45,680
curly braces i would like to check to
1594
01:01:45,680 --> 01:01:49,200
see if age is greater than or equal to
1595
01:01:49,200 --> 01:01:52,000
18. if it is then we'll execute whatever
1596
01:01:52,000 --> 01:01:54,240
code is within these curly braces and
1597
01:01:54,240 --> 01:01:57,280
i'll display a message
1598
01:01:57,680 --> 01:02:01,200
you are an adult
1599
01:02:01,200 --> 01:02:03,680
my current age is 21 the if statement
1600
01:02:03,680 --> 01:02:05,200
checks this condition
1601
01:02:05,200 --> 01:02:08,319
it's true so we do this if it's not true
1602
01:02:08,319 --> 01:02:10,799
like i'm 12 it will skip over anything
1603
01:02:10,799 --> 01:02:13,200
within these curly braces entirely this
1604
01:02:13,200 --> 01:02:15,599
gives us a lot of options if statements
1605
01:02:15,599 --> 01:02:18,319
are a basic form of decision making now
1606
01:02:18,319 --> 01:02:19,760
in place of just skipping this code
1607
01:02:19,760 --> 01:02:22,079
entirely we could do something else if
1608
01:02:22,079 --> 01:02:23,440
you would like to take a different
1609
01:02:23,440 --> 01:02:25,520
course of action you could add an else
1610
01:02:25,520 --> 01:02:27,680
statement else let's print a different
1611
01:02:27,680 --> 01:02:30,160
message
1612
01:02:30,720 --> 01:02:33,680
you are a child
1613
01:02:33,680 --> 01:02:35,520
now when i run the same code we will
1614
01:02:35,520 --> 01:02:38,000
check this if statement if it's false
1615
01:02:38,000 --> 01:02:39,839
then we skip everything within the curly
1616
01:02:39,839 --> 01:02:42,559
braces if there's an else statement we
1617
01:02:42,559 --> 01:02:44,319
instead do this
1618
01:02:44,319 --> 01:02:46,720
you are a child if there's any other
1619
01:02:46,720 --> 01:02:48,160
conditions you would like to check
1620
01:02:48,160 --> 01:02:49,839
before reaching the else statement you
1621
01:02:49,839 --> 01:02:52,640
can add an else if statement
1622
01:02:52,640 --> 01:02:53,680
else
1623
01:02:53,680 --> 01:02:57,280
if maybe i'll check to see if age is
1624
01:02:57,280 --> 01:02:59,760
less than zero and we'll display
1625
01:02:59,760 --> 01:03:01,400
something else
1626
01:03:01,400 --> 01:03:03,119
console.log
1627
01:03:03,119 --> 01:03:04,960
you haven't
1628
01:03:04,960 --> 01:03:07,359
been born yet
1629
01:03:07,359 --> 01:03:09,680
currently my age is 12
1630
01:03:09,680 --> 01:03:12,400
this condition isn't true we skip it
1631
01:03:12,400 --> 01:03:14,880
this condition isn't true we skip it and
1632
01:03:14,880 --> 01:03:16,640
we move on to the else statement kind of
1633
01:03:16,640 --> 01:03:19,039
like a last resort now if this condition
1634
01:03:19,039 --> 01:03:20,160
is true
1635
01:03:20,160 --> 01:03:22,960
age is less than zero maybe i'm negative
1636
01:03:22,960 --> 01:03:25,200
five years old well then
1637
01:03:25,200 --> 01:03:27,359
this statement is false but this one is
1638
01:03:27,359 --> 01:03:29,359
true and you can add as many else if
1639
01:03:29,359 --> 01:03:31,280
statements as you want now check this
1640
01:03:31,280 --> 01:03:33,599
out let's check to see if somebody is a
1641
01:03:33,599 --> 01:03:36,400
senior if they're 65 or older
1642
01:03:36,400 --> 01:03:39,520
else if age is greater than or equal to
1643
01:03:39,520 --> 01:03:42,240
65.
1644
01:03:43,280 --> 01:03:45,520
you are a senior
1645
01:03:45,520 --> 01:03:47,119
citizen
1646
01:03:47,119 --> 01:03:50,559
so let's change our age to 65.
1647
01:03:50,559 --> 01:03:52,240
now check this out
1648
01:03:52,240 --> 01:03:54,640
so it displays the message you are an
1649
01:03:54,640 --> 01:03:57,119
adult still that's because this if
1650
01:03:57,119 --> 01:03:59,520
statement is still true our age is
1651
01:03:59,520 --> 01:04:01,839
greater than or equal to 18. so the
1652
01:04:01,839 --> 01:04:04,400
order of your if statements does matter
1653
01:04:04,400 --> 01:04:06,000
if i would like to check this condition
1654
01:04:06,000 --> 01:04:07,839
first i should probably move this to the
1655
01:04:07,839 --> 01:04:10,240
top and make it an if statement so let's
1656
01:04:10,240 --> 01:04:12,720
make this statement if and the second
1657
01:04:12,720 --> 01:04:15,119
one else if
1658
01:04:15,119 --> 01:04:15,920
there
1659
01:04:15,920 --> 01:04:18,079
you are a senior citizen if you need to
1660
01:04:18,079 --> 01:04:20,480
check a boolean value that's actually
1661
01:04:20,480 --> 01:04:22,799
really easy to do with if statements
1662
01:04:22,799 --> 01:04:24,960
so i'm going to turn all of these into
1663
01:04:24,960 --> 01:04:28,079
comments and here's another example
1664
01:04:28,079 --> 01:04:30,160
maybe we have a variable
1665
01:04:30,160 --> 01:04:32,720
online if somebody's online
1666
01:04:32,720 --> 01:04:35,520
this is true if they're offline this is
1667
01:04:35,520 --> 01:04:37,599
false if you need to check a boolean
1668
01:04:37,599 --> 01:04:40,400
value using an if statement you could
1669
01:04:40,400 --> 01:04:42,079
just place the boolean variable within
1670
01:04:42,079 --> 01:04:47,520
here if online so if a user is online
1671
01:04:47,520 --> 01:04:49,200
let's display
1672
01:04:49,200 --> 01:04:51,280
you are online
1673
01:04:51,280 --> 01:04:54,280
else
1674
01:04:54,799 --> 01:04:57,440
you are offline
1675
01:04:57,440 --> 01:04:59,680
okay online is set to true you are
1676
01:04:59,680 --> 01:05:02,079
online if this was false
1677
01:05:02,079 --> 01:05:04,799
you are offline so yeah everybody those
1678
01:05:04,799 --> 01:05:07,359
are if statements it's a basic form of
1679
01:05:07,359 --> 01:05:08,720
decision making
1680
01:05:08,720 --> 01:05:12,160
if a condition is true then do something
1681
01:05:12,160 --> 01:05:14,960
if not then don't do it so yeah those
1682
01:05:14,960 --> 01:05:17,119
are if statements in javascript hey if
1683
01:05:17,119 --> 01:05:18,640
this video helped you out you can help
1684
01:05:18,640 --> 01:05:20,400
me out by smashing that like button
1685
01:05:20,400 --> 01:05:22,000
leave a random comment down below and
1686
01:05:22,000 --> 01:05:23,359
subscribe if you'd like to become a
1687
01:05:23,359 --> 01:05:25,839
fellow bro
1688
01:05:25,839 --> 01:05:27,599
hey guys in this video i'm going to
1689
01:05:27,599 --> 01:05:30,160
explain the checked property of check
1690
01:05:30,160 --> 01:05:32,720
boxes and radio buttons all the checked
1691
01:05:32,720 --> 01:05:34,880
property does is let us know if a check
1692
01:05:34,880 --> 01:05:37,119
box or a radio button is selected
1693
01:05:37,119 --> 01:05:38,880
accessing the checked property will give
1694
01:05:38,880 --> 01:05:41,839
us a boolean value true or false heading
1695
01:05:41,839 --> 01:05:43,839
to our html document let's create a
1696
01:05:43,839 --> 01:05:45,200
checkbox
1697
01:05:45,200 --> 01:05:46,400
input
1698
01:05:46,400 --> 01:05:49,119
the type will equal
1699
01:05:49,119 --> 01:05:51,680
check box
1700
01:05:51,680 --> 01:05:54,640
give this check box a unique id
1701
01:05:54,640 --> 01:05:57,200
my check box is good
1702
01:05:57,200 --> 01:05:58,720
there's our check box but we should
1703
01:05:58,720 --> 01:06:00,400
probably add a label because we don't
1704
01:06:00,400 --> 01:06:03,119
know what it's for
1705
01:06:03,119 --> 01:06:05,039
label for
1706
01:06:05,039 --> 01:06:06,160
equals
1707
01:06:06,160 --> 01:06:07,680
copy your id
1708
01:06:07,680 --> 01:06:10,160
paste it
1709
01:06:10,160 --> 01:06:11,839
close the label
1710
01:06:11,839 --> 01:06:13,440
let's add some text
1711
01:06:13,440 --> 01:06:16,799
perhaps this is a subscribe button
1712
01:06:16,799 --> 01:06:19,280
we can check either the check box itself
1713
01:06:19,280 --> 01:06:21,839
or click on the label to check that
1714
01:06:21,839 --> 01:06:23,839
then we will add a button this will be a
1715
01:06:23,839 --> 01:06:25,200
submit button
1716
01:06:25,200 --> 01:06:29,920
button id equals my button
1717
01:06:29,920 --> 01:06:32,960
let's close it add some text
1718
01:06:32,960 --> 01:06:34,319
submit
1719
01:06:34,319 --> 01:06:36,559
i think i'll add this on a new line i'll
1720
01:06:36,559 --> 01:06:38,480
add a line break
1721
01:06:38,480 --> 01:06:40,400
and that should be good for now
1722
01:06:40,400 --> 01:06:42,720
heading to our javascript file when we
1723
01:06:42,720 --> 01:06:44,400
click on this button we would like to do
1724
01:06:44,400 --> 01:06:45,520
something
1725
01:06:45,520 --> 01:06:47,760
first we will select this button by
1726
01:06:47,760 --> 01:06:50,640
typing document dot
1727
01:06:50,640 --> 01:06:51,599
get
1728
01:06:51,599 --> 01:06:52,640
element
1729
01:06:52,640 --> 01:06:54,240
by id
1730
01:06:54,240 --> 01:06:56,480
the id of that button was
1731
01:06:56,480 --> 01:06:57,760
my button
1732
01:06:57,760 --> 01:07:01,359
set the on click event attribute equal
1733
01:07:01,359 --> 01:07:03,520
to a function
1734
01:07:03,520 --> 01:07:05,280
when we click on this button what would
1735
01:07:05,280 --> 01:07:07,200
we like to do i would like to see if
1736
01:07:07,200 --> 01:07:09,680
this checkbox is checked or not
1737
01:07:09,680 --> 01:07:12,000
i'll use an if else statement
1738
01:07:12,000 --> 01:07:13,920
if that checkbox is checked we'll do
1739
01:07:13,920 --> 01:07:16,960
something if not we'll do something else
1740
01:07:16,960 --> 01:07:19,359
within the parentheses we'll select this
1741
01:07:19,359 --> 01:07:21,039
checkbox
1742
01:07:21,039 --> 01:07:22,880
document dot
1743
01:07:22,880 --> 01:07:23,920
get
1744
01:07:23,920 --> 01:07:26,559
element by id
1745
01:07:26,559 --> 01:07:29,200
the id of that checkbox was
1746
01:07:29,200 --> 01:07:31,440
my checkbox
1747
01:07:31,440 --> 01:07:34,880
follow this with the checked property
1748
01:07:34,880 --> 01:07:36,880
we can check to see if this is true by
1749
01:07:36,880 --> 01:07:38,880
using the comparison operator which is
1750
01:07:38,880 --> 01:07:41,280
two equal signs true
1751
01:07:41,280 --> 01:07:42,559
this is optional but you don't
1752
01:07:42,559 --> 01:07:44,640
necessarily need to write that
1753
01:07:44,640 --> 01:07:47,839
if document.getelementbyid
1754
01:07:47,839 --> 01:07:50,640
my checkbox is checked what are we going
1755
01:07:50,640 --> 01:07:51,520
to do
1756
01:07:51,520 --> 01:07:52,960
let's let the user know that they're
1757
01:07:52,960 --> 01:07:54,240
subscribed
1758
01:07:54,240 --> 01:07:57,240
console.log
1759
01:07:57,520 --> 01:08:01,280
you are subscribed
1760
01:08:01,280 --> 01:08:04,799
else if this checkbox is not checked
1761
01:08:04,799 --> 01:08:07,039
then you are
1762
01:08:07,039 --> 01:08:09,440
not subscribed
1763
01:08:09,440 --> 01:08:12,240
okay let's try it i'll just press submit
1764
01:08:12,240 --> 01:08:14,559
you are not subscribed i'll reload the
1765
01:08:14,559 --> 01:08:16,880
page check that checkbox again press
1766
01:08:16,880 --> 01:08:19,359
submit you are subscribed
1767
01:08:19,359 --> 01:08:20,960
now this can be a little difficult to
1768
01:08:20,960 --> 01:08:23,040
read what i like to do is store elements
1769
01:08:23,040 --> 01:08:25,359
within a variable for readability i'm
1770
01:08:25,359 --> 01:08:28,158
going to copy this section
1771
01:08:28,158 --> 01:08:30,080
then assign it to a variable maybe a
1772
01:08:30,080 --> 01:08:32,960
constant const my
1773
01:08:32,960 --> 01:08:34,640
checkbox
1774
01:08:34,640 --> 01:08:37,120
equals
1775
01:08:37,120 --> 01:08:40,000
by document.getelementbyid my check box
1776
01:08:40,000 --> 01:08:41,759
we can refer to this element by this
1777
01:08:41,759 --> 01:08:43,439
variable name
1778
01:08:43,439 --> 01:08:46,799
if my check box is checked
1779
01:08:46,799 --> 01:08:48,960
this would do the same thing i find it
1780
01:08:48,960 --> 01:08:50,238
easier to read
1781
01:08:50,238 --> 01:08:52,319
okay level two let's do this again but
1782
01:08:52,319 --> 01:08:54,000
with radio buttons
1783
01:08:54,000 --> 01:08:56,158
heading back to our html file we'll
1784
01:08:56,158 --> 01:08:59,279
create three radio buttons
1785
01:08:59,279 --> 01:09:00,479
input
1786
01:09:00,479 --> 01:09:04,559
type equals radio
1787
01:09:04,880 --> 01:09:07,600
these will all have the same name
1788
01:09:07,600 --> 01:09:10,479
perhaps this will be card for payment
1789
01:09:10,479 --> 01:09:13,040
what kind of card are you going to use
1790
01:09:13,040 --> 01:09:15,359
the id of the first button will be a
1791
01:09:15,359 --> 01:09:16,640
visa button
1792
01:09:16,640 --> 01:09:18,880
if they're paying with a visa card
1793
01:09:18,880 --> 01:09:23,040
okay let's copy this paste it two times
1794
01:09:23,040 --> 01:09:26,479
a mastercard button
1795
01:09:26,479 --> 01:09:30,479
then what about paypal button
1796
01:09:32,158 --> 01:09:35,838
i'm gonna add a line break at the end
1797
01:09:36,399 --> 01:09:39,599
then let's add some labels
1798
01:09:40,719 --> 01:09:43,920
label close it
1799
01:09:43,920 --> 01:09:45,679
the four attribute will be the same as
1800
01:09:45,679 --> 01:09:46,839
the
1801
01:09:46,839 --> 01:09:51,198
id the text will be visa
1802
01:09:51,198 --> 01:09:54,158
then mastercard
1803
01:09:55,600 --> 01:09:58,560
master card
1804
01:09:59,360 --> 01:10:02,920
lastly paypal
1805
01:10:05,040 --> 01:10:07,040
paypal
1806
01:10:07,040 --> 01:10:10,000
okay we have our three payment options
1807
01:10:10,000 --> 01:10:11,520
since these three radio buttons are
1808
01:10:11,520 --> 01:10:13,760
within the same group we can only select
1809
01:10:13,760 --> 01:10:16,080
one be sure to save everything heading
1810
01:10:16,080 --> 01:10:18,159
back to our javascript file let's get
1811
01:10:18,159 --> 01:10:20,239
all of the buttons and store them within
1812
01:10:20,239 --> 01:10:22,080
a variable
1813
01:10:22,080 --> 01:10:24,159
const
1814
01:10:24,159 --> 01:10:26,080
visa button
1815
01:10:26,080 --> 01:10:29,360
equals document.getelementbyid
1816
01:10:29,360 --> 01:10:33,360
the id was visa button
1817
01:10:37,040 --> 01:10:38,560
then we have
1818
01:10:38,560 --> 01:10:40,159
mastercard
1819
01:10:40,159 --> 01:10:43,159
button
1820
01:10:44,080 --> 01:10:47,199
then paypal button
1821
01:10:53,760 --> 01:10:55,520
okay we have our buttons
1822
01:10:55,520 --> 01:10:57,360
using if else statements we'll check to
1823
01:10:57,360 --> 01:11:00,400
see which of these buttons is selected
1824
01:11:00,400 --> 01:11:03,360
first we'll check our visa button if
1825
01:11:03,360 --> 01:11:07,280
visa button dot checked
1826
01:11:07,520 --> 01:11:10,800
then we'll display a message
1827
01:11:11,120 --> 01:11:13,199
you are paying
1828
01:11:13,199 --> 01:11:16,560
with a visa
1829
01:11:18,640 --> 01:11:19,920
else if
1830
01:11:19,920 --> 01:11:22,239
mastercard button is checked you are
1831
01:11:22,239 --> 01:11:23,840
paying with a
1832
01:11:23,840 --> 01:11:26,840
mastercard
1833
01:11:27,520 --> 01:11:30,000
else if
1834
01:11:30,000 --> 01:11:32,000
paypal button is checked
1835
01:11:32,000 --> 01:11:34,400
you are paying with
1836
01:11:34,400 --> 01:11:36,560
paypal
1837
01:11:36,560 --> 01:11:41,480
else no radio button must be selected
1838
01:11:43,280 --> 01:11:48,560
you must select a payment type
1839
01:11:48,560 --> 01:11:50,239
let's try this
1840
01:11:50,239 --> 01:11:52,960
i'll select visa press submit you are
1841
01:11:52,960 --> 01:11:54,640
paying with the visa
1842
01:11:54,640 --> 01:11:57,040
mastercard submit you are paying with
1843
01:11:57,040 --> 01:11:59,760
the mastercard paypal submit
1844
01:11:59,760 --> 01:12:02,239
you are paying with paypal i'll refresh
1845
01:12:02,239 --> 01:12:04,960
the page not select anything that will
1846
01:12:04,960 --> 01:12:07,760
execute the else statement
1847
01:12:07,760 --> 01:12:10,159
you must select a payment type so yeah
1848
01:12:10,159 --> 01:12:12,480
everybody that is the checked property
1849
01:12:12,480 --> 01:12:14,159
they're typically found within check
1850
01:12:14,159 --> 01:12:16,560
boxes and radio buttons you can access
1851
01:12:16,560 --> 01:12:19,120
this property by following a check box
1852
01:12:19,120 --> 01:12:22,000
or a radio button with dot checked this
1853
01:12:22,000 --> 01:12:25,040
value will be true or false if it's true
1854
01:12:25,040 --> 01:12:27,280
you can do something if not you can do
1855
01:12:27,280 --> 01:12:29,199
something else so yeah that's the
1856
01:12:29,199 --> 01:12:31,120
checked property everybody if you would
1857
01:12:31,120 --> 01:12:33,120
like a copy of all this code i'll post
1858
01:12:33,120 --> 01:12:34,560
all of this in the comment section down
1859
01:12:34,560 --> 01:12:36,800
below and well yeah that's the checked
1860
01:12:36,800 --> 01:12:40,320
property in javascript
1861
01:12:41,199 --> 01:12:43,040
hey guys in this video i'm going to
1862
01:12:43,040 --> 01:12:45,840
explain switches a switch is a statement
1863
01:12:45,840 --> 01:12:49,280
that examines a value against many case
1864
01:12:49,280 --> 01:12:52,159
clauses it's more efficient than using
1865
01:12:52,159 --> 01:12:54,719
many else if statements in this example
1866
01:12:54,719 --> 01:12:56,560
i have a letter grade what i would like
1867
01:12:56,560 --> 01:12:58,640
to do is examine this letter grade if
1868
01:12:58,640 --> 01:13:01,360
our grade equals a then we will display
1869
01:13:01,360 --> 01:13:04,320
you did great else if grade equals b you
1870
01:13:04,320 --> 01:13:08,080
did good c you did okay d
1871
01:13:08,080 --> 01:13:11,920
you passed barely f you failed and there
1872
01:13:11,920 --> 01:13:13,840
is an else statement if something is not
1873
01:13:13,840 --> 01:13:15,360
one of these letter grades maybe the
1874
01:13:15,360 --> 01:13:18,080
word pizza pizza is not a letter grade
1875
01:13:18,080 --> 01:13:20,400
it's not normally good practice to use a
1876
01:13:20,400 --> 01:13:22,640
whole bunch of else if statements if you
1877
01:13:22,640 --> 01:13:24,719
find that you're using a lot of else if
1878
01:13:24,719 --> 01:13:26,880
statements it may be better to instead
1879
01:13:26,880 --> 01:13:29,040
create a switch let's write the same
1880
01:13:29,040 --> 01:13:31,360
program but use a switch instead to
1881
01:13:31,360 --> 01:13:34,080
create a switch we type switch a set of
1882
01:13:34,080 --> 01:13:36,080
parentheses and then a set of curly
1883
01:13:36,080 --> 01:13:38,320
braces within the parentheses we're
1884
01:13:38,320 --> 01:13:41,120
going to examine a value or variable i'm
1885
01:13:41,120 --> 01:13:43,440
going to examine our grade and we will
1886
01:13:43,440 --> 01:13:45,600
compare our grade against many case
1887
01:13:45,600 --> 01:13:46,480
clauses
1888
01:13:46,480 --> 01:13:48,320
and see if there's a match to create a
1889
01:13:48,320 --> 01:13:50,800
case clause you type case and then some
1890
01:13:50,800 --> 01:13:52,480
value that you would like to examine to
1891
01:13:52,480 --> 01:13:54,159
see if there's a match
1892
01:13:54,159 --> 01:13:57,360
case a we're checking to see if grade is
1893
01:13:57,360 --> 01:14:00,000
equal to a if they match then we do
1894
01:14:00,000 --> 01:14:02,400
something so add a colon then type
1895
01:14:02,400 --> 01:14:04,159
whatever you would like to do
1896
01:14:04,159 --> 01:14:07,120
console.log
1897
01:14:07,120 --> 01:14:09,840
you did great then at the end of your
1898
01:14:09,840 --> 01:14:12,560
case make sure to add the word break
1899
01:14:12,560 --> 01:14:15,280
this is our case if grade is equal to a
1900
01:14:15,280 --> 01:14:17,920
so what if grade is equal to b
1901
01:14:17,920 --> 01:14:18,960
case
1902
01:14:18,960 --> 01:14:19,760
b
1903
01:14:19,760 --> 01:14:20,880
you did
1904
01:14:20,880 --> 01:14:24,159
good case c
1905
01:14:24,800 --> 01:14:26,239
you did
1906
01:14:26,239 --> 01:14:27,280
okay
1907
01:14:27,280 --> 01:14:29,840
case d
1908
01:14:31,679 --> 01:14:32,960
you
1909
01:14:32,960 --> 01:14:35,440
passed
1910
01:14:35,520 --> 01:14:36,719
barely
1911
01:14:36,719 --> 01:14:39,360
case f
1912
01:14:40,640 --> 01:14:43,199
you failed now if there's no matching
1913
01:14:43,199 --> 01:14:46,560
cases you can add a default clause
1914
01:14:46,560 --> 01:14:47,679
default
1915
01:14:47,679 --> 01:14:49,360
let's display
1916
01:14:49,360 --> 01:14:51,920
grade
1917
01:14:52,560 --> 01:14:56,080
is not a letter grade
1918
01:14:56,080 --> 01:14:57,920
this will do the same thing except it's
1919
01:14:57,920 --> 01:15:00,480
more efficient our grade is a you did
1920
01:15:00,480 --> 01:15:01,280
great
1921
01:15:01,280 --> 01:15:02,880
b you did good
1922
01:15:02,880 --> 01:15:06,960
c you did okay d you passed barely
1923
01:15:06,960 --> 01:15:08,480
f you failed
1924
01:15:08,480 --> 01:15:10,960
let's make up something pizza
1925
01:15:10,960 --> 01:15:13,040
pizza is not a letter grade so that's a
1926
01:15:13,040 --> 01:15:15,760
switch statement now with your cases you
1927
01:15:15,760 --> 01:15:18,000
could put a condition instead of letter
1928
01:15:18,000 --> 01:15:20,159
grades what if we were working with
1929
01:15:20,159 --> 01:15:23,120
number grades like 95 is an a so another
1930
01:15:23,120 --> 01:15:25,040
way of writing the switch is that if
1931
01:15:25,040 --> 01:15:27,760
we're checking conditions we could pass
1932
01:15:27,760 --> 01:15:30,000
true within the switch we'll examine
1933
01:15:30,000 --> 01:15:32,719
true against some matching conditions
1934
01:15:32,719 --> 01:15:33,679
case
1935
01:15:33,679 --> 01:15:34,640
grade
1936
01:15:34,640 --> 01:15:37,360
is greater than or equal to 90 so that
1937
01:15:37,360 --> 01:15:38,640
would be an a
1938
01:15:38,640 --> 01:15:41,520
case grade is greater than or equal to
1939
01:15:41,520 --> 01:15:44,080
80. that would be a b grade is greater
1940
01:15:44,080 --> 01:15:47,120
than or equal to 70 that's a c grade is
1941
01:15:47,120 --> 01:15:50,719
greater than or equal to 60 that's a d
1942
01:15:50,719 --> 01:15:53,840
grade is less than 60 so that would be
1943
01:15:53,840 --> 01:15:56,239
an f now this way you can compare a
1944
01:15:56,239 --> 01:15:58,880
value or variable against many matching
1945
01:15:58,880 --> 01:16:01,199
conditions so that would be another way
1946
01:16:01,199 --> 01:16:03,199
of writing a switch so yeah everybody
1947
01:16:03,199 --> 01:16:05,440
that is a switch statement it examines a
1948
01:16:05,440 --> 01:16:08,320
value for a matching case against many
1949
01:16:08,320 --> 01:16:10,719
case clauses it's more efficient than
1950
01:16:10,719 --> 01:16:13,520
using many else if statements using an
1951
01:16:13,520 --> 01:16:15,520
elsif statement isn't bad but you don't
1952
01:16:15,520 --> 01:16:17,600
want like you know 10 of them so yeah
1953
01:16:17,600 --> 01:16:19,280
that's a switch everybody if you found
1954
01:16:19,280 --> 01:16:20,880
this video helpful you can help me out
1955
01:16:20,880 --> 01:16:22,560
by smashing that like button leave
1956
01:16:22,560 --> 01:16:24,400
random comments down below and subscribe
1957
01:16:24,400 --> 01:16:27,760
if you'd like to become a fellow bro
1958
01:16:27,760 --> 01:16:30,080
well alright then everybody we are on
1959
01:16:30,080 --> 01:16:33,679
the and as well as or logical operators
1960
01:16:33,679 --> 01:16:35,679
they give us the ability to check more
1961
01:16:35,679 --> 01:16:38,159
than one condition concurrently how is
1962
01:16:38,159 --> 01:16:40,640
this useful here's an example
1963
01:16:40,640 --> 01:16:43,199
we have a variable temp temp short for
1964
01:16:43,199 --> 01:16:45,040
temperature i would like to check to see
1965
01:16:45,040 --> 01:16:47,040
if my temperature falls within a certain
1966
01:16:47,040 --> 01:16:49,600
range using an if statement is 15
1967
01:16:49,600 --> 01:16:51,199
degrees celsius that's not a bad
1968
01:16:51,199 --> 01:16:53,360
temperature within the condition of an
1969
01:16:53,360 --> 01:16:55,679
if statement i would like to check if my
1970
01:16:55,679 --> 01:16:58,560
temperature is greater than 0 and less
1971
01:16:58,560 --> 01:17:00,840
than 30 so within the
1972
01:17:00,840 --> 01:17:04,080
parentheses let's check if temp is
1973
01:17:04,080 --> 01:17:06,560
greater than zero
1974
01:17:06,560 --> 01:17:08,239
and to check another condition
1975
01:17:08,239 --> 01:17:10,480
concurrently we can use
1976
01:17:10,480 --> 01:17:13,440
and which is two ampersands
1977
01:17:13,440 --> 01:17:15,920
and i would like to check if temp is
1978
01:17:15,920 --> 01:17:17,760
less than 30.
1979
01:17:17,760 --> 01:17:19,920
if my temperature falls within this
1980
01:17:19,920 --> 01:17:23,520
range between 0 and 30 then we execute
1981
01:17:23,520 --> 01:17:27,120
whatever's within this if statement
1982
01:17:27,280 --> 01:17:29,199
let's display
1983
01:17:29,199 --> 01:17:30,400
the weather
1984
01:17:30,400 --> 01:17:31,679
is good
1985
01:17:31,679 --> 01:17:34,080
else
1986
01:17:35,679 --> 01:17:38,080
the weather is bad
1987
01:17:38,080 --> 01:17:40,480
our temp is currently 15 15 degrees
1988
01:17:40,480 --> 01:17:41,600
celsius
1989
01:17:41,600 --> 01:17:43,920
the weather is good this condition is
1990
01:17:43,920 --> 01:17:44,800
true
1991
01:17:44,800 --> 01:17:47,600
and this condition is true with the and
1992
01:17:47,600 --> 01:17:50,320
logical operator both conditions must be
1993
01:17:50,320 --> 01:17:52,320
true in order for us to execute this if
1994
01:17:52,320 --> 01:17:54,800
statement if one of them is false then
1995
01:17:54,800 --> 01:17:56,080
we don't
1996
01:17:56,080 --> 01:17:58,400
what if our temperature was negative 10
1997
01:17:58,400 --> 01:18:00,800
this condition is false but this one is
1998
01:18:00,800 --> 01:18:01,679
true
1999
01:18:01,679 --> 01:18:04,239
and using the and logical operator both
2000
01:18:04,239 --> 01:18:06,320
conditions must be true which they're
2001
01:18:06,320 --> 01:18:08,800
not in this example so we skip this if
2002
01:18:08,800 --> 01:18:11,280
statement and execute the else statement
2003
01:18:11,280 --> 01:18:13,520
the weather is bad maybe our temperature
2004
01:18:13,520 --> 01:18:16,480
is 50 degrees celsius this time this
2005
01:18:16,480 --> 01:18:19,360
condition is true but this one is false
2006
01:18:19,360 --> 01:18:21,440
therefore we skip this if statement the
2007
01:18:21,440 --> 01:18:23,920
weather is bad that's one use of the and
2008
01:18:23,920 --> 01:18:25,679
logical operator you can check more than
2009
01:18:25,679 --> 01:18:27,679
one condition and it's useful if you
2010
01:18:27,679 --> 01:18:29,600
need to find if something is within a
2011
01:18:29,600 --> 01:18:31,120
certain range like a range of
2012
01:18:31,120 --> 01:18:33,360
temperatures let's talk about the or
2013
01:18:33,360 --> 01:18:35,760
logical operator using the or logical
2014
01:18:35,760 --> 01:18:38,400
operator either condition can be true in
2015
01:18:38,400 --> 01:18:40,400
order to execute this if statement
2016
01:18:40,400 --> 01:18:42,560
so let's rewrite this this time let's
2017
01:18:42,560 --> 01:18:44,880
check to see if temp is less than or
2018
01:18:44,880 --> 01:18:46,719
equal to zero
2019
01:18:46,719 --> 01:18:47,840
or
2020
01:18:47,840 --> 01:18:49,920
which is represented by two vertical
2021
01:18:49,920 --> 01:18:51,120
bars
2022
01:18:51,120 --> 01:18:53,120
or temp
2023
01:18:53,120 --> 01:18:56,320
is greater than or equal to 30. if at
2024
01:18:56,320 --> 01:18:58,480
least one of these conditions is true
2025
01:18:58,480 --> 01:19:00,560
then we execute this if statement but
2026
01:19:00,560 --> 01:19:03,520
the weather will be bad else the weather
2027
01:19:03,520 --> 01:19:06,400
is good our temperature is currently 50
2028
01:19:06,400 --> 01:19:08,239
the weather is bad what if our
2029
01:19:08,239 --> 01:19:11,120
temperature was negative 10. the weather
2030
01:19:11,120 --> 01:19:13,920
is still bad how about 15
2031
01:19:13,920 --> 01:19:15,600
well the weather is good
2032
01:19:15,600 --> 01:19:18,400
using the or logical operator either one
2033
01:19:18,400 --> 01:19:20,800
of these conditions can be true if one
2034
01:19:20,800 --> 01:19:22,320
is false and the other is true that's
2035
01:19:22,320 --> 01:19:24,400
completely fine now you can add more
2036
01:19:24,400 --> 01:19:26,560
than one logical operator let's head
2037
01:19:26,560 --> 01:19:28,480
back to our previous example i'm just
2038
01:19:28,480 --> 01:19:30,719
going to undo everything
2039
01:19:30,719 --> 01:19:33,199
let's throw in another variable let
2040
01:19:33,199 --> 01:19:35,920
sunny sunny will be a boolean value
2041
01:19:35,920 --> 01:19:37,920
sunny will equal true if it's sunny
2042
01:19:37,920 --> 01:19:40,080
outside if it's cloudy outside well then
2043
01:19:40,080 --> 01:19:42,159
sunny will be false so in order for the
2044
01:19:42,159 --> 01:19:43,920
weather to be good the temperature needs
2045
01:19:43,920 --> 01:19:45,679
to be above zero
2046
01:19:45,679 --> 01:19:47,280
and under 30
2047
01:19:47,280 --> 01:19:50,560
and sunny needs to be equal to true
2048
01:19:50,560 --> 01:19:52,400
but if you're working with a boolean
2049
01:19:52,400 --> 01:19:55,360
value you don't need to say equals true
2050
01:19:55,360 --> 01:19:57,840
you can just add that boolean variable
2051
01:19:57,840 --> 01:20:00,000
so our temperature is 15
2052
01:20:00,000 --> 01:20:02,400
and sunny is true all three of these
2053
01:20:02,400 --> 01:20:04,640
conditions are true therefore the
2054
01:20:04,640 --> 01:20:07,199
weather is good if the temperature is 15
2055
01:20:07,199 --> 01:20:10,080
but it's not sunny outside that's false
2056
01:20:10,080 --> 01:20:12,320
well then the weather is bad you can
2057
01:20:12,320 --> 01:20:14,320
link more than one condition using these
2058
01:20:14,320 --> 01:20:17,440
logical operators so yeah that's the and
2059
01:20:17,440 --> 01:20:20,080
as well as the or logical operators you
2060
01:20:20,080 --> 01:20:21,760
can check more than one condition
2061
01:20:21,760 --> 01:20:23,679
concurrently hey if this video helped
2062
01:20:23,679 --> 01:20:25,600
you out you can help me out by smashing
2063
01:20:25,600 --> 01:20:26,960
that like button leave a random comment
2064
01:20:26,960 --> 01:20:28,480
down below and subscribe if you'd like
2065
01:20:28,480 --> 01:20:31,040
to become a fellow bro
2066
01:20:31,040 --> 01:20:33,440
hey everybody let's talk about the nut
2067
01:20:33,440 --> 01:20:35,600
logical operator which is represented by
2068
01:20:35,600 --> 01:20:37,920
an exclamation point within a condition
2069
01:20:37,920 --> 01:20:39,600
it's typically used to reverse a
2070
01:20:39,600 --> 01:20:42,320
condition's boolean value a condition
2071
01:20:42,320 --> 01:20:44,320
that is true that's preceded with the
2072
01:20:44,320 --> 01:20:46,960
not logical operator becomes false if
2073
01:20:46,960 --> 01:20:49,280
it's false originally it becomes true it
2074
01:20:49,280 --> 01:20:51,199
gives us a few additional ways to write
2075
01:20:51,199 --> 01:20:53,920
code here's an example we have variable
2076
01:20:53,920 --> 01:20:56,000
temperature temp for short and i'll set
2077
01:20:56,000 --> 01:20:58,719
this to 15 degrees celsius if i would
2078
01:20:58,719 --> 01:21:00,400
like to check to see if it's warm or
2079
01:21:00,400 --> 01:21:02,639
cold outside normally i could write an
2080
01:21:02,639 --> 01:21:04,080
if else statement
2081
01:21:04,080 --> 01:21:06,960
if temp is greater than zero then it's
2082
01:21:06,960 --> 01:21:09,360
warm outside
2083
01:21:09,360 --> 01:21:12,840
it's warm outside
2084
01:21:12,840 --> 01:21:15,679
else it's cold outside
2085
01:21:15,679 --> 01:21:17,679
currently our temperature is 15 degrees
2086
01:21:17,679 --> 01:21:20,159
celsius it's warm outside if it was
2087
01:21:20,159 --> 01:21:23,280
negative 15 it's cold outside using the
2088
01:21:23,280 --> 01:21:25,280
not logical operator we can write this
2089
01:21:25,280 --> 01:21:27,440
code a different way if we choose to we
2090
01:21:27,440 --> 01:21:29,840
can check to see if the temperature is
2091
01:21:29,840 --> 01:21:32,480
not greater than zero so to do so we
2092
01:21:32,480 --> 01:21:33,920
will surround this condition with a set
2093
01:21:33,920 --> 01:21:35,679
of parentheses and precede this
2094
01:21:35,679 --> 01:21:37,840
condition with the not logical operator
2095
01:21:37,840 --> 01:21:40,159
an exclamation point in plain english
2096
01:21:40,159 --> 01:21:43,199
we're checking to see if our temperature
2097
01:21:43,199 --> 01:21:46,159
is not greater than zero so that would
2098
01:21:46,159 --> 01:21:48,320
mean it's cold outside right
2099
01:21:48,320 --> 01:21:50,719
else it's warm outside
2100
01:21:50,719 --> 01:21:54,000
so taking our same temp of negative 15
2101
01:21:54,000 --> 01:21:55,760
it's cold outside
2102
01:21:55,760 --> 01:21:58,800
if it's 15 it's warm outside now let's
2103
01:21:58,800 --> 01:22:01,440
do this with a boolean variable
2104
01:22:01,440 --> 01:22:02,400
let
2105
01:22:02,400 --> 01:22:04,719
sunny equal true
2106
01:22:04,719 --> 01:22:05,920
if
2107
01:22:05,920 --> 01:22:07,679
with a boolean variable if you would
2108
01:22:07,679 --> 01:22:09,679
like to place it within a condition all
2109
01:22:09,679 --> 01:22:11,199
you have to do to check if it's true is
2110
01:22:11,199 --> 01:22:12,639
just write the name of the boolean
2111
01:22:12,639 --> 01:22:14,960
variable if sunny
2112
01:22:14,960 --> 01:22:16,880
so you don't necessarily need if sunny
2113
01:22:16,880 --> 01:22:18,159
equals true
2114
01:22:18,159 --> 01:22:19,280
if sunny
2115
01:22:19,280 --> 01:22:21,280
if sunny is true
2116
01:22:21,280 --> 01:22:26,239
then it's sunny outside else
2117
01:22:27,120 --> 01:22:30,159
it's cloudy outside
2118
01:22:30,159 --> 01:22:32,320
currently sunny is true
2119
01:22:32,320 --> 01:22:34,639
therefore it's sunny outside if i would
2120
01:22:34,639 --> 01:22:37,120
like to check if it's not sunny outside
2121
01:22:37,120 --> 01:22:39,600
i can precede this boolean variable with
2122
01:22:39,600 --> 01:22:42,159
the not logical operator and you don't
2123
01:22:42,159 --> 01:22:43,679
necessarily need to surround it with
2124
01:22:43,679 --> 01:22:45,679
those parentheses if it's a boolean
2125
01:22:45,679 --> 01:22:48,880
variable if it's not sunny well then
2126
01:22:48,880 --> 01:22:52,239
it's cloudy outside else it's sunny
2127
01:22:52,239 --> 01:22:55,760
outside sunny is true it's sunny outside
2128
01:22:55,760 --> 01:22:58,639
if it's false it's cloudy outside so by
2129
01:22:58,639 --> 01:23:01,199
using the not logical operator it gives
2130
01:23:01,199 --> 01:23:02,960
us a few additional ways in which we
2131
01:23:02,960 --> 01:23:05,040
could write code more versatility is a
2132
01:23:05,040 --> 01:23:06,960
good thing so yeah that's the not
2133
01:23:06,960 --> 01:23:09,120
logical operator it's typically used to
2134
01:23:09,120 --> 01:23:12,080
reverse a condition's boolean value if a
2135
01:23:12,080 --> 01:23:14,880
condition is true it's now false if it's
2136
01:23:14,880 --> 01:23:17,600
originally false it's now true and that
2137
01:23:17,600 --> 01:23:19,679
is the not logical operator hey if you
2138
01:23:19,679 --> 01:23:21,199
found this video helpful be sure to
2139
01:23:21,199 --> 01:23:22,800
smash that like button leave a random
2140
01:23:22,800 --> 01:23:24,400
comment down below and subscribe if
2141
01:23:24,400 --> 01:23:27,280
you'd like to become a fellow bro
2142
01:23:27,280 --> 01:23:29,679
well sup everybody i have to explain
2143
01:23:29,679 --> 01:23:31,760
what a while loop is a while loop is
2144
01:23:31,760 --> 01:23:34,320
like an if statement except it repeats
2145
01:23:34,320 --> 01:23:37,280
some code while some condition is true
2146
01:23:37,280 --> 01:23:39,679
it could potentially repeat an infinite
2147
01:23:39,679 --> 01:23:41,600
amount of times here's an example let's
2148
01:23:41,600 --> 01:23:43,600
say we need to get some user input we
2149
01:23:43,600 --> 01:23:45,280
would like a user to type in their
2150
01:23:45,280 --> 01:23:48,320
username let's create a variable user
2151
01:23:48,320 --> 01:23:50,719
name and i will set the sequel to just
2152
01:23:50,719 --> 01:23:52,639
an empty string for this example to
2153
01:23:52,639 --> 01:23:55,120
create a while loop we type while set a
2154
01:23:55,120 --> 01:23:58,000
parenthesis set of curly braces we will
2155
01:23:58,000 --> 01:24:00,800
continue this code while the condition
2156
01:24:00,800 --> 01:24:03,520
remains true let's check to see if our
2157
01:24:03,520 --> 01:24:06,880
username is equal to an empty string
2158
01:24:06,880 --> 01:24:08,560
that means the user didn't type in
2159
01:24:08,560 --> 01:24:10,960
anything so within the curly braces
2160
01:24:10,960 --> 01:24:13,199
let's create a window prompt username
2161
01:24:13,199 --> 01:24:16,960
equals window dot prompt
2162
01:24:16,960 --> 01:24:20,080
enter your name then at the end let's
2163
01:24:20,080 --> 01:24:21,040
display
2164
01:24:21,040 --> 01:24:23,760
console.log
2165
01:24:23,760 --> 01:24:25,280
hello
2166
01:24:25,280 --> 01:24:28,080
user name and to your name i'm just
2167
01:24:28,080 --> 01:24:30,480
going to press ok when we reach the end
2168
01:24:30,480 --> 01:24:32,320
of a while loop we check the condition
2169
01:24:32,320 --> 01:24:34,800
again if it's still true we repeat this
2170
01:24:34,800 --> 01:24:36,719
code for another iteration and then
2171
01:24:36,719 --> 01:24:38,960
check again i'm hitting ok but i can't
2172
01:24:38,960 --> 01:24:41,280
exit this window prompt until i enter in
2173
01:24:41,280 --> 01:24:43,360
my name i'll type in my first name hit
2174
01:24:43,360 --> 01:24:46,719
enter hello bro my username although in
2175
01:24:46,719 --> 01:24:48,480
this example i'm hitting ok but you
2176
01:24:48,480 --> 01:24:50,560
could hit cancel to exit let's add
2177
01:24:50,560 --> 01:24:53,199
another condition
2178
01:24:53,199 --> 01:24:54,480
or
2179
01:24:54,480 --> 01:24:57,360
username is equal to null that means
2180
01:24:57,360 --> 01:24:59,920
there's no value to exit i simply can't
2181
01:24:59,920 --> 01:25:01,360
press ok
2182
01:25:01,360 --> 01:25:04,080
or cancel i need to type in something
2183
01:25:04,080 --> 01:25:05,760
because we're stuck in a while loop i'll
2184
01:25:05,760 --> 01:25:07,600
type in my first name again and then we
2185
01:25:07,600 --> 01:25:09,840
can exit once i typed in something for
2186
01:25:09,840 --> 01:25:12,159
my username both of these conditions
2187
01:25:12,159 --> 01:25:14,560
were no longer true then we could escape
2188
01:25:14,560 --> 01:25:16,239
the while loop and continue on with the
2189
01:25:16,239 --> 01:25:18,159
rest of the program now when you create
2190
01:25:18,159 --> 01:25:19,840
a while loop you should write some sort
2191
01:25:19,840 --> 01:25:21,600
of way to escape the while loop from
2192
01:25:21,600 --> 01:25:23,440
within it otherwise you'll encounter
2193
01:25:23,440 --> 01:25:26,400
what's called an infinite loop like this
2194
01:25:26,400 --> 01:25:27,360
while
2195
01:25:27,360 --> 01:25:30,560
and our condition is while one is equal
2196
01:25:30,560 --> 01:25:33,440
to one there's no way we can escape it
2197
01:25:33,440 --> 01:25:36,400
console.log
2198
01:25:36,400 --> 01:25:37,920
help
2199
01:25:37,920 --> 01:25:42,960
i'm stuck in an infinite loop
2200
01:25:42,960 --> 01:25:44,960
so this code will just repeat forever
2201
01:25:44,960 --> 01:25:46,560
you can see that the counter here is
2202
01:25:46,560 --> 01:25:49,199
going up i think my web browser froze so
2203
01:25:49,199 --> 01:25:51,199
yeah that's a while loop everybody it
2204
01:25:51,199 --> 01:25:53,760
repeats some section of code while some
2205
01:25:53,760 --> 01:25:55,360
condition is true
2206
01:25:55,360 --> 01:25:57,040
once you reach the end of that section
2207
01:25:57,040 --> 01:25:59,679
of code you check the condition again if
2208
01:25:59,679 --> 01:26:01,440
it's still true you repeat the code
2209
01:26:01,440 --> 01:26:03,760
again and this could potentially repeat
2210
01:26:03,760 --> 01:26:05,760
an infinite amount of times so that's a
2211
01:26:05,760 --> 01:26:07,920
while loop in javascript if you found
2212
01:26:07,920 --> 01:26:09,440
this video helpful be sure to help me
2213
01:26:09,440 --> 01:26:11,280
out by smashing that like button leave a
2214
01:26:11,280 --> 01:26:13,120
random comment down below and subscribe
2215
01:26:13,120 --> 01:26:16,080
if you'd like to become a fellow bro
2216
01:26:16,080 --> 01:26:18,159
all right guys i have to explain a
2217
01:26:18,159 --> 01:26:20,480
variation of the while loop it's called
2218
01:26:20,480 --> 01:26:22,880
a do while loop how this works is that
2219
01:26:22,880 --> 01:26:24,880
you do something then you check the
2220
01:26:24,880 --> 01:26:26,639
condition then you repeat if the
2221
01:26:26,639 --> 01:26:28,960
condition is true so going back to the
2222
01:26:28,960 --> 01:26:31,199
previous topic on while loops i have a
2223
01:26:31,199 --> 01:26:32,719
while loop we're checking if our
2224
01:26:32,719 --> 01:26:34,800
username is an empty string which it is
2225
01:26:34,800 --> 01:26:37,120
originally while this condition is true
2226
01:26:37,120 --> 01:26:39,199
we will prompt the user to enter their
2227
01:26:39,199 --> 01:26:41,600
name if i run this program and press ok
2228
01:26:41,600 --> 01:26:43,920
without typing anything i can't escape
2229
01:26:43,920 --> 01:26:46,159
this prompt once i type in my name hit
2230
01:26:46,159 --> 01:26:48,560
enter or ok we escape the while loop and
2231
01:26:48,560 --> 01:26:50,400
move on with the rest of our program
2232
01:26:50,400 --> 01:26:52,639
which displays hello whatever your
2233
01:26:52,639 --> 01:26:54,880
username is what i would like to do is
2234
01:26:54,880 --> 01:26:57,040
not assign my username right away i'll
2235
01:26:57,040 --> 01:26:58,880
declare this variable but not yet assign
2236
01:26:58,880 --> 01:27:00,800
it anything now when i run this program
2237
01:27:00,800 --> 01:27:02,320
here's the problem we don't get that
2238
01:27:02,320 --> 01:27:04,159
window prompt to type in our name we
2239
01:27:04,159 --> 01:27:06,000
skip the while loop and continue on with
2240
01:27:06,000 --> 01:27:07,440
the rest of the program and this
2241
01:27:07,440 --> 01:27:10,080
displays hello undefined we're using a
2242
01:27:10,080 --> 01:27:11,679
variable we declared but not yet
2243
01:27:11,679 --> 01:27:13,840
assigned once we reached our while loop
2244
01:27:13,840 --> 01:27:16,159
this condition was false from the get go
2245
01:27:16,159 --> 01:27:18,480
so we skip over the while loop entirely
2246
01:27:18,480 --> 01:27:20,400
what if i would like to run this code at
2247
01:27:20,400 --> 01:27:22,560
least once and then prompt again if the
2248
01:27:22,560 --> 01:27:24,880
username is equal to an empty string one
2249
01:27:24,880 --> 01:27:26,880
way although i wouldn't recommend it is
2250
01:27:26,880 --> 01:27:29,120
that we can copy this line of code and
2251
01:27:29,120 --> 01:27:30,800
paste it before the while loop that
2252
01:27:30,800 --> 01:27:33,199
prompt comes up i'll skip it and then we
2253
01:27:33,199 --> 01:27:34,800
enter the while loop and we're stuck
2254
01:27:34,800 --> 01:27:36,880
within it until we type in something i
2255
01:27:36,880 --> 01:27:38,320
don't like to repeat code if i don't
2256
01:27:38,320 --> 01:27:40,000
have to so there's a better way of
2257
01:27:40,000 --> 01:27:41,920
writing this let's convert this while
2258
01:27:41,920 --> 01:27:44,560
loop to a do while loop let's copy the
2259
01:27:44,560 --> 01:27:46,400
while keyword and the condition and
2260
01:27:46,400 --> 01:27:47,920
we're going to move it to the end of our
2261
01:27:47,920 --> 01:27:50,480
curly braces and preceding the opening
2262
01:27:50,480 --> 01:27:52,880
curly brace we will write do instead of
2263
01:27:52,880 --> 01:27:54,639
checking the condition first we check
2264
01:27:54,639 --> 01:27:57,360
the condition last we will do all of
2265
01:27:57,360 --> 01:27:59,840
this code and then check the condition
2266
01:27:59,840 --> 01:28:01,840
now if i run this program we encounter
2267
01:28:01,840 --> 01:28:04,159
that window prompt i'll press ok and try
2268
01:28:04,159 --> 01:28:06,639
and skip this prompt i can't i'll type
2269
01:28:06,639 --> 01:28:08,639
in my name and we escape the while loop
2270
01:28:08,639 --> 01:28:11,199
so a do while loop is a variation of the
2271
01:28:11,199 --> 01:28:13,360
while loop you do something first and
2272
01:28:13,360 --> 01:28:15,600
then check the condition with a standard
2273
01:28:15,600 --> 01:28:17,679
while loop you check the condition and
2274
01:28:17,679 --> 01:28:19,440
then do something if that condition is
2275
01:28:19,440 --> 01:28:21,600
true with a standard while loop you may
2276
01:28:21,600 --> 01:28:24,080
skip some code entirely if the condition
2277
01:28:24,080 --> 01:28:26,239
is false from the beginning with a do
2278
01:28:26,239 --> 01:28:28,960
while loop you do it at least once and
2279
01:28:28,960 --> 01:28:31,199
then check the condition so yeah that's
2280
01:28:31,199 --> 01:28:34,080
a super quick video on do while loops in
2281
01:28:34,080 --> 01:28:36,159
the next topic we'll move on to for
2282
01:28:36,159 --> 01:28:38,239
loops
2283
01:28:38,239 --> 01:28:40,239
well i should probably explain four
2284
01:28:40,239 --> 01:28:42,960
loops a for loop repeats some code a
2285
01:28:42,960 --> 01:28:44,960
certain amount of times there's a lot of
2286
01:28:44,960 --> 01:28:47,120
similarities between what for loops and
2287
01:28:47,120 --> 01:28:48,800
while loops can do there are a few
2288
01:28:48,800 --> 01:28:50,719
discrepancies though you tend to use for
2289
01:28:50,719 --> 01:28:52,480
loops when you want to repeat code a
2290
01:28:52,480 --> 01:28:54,400
limited amount of times a while loop is
2291
01:28:54,400 --> 01:28:56,159
better suited to something that could
2292
01:28:56,159 --> 01:28:58,000
execute an infinite amount of times
2293
01:28:58,000 --> 01:28:59,679
possibly in this example we'll create a
2294
01:28:59,679 --> 01:29:01,520
program to count to ten we'll start at
2295
01:29:01,520 --> 01:29:03,360
one count to ten and we can do that with
2296
01:29:03,360 --> 01:29:05,360
a for loop so type four
2297
01:29:05,360 --> 01:29:07,600
parentheses curly braces
2298
01:29:07,600 --> 01:29:08,960
what would we like to do within our
2299
01:29:08,960 --> 01:29:12,159
curly braces let's display a variable
2300
01:29:12,159 --> 01:29:13,840
console.log
2301
01:29:13,840 --> 01:29:15,760
let's call this counter now within the
2302
01:29:15,760 --> 01:29:17,440
for loop there's three statements that
2303
01:29:17,440 --> 01:29:19,520
we can add the first is that we can
2304
01:29:19,520 --> 01:29:22,239
declare and assign a counter or some
2305
01:29:22,239 --> 01:29:23,920
other variable the first statement we
2306
01:29:23,920 --> 01:29:26,159
will type let
2307
01:29:26,159 --> 01:29:27,760
counter
2308
01:29:27,760 --> 01:29:29,679
and i'll set the sql to 1.
2309
01:29:29,679 --> 01:29:31,520
then make sure to add a semicolon at the
2310
01:29:31,520 --> 01:29:33,280
end to terminate the first statement
2311
01:29:33,280 --> 01:29:35,360
second is our condition we'll continue
2312
01:29:35,360 --> 01:29:37,040
this statement as long as this condition
2313
01:29:37,040 --> 01:29:39,199
is true i will continue this for loop as
2314
01:29:39,199 --> 01:29:41,840
long as our counter variable is less
2315
01:29:41,840 --> 01:29:43,840
than or equal to 10
2316
01:29:43,840 --> 01:29:45,920
then add a semicolon and the third
2317
01:29:45,920 --> 01:29:47,840
statement is that we can increment our
2318
01:29:47,840 --> 01:29:49,040
counter
2319
01:29:49,040 --> 01:29:50,080
counter
2320
01:29:50,080 --> 01:29:53,120
plus equals one after each iteration we
2321
01:29:53,120 --> 01:29:55,280
increment our counter variable by one
2322
01:29:55,280 --> 01:29:57,280
after running this program we begin at
2323
01:29:57,280 --> 01:29:59,440
one and count to ten if i need to count
2324
01:29:59,440 --> 01:30:02,239
to 100 i can just change this condition
2325
01:30:02,239 --> 01:30:05,120
counter is less than or equal to 100 and
2326
01:30:05,120 --> 01:30:08,400
we have counted from 1 to 100 if i need
2327
01:30:08,400 --> 01:30:09,920
to begin at a different number i could
2328
01:30:09,920 --> 01:30:11,440
just set my counter to a different
2329
01:30:11,440 --> 01:30:13,199
number like i don't know
2330
01:30:13,199 --> 01:30:16,080
50. now we have begun at 50 and counted
2331
01:30:16,080 --> 01:30:18,880
to 100 so this variable counter it's
2332
01:30:18,880 --> 01:30:21,120
known as a local variable it doesn't
2333
01:30:21,120 --> 01:30:23,120
exist outside of this for loop what a
2334
01:30:23,120 --> 01:30:24,880
lot of programmers do if they need some
2335
01:30:24,880 --> 01:30:27,040
sort of temporary variable within a for
2336
01:30:27,040 --> 01:30:28,880
loop to keep track of what iteration
2337
01:30:28,880 --> 01:30:31,120
they're on they'll simply just use i
2338
01:30:31,120 --> 01:30:32,800
that's a common convention so let's
2339
01:30:32,800 --> 01:30:35,760
change counter to i
2340
01:30:35,760 --> 01:30:37,120
so let i
2341
01:30:37,120 --> 01:30:39,679
equal let's change this back to 1
2342
01:30:39,679 --> 01:30:42,239
we'll count to 10 and increment i by one
2343
01:30:42,239 --> 01:30:44,000
after each iteration
2344
01:30:44,000 --> 01:30:46,239
now we could begin at 10 and count down
2345
01:30:46,239 --> 01:30:48,159
if we were to take that approach we'll
2346
01:30:48,159 --> 01:30:50,880
set our variable i to 10 we'll continue
2347
01:30:50,880 --> 01:30:52,960
this for loop as long as i is greater
2348
01:30:52,960 --> 01:30:55,840
than zero and we will decrement i by one
2349
01:30:55,840 --> 01:30:57,120
we could pretend that this is a
2350
01:30:57,120 --> 01:30:59,600
countdown to new year's day this time
2351
01:30:59,600 --> 01:31:01,679
when we escape the for loop let's print
2352
01:31:01,679 --> 01:31:03,199
happy new year
2353
01:31:03,199 --> 01:31:05,679
console.log
2354
01:31:05,679 --> 01:31:08,880
happy new year okay we begin at 10 count
2355
01:31:08,880 --> 01:31:11,120
down to one once this condition is no
2356
01:31:11,120 --> 01:31:13,360
longer true we escape the for loop and
2357
01:31:13,360 --> 01:31:16,080
print happy new year so this simulates a
2358
01:31:16,080 --> 01:31:18,080
countdown so for loops are fairly
2359
01:31:18,080 --> 01:31:20,480
flexible we can increment or decrement
2360
01:31:20,480 --> 01:31:22,880
our counter i we're counting down by one
2361
01:31:22,880 --> 01:31:24,800
currently but we could count down by two
2362
01:31:24,800 --> 01:31:26,960
if we would like let's change i minus
2363
01:31:26,960 --> 01:31:29,840
equals one to two and now we will count
2364
01:31:29,840 --> 01:31:32,800
down by two ten eight six four two happy
2365
01:31:32,800 --> 01:31:35,360
new year or even three ten seven four
2366
01:31:35,360 --> 01:31:37,040
one happy new year so yeah that's
2367
01:31:37,040 --> 01:31:39,600
basically a for loop we repeat some code
2368
01:31:39,600 --> 01:31:41,600
a certain amount of times there's a lot
2369
01:31:41,600 --> 01:31:43,520
of overlap where you could use either a
2370
01:31:43,520 --> 01:31:45,520
while loop or a for loop a while loop
2371
01:31:45,520 --> 01:31:47,520
tends to be better in certain situations
2372
01:31:47,520 --> 01:31:49,679
where you may potentially need to repeat
2373
01:31:49,679 --> 01:31:51,600
some code an infinite amount of times
2374
01:31:51,600 --> 01:31:53,440
for loops are more suitable for code
2375
01:31:53,440 --> 01:31:55,120
where you only need to repeat it a
2376
01:31:55,120 --> 01:31:57,600
certain or given amount of times so yeah
2377
01:31:57,600 --> 01:31:59,280
those are for loops hey if this video
2378
01:31:59,280 --> 01:32:00,880
helped you out you can help me out by
2379
01:32:00,880 --> 01:32:02,480
smashing that like button leave random
2380
01:32:02,480 --> 01:32:04,080
comments down below and subscribe if
2381
01:32:04,080 --> 01:32:07,199
you'd like to become a fellow bro
2382
01:32:07,199 --> 01:32:09,679
hey guys i have a super quick video on
2383
01:32:09,679 --> 01:32:12,480
the break and the continue statements
2384
01:32:12,480 --> 01:32:14,239
let's begin with the break statement the
2385
01:32:14,239 --> 01:32:16,080
break statement breaks out of a loop
2386
01:32:16,080 --> 01:32:18,239
entirely in this example i'm going to
2387
01:32:18,239 --> 01:32:20,000
create a for loop we will count the
2388
01:32:20,000 --> 01:32:22,639
numbers 1 through 20. however once we
2389
01:32:22,639 --> 01:32:25,520
reach 13 i would like to exit this loop
2390
01:32:25,520 --> 01:32:27,679
because 13 is considered an unlucky
2391
01:32:27,679 --> 01:32:29,920
number i know it's a weird example so
2392
01:32:29,920 --> 01:32:33,840
let's define our counter i i equals one
2393
01:32:33,840 --> 01:32:36,560
and i will continue this as long as i is
2394
01:32:36,560 --> 01:32:40,000
less than or equal to 20 then increment
2395
01:32:40,000 --> 01:32:41,520
i by one
2396
01:32:41,520 --> 01:32:42,800
for the time being let's display
2397
01:32:42,800 --> 01:32:44,960
whatever i is this will display the
2398
01:32:44,960 --> 01:32:47,520
numbers 1 through 20. if i would like to
2399
01:32:47,520 --> 01:32:49,679
break out of this loop entirely if we
2400
01:32:49,679 --> 01:32:52,480
reach the number 13 i can add a break
2401
01:32:52,480 --> 01:32:56,719
statement so let's check to see if
2402
01:32:56,719 --> 01:32:57,840
i
2403
01:32:57,840 --> 01:32:59,760
is equal to 13
2404
01:32:59,760 --> 01:33:02,159
if it is then we'll break
2405
01:33:02,159 --> 01:33:04,800
okay we have the numbers 1 through 12
2406
01:33:04,800 --> 01:33:06,880
then we break there's also the continue
2407
01:33:06,880 --> 01:33:09,600
statement it will skip an iteration of a
2408
01:33:09,600 --> 01:33:13,760
loop if we replace break with continue
2409
01:33:13,760 --> 01:33:16,239
we will skip that specific iteration and
2410
01:33:16,239 --> 01:33:17,679
continue on
2411
01:33:17,679 --> 01:33:20,639
so we have 1 through 20 but the number
2412
01:33:20,639 --> 01:33:23,040
13 is not here so that's the main
2413
01:33:23,040 --> 01:33:25,280
difference between break and continue
2414
01:33:25,280 --> 01:33:27,920
break will break out of a loop entirely
2415
01:33:27,920 --> 01:33:30,719
continue will skip only that iteration
2416
01:33:30,719 --> 01:33:32,159
so yeah those are the differences
2417
01:33:32,159 --> 01:33:33,760
between the break and continue
2418
01:33:33,760 --> 01:33:36,159
statements
2419
01:33:36,159 --> 01:33:37,920
hey guys in this topic i'll be
2420
01:33:37,920 --> 01:33:40,400
explaining nested loops a nested loop is
2421
01:33:40,400 --> 01:33:42,560
just a loop inside of another loop
2422
01:33:42,560 --> 01:33:44,400
here's an example so i'll create an
2423
01:33:44,400 --> 01:33:46,080
outer loop that will count i don't know
2424
01:33:46,080 --> 01:33:47,360
to three
2425
01:33:47,360 --> 01:33:48,560
let i
2426
01:33:48,560 --> 01:33:52,080
equal one continue this as long as i is
2427
01:33:52,080 --> 01:33:54,639
less than or equal to three increment i
2428
01:33:54,639 --> 01:33:56,560
by one
2429
01:33:56,560 --> 01:33:58,800
during each iteration i will display the
2430
01:33:58,800 --> 01:34:00,400
value of i
2431
01:34:00,400 --> 01:34:03,199
console.log i this counts up to three
2432
01:34:03,199 --> 01:34:04,880
what if i would like to repeat this
2433
01:34:04,880 --> 01:34:07,280
entire for loop so many times well i
2434
01:34:07,280 --> 01:34:09,280
could put a for loop inside of a for
2435
01:34:09,280 --> 01:34:10,480
loop
2436
01:34:10,480 --> 01:34:12,239
so let me copy this
2437
01:34:12,239 --> 01:34:14,239
get rid of this line
2438
01:34:14,239 --> 01:34:16,320
and paste another for loop within the
2439
01:34:16,320 --> 01:34:18,719
curly braces of the outer for loop we
2440
01:34:18,719 --> 01:34:21,040
have a nested loop it's a loop inside of
2441
01:34:21,040 --> 01:34:23,120
another loop now one thing with nested
2442
01:34:23,120 --> 01:34:25,280
loops is that you'll want another
2443
01:34:25,280 --> 01:34:27,520
counter besides i because we don't want
2444
01:34:27,520 --> 01:34:29,520
to reuse the same counter a common
2445
01:34:29,520 --> 01:34:31,679
convention of the counter of nested
2446
01:34:31,679 --> 01:34:34,400
loops is that they'll use a counter of j
2447
01:34:34,400 --> 01:34:37,120
because j comes after in the alphabet
2448
01:34:37,120 --> 01:34:39,840
so let's replace i with j
2449
01:34:39,840 --> 01:34:42,880
and do so with our console.log statement
2450
01:34:42,880 --> 01:34:45,119
so if i was to run this program we would
2451
01:34:45,119 --> 01:34:47,360
count the numbers one through three
2452
01:34:47,360 --> 01:34:49,760
three times once we complete all of our
2453
01:34:49,760 --> 01:34:52,400
iterations of the nested loop we then
2454
01:34:52,400 --> 01:34:55,440
complete one iteration of the outer loop
2455
01:34:55,440 --> 01:34:57,840
if i change this outer loop to continue
2456
01:34:57,840 --> 01:35:00,000
as long as i is less than or equal to
2457
01:35:00,000 --> 01:35:02,800
two well then we will repeat this inner
2458
01:35:02,800 --> 01:35:05,280
loop a total of two times here's a
2459
01:35:05,280 --> 01:35:07,600
different example let's use nested loops
2460
01:35:07,600 --> 01:35:09,840
to draw a rectangle within our dom and
2461
01:35:09,840 --> 01:35:12,080
we'll accept some user input the outer
2462
01:35:12,080 --> 01:35:14,320
loop will be in charge of the rows the
2463
01:35:14,320 --> 01:35:16,000
inner loop will be in charge of the
2464
01:35:16,000 --> 01:35:19,440
columns we'll declare two variables let
2465
01:35:19,440 --> 01:35:22,239
rows equal window
2466
01:35:22,239 --> 01:35:24,800
dot prompt
2467
01:35:24,800 --> 01:35:27,679
enter number of rows
2468
01:35:27,679 --> 01:35:29,119
then let
2469
01:35:29,119 --> 01:35:33,040
columns equal number of columns
2470
01:35:33,040 --> 01:35:35,040
the outer for loop i will continue as
2471
01:35:35,040 --> 01:35:38,480
long as i is less than or equal to rows
2472
01:35:38,480 --> 01:35:40,719
and the inner for loop will continue as
2473
01:35:40,719 --> 01:35:43,520
long as j is less than or equal to
2474
01:35:43,520 --> 01:35:44,639
columns
2475
01:35:44,639 --> 01:35:46,719
we'll need an html element to work with
2476
01:35:46,719 --> 01:35:49,440
so within your html file let's create a
2477
01:35:49,440 --> 01:35:51,360
label
2478
01:35:51,360 --> 01:35:52,639
label
2479
01:35:52,639 --> 01:35:53,760
close it
2480
01:35:53,760 --> 01:35:56,880
and i will give this an id of my
2481
01:35:56,880 --> 01:35:58,960
rectangle
2482
01:35:58,960 --> 01:36:00,480
make sure to save
2483
01:36:00,480 --> 01:36:02,800
then heading back to our javascript file
2484
01:36:02,800 --> 01:36:06,159
let's change the inner html of our label
2485
01:36:06,159 --> 01:36:09,280
i will select our label
2486
01:36:09,280 --> 01:36:12,000
element document.getelementby id
2487
01:36:12,000 --> 01:36:14,400
my rectangle
2488
01:36:14,400 --> 01:36:16,960
i will set the inner
2489
01:36:16,960 --> 01:36:19,840
html before we draw a rectangle let's
2490
01:36:19,840 --> 01:36:21,679
print some of these numbers
2491
01:36:21,679 --> 01:36:24,560
i would like to append the inner html i
2492
01:36:24,560 --> 01:36:26,560
will set this to plus
2493
01:36:26,560 --> 01:36:28,320
equals
2494
01:36:28,320 --> 01:36:30,080
our counter j
2495
01:36:30,080 --> 01:36:33,040
the inner for loop will print one row
2496
01:36:33,040 --> 01:36:35,280
then to go down to the next row we can
2497
01:36:35,280 --> 01:36:37,520
add a break line
2498
01:36:37,520 --> 01:36:38,400
document
2499
01:36:38,400 --> 01:36:42,239
dot get element by id
2500
01:36:42,239 --> 01:36:45,600
my rectangle
2501
01:36:45,760 --> 01:36:50,239
dot inner html and i'm going to append a
2502
01:36:50,239 --> 01:36:53,440
line break
2503
01:36:53,440 --> 01:36:55,040
okay so let's try this for now we'll
2504
01:36:55,040 --> 01:36:57,440
just print some numbers what about three
2505
01:36:57,440 --> 01:37:00,320
rows and nine columns
2506
01:37:00,320 --> 01:37:03,280
so we have three rows and nine columns
2507
01:37:03,280 --> 01:37:05,520
of our index j let's replace these
2508
01:37:05,520 --> 01:37:07,760
numbers with maybe a symbol so it's an
2509
01:37:07,760 --> 01:37:10,320
actual graphical rectangle
2510
01:37:10,320 --> 01:37:12,400
let's
2511
01:37:12,400 --> 01:37:13,920
symbol
2512
01:37:13,920 --> 01:37:17,520
window dot prompt
2513
01:37:17,920 --> 01:37:21,119
enter a symbol to use
2514
01:37:21,119 --> 01:37:25,119
and replace j with our symbol
2515
01:37:25,119 --> 01:37:26,960
enter a symbol to use what about a
2516
01:37:26,960 --> 01:37:29,920
dollar sign press ok
2517
01:37:29,920 --> 01:37:32,480
number of rows maybe
2518
01:37:32,480 --> 01:37:35,360
four rows five columns
2519
01:37:35,360 --> 01:37:37,840
and we have a graphical rectangle let's
2520
01:37:37,840 --> 01:37:40,960
try it again what about an asterisk
2521
01:37:40,960 --> 01:37:42,320
three rows
2522
01:37:42,320 --> 01:37:44,400
nine columns
2523
01:37:44,400 --> 01:37:45,280
sweet
2524
01:37:45,280 --> 01:37:47,280
so yeah everybody that's a nested loop
2525
01:37:47,280 --> 01:37:49,520
it's a loop inside of another loop when
2526
01:37:49,520 --> 01:37:50,960
you encounter them it's kind of
2527
01:37:50,960 --> 01:37:53,199
situational in this video we used a
2528
01:37:53,199 --> 01:37:55,440
nested loop to help us print a rectangle
2529
01:37:55,440 --> 01:37:57,440
so yeah that's a nested loop everybody
2530
01:37:57,440 --> 01:37:59,119
hey if you found this video helpful be
2531
01:37:59,119 --> 01:38:00,960
sure to help me out by smashing that
2532
01:38:00,960 --> 01:38:02,480
like button leave random comments down
2533
01:38:02,480 --> 01:38:04,000
below and subscribe if you'd like to
2534
01:38:04,000 --> 01:38:06,719
become a fellow bro
2535
01:38:06,719 --> 01:38:08,880
hey everybody we are moving on to
2536
01:38:08,880 --> 01:38:11,360
functions with a function you define
2537
01:38:11,360 --> 01:38:13,760
code once and then use it many times
2538
01:38:13,760 --> 01:38:16,080
whenever you need to perform some code
2539
01:38:16,080 --> 01:38:18,000
you simply call the function name in
2540
01:38:18,000 --> 01:38:20,400
this scenario i need to sing happy
2541
01:38:20,400 --> 01:38:22,960
birthday like three separate times so
2542
01:38:22,960 --> 01:38:24,880
without using a function i could write
2543
01:38:24,880 --> 01:38:28,440
something like this
2544
01:38:37,360 --> 01:38:39,840
this will be one iteration so when i run
2545
01:38:39,840 --> 01:38:42,320
this we sing happy birthday once if i
2546
01:38:42,320 --> 01:38:44,239
need to sing two additional times well i
2547
01:38:44,239 --> 01:38:46,639
would have to copy this code and paste
2548
01:38:46,639 --> 01:38:48,960
it two additional times to sing three
2549
01:38:48,960 --> 01:38:51,440
verses of my happy birthday song so we
2550
01:38:51,440 --> 01:38:52,960
don't like to repeat code if we don't
2551
01:38:52,960 --> 01:38:54,960
have to wouldn't it be nice if we could
2552
01:38:54,960 --> 01:38:56,800
write this code once and reuse it
2553
01:38:56,800 --> 01:38:58,560
whenever we need well we can do that
2554
01:38:58,560 --> 01:39:00,639
with a function so to create a function
2555
01:39:00,639 --> 01:39:02,239
we will type
2556
01:39:02,239 --> 01:39:04,800
function and then a unique function name
2557
01:39:04,800 --> 01:39:08,080
let's name this function happy birthday
2558
01:39:08,080 --> 01:39:09,679
add a set of parenthesis and a set of
2559
01:39:09,679 --> 01:39:11,760
curly braces any code you would like to
2560
01:39:11,760 --> 01:39:13,679
repeat simply place within the curly
2561
01:39:13,679 --> 01:39:16,080
braces
2562
01:39:16,080 --> 01:39:17,920
if i need to sing one verse of happy
2563
01:39:17,920 --> 01:39:20,560
birthday i take the function name and
2564
01:39:20,560 --> 01:39:22,719
add a set of parentheses this will
2565
01:39:22,719 --> 01:39:24,960
invoke otherwise known as call the
2566
01:39:24,960 --> 01:39:26,880
function so you type the function name
2567
01:39:26,880 --> 01:39:28,719
add a set of parentheses i like to think
2568
01:39:28,719 --> 01:39:30,480
of the parentheses as a pair of
2569
01:39:30,480 --> 01:39:32,239
telephones that are talking to each
2570
01:39:32,239 --> 01:39:34,159
other that's how you call a function if
2571
01:39:34,159 --> 01:39:36,159
i need to use this code again i can
2572
01:39:36,159 --> 01:39:38,320
simply call the function again so that's
2573
01:39:38,320 --> 01:39:40,960
2 times and three times
2574
01:39:40,960 --> 01:39:42,880
so we have accomplished our task of
2575
01:39:42,880 --> 01:39:45,040
singing three verses of my happy
2576
01:39:45,040 --> 01:39:46,639
birthday song what if we have some
2577
01:39:46,639 --> 01:39:47,760
variables
2578
01:39:47,760 --> 01:39:52,080
let user name equal your first name
2579
01:39:52,080 --> 01:39:55,280
and let age equal some age within my
2580
01:39:55,280 --> 01:39:57,520
happy birthday song i'm going to insert
2581
01:39:57,520 --> 01:39:59,360
my username
2582
01:39:59,360 --> 01:40:02,480
as well as my age
2583
01:40:03,280 --> 01:40:05,679
so within my happy birthday song
2584
01:40:05,679 --> 01:40:08,639
we have my username and my age functions
2585
01:40:08,639 --> 01:40:11,280
have access to global variables a global
2586
01:40:11,280 --> 01:40:13,520
variable is basically a variable that's
2587
01:40:13,520 --> 01:40:15,920
declared outside of any functions or any
2588
01:40:15,920 --> 01:40:18,400
set of curly braces for example we may
2589
01:40:18,400 --> 01:40:23,040
have a function to start our program
2590
01:40:23,040 --> 01:40:26,960
and we will place this code within here
2591
01:40:26,960 --> 01:40:29,040
i will call the start program function
2592
01:40:29,040 --> 01:40:30,800
to begin my program
2593
01:40:30,800 --> 01:40:32,960
but we have one issue uncaught reference
2594
01:40:32,960 --> 01:40:35,440
error username is not defined variables
2595
01:40:35,440 --> 01:40:37,199
when declared with the let keyword
2596
01:40:37,199 --> 01:40:39,119
aren't recognized outside of the
2597
01:40:39,119 --> 01:40:41,840
immediate set of curly braces so my
2598
01:40:41,840 --> 01:40:44,560
happy birthday function has no idea what
2599
01:40:44,560 --> 01:40:47,440
username or age is these variables only
2600
01:40:47,440 --> 01:40:49,840
exist within the immediate set of curly
2601
01:40:49,840 --> 01:40:52,400
braces what i could do is that when we
2602
01:40:52,400 --> 01:40:54,800
invoke the happy birthday function from
2603
01:40:54,800 --> 01:40:57,440
within the start program function we can
2604
01:40:57,440 --> 01:40:59,360
send some information to this happy
2605
01:40:59,360 --> 01:41:01,199
birthday function so that it will
2606
01:41:01,199 --> 01:41:03,840
recognize my username and my age to pass
2607
01:41:03,840 --> 01:41:06,400
some values or variables to a function
2608
01:41:06,400 --> 01:41:08,480
when you invoke that function within the
2609
01:41:08,480 --> 01:41:10,560
set of parentheses just list whatever
2610
01:41:10,560 --> 01:41:12,400
you would like to send over i would like
2611
01:41:12,400 --> 01:41:15,520
to send over my username and my age
2612
01:41:15,520 --> 01:41:18,239
these are known as arguments however you
2613
01:41:18,239 --> 01:41:21,360
need a matching set of parameters within
2614
01:41:21,360 --> 01:41:23,360
the declaration of my happy birthday
2615
01:41:23,360 --> 01:41:25,440
function i need a matching set of
2616
01:41:25,440 --> 01:41:26,800
parameters
2617
01:41:26,800 --> 01:41:28,800
you can just copy the arguments over and
2618
01:41:28,800 --> 01:41:30,639
then paste them within the happy
2619
01:41:30,639 --> 01:41:32,560
birthday function these are known as
2620
01:41:32,560 --> 01:41:34,719
parameters you need to send over a
2621
01:41:34,719 --> 01:41:37,040
username and an age so now this should
2622
01:41:37,040 --> 01:41:37,840
work
2623
01:41:37,840 --> 01:41:40,560
so here's my username and my age
2624
01:41:40,560 --> 01:41:42,400
if i invoke this function without
2625
01:41:42,400 --> 01:41:44,480
passing my arguments well then this
2626
01:41:44,480 --> 01:41:46,480
function doesn't know what my username
2627
01:41:46,480 --> 01:41:48,719
and age is and with your parameters they
2628
01:41:48,719 --> 01:41:50,400
don't necessarily need to be the same
2629
01:41:50,400 --> 01:41:52,480
name you can rename these values when
2630
01:41:52,480 --> 01:41:54,400
you receive them within the function
2631
01:41:54,400 --> 01:41:58,080
i'll rename username as a and age as b
2632
01:41:58,080 --> 01:42:01,199
and i'll change that here and here
2633
01:42:01,199 --> 01:42:02,880
this would also work but make sure you
2634
01:42:02,880 --> 01:42:04,880
get the orders of the arguments and the
2635
01:42:04,880 --> 01:42:06,639
parameters right so if i switch these
2636
01:42:06,639 --> 01:42:09,760
around happy birthday dear 21 you are
2637
01:42:09,760 --> 01:42:12,000
bro years old so the order of the
2638
01:42:12,000 --> 01:42:14,239
arguments and the parameters does matter
2639
01:42:14,239 --> 01:42:16,080
so yeah everybody that's a function you
2640
01:42:16,080 --> 01:42:18,239
define code once and then you can use it
2641
01:42:18,239 --> 01:42:21,040
many times to perform some subroutine of
2642
01:42:21,040 --> 01:42:23,360
code you just call the function name
2643
01:42:23,360 --> 01:42:25,119
this is useful because we don't have to
2644
01:42:25,119 --> 01:42:27,360
repeat code more than once if you find
2645
01:42:27,360 --> 01:42:28,719
that you're going to repeat code more
2646
01:42:28,719 --> 01:42:30,639
than once you could write it within a
2647
01:42:30,639 --> 01:42:32,719
function so yeah those are functions
2648
01:42:32,719 --> 01:42:34,239
everybody in the next video we'll
2649
01:42:34,239 --> 01:42:36,239
discuss return statements hey if this
2650
01:42:36,239 --> 01:42:37,920
video helped you out you can help me out
2651
01:42:37,920 --> 01:42:39,520
by smashing that like button leave
2652
01:42:39,520 --> 01:42:41,360
random comments down below and subscribe
2653
01:42:41,360 --> 01:42:44,719
if you'd like to become a fellow bro
2654
01:42:44,719 --> 01:42:46,960
well alright everybody i need to discuss
2655
01:42:46,960 --> 01:42:48,639
the return statement the return
2656
01:42:48,639 --> 01:42:50,880
statement is used within a function to
2657
01:42:50,880 --> 01:42:53,360
return a value back to the spot in which
2658
01:42:53,360 --> 01:42:55,280
you invoked a function when you call a
2659
01:42:55,280 --> 01:42:57,600
function you can return some information
2660
01:42:57,600 --> 01:42:58,880
to demonstrate this we're going to
2661
01:42:58,880 --> 01:43:01,040
create a program to calculate the area
2662
01:43:01,040 --> 01:43:03,600
of a rectangle using a function and then
2663
01:43:03,600 --> 01:43:05,520
return the value so to create this
2664
01:43:05,520 --> 01:43:07,760
program let's declare a few variables
2665
01:43:07,760 --> 01:43:09,679
let area
2666
01:43:09,679 --> 01:43:10,480
let
2667
01:43:10,480 --> 01:43:11,440
with
2668
01:43:11,440 --> 01:43:14,320
and let height we'll accept some user
2669
01:43:14,320 --> 01:43:15,840
input
2670
01:43:15,840 --> 01:43:16,639
width
2671
01:43:16,639 --> 01:43:18,560
equals window
2672
01:43:18,560 --> 01:43:19,520
dot
2673
01:43:19,520 --> 01:43:21,600
prompt
2674
01:43:21,600 --> 01:43:22,719
enter
2675
01:43:22,719 --> 01:43:25,920
width copy this line paste it change
2676
01:43:25,920 --> 01:43:28,239
width to height
2677
01:43:28,239 --> 01:43:30,719
here and here at the bottom of our
2678
01:43:30,719 --> 01:43:32,480
program let's create a function to
2679
01:43:32,480 --> 01:43:35,119
calculate the area of a rectangle so to
2680
01:43:35,119 --> 01:43:37,440
create a function type function
2681
01:43:37,440 --> 01:43:39,119
let's name this get
2682
01:43:39,119 --> 01:43:42,000
area parentheses curly braces
2683
01:43:42,000 --> 01:43:44,159
when we use this function to calculate
2684
01:43:44,159 --> 01:43:46,000
the area of a rectangle we'll need to
2685
01:43:46,000 --> 01:43:48,480
set up some parameters in order to do so
2686
01:43:48,480 --> 01:43:50,639
we'll need a width
2687
01:43:50,639 --> 01:43:52,639
and a height when we invoke this
2688
01:43:52,639 --> 01:43:54,880
function we will pass these variables as
2689
01:43:54,880 --> 01:43:56,960
arguments when we assign our area
2690
01:43:56,960 --> 01:43:59,920
variable let's set this equal to then
2691
01:43:59,920 --> 01:44:01,520
invoke the get
2692
01:44:01,520 --> 01:44:03,280
area function
2693
01:44:03,280 --> 01:44:05,440
but we need to pass in a matching set of
2694
01:44:05,440 --> 01:44:06,639
arguments
2695
01:44:06,639 --> 01:44:08,480
our width
2696
01:44:08,480 --> 01:44:11,280
and our height
2697
01:44:11,520 --> 01:44:13,840
let's fill in this get area function i
2698
01:44:13,840 --> 01:44:16,480
will declare a temporary variable let
2699
01:44:16,480 --> 01:44:17,520
result
2700
01:44:17,520 --> 01:44:19,440
equals and to calculate the area of a
2701
01:44:19,440 --> 01:44:22,960
rectangle it's simply width times height
2702
01:44:22,960 --> 01:44:25,040
if i need to send a value back to the
2703
01:44:25,040 --> 01:44:27,119
spot in which we called a function we
2704
01:44:27,119 --> 01:44:29,199
can use the return keyword i would like
2705
01:44:29,199 --> 01:44:30,960
to return the result
2706
01:44:30,960 --> 01:44:32,639
then at the end of our program but
2707
01:44:32,639 --> 01:44:34,480
before our function let's display the
2708
01:44:34,480 --> 01:44:36,080
result just to be sure that it works
2709
01:44:36,080 --> 01:44:37,320
fine
2710
01:44:37,320 --> 01:44:38,960
console.log
2711
01:44:38,960 --> 01:44:40,639
the area
2712
01:44:40,639 --> 01:44:42,800
is
2713
01:44:42,800 --> 01:44:45,600
area okay let's try it okay enter the
2714
01:44:45,600 --> 01:44:46,560
width
2715
01:44:46,560 --> 01:44:49,520
maybe five height six the area is thirty
2716
01:44:49,520 --> 01:44:51,520
let's try it one more time how about the
2717
01:44:51,520 --> 01:44:54,239
width is six and the height is nine the
2718
01:44:54,239 --> 01:44:56,960
area is 54. so when we returned the
2719
01:44:56,960 --> 01:44:59,600
result once this function was completed
2720
01:44:59,600 --> 01:45:01,440
just imagine that we're replacing this
2721
01:45:01,440 --> 01:45:04,159
function call with 54. we're returning a
2722
01:45:04,159 --> 01:45:06,080
value back to the spot we invoked a
2723
01:45:06,080 --> 01:45:09,040
function we assigned 54 to our variable
2724
01:45:09,040 --> 01:45:10,960
area you could shorten this code as well
2725
01:45:10,960 --> 01:45:12,880
you don't necessarily need a temporary
2726
01:45:12,880 --> 01:45:14,960
variable we could eliminate this line
2727
01:45:14,960 --> 01:45:18,080
and return width times height and that
2728
01:45:18,080 --> 01:45:22,159
would do the same thing 6 times 9 is 54.
2729
01:45:22,159 --> 01:45:23,840
so yeah everybody that is the return
2730
01:45:23,840 --> 01:45:26,560
statement it returns a value back to the
2731
01:45:26,560 --> 01:45:28,480
place where you invoked a function so
2732
01:45:28,480 --> 01:45:30,320
yeah that's the return statement hey if
2733
01:45:30,320 --> 01:45:31,920
you found this video helpful be sure to
2734
01:45:31,920 --> 01:45:33,520
smash that like button leave a random
2735
01:45:33,520 --> 01:45:35,119
comment down below and subscribe if
2736
01:45:35,119 --> 01:45:38,320
you'd like to become a fellow bro
2737
01:45:38,320 --> 01:45:40,000
hey guys in this video i'm going to
2738
01:45:40,000 --> 01:45:42,400
explain the ternary operator it's a
2739
01:45:42,400 --> 01:45:44,639
shortcut for an if-else statement it's
2740
01:45:44,639 --> 01:45:46,560
represented by a question mark there's
2741
01:45:46,560 --> 01:45:48,719
three parts we write some condition
2742
01:45:48,719 --> 01:45:50,320
follow the condition with a question
2743
01:45:50,320 --> 01:45:52,320
mark as if we're asking a question if
2744
01:45:52,320 --> 01:45:54,400
that condition is true we perform an
2745
01:45:54,400 --> 01:45:56,639
expression if that condition is false we
2746
01:45:56,639 --> 01:45:58,320
perform a different expression and
2747
01:45:58,320 --> 01:46:00,239
separate each expression with a colon
2748
01:46:00,239 --> 01:46:01,840
here's an example let's create a
2749
01:46:01,840 --> 01:46:04,000
function that will check a user's age
2750
01:46:04,000 --> 01:46:05,920
initially we'll use an if else statement
2751
01:46:05,920 --> 01:46:07,360
and then later write it using the
2752
01:46:07,360 --> 01:46:09,679
ternary operator maybe we have a boolean
2753
01:46:09,679 --> 01:46:13,119
variable let adult and we will invoke a
2754
01:46:13,119 --> 01:46:15,360
check age function and pass in some age
2755
01:46:15,360 --> 01:46:17,119
let's define this function
2756
01:46:17,119 --> 01:46:18,239
function
2757
01:46:18,239 --> 01:46:19,119
check
2758
01:46:19,119 --> 01:46:20,080
age
2759
01:46:20,080 --> 01:46:22,560
there is one argument let's name this
2760
01:46:22,560 --> 01:46:24,960
age i need to check to see if age is
2761
01:46:24,960 --> 01:46:26,960
greater than or equal to 18 i could
2762
01:46:26,960 --> 01:46:28,080
write
2763
01:46:28,080 --> 01:46:30,000
with an if statement
2764
01:46:30,000 --> 01:46:33,119
if age is greater than or equal to 18
2765
01:46:33,119 --> 01:46:36,960
let's return a boolean value of true
2766
01:46:36,960 --> 01:46:38,320
else
2767
01:46:38,320 --> 01:46:39,520
return
2768
01:46:39,520 --> 01:46:42,000
false let's display whatever our adult
2769
01:46:42,000 --> 01:46:43,840
boolean variable is
2770
01:46:43,840 --> 01:46:46,840
console.log
2771
01:46:47,119 --> 01:46:49,600
adult i passed the number 21 as an
2772
01:46:49,600 --> 01:46:52,719
argument check age returns true if i was
2773
01:46:52,719 --> 01:46:55,199
12 well then check age returns false
2774
01:46:55,199 --> 01:46:57,040
there's another way of writing this and
2775
01:46:57,040 --> 01:46:58,639
i think it's a lot easier lot less
2776
01:46:58,639 --> 01:47:01,119
syntax this function will return a value
2777
01:47:01,119 --> 01:47:03,199
so i'm going to precede our tenary
2778
01:47:03,199 --> 01:47:05,360
operator with the return statement to
2779
01:47:05,360 --> 01:47:07,520
use the ternary operator we write a
2780
01:47:07,520 --> 01:47:09,199
condition followed with a question mark
2781
01:47:09,199 --> 01:47:11,119
as if we're asking a question
2782
01:47:11,119 --> 01:47:14,239
age is greater than or equal to 18
2783
01:47:14,239 --> 01:47:16,880
question mark second is an expression if
2784
01:47:16,880 --> 01:47:18,960
that condition is true we're returning
2785
01:47:18,960 --> 01:47:21,600
something let's return true and the
2786
01:47:21,600 --> 01:47:23,360
second expression if that condition is
2787
01:47:23,360 --> 01:47:25,600
false after a colon write some
2788
01:47:25,600 --> 01:47:27,600
expression or some value
2789
01:47:27,600 --> 01:47:29,440
false so this will do the same thing
2790
01:47:29,440 --> 01:47:31,600
currently i'm 12 this returns false if
2791
01:47:31,600 --> 01:47:34,320
i'm 21 well then it's true we have three
2792
01:47:34,320 --> 01:47:37,199
parts a condition with a question mark
2793
01:47:37,199 --> 01:47:39,760
an expression if that condition is true
2794
01:47:39,760 --> 01:47:41,679
colon and expression if that condition
2795
01:47:41,679 --> 01:47:44,320
is false here's another example maybe
2796
01:47:44,320 --> 01:47:45,840
we're playing a game and we have to
2797
01:47:45,840 --> 01:47:48,080
check to see if somebody won the game
2798
01:47:48,080 --> 01:47:50,719
i'll declare a check winner function
2799
01:47:50,719 --> 01:47:51,840
function
2800
01:47:51,840 --> 01:47:52,639
check
2801
01:47:52,639 --> 01:47:53,840
winner
2802
01:47:53,840 --> 01:47:56,400
let's declare a parameter of when this
2803
01:47:56,400 --> 01:47:58,719
will be a boolean value we're not
2804
01:47:58,719 --> 01:48:00,239
returning anything we don't need that
2805
01:48:00,239 --> 01:48:02,639
return keyword if we're examining a
2806
01:48:02,639 --> 01:48:04,960
boolean variable or parameter all we
2807
01:48:04,960 --> 01:48:07,119
need to do is type the name win question
2808
01:48:07,119 --> 01:48:10,480
mark if when is true what do we do
2809
01:48:10,480 --> 01:48:12,560
let's display a message this time
2810
01:48:12,560 --> 01:48:14,400
console.log
2811
01:48:14,400 --> 01:48:15,600
you win
2812
01:48:15,600 --> 01:48:18,320
colon then some expression if win is
2813
01:48:18,320 --> 01:48:20,239
false
2814
01:48:20,239 --> 01:48:21,920
you lose
2815
01:48:21,920 --> 01:48:24,320
then let's invoke this function
2816
01:48:24,320 --> 01:48:25,199
check
2817
01:48:25,199 --> 01:48:26,320
winner
2818
01:48:26,320 --> 01:48:28,719
then we can pass in a boolean value or
2819
01:48:28,719 --> 01:48:30,080
variable
2820
01:48:30,080 --> 01:48:31,760
like true
2821
01:48:31,760 --> 01:48:32,960
you win
2822
01:48:32,960 --> 01:48:34,400
or false
2823
01:48:34,400 --> 01:48:37,280
you lose so that's the ternary operator
2824
01:48:37,280 --> 01:48:40,000
it's a shortcut for an if else statement
2825
01:48:40,000 --> 01:48:41,760
you write some condition followed by a
2826
01:48:41,760 --> 01:48:44,159
question mark if that condition is true
2827
01:48:44,159 --> 01:48:46,400
you do something if not you do something
2828
01:48:46,400 --> 01:48:48,960
else it's a shortcut basically so yeah
2829
01:48:48,960 --> 01:48:51,119
that's the ternary operator if you would
2830
01:48:51,119 --> 01:48:53,040
like a copy of this code i'll post it to
2831
01:48:53,040 --> 01:48:54,960
the comment section down below and well
2832
01:48:54,960 --> 01:48:57,199
yeah that's the ternary operator in
2833
01:48:57,199 --> 01:48:59,840
javascript
2834
01:48:59,840 --> 01:49:01,440
guys in this video i'm going to explain
2835
01:49:01,440 --> 01:49:03,440
some of the differences between the let
2836
01:49:03,440 --> 01:49:05,040
and var keywords when declaring
2837
01:49:05,040 --> 01:49:07,520
variables variable scope is where a
2838
01:49:07,520 --> 01:49:09,679
variable is accessible any variable
2839
01:49:09,679 --> 01:49:11,679
declared with the let keyword is limited
2840
01:49:11,679 --> 01:49:13,599
to a block scope it doesn't exist
2841
01:49:13,599 --> 01:49:16,159
outside any sets of curly braces any
2842
01:49:16,159 --> 01:49:18,239
variable declared with the var keyword
2843
01:49:18,239 --> 01:49:20,320
is limited to a function here's an
2844
01:49:20,320 --> 01:49:22,719
example let's create a for loop
2845
01:49:22,719 --> 01:49:24,800
we'll count up to three
2846
01:49:24,800 --> 01:49:27,679
we will declare a counter of i set this
2847
01:49:27,679 --> 01:49:30,000
equal to one will continue as long as i
2848
01:49:30,000 --> 01:49:32,320
is less than or equal to three then
2849
01:49:32,320 --> 01:49:35,119
increment i by one so within my for loop
2850
01:49:35,119 --> 01:49:37,119
i will display whatever value i is
2851
01:49:37,119 --> 01:49:38,880
console.log i
2852
01:49:38,880 --> 01:49:41,280
our results are one two three what if i
2853
01:49:41,280 --> 01:49:43,520
attempt to access this variable i
2854
01:49:43,520 --> 01:49:45,440
outside of this for loop
2855
01:49:45,440 --> 01:49:48,239
console.log
2856
01:49:48,239 --> 01:49:50,560
hi what would happen exactly we have an
2857
01:49:50,560 --> 01:49:52,719
uncaught reference error i is not
2858
01:49:52,719 --> 01:49:55,119
defined that's because any variable
2859
01:49:55,119 --> 01:49:57,440
declared with the let keyword is limited
2860
01:49:57,440 --> 01:49:59,679
to a block scope it doesn't exist
2861
01:49:59,679 --> 01:50:01,760
outside of the set of curly braces
2862
01:50:01,760 --> 01:50:03,360
that's why my interpreter doesn't know
2863
01:50:03,360 --> 01:50:05,840
what the heck this is if we instead used
2864
01:50:05,840 --> 01:50:07,920
the var keyword to declare a variable
2865
01:50:07,920 --> 01:50:10,400
well then this variable i can exist
2866
01:50:10,400 --> 01:50:12,159
outside of curly braces and that could
2867
01:50:12,159 --> 01:50:14,159
be a problem so now if i attempt to
2868
01:50:14,159 --> 01:50:17,040
display whatever i is it's now 4. so if
2869
01:50:17,040 --> 01:50:19,280
i would like to reuse this counter i but
2870
01:50:19,280 --> 01:50:21,360
we declared it with var since it already
2871
01:50:21,360 --> 01:50:23,199
has a value it's going to potentially
2872
01:50:23,199 --> 01:50:25,360
mess with my program and cause problems
2873
01:50:25,360 --> 01:50:27,199
however any variable declared with the
2874
01:50:27,199 --> 01:50:29,599
var keyword doesn't exist outside of any
2875
01:50:29,599 --> 01:50:31,440
functions if it's within one let's
2876
01:50:31,440 --> 01:50:32,639
create a function
2877
01:50:32,639 --> 01:50:34,320
function do
2878
01:50:34,320 --> 01:50:35,360
something
2879
01:50:35,360 --> 01:50:37,280
i don't really care what it does
2880
01:50:37,280 --> 01:50:39,440
so let's copy this for loop place it
2881
01:50:39,440 --> 01:50:41,760
within the function and invoke this
2882
01:50:41,760 --> 01:50:43,440
function do
2883
01:50:43,440 --> 01:50:44,719
something
2884
01:50:44,719 --> 01:50:46,800
now when i display i after it's been
2885
01:50:46,800 --> 01:50:48,960
declared with the var keyword we have
2886
01:50:48,960 --> 01:50:51,360
that uncaught reference error i is not
2887
01:50:51,360 --> 01:50:53,679
defined a variable declared with the var
2888
01:50:53,679 --> 01:50:56,639
keyword can escape a set of curly braces
2889
01:50:56,639 --> 01:50:58,480
but it can't escape a function if it's
2890
01:50:58,480 --> 01:51:00,239
contained within one and that's where we
2891
01:51:00,239 --> 01:51:02,480
need to discuss global variables a
2892
01:51:02,480 --> 01:51:04,960
global variable is declared outside of
2893
01:51:04,960 --> 01:51:07,440
any functions so we have our function if
2894
01:51:07,440 --> 01:51:09,599
i was to declare let
2895
01:51:09,599 --> 01:51:11,599
name equal
2896
01:51:11,599 --> 01:51:14,320
some name since this variable is outside
2897
01:51:14,320 --> 01:51:16,080
of any functions it's considered a
2898
01:51:16,080 --> 01:51:18,400
global variable my entire program has
2899
01:51:18,400 --> 01:51:20,639
access to it and recognizes what it is
2900
01:51:20,639 --> 01:51:22,400
if you use the var keyword when
2901
01:51:22,400 --> 01:51:24,639
declaring a global variable that can be
2902
01:51:24,639 --> 01:51:26,960
problematic because if a variable
2903
01:51:26,960 --> 01:51:30,000
declared with var is global it can and
2904
01:51:30,000 --> 01:51:31,760
will change the browser's window
2905
01:51:31,760 --> 01:51:33,840
properties here's an example for a
2906
01:51:33,840 --> 01:51:35,840
demonstration within my console i'm
2907
01:51:35,840 --> 01:51:37,920
going to access my browser's window
2908
01:51:37,920 --> 01:51:40,960
object i will type window
2909
01:51:40,960 --> 01:51:43,440
select it hit enter we haven't discussed
2910
01:51:43,440 --> 01:51:45,360
objects yet we will in a future video
2911
01:51:45,360 --> 01:51:47,760
but windows have properties and my
2912
01:51:47,760 --> 01:51:50,320
browser window has one property of name
2913
01:51:50,320 --> 01:51:52,080
and currently it's just an empty string
2914
01:51:52,080 --> 01:51:54,320
let's say that i'm a beginner programmer
2915
01:51:54,320 --> 01:51:55,840
and i would like to declare a name
2916
01:51:55,840 --> 01:51:58,400
variable to hold a username
2917
01:51:58,400 --> 01:52:00,960
so if i was to use the var keyword and i
2918
01:52:00,960 --> 01:52:03,280
create a variable name and set the
2919
01:52:03,280 --> 01:52:05,440
sequel to whatever my name is
2920
01:52:05,440 --> 01:52:07,840
behind the scenes i will unintentionally
2921
01:52:07,840 --> 01:52:10,000
change this window property of name so
2922
01:52:10,000 --> 01:52:12,000
i'm going to save and run this let's
2923
01:52:12,000 --> 01:52:15,119
access our window again
2924
01:52:15,199 --> 01:52:18,679
and let's find name there so i have
2925
01:52:18,679 --> 01:52:21,040
unintentionally changed this windows
2926
01:52:21,040 --> 01:52:23,040
property of name and i didn't even
2927
01:52:23,040 --> 01:52:24,960
realize it and that could happen to any
2928
01:52:24,960 --> 01:52:26,480
of these properties and that could of
2929
01:52:26,480 --> 01:52:28,560
course possibly mess with your program
2930
01:52:28,560 --> 01:52:30,480
and cause problems now this time i'm
2931
01:52:30,480 --> 01:52:33,119
going to use the let keyword and you may
2932
01:52:33,119 --> 01:52:35,119
need to open this again with live server
2933
01:52:35,119 --> 01:52:37,920
this time i'm using the let keyword
2934
01:52:37,920 --> 01:52:40,000
i will type window again
2935
01:52:40,000 --> 01:52:42,719
select it hit enter and let's find that
2936
01:52:42,719 --> 01:52:45,360
name property again there by using the
2937
01:52:45,360 --> 01:52:47,280
let keyword i was able to declare this
2938
01:52:47,280 --> 01:52:49,360
variable name without changing this
2939
01:52:49,360 --> 01:52:51,760
windows property of name so yeah those
2940
01:52:51,760 --> 01:52:53,599
are some of the differences between the
2941
01:52:53,599 --> 01:52:55,840
var and the let keywords a variable
2942
01:52:55,840 --> 01:52:58,080
declared with let is limited to a block
2943
01:52:58,080 --> 01:53:00,560
scope a variable declared with var is
2944
01:53:00,560 --> 01:53:02,639
limited to a function scope if you
2945
01:53:02,639 --> 01:53:05,199
declare a global variable with var it
2946
01:53:05,199 --> 01:53:07,119
can potentially mess with your browser's
2947
01:53:07,119 --> 01:53:09,360
window properties let will avoid that
2948
01:53:09,360 --> 01:53:11,520
problem so it's considered good practice
2949
01:53:11,520 --> 01:53:13,920
to use let over var but you should still
2950
01:53:13,920 --> 01:53:16,239
know that var exists so yeah everybody
2951
01:53:16,239 --> 01:53:17,440
those are a few of the differences
2952
01:53:17,440 --> 01:53:19,440
between var and let hey if you found
2953
01:53:19,440 --> 01:53:21,199
this video helpful please be sure to
2954
01:53:21,199 --> 01:53:22,800
smash that like button leave random
2955
01:53:22,800 --> 01:53:24,400
comments down below and subscribe if
2956
01:53:24,400 --> 01:53:27,199
you'd like to become a fellow bro
2957
01:53:27,199 --> 01:53:29,119
hey guys in this topic i'm going to be
2958
01:53:29,119 --> 01:53:31,119
discussing template literals they're
2959
01:53:31,119 --> 01:53:33,440
delimited with backticks instead of
2960
01:53:33,440 --> 01:53:35,599
double or single quotes they allow for
2961
01:53:35,599 --> 01:53:37,840
embedded variables and expressions maybe
2962
01:53:37,840 --> 01:53:39,440
we're working on a page for an online
2963
01:53:39,440 --> 01:53:42,239
store let's create three variables let
2964
01:53:42,239 --> 01:53:45,280
username make up some username
2965
01:53:45,280 --> 01:53:47,760
let items for the number of items that
2966
01:53:47,760 --> 01:53:50,480
we have our cart has maybe three items
2967
01:53:50,480 --> 01:53:52,000
in it let
2968
01:53:52,000 --> 01:53:53,119
total
2969
01:53:53,119 --> 01:53:56,639
and make up some total price 75
2970
01:53:56,639 --> 01:53:58,320
okay so normally if i would like to
2971
01:53:58,320 --> 01:54:00,159
display these variables along with some
2972
01:54:00,159 --> 01:54:02,719
strings with console.log i would write
2973
01:54:02,719 --> 01:54:04,639
something like this
2974
01:54:04,639 --> 01:54:06,320
hello
2975
01:54:06,320 --> 01:54:09,320
username
2976
01:54:10,880 --> 01:54:13,440
you have
2977
01:54:13,440 --> 01:54:15,920
items
2978
01:54:16,239 --> 01:54:17,679
items
2979
01:54:17,679 --> 01:54:22,040
in your cart
2980
01:54:22,239 --> 01:54:25,440
your total is
2981
01:54:25,440 --> 01:54:27,280
dollar sign
2982
01:54:27,280 --> 01:54:29,840
total hello bro you have three items in
2983
01:54:29,840 --> 01:54:32,800
your cart your total is 75 dollars
2984
01:54:32,800 --> 01:54:34,639
another way of writing this which i like
2985
01:54:34,639 --> 01:54:37,440
to use is to use template literals in
2986
01:54:37,440 --> 01:54:39,360
place of singular double quotes for
2987
01:54:39,360 --> 01:54:42,159
strings we will use these backticks so
2988
01:54:42,159 --> 01:54:43,760
i'm going to copy these three lines of
2989
01:54:43,760 --> 01:54:47,880
code turn these into comments
2990
01:54:51,040 --> 01:54:53,280
so when using backticks we can embed a
2991
01:54:53,280 --> 01:54:55,760
variable or expression within a string
2992
01:54:55,760 --> 01:54:58,159
for this first string let's type
2993
01:54:58,159 --> 01:55:00,639
hello and to embed a variable or
2994
01:55:00,639 --> 01:55:03,360
expression use a dollar sign followed by
2995
01:55:03,360 --> 01:55:05,119
a set of curly braces
2996
01:55:05,119 --> 01:55:07,520
and we can embed a value variable or
2997
01:55:07,520 --> 01:55:09,840
expression i would like to embed my
2998
01:55:09,840 --> 01:55:11,280
username
2999
01:55:11,280 --> 01:55:14,400
so currently this is what we have hello
3000
01:55:14,400 --> 01:55:17,440
your username let's try this again
3001
01:55:17,440 --> 01:55:21,119
you have i would like to embed items
3002
01:55:21,119 --> 01:55:24,000
so that's dollar sign curly braces type
3003
01:55:24,000 --> 01:55:25,280
the variable name
3004
01:55:25,280 --> 01:55:27,280
items
3005
01:55:27,280 --> 01:55:28,880
the word items
3006
01:55:28,880 --> 01:55:31,440
in your cart
3007
01:55:31,440 --> 01:55:35,040
you have three items in your cart
3008
01:55:35,040 --> 01:55:38,159
your total is
3009
01:55:38,159 --> 01:55:41,520
dollar sign curly braces total
3010
01:55:41,520 --> 01:55:44,080
so if you need that dollar sign i could
3011
01:55:44,080 --> 01:55:45,599
just precede this with another dollar
3012
01:55:45,599 --> 01:55:46,480
sign
3013
01:55:46,480 --> 01:55:48,880
your total is 75
3014
01:55:48,880 --> 01:55:50,880
i find using template literals a lot
3015
01:55:50,880 --> 01:55:52,800
easier to work with here's a scenario
3016
01:55:52,800 --> 01:55:54,639
what if we would like to display one
3017
01:55:54,639 --> 01:55:57,840
very long string here's what we can do
3018
01:55:57,840 --> 01:56:00,400
let's assign a new variable let
3019
01:56:00,400 --> 01:56:02,400
text equals
3020
01:56:02,400 --> 01:56:04,159
and i will copy what we've placed within
3021
01:56:04,159 --> 01:56:07,040
these log statements
3022
01:56:10,000 --> 01:56:11,920
make sure it begins and ends with that
3023
01:56:11,920 --> 01:56:13,760
backtick character
3024
01:56:13,760 --> 01:56:16,000
so we have assigned this entire string
3025
01:56:16,000 --> 01:56:18,639
template to a variable text
3026
01:56:18,639 --> 01:56:20,639
if i need to display all of this
3027
01:56:20,639 --> 01:56:23,440
i can simply just use this text variable
3028
01:56:23,440 --> 01:56:26,080
console.log text
3029
01:56:26,080 --> 01:56:27,760
hello bro you have three items in your
3030
01:56:27,760 --> 01:56:30,320
cart your total is 75
3031
01:56:30,320 --> 01:56:32,159
where this is really helpful is if we
3032
01:56:32,159 --> 01:56:34,560
need to update an html element with some
3033
01:56:34,560 --> 01:56:35,440
text
3034
01:56:35,440 --> 01:56:37,599
especially if it takes up several lines
3035
01:56:37,599 --> 01:56:39,840
let's head to our html file i'll create
3036
01:56:39,840 --> 01:56:41,840
a new label
3037
01:56:41,840 --> 01:56:43,040
label
3038
01:56:43,040 --> 01:56:44,639
add a closing tag
3039
01:56:44,639 --> 01:56:46,800
i'll give this an id of
3040
01:56:46,800 --> 01:56:47,679
my
3041
01:56:47,679 --> 01:56:48,960
label
3042
01:56:48,960 --> 01:56:50,400
in place of displaying this to my
3043
01:56:50,400 --> 01:56:53,280
console i'm going to update this label
3044
01:56:53,280 --> 01:56:54,880
to select that label i will type
3045
01:56:54,880 --> 01:56:56,480
document dot
3046
01:56:56,480 --> 01:56:57,280
get
3047
01:56:57,280 --> 01:57:02,000
element by id the id was my label
3048
01:57:02,000 --> 01:57:03,840
change the inner
3049
01:57:03,840 --> 01:57:08,480
html equal to text
3050
01:57:08,560 --> 01:57:10,560
and this is what we have currently
3051
01:57:10,560 --> 01:57:13,360
except i'm going to add some line breaks
3052
01:57:13,360 --> 01:57:16,480
i'll add that here
3053
01:57:16,960 --> 01:57:20,400
here and maybe here
3054
01:57:21,520 --> 01:57:22,880
so there you go
3055
01:57:22,880 --> 01:57:24,639
hello bro you have three items in your
3056
01:57:24,639 --> 01:57:27,199
cart your total is 75 dollars by using
3057
01:57:27,199 --> 01:57:29,360
template literals they allow for
3058
01:57:29,360 --> 01:57:31,760
embedded variables and expressions it's
3059
01:57:31,760 --> 01:57:34,159
a more flexible way to write some output
3060
01:57:34,159 --> 01:57:35,440
i'll be using these a lot more in the
3061
01:57:35,440 --> 01:57:37,840
future because well i like them so yeah
3062
01:57:37,840 --> 01:57:40,000
those are template literals everybody if
3063
01:57:40,000 --> 01:57:41,520
you would like a copy of this code i'll
3064
01:57:41,520 --> 01:57:42,960
post this in the comment section down
3065
01:57:42,960 --> 01:57:45,360
below and well yeah those are template
3066
01:57:45,360 --> 01:57:48,639
literals in javascript
3067
01:57:48,639 --> 01:57:50,480
hey what's going on everybody so in this
3068
01:57:50,480 --> 01:57:52,320
video i'm going to explain the two
3069
01:57:52,320 --> 01:57:54,960
locale string method of numbers what
3070
01:57:54,960 --> 01:57:57,360
this does is that it returns a string
3071
01:57:57,360 --> 01:58:00,080
with a language sensitive representation
3072
01:58:00,080 --> 01:58:02,080
of this number it's fantastic if you
3073
01:58:02,080 --> 01:58:04,719
need to format a number as some currency
3074
01:58:04,719 --> 01:58:06,960
we have a number with numbers there's a
3075
01:58:06,960 --> 01:58:09,840
built-in method named to locale string
3076
01:58:09,840 --> 01:58:12,880
and a few arguments the first is locale
3077
01:58:12,880 --> 01:58:15,280
locale specifies the language if you
3078
01:58:15,280 --> 01:58:17,360
pass in undefined well then you'll use
3079
01:58:17,360 --> 01:58:20,080
the default set in your browser next is
3080
01:58:20,080 --> 01:58:22,400
options options is an object with
3081
01:58:22,400 --> 01:58:24,719
formatting options we can specify if
3082
01:58:24,719 --> 01:58:26,960
this number is currency a unit of
3083
01:58:26,960 --> 01:58:29,280
measurement a percent let's begin let's
3084
01:58:29,280 --> 01:58:32,480
create a number let my num
3085
01:58:32,480 --> 01:58:34,960
equals make up a number i'll just pick
3086
01:58:34,960 --> 01:58:37,920
one two three four five six point seven
3087
01:58:37,920 --> 01:58:39,199
eight nine
3088
01:58:39,199 --> 01:58:41,920
so i'm going to reassign my num
3089
01:58:41,920 --> 01:58:45,119
equals my num dot to
3090
01:58:45,119 --> 01:58:46,320
locale
3091
01:58:46,320 --> 01:58:47,440
string
3092
01:58:47,440 --> 01:58:50,080
the first argument is locale this is a
3093
01:58:50,080 --> 01:58:52,480
string then we'll pass in a language
3094
01:58:52,480 --> 01:58:55,599
united states english that would be en
3095
01:58:55,599 --> 01:58:56,800
us
3096
01:58:56,800 --> 01:58:58,800
then let's display my num
3097
01:58:58,800 --> 01:59:01,800
console.log
3098
01:59:01,840 --> 01:59:02,719
num
3099
01:59:02,719 --> 01:59:04,800
so when i run this this number is
3100
01:59:04,800 --> 01:59:06,719
formatted with a comma for every
3101
01:59:06,719 --> 01:59:10,639
thousands place so that is us english
3102
01:59:10,639 --> 01:59:13,840
okay next let's try hindi
3103
01:59:13,840 --> 01:59:17,679
so let's copy this paste it hindi is
3104
01:59:17,679 --> 01:59:20,000
h i dash
3105
01:59:20,000 --> 01:59:21,599
i n
3106
01:59:21,599 --> 01:59:23,440
so there's a different number formatting
3107
01:59:23,440 --> 01:59:24,800
system
3108
01:59:24,800 --> 01:59:27,199
that is hindi let's try one more what
3109
01:59:27,199 --> 01:59:30,000
about standard german
3110
01:59:30,000 --> 01:59:32,880
standard german is d e
3111
01:59:32,880 --> 01:59:33,760
dash
3112
01:59:33,760 --> 01:59:35,679
d e
3113
01:59:35,679 --> 01:59:37,360
there's a different number formatting
3114
01:59:37,360 --> 01:59:38,880
system
3115
01:59:38,880 --> 01:59:40,800
standard
3116
01:59:40,800 --> 01:59:42,080
german
3117
01:59:42,080 --> 01:59:43,599
whatever language you need you just have
3118
01:59:43,599 --> 01:59:45,679
to look up the format code then pass
3119
01:59:45,679 --> 01:59:48,239
that as a string but those are a few
3120
01:59:48,239 --> 01:59:50,480
this time let's format some currency we
3121
01:59:50,480 --> 01:59:52,639
would set that within options
3122
01:59:52,639 --> 01:59:55,360
i'll pick us english
3123
01:59:55,360 --> 01:59:57,040
to add some options we'll use curly
3124
01:59:57,040 --> 01:59:59,599
braces
3125
01:59:59,599 --> 02:00:01,199
set the style
3126
02:00:01,199 --> 02:00:02,239
to
3127
02:00:02,239 --> 02:00:04,800
currency
3128
02:00:05,520 --> 02:00:07,520
then currency
3129
02:00:07,520 --> 02:00:09,920
colon
3130
02:00:10,080 --> 02:00:11,440
then a unit
3131
02:00:11,440 --> 02:00:13,599
usd is us dollars
3132
02:00:13,599 --> 02:00:16,400
so this number is now in us dollars and
3133
02:00:16,400 --> 02:00:18,000
you can see too that this number is
3134
02:00:18,000 --> 02:00:20,320
rounded to dollars and cents
3135
02:00:20,320 --> 02:00:22,800
originally we had three decimal places
3136
02:00:22,800 --> 02:00:24,320
okay let's try another
3137
02:00:24,320 --> 02:00:27,199
what about rupees
3138
02:00:27,199 --> 02:00:29,520
let's copy this paste it
3139
02:00:29,520 --> 02:00:30,719
h i
3140
02:00:30,719 --> 02:00:32,080
i n
3141
02:00:32,080 --> 02:00:33,360
currency
3142
02:00:33,360 --> 02:00:37,280
rupees have a currency code of i n r
3143
02:00:37,280 --> 02:00:38,840
there then let's try
3144
02:00:38,840 --> 02:00:42,320
euros let's set the language to german d
3145
02:00:42,320 --> 02:00:46,320
e dash d e the currency code is e u r
3146
02:00:46,320 --> 02:00:47,840
for euros
3147
02:00:47,840 --> 02:00:49,440
now there's other styles besides
3148
02:00:49,440 --> 02:00:52,719
currency this time let's try a percent
3149
02:00:52,719 --> 02:00:53,760
mynum
3150
02:00:53,760 --> 02:00:54,960
equals
3151
02:00:54,960 --> 02:00:56,639
mynum
3152
02:00:56,639 --> 02:00:57,520
to
3153
02:00:57,520 --> 02:00:58,560
locale
3154
02:00:58,560 --> 02:01:00,960
string
3155
02:01:01,040 --> 02:01:03,280
this time i will set the first argument
3156
02:01:03,280 --> 02:01:07,639
in place of a language to be undefined
3157
02:01:08,639 --> 02:01:10,800
but i will set the style
3158
02:01:10,800 --> 02:01:12,000
to be
3159
02:01:12,000 --> 02:01:13,679
percent
3160
02:01:13,679 --> 02:01:16,400
this number as a percent is
3161
02:01:16,400 --> 02:01:19,920
12 million percent so let's change that
3162
02:01:19,920 --> 02:01:23,119
100 would be ten thousand percent point
3163
02:01:23,119 --> 02:01:24,960
five is fifty percent
3164
02:01:24,960 --> 02:01:27,760
point zero one is one percent that's how
3165
02:01:27,760 --> 02:01:30,719
to format your number as a percent
3166
02:01:30,719 --> 02:01:32,880
then let's go to units
3167
02:01:32,880 --> 02:01:38,800
my num equals my mynum.2 locale string
3168
02:01:38,800 --> 02:01:40,480
we can set the first argument to be
3169
02:01:40,480 --> 02:01:43,119
undefined
3170
02:01:43,199 --> 02:01:47,199
then under options set the style to be
3171
02:01:47,199 --> 02:01:49,599
unit a unit of measurement what kind of
3172
02:01:49,599 --> 02:01:51,840
unit would we like i will separate that
3173
02:01:51,840 --> 02:01:53,520
with a comma
3174
02:01:53,520 --> 02:01:57,679
unit colon space what about celsius
3175
02:01:57,679 --> 02:02:00,400
celsius
3176
02:02:00,560 --> 02:02:02,840
so currently my number is
3177
02:02:02,840 --> 02:02:05,920
100 make sure it's lower case
3178
02:02:05,920 --> 02:02:08,239
there 100 degrees celsius
3179
02:02:08,239 --> 02:02:09,520
you have a couple different units to
3180
02:02:09,520 --> 02:02:13,119
choose from celsius kilometers miles
3181
02:02:13,119 --> 02:02:15,360
kilograms whatever you need
3182
02:02:15,360 --> 02:02:16,880
well okay then everybody that is an
3183
02:02:16,880 --> 02:02:19,280
introduction to the two locale string
3184
02:02:19,280 --> 02:02:22,239
method it returns a string with a
3185
02:02:22,239 --> 02:02:24,480
language sensitive representation of a
3186
02:02:24,480 --> 02:02:26,639
number you can set the locale which
3187
02:02:26,639 --> 02:02:29,040
specifies the language by passing in
3188
02:02:29,040 --> 02:02:31,119
undefined as an argument that will use
3189
02:02:31,119 --> 02:02:33,280
the default set in the browser then you
3190
02:02:33,280 --> 02:02:35,360
can pass in formatting options would you
3191
02:02:35,360 --> 02:02:37,280
like to set the style to currency
3192
02:02:37,280 --> 02:02:40,719
percent a unit of measurement etc so
3193
02:02:40,719 --> 02:02:43,040
yeah everybody that is the two locale
3194
02:02:43,040 --> 02:02:47,080
string method in javascript
3195
02:02:48,639 --> 02:02:50,239
hey guys in this video we're going to
3196
02:02:50,239 --> 02:02:52,400
create a very basic number guessing game
3197
02:02:52,400 --> 02:02:54,880
in javascript so heading to our html
3198
02:02:54,880 --> 02:02:56,960
file let's create some html elements
3199
02:02:56,960 --> 02:02:58,639
i'll need a title for this game i'll use
3200
02:02:58,639 --> 02:03:02,320
an h1 take we'll add some text number
3201
02:03:02,320 --> 02:03:03,520
guessing
3202
02:03:03,520 --> 02:03:05,520
game let's save that i'll add a
3203
02:03:05,520 --> 02:03:08,800
paragraph right underneath pick a number
3204
02:03:08,800 --> 02:03:10,320
between
3205
02:03:10,320 --> 02:03:11,199
1
3206
02:03:11,199 --> 02:03:13,199
through 10
3207
02:03:13,199 --> 02:03:16,159
and i'll add a label
3208
02:03:16,800 --> 02:03:20,239
label close it
3209
02:03:20,480 --> 02:03:23,280
enter a guess
3210
02:03:23,280 --> 02:03:25,440
we'll need some input
3211
02:03:25,440 --> 02:03:31,760
this will be a text box input id equals
3212
02:03:31,840 --> 02:03:32,880
guess
3213
02:03:32,880 --> 02:03:34,400
field
3214
02:03:34,400 --> 02:03:36,560
and a submit button
3215
02:03:36,560 --> 02:03:37,679
input
3216
02:03:37,679 --> 02:03:38,719
type
3217
02:03:38,719 --> 02:03:40,840
equals
3218
02:03:40,840 --> 02:03:45,360
submit the id will be submit
3219
02:03:45,360 --> 02:03:47,520
button
3220
02:03:47,520 --> 02:03:49,360
and those are the html elements that
3221
02:03:49,360 --> 02:03:50,960
we'll need now heading back to our
3222
02:03:50,960 --> 02:03:53,040
javascript file we'll need a random
3223
02:03:53,040 --> 02:03:54,320
number
3224
02:03:54,320 --> 02:03:56,560
i'll make this a constant
3225
02:03:56,560 --> 02:03:58,560
and it will be answer
3226
02:03:58,560 --> 02:04:00,960
answer will be a random number between 1
3227
02:04:00,960 --> 02:04:02,320
and 10.
3228
02:04:02,320 --> 02:04:06,440
so we will use math.random
3229
02:04:07,040 --> 02:04:08,960
times 10.
3230
02:04:08,960 --> 02:04:10,400
this will give us a random number
3231
02:04:10,400 --> 02:04:13,360
between zero and nine so let's add one
3232
02:04:13,360 --> 02:04:15,679
for numbers one through ten
3233
02:04:15,679 --> 02:04:17,280
then we will surround this with
3234
02:04:17,280 --> 02:04:20,360
math.floor to round it
3235
02:04:20,360 --> 02:04:22,880
math.floor and i will surround this code
3236
02:04:22,880 --> 02:04:26,239
with math.floor
3237
02:04:26,920 --> 02:04:29,440
math.floor we'll keep track of our
3238
02:04:29,440 --> 02:04:31,440
guesses
3239
02:04:31,440 --> 02:04:32,880
let
3240
02:04:32,880 --> 02:04:36,239
guesses equal zero
3241
02:04:36,239 --> 02:04:38,079
so when we click on this button we would
3242
02:04:38,079 --> 02:04:39,920
like to do something but we need to
3243
02:04:39,920 --> 02:04:41,840
select this button
3244
02:04:41,840 --> 02:04:44,960
and the id was submit button
3245
02:04:44,960 --> 02:04:47,119
document dot get
3246
02:04:47,119 --> 02:04:50,719
element by id
3247
02:04:51,599 --> 02:04:53,760
the id is submit button
3248
02:04:53,760 --> 02:04:56,320
and set the on click event attribute
3249
02:04:56,320 --> 02:04:59,520
equal to a function
3250
02:04:59,520 --> 02:05:01,440
what would we like to do well there's a
3251
02:05:01,440 --> 02:05:03,199
lot of stuff we have to do we'll need to
3252
02:05:03,199 --> 02:05:05,760
get the value from this text box
3253
02:05:05,760 --> 02:05:09,079
so document.getelementbyid
3254
02:05:09,679 --> 02:05:12,800
the id is
3255
02:05:12,960 --> 02:05:15,599
guest field
3256
02:05:17,119 --> 02:05:19,920
dot value and we will assign this to
3257
02:05:19,920 --> 02:05:22,000
guess so let's declare that and assign
3258
02:05:22,000 --> 02:05:24,079
it whenever we make a guess we should
3259
02:05:24,079 --> 02:05:26,079
increment guesses by 1 to keep track of
3260
02:05:26,079 --> 02:05:28,960
it guesses plus equals 1.
3261
02:05:28,960 --> 02:05:31,040
we'll first check to see if our guess is
3262
02:05:31,040 --> 02:05:33,119
equal to our answer
3263
02:05:33,119 --> 02:05:36,719
if guess is equal to our answer
3264
02:05:36,719 --> 02:05:39,360
let's create an alert message
3265
02:05:39,360 --> 02:05:41,760
alert
3266
02:05:42,320 --> 02:05:45,760
i'll use a template literal
3267
02:05:46,840 --> 02:05:48,480
answer
3268
02:05:48,480 --> 02:05:51,760
is the number
3269
02:05:53,119 --> 02:05:53,840
it
3270
02:05:53,840 --> 02:05:56,560
took you
3271
02:05:58,159 --> 02:06:00,159
guesses
3272
02:06:00,159 --> 02:06:02,560
guesses
3273
02:06:02,560 --> 02:06:05,280
else if
3274
02:06:06,400 --> 02:06:10,719
our guess is less than answer
3275
02:06:10,960 --> 02:06:14,239
we will alert the user
3276
02:06:15,280 --> 02:06:18,159
too small
3277
02:06:18,560 --> 02:06:19,599
else
3278
02:06:19,599 --> 02:06:22,000
alert
3279
02:06:22,880 --> 02:06:25,920
too large
3280
02:06:25,920 --> 02:06:28,000
okay make sure you save everything and
3281
02:06:28,000 --> 02:06:30,400
let's run it enter a number i'll pick
3282
02:06:30,400 --> 02:06:32,800
something right in the middle five
3283
02:06:32,800 --> 02:06:34,239
too small
3284
02:06:34,239 --> 02:06:36,400
what about seven
3285
02:06:36,400 --> 02:06:37,920
two small
3286
02:06:37,920 --> 02:06:39,679
nine
3287
02:06:39,679 --> 02:06:41,360
nine is the number it took you three
3288
02:06:41,360 --> 02:06:42,960
guesses
3289
02:06:42,960 --> 02:06:45,360
so yeah that is a very basic number
3290
02:06:45,360 --> 02:06:47,199
guessing game everybody
3291
02:06:47,199 --> 02:06:48,880
if you would like a copy of this code
3292
02:06:48,880 --> 02:06:50,320
i'll post this in the comment section
3293
02:06:50,320 --> 02:06:53,040
down below and well yeah that's a very
3294
02:06:53,040 --> 02:06:55,920
basic number guessing game in javascript
3295
02:06:55,920 --> 02:06:57,760
hey everybody in this video we're going
3296
02:06:57,760 --> 02:06:59,679
to create a practice program in
3297
02:06:59,679 --> 02:07:01,920
javascript to convert some temperatures
3298
02:07:01,920 --> 02:07:04,800
using some html elements so sit back
3299
02:07:04,800 --> 02:07:08,159
relax and enjoy the show
3300
02:07:08,159 --> 02:07:10,639
okay let's begin everybody before we add
3301
02:07:10,639 --> 02:07:13,280
html elements to our dom let's work on
3302
02:07:13,280 --> 02:07:15,760
the logic behind the functions let's
3303
02:07:15,760 --> 02:07:18,320
declare and assign variable temperature
3304
02:07:18,320 --> 02:07:20,400
and set the sequel to some temperature
3305
02:07:20,400 --> 02:07:21,920
doesn't matter if it's fahrenheit or
3306
02:07:21,920 --> 02:07:23,520
celsius yet and at the end of our
3307
02:07:23,520 --> 02:07:26,239
program let's declare two functions
3308
02:07:26,239 --> 02:07:27,360
function
3309
02:07:27,360 --> 02:07:29,040
to
3310
02:07:29,040 --> 02:07:31,599
celsius
3311
02:07:31,599 --> 02:07:33,760
and there will be one parameter
3312
02:07:33,760 --> 02:07:37,599
temperature and function two
3313
02:07:37,599 --> 02:07:40,159
fahrenheit the formula to convert a
3314
02:07:40,159 --> 02:07:43,840
temperature in fahrenheit to celsius is
3315
02:07:43,840 --> 02:07:45,280
our temperature
3316
02:07:45,280 --> 02:07:47,040
minus 32
3317
02:07:47,040 --> 02:07:48,719
times
3318
02:07:48,719 --> 02:07:50,639
five divided by nine
3319
02:07:50,639 --> 02:07:52,480
the formula to convert celsius to
3320
02:07:52,480 --> 02:07:55,840
fahrenheit is return
3321
02:07:55,840 --> 02:07:57,040
temp
3322
02:07:57,040 --> 02:07:59,840
times 9 divided by 5
3323
02:07:59,840 --> 02:08:01,520
plus 32
3324
02:08:01,520 --> 02:08:03,119
let's say that our temperature is
3325
02:08:03,119 --> 02:08:05,119
currently in fahrenheit and i need to
3326
02:08:05,119 --> 02:08:08,079
convert this to celsius i will reassign
3327
02:08:08,079 --> 02:08:08,880
temp
3328
02:08:08,880 --> 02:08:12,239
equal to and i will invoke the 2 celsius
3329
02:08:12,239 --> 02:08:13,360
function
3330
02:08:13,360 --> 02:08:15,679
and pass in my temperature then at the
3331
02:08:15,679 --> 02:08:17,040
end we will display whatever our
3332
02:08:17,040 --> 02:08:19,760
temperature is console.log
3333
02:08:19,760 --> 02:08:22,480
temp so the temperature should be zero
3334
02:08:22,480 --> 02:08:24,639
zero degrees celsius if we're converting
3335
02:08:24,639 --> 02:08:27,119
this to fahrenheit then 32 degrees
3336
02:08:27,119 --> 02:08:29,520
celsius converted to fahrenheit
3337
02:08:29,520 --> 02:08:32,480
is 89.6 degrees now that we have the
3338
02:08:32,480 --> 02:08:34,880
logic behind these functions completed
3339
02:08:34,880 --> 02:08:37,280
let's add some html elements heading to
3340
02:08:37,280 --> 02:08:40,320
our html file within the body tags we
3341
02:08:40,320 --> 02:08:43,199
will create a label
3342
02:08:44,079 --> 02:08:47,040
then close it and i'll add a line break
3343
02:08:47,040 --> 02:08:49,440
for some text let's add
3344
02:08:49,440 --> 02:08:50,400
enter
3345
02:08:50,400 --> 02:08:51,679
a
3346
02:08:51,679 --> 02:08:54,480
temperature
3347
02:08:54,719 --> 02:08:57,040
then we'll create a text box
3348
02:08:57,040 --> 02:08:59,840
so use a self-closing input tag i'll
3349
02:08:59,840 --> 02:09:02,800
create a line break
3350
02:09:03,679 --> 02:09:05,440
the type
3351
02:09:05,440 --> 02:09:07,520
is text
3352
02:09:07,520 --> 02:09:09,119
the id
3353
02:09:09,119 --> 02:09:12,400
let's say is text box
3354
02:09:12,400 --> 02:09:14,880
let's create a second label
3355
02:09:14,880 --> 02:09:17,280
label
3356
02:09:17,679 --> 02:09:18,880
close it
3357
02:09:18,880 --> 02:09:21,760
add a line break we will add the text
3358
02:09:21,760 --> 02:09:25,199
convert to colon we'll create two radio
3359
02:09:25,199 --> 02:09:26,719
buttons
3360
02:09:26,719 --> 02:09:27,840
input
3361
02:09:27,840 --> 02:09:29,280
type
3362
02:09:29,280 --> 02:09:32,239
equals radio so with a radio button if
3363
02:09:32,239 --> 02:09:33,679
they're in the same group you can only
3364
02:09:33,679 --> 02:09:36,320
select one
3365
02:09:36,320 --> 02:09:38,159
this first radio button will be for
3366
02:09:38,159 --> 02:09:39,520
celsius
3367
02:09:39,520 --> 02:09:42,719
we'll give this a unique id of c button
3368
02:09:42,719 --> 02:09:45,599
for celsius
3369
02:09:45,599 --> 02:09:46,400
name
3370
02:09:46,400 --> 02:09:47,440
unit
3371
02:09:47,440 --> 02:09:50,000
let's save there's one radio button and
3372
02:09:50,000 --> 02:09:51,679
we'll add a label next to this radio
3373
02:09:51,679 --> 02:09:53,280
button
3374
02:09:53,280 --> 02:09:54,560
label
3375
02:09:54,560 --> 02:09:56,880
close it
3376
02:09:56,880 --> 02:09:58,480
add a line break
3377
02:09:58,480 --> 02:10:00,639
and the label will be
3378
02:10:00,639 --> 02:10:03,280
celsius
3379
02:10:03,760 --> 02:10:06,880
okay let's copy these two lines
3380
02:10:06,880 --> 02:10:10,239
and paste them and make a few changes
3381
02:10:10,239 --> 02:10:13,119
this will be the fahrenheit radio button
3382
02:10:13,119 --> 02:10:15,679
we'll give this an id of f button the
3383
02:10:15,679 --> 02:10:17,840
name will be unit
3384
02:10:17,840 --> 02:10:20,800
and change the label
3385
02:10:20,800 --> 02:10:23,199
fair and height
3386
02:10:23,199 --> 02:10:24,800
since these radio buttons are within the
3387
02:10:24,800 --> 02:10:27,360
same group they have the same name
3388
02:10:27,360 --> 02:10:29,280
we can only select one of them
3389
02:10:29,280 --> 02:10:32,079
if they had a different name
3390
02:10:32,079 --> 02:10:34,079
like c or f
3391
02:10:34,079 --> 02:10:35,520
well then you can select both of them
3392
02:10:35,520 --> 02:10:37,599
because they're in different groups
3393
02:10:37,599 --> 02:10:40,000
so these will have the same name unit
3394
02:10:40,000 --> 02:10:41,599
let's create a submit button right at
3395
02:10:41,599 --> 02:10:42,880
the end
3396
02:10:42,880 --> 02:10:44,320
so button
3397
02:10:44,320 --> 02:10:45,679
let's close it
3398
02:10:45,679 --> 02:10:48,639
add a line break
3399
02:10:49,520 --> 02:10:53,280
the type will be button
3400
02:10:53,679 --> 02:10:57,040
and the id is maybe submit
3401
02:10:57,040 --> 02:10:58,000
button
3402
02:10:58,000 --> 02:11:00,960
let's add some text to this button
3403
02:11:00,960 --> 02:11:03,440
submit
3404
02:11:03,679 --> 02:11:05,520
i'm going to close that at the very
3405
02:11:05,520 --> 02:11:07,599
bottom of our dom let's add a label that
3406
02:11:07,599 --> 02:11:10,800
will display the temperature
3407
02:11:10,800 --> 02:11:13,360
use a pair of label tags i'll give this
3408
02:11:13,360 --> 02:11:15,280
an id of
3409
02:11:15,280 --> 02:11:16,320
temp
3410
02:11:16,320 --> 02:11:18,480
label and those are all the html
3411
02:11:18,480 --> 02:11:20,000
elements that we'll need
3412
02:11:20,000 --> 02:11:22,159
let's save head back to our javascript
3413
02:11:22,159 --> 02:11:24,719
file when we click on the submit button
3414
02:11:24,719 --> 02:11:26,079
there's a lot of things that we need to
3415
02:11:26,079 --> 02:11:28,480
do at the very top of my program i'm
3416
02:11:28,480 --> 02:11:29,920
going to type
3417
02:11:29,920 --> 02:11:34,400
document dot get element by id
3418
02:11:34,400 --> 02:11:39,040
the id of this button is submit button
3419
02:11:39,040 --> 02:11:41,040
dot on click
3420
02:11:41,040 --> 02:11:43,840
and i will set the sequel to a function
3421
02:11:43,840 --> 02:11:46,159
parentheses curly braces
3422
02:11:46,159 --> 02:11:47,599
there's a lot of lines of code that
3423
02:11:47,599 --> 02:11:49,119
we're going to add between these curly
3424
02:11:49,119 --> 02:11:51,360
braces so let's get rid of these three
3425
02:11:51,360 --> 02:11:52,800
lines of code we don't need them any
3426
02:11:52,800 --> 02:11:54,880
longer i'm going to declare a local
3427
02:11:54,880 --> 02:11:57,599
variable within this function let
3428
02:11:57,599 --> 02:11:58,800
temp
3429
02:11:58,800 --> 02:12:00,800
so when we click on our button let's
3430
02:12:00,800 --> 02:12:02,400
check to see if one of these radio
3431
02:12:02,400 --> 02:12:04,000
buttons is selected
3432
02:12:04,000 --> 02:12:06,480
let's begin with the celsius button
3433
02:12:06,480 --> 02:12:08,000
if
3434
02:12:08,000 --> 02:12:10,719
document dot get
3435
02:12:10,719 --> 02:12:13,520
element by id
3436
02:12:13,520 --> 02:12:16,480
and the id of the celsius button is c
3437
02:12:16,480 --> 02:12:18,800
button
3438
02:12:19,599 --> 02:12:21,520
we need to check to see if this radio
3439
02:12:21,520 --> 02:12:23,280
button is checked or not
3440
02:12:23,280 --> 02:12:24,880
so add dot
3441
02:12:24,880 --> 02:12:26,000
checked
3442
02:12:26,000 --> 02:12:27,119
if this
3443
02:12:27,119 --> 02:12:28,560
equals true
3444
02:12:28,560 --> 02:12:31,040
but since this is a boolean value you
3445
02:12:31,040 --> 02:12:33,199
don't need to necessarily add equals
3446
02:12:33,199 --> 02:12:35,480
true
3447
02:12:35,480 --> 02:12:37,599
document.getelementbyid c button dot
3448
02:12:37,599 --> 02:12:38,560
checked
3449
02:12:38,560 --> 02:12:40,400
that's fine as a condition
3450
02:12:40,400 --> 02:12:42,960
if our celsius button is selected do
3451
02:12:42,960 --> 02:12:44,880
something
3452
02:12:44,880 --> 02:12:47,199
else if
3453
02:12:47,199 --> 02:12:49,360
our fahrenheit button is selected do
3454
02:12:49,360 --> 02:12:50,480
something else
3455
02:12:50,480 --> 02:12:54,159
let's copy our condition paste it
3456
02:12:54,159 --> 02:12:56,480
this time we are checking the fahrenheit
3457
02:12:56,480 --> 02:12:59,920
button that is f button
3458
02:12:59,920 --> 02:13:02,079
and lastly else else means we didn't
3459
02:13:02,079 --> 02:13:04,320
select anything let's work on the else
3460
02:13:04,320 --> 02:13:06,960
statement first that's fairly easy so i
3461
02:13:06,960 --> 02:13:08,719
would like to change our label that's
3462
02:13:08,719 --> 02:13:12,239
down here at the bottom temp label
3463
02:13:12,239 --> 02:13:17,599
so type document dot get element by id
3464
02:13:17,599 --> 02:13:20,880
temp label and i will change the inner
3465
02:13:20,880 --> 02:13:24,239
html i'll set the sequel to
3466
02:13:24,239 --> 02:13:26,480
select a unit
3467
02:13:26,480 --> 02:13:28,239
if i were to click on the submit button
3468
02:13:28,239 --> 02:13:30,719
it should change the text of my label to
3469
02:13:30,719 --> 02:13:33,360
select unit we know that it's working if
3470
02:13:33,360 --> 02:13:35,520
we enter in a temperature press the
3471
02:13:35,520 --> 02:13:37,199
celsius radio button
3472
02:13:37,199 --> 02:13:38,639
then click submit
3473
02:13:38,639 --> 02:13:40,320
what would we like to do
3474
02:13:40,320 --> 02:13:42,400
let's extract the temperature from this
3475
02:13:42,400 --> 02:13:43,599
text box
3476
02:13:43,599 --> 02:13:46,560
so we will take our temperature variable
3477
02:13:46,560 --> 02:13:48,560
set the sequel to
3478
02:13:48,560 --> 02:13:52,639
document dot get element by id
3479
02:13:52,639 --> 02:13:58,000
and the id of this text box is text box
3480
02:13:58,000 --> 02:13:59,760
dot value
3481
02:13:59,760 --> 02:14:01,840
when we accept user input from a text
3482
02:14:01,840 --> 02:14:04,159
box it's of a string data type we'll
3483
02:14:04,159 --> 02:14:06,239
need to convert that to a number so
3484
02:14:06,239 --> 02:14:09,760
let's reassign temp equal to
3485
02:14:09,760 --> 02:14:11,760
use the number constructor and pass in
3486
02:14:11,760 --> 02:14:13,119
our temperature
3487
02:14:13,119 --> 02:14:14,960
it's assumed to be in fahrenheit if we
3488
02:14:14,960 --> 02:14:17,040
would like to convert to celsius let's
3489
02:14:17,040 --> 02:14:19,920
invoke the 2 celsius function
3490
02:14:19,920 --> 02:14:23,119
i'll reassign temp equal to
3491
02:14:23,119 --> 02:14:26,239
the 2 celsius function invoke it and
3492
02:14:26,239 --> 02:14:28,480
then we need to pass in our temperature
3493
02:14:28,480 --> 02:14:30,079
after the temperature is converted we
3494
02:14:30,079 --> 02:14:32,159
need to change our label
3495
02:14:32,159 --> 02:14:35,360
so copy this line of code paste it
3496
02:14:35,360 --> 02:14:38,719
and i will set the sql to
3497
02:14:39,119 --> 02:14:41,119
temp plus
3498
02:14:41,119 --> 02:14:43,520
if you're writing this code on a pc if
3499
02:14:43,520 --> 02:14:45,520
you need the degree symbol one shortcut
3500
02:14:45,520 --> 02:14:47,199
is to hold alt
3501
02:14:47,199 --> 02:14:49,280
and on the numpad press
3502
02:14:49,280 --> 02:14:51,280
zero one seven six
3503
02:14:51,280 --> 02:14:53,520
if you're on a mac then do this
3504
02:14:53,520 --> 02:14:55,599
i'll post it on the screen right now so
3505
02:14:55,599 --> 02:14:58,400
this temperature is in celsius
3506
02:14:58,400 --> 02:14:59,920
okay let's test this
3507
02:14:59,920 --> 02:15:01,599
32 degrees
3508
02:15:01,599 --> 02:15:03,520
converted to celsius
3509
02:15:03,520 --> 02:15:05,760
is 0 degrees celsius
3510
02:15:05,760 --> 02:15:08,079
if this was 50 degrees fahrenheit
3511
02:15:08,079 --> 02:15:09,760
converted to celsius
3512
02:15:09,760 --> 02:15:12,159
that would be at 10 degrees celsius
3513
02:15:12,159 --> 02:15:14,320
now with the fahrenheit radio button
3514
02:15:14,320 --> 02:15:16,480
let's copy everything we have within
3515
02:15:16,480 --> 02:15:18,000
this if statement
3516
02:15:18,000 --> 02:15:19,280
paste it
3517
02:15:19,280 --> 02:15:21,679
and we will invoke the 2 fahrenheit
3518
02:15:21,679 --> 02:15:23,599
function instead of the two celsius
3519
02:15:23,599 --> 02:15:25,840
function
3520
02:15:25,840 --> 02:15:30,079
and change the temperature from c to f
3521
02:15:30,079 --> 02:15:31,920
okay let's try this
3522
02:15:31,920 --> 02:15:33,280
zero degrees
3523
02:15:33,280 --> 02:15:36,480
in celsius converted to fahrenheit is 32
3524
02:15:36,480 --> 02:15:39,199
degrees fahrenheit so yeah that was a
3525
02:15:39,199 --> 02:15:41,360
practice project to convert temperatures
3526
02:15:41,360 --> 02:15:43,199
using javascript hey if you found this
3527
02:15:43,199 --> 02:15:45,119
video helpful please be sure to smash
3528
02:15:45,119 --> 02:15:46,639
that like button leave a random comment
3529
02:15:46,639 --> 02:15:48,320
down below and subscribe if you'd like
3530
02:15:48,320 --> 02:15:50,880
to become a fellow bro
3531
02:15:50,880 --> 02:15:53,199
well well well we've finally made it to
3532
02:15:53,199 --> 02:15:56,320
a raise think of an array as a variable
3533
02:15:56,320 --> 02:15:59,040
that can store multiple values it's a
3534
02:15:59,040 --> 02:16:01,119
little more complex than that but that's
3535
02:16:01,119 --> 02:16:02,960
an easy way of thinking about them in
3536
02:16:02,960 --> 02:16:05,920
this example i have a variable fruit
3537
02:16:05,920 --> 02:16:07,280
fruit contains some string
3538
02:16:07,280 --> 02:16:09,760
representation of a fruit like an apple
3539
02:16:09,760 --> 02:16:12,079
i can store multiple names of fruits
3540
02:16:12,079 --> 02:16:14,320
within this variable if i convert it to
3541
02:16:14,320 --> 02:16:16,960
an array so to change this variable into
3542
02:16:16,960 --> 02:16:19,360
an array surround all of your values
3543
02:16:19,360 --> 02:16:21,520
with a set of square brackets and
3544
02:16:21,520 --> 02:16:24,320
separate each value with a comma maybe i
3545
02:16:24,320 --> 02:16:26,320
don't want just an apple maybe i would
3546
02:16:26,320 --> 02:16:27,440
also like
3547
02:16:27,440 --> 02:16:28,880
an orange
3548
02:16:28,880 --> 02:16:31,280
and a banana just so it's more obvious
3549
02:16:31,280 --> 02:16:33,280
that this contains multiple values i'm
3550
02:16:33,280 --> 02:16:35,519
going to change this variable name from
3551
02:16:35,519 --> 02:16:37,280
fruit to fruits well i guess in the
3552
02:16:37,280 --> 02:16:39,040
english language fruit would also be
3553
02:16:39,040 --> 02:16:41,280
plural but it's just not obvious so we
3554
02:16:41,280 --> 02:16:43,439
have an array fruits now look what
3555
02:16:43,439 --> 02:16:45,398
happens when i display this array using
3556
02:16:45,398 --> 02:16:47,200
console.log
3557
02:16:47,200 --> 02:16:50,000
if i display my array fruits it will
3558
02:16:50,000 --> 02:16:52,240
display all of this junk you can see the
3559
02:16:52,240 --> 02:16:54,718
different values within this array apple
3560
02:16:54,718 --> 02:16:57,920
orange banana each value within an array
3561
02:16:57,920 --> 02:17:00,080
is also known as an element if i would
3562
02:17:00,080 --> 02:17:02,318
like to access a particular element
3563
02:17:02,318 --> 02:17:04,478
within this array when i use the array
3564
02:17:04,478 --> 02:17:07,200
name i will add a set of square brackets
3565
02:17:07,200 --> 02:17:09,280
then i need to list an index number of
3566
02:17:09,280 --> 02:17:11,280
the element i would like to access
3567
02:17:11,280 --> 02:17:13,519
computers they always start with zero so
3568
02:17:13,519 --> 02:17:15,920
the first element in my array has an
3569
02:17:15,920 --> 02:17:19,280
index of zero if i was to display fruits
3570
02:17:19,280 --> 02:17:21,920
at index of zero then we have access to
3571
02:17:21,920 --> 02:17:23,840
the first element which contains an
3572
02:17:23,840 --> 02:17:24,718
apple
3573
02:17:24,718 --> 02:17:26,879
the next element which has an index of
3574
02:17:26,879 --> 02:17:30,160
one is my orange at index 2 there's the
3575
02:17:30,160 --> 02:17:32,398
banana if i attempt to access an element
3576
02:17:32,398 --> 02:17:35,519
that doesn't exist such as index 3 well
3577
02:17:35,519 --> 02:17:36,959
then it's undefined
3578
02:17:36,959 --> 02:17:38,558
you can also update and change the
3579
02:17:38,558 --> 02:17:40,398
elements of an array
3580
02:17:40,398 --> 02:17:42,799
so to do that type the name of the array
3581
02:17:42,799 --> 02:17:44,718
followed by an index number
3582
02:17:44,718 --> 02:17:48,318
maybe fruits at index of zero is now a
3583
02:17:48,318 --> 02:17:50,000
coconut
3584
02:17:50,000 --> 02:17:52,080
and then let's display fruits
3585
02:17:52,080 --> 02:17:54,879
so we have a coconut orange and banana
3586
02:17:54,879 --> 02:17:57,599
if i change this to one we have an apple
3587
02:17:57,599 --> 02:17:59,359
coconut banana
3588
02:17:59,359 --> 02:18:00,478
two
3589
02:18:00,478 --> 02:18:02,879
apple orange coconut so to access an
3590
02:18:02,879 --> 02:18:04,558
element from an array you type the array
3591
02:18:04,558 --> 02:18:06,558
name followed by a set of square
3592
02:18:06,558 --> 02:18:09,280
brackets and then an index number you
3593
02:18:09,280 --> 02:18:11,359
can later on add an element
3594
02:18:11,359 --> 02:18:13,679
to do that use the push method
3595
02:18:13,679 --> 02:18:15,280
type the array name
3596
02:18:15,280 --> 02:18:17,439
dot push parenthesis
3597
02:18:17,439 --> 02:18:19,120
then add a value
3598
02:18:19,120 --> 02:18:20,959
like a lemon
3599
02:18:20,959 --> 02:18:23,280
we have an apple an orange a banana and
3600
02:18:23,280 --> 02:18:26,318
a lemon this will add an element
3601
02:18:26,318 --> 02:18:28,558
we can remove the last element
3602
02:18:28,558 --> 02:18:31,439
fruits dot pop
3603
02:18:31,439 --> 02:18:33,760
okay that lemon is no longer there apple
3604
02:18:33,760 --> 02:18:35,439
orange banana
3605
02:18:35,439 --> 02:18:37,679
removes last
3606
02:18:37,679 --> 02:18:39,040
element
3607
02:18:39,040 --> 02:18:41,120
we have the capability to add an element
3608
02:18:41,120 --> 02:18:43,200
to the beginning of an array type the
3609
02:18:43,200 --> 02:18:46,080
array name dot on
3610
02:18:46,080 --> 02:18:47,200
shift
3611
02:18:47,200 --> 02:18:49,040
method
3612
02:18:49,040 --> 02:18:50,718
and add a value
3613
02:18:50,718 --> 02:18:53,280
maybe a mango
3614
02:18:53,280 --> 02:18:56,318
mango apple orange banana
3615
02:18:56,318 --> 02:18:58,080
this method will add
3616
02:18:58,080 --> 02:19:00,478
element to the beginning
3617
02:19:00,478 --> 02:19:02,638
and the shift method will remove the
3618
02:19:02,638 --> 02:19:04,160
beginning element
3619
02:19:04,160 --> 02:19:05,120
fruits
3620
02:19:05,120 --> 02:19:07,840
dot shift
3621
02:19:07,840 --> 02:19:10,879
apple orange banana that mango is gone
3622
02:19:10,879 --> 02:19:13,280
because i ate it so the shift method
3623
02:19:13,280 --> 02:19:16,080
removes element from
3624
02:19:16,080 --> 02:19:17,760
beginning
3625
02:19:17,760 --> 02:19:19,519
you can access the length property of an
3626
02:19:19,519 --> 02:19:21,920
array i'll store this within a variable
3627
02:19:21,920 --> 02:19:22,959
let
3628
02:19:22,959 --> 02:19:23,840
length
3629
02:19:23,840 --> 02:19:25,439
equal fruits
3630
02:19:25,439 --> 02:19:26,398
dot
3631
02:19:26,398 --> 02:19:28,080
length
3632
02:19:28,080 --> 02:19:30,240
and the length of my array
3633
02:19:30,240 --> 02:19:32,478
is three we have three elements an apple
3634
02:19:32,478 --> 02:19:34,799
an orange and a banana you can find the
3635
02:19:34,799 --> 02:19:36,638
index of an element
3636
02:19:36,638 --> 02:19:37,679
let
3637
02:19:37,679 --> 02:19:40,558
index equal the array name
3638
02:19:40,558 --> 02:19:41,679
dot
3639
02:19:41,679 --> 02:19:44,799
index of method i would like to find the
3640
02:19:44,799 --> 02:19:47,120
index of our apple value
3641
02:19:47,120 --> 02:19:49,359
and i will display this
3642
02:19:49,359 --> 02:19:50,880
so again computers always start with
3643
02:19:50,880 --> 02:19:51,760
zero
3644
02:19:51,760 --> 02:19:54,240
the index of my apple value was found at
3645
02:19:54,240 --> 02:19:57,359
index zero the beginning
3646
02:19:57,359 --> 02:19:59,840
orange would be at one
3647
02:19:59,840 --> 02:20:02,720
banana is at two if you search for an
3648
02:20:02,720 --> 02:20:04,800
index of an element that doesn't exist
3649
02:20:04,800 --> 02:20:06,560
such as
3650
02:20:06,560 --> 02:20:08,560
how about a kiwi well then this will
3651
02:20:08,560 --> 02:20:11,120
return an index of negative one that
3652
02:20:11,120 --> 02:20:12,800
means it was not found
3653
02:20:12,800 --> 02:20:14,960
so yeah everybody that's an array think
3654
02:20:14,960 --> 02:20:17,359
of it as a variable that can store
3655
02:20:17,359 --> 02:20:19,359
multiple values it's a little more
3656
02:20:19,359 --> 02:20:21,439
complex than that but that's an easy way
3657
02:20:21,439 --> 02:20:23,680
to think about it so yeah this is the
3658
02:20:23,680 --> 02:20:26,319
beginning on a small series on arrays
3659
02:20:26,319 --> 02:20:28,000
hey if this video helped you out you can
3660
02:20:28,000 --> 02:20:30,080
help me out by smashing that like button
3661
02:20:30,080 --> 02:20:31,680
leave a random comment down below and
3662
02:20:31,680 --> 02:20:32,960
subscribe if you'd like to become a
3663
02:20:32,960 --> 02:20:35,040
fellow bro
3664
02:20:35,040 --> 02:20:36,960
hey guys in this video i'm going to show
3665
02:20:36,960 --> 02:20:38,640
you how we can loop through the elements
3666
02:20:38,640 --> 02:20:40,880
of an array maybe we have an array of
3667
02:20:40,880 --> 02:20:43,600
prices and i will set this equal to and
3668
02:20:43,600 --> 02:20:45,200
just make up some prices it doesn't
3669
02:20:45,200 --> 02:20:47,840
really matter five dollars ten dollars
3670
02:20:47,840 --> 02:20:49,920
fifteen dollars twenty dollars good
3671
02:20:49,920 --> 02:20:52,399
enough two popular ways to iterate over
3672
02:20:52,399 --> 02:20:54,880
an array is one to use a standard for
3673
02:20:54,880 --> 02:20:57,520
loop and the other is a for of statement
3674
02:20:57,520 --> 02:20:59,840
let's begin with a standard for loop so
3675
02:20:59,840 --> 02:21:02,640
type four parentheses curly braces we'll
3676
02:21:02,640 --> 02:21:05,520
need to create a counter let i set this
3677
02:21:05,520 --> 02:21:07,680
equal to zero i would like to continue
3678
02:21:07,680 --> 02:21:10,240
this while i is less than the length
3679
02:21:10,240 --> 02:21:14,000
property of our array prices prices dot
3680
02:21:14,000 --> 02:21:17,439
length then i will increment i by one i
3681
02:21:17,439 --> 02:21:20,479
plus equals one during each iteration i
3682
02:21:20,479 --> 02:21:23,920
will display my array prices but we
3683
02:21:23,920 --> 02:21:27,520
gotta list an index so that index is i
3684
02:21:27,520 --> 02:21:30,080
prices at index of i during the first
3685
02:21:30,080 --> 02:21:32,880
iteration i will be zero during the next
3686
02:21:32,880 --> 02:21:35,439
iteration it would be one then two then
3687
02:21:35,439 --> 02:21:37,840
three so on and so forth so after
3688
02:21:37,840 --> 02:21:40,000
running this program this will iterate
3689
02:21:40,000 --> 02:21:42,640
and display the elements of my array 5
3690
02:21:42,640 --> 02:21:45,280
10 15 20 and it will adjust according to
3691
02:21:45,280 --> 02:21:47,439
the size of the array and my for loop
3692
02:21:47,439 --> 02:21:49,439
will iterate over that price as well now
3693
02:21:49,439 --> 02:21:51,280
if you would like to iterate backwards
3694
02:21:51,280 --> 02:21:53,760
over this for loop here's how to do that
3695
02:21:53,760 --> 02:21:57,120
so i'm going to set my counter equal to
3696
02:21:57,120 --> 02:22:00,880
prices dot length minus 1. i would like
3697
02:22:00,880 --> 02:22:03,600
to continue this as long as i is greater
3698
02:22:03,600 --> 02:22:06,880
than or equal to 0 and then lastly we
3699
02:22:06,880 --> 02:22:10,000
will decrement i by 1. this allows me to
3700
02:22:10,000 --> 02:22:12,399
iterate over my array backwards we're
3701
02:22:12,399 --> 02:22:16,560
beginning at the end 25 20 15 10 5. so
3702
02:22:16,560 --> 02:22:18,560
that's how to use a standard for loop to
3703
02:22:18,560 --> 02:22:20,720
iterate over the elements of an array an
3704
02:22:20,720 --> 02:22:23,680
additional option is a for up statement
3705
02:22:23,680 --> 02:22:26,319
we'll type four parentheses curly braces
3706
02:22:26,319 --> 02:22:28,160
we'll declare a counter
3707
02:22:28,160 --> 02:22:29,040
let
3708
02:22:29,040 --> 02:22:30,000
i
3709
02:22:30,000 --> 02:22:31,120
of
3710
02:22:31,120 --> 02:22:34,479
our array prices
3711
02:22:34,479 --> 02:22:37,359
console.log
3712
02:22:37,359 --> 02:22:39,920
just i and this will also iterate over
3713
02:22:39,920 --> 02:22:41,840
our array but a better way of writing
3714
02:22:41,840 --> 02:22:44,720
this in place of our counter i let's use
3715
02:22:44,720 --> 02:22:46,880
a word that's more descriptive of what's
3716
02:22:46,880 --> 02:22:49,359
within our array our array is named
3717
02:22:49,359 --> 02:22:50,479
prices
3718
02:22:50,479 --> 02:22:53,200
an element of this array prices would be
3719
02:22:53,200 --> 02:22:55,359
price so let's change this to price
3720
02:22:55,359 --> 02:22:57,240
because it's more descriptive
3721
02:22:57,240 --> 02:23:00,080
console.log price and i think people
3722
02:23:00,080 --> 02:23:03,200
would understand that more there 5 10 15
3723
02:23:03,200 --> 02:23:05,840
20 25. so yeah everybody those are a
3724
02:23:05,840 --> 02:23:07,760
couple different ways to iterate over
3725
02:23:07,760 --> 02:23:09,600
the elements of an array hey if this
3726
02:23:09,600 --> 02:23:11,439
video helped you out you can help me out
3727
02:23:11,439 --> 02:23:12,960
by smashing that like button leave a
3728
02:23:12,960 --> 02:23:14,800
random comment down below and subscribe
3729
02:23:14,800 --> 02:23:18,240
if you'd like to become a fellow bro
3730
02:23:18,240 --> 02:23:20,319
hey everybody in this video i'm going to
3731
02:23:20,319 --> 02:23:22,000
show you how we can sort an array of
3732
02:23:22,000 --> 02:23:24,560
strings in javascript let's create an
3733
02:23:24,560 --> 02:23:27,280
array of fruits because i'm hungry and
3734
02:23:27,280 --> 02:23:29,280
add some various names of fruits make
3735
02:23:29,280 --> 02:23:30,840
sure that they're not in order
3736
02:23:30,840 --> 02:23:32,399
banana
3737
02:23:32,399 --> 02:23:34,800
apple
3738
02:23:35,359 --> 02:23:37,840
orange
3739
02:23:38,319 --> 02:23:40,880
and a mango to display my array of
3740
02:23:40,880 --> 02:23:44,479
strings i can use a for of statement
3741
02:23:44,479 --> 02:23:45,760
we'll create a for loop and the
3742
02:23:45,760 --> 02:23:48,479
statement within the for loop is let
3743
02:23:48,479 --> 02:23:52,319
fruit of fruits
3744
02:23:52,479 --> 02:23:55,479
console.log
3745
02:23:55,760 --> 02:23:56,720
fruit
3746
02:23:56,720 --> 02:23:58,560
singular
3747
02:23:58,560 --> 02:24:01,040
and here is my array of fruits
3748
02:24:01,040 --> 02:24:03,439
to sort this array there is a built-in
3749
02:24:03,439 --> 02:24:05,920
method of arrays called sort but i'm
3750
02:24:05,920 --> 02:24:08,720
going to reassign this to the same array
3751
02:24:08,720 --> 02:24:12,160
fruits equals fruits and use the sort
3752
02:24:12,160 --> 02:24:13,280
method
3753
02:24:13,280 --> 02:24:15,280
and my fruits are now sorted in
3754
02:24:15,280 --> 02:24:17,520
alphabetical order if you need this in
3755
02:24:17,520 --> 02:24:19,359
reverse alphabetical order this is what
3756
02:24:19,359 --> 02:24:20,560
you can do
3757
02:24:20,560 --> 02:24:22,000
i'm going to turn that line into a
3758
02:24:22,000 --> 02:24:24,960
comment copy it paste it we're going to
3759
02:24:24,960 --> 02:24:29,520
do some method chaining add dot reverse
3760
02:24:29,520 --> 02:24:31,600
parentheses
3761
02:24:31,600 --> 02:24:33,920
and my array of strings is now sorted in
3762
02:24:33,920 --> 02:24:35,760
reverse alphabetical order so yeah
3763
02:24:35,760 --> 02:24:37,840
that's a super quick video on how to
3764
02:24:37,840 --> 02:24:40,160
sort an array of strings in alphabetical
3765
02:24:40,160 --> 02:24:42,319
order and reverse alphabetical order if
3766
02:24:42,319 --> 02:24:43,920
you would like a copy of this code i'll
3767
02:24:43,920 --> 02:24:45,359
post this in the comment section down
3768
02:24:45,359 --> 02:24:47,840
below and well yeah that's how to sort
3769
02:24:47,840 --> 02:24:51,439
an array of strings in javascript
3770
02:24:51,439 --> 02:24:53,120
hey guys in this video i'm going to
3771
02:24:53,120 --> 02:24:55,840
discuss 2d arrays also known as
3772
02:24:55,840 --> 02:24:58,319
multi-dimensional arrays it's an array
3773
02:24:58,319 --> 02:25:00,640
made up of arrays so in this example
3774
02:25:00,640 --> 02:25:03,359
we'll create a grocery shopping list and
3775
02:25:03,359 --> 02:25:05,359
that grocery shopping list will be made
3776
02:25:05,359 --> 02:25:08,160
up of separate individual lists a list
3777
02:25:08,160 --> 02:25:10,800
for fruit vegetables and meat let's
3778
02:25:10,800 --> 02:25:12,720
begin by creating three individual
3779
02:25:12,720 --> 02:25:14,960
arrays and then add them all to one
3780
02:25:14,960 --> 02:25:16,880
array to contain them let's begin with
3781
02:25:16,880 --> 02:25:20,319
an array named fruit or fruits
3782
02:25:20,319 --> 02:25:23,600
let fruits equal and think of some fruit
3783
02:25:23,600 --> 02:25:26,000
i'm thinking apples
3784
02:25:26,000 --> 02:25:27,920
oranges
3785
02:25:27,920 --> 02:25:30,640
bananas this is our first array let's
3786
02:25:30,640 --> 02:25:34,720
create another i'll name this vegetables
3787
02:25:35,359 --> 02:25:38,640
what about carrots
3788
02:25:38,640 --> 02:25:41,040
onions
3789
02:25:41,040 --> 02:25:44,800
and potatoes and lastly meat
3790
02:25:44,800 --> 02:25:47,439
or meats
3791
02:25:47,439 --> 02:25:49,600
eggs
3792
02:25:49,600 --> 02:25:52,000
chicken
3793
02:25:52,000 --> 02:25:55,359
fish we have three individual arrays if
3794
02:25:55,359 --> 02:25:57,280
we wanted to we could add these
3795
02:25:57,280 --> 02:26:00,240
individual arrays into another array i'm
3796
02:26:00,240 --> 02:26:01,920
going to create another array this will
3797
02:26:01,920 --> 02:26:04,840
be our two-dimensional array named
3798
02:26:04,840 --> 02:26:07,200
grocery list
3799
02:26:07,200 --> 02:26:10,000
equals and add the names of the arrays
3800
02:26:10,000 --> 02:26:11,760
that you would like to add
3801
02:26:11,760 --> 02:26:13,520
fruits
3802
02:26:13,520 --> 02:26:15,439
vegetables
3803
02:26:15,439 --> 02:26:16,720
and meats
3804
02:26:16,720 --> 02:26:18,720
so grocery list is our two dimensional
3805
02:26:18,720 --> 02:26:20,640
array how can we iterate over the
3806
02:26:20,640 --> 02:26:22,800
elements of this two-dimensional array
3807
02:26:22,800 --> 02:26:25,520
we can use nested loops i'll use nested
3808
02:26:25,520 --> 02:26:27,920
four of loops
3809
02:26:27,920 --> 02:26:29,520
the outer loop will be in charge of
3810
02:26:29,520 --> 02:26:31,359
managing each list
3811
02:26:31,359 --> 02:26:32,560
let
3812
02:26:32,560 --> 02:26:35,680
let's name each item maybe list
3813
02:26:35,680 --> 02:26:36,880
let list
3814
02:26:36,880 --> 02:26:38,160
of
3815
02:26:38,160 --> 02:26:40,000
grocery list
3816
02:26:40,000 --> 02:26:42,080
so right now if i display list it's
3817
02:26:42,080 --> 02:26:43,840
going to display all of the individual
3818
02:26:43,840 --> 02:26:46,960
arrays i'm just going to test that
3819
02:26:46,960 --> 02:26:48,720
console.log
3820
02:26:48,720 --> 02:26:50,960
list this for loop will iterate three
3821
02:26:50,960 --> 02:26:53,040
times one for each array within our
3822
02:26:53,040 --> 02:26:55,600
grocery list and it displays details of
3823
02:26:55,600 --> 02:26:57,840
each list to access the individual
3824
02:26:57,840 --> 02:27:00,640
elements within each list each array i'm
3825
02:27:00,640 --> 02:27:02,720
going to create a nested loop we'll use
3826
02:27:02,720 --> 02:27:04,960
a for of loop again
3827
02:27:04,960 --> 02:27:05,760
let
3828
02:27:05,760 --> 02:27:07,200
how about food
3829
02:27:07,200 --> 02:27:09,439
like each food item
3830
02:27:09,439 --> 02:27:10,479
of
3831
02:27:10,479 --> 02:27:12,479
list
3832
02:27:12,479 --> 02:27:15,680
and i will display console.log
3833
02:27:15,680 --> 02:27:18,800
each food item each element food so this
3834
02:27:18,800 --> 02:27:20,800
will display all the elements of my
3835
02:27:20,800 --> 02:27:23,040
two-dimensional array apples oranges
3836
02:27:23,040 --> 02:27:24,800
bananas carrots onions potatoes eggs
3837
02:27:24,800 --> 02:27:26,560
chicken fish what if you need to change
3838
02:27:26,560 --> 02:27:27,680
one of these elements within your
3839
02:27:27,680 --> 02:27:29,359
two-dimensional array with a
3840
02:27:29,359 --> 02:27:31,359
two-dimensional array there are two
3841
02:27:31,359 --> 02:27:33,680
indices to access one of these elements
3842
02:27:33,680 --> 02:27:35,920
within a two-dimensional array
3843
02:27:35,920 --> 02:27:37,280
type the name of the two-dimensional
3844
02:27:37,280 --> 02:27:40,080
array and there's two indices so if we
3845
02:27:40,080 --> 02:27:41,840
take a look at the way that all of these
3846
02:27:41,840 --> 02:27:43,600
elements are set up
3847
02:27:43,600 --> 02:27:46,240
it kind of resembles a grid there's rows
3848
02:27:46,240 --> 02:27:49,200
and columns picturing a grid the first
3849
02:27:49,200 --> 02:27:52,000
index would be the row the second index
3850
02:27:52,000 --> 02:27:54,319
is the column i would like to replace
3851
02:27:54,319 --> 02:27:56,720
apples with mangoes i need to find the
3852
02:27:56,720 --> 02:27:58,960
row number and the column number so
3853
02:27:58,960 --> 02:28:00,720
computers they always start with zero
3854
02:28:00,720 --> 02:28:04,000
the first row would be row zero
3855
02:28:04,000 --> 02:28:06,240
and the first column would be column
3856
02:28:06,240 --> 02:28:07,200
zero
3857
02:28:07,200 --> 02:28:10,080
so the indices are zero zero and i will
3858
02:28:10,080 --> 02:28:11,760
set the sql to
3859
02:28:11,760 --> 02:28:13,600
mangoes i think that's how you spell
3860
02:28:13,600 --> 02:28:15,280
mangoes
3861
02:28:15,280 --> 02:28:17,760
yeah after refreshing we have mangoes
3862
02:28:17,760 --> 02:28:19,439
oranges bananas
3863
02:28:19,439 --> 02:28:21,280
let's replace bananas
3864
02:28:21,280 --> 02:28:24,560
that is row zero column two
3865
02:28:24,560 --> 02:28:28,399
zero one two so change the second index
3866
02:28:28,399 --> 02:28:29,680
to two
3867
02:28:29,680 --> 02:28:32,240
apples orange's main goes let's replace
3868
02:28:32,240 --> 02:28:34,319
eggs with steak
3869
02:28:34,319 --> 02:28:38,800
that would be row zero one two
3870
02:28:38,800 --> 02:28:41,840
column zero
3871
02:28:42,319 --> 02:28:44,880
steak chicken fish for my last example
3872
02:28:44,880 --> 02:28:47,760
let's replace fish with steak that is
3873
02:28:47,760 --> 02:28:52,080
row zero one two column zero one two
3874
02:28:52,080 --> 02:28:55,120
two two eggs chicken steak yeah
3875
02:28:55,120 --> 02:28:57,680
everybody that's basically a 2d array
3876
02:28:57,680 --> 02:29:00,240
it's an array of arrays it's useful if
3877
02:29:00,240 --> 02:29:02,080
you ever need some sort of grid of
3878
02:29:02,080 --> 02:29:04,080
information we probably won't be using
3879
02:29:04,080 --> 02:29:06,479
2d arrays too much in the future so yeah
3880
02:29:06,479 --> 02:29:08,479
those are 2d arrays hey if you found
3881
02:29:08,479 --> 02:29:10,160
this video helpful please be sure to
3882
02:29:10,160 --> 02:29:11,760
smash that like button leave random
3883
02:29:11,760 --> 02:29:13,359
comments down below and subscribe if
3884
02:29:13,359 --> 02:29:16,240
you'd like to become a fellow bro
3885
02:29:16,240 --> 02:29:18,160
hey y'all everybody in this video i'm
3886
02:29:18,160 --> 02:29:20,319
going to explain the spread operator it
3887
02:29:20,319 --> 02:29:23,359
allows an iterable such as an array or
3888
02:29:23,359 --> 02:29:25,840
string to be expanded in place where
3889
02:29:25,840 --> 02:29:28,240
zero or more arguments are expected
3890
02:29:28,240 --> 02:29:30,240
basically speaking when used with an
3891
02:29:30,240 --> 02:29:32,720
array it unpacks the elements in too
3892
02:29:32,720 --> 02:29:35,040
many individual pieces let me give you a
3893
02:29:35,040 --> 02:29:36,800
demonstration we have an array of
3894
02:29:36,800 --> 02:29:39,920
numbers let numbers equals
3895
02:29:39,920 --> 02:29:41,359
make up some numbers put them within
3896
02:29:41,359 --> 02:29:44,319
this array one two three four five six
3897
02:29:44,319 --> 02:29:45,680
seven eight
3898
02:29:45,680 --> 02:29:46,479
nine
3899
02:29:46,479 --> 02:29:49,520
using console.log if i was to display
3900
02:29:49,520 --> 02:29:51,760
this array directly
3901
02:29:51,760 --> 02:29:53,600
console.log numbers
3902
02:29:53,600 --> 02:29:55,760
this will display the details of this
3903
02:29:55,760 --> 02:29:57,680
array this array object
3904
02:29:57,680 --> 02:30:00,399
now if i was to precede my array with
3905
02:30:00,399 --> 02:30:02,080
the spread operator
3906
02:30:02,080 --> 02:30:05,200
which is three dots it would expand the
3907
02:30:05,200 --> 02:30:07,040
elements of this array we have the
3908
02:30:07,040 --> 02:30:09,439
elements one through nine actually you
3909
02:30:09,439 --> 02:30:10,960
can do this with a string too i'll give
3910
02:30:10,960 --> 02:30:12,319
you a demonstration
3911
02:30:12,319 --> 02:30:13,680
let
3912
02:30:13,680 --> 02:30:19,319
user name equal your first and last name
3913
02:30:19,680 --> 02:30:22,800
so i will use the spread operator
3914
02:30:22,800 --> 02:30:25,040
a string
3915
02:30:25,040 --> 02:30:26,960
and this will spread the string into
3916
02:30:26,960 --> 02:30:28,960
individual characters how could this be
3917
02:30:28,960 --> 02:30:30,880
useful let's say we need to find the
3918
02:30:30,880 --> 02:30:33,359
maximum value within this array well we
3919
02:30:33,359 --> 02:30:34,680
could use the
3920
02:30:34,680 --> 02:30:37,760
math.max method but with the max method
3921
02:30:37,760 --> 02:30:40,880
it accepts a varying number of arguments
3922
02:30:40,880 --> 02:30:44,000
we could pass in a value a variable like
3923
02:30:44,000 --> 02:30:47,200
x y z whatever the maximum is we would
3924
02:30:47,200 --> 02:30:49,040
return it and we can store that within a
3925
02:30:49,040 --> 02:30:50,640
variable let
3926
02:30:50,640 --> 02:30:52,080
maximum
3927
02:30:52,080 --> 02:30:55,040
equal math.max so what if we stick an
3928
02:30:55,040 --> 02:30:56,319
array in here
3929
02:30:56,319 --> 02:30:59,640
math.max numbers and then display it
3930
02:30:59,640 --> 02:31:01,520
console.log
3931
02:31:01,520 --> 02:31:04,399
maximum what happens exactly
3932
02:31:04,399 --> 02:31:07,359
uh so this returns not a number in place
3933
02:31:07,359 --> 02:31:09,439
of passing this array directly to a
3934
02:31:09,439 --> 02:31:12,240
method we could expand it by using the
3935
02:31:12,240 --> 02:31:14,560
spread operator so this will unpack
3936
02:31:14,560 --> 02:31:17,280
these elements into individual arguments
3937
02:31:17,280 --> 02:31:19,600
now when i run this we have our maximum
3938
02:31:19,600 --> 02:31:23,600
value of 9. if i added another like 10
3939
02:31:23,600 --> 02:31:25,680
well then it returns 10. so it's
3940
02:31:25,680 --> 02:31:28,479
fantastic if you need to pass in a
3941
02:31:28,479 --> 02:31:30,160
varying amount of arguments to a
3942
02:31:30,160 --> 02:31:32,160
function or method here's another
3943
02:31:32,160 --> 02:31:33,920
example we're a teacher and we have two
3944
02:31:33,920 --> 02:31:36,560
classes each represented by an array of
3945
02:31:36,560 --> 02:31:38,800
student names i need to merge these two
3946
02:31:38,800 --> 02:31:41,840
classes into one we'll create class 1
3947
02:31:41,840 --> 02:31:45,439
let class 1 equal some student names we
3948
02:31:45,439 --> 02:31:48,399
have spongebob
3949
02:31:48,479 --> 02:31:50,080
patrick
3950
02:31:50,080 --> 02:31:53,280
and sandy class 2 has these students
3951
02:31:53,280 --> 02:31:57,080
class 2 equals
3952
02:31:57,439 --> 02:31:59,760
squidward
3953
02:31:59,760 --> 02:32:02,240
mr krabs
3954
02:32:02,240 --> 02:32:03,920
and plankton
3955
02:32:03,920 --> 02:32:06,319
to add an element to an array you type
3956
02:32:06,319 --> 02:32:08,000
the array name
3957
02:32:08,000 --> 02:32:11,840
followed by the push method
3958
02:32:11,840 --> 02:32:13,760
and pass in an element if i would like
3959
02:32:13,760 --> 02:32:15,760
to add all of these students you would
3960
02:32:15,760 --> 02:32:17,520
think that i passed the array name as an
3961
02:32:17,520 --> 02:32:18,920
argument
3962
02:32:18,920 --> 02:32:21,680
class1.push class2
3963
02:32:21,680 --> 02:32:23,439
so let me show you what happens
3964
02:32:23,439 --> 02:32:25,920
console.log
3965
02:32:25,920 --> 02:32:27,920
class 1
3966
02:32:27,920 --> 02:32:30,640
so we have three students and an array
3967
02:32:30,640 --> 02:32:33,359
so we have an entire array as an element
3968
02:32:33,359 --> 02:32:35,680
which i didn't want to do so in place of
3969
02:32:35,680 --> 02:32:38,160
adding this class directly as an element
3970
02:32:38,160 --> 02:32:39,760
let's spread it into individual
3971
02:32:39,760 --> 02:32:42,640
arguments by using the spread operator
3972
02:32:42,640 --> 02:32:44,880
now when i display this array we have
3973
02:32:44,880 --> 02:32:47,280
six students spongebob patrick sandy
3974
02:32:47,280 --> 02:32:49,680
squidward mr krabs and plankton another
3975
02:32:49,680 --> 02:32:51,760
way to display this is to use the spread
3976
02:32:51,760 --> 02:32:53,439
operator
3977
02:32:53,439 --> 02:32:55,439
i will precede class 1 with the spread
3978
02:32:55,439 --> 02:32:56,479
operator
3979
02:32:56,479 --> 02:32:58,880
class 1 is divided into individual
3980
02:32:58,880 --> 02:33:00,880
elements spongebob patrick sandy
3981
02:33:00,880 --> 02:33:03,120
squidward mr krabs plankton
3982
02:33:03,120 --> 02:33:05,359
so the spread operator allows an
3983
02:33:05,359 --> 02:33:08,399
iterable such as an array or string to
3984
02:33:08,399 --> 02:33:11,280
be expanded in place where zero or more
3985
02:33:11,280 --> 02:33:13,680
arguments are expected by preceding an
3986
02:33:13,680 --> 02:33:16,080
array with the spread operator you
3987
02:33:16,080 --> 02:33:18,160
unpack the elements it's kind of like
3988
02:33:18,160 --> 02:33:19,920
you're opening a box and taking out
3989
02:33:19,920 --> 02:33:22,000
whatever's inside so that's the spread
3990
02:33:22,000 --> 02:33:23,920
operator if you would like a copy of
3991
02:33:23,920 --> 02:33:25,600
this code i'll post this in the comments
3992
02:33:25,600 --> 02:33:27,520
section down below don't be afraid to
3993
02:33:27,520 --> 02:33:29,120
smash that like button leave random
3994
02:33:29,120 --> 02:33:30,800
comments down below and subscribe if
3995
02:33:30,800 --> 02:33:34,319
you'd like to become a fellow bro
3996
02:33:34,319 --> 02:33:35,840
everybody in this video i'm going to
3997
02:33:35,840 --> 02:33:38,640
explain rest parameters rest parameters
3998
02:33:38,640 --> 02:33:41,120
represents an indefinite number of
3999
02:33:41,120 --> 02:33:43,680
parameters it packs arguments into an
4000
02:33:43,680 --> 02:33:45,439
array when you pass arguments to a
4001
02:33:45,439 --> 02:33:47,040
function here's how using rest
4002
02:33:47,040 --> 02:33:49,200
parameters would be useful i have five
4003
02:33:49,200 --> 02:33:52,080
different variables
4004
02:33:55,439 --> 02:33:57,840
i would like to sum a varying amount of
4005
02:33:57,840 --> 02:33:59,520
these variables
4006
02:33:59,520 --> 02:34:01,439
using console.log
4007
02:34:01,439 --> 02:34:04,000
i will invoke a sum function which we
4008
02:34:04,000 --> 02:34:06,160
will need to declare let's pass in
4009
02:34:06,160 --> 02:34:08,720
variables a and b to begin with i need a
4010
02:34:08,720 --> 02:34:10,720
matching function that is named sum that
4011
02:34:10,720 --> 02:34:13,280
accepts two arguments
4012
02:34:13,280 --> 02:34:15,359
function sum
4013
02:34:15,359 --> 02:34:18,560
arguments a and b we will simply return
4014
02:34:18,560 --> 02:34:22,000
a plus b so this returns three variables
4015
02:34:22,000 --> 02:34:23,920
a and b are arguments what if i would
4016
02:34:23,920 --> 02:34:25,600
like to add another argument
4017
02:34:25,600 --> 02:34:28,319
like c this specific sum function isn't
4018
02:34:28,319 --> 02:34:30,560
suitable for three arguments i would
4019
02:34:30,560 --> 02:34:33,120
need a sum function that accepts three
4020
02:34:33,120 --> 02:34:34,720
arguments this time
4021
02:34:34,720 --> 02:34:37,040
a b c
4022
02:34:37,040 --> 02:34:39,760
return a plus b plus c
4023
02:34:39,760 --> 02:34:41,920
then i could accept three arguments but
4024
02:34:41,920 --> 02:34:43,520
wait what if i have to pass in another
4025
02:34:43,520 --> 02:34:46,240
argument like d
4026
02:34:46,240 --> 02:34:48,399
well this function no longer is suitable
4027
02:34:48,399 --> 02:34:50,000
i would need to create another function
4028
02:34:50,000 --> 02:34:52,479
that accepts four arguments
4029
02:34:52,479 --> 02:34:54,319
a b c
4030
02:34:54,319 --> 02:34:57,920
d return a plus b plus c plus d you can
4031
02:34:57,920 --> 02:35:00,000
kind of see where i'm going with this we
4032
02:35:00,000 --> 02:35:02,080
have three different sum functions if
4033
02:35:02,080 --> 02:35:04,399
this time we need to pass in two again
4034
02:35:04,399 --> 02:35:06,080
two arguments
4035
02:35:06,080 --> 02:35:08,560
we receive not a number we would need to
4036
02:35:08,560 --> 02:35:10,319
give each of these functions a unique
4037
02:35:10,319 --> 02:35:12,479
name like sum2
4038
02:35:12,479 --> 02:35:14,800
sum3 sum4
4039
02:35:14,800 --> 02:35:16,640
but this isn't practical
4040
02:35:16,640 --> 02:35:18,080
wouldn't it be better if we had a
4041
02:35:18,080 --> 02:35:20,240
function that could accept an indefinite
4042
02:35:20,240 --> 02:35:22,319
number of parameters while we can with
4043
02:35:22,319 --> 02:35:24,000
the rest parameters
4044
02:35:24,000 --> 02:35:26,000
so let's get rid of all this code we
4045
02:35:26,000 --> 02:35:27,840
will declare a function that will accept
4046
02:35:27,840 --> 02:35:30,560
any number of arguments
4047
02:35:30,560 --> 02:35:32,000
function
4048
02:35:32,000 --> 02:35:34,319
sum
4049
02:35:34,399 --> 02:35:36,319
the rest parameters will pack these
4050
02:35:36,319 --> 02:35:38,240
arguments into an array
4051
02:35:38,240 --> 02:35:40,240
so we will use the three dots for the
4052
02:35:40,240 --> 02:35:42,560
rest parameters then we need a name for
4053
02:35:42,560 --> 02:35:45,359
this array like numbers
4054
02:35:45,359 --> 02:35:47,120
if our elements are within an array we
4055
02:35:47,120 --> 02:35:49,120
would just need some way to access those
4056
02:35:49,120 --> 02:35:50,640
elements there's a couple different ways
4057
02:35:50,640 --> 02:35:52,479
you could do this i'm going to create a
4058
02:35:52,479 --> 02:35:55,520
total let total equal zero and we will
4059
02:35:55,520 --> 02:35:57,520
iterate over our array
4060
02:35:57,520 --> 02:35:58,560
for
4061
02:35:58,560 --> 02:35:59,439
let
4062
02:35:59,439 --> 02:36:00,640
number
4063
02:36:00,640 --> 02:36:02,960
of our array numbers
4064
02:36:02,960 --> 02:36:06,800
we will take our total incremented by
4065
02:36:06,800 --> 02:36:10,640
our number and then at the end return
4066
02:36:10,640 --> 02:36:11,680
total
4067
02:36:11,680 --> 02:36:13,760
this time we can pass two arguments to
4068
02:36:13,760 --> 02:36:15,280
this function
4069
02:36:15,280 --> 02:36:17,439
or three
4070
02:36:17,439 --> 02:36:19,600
or four
4071
02:36:19,600 --> 02:36:21,280
five
4072
02:36:21,280 --> 02:36:23,200
doesn't matter so that's the benefit of
4073
02:36:23,200 --> 02:36:25,680
using rest parameters they represent an
4074
02:36:25,680 --> 02:36:28,080
indefinite number of parameters when you
4075
02:36:28,080 --> 02:36:30,399
call a function that has rest parameters
4076
02:36:30,399 --> 02:36:32,399
it will pack the individual arguments
4077
02:36:32,399 --> 02:36:34,160
into an array then you would just need
4078
02:36:34,160 --> 02:36:36,000
some way to access the elements of that
4079
02:36:36,000 --> 02:36:37,760
array you can mix and match the
4080
02:36:37,760 --> 02:36:40,800
parameters for example you could say x
4081
02:36:40,800 --> 02:36:41,840
and y
4082
02:36:41,840 --> 02:36:43,359
if i was to write it this way these
4083
02:36:43,359 --> 02:36:45,280
first two arguments will be x and y
4084
02:36:45,280 --> 02:36:47,280
respectively and any arguments
4085
02:36:47,280 --> 02:36:49,200
afterwards would be packed into this
4086
02:36:49,200 --> 02:36:51,520
array if you're mixing and matching rest
4087
02:36:51,520 --> 02:36:53,040
parameters along with some named
4088
02:36:53,040 --> 02:36:54,720
parameters make sure that the rest
4089
02:36:54,720 --> 02:36:56,880
parameters take up the last parameter
4090
02:36:56,880 --> 02:36:59,040
within this function so yeah that's rest
4091
02:36:59,040 --> 02:37:00,720
parameters if you would like a copy of
4092
02:37:00,720 --> 02:37:02,319
this code i'll post this to the comment
4093
02:37:02,319 --> 02:37:03,840
section down below if you haven't
4094
02:37:03,840 --> 02:37:05,600
already smash that like button leave
4095
02:37:05,600 --> 02:37:07,359
random comments down below and subscribe
4096
02:37:07,359 --> 02:37:10,640
if you'd like to become a fellow bro
4097
02:37:10,640 --> 02:37:12,319
hey guys in this video i'm going to
4098
02:37:12,319 --> 02:37:14,960
discuss callbacks a callback is a
4099
02:37:14,960 --> 02:37:17,280
function passed as an argument to
4100
02:37:17,280 --> 02:37:18,560
another function
4101
02:37:18,560 --> 02:37:20,640
using callbacks is a popular programming
4102
02:37:20,640 --> 02:37:22,399
technique because it ensures that a
4103
02:37:22,399 --> 02:37:24,319
function is not going to run before a
4104
02:37:24,319 --> 02:37:26,479
task is completed here's an example of
4105
02:37:26,479 --> 02:37:28,319
where a callback would be useful let's
4106
02:37:28,319 --> 02:37:30,240
say we need to add two numbers together
4107
02:37:30,240 --> 02:37:31,359
let
4108
02:37:31,359 --> 02:37:33,920
total equal sum
4109
02:37:33,920 --> 02:37:36,240
pass two numbers as arguments
4110
02:37:36,240 --> 02:37:37,840
maybe two and three i'll create a
4111
02:37:37,840 --> 02:37:39,760
function to add these numbers together
4112
02:37:39,760 --> 02:37:42,000
and return something we will declare a
4113
02:37:42,000 --> 02:37:44,080
function sum to add these two numbers
4114
02:37:44,080 --> 02:37:45,280
together
4115
02:37:45,280 --> 02:37:46,960
and set up some matching parameters to
4116
02:37:46,960 --> 02:37:50,479
these arguments let's name these x and y
4117
02:37:50,479 --> 02:37:54,560
let result equal x plus y
4118
02:37:54,560 --> 02:37:56,800
return result
4119
02:37:56,800 --> 02:37:58,720
then afterwards i would like to display
4120
02:37:58,720 --> 02:38:00,800
my result one function to display the
4121
02:38:00,800 --> 02:38:02,880
results to the console and the other to
4122
02:38:02,880 --> 02:38:05,120
the dom function
4123
02:38:05,120 --> 02:38:06,960
display
4124
02:38:06,960 --> 02:38:08,800
console
4125
02:38:08,800 --> 02:38:10,880
i'll name this argument
4126
02:38:10,880 --> 02:38:13,040
output
4127
02:38:13,040 --> 02:38:16,040
console.log
4128
02:38:17,040 --> 02:38:18,080
output
4129
02:38:18,080 --> 02:38:20,240
after finding a total we will invoke the
4130
02:38:20,240 --> 02:38:22,399
display console function and we will
4131
02:38:22,399 --> 02:38:24,960
pass our total as an argument this
4132
02:38:24,960 --> 02:38:27,359
displays the number 5 to the console now
4133
02:38:27,359 --> 02:38:28,800
let's create a function to display some
4134
02:38:28,800 --> 02:38:30,399
output to the dom
4135
02:38:30,399 --> 02:38:31,359
function
4136
02:38:31,359 --> 02:38:32,640
display
4137
02:38:32,640 --> 02:38:33,680
dom
4138
02:38:33,680 --> 02:38:35,680
we'll keep output as the argument
4139
02:38:35,680 --> 02:38:37,920
however we will change the inner html of
4140
02:38:37,920 --> 02:38:38,960
an element
4141
02:38:38,960 --> 02:38:41,520
so heading to our html file let's create
4142
02:38:41,520 --> 02:38:43,840
a label
4143
02:38:44,160 --> 02:38:45,600
close it
4144
02:38:45,600 --> 02:38:48,240
i'll give this an id of
4145
02:38:48,240 --> 02:38:49,280
my
4146
02:38:49,280 --> 02:38:50,720
label
4147
02:38:50,720 --> 02:38:53,439
save head back to your javascript file
4148
02:38:53,439 --> 02:38:56,240
and i would like to select my label
4149
02:38:56,240 --> 02:38:59,240
document.getelementbyid
4150
02:39:00,000 --> 02:39:03,359
the id of that label was my label
4151
02:39:03,359 --> 02:39:05,040
and i will set the
4152
02:39:05,040 --> 02:39:05,920
inner
4153
02:39:05,920 --> 02:39:09,760
html equal to the output that i receive
4154
02:39:09,760 --> 02:39:12,000
i will instead invoke the display dom
4155
02:39:12,000 --> 02:39:14,720
function display dom pass in total as an
4156
02:39:14,720 --> 02:39:17,120
argument and we have the number five
4157
02:39:17,120 --> 02:39:18,880
another way of writing this is that we
4158
02:39:18,880 --> 02:39:21,200
could use a callback we can pass a
4159
02:39:21,200 --> 02:39:23,439
function as an argument to another
4160
02:39:23,439 --> 02:39:25,600
function let's get rid of these lines we
4161
02:39:25,600 --> 02:39:27,439
won't need them anymore but we will keep
4162
02:39:27,439 --> 02:39:29,760
these two functions this time i'm going
4163
02:39:29,760 --> 02:39:32,080
to invoke a sum function
4164
02:39:32,080 --> 02:39:33,920
pass in my arguments
4165
02:39:33,920 --> 02:39:35,439
2 and 3
4166
02:39:35,439 --> 02:39:37,280
and also the name of a function i would
4167
02:39:37,280 --> 02:39:39,280
like to execute once we finish this
4168
02:39:39,280 --> 02:39:40,319
function
4169
02:39:40,319 --> 02:39:41,920
let's invoke the display console
4170
02:39:41,920 --> 02:39:44,560
function once we finish the sum function
4171
02:39:44,560 --> 02:39:46,399
so we will pass the name of this
4172
02:39:46,399 --> 02:39:48,720
function as an argument just be sure not
4173
02:39:48,720 --> 02:39:50,479
to add that set of parentheses after
4174
02:39:50,479 --> 02:39:52,240
because then you would be invoking that
4175
02:39:52,240 --> 02:39:54,880
function just the name of the function
4176
02:39:54,880 --> 02:39:57,760
next i need to set up this sum function
4177
02:39:57,760 --> 02:39:59,600
this specific function will have three
4178
02:39:59,600 --> 02:40:01,920
parameters the first two are the numbers
4179
02:40:01,920 --> 02:40:03,760
we would like to add together and the
4180
02:40:03,760 --> 02:40:06,479
third is a callback so let's define a
4181
02:40:06,479 --> 02:40:09,040
sum function function sum and there's
4182
02:40:09,040 --> 02:40:12,080
three parameters we'll name these x
4183
02:40:12,080 --> 02:40:13,120
y
4184
02:40:13,120 --> 02:40:14,560
and for the callback you can really name
4185
02:40:14,560 --> 02:40:15,840
it anything
4186
02:40:15,840 --> 02:40:17,680
i'll just name it callback and again
4187
02:40:17,680 --> 02:40:19,359
make sure not to add that extra set of
4188
02:40:19,359 --> 02:40:21,200
parentheses afterwards
4189
02:40:21,200 --> 02:40:23,439
so call back we'll add these two numbers
4190
02:40:23,439 --> 02:40:26,080
together let result
4191
02:40:26,080 --> 02:40:29,680
equal x plus y and then we will invoke
4192
02:40:29,680 --> 02:40:31,200
our callback so at the end of the
4193
02:40:31,200 --> 02:40:32,720
function type
4194
02:40:32,720 --> 02:40:34,640
callback remember that it stores a
4195
02:40:34,640 --> 02:40:36,720
function kind of like a variable then
4196
02:40:36,720 --> 02:40:38,720
add a set of parentheses then we'll be
4197
02:40:38,720 --> 02:40:40,800
invoking this function and if there's
4198
02:40:40,800 --> 02:40:42,080
any arguments
4199
02:40:42,080 --> 02:40:44,560
we have one parameter output make sure
4200
02:40:44,560 --> 02:40:45,840
to pass that in
4201
02:40:45,840 --> 02:40:48,880
so pass in result execute my callback
4202
02:40:48,880 --> 02:40:50,800
pass result as an argument
4203
02:40:50,800 --> 02:40:52,800
and this display is my output if i would
4204
02:40:52,800 --> 02:40:54,560
like to pivot and instead display my
4205
02:40:54,560 --> 02:40:56,000
output to the dom
4206
02:40:56,000 --> 02:40:58,000
i'll just pass in a different callback
4207
02:40:58,000 --> 02:41:00,800
display dom so we'll pivot and execute a
4208
02:41:00,800 --> 02:41:02,800
different function and our output is
4209
02:41:02,800 --> 02:41:04,560
displayed to the dom so you don't
4210
02:41:04,560 --> 02:41:06,319
necessarily need to name this parameter
4211
02:41:06,319 --> 02:41:07,439
callback
4212
02:41:07,439 --> 02:41:09,840
we could name this maybe
4213
02:41:09,840 --> 02:41:11,120
do something
4214
02:41:11,120 --> 02:41:13,359
at the end of the function do something
4215
02:41:13,359 --> 02:41:14,960
do some other function
4216
02:41:14,960 --> 02:41:18,640
or maybe my funk this would be valid too
4217
02:41:18,640 --> 02:41:21,120
so remember you can rename parameters so
4218
02:41:21,120 --> 02:41:23,200
using callbacks is an additional way in
4219
02:41:23,200 --> 02:41:25,359
which we can write code it ensures that
4220
02:41:25,359 --> 02:41:27,359
a function is not going to run before a
4221
02:41:27,359 --> 02:41:29,600
task is completed it helps us develop
4222
02:41:29,600 --> 02:41:31,840
asynchronous code when one function has
4223
02:41:31,840 --> 02:41:34,000
to wait for another function it helps us
4224
02:41:34,000 --> 02:41:36,319
avoid errors and potential problems for
4225
02:41:36,319 --> 02:41:38,399
example we could wait for a file to load
4226
02:41:38,399 --> 02:41:41,120
once the file loads then we do something
4227
02:41:41,120 --> 02:41:42,960
we'll have more practice with callbacks
4228
02:41:42,960 --> 02:41:44,880
in future videos so yeah that's a
4229
02:41:44,880 --> 02:41:46,800
callback everybody you can pass a
4230
02:41:46,800 --> 02:41:48,880
function as an argument to another
4231
02:41:48,880 --> 02:41:51,040
function and then execute it later hey
4232
02:41:51,040 --> 02:41:52,399
if this video helped you out you can
4233
02:41:52,399 --> 02:41:54,240
help me out by smashing that like button
4234
02:41:54,240 --> 02:41:55,840
leave a random comment down below and
4235
02:41:55,840 --> 02:41:57,200
subscribe if you'd like to become a
4236
02:41:57,200 --> 02:41:59,279
fellow bro
4237
02:41:59,279 --> 02:42:00,960
hey guys in this video i'm going to
4238
02:42:00,960 --> 02:42:03,760
discuss the for each method of arrays
4239
02:42:03,760 --> 02:42:06,399
the for each method executes a provided
4240
02:42:06,399 --> 02:42:08,720
callback function once for each array
4241
02:42:08,720 --> 02:42:12,000
element we have an array of students let
4242
02:42:12,000 --> 02:42:13,120
students
4243
02:42:13,120 --> 02:42:15,279
equals and make up some student names in
4244
02:42:15,279 --> 02:42:16,880
this example let's make all of the
4245
02:42:16,880 --> 02:42:18,399
letters lowercase we'll create a
4246
02:42:18,399 --> 02:42:20,319
function that will capitalize the first
4247
02:42:20,319 --> 02:42:22,319
letter of each student name so i'm going
4248
02:42:22,319 --> 02:42:25,600
to write spongebob all lowercase
4249
02:42:25,600 --> 02:42:26,880
patrick
4250
02:42:26,880 --> 02:42:28,640
and squidward
4251
02:42:28,640 --> 02:42:30,160
let's define a function that will
4252
02:42:30,160 --> 02:42:32,319
capitalize the first letter of each
4253
02:42:32,319 --> 02:42:34,560
string function
4254
02:42:34,560 --> 02:42:37,600
capital lies
4255
02:42:37,600 --> 02:42:40,399
now with the for each method there's up
4256
02:42:40,399 --> 02:42:42,640
to three arguments that are provided
4257
02:42:42,640 --> 02:42:44,319
automatically behind the scenes that we
4258
02:42:44,319 --> 02:42:46,160
have access to
4259
02:42:46,160 --> 02:42:47,680
an element
4260
02:42:47,680 --> 02:42:49,359
an index
4261
02:42:49,359 --> 02:42:51,840
and the array we don't necessarily need
4262
02:42:51,840 --> 02:42:54,160
to use any of these but they're provided
4263
02:42:54,160 --> 02:42:56,640
for us but in this function we will to
4264
02:42:56,640 --> 02:42:58,640
capitalize the first letter of each
4265
02:42:58,640 --> 02:43:01,040
string this is what we can write i would
4266
02:43:01,040 --> 02:43:04,399
like to access my array parameter at
4267
02:43:04,399 --> 02:43:05,520
index
4268
02:43:05,520 --> 02:43:07,279
of index
4269
02:43:07,279 --> 02:43:09,040
equals
4270
02:43:09,040 --> 02:43:11,200
our element
4271
02:43:11,200 --> 02:43:13,760
at index of zero
4272
02:43:13,760 --> 02:43:16,319
this will select the first character of
4273
02:43:16,319 --> 02:43:18,160
every string dot
4274
02:43:18,160 --> 02:43:19,760
follow this with the
4275
02:43:19,760 --> 02:43:21,040
two
4276
02:43:21,040 --> 02:43:23,840
uppercase method
4277
02:43:24,000 --> 02:43:25,439
plus
4278
02:43:25,439 --> 02:43:26,560
element
4279
02:43:26,560 --> 02:43:27,439
dot
4280
02:43:27,439 --> 02:43:29,920
sub string method
4281
02:43:29,920 --> 02:43:30,960
pass in
4282
02:43:30,960 --> 02:43:34,000
one this line right here will capitalize
4283
02:43:34,000 --> 02:43:36,319
the first letter of each string then i
4284
02:43:36,319 --> 02:43:38,040
will print one of these elements
4285
02:43:38,040 --> 02:43:42,160
console.log students at index of zero
4286
02:43:42,160 --> 02:43:44,479
now to use the for each method we will
4287
02:43:44,479 --> 02:43:46,800
type the name of the array
4288
02:43:46,800 --> 02:43:49,439
students dot for
4289
02:43:49,439 --> 02:43:50,800
each
4290
02:43:50,800 --> 02:43:53,520
then pass in a callback as an argument
4291
02:43:53,520 --> 02:43:55,520
we will apply this callback to every
4292
02:43:55,520 --> 02:43:58,399
element in an array the callback is the
4293
02:43:58,399 --> 02:44:00,479
name of the function capitalize make
4294
02:44:00,479 --> 02:44:02,319
sure to not add an additional set of
4295
02:44:02,319 --> 02:44:04,720
parentheses that will call the function
4296
02:44:04,720 --> 02:44:06,319
we're only passing the name of the
4297
02:44:06,319 --> 02:44:07,840
function as an argument this will
4298
02:44:07,840 --> 02:44:10,800
display the first student spongebob and
4299
02:44:10,800 --> 02:44:13,600
the first letter of his name is capital
4300
02:44:13,600 --> 02:44:15,120
let's do this again but create an
4301
02:44:15,120 --> 02:44:17,040
additional function to print each
4302
02:44:17,040 --> 02:44:19,600
student that's within my array
4303
02:44:19,600 --> 02:44:21,120
function
4304
02:44:21,120 --> 02:44:22,319
print
4305
02:44:22,319 --> 02:44:24,479
up to three arguments are provided for
4306
02:44:24,479 --> 02:44:27,680
us an element an index and an array we
4307
02:44:27,680 --> 02:44:29,760
don't necessarily need to use all three
4308
02:44:29,760 --> 02:44:31,680
i would just like to use element
4309
02:44:31,680 --> 02:44:33,520
and remember you can rename these two
4310
02:44:33,520 --> 02:44:37,359
such as x y or z but i would do
4311
02:44:37,359 --> 02:44:39,120
something that's descriptive but this
4312
02:44:39,120 --> 02:44:40,880
time we only need element
4313
02:44:40,880 --> 02:44:42,040
using
4314
02:44:42,040 --> 02:44:43,840
console.log
4315
02:44:43,840 --> 02:44:46,319
i am going to display each element and
4316
02:44:46,319 --> 02:44:49,120
again we will use the for each method
4317
02:44:49,120 --> 02:44:51,200
students dot for
4318
02:44:51,200 --> 02:44:54,880
each passing our callback of print
4319
02:44:54,880 --> 02:44:56,399
and here we go
4320
02:44:56,399 --> 02:44:59,359
yeah spongebob patrick squidward alright
4321
02:44:59,359 --> 02:45:02,000
everybody that is the for each method of
4322
02:45:02,000 --> 02:45:05,040
arrays it executes a provided callback
4323
02:45:05,040 --> 02:45:08,240
function once for each array element so
4324
02:45:08,240 --> 02:45:10,160
yeah everybody that is the for each
4325
02:45:10,160 --> 02:45:11,840
method of arrays if you would like a
4326
02:45:11,840 --> 02:45:13,520
copy of this code i'll post this in the
4327
02:45:13,520 --> 02:45:15,760
comment section down below and well yeah
4328
02:45:15,760 --> 02:45:18,479
that's the for each method of arrays in
4329
02:45:18,479 --> 02:45:20,960
javascript
4330
02:45:20,960 --> 02:45:23,120
hey everybody in this video we're going
4331
02:45:23,120 --> 02:45:25,840
to discuss the map method of arrays the
4332
02:45:25,840 --> 02:45:28,640
map method executes a provided callback
4333
02:45:28,640 --> 02:45:31,840
function once for each array element and
4334
02:45:31,840 --> 02:45:34,000
creates a new array here's an example
4335
02:45:34,000 --> 02:45:36,080
let's create an array of numbers
4336
02:45:36,080 --> 02:45:36,880
let
4337
02:45:36,880 --> 02:45:37,920
numbers
4338
02:45:37,920 --> 02:45:39,520
equal
4339
02:45:39,520 --> 02:45:43,439
one two three 4 5.
4340
02:45:43,439 --> 02:45:45,680
we're going to use the map method of
4341
02:45:45,680 --> 02:45:48,399
this array at numbers and the map method
4342
02:45:48,399 --> 02:45:51,200
will square each element in this array
4343
02:45:51,200 --> 02:45:53,680
and create a new array let's define a
4344
02:45:53,680 --> 02:45:55,439
square function
4345
02:45:55,439 --> 02:45:56,720
function
4346
02:45:56,720 --> 02:45:58,319
square
4347
02:45:58,319 --> 02:46:00,880
we need at least one parameter element
4348
02:46:00,880 --> 02:46:02,720
we will return
4349
02:46:02,720 --> 02:46:03,600
math
4350
02:46:03,600 --> 02:46:05,920
use the power method
4351
02:46:05,920 --> 02:46:08,800
pass in our element as a base raised to
4352
02:46:08,800 --> 02:46:10,240
the power of 2.
4353
02:46:10,240 --> 02:46:12,160
this is our square function
4354
02:46:12,160 --> 02:46:13,920
we will pass the name of this function
4355
02:46:13,920 --> 02:46:16,640
square as a callback to the map method
4356
02:46:16,640 --> 02:46:18,399
and this will create a new array so
4357
02:46:18,399 --> 02:46:20,399
let's define a new array
4358
02:46:20,399 --> 02:46:21,439
let
4359
02:46:21,439 --> 02:46:22,800
squares
4360
02:46:22,800 --> 02:46:26,080
equal to then i would like to invoke the
4361
02:46:26,080 --> 02:46:29,200
map method of my numbers array
4362
02:46:29,200 --> 02:46:30,240
numbers
4363
02:46:30,240 --> 02:46:31,359
dot map
4364
02:46:31,359 --> 02:46:34,080
pass in my callback of square and then i
4365
02:46:34,080 --> 02:46:35,760
would like to display the elements of
4366
02:46:35,760 --> 02:46:37,520
squares i'll do that with a print
4367
02:46:37,520 --> 02:46:38,880
function
4368
02:46:38,880 --> 02:46:40,160
function
4369
02:46:40,160 --> 02:46:42,319
print
4370
02:46:42,319 --> 02:46:44,160
element
4371
02:46:44,160 --> 02:46:47,160
console.log
4372
02:46:47,760 --> 02:46:49,439
element
4373
02:46:49,439 --> 02:46:51,920
and i will use the for each method of my
4374
02:46:51,920 --> 02:46:54,800
squares array squares
4375
02:46:54,800 --> 02:46:57,760
for each method pass in a callback of
4376
02:46:57,760 --> 02:46:59,600
print
4377
02:46:59,600 --> 02:47:00,960
let's take a look at the elements of
4378
02:47:00,960 --> 02:47:02,160
squares
4379
02:47:02,160 --> 02:47:04,880
yeah each element from my numbers array
4380
02:47:04,880 --> 02:47:07,600
was squared and added to a new array
4381
02:47:07,600 --> 02:47:08,640
squares
4382
02:47:08,640 --> 02:47:12,080
1 4 9 16 25. hey for fun let's create a
4383
02:47:12,080 --> 02:47:14,800
cube function
4384
02:47:14,960 --> 02:47:16,399
function
4385
02:47:16,399 --> 02:47:18,240
cube
4386
02:47:18,240 --> 02:47:21,600
we need at least an element parameter
4387
02:47:21,600 --> 02:47:23,120
copy this line
4388
02:47:23,120 --> 02:47:25,920
but change two to three
4389
02:47:25,920 --> 02:47:29,279
element to the power of three
4390
02:47:30,000 --> 02:47:31,439
let
4391
02:47:31,439 --> 02:47:32,479
cubes
4392
02:47:32,479 --> 02:47:33,600
equals
4393
02:47:33,600 --> 02:47:35,680
numbers dot map
4394
02:47:35,680 --> 02:47:36,640
cube
4395
02:47:36,640 --> 02:47:38,000
cubes
4396
02:47:38,000 --> 02:47:40,399
for each method pass in print as a
4397
02:47:40,399 --> 02:47:41,439
callback
4398
02:47:41,439 --> 02:47:43,520
and all of my original numbers are now
4399
02:47:43,520 --> 02:47:46,720
cubed and stored in a new array cubes 1
4400
02:47:46,720 --> 02:47:50,399
8 27 64 125 so yeah everybody that is
4401
02:47:50,399 --> 02:47:53,439
the map method of arrays it executes a
4402
02:47:53,439 --> 02:47:56,080
provided callback function once for each
4403
02:47:56,080 --> 02:47:59,040
array element and it creates an entirely
4404
02:47:59,040 --> 02:48:01,439
new array so yeah that's the map method
4405
02:48:01,439 --> 02:48:03,200
of arrays if you would like a copy of
4406
02:48:03,200 --> 02:48:04,880
this code i'll post this in the comments
4407
02:48:04,880 --> 02:48:07,200
section down below and well yeah that's
4408
02:48:07,200 --> 02:48:11,600
the map method of arrays in javascript
4409
02:48:11,600 --> 02:48:13,200
all right let's talk about the filter
4410
02:48:13,200 --> 02:48:15,439
method the filter method creates a new
4411
02:48:15,439 --> 02:48:18,560
array with all elements that pass a test
4412
02:48:18,560 --> 02:48:21,040
provided by a function we can filter out
4413
02:48:21,040 --> 02:48:22,960
elements from an array and create a new
4414
02:48:22,960 --> 02:48:24,960
one based on certain criteria let's
4415
02:48:24,960 --> 02:48:28,080
create an array of student ages
4416
02:48:28,080 --> 02:48:29,600
let
4417
02:48:29,600 --> 02:48:30,640
ages
4418
02:48:30,640 --> 02:48:32,160
equals
4419
02:48:32,160 --> 02:48:34,240
make up some student ages
4420
02:48:34,240 --> 02:48:35,680
18
4421
02:48:35,680 --> 02:48:37,200
16
4422
02:48:37,200 --> 02:48:38,560
21
4423
02:48:38,560 --> 02:48:40,080
17
4424
02:48:40,080 --> 02:48:41,600
19
4425
02:48:41,600 --> 02:48:43,760
90. i'm going to create a new array of
4426
02:48:43,760 --> 02:48:46,000
anybody that's 18 or older so i will
4427
02:48:46,000 --> 02:48:50,319
create a function to check age function
4428
02:48:50,319 --> 02:48:51,359
check
4429
02:48:51,359 --> 02:48:52,720
age
4430
02:48:52,720 --> 02:48:54,479
and there is one parameter
4431
02:48:54,479 --> 02:48:56,479
element this will create a new array we
4432
02:48:56,479 --> 02:48:58,720
will return and now we need some tests
4433
02:48:58,720 --> 02:49:00,640
to filter out any elements that don't
4434
02:49:00,640 --> 02:49:02,319
meet the criteria
4435
02:49:02,319 --> 02:49:04,160
our test will be
4436
02:49:04,160 --> 02:49:08,160
element is greater than or equal to 18.
4437
02:49:08,160 --> 02:49:10,319
so we will use this check age function
4438
02:49:10,319 --> 02:49:12,800
as a callback within the filter method
4439
02:49:12,800 --> 02:49:14,960
this will create a new array so let's
4440
02:49:14,960 --> 02:49:17,200
define that let
4441
02:49:17,200 --> 02:49:18,720
adults
4442
02:49:18,720 --> 02:49:20,000
equal
4443
02:49:20,000 --> 02:49:22,399
ages the original array
4444
02:49:22,399 --> 02:49:23,439
dot
4445
02:49:23,439 --> 02:49:24,720
filter
4446
02:49:24,720 --> 02:49:27,520
pass in the check age function as a
4447
02:49:27,520 --> 02:49:29,200
callback and then we just need to
4448
02:49:29,200 --> 02:49:30,640
display all of the elements of this
4449
02:49:30,640 --> 02:49:32,399
array
4450
02:49:32,399 --> 02:49:34,560
adults dot for
4451
02:49:34,560 --> 02:49:36,000
each
4452
02:49:36,000 --> 02:49:38,240
pass in a callback to
4453
02:49:38,240 --> 02:49:39,920
a print function which we'll define
4454
02:49:39,920 --> 02:49:41,920
momentarily
4455
02:49:41,920 --> 02:49:45,200
function print
4456
02:49:45,680 --> 02:49:46,960
element
4457
02:49:46,960 --> 02:49:49,920
console.log
4458
02:49:49,920 --> 02:49:51,120
element
4459
02:49:51,120 --> 02:49:54,560
so the elements in our new array are 18
4460
02:49:54,560 --> 02:49:57,840
21 1990 so yeah that's basically the
4461
02:49:57,840 --> 02:50:00,479
filter method it creates an entirely new
4462
02:50:00,479 --> 02:50:03,120
array with all the elements that pass a
4463
02:50:03,120 --> 02:50:05,439
test provided by a function and this
4464
02:50:05,439 --> 02:50:07,600
will leave the original array alone but
4465
02:50:07,600 --> 02:50:09,760
it creates an entirely new array of
4466
02:50:09,760 --> 02:50:12,319
elements that pass a test so yeah that's
4467
02:50:12,319 --> 02:50:14,080
the filter method everybody if you would
4468
02:50:14,080 --> 02:50:15,920
like a copy of this code i'll post this
4469
02:50:15,920 --> 02:50:17,600
in the comment section down below and
4470
02:50:17,600 --> 02:50:19,840
well yeah that's the filter method of
4471
02:50:19,840 --> 02:50:23,359
arrays in javascript
4472
02:50:23,359 --> 02:50:25,040
hey guys in this video i'm going to
4473
02:50:25,040 --> 02:50:27,200
explain the reduced method of arrays the
4474
02:50:27,200 --> 02:50:29,680
reduce method reduces an array to a
4475
02:50:29,680 --> 02:50:32,080
single value a very good use of the
4476
02:50:32,080 --> 02:50:35,040
reduce method would be to sum up all the
4477
02:50:35,040 --> 02:50:37,120
values of an array imagine we have an
4478
02:50:37,120 --> 02:50:39,279
online store and somebody has a bunch of
4479
02:50:39,279 --> 02:50:41,200
items in their cart we could sum up the
4480
02:50:41,200 --> 02:50:43,200
prices of all the items and create a
4481
02:50:43,200 --> 02:50:46,080
total let's create an array of prices
4482
02:50:46,080 --> 02:50:50,240
let prices equals make up some prices
4483
02:50:50,240 --> 02:50:52,960
five dollars ten dollars fifteen twenty
4484
02:50:52,960 --> 02:50:54,640
twenty five that's fine and i will
4485
02:50:54,640 --> 02:50:56,479
create a function that we will use as a
4486
02:50:56,479 --> 02:50:58,080
callback function
4487
02:50:58,080 --> 02:50:59,120
checkout
4488
02:50:59,120 --> 02:51:00,399
we'll call this function when we would
4489
02:51:00,399 --> 02:51:02,399
like to check out and make a purchase so
4490
02:51:02,399 --> 02:51:04,479
there are at least two parameters that
4491
02:51:04,479 --> 02:51:07,359
we need total this parameter will hold
4492
02:51:07,359 --> 02:51:10,000
the accumulated value as well as the
4493
02:51:10,000 --> 02:51:12,640
current element that we're working with
4494
02:51:12,640 --> 02:51:14,640
we will return
4495
02:51:14,640 --> 02:51:16,240
if we're trying to create a sum of all
4496
02:51:16,240 --> 02:51:17,439
the elements
4497
02:51:17,439 --> 02:51:19,200
total plus
4498
02:51:19,200 --> 02:51:22,080
element so when we return a value we
4499
02:51:22,080 --> 02:51:24,479
will reuse it as an argument for the
4500
02:51:24,479 --> 02:51:26,720
next iteration this is how we can reduce
4501
02:51:26,720 --> 02:51:28,560
all the elements of an array to a single
4502
02:51:28,560 --> 02:51:31,600
value let's create a total variable and
4503
02:51:31,600 --> 02:51:33,520
this will hold the final price that the
4504
02:51:33,520 --> 02:51:35,680
user has to pay to reduce all of these
4505
02:51:35,680 --> 02:51:38,080
prices to a single value we type the
4506
02:51:38,080 --> 02:51:40,479
name of the array use the built in
4507
02:51:40,479 --> 02:51:42,319
reduce method
4508
02:51:42,319 --> 02:51:46,240
and pass in the callback checkout
4509
02:51:46,240 --> 02:51:48,000
then i will just display what our total
4510
02:51:48,000 --> 02:51:50,800
is console.log
4511
02:51:50,800 --> 02:51:52,800
i'll use a template literal for this i
4512
02:51:52,800 --> 02:51:54,960
will use a set of backticks
4513
02:51:54,960 --> 02:51:56,479
the total
4514
02:51:56,479 --> 02:51:58,800
is
4515
02:51:59,439 --> 02:52:00,960
total
4516
02:52:00,960 --> 02:52:01,920
okay
4517
02:52:01,920 --> 02:52:05,040
so the total price is 75
4518
02:52:05,040 --> 02:52:07,040
let's add something else something that
4519
02:52:07,040 --> 02:52:08,800
is 30
4520
02:52:08,800 --> 02:52:11,279
now the total is 105
4521
02:52:11,279 --> 02:52:13,680
so that's the reduced method of arrays
4522
02:52:13,680 --> 02:52:16,720
it reduces an array to a single value a
4523
02:52:16,720 --> 02:52:19,520
fantastic use of the reduce method is to
4524
02:52:19,520 --> 02:52:22,160
sum up an array of values like items in
4525
02:52:22,160 --> 02:52:23,920
a shopping cart and create a total
4526
02:52:23,920 --> 02:52:25,760
that's the reduced method of arrays if
4527
02:52:25,760 --> 02:52:27,359
you would like a copy of this code i'll
4528
02:52:27,359 --> 02:52:28,720
post this in the comments section down
4529
02:52:28,720 --> 02:52:30,800
below and well yeah that's the reduced
4530
02:52:30,800 --> 02:52:34,960
method of arrays in javascript
4531
02:52:35,040 --> 02:52:36,880
hey guys in this video i'm going to show
4532
02:52:36,880 --> 02:52:38,960
you how we can sort an array of numbers
4533
02:52:38,960 --> 02:52:41,200
in javascript let's imagine that we're a
4534
02:52:41,200 --> 02:52:43,279
teacher and we have a student grade book
4535
02:52:43,279 --> 02:52:46,080
our task is to sort our students grades
4536
02:52:46,080 --> 02:52:48,240
in either ascending order or descending
4537
02:52:48,240 --> 02:52:50,960
order let's begin let's create an array
4538
02:52:50,960 --> 02:52:52,319
named grades
4539
02:52:52,319 --> 02:52:54,240
and make up some student grades i'm
4540
02:52:54,240 --> 02:52:56,479
thinking 100
4541
02:52:56,479 --> 02:52:58,080
50
4542
02:52:58,080 --> 02:52:59,279
90
4543
02:52:59,279 --> 02:53:00,479
60
4544
02:53:00,479 --> 02:53:01,359
80
4545
02:53:01,359 --> 02:53:03,279
70. let's create a function that will be
4546
02:53:03,279 --> 02:53:05,439
used as a callback to sort these numbers
4547
02:53:05,439 --> 02:53:07,359
in descending order so the greatest
4548
02:53:07,359 --> 02:53:09,439
number will be first the lowest number
4549
02:53:09,439 --> 02:53:10,880
will be last
4550
02:53:10,880 --> 02:53:13,279
function
4551
02:53:13,279 --> 02:53:14,720
let's name this
4552
02:53:14,720 --> 02:53:16,960
descending
4553
02:53:16,960 --> 02:53:19,279
sort
4554
02:53:19,600 --> 02:53:22,960
there's two parameters x and y all we
4555
02:53:22,960 --> 02:53:25,120
have to do is return
4556
02:53:25,120 --> 02:53:28,399
y minus x this will compare two values
4557
02:53:28,399 --> 02:53:30,720
at a time to sort this array and use
4558
02:53:30,720 --> 02:53:33,359
this callback function i will reassign
4559
02:53:33,359 --> 02:53:36,560
grades equal to grades
4560
02:53:36,560 --> 02:53:39,760
dot sort method and pass in a callback
4561
02:53:39,760 --> 02:53:42,080
descending sort and then we just need to
4562
02:53:42,080 --> 02:53:44,479
display our grades grades
4563
02:53:44,479 --> 02:53:47,760
i'll use the built-in for each method
4564
02:53:47,760 --> 02:53:50,319
and i will pass a callback of print then
4565
02:53:50,319 --> 02:53:53,120
we just need to define this function
4566
02:53:53,120 --> 02:53:54,479
function
4567
02:53:54,479 --> 02:53:55,680
print
4568
02:53:55,680 --> 02:53:57,680
there is one parameter
4569
02:53:57,680 --> 02:53:59,920
of element
4570
02:53:59,920 --> 02:54:02,800
console.log
4571
02:54:02,800 --> 02:54:04,960
element
4572
02:54:04,960 --> 02:54:06,960
now after running this
4573
02:54:06,960 --> 02:54:09,680
our grades are in descending order let's
4574
02:54:09,680 --> 02:54:11,439
create a callback function for ascending
4575
02:54:11,439 --> 02:54:12,479
order
4576
02:54:12,479 --> 02:54:15,040
so let's copy this paste it change
4577
02:54:15,040 --> 02:54:16,640
descending to
4578
02:54:16,640 --> 02:54:18,960
ascending return
4579
02:54:18,960 --> 02:54:21,520
x minus y
4580
02:54:21,520 --> 02:54:23,600
and for the callback we will pass the
4581
02:54:23,600 --> 02:54:26,399
argument ascending order
4582
02:54:26,399 --> 02:54:28,240
and our grades are now in ascending
4583
02:54:28,240 --> 02:54:30,880
order the lowest is first the highest is
4584
02:54:30,880 --> 02:54:33,120
last so yeah that's how to sort an array
4585
02:54:33,120 --> 02:54:34,880
of numbers if you would like a copy of
4586
02:54:34,880 --> 02:54:36,640
this code i'll post this in the comment
4587
02:54:36,640 --> 02:54:38,880
section down below and well yeah that's
4588
02:54:38,880 --> 02:54:40,720
how to sort an array of numbers in
4589
02:54:40,720 --> 02:54:43,200
javascript
4590
02:54:43,200 --> 02:54:44,880
hey guys in this video i'm going to
4591
02:54:44,880 --> 02:54:47,200
explain function expressions a function
4592
02:54:47,200 --> 02:54:49,760
expression is a function without a name
4593
02:54:49,760 --> 02:54:52,160
also known as an anonymous function a
4594
02:54:52,160 --> 02:54:54,399
benefit is that function expressions
4595
02:54:54,399 --> 02:54:56,319
help us to avoid polluting the global
4596
02:54:56,319 --> 02:54:59,040
scope with random function names we can
4597
02:54:59,040 --> 02:55:00,960
write a function and then forget about
4598
02:55:00,960 --> 02:55:02,399
it if i need to create a function to
4599
02:55:02,399 --> 02:55:04,560
display a greeting message using a
4600
02:55:04,560 --> 02:55:06,479
function declaration i would need to
4601
02:55:06,479 --> 02:55:09,359
think of a unique function name such as
4602
02:55:09,359 --> 02:55:12,160
function how about the say
4603
02:55:12,160 --> 02:55:14,840
hello function
4604
02:55:14,840 --> 02:55:17,600
console.log hello then to invoke this
4605
02:55:17,600 --> 02:55:19,279
function i just type the function name
4606
02:55:19,279 --> 02:55:21,200
followed by a set of parentheses boom we
4607
02:55:21,200 --> 02:55:23,439
have our function so it can get somewhat
4608
02:55:23,439 --> 02:55:25,520
tedious and annoying to think of unique
4609
02:55:25,520 --> 02:55:27,600
function names especially if the rest of
4610
02:55:27,600 --> 02:55:29,200
your program is not going to use it
4611
02:55:29,200 --> 02:55:30,720
another way that we could write this is
4612
02:55:30,720 --> 02:55:32,880
to use a function expression and we will
4613
02:55:32,880 --> 02:55:34,960
store that within a variable i will use
4614
02:55:34,960 --> 02:55:37,279
the const keyword this time const is
4615
02:55:37,279 --> 02:55:38,960
just a variable that you can't change
4616
02:55:38,960 --> 02:55:41,200
the value of later const
4617
02:55:41,200 --> 02:55:42,240
greeting
4618
02:55:42,240 --> 02:55:44,560
and i will set the sequel to a function
4619
02:55:44,560 --> 02:55:47,279
expression we will type function
4620
02:55:47,279 --> 02:55:50,240
parentheses curly braces and we do not
4621
02:55:50,240 --> 02:55:52,560
need to think of a unique function name
4622
02:55:52,560 --> 02:55:54,800
what would we like to do
4623
02:55:54,800 --> 02:55:57,800
console.log
4624
02:55:58,319 --> 02:56:00,800
hello then to invoke the function stored
4625
02:56:00,800 --> 02:56:02,880
within this variable i just type the
4626
02:56:02,880 --> 02:56:04,720
variable name followed by a set of
4627
02:56:04,720 --> 02:56:07,200
parentheses to invoke it
4628
02:56:07,200 --> 02:56:09,600
hello so there is no need to think of a
4629
02:56:09,600 --> 02:56:12,000
unique function name we can assign an
4630
02:56:12,000 --> 02:56:14,640
anonymous function to a variable or some
4631
02:56:14,640 --> 02:56:16,399
other entity which we'll discuss in the
4632
02:56:16,399 --> 02:56:18,319
future here's another example we'll
4633
02:56:18,319 --> 02:56:20,560
create a counter using two buttons we
4634
02:56:20,560 --> 02:56:22,720
can increase or decrease our counter
4635
02:56:22,720 --> 02:56:24,720
heading to our html file let's create a
4636
02:56:24,720 --> 02:56:26,560
label and two buttons
4637
02:56:26,560 --> 02:56:29,279
we'll need an opening label tag close it
4638
02:56:29,279 --> 02:56:30,720
add a line break
4639
02:56:30,720 --> 02:56:34,080
i will set the id of this label to my
4640
02:56:34,080 --> 02:56:35,040
label
4641
02:56:35,040 --> 02:56:37,200
i'll add some text
4642
02:56:37,200 --> 02:56:39,520
just zero
4643
02:56:39,520 --> 02:56:41,520
we'll need two buttons
4644
02:56:41,520 --> 02:56:42,800
button
4645
02:56:42,800 --> 02:56:45,359
close it
4646
02:56:45,439 --> 02:56:48,080
the idea of this first button will be
4647
02:56:48,080 --> 02:56:49,760
decrease
4648
02:56:49,760 --> 02:56:51,200
button
4649
02:56:51,200 --> 02:56:54,240
the text will be decreased
4650
02:56:54,240 --> 02:56:56,399
and we'll need an increase button
4651
02:56:56,399 --> 02:56:59,279
copy this paste it change decrease to
4652
02:56:59,279 --> 02:57:01,680
increase
4653
02:57:02,080 --> 02:57:04,240
and we have our two buttons
4654
02:57:04,240 --> 02:57:06,960
if we were using function declarations
4655
02:57:06,960 --> 02:57:08,960
we would need to think of two unique
4656
02:57:08,960 --> 02:57:11,840
names to link to these two buttons using
4657
02:57:11,840 --> 02:57:13,920
function declarations we would probably
4658
02:57:13,920 --> 02:57:15,840
say something like
4659
02:57:15,840 --> 02:57:17,279
function
4660
02:57:17,279 --> 02:57:18,560
increase
4661
02:57:18,560 --> 02:57:20,800
count
4662
02:57:20,800 --> 02:57:23,760
and we need a count variable let count
4663
02:57:23,760 --> 02:57:25,359
equal zero
4664
02:57:25,359 --> 02:57:27,680
we will increment count by one count
4665
02:57:27,680 --> 02:57:29,520
plus equals one
4666
02:57:29,520 --> 02:57:32,160
and change the text of this label so we
4667
02:57:32,160 --> 02:57:35,279
need to select it document dot
4668
02:57:35,279 --> 02:57:39,359
get element by id
4669
02:57:39,359 --> 02:57:42,720
we are selecting my label
4670
02:57:42,720 --> 02:57:46,240
change the inner html equal
4671
02:57:46,240 --> 02:57:47,600
to count
4672
02:57:47,600 --> 02:57:49,600
then we will create a decrease count
4673
02:57:49,600 --> 02:57:50,800
function
4674
02:57:50,800 --> 02:57:54,399
function decrease count count minus
4675
02:57:54,399 --> 02:57:56,560
equals one
4676
02:57:56,560 --> 02:57:58,319
lastly we just need to link these
4677
02:57:58,319 --> 02:58:01,040
buttons to these functions there is an
4678
02:58:01,040 --> 02:58:03,279
on click event attribute beginning with
4679
02:58:03,279 --> 02:58:04,880
the decrease button
4680
02:58:04,880 --> 02:58:07,680
take the on click attribute set the
4681
02:58:07,680 --> 02:58:10,720
sequel to the appropriate function
4682
02:58:10,720 --> 02:58:12,240
decrease count
4683
02:58:12,240 --> 02:58:14,479
add a set of parentheses
4684
02:58:14,479 --> 02:58:15,920
copy this
4685
02:58:15,920 --> 02:58:18,319
paste it for increase button and we will
4686
02:58:18,319 --> 02:58:20,880
select the increase count function
4687
02:58:20,880 --> 02:58:23,279
let's save and run this
4688
02:58:23,279 --> 02:58:25,920
so with this label we can increase it
4689
02:58:25,920 --> 02:58:28,319
and decrease it however i think this
4690
02:58:28,319 --> 02:58:29,760
would be better with function
4691
02:58:29,760 --> 02:58:32,000
expressions we don't need to declare two
4692
02:58:32,000 --> 02:58:34,640
functions such as increase count and
4693
02:58:34,640 --> 02:58:36,319
decrease count we can assign some
4694
02:58:36,319 --> 02:58:38,240
function expressions to these buttons
4695
02:58:38,240 --> 02:58:40,240
and then simply forget about them i'm
4696
02:58:40,240 --> 02:58:41,439
going to get rid of both of these
4697
02:58:41,439 --> 02:58:43,359
functions and we will create some
4698
02:58:43,359 --> 02:58:45,600
function expressions let's select this
4699
02:58:45,600 --> 02:58:47,040
increase button
4700
02:58:47,040 --> 02:58:50,640
i'll just copy what we have here
4701
02:58:52,160 --> 02:58:53,600
increase
4702
02:58:53,600 --> 02:58:55,279
button
4703
02:58:55,279 --> 02:58:56,319
dot
4704
02:58:56,319 --> 02:58:57,200
on
4705
02:58:57,200 --> 02:58:58,399
click
4706
02:58:58,399 --> 02:59:01,920
set the sequel to a function expression
4707
02:59:01,920 --> 02:59:03,840
what would we like to do
4708
02:59:03,840 --> 02:59:06,000
these two lines of code
4709
02:59:06,000 --> 02:59:08,240
increase count by one and change our
4710
02:59:08,240 --> 02:59:09,439
label
4711
02:59:09,439 --> 02:59:11,680
let's copy this function expression
4712
02:59:11,680 --> 02:59:12,880
paste it
4713
02:59:12,880 --> 02:59:14,720
do the same thing but with the decrease
4714
02:59:14,720 --> 02:59:17,200
button decrease button
4715
02:59:17,200 --> 02:59:19,359
decrement count by one and change our
4716
02:59:19,359 --> 02:59:20,399
label
4717
02:59:20,399 --> 02:59:22,560
we can get rid of this previous code
4718
02:59:22,560 --> 02:59:25,359
let's save heading back to our html file
4719
02:59:25,359 --> 02:59:27,200
we can also get rid of these on click
4720
02:59:27,200 --> 02:59:29,600
event attributes
4721
02:59:29,600 --> 02:59:31,840
okay save everything and this should
4722
02:59:31,840 --> 02:59:34,240
work we can increase our label and
4723
02:59:34,240 --> 02:59:36,319
decrease it and there is no need to
4724
02:59:36,319 --> 02:59:39,120
create two unique function names we were
4725
02:59:39,120 --> 02:59:41,359
able to assign function expressions to
4726
02:59:41,359 --> 02:59:44,160
the event attribute of an html element
4727
02:59:44,160 --> 02:59:46,399
so those are function expressions if you
4728
02:59:46,399 --> 02:59:48,160
would like a copy of this code i'll post
4729
02:59:48,160 --> 02:59:49,760
this in the comments section down below
4730
02:59:49,760 --> 02:59:51,680
don't be afraid to smash that like
4731
02:59:51,680 --> 02:59:53,520
button leave a random comment down below
4732
02:59:53,520 --> 02:59:55,040
and subscribe if you'd like to become a
4733
02:59:55,040 --> 02:59:57,359
fellow bro
4734
02:59:57,359 --> 02:59:58,960
hey guys in this video i'm going to
4735
02:59:58,960 --> 03:00:01,359
explain arrow function expressions which
4736
03:00:01,359 --> 03:00:03,920
are represented by well and arrow it's a
4737
03:00:03,920 --> 03:00:06,800
compact alternative to a traditional
4738
03:00:06,800 --> 03:00:08,560
function expression i'll give you a
4739
03:00:08,560 --> 03:00:10,080
whole bunch of examples of where this
4740
03:00:10,080 --> 03:00:11,760
could be useful let's create a
4741
03:00:11,760 --> 03:00:13,600
traditional function expression which we
4742
03:00:13,600 --> 03:00:15,520
learned about in the last topic
4743
03:00:15,520 --> 03:00:18,000
i'll create a constant
4744
03:00:18,000 --> 03:00:19,120
greeting
4745
03:00:19,120 --> 03:00:21,200
and i will set the sequel to a function
4746
03:00:21,200 --> 03:00:22,479
expression
4747
03:00:22,479 --> 03:00:25,359
function we have an argument
4748
03:00:25,359 --> 03:00:28,240
maybe username
4749
03:00:28,760 --> 03:00:30,479
console.log
4750
03:00:30,479 --> 03:00:32,640
will display a message i'll use a
4751
03:00:32,640 --> 03:00:34,720
template literal
4752
03:00:34,720 --> 03:00:36,880
hello
4753
03:00:36,880 --> 03:00:40,560
then my placeholder user name
4754
03:00:40,560 --> 03:00:42,640
so to invoke this function i type the
4755
03:00:42,640 --> 03:00:45,520
function name a set of parentheses and i
4756
03:00:45,520 --> 03:00:47,600
have one argument a username i'll type
4757
03:00:47,600 --> 03:00:49,760
in my first name run this
4758
03:00:49,760 --> 03:00:52,160
and this displays hello whatever your
4759
03:00:52,160 --> 03:00:53,680
username is
4760
03:00:53,680 --> 03:00:55,680
a compact alternative to this function
4761
03:00:55,680 --> 03:00:57,840
expression is that we could convert it
4762
03:00:57,840 --> 03:01:00,000
to an arrow function expression
4763
03:01:00,000 --> 03:01:02,160
we will eliminate these things
4764
03:01:02,160 --> 03:01:04,560
eliminate the function keyword after
4765
03:01:04,560 --> 03:01:07,439
your arguments add an arrow
4766
03:01:07,439 --> 03:01:10,319
and we can eliminate these curly braces
4767
03:01:10,319 --> 03:01:13,279
looks good to me does this still work
4768
03:01:13,279 --> 03:01:14,720
yes it does
4769
03:01:14,720 --> 03:01:16,560
so depending on what arguments you have
4770
03:01:16,560 --> 03:01:19,040
if you have no arguments you need just a
4771
03:01:19,040 --> 03:01:20,800
set of parentheses
4772
03:01:20,800 --> 03:01:22,800
if you have one argument you don't
4773
03:01:22,800 --> 03:01:24,960
necessarily need to enclose this in a
4774
03:01:24,960 --> 03:01:26,479
set of parentheses
4775
03:01:26,479 --> 03:01:28,800
if you have two or more arguments you do
4776
03:01:28,800 --> 03:01:30,640
need a set of parentheses
4777
03:01:30,640 --> 03:01:32,399
that is one example of the arrow
4778
03:01:32,399 --> 03:01:33,520
function
4779
03:01:33,520 --> 03:01:35,600
let's try a different example the second
4780
03:01:35,600 --> 03:01:38,319
example will have two arguments it will
4781
03:01:38,319 --> 03:01:40,240
calculate a percentage
4782
03:01:40,240 --> 03:01:43,200
first let's write a function expression
4783
03:01:43,200 --> 03:01:44,720
const
4784
03:01:44,720 --> 03:01:45,840
percent
4785
03:01:45,840 --> 03:01:48,560
equals a function expression there are
4786
03:01:48,560 --> 03:01:49,920
two arguments
4787
03:01:49,920 --> 03:01:51,200
x and y
4788
03:01:51,200 --> 03:01:53,439
x will be the denominator y will be the
4789
03:01:53,439 --> 03:01:56,800
denominator and i would like to return x
4790
03:01:56,800 --> 03:01:58,319
divided by y
4791
03:01:58,319 --> 03:02:00,160
times 100.
4792
03:02:00,160 --> 03:02:03,120
let's use console.log
4793
03:02:03,120 --> 03:02:05,439
i'll use a template literal for this i
4794
03:02:05,439 --> 03:02:07,279
would like to display here's my
4795
03:02:07,279 --> 03:02:08,800
placeholder
4796
03:02:08,800 --> 03:02:10,640
i'll invoke this function
4797
03:02:10,640 --> 03:02:11,680
percent
4798
03:02:11,680 --> 03:02:13,520
pass in two arguments
4799
03:02:13,520 --> 03:02:16,399
a nominator and a denominator
4800
03:02:16,399 --> 03:02:19,040
what is 75
4801
03:02:19,040 --> 03:02:21,120
over 100
4802
03:02:21,120 --> 03:02:24,000
then let's add percent to the end
4803
03:02:24,000 --> 03:02:25,600
so 75
4804
03:02:25,600 --> 03:02:28,640
over 100 is 75
4805
03:02:28,640 --> 03:02:33,319
what about 80 over 150
4806
03:02:33,319 --> 03:02:36,479
53.3 repeating percent in place of a
4807
03:02:36,479 --> 03:02:38,399
function expression let's use the arrow
4808
03:02:38,399 --> 03:02:39,760
function
4809
03:02:39,760 --> 03:02:42,080
so we eliminate the function keyword
4810
03:02:42,080 --> 03:02:45,520
keep the arguments add an arrow
4811
03:02:45,520 --> 03:02:48,560
and we can eliminate these curly braces
4812
03:02:48,560 --> 03:02:50,000
and we don't necessarily need this
4813
03:02:50,000 --> 03:02:52,240
return statement
4814
03:02:52,240 --> 03:02:54,000
and this still works the same
4815
03:02:54,000 --> 03:02:57,760
what about 45 over 50 that is 90
4816
03:02:57,760 --> 03:02:59,840
here's one last exercise
4817
03:02:59,840 --> 03:03:02,319
we'll reuse the code on the video on
4818
03:03:02,319 --> 03:03:04,720
sorting an array of integers in that
4819
03:03:04,720 --> 03:03:06,720
example we had an array of student
4820
03:03:06,720 --> 03:03:08,160
grades
4821
03:03:08,160 --> 03:03:10,160
and these are integers one student has
4822
03:03:10,160 --> 03:03:11,359
100
4823
03:03:11,359 --> 03:03:13,600
another has 50
4824
03:03:13,600 --> 03:03:14,720
90
4825
03:03:14,720 --> 03:03:16,000
60
4826
03:03:16,000 --> 03:03:16,960
80
4827
03:03:16,960 --> 03:03:18,560
70.
4828
03:03:18,560 --> 03:03:20,560
if i was using standard functions i
4829
03:03:20,560 --> 03:03:22,399
would write something like this
4830
03:03:22,399 --> 03:03:24,160
function
4831
03:03:24,160 --> 03:03:27,160
descending
4832
03:03:28,479 --> 03:03:32,000
there are two arguments x and y
4833
03:03:32,000 --> 03:03:36,080
return y minus x
4834
03:03:36,240 --> 03:03:39,120
and i will also create a print function
4835
03:03:39,120 --> 03:03:40,560
function
4836
03:03:40,560 --> 03:03:42,319
print
4837
03:03:42,319 --> 03:03:45,520
we have one argument an element
4838
03:03:45,520 --> 03:03:48,520
console.log
4839
03:03:49,600 --> 03:03:50,880
element
4840
03:03:50,880 --> 03:03:52,319
to sort and display each of these
4841
03:03:52,319 --> 03:03:53,840
elements i will use two separate
4842
03:03:53,840 --> 03:03:55,760
functions
4843
03:03:55,760 --> 03:03:57,920
grades dot sort
4844
03:03:57,920 --> 03:04:01,359
pass in a callback descending
4845
03:04:01,359 --> 03:04:03,600
and i would also like to print
4846
03:04:03,600 --> 03:04:06,080
grades for each
4847
03:04:06,080 --> 03:04:08,399
pass in a callback to print
4848
03:04:08,399 --> 03:04:10,319
so all of these grades are in descending
4849
03:04:10,319 --> 03:04:12,640
order if we were writing the same code
4850
03:04:12,640 --> 03:04:15,040
using function expressions i could stick
4851
03:04:15,040 --> 03:04:17,120
a function expression within each of
4852
03:04:17,120 --> 03:04:19,200
these methods let's turn this first
4853
03:04:19,200 --> 03:04:21,600
function into a function expression
4854
03:04:21,600 --> 03:04:22,800
cut it
4855
03:04:22,800 --> 03:04:25,439
and paste it within the sort method
4856
03:04:25,439 --> 03:04:27,040
and we will do the same thing with print
4857
03:04:27,040 --> 03:04:29,600
get rid of the name
4858
03:04:29,600 --> 03:04:31,600
cut this
4859
03:04:31,600 --> 03:04:33,279
and paste it
4860
03:04:33,279 --> 03:04:35,439
and that does the same thing but it uses
4861
03:04:35,439 --> 03:04:37,359
function expressions in place of
4862
03:04:37,359 --> 03:04:39,680
function expressions let's now use the
4863
03:04:39,680 --> 03:04:41,600
arrow function we get rid of the
4864
03:04:41,600 --> 03:04:44,399
function keyword
4865
03:04:44,479 --> 03:04:47,520
add an arrow after the arguments
4866
03:04:47,520 --> 03:04:51,840
and we can eliminate these curly braces
4867
03:04:52,880 --> 03:04:54,560
and we don't need the return keyword
4868
03:04:54,560 --> 03:04:56,319
either let's do the same thing with the
4869
03:04:56,319 --> 03:04:58,720
for each method to display each element
4870
03:04:58,720 --> 03:05:01,359
get rid of the function keyword
4871
03:05:01,359 --> 03:05:04,000
add an arrow after the arguments we can
4872
03:05:04,000 --> 03:05:06,160
eliminate the curly braces
4873
03:05:06,160 --> 03:05:08,000
and that semicolon
4874
03:05:08,000 --> 03:05:10,800
all right let's see if this works
4875
03:05:10,800 --> 03:05:13,200
i can't believe it it actually worked in
4876
03:05:13,200 --> 03:05:14,960
place of function expressions we were
4877
03:05:14,960 --> 03:05:17,120
able to instead use arrow function
4878
03:05:17,120 --> 03:05:19,760
expressions it's a compact alternative
4879
03:05:19,760 --> 03:05:22,319
to a traditional function expression
4880
03:05:22,319 --> 03:05:24,479
it makes your code a lot more readable
4881
03:05:24,479 --> 03:05:26,880
so yeah those were a few examples of us
4882
03:05:26,880 --> 03:05:28,800
using the arrow function you have your
4883
03:05:28,800 --> 03:05:31,359
arguments on one side arrow then some
4884
03:05:31,359 --> 03:05:33,359
code you would like to perform so yeah
4885
03:05:33,359 --> 03:05:35,279
that's the arrow function everybody if
4886
03:05:35,279 --> 03:05:36,880
you found this video helpful please be
4887
03:05:36,880 --> 03:05:38,720
sure to smash that like button leave a
4888
03:05:38,720 --> 03:05:40,479
random comment down below and subscribe
4889
03:05:40,479 --> 03:05:43,920
if you'd like to become a fellow bro
4890
03:05:43,920 --> 03:05:45,680
hey guys in this video i'm going to show
4891
03:05:45,680 --> 03:05:47,439
you how we can shuffle the elements of
4892
03:05:47,439 --> 03:05:49,600
an array this would work perfect for a
4893
03:05:49,600 --> 03:05:52,080
card game let's say we have an array of
4894
03:05:52,080 --> 03:05:53,359
cards
4895
03:05:53,359 --> 03:05:56,479
let cards equal
4896
03:05:56,479 --> 03:05:58,840
these will be string representations of
4897
03:05:58,840 --> 03:06:01,840
cards we have an ace
4898
03:06:01,840 --> 03:06:02,960
a two
4899
03:06:02,960 --> 03:06:05,040
i'm not worried about the suit but you
4900
03:06:05,040 --> 03:06:06,399
can feel free to add a suit if you would
4901
03:06:06,399 --> 03:06:10,160
like i'm just gonna fill in this array
4902
03:06:11,600 --> 03:06:12,840
then we have a
4903
03:06:12,840 --> 03:06:15,760
jack a queen
4904
03:06:15,760 --> 03:06:19,040
and a king so this is our array of cards
4905
03:06:19,040 --> 03:06:21,040
to shuffle this array i can create a
4906
03:06:21,040 --> 03:06:22,800
function to do that for me
4907
03:06:22,800 --> 03:06:23,920
function
4908
03:06:23,920 --> 03:06:24,960
shuffle
4909
03:06:24,960 --> 03:06:28,319
and there will be one parameter an array
4910
03:06:28,319 --> 03:06:30,080
when we invoke this function we need to
4911
03:06:30,080 --> 03:06:32,560
pass an array as an argument
4912
03:06:32,560 --> 03:06:35,920
shuffle our array cards now to shuffle
4913
03:06:35,920 --> 03:06:38,080
this we'll begin at the end i need this
4914
03:06:38,080 --> 03:06:40,000
array's length and i will store that
4915
03:06:40,000 --> 03:06:43,200
within a variable that we will name
4916
03:06:43,200 --> 03:06:44,479
current
4917
03:06:44,479 --> 03:06:46,960
index and set this equal to the arrays
4918
03:06:46,960 --> 03:06:50,319
length we will begin at the end
4919
03:06:50,319 --> 03:06:52,560
and we'll need a while loop
4920
03:06:52,560 --> 03:06:54,960
our condition is while
4921
03:06:54,960 --> 03:06:57,840
current index does not equal zero now
4922
03:06:57,840 --> 03:06:59,760
we'll need a random number between the
4923
03:06:59,760 --> 03:07:02,080
beginning of this array up to the length
4924
03:07:02,080 --> 03:07:05,680
of the array let's declare random
4925
03:07:05,680 --> 03:07:06,720
index
4926
03:07:06,720 --> 03:07:08,640
set the sql to
4927
03:07:08,640 --> 03:07:11,600
math math.random
4928
03:07:11,600 --> 03:07:12,640
times
4929
03:07:12,640 --> 03:07:15,680
the arrays length property
4930
03:07:15,680 --> 03:07:19,800
then surround this with math.floor
4931
03:07:19,800 --> 03:07:23,040
math.floor decrement our current index
4932
03:07:23,040 --> 03:07:26,800
by one current index minus equals one so
4933
03:07:26,800 --> 03:07:29,359
i'm going to maximize this
4934
03:07:29,359 --> 03:07:31,680
we will begin at the end we will swap
4935
03:07:31,680 --> 03:07:34,319
this element with another one during the
4936
03:07:34,319 --> 03:07:35,840
next iteration
4937
03:07:35,840 --> 03:07:37,920
the previous element will be the current
4938
03:07:37,920 --> 03:07:40,880
index we will swap this element with
4939
03:07:40,880 --> 03:07:42,960
another one chosen randomly we'll
4940
03:07:42,960 --> 03:07:45,040
continue this process until we reach the
4941
03:07:45,040 --> 03:07:47,439
end of our array then stop then it's a
4942
03:07:47,439 --> 03:07:48,960
matter of swapping two elements but
4943
03:07:48,960 --> 03:07:51,520
we'll need some temporary storage
4944
03:07:51,520 --> 03:07:52,560
let
4945
03:07:52,560 --> 03:07:53,600
temp
4946
03:07:53,600 --> 03:07:55,439
equal array
4947
03:07:55,439 --> 03:07:58,880
at our current index
4948
03:07:59,760 --> 03:08:00,960
array
4949
03:08:00,960 --> 03:08:04,080
at our current index
4950
03:08:04,080 --> 03:08:09,520
equals array at our random index
4951
03:08:11,439 --> 03:08:12,880
array
4952
03:08:12,880 --> 03:08:16,399
at our random index equals temp
4953
03:08:16,399 --> 03:08:17,760
at the end of this function you can
4954
03:08:17,760 --> 03:08:19,680
return the array if you like if you
4955
03:08:19,680 --> 03:08:21,760
would like to reassign it
4956
03:08:21,760 --> 03:08:24,000
i'm going to display all of the cards
4957
03:08:24,000 --> 03:08:26,160
the array itself
4958
03:08:26,160 --> 03:08:28,800
console.log
4959
03:08:28,800 --> 03:08:30,640
cards
4960
03:08:30,640 --> 03:08:33,120
so here's our array
4961
03:08:33,120 --> 03:08:34,880
every time i run this program these
4962
03:08:34,880 --> 03:08:37,200
elements are being shuffled
4963
03:08:37,200 --> 03:08:38,720
if you would like the top element that
4964
03:08:38,720 --> 03:08:40,800
would have an index of zero
4965
03:08:40,800 --> 03:08:42,800
the top card in my deck currently is an
4966
03:08:42,800 --> 03:08:44,000
eight
4967
03:08:44,000 --> 03:08:45,920
now it's a king
4968
03:08:45,920 --> 03:08:47,920
one trick if you would like to deal all
4969
03:08:47,920 --> 03:08:50,240
the cards is that you can use the for
4970
03:08:50,240 --> 03:08:52,080
each method of arrays
4971
03:08:52,080 --> 03:08:56,240
cards dot for each method we can pass in
4972
03:08:56,240 --> 03:08:58,800
a callback a function expression or an
4973
03:08:58,800 --> 03:09:00,640
arrow function expression
4974
03:09:00,640 --> 03:09:02,560
i'll use an arrow function expression we
4975
03:09:02,560 --> 03:09:05,840
have one argument let's name this card
4976
03:09:05,840 --> 03:09:07,200
arrow
4977
03:09:07,200 --> 03:09:09,000
i would like to display that card
4978
03:09:09,000 --> 03:09:11,439
console.log card
4979
03:09:11,439 --> 03:09:13,200
this will deal all of the cards in my
4980
03:09:13,200 --> 03:09:14,800
deck
4981
03:09:14,800 --> 03:09:17,920
and every time i run it it's shuffled
4982
03:09:17,920 --> 03:09:19,600
so yeah everybody that's one way in
4983
03:09:19,600 --> 03:09:21,359
which we can shuffle the elements of an
4984
03:09:21,359 --> 03:09:23,200
array if you would like a copy of this
4985
03:09:23,200 --> 03:09:24,800
code i'll post this in the comment
4986
03:09:24,800 --> 03:09:27,279
section down below and well yeah that's
4987
03:09:27,279 --> 03:09:29,200
how to shuffle the elements of an array
4988
03:09:29,200 --> 03:09:31,840
in javascript
4989
03:09:32,399 --> 03:09:34,239
hey yeah everybody in this video i'm
4990
03:09:34,239 --> 03:09:36,640
going to explain nested functions it's a
4991
03:09:36,640 --> 03:09:38,800
function inside of another function
4992
03:09:38,800 --> 03:09:41,120
think of the topic on nested loops it's
4993
03:09:41,120 --> 03:09:43,120
a loop inside of another loop outer
4994
03:09:43,120 --> 03:09:46,000
functions have access to inner functions
4995
03:09:46,000 --> 03:09:49,200
inner functions are hidden from outside
4996
03:09:49,200 --> 03:09:51,040
the outer function they add data
4997
03:09:51,040 --> 03:09:53,359
security and they're used in closures
4998
03:09:53,359 --> 03:09:55,760
which is a future video topic
4999
03:09:55,760 --> 03:09:57,680
so let's begin we'll use traditional
5000
03:09:57,680 --> 03:09:59,680
functions and then create some nested
5001
03:09:59,680 --> 03:10:02,640
functions i have two variables let
5002
03:10:02,640 --> 03:10:05,600
username equal some username
5003
03:10:05,600 --> 03:10:07,920
and a variable to hold the amount of
5004
03:10:07,920 --> 03:10:10,880
messages that i have like an inbox let
5005
03:10:10,880 --> 03:10:12,399
user inbox
5006
03:10:12,399 --> 03:10:13,439
equal
5007
03:10:13,439 --> 03:10:15,520
zero or some other number i'm going to
5008
03:10:15,520 --> 03:10:17,439
create a message to log in like we're
5009
03:10:17,439 --> 03:10:19,520
logging into our email
5010
03:10:19,520 --> 03:10:20,640
function
5011
03:10:20,640 --> 03:10:22,960
login i'll create a function to display
5012
03:10:22,960 --> 03:10:24,840
our username
5013
03:10:24,840 --> 03:10:26,960
function display
5014
03:10:26,960 --> 03:10:28,319
user
5015
03:10:28,319 --> 03:10:30,560
name
5016
03:10:30,800 --> 03:10:32,560
and another function to display our
5017
03:10:32,560 --> 03:10:34,720
inbox
5018
03:10:34,720 --> 03:10:36,560
user
5019
03:10:36,560 --> 03:10:39,359
inbox when i display my username let's
5020
03:10:39,359 --> 03:10:41,279
use console.log
5021
03:10:41,279 --> 03:10:44,560
i'll use a template literal
5022
03:10:44,560 --> 03:10:45,920
welcome
5023
03:10:45,920 --> 03:10:47,920
i'll add a placeholder
5024
03:10:47,920 --> 03:10:51,279
user name with the display user inbox
5025
03:10:51,279 --> 03:10:52,399
function
5026
03:10:52,399 --> 03:10:54,960
we will display how many messages we
5027
03:10:54,960 --> 03:10:56,640
have you
5028
03:10:56,640 --> 03:10:57,600
have
5029
03:10:57,600 --> 03:10:59,600
add a placeholder
5030
03:10:59,600 --> 03:11:02,160
user inbox
5031
03:11:02,160 --> 03:11:03,040
new
5032
03:11:03,040 --> 03:11:04,319
messages
5033
03:11:04,319 --> 03:11:06,239
after logging in i would like to invoke
5034
03:11:06,239 --> 03:11:09,680
these two functions display username
5035
03:11:09,680 --> 03:11:12,000
and display user inbox
5036
03:11:12,000 --> 03:11:14,560
and we need to invoke the login function
5037
03:11:14,560 --> 03:11:16,880
as if we were logging in welcome bro you
5038
03:11:16,880 --> 03:11:18,560
have zero new messages
5039
03:11:18,560 --> 03:11:20,800
now a problem with this we have access
5040
03:11:20,800 --> 03:11:23,359
to the display username function as well
5041
03:11:23,359 --> 03:11:25,920
as the display user inbox function from
5042
03:11:25,920 --> 03:11:28,479
outside of the login function meaning
5043
03:11:28,479 --> 03:11:30,239
that we don't need to log in in order to
5044
03:11:30,239 --> 03:11:32,880
display our username and our inbox so i
5045
03:11:32,880 --> 03:11:35,040
can invoke these functions directly
5046
03:11:35,040 --> 03:11:36,479
display username
5047
03:11:36,479 --> 03:11:38,560
display user inbox and there's no need
5048
03:11:38,560 --> 03:11:40,160
to even log in
5049
03:11:40,160 --> 03:11:42,880
welcome bro you have zero new messages
5050
03:11:42,880 --> 03:11:45,040
to add a little bit of data security i
5051
03:11:45,040 --> 03:11:47,279
could place these functions within
5052
03:11:47,279 --> 03:11:48,560
another function
5053
03:11:48,560 --> 03:11:50,720
so in order to access the display
5054
03:11:50,720 --> 03:11:55,120
username function i need to first log in
5055
03:11:55,120 --> 03:11:57,040
and the same thing goes with the display
5056
03:11:57,040 --> 03:12:00,160
user inbox function
5057
03:12:00,560 --> 03:12:03,120
if i attempt to invoke these functions
5058
03:12:03,120 --> 03:12:04,560
when they're nested
5059
03:12:04,560 --> 03:12:06,399
i don't have access to them it adds some
5060
03:12:06,399 --> 03:12:08,319
security in order to invoke these
5061
03:12:08,319 --> 03:12:11,439
functions i first need to login
5062
03:12:11,439 --> 03:12:14,080
so we'll invoke the login function
5063
03:12:14,080 --> 03:12:16,560
and then we have access to the display
5064
03:12:16,560 --> 03:12:18,880
username function and the display user
5065
03:12:18,880 --> 03:12:21,359
inbox function so yeah those are nested
5066
03:12:21,359 --> 03:12:23,520
functions they're functions inside of
5067
03:12:23,520 --> 03:12:25,920
other functions the outer function has
5068
03:12:25,920 --> 03:12:28,160
access to inner functions
5069
03:12:28,160 --> 03:12:30,720
inner functions are hidden from outside
5070
03:12:30,720 --> 03:12:32,720
the outer function it adds some data
5071
03:12:32,720 --> 03:12:34,640
security and they're also used in
5072
03:12:34,640 --> 03:12:37,840
closures which is a future video topic
5073
03:12:37,840 --> 03:12:40,160
so yeah those are nested functions if
5074
03:12:40,160 --> 03:12:41,760
you would like a copy of this code i'll
5075
03:12:41,760 --> 03:12:43,279
post this in the comments section down
5076
03:12:43,279 --> 03:12:45,760
below and well yeah those are nested
5077
03:12:45,760 --> 03:12:49,040
functions in javascript
5078
03:12:49,040 --> 03:12:50,720
hey guys in this video i'm going to
5079
03:12:50,720 --> 03:12:53,840
explain what a map is a map is an object
5080
03:12:53,840 --> 03:12:56,800
that holds key value pairs of any data
5081
03:12:56,800 --> 03:12:59,359
type here's an example we have an online
5082
03:12:59,359 --> 03:13:02,960
store we can associate prices with items
5083
03:13:02,960 --> 03:13:04,319
constant
5084
03:13:04,319 --> 03:13:05,600
store
5085
03:13:05,600 --> 03:13:07,680
equals new
5086
03:13:07,680 --> 03:13:08,560
map
5087
03:13:08,560 --> 03:13:11,120
add a set of parenthesis
5088
03:13:11,120 --> 03:13:14,920
a set of square brackets
5089
03:13:14,960 --> 03:13:16,720
and within the square brackets we can
5090
03:13:16,720 --> 03:13:19,120
create key value pairs add each key
5091
03:13:19,120 --> 03:13:21,200
value pair to a set of square brackets
5092
03:13:21,200 --> 03:13:23,680
and separate each pair with a comma the
5093
03:13:23,680 --> 03:13:26,080
first value is the key the first key
5094
03:13:26,080 --> 03:13:28,479
value pair is a t-shirt and the
5095
03:13:28,479 --> 03:13:30,960
associated value is twenty for twenty
5096
03:13:30,960 --> 03:13:33,520
dollars this is one pair in my map
5097
03:13:33,520 --> 03:13:35,920
object let's create another my second
5098
03:13:35,920 --> 03:13:36,720
pair
5099
03:13:36,720 --> 03:13:38,080
are jeans
5100
03:13:38,080 --> 03:13:40,560
they go for about thirty dollars maybe
5101
03:13:40,560 --> 03:13:44,080
the third pair in my map are socks
5102
03:13:44,080 --> 03:13:46,560
i'll sell them for ten dollars and what
5103
03:13:46,560 --> 03:13:48,800
about underwear they're expensive
5104
03:13:48,800 --> 03:13:50,560
they're fifty dollars they're very good
5105
03:13:50,560 --> 03:13:53,120
underwear okay here is my map object
5106
03:13:53,120 --> 03:13:55,680
it's named store to iterate over the
5107
03:13:55,680 --> 03:13:57,920
pairs in my map i can easily do this
5108
03:13:57,920 --> 03:14:01,120
using the for each method so type my map
5109
03:14:01,120 --> 03:14:03,439
name dot for
5110
03:14:03,439 --> 03:14:04,640
each
5111
03:14:04,640 --> 03:14:07,279
i can pass in a callback a function
5112
03:14:07,279 --> 03:14:09,439
expression or an arrow function
5113
03:14:09,439 --> 03:14:11,600
expression i'll use an arrow function
5114
03:14:11,600 --> 03:14:14,000
expression we have two parameters a
5115
03:14:14,000 --> 03:14:15,359
value
5116
03:14:15,359 --> 03:14:16,880
and a key
5117
03:14:16,880 --> 03:14:18,000
arrow
5118
03:14:18,000 --> 03:14:19,840
i would like to display my key value
5119
03:14:19,840 --> 03:14:20,760
pairs
5120
03:14:20,760 --> 03:14:24,479
console.log i'll use a template literal
5121
03:14:24,479 --> 03:14:26,399
i'll add a placeholder
5122
03:14:26,399 --> 03:14:27,680
key
5123
03:14:27,680 --> 03:14:30,319
and each value
5124
03:14:30,319 --> 03:14:31,600
value
5125
03:14:31,600 --> 03:14:33,760
and i'll add a dollar sign
5126
03:14:33,760 --> 03:14:35,920
so this is what i have currently
5127
03:14:35,920 --> 03:14:38,399
this line will iterate and display each
5128
03:14:38,399 --> 03:14:40,800
pair of my map and here is each pair
5129
03:14:40,800 --> 03:14:42,080
it's kind of like i'm listing all the
5130
03:14:42,080 --> 03:14:44,080
items in my store if i need to get one
5131
03:14:44,080 --> 03:14:45,600
of these items like i would like to make
5132
03:14:45,600 --> 03:14:48,160
a purchase there is a get method i'll
5133
03:14:48,160 --> 03:14:50,080
create a variable to store the prices
5134
03:14:50,080 --> 03:14:53,200
that are going to add up let shopping
5135
03:14:53,200 --> 03:14:56,080
cart i'll set the sequel to zero if i
5136
03:14:56,080 --> 03:14:57,680
would like to buy one of these items
5137
03:14:57,680 --> 03:14:59,760
there is an associated price and i just
5138
03:14:59,760 --> 03:15:02,560
need to access this value so i will take
5139
03:15:02,560 --> 03:15:05,520
shopping cart and increment it to access
5140
03:15:05,520 --> 03:15:07,600
one of these values i will type the name
5141
03:15:07,600 --> 03:15:10,000
of the map store get
5142
03:15:10,000 --> 03:15:11,760
and then a key i would like to buy a
5143
03:15:11,760 --> 03:15:13,200
t-shirt
5144
03:15:13,200 --> 03:15:14,720
so i will get
5145
03:15:14,720 --> 03:15:16,640
my t-shirt and let's display our
5146
03:15:16,640 --> 03:15:19,279
shopping cart console.log
5147
03:15:19,279 --> 03:15:20,720
shopping cart
5148
03:15:20,720 --> 03:15:22,960
currently our shopping cart is twenty we
5149
03:15:22,960 --> 03:15:24,720
have to pay twenty dollars i would like
5150
03:15:24,720 --> 03:15:26,640
to add another item
5151
03:15:26,640 --> 03:15:28,399
i want some of that fancy underwear
5152
03:15:28,399 --> 03:15:31,520
store dot get underwear my total is now
5153
03:15:31,520 --> 03:15:32,880
seventy dollars
5154
03:15:32,880 --> 03:15:35,520
twenty dollars plus fifty so that is the
5155
03:15:35,520 --> 03:15:38,160
get method here's a few other methods
5156
03:15:38,160 --> 03:15:40,399
there's a set method we can add a pair
5157
03:15:40,399 --> 03:15:42,239
to our map
5158
03:15:42,239 --> 03:15:43,359
store
5159
03:15:43,359 --> 03:15:46,399
dot set i would like to add a hat
5160
03:15:46,399 --> 03:15:49,920
and the price is 40. it's an expensive
5161
03:15:49,920 --> 03:15:52,239
hat it's gucci now when we iterate over
5162
03:15:52,239 --> 03:15:54,800
this map we have a t-shirt jeans socks
5163
03:15:54,800 --> 03:15:56,800
underwear and a hat that is the set
5164
03:15:56,800 --> 03:15:59,279
method we can delete a pair
5165
03:15:59,279 --> 03:16:03,200
store dot delete type in the key
5166
03:16:03,200 --> 03:16:04,239
hat
5167
03:16:04,239 --> 03:16:06,640
and our hat is gone we can check if
5168
03:16:06,640 --> 03:16:08,640
there's a key within our map type the
5169
03:16:08,640 --> 03:16:10,319
map name
5170
03:16:10,319 --> 03:16:11,359
has
5171
03:16:11,359 --> 03:16:14,239
and pass in a key do we have any hats
5172
03:16:14,239 --> 03:16:16,080
this will return a boolean value so i'm
5173
03:16:16,080 --> 03:16:17,600
just going to place this within a
5174
03:16:17,600 --> 03:16:20,720
console.log statement
5175
03:16:20,720 --> 03:16:23,760
do we have any hats store has hat
5176
03:16:23,760 --> 03:16:24,880
false
5177
03:16:24,880 --> 03:16:27,600
store has underwear
5178
03:16:27,600 --> 03:16:28,720
that's true
5179
03:16:28,720 --> 03:16:30,800
that is the haz method and the last one
5180
03:16:30,800 --> 03:16:33,359
i'll show you is the size property type
5181
03:16:33,359 --> 03:16:36,640
the map name store dot size
5182
03:16:36,640 --> 03:16:40,560
i'll put this within console.log
5183
03:16:41,279 --> 03:16:43,359
and the amount of pairs within our map
5184
03:16:43,359 --> 03:16:46,000
is four so yeah that's a map everybody
5185
03:16:46,000 --> 03:16:48,239
it's an object that holds key value
5186
03:16:48,239 --> 03:16:50,800
pairs of any data type you can associate
5187
03:16:50,800 --> 03:16:53,359
a key with the value one example that we
5188
03:16:53,359 --> 03:16:55,760
covered is if we had an online store we
5189
03:16:55,760 --> 03:16:58,319
could associate products with prices so
5190
03:16:58,319 --> 03:17:00,080
those are maps everybody hey if you
5191
03:17:00,080 --> 03:17:01,760
found this video helpful please be sure
5192
03:17:01,760 --> 03:17:03,600
to help me and smash that like button
5193
03:17:03,600 --> 03:17:05,120
leave a random comment down below and
5194
03:17:05,120 --> 03:17:06,479
subscribe if you'd like to become a
5195
03:17:06,479 --> 03:17:08,800
fellow bro
5196
03:17:08,800 --> 03:17:11,200
alright guys we have finally made it to
5197
03:17:11,200 --> 03:17:13,920
object-oriented programming an object is
5198
03:17:13,920 --> 03:17:16,640
a group of properties and methods
5199
03:17:16,640 --> 03:17:19,600
properties are what an object has like a
5200
03:17:19,600 --> 03:17:23,120
name a color a year methods are what an
5201
03:17:23,120 --> 03:17:25,120
object can do for example if we're
5202
03:17:25,120 --> 03:17:27,279
creating a car object two possible
5203
03:17:27,279 --> 03:17:29,920
methods could be drive and break so
5204
03:17:29,920 --> 03:17:31,920
let's create some car objects to begin
5205
03:17:31,920 --> 03:17:35,760
with i'll create a constant constant car
5206
03:17:35,760 --> 03:17:37,439
and this is how we can create an object
5207
03:17:37,439 --> 03:17:39,279
within a set of curly braces we can
5208
03:17:39,279 --> 03:17:41,279
create some properties which are kind of
5209
03:17:41,279 --> 03:17:44,160
like variables this car has a model
5210
03:17:44,160 --> 03:17:45,359
property
5211
03:17:45,359 --> 03:17:47,200
and we can assign a value
5212
03:17:47,200 --> 03:17:49,840
this car is a mustang
5213
03:17:49,840 --> 03:17:53,040
separate each property with a comma
5214
03:17:53,040 --> 03:17:55,520
let's create a color property the color
5215
03:17:55,520 --> 03:17:56,960
could be red
5216
03:17:56,960 --> 03:17:58,160
and a year
5217
03:17:58,160 --> 03:18:01,359
the year is 20 23.
5218
03:18:01,359 --> 03:18:03,760
we can also add some functions too i'll
5219
03:18:03,760 --> 03:18:06,399
create a drive function so create a
5220
03:18:06,399 --> 03:18:07,680
function name
5221
03:18:07,680 --> 03:18:09,520
colon
5222
03:18:09,520 --> 03:18:12,560
function parentheses curly braces
5223
03:18:12,560 --> 03:18:14,640
when we invoke the drive function we can
5224
03:18:14,640 --> 03:18:15,840
do something
5225
03:18:15,840 --> 03:18:17,520
i'll display a message
5226
03:18:17,520 --> 03:18:19,439
console.log
5227
03:18:19,439 --> 03:18:22,640
you drive the car let's create another
5228
03:18:22,640 --> 03:18:23,600
function
5229
03:18:23,600 --> 03:18:25,680
this will be the
5230
03:18:25,680 --> 03:18:29,279
break method so function parenthesis
5231
03:18:29,279 --> 03:18:30,880
curly braces
5232
03:18:30,880 --> 03:18:35,040
you step on the brakes
5233
03:18:35,040 --> 03:18:37,439
and there we go we have a car object
5234
03:18:37,439 --> 03:18:39,600
within a set of curly braces you can
5235
03:18:39,600 --> 03:18:42,560
assign properties and methods and then
5236
03:18:42,560 --> 03:18:44,160
at the end make sure you don't have a
5237
03:18:44,160 --> 03:18:46,880
comma an object can have properties what
5238
03:18:46,880 --> 03:18:50,399
an object has like a model a color and a
5239
03:18:50,399 --> 03:18:51,439
year
5240
03:18:51,439 --> 03:18:54,000
and methods what an object can do this
5241
03:18:54,000 --> 03:18:56,560
car can drive and it can break to access
5242
03:18:56,560 --> 03:18:58,239
an object's properties and methods
5243
03:18:58,239 --> 03:19:00,720
follow the object name with a dot if i
5244
03:19:00,720 --> 03:19:02,720
need access to the model of this car i
5245
03:19:02,720 --> 03:19:05,120
would type the object name
5246
03:19:05,120 --> 03:19:06,000
car
5247
03:19:06,000 --> 03:19:07,279
dot
5248
03:19:07,279 --> 03:19:08,319
model
5249
03:19:08,319 --> 03:19:10,000
and i will display this within
5250
03:19:10,000 --> 03:19:12,960
console.log but you can assign it change
5251
03:19:12,960 --> 03:19:14,880
it do whatever you want with it this
5252
03:19:14,880 --> 03:19:17,680
statement displays mustang
5253
03:19:17,680 --> 03:19:19,680
if i need the color property i would
5254
03:19:19,680 --> 03:19:21,600
type the object name
5255
03:19:21,600 --> 03:19:23,040
dot color
5256
03:19:23,040 --> 03:19:25,600
which is red
5257
03:19:25,600 --> 03:19:27,520
car dot year
5258
03:19:27,520 --> 03:19:30,160
is 20 23 if i would like to invoke one
5259
03:19:30,160 --> 03:19:31,920
of these methods found within my car
5260
03:19:31,920 --> 03:19:34,479
object i would type the object's name
5261
03:19:34,479 --> 03:19:36,160
followed by the method i would like to
5262
03:19:36,160 --> 03:19:37,520
perform
5263
03:19:37,520 --> 03:19:40,000
car dot drive add a set of parenthesis
5264
03:19:40,000 --> 03:19:41,279
to invoke it
5265
03:19:41,279 --> 03:19:43,200
you drive the car
5266
03:19:43,200 --> 03:19:46,720
then we have car dot break
5267
03:19:46,720 --> 03:19:48,960
you step on the brakes we can create
5268
03:19:48,960 --> 03:19:50,960
multiple objects each with different
5269
03:19:50,960 --> 03:19:53,120
properties and methods one way to do
5270
03:19:53,120 --> 03:19:54,960
this we haven't discussed classes or
5271
03:19:54,960 --> 03:19:56,880
constructors yet we can just create a
5272
03:19:56,880 --> 03:19:59,279
new name and we'll rename car as car 1
5273
03:19:59,279 --> 03:20:01,520
so we have two different car objects but
5274
03:20:01,520 --> 03:20:03,439
i'll change the properties of card too
5275
03:20:03,439 --> 03:20:06,000
we'll keep drive and break the same
5276
03:20:06,000 --> 03:20:08,479
this will be a corvette
5277
03:20:08,479 --> 03:20:12,560
the color is blue and the year is 20 24.
5278
03:20:12,560 --> 03:20:15,120
so this car is named car 2. in order to
5279
03:20:15,120 --> 03:20:17,120
access this car's model i would type the
5280
03:20:17,120 --> 03:20:18,479
name of the object
5281
03:20:18,479 --> 03:20:19,600
car 2
5282
03:20:19,600 --> 03:20:21,120
car 2. model
5283
03:20:21,120 --> 03:20:24,080
car 2.color car 2.ear
5284
03:20:24,080 --> 03:20:27,760
car 2 dot drive car 2 dot break
5285
03:20:27,760 --> 03:20:30,640
we have a corvette the color is blue
5286
03:20:30,640 --> 03:20:32,479
the year is 20 24.
5287
03:20:32,479 --> 03:20:34,479
when we invoke the drive method you
5288
03:20:34,479 --> 03:20:37,040
drive the car the brake method
5289
03:20:37,040 --> 03:20:38,239
displays
5290
03:20:38,239 --> 03:20:40,800
you step on the brakes so yeah those are
5291
03:20:40,800 --> 03:20:43,600
objects it's a group of properties and
5292
03:20:43,600 --> 03:20:45,840
methods that have a name properties are
5293
03:20:45,840 --> 03:20:48,399
what an object has methods are what an
5294
03:20:48,399 --> 03:20:50,160
object can do they're functions that
5295
03:20:50,160 --> 03:20:52,560
belong to an object to access properties
5296
03:20:52,560 --> 03:20:55,279
and methods of an object you use a dot
5297
03:20:55,279 --> 03:20:57,520
following the object name so those are
5298
03:20:57,520 --> 03:20:59,359
objects if you would like a copy of this
5299
03:20:59,359 --> 03:21:00,880
code i'll post this to the comment
5300
03:21:00,880 --> 03:21:02,479
section down below if you haven't
5301
03:21:02,479 --> 03:21:04,399
already please be sure to smash that
5302
03:21:04,399 --> 03:21:05,840
like button leave random comments down
5303
03:21:05,840 --> 03:21:07,439
below and subscribe if you'd like to
5304
03:21:07,439 --> 03:21:10,239
become a fellow bro
5305
03:21:10,239 --> 03:21:12,080
hey yeah everybody in this video i'm
5306
03:21:12,080 --> 03:21:15,120
going to explain the this keyword all it
5307
03:21:15,120 --> 03:21:18,080
is is a reference to a particular object
5308
03:21:18,080 --> 03:21:20,160
but it depends on the immediate context
5309
03:21:20,160 --> 03:21:22,640
where you use the this keyword here's an
5310
03:21:22,640 --> 03:21:26,000
example i have two objects car one and
5311
03:21:26,000 --> 03:21:29,600
car two car one is a red mustang car two
5312
03:21:29,600 --> 03:21:32,160
is a blue corvette each has a similar
5313
03:21:32,160 --> 03:21:33,439
drive function
5314
03:21:33,439 --> 03:21:34,960
you drive the car
5315
03:21:34,960 --> 03:21:36,640
in place of the word car why don't we
5316
03:21:36,640 --> 03:21:39,359
replace the word car with the model of
5317
03:21:39,359 --> 03:21:41,520
each car object i'm currently using a
5318
03:21:41,520 --> 03:21:43,920
template literal i could insert the
5319
03:21:43,920 --> 03:21:45,920
model of my car will create a
5320
03:21:45,920 --> 03:21:47,439
placeholder
5321
03:21:47,439 --> 03:21:49,200
this is what happens if i just type
5322
03:21:49,200 --> 03:21:51,279
model you think this would work because
5323
03:21:51,279 --> 03:21:53,120
this is like a variable right and let's
5324
03:21:53,120 --> 03:21:55,439
do the same thing with cartoon i will
5325
03:21:55,439 --> 03:21:57,680
invoke these two methods
5326
03:21:57,680 --> 03:22:00,000
car one dot drive
5327
03:22:00,000 --> 03:22:02,640
and cartoon.drive
5328
03:22:02,640 --> 03:22:05,200
okay we ran into an error model is not
5329
03:22:05,200 --> 03:22:08,479
defined at object drive within an object
5330
03:22:08,479 --> 03:22:09,840
if i would like to use one of these
5331
03:22:09,840 --> 03:22:12,479
properties i would need to precede this
5332
03:22:12,479 --> 03:22:15,920
property with the this keyword in place
5333
03:22:15,920 --> 03:22:19,840
of model i would type this dot model and
5334
03:22:19,840 --> 03:22:22,560
do the same thing with cartoon this
5335
03:22:22,560 --> 03:22:23,760
dot model
5336
03:22:23,760 --> 03:22:25,600
let's run this again you drive the
5337
03:22:25,600 --> 03:22:27,920
mustang you drive the corvette this
5338
03:22:27,920 --> 03:22:29,520
refers to the object that we're
5339
03:22:29,520 --> 03:22:31,439
currently working with the immediate
5340
03:22:31,439 --> 03:22:34,000
context within this object the immediate
5341
03:22:34,000 --> 03:22:37,439
context of this is car1 in this other
5342
03:22:37,439 --> 03:22:40,479
object the immediate context of this is
5343
03:22:40,479 --> 03:22:43,279
car two within an object when using the
5344
03:22:43,279 --> 03:22:45,279
this keyword just imagine that we're
5345
03:22:45,279 --> 03:22:48,160
replacing this with the object name
5346
03:22:48,160 --> 03:22:51,760
car1.model and cartoo.model
5347
03:22:51,760 --> 03:22:54,160
this will do the same thing so this is
5348
03:22:54,160 --> 03:22:55,920
just a reference to the object we're
5349
03:22:55,920 --> 03:22:57,359
working with let me show you what
5350
03:22:57,359 --> 03:22:59,840
happens if i use the this keyword
5351
03:22:59,840 --> 03:23:01,760
outside of any objects i will
5352
03:23:01,760 --> 03:23:04,760
console.log
5353
03:23:04,960 --> 03:23:07,359
this so what the heck is this we're
5354
03:23:07,359 --> 03:23:10,000
actually within the context of a window
5355
03:23:10,000 --> 03:23:12,399
object if you use the this keyword
5356
03:23:12,399 --> 03:23:14,560
outside of any objects that you declared
5357
03:23:14,560 --> 03:23:16,960
well then this refers to instead the
5358
03:23:16,960 --> 03:23:18,640
window object because that is the
5359
03:23:18,640 --> 03:23:20,720
immediate context and i can change one
5360
03:23:20,720 --> 03:23:22,800
of these properties if i wanted to let's
5361
03:23:22,800 --> 03:23:25,600
change this name property i would type
5362
03:23:25,600 --> 03:23:27,200
this
5363
03:23:27,200 --> 03:23:28,840
dot name
5364
03:23:28,840 --> 03:23:31,040
equals my
5365
03:23:31,040 --> 03:23:33,760
cool window
5366
03:23:33,760 --> 03:23:36,000
so i can run this
5367
03:23:36,000 --> 03:23:38,399
let's find that property
5368
03:23:38,399 --> 03:23:41,920
there's my name property my cool window
5369
03:23:41,920 --> 03:23:43,600
if i would like to access this property
5370
03:23:43,600 --> 03:23:47,040
directly i would follow this with dot
5371
03:23:47,040 --> 03:23:49,040
name
5372
03:23:49,040 --> 03:23:50,560
my cool window
5373
03:23:50,560 --> 03:23:52,720
so that's the this keyword everybody
5374
03:23:52,720 --> 03:23:55,520
it's a reference to a particular object
5375
03:23:55,520 --> 03:23:57,359
just imagine that we're replacing the
5376
03:23:57,359 --> 03:24:00,880
keyword this with the name of an object
5377
03:24:00,880 --> 03:24:02,319
hey if you found this video helpful
5378
03:24:02,319 --> 03:24:04,399
please be sure to smash that like button
5379
03:24:04,399 --> 03:24:06,000
leave a random comment down below and
5380
03:24:06,000 --> 03:24:07,279
subscribe if you'd like to become a
5381
03:24:07,279 --> 03:24:09,600
fellow bro
5382
03:24:09,600 --> 03:24:11,680
hey everybody in this video i'm going to
5383
03:24:11,680 --> 03:24:14,800
explain classes a class is a blueprint
5384
03:24:14,800 --> 03:24:17,600
for creating objects within a class we
5385
03:24:17,600 --> 03:24:19,920
can define what properties and methods
5386
03:24:19,920 --> 03:24:21,920
that type of object should have and they
5387
03:24:21,920 --> 03:24:24,080
typically contain a constructor to
5388
03:24:24,080 --> 03:24:26,080
assign some unique properties but we're
5389
03:24:26,080 --> 03:24:27,920
going to focus on constructors in the
5390
03:24:27,920 --> 03:24:29,600
next video i had some trouble
5391
03:24:29,600 --> 03:24:31,760
brainstorming some possible class ideas
5392
03:24:31,760 --> 03:24:33,760
that wouldn't utilize a constructor but
5393
03:24:33,760 --> 03:24:36,080
one that i thought of could be a player
5394
03:24:36,080 --> 03:24:37,920
we can create a player class as a
5395
03:24:37,920 --> 03:24:40,239
blueprint and create some player objects
5396
03:24:40,239 --> 03:24:43,920
so to create a class we will type class
5397
03:24:43,920 --> 03:24:45,120
player
5398
03:24:45,120 --> 03:24:47,200
curly braces and we can define a
5399
03:24:47,200 --> 03:24:49,600
properties and methods that all player
5400
03:24:49,600 --> 03:24:52,160
objects will have its blueprint maybe
5401
03:24:52,160 --> 03:24:54,319
each player has their own individual
5402
03:24:54,319 --> 03:24:56,800
score and i will set this to zero each
5403
03:24:56,800 --> 03:24:58,880
player will start at zero each player
5404
03:24:58,880 --> 03:25:01,520
has a pause method now when you declare
5405
03:25:01,520 --> 03:25:03,120
a method within a class you don't
5406
03:25:03,120 --> 03:25:05,359
necessarily need that function keyword
5407
03:25:05,359 --> 03:25:07,359
when a player pauses we will display a
5408
03:25:07,359 --> 03:25:10,960
message console.log
5409
03:25:10,960 --> 03:25:12,960
you paused
5410
03:25:12,960 --> 03:25:17,120
the game and maybe an exit method
5411
03:25:17,120 --> 03:25:19,120
console.log
5412
03:25:19,120 --> 03:25:19,920
you
5413
03:25:19,920 --> 03:25:22,880
exited the game now to utilize this
5414
03:25:22,880 --> 03:25:24,960
class to create an object we would need
5415
03:25:24,960 --> 03:25:28,399
an object name let's say constant
5416
03:25:28,399 --> 03:25:30,560
player1 this will be the first player
5417
03:25:30,560 --> 03:25:32,960
that joins now to utilize this class to
5418
03:25:32,960 --> 03:25:35,120
create a player object we use the new
5419
03:25:35,120 --> 03:25:37,040
keyword followed by the name of the
5420
03:25:37,040 --> 03:25:37,920
class
5421
03:25:37,920 --> 03:25:38,880
player
5422
03:25:38,880 --> 03:25:41,359
add a set of parentheses and a semicolon
5423
03:25:41,359 --> 03:25:43,520
we have created player one player one
5424
03:25:43,520 --> 03:25:46,160
has a score property and two methods
5425
03:25:46,160 --> 03:25:48,800
pause and exit let's display what player
5426
03:25:48,800 --> 03:25:51,319
one score is
5427
03:25:51,319 --> 03:25:52,960
console.log
5428
03:25:52,960 --> 03:25:55,040
player1.score
5429
03:25:55,040 --> 03:25:56,800
and that is currently zero but you can
5430
03:25:56,800 --> 03:26:00,399
adjust its score player one dot score
5431
03:26:00,399 --> 03:26:02,880
plus equals one he scored a point player
5432
03:26:02,880 --> 03:26:05,760
one now has one point player one dot
5433
03:26:05,760 --> 03:26:07,520
pause method
5434
03:26:07,520 --> 03:26:09,600
you pause the game and player one
5435
03:26:09,600 --> 03:26:13,600
connects it player one dot exit
5436
03:26:13,600 --> 03:26:15,600
you exited the game we can reuse this
5437
03:26:15,600 --> 03:26:18,479
class to create multiple players i'll
5438
03:26:18,479 --> 03:26:21,439
create player two constant player two
5439
03:26:21,439 --> 03:26:24,000
equals new player i'll display player
5440
03:26:24,000 --> 03:26:26,000
two score
5441
03:26:26,000 --> 03:26:27,920
and we will have player two exit the
5442
03:26:27,920 --> 03:26:29,120
game
5443
03:26:29,120 --> 03:26:30,960
player one has one point player two has
5444
03:26:30,960 --> 03:26:33,439
zero points player one invoked the pause
5445
03:26:33,439 --> 03:26:35,680
method player two invoked the exited
5446
03:26:35,680 --> 03:26:37,600
method you can keep on reusing this
5447
03:26:37,600 --> 03:26:39,279
class as much as you would like this
5448
03:26:39,279 --> 03:26:41,840
time i would like four players so same
5449
03:26:41,840 --> 03:26:44,319
process as before declare a unique
5450
03:26:44,319 --> 03:26:47,120
object name set the sql to use the new
5451
03:26:47,120 --> 03:26:49,279
keyword the name of the class that's a
5452
03:26:49,279 --> 03:26:51,359
class it's basically a blueprint for
5453
03:26:51,359 --> 03:26:53,600
creating objects we can create as many
5454
03:26:53,600 --> 03:26:55,840
objects as we want and within this class
5455
03:26:55,840 --> 03:26:58,000
we define what properties and methods
5456
03:26:58,000 --> 03:27:00,319
that each object created from this class
5457
03:27:00,319 --> 03:27:02,080
should have and in the next video we're
5458
03:27:02,080 --> 03:27:04,160
going to discuss constructors so we can
5459
03:27:04,160 --> 03:27:06,080
assign some unique properties to each
5460
03:27:06,080 --> 03:27:08,399
object so those are classes hey if you
5461
03:27:08,399 --> 03:27:10,399
liked this video let me know by smashing
5462
03:27:10,399 --> 03:27:11,920
that like button leave a random comment
5463
03:27:11,920 --> 03:27:13,680
down below and subscribe if you'd like
5464
03:27:13,680 --> 03:27:16,720
to become a fellow bro
5465
03:27:16,800 --> 03:27:19,040
all right everybody constructors a
5466
03:27:19,040 --> 03:27:21,520
constructor is a special method of a
5467
03:27:21,520 --> 03:27:24,640
class its job is to accept arguments and
5468
03:27:24,640 --> 03:27:26,640
assign properties or anything else you
5469
03:27:26,640 --> 03:27:28,319
would like to do when you instantiate an
5470
03:27:28,319 --> 03:27:30,720
object let's create a student class
5471
03:27:30,720 --> 03:27:31,840
class
5472
03:27:31,840 --> 03:27:34,160
student to create a constructor we will
5473
03:27:34,160 --> 03:27:35,520
type
5474
03:27:35,520 --> 03:27:36,880
constructor
5475
03:27:36,880 --> 03:27:39,279
parentheses curly braces now when we
5476
03:27:39,279 --> 03:27:41,200
create an object a student object in
5477
03:27:41,200 --> 03:27:42,880
this example we can assign some
5478
03:27:42,880 --> 03:27:46,080
properties each student has a name an
5479
03:27:46,080 --> 03:27:49,359
age and a gpa within the constructor we
5480
03:27:49,359 --> 03:27:51,200
can assign some of these arguments to
5481
03:27:51,200 --> 03:27:53,680
properties of this class we can state
5482
03:27:53,680 --> 03:27:54,640
this
5483
03:27:54,640 --> 03:27:55,600
dot
5484
03:27:55,600 --> 03:27:56,720
name
5485
03:27:56,720 --> 03:27:58,399
equals name
5486
03:27:58,399 --> 03:28:01,600
this dot age equals age
5487
03:28:01,600 --> 03:28:05,439
this dot gpa equals gpa
5488
03:28:05,439 --> 03:28:07,359
there we go there's our constructor you
5489
03:28:07,359 --> 03:28:09,600
can also add additional properties or
5490
03:28:09,600 --> 03:28:11,840
methods outside the constructor let's
5491
03:28:11,840 --> 03:28:14,080
add a study method
5492
03:28:14,080 --> 03:28:16,479
study
5493
03:28:17,200 --> 03:28:18,640
let's display
5494
03:28:18,640 --> 03:28:21,040
i'll use a template literal
5495
03:28:21,040 --> 03:28:23,520
this dot name
5496
03:28:23,520 --> 03:28:26,720
is studying
5497
03:28:26,720 --> 03:28:29,040
okay let's create some student objects
5498
03:28:29,040 --> 03:28:31,279
we can use var let or const i'll use
5499
03:28:31,279 --> 03:28:32,960
const
5500
03:28:32,960 --> 03:28:34,800
student one
5501
03:28:34,800 --> 03:28:36,319
equals new
5502
03:28:36,319 --> 03:28:37,600
student
5503
03:28:37,600 --> 03:28:39,120
within our constructor we have three
5504
03:28:39,120 --> 03:28:40,880
parameters that means that we need to
5505
03:28:40,880 --> 03:28:44,080
pass three arguments a name an age and a
5506
03:28:44,080 --> 03:28:45,920
gpa so that we can assign these
5507
03:28:45,920 --> 03:28:48,160
properties the first student will be
5508
03:28:48,160 --> 03:28:50,880
spongebob that's his name his age he'll
5509
03:28:50,880 --> 03:28:55,600
be 30 and his gpa he has a 3.2 gpa and i
5510
03:28:55,600 --> 03:28:59,560
can display these console.log
5511
03:29:00,040 --> 03:29:03,120
student1 dot name
5512
03:29:03,120 --> 03:29:06,680
his name is spongebob
5513
03:29:06,720 --> 03:29:09,920
age and a gpa spongebob his age is 30
5514
03:29:09,920 --> 03:29:13,359
his gpa is 3.2 and for fun let's invoke
5515
03:29:13,359 --> 03:29:16,000
the study method
5516
03:29:16,000 --> 03:29:20,160
student one dot study
5517
03:29:20,160 --> 03:29:22,080
spongebob is studying
5518
03:29:22,080 --> 03:29:24,000
now we can reuse this class as a
5519
03:29:24,000 --> 03:29:26,160
template to create other students and
5520
03:29:26,160 --> 03:29:28,160
pass some unique arguments to assign to
5521
03:29:28,160 --> 03:29:29,840
its properties
5522
03:29:29,840 --> 03:29:32,239
let's create student 2
5523
03:29:32,239 --> 03:29:34,479
student 2 equals new student the
5524
03:29:34,479 --> 03:29:36,960
student's name will be patrick
5525
03:29:36,960 --> 03:29:42,560
he will be 35 years old his gpa is 1.5
5526
03:29:42,560 --> 03:29:44,560
and we have access to student two's
5527
03:29:44,560 --> 03:29:46,319
properties and methods
5528
03:29:46,319 --> 03:29:50,319
student two dot name age gpa
5529
03:29:50,319 --> 03:29:52,800
and he has his own study method
5530
03:29:52,800 --> 03:29:56,640
okay patrick is 35 his gpa is 1.5 and
5531
03:29:56,640 --> 03:29:58,880
patrick is studying we can reuse this
5532
03:29:58,880 --> 03:30:00,880
class to create as many students as we
5533
03:30:00,880 --> 03:30:02,720
like
5534
03:30:02,720 --> 03:30:05,040
so lastly let's create one last student
5535
03:30:05,040 --> 03:30:06,560
student 3
5536
03:30:06,560 --> 03:30:09,359
student 3 will have a name of sandy
5537
03:30:09,359 --> 03:30:12,399
sandy will be 27
5538
03:30:12,399 --> 03:30:15,600
and sandy's gpa is of course a 4.0
5539
03:30:15,600 --> 03:30:17,520
student 3 has its own unique properties
5540
03:30:17,520 --> 03:30:18,880
and methods
5541
03:30:18,880 --> 03:30:21,520
a name age gpa
5542
03:30:21,520 --> 03:30:25,439
and study method we have sandy she's 27
5543
03:30:25,439 --> 03:30:28,479
her gpa is 4 well 4.0
5544
03:30:28,479 --> 03:30:30,479
and she has her own study method sandy
5545
03:30:30,479 --> 03:30:32,960
is studying so that's a constructor it's
5546
03:30:32,960 --> 03:30:35,680
a special type of method within a class
5547
03:30:35,680 --> 03:30:37,680
it accepts arguments and assigns
5548
03:30:37,680 --> 03:30:39,920
properties when you construct an object
5549
03:30:39,920 --> 03:30:41,840
so those are constructors hey if you
5550
03:30:41,840 --> 03:30:43,680
haven't already please be sure you smash
5551
03:30:43,680 --> 03:30:45,359
that like button leave random comments
5552
03:30:45,359 --> 03:30:47,279
down below and subscribe if you'd like
5553
03:30:47,279 --> 03:30:49,920
to become a fellow bro
5554
03:30:49,920 --> 03:30:51,439
hey guys in this video i'm going to
5555
03:30:51,439 --> 03:30:54,000
explain the static keyword a member that
5556
03:30:54,000 --> 03:30:56,319
is static whether it's a property or a
5557
03:30:56,319 --> 03:30:59,120
method belongs to a class and not any
5558
03:30:59,120 --> 03:31:01,200
objects created from that class in this
5559
03:31:01,200 --> 03:31:02,960
example i have a class car and a
5560
03:31:02,960 --> 03:31:05,120
constructor when we create a car object
5561
03:31:05,120 --> 03:31:07,359
we can pass in a model of a car so i
5562
03:31:07,359 --> 03:31:09,760
have three car objects suppose i would
5563
03:31:09,760 --> 03:31:12,160
like to keep track of how many cars i
5564
03:31:12,160 --> 03:31:13,680
have created we're entering some cars
5565
03:31:13,680 --> 03:31:15,520
into a race well we could create a
5566
03:31:15,520 --> 03:31:17,359
property to keep track of how many cars
5567
03:31:17,359 --> 03:31:18,960
that we have instantiated let me show
5568
03:31:18,960 --> 03:31:20,319
you why we would want to make this
5569
03:31:20,319 --> 03:31:22,720
static so we have a property
5570
03:31:22,720 --> 03:31:24,160
number
5571
03:31:24,160 --> 03:31:26,640
of cars and i will set this equal to
5572
03:31:26,640 --> 03:31:28,640
zero within the constructor we can add
5573
03:31:28,640 --> 03:31:30,640
additional code every time we create a
5574
03:31:30,640 --> 03:31:33,040
car let's increment cars by one number
5575
03:31:33,040 --> 03:31:35,680
of cars plus equals one now i will
5576
03:31:35,680 --> 03:31:38,000
precede number of cars with the this
5577
03:31:38,000 --> 03:31:39,760
keyword so what happens when we display
5578
03:31:39,760 --> 03:31:42,439
number of cars for each car
5579
03:31:42,439 --> 03:31:44,399
console.log
5580
03:31:44,399 --> 03:31:46,960
car one dot number of cars
5581
03:31:46,960 --> 03:31:49,520
car two car three
5582
03:31:49,520 --> 03:31:51,520
so the total number of cars within our
5583
03:31:51,520 --> 03:31:53,680
race is one i guess so the reason why
5584
03:31:53,680 --> 03:31:56,000
number of cars is in three is because
5585
03:31:56,000 --> 03:31:58,720
each car object has their own number of
5586
03:31:58,720 --> 03:32:01,359
cars variable each car object can share
5587
03:32:01,359 --> 03:32:03,600
the same property or method and we would
5588
03:32:03,600 --> 03:32:06,160
precede that property or method with the
5589
03:32:06,160 --> 03:32:08,479
static keyword instead of each car
5590
03:32:08,479 --> 03:32:10,640
having their own copy the car class
5591
03:32:10,640 --> 03:32:13,200
itself has the only copy then to
5592
03:32:13,200 --> 03:32:16,000
increment number of cars by one in place
5593
03:32:16,000 --> 03:32:17,760
of the this keyword i will use the name
5594
03:32:17,760 --> 03:32:20,560
of the class car the car classes number
5595
03:32:20,560 --> 03:32:23,279
of cars will be incremented by one every
5596
03:32:23,279 --> 03:32:25,279
time we call the car constructor there's
5597
03:32:25,279 --> 03:32:27,359
a problem here i'm accessing the number
5598
03:32:27,359 --> 03:32:29,920
of cars property but no one object from
5599
03:32:29,920 --> 03:32:31,520
this class has ownership of this
5600
03:32:31,520 --> 03:32:34,399
variable to access the static property
5601
03:32:34,399 --> 03:32:37,120
of number of cars i will use the name of
5602
03:32:37,120 --> 03:32:38,800
the class car
5603
03:32:38,800 --> 03:32:41,439
car dot number of cars how many cars
5604
03:32:41,439 --> 03:32:43,520
have we created we have created three
5605
03:32:43,520 --> 03:32:46,319
cars and i could create another
5606
03:32:46,319 --> 03:32:47,680
car for
5607
03:32:47,680 --> 03:32:49,200
is a
5608
03:32:49,200 --> 03:32:50,560
ferrari
5609
03:32:50,560 --> 03:32:52,399
now we have four cars
5610
03:32:52,399 --> 03:32:54,000
or even
5611
03:32:54,000 --> 03:32:55,279
five cars
5612
03:32:55,279 --> 03:32:57,680
a static property is useful for caches
5613
03:32:57,680 --> 03:32:59,840
or anything with a fixed configuration
5614
03:32:59,840 --> 03:33:01,520
we also have the capability to make
5615
03:33:01,520 --> 03:33:03,760
methods static it's more or less useful
5616
03:33:03,760 --> 03:33:06,000
for utility functions now we would like
5617
03:33:06,000 --> 03:33:07,920
to start our fictional race we could
5618
03:33:07,920 --> 03:33:10,560
create a start race method
5619
03:33:10,560 --> 03:33:11,680
start
5620
03:33:11,680 --> 03:33:14,000
race
5621
03:33:14,040 --> 03:33:15,600
console.log
5622
03:33:15,600 --> 03:33:16,800
three
5623
03:33:16,800 --> 03:33:18,319
two
5624
03:33:18,319 --> 03:33:19,600
one
5625
03:33:19,600 --> 03:33:22,720
go so currently this is not static so
5626
03:33:22,720 --> 03:33:24,960
any one of these cars can begin the race
5627
03:33:24,960 --> 03:33:26,319
on their own terms
5628
03:33:26,319 --> 03:33:29,040
car one wants to begin the race start
5629
03:33:29,040 --> 03:33:30,160
race
5630
03:33:30,160 --> 03:33:32,239
three two one go so i'm going to make
5631
03:33:32,239 --> 03:33:34,560
this a static method it's more or less a
5632
03:33:34,560 --> 03:33:36,160
utility function
5633
03:33:36,160 --> 03:33:39,200
so precede the method name with static
5634
03:33:39,200 --> 03:33:41,520
now to invoke this start race method
5635
03:33:41,520 --> 03:33:44,080
type the name of the class car car dot
5636
03:33:44,080 --> 03:33:46,399
start race three two one go
5637
03:33:46,399 --> 03:33:48,160
basically that's the static keyword
5638
03:33:48,160 --> 03:33:50,080
anything that's static belongs to the
5639
03:33:50,080 --> 03:33:52,319
class and not the objects one place
5640
03:33:52,319 --> 03:33:54,160
where we do see static methods is within
5641
03:33:54,160 --> 03:33:57,040
the math class say i need to round
5642
03:33:57,040 --> 03:33:59,840
math the name of the class dot round
5643
03:33:59,840 --> 03:34:00,800
method
5644
03:34:00,800 --> 03:34:02,640
i wouldn't need to create a math object
5645
03:34:02,640 --> 03:34:07,279
such as const math1 equals new math that
5646
03:34:07,279 --> 03:34:09,200
would be silly to access the round
5647
03:34:09,200 --> 03:34:10,800
method i would just type the name of the
5648
03:34:10,800 --> 03:34:12,000
class
5649
03:34:12,000 --> 03:34:14,239
dot round it's a static method that's
5650
03:34:14,239 --> 03:34:15,920
the static keyword everybody if you
5651
03:34:15,920 --> 03:34:17,600
would like a copy of this code i'll post
5652
03:34:17,600 --> 03:34:19,200
this to the comment section down below
5653
03:34:19,200 --> 03:34:21,520
and well yeah that's the static keyword
5654
03:34:21,520 --> 03:34:24,399
in javascript
5655
03:34:24,399 --> 03:34:26,239
hey y'all everybody in this video i'm
5656
03:34:26,239 --> 03:34:28,399
going to explain inheritance inheritance
5657
03:34:28,399 --> 03:34:30,800
is when a child class can inherit all
5658
03:34:30,800 --> 03:34:33,200
the methods and properties from another
5659
03:34:33,200 --> 03:34:35,439
class how is this useful let me give you
5660
03:34:35,439 --> 03:34:37,359
a demonstration our job is to create
5661
03:34:37,359 --> 03:34:41,279
three classes a class for rabbits fish
5662
03:34:41,279 --> 03:34:43,520
and hawks three specific kinds of
5663
03:34:43,520 --> 03:34:46,160
animals so we have class
5664
03:34:46,160 --> 03:34:47,279
rabbit
5665
03:34:47,279 --> 03:34:50,080
each rabbit will have a boolean variable
5666
03:34:50,080 --> 03:34:52,800
called alive and i will set this to true
5667
03:34:52,800 --> 03:34:55,040
if you're a rabbit object you're alive
5668
03:34:55,040 --> 03:34:58,880
and a name equal to rabbit
5669
03:34:58,880 --> 03:35:02,720
each rabbit should be able to eat
5670
03:35:02,720 --> 03:35:05,200
console.log
5671
03:35:05,200 --> 03:35:08,800
i'll use a template literal
5672
03:35:08,840 --> 03:35:11,760
this at a place holder
5673
03:35:11,760 --> 03:35:13,840
this dot name
5674
03:35:13,840 --> 03:35:16,399
is eating and all rabbits should be able
5675
03:35:16,399 --> 03:35:18,560
to sleep
5676
03:35:18,560 --> 03:35:20,560
sleep this
5677
03:35:20,560 --> 03:35:22,239
this dot name
5678
03:35:22,239 --> 03:35:23,520
is
5679
03:35:23,520 --> 03:35:24,560
sleeping
5680
03:35:24,560 --> 03:35:26,720
and one more how about run
5681
03:35:26,720 --> 03:35:29,600
run this this dot name
5682
03:35:29,600 --> 03:35:30,640
is
5683
03:35:30,640 --> 03:35:32,960
running this is our rabbit object so
5684
03:35:32,960 --> 03:35:35,520
let's copy this class paste it
5685
03:35:35,520 --> 03:35:39,040
and convert the second class to fish
5686
03:35:39,040 --> 03:35:42,560
alive will remain true name will be fish
5687
03:35:42,560 --> 03:35:44,960
eat and sleep will remain the same but
5688
03:35:44,960 --> 03:35:46,399
fish they don't run well because they
5689
03:35:46,399 --> 03:35:49,040
don't have legs they will instead have a
5690
03:35:49,040 --> 03:35:50,319
swim method
5691
03:35:50,319 --> 03:35:54,160
this this dot name is swimming
5692
03:35:54,160 --> 03:35:56,239
copy all this code
5693
03:35:56,239 --> 03:35:58,239
and we will create a hawk class this
5694
03:35:58,239 --> 03:36:01,040
will be the last class class
5695
03:36:01,040 --> 03:36:04,000
puck name is hawk
5696
03:36:04,000 --> 03:36:06,399
hawks will be able to eat and sleep in
5697
03:36:06,399 --> 03:36:08,319
place of a swim method they will have a
5698
03:36:08,319 --> 03:36:09,920
fly method
5699
03:36:09,920 --> 03:36:11,760
this this dot name
5700
03:36:11,760 --> 03:36:12,720
is
5701
03:36:12,720 --> 03:36:15,359
flying we have three separate classes
5702
03:36:15,359 --> 03:36:17,359
rabbit fish and hawk but with
5703
03:36:17,359 --> 03:36:19,600
programming we don't like to repeat code
5704
03:36:19,600 --> 03:36:21,359
and we're repeating quite a lot of code
5705
03:36:21,359 --> 03:36:23,920
within each of these classes each class
5706
03:36:23,920 --> 03:36:26,640
has an identical eat and sleep method as
5707
03:36:26,640 --> 03:36:29,359
well as in a live boolean variable so to
5708
03:36:29,359 --> 03:36:31,120
eliminate the need for us to repeat some
5709
03:36:31,120 --> 03:36:32,880
of this code we can simply create a
5710
03:36:32,880 --> 03:36:34,960
class and all of these individual animal
5711
03:36:34,960 --> 03:36:36,800
classes can inherit some properties and
5712
03:36:36,800 --> 03:36:39,279
methods from that one class so what do
5713
03:36:39,279 --> 03:36:41,120
all of these classes have in common and
5714
03:36:41,120 --> 03:36:43,200
a live boolean variable and an eat and
5715
03:36:43,200 --> 03:36:44,960
sleep method so why don't we create a
5716
03:36:44,960 --> 03:36:46,960
separate class and each of these classes
5717
03:36:46,960 --> 03:36:48,880
will inherit from that one class this
5718
03:36:48,880 --> 03:36:51,520
will be a parent class rabbit fish and
5719
03:36:51,520 --> 03:36:53,920
hawk will be children classes
5720
03:36:53,920 --> 03:36:55,920
so let's create class
5721
03:36:55,920 --> 03:36:57,600
animal that's descriptive of what they
5722
03:36:57,600 --> 03:37:00,640
all have in common class rabbit will
5723
03:37:00,640 --> 03:37:03,600
extends the animal class rabbit is the
5724
03:37:03,600 --> 03:37:06,239
child class animal is the parent class
5725
03:37:06,239 --> 03:37:09,520
do the same thing with fish
5726
03:37:09,520 --> 03:37:10,960
and hawk
5727
03:37:10,960 --> 03:37:12,640
any properties or methods that all of
5728
03:37:12,640 --> 03:37:14,479
these classes have in common we can put
5729
03:37:14,479 --> 03:37:16,880
within the parent class they each have
5730
03:37:16,880 --> 03:37:19,200
an alive property set to true we can
5731
03:37:19,200 --> 03:37:21,279
eliminate that for each class and stick
5732
03:37:21,279 --> 03:37:23,120
it within the parent class there's no
5733
03:37:23,120 --> 03:37:25,520
need to repeat this variable each time
5734
03:37:25,520 --> 03:37:27,200
they each have the same eat and sleep
5735
03:37:27,200 --> 03:37:29,200
methods
5736
03:37:29,200 --> 03:37:31,120
we can copy those
5737
03:37:31,120 --> 03:37:33,120
paste it within the animal class
5738
03:37:33,120 --> 03:37:34,640
and then eliminate each of these methods
5739
03:37:34,640 --> 03:37:37,920
within the children classes
5740
03:37:37,920 --> 03:37:39,760
so any properties or methods that are
5741
03:37:39,760 --> 03:37:42,000
unique to these children classes you
5742
03:37:42,000 --> 03:37:43,680
should keep within the children classes
5743
03:37:43,680 --> 03:37:45,439
rabbit inherits these properties and
5744
03:37:45,439 --> 03:37:47,439
methods from the animal class
5745
03:37:47,439 --> 03:37:49,200
fish does the same as well as well as
5746
03:37:49,200 --> 03:37:51,120
hawk and you can see that we eliminated
5747
03:37:51,120 --> 03:37:53,120
a lot of lines of code there was no need
5748
03:37:53,120 --> 03:37:54,960
for us to repeat ourselves let's create
5749
03:37:54,960 --> 03:37:56,640
some animal objects
5750
03:37:56,640 --> 03:38:01,920
const rabbit equals new rabbit
5751
03:38:02,560 --> 03:38:05,120
fish equals new
5752
03:38:05,120 --> 03:38:07,359
fish
5753
03:38:07,359 --> 03:38:10,319
hawk equals new hawk let's test to make
5754
03:38:10,319 --> 03:38:12,479
sure that each of these children classes
5755
03:38:12,479 --> 03:38:13,920
does in fact have access to these
5756
03:38:13,920 --> 03:38:15,920
properties and methods rabbits should
5757
03:38:15,920 --> 03:38:18,880
have in a live boolean variable
5758
03:38:18,880 --> 03:38:20,800
if you're a rabbit you're alive
5759
03:38:20,800 --> 03:38:23,920
congratulations console.log rabbit.alive
5760
03:38:23,920 --> 03:38:25,600
that is true
5761
03:38:25,600 --> 03:38:27,120
even though within the rabbit class we
5762
03:38:27,120 --> 03:38:29,120
didn't write in a live boolean variable
5763
03:38:29,120 --> 03:38:31,040
within the rabbit class itself it
5764
03:38:31,040 --> 03:38:33,359
inherited one from its parent and i bet
5765
03:38:33,359 --> 03:38:34,960
rabbits can also
5766
03:38:34,960 --> 03:38:37,120
eat
5767
03:38:37,120 --> 03:38:40,560
i bet they can sleep
5768
03:38:40,560 --> 03:38:43,359
and they can run
5769
03:38:45,520 --> 03:38:47,840
yep the rabbit is eating sleeping
5770
03:38:47,840 --> 03:38:51,520
running so let's test this with fish
5771
03:38:51,520 --> 03:38:54,560
fish not alive fish.eat fish.sleep what
5772
03:38:54,560 --> 03:38:56,479
would happen exactly if i attempt to
5773
03:38:56,479 --> 03:38:59,279
invoke the run method of fish fish.run
5774
03:38:59,279 --> 03:39:02,000
is not a function so this run method
5775
03:39:02,000 --> 03:39:04,160
belongs to the rabbit class fish don't
5776
03:39:04,160 --> 03:39:05,840
have a run method but what they have in
5777
03:39:05,840 --> 03:39:08,800
common are those eat and sleep methods
5778
03:39:08,800 --> 03:39:11,040
fish do have a swim method so the fish
5779
03:39:11,040 --> 03:39:12,640
is eating the fish is sleeping the fish
5780
03:39:12,640 --> 03:39:14,960
is swimming and let's test hawk
5781
03:39:14,960 --> 03:39:18,640
hawk dot alive hawk.e hawk.sleep
5782
03:39:18,640 --> 03:39:20,160
hawk.fly
5783
03:39:20,160 --> 03:39:22,560
okay true this hawk is eating this hawk
5784
03:39:22,560 --> 03:39:24,560
is sleeping this hawk is flying so
5785
03:39:24,560 --> 03:39:26,640
that's inheritance everybody a child
5786
03:39:26,640 --> 03:39:28,720
class can inherit all of the properties
5787
03:39:28,720 --> 03:39:30,800
and methods from a parrot class if you
5788
03:39:30,800 --> 03:39:33,520
have multiple classes that all have the
5789
03:39:33,520 --> 03:39:35,520
same properties and methods you could
5790
03:39:35,520 --> 03:39:36,960
stick those properties and methods
5791
03:39:36,960 --> 03:39:39,040
within a parent class and there's no
5792
03:39:39,040 --> 03:39:40,479
need to repeat those properties and
5793
03:39:40,479 --> 03:39:42,640
methods if they're identical not only
5794
03:39:42,640 --> 03:39:44,319
does this eliminate repetition when
5795
03:39:44,319 --> 03:39:45,760
you're coding but if you need to make
5796
03:39:45,760 --> 03:39:48,160
any changes to one of these methods it's
5797
03:39:48,160 --> 03:39:50,560
all in one place if my boss says i need
5798
03:39:50,560 --> 03:39:52,960
to change the eat method to
5799
03:39:52,960 --> 03:39:55,760
this this dot name is eating to
5800
03:39:55,760 --> 03:39:57,600
hunting i just have to make this change
5801
03:39:57,600 --> 03:39:59,680
in one place if we had an eat method
5802
03:39:59,680 --> 03:40:01,359
within each of these classes i would
5803
03:40:01,359 --> 03:40:03,120
have to go to each individual class and
5804
03:40:03,120 --> 03:40:05,040
make that change manually it's not bad
5805
03:40:05,040 --> 03:40:06,800
if there's three classes but imagine if
5806
03:40:06,800 --> 03:40:08,399
you had hundreds that would be a lot of
5807
03:40:08,399 --> 03:40:09,840
unnecessary work when you could just
5808
03:40:09,840 --> 03:40:11,680
make that change in one place so that's
5809
03:40:11,680 --> 03:40:13,359
inheritance everybody if you would like
5810
03:40:13,359 --> 03:40:15,120
a copy of this code i'll post this in
5811
03:40:15,120 --> 03:40:16,640
the comments section down below if you
5812
03:40:16,640 --> 03:40:18,239
haven't already please be sure you've
5813
03:40:18,239 --> 03:40:19,840
smashed that like button leave random
5814
03:40:19,840 --> 03:40:21,520
comments down below and subscribe if
5815
03:40:21,520 --> 03:40:24,399
you'd like to become a fellow bro
5816
03:40:24,399 --> 03:40:26,080
hey guys in this video i'm going to
5817
03:40:26,080 --> 03:40:28,399
explain the super key word the super
5818
03:40:28,399 --> 03:40:31,040
keyword when using inheritance refers to
5819
03:40:31,040 --> 03:40:32,479
the parent class
5820
03:40:32,479 --> 03:40:34,399
it's commonly used to invoke the
5821
03:40:34,399 --> 03:40:36,720
constructor of a parent class here's an
5822
03:40:36,720 --> 03:40:39,439
example i have three children classes
5823
03:40:39,439 --> 03:40:42,160
rabbit fish and hawk they all inherit
5824
03:40:42,160 --> 03:40:44,640
from the animal parent class suppose we
5825
03:40:44,640 --> 03:40:46,560
have a constructor within each of these
5826
03:40:46,560 --> 03:40:49,439
child classes
5827
03:40:49,439 --> 03:40:52,000
constructor rabbits will have these
5828
03:40:52,000 --> 03:40:53,439
three properties
5829
03:40:53,439 --> 03:40:54,560
a name
5830
03:40:54,560 --> 03:40:57,040
an age and a run
5831
03:40:57,040 --> 03:40:57,920
speed
5832
03:40:57,920 --> 03:40:59,840
so to assign these properties when we
5833
03:40:59,840 --> 03:41:02,000
instantiate a rabbit object we could
5834
03:41:02,000 --> 03:41:05,920
assign this dot name equals name
5835
03:41:05,920 --> 03:41:07,680
this dot age
5836
03:41:07,680 --> 03:41:11,600
equals age and this dot run speed
5837
03:41:11,600 --> 03:41:13,359
equals run speed
5838
03:41:13,359 --> 03:41:15,600
i'll copy this constructor
5839
03:41:15,600 --> 03:41:18,000
paste it within the fish child class
5840
03:41:18,000 --> 03:41:19,760
fish well they don't have a run speed
5841
03:41:19,760 --> 03:41:22,560
but they will have a swim speed property
5842
03:41:22,560 --> 03:41:24,399
swim speed
5843
03:41:24,399 --> 03:41:25,359
this
5844
03:41:25,359 --> 03:41:28,880
swim speed equals swim speed let's copy
5845
03:41:28,880 --> 03:41:30,560
our constructor paste it within hawk
5846
03:41:30,560 --> 03:41:34,640
change swim speed to fly speed
5847
03:41:34,640 --> 03:41:37,600
this fly speed equals fly speed let's
5848
03:41:37,600 --> 03:41:39,439
create some of these objects
5849
03:41:39,439 --> 03:41:42,800
const rabbit equals new
5850
03:41:42,800 --> 03:41:44,960
rabbit we have to pass in three
5851
03:41:44,960 --> 03:41:46,720
properties a name
5852
03:41:46,720 --> 03:41:49,199
an age and a run speed for the name
5853
03:41:49,199 --> 03:41:52,160
we'll say rabbit
5854
03:41:52,160 --> 03:41:55,359
age one one for one year old
5855
03:41:55,359 --> 03:41:57,760
and a run speed of 40 kilometers per
5856
03:41:57,760 --> 03:41:59,279
hour i don't know if that's accurate i
5857
03:41:59,279 --> 03:42:00,640
just made up a number
5858
03:42:00,640 --> 03:42:03,840
okay let's create a fish object
5859
03:42:03,840 --> 03:42:05,680
fish equals new
5860
03:42:05,680 --> 03:42:06,720
fish
5861
03:42:06,720 --> 03:42:09,680
the name is fish the age will be
5862
03:42:09,680 --> 03:42:11,439
i guess two
5863
03:42:11,439 --> 03:42:14,479
swim speed 80 kilometers per hour okay
5864
03:42:14,479 --> 03:42:16,160
then we have
5865
03:42:16,160 --> 03:42:19,040
hawk equals new hawk
5866
03:42:19,040 --> 03:42:20,560
pass in a name
5867
03:42:20,560 --> 03:42:21,439
hawk
5868
03:42:21,439 --> 03:42:24,800
and age maybe three and a fly speed
5869
03:42:24,800 --> 03:42:27,279
200 kilometers per hour
5870
03:42:27,279 --> 03:42:29,439
just to test everything i'm going to
5871
03:42:29,439 --> 03:42:31,680
display my rabbit's name
5872
03:42:31,680 --> 03:42:34,640
age and run speed
5873
03:42:34,640 --> 03:42:36,840
rabbit dot
5874
03:42:36,840 --> 03:42:40,399
name rabbit dot age
5875
03:42:40,399 --> 03:42:42,319
and rabbit dot
5876
03:42:42,319 --> 03:42:43,120
run
5877
03:42:43,120 --> 03:42:44,160
speed
5878
03:42:44,160 --> 03:42:46,239
okay let's take a look
5879
03:42:46,239 --> 03:42:48,399
what the heck so we encountered an
5880
03:42:48,399 --> 03:42:51,040
uncaught reference error must call super
5881
03:42:51,040 --> 03:42:53,359
constructor in derived class before
5882
03:42:53,359 --> 03:42:55,600
accessing this so if we have any
5883
03:42:55,600 --> 03:42:57,600
children classes and those children
5884
03:42:57,600 --> 03:42:59,840
classes have constructors we would want
5885
03:42:59,840 --> 03:43:02,319
to invoke the constructor of a parent
5886
03:43:02,319 --> 03:43:04,239
class one of the reasons why this would
5887
03:43:04,239 --> 03:43:06,080
be useful is that within each of these
5888
03:43:06,080 --> 03:43:08,000
constructors we're repeating some code
5889
03:43:08,000 --> 03:43:10,080
this dot name equals name and this dot
5890
03:43:10,080 --> 03:43:12,319
age equals edge but the run speed swim
5891
03:43:12,319 --> 03:43:14,560
speed and fly speed are all unique to
5892
03:43:14,560 --> 03:43:16,800
each of these classes to promote code
5893
03:43:16,800 --> 03:43:19,120
reusability we can call the constructor
5894
03:43:19,120 --> 03:43:21,359
of a parent class to assign the name and
5895
03:43:21,359 --> 03:43:23,279
age properties so within the animal
5896
03:43:23,279 --> 03:43:26,560
class i will create a constructor
5897
03:43:26,560 --> 03:43:28,720
we will have a name and age property and
5898
03:43:28,720 --> 03:43:30,479
then we'll assign them
5899
03:43:30,479 --> 03:43:34,399
this dot name equals name
5900
03:43:34,399 --> 03:43:38,319
this dot age equals age
5901
03:43:38,319 --> 03:43:40,720
we can eliminate these two lines
5902
03:43:40,720 --> 03:43:42,399
and when we construct a new rabbit
5903
03:43:42,399 --> 03:43:44,160
object we will first invoke the
5904
03:43:44,160 --> 03:43:47,439
superconstructor so you type super
5905
03:43:47,439 --> 03:43:48,800
then pass your arguments to the
5906
03:43:48,800 --> 03:43:51,040
superconstructor we will pass our name
5907
03:43:51,040 --> 03:43:53,279
and age and we can do this with the
5908
03:43:53,279 --> 03:43:55,680
other constructors
5909
03:43:55,680 --> 03:43:58,080
this eliminates some code repetition
5910
03:43:58,080 --> 03:43:59,920
and this should work so we have our
5911
03:43:59,920 --> 03:44:02,319
rabbit we have our name rabbit age is
5912
03:44:02,319 --> 03:44:04,319
one year old and the speed is 40
5913
03:44:04,319 --> 03:44:06,319
kilometers per hour let's try this with
5914
03:44:06,319 --> 03:44:10,080
fish fish dot name fish dot age fish dot
5915
03:44:10,080 --> 03:44:12,239
swim speed
5916
03:44:12,239 --> 03:44:14,479
the name of this fish is fish this fish
5917
03:44:14,479 --> 03:44:17,040
is two years old swim speed of 80. and
5918
03:44:17,040 --> 03:44:19,359
in our last example we have hawk
5919
03:44:19,359 --> 03:44:22,960
hawk.name hawk.h hawk.flyspeed
5920
03:44:22,960 --> 03:44:24,399
the name is hawk
5921
03:44:24,399 --> 03:44:26,640
this hawk is 3 years old and has a fly
5922
03:44:26,640 --> 03:44:28,800
speed of 200 kilometers per hour so the
5923
03:44:28,800 --> 03:44:30,960
super keyword refers to a parent class
5924
03:44:30,960 --> 03:44:32,560
it's commonly used to invoke the
5925
03:44:32,560 --> 03:44:34,560
constructor of a parent class it helps
5926
03:44:34,560 --> 03:44:36,399
with code reusability we could write
5927
03:44:36,399 --> 03:44:38,720
this code once and simply reuse it by
5928
03:44:38,720 --> 03:44:40,479
invoking the superconstructor of the
5929
03:44:40,479 --> 03:44:42,960
parent class then if any class has any
5930
03:44:42,960 --> 03:44:45,040
unique properties you can assign those
5931
03:44:45,040 --> 03:44:47,040
unique properties within the constructor
5932
03:44:47,040 --> 03:44:49,199
of each of these child classes so that
5933
03:44:49,199 --> 03:44:51,359
is the super keyword it just refers to
5934
03:44:51,359 --> 03:44:52,880
the parent class if you would like a
5935
03:44:52,880 --> 03:44:54,720
copy of this code i'll post this to the
5936
03:44:54,720 --> 03:44:56,560
comments section down below and well
5937
03:44:56,560 --> 03:44:58,319
yeah that's the super keyword in
5938
03:44:58,319 --> 03:45:00,960
javascript
5939
03:45:00,960 --> 03:45:02,720
hey yeah everybody in this video i'm
5940
03:45:02,720 --> 03:45:04,880
going to explain getters and setters
5941
03:45:04,880 --> 03:45:06,800
let's begin with getters to create a
5942
03:45:06,800 --> 03:45:09,439
getter you use the get keyword its job
5943
03:45:09,439 --> 03:45:11,439
is to bind an object property to a
5944
03:45:11,439 --> 03:45:13,600
function when that property is accessed
5945
03:45:13,600 --> 03:45:15,359
here's an example let's create a class
5946
03:45:15,359 --> 03:45:16,960
car class
5947
03:45:16,960 --> 03:45:18,080
car
5948
03:45:18,080 --> 03:45:21,760
i'll create a constructor constructor
5949
03:45:21,760 --> 03:45:23,840
we'll have one parameter power this will
5950
03:45:23,840 --> 03:45:24,840
be in
5951
03:45:24,840 --> 03:45:29,279
horsepower this dot power equals power
5952
03:45:29,279 --> 03:45:32,399
when i create a car object let car equal
5953
03:45:32,399 --> 03:45:35,439
new car i will pass in a unit for power
5954
03:45:35,439 --> 03:45:37,600
this will be in horsepower 400
5955
03:45:37,600 --> 03:45:39,120
horsepower is good
5956
03:45:39,120 --> 03:45:42,160
then i will display this console.log
5957
03:45:42,160 --> 03:45:44,880
car.power
5958
03:45:44,880 --> 03:45:47,439
my car has 400 horsepower one thing i
5959
03:45:47,439 --> 03:45:49,040
don't like about how my code is written
5960
03:45:49,040 --> 03:45:50,640
is that when i'm displaying the car's
5961
03:45:50,640 --> 03:45:52,479
horsepower there's no unit of
5962
03:45:52,479 --> 03:45:54,319
measurement when i display this property
5963
03:45:54,319 --> 03:45:55,640
i could add on
5964
03:45:55,640 --> 03:45:57,600
car.power plus
5965
03:45:57,600 --> 03:46:00,160
hp for horsepower but i have a better
5966
03:46:00,160 --> 03:46:02,800
idea let's use a getter when we get this
5967
03:46:02,800 --> 03:46:04,720
property we can add some additional
5968
03:46:04,720 --> 03:46:07,359
logic i will type get the name of the
5969
03:46:07,359 --> 03:46:09,760
property power
5970
03:46:09,760 --> 03:46:13,040
parentheses curly braces when we access
5971
03:46:13,040 --> 03:46:15,279
this property we will instead invoke a
5972
03:46:15,279 --> 03:46:17,439
function we will return
5973
03:46:17,439 --> 03:46:19,359
this dot power
5974
03:46:19,359 --> 03:46:20,880
now there's a couple issues with this
5975
03:46:20,880 --> 03:46:23,199
when i run this first we have a type
5976
03:46:23,199 --> 03:46:26,239
error cannot set property power of car
5977
03:46:26,239 --> 03:46:28,160
the property name and the getter cannot
5978
03:46:28,160 --> 03:46:30,080
have the same name a common naming
5979
03:46:30,080 --> 03:46:32,479
convention with properties of objects if
5980
03:46:32,479 --> 03:46:34,640
you have getters and setters set up is
5981
03:46:34,640 --> 03:46:36,399
that you take the property name and
5982
03:46:36,399 --> 03:46:38,479
precede it with an underscore this lets
5983
03:46:38,479 --> 03:46:40,319
other developers know that this is a
5984
03:46:40,319 --> 03:46:42,000
protected property and you probably
5985
03:46:42,000 --> 03:46:43,920
shouldn't mess with it now if i run this
5986
03:46:43,920 --> 03:46:46,560
we have another issue
5987
03:46:46,560 --> 03:46:49,040
uncaught range error maximum call stack
5988
03:46:49,040 --> 03:46:51,120
size exceeded that's because when we
5989
03:46:51,120 --> 03:46:53,920
invoke this getter we're returning this
5990
03:46:53,920 --> 03:46:56,080
dot power again we're invoking the
5991
03:46:56,080 --> 03:46:58,080
getter method from inside of it we will
5992
03:46:58,080 --> 03:47:01,840
instead return this dot underscore power
5993
03:47:01,840 --> 03:47:03,680
underscore power is the protected
5994
03:47:03,680 --> 03:47:06,479
property now when i run this it displays
5995
03:47:06,479 --> 03:47:09,359
my number 400 by associating a protected
5996
03:47:09,359 --> 03:47:11,359
property with only a getter this
5997
03:47:11,359 --> 03:47:13,840
protected property is read-only and not
5998
03:47:13,840 --> 03:47:17,040
writable if i was to take car.power and
5999
03:47:17,040 --> 03:47:19,359
set this to like a gajillion and i
6000
03:47:19,359 --> 03:47:21,680
display my car's power well it's still
6001
03:47:21,680 --> 03:47:24,479
at 400 we do not have direct access to
6002
03:47:24,479 --> 03:47:26,960
this property i mean theoretically you
6003
03:47:26,960 --> 03:47:29,520
could set car dot underscore power to a
6004
03:47:29,520 --> 03:47:31,520
kajillion but developers know that if
6005
03:47:31,520 --> 03:47:33,840
you see underscore than a property name
6006
03:47:33,840 --> 03:47:35,199
that means it's protected and you
6007
03:47:35,199 --> 03:47:36,880
shouldn't mess with it at all another
6008
03:47:36,880 --> 03:47:38,160
thing that we can do with getters is
6009
03:47:38,160 --> 03:47:40,239
that we can add some additional logic
6010
03:47:40,239 --> 03:47:43,120
instead of just returning this.power
6011
03:47:43,120 --> 03:47:45,439
let's add horsepower to the end
6012
03:47:45,439 --> 03:47:46,960
i'll place this within a template
6013
03:47:46,960 --> 03:47:49,359
literal
6014
03:47:50,319 --> 03:47:53,359
and add hp for horsepower
6015
03:47:53,359 --> 03:47:55,520
so when i display my car's power
6016
03:47:55,520 --> 03:47:58,160
it's 400 horsepower so that's a few
6017
03:47:58,160 --> 03:48:00,239
benefits of getters is that one it
6018
03:48:00,239 --> 03:48:02,800
increases data security and two you can
6019
03:48:02,800 --> 03:48:04,640
perform some additional logic when you
6020
03:48:04,640 --> 03:48:06,640
access a property directly to use a
6021
03:48:06,640 --> 03:48:08,479
getter you type the object name followed
6022
03:48:08,479 --> 03:48:10,560
by the getter name it appears to be no
6023
03:48:10,560 --> 03:48:12,160
different from accessing that property
6024
03:48:12,160 --> 03:48:14,800
directly we don't need to invoke a power
6025
03:48:14,800 --> 03:48:16,880
method you just type power as if it's a
6026
03:48:16,880 --> 03:48:18,880
property let's create a protected
6027
03:48:18,880 --> 03:48:20,640
property for gas
6028
03:48:20,640 --> 03:48:23,040
within my constructor i'll set this
6029
03:48:23,040 --> 03:48:25,120
dot underscore gas
6030
03:48:25,120 --> 03:48:28,479
to some amount of gas in liters perhaps
6031
03:48:28,479 --> 03:48:30,479
we're selling cars and each car will
6032
03:48:30,479 --> 03:48:33,040
have 25 liters of gas to start with then
6033
03:48:33,040 --> 03:48:36,560
to display my gas i can use a getter
6034
03:48:36,560 --> 03:48:37,760
get
6035
03:48:37,760 --> 03:48:38,640
gas
6036
03:48:38,640 --> 03:48:42,640
return this dot underscore gas
6037
03:48:42,640 --> 03:48:44,800
the unit of measurement will be liters
6038
03:48:44,800 --> 03:48:47,040
let's display my car's power
6039
03:48:47,040 --> 03:48:49,279
and its gas
6040
03:48:49,279 --> 03:48:51,600
25 liters let's add some additional
6041
03:48:51,600 --> 03:48:53,680
logic along with the gas level i would
6042
03:48:53,680 --> 03:48:55,439
like to display the percentage of what
6043
03:48:55,439 --> 03:48:58,000
my tank is at so zero percent would be
6044
03:48:58,000 --> 03:49:01,040
empty 100 would be full i'll return a
6045
03:49:01,040 --> 03:49:03,120
template literal
6046
03:49:03,120 --> 03:49:04,479
what about
6047
03:49:04,479 --> 03:49:06,399
this dot
6048
03:49:06,399 --> 03:49:08,640
underscore gas
6049
03:49:08,640 --> 03:49:11,359
divided by maybe some max tank size like
6050
03:49:11,359 --> 03:49:13,279
50 liters
6051
03:49:13,279 --> 03:49:15,439
times 100
6052
03:49:15,439 --> 03:49:16,640
percent
6053
03:49:16,640 --> 03:49:19,680
if my guess level is at 25 well then my
6054
03:49:19,680 --> 03:49:22,000
tank is half full and i can change this
6055
03:49:22,000 --> 03:49:24,640
to a different number like 50. now my
6056
03:49:24,640 --> 03:49:26,960
tank is completely full
6057
03:49:26,960 --> 03:49:28,239
or it's empty
6058
03:49:28,239 --> 03:49:30,800
now let's work on setters setters bind
6059
03:49:30,800 --> 03:49:32,880
an object property to a function when
6060
03:49:32,880 --> 03:49:35,359
that property is assigned a value with
6061
03:49:35,359 --> 03:49:37,120
my power property i don't want anybody
6062
03:49:37,120 --> 03:49:39,439
to mess with that that's why i only have
6063
03:49:39,439 --> 03:49:41,520
a getter but with the gas level i would
6064
03:49:41,520 --> 03:49:43,840
encourage people to change the gas level
6065
03:49:43,840 --> 03:49:45,199
they would want to be able to fill their
6066
03:49:45,199 --> 03:49:48,319
tank i'll create a setter set
6067
03:49:48,319 --> 03:49:50,319
gas
6068
03:49:50,319 --> 03:49:52,319
we'll need some argument i'll name this
6069
03:49:52,319 --> 03:49:55,199
value to fill my car with gas i can take
6070
03:49:55,199 --> 03:49:56,720
this dot
6071
03:49:56,720 --> 03:50:00,319
gas property equals value since gas has
6072
03:50:00,319 --> 03:50:02,720
a setter that means it's writable i'll
6073
03:50:02,720 --> 03:50:05,520
take my car's gas property and set the
6074
03:50:05,520 --> 03:50:07,680
sql to some value as if i'm filling the
6075
03:50:07,680 --> 03:50:09,359
tank currently there's no additional
6076
03:50:09,359 --> 03:50:11,760
logic within this gas setter a user
6077
03:50:11,760 --> 03:50:14,319
could theoretically add like a kajillion
6078
03:50:14,319 --> 03:50:15,760
liters of gas
6079
03:50:15,760 --> 03:50:18,080
now my gas is like 2 billion percent
6080
03:50:18,080 --> 03:50:20,160
full i might want to limit that i'll
6081
03:50:20,160 --> 03:50:22,000
check to see if our value that we
6082
03:50:22,000 --> 03:50:23,040
receive
6083
03:50:23,040 --> 03:50:26,399
is greater than maybe 50. so that will
6084
03:50:26,399 --> 03:50:29,040
be the max tank size if the value is
6085
03:50:29,040 --> 03:50:31,920
greater than 50 then let's set value
6086
03:50:31,920 --> 03:50:34,640
equal to 50 to limit it
6087
03:50:34,640 --> 03:50:36,479
if we attempt to add over a billion
6088
03:50:36,479 --> 03:50:37,840
liters of gas
6089
03:50:37,840 --> 03:50:40,080
it will be capped at 50. or possibly
6090
03:50:40,080 --> 03:50:41,439
somebody will set this to a negative
6091
03:50:41,439 --> 03:50:44,239
number negative 100 liters
6092
03:50:44,239 --> 03:50:46,560
else if value
6093
03:50:46,560 --> 03:50:50,000
is less than zero
6094
03:50:50,000 --> 03:50:51,359
value
6095
03:50:51,359 --> 03:50:54,160
now equals zero
6096
03:50:54,160 --> 03:50:56,239
there now our tank is empty so yeah
6097
03:50:56,239 --> 03:50:59,040
everybody those are getters and setters
6098
03:50:59,040 --> 03:51:00,880
they bind an object's property to a
6099
03:51:00,880 --> 03:51:03,040
function when that property is accessed
6100
03:51:03,040 --> 03:51:05,199
in the case of a getter or assigned a
6101
03:51:05,199 --> 03:51:07,760
value in the case of setters so those
6102
03:51:07,760 --> 03:51:10,000
are getters and setters everybody if you
6103
03:51:10,000 --> 03:51:12,000
would like a copy of my code i'll post
6104
03:51:12,000 --> 03:51:13,600
this in the comment section down below
6105
03:51:13,600 --> 03:51:16,000
and well yeah those are getters and
6106
03:51:16,000 --> 03:51:19,439
setters in javascript
6107
03:51:19,439 --> 03:51:21,120
hey guys in this video i'm going to show
6108
03:51:21,120 --> 03:51:22,880
you how we can pass objects to a
6109
03:51:22,880 --> 03:51:24,960
function as an argument i have a class
6110
03:51:24,960 --> 03:51:27,199
car and we have three car objects each
6111
03:51:27,199 --> 03:51:29,920
car has a model year and color how could
6112
03:51:29,920 --> 03:51:32,160
passing objects to a function be useful
6113
03:51:32,160 --> 03:51:34,160
i need a function to display all of the
6114
03:51:34,160 --> 03:51:36,080
information for each car so let's create
6115
03:51:36,080 --> 03:51:37,279
a function
6116
03:51:37,279 --> 03:51:38,640
function
6117
03:51:38,640 --> 03:51:41,439
and we could name this display
6118
03:51:41,439 --> 03:51:43,680
info if we're accepting an object as an
6119
03:51:43,680 --> 03:51:45,120
argument we need a matching set of
6120
03:51:45,120 --> 03:51:47,040
parameters i think we should just name
6121
03:51:47,040 --> 03:51:49,680
it simply car all lowercase remember
6122
03:51:49,680 --> 03:51:51,439
when you pass an object to a function
6123
03:51:51,439 --> 03:51:53,279
the parameter name is kind of like a
6124
03:51:53,279 --> 03:51:54,399
nickname that you're giving it
6125
03:51:54,399 --> 03:51:57,040
temporarily then to invoke this function
6126
03:51:57,040 --> 03:51:59,199
i will type the function name display
6127
03:51:59,199 --> 03:52:01,680
info and pass a car object as an
6128
03:52:01,680 --> 03:52:03,840
argument i would like to display car
6129
03:52:03,840 --> 03:52:06,319
one's info within this function let's
6130
03:52:06,319 --> 03:52:09,600
display this car's model year and color
6131
03:52:09,600 --> 03:52:11,840
car
6132
03:52:11,840 --> 03:52:14,160
model
6133
03:52:15,199 --> 03:52:16,239
year
6134
03:52:16,239 --> 03:52:17,920
and color
6135
03:52:17,920 --> 03:52:20,560
okay let's see if it works yeah
6136
03:52:20,560 --> 03:52:22,399
with this car object the model is a
6137
03:52:22,399 --> 03:52:24,479
mustang the year is 20 23 the color is
6138
03:52:24,479 --> 03:52:26,640
red we can pass a different object as an
6139
03:52:26,640 --> 03:52:28,640
argument this time how about car two
6140
03:52:28,640 --> 03:52:30,720
display info card two
6141
03:52:30,720 --> 03:52:34,080
car two is a corvette the year is 2024
6142
03:52:34,080 --> 03:52:35,840
and the color is blue
6143
03:52:35,840 --> 03:52:37,439
then car three
6144
03:52:37,439 --> 03:52:40,000
is a lambo the year is 2022 and the
6145
03:52:40,000 --> 03:52:41,760
color is yellow let's create another
6146
03:52:41,760 --> 03:52:44,000
function to maybe change the color of a
6147
03:52:44,000 --> 03:52:46,800
car and we will have two parameters
6148
03:52:46,800 --> 03:52:47,840
function
6149
03:52:47,840 --> 03:52:50,399
change color
6150
03:52:50,399 --> 03:52:53,120
we will accept a car object
6151
03:52:53,120 --> 03:52:55,520
and a color we will take
6152
03:52:55,520 --> 03:52:56,720
car
6153
03:52:56,720 --> 03:52:59,199
dot color set is equal to
6154
03:52:59,199 --> 03:53:01,279
whatever color we receive as an argument
6155
03:53:01,279 --> 03:53:04,080
let's change the color of our lambo so i
6156
03:53:04,080 --> 03:53:06,880
will invoke the change color function
6157
03:53:06,880 --> 03:53:09,680
and i will pass in two arguments a car
6158
03:53:09,680 --> 03:53:10,960
and a color
6159
03:53:10,960 --> 03:53:12,479
car three
6160
03:53:12,479 --> 03:53:14,880
and let's change the color to gold and
6161
03:53:14,880 --> 03:53:17,279
then display it and the color of our
6162
03:53:17,279 --> 03:53:19,760
lambo is now gold and not yellow that's
6163
03:53:19,760 --> 03:53:21,760
how to pass an object to a function when
6164
03:53:21,760 --> 03:53:24,000
you invoke a function just pass the name
6165
03:53:24,000 --> 03:53:26,160
of an object as an argument and with the
6166
03:53:26,160 --> 03:53:28,239
parameter name just find a name of
6167
03:53:28,239 --> 03:53:29,680
what's descriptive of what you're
6168
03:53:29,680 --> 03:53:31,600
accepting as an argument so that's how
6169
03:53:31,600 --> 03:53:34,080
to pass objects as arguments if you
6170
03:53:34,080 --> 03:53:35,840
would like a copy of this code i'll post
6171
03:53:35,840 --> 03:53:37,439
this in the comment section down below
6172
03:53:37,439 --> 03:53:39,199
if you haven't already please be sure to
6173
03:53:39,199 --> 03:53:40,720
smash that like button leave a random
6174
03:53:40,720 --> 03:53:42,479
comment down below and subscribe if
6175
03:53:42,479 --> 03:53:45,439
you'd like to become a fellow bro
6176
03:53:45,439 --> 03:53:47,279
hey guys in this video i'm going to show
6177
03:53:47,279 --> 03:53:49,040
you how we can create an array of
6178
03:53:49,040 --> 03:53:51,600
objects in javascript i have a class car
6179
03:53:51,600 --> 03:53:53,840
and we have three car objects car one
6180
03:53:53,840 --> 03:53:56,720
cartoon car three each car has a model
6181
03:53:56,720 --> 03:53:59,760
year and color as well as a drive method
6182
03:53:59,760 --> 03:54:01,760
let's come up with a descriptive name of
6183
03:54:01,760 --> 03:54:02,960
this array
6184
03:54:02,960 --> 03:54:04,880
what about cars
6185
03:54:04,880 --> 03:54:06,720
then with creating arrays you use a pair
6186
03:54:06,720 --> 03:54:08,800
of straight brackets what would we like
6187
03:54:08,800 --> 03:54:10,720
to add to this raised elements well the
6188
03:54:10,720 --> 03:54:13,439
names of our objects car 1
6189
03:54:13,439 --> 03:54:14,720
card 2
6190
03:54:14,720 --> 03:54:16,560
and car 3. let me show you what happens
6191
03:54:16,560 --> 03:54:18,720
when we display one of these elements
6192
03:54:18,720 --> 03:54:21,279
console.log type the name of the array
6193
03:54:21,279 --> 03:54:23,120
followed by an element number the first
6194
03:54:23,120 --> 03:54:25,199
element has an index of zero because
6195
03:54:25,199 --> 03:54:27,120
computers always start with zero and we
6196
03:54:27,120 --> 03:54:29,279
have our car object but if you need one
6197
03:54:29,279 --> 03:54:31,120
of these properties or methods you would
6198
03:54:31,120 --> 03:54:33,439
follow the array name and the index
6199
03:54:33,439 --> 03:54:36,160
number dot and then a property or method
6200
03:54:36,160 --> 03:54:38,880
so at index 0 of our array cards i would
6201
03:54:38,880 --> 03:54:41,600
like to access the model property the
6202
03:54:41,600 --> 03:54:44,000
first element of this array has a model
6203
03:54:44,000 --> 03:54:46,160
property of mustang if i were to repeat
6204
03:54:46,160 --> 03:54:49,199
the process for cars at index of one and
6205
03:54:49,199 --> 03:54:51,279
two those respective models are a
6206
03:54:51,279 --> 03:54:53,199
corvette and a lambo if you need to
6207
03:54:53,199 --> 03:54:55,680
access a property or method of an object
6208
03:54:55,680 --> 03:54:57,199
that's within an array you type the
6209
03:54:57,199 --> 03:54:59,439
array name and index number followed by
6210
03:54:59,439 --> 03:55:02,160
dot then a given property or method just
6211
03:55:02,160 --> 03:55:04,160
for practice let's invoke the drive
6212
03:55:04,160 --> 03:55:06,080
method of each of these cars so i would
6213
03:55:06,080 --> 03:55:08,479
type the array name and index number
6214
03:55:08,479 --> 03:55:10,560
follow it with a dot then a property or
6215
03:55:10,560 --> 03:55:13,199
method i would like the element at index
6216
03:55:13,199 --> 03:55:15,600
zero within this array to use the drive
6217
03:55:15,600 --> 03:55:17,439
method let me get rid of these lines
6218
03:55:17,439 --> 03:55:19,760
okay you drive the mustang let's do that
6219
03:55:19,760 --> 03:55:22,960
with the other elements one and two
6220
03:55:22,960 --> 03:55:24,319
you drive the mustang you drive the
6221
03:55:24,319 --> 03:55:26,399
corvette you drive the lambo just as a
6222
03:55:26,399 --> 03:55:28,080
challenge let's create a function that
6223
03:55:28,080 --> 03:55:29,840
will loop over the elements of this
6224
03:55:29,840 --> 03:55:31,680
array and invoke the drive method of
6225
03:55:31,680 --> 03:55:33,680
each element we're going to have a race
6226
03:55:33,680 --> 03:55:35,840
we need a function to start our race and
6227
03:55:35,840 --> 03:55:38,080
have every car use its drive method so i
6228
03:55:38,080 --> 03:55:40,479
will create a function let's name this
6229
03:55:40,479 --> 03:55:41,520
start
6230
03:55:41,520 --> 03:55:43,040
race
6231
03:55:43,040 --> 03:55:44,960
say that we need to pass our array of
6232
03:55:44,960 --> 03:55:46,960
objects as an argument we'll need a
6233
03:55:46,960 --> 03:55:48,640
matching set of parameters i will name
6234
03:55:48,640 --> 03:55:51,359
this parameter cars and i would need to
6235
03:55:51,359 --> 03:55:52,800
loop through all of the elements within
6236
03:55:52,800 --> 03:55:55,359
this array i can use a4 loop for that
6237
03:55:55,359 --> 03:55:57,520
let's say const
6238
03:55:57,520 --> 03:56:01,359
car of cars car is the current element i
6239
03:56:01,359 --> 03:56:03,680
need each car to use its drive method
6240
03:56:03,680 --> 03:56:05,600
then we need to invoke this function
6241
03:56:05,600 --> 03:56:08,160
start race then we can pass in our array
6242
03:56:08,160 --> 03:56:10,399
of objects cars
6243
03:56:10,399 --> 03:56:11,760
you drive the mustang you drive the
6244
03:56:11,760 --> 03:56:13,920
corvette you drive the lambo hey let's
6245
03:56:13,920 --> 03:56:16,000
add another car
6246
03:56:16,000 --> 03:56:17,359
car 4
6247
03:56:17,359 --> 03:56:19,600
is a ferrari
6248
03:56:19,600 --> 03:56:22,880
the year is 2025.
6249
03:56:22,880 --> 03:56:24,720
the color is
6250
03:56:24,720 --> 03:56:26,960
white so let's add this object to our
6251
03:56:26,960 --> 03:56:27,840
array
6252
03:56:27,840 --> 03:56:29,840
car four
6253
03:56:29,840 --> 03:56:32,000
now we have four cars in our race
6254
03:56:32,000 --> 03:56:34,399
you drive the ferrari so yeah that's an
6255
03:56:34,399 --> 03:56:36,720
example of an array of objects it's a
6256
03:56:36,720 --> 03:56:38,560
useful programming technique because you
6257
03:56:38,560 --> 03:56:40,560
can keep all objects organized within an
6258
03:56:40,560 --> 03:56:42,640
array so that's an array of objects if
6259
03:56:42,640 --> 03:56:44,160
you would like a copy of this code i'll
6260
03:56:44,160 --> 03:56:45,520
post this in the comments section down
6261
03:56:45,520 --> 03:56:47,199
below if you haven't already please be
6262
03:56:47,199 --> 03:56:48,720
sure you smash that like button leave
6263
03:56:48,720 --> 03:56:50,399
random comment down below and subscribe
6264
03:56:50,399 --> 03:56:53,600
if you'd like to become a fellow bro
6265
03:56:53,600 --> 03:56:55,279
hey guys in this video i'm going to
6266
03:56:55,279 --> 03:56:57,439
explain anonymous objects they are
6267
03:56:57,439 --> 03:56:59,760
objects without a name a benefit is that
6268
03:56:59,760 --> 03:57:02,080
using anonymous objects requires less
6269
03:57:02,080 --> 03:57:04,319
syntax and there's no need for unique
6270
03:57:04,319 --> 03:57:06,800
names however a downside is that we do
6271
03:57:06,800 --> 03:57:08,720
not have direct access to one of these
6272
03:57:08,720 --> 03:57:11,040
objects so here's an example we're going
6273
03:57:11,040 --> 03:57:13,359
to create an array of cards each card
6274
03:57:13,359 --> 03:57:14,960
will be an object which we'll create
6275
03:57:14,960 --> 03:57:16,800
with a class
6276
03:57:16,800 --> 03:57:17,840
class
6277
03:57:17,840 --> 03:57:19,359
card
6278
03:57:19,359 --> 03:57:22,479
i'll add a constructor
6279
03:57:23,920 --> 03:57:27,040
each card will have both a value and a
6280
03:57:27,040 --> 03:57:27,920
suit
6281
03:57:27,920 --> 03:57:32,080
this dot value equals value
6282
03:57:32,080 --> 03:57:35,199
this dot suit equals suit
6283
03:57:35,199 --> 03:57:37,040
suppose i would like to make a card
6284
03:57:37,040 --> 03:57:39,279
normally if we're using names not
6285
03:57:39,279 --> 03:57:41,920
anonymous objects i would declare a
6286
03:57:41,920 --> 03:57:45,600
unique name like let card one
6287
03:57:45,600 --> 03:57:47,920
equal new then the name of the class to
6288
03:57:47,920 --> 03:57:50,800
instantiate an object card for this
6289
03:57:50,800 --> 03:57:53,040
object i need to pass in a value and a
6290
03:57:53,040 --> 03:57:56,560
suit this card will be the ace of hearts
6291
03:57:56,560 --> 03:57:57,279
a
6292
03:57:57,279 --> 03:57:59,359
the second argument will be
6293
03:57:59,359 --> 03:58:00,720
hearts
6294
03:58:00,720 --> 03:58:03,359
i'll create a couple other cards
6295
03:58:03,359 --> 03:58:06,399
card two will be named well card two
6296
03:58:06,399 --> 03:58:10,800
card two will be the ace of spades
6297
03:58:11,439 --> 03:58:13,840
card three
6298
03:58:13,840 --> 03:58:15,199
ace of
6299
03:58:15,199 --> 03:58:17,760
diamonds
6300
03:58:17,760 --> 03:58:21,520
card four will be ace of clubs
6301
03:58:21,520 --> 03:58:23,359
i'll just create four more cards i'll
6302
03:58:23,359 --> 03:58:25,120
copy all this
6303
03:58:25,120 --> 03:58:27,040
paste it
6304
03:58:27,040 --> 03:58:29,120
we have cards five
6305
03:58:29,120 --> 03:58:31,359
six seven eight
6306
03:58:31,359 --> 03:58:33,680
these cards will be two of hearts two of
6307
03:58:33,680 --> 03:58:36,640
spades two of diamonds and two of clubs
6308
03:58:36,640 --> 03:58:38,000
i'll just stick with eight cards for
6309
03:58:38,000 --> 03:58:39,920
this example i don't need a whole deck
6310
03:58:39,920 --> 03:58:42,560
of 52 cards to explain this so we have
6311
03:58:42,560 --> 03:58:44,239
eight cards i'm going to add them to an
6312
03:58:44,239 --> 03:58:45,279
array
6313
03:58:45,279 --> 03:58:46,319
let
6314
03:58:46,319 --> 03:58:49,199
cards equal
6315
03:58:49,199 --> 03:58:51,359
we'll add card one
6316
03:58:51,359 --> 03:58:52,479
card two
6317
03:58:52,479 --> 03:58:55,800
and the others
6318
03:58:58,239 --> 03:59:00,080
that's good enough to access one of
6319
03:59:00,080 --> 03:59:02,080
these properties of one of these cards i
6320
03:59:02,080 --> 03:59:04,720
can either do so directly by the object
6321
03:59:04,720 --> 03:59:07,920
name or by an array element
6322
03:59:07,920 --> 03:59:12,600
so if i were to display console.log
6323
03:59:12,720 --> 03:59:13,840
one
6324
03:59:13,840 --> 03:59:16,239
dot value
6325
03:59:16,239 --> 03:59:19,359
plus card one dot suit
6326
03:59:19,359 --> 03:59:22,319
this would display ace of hearts
6327
03:59:22,319 --> 03:59:25,279
or i could access this object indirectly
6328
03:59:25,279 --> 03:59:27,120
via the array
6329
03:59:27,120 --> 03:59:29,920
that would be my array cards
6330
03:59:29,920 --> 03:59:34,560
at index of zero dot value plus
6331
03:59:34,560 --> 03:59:37,600
cards at index of zero
6332
03:59:37,600 --> 03:59:38,880
dot suit
6333
03:59:38,880 --> 03:59:40,720
this would do the same thing
6334
03:59:40,720 --> 03:59:42,000
with this first statement we're
6335
03:59:42,000 --> 03:59:44,319
accessing this object directly by its
6336
03:59:44,319 --> 03:59:46,560
name in the second statement we're
6337
03:59:46,560 --> 03:59:49,359
accessing this object indirectly via an
6338
03:59:49,359 --> 03:59:51,840
array index you know this does in fact
6339
03:59:51,840 --> 03:59:53,279
work and all but i would write this a
6340
03:59:53,279 --> 03:59:54,960
different way there's no need to create
6341
03:59:54,960 --> 03:59:56,560
all of these unique names that's a lot
6342
03:59:56,560 --> 03:59:58,720
of work if we were to rewrite this using
6343
03:59:58,720 --> 04:00:00,960
some anonymous objects this is what we
6344
04:00:00,960 --> 04:00:02,720
can do we'll get rid of everything
6345
04:00:02,720 --> 04:00:06,520
before the new keyword
6346
04:00:09,279 --> 04:00:11,279
we've created eight cards but we have no
6347
04:00:11,279 --> 04:00:13,439
way to reference them in place of adding
6348
04:00:13,439 --> 04:00:15,600
some object names directly to our array
6349
04:00:15,600 --> 04:00:18,080
when we instantiate a card object we can
6350
04:00:18,080 --> 04:00:20,720
actually place that within the array
6351
04:00:20,720 --> 04:00:22,960
we'll add a new card
6352
04:00:22,960 --> 04:00:24,560
with these properties
6353
04:00:24,560 --> 04:00:26,960
and that will be the first element
6354
04:00:26,960 --> 04:00:29,199
then let's add the others
6355
04:00:29,199 --> 04:00:32,840
i'll speed up the footage
6356
04:00:45,840 --> 04:00:48,000
this would do the same thing however we
6357
04:00:48,000 --> 04:00:50,560
have a reference error card 1 is not
6358
04:00:50,560 --> 04:00:53,760
defined so there is no card 1 anymore my
6359
04:00:53,760 --> 04:00:55,279
interpreter doesn't know what the heck
6360
04:00:55,279 --> 04:00:57,359
card 1 is that's because we never
6361
04:00:57,359 --> 04:01:00,239
declared it so using anonymous objects
6362
04:01:00,239 --> 04:01:02,960
we have no way to directly access these
6363
04:01:02,960 --> 04:01:05,120
objects by a name because well they
6364
04:01:05,120 --> 04:01:07,520
don't have one in order to access the
6365
04:01:07,520 --> 04:01:09,600
properties of an object i would need to
6366
04:01:09,600 --> 04:01:11,120
do so indirectly
6367
04:01:11,120 --> 04:01:13,439
since these objects are within an array
6368
04:01:13,439 --> 04:01:15,920
i can reference them by an index number
6369
04:01:15,920 --> 04:01:18,880
cards at index 0 would be the ace of
6370
04:01:18,880 --> 04:01:19,840
hearts
6371
04:01:19,840 --> 04:01:22,319
cards at index of 1
6372
04:01:22,319 --> 04:01:24,479
is my ace of spades
6373
04:01:24,479 --> 04:01:26,720
and then 7
6374
04:01:26,720 --> 04:01:29,040
is my two of clubs so you can see that
6375
04:01:29,040 --> 04:01:30,880
this still works and it's a lot less
6376
04:01:30,880 --> 04:01:33,279
syntax sometimes it would be impractical
6377
04:01:33,279 --> 04:01:35,199
to give an object a name if you're never
6378
04:01:35,199 --> 04:01:37,359
ever going to reference it directly for
6379
04:01:37,359 --> 04:01:39,439
fun let's display all of the cards
6380
04:01:39,439 --> 04:01:41,359
within this deck using the for each
6381
04:01:41,359 --> 04:01:42,960
method
6382
04:01:42,960 --> 04:01:46,560
cards dot for each
6383
04:01:46,560 --> 04:01:49,520
we will take card as an argument arrow
6384
04:01:49,520 --> 04:01:50,800
function
6385
04:01:50,800 --> 04:01:51,920
console
6386
04:01:51,920 --> 04:01:53,439
dot log
6387
04:01:53,439 --> 04:01:56,720
i'll use a template literal
6388
04:01:56,880 --> 04:01:58,960
let's take card
6389
04:01:58,960 --> 04:02:01,600
dot value
6390
04:02:01,600 --> 04:02:04,000
and also display
6391
04:02:04,000 --> 04:02:07,359
card dot suit
6392
04:02:07,359 --> 04:02:09,439
there here are all the cards in my deck
6393
04:02:09,439 --> 04:02:11,840
displayed so yeah everybody those are
6394
04:02:11,840 --> 04:02:14,239
anonymous objects they're just objects
6395
04:02:14,239 --> 04:02:16,239
without a name you don't need to declare
6396
04:02:16,239 --> 04:02:18,640
a name to create an object names are
6397
04:02:18,640 --> 04:02:21,439
just used as a reference as storage the
6398
04:02:21,439 --> 04:02:23,199
downside is that you don't have direct
6399
04:02:23,199 --> 04:02:25,439
access to an anonymous object you would
6400
04:02:25,439 --> 04:02:27,359
typically store them within something
6401
04:02:27,359 --> 04:02:29,760
like an array and some benefits is that
6402
04:02:29,760 --> 04:02:32,160
there's less syntax it reduces the size
6403
04:02:32,160 --> 04:02:33,920
of your code and there's no need to
6404
04:02:33,920 --> 04:02:35,920
create some unique names so those are
6405
04:02:35,920 --> 04:02:37,760
anonymous objects everybody if you would
6406
04:02:37,760 --> 04:02:39,520
like a copy of this code i'll post this
6407
04:02:39,520 --> 04:02:41,279
in the comments section down below and
6408
04:02:41,279 --> 04:02:43,600
well yeah those are anonymous objects in
6409
04:02:43,600 --> 04:02:46,239
javascript
6410
04:02:46,239 --> 04:02:47,760
hey guys in this video i'm going to
6411
04:02:47,760 --> 04:02:50,000
explain error handling errors are
6412
04:02:50,000 --> 04:02:52,560
objects with a description of something
6413
04:02:52,560 --> 04:02:54,640
that went wrong encountering an error
6414
04:02:54,640 --> 04:02:56,800
will halt the execution of your program
6415
04:02:56,800 --> 04:02:58,800
we would like some way to gracefully
6416
04:02:58,800 --> 04:03:00,640
handle these errors without interrupting
6417
04:03:00,640 --> 04:03:02,640
our program errors can happen for one of
6418
04:03:02,640 --> 04:03:04,880
a few reasons a few examples would be
6419
04:03:04,880 --> 04:03:06,720
you can't open a file you lose
6420
04:03:06,720 --> 04:03:08,960
connection to a device a user types in
6421
04:03:08,960 --> 04:03:11,279
some incorrect user input or a type
6422
04:03:11,279 --> 04:03:13,199
error one example of type error is that
6423
04:03:13,199 --> 04:03:15,560
we mistype something instead of saying
6424
04:03:15,560 --> 04:03:19,199
console.log maybe we misspell log as leg
6425
04:03:19,199 --> 04:03:21,760
console.leg well we encountered an
6426
04:03:21,760 --> 04:03:24,800
uncaught type error console.lag is not a
6427
04:03:24,800 --> 04:03:26,720
function if an error happens while your
6428
04:03:26,720 --> 04:03:28,560
program is running it can cause your
6429
04:03:28,560 --> 04:03:30,080
program to stop we would like to
6430
04:03:30,080 --> 04:03:32,000
gracefully handle these errors and one
6431
04:03:32,000 --> 04:03:33,680
way to do that is to surround any
6432
04:03:33,680 --> 04:03:36,000
dangerous code with a try block code
6433
04:03:36,000 --> 04:03:37,680
that could cause an error is considered
6434
04:03:37,680 --> 04:03:40,000
dangerous accepting user input is almost
6435
04:03:40,000 --> 04:03:41,439
always dangerous because you don't know
6436
04:03:41,439 --> 04:03:42,800
what they're going to type in so let's
6437
04:03:42,800 --> 04:03:44,640
create a try block and add some
6438
04:03:44,640 --> 04:03:46,319
dangerous code now if you have a try
6439
04:03:46,319 --> 04:03:48,080
block you need to follow this with a
6440
04:03:48,080 --> 04:03:49,359
catch block
6441
04:03:49,359 --> 04:03:51,920
and there's one argument an error if
6442
04:03:51,920 --> 04:03:53,680
something goes wrong we will do
6443
04:03:53,680 --> 04:03:54,880
something else
6444
04:03:54,880 --> 04:03:56,720
in fact i will display whatever our
6445
04:03:56,720 --> 04:03:59,120
error is console.log
6446
04:03:59,120 --> 04:04:02,800
error again if i type console.leg
6447
04:04:02,800 --> 04:04:05,199
while this won't interrupt my program it
6448
04:04:05,199 --> 04:04:07,520
will simply just display the error type
6449
04:04:07,520 --> 04:04:10,160
air console.lag is not a function so we
6450
04:04:10,160 --> 04:04:12,000
don't get that big nasty red error
6451
04:04:12,000 --> 04:04:14,160
message sometimes in your program
6452
04:04:14,160 --> 04:04:15,840
something will go wrong but it won't
6453
04:04:15,840 --> 04:04:17,840
generate an error here's an example
6454
04:04:17,840 --> 04:04:19,760
let's accept some user input we'll ask a
6455
04:04:19,760 --> 04:04:21,840
user to type in a number let's declare
6456
04:04:21,840 --> 04:04:27,439
variable x x equals window dot prompt
6457
04:04:28,080 --> 04:04:31,359
enter a number when we accept user input
6458
04:04:31,359 --> 04:04:33,120
it's of a string data type we'll need to
6459
04:04:33,120 --> 04:04:35,600
convert that to a number
6460
04:04:35,600 --> 04:04:38,080
okay at the end let's display a message
6461
04:04:38,080 --> 04:04:39,600
console.log
6462
04:04:39,600 --> 04:04:43,040
i'll use a template literal
6463
04:04:43,040 --> 04:04:46,239
x is a number
6464
04:04:46,239 --> 04:04:48,800
okay let's run this enter a number well
6465
04:04:48,800 --> 04:04:50,800
pizza is my favorite number i'll enter
6466
04:04:50,800 --> 04:04:53,279
that in so even though this didn't cause
6467
04:04:53,279 --> 04:04:55,120
any errors it's definitely going to
6468
04:04:55,120 --> 04:04:57,279
cause problems for me in my program
6469
04:04:57,279 --> 04:04:59,279
something is going to go wrong using
6470
04:04:59,279 --> 04:05:01,600
this throw keyword i can execute a
6471
04:05:01,600 --> 04:05:03,439
user-defined error but we'll need to
6472
04:05:03,439 --> 04:05:05,359
know when to use it i'll add an if
6473
04:05:05,359 --> 04:05:08,880
statement if and the condition is is
6474
04:05:08,880 --> 04:05:11,040
not a number
6475
04:05:11,040 --> 04:05:14,720
pass in x if this is true then we will
6476
04:05:14,720 --> 04:05:18,319
throw an error or an argument to be used
6477
04:05:18,319 --> 04:05:20,000
as an error so we're just displaying a
6478
04:05:20,000 --> 04:05:21,840
message in this example
6479
04:05:21,840 --> 04:05:25,199
that wasn't a number
6480
04:05:25,199 --> 04:05:27,760
so now when i run this again i'll enter
6481
04:05:27,760 --> 04:05:29,199
pizza in
6482
04:05:29,199 --> 04:05:31,199
that wasn't a number what if the user
6483
04:05:31,199 --> 04:05:32,720
doesn't type in anything they just press
6484
04:05:32,720 --> 04:05:34,479
the ok button
6485
04:05:34,479 --> 04:05:36,800
0 isn't a number so that means they
6486
04:05:36,800 --> 04:05:38,800
didn't type in anything we can throw a
6487
04:05:38,800 --> 04:05:41,120
user defined error we'll need another if
6488
04:05:41,120 --> 04:05:42,080
statement
6489
04:05:42,080 --> 04:05:42,880
if
6490
04:05:42,880 --> 04:05:45,840
x is equal to an empty string
6491
04:05:45,840 --> 04:05:48,399
then throw
6492
04:05:48,399 --> 04:05:50,080
that was
6493
04:05:50,080 --> 04:05:51,120
empty
6494
04:05:51,120 --> 04:05:54,399
let's try this again i'll just press ok
6495
04:05:54,399 --> 04:05:57,199
that was empty so an error is an object
6496
04:05:57,199 --> 04:05:58,960
with a description of something that
6497
04:05:58,960 --> 04:06:00,880
went wrong there may be times when
6498
04:06:00,880 --> 04:06:02,640
something will go wrong in your program
6499
04:06:02,640 --> 04:06:04,319
but it doesn't raise an error like
6500
04:06:04,319 --> 04:06:05,840
somebody types in something that you
6501
04:06:05,840 --> 04:06:07,840
didn't anticipate you can use this throw
6502
04:06:07,840 --> 04:06:10,720
keyword to execute a user defined error
6503
04:06:10,720 --> 04:06:12,239
now there's one more statement that we
6504
04:06:12,239 --> 04:06:14,960
can add to this you can add a finally
6505
04:06:14,960 --> 04:06:17,279
block finally we'll always execute
6506
04:06:17,279 --> 04:06:18,560
doesn't matter if your code is
6507
04:06:18,560 --> 04:06:20,800
successful or unsuccessful if there's an
6508
04:06:20,800 --> 04:06:22,479
error here's one use of the finally
6509
04:06:22,479 --> 04:06:25,040
block let's say we open a file after we
6510
04:06:25,040 --> 04:06:26,720
open the file and are done with it we
6511
04:06:26,720 --> 04:06:28,560
would like to close it but if we open
6512
04:06:28,560 --> 04:06:30,080
the file and cause an error we would
6513
04:06:30,080 --> 04:06:32,000
still like to close it so the finally
6514
04:06:32,000 --> 04:06:34,080
block is good for any sort of cleanup
6515
04:06:34,080 --> 04:06:35,359
but we're not going to be working on
6516
04:06:35,359 --> 04:06:37,040
opening files let's just display a
6517
04:06:37,040 --> 04:06:38,840
message
6518
04:06:38,840 --> 04:06:44,239
console.log this always executes
6519
04:06:44,239 --> 04:06:45,600
let's run this again i'll enter in a
6520
04:06:45,600 --> 04:06:49,199
number 123. 123 is a number this always
6521
04:06:49,199 --> 04:06:50,960
executes so even if our code is
6522
04:06:50,960 --> 04:06:52,720
successful we will still execute the
6523
04:06:52,720 --> 04:06:54,720
finally block let's enter in something
6524
04:06:54,720 --> 04:06:57,199
that's not a number like the word pizza
6525
04:06:57,199 --> 04:06:58,880
that wasn't a number this always
6526
04:06:58,880 --> 04:07:01,279
executes so yeah everybody those are a
6527
04:07:01,279 --> 04:07:03,680
few ways to handle errors and error is
6528
04:07:03,680 --> 04:07:05,439
an object with a description of
6529
04:07:05,439 --> 04:07:07,359
something that went wrong sometimes
6530
04:07:07,359 --> 04:07:09,199
things will go wrong and they don't
6531
04:07:09,199 --> 04:07:11,199
cause errors if that's the case you
6532
04:07:11,199 --> 04:07:13,279
would want to use this throw keyword and
6533
04:07:13,279 --> 04:07:16,000
it will execute a user defined error or
6534
04:07:16,000 --> 04:07:18,239
a message or value that can be used in
6535
04:07:18,239 --> 04:07:20,000
an error so that's some basic error
6536
04:07:20,000 --> 04:07:21,600
handling if you would like a copy of
6537
04:07:21,600 --> 04:07:23,279
this code i'll post this to the comment
6538
04:07:23,279 --> 04:07:25,520
section down below and well yeah that's
6539
04:07:25,520 --> 04:07:28,479
error handling in javascript
6540
04:07:28,479 --> 04:07:30,479
hey yeah everybody in this video i'm
6541
04:07:30,479 --> 04:07:32,800
going to explain the set timeout method
6542
04:07:32,800 --> 04:07:35,760
it invokes a function after a number of
6543
04:07:35,760 --> 04:07:38,319
milliseconds it's an asynchronous
6544
04:07:38,319 --> 04:07:40,560
function meaning that it doesn't pause
6545
04:07:40,560 --> 04:07:42,800
the execution of your program for my
6546
04:07:42,800 --> 04:07:44,479
example we're going to be annoying we're
6547
04:07:44,479 --> 04:07:46,640
going to spam some alert messages after
6548
04:07:46,640 --> 04:07:48,880
a given amount of milliseconds let's
6549
04:07:48,880 --> 04:07:51,120
create a few functions
6550
04:07:51,120 --> 04:07:52,319
function
6551
04:07:52,319 --> 04:07:54,479
will display three messages this will be
6552
04:07:54,479 --> 04:07:55,840
the first message
6553
04:07:55,840 --> 04:07:56,720
first
6554
04:07:56,720 --> 04:07:59,600
message i'll create an alert
6555
04:07:59,600 --> 04:08:00,720
alert
6556
04:08:00,720 --> 04:08:04,720
i'll use a template literal
6557
04:08:04,720 --> 04:08:07,840
buy this course for
6558
04:08:07,840 --> 04:08:10,159
five hundred dollars okay let's create
6559
04:08:10,159 --> 04:08:12,720
two more messages
6560
04:08:12,720 --> 04:08:14,960
second message
6561
04:08:14,960 --> 04:08:17,600
third message for the second message
6562
04:08:17,600 --> 04:08:21,199
let's say that this is not a scam the
6563
04:08:21,199 --> 04:08:23,439
third message will be
6564
04:08:23,439 --> 04:08:24,399
do it
6565
04:08:24,399 --> 04:08:26,560
i would like to invoke these functions
6566
04:08:26,560 --> 04:08:29,359
after a given amount of time i will use
6567
04:08:29,359 --> 04:08:30,840
the
6568
04:08:30,840 --> 04:08:33,439
set timeout
6569
04:08:33,439 --> 04:08:34,800
method
6570
04:08:34,800 --> 04:08:37,760
so we pass in a callback a function
6571
04:08:37,760 --> 04:08:38,880
expression
6572
04:08:38,880 --> 04:08:41,199
or an arrow function expression just to
6573
04:08:41,199 --> 04:08:43,120
keep things simple i'm going to pass a
6574
04:08:43,120 --> 04:08:45,120
callback let's begin with the first
6575
04:08:45,120 --> 04:08:47,439
message that's the first argument the
6576
04:08:47,439 --> 04:08:49,600
callback is the first argument
6577
04:08:49,600 --> 04:08:52,159
and then a given amount of milliseconds
6578
04:08:52,159 --> 04:08:54,560
we would like this function to execute
6579
04:08:54,560 --> 04:08:57,439
after three thousand milliseconds three
6580
04:08:57,439 --> 04:09:00,399
seconds we will alert the user to buy
6581
04:09:00,399 --> 04:09:02,640
this course for five hundred dollars it
6582
04:09:02,640 --> 04:09:05,120
is possible to have multiple set timeout
6583
04:09:05,120 --> 04:09:07,840
methods executing concurrently let's
6584
04:09:07,840 --> 04:09:09,840
invoke the set timeout method two
6585
04:09:09,840 --> 04:09:11,359
additional times
6586
04:09:11,359 --> 04:09:12,880
after about
6587
04:09:12,880 --> 04:09:16,159
maybe six seconds we will invoke the
6588
04:09:16,159 --> 04:09:17,840
second message function
6589
04:09:17,840 --> 04:09:20,080
then we will invoke the third message
6590
04:09:20,080 --> 04:09:22,640
function after nine seconds
6591
04:09:22,640 --> 04:09:25,439
okay let's try
6592
04:09:27,439 --> 04:09:31,920
buy this course for five hundred dollars
6593
04:09:31,920 --> 04:09:34,399
this is not a scam
6594
04:09:34,399 --> 04:09:36,960
do it if at any time you ever need to
6595
04:09:36,960 --> 04:09:39,600
clear or cancel your set timeout method
6596
04:09:39,600 --> 04:09:42,399
you can use the clear timeout method
6597
04:09:42,399 --> 04:09:44,399
but i'm going to link this to a button
6598
04:09:44,399 --> 04:09:46,159
let's create a button within our html
6599
04:09:46,159 --> 04:09:48,159
document
6600
04:09:48,159 --> 04:09:50,399
button
6601
04:09:50,399 --> 04:09:53,760
id of my button
6602
04:09:53,760 --> 04:09:56,560
and the text will be by if we want to
6603
04:09:56,560 --> 04:09:58,239
buy that fictional course that we're
6604
04:09:58,239 --> 04:09:59,199
selling
6605
04:09:59,199 --> 04:10:03,120
i need to select this button by its id
6606
04:10:03,120 --> 04:10:04,239
document
6607
04:10:04,239 --> 04:10:05,199
dot
6608
04:10:05,199 --> 04:10:06,159
get
6609
04:10:06,159 --> 04:10:08,800
element by id
6610
04:10:08,800 --> 04:10:13,120
the id was my button set the on click
6611
04:10:13,120 --> 04:10:17,040
attribute equal to a function
6612
04:10:17,040 --> 04:10:19,120
i will invoke the clear
6613
04:10:19,120 --> 04:10:20,800
timeout method
6614
04:10:20,800 --> 04:10:23,520
but we need to pass in the id of a timer
6615
04:10:23,520 --> 04:10:25,840
when you invoke the set timeout method
6616
04:10:25,840 --> 04:10:28,399
it will return an id so let's store that
6617
04:10:28,399 --> 04:10:30,880
within a variable
6618
04:10:30,880 --> 04:10:31,920
let
6619
04:10:31,920 --> 04:10:33,680
timer 1
6620
04:10:33,680 --> 04:10:35,840
equal set timeout
6621
04:10:35,840 --> 04:10:38,560
then let's create timer two
6622
04:10:38,560 --> 04:10:40,960
and timer three
6623
04:10:40,960 --> 04:10:43,439
so we will pass these variables as
6624
04:10:43,439 --> 04:10:47,120
arguments to the clear timeout method
6625
04:10:47,120 --> 04:10:50,319
so copy and paste timers one two and
6626
04:10:50,319 --> 04:10:52,880
three let's alert the user so if they
6627
04:10:52,880 --> 04:10:55,120
click on this buy button they buy our
6628
04:10:55,120 --> 04:10:57,680
fictional course
6629
04:10:57,680 --> 04:11:00,800
thanks for buying
6630
04:11:01,199 --> 04:11:03,279
buy this course for five hundred dollars
6631
04:11:03,279 --> 04:11:04,640
i'll click buy
6632
04:11:04,640 --> 04:11:06,720
thanks for buying that will cancel and
6633
04:11:06,720 --> 04:11:09,040
clear the set timeout methods
6634
04:11:09,040 --> 04:11:10,640
if you need to pass arguments to a
6635
04:11:10,640 --> 04:11:12,640
function you can list those after the
6636
04:11:12,640 --> 04:11:14,880
milliseconds argument let's create
6637
04:11:14,880 --> 04:11:16,479
variable item
6638
04:11:16,479 --> 04:11:18,319
we would like to sell
6639
04:11:18,319 --> 04:11:23,680
how about a crypto currency this time
6640
04:11:23,680 --> 04:11:26,000
we will list a price let
6641
04:11:26,000 --> 04:11:30,319
price equals 420 69. so i'm going to
6642
04:11:30,319 --> 04:11:32,720
pass these two variables as arguments
6643
04:11:32,720 --> 04:11:33,600
item
6644
04:11:33,600 --> 04:11:35,760
and price when i invoke the first
6645
04:11:35,760 --> 04:11:37,920
message function but we need parameters
6646
04:11:37,920 --> 04:11:40,239
item and price
6647
04:11:40,239 --> 04:11:42,720
let's actually use these buy
6648
04:11:42,720 --> 04:11:44,319
this
6649
04:11:44,319 --> 04:11:45,520
item
6650
04:11:45,520 --> 04:11:48,080
for
6651
04:11:48,080 --> 04:11:48,880
price
6652
04:11:48,880 --> 04:11:52,159
buy this cryptocurrency for 4 20 69. so
6653
04:11:52,159 --> 04:11:54,319
yeah everybody that is the set timeout
6654
04:11:54,319 --> 04:11:57,040
method it invokes a function after a
6655
04:11:57,040 --> 04:11:58,880
number of milliseconds it's an
6656
04:11:58,880 --> 04:12:00,960
asynchronous function it doesn't pause
6657
04:12:00,960 --> 04:12:02,399
the execution of the rest of your
6658
04:12:02,399 --> 04:12:04,640
program if you need to perform a task
6659
04:12:04,640 --> 04:12:06,640
after a given amount of time you can
6660
04:12:06,640 --> 04:12:09,199
invoke the set timeout method hey if you
6661
04:12:09,199 --> 04:12:10,880
found this video helpful please be sure
6662
04:12:10,880 --> 04:12:12,800
to smash that like button leave a random
6663
04:12:12,800 --> 04:12:14,479
comment down below and subscribe if
6664
04:12:14,479 --> 04:12:17,840
you'd like to become a fellow bro
6665
04:12:17,840 --> 04:12:19,520
hey guys in this video i'm going to
6666
04:12:19,520 --> 04:12:22,159
explain the set interval method the set
6667
04:12:22,159 --> 04:12:24,319
interval method much like the set
6668
04:12:24,319 --> 04:12:26,640
timeout method it invokes a function
6669
04:12:26,640 --> 04:12:28,239
repeatedly after a number of
6670
04:12:28,239 --> 04:12:30,239
milliseconds it's an asynchronous
6671
04:12:30,239 --> 04:12:32,080
function it doesn't pause the execution
6672
04:12:32,080 --> 04:12:34,239
of your program let's create a count up
6673
04:12:34,239 --> 04:12:37,279
timer i will declare a counter let
6674
04:12:37,279 --> 04:12:39,760
count set the sql to zero and we will
6675
04:12:39,760 --> 04:12:43,840
accept some user input let max equals
6676
04:12:43,840 --> 04:12:44,880
window
6677
04:12:44,880 --> 04:12:47,680
dot prompt
6678
04:12:47,760 --> 04:12:51,359
count up to what number i'll need to
6679
04:12:51,359 --> 04:12:53,520
convert the user input to a number
6680
04:12:53,520 --> 04:12:55,279
because it's normally of the string data
6681
04:12:55,279 --> 04:12:57,520
type when we accept user input now let's
6682
04:12:57,520 --> 04:12:59,920
invoke the set interval method
6683
04:12:59,920 --> 04:13:00,960
set
6684
04:13:00,960 --> 04:13:02,000
interval
6685
04:13:02,000 --> 04:13:04,399
we can pass in a callback a function
6686
04:13:04,399 --> 04:13:06,960
expression an arrow function expression
6687
04:13:06,960 --> 04:13:08,399
let's just pass a callback to keep
6688
04:13:08,399 --> 04:13:10,319
things simple count
6689
04:13:10,319 --> 04:13:12,479
up after how many milliseconds would we
6690
04:13:12,479 --> 04:13:14,479
like to repeat this function maybe one
6691
04:13:14,479 --> 04:13:16,640
thousand for one second let's declare
6692
04:13:16,640 --> 04:13:18,640
this function
6693
04:13:18,640 --> 04:13:19,840
function
6694
04:13:19,840 --> 04:13:21,199
count up
6695
04:13:21,199 --> 04:13:23,439
i will increment count by one this will
6696
04:13:23,439 --> 04:13:25,439
be our counter and we will display
6697
04:13:25,439 --> 04:13:26,960
whatever count is
6698
04:13:26,960 --> 04:13:29,760
console.log
6699
04:13:29,760 --> 04:13:32,479
count let's stop if
6700
04:13:32,479 --> 04:13:33,680
count
6701
04:13:33,680 --> 04:13:36,319
is greater than or equal to
6702
04:13:36,319 --> 04:13:39,040
max the number that we enter in the user
6703
04:13:39,040 --> 04:13:42,159
input to stop the set interval method we
6704
04:13:42,159 --> 04:13:46,080
can use the clear interval method
6705
04:13:46,080 --> 04:13:47,920
however as an argument we need to pass
6706
04:13:47,920 --> 04:13:50,479
in the id of the set interval method so
6707
04:13:50,479 --> 04:13:52,399
we can actually assign that
6708
04:13:52,399 --> 04:13:53,520
constant
6709
04:13:53,520 --> 04:13:56,640
let's name this my timer equals set
6710
04:13:56,640 --> 04:13:59,439
interval my timer is storing the id of
6711
04:13:59,439 --> 04:14:01,600
the set interval method i'll pass that
6712
04:14:01,600 --> 04:14:03,359
as an argument to the clear interval
6713
04:14:03,359 --> 04:14:05,600
method and when i run this we should
6714
04:14:05,600 --> 04:14:08,000
count up to a number that we set count
6715
04:14:08,000 --> 04:14:10,000
up to what let's count up to ten
6716
04:14:10,000 --> 04:14:12,560
press okay
6717
04:14:12,560 --> 04:14:17,439
okay we begin at one two three four
6718
04:14:17,439 --> 04:14:20,720
and we should stop at ten
6719
04:14:21,359 --> 04:14:23,840
yeah if you ever need to pass arguments
6720
04:14:23,840 --> 04:14:26,239
to a function a callback maybe a lot of
6721
04:14:26,239 --> 04:14:28,560
these variables are within a function so
6722
04:14:28,560 --> 04:14:30,800
if i need to pass max i can add that as
6723
04:14:30,800 --> 04:14:32,560
an argument then make sure to have a
6724
04:14:32,560 --> 04:14:34,640
matching set of parameters so that is
6725
04:14:34,640 --> 04:14:36,800
the set interval method everybody it
6726
04:14:36,800 --> 04:14:39,600
invokes a function repeatedly after a
6727
04:14:39,600 --> 04:14:41,520
number of milliseconds if this video
6728
04:14:41,520 --> 04:14:43,359
helped you out please be sure to smash
6729
04:14:43,359 --> 04:14:44,800
that like button leave a random comment
6730
04:14:44,800 --> 04:14:46,399
down below and subscribe if you'd like
6731
04:14:46,399 --> 04:14:49,520
to become a fellow bro
6732
04:14:49,520 --> 04:14:51,840
hey guys in this video i'm gonna discuss
6733
04:14:51,840 --> 04:14:54,080
date objects date objects are used to
6734
04:14:54,080 --> 04:14:56,479
work with dates and times to create a
6735
04:14:56,479 --> 04:14:58,080
date object we just call the date
6736
04:14:58,080 --> 04:14:59,600
constructor let
6737
04:14:59,600 --> 04:15:02,640
date equal new date
6738
04:15:02,640 --> 04:15:04,479
and then we can display the date
6739
04:15:04,479 --> 04:15:06,640
console dialogue
6740
04:15:06,640 --> 04:15:08,800
date and here's the current date and
6741
04:15:08,800 --> 04:15:10,640
time including time zone although it's
6742
04:15:10,640 --> 04:15:12,560
not that readable we can actually change
6743
04:15:12,560 --> 04:15:16,319
that i will set the date equal to date
6744
04:15:16,319 --> 04:15:17,120
dot
6745
04:15:17,120 --> 04:15:18,000
2
6746
04:15:18,000 --> 04:15:19,040
locale
6747
04:15:19,040 --> 04:15:20,720
string method
6748
04:15:20,720 --> 04:15:22,640
and that is a lot more readable hey for
6749
04:15:22,640 --> 04:15:24,720
fun within our dom let's create a label
6750
04:15:24,720 --> 04:15:26,319
and update the label with the current
6751
04:15:26,319 --> 04:15:29,199
date and time so within my html file i'm
6752
04:15:29,199 --> 04:15:31,840
going to create a new
6753
04:15:31,840 --> 04:15:34,000
close it
6754
04:15:34,000 --> 04:15:36,319
i'll give this a unique id my label is
6755
04:15:36,319 --> 04:15:38,080
fine
6756
04:15:38,080 --> 04:15:40,319
and i will change the inner html of my
6757
04:15:40,319 --> 04:15:42,080
label
6758
04:15:42,080 --> 04:15:44,479
document dot
6759
04:15:44,479 --> 04:15:45,279
get
6760
04:15:45,279 --> 04:15:47,040
element by
6761
04:15:47,040 --> 04:15:48,319
id
6762
04:15:48,319 --> 04:15:52,080
pass in a unique id my label change the
6763
04:15:52,080 --> 04:15:55,120
inner html equal to
6764
04:15:55,120 --> 04:15:57,359
date
6765
04:15:57,359 --> 04:15:59,279
yeah it's pretty cool right now with the
6766
04:15:59,279 --> 04:16:01,120
day constructor if you don't pass in any
6767
04:16:01,120 --> 04:16:03,120
arguments date will equal the current
6768
04:16:03,120 --> 04:16:05,680
date and time however if we pass zero as
6769
04:16:05,680 --> 04:16:07,840
an argument zero is a reference point
6770
04:16:07,840 --> 04:16:10,239
for us this is known as the epic it will
6771
04:16:10,239 --> 04:16:12,800
be some date around the year 1969.
6772
04:16:12,800 --> 04:16:14,960
imagine this is the date where time
6773
04:16:14,960 --> 04:16:17,279
began not really but just imagine it
6774
04:16:17,279 --> 04:16:19,359
this is a reference point for us within
6775
04:16:19,359 --> 04:16:21,199
the day constructor we can pass an
6776
04:16:21,199 --> 04:16:23,439
amount of milliseconds as an argument so
6777
04:16:23,439 --> 04:16:25,439
if i pass in i don't know how many
6778
04:16:25,439 --> 04:16:27,760
milliseconds that is this will create a
6779
04:16:27,760 --> 04:16:31,120
new date 10 million milliseconds after
6780
04:16:31,120 --> 04:16:33,359
this date the starting point so 10
6781
04:16:33,359 --> 04:16:36,720
million milliseconds after our epic is
6782
04:16:36,720 --> 04:16:39,520
the same day but 8 o'clock at night so
6783
04:16:39,520 --> 04:16:40,720
i'm just gonna pass in some random
6784
04:16:40,720 --> 04:16:43,520
numbers and see what dates we get
6785
04:16:43,520 --> 04:16:45,680
this number equates to the date
6786
04:16:45,680 --> 04:16:47,680
september 8th 2001
6787
04:16:47,680 --> 04:16:50,239
8pm and let's see what this is
6788
04:16:50,239 --> 04:16:52,880
okay now we're in the year 33 000. so
6789
04:16:52,880 --> 04:16:54,640
you can pass an amount of milliseconds
6790
04:16:54,640 --> 04:16:56,880
to the date constructor but zero is a
6791
04:16:56,880 --> 04:16:58,399
reference point there are additional
6792
04:16:58,399 --> 04:17:00,159
arguments you can pass too the first
6793
04:17:00,159 --> 04:17:02,159
argument is a year let's pick the year
6794
04:17:02,159 --> 04:17:05,520
2023 the next argument is the month zero
6795
04:17:05,520 --> 04:17:07,760
corresponds with january february
6796
04:17:07,760 --> 04:17:09,760
corresponds with one then you just
6797
04:17:09,760 --> 04:17:12,080
follow that pattern i'll pick uh let's
6798
04:17:12,080 --> 04:17:14,399
say january so zero the next arguments
6799
04:17:14,399 --> 04:17:15,760
for the day
6800
04:17:15,760 --> 04:17:17,279
the hour
6801
04:17:17,279 --> 04:17:18,239
minutes
6802
04:17:18,239 --> 04:17:19,279
seconds
6803
04:17:19,279 --> 04:17:22,159
and even milliseconds so after passing
6804
04:17:22,159 --> 04:17:24,159
how many arguments do we have here i
6805
04:17:24,159 --> 04:17:25,600
think seven
6806
04:17:25,600 --> 04:17:29,279
the current date and time is january 1st
6807
04:17:29,279 --> 04:17:31,840
2023 about two in the morning if it's
6808
04:17:31,840 --> 04:17:33,760
easier for you you can even pass in a
6809
04:17:33,760 --> 04:17:36,479
string representation of a date and time
6810
04:17:36,479 --> 04:17:38,800
how about january
6811
04:17:38,800 --> 04:17:40,479
1st
6812
04:17:40,479 --> 04:17:43,439
2023
6813
04:17:44,080 --> 04:17:46,159
midnight
6814
04:17:46,159 --> 04:17:47,760
that's another option available to you
6815
04:17:47,760 --> 04:17:50,159
too you can even get properties from a
6816
04:17:50,159 --> 04:17:52,000
current date if you need the year we
6817
04:17:52,000 --> 04:17:54,319
could assign that to a variable let year
6818
04:17:54,319 --> 04:17:55,359
equal
6819
04:17:55,359 --> 04:17:57,439
date dot get
6820
04:17:57,439 --> 04:17:59,520
full year
6821
04:17:59,520 --> 04:18:02,399
and let's display this
6822
04:18:02,399 --> 04:18:05,120
so the current year is 2022
6823
04:18:05,120 --> 04:18:06,560
day of the month
6824
04:18:06,560 --> 04:18:08,800
day of month
6825
04:18:08,800 --> 04:18:11,680
the corresponding method is get
6826
04:18:11,680 --> 04:18:13,520
date
6827
04:18:13,520 --> 04:18:15,600
let's display day of month
6828
04:18:15,600 --> 04:18:19,880
and it is the seventh currently
6829
04:18:21,279 --> 04:18:23,359
day of week
6830
04:18:23,359 --> 04:18:24,560
get
6831
04:18:24,560 --> 04:18:26,800
day
6832
04:18:27,680 --> 04:18:30,479
sunday is zero monday is one tuesday is
6833
04:18:30,479 --> 04:18:32,560
two you just follow that pattern since
6834
04:18:32,560 --> 04:18:34,399
day of week is one that means it's
6835
04:18:34,399 --> 04:18:37,600
monday we have access to the month
6836
04:18:37,600 --> 04:18:40,000
get month
6837
04:18:40,000 --> 04:18:42,960
the month is currently one january is
6838
04:18:42,960 --> 04:18:45,439
zero february is one march is two you
6839
04:18:45,439 --> 04:18:47,680
follow that pattern we have access to
6840
04:18:47,680 --> 04:18:49,040
hours
6841
04:18:49,040 --> 04:18:51,120
get
6842
04:18:51,120 --> 04:18:52,479
hours
6843
04:18:52,479 --> 04:18:54,960
the current hour is 10
6844
04:18:54,960 --> 04:18:57,840
this is in military time so the possible
6845
04:18:57,840 --> 04:19:00,640
hours are between 0 and 23
6846
04:19:00,640 --> 04:19:03,040
minutes
6847
04:19:03,040 --> 04:19:05,359
get minutes
6848
04:19:05,359 --> 04:19:07,359
so the current time for me is six
6849
04:19:07,359 --> 04:19:08,720
minutes after 10
6850
04:19:08,720 --> 04:19:10,239
seconds
6851
04:19:10,239 --> 04:19:11,920
get seconds
6852
04:19:11,920 --> 04:19:13,680
every time i refresh this page you can
6853
04:19:13,680 --> 04:19:15,760
see that the seconds is going up and
6854
04:19:15,760 --> 04:19:18,000
even milliseconds let's name this
6855
04:19:18,000 --> 04:19:20,880
variable ms for short
6856
04:19:20,880 --> 04:19:21,760
get
6857
04:19:21,760 --> 04:19:24,560
milliseconds
6858
04:19:24,560 --> 04:19:26,239
and this is pretty much updating every
6859
04:19:26,239 --> 04:19:28,479
time i refresh the page so if you need
6860
04:19:28,479 --> 04:19:30,000
one of these properties of a date you
6861
04:19:30,000 --> 04:19:32,000
can use an associated method you can
6862
04:19:32,000 --> 04:19:34,239
also set these properties too i would
6863
04:19:34,239 --> 04:19:37,520
like to set the year of my date date
6864
04:19:37,520 --> 04:19:42,159
set full year i'll pass in the year 2024
6865
04:19:42,159 --> 04:19:44,560
and let's display our date
6866
04:19:44,560 --> 04:19:46,960
so for me i'm recording this on february
6867
04:19:46,960 --> 04:19:50,399
7th but the year is changed to 2024
6868
04:19:50,399 --> 04:19:52,800
there's also set month
6869
04:19:52,800 --> 04:19:55,600
date dot set month i'll change the month
6870
04:19:55,600 --> 04:19:58,560
to december so that would be 11 december
6871
04:19:58,560 --> 04:20:01,199
7th 2024 you can set the day of the
6872
04:20:01,199 --> 04:20:02,720
month
6873
04:20:02,720 --> 04:20:07,600
date dot set date i'll change this to 31
6874
04:20:07,600 --> 04:20:11,520
december 31st 2024 we have set hours
6875
04:20:11,520 --> 04:20:15,120
date dot set hours let's move this to 12
6876
04:20:15,120 --> 04:20:18,560
that would be 12 p.m and 23 would be 11
6877
04:20:18,560 --> 04:20:21,520
p.m same thing applies four minutes set
6878
04:20:21,520 --> 04:20:24,640
minutes what about one seconds set
6879
04:20:24,640 --> 04:20:25,680
seconds
6880
04:20:25,680 --> 04:20:27,680
i don't know 30 and even milliseconds
6881
04:20:27,680 --> 04:20:28,720
although we're not displaying it
6882
04:20:28,720 --> 04:20:30,960
currently date dot set milliseconds and
6883
04:20:30,960 --> 04:20:32,880
pass in some amount of milliseconds so
6884
04:20:32,880 --> 04:20:35,199
those are various set methods you can
6885
04:20:35,199 --> 04:20:37,279
set a property of a date we can even
6886
04:20:37,279 --> 04:20:38,960
create our own custom functions to
6887
04:20:38,960 --> 04:20:40,960
format a date and time so i'm going to
6888
04:20:40,960 --> 04:20:42,479
get rid of this line let's create a
6889
04:20:42,479 --> 04:20:45,439
function to format the date first
6890
04:20:45,439 --> 04:20:46,640
function
6891
04:20:46,640 --> 04:20:48,800
format date
6892
04:20:48,800 --> 04:20:51,680
we will accept a date as an argument
6893
04:20:51,680 --> 04:20:53,520
let's create a few variables for the
6894
04:20:53,520 --> 04:20:55,680
year month and day
6895
04:20:55,680 --> 04:20:59,120
let year equal and we can get the year
6896
04:20:59,120 --> 04:21:01,040
date dot get
6897
04:21:01,040 --> 04:21:03,279
full year
6898
04:21:03,279 --> 04:21:04,880
let month
6899
04:21:04,880 --> 04:21:08,800
equal date dot get month
6900
04:21:08,800 --> 04:21:11,199
and let day
6901
04:21:11,199 --> 04:21:13,040
equal date
6902
04:21:13,040 --> 04:21:13,920
dot
6903
04:21:13,920 --> 04:21:14,880
get
6904
04:21:14,880 --> 04:21:17,040
date so let's return a string
6905
04:21:17,040 --> 04:21:20,319
representation of the year month and day
6906
04:21:20,319 --> 04:21:23,040
return i'll use a template literal so if
6907
04:21:23,040 --> 04:21:24,560
you would like the month first like
6908
04:21:24,560 --> 04:21:26,159
what's displayed in my web browser we
6909
04:21:26,159 --> 04:21:28,399
can put the month first
6910
04:21:28,399 --> 04:21:31,840
then the day next
6911
04:21:33,199 --> 04:21:35,120
end of the year
6912
04:21:35,120 --> 04:21:37,680
when i update my label i will invoke the
6913
04:21:37,680 --> 04:21:39,840
format date function that i created and
6914
04:21:39,840 --> 04:21:41,600
pass in our date
6915
04:21:41,600 --> 04:21:44,159
there we go remember though that january
6916
04:21:44,159 --> 04:21:46,239
is zero and february is one but
6917
04:21:46,239 --> 04:21:48,159
whoever's using this program probably
6918
04:21:48,159 --> 04:21:50,319
won't know that let's add one to our
6919
04:21:50,319 --> 04:21:51,600
month
6920
04:21:51,600 --> 04:21:53,520
there now it's february and for some
6921
04:21:53,520 --> 04:21:55,120
additional practice let's create a
6922
04:21:55,120 --> 04:21:57,040
format time function
6923
04:21:57,040 --> 04:21:58,399
function
6924
04:21:58,399 --> 04:22:00,399
format time
6925
04:22:00,399 --> 04:22:02,960
we have one parameter a date
6926
04:22:02,960 --> 04:22:07,920
let hours equal date dot get hours
6927
04:22:07,920 --> 04:22:09,840
let minutes
6928
04:22:09,840 --> 04:22:13,439
equal date dot get minutes
6929
04:22:13,439 --> 04:22:15,359
let seconds
6930
04:22:15,359 --> 04:22:19,439
equal date dot get seconds
6931
04:22:19,439 --> 04:22:21,680
let's return a string representation of
6932
04:22:21,680 --> 04:22:24,640
the hours minutes and seconds i'll use a
6933
04:22:24,640 --> 04:22:27,439
template literal
6934
04:22:27,920 --> 04:22:30,920
hours
6935
04:22:32,080 --> 04:22:34,479
minutes
6936
04:22:36,720 --> 04:22:38,080
seconds
6937
04:22:38,080 --> 04:22:40,159
let's invoke the format time function
6938
04:22:40,159 --> 04:22:42,880
that we created
6939
04:22:44,720 --> 04:22:46,640
yeah and that's my current time but it's
6940
04:22:46,640 --> 04:22:48,640
currently in military time let's change
6941
04:22:48,640 --> 04:22:52,239
it to standard let's add am or pm let i
6942
04:22:52,239 --> 04:22:54,560
don't know what to name this variable am
6943
04:22:54,560 --> 04:22:55,520
or
6944
04:22:55,520 --> 04:22:57,279
pm i guess
6945
04:22:57,279 --> 04:23:00,399
equals and i'll use the ternary operator
6946
04:23:00,399 --> 04:23:03,680
our condition is we'll check if hours
6947
04:23:03,680 --> 04:23:06,479
is greater than or equal to 12
6948
04:23:06,479 --> 04:23:08,800
question mark if that's true we will
6949
04:23:08,800 --> 04:23:13,439
return pm if it's false we'll return am
6950
04:23:13,439 --> 04:23:15,920
and i will display
6951
04:23:15,920 --> 04:23:17,840
my variable am
6952
04:23:17,840 --> 04:23:19,680
or pm
6953
04:23:19,680 --> 04:23:22,159
and it is currently am the hours are
6954
04:23:22,159 --> 04:23:24,239
still currently in military time so to
6955
04:23:24,239 --> 04:23:26,159
convert that to standard this is one way
6956
04:23:26,159 --> 04:23:28,560
in which we can do that let's reassign
6957
04:23:28,560 --> 04:23:32,640
hours equal to we'll write hours
6958
04:23:32,640 --> 04:23:35,680
modulus 12. modulus finds the remainder
6959
04:23:35,680 --> 04:23:37,920
of any division then i'll use the
6960
04:23:37,920 --> 04:23:40,080
bitwise or operator
6961
04:23:40,080 --> 04:23:42,800
12. we're taking hours modulus 12. if
6962
04:23:42,800 --> 04:23:45,040
ours is currently 12 this expression
6963
04:23:45,040 --> 04:23:47,439
would evaluate to be zero then using the
6964
04:23:47,439 --> 04:23:50,239
orbit wise operator we would instead use
6965
04:23:50,239 --> 04:23:52,960
12. so this line will convert your hours
6966
04:23:52,960 --> 04:23:55,359
from military to standard so yeah
6967
04:23:55,359 --> 04:23:57,520
everybody that's a few things we can do
6968
04:23:57,520 --> 04:23:59,600
with date objects they're used to work
6969
04:23:59,600 --> 04:24:01,600
with dates and times if you would like a
6970
04:24:01,600 --> 04:24:03,520
copy of this code i'll post all of this
6971
04:24:03,520 --> 04:24:05,199
in the comment section down below and
6972
04:24:05,199 --> 04:24:08,840
well yeah those are date objects in
6973
04:24:08,840 --> 04:24:11,680
javascript hey everybody in this video
6974
04:24:11,680 --> 04:24:13,199
we're going to create a practice program
6975
04:24:13,199 --> 04:24:15,359
to create a clock that will update every
6976
04:24:15,359 --> 04:24:17,920
second let's begin by going to our html
6977
04:24:17,920 --> 04:24:20,080
file and i will add a label we'll use a
6978
04:24:20,080 --> 04:24:23,600
pair of label tags and i will set the id
6979
04:24:23,600 --> 04:24:25,040
equal to
6980
04:24:25,040 --> 04:24:25,920
my
6981
04:24:25,920 --> 04:24:26,880
label
6982
04:24:26,880 --> 04:24:29,040
save head back to your html document
6983
04:24:29,040 --> 04:24:30,960
i'll assign that variable to a label
6984
04:24:30,960 --> 04:24:33,760
constant my label
6985
04:24:33,760 --> 04:24:34,880
equals
6986
04:24:34,880 --> 04:24:40,319
document dot get element by id
6987
04:24:40,319 --> 04:24:42,800
the id was my label
6988
04:24:42,800 --> 04:24:45,520
and we'll create an update function
6989
04:24:45,520 --> 04:24:47,359
function
6990
04:24:47,359 --> 04:24:48,880
update
6991
04:24:48,880 --> 04:24:50,720
and we will get the current date and
6992
04:24:50,720 --> 04:24:51,600
time
6993
04:24:51,600 --> 04:24:52,399
let
6994
04:24:52,399 --> 04:24:54,800
date equal new
6995
04:24:54,800 --> 04:24:55,840
date
6996
04:24:55,840 --> 04:24:58,159
let's update this label just to test it
6997
04:24:58,159 --> 04:25:03,120
my label dot inner html equals date
6998
04:25:03,120 --> 04:25:05,600
and then we should invoke this function
6999
04:25:05,600 --> 04:25:07,520
update
7000
04:25:07,520 --> 04:25:09,840
so that's the current date and time but
7001
04:25:09,840 --> 04:25:11,920
i would like to format this within our
7002
04:25:11,920 --> 04:25:13,760
update function let's create a nested
7003
04:25:13,760 --> 04:25:16,800
function to format the time
7004
04:25:16,800 --> 04:25:17,920
function
7005
04:25:17,920 --> 04:25:20,560
format time
7006
04:25:20,560 --> 04:25:24,560
let's get the hours let hours equal date
7007
04:25:24,560 --> 04:25:27,920
dot get hours
7008
04:25:27,920 --> 04:25:30,479
the minutes
7009
04:25:30,720 --> 04:25:35,279
let minutes date dot get minutes
7010
04:25:35,279 --> 04:25:38,080
and seconds
7011
04:25:38,239 --> 04:25:41,920
seconds date dot get seconds
7012
04:25:41,920 --> 04:25:46,479
let's return a template literal
7013
04:25:47,279 --> 04:25:49,840
hours
7014
04:25:51,199 --> 04:25:53,600
minutes
7015
04:25:55,520 --> 04:25:57,040
seconds
7016
04:25:57,040 --> 04:25:58,640
make sure you spell return right let's
7017
04:25:58,640 --> 04:26:01,199
invoke the format time function
7018
04:26:01,199 --> 04:26:03,840
and we should probably pass a date
7019
04:26:03,840 --> 04:26:05,760
so set up a date as an argument and a
7020
04:26:05,760 --> 04:26:07,359
parameter
7021
04:26:07,359 --> 04:26:09,439
this time is in military time so let's
7022
04:26:09,439 --> 04:26:12,880
set this to standard we'll need am or pm
7023
04:26:12,880 --> 04:26:15,279
let am or
7024
04:26:15,279 --> 04:26:16,880
pm
7025
04:26:16,880 --> 04:26:20,159
equal i'll use a ternary operator
7026
04:26:20,159 --> 04:26:22,080
we'll check if hours is greater than or
7027
04:26:22,080 --> 04:26:24,800
equal to 12 question mark if that's true
7028
04:26:24,800 --> 04:26:26,319
return pm
7029
04:26:26,319 --> 04:26:29,120
otherwise return am
7030
04:26:29,120 --> 04:26:33,199
let's add our variable am or pm
7031
04:26:33,359 --> 04:26:36,640
my current time is 12 22 pm the hours is
7032
04:26:36,640 --> 04:26:38,159
still in military time although you
7033
04:26:38,159 --> 04:26:40,239
can't tell from my example to convert
7034
04:26:40,239 --> 04:26:43,439
that between 1 and 12 i can set hours
7035
04:26:43,439 --> 04:26:45,680
equal to
7036
04:26:45,680 --> 04:26:48,399
hours modulus 12
7037
04:26:48,399 --> 04:26:51,040
bitwise operator 12.
7038
04:26:51,040 --> 04:26:52,880
modulus gives you the remainder of any
7039
04:26:52,880 --> 04:26:55,520
division if hours is 12
7040
04:26:55,520 --> 04:26:57,520
then the remainder of 12 divided by 12
7041
04:26:57,520 --> 04:26:58,640
is 0
7042
04:26:58,640 --> 04:27:01,439
then we would instead use 12 in place of
7043
04:27:01,439 --> 04:27:04,239
0 using the or bitwise operator since
7044
04:27:04,239 --> 04:27:06,399
i'm filming this at 12 pm there's no
7045
04:27:06,399 --> 04:27:08,399
change i would like this clock to update
7046
04:27:08,399 --> 04:27:10,479
every second after we invoke the update
7047
04:27:10,479 --> 04:27:13,520
function let's invoke the set
7048
04:27:13,520 --> 04:27:16,399
interval method of our window object the
7049
04:27:16,399 --> 04:27:18,880
first argument is a callback
7050
04:27:18,880 --> 04:27:20,399
we'll pass our update function as a
7051
04:27:20,399 --> 04:27:23,520
callback and then a delay every 1000
7052
04:27:23,520 --> 04:27:25,600
milliseconds i would like to update our
7053
04:27:25,600 --> 04:27:26,399
clock
7054
04:27:26,399 --> 04:27:29,359
there it's updating currently
7055
04:27:29,359 --> 04:27:31,120
now if one of these variables is a
7056
04:27:31,120 --> 04:27:33,040
single digit we don't have any leading
7057
04:27:33,040 --> 04:27:34,399
zeros
7058
04:27:34,399 --> 04:27:35,680
i'd like to change that if we have a
7059
04:27:35,680 --> 04:27:37,760
single digit i would like a leading zero
7060
04:27:37,760 --> 04:27:40,640
before the actual time unit so let's
7061
04:27:40,640 --> 04:27:43,439
create another nested function
7062
04:27:43,439 --> 04:27:44,960
function
7063
04:27:44,960 --> 04:27:47,359
let's call this format
7064
04:27:47,359 --> 04:27:49,840
zeros
7065
04:27:50,640 --> 04:27:53,439
we'll accept a time let's set our time
7066
04:27:53,439 --> 04:27:55,920
parameter equal to
7067
04:27:55,920 --> 04:27:56,720
time
7068
04:27:56,720 --> 04:27:59,840
dot to string this will convert a number
7069
04:27:59,840 --> 04:28:02,080
to a string then we will use some string
7070
04:28:02,080 --> 04:28:04,159
concatenation
7071
04:28:04,159 --> 04:28:05,920
and return
7072
04:28:05,920 --> 04:28:08,239
we'll use the ternary operator let's
7073
04:28:08,239 --> 04:28:11,359
check the times length property
7074
04:28:11,359 --> 04:28:13,680
if it's less than two that means it's a
7075
04:28:13,680 --> 04:28:15,120
single digit
7076
04:28:15,120 --> 04:28:17,520
if our time length is a single digit
7077
04:28:17,520 --> 04:28:19,840
then we will precede
7078
04:28:19,840 --> 04:28:23,120
our time with zero if it's false we'll
7079
04:28:23,120 --> 04:28:25,359
just return time back so then we just
7080
04:28:25,359 --> 04:28:27,199
need to invoke and reassign hours
7081
04:28:27,199 --> 04:28:29,439
minutes and seconds we'll invoke our
7082
04:28:29,439 --> 04:28:32,399
format zeros function
7083
04:28:32,399 --> 04:28:35,600
pass in our hours
7084
04:28:35,600 --> 04:28:37,680
and do the same thing with minutes and
7085
04:28:37,680 --> 04:28:40,080
seconds
7086
04:28:41,359 --> 04:28:43,840
and we now have some leading zeros
7087
04:28:43,840 --> 04:28:45,760
so yeah i thought that would be a fun
7088
04:28:45,760 --> 04:28:47,840
practice project we have a clock that
7089
04:28:47,840 --> 04:28:49,600
will update every second if you would
7090
04:28:49,600 --> 04:28:51,439
like a copy of this code i'll post this
7091
04:28:51,439 --> 04:28:53,199
to the comment section down below and
7092
04:28:53,199 --> 04:28:55,520
well yeah that's a simple clock program
7093
04:28:55,520 --> 04:28:58,239
in javascript
7094
04:28:58,239 --> 04:29:00,319
hey guys for this topic i will explain
7095
04:29:00,319 --> 04:29:02,239
the differences between synchronous and
7096
04:29:02,239 --> 04:29:04,239
asynchronous code in a span of a few
7097
04:29:04,239 --> 04:29:06,080
minutes synchronous code isn't an
7098
04:29:06,080 --> 04:29:08,239
ordered sequence they are step-by-step
7099
04:29:08,239 --> 04:29:10,000
linear instructions you start some
7100
04:29:10,000 --> 04:29:12,239
process now and you finish now the rest
7101
04:29:12,239 --> 04:29:13,840
of your program has to wait for a
7102
04:29:13,840 --> 04:29:15,600
synchronous process just a quick
7103
04:29:15,600 --> 04:29:17,920
demonstration let's display something
7104
04:29:17,920 --> 04:29:20,159
start
7105
04:29:20,159 --> 04:29:23,120
this step is
7106
04:29:23,120 --> 04:29:25,040
synchronous
7107
04:29:25,040 --> 04:29:26,560
and then
7108
04:29:26,560 --> 04:29:27,760
end
7109
04:29:27,760 --> 04:29:30,000
so these are step-by-step instructions
7110
04:29:30,000 --> 04:29:31,920
in order to move on to the second step
7111
04:29:31,920 --> 04:29:33,520
we first need to complete the first step
7112
04:29:33,520 --> 04:29:35,040
no matter how long the statement will
7113
04:29:35,040 --> 04:29:36,960
take our next step has to wait for the
7114
04:29:36,960 --> 04:29:39,040
first step to finish so our results are
7115
04:29:39,040 --> 04:29:41,439
start this step is synchronous and end
7116
04:29:41,439 --> 04:29:43,439
now asynchronous code can be out of
7117
04:29:43,439 --> 04:29:45,680
sequence these may be tasks such as
7118
04:29:45,680 --> 04:29:48,479
accessing a database fetching a file
7119
04:29:48,479 --> 04:29:50,640
basically tasks that take time they'll
7120
04:29:50,640 --> 04:29:52,560
take an indeterminate amount of time you
7121
04:29:52,560 --> 04:29:54,000
don't want the rest of your program to
7122
04:29:54,000 --> 04:29:55,920
wait around for some process to finish
7123
04:29:55,920 --> 04:29:58,319
you start now finish sometime later and
7124
04:29:58,319 --> 04:29:59,840
the rest of your program can carry on
7125
04:29:59,840 --> 04:30:01,760
with whatever it was doing an example of
7126
04:30:01,760 --> 04:30:04,239
some asynchronous code would be the set
7127
04:30:04,239 --> 04:30:06,000
timeout method so i'm going to replace
7128
04:30:06,000 --> 04:30:08,479
the second line with set
7129
04:30:08,479 --> 04:30:11,520
time out just to keep things simple i'll
7130
04:30:11,520 --> 04:30:14,239
pass in an arrow function expression and
7131
04:30:14,239 --> 04:30:16,159
we will display a message
7132
04:30:16,159 --> 04:30:19,159
console.log
7133
04:30:19,359 --> 04:30:20,880
this is
7134
04:30:20,880 --> 04:30:23,520
asynchronous
7135
04:30:23,520 --> 04:30:26,800
and we'll wait maybe five seconds
7136
04:30:26,800 --> 04:30:29,199
okay we have start and end and that
7137
04:30:29,199 --> 04:30:30,800
asynchronous code should kick in right
7138
04:30:30,800 --> 04:30:33,760
about now so you can see that this step
7139
04:30:33,760 --> 04:30:36,000
is out of order it's asynchronous it's
7140
04:30:36,000 --> 04:30:37,680
out of sequence it's running in the
7141
04:30:37,680 --> 04:30:39,920
background step three in my program
7142
04:30:39,920 --> 04:30:41,359
doesn't need to wait for step two to
7143
04:30:41,359 --> 04:30:44,080
finish step two will start now finish
7144
04:30:44,080 --> 04:30:45,840
sometime later when it's ready that's
7145
04:30:45,840 --> 04:30:47,680
the main differences between synchronous
7146
04:30:47,680 --> 04:30:50,000
and asynchronous code synchronous code
7147
04:30:50,000 --> 04:30:51,680
has to be in order it's a set of linear
7148
04:30:51,680 --> 04:30:54,080
instructions you start now and finish
7149
04:30:54,080 --> 04:30:57,120
now such as a console.log statement
7150
04:30:57,120 --> 04:30:59,520
asynchronous code is out of sequence it
7151
04:30:59,520 --> 04:31:01,520
takes an indeterminate amount of time
7152
04:31:01,520 --> 04:31:03,199
like accessing a database fetching a
7153
04:31:03,199 --> 04:31:06,800
file etc start now finish sometime later
7154
04:31:06,800 --> 04:31:08,560
and it doesn't pause your program it
7155
04:31:08,560 --> 04:31:10,000
will continue on with the rest of the
7156
04:31:10,000 --> 04:31:12,080
instructions so that is a quick
7157
04:31:12,080 --> 04:31:13,920
description of the differences between
7158
04:31:13,920 --> 04:31:15,920
synchronous and asynchronous code in
7159
04:31:15,920 --> 04:31:18,640
javascript
7160
04:31:18,720 --> 04:31:21,279
hey guys here's a super quick video on
7161
04:31:21,279 --> 04:31:24,239
the time method of the console object i
7162
04:31:24,239 --> 04:31:26,080
think you would find this fairly useful
7163
04:31:26,080 --> 04:31:28,720
it's a great utility method its job is
7164
04:31:28,720 --> 04:31:31,040
to start a timer that you can use to
7165
04:31:31,040 --> 04:31:33,439
track how long an operation takes
7166
04:31:33,439 --> 04:31:35,279
and you can give each timer a unique
7167
04:31:35,279 --> 04:31:37,680
name to track how much time has elapsed
7168
04:31:37,680 --> 04:31:40,319
for a given operation i can use
7169
04:31:40,319 --> 04:31:42,560
console.time
7170
04:31:42,560 --> 04:31:44,960
to give this timer a unique name i can
7171
04:31:44,960 --> 04:31:47,279
pass that as a string argument
7172
04:31:47,279 --> 04:31:49,279
let's see how long it takes for a user
7173
04:31:49,279 --> 04:31:50,800
to click a button
7174
04:31:50,800 --> 04:31:54,080
i'll name this timer response
7175
04:31:54,080 --> 04:31:55,760
time
7176
04:31:55,760 --> 04:31:58,000
so this is the start
7177
04:31:58,000 --> 04:31:59,680
to end this timer
7178
04:31:59,680 --> 04:32:02,560
i can use time end at the end of my
7179
04:32:02,560 --> 04:32:06,880
program i will invoke console.time
7180
04:32:06,880 --> 04:32:07,920
end
7181
04:32:07,920 --> 04:32:10,479
and pass the same name as an argument
7182
04:32:10,479 --> 04:32:12,399
response time
7183
04:32:12,399 --> 04:32:14,479
so this is the end if i run this
7184
04:32:14,479 --> 04:32:17,520
currently the time end method will print
7185
04:32:17,520 --> 04:32:20,080
the elapsed time in my console this
7186
04:32:20,080 --> 04:32:21,880
program took
7187
04:32:21,880 --> 04:32:24,640
.0019 milliseconds to complete
7188
04:32:24,640 --> 04:32:26,640
let's actually do something
7189
04:32:26,640 --> 04:32:28,239
i'll create an alert
7190
04:32:28,239 --> 04:32:30,640
alert
7191
04:32:31,840 --> 04:32:35,359
click the ok button
7192
04:32:35,359 --> 04:32:38,080
and let's see how much time elapses
7193
04:32:38,080 --> 04:32:42,080
okay click the ok button i will press ok
7194
04:32:42,080 --> 04:32:43,760
and the response time for me to click on
7195
04:32:43,760 --> 04:32:47,840
that button was 4 86 milliseconds so
7196
04:32:47,840 --> 04:32:49,359
about 4 seconds
7197
04:32:49,359 --> 04:32:51,040
if you ever need to track how long an
7198
04:32:51,040 --> 04:32:53,359
operation takes you can use the time
7199
04:32:53,359 --> 04:32:55,680
method of the console object however if
7200
04:32:55,680 --> 04:32:57,920
you have an asynchronous process
7201
04:32:57,920 --> 04:32:59,520
well the time end method isn't going to
7202
04:32:59,520 --> 04:33:01,040
wait around for it
7203
04:33:01,040 --> 04:33:04,160
so what if we have set timeout set
7204
04:33:04,160 --> 04:33:06,561
time out
7205
04:33:06,561 --> 04:33:08,639
we'll just display a message we need a
7206
04:33:08,639 --> 04:33:10,639
callback i'll simply just pass in an
7207
04:33:10,639 --> 04:33:12,080
arrow function
7208
04:33:12,080 --> 04:33:15,080
console.log
7209
04:33:15,759 --> 04:33:17,199
hello
7210
04:33:17,199 --> 04:33:19,520
i would like this to execute after three
7211
04:33:19,520 --> 04:33:21,599
thousand milliseconds so three seconds
7212
04:33:21,599 --> 04:33:23,278
so when i run this
7213
04:33:23,278 --> 04:33:25,759
we have already completed our program
7214
04:33:25,759 --> 04:33:27,840
and then the set timeout function kicks
7215
04:33:27,840 --> 04:33:28,561
in
7216
04:33:28,561 --> 04:33:30,400
so the time method and the time end
7217
04:33:30,400 --> 04:33:33,199
method will track how long a synchronous
7218
04:33:33,199 --> 04:33:35,278
operation takes so yeah i thought that
7219
04:33:35,278 --> 04:33:37,438
would be a very useful utility method
7220
04:33:37,438 --> 04:33:39,438
that you might be interested in if you
7221
04:33:39,438 --> 04:33:41,118
would like a copy of this code i'll post
7222
04:33:41,118 --> 04:33:42,799
this in the comment section down below
7223
04:33:42,799 --> 04:33:44,958
and well yeah that's the time method of
7224
04:33:44,958 --> 04:33:48,240
the console object
7225
04:33:48,240 --> 04:33:51,199
hey everybody we have a big topic today
7226
04:33:51,199 --> 04:33:54,561
that is promises promises are an object
7227
04:33:54,561 --> 04:33:56,799
that encapsulate the result of an
7228
04:33:56,799 --> 04:33:58,400
asynchronous operation
7229
04:33:58,400 --> 04:34:01,039
a benefit is that they let asynchronous
7230
04:34:01,039 --> 04:34:03,919
methods return values like they were
7231
04:34:03,919 --> 04:34:06,320
synchronous it's a promise to return
7232
04:34:06,320 --> 04:34:08,320
something in the future for this topic
7233
04:34:08,320 --> 04:34:10,240
let's pretend that we're fetching a file
7234
04:34:10,240 --> 04:34:12,561
i haven't talked about the fetch api yet
7235
04:34:12,561 --> 04:34:14,160
but we'll do so in the future for the
7236
04:34:14,160 --> 04:34:15,599
time being we'll just pretend that we're
7237
04:34:15,599 --> 04:34:16,958
loading a file
7238
04:34:16,958 --> 04:34:20,080
let file loaded this will be a boolean
7239
04:34:20,080 --> 04:34:22,799
value true or false i'll set this to
7240
04:34:22,799 --> 04:34:25,118
true loading a file is an asynchronous
7241
04:34:25,118 --> 04:34:27,680
process we could encapsulate this
7242
04:34:27,680 --> 04:34:31,039
asynchronous process with a promise when
7243
04:34:31,039 --> 04:34:33,520
this process is finished the promise can
7244
04:34:33,520 --> 04:34:36,240
return a value or catch any exceptions
7245
04:34:36,240 --> 04:34:38,000
if there's any problems like we can't
7246
04:34:38,000 --> 04:34:39,759
find the file so this is how to create a
7247
04:34:39,759 --> 04:34:42,000
promise
7248
04:34:42,000 --> 04:34:44,320
let's declare one const
7249
04:34:44,320 --> 04:34:46,000
promise
7250
04:34:46,000 --> 04:34:48,400
equals new
7251
04:34:48,400 --> 04:34:50,879
promise
7252
04:34:51,039 --> 04:34:52,719
within the promise we can list a
7253
04:34:52,719 --> 04:34:55,359
callback a function expression or an
7254
04:34:55,359 --> 04:34:57,438
arrow function expression i'll use an
7255
04:34:57,438 --> 04:35:00,560
arrow function expression
7256
04:35:01,359 --> 04:35:03,599
there's two arguments
7257
04:35:03,599 --> 04:35:04,958
resolve
7258
04:35:04,958 --> 04:35:07,118
and reject
7259
04:35:07,118 --> 04:35:09,039
if our asynchronous process is
7260
04:35:09,039 --> 04:35:11,759
successful we will invoke this resolve
7261
04:35:11,759 --> 04:35:15,438
callback if not we'll invoke reject
7262
04:35:15,438 --> 04:35:17,039
so what would we like to do within this
7263
04:35:17,039 --> 04:35:19,520
promise we can place any asynchronous
7264
04:35:19,520 --> 04:35:22,480
code within this promise
7265
04:35:22,480 --> 04:35:24,160
then let's check to see if our file is
7266
04:35:24,160 --> 04:35:26,400
loaded if
7267
04:35:26,400 --> 04:35:28,639
file loaded
7268
04:35:28,639 --> 04:35:32,639
we will invoke this callback of resolve
7269
04:35:32,639 --> 04:35:34,160
if you have any arguments for this
7270
04:35:34,160 --> 04:35:36,400
callback you can place those here i'll
7271
04:35:36,400 --> 04:35:38,000
add some text
7272
04:35:38,000 --> 04:35:40,639
file loaded
7273
04:35:40,639 --> 04:35:41,840
else
7274
04:35:41,840 --> 04:35:44,561
we will invoke reject
7275
04:35:44,561 --> 04:35:47,919
place any arguments within this callback
7276
04:35:47,919 --> 04:35:51,199
file not loaded
7277
04:35:51,199 --> 04:35:53,359
that's step one of our promise
7278
04:35:53,359 --> 04:35:55,118
promises have a state
7279
04:35:55,118 --> 04:35:57,840
they're pending then they're fulfilled
7280
04:35:57,840 --> 04:36:01,039
or rejected and the result is what can
7281
04:36:01,039 --> 04:36:02,639
be returned
7282
04:36:02,639 --> 04:36:04,719
there's two parts to a promise the
7283
04:36:04,719 --> 04:36:07,599
producing code which is this portion and
7284
04:36:07,599 --> 04:36:10,160
the consuming code if this promise is
7285
04:36:10,160 --> 04:36:12,400
resolved what do we want to do we'll
7286
04:36:12,400 --> 04:36:15,278
handle that with the consuming code
7287
04:36:15,278 --> 04:36:17,359
we will take our promise
7288
04:36:17,359 --> 04:36:20,080
follow the promise with invoking the
7289
04:36:20,080 --> 04:36:21,278
then method
7290
04:36:21,278 --> 04:36:23,520
if our promise is resolved then we can
7291
04:36:23,520 --> 04:36:25,118
perform some function
7292
04:36:25,118 --> 04:36:27,840
this can be a callback to a function
7293
04:36:27,840 --> 04:36:30,719
a function expression
7294
04:36:30,719 --> 04:36:33,438
or an arrow function expression
7295
04:36:33,438 --> 04:36:34,719
i'll stick with an arrow function
7296
04:36:34,719 --> 04:36:35,840
expression
7297
04:36:35,840 --> 04:36:37,359
if we have any arguments we can list
7298
04:36:37,359 --> 04:36:39,759
them here we're returning one value just
7299
04:36:39,759 --> 04:36:41,680
some text
7300
04:36:41,680 --> 04:36:42,799
value
7301
04:36:42,799 --> 04:36:43,919
arrow
7302
04:36:43,919 --> 04:36:46,919
then let's do something
7303
04:36:46,919 --> 04:36:48,879
console.log
7304
04:36:48,879 --> 04:36:51,278
whatever my value is
7305
04:36:51,278 --> 04:36:53,520
okay let's try this
7306
04:36:53,520 --> 04:36:55,438
after creating this promise my file
7307
04:36:55,438 --> 04:36:57,759
loads that's set to true
7308
04:36:57,759 --> 04:36:59,359
file loaded
7309
04:36:59,359 --> 04:37:00,879
what would happen if we couldn't locate
7310
04:37:00,879 --> 04:37:05,118
a file this value is false
7311
04:37:05,118 --> 04:37:07,199
well we have an uncaught exception
7312
04:37:07,199 --> 04:37:10,240
because we invoked this reject callback
7313
04:37:10,240 --> 04:37:12,400
to catch any exceptions we can follow
7314
04:37:12,400 --> 04:37:13,759
then with
7315
04:37:13,759 --> 04:37:15,118
catch
7316
04:37:15,118 --> 04:37:17,599
and i'll place this on a new line
7317
04:37:17,599 --> 04:37:19,599
just so it's easier to read
7318
04:37:19,599 --> 04:37:22,240
we have one argument so let's set up one
7319
04:37:22,240 --> 04:37:24,639
parameter this is the argument we will
7320
04:37:24,639 --> 04:37:27,359
name this parameter error
7321
04:37:27,359 --> 04:37:30,080
arrow what would we like to do
7322
04:37:30,080 --> 04:37:31,919
console.log
7323
04:37:31,919 --> 04:37:34,160
whatever my error is
7324
04:37:34,160 --> 04:37:36,240
when i run this again
7325
04:37:36,240 --> 04:37:38,561
we have caught this exception
7326
04:37:38,561 --> 04:37:40,719
file not loaded so that's kind of the
7327
04:37:40,719 --> 04:37:42,480
basics of a promise
7328
04:37:42,480 --> 04:37:44,400
it's a promise to return something in
7329
04:37:44,400 --> 04:37:46,719
the future the state is pending then
7330
04:37:46,719 --> 04:37:49,438
it's either fulfilled or rejected now
7331
04:37:49,438 --> 04:37:51,359
you don't necessarily need to reject a
7332
04:37:51,359 --> 04:37:52,719
promise
7333
04:37:52,719 --> 04:37:56,080
this would still work too
7334
04:37:56,080 --> 04:37:57,599
it doesn't do anything
7335
04:37:57,599 --> 04:37:59,840
here's another example
7336
04:37:59,840 --> 04:38:01,599
let's create a separate promise we'll
7337
04:38:01,599 --> 04:38:03,599
wait for five seconds then display a
7338
04:38:03,599 --> 04:38:05,599
message
7339
04:38:05,599 --> 04:38:07,199
const
7340
04:38:07,199 --> 04:38:09,039
promise
7341
04:38:09,039 --> 04:38:11,520
equals
7342
04:38:11,520 --> 04:38:12,958
new
7343
04:38:12,958 --> 04:38:14,561
promise
7344
04:38:14,561 --> 04:38:16,639
i'll write an arrow function expression
7345
04:38:16,639 --> 04:38:19,520
this time we will only resolve
7346
04:38:19,520 --> 04:38:21,520
arrow
7347
04:38:21,520 --> 04:38:24,400
and then maybe some curly braces okay
7348
04:38:24,400 --> 04:38:27,199
what are we gonna do within this promise
7349
04:38:27,199 --> 04:38:31,278
let's set timeout for five seconds
7350
04:38:31,278 --> 04:38:34,159
set timeout
7351
04:38:34,240 --> 04:38:35,359
resolve
7352
04:38:35,359 --> 04:38:36,840
after five
7353
04:38:36,840 --> 04:38:40,320
seconds that's 5000 milliseconds
7354
04:38:40,320 --> 04:38:41,919
then i will take
7355
04:38:41,919 --> 04:38:43,840
the name of my promise
7356
04:38:43,840 --> 04:38:45,438
follow it with then
7357
04:38:45,438 --> 04:38:47,199
then pass in a function
7358
04:38:47,199 --> 04:38:49,278
so you don't necessarily need to return
7359
04:38:49,278 --> 04:38:51,359
a value when resolving
7360
04:38:51,359 --> 04:38:54,639
so this function will have no arguments
7361
04:38:54,639 --> 04:38:56,639
arrow
7362
04:38:56,639 --> 04:39:00,561
will display message console.log
7363
04:39:00,561 --> 04:39:03,039
thanks for waiting
7364
04:39:03,039 --> 04:39:04,879
after five seconds this should display
7365
04:39:04,879 --> 04:39:07,840
my message
7366
04:39:11,438 --> 04:39:13,278
thanks for waiting
7367
04:39:13,278 --> 04:39:14,561
what if you would like to pass an
7368
04:39:14,561 --> 04:39:16,799
argument to a promise let's rename this
7369
04:39:16,799 --> 04:39:18,561
promise object as something else
7370
04:39:18,561 --> 04:39:20,719
something that's more descriptive
7371
04:39:20,719 --> 04:39:23,359
like wait
7372
04:39:23,438 --> 04:39:25,680
wait is now a promise
7373
04:39:25,680 --> 04:39:27,438
when we create this promise we can pass
7374
04:39:27,438 --> 04:39:29,359
an argument how about an amount of
7375
04:39:29,359 --> 04:39:31,759
milliseconds let's wait for three
7376
04:39:31,759 --> 04:39:33,278
seconds that's three thousand
7377
04:39:33,278 --> 04:39:34,719
milliseconds
7378
04:39:34,719 --> 04:39:36,639
i'll precede this promise with an arrow
7379
04:39:36,639 --> 04:39:38,480
function
7380
04:39:38,480 --> 04:39:41,680
and list any arguments before the arrow
7381
04:39:41,680 --> 04:39:43,440
maybe time
7382
04:39:43,440 --> 04:39:45,520
time will be this value whatever we're
7383
04:39:45,520 --> 04:39:46,638
passing in
7384
04:39:46,638 --> 04:39:49,360
we'll wait for some amount of time
7385
04:39:49,360 --> 04:39:51,520
3000 milliseconds
7386
04:39:51,520 --> 04:39:54,400
let's run this again
7387
04:39:54,798 --> 04:39:58,240
one two three thanks for waiting so yeah
7388
04:39:58,240 --> 04:40:00,320
those are promises
7389
04:40:00,320 --> 04:40:02,480
they're objects that encapsulate the
7390
04:40:02,480 --> 04:40:05,120
result of an asynchronous operation
7391
04:40:05,120 --> 04:40:07,360
they let asynchronous methods return
7392
04:40:07,360 --> 04:40:10,080
values as if they were synchronous it's
7393
04:40:10,080 --> 04:40:11,760
a promise to return something in the
7394
04:40:11,760 --> 04:40:14,240
future so those are promises if you
7395
04:40:14,240 --> 04:40:15,680
would like a copy of the code we worked
7396
04:40:15,680 --> 04:40:17,440
on today i'll post that in the comments
7397
04:40:17,440 --> 04:40:19,680
section down below and well yeah that's
7398
04:40:19,680 --> 04:40:21,600
an introduction to promises in
7399
04:40:21,600 --> 04:40:24,320
javascript
7400
04:40:24,638 --> 04:40:26,320
hey guys in this topic i'm going to
7401
04:40:26,320 --> 04:40:29,040
explain the async keyword it makes a
7402
04:40:29,040 --> 04:40:31,200
function return a promise in this
7403
04:40:31,200 --> 04:40:33,200
example i have a promise there's a
7404
04:40:33,200 --> 04:40:35,760
callback with two arguments a callback
7405
04:40:35,760 --> 04:40:38,240
for resolve and a callback for reject
7406
04:40:38,240 --> 04:40:40,080
all we're doing is we're checking to see
7407
04:40:40,080 --> 04:40:42,080
if a file loaded not really but we're
7408
04:40:42,080 --> 04:40:43,520
pretending we are because i haven't
7409
04:40:43,520 --> 04:40:45,280
taught about loading files yet so we
7410
04:40:45,280 --> 04:40:47,120
have a boolean variable file loaded it's
7411
04:40:47,120 --> 04:40:49,360
set to true if our file didn't load we
7412
04:40:49,360 --> 04:40:51,920
can set this to false if file loaded we
7413
04:40:51,920 --> 04:40:54,080
will invoke this callback of resolve if
7414
04:40:54,080 --> 04:40:56,000
not we'll invoke this other callback of
7415
04:40:56,000 --> 04:40:58,480
reject so currently my file loaded if i
7416
04:40:58,480 --> 04:41:00,718
switch this to false my file is not
7417
04:41:00,718 --> 04:41:02,480
loaded an easier way of writing this is
7418
04:41:02,480 --> 04:41:04,400
that we can stick this code within an
7419
04:41:04,400 --> 04:41:06,240
async function and it will return a
7420
04:41:06,240 --> 04:41:08,240
promise so let's create an async
7421
04:41:08,240 --> 04:41:11,040
function use the keyword async
7422
04:41:11,040 --> 04:41:13,200
function and what's a descriptive name
7423
04:41:13,200 --> 04:41:15,440
of this function how about load
7424
04:41:15,440 --> 04:41:16,718
file
7425
04:41:16,718 --> 04:41:19,040
that's an asynchronous process i'll copy
7426
04:41:19,040 --> 04:41:20,240
this code
7427
04:41:20,240 --> 04:41:21,280
cut it
7428
04:41:21,280 --> 04:41:23,360
i'll delete this promise
7429
04:41:23,360 --> 04:41:25,520
and paste my code within this async
7430
04:41:25,520 --> 04:41:27,520
function there's no need to use this
7431
04:41:27,520 --> 04:41:29,760
resolve or reject callback if my
7432
04:41:29,760 --> 04:41:32,320
asynchronous process is successful i
7433
04:41:32,320 --> 04:41:35,040
will simply return a value
7434
04:41:35,040 --> 04:41:37,680
return file loaded this will be an
7435
04:41:37,680 --> 04:41:40,240
argument if my asynchronous process is
7436
04:41:40,240 --> 04:41:42,958
not successful i could instead use the
7437
04:41:42,958 --> 04:41:45,520
throw keyword
7438
04:41:45,520 --> 04:41:47,120
this will raise an error and we can
7439
04:41:47,120 --> 04:41:49,600
catch it so this is my error message
7440
04:41:49,600 --> 04:41:51,600
right here now to invoke this function
7441
04:41:51,600 --> 04:41:54,000
in place of a promise i will invoke the
7442
04:41:54,000 --> 04:41:56,080
function that contains my asynchronous
7443
04:41:56,080 --> 04:41:57,040
process
7444
04:41:57,040 --> 04:41:59,840
load file so let's check to see if this
7445
04:41:59,840 --> 04:42:02,320
works file loaded is set to true file
7446
04:42:02,320 --> 04:42:03,360
loaded
7447
04:42:03,360 --> 04:42:05,840
i'll set this to false file not loaded
7448
04:42:05,840 --> 04:42:08,320
when using the then and catch methods in
7449
04:42:08,320 --> 04:42:10,480
place of adding these methods after a
7450
04:42:10,480 --> 04:42:12,878
promise object we would invoke a
7451
04:42:12,878 --> 04:42:15,360
function and a promise is going to be
7452
04:42:15,360 --> 04:42:18,000
returned back to this place in which you
7453
04:42:18,000 --> 04:42:19,680
invoke the function another way of
7454
04:42:19,680 --> 04:42:21,440
writing this that's a little more clear
7455
04:42:21,440 --> 04:42:23,840
but it's a lot more syntax is if we were
7456
04:42:23,840 --> 04:42:25,920
not using the async function we would
7457
04:42:25,920 --> 04:42:27,840
return a promise i'll eliminate this
7458
04:42:27,840 --> 04:42:29,920
async keyword just for a demonstration
7459
04:42:29,920 --> 04:42:32,638
so we're returning a promise object
7460
04:42:32,638 --> 04:42:35,520
promise dot resolve
7461
04:42:35,520 --> 04:42:38,000
and i will pass this text as an argument
7462
04:42:38,000 --> 04:42:39,520
else
7463
04:42:39,520 --> 04:42:42,000
return
7464
04:42:42,000 --> 04:42:45,440
promise dot reject
7465
04:42:45,440 --> 04:42:46,798
this would do the same thing but it's
7466
04:42:46,798 --> 04:42:50,400
more syntax file loaded
7467
04:42:50,400 --> 04:42:52,320
file not loaded when we invoke this
7468
04:42:52,320 --> 04:42:54,638
function we're returning a promise back
7469
04:42:54,638 --> 04:42:56,320
to the spot in which we invoked this
7470
04:42:56,320 --> 04:42:58,240
function but i like using the sync
7471
04:42:58,240 --> 04:43:00,480
keyword it's a lot less text so yeah
7472
04:43:00,480 --> 04:43:02,480
that's the async keyword everybody it
7473
04:43:02,480 --> 04:43:05,040
makes a function return a promise and it
7474
04:43:05,040 --> 04:43:07,600
pairs very well with the await keyword
7475
04:43:07,600 --> 04:43:09,760
which we'll discuss in the next topic in
7476
04:43:09,760 --> 04:43:13,920
about three two one go
7477
04:43:13,920 --> 04:43:15,920
hey guys in this topic i'm going to
7478
04:43:15,920 --> 04:43:18,480
explain the await keyword it makes an
7479
04:43:18,480 --> 04:43:21,440
async function wait for a promise in
7480
04:43:21,440 --> 04:43:24,160
this example i have an async function
7481
04:43:24,160 --> 04:43:26,240
that is named load file
7482
04:43:26,240 --> 04:43:28,160
we're pretending to load a file that
7483
04:43:28,160 --> 04:43:30,080
would be an asynchronous process
7484
04:43:30,080 --> 04:43:32,718
if our file loads we will return a
7485
04:43:32,718 --> 04:43:35,440
promise with this text as an argument
7486
04:43:35,440 --> 04:43:36,718
file loaded
7487
04:43:36,718 --> 04:43:38,400
if for some reason we can't load our
7488
04:43:38,400 --> 04:43:41,440
file we will throw an error and catch it
7489
04:43:41,440 --> 04:43:43,680
and display this error message another
7490
04:43:43,680 --> 04:43:45,360
way of writing this without using these
7491
04:43:45,360 --> 04:43:47,600
two lines of code invoking an async
7492
04:43:47,600 --> 04:43:50,480
function followed with then and catch is
7493
04:43:50,480 --> 04:43:52,798
that we could use the await keyword so
7494
04:43:52,798 --> 04:43:55,840
we will type await then invoke an async
7495
04:43:55,840 --> 04:43:56,798
function
7496
04:43:56,798 --> 04:43:58,718
await load file
7497
04:43:58,718 --> 04:44:01,360
we're waiting for a promise awaiting an
7498
04:44:01,360 --> 04:44:03,680
async function in this example will
7499
04:44:03,680 --> 04:44:06,958
return some text file loaded or file not
7500
04:44:06,958 --> 04:44:08,958
loaded so i'm going to store that text
7501
04:44:08,958 --> 04:44:11,200
within a temporary variable let's say
7502
04:44:11,200 --> 04:44:12,240
let
7503
04:44:12,240 --> 04:44:13,520
message
7504
04:44:13,520 --> 04:44:16,798
equal await load file then i'll simply
7505
04:44:16,798 --> 04:44:20,400
display that message console.log
7506
04:44:20,400 --> 04:44:22,798
message and i can eliminate these two
7507
04:44:22,798 --> 04:44:24,240
lines
7508
04:44:24,240 --> 04:44:25,840
so after running this
7509
04:44:25,840 --> 04:44:28,400
we have an uncaught syntax error a
7510
04:44:28,400 --> 04:44:31,760
weight is only valid in async functions
7511
04:44:31,760 --> 04:44:34,400
so in order to use this await keyword we
7512
04:44:34,400 --> 04:44:36,638
need to place it within another async
7513
04:44:36,638 --> 04:44:38,878
function so maybe we're beginning the
7514
04:44:38,878 --> 04:44:41,440
process of loading a file usually that
7515
04:44:41,440 --> 04:44:43,200
takes more than one asynchronous process
7516
04:44:43,200 --> 04:44:45,120
you have to first locate the file open
7517
04:44:45,120 --> 04:44:47,440
it then close it so i'm going to declare
7518
04:44:47,440 --> 04:44:49,120
an async function
7519
04:44:49,120 --> 04:44:51,440
async function
7520
04:44:51,440 --> 04:44:52,718
start
7521
04:44:52,718 --> 04:44:54,718
process
7522
04:44:54,718 --> 04:44:56,718
and i will stick these two lines within
7523
04:44:56,718 --> 04:44:59,040
that function you can only use the await
7524
04:44:59,040 --> 04:45:01,760
keyword within an async function
7525
04:45:01,760 --> 04:45:03,520
and then we need to invoke this function
7526
04:45:03,520 --> 04:45:05,360
to begin the process
7527
04:45:05,360 --> 04:45:07,200
start process
7528
04:45:07,200 --> 04:45:09,680
file loaded there was no need to invoke
7529
04:45:09,680 --> 04:45:12,160
this async function followed with
7530
04:45:12,160 --> 04:45:15,600
then and catch we can eliminate that
7531
04:45:15,600 --> 04:45:17,760
all you need is a weight but it does
7532
04:45:17,760 --> 04:45:19,840
have to be within an async function
7533
04:45:19,840 --> 04:45:21,680
now check this out what if our file
7534
04:45:21,680 --> 04:45:25,760
doesn't load this will throw an error
7535
04:45:25,760 --> 04:45:28,958
uncaught in promise file not loaded we
7536
04:45:28,958 --> 04:45:30,718
would need some way to catch this error
7537
04:45:30,718 --> 04:45:32,320
when it's thrown
7538
04:45:32,320 --> 04:45:33,840
one way in which we can do that is to
7539
04:45:33,840 --> 04:45:36,080
surround any dangerous code with a try
7540
04:45:36,080 --> 04:45:36,958
block
7541
04:45:36,958 --> 04:45:38,958
see the topic on the series in error
7542
04:45:38,958 --> 04:45:41,040
handling where i'll discuss this more in
7543
04:45:41,040 --> 04:45:41,920
depth
7544
04:45:41,920 --> 04:45:45,120
so try this code if there are any errors
7545
04:45:45,120 --> 04:45:48,080
we will catch them so catch there is one
7546
04:45:48,080 --> 04:45:49,920
argument of error
7547
04:45:49,920 --> 04:45:52,240
and i will simply display our error
7548
04:45:52,240 --> 04:45:53,760
catch
7549
04:45:53,760 --> 04:45:55,280
console.log
7550
04:45:55,280 --> 04:45:56,160
error
7551
04:45:56,160 --> 04:45:57,760
so let's run this again
7552
04:45:57,760 --> 04:46:00,878
file not loaded so this is very helpful
7553
04:46:00,878 --> 04:46:02,400
if we have to wait for more than one
7554
04:46:02,400 --> 04:46:04,320
asynchronous function we'll discuss this
7555
04:46:04,320 --> 04:46:06,160
in the next topic maybe not only do we
7556
04:46:06,160 --> 04:46:08,000
have to load a file but we first need to
7557
04:46:08,000 --> 04:46:10,240
locate it and we have to close it in the
7558
04:46:10,240 --> 04:46:11,680
next topic we'll have to wait for
7559
04:46:11,680 --> 04:46:13,920
multiple asynchronous functions we might
7560
04:46:13,920 --> 04:46:16,160
have locate file
7561
04:46:16,160 --> 04:46:19,200
and close file
7562
04:46:19,280 --> 04:46:21,280
so when we begin a process we're waiting
7563
04:46:21,280 --> 04:46:23,840
for multiple asynchronous functions but
7564
04:46:23,840 --> 04:46:25,840
we'll discuss that in the next topic so
7565
04:46:25,840 --> 04:46:27,920
yeah that's the await keyword everybody
7566
04:46:27,920 --> 04:46:30,000
it makes an asynchronous function wait
7567
04:46:30,000 --> 04:46:32,000
for a promise when using the await
7568
04:46:32,000 --> 04:46:33,760
keyword you don't need to invoke a
7569
04:46:33,760 --> 04:46:36,160
function then follow it with the then
7570
04:46:36,160 --> 04:46:38,240
method and catch
7571
04:46:38,240 --> 04:46:40,000
there's no need for that so yeah
7572
04:46:40,000 --> 04:46:41,920
everybody that's the await keyword we'll
7573
04:46:41,920 --> 04:46:45,520
practice this more in the next topic
7574
04:46:45,520 --> 04:46:47,200
hey guys in this video i'm going to
7575
04:46:47,200 --> 04:46:49,920
explain es6 modules the idea behind a
7576
04:46:49,920 --> 04:46:52,638
module is that it's a file of reusable
7577
04:46:52,638 --> 04:46:55,200
code we can import sections of
7578
04:46:55,200 --> 04:46:57,680
pre-written code to use whenever we want
7579
04:46:57,680 --> 04:46:59,520
they're great for any general utility
7580
04:46:59,520 --> 04:47:01,600
values and functions they help to make
7581
04:47:01,600 --> 04:47:04,240
your code more reusable and maintainable
7582
04:47:04,240 --> 04:47:06,400
think of modules as separate chapters of
7583
04:47:06,400 --> 04:47:08,638
a book each javascript file could be its
7584
04:47:08,638 --> 04:47:10,560
own individual chapter to begin working
7585
04:47:10,560 --> 04:47:12,958
with modules within the opening script
7586
04:47:12,958 --> 04:47:15,360
tag of your html document add this
7587
04:47:15,360 --> 04:47:16,560
attribute
7588
04:47:16,560 --> 04:47:19,280
type equals module
7589
04:47:19,280 --> 04:47:22,400
then we'll create a new javascript file
7590
04:47:22,400 --> 04:47:24,240
i'll name this
7591
04:47:24,240 --> 04:47:26,638
math underscore util
7592
04:47:26,638 --> 04:47:28,080
dot js
7593
04:47:28,080 --> 04:47:30,240
this file will contain some general map
7594
04:47:30,240 --> 04:47:32,480
utility functions such as get the
7595
04:47:32,480 --> 04:47:35,040
circumference of a circle or the area so
7596
04:47:35,040 --> 04:47:36,878
let's begin i'll create a constant as
7597
04:47:36,878 --> 04:47:39,040
well const pi
7598
04:47:39,040 --> 04:47:42,958
i'll set the sequel to 3.14159
7599
04:47:42,958 --> 04:47:45,360
and maybe some functions
7600
04:47:45,360 --> 04:47:47,520
function
7601
04:47:47,520 --> 04:47:48,638
get
7602
04:47:48,638 --> 04:47:50,958
circumference
7603
04:47:50,958 --> 04:47:54,400
will accept a radius
7604
04:47:54,400 --> 04:47:56,320
and we'll return
7605
04:47:56,320 --> 04:47:57,440
two
7606
04:47:57,440 --> 04:47:58,958
times pi
7607
04:47:58,958 --> 04:48:01,360
times radius
7608
04:48:01,360 --> 04:48:05,520
then let's create a get area function
7609
04:48:05,680 --> 04:48:06,638
get
7610
04:48:06,638 --> 04:48:07,920
area
7611
04:48:07,920 --> 04:48:09,760
will return
7612
04:48:09,760 --> 04:48:10,718
pi
7613
04:48:10,718 --> 04:48:12,958
times radius
7614
04:48:12,958 --> 04:48:15,200
times radius
7615
04:48:15,200 --> 04:48:17,840
to export any variables or functions i
7616
04:48:17,840 --> 04:48:20,000
will precede them with the export
7617
04:48:20,000 --> 04:48:21,920
keyword
7618
04:48:21,920 --> 04:48:24,000
i like to do this inline
7619
04:48:24,000 --> 04:48:25,280
then save
7620
04:48:25,280 --> 04:48:27,200
heading back to our main javascript file
7621
04:48:27,200 --> 04:48:29,280
we can import everything from this
7622
04:48:29,280 --> 04:48:32,240
javascript file to do that we will type
7623
04:48:32,240 --> 04:48:33,200
import
7624
04:48:33,200 --> 04:48:35,920
then within curly braces then list all
7625
04:48:35,920 --> 04:48:37,680
of the variables or functions we would
7626
04:48:37,680 --> 04:48:39,280
like to import
7627
04:48:39,280 --> 04:48:42,560
i would like to import pi
7628
04:48:42,560 --> 04:48:45,520
get circumference
7629
04:48:45,760 --> 04:48:49,040
and get area
7630
04:48:49,840 --> 04:48:51,760
add from
7631
04:48:51,760 --> 04:48:54,320
then an absolute or relative file path
7632
04:48:54,320 --> 04:48:56,958
i'll use a relative file path because
7633
04:48:56,958 --> 04:48:59,440
these files are right next to each other
7634
04:48:59,440 --> 04:49:02,320
so that would be dot forward slash the
7635
04:49:02,320 --> 04:49:05,680
name of the file math underscore util
7636
04:49:05,680 --> 04:49:07,440
dot js
7637
04:49:07,440 --> 04:49:09,760
and that's it so we can use these
7638
04:49:09,760 --> 04:49:11,760
variables and functions as if they were
7639
04:49:11,760 --> 04:49:13,200
in the same file
7640
04:49:13,200 --> 04:49:16,878
to test this i will use console.log
7641
04:49:16,878 --> 04:49:17,920
pi
7642
04:49:17,920 --> 04:49:21,280
and that value is 3.14159
7643
04:49:21,280 --> 04:49:23,040
we have access to this variable even
7644
04:49:23,040 --> 04:49:25,040
though it's not written within this file
7645
04:49:25,040 --> 04:49:27,040
we've imported that variable from the
7646
04:49:27,040 --> 04:49:29,280
separate javascript file let's calculate
7647
04:49:29,280 --> 04:49:31,440
the circumference of a circle
7648
04:49:31,440 --> 04:49:32,360
let
7649
04:49:32,360 --> 04:49:34,000
circumference
7650
04:49:34,000 --> 04:49:35,120
equal
7651
04:49:35,120 --> 04:49:38,400
get circumference
7652
04:49:38,400 --> 04:49:40,958
pass in a radius maybe 10.
7653
04:49:40,958 --> 04:49:43,360
then let's display our circumference
7654
04:49:43,360 --> 04:49:44,878
console.log
7655
04:49:44,878 --> 04:49:46,400
circumference
7656
04:49:46,400 --> 04:49:48,480
if we pass in 10 as a radius the
7657
04:49:48,480 --> 04:49:52,080
circumference is 62.83 we also have get
7658
04:49:52,080 --> 04:49:54,240
area 2.
7659
04:49:54,240 --> 04:49:57,200
let area equals
7660
04:49:57,200 --> 04:50:00,080
get area
7661
04:50:00,080 --> 04:50:02,840
the area would be
7662
04:50:02,840 --> 04:50:05,520
314.159 even though all of this code is
7663
04:50:05,520 --> 04:50:07,760
written in a separate file we can access
7664
04:50:07,760 --> 04:50:10,240
it from another file by importing it
7665
04:50:10,240 --> 04:50:11,600
make sure that anything you want to
7666
04:50:11,600 --> 04:50:13,920
export is preceded with this export
7667
04:50:13,920 --> 04:50:15,920
keyword there is another way to import
7668
04:50:15,920 --> 04:50:17,760
this too
7669
04:50:17,760 --> 04:50:21,120
i'm going to copy all of this
7670
04:50:22,000 --> 04:50:23,840
and paste it
7671
04:50:23,840 --> 04:50:26,000
if there's a lot to import we can simply
7672
04:50:26,000 --> 04:50:28,638
import everything by using an asterisk
7673
04:50:28,638 --> 04:50:30,240
but we'll give all of these imports an
7674
04:50:30,240 --> 04:50:32,718
alias that's like a nickname import
7675
04:50:32,718 --> 04:50:34,400
asterisk as
7676
04:50:34,400 --> 04:50:36,240
your nickname should be descriptive of
7677
04:50:36,240 --> 04:50:38,480
the file that you're importing this is
7678
04:50:38,480 --> 04:50:39,920
as if we're creating a separate
7679
04:50:39,920 --> 04:50:41,040
namespace
7680
04:50:41,040 --> 04:50:42,000
maybe
7681
04:50:42,000 --> 04:50:42,958
math
7682
04:50:42,958 --> 04:50:45,280
util now in order to use these variables
7683
04:50:45,280 --> 04:50:47,440
or functions i would need to precede
7684
04:50:47,440 --> 04:50:51,120
them with that namespace mathutil
7685
04:50:51,120 --> 04:50:52,878
dot pi
7686
04:50:52,878 --> 04:50:55,840
mathutil dot get circumference
7687
04:50:55,840 --> 04:50:58,798
mathutil dot get area then this would
7688
04:50:58,798 --> 04:51:01,200
work when i'm using math util dot pi
7689
04:51:01,200 --> 04:51:03,840
that's kind of similar to math.pi it's
7690
04:51:03,840 --> 04:51:05,680
kind of the same concept except we
7691
04:51:05,680 --> 04:51:06,878
ourselves created the separate
7692
04:51:06,878 --> 04:51:08,878
javascript file so yeah those are
7693
04:51:08,878 --> 04:51:10,878
modules they're files of pre-written
7694
04:51:10,878 --> 04:51:12,638
code that we can import and use whenever
7695
04:51:12,638 --> 04:51:14,480
we need to if you would like a copy of
7696
04:51:14,480 --> 04:51:16,240
this code i'll post this in the comment
7697
04:51:16,240 --> 04:51:18,160
section down below and well yeah that's
7698
04:51:18,160 --> 04:51:22,560
an introduction to modules in javascript
7699
04:51:22,560 --> 04:51:24,320
hey guys in this video i'm going to
7700
04:51:24,320 --> 04:51:27,280
quickly explain the dom it's an acronym
7701
04:51:27,280 --> 04:51:29,840
for document object model technically
7702
04:51:29,840 --> 04:51:32,718
it's an api an application programming
7703
04:51:32,718 --> 04:51:35,120
interface it's an interface that's used
7704
04:51:35,120 --> 04:51:37,360
for changing the content of a page and
7705
04:51:37,360 --> 04:51:40,080
it's arranged in a hierarchical tree our
7706
04:51:40,080 --> 04:51:42,878
dom is a representation of an html
7707
04:51:42,878 --> 04:51:45,680
document and this tree contains nodes
7708
04:51:45,680 --> 04:51:48,000
the first child of our document is the
7709
04:51:48,000 --> 04:51:51,360
html root element so our html tags
7710
04:51:51,360 --> 04:51:53,520
enclose all of this content including
7711
04:51:53,520 --> 04:51:55,600
the head and the body elements
7712
04:51:55,600 --> 04:51:57,200
which are here
7713
04:51:57,200 --> 04:51:59,600
and here the head can contain a title
7714
04:51:59,600 --> 04:52:01,920
and some text which you see right here
7715
04:52:01,920 --> 04:52:03,840
and the body can contain some various
7716
04:52:03,840 --> 04:52:06,638
html elements like a div tag by
7717
04:52:06,638 --> 04:52:08,718
interacting with this dom we can change
7718
04:52:08,718 --> 04:52:11,280
the elements of a web page document is
7719
04:52:11,280 --> 04:52:14,120
the entry point of our dom if i typed
7720
04:52:14,120 --> 04:52:16,000
console.log
7721
04:52:16,000 --> 04:52:18,878
document this would display my dom and
7722
04:52:18,878 --> 04:52:21,520
everything within it so we have document
7723
04:52:21,520 --> 04:52:23,360
our html node
7724
04:52:23,360 --> 04:52:24,560
the head
7725
04:52:24,560 --> 04:52:26,638
and the body and a bunch of stuff within
7726
04:52:26,638 --> 04:52:28,920
those nodes too now if you were to type
7727
04:52:28,920 --> 04:52:30,480
console.dir
7728
04:52:30,480 --> 04:52:32,798
then pass document as an argument this
7729
04:52:32,798 --> 04:52:34,878
will list all of the properties of your
7730
04:52:34,878 --> 04:52:37,120
dom if i need access to the title of my
7731
04:52:37,120 --> 04:52:39,600
document i can access that by typing
7732
04:52:39,600 --> 04:52:42,480
document dot title property and then
7733
04:52:42,480 --> 04:52:45,798
i'll display this console.log
7734
04:52:45,798 --> 04:52:48,080
document.title and the title of my
7735
04:52:48,080 --> 04:52:50,798
document is well document or i could get
7736
04:52:50,798 --> 04:52:56,280
the url console.log document.url
7737
04:52:56,718 --> 04:52:59,040
so this is my url and you can change
7738
04:52:59,040 --> 04:53:01,120
these properties too i can change the
7739
04:53:01,120 --> 04:53:05,280
title of my webpage document.title
7740
04:53:05,280 --> 04:53:07,040
set the sql to something
7741
04:53:07,040 --> 04:53:09,840
title goes here and taking a look at
7742
04:53:09,840 --> 04:53:12,080
this tab of my web browser you can see
7743
04:53:12,080 --> 04:53:14,560
that it says title goes here i can even
7744
04:53:14,560 --> 04:53:17,320
change the location
7745
04:53:17,320 --> 04:53:18,958
document.location
7746
04:53:18,958 --> 04:53:21,680
i'll set the sequel to google
7747
04:53:21,680 --> 04:53:24,958
so http colon two forward slashes
7748
04:53:24,958 --> 04:53:27,920
www.google.com
7749
04:53:27,920 --> 04:53:29,920
then when i save this it takes me to
7750
04:53:29,920 --> 04:53:30,958
google
7751
04:53:30,958 --> 04:53:33,280
by accessing the dom i have access to my
7752
04:53:33,280 --> 04:53:35,120
body element and i can change something
7753
04:53:35,120 --> 04:53:36,958
about the body of my document if i would
7754
04:53:36,958 --> 04:53:38,878
like to change the background color i
7755
04:53:38,878 --> 04:53:41,040
would type document
7756
04:53:41,040 --> 04:53:43,520
dot the name of the node body in this
7757
04:53:43,520 --> 04:53:44,480
case
7758
04:53:44,480 --> 04:53:47,280
and i will set the style
7759
04:53:47,280 --> 04:53:49,280
background color
7760
04:53:49,280 --> 04:53:50,958
sky blue
7761
04:53:50,958 --> 04:53:52,560
and this will change the background
7762
04:53:52,560 --> 04:53:55,440
color of the body of my document if i
7763
04:53:55,440 --> 04:53:57,840
need to change some html element like
7764
04:53:57,840 --> 04:54:00,320
this div tag i could type
7765
04:54:00,320 --> 04:54:01,520
document
7766
04:54:01,520 --> 04:54:03,280
dot get
7767
04:54:03,280 --> 04:54:05,840
element by id
7768
04:54:05,840 --> 04:54:07,840
and we will search for an id within our
7769
04:54:07,840 --> 04:54:08,718
dom
7770
04:54:08,718 --> 04:54:13,360
like my div and i can set the inner html
7771
04:54:13,360 --> 04:54:17,120
equal to some text like hello
7772
04:54:17,120 --> 04:54:18,080
hello
7773
04:54:18,080 --> 04:54:20,000
that's a quick summary of what the dom
7774
04:54:20,000 --> 04:54:22,798
is it's an api an application
7775
04:54:22,798 --> 04:54:25,440
programming interface it's an interface
7776
04:54:25,440 --> 04:54:27,840
for changing the content of a page we
7777
04:54:27,840 --> 04:54:29,600
can change content without having to
7778
04:54:29,600 --> 04:54:32,160
reload the page to reflect any changes
7779
04:54:32,160 --> 04:54:34,638
document is the access point each node
7780
04:54:34,638 --> 04:54:36,958
can contain an object this tree is a
7781
04:54:36,958 --> 04:54:40,798
model so it's a document object model a
7782
04:54:40,798 --> 04:54:42,798
dom and in the next few topics i'll
7783
04:54:42,798 --> 04:54:44,878
demonstrate how we can search for given
7784
04:54:44,878 --> 04:54:47,840
elements and traverse nodes so that is a
7785
04:54:47,840 --> 04:54:51,600
quick overview of what the dom is
7786
04:54:51,600 --> 04:54:53,360
hey guys in this video i'm going to show
7787
04:54:53,360 --> 04:54:55,200
you a few different ways we can select
7788
04:54:55,200 --> 04:54:57,280
elements in a web page let's begin with
7789
04:54:57,280 --> 04:54:59,040
something very basic i'm going to create
7790
04:54:59,040 --> 04:55:01,680
an h1 header tag we'll create a menu for
7791
04:55:01,680 --> 04:55:05,520
a restaurant this is the menu and i will
7792
04:55:05,520 --> 04:55:08,240
give this h1 element a unique id
7793
04:55:08,240 --> 04:55:09,680
my
7794
04:55:09,680 --> 04:55:12,958
title to select this element i can type
7795
04:55:12,958 --> 04:55:15,440
document dot get
7796
04:55:15,440 --> 04:55:18,480
element by id
7797
04:55:18,480 --> 04:55:21,920
and the id was my title and i will store
7798
04:55:21,920 --> 04:55:23,840
this within a variable
7799
04:55:23,840 --> 04:55:24,718
let
7800
04:55:24,718 --> 04:55:28,878
element equal document.getelementbyid
7801
04:55:28,878 --> 04:55:30,798
and what should we do with this element
7802
04:55:30,798 --> 04:55:33,280
let's change the background color
7803
04:55:33,280 --> 04:55:35,200
so type element
7804
04:55:35,200 --> 04:55:36,638
dot style
7805
04:55:36,638 --> 04:55:38,840
dot background
7806
04:55:38,840 --> 04:55:41,760
color i'll set this to
7807
04:55:41,760 --> 04:55:45,440
light green that's a good color
7808
04:55:45,440 --> 04:55:47,600
so yeah you can select an element by its
7809
04:55:47,600 --> 04:55:49,840
id but you probably knew that already we
7810
04:55:49,840 --> 04:55:51,280
have a little bit of experience with
7811
04:55:51,280 --> 04:55:52,400
this method
7812
04:55:52,400 --> 04:55:54,958
let's select elements by their name the
7813
04:55:54,958 --> 04:55:56,480
name attribute is useful if you have
7814
04:55:56,480 --> 04:55:58,160
more than one elements in some sort of
7815
04:55:58,160 --> 04:56:00,718
group like radio buttons let's create
7816
04:56:00,718 --> 04:56:02,798
some radio buttons
7817
04:56:02,798 --> 04:56:03,840
input
7818
04:56:03,840 --> 04:56:06,400
type equals radio
7819
04:56:06,400 --> 04:56:08,480
and i will give this a name of fruits
7820
04:56:08,480 --> 04:56:11,520
the first will have a value of apple
7821
04:56:11,520 --> 04:56:13,840
then i'll create a label for this
7822
04:56:13,840 --> 04:56:14,958
label
7823
04:56:14,958 --> 04:56:15,760
for
7824
04:56:15,760 --> 04:56:17,360
equals
7825
04:56:17,360 --> 04:56:18,400
apple
7826
04:56:18,400 --> 04:56:21,840
close the label i'll add a line
7827
04:56:21,840 --> 04:56:24,000
and i will type apple okay two more
7828
04:56:24,000 --> 04:56:25,520
fruits
7829
04:56:25,520 --> 04:56:27,920
what about an orange
7830
04:56:27,920 --> 04:56:29,360
for orange
7831
04:56:29,360 --> 04:56:31,280
the text will be orange
7832
04:56:31,280 --> 04:56:32,798
and a banana
7833
04:56:32,798 --> 04:56:34,240
value
7834
04:56:34,240 --> 04:56:35,600
banana
7835
04:56:35,600 --> 04:56:39,040
for banana and the text will be banana
7836
04:56:39,040 --> 04:56:41,120
so each of these three radio buttons has
7837
04:56:41,120 --> 04:56:43,680
the same name fruits to select all of
7838
04:56:43,680 --> 04:56:45,520
these radio buttons within my document i
7839
04:56:45,520 --> 04:56:48,958
can type document dot get
7840
04:56:48,958 --> 04:56:51,760
elements that's plural with an s
7841
04:56:51,760 --> 04:56:53,840
by name
7842
04:56:53,840 --> 04:56:56,958
then pass a name as an argument fruits
7843
04:56:56,958 --> 04:56:59,760
this method returns a node list
7844
04:56:59,760 --> 04:57:01,600
it's similar to an array
7845
04:57:01,600 --> 04:57:03,600
so i will store that within
7846
04:57:03,600 --> 04:57:05,520
let's say fruits
7847
04:57:05,520 --> 04:57:08,320
let fruits equal document dot get
7848
04:57:08,320 --> 04:57:11,280
elements by name fruits so what would i
7849
04:57:11,280 --> 04:57:13,920
like to do with this node list of fruits
7850
04:57:13,920 --> 04:57:16,840
if i were to use console.log and display
7851
04:57:16,840 --> 04:57:19,360
fruits well we have a node list with our
7852
04:57:19,360 --> 04:57:22,000
three elements a radio button for apple
7853
04:57:22,000 --> 04:57:24,480
orange and banana if i add an index
7854
04:57:24,480 --> 04:57:27,200
number only that element is displayed so
7855
04:57:27,200 --> 04:57:30,000
we have our apple at index one would be
7856
04:57:30,000 --> 04:57:31,360
our orange
7857
04:57:31,360 --> 04:57:33,680
and two is our banana
7858
04:57:33,680 --> 04:57:34,878
maybe i would like to change the
7859
04:57:34,878 --> 04:57:36,160
background color of each of these
7860
04:57:36,160 --> 04:57:38,400
elements what's one way we can check to
7861
04:57:38,400 --> 04:57:40,638
see if one of these elements is selected
7862
04:57:40,638 --> 04:57:42,638
i'm going to add an attribute
7863
04:57:42,638 --> 04:57:45,440
with the apple radio tag let's add this
7864
04:57:45,440 --> 04:57:47,120
attribute
7865
04:57:47,120 --> 04:57:49,440
checked equals checked
7866
04:57:49,440 --> 04:57:52,560
by default this will already be selected
7867
04:57:52,560 --> 04:57:54,480
i would like to print whatever radio
7868
04:57:54,480 --> 04:57:56,878
button is checked we've already selected
7869
04:57:56,878 --> 04:57:59,360
our elements by their name fruits so i
7870
04:57:59,360 --> 04:58:01,680
will use the for each method of this
7871
04:58:01,680 --> 04:58:05,920
node list fruits for each i will cycle
7872
04:58:05,920 --> 04:58:08,240
through this node list of elements and
7873
04:58:08,240 --> 04:58:10,080
find whatever element is checked and
7874
04:58:10,080 --> 04:58:12,798
display it so we have our parameters
7875
04:58:12,798 --> 04:58:13,760
fruit
7876
04:58:13,760 --> 04:58:16,400
arrow function
7877
04:58:16,400 --> 04:58:19,680
let's check to see if our fruit
7878
04:58:19,680 --> 04:58:22,560
is checked fruit dot checked
7879
04:58:22,560 --> 04:58:24,360
then we will
7880
04:58:24,360 --> 04:58:25,920
console.log
7881
04:58:25,920 --> 04:58:27,760
whatever our fruit is
7882
04:58:27,760 --> 04:58:30,480
then add dot value
7883
04:58:30,480 --> 04:58:33,040
so our apple is selected
7884
04:58:33,040 --> 04:58:35,040
let's change the checked attribute of
7885
04:58:35,040 --> 04:58:37,840
our orange this time
7886
04:58:37,840 --> 04:58:41,638
so we have orange now
7887
04:58:42,400 --> 04:58:44,480
and banana
7888
04:58:44,480 --> 04:58:47,280
so that is get elements by name now we
7889
04:58:47,280 --> 04:58:50,000
have get elements by tag name heading to
7890
04:58:50,000 --> 04:58:52,560
our html file let's create an unordered
7891
04:58:52,560 --> 04:58:54,560
list
7892
04:58:54,560 --> 04:58:56,638
ul for unordered list
7893
04:58:56,638 --> 04:58:57,840
close it
7894
04:58:57,840 --> 04:59:01,280
we will add some list items
7895
04:59:02,480 --> 04:59:05,680
perhaps these are vegetables
7896
04:59:05,680 --> 04:59:08,160
carrots
7897
04:59:09,280 --> 04:59:12,080
potatoes
7898
04:59:12,240 --> 04:59:14,480
and onions
7899
04:59:14,480 --> 04:59:16,958
we can select elements by a given tag
7900
04:59:16,958 --> 04:59:19,280
name like list item let's store our
7901
04:59:19,280 --> 04:59:22,240
elements within vegetables let
7902
04:59:22,240 --> 04:59:23,680
vegetables
7903
04:59:23,680 --> 04:59:25,040
equal
7904
04:59:25,040 --> 04:59:27,680
document dot get
7905
04:59:27,680 --> 04:59:31,520
elements by tag name
7906
04:59:31,520 --> 04:59:33,360
and the tag name was
7907
04:59:33,360 --> 04:59:36,480
li for list items this method returns
7908
04:59:36,480 --> 04:59:39,040
technically an html collection it
7909
04:59:39,040 --> 04:59:41,440
behaves similarly to an array let's take
7910
04:59:41,440 --> 04:59:44,400
vegetables at index of zero
7911
04:59:44,400 --> 04:59:46,718
that would be our carrots i'm going to
7912
04:59:46,718 --> 04:59:49,280
access the style the background color
7913
04:59:49,280 --> 04:59:52,000
property
7914
04:59:52,000 --> 04:59:56,480
set the sequel to light green
7915
04:59:56,480 --> 04:59:58,000
the second element would have an index
7916
04:59:58,000 --> 04:59:59,680
of one
7917
04:59:59,680 --> 05:00:01,920
and the next element would be two
7918
05:00:01,920 --> 05:00:05,040
so that is get elements by tag name find
7919
05:00:05,040 --> 05:00:06,878
tags that you would like to select and
7920
05:00:06,878 --> 05:00:08,320
then just pass that as an argument to
7921
05:00:08,320 --> 05:00:09,440
this method
7922
05:00:09,440 --> 05:00:12,638
okay we have get elements by class name
7923
05:00:12,638 --> 05:00:14,160
i think this time i'll create some div
7924
05:00:14,160 --> 05:00:15,680
elements
7925
05:00:15,680 --> 05:00:17,840
div close it
7926
05:00:17,840 --> 05:00:21,840
and i will give this a class of desserts
7927
05:00:21,840 --> 05:00:23,280
desserts
7928
05:00:23,280 --> 05:00:26,638
first we'll have ice cream
7929
05:00:28,638 --> 05:00:32,638
cake and maybe pie
7930
05:00:32,638 --> 05:00:34,560
each of these three developments has the
7931
05:00:34,560 --> 05:00:36,718
same class name desserts
7932
05:00:36,718 --> 05:00:38,958
i'll store elements within
7933
05:00:38,958 --> 05:00:40,160
let
7934
05:00:40,160 --> 05:00:42,080
desserts
7935
05:00:42,080 --> 05:00:43,440
equals
7936
05:00:43,440 --> 05:00:45,840
document dot get
7937
05:00:45,840 --> 05:00:48,480
elements plural with an s
7938
05:00:48,480 --> 05:00:49,600
by
7939
05:00:49,600 --> 05:00:50,798
class
7940
05:00:50,798 --> 05:00:52,798
name
7941
05:00:52,798 --> 05:00:55,840
and the class name was desserts
7942
05:00:55,840 --> 05:00:58,160
desserts is technically an array-like
7943
05:00:58,160 --> 05:01:00,560
object but it behaves similarly to an
7944
05:01:00,560 --> 05:01:01,760
array again
7945
05:01:01,760 --> 05:01:03,440
so i'm going to change the background
7946
05:01:03,440 --> 05:01:07,360
color let's start with element zero
7947
05:01:07,360 --> 05:01:10,718
so that is the first element index zero
7948
05:01:10,718 --> 05:01:12,240
index one
7949
05:01:12,240 --> 05:01:13,920
and index two
7950
05:01:13,920 --> 05:01:16,400
that is the get elements by classname
7951
05:01:16,400 --> 05:01:19,040
method now we have query selector query
7952
05:01:19,040 --> 05:01:21,200
selector tends to be fairly popular we
7953
05:01:21,200 --> 05:01:24,160
can select an element by either an id
7954
05:01:24,160 --> 05:01:25,680
a class name
7955
05:01:25,680 --> 05:01:26,798
a tag
7956
05:01:26,798 --> 05:01:29,680
or an attribute let's create variable
7957
05:01:29,680 --> 05:01:30,958
element
7958
05:01:30,958 --> 05:01:32,798
equal
7959
05:01:32,798 --> 05:01:33,920
document
7960
05:01:33,920 --> 05:01:35,040
dot
7961
05:01:35,040 --> 05:01:38,080
query selector
7962
05:01:39,280 --> 05:01:42,080
then pass in an id a class name a tag
7963
05:01:42,080 --> 05:01:44,718
name or an attribute let's begin with an
7964
05:01:44,718 --> 05:01:49,200
id let's select id my title
7965
05:01:49,200 --> 05:01:50,878
if you're selecting an id be sure to
7966
05:01:50,878 --> 05:01:53,680
precede the name with a hashtag i will
7967
05:01:53,680 --> 05:01:55,280
change the background color of this
7968
05:01:55,280 --> 05:01:58,638
element to light green again
7969
05:01:58,638 --> 05:02:00,560
there so using query selector we've
7970
05:02:00,560 --> 05:02:03,360
selected an element by a unique id
7971
05:02:03,360 --> 05:02:06,480
we can also do so by a class name
7972
05:02:06,480 --> 05:02:08,560
so class desserts
7973
05:02:08,560 --> 05:02:09,760
with a class name you're going to
7974
05:02:09,760 --> 05:02:12,480
precede the class name with a dot
7975
05:02:12,480 --> 05:02:15,200
so class desserts now with quarry
7976
05:02:15,200 --> 05:02:18,160
selector it selects the first element of
7977
05:02:18,160 --> 05:02:19,840
any group if you need all of the
7978
05:02:19,840 --> 05:02:21,600
elements you'll need to use query
7979
05:02:21,600 --> 05:02:23,440
selector all which we'll cover in just a
7980
05:02:23,440 --> 05:02:25,360
little bit you can also select by a tag
7981
05:02:25,360 --> 05:02:28,480
name like list item the first list item
7982
05:02:28,480 --> 05:02:30,798
within my document will be selected or
7983
05:02:30,798 --> 05:02:32,718
you could even use an attribute so maybe
7984
05:02:32,718 --> 05:02:34,560
the first element that has the four
7985
05:02:34,560 --> 05:02:36,400
attribute i would like to select
7986
05:02:36,400 --> 05:02:38,878
i will use a set of square brackets and
7987
05:02:38,878 --> 05:02:41,600
type four the first element with the
7988
05:02:41,600 --> 05:02:43,920
four attribute is selected if you need
7989
05:02:43,920 --> 05:02:46,480
all elements you can use query selector
7990
05:02:46,480 --> 05:02:47,200
all
7991
05:02:47,200 --> 05:02:49,600
so let's copy this paste it
7992
05:02:49,600 --> 05:02:53,680
let elements query selector all i would
7993
05:02:53,680 --> 05:02:56,080
like to select all list items and change
7994
05:02:56,080 --> 05:02:57,760
the background color but we'll need to
7995
05:02:57,760 --> 05:03:00,080
iterate over each of the elements
7996
05:03:00,080 --> 05:03:03,600
i'll use a for each loop
7997
05:03:05,120 --> 05:03:07,520
elements for each
7998
05:03:07,520 --> 05:03:09,520
element
7999
05:03:09,520 --> 05:03:11,200
i will change the background color of
8000
05:03:11,200 --> 05:03:13,920
each element there each list item was
8001
05:03:13,920 --> 05:03:16,560
selected how about anything with the
8002
05:03:16,560 --> 05:03:19,600
desserts class name
8003
05:03:19,680 --> 05:03:23,200
or anything with the v4 attribute
8004
05:03:23,200 --> 05:03:25,200
so yeah those are a few different ways
8005
05:03:25,200 --> 05:03:27,840
in which we can select html elements if
8006
05:03:27,840 --> 05:03:29,600
you would like a copy of this code i'll
8007
05:03:29,600 --> 05:03:31,120
post all of this in the comments section
8008
05:03:31,120 --> 05:03:33,120
down below and well yeah that's how to
8009
05:03:33,120 --> 05:03:37,040
select elements using javascript
8010
05:03:37,040 --> 05:03:38,958
hey guys for this topic i'm going to
8011
05:03:38,958 --> 05:03:41,280
show you a few different dom traversal
8012
05:03:41,280 --> 05:03:43,280
techniques but before we do that i need
8013
05:03:43,280 --> 05:03:45,680
to explain some family relationships
8014
05:03:45,680 --> 05:03:48,160
between elements say we select our body
8015
05:03:48,160 --> 05:03:51,760
element within my html document i have
8016
05:03:51,760 --> 05:03:54,560
three unordered lists a list for fruit
8017
05:03:54,560 --> 05:03:57,040
vegetables and dessert they are arranged
8018
05:03:57,040 --> 05:03:59,600
in a hierarchy a family tree if we
8019
05:03:59,600 --> 05:04:02,560
select our body these three lists since
8020
05:04:02,560 --> 05:04:04,400
they're contained within the body
8021
05:04:04,400 --> 05:04:06,560
they would be considered children my
8022
05:04:06,560 --> 05:04:08,718
list of fruit is a child same with
8023
05:04:08,718 --> 05:04:10,000
vegetables
8024
05:04:10,000 --> 05:04:11,760
and dessert
8025
05:04:11,760 --> 05:04:13,840
since my list of fruit appears first
8026
05:04:13,840 --> 05:04:16,560
within my body the fruit list would be
8027
05:04:16,560 --> 05:04:19,440
considered the first child kind of like
8028
05:04:19,440 --> 05:04:20,958
it's the firstborn
8029
05:04:20,958 --> 05:04:23,760
dessert is last that would be the last
8030
05:04:23,760 --> 05:04:24,718
child
8031
05:04:24,718 --> 05:04:27,280
it's the last born if we select this
8032
05:04:27,280 --> 05:04:30,718
middle list of vegetables
8033
05:04:30,798 --> 05:04:33,120
then my body element is considered the
8034
05:04:33,120 --> 05:04:36,160
parent of this list the body element is
8035
05:04:36,160 --> 05:04:37,600
the parent
8036
05:04:37,600 --> 05:04:39,840
the two other lists would be considered
8037
05:04:39,840 --> 05:04:41,920
siblings
8038
05:04:41,920 --> 05:04:43,520
sibling
8039
05:04:43,520 --> 05:04:46,160
and sibling
8040
05:04:46,160 --> 05:04:48,638
since fruit appears before vegetables
8041
05:04:48,638 --> 05:04:52,240
fruit would be considered the previous
8042
05:04:52,240 --> 05:04:53,440
sibling
8043
05:04:53,440 --> 05:04:56,320
dessert would be considered the next
8044
05:04:56,320 --> 05:04:58,240
sibling
8045
05:04:58,240 --> 05:05:00,958
my unordered list does have children too
8046
05:05:00,958 --> 05:05:02,638
each of these list items would be
8047
05:05:02,638 --> 05:05:05,280
children of my unordered list yeah
8048
05:05:05,280 --> 05:05:07,600
that's a quick overview of the family
8049
05:05:07,600 --> 05:05:10,000
relationships between elements with that
8050
05:05:10,000 --> 05:05:12,560
out of the way here's my html document
8051
05:05:12,560 --> 05:05:14,400
within the body i have three unordered
8052
05:05:14,400 --> 05:05:15,520
lists
8053
05:05:15,520 --> 05:05:18,480
each has a unique id fruit vegetables
8054
05:05:18,480 --> 05:05:20,240
and dessert
8055
05:05:20,240 --> 05:05:22,240
suppose i would like to select the body
8056
05:05:22,240 --> 05:05:24,160
of my document i'll store that within a
8057
05:05:24,160 --> 05:05:26,160
variable let
8058
05:05:26,160 --> 05:05:27,440
element
8059
05:05:27,440 --> 05:05:28,638
equal
8060
05:05:28,638 --> 05:05:29,760
document
8061
05:05:29,760 --> 05:05:31,760
dot body
8062
05:05:31,760 --> 05:05:33,680
to select the first child that would be
8063
05:05:33,680 --> 05:05:35,760
the first unordered list
8064
05:05:35,760 --> 05:05:37,600
i'll store that within another variable
8065
05:05:37,600 --> 05:05:40,958
let child equal
8066
05:05:40,958 --> 05:05:42,958
element dot
8067
05:05:42,958 --> 05:05:46,080
first element child
8068
05:05:46,080 --> 05:05:47,920
let's color the background of this child
8069
05:05:47,920 --> 05:05:50,080
element green child
8070
05:05:50,080 --> 05:05:50,958
dot
8071
05:05:50,958 --> 05:05:53,040
background color
8072
05:05:53,040 --> 05:05:54,080
equals
8073
05:05:54,080 --> 05:05:55,120
light
8074
05:05:55,120 --> 05:05:57,360
green
8075
05:05:57,360 --> 05:06:00,718
this entire unordered list is now green
8076
05:06:00,718 --> 05:06:02,080
let me show you what happens when i
8077
05:06:02,080 --> 05:06:06,160
select the last element child of my body
8078
05:06:06,160 --> 05:06:08,560
so nothing appears to change
8079
05:06:08,560 --> 05:06:10,160
let's take a look at this child using
8080
05:06:10,160 --> 05:06:14,320
console.log console.log
8081
05:06:14,320 --> 05:06:15,520
child
8082
05:06:15,520 --> 05:06:17,520
so when i selected the last element of
8083
05:06:17,520 --> 05:06:20,878
my body we selected the script element
8084
05:06:20,878 --> 05:06:23,040
this element is the last child of my
8085
05:06:23,040 --> 05:06:25,440
body so that's something you need to pay
8086
05:06:25,440 --> 05:06:26,798
attention to
8087
05:06:26,798 --> 05:06:29,520
let's select our vegetable list
8088
05:06:29,520 --> 05:06:31,360
document dot
8089
05:06:31,360 --> 05:06:32,400
query
8090
05:06:32,400 --> 05:06:34,320
selector
8091
05:06:34,320 --> 05:06:36,240
i will select
8092
05:06:36,240 --> 05:06:38,320
vegetables
8093
05:06:38,320 --> 05:06:40,400
we will select the
8094
05:06:40,400 --> 05:06:42,878
parent element
8095
05:06:42,878 --> 05:06:46,160
i'll rename this as parent
8096
05:06:46,160 --> 05:06:48,320
the parent of this list would be the
8097
05:06:48,320 --> 05:06:50,798
entire body of my document that's why
8098
05:06:50,798 --> 05:06:53,120
the entire background color is now green
8099
05:06:53,120 --> 05:06:57,200
we can choose the next element sibling
8100
05:06:57,200 --> 05:06:59,440
sibling
8101
05:06:59,440 --> 05:07:00,958
sibling
8102
05:07:00,958 --> 05:07:03,760
if we select our list of vegetables the
8103
05:07:03,760 --> 05:07:07,040
next sibling would be my list of dessert
8104
05:07:07,040 --> 05:07:09,360
and the previous element sibling
8105
05:07:09,360 --> 05:07:11,120
is my list of fruit
8106
05:07:11,120 --> 05:07:13,200
what if you need one of these list items
8107
05:07:13,200 --> 05:07:16,718
i'll select my list of fruit
8108
05:07:16,718 --> 05:07:19,360
then select one of these elements
8109
05:07:19,360 --> 05:07:22,480
first element child
8110
05:07:25,200 --> 05:07:28,638
that would be the apple list item
8111
05:07:28,638 --> 05:07:31,360
last element child is the banana
8112
05:07:31,360 --> 05:07:33,440
another way to access the children of an
8113
05:07:33,440 --> 05:07:36,160
element is via an index
8114
05:07:36,160 --> 05:07:38,560
this time i will follow element with
8115
05:07:38,560 --> 05:07:41,920
children and list an index number
8116
05:07:41,920 --> 05:07:43,600
0 is the beginning
8117
05:07:43,600 --> 05:07:46,000
and we will change the first element at
8118
05:07:46,000 --> 05:07:46,840
index
8119
05:07:46,840 --> 05:07:49,760
0. one would be the second element
8120
05:07:49,760 --> 05:07:51,040
orange
8121
05:07:51,040 --> 05:07:53,760
two is the next element banana
8122
05:07:53,760 --> 05:07:55,280
if you need all the elements you can
8123
05:07:55,280 --> 05:07:57,840
simply use children this will return a
8124
05:07:57,840 --> 05:07:59,920
collection we would need to convert that
8125
05:07:59,920 --> 05:08:02,400
to an array this collection doesn't have
8126
05:08:02,400 --> 05:08:04,400
the for each method
8127
05:08:04,400 --> 05:08:05,718
i will surround
8128
05:08:05,718 --> 05:08:08,320
element.children with
8129
05:08:08,320 --> 05:08:09,600
array
8130
05:08:09,600 --> 05:08:12,240
dot from method
8131
05:08:12,240 --> 05:08:14,080
and we can iterate over the elements of
8132
05:08:14,080 --> 05:08:15,280
this array
8133
05:08:15,280 --> 05:08:18,638
let's rename child as children
8134
05:08:18,638 --> 05:08:20,000
children
8135
05:08:20,000 --> 05:08:20,878
dot
8136
05:08:20,878 --> 05:08:21,760
for
8137
05:08:21,760 --> 05:08:23,360
each method
8138
05:08:23,360 --> 05:08:26,160
we have our parameters child arrow
8139
05:08:26,160 --> 05:08:28,000
function
8140
05:08:28,000 --> 05:08:29,840
we'll change the style of each of the
8141
05:08:29,840 --> 05:08:32,878
children of this list
8142
05:08:33,760 --> 05:08:36,080
now each child is green
8143
05:08:36,080 --> 05:08:38,560
we could select a different list like
8144
05:08:38,560 --> 05:08:41,200
vegetables
8145
05:08:42,080 --> 05:08:42,840
or
8146
05:08:42,840 --> 05:08:44,400
dessert
8147
05:08:44,400 --> 05:08:46,638
so yeah those are a few dom traversal
8148
05:08:46,638 --> 05:08:48,798
techniques elements are arranged in a
8149
05:08:48,798 --> 05:08:51,760
family tree whatever element you select
8150
05:08:51,760 --> 05:08:54,080
that element can have a parent maybe
8151
05:08:54,080 --> 05:08:56,480
some children or siblings
8152
05:08:56,480 --> 05:08:58,400
so yeah those are a few basic dom
8153
05:08:58,400 --> 05:09:02,560
traversal techniques in javascript
8154
05:09:02,560 --> 05:09:04,240
hey guys for this topic i'm going to
8155
05:09:04,240 --> 05:09:06,798
show you how we can add and change html
8156
05:09:06,798 --> 05:09:08,958
elements i need to create an h1 header
8157
05:09:08,958 --> 05:09:12,958
tag to do that i will type document dot
8158
05:09:12,958 --> 05:09:14,480
create
8159
05:09:14,480 --> 05:09:16,320
element
8160
05:09:16,320 --> 05:09:18,320
within the create element method and
8161
05:09:18,320 --> 05:09:20,638
within quotes i can list a tag that i
8162
05:09:20,638 --> 05:09:22,160
would like to create i would like to
8163
05:09:22,160 --> 05:09:24,480
create an h1 header tag i'll assign this
8164
05:09:24,480 --> 05:09:26,638
to a variable
8165
05:09:26,638 --> 05:09:29,280
constant maybe name tag we'll store our
8166
05:09:29,280 --> 05:09:31,760
name name tag is an h1 header tag
8167
05:09:31,760 --> 05:09:33,600
however it doesn't contain any text
8168
05:09:33,600 --> 05:09:35,680
that's where changing html elements
8169
05:09:35,680 --> 05:09:37,760
comes in two ways in which i could add
8170
05:09:37,760 --> 05:09:40,000
some text to this h1 header tag is to
8171
05:09:40,000 --> 05:09:43,120
either use inner html or text content
8172
05:09:43,120 --> 05:09:44,798
text content is the preferred way
8173
05:09:44,798 --> 05:09:47,200
because inner html is vulnerable to
8174
05:09:47,200 --> 05:09:49,040
something called cross site scripting
8175
05:09:49,040 --> 05:09:50,718
attacks i'll give you a demonstration
8176
05:09:50,718 --> 05:09:53,280
momentarily if we were using inner html
8177
05:09:53,280 --> 05:09:56,000
i would take my tag dot
8178
05:09:56,000 --> 05:09:56,878
inner
8179
05:09:56,878 --> 05:09:58,240
html
8180
05:09:58,240 --> 05:10:00,160
set the sql to some text maybe your
8181
05:10:00,160 --> 05:10:02,400
first name then to add this element i
8182
05:10:02,400 --> 05:10:04,080
would need to declare where i would like
8183
05:10:04,080 --> 05:10:06,320
to add this tag specifically the body of
8184
05:10:06,320 --> 05:10:08,798
my document would be a good place
8185
05:10:08,798 --> 05:10:10,718
document dot
8186
05:10:10,718 --> 05:10:14,958
body and i will use the append method
8187
05:10:14,958 --> 05:10:16,878
and place my tag within the append
8188
05:10:16,878 --> 05:10:18,878
method as an argument this will create
8189
05:10:18,878 --> 05:10:21,040
an h1 header tag then add it to the body
8190
05:10:21,040 --> 05:10:22,798
of my document what if we accept some
8191
05:10:22,798 --> 05:10:24,560
user input instead
8192
05:10:24,560 --> 05:10:25,680
window
8193
05:10:25,680 --> 05:10:28,320
dot prompt
8194
05:10:28,320 --> 05:10:30,718
enter your name
8195
05:10:30,718 --> 05:10:32,798
enter your name type in your first name
8196
05:10:32,798 --> 05:10:35,520
press ok and there's your h1 header tag
8197
05:10:35,520 --> 05:10:36,958
now here's the problem with using
8198
05:10:36,958 --> 05:10:39,920
innerhtml we could also assign some tags
8199
05:10:39,920 --> 05:10:41,920
along with some text if i was to run
8200
05:10:41,920 --> 05:10:43,920
this again i could place a malicious
8201
05:10:43,920 --> 05:10:46,000
script within the user input this would
8202
05:10:46,000 --> 05:10:48,160
be an example of a cross site script
8203
05:10:48,160 --> 05:10:50,000
attack i'm going to blur this section
8204
05:10:50,000 --> 05:10:51,120
just because i don't want to give you
8205
05:10:51,120 --> 05:10:53,200
guys any ideas then if i were to press
8206
05:10:53,200 --> 05:10:55,840
ok this would activate my script this
8207
05:10:55,840 --> 05:10:57,680
script that i wrote just has a pop-up
8208
05:10:57,680 --> 05:11:00,000
that says virus the preferred way of
8209
05:11:00,000 --> 05:11:02,560
adding some text to an element is to set
8210
05:11:02,560 --> 05:11:05,200
the text content when i run this program
8211
05:11:05,200 --> 05:11:08,320
again and enter my malicious script this
8212
05:11:08,320 --> 05:11:11,200
script will be instead parsed as text
8213
05:11:11,200 --> 05:11:13,520
setting text content is a much safer
8214
05:11:13,520 --> 05:11:16,080
approach than using innerhtml this time
8215
05:11:16,080 --> 05:11:17,760
we're going to add a list item to an
8216
05:11:17,760 --> 05:11:19,920
unordered list heading to our html
8217
05:11:19,920 --> 05:11:23,920
document we'll create an unordered list
8218
05:11:23,920 --> 05:11:27,920
i'll give this list an id of fruit
8219
05:11:27,920 --> 05:11:32,200
then we will add some list items
8220
05:11:33,040 --> 05:11:35,440
apple
8221
05:11:37,520 --> 05:11:39,920
orange
8222
05:11:40,638 --> 05:11:43,200
banana
8223
05:11:43,520 --> 05:11:45,440
okay here's my list
8224
05:11:45,440 --> 05:11:48,000
to append an item to my list well i need
8225
05:11:48,000 --> 05:11:49,760
to know what my list is
8226
05:11:49,760 --> 05:11:51,600
i'll store that within a variable
8227
05:11:51,600 --> 05:11:52,958
constant
8228
05:11:52,958 --> 05:11:54,718
my list
8229
05:11:54,718 --> 05:11:57,440
and i would need to select this list
8230
05:11:57,440 --> 05:12:00,958
i can do that with document dot query
8231
05:12:00,958 --> 05:12:02,958
selector
8232
05:12:02,958 --> 05:12:04,718
pass in an id
8233
05:12:04,718 --> 05:12:06,400
the id was
8234
05:12:06,400 --> 05:12:08,718
fruit
8235
05:12:09,280 --> 05:12:12,840
then i can create a list item element
8236
05:12:12,840 --> 05:12:14,878
construct list
8237
05:12:14,878 --> 05:12:18,160
item equals document
8238
05:12:18,160 --> 05:12:20,400
dot create
8239
05:12:20,400 --> 05:12:21,520
element
8240
05:12:21,520 --> 05:12:23,040
what kind of element would we like to
8241
05:12:23,040 --> 05:12:25,200
create let's create a list item element
8242
05:12:25,200 --> 05:12:27,040
to set the text of this list item
8243
05:12:27,040 --> 05:12:29,840
element i will take my list item then
8244
05:12:29,840 --> 05:12:33,200
set the text content property equal to
8245
05:12:33,200 --> 05:12:35,840
whatever i want i feel like a main go
8246
05:12:35,840 --> 05:12:37,840
then we just need to add this list item
8247
05:12:37,840 --> 05:12:41,360
to my list my list use the append method
8248
05:12:41,360 --> 05:12:43,840
then pass in my list item
8249
05:12:43,840 --> 05:12:45,840
there we have added a mango if you would
8250
05:12:45,840 --> 05:12:47,520
like this list item at the beginning of
8251
05:12:47,520 --> 05:12:49,840
your list you can use prepend
8252
05:12:49,840 --> 05:12:51,680
pre-penned
8253
05:12:51,680 --> 05:12:54,958
now we have mango apple orange banana
8254
05:12:54,958 --> 05:12:56,798
otherwise if you need to place an item
8255
05:12:56,798 --> 05:12:58,718
somewhere within the middle of the list
8256
05:12:58,718 --> 05:13:00,320
it's a little more complex but here's
8257
05:13:00,320 --> 05:13:02,878
how take your parent element my list in
8258
05:13:02,878 --> 05:13:04,080
this example
8259
05:13:04,080 --> 05:13:05,040
dot
8260
05:13:05,040 --> 05:13:06,400
insert
8261
05:13:06,400 --> 05:13:07,840
before method
8262
05:13:07,840 --> 05:13:10,400
pass in what's to be inserted our list
8263
05:13:10,400 --> 05:13:12,160
item then we need to get all of the
8264
05:13:12,160 --> 05:13:14,878
elements we can do that by taking my
8265
05:13:14,878 --> 05:13:16,160
list again
8266
05:13:16,160 --> 05:13:17,120
dot
8267
05:13:17,120 --> 05:13:18,240
get
8268
05:13:18,240 --> 05:13:19,440
elements
8269
05:13:19,440 --> 05:13:21,680
by tag name
8270
05:13:21,680 --> 05:13:23,840
we would like to select all list item
8271
05:13:23,840 --> 05:13:25,520
elements
8272
05:13:25,520 --> 05:13:28,160
then an index number to insert main go
8273
05:13:28,160 --> 05:13:29,440
before orange
8274
05:13:29,440 --> 05:13:31,840
the index would be 1. following the get
8275
05:13:31,840 --> 05:13:33,680
elements by tagname method i will add
8276
05:13:33,680 --> 05:13:35,040
the index of where i would like to
8277
05:13:35,040 --> 05:13:38,240
insert this list item so 1. now we have
8278
05:13:38,240 --> 05:13:41,040
apple mango orange banana
8279
05:13:41,040 --> 05:13:42,400
two would be
8280
05:13:42,400 --> 05:13:44,798
apple orange mango banana
8281
05:13:44,798 --> 05:13:47,040
and then three would be the end so yeah
8282
05:13:47,040 --> 05:13:49,040
everybody that's a beginner's way to add
8283
05:13:49,040 --> 05:13:51,360
and change html elements if you would
8284
05:13:51,360 --> 05:13:53,120
like a copy of this code i'll post all
8285
05:13:53,120 --> 05:13:54,400
of this in the comment section down
8286
05:13:54,400 --> 05:13:56,798
below and well yeah that's how to add
8287
05:13:56,798 --> 05:13:58,638
and change html elements using
8288
05:13:58,638 --> 05:14:01,280
javascript
8289
05:14:02,400 --> 05:14:04,240
hey what's up guys in this video i'm
8290
05:14:04,240 --> 05:14:05,840
going to show you how we can change some
8291
05:14:05,840 --> 05:14:08,240
css properties of elements heading to
8292
05:14:08,240 --> 05:14:10,958
our index.html file let's create a
8293
05:14:10,958 --> 05:14:13,680
simple h1 header tag
8294
05:14:13,680 --> 05:14:17,360
i'll give this header tag a unique id of
8295
05:14:17,360 --> 05:14:19,280
my title
8296
05:14:19,280 --> 05:14:21,040
then add some text
8297
05:14:21,040 --> 05:14:24,160
this is my title
8298
05:14:24,160 --> 05:14:25,440
then save
8299
05:14:25,440 --> 05:14:27,680
in order to change the css properties of
8300
05:14:27,680 --> 05:14:30,000
this element we first need to select
8301
05:14:30,000 --> 05:14:32,080
this element i'll declare this as a
8302
05:14:32,080 --> 05:14:33,200
constant
8303
05:14:33,200 --> 05:14:34,480
const
8304
05:14:34,480 --> 05:14:35,760
title
8305
05:14:35,760 --> 05:14:37,760
equals then to select an element you
8306
05:14:37,760 --> 05:14:38,718
type
8307
05:14:38,718 --> 05:14:39,920
document
8308
05:14:39,920 --> 05:14:41,040
dot
8309
05:14:41,040 --> 05:14:42,160
get
8310
05:14:42,160 --> 05:14:43,360
element
8311
05:14:43,360 --> 05:14:44,958
by id
8312
05:14:44,958 --> 05:14:46,958
within the parentheses and within quotes
8313
05:14:46,958 --> 05:14:49,440
we will list the id of our element we
8314
05:14:49,440 --> 05:14:51,480
would like to select
8315
05:14:51,480 --> 05:14:53,920
getelementbyid is a very basic way of
8316
05:14:53,920 --> 05:14:55,920
selecting an element in the future we'll
8317
05:14:55,920 --> 05:14:58,160
cover more advanced methods but for now
8318
05:14:58,160 --> 05:15:00,958
we'll stick with getelementbyid so we're
8319
05:15:00,958 --> 05:15:03,280
storing this within a variable title and
8320
05:15:03,280 --> 05:15:05,200
now we can do stuff with it
8321
05:15:05,200 --> 05:15:07,760
to change css properties of some element
8322
05:15:07,760 --> 05:15:10,400
such as my title i will follow the name
8323
05:15:10,400 --> 05:15:13,440
of this element title then access the
8324
05:15:13,440 --> 05:15:15,120
style attribute
8325
05:15:15,120 --> 05:15:17,600
dot then a css property
8326
05:15:17,600 --> 05:15:18,958
maybe i would like to change the
8327
05:15:18,958 --> 05:15:21,120
background color so i will list that
8328
05:15:21,120 --> 05:15:22,320
property
8329
05:15:22,320 --> 05:15:23,440
background
8330
05:15:23,440 --> 05:15:24,480
color
8331
05:15:24,480 --> 05:15:26,400
when accessing a css property through
8332
05:15:26,400 --> 05:15:29,360
javascript the format is in camel case
8333
05:15:29,360 --> 05:15:31,760
normally with css there's a dash between
8334
05:15:31,760 --> 05:15:34,798
words such as background dash color but
8335
05:15:34,798 --> 05:15:36,400
if you're using javascript you use
8336
05:15:36,400 --> 05:15:37,760
camelcase
8337
05:15:37,760 --> 05:15:39,680
the first letter of the first word is
8338
05:15:39,680 --> 05:15:41,760
not capital then the first letter of any
8339
05:15:41,760 --> 05:15:44,080
word after the first is capital
8340
05:15:44,080 --> 05:15:46,718
background color and set the sequel to a
8341
05:15:46,718 --> 05:15:49,680
color you can pick a color name maybe
8342
05:15:49,680 --> 05:15:51,520
blue
8343
05:15:51,520 --> 05:15:54,798
you can select rgb values rgb within the
8344
05:15:54,798 --> 05:15:56,878
parenthesis you have three numbers each
8345
05:15:56,878 --> 05:15:58,878
corresponds to the amount of red green
8346
05:15:58,878 --> 05:16:00,080
and blue
8347
05:16:00,080 --> 05:16:04,240
uh so maybe 50 200 250 i don't know what
8348
05:16:04,240 --> 05:16:05,920
this color is going to be
8349
05:16:05,920 --> 05:16:08,000
oh i kind of like that color or you
8350
05:16:08,000 --> 05:16:10,240
could use hexadecimal values
8351
05:16:10,240 --> 05:16:14,240
what's 2 two two two two two
8352
05:16:14,240 --> 05:16:16,798
okay that is a dark gray you have a
8353
05:16:16,798 --> 05:16:18,878
couple options when it comes to colors
8354
05:16:18,878 --> 05:16:21,280
let's change the font of my title what
8355
05:16:21,280 --> 05:16:22,798
element would we like we would like
8356
05:16:22,798 --> 05:16:25,840
title we'll access the style then a
8357
05:16:25,840 --> 05:16:28,958
property color
8358
05:16:28,958 --> 05:16:33,680
uh let's use rgb this time rgb
8359
05:16:33,680 --> 05:16:35,840
5200
8360
05:16:35,840 --> 05:16:38,400
250
8361
05:16:38,400 --> 05:16:40,240
there and we have that color again so
8362
05:16:40,240 --> 05:16:43,600
again you can use color names rgb values
8363
05:16:43,600 --> 05:16:45,840
or hexadecimal values and if you don't
8364
05:16:45,840 --> 05:16:48,000
know any you can always google hex color
8365
05:16:48,000 --> 05:16:49,360
picker
8366
05:16:49,360 --> 05:16:51,360
and pick a color that you like
8367
05:16:51,360 --> 05:16:53,200
here are the rgb values or the
8368
05:16:53,200 --> 05:16:55,360
hexadecimal values okay let's change a
8369
05:16:55,360 --> 05:16:57,040
few other properties
8370
05:16:57,040 --> 05:16:58,878
we're selecting our title
8371
05:16:58,878 --> 05:17:00,958
accessing the style attribute
8372
05:17:00,958 --> 05:17:03,440
then let's change the font family
8373
05:17:03,440 --> 05:17:06,480
property i will set the sequel to
8374
05:17:06,480 --> 05:17:10,400
pick a font i like this font
8375
05:17:10,560 --> 05:17:12,560
and there we go we have a new font i
8376
05:17:12,560 --> 05:17:14,798
would like to text align center this
8377
05:17:14,798 --> 05:17:16,080
element
8378
05:17:16,080 --> 05:17:18,718
title dot style
8379
05:17:18,718 --> 05:17:20,000
dot
8380
05:17:20,000 --> 05:17:21,040
text
8381
05:17:21,040 --> 05:17:24,958
align equals center
8382
05:17:24,958 --> 05:17:26,718
then add a border
8383
05:17:26,718 --> 05:17:28,798
title dot style
8384
05:17:28,798 --> 05:17:30,878
dot border
8385
05:17:30,878 --> 05:17:36,320
equals 2 pixels solid is good
8386
05:17:36,320 --> 05:17:38,080
yeah and we have a border
8387
05:17:38,080 --> 05:17:40,400
to hide an element we can access the
8388
05:17:40,400 --> 05:17:42,160
display property
8389
05:17:42,160 --> 05:17:44,480
title dot style
8390
05:17:44,480 --> 05:17:45,760
dot
8391
05:17:45,760 --> 05:17:46,958
display
8392
05:17:46,958 --> 05:17:49,200
equals none
8393
05:17:49,200 --> 05:17:51,520
this will hide this element or we could
8394
05:17:51,520 --> 05:17:53,840
set this to block to display it
8395
05:17:53,840 --> 05:17:56,240
so yeah everybody that is a very basic
8396
05:17:56,240 --> 05:17:58,638
way to change css properties you would
8397
05:17:58,638 --> 05:18:00,480
first need to select an element or
8398
05:18:00,480 --> 05:18:03,360
elements follow that element with dot
8399
05:18:03,360 --> 05:18:05,760
the style attribute dot
8400
05:18:05,760 --> 05:18:08,000
the css property and you can set it
8401
05:18:08,000 --> 05:18:10,560
equal to a new value that is how to add
8402
05:18:10,560 --> 05:18:12,320
and change css properties using
8403
05:18:12,320 --> 05:18:14,320
javascript if you would like a copy of
8404
05:18:14,320 --> 05:18:15,760
all this code i'll post this in the
8405
05:18:15,760 --> 05:18:17,840
comments section down below be sure to
8406
05:18:17,840 --> 05:18:19,600
look underneath the original video in
8407
05:18:19,600 --> 05:18:22,160
the 100 video playlist and well yeah
8408
05:18:22,160 --> 05:18:24,400
that's how to change some css properties
8409
05:18:24,400 --> 05:18:27,440
using javascript
8410
05:18:27,920 --> 05:18:29,600
hey guys in this video i'm going to
8411
05:18:29,600 --> 05:18:32,160
explain events an event is some action
8412
05:18:32,160 --> 05:18:34,718
that the user or the browser does many
8413
05:18:34,718 --> 05:18:37,440
html elements contain event attributes
8414
05:18:37,440 --> 05:18:40,878
for example let's create a button
8415
05:18:40,878 --> 05:18:45,360
i'll give this button an id of my button
8416
05:18:45,520 --> 05:18:48,400
for the text let's just say button
8417
05:18:48,400 --> 05:18:51,120
buttons have an on click event attribute
8418
05:18:51,120 --> 05:18:52,878
on click
8419
05:18:52,878 --> 05:18:54,560
i can set this equal to the name of a
8420
05:18:54,560 --> 05:18:56,240
function and invoke it
8421
05:18:56,240 --> 05:18:57,520
let's create a function that will do
8422
05:18:57,520 --> 05:19:00,160
something i'll cleverly name this
8423
05:19:00,160 --> 05:19:01,840
function do
8424
05:19:01,840 --> 05:19:04,320
something
8425
05:19:04,560 --> 05:19:08,080
all we'll do is create an alert alert
8426
05:19:08,080 --> 05:19:08,958
you
8427
05:19:08,958 --> 05:19:10,000
did
8428
05:19:10,000 --> 05:19:11,600
something
8429
05:19:11,600 --> 05:19:14,080
i will set the on click attribute equal
8430
05:19:14,080 --> 05:19:16,560
to the name of my function do
8431
05:19:16,560 --> 05:19:17,600
something
8432
05:19:17,600 --> 05:19:19,360
then invoke it
8433
05:19:19,360 --> 05:19:21,680
when i click on this button my on click
8434
05:19:21,680 --> 05:19:23,440
event attribute will invoke this
8435
05:19:23,440 --> 05:19:25,200
function do something
8436
05:19:25,200 --> 05:19:27,360
you did something in place of setting
8437
05:19:27,360 --> 05:19:28,958
event attributes directly within your
8438
05:19:28,958 --> 05:19:31,280
html elements you could do so within
8439
05:19:31,280 --> 05:19:33,360
your javascript file
8440
05:19:33,360 --> 05:19:36,480
i would first need to select my element
8441
05:19:36,480 --> 05:19:37,600
constant
8442
05:19:37,600 --> 05:19:39,600
element equals
8443
05:19:39,600 --> 05:19:44,560
document dot get element by id
8444
05:19:44,560 --> 05:19:47,200
the id was my button
8445
05:19:47,200 --> 05:19:49,040
i will take this element
8446
05:19:49,040 --> 05:19:51,840
and set the on click event attribute
8447
05:19:51,840 --> 05:19:53,760
equal to some function
8448
05:19:53,760 --> 05:19:54,638
do
8449
05:19:54,638 --> 05:19:57,040
something be sure not to invoke it this
8450
05:19:57,040 --> 05:19:58,718
is a callback
8451
05:19:58,718 --> 05:20:00,878
save everything then when i click this
8452
05:20:00,878 --> 05:20:02,718
button we activate the on click event
8453
05:20:02,718 --> 05:20:05,200
attribute so that's one event attribute
8454
05:20:05,200 --> 05:20:08,160
on click another event attribute is on
8455
05:20:08,160 --> 05:20:10,638
load when the web browser loads it does
8456
05:20:10,638 --> 05:20:12,718
something this time i'm going to select
8457
05:20:12,718 --> 05:20:14,718
the body of my document
8458
05:20:14,718 --> 05:20:19,520
constant element equals document.body
8459
05:20:19,520 --> 05:20:23,040
i will take this element and set the on
8460
05:20:23,040 --> 05:20:25,920
load attribute equal to some function
8461
05:20:25,920 --> 05:20:26,878
do
8462
05:20:26,878 --> 05:20:28,480
something
8463
05:20:28,480 --> 05:20:30,718
when the body of my document loads it
8464
05:20:30,718 --> 05:20:33,680
activates the onload event attribute
8465
05:20:33,680 --> 05:20:35,680
another option is that within the
8466
05:20:35,680 --> 05:20:38,240
opening body tag of your html file you
8467
05:20:38,240 --> 05:20:41,440
can directly set that attribute
8468
05:20:41,440 --> 05:20:44,320
do something and this would work too
8469
05:20:44,320 --> 05:20:46,958
that is the onload event attribute when
8470
05:20:46,958 --> 05:20:49,440
an element loads it does something
8471
05:20:49,440 --> 05:20:51,440
next we have the on change event
8472
05:20:51,440 --> 05:20:53,120
attribute
8473
05:20:53,120 --> 05:20:54,798
element dot
8474
05:20:54,798 --> 05:20:55,760
on
8475
05:20:55,760 --> 05:20:57,840
change equals
8476
05:20:57,840 --> 05:20:59,920
do something
8477
05:20:59,920 --> 05:21:02,240
when an element has been changed it
8478
05:21:02,240 --> 05:21:04,718
activates the on change event attribute
8479
05:21:04,718 --> 05:21:06,718
heading to our html document let's
8480
05:21:06,718 --> 05:21:09,280
create a text box
8481
05:21:09,280 --> 05:21:10,638
input
8482
05:21:10,638 --> 05:21:14,080
id equals my text
8483
05:21:14,080 --> 05:21:16,160
get rid of this
8484
05:21:16,160 --> 05:21:18,878
we will select our text box constant
8485
05:21:18,878 --> 05:21:20,878
element equals
8486
05:21:20,878 --> 05:21:26,080
document dot get element by id
8487
05:21:26,080 --> 05:21:27,600
we are selecting
8488
05:21:27,600 --> 05:21:29,360
my text
8489
05:21:29,360 --> 05:21:31,200
now when we make changes to this text
8490
05:21:31,200 --> 05:21:32,878
box
8491
05:21:32,878 --> 05:21:34,400
and then leave
8492
05:21:34,400 --> 05:21:36,160
it activates my on change event
8493
05:21:36,160 --> 05:21:39,040
attribute if used with a text box
8494
05:21:39,040 --> 05:21:40,958
you could format some text once you
8495
05:21:40,958 --> 05:21:41,920
leave
8496
05:21:41,920 --> 05:21:43,680
okay this next one is going to be fun
8497
05:21:43,680 --> 05:21:46,000
let's create a div tag
8498
05:21:46,000 --> 05:21:46,878
div
8499
05:21:46,878 --> 05:21:49,920
id equals my div
8500
05:21:49,920 --> 05:21:50,958
close it
8501
05:21:50,958 --> 05:21:53,280
then heading to our css style sheet
8502
05:21:53,280 --> 05:21:56,080
let's select my div
8503
05:21:56,080 --> 05:21:59,680
we'll change the background color
8504
05:21:59,840 --> 05:22:03,200
to light green
8505
05:22:03,200 --> 05:22:05,440
and the width to 100
8506
05:22:05,440 --> 05:22:07,440
100 pixels
8507
05:22:07,440 --> 05:22:11,120
and the height to be 100 as well
8508
05:22:11,760 --> 05:22:16,320
let's save we'll select this id my div
8509
05:22:16,320 --> 05:22:20,798
constant element equals document dot get
8510
05:22:20,798 --> 05:22:23,600
element by id
8511
05:22:23,600 --> 05:22:26,080
the id was my div
8512
05:22:26,080 --> 05:22:28,160
when we move our cursor over this div
8513
05:22:28,160 --> 05:22:30,798
tag we'll change the background color we
8514
05:22:30,798 --> 05:22:32,558
will take our element
8515
05:22:32,558 --> 05:22:34,558
and use the on
8516
05:22:34,558 --> 05:22:37,040
mouse over event attribute
8517
05:22:37,040 --> 05:22:39,120
set the sql to a function
8518
05:22:39,120 --> 05:22:42,840
do something
8519
05:22:44,080 --> 05:22:46,718
change the background color element dot
8520
05:22:46,718 --> 05:22:48,638
style dot
8521
05:22:48,638 --> 05:22:49,920
background
8522
05:22:49,920 --> 05:22:50,958
color
8523
05:22:50,958 --> 05:22:53,920
equals how about red
8524
05:22:53,920 --> 05:22:56,160
when i hover my cursor over this div tag
8525
05:22:56,160 --> 05:22:58,480
it's gonna change the color when you
8526
05:22:58,480 --> 05:22:59,840
leave an element
8527
05:22:59,840 --> 05:23:02,320
you can set the on mouse out event
8528
05:23:02,320 --> 05:23:04,798
attribute
8529
05:23:05,840 --> 05:23:09,520
element dot on mouse out we'll create a
8530
05:23:09,520 --> 05:23:12,958
function to do something else let's copy
8531
05:23:12,958 --> 05:23:14,480
this paste it
8532
05:23:14,480 --> 05:23:16,878
do something else
8533
05:23:16,878 --> 05:23:18,958
we'll change the background color to
8534
05:23:18,958 --> 05:23:20,958
light green again
8535
05:23:20,958 --> 05:23:22,400
so when i enter
8536
05:23:22,400 --> 05:23:24,798
the color is red when i leave it's green
8537
05:23:24,798 --> 05:23:27,840
red green red green red green so that is
8538
05:23:27,840 --> 05:23:30,558
on mouse over and on mouse out
8539
05:23:30,558 --> 05:23:35,040
okay we're on the last two on mouse down
8540
05:23:35,040 --> 05:23:36,638
element dot
8541
05:23:36,638 --> 05:23:39,520
on mouse down
8542
05:23:39,520 --> 05:23:42,878
equals do something
8543
05:23:42,878 --> 05:23:44,718
so when i click down when i click on
8544
05:23:44,718 --> 05:23:45,920
this element
8545
05:23:45,920 --> 05:23:47,680
it activates the on mouse event
8546
05:23:47,680 --> 05:23:48,878
attribute
8547
05:23:48,878 --> 05:23:50,878
by itself it's basically no different
8548
05:23:50,878 --> 05:23:53,360
from the on click event attribute but it
8549
05:23:53,360 --> 05:23:55,600
pairs very well with the on mouse up
8550
05:23:55,600 --> 05:23:58,400
event attribute
8551
05:23:58,878 --> 05:24:00,958
on mouse up
8552
05:24:00,958 --> 05:24:02,958
when we let go of our mouse
8553
05:24:02,958 --> 05:24:05,200
we can perform another function do
8554
05:24:05,200 --> 05:24:06,320
something else
8555
05:24:06,320 --> 05:24:08,480
when i click and hold down it's red when
8556
05:24:08,480 --> 05:24:11,840
i let go it's green red green red green
8557
05:24:11,840 --> 05:24:13,680
red green all right everybody so those
8558
05:24:13,680 --> 05:24:16,718
are a few events on click on load on
8559
05:24:16,718 --> 05:24:19,360
change on mouse over on mouse out on
8560
05:24:19,360 --> 05:24:21,760
mouse down on mouse up and in the next
8561
05:24:21,760 --> 05:24:24,240
topic we'll look at event handlers if
8562
05:24:24,240 --> 05:24:25,920
you would like a copy of all this code
8563
05:24:25,920 --> 05:24:27,360
i'll post all of this in the comments
8564
05:24:27,360 --> 05:24:29,440
section down below and well yeah those
8565
05:24:29,440 --> 05:24:33,040
are a few javascript events
8566
05:24:33,280 --> 05:24:34,958
hey guys in this video i'm going to
8567
05:24:34,958 --> 05:24:37,680
explain the add event listener method we
8568
05:24:37,680 --> 05:24:39,920
can link an event and a function to an
8569
05:24:39,920 --> 05:24:42,080
html element using the add event
8570
05:24:42,080 --> 05:24:44,400
listener method is the preferred way of
8571
05:24:44,400 --> 05:24:46,718
handling events a benefit is that one
8572
05:24:46,718 --> 05:24:49,440
element can have several event listeners
8573
05:24:49,440 --> 05:24:51,520
even the same event can invoke different
8574
05:24:51,520 --> 05:24:53,920
functions within my html document i'm
8575
05:24:53,920 --> 05:24:56,878
going to create a div
8576
05:24:56,878 --> 05:24:58,160
div
8577
05:24:58,160 --> 05:24:59,920
close it
8578
05:24:59,920 --> 05:25:03,120
i'll give this a unique id of inner div
8579
05:25:03,120 --> 05:25:06,798
eventually we'll create an outer div
8580
05:25:06,798 --> 05:25:08,798
then within my css style sheet i'll
8581
05:25:08,798 --> 05:25:09,840
select
8582
05:25:09,840 --> 05:25:10,798
inner
8583
05:25:10,798 --> 05:25:12,638
div
8584
05:25:12,638 --> 05:25:15,040
i'll give this a background color of
8585
05:25:15,040 --> 05:25:16,840
light
8586
05:25:16,840 --> 05:25:20,798
green light green
8587
05:25:20,798 --> 05:25:22,480
a width
8588
05:25:22,480 --> 05:25:24,798
of 100
8589
05:25:24,798 --> 05:25:27,840
a height of 100
8590
05:25:28,320 --> 05:25:30,160
and a border
8591
05:25:30,160 --> 05:25:33,840
one pixel solid is good
8592
05:25:33,920 --> 05:25:36,000
save everything and here's our div
8593
05:25:36,000 --> 05:25:38,240
element using the add event listener
8594
05:25:38,240 --> 05:25:39,280
method
8595
05:25:39,280 --> 05:25:41,440
this development can listen for multiple
8596
05:25:41,440 --> 05:25:44,480
events we'll need to select an element
8597
05:25:44,480 --> 05:25:47,440
i'll name this const
8598
05:25:47,440 --> 05:25:49,440
inner div
8599
05:25:49,440 --> 05:25:50,558
equals
8600
05:25:50,558 --> 05:25:51,760
document
8601
05:25:51,760 --> 05:25:53,280
dot get
8602
05:25:53,280 --> 05:25:55,920
element by id
8603
05:25:55,920 --> 05:26:00,000
we're selecting inner div
8604
05:26:00,000 --> 05:26:01,520
to add an event listener you take your
8605
05:26:01,520 --> 05:26:03,920
element inner div in this case
8606
05:26:03,920 --> 05:26:05,840
then invoke the add event listener
8607
05:26:05,840 --> 05:26:07,520
method
8608
05:26:07,520 --> 05:26:09,760
the first argument is an event
8609
05:26:09,760 --> 05:26:11,840
let's add mouse over
8610
05:26:11,840 --> 05:26:13,040
mouse
8611
05:26:13,040 --> 05:26:14,480
over
8612
05:26:14,480 --> 05:26:15,920
then a function
8613
05:26:15,920 --> 05:26:18,160
let's create a function to change the
8614
05:26:18,160 --> 05:26:19,600
color red
8615
05:26:19,600 --> 05:26:22,958
this will be a callback change red
8616
05:26:22,958 --> 05:26:25,840
let's define this function
8617
05:26:25,840 --> 05:26:27,200
function
8618
05:26:27,200 --> 05:26:30,320
change red
8619
05:26:30,798 --> 05:26:33,440
let's take our inner div
8620
05:26:33,440 --> 05:26:35,280
take the style
8621
05:26:35,280 --> 05:26:38,558
set the background color property
8622
05:26:38,558 --> 05:26:40,718
equal to red
8623
05:26:40,718 --> 05:26:42,718
when i hover my cursor over this div
8624
05:26:42,718 --> 05:26:45,360
element it changes red a benefit of
8625
05:26:45,360 --> 05:26:47,600
using the add event listener method is
8626
05:26:47,600 --> 05:26:49,440
that it's easy to wait for multiple
8627
05:26:49,440 --> 05:26:51,040
events
8628
05:26:51,040 --> 05:26:53,040
so to wait for another event
8629
05:26:53,040 --> 05:26:54,798
we can simply invoke the add event
8630
05:26:54,798 --> 05:26:56,480
listener method again
8631
05:26:56,480 --> 05:26:58,718
this time we'll also listen for the
8632
05:26:58,718 --> 05:27:02,160
event of mouse out
8633
05:27:02,160 --> 05:27:04,718
let's create a function to change
8634
05:27:04,718 --> 05:27:06,958
green
8635
05:27:07,520 --> 05:27:10,878
function change green
8636
05:27:10,878 --> 05:27:13,600
background color equals light
8637
05:27:13,600 --> 05:27:14,558
green
8638
05:27:14,558 --> 05:27:16,400
this development is listening for two
8639
05:27:16,400 --> 05:27:19,520
events mouse over and mouse out
8640
05:27:19,520 --> 05:27:21,360
using the ad event listener method it's
8641
05:27:21,360 --> 05:27:23,600
really easy to wait for multiple events
8642
05:27:23,600 --> 05:27:25,520
there's one more argument within the ad
8643
05:27:25,520 --> 05:27:27,360
event listener method
8644
05:27:27,360 --> 05:27:30,320
that is the use capture argument
8645
05:27:30,320 --> 05:27:31,920
suppose we have an element within
8646
05:27:31,920 --> 05:27:33,520
another element
8647
05:27:33,520 --> 05:27:35,360
i'll surround this inner development
8648
05:27:35,360 --> 05:27:37,520
with another div
8649
05:27:37,520 --> 05:27:40,080
div the id will be
8650
05:27:40,080 --> 05:27:42,558
outer div
8651
05:27:42,558 --> 05:27:46,320
be sure to enclose your inner div
8652
05:27:46,718 --> 05:27:51,480
i'll change the style of outer div
8653
05:27:54,000 --> 05:27:56,000
i'll get rid of the border
8654
05:27:56,000 --> 05:27:59,680
the width and the height will be 200.
8655
05:27:59,680 --> 05:28:01,440
save everything
8656
05:28:01,440 --> 05:28:04,320
so we have an outer div and an inner div
8657
05:28:04,320 --> 05:28:06,240
the inner development is inside of the
8658
05:28:06,240 --> 05:28:08,000
outer div element what would happen
8659
05:28:08,000 --> 05:28:09,840
exactly if both of these elements are
8660
05:28:09,840 --> 05:28:12,240
waiting for the same event mouse over
8661
05:28:12,240 --> 05:28:14,400
which event would be handled first the
8662
05:28:14,400 --> 05:28:16,558
inner element or the outer element well
8663
05:28:16,558 --> 05:28:18,718
we can set that with the third argument
8664
05:28:18,718 --> 05:28:21,120
the use capture argument let's add an
8665
05:28:21,120 --> 05:28:23,280
event to the outer div i'll store this
8666
05:28:23,280 --> 05:28:26,000
within a variable
8667
05:28:26,000 --> 05:28:27,360
outer div
8668
05:28:27,360 --> 05:28:30,878
the id is outer div i'll eliminate all
8669
05:28:30,878 --> 05:28:32,480
of this
8670
05:28:32,480 --> 05:28:34,320
i'll add an event listener to the inner
8671
05:28:34,320 --> 05:28:35,360
div
8672
05:28:35,360 --> 05:28:37,200
add event
8673
05:28:37,200 --> 05:28:39,120
listener
8674
05:28:39,120 --> 05:28:42,638
the event this time will be click
8675
05:28:43,280 --> 05:28:46,160
the function will be change
8676
05:28:46,160 --> 05:28:47,680
blue this time
8677
05:28:47,680 --> 05:28:49,520
we'll add the same event to the outer
8678
05:28:49,520 --> 05:28:50,718
div
8679
05:28:50,718 --> 05:28:51,760
outer
8680
05:28:51,760 --> 05:28:53,040
div
8681
05:28:53,040 --> 05:28:55,440
add event listener click change blue
8682
05:28:55,440 --> 05:28:58,400
let's define this function change blue
8683
05:28:58,400 --> 05:29:01,440
function change blue
8684
05:29:01,440 --> 05:29:03,360
we will take this
8685
05:29:03,360 --> 05:29:08,320
dot style dot background color
8686
05:29:08,558 --> 05:29:09,760
equals
8687
05:29:09,760 --> 05:29:10,798
light
8688
05:29:10,798 --> 05:29:12,400
blue
8689
05:29:12,400 --> 05:29:13,760
both of these div elements are doing the
8690
05:29:13,760 --> 05:29:15,040
same thing
8691
05:29:15,040 --> 05:29:16,480
but there's some overlap
8692
05:29:16,480 --> 05:29:18,080
the inner div element and the outer
8693
05:29:18,080 --> 05:29:20,480
development are within the same space so
8694
05:29:20,480 --> 05:29:22,240
when i click on the inner div element
8695
05:29:22,240 --> 05:29:23,760
we're also clicking on the outer div
8696
05:29:23,760 --> 05:29:26,000
element at the same time which element
8697
05:29:26,000 --> 05:29:27,920
handles its event first the inner
8698
05:29:27,920 --> 05:29:29,920
element or the outer element just to
8699
05:29:29,920 --> 05:29:31,520
test this within the change blue
8700
05:29:31,520 --> 05:29:33,680
function let's create an alert just to
8701
05:29:33,680 --> 05:29:36,000
slow things down
8702
05:29:36,000 --> 05:29:36,878
you
8703
05:29:36,878 --> 05:29:38,400
selected
8704
05:29:38,400 --> 05:29:40,798
then let's display the id of the element
8705
05:29:40,798 --> 05:29:42,718
that is being handled
8706
05:29:42,718 --> 05:29:44,958
this dot id
8707
05:29:44,958 --> 05:29:47,680
okay so when i click on this element
8708
05:29:47,680 --> 05:29:49,840
we're handling the inner div first
8709
05:29:49,840 --> 05:29:51,680
before the outer div
8710
05:29:51,680 --> 05:29:53,920
but with the outer element if you pass
8711
05:29:53,920 --> 05:29:57,200
in another argument true for use capture
8712
05:29:57,200 --> 05:29:59,280
well then we will handle the outer
8713
05:29:59,280 --> 05:30:02,400
element first before the inner element
8714
05:30:02,400 --> 05:30:04,160
so that's what that third argument is
8715
05:30:04,160 --> 05:30:06,400
use capture if two elements are taking
8716
05:30:06,400 --> 05:30:08,320
the same space and they're listening for
8717
05:30:08,320 --> 05:30:10,320
the same event you could specify which
8718
05:30:10,320 --> 05:30:12,638
one has preference yeah that's the add
8719
05:30:12,638 --> 05:30:14,798
event listener method it's the preferred
8720
05:30:14,798 --> 05:30:16,718
way of having an element listen for
8721
05:30:16,718 --> 05:30:18,718
events as arguments you can pass in an
8722
05:30:18,718 --> 05:30:21,600
event a function to be executed and a
8723
05:30:21,600 --> 05:30:23,120
preference if you would like an outer
8724
05:30:23,120 --> 05:30:25,360
element to be handled first so yeah
8725
05:30:25,360 --> 05:30:27,040
that's the add event listener method
8726
05:30:27,040 --> 05:30:29,440
everybody
8727
05:30:29,440 --> 05:30:31,120
hey everybody in this video i'm gonna
8728
05:30:31,120 --> 05:30:33,200
demonstrate how we can show and hide
8729
05:30:33,200 --> 05:30:36,000
html elements for this demonstration i
8730
05:30:36,000 --> 05:30:38,000
recommend downloading a picture here's a
8731
05:30:38,000 --> 05:30:39,840
picture of a card that i found place it
8732
05:30:39,840 --> 05:30:41,120
within the same folder as your
8733
05:30:41,120 --> 05:30:43,280
javascript file when i click on a button
8734
05:30:43,280 --> 05:30:45,280
i would like to toggle between showing
8735
05:30:45,280 --> 05:30:48,000
and hiding that image heading to my html
8736
05:30:48,000 --> 05:30:50,080
file let's create a button
8737
05:30:50,080 --> 05:30:51,040
button
8738
05:30:51,040 --> 05:30:54,400
id equals my button
8739
05:30:54,400 --> 05:30:57,440
i'll add some text toggle is fine
8740
05:30:57,440 --> 05:30:59,440
we will add our image
8741
05:30:59,440 --> 05:31:04,000
image id equals my image
8742
05:31:04,000 --> 05:31:06,320
set the source equal to where your file
8743
05:31:06,320 --> 05:31:08,558
is located this image is in the same
8744
05:31:08,558 --> 05:31:11,200
folder as my html file i just need the
8745
05:31:11,200 --> 05:31:14,240
file name my file name is card.jpg but
8746
05:31:14,240 --> 05:31:15,440
it's probably going to be something
8747
05:31:15,440 --> 05:31:16,878
different for you
8748
05:31:16,878 --> 05:31:19,840
car dot jpeg let's change the size it's
8749
05:31:19,840 --> 05:31:20,840
a little
8750
05:31:20,840 --> 05:31:23,600
massive heading to my style sheet i will
8751
05:31:23,600 --> 05:31:24,718
select
8752
05:31:24,718 --> 05:31:26,160
my image
8753
05:31:26,160 --> 05:31:28,480
set the width to something reasonable
8754
05:31:28,480 --> 05:31:30,480
like 300 pixels
8755
05:31:30,480 --> 05:31:32,480
that's a lot better at the bottom i'll
8756
05:31:32,480 --> 05:31:34,558
add some text a paragraph element is
8757
05:31:34,558 --> 05:31:36,080
fine
8758
05:31:36,080 --> 05:31:38,798
press the button
8759
05:31:38,798 --> 05:31:40,878
heading to my javascript file i'll
8760
05:31:40,878 --> 05:31:43,600
select my button and my image
8761
05:31:43,600 --> 05:31:45,440
const my
8762
05:31:45,440 --> 05:31:46,400
button
8763
05:31:46,400 --> 05:31:49,440
equals document dot
8764
05:31:49,440 --> 05:31:50,480
query
8765
05:31:50,480 --> 05:31:52,000
selector
8766
05:31:52,000 --> 05:31:54,160
i'm selecting
8767
05:31:54,160 --> 05:31:56,240
my button
8768
05:31:56,240 --> 05:31:59,520
do the same thing with image
8769
05:32:01,920 --> 05:32:05,440
my image
8770
05:32:05,440 --> 05:32:07,200
next i'll add an event listener to this
8771
05:32:07,200 --> 05:32:08,080
button
8772
05:32:08,080 --> 05:32:10,798
my button dot add
8773
05:32:10,798 --> 05:32:13,440
event listener
8774
05:32:13,440 --> 05:32:15,600
when we click on this button we're going
8775
05:32:15,600 --> 05:32:17,920
to perform a function i'll use an arrow
8776
05:32:17,920 --> 05:32:20,000
function expression
8777
05:32:20,000 --> 05:32:21,760
what we'll do is check the display
8778
05:32:21,760 --> 05:32:23,600
property of this image
8779
05:32:23,600 --> 05:32:26,080
i'll use an if statement
8780
05:32:26,080 --> 05:32:27,280
if
8781
05:32:27,280 --> 05:32:28,878
my image
8782
05:32:28,878 --> 05:32:30,320
dot style
8783
05:32:30,320 --> 05:32:35,040
dot display is equal to none
8784
05:32:35,040 --> 05:32:37,680
none means that this image is hidden
8785
05:32:37,680 --> 05:32:39,360
if it's block that means it's being
8786
05:32:39,360 --> 05:32:42,160
displayed if this image is hidden my
8787
05:32:42,160 --> 05:32:45,280
image dot style dot display
8788
05:32:45,280 --> 05:32:47,760
equals block
8789
05:32:47,760 --> 05:32:49,840
this will display my image
8790
05:32:49,840 --> 05:32:52,240
else let's hide our image
8791
05:32:52,240 --> 05:32:55,360
my image dot style dot display
8792
05:32:55,360 --> 05:32:56,558
equals
8793
05:32:56,558 --> 05:32:58,400
none
8794
05:32:58,400 --> 05:33:00,480
when i click on this button it hides my
8795
05:33:00,480 --> 05:33:02,400
image when i click on it again it
8796
05:33:02,400 --> 05:33:04,958
displays my image now it's hidden now
8797
05:33:04,958 --> 05:33:06,878
it's showing hidden showing hidden
8798
05:33:06,878 --> 05:33:08,480
showing there's one issue with this
8799
05:33:08,480 --> 05:33:10,480
though perhaps i would like this image
8800
05:33:10,480 --> 05:33:12,638
to be hidden from the beginning then we
8801
05:33:12,638 --> 05:33:15,040
display it to do that we can go to our
8802
05:33:15,040 --> 05:33:17,440
style sheet then set the display
8803
05:33:17,440 --> 05:33:20,718
property to none
8804
05:33:20,718 --> 05:33:22,958
now check this out when i click on this
8805
05:33:22,958 --> 05:33:25,040
button the first time it doesn't do
8806
05:33:25,040 --> 05:33:27,360
anything until i click on it a second
8807
05:33:27,360 --> 05:33:29,520
time then it performs normally the
8808
05:33:29,520 --> 05:33:31,120
reason that this is happening is that
8809
05:33:31,120 --> 05:33:33,120
the style from our style sheet is not
8810
05:33:33,120 --> 05:33:35,280
ready yet to demonstrate that i'm going
8811
05:33:35,280 --> 05:33:40,240
to console.log the display of my image
8812
05:33:40,958 --> 05:33:42,878
when i click on this button well there's
8813
05:33:42,878 --> 05:33:45,200
no value within my display
8814
05:33:45,200 --> 05:33:48,400
if the display of my image is equal to
8815
05:33:48,400 --> 05:33:51,120
the property none then we show the image
8816
05:33:51,120 --> 05:33:53,360
but this is technically null and not the
8817
05:33:53,360 --> 05:33:55,600
property none therefore we will hide
8818
05:33:55,600 --> 05:33:58,320
this image which it already is a quick
8819
05:33:58,320 --> 05:34:00,240
fix for that is that we can add some
8820
05:34:00,240 --> 05:34:02,480
inline styling to this element i'll add
8821
05:34:02,480 --> 05:34:04,718
this display property inline instead of
8822
05:34:04,718 --> 05:34:07,680
externally find your image tag set the
8823
05:34:07,680 --> 05:34:10,400
style attribute equal to that css
8824
05:34:10,400 --> 05:34:12,320
property
8825
05:34:12,320 --> 05:34:14,558
this should work now i will click once
8826
05:34:14,558 --> 05:34:18,080
to display my image then hide it display
8827
05:34:18,080 --> 05:34:19,840
hide it another option is that we can
8828
05:34:19,840 --> 05:34:22,240
set the visibility of this image when we
8829
05:34:22,240 --> 05:34:24,080
toggle the display property all the
8830
05:34:24,080 --> 05:34:25,600
elements that are underneath are going
8831
05:34:25,600 --> 05:34:27,840
to shift downwards but if we were to use
8832
05:34:27,840 --> 05:34:30,400
visibility this space will be reserved
8833
05:34:30,400 --> 05:34:35,120
let's replace display with visibility
8834
05:34:36,400 --> 05:34:40,958
in place of none we will check hidden
8835
05:34:42,558 --> 05:34:46,878
in place of block this will be visible
8836
05:34:46,878 --> 05:34:49,520
within the inline styling let's set
8837
05:34:49,520 --> 05:34:51,120
display to
8838
05:34:51,120 --> 05:34:53,440
visibility
8839
05:34:53,440 --> 05:34:56,958
our visibility will be hidden
8840
05:34:56,958 --> 05:34:59,360
make sure to save everything
8841
05:34:59,360 --> 05:35:01,280
all of the space is reserved for that
8842
05:35:01,280 --> 05:35:03,440
image you can see that my text down at
8843
05:35:03,440 --> 05:35:06,080
the bottom is way down here when i show
8844
05:35:06,080 --> 05:35:08,320
and hide this image there's no shifting
8845
05:35:08,320 --> 05:35:11,120
of elements this space is reserved so
8846
05:35:11,120 --> 05:35:13,200
that's another option too you can always
8847
05:35:13,200 --> 05:35:15,760
toggle the visibility of an element so
8848
05:35:15,760 --> 05:35:17,920
yeah everybody that is how to show and
8849
05:35:17,920 --> 05:35:20,160
hide html elements if you would like a
8850
05:35:20,160 --> 05:35:22,000
copy of this code i'll post all of this
8851
05:35:22,000 --> 05:35:23,760
in the comment section down below and
8852
05:35:23,760 --> 05:35:25,600
well yeah that's how to show and hide
8853
05:35:25,600 --> 05:35:29,280
html elements using javascript
8854
05:35:29,280 --> 05:35:31,040
hey yeah everybody in this video i'm
8855
05:35:31,040 --> 05:35:32,798
going to show you how we can detect key
8856
05:35:32,798 --> 05:35:34,798
presses to do that we can add an event
8857
05:35:34,798 --> 05:35:36,718
listener to our window
8858
05:35:36,718 --> 05:35:39,440
window dot add
8859
05:35:39,440 --> 05:35:40,878
event
8860
05:35:40,878 --> 05:35:42,240
listener
8861
05:35:42,240 --> 05:35:44,798
the first argument is the attribute
8862
05:35:44,798 --> 05:35:47,760
let's detect any key down events
8863
05:35:47,760 --> 05:35:50,480
second is a callback a function
8864
05:35:50,480 --> 05:35:52,240
expression or an arrow function
8865
05:35:52,240 --> 05:35:54,320
expression let's use an arrow function
8866
05:35:54,320 --> 05:35:56,958
expression there is one argument
8867
05:35:56,958 --> 05:35:58,160
event
8868
05:35:58,160 --> 05:36:00,638
arrow then what would we like to do when
8869
05:36:00,638 --> 05:36:02,878
we press down on a key let's display
8870
05:36:02,878 --> 05:36:04,638
what key was pressed
8871
05:36:04,638 --> 05:36:07,040
console.log
8872
05:36:07,040 --> 05:36:08,320
event
8873
05:36:08,320 --> 05:36:12,160
dot key and that's it let's test it
8874
05:36:12,160 --> 05:36:13,920
press some random keys
8875
05:36:13,920 --> 05:36:18,798
q w e r t y a s d
8876
05:36:18,798 --> 05:36:22,320
enter backspace one two three the arrow
8877
05:36:22,320 --> 05:36:25,680
keys would be up down left right okay we
8878
05:36:25,680 --> 05:36:27,600
know that that works let's have some fun
8879
05:36:27,600 --> 05:36:29,520
with this let's create a box within our
8880
05:36:29,520 --> 05:36:31,680
window and we can move the box with key
8881
05:36:31,680 --> 05:36:33,840
events heading to our html file let's
8882
05:36:33,840 --> 05:36:35,520
create a div tag pretend this wasn't
8883
05:36:35,520 --> 05:36:36,558
here
8884
05:36:36,558 --> 05:36:40,000
div id equals my div
8885
05:36:40,000 --> 05:36:41,200
close it
8886
05:36:41,200 --> 05:36:43,360
let's style it
8887
05:36:43,360 --> 05:36:45,520
my div
8888
05:36:45,520 --> 05:36:48,798
will set the background color
8889
05:36:48,798 --> 05:36:51,280
to whatever color you would like
8890
05:36:51,280 --> 05:36:52,958
set a width
8891
05:36:52,958 --> 05:36:55,200
100 pixels is good
8892
05:36:55,200 --> 05:36:57,200
a height
8893
05:36:57,200 --> 05:36:58,878
100
8894
05:36:58,878 --> 05:37:00,798
you could add a border if you would like
8895
05:37:00,798 --> 05:37:03,920
border one pixel solid
8896
05:37:03,920 --> 05:37:06,798
and we'll want to set a position
8897
05:37:06,798 --> 05:37:09,040
position
8898
05:37:09,040 --> 05:37:12,798
either absolute or relative
8899
05:37:12,798 --> 05:37:15,200
we'll move our box relative to the body
8900
05:37:15,200 --> 05:37:18,240
that it's in okay save everything
8901
05:37:18,240 --> 05:37:21,040
i'm going to select our development
8902
05:37:21,040 --> 05:37:22,160
const
8903
05:37:22,160 --> 05:37:23,600
my div
8904
05:37:23,600 --> 05:37:26,480
equals document dot
8905
05:37:26,480 --> 05:37:27,600
get
8906
05:37:27,600 --> 05:37:30,798
element by id
8907
05:37:30,798 --> 05:37:32,958
my div
8908
05:37:32,958 --> 05:37:34,638
and we'll add an event listener to our
8909
05:37:34,638 --> 05:37:36,160
window
8910
05:37:36,160 --> 05:37:38,080
in place of an arrow function expression
8911
05:37:38,080 --> 05:37:41,440
let's pass a callback as an argument
8912
05:37:41,440 --> 05:37:44,798
let's pass a callback to a move function
8913
05:37:44,798 --> 05:37:47,520
and we'll need to define that function
8914
05:37:47,520 --> 05:37:48,958
move
8915
05:37:48,958 --> 05:37:50,638
there's going to be one argument that's
8916
05:37:50,638 --> 05:37:52,878
provided for us within the parameters of
8917
05:37:52,878 --> 05:37:55,760
the move function let's add event this
8918
05:37:55,760 --> 05:37:58,080
argument is provided for us i think the
8919
05:37:58,080 --> 05:38:00,320
best way to detect certain keystrokes
8920
05:38:00,320 --> 05:38:02,558
would be with a switch let's detect
8921
05:38:02,558 --> 05:38:05,920
arrow down up left right switch
8922
05:38:05,920 --> 05:38:08,798
we're examining event dot key for any
8923
05:38:08,798 --> 05:38:10,160
matching cases
8924
05:38:10,160 --> 05:38:13,360
the first case will be arrow down
8925
05:38:13,360 --> 05:38:14,480
arrow
8926
05:38:14,480 --> 05:38:15,920
down
8927
05:38:15,920 --> 05:38:17,200
we'll have to keep track of some
8928
05:38:17,200 --> 05:38:18,878
coordinates of where our div element
8929
05:38:18,878 --> 05:38:21,440
currently is let's create variables x
8930
05:38:21,440 --> 05:38:22,400
and y
8931
05:38:22,400 --> 05:38:25,520
let x equal zero
8932
05:38:25,520 --> 05:38:28,558
let y equal zero as well whenever we
8933
05:38:28,558 --> 05:38:31,840
press down let's move y by maybe five
8934
05:38:31,840 --> 05:38:33,920
pixels down y
8935
05:38:33,920 --> 05:38:37,600
plus equals five then we will take my
8936
05:38:37,600 --> 05:38:38,958
div
8937
05:38:38,958 --> 05:38:40,798
take the style
8938
05:38:40,798 --> 05:38:43,120
access the top property
8939
05:38:43,120 --> 05:38:46,000
and set the sequel to whatever y is
8940
05:38:46,000 --> 05:38:48,958
plus pixels
8941
05:38:49,280 --> 05:38:51,840
then at the end of our case let's break
8942
05:38:51,840 --> 05:38:53,360
so when we save
8943
05:38:53,360 --> 05:38:55,520
by pressing down we should be able to
8944
05:38:55,520 --> 05:38:56,878
move down
8945
05:38:56,878 --> 05:38:59,440
yeah there we go let's do the same thing
8946
05:38:59,440 --> 05:39:02,558
with the other arrow keys
8947
05:39:02,958 --> 05:39:06,400
arrow up we will decrement y
8948
05:39:06,400 --> 05:39:09,440
so we should be able to move down
8949
05:39:09,440 --> 05:39:11,920
and up
8950
05:39:13,760 --> 05:39:17,718
arrow right
8951
05:39:18,320 --> 05:39:20,558
x plus equals five
8952
05:39:20,558 --> 05:39:24,160
my div dot style dot left
8953
05:39:24,160 --> 05:39:26,878
equals x plus pixels
8954
05:39:26,878 --> 05:39:31,080
now we should be able to go right
8955
05:39:32,798 --> 05:39:33,760
arrow
8956
05:39:33,760 --> 05:39:35,520
left
8957
05:39:35,520 --> 05:39:36,320
x
8958
05:39:36,320 --> 05:39:38,798
minus equals five
8959
05:39:38,798 --> 05:39:40,878
now we can go right
8960
05:39:40,878 --> 05:39:42,240
down
8961
05:39:42,240 --> 05:39:43,360
left
8962
05:39:43,360 --> 05:39:45,520
and up
8963
05:39:45,520 --> 05:39:47,440
then you could add a default case too if
8964
05:39:47,440 --> 05:39:50,638
you would like default
8965
05:39:50,638 --> 05:39:52,080
break
8966
05:39:52,080 --> 05:39:54,320
so yeah that's how to detect key presses
8967
05:39:54,320 --> 05:39:56,480
everybody you can add an event listener
8968
05:39:56,480 --> 05:39:58,718
to your window the event attribute is
8969
05:39:58,718 --> 05:40:00,638
key down you could also switch this to
8970
05:40:00,638 --> 05:40:03,040
key up when you let go of a key then it
8971
05:40:03,040 --> 05:40:05,040
triggers the event the second argument
8972
05:40:05,040 --> 05:40:07,760
is a callback a function expression or
8973
05:40:07,760 --> 05:40:09,840
an arrow function expression so yeah
8974
05:40:09,840 --> 05:40:12,160
that's one way to detect key presses if
8975
05:40:12,160 --> 05:40:13,920
you would like a copy of this code i'll
8976
05:40:13,920 --> 05:40:15,360
post this in the comments section down
8977
05:40:15,360 --> 05:40:18,000
below and well yeah that's a very basic
8978
05:40:18,000 --> 05:40:22,558
way to detect key presses in javascript
8979
05:40:22,558 --> 05:40:24,400
hey guys in this video i'm going to show
8980
05:40:24,400 --> 05:40:26,160
you how we can create a few simple
8981
05:40:26,160 --> 05:40:28,160
animations using javascript we'll create
8982
05:40:28,160 --> 05:40:29,920
a button and a development heading to
8983
05:40:29,920 --> 05:40:32,798
our html file let's create a button
8984
05:40:32,798 --> 05:40:35,120
i'll give this an id of
8985
05:40:35,120 --> 05:40:37,040
my button
8986
05:40:37,040 --> 05:40:39,360
i'll add text begin
8987
05:40:39,360 --> 05:40:42,080
this button will begin the animation
8988
05:40:42,080 --> 05:40:44,080
i'll create a development
8989
05:40:44,080 --> 05:40:47,680
id equals my div
8990
05:40:47,680 --> 05:40:49,840
let's style our development
8991
05:40:49,840 --> 05:40:51,520
my div
8992
05:40:51,520 --> 05:40:55,558
choose a background color
8993
05:40:56,240 --> 05:40:58,080
i'll pick light green
8994
05:40:58,080 --> 05:41:01,120
but choose whatever color you like
8995
05:41:01,120 --> 05:41:04,798
a width of 100 is good
8996
05:41:04,798 --> 05:41:07,920
a height of 100 is good as well
8997
05:41:07,920 --> 05:41:10,480
then set the position property to
8998
05:41:10,480 --> 05:41:12,160
relative
8999
05:41:12,160 --> 05:41:14,080
we'll position this element relative to
9000
05:41:14,080 --> 05:41:15,760
the container that it's in the body of
9001
05:41:15,760 --> 05:41:17,280
our document
9002
05:41:17,280 --> 05:41:19,760
okay save everything in our javascript
9003
05:41:19,760 --> 05:41:22,400
file let's store our button element
9004
05:41:22,400 --> 05:41:26,558
constant my button equals document dot
9005
05:41:26,558 --> 05:41:30,240
get element by id
9006
05:41:30,240 --> 05:41:31,920
my button
9007
05:41:31,920 --> 05:41:33,840
then let's get our development
9008
05:41:33,840 --> 05:41:37,040
i'll name this my animation
9009
05:41:37,040 --> 05:41:39,360
get element by id
9010
05:41:39,360 --> 05:41:40,400
my
9011
05:41:40,400 --> 05:41:41,360
div
9012
05:41:41,360 --> 05:41:43,040
when we press on the button we'll begin
9013
05:41:43,040 --> 05:41:44,480
an animation
9014
05:41:44,480 --> 05:41:46,160
we'll want to add an event listener to
9015
05:41:46,160 --> 05:41:47,920
the button
9016
05:41:47,920 --> 05:41:50,240
my button dot
9017
05:41:50,240 --> 05:41:54,718
add event listener
9018
05:41:55,040 --> 05:41:58,320
the event will be click
9019
05:41:58,798 --> 05:42:01,760
then we will pass a call back begin
9020
05:42:01,760 --> 05:42:03,760
let's define this function
9021
05:42:03,760 --> 05:42:04,840
function
9022
05:42:04,840 --> 05:42:06,480
begin
9023
05:42:06,480 --> 05:42:08,638
what would we like to do i'm going to
9024
05:42:08,638 --> 05:42:10,958
declare a timer id we'll use the set
9025
05:42:10,958 --> 05:42:12,320
interval method
9026
05:42:12,320 --> 05:42:13,680
let
9027
05:42:13,680 --> 05:42:16,638
timer id i'll set the sequel to null for
9028
05:42:16,638 --> 05:42:18,638
now we'll need some coordinates to keep
9029
05:42:18,638 --> 05:42:20,958
track of our position of our div element
9030
05:42:20,958 --> 05:42:23,680
let x i'll set this equal to zero
9031
05:42:23,680 --> 05:42:25,040
let y
9032
05:42:25,040 --> 05:42:26,718
equal zero as well
9033
05:42:26,718 --> 05:42:30,160
we will use the set interval method
9034
05:42:30,160 --> 05:42:32,638
passing a callback to a function
9035
05:42:32,638 --> 05:42:34,400
we'll name this frame
9036
05:42:34,400 --> 05:42:36,240
after how many milliseconds would we
9037
05:42:36,240 --> 05:42:38,320
like to invoke this function
9038
05:42:38,320 --> 05:42:40,878
what about every five milliseconds
9039
05:42:40,878 --> 05:42:42,320
that's a good speed
9040
05:42:42,320 --> 05:42:44,718
set interval returns an id of the timer
9041
05:42:44,718 --> 05:42:45,840
that we're using
9042
05:42:45,840 --> 05:42:48,878
so let's assign this to timer id
9043
05:42:48,878 --> 05:42:50,798
we will use this timer id to stop the
9044
05:42:50,798 --> 05:42:52,638
animation when it's completed we're
9045
05:42:52,638 --> 05:42:54,638
going to create an inner function
9046
05:42:54,638 --> 05:42:57,600
function frame
9047
05:42:57,600 --> 05:42:59,280
this inner frame function will be in
9048
05:42:59,280 --> 05:43:01,200
charge of updating our div
9049
05:43:01,200 --> 05:43:03,280
every five milliseconds
9050
05:43:03,280 --> 05:43:05,280
but when would we like to stop let's
9051
05:43:05,280 --> 05:43:08,558
slide our div element to maybe like 300
9052
05:43:08,558 --> 05:43:10,718
pixels out so i'm going to write an if
9053
05:43:10,718 --> 05:43:12,400
statement
9054
05:43:12,400 --> 05:43:13,520
if
9055
05:43:13,520 --> 05:43:14,320
x
9056
05:43:14,320 --> 05:43:16,878
is greater than or equal to
9057
05:43:16,878 --> 05:43:18,638
200
9058
05:43:18,638 --> 05:43:21,360
then we will stop set interval we can do
9059
05:43:21,360 --> 05:43:23,840
that by using the clear
9060
05:43:23,840 --> 05:43:25,280
interval method
9061
05:43:25,280 --> 05:43:27,520
pass in our timer id
9062
05:43:27,520 --> 05:43:29,760
this will stop the animation
9063
05:43:29,760 --> 05:43:31,600
else if we would like to advance one
9064
05:43:31,600 --> 05:43:32,638
frame
9065
05:43:32,638 --> 05:43:34,798
let's increment x by one
9066
05:43:34,798 --> 05:43:36,240
x plus
9067
05:43:36,240 --> 05:43:38,958
equals one then we just need to adjust
9068
05:43:38,958 --> 05:43:42,000
the left property of this development
9069
05:43:42,000 --> 05:43:45,040
we'll take myanimation
9070
05:43:45,040 --> 05:43:46,638
access the style
9071
05:43:46,638 --> 05:43:48,718
access the left property
9072
05:43:48,718 --> 05:43:51,760
set the sql to whatever x is currently
9073
05:43:51,760 --> 05:43:52,878
then add
9074
05:43:52,878 --> 05:43:54,958
pixels
9075
05:43:54,958 --> 05:43:56,798
so when we begin this animation it
9076
05:43:56,798 --> 05:43:59,200
should slide to the right
9077
05:43:59,200 --> 05:44:01,120
and stop right about here now let's move
9078
05:44:01,120 --> 05:44:03,520
this animation down instead of right
9079
05:44:03,520 --> 05:44:07,040
if y is greater than or equal to 200
9080
05:44:07,040 --> 05:44:09,520
y plus equals one
9081
05:44:09,520 --> 05:44:11,520
myanimation.style
9082
05:44:11,520 --> 05:44:15,680
dot top equals y plus pixels
9083
05:44:15,680 --> 05:44:18,080
now this animation moves down
9084
05:44:18,080 --> 05:44:20,160
we could combine them both if x is
9085
05:44:20,160 --> 05:44:23,120
greater than or equal to 200
9086
05:44:23,120 --> 05:44:24,000
or
9087
05:44:24,000 --> 05:44:27,920
y is greater than or equal to 200
9088
05:44:27,920 --> 05:44:30,160
then we will increment both x and y by
9089
05:44:30,160 --> 05:44:33,360
one x plus equals one
9090
05:44:33,360 --> 05:44:37,600
my animation dot style dot left equals x
9091
05:44:37,600 --> 05:44:40,718
plus pixels
9092
05:44:40,718 --> 05:44:42,718
now this animation moves diagonally
9093
05:44:42,718 --> 05:44:45,360
that's a simple slide animation let's
9094
05:44:45,360 --> 05:44:47,120
make this element rotate i think that
9095
05:44:47,120 --> 05:44:48,878
would be cool let's eliminate some of
9096
05:44:48,878 --> 05:44:50,320
these lines of code
9097
05:44:50,320 --> 05:44:52,080
i'll post all of this in the notes so
9098
05:44:52,080 --> 05:44:55,718
don't worry about that
9099
05:44:56,718 --> 05:45:00,400
okay we'll need a variable degrees
9100
05:45:00,400 --> 05:45:02,400
set this equal to zero
9101
05:45:02,400 --> 05:45:06,160
let's perform a full 360 degree rotation
9102
05:45:06,160 --> 05:45:07,120
if
9103
05:45:07,120 --> 05:45:11,360
degrees is greater than or equal to 360.
9104
05:45:11,360 --> 05:45:13,280
then we will stop our timer
9105
05:45:13,280 --> 05:45:15,600
else we will rotate by increasing
9106
05:45:15,600 --> 05:45:17,680
degrees by one or a different number if
9107
05:45:17,680 --> 05:45:20,160
you would like this to be faster
9108
05:45:20,160 --> 05:45:23,120
take my animation
9109
05:45:23,120 --> 05:45:25,760
dot style
9110
05:45:25,760 --> 05:45:28,320
dot transform
9111
05:45:28,320 --> 05:45:30,638
set the sequel to this string is going
9112
05:45:30,638 --> 05:45:32,400
to be a little bit awkward
9113
05:45:32,400 --> 05:45:34,400
let's rotate
9114
05:45:34,400 --> 05:45:36,240
x
9115
05:45:36,240 --> 05:45:38,400
parentheses we'll need to insert our
9116
05:45:38,400 --> 05:45:41,520
degrees within quotes
9117
05:45:42,878 --> 05:45:44,558
plus degrees
9118
05:45:44,558 --> 05:45:46,798
plus
9119
05:45:46,798 --> 05:45:48,080
d e g
9120
05:45:48,080 --> 05:45:49,680
okay and that should be it
9121
05:45:49,680 --> 05:45:52,000
so this will rotate my element on the
9122
05:45:52,000 --> 05:45:54,000
x-axis
9123
05:45:54,000 --> 05:45:57,680
and it does a full 360 degree turn
9124
05:45:57,680 --> 05:46:01,920
okay rotate on the y-axis
9125
05:46:02,160 --> 05:46:03,680
pretty cool right
9126
05:46:03,680 --> 05:46:06,160
or z
9127
05:46:08,638 --> 05:46:10,638
hey just for fun let's rotate this
9128
05:46:10,638 --> 05:46:12,160
element and have it move diagonally
9129
05:46:12,160 --> 05:46:14,000
across the screen this will just be
9130
05:46:14,000 --> 05:46:15,200
practice
9131
05:46:15,200 --> 05:46:17,280
let x equal zero
9132
05:46:17,280 --> 05:46:20,718
let y equal zero
9133
05:46:21,200 --> 05:46:23,600
we'll continue this as long as x is
9134
05:46:23,600 --> 05:46:27,600
greater than or equal to two hundred
9135
05:46:27,760 --> 05:46:30,240
or y is greater than or equal to two
9136
05:46:30,240 --> 05:46:33,240
hundred
9137
05:46:33,760 --> 05:46:36,000
increment x by one
9138
05:46:36,000 --> 05:46:39,280
increment y by one
9139
05:46:41,200 --> 05:46:44,718
my animation dot style
9140
05:46:44,718 --> 05:46:49,520
dot left equals x plus pixels
9141
05:46:52,200 --> 05:46:54,718
myanimation.style.top equals y plus
9142
05:46:54,718 --> 05:46:56,400
pixels
9143
05:46:56,400 --> 05:46:58,480
okay this should move diagonally and
9144
05:46:58,480 --> 05:47:00,480
rotate
9145
05:47:00,480 --> 05:47:02,000
okay let's make the spin a little bit
9146
05:47:02,000 --> 05:47:06,000
faster i'll increase degrees by two
9147
05:47:06,400 --> 05:47:09,040
or three
9148
05:47:09,840 --> 05:47:13,440
or a higher number like five
9149
05:47:13,680 --> 05:47:15,920
okay last one we can scale the size of
9150
05:47:15,920 --> 05:47:17,360
an image
9151
05:47:17,360 --> 05:47:18,878
so let's get rid of what we have written
9152
05:47:18,878 --> 05:47:21,360
currently
9153
05:47:23,040 --> 05:47:26,160
okay let's create two new variables
9154
05:47:26,160 --> 05:47:28,400
let scale x
9155
05:47:28,400 --> 05:47:29,600
equal
9156
05:47:29,600 --> 05:47:33,120
one one means one hundred percent
9157
05:47:33,120 --> 05:47:34,160
let
9158
05:47:34,160 --> 05:47:35,920
scale y
9159
05:47:35,920 --> 05:47:38,000
equal one as well
9160
05:47:38,000 --> 05:47:40,480
let's scale this image twice as big our
9161
05:47:40,480 --> 05:47:42,480
condition to stop is
9162
05:47:42,480 --> 05:47:43,600
if
9163
05:47:43,600 --> 05:47:45,520
scale x
9164
05:47:45,520 --> 05:47:48,160
is greater than or equal to two
9165
05:47:48,160 --> 05:47:49,600
else
9166
05:47:49,600 --> 05:47:50,718
scale
9167
05:47:50,718 --> 05:47:52,878
x plus equals
9168
05:47:52,878 --> 05:47:55,360
maybe 0.01
9169
05:47:55,360 --> 05:47:57,280
this would be one percent then we will
9170
05:47:57,280 --> 05:47:58,638
take my
9171
05:47:58,638 --> 05:48:00,160
animation
9172
05:48:00,160 --> 05:48:03,600
dot style dot transform
9173
05:48:03,600 --> 05:48:06,000
set the sequel to
9174
05:48:06,000 --> 05:48:08,160
scale
9175
05:48:08,160 --> 05:48:10,558
then within parenthesis
9176
05:48:10,558 --> 05:48:14,638
we'll need to insert scale x and scale y
9177
05:48:14,878 --> 05:48:16,080
plus
9178
05:48:16,080 --> 05:48:17,840
scale x
9179
05:48:17,840 --> 05:48:20,080
plus
9180
05:48:20,080 --> 05:48:20,958
comma
9181
05:48:20,958 --> 05:48:23,600
plus scale y
9182
05:48:23,600 --> 05:48:25,520
plus
9183
05:48:25,520 --> 05:48:27,520
and that should be good if we're only
9184
05:48:27,520 --> 05:48:30,000
scaling x upwards then this element will
9185
05:48:30,000 --> 05:48:32,320
expand horizontally
9186
05:48:32,320 --> 05:48:34,718
if we stop where scale x is three it
9187
05:48:34,718 --> 05:48:36,400
would expand to be three hundred percent
9188
05:48:36,400 --> 05:48:39,680
on the x-axis okay let's do this with y
9189
05:48:39,680 --> 05:48:42,638
scale y
9190
05:48:44,080 --> 05:48:47,840
y plus equals 0.01 this element will
9191
05:48:47,840 --> 05:48:50,400
expand on the y-axis
9192
05:48:50,400 --> 05:48:52,798
then let's do both if
9193
05:48:52,798 --> 05:48:54,400
scale x
9194
05:48:54,400 --> 05:48:57,360
is greater than or equal to 2
9195
05:48:57,360 --> 05:49:00,480
or scale y is greater than or equal to 2
9196
05:49:00,480 --> 05:49:03,360
then increase both scale x and scale y
9197
05:49:03,360 --> 05:49:05,280
then this element will expand in both
9198
05:49:05,280 --> 05:49:07,360
directions or we could shrink an element
9199
05:49:07,360 --> 05:49:10,160
by scaling down scale x is less than or
9200
05:49:10,160 --> 05:49:13,280
equal to what about 0.1
9201
05:49:13,280 --> 05:49:16,958
do the same thing with scale y
9202
05:49:17,280 --> 05:49:19,360
less than or equal to
9203
05:49:19,360 --> 05:49:21,280
0.1
9204
05:49:21,280 --> 05:49:24,878
then let's decrement scale x and scale y
9205
05:49:24,878 --> 05:49:28,080
this will cause an image to shrink
9206
05:49:28,080 --> 05:49:30,000
so yeah those are a few simple
9207
05:49:30,000 --> 05:49:32,160
animations you can do with javascript if
9208
05:49:32,160 --> 05:49:33,200
you would like the code that i've
9209
05:49:33,200 --> 05:49:34,638
written here i'll post this in the
9210
05:49:34,638 --> 05:49:36,558
comments section down below and well
9211
05:49:36,558 --> 05:49:38,718
yeah those are a few simple animations
9212
05:49:38,718 --> 05:49:41,680
with javascript
9213
05:49:41,840 --> 05:49:43,520
hey guys in this video we're going to
9214
05:49:43,520 --> 05:49:46,080
draw some simple shapes using the canvas
9215
05:49:46,080 --> 05:49:49,120
api it's a means for drawing graphics
9216
05:49:49,120 --> 05:49:51,600
it's useful for animations games and
9217
05:49:51,600 --> 05:49:53,920
data visualization heading to our html
9218
05:49:53,920 --> 05:49:55,600
document we'll need to create a canvas
9219
05:49:55,600 --> 05:49:57,120
element
9220
05:49:57,120 --> 05:49:58,240
canvas
9221
05:49:58,240 --> 05:49:59,680
close it
9222
05:49:59,680 --> 05:50:02,080
i'll give this an id
9223
05:50:02,080 --> 05:50:03,920
my canvas
9224
05:50:03,920 --> 05:50:05,760
and we should probably set a width and a
9225
05:50:05,760 --> 05:50:07,040
height
9226
05:50:07,040 --> 05:50:10,400
width equals maybe 500
9227
05:50:10,400 --> 05:50:13,440
and height equals 500 as well
9228
05:50:13,440 --> 05:50:15,680
let's save there's no apparent change
9229
05:50:15,680 --> 05:50:17,600
but we do have a canvas here to better
9230
05:50:17,600 --> 05:50:20,320
visualize it heading to our css file
9231
05:50:20,320 --> 05:50:22,000
make sure that you link it within your
9232
05:50:22,000 --> 05:50:23,600
html file
9233
05:50:23,600 --> 05:50:25,840
we can begin styling the canvas i will
9234
05:50:25,840 --> 05:50:28,798
select my canvas
9235
05:50:28,798 --> 05:50:31,040
i'll add a border
9236
05:50:31,040 --> 05:50:33,760
border one pixel
9237
05:50:33,760 --> 05:50:34,878
solid
9238
05:50:34,878 --> 05:50:36,240
black
9239
05:50:36,240 --> 05:50:38,320
yeah and there's our canvas
9240
05:50:38,320 --> 05:50:40,000
you can also change the background color
9241
05:50:40,000 --> 05:50:41,760
too
9242
05:50:41,760 --> 05:50:43,280
background
9243
05:50:43,280 --> 05:50:44,878
color
9244
05:50:44,878 --> 05:50:46,400
and pick a color
9245
05:50:46,400 --> 05:50:48,958
maybe sky blue
9246
05:50:48,958 --> 05:50:51,920
that's pretty nice so this is our canvas
9247
05:50:51,920 --> 05:50:53,840
but just for demonstration purposes i'm
9248
05:50:53,840 --> 05:50:56,000
going to turn this line into a comment
9249
05:50:56,000 --> 05:50:58,558
we can begin drawing some shapes
9250
05:50:58,558 --> 05:51:00,798
let's head to our javascript file
9251
05:51:00,798 --> 05:51:02,718
i'm going to store my canvas within a
9252
05:51:02,718 --> 05:51:04,240
workable variable
9253
05:51:04,240 --> 05:51:06,240
let canvas
9254
05:51:06,240 --> 05:51:07,440
equal
9255
05:51:07,440 --> 05:51:10,080
document dot get
9256
05:51:10,080 --> 05:51:15,520
element by id the id is mycanvas
9257
05:51:15,520 --> 05:51:17,280
to draw on the canvas we need to get the
9258
05:51:17,280 --> 05:51:18,718
context
9259
05:51:18,718 --> 05:51:20,320
and i will store that within another
9260
05:51:20,320 --> 05:51:24,080
variable let context
9261
05:51:24,080 --> 05:51:25,680
equal
9262
05:51:25,680 --> 05:51:28,240
canvas dot get
9263
05:51:28,240 --> 05:51:30,400
context
9264
05:51:30,400 --> 05:51:32,160
and there's one argument
9265
05:51:32,160 --> 05:51:33,360
2d
9266
05:51:33,360 --> 05:51:36,080
imagine that context is a painting
9267
05:51:36,080 --> 05:51:38,878
within a picture frame and the canvas is
9268
05:51:38,878 --> 05:51:41,040
the picture and the frame together
9269
05:51:41,040 --> 05:51:43,280
that's how i imagine it at least to draw
9270
05:51:43,280 --> 05:51:44,878
on the canvas we need to draw on the
9271
05:51:44,878 --> 05:51:46,958
context the painting itself and not the
9272
05:51:46,958 --> 05:51:49,440
frame let's begin with a simple line i
9273
05:51:49,440 --> 05:51:51,360
would like to draw on the context to
9274
05:51:51,360 --> 05:51:53,440
begin drawing a line i will use the
9275
05:51:53,440 --> 05:51:54,558
begin
9276
05:51:54,558 --> 05:51:56,480
path method then we need starting
9277
05:51:56,480 --> 05:51:58,558
coordinates we'll begin in the top left
9278
05:51:58,558 --> 05:52:01,760
corner type context dot
9279
05:52:01,760 --> 05:52:02,638
move
9280
05:52:02,638 --> 05:52:04,000
2
9281
05:52:04,000 --> 05:52:06,160
then pass in a pair of coordinates the
9282
05:52:06,160 --> 05:52:08,240
top left corner of our canvas is zero
9283
05:52:08,240 --> 05:52:10,798
zero think of the move to method as if
9284
05:52:10,798 --> 05:52:12,958
we're placing our brush on the canvas
9285
05:52:12,958 --> 05:52:15,120
we're picking a starting position to
9286
05:52:15,120 --> 05:52:17,520
draw a line we have to use the line two
9287
05:52:17,520 --> 05:52:19,040
method
9288
05:52:19,040 --> 05:52:20,320
context
9289
05:52:20,320 --> 05:52:23,440
dot line two
9290
05:52:23,440 --> 05:52:25,920
then pass in another set of coordinates
9291
05:52:25,920 --> 05:52:27,920
so the bottom right corner over here
9292
05:52:27,920 --> 05:52:30,480
would be five hundred five hundred
9293
05:52:30,480 --> 05:52:32,558
then we will follow this with the stroke
9294
05:52:32,558 --> 05:52:33,600
method
9295
05:52:33,600 --> 05:52:37,040
context dot stroke
9296
05:52:37,040 --> 05:52:39,280
there and there's our line let's pick a
9297
05:52:39,280 --> 05:52:40,638
different set of coordinates what about
9298
05:52:40,638 --> 05:52:43,120
right in the middle so 250
9299
05:52:43,120 --> 05:52:45,120
250
9300
05:52:45,120 --> 05:52:46,480
you can continue this line in a
9301
05:52:46,480 --> 05:52:48,798
different direction just invoke the line
9302
05:52:48,798 --> 05:52:50,878
two method again
9303
05:52:50,878 --> 05:52:52,558
i'll take this line from the middle of
9304
05:52:52,558 --> 05:52:54,958
our canvas to the bottom now i'll follow
9305
05:52:54,958 --> 05:52:57,760
line two with another line two method
9306
05:52:57,760 --> 05:52:59,680
if i need to connect the line down here
9307
05:52:59,680 --> 05:53:02,878
i'll follow line two with two fifty
9308
05:53:02,878 --> 05:53:04,400
five 500
9309
05:53:04,400 --> 05:53:06,878
there so we have drawn two lines i would
9310
05:53:06,878 --> 05:53:08,718
like to draw a line from the top right
9311
05:53:08,718 --> 05:53:10,718
corner to the middle my brush is
9312
05:53:10,718 --> 05:53:12,558
currently down here i need to move this
9313
05:53:12,558 --> 05:53:14,558
brush to the top right corner
9314
05:53:14,558 --> 05:53:17,440
i will invoke the move two method
9315
05:53:17,440 --> 05:53:19,120
and the top right corner of my canvas
9316
05:53:19,120 --> 05:53:21,120
would have coordinates of
9317
05:53:21,120 --> 05:53:23,920
500 0.
9318
05:53:23,920 --> 05:53:27,040
then again invoke the line 2 method
9319
05:53:27,040 --> 05:53:31,798
and the middle is 250 250.
9320
05:53:32,160 --> 05:53:34,240
cool we have drawn some lines
9321
05:53:34,240 --> 05:53:36,718
we can set a line width currently the
9322
05:53:36,718 --> 05:53:38,718
width is one pixel
9323
05:53:38,718 --> 05:53:41,680
context dot line
9324
05:53:41,680 --> 05:53:42,878
width
9325
05:53:42,878 --> 05:53:45,680
equals what about 10
9326
05:53:45,680 --> 05:53:48,160
so we have some extra thick lines
9327
05:53:48,160 --> 05:53:51,440
you can also change the stroke color
9328
05:53:51,440 --> 05:53:54,080
context dot stroke
9329
05:53:54,080 --> 05:53:55,120
style
9330
05:53:55,120 --> 05:53:57,600
equals and pick a color this can be a
9331
05:53:57,600 --> 05:54:00,638
color name rgb values or hexadecimal
9332
05:54:00,638 --> 05:54:01,760
values
9333
05:54:01,760 --> 05:54:03,360
i'll say just purple
9334
05:54:03,360 --> 05:54:04,718
i like purple
9335
05:54:04,718 --> 05:54:06,878
we have some purple lines now that's how
9336
05:54:06,878 --> 05:54:09,520
to draw some lines on our canvas draw
9337
05:54:09,520 --> 05:54:11,360
lines this time we're going to create a
9338
05:54:11,360 --> 05:54:13,680
triangle to create a triangle we have to
9339
05:54:13,680 --> 05:54:16,080
use the line two method
9340
05:54:16,080 --> 05:54:21,040
we'll type context dot begin path
9341
05:54:21,040 --> 05:54:25,520
and end this with context dot stroke
9342
05:54:25,520 --> 05:54:27,360
we'll need to place our brush somewhere
9343
05:54:27,360 --> 05:54:29,440
maybe the top middle
9344
05:54:29,440 --> 05:54:31,520
to place our brush down we will use the
9345
05:54:31,520 --> 05:54:33,840
move to method context
9346
05:54:33,840 --> 05:54:34,878
move
9347
05:54:34,878 --> 05:54:36,080
two
9348
05:54:36,080 --> 05:54:37,920
so the coordinates of the top middle
9349
05:54:37,920 --> 05:54:39,280
would be
9350
05:54:39,280 --> 05:54:40,840
250
9351
05:54:40,840 --> 05:54:43,520
zero let's draw a line to the middle of
9352
05:54:43,520 --> 05:54:45,920
the left border
9353
05:54:45,920 --> 05:54:47,280
context
9354
05:54:47,280 --> 05:54:48,480
line
9355
05:54:48,480 --> 05:54:49,680
two
9356
05:54:49,680 --> 05:54:51,200
that would be
9357
05:54:51,200 --> 05:54:54,638
zero two fifty
9358
05:54:54,718 --> 05:54:57,200
let's draw another line
9359
05:54:57,200 --> 05:54:58,718
line two
9360
05:54:58,718 --> 05:55:00,638
maybe the opposite side
9361
05:55:00,638 --> 05:55:02,958
that would be 500
9362
05:55:02,958 --> 05:55:04,798
250
9363
05:55:04,798 --> 05:55:06,638
and another line
9364
05:55:06,638 --> 05:55:09,840
let's bring it back to the top
9365
05:55:09,840 --> 05:55:11,840
is 250
9366
05:55:11,840 --> 05:55:13,600
0.
9367
05:55:13,600 --> 05:55:15,840
there we have a triangle there is a fill
9368
05:55:15,840 --> 05:55:18,400
method to fill this triangle
9369
05:55:18,400 --> 05:55:20,798
in place of stroke you can use fill you
9370
05:55:20,798 --> 05:55:22,638
can pick a fill color
9371
05:55:22,638 --> 05:55:24,240
maybe you want more options than just
9372
05:55:24,240 --> 05:55:25,920
black
9373
05:55:25,920 --> 05:55:28,000
so that would be context
9374
05:55:28,000 --> 05:55:30,000
dot fill
9375
05:55:30,000 --> 05:55:32,638
style and pick a color
9376
05:55:32,638 --> 05:55:35,440
maybe yellow
9377
05:55:36,400 --> 05:55:38,400
if you need a border you can use the
9378
05:55:38,400 --> 05:55:41,200
stroke method along with the fill method
9379
05:55:41,200 --> 05:55:42,638
there we have a border around our
9380
05:55:42,638 --> 05:55:44,160
triangle
9381
05:55:44,160 --> 05:55:46,320
we can increase the border width
9382
05:55:46,320 --> 05:55:49,760
technically that's the stroke width
9383
05:55:49,760 --> 05:55:52,718
context dot line
9384
05:55:52,718 --> 05:55:53,680
width
9385
05:55:53,680 --> 05:55:57,200
equals maybe five
9386
05:55:57,200 --> 05:56:00,000
let's change that to maybe ten
9387
05:56:00,000 --> 05:56:02,320
there that's better to change the color
9388
05:56:02,320 --> 05:56:04,240
of the border all you have to do is set
9389
05:56:04,240 --> 05:56:08,638
the stroke style context dot stroke
9390
05:56:08,638 --> 05:56:10,080
style
9391
05:56:10,080 --> 05:56:11,360
equals
9392
05:56:11,360 --> 05:56:13,600
currently it's black maybe we can pick i
9393
05:56:13,600 --> 05:56:15,600
don't know gray
9394
05:56:15,600 --> 05:56:17,600
there we have a gray border
9395
05:56:17,600 --> 05:56:19,680
so that's how to draw a triangle next
9396
05:56:19,680 --> 05:56:21,760
we'll move to rectangles
9397
05:56:21,760 --> 05:56:23,680
to draw an empty rectangle we will use
9398
05:56:23,680 --> 05:56:26,000
the stroke rect method
9399
05:56:26,000 --> 05:56:27,840
context dot
9400
05:56:27,840 --> 05:56:28,958
stroke
9401
05:56:28,958 --> 05:56:30,400
rect
9402
05:56:30,400 --> 05:56:31,920
we'll need starting coordinates maybe
9403
05:56:31,920 --> 05:56:35,360
the top left corner that is 0 0
9404
05:56:35,360 --> 05:56:38,320
followed by a width and a height
9405
05:56:38,320 --> 05:56:40,638
so for the width 250 and the height 250
9406
05:56:40,638 --> 05:56:41,760
as well
9407
05:56:41,760 --> 05:56:42,878
there
9408
05:56:42,878 --> 05:56:45,680
we have a rectangle to change the stroke
9409
05:56:45,680 --> 05:56:47,680
style we can set the stroke style
9410
05:56:47,680 --> 05:56:48,958
property
9411
05:56:48,958 --> 05:56:52,000
context dot stroke
9412
05:56:52,000 --> 05:56:53,280
style
9413
05:56:53,280 --> 05:56:56,558
equals let's pick i don't know red
9414
05:56:56,558 --> 05:56:58,480
there so we have a red border
9415
05:56:58,480 --> 05:57:00,558
i think i'll change that to black though
9416
05:57:00,558 --> 05:57:02,798
if you need a filled rectangle
9417
05:57:02,798 --> 05:57:05,680
use the fill rect method so i'm going to
9418
05:57:05,680 --> 05:57:08,000
copy this paste it
9419
05:57:08,000 --> 05:57:09,360
and this would be
9420
05:57:09,360 --> 05:57:11,600
fill rect
9421
05:57:11,600 --> 05:57:13,920
so this will fill our rectangle if you
9422
05:57:13,920 --> 05:57:16,638
need to change the fill color we can set
9423
05:57:16,638 --> 05:57:18,878
the fill style property
9424
05:57:18,878 --> 05:57:20,718
context dot
9425
05:57:20,718 --> 05:57:21,600
fill
9426
05:57:21,600 --> 05:57:23,200
style
9427
05:57:23,200 --> 05:57:26,240
and pick a color maybe red again
9428
05:57:26,240 --> 05:57:28,240
there we have a red rectangle with a
9429
05:57:28,240 --> 05:57:30,718
black border to change the border
9430
05:57:30,718 --> 05:57:32,638
thickness you just have to change the
9431
05:57:32,638 --> 05:57:35,360
line width property
9432
05:57:35,360 --> 05:57:38,080
i'll keep that as it originally was
9433
05:57:38,080 --> 05:57:41,440
okay let's create another rectangle
9434
05:57:41,440 --> 05:57:43,120
so i'll copy this
9435
05:57:43,120 --> 05:57:44,798
paste it
9436
05:57:44,798 --> 05:57:47,040
i'll make the original one black let's
9437
05:57:47,040 --> 05:57:48,878
place the second rectangle directly
9438
05:57:48,878 --> 05:57:50,240
underneath the first
9439
05:57:50,240 --> 05:57:52,798
so the starting position would be
9440
05:57:52,798 --> 05:57:54,000
0
9441
05:57:54,000 --> 05:57:55,520
250
9442
05:57:55,520 --> 05:57:58,320
and the stroke would be 0 250 as well
9443
05:57:58,320 --> 05:58:00,798
there let's create another this will be
9444
05:58:00,798 --> 05:58:01,600
a
9445
05:58:01,600 --> 05:58:03,600
green square and the starting corner
9446
05:58:03,600 --> 05:58:06,240
will be 250 250 same thing with the
9447
05:58:06,240 --> 05:58:08,718
stroke okay last one let's fill in this
9448
05:58:08,718 --> 05:58:10,958
empty space
9449
05:58:10,958 --> 05:58:12,958
a blue square
9450
05:58:12,958 --> 05:58:15,600
and this will begin at this point
9451
05:58:15,600 --> 05:58:17,200
that is
9452
05:58:17,200 --> 05:58:19,200
250 0
9453
05:58:19,200 --> 05:58:22,080
for the coordinates
9454
05:58:22,638 --> 05:58:24,840
so yeah we have successfully drawn four
9455
05:58:24,840 --> 05:58:26,958
squares okay this time we're going to
9456
05:58:26,958 --> 05:58:28,558
draw a circle
9457
05:58:28,558 --> 05:58:29,920
context
9458
05:58:29,920 --> 05:58:30,798
dot
9459
05:58:30,798 --> 05:58:31,840
begin
9460
05:58:31,840 --> 05:58:33,680
path
9461
05:58:33,680 --> 05:58:36,080
and we will end the section with context
9462
05:58:36,080 --> 05:58:38,480
dot stroke
9463
05:58:38,480 --> 05:58:40,480
to draw a circle we will use the arc
9464
05:58:40,480 --> 05:58:41,440
method
9465
05:58:41,440 --> 05:58:44,000
context.arc
9466
05:58:44,000 --> 05:58:46,080
and there's a few arguments
9467
05:58:46,080 --> 05:58:47,760
starting coordinates for the center of
9468
05:58:47,760 --> 05:58:48,958
our circle
9469
05:58:48,958 --> 05:58:50,878
if i place the starting coordinates at 0
9470
05:58:50,878 --> 05:58:51,840
0
9471
05:58:51,840 --> 05:58:53,760
the center of my circle will begin in
9472
05:58:53,760 --> 05:58:56,480
the top left corner of my canvas
9473
05:58:56,480 --> 05:58:59,360
then a radius what about 100
9474
05:58:59,360 --> 05:59:01,200
for the next two arguments we need a
9475
05:59:01,200 --> 05:59:03,040
starting angle and ending angle in
9476
05:59:03,040 --> 05:59:04,878
radians
9477
05:59:04,878 --> 05:59:08,240
for a full circle that would be zero 2
9478
05:59:08,240 --> 05:59:10,400
then multiply 2 by
9479
05:59:10,400 --> 05:59:13,600
math dot pi
9480
05:59:13,600 --> 05:59:16,320
there well it's a part of a circle
9481
05:59:16,320 --> 05:59:18,080
but the center is starting the top left
9482
05:59:18,080 --> 05:59:20,638
corner i'll set the x and y coordinates
9483
05:59:20,638 --> 05:59:23,840
to 100
9484
05:59:23,840 --> 05:59:25,520
there that's much better
9485
05:59:25,520 --> 05:59:28,160
what about the middle 250
9486
05:59:28,160 --> 05:59:30,000
250
9487
05:59:30,000 --> 05:59:34,558
i'll increase the radius to maybe 200.
9488
05:59:34,558 --> 05:59:36,480
the starting angle and ending angle are
9489
05:59:36,480 --> 05:59:38,000
in radians
9490
05:59:38,000 --> 05:59:39,360
so you can change these for an
9491
05:59:39,360 --> 05:59:41,440
incomplete circle
9492
05:59:41,440 --> 05:59:43,280
if you ever need to reverse these
9493
05:59:43,280 --> 05:59:47,040
pass in true for the last argument
9494
05:59:47,040 --> 05:59:48,798
this would probably be helpful to you if
9495
05:59:48,798 --> 05:59:51,040
you have a pie chart to work on
9496
05:59:51,040 --> 05:59:53,920
okay let's fill the circle
9497
05:59:53,920 --> 05:59:57,760
context dot fill
9498
05:59:58,160 --> 05:59:59,840
let's change the color
9499
05:59:59,840 --> 06:00:03,120
that would be fill style context
9500
06:00:03,120 --> 06:00:05,360
fill style
9501
06:00:05,360 --> 06:00:07,680
i'll set this to maybe light blue let's
9502
06:00:07,680 --> 06:00:09,840
try draw bubble
9503
06:00:09,840 --> 06:00:11,760
and i'll give this a thicker stroke for
9504
06:00:11,760 --> 06:00:14,240
the border
9505
06:00:14,320 --> 06:00:19,280
context dot line with property equals
9506
06:00:19,280 --> 06:00:21,600
maybe 10.
9507
06:00:21,600 --> 06:00:24,080
i'll change the color as well
9508
06:00:24,080 --> 06:00:26,878
context dot stroke
9509
06:00:26,878 --> 06:00:28,160
style
9510
06:00:28,160 --> 06:00:30,160
equals
9511
06:00:30,160 --> 06:00:33,440
maybe dark blue
9512
06:00:33,440 --> 06:00:34,558
sweet
9513
06:00:34,558 --> 06:00:36,958
so yeah that's how to draw a circle
9514
06:00:36,958 --> 06:00:38,558
and for this last section we'll draw
9515
06:00:38,558 --> 06:00:40,878
some text
9516
06:00:40,878 --> 06:00:41,840
draw
9517
06:00:41,840 --> 06:00:43,920
text
9518
06:00:43,920 --> 06:00:47,120
to draw some text you would type context
9519
06:00:47,120 --> 06:00:48,000
dot
9520
06:00:48,000 --> 06:00:49,120
fill
9521
06:00:49,120 --> 06:00:50,718
text
9522
06:00:50,718 --> 06:00:52,958
what's some text we would like to add
9523
06:00:52,958 --> 06:00:55,840
maybe you win
9524
06:00:55,840 --> 06:00:57,520
then we need some coordinates let's
9525
06:00:57,520 --> 06:00:59,760
begin at zero zero
9526
06:00:59,760 --> 06:01:01,280
you can't really see it right now it's
9527
06:01:01,280 --> 06:01:03,520
above the top left corner let's bring it
9528
06:01:03,520 --> 06:01:07,280
down a little bit 100 100
9529
06:01:07,280 --> 06:01:09,520
there although it's very small let's
9530
06:01:09,520 --> 06:01:11,600
change the font
9531
06:01:11,600 --> 06:01:12,878
context
9532
06:01:12,878 --> 06:01:16,480
dot font property equals and pick a font
9533
06:01:16,480 --> 06:01:19,520
maybe 50 pixels and the font style will
9534
06:01:19,520 --> 06:01:21,520
be you know pick whatever you want but i
9535
06:01:21,520 --> 06:01:24,160
like mv bully
9536
06:01:24,160 --> 06:01:25,920
that's better
9537
06:01:25,920 --> 06:01:28,480
we can change the fill style that's the
9538
06:01:28,480 --> 06:01:30,320
font color
9539
06:01:30,320 --> 06:01:32,480
context dot
9540
06:01:32,480 --> 06:01:33,520
fill
9541
06:01:33,520 --> 06:01:34,718
style
9542
06:01:34,718 --> 06:01:38,400
equals maybe gray
9543
06:01:38,798 --> 06:01:40,798
now if you need to center some text
9544
06:01:40,798 --> 06:01:43,600
within a canvas this is what you can do
9545
06:01:43,600 --> 06:01:45,280
we'll need to calculate half of our
9546
06:01:45,280 --> 06:01:47,040
canvas's width
9547
06:01:47,040 --> 06:01:48,798
so as the second argument within the
9548
06:01:48,798 --> 06:01:53,280
fill text method type canvas dot width
9549
06:01:53,280 --> 06:01:55,600
divided by two do the same thing with
9550
06:01:55,600 --> 06:01:57,840
the height
9551
06:01:59,040 --> 06:02:02,320
canvas dot height divided by 2.
9552
06:02:02,320 --> 06:02:04,000
we're almost there
9553
06:02:04,000 --> 06:02:05,920
we will need to set the text align
9554
06:02:05,920 --> 06:02:07,840
property to center
9555
06:02:07,840 --> 06:02:10,718
preceding our fill text method
9556
06:02:10,718 --> 06:02:12,718
context dot
9557
06:02:12,718 --> 06:02:13,600
text
9558
06:02:13,600 --> 06:02:14,798
align
9559
06:02:14,798 --> 06:02:17,360
equals center
9560
06:02:17,360 --> 06:02:19,920
and that's how to center some text
9561
06:02:19,920 --> 06:02:22,160
so yeah everybody those are a few simple
9562
06:02:22,160 --> 06:02:24,320
shapes along with text that you can draw
9563
06:02:24,320 --> 06:02:25,920
on a canvas
9564
06:02:25,920 --> 06:02:27,440
if you would like a copy of this code
9565
06:02:27,440 --> 06:02:28,958
i'll post this in the comments section
9566
06:02:28,958 --> 06:02:31,040
down below and well yeah that's an
9567
06:02:31,040 --> 06:02:33,280
introduction to the canvas api in
9568
06:02:33,280 --> 06:02:35,920
javascript
9569
06:02:35,920 --> 06:02:37,520
hey guys in this video i'm going to
9570
06:02:37,520 --> 06:02:39,440
explain the window object it's an
9571
06:02:39,440 --> 06:02:41,360
interface used to talk with the web
9572
06:02:41,360 --> 06:02:43,280
browser and actually the dom the
9573
06:02:43,280 --> 06:02:45,600
document object model is a property of
9574
06:02:45,600 --> 06:02:47,360
the window let's examine this window
9575
06:02:47,360 --> 06:02:51,440
object i will use console.dir
9576
06:02:51,440 --> 06:02:54,080
and pass our window as an argument
9577
06:02:54,080 --> 06:02:56,558
here's our window object
9578
06:02:56,558 --> 06:02:59,120
it has its own properties and methods
9579
06:02:59,120 --> 06:03:01,840
one of which is document the document
9580
06:03:01,840 --> 06:03:03,920
object model by interacting with the
9581
06:03:03,920 --> 06:03:05,920
properties and methods of our window we
9582
06:03:05,920 --> 06:03:07,680
can change the behavior of our web
9583
06:03:07,680 --> 06:03:09,920
browser let's cover a few
9584
06:03:09,920 --> 06:03:12,000
i need the width and height of this
9585
06:03:12,000 --> 06:03:13,920
inner window to do that i would type
9586
06:03:13,920 --> 06:03:16,718
window dot inner width
9587
06:03:16,718 --> 06:03:20,718
and window dot inner height
9588
06:03:20,718 --> 06:03:22,958
i'll place these within console.log to
9589
06:03:22,958 --> 06:03:27,000
display it console.log
9590
06:03:27,920 --> 06:03:30,798
the inner width of this window is 383
9591
06:03:30,798 --> 06:03:33,360
pixels and the height is 263 but we
9592
06:03:33,360 --> 06:03:35,920
could change these
9593
06:03:35,920 --> 06:03:38,240
now the width is 488 and the height is
9594
06:03:38,240 --> 06:03:41,520
208 there's also outer width and outer
9595
06:03:41,520 --> 06:03:43,760
height
9596
06:03:44,000 --> 06:03:46,840
so these are now respectively 871 and
9597
06:03:46,840 --> 06:03:50,638
984 another is scroll x and scroll y but
9598
06:03:50,638 --> 06:03:52,240
we'll need some scroll bars within our
9599
06:03:52,240 --> 06:03:54,718
window i'll create a very large
9600
06:03:54,718 --> 06:03:56,638
development div
9601
06:03:56,638 --> 06:03:59,120
id equals my
9602
06:03:59,120 --> 06:04:00,480
div
9603
06:04:00,480 --> 06:04:02,958
i'll close it
9604
06:04:02,958 --> 06:04:05,600
and add these properties a width of 1000
9605
06:04:05,600 --> 06:04:08,400
a height of 1000 and a background color
9606
06:04:08,400 --> 06:04:10,000
so we have these scroll bars now within
9607
06:04:10,000 --> 06:04:11,280
our window
9608
06:04:11,280 --> 06:04:13,680
i can get the scroll x and scroll y
9609
06:04:13,680 --> 06:04:15,760
properties to shown how far that we have
9610
06:04:15,760 --> 06:04:18,080
scrolled console.log
9611
06:04:18,080 --> 06:04:22,240
window dot scroll x
9612
06:04:22,558 --> 06:04:24,240
and scroll
9613
06:04:24,240 --> 06:04:26,480
y
9614
06:04:27,440 --> 06:04:31,520
scroll x is this value 631 and scroll y
9615
06:04:31,520 --> 06:04:34,160
is this value 758. if i were to place
9616
06:04:34,160 --> 06:04:36,160
these back in their original positions
9617
06:04:36,160 --> 06:04:38,240
well then these values are both zero
9618
06:04:38,240 --> 06:04:40,878
that is scroll x and scroll y i'll get
9619
06:04:40,878 --> 06:04:43,520
rid of this div element
9620
06:04:43,520 --> 06:04:45,760
we can change the href property of this
9621
06:04:45,760 --> 06:04:47,680
window so that it redirects somewhere
9622
06:04:47,680 --> 06:04:50,080
else i'll display that
9623
06:04:50,080 --> 06:04:51,600
console.log
9624
06:04:51,600 --> 06:04:56,798
window now href is found within location
9625
06:04:57,200 --> 06:04:58,480
dot window.location.href
9626
06:04:58,480 --> 06:05:00,638
the current href property has this
9627
06:05:00,638 --> 06:05:03,040
address it's basically my web address
9628
06:05:03,040 --> 06:05:06,718
but we could redirect it somewhere else
9629
06:05:06,920 --> 06:05:09,280
window.location.href equals pick a
9630
06:05:09,280 --> 06:05:12,520
website maybe google https
9631
06:05:12,520 --> 06:05:15,360
colon2forwardslashes google.com
9632
06:05:15,360 --> 06:05:17,600
this should redirect me to google
9633
06:05:17,600 --> 06:05:18,480
yeah
9634
06:05:18,480 --> 06:05:20,240
hey if you need the host name that would
9635
06:05:20,240 --> 06:05:21,120
be
9636
06:05:21,120 --> 06:05:26,480
window dot location dot host name here's
9637
06:05:26,480 --> 06:05:28,480
my host name it's just a local ip
9638
06:05:28,480 --> 06:05:30,520
address
9639
06:05:30,520 --> 06:05:33,200
127.0.0.1 if you need the path name that
9640
06:05:33,200 --> 06:05:34,160
would be
9641
06:05:34,160 --> 06:05:36,000
window dot
9642
06:05:36,000 --> 06:05:38,000
location dot
9643
06:05:38,000 --> 06:05:39,520
path name
9644
06:05:39,520 --> 06:05:42,040
and my path name is forward slash
9645
06:05:42,040 --> 06:05:44,320
index.html for me this would be the
9646
06:05:44,320 --> 06:05:47,520
landing page here's a few useful methods
9647
06:05:47,520 --> 06:05:50,798
we're going to create a button though
9648
06:05:50,958 --> 06:05:55,760
button id equals my button
9649
06:05:55,760 --> 06:05:57,120
let's close it
9650
06:05:57,120 --> 06:06:00,160
i'll add some text just buttons fine
9651
06:06:00,160 --> 06:06:02,558
i'm going to select this button constant
9652
06:06:02,558 --> 06:06:04,160
my button
9653
06:06:04,160 --> 06:06:06,320
equals document
9654
06:06:06,320 --> 06:06:08,798
dot i'll use query selector this time
9655
06:06:08,798 --> 06:06:11,840
query selector
9656
06:06:11,840 --> 06:06:14,958
the id was my button
9657
06:06:14,958 --> 06:06:17,280
and let's add some event listeners
9658
06:06:17,280 --> 06:06:18,958
when i click on this button i would like
9659
06:06:18,958 --> 06:06:20,480
to open a new window
9660
06:06:20,480 --> 06:06:24,080
i'll take my button invoke the add event
9661
06:06:24,080 --> 06:06:26,958
listener method
9662
06:06:28,320 --> 06:06:29,760
when we click
9663
06:06:29,760 --> 06:06:32,080
i will perform a function i'll use an
9664
06:06:32,080 --> 06:06:34,558
arrow function expression
9665
06:06:34,558 --> 06:06:37,200
and i will invoke the windows open
9666
06:06:37,200 --> 06:06:38,480
method
9667
06:06:38,480 --> 06:06:40,400
when we click on this button it opens a
9668
06:06:40,400 --> 06:06:41,760
new window
9669
06:06:41,760 --> 06:06:44,000
you could pass the url within the open
9670
06:06:44,000 --> 06:06:46,240
method
9671
06:06:46,240 --> 06:06:48,240
i'll place this address for google as an
9672
06:06:48,240 --> 06:06:49,360
argument
9673
06:06:49,360 --> 06:06:51,120
then when i click on the button it takes
9674
06:06:51,120 --> 06:06:52,718
me to that website
9675
06:06:52,718 --> 06:06:54,798
another is close
9676
06:06:54,798 --> 06:06:57,440
window dot close
9677
06:06:57,440 --> 06:06:59,680
this will close the current window when
9678
06:06:59,680 --> 06:07:02,840
i click on this button it closes the
9679
06:07:02,840 --> 06:07:05,840
window okay another method is print
9680
06:07:05,840 --> 06:07:07,600
window dot print
9681
06:07:07,600 --> 06:07:09,680
and we can print the current page so
9682
06:07:09,680 --> 06:07:12,000
when i click this button
9683
06:07:12,000 --> 06:07:14,080
we initiate the process of printing this
9684
06:07:14,080 --> 06:07:15,200
page
9685
06:07:15,200 --> 06:07:17,520
we do have a few pop-ups too
9686
06:07:17,520 --> 06:07:20,958
window dot alert
9687
06:07:22,240 --> 06:07:24,480
i'll just display the message hello
9688
06:07:24,480 --> 06:07:25,840
hello
9689
06:07:25,840 --> 06:07:28,878
there's also window.confirm we can have
9690
06:07:28,878 --> 06:07:32,718
a user confirm something
9691
06:07:32,718 --> 06:07:36,798
press ok to continue
9692
06:07:36,958 --> 06:07:39,920
press ok to continue
9693
06:07:39,920 --> 06:07:42,160
window.prompt is another
9694
06:07:42,160 --> 06:07:44,840
window dot
9695
06:07:44,840 --> 06:07:49,200
prompt enter your age
9696
06:07:49,200 --> 06:07:51,280
let's store this within a variable
9697
06:07:51,280 --> 06:07:53,680
let age equal
9698
06:07:53,680 --> 06:07:56,400
window dot prompt
9699
06:07:56,400 --> 06:08:00,798
if age is less than 18
9700
06:08:00,798 --> 06:08:02,400
let's create an alert
9701
06:08:02,400 --> 06:08:05,360
window dot alert
9702
06:08:05,360 --> 06:08:11,600
you must be 18 plus to visit this site
9703
06:08:11,600 --> 06:08:14,080
then we will close the window window dot
9704
06:08:14,080 --> 06:08:15,520
close
9705
06:08:15,520 --> 06:08:17,360
okay enter your age let's say that i'm
9706
06:08:17,360 --> 06:08:19,280
12 i press ok
9707
06:08:19,280 --> 06:08:21,760
you must be 18 plus to visit the site
9708
06:08:21,760 --> 06:08:24,000
then it closes the window
9709
06:08:24,000 --> 06:08:26,000
well okay then everybody that is the
9710
06:08:26,000 --> 06:08:28,798
window object it's an interface used to
9711
06:08:28,798 --> 06:08:30,878
talk with the web browser there's a lot
9712
06:08:30,878 --> 06:08:32,958
of useful properties and methods related
9713
06:08:32,958 --> 06:08:34,958
to the window object if you would like a
9714
06:08:34,958 --> 06:08:36,878
copy of all my code i'll post all of
9715
06:08:36,878 --> 06:08:38,798
this in the comments section down below
9716
06:08:38,798 --> 06:08:40,878
and well yeah that's an introduction to
9717
06:08:40,878 --> 06:08:44,958
the window object using javascript
9718
06:08:45,040 --> 06:08:46,798
hey guys in this video i'm going to
9719
06:08:46,798 --> 06:08:50,080
explain cookies a cookie is a small text
9720
06:08:50,080 --> 06:08:52,080
file that is stored on your computer
9721
06:08:52,080 --> 06:08:53,920
it's used to remember information about
9722
06:08:53,920 --> 06:08:57,040
a user data within a cookie is saved in
9723
06:08:57,040 --> 06:08:59,520
name value pairs in order to check to
9724
06:08:59,520 --> 06:09:01,680
see if we have cookies enabled within
9725
06:09:01,680 --> 06:09:06,000
console.log i can type navigator dot
9726
06:09:06,000 --> 06:09:07,280
cookie
9727
06:09:07,280 --> 06:09:08,798
enabled
9728
06:09:08,798 --> 06:09:10,558
for me that's true i have cookies
9729
06:09:10,558 --> 06:09:13,360
enabled to add a cookie we would type
9730
06:09:13,360 --> 06:09:16,080
document.cookie
9731
06:09:16,080 --> 06:09:18,080
we will set the sequel to a string but
9732
06:09:18,080 --> 06:09:20,638
the string has certain components first
9733
06:09:20,638 --> 06:09:22,878
is a name value pair i would like to
9734
06:09:22,878 --> 06:09:25,040
create a cookie to store a user's first
9735
06:09:25,040 --> 06:09:27,600
name the name of this pair will be let's
9736
06:09:27,600 --> 06:09:30,558
say first name set the sequel to some
9737
06:09:30,558 --> 06:09:33,040
value the value that i will pick is
9738
06:09:33,040 --> 06:09:34,240
spongebob
9739
06:09:34,240 --> 06:09:36,558
then end it with a semicolon now you can
9740
06:09:36,558 --> 06:09:38,718
add an expiration date when the current
9741
06:09:38,718 --> 06:09:40,400
time has passed a certain expiration
9742
06:09:40,400 --> 06:09:42,160
date well then this cookie will be
9743
06:09:42,160 --> 06:09:44,878
deleted to set an expiration date set
9744
06:09:44,878 --> 06:09:48,400
expires equal to then pick a date i'll
9745
06:09:48,400 --> 06:09:50,840
pick sunday
9746
06:09:50,840 --> 06:09:53,200
first january
9747
06:09:53,200 --> 06:09:55,200
maybe a date that's really far out like
9748
06:09:55,200 --> 06:09:56,878
20 30.
9749
06:09:56,878 --> 06:09:59,600
at midnight
9750
06:09:59,600 --> 06:10:01,120
utc
9751
06:10:01,120 --> 06:10:03,680
then semicolon we're also able to set a
9752
06:10:03,680 --> 06:10:05,360
path for this cookie i'll use the
9753
06:10:05,360 --> 06:10:07,920
default path that would be path equals
9754
06:10:07,920 --> 06:10:10,000
forward slash okay let's take a look at
9755
06:10:10,000 --> 06:10:11,200
this cookie
9756
06:10:11,200 --> 06:10:13,360
console.log
9757
06:10:13,360 --> 06:10:16,718
document dot cookie even though this
9758
06:10:16,718 --> 06:10:18,798
appears to be a string it's actually an
9759
06:10:18,798 --> 06:10:21,040
object the expiration date and the path
9760
06:10:21,040 --> 06:10:23,120
aren't displayed all that's displayed is
9761
06:10:23,120 --> 06:10:25,360
the name and the value you can add more
9762
06:10:25,360 --> 06:10:27,680
than one cookie so again we will type
9763
06:10:27,680 --> 06:10:29,600
document.cookie
9764
06:10:29,600 --> 06:10:31,280
let's copy this text
9765
06:10:31,280 --> 06:10:33,600
paste it
9766
06:10:33,600 --> 06:10:34,480
last
9767
06:10:34,480 --> 06:10:35,520
name
9768
06:10:35,520 --> 06:10:39,280
last name equals square
9769
06:10:39,280 --> 06:10:40,878
pants
9770
06:10:40,878 --> 06:10:42,240
then let's take a look at our cookie
9771
06:10:42,240 --> 06:10:43,440
again
9772
06:10:43,440 --> 06:10:45,840
the cookie property of our document can
9773
06:10:45,840 --> 06:10:48,080
hold more than one cookie but to access
9774
06:10:48,080 --> 06:10:49,920
it you would just type cookie to
9775
06:10:49,920 --> 06:10:51,920
overwrite a cookie you would just change
9776
06:10:51,920 --> 06:10:54,240
the value of a name value pair if i
9777
06:10:54,240 --> 06:10:55,760
overwrite the first name and the last
9778
06:10:55,760 --> 06:10:58,080
name they'll change this time let's set
9779
06:10:58,080 --> 06:11:00,320
first name to patrick
9780
06:11:00,320 --> 06:11:03,360
last name star there we go first name
9781
06:11:03,360 --> 06:11:05,920
patrick last name star if we change the
9782
06:11:05,920 --> 06:11:07,840
expiration date to a date that has
9783
06:11:07,840 --> 06:11:09,680
already passed that will delete this
9784
06:11:09,680 --> 06:11:12,320
cookie it'll expire so maybe the last
9785
06:11:12,320 --> 06:11:15,600
name expired in the year 2000
9786
06:11:15,600 --> 06:11:17,680
that last name portion of my cookie is
9787
06:11:17,680 --> 06:11:19,760
no longer there so that's how to expire
9788
06:11:19,760 --> 06:11:21,680
a cookie at this point we're going to
9789
06:11:21,680 --> 06:11:23,680
create a function to create a cookie
9790
06:11:23,680 --> 06:11:26,160
instead of doing so manually
9791
06:11:26,160 --> 06:11:28,840
function set
9792
06:11:28,840 --> 06:11:31,440
cookie we'll need a few things
9793
06:11:31,440 --> 06:11:33,840
a name because these are a name value
9794
06:11:33,840 --> 06:11:35,520
pairs
9795
06:11:35,520 --> 06:11:36,718
a value
9796
06:11:36,718 --> 06:11:38,320
and an expiration date although that's
9797
06:11:38,320 --> 06:11:40,638
optional for the third argument let's
9798
06:11:40,638 --> 06:11:44,080
name this parameter days to live as an
9799
06:11:44,080 --> 06:11:46,080
argument you'll pass a number of days
9800
06:11:46,080 --> 06:11:47,520
that you would like this cookie to be
9801
06:11:47,520 --> 06:11:50,400
set to expire 365 would be one year
9802
06:11:50,400 --> 06:11:52,558
let's create a date object
9803
06:11:52,558 --> 06:11:53,680
const
9804
06:11:53,680 --> 06:11:57,600
date equals new date i will set the time
9805
06:11:57,600 --> 06:11:59,280
of this date date
9806
06:11:59,280 --> 06:12:00,878
dot set
9807
06:12:00,878 --> 06:12:03,120
time method within the parentheses we
9808
06:12:03,120 --> 06:12:06,638
will take date dot get time
9809
06:12:06,638 --> 06:12:08,160
this will return the current time in
9810
06:12:08,160 --> 06:12:10,798
milliseconds then add the amount of days
9811
06:12:10,798 --> 06:12:13,680
to live converted to milliseconds
9812
06:12:13,680 --> 06:12:15,600
days to live
9813
06:12:15,600 --> 06:12:18,320
times 24 hours
9814
06:12:18,320 --> 06:12:19,440
times
9815
06:12:19,440 --> 06:12:21,440
60 minutes
9816
06:12:21,440 --> 06:12:24,558
times 60 seconds
9817
06:12:24,558 --> 06:12:27,920
times 1000 for 1000 milliseconds our
9818
06:12:27,920 --> 06:12:30,718
future date is currently in milliseconds
9819
06:12:30,718 --> 06:12:34,400
we'll convert that to utc string date
9820
06:12:34,400 --> 06:12:35,200
to
9821
06:12:35,200 --> 06:12:36,798
utc
9822
06:12:36,798 --> 06:12:38,958
string method i'll assign this to a
9823
06:12:38,958 --> 06:12:40,558
variable
9824
06:12:40,558 --> 06:12:41,680
let
9825
06:12:41,680 --> 06:12:43,520
expires
9826
06:12:43,520 --> 06:12:44,638
equals
9827
06:12:44,638 --> 06:12:45,680
and then we'll use some string
9828
06:12:45,680 --> 06:12:48,000
concatenation we'll need to set this
9829
06:12:48,000 --> 06:12:49,680
expires value
9830
06:12:49,680 --> 06:12:52,840
so within quotes expires
9831
06:12:52,840 --> 06:12:55,040
equals plus
9832
06:12:55,040 --> 06:12:57,360
our future date converted to a utc
9833
06:12:57,360 --> 06:13:00,440
string then we can assign our cookie
9834
06:13:00,440 --> 06:13:03,280
document.cookie equals i'll use a
9835
06:13:03,280 --> 06:13:05,040
template literal
9836
06:13:05,040 --> 06:13:07,680
we're inserting our name that we pass in
9837
06:13:07,680 --> 06:13:09,680
equals
9838
06:13:09,680 --> 06:13:12,080
our value
9839
06:13:12,080 --> 06:13:15,360
add a semicolon to finish this section
9840
06:13:15,360 --> 06:13:18,320
then the expire section
9841
06:13:18,320 --> 06:13:20,160
expires
9842
06:13:20,160 --> 06:13:21,600
then you could also add a path if you
9843
06:13:21,600 --> 06:13:22,480
would like
9844
06:13:22,480 --> 06:13:25,280
path equals forward slash that's the
9845
06:13:25,280 --> 06:13:27,120
default path okay now let's create some
9846
06:13:27,120 --> 06:13:30,320
cookies i will invoke our set cookie
9847
06:13:30,320 --> 06:13:31,760
function
9848
06:13:31,760 --> 06:13:34,480
pass in a name value pair and the amount
9849
06:13:34,480 --> 06:13:36,320
of days i would like this cookie active
9850
06:13:36,320 --> 06:13:38,878
what about an email address
9851
06:13:38,878 --> 06:13:40,558
email
9852
06:13:40,558 --> 06:13:42,718
make up some email address
9853
06:13:42,718 --> 06:13:44,360
sponge
9854
06:13:44,360 --> 06:13:46,080
gmail.com
9855
06:13:46,080 --> 06:13:48,878
i would like this cookie to live for 365
9856
06:13:48,878 --> 06:13:52,360
days then let's display our cookies
9857
06:13:52,360 --> 06:13:54,320
console.log
9858
06:13:54,320 --> 06:13:56,320
document dot
9859
06:13:56,320 --> 06:13:58,798
cookie
9860
06:13:58,798 --> 06:14:01,120
and here's our cookies we still have our
9861
06:14:01,120 --> 06:14:02,718
first name and last name because we
9862
06:14:02,718 --> 06:14:04,718
never deleted them but we do have an
9863
06:14:04,718 --> 06:14:08,798
email now email equals sponge gmail.com
9864
06:14:08,798 --> 06:14:10,718
how can we delete a cookie all we have
9865
06:14:10,718 --> 06:14:12,798
to do is set the expiration date to a
9866
06:14:12,798 --> 06:14:15,600
pass date here's one easy way to do that
9867
06:14:15,600 --> 06:14:17,760
let's create function
9868
06:14:17,760 --> 06:14:18,878
delete
9869
06:14:18,878 --> 06:14:20,320
cookie
9870
06:14:20,320 --> 06:14:22,958
all we'll need is a name
9871
06:14:22,958 --> 06:14:26,638
what we'll do is invoke set cookie
9872
06:14:26,638 --> 06:14:29,120
pass our name as an argument
9873
06:14:29,120 --> 06:14:31,440
null for the value and null for the days
9874
06:14:31,440 --> 06:14:33,680
to live if i was to invoke the delete
9875
06:14:33,680 --> 06:14:36,080
cookie method and pass in the name email
9876
06:14:36,080 --> 06:14:38,080
well that email cookie is no longer in
9877
06:14:38,080 --> 06:14:40,320
here so it's gone let's delete the other
9878
06:14:40,320 --> 06:14:42,240
cookies too
9879
06:14:42,240 --> 06:14:43,440
delete
9880
06:14:43,440 --> 06:14:44,840
first name
9881
06:14:44,840 --> 06:14:47,680
delete last name
9882
06:14:47,680 --> 06:14:49,760
and those three cookies are cleared
9883
06:14:49,760 --> 06:14:51,840
let's create a function to get the value
9884
06:14:51,840 --> 06:14:54,240
of a cookie by a name let's declare this
9885
06:14:54,240 --> 06:14:56,480
function function
9886
06:14:56,480 --> 06:14:57,360
get
9887
06:14:57,360 --> 06:14:59,440
cookie
9888
06:14:59,440 --> 06:15:02,000
we'll accept a name as an argument what
9889
06:15:02,000 --> 06:15:04,480
we'll need to do is decode our cookie
9890
06:15:04,480 --> 06:15:06,400
i'll store this within a constant
9891
06:15:06,400 --> 06:15:07,760
constant
9892
06:15:07,760 --> 06:15:09,520
c for cookie
9893
06:15:09,520 --> 06:15:11,040
decoded
9894
06:15:11,040 --> 06:15:12,958
equals
9895
06:15:12,958 --> 06:15:14,480
decode
9896
06:15:14,480 --> 06:15:16,320
uri
9897
06:15:16,320 --> 06:15:18,718
component
9898
06:15:18,718 --> 06:15:20,160
then pass in
9899
06:15:20,160 --> 06:15:22,638
document.cookie
9900
06:15:22,638 --> 06:15:24,400
let's take a look at this
9901
06:15:24,400 --> 06:15:27,400
console.log
9902
06:15:28,958 --> 06:15:30,958
c decoded let's make sure we have at
9903
06:15:30,958 --> 06:15:32,558
least two cookies
9904
06:15:32,558 --> 06:15:35,040
set cookie
9905
06:15:35,040 --> 06:15:37,680
first name
9906
06:15:38,718 --> 06:15:40,958
spongebob
9907
06:15:40,958 --> 06:15:44,718
365 days to live
9908
06:15:45,200 --> 06:15:48,558
set cookie last name
9909
06:15:48,878 --> 06:15:51,760
squarepants
9910
06:15:51,760 --> 06:15:56,798
then i will invoke the getcookie method
9911
06:15:56,798 --> 06:15:59,600
then pass in a name first name is fine
9912
06:15:59,600 --> 06:16:01,440
okay let's see what we have this is
9913
06:16:01,440 --> 06:16:03,920
what's stored within c decoded we have
9914
06:16:03,920 --> 06:16:05,920
our name value pairs what we'll need to
9915
06:16:05,920 --> 06:16:08,080
do is split each of these name value
9916
06:16:08,080 --> 06:16:10,638
pairs at each of these semicolons that's
9917
06:16:10,638 --> 06:16:13,040
how to separate them so after this first
9918
06:16:13,040 --> 06:16:14,958
statement we'll take
9919
06:16:14,958 --> 06:16:16,558
c decoded
9920
06:16:16,558 --> 06:16:19,840
and split at each semicolon semicolon
9921
06:16:19,840 --> 06:16:22,400
space this will return an array we'll
9922
06:16:22,400 --> 06:16:24,400
store that within const
9923
06:16:24,400 --> 06:16:25,680
c
9924
06:16:25,680 --> 06:16:26,958
array
9925
06:16:26,958 --> 06:16:31,200
cookie array let's display cookie array
9926
06:16:31,200 --> 06:16:34,000
console.log cookie array each of those
9927
06:16:34,000 --> 06:16:36,240
name value pairs is now within separate
9928
06:16:36,240 --> 06:16:38,480
elements what we're going to do is for
9929
06:16:38,480 --> 06:16:40,240
each element we'll check to see if
9930
06:16:40,240 --> 06:16:41,920
there's a match between each of these
9931
06:16:41,920 --> 06:16:43,680
element names and the name that we're
9932
06:16:43,680 --> 06:16:46,400
looking for let's take our cookie array
9933
06:16:46,400 --> 06:16:47,760
c array
9934
06:16:47,760 --> 06:16:49,440
and use the for
9935
06:16:49,440 --> 06:16:50,718
each method
9936
06:16:50,718 --> 06:16:52,638
i'll use an arrow function expression
9937
06:16:52,638 --> 06:16:56,080
there is one parameter element arrow
9938
06:16:56,080 --> 06:16:57,920
what would we like to do for each
9939
06:16:57,920 --> 06:16:59,120
element
9940
06:16:59,120 --> 06:17:01,200
i'll use an if statement
9941
06:17:01,200 --> 06:17:03,200
let's check to see if
9942
06:17:03,200 --> 06:17:04,638
our element
9943
06:17:04,638 --> 06:17:07,440
at index of
9944
06:17:07,440 --> 06:17:09,680
the name that we're looking for
9945
06:17:09,680 --> 06:17:12,240
is equal to zero say that we're looking
9946
06:17:12,240 --> 06:17:14,718
for last name we'll iterate over each
9947
06:17:14,718 --> 06:17:17,040
element of our array and see if there's
9948
06:17:17,040 --> 06:17:19,360
a match we check the first element these
9949
06:17:19,360 --> 06:17:22,000
don't match but this one does if there's
9950
06:17:22,000 --> 06:17:24,958
a match then let's return the result
9951
06:17:24,958 --> 06:17:26,080
results
9952
06:17:26,080 --> 06:17:27,200
equals
9953
06:17:27,200 --> 06:17:29,600
and we'll create a substring
9954
06:17:29,600 --> 06:17:32,600
element.substring
9955
06:17:34,480 --> 06:17:38,080
name dot length plus one the length of
9956
06:17:38,080 --> 06:17:40,160
the name plus one will create a
9957
06:17:40,160 --> 06:17:42,798
substring and return this text so let's
9958
06:17:42,798 --> 06:17:44,000
declare
9959
06:17:44,000 --> 06:17:46,160
let result
9960
06:17:46,160 --> 06:17:48,160
at the end
9961
06:17:48,160 --> 06:17:49,280
return
9962
06:17:49,280 --> 06:17:50,638
result
9963
06:17:50,638 --> 06:17:53,280
actually i'm going to set result to null
9964
06:17:53,280 --> 06:17:55,200
okay let's see if this works
9965
06:17:55,200 --> 06:17:57,120
so we have two cookies
9966
06:17:57,120 --> 06:18:00,240
and i'm going to console.log
9967
06:18:00,240 --> 06:18:01,440
getcookie
9968
06:18:01,440 --> 06:18:03,600
pass in my first name
9969
06:18:03,600 --> 06:18:07,040
and that first name is spongebob
9970
06:18:07,040 --> 06:18:08,958
let's get last name
9971
06:18:08,958 --> 06:18:10,320
last name
9972
06:18:10,320 --> 06:18:13,280
spongebob and squarepants okay yeah
9973
06:18:13,280 --> 06:18:16,000
that's how to set delete and get cookies
9974
06:18:16,000 --> 06:18:17,840
let's take this a step further let's
9975
06:18:17,840 --> 06:18:20,160
create some text fields a submit button
9976
06:18:20,160 --> 06:18:22,320
and a get cookies button heading to our
9977
06:18:22,320 --> 06:18:25,520
html file let's create some text fields
9978
06:18:25,520 --> 06:18:27,520
and some labels
9979
06:18:27,520 --> 06:18:28,558
label
9980
06:18:28,558 --> 06:18:29,840
for
9981
06:18:29,840 --> 06:18:31,360
equals
9982
06:18:31,360 --> 06:18:34,558
first text
9983
06:18:35,120 --> 06:18:38,160
first name colon
9984
06:18:38,160 --> 06:18:40,958
close the label
9985
06:18:41,040 --> 06:18:42,638
input
9986
06:18:42,638 --> 06:18:46,400
id equals first text
9987
06:18:46,400 --> 06:18:49,440
i'll add a line break
9988
06:18:49,440 --> 06:18:52,878
let's do the same thing with last name
9989
06:18:52,878 --> 06:18:55,120
last text
9990
06:18:55,120 --> 06:18:57,520
last name
9991
06:18:57,520 --> 06:19:00,958
id is last text
9992
06:19:00,958 --> 06:19:03,440
i'll create a submit button
9993
06:19:03,440 --> 06:19:06,080
button id equals
9994
06:19:06,080 --> 06:19:07,200
submit
9995
06:19:07,200 --> 06:19:09,760
now let's name this btn for short submit
9996
06:19:09,760 --> 06:19:12,240
button
9997
06:19:12,240 --> 06:19:14,798
will be
9998
06:19:15,120 --> 06:19:17,680
submit
9999
06:19:17,920 --> 06:19:21,520
then a get cookies button
10000
06:19:21,680 --> 06:19:25,920
cookies btn for button the text will be
10001
06:19:25,920 --> 06:19:27,040
get
10002
06:19:27,040 --> 06:19:29,600
cookies
10003
06:19:29,840 --> 06:19:32,718
okay heading back to our javascript file
10004
06:19:32,718 --> 06:19:34,798
let's select these elements
10005
06:19:34,798 --> 06:19:36,160
const
10006
06:19:36,160 --> 06:19:38,160
first text
10007
06:19:38,160 --> 06:19:40,400
equals document
10008
06:19:40,400 --> 06:19:41,600
dot
10009
06:19:41,600 --> 06:19:44,478
query selector
10010
06:19:44,478 --> 06:19:50,718
we're selecting the id of first text
10011
06:19:51,600 --> 06:19:55,840
let's do the same thing with last text
10012
06:20:01,120 --> 06:20:03,280
our submit button
10013
06:20:03,280 --> 06:20:07,320
submit button
10014
06:20:09,600 --> 06:20:12,638
then cookie button
10015
06:20:13,200 --> 06:20:14,400
cookie
10016
06:20:14,400 --> 06:20:16,798
button
10017
06:20:19,360 --> 06:20:20,878
i'm going to add an event listener to
10018
06:20:20,878 --> 06:20:22,478
our submit button
10019
06:20:22,478 --> 06:20:25,040
submit button dot add
10020
06:20:25,040 --> 06:20:26,478
event
10021
06:20:26,478 --> 06:20:29,040
listener
10022
06:20:29,520 --> 06:20:32,400
the event attribute will be click
10023
06:20:32,400 --> 06:20:34,878
when we click we're going to perform an
10024
06:20:34,878 --> 06:20:37,440
arrow function expression
10025
06:20:37,440 --> 06:20:39,200
we will set the cookies according to
10026
06:20:39,200 --> 06:20:41,440
what these values are within these text
10027
06:20:41,440 --> 06:20:44,240
boxes we will invoke the set cookie
10028
06:20:44,240 --> 06:20:46,160
function
10029
06:20:46,160 --> 06:20:49,440
the name will be first name
10030
06:20:49,440 --> 06:20:51,680
the value will be whatever's within our
10031
06:20:51,680 --> 06:20:53,440
text boxes
10032
06:20:53,440 --> 06:20:55,280
first text
10033
06:20:55,280 --> 06:20:57,360
dot value
10034
06:20:57,360 --> 06:20:59,280
and days to live i'll set this to be a
10035
06:20:59,280 --> 06:21:00,200
year
10036
06:21:00,200 --> 06:21:02,400
365 days
10037
06:21:02,400 --> 06:21:05,040
let's do the same thing with our last
10038
06:21:05,040 --> 06:21:06,000
name
10039
06:21:06,000 --> 06:21:09,440
set cookie last name
10040
06:21:09,760 --> 06:21:12,080
last text.value
10041
06:21:12,080 --> 06:21:13,600
let's add an event listener to our
10042
06:21:13,600 --> 06:21:15,680
cookie button that will populate the
10043
06:21:15,680 --> 06:21:17,680
fields that function will search for any
10044
06:21:17,680 --> 06:21:20,320
cookies and return a value okay we have
10045
06:21:20,320 --> 06:21:23,200
cookie button add event listener
10046
06:21:23,200 --> 06:21:25,520
click
10047
06:21:26,718 --> 06:21:28,558
first text
10048
06:21:28,558 --> 06:21:30,000
dot value
10049
06:21:30,000 --> 06:21:31,360
equals
10050
06:21:31,360 --> 06:21:32,718
get
10051
06:21:32,718 --> 06:21:35,120
cookie
10052
06:21:35,200 --> 06:21:38,718
then the name of the cookie first name
10053
06:21:38,718 --> 06:21:41,680
do the same thing with last text last
10054
06:21:41,680 --> 06:21:42,558
text
10055
06:21:42,558 --> 06:21:44,878
value gut cookie
10056
06:21:44,878 --> 06:21:47,840
last name
10057
06:21:51,520 --> 06:21:53,680
whoops looks like i misspelt this id
10058
06:21:53,680 --> 06:21:55,520
cookie button now before we run this i'm
10059
06:21:55,520 --> 06:21:58,878
just going to delete our cookies
10060
06:21:58,878 --> 06:22:00,798
delete cookie
10061
06:22:00,798 --> 06:22:03,360
first name
10062
06:22:03,520 --> 06:22:06,878
delete cookie last name
10063
06:22:08,878 --> 06:22:11,440
then save and run it
10064
06:22:11,440 --> 06:22:13,520
so when i press get cookies well we
10065
06:22:13,520 --> 06:22:15,200
don't have any first name or last name
10066
06:22:15,200 --> 06:22:17,200
cookies there's nothing to populate
10067
06:22:17,200 --> 06:22:19,280
these text fields so i'm going to type
10068
06:22:19,280 --> 06:22:21,520
in a first name and a last name
10069
06:22:21,520 --> 06:22:23,760
spongebob
10070
06:22:23,760 --> 06:22:25,840
squarepants
10071
06:22:25,840 --> 06:22:27,280
press submit
10072
06:22:27,280 --> 06:22:28,958
this first name and last name are stored
10073
06:22:28,958 --> 06:22:31,440
as cookies now so i'm going to refresh
10074
06:22:31,440 --> 06:22:33,920
this page press that get cookies button
10075
06:22:33,920 --> 06:22:35,760
again that will populate these text
10076
06:22:35,760 --> 06:22:38,080
fields with the cookies i have so yeah
10077
06:22:38,080 --> 06:22:39,840
everybody that's an introduction to
10078
06:22:39,840 --> 06:22:42,558
cookies their small text files stored on
10079
06:22:42,558 --> 06:22:44,240
your computer they're used to remember
10080
06:22:44,240 --> 06:22:46,080
information about the user and they're
10081
06:22:46,080 --> 06:22:48,718
saved in name value pairs if you would
10082
06:22:48,718 --> 06:22:50,240
like a copy of all this code i know
10083
06:22:50,240 --> 06:22:52,160
there's a lot here i'll post all of this
10084
06:22:52,160 --> 06:22:53,920
in the comment section down below and
10085
06:22:53,920 --> 06:22:55,840
well yeah that's an introduction to
10086
06:22:55,840 --> 06:22:58,000
cookies using javascript
10087
06:22:58,000 --> 06:22:59,840
hey what's going on everybody it's you
10088
06:22:59,840 --> 06:23:01,520
bro hope you're doing well and in this
10089
06:23:01,520 --> 06:23:02,718
video we're going to create an
10090
06:23:02,718 --> 06:23:05,200
interactive stopwatch using javascript
10091
06:23:05,200 --> 06:23:09,520
so sit back relax and enjoy the show
10092
06:23:09,520 --> 06:23:12,000
sup guys let's create a stopwatch head
10093
06:23:12,000 --> 06:23:14,240
to your html file then we will create a
10094
06:23:14,240 --> 06:23:17,040
container to hold our stopwatch
10095
06:23:17,040 --> 06:23:18,160
div
10096
06:23:18,160 --> 06:23:21,440
id equals i'll name this time
10097
06:23:21,440 --> 06:23:22,798
container
10098
06:23:22,798 --> 06:23:24,718
then we're gonna close it within our
10099
06:23:24,718 --> 06:23:27,680
time container let's add a div for the
10100
06:23:27,680 --> 06:23:29,360
time display
10101
06:23:29,360 --> 06:23:34,478
id equals time display i'll add a time
10102
06:23:34,478 --> 06:23:35,760
zero zero
10103
06:23:35,760 --> 06:23:38,878
colon zero zero colon zero zero hours
10104
06:23:38,878 --> 06:23:40,638
minutes and seconds
10105
06:23:40,638 --> 06:23:42,958
let's close that div section we'll add
10106
06:23:42,958 --> 06:23:46,080
three buttons start pause and restart
10107
06:23:46,080 --> 06:23:49,040
button id equals
10108
06:23:49,040 --> 06:23:51,680
start button
10109
06:23:51,680 --> 06:23:53,920
i'll put this within a class class
10110
06:23:53,920 --> 06:23:55,200
equals
10111
06:23:55,200 --> 06:23:57,840
timer button
10112
06:23:57,840 --> 06:24:00,878
let's close the button add some text
10113
06:24:00,878 --> 06:24:02,878
the text will be start
10114
06:24:02,878 --> 06:24:04,878
let's copy this button
10115
06:24:04,878 --> 06:24:06,798
paste it two times
10116
06:24:06,798 --> 06:24:10,478
the second button is a pause button
10117
06:24:10,478 --> 06:24:13,120
the text will be pause
10118
06:24:13,120 --> 06:24:16,400
next will be a reset button reset the
10119
06:24:16,400 --> 06:24:19,040
text is reset
10120
06:24:19,040 --> 06:24:20,958
that is everything for our html file
10121
06:24:20,958 --> 06:24:23,440
let's head to our css file let's begin
10122
06:24:23,440 --> 06:24:26,080
by adding some properties to the timer
10123
06:24:26,080 --> 06:24:27,920
button class
10124
06:24:27,920 --> 06:24:29,760
i'll set a width
10125
06:24:29,760 --> 06:24:33,200
of 80 pixels
10126
06:24:33,360 --> 06:24:34,558
a height
10127
06:24:34,558 --> 06:24:37,600
of 30 pixels
10128
06:24:38,080 --> 06:24:40,400
a border
10129
06:24:40,400 --> 06:24:44,240
3 pixels solid is good
10130
06:24:45,680 --> 06:24:49,200
a border radius of
10131
06:24:49,200 --> 06:24:51,920
12 pixels
10132
06:24:51,920 --> 06:24:54,878
a background color
10133
06:24:55,360 --> 06:24:58,558
uh let's select a hexadecimal value pick
10134
06:24:58,558 --> 06:25:00,080
whatever color you want i'm going to
10135
06:25:00,080 --> 06:25:02,558
pick a dark gray color you can always
10136
06:25:02,558 --> 06:25:04,958
use a color picker to select the color
10137
06:25:04,958 --> 06:25:08,080
i'll set the color to be white
10138
06:25:08,080 --> 06:25:11,600
i'll set the cursor to be a pointer
10139
06:25:11,600 --> 06:25:13,280
when we hover our cursor over that
10140
06:25:13,280 --> 06:25:16,400
button then we have a cursor pointer
10141
06:25:16,400 --> 06:25:19,040
then i'll change the font family font
10142
06:25:19,040 --> 06:25:20,798
family
10143
06:25:20,798 --> 06:25:22,718
i'll pick this font
10144
06:25:22,718 --> 06:25:26,400
with the backup of monospace
10145
06:25:26,400 --> 06:25:27,680
yeah cool
10146
06:25:27,680 --> 06:25:30,638
let's change the time display right here
10147
06:25:30,638 --> 06:25:32,878
this is an id
10148
06:25:32,878 --> 06:25:36,160
time display
10149
06:25:36,160 --> 06:25:40,558
i'll edit the font size font dash size
10150
06:25:40,558 --> 06:25:44,320
this will be 75 pixels
10151
06:25:44,320 --> 06:25:46,320
i'll select a color
10152
06:25:46,320 --> 06:25:48,958
uh go ahead and pick a color
10153
06:25:48,958 --> 06:25:51,760
i'll select maybe
10154
06:25:51,760 --> 06:25:55,478
that that looks good
10155
06:25:58,080 --> 06:26:02,240
sweet i'll change the font family
10156
06:26:02,878 --> 06:26:04,320
then let's add a background color and
10157
06:26:04,320 --> 06:26:06,000
center everything
10158
06:26:06,000 --> 06:26:10,478
this will be our time container
10159
06:26:10,718 --> 06:26:12,878
text align
10160
06:26:12,878 --> 06:26:15,280
center
10161
06:26:15,360 --> 06:26:17,360
i'll add a border
10162
06:26:17,360 --> 06:26:20,798
3 pixel solid
10163
06:26:20,878 --> 06:26:21,920
border
10164
06:26:21,920 --> 06:26:25,520
radius of 25 pixels
10165
06:26:25,520 --> 06:26:27,200
background
10166
06:26:27,200 --> 06:26:29,040
color
10167
06:26:29,040 --> 06:26:33,440
of this is a dark gray color six twos
10168
06:26:33,440 --> 06:26:35,760
yeah there we go so there's our css
10169
06:26:35,760 --> 06:26:39,280
styling our css styling is done our html
10170
06:26:39,280 --> 06:26:40,638
file is done let's head to our
10171
06:26:40,638 --> 06:26:42,080
javascript file
10172
06:26:42,080 --> 06:26:43,520
let's select all the elements that we'll
10173
06:26:43,520 --> 06:26:46,798
need first is the time display let's get
10174
06:26:46,798 --> 06:26:47,840
that id
10175
06:26:47,840 --> 06:26:50,000
time display i'll store this as a
10176
06:26:50,000 --> 06:26:54,000
constant const time display equals
10177
06:26:54,000 --> 06:26:55,440
document
10178
06:26:55,440 --> 06:26:56,638
dot
10179
06:26:56,638 --> 06:26:57,760
query
10180
06:26:57,760 --> 06:27:00,400
selector
10181
06:27:00,558 --> 06:27:02,478
we're selecting an id
10182
06:27:02,478 --> 06:27:06,240
the id is time display
10183
06:27:06,240 --> 06:27:09,280
next is our start button let's copy this
10184
06:27:09,280 --> 06:27:11,520
paste it
10185
06:27:11,520 --> 06:27:12,638
start
10186
06:27:12,638 --> 06:27:15,040
button
10187
06:27:15,040 --> 06:27:16,000
start
10188
06:27:16,000 --> 06:27:18,400
button
10189
06:27:19,200 --> 06:27:20,840
then our pause
10190
06:27:20,840 --> 06:27:24,638
button pause button
10191
06:27:24,638 --> 06:27:26,400
pause button
10192
06:27:26,400 --> 06:27:28,558
then reset button
10193
06:27:28,558 --> 06:27:31,760
reset button
10194
06:27:31,760 --> 06:27:34,400
the id is reset button
10195
06:27:34,400 --> 06:27:36,718
here's the variables we'll need
10196
06:27:36,718 --> 06:27:37,680
let
10197
06:27:37,680 --> 06:27:39,360
start time
10198
06:27:39,360 --> 06:27:40,798
i'll go ahead and set this to zero right
10199
06:27:40,798 --> 06:27:42,320
away
10200
06:27:42,320 --> 06:27:43,360
let
10201
06:27:43,360 --> 06:27:45,280
elapsed time
10202
06:27:45,280 --> 06:27:47,920
equal zero
10203
06:27:47,920 --> 06:27:49,040
let
10204
06:27:49,040 --> 06:27:50,558
current time
10205
06:27:50,558 --> 06:27:52,958
equal zero
10206
06:27:52,958 --> 06:27:55,520
let paused this will be a boolean
10207
06:27:55,520 --> 06:27:56,798
variable
10208
06:27:56,798 --> 06:27:58,718
if our timer is currently paused we'll
10209
06:27:58,718 --> 06:28:00,478
set this to be true false if it's
10210
06:28:00,478 --> 06:28:02,000
running
10211
06:28:02,000 --> 06:28:03,120
let
10212
06:28:03,120 --> 06:28:05,280
interval id
10213
06:28:05,280 --> 06:28:08,558
let hours hrs is fine
10214
06:28:08,558 --> 06:28:10,240
equals zero
10215
06:28:10,240 --> 06:28:11,280
let
10216
06:28:11,280 --> 06:28:14,000
mins for minutes equals zero
10217
06:28:14,000 --> 06:28:17,120
let then sec four seconds
10218
06:28:17,120 --> 06:28:19,120
equals zero here's what we're going to
10219
06:28:19,120 --> 06:28:20,718
do we're going to add event listeners to
10220
06:28:20,718 --> 06:28:22,240
each of our buttons
10221
06:28:22,240 --> 06:28:25,680
start pause and reset
10222
06:28:25,760 --> 06:28:26,718
add
10223
06:28:26,718 --> 06:28:28,160
event
10224
06:28:28,160 --> 06:28:30,638
listener
10225
06:28:31,360 --> 06:28:34,798
i'll fill these in momentarily
10226
06:28:36,240 --> 06:28:37,920
pause button
10227
06:28:37,920 --> 06:28:39,680
then reset button
10228
06:28:39,680 --> 06:28:41,520
then we'll declare a function to update
10229
06:28:41,520 --> 06:28:43,040
our time
10230
06:28:43,040 --> 06:28:44,240
function
10231
06:28:44,240 --> 06:28:47,040
update time
10232
06:28:47,040 --> 06:28:48,638
let's fill in these event listeners
10233
06:28:48,638 --> 06:28:50,080
these will each be
10234
06:28:50,080 --> 06:28:51,920
click
10235
06:28:51,920 --> 06:28:55,680
we'll use an arrow function expression
10236
06:28:55,840 --> 06:29:00,320
i'll just copy this and paste it
10237
06:29:00,320 --> 06:29:04,080
okay let's begin with our start button
10238
06:29:04,080 --> 06:29:08,240
we'll check to see if paused is true
10239
06:29:08,240 --> 06:29:09,360
if
10240
06:29:09,360 --> 06:29:12,000
paused
10241
06:29:13,280 --> 06:29:15,520
we'll take paused
10242
06:29:15,520 --> 06:29:18,558
set it to false
10243
06:29:19,120 --> 06:29:21,120
calculate the start time
10244
06:29:21,120 --> 06:29:26,320
start time equals date dot now method
10245
06:29:26,320 --> 06:29:28,718
the now method of date will give you the
10246
06:29:28,718 --> 06:29:31,760
current date and time in milliseconds
10247
06:29:31,760 --> 06:29:34,000
minus elapsed
10248
06:29:34,000 --> 06:29:36,400
time which will initially be zero to
10249
06:29:36,400 --> 06:29:37,920
begin with
10250
06:29:37,920 --> 06:29:40,400
then we'll begin our timer
10251
06:29:40,400 --> 06:29:43,600
interval id
10252
06:29:43,600 --> 06:29:45,840
equals set
10253
06:29:45,840 --> 06:29:47,600
interval
10254
06:29:47,600 --> 06:29:49,520
we'll need a callback the callback will
10255
06:29:49,520 --> 06:29:51,680
be update time
10256
06:29:51,680 --> 06:29:54,080
let's invoke this function every maybe
10257
06:29:54,080 --> 06:29:56,878
75 milliseconds that should be good
10258
06:29:56,878 --> 06:29:58,638
enough
10259
06:29:58,638 --> 06:30:01,600
let's fill in the update time function
10260
06:30:01,600 --> 06:30:04,878
we'll calculate how much time is passed
10261
06:30:04,878 --> 06:30:06,400
elapsed time
10262
06:30:06,400 --> 06:30:07,520
equals
10263
06:30:07,520 --> 06:30:10,638
whatever time it is right now date.now
10264
06:30:10,638 --> 06:30:12,160
method
10265
06:30:12,160 --> 06:30:14,718
minus the original start time
10266
06:30:14,718 --> 06:30:17,920
this will be a time in milliseconds
10267
06:30:17,920 --> 06:30:19,760
we'll have to format it so that we can
10268
06:30:19,760 --> 06:30:22,240
display it within our timer
10269
06:30:22,240 --> 06:30:24,478
let's take our seconds
10270
06:30:24,478 --> 06:30:26,440
set the sql to
10271
06:30:26,440 --> 06:30:29,840
math.floor method
10272
06:30:30,000 --> 06:30:34,160
we will pass in our elapsed time
10273
06:30:34,320 --> 06:30:36,798
divided by 1000 because it's normally in
10274
06:30:36,798 --> 06:30:41,280
milliseconds modulus 60.
10275
06:30:41,280 --> 06:30:42,638
i'm going to put these within
10276
06:30:42,638 --> 06:30:44,160
parentheses
10277
06:30:44,160 --> 06:30:47,840
it's a similar process for minutes
10278
06:30:48,080 --> 06:30:51,040
except this section is 1000
10279
06:30:51,040 --> 06:30:52,798
times 60.
10280
06:30:52,798 --> 06:30:55,200
that will be 60 000 milliseconds in
10281
06:30:55,200 --> 06:30:56,958
every minute
10282
06:30:56,958 --> 06:31:00,320
let's calculate the hours
10283
06:31:03,600 --> 06:31:05,600
hours
10284
06:31:05,600 --> 06:31:08,400
1000 times 60
10285
06:31:08,400 --> 06:31:10,638
times 60.
10286
06:31:10,638 --> 06:31:13,840
then we'll need to update our display
10287
06:31:13,840 --> 06:31:17,200
that is time display
10288
06:31:17,520 --> 06:31:19,360
dot text
10289
06:31:19,360 --> 06:31:20,718
content
10290
06:31:20,718 --> 06:31:23,280
equals i'll use a template literal
10291
06:31:23,280 --> 06:31:26,400
will display the hours
10292
06:31:26,400 --> 06:31:28,160
colon
10293
06:31:28,160 --> 06:31:30,798
the minutes
10294
06:31:31,200 --> 06:31:33,440
then the seconds
10295
06:31:33,440 --> 06:31:35,040
let's take a look to see what we have so
10296
06:31:35,040 --> 06:31:36,798
far
10297
06:31:36,798 --> 06:31:38,240
we'll have one issue
10298
06:31:38,240 --> 06:31:40,160
let's press start
10299
06:31:40,160 --> 06:31:42,080
you can see that it's currently running
10300
06:31:42,080 --> 06:31:44,160
but when we display zeros i would like
10301
06:31:44,160 --> 06:31:46,718
two zeros let's add a zero as padding
10302
06:31:46,718 --> 06:31:48,798
for any single digit numbers
10303
06:31:48,798 --> 06:31:51,280
i'll create an inner function
10304
06:31:51,280 --> 06:31:52,718
function
10305
06:31:52,718 --> 06:31:53,600
pad
10306
06:31:53,600 --> 06:31:57,040
will accept a unit
10307
06:31:57,680 --> 06:32:00,718
we will return
10308
06:32:00,718 --> 06:32:04,080
this will be a ternary operator
10309
06:32:04,080 --> 06:32:05,360
it might be a little difficult to
10310
06:32:05,360 --> 06:32:06,478
understand
10311
06:32:06,478 --> 06:32:08,718
so we're going to add a zero to the
10312
06:32:08,718 --> 06:32:11,360
front of our unit whatever we pass in
10313
06:32:11,360 --> 06:32:13,840
hours minutes and seconds
10314
06:32:13,840 --> 06:32:16,798
we'll access the length property
10315
06:32:16,798 --> 06:32:19,600
if we add a zero to our unit
10316
06:32:19,600 --> 06:32:22,080
what's the length is that length greater
10317
06:32:22,080 --> 06:32:24,638
than two question mark
10318
06:32:24,638 --> 06:32:25,920
if it is
10319
06:32:25,920 --> 06:32:28,958
we'll simply return unit
10320
06:32:28,958 --> 06:32:30,798
otherwise
10321
06:32:30,798 --> 06:32:35,360
we'll prepend a zero plus unit
10322
06:32:35,440 --> 06:32:39,040
then we'll invoke the pad function
10323
06:32:39,040 --> 06:32:43,360
our seconds equals invoke pad
10324
06:32:43,360 --> 06:32:45,600
pass in our seconds
10325
06:32:45,600 --> 06:32:49,440
do the same thing with minutes and hours
10326
06:32:50,240 --> 06:32:52,638
minutes
10327
06:32:55,280 --> 06:32:56,798
hours
10328
06:32:56,798 --> 06:33:00,240
then let's move this line down
10329
06:33:00,638 --> 06:33:02,718
right about here
10330
06:33:02,718 --> 06:33:04,320
okay we should have some zeros as
10331
06:33:04,320 --> 06:33:06,798
padding now
10332
06:33:06,798 --> 06:33:09,840
yeah there we go one two three
10333
06:33:09,840 --> 06:33:11,440
so we have no way to pause this timer
10334
06:33:11,440 --> 06:33:13,360
let's work on that next
10335
06:33:13,360 --> 06:33:16,160
let's head to our pause button
10336
06:33:16,160 --> 06:33:19,280
we'll check to see if
10337
06:33:19,280 --> 06:33:20,240
not
10338
06:33:20,240 --> 06:33:22,718
paused
10339
06:33:23,200 --> 06:33:27,840
then we will set paused equal to true
10340
06:33:28,400 --> 06:33:32,000
we'll calculate the elapsed time
10341
06:33:32,638 --> 06:33:35,840
equals date dot now
10342
06:33:35,840 --> 06:33:39,040
minus our start time
10343
06:33:39,040 --> 06:33:41,360
this will save how much time has passed
10344
06:33:41,360 --> 06:33:43,040
in milliseconds
10345
06:33:43,040 --> 06:33:47,040
then clear our set interval method
10346
06:33:47,360 --> 06:33:48,958
clear
10347
06:33:48,958 --> 06:33:50,000
interval
10348
06:33:50,000 --> 06:33:53,280
we'll pass our id as an argument
10349
06:33:53,280 --> 06:33:54,958
okay now we should be able to start our
10350
06:33:54,958 --> 06:33:56,958
timer
10351
06:33:56,958 --> 06:33:57,840
one
10352
06:33:57,840 --> 06:33:58,638
two
10353
06:33:58,638 --> 06:34:01,200
three and pause
10354
06:34:01,200 --> 06:34:03,200
and you can see that it's paused
10355
06:34:03,200 --> 06:34:05,840
lastly reset this one's kind of easy
10356
06:34:05,840 --> 06:34:09,120
head to our reset button
10357
06:34:09,120 --> 06:34:10,878
we'll take paused
10358
06:34:10,878 --> 06:34:13,280
set that to true
10359
06:34:13,280 --> 06:34:16,000
clear our timer
10360
06:34:16,000 --> 06:34:18,878
take start elapsed and current time
10361
06:34:18,878 --> 06:34:22,600
set them all to zero
10362
06:34:22,718 --> 06:34:24,878
take hours minutes and seconds
10363
06:34:24,878 --> 06:34:27,840
set them to zero
10364
06:34:28,478 --> 06:34:30,798
then lastly change our time display to
10365
06:34:30,798 --> 06:34:32,958
all zeros
10366
06:34:32,958 --> 06:34:34,160
time
10367
06:34:34,160 --> 06:34:35,600
display
10368
06:34:35,600 --> 06:34:37,040
dot text
10369
06:34:37,040 --> 06:34:38,558
content
10370
06:34:38,558 --> 06:34:39,920
equals
10371
06:34:39,920 --> 06:34:42,718
zero zero colon zero zero colon zero
10372
06:34:42,718 --> 06:34:43,680
zero
10373
06:34:43,680 --> 06:34:45,680
oh then make sure you don't add this let
10374
06:34:45,680 --> 06:34:47,520
keyword then we're creating a local
10375
06:34:47,520 --> 06:34:50,240
variable
10376
06:34:50,240 --> 06:34:53,040
this should work now
10377
06:34:53,120 --> 06:34:56,400
one two three we can pause
10378
06:34:56,400 --> 06:35:00,160
we can reset we can start again
10379
06:35:00,160 --> 06:35:02,558
and reset all right everybody that is a
10380
06:35:02,558 --> 06:35:04,638
simple timer if you'd like to make a
10381
06:35:04,638 --> 06:35:06,160
more advanced version you could add
10382
06:35:06,160 --> 06:35:08,400
milliseconds and well yeah that's a
10383
06:35:08,400 --> 06:35:11,520
basic stopwatch using javascript
10384
06:35:11,520 --> 06:35:13,120
hey guys in this video we're going to
10385
06:35:13,120 --> 06:35:15,200
create a game of rock paper scissors
10386
06:35:15,200 --> 06:35:18,080
using javascript so sit back relax and
10387
06:35:18,080 --> 06:35:20,798
enjoy the show
10388
06:35:20,878 --> 06:35:22,878
all right then everybody in this video
10389
06:35:22,878 --> 06:35:24,718
we're going to be creating a game of
10390
06:35:24,718 --> 06:35:27,360
rock paper scissors we'll be using html
10391
06:35:27,360 --> 06:35:29,920
elements javascript code and styling
10392
06:35:29,920 --> 06:35:31,360
with css
10393
06:35:31,360 --> 06:35:33,680
let's head to our html document
10394
06:35:33,680 --> 06:35:35,760
i'll create a div section to contain our
10395
06:35:35,760 --> 06:35:36,878
game
10396
06:35:36,878 --> 06:35:38,240
div
10397
06:35:38,240 --> 06:35:42,000
id equals i'll name this game
10398
06:35:42,000 --> 06:35:43,760
div
10399
06:35:43,760 --> 06:35:47,478
we'll close it
10400
06:35:48,080 --> 06:35:50,080
i'll create three labels one for the
10401
06:35:50,080 --> 06:35:52,878
player the computer and the result who
10402
06:35:52,878 --> 06:35:54,638
won
10403
06:35:54,638 --> 06:35:59,000
these will be h1 header tags
10404
06:36:01,440 --> 06:36:03,120
i'll give each of these header tags a
10405
06:36:03,120 --> 06:36:04,080
class
10406
06:36:04,080 --> 06:36:06,080
class equals
10407
06:36:06,080 --> 06:36:07,280
game
10408
06:36:07,280 --> 06:36:09,680
text
10409
06:36:12,558 --> 06:36:14,718
i'll give the first h1 header tag a
10410
06:36:14,718 --> 06:36:18,638
unique id of player text
10411
06:36:18,638 --> 06:36:21,200
the second header tag will be computer
10412
06:36:21,200 --> 06:36:23,280
text
10413
06:36:23,280 --> 06:36:26,240
third will be
10414
06:36:26,478 --> 06:36:29,840
result text
10415
06:36:30,000 --> 06:36:32,160
the first h1 header tag will have this
10416
06:36:32,160 --> 06:36:34,240
initial text
10417
06:36:34,240 --> 06:36:37,440
player colon space
10418
06:36:37,440 --> 06:36:38,478
then
10419
06:36:38,478 --> 06:36:39,600
computer
10420
06:36:39,600 --> 06:36:42,798
colon space
10421
06:36:42,798 --> 06:36:45,680
result colon space
10422
06:36:45,680 --> 06:36:47,280
let's add three buttons
10423
06:36:47,280 --> 06:36:50,718
for rock paper scissors
10424
06:36:53,280 --> 06:36:56,878
button close it
10425
06:36:56,878 --> 06:36:58,958
each of these buttons will have a class
10426
06:36:58,958 --> 06:36:59,840
class
10427
06:36:59,840 --> 06:37:01,360
equals
10428
06:37:01,360 --> 06:37:03,120
choice
10429
06:37:03,120 --> 06:37:05,520
button
10430
06:37:06,160 --> 06:37:08,878
we'll need three buttons
10431
06:37:08,878 --> 06:37:11,440
add some text
10432
06:37:11,440 --> 06:37:13,760
rock
10433
06:37:14,080 --> 06:37:16,558
paper
10434
06:37:17,360 --> 06:37:19,840
scissors
10435
06:37:19,840 --> 06:37:21,680
that's everything for now
10436
06:37:21,680 --> 06:37:24,400
let's head to our javascript file
10437
06:37:24,400 --> 06:37:25,920
let's select all of the elements that
10438
06:37:25,920 --> 06:37:27,280
we'll need
10439
06:37:27,280 --> 06:37:30,000
we'll begin with the player text
10440
06:37:30,000 --> 06:37:31,040
const
10441
06:37:31,040 --> 06:37:32,798
player text
10442
06:37:32,798 --> 06:37:35,840
equals document dot
10443
06:37:35,840 --> 06:37:37,120
query
10444
06:37:37,120 --> 06:37:38,478
selector
10445
06:37:38,478 --> 06:37:40,320
we are selecting
10446
06:37:40,320 --> 06:37:45,760
an id that id was player text
10447
06:37:45,840 --> 06:37:48,240
we'll select the computer text
10448
06:37:48,240 --> 06:37:50,558
const
10449
06:37:50,558 --> 06:37:56,160
computer text the id was computer text
10450
06:37:56,160 --> 06:37:59,840
we'll select the result text
10451
06:38:00,240 --> 06:38:02,718
const
10452
06:38:02,878 --> 06:38:07,920
result text the id was result text
10453
06:38:07,920 --> 06:38:11,200
then we will select all of the buttons
10454
06:38:11,200 --> 06:38:12,718
const
10455
06:38:12,718 --> 06:38:14,798
choice
10456
06:38:14,798 --> 06:38:16,878
buttons
10457
06:38:16,878 --> 06:38:18,160
equals
10458
06:38:18,160 --> 06:38:20,558
document dot
10459
06:38:20,558 --> 06:38:21,680
query
10460
06:38:21,680 --> 06:38:23,120
selector
10461
06:38:23,120 --> 06:38:25,360
all
10462
06:38:25,600 --> 06:38:30,400
we are selecting the choice button class
10463
06:38:30,400 --> 06:38:33,120
we'll create three variables let player
10464
06:38:33,120 --> 06:38:35,200
to store the player's choice
10465
06:38:35,200 --> 06:38:36,638
let computer
10466
06:38:36,638 --> 06:38:39,200
for the computer's random choice
10467
06:38:39,200 --> 06:38:41,040
let result
10468
06:38:41,040 --> 06:38:43,360
to display who won
10469
06:38:43,360 --> 06:38:45,280
we'll iterate over our choice buttons
10470
06:38:45,280 --> 06:38:47,360
using the for each method
10471
06:38:47,360 --> 06:38:49,120
choice buttons
10472
06:38:49,120 --> 06:38:51,760
for each
10473
06:38:51,760 --> 06:38:55,280
we'll use an arrow function expression
10474
06:38:55,280 --> 06:38:57,360
for each button within our choice
10475
06:38:57,360 --> 06:38:59,600
buttons array
10476
06:38:59,600 --> 06:39:02,240
we will use an arrow function i'm going
10477
06:39:02,240 --> 06:39:03,840
to add an event listener to each of
10478
06:39:03,840 --> 06:39:05,760
these buttons within the arrow function
10479
06:39:05,760 --> 06:39:07,200
expression
10480
06:39:07,200 --> 06:39:08,080
button
10481
06:39:08,080 --> 06:39:09,120
dot
10482
06:39:09,120 --> 06:39:10,160
add
10483
06:39:10,160 --> 06:39:11,680
event
10484
06:39:11,680 --> 06:39:14,160
listener
10485
06:39:14,160 --> 06:39:15,680
the event is
10486
06:39:15,680 --> 06:39:16,638
click
10487
06:39:16,638 --> 06:39:18,320
when we click on a button we're going to
10488
06:39:18,320 --> 06:39:20,000
do something
10489
06:39:20,000 --> 06:39:22,160
within the ad event listener we'll add a
10490
06:39:22,160 --> 06:39:24,160
second arrow function expression
10491
06:39:24,160 --> 06:39:26,718
there are no arguments
10492
06:39:26,718 --> 06:39:29,718
arrow
10493
06:39:30,000 --> 06:39:31,760
we're gonna do a bunch of stuff
10494
06:39:31,760 --> 06:39:33,840
so let's expand this
10495
06:39:33,840 --> 06:39:35,360
when we click on a button what are we
10496
06:39:35,360 --> 06:39:36,638
going to do
10497
06:39:36,638 --> 06:39:38,718
let's take our player
10498
06:39:38,718 --> 06:39:40,958
this is our player's choice
10499
06:39:40,958 --> 06:39:42,718
set the sequel to
10500
06:39:42,718 --> 06:39:45,120
whatever button we select
10501
06:39:45,120 --> 06:39:49,440
and get the text content
10502
06:39:49,440 --> 06:39:51,600
if we click on this rock button then our
10503
06:39:51,600 --> 06:39:54,478
player variable contains the string rock
10504
06:39:54,478 --> 06:39:56,478
same thing goes with paper and scissors
10505
06:39:56,478 --> 06:39:59,040
if we select those then we will invoke
10506
06:39:59,040 --> 06:40:01,600
the computer's turn
10507
06:40:01,600 --> 06:40:02,878
computer
10508
06:40:02,878 --> 06:40:04,000
turn
10509
06:40:04,000 --> 06:40:05,120
function
10510
06:40:05,120 --> 06:40:08,240
we'll need to declare this function
10511
06:40:08,240 --> 06:40:10,400
outside of our for each method let's
10512
06:40:10,400 --> 06:40:12,638
declare this computer turn function
10513
06:40:12,638 --> 06:40:16,160
function computer turn
10514
06:40:16,160 --> 06:40:18,000
we'll select a random number between one
10515
06:40:18,000 --> 06:40:20,558
and three
10516
06:40:21,520 --> 06:40:25,120
const brand number
10517
06:40:25,120 --> 06:40:26,400
equals
10518
06:40:26,400 --> 06:40:29,400
math.floor
10519
06:40:29,760 --> 06:40:32,120
we'll need a random number
10520
06:40:32,120 --> 06:40:35,280
math.random method
10521
06:40:35,280 --> 06:40:36,878
there's only three choices we'll
10522
06:40:36,878 --> 06:40:40,240
multiply this by three
10523
06:40:40,240 --> 06:40:41,920
then add one
10524
06:40:41,920 --> 06:40:43,280
this will give us a random number
10525
06:40:43,280 --> 06:40:45,120
between one and three
10526
06:40:45,120 --> 06:40:46,798
we can examine this random number with a
10527
06:40:46,798 --> 06:40:48,878
switch
10528
06:40:48,878 --> 06:40:50,558
switch
10529
06:40:50,558 --> 06:40:53,040
rand number
10530
06:40:53,040 --> 06:40:56,160
we'll find any matching cases
10531
06:40:56,160 --> 06:40:58,718
case one
10532
06:40:58,718 --> 06:41:00,558
computer
10533
06:41:00,558 --> 06:41:02,798
will equal the string
10534
06:41:02,798 --> 06:41:04,558
rock
10535
06:41:04,558 --> 06:41:07,958
then break
10536
06:41:09,520 --> 06:41:14,240
case two computer equals paper
10537
06:41:16,558 --> 06:41:21,520
case three computer equals scissors
10538
06:41:21,520 --> 06:41:23,360
and that's it for the computer turn
10539
06:41:23,360 --> 06:41:24,638
function
10540
06:41:24,638 --> 06:41:26,240
the player variable will be assigned a
10541
06:41:26,240 --> 06:41:28,240
string as well as the computer
10542
06:41:28,240 --> 06:41:29,840
next what we'll need to do is set the
10543
06:41:29,840 --> 06:41:32,240
text with our choices
10544
06:41:32,240 --> 06:41:35,280
player text dot text
10545
06:41:35,280 --> 06:41:36,558
content
10546
06:41:36,558 --> 06:41:40,240
equals i'll use a template literal
10547
06:41:40,240 --> 06:41:43,360
player colon space
10548
06:41:43,360 --> 06:41:45,360
i'll add a placeholder
10549
06:41:45,360 --> 06:41:47,760
player
10550
06:41:47,760 --> 06:41:51,840
let's do the same thing with computer
10551
06:41:52,000 --> 06:41:53,440
computer text
10552
06:41:53,440 --> 06:41:55,600
text content
10553
06:41:55,600 --> 06:41:58,160
computer
10554
06:41:58,160 --> 06:42:00,558
computer
10555
06:42:00,558 --> 06:42:04,360
then the results text
10556
06:42:06,320 --> 06:42:09,840
result text dot text content
10557
06:42:09,840 --> 06:42:12,478
we will set the sql to
10558
06:42:12,478 --> 06:42:16,718
invoke a check winner function
10559
06:42:16,718 --> 06:42:18,958
we'll need to define this function
10560
06:42:18,958 --> 06:42:21,280
let's do that after the computer turn
10561
06:42:21,280 --> 06:42:23,440
function
10562
06:42:23,440 --> 06:42:24,558
function
10563
06:42:24,558 --> 06:42:27,280
check winner
10564
06:42:28,478 --> 06:42:30,478
first we'll compare if our player and
10565
06:42:30,478 --> 06:42:32,478
computer strings are the same
10566
06:42:32,478 --> 06:42:33,920
if player
10567
06:42:33,920 --> 06:42:36,958
is equal to computer
10568
06:42:36,958 --> 06:42:38,798
we will return a value
10569
06:42:38,798 --> 06:42:40,638
this will be a string
10570
06:42:40,638 --> 06:42:43,840
draw it's a tie
10571
06:42:44,478 --> 06:42:47,840
else if
10572
06:42:47,840 --> 06:42:49,280
computer
10573
06:42:49,280 --> 06:42:51,200
is equal to
10574
06:42:51,200 --> 06:42:53,600
rock
10575
06:42:55,200 --> 06:42:56,798
i'm going to return
10576
06:42:56,798 --> 06:42:59,200
then use the ternary operator our
10577
06:42:59,200 --> 06:43:01,200
condition is if
10578
06:43:01,200 --> 06:43:02,240
player
10579
06:43:02,240 --> 06:43:04,878
is equal to paper
10580
06:43:04,878 --> 06:43:07,120
question mark
10581
06:43:07,120 --> 06:43:09,440
if the computer is equal to rock
10582
06:43:09,440 --> 06:43:11,600
is the player equal to paper
10583
06:43:11,600 --> 06:43:15,280
if so then we will return a string
10584
06:43:15,280 --> 06:43:16,320
you
10585
06:43:16,320 --> 06:43:18,080
win
10586
06:43:18,080 --> 06:43:20,240
if this condition is false we'll return
10587
06:43:20,240 --> 06:43:22,000
a different string
10588
06:43:22,000 --> 06:43:23,200
you
10589
06:43:23,200 --> 06:43:25,440
lose
10590
06:43:25,440 --> 06:43:26,958
okay let's create another elsif
10591
06:43:26,958 --> 06:43:30,878
statement else if computer is equal to
10592
06:43:30,878 --> 06:43:31,840
paper
10593
06:43:31,840 --> 06:43:34,638
and the player is equal to
10594
06:43:34,638 --> 06:43:37,200
scissors
10595
06:43:38,878 --> 06:43:41,040
add another else if statement
10596
06:43:41,040 --> 06:43:44,160
else if the computer is equal to
10597
06:43:44,160 --> 06:43:49,520
scissors and the player is equal to rock
10598
06:43:49,520 --> 06:43:52,958
and that should be it let's test it
10599
06:43:52,958 --> 06:43:54,718
okay rock
10600
06:43:54,718 --> 06:43:58,240
it's a draw paper you lose
10601
06:43:58,240 --> 06:44:00,718
scissors you win
10602
06:44:00,718 --> 06:44:03,600
alright so that is the logic of our game
10603
06:44:03,600 --> 06:44:08,000
hey for fun let's style this using css
10604
06:44:09,120 --> 06:44:11,040
let's begin with our game development
10605
06:44:11,040 --> 06:44:13,760
that surrounds our game
10606
06:44:13,760 --> 06:44:16,718
let's take this id we will style game
10607
06:44:16,718 --> 06:44:18,718
div it's an id
10608
06:44:18,718 --> 06:44:20,718
i'll set the font
10609
06:44:20,718 --> 06:44:21,840
family
10610
06:44:21,840 --> 06:44:24,718
to
10611
06:44:24,718 --> 06:44:25,920
brush
10612
06:44:25,920 --> 06:44:27,440
script
10613
06:44:27,440 --> 06:44:29,360
mt
10614
06:44:29,360 --> 06:44:31,120
if my web browser can't display this
10615
06:44:31,120 --> 06:44:36,080
font i'll have a fallback of cursive
10616
06:44:36,240 --> 06:44:38,958
i'll add a border
10617
06:44:38,958 --> 06:44:42,558
three pixels solid
10618
06:44:42,638 --> 06:44:45,840
i'll round the corners
10619
06:44:45,840 --> 06:44:47,120
border
10620
06:44:47,120 --> 06:44:48,558
radius
10621
06:44:48,558 --> 06:44:51,280
25 pixels
10622
06:44:51,280 --> 06:44:54,718
that's better but let's add some padding
10623
06:44:54,718 --> 06:44:56,000
padding
10624
06:44:56,000 --> 06:44:58,718
10 pixels is good
10625
06:44:58,718 --> 06:45:01,360
i'll add a background color
10626
06:45:01,360 --> 06:45:03,600
background dash color
10627
06:45:03,600 --> 06:45:07,280
maybe light gray
10628
06:45:07,280 --> 06:45:10,160
i'll align the text in the center
10629
06:45:10,160 --> 06:45:11,280
text
10630
06:45:11,280 --> 06:45:12,878
align
10631
06:45:12,878 --> 06:45:14,638
center
10632
06:45:14,638 --> 06:45:17,200
okay we're getting somewhere
10633
06:45:17,200 --> 06:45:19,440
let's select these buttons they all have
10634
06:45:19,440 --> 06:45:21,360
the same class
10635
06:45:21,360 --> 06:45:24,400
that class is choice button
10636
06:45:24,400 --> 06:45:27,040
i like to add classes at the top
10637
06:45:27,040 --> 06:45:29,520
choice button class
10638
06:45:29,520 --> 06:45:31,840
let's change the line
10639
06:45:31,840 --> 06:45:33,520
height to
10640
06:45:33,520 --> 06:45:36,400
30 pixels
10641
06:45:37,920 --> 06:45:39,440
then set a width because all of these
10642
06:45:39,440 --> 06:45:41,040
buttons have different widths to
10643
06:45:41,040 --> 06:45:42,718
accommodate the text
10644
06:45:42,718 --> 06:45:44,958
i would like them all to be uniform
10645
06:45:44,958 --> 06:45:46,000
width
10646
06:45:46,000 --> 06:45:47,680
of
10647
06:45:47,680 --> 06:45:49,920
pixels is good
10648
06:45:49,920 --> 06:45:52,000
uh let's expand this a little bit
10649
06:45:52,000 --> 06:45:55,760
there let's select our player text
10650
06:45:55,760 --> 06:45:59,280
id player text
10651
06:45:59,360 --> 06:46:03,040
i'll change the color of the text color
10652
06:46:03,040 --> 06:46:04,400
pick a color
10653
06:46:04,400 --> 06:46:07,280
i'll be blue
10654
06:46:07,520 --> 06:46:09,040
let's do the same thing with computer
10655
06:46:09,040 --> 06:46:11,360
text
10656
06:46:12,160 --> 06:46:14,558
id computer text
10657
06:46:14,558 --> 06:46:17,280
i think the computer should be red
10658
06:46:17,280 --> 06:46:20,000
okay let's try this one last time
10659
06:46:20,000 --> 06:46:21,360
rock
10660
06:46:21,360 --> 06:46:22,798
paper
10661
06:46:22,798 --> 06:46:24,080
scissors
10662
06:46:24,080 --> 06:46:26,638
well alright everybody that is a basic
10663
06:46:26,638 --> 06:46:28,400
game of rock paper scissors for
10664
06:46:28,400 --> 06:46:29,440
beginners
10665
06:46:29,440 --> 06:46:30,718
if you would like a copy of my
10666
06:46:30,718 --> 06:46:33,280
javascript code as well as the html and
10667
06:46:33,280 --> 06:46:35,600
the css markup i'll post all of this in
10668
06:46:35,600 --> 06:46:37,440
the comments section down below and pin
10669
06:46:37,440 --> 06:46:39,200
it to the top be sure to check
10670
06:46:39,200 --> 06:46:40,878
underneath the original video in the
10671
06:46:40,878 --> 06:46:43,840
playlist but yeah that is a basic game
10672
06:46:43,840 --> 06:46:46,958
of rock paper scissors using javascript
10673
06:46:46,958 --> 06:46:48,400
hey what's going on everybody it's you
10674
06:46:48,400 --> 06:46:49,920
bro hope you're doing well and in this
10675
06:46:49,920 --> 06:46:51,600
video we're going to create a game of
10676
06:46:51,600 --> 06:46:54,558
tic-tac-toe using javascript so sit back
10677
06:46:54,558 --> 06:46:58,000
relax and enjoy the show
10678
06:46:58,000 --> 06:46:59,840
well all right everybody let's create a
10679
06:46:59,840 --> 06:47:02,320
game of tic-tac-toe let's create a div
10680
06:47:02,320 --> 06:47:04,400
element to contain our game
10681
06:47:04,400 --> 06:47:05,440
div
10682
06:47:05,440 --> 06:47:10,478
this will have an id of what about game
10683
06:47:10,478 --> 06:47:13,478
container
10684
06:47:13,680 --> 06:47:16,478
let's close it
10685
06:47:17,440 --> 06:47:19,280
i'll add a title
10686
06:47:19,280 --> 06:47:21,360
this will be an h1 header tag
10687
06:47:21,360 --> 06:47:23,040
the title will be
10688
06:47:23,040 --> 06:47:23,920
tick
10689
06:47:23,920 --> 06:47:24,840
tack
10690
06:47:24,840 --> 06:47:27,520
toe we'll create a div section for a
10691
06:47:27,520 --> 06:47:29,440
grid
10692
06:47:29,440 --> 06:47:32,240
div id equals
10693
06:47:32,240 --> 06:47:34,080
let's name this cell
10694
06:47:34,080 --> 06:47:35,280
container
10695
06:47:35,280 --> 06:47:37,440
it will contain nine cells for each of
10696
06:47:37,440 --> 06:47:39,440
the spaces
10697
06:47:39,440 --> 06:47:42,000
then let's close it
10698
06:47:42,000 --> 06:47:44,000
within our cell container div we're
10699
06:47:44,000 --> 06:47:46,558
going to create nine div tags
10700
06:47:46,558 --> 06:47:48,080
div
10701
06:47:48,080 --> 06:47:50,160
we'll create an attribute named
10702
06:47:50,160 --> 06:47:51,040
cell
10703
06:47:51,040 --> 06:47:52,240
index
10704
06:47:52,240 --> 06:47:55,360
i'll set the first equal to zero
10705
06:47:55,360 --> 06:47:57,840
add them to a class the class will be
10706
06:47:57,840 --> 06:47:59,520
cell
10707
06:47:59,520 --> 06:48:01,680
then close the div tag
10708
06:48:01,680 --> 06:48:04,400
let's copy this development and paste it
10709
06:48:04,400 --> 06:48:06,320
eight additional times so we should have
10710
06:48:06,320 --> 06:48:09,840
a total of nine
10711
06:48:11,200 --> 06:48:14,000
we have cell index zero one
10712
06:48:14,000 --> 06:48:16,160
two three four
10713
06:48:16,160 --> 06:48:17,200
five
10714
06:48:17,200 --> 06:48:18,240
six
10715
06:48:18,240 --> 06:48:19,200
seven
10716
06:48:19,200 --> 06:48:20,160
eight
10717
06:48:20,160 --> 06:48:22,718
outside of our cell container that ends
10718
06:48:22,718 --> 06:48:24,558
right here it looks like
10719
06:48:24,558 --> 06:48:27,200
we'll create an h2 header tag to display
10720
06:48:27,200 --> 06:48:30,638
some messages like whose turn it is who
10721
06:48:30,638 --> 06:48:31,760
won
10722
06:48:31,760 --> 06:48:34,000
so let's close this h2 header tag i will
10723
06:48:34,000 --> 06:48:38,798
give this an id of what about status
10724
06:48:38,798 --> 06:48:40,958
text
10725
06:48:40,958 --> 06:48:43,840
then we'll need a button
10726
06:48:44,638 --> 06:48:47,040
button
10727
06:48:47,760 --> 06:48:51,360
the id will be restart button
10728
06:48:51,360 --> 06:48:52,958
add some text
10729
06:48:52,958 --> 06:48:54,160
restart
10730
06:48:54,160 --> 06:48:55,840
okay that should be everything within
10731
06:48:55,840 --> 06:48:58,478
our html file let's head to our css
10732
06:48:58,478 --> 06:49:00,718
style sheet be sure to save everything
10733
06:49:00,718 --> 06:49:02,718
let's begin with the cell class
10734
06:49:02,718 --> 06:49:04,958
this is a class so use dot the name of
10735
06:49:04,958 --> 06:49:06,478
the class cell
10736
06:49:06,478 --> 06:49:10,240
i'll set a width of 75 pixels
10737
06:49:10,240 --> 06:49:11,840
a height
10738
06:49:11,840 --> 06:49:15,120
of 75 pixels
10739
06:49:15,440 --> 06:49:16,840
i'll add a
10740
06:49:16,840 --> 06:49:21,600
border 2 pixel solid is good
10741
06:49:22,878 --> 06:49:25,360
okay here's our div elements so far but
10742
06:49:25,360 --> 06:49:27,200
we got to put them in a grid they're all
10743
06:49:27,200 --> 06:49:28,558
in a single column
10744
06:49:28,558 --> 06:49:31,040
i'll select our cell container
10745
06:49:31,040 --> 06:49:33,760
we're selecting this by an id cell
10746
06:49:33,760 --> 06:49:36,000
container
10747
06:49:36,000 --> 06:49:37,760
the display
10748
06:49:37,760 --> 06:49:40,638
will be grid
10749
06:49:40,638 --> 06:49:42,320
grid
10750
06:49:42,320 --> 06:49:44,840
template
10751
06:49:44,840 --> 06:49:46,400
columns
10752
06:49:46,400 --> 06:49:48,400
repeat
10753
06:49:48,400 --> 06:49:49,440
three
10754
06:49:49,440 --> 06:49:51,920
auto
10755
06:49:52,080 --> 06:49:54,240
then we gotta set a width
10756
06:49:54,240 --> 06:49:55,680
width
10757
06:49:55,680 --> 06:49:58,958
225 pixels
10758
06:49:59,360 --> 06:50:00,878
margin
10759
06:50:00,878 --> 06:50:02,240
auto
10760
06:50:02,240 --> 06:50:03,440
yeah
10761
06:50:03,440 --> 06:50:05,600
let's head back to our cell class
10762
06:50:05,600 --> 06:50:07,360
let's add a box shadow
10763
06:50:07,360 --> 06:50:08,638
box
10764
06:50:08,638 --> 06:50:10,478
shadow
10765
06:50:10,478 --> 06:50:14,320
zero zero zero two pixels
10766
06:50:14,320 --> 06:50:16,080
all of these lines should have an even
10767
06:50:16,080 --> 06:50:17,760
thickness
10768
06:50:17,760 --> 06:50:20,400
i'll add a line height
10769
06:50:20,400 --> 06:50:23,760
of 75 pixels
10770
06:50:24,958 --> 06:50:27,120
just to test the x's and o's i'm going
10771
06:50:27,120 --> 06:50:30,080
to add an x like right here and an o
10772
06:50:30,080 --> 06:50:31,760
here
10773
06:50:31,760 --> 06:50:34,160
let's edit the font
10774
06:50:34,160 --> 06:50:35,520
font
10775
06:50:35,520 --> 06:50:37,200
size
10776
06:50:37,200 --> 06:50:39,440
50 pixels
10777
06:50:39,440 --> 06:50:41,360
when i move my cursor over these cells i
10778
06:50:41,360 --> 06:50:43,680
would like my cursor to be a pointer to
10779
06:50:43,680 --> 06:50:45,280
indicate that we can click on something
10780
06:50:45,280 --> 06:50:46,958
so cursor
10781
06:50:46,958 --> 06:50:49,600
pointer let's test that yeah there's our
10782
06:50:49,600 --> 06:50:51,840
pointer okay let's select our game
10783
06:50:51,840 --> 06:50:53,520
container i'm going to center everything
10784
06:50:53,520 --> 06:50:55,200
and change the font
10785
06:50:55,200 --> 06:50:56,478
game
10786
06:50:56,478 --> 06:50:58,638
container
10787
06:50:58,638 --> 06:51:00,320
i'll add a font
10788
06:51:00,320 --> 06:51:02,400
family
10789
06:51:02,400 --> 06:51:03,760
i'll select
10790
06:51:03,760 --> 06:51:06,080
permanent
10791
06:51:06,080 --> 06:51:08,558
marker i like that font
10792
06:51:08,558 --> 06:51:12,160
and a backup of cursive
10793
06:51:12,478 --> 06:51:13,840
then we just got to center everything
10794
06:51:13,840 --> 06:51:15,760
within this container
10795
06:51:15,760 --> 06:51:17,920
text align
10796
06:51:17,920 --> 06:51:19,120
center
10797
06:51:19,120 --> 06:51:22,000
yeah and that's our css style sheet
10798
06:51:22,000 --> 06:51:23,600
so let's get rid of this exyno that we
10799
06:51:23,600 --> 06:51:25,440
have i was just writing that there to
10800
06:51:25,440 --> 06:51:27,280
test it
10801
06:51:27,280 --> 06:51:30,080
okay so our html file is done our css
10802
06:51:30,080 --> 06:51:31,360
file is done let's head to our
10803
06:51:31,360 --> 06:51:34,240
javascript file
10804
06:51:34,718 --> 06:51:36,798
okay now we're within our javascript
10805
06:51:36,798 --> 06:51:38,558
file let's declare all of the variables
10806
06:51:38,558 --> 06:51:40,080
that we'll need
10807
06:51:40,080 --> 06:51:41,120
const
10808
06:51:41,120 --> 06:51:42,638
cells
10809
06:51:42,638 --> 06:51:43,760
equals
10810
06:51:43,760 --> 06:51:45,840
document dot
10811
06:51:45,840 --> 06:51:47,440
query
10812
06:51:47,440 --> 06:51:48,958
selector
10813
06:51:48,958 --> 06:51:50,160
all
10814
06:51:50,160 --> 06:51:53,600
we're selecting a class the cell class
10815
06:51:53,600 --> 06:51:55,680
dot cell
10816
06:51:55,680 --> 06:51:58,718
then let's select status text const
10817
06:51:58,718 --> 06:52:00,638
status text
10818
06:52:00,638 --> 06:52:03,120
document dot query selector
10819
06:52:03,120 --> 06:52:05,600
not query selector all
10820
06:52:05,600 --> 06:52:08,478
we are selecting
10821
06:52:08,478 --> 06:52:10,000
status
10822
06:52:10,000 --> 06:52:12,400
text
10823
06:52:12,558 --> 06:52:15,680
then our restart button
10824
06:52:15,920 --> 06:52:17,680
const
10825
06:52:17,680 --> 06:52:19,200
restart
10826
06:52:19,200 --> 06:52:21,440
button id
10827
06:52:21,440 --> 06:52:23,840
restart button
10828
06:52:23,840 --> 06:52:25,520
we need a constant of all of our win
10829
06:52:25,520 --> 06:52:27,280
conditions
10830
06:52:27,280 --> 06:52:29,440
const win
10831
06:52:29,440 --> 06:52:31,760
conditions
10832
06:52:31,760 --> 06:52:33,600
win conditions will be a two-dimensional
10833
06:52:33,600 --> 06:52:35,200
array of indices
10834
06:52:35,200 --> 06:52:37,040
if three cells all have the same
10835
06:52:37,040 --> 06:52:39,360
character we would need to check that
10836
06:52:39,360 --> 06:52:40,878
but we'll have to know what cells to
10837
06:52:40,878 --> 06:52:41,840
check
10838
06:52:41,840 --> 06:52:43,920
let's begin with the first row these
10839
06:52:43,920 --> 06:52:47,680
would have indices of 0 1 2 so let's add
10840
06:52:47,680 --> 06:52:48,400
that
10841
06:52:48,400 --> 06:52:51,920
zero one two
10842
06:52:51,920 --> 06:52:53,520
then the second row
10843
06:52:53,520 --> 06:52:56,878
three four five
10844
06:52:59,040 --> 06:53:03,160
six seven eight
10845
06:53:03,600 --> 06:53:05,840
then columns
10846
06:53:05,840 --> 06:53:09,200
zero three six
10847
06:53:13,760 --> 06:53:17,638
one four seven
10848
06:53:20,320 --> 06:53:23,440
two five eight
10849
06:53:25,600 --> 06:53:28,240
then the diagonals
10850
06:53:28,240 --> 06:53:31,840
zero four eight
10851
06:53:34,558 --> 06:53:37,840
two four six
10852
06:53:40,240 --> 06:53:41,680
and that's it
10853
06:53:41,680 --> 06:53:43,600
we'll need an array of placeholders i'll
10854
06:53:43,600 --> 06:53:47,520
name this options let options equals
10855
06:53:47,520 --> 06:53:49,760
options will be originally
10856
06:53:49,760 --> 06:53:52,080
an array of empty strings
10857
06:53:52,080 --> 06:53:54,080
we'll need nine of them
10858
06:53:54,080 --> 06:53:57,798
one for each cell
10859
06:54:01,760 --> 06:54:03,840
okay let's make sure we have nine
10860
06:54:03,840 --> 06:54:06,798
one two three four five six seven eight
10861
06:54:06,798 --> 06:54:08,638
nine
10862
06:54:08,638 --> 06:54:10,000
we'll need to keep track of the current
10863
06:54:10,000 --> 06:54:10,840
player
10864
06:54:10,840 --> 06:54:13,040
let's current
10865
06:54:13,040 --> 06:54:16,878
player equal x
10866
06:54:17,120 --> 06:54:18,638
then we'll need a boolean variable to
10867
06:54:18,638 --> 06:54:20,878
keep track if our game is running
10868
06:54:20,878 --> 06:54:23,440
let running equals
10869
06:54:23,440 --> 06:54:24,400
false
10870
06:54:24,400 --> 06:54:25,840
we'll switch this to true when we
10871
06:54:25,840 --> 06:54:27,600
initialize our game
10872
06:54:27,600 --> 06:54:28,878
let's create all of the different
10873
06:54:28,878 --> 06:54:30,718
functions that we'll need
10874
06:54:30,718 --> 06:54:32,000
function
10875
06:54:32,000 --> 06:54:35,000
initialize
10876
06:54:35,200 --> 06:54:37,520
game
10877
06:54:39,600 --> 06:54:41,040
function
10878
06:54:41,040 --> 06:54:44,000
cell clicked
10879
06:54:47,360 --> 06:54:49,440
function
10880
06:54:49,440 --> 06:54:51,280
update cell
10881
06:54:51,280 --> 06:54:53,920
there will be two parameters cell as
10882
06:54:53,920 --> 06:54:57,040
well as index
10883
06:54:57,840 --> 06:54:59,280
function
10884
06:54:59,280 --> 06:55:02,320
change player
10885
06:55:04,080 --> 06:55:05,520
function
10886
06:55:05,520 --> 06:55:06,558
check
10887
06:55:06,558 --> 06:55:09,558
winner
10888
06:55:10,718 --> 06:55:12,080
function
10889
06:55:12,080 --> 06:55:15,520
restart game
10890
06:55:16,160 --> 06:55:18,240
okay and that's the functions
10891
06:55:18,240 --> 06:55:20,320
when we begin our game let's initialize
10892
06:55:20,320 --> 06:55:23,400
the game
10893
06:55:23,600 --> 06:55:25,840
we'll use this function initialize game
10894
06:55:25,840 --> 06:55:27,600
to take care of any setup before we need
10895
06:55:27,600 --> 06:55:28,718
to start
10896
06:55:28,718 --> 06:55:30,240
we'll have to add some event listeners
10897
06:55:30,240 --> 06:55:32,080
to our cells
10898
06:55:32,080 --> 06:55:35,120
take our cells
10899
06:55:35,120 --> 06:55:37,120
then use the for
10900
06:55:37,120 --> 06:55:39,360
each method
10901
06:55:39,360 --> 06:55:41,280
we'll use an arrow function expression
10902
06:55:41,280 --> 06:55:44,478
for each cell
10903
06:55:44,798 --> 06:55:47,280
we will take our cell
10904
06:55:47,280 --> 06:55:49,440
add event
10905
06:55:49,440 --> 06:55:51,600
listener
10906
06:55:51,600 --> 06:55:54,000
the event will be click
10907
06:55:54,000 --> 06:55:58,638
we will add a callback of cell clicked
10908
06:55:59,200 --> 06:56:00,878
the second line will add an event
10909
06:56:00,878 --> 06:56:02,958
listener to our restart button
10910
06:56:02,958 --> 06:56:05,200
restart button
10911
06:56:05,200 --> 06:56:07,680
dot add event
10912
06:56:07,680 --> 06:56:09,760
listener
10913
06:56:09,760 --> 06:56:12,000
when we click
10914
06:56:12,000 --> 06:56:15,200
we are going to invoke the restart game
10915
06:56:15,200 --> 06:56:18,200
function
10916
06:56:18,878 --> 06:56:20,958
for the third line let's update our
10917
06:56:20,958 --> 06:56:23,840
status text
10918
06:56:23,840 --> 06:56:27,600
status text dot text
10919
06:56:27,600 --> 06:56:29,200
content
10920
06:56:29,200 --> 06:56:31,520
set the sequel to i'll use the template
10921
06:56:31,520 --> 06:56:33,040
literal
10922
06:56:33,040 --> 06:56:34,240
current
10923
06:56:34,240 --> 06:56:36,400
player
10924
06:56:36,400 --> 06:56:39,680
it's their turn
10925
06:56:39,680 --> 06:56:43,280
okay looks like we have one problem
10926
06:56:43,760 --> 06:56:45,280
looks like i accidentally capitalized
10927
06:56:45,280 --> 06:56:47,680
that l in current player okay let's work
10928
06:56:47,680 --> 06:56:50,000
on the cell clicked function
10929
06:56:50,000 --> 06:56:51,520
when we click on a cell what are we
10930
06:56:51,520 --> 06:56:52,638
going to do
10931
06:56:52,638 --> 06:56:55,520
i'll create a local variable
10932
06:56:55,520 --> 06:56:56,638
const
10933
06:56:56,638 --> 06:56:57,440
cell
10934
06:56:57,440 --> 06:56:58,478
index
10935
06:56:58,478 --> 06:57:00,400
i will set the sequel to
10936
06:57:00,400 --> 06:57:02,400
this
10937
06:57:02,400 --> 06:57:04,240
this refers to whatever cell that we
10938
06:57:04,240 --> 06:57:05,200
click on
10939
06:57:05,200 --> 06:57:08,638
we will get an attribute get
10940
06:57:08,638 --> 06:57:10,558
attribute
10941
06:57:10,558 --> 06:57:13,360
the attribute that we're getting is cell
10942
06:57:13,360 --> 06:57:15,840
index
10943
06:57:18,798 --> 06:57:20,958
we have an index number what we would
10944
06:57:20,958 --> 06:57:23,200
like to check to see is if that index
10945
06:57:23,200 --> 06:57:25,040
number within our options our
10946
06:57:25,040 --> 06:57:26,718
placeholders
10947
06:57:26,718 --> 06:57:29,440
are not empty we'll only want to update
10948
06:57:29,440 --> 06:57:32,958
a cell if there's nothing there
10949
06:57:32,958 --> 06:57:33,840
if
10950
06:57:33,840 --> 06:57:35,360
our options
10951
06:57:35,360 --> 06:57:38,958
at index of cell index
10952
06:57:38,958 --> 06:57:43,200
does not equal an empty space or if the
10953
06:57:43,200 --> 06:57:45,280
game is not
10954
06:57:45,280 --> 06:57:47,600
running
10955
06:57:48,240 --> 06:57:51,440
then we will return not do anything
10956
06:57:51,440 --> 06:57:53,120
otherwise
10957
06:57:53,120 --> 06:57:54,840
we will invoke the
10958
06:57:54,840 --> 06:57:57,280
update cell function
10959
06:57:57,280 --> 06:58:00,478
pass in this as an argument as well as
10960
06:58:00,478 --> 06:58:03,200
cell index
10961
06:58:03,200 --> 06:58:04,718
followed by
10962
06:58:04,718 --> 06:58:05,680
check
10963
06:58:05,680 --> 06:58:06,718
winner
10964
06:58:06,718 --> 06:58:08,320
function
10965
06:58:08,320 --> 06:58:10,240
okay let's head to the update cell
10966
06:58:10,240 --> 06:58:12,000
function
10967
06:58:12,000 --> 06:58:13,200
take
10968
06:58:13,200 --> 06:58:14,798
options
10969
06:58:14,798 --> 06:58:18,958
at index of the index parameter
10970
06:58:18,958 --> 06:58:23,360
set the sequel to the current player
10971
06:58:23,360 --> 06:58:26,718
so we're updating our placeholders
10972
06:58:26,718 --> 06:58:29,040
then change the text content of one of
10973
06:58:29,040 --> 06:58:30,240
these cells
10974
06:58:30,240 --> 06:58:31,440
whatever cell that we click on
10975
06:58:31,440 --> 06:58:32,798
originally
10976
06:58:32,798 --> 06:58:34,958
cell dot text
10977
06:58:34,958 --> 06:58:36,240
content
10978
06:58:36,240 --> 06:58:38,798
equals current player
10979
06:58:38,798 --> 06:58:40,320
now we should be able to click on at
10980
06:58:40,320 --> 06:58:43,040
least one of these cells
10981
06:58:43,040 --> 06:58:45,280
oh i forgot one thing when we initialize
10982
06:58:45,280 --> 06:58:48,638
our game we have to set running to true
10983
06:58:48,638 --> 06:58:51,680
running equals true
10984
06:58:51,680 --> 06:58:52,718
there
10985
06:58:52,718 --> 06:58:55,440
so we can click on one of these spaces
10986
06:58:55,440 --> 06:58:56,240
x
10987
06:58:56,240 --> 06:58:58,558
x x x
10988
06:58:58,558 --> 06:59:01,120
then we just need to change the player
10989
06:59:01,120 --> 06:59:04,000
take our current player
10990
06:59:04,000 --> 06:59:06,320
then we will set the sequel to then use
10991
06:59:06,320 --> 06:59:10,000
the ternary operator our condition is if
10992
06:59:10,000 --> 06:59:12,080
change player
10993
06:59:12,080 --> 06:59:14,160
is equal to x
10994
06:59:14,160 --> 06:59:16,798
if current player is equal to x we will
10995
06:59:16,798 --> 06:59:19,600
reassign our current player with
10996
06:59:19,600 --> 06:59:21,520
o
10997
06:59:21,520 --> 06:59:22,958
otherwise
10998
06:59:22,958 --> 06:59:25,200
x
10999
06:59:25,600 --> 06:59:27,920
then take status
11000
06:59:27,920 --> 06:59:29,120
text
11001
06:59:29,120 --> 06:59:29,920
dot
11002
06:59:29,920 --> 06:59:31,520
text
11003
06:59:31,520 --> 06:59:33,040
content
11004
06:59:33,040 --> 06:59:34,878
set the sequel to i'll use the template
11005
06:59:34,878 --> 06:59:37,280
literal
11006
06:59:37,520 --> 06:59:39,600
current player
11007
06:59:39,600 --> 06:59:41,280
it's their
11008
06:59:41,280 --> 06:59:42,320
turn
11009
06:59:42,320 --> 06:59:43,680
yeah just temporarily i'm going to
11010
06:59:43,680 --> 06:59:45,600
invoke this function after updating a
11011
06:59:45,600 --> 06:59:47,440
cell
11012
06:59:47,440 --> 06:59:48,718
now we should be able to alternate
11013
06:59:48,718 --> 06:59:50,478
between x's and o's
11014
06:59:50,478 --> 06:59:54,240
x o x o x o yeah we're good i'm going to
11015
06:59:54,240 --> 06:59:56,160
remove this i was just testing it to
11016
06:59:56,160 --> 06:59:58,320
make sure it works okay then head to our
11017
06:59:58,320 --> 07:00:00,240
check winner function we have a lot to
11018
07:00:00,240 --> 07:00:02,400
do here we'll create a temporary
11019
07:00:02,400 --> 07:00:06,000
variable of round one
11020
07:00:06,000 --> 07:00:08,400
i will set the sequel to false if
11021
07:00:08,400 --> 07:00:11,360
somebody wins we'll flip this to be true
11022
07:00:11,360 --> 07:00:13,120
we'll use a for loop we haven't used
11023
07:00:13,120 --> 07:00:14,958
those for a while
11024
07:00:14,958 --> 07:00:17,120
we will iterate over all of the win
11025
07:00:17,120 --> 07:00:19,040
conditions within our two-dimensional
11026
07:00:19,040 --> 07:00:22,000
array the first statement will be let i
11027
07:00:22,000 --> 07:00:23,600
equal zero
11028
07:00:23,600 --> 07:00:26,240
we'll continue this as long as i is less
11029
07:00:26,240 --> 07:00:27,120
than
11030
07:00:27,120 --> 07:00:28,840
our win
11031
07:00:28,840 --> 07:00:32,478
conditions dot length property
11032
07:00:32,478 --> 07:00:35,600
increment i by one we will iterate over
11033
07:00:35,600 --> 07:00:38,478
each inner array within win conditions
11034
07:00:38,478 --> 07:00:40,400
but let's store each of these arrays
11035
07:00:40,400 --> 07:00:44,080
within a temporary variable const
11036
07:00:44,080 --> 07:00:46,240
condition
11037
07:00:46,240 --> 07:00:47,600
equals
11038
07:00:47,600 --> 07:00:49,200
when condition
11039
07:00:49,200 --> 07:00:52,638
at our current index of i
11040
07:00:52,638 --> 07:00:55,760
each row has three indices
11041
07:00:55,760 --> 07:00:59,440
const cell a equals
11042
07:00:59,440 --> 07:01:00,958
our options
11043
07:01:00,958 --> 07:01:02,878
but at what index
11044
07:01:02,878 --> 07:01:04,400
while the index is going to be our
11045
07:01:04,400 --> 07:01:05,680
condition
11046
07:01:05,680 --> 07:01:10,400
at index 0 then we have cell b
11047
07:01:10,400 --> 07:01:15,680
cell b condition at one
11048
07:01:16,478 --> 07:01:18,000
cell c
11049
07:01:18,000 --> 07:01:20,400
condition index of two let me explain
11050
07:01:20,400 --> 07:01:21,840
how this is gonna work
11051
07:01:21,840 --> 07:01:24,000
let's head to the top we're iterating
11052
07:01:24,000 --> 07:01:26,240
over all of these inner arrays we'll
11053
07:01:26,240 --> 07:01:28,160
begin with the first we have three
11054
07:01:28,160 --> 07:01:31,440
indices zero one two we're going to
11055
07:01:31,440 --> 07:01:33,280
check within our options at these three
11056
07:01:33,280 --> 07:01:34,718
indices
11057
07:01:34,718 --> 07:01:37,280
zero one two at least that row to begin
11058
07:01:37,280 --> 07:01:38,400
with
11059
07:01:38,400 --> 07:01:41,200
if these three are not spaces and
11060
07:01:41,200 --> 07:01:43,200
they're all the same that means somebody
11061
07:01:43,200 --> 07:01:45,280
won if there is no winner we'll check
11062
07:01:45,280 --> 07:01:47,360
the next set of win conditions three
11063
07:01:47,360 --> 07:01:48,718
four and five
11064
07:01:48,718 --> 07:01:51,120
these are indices within our option at
11065
07:01:51,120 --> 07:01:52,798
these three indices
11066
07:01:52,798 --> 07:01:54,718
three four and five
11067
07:01:54,718 --> 07:01:56,958
if there are no spaces and they're all
11068
07:01:56,958 --> 07:01:58,878
the same character that means somebody
11069
07:01:58,878 --> 07:02:01,280
won we'll repeat this process for each
11070
07:02:01,280 --> 07:02:02,958
set of win conditions
11071
07:02:02,958 --> 07:02:05,280
so heading back down to our check winner
11072
07:02:05,280 --> 07:02:06,958
function we'll need to check if there's
11073
07:02:06,958 --> 07:02:08,638
any empty spaces
11074
07:02:08,638 --> 07:02:09,680
if
11075
07:02:09,680 --> 07:02:11,360
cell a
11076
07:02:11,360 --> 07:02:13,840
is equal to an empty space
11077
07:02:13,840 --> 07:02:14,798
or
11078
07:02:14,798 --> 07:02:16,000
cell b
11079
07:02:16,000 --> 07:02:18,478
is equal to an empty space
11080
07:02:18,478 --> 07:02:19,440
or
11081
07:02:19,440 --> 07:02:20,638
cell c
11082
07:02:20,638 --> 07:02:23,520
is equal to an empty space if there's an
11083
07:02:23,520 --> 07:02:24,878
empty space
11084
07:02:24,878 --> 07:02:27,280
we'll continue and skip this iteration
11085
07:02:27,280 --> 07:02:29,360
so if there are no empty spaces that
11086
07:02:29,360 --> 07:02:31,600
means all the spaces are full let's make
11087
07:02:31,600 --> 07:02:34,400
sure they're all the same character
11088
07:02:34,400 --> 07:02:35,360
if
11089
07:02:35,360 --> 07:02:36,558
cell a
11090
07:02:36,558 --> 07:02:37,920
is equal to
11091
07:02:37,920 --> 07:02:40,080
cell b
11092
07:02:40,080 --> 07:02:41,680
and
11093
07:02:41,680 --> 07:02:42,878
cell b
11094
07:02:42,878 --> 07:02:44,478
is equal to
11095
07:02:44,478 --> 07:02:45,920
cell c
11096
07:02:45,920 --> 07:02:48,558
that means we have a winner we'll take
11097
07:02:48,558 --> 07:02:51,360
our local variable of round one set the
11098
07:02:51,360 --> 07:02:53,040
sql to true
11099
07:02:53,040 --> 07:02:54,718
then break we don't need to continue
11100
07:02:54,718 --> 07:02:56,400
this for loop anymore we can break out
11101
07:02:56,400 --> 07:02:57,280
of it
11102
07:02:57,280 --> 07:02:59,520
outside of our for loop
11103
07:02:59,520 --> 07:03:01,280
if
11104
07:03:01,280 --> 07:03:04,558
round one is equal to true
11105
07:03:04,558 --> 07:03:08,160
let's update our status text right here
11106
07:03:08,160 --> 07:03:09,200
status
11107
07:03:09,200 --> 07:03:12,320
text dot text
11108
07:03:12,320 --> 07:03:13,600
content
11109
07:03:13,600 --> 07:03:17,440
is equal to i'll use a template literal
11110
07:03:17,440 --> 07:03:18,878
current
11111
07:03:18,878 --> 07:03:21,280
player
11112
07:03:21,440 --> 07:03:23,600
wins
11113
07:03:23,600 --> 07:03:27,680
running equals false the game is over
11114
07:03:27,680 --> 07:03:29,760
else if
11115
07:03:29,760 --> 07:03:32,558
so if there's no spaces left it's a draw
11116
07:03:32,558 --> 07:03:34,638
we could check that with this condition
11117
07:03:34,638 --> 07:03:36,320
take our options
11118
07:03:36,320 --> 07:03:39,280
then use the includes method
11119
07:03:39,280 --> 07:03:41,440
we'll check for any spaces
11120
07:03:41,440 --> 07:03:43,840
does our array include any spaces
11121
07:03:43,840 --> 07:03:45,520
precede this with the not logical
11122
07:03:45,520 --> 07:03:46,400
operator
11123
07:03:46,400 --> 07:03:48,638
if our options does not include any
11124
07:03:48,638 --> 07:03:51,760
spaces if this is true then we will
11125
07:03:51,760 --> 07:03:55,120
update our status text
11126
07:03:55,120 --> 07:03:57,440
to equal
11127
07:03:57,440 --> 07:03:58,878
draw
11128
07:03:58,878 --> 07:04:01,680
running equals false
11129
07:04:01,680 --> 07:04:02,798
else
11130
07:04:02,798 --> 07:04:05,840
we can change player change
11131
07:04:05,840 --> 07:04:07,760
player
11132
07:04:07,760 --> 07:04:10,080
okay let's try this
11133
07:04:10,080 --> 07:04:12,878
x o x o
11134
07:04:12,878 --> 07:04:14,478
x o
11135
07:04:14,478 --> 07:04:16,878
uh we should have a winner here yeah x
11136
07:04:16,878 --> 07:04:19,280
wins let's work on this restart button
11137
07:04:19,280 --> 07:04:20,798
to restart the game
11138
07:04:20,798 --> 07:04:23,040
we'll take current
11139
07:04:23,040 --> 07:04:24,080
player
11140
07:04:24,080 --> 07:04:28,240
set the sequel to x
11141
07:04:28,240 --> 07:04:30,320
take our options
11142
07:04:30,320 --> 07:04:32,080
i'll just copy this we're going to reset
11143
07:04:32,080 --> 07:04:34,320
them
11144
07:04:34,798 --> 07:04:37,120
so they're all empty spaces
11145
07:04:37,120 --> 07:04:39,040
status text
11146
07:04:39,040 --> 07:04:40,558
dot
11147
07:04:40,558 --> 07:04:43,520
text content
11148
07:04:43,520 --> 07:04:44,718
equals
11149
07:04:44,718 --> 07:04:48,000
i'll use a template literal
11150
07:04:48,000 --> 07:04:50,400
current players
11151
07:04:50,400 --> 07:04:51,760
turn
11152
07:04:51,760 --> 07:04:54,080
we'll have to clear each cell
11153
07:04:54,080 --> 07:04:55,120
take
11154
07:04:55,120 --> 07:04:56,718
our cells
11155
07:04:56,718 --> 07:04:58,478
use the for
11156
07:04:58,478 --> 07:04:59,920
each method
11157
07:04:59,920 --> 07:05:02,000
use an arrow function expression
11158
07:05:02,000 --> 07:05:03,520
for each cell
11159
07:05:03,520 --> 07:05:05,600
we will take that cell
11160
07:05:05,600 --> 07:05:07,600
update the text
11161
07:05:07,600 --> 07:05:08,958
content
11162
07:05:08,958 --> 07:05:12,400
equal to an empty space
11163
07:05:12,400 --> 07:05:16,558
then set running equal to true
11164
07:05:16,558 --> 07:05:18,160
okay let me make sure the game's a draw
11165
07:05:18,160 --> 07:05:20,558
first
11166
07:05:23,280 --> 07:05:25,840
okay draw i'm going to restart and we
11167
07:05:25,840 --> 07:05:27,200
have a new game
11168
07:05:27,200 --> 07:05:29,920
so yeah everybody that is a basic game
11169
07:05:29,920 --> 07:05:32,000
of tic-tac-toe if you would like a copy
11170
07:05:32,000 --> 07:05:33,760
of all this code i'll post this in the
11171
07:05:33,760 --> 07:05:35,600
comment section down below underneath
11172
07:05:35,600 --> 07:05:37,840
the original video in this playlist and
11173
07:05:37,840 --> 07:05:40,080
well yeah that's a basic game of
11174
07:05:40,080 --> 07:05:43,280
tic-tac-toe using javascript
11175
07:05:43,280 --> 07:05:45,120
hey what's going on everybody it's your
11176
07:05:45,120 --> 07:05:46,798
bro hope you're doing well and in this
11177
07:05:46,798 --> 07:05:48,240
video we're going to create a game of
11178
07:05:48,240 --> 07:05:51,360
snake using javascript so sit back relax
11179
07:05:51,360 --> 07:05:54,080
and enjoy the show
11180
07:05:54,080 --> 07:05:56,400
hey what is up everybody in this video
11181
07:05:56,400 --> 07:05:58,718
we're going to create a game of snake
11182
07:05:58,718 --> 07:06:01,440
let's head to our html file i'll create
11183
07:06:01,440 --> 07:06:03,760
a div as a container
11184
07:06:03,760 --> 07:06:07,280
div id equals game
11185
07:06:07,280 --> 07:06:09,680
container
11186
07:06:09,680 --> 07:06:11,200
let's close it
11187
07:06:11,200 --> 07:06:13,120
we'll be using a canvas to draw our
11188
07:06:13,120 --> 07:06:15,280
components
11189
07:06:15,280 --> 07:06:18,558
canvas the id will be
11190
07:06:18,558 --> 07:06:19,440
game
11191
07:06:19,440 --> 07:06:20,798
board
11192
07:06:20,798 --> 07:06:22,718
i'm gonna go ahead and set the width and
11193
07:06:22,718 --> 07:06:26,000
the height attributes right away
11194
07:06:26,000 --> 07:06:27,920
500 for the width
11195
07:06:27,920 --> 07:06:31,440
the height will be 500 as well
11196
07:06:31,440 --> 07:06:33,440
then close the canvas
11197
07:06:33,440 --> 07:06:36,160
i'll add a score
11198
07:06:36,160 --> 07:06:37,200
div
11199
07:06:37,200 --> 07:06:40,638
id equals score
11200
07:06:40,638 --> 07:06:43,120
let's close the div section the initial
11201
07:06:43,120 --> 07:06:45,120
score will be set to zero
11202
07:06:45,120 --> 07:06:47,440
then we'll need a reset button
11203
07:06:47,440 --> 07:06:48,478
button
11204
07:06:48,478 --> 07:06:50,000
close it
11205
07:06:50,000 --> 07:06:52,240
id equals
11206
07:06:52,240 --> 07:06:56,320
reset button the text is reset
11207
07:06:56,320 --> 07:06:58,400
okay let's work on our css styling
11208
07:06:58,400 --> 07:07:01,600
that's everything for our html file
11209
07:07:01,600 --> 07:07:03,840
let's select our game board
11210
07:07:03,840 --> 07:07:04,798
game
11211
07:07:04,798 --> 07:07:05,760
board
11212
07:07:05,760 --> 07:07:08,798
this is an id i'll add a border
11213
07:07:08,798 --> 07:07:10,160
border
11214
07:07:10,160 --> 07:07:12,878
3 pixel solid is good
11215
07:07:12,878 --> 07:07:14,878
let's enter everything within our game
11216
07:07:14,878 --> 07:07:17,840
container
11217
07:07:19,040 --> 07:07:21,040
game container
11218
07:07:21,040 --> 07:07:23,120
text align
11219
07:07:23,120 --> 07:07:25,040
center
11220
07:07:25,040 --> 07:07:26,878
i'll change the font of the score you
11221
07:07:26,878 --> 07:07:30,680
can barely see it
11222
07:07:31,760 --> 07:07:32,958
font
11223
07:07:32,958 --> 07:07:34,160
family
11224
07:07:34,160 --> 07:07:36,240
pick whatever font you like
11225
07:07:36,240 --> 07:07:40,160
one font that i like is permanent marker
11226
07:07:40,160 --> 07:07:41,360
permanent
11227
07:07:41,360 --> 07:07:42,638
marker
11228
07:07:42,638 --> 07:07:45,280
then a backup of cursive
11229
07:07:45,280 --> 07:07:48,718
let's change the font size font size 100
11230
07:07:48,718 --> 07:07:50,160
pixels
11231
07:07:50,160 --> 07:07:53,600
then let's work on our button next
11232
07:07:53,600 --> 07:07:56,638
reset button
11233
07:07:56,958 --> 07:07:59,200
i'll pick the same font family
11234
07:07:59,200 --> 07:08:03,920
the font size will be 22 pixels
11235
07:08:04,718 --> 07:08:08,798
a width of 100 pixels
11236
07:08:08,798 --> 07:08:13,080
a height of 50 pixels
11237
07:08:13,360 --> 07:08:14,798
border
11238
07:08:14,798 --> 07:08:18,240
4 pixels solid
11239
07:08:18,878 --> 07:08:22,558
border radius to round the corners
11240
07:08:22,558 --> 07:08:25,280
15 pixels
11241
07:08:25,280 --> 07:08:27,280
then cursor
11242
07:08:27,280 --> 07:08:29,440
pointer when we hover our cursor over
11243
07:08:29,440 --> 07:08:32,240
this button okay that is our css style
11244
07:08:32,240 --> 07:08:35,200
sheet and our index.html file let's head
11245
07:08:35,200 --> 07:08:37,200
to our javascript file
11246
07:08:37,200 --> 07:08:39,360
we have a lot of variables to declare
11247
07:08:39,360 --> 07:08:41,600
let's begin with the constants
11248
07:08:41,600 --> 07:08:43,040
const
11249
07:08:43,040 --> 07:08:44,718
game board
11250
07:08:44,718 --> 07:08:47,520
equals document dot
11251
07:08:47,520 --> 07:08:48,638
query
11252
07:08:48,638 --> 07:08:50,000
selector
11253
07:08:50,000 --> 07:08:52,320
we are selecting an id
11254
07:08:52,320 --> 07:08:55,200
that id is game board
11255
07:08:55,200 --> 07:08:57,200
that's our canvas
11256
07:08:57,200 --> 07:08:59,760
with a canvas to paint on the canvas we
11257
07:08:59,760 --> 07:09:02,798
have to get the context
11258
07:09:02,798 --> 07:09:03,840
const
11259
07:09:03,840 --> 07:09:06,718
ctx for context
11260
07:09:06,718 --> 07:09:08,958
equals game board
11261
07:09:08,958 --> 07:09:11,280
dot get
11262
07:09:11,280 --> 07:09:13,280
context
11263
07:09:13,280 --> 07:09:16,080
then pass in 2d
11264
07:09:16,080 --> 07:09:19,440
we'll store our score text as a variable
11265
07:09:19,440 --> 07:09:20,840
const
11266
07:09:20,840 --> 07:09:23,520
score text
11267
07:09:23,520 --> 07:09:26,478
equals document dot
11268
07:09:26,478 --> 07:09:30,080
query selector
11269
07:09:30,080 --> 07:09:32,958
we're selecting an id the id is
11270
07:09:32,958 --> 07:09:34,000
score
11271
07:09:34,000 --> 07:09:36,878
let's change this id to score text we're
11272
07:09:36,878 --> 07:09:39,120
already going to have a score variable
11273
07:09:39,120 --> 07:09:41,200
hey this is future bro so i forgot to
11274
07:09:41,200 --> 07:09:43,440
change that within my css style sheet
11275
07:09:43,440 --> 07:09:44,958
make sure you make that change here as
11276
07:09:44,958 --> 07:09:45,840
well
11277
07:09:45,840 --> 07:09:48,160
let's select our reset button
11278
07:09:48,160 --> 07:09:49,440
const
11279
07:09:49,440 --> 07:09:51,280
reset button
11280
07:09:51,280 --> 07:09:54,240
equals document dot
11281
07:09:54,240 --> 07:09:56,958
query selector
11282
07:09:56,958 --> 07:10:00,798
we are selecting our reset button
11283
07:10:00,798 --> 07:10:02,798
i'm going to create a variable to hold
11284
07:10:02,798 --> 07:10:05,600
the width of our game board
11285
07:10:05,600 --> 07:10:06,798
const
11286
07:10:06,798 --> 07:10:08,638
game width
11287
07:10:08,638 --> 07:10:10,080
equals
11288
07:10:10,080 --> 07:10:13,040
game board dot width
11289
07:10:13,040 --> 07:10:14,478
then game height
11290
07:10:14,478 --> 07:10:18,000
const game height
11291
07:10:18,000 --> 07:10:21,200
equals game board dot height
11292
07:10:21,200 --> 07:10:22,798
width and height are the attributes that
11293
07:10:22,798 --> 07:10:25,440
we assigned within our canvas
11294
07:10:25,440 --> 07:10:27,200
initially when we run this javascript
11295
07:10:27,200 --> 07:10:28,638
file if we would have declared those
11296
07:10:28,638 --> 07:10:30,958
within the css file they wouldn't have
11297
07:10:30,958 --> 07:10:32,798
been available to us right away that's
11298
07:10:32,798 --> 07:10:34,558
why i assigned these attributes within
11299
07:10:34,558 --> 07:10:37,040
the element itself inline we're going to
11300
07:10:37,040 --> 07:10:39,600
assign some colors the first is for the
11301
07:10:39,600 --> 07:10:41,840
board background
11302
07:10:41,840 --> 07:10:43,040
const
11303
07:10:43,040 --> 07:10:44,320
board
11304
07:10:44,320 --> 07:10:46,000
background
11305
07:10:46,000 --> 07:10:48,958
equals pick a color i'll just pick white
11306
07:10:48,958 --> 07:10:51,600
pick a color for your snake
11307
07:10:51,600 --> 07:10:52,878
const
11308
07:10:52,878 --> 07:10:54,478
snake color
11309
07:10:54,478 --> 07:10:57,600
i'll pick light green
11310
07:10:57,920 --> 07:10:59,920
but feel free to pick a different color
11311
07:10:59,920 --> 07:11:02,798
i'll add a black border around the snake
11312
07:11:02,798 --> 07:11:03,840
snake
11313
07:11:03,840 --> 07:11:06,320
border equals black
11314
07:11:06,320 --> 07:11:08,320
then we'll need some color for the food
11315
07:11:08,320 --> 07:11:11,760
const food color
11316
07:11:11,760 --> 07:11:13,280
equals
11317
07:11:13,280 --> 07:11:16,558
red it's an apple okay then a unit size
11318
07:11:16,558 --> 07:11:18,478
what's the size of everything within our
11319
07:11:18,478 --> 07:11:19,360
game
11320
07:11:19,360 --> 07:11:20,400
const
11321
07:11:20,400 --> 07:11:22,080
unit size
11322
07:11:22,080 --> 07:11:24,878
equals i'll pick 25 pixels now we'll
11323
07:11:24,878 --> 07:11:26,878
create a couple more variables
11324
07:11:26,878 --> 07:11:27,760
let
11325
07:11:27,760 --> 07:11:30,080
running equal false
11326
07:11:30,080 --> 07:11:32,000
we'll examine running to see if our game
11327
07:11:32,000 --> 07:11:33,840
is currently running or not
11328
07:11:33,840 --> 07:11:34,718
let
11329
07:11:34,718 --> 07:11:35,520
x
11330
07:11:35,520 --> 07:11:38,878
velocity x velocity will be how far we
11331
07:11:38,878 --> 07:11:42,400
move on the x-axis every single game
11332
07:11:42,400 --> 07:11:45,200
tick i will set this to be the unit size
11333
07:11:45,200 --> 07:11:48,638
we'll be moving 25 pixels on the x-axis
11334
07:11:48,638 --> 07:11:51,040
every game tick if x-velocity is a
11335
07:11:51,040 --> 07:11:53,360
positive number we'll move to the right
11336
07:11:53,360 --> 07:11:55,680
if it's negative we'll move to the left
11337
07:11:55,680 --> 07:11:57,840
then we have y velocity
11338
07:11:57,840 --> 07:11:59,600
let y
11339
07:11:59,600 --> 07:12:02,798
velocity equal zero that means we're not
11340
07:12:02,798 --> 07:12:04,878
moving up or down if we would like to
11341
07:12:04,878 --> 07:12:07,280
move down we would set this to unit size
11342
07:12:07,280 --> 07:12:09,200
if we would like to move up we would set
11343
07:12:09,200 --> 07:12:11,360
that to negative unit size to move up up
11344
07:12:11,360 --> 07:12:14,240
or down one space one unit but to begin
11345
07:12:14,240 --> 07:12:15,920
we'll just be moving to the right we'll
11346
07:12:15,920 --> 07:12:17,840
need the coordinates of our food
11347
07:12:17,840 --> 07:12:18,798
let
11348
07:12:18,798 --> 07:12:21,120
food x coordinate
11349
07:12:21,120 --> 07:12:22,160
let
11350
07:12:22,160 --> 07:12:23,280
food
11351
07:12:23,280 --> 07:12:25,040
why
11352
07:12:25,040 --> 07:12:27,360
we'll calculate these randomly later
11353
07:12:27,360 --> 07:12:29,440
within a function
11354
07:12:29,440 --> 07:12:30,240
let
11355
07:12:30,240 --> 07:12:31,280
score
11356
07:12:31,280 --> 07:12:33,040
equal zero
11357
07:12:33,040 --> 07:12:34,958
then we need our snake our snake is
11358
07:12:34,958 --> 07:12:37,440
going to be an array of objects an array
11359
07:12:37,440 --> 07:12:39,840
of parts
11360
07:12:40,000 --> 07:12:41,440
let
11361
07:12:41,440 --> 07:12:42,558
snake
11362
07:12:42,558 --> 07:12:44,478
equal
11363
07:12:44,478 --> 07:12:46,638
straight brackets it's an array
11364
07:12:46,638 --> 07:12:48,878
and we will create objects an object for
11365
07:12:48,878 --> 07:12:51,360
each body part each body part will have
11366
07:12:51,360 --> 07:12:54,160
an x and a y coordinate
11367
07:12:54,160 --> 07:12:55,920
let's begin with the tail
11368
07:12:55,920 --> 07:12:57,600
the tail will begin in the top left
11369
07:12:57,600 --> 07:13:00,478
corner so i will set those coordinates
11370
07:13:00,478 --> 07:13:02,958
0 0 is the top left corner
11371
07:13:02,958 --> 07:13:05,600
initially let's create 5 body parts to
11372
07:13:05,600 --> 07:13:07,680
the snake but after it eats its food
11373
07:13:07,680 --> 07:13:10,718
it's going to increase by one
11374
07:13:10,718 --> 07:13:13,120
let's add a second body part this is a
11375
07:13:13,120 --> 07:13:16,080
separate object separated with a comma
11376
07:13:16,080 --> 07:13:18,400
but i don't want this in the same space
11377
07:13:18,400 --> 07:13:20,400
i'll place this next body part to the
11378
07:13:20,400 --> 07:13:24,558
right of the first one so let's say x is
11379
07:13:24,558 --> 07:13:26,080
unit size
11380
07:13:26,080 --> 07:13:29,520
times one or just unit size i suppose
11381
07:13:29,520 --> 07:13:32,958
okay let's add another body part
11382
07:13:32,958 --> 07:13:35,840
unit size times two
11383
07:13:35,840 --> 07:13:38,080
so we'll have three squares
11384
07:13:38,080 --> 07:13:41,200
okay two more body parts unit size times
11385
07:13:41,200 --> 07:13:42,878
three
11386
07:13:42,878 --> 07:13:44,878
unit size times four
11387
07:13:44,878 --> 07:13:46,558
and that's our snake
11388
07:13:46,558 --> 07:13:48,558
it's an array of objects
11389
07:13:48,558 --> 07:13:51,760
body parts each object is a body part of
11390
07:13:51,760 --> 07:13:54,400
the snake they each have their own x and
11391
07:13:54,400 --> 07:13:56,080
y coordinates
11392
07:13:56,080 --> 07:13:57,520
let's add an event listener to our
11393
07:13:57,520 --> 07:14:00,400
window to listen for key events
11394
07:14:00,400 --> 07:14:02,798
window dot add
11395
07:14:02,798 --> 07:14:04,000
event
11396
07:14:04,000 --> 07:14:05,440
listener
11397
07:14:05,440 --> 07:14:08,798
the event is key down
11398
07:14:08,798 --> 07:14:11,680
we'll pass in a callback to a change
11399
07:14:11,680 --> 07:14:13,520
direction function
11400
07:14:13,520 --> 07:14:15,120
then we'll add an event listener to our
11401
07:14:15,120 --> 07:14:18,080
reset button to restart the game
11402
07:14:18,080 --> 07:14:19,360
reset
11403
07:14:19,360 --> 07:14:20,400
button
11404
07:14:20,400 --> 07:14:22,000
dot add
11405
07:14:22,000 --> 07:14:23,120
event
11406
07:14:23,120 --> 07:14:24,878
listener
11407
07:14:24,878 --> 07:14:27,200
when we click
11408
07:14:27,200 --> 07:14:30,478
we will invoke the reset game function
11409
07:14:30,478 --> 07:14:32,478
which we still need to declare
11410
07:14:32,478 --> 07:14:35,360
then we will invoke game start
11411
07:14:35,360 --> 07:14:36,958
let's declare the functions that we'll
11412
07:14:36,958 --> 07:14:37,920
need
11413
07:14:37,920 --> 07:14:39,680
let's begin with the first
11414
07:14:39,680 --> 07:14:40,718
function
11415
07:14:40,718 --> 07:14:43,958
game start
11416
07:14:45,280 --> 07:14:46,638
function
11417
07:14:46,638 --> 07:14:49,760
next tick
11418
07:14:51,200 --> 07:14:52,400
function
11419
07:14:52,400 --> 07:14:53,440
clear
11420
07:14:53,440 --> 07:14:54,878
board
11421
07:14:54,878 --> 07:14:56,798
clear board is going to be in charge of
11422
07:14:56,798 --> 07:14:58,718
repainting the board
11423
07:14:58,718 --> 07:15:00,320
function
11424
07:15:00,320 --> 07:15:03,280
create food
11425
07:15:03,280 --> 07:15:05,040
the create food function will find a
11426
07:15:05,040 --> 07:15:06,798
random place within our game board to
11427
07:15:06,798 --> 07:15:09,280
place a food item
11428
07:15:09,280 --> 07:15:10,878
function
11429
07:15:10,878 --> 07:15:13,680
draw food
11430
07:15:13,680 --> 07:15:15,520
we'll need to paint the food within our
11431
07:15:15,520 --> 07:15:16,878
game board
11432
07:15:16,878 --> 07:15:18,080
function
11433
07:15:18,080 --> 07:15:18,958
move
11434
07:15:18,958 --> 07:15:21,360
snake
11435
07:15:22,798 --> 07:15:24,240
function
11436
07:15:24,240 --> 07:15:27,638
draw snake
11437
07:15:29,120 --> 07:15:30,718
function
11438
07:15:30,718 --> 07:15:34,000
change direction
11439
07:15:34,798 --> 07:15:36,798
function
11440
07:15:36,798 --> 07:15:37,840
check
11441
07:15:37,840 --> 07:15:41,718
game over
11442
07:15:42,320 --> 07:15:44,000
function
11443
07:15:44,000 --> 07:15:48,000
display game over
11444
07:15:48,000 --> 07:15:49,760
then lastly
11445
07:15:49,760 --> 07:15:50,878
function
11446
07:15:50,878 --> 07:15:53,520
reset game
11447
07:15:53,520 --> 07:15:56,638
those are the functions
11448
07:15:56,638 --> 07:15:58,558
let's begin with the create food
11449
07:15:58,558 --> 07:15:59,920
function
11450
07:15:59,920 --> 07:16:02,878
we'll create an inner function
11451
07:16:02,878 --> 07:16:04,080
function
11452
07:16:04,080 --> 07:16:06,320
random food
11453
07:16:06,320 --> 07:16:09,360
there will be two parameters min
11454
07:16:09,360 --> 07:16:11,040
then max
11455
07:16:11,040 --> 07:16:13,040
within this function we'll return a
11456
07:16:13,040 --> 07:16:14,320
random number
11457
07:16:14,320 --> 07:16:15,360
return
11458
07:16:15,360 --> 07:16:16,320
rand
11459
07:16:16,320 --> 07:16:17,440
number
11460
07:16:17,440 --> 07:16:19,600
so to calculate this number we'll store
11461
07:16:19,600 --> 07:16:22,400
this within const rand
11462
07:16:22,400 --> 07:16:23,440
num
11463
07:16:23,440 --> 07:16:26,000
set the sequel to math
11464
07:16:26,000 --> 07:16:28,718
dot round
11465
07:16:28,798 --> 07:16:29,680
math
11466
07:16:29,680 --> 07:16:32,400
dot random
11467
07:16:32,400 --> 07:16:36,000
we will multiply this by
11468
07:16:36,000 --> 07:16:37,120
max
11469
07:16:37,120 --> 07:16:39,280
minus min
11470
07:16:39,280 --> 07:16:41,760
plus the minimum if there is one i think
11471
07:16:41,760 --> 07:16:43,760
i'm going to expand this
11472
07:16:43,760 --> 07:16:45,440
all right just to demonstrate this i'm
11473
07:16:45,440 --> 07:16:48,160
going to invoke the random food function
11474
07:16:48,160 --> 07:16:52,160
and store this within food x
11475
07:16:52,160 --> 07:16:54,958
food x equals random food
11476
07:16:54,958 --> 07:16:57,280
we'll pass the minimum zero
11477
07:16:57,280 --> 07:16:58,878
then the max
11478
07:16:58,878 --> 07:16:59,840
game
11479
07:16:59,840 --> 07:17:00,718
with
11480
07:17:00,718 --> 07:17:04,478
minus our unit size
11481
07:17:04,478 --> 07:17:06,080
temporarily i'm just going to display
11482
07:17:06,080 --> 07:17:08,718
whatever food x is i have to explain
11483
07:17:08,718 --> 07:17:10,000
something
11484
07:17:10,000 --> 07:17:12,000
then we will invoke this create food
11485
07:17:12,000 --> 07:17:14,000
function temporarily
11486
07:17:14,000 --> 07:17:15,920
i gotta explain something
11487
07:17:15,920 --> 07:17:17,440
currently when we're generating a random
11488
07:17:17,440 --> 07:17:19,600
number that random number is between
11489
07:17:19,600 --> 07:17:22,240
zero then our game width minus our unit
11490
07:17:22,240 --> 07:17:24,478
size every time i run this i have a
11491
07:17:24,478 --> 07:17:26,958
random number between that range however
11492
07:17:26,958 --> 07:17:28,878
i would like to divide the width and the
11493
07:17:28,878 --> 07:17:31,440
height of my game in two spaces
11494
07:17:31,440 --> 07:17:34,798
i will take this part of this expression
11495
07:17:34,798 --> 07:17:36,638
divided by
11496
07:17:36,638 --> 07:17:37,760
unit
11497
07:17:37,760 --> 07:17:39,280
size
11498
07:17:39,280 --> 07:17:41,760
now we'll receive a random space between
11499
07:17:41,760 --> 07:17:46,398
0 and 24 because 500 divided by 25
11500
07:17:46,398 --> 07:17:47,840
our unit size
11501
07:17:47,840 --> 07:17:51,760
is 25 available spaces
11502
07:17:51,920 --> 07:17:54,398
to accurately place the food in the top
11503
07:17:54,398 --> 07:17:56,718
left corner of one of these spaces
11504
07:17:56,718 --> 07:18:01,520
i will multiply all of this by unit size
11505
07:18:01,520 --> 07:18:04,160
so every time i refresh this you can see
11506
07:18:04,160 --> 07:18:06,080
that all of these numbers are divisible
11507
07:18:06,080 --> 07:18:08,878
by 25
11508
07:18:08,878 --> 07:18:10,718
and that's what we're looking for let's
11509
07:18:10,718 --> 07:18:13,280
create food why
11510
07:18:13,280 --> 07:18:14,798
food y
11511
07:18:14,798 --> 07:18:16,798
equals the same thing
11512
07:18:16,798 --> 07:18:19,440
okay that is our create food function
11513
07:18:19,440 --> 07:18:22,160
let's move on to the draw food function
11514
07:18:22,160 --> 07:18:24,240
this is fairly easy we'll take our
11515
07:18:24,240 --> 07:18:25,600
context
11516
07:18:25,600 --> 07:18:27,520
set the fill
11517
07:18:27,520 --> 07:18:28,638
style
11518
07:18:28,638 --> 07:18:30,878
equal to our food
11519
07:18:30,878 --> 07:18:32,240
color
11520
07:18:32,240 --> 07:18:34,718
take our context
11521
07:18:34,718 --> 07:18:35,600
fill
11522
07:18:35,600 --> 07:18:38,000
rect to fill a rectangle
11523
07:18:38,000 --> 07:18:40,000
we need a pair of coordinates
11524
07:18:40,000 --> 07:18:41,680
food x
11525
07:18:41,680 --> 07:18:43,280
food y
11526
07:18:43,280 --> 07:18:45,360
we need a width and a height
11527
07:18:45,360 --> 07:18:49,920
they are both going to be unit size
11528
07:18:50,718 --> 07:18:53,040
now if i were to invoke the draw food
11529
07:18:53,040 --> 07:18:54,718
function
11530
07:18:54,718 --> 07:18:56,478
after the create food function
11531
07:18:56,478 --> 07:18:58,398
this will draw a random food item every
11532
07:18:58,398 --> 07:19:00,958
time we refresh the page
11533
07:19:00,958 --> 07:19:02,718
that's what we're looking for that
11534
07:19:02,718 --> 07:19:04,718
completes the create food function and
11535
07:19:04,718 --> 07:19:06,878
the draw food function i'm going to
11536
07:19:06,878 --> 07:19:09,200
eliminate these two function calls
11537
07:19:09,200 --> 07:19:11,040
we're going to fill in the game start
11538
07:19:11,040 --> 07:19:12,398
function
11539
07:19:12,398 --> 07:19:15,200
we will set running equal to true our
11540
07:19:15,200 --> 07:19:17,360
game is currently running
11541
07:19:17,360 --> 07:19:19,600
we'll update the score
11542
07:19:19,600 --> 07:19:21,360
text
11543
07:19:21,360 --> 07:19:22,840
text
11544
07:19:22,840 --> 07:19:25,280
content to equal
11545
07:19:25,280 --> 07:19:27,360
whatever our score is initially it's
11546
07:19:27,360 --> 07:19:28,798
going to be zero
11547
07:19:28,798 --> 07:19:32,320
we'll invoke the create food function
11548
07:19:32,320 --> 07:19:35,680
then the draw food function
11549
07:19:35,680 --> 07:19:38,798
then lastly next tick
11550
07:19:38,798 --> 07:19:40,558
next tick is what we want to do every
11551
07:19:40,558 --> 07:19:42,958
round every time we update the clock
11552
07:19:42,958 --> 07:19:45,200
okay every time i refresh this an apple
11553
07:19:45,200 --> 07:19:46,878
should appear randomly within your game
11554
07:19:46,878 --> 07:19:49,200
board let's close the game start
11555
07:19:49,200 --> 07:19:51,200
function we'll work on the next tick
11556
07:19:51,200 --> 07:19:54,000
function we'll check to see if our game
11557
07:19:54,000 --> 07:19:55,520
is currently running
11558
07:19:55,520 --> 07:19:56,320
if
11559
07:19:56,320 --> 07:19:58,718
running
11560
07:19:59,680 --> 07:20:02,958
let's invoke the set timeout method
11561
07:20:02,958 --> 07:20:05,600
set timeout we'll use an arrow function
11562
07:20:05,600 --> 07:20:08,000
expression
11563
07:20:08,000 --> 07:20:09,760
there's a lot we have to do
11564
07:20:09,760 --> 07:20:12,160
we'll list an order of steps first we'll
11565
07:20:12,160 --> 07:20:16,398
clear the board clear board function
11566
07:20:16,398 --> 07:20:17,760
followed by
11567
07:20:17,760 --> 07:20:18,798
draw
11568
07:20:18,798 --> 07:20:20,398
food
11569
07:20:20,398 --> 07:20:23,600
move snake
11570
07:20:23,760 --> 07:20:26,558
draw snake
11571
07:20:27,200 --> 07:20:29,920
check game over
11572
07:20:29,920 --> 07:20:34,440
then invoke next tick again
11573
07:20:34,878 --> 07:20:37,520
so after the right curly brace
11574
07:20:37,520 --> 07:20:39,440
how often do we want a game tick to
11575
07:20:39,440 --> 07:20:40,320
occur
11576
07:20:40,320 --> 07:20:42,478
maybe 75 milliseconds that's a good
11577
07:20:42,478 --> 07:20:45,200
speed if you would like a slower speed
11578
07:20:45,200 --> 07:20:47,520
you can increase the number or decrease
11579
07:20:47,520 --> 07:20:49,920
the number for a faster speed i'll just
11580
07:20:49,920 --> 07:20:51,440
pick 75.
11581
07:20:51,440 --> 07:20:53,840
then we'll add an else statement
11582
07:20:53,840 --> 07:20:55,200
else
11583
07:20:55,200 --> 07:20:56,478
display
11584
07:20:56,478 --> 07:20:58,958
game over
11585
07:20:58,958 --> 07:21:01,280
if our game currently isn't running that
11586
07:21:01,280 --> 07:21:03,200
means the game's over
11587
07:21:03,200 --> 07:21:06,000
so that is the next tick function
11588
07:21:06,000 --> 07:21:08,240
let's work on clear board
11589
07:21:08,240 --> 07:21:10,080
this is fairly simple
11590
07:21:10,080 --> 07:21:12,398
we'll take our context
11591
07:21:12,398 --> 07:21:14,000
set the fill
11592
07:21:14,000 --> 07:21:16,718
style we'll take our fill style set the
11593
07:21:16,718 --> 07:21:18,958
sequel to board
11594
07:21:18,958 --> 07:21:20,160
background
11595
07:21:20,160 --> 07:21:22,718
this is a color that we picked
11596
07:21:22,718 --> 07:21:25,040
take our context
11597
07:21:25,040 --> 07:21:26,398
fill
11598
07:21:26,398 --> 07:21:27,280
wrecked
11599
07:21:27,280 --> 07:21:30,080
will begin in the top left corner zero
11600
07:21:30,080 --> 07:21:32,320
zero
11601
07:21:32,320 --> 07:21:34,958
will end in the bottom right corner
11602
07:21:34,958 --> 07:21:37,280
we will take our game width
11603
07:21:37,280 --> 07:21:40,478
for the width game height for the height
11604
07:21:40,478 --> 07:21:42,320
that's it for this function
11605
07:21:42,320 --> 07:21:45,280
let's work on draw snake
11606
07:21:45,280 --> 07:21:48,240
we'll need to change the fill style
11607
07:21:48,240 --> 07:21:51,040
context dot
11608
07:21:51,040 --> 07:21:52,160
fill
11609
07:21:52,160 --> 07:21:53,360
style
11610
07:21:53,360 --> 07:21:54,638
equals
11611
07:21:54,638 --> 07:21:55,840
snake
11612
07:21:55,840 --> 07:21:56,718
color
11613
07:21:56,718 --> 07:21:59,200
for my snake i picked green
11614
07:21:59,200 --> 07:22:00,878
if you have a border we can set the
11615
07:22:00,878 --> 07:22:05,120
stroke style context dot stroke
11616
07:22:05,120 --> 07:22:06,638
style
11617
07:22:06,638 --> 07:22:09,600
equals snake
11618
07:22:09,600 --> 07:22:10,878
border
11619
07:22:10,878 --> 07:22:12,798
we'll take our snake it's an array of
11620
07:22:12,798 --> 07:22:13,920
objects
11621
07:22:13,920 --> 07:22:16,000
so there is a for
11622
07:22:16,000 --> 07:22:18,398
each method
11623
07:22:18,398 --> 07:22:20,958
i'll use an arrow function expression
11624
07:22:20,958 --> 07:22:25,120
the argument is snake part
11625
07:22:25,120 --> 07:22:28,080
for every snake part
11626
07:22:28,080 --> 07:22:30,558
then within curly braces
11627
07:22:30,558 --> 07:22:33,200
i will take our context
11628
07:22:33,200 --> 07:22:34,638
fill
11629
07:22:34,638 --> 07:22:37,360
rectangle
11630
07:22:37,600 --> 07:22:38,840
snake
11631
07:22:38,840 --> 07:22:41,680
part each snake part has an x coordinate
11632
07:22:41,680 --> 07:22:43,760
and a y coordinate so we'll begin
11633
07:22:43,760 --> 07:22:45,440
painting wherever that snake part
11634
07:22:45,440 --> 07:22:48,320
currently is on the x-axis
11635
07:22:48,320 --> 07:22:51,520
and the y-axis
11636
07:22:51,600 --> 07:22:53,040
what's the width and the height of the
11637
07:22:53,040 --> 07:22:58,000
snake part well our unit size 25 pixels
11638
07:22:58,000 --> 07:23:01,440
and the height is 25 as well unit size
11639
07:23:01,440 --> 07:23:02,958
hey if you have a border we can fill
11640
07:23:02,958 --> 07:23:05,280
that in
11641
07:23:06,320 --> 07:23:09,840
let's copy this paste it
11642
07:23:11,120 --> 07:23:12,398
stroke
11643
07:23:12,398 --> 07:23:13,840
wrecked
11644
07:23:13,840 --> 07:23:15,600
looks like i misspelt something clear
11645
07:23:15,600 --> 07:23:17,040
board phil
11646
07:23:17,040 --> 07:23:18,080
rhett
11647
07:23:18,080 --> 07:23:20,398
not rexed okay and that will draw our
11648
07:23:20,398 --> 07:23:23,440
snake so now how do we move our snake
11649
07:23:23,440 --> 07:23:26,558
let's go to the move snake function
11650
07:23:26,558 --> 07:23:28,160
to move the snake we're going to create
11651
07:23:28,160 --> 07:23:30,160
a new head of the snake in the direction
11652
07:23:30,160 --> 07:23:32,080
that we're moving then eliminate the
11653
07:23:32,080 --> 07:23:33,280
tail
11654
07:23:33,280 --> 07:23:34,320
const
11655
07:23:34,320 --> 07:23:35,600
head
11656
07:23:35,600 --> 07:23:37,280
equals
11657
07:23:37,280 --> 07:23:39,600
then within curly braces
11658
07:23:39,600 --> 07:23:42,240
the x-coordinate will be r snake's
11659
07:23:42,240 --> 07:23:44,558
current head so that has an index of
11660
07:23:44,558 --> 07:23:47,760
zero take the x-coordinate of that
11661
07:23:47,760 --> 07:23:48,718
plus
11662
07:23:48,718 --> 07:23:51,360
our x velocity
11663
07:23:51,360 --> 07:23:54,240
how far are we moving on the x-axis this
11664
07:23:54,240 --> 07:23:56,000
will be a positive number if we're going
11665
07:23:56,000 --> 07:23:57,680
right a negative number if we're going
11666
07:23:57,680 --> 07:24:00,558
left okay then do the same thing with y
11667
07:24:00,558 --> 07:24:02,958
y equals snake
11668
07:24:02,958 --> 07:24:05,040
at index of zero
11669
07:24:05,040 --> 07:24:06,478
dot y
11670
07:24:06,478 --> 07:24:08,558
plus the y
11671
07:24:08,558 --> 07:24:10,000
velocity
11672
07:24:10,000 --> 07:24:12,478
then to add this new head to our snake
11673
07:24:12,478 --> 07:24:15,200
we can use the unshift method
11674
07:24:15,200 --> 07:24:17,280
snake dot on
11675
07:24:17,280 --> 07:24:18,558
shift
11676
07:24:18,558 --> 07:24:20,798
head
11677
07:24:20,798 --> 07:24:23,200
okay so our snake is just expanding now
11678
07:24:23,200 --> 07:24:24,958
we need to eliminate the tail
11679
07:24:24,958 --> 07:24:26,878
after each move i'll use an if else
11680
07:24:26,878 --> 07:24:29,280
statement
11681
07:24:30,840 --> 07:24:32,798
if
11682
07:24:32,798 --> 07:24:34,958
else
11683
07:24:34,958 --> 07:24:37,760
else will be snake dot pop
11684
07:24:37,760 --> 07:24:39,440
that will eliminate the tail every time
11685
07:24:39,440 --> 07:24:40,478
that we move
11686
07:24:40,478 --> 07:24:42,398
uh but i gotta fill in something for now
11687
07:24:42,398 --> 07:24:45,440
i'm just going to set this to be false
11688
07:24:45,440 --> 07:24:47,040
just temporarily
11689
07:24:47,040 --> 07:24:49,120
okay our snake should move now
11690
07:24:49,120 --> 07:24:50,878
within this if statement we'll check to
11691
07:24:50,878 --> 07:24:53,360
see if the food was eaten
11692
07:24:53,360 --> 07:24:54,160
if
11693
07:24:54,160 --> 07:24:58,320
food is eaten so that's a comment if the
11694
07:24:58,320 --> 07:25:00,638
head of our snake snake
11695
07:25:00,638 --> 07:25:02,798
at index zero
11696
07:25:02,798 --> 07:25:05,200
take the x-coordinate we will use the
11697
07:25:05,200 --> 07:25:06,878
comparison operator
11698
07:25:06,878 --> 07:25:09,600
is the x-coordinate of our snake
11699
07:25:09,600 --> 07:25:12,718
equal to the x-coordinate of our food
11700
07:25:12,718 --> 07:25:14,798
food x
11701
07:25:14,798 --> 07:25:16,240
and
11702
07:25:16,240 --> 07:25:18,320
let's copy this portion
11703
07:25:18,320 --> 07:25:19,920
the y coordinate of the head of the
11704
07:25:19,920 --> 07:25:21,920
snake is equal to
11705
07:25:21,920 --> 07:25:23,520
food y
11706
07:25:23,520 --> 07:25:25,120
if the head of the snake and the food
11707
07:25:25,120 --> 07:25:26,558
are overlapping
11708
07:25:26,558 --> 07:25:29,360
well then the snake has eaten the apple
11709
07:25:29,360 --> 07:25:32,160
let's increment the player score by one
11710
07:25:32,160 --> 07:25:34,718
score plus equals one
11711
07:25:34,718 --> 07:25:36,558
change the score text
11712
07:25:36,558 --> 07:25:37,760
score
11713
07:25:37,760 --> 07:25:38,878
text
11714
07:25:38,878 --> 07:25:40,840
dot text
11715
07:25:40,840 --> 07:25:44,798
content equals the new score
11716
07:25:44,798 --> 07:25:47,040
then create a new food object
11717
07:25:47,040 --> 07:25:48,000
create
11718
07:25:48,000 --> 07:25:49,200
food
11719
07:25:49,200 --> 07:25:50,878
we have no way to test this until we can
11720
07:25:50,878 --> 07:25:53,040
control our snake let's close out of
11721
07:25:53,040 --> 07:25:54,798
this function
11722
07:25:54,798 --> 07:25:57,760
then go to the change direction function
11723
07:25:57,760 --> 07:25:59,360
the change direction function should
11724
07:25:59,360 --> 07:26:02,320
have one parameter event we invoke this
11725
07:26:02,320 --> 07:26:04,398
function every time we press a key our
11726
07:26:04,398 --> 07:26:06,320
window is currently looking for the key
11727
07:26:06,320 --> 07:26:09,680
down event we'll store this within const
11728
07:26:09,680 --> 07:26:10,638
key
11729
07:26:10,638 --> 07:26:13,760
pressed set the sql to event
11730
07:26:13,760 --> 07:26:14,878
dot
11731
07:26:14,878 --> 07:26:15,680
key
11732
07:26:15,680 --> 07:26:16,958
code
11733
07:26:16,958 --> 07:26:18,958
so i'm going to display whatever key
11734
07:26:18,958 --> 07:26:21,840
pressed is currently with console.log
11735
07:26:21,840 --> 07:26:23,360
i'm going to click on my window and
11736
07:26:23,360 --> 07:26:24,718
press
11737
07:26:24,718 --> 07:26:25,440
up
11738
07:26:25,440 --> 07:26:28,000
down left right
11739
07:26:28,000 --> 07:26:30,240
each of those four arrow keys has a key
11740
07:26:30,240 --> 07:26:32,798
number that's what we're looking for
11741
07:26:32,798 --> 07:26:35,520
in fact i'm going to store those
11742
07:26:35,520 --> 07:26:37,520
let's say const
11743
07:26:37,520 --> 07:26:38,718
left
11744
07:26:38,718 --> 07:26:41,200
equals 37
11745
07:26:41,200 --> 07:26:42,478
const
11746
07:26:42,478 --> 07:26:46,080
right equals 39
11747
07:26:46,080 --> 07:26:47,280
const
11748
07:26:47,280 --> 07:26:48,240
up
11749
07:26:48,240 --> 07:26:51,440
equals 38
11750
07:26:51,440 --> 07:26:52,398
const
11751
07:26:52,398 --> 07:26:53,440
down
11752
07:26:53,440 --> 07:26:55,040
equals 40.
11753
07:26:55,040 --> 07:26:57,200
actually i'm gonna rearrange these so
11754
07:26:57,200 --> 07:26:59,440
that they're all in numeric order
11755
07:26:59,440 --> 07:27:01,680
much better this is how to determine
11756
07:27:01,680 --> 07:27:03,920
which way our snake is headed
11757
07:27:03,920 --> 07:27:06,320
i'll store these within some constants
11758
07:27:06,320 --> 07:27:07,520
const
11759
07:27:07,520 --> 07:27:09,520
going up
11760
07:27:09,520 --> 07:27:10,638
equals
11761
07:27:10,638 --> 07:27:12,638
we'll write a condition
11762
07:27:12,638 --> 07:27:16,638
is the y velocity of our snake
11763
07:27:16,638 --> 07:27:18,160
equal to
11764
07:27:18,160 --> 07:27:19,280
negative
11765
07:27:19,280 --> 07:27:21,520
unit size
11766
07:27:21,520 --> 07:27:24,160
so our unit size is currently 25. if the
11767
07:27:24,160 --> 07:27:26,878
y velocity of the snake is negative 25
11768
07:27:26,878 --> 07:27:28,878
that means we're moving up so let's do
11769
07:27:28,878 --> 07:27:31,360
the same with the other directions
11770
07:27:31,360 --> 07:27:33,760
going up
11771
07:27:33,760 --> 07:27:35,120
going
11772
07:27:35,120 --> 07:27:36,398
down
11773
07:27:36,398 --> 07:27:37,760
going
11774
07:27:37,760 --> 07:27:41,200
right going left
11775
07:27:41,200 --> 07:27:44,160
going down is going to be y velocity is
11776
07:27:44,160 --> 07:27:45,920
equal to
11777
07:27:45,920 --> 07:27:49,120
unit size positive then x velocity is
11778
07:27:49,120 --> 07:27:50,320
equal to
11779
07:27:50,320 --> 07:27:52,398
positive unit size
11780
07:27:52,398 --> 07:27:55,760
going left is x velocity is equal to
11781
07:27:55,760 --> 07:27:58,320
negative unit size
11782
07:27:58,320 --> 07:28:00,798
so these will be boolean variables
11783
07:28:00,798 --> 07:28:03,040
i'm going to write a switch
11784
07:28:03,040 --> 07:28:05,040
switch
11785
07:28:05,040 --> 07:28:07,760
will examine true against many matching
11786
07:28:07,760 --> 07:28:09,600
cases
11787
07:28:09,600 --> 07:28:12,798
the first case will be
11788
07:28:12,798 --> 07:28:14,320
key
11789
07:28:14,320 --> 07:28:16,160
pressed
11790
07:28:16,160 --> 07:28:19,120
is equal to left
11791
07:28:19,120 --> 07:28:20,478
and
11792
07:28:20,478 --> 07:28:22,320
we are not
11793
07:28:22,320 --> 07:28:23,440
going
11794
07:28:23,440 --> 07:28:24,398
right
11795
07:28:24,398 --> 07:28:26,240
we don't want to be able to move like to
11796
07:28:26,240 --> 07:28:28,398
the left then immediately to the right
11797
07:28:28,398 --> 07:28:30,398
because according to the rules of snake
11798
07:28:30,398 --> 07:28:32,638
if any body parts touch then we lose the
11799
07:28:32,638 --> 07:28:34,478
game we don't want to accidentally go
11800
07:28:34,478 --> 07:28:36,718
back into any body parts so if we're
11801
07:28:36,718 --> 07:28:39,120
going left we can continue to go left
11802
07:28:39,120 --> 07:28:42,080
otherwise up or down but not right
11803
07:28:42,080 --> 07:28:43,680
because then we lose the game if we
11804
07:28:43,680 --> 07:28:46,478
would like to go left then let's set the
11805
07:28:46,478 --> 07:28:47,360
x
11806
07:28:47,360 --> 07:28:49,920
velocity equal to
11807
07:28:49,920 --> 07:28:50,878
negative
11808
07:28:50,878 --> 07:28:52,798
unit size
11809
07:28:52,798 --> 07:28:55,280
take our y velocity
11810
07:28:55,280 --> 07:28:57,200
set that equal to zero we're no longer
11811
07:28:57,200 --> 07:28:59,120
going up or down
11812
07:28:59,120 --> 07:29:00,798
then break
11813
07:29:00,798 --> 07:29:04,080
let's add another case keypressed equals
11814
07:29:04,080 --> 07:29:06,160
up and we are not
11815
07:29:06,160 --> 07:29:08,558
going down
11816
07:29:08,558 --> 07:29:12,718
x velocity equals zero y velocity equals
11817
07:29:12,718 --> 07:29:15,280
negative unit size
11818
07:29:15,280 --> 07:29:16,958
we need another case
11819
07:29:16,958 --> 07:29:19,360
key pressed equals
11820
07:29:19,360 --> 07:29:22,638
right and we are not going left
11821
07:29:22,638 --> 07:29:26,638
x velocity equals unit size y velocity
11822
07:29:26,638 --> 07:29:28,558
equals zero
11823
07:29:28,558 --> 07:29:30,958
okay last one
11824
07:29:30,958 --> 07:29:33,920
key pressed equals down
11825
07:29:33,920 --> 07:29:37,280
and we are not going up
11826
07:29:37,280 --> 07:29:40,478
x velocity equals zero y velocity
11827
07:29:40,478 --> 07:29:43,840
equals unit size
11828
07:29:44,398 --> 07:29:45,840
okay we should be able to control our
11829
07:29:45,840 --> 07:29:48,958
snake now yeah there we go we can go up
11830
07:29:48,958 --> 07:29:50,718
left down right
11831
07:29:50,718 --> 07:29:52,240
so let's eat the apple and we should
11832
07:29:52,240 --> 07:29:55,200
expand and our score increases now let's
11833
07:29:55,200 --> 07:29:57,440
work on getting a game over because we
11834
07:29:57,440 --> 07:29:59,440
can go off the screen currently which we
11835
07:29:59,440 --> 07:30:01,040
should not be able to do
11836
07:30:01,040 --> 07:30:04,478
okay let's close out of this function
11837
07:30:04,478 --> 07:30:06,160
check game over
11838
07:30:06,160 --> 07:30:08,240
if we pass one of these borders then we
11839
07:30:08,240 --> 07:30:10,398
have a game over i'll write a switch to
11840
07:30:10,398 --> 07:30:11,760
check that
11841
07:30:11,760 --> 07:30:12,878
switch
11842
07:30:12,878 --> 07:30:14,558
will examine true
11843
07:30:14,558 --> 07:30:16,958
against many matching cases
11844
07:30:16,958 --> 07:30:19,280
the first case will be
11845
07:30:19,280 --> 07:30:21,200
is the head of our snake
11846
07:30:21,200 --> 07:30:23,600
snake at index zero
11847
07:30:23,600 --> 07:30:25,440
take the x coordinate
11848
07:30:25,440 --> 07:30:27,600
is that less than zero
11849
07:30:27,600 --> 07:30:30,160
that means we went over the left border
11850
07:30:30,160 --> 07:30:32,798
if this case evaluates to be true well
11851
07:30:32,798 --> 07:30:35,360
we have a matching case so let's take
11852
07:30:35,360 --> 07:30:38,638
running set the sequel to false
11853
07:30:38,638 --> 07:30:40,320
then break
11854
07:30:40,320 --> 07:30:42,160
so if i go over the left border the game
11855
07:30:42,160 --> 07:30:44,240
stops running okay let's add another
11856
07:30:44,240 --> 07:30:45,360
case
11857
07:30:45,360 --> 07:30:47,280
if the x coordinate of the head of our
11858
07:30:47,280 --> 07:30:48,478
snake
11859
07:30:48,478 --> 07:30:51,600
is greater than or equal to the game
11860
07:30:51,600 --> 07:30:53,600
width
11861
07:30:53,600 --> 07:30:56,080
now if we go over the right border
11862
07:30:56,080 --> 07:30:58,798
the game ends
11863
07:30:58,798 --> 07:31:00,558
take the y coordinate of the head of our
11864
07:31:00,558 --> 07:31:01,600
snake
11865
07:31:01,600 --> 07:31:03,440
check to see if it's
11866
07:31:03,440 --> 07:31:04,398
less
11867
07:31:04,398 --> 07:31:07,840
than zero that means we go over the top
11868
07:31:07,840 --> 07:31:11,680
the game's over then the last one is
11869
07:31:11,680 --> 07:31:12,558
case
11870
07:31:12,558 --> 07:31:16,080
y is greater than or equal to
11871
07:31:16,080 --> 07:31:17,280
game
11872
07:31:17,280 --> 07:31:19,280
height we shouldn't be able to go over
11873
07:31:19,280 --> 07:31:21,440
the bottom border yeah
11874
07:31:21,440 --> 07:31:23,040
awesome there is another game over
11875
07:31:23,040 --> 07:31:25,520
condition two if any body parts of the
11876
07:31:25,520 --> 07:31:27,680
snake overlap to check that i'm going to
11877
07:31:27,680 --> 07:31:29,920
use a for loop to iterate over the body
11878
07:31:29,920 --> 07:31:32,240
parts
11879
07:31:32,240 --> 07:31:33,120
four
11880
07:31:33,120 --> 07:31:36,000
then our index is going to be i we don't
11881
07:31:36,000 --> 07:31:39,040
want to begin at the head
11882
07:31:39,040 --> 07:31:41,840
let i equal one that's why i'm not using
11883
07:31:41,840 --> 07:31:44,160
the for each method our condition to
11884
07:31:44,160 --> 07:31:48,080
continue is i is less than snake dot
11885
07:31:48,080 --> 07:31:49,600
length property
11886
07:31:49,600 --> 07:31:51,680
increment i by one
11887
07:31:51,680 --> 07:31:54,798
we'll write an if statement
11888
07:31:54,878 --> 07:31:55,840
if
11889
07:31:55,840 --> 07:31:57,360
our snake
11890
07:31:57,360 --> 07:31:59,520
at index of i that's going to be one of
11891
07:31:59,520 --> 07:32:01,920
the body parts besides the head because
11892
07:32:01,920 --> 07:32:03,440
we're starting at one
11893
07:32:03,440 --> 07:32:06,160
if the x coordinate of that body part
11894
07:32:06,160 --> 07:32:07,600
is equal to
11895
07:32:07,600 --> 07:32:10,398
snake at index of zero
11896
07:32:10,398 --> 07:32:12,398
take that x coordinate
11897
07:32:12,398 --> 07:32:15,840
and let's copy this
11898
07:32:15,840 --> 07:32:17,440
paste it
11899
07:32:17,440 --> 07:32:20,558
change x to y
11900
07:32:20,878 --> 07:32:23,440
if the head of our snake is equal to one
11901
07:32:23,440 --> 07:32:25,760
of the body parts that means the game's
11902
07:32:25,760 --> 07:32:26,958
over
11903
07:32:26,958 --> 07:32:29,920
running equals false okay let's test
11904
07:32:29,920 --> 07:32:31,200
that
11905
07:32:31,200 --> 07:32:33,040
yeah the game just ended now we'll want
11906
07:32:33,040 --> 07:32:35,120
to display game over let's close out of
11907
07:32:35,120 --> 07:32:37,280
the check game over function then head
11908
07:32:37,280 --> 07:32:39,360
to display game over i'll take the
11909
07:32:39,360 --> 07:32:41,360
context
11910
07:32:41,360 --> 07:32:44,240
access the font set the sql to a font of
11911
07:32:44,240 --> 07:32:47,360
your choosing i'll pick 50 pixels
11912
07:32:47,360 --> 07:32:48,478
mv
11913
07:32:48,478 --> 07:32:49,680
bully
11914
07:32:49,680 --> 07:32:52,718
context dot fill
11915
07:32:52,718 --> 07:32:54,160
style
11916
07:32:54,160 --> 07:32:56,958
pick a color i'll just pick black
11917
07:32:56,958 --> 07:33:00,160
i'll center the text context
11918
07:33:00,160 --> 07:33:02,240
text align
11919
07:33:02,240 --> 07:33:03,440
equals
11920
07:33:03,440 --> 07:33:04,638
center
11921
07:33:04,638 --> 07:33:06,878
to draw some text on a canvas we'll type
11922
07:33:06,878 --> 07:33:10,000
context dot fill
11923
07:33:10,000 --> 07:33:11,760
text
11924
07:33:11,760 --> 07:33:13,840
add a string of text
11925
07:33:13,840 --> 07:33:15,520
game over
11926
07:33:15,520 --> 07:33:17,200
then your placement
11927
07:33:17,200 --> 07:33:18,878
i'll put this right in the middle
11928
07:33:18,878 --> 07:33:19,920
game
11929
07:33:19,920 --> 07:33:22,638
width divided by two
11930
07:33:22,638 --> 07:33:23,760
comma
11931
07:33:23,760 --> 07:33:24,958
game
11932
07:33:24,958 --> 07:33:25,760
height
11933
07:33:25,760 --> 07:33:27,360
divided by two
11934
07:33:27,360 --> 07:33:29,360
then set running
11935
07:33:29,360 --> 07:33:32,160
equal to false
11936
07:33:32,160 --> 07:33:34,240
okay so when we hit a border
11937
07:33:34,240 --> 07:33:35,600
it's game over
11938
07:33:35,600 --> 07:33:36,958
then when the head of our snake runs
11939
07:33:36,958 --> 07:33:39,920
into a body part it's also game over
11940
07:33:39,920 --> 07:33:41,600
the last thing we need to do is set up
11941
07:33:41,600 --> 07:33:43,600
the reset game function
11942
07:33:43,600 --> 07:33:45,760
we'll take our score set that back to
11943
07:33:45,760 --> 07:33:47,280
zero
11944
07:33:47,280 --> 07:33:50,000
take the x velocity
11945
07:33:50,000 --> 07:33:54,000
set that equal to unit size
11946
07:33:54,000 --> 07:33:56,398
take our y velocity
11947
07:33:56,398 --> 07:33:58,958
set that equal to zero
11948
07:33:58,958 --> 07:34:02,000
we'll recreate our snake
11949
07:34:02,000 --> 07:34:03,920
copy that
11950
07:34:03,920 --> 07:34:06,398
paste it but we're not going to use the
11951
07:34:06,398 --> 07:34:07,680
let keyword
11952
07:34:07,680 --> 07:34:12,398
then invoke the game start function
11953
07:34:12,398 --> 07:34:14,320
so after we get game over we should be
11954
07:34:14,320 --> 07:34:16,240
able to reset our game
11955
07:34:16,240 --> 07:34:18,958
yeah sweet well okay then everybody that
11956
07:34:18,958 --> 07:34:21,760
is a game of snake using javascript if
11957
07:34:21,760 --> 07:34:23,360
you would like a copy of all this code
11958
07:34:23,360 --> 07:34:25,040
i'll post this in the comments section
11959
07:34:25,040 --> 07:34:27,280
down below be sure to check the original
11960
07:34:27,280 --> 07:34:29,360
playlist and well yeah that's how to
11961
07:34:29,360 --> 07:34:32,320
create a game of snake using javascript
11962
07:34:32,320 --> 07:34:34,080
hey what's going on everybody it's you
11963
07:34:34,080 --> 07:34:35,680
bro hope you're doing well and in this
11964
07:34:35,680 --> 07:34:37,440
video we're going to create a game of
11965
07:34:37,440 --> 07:34:40,320
pong using javascript so sit back relax
11966
07:34:40,320 --> 07:34:42,878
and enjoy the show
11967
07:34:42,878 --> 07:34:44,718
all right let's begin everybody let's
11968
07:34:44,718 --> 07:34:48,160
create a div section to contain our game
11969
07:34:48,160 --> 07:34:49,440
div
11970
07:34:49,440 --> 07:34:50,958
close it
11971
07:34:50,958 --> 07:34:53,680
the id will be game
11972
07:34:53,680 --> 07:34:56,000
container
11973
07:34:56,000 --> 07:34:58,080
we'll add a canvas
11974
07:34:58,080 --> 07:34:59,200
canvas
11975
07:34:59,200 --> 07:35:00,798
close it
11976
07:35:00,798 --> 07:35:02,638
the id will be
11977
07:35:02,638 --> 07:35:03,520
game
11978
07:35:03,520 --> 07:35:05,040
board
11979
07:35:05,040 --> 07:35:07,440
i'll set a width and a height in line
11980
07:35:07,440 --> 07:35:09,760
the width will be 500
11981
07:35:09,760 --> 07:35:12,958
the height will be 500 we'll add a score
11982
07:35:12,958 --> 07:35:16,000
this will be another div section
11983
07:35:16,000 --> 07:35:18,958
id equals score
11984
07:35:18,958 --> 07:35:20,000
text
11985
07:35:20,000 --> 07:35:22,320
the initial text will be 0
11986
07:35:22,320 --> 07:35:24,638
colon 0
11987
07:35:24,638 --> 07:35:26,878
then a reset button
11988
07:35:26,878 --> 07:35:27,920
button
11989
07:35:27,920 --> 07:35:29,520
close it
11990
07:35:29,520 --> 07:35:33,120
the id is reset button
11991
07:35:33,120 --> 07:35:35,360
the text is reset
11992
07:35:35,360 --> 07:35:38,080
cool that's our html file let's head to
11993
07:35:38,080 --> 07:35:39,920
our css style sheet
11994
07:35:39,920 --> 07:35:42,320
id game board
11995
07:35:42,320 --> 07:35:43,600
border
11996
07:35:43,600 --> 07:35:45,680
3 pixel solid
11997
07:35:45,680 --> 07:35:49,120
let's change our score text next
11998
07:35:49,120 --> 07:35:51,520
score text
11999
07:35:51,520 --> 07:35:54,080
pick a font family font
12000
07:35:54,080 --> 07:35:55,920
family
12001
07:35:55,920 --> 07:35:59,120
pick a font that you like
12002
07:35:59,120 --> 07:36:01,680
then i'll add a backup
12003
07:36:01,680 --> 07:36:05,120
i'll change the font size to 100 pixels
12004
07:36:05,120 --> 07:36:06,798
that should be readable
12005
07:36:06,798 --> 07:36:09,120
okay let's work on the button
12006
07:36:09,120 --> 07:36:10,878
reset button
12007
07:36:10,878 --> 07:36:15,280
i'll set a font of permanent marker
12008
07:36:15,600 --> 07:36:19,360
then a backup of cursive
12009
07:36:19,360 --> 07:36:20,478
font
12010
07:36:20,478 --> 07:36:22,000
size
12011
07:36:22,000 --> 07:36:24,718
22 pixels
12012
07:36:24,718 --> 07:36:29,040
a width of 100 pixels
12013
07:36:29,040 --> 07:36:30,558
a height
12014
07:36:30,558 --> 07:36:33,440
of 50 pixels
12015
07:36:33,440 --> 07:36:37,280
border 4 pixels solid
12016
07:36:37,280 --> 07:36:38,798
border
12017
07:36:38,798 --> 07:36:40,958
radius
12018
07:36:40,958 --> 07:36:43,760
15 pixels
12019
07:36:43,760 --> 07:36:45,680
then cursor
12020
07:36:45,680 --> 07:36:46,958
pointer
12021
07:36:46,958 --> 07:36:48,638
when we hover our cursor over this
12022
07:36:48,638 --> 07:36:51,200
button the cursor is now a pointer then
12023
07:36:51,200 --> 07:36:53,680
let's enter everything i will select our
12024
07:36:53,680 --> 07:36:56,398
game container
12025
07:36:58,080 --> 07:37:00,478
text align
12026
07:37:00,478 --> 07:37:01,680
center
12027
07:37:01,680 --> 07:37:04,080
all right there we go so that's our css
12028
07:37:04,080 --> 07:37:06,718
style sheet and our html file let's head
12029
07:37:06,718 --> 07:37:08,398
to our javascript file
12030
07:37:08,398 --> 07:37:11,280
let's declare all the constants first
12031
07:37:11,280 --> 07:37:13,840
const game board
12032
07:37:13,840 --> 07:37:16,080
equals document dot
12033
07:37:16,080 --> 07:37:19,360
query selector
12034
07:37:19,360 --> 07:37:24,160
we are selecting our game board
12035
07:37:25,200 --> 07:37:26,478
const
12036
07:37:26,478 --> 07:37:28,478
context
12037
07:37:28,478 --> 07:37:31,360
equals game board
12038
07:37:31,360 --> 07:37:33,200
dot get
12039
07:37:33,200 --> 07:37:36,200
context
12040
07:37:36,798 --> 07:37:38,638
2d
12041
07:37:38,638 --> 07:37:41,280
context is what we draw on
12042
07:37:41,280 --> 07:37:44,398
const score text
12043
07:37:44,398 --> 07:37:46,958
equals document dot
12044
07:37:46,958 --> 07:37:50,520
query selector
12045
07:37:51,040 --> 07:37:55,440
we are selecting the score text
12046
07:37:55,440 --> 07:37:58,558
i'm just going to copy this
12047
07:37:58,798 --> 07:38:02,000
then we have our reset button
12048
07:38:02,000 --> 07:38:05,760
id is reset button
12049
07:38:05,760 --> 07:38:07,200
let's store the game with within a
12050
07:38:07,200 --> 07:38:11,360
variable what's the size of our canvas
12051
07:38:11,360 --> 07:38:13,040
const
12052
07:38:13,040 --> 07:38:14,718
game with
12053
07:38:14,718 --> 07:38:16,080
equals
12054
07:38:16,080 --> 07:38:19,520
game board dot width attribute
12055
07:38:19,520 --> 07:38:21,840
then we have height
12056
07:38:21,840 --> 07:38:23,600
game
12057
07:38:23,600 --> 07:38:25,920
height
12058
07:38:26,398 --> 07:38:29,520
equals game board dot height i'm only
12059
07:38:29,520 --> 07:38:31,040
storing them within a variable so that
12060
07:38:31,040 --> 07:38:32,878
they're accessible let's select some
12061
07:38:32,878 --> 07:38:34,000
colors
12062
07:38:34,000 --> 07:38:35,520
const
12063
07:38:35,520 --> 07:38:37,600
board
12064
07:38:37,600 --> 07:38:38,878
background
12065
07:38:38,878 --> 07:38:40,478
what color
12066
07:38:40,478 --> 07:38:42,558
i'll select forest green kind of like
12067
07:38:42,558 --> 07:38:44,798
it's a tennis court
12068
07:38:44,798 --> 07:38:48,398
const paddle one color
12069
07:38:48,398 --> 07:38:49,920
what color would you like the first
12070
07:38:49,920 --> 07:38:53,920
player to be i'll pick light blue
12071
07:38:54,878 --> 07:38:56,798
then paddle two
12072
07:38:56,798 --> 07:38:59,280
paddle two color
12073
07:38:59,280 --> 07:39:01,120
player two will be red
12074
07:39:01,120 --> 07:39:03,120
if you want you can add a border
12075
07:39:03,120 --> 07:39:04,240
const
12076
07:39:04,240 --> 07:39:06,000
paddle border
12077
07:39:06,000 --> 07:39:08,160
equals black
12078
07:39:08,160 --> 07:39:10,798
const ball color
12079
07:39:10,798 --> 07:39:12,478
i'll make this yellow like it's a tennis
12080
07:39:12,478 --> 07:39:14,320
ball
12081
07:39:14,320 --> 07:39:15,520
const
12082
07:39:15,520 --> 07:39:16,398
ball
12083
07:39:16,398 --> 07:39:17,440
border
12084
07:39:17,440 --> 07:39:18,798
color
12085
07:39:18,798 --> 07:39:21,200
i'll pick black
12086
07:39:21,200 --> 07:39:22,798
so what do we want the size of the ball
12087
07:39:22,798 --> 07:39:25,040
to be i'll store that as a constant
12088
07:39:25,040 --> 07:39:26,478
const
12089
07:39:26,478 --> 07:39:27,840
ball
12090
07:39:27,840 --> 07:39:29,280
radius
12091
07:39:29,280 --> 07:39:30,958
i'll set this to be
12092
07:39:30,958 --> 07:39:32,718
12.5
12093
07:39:32,718 --> 07:39:35,520
then the full diameter is 25 then our
12094
07:39:35,520 --> 07:39:37,600
paddle speed how far do we want our
12095
07:39:37,600 --> 07:39:39,360
paddles to move when we press down on a
12096
07:39:39,360 --> 07:39:40,958
button
12097
07:39:40,958 --> 07:39:43,040
paddle speed
12098
07:39:43,040 --> 07:39:46,000
equals 50. that's good enough
12099
07:39:46,000 --> 07:39:47,440
feel free to change this number if you'd
12100
07:39:47,440 --> 07:39:50,558
like now we have let
12101
07:39:50,558 --> 07:39:51,680
interval
12102
07:39:51,680 --> 07:39:54,160
id
12103
07:39:54,718 --> 07:39:55,600
let
12104
07:39:55,600 --> 07:39:57,440
ball speed
12105
07:39:57,440 --> 07:39:59,600
i'll set this to be one that will be the
12106
07:39:59,600 --> 07:40:01,120
lowest speed
12107
07:40:01,120 --> 07:40:03,200
then ball x and ball wide they will be
12108
07:40:03,200 --> 07:40:06,320
coordinates let ball x
12109
07:40:06,320 --> 07:40:08,320
equal initially i'll place the ball
12110
07:40:08,320 --> 07:40:11,120
right in the center of our game board
12111
07:40:11,120 --> 07:40:13,040
i will take game
12112
07:40:13,040 --> 07:40:15,920
width divided by two
12113
07:40:15,920 --> 07:40:18,160
then ball y is going to be
12114
07:40:18,160 --> 07:40:21,040
game height divided by two
12115
07:40:21,040 --> 07:40:22,320
let
12116
07:40:22,320 --> 07:40:25,600
ball x direction
12117
07:40:25,600 --> 07:40:27,680
i'll set that to be zero ball x
12118
07:40:27,680 --> 07:40:29,760
direction will be the direction in which
12119
07:40:29,760 --> 07:40:32,478
the ball is headed on the x axis
12120
07:40:32,478 --> 07:40:34,000
then ball
12121
07:40:34,000 --> 07:40:36,798
y direction
12122
07:40:37,200 --> 07:40:41,600
let player one score equal
12123
07:40:41,600 --> 07:40:43,040
zero
12124
07:40:43,040 --> 07:40:47,120
let player two score equal zero
12125
07:40:47,120 --> 07:40:49,520
we'll define two paddle objects
12126
07:40:49,520 --> 07:40:51,600
let paddle one
12127
07:40:51,600 --> 07:40:52,718
equal
12128
07:40:52,718 --> 07:40:55,600
this paddle will have a width property
12129
07:40:55,600 --> 07:40:58,478
of 25
12130
07:40:58,478 --> 07:41:00,240
a height
12131
07:41:00,240 --> 07:41:02,558
of 100
12132
07:41:02,558 --> 07:41:04,558
these are in pixels the starting x
12133
07:41:04,558 --> 07:41:06,718
coordinate will be zero all the way at
12134
07:41:06,718 --> 07:41:08,638
the left border
12135
07:41:08,638 --> 07:41:10,798
then y zero
12136
07:41:10,798 --> 07:41:12,878
so this petal will be in the top left
12137
07:41:12,878 --> 07:41:14,320
corner initially
12138
07:41:14,320 --> 07:41:16,878
so let's copy this paste it
12139
07:41:16,878 --> 07:41:19,200
change paddle one to paddle two
12140
07:41:19,200 --> 07:41:20,398
the width and the height will be the
12141
07:41:20,398 --> 07:41:22,558
same i would like paddle 2 to be on the
12142
07:41:22,558 --> 07:41:24,718
other side of my canvas
12143
07:41:24,718 --> 07:41:27,440
i will set x to equal game
12144
07:41:27,440 --> 07:41:29,440
width then i'm going to subtract
12145
07:41:29,440 --> 07:41:32,000
negative 25 whatever the width is if i
12146
07:41:32,000 --> 07:41:34,798
set x to be game with the left border is
12147
07:41:34,798 --> 07:41:36,878
going to be lined up with the right
12148
07:41:36,878 --> 07:41:38,798
border of my game board so i'm just
12149
07:41:38,798 --> 07:41:41,040
going to shift it over by the width 25
12150
07:41:41,040 --> 07:41:43,280
pixels so that you can actually see it
12151
07:41:43,280 --> 07:41:46,080
then y is game height
12152
07:41:46,080 --> 07:41:48,398
minus the height of our paddle
12153
07:41:48,398 --> 07:41:50,240
and those are the two paddles
12154
07:41:50,240 --> 07:41:51,680
let's add an event listener to the
12155
07:41:51,680 --> 07:41:54,638
window to listen for key down events
12156
07:41:54,638 --> 07:41:56,798
window dot add
12157
07:41:56,798 --> 07:41:57,840
event
12158
07:41:57,840 --> 07:41:59,040
listener
12159
07:41:59,040 --> 07:42:00,718
we will listen for
12160
07:42:00,718 --> 07:42:03,680
key down
12161
07:42:03,680 --> 07:42:06,878
then we will invoke a change direction
12162
07:42:06,878 --> 07:42:08,080
function
12163
07:42:08,080 --> 07:42:09,920
let's add an event listener to our reset
12164
07:42:09,920 --> 07:42:11,360
button
12165
07:42:11,360 --> 07:42:12,558
reset
12166
07:42:12,558 --> 07:42:13,520
button
12167
07:42:13,520 --> 07:42:17,680
dot add event listener
12168
07:42:17,680 --> 07:42:19,920
we are waiting for click
12169
07:42:19,920 --> 07:42:23,600
then invoke the reset game function
12170
07:42:23,600 --> 07:42:25,120
we'll still need to declare these two
12171
07:42:25,120 --> 07:42:27,120
functions when we would like to start
12172
07:42:27,120 --> 07:42:29,760
our game we'll invoke the game start
12173
07:42:29,760 --> 07:42:31,840
function then we'll need to declare
12174
07:42:31,840 --> 07:42:34,240
these functions the first function is
12175
07:42:34,240 --> 07:42:35,280
function
12176
07:42:35,280 --> 07:42:38,080
game start
12177
07:42:38,080 --> 07:42:40,558
we'll fill these in momentarily
12178
07:42:40,558 --> 07:42:42,840
function next
12179
07:42:42,840 --> 07:42:45,120
tick function
12180
07:42:45,120 --> 07:42:48,160
clear board
12181
07:42:48,160 --> 07:42:49,200
function
12182
07:42:49,200 --> 07:42:51,920
draw paddles
12183
07:42:51,920 --> 07:42:55,120
function create ball
12184
07:42:55,120 --> 07:42:56,240
function
12185
07:42:56,240 --> 07:42:58,718
move ball
12186
07:42:58,718 --> 07:43:00,000
function
12187
07:43:00,000 --> 07:43:02,638
draw ball
12188
07:43:03,200 --> 07:43:04,558
function
12189
07:43:04,558 --> 07:43:05,520
check
12190
07:43:05,520 --> 07:43:08,080
collision
12191
07:43:08,398 --> 07:43:09,520
function
12192
07:43:09,520 --> 07:43:10,840
change
12193
07:43:10,840 --> 07:43:13,280
direction function
12194
07:43:13,280 --> 07:43:16,320
update score
12195
07:43:16,320 --> 07:43:17,440
function
12196
07:43:17,440 --> 07:43:20,080
reset game
12197
07:43:20,080 --> 07:43:22,320
okay those are all the functions
12198
07:43:22,320 --> 07:43:23,760
why don't we begin by drawing the
12199
07:43:23,760 --> 07:43:25,920
paddles we'll start with the easy stuff
12200
07:43:25,920 --> 07:43:28,638
so we will take our context
12201
07:43:28,638 --> 07:43:30,558
set the stroke
12202
07:43:30,558 --> 07:43:33,840
style equal to the paddle
12203
07:43:33,840 --> 07:43:35,040
border
12204
07:43:35,040 --> 07:43:37,360
let's begin by drawing paddle one
12205
07:43:37,360 --> 07:43:39,440
context
12206
07:43:39,440 --> 07:43:41,200
dot fill
12207
07:43:41,200 --> 07:43:42,478
style
12208
07:43:42,478 --> 07:43:43,840
equals
12209
07:43:43,840 --> 07:43:44,878
paddle
12210
07:43:44,878 --> 07:43:46,080
one
12211
07:43:46,080 --> 07:43:47,920
color
12212
07:43:47,920 --> 07:43:49,520
context
12213
07:43:49,520 --> 07:43:50,638
fill
12214
07:43:50,638 --> 07:43:52,718
rectangle
12215
07:43:52,718 --> 07:43:54,958
the first two arguments are the x and
12216
07:43:54,958 --> 07:43:57,040
the y coordinate of the top left corner
12217
07:43:57,040 --> 07:43:59,040
of the rectangle
12218
07:43:59,040 --> 07:44:00,240
that will be
12219
07:44:00,240 --> 07:44:02,638
paddle one dot x
12220
07:44:02,638 --> 07:44:04,240
then paddle
12221
07:44:04,240 --> 07:44:06,320
one dot y
12222
07:44:06,320 --> 07:44:08,320
the next two arguments are the width and
12223
07:44:08,320 --> 07:44:10,080
the height of this rectangle
12224
07:44:10,080 --> 07:44:13,600
the width will be paddle one dot width
12225
07:44:13,600 --> 07:44:16,878
then paddle one dot height
12226
07:44:16,878 --> 07:44:18,718
to test this i'm going to invoke draw
12227
07:44:18,718 --> 07:44:20,840
paddles right after game start
12228
07:44:20,840 --> 07:44:22,718
temporarily okay there's our first
12229
07:44:22,718 --> 07:44:24,718
paddle but let's add a border we can
12230
07:44:24,718 --> 07:44:27,440
just copy this
12231
07:44:27,440 --> 07:44:29,280
paste it
12232
07:44:29,280 --> 07:44:32,638
change fill rect to stroke
12233
07:44:32,638 --> 07:44:33,680
rat
12234
07:44:33,680 --> 07:44:36,000
okay there is our first paddle let's
12235
07:44:36,000 --> 07:44:38,878
copy these three lines of code
12236
07:44:38,878 --> 07:44:40,638
paste it
12237
07:44:40,638 --> 07:44:44,878
change paddle one to paddle two
12238
07:44:47,920 --> 07:44:49,920
and there's our second paddle i'll
12239
07:44:49,920 --> 07:44:52,160
eliminate this function call
12240
07:44:52,160 --> 07:44:54,240
let's fill in game start when we begin
12241
07:44:54,240 --> 07:44:56,558
the game what are we going to do first
12242
07:44:56,558 --> 07:44:58,718
we'll create a ball
12243
07:44:58,718 --> 07:45:01,920
then invoke the next tick function
12244
07:45:01,920 --> 07:45:05,840
let's fill in the next tick function
12245
07:45:06,080 --> 07:45:10,320
we will take our set interval id
12246
07:45:10,320 --> 07:45:12,558
set this equal to
12247
07:45:12,558 --> 07:45:13,520
set
12248
07:45:13,520 --> 07:45:15,840
timeout method
12249
07:45:15,840 --> 07:45:19,280
we'll pass in a callback
12250
07:45:19,280 --> 07:45:20,958
then we'll add a routine what are all
12251
07:45:20,958 --> 07:45:23,120
the things that we're gonna do first we
12252
07:45:23,120 --> 07:45:25,840
will clear and redraw our board
12253
07:45:25,840 --> 07:45:26,718
clear
12254
07:45:26,718 --> 07:45:28,398
board
12255
07:45:28,398 --> 07:45:30,320
then draw
12256
07:45:30,320 --> 07:45:32,718
paddles
12257
07:45:32,718 --> 07:45:33,840
move
12258
07:45:33,840 --> 07:45:36,160
ball
12259
07:45:36,160 --> 07:45:38,718
draw ball
12260
07:45:38,718 --> 07:45:42,000
draw ball is going to have two arguments
12261
07:45:42,000 --> 07:45:44,160
ball x
12262
07:45:44,160 --> 07:45:45,760
ball y
12263
07:45:45,760 --> 07:45:47,120
and we should probably fill that in
12264
07:45:47,120 --> 07:45:49,520
within the parameters
12265
07:45:49,520 --> 07:45:50,398
there
12266
07:45:50,398 --> 07:45:54,240
then we will check collision
12267
07:45:55,040 --> 07:45:57,200
then invoke next tick
12268
07:45:57,200 --> 07:45:59,280
for another round i would like to repeat
12269
07:45:59,280 --> 07:46:02,320
this routine every 10 milliseconds
12270
07:46:02,320 --> 07:46:04,798
that is the next tick function let's
12271
07:46:04,798 --> 07:46:07,200
fill in the clear board function
12272
07:46:07,200 --> 07:46:09,280
all we're doing is redrawing our board
12273
07:46:09,280 --> 07:46:11,520
let's take our context
12274
07:46:11,520 --> 07:46:13,360
set the fill
12275
07:46:13,360 --> 07:46:17,200
style equal to the board
12276
07:46:17,200 --> 07:46:18,558
background
12277
07:46:18,558 --> 07:46:20,478
context dot
12278
07:46:20,478 --> 07:46:21,760
fill
12279
07:46:21,760 --> 07:46:22,718
rect
12280
07:46:22,718 --> 07:46:26,240
will begin in the top left corner 0 0
12281
07:46:26,240 --> 07:46:28,878
the width will be game width
12282
07:46:28,878 --> 07:46:31,760
the height will be game height
12283
07:46:31,760 --> 07:46:33,840
and that's our background
12284
07:46:33,840 --> 07:46:36,080
so that is the clear board function
12285
07:46:36,080 --> 07:46:38,160
we're redrawing our canvas
12286
07:46:38,160 --> 07:46:39,920
let's head to the change direction
12287
07:46:39,920 --> 07:46:41,920
function this will be in charge of
12288
07:46:41,920 --> 07:46:43,440
moving the paddles
12289
07:46:43,440 --> 07:46:45,040
but we'll need to know what key that we
12290
07:46:45,040 --> 07:46:47,600
pressed i'll store that as a constant
12291
07:46:47,600 --> 07:46:48,398
key
12292
07:46:48,398 --> 07:46:50,320
pressed
12293
07:46:50,320 --> 07:46:53,360
equals our event dot
12294
07:46:53,360 --> 07:46:55,120
key code
12295
07:46:55,120 --> 07:46:57,280
uh so we do have one parameter that's
12296
07:46:57,280 --> 07:46:59,920
event so fill that in let's display
12297
07:46:59,920 --> 07:47:01,840
whatever key pressed is
12298
07:47:01,840 --> 07:47:04,558
console.log key pressed
12299
07:47:04,558 --> 07:47:07,200
w has a key code of 87
12300
07:47:07,200 --> 07:47:08,878
s is 83.
12301
07:47:08,878 --> 07:47:11,200
the up arrow key is 38
12302
07:47:11,200 --> 07:47:13,840
the down arrow key is 40. so we can use
12303
07:47:13,840 --> 07:47:16,080
that for something
12304
07:47:16,080 --> 07:47:18,878
const paddle 1
12305
07:47:18,878 --> 07:47:19,840
up
12306
07:47:19,840 --> 07:47:22,798
equals 87
12307
07:47:22,798 --> 07:47:25,440
const paddle 1
12308
07:47:25,440 --> 07:47:26,718
down
12309
07:47:26,718 --> 07:47:29,760
equals 83
12310
07:47:29,760 --> 07:47:32,320
const paddle 2 up
12311
07:47:32,320 --> 07:47:33,760
is 38
12312
07:47:33,760 --> 07:47:36,160
const paddle 2 down
12313
07:47:36,160 --> 07:47:38,958
is 40. let's write a switch to look at
12314
07:47:38,958 --> 07:47:41,040
key pressed
12315
07:47:41,040 --> 07:47:43,680
switch we're examining key pressed
12316
07:47:43,680 --> 07:47:46,558
against many matching cases
12317
07:47:46,558 --> 07:47:48,798
the first case is
12318
07:47:48,798 --> 07:47:51,360
paddle one up
12319
07:47:51,360 --> 07:47:54,000
if we press up we will take
12320
07:47:54,000 --> 07:47:55,680
paddle one
12321
07:47:55,680 --> 07:47:58,558
axis the y property
12322
07:47:58,558 --> 07:48:01,440
minus equals our paddle
12323
07:48:01,440 --> 07:48:02,398
speed
12324
07:48:02,398 --> 07:48:04,558
you could also think of paddle speed as
12325
07:48:04,558 --> 07:48:06,558
paddle distance how far we're going to
12326
07:48:06,558 --> 07:48:08,638
move then break
12327
07:48:08,638 --> 07:48:10,798
so we should be able to move up
12328
07:48:10,798 --> 07:48:13,840
but we can't move down
12329
07:48:13,920 --> 07:48:15,280
paddle one
12330
07:48:15,280 --> 07:48:16,240
down
12331
07:48:16,240 --> 07:48:18,878
the y property of paddle one
12332
07:48:18,878 --> 07:48:21,760
plus equals paddle speed
12333
07:48:21,760 --> 07:48:23,760
by pressing s we can move down by
12334
07:48:23,760 --> 07:48:25,920
pressing w we can move up but currently
12335
07:48:25,920 --> 07:48:28,080
we're exiting the game board let's write
12336
07:48:28,080 --> 07:48:30,320
an if statement to check that
12337
07:48:30,320 --> 07:48:31,440
if
12338
07:48:31,440 --> 07:48:36,160
paddle 1 dot y is greater than zero
12339
07:48:36,160 --> 07:48:39,120
then we will move
12340
07:48:39,120 --> 07:48:41,360
then within the second case paddle one
12341
07:48:41,360 --> 07:48:43,840
down we'll write another if statement
12342
07:48:43,840 --> 07:48:47,120
if the y property of paddle one is
12343
07:48:47,120 --> 07:48:49,280
greater than
12344
07:48:49,280 --> 07:48:50,240
game
12345
07:48:50,240 --> 07:48:52,558
height
12346
07:48:52,638 --> 07:48:54,798
okay now check this out so we can't go
12347
07:48:54,798 --> 07:48:56,320
above the border
12348
07:48:56,320 --> 07:48:58,638
but we can go below the border up to a
12349
07:48:58,638 --> 07:49:00,398
certain point from game height we need
12350
07:49:00,398 --> 07:49:02,878
to subtract the height of our paddle
12351
07:49:02,878 --> 07:49:04,320
game height
12352
07:49:04,320 --> 07:49:05,360
minus
12353
07:49:05,360 --> 07:49:06,558
paddle
12354
07:49:06,558 --> 07:49:07,760
one
12355
07:49:07,760 --> 07:49:10,320
axis the height property we cannot go
12356
07:49:10,320 --> 07:49:12,478
above the game board
12357
07:49:12,478 --> 07:49:17,718
or below okay let's work on paddle two
12358
07:49:19,360 --> 07:49:23,280
case paddle 2 up
12359
07:49:24,878 --> 07:49:27,360
we'll check to see if paddle 2's y
12360
07:49:27,360 --> 07:49:29,360
property
12361
07:49:29,360 --> 07:49:33,040
is greater than zero
12362
07:49:34,080 --> 07:49:36,398
then we will take the y property of
12363
07:49:36,398 --> 07:49:37,760
paddle two
12364
07:49:37,760 --> 07:49:40,160
minus equals the paddle speed
12365
07:49:40,160 --> 07:49:42,320
so we cannot move paddle two above the
12366
07:49:42,320 --> 07:49:45,600
game board then our last case is paddle
12367
07:49:45,600 --> 07:49:47,760
two down
12368
07:49:47,760 --> 07:49:50,320
okay let's copy what we have here
12369
07:49:50,320 --> 07:49:53,280
within paddle one down
12370
07:49:53,280 --> 07:49:56,080
paste it but change paddle one to paddle
12371
07:49:56,080 --> 07:49:57,600
two
12372
07:49:57,600 --> 07:50:00,638
y property of paddle 2 plus equals the
12373
07:50:00,638 --> 07:50:02,718
paddle speed with paddle 2 we shouldn't
12374
07:50:02,718 --> 07:50:04,878
be able to go below or above the game
12375
07:50:04,878 --> 07:50:07,200
board these paddles are complete let's
12376
07:50:07,200 --> 07:50:09,200
close out of the change direction
12377
07:50:09,200 --> 07:50:11,440
function let's work on the draw drawball
12378
07:50:11,440 --> 07:50:12,478
function
12379
07:50:12,478 --> 07:50:14,398
we'll take the context
12380
07:50:14,398 --> 07:50:16,000
set the fill
12381
07:50:16,000 --> 07:50:19,920
style equal to the ball color
12382
07:50:19,920 --> 07:50:23,840
context dot stroke style
12383
07:50:23,840 --> 07:50:26,958
equals ball border
12384
07:50:26,958 --> 07:50:28,398
color
12385
07:50:28,398 --> 07:50:30,398
we can set a line width if we'd like
12386
07:50:30,398 --> 07:50:33,600
context dot line with
12387
07:50:33,600 --> 07:50:35,520
equals to
12388
07:50:35,520 --> 07:50:36,798
context
12389
07:50:36,798 --> 07:50:37,840
begin
12390
07:50:37,840 --> 07:50:39,280
path
12391
07:50:39,280 --> 07:50:40,718
context
12392
07:50:40,718 --> 07:50:43,200
arc to draw a circle
12393
07:50:43,200 --> 07:50:45,040
ball x
12394
07:50:45,040 --> 07:50:46,478
ball y
12395
07:50:46,478 --> 07:50:49,440
then the radius ball radius
12396
07:50:49,440 --> 07:50:52,240
zero this is for radians
12397
07:50:52,240 --> 07:50:56,240
two times math dot pi
12398
07:50:56,240 --> 07:50:59,600
context dot stroke method
12399
07:50:59,600 --> 07:51:02,798
context dot fill method
12400
07:51:02,798 --> 07:51:05,040
and there's our ball okay that is the
12401
07:51:05,040 --> 07:51:07,280
draw ball function let's head to the
12402
07:51:07,280 --> 07:51:09,520
create ball function when we create a
12403
07:51:09,520 --> 07:51:12,000
new ball let's set the ball
12404
07:51:12,000 --> 07:51:15,120
speed equal to one let's begin with the
12405
07:51:15,120 --> 07:51:17,520
x-axis
12406
07:51:17,520 --> 07:51:19,360
if
12407
07:51:19,360 --> 07:51:20,240
math
12408
07:51:20,240 --> 07:51:22,558
dot round
12409
07:51:22,558 --> 07:51:23,520
math
12410
07:51:23,520 --> 07:51:25,520
dot random
12411
07:51:25,520 --> 07:51:27,760
is equal to one
12412
07:51:27,760 --> 07:51:29,600
this part of the condition will give us
12413
07:51:29,600 --> 07:51:32,638
a random number between zero and one
12414
07:51:32,638 --> 07:51:35,120
if that number is one let's move to the
12415
07:51:35,120 --> 07:51:38,000
right if not let's move to the left so
12416
07:51:38,000 --> 07:51:39,680
if we would like to move to the right
12417
07:51:39,680 --> 07:51:41,360
let's set ball
12418
07:51:41,360 --> 07:51:43,440
x direction
12419
07:51:43,440 --> 07:51:46,320
equal to one
12420
07:51:46,478 --> 07:51:47,520
else
12421
07:51:47,520 --> 07:51:49,600
ball x direction
12422
07:51:49,600 --> 07:51:51,840
equals negative one we're moving to the
12423
07:51:51,840 --> 07:51:54,240
left okay let's copy these if else
12424
07:51:54,240 --> 07:51:56,160
statements
12425
07:51:56,160 --> 07:51:59,760
then change ball x to ball y
12426
07:51:59,760 --> 07:52:01,520
then when we create a new ball we'll set
12427
07:52:01,520 --> 07:52:03,600
it to be right in the middle set the
12428
07:52:03,600 --> 07:52:07,840
ball x coordinate equal to game width
12429
07:52:07,840 --> 07:52:09,840
divided by two
12430
07:52:09,840 --> 07:52:13,360
set the ball y coordinate to game
12431
07:52:13,360 --> 07:52:15,680
height divided by two
12432
07:52:15,680 --> 07:52:19,280
then invoke the draw ball function
12433
07:52:19,280 --> 07:52:24,000
pass in as arguments ball x ball y
12434
07:52:24,000 --> 07:52:26,000
that's the create ball function then we
12435
07:52:26,000 --> 07:52:27,680
need to move the ball
12436
07:52:27,680 --> 07:52:31,360
ball x plus equals
12437
07:52:31,360 --> 07:52:33,920
the ball's speed
12438
07:52:33,920 --> 07:52:35,120
times
12439
07:52:35,120 --> 07:52:40,878
the ball's direction ball x direction
12440
07:52:40,878 --> 07:52:43,680
then do the same thing with y
12441
07:52:43,680 --> 07:52:48,320
ball y plus equals ball speed times ball
12442
07:52:48,320 --> 07:52:49,680
y direction
12443
07:52:49,680 --> 07:52:51,200
so our ball should move in a random
12444
07:52:51,200 --> 07:52:52,958
direction
12445
07:52:52,958 --> 07:52:54,638
let's just test that by refreshing the
12446
07:52:54,638 --> 07:52:56,320
page
12447
07:52:56,320 --> 07:52:58,320
so let's close out of the move ball
12448
07:52:58,320 --> 07:53:00,398
function
12449
07:53:00,398 --> 07:53:02,958
now we'll need to check collisions
12450
07:53:02,958 --> 07:53:04,638
let's check to see if we hit the top
12451
07:53:04,638 --> 07:53:07,760
border if so then we'll bounce off of it
12452
07:53:07,760 --> 07:53:09,040
if
12453
07:53:09,040 --> 07:53:10,798
ball y
12454
07:53:10,798 --> 07:53:14,638
is greater than or equal to zero
12455
07:53:14,638 --> 07:53:15,600
plus
12456
07:53:15,600 --> 07:53:18,798
the ball radius remember the center of
12457
07:53:18,798 --> 07:53:20,958
the ball is where we place the x and y
12458
07:53:20,958 --> 07:53:22,958
coordinates so that's why we're adding
12459
07:53:22,958 --> 07:53:25,600
the ball radius to account for that if
12460
07:53:25,600 --> 07:53:28,240
we touch the top border let's change the
12461
07:53:28,240 --> 07:53:29,760
y direction
12462
07:53:29,760 --> 07:53:30,638
ball
12463
07:53:30,638 --> 07:53:31,520
y
12464
07:53:31,520 --> 07:53:33,120
direction
12465
07:53:33,120 --> 07:53:36,160
times equals negative one that will
12466
07:53:36,160 --> 07:53:38,160
reverse the direction
12467
07:53:38,160 --> 07:53:40,160
i'm just going to test that so we should
12468
07:53:40,160 --> 07:53:42,398
bounce off the top yeah let's check to
12469
07:53:42,398 --> 07:53:45,120
see if we hit the bottom border if ball
12470
07:53:45,120 --> 07:53:46,000
y
12471
07:53:46,000 --> 07:53:48,240
is greater than or equal to
12472
07:53:48,240 --> 07:53:49,360
game
12473
07:53:49,360 --> 07:53:50,320
height
12474
07:53:50,320 --> 07:53:53,760
minus ball radius
12475
07:53:53,760 --> 07:53:57,120
then we will change direction
12476
07:53:57,120 --> 07:53:58,958
okay let's check that
12477
07:53:58,958 --> 07:54:01,440
yep we just bounced off the bottom
12478
07:54:01,440 --> 07:54:04,478
what if we touch the left border
12479
07:54:04,478 --> 07:54:05,360
if
12480
07:54:05,360 --> 07:54:10,240
ball x is less than or equal to zero
12481
07:54:10,240 --> 07:54:12,558
let's update player two score
12482
07:54:12,558 --> 07:54:14,558
player two score
12483
07:54:14,558 --> 07:54:16,958
plus equals one
12484
07:54:16,958 --> 07:54:20,080
we'll update the score
12485
07:54:20,398 --> 07:54:23,958
create a new ball
12486
07:54:24,398 --> 07:54:25,920
then return
12487
07:54:25,920 --> 07:54:27,440
once we hit the left border that should
12488
07:54:27,440 --> 07:54:30,000
create a new ball yep there it is okay
12489
07:54:30,000 --> 07:54:33,200
let's copy this if statement
12490
07:54:33,200 --> 07:54:34,798
if ball x
12491
07:54:34,798 --> 07:54:37,520
is greater than or equal to
12492
07:54:37,520 --> 07:54:38,558
game
12493
07:54:38,558 --> 07:54:39,760
width
12494
07:54:39,760 --> 07:54:42,000
then we will update player one score
12495
07:54:42,000 --> 07:54:43,760
when we hit the right border
12496
07:54:43,760 --> 07:54:45,360
that creates a new ball
12497
07:54:45,360 --> 07:54:47,040
okay now this is the tough part we're
12498
07:54:47,040 --> 07:54:48,958
going to bounce off the paddles
12499
07:54:48,958 --> 07:54:52,478
i'm going to maximize the screen
12500
07:54:52,478 --> 07:54:53,760
if
12501
07:54:53,760 --> 07:54:57,920
ball x is less than or equal to this is
12502
07:54:57,920 --> 07:54:59,840
for paddle one
12503
07:54:59,840 --> 07:55:01,600
paddle one
12504
07:55:01,600 --> 07:55:03,760
take the x property
12505
07:55:03,760 --> 07:55:05,360
plus
12506
07:55:05,360 --> 07:55:07,600
paddle one
12507
07:55:07,600 --> 07:55:10,320
axis the width property
12508
07:55:10,320 --> 07:55:13,200
plus the ball radius
12509
07:55:13,200 --> 07:55:16,080
on the x-axis if so let's check the
12510
07:55:16,080 --> 07:55:17,920
y-axis
12511
07:55:17,920 --> 07:55:18,958
if
12512
07:55:18,958 --> 07:55:20,398
ball y
12513
07:55:20,398 --> 07:55:22,320
is greater than
12514
07:55:22,320 --> 07:55:24,080
paddle one
12515
07:55:24,080 --> 07:55:26,798
take the y property
12516
07:55:26,798 --> 07:55:27,840
and
12517
07:55:27,840 --> 07:55:28,798
ball
12518
07:55:28,798 --> 07:55:29,680
y
12519
07:55:29,680 --> 07:55:31,360
is less than
12520
07:55:31,360 --> 07:55:32,718
paddle one
12521
07:55:32,718 --> 07:55:34,398
dot y
12522
07:55:34,398 --> 07:55:36,718
plus paddle one
12523
07:55:36,718 --> 07:55:38,160
dot height
12524
07:55:38,160 --> 07:55:40,160
that means there's a collision
12525
07:55:40,160 --> 07:55:42,638
we'll take ball x
12526
07:55:42,638 --> 07:55:44,398
direction
12527
07:55:44,398 --> 07:55:46,878
multiply this by negative one
12528
07:55:46,878 --> 07:55:48,478
hey if you want for fun you can increase
12529
07:55:48,478 --> 07:55:51,440
the ball speed ball speed
12530
07:55:51,440 --> 07:55:54,798
plus equals one
12531
07:55:55,520 --> 07:55:57,120
okay we should be able to bounce off
12532
07:55:57,120 --> 07:55:59,520
this paddle yeah there we go okay let's
12533
07:55:59,520 --> 07:56:02,000
do the same thing with the right paddle
12534
07:56:02,000 --> 07:56:04,160
so i'm going to copy this section
12535
07:56:04,160 --> 07:56:05,520
paste it
12536
07:56:05,520 --> 07:56:08,080
is greater than or equal to
12537
07:56:08,080 --> 07:56:11,760
the x property of paddle 2
12538
07:56:11,840 --> 07:56:14,398
minus the ball radius
12539
07:56:14,398 --> 07:56:18,000
if ball y is greater than the y property
12540
07:56:18,000 --> 07:56:19,920
of paddle two
12541
07:56:19,920 --> 07:56:20,798
and
12542
07:56:20,798 --> 07:56:24,320
ball y is greater than the y property of
12543
07:56:24,320 --> 07:56:25,920
paddle two
12544
07:56:25,920 --> 07:56:29,680
plus paddle two's height
12545
07:56:29,760 --> 07:56:31,520
so we should be able to bounce off these
12546
07:56:31,520 --> 07:56:34,160
paddles now
12547
07:56:34,638 --> 07:56:37,680
then the ball speeds up
12548
07:56:39,520 --> 07:56:41,120
so we do have one situation where the
12549
07:56:41,120 --> 07:56:43,600
ball might get stuck
12550
07:56:43,600 --> 07:56:46,638
kind of like this
12551
07:56:46,638 --> 07:56:48,080
if that happens i'm just going to push
12552
07:56:48,080 --> 07:56:49,600
the ball out of the way
12553
07:56:49,600 --> 07:56:51,760
and move it forward
12554
07:56:51,760 --> 07:56:55,680
ball x equals
12555
07:56:56,080 --> 07:56:58,878
paddle one
12556
07:56:58,878 --> 07:57:01,680
take the x property
12557
07:57:01,680 --> 07:57:02,878
plus
12558
07:57:02,878 --> 07:57:04,160
paddle
12559
07:57:04,160 --> 07:57:06,878
one dot width plus
12560
07:57:06,878 --> 07:57:08,958
ball radius
12561
07:57:08,958 --> 07:57:12,638
this is if ball gets stuck
12562
07:57:12,638 --> 07:57:14,398
this line of code helps prevent the ball
12563
07:57:14,398 --> 07:57:16,478
from getting stuck within the paddle
12564
07:57:16,478 --> 07:57:19,040
this statement will be ball x equals
12565
07:57:19,040 --> 07:57:20,958
paddle two dot x
12566
07:57:20,958 --> 07:57:23,280
minus the ball radius
12567
07:57:23,280 --> 07:57:26,558
okay now let's update the score
12568
07:57:26,558 --> 07:57:28,160
update score
12569
07:57:28,160 --> 07:57:29,600
this one's easy
12570
07:57:29,600 --> 07:57:32,160
score text
12571
07:57:32,160 --> 07:57:35,120
dot text content
12572
07:57:35,120 --> 07:57:39,040
equals i'll use the template literal
12573
07:57:39,360 --> 07:57:40,478
player
12574
07:57:40,478 --> 07:57:42,478
one score
12575
07:57:42,478 --> 07:57:44,958
colon
12576
07:57:45,520 --> 07:57:46,878
player
12577
07:57:46,878 --> 07:57:50,000
two score
12578
07:57:50,160 --> 07:57:51,760
so we should be able to keep track of
12579
07:57:51,760 --> 07:57:54,638
the score
12580
07:57:54,638 --> 07:57:56,638
okay that's one
12581
07:57:56,638 --> 07:57:59,280
and one
12582
07:57:59,600 --> 07:58:03,200
lastly we have the reset button
12583
07:58:03,440 --> 07:58:06,320
reset game
12584
07:58:07,040 --> 07:58:08,398
player one
12585
07:58:08,398 --> 07:58:11,920
score equals zero
12586
07:58:11,920 --> 07:58:15,040
player two score equals zero
12587
07:58:15,040 --> 07:58:16,878
let's reset these paddles i'll just copy
12588
07:58:16,878 --> 07:58:18,320
them
12589
07:58:18,320 --> 07:58:20,840
paste them get rid of the let
12590
07:58:20,840 --> 07:58:23,200
keyword by setting the x and the y
12591
07:58:23,200 --> 07:58:24,558
coordinates that will put them in their
12592
07:58:24,558 --> 07:58:26,398
original positions
12593
07:58:26,398 --> 07:58:28,478
to demonstrate i'll press reset and they
12594
07:58:28,478 --> 07:58:31,200
go back to their respective corners
12595
07:58:31,200 --> 07:58:34,478
ball x equals zero
12596
07:58:34,478 --> 07:58:37,760
ball y equals zero
12597
07:58:37,760 --> 07:58:39,360
ball x
12598
07:58:39,360 --> 07:58:40,558
direction
12599
07:58:40,558 --> 07:58:42,718
equals zero
12600
07:58:42,718 --> 07:58:45,120
ball y direction
12601
07:58:45,120 --> 07:58:47,040
equals zero
12602
07:58:47,040 --> 07:58:51,000
will update the score
12603
07:58:51,120 --> 07:58:54,080
we'll invoke the clear
12604
07:58:54,080 --> 07:58:58,160
interval method pass in our interval id
12605
07:58:58,160 --> 07:59:01,120
then invoke game start again
12606
07:59:01,120 --> 07:59:02,798
okay we should be able to restart this
12607
07:59:02,798 --> 07:59:04,798
game
12608
07:59:04,798 --> 07:59:06,798
yeah there we go well okay then
12609
07:59:06,798 --> 07:59:09,200
everybody that is a game of pong using
12610
07:59:09,200 --> 07:59:11,280
javascript if you would like a copy of
12611
07:59:11,280 --> 07:59:13,040
this code i'll post this in the comment
12612
07:59:13,040 --> 07:59:15,040
section down below be sure to check the
12613
07:59:15,040 --> 07:59:17,280
original playlist and well yeah that's a
12614
07:59:17,280 --> 07:59:21,398
game of pong using javascript
12615
07:59:59,760 --> 08:00:01,840
you
811986
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.