All language subtitles for [English (auto-generated)] BEST JavaScript Tutorial for Beginners for Getting a Job 2021 (High Quality, Project Based Course) [DownSub.com]
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:03,199
so what we just built here is called mvc
2
00:00:03,199 --> 00:00:04,960
and i'm telling you if you master this
3
00:00:04,960 --> 00:00:06,399
coding pattern you will never have
4
00:00:06,399 --> 00:00:09,040
trouble getting a job as a web developer
5
00:00:09,040 --> 00:00:11,280
what's up guys welcome to my javascript
6
00:00:11,280 --> 00:00:14,160
tutorial for absolute beginners now this
7
00:00:14,160 --> 00:00:16,160
tutorial is specifically designed for
8
00:00:16,160 --> 00:00:18,320
new programmers to help you get a job in
9
00:00:18,320 --> 00:00:19,920
software engineering
10
00:00:19,920 --> 00:00:21,760
and unlike other tutorials where your
11
00:00:21,760 --> 00:00:24,160
code looks like this after an hour we're
12
00:00:24,160 --> 00:00:26,160
going to be building a fully functional
13
00:00:26,160 --> 00:00:27,680
app and we're going to go through not
14
00:00:27,680 --> 00:00:29,840
only the fundamentals of javascript but
15
00:00:29,840 --> 00:00:32,640
also the advanced concepts like closures
16
00:00:32,640 --> 00:00:34,880
the document object model and most
17
00:00:34,880 --> 00:00:38,079
importantly the mvc architecture and
18
00:00:38,079 --> 00:00:39,520
this is something that people rarely
19
00:00:39,520 --> 00:00:41,280
teach but it's essential if you want to
20
00:00:41,280 --> 00:00:44,239
succeed as a software engineer now this
21
00:00:44,239 --> 00:00:46,399
tutorial is packed with information so
22
00:00:46,399 --> 00:00:48,559
you'll want to go through it section by
23
00:00:48,559 --> 00:00:50,719
section and pause whenever you need to
24
00:00:50,719 --> 00:00:52,640
review or catch up
25
00:00:52,640 --> 00:00:54,480
in the description you'll find the
26
00:00:54,480 --> 00:00:56,559
different sections of this tutorial and
27
00:00:56,559 --> 00:00:58,879
you can also see it in the progress bar
28
00:00:58,879 --> 00:01:00,800
below the video here
29
00:01:00,800 --> 00:01:02,399
and throughout the tutorial i put
30
00:01:02,399 --> 00:01:05,040
together a series of exercises to help
31
00:01:05,040 --> 00:01:08,000
you practice the concepts that we learn
32
00:01:08,000 --> 00:01:10,080
and the answers for these exercises as
33
00:01:10,080 --> 00:01:12,479
well as the code in progress can also be
34
00:01:12,479 --> 00:01:14,479
found in the description
35
00:01:14,479 --> 00:01:16,880
and with that let's get started with the
36
00:01:16,880 --> 00:01:19,600
tutorial
37
00:01:20,960 --> 00:01:22,960
since this is a tutorial for absolute
38
00:01:22,960 --> 00:01:24,560
beginners i'm going to start from the
39
00:01:24,560 --> 00:01:26,560
ground up and i'm going to explain every
40
00:01:26,560 --> 00:01:28,080
step that i do
41
00:01:28,080 --> 00:01:29,759
now the first question we have to answer
42
00:01:29,759 --> 00:01:32,799
is what is programming so programming is
43
00:01:32,799 --> 00:01:34,560
basically giving instructions to a
44
00:01:34,560 --> 00:01:36,560
computer and the computer follows our
45
00:01:36,560 --> 00:01:38,640
instructions now i found the best way to
46
00:01:38,640 --> 00:01:40,799
demonstrate this is to try it ourselves
47
00:01:40,799 --> 00:01:42,000
and to give the computer some
48
00:01:42,000 --> 00:01:44,159
instructions to follow
49
00:01:44,159 --> 00:01:46,079
so the first step that we have to do is
50
00:01:46,079 --> 00:01:48,079
to download the google chrome web
51
00:01:48,079 --> 00:01:50,000
browser now if you already have chrome
52
00:01:50,000 --> 00:01:52,000
installed you can skip to this time in
53
00:01:52,000 --> 00:01:54,000
the video but if you don't you can
54
00:01:54,000 --> 00:01:56,320
download chrome by just searching for
55
00:01:56,320 --> 00:01:58,159
google chrome
56
00:01:58,159 --> 00:02:00,000
clicking that download link
57
00:02:00,000 --> 00:02:02,560
and installing the browser so i'm using
58
00:02:02,560 --> 00:02:04,159
mac here but it doesn't matter if you're
59
00:02:04,159 --> 00:02:06,320
on mac or windows all the steps in this
60
00:02:06,320 --> 00:02:08,720
course are the same for both
61
00:02:08,720 --> 00:02:10,639
now i recommend chrome because most
62
00:02:10,639 --> 00:02:12,160
professional web development is done
63
00:02:12,160 --> 00:02:14,400
using chrome so if you want to get a job
64
00:02:14,400 --> 00:02:16,400
it's beneficial to learn how to develop
65
00:02:16,400 --> 00:02:18,080
in chrome
66
00:02:18,080 --> 00:02:20,400
okay so once you've installed chrome
67
00:02:20,400 --> 00:02:22,720
we're going to open google chrome
68
00:02:22,720 --> 00:02:25,440
and we're going to open a new tab
69
00:02:25,440 --> 00:02:27,440
and now we're going to right click in
70
00:02:27,440 --> 00:02:29,760
one of this white area here
71
00:02:29,760 --> 00:02:32,720
and we're going to click inspect
72
00:02:32,720 --> 00:02:34,400
so when the inspect window opens we're
73
00:02:34,400 --> 00:02:36,480
going to click the console tab
74
00:02:36,480 --> 00:02:37,920
and here's where we can give
75
00:02:37,920 --> 00:02:40,400
instructions for the computer to follow
76
00:02:40,400 --> 00:02:41,680
so the first instruction that we're
77
00:02:41,680 --> 00:02:42,640
going to learn
78
00:02:42,640 --> 00:02:43,360
is
79
00:02:43,360 --> 00:02:45,680
alert
80
00:02:45,680 --> 00:02:47,599
hello
81
00:02:47,599 --> 00:02:48,959
now you have to write the instruction
82
00:02:48,959 --> 00:02:51,200
out exactly as i have it here because in
83
00:02:51,200 --> 00:02:52,959
programming if you don't write it out
84
00:02:52,959 --> 00:02:54,879
exactly the computer will give you an
85
00:02:54,879 --> 00:02:56,959
error so once we have our instruction
86
00:02:56,959 --> 00:02:58,159
we're going to tell the computer to
87
00:02:58,159 --> 00:03:00,239
follow an instruction otherwise known as
88
00:03:00,239 --> 00:03:02,840
running our code by pressing
89
00:03:02,840 --> 00:03:04,560
enter
90
00:03:04,560 --> 00:03:05,840
so you can see that the computer
91
00:03:05,840 --> 00:03:07,680
followed our instructions it created a
92
00:03:07,680 --> 00:03:11,040
pop-up with a text hello inside
93
00:03:11,040 --> 00:03:12,400
now we're going to learn our second
94
00:03:12,400 --> 00:03:14,000
instruction which is
95
00:03:14,000 --> 00:03:16,840
console.log
96
00:03:16,840 --> 00:03:20,640
hi and what this does is it displays the
97
00:03:20,640 --> 00:03:23,760
message inside the quotes which is high
98
00:03:23,760 --> 00:03:26,159
inside the console and when we press
99
00:03:26,159 --> 00:03:28,400
enter we'll see that the computer did
100
00:03:28,400 --> 00:03:30,640
exactly what we told it to do
101
00:03:30,640 --> 00:03:31,920
now the third instruction i want to
102
00:03:31,920 --> 00:03:33,280
introduce right now
103
00:03:33,280 --> 00:03:34,080
is
104
00:03:34,080 --> 00:03:35,360
it's a little bit complicated so you
105
00:03:35,360 --> 00:03:37,280
have to write it out exactly
106
00:03:37,280 --> 00:03:40,959
document dot body dot inner
107
00:03:40,959 --> 00:03:42,480
html
108
00:03:42,480 --> 00:03:44,640
equals hello
109
00:03:44,640 --> 00:03:46,560
and you have to be careful of the case
110
00:03:46,560 --> 00:03:48,640
sensitivity here so the lowercase and
111
00:03:48,640 --> 00:03:51,120
uppercase has to match exactly
112
00:03:51,120 --> 00:03:53,200
now what this instruction does is it
113
00:03:53,200 --> 00:03:55,200
tells the computer to overwrite this
114
00:03:55,200 --> 00:03:57,280
entire page and just replace it with
115
00:03:57,280 --> 00:04:00,159
whatever's in this quote which is hello
116
00:04:00,159 --> 00:04:01,680
now when we run this instruction we're
117
00:04:01,680 --> 00:04:03,439
gonna see the computer did exactly what
118
00:04:03,439 --> 00:04:05,360
we told it to do
119
00:04:05,360 --> 00:04:07,280
all right so we just learned three
120
00:04:07,280 --> 00:04:08,799
different instructions that we can give
121
00:04:08,799 --> 00:04:10,799
to the computer and the computer does
122
00:04:10,799 --> 00:04:12,879
whatever we tell it to do and that's
123
00:04:12,879 --> 00:04:14,319
what programming is that's what
124
00:04:14,319 --> 00:04:16,959
programmers like me do at work every day
125
00:04:16,959 --> 00:04:18,720
in this tutorial we're going to be using
126
00:04:18,720 --> 00:04:20,560
around 30 maybe 40 different
127
00:04:20,560 --> 00:04:22,800
instructions in javascript and don't
128
00:04:22,800 --> 00:04:24,560
worry about memorizing all of these
129
00:04:24,560 --> 00:04:26,400
because i prepared a cheat sheet that
130
00:04:26,400 --> 00:04:28,800
explains all the code that we use and
131
00:04:28,800 --> 00:04:30,320
you can find this cheat sheet in the
132
00:04:30,320 --> 00:04:33,199
link in the description here so the code
133
00:04:33,199 --> 00:04:34,880
that we wrote here is from a programming
134
00:04:34,880 --> 00:04:36,880
language called javascript and
135
00:04:36,880 --> 00:04:38,800
javascript offers many different
136
00:04:38,800 --> 00:04:40,320
instructions that we can give to the
137
00:04:40,320 --> 00:04:42,720
computer and as programmers we take
138
00:04:42,720 --> 00:04:44,320
these instructions and then we combine
139
00:04:44,320 --> 00:04:46,160
them to create even more complex
140
00:04:46,160 --> 00:04:48,320
instructions so eventually we can create
141
00:04:48,320 --> 00:04:52,080
something like order pizza
142
00:04:52,080 --> 00:04:54,400
which will order pay and get someone to
143
00:04:54,400 --> 00:04:56,800
deliver pizza to where you live right
144
00:04:56,800 --> 00:04:58,400
now we haven't created this instruction
145
00:04:58,400 --> 00:05:00,000
so the computer is going to give us an
146
00:05:00,000 --> 00:05:02,080
error
147
00:05:02,080 --> 00:05:03,520
so now that we've learned a little bit
148
00:05:03,520 --> 00:05:06,000
of javascript code and we ran that code
149
00:05:06,000 --> 00:05:07,199
we're going to learn more about the
150
00:05:07,199 --> 00:05:09,360
javascript programming language so
151
00:05:09,360 --> 00:05:11,120
what's the difference between javascript
152
00:05:11,120 --> 00:05:13,199
and other programming languages well one
153
00:05:13,199 --> 00:05:14,960
thing is that javascript is the only
154
00:05:14,960 --> 00:05:17,120
language that can run in a browser so
155
00:05:17,120 --> 00:05:19,199
that's why we're learning it right now
156
00:05:19,199 --> 00:05:20,960
and one big difference between different
157
00:05:20,960 --> 00:05:22,320
programming languages is something
158
00:05:22,320 --> 00:05:25,360
called syntax and syntax is like grammar
159
00:05:25,360 --> 00:05:28,320
in english or french etc it's the rules
160
00:05:28,320 --> 00:05:30,479
for how each language is written for
161
00:05:30,479 --> 00:05:32,400
example in english sentences begin with
162
00:05:32,400 --> 00:05:34,560
a capital and end with a period
163
00:05:34,560 --> 00:05:36,880
but unlike in english you can misspell
164
00:05:36,880 --> 00:05:38,479
things and people will still understand
165
00:05:38,479 --> 00:05:39,759
what you mean
166
00:05:39,759 --> 00:05:41,520
in programming however you have to
167
00:05:41,520 --> 00:05:44,000
follow the rules of syntax exactly
168
00:05:44,000 --> 00:05:45,280
otherwise you're just going to get
169
00:05:45,280 --> 00:05:46,320
errors
170
00:05:46,320 --> 00:05:48,080
to demonstrate this i'm going to write
171
00:05:48,080 --> 00:05:50,720
the alert code that we had earlier
172
00:05:50,720 --> 00:05:52,639
but this time i'm going to delete the
173
00:05:52,639 --> 00:05:55,039
second quote and when i press enter to
174
00:05:55,039 --> 00:05:56,639
run it you're going to see that we have
175
00:05:56,639 --> 00:05:58,720
a syntax error so that's really
176
00:05:58,720 --> 00:06:00,080
important in programming you have to
177
00:06:00,080 --> 00:06:02,720
follow the syntax rules exactly
178
00:06:02,720 --> 00:06:04,080
one thing you'll notice is that our
179
00:06:04,080 --> 00:06:06,479
instructions end with a semicolon
180
00:06:06,479 --> 00:06:08,880
in javascript a semicolon marks the end
181
00:06:08,880 --> 00:06:10,800
of an instruction it's sort of like a
182
00:06:10,800 --> 00:06:13,360
period in english where a period marks
183
00:06:13,360 --> 00:06:15,520
the end of a sentence
184
00:06:15,520 --> 00:06:17,520
now according to the syntax rules of
185
00:06:17,520 --> 00:06:20,160
javascript a semicolon is actually
186
00:06:20,160 --> 00:06:22,479
optional it's only if you have two
187
00:06:22,479 --> 00:06:24,800
different instructions on one line then
188
00:06:24,800 --> 00:06:26,240
you need to separate them with a
189
00:06:26,240 --> 00:06:28,080
semicolon so that the computer
190
00:06:28,080 --> 00:06:29,600
recognizes that there are two
191
00:06:29,600 --> 00:06:31,520
instructions in this line
192
00:06:31,520 --> 00:06:32,960
throughout this tutorial i'm going to
193
00:06:32,960 --> 00:06:35,120
use semicolons in my code because i
194
00:06:35,120 --> 00:06:36,639
noticed that a lot of companies still
195
00:06:36,639 --> 00:06:38,720
prefer to use semicolons even though
196
00:06:38,720 --> 00:06:40,160
they're optional
197
00:06:40,160 --> 00:06:41,840
so for now i'm going to give you some
198
00:06:41,840 --> 00:06:43,919
exercises that you can try so pause the
199
00:06:43,919 --> 00:06:46,080
video the answers are in the link in the
200
00:06:46,080 --> 00:06:48,560
description and see if you can use what
201
00:06:48,560 --> 00:06:53,360
we just learned to solve these exercises
202
00:06:54,080 --> 00:06:55,440
so now we're going to work on our first
203
00:06:55,440 --> 00:06:57,680
project project based learning is one of
204
00:06:57,680 --> 00:06:59,599
the best ways to learn programming
205
00:06:59,599 --> 00:07:01,120
so the project we're going to do is
206
00:07:01,120 --> 00:07:02,800
we're going to create our own version of
207
00:07:02,800 --> 00:07:05,520
the google tasks application so google
208
00:07:05,520 --> 00:07:08,160
task is basically a to-do list and one
209
00:07:08,160 --> 00:07:09,919
of the things we need to do is to
210
00:07:09,919 --> 00:07:12,479
display a list so how do we display
211
00:07:12,479 --> 00:07:14,479
things on a web page we're going to
212
00:07:14,479 --> 00:07:17,120
learn that code right now
213
00:07:17,120 --> 00:07:18,639
normally when we're programming we don't
214
00:07:18,639 --> 00:07:20,319
just write all the code in the console
215
00:07:20,319 --> 00:07:22,960
here we write our code in files so the
216
00:07:22,960 --> 00:07:24,240
first thing we're going to do is we're
217
00:07:24,240 --> 00:07:26,319
going to download a text editor to help
218
00:07:26,319 --> 00:07:27,919
us write our code
219
00:07:27,919 --> 00:07:29,680
the text editor we're going to download
220
00:07:29,680 --> 00:07:30,800
is called
221
00:07:30,800 --> 00:07:33,039
vs code
222
00:07:33,039 --> 00:07:34,960
now you can use any text editor that you
223
00:07:34,960 --> 00:07:37,599
want but right now vs code is the most
224
00:07:37,599 --> 00:07:39,680
popular text editor for professional
225
00:07:39,680 --> 00:07:41,120
programmers so that's what i'm going to
226
00:07:41,120 --> 00:07:42,639
teach you
227
00:07:42,639 --> 00:07:44,560
so once you've installed vs code
228
00:07:44,560 --> 00:07:46,720
we're going to want to create a new
229
00:07:46,720 --> 00:07:48,160
folder
230
00:07:48,160 --> 00:07:49,440
you can put it anywhere but i'm going to
231
00:07:49,440 --> 00:07:51,360
put it on my desktop and i'm going to
232
00:07:51,360 --> 00:07:53,280
call the folder code so this folder is
233
00:07:53,280 --> 00:07:54,960
where we're going to store all the code
234
00:07:54,960 --> 00:07:56,639
for our project
235
00:07:56,639 --> 00:08:00,479
and now we're going to open vs code
236
00:08:00,479 --> 00:08:02,639
and we're just going to open the new
237
00:08:02,639 --> 00:08:06,720
folder that we created in vs code
238
00:08:06,960 --> 00:08:08,639
so now we're going to create our first
239
00:08:08,639 --> 00:08:10,879
file containing our code so we're going
240
00:08:10,879 --> 00:08:13,360
to create this new file icon
241
00:08:13,360 --> 00:08:15,199
and we're going to call this file to
242
00:08:15,199 --> 00:08:18,000
do.html
243
00:08:18,000 --> 00:08:19,520
so it's important that you end the file
244
00:08:19,520 --> 00:08:21,919
name with dot html because that's going
245
00:08:21,919 --> 00:08:23,360
to tell the computer that this file
246
00:08:23,360 --> 00:08:25,360
contains a web page rather than just
247
00:08:25,360 --> 00:08:26,479
text
248
00:08:26,479 --> 00:08:28,800
so what does html mean
249
00:08:28,800 --> 00:08:31,440
html stands for hypertext markup
250
00:08:31,440 --> 00:08:33,519
language it's another language that we
251
00:08:33,519 --> 00:08:35,200
can use to give the computer
252
00:08:35,200 --> 00:08:37,760
instructions just like javascript so
253
00:08:37,760 --> 00:08:38,958
we're going to learn our first
254
00:08:38,958 --> 00:08:41,279
instructions in html
255
00:08:41,279 --> 00:08:42,399
so the first instruction we're going to
256
00:08:42,399 --> 00:08:44,159
learn is called
257
00:08:44,159 --> 00:08:46,560
button
258
00:08:48,000 --> 00:08:49,680
so this is an instruction that tells a
259
00:08:49,680 --> 00:08:51,680
computer to create a button with the
260
00:08:51,680 --> 00:08:54,399
text press me inside the button
261
00:08:54,399 --> 00:08:56,800
now you can run these instructions by
262
00:08:56,800 --> 00:08:58,399
opening the folder that contains your
263
00:08:58,399 --> 00:08:59,440
code
264
00:08:59,440 --> 00:09:01,760
and you want to open your code with
265
00:09:01,760 --> 00:09:03,920
google chrome
266
00:09:03,920 --> 00:09:06,160
and as you can see it creates a web page
267
00:09:06,160 --> 00:09:08,240
and it creates a button based on our
268
00:09:08,240 --> 00:09:09,839
instructions
269
00:09:09,839 --> 00:09:12,560
html is used to create visual elements
270
00:09:12,560 --> 00:09:14,959
on a web page whether that's a button a
271
00:09:14,959 --> 00:09:17,680
text or an image and we call this
272
00:09:17,680 --> 00:09:20,880
process rendering so html is used to
273
00:09:20,880 --> 00:09:23,440
render visuals while javascript is used
274
00:09:23,440 --> 00:09:25,600
to manage information they both work
275
00:09:25,600 --> 00:09:26,959
together
276
00:09:26,959 --> 00:09:28,320
now we're going to teach the syntax of
277
00:09:28,320 --> 00:09:30,720
html right now these things are called
278
00:09:30,720 --> 00:09:33,040
tags they describe what kind of visual
279
00:09:33,040 --> 00:09:34,880
element this is
280
00:09:34,880 --> 00:09:36,720
so in this example we are creating a
281
00:09:36,720 --> 00:09:38,720
button and this tag represents the start
282
00:09:38,720 --> 00:09:40,880
of a button and we have a slash here it
283
00:09:40,880 --> 00:09:42,959
means it's the end of the button and
284
00:09:42,959 --> 00:09:45,360
whatever is in between is the contents
285
00:09:45,360 --> 00:09:47,839
of the button otherwise the label that's
286
00:09:47,839 --> 00:09:50,560
in the button which you can see here
287
00:09:50,560 --> 00:09:52,720
and similar to javascript the syntax of
288
00:09:52,720 --> 00:09:55,200
html has to be just right otherwise it
289
00:09:55,200 --> 00:09:56,720
won't work
290
00:09:56,720 --> 00:09:58,080
now we're going to learn our second
291
00:09:58,080 --> 00:10:00,080
instruction in html
292
00:10:00,080 --> 00:10:03,519
and this is the script tag
293
00:10:03,519 --> 00:10:05,600
so what this code does is it tells the
294
00:10:05,600 --> 00:10:08,000
browser to load the javascript that's
295
00:10:08,000 --> 00:10:09,920
contained in the script
296
00:10:09,920 --> 00:10:11,279
so we're going to write the javascript
297
00:10:11,279 --> 00:10:13,680
that we had earlier
298
00:10:13,680 --> 00:10:15,440
and we can reload the file by just
299
00:10:15,440 --> 00:10:16,959
refreshing the page
300
00:10:16,959 --> 00:10:18,800
and you can see that the browser
301
00:10:18,800 --> 00:10:20,480
followed our instructions and it ran
302
00:10:20,480 --> 00:10:22,560
this code
303
00:10:22,560 --> 00:10:24,399
and it created a pop-up with the text
304
00:10:24,399 --> 00:10:26,720
hello inside of it now the thing to note
305
00:10:26,720 --> 00:10:28,800
here is that we can't load javascript
306
00:10:28,800 --> 00:10:31,040
directly in a web page javascript has to
307
00:10:31,040 --> 00:10:33,440
be loaded within html
308
00:10:33,440 --> 00:10:34,880
so we learned our first two instructions
309
00:10:34,880 --> 00:10:37,200
in html and we'll be switching back and
310
00:10:37,200 --> 00:10:38,880
forth between these two languages
311
00:10:38,880 --> 00:10:41,040
because they work together to create web
312
00:10:41,040 --> 00:10:42,000
pages
313
00:10:42,000 --> 00:10:44,160
now some more exercises for you to try
314
00:10:44,160 --> 00:10:45,680
to help you understand what we just
315
00:10:45,680 --> 00:10:47,839
learned
316
00:10:48,800 --> 00:10:51,120
let's start building out our to-do list
317
00:10:51,120 --> 00:10:53,200
so what do we need to create a to-do
318
00:10:53,200 --> 00:10:55,519
list well we need to save a list of
319
00:10:55,519 --> 00:10:57,440
items that we have to do
320
00:10:57,440 --> 00:11:00,079
so this list can be get groceries wash
321
00:11:00,079 --> 00:11:02,240
the car and make dinner
322
00:11:02,240 --> 00:11:03,360
so we're going to learn a feature in
323
00:11:03,360 --> 00:11:05,920
javascript called variables and this
324
00:11:05,920 --> 00:11:07,279
feature is going to help us save our
325
00:11:07,279 --> 00:11:08,399
list
326
00:11:08,399 --> 00:11:10,000
so we're going to create a variable by
327
00:11:10,000 --> 00:11:11,680
using this code
328
00:11:11,680 --> 00:11:13,360
let to do
329
00:11:13,360 --> 00:11:17,440
one equals get groceries
330
00:11:17,440 --> 00:11:19,120
let to do two
331
00:11:19,120 --> 00:11:20,959
equals
332
00:11:20,959 --> 00:11:23,519
wash car
333
00:11:23,680 --> 00:11:25,680
and i'm going to create the third to-do
334
00:11:25,680 --> 00:11:26,720
list
335
00:11:26,720 --> 00:11:27,600
i
336
00:11:27,600 --> 00:11:29,760
and we're going to delete this alert
337
00:11:29,760 --> 00:11:31,360
code
338
00:11:31,360 --> 00:11:33,440
so variables are like containers you can
339
00:11:33,440 --> 00:11:35,920
save a value inside a variable
340
00:11:35,920 --> 00:11:38,320
this is a syntax for a variable
341
00:11:38,320 --> 00:11:41,440
let means to create a variable to do one
342
00:11:41,440 --> 00:11:43,680
is the name of the variable or the name
343
00:11:43,680 --> 00:11:45,120
of the container
344
00:11:45,120 --> 00:11:47,519
and this part means to save the value
345
00:11:47,519 --> 00:11:50,560
get groceries inside this variable this
346
00:11:50,560 --> 00:11:53,040
is also called assigning a value to a
347
00:11:53,040 --> 00:11:54,959
variable so when we're creating
348
00:11:54,959 --> 00:11:56,639
variables we generally don't want to use
349
00:11:56,639 --> 00:11:59,440
names like a or b because we want to
350
00:11:59,440 --> 00:12:01,279
make our variable names easy for someone
351
00:12:01,279 --> 00:12:04,079
reading the code to understand
352
00:12:04,079 --> 00:12:05,839
now we're going to run this code by
353
00:12:05,839 --> 00:12:07,600
going into the browser we're just going
354
00:12:07,600 --> 00:12:09,680
to refresh the page and we're going to
355
00:12:09,680 --> 00:12:11,120
right click
356
00:12:11,120 --> 00:12:12,560
inspect
357
00:12:12,560 --> 00:12:14,560
and open the console
358
00:12:14,560 --> 00:12:15,800
and now we can
359
00:12:15,800 --> 00:12:17,440
console.log
360
00:12:17,440 --> 00:12:20,079
to do one
361
00:12:20,079 --> 00:12:21,839
so you can see that to do one the
362
00:12:21,839 --> 00:12:24,079
variable was defined and when we console
363
00:12:24,079 --> 00:12:26,560
log the variable it actually logs the
364
00:12:26,560 --> 00:12:28,800
value inside the variable
365
00:12:28,800 --> 00:12:32,000
now i want you to try console.log to do
366
00:12:32,000 --> 00:12:33,120
one
367
00:12:33,120 --> 00:12:36,000
but with quotes around it
368
00:12:36,000 --> 00:12:37,040
and you'll notice that instead of
369
00:12:37,040 --> 00:12:40,079
logging the value it just logs to do one
370
00:12:40,079 --> 00:12:41,600
as a text
371
00:12:41,600 --> 00:12:44,000
so why does this happen to do one is a
372
00:12:44,000 --> 00:12:46,959
variable it's a container so it will log
373
00:12:46,959 --> 00:12:49,360
the value inside the variable
374
00:12:49,360 --> 00:12:51,519
but to do one with quotes around it it's
375
00:12:51,519 --> 00:12:53,519
not a variable anymore it's an actual
376
00:12:53,519 --> 00:12:56,320
value so it would just log this value
377
00:12:56,320 --> 00:12:57,920
now this type of value with quotes
378
00:12:57,920 --> 00:13:00,160
around it is called a string which in
379
00:13:00,160 --> 00:13:02,079
programming is just another name for a
380
00:13:02,079 --> 00:13:04,240
piece of text now you can verify that
381
00:13:04,240 --> 00:13:06,800
this is a string by giving this code
382
00:13:06,800 --> 00:13:08,240
type of
383
00:13:08,240 --> 00:13:11,040
to do one
384
00:13:11,279 --> 00:13:12,880
and the computer will tell you that this
385
00:13:12,880 --> 00:13:14,560
is a string
386
00:13:14,560 --> 00:13:16,800
another type of value in javascript is a
387
00:13:16,800 --> 00:13:19,839
number so if you do type of 99 it will
388
00:13:19,839 --> 00:13:22,000
tell you that this is a number but right
389
00:13:22,000 --> 00:13:23,920
now we're just working with strings
390
00:13:23,920 --> 00:13:25,279
one thing i want to show you is if you
391
00:13:25,279 --> 00:13:27,920
type type of to-do one notice that it
392
00:13:27,920 --> 00:13:30,320
doesn't say variable typo actually gives
393
00:13:30,320 --> 00:13:32,320
you the type of the value that's stored
394
00:13:32,320 --> 00:13:34,560
inside the variable which is in our case
395
00:13:34,560 --> 00:13:37,279
this string get groceries
396
00:13:37,279 --> 00:13:38,399
another thing that you can do with
397
00:13:38,399 --> 00:13:40,320
strings is you can combine them together
398
00:13:40,320 --> 00:13:42,480
using the plus sign so i can say
399
00:13:42,480 --> 00:13:45,120
console.log
400
00:13:45,120 --> 00:13:46,639
hello
401
00:13:46,639 --> 00:13:50,560
and plus my name simon
402
00:13:50,560 --> 00:13:54,560
and i will log the combined string
403
00:13:59,519 --> 00:14:01,199
so before we continue i'm going to teach
404
00:14:01,199 --> 00:14:04,079
you some html standards so when we write
405
00:14:04,079 --> 00:14:06,720
html there's usually a very specific
406
00:14:06,720 --> 00:14:08,800
structure that our page has to follow
407
00:14:08,800 --> 00:14:11,199
and the structure looks like this
408
00:14:11,199 --> 00:14:13,920
we're going to create an html tag
409
00:14:13,920 --> 00:14:16,320
and inside the html tag i want to create
410
00:14:16,320 --> 00:14:18,079
a body tag
411
00:14:18,079 --> 00:14:20,320
we have tags inside another tag this is
412
00:14:20,320 --> 00:14:22,160
called nesting
413
00:14:22,160 --> 00:14:23,680
so inside the body tag we're actually
414
00:14:23,680 --> 00:14:26,240
going to put all of our code
415
00:14:26,240 --> 00:14:27,440
and we're going to indent this code to
416
00:14:27,440 --> 00:14:28,720
make it look nicer so if you want to
417
00:14:28,720 --> 00:14:30,320
indent multiple lines you select the
418
00:14:30,320 --> 00:14:33,279
lines and you press tab
419
00:14:33,279 --> 00:14:36,240
so what does this mean so the html tag
420
00:14:36,240 --> 00:14:38,560
says this document contains the html
421
00:14:38,560 --> 00:14:40,560
code here's the start of the html
422
00:14:40,560 --> 00:14:42,880
document and here's the end the body tag
423
00:14:42,880 --> 00:14:45,120
says here is everything that's visual
424
00:14:45,120 --> 00:14:47,360
that you can see on the web page here's
425
00:14:47,360 --> 00:14:48,720
the start of the body here's the end of
426
00:14:48,720 --> 00:14:50,880
the body and all of the visual elements
427
00:14:50,880 --> 00:14:52,240
that are shown in the web page are
428
00:14:52,240 --> 00:14:54,399
inside the body so there's one other
429
00:14:54,399 --> 00:14:56,800
section that appears in every html page
430
00:14:56,800 --> 00:14:59,839
and that's called the head section
431
00:14:59,839 --> 00:15:01,920
and this section has information about
432
00:15:01,920 --> 00:15:03,440
the page but doesn't necessarily have
433
00:15:03,440 --> 00:15:05,519
things that show up on the page
434
00:15:05,519 --> 00:15:07,440
so for example one thing we can put in
435
00:15:07,440 --> 00:15:10,720
the head element is the title
436
00:15:10,720 --> 00:15:13,040
and we're going to call this my to-do
437
00:15:13,040 --> 00:15:15,360
app
438
00:15:16,240 --> 00:15:19,120
now we're going to run this html
439
00:15:19,120 --> 00:15:20,639
and you'll notice that the title shows
440
00:15:20,639 --> 00:15:22,720
up in the tabs but it doesn't
441
00:15:22,720 --> 00:15:24,880
necessarily show up inside the web page
442
00:15:24,880 --> 00:15:26,880
as a visual element
443
00:15:26,880 --> 00:15:28,560
so this is just a standard that we use
444
00:15:28,560 --> 00:15:30,399
for html and we're going to follow it
445
00:15:30,399 --> 00:15:31,600
because this is what professional
446
00:15:31,600 --> 00:15:34,000
programmers use
447
00:15:34,000 --> 00:15:36,639
let's learn our next bit of html code so
448
00:15:36,639 --> 00:15:38,880
we're going to go under this button and
449
00:15:38,880 --> 00:15:40,000
we're going to create what's called a
450
00:15:40,000 --> 00:15:42,160
div element and inside this div we're
451
00:15:42,160 --> 00:15:44,240
going to type out our to-do's so get
452
00:15:44,240 --> 00:15:46,000
groceries
453
00:15:46,000 --> 00:15:48,240
and we're just going to
454
00:15:48,240 --> 00:15:51,040
create a div for each of our to-do list
455
00:15:51,040 --> 00:15:52,160
items
456
00:15:52,160 --> 00:15:54,880
so this guy's gonna be wash car
457
00:15:54,880 --> 00:15:57,120
and make dinner
458
00:15:57,120 --> 00:15:59,360
so what does a div do well we already
459
00:15:59,360 --> 00:16:02,480
know that a button renders a button a
460
00:16:02,480 --> 00:16:04,079
div on the other hand it renders an
461
00:16:04,079 --> 00:16:06,160
invisible box around whatever's inside
462
00:16:06,160 --> 00:16:08,240
that div so here it's going to render
463
00:16:08,240 --> 00:16:10,240
this text and it's going to create a box
464
00:16:10,240 --> 00:16:12,240
around that text and to see that we're
465
00:16:12,240 --> 00:16:14,079
going to go back into chrome and we're
466
00:16:14,079 --> 00:16:15,839
going to refresh the page to run our
467
00:16:15,839 --> 00:16:17,199
code
468
00:16:17,199 --> 00:16:18,639
so now you can see that we're rendering
469
00:16:18,639 --> 00:16:21,120
the three items in our to-do list now
470
00:16:21,120 --> 00:16:22,959
the divs are actually invisible but what
471
00:16:22,959 --> 00:16:25,920
you can do is you can click this mouse
472
00:16:25,920 --> 00:16:27,440
box icon
473
00:16:27,440 --> 00:16:30,160
and just hover hover over this div and
474
00:16:30,160 --> 00:16:31,600
it will highlight the div even though
475
00:16:31,600 --> 00:16:33,120
it's invisible so you can see that we
476
00:16:33,120 --> 00:16:34,800
created a div here
477
00:16:34,800 --> 00:16:36,959
and here and here so three divs for
478
00:16:36,959 --> 00:16:39,440
three to do list items if you click into
479
00:16:39,440 --> 00:16:41,199
it you can see that in our page we've
480
00:16:41,199 --> 00:16:44,800
created three divs here
481
00:16:49,600 --> 00:16:51,279
so when we're creating our app we don't
482
00:16:51,279 --> 00:16:52,720
actually want to write everything in
483
00:16:52,720 --> 00:16:54,320
html like this
484
00:16:54,320 --> 00:16:55,440
because we want to have the
485
00:16:55,440 --> 00:16:57,680
functionality to add and remove items
486
00:16:57,680 --> 00:17:00,639
from our list and html can't do that so
487
00:17:00,639 --> 00:17:02,880
we have to put everything in javascript
488
00:17:02,880 --> 00:17:04,640
remember that javascript is the language
489
00:17:04,640 --> 00:17:06,559
that you use to manage information while
490
00:17:06,559 --> 00:17:09,439
html just displays visual elements so
491
00:17:09,439 --> 00:17:11,119
next we're going to learn the javascript
492
00:17:11,119 --> 00:17:14,079
code for adding these divs to the body
493
00:17:14,079 --> 00:17:16,640
and we can do so in a few steps
494
00:17:16,640 --> 00:17:18,640
so the first step that we have to do
495
00:17:18,640 --> 00:17:21,439
is we're going to create a new html
496
00:17:21,439 --> 00:17:24,400
element in javascript by using this code
497
00:17:24,400 --> 00:17:27,400
document.createelement
498
00:17:27,679 --> 00:17:29,679
and we're going to create the div
499
00:17:29,679 --> 00:17:32,240
element just like we have here now you
500
00:17:32,240 --> 00:17:33,600
have to make sure that you write this
501
00:17:33,600 --> 00:17:36,640
out exactly and it's case sensitive
502
00:17:36,640 --> 00:17:38,640
otherwise you'll get an error
503
00:17:38,640 --> 00:17:40,559
so now that we have a div we want the
504
00:17:40,559 --> 00:17:42,480
text inside the div to be this to do
505
00:17:42,480 --> 00:17:44,640
item so to do that we're going to use
506
00:17:44,640 --> 00:17:47,760
this code element dot inner
507
00:17:47,760 --> 00:17:49,039
text
508
00:17:49,039 --> 00:17:50,080
equals
509
00:17:50,080 --> 00:17:51,520
to do one
510
00:17:51,520 --> 00:17:53,919
and what this does is it takes our div
511
00:17:53,919 --> 00:17:55,600
and it sets the
512
00:17:55,600 --> 00:17:58,160
text inside the div to be the value of
513
00:17:58,160 --> 00:18:00,720
to do one which is get groceries
514
00:18:00,720 --> 00:18:02,080
and the last thing we're going to do is
515
00:18:02,080 --> 00:18:04,240
we're going to add this div to our page
516
00:18:04,240 --> 00:18:06,480
and we can do that by using this command
517
00:18:06,480 --> 00:18:09,480
document.body.appendchild
518
00:18:12,840 --> 00:18:14,559
element
519
00:18:14,559 --> 00:18:16,080
so i'm going to pause here because i
520
00:18:16,080 --> 00:18:18,240
realize i just introduced a lot of code
521
00:18:18,240 --> 00:18:19,840
and i'm going to explain everything step
522
00:18:19,840 --> 00:18:21,039
by step
523
00:18:21,039 --> 00:18:22,640
so when we break it down you realize
524
00:18:22,640 --> 00:18:24,240
that we actually have a lot of the
525
00:18:24,240 --> 00:18:26,480
concepts that we need to understand this
526
00:18:26,480 --> 00:18:27,520
code
527
00:18:27,520 --> 00:18:29,520
first of all remember that javascript is
528
00:18:29,520 --> 00:18:32,480
about giving instructions to a computer
529
00:18:32,480 --> 00:18:35,360
so this part of the code says create an
530
00:18:35,360 --> 00:18:38,559
html element and this is the type of
531
00:18:38,559 --> 00:18:40,480
element that we want to create
532
00:18:40,480 --> 00:18:42,400
so what's interesting here is that we
533
00:18:42,400 --> 00:18:45,760
have an html element inside javascript
534
00:18:45,760 --> 00:18:48,559
so one of these is inside here
535
00:18:48,559 --> 00:18:50,080
and this is one of the special features
536
00:18:50,080 --> 00:18:51,919
of javascript where we can blur the
537
00:18:51,919 --> 00:18:55,600
lines between html and javascript
538
00:18:55,600 --> 00:18:58,080
so we can create elements in javascript
539
00:18:58,080 --> 00:19:00,720
and we can also save them in variables
540
00:19:00,720 --> 00:19:02,720
remember earlier that we are saving
541
00:19:02,720 --> 00:19:05,600
strings inside variables while an html
542
00:19:05,600 --> 00:19:07,840
element is another type of value that we
543
00:19:07,840 --> 00:19:10,480
can save inside a variable
544
00:19:10,480 --> 00:19:12,320
so that's the first line of code here
545
00:19:12,320 --> 00:19:14,080
now the second line is just another
546
00:19:14,080 --> 00:19:17,200
instruction to change the text inside
547
00:19:17,200 --> 00:19:20,320
the html element that we just created
548
00:19:20,320 --> 00:19:22,160
it's the same thing as going up here and
549
00:19:22,160 --> 00:19:24,160
changing the text here to be something
550
00:19:24,160 --> 00:19:26,160
else so the way that the second line of
551
00:19:26,160 --> 00:19:28,960
code works is that you take your html
552
00:19:28,960 --> 00:19:32,080
element you add a dot inner text after
553
00:19:32,080 --> 00:19:35,039
it and then you can set it to whatever
554
00:19:35,039 --> 00:19:36,960
string that you want
555
00:19:36,960 --> 00:19:39,039
now the third line here is just another
556
00:19:39,039 --> 00:19:42,160
instruction that adds an element to the
557
00:19:42,160 --> 00:19:44,240
end of the body
558
00:19:44,240 --> 00:19:47,120
so document.body actually refers to the
559
00:19:47,120 --> 00:19:48,880
body here
560
00:19:48,880 --> 00:19:50,720
and you'll notice that there's a lot of
561
00:19:50,720 --> 00:19:53,840
elements that are nested inside the body
562
00:19:53,840 --> 00:19:55,840
typically we call these elements
563
00:19:55,840 --> 00:19:59,039
children and the body the parent element
564
00:19:59,039 --> 00:20:01,120
so that's where appended child comes
565
00:20:01,120 --> 00:20:01,919
from
566
00:20:01,919 --> 00:20:04,000
and what this does is that it adds
567
00:20:04,000 --> 00:20:06,480
another child to the body so it's going
568
00:20:06,480 --> 00:20:08,880
to put it after all of these other child
569
00:20:08,880 --> 00:20:10,159
elements
570
00:20:10,159 --> 00:20:12,159
inside these brackets is the element
571
00:20:12,159 --> 00:20:14,320
that we want to add so we're going to
572
00:20:14,320 --> 00:20:17,520
take the div that we created up here
573
00:20:17,520 --> 00:20:19,760
so at the end of the day this is just
574
00:20:19,760 --> 00:20:21,760
three more instructions that i'm
575
00:20:21,760 --> 00:20:24,320
introducing to you in javascript
576
00:20:24,320 --> 00:20:25,520
and you don't have to worry about
577
00:20:25,520 --> 00:20:27,360
memorizing all of these instructions
578
00:20:27,360 --> 00:20:29,520
because remember i added a link to a
579
00:20:29,520 --> 00:20:31,280
cheat sheet that you can find in the
580
00:20:31,280 --> 00:20:33,600
description so how do i know all of
581
00:20:33,600 --> 00:20:35,280
these different types of instructions
582
00:20:35,280 --> 00:20:37,360
and how are you supposed to discover
583
00:20:37,360 --> 00:20:39,360
what types of instructions are available
584
00:20:39,360 --> 00:20:40,960
in javascript
585
00:20:40,960 --> 00:20:42,400
well i actually learned the same way
586
00:20:42,400 --> 00:20:44,480
that you're learning right now i learned
587
00:20:44,480 --> 00:20:46,640
each instruction one by one and i just
588
00:20:46,640 --> 00:20:48,799
memorized them but the most important
589
00:20:48,799 --> 00:20:50,480
thing here is to know what you want to
590
00:20:50,480 --> 00:20:52,960
do for example if i know that i want to
591
00:20:52,960 --> 00:20:55,679
create an html element but i forgot the
592
00:20:55,679 --> 00:20:57,919
code for it then i can just go to google
593
00:20:57,919 --> 00:21:00,640
and search how to create an html element
594
00:21:00,640 --> 00:21:03,200
in javascript
595
00:21:03,200 --> 00:21:05,039
and now we can run our code and see what
596
00:21:05,039 --> 00:21:06,559
happens
597
00:21:06,559 --> 00:21:08,320
so you see that because we have these
598
00:21:08,320 --> 00:21:10,799
three divs here these three items are
599
00:21:10,799 --> 00:21:12,720
still showing but you also notice that
600
00:21:12,720 --> 00:21:15,520
our javascript added a new div to the
601
00:21:15,520 --> 00:21:17,600
page and that's what we want we want our
602
00:21:17,600 --> 00:21:19,520
javascript to manage our information and
603
00:21:19,520 --> 00:21:22,320
determine what gets displayed
604
00:21:22,320 --> 00:21:23,760
i'm just going to take a break here to
605
00:21:23,760 --> 00:21:25,200
introduce myself
606
00:21:25,200 --> 00:21:27,679
i'm simon from super simple.dev and my
607
00:21:27,679 --> 00:21:29,039
mission is to make a tech career
608
00:21:29,039 --> 00:21:31,440
possible for anyone if you're getting
609
00:21:31,440 --> 00:21:33,120
value from this tutorial you can help me
610
00:21:33,120 --> 00:21:34,880
out in my mission by clicking that like
611
00:21:34,880 --> 00:21:36,640
button below so that this video will
612
00:21:36,640 --> 00:21:38,080
reach more people
613
00:21:38,080 --> 00:21:39,600
if you want to learn tech with me please
614
00:21:39,600 --> 00:21:41,600
subscribe for the latest videos and
615
00:21:41,600 --> 00:21:43,919
lessons thanks for watching so far let's
616
00:21:43,919 --> 00:21:45,679
get back to the tutorial
617
00:21:45,679 --> 00:21:46,960
now we're going to go back and we're
618
00:21:46,960 --> 00:21:49,360
going to create divs and insert divs for
619
00:21:49,360 --> 00:21:52,000
the other two items in our to-do list
620
00:21:52,000 --> 00:21:53,760
so what we can do here is we're just
621
00:21:53,760 --> 00:21:55,840
going to copy these lines
622
00:21:55,840 --> 00:21:58,159
and paste them and one thing you can do
623
00:21:58,159 --> 00:21:59,440
is
624
00:21:59,440 --> 00:22:01,440
we can reuse this variable name so we
625
00:22:01,440 --> 00:22:04,720
can reassign a variable to another value
626
00:22:04,720 --> 00:22:06,159
and this time we're going to create a
627
00:22:06,159 --> 00:22:08,159
new div and we're going to reassign it
628
00:22:08,159 --> 00:22:10,720
to the element variable and we're going
629
00:22:10,720 --> 00:22:13,520
to set the inner text of this new div to
630
00:22:13,520 --> 00:22:14,880
to do two
631
00:22:14,880 --> 00:22:16,720
and now we're gonna again we're gonna
632
00:22:16,720 --> 00:22:19,200
add this div to the body
633
00:22:19,200 --> 00:22:21,280
and when we run our code
634
00:22:21,280 --> 00:22:23,120
we can see that we added our second to
635
00:22:23,120 --> 00:22:25,679
do item and we're just gonna get rid of
636
00:22:25,679 --> 00:22:27,360
these divs so that we don't get confused
637
00:22:27,360 --> 00:22:28,960
here
638
00:22:28,960 --> 00:22:30,480
now i'm just gonna do it for the third
639
00:22:30,480 --> 00:22:33,120
one as well
640
00:22:34,720 --> 00:22:36,159
to do three
641
00:22:36,159 --> 00:22:38,159
and this is called reassigning the value
642
00:22:38,159 --> 00:22:39,440
of a variable
643
00:22:39,440 --> 00:22:41,280
now when we run this code we're going to
644
00:22:41,280 --> 00:22:43,520
see that our javascript added the three
645
00:22:43,520 --> 00:22:47,039
items to our web page
646
00:22:53,280 --> 00:22:54,640
one thing that you'll notice is that
647
00:22:54,640 --> 00:22:56,559
we've written a lot of code here so
648
00:22:56,559 --> 00:22:58,320
let's clean this up by using another
649
00:22:58,320 --> 00:23:01,280
feature of javascript called functions
650
00:23:01,280 --> 00:23:03,360
so functions are basically reusable
651
00:23:03,360 --> 00:23:06,480
pieces of code it helps us run this code
652
00:23:06,480 --> 00:23:08,320
over and over again without having to
653
00:23:08,320 --> 00:23:10,880
write the code over and over again
654
00:23:10,880 --> 00:23:12,000
so we're going to write our first
655
00:23:12,000 --> 00:23:12,960
function
656
00:23:12,960 --> 00:23:15,600
called add to do and this is a syntax
657
00:23:15,600 --> 00:23:17,520
for writing a function you have two
658
00:23:17,520 --> 00:23:19,600
round brackets and two squares brackets
659
00:23:19,600 --> 00:23:22,000
and we're going to paste copy paste this
660
00:23:22,000 --> 00:23:24,880
code inside the function
661
00:23:24,880 --> 00:23:26,000
i'm going to
662
00:23:26,000 --> 00:23:29,120
format it a little bit to look nice
663
00:23:29,120 --> 00:23:30,720
so what we can do with this function is
664
00:23:30,720 --> 00:23:32,799
that we can run this function by using
665
00:23:32,799 --> 00:23:33,919
this code
666
00:23:33,919 --> 00:23:37,039
add to do bracket bracket
667
00:23:37,039 --> 00:23:39,200
when the computer sees this line of code
668
00:23:39,200 --> 00:23:40,799
it's going to run the function which
669
00:23:40,799 --> 00:23:42,799
means it's going to run these lines of
670
00:23:42,799 --> 00:23:45,679
code one by one so let's refresh a page
671
00:23:45,679 --> 00:23:47,440
and see what happens
672
00:23:47,440 --> 00:23:48,799
so you'll notice that there isn't much
673
00:23:48,799 --> 00:23:50,960
difference because here we're basically
674
00:23:50,960 --> 00:23:53,440
doing the same thing as before we are
675
00:23:53,440 --> 00:23:56,159
running these lines one by one now the
676
00:23:56,159 --> 00:23:58,000
cool thing about functions is that if
677
00:23:58,000 --> 00:23:59,279
you
678
00:23:59,279 --> 00:24:01,200
run the function again
679
00:24:01,200 --> 00:24:03,360
it will run these three lines of code
680
00:24:03,360 --> 00:24:06,080
again without you having to rewrite them
681
00:24:06,080 --> 00:24:07,440
so if we go back to the web page and
682
00:24:07,440 --> 00:24:09,520
refresh you can see that get groceries
683
00:24:09,520 --> 00:24:11,840
has been added two times so you can see
684
00:24:11,840 --> 00:24:13,360
that functions are really good for
685
00:24:13,360 --> 00:24:16,400
reusing pieces of code
686
00:24:16,400 --> 00:24:18,000
so one of the problems that we have here
687
00:24:18,000 --> 00:24:19,600
is that this function only runs for
688
00:24:19,600 --> 00:24:21,919
to-do one how do we adapt this function
689
00:24:21,919 --> 00:24:24,400
to also work with to do two and to do
690
00:24:24,400 --> 00:24:26,480
three or any to-do that we have in the
691
00:24:26,480 --> 00:24:27,520
future
692
00:24:27,520 --> 00:24:29,279
and the way we do that is we're gonna
693
00:24:29,279 --> 00:24:31,760
introduce a new variable up here let's
694
00:24:31,760 --> 00:24:33,200
call it to do
695
00:24:33,200 --> 00:24:34,720
title
696
00:24:34,720 --> 00:24:36,960
and we're going to use this variable
697
00:24:36,960 --> 00:24:39,520
in here
698
00:24:39,520 --> 00:24:40,880
so what we're doing here is we're
699
00:24:40,880 --> 00:24:42,640
creating a variable just like we had
700
00:24:42,640 --> 00:24:45,919
before but here we don't have to use let
701
00:24:45,919 --> 00:24:47,360
we just have to give the name of the
702
00:24:47,360 --> 00:24:48,960
variable and then we can use the
703
00:24:48,960 --> 00:24:51,360
variable inside our function
704
00:24:51,360 --> 00:24:52,960
now we have a variable here at the top
705
00:24:52,960 --> 00:24:55,360
of a function this is called a parameter
706
00:24:55,360 --> 00:24:57,520
so how do we set the value of this
707
00:24:57,520 --> 00:24:59,520
variable or this parameter
708
00:24:59,520 --> 00:25:01,279
so we can do it by setting the value
709
00:25:01,279 --> 00:25:03,279
here let's set the value to
710
00:25:03,279 --> 00:25:05,360
new to do and let's get rid of this line
711
00:25:05,360 --> 00:25:07,440
so we don't get confused
712
00:25:07,440 --> 00:25:09,120
now let's refresh our page and see what
713
00:25:09,120 --> 00:25:11,440
happens
714
00:25:13,600 --> 00:25:16,000
so what happens here is that to do title
715
00:25:16,000 --> 00:25:18,240
gets the value of whatever is between
716
00:25:18,240 --> 00:25:20,559
these brackets and that parameter can
717
00:25:20,559 --> 00:25:22,799
then be used as just a regular variable
718
00:25:22,799 --> 00:25:24,400
inside the function so we're going to
719
00:25:24,400 --> 00:25:26,799
set the inner text to the value of to do
720
00:25:26,799 --> 00:25:29,360
title which is set over here as near to
721
00:25:29,360 --> 00:25:30,240
do
722
00:25:30,240 --> 00:25:32,559
so let's set the value of to-do title to
723
00:25:32,559 --> 00:25:35,039
be our to-do
724
00:25:35,039 --> 00:25:37,279
and let's refresh the page and you can
725
00:25:37,279 --> 00:25:38,880
see that we're back to rendering our
726
00:25:38,880 --> 00:25:40,720
first to-do item
727
00:25:40,720 --> 00:25:42,799
now let's use our function for 222 and
728
00:25:42,799 --> 00:25:44,640
to do three so we can get rid of all
729
00:25:44,640 --> 00:25:46,799
this code and we just have to run the
730
00:25:46,799 --> 00:25:49,039
function
731
00:25:49,039 --> 00:25:51,760
with different values i'm going to copy
732
00:25:51,760 --> 00:25:52,799
this
733
00:25:52,799 --> 00:25:56,400
i'm going to paste this
734
00:25:56,400 --> 00:25:57,919
we're just going to
735
00:25:57,919 --> 00:25:59,520
refresh the page and you can see that
736
00:25:59,520 --> 00:26:00,960
it's doing the exact same thing as
737
00:26:00,960 --> 00:26:03,760
before except now we are reusing the
738
00:26:03,760 --> 00:26:05,760
code inside this function instead of
739
00:26:05,760 --> 00:26:10,039
having to write it all over again
740
00:26:10,880 --> 00:26:12,799
here's some terminology that's related
741
00:26:12,799 --> 00:26:15,039
to functions that you need to know now
742
00:26:15,039 --> 00:26:17,039
these are the proper terminologies that
743
00:26:17,039 --> 00:26:18,799
most programmers use so it's important
744
00:26:18,799 --> 00:26:21,279
to understand what they mean
745
00:26:21,279 --> 00:26:23,520
so when we're creating a function this
746
00:26:23,520 --> 00:26:26,960
is known as defining a function
747
00:26:26,960 --> 00:26:28,960
and we've already had an example of
748
00:26:28,960 --> 00:26:32,080
running a function this is also known as
749
00:26:32,080 --> 00:26:33,840
calling the function
750
00:26:33,840 --> 00:26:36,080
and sometimes we say this is executing a
751
00:26:36,080 --> 00:26:38,720
function they all mean the same thing
752
00:26:38,720 --> 00:26:40,640
when a function has a parameter like
753
00:26:40,640 --> 00:26:42,960
this we say that the function takes a
754
00:26:42,960 --> 00:26:44,480
parameter
755
00:26:44,480 --> 00:26:47,360
so this function takes one parameter
756
00:26:47,360 --> 00:26:48,799
that's a string
757
00:26:48,799 --> 00:26:50,400
functions can also have multiple
758
00:26:50,400 --> 00:26:52,400
parameters and you separate them by
759
00:26:52,400 --> 00:26:54,480
commas so we would normally say that
760
00:26:54,480 --> 00:26:57,039
this function takes two parameters a
761
00:26:57,039 --> 00:27:00,080
string and a number
762
00:27:00,080 --> 00:27:01,760
now when we're calling the function we
763
00:27:01,760 --> 00:27:04,960
can set the parameter by putting a value
764
00:27:04,960 --> 00:27:06,720
between the brackets
765
00:27:06,720 --> 00:27:10,000
this is also known as passing in a value
766
00:27:10,000 --> 00:27:12,799
to the function
767
00:27:13,120 --> 00:27:15,679
so for this function we're passing in a
768
00:27:15,679 --> 00:27:18,399
string and a number
769
00:27:18,399 --> 00:27:20,640
and the value that we pass in can also
770
00:27:20,640 --> 00:27:23,360
be called an argument
771
00:27:23,360 --> 00:27:25,279
so these are the proper terminologies
772
00:27:25,279 --> 00:27:26,960
that you'll come across as you learn
773
00:27:26,960 --> 00:27:28,880
programming and i'll be using these
774
00:27:28,880 --> 00:27:30,320
throughout the rest of the course to
775
00:27:30,320 --> 00:27:32,640
give you more practice in recognizing
776
00:27:32,640 --> 00:27:35,600
and understanding what they mean
777
00:27:35,600 --> 00:27:36,880
before we continue here's some more
778
00:27:36,880 --> 00:27:38,799
exercises to get you more familiar with
779
00:27:38,799 --> 00:27:42,640
functions and function parameters
780
00:27:44,080 --> 00:27:45,919
so now that we're using functions in our
781
00:27:45,919 --> 00:27:48,240
code you can see that it's a lot cleaner
782
00:27:48,240 --> 00:27:50,080
but there's another problem here if
783
00:27:50,080 --> 00:27:52,480
someone wants to use our to-do app they
784
00:27:52,480 --> 00:27:54,480
have to go in the code and modify
785
00:27:54,480 --> 00:27:56,399
themselves so if they want to add a new
786
00:27:56,399 --> 00:27:58,960
to-do they would have to go in
787
00:27:58,960 --> 00:28:01,919
and type out the to-do in the code and
788
00:28:01,919 --> 00:28:03,919
also run this function and that's not
789
00:28:03,919 --> 00:28:05,440
possible when you're using an
790
00:28:05,440 --> 00:28:07,360
application
791
00:28:07,360 --> 00:28:09,440
so how do we solve this problem
792
00:28:09,440 --> 00:28:10,720
we're going to use another feature of
793
00:28:10,720 --> 00:28:13,600
javascript called an array
794
00:28:13,600 --> 00:28:15,360
so we learned about strings already
795
00:28:15,360 --> 00:28:17,679
which is this thing inside quotes now
796
00:28:17,679 --> 00:28:19,279
we're going to learn about another type
797
00:28:19,279 --> 00:28:21,440
of value in javascript called the array
798
00:28:21,440 --> 00:28:23,600
type and this is what an array looks
799
00:28:23,600 --> 00:28:25,520
like
800
00:28:25,520 --> 00:28:30,440
let to do's equals get groceries
801
00:28:30,440 --> 00:28:33,640
[Applause]
802
00:28:34,720 --> 00:28:36,640
so this is an array and you can tell
803
00:28:36,640 --> 00:28:38,559
that it's an array by the square
804
00:28:38,559 --> 00:28:40,480
brackets around this and this tells the
805
00:28:40,480 --> 00:28:42,080
computer that hey we're creating an
806
00:28:42,080 --> 00:28:43,360
array here
807
00:28:43,360 --> 00:28:45,120
and you'll notice that inside this array
808
00:28:45,120 --> 00:28:47,520
we can store multiple values so right
809
00:28:47,520 --> 00:28:50,000
now we're storing three string values
810
00:28:50,000 --> 00:28:52,720
and these values are separated by commas
811
00:28:52,720 --> 00:28:54,880
that's how the computer knows that this
812
00:28:54,880 --> 00:28:57,360
is a separate value from this
813
00:28:57,360 --> 00:28:59,279
so just like how a string represents a
814
00:28:59,279 --> 00:29:02,399
piece of text an array represents a list
815
00:29:02,399 --> 00:29:03,840
of values
816
00:29:03,840 --> 00:29:05,760
and the nice thing about an array is if
817
00:29:05,760 --> 00:29:08,720
we want to add to this list our code can
818
00:29:08,720 --> 00:29:10,880
add the values without having to modify
819
00:29:10,880 --> 00:29:13,279
the code ourselves
820
00:29:13,279 --> 00:29:15,200
and here's a command for modifying an
821
00:29:15,200 --> 00:29:17,039
array
822
00:29:17,039 --> 00:29:19,760
to do's dot push
823
00:29:19,760 --> 00:29:20,799
another
824
00:29:20,799 --> 00:29:22,799
to do
825
00:29:22,799 --> 00:29:24,960
and we're going to go into our web page
826
00:29:24,960 --> 00:29:28,000
here we can go into the console tab
827
00:29:28,000 --> 00:29:29,240
and we're going to
828
00:29:29,240 --> 00:29:33,279
console.log our to-do's array
829
00:29:33,279 --> 00:29:34,720
and you'll notice that we have four
830
00:29:34,720 --> 00:29:36,799
values in our array because we just
831
00:29:36,799 --> 00:29:39,520
added it here
832
00:29:39,520 --> 00:29:41,200
i'm going to do another syntax lesson
833
00:29:41,200 --> 00:29:43,520
here so we just learned what a function
834
00:29:43,520 --> 00:29:46,399
is and you'll notice that push is also a
835
00:29:46,399 --> 00:29:48,240
function but it's a function that is
836
00:29:48,240 --> 00:29:50,559
predefined by javascript
837
00:29:50,559 --> 00:29:52,720
and when you see a dot here it means
838
00:29:52,720 --> 00:29:55,440
that this function is attached to this
839
00:29:55,440 --> 00:29:56,720
array
840
00:29:56,720 --> 00:29:59,039
in javascript arrays have many functions
841
00:29:59,039 --> 00:30:01,679
attached to them when arrays are created
842
00:30:01,679 --> 00:30:03,120
for example another function that's
843
00:30:03,120 --> 00:30:05,120
attached to array is called
844
00:30:05,120 --> 00:30:06,480
pop
845
00:30:06,480 --> 00:30:08,480
and what pop does is the opposite of
846
00:30:08,480 --> 00:30:11,120
push it removes the last element of an
847
00:30:11,120 --> 00:30:13,600
array so if we go back to our web page
848
00:30:13,600 --> 00:30:15,600
and we refresh the page
849
00:30:15,600 --> 00:30:17,919
and we console.
850
00:30:17,919 --> 00:30:18,960
log
851
00:30:18,960 --> 00:30:21,200
our list again
852
00:30:21,200 --> 00:30:23,360
we'll notice that the fourth element has
853
00:30:23,360 --> 00:30:27,039
now been taken away by this line of code
854
00:30:27,039 --> 00:30:29,039
we have a function that's attached to an
855
00:30:29,039 --> 00:30:31,919
array like this this is called a method
856
00:30:31,919 --> 00:30:34,399
and you can access methods using this
857
00:30:34,399 --> 00:30:37,600
dot notation so if you have an array you
858
00:30:37,600 --> 00:30:40,480
can access all the methods here using a
859
00:30:40,480 --> 00:30:43,520
dot and strings have methods as well for
860
00:30:43,520 --> 00:30:45,760
example you can do hello
861
00:30:45,760 --> 00:30:48,480
dot and access any of these methods so
862
00:30:48,480 --> 00:30:51,440
we're gonna access to uppercase as an
863
00:30:51,440 --> 00:30:53,360
example and we're going to run this
864
00:30:53,360 --> 00:30:55,600
method or this function attached to the
865
00:30:55,600 --> 00:30:56,960
string
866
00:30:56,960 --> 00:30:59,840
so let's go back and refresh our page
867
00:30:59,840 --> 00:31:01,519
and you'll see that it logs the
868
00:31:01,519 --> 00:31:05,120
uppercase version of our string
869
00:31:05,120 --> 00:31:06,880
so that's what methods are and we use
870
00:31:06,880 --> 00:31:09,440
methods all the time on the job for now
871
00:31:09,440 --> 00:31:11,480
we're just going to use the
872
00:31:11,480 --> 00:31:15,279
array.push method for our project
873
00:31:15,279 --> 00:31:17,200
now let's use our new array to render
874
00:31:17,200 --> 00:31:18,880
our to-do list instead of using these
875
00:31:18,880 --> 00:31:21,279
variables as we said before if someone's
876
00:31:21,279 --> 00:31:23,760
using our app we can't really be
877
00:31:23,760 --> 00:31:25,840
modifying our source code and adding new
878
00:31:25,840 --> 00:31:28,320
variables for new to-do items so how do
879
00:31:28,320 --> 00:31:30,159
we solve this problem well we're going
880
00:31:30,159 --> 00:31:31,840
to use another feature in javascript
881
00:31:31,840 --> 00:31:33,279
called loops
882
00:31:33,279 --> 00:31:35,279
and the way the loops work is that you
883
00:31:35,279 --> 00:31:37,600
start with an array and use the dot
884
00:31:37,600 --> 00:31:39,760
notation to see what other methods are
885
00:31:39,760 --> 00:31:41,279
on the array so
886
00:31:41,279 --> 00:31:43,519
one method that we used already is push
887
00:31:43,519 --> 00:31:44,799
this time we're going to use the for
888
00:31:44,799 --> 00:31:46,720
each method and we're going to call for
889
00:31:46,720 --> 00:31:48,559
each
890
00:31:48,559 --> 00:31:52,640
so we're going to pass in a function
891
00:31:52,640 --> 00:31:54,320
and what for each does
892
00:31:54,320 --> 00:31:57,120
is that it goes through each value of
893
00:31:57,120 --> 00:31:59,279
the array so it takes this value and it
894
00:31:59,279 --> 00:32:01,840
puts this value inside a parameter in
895
00:32:01,840 --> 00:32:04,080
the function and it's going to run this
896
00:32:04,080 --> 00:32:06,159
function and then for the second value
897
00:32:06,159 --> 00:32:07,279
it's going to put the second value in
898
00:32:07,279 --> 00:32:08,559
the parameter it's going to run the
899
00:32:08,559 --> 00:32:11,120
function again and for the third value
900
00:32:11,120 --> 00:32:13,120
it's going to put this in the parameter
901
00:32:13,120 --> 00:32:15,039
and run it again so it's going to loop
902
00:32:15,039 --> 00:32:17,679
through each value in the array and it's
903
00:32:17,679 --> 00:32:19,840
going to run this function that's what
904
00:32:19,840 --> 00:32:21,519
for each means
905
00:32:21,519 --> 00:32:23,039
so since these values are going into
906
00:32:23,039 --> 00:32:25,360
this param let's rename it to something
907
00:32:25,360 --> 00:32:28,880
more understandable like to do title
908
00:32:28,880 --> 00:32:30,640
and in order to render our to-do's in
909
00:32:30,640 --> 00:32:33,200
the web page let's just reuse this code
910
00:32:33,200 --> 00:32:34,799
so we're just going to copy
911
00:32:34,799 --> 00:32:36,559
and paste it here
912
00:32:36,559 --> 00:32:37,760
and now we can get rid of these
913
00:32:37,760 --> 00:32:39,919
variables because we're using the loop
914
00:32:39,919 --> 00:32:43,840
to actually render our array
915
00:32:44,880 --> 00:32:47,200
so if we go back to our web page
916
00:32:47,200 --> 00:32:49,279
we can see that we're still rendering
917
00:32:49,279 --> 00:32:51,279
our to-do list but now we're using a
918
00:32:51,279 --> 00:32:54,279
loop
919
00:33:03,760 --> 00:33:05,600
so the advantage of using an array to
920
00:33:05,600 --> 00:33:07,919
render this list is that if we want to
921
00:33:07,919 --> 00:33:09,679
add something to our to-do list we don't
922
00:33:09,679 --> 00:33:11,600
need to go into our code and add new
923
00:33:11,600 --> 00:33:14,080
variables we can actually hook up our
924
00:33:14,080 --> 00:33:16,240
button so that when we click our button
925
00:33:16,240 --> 00:33:19,519
it'll call some code like to do.push and
926
00:33:19,519 --> 00:33:21,519
a button click will be adding stuff to
927
00:33:21,519 --> 00:33:23,279
our to-do list
928
00:33:23,279 --> 00:33:25,519
so let's set that up now
929
00:33:25,519 --> 00:33:27,600
first let's get rid of this line because
930
00:33:27,600 --> 00:33:29,200
we're going to use the button to add
931
00:33:29,200 --> 00:33:31,440
stuff to the array so the re the way we
932
00:33:31,440 --> 00:33:34,159
do that is by using some html we're
933
00:33:34,159 --> 00:33:37,120
going to use on click equals
934
00:33:37,120 --> 00:33:40,159
to dose dot push
935
00:33:40,159 --> 00:33:43,120
new to do
936
00:33:44,480 --> 00:33:47,840
so this bit of code here is part of html
937
00:33:47,840 --> 00:33:50,480
and this is known as an attribute so in
938
00:33:50,480 --> 00:33:52,880
html you can change the behavior or
939
00:33:52,880 --> 00:33:54,960
appearance of elements by adding
940
00:33:54,960 --> 00:33:57,360
attributes for example another attribute
941
00:33:57,360 --> 00:34:00,159
that a button can have is the disabled
942
00:34:00,159 --> 00:34:02,240
attribute
943
00:34:02,240 --> 00:34:03,919
and when you set the value of this
944
00:34:03,919 --> 00:34:07,279
attribute to true you'll notice that
945
00:34:07,279 --> 00:34:09,440
this button becomes disabled and i can't
946
00:34:09,440 --> 00:34:11,119
click it anymore
947
00:34:11,119 --> 00:34:13,679
so the syntax of an attribute looks like
948
00:34:13,679 --> 00:34:16,320
this the attribute name is on the left
949
00:34:16,320 --> 00:34:18,560
and then the attribute's value is on the
950
00:34:18,560 --> 00:34:19,440
right
951
00:34:19,440 --> 00:34:21,199
and it's surrounded by double quotes and
952
00:34:21,199 --> 00:34:23,520
there's no space between the equal sign
953
00:34:23,520 --> 00:34:24,639
so it's a little different than
954
00:34:24,639 --> 00:34:26,560
javascript
955
00:34:26,560 --> 00:34:28,480
so let's go back to this on click
956
00:34:28,480 --> 00:34:30,960
attribute and this basically says when i
957
00:34:30,960 --> 00:34:32,800
click this button we're going to run the
958
00:34:32,800 --> 00:34:35,040
code that's inside the value of this
959
00:34:35,040 --> 00:34:36,320
attribute so in this case we're going to
960
00:34:36,320 --> 00:34:39,119
run to do stop push new to do and that's
961
00:34:39,119 --> 00:34:40,239
going to push
962
00:34:40,239 --> 00:34:41,199
this
963
00:34:41,199 --> 00:34:42,960
onto the end of the array so let's give
964
00:34:42,960 --> 00:34:44,960
that a try
965
00:34:44,960 --> 00:34:46,879
so when i click this button i can
966
00:34:46,879 --> 00:34:50,079
console.log to do's
967
00:34:50,079 --> 00:34:51,679
and you'll notice that the new to do
968
00:34:51,679 --> 00:34:53,359
value has been added to the end of the
969
00:34:53,359 --> 00:34:56,000
array so everything's working
970
00:34:56,000 --> 00:34:58,880
so a best practice is not to write your
971
00:34:58,880 --> 00:35:01,440
code in the html like this instead what
972
00:35:01,440 --> 00:35:05,599
we want to do is create a function
973
00:35:05,599 --> 00:35:07,520
and we're going to move this code inside
974
00:35:07,520 --> 00:35:09,200
the function
975
00:35:09,200 --> 00:35:11,200
and instead we're going to
976
00:35:11,200 --> 00:35:13,440
just call the function
977
00:35:13,440 --> 00:35:14,839
when we click the
978
00:35:14,839 --> 00:35:17,520
button okay so you notice that we're
979
00:35:17,520 --> 00:35:20,560
always adding the same value new to do
980
00:35:20,560 --> 00:35:23,200
to our array but ideally we would want
981
00:35:23,200 --> 00:35:25,440
to type in a text box what we want our
982
00:35:25,440 --> 00:35:26,720
to do to be
983
00:35:26,720 --> 00:35:29,359
and then use the value of the text box
984
00:35:29,359 --> 00:35:31,680
so let's create that next so how do we
985
00:35:31,680 --> 00:35:34,560
create a text box in html well the code
986
00:35:34,560 --> 00:35:36,640
for that is
987
00:35:36,640 --> 00:35:37,839
input
988
00:35:37,839 --> 00:35:41,200
type equals text
989
00:35:41,200 --> 00:35:42,839
and close
990
00:35:42,839 --> 00:35:45,200
input so here you'll notice that we're
991
00:35:45,200 --> 00:35:47,359
using attributes again this time we're
992
00:35:47,359 --> 00:35:49,200
using the type attribute and we're
993
00:35:49,200 --> 00:35:51,680
setting it to the value of text and what
994
00:35:51,680 --> 00:35:54,800
this does is it creates a text box
995
00:35:54,800 --> 00:35:57,440
in our web page so if we refresh our web
996
00:35:57,440 --> 00:35:58,960
page we can see that we just created a
997
00:35:58,960 --> 00:36:01,760
text box that we can now type into
998
00:36:01,760 --> 00:36:03,760
now if we change the type attribute to
999
00:36:03,760 --> 00:36:06,400
another value like check box you'll
1000
00:36:06,400 --> 00:36:08,480
notice that it changes the appearance
1001
00:36:08,480 --> 00:36:11,119
and the behavior of the input element so
1002
00:36:11,119 --> 00:36:12,960
now it becomes an actual checkbox that i
1003
00:36:12,960 --> 00:36:14,960
can check and uncheck
1004
00:36:14,960 --> 00:36:16,800
but let's go back and we'll change this
1005
00:36:16,800 --> 00:36:18,560
back to a text box because that's what
1006
00:36:18,560 --> 00:36:19,680
we want
1007
00:36:19,680 --> 00:36:21,920
so now a short syntax lesson here if
1008
00:36:21,920 --> 00:36:24,240
there's nothing between the opening and
1009
00:36:24,240 --> 00:36:26,880
closing tags you want to use the html
1010
00:36:26,880 --> 00:36:28,960
shorthand which is this and this
1011
00:36:28,960 --> 00:36:31,119
basically means the same thing
1012
00:36:31,119 --> 00:36:33,520
so now when we click our add button by
1013
00:36:33,520 --> 00:36:36,320
the way let's change this to say add
1014
00:36:36,320 --> 00:36:37,200
to do
1015
00:36:37,200 --> 00:36:38,880
so when we click this button
1016
00:36:38,880 --> 00:36:41,119
we want our code to grab the value
1017
00:36:41,119 --> 00:36:43,440
inside the text box and then push that
1018
00:36:43,440 --> 00:36:46,320
value onto our array so how do we do
1019
00:36:46,320 --> 00:36:47,280
that
1020
00:36:47,280 --> 00:36:48,640
i'm going to introduce yet another
1021
00:36:48,640 --> 00:36:50,480
attribute and you notice attributes are
1022
00:36:50,480 --> 00:36:53,119
really important html and this attribute
1023
00:36:53,119 --> 00:36:55,119
is one of the most important attributes
1024
00:36:55,119 --> 00:36:57,280
and it's called the id attribute so
1025
00:36:57,280 --> 00:36:59,040
we're going to put
1026
00:36:59,040 --> 00:37:00,000
id
1027
00:37:00,000 --> 00:37:03,760
equals to do dash title
1028
00:37:03,760 --> 00:37:07,200
and what id does is that it identifies
1029
00:37:07,200 --> 00:37:09,839
this element so that we can grab it with
1030
00:37:09,839 --> 00:37:12,079
our javascript so that we can find it
1031
00:37:12,079 --> 00:37:14,079
and grab it with our javascript so to
1032
00:37:14,079 --> 00:37:17,040
use our new id we can go in here and
1033
00:37:17,040 --> 00:37:19,520
we're going to type out this code
1034
00:37:19,520 --> 00:37:20,960
we're going to create a variable called
1035
00:37:20,960 --> 00:37:23,200
text box to store our text box so let
1036
00:37:23,200 --> 00:37:25,200
text box and we're going to set it to
1037
00:37:25,200 --> 00:37:27,280
this value we're going to run this code
1038
00:37:27,280 --> 00:37:29,760
to grab
1039
00:37:29,760 --> 00:37:33,839
our text box so document.getelementbyid
1040
00:37:33,839 --> 00:37:36,480
and you're going to give the id of the
1041
00:37:36,480 --> 00:37:39,280
input element of our text box
1042
00:37:39,280 --> 00:37:41,280
and to get whatever we typed in the text
1043
00:37:41,280 --> 00:37:44,160
box we're going to do let the title
1044
00:37:44,160 --> 00:37:47,520
equals text box dot value
1045
00:37:47,520 --> 00:37:50,240
oops
1046
00:37:50,240 --> 00:37:52,400
and now instead of pushing new to do to
1047
00:37:52,400 --> 00:37:54,400
our array every time we're going to push
1048
00:37:54,400 --> 00:37:56,240
whatever's in our text box so we're
1049
00:37:56,240 --> 00:37:59,119
going to do title
1050
00:37:59,280 --> 00:38:01,200
remember we're just giving instructions
1051
00:38:01,200 --> 00:38:02,480
to the computer
1052
00:38:02,480 --> 00:38:05,040
so this code tells the computer to get
1053
00:38:05,040 --> 00:38:08,400
an html element by its id
1054
00:38:08,400 --> 00:38:10,720
and using dot value we can get whatever
1055
00:38:10,720 --> 00:38:13,280
we typed out of the text box
1056
00:38:13,280 --> 00:38:14,960
and if you're ever unsure about what
1057
00:38:14,960 --> 00:38:17,280
code to use then you can always google
1058
00:38:17,280 --> 00:38:20,079
what you want to do
1059
00:38:23,920 --> 00:38:25,760
now let's refresh your page and see what
1060
00:38:25,760 --> 00:38:28,160
happens so let me type a new to-do
1061
00:38:28,160 --> 00:38:29,599
one two three
1062
00:38:29,599 --> 00:38:31,040
add to do
1063
00:38:31,040 --> 00:38:33,200
let me console.log here just to make
1064
00:38:33,200 --> 00:38:34,720
sure that it worked
1065
00:38:34,720 --> 00:38:36,160
so now you see that we're able to use
1066
00:38:36,160 --> 00:38:41,880
this text box to add items to our array
1067
00:38:51,359 --> 00:38:52,800
so you might be wondering how come our
1068
00:38:52,800 --> 00:38:54,880
new to do is not showing up in our
1069
00:38:54,880 --> 00:38:56,240
webpage yet
1070
00:38:56,240 --> 00:38:58,480
and the reason for that is we have this
1071
00:38:58,480 --> 00:39:01,200
code which goes through each to do and
1072
00:39:01,200 --> 00:39:03,040
renders it on the web page
1073
00:39:03,040 --> 00:39:04,720
but when we click the button this code
1074
00:39:04,720 --> 00:39:06,240
has already finished it's not going to
1075
00:39:06,240 --> 00:39:08,320
run anymore so what we actually want to
1076
00:39:08,320 --> 00:39:09,440
do is
1077
00:39:09,440 --> 00:39:11,359
we want to run this code
1078
00:39:11,359 --> 00:39:13,440
again
1079
00:39:13,440 --> 00:39:16,000
and this code is going to run on our
1080
00:39:16,000 --> 00:39:18,400
updated to-do list because we pushed a
1081
00:39:18,400 --> 00:39:21,119
new element to the end of the list
1082
00:39:21,119 --> 00:39:23,359
but remember that functions are reusable
1083
00:39:23,359 --> 00:39:25,520
pieces of code so instead of copying
1084
00:39:25,520 --> 00:39:27,280
this code over and over
1085
00:39:27,280 --> 00:39:29,520
let's create a new function and let's
1086
00:39:29,520 --> 00:39:31,040
call it render
1087
00:39:31,040 --> 00:39:32,720
and we're going to copy we're going to
1088
00:39:32,720 --> 00:39:35,040
cut and paste this code inside
1089
00:39:35,040 --> 00:39:36,320
and instead of
1090
00:39:36,320 --> 00:39:37,599
writing that code all over again we're
1091
00:39:37,599 --> 00:39:39,520
just going to call render in both of
1092
00:39:39,520 --> 00:39:42,760
these places
1093
00:39:43,440 --> 00:39:45,520
okay so what should happen is when we
1094
00:39:45,520 --> 00:39:47,440
start our web page it's going to call
1095
00:39:47,440 --> 00:39:50,240
render which is going to use this array
1096
00:39:50,240 --> 00:39:52,000
and render these three elements on the
1097
00:39:52,000 --> 00:39:53,920
web page and then when we click the
1098
00:39:53,920 --> 00:39:57,040
button it's going to add a to do to our
1099
00:39:57,040 --> 00:39:58,880
array and it's going to render it again
1100
00:39:58,880 --> 00:40:01,920
so let's see what happens
1101
00:40:03,920 --> 00:40:07,040
i'm going to add another to do
1102
00:40:07,040 --> 00:40:08,640
and you'll notice that instead of adding
1103
00:40:08,640 --> 00:40:10,640
it to the end of the list we're actually
1104
00:40:10,640 --> 00:40:13,440
copying this list again and then adding
1105
00:40:13,440 --> 00:40:15,839
it to the end so what went wrong here
1106
00:40:15,839 --> 00:40:17,359
well you'll notice that the render
1107
00:40:17,359 --> 00:40:19,839
method actually just keeps adding stuff
1108
00:40:19,839 --> 00:40:22,000
to the web page it doesn't actually
1109
00:40:22,000 --> 00:40:24,960
reset our list so what we want to do
1110
00:40:24,960 --> 00:40:27,280
is we want some code here that will
1111
00:40:27,280 --> 00:40:28,560
reset
1112
00:40:28,560 --> 00:40:31,359
our list to be empty and then we'll
1113
00:40:31,359 --> 00:40:33,599
re-render our list
1114
00:40:33,599 --> 00:40:35,760
by the way this piece of code is called
1115
00:40:35,760 --> 00:40:38,560
a comment and the syntax for comment is
1116
00:40:38,560 --> 00:40:40,640
you started with slash slash and
1117
00:40:40,640 --> 00:40:42,880
everything after this the computer would
1118
00:40:42,880 --> 00:40:44,400
just ignore so
1119
00:40:44,400 --> 00:40:46,720
if i put two slashes here it'll start
1120
00:40:46,720 --> 00:40:49,440
ignoring all this code
1121
00:40:49,440 --> 00:40:51,440
and there's two ways of writing comments
1122
00:40:51,440 --> 00:40:53,200
one is using double slash and the other
1123
00:40:53,200 --> 00:40:55,680
is one is using slash star in case you
1124
00:40:55,680 --> 00:40:57,440
want to write
1125
00:40:57,440 --> 00:41:00,240
multiple lines on your comments
1126
00:41:00,240 --> 00:41:01,839
and what comments are good for is that
1127
00:41:01,839 --> 00:41:04,000
it allows us to comment on our code so
1128
00:41:04,000 --> 00:41:06,079
that anyone who reads the code will be
1129
00:41:06,079 --> 00:41:08,160
able to understand it better
1130
00:41:08,160 --> 00:41:09,760
but it doesn't actually run the code so
1131
00:41:09,760 --> 00:41:11,920
we don't have to worry about any bugs or
1132
00:41:11,920 --> 00:41:14,560
behavior changes
1133
00:41:14,560 --> 00:41:16,319
now let's learn how to reset our list
1134
00:41:16,319 --> 00:41:18,960
before we re-render it so let's get rid
1135
00:41:18,960 --> 00:41:20,960
of these comments for now
1136
00:41:20,960 --> 00:41:22,400
and you'll remember at the beginning of
1137
00:41:22,400 --> 00:41:24,000
this course that we learned a piece of
1138
00:41:24,000 --> 00:41:25,280
code called
1139
00:41:25,280 --> 00:41:27,920
document.body.inner
1140
00:41:27,920 --> 00:41:31,599
html equals hello
1141
00:41:31,599 --> 00:41:33,920
so what this code does is that it wipes
1142
00:41:33,920 --> 00:41:35,599
out the entire body
1143
00:41:35,599 --> 00:41:38,240
and in this case body is the actual body
1144
00:41:38,240 --> 00:41:40,160
element so everything inside body gets
1145
00:41:40,160 --> 00:41:42,880
wiped out and is replaced with
1146
00:41:42,880 --> 00:41:44,960
this value
1147
00:41:44,960 --> 00:41:46,960
so this is sort of what we want we want
1148
00:41:46,960 --> 00:41:49,040
to wipe out our list but we don't really
1149
00:41:49,040 --> 00:41:50,640
want to wipe out the
1150
00:41:50,640 --> 00:41:53,040
button and the text box
1151
00:41:53,040 --> 00:41:55,359
so what do we do well let's go into our
1152
00:41:55,359 --> 00:41:58,079
code and we're actually going to put our
1153
00:41:58,079 --> 00:42:00,160
list inside
1154
00:42:00,160 --> 00:42:02,960
a div so remember when we put one
1155
00:42:02,960 --> 00:42:05,280
element inside another element this is
1156
00:42:05,280 --> 00:42:07,280
called nesting so what we're going to do
1157
00:42:07,280 --> 00:42:09,760
now is we're going to put our list our
1158
00:42:09,760 --> 00:42:12,640
to-do list inside this div we're going
1159
00:42:12,640 --> 00:42:14,960
to nest it inside this div so that we
1160
00:42:14,960 --> 00:42:17,359
can use that command to wipe out
1161
00:42:17,359 --> 00:42:19,200
everything in this div but still leave
1162
00:42:19,200 --> 00:42:22,319
everything else untouched
1163
00:42:22,319 --> 00:42:24,319
so how do we render our to-do list
1164
00:42:24,319 --> 00:42:25,920
inside this div
1165
00:42:25,920 --> 00:42:27,599
well you'll notice here in our render
1166
00:42:27,599 --> 00:42:28,640
function
1167
00:42:28,640 --> 00:42:30,560
that we're creating elements and we're
1168
00:42:30,560 --> 00:42:33,119
attaching them to the body
1169
00:42:33,119 --> 00:42:34,800
so instead of the body we want to attach
1170
00:42:34,800 --> 00:42:36,880
the elements to the div so how do we do
1171
00:42:36,880 --> 00:42:38,880
that so while we do it in the same way
1172
00:42:38,880 --> 00:42:41,520
that we access our text box by using the
1173
00:42:41,520 --> 00:42:44,000
id attribute again
1174
00:42:44,000 --> 00:42:45,839
you'll notice that id is really
1175
00:42:45,839 --> 00:42:48,000
important in web development
1176
00:42:48,000 --> 00:42:50,880
so let's give it an idea of to do dash
1177
00:42:50,880 --> 00:42:53,040
list
1178
00:42:53,040 --> 00:42:55,599
and down here instead of adding elements
1179
00:42:55,599 --> 00:42:58,079
to the body we're going to add elements
1180
00:42:58,079 --> 00:43:01,599
here so remember we can grab our to-do
1181
00:43:01,599 --> 00:43:03,200
list
1182
00:43:03,200 --> 00:43:07,160
by using the document.getelementbyid
1183
00:43:07,520 --> 00:43:12,599
so now you want to get the id of this
1184
00:43:14,079 --> 00:43:15,760
and now instead of documenting our body
1185
00:43:15,760 --> 00:43:18,640
we're going to do to do list
1186
00:43:18,640 --> 00:43:21,599
dot append child
1187
00:43:21,920 --> 00:43:24,160
so we're going to start rendering inside
1188
00:43:24,160 --> 00:43:27,359
here instead of inside the body so let's
1189
00:43:27,359 --> 00:43:29,920
go back to our web page and refresh
1190
00:43:29,920 --> 00:43:32,319
go to the elements tab to see our html
1191
00:43:32,319 --> 00:43:34,319
structure and you'll notice that we now
1192
00:43:34,319 --> 00:43:37,839
have this div id equals to-do list and
1193
00:43:37,839 --> 00:43:40,160
inside this div we've rendered our to-do
1194
00:43:40,160 --> 00:43:41,680
list
1195
00:43:41,680 --> 00:43:43,599
so now the next step is that before we
1196
00:43:43,599 --> 00:43:45,359
re-render the list we want to get rid of
1197
00:43:45,359 --> 00:43:46,960
it we're going to wipe this out so we
1198
00:43:46,960 --> 00:43:48,960
already know the command for that
1199
00:43:48,960 --> 00:43:53,319
so remember that document.body.interhtml
1200
00:43:55,760 --> 00:43:57,359
will reset the body
1201
00:43:57,359 --> 00:43:59,359
now let's go in
1202
00:43:59,359 --> 00:44:02,960
and reset our list so document dot get
1203
00:44:02,960 --> 00:44:05,920
element by id
1204
00:44:06,640 --> 00:44:09,119
dot inner html
1205
00:44:09,119 --> 00:44:10,640
and this is going to
1206
00:44:10,640 --> 00:44:12,880
wipe out a list and replace it with an
1207
00:44:12,880 --> 00:44:15,440
empty string
1208
00:44:15,520 --> 00:44:17,440
so now let's go back into our web page
1209
00:44:17,440 --> 00:44:18,640
refresh
1210
00:44:18,640 --> 00:44:22,400
i'm going to add another to do
1211
00:44:22,400 --> 00:44:24,640
and there you go we're not going to be
1212
00:44:24,640 --> 00:44:26,400
copying this list over we're just going
1213
00:44:26,400 --> 00:44:29,280
to be adding something to the end of it
1214
00:44:29,280 --> 00:44:30,960
so what's happening here is that when we
1215
00:44:30,960 --> 00:44:33,359
click this add to do button
1216
00:44:33,359 --> 00:44:34,960
it's going to run this function which
1217
00:44:34,960 --> 00:44:36,480
will add
1218
00:44:36,480 --> 00:44:39,119
whatever's in the text box to our array
1219
00:44:39,119 --> 00:44:40,640
and then we're going to call render
1220
00:44:40,640 --> 00:44:42,079
which will
1221
00:44:42,079 --> 00:44:43,760
reset our list
1222
00:44:43,760 --> 00:44:46,400
and then it will re-render the updated
1223
00:44:46,400 --> 00:44:47,440
list
1224
00:44:47,440 --> 00:44:49,040
so before we continue i'm going to give
1225
00:44:49,040 --> 00:44:51,040
another syntax lesson there's actually
1226
00:44:51,040 --> 00:44:52,960
three ways to create a variable in
1227
00:44:52,960 --> 00:44:54,319
javascript
1228
00:44:54,319 --> 00:44:55,200
let
1229
00:44:55,200 --> 00:44:56,960
const and var
1230
00:44:56,960 --> 00:44:58,880
so let creates a variable that can be
1231
00:44:58,880 --> 00:45:01,119
reassigned for example you can say let
1232
00:45:01,119 --> 00:45:04,000
num equals 5 and then you can reassign
1233
00:45:04,000 --> 00:45:06,319
the variable to another value so num
1234
00:45:06,319 --> 00:45:07,920
equals 6.
1235
00:45:07,920 --> 00:45:09,280
const on the other hand creates a
1236
00:45:09,280 --> 00:45:11,599
variable that can't be reassigned it
1237
00:45:11,599 --> 00:45:13,599
stands for constant so when you set the
1238
00:45:13,599 --> 00:45:15,280
value of the variable you can't change
1239
00:45:15,280 --> 00:45:16,400
it anymore
1240
00:45:16,400 --> 00:45:18,560
and var is an old way of defining a
1241
00:45:18,560 --> 00:45:20,160
variable from an older version of
1242
00:45:20,160 --> 00:45:22,160
javascript and we generally don't use it
1243
00:45:22,160 --> 00:45:23,599
anymore
1244
00:45:23,599 --> 00:45:25,680
so let's go back into our code and we
1245
00:45:25,680 --> 00:45:27,680
notice that for all of our variables
1246
00:45:27,680 --> 00:45:30,079
we're not really reassigning them
1247
00:45:30,079 --> 00:45:32,079
so let's follow our best practice and
1248
00:45:32,079 --> 00:45:36,319
change these let's to const
1249
00:45:39,280 --> 00:45:41,200
generally you always want to use const
1250
00:45:41,200 --> 00:45:43,839
except let's say if you want to assign
1251
00:45:43,839 --> 00:45:47,040
this element to something else
1252
00:45:47,040 --> 00:45:48,560
then you have no choice but to change
1253
00:45:48,560 --> 00:45:52,200
this to a let
1254
00:46:02,160 --> 00:46:03,839
let's learn more about javascript by
1255
00:46:03,839 --> 00:46:05,760
adding another feature we're going to
1256
00:46:05,760 --> 00:46:08,800
add a due date to our to-do list
1257
00:46:08,800 --> 00:46:10,640
so the first thing we'll need is a date
1258
00:46:10,640 --> 00:46:12,960
picker and the html code to create a
1259
00:46:12,960 --> 00:46:16,240
date picker is this
1260
00:46:16,640 --> 00:46:17,760
input
1261
00:46:17,760 --> 00:46:21,440
type equals date
1262
00:46:21,520 --> 00:46:23,119
so you notice that it's another input
1263
00:46:23,119 --> 00:46:25,359
element except the type attribute now is
1264
00:46:25,359 --> 00:46:27,599
set to date which would change the
1265
00:46:27,599 --> 00:46:29,760
behavior and appearance of this element
1266
00:46:29,760 --> 00:46:32,079
to be a date picker so let's go back
1267
00:46:32,079 --> 00:46:34,400
into our web page and refresh
1268
00:46:34,400 --> 00:46:36,160
and now you'll see that we have a date
1269
00:46:36,160 --> 00:46:39,119
picker that is provided by the browser
1270
00:46:39,119 --> 00:46:40,960
i actually want this text box to be in
1271
00:46:40,960 --> 00:46:43,040
front of the date picker so we're just
1272
00:46:43,040 --> 00:46:45,280
going to move this
1273
00:46:45,280 --> 00:46:48,880
behind the text box
1274
00:46:49,520 --> 00:46:51,599
so now when we added to do we want to
1275
00:46:51,599 --> 00:46:54,319
associate a due date with it
1276
00:46:54,319 --> 00:46:56,000
so the first thing we need to do is to
1277
00:46:56,000 --> 00:46:58,000
grab the date that we selected in the
1278
00:46:58,000 --> 00:47:00,480
date picker so how do we grab the date
1279
00:47:00,480 --> 00:47:02,640
that we selected from the date picker
1280
00:47:02,640 --> 00:47:04,079
well we did something similar when we
1281
00:47:04,079 --> 00:47:06,640
grabbed the value from the text box
1282
00:47:06,640 --> 00:47:07,760
and we're going to do the same thing
1283
00:47:07,760 --> 00:47:10,720
here we're going to add an id
1284
00:47:10,720 --> 00:47:12,560
to the date picker let's call it the
1285
00:47:12,560 --> 00:47:14,560
date picker
1286
00:47:14,560 --> 00:47:16,560
and we're going to grab this element in
1287
00:47:16,560 --> 00:47:18,079
javascript and we're going to get the
1288
00:47:18,079 --> 00:47:19,920
value of this element
1289
00:47:19,920 --> 00:47:22,079
so let's go back down here
1290
00:47:22,079 --> 00:47:23,599
and here we're going to
1291
00:47:23,599 --> 00:47:25,920
create a variable to store our date
1292
00:47:25,920 --> 00:47:28,880
picker let's call it a date picker
1293
00:47:28,880 --> 00:47:30,720
and we're going to
1294
00:47:30,720 --> 00:47:32,079
get
1295
00:47:32,079 --> 00:47:34,400
an element by id and the id we're going
1296
00:47:34,400 --> 00:47:36,839
to use is the date picker
1297
00:47:36,839 --> 00:47:40,319
id so now this code will grab the date
1298
00:47:40,319 --> 00:47:43,520
picker from html and store it in this
1299
00:47:43,520 --> 00:47:44,960
variable
1300
00:47:44,960 --> 00:47:46,960
so now let's get the date from the date
1301
00:47:46,960 --> 00:47:49,359
picker so we're going to do
1302
00:47:49,359 --> 00:47:53,040
due date equals date picker
1303
00:47:53,040 --> 00:47:54,800
so now we have the title
1304
00:47:54,800 --> 00:47:58,000
and the due date for the to do
1305
00:47:58,000 --> 00:47:59,520
but now the question is how do we
1306
00:47:59,520 --> 00:48:02,720
associate a due date with each to-do
1307
00:48:02,720 --> 00:48:04,319
and for that we'll need to learn about
1308
00:48:04,319 --> 00:48:07,440
the object type in javascript so we
1309
00:48:07,440 --> 00:48:09,839
learned about strings and array types in
1310
00:48:09,839 --> 00:48:11,359
this course we're going to learn about
1311
00:48:11,359 --> 00:48:13,359
the third type in javascript called
1312
00:48:13,359 --> 00:48:16,240
objects so what are objects objects
1313
00:48:16,240 --> 00:48:18,160
basically group different values that
1314
00:48:18,160 --> 00:48:20,480
are related together this was what an
1315
00:48:20,480 --> 00:48:23,040
object looks like so the syntax of an
1316
00:48:23,040 --> 00:48:24,800
object is that you start with a curly
1317
00:48:24,800 --> 00:48:26,640
brace and you end with a curly brace and
1318
00:48:26,640 --> 00:48:28,800
everything inside is part of the object
1319
00:48:28,800 --> 00:48:30,400
so we're going to group two related
1320
00:48:30,400 --> 00:48:32,960
values together and for each value we're
1321
00:48:32,960 --> 00:48:34,640
going to give it a label so for this
1322
00:48:34,640 --> 00:48:36,720
value we're going to call it the title
1323
00:48:36,720 --> 00:48:38,240
and for this value it's going to be
1324
00:48:38,240 --> 00:48:40,400
called the due date and this is the
1325
00:48:40,400 --> 00:48:43,520
syntax for these labels so these labels
1326
00:48:43,520 --> 00:48:46,720
are actually called property value pairs
1327
00:48:46,720 --> 00:48:49,520
and these pairs are separated by a comma
1328
00:48:49,520 --> 00:48:52,960
so that's the syntax of an object
1329
00:48:52,960 --> 00:48:55,040
so why do we have these properly valued
1330
00:48:55,040 --> 00:48:58,559
pairs it lets us access specific values
1331
00:48:58,559 --> 00:49:01,119
within the object so if we just wanted
1332
00:49:01,119 --> 00:49:03,520
the title of this to do we can do to do
1333
00:49:03,520 --> 00:49:06,480
dot title and just get the title if we
1334
00:49:06,480 --> 00:49:08,640
wanted the due date we can do to do dot
1335
00:49:08,640 --> 00:49:11,119
due date and that will give us the due
1336
00:49:11,119 --> 00:49:12,160
date
1337
00:49:12,160 --> 00:49:13,760
so let's go back in our code and get
1338
00:49:13,760 --> 00:49:16,559
some more practice with objects so we're
1339
00:49:16,559 --> 00:49:19,280
going to go here and convert our to-do's
1340
00:49:19,280 --> 00:49:21,920
from strings into objects and the way
1341
00:49:21,920 --> 00:49:24,240
that we do that remember the syntax is
1342
00:49:24,240 --> 00:49:25,920
that we're going to use these curly
1343
00:49:25,920 --> 00:49:28,240
braces to say this is an object
1344
00:49:28,240 --> 00:49:31,280
and we're going to put the value here
1345
00:49:31,280 --> 00:49:32,800
and we're going to set the property or
1346
00:49:32,800 --> 00:49:35,040
the label of this value to be the title
1347
00:49:35,040 --> 00:49:37,280
so this is the value that represents the
1348
00:49:37,280 --> 00:49:38,480
title
1349
00:49:38,480 --> 00:49:40,720
and now we're going to add a value for
1350
00:49:40,720 --> 00:49:42,640
the due date and this can be anything
1351
00:49:42,640 --> 00:49:44,400
you want so i'm just going to put some
1352
00:49:44,400 --> 00:49:46,240
random value
1353
00:49:46,240 --> 00:49:47,440
here
1354
00:49:47,440 --> 00:49:48,960
so that looks pretty good now let's
1355
00:49:48,960 --> 00:49:50,720
convert the rest of them
1356
00:49:50,720 --> 00:49:54,400
to objects so the title
1357
00:49:57,920 --> 00:50:01,559
and the due dates
1358
00:50:17,920 --> 00:50:19,119
so you have to make sure that these
1359
00:50:19,119 --> 00:50:21,599
properties are the same for each object
1360
00:50:21,599 --> 00:50:23,760
otherwise if you access the title and
1361
00:50:23,760 --> 00:50:26,319
this one's called title with two e's
1362
00:50:26,319 --> 00:50:27,680
then you're not going to get the same
1363
00:50:27,680 --> 00:50:29,680
results so we're just going to make sure
1364
00:50:29,680 --> 00:50:32,240
that title and due date are the same for
1365
00:50:32,240 --> 00:50:33,760
each object
1366
00:50:33,760 --> 00:50:35,200
so the next thing we want to do is that
1367
00:50:35,200 --> 00:50:37,920
when we create our to-do's we also want
1368
00:50:37,920 --> 00:50:40,880
to add an object to this array instead
1369
00:50:40,880 --> 00:50:43,839
of a string which we have here
1370
00:50:43,839 --> 00:50:46,640
so to do that we're going to
1371
00:50:46,640 --> 00:50:47,760
get rid of this and we're going to
1372
00:50:47,760 --> 00:50:50,000
create an object and we're also going to
1373
00:50:50,000 --> 00:50:52,160
have a title
1374
00:50:52,160 --> 00:50:54,480
property and the value is going to be
1375
00:50:54,480 --> 00:50:56,079
the title here
1376
00:50:56,079 --> 00:50:58,720
and the due date property and we're just
1377
00:50:58,720 --> 00:51:01,280
going to use this value here
1378
00:51:01,280 --> 00:51:03,119
so that's all we need so the last thing
1379
00:51:03,119 --> 00:51:04,960
that we need to do here is that in our
1380
00:51:04,960 --> 00:51:06,559
render function you'll notice that we're
1381
00:51:06,559 --> 00:51:08,880
still assuming each to do
1382
00:51:08,880 --> 00:51:10,640
is just a title but that's not true
1383
00:51:10,640 --> 00:51:13,200
anymore it's an object so we're going to
1384
00:51:13,200 --> 00:51:16,160
convert it into an object
1385
00:51:16,160 --> 00:51:18,079
and instead of using to do title we're
1386
00:51:18,079 --> 00:51:19,920
going to access the title inside the
1387
00:51:19,920 --> 00:51:22,160
object so we're going to use to do dot
1388
00:51:22,160 --> 00:51:24,240
title
1389
00:51:24,240 --> 00:51:26,319
and now let's go back and refresh our
1390
00:51:26,319 --> 00:51:27,520
page
1391
00:51:27,520 --> 00:51:29,599
just going to add another to do
1392
00:51:29,599 --> 00:51:31,440
pick a date
1393
00:51:31,440 --> 00:51:32,720
and you'll notice that we're adding to
1394
00:51:32,720 --> 00:51:34,480
the end of the list but we're not really
1395
00:51:34,480 --> 00:51:36,960
rendering the due date here because in
1396
00:51:36,960 --> 00:51:39,040
our code you'll notice that we don't
1397
00:51:39,040 --> 00:51:40,400
really do anything with the due date
1398
00:51:40,400 --> 00:51:42,640
we're just using the title so let's deal
1399
00:51:42,640 --> 00:51:44,400
with that next
1400
00:51:44,400 --> 00:51:47,359
and i'm just going to
1401
00:51:47,359 --> 00:51:49,920
add the to due date after the title just
1402
00:51:49,920 --> 00:51:52,000
to make it simple so we're going to add
1403
00:51:52,000 --> 00:51:54,240
a space in between them and we're going
1404
00:51:54,240 --> 00:51:56,960
to add the to do
1405
00:51:56,960 --> 00:51:58,960
due date
1406
00:51:58,960 --> 00:52:00,319
so with strings you can add them
1407
00:52:00,319 --> 00:52:02,720
together and it will combine the string
1408
00:52:02,720 --> 00:52:05,839
so let's refresh and we can see that our
1409
00:52:05,839 --> 00:52:08,079
due dates are appearing beside our to-do
1410
00:52:08,079 --> 00:52:09,440
list and don't worry we're going to make
1411
00:52:09,440 --> 00:52:13,400
this look better later
1412
00:52:22,240 --> 00:52:24,319
so now that we learned about objects we
1413
00:52:24,319 --> 00:52:26,240
can add the next functionality to our
1414
00:52:26,240 --> 00:52:28,079
to-do list which is the ability to
1415
00:52:28,079 --> 00:52:30,720
delete items from the list so the first
1416
00:52:30,720 --> 00:52:32,880
thing we need to do is to add a delete
1417
00:52:32,880 --> 00:52:34,800
button beside each of these and the way
1418
00:52:34,800 --> 00:52:36,319
to do that is we're going to go back
1419
00:52:36,319 --> 00:52:38,640
into our code
1420
00:52:38,640 --> 00:52:39,839
and
1421
00:52:39,839 --> 00:52:42,319
beside this we're going to add a new
1422
00:52:42,319 --> 00:52:44,079
button so let's
1423
00:52:44,079 --> 00:52:46,960
create a button
1424
00:52:46,960 --> 00:52:49,680
document dot create element
1425
00:52:49,680 --> 00:52:50,960
and we're going to create a button this
1426
00:52:50,960 --> 00:52:53,200
time
1427
00:52:53,839 --> 00:52:55,680
let's call it the delete button and
1428
00:52:55,680 --> 00:52:58,839
we're going to set the text inside the
1429
00:52:58,839 --> 00:53:01,440
button to be
1430
00:53:01,440 --> 00:53:03,680
delete so we know what it does what it
1431
00:53:03,680 --> 00:53:05,200
does
1432
00:53:05,200 --> 00:53:06,319
and we're going to add this delete
1433
00:53:06,319 --> 00:53:08,319
button to each of these elements so
1434
00:53:08,319 --> 00:53:10,640
we're going to take element dot append
1435
00:53:10,640 --> 00:53:12,800
child
1436
00:53:12,800 --> 00:53:15,599
delete button
1437
00:53:15,599 --> 00:53:18,480
so basically this element here
1438
00:53:18,480 --> 00:53:20,880
represents this div and we're just going
1439
00:53:20,880 --> 00:53:22,559
to add a button to the end of it that's
1440
00:53:22,559 --> 00:53:24,160
what that code does
1441
00:53:24,160 --> 00:53:25,040
let's
1442
00:53:25,040 --> 00:53:26,800
rerun the code and see that we have
1443
00:53:26,800 --> 00:53:28,559
these delete buttons here you'll notice
1444
00:53:28,559 --> 00:53:30,000
that the delete buttons are a little too
1445
00:53:30,000 --> 00:53:31,520
close to the date and we're going to fix
1446
00:53:31,520 --> 00:53:32,880
that next
1447
00:53:32,880 --> 00:53:34,400
and to do this i'm going to introduce
1448
00:53:34,400 --> 00:53:36,880
another very important attribute in html
1449
00:53:36,880 --> 00:53:39,760
called the style attribute and this time
1450
00:53:39,760 --> 00:53:42,000
instead of adding it to html here
1451
00:53:42,000 --> 00:53:44,240
because the list doesn't exist yet we're
1452
00:53:44,240 --> 00:53:47,280
going to just add it in javascript
1453
00:53:47,280 --> 00:53:49,200
and to do that we're going to do
1454
00:53:49,200 --> 00:53:53,040
delete button dot style
1455
00:53:53,040 --> 00:53:54,960
equals
1456
00:53:54,960 --> 00:53:57,040
and we want to add some spacing on the
1457
00:53:57,040 --> 00:54:00,480
left side so we're going to do margin
1458
00:54:00,480 --> 00:54:02,480
dash left
1459
00:54:02,480 --> 00:54:04,640
and we're going to set it to maybe 12
1460
00:54:04,640 --> 00:54:06,640
pixels
1461
00:54:06,640 --> 00:54:08,640
and let's go back and refresh the page
1462
00:54:08,640 --> 00:54:10,880
so you'll notice that we added 12 pixels
1463
00:54:10,880 --> 00:54:12,319
of margin
1464
00:54:12,319 --> 00:54:14,079
to our buttons so they're a little bit
1465
00:54:14,079 --> 00:54:17,280
more spaced apart now if we go into our
1466
00:54:17,280 --> 00:54:19,520
list and look at our buttons you'll see
1467
00:54:19,520 --> 00:54:22,000
that when we set this property in
1468
00:54:22,000 --> 00:54:23,359
javascript
1469
00:54:23,359 --> 00:54:25,119
the corresponding
1470
00:54:25,119 --> 00:54:27,839
attribute is set in html
1471
00:54:27,839 --> 00:54:29,760
so you'll notice that we're manipulating
1472
00:54:29,760 --> 00:54:32,800
html with javascript pretty often so
1473
00:54:32,800 --> 00:54:35,119
here we're setting the style of the html
1474
00:54:35,119 --> 00:54:37,440
element to this we can set the text of
1475
00:54:37,440 --> 00:54:40,079
the html element we can grab the value
1476
00:54:40,079 --> 00:54:42,640
inside the html element
1477
00:54:42,640 --> 00:54:44,880
and this feature is called the document
1478
00:54:44,880 --> 00:54:47,440
object model or the dom and this is one
1479
00:54:47,440 --> 00:54:48,880
of the most important features of
1480
00:54:48,880 --> 00:54:51,119
javascript basically you can grab stuff
1481
00:54:51,119 --> 00:54:52,400
from html
1482
00:54:52,400 --> 00:54:54,480
and that gets converted into a
1483
00:54:54,480 --> 00:54:56,880
javascript object so that you can use it
1484
00:54:56,880 --> 00:54:59,920
seamlessly in javascript
1485
00:54:59,920 --> 00:55:01,599
now another thing i want to note is that
1486
00:55:01,599 --> 00:55:03,839
the style attribute here
1487
00:55:03,839 --> 00:55:06,559
this value is another language called
1488
00:55:06,559 --> 00:55:09,839
css and the css syntax is sort of like
1489
00:55:09,839 --> 00:55:12,160
this you have a style name and then you
1490
00:55:12,160 --> 00:55:14,559
have the value of the style you can have
1491
00:55:14,559 --> 00:55:17,119
another style and another value for that
1492
00:55:17,119 --> 00:55:18,960
style and you can add different styles
1493
00:55:18,960 --> 00:55:21,040
like this together
1494
00:55:21,040 --> 00:55:22,960
so if you want to learn more about html
1495
00:55:22,960 --> 00:55:24,960
or css i can do separate videos on that
1496
00:55:24,960 --> 00:55:27,359
just let me know in the comments below
1497
00:55:27,359 --> 00:55:29,440
okay so now we have our delete button
1498
00:55:29,440 --> 00:55:31,200
how do we hook it up to a function so
1499
00:55:31,200 --> 00:55:33,359
that something happens when we click our
1500
00:55:33,359 --> 00:55:34,400
delete button
1501
00:55:34,400 --> 00:55:36,720
so remember our on click
1502
00:55:36,720 --> 00:55:38,799
attribute here we're going to do the
1503
00:55:38,799 --> 00:55:40,799
same thing except we're going to do it
1504
00:55:40,799 --> 00:55:41,760
here
1505
00:55:41,760 --> 00:55:44,240
in javascript
1506
00:55:44,240 --> 00:55:45,599
so we're going to use the document
1507
00:55:45,599 --> 00:55:48,480
object model or dom features again
1508
00:55:48,480 --> 00:55:50,160
and we're going to use this delete
1509
00:55:50,160 --> 00:55:52,640
button object and we're going to set the
1510
00:55:52,640 --> 00:55:54,799
on click
1511
00:55:54,799 --> 00:55:57,839
property to a function so let's create a
1512
00:55:57,839 --> 00:55:59,680
function right now actually let's create
1513
00:55:59,680 --> 00:56:04,400
a function called delete to do
1514
00:56:06,079 --> 00:56:07,760
and we're going to set the value of on
1515
00:56:07,760 --> 00:56:11,200
click to this function
1516
00:56:11,440 --> 00:56:13,040
so you'll notice it's a little different
1517
00:56:13,040 --> 00:56:15,599
from how we wrote it up here the value
1518
00:56:15,599 --> 00:56:17,119
of this attribute is actually some piece
1519
00:56:17,119 --> 00:56:19,040
of code that we want to run and here
1520
00:56:19,040 --> 00:56:20,960
we're giving it a
1521
00:56:20,960 --> 00:56:22,640
the name of the function rather than
1522
00:56:22,640 --> 00:56:24,720
running the actual function so that's
1523
00:56:24,720 --> 00:56:26,240
just a little difference between setting
1524
00:56:26,240 --> 00:56:28,400
attributes in html versus setting
1525
00:56:28,400 --> 00:56:30,799
attributes using the dom
1526
00:56:30,799 --> 00:56:32,799
so in our to do function let's
1527
00:56:32,799 --> 00:56:34,640
console.log a message
1528
00:56:34,640 --> 00:56:36,960
just so that we know
1529
00:56:36,960 --> 00:56:39,040
it's doing something
1530
00:56:39,040 --> 00:56:40,319
let's go back
1531
00:56:40,319 --> 00:56:42,000
and we're going to go into the console
1532
00:56:42,000 --> 00:56:43,760
tab to see our messages and just going
1533
00:56:43,760 --> 00:56:45,839
to click delete a few times
1534
00:56:45,839 --> 00:56:47,359
so now that we see that when we're
1535
00:56:47,359 --> 00:56:48,720
clicking this button
1536
00:56:48,720 --> 00:56:51,359
it's clearly running this function
1537
00:56:51,359 --> 00:56:55,200
which logs this message
1538
00:56:56,079 --> 00:56:58,000
so now let's write the actual code for
1539
00:56:58,000 --> 00:56:59,920
our delete to do function so let's get
1540
00:56:59,920 --> 00:57:02,160
rid of this first
1541
00:57:02,160 --> 00:57:03,520
and there's actually two things that we
1542
00:57:03,520 --> 00:57:05,920
need to do so when we click this delete
1543
00:57:05,920 --> 00:57:08,319
button we want to get rid of this line
1544
00:57:08,319 --> 00:57:09,839
from the web page
1545
00:57:09,839 --> 00:57:12,000
and we also want to go into this list
1546
00:57:12,000 --> 00:57:15,760
and remove the associated object
1547
00:57:15,760 --> 00:57:17,359
and it's actually a little easier than
1548
00:57:17,359 --> 00:57:19,280
you think because remember that our
1549
00:57:19,280 --> 00:57:21,440
render function will take whatever's
1550
00:57:21,440 --> 00:57:23,599
inside this to do and render it on the
1551
00:57:23,599 --> 00:57:24,640
web page
1552
00:57:24,640 --> 00:57:26,400
so as long as we remove
1553
00:57:26,400 --> 00:57:28,720
the correct to do from this list and
1554
00:57:28,720 --> 00:57:31,839
call render then everything will be fine
1555
00:57:31,839 --> 00:57:33,200
and we don't need to worry about
1556
00:57:33,200 --> 00:57:36,160
removing things from the web page
1557
00:57:36,160 --> 00:57:38,000
so when we click this delete button we
1558
00:57:38,000 --> 00:57:40,240
need a way to identify that this button
1559
00:57:40,240 --> 00:57:43,040
belongs to this to do so remove this one
1560
00:57:43,040 --> 00:57:44,720
and we would click this button we need a
1561
00:57:44,720 --> 00:57:46,559
way to identify that this button belongs
1562
00:57:46,559 --> 00:57:49,599
to the third to do so how do we do that
1563
00:57:49,599 --> 00:57:51,040
well one way we can do it using
1564
00:57:51,040 --> 00:57:53,280
everything that we've learned so far
1565
00:57:53,280 --> 00:57:56,079
is we can create an id and we can attach
1566
00:57:56,079 --> 00:57:58,559
the id to the
1567
00:57:58,559 --> 00:58:00,319
to do object here
1568
00:58:00,319 --> 00:58:02,240
and we can attach it to the delete
1569
00:58:02,240 --> 00:58:05,040
button so that serves as a link between
1570
00:58:05,040 --> 00:58:06,559
the two
1571
00:58:06,559 --> 00:58:08,480
so when we create these to do so let's
1572
00:58:08,480 --> 00:58:10,799
create an id
1573
00:58:10,799 --> 00:58:13,359
so let's just add a dummy id let's call
1574
00:58:13,359 --> 00:58:15,119
this id 1
1575
00:58:15,119 --> 00:58:17,200
and id
1576
00:58:17,200 --> 00:58:20,200
2.
1577
00:58:22,799 --> 00:58:24,319
so just make sure that these ids are
1578
00:58:24,319 --> 00:58:26,400
unique otherwise you won't know which
1579
00:58:26,400 --> 00:58:28,000
one to select
1580
00:58:28,000 --> 00:58:30,000
and now when we're creating a to-do we
1581
00:58:30,000 --> 00:58:32,480
also need to add an id so let's create
1582
00:58:32,480 --> 00:58:33,760
one here
1583
00:58:33,760 --> 00:58:36,400
const id and
1584
00:58:36,400 --> 00:58:38,480
a cool way to create an id in javascript
1585
00:58:38,480 --> 00:58:40,000
is just to use
1586
00:58:40,000 --> 00:58:42,559
the date
1587
00:58:42,559 --> 00:58:45,520
so we're going to type new date dot
1588
00:58:45,520 --> 00:58:47,680
get time
1589
00:58:47,680 --> 00:58:50,079
so what this code does is it gets the
1590
00:58:50,079 --> 00:58:51,680
current time
1591
00:58:51,680 --> 00:58:53,599
in milliseconds
1592
00:58:53,599 --> 00:58:55,760
and so we can actually run it here
1593
00:58:55,760 --> 00:58:58,960
new date dot get time
1594
00:58:58,960 --> 00:59:01,119
so it returns a number and the next time
1595
00:59:01,119 --> 00:59:02,240
you run it the number is going to be
1596
00:59:02,240 --> 00:59:03,839
different so that's pretty good it's
1597
00:59:03,839 --> 00:59:06,480
good enough for our project right now to
1598
00:59:06,480 --> 00:59:08,400
serve as an id
1599
00:59:08,400 --> 00:59:09,440
so that's what we're going to do we're
1600
00:59:09,440 --> 00:59:12,079
going to set the id of this
1601
00:59:12,079 --> 00:59:15,040
new to do object to be this id that we
1602
00:59:15,040 --> 00:59:16,960
created
1603
00:59:16,960 --> 00:59:18,720
so now what we want to do is we're going
1604
00:59:18,720 --> 00:59:21,040
to go down to our button
1605
00:59:21,040 --> 00:59:23,440
and we're going to set the id on the
1606
00:59:23,440 --> 00:59:25,599
button so that
1607
00:59:25,599 --> 00:59:29,680
they're linked together so id equals
1608
00:59:29,680 --> 00:59:30,640
to do
1609
00:59:30,640 --> 00:59:33,359
remember that to-do's now have
1610
00:59:33,359 --> 00:59:35,520
an id attached to them so we can access
1611
00:59:35,520 --> 00:59:38,319
them through the dot notation so dot
1612
00:59:38,319 --> 00:59:40,000
id
1613
00:59:40,000 --> 00:59:43,440
and let's refresh the page
1614
00:59:43,599 --> 00:59:46,160
now let's go into elements and we're
1615
00:59:46,160 --> 00:59:48,079
going to go into one of these to do so
1616
00:59:48,079 --> 00:59:49,839
just go into the to-do list
1617
00:59:49,839 --> 00:59:51,920
and you'll see that the button now has
1618
00:59:51,920 --> 00:59:55,040
an id attached to it so if i add another
1619
00:59:55,040 --> 00:59:57,440
to-do
1620
00:59:59,440 --> 01:00:01,040
and we look at the
1621
01:00:01,040 --> 01:00:02,480
html
1622
01:00:02,480 --> 01:00:04,640
you'll notice that the button now has an
1623
01:00:04,640 --> 01:00:06,160
id attribute
1624
01:00:06,160 --> 01:00:08,960
and if you console.log
1625
01:00:08,960 --> 01:00:10,799
the to-do's
1626
01:00:10,799 --> 01:00:12,400
and you look at the last one you'll
1627
01:00:12,400 --> 01:00:17,920
notice that this id now matches this one
1628
01:00:17,920 --> 01:00:20,400
so let's go back into our code and use
1629
01:00:20,400 --> 01:00:21,920
the two together
1630
01:00:21,920 --> 01:00:24,160
so how do we get the id of the delete
1631
01:00:24,160 --> 01:00:26,160
button that was clicked
1632
01:00:26,160 --> 01:00:28,160
so when you click a button it runs this
1633
01:00:28,160 --> 01:00:30,400
on click function and the on click
1634
01:00:30,400 --> 01:00:33,760
function gets provided with a parameter
1635
01:00:33,760 --> 01:00:36,079
called event
1636
01:00:36,079 --> 01:00:37,920
and we're just going to log
1637
01:00:37,920 --> 01:00:40,559
the event to see what's inside so this
1638
01:00:40,559 --> 01:00:42,640
event is just an object like we've
1639
01:00:42,640 --> 01:00:44,000
learned
1640
01:00:44,000 --> 01:00:47,040
so let's go back to our web page refresh
1641
01:00:47,040 --> 01:00:48,480
and another
1642
01:00:48,480 --> 01:00:51,200
well you can just click this
1643
01:00:51,200 --> 01:00:54,160
look at the console so we can see that
1644
01:00:54,160 --> 01:00:56,960
we just logged a mouse event when we
1645
01:00:56,960 --> 01:00:59,680
clicked the delete button it provided an
1646
01:00:59,680 --> 01:01:02,079
event to our function and when we log
1647
01:01:02,079 --> 01:01:05,359
the event this is what we see
1648
01:01:05,359 --> 01:01:06,960
so the inside this event we can actually
1649
01:01:06,960 --> 01:01:09,280
get the button that was clicked
1650
01:01:09,280 --> 01:01:12,079
and the property for that is called the
1651
01:01:12,079 --> 01:01:14,000
target so if you hover over that you can
1652
01:01:14,000 --> 01:01:15,119
see that
1653
01:01:15,119 --> 01:01:17,119
this target refers to this button right
1654
01:01:17,119 --> 01:01:18,880
here
1655
01:01:18,880 --> 01:01:20,799
so let's go back into our code and use
1656
01:01:20,799 --> 01:01:23,040
that
1657
01:01:23,040 --> 01:01:25,760
so here we're going to create a variable
1658
01:01:25,760 --> 01:01:27,359
to get our button
1659
01:01:27,359 --> 01:01:30,160
so event dot target
1660
01:01:30,160 --> 01:01:32,960
and remember that we set the id here so
1661
01:01:32,960 --> 01:01:34,400
we're going to
1662
01:01:34,400 --> 01:01:36,640
get the id cons id
1663
01:01:36,640 --> 01:01:39,839
to delete
1664
01:01:40,480 --> 01:01:44,640
equals delete button dot id
1665
01:01:45,040 --> 01:01:47,520
and now we need some code to
1666
01:01:47,520 --> 01:01:49,520
remove the to do
1667
01:01:49,520 --> 01:01:54,880
in this list that has this particular id
1668
01:02:05,520 --> 01:02:07,920
so how do we find and remove values from
1669
01:02:07,920 --> 01:02:10,240
an array well one way to do this is to
1670
01:02:10,240 --> 01:02:12,720
use another array method so what we're
1671
01:02:12,720 --> 01:02:14,559
going to do is we're going to type out
1672
01:02:14,559 --> 01:02:16,319
our array and then we're going to use
1673
01:02:16,319 --> 01:02:17,920
the dot notation again to see what
1674
01:02:17,920 --> 01:02:20,240
methods are on the array so we've
1675
01:02:20,240 --> 01:02:22,400
already used the for each method this
1676
01:02:22,400 --> 01:02:23,760
time we're going to use the filter
1677
01:02:23,760 --> 01:02:26,960
method and we're going to call filter
1678
01:02:26,960 --> 01:02:29,039
so filter is very similar to the for
1679
01:02:29,039 --> 01:02:31,280
each method it's going to go through
1680
01:02:31,280 --> 01:02:32,880
this array and it's going to loop
1681
01:02:32,880 --> 01:02:34,640
through the array so what that means is
1682
01:02:34,640 --> 01:02:38,559
that it's going to run a function
1683
01:02:38,559 --> 01:02:41,839
so it's going to take this value
1684
01:02:41,839 --> 01:02:44,160
and put it in a parameter so let's name
1685
01:02:44,160 --> 01:02:46,160
that parameter to do and then it's going
1686
01:02:46,160 --> 01:02:47,599
to run this function
1687
01:02:47,599 --> 01:02:49,440
and then the second step is going to
1688
01:02:49,440 --> 01:02:51,119
take the second value
1689
01:02:51,119 --> 01:02:52,799
put it in this parameter and run the
1690
01:02:52,799 --> 01:02:54,960
function again and it's going to do this
1691
01:02:54,960 --> 01:02:57,440
for each value in the array so it's
1692
01:02:57,440 --> 01:02:59,200
going to loop through the array and run
1693
01:02:59,200 --> 01:03:01,280
this function
1694
01:03:01,280 --> 01:03:04,079
so before we learn how to use filter we
1695
01:03:04,079 --> 01:03:06,000
need to learn another concept called the
1696
01:03:06,000 --> 01:03:08,640
return value of a function
1697
01:03:08,640 --> 01:03:10,799
so if we go up to our previous code
1698
01:03:10,799 --> 01:03:13,039
you'll notice that this is a function
1699
01:03:13,039 --> 01:03:14,720
you can tell by the
1700
01:03:14,720 --> 01:03:16,640
round brackets this means that we are
1701
01:03:16,640 --> 01:03:18,640
calling this function
1702
01:03:18,640 --> 01:03:21,280
and this function actually gives us a
1703
01:03:21,280 --> 01:03:22,960
value so in this case we're getting an
1704
01:03:22,960 --> 01:03:26,160
html element when we run this code and
1705
01:03:26,160 --> 01:03:27,920
then we're going to save this html
1706
01:03:27,920 --> 01:03:29,920
element in this variable and then we
1707
01:03:29,920 --> 01:03:32,400
manipulate the variable
1708
01:03:32,400 --> 01:03:34,640
so the value that a function gives us is
1709
01:03:34,640 --> 01:03:36,400
called the return value
1710
01:03:36,400 --> 01:03:38,960
so this function in particular returns
1711
01:03:38,960 --> 01:03:40,960
an html element
1712
01:03:40,960 --> 01:03:42,799
on the other hand if we have this
1713
01:03:42,799 --> 01:03:45,119
function here gettime it returns
1714
01:03:45,119 --> 01:03:47,359
something different it returns a number
1715
01:03:47,359 --> 01:03:48,960
that represents the current time in
1716
01:03:48,960 --> 01:03:50,640
milliseconds
1717
01:03:50,640 --> 01:03:53,119
so what determines the return value of a
1718
01:03:53,119 --> 01:03:54,079
function
1719
01:03:54,079 --> 01:03:56,240
well a function returns whatever we tell
1720
01:03:56,240 --> 01:03:59,039
it to return and this is how it works so
1721
01:03:59,039 --> 01:04:02,400
let's just write an example function
1722
01:04:02,400 --> 01:04:04,319
and we'll just call it funk
1723
01:04:04,319 --> 01:04:06,960
and we're going to tell it to return a
1724
01:04:06,960 --> 01:04:08,559
hundred
1725
01:04:08,559 --> 01:04:10,559
so if we go back into our web page and
1726
01:04:10,559 --> 01:04:12,720
we refresh
1727
01:04:12,720 --> 01:04:15,119
and now we can run this function
1728
01:04:15,119 --> 01:04:16,480
so when we run this function you'll
1729
01:04:16,480 --> 01:04:19,680
notice that it returns a value of 100
1730
01:04:19,680 --> 01:04:21,839
since 100 is the return value of this
1731
01:04:21,839 --> 01:04:24,400
function we can actually save it in a
1732
01:04:24,400 --> 01:04:26,799
variable
1733
01:04:26,799 --> 01:04:29,039
so now if i console.log
1734
01:04:29,039 --> 01:04:30,720
the variable
1735
01:04:30,720 --> 01:04:32,880
you'll see that the variable has stored
1736
01:04:32,880 --> 01:04:35,760
the return value of the function
1737
01:04:35,760 --> 01:04:37,440
and if we go back to our code you'll
1738
01:04:37,440 --> 01:04:38,720
notice that we've been doing this all
1739
01:04:38,720 --> 01:04:40,400
the time we call a function
1740
01:04:40,400 --> 01:04:42,559
the function returns a value and then we
1741
01:04:42,559 --> 01:04:45,440
store it in a variable
1742
01:04:45,440 --> 01:04:48,559
so now if we change the return value to
1743
01:04:48,559 --> 01:04:49,760
100
1744
01:04:49,760 --> 01:04:51,039
the string
1745
01:04:51,039 --> 01:04:52,640
and we go back
1746
01:04:52,640 --> 01:04:53,520
and
1747
01:04:53,520 --> 01:04:56,480
we rerun our function
1748
01:04:56,480 --> 01:04:58,160
you'll notice that it now returns a
1749
01:04:58,160 --> 01:05:00,559
string so a function returns whatever we
1750
01:05:00,559 --> 01:05:02,079
tell it to return
1751
01:05:02,079 --> 01:05:03,599
so a couple other things that we need to
1752
01:05:03,599 --> 01:05:06,319
know about this return statement is that
1753
01:05:06,319 --> 01:05:09,200
any code that comes after the return
1754
01:05:09,200 --> 01:05:11,599
doesn't run anymore so
1755
01:05:11,599 --> 01:05:15,359
i'll just type some code here and if we
1756
01:05:15,359 --> 01:05:16,799
refresh the page
1757
01:05:16,799 --> 01:05:18,559
and run the function
1758
01:05:18,559 --> 01:05:21,039
you'll notice that we don't console log
1759
01:05:21,039 --> 01:05:23,599
anything so we don't run this code and
1760
01:05:23,599 --> 01:05:26,079
that's because when you return a value
1761
01:05:26,079 --> 01:05:28,319
the function finishes immediately we're
1762
01:05:28,319 --> 01:05:30,480
not going to run anything else after a
1763
01:05:30,480 --> 01:05:32,640
return
1764
01:05:32,640 --> 01:05:34,240
so now that we know what our return
1765
01:05:34,240 --> 01:05:36,880
value is let's go back to our filter
1766
01:05:36,880 --> 01:05:37,839
method
1767
01:05:37,839 --> 01:05:38,720
here
1768
01:05:38,720 --> 01:05:40,799
so the way the filter works is that it
1769
01:05:40,799 --> 01:05:43,760
will loop through this to-do's array so
1770
01:05:43,760 --> 01:05:46,319
it will take this value it will put it
1771
01:05:46,319 --> 01:05:48,559
inside this parameter and run the
1772
01:05:48,559 --> 01:05:50,799
function then it will take the second
1773
01:05:50,799 --> 01:05:52,640
value and run the function again and it
1774
01:05:52,640 --> 01:05:54,799
will take the third value and so on
1775
01:05:54,799 --> 01:05:57,839
and if this function returns true
1776
01:05:57,839 --> 01:06:00,319
then we will keep this to do inside the
1777
01:06:00,319 --> 01:06:01,359
array
1778
01:06:01,359 --> 01:06:03,599
if it returns false then we will take it
1779
01:06:03,599 --> 01:06:05,599
out of the array
1780
01:06:05,599 --> 01:06:08,079
so that's sort of what we want but first
1781
01:06:08,079 --> 01:06:11,359
what are these values of true and false
1782
01:06:11,359 --> 01:06:13,359
so we learned a few types of values in
1783
01:06:13,359 --> 01:06:15,599
javascript already like strings and
1784
01:06:15,599 --> 01:06:16,720
objects
1785
01:06:16,720 --> 01:06:18,559
this is another type of value in
1786
01:06:18,559 --> 01:06:21,200
javascript called a boolean value
1787
01:06:21,200 --> 01:06:23,760
so there are only two boolean values
1788
01:06:23,760 --> 01:06:25,359
true
1789
01:06:25,359 --> 01:06:26,880
or false
1790
01:06:26,880 --> 01:06:29,039
and we can verify this by going back
1791
01:06:29,039 --> 01:06:30,720
into our web page we're going to go into
1792
01:06:30,720 --> 01:06:33,200
the console and we're going to type
1793
01:06:33,200 --> 01:06:35,280
type of true
1794
01:06:35,280 --> 01:06:37,200
so what are boolean values and how are
1795
01:06:37,200 --> 01:06:38,319
they used
1796
01:06:38,319 --> 01:06:40,640
a boolean value basically represents
1797
01:06:40,640 --> 01:06:43,359
whether something is true or false
1798
01:06:43,359 --> 01:06:45,119
so for example
1799
01:06:45,119 --> 01:06:47,520
is one less than five
1800
01:06:47,520 --> 01:06:49,119
so we all know from common sense that
1801
01:06:49,119 --> 01:06:51,599
this is true and in javascript this will
1802
01:06:51,599 --> 01:06:53,920
result in the value of true
1803
01:06:53,920 --> 01:06:56,799
now if we write as one greater than five
1804
01:06:56,799 --> 01:06:58,400
we know that this is false so in
1805
01:06:58,400 --> 01:07:00,400
javascript this results in the boolean
1806
01:07:00,400 --> 01:07:02,720
value of false
1807
01:07:02,720 --> 01:07:04,480
now what you see here is called a
1808
01:07:04,480 --> 01:07:06,880
comparison operator you basically put it
1809
01:07:06,880 --> 01:07:08,720
between two values and it compares the
1810
01:07:08,720 --> 01:07:11,760
values and returns whether or not this
1811
01:07:11,760 --> 01:07:14,880
is true or false so here's a list of the
1812
01:07:14,880 --> 01:07:17,680
other comparison operators in javascript
1813
01:07:17,680 --> 01:07:20,000
so this one means is this value equal to
1814
01:07:20,000 --> 01:07:22,720
another value this one means is it not
1815
01:07:22,720 --> 01:07:24,960
equal to and this one means greater than
1816
01:07:24,960 --> 01:07:26,640
or equal to and you can pretty much
1817
01:07:26,640 --> 01:07:28,799
figure out what the others mean
1818
01:07:28,799 --> 01:07:31,760
so since these comparisons result in a
1819
01:07:31,760 --> 01:07:34,319
boolean value we can save it in a
1820
01:07:34,319 --> 01:07:36,240
variable just like any other value in
1821
01:07:36,240 --> 01:07:39,680
javascript so we can save it in const
1822
01:07:39,680 --> 01:07:41,520
is it true
1823
01:07:41,520 --> 01:07:44,559
equal one less than oops one less than
1824
01:07:44,559 --> 01:07:45,760
five
1825
01:07:45,760 --> 01:07:48,319
and console.log
1826
01:07:48,319 --> 01:07:49,920
hit true
1827
01:07:49,920 --> 01:07:52,480
so you'll notice that this results in
1828
01:07:52,480 --> 01:07:54,240
the boolean value of true and then we
1829
01:07:54,240 --> 01:07:57,839
saved it in this variable
1830
01:08:06,079 --> 01:08:08,319
now that we know what a boolean value is
1831
01:08:08,319 --> 01:08:10,799
let's go back to our filter method
1832
01:08:10,799 --> 01:08:14,160
so in filter if we return true then we
1833
01:08:14,160 --> 01:08:16,640
will keep this to do in the array if we
1834
01:08:16,640 --> 01:08:18,960
return false we'll remove this to do
1835
01:08:18,960 --> 01:08:21,359
from the array so remember we want to
1836
01:08:21,359 --> 01:08:25,279
remove the to do that has this id
1837
01:08:25,279 --> 01:08:27,679
that matches this id so let's write out
1838
01:08:27,679 --> 01:08:31,359
our logic in the comments first
1839
01:08:31,759 --> 01:08:32,799
so
1840
01:08:32,799 --> 01:08:37,279
if the id of this to do matches
1841
01:08:37,279 --> 01:08:39,279
id to delete
1842
01:08:39,279 --> 01:08:42,238
then we're going to return false because
1843
01:08:42,238 --> 01:08:44,880
we want to remove this from our to user
1844
01:08:44,880 --> 01:08:46,319
rate
1845
01:08:46,319 --> 01:08:48,960
for everything else
1846
01:08:48,960 --> 01:08:52,238
return true
1847
01:08:52,238 --> 01:08:54,158
so this is the logic that we want now
1848
01:08:54,158 --> 01:08:56,238
how do we convert this into code well
1849
01:08:56,238 --> 01:08:57,600
there's actually a javascript feature
1850
01:08:57,600 --> 01:08:59,359
that is perfect for this and that's
1851
01:08:59,359 --> 01:09:01,359
called the if statement so here's how
1852
01:09:01,359 --> 01:09:03,279
the if statement works we're going to
1853
01:09:03,279 --> 01:09:05,439
have the word if and between these
1854
01:09:05,439 --> 01:09:07,920
brackets is some code that evaluates to
1855
01:09:07,920 --> 01:09:09,600
true or false
1856
01:09:09,600 --> 01:09:11,600
now if it evaluates to true we're going
1857
01:09:11,600 --> 01:09:14,000
to run this code otherwise we're going
1858
01:09:14,000 --> 01:09:17,440
to run some other code
1859
01:09:19,359 --> 01:09:21,920
so let's go back into our function here
1860
01:09:21,920 --> 01:09:24,560
our example function and try out the if
1861
01:09:24,560 --> 01:09:25,600
statement
1862
01:09:25,600 --> 01:09:27,040
so first we're going to create some
1863
01:09:27,040 --> 01:09:30,158
variables to play around with so let num
1864
01:09:30,158 --> 01:09:34,238
one equals one and const num five equals
1865
01:09:34,238 --> 01:09:35,359
five
1866
01:09:35,359 --> 01:09:36,560
so now we're going to write our if
1867
01:09:36,560 --> 01:09:38,319
statement we're gonna go
1868
01:09:38,319 --> 01:09:39,839
if
1869
01:09:39,839 --> 01:09:42,158
and we're gonna write else
1870
01:09:42,158 --> 01:09:43,439
just like that
1871
01:09:43,439 --> 01:09:45,359
and inside these brackets we're gonna
1872
01:09:45,359 --> 01:09:47,198
have some code that returns true or
1873
01:09:47,198 --> 01:09:49,040
false
1874
01:09:49,040 --> 01:09:50,799
so let's do num
1875
01:09:50,799 --> 01:09:55,760
one is less than num five
1876
01:09:55,760 --> 01:09:57,120
and we're going to write some code
1877
01:09:57,120 --> 01:09:58,880
inside each of these
1878
01:09:58,880 --> 01:10:01,199
so each of these blocks so this one and
1879
01:10:01,199 --> 01:10:04,159
this one these are called branches
1880
01:10:04,159 --> 01:10:05,840
you can think of it as the code is
1881
01:10:05,840 --> 01:10:07,760
branching off so it can either go this
1882
01:10:07,760 --> 01:10:10,480
way or it can go the other way
1883
01:10:10,480 --> 01:10:12,800
so i'm going to run some code here
1884
01:10:12,800 --> 01:10:14,560
and we're going to run
1885
01:10:14,560 --> 01:10:17,840
some code here
1886
01:10:18,640 --> 01:10:21,520
so if we look inside these brackets
1887
01:10:21,520 --> 01:10:24,000
is one less than five well this is
1888
01:10:24,000 --> 01:10:26,960
definitely true one is less than five so
1889
01:10:26,960 --> 01:10:29,199
that means we're going to run this code
1890
01:10:29,199 --> 01:10:32,000
so now let's go back into our web page
1891
01:10:32,000 --> 01:10:34,080
we're going to refresh to reload that
1892
01:10:34,080 --> 01:10:35,040
function
1893
01:10:35,040 --> 01:10:38,480
and we're going to run this function
1894
01:10:38,480 --> 01:10:40,080
and you'll notice here
1895
01:10:40,080 --> 01:10:44,400
that we ran this line of code and that's
1896
01:10:44,400 --> 01:10:46,960
how if statements work if this is true
1897
01:10:46,960 --> 01:10:48,560
we're going to run this
1898
01:10:48,560 --> 01:10:51,040
otherwise we're going to run this
1899
01:10:51,040 --> 01:10:54,960
now if we flip this so is 1 greater than
1900
01:10:54,960 --> 01:10:57,280
5 we know this is false so it's going to
1901
01:10:57,280 --> 01:10:59,920
run the else statement so an if
1902
01:10:59,920 --> 01:11:01,679
statement can actually have several of
1903
01:11:01,679 --> 01:11:03,679
these tests like this and the way to add
1904
01:11:03,679 --> 01:11:06,000
more tests is just to add another if
1905
01:11:06,000 --> 01:11:07,040
statement
1906
01:11:07,040 --> 01:11:10,640
after the else statement
1907
01:11:10,960 --> 01:11:12,960
so here i move the else statement to the
1908
01:11:12,960 --> 01:11:15,199
bottom and i added another if statement
1909
01:11:15,199 --> 01:11:16,800
in between
1910
01:11:16,800 --> 01:11:18,400
so here we can have some other code that
1911
01:11:18,400 --> 01:11:20,560
returns true or false let's do something
1912
01:11:20,560 --> 01:11:24,320
like is num1 greater than 100
1913
01:11:24,320 --> 01:11:26,800
so we all know that one is not greater
1914
01:11:26,800 --> 01:11:28,960
than a hundred so this is going to
1915
01:11:28,960 --> 01:11:30,960
return false
1916
01:11:30,960 --> 01:11:34,080
and we're not going to run this code
1917
01:11:34,080 --> 01:11:39,560
now let's add another if statement test
1918
01:11:40,640 --> 01:11:42,239
and this one's going to be
1919
01:11:42,239 --> 01:11:45,199
is num1 greater than zero
1920
01:11:45,199 --> 01:11:47,440
so we all know
1921
01:11:47,440 --> 01:11:49,280
that
1922
01:11:49,280 --> 01:11:51,360
one is greater than zero
1923
01:11:51,360 --> 01:11:53,520
so we're going to run
1924
01:11:53,520 --> 01:11:55,120
this code
1925
01:11:55,120 --> 01:11:57,360
so let's go back into our web page and
1926
01:11:57,360 --> 01:11:58,800
we're going to refresh to load that
1927
01:11:58,800 --> 01:12:00,000
function again
1928
01:12:00,000 --> 01:12:02,239
and now if we run this function
1929
01:12:02,239 --> 01:12:03,760
you'll notice that
1930
01:12:03,760 --> 01:12:06,880
it is running this code
1931
01:12:06,880 --> 01:12:08,719
so the way that an if statement works is
1932
01:12:08,719 --> 01:12:10,480
that you can have multiple tests like
1933
01:12:10,480 --> 01:12:12,800
this it's going to go through this test
1934
01:12:12,800 --> 01:12:15,280
and then this test and then this test
1935
01:12:15,280 --> 01:12:18,320
so the first test that evaluates to true
1936
01:12:18,320 --> 01:12:20,239
we're going to run the code inside the
1937
01:12:20,239 --> 01:12:23,199
branch that is associated with the test
1938
01:12:23,199 --> 01:12:26,480
if all of these tests evaluate to false
1939
01:12:26,480 --> 01:12:28,400
then we're going to run the code
1940
01:12:28,400 --> 01:12:30,480
in the else branch
1941
01:12:30,480 --> 01:12:33,760
and that's how if statements work
1942
01:12:33,760 --> 01:12:35,600
so now that we learned if statements
1943
01:12:35,600 --> 01:12:38,239
let's go back to our filter method
1944
01:12:38,239 --> 01:12:41,600
and let's write out our logic here in
1945
01:12:41,600 --> 01:12:42,719
code
1946
01:12:42,719 --> 01:12:44,080
so let's take the first part of our
1947
01:12:44,080 --> 01:12:47,679
logic if the id of this to do matches
1948
01:12:47,679 --> 01:12:50,880
id to delete so we're going to type if
1949
01:12:50,880 --> 01:12:53,120
the to do dot id
1950
01:12:53,120 --> 01:12:54,880
and we're going to see if it matches
1951
01:12:54,880 --> 01:12:58,239
this id i'm going to say equals and this
1952
01:12:58,239 --> 01:12:59,840
is the
1953
01:12:59,840 --> 01:13:01,840
the comparison operator so it's going to
1954
01:13:01,840 --> 01:13:04,239
compare whether this value is equal to
1955
01:13:04,239 --> 01:13:08,320
this value which is id to delete
1956
01:13:08,320 --> 01:13:11,120
so if the id of the to do equals the id
1957
01:13:11,120 --> 01:13:13,199
we want to delete then we want to remove
1958
01:13:13,199 --> 01:13:14,560
it from the array so remember we're
1959
01:13:14,560 --> 01:13:17,120
going to return false
1960
01:13:17,120 --> 01:13:19,760
so we're going to return false
1961
01:13:19,760 --> 01:13:21,520
and for everything else so we're going
1962
01:13:21,520 --> 01:13:23,199
to just write else
1963
01:13:23,199 --> 01:13:24,880
we're going to return
1964
01:13:24,880 --> 01:13:27,120
true
1965
01:13:27,120 --> 01:13:29,199
so this is the logic for removing the to
1966
01:13:29,199 --> 01:13:33,360
do with this id from the array here
1967
01:13:33,360 --> 01:13:34,640
so one more thing about the filter
1968
01:13:34,640 --> 01:13:36,239
method is that it doesn't actually
1969
01:13:36,239 --> 01:13:38,159
modify our original array it actually
1970
01:13:38,159 --> 01:13:41,280
creates a copy of the array with this to
1971
01:13:41,280 --> 01:13:42,960
do removed
1972
01:13:42,960 --> 01:13:45,120
so in order to update our original array
1973
01:13:45,120 --> 01:13:46,800
we're going to have to overwrite the
1974
01:13:46,800 --> 01:13:48,480
arrays variable
1975
01:13:48,480 --> 01:13:50,159
so we're going to reassign the to-do's
1976
01:13:50,159 --> 01:13:53,520
variable with the result of the filter
1977
01:13:53,520 --> 01:13:55,360
so since we're reassigning this variable
1978
01:13:55,360 --> 01:13:57,440
we have to go up here
1979
01:13:57,440 --> 01:14:02,960
and change this from a const to a let
1980
01:14:04,560 --> 01:14:08,159
so now we have the resulting array with
1981
01:14:08,159 --> 01:14:10,719
the to do removed
1982
01:14:10,719 --> 01:14:12,560
and to update our web page we're going
1983
01:14:12,560 --> 01:14:15,520
to call as the last step the render
1984
01:14:15,520 --> 01:14:17,040
function
1985
01:14:17,040 --> 01:14:19,440
now let's go back to our web page and
1986
01:14:19,440 --> 01:14:21,360
test it out
1987
01:14:21,360 --> 01:14:23,360
just going to press the delete button
1988
01:14:23,360 --> 01:14:25,360
and here
1989
01:14:25,360 --> 01:14:27,040
so now you can see that when we click
1990
01:14:27,040 --> 01:14:29,600
delete we're removing the to do that is
1991
01:14:29,600 --> 01:14:32,080
associated with that delete button
1992
01:14:32,080 --> 01:14:35,679
now let me add another to do
1993
01:14:36,080 --> 01:14:39,520
and try to remove the new one
1994
01:14:39,520 --> 01:14:41,120
well you can see that it's not working
1995
01:14:41,120 --> 01:14:43,120
now why is this happening
1996
01:14:43,120 --> 01:14:44,560
well this is actually one of the most
1997
01:14:44,560 --> 01:14:46,239
common pitfalls in javascript that i
1998
01:14:46,239 --> 01:14:48,239
wanted to show you
1999
01:14:48,239 --> 01:14:50,880
so if we go back into our code
2000
01:14:50,880 --> 01:14:52,640
and it's a little hard to figure it out
2001
01:14:52,640 --> 01:14:54,159
so i just want to show you so that you
2002
01:14:54,159 --> 01:14:55,760
have strategies when you're debugging
2003
01:14:55,760 --> 01:14:57,520
your own code
2004
01:14:57,520 --> 01:15:00,080
this method here gettime returns a
2005
01:15:00,080 --> 01:15:02,960
number so this is going to be a number
2006
01:15:02,960 --> 01:15:07,040
and we're saving a number as the id of
2007
01:15:07,040 --> 01:15:09,520
any new to do that we create
2008
01:15:09,520 --> 01:15:12,480
but here when we're removing the to-do
2009
01:15:12,480 --> 01:15:14,719
we're getting the id out of the button
2010
01:15:14,719 --> 01:15:17,600
and we get an id out of an html element
2011
01:15:17,600 --> 01:15:20,560
because of how the dom works this id is
2012
01:15:20,560 --> 01:15:22,800
actually going to be a string version of
2013
01:15:22,800 --> 01:15:26,159
the id so here when we access the id
2014
01:15:26,159 --> 01:15:28,159
from the to do it's going to be a number
2015
01:15:28,159 --> 01:15:32,000
because here we set a number
2016
01:15:32,000 --> 01:15:34,000
but when we access the id out of the
2017
01:15:34,000 --> 01:15:35,840
button this is actually a string so
2018
01:15:35,840 --> 01:15:39,760
we're comparing a number to a string
2019
01:15:39,760 --> 01:15:41,520
and when you compare a number to a
2020
01:15:41,520 --> 01:15:43,440
string unexpected things are bound to
2021
01:15:43,440 --> 01:15:45,120
happen
2022
01:15:45,120 --> 01:15:46,960
basically here it says
2023
01:15:46,960 --> 01:15:48,800
since this is a number and this is a
2024
01:15:48,800 --> 01:15:51,280
string this is going to return false
2025
01:15:51,280 --> 01:15:52,640
every time
2026
01:15:52,640 --> 01:15:54,880
because the types don't match
2027
01:15:54,880 --> 01:15:56,880
since this returns false
2028
01:15:56,880 --> 01:15:58,960
the filter function is always going to
2029
01:15:58,960 --> 01:16:00,640
return true so we're going to keep
2030
01:16:00,640 --> 01:16:02,320
everything in the array and the array is
2031
01:16:02,320 --> 01:16:04,159
not going to change
2032
01:16:04,159 --> 01:16:05,760
and that's why when you go back to the
2033
01:16:05,760 --> 01:16:07,920
web page and you try to delete any new
2034
01:16:07,920 --> 01:16:09,199
to do's
2035
01:16:09,199 --> 01:16:11,199
it doesn't remove it but when you click
2036
01:16:11,199 --> 01:16:13,520
the delete button for the old to do's
2037
01:16:13,520 --> 01:16:16,719
you'll notice that it works because
2038
01:16:16,719 --> 01:16:17,520
the
2039
01:16:17,520 --> 01:16:19,600
ids are strings
2040
01:16:19,600 --> 01:16:20,880
and
2041
01:16:20,880 --> 01:16:23,600
we're comparing a string to a string so
2042
01:16:23,600 --> 01:16:26,640
everything works as expected
2043
01:16:26,640 --> 01:16:28,560
so this is called a type error when
2044
01:16:28,560 --> 01:16:30,719
you're working with some sort of value
2045
01:16:30,719 --> 01:16:32,719
and you expect it to be one type but it
2046
01:16:32,719 --> 01:16:34,719
turns out to be another type and it's
2047
01:16:34,719 --> 01:16:37,360
one of the most common sources of bugs
2048
01:16:37,360 --> 01:16:39,280
in javascript
2049
01:16:39,280 --> 01:16:40,719
and that's why a new language called
2050
01:16:40,719 --> 01:16:42,880
typescript is getting really popular
2051
01:16:42,880 --> 01:16:44,719
because it's basically just an expanded
2052
01:16:44,719 --> 01:16:46,719
version of javascript but it helps you
2053
01:16:46,719 --> 01:16:50,320
deal with type errors like this
2054
01:16:50,320 --> 01:16:52,880
so how do we fix this bug well what we
2055
01:16:52,880 --> 01:16:56,080
can do is instead of setting the id to a
2056
01:16:56,080 --> 01:16:59,040
number here let's set it to a string and
2057
01:16:59,040 --> 01:17:00,800
a trick for converting a number to a
2058
01:17:00,800 --> 01:17:02,400
string in javascript
2059
01:17:02,400 --> 01:17:04,239
is to just add
2060
01:17:04,239 --> 01:17:06,239
the empty string
2061
01:17:06,239 --> 01:17:08,640
so now this id is going to be a string
2062
01:17:08,640 --> 01:17:10,320
and here
2063
01:17:10,320 --> 01:17:12,000
when we access the id it's also going to
2064
01:17:12,000 --> 01:17:14,159
be a string and the comparison is going
2065
01:17:14,159 --> 01:17:16,320
to work properly so let's go back to our
2066
01:17:16,320 --> 01:17:19,040
web page and try it out
2067
01:17:19,040 --> 01:17:22,480
i'm just going to add another to do
2068
01:17:23,199 --> 01:17:24,800
and now when i click this delete button
2069
01:17:24,800 --> 01:17:26,400
you'll notice that we actually delete
2070
01:17:26,400 --> 01:17:28,320
the to do from our list
2071
01:17:28,320 --> 01:17:31,520
and the others still work because
2072
01:17:31,520 --> 01:17:34,239
the other to-do's already have strings
2073
01:17:34,239 --> 01:17:37,719
for their ids
2074
01:17:48,239 --> 01:17:49,760
so the next concept we're going to learn
2075
01:17:49,760 --> 01:17:51,760
is one of the most important concepts in
2076
01:17:51,760 --> 01:17:53,120
this course
2077
01:17:53,120 --> 01:17:56,239
so what we just built here is called mvc
2078
01:17:56,239 --> 01:17:58,000
and i'm telling you if you master this
2079
01:17:58,000 --> 01:17:59,440
coding pattern you will never have
2080
01:17:59,440 --> 01:18:02,239
trouble getting a job as a web developer
2081
01:18:02,239 --> 01:18:06,320
so mbc stands for model view controller
2082
01:18:06,320 --> 01:18:08,320
so the way the mvc works is that it's a
2083
01:18:08,320 --> 01:18:11,040
way to structure our code so we split up
2084
01:18:11,040 --> 01:18:13,199
our code into three sections the model
2085
01:18:13,199 --> 01:18:14,800
section the view section and the
2086
01:18:14,800 --> 01:18:16,719
controller section
2087
01:18:16,719 --> 01:18:18,960
the model section contains all the code
2088
01:18:18,960 --> 01:18:22,080
that saves and manages our data
2089
01:18:22,080 --> 01:18:24,000
the view section contains all the code
2090
01:18:24,000 --> 01:18:26,239
that manages our visuals and manages
2091
01:18:26,239 --> 01:18:28,239
rendering our visuals according to the
2092
01:18:28,239 --> 01:18:30,560
data that's stored in the model
2093
01:18:30,560 --> 01:18:32,400
the controller section connects the two
2094
01:18:32,400 --> 01:18:34,880
together it responds to events from the
2095
01:18:34,880 --> 01:18:37,679
view like click events and then tells
2096
01:18:37,679 --> 01:18:42,480
the model to change its data accordingly
2097
01:18:44,880 --> 01:18:46,880
so let's go back into our code and see
2098
01:18:46,880 --> 01:18:50,159
if we can identify the sections of mvc
2099
01:18:50,159 --> 01:18:52,640
so let's start with the model
2100
01:18:52,640 --> 01:18:54,239
in our code the model section is
2101
01:18:54,239 --> 01:18:56,640
actually really simple it's just this to
2102
01:18:56,640 --> 01:18:58,560
do's array
2103
01:18:58,560 --> 01:19:00,800
so it might not look like much but an
2104
01:19:00,800 --> 01:19:02,960
array actually helps us save our data
2105
01:19:02,960 --> 01:19:05,080
and it offers some methods like to
2106
01:19:05,080 --> 01:19:06,640
dos.push
2107
01:19:06,640 --> 01:19:09,920
and to use dot filter
2108
01:19:09,920 --> 01:19:12,719
to manage our data
2109
01:19:12,719 --> 01:19:15,040
so let's call this the
2110
01:19:15,040 --> 01:19:16,400
model section
2111
01:19:16,400 --> 01:19:17,920
with a comment
2112
01:19:17,920 --> 01:19:20,080
and the proper way to organize our code
2113
01:19:20,080 --> 01:19:21,360
is that we want everything that's
2114
01:19:21,360 --> 01:19:25,520
related to managing data in this section
2115
01:19:25,520 --> 01:19:27,520
so there's actually two sections of code
2116
01:19:27,520 --> 01:19:29,360
that we want to move in here
2117
01:19:29,360 --> 01:19:32,320
the code that creates a to-do and the
2118
01:19:32,320 --> 01:19:36,800
code that deletes a to-do
2119
01:19:36,800 --> 01:19:38,719
because these two things are all about
2120
01:19:38,719 --> 01:19:40,640
managing data and we want to put them in
2121
01:19:40,640 --> 01:19:42,560
the model section
2122
01:19:42,560 --> 01:19:44,320
i'm going to explain in a bit why it's
2123
01:19:44,320 --> 01:19:46,560
important to organize our code like this
2124
01:19:46,560 --> 01:19:48,400
but let's go down to our add to do
2125
01:19:48,400 --> 01:19:49,360
function
2126
01:19:49,360 --> 01:19:51,840
and you'll notice our code for creating
2127
01:19:51,840 --> 01:19:54,000
a to-do is here so let's create a
2128
01:19:54,000 --> 01:19:56,080
function in our model section
2129
01:19:56,080 --> 01:19:58,480
that just handles creating a to-do so
2130
01:19:58,480 --> 01:20:01,360
let's call it create to do
2131
01:20:01,360 --> 01:20:05,640
and we're going to move this here
2132
01:20:08,800 --> 01:20:10,639
so it's going to create a to do as an
2133
01:20:10,639 --> 01:20:13,360
object and it's going to push this
2134
01:20:13,360 --> 01:20:15,360
object onto this array so it's going to
2135
01:20:15,360 --> 01:20:17,440
manage all the data now you'll notice
2136
01:20:17,440 --> 01:20:19,120
that we don't have the title and due
2137
01:20:19,120 --> 01:20:21,360
date values anymore so we'll have to
2138
01:20:21,360 --> 01:20:24,480
pass them in as parameters
2139
01:20:24,480 --> 01:20:27,360
so let's create a parameter called title
2140
01:20:27,360 --> 01:20:29,199
and in order to add a second parameter
2141
01:20:29,199 --> 01:20:32,560
we just separate it with a comma so do
2142
01:20:32,560 --> 01:20:34,080
date
2143
01:20:34,080 --> 01:20:35,760
so these parameters like we learned
2144
01:20:35,760 --> 01:20:38,320
before are variables we have two
2145
01:20:38,320 --> 01:20:40,239
variables in this function that we can
2146
01:20:40,239 --> 01:20:41,120
use
2147
01:20:41,120 --> 01:20:43,440
and to set the value of these variables
2148
01:20:43,440 --> 01:20:45,199
we can do that when we're calling the
2149
01:20:45,199 --> 01:20:49,280
function so when we say create to do
2150
01:20:49,280 --> 01:20:51,920
we can set the value of these variables
2151
01:20:51,920 --> 01:20:52,800
by
2152
01:20:52,800 --> 01:20:54,560
using this
2153
01:20:54,560 --> 01:20:55,760
say title
2154
01:20:55,760 --> 01:20:58,159
and due date
2155
01:20:58,159 --> 01:21:00,880
so here we're taking the value of title
2156
01:21:00,880 --> 01:21:03,520
we're putting it here and it gets set
2157
01:21:03,520 --> 01:21:05,840
as the value of this parameter
2158
01:21:05,840 --> 01:21:07,199
then we're taking the value of due date
2159
01:21:07,199 --> 01:21:09,679
we're putting here which gets set as the
2160
01:21:09,679 --> 01:21:11,600
value of this parameter and then we can
2161
01:21:11,600 --> 01:21:14,560
run the code as usual
2162
01:21:14,560 --> 01:21:16,400
so now
2163
01:21:16,400 --> 01:21:18,560
if we go back to our web page
2164
01:21:18,560 --> 01:21:19,920
and refresh
2165
01:21:19,920 --> 01:21:23,199
and create another to do
2166
01:21:24,239 --> 01:21:25,600
you'll notice that everything still
2167
01:21:25,600 --> 01:21:26,880
works the same
2168
01:21:26,880 --> 01:21:29,040
and that's because all we did was we
2169
01:21:29,040 --> 01:21:31,679
just separated our
2170
01:21:31,679 --> 01:21:33,920
our code that manages data into this
2171
01:21:33,920 --> 01:21:35,280
model section
2172
01:21:35,280 --> 01:21:37,440
so now let's deal with the code that
2173
01:21:37,440 --> 01:21:40,800
manages deleting it to do
2174
01:21:40,800 --> 01:21:42,960
so that oh by the way we can remove this
2175
01:21:42,960 --> 01:21:44,639
function
2176
01:21:44,639 --> 01:21:48,000
so the code for deleting a to do is here
2177
01:21:48,000 --> 01:21:51,760
so let's create a new function
2178
01:21:52,320 --> 01:21:54,239
called uh remove
2179
01:21:54,239 --> 01:21:56,960
to do
2180
01:21:56,960 --> 01:21:59,840
and let's move this logic or let's move
2181
01:21:59,840 --> 01:22:03,120
this code inside here
2182
01:22:03,679 --> 01:22:04,880
so
2183
01:22:04,880 --> 01:22:07,760
so all the code that deals with managing
2184
01:22:07,760 --> 01:22:10,159
the data in this to-do's array goes
2185
01:22:10,159 --> 01:22:13,360
inside this model section
2186
01:22:13,360 --> 01:22:15,600
so one thing that we need is the value
2187
01:22:15,600 --> 01:22:18,400
of id to delete so let's pass that in as
2188
01:22:18,400 --> 01:22:22,800
a parameter just like we did here
2189
01:22:23,520 --> 01:22:25,120
so in our
2190
01:22:25,120 --> 01:22:26,880
delete to do function we're going to
2191
01:22:26,880 --> 01:22:27,920
call
2192
01:22:27,920 --> 01:22:30,239
remove to do
2193
01:22:30,239 --> 01:22:32,400
and we're going to pass in the id to
2194
01:22:32,400 --> 01:22:34,800
delete
2195
01:22:34,880 --> 01:22:37,679
so this value is going to go
2196
01:22:37,679 --> 01:22:40,239
into this parameter and that's going to
2197
01:22:40,239 --> 01:22:41,520
be used here
2198
01:22:41,520 --> 01:22:43,199
so let's go back into our web page and
2199
01:22:43,199 --> 01:22:45,920
just make sure everything still works
2200
01:22:45,920 --> 01:22:48,960
another to do
2201
01:22:49,280 --> 01:22:50,800
to do
2202
01:22:50,800 --> 01:22:52,480
so we click delete you'll notice that
2203
01:22:52,480 --> 01:22:54,080
it's removing that to do if we click
2204
01:22:54,080 --> 01:22:55,920
another delete button it's removing that
2205
01:22:55,920 --> 01:22:57,280
to do
2206
01:22:57,280 --> 01:22:58,960
so everything still works we've just
2207
01:22:58,960 --> 01:23:02,880
organized our code a little differently
2208
01:23:02,880 --> 01:23:05,360
so now the second part of mvc is the
2209
01:23:05,360 --> 01:23:07,360
view section so let's see what code
2210
01:23:07,360 --> 01:23:09,440
manages the view section
2211
01:23:09,440 --> 01:23:11,679
well this one is a little easy because
2212
01:23:11,679 --> 01:23:14,239
we have this render function which
2213
01:23:14,239 --> 01:23:17,280
basically takes the data that's in the
2214
01:23:17,280 --> 01:23:18,320
model
2215
01:23:18,320 --> 01:23:20,800
and renders the view
2216
01:23:20,800 --> 01:23:23,920
so this is all we have for the view
2217
01:23:23,920 --> 01:23:25,679
section
2218
01:23:25,679 --> 01:23:27,679
now the html at the top here that
2219
01:23:27,679 --> 01:23:29,280
renders the
2220
01:23:29,280 --> 01:23:31,840
the text box the date picker and the add
2221
01:23:31,840 --> 01:23:33,760
to do button these are all part of the
2222
01:23:33,760 --> 01:23:34,560
view
2223
01:23:34,560 --> 01:23:36,400
because they handle rendering the visual
2224
01:23:36,400 --> 01:23:38,960
elements but for simplicity here in our
2225
01:23:38,960 --> 01:23:42,320
code we're just going to say this is the
2226
01:23:42,320 --> 01:23:43,440
view section
2227
01:23:43,440 --> 01:23:45,040
and now for the controllers the
2228
01:23:45,040 --> 01:23:47,440
controllers are code that respond to
2229
01:23:47,440 --> 01:23:49,920
events in the view so we have two
2230
01:23:49,920 --> 01:23:52,880
functions that respond to click events
2231
01:23:52,880 --> 01:23:54,560
so if we go back to our web page we have
2232
01:23:54,560 --> 01:23:56,480
two buttons here the add to do button
2233
01:23:56,480 --> 01:23:58,719
and the delete to do button so someone
2234
01:23:58,719 --> 01:24:00,719
using our app can interact with our app
2235
01:24:00,719 --> 01:24:02,639
by clicking these buttons
2236
01:24:02,639 --> 01:24:04,880
and the controller code is the one
2237
01:24:04,880 --> 01:24:06,639
that's supposed to respond to these
2238
01:24:06,639 --> 01:24:08,800
clicks and do something
2239
01:24:08,800 --> 01:24:12,080
so in our code we know that up here
2240
01:24:12,080 --> 01:24:14,320
if we click on the add to do button it's
2241
01:24:14,320 --> 01:24:16,639
going to run the add to do function
2242
01:24:16,639 --> 01:24:19,199
which is right here
2243
01:24:19,199 --> 01:24:21,760
so this is a controller function because
2244
01:24:21,760 --> 01:24:25,199
it responds to interactions in the view
2245
01:24:25,199 --> 01:24:28,000
and then it tells the model to update
2246
01:24:28,000 --> 01:24:30,880
based on the interaction in the same way
2247
01:24:30,880 --> 01:24:33,679
the delete to do function is also a
2248
01:24:33,679 --> 01:24:35,040
controller function because we're
2249
01:24:35,040 --> 01:24:37,040
attaching it to this delete button so it
2250
01:24:37,040 --> 01:24:39,600
responds to events or clicks on the
2251
01:24:39,600 --> 01:24:43,440
delete button so this part of our code
2252
01:24:43,440 --> 01:24:45,570
is the controller section
2253
01:24:45,570 --> 01:24:46,960
[Applause]
2254
01:24:46,960 --> 01:24:49,440
just the controller section
2255
01:24:49,440 --> 01:24:51,679
so now you can see that we've split our
2256
01:24:51,679 --> 01:24:54,400
code up into the model
2257
01:24:54,400 --> 01:24:55,679
the view
2258
01:24:55,679 --> 01:24:58,239
and the controller so why is this so
2259
01:24:58,239 --> 01:24:59,760
important
2260
01:24:59,760 --> 01:25:01,360
well one big reason is that it makes
2261
01:25:01,360 --> 01:25:03,679
your code easy to understand because
2262
01:25:03,679 --> 01:25:06,159
each section can handle a very specific
2263
01:25:06,159 --> 01:25:07,199
task
2264
01:25:07,199 --> 01:25:08,880
so as an example let's say that we
2265
01:25:08,880 --> 01:25:10,719
forgot all about mvc
2266
01:25:10,719 --> 01:25:12,560
and we just wrote our application out
2267
01:25:12,560 --> 01:25:14,159
directly
2268
01:25:14,159 --> 01:25:16,719
so if we go back to our application
2269
01:25:16,719 --> 01:25:18,639
we can see that we really only need two
2270
01:25:18,639 --> 01:25:20,400
functions we need a function for
2271
01:25:20,400 --> 01:25:22,639
handling this button click and we need a
2272
01:25:22,639 --> 01:25:25,040
function for handling this button click
2273
01:25:25,040 --> 01:25:26,960
so in our code
2274
01:25:26,960 --> 01:25:30,320
those two functions refer to these
2275
01:25:30,320 --> 01:25:32,159
everything else is part of mvc and we
2276
01:25:32,159 --> 01:25:34,239
can get rid of it but if we were to just
2277
01:25:34,239 --> 01:25:36,320
write our code we would just have these
2278
01:25:36,320 --> 01:25:37,840
two functions
2279
01:25:37,840 --> 01:25:40,480
now let's say that we didn't have mvc
2280
01:25:40,480 --> 01:25:42,320
well instead of this create to do
2281
01:25:42,320 --> 01:25:44,719
function we would have to write out a
2282
01:25:44,719 --> 01:25:46,080
bunch of code
2283
01:25:46,080 --> 01:25:47,040
that
2284
01:25:47,040 --> 01:25:49,760
manually creates our to-do like we had
2285
01:25:49,760 --> 01:25:51,120
before
2286
01:25:51,120 --> 01:25:53,040
so this code might not seem complicated
2287
01:25:53,040 --> 01:25:54,719
we just have a toduz array and we're
2288
01:25:54,719 --> 01:25:57,520
pushing another value onto the array but
2289
01:25:57,520 --> 01:25:59,040
in the future we might have some
2290
01:25:59,040 --> 01:26:01,280
validation for these title and due date
2291
01:26:01,280 --> 01:26:03,199
fields we might be sending some
2292
01:26:03,199 --> 01:26:05,679
notifications we might be doing some
2293
01:26:05,679 --> 01:26:08,000
data collection or we might be saving to
2294
01:26:08,000 --> 01:26:09,600
a backend server
2295
01:26:09,600 --> 01:26:11,280
so you can see that this code that
2296
01:26:11,280 --> 01:26:13,280
manages the data can get really
2297
01:26:13,280 --> 01:26:14,639
complicated
2298
01:26:14,639 --> 01:26:16,560
the same thing goes for the render
2299
01:26:16,560 --> 01:26:17,679
function
2300
01:26:17,679 --> 01:26:18,400
so
2301
01:26:18,400 --> 01:26:20,639
instead of having just a render function
2302
01:26:20,639 --> 01:26:23,120
we would have to go into the html and
2303
01:26:23,120 --> 01:26:25,520
make sure that it matches our array or
2304
01:26:25,520 --> 01:26:27,600
the current state of our array and then
2305
01:26:27,600 --> 01:26:28,639
we have to
2306
01:26:28,639 --> 01:26:30,639
figure out where we want to insert our
2307
01:26:30,639 --> 01:26:32,400
new to do
2308
01:26:32,400 --> 01:26:34,639
so in that case this function becomes
2309
01:26:34,639 --> 01:26:36,400
extremely complicated there's three
2310
01:26:36,400 --> 01:26:39,280
things going on we're responding to the
2311
01:26:39,280 --> 01:26:41,760
click event here we are managing all the
2312
01:26:41,760 --> 01:26:44,560
data here and we're managing all the
2313
01:26:44,560 --> 01:26:46,639
visuals at the bottom
2314
01:26:46,639 --> 01:26:49,520
so what mbc allows us to do is that it
2315
01:26:49,520 --> 01:26:52,400
allows each section to only do one thing
2316
01:26:52,400 --> 01:26:54,400
and it allows us to write the minimum
2317
01:26:54,400 --> 01:26:56,960
amount of code for the other sections so
2318
01:26:56,960 --> 01:26:58,960
what do i mean by that
2319
01:26:58,960 --> 01:27:01,120
well this is the controller section so
2320
01:27:01,120 --> 01:27:03,440
the only code that should be here is
2321
01:27:03,440 --> 01:27:06,159
code that deals with the button click or
2322
01:27:06,159 --> 01:27:09,920
the interaction so this is that code
2323
01:27:09,920 --> 01:27:11,920
and for code that's not related to the
2324
01:27:11,920 --> 01:27:14,560
controller mvc allows us to write the
2325
01:27:14,560 --> 01:27:16,880
minimum amount of code that we need to
2326
01:27:16,880 --> 01:27:19,840
get things done so in that case all we
2327
01:27:19,840 --> 01:27:23,040
need to do is create a to do and we want
2328
01:27:23,040 --> 01:27:25,600
to give the new to do this title and
2329
01:27:25,600 --> 01:27:27,679
this due date
2330
01:27:27,679 --> 01:27:29,760
and this is the minimum amount of code
2331
01:27:29,760 --> 01:27:32,080
that we need to write to get this to do
2332
01:27:32,080 --> 01:27:34,000
to become created
2333
01:27:34,000 --> 01:27:35,840
now inside this function there can be a
2334
01:27:35,840 --> 01:27:37,760
whole bunch of details but we don't need
2335
01:27:37,760 --> 01:27:39,600
to worry about it here because that's
2336
01:27:39,600 --> 01:27:41,199
done in the model we're in the
2337
01:27:41,199 --> 01:27:42,719
controller section we shouldn't be
2338
01:27:42,719 --> 01:27:44,800
worrying about those details
2339
01:27:44,800 --> 01:27:46,800
and similarly for the view
2340
01:27:46,800 --> 01:27:48,480
instead of having all the html
2341
01:27:48,480 --> 01:27:50,800
validation and rendering code we can
2342
01:27:50,800 --> 01:27:53,280
just have a render function
2343
01:27:53,280 --> 01:27:55,679
so calling render is the minimum amount
2344
01:27:55,679 --> 01:27:57,280
of code that we need
2345
01:27:57,280 --> 01:27:59,360
to update the view
2346
01:27:59,360 --> 01:28:02,000
so you can see that because of mvc
2347
01:28:02,000 --> 01:28:04,239
our controller only needs to have
2348
01:28:04,239 --> 01:28:06,320
controller code and it can write the
2349
01:28:06,320 --> 01:28:08,480
minimum amount of code for the model and
2350
01:28:08,480 --> 01:28:11,520
the minimum amount of code for the view
2351
01:28:11,520 --> 01:28:14,080
and this makes our functions really
2352
01:28:14,080 --> 01:28:16,800
simple and specific and that is a best
2353
01:28:16,800 --> 01:28:18,320
practice in programming you want your
2354
01:28:18,320 --> 01:28:20,639
functions to be doing one thing and one
2355
01:28:20,639 --> 01:28:23,040
thing only ideally
2356
01:28:23,040 --> 01:28:25,920
so that's a short introduction to mvc
2357
01:28:25,920 --> 01:28:27,760
it's a really important concept in web
2358
01:28:27,760 --> 01:28:30,320
development and it's what a lot of web
2359
01:28:30,320 --> 01:28:33,360
technologies are built on
2360
01:28:33,360 --> 01:28:35,679
so how does mvc help you in finding a
2361
01:28:35,679 --> 01:28:37,280
job so when you're looking for your
2362
01:28:37,280 --> 01:28:39,280
first job you usually have to build a
2363
01:28:39,280 --> 01:28:41,199
bunch of projects and then share those
2364
01:28:41,199 --> 01:28:42,960
projects along with the code to the
2365
01:28:42,960 --> 01:28:44,000
company
2366
01:28:44,000 --> 01:28:45,920
when you use mvc you can build bigger
2367
01:28:45,920 --> 01:28:47,440
and more impressive projects because
2368
01:28:47,440 --> 01:28:49,679
your code is well organized and when the
2369
01:28:49,679 --> 01:28:51,360
company looks at your code it's a lot
2370
01:28:51,360 --> 01:28:52,880
better for you if you're using an
2371
01:28:52,880 --> 01:28:54,639
architecture that their engineers are
2372
01:28:54,639 --> 01:28:57,120
using every day the second big reason
2373
01:28:57,120 --> 01:28:59,360
for understanding mvc is that when
2374
01:28:59,360 --> 01:29:01,520
developers are creating web applications
2375
01:29:01,520 --> 01:29:04,080
they usually use a javascript framework
2376
01:29:04,080 --> 01:29:05,679
and most of these frameworks are built
2377
01:29:05,679 --> 01:29:07,120
on mvc
2378
01:29:07,120 --> 01:29:08,800
and this is how i explain what a
2379
01:29:08,800 --> 01:29:11,280
framework is right now i have a render
2380
01:29:11,280 --> 01:29:13,360
function that has two steps the first
2381
01:29:13,360 --> 01:29:15,840
step is to blow away our whole list and
2382
01:29:15,840 --> 01:29:17,520
the second step is to re-render the
2383
01:29:17,520 --> 01:29:19,840
whole list another developer might want
2384
01:29:19,840 --> 01:29:21,440
to do it differently they might want to
2385
01:29:21,440 --> 01:29:23,040
keep everything the same and then just
2386
01:29:23,040 --> 01:29:25,280
look at which element in the list
2387
01:29:25,280 --> 01:29:27,199
changed and then replace that element or
2388
01:29:27,199 --> 01:29:29,199
remove it or add it
2389
01:29:29,199 --> 01:29:31,120
so basically every developer can create
2390
01:29:31,120 --> 01:29:33,520
web applications in a different way and
2391
01:29:33,520 --> 01:29:35,920
what frameworks do is they say here's a
2392
01:29:35,920 --> 01:29:37,199
collection of
2393
01:29:37,199 --> 01:29:40,400
functions methods and objects and these
2394
01:29:40,400 --> 01:29:43,040
will handle all of the rendering for you
2395
01:29:43,040 --> 01:29:45,199
and if every developer starts using a
2396
01:29:45,199 --> 01:29:47,120
framework all of our code starts looking
2397
01:29:47,120 --> 01:29:49,360
the same so when we go to one project
2398
01:29:49,360 --> 01:29:51,199
the code looks the same as another
2399
01:29:51,199 --> 01:29:54,239
project that's using the same framework
2400
01:29:54,239 --> 01:29:55,920
so it's a way to make all of our code
2401
01:29:55,920 --> 01:29:58,560
more consistent and readable and
2402
01:29:58,560 --> 01:30:00,239
frameworks can also provide a lot of
2403
01:30:00,239 --> 01:30:02,719
functionality that i don't have to write
2404
01:30:02,719 --> 01:30:04,800
for example like i mentioned before if
2405
01:30:04,800 --> 01:30:07,280
the list changes then they can provide a
2406
01:30:07,280 --> 01:30:09,520
render function that figures out what
2407
01:30:09,520 --> 01:30:11,440
changed for me instead of me having to
2408
01:30:11,440 --> 01:30:13,600
write my own render function so it saves
2409
01:30:13,600 --> 01:30:16,560
me a lot of time effort and code
2410
01:30:16,560 --> 01:30:17,920
since the majority of frameworks are
2411
01:30:17,920 --> 01:30:20,800
built on mvc sometimes known as mvw or
2412
01:30:20,800 --> 01:30:22,800
nvstar it's all the same
2413
01:30:22,800 --> 01:30:24,800
it helps you transition from framework
2414
01:30:24,800 --> 01:30:27,120
to framework if you understand mvc and
2415
01:30:27,120 --> 01:30:28,639
you won't have trouble learning the
2416
01:30:28,639 --> 01:30:31,040
latest framework because they all follow
2417
01:30:31,040 --> 01:30:33,199
a very similar pattern
2418
01:30:33,199 --> 01:30:34,560
and when you have a good understanding
2419
01:30:34,560 --> 01:30:36,719
of these frameworks it helps you in your
2420
01:30:36,719 --> 01:30:39,280
chances of getting a job tremendously
2421
01:30:39,280 --> 01:30:41,040
because you're using the exact same
2422
01:30:41,040 --> 01:30:43,040
frameworks that professionals use on the
2423
01:30:43,040 --> 01:30:46,040
job
2424
01:30:57,199 --> 01:30:59,679
now that we know what nbc is let's take
2425
01:30:59,679 --> 01:31:01,679
a look at another example to help us
2426
01:31:01,679 --> 01:31:02,560
learn
2427
01:31:02,560 --> 01:31:04,080
so we're going to be creating one last
2428
01:31:04,080 --> 01:31:06,159
feature for our app called save and
2429
01:31:06,159 --> 01:31:07,280
retrieve
2430
01:31:07,280 --> 01:31:09,760
so what save and retrieve does is that
2431
01:31:09,760 --> 01:31:12,080
if we go back to our web page
2432
01:31:12,080 --> 01:31:14,320
right now if i refresh the page you'll
2433
01:31:14,320 --> 01:31:16,639
see that this list gets reset
2434
01:31:16,639 --> 01:31:18,560
what save and retrieve does is that when
2435
01:31:18,560 --> 01:31:20,880
we refresh the page or we when we close
2436
01:31:20,880 --> 01:31:23,280
the browser and reopen it our list will
2437
01:31:23,280 --> 01:31:25,360
also be saved so we'll come back to the
2438
01:31:25,360 --> 01:31:27,120
same list
2439
01:31:27,120 --> 01:31:30,080
so how would we implement this feature
2440
01:31:30,080 --> 01:31:32,400
so let's go back to our code
2441
01:31:32,400 --> 01:31:34,719
so we know that saving our data has to
2442
01:31:34,719 --> 01:31:37,600
do with data and anything that has to do
2443
01:31:37,600 --> 01:31:40,400
with data goes into the model section
2444
01:31:40,400 --> 01:31:42,480
see how it works
2445
01:31:42,480 --> 01:31:45,040
so now let's go into our model section
2446
01:31:45,040 --> 01:31:47,280
which is right here and we're going to
2447
01:31:47,280 --> 01:31:49,760
add a new function to the end of it so
2448
01:31:49,760 --> 01:31:51,920
let's create a function called save to
2449
01:31:51,920 --> 01:31:54,239
do's
2450
01:31:55,360 --> 01:31:57,440
so how and where are we going to save
2451
01:31:57,440 --> 01:31:59,520
our to-do's well the browser actually
2452
01:31:59,520 --> 01:32:01,520
provides us some code to help us save
2453
01:32:01,520 --> 01:32:03,760
and retrieve data and this code is
2454
01:32:03,760 --> 01:32:06,320
called local storage
2455
01:32:06,320 --> 01:32:08,239
so the way that local storage works is
2456
01:32:08,239 --> 01:32:10,560
that it provides a set item function
2457
01:32:10,560 --> 01:32:13,280
which allows us to save data
2458
01:32:13,280 --> 01:32:15,120
in order to use set item we're going to
2459
01:32:15,120 --> 01:32:16,880
pass in a key
2460
01:32:16,880 --> 01:32:19,199
and we're going to pass in our data and
2461
01:32:19,199 --> 01:32:21,360
later we can use this key to retrieve
2462
01:32:21,360 --> 01:32:23,840
our data
2463
01:32:25,280 --> 01:32:27,120
so let's go back to our code and take a
2464
01:32:27,120 --> 01:32:30,080
look at how we can use local storage so
2465
01:32:30,080 --> 01:32:31,679
in our save to do's function what we
2466
01:32:31,679 --> 01:32:33,800
want to do is call
2467
01:32:33,800 --> 01:32:36,320
localstorage.set item
2468
01:32:36,320 --> 01:32:38,239
and we're going to give it a key here
2469
01:32:38,239 --> 01:32:40,080
that we're going to use to retrieve our
2470
01:32:40,080 --> 01:32:42,639
data later but for now let's prepare our
2471
01:32:42,639 --> 01:32:45,360
data so local storage can only save
2472
01:32:45,360 --> 01:32:48,320
strings and right now our data is in an
2473
01:32:48,320 --> 01:32:50,880
array so what we need to do is convert
2474
01:32:50,880 --> 01:32:52,880
this array into a string
2475
01:32:52,880 --> 01:32:55,199
so the command in javascript for
2476
01:32:55,199 --> 01:32:58,760
converting an array into a string is
2477
01:32:58,760 --> 01:33:01,040
json.stringify so this is a function and
2478
01:33:01,040 --> 01:33:02,719
we're going to pass in
2479
01:33:02,719 --> 01:33:05,360
our array
2480
01:33:05,360 --> 01:33:08,080
so the result of this is going to be the
2481
01:33:08,080 --> 01:33:10,719
string version of this array
2482
01:33:10,719 --> 01:33:12,159
and now let's change the key to
2483
01:33:12,159 --> 01:33:13,600
something that makes more sense let's
2484
01:33:13,600 --> 01:33:14,719
call this
2485
01:33:14,719 --> 01:33:17,120
just to do's
2486
01:33:17,120 --> 01:33:18,880
so now that we have this save to do's
2487
01:33:18,880 --> 01:33:21,600
function when do we run this function
2488
01:33:21,600 --> 01:33:22,800
well we should run this function
2489
01:33:22,800 --> 01:33:25,600
whenever our to-do's change
2490
01:33:25,600 --> 01:33:27,520
which is
2491
01:33:27,520 --> 01:33:28,719
here
2492
01:33:28,719 --> 01:33:31,440
and here so notice that here
2493
01:33:31,440 --> 01:33:33,920
we're pushing a new item onto the
2494
01:33:33,920 --> 01:33:36,159
to-do's array so it's changing here and
2495
01:33:36,159 --> 01:33:38,320
here we're reassigning it to a filtered
2496
01:33:38,320 --> 01:33:40,639
version of the to-do's array so it's
2497
01:33:40,639 --> 01:33:42,960
also changing here so
2498
01:33:42,960 --> 01:33:45,120
after this code runs we're just going to
2499
01:33:45,120 --> 01:33:48,639
call save to do's to save our updated
2500
01:33:48,639 --> 01:33:52,000
to-do list in local storage
2501
01:33:52,000 --> 01:33:54,239
so that takes care of the saving part of
2502
01:33:54,239 --> 01:33:56,719
this feature now how do we retrieve the
2503
01:33:56,719 --> 01:33:59,360
data from local storage when we start
2504
01:33:59,360 --> 01:34:00,480
the app
2505
01:34:00,480 --> 01:34:02,560
so we're going to go up here and when
2506
01:34:02,560 --> 01:34:04,480
our code runs for the first time
2507
01:34:04,480 --> 01:34:06,639
instead of using this default array
2508
01:34:06,639 --> 01:34:08,560
we're going to check local storage to
2509
01:34:08,560 --> 01:34:11,760
see if it already has a to-do's array
2510
01:34:11,760 --> 01:34:13,920
saved and if it does we're going to use
2511
01:34:13,920 --> 01:34:16,080
that and if it doesn't then we're going
2512
01:34:16,080 --> 01:34:18,480
to use this default array
2513
01:34:18,480 --> 01:34:20,480
so how do we do that well this looks
2514
01:34:20,480 --> 01:34:22,400
like a good situation to use an if
2515
01:34:22,400 --> 01:34:23,520
statement
2516
01:34:23,520 --> 01:34:25,840
so let's write out the logic here if
2517
01:34:25,840 --> 01:34:28,000
local storage
2518
01:34:28,000 --> 01:34:31,520
has a to-do's array
2519
01:34:31,520 --> 01:34:32,719
then
2520
01:34:32,719 --> 01:34:34,880
use it
2521
01:34:34,880 --> 01:34:36,880
otherwise
2522
01:34:36,880 --> 01:34:40,000
use the default array
2523
01:34:40,000 --> 01:34:41,440
so that's the logic that we want to
2524
01:34:41,440 --> 01:34:44,320
create so let's implement it right now
2525
01:34:44,320 --> 01:34:46,080
the first thing we have to do is we have
2526
01:34:46,080 --> 01:34:48,880
to create this to do's variable so that
2527
01:34:48,880 --> 01:34:51,040
we have something to save it in but
2528
01:34:51,040 --> 01:34:53,040
since we don't know beforehand whether
2529
01:34:53,040 --> 01:34:54,320
we're going to get something from local
2530
01:34:54,320 --> 01:34:56,560
storage or use the default array we're
2531
01:34:56,560 --> 01:34:58,800
going to leave this as empty
2532
01:34:58,800 --> 01:35:00,480
so when you're creating a variable you
2533
01:35:00,480 --> 01:35:02,080
don't have to assign a value to the
2534
01:35:02,080 --> 01:35:04,320
variable right away unless it's a
2535
01:35:04,320 --> 01:35:05,520
constant
2536
01:35:05,520 --> 01:35:06,639
so that's what we're doing right now
2537
01:35:06,639 --> 01:35:08,239
we're only creating the variable but
2538
01:35:08,239 --> 01:35:10,560
we're not giving it a value
2539
01:35:10,560 --> 01:35:12,560
and to implement this logic we're going
2540
01:35:12,560 --> 01:35:13,600
to go
2541
01:35:13,600 --> 01:35:16,719
and start our if statement so if
2542
01:35:16,719 --> 01:35:18,719
and else
2543
01:35:18,719 --> 01:35:20,800
so we already know what happens
2544
01:35:20,800 --> 01:35:23,600
in the else branch we're going to set to
2545
01:35:23,600 --> 01:35:24,880
do's
2546
01:35:24,880 --> 01:35:30,320
to equal the default array which is this
2547
01:35:32,080 --> 01:35:33,520
and if you want to indent this you can
2548
01:35:33,520 --> 01:35:36,719
select all these lines and press tab
2549
01:35:36,719 --> 01:35:40,000
so let's get rid of these lines
2550
01:35:40,080 --> 01:35:42,000
okay so now we have the else branch what
2551
01:35:42,000 --> 01:35:45,199
do we do for the if branch
2552
01:35:45,199 --> 01:35:47,840
so for this if test we need to check if
2553
01:35:47,840 --> 01:35:50,400
local storage has a to do's array so how
2554
01:35:50,400 --> 01:35:51,600
do we do that
2555
01:35:51,600 --> 01:35:53,920
well we can do it in two steps first we
2556
01:35:53,920 --> 01:35:58,239
can retrieve whatever's in local storage
2557
01:35:58,239 --> 01:36:03,199
and then we can check if it's an array
2558
01:36:03,440 --> 01:36:05,119
so how do we retrieve something from
2559
01:36:05,119 --> 01:36:06,400
local storage
2560
01:36:06,400 --> 01:36:07,920
so the code for that is
2561
01:36:07,920 --> 01:36:10,639
localstorage.getitem
2562
01:36:10,639 --> 01:36:12,800
and we're going to pass it a key so
2563
01:36:12,800 --> 01:36:16,320
remember that when we saved this data we
2564
01:36:16,320 --> 01:36:18,480
gave it the key of to-do's well now
2565
01:36:18,480 --> 01:36:20,239
we're going to give the same key to
2566
01:36:20,239 --> 01:36:22,400
retrieve that data
2567
01:36:22,400 --> 01:36:25,199
so we're going to say to do's
2568
01:36:25,199 --> 01:36:26,880
and this is going to get whatever's in
2569
01:36:26,880 --> 01:36:31,119
local storage under the key to do's
2570
01:36:31,199 --> 01:36:33,440
and let's save this oops so let's save
2571
01:36:33,440 --> 01:36:36,960
this data in a variable
2572
01:36:37,600 --> 01:36:39,440
so the way that local storage works is
2573
01:36:39,440 --> 01:36:42,080
that if something exists for this key it
2574
01:36:42,080 --> 01:36:44,639
will return a string so remember that
2575
01:36:44,639 --> 01:36:46,480
when we saved the data we had to convert
2576
01:36:46,480 --> 01:36:48,239
it into a string
2577
01:36:48,239 --> 01:36:50,400
so now when we retrieve the data we have
2578
01:36:50,400 --> 01:36:53,119
to convert it back into an array
2579
01:36:53,119 --> 01:36:55,119
and javascript provides a function for
2580
01:36:55,119 --> 01:36:57,119
converting a string to an array or an
2581
01:36:57,119 --> 01:36:58,080
object
2582
01:36:58,080 --> 01:37:02,320
and that function is json.parse
2583
01:37:02,320 --> 01:37:04,239
so we're going to run that function on
2584
01:37:04,239 --> 01:37:07,119
the result of this so now the second
2585
01:37:07,119 --> 01:37:10,400
step is to check if this is an array
2586
01:37:10,400 --> 01:37:12,320
so javascript actually has some code for
2587
01:37:12,320 --> 01:37:15,840
that too and that is array
2588
01:37:15,840 --> 01:37:18,080
dot is array
2589
01:37:18,080 --> 01:37:19,119
saved
2590
01:37:19,119 --> 01:37:21,360
to do's
2591
01:37:21,360 --> 01:37:23,119
okay now the last step that we have to
2592
01:37:23,119 --> 01:37:24,159
do is
2593
01:37:24,159 --> 01:37:26,480
if local storage has a todos array which
2594
01:37:26,480 --> 01:37:28,400
is what we have here
2595
01:37:28,400 --> 01:37:30,560
then we're going to use it as our to
2596
01:37:30,560 --> 01:37:32,480
do's so we're going to assign it to this
2597
01:37:32,480 --> 01:37:35,199
variable so all we have to do is assign
2598
01:37:35,199 --> 01:37:37,679
to dues and give you the value of our
2599
01:37:37,679 --> 01:37:40,719
saved to do's
2600
01:37:40,880 --> 01:37:44,400
now let's go back into our web page
2601
01:37:44,400 --> 01:37:46,719
and let's refresh to load all our new
2602
01:37:46,719 --> 01:37:48,800
code and see what happens
2603
01:37:48,800 --> 01:37:50,880
so now when i delete
2604
01:37:50,880 --> 01:37:52,960
and i refresh the page you'll see that
2605
01:37:52,960 --> 01:37:55,040
we get the same list back it's not
2606
01:37:55,040 --> 01:37:57,920
resetting to the default list anymore
2607
01:37:57,920 --> 01:38:02,920
so now if i add another to-do
2608
01:38:03,199 --> 01:38:07,199
and then i close my browser
2609
01:38:07,199 --> 01:38:10,480
and let's say that i want to go back
2610
01:38:10,480 --> 01:38:12,800
and reopen it
2611
01:38:12,800 --> 01:38:15,280
so i double click it to reopen
2612
01:38:15,280 --> 01:38:16,880
you'll notice that we get the same list
2613
01:38:16,880 --> 01:38:19,040
back including the new to do that i
2614
01:38:19,040 --> 01:38:21,760
created so our save and retrieve feature
2615
01:38:21,760 --> 01:38:23,920
is working perfectly
2616
01:38:23,920 --> 01:38:25,520
now if we go back to the code i just
2617
01:38:25,520 --> 01:38:28,800
want to show you how this fits into mvc
2618
01:38:28,800 --> 01:38:30,719
so you notice that the save and retrieve
2619
01:38:30,719 --> 01:38:33,520
feature focuses on managing data so
2620
01:38:33,520 --> 01:38:35,280
that's why it goes into the model
2621
01:38:35,280 --> 01:38:37,840
section of our code
2622
01:38:37,840 --> 01:38:40,159
so for this we actually created a new
2623
01:38:40,159 --> 01:38:41,280
function
2624
01:38:41,280 --> 01:38:44,080
and whenever we modify our array we're
2625
01:38:44,080 --> 01:38:46,080
running this function
2626
01:38:46,080 --> 01:38:47,760
now the beauty of this is that when we
2627
01:38:47,760 --> 01:38:50,000
go into our controller code you can see
2628
01:38:50,000 --> 01:38:51,920
that the controller is not about
2629
01:38:51,920 --> 01:38:54,639
managing data so it still runs the
2630
01:38:54,639 --> 01:38:56,719
minimal code of we just want to create a
2631
01:38:56,719 --> 01:38:59,440
to-do we want to add the title and we
2632
01:38:59,440 --> 01:39:01,760
want to give it a due date but we don't
2633
01:39:01,760 --> 01:39:03,840
care what else whatever else happens we
2634
01:39:03,840 --> 01:39:06,000
just want to create a to-do and the
2635
01:39:06,000 --> 01:39:08,000
model section will take care of any
2636
01:39:08,000 --> 01:39:10,400
other details that we want to add but
2637
01:39:10,400 --> 01:39:12,639
the controller code is isolated from
2638
01:39:12,639 --> 01:39:13,440
that
2639
01:39:13,440 --> 01:39:15,199
it doesn't have to worry about any of
2640
01:39:15,199 --> 01:39:16,880
those details
2641
01:39:16,880 --> 01:39:19,119
so this makes our code really simple and
2642
01:39:19,119 --> 01:39:21,199
flexible we can keep adding more
2643
01:39:21,199 --> 01:39:23,600
features into this function such as data
2644
01:39:23,600 --> 01:39:26,400
validation or saving to a remote server
2645
01:39:26,400 --> 01:39:28,960
without having to complicate the rest of
2646
01:39:28,960 --> 01:39:32,199
our code
2647
01:39:42,080 --> 01:39:43,600
so that brings us to the end of our
2648
01:39:43,600 --> 01:39:45,679
tutorial and if you made it this far i
2649
01:39:45,679 --> 01:39:47,600
just want to say congratulations because
2650
01:39:47,600 --> 01:39:49,199
we learned a ton of features in
2651
01:39:49,199 --> 01:39:51,040
javascript and we built a fully
2652
01:39:51,040 --> 01:39:52,719
functional app
2653
01:39:52,719 --> 01:39:54,239
now before i close off i just want to
2654
01:39:54,239 --> 01:39:55,760
introduce some more aspects of the
2655
01:39:55,760 --> 01:39:57,440
language that'll help you when you go
2656
01:39:57,440 --> 01:39:59,600
out and learn on your own
2657
01:39:59,600 --> 01:40:00,960
so the first thing i want to introduce
2658
01:40:00,960 --> 01:40:03,040
is that we've been defining functions
2659
01:40:03,040 --> 01:40:04,080
like this
2660
01:40:04,080 --> 01:40:05,840
we have the function word and then we
2661
01:40:05,840 --> 01:40:07,440
have the function name
2662
01:40:07,440 --> 01:40:09,199
the parameters and the rest of the
2663
01:40:09,199 --> 01:40:10,320
function
2664
01:40:10,320 --> 01:40:11,840
well in javascript there's another way
2665
01:40:11,840 --> 01:40:13,600
of defining functions and it looks like
2666
01:40:13,600 --> 01:40:14,960
this
2667
01:40:14,960 --> 01:40:17,520
first we create a variable to store our
2668
01:40:17,520 --> 01:40:19,440
function
2669
01:40:19,440 --> 01:40:20,960
and the name of this variable is going
2670
01:40:20,960 --> 01:40:22,480
to be the function name
2671
01:40:22,480 --> 01:40:23,679
and then we're going to type out
2672
01:40:23,679 --> 01:40:24,800
function
2673
01:40:24,800 --> 01:40:29,199
parameters and the body of the function
2674
01:40:29,199 --> 01:40:31,040
so this is another way of defining a
2675
01:40:31,040 --> 01:40:33,440
function in javascript and this is the
2676
01:40:33,440 --> 01:40:36,639
same thing as doing this what you'll
2677
01:40:36,639 --> 01:40:39,040
notice here is that a function is just
2678
01:40:39,040 --> 01:40:41,600
another type of value in javascript just
2679
01:40:41,600 --> 01:40:42,960
like a string
2680
01:40:42,960 --> 01:40:46,560
so if we go back to our web page
2681
01:40:46,560 --> 01:40:49,119
and we refresh the page
2682
01:40:49,119 --> 01:40:50,800
and we type
2683
01:40:50,800 --> 01:40:52,320
type of
2684
01:40:52,320 --> 01:40:54,320
add to do
2685
01:40:54,320 --> 01:40:55,520
2
2686
01:40:55,520 --> 01:40:57,679
you'll notice that javascript tells us
2687
01:40:57,679 --> 01:41:00,960
this has the type of function
2688
01:41:00,960 --> 01:41:02,960
so a function is just another type of
2689
01:41:02,960 --> 01:41:06,000
value in javascript and because of that
2690
01:41:06,000 --> 01:41:07,760
everything that you can do with for
2691
01:41:07,760 --> 01:41:09,760
example a string you can also do it with
2692
01:41:09,760 --> 01:41:10,880
a function
2693
01:41:10,880 --> 01:41:12,000
so we can
2694
01:41:12,000 --> 01:41:14,480
save a function to a variable
2695
01:41:14,480 --> 01:41:17,360
we can we can pass a function
2696
01:41:17,360 --> 01:41:20,800
into another function like this
2697
01:41:20,800 --> 01:41:22,560
and the third thing we can do is we can
2698
01:41:22,560 --> 01:41:23,600
actually
2699
01:41:23,600 --> 01:41:25,760
return a function
2700
01:41:25,760 --> 01:41:28,320
from a function
2701
01:41:28,320 --> 01:41:30,159
now this might look a little strange to
2702
01:41:30,159 --> 01:41:32,560
you because we have a function inside
2703
01:41:32,560 --> 01:41:35,040
another function but remember that this
2704
01:41:35,040 --> 01:41:36,960
is just another type of value like a
2705
01:41:36,960 --> 01:41:38,000
string
2706
01:41:38,000 --> 01:41:39,679
and anything that you can do with a
2707
01:41:39,679 --> 01:41:42,719
string you can do with a function
2708
01:41:42,719 --> 01:41:44,800
so in a newer version of javascript a
2709
01:41:44,800 --> 01:41:46,880
new way of creating a function was
2710
01:41:46,880 --> 01:41:48,880
introduced and that is called the arrow
2711
01:41:48,880 --> 01:41:51,040
function
2712
01:41:51,040 --> 01:41:52,719
so instead of writing the word function
2713
01:41:52,719 --> 01:41:54,960
here we're going to use
2714
01:41:54,960 --> 01:41:56,239
an arrow
2715
01:41:56,239 --> 01:41:59,280
and for now this is the same thing as
2716
01:41:59,280 --> 01:42:02,880
defining a function like this
2717
01:42:02,880 --> 01:42:04,719
so there's a few differences between a
2718
01:42:04,719 --> 01:42:07,119
regular function and an arrow function
2719
01:42:07,119 --> 01:42:08,960
the first thing is that it's a little
2720
01:42:08,960 --> 01:42:11,679
easier to read so that's why people now
2721
01:42:11,679 --> 01:42:13,600
prefer to use arrow functions over
2722
01:42:13,600 --> 01:42:16,400
regular functions to demonstrate if we
2723
01:42:16,400 --> 01:42:18,320
go into google
2724
01:42:18,320 --> 01:42:21,040
and we search for array filter
2725
01:42:21,040 --> 01:42:23,280
javascript to learn how to use an array
2726
01:42:23,280 --> 01:42:25,360
filter
2727
01:42:25,360 --> 01:42:26,239
and
2728
01:42:26,239 --> 01:42:28,000
if we look at the examples you'll notice
2729
01:42:28,000 --> 01:42:30,080
that they're using an arrow function
2730
01:42:30,080 --> 01:42:32,800
instead of a regular function
2731
01:42:32,800 --> 01:42:34,480
now the way that an arrow function works
2732
01:42:34,480 --> 01:42:36,639
is that in here you still have your
2733
01:42:36,639 --> 01:42:38,639
parameters like to do
2734
01:42:38,639 --> 01:42:40,800
and title
2735
01:42:40,800 --> 01:42:44,239
etc but if you have just one parameter
2736
01:42:44,239 --> 01:42:47,119
these brackets are optional
2737
01:42:47,119 --> 01:42:48,800
so if we go back
2738
01:42:48,800 --> 01:42:50,159
we'll notice that
2739
01:42:50,159 --> 01:42:51,760
they have one parameter here and the
2740
01:42:51,760 --> 01:42:53,600
brackets are optional so they chose to
2741
01:42:53,600 --> 01:42:54,840
leave it
2742
01:42:54,840 --> 01:42:58,000
out and the other difference is that if
2743
01:42:58,000 --> 01:43:01,600
the function is on one line like this
2744
01:43:01,600 --> 01:43:05,199
where the code is right after the arrow
2745
01:43:05,199 --> 01:43:08,159
then there's actually a return
2746
01:43:08,159 --> 01:43:11,440
keyword before this code
2747
01:43:11,440 --> 01:43:13,280
but in an arrow function this return
2748
01:43:13,280 --> 01:43:16,480
keyword is hidden so this is the same
2749
01:43:16,480 --> 01:43:18,800
thing as saying return
2750
01:43:18,800 --> 01:43:22,080
if word.length is greater than six
2751
01:43:22,080 --> 01:43:25,040
so if we go back here notice how we have
2752
01:43:25,040 --> 01:43:27,360
a return string here instead of writing
2753
01:43:27,360 --> 01:43:29,920
this we cannot put it all on one line
2754
01:43:29,920 --> 01:43:32,480
so we can just say string
2755
01:43:32,480 --> 01:43:35,600
and this means the same thing as return
2756
01:43:35,600 --> 01:43:39,520
string because everything is on one line
2757
01:43:39,520 --> 01:43:41,760
so we don't need that return statement
2758
01:43:41,760 --> 01:43:44,480
if we go back into our app to test it
2759
01:43:44,480 --> 01:43:46,639
out
2760
01:43:47,760 --> 01:43:50,719
and we have to refresh to load that code
2761
01:43:50,719 --> 01:43:52,800
now if we run our
2762
01:43:52,800 --> 01:43:56,320
add to do 2 function
2763
01:43:56,960 --> 01:43:59,600
you'll notice that it returns
2764
01:43:59,600 --> 01:44:01,440
the string without us having to write a
2765
01:44:01,440 --> 01:44:03,440
return statement
2766
01:44:03,440 --> 01:44:05,119
so arrow functions are a little bit
2767
01:44:05,119 --> 01:44:07,040
easier to read and they offer some
2768
01:44:07,040 --> 01:44:09,360
shortcuts for us when writing our code
2769
01:44:09,360 --> 01:44:11,280
and that's why people prefer it
2770
01:44:11,280 --> 01:44:14,239
over the old way of defining a function
2771
01:44:14,239 --> 01:44:16,000
and if you're looking for documentation
2772
01:44:16,000 --> 01:44:17,040
online you're going to see arrow
2773
01:44:17,040 --> 01:44:19,040
functions everywhere the old way of
2774
01:44:19,040 --> 01:44:21,199
writing a function is used in very
2775
01:44:21,199 --> 01:44:23,600
specific situations and for now we're
2776
01:44:23,600 --> 01:44:25,199
not going to explore those because those
2777
01:44:25,199 --> 01:44:29,480
are out of the scope for this tutorial
2778
01:44:39,760 --> 01:44:41,760
that's the end of this video you did
2779
01:44:41,760 --> 01:44:44,000
awesome we learned a bunch of javascript
2780
01:44:44,000 --> 01:44:45,760
features today and we use them to build
2781
01:44:45,760 --> 01:44:48,400
a fully functional app another thing we
2782
01:44:48,400 --> 01:44:50,239
can do is to learn how to make our app
2783
01:44:50,239 --> 01:44:51,920
look a lot nicer and that's going to
2784
01:44:51,920 --> 01:44:55,119
require some more html and css code now
2785
01:44:55,119 --> 01:44:57,679
html and css are not in scope for this
2786
01:44:57,679 --> 01:44:59,520
javascript course but if you want to
2787
01:44:59,520 --> 01:45:01,280
learn more about those languages let me
2788
01:45:01,280 --> 01:45:02,880
know in the comments and subscribe for
2789
01:45:02,880 --> 01:45:05,920
the latest videos and lessons i'm simon
2790
01:45:05,920 --> 01:45:07,600
from supersimple.dev
2791
01:45:07,600 --> 01:45:09,119
thanks again for watching and i'll see
2792
01:45:09,119 --> 01:45:12,080
you in the next one
188532
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.