All language subtitles for JavaScript Tutorial Full Course - Beginner to Pro - English -generated)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,719 --> 00:00:04,759 welcome to the complete JavaScript 2 00:00:02,679 --> 00:00:06,839 course in this course we're going to 3 00:00:04,759 --> 00:00:09,519 learn how to build complex websites 4 00:00:06,839 --> 00:00:11,799 using JavaScript from a beginner to a 5 00:00:09,519 --> 00:00:14,360 professional level and by the end of 6 00:00:11,799 --> 00:00:17,320 this course we're going to build 7 00:00:14,360 --> 00:00:20,279 amazon.com so this is an interactive 8 00:00:17,320 --> 00:00:24,400 multi-page website where we can add 9 00:00:20,279 --> 00:00:26,760 products to our cart create an order and 10 00:00:24,400 --> 00:00:29,359 even track our 11 00:00:26,760 --> 00:00:32,200 order along the way we're also going to 12 00:00:29,359 --> 00:00:36,600 build some smaller projects like a rock 13 00:00:32,200 --> 00:00:38,680 paper scissors game a to-do list and a 14 00:00:36,600 --> 00:00:41,079 calculator now you don't need any 15 00:00:38,680 --> 00:00:43,879 previous coding or technical experience 16 00:00:41,079 --> 00:00:45,920 to follow along this course will cover 17 00:00:43,879 --> 00:00:48,000 everything that you need to learn 18 00:00:45,920 --> 00:00:50,120 JavaScript we're going to start from the 19 00:00:48,000 --> 00:00:53,039 very basics of JavaScript and we're 20 00:00:50,120 --> 00:00:54,680 going to build our way up step by step 21 00:00:53,039 --> 00:00:57,079 we're going to learn the major features 22 00:00:54,680 --> 00:00:59,039 of JavaScript and also how to use 23 00:00:57,079 --> 00:01:02,719 JavaScript together with other 24 00:00:59,039 --> 00:01:05,199 Technologies like HTML and 25 00:01:02,719 --> 00:01:08,280 CSS we're also going to learn more 26 00:01:05,199 --> 00:01:12,479 advanced features like objectoriented 27 00:01:08,280 --> 00:01:15,240 programming backend callbacks promises 28 00:01:12,479 --> 00:01:18,000 and async await and we'll use these 29 00:01:15,240 --> 00:01:20,759 features together to build a complex 30 00:01:18,000 --> 00:01:23,200 project which is the Amazon 31 00:01:20,759 --> 00:01:26,920 Project this will let you practice what 32 00:01:23,200 --> 00:01:28,960 you learn and deepen your 33 00:01:26,920 --> 00:01:30,840 understanding you can find the different 34 00:01:28,960 --> 00:01:32,399 lessons of this course course below the 35 00:01:30,840 --> 00:01:34,479 video 36 00:01:32,399 --> 00:01:37,320 here and 37 00:01:34,479 --> 00:01:40,320 here if you need to you can adjust the 38 00:01:37,320 --> 00:01:43,000 speed of the video 39 00:01:40,320 --> 00:01:45,360 here and after each lesson I'm going to 40 00:01:43,000 --> 00:01:47,640 give you a bunch of exercises that you 41 00:01:45,360 --> 00:01:50,640 can do on your own to practice the 42 00:01:47,640 --> 00:01:52,719 skills that we learned in total this 43 00:01:50,640 --> 00:01:56,560 course will have over 44 00:01:52,719 --> 00:01:59,719 250 exercises so with that said let's 45 00:01:56,560 --> 00:01:59,719 get started 46 00:02:04,159 --> 00:02:08,720 before we begin if something in this 47 00:02:06,200 --> 00:02:10,920 course is not working properly make sure 48 00:02:08,720 --> 00:02:14,400 to check the first comment below the 49 00:02:10,920 --> 00:02:14,400 video for a list of 50 00:02:18,920 --> 00:02:23,400 solutions I'm going to be doing this 51 00:02:20,879 --> 00:02:26,080 course on Windows but the steps are the 52 00:02:23,400 --> 00:02:28,000 same if you're using a Mac we're going 53 00:02:26,080 --> 00:02:31,440 to start from the very basics of 54 00:02:28,000 --> 00:02:35,640 JavaScript and build our way up step by 55 00:02:31,440 --> 00:02:37,760 step first of all what is Javascript 56 00:02:35,640 --> 00:02:41,640 JavaScript is a technology that we use 57 00:02:37,760 --> 00:02:43,720 to create websites a website is like 58 00:02:41,640 --> 00:02:46,480 youtube.com or 59 00:02:43,720 --> 00:02:49,920 amazon.com it's how we access YouTube 60 00:02:46,480 --> 00:02:53,720 and Amazon on our computer if we were 61 00:02:49,920 --> 00:02:56,400 using a phone we usually use an app but 62 00:02:53,720 --> 00:02:58,560 on our computer we use a 63 00:02:56,400 --> 00:03:00,879 website so there's actually three 64 00:02:58,560 --> 00:03:06,159 technologies that we use to create 65 00:03:00,879 --> 00:03:08,080 websites HTML CSS and JavaScript each of 66 00:03:06,159 --> 00:03:12,200 these Technologies has a specific 67 00:03:08,080 --> 00:03:14,879 purpose when creating a website HTML 68 00:03:12,200 --> 00:03:18,200 creates the content of a website it 69 00:03:14,879 --> 00:03:21,879 creates all the buttons the text and the 70 00:03:18,200 --> 00:03:25,080 images CSS changes the appearance of the 71 00:03:21,879 --> 00:03:28,959 website by adding CSS we can make our 72 00:03:25,080 --> 00:03:30,439 website look nice and finally JavaScript 73 00:03:28,959 --> 00:03:34,000 makes our website 74 00:03:30,439 --> 00:03:37,080 interactive so if we only have HTML and 75 00:03:34,000 --> 00:03:40,000 CSS and we click one of these buttons 76 00:03:37,080 --> 00:03:42,080 our website doesn't do anything but once 77 00:03:40,000 --> 00:03:44,560 we add JavaScript we can make our 78 00:03:42,080 --> 00:03:48,280 website do something when we click a 79 00:03:44,560 --> 00:03:50,640 button such as add a product to our cart 80 00:03:48,280 --> 00:03:54,120 or create an 81 00:03:50,640 --> 00:03:57,079 order so this course is focused on 82 00:03:54,120 --> 00:04:00,000 JavaScript if you want to learn HTML and 83 00:03:57,079 --> 00:04:02,079 CSS in more detail I have a full whole 84 00:04:00,000 --> 00:04:03,720 course just like this one that you can 85 00:04:02,079 --> 00:04:06,879 find in the video 86 00:04:03,720 --> 00:04:09,239 description let's get started with 87 00:04:06,879 --> 00:04:11,400 JavaScript the first thing we need to do 88 00:04:09,239 --> 00:04:15,000 is to install a piece of software called 89 00:04:11,400 --> 00:04:17,959 a web browser a web browser lets us view 90 00:04:15,000 --> 00:04:19,799 websites that we create in this course 91 00:04:17,959 --> 00:04:22,400 the most popular web browser for web 92 00:04:19,799 --> 00:04:25,080 development is Google Chrome so I 93 00:04:22,400 --> 00:04:27,360 recommend using that if you don't have 94 00:04:25,080 --> 00:04:30,160 Google Chrome you can open the default 95 00:04:27,360 --> 00:04:33,400 web browser on your computer for windows 96 00:04:30,160 --> 00:04:37,400 it's Microsoft Edge and for Mac it's 97 00:04:33,400 --> 00:04:41,759 Safari type in google.com at the top and 98 00:04:37,400 --> 00:04:45,360 press enter search for Google 99 00:04:41,759 --> 00:04:48,759 Chrome click the result and follow the 100 00:04:45,360 --> 00:04:50,720 instructions to download and install 101 00:04:48,759 --> 00:04:51,919 once we've installed Chrome we're ready 102 00:04:50,720 --> 00:04:54,639 to 103 00:04:51,919 --> 00:04:57,280 begin we're going to start by learning 104 00:04:54,639 --> 00:05:00,080 what exactly is Javascript and how does 105 00:04:57,280 --> 00:05:02,759 it work the easiest way to understand 106 00:05:00,080 --> 00:05:05,199 JavaScript is we're basically giving 107 00:05:02,759 --> 00:05:07,400 instructions to a computer and then the 108 00:05:05,199 --> 00:05:09,320 computer follows our 109 00:05:07,400 --> 00:05:11,919 instructions I find the best way to 110 00:05:09,320 --> 00:05:17,240 learn this is to jump in and give it a 111 00:05:11,919 --> 00:05:20,199 try so first let's open Google 112 00:05:17,240 --> 00:05:21,199 Chrome next we're going to click at the 113 00:05:20,199 --> 00:05:24,319 top 114 00:05:21,199 --> 00:05:25,919 here and type 115 00:05:24,319 --> 00:05:27,479 super 116 00:05:25,919 --> 00:05:30,560 simple 117 00:05:27,479 --> 00:05:35,000 dodev slj 118 00:05:30,560 --> 00:05:37,440 s- Basics and press 119 00:05:35,000 --> 00:05:40,919 enter and now we'll learn how to give 120 00:05:37,440 --> 00:05:43,560 our computer some instructions to follow 121 00:05:40,919 --> 00:05:46,759 to do that we're going to go to a blank 122 00:05:43,560 --> 00:05:49,560 area of the web page we're going to 123 00:05:46,759 --> 00:05:53,319 rightclick click 124 00:05:49,560 --> 00:05:56,039 inspect and then click the console 125 00:05:53,319 --> 00:05:58,840 tab now if your console doesn't show up 126 00:05:56,039 --> 00:06:01,400 at the bottom like this you can click 127 00:05:58,840 --> 00:06:03,639 these three dots here here and then 128 00:06:01,400 --> 00:06:07,280 select this 129 00:06:03,639 --> 00:06:10,240 icon okay so inside the console we can 130 00:06:07,280 --> 00:06:13,160 give some instructions for our computer 131 00:06:10,240 --> 00:06:15,639 let's learn our first instruction we're 132 00:06:13,160 --> 00:06:17,240 going to type here and we're going to 133 00:06:15,639 --> 00:06:22,720 type 134 00:06:17,240 --> 00:06:26,080 alert Open Bracket single quote hello 135 00:06:22,720 --> 00:06:28,440 single quote close bracket and 136 00:06:26,080 --> 00:06:31,479 semicolon make sure to type this out 137 00:06:28,440 --> 00:06:33,400 exactly as I have it here in JavaScript 138 00:06:31,479 --> 00:06:37,160 if you don't type things out correctly 139 00:06:33,400 --> 00:06:40,160 it won't work so this is an instruction 140 00:06:37,160 --> 00:06:43,039 to create a popup with the text hello 141 00:06:40,160 --> 00:06:46,080 inside to get the computer to follow our 142 00:06:43,039 --> 00:06:48,840 instruction we can just press 143 00:06:46,080 --> 00:06:51,759 enter and as you can see the computer 144 00:06:48,840 --> 00:06:55,080 does exactly what we tell it to do it 145 00:06:51,759 --> 00:06:57,919 creates a popup with a text hello 146 00:06:55,080 --> 00:07:00,840 inside now let's press 147 00:06:57,919 --> 00:07:03,160 okay and let's give another instruction 148 00:07:00,840 --> 00:07:07,160 to the computer so we're going to type 149 00:07:03,160 --> 00:07:11,039 here again and we're going to type alert 150 00:07:07,160 --> 00:07:13,000 again Open Bracket single quote but this 151 00:07:11,039 --> 00:07:14,560 time we're going to type a different 152 00:07:13,000 --> 00:07:20,160 message in the 153 00:07:14,560 --> 00:07:22,039 popup good job and single quote close 154 00:07:20,160 --> 00:07:24,879 bracket and 155 00:07:22,039 --> 00:07:27,800 semicolon so this is an instruction to 156 00:07:24,879 --> 00:07:31,360 create a popup again but this time with 157 00:07:27,800 --> 00:07:32,479 a text good job in inside the popup if 158 00:07:31,360 --> 00:07:35,280 we press 159 00:07:32,479 --> 00:07:37,840 enter the computer does exactly what we 160 00:07:35,280 --> 00:07:41,240 tell it to do and it creates a popup 161 00:07:37,840 --> 00:07:43,440 with the text good job inside and that's 162 00:07:41,240 --> 00:07:46,000 basically the main idea behind 163 00:07:43,440 --> 00:07:48,800 JavaScript we're giving instructions to 164 00:07:46,000 --> 00:07:49,960 a computer and then the computer follows 165 00:07:48,800 --> 00:07:52,520 our 166 00:07:49,960 --> 00:07:55,599 instructions so note that we can open 167 00:07:52,520 --> 00:07:58,039 and use this console on any website 168 00:07:55,599 --> 00:07:59,639 we're just using superimple dodev as an 169 00:07:58,039 --> 00:08:01,159 example 170 00:07:59,639 --> 00:08:03,360 let's press 171 00:08:01,159 --> 00:08:06,800 okay and we're going to learn some 172 00:08:03,360 --> 00:08:06,800 terminology or 173 00:08:07,520 --> 00:08:13,159 naming the instructions we give to our 174 00:08:10,319 --> 00:08:15,759 computer these instructions are called 175 00:08:13,159 --> 00:08:18,319 code when we press enter and the 176 00:08:15,759 --> 00:08:21,639 computer follows our instructions this 177 00:08:18,319 --> 00:08:23,960 is called running the code now there are 178 00:08:21,639 --> 00:08:27,440 many different languages we can use to 179 00:08:23,960 --> 00:08:29,240 write code called programming languages 180 00:08:27,440 --> 00:08:31,519 it's similar to how we have many human 181 00:08:29,240 --> 00:08:33,959 Lang languages that we can use to 182 00:08:31,519 --> 00:08:38,719 communicate some examples of programming 183 00:08:33,959 --> 00:08:41,440 languages are JavaScript Python and Java 184 00:08:38,719 --> 00:08:44,039 so here we're writing our code using the 185 00:08:41,440 --> 00:08:46,240 JavaScript programming language so we 186 00:08:44,039 --> 00:08:49,279 call this JavaScript 187 00:08:46,240 --> 00:08:51,279 code now that we understand the basic 188 00:08:49,279 --> 00:08:53,480 terminology let's look at some more 189 00:08:51,279 --> 00:08:56,800 examples of what we can do with 190 00:08:53,480 --> 00:08:57,880 JavaScript code another thing we can do 191 00:08:56,800 --> 00:09:00,920 is 192 00:08:57,880 --> 00:09:07,760 math let's go back to the console and 193 00:09:00,920 --> 00:09:10,760 type here and we're going to type 2 + 2 194 00:09:07,760 --> 00:09:13,360 so this is an instruction or code that 195 00:09:10,760 --> 00:09:17,399 tells the computer to do some math to 196 00:09:13,360 --> 00:09:19,560 calculate 2 + 2 when we press 197 00:09:17,399 --> 00:09:22,760 enter the computer follows our 198 00:09:19,560 --> 00:09:26,440 instruction or runs this code calculates 199 00:09:22,760 --> 00:09:29,800 2+ 2 and gives us the result which is 200 00:09:26,440 --> 00:09:35,279 four let's do another example we're 201 00:09:29,800 --> 00:09:38,959 going to type 10 minus 3 and press enter 202 00:09:35,279 --> 00:09:41,560 to run this code again the computer does 203 00:09:38,959 --> 00:09:44,720 this math calculates the result and 204 00:09:41,560 --> 00:09:47,200 gives us seven so as you can see 205 00:09:44,720 --> 00:09:51,079 JavaScript can do many different things 206 00:09:47,200 --> 00:09:52,959 like create popups and do math let's 207 00:09:51,079 --> 00:09:55,839 look at one more example of what 208 00:09:52,959 --> 00:10:01,000 JavaScript can do we're going to go to 209 00:09:55,839 --> 00:10:01,000 the bottom and type out this code 210 00:10:02,880 --> 00:10:13,320 document.body do inner Capital 211 00:10:08,120 --> 00:10:15,160 HTML equals single quilt hello single 212 00:10:13,320 --> 00:10:17,360 quilt and 213 00:10:15,160 --> 00:10:20,160 semicolon make sure to write this out 214 00:10:17,360 --> 00:10:22,120 exactly as I have it here with the dots 215 00:10:20,160 --> 00:10:24,120 and the capital letters because 216 00:10:22,120 --> 00:10:27,760 JavaScript is case 217 00:10:24,120 --> 00:10:30,480 sensitive let's press enter to run this 218 00:10:27,760 --> 00:10:32,920 code what this code does is that it 219 00:10:30,480 --> 00:10:36,639 removes everything on the page and 220 00:10:32,920 --> 00:10:39,880 replaces it with the text hello in this 221 00:10:36,639 --> 00:10:42,800 example we use JavaScript to modify the 222 00:10:39,880 --> 00:10:44,839 web page modifying the web page is one 223 00:10:42,800 --> 00:10:46,839 of the most important features of 224 00:10:44,839 --> 00:10:49,639 JavaScript and we're going to be using 225 00:10:46,839 --> 00:10:52,279 this feature throughout this course and 226 00:10:49,639 --> 00:10:55,000 that's the basics of JavaScript we're 227 00:10:52,279 --> 00:10:57,600 giving instructions to a computer these 228 00:10:55,000 --> 00:10:59,320 instructions are called code and then 229 00:10:57,600 --> 00:11:02,200 the computer follows our instru 230 00:10:59,320 --> 00:11:04,839 instuctions this is called running the 231 00:11:02,200 --> 00:11:07,480 code and also don't worry about 232 00:11:04,839 --> 00:11:09,680 memorizing all this code you can find a 233 00:11:07,480 --> 00:11:13,360 copy of all the code that we write in 234 00:11:09,680 --> 00:11:13,360 this course in the video 235 00:11:14,760 --> 00:11:19,680 description now that we understand the 236 00:11:17,120 --> 00:11:22,079 basics of JavaScript let's learn more 237 00:11:19,680 --> 00:11:26,079 about the JavaScript language 238 00:11:22,079 --> 00:11:29,440 itself if we take a look at this code 239 00:11:26,079 --> 00:11:33,240 alert and hello you might be wondering 240 00:11:29,440 --> 00:11:35,000 ing what the brackets and the quote mean 241 00:11:33,240 --> 00:11:37,279 all of these characters have a special 242 00:11:35,000 --> 00:11:38,680 meaning in the language this is 243 00:11:37,279 --> 00:11:41,560 something called 244 00:11:38,680 --> 00:11:43,839 syntax syntax are the rules that we have 245 00:11:41,560 --> 00:11:46,839 to follow when using a programming 246 00:11:43,839 --> 00:11:49,680 language like JavaScript this is sort of 247 00:11:46,839 --> 00:11:52,880 like in English we also have rules that 248 00:11:49,680 --> 00:11:54,800 we follow called grammar the difference 249 00:11:52,880 --> 00:11:57,560 is that in English we don't have to 250 00:11:54,800 --> 00:12:00,079 follow the rules of grammar perfectly 251 00:11:57,560 --> 00:12:02,519 people can still understand us but in 252 00:12:00,079 --> 00:12:05,839 programming we have to follow the rules 253 00:12:02,519 --> 00:12:09,839 of syntax exactly otherwise the computer 254 00:12:05,839 --> 00:12:13,839 won't understand our code for example if 255 00:12:09,839 --> 00:12:19,440 we scroll down and we type this code 256 00:12:13,839 --> 00:12:22,480 alert Open Bracket quote hello quote 257 00:12:19,440 --> 00:12:25,880 close bracket and semicolon but this 258 00:12:22,480 --> 00:12:27,240 time we remove the first bracket and 259 00:12:25,880 --> 00:12:30,920 press 260 00:12:27,240 --> 00:12:33,519 enter it will give us a syntax error 261 00:12:30,920 --> 00:12:36,360 we're not following the rules of syntax 262 00:12:33,519 --> 00:12:39,560 so the computer doesn't understand this 263 00:12:36,360 --> 00:12:41,880 code now JavaScript has a lot of syntax 264 00:12:39,560 --> 00:12:44,720 rules and we can't learn them all at 265 00:12:41,880 --> 00:12:47,880 once right now instead we're going to 266 00:12:44,720 --> 00:12:50,760 learn the syntax rules one by one as we 267 00:12:47,880 --> 00:12:53,440 go through the course for now we'll just 268 00:12:50,760 --> 00:12:56,399 learn enough syntax to do the exercises 269 00:12:53,440 --> 00:13:00,279 at the end of this lesson what we need 270 00:12:56,399 --> 00:13:02,399 to know is that if we scroll up 271 00:13:00,279 --> 00:13:06,000 this part of the code tells a computer 272 00:13:02,399 --> 00:13:09,880 to create a popup the text between these 273 00:13:06,000 --> 00:13:12,399 quotes is the text inside the popup if 274 00:13:09,880 --> 00:13:15,760 we change this text we can change the 275 00:13:12,399 --> 00:13:17,240 text inside the popup same thing for 276 00:13:15,760 --> 00:13:20,000 this code down 277 00:13:17,240 --> 00:13:22,320 here if we change the text between the 278 00:13:20,000 --> 00:13:24,959 quotes it will change the text that 279 00:13:22,320 --> 00:13:27,480 appears on the page and that's all we 280 00:13:24,959 --> 00:13:30,199 need to know about JavaScript Syntax for 281 00:13:27,480 --> 00:13:32,800 now we'll learn exact ly what the other 282 00:13:30,199 --> 00:13:36,680 characters like the brackets quotes and 283 00:13:32,800 --> 00:13:38,680 semicolon mean later on in this course 284 00:13:36,680 --> 00:13:40,880 so in this lesson we learned that 285 00:13:38,680 --> 00:13:44,040 JavaScript is basically giving 286 00:13:40,880 --> 00:13:47,120 instructions to a computer we wrote our 287 00:13:44,040 --> 00:13:49,600 first few lines of JavaScript code we 288 00:13:47,120 --> 00:13:52,320 learned how to run our code using the 289 00:13:49,600 --> 00:13:55,160 console in our web browser and we 290 00:13:52,320 --> 00:13:57,480 learned about syntax which are the rules 291 00:13:55,160 --> 00:14:00,480 that we have to follow when writing our 292 00:13:57,480 --> 00:14:00,480 code 293 00:14:01,000 --> 00:14:05,120 thanks for watching this course so far 294 00:14:03,360 --> 00:14:07,199 if you want to support this channel you 295 00:14:05,120 --> 00:14:09,440 can check out the premium version of my 296 00:14:07,199 --> 00:14:11,880 courses just like this one where you can 297 00:14:09,440 --> 00:14:14,160 get a certificate of completion and a 298 00:14:11,880 --> 00:14:16,920 Better Learning platform if you're 299 00:14:14,160 --> 00:14:19,480 interested in extra videos from me more 300 00:14:16,920 --> 00:14:22,199 in-depth videos and behind the scenes 301 00:14:19,480 --> 00:14:24,880 content you can check out these links 302 00:14:22,199 --> 00:14:27,759 here you can also support the channel by 303 00:14:24,880 --> 00:14:29,639 liking the video subscribing and sharing 304 00:14:27,759 --> 00:14:33,120 this video with others 305 00:14:29,639 --> 00:14:35,639 thanks again and let's continue with the 306 00:14:33,120 --> 00:14:38,000 course here are some exercises you can 307 00:14:35,639 --> 00:14:40,320 try on your own to practice what we 308 00:14:38,000 --> 00:14:42,800 learned in this 309 00:14:40,320 --> 00:14:46,560 lesson you can find the solutions to 310 00:14:42,800 --> 00:14:46,560 these exercises in the video 311 00:14:52,480 --> 00:14:56,880 description now that we understand the 312 00:14:54,839 --> 00:14:59,120 basics of JavaScript we're going to 313 00:14:56,880 --> 00:15:01,600 learn about each feature of the language 314 00:14:59,120 --> 00:15:05,800 language in this lesson we're going to 315 00:15:01,600 --> 00:15:07,839 focus on numbers and math first we can 316 00:15:05,800 --> 00:15:10,800 close the console for 317 00:15:07,839 --> 00:15:13,800 now so I've actually prepared a project 318 00:15:10,800 --> 00:15:15,839 that we can use for this lesson to open 319 00:15:13,800 --> 00:15:20,839 this project we're going to go to our 320 00:15:15,839 --> 00:15:23,839 browser click up here and type 321 00:15:20,839 --> 00:15:26,639 super simple 322 00:15:23,839 --> 00:15:29,839 dodev SL 323 00:15:26,639 --> 00:15:31,839 projects SL Amazon 324 00:15:29,839 --> 00:15:34,560 and press 325 00:15:31,839 --> 00:15:36,800 enter so this is actually the final 326 00:15:34,560 --> 00:15:40,079 project we're going to create in this 327 00:15:36,800 --> 00:15:42,920 course this is an e-commerce website 328 00:15:40,079 --> 00:15:45,600 similar to Amazon where we can add 329 00:15:42,920 --> 00:15:48,480 products to our cart and then view our 330 00:15:45,600 --> 00:15:51,519 cart by clicking the top 331 00:15:48,480 --> 00:15:55,160 right and on this page we can see the 332 00:15:51,519 --> 00:15:56,480 products in our cart modify our cart and 333 00:15:55,160 --> 00:15:59,120 create an 334 00:15:56,480 --> 00:16:01,800 order for this lesson we're going to 335 00:15:59,120 --> 00:16:04,279 focus on this right section which 336 00:16:01,800 --> 00:16:07,240 calculates the cost of our cart 337 00:16:04,279 --> 00:16:09,199 including the cost of the products 338 00:16:07,240 --> 00:16:12,000 shipping and 339 00:16:09,199 --> 00:16:15,000 taxes this section is a great way to 340 00:16:12,000 --> 00:16:18,120 learn numbers and math in 341 00:16:15,000 --> 00:16:21,240 JavaScript first let's open the console 342 00:16:18,120 --> 00:16:23,480 again so we can write JavaScript code 343 00:16:21,240 --> 00:16:27,920 we're going to find a blank area of the 344 00:16:23,480 --> 00:16:31,440 web page rightclick click 345 00:16:27,920 --> 00:16:34,240 inspect and then click the 346 00:16:31,440 --> 00:16:37,199 console so we already saw some examples 347 00:16:34,240 --> 00:16:43,399 of math in the previous lesson for 348 00:16:37,199 --> 00:16:44,399 example we wrote the code 2 + 2 if we 349 00:16:43,399 --> 00:16:47,199 press 350 00:16:44,399 --> 00:16:49,680 enter the computer adds these two 351 00:16:47,199 --> 00:16:56,360 numbers and results in 352 00:16:49,680 --> 00:16:59,319 four we also did 10 minus 3 if we press 353 00:16:56,360 --> 00:17:01,600 enter the computer subtracts these two 354 00:16:59,319 --> 00:17:04,600 numbers and gives us 355 00:17:01,600 --> 00:17:07,919 seven in addition to adding and 356 00:17:04,600 --> 00:17:11,760 subtracting we can also multiply and 357 00:17:07,919 --> 00:17:16,000 divide to multiply we're going to type 358 00:17:11,760 --> 00:17:19,760 10 star 3 and press 359 00:17:16,000 --> 00:17:22,480 enter so star is the character for 360 00:17:19,760 --> 00:17:23,880 multiplying to divide we're going to 361 00:17:22,480 --> 00:17:25,880 type 362 00:17:23,880 --> 00:17:29,120 10 363 00:17:25,880 --> 00:17:32,960 slash2 and press enter 364 00:17:29,120 --> 00:17:35,520 so forward slash is the character for 365 00:17:32,960 --> 00:17:39,160 dividing now let's learn the syntax 366 00:17:35,520 --> 00:17:41,559 rules for numbers and math the Syntax 367 00:17:39,160 --> 00:17:44,200 for math is pretty straightforward in 368 00:17:41,559 --> 00:17:47,760 JavaScript we just write it out like 369 00:17:44,200 --> 00:17:51,640 normal math we can also do math with 370 00:17:47,760 --> 00:17:58,559 more than two numbers for example we can 371 00:17:51,640 --> 00:18:01,840 add two plus 2 and then plus another two 372 00:17:58,559 --> 00:18:04,360 if if we press enter the computer will 373 00:18:01,840 --> 00:18:08,640 add these three numbers together and 374 00:18:04,360 --> 00:18:12,360 result in six and finally JavaScript can 375 00:18:08,640 --> 00:18:15,360 handle decimal numbers like 376 00:18:12,360 --> 00:18:19,440 2.2 plus 377 00:18:15,360 --> 00:18:23,640 2.2 if we press enter it will calculate 378 00:18:19,440 --> 00:18:27,360 these decimal numbers and result in 379 00:18:23,640 --> 00:18:28,760 4.4 now let's do some practical examples 380 00:18:27,360 --> 00:18:31,960 we're going to calculate the number 381 00:18:28,760 --> 00:18:34,720 numers here in our final project but 382 00:18:31,960 --> 00:18:37,840 first let's set up our cart so we have 383 00:18:34,720 --> 00:18:42,000 the same products in our cart so make 384 00:18:37,840 --> 00:18:45,799 sure in your cart you have two socks 385 00:18:42,000 --> 00:18:48,039 with free shipping and you have one 386 00:18:45,799 --> 00:18:51,520 basketball with 387 00:18:48,039 --> 00:18:55,039 $4.99 of shipping and nothing else in 388 00:18:51,520 --> 00:18:58,480 your cart if you need to you can modify 389 00:18:55,039 --> 00:19:00,919 your cart by clicking update or delete 390 00:18:58,480 --> 00:19:03,760 or or by going to the homepage by 391 00:19:00,919 --> 00:19:05,159 clicking in the top left and adding any 392 00:19:03,760 --> 00:19:08,159 products that you're 393 00:19:05,159 --> 00:19:11,280 missing once you have the same products 394 00:19:08,159 --> 00:19:14,200 as me in your cart and you have the same 395 00:19:11,280 --> 00:19:17,120 numbers on the right we're ready to 396 00:19:14,200 --> 00:19:20,200 begin let's start by calculating this 397 00:19:17,120 --> 00:19:24,200 number up here this is the cost of the 398 00:19:20,200 --> 00:19:27,000 products before shipping and taxes to 399 00:19:24,200 --> 00:19:29,400 calculate this number using JavaScript 400 00:19:27,000 --> 00:19:32,600 we're just going to add the cost of the 401 00:19:29,400 --> 00:19:35,840 products so in our console we're going 402 00:19:32,600 --> 00:19:38,000 to add the cost of the socks which is 403 00:19:35,840 --> 00:19:42,039 $10 and 404 00:19:38,000 --> 00:19:44,640 90 we have two of them so we're going to 405 00:19:42,039 --> 00:19:47,520 multiply this by 406 00:19:44,640 --> 00:19:52,400 two and then we're going to add the cost 407 00:19:47,520 --> 00:19:52,400 of the basketball so plus 408 00:19:53,120 --> 00:19:58,240 $20.95 if we press 409 00:19:55,520 --> 00:19:59,480 enter the computer will do this math 410 00:19:58,240 --> 00:20:02,840 calculation 411 00:19:59,480 --> 00:20:04,280 and give us this number and if we scroll 412 00:20:02,840 --> 00:20:06,480 to the top of our 413 00:20:04,280 --> 00:20:10,440 project you'll see that the number that 414 00:20:06,480 --> 00:20:12,520 we got matches what we have on the page 415 00:20:10,440 --> 00:20:15,880 so that's how we use JavaScript to 416 00:20:12,520 --> 00:20:18,880 calculate this number here let's do 417 00:20:15,880 --> 00:20:21,679 another example we're going to calculate 418 00:20:18,880 --> 00:20:25,000 this third number which is the cost of 419 00:20:21,679 --> 00:20:26,919 the products plus shipping feel free to 420 00:20:25,000 --> 00:20:30,159 pause the video if you want to try it 421 00:20:26,919 --> 00:20:32,960 yourself first otherwise we'll do it 422 00:20:30,159 --> 00:20:35,520 together let's go back to our 423 00:20:32,960 --> 00:20:39,840 console and we're going to add the cost 424 00:20:35,520 --> 00:20:45,880 of the products again which is 425 00:20:39,840 --> 00:20:45,880 $10.90 * 2 plus 426 00:20:46,200 --> 00:20:53,919 $20.95 but this time we're going to add 427 00:20:49,360 --> 00:20:56,760 the cost of shipping which is 428 00:20:53,919 --> 00:20:59,720 $4.99 so 429 00:20:56,760 --> 00:21:02,159 4.99 and press and 430 00:20:59,720 --> 00:21:05,280 enter the computer will do this 431 00:21:02,159 --> 00:21:08,480 calculation and give us this number 432 00:21:05,280 --> 00:21:11,559 which matches what we see in the final 433 00:21:08,480 --> 00:21:14,039 project next we're going to learn a very 434 00:21:11,559 --> 00:21:15,720 important feature of math called the 435 00:21:14,039 --> 00:21:18,600 order of 436 00:21:15,720 --> 00:21:21,240 operations adding subtracting 437 00:21:18,600 --> 00:21:24,000 multiplying and dividing these are 438 00:21:21,240 --> 00:21:26,240 called operations and the characters in 439 00:21:24,000 --> 00:21:29,080 the middle are called 440 00:21:26,240 --> 00:21:32,919 operators now there's a certain order 441 00:21:29,080 --> 00:21:35,799 that we do these operations for example 442 00:21:32,919 --> 00:21:43,840 if we go back to our console and type 443 00:21:35,799 --> 00:21:47,880 the calculation 1 + 1 * 3 and press 444 00:21:43,840 --> 00:21:52,679 enter notice that it multiplies 1 * 3 445 00:21:47,880 --> 00:21:53,919 first which equals 3 and then adds 1 446 00:21:52,679 --> 00:21:58,200 which equals 447 00:21:53,919 --> 00:21:59,240 4 notice that it does not add 1 + 1 448 00:21:58,200 --> 00:22:04,200 first 449 00:21:59,240 --> 00:22:07,960 which equal 2 and then 2 * 3 = 450 00:22:04,200 --> 00:22:11,559 6 the way math normally works is that 451 00:22:07,960 --> 00:22:14,840 multiply and divide are done first add 452 00:22:11,559 --> 00:22:17,799 and subtract are done after this is 453 00:22:14,840 --> 00:22:20,640 called the order of operations and 454 00:22:17,799 --> 00:22:24,159 JavaScript follows the same rules 455 00:22:20,640 --> 00:22:27,960 multiply and divide are done first add 456 00:22:24,159 --> 00:22:30,760 and subtract are done after the order of 457 00:22:27,960 --> 00:22:32,880 operations is also called operator 458 00:22:30,760 --> 00:22:35,360 precedence in 459 00:22:32,880 --> 00:22:38,039 JavaScript one thing to note is that 460 00:22:35,360 --> 00:22:40,840 multiply and divide have the same 461 00:22:38,039 --> 00:22:43,640 priority so if we have both in a 462 00:22:40,840 --> 00:22:47,559 calculation it will calculate from left 463 00:22:43,640 --> 00:22:50,520 to right add and subtract also have the 464 00:22:47,559 --> 00:22:53,400 same priority if we have both in the 465 00:22:50,520 --> 00:22:56,600 calculation it will also calculate from 466 00:22:53,400 --> 00:22:59,640 left to right another feature of the 467 00:22:56,600 --> 00:23:02,400 order of operations is that we can use 468 00:22:59,640 --> 00:23:06,720 brackets to control which part of the 469 00:23:02,400 --> 00:23:10,760 calculation gets done first for example 470 00:23:06,720 --> 00:23:17,440 let's type out this calculation again 1 471 00:23:10,760 --> 00:23:18,919 + 1 * 3 but this time we're going to add 472 00:23:17,440 --> 00:23:24,880 an Open 473 00:23:18,919 --> 00:23:26,480 Bracket and a Clos bracket around 1 + 1 474 00:23:24,880 --> 00:23:29,440 if we press 475 00:23:26,480 --> 00:23:32,440 enter now the calculation inside the 476 00:23:29,440 --> 00:23:40,840 brackets will be done first so it will 477 00:23:32,440 --> 00:23:43,679 add 1 + 1 = 2 and then 2 * 3 = 6 so when 478 00:23:40,840 --> 00:23:46,440 we use brackets JavaScript will always 479 00:23:43,679 --> 00:23:49,520 do the calculation inside the brackets 480 00:23:46,440 --> 00:23:51,960 first and then do the rest of the 481 00:23:49,520 --> 00:23:54,799 calculation brackets have the highest 482 00:23:51,960 --> 00:23:57,400 priority in the order of 483 00:23:54,799 --> 00:23:59,440 operations now let's learn the syntax 484 00:23:57,400 --> 00:24:02,240 rules for brackets 485 00:23:59,440 --> 00:24:05,720 so we just have to have a matching Open 486 00:24:02,240 --> 00:24:08,760 Bracket and close bracket so something 487 00:24:05,720 --> 00:24:12,960 like this doesn't work because we have 488 00:24:08,760 --> 00:24:15,799 two Open brackets and no Clos bracket we 489 00:24:12,960 --> 00:24:18,880 also need to have a complete calculation 490 00:24:15,799 --> 00:24:23,200 in the brackets so this also doesn't 491 00:24:18,880 --> 00:24:25,159 work because 1 + 1 multiply is not a 492 00:24:23,200 --> 00:24:27,760 complete 493 00:24:25,159 --> 00:24:30,919 calculation now let's do some practical 494 00:24:27,760 --> 00:24:33,320 examples using the order of 495 00:24:30,919 --> 00:24:36,480 operations first we're going to do some 496 00:24:33,320 --> 00:24:38,200 more setup in the final project so we're 497 00:24:36,480 --> 00:24:40,720 actually going to remove all the 498 00:24:38,200 --> 00:24:43,960 products in our 499 00:24:40,720 --> 00:24:46,320 cart and then once our cart is empty 500 00:24:43,960 --> 00:24:47,799 we'll go to the homepage by clicking the 501 00:24:46,320 --> 00:24:50,480 top 502 00:24:47,799 --> 00:24:55,000 left and this time we're going to add 503 00:24:50,480 --> 00:25:00,000 some new products so we'll add one 504 00:24:55,000 --> 00:25:01,960 basketball and two T-shirts 505 00:25:00,000 --> 00:25:04,840 now let's go back to our cart by 506 00:25:01,960 --> 00:25:04,840 clicking the top 507 00:25:05,159 --> 00:25:11,080 right we'll scroll down and we're going 508 00:25:08,480 --> 00:25:14,039 to calculate the numbers in this section 509 00:25:11,080 --> 00:25:17,440 again let's calculate this number at the 510 00:25:14,039 --> 00:25:19,120 top which is the cost of the products 511 00:25:17,440 --> 00:25:22,279 we'll go back to our 512 00:25:19,120 --> 00:25:26,039 console and add the cost of the 513 00:25:22,279 --> 00:25:26,039 basketball which is 514 00:25:26,520 --> 00:25:34,200 $20.95 Plus the cost of the 515 00:25:30,159 --> 00:25:34,200 t-shirts which is 516 00:25:34,520 --> 00:25:42,520 $799 and we have two of them so we'll 517 00:25:38,159 --> 00:25:45,600 multiply this by two if we press 518 00:25:42,520 --> 00:25:49,919 enter this will give us the number that 519 00:25:45,600 --> 00:25:52,360 we see in the final project here notice 520 00:25:49,919 --> 00:25:54,720 that in this calculation we're taking 521 00:25:52,360 --> 00:25:57,240 advantage of the order of 522 00:25:54,720 --> 00:25:58,880 operations even though multiply is 523 00:25:57,240 --> 00:26:01,440 written last 524 00:25:58,880 --> 00:26:04,520 we still multiply these two numbers 525 00:26:01,440 --> 00:26:08,080 first so we calculate the cost of two 526 00:26:04,520 --> 00:26:10,159 T-shirts first and then we add the cost 527 00:26:08,080 --> 00:26:13,080 of the basketball 528 00:26:10,159 --> 00:26:15,200 after okay now let's try something a 529 00:26:13,080 --> 00:26:19,240 little more difficult we're going to 530 00:26:15,200 --> 00:26:22,840 learn how to calculate this 10% tax here 531 00:26:19,240 --> 00:26:25,440 so how do we calculate 10% of something 532 00:26:22,840 --> 00:26:28,960 unfortunately we can't just multiply by 533 00:26:25,440 --> 00:26:30,960 10% in JavaScript instead 534 00:26:28,960 --> 00:26:35,399 we have to calculate the percent 535 00:26:30,960 --> 00:26:40,120 manually so how do we do that 1% is 536 00:26:35,399 --> 00:26:46,039 actually equal to 1 / 100 that's what 537 00:26:40,120 --> 00:26:48,880 percent means so 10% is equal to 10 / 538 00:26:46,039 --> 00:26:53,000 100 which equals 539 00:26:48,880 --> 00:26:55,880 0.1 so to calculate 10% manually we just 540 00:26:53,000 --> 00:27:00,360 multiply this number by 541 00:26:55,880 --> 00:27:05,520 0.1 so we'll type 30 3 542 00:27:00,360 --> 00:27:07,919 6.93 multiplied by 10% or 543 00:27:05,520 --> 00:27:10,640 0.1 if we press 544 00:27:07,919 --> 00:27:12,679 enter it will give us a number that is 545 00:27:10,640 --> 00:27:15,880 close to what we have in the final 546 00:27:12,679 --> 00:27:18,360 project it's the same amount of money 547 00:27:15,880 --> 00:27:21,480 but our number is more accurate than we 548 00:27:18,360 --> 00:27:24,840 need so later in this lesson we'll learn 549 00:27:21,480 --> 00:27:28,320 how to round this number down to exactly 550 00:27:24,840 --> 00:27:30,159 what we have on the web page for now 551 00:27:28,320 --> 00:27:31,640 let's do some more practice with the 552 00:27:30,159 --> 00:27:35,159 order of 553 00:27:31,640 --> 00:27:38,200 operations let's calculate the tax again 554 00:27:35,159 --> 00:27:41,919 except instead of using the final number 555 00:27:38,200 --> 00:27:45,960 36.9 3 Let's calculate this number 556 00:27:41,919 --> 00:27:48,080 individually like here so at the bottom 557 00:27:45,960 --> 00:27:51,080 we're going to add the cost of the 558 00:27:48,080 --> 00:27:51,080 basketball 559 00:27:57,120 --> 00:28:05,279 20.95% 2 and then multiply this by the 560 00:28:01,559 --> 00:28:08,000 tax percent time 561 00:28:05,279 --> 00:28:11,399 0.1 and press 562 00:28:08,000 --> 00:28:14,600 enter unfortunately this number doesn't 563 00:28:11,399 --> 00:28:17,360 match what we got up here and that's 564 00:28:14,600 --> 00:28:20,159 because if we look at this calculation 565 00:28:17,360 --> 00:28:23,080 according to the order of operations 566 00:28:20,159 --> 00:28:25,799 multiply is done first so it will 567 00:28:23,080 --> 00:28:29,159 multiply these three numbers first and 568 00:28:25,799 --> 00:28:33,080 then add 20.95% 569 00:28:29,159 --> 00:28:35,240 after however this is not what we want 570 00:28:33,080 --> 00:28:38,320 we actually want to calculate this 571 00:28:35,240 --> 00:28:40,960 number first this is the cost of the 572 00:28:38,320 --> 00:28:43,360 products so how do we get the computer 573 00:28:40,960 --> 00:28:46,120 to calculate this first and then 574 00:28:43,360 --> 00:28:49,960 multiply by the tax percent 575 00:28:46,120 --> 00:28:52,440 0.1 to do this we can use brackets to 576 00:28:49,960 --> 00:28:55,960 control which part of the calculation 577 00:28:52,440 --> 00:28:59,559 gets done first so let's create this 578 00:28:55,960 --> 00:29:02,760 calculation again 20 579 00:28:59,559 --> 00:29:07,240 .95 + 580 00:29:02,760 --> 00:29:10,559 7.99 * 2 but this time we're going to 581 00:29:07,240 --> 00:29:13,360 add brackets around this part of the 582 00:29:10,559 --> 00:29:17,039 calculation to make sure that this gets 583 00:29:13,360 --> 00:29:21,200 calculated first and then at the end 584 00:29:17,039 --> 00:29:23,799 we'll multiply it by the tax percent 585 00:29:21,200 --> 00:29:26,720 0.1 and now if we press 586 00:29:23,799 --> 00:29:29,000 enter we will get the same number that 587 00:29:26,720 --> 00:29:31,799 we had before 588 00:29:29,000 --> 00:29:34,720 so that's how we use math and the Order 589 00:29:31,799 --> 00:29:38,000 of Operations to calculate the numbers 590 00:29:34,720 --> 00:29:40,080 in this section of the final project so 591 00:29:38,000 --> 00:29:42,640 don't worry you don't have to be good at 592 00:29:40,080 --> 00:29:45,760 math to learn JavaScript this is just to 593 00:29:42,640 --> 00:29:48,279 show you how math Works in 594 00:29:45,760 --> 00:29:50,360 JavaScript in the rest of this lesson 595 00:29:48,279 --> 00:29:53,799 we're going to learn some more details 596 00:29:50,360 --> 00:29:56,279 about numbers and math the first detail 597 00:29:53,799 --> 00:29:58,799 we're going to learn is a weird behavior 598 00:29:56,279 --> 00:30:00,799 of math in JavaScript 599 00:29:58,799 --> 00:30:04,039 first we'll need to learn some 600 00:30:00,799 --> 00:30:08,919 terminology in programming whole numbers 601 00:30:04,039 --> 00:30:11,039 like 2 3 and 4 are called integers 602 00:30:08,919 --> 00:30:15,559 decimal numbers like 603 00:30:11,039 --> 00:30:19,600 2.2 and 2.5 are called floating Point 604 00:30:15,559 --> 00:30:22,360 numbers or floats and unfortunately 605 00:30:19,600 --> 00:30:25,840 computers have a problem working with 606 00:30:22,360 --> 00:30:29,960 floats for example if we go back to our 607 00:30:25,840 --> 00:30:29,960 console and type the calculation 608 00:30:30,240 --> 00:30:35,640 0.1 plus 609 00:30:33,279 --> 00:30:38,640 0.2 and press 610 00:30:35,640 --> 00:30:42,200 enter it will give us this weird number 611 00:30:38,640 --> 00:30:43,279 here that's close to 0.3 but a little 612 00:30:42,200 --> 00:30:45,720 bit 613 00:30:43,279 --> 00:30:48,480 inaccurate so this problem is not 614 00:30:45,720 --> 00:30:51,679 specific to JavaScript many programming 615 00:30:48,480 --> 00:30:53,840 languages also have this problem the 616 00:30:51,679 --> 00:30:56,960 reason this happens is because of how 617 00:30:53,840 --> 00:30:59,320 computers store numbers I'll give a 618 00:30:56,960 --> 00:31:02,760 simplified explan 619 00:30:59,320 --> 00:31:06,559 computers can only store zeros and ones 620 00:31:02,760 --> 00:31:08,799 while humans can count from 0 to 9 so 621 00:31:06,559 --> 00:31:12,039 there's sort of a mismatch between our 622 00:31:08,799 --> 00:31:14,840 number systems for integers this is not 623 00:31:12,039 --> 00:31:18,559 a problem the computer can just use a 624 00:31:14,840 --> 00:31:22,240 bunch of zeros and ones to store it 625 00:31:18,559 --> 00:31:24,279 however for decimal numbers or floats 626 00:31:22,240 --> 00:31:27,559 sometimes the computer can't store the 627 00:31:24,279 --> 00:31:29,919 number properly for example here's how 628 00:31:27,559 --> 00:31:34,720 the computer computer tries to store 629 00:31:29,919 --> 00:31:37,279 0.2 using only zeros and ones at the top 630 00:31:34,720 --> 00:31:40,559 is what the computer stores at the 631 00:31:37,279 --> 00:31:42,960 bottom is what this is equal to in human 632 00:31:40,559 --> 00:31:46,720 numbers we can see that the computer 633 00:31:42,960 --> 00:31:49,559 gets closer and closer to 0.2 but never 634 00:31:46,720 --> 00:31:52,279 actually reaches it and this is why we 635 00:31:49,559 --> 00:31:55,320 get these inaccuracies when calculating 636 00:31:52,279 --> 00:31:58,960 with floats because computers can't 637 00:31:55,320 --> 00:32:01,880 store some floats properly note that 638 00:31:58,960 --> 00:32:05,000 this doesn't happen with all floats for 639 00:32:01,880 --> 00:32:08,200 example computers can store 640 00:32:05,000 --> 00:32:10,840 0.25 without any problems so these 641 00:32:08,200 --> 00:32:14,679 inaccuracies only happen with some 642 00:32:10,840 --> 00:32:17,240 floats but not all of them however when 643 00:32:14,679 --> 00:32:20,240 calculating money we definitely want to 644 00:32:17,240 --> 00:32:23,240 avoid any inaccuracies so let's learn 645 00:32:20,240 --> 00:32:26,399 how to do that first we're going to set 646 00:32:23,240 --> 00:32:29,799 up our cart again so make sure in your 647 00:32:26,399 --> 00:32:32,880 cart you have one basketball with free 648 00:32:29,799 --> 00:32:35,679 shipping and this time you have one 649 00:32:32,880 --> 00:32:40,679 t-shirt so we're going to click 650 00:32:35,679 --> 00:32:44,120 update change this to one and click 651 00:32:40,679 --> 00:32:46,919 save and this t-shirt also has free 652 00:32:44,120 --> 00:32:49,039 shipping now let's scroll up and we're 653 00:32:46,919 --> 00:32:51,720 going to calculate this number at the 654 00:32:49,039 --> 00:32:54,039 top again which is the cost of the 655 00:32:51,720 --> 00:32:56,760 products let's go into our 656 00:32:54,039 --> 00:32:59,840 console and we're going to add the cost 657 00:32:56,760 --> 00:32:59,840 of the basketball 658 00:33:00,760 --> 00:33:08,440 $20.95 plus the cost of the 659 00:33:04,320 --> 00:33:08,440 t-shirt which is 660 00:33:08,880 --> 00:33:14,559 $7.99 if we press 661 00:33:11,320 --> 00:33:17,039 enter calculating with these floats 662 00:33:14,559 --> 00:33:20,600 causes the inaccuracies that we saw 663 00:33:17,039 --> 00:33:23,159 earlier so how do we avoid this problem 664 00:33:20,600 --> 00:33:26,080 the best practice when calculating money 665 00:33:23,159 --> 00:33:29,760 is to calculate in cents instead of 666 00:33:26,080 --> 00:33:29,760 dollars so instead of 667 00:33:29,919 --> 00:33:38,639 $20.95 we're going to calculate 668 00:33:34,600 --> 00:33:41,399 2,95 plus 669 00:33:38,639 --> 00:33:44,240 $7.99 notice we're not using floats 670 00:33:41,399 --> 00:33:46,840 anymore so we avoid the 671 00:33:44,240 --> 00:33:49,880 inaccuracies but now this number is in 672 00:33:46,840 --> 00:33:52,960 cents so we need to convert back to 673 00:33:49,880 --> 00:33:56,120 Dollars to do that we'll surround this 674 00:33:52,960 --> 00:33:58,919 calculation with brackets to make sure 675 00:33:56,120 --> 00:34:04,039 we calculate this first and then at the 676 00:33:58,919 --> 00:34:07,600 end we'll divide by 100 to convert back 677 00:34:04,039 --> 00:34:11,159 to dollars because there are 100 cents 678 00:34:07,600 --> 00:34:13,800 in each dollar if we press 679 00:34:11,159 --> 00:34:16,919 enter and scroll 680 00:34:13,800 --> 00:34:20,760 up that's how we get the exact number 681 00:34:16,919 --> 00:34:23,639 that we see on the page so remember in 682 00:34:20,760 --> 00:34:25,919 programming calculations with floats are 683 00:34:23,639 --> 00:34:28,560 sometimes inaccurate and when working 684 00:34:25,919 --> 00:34:31,359 with money the best practice is to do 685 00:34:28,560 --> 00:34:34,159 the calculation in cents and then 686 00:34:31,359 --> 00:34:37,079 convert back to dollars at the 687 00:34:34,159 --> 00:34:40,720 end the next detail we're going to learn 688 00:34:37,079 --> 00:34:43,399 is how to round a number in JavaScript 689 00:34:40,720 --> 00:34:46,520 rounding just means taking a number and 690 00:34:43,399 --> 00:34:49,440 moving it to the nearest integer to 691 00:34:46,520 --> 00:34:52,879 round a number we can use this 692 00:34:49,440 --> 00:34:57,839 code capital m 693 00:34:52,879 --> 00:35:01,000 math. round Open Bracket close bracket 694 00:34:57,839 --> 00:35:03,200 so make sure you use a capital M because 695 00:35:01,000 --> 00:35:06,200 JavaScript is case 696 00:35:03,200 --> 00:35:09,359 sensitive now between the brackets we 697 00:35:06,200 --> 00:35:11,440 can put a number that we want to round 698 00:35:09,359 --> 00:35:15,320 like 699 00:35:11,440 --> 00:35:17,079 2.2 if we press enter it will round this 700 00:35:15,320 --> 00:35:23,560 number down to 701 00:35:17,079 --> 00:35:26,400 two if we do capital M math do round 702 00:35:23,560 --> 00:35:28,760 Open Bracket close bracket and between 703 00:35:26,400 --> 00:35:30,880 the brackets we put 704 00:35:28,760 --> 00:35:36,520 2.8 and press 705 00:35:30,880 --> 00:35:40,000 enter it will round 2.8 up to three now 706 00:35:36,520 --> 00:35:42,280 let's do a practical example with 707 00:35:40,000 --> 00:35:45,160 math.round we're going to calculate the 708 00:35:42,280 --> 00:35:48,520 tax again but this time we're going to 709 00:35:45,160 --> 00:35:52,440 round the result to exactly this number 710 00:35:48,520 --> 00:35:55,079 on the page so first let's calculate the 711 00:35:52,440 --> 00:35:57,640 tax we'll go into our 712 00:35:55,079 --> 00:36:01,800 console and add the cost of the product 713 00:35:57,640 --> 00:36:04,880 products in cents so 714 00:36:01,800 --> 00:36:07,560 2,95 for the 715 00:36:04,880 --> 00:36:10,240 basketball and plus the cost of the 716 00:36:07,560 --> 00:36:12,760 t-shirt which is 717 00:36:10,240 --> 00:36:16,079 $7.99 and we'll surround this in 718 00:36:12,760 --> 00:36:18,960 Brackets to make sure we calculate this 719 00:36:16,079 --> 00:36:23,240 first and then we're going to multiply 720 00:36:18,960 --> 00:36:26,400 this by the tax percent or 721 00:36:23,240 --> 00:36:29,760 0.1 and finally we'll convert this back 722 00:36:26,400 --> 00:36:32,640 to dollars so let's surround this in 723 00:36:29,760 --> 00:36:35,960 Brackets as well to make sure we 724 00:36:32,640 --> 00:36:41,240 calculate this first and at the end 725 00:36:35,960 --> 00:36:44,040 we'll divide by 100 to convert back to 726 00:36:41,240 --> 00:36:47,359 dollars so notice here we can have 727 00:36:44,040 --> 00:36:50,079 brackets inside brackets in this 728 00:36:47,359 --> 00:36:53,440 situation JavaScript will calculate the 729 00:36:50,079 --> 00:36:56,960 inner brackets first and then the outer 730 00:36:53,440 --> 00:36:58,280 brackets and then everything else if we 731 00:36:56,960 --> 00:37:01,960 press enter 732 00:36:58,280 --> 00:37:05,359 here this gives us 733 00:37:01,960 --> 00:37:08,480 2.89 which is close to the number on the 734 00:37:05,359 --> 00:37:12,040 page it's the same amount of money but 735 00:37:08,480 --> 00:37:15,119 our number is more accurate than we need 736 00:37:12,040 --> 00:37:18,920 so now let's learn how to use math.round 737 00:37:15,119 --> 00:37:22,680 to round this number to exactly 738 00:37:18,920 --> 00:37:26,200 2.89 first let's create a copy of this 739 00:37:22,680 --> 00:37:29,160 calculation so we'll select it right 740 00:37:26,200 --> 00:37:34,079 click copy 741 00:37:29,160 --> 00:37:36,920 and down here right click and 742 00:37:34,079 --> 00:37:39,240 paste by the way if you paste in the 743 00:37:36,920 --> 00:37:44,440 console you might see this warning 744 00:37:39,240 --> 00:37:44,440 message to fix this you can type allow 745 00:37:45,240 --> 00:37:51,200 pasting and press enter and now you can 746 00:37:52,440 --> 00:38:01,280 paste so let's continue and we were 747 00:37:55,200 --> 00:38:01,280 trying to round 2.89 4 to exactly 748 00:38:02,480 --> 00:38:08,040 2.89 so one thing we might try to do is 749 00:38:05,760 --> 00:38:12,040 to round this entire 750 00:38:08,040 --> 00:38:16,560 calculation however this will round 2.89 751 00:38:12,040 --> 00:38:20,040 4 to 3 because it rounds to the nearest 752 00:38:16,560 --> 00:38:23,440 integer instead what we want to round is 753 00:38:20,040 --> 00:38:28,599 this number here so remember that this 754 00:38:23,440 --> 00:38:30,560 number is the result in cents which is 755 00:38:28,599 --> 00:38:33,960 289.50 756 00:38:30,560 --> 00:38:36,040 if we round this number it will round 757 00:38:33,960 --> 00:38:40,079 down to 758 00:38:36,040 --> 00:38:40,079 289 which is exactly 759 00:38:40,520 --> 00:38:49,480 $289 so to round this calculation at the 760 00:38:44,280 --> 00:38:49,480 front we'll type capital M 761 00:38:50,560 --> 00:38:56,040 math.round so we already have brackets 762 00:38:53,079 --> 00:39:00,280 around this so we don't need extra 763 00:38:56,040 --> 00:39:03,560 brackets so this this will round to 764 00:39:00,280 --> 00:39:10,000 289 and finally we'll convert this back 765 00:39:03,560 --> 00:39:11,400 to dollars at the End by dividing by 100 766 00:39:10,000 --> 00:39:13,599 if we press 767 00:39:11,400 --> 00:39:16,440 enter that's how we rounded this 768 00:39:13,599 --> 00:39:17,160 calculation to get the exact number on 769 00:39:16,440 --> 00:39:20,720 the 770 00:39:17,160 --> 00:39:24,119 page so to summarize we first calculate 771 00:39:20,720 --> 00:39:27,079 the result in cents and then we round 772 00:39:24,119 --> 00:39:29,760 this to the nearest scent and then at 773 00:39:27,079 --> 00:39:32,720 the end and we convert back to 774 00:39:29,760 --> 00:39:35,440 Dollars again don't worry you don't have 775 00:39:32,720 --> 00:39:38,200 to be good at math to learn JavaScript 776 00:39:35,440 --> 00:39:40,720 these examples are just for learning 777 00:39:38,200 --> 00:39:43,599 almost all the math we do in JavaScript 778 00:39:40,720 --> 00:39:46,040 is more simple than this I'll give you 779 00:39:43,599 --> 00:39:49,119 some exercises at the end of this lesson 780 00:39:46,040 --> 00:39:50,359 to help you get more practice the last 781 00:39:49,119 --> 00:39:53,160 thing we're going to learn in this 782 00:39:50,359 --> 00:39:56,440 lesson is how to use Google when you're 783 00:39:53,160 --> 00:39:59,119 learning to code so far I've been giving 784 00:39:56,440 --> 00:40:01,319 you different pieces pces of code like 785 00:39:59,119 --> 00:40:03,880 alert and 786 00:40:01,319 --> 00:40:07,400 math.round you might be wondering where 787 00:40:03,880 --> 00:40:10,280 do I find this code these pieces of code 788 00:40:07,400 --> 00:40:13,520 are features of JavaScript and I find 789 00:40:10,280 --> 00:40:15,599 this code by searching on Google so 790 00:40:13,520 --> 00:40:18,240 we're going to learn how to use Google 791 00:40:15,599 --> 00:40:21,800 to learn new features of JavaScript on 792 00:40:18,240 --> 00:40:24,760 our own let's go to our browser and 793 00:40:21,800 --> 00:40:30,520 create a new tab at the 794 00:40:24,760 --> 00:40:30,520 top and we'll click here and type 795 00:40:30,920 --> 00:40:37,240 google.com and press 796 00:40:33,400 --> 00:40:39,640 enter and we'll search in Google so the 797 00:40:37,240 --> 00:40:42,800 trick to searching in Google is to 798 00:40:39,640 --> 00:40:46,079 search for what we are trying to do for 799 00:40:42,800 --> 00:40:49,400 example if we want to round a number we 800 00:40:46,079 --> 00:40:54,680 would search for 801 00:40:49,400 --> 00:40:58,680 JavaScript how to round a 802 00:40:54,680 --> 00:41:00,599 number if we press enter 803 00:40:58,680 --> 00:41:03,560 sometimes you might get the code right 804 00:41:00,599 --> 00:41:05,400 away in the first result and sometimes 805 00:41:03,560 --> 00:41:08,760 you might have to go into the other 806 00:41:05,400 --> 00:41:12,240 results to find the code that you need 807 00:41:08,760 --> 00:41:15,160 in my example I see the code math.round 808 00:41:12,240 --> 00:41:18,280 in the first result so I can click into 809 00:41:15,160 --> 00:41:21,400 this result read about what this code 810 00:41:18,280 --> 00:41:24,560 does and also see some examples of how 811 00:41:21,400 --> 00:41:26,520 to use it one thing to keep in mind is 812 00:41:24,560 --> 00:41:28,280 that when you're searching in Google you 813 00:41:26,520 --> 00:41:31,119 might not understand understand all the 814 00:41:28,280 --> 00:41:33,920 code that you get in your results for 815 00:41:31,119 --> 00:41:36,880 example here there's a lot of code that 816 00:41:33,920 --> 00:41:39,560 we haven't learned yet and actually 817 00:41:36,880 --> 00:41:42,800 that's okay we just need to look for the 818 00:41:39,560 --> 00:41:45,680 pieces of code that look familiar to us 819 00:41:42,800 --> 00:41:48,160 like this part of the code we can then 820 00:41:45,680 --> 00:41:50,920 use this part of the code in our own 821 00:41:48,160 --> 00:41:54,160 code we don't have to understand every 822 00:41:50,920 --> 00:41:56,319 bit of code that we get from Google and 823 00:41:54,160 --> 00:41:58,839 don't worry as we move through this 824 00:41:56,319 --> 00:42:01,160 course we we will understand more and 825 00:41:58,839 --> 00:42:02,440 more of this code that we get from 826 00:42:01,160 --> 00:42:05,319 Google 827 00:42:02,440 --> 00:42:08,520 results these days we also have some 828 00:42:05,319 --> 00:42:11,680 artificial intelligence or AI tools that 829 00:42:08,520 --> 00:42:13,480 can help us search for code I'll provide 830 00:42:11,680 --> 00:42:15,520 links to some of these tools in the 831 00:42:13,480 --> 00:42:18,200 video 832 00:42:15,520 --> 00:42:20,480 description by the way to find the links 833 00:42:18,200 --> 00:42:23,280 mentioned in each lesson you can click 834 00:42:20,480 --> 00:42:23,280 here in the 835 00:42:26,119 --> 00:42:32,000 description and this page contains all 836 00:42:28,559 --> 00:42:32,000 the links mentioned in each 837 00:42:33,599 --> 00:42:38,880 lesson and that's the end of this lesson 838 00:42:36,720 --> 00:42:42,040 in this lesson we learned how to work 839 00:42:38,880 --> 00:42:45,760 with numbers and math we learned the 840 00:42:42,040 --> 00:42:47,760 order of operations and brackets we 841 00:42:45,760 --> 00:42:50,160 calculated the numbers in the final 842 00:42:47,760 --> 00:42:52,319 Amazon Project we learned that 843 00:42:50,160 --> 00:42:54,920 calculations using floats can be 844 00:42:52,319 --> 00:42:57,400 inaccurate and how to avoid this problem 845 00:42:54,920 --> 00:42:59,760 when working with money we learned how 846 00:42:57,400 --> 00:43:02,160 to round numbers using 847 00:42:59,760 --> 00:43:05,319 math.round and we learned how to search 848 00:43:02,160 --> 00:43:07,800 for code on our own here are some 849 00:43:05,319 --> 00:43:11,319 exercises to help you practice numbers 850 00:43:07,800 --> 00:43:11,319 and math 851 00:43:31,119 --> 00:43:36,480 in the previous lesson we learned how to 852 00:43:33,280 --> 00:43:38,520 work with numbers and math in this 853 00:43:36,480 --> 00:43:41,280 lesson we're going to learn how to work 854 00:43:38,520 --> 00:43:43,680 with text and we're going to create the 855 00:43:41,280 --> 00:43:46,359 text in this section of the final 856 00:43:43,680 --> 00:43:48,800 project first make sure you have the 857 00:43:46,359 --> 00:43:51,920 final project open and you're on the 858 00:43:48,800 --> 00:43:55,680 checkout page also make sure you have 859 00:43:51,920 --> 00:43:58,040 the console open on this page if not you 860 00:43:55,680 --> 00:44:00,880 can go to a blank area 861 00:43:58,040 --> 00:44:04,599 right click click 862 00:44:00,880 --> 00:44:07,040 inspect and then click the 863 00:44:04,599 --> 00:44:10,440 console now we're going to learn a 864 00:44:07,040 --> 00:44:14,079 feature of JavaScript called strings a 865 00:44:10,440 --> 00:44:17,680 string represents text let's go to our 866 00:44:14,079 --> 00:44:21,800 console and create our first string 867 00:44:17,680 --> 00:44:25,440 we're going to type single quote hello 868 00:44:21,800 --> 00:44:29,000 and single quote and press 869 00:44:25,440 --> 00:44:32,240 enter this is a string and it represents 870 00:44:29,000 --> 00:44:35,640 the text hello and we've actually seen 871 00:44:32,240 --> 00:44:39,280 this code before in the first lesson the 872 00:44:35,640 --> 00:44:44,200 first piece of code that we learned was 873 00:44:39,280 --> 00:44:47,760 alert Open Bracket single quote hello 874 00:44:44,200 --> 00:44:50,839 single quote close bracket and 875 00:44:47,760 --> 00:44:53,200 semicolon this code creates a popup on 876 00:44:50,839 --> 00:44:57,359 the page if we press 877 00:44:53,200 --> 00:45:00,400 enter it creates a popup now in inside 878 00:44:57,359 --> 00:45:03,599 these brackets we actually created a 879 00:45:00,400 --> 00:45:07,160 string this string is the text that 880 00:45:03,599 --> 00:45:09,280 appears in the popup so as you can see a 881 00:45:07,160 --> 00:45:11,359 string represents 882 00:45:09,280 --> 00:45:14,079 text let's press 883 00:45:11,359 --> 00:45:17,880 okay and now let's learn the syntax 884 00:45:14,079 --> 00:45:21,200 rules for strings to create a string we 885 00:45:17,880 --> 00:45:24,280 just write some text like hello and then 886 00:45:21,200 --> 00:45:27,640 surround this text with single quotes 887 00:45:24,280 --> 00:45:30,760 one at the beginning and one at the end 888 00:45:27,640 --> 00:45:35,760 we can also add strings together for 889 00:45:30,760 --> 00:45:39,599 example let's type the string 890 00:45:35,760 --> 00:45:42,200 sum plus the string 891 00:45:39,599 --> 00:45:45,400 text and press 892 00:45:42,200 --> 00:45:47,880 enter when we add two strings it 893 00:45:45,400 --> 00:45:50,880 combines the strings together into a 894 00:45:47,880 --> 00:45:54,160 bigger string and this makes sense we're 895 00:45:50,880 --> 00:45:56,640 adding two pieces of text together we 896 00:45:54,160 --> 00:45:57,640 can also add more than two strings at a 897 00:45:56,640 --> 00:46:02,520 time 898 00:45:57,640 --> 00:46:06,760 for example we can type the string 899 00:46:02,520 --> 00:46:11,240 sum plus the string 900 00:46:06,760 --> 00:46:13,480 more plus the string 901 00:46:11,240 --> 00:46:16,559 text if we press 902 00:46:13,480 --> 00:46:20,599 enter the computer will combine all 903 00:46:16,559 --> 00:46:24,000 three strings into one big string by the 904 00:46:20,599 --> 00:46:27,200 way this is called concatenation when we 905 00:46:24,000 --> 00:46:30,599 combine strings together now in in the 906 00:46:27,200 --> 00:46:33,160 previous lesson we learned about numbers 907 00:46:30,599 --> 00:46:36,000 numbers and strings are two different 908 00:46:33,160 --> 00:46:39,079 types of values in JavaScript they 909 00:46:36,000 --> 00:46:41,400 represent different things and we can 910 00:46:39,079 --> 00:46:47,359 actually check what type of value 911 00:46:41,400 --> 00:46:50,400 something is using this code type of and 912 00:46:47,359 --> 00:46:53,800 then space and then a value like the 913 00:46:50,400 --> 00:46:57,400 number two if we press 914 00:46:53,800 --> 00:47:05,119 enter type of will tell us that two 915 00:46:57,400 --> 00:47:09,000 is a number if we do type of quote hello 916 00:47:05,119 --> 00:47:11,319 quote and press enter it will tell us 917 00:47:09,000 --> 00:47:14,599 that hello is a 918 00:47:11,319 --> 00:47:18,720 string now what if we added a string and 919 00:47:14,599 --> 00:47:21,160 a number together for example let's type 920 00:47:18,720 --> 00:47:25,680 the string 921 00:47:21,160 --> 00:47:27,839 hello plus the number three and press 922 00:47:25,680 --> 00:47:31,119 enter 923 00:47:27,839 --> 00:47:33,520 so when we add a string and a number 924 00:47:31,119 --> 00:47:37,440 JavaScript will automatically convert 925 00:47:33,520 --> 00:47:41,599 this number into a string so this gets 926 00:47:37,440 --> 00:47:42,720 converted into the string hello plus the 927 00:47:41,599 --> 00:47:45,640 string 928 00:47:42,720 --> 00:47:49,800 three and then it combines them together 929 00:47:45,640 --> 00:47:54,160 into the string hello 3 this feature is 930 00:47:49,800 --> 00:47:56,960 called type coersion or automatic type 931 00:47:54,160 --> 00:47:59,200 conversion okay now that we understand 932 00:47:56,960 --> 00:48:02,160 understand the basics of strings let's 933 00:47:59,200 --> 00:48:05,240 do some practical examples we're going 934 00:48:02,160 --> 00:48:08,520 to use strings and numbers to create the 935 00:48:05,240 --> 00:48:11,359 text in this section of the project 936 00:48:08,520 --> 00:48:15,280 first make sure that in your cart you 937 00:48:11,359 --> 00:48:19,280 have one basketball with free shipping 938 00:48:15,280 --> 00:48:22,520 and you have one t-shirt also with free 939 00:48:19,280 --> 00:48:26,559 shipping and you have no other 940 00:48:22,520 --> 00:48:29,880 products let's scroll up and we'll start 941 00:48:26,559 --> 00:48:32,680 by creating this text in the top right 942 00:48:29,880 --> 00:48:34,480 so this is the cost of the products but 943 00:48:32,680 --> 00:48:37,640 this time we're going to have a dollar 944 00:48:34,480 --> 00:48:38,839 sign so instead of just a number this 945 00:48:37,640 --> 00:48:41,599 will be 946 00:48:38,839 --> 00:48:44,720 text so one thing we can do to create 947 00:48:41,599 --> 00:48:49,599 this text is just type out the string 948 00:48:44,720 --> 00:48:51,760 directly like quote doll 949 00:48:49,599 --> 00:48:54,960 28.94 and 950 00:48:51,760 --> 00:48:57,319 quote however let's actually calculate 951 00:48:54,960 --> 00:48:59,640 this number using math 952 00:48:57,319 --> 00:49:01,839 remember the advantage of JavaScript is 953 00:48:59,640 --> 00:49:05,480 that it's able to do 954 00:49:01,839 --> 00:49:08,079 calculations so instead of typing out 955 00:49:05,480 --> 00:49:12,119 28.94 we're just going to create the 956 00:49:08,079 --> 00:49:16,599 string Dollar Sign Plus and we're going 957 00:49:12,119 --> 00:49:18,960 to calculate this number using math so 958 00:49:16,599 --> 00:49:22,400 we're going to add the cost of the 959 00:49:18,960 --> 00:49:22,400 basketball which is 960 00:49:22,960 --> 00:49:30,760 $2.95 961 00:49:25,000 --> 00:49:30,760 plus the cost of of the t-shirt which is 962 00:49:31,040 --> 00:49:36,559 $7.99 if we press 963 00:49:33,680 --> 00:49:39,520 enter unfortunately this doesn't look 964 00:49:36,559 --> 00:49:41,920 right instead of doing math and adding 965 00:49:39,520 --> 00:49:44,839 these two numbers together it just sort 966 00:49:41,920 --> 00:49:48,319 of put the numbers one after another 967 00:49:44,839 --> 00:49:51,680 into the string so why is this happening 968 00:49:48,319 --> 00:49:54,280 JavaScript adds from left to right so 969 00:49:51,680 --> 00:49:57,400 the first step is to add these two 970 00:49:54,280 --> 00:50:00,359 values and as we learned if we add a 971 00:49:57,400 --> 00:50:03,040 string and a number JavaScript will 972 00:50:00,359 --> 00:50:06,760 automatically convert this number into a 973 00:50:03,040 --> 00:50:11,040 string so the result of this calculation 974 00:50:06,760 --> 00:50:13,200 will be the string doll 975 00:50:11,040 --> 00:50:17,319 2.95 and 976 00:50:13,200 --> 00:50:22,079 quote the next step is to add 977 00:50:17,319 --> 00:50:25,839 7.99 so it will add 978 00:50:22,079 --> 00:50:27,280 7.99 and now we're adding again a string 979 00:50:25,839 --> 00:50:29,880 and a number 980 00:50:27,280 --> 00:50:33,040 so JavaScript will automatically convert 981 00:50:29,880 --> 00:50:36,400 this number into a string and combine 982 00:50:33,040 --> 00:50:39,400 them together that's why it puts 983 00:50:36,400 --> 00:50:40,640 7.99 at the end of this string instead 984 00:50:39,400 --> 00:50:44,040 of doing 985 00:50:40,640 --> 00:50:47,559 math so what we actually want to do here 986 00:50:44,040 --> 00:50:50,960 is to add these two numbers first and 987 00:50:47,559 --> 00:50:53,960 then combine the total with the string 988 00:50:50,960 --> 00:50:57,240 so how do we do this the solution is 989 00:50:53,960 --> 00:51:00,119 that strings also follow the order of 990 00:50:57,240 --> 00:51:02,799 operations just like numbers and 991 00:51:00,119 --> 00:51:05,480 remember in the order of operations we 992 00:51:02,799 --> 00:51:09,040 can use brackets to control which part 993 00:51:05,480 --> 00:51:11,559 of a calculation gets done first so 994 00:51:09,040 --> 00:51:18,680 let's type this calculation out again 995 00:51:11,559 --> 00:51:18,680 the string Dollar Sign Plus 20.95% 996 00:51:26,680 --> 00:51:32,079 close bracket around this 997 00:51:29,520 --> 00:51:35,799 calculation this tells the computer to 998 00:51:32,079 --> 00:51:38,000 calculate this number first using math 999 00:51:35,799 --> 00:51:41,400 and then combine this result with the 1000 00:51:38,000 --> 00:51:44,799 string after if we press 1001 00:51:41,400 --> 00:51:47,520 enter now this is doing math and it 1002 00:51:44,799 --> 00:51:49,760 solves our original problem 1003 00:51:47,520 --> 00:51:53,160 unfortunately now we have another 1004 00:51:49,760 --> 00:51:55,799 problem this math is inaccurate so 1005 00:51:53,160 --> 00:51:58,400 remember from the previous lesson doing 1006 00:51:55,799 --> 00:51:59,480 calculations with floats can sometimes 1007 00:51:58,400 --> 00:52:01,839 cause 1008 00:51:59,480 --> 00:52:04,920 inaccuracies and the best practice when 1009 00:52:01,839 --> 00:52:07,960 calculating money is always calculate in 1010 00:52:04,920 --> 00:52:11,599 cents and then convert back to dollars 1011 00:52:07,960 --> 00:52:14,520 at the end so at the bottom here we're 1012 00:52:11,599 --> 00:52:20,520 going to add the string 1013 00:52:14,520 --> 00:52:24,799 Dollar Plus open bracket and instead of 1014 00:52:20,520 --> 00:52:28,359 $20.95 we're going to add 1015 00:52:24,799 --> 00:52:32,400 2,95 Plus 1016 00:52:28,359 --> 00:52:34,920 $7.99 and close bracket and then at the 1017 00:52:32,400 --> 00:52:40,920 end we're going to convert this number 1018 00:52:34,920 --> 00:52:42,280 back to Dollars by dividing by 100 if we 1019 00:52:40,920 --> 00:52:44,880 press 1020 00:52:42,280 --> 00:52:46,680 enter this will give us the text that we 1021 00:52:44,880 --> 00:52:50,200 see in the final 1022 00:52:46,680 --> 00:52:54,160 project again strings follow the same 1023 00:52:50,200 --> 00:52:57,520 order of operations as numbers brackets 1024 00:52:54,160 --> 00:53:00,240 are always calculated first multiply and 1025 00:52:57,520 --> 00:53:03,880 divide are done next and add and 1026 00:53:00,240 --> 00:53:08,040 subtract are done after so adding this 1027 00:53:03,880 --> 00:53:11,359 result to the string is the last 1028 00:53:08,040 --> 00:53:14,720 step now let's do a more complicated 1029 00:53:11,359 --> 00:53:18,359 example let's use strings and numbers to 1030 00:53:14,720 --> 00:53:21,520 create this entire line of text not just 1031 00:53:18,359 --> 00:53:24,839 the dollar text to create this we're 1032 00:53:21,520 --> 00:53:27,280 going to go from left to right let's go 1033 00:53:24,839 --> 00:53:30,559 back in our console and we're going to 1034 00:53:27,280 --> 00:53:34,599 start with the string 1035 00:53:30,559 --> 00:53:37,960 items Open Bracket and here we could 1036 00:53:34,599 --> 00:53:40,599 just type two and close bracket but 1037 00:53:37,960 --> 00:53:44,319 let's actually calculate this number as 1038 00:53:40,599 --> 00:53:46,599 well using math so this number is the 1039 00:53:44,319 --> 00:53:50,640 quantity or the number of products in 1040 00:53:46,599 --> 00:53:53,280 our cart let's go back to our console 1041 00:53:50,640 --> 00:53:55,480 we're going to end the string here and 1042 00:53:53,280 --> 00:53:57,240 we're going to add and we're going to 1043 00:53:55,480 --> 00:54:01,799 calculate the quantity 1044 00:53:57,240 --> 00:54:04,559 so we have one basketball plus one 1045 00:54:01,799 --> 00:54:07,799 t-shirt again we run into the same 1046 00:54:04,559 --> 00:54:11,119 problem as before this gets calculated 1047 00:54:07,799 --> 00:54:13,599 first and results in a string and then 1048 00:54:11,119 --> 00:54:17,280 it will just combine the one at the end 1049 00:54:13,599 --> 00:54:19,480 of the string instead of doing math to 1050 00:54:17,280 --> 00:54:23,480 solve this we're going to use brackets 1051 00:54:19,480 --> 00:54:26,960 again so around the 1 + 1 we're going to 1052 00:54:23,480 --> 00:54:30,119 add Open Bracket and close 1053 00:54:26,960 --> 00:54:33,000 bracket this tells the computer to add 1054 00:54:30,119 --> 00:54:34,119 the numbers first and then combine it 1055 00:54:33,000 --> 00:54:37,240 with a 1056 00:54:34,119 --> 00:54:40,839 string all right let's keep going from 1057 00:54:37,240 --> 00:54:44,760 left to right we're going to add the 1058 00:54:40,839 --> 00:54:47,799 rest of the string quote close bracket 1059 00:54:44,760 --> 00:54:50,760 colon and let's just use a space to 1060 00:54:47,799 --> 00:54:54,680 represent the space between these two so 1061 00:54:50,760 --> 00:54:56,799 we'll type a space dollar sign and then 1062 00:54:54,680 --> 00:55:00,440 we're going to calculate this number 1063 00:54:56,799 --> 00:55:04,359 again and add it to the string so we'll 1064 00:55:00,440 --> 00:55:07,559 end the string and then plus and we 1065 00:55:04,359 --> 00:55:11,000 already did this calculation up here so 1066 00:55:07,559 --> 00:55:12,960 we can select this and then 1067 00:55:11,000 --> 00:55:17,520 rightclick 1068 00:55:12,960 --> 00:55:20,880 copy and here right click and 1069 00:55:17,520 --> 00:55:23,680 paste and press 1070 00:55:20,880 --> 00:55:25,760 enter by the way if you paste in the 1071 00:55:23,680 --> 00:55:26,960 console you might see this warning 1072 00:55:25,760 --> 00:55:31,200 message 1073 00:55:26,960 --> 00:55:31,200 to fix this you can type allow 1074 00:55:32,000 --> 00:55:37,960 pasting and press 1075 00:55:34,160 --> 00:55:37,960 enter and now you can 1076 00:55:39,920 --> 00:55:45,079 paste and that's how we create this 1077 00:55:42,440 --> 00:55:47,799 first line of text using strings and 1078 00:55:45,079 --> 00:55:51,079 numbers we break up the string into 1079 00:55:47,799 --> 00:55:52,760 Parts calculate the numbers and then add 1080 00:55:51,079 --> 00:55:55,440 the parts back 1081 00:55:52,760 --> 00:55:57,440 together all right now let's take these 1082 00:55:55,440 --> 00:56:01,160 strings that we created Creed and do 1083 00:55:57,440 --> 00:56:03,720 something with them let's create a popup 1084 00:56:01,160 --> 00:56:06,760 and display this first line of text 1085 00:56:03,720 --> 00:56:10,200 inside the popup we'll go back to our 1086 00:56:06,760 --> 00:56:11,920 console and type the code for creating a 1087 00:56:10,200 --> 00:56:15,200 popup 1088 00:56:11,920 --> 00:56:18,200 alert Open Bracket close bracket 1089 00:56:15,200 --> 00:56:21,039 semicolon and then inside the popup 1090 00:56:18,200 --> 00:56:24,359 we're going to display this string so 1091 00:56:21,039 --> 00:56:28,359 let's just make a copy of this code 1092 00:56:24,359 --> 00:56:31,520 we'll select it right click 1093 00:56:28,359 --> 00:56:34,119 copy and between the brackets right 1094 00:56:31,520 --> 00:56:36,520 click and 1095 00:56:34,119 --> 00:56:39,880 paste if we press 1096 00:56:36,520 --> 00:56:41,640 enter the computer displays a popup with 1097 00:56:39,880 --> 00:56:43,240 the first line of text of the final 1098 00:56:41,640 --> 00:56:46,200 project 1099 00:56:43,240 --> 00:56:48,880 inside so that's how we use strings to 1100 00:56:46,200 --> 00:56:51,200 create the text in the final project and 1101 00:56:48,880 --> 00:56:55,000 once we create the strings we can use 1102 00:56:51,200 --> 00:56:57,760 them in other JavaScript 1103 00:56:55,000 --> 00:57:00,319 code in in the rest of this lesson we're 1104 00:56:57,760 --> 00:57:03,440 going to learn more details about 1105 00:57:00,319 --> 00:57:06,760 strings in JavaScript there are actually 1106 00:57:03,440 --> 00:57:09,400 three ways to create a string the first 1107 00:57:06,760 --> 00:57:13,240 way is to use single quotes which we 1108 00:57:09,400 --> 00:57:17,880 already learned for example we can type 1109 00:57:13,240 --> 00:57:19,559 single quote hello and single quote and 1110 00:57:17,880 --> 00:57:22,640 press 1111 00:57:19,559 --> 00:57:26,319 enter the second way to create a string 1112 00:57:22,640 --> 00:57:30,839 is to use double quotes for example 1113 00:57:26,319 --> 00:57:32,480 double quote hello and double quote and 1114 00:57:30,839 --> 00:57:35,920 press 1115 00:57:32,480 --> 00:57:38,960 enter this creates the exact same string 1116 00:57:35,920 --> 00:57:42,559 as single quotes so which one should we 1117 00:57:38,960 --> 00:57:45,799 use to create a string single quotes or 1118 00:57:42,559 --> 00:57:48,799 double quotes in JavaScript I recommend 1119 00:57:45,799 --> 00:57:52,079 using single quotes by default they're a 1120 00:57:48,799 --> 00:57:54,440 little easier to read and type there is 1121 00:57:52,079 --> 00:57:57,160 one situation where double quotes might 1122 00:57:54,440 --> 00:58:00,440 be useful and that is is if we have a 1123 00:57:57,160 --> 00:58:03,200 single quote inside the string for 1124 00:58:00,440 --> 00:58:06,480 example if we type the 1125 00:58:03,200 --> 00:58:09,160 string I'm 1126 00:58:06,480 --> 00:58:13,039 learning 1127 00:58:09,160 --> 00:58:15,960 JavaScript and single quote and press 1128 00:58:13,039 --> 00:58:18,720 enter this doesn't work because there's 1129 00:58:15,960 --> 00:58:21,760 a single quote inside the string and 1130 00:58:18,720 --> 00:58:24,640 this single quote will end the string 1131 00:58:21,760 --> 00:58:28,480 early to solve this we can create the 1132 00:58:24,640 --> 00:58:31,640 string using double quotes for example 1133 00:58:28,480 --> 00:58:33,839 double quote I'm 1134 00:58:31,640 --> 00:58:37,839 learning 1135 00:58:33,839 --> 00:58:41,240 JavaScript and double quote and press 1136 00:58:37,839 --> 00:58:44,079 enter so now this works because we 1137 00:58:41,240 --> 00:58:46,559 created the string using double quotes 1138 00:58:44,079 --> 00:58:48,839 the single quote no longer has a special 1139 00:58:46,559 --> 00:58:52,039 meaning and it will no longer end the 1140 00:58:48,839 --> 00:58:54,559 string early so this is a situation 1141 00:58:52,039 --> 00:58:57,440 where double quotes might be useful 1142 00:58:54,559 --> 00:58:59,799 otherwise we use single quotes by 1143 00:58:57,440 --> 00:59:01,960 default now there's actually another 1144 00:58:59,799 --> 00:59:04,960 solution to this problem other than 1145 00:59:01,960 --> 00:59:08,960 using double quotes and that's by using 1146 00:59:04,960 --> 00:59:12,799 an escape character so a character is 1147 00:59:08,960 --> 00:59:17,520 one letter number or symbol in a piece 1148 00:59:12,799 --> 00:59:22,280 of textt for example the text hello has 1149 00:59:17,520 --> 00:59:26,960 five characters h e l l 1150 00:59:22,280 --> 00:59:29,880 o a character can be a letter in number 1151 00:59:26,960 --> 00:59:31,119 or also a symbol these are all 1152 00:59:29,880 --> 00:59:33,520 considered 1153 00:59:31,119 --> 00:59:36,240 characters now in addition to these 1154 00:59:33,520 --> 00:59:39,200 characters we have a special set of 1155 00:59:36,240 --> 00:59:42,480 characters we can also use in a string 1156 00:59:39,200 --> 00:59:46,119 called Escape characters an escape 1157 00:59:42,480 --> 00:59:49,039 character looks like this a backslash 1158 00:59:46,119 --> 00:59:52,200 and then another character like single 1159 00:59:49,039 --> 00:59:55,359 quote this combination actually counts 1160 00:59:52,200 --> 00:59:57,599 as one character in a string this 1161 00:59:55,359 --> 01:00:00,400 character creat creat a single quote 1162 00:59:57,599 --> 01:00:02,960 that is just text it doesn't have a 1163 01:00:00,400 --> 01:00:07,039 special meaning and it doesn't start or 1164 01:00:02,960 --> 01:00:10,240 end strings for example we can create 1165 01:00:07,039 --> 01:00:14,039 this string with single quotes again so 1166 01:00:10,240 --> 01:00:17,039 single quote I and this time we'll use 1167 01:00:14,039 --> 01:00:21,079 the Escape character back slash single 1168 01:00:17,039 --> 01:00:23,599 quote and then M 1169 01:00:21,079 --> 01:00:27,839 learning 1170 01:00:23,599 --> 01:00:31,160 JavaScript and single quote and press 1171 01:00:27,839 --> 01:00:34,319 enter and now this works because the 1172 01:00:31,160 --> 01:00:37,400 Escape character back SL single quote 1173 01:00:34,319 --> 01:00:39,839 creates a single quote that is just text 1174 01:00:37,400 --> 01:00:42,280 it doesn't start or end the 1175 01:00:39,839 --> 01:00:46,119 string another Escape character that's 1176 01:00:42,280 --> 01:00:48,760 useful to know is back SL double quote 1177 01:00:46,119 --> 01:00:52,200 this creates a double quote that is just 1178 01:00:48,760 --> 01:00:54,760 text it doesn't start or end a 1179 01:00:52,200 --> 01:00:58,039 string the last Escape character we'll 1180 01:00:54,760 --> 01:01:01,440 learn in this lesson is back 1181 01:00:58,039 --> 01:01:04,039 sln this is called the new line 1182 01:01:01,440 --> 01:01:07,480 character this Escape character doesn't 1183 01:01:04,039 --> 01:01:11,720 create the letter N anymore it creates a 1184 01:01:07,480 --> 01:01:15,079 new line of text for example let's 1185 01:01:11,720 --> 01:01:18,520 create a popup using 1186 01:01:15,079 --> 01:01:21,319 alert Open Bracket close bracket and 1187 01:01:18,520 --> 01:01:25,640 semicolon and then inside the brackets 1188 01:01:21,319 --> 01:01:26,920 we're going to create a string sum and 1189 01:01:25,640 --> 01:01:32,960 then back 1190 01:01:26,920 --> 01:01:35,520 sln text and single quote if we press 1191 01:01:32,960 --> 01:01:39,200 enter you'll notice that this Escape 1192 01:01:35,520 --> 01:01:41,880 character created a new line of text in 1193 01:01:39,200 --> 01:01:44,920 the popup and that's why we call this 1194 01:01:41,880 --> 01:01:47,799 the new line character so that's how 1195 01:01:44,920 --> 01:01:49,960 Escape characters work they are a set of 1196 01:01:47,799 --> 01:01:52,599 special characters that start with back 1197 01:01:49,960 --> 01:01:56,079 slash and we can use them in a 1198 01:01:52,599 --> 01:01:58,079 string now let's press okay 1199 01:01:56,079 --> 01:02:01,000 and we're going to learn the Third Way 1200 01:01:58,079 --> 01:02:04,799 of creating a string in JavaScript which 1201 01:02:01,000 --> 01:02:08,119 is using back ticks for example let's 1202 01:02:04,799 --> 01:02:13,960 type in our console and we'll type 1203 01:02:08,119 --> 01:02:17,279 backtick hello and backtick and press 1204 01:02:13,960 --> 01:02:20,680 enter this creates the same string as 1205 01:02:17,279 --> 01:02:23,680 single quotes and double quotes strings 1206 01:02:20,680 --> 01:02:26,279 created with backx actually have a name 1207 01:02:23,680 --> 01:02:28,000 we call these template strings and they 1208 01:02:26,279 --> 01:02:30,799 have a name because they have some 1209 01:02:28,000 --> 01:02:34,039 special features the first special 1210 01:02:30,799 --> 01:02:36,839 feature of template strings is called 1211 01:02:34,039 --> 01:02:40,400 interpolation interpolation lets us 1212 01:02:36,839 --> 01:02:43,240 insert a value directly into a string 1213 01:02:40,400 --> 01:02:46,200 for example let's say we want to create 1214 01:02:43,240 --> 01:02:48,599 the first line of text in our project 1215 01:02:46,200 --> 01:02:51,559 earlier in this lesson we created this 1216 01:02:48,599 --> 01:02:54,000 text with concatenation which means we 1217 01:02:51,559 --> 01:02:57,160 broke it up into parts and added them 1218 01:02:54,000 --> 01:03:00,440 back together in interpolation gives us 1219 01:02:57,160 --> 01:03:03,000 a much easier way to do this we'll go to 1220 01:03:00,440 --> 01:03:05,200 our console and we'll create this text 1221 01:03:03,000 --> 01:03:07,680 using a template string because 1222 01:03:05,200 --> 01:03:12,720 interpolation is only a feature of 1223 01:03:07,680 --> 01:03:16,319 template strings so we'll type backtick 1224 01:03:12,720 --> 01:03:19,520 items Open Bracket close bracket and 1225 01:03:16,319 --> 01:03:22,079 back tick and now between these brackets 1226 01:03:19,520 --> 01:03:25,319 we want to insert the quantity or the 1227 01:03:22,079 --> 01:03:27,760 number of products in our cart to insert 1228 01:03:25,319 --> 01:03:31,359 this with interpolation we're going to 1229 01:03:27,760 --> 01:03:34,440 type Dollar open curly bracket close 1230 01:03:31,359 --> 01:03:37,359 curly bracket so this combination of 1231 01:03:34,440 --> 01:03:40,400 characters allows us to insert values 1232 01:03:37,359 --> 01:03:42,880 directly into the string between these 1233 01:03:40,400 --> 01:03:47,119 curly brackets we can calculate the 1234 01:03:42,880 --> 01:03:48,119 quantity which is one basketball plus 1235 01:03:47,119 --> 01:03:52,079 one 1236 01:03:48,119 --> 01:03:55,319 t-shirt this will add 1 + 1 which equals 1237 01:03:52,079 --> 01:03:56,760 2 and then insert it directly into the 1238 01:03:55,319 --> 01:03:59,119 string 1239 01:03:56,760 --> 01:04:02,400 let's keep going and create the rest of 1240 01:03:59,119 --> 01:04:07,279 this line so we're going to type at the 1241 01:04:02,400 --> 01:04:09,760 end a Colin space and dollar and now 1242 01:04:07,279 --> 01:04:12,319 we're going to insert another value into 1243 01:04:09,760 --> 01:04:15,359 the string which is the cost of the 1244 01:04:12,319 --> 01:04:18,520 products to insert another value we're 1245 01:04:15,359 --> 01:04:21,680 going to type Dollar open curly bracket 1246 01:04:18,520 --> 01:04:24,079 close curly bracket again in between the 1247 01:04:21,680 --> 01:04:26,359 curly brackets we're going to calculate 1248 01:04:24,079 --> 01:04:29,520 the cost of the products 1249 01:04:26,359 --> 01:04:29,520 which is 1250 01:04:30,680 --> 01:04:35,680 2,95 1251 01:04:33,240 --> 01:04:38,079 plus 1252 01:04:35,680 --> 01:04:40,640 $7.99 and we'll surround this in 1253 01:04:38,079 --> 01:04:43,319 Brackets to make sure we calculate this 1254 01:04:40,640 --> 01:04:47,920 first and at the end we're going to 1255 01:04:43,319 --> 01:04:50,559 convert back to Dollars by dividing by 1256 01:04:47,920 --> 01:04:53,880 100 if we press 1257 01:04:50,559 --> 01:04:57,039 enter this creates the first line of 1258 01:04:53,880 --> 01:04:59,359 text again in our final project 1259 01:04:57,039 --> 01:05:02,200 if we compare this code which uses 1260 01:04:59,359 --> 01:05:05,079 interpolation to our previous code which 1261 01:05:02,200 --> 01:05:08,000 uses concatenation you can see that 1262 01:05:05,079 --> 01:05:10,920 interpolation is a much cleaner way of 1263 01:05:08,000 --> 01:05:13,720 inserting values into a string so 1264 01:05:10,920 --> 01:05:16,599 interpolation is a recommended 1265 01:05:13,720 --> 01:05:18,799 solution next template strings have 1266 01:05:16,599 --> 01:05:22,319 another special feature called 1267 01:05:18,799 --> 01:05:24,920 multi-line strings let's go to our 1268 01:05:22,319 --> 01:05:26,839 console and we're going to type a 1269 01:05:24,920 --> 01:05:29,520 backtick 1270 01:05:26,839 --> 01:05:35,440 sum and then press 1271 01:05:29,520 --> 01:05:38,400 enter and text and back tick notice that 1272 01:05:35,440 --> 01:05:41,760 this string is on multiple lines so we 1273 01:05:38,400 --> 01:05:44,640 call this a multi-line string and this 1274 01:05:41,760 --> 01:05:47,240 feature is only available for template 1275 01:05:44,640 --> 01:05:50,079 strings if we press 1276 01:05:47,240 --> 01:05:52,799 enter you can see that the new line in 1277 01:05:50,079 --> 01:05:55,880 the string just creates the new line 1278 01:05:52,799 --> 01:05:58,359 Escape character that we learned earlier 1279 01:05:55,880 --> 01:06:00,720 so that's the multi-line string feature 1280 01:05:58,359 --> 01:06:03,279 of template strings we're going to use 1281 01:06:00,720 --> 01:06:06,039 multi-line strings a lot later in this 1282 01:06:03,279 --> 01:06:08,920 course but for now I just wanted to 1283 01:06:06,039 --> 01:06:11,039 introduce you to this feature now you 1284 01:06:08,920 --> 01:06:14,680 might be wondering again what should we 1285 01:06:11,039 --> 01:06:17,160 use to create a string single quotes or 1286 01:06:14,680 --> 01:06:19,480 template strings even though template 1287 01:06:17,160 --> 01:06:22,240 strings have more features I find that 1288 01:06:19,480 --> 01:06:24,839 most JavaScript code still uses single 1289 01:06:22,240 --> 01:06:27,559 quotes by default so my personal 1290 01:06:24,839 --> 01:06:30,440 recommendation is to use single quotes 1291 01:06:27,559 --> 01:06:33,319 by default and if we need to insert a 1292 01:06:30,440 --> 01:06:36,640 value into the string or type the string 1293 01:06:33,319 --> 01:06:38,920 on multiple lines then use template 1294 01:06:36,640 --> 01:06:42,200 strings and that's the end of this 1295 01:06:38,920 --> 01:06:46,200 lesson in this lesson we learned about 1296 01:06:42,200 --> 01:06:48,960 strings a string represents text we 1297 01:06:46,200 --> 01:06:51,960 learned how to use strings and numbers 1298 01:06:48,960 --> 01:06:54,000 to create the text in our final project 1299 01:06:51,960 --> 01:06:56,839 we learned three different ways of 1300 01:06:54,000 --> 01:06:59,359 creating a string single quotes double 1301 01:06:56,839 --> 01:07:02,000 quotes and back ticks or template 1302 01:06:59,359 --> 01:07:04,720 strings we learned about Escape 1303 01:07:02,000 --> 01:07:06,920 characters and we learned two useful 1304 01:07:04,720 --> 01:07:09,720 features of template strings 1305 01:07:06,920 --> 01:07:12,319 interpolation and multi-line 1306 01:07:09,720 --> 01:07:17,039 strings here are some exercises to help 1307 01:07:12,319 --> 01:07:17,039 you practice working with strings 1308 01:07:35,640 --> 01:07:40,480 so far in this course we've learned some 1309 01:07:37,760 --> 01:07:44,119 basic features of JavaScript like 1310 01:07:40,480 --> 01:07:48,200 numbers and strings in this lesson we're 1311 01:07:44,119 --> 01:07:50,680 going to learn how to use HTML CSS and 1312 01:07:48,200 --> 01:07:52,319 JavaScript together because as I 1313 01:07:50,680 --> 01:07:55,240 mentioned at the beginning of this 1314 01:07:52,319 --> 01:07:57,200 course we use all three Technologies to 1315 01:07:55,240 --> 01:08:00,799 build build a full 1316 01:07:57,200 --> 01:08:04,400 website HTML creates the content of a 1317 01:08:00,799 --> 01:08:07,240 website like the buttons and the text 1318 01:08:04,400 --> 01:08:11,520 CSS changes the appearance of the 1319 01:08:07,240 --> 01:08:13,240 website and JavaScript makes the website 1320 01:08:11,520 --> 01:08:17,080 interactive the first thing we're going 1321 01:08:13,240 --> 01:08:19,759 to do is review the basics of HTML and 1322 01:08:17,080 --> 01:08:20,679 CSS that we will need for the rest of 1323 01:08:19,759 --> 01:08:24,239 this 1324 01:08:20,679 --> 01:08:27,159 course now in order to write HTML and 1325 01:08:24,239 --> 01:08:31,120 CSS code we need to install another 1326 01:08:27,159 --> 01:08:34,000 piece of software called a code editor a 1327 01:08:31,120 --> 01:08:37,199 code editor helps us write and organize 1328 01:08:34,000 --> 01:08:41,279 our code the most popular code editor 1329 01:08:37,199 --> 01:08:45,839 for web development is called vs code or 1330 01:08:41,279 --> 01:08:49,679 Visual Studio code to install vs code 1331 01:08:45,839 --> 01:08:53,480 you can go to google.com in your 1332 01:08:49,679 --> 01:08:56,239 browser search for VSS 1333 01:08:53,480 --> 01:08:58,679 code click the result 1334 01:08:56,239 --> 01:09:00,199 and follow the instructions to download 1335 01:08:58,679 --> 01:09:03,440 and 1336 01:09:00,199 --> 01:09:06,799 install once we've installed VSS code 1337 01:09:03,440 --> 01:09:07,880 we're ready to review the basics of HTML 1338 01:09:06,799 --> 01:09:10,679 and 1339 01:09:07,880 --> 01:09:15,159 CSS we'll start with 1340 01:09:10,679 --> 01:09:18,640 HTML HTML stands for hyper text markup 1341 01:09:15,159 --> 01:09:21,600 language and just like JavaScript HTML 1342 01:09:18,640 --> 01:09:24,799 is also giving instructions to a 1343 01:09:21,600 --> 01:09:28,279 computer now unlike JavaScript we can't 1344 01:09:24,799 --> 01:09:32,960 use the console to write and run HTML 1345 01:09:28,279 --> 01:09:37,000 code instead we write HTML code inside a 1346 01:09:32,960 --> 01:09:39,920 file using our code editor let's do that 1347 01:09:37,000 --> 01:09:42,679 right now we'll start by going to our 1348 01:09:39,920 --> 01:09:44,000 desktop so I'm going to close my browser 1349 01:09:42,679 --> 01:09:46,520 to go to my 1350 01:09:44,000 --> 01:09:52,319 desktop and then we're going to create a 1351 01:09:46,520 --> 01:09:55,040 new folder so we can right click new 1352 01:09:52,319 --> 01:09:57,480 folder this folder is going to contain 1353 01:09:55,040 --> 01:10:03,520 all the code that we write in this 1354 01:09:57,480 --> 01:10:06,040 course let's name this folder JavaScript 1355 01:10:03,520 --> 01:10:09,800 dasc next we're going to open this 1356 01:10:06,040 --> 01:10:11,320 folder in our code editor so let's open 1357 01:10:09,800 --> 01:10:14,640 vs 1358 01:10:11,320 --> 01:10:17,320 code and then we'll click 1359 01:10:14,640 --> 01:10:20,199 file open 1360 01:10:17,320 --> 01:10:23,400 folder and find the folder that we just 1361 01:10:20,199 --> 01:10:25,600 created for me it's on the 1362 01:10:23,400 --> 01:10:28,600 desktop and it's this folder right right 1363 01:10:25,600 --> 01:10:32,000 here so we're going to select the folder 1364 01:10:28,600 --> 01:10:32,000 and then click select 1365 01:10:32,360 --> 01:10:38,120 folder now we can close this startup 1366 01:10:35,840 --> 01:10:41,920 message and we're going to create an 1367 01:10:38,120 --> 01:10:44,080 HTML file we're going to click this icon 1368 01:10:41,920 --> 01:10:48,280 to create a new 1369 01:10:44,080 --> 01:10:50,679 file and we're going to name this file 1370 01:10:48,280 --> 01:10:53,400 website do 1371 01:10:50,679 --> 01:10:56,400 HTML and press 1372 01:10:53,400 --> 01:10:59,239 enter ending the file name name with 1373 01:10:56,400 --> 01:11:03,199 HTML tells a computer that this file 1374 01:10:59,239 --> 01:11:07,120 contains HTML code rather than just 1375 01:11:03,199 --> 01:11:10,440 text now let's review some basic HTML 1376 01:11:07,120 --> 01:11:15,159 code we're going to type the code less 1377 01:11:10,440 --> 01:11:17,800 than button greater than and we also 1378 01:11:15,159 --> 01:11:20,320 need to type less than slash button 1379 01:11:17,800 --> 01:11:23,679 greater than but our code editor might 1380 01:11:20,320 --> 01:11:27,600 autocomplete this for us so this code 1381 01:11:23,679 --> 01:11:29,600 tells a computer to create a button and 1382 01:11:27,600 --> 01:11:32,360 inside the button we're going to have 1383 01:11:29,600 --> 01:11:35,280 the text 1384 01:11:32,360 --> 01:11:36,800 hello now let's save this file by 1385 01:11:35,280 --> 01:11:38,320 clicking 1386 01:11:36,800 --> 01:11:42,760 file 1387 01:11:38,320 --> 01:11:45,159 save or by typing contrl s on Windows or 1388 01:11:42,760 --> 01:11:48,840 command s on 1389 01:11:45,159 --> 01:11:52,199 Mac now to run this HTML code we're 1390 01:11:48,840 --> 01:11:55,679 going to open this file in our browser 1391 01:11:52,199 --> 01:11:59,400 let's go back to our desktop 1392 01:11:55,679 --> 01:11:59,400 we're going to open the folder that we 1393 01:11:59,440 --> 01:12:10,360 created find the HTML file select it and 1394 01:12:04,480 --> 01:12:10,360 then right click open with Google 1395 01:12:10,639 --> 01:12:16,400 Chrome as you can see we have a website 1396 01:12:13,880 --> 01:12:21,480 and we created a button with the text 1397 01:12:16,400 --> 01:12:23,880 hello inside and if we look back at our 1398 01:12:21,480 --> 01:12:25,800 code that's exactly what we told the 1399 01:12:23,880 --> 01:12:29,159 computer to do 1400 01:12:25,800 --> 01:12:32,040 so just like JavaScript HTML is also 1401 01:12:29,159 --> 01:12:35,360 giving instructions to a 1402 01:12:32,040 --> 01:12:38,040 computer let's review some more HTML 1403 01:12:35,360 --> 01:12:40,840 code first I'm just going to rearrange 1404 01:12:38,040 --> 01:12:44,520 my windows here so that it's easy to see 1405 01:12:40,840 --> 01:12:44,520 the website side by 1406 01:12:44,679 --> 01:12:50,639 side next we're going to click the end 1407 01:12:48,040 --> 01:12:52,760 of the first line and press enter to 1408 01:12:50,639 --> 01:12:57,639 create a new line and we're going to 1409 01:12:52,760 --> 01:13:00,560 type the code less than P greater than 1410 01:12:57,639 --> 01:13:04,560 and we also need to type less than SLP 1411 01:13:00,560 --> 01:13:07,639 greater than so this code creates a 1412 01:13:04,560 --> 01:13:11,520 paragraph and inside the paragraph we're 1413 01:13:07,639 --> 01:13:15,639 going to put some text for example 1414 01:13:11,520 --> 01:13:18,600 paragraph of text now let's save our 1415 01:13:15,639 --> 01:13:20,480 file by typing contrl s on Windows or 1416 01:13:18,600 --> 01:13:23,280 command s on 1417 01:13:20,480 --> 01:13:25,920 Mac and to update our website to the new 1418 01:13:23,280 --> 01:13:29,800 code we're going to go to our website 1419 01:13:25,920 --> 01:13:33,800 and click refresh or typ controlr on 1420 01:13:29,800 --> 01:13:35,960 Windows or command r on 1421 01:13:33,800 --> 01:13:39,239 Mac and now you can see that the 1422 01:13:35,960 --> 01:13:42,080 computer created a paragraph of text 1423 01:13:39,239 --> 01:13:45,360 just like we told it to do and that's 1424 01:13:42,080 --> 01:13:47,199 the basics of HTML the computer reads 1425 01:13:45,360 --> 01:13:50,920 our code and then follows our 1426 01:13:47,199 --> 01:13:54,360 instructions one by one first it creates 1427 01:13:50,920 --> 01:13:56,840 a button and then it creates a paragraph 1428 01:13:54,360 --> 01:14:00,440 of text 1429 01:13:56,840 --> 01:14:02,600 now let's review the syntax rules for 1430 01:14:00,440 --> 01:14:04,920 HTML each of these things that were 1431 01:14:02,600 --> 01:14:08,719 displaying on a web page these are 1432 01:14:04,920 --> 01:14:11,880 called HTML elements so a button is a 1433 01:14:08,719 --> 01:14:15,760 type of HTML element and a paragraph is 1434 01:14:11,880 --> 01:14:18,199 another type of HTML element to create 1435 01:14:15,760 --> 01:14:21,960 an element we start with a less than 1436 01:14:18,199 --> 01:14:25,679 symbol and then the element name like a 1437 01:14:21,960 --> 01:14:28,960 button and then the greater than symbol 1438 01:14:25,679 --> 01:14:32,159 so this whole thing is called an HTML 1439 01:14:28,960 --> 01:14:33,639 tag this tells a computer to create a 1440 01:14:32,159 --> 01:14:37,800 button 1441 01:14:33,639 --> 01:14:40,600 element every HTML tag needs a matching 1442 01:14:37,800 --> 01:14:43,560 tag the matching tag follows the same 1443 01:14:40,600 --> 01:14:47,120 syntax except it has a slash in front of 1444 01:14:43,560 --> 01:14:49,639 the element name so this is called the 1445 01:14:47,120 --> 01:14:52,199 opening tag and this is called the 1446 01:14:49,639 --> 01:14:54,520 closing tag you can think of the opening 1447 01:14:52,199 --> 01:14:57,840 tag as the start of the button and the 1448 01:14:54,520 --> 01:15:00,199 closing tag tag as the end of the button 1449 01:14:57,840 --> 01:15:03,159 and in between we have the contents 1450 01:15:00,199 --> 01:15:04,360 inside the button in this case it's the 1451 01:15:03,159 --> 01:15:07,400 text 1452 01:15:04,360 --> 01:15:10,679 hello in addition to text we can also 1453 01:15:07,400 --> 01:15:13,320 have elements inside an element for 1454 01:15:10,679 --> 01:15:17,199 example we can go to our paragraph 1455 01:15:13,320 --> 01:15:21,679 element and we can add a button inside 1456 01:15:17,199 --> 01:15:24,120 so less than button greater than and we 1457 01:15:21,679 --> 01:15:26,679 also need less than SL button greater 1458 01:15:24,120 --> 01:15:28,880 than and then inside the button we'll 1459 01:15:26,679 --> 01:15:33,080 also have the text 1460 01:15:28,880 --> 01:15:34,320 hello if we save this file and refresh 1461 01:15:33,080 --> 01:15:37,400 the 1462 01:15:34,320 --> 01:15:39,120 page now our paragraph element has a 1463 01:15:37,400 --> 01:15:42,960 button element 1464 01:15:39,120 --> 01:15:46,159 inside by the way this is called nesting 1465 01:15:42,960 --> 01:15:48,639 when we have an element inside another 1466 01:15:46,159 --> 01:15:52,040 element the last syntax rule we have to 1467 01:15:48,639 --> 01:15:55,239 review is that in HTML if we have 1468 01:15:52,040 --> 01:15:58,080 multiple spaces for example if we go 1469 01:15:55,239 --> 01:16:01,280 here and type multiple 1470 01:15:58,080 --> 01:16:05,000 spaces multiple spaces are combined into 1471 01:16:01,280 --> 01:16:07,639 one space on the web page so if we save 1472 01:16:05,000 --> 01:16:09,880 this code and 1473 01:16:07,639 --> 01:16:12,760 refresh you'll notice that all of the 1474 01:16:09,880 --> 01:16:16,239 spaces that we added only show up as one 1475 01:16:12,760 --> 01:16:20,639 space on the web page in addition in 1476 01:16:16,239 --> 01:16:22,560 HTML new lines also count as spaces so 1477 01:16:20,639 --> 01:16:26,639 if we go back to our 1478 01:16:22,560 --> 01:16:32,239 code and we type here and press enter to 1479 01:16:26,639 --> 01:16:34,920 create a new line and then save and 1480 01:16:32,239 --> 01:16:37,320 refresh notice that the new line doesn't 1481 01:16:34,920 --> 01:16:40,600 appear on the web page and that's 1482 01:16:37,320 --> 01:16:44,080 because this new line here is the same 1483 01:16:40,600 --> 01:16:47,400 as spaces in HTML so all of these get 1484 01:16:44,080 --> 01:16:50,120 combined into one space on the web 1485 01:16:47,400 --> 01:16:52,560 page at first this might seem a little 1486 01:16:50,120 --> 01:16:55,480 weird but this feature gives us a lot of 1487 01:16:52,560 --> 01:16:59,600 flexibility when organizing our h ml 1488 01:16:55,480 --> 01:17:03,000 code for example we can go into our code 1489 01:16:59,600 --> 01:17:04,960 and add a new line here to separate 1490 01:17:03,000 --> 01:17:07,480 these two elements and make our code 1491 01:17:04,960 --> 01:17:10,600 easier to read another thing we usually 1492 01:17:07,480 --> 01:17:13,280 do to make our code easier to read is to 1493 01:17:10,600 --> 01:17:16,719 put the contents of an element on its 1494 01:17:13,280 --> 01:17:21,239 own line for example we're going to type 1495 01:17:16,719 --> 01:17:22,360 here and press enter and here and press 1496 01:17:21,239 --> 01:17:24,920 enter 1497 01:17:22,360 --> 01:17:27,719 again to put the contents of the button 1498 01:17:24,920 --> 01:17:30,600 Buton on its own line now it's a lot 1499 01:17:27,719 --> 01:17:33,400 easier to see where this button starts 1500 01:17:30,600 --> 01:17:35,840 and where it ends we can do the same 1501 01:17:33,400 --> 01:17:39,400 thing with the paragraph We can type 1502 01:17:35,840 --> 01:17:42,920 here and press enter and type here as 1503 01:17:39,400 --> 01:17:44,719 well and press enter and then remove 1504 01:17:42,920 --> 01:17:47,000 some of the Extra 1505 01:17:44,719 --> 01:17:50,480 Spaces to put the content of the 1506 01:17:47,000 --> 01:17:51,560 paragraph on its own line and now if we 1507 01:17:50,480 --> 01:17:55,280 save our 1508 01:17:51,560 --> 01:17:57,040 file and refresh 1509 01:17:55,280 --> 01:18:00,320 you notice that none of the new lines 1510 01:17:57,040 --> 01:18:03,719 that we added appear on the web page by 1511 01:18:00,320 --> 01:18:07,159 the way a web page is a single page like 1512 01:18:03,719 --> 01:18:09,360 the homepage or the cart page together 1513 01:18:07,159 --> 01:18:13,199 we call this a 1514 01:18:09,360 --> 01:18:15,000 website so that's the basics of HTML and 1515 01:18:13,199 --> 01:18:19,560 HTML 1516 01:18:15,000 --> 01:18:23,159 syntax now let's review CSS CSS stands 1517 01:18:19,560 --> 01:18:26,239 for cascading style sheets and CSS 1518 01:18:23,159 --> 01:18:29,360 changes the appearance of our website 1519 01:18:26,239 --> 01:18:33,320 let's review some CSS code we're going 1520 01:18:29,360 --> 01:18:37,000 to write our CSS code inside our HTML 1521 01:18:33,320 --> 01:18:40,280 file first let's type here and create 1522 01:18:37,000 --> 01:18:43,080 some new lines at the top and then at 1523 01:18:40,280 --> 01:18:47,600 the top we're going to create a special 1524 01:18:43,080 --> 01:18:52,080 HTML element called the style element so 1525 01:18:47,600 --> 01:18:55,040 let's type less than style greater 1526 01:18:52,080 --> 01:18:58,840 than and we also need the closing tag 1527 01:18:55,040 --> 01:19:01,239 less than SL style greater than so the 1528 01:18:58,840 --> 01:19:04,560 style element is unique because it 1529 01:19:01,239 --> 01:19:07,520 doesn't appear visibly on the page 1530 01:19:04,560 --> 01:19:10,920 instead the purpose of the style element 1531 01:19:07,520 --> 01:19:14,560 is that it lets us write CSS code 1532 01:19:10,920 --> 01:19:17,000 inside so inside here let's press enter 1533 01:19:14,560 --> 01:19:18,679 to create a new line and we're going to 1534 01:19:17,000 --> 01:19:22,480 type 1535 01:19:18,679 --> 01:19:24,840 button open curly bracket close curly 1536 01:19:22,480 --> 01:19:27,719 bracket and then inside the curly 1537 01:19:24,840 --> 01:19:32,400 brackets we're also going to press enter 1538 01:19:27,719 --> 01:19:34,840 to create a new line so this is CSS code 1539 01:19:32,400 --> 01:19:37,440 and this code tells a computer to change 1540 01:19:34,840 --> 01:19:39,960 the appearance of all buttons on the 1541 01:19:37,440 --> 01:19:42,560 page and then inside these curly 1542 01:19:39,960 --> 01:19:45,800 brackets we can tell the computer how to 1543 01:19:42,560 --> 01:19:47,840 change the appearance for example we can 1544 01:19:45,800 --> 01:19:52,960 type 1545 01:19:47,840 --> 01:19:55,760 background- color colon red and 1546 01:19:52,960 --> 01:19:57,440 semicolon if we save 1547 01:19:55,760 --> 01:20:00,639 and 1548 01:19:57,440 --> 01:20:02,880 refresh you can see that this CSS code 1549 01:20:00,639 --> 01:20:05,159 changes the appearance of all buttons on 1550 01:20:02,880 --> 01:20:07,080 the page and changes their background 1551 01:20:05,159 --> 01:20:10,360 color to 1552 01:20:07,080 --> 01:20:13,480 Red let's do another example we're going 1553 01:20:10,360 --> 01:20:16,080 to go back to our CSS code going to type 1554 01:20:13,480 --> 01:20:21,159 here and press enter to create a new 1555 01:20:16,080 --> 01:20:25,760 line and we're going to add color colon 1556 01:20:21,159 --> 01:20:28,040 white and semicolon let's save 1557 01:20:25,760 --> 01:20:31,320 and 1558 01:20:28,040 --> 01:20:35,080 refresh and this CSS code changes the 1559 01:20:31,320 --> 01:20:38,520 text color of all buttons on the page to 1560 01:20:35,080 --> 01:20:40,199 White let's do one more example we're 1561 01:20:38,520 --> 01:20:45,320 going to type the 1562 01:20:40,199 --> 01:20:49,639 code order Colon none and 1563 01:20:45,320 --> 01:20:52,719 semicolon let's save this and 1564 01:20:49,639 --> 01:20:56,000 refresh and this CSS code removes the 1565 01:20:52,719 --> 01:20:58,639 border for all buttons on the page 1566 01:20:56,000 --> 01:21:00,760 and that's how CSS works we tell the 1567 01:20:58,639 --> 01:21:03,239 computer which elements we want to 1568 01:21:00,760 --> 01:21:05,840 change and then how to change the 1569 01:21:03,239 --> 01:21:08,400 appearance of these 1570 01:21:05,840 --> 01:21:09,719 elements now let's review the syntax 1571 01:21:08,400 --> 01:21:12,960 rules for 1572 01:21:09,719 --> 01:21:16,560 CSS this part of the CSS code is called 1573 01:21:12,960 --> 01:21:19,120 the CSS selector it tells a computer 1574 01:21:16,560 --> 01:21:22,280 which elements we want to change in 1575 01:21:19,120 --> 01:21:25,159 other words it selects which elements to 1576 01:21:22,280 --> 01:21:28,960 change inside the curly brackets we have 1577 01:21:25,159 --> 01:21:30,719 some CSS Styles this tells a computer 1578 01:21:28,960 --> 01:21:34,080 how to change the 1579 01:21:30,719 --> 01:21:37,280 appearance for each style the left side 1580 01:21:34,080 --> 01:21:40,239 is called the CSS property this tells a 1581 01:21:37,280 --> 01:21:43,600 computer what we're changing the right 1582 01:21:40,239 --> 01:21:45,800 side is called the CSS value this tells 1583 01:21:43,600 --> 01:21:49,440 a computer what we're changing the 1584 01:21:45,800 --> 01:21:52,239 property to for example this style 1585 01:21:49,440 --> 01:21:55,360 changes the background color to 1586 01:21:52,239 --> 01:21:58,639 Red we use a colon to separate the 1587 01:21:55,360 --> 01:22:01,280 property and the value and a semicolon 1588 01:21:58,639 --> 01:22:04,760 at the end to tell the computer that 1589 01:22:01,280 --> 01:22:08,360 this is the end of a style so that's the 1590 01:22:04,760 --> 01:22:10,679 basic syntax of CSS we tell the computer 1591 01:22:08,360 --> 01:22:12,840 which elements we want to change and 1592 01:22:10,679 --> 01:22:14,159 then how to change the appearance of 1593 01:22:12,840 --> 01:22:16,080 these 1594 01:22:14,159 --> 01:22:18,600 elements the next thing we're going to 1595 01:22:16,080 --> 01:22:22,560 review is HTML 1596 01:22:18,600 --> 01:22:24,520 attributes right now this CSS selector 1597 01:22:22,560 --> 01:22:27,800 changes the appearance of all but 1598 01:22:24,520 --> 01:22:29,520 buttons on the page however what if we 1599 01:22:27,800 --> 01:22:32,080 only want to change the appearance of 1600 01:22:29,520 --> 01:22:34,440 this one button and we want to style the 1601 01:22:32,080 --> 01:22:36,719 other button differently how do we do 1602 01:22:34,440 --> 01:22:40,480 that we're going to review a feature of 1603 01:22:36,719 --> 01:22:44,280 HTML called attributes attributes change 1604 01:22:40,480 --> 01:22:46,560 the behavior of an element for example 1605 01:22:44,280 --> 01:22:49,639 we're going to go to the opening tag of 1606 01:22:46,560 --> 01:22:53,440 the first button and then here we're 1607 01:22:49,639 --> 01:22:55,360 going to add an HTML attribute by typing 1608 01:22:53,440 --> 01:22:59,920 space 1609 01:22:55,360 --> 01:23:02,840 title equals double quotes and then in 1610 01:22:59,920 --> 01:23:05,280 between the double quotes we're going to 1611 01:23:02,840 --> 01:23:09,480 type tool 1612 01:23:05,280 --> 01:23:12,120 tip so this is an HTML attribute and 1613 01:23:09,480 --> 01:23:14,440 this attribute adds a tool tip to this 1614 01:23:12,120 --> 01:23:17,480 button if you're wondering what a tool 1615 01:23:14,440 --> 01:23:20,199 tip is if we hover over some code in our 1616 01:23:17,480 --> 01:23:21,840 code editor it'll show us this sort of 1617 01:23:20,199 --> 01:23:24,719 popup with more 1618 01:23:21,840 --> 01:23:26,600 information this is called a tool tip 1619 01:23:24,719 --> 01:23:29,880 when we hover over something and this 1620 01:23:26,600 --> 01:23:34,080 shows up so if we save our 1621 01:23:29,880 --> 01:23:37,080 file and refresh the web page and now we 1622 01:23:34,080 --> 01:23:40,400 hover over our button we can see that 1623 01:23:37,080 --> 01:23:43,480 the HTML attribute added a tool tip to 1624 01:23:40,400 --> 01:23:47,400 our button and then inside the double 1625 01:23:43,480 --> 01:23:49,840 quotes is the text inside the tool tip 1626 01:23:47,400 --> 01:23:52,800 for example we can actually change this 1627 01:23:49,840 --> 01:23:57,320 to good 1628 01:23:52,800 --> 01:24:00,480 job and if we save save and 1629 01:23:57,320 --> 01:24:02,440 refresh and hover over our button the 1630 01:24:00,480 --> 01:24:05,679 text between the double quotes is what 1631 01:24:02,440 --> 01:24:08,520 shows up in our tool tip so that's an 1632 01:24:05,679 --> 01:24:11,080 example of an HTML 1633 01:24:08,520 --> 01:24:12,520 attribute now let's review the syntax 1634 01:24:11,080 --> 01:24:15,320 rules for 1635 01:24:12,520 --> 01:24:18,400 attributes we always put attributes on 1636 01:24:15,320 --> 01:24:20,159 the opening tag and we add a space 1637 01:24:18,400 --> 01:24:23,480 between the element name and the 1638 01:24:20,159 --> 01:24:26,280 attribute to separate them on the left 1639 01:24:23,480 --> 01:24:28,560 side is the attribute name this tells 1640 01:24:26,280 --> 01:24:31,520 the computer what we're changing about 1641 01:24:28,560 --> 01:24:34,600 this element on the right side is the 1642 01:24:31,520 --> 01:24:37,840 attribute value this tells a computer 1643 01:24:34,600 --> 01:24:40,679 what we're changing this attribute to 1644 01:24:37,840 --> 01:24:44,800 for example here we're changing the tool 1645 01:24:40,679 --> 01:24:47,280 tip of this element to good job we 1646 01:24:44,800 --> 01:24:49,560 separate the name and value with an 1647 01:24:47,280 --> 01:24:52,080 equal sign and make sure there are no 1648 01:24:49,560 --> 01:24:56,000 spaces around the equal sign and we 1649 01:24:52,080 --> 01:24:58,000 surround the value with double quotes 1650 01:24:56,000 --> 01:25:01,040 now let's go back to the original 1651 01:24:58,000 --> 01:25:03,719 problem how do we style these buttons 1652 01:25:01,040 --> 01:25:06,199 differently we can do this using a very 1653 01:25:03,719 --> 01:25:09,360 important attribute called the class 1654 01:25:06,199 --> 01:25:11,880 attribute let's review that right now 1655 01:25:09,360 --> 01:25:16,159 we're going to go back to our button and 1656 01:25:11,880 --> 01:25:20,040 then here we're going to type space 1657 01:25:16,159 --> 01:25:21,679 class equals double quotes and then 1658 01:25:20,040 --> 01:25:25,280 inside the double quotes we're going to 1659 01:25:21,679 --> 01:25:28,480 type red Das 1660 01:25:25,280 --> 01:25:32,280 button so the class attribute sort of 1661 01:25:28,480 --> 01:25:36,159 adds a label to an element so here we 1662 01:25:32,280 --> 01:25:38,480 labeled this button as a red button and 1663 01:25:36,159 --> 01:25:40,040 now that we labeled this element we can 1664 01:25:38,480 --> 01:25:42,719 select it in 1665 01:25:40,040 --> 01:25:46,040 CSS to do that we're going to go to our 1666 01:25:42,719 --> 01:25:46,880 CSS code we're going to remove the 1667 01:25:46,040 --> 01:25:49,440 button 1668 01:25:46,880 --> 01:25:51,159 selector and replace it 1669 01:25:49,440 --> 01:25:52,840 witht 1670 01:25:51,159 --> 01:25:55,440 red- 1671 01:25:52,840 --> 01:25:58,119 button when we start the selector with a 1672 01:25:55,440 --> 01:26:01,119 DOT this means that now we're looking 1673 01:25:58,119 --> 01:26:04,280 for a class so here we're going to look 1674 01:26:01,119 --> 01:26:09,280 for all elements with the class red dash 1675 01:26:04,280 --> 01:26:12,520 button which is only this element if we 1676 01:26:09,280 --> 01:26:15,320 save and 1677 01:26:12,520 --> 01:26:18,920 refresh now only the elements with the 1678 01:26:15,320 --> 01:26:21,480 class red button gets these Styles we 1679 01:26:18,920 --> 01:26:24,760 can also add the same class to multiple 1680 01:26:21,480 --> 01:26:25,679 elements for example we can go to to our 1681 01:26:24,760 --> 01:26:31,000 other 1682 01:26:25,679 --> 01:26:32,840 button and add the class equals 1683 01:26:31,000 --> 01:26:37,320 red- 1684 01:26:32,840 --> 01:26:40,040 button if we save and 1685 01:26:37,320 --> 01:26:42,280 refresh now these Styles get applied to 1686 01:26:40,040 --> 01:26:45,800 both buttons again because they both 1687 01:26:42,280 --> 01:26:48,159 have the class red-b button if we want 1688 01:26:45,800 --> 01:26:51,480 to style this button differently we just 1689 01:26:48,159 --> 01:26:55,480 give it a different class for example we 1690 01:26:51,480 --> 01:26:59,719 can change this to Yellow yellow dash 1691 01:26:55,480 --> 01:27:03,280 button and then in our CSS we can add 1692 01:26:59,719 --> 01:27:06,280 another selector so we can type dot this 1693 01:27:03,280 --> 01:27:10,159 means we're going to select a class and 1694 01:27:06,280 --> 01:27:13,760 we're going to select the yellow Das 1695 01:27:10,159 --> 01:27:17,199 button class open curly bracket close 1696 01:27:13,760 --> 01:27:19,360 curly bracket and then inside here we 1697 01:27:17,199 --> 01:27:21,119 can add some styles for the yellow 1698 01:27:19,360 --> 01:27:26,600 button for 1699 01:27:21,119 --> 01:27:28,440 example background Das color colon 1700 01:27:26,600 --> 01:27:33,280 yellow and 1701 01:27:28,440 --> 01:27:36,000 semicolon now let's save and 1702 01:27:33,280 --> 01:27:40,360 refresh and that's how we use the class 1703 01:27:36,000 --> 01:27:42,400 attribute in HTML to modify specific 1704 01:27:40,360 --> 01:27:45,719 elements the final thing we need to 1705 01:27:42,400 --> 01:27:48,480 review is something called the HTML 1706 01:27:45,719 --> 01:27:51,080 structure let's go back to our website 1707 01:27:48,480 --> 01:27:51,880 we're going to find a blank area and 1708 01:27:51,080 --> 01:27:56,119 then 1709 01:27:51,880 --> 01:27:56,119 rightclick and then click inspect 1710 01:27:56,560 --> 01:28:03,920 so we already learned about the console 1711 01:28:00,000 --> 01:28:06,280 tab here we can write and run JavaScript 1712 01:28:03,920 --> 01:28:10,920 code now we're going to go to the 1713 01:28:06,280 --> 01:28:13,639 elements tab which is for HTML and CSS 1714 01:28:10,920 --> 01:28:17,639 so in the elements tab we can see all 1715 01:28:13,639 --> 01:28:20,480 the HTML on the page as well as all the 1716 01:28:17,639 --> 01:28:22,360 CSS on the page and we can play around 1717 01:28:20,480 --> 01:28:25,520 with the CSS if we 1718 01:28:22,360 --> 01:28:28,440 want now if we go back to to the 1719 01:28:25,520 --> 01:28:31,239 HTML you'll notice that our web page has 1720 01:28:28,440 --> 01:28:34,600 some elements that we didn't add before 1721 01:28:31,239 --> 01:28:36,400 the HTML element the head element and 1722 01:28:34,600 --> 01:28:38,719 the body 1723 01:28:36,400 --> 01:28:41,800 element so this is the structure that 1724 01:28:38,719 --> 01:28:44,840 we're supposed to follow when writing 1725 01:28:41,800 --> 01:28:47,080 HTML in our code we didn't follow the 1726 01:28:44,840 --> 01:28:48,320 structure so the browser actually adds 1727 01:28:47,080 --> 01:28:50,719 it for us 1728 01:28:48,320 --> 01:28:53,199 automatically however it's still best 1729 01:28:50,719 --> 01:28:55,480 practice to follow this structure in our 1730 01:28:53,199 --> 01:28:57,440 code because it gives us access to some 1731 01:28:55,480 --> 01:29:00,920 more features of 1732 01:28:57,440 --> 01:29:02,440 HTML let's review the HTML structure and 1733 01:29:00,920 --> 01:29:05,760 add it to our 1734 01:29:02,440 --> 01:29:07,719 code we're going to type up here and 1735 01:29:05,760 --> 01:29:11,560 we're going to create some new 1736 01:29:07,719 --> 01:29:14,960 lines and now every HTML file is going 1737 01:29:11,560 --> 01:29:18,320 to start with less than 1738 01:29:14,960 --> 01:29:20,400 exclamation dock type in capitals and 1739 01:29:18,320 --> 01:29:22,840 then space 1740 01:29:20,400 --> 01:29:27,000 HTML and greater 1741 01:29:22,840 --> 01:29:28,639 than this is not an HTML element it's a 1742 01:29:27,000 --> 01:29:31,480 special line of code that tells a 1743 01:29:28,639 --> 01:29:34,400 browser to use a modern version of 1744 01:29:31,480 --> 01:29:36,320 HTML if we don't have this the browser 1745 01:29:34,400 --> 01:29:39,159 might fall back to an older version of 1746 01:29:36,320 --> 01:29:42,400 HTML that has less 1747 01:29:39,159 --> 01:29:46,600 features next we're going to create an 1748 01:29:42,400 --> 01:29:49,320 HTML element so less than 1749 01:29:46,600 --> 01:29:52,960 HTML and greater 1750 01:29:49,320 --> 01:29:54,639 than the HTML element represents the 1751 01:29:52,960 --> 01:29:57,400 entire web page 1752 01:29:54,639 --> 01:29:58,880 so everything on the web page should go 1753 01:29:57,400 --> 01:30:02,080 inside this 1754 01:29:58,880 --> 01:30:05,280 element and then inside the HTML element 1755 01:30:02,080 --> 01:30:08,360 we're also going to split it up into two 1756 01:30:05,280 --> 01:30:13,480 sections the head 1757 01:30:08,360 --> 01:30:16,320 element and the body 1758 01:30:13,480 --> 01:30:18,679 element so the body element is supposed 1759 01:30:16,320 --> 01:30:22,199 to contain everything that is visible on 1760 01:30:18,679 --> 01:30:25,560 the page in our example that would be 1761 01:30:22,199 --> 01:30:27,360 the buttons and the parag graph so let's 1762 01:30:25,560 --> 01:30:29,560 actually select this 1763 01:30:27,360 --> 01:30:32,159 code and we're going to 1764 01:30:29,560 --> 01:30:35,560 rightclick and 1765 01:30:32,159 --> 01:30:41,080 cut and then inside the 1766 01:30:35,560 --> 01:30:41,080 body we're going to right click and 1767 01:30:41,880 --> 01:30:48,400 paste the head element contains 1768 01:30:45,119 --> 01:30:50,159 information about the page in general 1769 01:30:48,400 --> 01:30:52,440 everything that is not visible on the 1770 01:30:50,159 --> 01:30:55,480 page goes inside the 1771 01:30:52,440 --> 01:30:58,320 head in our examp example that would be 1772 01:30:55,480 --> 01:30:59,320 the style element so let's select this 1773 01:30:58,320 --> 01:31:04,000 as 1774 01:30:59,320 --> 01:31:07,840 well we're going to rightclick and 1775 01:31:04,000 --> 01:31:12,159 cut and then inside the 1776 01:31:07,840 --> 01:31:15,280 head we're going to right click and 1777 01:31:12,159 --> 01:31:18,159 paste and at the bottom let's just clean 1778 01:31:15,280 --> 01:31:21,360 up by removing all of the extra new 1779 01:31:18,159 --> 01:31:24,639 lines so this is a structure that we're 1780 01:31:21,360 --> 01:31:27,679 supposed to follow in all of our HTML 1781 01:31:24,639 --> 01:31:30,840 files as you can see this structure uses 1782 01:31:27,679 --> 01:31:33,800 a lot of nesting or elements inside 1783 01:31:30,840 --> 01:31:36,800 elements to organize our 1784 01:31:33,800 --> 01:31:40,040 code okay so what are the benefits of 1785 01:31:36,800 --> 01:31:41,679 following this structure one benefit is 1786 01:31:40,040 --> 01:31:45,440 that there are other elements that we 1787 01:31:41,679 --> 01:31:48,600 can use inside the head for example 1788 01:31:45,440 --> 01:31:53,280 let's type here press enter and we're 1789 01:31:48,600 --> 01:31:56,119 going to add the title element so the 1790 01:31:53,280 --> 01:32:03,800 title element sets the text in the 1791 01:31:56,119 --> 01:32:07,800 tabs inside we can type HTML CSS review 1792 01:32:03,800 --> 01:32:09,480 if we save and refresh the title will 1793 01:32:07,800 --> 01:32:12,480 show up in the 1794 01:32:09,480 --> 01:32:14,760 tab so this is an example of an element 1795 01:32:12,480 --> 01:32:17,239 that we can now add in the 1796 01:32:14,760 --> 01:32:19,760 head another benefit that we get from 1797 01:32:17,239 --> 01:32:22,480 this structure is the ability to 1798 01:32:19,760 --> 01:32:24,840 automatically refresh our web page when 1799 01:32:22,480 --> 01:32:28,080 we change our code 1800 01:32:24,840 --> 01:32:31,400 so far every time we change our code we 1801 01:32:28,080 --> 01:32:34,560 have to save and then manually refresh 1802 01:32:31,400 --> 01:32:37,400 the web page we can actually automate 1803 01:32:34,560 --> 01:32:40,400 this by following the structure and then 1804 01:32:37,400 --> 01:32:43,880 installing a VSS code 1805 01:32:40,400 --> 01:32:46,920 extension so let's click the extension 1806 01:32:43,880 --> 01:32:49,760 area and we're going to search for the 1807 01:32:46,920 --> 01:32:54,199 extension live 1808 01:32:49,760 --> 01:32:55,960 server and click install 1809 01:32:54,199 --> 01:32:57,600 once we have that installed we're going 1810 01:32:55,960 --> 01:33:00,119 to change a 1811 01:32:57,600 --> 01:33:03,000 setting let's open the settings by 1812 01:33:00,119 --> 01:33:05,960 clicking this icon in the bottom left 1813 01:33:03,000 --> 01:33:05,960 and then click 1814 01:33:06,040 --> 01:33:12,960 settings at the top we're going to 1815 01:33:08,520 --> 01:33:14,880 search for live 1816 01:33:12,960 --> 01:33:18,960 server and we're going to look for a 1817 01:33:14,880 --> 01:33:21,119 setting called live server custom 1818 01:33:18,960 --> 01:33:22,560 browser and we're going to change this 1819 01:33:21,119 --> 01:33:25,199 to 1820 01:33:22,560 --> 01:33:27,520 Chrome now now if Google Chrome is 1821 01:33:25,199 --> 01:33:29,719 already your default web browser you 1822 01:33:27,520 --> 01:33:30,400 don't need to do this step but it's nice 1823 01:33:29,719 --> 01:33:33,400 to 1824 01:33:30,400 --> 01:33:37,239 know now let's close the 1825 01:33:33,400 --> 01:33:37,239 settings and go back to our 1826 01:33:37,600 --> 01:33:43,880 code next instead of opening this file 1827 01:33:40,960 --> 01:33:46,159 directly in the browser like before 1828 01:33:43,880 --> 01:33:49,360 we're going to use live server to open 1829 01:33:46,159 --> 01:33:52,639 this file to do that we're going to 1830 01:33:49,360 --> 01:33:56,239 rightclick this file in our code editor 1831 01:33:52,639 --> 01:33:58,800 and then click open open with live 1832 01:33:56,239 --> 01:34:01,760 server if we open a file with live 1833 01:33:58,800 --> 01:34:04,199 server this web page will automatically 1834 01:34:01,760 --> 01:34:08,159 refresh when we change our 1835 01:34:04,199 --> 01:34:12,199 code for example we can go here and 1836 01:34:08,159 --> 01:34:15,760 change the background color to Green if 1837 01:34:12,199 --> 01:34:18,040 we save live server will automatically 1838 01:34:15,760 --> 01:34:20,080 refresh the web page so we don't have to 1839 01:34:18,040 --> 01:34:23,000 do that back and forth 1840 01:34:20,080 --> 01:34:25,280 anymore let's just change it back to Red 1841 01:34:23,000 --> 01:34:26,440 so that the class last name makes sense 1842 01:34:25,280 --> 01:34:31,280 and 1843 01:34:26,440 --> 01:34:31,280 save and we can also close the previous 1844 01:34:31,320 --> 01:34:38,719 tab so this is the HTML structure it 1845 01:34:35,520 --> 01:34:41,360 gives us access to more features of 1846 01:34:38,719 --> 01:34:45,000 HTML and this is all we need to review 1847 01:34:41,360 --> 01:34:47,760 about HTML and CSS for now if you want 1848 01:34:45,000 --> 01:34:52,080 to learn more you can check out my HTML 1849 01:34:47,760 --> 01:34:54,800 and CSS full course in the video 1850 01:34:52,080 --> 01:34:57,080 description by the way to find the links 1851 01:34:54,800 --> 01:34:59,840 mentioned in each lesson you can click 1852 01:34:57,080 --> 01:34:59,840 here in the 1853 01:35:02,719 --> 01:35:08,560 description and this page contains all 1854 01:35:05,119 --> 01:35:08,560 the links mentioned in each 1855 01:35:10,159 --> 01:35:15,320 lesson before we add JavaScript to our 1856 01:35:12,760 --> 01:35:16,440 website we're going to review some VSS 1857 01:35:15,320 --> 01:35:18,719 code 1858 01:35:16,440 --> 01:35:22,159 setup so you'll notice that many of 1859 01:35:18,719 --> 01:35:24,760 these lines have spaces at the front 1860 01:35:22,159 --> 01:35:27,679 these spaces are called in indents and 1861 01:35:24,760 --> 01:35:30,560 indents make our code easier to read for 1862 01:35:27,679 --> 01:35:33,600 example we can easily tell that this 1863 01:35:30,560 --> 01:35:36,119 code is inside the brackets because it 1864 01:35:33,600 --> 01:35:39,040 has an extra indent in the 1865 01:35:36,119 --> 01:35:41,280 front if we didn't have indents here's 1866 01:35:39,040 --> 01:35:42,840 what the code would look like it's a lot 1867 01:35:41,280 --> 01:35:46,199 harder to 1868 01:35:42,840 --> 01:35:49,159 read to add an indent we can type at the 1869 01:35:46,199 --> 01:35:52,360 beginning of the line and press 1870 01:35:49,159 --> 01:35:55,679 tab to remove an indent we can press 1871 01:35:52,360 --> 01:35:59,600 shift tab or just use 1872 01:35:55,679 --> 01:36:03,719 backspace now by default VSS code uses 1873 01:35:59,600 --> 01:36:06,920 four spaces per indet however in HTML 1874 01:36:03,719 --> 01:36:09,119 CSS and JavaScript we usually use two 1875 01:36:06,920 --> 01:36:11,960 spaces per indet so we're going to 1876 01:36:09,119 --> 01:36:14,400 update this setting let's go to our 1877 01:36:11,960 --> 01:36:15,440 settings by clicking this icon in the 1878 01:36:14,400 --> 01:36:18,800 bottom 1879 01:36:15,440 --> 01:36:21,280 left and then click 1880 01:36:18,800 --> 01:36:24,760 settings and at the top we're going to 1881 01:36:21,280 --> 01:36:27,440 search for tab 1882 01:36:24,760 --> 01:36:30,159 and we're going to change the tab size 1883 01:36:27,440 --> 01:36:32,960 from 4 to 1884 01:36:30,159 --> 01:36:34,960 2 now let's close the 1885 01:36:32,960 --> 01:36:37,560 settings and we're also going to change 1886 01:36:34,960 --> 01:36:41,080 the indents for this file we're going to 1887 01:36:37,560 --> 01:36:43,679 go down here and click select 1888 01:36:41,080 --> 01:36:44,920 indent and we're going to choose indent 1889 01:36:43,679 --> 01:36:48,080 using 1890 01:36:44,920 --> 01:36:50,960 spaces and then click 1891 01:36:48,080 --> 01:36:54,800 two and now if we type at the start of a 1892 01:36:50,960 --> 01:36:56,239 line and press tab it will only add two 1893 01:36:54,800 --> 01:36:59,679 spaces per 1894 01:36:56,239 --> 01:37:03,000 indent so let's reformat our code to use 1895 01:36:59,679 --> 01:37:04,800 two spaces per indent instead of four 1896 01:37:03,000 --> 01:37:07,880 first we're going to remove all the 1897 01:37:04,800 --> 01:37:10,520 indents in our code a shortcut for this 1898 01:37:07,880 --> 01:37:14,560 is to select all of these 1899 01:37:10,520 --> 01:37:17,040 lines and press shift tab to remove one 1900 01:37:14,560 --> 01:37:20,520 indent for each line so we're going to 1901 01:37:17,040 --> 01:37:22,239 keep pressing shift tab until we remove 1902 01:37:20,520 --> 01:37:24,840 all the 1903 01:37:22,239 --> 01:37:28,560 indents next next we're going to add the 1904 01:37:24,840 --> 01:37:30,920 indents back using a similar shortcut 1905 01:37:28,560 --> 01:37:34,239 we're going to select the head 1906 01:37:30,920 --> 01:37:37,360 element and press tab to add one indent 1907 01:37:34,239 --> 01:37:40,599 for each line and then select everything 1908 01:37:37,360 --> 01:37:43,119 inside the head and press tab to add 1909 01:37:40,599 --> 01:37:47,040 another indent and then select 1910 01:37:43,119 --> 01:37:49,400 everything inside style and press Tab 1911 01:37:47,040 --> 01:37:51,679 and then inside each of these brackets 1912 01:37:49,400 --> 01:37:55,920 we're going to select the lines press 1913 01:37:51,679 --> 01:37:58,679 Tab and then here press tab we'll do the 1914 01:37:55,920 --> 01:38:02,320 same thing for the body element we'll 1915 01:37:58,679 --> 01:38:05,320 select all these lines and press tab 1916 01:38:02,320 --> 01:38:08,040 we'll select everything inside the body 1917 01:38:05,320 --> 01:38:11,360 press Tab and then inside each of these 1918 01:38:08,040 --> 01:38:12,599 elements we're going to press Tab and 1919 01:38:11,360 --> 01:38:16,520 press 1920 01:38:12,599 --> 01:38:19,520 tab now if we save our website will look 1921 01:38:16,520 --> 01:38:20,599 the same because all we did was adjust 1922 01:38:19,520 --> 01:38:23,000 the 1923 01:38:20,599 --> 01:38:25,599 indents so there's one other setting in 1924 01:38:23,000 --> 01:38:28,679 vs code that we're going to set up which 1925 01:38:25,599 --> 01:38:30,639 is line wrapping if you look at this 1926 01:38:28,679 --> 01:38:33,239 line of code you can see that it's 1927 01:38:30,639 --> 01:38:36,480 really long and we need to scroll 1928 01:38:33,239 --> 01:38:38,440 horizontally to see the rest of the line 1929 01:38:36,480 --> 01:38:41,719 instead we're going to make this line 1930 01:38:38,440 --> 01:38:44,800 wrap around if it gets too long let's go 1931 01:38:41,719 --> 01:38:46,800 back into our settings by clicking here 1932 01:38:44,800 --> 01:38:49,280 and then click the 1933 01:38:46,800 --> 01:38:51,599 settings and then up here we're going to 1934 01:38:49,280 --> 01:38:54,159 search for 1935 01:38:51,599 --> 01:38:58,239 wrap and we're going to look for editor 1936 01:38:54,159 --> 01:39:01,040 word wrap and we're going to turn this 1937 01:38:58,239 --> 01:39:02,239 on now if we close our settings and go 1938 01:39:01,040 --> 01:39:05,360 back to our 1939 01:39:02,239 --> 01:39:08,119 code this line which is really long will 1940 01:39:05,360 --> 01:39:10,560 wrap around to the next line instead of 1941 01:39:08,119 --> 01:39:13,480 us having to horizontally 1942 01:39:10,560 --> 01:39:18,080 scroll so that's the vs code setup that 1943 01:39:13,480 --> 01:39:20,639 we need two spaces per indent and line 1944 01:39:18,080 --> 01:39:23,719 wrapping finally we're going to add 1945 01:39:20,639 --> 01:39:26,119 JavaScript to this website so we learned 1946 01:39:23,719 --> 01:39:28,760 that we can use the console to write and 1947 01:39:26,119 --> 01:39:31,400 run JavaScript well there's actually 1948 01:39:28,760 --> 01:39:34,760 another way to run JavaScript which is 1949 01:39:31,400 --> 01:39:38,080 inside an HTML file let's learn how to 1950 01:39:34,760 --> 01:39:40,960 do that so we learned that HTML has a 1951 01:39:38,080 --> 01:39:44,719 special element that runs CSS code 1952 01:39:40,960 --> 01:39:46,960 called the style element while HTML has 1953 01:39:44,719 --> 01:39:49,719 another special element that runs 1954 01:39:46,960 --> 01:39:51,639 JavaScript code and this element is 1955 01:39:49,719 --> 01:39:54,040 called the script 1956 01:39:51,639 --> 01:39:57,440 element so inside 1957 01:39:54,040 --> 01:40:01,679 body at the bottom of the body we're 1958 01:39:57,440 --> 01:40:05,239 going to add less than script greater 1959 01:40:01,679 --> 01:40:08,159 than and the closing tag less than SL 1960 01:40:05,239 --> 01:40:09,920 script greater than and inside the 1961 01:40:08,159 --> 01:40:12,840 script element we can put some 1962 01:40:09,920 --> 01:40:18,960 JavaScript code we're going to add the 1963 01:40:12,840 --> 01:40:22,960 code alert Open Bracket quote hello 1964 01:40:18,960 --> 01:40:24,400 quote close bracket and semicolon if we 1965 01:40:22,960 --> 01:40:26,920 save 1966 01:40:24,400 --> 01:40:28,800 live server will refresh the page and 1967 01:40:26,920 --> 01:40:31,440 now you notice that the computer now 1968 01:40:28,800 --> 01:40:34,080 runs our JavaScript code just like it 1969 01:40:31,440 --> 01:40:37,320 did in the console it creates a popup 1970 01:40:34,080 --> 01:40:37,320 with the text hello 1971 01:40:37,560 --> 01:40:42,360 inside and we usually put the script 1972 01:40:39,880 --> 01:40:43,920 element at the bottom of the body 1973 01:40:42,360 --> 01:40:46,840 because we want the web page to be 1974 01:40:43,920 --> 01:40:48,679 created first and then we use JavaScript 1975 01:40:46,840 --> 01:40:51,119 to make it 1976 01:40:48,679 --> 01:40:53,000 interactive for the rest of this course 1977 01:40:51,119 --> 01:40:56,480 we're going to run our JavaScript code 1978 01:40:53,000 --> 01:40:58,920 using using an HTML file the console 1979 01:40:56,480 --> 01:41:02,239 that we used in the previous lessons is 1980 01:40:58,920 --> 01:41:05,520 more of a way to try out JavaScript code 1981 01:41:02,239 --> 01:41:07,719 we wouldn't use a console for a real 1982 01:41:05,520 --> 01:41:10,480 website in addition to the script 1983 01:41:07,719 --> 01:41:13,960 element we have another way to run 1984 01:41:10,480 --> 01:41:17,400 JavaScript code inside HTML and that's 1985 01:41:13,960 --> 01:41:20,360 by using an attribute let's go back to 1986 01:41:17,400 --> 01:41:23,960 our first button and here we're going to 1987 01:41:20,360 --> 01:41:26,719 add another attribute so let's type 1988 01:41:23,960 --> 01:41:28,480 space and this time we're going to add 1989 01:41:26,719 --> 01:41:32,719 the 1990 01:41:28,480 --> 01:41:36,040 onclick attribute equals double 1991 01:41:32,719 --> 01:41:38,920 quotes So this attribute will run 1992 01:41:36,040 --> 01:41:42,760 JavaScript code whenever we click this 1993 01:41:38,920 --> 01:41:45,520 button that's what onclick means between 1994 01:41:42,760 --> 01:41:49,080 the double quotes we can write some 1995 01:41:45,520 --> 01:41:52,480 JavaScript for example let's press enter 1996 01:41:49,080 --> 01:41:53,960 enter and then here we're going to type 1997 01:41:52,480 --> 01:41:55,800 alert 1998 01:41:53,960 --> 01:42:02,119 Open Bracket 1999 01:41:55,800 --> 01:42:04,639 quote good job quote close bracket and 2000 01:42:02,119 --> 01:42:07,840 semicolon now if we 2001 01:42:04,639 --> 01:42:10,840 save live server will refresh the page 2002 01:42:07,840 --> 01:42:14,080 and run this line of JavaScript code 2003 01:42:10,840 --> 01:42:15,000 let's press okay and now if we click 2004 01:42:14,080 --> 01:42:17,920 this 2005 01:42:15,000 --> 01:42:20,400 button it will run this line of 2006 01:42:17,920 --> 01:42:25,119 JavaScript code and create a popup with 2007 01:42:20,400 --> 01:42:25,119 a text good job inside 2008 01:42:25,560 --> 01:42:31,560 so those are two places where we can run 2009 01:42:27,960 --> 01:42:34,960 JavaScript code the script element and 2010 01:42:31,560 --> 01:42:37,960 the onclick attribute the code in the 2011 01:42:34,960 --> 01:42:41,119 script element runs first when the page 2012 01:42:37,960 --> 01:42:44,119 is loaded the code in the onclick 2013 01:42:41,119 --> 01:42:46,520 attribute runs after when we click the 2014 01:42:44,119 --> 01:42:51,119 button on the 2015 01:42:46,520 --> 01:42:54,000 page and that's how we use HTML CSS and 2016 01:42:51,119 --> 01:42:56,800 JavaScript together on a website 2017 01:42:54,000 --> 01:43:01,520 notice that everything starts from 2018 01:42:56,800 --> 01:43:05,960 HTML HTML first creates the website and 2019 01:43:01,520 --> 01:43:09,159 then inside the HTML we can run CSS code 2020 01:43:05,960 --> 01:43:13,480 to change the appearance and we can run 2021 01:43:09,159 --> 01:43:15,679 JavaScript code to make the website 2022 01:43:13,480 --> 01:43:17,480 interactive now let's learn some new 2023 01:43:15,679 --> 01:43:20,560 features of JavaScript that we haven't 2024 01:43:17,480 --> 01:43:23,400 seen yet in this course the next feature 2025 01:43:20,560 --> 01:43:25,080 we're going to learn is called comments 2026 01:43:23,400 --> 01:43:27,920 comments are pieces of code that the 2027 01:43:25,080 --> 01:43:30,400 computer ignores so let's create our 2028 01:43:27,920 --> 01:43:32,080 first comment we're going to go down to 2029 01:43:30,400 --> 01:43:36,480 the script element 2030 01:43:32,080 --> 01:43:40,440 here and type forward slash forward 2031 01:43:36,480 --> 01:43:42,920 slash so this is a comment anything 2032 01:43:40,440 --> 01:43:45,400 after the double slash will be ignored 2033 01:43:42,920 --> 01:43:50,679 by the computer so we can type anything 2034 01:43:45,400 --> 01:43:55,040 we want like this is a 2035 01:43:50,679 --> 01:43:57,840 comment if we save it ignores this line 2036 01:43:55,040 --> 01:44:00,800 and creates the popup in the next 2037 01:43:57,840 --> 01:44:02,920 line now let's close this popup and 2038 01:44:00,800 --> 01:44:05,239 learn why we use 2039 01:44:02,920 --> 01:44:07,679 comments comments are useful for 2040 01:44:05,239 --> 01:44:11,400 providing more information for people 2041 01:44:07,679 --> 01:44:16,000 reading our code for example we can 2042 01:44:11,400 --> 01:44:20,400 create a comment above this code like 2043 01:44:16,000 --> 01:44:23,760 this code creates a 2044 01:44:20,400 --> 01:44:27,320 popup this comment helps others and 2045 01:44:23,760 --> 01:44:29,679 ourselves understand what this code does 2046 01:44:27,320 --> 01:44:32,960 so that's one way we use 2047 01:44:29,679 --> 01:44:34,840 comments another way we use comments is 2048 01:44:32,960 --> 01:44:37,560 if we don't want to run some code 2049 01:44:34,840 --> 01:44:42,080 anymore but we also don't want to delete 2050 01:44:37,560 --> 01:44:45,280 it for example this code creates a popup 2051 01:44:42,080 --> 01:44:46,320 every time we refresh the page and this 2052 01:44:45,280 --> 01:44:49,080 can be 2053 01:44:46,320 --> 01:44:52,159 annoying so I want to stop running this 2054 01:44:49,080 --> 01:44:53,840 code but I also don't want to delete it 2055 01:44:52,159 --> 01:44:55,080 because you might want to review this 2056 01:44:53,840 --> 01:44:58,960 code 2057 01:44:55,080 --> 01:45:01,639 later so what we can do here is to add a 2058 01:44:58,960 --> 01:45:04,199 double slash in front of this line to 2059 01:45:01,639 --> 01:45:08,080 turn this code into a 2060 01:45:04,199 --> 01:45:10,920 comment now if we save the computer will 2061 01:45:08,080 --> 01:45:12,000 ignore this line of code and not create 2062 01:45:10,920 --> 01:45:15,360 the 2063 01:45:12,000 --> 01:45:18,199 popup this is called commenting out the 2064 01:45:15,360 --> 01:45:20,679 code it's useful if we don't want to run 2065 01:45:18,199 --> 01:45:24,800 some code anymore but we also don't want 2066 01:45:20,679 --> 01:45:27,119 to delete it for informational purposes 2067 01:45:24,800 --> 01:45:31,040 so JavaScript has another Syntax for 2068 01:45:27,119 --> 01:45:34,440 writing comments and that syntax is 2069 01:45:31,040 --> 01:45:38,280 slash star and star 2070 01:45:34,440 --> 01:45:42,119 slash here anything between the stars is 2071 01:45:38,280 --> 01:45:44,800 a comment this is called a multi-line 2072 01:45:42,119 --> 01:45:48,080 comment because between the Stars we can 2073 01:45:44,800 --> 01:45:53,040 write a comment on multiple lines for 2074 01:45:48,080 --> 01:45:56,800 example we can type multi enter line 2075 01:45:53,040 --> 01:45:59,880 Line enter 2076 01:45:56,800 --> 01:46:02,719 comment on the other hand double slash 2077 01:45:59,880 --> 01:46:06,520 is known as a single line 2078 01:46:02,719 --> 01:46:10,239 comment now just like JavaScript HTML 2079 01:46:06,520 --> 01:46:11,719 and CSS also have comments but they use 2080 01:46:10,239 --> 01:46:15,280 a different 2081 01:46:11,719 --> 01:46:19,599 syntax if we scroll up to our 2082 01:46:15,280 --> 01:46:21,119 HTML here we can type less than 2083 01:46:19,599 --> 01:46:25,520 exclamation 2084 01:46:21,119 --> 01:46:28,360 d-h and we also need dh- greater than so 2085 01:46:25,520 --> 01:46:34,280 anything between these double dashes is 2086 01:46:28,360 --> 01:46:38,760 a comment for example this is a 2087 01:46:34,280 --> 01:46:44,080 comment in CSS to create a comment we 2088 01:46:38,760 --> 01:46:47,119 can type slash star star slash so it's 2089 01:46:44,080 --> 01:46:50,239 the same multi-line comment as 2090 01:46:47,119 --> 01:46:53,400 JavaScript between the Stars we can add 2091 01:46:50,239 --> 01:46:55,599 a comment so this 2092 01:46:53,400 --> 01:46:57,960 is a 2093 01:46:55,599 --> 01:47:01,000 comment if we save our 2094 01:46:57,960 --> 01:47:03,800 file nothing changes on our web page 2095 01:47:01,000 --> 01:47:06,159 because comments are ignored by the 2096 01:47:03,800 --> 01:47:08,800 computer the last feature of JavaScript 2097 01:47:06,159 --> 01:47:11,320 we're going to learn is called 2098 01:47:08,800 --> 01:47:15,360 console.log let's go back to our 2099 01:47:11,320 --> 01:47:20,320 code and at the bottom in the script 2100 01:47:15,360 --> 01:47:23,040 element we're going to type the code 2 + 2101 01:47:20,320 --> 01:47:26,960 2 so we learned that this code in Java 2102 01:47:23,040 --> 01:47:28,480 script does some math however if we save 2103 01:47:26,960 --> 01:47:32,239 this 2104 01:47:28,480 --> 01:47:34,440 file where is the result of this math so 2105 01:47:32,239 --> 01:47:37,520 we might think that the result will show 2106 01:47:34,440 --> 01:47:40,119 up in the console but if we 2107 01:47:37,520 --> 01:47:42,840 rightclick click 2108 01:47:40,119 --> 01:47:45,280 inspect and then click the 2109 01:47:42,840 --> 01:47:48,400 console you'll notice that the result 2110 01:47:45,280 --> 01:47:52,960 doesn't show up here either and that's 2111 01:47:48,400 --> 01:47:56,000 because before we were running 2 + 2 2112 01:47:52,960 --> 01:47:59,199 inside the console so the result shows 2113 01:47:56,000 --> 01:48:01,920 up in the console but now we're not 2114 01:47:59,199 --> 01:48:04,920 running this code in the console anymore 2115 01:48:01,920 --> 01:48:07,000 we're running this code on the web page 2116 01:48:04,920 --> 01:48:09,760 so how do we see this 2117 01:48:07,000 --> 01:48:13,639 result the solution is we're going to 2118 01:48:09,760 --> 01:48:16,119 redirect this result back to the console 2119 01:48:13,639 --> 01:48:17,199 to do that we're going to use this piece 2120 01:48:16,119 --> 01:48:20,199 of 2121 01:48:17,199 --> 01:48:20,199 code 2122 01:48:20,639 --> 01:48:25,320 console.log Open Bracket close bracket 2123 01:48:23,840 --> 01:48:28,360 and 2124 01:48:25,320 --> 01:48:31,159 semicolon console.log will display 2125 01:48:28,360 --> 01:48:34,480 whatever's between the brackets back in 2126 01:48:31,159 --> 01:48:38,080 the console so if we move this code 2127 01:48:34,480 --> 01:48:41,560 between the brackets so we select this 2128 01:48:38,080 --> 01:48:45,960 and then right click cut and then 2129 01:48:41,560 --> 01:48:47,199 between the brackets right click paste 2130 01:48:45,960 --> 01:48:49,960 and 2131 01:48:47,199 --> 01:48:53,119 save the result of this code is 2132 01:48:49,960 --> 01:48:56,080 displayed back in the console 2133 01:48:53,119 --> 01:49:00,639 and console.log works for any code that 2134 01:48:56,080 --> 01:49:04,159 we learned so far for example we can use 2135 01:49:00,639 --> 01:49:07,159 strings so let's create a new line and 2136 01:49:04,159 --> 01:49:07,159 type 2137 01:49:07,639 --> 01:49:13,040 console.log Open Bracket close bracket 2138 01:49:10,560 --> 01:49:14,400 semicolon and then between the brackets 2139 01:49:13,040 --> 01:49:22,119 let's type some 2140 01:49:14,400 --> 01:49:23,639 strings quote Su quote plus quote text 2141 01:49:22,119 --> 01:49:25,360 quote 2142 01:49:23,639 --> 01:49:28,960 and save this 2143 01:49:25,360 --> 01:49:31,520 file again it will display the result of 2144 01:49:28,960 --> 01:49:32,480 whatever's between the brackets back in 2145 01:49:31,520 --> 01:49:35,159 the 2146 01:49:32,480 --> 01:49:37,719 console so that's how we see the result 2147 01:49:35,159 --> 01:49:40,760 of JavaScript code that is running in a 2148 01:49:37,719 --> 01:49:43,159 file we use 2149 01:49:40,760 --> 01:49:46,239 console.log and that's the end of this 2150 01:49:43,159 --> 01:49:50,920 lesson in this lesson we reviewed the 2151 01:49:46,239 --> 01:49:54,719 basics of HTML and CSS we set up our 2152 01:49:50,920 --> 01:49:58,599 code editor vs code we learned how to 2153 01:49:54,719 --> 01:50:01,880 load JavaScript inside an HTML file 2154 01:49:58,599 --> 01:50:04,520 using the script element and the onclick 2155 01:50:01,880 --> 01:50:09,280 attribute we learned how to create 2156 01:50:04,520 --> 01:50:09,280 comments and we learned how to use 2157 01:50:10,639 --> 01:50:14,920 console.log thanks for watching this 2158 01:50:12,520 --> 01:50:16,679 course so far if you want to support 2159 01:50:14,920 --> 01:50:18,920 this channel you can check out the 2160 01:50:16,679 --> 01:50:21,080 premium version of my courses just like 2161 01:50:18,920 --> 01:50:24,599 this one where you can get a certificate 2162 01:50:21,080 --> 01:50:26,520 of completion and a learning platform if 2163 01:50:24,599 --> 01:50:29,760 you're interested in extra videos from 2164 01:50:26,520 --> 01:50:32,040 me more in-depth videos and behind thes 2165 01:50:29,760 --> 01:50:34,920 scenes content you can check out these 2166 01:50:32,040 --> 01:50:37,800 links here you can also support the 2167 01:50:34,920 --> 01:50:40,320 channel by liking the video subscribing 2168 01:50:37,800 --> 01:50:44,280 and sharing this video with others 2169 01:50:40,320 --> 01:50:44,280 thanks again and let's continue with the 2170 01:50:44,320 --> 01:50:49,119 course here are some exercises you can 2171 01:50:46,880 --> 01:50:53,320 try on your own to practice what we 2172 01:50:49,119 --> 01:50:53,320 learned in this lesson 2173 01:51:10,679 --> 01:51:14,920 in this lesson we're going to learn the 2174 01:51:12,760 --> 01:51:17,000 next feature of JavaScript called 2175 01:51:14,920 --> 01:51:19,520 variables and we're going to use 2176 01:51:17,000 --> 01:51:21,639 variables to build a simple version of 2177 01:51:19,520 --> 01:51:23,280 the cart quantity feature of the final 2178 01:51:21,639 --> 01:51:27,320 project 2179 01:51:23,280 --> 01:51:30,119 first let's create a new HTML file just 2180 01:51:27,320 --> 01:51:33,440 for learning variables we're going to go 2181 01:51:30,119 --> 01:51:36,360 to our code editor and click this icon 2182 01:51:33,440 --> 01:51:39,280 to show our files and then click this 2183 01:51:36,360 --> 01:51:44,119 icon to create a new 2184 01:51:39,280 --> 01:51:46,560 file and we're going to name this file 2185 01:51:44,119 --> 01:51:48,239 05- 2186 01:51:46,560 --> 01:51:50,400 variables. 2187 01:51:48,239 --> 01:51:53,639 HTML and press 2188 01:51:50,400 --> 01:51:55,560 enter so the 05 just means that this 2189 01:51:53,639 --> 01:51:58,800 file is for lesson 2190 01:51:55,560 --> 01:52:00,440 five let's also add the lesson number to 2191 01:51:58,800 --> 01:52:04,440 website. 2192 01:52:00,440 --> 01:52:06,719 HTML we'll select this file and then 2193 01:52:04,440 --> 01:52:10,880 rightclick 2194 01:52:06,719 --> 01:52:13,560 rename and then at the front We'll add 2195 01:52:10,880 --> 01:52:16,560 04- and press 2196 01:52:13,560 --> 01:52:21,199 enter next we're going to copy all the 2197 01:52:16,560 --> 01:52:24,400 code in website. HTML to variables. 2198 01:52:21,199 --> 01:52:26,719 HTML so let's click in this file and 2199 01:52:24,400 --> 01:52:30,079 we're going to select all of this code 2200 01:52:26,719 --> 01:52:32,400 by typing control a on Windows or 2201 01:52:30,079 --> 01:52:35,079 command a on 2202 01:52:32,400 --> 01:52:36,840 Mac and then we're going to 2203 01:52:35,079 --> 01:52:40,599 rightclick 2204 01:52:36,840 --> 01:52:44,840 copy and then inside variables. 2205 01:52:40,599 --> 01:52:47,920 HTML rightclick and 2206 01:52:44,840 --> 01:52:50,679 paste let's click this icon to hide our 2207 01:52:47,920 --> 01:52:53,400 files for now and we'll prepare this 2208 01:52:50,679 --> 01:52:56,159 file for this lesson so let's scroll up 2209 01:52:53,400 --> 01:53:00,440 to the top and first we're going to 2210 01:52:56,159 --> 01:53:03,000 change the title to 2211 01:53:00,440 --> 01:53:05,639 variables next we're going to delete all 2212 01:53:03,000 --> 01:53:08,719 the CSS because we're going to start 2213 01:53:05,639 --> 01:53:13,000 over and we're going to delete all the 2214 01:53:08,719 --> 01:53:13,000 HTML except for the script 2215 01:53:13,079 --> 01:53:18,400 element and let's delete all the 2216 01:53:15,239 --> 01:53:22,760 JavaScript from the previous 2217 01:53:18,400 --> 01:53:25,159 lesson and finally let's save 2218 01:53:22,760 --> 01:53:28,280 and we'll open this file in our browser 2219 01:53:25,159 --> 01:53:30,000 by right clicking and then open with 2220 01:53:28,280 --> 01:53:32,920 live 2221 01:53:30,000 --> 01:53:36,880 server and now we can close the previous 2222 01:53:32,920 --> 01:53:39,920 tab and the previous code for 2223 01:53:36,880 --> 01:53:43,040 now now we're ready to learn 2224 01:53:39,920 --> 01:53:46,840 variables first of all what is a 2225 01:53:43,040 --> 01:53:49,599 variable a variable is like a container 2226 01:53:46,840 --> 01:53:52,639 we can save a value like a number or a 2227 01:53:49,599 --> 01:53:56,079 string inside a variable and then then 2228 01:53:52,639 --> 01:53:57,880 use it later Let's do an example and 2229 01:53:56,079 --> 01:54:00,960 create our first 2230 01:53:57,880 --> 01:54:05,599 variable inside the script element we're 2231 01:54:00,960 --> 01:54:09,280 going to type the code let and space the 2232 01:54:05,599 --> 01:54:11,840 word let creates a new variable and now 2233 01:54:09,280 --> 01:54:16,000 we're going to choose a name for our new 2234 01:54:11,840 --> 01:54:19,239 variable let's name this variable 2235 01:54:16,000 --> 01:54:22,760 one so this creates a variable or a 2236 01:54:19,239 --> 01:54:27,280 container named variable one and now we 2237 01:54:22,760 --> 01:54:31,440 can save a value inside this variable to 2238 01:54:27,280 --> 01:54:34,560 do that we're going to type space equals 2239 01:54:31,440 --> 01:54:38,560 space and then a value like a number or 2240 01:54:34,560 --> 01:54:41,599 a string so let's save the number three 2241 01:54:38,560 --> 01:54:45,639 inside and we'll type a semicolon at the 2242 01:54:41,599 --> 01:54:49,079 end so here we use the let to create a 2243 01:54:45,639 --> 01:54:51,880 new variable we named this variable 2244 01:54:49,079 --> 01:54:54,280 variable one and we saved the value 2245 01:54:51,880 --> 01:54:57,239 three inside this 2246 01:54:54,280 --> 01:55:01,239 variable now that we saved this value we 2247 01:54:57,239 --> 01:55:05,199 can use it later for example let's type 2248 01:55:01,239 --> 01:55:09,280 a new line and type the code 2249 01:55:05,199 --> 01:55:10,560 console. log Open Bracket close bracket 2250 01:55:09,280 --> 01:55:12,679 and 2251 01:55:10,560 --> 01:55:14,920 semicolon remember from the previous 2252 01:55:12,679 --> 01:55:17,599 lesson that this code displays 2253 01:55:14,920 --> 01:55:21,480 whatever's inside the brackets in the 2254 01:55:17,599 --> 01:55:22,920 console so far we put a value inside 2255 01:55:21,480 --> 01:55:26,560 like the number 2256 01:55:22,920 --> 01:55:29,679 two if we save and then go to our 2257 01:55:26,560 --> 01:55:31,679 website and open the console so right 2258 01:55:29,679 --> 01:55:34,440 click 2259 01:55:31,679 --> 01:55:37,360 inspect and then click the 2260 01:55:34,440 --> 01:55:39,079 console we can see that the number two 2261 01:55:37,360 --> 01:55:41,719 was displayed in the 2262 01:55:39,079 --> 01:55:44,599 console so now let's try replacing this 2263 01:55:41,719 --> 01:55:48,560 value with the variable that we created 2264 01:55:44,599 --> 01:55:52,719 above so instead of two we're going to 2265 01:55:48,560 --> 01:55:53,960 console.log variable one 2266 01:55:52,719 --> 01:55:58,000 if we 2267 01:55:53,960 --> 01:56:00,280 save notice that it now displays three 2268 01:55:58,000 --> 01:56:04,040 so it's displaying whatever value is 2269 01:56:00,280 --> 01:56:07,320 saved inside the variable as you can see 2270 01:56:04,040 --> 01:56:10,119 we can save a value inside a variable 2271 01:56:07,320 --> 01:56:13,599 and then use that variable later 2272 01:56:10,119 --> 01:56:16,079 on let's do another example we're going 2273 01:56:13,599 --> 01:56:18,760 to create another variable so we're 2274 01:56:16,079 --> 01:56:20,079 going to create some new lines and we'll 2275 01:56:18,760 --> 01:56:23,199 type 2276 01:56:20,079 --> 01:56:25,800 let and this time let's name this 2277 01:56:23,199 --> 01:56:27,840 variable 2278 01:56:25,800 --> 01:56:31,639 calculation and we're going to type 2279 01:56:27,840 --> 01:56:34,719 space equals space to save a value 2280 01:56:31,639 --> 01:56:36,719 inside this new variable and this time 2281 01:56:34,719 --> 01:56:37,960 instead of just a number we're going to 2282 01:56:36,719 --> 01:56:43,920 save a 2283 01:56:37,960 --> 01:56:46,920 calculation 2 + 2 and semicolon so this 2284 01:56:43,920 --> 01:56:50,480 will calculate 2 + 2 and then save the 2285 01:56:46,920 --> 01:56:53,520 result which is four into the 2286 01:56:50,480 --> 01:56:56,760 variable if we call 2287 01:56:53,520 --> 01:56:59,400 console. log Open 2288 01:56:56,760 --> 01:57:03,320 Bracket 2289 01:56:59,400 --> 01:57:06,639 calculation and semicolon and 2290 01:57:03,320 --> 01:57:08,920 save it will show us that the value for 2291 01:57:06,639 --> 01:57:11,840 is saved inside 2292 01:57:08,920 --> 01:57:16,040 calculation now let's try this code 2293 01:57:11,840 --> 01:57:19,760 we're going to type a new line and then 2294 01:57:16,040 --> 01:57:22,840 console. log Open Bracket and 2295 01:57:19,760 --> 01:57:26,520 calculation again 2296 01:57:22,840 --> 01:57:29,760 and plus 2 and 2297 01:57:26,520 --> 01:57:31,159 semicolon so here we're using a variable 2298 01:57:29,760 --> 01:57:33,599 inside a 2299 01:57:31,159 --> 01:57:36,000 calculation what this does is that it 2300 01:57:33,599 --> 01:57:39,320 takes whatever value is saved inside the 2301 01:57:36,000 --> 01:57:41,280 variable right now it is four and then 2302 01:57:39,320 --> 01:57:45,760 substitutes that value into the 2303 01:57:41,280 --> 01:57:46,960 calculation so this will be 4 + 2 which 2304 01:57:45,760 --> 01:57:48,960 equals 2305 01:57:46,960 --> 01:57:51,800 six if we 2306 01:57:48,960 --> 01:57:53,880 save we can see that in the console it 2307 01:57:51,800 --> 01:57:57,119 display blade 2308 01:57:53,880 --> 01:57:59,920 six so we can use a variable wherever we 2309 01:57:57,119 --> 01:58:02,159 use a value and JavaScript will 2310 01:57:59,920 --> 01:58:04,119 substitute whatever is inside the 2311 01:58:02,159 --> 01:58:07,599 variable into the 2312 01:58:04,119 --> 01:58:09,960 code let's do another example we're 2313 01:58:07,599 --> 01:58:13,119 going to type some new lines and create 2314 01:58:09,960 --> 01:58:15,560 a new variable again using 2315 01:58:13,119 --> 01:58:17,679 let and we're going to name this 2316 01:58:15,560 --> 01:58:20,719 variable 2317 01:58:17,679 --> 01:58:23,800 result and we're going to make it equal 2318 01:58:20,719 --> 01:58:28,040 to calcul 2319 01:58:23,800 --> 01:58:31,040 ation plus 2 and 2320 01:58:28,040 --> 01:58:33,840 semicolon so this does the same thing as 2321 01:58:31,040 --> 01:58:36,159 a code before it will take whatever 2322 01:58:33,840 --> 01:58:39,560 value is inside calculation which is 2323 01:58:36,159 --> 01:58:42,800 four and substitute it into this code so 2324 01:58:39,560 --> 01:58:46,280 we get 4 + 2 = 2325 01:58:42,800 --> 01:58:48,239 6 but this time we have an extra step 2326 01:58:46,280 --> 01:58:50,800 we're going to save this result which is 2327 01:58:48,239 --> 01:58:54,960 six into another 2328 01:58:50,800 --> 01:58:54,960 variable so so if we 2329 01:58:55,360 --> 01:59:00,239 console.log 2330 01:58:58,280 --> 01:59:04,560 result and 2331 01:59:00,239 --> 01:59:06,480 save we can see that six was saved into 2332 01:59:04,560 --> 01:59:09,679 the result 2333 01:59:06,480 --> 01:59:13,119 variable again we can use a variable 2334 01:59:09,679 --> 01:59:15,800 wherever we use a value now in addition 2335 01:59:13,119 --> 01:59:18,320 to numbers we can save any type of value 2336 01:59:15,800 --> 01:59:21,480 in a variable including 2337 01:59:18,320 --> 01:59:23,560 strings for example let's create a new 2338 01:59:21,480 --> 01:59:27,000 variable using 2339 01:59:23,560 --> 01:59:29,880 let and let's name this 2340 01:59:27,000 --> 01:59:33,280 message and we're going to save inside 2341 01:59:29,880 --> 01:59:37,520 this variable a string this time so 2342 01:59:33,280 --> 01:59:39,520 quote hello quote and 2343 01:59:37,520 --> 01:59:44,239 semicolon again we're going to 2344 01:59:39,520 --> 01:59:44,239 console.log this variable so 2345 01:59:44,440 --> 01:59:50,520 console.log and 2346 01:59:47,040 --> 01:59:52,280 message and a semicolon at the end if we 2347 01:59:50,520 --> 01:59:54,960 save 2348 01:59:52,280 --> 01:59:57,360 it will show us that the string hello is 2349 01:59:54,960 --> 02:00:00,119 saved inside this 2350 01:59:57,360 --> 02:00:02,560 variable okay now that we learned how 2351 02:00:00,119 --> 02:00:04,079 variables work let's learn the syntax 2352 02:00:02,560 --> 02:00:07,639 rules for 2353 02:00:04,079 --> 02:00:10,280 variables the word let creates a new 2354 02:00:07,639 --> 02:00:13,199 variable and then we give the variable a 2355 02:00:10,280 --> 02:00:15,880 name so we can name variables almost 2356 02:00:13,199 --> 02:00:18,840 anything we want except for a few 2357 02:00:15,880 --> 02:00:21,440 restrictions number one we can't use 2358 02:00:18,840 --> 02:00:22,599 special words like let as a variable 2359 02:00:21,440 --> 02:00:25,280 name 2360 02:00:22,599 --> 02:00:27,719 that's because let already has a special 2361 02:00:25,280 --> 02:00:31,320 meaning in JavaScript it creates a 2362 02:00:27,719 --> 02:00:34,920 variable so let is a reserved word 2363 02:00:31,320 --> 02:00:37,920 however we can use let one or let two as 2364 02:00:34,920 --> 02:00:41,480 variable names we just can't use 2365 02:00:37,920 --> 02:00:44,159 let number two we can't start a variable 2366 02:00:41,480 --> 02:00:46,599 name with a number if we start with a 2367 02:00:44,159 --> 02:00:49,639 number JavaScript will think this is a 2368 02:00:46,599 --> 02:00:52,320 number instead of a variable name 2369 02:00:49,639 --> 02:00:53,199 however we can use numbers in the middle 2370 02:00:52,320 --> 02:00:56,800 or the 2371 02:00:53,199 --> 02:01:00,000 end lastly we can't use most special 2372 02:00:56,800 --> 02:01:03,000 characters like these or a space in a 2373 02:01:00,000 --> 02:01:06,679 variable name however there are two 2374 02:01:03,000 --> 02:01:09,040 special characters we can use dollar and 2375 02:01:06,679 --> 02:01:10,639 underscore so those are the rules for 2376 02:01:09,040 --> 02:01:13,119 naming 2377 02:01:10,639 --> 02:01:15,760 variables and then to save something 2378 02:01:13,119 --> 02:01:19,239 inside a variable we just use the equal 2379 02:01:15,760 --> 02:01:22,079 sign and then the value we want to save 2380 02:01:19,239 --> 02:01:24,760 and that's it after we create a variable 2381 02:01:22,079 --> 02:01:27,159 we can start using it in the rest of our 2382 02:01:24,760 --> 02:01:29,639 code now there's one last part of the 2383 02:01:27,159 --> 02:01:32,719 syntax that we haven't learned which is 2384 02:01:29,639 --> 02:01:36,040 the semicolon we've seen semicolons in 2385 02:01:32,719 --> 02:01:39,239 many places already in JavaScript a 2386 02:01:36,040 --> 02:01:42,280 semicolon means this is the end of an 2387 02:01:39,239 --> 02:01:45,440 instruction it's similar to a period in 2388 02:01:42,280 --> 02:01:47,880 English in English a period means this 2389 02:01:45,440 --> 02:01:51,599 is the end of a 2390 02:01:47,880 --> 02:01:54,679 sentence so here this semicolon tells 2391 02:01:51,599 --> 02:01:57,639 Java script that this is the end of an 2392 02:01:54,679 --> 02:02:01,360 instruction and now after this we can 2393 02:01:57,639 --> 02:02:05,079 add another instruction like 2394 02:02:01,360 --> 02:02:09,320 console. log Open Bracket and then the 2395 02:02:05,079 --> 02:02:11,199 string semicolon and a semicolon at the 2396 02:02:09,320 --> 02:02:14,079 end if we 2397 02:02:11,199 --> 02:02:17,440 save it will run these two instructions 2398 02:02:14,079 --> 02:02:19,560 one after another without any 2399 02:02:17,440 --> 02:02:23,560 problems so if we didn't have the 2400 02:02:19,560 --> 02:02:26,159 semicolon here and and we save it 2401 02:02:23,560 --> 02:02:29,719 wouldn't work because JavaScript thinks 2402 02:02:26,159 --> 02:02:32,520 that this entire line is one 2403 02:02:29,719 --> 02:02:35,079 instruction so we need semicolons to 2404 02:02:32,520 --> 02:02:37,079 separate different instructions in 2405 02:02:35,079 --> 02:02:39,880 JavaScript if we 2406 02:02:37,079 --> 02:02:43,280 save everything goes back to 2407 02:02:39,880 --> 02:02:45,000 normal so JavaScript is a little special 2408 02:02:43,280 --> 02:02:46,239 because it has a feature called 2409 02:02:45,000 --> 02:02:48,440 semicolon 2410 02:02:46,239 --> 02:02:51,199 insertion that means it will try to 2411 02:02:48,440 --> 02:02:54,239 insert the last semicolon in a line of 2412 02:02:51,199 --> 02:02:56,440 code automat automatically for example 2413 02:02:54,239 --> 02:02:59,040 if we removed the last 2414 02:02:56,440 --> 02:03:01,800 semicolon and 2415 02:02:59,040 --> 02:03:04,239 save this code will work without that 2416 02:03:01,800 --> 02:03:07,360 semicolon and that's because JavaScript 2417 02:03:04,239 --> 02:03:10,119 inserts the last semicolon 2418 02:03:07,360 --> 02:03:12,599 automatically however a lot of companies 2419 02:03:10,119 --> 02:03:14,880 still prefer to use semicolons in their 2420 02:03:12,599 --> 02:03:16,679 JavaScript code because semicolon 2421 02:03:14,880 --> 02:03:19,719 insertion can sometimes put the 2422 02:03:16,679 --> 02:03:21,840 semicolon in the wrong place for this 2423 02:03:19,719 --> 02:03:24,119 reason we're also going to use sem 2424 02:03:21,840 --> 02:03:27,719 colons in this course to help you get 2425 02:03:24,119 --> 02:03:29,960 used to JavaScript code with 2426 02:03:27,719 --> 02:03:32,719 semicolons next we're going to learn how 2427 02:03:29,960 --> 02:03:36,440 to change the value that is saved inside 2428 02:03:32,719 --> 02:03:39,920 a variable for example here we save the 2429 02:03:36,440 --> 02:03:41,840 number three inside variable one now 2430 02:03:39,920 --> 02:03:44,920 we're going to change the value saved 2431 02:03:41,840 --> 02:03:47,199 inside variable one to do that we're 2432 02:03:44,920 --> 02:03:48,239 going to go to the bottom and create 2433 02:03:47,199 --> 02:03:50,239 some new 2434 02:03:48,239 --> 02:03:53,079 lines and we're going to type the 2435 02:03:50,239 --> 02:03:56,840 variable name again so 2436 02:03:53,079 --> 02:03:59,760 variable one and now to change the value 2437 02:03:56,840 --> 02:04:02,440 saved inside we're just going to type 2438 02:03:59,760 --> 02:04:06,159 the equal sign and then a different 2439 02:04:02,440 --> 02:04:09,599 value like five and 2440 02:04:06,159 --> 02:04:12,599 semicolon and now if we console.log this 2441 02:04:09,599 --> 02:04:12,599 variable 2442 02:04:13,320 --> 02:04:21,719 console.log variable 1 and semicolon and 2443 02:04:18,639 --> 02:04:23,920 save it will show us that five is now 2444 02:04:21,719 --> 02:04:27,119 now saved inside variable 2445 02:04:23,920 --> 02:04:30,599 one notice also that if we scroll up to 2446 02:04:27,119 --> 02:04:33,280 the top the first console.log still 2447 02:04:30,599 --> 02:04:35,639 displays the value three and that's 2448 02:04:33,280 --> 02:04:38,239 because at this point in the code 2449 02:04:35,639 --> 02:04:41,119 variable one still contained three we 2450 02:04:38,239 --> 02:04:43,480 didn't update the variable yet so when 2451 02:04:41,119 --> 02:04:46,199 we first saved a value inside this 2452 02:04:43,480 --> 02:04:48,159 variable this is called assigning a 2453 02:04:46,199 --> 02:04:50,960 value to a 2454 02:04:48,159 --> 02:04:53,480 variable then when we change the value 2455 02:04:50,960 --> 02:04:56,520 inside this varable iable this is called 2456 02:04:53,480 --> 02:04:58,800 reassigning a value to a 2457 02:04:56,520 --> 02:05:02,119 variable now let's learn the syntax 2458 02:04:58,800 --> 02:05:04,199 rules for reassigning a value notice 2459 02:05:02,119 --> 02:05:07,520 that we don't use the word let when 2460 02:05:04,199 --> 02:05:08,559 reassigning that's because let creates a 2461 02:05:07,520 --> 02:05:12,239 new 2462 02:05:08,559 --> 02:05:14,559 variable so if we try to use let again 2463 02:05:12,239 --> 02:05:18,719 it would try to create a new variable 2464 02:05:14,559 --> 02:05:21,400 named variable one however variable one 2465 02:05:18,719 --> 02:05:24,239 already exists and we can't create two 2466 02:05:21,400 --> 02:05:28,079 VAR variables with the same name so this 2467 02:05:24,239 --> 02:05:31,280 would cause an error instead to reassign 2468 02:05:28,079 --> 02:05:35,679 a variable we just type out the variable 2469 02:05:31,280 --> 02:05:39,119 name and make it equal to something else 2470 02:05:35,679 --> 02:05:41,440 so that's the Syntax for reassigning a 2471 02:05:39,119 --> 02:05:44,800 variable now we're going to do one last 2472 02:05:41,440 --> 02:05:47,360 example before working on the project 2473 02:05:44,800 --> 02:05:49,040 let's go to the end of our code we're 2474 02:05:47,360 --> 02:05:51,639 going to type some new 2475 02:05:49,040 --> 02:05:54,599 lines and this time we're going to re 2476 02:05:51,639 --> 02:05:57,199 reassign variable one again so we can 2477 02:05:54,599 --> 02:06:03,639 reassign a variable as many times as we 2478 02:05:57,199 --> 02:06:07,760 want so let's type variable 1 and then 2479 02:06:03,639 --> 02:06:11,880 equals and we're going to type variable 2480 02:06:07,760 --> 02:06:14,440 1 + 1 and 2481 02:06:11,880 --> 02:06:17,800 semicolon so we learned earlier that we 2482 02:06:14,440 --> 02:06:20,199 can use a variable inside a calculation 2483 02:06:17,800 --> 02:06:22,559 and it will substitute whatever value is 2484 02:06:20,199 --> 02:06:24,960 inside the variable 2485 02:06:22,559 --> 02:06:27,480 here we're also using a variable inside 2486 02:06:24,960 --> 02:06:28,880 the calculation but we're using this 2487 02:06:27,480 --> 02:06:31,400 variable 2488 02:06:28,880 --> 02:06:34,320 itself so it's essentially taking this 2489 02:06:31,400 --> 02:06:36,480 variable increasing it by one and then 2490 02:06:34,320 --> 02:06:39,239 saving it 2491 02:06:36,480 --> 02:06:43,639 back if we 2492 02:06:39,239 --> 02:06:46,199 console.log this variable variable 2493 02:06:43,639 --> 02:06:50,480 one and 2494 02:06:46,199 --> 02:06:53,480 save variable one now contains six so it 2495 02:06:50,480 --> 02:06:56,840 took the previous value in variable one 2496 02:06:53,480 --> 02:06:57,760 which was five added one and then saved 2497 02:06:56,840 --> 02:07:00,639 it 2498 02:06:57,760 --> 02:07:04,119 back so this is how we increase the 2499 02:07:00,639 --> 02:07:06,840 value of a variable by a certain 2500 02:07:04,119 --> 02:07:09,239 number okay now that we know how to 2501 02:07:06,840 --> 02:07:12,360 create and reassign variables we're 2502 02:07:09,239 --> 02:07:14,280 ready to work on a project so I actually 2503 02:07:12,360 --> 02:07:17,239 prepared a project that we can do for 2504 02:07:14,280 --> 02:07:20,320 this lesson and we can see this project 2505 02:07:17,239 --> 02:07:23,559 by going to our browser clicking at the 2506 02:07:20,320 --> 02:07:26,360 top and and typing 2507 02:07:23,559 --> 02:07:31,280 super simple 2508 02:07:26,360 --> 02:07:34,119 dodev SL projects SLV 2509 02:07:31,280 --> 02:07:37,000 variables and press 2510 02:07:34,119 --> 02:07:39,880 enter so we're going to work on the cart 2511 02:07:37,000 --> 02:07:42,040 quantity feature of the final project so 2512 02:07:39,880 --> 02:07:44,639 in the final project we have a number in 2513 02:07:42,040 --> 02:07:47,360 the top right corner that shows how many 2514 02:07:44,639 --> 02:07:49,920 products are in our cart when we click 2515 02:07:47,360 --> 02:07:52,119 the add to cart button this quantity 2516 02:07:49,920 --> 02:07:54,520 will increase 2517 02:07:52,119 --> 02:07:57,079 so what you see here is a simple version 2518 02:07:54,520 --> 02:08:00,360 of the cart quantity feature that we can 2519 02:07:57,079 --> 02:08:02,960 create right now let's go over how it 2520 02:08:00,360 --> 02:08:06,320 works first make sure you have the 2521 02:08:02,960 --> 02:08:09,440 console open on this page now when we 2522 02:08:06,320 --> 02:08:11,639 click the show quantity button it will 2523 02:08:09,440 --> 02:08:15,040 show us that there are zero products in 2524 02:08:11,639 --> 02:08:17,599 our cart if we click the add to cart 2525 02:08:15,040 --> 02:08:22,119 button it will increase the quantity by 2526 02:08:17,599 --> 02:08:24,960 one and display it in the console 2527 02:08:22,119 --> 02:08:28,440 if we click add to cart again it will 2528 02:08:24,960 --> 02:08:30,800 increase the quantity by one again now 2529 02:08:28,440 --> 02:08:33,320 in the final project we can also add 2530 02:08:30,800 --> 02:08:35,320 more than one product at a time using 2531 02:08:33,320 --> 02:08:37,960 this 2532 02:08:35,320 --> 02:08:40,239 drop-down for now we're going to build a 2533 02:08:37,960 --> 02:08:42,960 simpler version of that we're going to 2534 02:08:40,239 --> 02:08:45,599 use the plus two and plus three buttons 2535 02:08:42,960 --> 02:08:48,000 to add multiple products so when we 2536 02:08:45,599 --> 02:08:50,480 click the plus two button it will 2537 02:08:48,000 --> 02:08:52,480 increase the quantity by two and display 2538 02:08:50,480 --> 02:08:54,880 it in the console 2539 02:08:52,480 --> 02:08:57,960 and when we click the plus three button 2540 02:08:54,880 --> 02:09:00,599 it will increase the quantity by 2541 02:08:57,960 --> 02:09:03,280 three and finally when we click the 2542 02:09:00,599 --> 02:09:06,320 reset cart button it will reset the 2543 02:09:03,280 --> 02:09:08,440 quantity back to zero and display this 2544 02:09:06,320 --> 02:09:11,040 message in the 2545 02:09:08,440 --> 02:09:13,920 console so now that we understand how 2546 02:09:11,040 --> 02:09:18,440 this project Works let's build this step 2547 02:09:13,920 --> 02:09:21,599 by step first let's create a new HTML 2548 02:09:18,440 --> 02:09:24,639 file just for this project we're going 2549 02:09:21,599 --> 02:09:27,880 to go to our code editor and click this 2550 02:09:24,639 --> 02:09:31,159 icon to show our files and then click 2551 02:09:27,880 --> 02:09:35,920 this icon to create a new 2552 02:09:31,159 --> 02:09:38,599 file let's name this file 05 2553 02:09:35,920 --> 02:09:40,320 dcart D 2554 02:09:38,599 --> 02:09:42,639 quantity. 2555 02:09:40,320 --> 02:09:47,800 HTML and press 2556 02:09:42,639 --> 02:09:49,639 enter next let's open variables. HTML 2557 02:09:47,800 --> 02:09:52,079 and we're going to copy all the code 2558 02:09:49,639 --> 02:09:56,119 here into our new 2559 02:09:52,079 --> 02:10:00,040 file let's type control a on Windows or 2560 02:09:56,119 --> 02:10:01,400 command a on Mac to select all this code 2561 02:10:00,040 --> 02:10:03,440 and then 2562 02:10:01,400 --> 02:10:05,840 rightclick 2563 02:10:03,440 --> 02:10:10,679 copy and then in our new 2564 02:10:05,840 --> 02:10:10,679 file right click and 2565 02:10:10,920 --> 02:10:16,559 paste let's close this for 2566 02:10:13,639 --> 02:10:22,400 now and in the new file we're going to 2567 02:10:16,559 --> 02:10:24,239 change the title to cart quantity 2568 02:10:22,400 --> 02:10:26,960 and let's remove all of our JavaScript 2569 02:10:24,239 --> 02:10:30,280 code but keep the script element because 2570 02:10:26,960 --> 02:10:33,719 we're going to write new JavaScript 2571 02:10:30,280 --> 02:10:35,800 code now we're ready to begin let's 2572 02:10:33,719 --> 02:10:39,159 start by creating these 2573 02:10:35,800 --> 02:10:40,920 buttons let's go to the body element 2574 02:10:39,159 --> 02:10:44,320 going to create a new 2575 02:10:40,920 --> 02:10:47,000 line and here as we learned in the 2576 02:10:44,320 --> 02:10:52,159 previous lesson we can create a button 2577 02:10:47,000 --> 02:10:54,440 using this HTML code less than button 2578 02:10:52,159 --> 02:10:57,079 greater than and the closing tag less 2579 02:10:54,440 --> 02:10:59,679 than SLB button greater 2580 02:10:57,079 --> 02:11:03,280 than inside this button we're going to 2581 02:10:59,679 --> 02:11:05,920 have the text show 2582 02:11:03,280 --> 02:11:08,639 quantity and let's 2583 02:11:05,920 --> 02:11:11,920 save now let's open this file in live 2584 02:11:08,639 --> 02:11:14,280 server by right clicking and then open 2585 02:11:11,920 --> 02:11:16,639 with live 2586 02:11:14,280 --> 02:11:19,920 server and we can see that we created 2587 02:11:16,639 --> 02:11:22,159 the first button now let's create the 2588 02:11:19,920 --> 02:11:25,040 other buttons here feel free to pause 2589 02:11:22,159 --> 02:11:27,800 the video if you want to try it 2590 02:11:25,040 --> 02:11:31,679 yourself we're going to go back to our 2591 02:11:27,800 --> 02:11:33,920 HTML and create another 2592 02:11:31,679 --> 02:11:38,000 button inside this one we're going to 2593 02:11:33,920 --> 02:11:41,599 have the text add to 2594 02:11:38,000 --> 02:11:43,559 cart let's create another 2595 02:11:41,599 --> 02:11:45,440 button this one is going to have the 2596 02:11:43,559 --> 02:11:49,280 text plus 2597 02:11:45,440 --> 02:11:53,559 two and we'll create another 2598 02:11:49,280 --> 02:11:57,280 button this one is going to be + 3 and 2599 02:11:53,559 --> 02:12:00,520 finally we'll create the last 2600 02:11:57,280 --> 02:12:02,360 button and this one is 2601 02:12:00,520 --> 02:12:04,599 reset 2602 02:12:02,360 --> 02:12:07,719 cart and now let's 2603 02:12:04,599 --> 02:12:10,880 save and if we go back to our 2604 02:12:07,719 --> 02:12:13,239 tab we've created all the 2605 02:12:10,880 --> 02:12:15,159 buttons now let's make this web page 2606 02:12:13,239 --> 02:12:17,440 interactive with 2607 02:12:15,159 --> 02:12:20,599 JavaScript first of all we need 2608 02:12:17,440 --> 02:12:22,880 somewhere to save the cart quantity so 2609 02:12:20,599 --> 02:12:25,199 as we learned in this lesson variables 2610 02:12:22,880 --> 02:12:28,719 are the perfect solution for this 2611 02:12:25,199 --> 02:12:31,119 variables let us save a value so let's 2612 02:12:28,719 --> 02:12:34,719 go into the script element and we're 2613 02:12:31,119 --> 02:12:37,679 going to create a variable by typing 2614 02:12:34,719 --> 02:12:41,320 let and this variable is going to save 2615 02:12:37,679 --> 02:12:45,040 the cart quantity so let's just name it 2616 02:12:41,320 --> 02:12:47,719 cart capital Q 2617 02:12:45,040 --> 02:12:52,000 quantity so the card quantity will start 2618 02:12:47,719 --> 02:12:54,079 out at zero so we're going to type equal 2619 02:12:52,000 --> 02:12:57,280 Z and 2620 02:12:54,079 --> 02:12:59,840 semicolon next when we click the show 2621 02:12:57,280 --> 02:13:03,119 quantity button we want to display the 2622 02:12:59,840 --> 02:13:05,760 quantity in the console to do this we 2623 02:13:03,119 --> 02:13:08,559 learned a special HTML attribute in the 2624 02:13:05,760 --> 02:13:11,079 previous lesson called the onclick 2625 02:13:08,559 --> 02:13:15,320 attribute so we're going to type in this 2626 02:13:11,079 --> 02:13:19,520 opening tag here space 2627 02:13:15,320 --> 02:13:22,599 onclick equals and double quotes So the 2628 02:13:19,520 --> 02:13:24,840 onclick attribute run some JavaScript 2629 02:13:22,599 --> 02:13:27,360 when we click this 2630 02:13:24,840 --> 02:13:30,559 button and between the double quotes we 2631 02:13:27,360 --> 02:13:31,679 can write JavaScript code so let's press 2632 02:13:30,559 --> 02:13:34,719 enter 2633 02:13:31,679 --> 02:13:37,760 enter And now when we click this button 2634 02:13:34,719 --> 02:13:42,760 we want to display the card quantity in 2635 02:13:37,760 --> 02:13:42,760 the console so let's type 2636 02:13:43,040 --> 02:13:49,199 console.log Open Bracket close bracket 2637 02:13:46,320 --> 02:13:52,159 semicolon and then between the brackets 2638 02:13:49,199 --> 02:13:55,320 we're going to display a message 2639 02:13:52,159 --> 02:13:57,760 now if we go back to Super 2640 02:13:55,320 --> 02:14:00,559 simple. and we click show 2641 02:13:57,760 --> 02:14:01,599 quantity we want to display this message 2642 02:14:00,559 --> 02:14:04,599 in the 2643 02:14:01,599 --> 02:14:07,880 console so in our code between the 2644 02:14:04,599 --> 02:14:10,679 brackets we're going to type the string 2645 02:14:07,880 --> 02:14:13,719 quote cart 2646 02:14:10,679 --> 02:14:18,119 quantity colon 2647 02:14:13,719 --> 02:14:19,400 0 now let's save and go back to our tab 2648 02:14:18,119 --> 02:14:21,920 to try it 2649 02:14:19,400 --> 02:14:25,639 out so we're going to open the console 2650 02:14:21,920 --> 02:14:28,159 first by right clicking and then click 2651 02:14:25,639 --> 02:14:30,599 inspect and then click the 2652 02:14:28,159 --> 02:14:33,559 console and now when we press the show 2653 02:14:30,599 --> 02:14:36,239 quantity button it should display that 2654 02:14:33,559 --> 02:14:39,599 message in the 2655 02:14:36,239 --> 02:14:42,079 console okay so we just made our project 2656 02:14:39,599 --> 02:14:44,760 interactive however we obviously don't 2657 02:14:42,079 --> 02:14:47,639 want to display zero every time we want 2658 02:14:44,760 --> 02:14:50,800 to display the value inside cart 2659 02:14:47,639 --> 02:14:52,480 quantity so we need to insert this value 2660 02:14:50,800 --> 02:14:55,360 in inside this 2661 02:14:52,480 --> 02:14:57,520 string remember from the strings lesson 2662 02:14:55,360 --> 02:14:59,880 when we want to insert a value into a 2663 02:14:57,520 --> 02:15:03,159 string we're going to use a template 2664 02:14:59,880 --> 02:15:05,040 string so let's switch the single quotes 2665 02:15:03,159 --> 02:15:08,360 to back 2666 02:15:05,040 --> 02:15:11,880 ticks to create a template 2667 02:15:08,360 --> 02:15:15,320 string and now instead of zero we're 2668 02:15:11,880 --> 02:15:18,280 going to insert a value using Dollar 2669 02:15:15,320 --> 02:15:21,040 open curly bracket and close curly 2670 02:15:18,280 --> 02:15:23,800 bracket and in between these brackets 2671 02:15:21,040 --> 02:15:25,320 we're going to insert this 2672 02:15:23,800 --> 02:15:29,239 variable 2673 02:15:25,320 --> 02:15:31,880 cart capital Q 2674 02:15:29,239 --> 02:15:34,440 quantity remember that JavaScript is 2675 02:15:31,880 --> 02:15:37,119 case sensitive so make sure you type the 2676 02:15:34,440 --> 02:15:40,119 variable name exactly the 2677 02:15:37,119 --> 02:15:43,520 same now let's 2678 02:15:40,119 --> 02:15:46,199 save and click show quantity 2679 02:15:43,520 --> 02:15:49,639 again and now it displays the value 2680 02:15:46,199 --> 02:15:52,000 inside card quantity in the console so 2681 02:15:49,639 --> 02:15:53,119 notice that variables also work with 2682 02:15:52,000 --> 02:15:55,639 string 2683 02:15:53,119 --> 02:15:58,960 interpolation again we can use a 2684 02:15:55,639 --> 02:16:01,159 variable wherever we use a value and it 2685 02:15:58,960 --> 02:16:04,760 will just substitute whatever value is 2686 02:16:01,159 --> 02:16:07,000 saved inside this variable into the 2687 02:16:04,760 --> 02:16:08,199 code now let's make the rest of the 2688 02:16:07,000 --> 02:16:10,800 buttons 2689 02:16:08,199 --> 02:16:13,480 interactive so when we click the add to 2690 02:16:10,800 --> 02:16:16,079 cart button we're going to add one to 2691 02:16:13,480 --> 02:16:19,079 this cart quantity and display it in the 2692 02:16:16,079 --> 02:16:21,639 console so again we're going to type 2693 02:16:19,079 --> 02:16:26,280 here and type space 2694 02:16:21,639 --> 02:16:28,480 on click equals double quotes and 2695 02:16:26,280 --> 02:16:29,639 between the double quotes we're going to 2696 02:16:28,480 --> 02:16:32,120 add 2697 02:16:29,639 --> 02:16:35,760 JavaScript let's also add some new lines 2698 02:16:32,120 --> 02:16:39,760 here to make our code easier to 2699 02:16:35,760 --> 02:16:42,000 read okay so in the onclick attribute 2700 02:16:39,760 --> 02:16:45,559 we're going to do two things the first 2701 02:16:42,000 --> 02:16:48,479 one is increase the card quantity by 2702 02:16:45,559 --> 02:16:51,000 one we can do that by reassigning the 2703 02:16:48,479 --> 02:16:52,840 card quantity variable 2704 02:16:51,000 --> 02:16:55,639 so we're going to type just the variable 2705 02:16:52,840 --> 02:16:57,639 name cart 2706 02:16:55,639 --> 02:17:01,359 quantity and we're going to make it 2707 02:16:57,639 --> 02:17:01,359 equal to 2708 02:17:01,840 --> 02:17:08,880 itself plus one and 2709 02:17:06,359 --> 02:17:11,880 semicolon next we're going to display 2710 02:17:08,880 --> 02:17:13,080 the updated quantity in the console so 2711 02:17:11,880 --> 02:17:15,559 let's press 2712 02:17:13,080 --> 02:17:18,120 enter and we're going to display this 2713 02:17:15,559 --> 02:17:20,719 message again so we can actually just 2714 02:17:18,120 --> 02:17:23,679 copy this code so we'll select it and 2715 02:17:20,719 --> 02:17:25,599 then right click and 2716 02:17:23,679 --> 02:17:29,679 copy and then 2717 02:17:25,599 --> 02:17:32,200 here right click and 2718 02:17:29,679 --> 02:17:36,040 paste now if we 2719 02:17:32,200 --> 02:17:38,399 save and click the add to card 2720 02:17:36,040 --> 02:17:40,639 button we'll see that it increased the 2721 02:17:38,399 --> 02:17:43,319 quantity by one and displayed the 2722 02:17:40,639 --> 02:17:45,200 updated quantity in the console let's 2723 02:17:43,319 --> 02:17:48,359 press it again to make sure it 2724 02:17:45,200 --> 02:17:51,200 works so it increases it by one again 2725 02:17:48,359 --> 02:17:52,840 and then displays it in the console 2726 02:17:51,200 --> 02:17:55,000 so that's basically how the cart 2727 02:17:52,840 --> 02:17:57,960 quantity feature works in the final 2728 02:17:55,000 --> 02:18:00,160 project first we need a variable to save 2729 02:17:57,960 --> 02:18:03,439 the quantity and then when we click a 2730 02:18:00,160 --> 02:18:06,559 button we're going to update the 2731 02:18:03,439 --> 02:18:09,319 quantity so now let's make the rest of 2732 02:18:06,559 --> 02:18:12,040 these buttons interactive feel free to 2733 02:18:09,319 --> 02:18:14,960 pause the video if you want to try it 2734 02:18:12,040 --> 02:18:18,719 yourself for the plus two button we're 2735 02:18:14,960 --> 02:18:22,240 going to add the onclick attribute again 2736 02:18:18,719 --> 02:18:24,800 onclick equals double quote 2737 02:18:22,240 --> 02:18:30,519 and inside we're going to increase the 2738 02:18:24,800 --> 02:18:34,840 cart quantity by two so cart quantity 2739 02:18:30,519 --> 02:18:39,040 and then we'll just make it equal to 2740 02:18:34,840 --> 02:18:41,080 itself plus two and 2741 02:18:39,040 --> 02:18:43,000 semicolon and again we're going to 2742 02:18:41,080 --> 02:18:45,920 display the updated quantity in the 2743 02:18:43,000 --> 02:18:49,719 console so we can create a new line and 2744 02:18:45,920 --> 02:18:51,399 just copy this code so select it right 2745 02:18:49,719 --> 02:18:56,719 click 2746 02:18:51,399 --> 02:19:00,920 copy and here right click and 2747 02:18:56,719 --> 02:19:03,800 paste let's save and click plus two a 2748 02:19:00,920 --> 02:19:06,359 few times and we can see that it adds 2749 02:19:03,800 --> 02:19:07,639 two to the card quantity and displays it 2750 02:19:06,359 --> 02:19:10,200 in the 2751 02:19:07,639 --> 02:19:11,160 console for the plus three button we'll 2752 02:19:10,200 --> 02:19:14,359 do the 2753 02:19:11,160 --> 02:19:17,160 same let's type an onclick 2754 02:19:14,359 --> 02:19:19,479 attribute click equals double 2755 02:19:17,160 --> 02:19:23,439 quotes and here we're going to increase 2756 02:19:19,479 --> 02:19:26,679 the card quantity by three three so cart 2757 02:19:23,439 --> 02:19:30,760 quantity equals 2758 02:19:26,679 --> 02:19:32,960 itself plus 3 and 2759 02:19:30,760 --> 02:19:34,880 semicolon and we're going to display 2760 02:19:32,960 --> 02:19:39,920 this in the console again so we'll 2761 02:19:34,880 --> 02:19:44,160 select this right click copy and here 2762 02:19:39,920 --> 02:19:47,120 rightclick and paste let's save again 2763 02:19:44,160 --> 02:19:49,319 and try it out to make sure it works so+ 2764 02:19:47,120 --> 02:19:52,520 three + 2765 02:19:49,319 --> 02:19:53,520 three and finally let's do the reset 2766 02:19:52,520 --> 02:19:56,319 card 2767 02:19:53,520 --> 02:20:00,760 button so we'll add the onclick 2768 02:19:56,319 --> 02:20:03,439 attribute again on click equals double 2769 02:20:00,760 --> 02:20:04,800 quotes and inside the double quotes 2770 02:20:03,439 --> 02:20:07,120 we're going to do something a little 2771 02:20:04,800 --> 02:20:09,280 different this time instead of 2772 02:20:07,120 --> 02:20:10,920 increasing the cart quantity we'll set 2773 02:20:09,280 --> 02:20:14,280 it back to 2774 02:20:10,920 --> 02:20:18,520 zero to do this we can just reassign the 2775 02:20:14,280 --> 02:20:23,680 cart quantity variable so cart 2776 02:20:18,520 --> 02:20:25,800 quantity and just make it equal to Z and 2777 02:20:23,680 --> 02:20:27,200 semicolon now if we go back to 2778 02:20:25,800 --> 02:20:30,240 superimple 2779 02:20:27,200 --> 02:20:32,520 dodev and try out this reset cart 2780 02:20:30,240 --> 02:20:35,399 button you'll notice that it displays 2781 02:20:32,520 --> 02:20:37,920 two messages the first message is cart 2782 02:20:35,399 --> 02:20:39,840 was reset so we're going to display that 2783 02:20:37,920 --> 02:20:42,840 message in our code 2784 02:20:39,840 --> 02:20:42,840 first 2785 02:20:43,960 --> 02:20:51,439 console.log and the string cart was 2786 02:20:48,439 --> 02:20:51,439 reset 2787 02:20:52,280 --> 02:20:57,120 and the second message is the same 2788 02:20:54,640 --> 02:20:58,800 message as the other buttons so we can 2789 02:20:57,120 --> 02:21:02,880 just select 2790 02:20:58,800 --> 02:21:09,040 this and then right click 2791 02:21:02,880 --> 02:21:12,960 copy and then here right click and 2792 02:21:09,040 --> 02:21:15,120 paste and now let's save and go back to 2793 02:21:12,960 --> 02:21:18,920 our Tab and give this a 2794 02:21:15,120 --> 02:21:20,880 try so the cart quantity starts at zero 2795 02:21:18,920 --> 02:21:24,479 when we press add to cart increase in es 2796 02:21:20,880 --> 02:21:27,120 by one this increases by two this 2797 02:21:24,479 --> 02:21:31,399 increases by three and when we click 2798 02:21:27,120 --> 02:21:34,040 reset cart it'll display cart was reset 2799 02:21:31,399 --> 02:21:37,040 and reset the cart quantity back to 2800 02:21:34,040 --> 02:21:38,920 zero and that's it we just created a 2801 02:21:37,040 --> 02:21:41,399 simple version of the cart quantity 2802 02:21:38,920 --> 02:21:42,720 feature of our final project using 2803 02:21:41,399 --> 02:21:44,439 everything that we learned in this 2804 02:21:42,720 --> 02:21:46,399 course so 2805 02:21:44,439 --> 02:21:48,960 far now we're going to learn some 2806 02:21:46,399 --> 02:21:52,560 shortcuts for reassigning 2807 02:21:48,960 --> 02:21:56,319 variables so if we look at this line of 2808 02:21:52,560 --> 02:21:59,640 code here we're increasing the value of 2809 02:21:56,319 --> 02:22:02,120 this variable by two and then saving it 2810 02:21:59,640 --> 02:22:07,520 back well there's actually a shortcut 2811 02:22:02,120 --> 02:22:11,359 for this and that shortcut is cart 2812 02:22:07,520 --> 02:22:14,399 quantity plus equal 2 and 2813 02:22:11,359 --> 02:22:17,800 semicolon so this does the same thing as 2814 02:22:14,399 --> 02:22:19,920 the line above but it's just shorter the 2815 02:22:17,800 --> 02:22:22,000 plus means we're going to add two to 2816 02:22:19,920 --> 02:22:23,760 cart quantity 2817 02:22:22,000 --> 02:22:27,000 and equals means we're going to save 2818 02:22:23,760 --> 02:22:29,319 that result back to card quantity so we 2819 02:22:27,000 --> 02:22:32,720 can actually delete this 2820 02:22:29,319 --> 02:22:34,000 line and the code will work the same way 2821 02:22:32,720 --> 02:22:36,399 if we 2822 02:22:34,000 --> 02:22:39,920 save and try it 2823 02:22:36,399 --> 02:22:42,200 out it will still add a two and then 2824 02:22:39,920 --> 02:22:46,040 save it back in card 2825 02:22:42,200 --> 02:22:48,840 quantity let's scroll up and practice by 2826 02:22:46,040 --> 02:22:52,720 using this shortcut for card quantity + 2827 02:22:48,840 --> 02:22:54,600 one so instead of this line we can type 2828 02:22:52,720 --> 02:22:59,000 cart 2829 02:22:54,600 --> 02:23:01,120 quantity plus equal 1 and 2830 02:22:59,000 --> 02:23:04,120 semicolon so it turns out that in 2831 02:23:01,120 --> 02:23:06,479 programming plus equals 1 is so common 2832 02:23:04,120 --> 02:23:11,319 that we have an even shorter shortcut 2833 02:23:06,479 --> 02:23:12,880 for plus equals 1 and that shortcut is 2834 02:23:11,319 --> 02:23:16,040 cart 2835 02:23:12,880 --> 02:23:19,240 quantity plus plus and 2836 02:23:16,040 --> 02:23:21,319 semicolon so this is the same thing as 2837 02:23:19,240 --> 02:23:23,840 plus equal 1 2838 02:23:21,319 --> 02:23:27,960 and all three lines do the same 2839 02:23:23,840 --> 02:23:30,560 thing so we can remove these 2840 02:23:27,960 --> 02:23:32,000 lines and the code will work the same 2841 02:23:30,560 --> 02:23:33,920 because it's just a 2842 02:23:32,000 --> 02:23:37,640 shortcut if we 2843 02:23:33,920 --> 02:23:40,680 save so if we click add to 2844 02:23:37,640 --> 02:23:43,319 cart it will add one every 2845 02:23:40,680 --> 02:23:44,560 time so those are some shortcuts for 2846 02:23:43,319 --> 02:23:46,840 reassigning 2847 02:23:44,560 --> 02:23:49,439 variables we also have these shortcuts 2848 02:23:46,840 --> 02:23:53,600 for the other operators like minus 2849 02:23:49,439 --> 02:23:56,520 equals multiply equals divide equals and 2850 02:23:53,600 --> 02:23:58,520 minus minus I'll leave some exercises 2851 02:23:56,520 --> 02:24:00,000 for these other shortcuts at the end of 2852 02:23:58,520 --> 02:24:02,279 this 2853 02:24:00,000 --> 02:24:05,560 lesson next we're going to learn some 2854 02:24:02,279 --> 02:24:08,160 best practices for naming variables so 2855 02:24:05,560 --> 02:24:08,160 if we scroll 2856 02:24:08,600 --> 02:24:15,279 down notice that our variable cart 2857 02:24:11,319 --> 02:24:17,560 quantity has two words in it cart and 2858 02:24:15,279 --> 02:24:20,399 quantity we learned earlier that we 2859 02:24:17,560 --> 02:24:22,399 can't have spaces in a variable name so 2860 02:24:20,399 --> 02:24:25,560 if we want to have multiple words we 2861 02:24:22,399 --> 02:24:27,200 have to combine them together like this 2862 02:24:25,560 --> 02:24:30,200 now you might be wondering why we wrote 2863 02:24:27,200 --> 02:24:31,399 it this way with a lowercase c and a 2864 02:24:30,200 --> 02:24:34,560 capital 2865 02:24:31,399 --> 02:24:38,319 Q This is actually a naming convention 2866 02:24:34,560 --> 02:24:41,080 called cam case in cam case we combine 2867 02:24:38,319 --> 02:24:43,240 the words together and capitalize every 2868 02:24:41,080 --> 02:24:46,960 word except the first 2869 02:24:43,240 --> 02:24:49,880 word so for cart quantity we capitalize 2870 02:24:46,960 --> 02:24:51,439 quantity but we keep cart lowercase 2871 02:24:49,880 --> 02:24:54,399 because it's the first 2872 02:24:51,439 --> 02:24:57,200 word cam case is actually the standard 2873 02:24:54,399 --> 02:25:00,200 naming convention for JavaScript all of 2874 02:24:57,200 --> 02:25:02,760 our variable names should use cam 2875 02:25:00,200 --> 02:25:05,600 case now in programming there are other 2876 02:25:02,760 --> 02:25:09,160 naming conventions that exist another 2877 02:25:05,600 --> 02:25:11,600 one is called Pascal case Pascal case is 2878 02:25:09,160 --> 02:25:14,479 the same as camel case except we 2879 02:25:11,600 --> 02:25:17,439 capitalize the first word so for cart 2880 02:25:14,479 --> 02:25:21,560 quantity Pascal case would be Capital 2881 02:25:17,439 --> 02:25:24,080 cart and capital quantity in JavaScript 2882 02:25:21,560 --> 02:25:26,160 there is one feature where we use Pascal 2883 02:25:24,080 --> 02:25:29,560 case which we'll learn later in this 2884 02:25:26,160 --> 02:25:32,920 course for everything else we use camel 2885 02:25:29,560 --> 02:25:36,080 case another naming convention is Kebab 2886 02:25:32,920 --> 02:25:39,160 case here we keep the words lowercase 2887 02:25:36,080 --> 02:25:40,040 and combine them with a dash like cart 2888 02:25:39,160 --> 02:25:42,720 Dash 2889 02:25:40,040 --> 02:25:45,479 quantity Kebab case doesn't work in 2890 02:25:42,720 --> 02:25:49,080 JavaScript because the dash is already a 2891 02:25:45,479 --> 02:25:52,840 minus symbol however we use Kebab case 2892 02:25:49,080 --> 02:25:55,520 in HTML and CSS and also in our file 2893 02:25:52,840 --> 02:25:59,080 names as you can see 2894 02:25:55,520 --> 02:26:01,279 here and finally we have snake case 2895 02:25:59,080 --> 02:26:04,359 where we keep the words lowercase and 2896 02:26:01,279 --> 02:26:06,560 combine them with underscores snake case 2897 02:26:04,359 --> 02:26:08,439 is used in other languages but it's not 2898 02:26:06,560 --> 02:26:10,479 really used in 2899 02:26:08,439 --> 02:26:14,000 JavaScript so those are the common 2900 02:26:10,479 --> 02:26:16,160 naming conventions that we use in 2901 02:26:14,000 --> 02:26:19,040 programming now one more thing I want to 2902 02:26:16,160 --> 02:26:21,399 note is that when we name our variables 2903 02:26:19,040 --> 02:26:24,880 try to pick a name that is not too short 2904 02:26:21,399 --> 02:26:26,880 or too long for example instead of cart 2905 02:26:24,880 --> 02:26:28,680 quantity we could have named this 2906 02:26:26,880 --> 02:26:31,760 variable 2907 02:26:28,680 --> 02:26:35,240 C but this name is too short it's hard 2908 02:26:31,760 --> 02:26:40,000 to understand what c means we could also 2909 02:26:35,240 --> 02:26:44,600 use a name like this quantity of 2910 02:26:40,000 --> 02:26:47,000 products in the cart but this name is 2911 02:26:44,600 --> 02:26:49,760 too long it's hard to read this in the 2912 02:26:47,000 --> 02:26:52,160 code so try to have a balance between a 2913 02:26:49,760 --> 02:26:54,960 name that is understandable but not too 2914 02:26:52,160 --> 02:26:56,760 long like cart 2915 02:26:54,960 --> 02:26:58,960 quantity the last thing we're going to 2916 02:26:56,760 --> 02:27:02,319 learn in this lesson is there are three 2917 02:26:58,960 --> 02:27:05,920 ways to create variables in JavaScript 2918 02:27:02,319 --> 02:27:07,960 let's go back to our variables. HTML 2919 02:27:05,920 --> 02:27:09,000 file and we're going to learn those 2920 02:27:07,960 --> 02:27:12,359 three 2921 02:27:09,000 --> 02:27:14,800 ways so the first way is to use let 2922 02:27:12,359 --> 02:27:17,560 which we already learned the second way 2923 02:27:14,800 --> 02:27:22,319 to create a variable is to use the word 2924 02:27:17,560 --> 02:27:25,760 const so at the bottom we can type const 2925 02:27:22,319 --> 02:27:30,800 space and then a variable name like 2926 02:27:25,760 --> 02:27:33,920 variable 2 and make it equal to 2927 02:27:30,800 --> 02:27:36,760 three so const creates a variable just 2928 02:27:33,920 --> 02:27:40,359 like let except we can't change its 2929 02:27:36,760 --> 02:27:43,200 value later this value stays constant 2930 02:27:40,359 --> 02:27:49,840 that's why it's called const if we try 2931 02:27:43,200 --> 02:27:52,439 to change its value like variable 2 = 5 2932 02:27:49,840 --> 02:27:55,000 and then and we save this and we open 2933 02:27:52,439 --> 02:27:59,399 this file in the browser by right 2934 02:27:55,000 --> 02:28:03,080 clicking and then open with live 2935 02:27:59,399 --> 02:28:05,800 server and then open the console so 2936 02:28:03,080 --> 02:28:09,560 rightclick 2937 02:28:05,800 --> 02:28:09,560 inspect and then click the 2938 02:28:10,000 --> 02:28:15,680 console it will give us an error when we 2939 02:28:12,560 --> 02:28:19,720 try to change this variable so this 2940 02:28:15,680 --> 02:28:21,439 doesn't work let's remove this line and 2941 02:28:19,720 --> 02:28:23,560 Save 2942 02:28:21,439 --> 02:28:26,880 so why would we ever use const instead 2943 02:28:23,560 --> 02:28:29,439 of let well const makes our code safer 2944 02:28:26,880 --> 02:28:32,600 and easier to understand when we create 2945 02:28:29,439 --> 02:28:35,080 a variable with const we know for sure 2946 02:28:32,600 --> 02:28:38,640 that this variable will always contain 2947 02:28:35,080 --> 02:28:41,560 three for let however if we create a 2948 02:28:38,640 --> 02:28:44,319 variable it's hard to know what value is 2949 02:28:41,560 --> 02:28:47,000 inside this variable because later on we 2950 02:28:44,319 --> 02:28:50,439 can change this value in our 2951 02:28:47,000 --> 02:28:52,439 code so in order to keep our code safer 2952 02:28:50,439 --> 02:28:55,520 it's actually a best practice to use 2953 02:28:52,439 --> 02:28:57,520 const by default and only use let when 2954 02:28:55,520 --> 02:29:00,319 we know that we need to change the 2955 02:28:57,520 --> 02:29:03,640 variable so here it's actually best 2956 02:29:00,319 --> 02:29:05,680 practice to use const to create this 2957 02:29:03,640 --> 02:29:08,800 calculation variable because we're not 2958 02:29:05,680 --> 02:29:10,040 changing it later same thing for the 2959 02:29:08,800 --> 02:29:13,880 result 2960 02:29:10,040 --> 02:29:16,960 variable and the message 2961 02:29:13,880 --> 02:29:19,279 variable now for variable one we have to 2962 02:29:16,960 --> 02:29:21,040 use let because we're changing it later 2963 02:29:19,279 --> 02:29:23,560 in this code 2964 02:29:21,040 --> 02:29:25,800 so that's const it creates a variable 2965 02:29:23,560 --> 02:29:28,600 that can't be changed 2966 02:29:25,800 --> 02:29:32,760 later finally the Third Way of creating 2967 02:29:28,600 --> 02:29:37,279 a variable is to use another word VAR so 2968 02:29:32,760 --> 02:29:42,240 at the bottom we can type VAR space and 2969 02:29:37,279 --> 02:29:44,479 then a variable name like variable 3 and 2970 02:29:42,240 --> 02:29:47,399 make it equal to 2971 02:29:44,479 --> 02:29:49,479 three so VAR creates a variable just 2972 02:29:47,399 --> 02:29:50,560 like let and this variable can be 2973 02:29:49,479 --> 02:29:53,640 changed 2974 02:29:50,560 --> 02:29:57,399 later VAR is actually the original way 2975 02:29:53,640 --> 02:29:58,840 to create variables in JavaScript VAR 2976 02:29:57,399 --> 02:30:01,760 means 2977 02:29:58,840 --> 02:30:04,240 variable however VAR has some issues 2978 02:30:01,760 --> 02:30:06,600 that we'll learn later in this course 2979 02:30:04,240 --> 02:30:09,920 and because of these issues we don't use 2980 02:30:06,600 --> 02:30:12,439 VAR in new JavaScript code you might see 2981 02:30:09,920 --> 02:30:15,439 VAR in older JavaScript code so it's 2982 02:30:12,439 --> 02:30:18,680 still useful to know so those are three 2983 02:30:15,439 --> 02:30:23,279 ways to create variables in JavaScript 2984 02:30:18,680 --> 02:30:25,600 let constant and VAR we use const by 2985 02:30:23,279 --> 02:30:28,600 default and if we have to change a 2986 02:30:25,600 --> 02:30:30,680 variable then use 2987 02:30:28,600 --> 02:30:34,319 let so one more thing we're going to 2988 02:30:30,680 --> 02:30:37,560 learn is that we can use type of with 2989 02:30:34,319 --> 02:30:42,439 variables so remember that type of tells 2990 02:30:37,560 --> 02:30:43,359 us what type a value is for example if 2991 02:30:42,439 --> 02:30:45,680 we 2992 02:30:43,359 --> 02:30:47,240 wrote 2993 02:30:45,680 --> 02:30:51,120 console. 2994 02:30:47,240 --> 02:30:53,680 log type of 2995 02:30:51,120 --> 02:30:57,399 three and 2996 02:30:53,680 --> 02:30:58,279 save type of will tell us that three is 2997 02:30:57,399 --> 02:31:01,560 a 2998 02:30:58,279 --> 02:31:02,960 number well we can actually use type of 2999 02:31:01,560 --> 02:31:06,720 with a 3000 02:31:02,960 --> 02:31:10,720 variable so if we change this to 3001 02:31:06,720 --> 02:31:13,720 variable 2 and 3002 02:31:10,720 --> 02:31:17,160 save type of will tell us the type of 3003 02:31:13,720 --> 02:31:19,520 value inside the variable which is a 3004 02:31:17,160 --> 02:31:21,880 number and that's what we see in the 3005 02:31:19,520 --> 02:31:24,399 console 3006 02:31:21,880 --> 02:31:28,439 now let's check the type of this 3007 02:31:24,399 --> 02:31:33,359 variable message so message contains a 3008 02:31:28,439 --> 02:31:33,359 string and if we do 3009 02:31:33,960 --> 02:31:41,200 console.log type of 3010 02:31:38,600 --> 02:31:44,040 message and 3011 02:31:41,200 --> 02:31:48,120 save this will tell us that the value 3012 02:31:44,040 --> 02:31:50,960 inside the message variable is a string 3013 02:31:48,120 --> 02:31:54,840 so we can use type of to check the type 3014 02:31:50,960 --> 02:31:57,000 of value that is saved inside a variable 3015 02:31:54,840 --> 02:31:59,960 and that's the end of this lesson in 3016 02:31:57,000 --> 02:32:02,640 this lesson we learned about variables 3017 02:31:59,960 --> 02:32:05,840 which are a way to save values and use 3018 02:32:02,640 --> 02:32:08,600 them later we learned how to reassign a 3019 02:32:05,840 --> 02:32:11,279 variable we created the cart quantity 3020 02:32:08,600 --> 02:32:13,439 feature of the final project we learned 3021 02:32:11,279 --> 02:32:15,520 some shortcuts for reassigning a 3022 02:32:13,439 --> 02:32:17,720 variable we learned some naming 3023 02:32:15,520 --> 02:32:20,279 conventions and best practices for 3024 02:32:17,720 --> 02:32:24,040 variable names and we learned learned 3025 02:32:20,279 --> 02:32:25,920 three ways to create a variable let 3026 02:32:24,040 --> 02:32:28,800 const and 3027 02:32:25,920 --> 02:32:32,960 VAR here are some exercises to help you 3028 02:32:28,800 --> 02:32:32,960 practice using variables 3029 02:32:58,680 --> 02:33:03,520 in this lesson we're going to learn two 3030 02:33:00,920 --> 02:33:06,200 more features of JavaScript called 3031 02:33:03,520 --> 02:33:08,520 booleans and if statements and we're 3032 02:33:06,200 --> 02:33:11,600 going to use these features to build 3033 02:33:08,520 --> 02:33:14,479 this rock paper scissors 3034 02:33:11,600 --> 02:33:17,840 project let's start by creating a new 3035 02:33:14,479 --> 02:33:20,279 HTML file for this lesson we're going to 3036 02:33:17,840 --> 02:33:23,160 go to our code editor and click this 3037 02:33:20,279 --> 02:33:26,640 icon to show our files and then click 3038 02:33:23,160 --> 02:33:30,200 this icon to create a new file we're 3039 02:33:26,640 --> 02:33:32,640 going to name this file 3040 02:33:30,200 --> 02:33:34,279 06- 3041 02:33:32,640 --> 02:33:36,920 booleans 3042 02:33:34,279 --> 02:33:40,640 HTML and press 3043 02:33:36,920 --> 02:33:43,000 enter let's open variables. 3044 02:33:40,640 --> 02:33:46,200 HTML and we're going to copy all this 3045 02:33:43,000 --> 02:33:49,000 code into our new file let's select the 3046 02:33:46,200 --> 02:33:52,560 code using control a on Windows or 3047 02:33:49,000 --> 02:33:54,720 command a on Mac and then 3048 02:33:52,560 --> 02:33:56,880 rightclick 3049 02:33:54,720 --> 02:34:00,880 copy and in 3050 02:33:56,880 --> 02:34:04,359 here rightclick and 3051 02:34:00,880 --> 02:34:06,960 paste let's close this for now and 3052 02:34:04,359 --> 02:34:09,840 prepare this file for this lesson so 3053 02:34:06,960 --> 02:34:11,880 we'll scroll to the top and change this 3054 02:34:09,840 --> 02:34:14,080 to 3055 02:34:11,880 --> 02:34:16,720 booleans and then remove all the 3056 02:34:14,080 --> 02:34:19,720 JavaScript code but keep the script 3057 02:34:16,720 --> 02:34:19,720 element 3058 02:34:20,840 --> 02:34:25,120 now let's 3059 02:34:22,000 --> 02:34:27,880 save and open this file in live server 3060 02:34:25,120 --> 02:34:29,880 by right clicking and then open with 3061 02:34:27,880 --> 02:34:34,319 live 3062 02:34:29,880 --> 02:34:34,319 server and we can close the previous 3063 02:34:34,680 --> 02:34:42,120 tabs as well as the previous code for 3064 02:34:39,120 --> 02:34:44,760 now we'll start by learning what are 3065 02:34:42,120 --> 02:34:47,439 booleans so far in this course we learn 3066 02:34:44,760 --> 02:34:50,880 two types of values in JavaScript 3067 02:34:47,439 --> 02:34:53,800 numbers and strings booleans are another 3068 02:34:50,880 --> 02:34:56,040 type of value in JavaScript but booleans 3069 02:34:53,800 --> 02:34:59,279 are special because there are only two 3070 02:34:56,040 --> 02:35:02,680 Boolean values true and 3071 02:34:59,279 --> 02:35:08,720 false let's click in the script element 3072 02:35:02,680 --> 02:35:11,760 and type true press enter and type 3073 02:35:08,720 --> 02:35:15,880 false so these are the only two Boolean 3074 02:35:11,760 --> 02:35:18,880 values that exist true and false so 3075 02:35:15,880 --> 02:35:20,800 what's the purpose of Boolean values a 3076 02:35:18,880 --> 02:35:23,279 Boolean value represents whether 3077 02:35:20,800 --> 02:35:26,439 something is true or 3078 02:35:23,279 --> 02:35:29,319 false for example in JavaScript we can 3079 02:35:26,439 --> 02:35:35,319 compare two numbers let's create some 3080 02:35:29,319 --> 02:35:38,240 new lines and type the code 3 less than 3081 02:35:35,319 --> 02:35:41,399 5 so this code checks whether the number 3082 02:35:38,240 --> 02:35:45,240 three is less than five and of course 3083 02:35:41,399 --> 02:35:49,240 this is true so if we console.log this 3084 02:35:45,240 --> 02:35:49,240 code console.log 3085 02:35:50,800 --> 02:35:55,240 and 3086 02:35:51,960 --> 02:35:58,479 save and then go to our website and open 3087 02:35:55,240 --> 02:36:00,520 the console rightclick 3088 02:35:58,479 --> 02:36:02,840 inspect and 3089 02:36:00,520 --> 02:36:06,160 console it will show us that the result 3090 02:36:02,840 --> 02:36:08,479 of this code is the Boolean value 3091 02:36:06,160 --> 02:36:11,800 true let's try another 3092 02:36:08,479 --> 02:36:14,279 example let's delete this less than 3093 02:36:11,800 --> 02:36:17,120 symbol and replace it with a greater 3094 02:36:14,279 --> 02:36:20,080 than symbol so now this code checks 3095 02:36:17,120 --> 02:36:21,960 whether three is greater than five 3096 02:36:20,080 --> 02:36:24,040 and we know that three is not greater 3097 02:36:21,960 --> 02:36:26,640 than five so if we 3098 02:36:24,040 --> 02:36:29,080 save the computer will tell us that the 3099 02:36:26,640 --> 02:36:32,960 result of this comparison is the Boolean 3100 02:36:29,080 --> 02:36:35,040 value false so as you can see a Boolean 3101 02:36:32,960 --> 02:36:37,160 value represents whether something is 3102 02:36:35,040 --> 02:36:39,840 true or 3103 02:36:37,160 --> 02:36:43,040 false now let's learn the syntax rules 3104 02:36:39,840 --> 02:36:46,399 for booleans to create a Boolean we just 3105 02:36:43,040 --> 02:36:49,680 type true or false note that we don't 3106 02:36:46,399 --> 02:36:51,840 surround these with quotes like this if 3107 02:36:49,680 --> 02:36:53,680 we surround them with quotes this is now 3108 02:36:51,840 --> 02:36:57,680 a string not a 3109 02:36:53,680 --> 02:37:02,479 Boolean we can use type of to check this 3110 02:36:57,680 --> 02:37:02,479 so at the front we can type 3111 02:37:02,960 --> 02:37:09,160 console.log type of 3112 02:37:06,920 --> 02:37:12,720 true if we 3113 02:37:09,160 --> 02:37:16,760 save this will tell us that this is a 3114 02:37:12,720 --> 02:37:20,279 string now if we remove the 3115 02:37:16,760 --> 02:37:24,080 quotes and Save 3116 02:37:20,279 --> 02:37:27,479 this will tell us that this is now a 3117 02:37:24,080 --> 02:37:30,439 Boolean so when creating a Boolean don't 3118 02:37:27,479 --> 02:37:32,800 put quotes around true or 3119 02:37:30,439 --> 02:37:36,000 false let's 3120 02:37:32,800 --> 02:37:39,760 save now one way to create a Boolean is 3121 02:37:36,000 --> 02:37:43,000 to compare two numbers for example 3 3122 02:37:39,760 --> 02:37:46,680 greater than 5 so this greater than 3123 02:37:43,000 --> 02:37:48,399 symbol is called a comparison operator 3124 02:37:46,680 --> 02:37:50,399 and there are many other comparison 3125 02:37:48,399 --> 02:37:53,319 operators we can use 3126 02:37:50,399 --> 02:37:56,399 we saw less than earlier and we also 3127 02:37:53,319 --> 02:37:59,520 have greater than or equal to less than 3128 02:37:56,399 --> 02:38:02,080 or equal to Triple equals which checks 3129 02:37:59,520 --> 02:38:04,920 if two values are equal to each other 3130 02:38:02,080 --> 02:38:07,359 and exclamation double equals which 3131 02:38:04,920 --> 02:38:10,040 checks if two values are not equal to 3132 02:38:07,359 --> 02:38:12,960 each other now JavaScript is a little 3133 02:38:10,040 --> 02:38:16,399 special because it has two ways to check 3134 02:38:12,960 --> 02:38:19,080 if two values are equal triple equals 3135 02:38:16,399 --> 02:38:21,560 and double equals the difference is that 3136 02:38:19,080 --> 02:38:25,560 double equals tries to convert both 3137 02:38:21,560 --> 02:38:30,680 values into the same type for example in 3138 02:38:25,560 --> 02:38:32,319 our code let's create some new lines and 3139 02:38:30,680 --> 02:38:36,200 console. 3140 02:38:32,319 --> 02:38:40,920 log and let's check if the number five 3141 02:38:36,200 --> 02:38:40,920 is equal to the string 3142 02:38:41,479 --> 02:38:45,840 5. if we 3143 02:38:43,399 --> 02:38:48,880 save it will tell us that these two 3144 02:38:45,840 --> 02:38:50,760 values are equal to each other even 3145 02:38:48,880 --> 02:38:53,600 though one of of them is a number and 3146 02:38:50,760 --> 02:38:56,319 the other is a string and that's because 3147 02:38:53,600 --> 02:38:59,560 the double equals converts both values 3148 02:38:56,319 --> 02:39:02,439 into the same type so converts both of 3149 02:38:59,560 --> 02:39:05,040 these into the number five and then 3150 02:39:02,439 --> 02:39:08,319 Compares them so that's why they are 3151 02:39:05,040 --> 02:39:10,840 equal however this is not a good idea 3152 02:39:08,319 --> 02:39:14,439 because this value is a number and this 3153 02:39:10,840 --> 02:39:16,200 value is a string it's a piece of text 3154 02:39:14,439 --> 02:39:19,479 so they shouldn't really be equal to 3155 02:39:16,200 --> 02:39:22,120 each other that's why in JavaScript we 3156 02:39:19,479 --> 02:39:24,800 always use the triple equals to check if 3157 02:39:22,120 --> 02:39:28,479 two values are the same so that we avoid 3158 02:39:24,800 --> 02:39:29,760 the conversion behavior of double equals 3159 02:39:28,479 --> 02:39:32,399 so if we 3160 02:39:29,760 --> 02:39:35,120 save now it will tell us that these two 3161 02:39:32,399 --> 02:39:37,520 values are not equal to each other 3162 02:39:35,120 --> 02:39:39,120 because one value is a number and the 3163 02:39:37,520 --> 02:39:42,399 other value is a 3164 02:39:39,120 --> 02:39:45,560 string so this rule also applies to not 3165 02:39:42,399 --> 02:39:48,319 equal to in JavaScript we always use 3166 02:39:45,560 --> 02:39:51,040 exclamation double equals instead of 3167 02:39:48,319 --> 02:39:52,760 exclamation equals to avoid the 3168 02:39:51,040 --> 02:39:55,200 conversion 3169 02:39:52,760 --> 02:39:58,399 Behavior All right so those are 3170 02:39:55,200 --> 02:40:01,080 comparison operators in the order of 3171 02:39:58,399 --> 02:40:04,240 operations comparison operators have a 3172 02:40:01,080 --> 02:40:08,720 lower priority than math so if we did 3173 02:40:04,240 --> 02:40:12,560 something like 5 and then minus 3174 02:40:08,720 --> 02:40:16,000 5 this would calculate 5 - 5 first which 3175 02:40:12,560 --> 02:40:19,040 is zero and then compare the two numbers 3176 02:40:16,000 --> 02:40:19,880 so three is now greater than zero so if 3177 02:40:19,040 --> 02:40:23,359 we 3178 02:40:19,880 --> 02:40:26,399 save the first comparison will now be 3179 02:40:23,359 --> 02:40:29,319 true so comparison operators have a 3180 02:40:26,399 --> 02:40:31,760 lower priority than math 3181 02:40:29,319 --> 02:40:34,080 operators now that we know what Boolean 3182 02:40:31,760 --> 02:40:36,560 values are we're going to combine them 3183 02:40:34,080 --> 02:40:39,760 with a really useful feature called if 3184 02:40:36,560 --> 02:40:42,760 statements an if statement lets us write 3185 02:40:39,760 --> 02:40:45,840 multiple groups of code and then decide 3186 02:40:42,760 --> 02:40:48,760 which code to run Let's do an example 3187 02:40:45,840 --> 02:40:50,000 and create our first if statement we're 3188 02:40:48,760 --> 02:40:53,640 going to go to the the 3189 02:40:50,000 --> 02:40:58,680 bottom and create some new 3190 02:40:53,640 --> 02:41:01,840 lines and type if and Open Bracket Clos 3191 02:40:58,680 --> 02:41:03,000 bracket and open curly bracket closed 3192 02:41:01,840 --> 02:41:06,000 curly 3193 02:41:03,000 --> 02:41:09,399 bracket between these round brackets 3194 02:41:06,000 --> 02:41:12,640 we're going to put a Boolean value like 3195 02:41:09,399 --> 02:41:15,760 true and in between the curly brackets 3196 02:41:12,640 --> 02:41:19,760 we're going to put some code to run for 3197 02:41:15,760 --> 02:41:19,760 example console.log 3198 02:41:21,399 --> 02:41:27,080 hello so the way that an if statement 3199 02:41:23,800 --> 02:41:29,720 works is that if this Boolean value is 3200 02:41:27,080 --> 02:41:32,640 true then we're going to run the code in 3201 02:41:29,720 --> 02:41:35,479 the curly brackets if this Boolean value 3202 02:41:32,640 --> 02:41:37,840 is false then we're not going to run the 3203 02:41:35,479 --> 02:41:40,680 code if we 3204 02:41:37,840 --> 02:41:43,319 save the value between the brackets is 3205 02:41:40,680 --> 02:41:44,479 true so we're going to run this code and 3206 02:41:43,319 --> 02:41:51,000 display 3207 02:41:44,479 --> 02:41:54,000 hello now if we change this to false and 3208 02:41:51,000 --> 02:41:57,399 save it will not run this 3209 02:41:54,000 --> 02:42:00,200 code so an if statement lets us decide 3210 02:41:57,399 --> 02:42:01,880 whether or not to run some code based on 3211 02:42:00,200 --> 02:42:04,240 this Boolean 3212 02:42:01,880 --> 02:42:06,399 value now if statements have another 3213 02:42:04,240 --> 02:42:09,000 useful feature called 3214 02:42:06,399 --> 02:42:10,760 else let's go to the end of this if 3215 02:42:09,000 --> 02:42:12,520 statement and 3216 02:42:10,760 --> 02:42:15,880 type 3217 02:42:12,520 --> 02:42:18,240 else open curly bracket close curly 3218 02:42:15,880 --> 02:42:20,120 bracket and then inside these curly 3219 02:42:18,240 --> 02:42:24,319 brackets we're going to put some 3220 02:42:20,120 --> 02:42:24,319 different code like 3221 02:42:24,760 --> 02:42:31,240 console.log 3222 02:42:27,439 --> 02:42:33,800 else the way else works is if this 3223 02:42:31,240 --> 02:42:37,080 Boolean value is true the computer will 3224 02:42:33,800 --> 02:42:38,720 run this code otherwise we will run the 3225 02:42:37,080 --> 02:42:42,000 code inside 3226 02:42:38,720 --> 02:42:45,800 else let's give it a try we'll change 3227 02:42:42,000 --> 02:42:48,760 this Boolean value to true and 3228 02:42:45,800 --> 02:42:49,800 save and because it is true it will run 3229 02:42:48,760 --> 02:42:51,240 this code 3230 02:42:49,800 --> 02:42:56,319 and display 3231 02:42:51,240 --> 02:42:57,279 hello now if this Boolean value is false 3232 02:42:56,319 --> 02:43:00,359 and 3233 02:42:57,279 --> 02:43:02,359 save because it is false it will not run 3234 02:43:00,359 --> 02:43:05,600 this code and it will run the code 3235 02:43:02,359 --> 02:43:09,200 inside else and display 3236 02:43:05,600 --> 02:43:11,880 else so using IF and else we can write 3237 02:43:09,200 --> 02:43:13,200 two groups of code and then decide which 3238 02:43:11,880 --> 02:43:16,760 code to 3239 02:43:13,200 --> 02:43:18,439 run now let's do a practical example 3240 02:43:16,760 --> 02:43:20,560 we're going to write some code to check 3241 02:43:18,439 --> 02:43:23,439 if someone is old enough to 3242 02:43:20,560 --> 02:43:24,600 drive let's go to the bottom and create 3243 02:43:23,439 --> 02:43:28,240 some new 3244 02:43:24,600 --> 02:43:30,640 lines and we're going to type if 3245 02:43:28,240 --> 02:43:33,800 brackets and curly 3246 02:43:30,640 --> 02:43:36,880 brackets and inside here we're going to 3247 02:43:33,800 --> 02:43:40,240 compare two numbers the first number is 3248 02:43:36,880 --> 02:43:42,720 a person's age let's pretend we have a 3249 02:43:40,240 --> 02:43:44,399 person who is 30 years old so we're 3250 02:43:42,720 --> 02:43:48,080 going to type 3251 02:43:44,399 --> 02:43:51,279 30 then we're going to check if this age 3252 02:43:48,080 --> 02:43:54,200 is greater than or equal to the legal 3253 02:43:51,279 --> 02:43:57,000 driving age Let's Pretend the legal 3254 02:43:54,200 --> 02:43:59,920 driving age is 3255 02:43:57,000 --> 02:44:03,160 16 notice that between the brackets we 3256 02:43:59,920 --> 02:44:05,479 don't have to just put a Boolean value 3257 02:44:03,160 --> 02:44:06,680 we can put any code that results in a 3258 02:44:05,479 --> 02:44:09,720 Boolean 3259 02:44:06,680 --> 02:44:12,399 value so this comparison results in the 3260 02:44:09,720 --> 02:44:14,520 Boolean value true that means the 3261 02:44:12,399 --> 02:44:18,520 computer will run the code between the 3262 02:44:14,520 --> 02:44:19,760 curly brackets so in here let's type 3263 02:44:18,520 --> 02:44:22,359 console 3264 02:44:19,760 --> 02:44:27,160 . 3265 02:44:22,359 --> 02:44:30,560 log you can 3266 02:44:27,160 --> 02:44:33,600 drive now if this is not true let's run 3267 02:44:30,560 --> 02:44:34,960 some different code at the bottom we'll 3268 02:44:33,600 --> 02:44:38,720 type 3269 02:44:34,960 --> 02:44:42,800 else and curly brackets and then inside 3270 02:44:38,720 --> 02:44:42,800 these brackets we'll type 3271 02:44:43,640 --> 02:44:50,640 console.log you canot drive 3272 02:44:49,359 --> 02:44:53,040 now let's 3273 02:44:50,640 --> 02:44:56,479 save and it will tell me that this 3274 02:44:53,040 --> 02:44:59,880 person who is 30 years old can 3275 02:44:56,479 --> 02:45:04,240 drive if we go back and change this to a 3276 02:44:59,880 --> 02:45:06,920 person that is 15 years old and 3277 02:45:04,240 --> 02:45:10,600 save this comparison will result in 3278 02:45:06,920 --> 02:45:13,840 false so it will display you cannot 3279 02:45:10,600 --> 02:45:16,279 drive so that's how we use if statements 3280 02:45:13,840 --> 02:45:18,600 we can write multiple groups of code for 3281 02:45:16,279 --> 02:45:21,200 different situations and then decide 3282 02:45:18,600 --> 02:45:24,200 which Cod code to 3283 02:45:21,200 --> 02:45:27,240 run now let's learn the syntax rules for 3284 02:45:24,200 --> 02:45:30,399 if statements to create an if statement 3285 02:45:27,240 --> 02:45:33,720 we type if and then round brackets with 3286 02:45:30,399 --> 02:45:37,439 a Boolean value inside this is called 3287 02:45:33,720 --> 02:45:40,640 the condition if this condition is true 3288 02:45:37,439 --> 02:45:43,240 we will run the code inside the curly 3289 02:45:40,640 --> 02:45:45,680 brackets we can also add an else 3290 02:45:43,240 --> 02:45:49,160 statement this code will run if the 3291 02:45:45,680 --> 02:45:51,720 condition is false the else statement is 3292 02:45:49,160 --> 02:45:54,560 optional we don't have to have 3293 02:45:51,720 --> 02:45:57,600 one so these curly brackets and the code 3294 02:45:54,560 --> 02:45:59,680 inside these are called branches because 3295 02:45:57,600 --> 02:46:02,399 it's like a tree branch the code is 3296 02:45:59,680 --> 02:46:06,160 splitting off into two directions and we 3297 02:46:02,399 --> 02:46:09,279 either run this code or we run this 3298 02:46:06,160 --> 02:46:12,640 code one more thing to know is in a 3299 02:46:09,279 --> 02:46:15,680 branch if we only have one line of code 3300 02:46:12,640 --> 02:46:17,760 the curly brackets are actually optional 3301 02:46:15,680 --> 02:46:19,520 so we can delete these and the code will 3302 02:46:17,760 --> 02:46:21,800 still work 3303 02:46:19,520 --> 02:46:24,720 however if we have more than one line of 3304 02:46:21,800 --> 02:46:28,399 code in this Branch for example if we 3305 02:46:24,720 --> 02:46:28,399 add another line 3306 02:46:28,800 --> 02:46:34,279 console.log 3307 02:46:31,640 --> 02:46:36,920 congrats now this branch has two lines 3308 02:46:34,279 --> 02:46:41,240 of code so the curly brackets are 3309 02:46:36,920 --> 02:46:41,240 mandatory so we have to add them 3310 02:46:42,520 --> 02:46:46,920 back now we're going to learn another 3311 02:46:44,640 --> 02:46:50,640 feature of if statements which is we can 3312 02:46:46,920 --> 02:46:52,840 have more than one condition for example 3313 02:46:50,640 --> 02:46:58,000 we're going to go to the end of this if 3314 02:46:52,840 --> 02:47:00,399 branch and type else if and then 3315 02:46:58,000 --> 02:47:03,520 brackets and curly 3316 02:47:00,399 --> 02:47:05,160 brackets else if lets us add another 3317 02:47:03,520 --> 02:47:08,000 Branch to our if 3318 02:47:05,160 --> 02:47:11,760 statement between these round brackets 3319 02:47:08,000 --> 02:47:13,920 we can add another condition for example 3320 02:47:11,760 --> 02:47:17,399 if a person is almost old enough to 3321 02:47:13,920 --> 02:47:19,479 drive let's display a different message 3322 02:47:17,399 --> 02:47:21,600 so inside this condition 3323 02:47:19,479 --> 02:47:27,279 let's check if this person's age which 3324 02:47:21,600 --> 02:47:29,240 is 15 is greater than or equal to 14 so 3325 02:47:27,279 --> 02:47:32,279 we're checking if they are almost old 3326 02:47:29,240 --> 02:47:34,319 enough to drive and now inside the curly 3327 02:47:32,279 --> 02:47:35,720 brackets we're going to display a 3328 02:47:34,319 --> 02:47:38,720 different 3329 02:47:35,720 --> 02:47:38,720 message 3330 02:47:40,279 --> 02:47:44,439 console.log almost 3331 02:47:44,520 --> 02:47:50,120 there so the way this works is that the 3332 02:47:47,120 --> 02:47:53,439 if statement checks the condition one by 3333 02:47:50,120 --> 02:47:56,240 one first it checks if this is true if 3334 02:47:53,439 --> 02:47:58,760 it is it will run this Branch then it 3335 02:47:56,240 --> 02:48:01,640 checks if this is true if it is it will 3336 02:47:58,760 --> 02:48:04,479 run this branch and if none of the 3337 02:48:01,640 --> 02:48:08,479 conditions are true it will run the else 3338 02:48:04,479 --> 02:48:10,600 Branch so here this condition is false 3339 02:48:08,479 --> 02:48:13,200 so we're not going to run this code but 3340 02:48:10,600 --> 02:48:16,319 now the second condition is true so 3341 02:48:13,200 --> 02:48:19,319 we're going to run this code if we 3342 02:48:16,319 --> 02:48:20,800 save we display almost there 3343 02:48:19,319 --> 02:48:24,359 in the 3344 02:48:20,800 --> 02:48:26,760 console so using else if branches we can 3345 02:48:24,359 --> 02:48:27,960 add as many conditions as we want to an 3346 02:48:26,760 --> 02:48:29,760 if 3347 02:48:27,960 --> 02:48:31,680 statement the next thing we're going to 3348 02:48:29,760 --> 02:48:33,760 learn is that we can combine if 3349 02:48:31,680 --> 02:48:34,920 statements with variables from the 3350 02:48:33,760 --> 02:48:38,080 previous 3351 02:48:34,920 --> 02:48:40,800 lesson for example here we wrote the 3352 02:48:38,080 --> 02:48:43,399 person's age two times so we can 3353 02:48:40,800 --> 02:48:45,080 actually save this age in a variable and 3354 02:48:43,399 --> 02:48:49,279 then reuse that 3355 02:48:45,080 --> 02:48:54,279 value so above the if statement 3356 02:48:49,279 --> 02:48:57,560 let's create a variable using const and 3357 02:48:54,279 --> 02:49:00,080 let's name this variable H and we'll 3358 02:48:57,560 --> 02:49:02,520 make it equal to 3359 02:49:00,080 --> 02:49:05,240 15 remember the best practice for 3360 02:49:02,520 --> 02:49:08,040 creating a variable is to use const by 3361 02:49:05,240 --> 02:49:10,240 default and only use let if we're going 3362 02:49:08,040 --> 02:49:13,000 to change this value 3363 02:49:10,240 --> 02:49:15,720 later and now that we saved this number 3364 02:49:13,000 --> 02:49:19,120 in a variable we can use the variable 3365 02:49:15,720 --> 02:49:25,359 inside the if statement so we'll replace 3366 02:49:19,120 --> 02:49:28,479 15 with age and here as well with 3367 02:49:25,359 --> 02:49:31,000 age so this will take whatever value is 3368 02:49:28,479 --> 02:49:32,120 inside the variable and substitute it 3369 02:49:31,000 --> 02:49:34,399 into the 3370 02:49:32,120 --> 02:49:37,319 code if we 3371 02:49:34,399 --> 02:49:40,120 save the if statement works the same way 3372 02:49:37,319 --> 02:49:43,000 as before and we run this 3373 02:49:40,120 --> 02:49:45,399 line okay now we're ready to use 3374 02:49:43,000 --> 02:49:47,840 booleans and if statements to do a 3375 02:49:45,399 --> 02:49:51,120 project we're going to build a rock 3376 02:49:47,840 --> 02:49:53,359 paper scissors gate G first let's take a 3377 02:49:51,120 --> 02:49:57,560 look at the project we're going to go to 3378 02:49:53,359 --> 02:50:02,520 our browser and at the top create a new 3379 02:49:57,560 --> 02:50:06,399 tab and we're going to type here 3380 02:50:02,520 --> 02:50:08,800 superimple dodev SL 3381 02:50:06,399 --> 02:50:11,279 projects SL 3382 02:50:08,800 --> 02:50:14,120 booleans and press 3383 02:50:11,279 --> 02:50:16,680 enter here we can see a simple version 3384 02:50:14,120 --> 02:50:19,560 of a rock paper scissors game and if we 3385 02:50:16,680 --> 02:50:22,680 click this link it will take us to the 3386 02:50:19,560 --> 02:50:24,720 final version of this game so in this 3387 02:50:22,680 --> 02:50:27,319 game we can pick a 3388 02:50:24,720 --> 02:50:30,319 move and then the computer will pick a 3389 02:50:27,319 --> 02:50:33,600 random move and it will show us the 3390 02:50:30,319 --> 02:50:37,359 result and we also have a score of how 3391 02:50:33,600 --> 02:50:39,319 many times we won lost and tied so we're 3392 02:50:37,359 --> 02:50:42,399 going to build this as we go through the 3393 02:50:39,319 --> 02:50:44,479 course but for now let's go 3394 02:50:42,399 --> 02:50:47,560 back and we're going to start by 3395 02:50:44,479 --> 02:50:49,840 building this simple version of the game 3396 02:50:47,560 --> 02:50:51,640 so how this simple version works is we 3397 02:50:49,840 --> 02:50:53,920 click one of these buttons to pick a 3398 02:50:51,640 --> 02:50:56,399 move so let's pick 3399 02:50:53,920 --> 02:50:59,279 Rock then the computer will randomly 3400 02:50:56,399 --> 02:51:01,640 pick a move in this case it's paper but 3401 02:50:59,279 --> 02:51:03,960 for you it might be different and it 3402 02:51:01,640 --> 02:51:05,680 will compare the two moves and display 3403 02:51:03,960 --> 02:51:07,960 the results in this 3404 02:51:05,680 --> 02:51:11,720 popup so the rules of rock paper 3405 02:51:07,960 --> 02:51:15,239 scissors are rock beats scissors paper 3406 02:51:11,720 --> 02:51:18,120 beats Rock and scissors beats 3407 02:51:15,239 --> 02:51:19,560 paper let's press okay and we'll get 3408 02:51:18,120 --> 02:51:22,720 started on this 3409 02:51:19,560 --> 02:51:25,359 project first we'll create a new HTML 3410 02:51:22,720 --> 02:51:28,000 file just for this project let's go to 3411 02:51:25,359 --> 02:51:30,920 our code editor and click here and 3412 02:51:28,000 --> 02:51:33,640 create a new file and we're going to 3413 02:51:30,920 --> 02:51:36,640 name this file 3414 02:51:33,640 --> 02:51:36,640 06- 3415 02:51:39,640 --> 02:51:46,840 rock-paper-scissors HTML and press 3416 02:51:43,560 --> 02:51:50,680 enter next let's copy all the code from 3417 02:51:46,840 --> 02:51:53,880 booleans HTML into this new file so 3418 02:51:50,680 --> 02:51:56,560 we'll open this again and then control a 3419 02:51:53,880 --> 02:51:58,880 or command a to select all the code and 3420 02:51:56,560 --> 02:52:03,680 then right click 3421 02:51:58,880 --> 02:52:07,560 copy and in here right click and 3422 02:52:03,680 --> 02:52:12,640 paste let's close this and prepare this 3423 02:52:07,560 --> 02:52:14,920 file so we'll change this to rock 3424 02:52:12,640 --> 02:52:17,120 paper 3425 02:52:14,920 --> 02:52:20,520 scissors and remove all of the 3426 02:52:17,120 --> 02:52:20,520 JavaScript code that we had 3427 02:52:22,239 --> 02:52:29,120 before finally let's save and open this 3428 02:52:26,040 --> 02:52:32,319 file in the browser by right clicking 3429 02:52:29,120 --> 02:52:35,920 and then open with live 3430 02:52:32,319 --> 02:52:38,120 server let's take a look at the project 3431 02:52:35,920 --> 02:52:40,760 again so we're going to start by 3432 02:52:38,120 --> 02:52:44,160 creating the text first because this 3433 02:52:40,760 --> 02:52:46,640 text is in the final version of the 3434 02:52:44,160 --> 02:52:49,399 project so in our 3435 02:52:46,640 --> 02:52:53,439 HTML we're going to create a paragraph 3436 02:52:49,399 --> 02:52:56,040 element for this text so less than P 3437 02:52:53,439 --> 02:52:59,040 greater than and the closing tag less 3438 02:52:56,040 --> 02:53:02,080 than /p greater than inside our 3439 02:52:59,040 --> 02:53:05,880 paragraph we're going to have the text 3440 02:53:02,080 --> 02:53:10,160 rock paper 3441 02:53:05,880 --> 02:53:12,600 scissors next let's create the 3442 02:53:10,160 --> 02:53:16,160 buttons so we'll go to our 3443 02:53:12,600 --> 02:53:18,760 HTML and create a 3444 02:53:16,160 --> 02:53:21,080 button and inside this first button 3445 02:53:18,760 --> 02:53:25,200 we'll have the text 3446 02:53:21,080 --> 02:53:28,520 Rock Let's create another 3447 02:53:25,200 --> 02:53:31,640 button this one will be 3448 02:53:28,520 --> 02:53:35,359 paper and we'll create the last 3449 02:53:31,640 --> 02:53:39,000 button this one will be 3450 02:53:35,359 --> 02:53:40,399 scissors now let's save and check our 3451 02:53:39,000 --> 02:53:43,279 new 3452 02:53:40,399 --> 02:53:44,359 tab and now we created all the elements 3453 02:53:43,279 --> 02:53:47,080 that we 3454 02:53:44,359 --> 02:53:49,920 need next let's make these elements 3455 02:53:47,080 --> 02:53:52,399 interactive with Javas JavaScript so 3456 02:53:49,920 --> 02:53:54,800 when writing JavaScript a good strategy 3457 02:53:52,399 --> 02:53:57,720 is to think about what steps we need to 3458 02:53:54,800 --> 02:54:01,359 do and then convert those steps into 3459 02:53:57,720 --> 02:54:03,520 code so here when we click a button the 3460 02:54:01,359 --> 02:54:05,840 first step is the computer will randomly 3461 02:54:03,520 --> 02:54:08,600 select a move and then we're going to 3462 02:54:05,840 --> 02:54:11,760 compare the moves to get the result and 3463 02:54:08,600 --> 02:54:14,960 finally we'll display the result in a 3464 02:54:11,760 --> 02:54:17,840 popup by the way this is called an 3465 02:54:14,960 --> 02:54:20,800 algorithm an algorithm is a set of steps 3466 02:54:17,840 --> 02:54:23,200 to complete delete a task or to solve a 3467 02:54:20,800 --> 02:54:25,239 problem now that we know what steps we 3468 02:54:23,200 --> 02:54:28,680 need to do we're going to convert these 3469 02:54:25,239 --> 02:54:30,960 steps or this algorithm into 3470 02:54:28,680 --> 02:54:33,200 code first we're going to run some 3471 02:54:30,960 --> 02:54:36,760 JavaScript when we click these buttons 3472 02:54:33,200 --> 02:54:38,800 so let's use the onclick attribute again 3473 02:54:36,760 --> 02:54:41,680 we'll go to the opening tag of the 3474 02:54:38,800 --> 02:54:44,200 button and type 3475 02:54:41,680 --> 02:54:46,359 onclick equals double 3476 02:54:44,200 --> 02:54:50,319 quotes and then inside these double 3477 02:54:46,359 --> 02:54:50,319 quotes we can add our JavaScript 3478 02:54:51,000 --> 02:54:56,080 so now we have to figure out how to 3479 02:54:52,800 --> 02:54:58,160 randomly select a move for the computer 3480 02:54:56,080 --> 02:54:59,760 to do this we need to learn a new piece 3481 02:54:58,160 --> 02:55:03,359 of JavaScript 3482 02:54:59,760 --> 02:55:05,319 code so here we're going to type capital 3483 02:55:03,359 --> 02:55:09,840 m 3484 02:55:05,319 --> 02:55:12,560 math.random Open Bracket close bracket 3485 02:55:09,840 --> 02:55:16,600 math.random generates a random number 3486 02:55:12,560 --> 02:55:20,319 between 0o and 1 so if we console.log 3487 02:55:16,600 --> 02:55:20,319 this console 3488 02:55:21,640 --> 02:55:27,439 log and 3489 02:55:24,560 --> 02:55:30,960 save and click this 3490 02:55:27,439 --> 02:55:33,120 button and then check in our console by 3491 02:55:30,960 --> 02:55:35,680 right-clicking 3492 02:55:33,120 --> 02:55:38,560 inspect and the 3493 02:55:35,680 --> 02:55:42,560 console it will display a random number 3494 02:55:38,560 --> 02:55:44,960 between Z and 1 if we click this button 3495 02:55:42,560 --> 02:55:48,640 again it will generate a different 3496 02:55:44,960 --> 02:55:51,200 random number so every time we run MTH 3497 02:55:48,640 --> 02:55:54,840 random it generates a different random 3498 02:55:51,200 --> 02:55:57,279 number between 0 and one now to be more 3499 02:55:54,840 --> 02:56:00,720 precise it actually generates a number 3500 02:55:57,279 --> 02:56:03,479 greater than equal to zero and less than 3501 02:56:00,720 --> 02:56:06,560 one now let's save this random number in 3502 02:56:03,479 --> 02:56:08,359 a variable so we can use it later so 3503 02:56:06,560 --> 02:56:10,479 we'll replace the 3504 02:56:08,359 --> 02:56:13,399 console.log with 3505 02:56:10,479 --> 02:56:19,080 const and then a variable name let's 3506 02:56:13,399 --> 02:56:21,080 just name it random capital N number 3507 02:56:19,080 --> 02:56:23,960 and then 3508 02:56:21,080 --> 02:56:25,080 equals so we saved this random number 3509 02:56:23,960 --> 02:56:28,239 inside this 3510 02:56:25,080 --> 02:56:31,760 variable next we need a way to convert 3511 02:56:28,239 --> 02:56:34,880 this random number into a move like rock 3512 02:56:31,760 --> 02:56:38,279 paper or scissors to do this let's 3513 02:56:34,880 --> 02:56:40,800 imagine the space between 0 and 1 and 3514 02:56:38,279 --> 02:56:44,600 then divide the space into three equal 3515 02:56:40,800 --> 02:56:46,600 parts each part represents a move if we 3516 02:56:44,600 --> 02:56:50,640 generate a random number and it's 3517 02:56:46,600 --> 02:56:53,880 between 0 and 1/3 the move will be Rock 3518 02:56:50,640 --> 02:56:56,760 if it's between 1/3 and 2/3 the move 3519 02:56:53,880 --> 02:57:00,319 will be paper and if it's between 2/3 3520 02:56:56,760 --> 02:57:02,680 and one the move will be scissors this 3521 02:57:00,319 --> 02:57:04,920 is how we can convert the random number 3522 02:57:02,680 --> 02:57:08,040 into a move and this will give the 3523 02:57:04,920 --> 02:57:09,359 computer an equal chance of picking rock 3524 02:57:08,040 --> 02:57:11,880 paper or 3525 02:57:09,359 --> 02:57:14,960 scissors now let's convert these steps 3526 02:57:11,880 --> 02:57:17,239 into code and an if statement is the 3527 02:57:14,960 --> 02:57:20,160 perfect solution for this this already 3528 02:57:17,239 --> 02:57:23,479 looks like an if statement so in our 3529 02:57:20,160 --> 02:57:28,399 onclick attribute let's add some new 3530 02:57:23,479 --> 02:57:31,720 lines and type if brackets and curly 3531 02:57:28,399 --> 02:57:33,640 brackets for this first condition we're 3532 02:57:31,720 --> 02:57:38,279 going to check if the random number is 3533 02:57:33,640 --> 02:57:42,439 between 0 and 1/3 so let's check if 3534 02:57:38,279 --> 02:57:43,640 random number is greater than or equal 3535 02:57:42,439 --> 02:57:46,680 to 3536 02:57:43,640 --> 02:57:53,319 zero but in this case we also need to 3537 02:57:46,680 --> 02:57:55,760 check that random number is less than 3538 02:57:53,319 --> 02:57:59,760 1/3 because we're checking if the number 3539 02:57:55,760 --> 02:58:01,960 is between 0 and 1/3 but how do we do 3540 02:57:59,760 --> 02:58:05,520 this how do we check that two 3541 02:58:01,960 --> 02:58:07,239 comparisons are both true to do this 3542 02:58:05,520 --> 02:58:10,560 we're going to learn another type of 3543 02:58:07,239 --> 02:58:13,439 operator in JavaScript called logical 3544 02:58:10,560 --> 02:58:15,960 operators logical operators let us 3545 02:58:13,439 --> 02:58:18,920 combine Boolean values which is what we 3546 02:58:15,960 --> 02:58:20,279 need to do here let's go back into 3547 02:58:18,920 --> 02:58:23,239 booleans 3548 02:58:20,279 --> 02:58:26,399 HTML and we're going to learn logical 3549 02:58:23,239 --> 02:58:27,560 operators so first let's open the tab 3550 02:58:26,399 --> 02:58:30,239 for this 3551 02:58:27,560 --> 02:58:32,760 file and we're going to comment out all 3552 02:58:30,239 --> 02:58:34,920 the previous code so our console doesn't 3553 02:58:32,760 --> 02:58:37,479 get too messy 3554 02:58:34,920 --> 02:58:39,640 slashstar and then at the 3555 02:58:37,479 --> 02:58:42,600 bottom star 3556 02:58:39,640 --> 02:58:45,319 slash now let's create some new 3557 02:58:42,600 --> 02:58:47,680 lines and the first logical operator 3558 02:58:45,319 --> 02:58:49,279 we're going to learn is called the and 3559 02:58:47,680 --> 02:58:51,200 operator 3560 02:58:49,279 --> 02:58:55,279 let's type 3561 02:58:51,200 --> 02:59:00,080 console. log a Boolean value 3562 02:58:55,279 --> 02:59:03,840 true and another Boolean value 3563 02:59:00,080 --> 02:59:06,800 true so this is the and operator and it 3564 02:59:03,840 --> 02:59:09,399 checks if two Boolean values are both 3565 02:59:06,800 --> 02:59:12,760 true so it checks if the left side is 3566 02:59:09,399 --> 02:59:16,200 true and the right side is true that's 3567 02:59:12,760 --> 02:59:18,520 why it's called the and operator so here 3568 02:59:16,200 --> 02:59:21,239 the left side is true and the right side 3569 02:59:18,520 --> 02:59:24,080 is also true so the and operator will 3570 02:59:21,239 --> 02:59:28,680 result in true if we 3571 02:59:24,080 --> 02:59:31,960 save this displays true now if one of 3572 02:59:28,680 --> 02:59:34,040 these sides is false for example if we 3573 02:59:31,960 --> 02:59:37,600 change this to 3574 02:59:34,040 --> 02:59:40,239 false now let's check is the left side 3575 02:59:37,600 --> 02:59:44,239 and the right side both true in this 3576 02:59:40,239 --> 02:59:49,439 case no so and will now result in false 3577 02:59:44,239 --> 02:59:52,200 if we save it now displays false 3578 02:59:49,439 --> 02:59:55,080 so that's the logical and operator it 3579 02:59:52,200 --> 02:59:58,520 checks if the left side is true and the 3580 02:59:55,080 --> 03:00:01,600 right side is also true so this is 3581 02:59:58,520 --> 03:00:07,880 exactly what we need for our project for 3582 03:00:01,600 --> 03:00:07,880 example let's create a new line and type 3583 03:00:08,279 --> 03:00:13,160 console.log and let's say that we 3584 03:00:10,160 --> 03:00:16,120 generated a random number like 3585 03:00:13,160 --> 03:00:19,880 0.2 and we want to check if it's between 3586 03:00:16,120 --> 03:00:25,239 0 and 1/3 so we can do 3587 03:00:19,880 --> 03:00:27,200 0.2 is greater than or equal to 0 and 3588 03:00:25,239 --> 03:00:30,960 we're also going to check if 3589 03:00:27,200 --> 03:00:33,840 0.2 is less than 3590 03:00:30,960 --> 03:00:36,040 1/3 so we're using the logical and 3591 03:00:33,840 --> 03:00:40,080 operator to check if the left side is 3592 03:00:36,040 --> 03:00:43,479 true and the right side is also true so 3593 03:00:40,080 --> 03:00:48,160 in this example both sides are true 0.2 3594 03:00:43,479 --> 03:00:51,560 is between 0 and 1/3 so if we save 3595 03:00:48,160 --> 03:00:54,479 this whole thing will display true now 3596 03:00:51,560 --> 03:00:57,479 in the order of operations logical 3597 03:00:54,479 --> 03:01:00,319 operators like and have a lower priority 3598 03:00:57,479 --> 03:01:03,760 than math and comparisons so in this 3599 03:01:00,319 --> 03:01:05,239 example it will calculate the math first 3600 03:01:03,760 --> 03:01:07,439 and then it will calculate the 3601 03:01:05,239 --> 03:01:09,960 comparison on the left and then the 3602 03:01:07,439 --> 03:01:12,680 comparison on the right and then at the 3603 03:01:09,960 --> 03:01:14,720 end the and operator checks if both 3604 03:01:12,680 --> 03:01:17,680 sides result in 3605 03:01:14,720 --> 03:01:20,560 true now let's use the and operator in 3606 03:01:17,680 --> 03:01:21,720 our project let's go back to the file 3607 03:01:20,560 --> 03:01:24,080 for our 3608 03:01:21,720 --> 03:01:26,120 project and then inside this first 3609 03:01:24,080 --> 03:01:28,319 condition we're going to check if the 3610 03:01:26,120 --> 03:01:32,359 random number is greater than or equal 3611 03:01:28,319 --> 03:01:35,960 to zero and at the same time is the 3612 03:01:32,359 --> 03:01:38,600 random number less than 1/3 so this is 3613 03:01:35,960 --> 03:01:39,560 how we check if a number is between 0 3614 03:01:38,600 --> 03:01:42,319 and 3615 03:01:39,560 --> 03:01:46,080 1/3 in this case the computer will pick 3616 03:01:42,319 --> 03:01:48,359 Rock so between the curly brackets let's 3617 03:01:46,080 --> 03:01:52,239 just type con 3618 03:01:48,359 --> 03:01:55,080 cons. log 3619 03:01:52,239 --> 03:01:57,880 Rock let's get some practice and create 3620 03:01:55,080 --> 03:02:00,200 the other two branches for picking paper 3621 03:01:57,880 --> 03:02:02,880 and scissors feel free to pause this 3622 03:02:00,200 --> 03:02:07,359 video if you want to try it yourself 3623 03:02:02,880 --> 03:02:10,000 first so here we're going to type else 3624 03:02:07,359 --> 03:02:13,439 if to add another branch and then 3625 03:02:10,000 --> 03:02:18,080 brackets and curly brackets and in this 3626 03:02:13,439 --> 03:02:23,200 condition we're going to check if random 3627 03:02:18,080 --> 03:02:24,960 number is greater than or equal to 3628 03:02:23,200 --> 03:02:26,640 1/3 3629 03:02:24,960 --> 03:02:32,800 and 3630 03:02:26,640 --> 03:02:32,800 random number is less than 3631 03:02:33,279 --> 03:02:40,479 2/3 so if this random number is between 3632 03:02:36,399 --> 03:02:44,880 1/3 and 2/3 inside here we're going to 3633 03:02:40,479 --> 03:02:47,840 console do log 3634 03:02:44,880 --> 03:02:51,319 paper and finally let's create another 3635 03:02:47,840 --> 03:02:54,120 Branch using else 3636 03:02:51,319 --> 03:02:57,960 if random 3637 03:02:54,120 --> 03:03:04,600 number greater than or equal to 3638 03:02:57,960 --> 03:03:08,080 2/3 and random number less than 3639 03:03:04,600 --> 03:03:08,080 1 and 3640 03:03:08,720 --> 03:03:11,720 console.log 3641 03:03:11,920 --> 03:03:16,560 scissors if we 3642 03:03:13,800 --> 03:03:18,359 save and then go back to our rock paper 3643 03:03:16,560 --> 03:03:21,600 scissors tab 3644 03:03:18,359 --> 03:03:24,359 and then click the rock button a few 3645 03:03:21,600 --> 03:03:25,120 times it will pick some random moves for 3646 03:03:24,359 --> 03:03:27,520 the 3647 03:03:25,120 --> 03:03:30,040 computer so that's how we generate the 3648 03:03:27,520 --> 03:03:32,520 computer's move one thing I want to 3649 03:03:30,040 --> 03:03:35,399 point out is that I use the less than 3650 03:03:32,520 --> 03:03:37,720 comparison instead of less than or equal 3651 03:03:35,399 --> 03:03:40,279 to I just did this so that the 3652 03:03:37,720 --> 03:03:42,439 comparisons don't 3653 03:03:40,279 --> 03:03:44,680 overlap before we continue in the 3654 03:03:42,439 --> 03:03:47,960 project we're going to learn two other 3655 03:03:44,680 --> 03:03:50,080 logical operators we can use let's go 3656 03:03:47,960 --> 03:03:52,960 back to booleans 3657 03:03:50,080 --> 03:03:55,000 HTML and let's also open the tab for 3658 03:03:52,960 --> 03:03:58,200 this 3659 03:03:55,000 --> 03:04:02,160 file the next logical operator is called 3660 03:03:58,200 --> 03:04:06,800 the or operator for example let's type 3661 03:04:02,160 --> 03:04:06,800 some new lines and type 3662 03:04:07,120 --> 03:04:15,920 console.log true or 3663 03:04:11,880 --> 03:04:19,399 false so this is the logical or operator 3664 03:04:15,920 --> 03:04:22,319 it checks if at least one side is true 3665 03:04:19,399 --> 03:04:24,319 so it checks if the left side is true or 3666 03:04:22,319 --> 03:04:26,319 the right side is true that's why it's 3667 03:04:24,319 --> 03:04:29,479 called the or 3668 03:04:26,319 --> 03:04:32,399 operator so here one of these sides is 3669 03:04:29,479 --> 03:04:34,359 true so the or operator will result in 3670 03:04:32,399 --> 03:04:37,359 true if we 3671 03:04:34,359 --> 03:04:40,840 save this displays 3672 03:04:37,359 --> 03:04:43,520 true if both sides are true it will also 3673 03:04:40,840 --> 03:04:44,560 result in true because at least one side 3674 03:04:43,520 --> 03:04:47,080 is 3675 03:04:44,560 --> 03:04:49,560 true the last logical operator we're 3676 03:04:47,080 --> 03:04:52,680 going to learn is called the not 3677 03:04:49,560 --> 03:04:55,200 operator for example let's create a new 3678 03:04:52,680 --> 03:04:59,960 line and type 3679 03:04:55,200 --> 03:05:03,080 console. log exclamation 3680 03:04:59,960 --> 03:05:06,680 true so the exclamation is called the 3681 03:05:03,080 --> 03:05:09,760 not operator the not operator only uses 3682 03:05:06,680 --> 03:05:13,000 one Boolean value and it flips it into 3683 03:05:09,760 --> 03:05:16,200 the opposite value so if this value is 3684 03:05:13,000 --> 03:05:18,680 true the not operator will flip it into 3685 03:05:16,200 --> 03:05:22,960 false so if we 3686 03:05:18,680 --> 03:05:25,080 save this will result in false now if 3687 03:05:22,960 --> 03:05:28,160 you think about it if we say that 3688 03:05:25,080 --> 03:05:30,720 something is not true that's the same 3689 03:05:28,160 --> 03:05:33,520 thing as saying that it is false so 3690 03:05:30,720 --> 03:05:38,279 that's why this is called the not 3691 03:05:33,520 --> 03:05:41,479 operator now if we change the value to 3692 03:05:38,279 --> 03:05:44,520 false again the not operator will flip 3693 03:05:41,479 --> 03:05:48,720 this into true so if we 3694 03:05:44,520 --> 03:05:51,960 save this will now result in true 3695 03:05:48,720 --> 03:05:54,160 so those are the logical or and not 3696 03:05:51,960 --> 03:05:56,239 operators we don't need to use these in 3697 03:05:54,160 --> 03:05:58,279 our project right now but I'll leave 3698 03:05:56,239 --> 03:06:01,239 some exercises for these at the end of 3699 03:05:58,279 --> 03:06:05,200 this lesson now let's go back and 3700 03:06:01,239 --> 03:06:07,920 continue the project let's open the code 3701 03:06:05,200 --> 03:06:11,200 and open the 3702 03:06:07,920 --> 03:06:14,080 tab the next step is to compare the 3703 03:06:11,200 --> 03:06:16,960 computer's move to our move which is 3704 03:06:14,080 --> 03:06:19,439 Rock so first let's save the computer's 3705 03:06:16,960 --> 03:06:22,040 move in a variable so we can compare it 3706 03:06:19,439 --> 03:06:23,880 later so instead of 3707 03:06:22,040 --> 03:06:25,760 console.log we're going to create a 3708 03:06:23,880 --> 03:06:27,279 variable with 3709 03:06:25,760 --> 03:06:31,399 const 3710 03:06:27,279 --> 03:06:33,920 computer capital M move and make it 3711 03:06:31,399 --> 03:06:36,760 equal to the string 3712 03:06:33,920 --> 03:06:43,040 Rock and we'll do the same thing 3713 03:06:36,760 --> 03:06:45,319 here const computer move equals 3714 03:06:43,040 --> 03:06:52,000 paper and 3715 03:06:45,319 --> 03:06:52,000 here const comp futter move equals 3716 03:06:52,640 --> 03:07:00,319 scissors and now at the bottom let's 3717 03:06:57,160 --> 03:07:02,239 console.log the computer move variable 3718 03:07:00,319 --> 03:07:05,840 to make sure that it 3719 03:07:02,239 --> 03:07:07,080 worked if we save and then click our 3720 03:07:05,840 --> 03:07:09,680 Rock 3721 03:07:07,080 --> 03:07:12,239 button unfortunately it gives us an 3722 03:07:09,680 --> 03:07:15,279 error telling us that computer move is 3723 03:07:12,239 --> 03:07:19,040 not defined even though we created it 3724 03:07:15,279 --> 03:07:21,560 right here so what is happening here if 3725 03:07:19,040 --> 03:07:25,600 statements have a special feature they 3726 03:07:21,560 --> 03:07:29,720 create something called a scope a scope 3727 03:07:25,600 --> 03:07:31,800 limits where a variable exists basically 3728 03:07:29,720 --> 03:07:35,160 whenever we have these curly brackets 3729 03:07:31,800 --> 03:07:37,800 like this any variable we create inside 3730 03:07:35,160 --> 03:07:40,680 the curly brackets will only exist 3731 03:07:37,800 --> 03:07:42,920 inside the curly brackets and we can't 3732 03:07:40,680 --> 03:07:46,479 use these variables outside the curly 3733 03:07:42,920 --> 03:07:48,880 brackets so this is called a scope 3734 03:07:46,479 --> 03:07:51,680 Scopes are a feature of many programming 3735 03:07:48,880 --> 03:07:55,120 languages and they help us avoid naming 3736 03:07:51,680 --> 03:08:00,279 conflicts for example if I create a 3737 03:07:55,120 --> 03:08:01,720 variable here called random number 3738 03:08:00,279 --> 03:08:05,479 equals 3739 03:08:01,720 --> 03:08:07,359 0.5 and this didn't create a scope so if 3740 03:08:05,479 --> 03:08:10,000 I delete these curly 3741 03:08:07,359 --> 03:08:12,960 brackets this variable name will now 3742 03:08:10,000 --> 03:08:14,920 conflict with the variable name up here 3743 03:08:12,960 --> 03:08:17,800 because we can't have two variables with 3744 03:08:14,920 --> 03:08:20,160 the same name so you can imagine that if 3745 03:08:17,800 --> 03:08:22,359 we didn't have scope and we had hundreds 3746 03:08:20,160 --> 03:08:23,920 or even thousands of lines of code we 3747 03:08:22,359 --> 03:08:26,840 would quickly start to run out of 3748 03:08:23,920 --> 03:08:30,239 variable names so by creating a new 3749 03:08:26,840 --> 03:08:33,080 scope here all the variable names inside 3750 03:08:30,239 --> 03:08:35,479 the scope only exist between the curly 3751 03:08:33,080 --> 03:08:39,399 brackets and it won't affect anything 3752 03:08:35,479 --> 03:08:41,640 outside in the code so Scopes help us 3753 03:08:39,399 --> 03:08:44,239 avoid naming 3754 03:08:41,640 --> 03:08:46,920 conflicts okay so I'll delete this 3755 03:08:44,239 --> 03:08:48,080 example and we'll learn how to access 3756 03:08:46,920 --> 03:08:51,840 this variable 3757 03:08:48,080 --> 03:08:54,560 outside of this scope so to access this 3758 03:08:51,840 --> 03:08:57,160 variable outside of the if statement we 3759 03:08:54,560 --> 03:09:00,560 have to create this variable outside of 3760 03:08:57,160 --> 03:09:03,479 the if statement so above 3761 03:09:00,560 --> 03:09:05,680 here we can create this 3762 03:09:03,479 --> 03:09:07,479 variable 3763 03:09:05,680 --> 03:09:10,600 const 3764 03:09:07,479 --> 03:09:13,239 computer move and let's just make it 3765 03:09:10,600 --> 03:09:16,680 equal to the empty string for 3766 03:09:13,239 --> 03:09:19,479 now so this variable was not created 3767 03:09:16,680 --> 03:09:22,279 inside any curly brackets so it's 3768 03:09:19,479 --> 03:09:25,640 accessible from all of this code inside 3769 03:09:22,279 --> 03:09:27,000 the onclick attribute including inside 3770 03:09:25,640 --> 03:09:29,160 the if 3771 03:09:27,000 --> 03:09:31,640 statement and then inside the if 3772 03:09:29,160 --> 03:09:33,800 statement instead of creating a new 3773 03:09:31,640 --> 03:09:36,800 variable we're just going to save the 3774 03:09:33,800 --> 03:09:38,960 move inside this variable up here 3775 03:09:36,800 --> 03:09:40,560 because we can access this variable 3776 03:09:38,960 --> 03:09:43,640 anywhere else in the 3777 03:09:40,560 --> 03:09:46,800 code so instead of using const we're 3778 03:09:43,640 --> 03:09:49,800 just going to reassign computer move and 3779 03:09:46,800 --> 03:09:51,040 we're going to move it here as well and 3780 03:09:49,800 --> 03:09:54,000 here as 3781 03:09:51,040 --> 03:09:59,040 well and remember when we reassign a 3782 03:09:54,000 --> 03:10:01,520 variable we have to switch from const to 3783 03:09:59,040 --> 03:10:04,560 let and now if we 3784 03:10:01,520 --> 03:10:07,640 save and then click the rock 3785 03:10:04,560 --> 03:10:10,760 button we're able to console.log 3786 03:10:07,640 --> 03:10:14,399 computer move because computer move is 3787 03:10:10,760 --> 03:10:17,960 no longer inside this scope it's up here 3788 03:10:14,399 --> 03:10:20,800 so we can access it in this line of code 3789 03:10:17,960 --> 03:10:23,560 so that's how scope Works any variable 3790 03:10:20,800 --> 03:10:26,960 that we create inside the curly brackets 3791 03:10:23,560 --> 03:10:29,239 will only exist inside the curly 3792 03:10:26,960 --> 03:10:31,120 brackets so remember in the previous 3793 03:10:29,239 --> 03:10:34,239 lesson we learned a third way of 3794 03:10:31,120 --> 03:10:35,960 creating variables called VAR the issue 3795 03:10:34,239 --> 03:10:39,600 with VAR is that it doesn't really 3796 03:10:35,960 --> 03:10:43,040 follow the rules of scope for example 3797 03:10:39,600 --> 03:10:45,720 here if we create a variable with VAR 3798 03:10:43,040 --> 03:10:48,000 like VAR and 3799 03:10:45,720 --> 03:10:50,239 random number 3800 03:10:48,000 --> 03:10:53,800 equals 3801 03:10:50,239 --> 03:10:56,760 0.5 this variable will conflict with the 3802 03:10:53,800 --> 03:10:59,760 variable up here with the same name even 3803 03:10:56,760 --> 03:11:02,200 though it's inside a scope so that's why 3804 03:10:59,760 --> 03:11:04,960 we don't use VAR anymore and we just use 3805 03:11:02,200 --> 03:11:08,080 const and let because of VAR doesn't 3806 03:11:04,960 --> 03:11:10,760 really follow the rules of scope so we 3807 03:11:08,080 --> 03:11:13,120 can remove this for now and move on to 3808 03:11:10,760 --> 03:11:13,120 the next 3809 03:11:13,520 --> 03:11:18,200 step so we generated a random move for 3810 03:11:16,760 --> 03:11:20,319 the computer 3811 03:11:18,200 --> 03:11:23,000 now we have to compare our move which is 3812 03:11:20,319 --> 03:11:26,439 Rock to the computer's move to get the 3813 03:11:23,000 --> 03:11:29,359 result and see who wins to do that we'll 3814 03:11:26,439 --> 03:11:33,279 use another if statement so at the 3815 03:11:29,359 --> 03:11:35,279 bottom here let's type if and then 3816 03:11:33,279 --> 03:11:38,760 brackets and curly 3817 03:11:35,279 --> 03:11:41,600 brackets and then inside this condition 3818 03:11:38,760 --> 03:11:48,120 let's check if the 3819 03:11:41,600 --> 03:11:50,560 computer move is equal to rock 3820 03:11:48,120 --> 03:11:53,000 so remember in JavaScript we always use 3821 03:11:50,560 --> 03:11:54,920 triple equals instead of double equals 3822 03:11:53,000 --> 03:11:57,439 to avoid the conversion 3823 03:11:54,920 --> 03:12:01,040 behavior and now if the computer's move 3824 03:11:57,439 --> 03:12:03,840 is rock and our move is Rock then inside 3825 03:12:01,040 --> 03:12:06,200 the curly brackets the result is going 3826 03:12:03,840 --> 03:12:08,680 to be a 3827 03:12:06,200 --> 03:12:12,160 tie and let's save the string in a 3828 03:12:08,680 --> 03:12:14,760 variable so we can use it later again we 3829 03:12:12,160 --> 03:12:16,800 have the same situation as before we 3830 03:12:14,760 --> 03:12:20,000 don't want to create the variable inside 3831 03:12:16,800 --> 03:12:23,200 here because then it only exists in this 3832 03:12:20,000 --> 03:12:25,680 scope between the curly brackets instead 3833 03:12:23,200 --> 03:12:28,560 we're going to create the variable above 3834 03:12:25,680 --> 03:12:31,120 outside the if statement so we'll use 3835 03:12:28,560 --> 03:12:33,200 let to create this variable because 3836 03:12:31,120 --> 03:12:36,479 we're going to reassign it in the if 3837 03:12:33,200 --> 03:12:39,279 statement let's name this variable 3838 03:12:36,479 --> 03:12:42,840 result and just make it equal to the 3839 03:12:39,279 --> 03:12:46,560 empty string at the top and then in the 3840 03:12:42,840 --> 03:12:48,200 if statement we'll save the result into 3841 03:12:46,560 --> 03:12:49,279 this variable 3842 03:12:48,200 --> 03:12:53,200 so 3843 03:12:49,279 --> 03:12:56,120 result equals 3844 03:12:53,200 --> 03:13:00,479 tie now let's create the other branches 3845 03:12:56,120 --> 03:13:04,479 of this if statement so here we'll add 3846 03:13:00,479 --> 03:13:07,840 else if brackets and curly 3847 03:13:04,479 --> 03:13:12,600 brackets and we'll check if the 3848 03:13:07,840 --> 03:13:16,120 computer move is equal to paper this 3849 03:13:12,600 --> 03:13:17,920 time if the computer move is paper and 3850 03:13:16,120 --> 03:13:22,279 we picked Rock 3851 03:13:17,920 --> 03:13:25,080 then we lose so the result is equal to 3852 03:13:22,279 --> 03:13:29,359 you 3853 03:13:25,080 --> 03:13:33,239 lose and finally we'll add another else 3854 03:13:29,359 --> 03:13:35,120 if brackets and curly brackets and we'll 3855 03:13:33,239 --> 03:13:40,760 check if the 3856 03:13:35,120 --> 03:13:43,080 computer move is equal to 3857 03:13:40,760 --> 03:13:47,200 scissors and if the computer picked 3858 03:13:43,080 --> 03:13:51,359 scissors and we pick Rock the result is 3859 03:13:47,200 --> 03:13:54,600 is that you 3860 03:13:51,359 --> 03:13:57,800 win so that's how we compare our move to 3861 03:13:54,600 --> 03:14:00,319 the computer's random move using an if 3862 03:13:57,800 --> 03:14:03,200 statement the last step is to display 3863 03:14:00,319 --> 03:14:06,680 this result in the 3864 03:14:03,200 --> 03:14:11,680 popup so at the bottom let's create a 3865 03:14:06,680 --> 03:14:14,040 popup using alert Open Bracket close 3866 03:14:11,680 --> 03:14:16,399 bracket and let's go back to super 3867 03:14:14,040 --> 03:14:20,000 simple Dev to see the message in the 3868 03:14:16,399 --> 03:14:22,800 popup so we'll click this and in the 3869 03:14:20,000 --> 03:14:25,640 popup it shows our move the computer's 3870 03:14:22,800 --> 03:14:27,840 move and then the result so we can 3871 03:14:25,640 --> 03:14:31,040 actually just copy 3872 03:14:27,840 --> 03:14:36,120 this rightclick 3873 03:14:31,040 --> 03:14:39,000 copy and then in here right click and 3874 03:14:36,120 --> 03:14:41,359 paste so obviously the computer is not 3875 03:14:39,000 --> 03:14:43,040 going to pick this move every time and 3876 03:14:41,359 --> 03:14:46,319 this is not going to be the same result 3877 03:14:43,040 --> 03:14:49,120 every time instead we want to insert 3878 03:14:46,319 --> 03:14:52,399 computer move into here and we want to 3879 03:14:49,120 --> 03:14:54,800 insert result into here so because we 3880 03:14:52,399 --> 03:14:57,319 want to insert values we're going to 3881 03:14:54,800 --> 03:14:59,600 turn this into a template string using 3882 03:14:57,319 --> 03:14:59,600 back 3883 03:14:59,840 --> 03:15:05,520 ticks and now instead of paper we're 3884 03:15:03,040 --> 03:15:08,439 going to insert a value using dollar and 3885 03:15:05,520 --> 03:15:10,359 curly brackets and we're going to insert 3886 03:15:08,439 --> 03:15:12,319 the variable 3887 03:15:10,359 --> 03:15:16,560 computer 3888 03:15:12,319 --> 03:15:17,960 move and instead of this text every time 3889 03:15:16,560 --> 03:15:21,880 we're going to 3890 03:15:17,960 --> 03:15:24,800 insert the variable 3891 03:15:21,880 --> 03:15:28,399 result and now if we 3892 03:15:24,800 --> 03:15:31,399 save and we can click okay to close this 3893 03:15:28,399 --> 03:15:32,359 and go back to our Tab and click the 3894 03:15:31,399 --> 03:15:34,960 rock 3895 03:15:32,359 --> 03:15:37,160 button we're going to pick Rock the 3896 03:15:34,960 --> 03:15:39,479 computer will pick a random move and it 3897 03:15:37,160 --> 03:15:41,840 will show us the result in a 3898 03:15:39,479 --> 03:15:45,279 popup so that's how we create this 3899 03:15:41,840 --> 03:15:48,840 simple version of rock paper scissors in 3900 03:15:45,279 --> 03:15:51,800 our code we did things step by step 3901 03:15:48,840 --> 03:15:54,279 first we generated a random number we 3902 03:15:51,800 --> 03:15:57,319 converted it into a move for the 3903 03:15:54,279 --> 03:15:59,760 computer and then we compared our move 3904 03:15:57,319 --> 03:16:01,600 to the computer move and we displayed 3905 03:15:59,760 --> 03:16:04,120 the result in a 3906 03:16:01,600 --> 03:16:06,640 popup so as you can see when we're 3907 03:16:04,120 --> 03:16:09,399 writing JavaScript we first figure out 3908 03:16:06,640 --> 03:16:12,199 what steps we need to do this is called 3909 03:16:09,399 --> 03:16:15,560 an algorithm and then we convert these 3910 03:16:12,199 --> 03:16:18,279 steps or this algorithm into 3911 03:16:15,560 --> 03:16:20,080 code so now now we can actually remove 3912 03:16:18,279 --> 03:16:22,279 this console.log because we don't need 3913 03:16:20,080 --> 03:16:22,279 it 3914 03:16:22,439 --> 03:16:28,160 anymore and the last step is to make the 3915 03:16:25,319 --> 03:16:29,800 other buttons here interactive so feel 3916 03:16:28,160 --> 03:16:31,439 free to pause this video if you want to 3917 03:16:29,800 --> 03:16:35,120 try it yourself 3918 03:16:31,439 --> 03:16:36,960 first for the paper button let's add 3919 03:16:35,120 --> 03:16:41,120 some new lines to make it a little 3920 03:16:36,960 --> 03:16:43,520 easier to read and again we'll add the 3921 03:16:41,120 --> 03:16:46,520 onclick 3922 03:16:43,520 --> 03:16:48,439 attribute and then inside here we can 3923 03:16:46,520 --> 03:16:51,560 actually make a copy of all the 3924 03:16:48,439 --> 03:16:53,199 JavaScript we wrote so we can select all 3925 03:16:51,560 --> 03:16:57,600 of 3926 03:16:53,199 --> 03:17:03,920 this and then write click 3927 03:16:57,600 --> 03:17:03,920 copy and then in here right click and 3928 03:17:04,760 --> 03:17:10,920 paste if the formatting doesn't look 3929 03:17:07,080 --> 03:17:15,479 right we can select all of these 3930 03:17:10,920 --> 03:17:18,840 lines and press tab to add indents until 3931 03:17:15,479 --> 03:17:18,840 the formatting looks good 3932 03:17:19,600 --> 03:17:24,439 the one thing we have to change here is 3933 03:17:21,479 --> 03:17:26,880 that now we picked paper so the result 3934 03:17:24,439 --> 03:17:30,760 is going to be different if the computer 3935 03:17:26,880 --> 03:17:32,520 picks Rock and we pick paper that means 3936 03:17:30,760 --> 03:17:36,800 you 3937 03:17:32,520 --> 03:17:40,720 win if the computer picks paper now it 3938 03:17:36,800 --> 03:17:45,040 means that we tie and if the computer 3939 03:17:40,720 --> 03:17:48,000 picks scissors that means you 3940 03:17:45,040 --> 03:17:51,160 lose and here what will also change up 3941 03:17:48,000 --> 03:17:54,560 pick Rock to 3942 03:17:51,160 --> 03:17:54,560 paper and that's 3943 03:17:55,680 --> 03:18:02,359 it the last step is to make the scissors 3944 03:17:58,399 --> 03:18:05,439 button interactive so again 3945 03:18:02,359 --> 03:18:07,840 onclick equals double 3946 03:18:05,439 --> 03:18:10,960 quotes and then we'll make a copy of the 3947 03:18:07,840 --> 03:18:13,520 code so I already have it so just write 3948 03:18:10,960 --> 03:18:16,560 click and 3949 03:18:13,520 --> 03:18:19,160 paste and then again select the code 3950 03:18:16,560 --> 03:18:21,239 that we need to fix the formatting for 3951 03:18:19,160 --> 03:18:25,000 and press 3952 03:18:21,239 --> 03:18:28,520 Tab and here we pick scissors so if the 3953 03:18:25,000 --> 03:18:32,880 computer picks Rock then the result is 3954 03:18:28,520 --> 03:18:36,880 you lose if the computer picks paper the 3955 03:18:32,880 --> 03:18:41,319 result is you win and if the computer 3956 03:18:36,880 --> 03:18:45,479 also picks scissors the result is a 3957 03:18:41,319 --> 03:18:47,640 tie and we'll change you pick Rock to 3958 03:18:45,479 --> 03:18:51,640 scissors 3959 03:18:47,640 --> 03:18:54,800 now if we save and try out our game if 3960 03:18:51,640 --> 03:18:56,920 we click Rock we're going to pick Rock 3961 03:18:54,800 --> 03:19:00,279 computer picks a random move and it will 3962 03:18:56,920 --> 03:19:01,960 show us the result here let's press okay 3963 03:19:00,279 --> 03:19:05,239 and try 3964 03:19:01,960 --> 03:19:06,120 paper this also works correctly let's 3965 03:19:05,239 --> 03:19:08,520 press 3966 03:19:06,120 --> 03:19:11,920 okay and click 3967 03:19:08,520 --> 03:19:14,560 scissors and this also works 3968 03:19:11,920 --> 03:19:16,720 correctly and that's it we just finished 3969 03:19:14,560 --> 03:19:18,560 creating a simple version of this rock 3970 03:19:16,720 --> 03:19:21,239 rock paper scissors 3971 03:19:18,560 --> 03:19:22,920 game in the rest of this lesson we're 3972 03:19:21,239 --> 03:19:24,800 going to learn more details about 3973 03:19:22,920 --> 03:19:28,359 booleans and if 3974 03:19:24,800 --> 03:19:31,600 statements let's go back to booleans 3975 03:19:28,359 --> 03:19:34,319 HTML and open the tab for this 3976 03:19:31,600 --> 03:19:37,319 file and let's comment out the previous 3977 03:19:34,319 --> 03:19:41,399 code using 3978 03:19:37,319 --> 03:19:43,080 slashstar and star slash and create some 3979 03:19:41,399 --> 03:19:44,880 new 3980 03:19:43,080 --> 03:19:48,160 lines the next thing we're going to 3981 03:19:44,880 --> 03:19:51,199 learn is called truthy and and falsy 3982 03:19:48,160 --> 03:19:55,319 values so if statements don't just work 3983 03:19:51,199 --> 03:19:57,319 with Boolean values like true and false 3984 03:19:55,319 --> 03:20:01,160 they actually work with any type of 3985 03:19:57,319 --> 03:20:06,640 value including numbers and strings for 3986 03:20:01,160 --> 03:20:09,880 example let's type if brackets and curly 3987 03:20:06,640 --> 03:20:12,359 brackets and inside this condition 3988 03:20:09,880 --> 03:20:16,319 instead of putting a Boolean value we're 3989 03:20:12,359 --> 03:20:21,199 going to put a number like five and then 3990 03:20:16,319 --> 03:20:24,880 in inside the curly brackets let's 3991 03:20:21,199 --> 03:20:27,000 console.log the string 3992 03:20:24,880 --> 03:20:30,120 truthy if we 3993 03:20:27,000 --> 03:20:33,199 save the number five behaves just like 3994 03:20:30,120 --> 03:20:37,040 true it causes the if statement to run 3995 03:20:33,199 --> 03:20:40,840 this code so we call this value a truthy 3996 03:20:37,040 --> 03:20:43,239 value because it behaves just like true 3997 03:20:40,840 --> 03:20:48,319 now let's change this value to a 3998 03:20:43,239 --> 03:20:51,199 different number zero if we say 3999 03:20:48,319 --> 03:20:53,640 the number zero behaves just like false 4000 03:20:51,199 --> 03:20:57,120 it causes the if statement to not run 4001 03:20:53,640 --> 03:21:00,760 this code we call this value zero a 4002 03:20:57,120 --> 03:21:03,720 falsy value because it behaves just like 4003 03:21:00,760 --> 03:21:07,840 false so how do we know if a value is 4004 03:21:03,720 --> 03:21:11,399 truthy or falsy in JavaScript the list 4005 03:21:07,840 --> 03:21:16,199 of falsy values are false the number 4006 03:21:11,399 --> 03:21:17,760 zero the empty string Nan undefined and 4007 03:21:16,199 --> 03:21:20,359 null 4008 03:21:17,760 --> 03:21:21,960 any value that is not on this list is a 4009 03:21:20,359 --> 03:21:25,880 truthy 4010 03:21:21,960 --> 03:21:28,680 value so why do we use truthy and falsy 4011 03:21:25,880 --> 03:21:32,680 values one way we use them is like a 4012 03:21:28,680 --> 03:21:36,279 shortcut in our code for example let's 4013 03:21:32,680 --> 03:21:40,279 create a variable at the bottom using 4014 03:21:36,279 --> 03:21:44,199 const and let's call it cart 4015 03:21:40,279 --> 03:21:48,520 quantity and make it equal to 4016 03:21:44,199 --> 03:21:52,359 five next let's create an if statement 4017 03:21:48,520 --> 03:21:57,439 if brackets and curly brackets and 4018 03:21:52,359 --> 03:22:03,239 inside the curly brackets let's type 4019 03:21:57,439 --> 03:22:03,239 console.log the string cart has 4020 03:22:03,560 --> 03:22:09,199 products now let's say that we only want 4021 03:22:06,000 --> 03:22:12,600 to display this message if our cart has 4022 03:22:09,199 --> 03:22:17,080 products usually inside this condition 4023 03:22:12,600 --> 03:22:20,800 we would check if cart quantity 4024 03:22:17,080 --> 03:22:23,040 is greater than zero but now instead of 4025 03:22:20,800 --> 03:22:26,239 doing this comparison we can actually 4026 03:22:23,040 --> 03:22:29,560 just type cart quantity and it will 4027 03:22:26,239 --> 03:22:32,760 behave the same way if the card quantity 4028 03:22:29,560 --> 03:22:36,160 is not zero they will both behave like 4029 03:22:32,760 --> 03:22:39,920 true if the card quantity is zero they 4030 03:22:36,160 --> 03:22:42,920 will both behave like false so here the 4031 03:22:39,920 --> 03:22:45,000 card quantity is five which is a truthy 4032 03:22:42,920 --> 03:22:48,279 value if we 4033 03:22:45,000 --> 03:22:51,199 save this caus the if statement to run 4034 03:22:48,279 --> 03:22:54,080 this code as you can see instead of 4035 03:22:51,199 --> 03:22:56,640 doing comparisons we can use truthy and 4036 03:22:54,080 --> 03:22:59,560 falsey values as 4037 03:22:56,640 --> 03:23:03,720 shortcuts truthy and falsy values also 4038 03:22:59,560 --> 03:23:07,199 work with logical operators for example 4039 03:23:03,720 --> 03:23:07,199 if we 4040 03:23:07,600 --> 03:23:11,680 console.log the not operator and the 4041 03:23:10,479 --> 03:23:15,520 number 4042 03:23:11,680 --> 03:23:18,560 zero zero is a falsy value it behaves 4043 03:23:15,520 --> 03:23:21,560 just like false so the not operator will 4044 03:23:18,560 --> 03:23:24,439 flip it into true if we 4045 03:23:21,560 --> 03:23:27,439 save this will display 4046 03:23:24,439 --> 03:23:30,359 true now that we understand truthy and 4047 03:23:27,439 --> 03:23:32,359 falsy values let's go over the list of 4048 03:23:30,359 --> 03:23:34,840 falsy values because there are some that 4049 03:23:32,359 --> 03:23:39,120 we haven't learned yet we already 4050 03:23:34,840 --> 03:23:42,439 learned false zero and the empty string 4051 03:23:39,120 --> 03:23:43,680 the next falsy value is Nan which means 4052 03:23:42,439 --> 03:23:47,160 not a 4053 03:23:43,680 --> 03:23:51,520 number we get Nan if we do some invalid 4054 03:23:47,160 --> 03:23:54,439 math for example at the bottom let's 4055 03:23:51,520 --> 03:23:57,160 type 4056 03:23:54,439 --> 03:24:00,560 console.log the string 4057 03:23:57,160 --> 03:24:04,880 text divided by the number 4058 03:24:00,560 --> 03:24:08,199 five so obviously this is not valid math 4059 03:24:04,880 --> 03:24:11,120 if we save JavaScript will tell us that 4060 03:24:08,199 --> 03:24:12,760 the result of this calculation is not a 4061 03:24:11,120 --> 03:24:16,920 number or 4062 03:24:12,760 --> 03:24:18,960 Nan the next falsy value is undefined 4063 03:24:16,920 --> 03:24:22,760 undefined represents that something 4064 03:24:18,960 --> 03:24:26,199 doesn't have a value for example at the 4065 03:24:22,760 --> 03:24:30,279 bottom if we create a variable with 4066 03:24:26,199 --> 03:24:33,199 let variable one and we don't give it a 4067 03:24:30,279 --> 03:24:36,800 value and then we 4068 03:24:33,199 --> 03:24:36,800 console. log this 4069 03:24:37,160 --> 03:24:42,680 variable and we 4070 03:24:39,479 --> 03:24:45,720 save it will show us that variable one 4071 03:24:42,680 --> 03:24:48,520 contains the value undefined so 4072 03:24:45,720 --> 03:24:50,359 undefined represents that this variable 4073 03:24:48,520 --> 03:24:53,239 doesn't have a 4074 03:24:50,359 --> 03:24:55,640 value one thing to note with undefined 4075 03:24:53,239 --> 03:24:58,920 is that we can only use this syntax with 4076 03:24:55,640 --> 03:25:01,479 let we can't use it with 4077 03:24:58,920 --> 03:25:05,920 const if we really want to use this with 4078 03:25:01,479 --> 03:25:08,000 const we have to type out equals 4079 03:25:05,920 --> 03:25:11,560 undefined 4080 03:25:08,000 --> 03:25:13,479 directly the last faly value is null 4081 03:25:11,560 --> 03:25:15,279 we'll need some extra knowledge to 4082 03:25:13,479 --> 03:25:17,840 understand null so we'll learn about 4083 03:25:15,279 --> 03:25:19,680 null later in in this 4084 03:25:17,840 --> 03:25:22,160 course the last thing we're going to 4085 03:25:19,680 --> 03:25:24,960 learn in this lesson are some shortcuts 4086 03:25:22,160 --> 03:25:27,680 for if statements called the Turner 4087 03:25:24,960 --> 03:25:29,120 operator the guard operator and the 4088 03:25:27,680 --> 03:25:31,840 default 4089 03:25:29,120 --> 03:25:33,960 operator first let's comment out this 4090 03:25:31,840 --> 03:25:38,319 code so our console doesn't get too 4091 03:25:33,960 --> 03:25:40,000 messy SL star and then at the bottom 4092 03:25:38,319 --> 03:25:43,199 star 4093 03:25:40,000 --> 03:25:46,680 slash and let's scroll down and we'll 4094 03:25:43,199 --> 03:25:49,840 start by learning the Turner operator 4095 03:25:46,680 --> 03:25:53,960 we're going to type a Boolean value like 4096 03:25:49,840 --> 03:25:56,439 true and question mark another value for 4097 03:25:53,960 --> 03:26:00,560 example the string 4098 03:25:56,439 --> 03:26:02,840 truthy and then colon and another value 4099 03:26:00,560 --> 03:26:06,760 like the string 4100 03:26:02,840 --> 03:26:10,080 falsy so this is the Turner operator 4101 03:26:06,760 --> 03:26:13,160 this is similar to an if else statement 4102 03:26:10,080 --> 03:26:15,920 the first value is like the condition if 4103 03:26:13,160 --> 03:26:18,760 it's truthy the result of this code is a 4104 03:26:15,920 --> 03:26:21,640 value after the question mark if the 4105 03:26:18,760 --> 03:26:25,239 first value is falsy the result of this 4106 03:26:21,640 --> 03:26:27,760 code is the value after the colon so you 4107 03:26:25,239 --> 03:26:31,880 can think of the question mark as the if 4108 03:26:27,760 --> 03:26:34,199 branch and the colon as the else Branch 4109 03:26:31,880 --> 03:26:36,640 this is sort of like a shortcut for an 4110 03:26:34,199 --> 03:26:39,399 if statement like 4111 03:26:36,640 --> 03:26:41,960 this now an advantage of the Turner 4112 03:26:39,399 --> 03:26:45,520 operator over an if statement is that we 4113 03:26:41,960 --> 03:26:46,960 can save a tary operator in a variable 4114 03:26:45,520 --> 03:26:48,800 so at the front 4115 03:26:46,960 --> 03:26:52,439 we can do 4116 03:26:48,800 --> 03:26:56,399 const result 4117 03:26:52,439 --> 03:26:58,840 equals and now if we 4118 03:26:56,399 --> 03:27:01,439 console.log the 4119 03:26:58,840 --> 03:27:04,800 result and 4120 03:27:01,439 --> 03:27:07,279 save the first value is true so the 4121 03:27:04,800 --> 03:27:08,760 value after the question mark gets saved 4122 03:27:07,279 --> 03:27:11,800 in the 4123 03:27:08,760 --> 03:27:14,920 variable as always we can use truthy and 4124 03:27:11,800 --> 03:27:18,160 falsy values instead of just true and 4125 03:27:14,920 --> 03:27:22,399 false so here if we change the first 4126 03:27:18,160 --> 03:27:25,680 value to a falsy value like zero this 4127 03:27:22,399 --> 03:27:29,239 will cause the value after the colon to 4128 03:27:25,680 --> 03:27:33,359 get saved inside the variable if we 4129 03:27:29,239 --> 03:27:35,880 save that's exactly what happens so this 4130 03:27:33,359 --> 03:27:40,680 is the turnery operator it's sort of a 4131 03:27:35,880 --> 03:27:43,720 shortcut for an if else statement like 4132 03:27:40,680 --> 03:27:47,120 this the next shortcut is called the 4133 03:27:43,720 --> 03:27:50,319 guard operator earlier in this lesson we 4134 03:27:47,120 --> 03:27:52,239 learned the and operator which checks if 4135 03:27:50,319 --> 03:27:55,199 two sides are both 4136 03:27:52,239 --> 03:27:57,600 truthy the and operator has a special 4137 03:27:55,199 --> 03:28:00,479 feature let's say we have an and 4138 03:27:57,600 --> 03:28:04,000 operator and the left side is 4139 03:28:00,479 --> 03:28:06,479 false in this situation we already know 4140 03:28:04,000 --> 03:28:09,239 it's impossible for both sides to be 4141 03:28:06,479 --> 03:28:12,560 truthy so we don't even need to check 4142 03:28:09,239 --> 03:28:15,120 the right side so the and operator 4143 03:28:12,560 --> 03:28:18,239 actually stops early and doesn't even 4144 03:28:15,120 --> 03:28:19,920 run the code on the right this is called 4145 03:28:18,239 --> 03:28:23,319 a shortcircuit 4146 03:28:19,920 --> 03:28:26,439 evaluation for example let's go into our 4147 03:28:23,319 --> 03:28:28,120 code and create some new lines and we'll 4148 03:28:26,439 --> 03:28:29,720 type 4149 03:28:28,120 --> 03:28:31,359 false 4150 03:28:29,720 --> 03:28:33,160 and 4151 03:28:31,359 --> 03:28:36,080 console. 4152 03:28:33,160 --> 03:28:38,239 log 4153 03:28:36,080 --> 03:28:42,239 hello if we 4154 03:28:38,239 --> 03:28:45,439 save the left side is falsy so the and 4155 03:28:42,239 --> 03:28:47,880 operator stops early or short circuits 4156 03:28:45,439 --> 03:28:51,080 and it doesn't even run the code on the 4157 03:28:47,880 --> 03:28:54,479 right so as you can see we can use the 4158 03:28:51,080 --> 03:28:57,439 value on the left to block or guard the 4159 03:28:54,479 --> 03:29:01,279 code on the right so when we use the and 4160 03:28:57,439 --> 03:29:04,160 operator like this we call it the guard 4161 03:29:01,279 --> 03:29:07,160 operator this is sort of like a shortcut 4162 03:29:04,160 --> 03:29:10,359 for an if statement like 4163 03:29:07,160 --> 03:29:12,920 this and just like the Turner operator 4164 03:29:10,359 --> 03:29:16,399 we can save the guard operator in a 4165 03:29:12,920 --> 03:29:19,279 variable for example let's create a 4166 03:29:16,399 --> 03:29:23,640 variable const 4167 03:29:19,279 --> 03:29:27,960 message and make it equal to 4168 03:29:23,640 --> 03:29:31,319 false and the string 4169 03:29:27,960 --> 03:29:34,040 hello if the first value is falsy the 4170 03:29:31,319 --> 03:29:38,640 guard operator will stop early and the 4171 03:29:34,040 --> 03:29:42,000 result will be the first value if we 4172 03:29:38,640 --> 03:29:44,319 console. log this 4173 03:29:42,000 --> 03:29:47,359 message and 4174 03:29:44,319 --> 03:29:48,680 save the value f false will be saved 4175 03:29:47,359 --> 03:29:51,239 inside this 4176 03:29:48,680 --> 03:29:55,680 variable now let's change this value to 4177 03:29:51,239 --> 03:29:58,000 a truthy value like the number five now 4178 03:29:55,680 --> 03:30:01,760 it will not stop early and it will give 4179 03:29:58,000 --> 03:30:05,040 us the second value as the result if we 4180 03:30:01,760 --> 03:30:06,600 save the string hello is now saved 4181 03:30:05,040 --> 03:30:09,479 inside this 4182 03:30:06,600 --> 03:30:12,760 variable this is sort of like a shortcut 4183 03:30:09,479 --> 03:30:15,199 for an if statement like this so don't 4184 03:30:12,760 --> 03:30:17,279 worry if it's a bit confusing all of 4185 03:30:15,199 --> 03:30:19,600 these are just shortcuts for an if 4186 03:30:17,279 --> 03:30:23,279 statement they're sort of like an if 4187 03:30:19,600 --> 03:30:26,279 statement that we can write on a single 4188 03:30:23,279 --> 03:30:28,920 line the last shortcut is called the 4189 03:30:26,279 --> 03:30:31,359 default operator which is similar to the 4190 03:30:28,920 --> 03:30:34,479 guard operator but uses 4191 03:30:31,359 --> 03:30:37,760 or earlier in this lesson we learned the 4192 03:30:34,479 --> 03:30:41,319 or operator which checks if at least one 4193 03:30:37,760 --> 03:30:44,600 side is truthy let's imagine we have an 4194 03:30:41,319 --> 03:30:47,880 or operator and the left side is 4195 03:30:44,600 --> 03:30:50,439 true in this situation we already know 4196 03:30:47,880 --> 03:30:53,279 the result of the or operator because 4197 03:30:50,439 --> 03:30:56,520 the left side is already truthy we don't 4198 03:30:53,279 --> 03:30:59,680 even need to check the right side so the 4199 03:30:56,520 --> 03:31:03,600 or operator also stops early or short 4200 03:30:59,680 --> 03:31:07,000 circuits if the first value is truthy 4201 03:31:03,600 --> 03:31:09,199 for example let's create some new lines 4202 03:31:07,000 --> 03:31:11,960 and let's say that in our final Amazon 4203 03:31:09,199 --> 03:31:15,319 Project we can choose a currency to pay 4204 03:31:11,960 --> 03:31:16,600 for our order let's create a variable 4205 03:31:15,319 --> 03:31:20,640 with 4206 03:31:16,600 --> 03:31:23,880 const currency and make it equal to the 4207 03:31:20,640 --> 03:31:26,399 string e or 4208 03:31:23,880 --> 03:31:29,160 Euros now let's say that choosing a 4209 03:31:26,399 --> 03:31:32,199 currency is optional if we don't choose 4210 03:31:29,160 --> 03:31:36,279 a currency we'll use a default value of 4211 03:31:32,199 --> 03:31:39,359 USD or United States dollars to create a 4212 03:31:36,279 --> 03:31:44,359 default value we can go to the end and 4213 03:31:39,359 --> 03:31:46,560 type or the string 4214 03:31:44,359 --> 03:31:49,439 USD so here 4215 03:31:46,560 --> 03:31:52,160 the left side is truthy so the or 4216 03:31:49,439 --> 03:31:54,960 operator will stop early and it will 4217 03:31:52,160 --> 03:31:58,000 result in the value on the 4218 03:31:54,960 --> 03:32:01,640 left if we 4219 03:31:58,000 --> 03:32:03,479 console. log the 4220 03:32:01,640 --> 03:32:06,319 currency and 4221 03:32:03,479 --> 03:32:08,720 save it will display 4222 03:32:06,319 --> 03:32:11,720 Euros now let's say that we didn't 4223 03:32:08,720 --> 03:32:13,840 choose a currency because it's optional 4224 03:32:11,720 --> 03:32:17,840 so let's go here and we're going to 4225 03:32:13,840 --> 03:32:17,840 change this currency to 4226 03:32:17,880 --> 03:32:24,199 undefined now the left side is falsy so 4227 03:32:21,479 --> 03:32:26,520 the or operator does not short circuit 4228 03:32:24,199 --> 03:32:30,000 it will continue and the result will be 4229 03:32:26,520 --> 03:32:34,720 the value on the right if we 4230 03:32:30,000 --> 03:32:37,479 save now the string USD is saved inside 4231 03:32:34,720 --> 03:32:41,199 currency as you can see we can use the 4232 03:32:37,479 --> 03:32:43,840 or operator to set a default value so 4233 03:32:41,199 --> 03:32:47,120 when we use the or operator like this we 4234 03:32:43,840 --> 03:32:49,199 call it the default operator 4235 03:32:47,120 --> 03:32:52,600 the default operator is sort of a 4236 03:32:49,199 --> 03:32:55,439 shortcut for an if statement like 4237 03:32:52,600 --> 03:32:57,120 this so don't worry remember that 4238 03:32:55,439 --> 03:33:00,160 everything that we learned in this last 4239 03:32:57,120 --> 03:33:02,920 section are basically some shortcuts for 4240 03:33:00,160 --> 03:33:05,080 if statements we don't have to use them 4241 03:33:02,920 --> 03:33:08,120 but they can save us some 4242 03:33:05,080 --> 03:33:10,439 typing and that's the end of this lesson 4243 03:33:08,120 --> 03:33:13,199 in this lesson we learned about booleans 4244 03:33:10,439 --> 03:33:16,120 and if statements Boolean values 4245 03:33:13,199 --> 03:33:18,399 represent whether something is true or 4246 03:33:16,120 --> 03:33:21,160 false we learned how to use if 4247 03:33:18,399 --> 03:33:23,439 statements to make decisions in our code 4248 03:33:21,160 --> 03:33:26,199 we learn how to use comparison and 4249 03:33:23,439 --> 03:33:28,920 logical operators we learned about 4250 03:33:26,199 --> 03:33:32,080 algorithms and created a simple version 4251 03:33:28,920 --> 03:33:35,640 of rock paper scissors we learned about 4252 03:33:32,080 --> 03:33:38,359 truthy and falsey values and we learned 4253 03:33:35,640 --> 03:33:41,279 some shortcuts for if statements the 4254 03:33:38,359 --> 03:33:44,040 turnery operator the guard operator and 4255 03:33:41,279 --> 03:33:46,560 the default operator here are some 4256 03:33:44,040 --> 03:33:49,160 exercises to help you practice booleans 4257 03:33:46,560 --> 03:33:49,160 and if 4258 03:34:14,479 --> 03:34:18,880 statements in this lesson we're going to 4259 03:34:16,880 --> 03:34:21,560 learn the next feature of JavaScript 4260 03:34:18,880 --> 03:34:24,080 called functions and we're going to use 4261 03:34:21,560 --> 03:34:27,600 functions to improve our rock paper 4262 03:34:24,080 --> 03:34:30,560 scissors project first let's create a 4263 03:34:27,600 --> 03:34:31,439 new file for this lesson we'll go to our 4264 03:34:30,560 --> 03:34:34,520 code 4265 03:34:31,439 --> 03:34:38,760 editor and create a new 4266 03:34:34,520 --> 03:34:41,359 file and let's name this file 4267 03:34:38,760 --> 03:34:43,120 07- 4268 03:34:41,359 --> 03:34:46,800 functions. 4269 03:34:43,120 --> 03:34:50,439 HTML and press enter 4270 03:34:46,800 --> 03:34:55,160 next let's copy booleans HTML to 4271 03:34:50,439 --> 03:34:57,279 functions. HTML so we'll click this file 4272 03:34:55,160 --> 03:34:59,479 and then select all this code using 4273 03:34:57,279 --> 03:35:03,760 control a or command 4274 03:34:59,479 --> 03:35:05,760 a and then right click 4275 03:35:03,760 --> 03:35:10,319 copy and in 4276 03:35:05,760 --> 03:35:13,760 here rightclick and 4277 03:35:10,319 --> 03:35:16,600 paste and we'll hide our files and 4278 03:35:13,760 --> 03:35:21,800 prepare this file for this lesson 4279 03:35:16,600 --> 03:35:24,399 so at the top we'll change the title to 4280 03:35:21,800 --> 03:35:27,199 functions and we'll remove all the code 4281 03:35:24,399 --> 03:35:27,199 in the script 4282 03:35:28,279 --> 03:35:33,040 element and let's 4283 03:35:30,359 --> 03:35:34,880 save and we'll open this file in live 4284 03:35:33,040 --> 03:35:38,120 server by 4285 03:35:34,880 --> 03:35:40,359 right-clicking open with live 4286 03:35:38,120 --> 03:35:42,960 server and we're going to open the 4287 03:35:40,359 --> 03:35:45,960 console as well so right 4288 03:35:42,960 --> 03:35:47,960 click inspect 4289 03:35:45,960 --> 03:35:50,760 and then click the 4290 03:35:47,960 --> 03:35:53,160 console and finally we can close the 4291 03:35:50,760 --> 03:35:57,439 previous 4292 03:35:53,160 --> 03:35:57,439 tabs as well as the previous 4293 03:35:58,800 --> 03:36:05,640 code okay now we're ready to learn 4294 03:36:02,560 --> 03:36:10,160 functions first of all what is a 4295 03:36:05,640 --> 03:36:13,239 function a function lets us reuse code 4296 03:36:10,160 --> 03:36:16,120 let's create our first function we're 4297 03:36:13,239 --> 03:36:18,720 going to go to the script element 4298 03:36:16,120 --> 03:36:20,680 and type the word 4299 03:36:18,720 --> 03:36:25,080 function and 4300 03:36:20,680 --> 03:36:27,120 space so this creates a new function and 4301 03:36:25,080 --> 03:36:29,560 then we're going to give a name to our 4302 03:36:27,120 --> 03:36:33,520 function we can name it almost anything 4303 03:36:29,560 --> 03:36:36,399 we want but for now let's just name it 4304 03:36:33,520 --> 03:36:40,239 function one and then we're going to 4305 03:36:36,399 --> 03:36:43,040 type round brackets and curly 4306 03:36:40,239 --> 03:36:45,080 brackets then inside these curly 4307 03:36:43,040 --> 03:36:46,120 brackets we're going to create a new 4308 03:36:45,080 --> 03:36:51,319 line 4309 03:36:46,120 --> 03:36:51,319 and we're going to type some code so 4310 03:36:52,239 --> 03:36:59,960 console.log the string 4311 03:36:55,199 --> 03:36:59,960 hello and we'll also type 4312 03:37:00,239 --> 03:37:06,239 console.log 2 + 4313 03:37:03,720 --> 03:37:09,880 2 and 4314 03:37:06,239 --> 03:37:12,760 save so this creates a function now 4315 03:37:09,880 --> 03:37:16,040 let's learn how to use this function 4316 03:37:12,760 --> 03:37:18,880 we're going to type at the bottom 4317 03:37:16,040 --> 03:37:22,560 the function name so 4318 03:37:18,880 --> 03:37:24,239 function one and then Open Bracket close 4319 03:37:22,560 --> 03:37:27,160 bracket and 4320 03:37:24,239 --> 03:37:30,439 semicolon when we type the function name 4321 03:37:27,160 --> 03:37:33,840 with brackets this will run the code 4322 03:37:30,439 --> 03:37:36,760 inside the function if we 4323 03:37:33,840 --> 03:37:39,359 save notice that it runs the code inside 4324 03:37:36,760 --> 03:37:40,439 the function and displays the two lines 4325 03:37:39,359 --> 03:37:43,960 in the 4326 03:37:40,439 --> 03:37:47,239 console so why do we use functions a 4327 03:37:43,960 --> 03:37:49,319 function lets us reuse code so if we 4328 03:37:47,239 --> 03:37:52,000 wanted to run both these lines of code 4329 03:37:49,319 --> 03:37:55,760 again all we have to do is type the 4330 03:37:52,000 --> 03:38:01,080 function name again with brackets so 4331 03:37:55,760 --> 03:38:03,840 function one and brackets again if we 4332 03:38:01,080 --> 03:38:07,199 save this will run the code inside the 4333 03:38:03,840 --> 03:38:10,199 function two times and we can reuse this 4334 03:38:07,199 --> 03:38:13,520 code as many times as we want by typing 4335 03:38:10,199 --> 03:38:16,120 the function name with brackets so 4336 03:38:13,520 --> 03:38:19,439 that's what a function is it lets us 4337 03:38:16,120 --> 03:38:22,680 reuse code now let's learn the syntax 4338 03:38:19,439 --> 03:38:24,399 rules for functions the word function 4339 03:38:22,680 --> 03:38:27,120 creates a new 4340 03:38:24,399 --> 03:38:29,520 function and then we choose a name for 4341 03:38:27,120 --> 03:38:32,720 our function the rules for function 4342 03:38:29,520 --> 03:38:36,239 names are the same as variable names we 4343 03:38:32,720 --> 03:38:39,120 can't use special words like function we 4344 03:38:36,239 --> 03:38:41,640 can't start with a number and we can't 4345 03:38:39,120 --> 03:38:44,840 use special characters although dollar 4346 03:38:41,640 --> 03:38:47,080 sign and underscore are allowed and just 4347 03:38:44,840 --> 03:38:50,239 like variable names the best practice is 4348 03:38:47,080 --> 03:38:53,920 to use camo case for function 4349 03:38:50,239 --> 03:38:56,319 names and inside these curly brackets we 4350 03:38:53,920 --> 03:38:58,920 put the code that we want to run when we 4351 03:38:56,319 --> 03:39:02,199 use the function this is called the 4352 03:38:58,920 --> 03:39:05,760 function body now one very important 4353 03:39:02,199 --> 03:39:08,439 thing to remember is that this code only 4354 03:39:05,760 --> 03:39:12,000 creates a function it doesn't run the 4355 03:39:08,439 --> 03:39:14,479 code in the function or do anything else 4356 03:39:12,000 --> 03:39:18,040 in order to actually run the code we 4357 03:39:14,479 --> 03:39:20,960 have to use the function like below by 4358 03:39:18,040 --> 03:39:23,720 typing the function name with 4359 03:39:20,960 --> 03:39:26,319 brackets this is known as calling the 4360 03:39:23,720 --> 03:39:28,960 function it's also known as running the 4361 03:39:26,319 --> 03:39:31,359 function or executing the 4362 03:39:28,960 --> 03:39:34,520 function now that we learned the syntax 4363 03:39:31,359 --> 03:39:37,160 rules for functions let's do a practical 4364 03:39:34,520 --> 03:39:39,120 example we're going to use functions to 4365 03:39:37,160 --> 03:39:42,840 improve the code of our rock paper 4366 03:39:39,120 --> 03:39:45,720 scissors project first let's click here 4367 03:39:42,840 --> 03:39:48,239 to show our files and we're actually 4368 03:39:45,720 --> 03:39:50,720 going to make a copy of the project just 4369 03:39:48,239 --> 03:39:52,479 for this lesson so we're going to 4370 03:39:50,720 --> 03:39:54,239 rightclick this 4371 03:39:52,479 --> 03:39:58,120 file 4372 03:39:54,239 --> 03:39:59,720 copy and then at the bottom right click 4373 03:39:58,120 --> 03:40:02,040 and 4374 03:39:59,720 --> 03:40:05,800 paste and we're going to rename this 4375 03:40:02,040 --> 03:40:09,680 file so rightclick 4376 03:40:05,800 --> 03:40:13,080 rename remove copy at the end and then 4377 03:40:09,680 --> 03:40:16,920 at the beginning we'll change this to 4378 03:40:13,080 --> 03:40:19,319 07 and press enter 4379 03:40:16,920 --> 03:40:23,040 next we're going to open this file in 4380 03:40:19,319 --> 03:40:25,600 live server by right clicking and then 4381 03:40:23,040 --> 03:40:28,479 open with live 4382 03:40:25,600 --> 03:40:32,600 server and let's also open the console 4383 03:40:28,479 --> 03:40:36,080 on this page we'll right click 4384 03:40:32,600 --> 03:40:39,279 inspect and then click the 4385 03:40:36,080 --> 03:40:43,160 console all right now if we look at the 4386 03:40:39,279 --> 03:40:45,560 code for generating a computer move 4387 03:40:43,160 --> 03:40:47,160 you'll notice that this code is the same 4388 03:40:45,560 --> 03:40:50,239 for all three 4389 03:40:47,160 --> 03:40:53,359 buttons so this is a perfect situation 4390 03:40:50,239 --> 03:40:57,560 to use a function because a function 4391 03:40:53,359 --> 03:41:00,199 lets us reuse code so let's scroll down 4392 03:40:57,560 --> 03:41:04,920 to the script element and we're going to 4393 03:41:00,199 --> 03:41:08,239 make a function here so let's type 4394 03:41:04,920 --> 03:41:10,359 function and then a function name let's 4395 03:41:08,239 --> 03:41:12,080 name this function 4396 03:41:10,359 --> 03:41:15,880 pick 4397 03:41:12,080 --> 03:41:19,160 computer move and then brackets and 4398 03:41:15,880 --> 03:41:22,120 curly brackets so one recommendation for 4399 03:41:19,160 --> 03:41:25,199 function names is to use a verb or an 4400 03:41:22,120 --> 03:41:28,239 action in the name such as 4401 03:41:25,199 --> 03:41:31,640 pick so next we're going to move all the 4402 03:41:28,239 --> 03:41:34,840 code for picking a computer move into 4403 03:41:31,640 --> 03:41:38,199 our new function so let's create a new 4404 03:41:34,840 --> 03:41:40,279 line here and we'll scroll up to the 4405 03:41:38,199 --> 03:41:43,239 scissors button because it's the closest 4406 03:41:40,279 --> 03:41:46,520 one and we'll select all of this code 4407 03:41:43,239 --> 03:41:48,880 for picking a computer move and then 4408 03:41:46,520 --> 03:41:51,760 rightclick 4409 03:41:48,880 --> 03:41:56,560 cut and inside the 4410 03:41:51,760 --> 03:41:59,479 function right click and 4411 03:41:56,560 --> 03:42:02,520 paste if we need to fix the formatting 4412 03:41:59,479 --> 03:42:04,159 we can select these lines and press tab 4413 03:42:02,520 --> 03:42:06,720 to add 4414 03:42:04,159 --> 03:42:08,800 indents so the reason we created this 4415 03:42:06,720 --> 03:42:10,880 function in the script element is 4416 03:42:08,800 --> 03:42:13,800 because remember that the script element 4417 03:42:10,880 --> 03:42:16,040 runs when the page is loaded so we want 4418 03:42:13,800 --> 03:42:19,600 to make sure that we create the function 4419 03:42:16,040 --> 03:42:22,680 first and then we use it in the buttons 4420 03:42:19,600 --> 03:42:25,159 after now remember this code just 4421 03:42:22,680 --> 03:42:27,920 creates a function it doesn't actually 4422 03:42:25,159 --> 03:42:31,279 run the code inside to run the code 4423 03:42:27,920 --> 03:42:33,520 inside we're going to call this function 4424 03:42:31,279 --> 03:42:36,199 so let's scroll up to the scissors 4425 03:42:33,520 --> 03:42:39,800 button and we're going to call this 4426 03:42:36,199 --> 03:42:44,880 function by typing the function name 4427 03:42:39,800 --> 03:42:46,800 pick computer move Open Bracket close 4428 03:42:44,880 --> 03:42:49,000 bracket ET and 4429 03:42:46,800 --> 03:42:52,199 semicolon if we 4430 03:42:49,000 --> 03:42:55,239 save and then click the scissors 4431 03:42:52,199 --> 03:42:58,199 button unfortunately it now gives us an 4432 03:42:55,239 --> 03:43:00,720 error saying computer move is not 4433 03:42:58,199 --> 03:43:03,760 defined now this might seem weird 4434 03:43:00,720 --> 03:43:06,920 because down here we actually create the 4435 03:43:03,760 --> 03:43:09,159 computer move so what's going on the 4436 03:43:06,920 --> 03:43:12,560 reason this happens is because functions 4437 03:43:09,159 --> 03:43:15,159 create a scope in the previous lesson we 4438 03:43:12,560 --> 03:43:18,199 learned about Scopes which limit weight 4439 03:43:15,159 --> 03:43:20,319 where a variable exists we learned about 4440 03:43:18,199 --> 03:43:22,840 if statements and that if statements 4441 03:43:20,319 --> 03:43:25,319 create a scope between the curly 4442 03:43:22,840 --> 03:43:28,199 brackets this means that any variable 4443 03:43:25,319 --> 03:43:30,920 created between the curly brackets only 4444 03:43:28,199 --> 03:43:34,840 exist between the curly 4445 03:43:30,920 --> 03:43:37,560 brackets functions also create a scope 4446 03:43:34,840 --> 03:43:41,080 any variable that is created between 4447 03:43:37,560 --> 03:43:43,720 these curly brackets only exists inside 4448 03:43:41,080 --> 03:43:46,600 the curly brackets and we can't use it 4449 03:43:43,720 --> 03:43:49,800 outside the curly Brack ETS like we do 4450 03:43:46,600 --> 03:43:53,319 here so that's why we're getting this 4451 03:43:49,800 --> 03:43:55,399 error so how do we solve this problem 4452 03:43:53,319 --> 03:43:57,640 remember that with if statements we 4453 03:43:55,399 --> 03:44:01,399 solve this by creating the variable 4454 03:43:57,640 --> 03:44:03,600 outside of the if statement like this we 4455 03:44:01,399 --> 03:44:06,640 can use a similar solution with 4456 03:44:03,600 --> 03:44:08,720 functions to make this variable exist 4457 03:44:06,640 --> 03:44:11,840 outside the function we just have to 4458 03:44:08,720 --> 03:44:14,479 create it outside the function so let's 4459 03:44:11,840 --> 03:44:16,199 select this code we're going to 4460 03:44:14,479 --> 03:44:17,800 rightclick 4461 03:44:16,199 --> 03:44:21,279 and 4462 03:44:17,800 --> 03:44:24,840 cut and remove these lines and then 4463 03:44:21,279 --> 03:44:27,720 outside the function we're going to 4464 03:44:24,840 --> 03:44:31,520 rightclick and 4465 03:44:27,720 --> 03:44:34,520 paste so now this variable is no longer 4466 03:44:31,520 --> 03:44:37,640 inside the function's scope and it can 4467 03:44:34,520 --> 03:44:39,840 be used anywhere else in the code so 4468 03:44:37,640 --> 03:44:42,439 variables like this that can be accessed 4469 03:44:39,840 --> 03:44:45,560 anywhere are called Global 4470 03:44:42,439 --> 03:44:47,920 variables if we save 4471 03:44:45,560 --> 03:44:51,000 and now click the scissors 4472 03:44:47,920 --> 03:44:53,880 button everything is working again 4473 03:44:51,000 --> 03:44:55,279 because this code above can now access 4474 03:44:53,880 --> 03:44:57,600 this 4475 03:44:55,279 --> 03:45:00,800 variable now let's press 4476 03:44:57,600 --> 03:45:04,560 okay and let's use this function to 4477 03:45:00,800 --> 03:45:06,479 reuse this code so we'll scroll up to 4478 03:45:04,560 --> 03:45:09,760 the paper 4479 03:45:06,479 --> 03:45:12,720 button and now instead of all this code 4480 03:45:09,760 --> 03:45:16,399 again we're just going to remove it and 4481 03:45:12,720 --> 03:45:20,760 call the function pick 4482 03:45:16,399 --> 03:45:24,560 computer move Open Bracket close bracket 4483 03:45:20,760 --> 03:45:26,120 semicolon and also with the rock button 4484 03:45:24,560 --> 03:45:29,359 we'll select all this 4485 03:45:26,120 --> 03:45:31,159 code and remove it and just call the 4486 03:45:29,359 --> 03:45:32,800 function 4487 03:45:31,159 --> 03:45:36,920 pick 4488 03:45:32,800 --> 03:45:39,239 computer move brackets and 4489 03:45:36,920 --> 03:45:42,080 semicolon now if we 4490 03:45:39,239 --> 03:45:45,439 save and click the rock 4491 03:45:42,080 --> 03:45:48,560 button it works just like before 4492 03:45:45,439 --> 03:45:52,800 we'll press okay and click the paper 4493 03:45:48,560 --> 03:45:55,760 button it also works just like before so 4494 03:45:52,800 --> 03:45:57,840 as you can see functions let us reuse 4495 03:45:55,760 --> 03:46:00,800 code and they make our code a lot 4496 03:45:57,840 --> 03:46:03,960 cleaner by removing all the duplication 4497 03:46:00,800 --> 03:46:06,760 that we had another benefit of removing 4498 03:46:03,960 --> 03:46:09,080 duplication is that before if we ever 4499 03:46:06,760 --> 03:46:11,680 wanted to update the code for picking a 4500 03:46:09,080 --> 03:46:15,279 computer move we had to update it in 4501 03:46:11,680 --> 03:46:17,279 three places in each of the buttons but 4502 03:46:15,279 --> 03:46:20,319 now if we scroll 4503 03:46:17,279 --> 03:46:23,840 down we only have to update it in one 4504 03:46:20,319 --> 03:46:26,319 place inside this function so functions 4505 03:46:23,840 --> 03:46:27,319 also make our code easier to update in 4506 03:46:26,319 --> 03:46:29,760 the 4507 03:46:27,319 --> 03:46:32,319 future next we're going to learn a 4508 03:46:29,760 --> 03:46:35,439 feature of functions called a return 4509 03:46:32,319 --> 03:46:39,279 statement a return statement lets us get 4510 03:46:35,439 --> 03:46:43,159 a value out of a function so at the end 4511 03:46:39,279 --> 03:46:46,760 of this function let's type some new 4512 03:46:43,159 --> 03:46:50,080 lines and we're going to type type 4513 03:46:46,760 --> 03:46:52,399 return and then a value like the number 4514 03:46:50,080 --> 03:46:55,359 five and 4515 03:46:52,399 --> 03:46:58,800 semicolon now whenever we call this 4516 03:46:55,359 --> 03:47:02,399 function it will result in a value the 4517 03:46:58,800 --> 03:47:05,520 number five so up 4518 03:47:02,399 --> 03:47:08,640 here when we call this function this 4519 03:47:05,520 --> 03:47:14,239 will result in the number five so we can 4520 03:47:08,640 --> 03:47:14,239 actually console.log this console.log 4521 03:47:16,920 --> 03:47:21,159 and then 4522 03:47:18,120 --> 03:47:24,279 save and click the scissors 4523 03:47:21,159 --> 03:47:26,720 button and it will show us that calling 4524 03:47:24,279 --> 03:47:29,960 the function now results in the number 4525 03:47:26,720 --> 03:47:32,600 five and we display it in the 4526 03:47:29,960 --> 03:47:36,319 console so that's how a return statement 4527 03:47:32,600 --> 03:47:40,159 works it lets us get a value out of this 4528 03:47:36,319 --> 03:47:42,359 function let's press okay and do another 4529 03:47:40,159 --> 03:47:45,239 example we'll scroll 4530 03:47:42,359 --> 03:47:48,159 down and this time let's change CH this 4531 03:47:45,239 --> 03:47:51,600 value to the string 4532 03:47:48,159 --> 03:47:55,920 Rock now whenever we call this function 4533 03:47:51,600 --> 03:47:59,080 it will result in the string Rock if we 4534 03:47:55,920 --> 03:48:01,800 save and then click 4535 03:47:59,080 --> 03:48:04,720 scissors and scroll up 4536 03:48:01,800 --> 03:48:07,840 here calling this function now results 4537 03:48:04,720 --> 03:48:09,199 in the string Rock and then we display 4538 03:48:07,840 --> 03:48:12,439 it in the 4539 03:48:09,199 --> 03:48:14,520 console so now let's press okay and 4540 03:48:12,439 --> 03:48:16,760 we'll learn the syntax rules for a 4541 03:48:14,520 --> 03:48:16,760 return 4542 03:48:16,840 --> 03:48:22,680 statement so we just have to type the 4543 03:48:19,040 --> 03:48:26,040 word return and then a value and it will 4544 03:48:22,680 --> 03:48:28,319 get this value out of the function now 4545 03:48:26,040 --> 03:48:31,800 instead of just a value we can also 4546 03:48:28,319 --> 03:48:34,239 return a calculation or a variable or 4547 03:48:31,800 --> 03:48:37,920 anything that results in a 4548 03:48:34,239 --> 03:48:41,359 value so this is known as returning a 4549 03:48:37,920 --> 03:48:44,960 value from a function and this value is 4550 03:48:41,359 --> 03:48:47,000 known as the return value we also Al 4551 03:48:44,960 --> 03:48:49,800 don't have to return anything from a 4552 03:48:47,000 --> 03:48:52,960 function if a function doesn't have a 4553 03:48:49,800 --> 03:48:56,680 return statement or we return without a 4554 03:48:52,960 --> 03:48:57,880 value like this this will return the 4555 03:48:56,680 --> 03:49:00,239 value 4556 03:48:57,880 --> 03:49:03,359 undefined so if we 4557 03:49:00,239 --> 03:49:07,359 save and then click scissors 4558 03:49:03,359 --> 03:49:10,159 again the function will return 4559 03:49:07,359 --> 03:49:12,960 undefined and lastly when we use a 4560 03:49:10,159 --> 03:49:15,760 return statement it ends the function 4561 03:49:12,960 --> 03:49:20,359 immediately so after the the return 4562 03:49:15,760 --> 03:49:20,359 statement if we type 4563 03:49:20,960 --> 03:49:29,120 console.log the string 4564 03:49:24,279 --> 03:49:32,399 after and press okay here and save this 4565 03:49:29,120 --> 03:49:35,159 file and then click the scissors 4566 03:49:32,399 --> 03:49:38,680 button notice that it doesn't run this 4567 03:49:35,159 --> 03:49:41,120 code and that's because once we return 4568 03:49:38,680 --> 03:49:43,439 the code returns back to where we called 4569 03:49:41,120 --> 03:49:45,600 the function that's why it's called a 4570 03:49:43,439 --> 03:49:49,000 return statement so it's not going to 4571 03:49:45,600 --> 03:49:51,600 run anything after the return so we can 4572 03:49:49,000 --> 03:49:51,600 remove this 4573 03:49:51,720 --> 03:49:56,960 code and press 4574 03:49:54,239 --> 03:49:59,199 okay so now that we understand return 4575 03:49:56,960 --> 03:50:02,399 statements we're going to learn a better 4576 03:49:59,199 --> 03:50:05,479 way of getting the computer move out of 4577 03:50:02,399 --> 03:50:07,560 this function so first let's actually 4578 03:50:05,479 --> 03:50:11,000 move this variable back into the 4579 03:50:07,560 --> 03:50:13,439 function so we right click and 4580 03:50:11,000 --> 03:50:18,279 cut and then in 4581 03:50:13,439 --> 03:50:22,120 here right right click and 4582 03:50:18,279 --> 03:50:22,120 paste and delete these 4583 03:50:22,920 --> 03:50:28,520 lines so now we're back to the original 4584 03:50:25,600 --> 03:50:31,319 problem how do we access computer move 4585 03:50:28,520 --> 03:50:34,760 outside of the function so another 4586 03:50:31,319 --> 03:50:37,319 solution is to return this variable 4587 03:50:34,760 --> 03:50:40,720 because remember a return statement lets 4588 03:50:37,319 --> 03:50:41,800 us get a value out of the function so 4589 03:50:40,720 --> 03:50:45,439 down 4590 03:50:41,800 --> 03:50:47,479 here we're going to return the 4591 03:50:45,439 --> 03:50:50,600 computer 4592 03:50:47,479 --> 03:50:53,439 move so this will take whatever value is 4593 03:50:50,600 --> 03:50:56,760 saved inside computer move which is one 4594 03:50:53,439 --> 03:50:58,800 of these values and return it out of the 4595 03:50:56,760 --> 03:51:02,399 function if we 4596 03:50:58,800 --> 03:51:04,920 save and then click these scissors 4597 03:51:02,399 --> 03:51:08,359 button we were able to get the move 4598 03:51:04,920 --> 03:51:10,479 scissors outside of this function so 4599 03:51:08,359 --> 03:51:13,000 what happened here is that we return 4600 03:51:10,479 --> 03:51:15,960 this move and then up 4601 03:51:13,000 --> 03:51:18,399 here we took the move and displayed it 4602 03:51:15,960 --> 03:51:20,720 in the console to get 4603 03:51:18,399 --> 03:51:22,880 scissors so that's another way of 4604 03:51:20,720 --> 03:51:26,000 getting the computer move out of a 4605 03:51:22,880 --> 03:51:29,000 function using the return 4606 03:51:26,000 --> 03:51:32,199 statement however now we're back to this 4607 03:51:29,000 --> 03:51:34,920 problem with computer move not being 4608 03:51:32,199 --> 03:51:38,920 defined because we moved it back into 4609 03:51:34,920 --> 03:51:42,600 the function's scope so let's scroll up 4610 03:51:38,920 --> 03:51:45,359 and remember that this return value is 4611 03:51:42,600 --> 03:51:48,159 just a value we can use it like any 4612 03:51:45,359 --> 03:51:51,560 other value for example we can 4613 03:51:48,159 --> 03:51:55,159 console.log it or we can also save this 4614 03:51:51,560 --> 03:51:57,880 in a variable so let's do that right now 4615 03:51:55,159 --> 03:52:01,840 we're going to remove 4616 03:51:57,880 --> 03:52:05,760 this and create a new variable with 4617 03:52:01,840 --> 03:52:09,800 const and let's just name this variable 4618 03:52:05,760 --> 03:52:13,279 computer move to match what we use down 4619 03:52:09,800 --> 03:52:15,640 here and we use equals to save the 4620 03:52:13,279 --> 03:52:18,760 return value inside inside this 4621 03:52:15,640 --> 03:52:22,279 variable so note that even though we use 4622 03:52:18,760 --> 03:52:25,080 computer move here as well as here 4623 03:52:22,279 --> 03:52:27,640 there're different variables this one is 4624 03:52:25,080 --> 03:52:29,279 inside the function's scope so it 4625 03:52:27,640 --> 03:52:32,880 prevents it from conflicting with 4626 03:52:29,279 --> 03:52:35,880 variables outside the function's scope 4627 03:52:32,880 --> 03:52:38,640 that's one of the main benefits of 4628 03:52:35,880 --> 03:52:41,640 Scopes so now that we saved the return 4629 03:52:38,640 --> 03:52:44,040 value into this variable and we're using 4630 03:52:41,640 --> 03:52:46,560 this variable down here the code should 4631 03:52:44,040 --> 03:52:49,399 now work work if we 4632 03:52:46,560 --> 03:52:52,159 save and click 4633 03:52:49,399 --> 03:52:55,399 scissors everything is back to 4634 03:52:52,159 --> 03:52:57,279 normal and let's press okay so don't 4635 03:52:55,399 --> 03:52:59,520 worry functions can be a little 4636 03:52:57,279 --> 03:53:02,760 confusing at first because the code is 4637 03:52:59,520 --> 03:53:05,319 no longer simply going from top to 4638 03:53:02,760 --> 03:53:08,239 bottom it sort of jumps around a little 4639 03:53:05,319 --> 03:53:11,319 bit so let's go through this step by 4640 03:53:08,239 --> 03:53:14,159 step when we click this button we're 4641 03:53:11,319 --> 03:53:16,800 going to run this function so the code 4642 03:53:14,159 --> 03:53:18,880 is going going to jump down here and 4643 03:53:16,800 --> 03:53:20,720 then it's going to run all of this code 4644 03:53:18,880 --> 03:53:24,040 from top to 4645 03:53:20,720 --> 03:53:27,279 bottom at the end we're going to return 4646 03:53:24,040 --> 03:53:30,040 whatever is inside computer move back to 4647 03:53:27,279 --> 03:53:33,600 where we called this function so we're 4648 03:53:30,040 --> 03:53:35,720 going to return all the way back here 4649 03:53:33,600 --> 03:53:39,760 and then we're going to save that return 4650 03:53:35,720 --> 03:53:42,279 value into a new variable computer move 4651 03:53:39,760 --> 03:53:45,279 and then use computer move down 4652 03:53:42,279 --> 03:53:48,239 here so following the code line by line 4653 03:53:45,279 --> 03:53:50,399 like this is called tracing the code 4654 03:53:48,239 --> 03:53:52,479 it's a useful technique to understand 4655 03:53:50,399 --> 03:53:54,800 exactly what the code is 4656 03:53:52,479 --> 03:53:58,720 doing now let's compare the two 4657 03:53:54,800 --> 03:54:01,159 solutions that we used a global variable 4658 03:53:58,720 --> 03:54:04,120 and returning a 4659 03:54:01,159 --> 03:54:07,560 variable so returning a variable like 4660 03:54:04,120 --> 03:54:10,279 this is actually preferred over using a 4661 03:54:07,560 --> 03:54:12,640 global variable and that's because a 4662 03:54:10,279 --> 03:54:15,479 scope can help us prevent naming 4663 03:54:12,640 --> 03:54:18,080 conflicts so all the variables inside 4664 03:54:15,479 --> 03:54:21,359 here will not conflict with anything 4665 03:54:18,080 --> 03:54:23,880 outside the scope so generally it's a 4666 03:54:21,359 --> 03:54:26,000 best practice to keep things inside a 4667 03:54:23,880 --> 03:54:28,800 scope if we 4668 03:54:26,000 --> 03:54:31,960 can now let's move on and use this 4669 03:54:28,800 --> 03:54:34,000 function in the other buttons feel free 4670 03:54:31,960 --> 03:54:35,800 to pause the video if you want to try it 4671 03:54:34,000 --> 03:54:39,840 yourself 4672 03:54:35,800 --> 03:54:43,439 first so we'll scroll up to the paper 4673 03:54:39,840 --> 03:54:46,319 button and then here we're going to save 4674 03:54:43,439 --> 03:54:48,319 The Returned computer move into a 4675 03:54:46,319 --> 03:54:50,000 variable so 4676 03:54:48,319 --> 03:54:53,319 const 4677 03:54:50,000 --> 03:54:57,040 computer move so that it matches the 4678 03:54:53,319 --> 03:55:00,399 code down here make it equal to whatever 4679 03:54:57,040 --> 03:55:02,640 is returned and then same thing for the 4680 03:55:00,399 --> 03:55:06,960 rock button we're going to save the 4681 03:55:02,640 --> 03:55:09,960 return value in a variable const 4682 03:55:06,960 --> 03:55:12,000 computer move 4683 03:55:09,960 --> 03:55:16,159 equals if we 4684 03:55:12,000 --> 03:55:17,080 save and click the rock button 4685 03:55:16,159 --> 03:55:20,239 it 4686 03:55:17,080 --> 03:55:24,279 works let's press okay and click the 4687 03:55:20,239 --> 03:55:27,560 paper button and this also works and 4688 03:55:24,279 --> 03:55:29,800 let's press okay so that's how we use a 4689 03:55:27,560 --> 03:55:31,720 return statement to take advantage of 4690 03:55:29,800 --> 03:55:35,199 the benefits of 4691 03:55:31,720 --> 03:55:38,040 scope next if we look at the rest of 4692 03:55:35,199 --> 03:55:40,399 this code where we compare our move and 4693 03:55:38,040 --> 03:55:43,040 display the result you'll notice that 4694 03:55:40,399 --> 03:55:44,920 this code is very similar for each of 4695 03:55:43,040 --> 03:55:48,319 the buttons you the only thing that's 4696 03:55:44,920 --> 03:55:51,120 different is the result and the message 4697 03:55:48,319 --> 03:55:54,520 that we display so we can actually use 4698 03:55:51,120 --> 03:55:57,800 functions here as well to reuse code 4699 03:55:54,520 --> 03:55:59,520 that has small differences to do this 4700 03:55:57,800 --> 03:56:01,399 we're going to need another feature of 4701 03:55:59,520 --> 03:56:03,840 functions called 4702 03:56:01,399 --> 03:56:05,640 parameters so let's go back into 4703 03:56:03,840 --> 03:56:10,359 functions. 4704 03:56:05,640 --> 03:56:12,960 HTML and let's open the tab for this 4705 03:56:10,359 --> 03:56:14,800 file and let's also comment out the 4706 03:56:12,960 --> 03:56:20,279 previous code by typing 4707 03:56:14,800 --> 03:56:23,080 slash star and at the bottom star 4708 03:56:20,279 --> 03:56:25,960 slash and create some new 4709 03:56:23,080 --> 03:56:28,640 lines and now we're ready to learn 4710 03:56:25,960 --> 03:56:31,640 parameters so parameters are sort of the 4711 03:56:28,640 --> 03:56:35,080 opposite of a return statement a return 4712 03:56:31,640 --> 03:56:39,080 statement gets a value out of a function 4713 03:56:35,080 --> 03:56:41,880 a parameter puts a value into a function 4714 03:56:39,080 --> 03:56:43,840 so let's do an example let's say that 4715 03:56:41,880 --> 03:56:44,880 we're working on the final Amazon 4716 03:56:43,840 --> 03:56:47,640 Project 4717 03:56:44,880 --> 03:56:50,439 and we need to calculate the tax for our 4718 03:56:47,640 --> 03:56:53,840 order let's create a function to 4719 03:56:50,439 --> 03:56:57,159 calculate the tax so 4720 03:56:53,840 --> 03:57:01,199 function let's name it 4721 03:56:57,159 --> 03:57:03,520 calculate tax brackets and curly 4722 03:57:01,199 --> 03:57:06,080 brackets and then inside the curly 4723 03:57:03,520 --> 03:57:08,479 brackets let's say that the cost of our 4724 03:57:06,080 --> 03:57:12,720 products is 4725 03:57:08,479 --> 03:57:16,560 1,000 or $10 and the tax is 4726 03:57:12,720 --> 03:57:19,319 10% to calc calculate the tax we can do 4727 03:57:16,560 --> 03:57:21,399 1,000 * 4728 03:57:19,319 --> 03:57:24,159 0.1 which is 4729 03:57:21,399 --> 03:57:29,800 10% let's also display this number in 4730 03:57:24,159 --> 03:57:29,800 the console so at the front we'll type 4731 03:57:30,239 --> 03:57:33,640 console.log and 4732 03:57:34,319 --> 03:57:40,880 brackets now let's save and remember 4733 03:57:38,279 --> 03:57:43,840 this code only creates a function it 4734 03:57:40,880 --> 03:57:46,640 doesn't run the code inside to run this 4735 03:57:43,840 --> 03:57:48,800 code we're going to call this function 4736 03:57:46,640 --> 03:57:54,359 so down here we're going to type the 4737 03:57:48,800 --> 03:57:56,319 function name calculate tax and then 4738 03:57:54,359 --> 03:57:59,920 brackets if we 4739 03:57:56,319 --> 03:58:04,239 save it runs this code and then it 4740 03:57:59,920 --> 03:58:07,680 displays the tax which is 100 cents or 4741 03:58:04,239 --> 03:58:10,560 $1 however right now this function only 4742 03:58:07,680 --> 03:58:12,880 calculates the tax for 4743 03:58:10,560 --> 03:58:15,920 1,000 what if we want to calculate the 4744 03:58:12,880 --> 03:58:18,640 tax for a different amount how would we 4745 03:58:15,920 --> 03:58:21,399 modify this function to work with any 4746 03:58:18,640 --> 03:58:24,600 number to do this we're going to learn a 4747 03:58:21,399 --> 03:58:27,439 feature of functions called parameters a 4748 03:58:24,600 --> 03:58:30,080 parameter lets us put a value into a 4749 03:58:27,439 --> 03:58:33,680 function so let's finally create our 4750 03:58:30,080 --> 03:58:36,399 first parameter so inside these round 4751 03:58:33,680 --> 03:58:38,960 brackets we're going to choose a name 4752 03:58:36,399 --> 03:58:40,520 for our parameter so let's just name 4753 03:58:38,960 --> 03:58:42,239 this 4754 03:58:40,520 --> 03:58:45,560 parameter 4755 03:58:42,239 --> 03:58:49,000 1 a parameter one works the same way as 4756 03:58:45,560 --> 03:58:51,600 a variable we can save a value inside 4757 03:58:49,000 --> 03:58:52,359 this parameter and then use it just like 4758 03:58:51,600 --> 03:58:55,439 a 4759 03:58:52,359 --> 03:58:58,600 variable so first let's save a value 4760 03:58:55,439 --> 03:59:00,279 inside the parameter to do that we're 4761 03:58:58,600 --> 03:59:03,560 going to go to where we call the 4762 03:59:00,279 --> 03:59:08,399 function and then inside these brackets 4763 03:59:03,560 --> 03:59:12,199 we're going to put a value for example 4764 03:59:08,399 --> 03:59:14,159 2,000 this will save this value into 4765 03:59:12,199 --> 03:59:16,439 this parameter 4766 03:59:14,159 --> 03:59:18,800 and now we can use it just like a 4767 03:59:16,439 --> 03:59:21,800 variable so instead of 4768 03:59:18,800 --> 03:59:25,600 1,000 we're going to type 4769 03:59:21,800 --> 03:59:29,399 parameter 1 and it will take whatever 4770 03:59:25,600 --> 03:59:33,600 value is inside parameter 1 in this case 4771 03:59:29,399 --> 03:59:34,520 2,000 and substitute it into the code if 4772 03:59:33,600 --> 03:59:38,800 we 4773 03:59:34,520 --> 03:59:44,319 save it now calculates 2,000 * 4774 03:59:38,800 --> 03:59:46,640 0.1 to give us 200 cents as the tax 4775 03:59:44,319 --> 03:59:49,279 now every time we call the function we 4776 03:59:46,640 --> 03:59:52,960 can save a different value into the 4777 03:59:49,279 --> 03:59:56,760 parameter for example let's create a new 4778 03:59:52,960 --> 03:59:59,399 line and call the function 4779 03:59:56,760 --> 04:00:02,000 calculate 4780 03:59:59,399 --> 04:00:03,720 tax and this time we're going to save 4781 04:00:02,000 --> 04:00:08,640 the value 4782 04:00:03,720 --> 04:00:11,120 5,000 into parameter 1 if we 4783 04:00:08,640 --> 04:00:14,279 save this line of code will calculate 4784 04:00:11,120 --> 04:00:17,800 the tax for 5,000 4785 04:00:14,279 --> 04:00:21,680 and result in 500 cents so as you can 4786 04:00:17,800 --> 04:00:24,120 see a parameter lets us put values into 4787 04:00:21,680 --> 04:00:26,600 a function and this allows our function 4788 04:00:24,120 --> 04:00:29,479 to work with any 4789 04:00:26,600 --> 04:00:33,000 value now let's learn the syntax rules 4790 04:00:29,479 --> 04:00:36,120 for parameters to create a parameter we 4791 04:00:33,000 --> 04:00:38,920 just type the parameter name between the 4792 04:00:36,120 --> 04:00:42,120 brackets parameter names follow the same 4793 04:00:38,920 --> 04:00:44,560 rules as variable names we can't use 4794 04:00:42,120 --> 04:00:46,800 special words we can't start with a 4795 04:00:44,560 --> 04:00:50,000 number and we can't use special 4796 04:00:46,800 --> 04:00:52,439 characters other than dollar sign and 4797 04:00:50,000 --> 04:00:53,920 underscore and the best practice is to 4798 04:00:52,439 --> 04:00:56,239 use cam 4799 04:00:53,920 --> 04:00:59,680 case let's actually change this 4800 04:00:56,239 --> 04:01:00,560 parameter name to cost so the code makes 4801 04:00:59,680 --> 04:01:03,159 more 4802 04:01:00,560 --> 04:01:06,080 sense and 4803 04:01:03,159 --> 04:01:09,279 save next when a function has a 4804 04:01:06,080 --> 04:01:12,239 parameter we usually say this function 4805 04:01:09,279 --> 04:01:15,359 takes a parameter if this parameter 4806 04:01:12,239 --> 04:01:16,720 should be a number we say this function 4807 04:01:15,359 --> 04:01:19,520 takes a 4808 04:01:16,720 --> 04:01:22,680 number when we put a value into the 4809 04:01:19,520 --> 04:01:25,960 function like this we call this passing 4810 04:01:22,680 --> 04:01:28,800 a value into the function another name 4811 04:01:25,960 --> 04:01:31,520 for this value is called the 4812 04:01:28,800 --> 04:01:33,920 argument and believe it or not we've 4813 04:01:31,520 --> 04:01:36,319 actually used functions and parameters 4814 04:01:33,920 --> 04:01:40,199 this whole time the first code that we 4815 04:01:36,319 --> 04:01:43,000 learned in this course was alert alert 4816 04:01:40,199 --> 04:01:46,120 is actually a function you can tell it's 4817 04:01:43,000 --> 04:01:49,159 a function because of the round brackets 4818 04:01:46,120 --> 04:01:51,800 and between the brackets we put a string 4819 04:01:49,159 --> 04:01:55,040 so this is an example of a 4820 04:01:51,800 --> 04:01:58,359 parameter and lastly a parameter only 4821 04:01:55,040 --> 04:02:01,279 exists inside the function's scope so we 4822 04:01:58,359 --> 04:02:04,399 can only use this parameter between the 4823 04:02:01,279 --> 04:02:05,439 curly brackets we can't use it outside 4824 04:02:04,399 --> 04:02:08,399 the 4825 04:02:05,439 --> 04:02:12,159 function next a function can actually 4826 04:02:08,399 --> 04:02:14,920 have more than one parameter for example 4827 04:02:12,159 --> 04:02:18,319 right now our calculate TX function can 4828 04:02:14,920 --> 04:02:20,359 only calculate a 10% tax but what if 4829 04:02:18,319 --> 04:02:23,600 we're in a different country and we want 4830 04:02:20,359 --> 04:02:27,199 to calculate a different tax percent to 4831 04:02:23,600 --> 04:02:30,359 solve this we can add a second parameter 4832 04:02:27,199 --> 04:02:34,120 by typing after the first parameter a 4833 04:02:30,359 --> 04:02:38,680 comma and then another parameter name 4834 04:02:34,120 --> 04:02:42,080 let's name this tax 4835 04:02:38,680 --> 04:02:44,439 percent and now to save a value into the 4836 04:02:42,080 --> 04:02:48,680 second parameter we'll go go to where we 4837 04:02:44,439 --> 04:02:51,920 call this function and also type a comma 4838 04:02:48,680 --> 04:02:57,319 and a second value like 4839 04:02:51,920 --> 04:02:59,600 0.2 or a 20% tax so the first value gets 4840 04:02:57,319 --> 04:03:02,640 saved in the first parameter and the 4841 04:02:59,600 --> 04:03:05,199 second value gets saved in the second 4842 04:03:02,640 --> 04:03:08,279 parameter and now we can use our second 4843 04:03:05,199 --> 04:03:11,840 parameter just like a variable so 4844 04:03:08,279 --> 04:03:15,560 instead of 0.1 every time we're going to 4845 04:03:11,840 --> 04:03:18,279 calculate it with tax 4846 04:03:15,560 --> 04:03:21,600 percent and it will substitute whatever 4847 04:03:18,279 --> 04:03:23,920 value is inside tax percent into the 4848 04:03:21,600 --> 04:03:27,920 code if we 4849 04:03:23,920 --> 04:03:28,720 save this first line will multiply 2,000 4850 04:03:27,920 --> 04:03:32,920 by 4851 04:03:28,720 --> 04:03:35,920 0.2 and give us 400 cents as the 4852 04:03:32,920 --> 04:03:38,359 result so we can have as many parameters 4853 04:03:35,920 --> 04:03:40,319 as we want in a function we just have to 4854 04:03:38,359 --> 04:03:42,960 separate them with a 4855 04:03:40,319 --> 04:03:45,319 comma Now notice that the second time we 4856 04:03:42,960 --> 04:03:48,120 call this function we didn't give a 4857 04:03:45,319 --> 04:03:50,960 value for the second parameter if we 4858 04:03:48,120 --> 04:03:53,359 don't save a value into the parameter 4859 04:03:50,960 --> 04:03:57,720 the parameter will get the value 4860 04:03:53,359 --> 04:04:03,560 undefined so here if we 4861 04:03:57,720 --> 04:04:05,760 console. log the second parameter tax 4862 04:04:03,560 --> 04:04:08,520 percent and 4863 04:04:05,760 --> 04:04:11,800 save it will show us that the second 4864 04:04:08,520 --> 04:04:15,159 time tax percent is 4865 04:04:11,800 --> 04:04:18,720 undefined and then multiplying the cost 4866 04:04:15,159 --> 04:04:23,040 time undefined is not valid math so we 4867 04:04:18,720 --> 04:04:25,000 get the result Nan or not a number so 4868 04:04:23,040 --> 04:04:28,080 now let's remove the 4869 04:04:25,000 --> 04:04:31,720 console.log and we'll learn how to fix 4870 04:04:28,080 --> 04:04:34,239 this so to fix this we can either pass a 4871 04:04:31,720 --> 04:04:38,199 second value into the function like this 4872 04:04:34,239 --> 04:04:41,960 line or we can also set a default value 4873 04:04:38,199 --> 04:04:45,120 for the parameter to set a default value 4874 04:04:41,960 --> 04:04:48,960 we can just type after the parameter 4875 04:04:45,120 --> 04:04:52,080 and then add equal sign and a default 4876 04:04:48,960 --> 04:04:54,279 value like 4877 04:04:52,080 --> 04:04:57,159 0.1 if we 4878 04:04:54,279 --> 04:05:01,159 save the second function call Will 4879 04:04:57,159 --> 04:05:03,920 multiply 5,000 by the default value 4880 04:05:01,159 --> 04:05:07,319 0.1 and give us 4881 04:05:03,920 --> 04:05:08,840 500 so that's how we add a default value 4882 04:05:07,319 --> 04:05:11,600 for a 4883 04:05:08,840 --> 04:05:13,920 parameter all right now that we learned 4884 04:05:11,600 --> 04:05:16,479 parameters we're going to go back to the 4885 04:05:13,920 --> 04:05:19,560 rock paper scissors project and reuse 4886 04:05:16,479 --> 04:05:23,120 even more code so let's go back to the 4887 04:05:19,560 --> 04:05:24,560 file for the project and also the tab 4888 04:05:23,120 --> 04:05:27,680 for this 4889 04:05:24,560 --> 04:05:30,399 project and now if we look at the code 4890 04:05:27,680 --> 04:05:33,080 inside each of the buttons you'll notice 4891 04:05:30,399 --> 04:05:35,640 that the code is basically the same for 4892 04:05:33,080 --> 04:05:39,159 all three buttons the only small 4893 04:05:35,640 --> 04:05:42,399 difference is the move that we picked so 4894 04:05:39,159 --> 04:05:44,760 this is a perfect situation to use a 4895 04:05:42,399 --> 04:05:48,399 parameter so so inside the script 4896 04:05:44,760 --> 04:05:53,560 element let's create a new function to 4897 04:05:48,399 --> 04:05:56,840 reuse all of this code we'll type 4898 04:05:53,560 --> 04:06:01,479 function and let's name this function 4899 04:05:56,840 --> 04:06:04,560 play game brackets and curly 4900 04:06:01,479 --> 04:06:07,279 brackets next let's move all of this 4901 04:06:04,560 --> 04:06:10,319 code inside the function so we can reuse 4902 04:06:07,279 --> 04:06:12,560 it we'll select this 4903 04:06:10,319 --> 04:06:14,399 code and then 4904 04:06:12,560 --> 04:06:17,600 rightclick 4905 04:06:14,399 --> 04:06:22,319 cut and inside the 4906 04:06:17,600 --> 04:06:25,960 function right click and 4907 04:06:22,319 --> 04:06:30,000 paste and if we need to reformat we can 4908 04:06:25,960 --> 04:06:32,640 select these lines and press 4909 04:06:30,000 --> 04:06:36,159 tab so the value that is different for 4910 04:06:32,640 --> 04:06:39,399 each button is the move that we picked 4911 04:06:36,159 --> 04:06:42,080 so we're going to turn this move into a 4912 04:06:39,399 --> 04:06:45,159 parameter so at the top between the 4913 04:06:42,080 --> 04:06:50,279 brackets we're going to create a new 4914 04:06:45,159 --> 04:06:53,760 parameter let's name it Player 4915 04:06:50,279 --> 04:06:57,000 move and now let's save a value into 4916 04:06:53,760 --> 04:06:59,920 this parameter so inside our scissors 4917 04:06:57,000 --> 04:07:04,640 button we're going to call this function 4918 04:06:59,920 --> 04:07:08,359 and run all this code so let's type play 4919 04:07:04,640 --> 04:07:11,000 game and call this function and for the 4920 04:07:08,359 --> 04:07:14,680 scissors button let's save the string 4921 04:07:11,000 --> 04:07:18,040 scissors into Player move to do that 4922 04:07:14,680 --> 04:07:21,520 we'll type between the brackets here and 4923 04:07:18,040 --> 04:07:24,680 type the string 4924 04:07:21,520 --> 04:07:28,000 scissors so as we learned this will save 4925 04:07:24,680 --> 04:07:30,880 scissors into Player move and now we can 4926 04:07:28,000 --> 04:07:32,159 use Player move just like a variable in 4927 04:07:30,880 --> 04:07:35,040 the 4928 04:07:32,159 --> 04:07:38,319 function now let's modify the code based 4929 04:07:35,040 --> 04:07:41,720 on the parameter so here this code 4930 04:07:38,319 --> 04:07:45,040 calculates the result however this only 4931 04:07:41,720 --> 04:07:47,520 works if the player move is scissors so 4932 04:07:45,040 --> 04:07:51,239 we're going to put this in an if 4933 04:07:47,520 --> 04:07:52,760 statement so we'll type 4934 04:07:51,239 --> 04:07:57,800 if 4935 04:07:52,760 --> 04:08:01,359 Player move equals 4936 04:07:57,800 --> 04:08:03,960 scissors and curly brackets inside this 4937 04:08:01,359 --> 04:08:07,479 if statement we're going to put this if 4938 04:08:03,960 --> 04:08:09,399 statement so let's select this and then 4939 04:08:07,479 --> 04:08:12,040 right click 4940 04:08:09,399 --> 04:08:16,000 cut and then inside 4941 04:08:12,040 --> 04:08:20,600 here right click click and 4942 04:08:16,000 --> 04:08:23,479 paste and select these lines and press 4943 04:08:20,600 --> 04:08:26,920 tab so notice that we can put an if 4944 04:08:23,479 --> 04:08:29,640 statement inside another if statement 4945 04:08:26,920 --> 04:08:32,920 and we did this because this code only 4946 04:08:29,640 --> 04:08:35,840 makes sense if the player move is 4947 04:08:32,920 --> 04:08:39,319 scissors and finally let's modify the 4948 04:08:35,840 --> 04:08:41,119 code that displays the message so here 4949 04:08:39,319 --> 04:08:43,920 instead of displaying scissors every 4950 04:08:41,119 --> 04:08:45,880 time we're going to display whatever is 4951 04:08:43,920 --> 04:08:49,800 saved inside Player 4952 04:08:45,880 --> 04:08:52,640 move so we'll replace it with Dollar 4953 04:08:49,800 --> 04:08:57,319 open curly bracket close curly bracket 4954 04:08:52,640 --> 04:09:00,159 and in between we'll insert Player 4955 04:08:57,319 --> 04:09:03,080 move and that's it we modify this 4956 04:09:00,159 --> 04:09:04,399 function to use a parameter instead of 4957 04:09:03,080 --> 04:09:08,080 always using 4958 04:09:04,399 --> 04:09:09,840 scissors now let's save and click the 4959 04:09:08,080 --> 04:09:12,760 scissors 4960 04:09:09,840 --> 04:09:15,600 button and you'll notice that it works 4961 04:09:12,760 --> 04:09:18,359 we displayed the move scissors and we 4962 04:09:15,600 --> 04:09:21,279 calculated the result 4963 04:09:18,359 --> 04:09:23,920 correctly let's press okay and we're 4964 04:09:21,279 --> 04:09:26,600 going to use this function to reuse the 4965 04:09:23,920 --> 04:09:30,600 code for the other buttons so we'll 4966 04:09:26,600 --> 04:09:33,000 scroll up and for the paper button we're 4967 04:09:30,600 --> 04:09:36,040 going to use a different if statement to 4968 04:09:33,000 --> 04:09:39,000 calculate the result so let's actually 4969 04:09:36,040 --> 04:09:40,199 copy this into the function we're going 4970 04:09:39,000 --> 04:09:43,520 to 4971 04:09:40,199 --> 04:09:45,800 rightclick click copy 4972 04:09:43,520 --> 04:09:48,080 and then inside the 4973 04:09:45,800 --> 04:09:52,119 function we're going to create another 4974 04:09:48,080 --> 04:09:57,560 Branch if the player move is paper so 4975 04:09:52,119 --> 04:09:58,520 here let's type else if brackets and 4976 04:09:57,560 --> 04:10:01,439 curly 4977 04:09:58,520 --> 04:10:03,680 brackets inside this condition we're 4978 04:10:01,439 --> 04:10:10,199 going to check if the 4979 04:10:03,680 --> 04:10:12,960 player move is equal to paper this time 4980 04:10:10,199 --> 04:10:14,359 and then inside these curly brackets 4981 04:10:12,960 --> 04:10:17,520 we're going going to paste the if 4982 04:10:14,359 --> 04:10:20,479 statement we copied earlier so right 4983 04:10:17,520 --> 04:10:23,880 click and 4984 04:10:20,479 --> 04:10:26,439 paste and we can select these lines and 4985 04:10:23,880 --> 04:10:28,920 press tab to 4986 04:10:26,439 --> 04:10:32,159 reformat so now we have the code that 4987 04:10:28,920 --> 04:10:35,040 determines the result for paper down 4988 04:10:32,159 --> 04:10:37,040 here we don't need to modify this code 4989 04:10:35,040 --> 04:10:38,920 because we're always displaying whatever 4990 04:10:37,040 --> 04:10:42,199 is inside Player 4991 04:10:38,920 --> 04:10:43,920 move finally we can scroll up to the 4992 04:10:42,199 --> 04:10:47,600 paper button 4993 04:10:43,920 --> 04:10:51,720 and remove all this duplicated code and 4994 04:10:47,600 --> 04:10:56,319 just use our function play 4995 04:10:51,720 --> 04:10:59,640 game and we're going to pass paper 4996 04:10:56,319 --> 04:11:03,000 inside so now paper will get saved 4997 04:10:59,640 --> 04:11:04,840 inside Player move and it's going to run 4998 04:11:03,000 --> 04:11:07,000 this if statement 4999 04:11:04,840 --> 04:11:10,720 instead if we 5000 04:11:07,000 --> 04:11:14,239 save and click the paper 5001 04:11:10,720 --> 04:11:16,800 button in this example I pick paper 5002 04:11:14,239 --> 04:11:18,479 computer pick Rock and it calculated the 5003 04:11:16,800 --> 04:11:22,239 result 5004 04:11:18,479 --> 04:11:25,359 correctly let's press okay and use this 5005 04:11:22,239 --> 04:11:27,439 function for the rock button feel free 5006 04:11:25,359 --> 04:11:28,600 to pause the video if you want to try it 5007 04:11:27,439 --> 04:11:31,520 yourself 5008 04:11:28,600 --> 04:11:34,840 first so let's scroll up to the Rock 5009 04:11:31,520 --> 04:11:38,279 button and first we need to copy this if 5010 04:11:34,840 --> 04:11:40,359 statement into the function so let's 5011 04:11:38,279 --> 04:11:44,399 rightclick 5012 04:11:40,359 --> 04:11:50,319 copy and then inside the function 5013 04:11:44,399 --> 04:11:55,119 let's create a branch for rock else if 5014 04:11:50,319 --> 04:11:58,760 Player move is equal to 5015 04:11:55,119 --> 04:12:02,399 rock and then curly brackets and inside 5016 04:11:58,760 --> 04:12:04,479 here we're going to rightclick and paste 5017 04:12:02,399 --> 04:12:07,800 the if statement we copied 5018 04:12:04,479 --> 04:12:10,800 earlier and now let's select these lines 5019 04:12:07,800 --> 04:12:13,199 and press tab a few times to 5020 04:12:10,800 --> 04:12:14,319 reformat and again this doesn't need 5021 04:12:13,199 --> 04:12:17,800 need to 5022 04:12:14,319 --> 04:12:20,600 change the last step is to scroll up to 5023 04:12:17,800 --> 04:12:23,479 our Rock button and instead of 5024 04:12:20,600 --> 04:12:27,640 duplicating all of this code we're going 5025 04:12:23,479 --> 04:12:32,960 to remove it and just call our function 5026 04:12:27,640 --> 04:12:36,000 instead play game brackets and then 5027 04:12:32,960 --> 04:12:38,279 inside we'll have the string 5028 04:12:36,000 --> 04:12:41,840 Rock now let's 5029 04:12:38,279 --> 04:12:45,040 save and click the rock 5030 04:12:41,840 --> 04:12:47,239 button so I picked Rock the computer 5031 04:12:45,040 --> 04:12:48,880 picked paper and we calculated the 5032 04:12:47,239 --> 04:12:53,119 result 5033 04:12:48,880 --> 04:12:55,680 correctly let's press okay so by using a 5034 04:12:53,119 --> 04:12:59,319 function with a parameter we were able 5035 04:12:55,680 --> 04:13:01,319 to reuse all of the code in our buttons 5036 04:12:59,319 --> 04:13:05,520 and you can see that it made our code a 5037 04:13:01,319 --> 04:13:09,359 lot cleaner so lastly notice that inside 5038 04:13:05,520 --> 04:13:11,760 a function we can call other functions 5039 04:13:09,359 --> 04:13:14,960 let's go through the code step by step 5040 04:13:11,760 --> 04:13:18,000 so that we understand how how it works 5041 04:13:14,960 --> 04:13:21,239 so here when we call the play game 5042 04:13:18,000 --> 04:13:24,399 function we go inside here and then we 5043 04:13:21,239 --> 04:13:27,119 call the pick computer move function so 5044 04:13:24,399 --> 04:13:30,920 now we're going to go inside this 5045 04:13:27,119 --> 04:13:33,319 function and run all the code and then 5046 04:13:30,920 --> 04:13:37,279 when this function returns we're going 5047 04:13:33,319 --> 04:13:40,199 to return back to where we called it and 5048 04:13:37,279 --> 04:13:42,520 then keep going and run the rest of the 5049 04:13:40,199 --> 04:13:45,000 code and then when the play game 5050 04:13:42,520 --> 04:13:47,600 function finishes it's going to return 5051 04:13:45,000 --> 04:13:51,359 back to 5052 04:13:47,600 --> 04:13:52,600 here so inside a function we can call 5053 04:13:51,359 --> 04:13:54,640 other 5054 04:13:52,600 --> 04:13:58,040 functions now one last thing we're going 5055 04:13:54,640 --> 04:14:00,560 to do for this code is we'll scroll 5056 04:13:58,040 --> 04:14:03,439 down and we're just going to add some 5057 04:14:00,560 --> 04:14:05,920 new lines to this if statement to 5058 04:14:03,439 --> 04:14:08,880 separate the branches and make the code 5059 04:14:05,920 --> 04:14:08,880 a little easier to 5060 04:14:09,080 --> 04:14:14,000 read and 5061 04:14:11,520 --> 04:14:16,800 save and let's scroll 5062 04:14:14,000 --> 04:14:19,840 up and that's the end of this 5063 04:14:16,800 --> 04:14:23,479 lesson in this lesson we learned about 5064 04:14:19,840 --> 04:14:25,600 functions which let us reuse code we 5065 04:14:23,479 --> 04:14:28,720 learned how to get a value out of a 5066 04:14:25,600 --> 04:14:31,560 function using return we learn how to 5067 04:14:28,720 --> 04:14:34,319 put values into a function using 5068 04:14:31,560 --> 04:14:37,080 parameters and we improve the code for 5069 04:14:34,319 --> 04:14:39,199 our rock paper scissors project by 5070 04:14:37,080 --> 04:14:42,199 reusing the code and removing 5071 04:14:39,199 --> 04:14:42,199 duplication 5072 04:14:43,159 --> 04:14:47,319 thanks for watching this course so far 5073 04:14:45,560 --> 04:14:49,359 if you want to support this channel you 5074 04:14:47,319 --> 04:14:51,640 can check out the premium version of my 5075 04:14:49,359 --> 04:14:54,040 courses just like this one where you can 5076 04:14:51,640 --> 04:14:56,319 get a certificate of completion and a 5077 04:14:54,040 --> 04:14:59,119 Better Learning platform if you're 5078 04:14:56,319 --> 04:15:01,680 interested in extra videos from me more 5079 04:14:59,119 --> 04:15:04,399 in-depth videos and behind the scenes 5080 04:15:01,680 --> 04:15:07,119 content you can check out these links 5081 04:15:04,399 --> 04:15:09,920 here you can also support the channel by 5082 04:15:07,119 --> 04:15:12,640 liking the video subscribing and sharing 5083 04:15:09,920 --> 04:15:15,319 this video with others thanks again and 5084 04:15:12,640 --> 04:15:17,840 let's go continue with the 5085 04:15:15,319 --> 04:15:20,359 course here are some exercises you can 5086 04:15:17,840 --> 04:15:23,359 do on your own to practice using 5087 04:15:20,359 --> 04:15:23,359 functions 5088 04:15:48,080 --> 04:15:52,880 in this lesson we're going to learn 5089 04:15:49,880 --> 04:15:55,399 another type of value in JavaScript 5090 04:15:52,880 --> 04:15:58,439 called objects and we're going to use 5091 04:15:55,399 --> 04:16:01,560 objects to create a score in our rock 5092 04:15:58,439 --> 04:16:04,600 paper scissors project first let's 5093 04:16:01,560 --> 04:16:08,239 create a new file for this lesson we'll 5094 04:16:04,600 --> 04:16:11,640 go to our code editor and click this 5095 04:16:08,239 --> 04:16:16,399 icon and then click this icon to create 5096 04:16:11,640 --> 04:16:18,239 a new file and we'll name this file 5097 04:16:16,399 --> 04:16:19,840 08- 5098 04:16:18,239 --> 04:16:22,359 objects. 5099 04:16:19,840 --> 04:16:25,880 HTML and press 5100 04:16:22,359 --> 04:16:30,520 enter next we'll copy all the code in 5101 04:16:25,880 --> 04:16:33,439 functions. HTML to objects. HTML so 5102 04:16:30,520 --> 04:16:36,600 we'll type here and press contrl a or 5103 04:16:33,439 --> 04:16:38,760 command a to select the code and then 5104 04:16:36,600 --> 04:16:41,159 right click 5105 04:16:38,760 --> 04:16:43,399 copy and in 5106 04:16:41,159 --> 04:16:44,760 here right click 5107 04:16:43,399 --> 04:16:48,279 and 5108 04:16:44,760 --> 04:16:50,399 paste now let's hide the files for now 5109 04:16:48,279 --> 04:16:55,239 and we'll prepare this file for this 5110 04:16:50,399 --> 04:16:59,680 lesson so we can change this to objects 5111 04:16:55,239 --> 04:16:59,680 and remove all the code in the script 5112 04:16:59,800 --> 04:17:07,720 element finally let's save and open this 5113 04:17:04,119 --> 04:17:09,800 in live server by right clicking open 5114 04:17:07,720 --> 04:17:12,640 with live 5115 04:17:09,800 --> 04:17:16,279 server and let's also open the console 5116 04:17:12,640 --> 04:17:17,720 on on this page so we'll right click 5117 04:17:16,279 --> 04:17:21,119 click 5118 04:17:17,720 --> 04:17:24,640 inspect and click the 5119 04:17:21,119 --> 04:17:29,040 console let's close our previous 5120 04:17:24,640 --> 04:17:29,040 tabs and also the previous 5121 04:17:29,199 --> 04:17:36,199 code and now we're ready to learn 5122 04:17:31,960 --> 04:17:39,560 objects so what is an object an object 5123 04:17:36,199 --> 04:17:43,199 groups multiple values together let's do 5124 04:17:39,560 --> 04:17:45,920 an example and create our first object 5125 04:17:43,199 --> 04:17:50,319 we'll go to the script element and we'll 5126 04:17:45,920 --> 04:17:53,960 create a variable using const and we'll 5127 04:17:50,319 --> 04:17:57,359 name this variable product and make it 5128 04:17:53,960 --> 04:18:02,199 equal to open curly bracket close curly 5129 04:17:57,359 --> 04:18:05,439 bracket and semic it so this is an 5130 04:18:02,199 --> 04:18:10,279 object inside this object we can add 5131 04:18:05,439 --> 04:18:14,359 some values so we'll type enter and then 5132 04:18:10,279 --> 04:18:16,159 Name colon and then a value like the 5133 04:18:14,359 --> 04:18:20,279 string 5134 04:18:16,159 --> 04:18:25,319 socks and at the end we'll type a 5135 04:18:20,279 --> 04:18:29,279 comma and then price colon and another 5136 04:18:25,319 --> 04:18:32,600 value like the number 5137 04:18:29,279 --> 04:18:36,199 1,90 so inside our object we just added 5138 04:18:32,600 --> 04:18:39,560 two values so at the bottom here let's 5139 04:18:36,199 --> 04:18:42,560 try console. logging this object 5140 04:18:39,560 --> 04:18:42,560 console.log 5141 04:18:44,520 --> 04:18:50,040 product and 5142 04:18:46,760 --> 04:18:51,640 save and it will display both values 5143 04:18:50,040 --> 04:18:55,040 into the 5144 04:18:51,640 --> 04:18:58,880 console so as you can see an object lets 5145 04:18:55,040 --> 04:19:01,159 us group multiple values together so for 5146 04:18:58,880 --> 04:19:04,040 each of these values we have some code 5147 04:19:01,159 --> 04:19:06,920 on the left this code is called the 5148 04:19:04,040 --> 04:19:10,600 property and using the property we can 5149 04:19:06,920 --> 04:19:16,560 access specific values in the object for 5150 04:19:10,600 --> 04:19:16,560 example let's type and enter and 5151 04:19:16,920 --> 04:19:23,680 console.log we're going to type the 5152 04:19:19,080 --> 04:19:26,840 object product and then a DOT and then a 5153 04:19:23,680 --> 04:19:30,840 property like 5154 04:19:26,840 --> 04:19:33,279 name so dot name Will access the value 5155 04:19:30,840 --> 04:19:36,840 that is associated with the name 5156 04:19:33,279 --> 04:19:37,760 property which is the string socks so if 5157 04:19:36,840 --> 04:19:41,000 we 5158 04:19:37,760 --> 04:19:42,840 save it will display the string socks in 5159 04:19:41,000 --> 04:19:44,800 the console 5160 04:19:42,840 --> 04:19:47,359 let's do another example and we'll 5161 04:19:44,800 --> 04:19:52,840 access this second value using the 5162 04:19:47,359 --> 04:19:58,319 property price so we'll type enter 5163 04:19:52,840 --> 04:20:02,560 console. log the object product and then 5164 04:19:58,319 --> 04:20:05,880 a DOT and then a property 5165 04:20:02,560 --> 04:20:09,399 price so this will access the value 5166 04:20:05,880 --> 04:20:12,920 associated with price which is 5167 04:20:09,399 --> 04:20:15,239 1,090 if we save 5168 04:20:12,920 --> 04:20:18,520 that's what gets displayed in the 5169 04:20:15,239 --> 04:20:22,399 console now we can also use the property 5170 04:20:18,520 --> 04:20:25,119 to change a value inside an object to do 5171 04:20:22,399 --> 04:20:30,159 that we're going to type the object's 5172 04:20:25,119 --> 04:20:34,040 name product and then Dot and a property 5173 04:20:30,159 --> 04:20:40,359 like name and just make it equal to 5174 04:20:34,040 --> 04:20:42,880 something else like the string cotton 5175 04:20:40,359 --> 04:20:45,439 socks so this code will change change 5176 04:20:42,880 --> 04:20:49,159 the value associated with the name 5177 04:20:45,439 --> 04:20:53,239 property to cotton socks so at the 5178 04:20:49,159 --> 04:20:53,239 bottom if we type 5179 04:20:53,479 --> 04:20:58,199 console.log the product 5180 04:20:56,080 --> 04:21:01,159 again and 5181 04:20:58,199 --> 04:21:03,920 save you'll notice the value associated 5182 04:21:01,159 --> 04:21:04,880 with the name property was changed to 5183 04:21:03,920 --> 04:21:08,319 cotton 5184 04:21:04,880 --> 04:21:10,960 socks so that's how an object works we 5185 04:21:08,319 --> 04:21:13,479 group multiple values together and then 5186 04:21:10,960 --> 04:21:16,840 we can access or change these values 5187 04:21:13,479 --> 04:21:20,080 using the properties on the left now 5188 04:21:16,840 --> 04:21:22,640 let's learn the syntax rules for objects 5189 04:21:20,080 --> 04:21:25,640 to create an object we start with an 5190 04:21:22,640 --> 04:21:28,960 open curly bracket and end with a closed 5191 04:21:25,640 --> 04:21:30,640 curly bracket inside the object we can 5192 04:21:28,960 --> 04:21:33,640 put multiple 5193 04:21:30,640 --> 04:21:36,760 values for each value the code on the 5194 04:21:33,640 --> 04:21:40,439 left is called the property this is how 5195 04:21:36,760 --> 04:21:43,199 we access the value inside the object we 5196 04:21:40,439 --> 04:21:46,159 separate the property and value with a 5197 04:21:43,199 --> 04:21:49,560 colon and this is called a property 5198 04:21:46,159 --> 04:21:52,520 value pair we can have many property 5199 04:21:49,560 --> 04:21:54,159 value pairs in an object and we separate 5200 04:21:52,520 --> 04:21:57,040 them with a 5201 04:21:54,159 --> 04:22:00,239 comma to access a value inside the 5202 04:21:57,040 --> 04:22:03,239 object we type the object's name and 5203 04:22:00,239 --> 04:22:06,960 then Dot and then the property we want 5204 04:22:03,239 --> 04:22:10,080 to access like name this will give us 5205 04:22:06,960 --> 04:22:13,560 the value associated with the name 5206 04:22:10,080 --> 04:22:16,279 property this syntax is called do 5207 04:22:13,560 --> 04:22:19,159 notation if we access a property that 5208 04:22:16,279 --> 04:22:22,680 doesn't exist the value will be 5209 04:22:19,159 --> 04:22:26,040 undefined to change a value in an object 5210 04:22:22,680 --> 04:22:29,560 we can use the dot notation again so 5211 04:22:26,040 --> 04:22:32,880 product. name and just make it equal to 5212 04:22:29,560 --> 04:22:36,279 something else now using the syntax we 5213 04:22:32,880 --> 04:22:39,760 can also add a value to an object we can 5214 04:22:36,279 --> 04:22:43,520 type a property that doesn't exist for 5215 04:22:39,760 --> 04:22:47,199 example here we can type 5216 04:22:43,520 --> 04:22:49,760 product do new 5217 04:22:47,199 --> 04:22:53,239 property and make this equal to 5218 04:22:49,760 --> 04:22:56,520 something like the Boolean value 5219 04:22:53,239 --> 04:22:59,720 true if we 5220 04:22:56,520 --> 04:23:02,159 console.log the product 5221 04:22:59,720 --> 04:23:04,880 again and 5222 04:23:02,159 --> 04:23:07,880 save it will add this value to the 5223 04:23:04,880 --> 04:23:10,960 object along with the new 5224 04:23:07,880 --> 04:23:14,000 property we can also remove a value from 5225 04:23:10,960 --> 04:23:17,560 an object by typing 5226 04:23:14,000 --> 04:23:20,640 delete and then the object 5227 04:23:17,560 --> 04:23:25,239 product Dot and then the property we 5228 04:23:20,640 --> 04:23:29,000 want to delete like new 5229 04:23:25,239 --> 04:23:29,000 property if we 5230 04:23:29,479 --> 04:23:35,080 console.log the product 5231 04:23:32,600 --> 04:23:38,840 again and 5232 04:23:35,080 --> 04:23:39,920 save this code will delete new property 5233 04:23:38,840 --> 04:23:42,760 from the 5234 04:23:39,920 --> 04:23:45,920 object and lastly notice that then an 5235 04:23:42,760 --> 04:23:49,359 object is just another type of value we 5236 04:23:45,920 --> 04:23:53,040 can save an object inside a variable and 5237 04:23:49,359 --> 04:23:58,279 we can also console.log objects if we 5238 04:23:53,040 --> 04:24:00,520 check the type of this value so type of 5239 04:23:58,279 --> 04:24:02,960 product and 5240 04:24:00,520 --> 04:24:06,199 save and scroll up in the 5241 04:24:02,960 --> 04:24:09,920 console it will tell us that product is 5242 04:24:06,199 --> 04:24:13,319 an object so let's remove 5243 04:24:09,920 --> 04:24:16,800 this and Save 5244 04:24:13,319 --> 04:24:19,359 and now we'll learn why we use objects 5245 04:24:16,800 --> 04:24:21,960 so objects make our code more 5246 04:24:19,359 --> 04:24:24,800 organized instead of creating a bunch of 5247 04:24:21,960 --> 04:24:28,399 variables for the products values like 5248 04:24:24,800 --> 04:24:30,279 the name and the price and other details 5249 04:24:28,399 --> 04:24:34,560 we can just group all these related 5250 04:24:30,279 --> 04:24:37,399 values together into one object objects 5251 04:24:34,560 --> 04:24:40,239 also allow us to use multiple values 5252 04:24:37,399 --> 04:24:42,840 together instead of console. logging 5253 04:24:40,239 --> 04:24:45,720 each of these values individually we can 5254 04:24:42,840 --> 04:24:48,080 just console log the whole object and 5255 04:24:45,720 --> 04:24:51,359 display all the values at 5256 04:24:48,080 --> 04:24:54,359 once so objects let us group multiple 5257 04:24:51,359 --> 04:24:56,080 values together and let us use multiple 5258 04:24:54,359 --> 04:24:59,080 values 5259 04:24:56,080 --> 04:25:01,960 together next let's do a practical 5260 04:24:59,080 --> 04:25:05,319 example with objects we're going to add 5261 04:25:01,960 --> 04:25:07,199 a score to our rock paper scissors game 5262 04:25:05,319 --> 04:25:09,880 so I've actually prepared a project we 5263 04:25:07,199 --> 04:25:12,479 can use for this lesson to see this 5264 04:25:09,880 --> 04:25:14,640 project we're going to go to our website 5265 04:25:12,479 --> 04:25:19,000 and then create a new 5266 04:25:14,640 --> 04:25:26,319 tab and then up here we're going to type 5267 04:25:19,000 --> 04:25:29,279 super simple dodev SL projects 5268 04:25:26,319 --> 04:25:32,279 slobs and press 5269 04:25:29,279 --> 04:25:34,920 enter so this is our simplified rock 5270 04:25:32,279 --> 04:25:36,920 paper scissors game again except in this 5271 04:25:34,920 --> 04:25:39,640 project when we play the 5272 04:25:36,920 --> 04:25:43,960 game it's also going to keep track of a 5273 04:25:39,640 --> 04:25:47,279 score so how many times we win lose and 5274 04:25:43,960 --> 04:25:51,359 tie and also if we click 5275 04:25:47,279 --> 04:25:54,000 okay we have a reset score button so if 5276 04:25:51,359 --> 04:25:57,080 we play the game a few 5277 04:25:54,000 --> 04:26:00,119 times and add to our 5278 04:25:57,080 --> 04:26:03,000 score and then click the reset score 5279 04:26:00,119 --> 04:26:06,520 button this will reset the score back to 5280 04:26:03,000 --> 04:26:08,760 zero so if we play the game again the 5281 04:26:06,520 --> 04:26:11,199 score starts from zero 5282 04:26:08,760 --> 04:26:14,279 again so we're going to learn how to use 5283 04:26:11,199 --> 04:26:15,840 objects to create create this feature 5284 04:26:14,279 --> 04:26:19,439 let's press 5285 04:26:15,840 --> 04:26:22,439 okay and first let's create a copy of 5286 04:26:19,439 --> 04:26:25,159 our project just for this lesson we'll 5287 04:26:22,439 --> 04:26:28,000 go to our code editor and click this 5288 04:26:25,159 --> 04:26:30,680 icon to show our files and we'll make a 5289 04:26:28,000 --> 04:26:33,680 copy of the rock paper scissors project 5290 04:26:30,680 --> 04:26:35,840 from the previous lesson so we'll 5291 04:26:33,680 --> 04:26:40,880 rightclick and 5292 04:26:35,840 --> 04:26:45,080 copy and down here right click and 5293 04:26:40,880 --> 04:26:45,920 paste and we we right click and rename 5294 04:26:45,080 --> 04:26:49,920 this 5295 04:26:45,920 --> 04:26:54,199 file we'll remove the copy at the end 5296 04:26:49,920 --> 04:26:55,720 and at the front we'll change this to 08 5297 04:26:54,199 --> 04:26:59,640 and press 5298 04:26:55,720 --> 04:27:03,520 enter now let's hide these files and we 5299 04:26:59,640 --> 04:27:06,600 can close the 07 file for now and let's 5300 04:27:03,520 --> 04:27:10,359 open this in live server by right 5301 04:27:06,600 --> 04:27:13,359 clicking and open with live 5302 04:27:10,359 --> 04:27:15,239 server let's also open the console on 5303 04:27:13,359 --> 04:27:16,880 this page so 5304 04:27:15,239 --> 04:27:19,680 rightclick 5305 04:27:16,880 --> 04:27:22,520 inspect and the 5306 04:27:19,680 --> 04:27:26,239 console all right now let's go into our 5307 04:27:22,520 --> 04:27:28,920 code and add a score to our game so 5308 04:27:26,239 --> 04:27:31,640 remember when writing JavaScript a good 5309 04:27:28,920 --> 04:27:35,279 strategy is to think about what steps we 5310 04:27:31,640 --> 04:27:38,680 need to do this is called an algorithm 5311 04:27:35,279 --> 04:27:41,080 and then convert those steps into code 5312 04:27:38,680 --> 04:27:43,080 so we already had an algorithm for the 5313 04:27:41,080 --> 04:27:45,520 rock paper scissors game 5314 04:27:43,080 --> 04:27:49,119 let's update this algorithm to include 5315 04:27:45,520 --> 04:27:52,000 the score so first the computer randomly 5316 04:27:49,119 --> 04:27:54,800 selects a move this step will stay the 5317 04:27:52,000 --> 04:27:57,560 same number two we're going to compare 5318 04:27:54,800 --> 04:28:00,399 the moves to get the result this step 5319 04:27:57,560 --> 04:28:03,239 will also be the same but now we're 5320 04:28:00,399 --> 04:28:06,840 going to add an extra step after getting 5321 04:28:03,239 --> 04:28:09,720 the result we're going to update a score 5322 04:28:06,840 --> 04:28:13,119 and finally we'll display the result and 5323 04:28:09,720 --> 04:28:15,600 the score in a popup now let's convert 5324 04:28:13,119 --> 04:28:17,880 these updated steps or this algorithm 5325 04:28:15,600 --> 04:28:21,000 into code the first thing we need to 5326 04:28:17,880 --> 04:28:24,279 change is we added a third step we're 5327 04:28:21,000 --> 04:28:26,920 going to save and update a score so 5328 04:28:24,279 --> 04:28:29,800 first we need somewhere to save our 5329 04:28:26,920 --> 04:28:34,080 score so one place we can save it is 5330 04:28:29,800 --> 04:28:37,479 inside a variable let's go to our script 5331 04:28:34,080 --> 04:28:41,000 element and at the top we're going to 5332 04:28:37,479 --> 04:28:42,920 create a variable for our score using 5333 04:28:41,000 --> 04:28:46,920 const 5334 04:28:42,920 --> 04:28:50,199 score and make it equal to an 5335 04:28:46,920 --> 04:28:53,000 object and inside this object we're 5336 04:28:50,199 --> 04:28:55,840 going to have the property 5337 04:28:53,000 --> 04:28:59,479 wins so this will keep track of how many 5338 04:28:55,840 --> 04:29:02,680 wins that we have we'll type colon and 5339 04:28:59,479 --> 04:29:06,399 the number zero because the score starts 5340 04:29:02,680 --> 04:29:08,840 at zero and then a comma and then 5341 04:29:06,399 --> 04:29:11,640 another property 5342 04:29:08,840 --> 04:29:15,399 losses this will be how many losses we 5343 04:29:11,640 --> 04:29:21,840 have and then colon and zero and then 5344 04:29:15,399 --> 04:29:23,920 another comma and ties colon and zero so 5345 04:29:21,840 --> 04:29:26,760 we use an object here because these 5346 04:29:23,920 --> 04:29:28,720 values are related to each other you 5347 04:29:26,760 --> 04:29:30,840 could use separate variables but we're 5348 04:29:28,720 --> 04:29:33,080 using an object for 5349 04:29:30,840 --> 04:29:35,479 convenience another thing you'll notice 5350 04:29:33,080 --> 04:29:39,040 is that we're creating this variable 5351 04:29:35,479 --> 04:29:42,159 outside of a function and outside of a 5352 04:29:39,040 --> 04:29:45,000 scope and that's because every time we 5353 04:29:42,159 --> 04:29:48,239 make a move we need to update the score 5354 04:29:45,000 --> 04:29:51,359 from last time so in order to save the 5355 04:29:48,239 --> 04:29:53,920 score from last time we need to keep it 5356 04:29:51,359 --> 04:29:56,199 outside the function if the score was 5357 04:29:53,920 --> 04:29:59,000 inside the function the function would 5358 04:29:56,199 --> 04:30:02,159 create a new score every time which is 5359 04:29:59,000 --> 04:30:04,960 not what we want so in this situation in 5360 04:30:02,159 --> 04:30:07,439 order to save a score from last time 5361 04:30:04,960 --> 04:30:11,279 we're going to keep this variable 5362 04:30:07,439 --> 04:30:13,119 outside now every time we play the game 5363 04:30:11,279 --> 04:30:15,800 after we calc calate the result we're 5364 04:30:13,119 --> 04:30:18,319 going to update the score so let's 5365 04:30:15,800 --> 04:30:22,520 scroll down to where we calculate the 5366 04:30:18,319 --> 04:30:24,239 result which is here and then below it 5367 04:30:22,520 --> 04:30:27,359 we're going to type some new 5368 04:30:24,239 --> 04:30:29,960 lines and type the code for updating the 5369 04:30:27,359 --> 04:30:34,439 score so we can do that with an if 5370 04:30:29,960 --> 04:30:37,680 statement so we'll type if brackets and 5371 04:30:34,439 --> 04:30:39,880 curly brackets and inside the condition 5372 04:30:37,680 --> 04:30:42,439 we're going to check if the result is a 5373 04:30:39,880 --> 04:30:44,720 win if it is a win 5374 04:30:42,439 --> 04:30:48,800 we're going to update the winds property 5375 04:30:44,720 --> 04:30:51,479 and increase it by one so down 5376 04:30:48,800 --> 04:30:57,520 here if the 5377 04:30:51,479 --> 04:31:00,239 result is equal to the string you 5378 04:30:57,520 --> 04:31:03,319 win and make sure that this string 5379 04:31:00,239 --> 04:31:07,560 matches the string up here inside the 5380 04:31:03,319 --> 04:31:13,720 curly brackets we're going to update 5381 04:31:07,560 --> 04:31:16,000 score. wins and we'll make it equal to 5382 04:31:13,720 --> 04:31:19,560 itself plus 5383 04:31:16,000 --> 04:31:23,560 one so this is how we increase the winds 5384 04:31:19,560 --> 04:31:25,840 property in the score object by one now 5385 04:31:23,560 --> 04:31:27,960 remember from the variables lesson that 5386 04:31:25,840 --> 04:31:31,720 we have a shortcut for increasing 5387 04:31:27,960 --> 04:31:35,760 something by one and that shortcut 5388 04:31:31,720 --> 04:31:39,159 is plus = 5389 04:31:35,760 --> 04:31:42,560 1 next let's add another Branch to this 5390 04:31:39,159 --> 04:31:48,040 if statement for the other two results 5391 04:31:42,560 --> 04:31:50,119 so else if brackets and curly brackets 5392 04:31:48,040 --> 04:31:55,159 and in the second condition we're going 5393 04:31:50,119 --> 04:31:57,880 to check if result is equal to the 5394 04:31:55,159 --> 04:32:00,720 string you 5395 04:31:57,880 --> 04:32:04,279 lose and make sure this string matches 5396 04:32:00,720 --> 04:32:09,080 the string up here so if the result is 5397 04:32:04,279 --> 04:32:13,359 you lose we're going to update score do 5398 04:32:09,080 --> 04:32:15,640 losses property and increase it by 1 so 5399 04:32:13,359 --> 04:32:19,279 plus equal 5400 04:32:15,640 --> 04:32:25,640 1 and finally we'll check if the result 5401 04:32:19,279 --> 04:32:27,159 is a tie else if brackets and curly 5402 04:32:25,640 --> 04:32:32,040 brackets 5403 04:32:27,159 --> 04:32:35,439 result equals to the string 5404 04:32:32,040 --> 04:32:40,159 tie and here we'll update 5405 04:32:35,439 --> 04:32:42,880 score. ties plus equal 5406 04:32:40,159 --> 04:32:46,199 1 so we converted this step in the 5407 04:32:42,880 --> 04:32:49,600 algorithm to code the score starts at 5408 04:32:46,199 --> 04:32:51,600 zero and every time we play the game 5409 04:32:49,600 --> 04:32:55,119 after we calculate the result we're 5410 04:32:51,600 --> 04:32:58,119 going to update the score using this 5411 04:32:55,119 --> 04:33:00,640 code now let's convert the last step 5412 04:32:58,119 --> 04:33:03,520 into code we're going to display the 5413 04:33:00,640 --> 04:33:06,320 result and the score in the popup at the 5414 04:33:03,520 --> 04:33:08,359 end so let's actually go back to the 5415 04:33:06,320 --> 04:33:10,680 super simple dodev 5416 04:33:08,359 --> 04:33:13,639 project and we'll see what the popup 5417 04:33:10,680 --> 04:33:16,680 looks like we'll click one of these 5418 04:33:13,639 --> 04:33:20,840 buttons and in this popup on the second 5419 04:33:16,680 --> 04:33:23,840 line we display the score so in our 5420 04:33:20,840 --> 04:33:27,119 code this is the code that displays that 5421 04:33:23,840 --> 04:33:30,160 popup so how do we add a second line to 5422 04:33:27,119 --> 04:33:32,320 the popup remember that template strings 5423 04:33:30,160 --> 04:33:35,879 have another special feature called 5424 04:33:32,320 --> 04:33:38,760 multi-line strings so we can type at the 5425 04:33:35,879 --> 04:33:42,760 end of this template string and press 5426 04:33:38,760 --> 04:33:47,760 enter to add another line to this Str 5427 04:33:42,760 --> 04:33:50,320 so here we can type winds colon and 5428 04:33:47,760 --> 04:33:53,240 we're going to insert a value using 5429 04:33:50,320 --> 04:33:56,400 Dollar open curly bracket Clos curly 5430 04:33:53,240 --> 04:33:59,279 bracket and here we're going to insert 5431 04:33:56,400 --> 04:34:05,520 the number of wins that we have which is 5432 04:33:59,279 --> 04:34:08,119 score. wins so we'll type score 5433 04:34:05,520 --> 04:34:11,520 dowins next we're going to show the 5434 04:34:08,119 --> 04:34:12,279 number of losses that we have so after 5435 04:34:11,520 --> 04:34:17,561 this 5436 04:34:12,279 --> 04:34:19,119 let's type a comma and losses colon and 5437 04:34:17,561 --> 04:34:22,561 insert another 5438 04:34:19,119 --> 04:34:24,080 value here we'll insert 5439 04:34:22,561 --> 04:34:28,199 score. 5440 04:34:24,080 --> 04:34:32,039 losses and finally we'll type a comma 5441 04:34:28,199 --> 04:34:37,561 and we'll display ties colon and 5442 04:34:32,039 --> 04:34:40,639 inserted value score dot 5443 04:34:37,561 --> 04:34:43,359 ties and this will display our score in 5444 04:34:40,639 --> 04:34:47,520 the popup so let's save this and give it 5445 04:34:43,359 --> 04:34:51,000 a try we'll click okay and then go back 5446 04:34:47,520 --> 04:34:54,119 to our Tab and play the 5447 04:34:51,000 --> 04:34:57,600 game so now on the second line we will 5448 04:34:54,119 --> 04:34:59,799 display the score one small issue here 5449 04:34:57,600 --> 04:35:02,840 is that we have a lot of spaces on the 5450 04:34:59,799 --> 04:35:05,680 second line so these spaces correspond 5451 04:35:02,840 --> 04:35:08,439 to the spaces here when we use 5452 04:35:05,680 --> 04:35:11,320 multi-line strings these Extra Spaces 5453 04:35:08,439 --> 04:35:13,799 will show up in the popup so to remove 5454 04:35:11,320 --> 04:35:17,439 these SP cases we can just remove them 5455 04:35:13,799 --> 04:35:19,600 in the code like this now if we 5456 04:35:17,439 --> 04:35:23,359 save press 5457 04:35:19,600 --> 04:35:25,799 okay and play the game it will show our 5458 04:35:23,359 --> 04:35:29,680 score on the second line without those 5459 04:35:25,799 --> 04:35:32,320 Extra Spaces at the front let's press 5460 04:35:29,680 --> 04:35:35,039 okay we're going to add one more feature 5461 04:35:32,320 --> 04:35:39,639 to this project if we go back to Super 5462 04:35:35,039 --> 04:35:41,879 simple. Dev we have a reset score button 5463 04:35:39,639 --> 04:35:44,480 which will reset the score back to zero 5464 04:35:41,879 --> 04:35:47,199 when we click it let's add this button 5465 04:35:44,480 --> 04:35:50,240 in our project so we'll scroll 5466 04:35:47,199 --> 04:35:54,240 up and inside the 5467 04:35:50,240 --> 04:35:58,119 HTML We'll add a button with the code 5468 04:35:54,240 --> 04:36:02,600 less than button greater than and inside 5469 04:35:58,119 --> 04:36:06,080 the button we'll have the text reset 5470 04:36:02,600 --> 04:36:09,959 score and now when we click this button 5471 04:36:06,080 --> 04:36:13,719 we'll run some code using the onclick 5472 04:36:09,959 --> 04:36:16,400 attribute equals double quotes and then 5473 04:36:13,719 --> 04:36:19,000 in here we're going to write some code 5474 04:36:16,400 --> 04:36:22,400 to reset this score back to 5475 04:36:19,000 --> 04:36:25,080 zero so one way we can do it is because 5476 04:36:22,400 --> 04:36:27,920 this score is an object we can use dot 5477 04:36:25,080 --> 04:36:31,160 notation to change everything back to 5478 04:36:27,920 --> 04:36:36,680 zero so we can type 5479 04:36:31,160 --> 04:36:38,520 score. wins and make it equal to zero 5480 04:36:36,680 --> 04:36:43,799 we'll also type 5481 04:36:38,520 --> 04:36:45,279 score. losses and make it equal to Z and 5482 04:36:43,799 --> 04:36:49,760 finally 5483 04:36:45,279 --> 04:36:51,959 score. ties and make it equal to 5484 04:36:49,760 --> 04:36:54,600 zero now let's 5485 04:36:51,959 --> 04:36:58,920 save and go back to our 5486 04:36:54,600 --> 04:37:01,160 Tab and play the game a few times to get 5487 04:36:58,920 --> 04:37:01,160 a 5488 04:37:01,959 --> 04:37:10,160 score let's press okay and press reset 5489 04:37:06,400 --> 04:37:12,000 score and then play the game again and 5490 04:37:10,160 --> 04:37:15,199 you'll notice that the score started 5491 04:37:12,000 --> 04:37:18,320 from zero again so that's how we create 5492 04:37:15,199 --> 04:37:20,760 the reset score button we just set all 5493 04:37:18,320 --> 04:37:23,799 of these properties back to 5494 04:37:20,760 --> 04:37:26,279 zero and that's it that's how we add a 5495 04:37:23,799 --> 04:37:30,320 score feature to our rock paper scissors 5496 04:37:26,279 --> 04:37:32,561 project using an object to save the 5497 04:37:30,320 --> 04:37:34,760 score let's press 5498 04:37:32,561 --> 04:37:38,600 okay and we're going to learn more 5499 04:37:34,760 --> 04:37:40,080 details about objects let's go back to 5500 04:37:38,600 --> 04:37:43,680 objects. 5501 04:37:40,080 --> 04:37:46,400 HTML and what open the tab for this 5502 04:37:43,680 --> 04:37:49,080 file and let's also comment out the 5503 04:37:46,400 --> 04:37:51,240 previous code using 5504 04:37:49,080 --> 04:37:53,639 slashstar and at the 5505 04:37:51,240 --> 04:37:55,600 bottom star 5506 04:37:53,639 --> 04:37:58,840 slash and 5507 04:37:55,600 --> 04:38:00,199 save now let's scroll to the bottom and 5508 04:37:58,840 --> 04:38:03,520 we'll do some 5509 04:38:00,199 --> 04:38:08,639 setup so first let's create a variable 5510 04:38:03,520 --> 04:38:12,320 with const we'll name it Product 2 and 5511 04:38:08,639 --> 04:38:15,719 make it equal to another object 5512 04:38:12,320 --> 04:38:19,639 inside this object we have the property 5513 04:38:15,719 --> 04:38:22,600 Name colon and the string 5514 04:38:19,639 --> 04:38:26,199 shirt and we're going to console.log 5515 04:38:22,600 --> 04:38:26,199 this object so 5516 04:38:26,920 --> 04:38:34,160 console.log Product 2 and 5517 04:38:31,879 --> 04:38:37,240 save so the next detail we're going to 5518 04:38:34,160 --> 04:38:38,959 learn about objects is another way to 5519 04:38:37,240 --> 04:38:41,639 access the values 5520 04:38:38,959 --> 04:38:46,959 inside we previously learned the 5521 04:38:41,639 --> 04:38:46,959 notation to do this for example 5522 04:38:47,199 --> 04:38:53,000 console.log product 5523 04:38:50,719 --> 04:38:56,000 to. 5524 04:38:53,000 --> 04:38:58,600 name this will access the value 5525 04:38:56,000 --> 04:39:01,439 associated with the name 5526 04:38:58,600 --> 04:39:04,799 property in JavaScript we have another 5527 04:39:01,439 --> 04:39:07,920 way of accessing values inside objects 5528 04:39:04,799 --> 04:39:11,920 which is using square brackets so we'll 5529 04:39:07,920 --> 04:39:13,039 create a new line and type console 5530 04:39:11,920 --> 04:39:18,199 do 5531 04:39:13,039 --> 04:39:19,320 log product 2 and this time we'll type 5532 04:39:18,199 --> 04:39:21,480 square 5533 04:39:19,320 --> 04:39:25,879 brackets and then inside the square 5534 04:39:21,480 --> 04:39:30,480 brackets we'll have a string with 5535 04:39:25,879 --> 04:39:32,480 name so this is called bracket notation 5536 04:39:30,480 --> 04:39:35,600 and this does the same thing as dot 5537 04:39:32,480 --> 04:39:40,040 notation up here it gets the value 5538 04:39:35,600 --> 04:39:43,520 associated with the name property if we 5539 04:39:40,040 --> 04:39:47,480 save these two lines of code both access 5540 04:39:43,520 --> 04:39:49,798 the string shirt which we see here so 5541 04:39:47,480 --> 04:39:52,718 when do we use the bracket notation 5542 04:39:49,798 --> 04:39:55,680 instead of the dot notation bracket 5543 04:39:52,718 --> 04:39:59,240 notation lets us use properties that 5544 04:39:55,680 --> 04:40:04,878 normally don't work with do notation for 5545 04:39:59,240 --> 04:40:04,878 example at the bottom we can type 5546 04:40:05,240 --> 04:40:15,520 console.log Product 2 and then use a DOT 5547 04:40:10,360 --> 04:40:15,520 and try to access a property called 5548 04:40:16,878 --> 04:40:23,400 delivery-time if we 5549 04:40:19,080 --> 04:40:25,360 save this property will give us an error 5550 04:40:23,400 --> 04:40:27,878 and that's because JavaScript thinks 5551 04:40:25,360 --> 04:40:31,520 that this Dash is a minus sign and it's 5552 04:40:27,878 --> 04:40:33,958 trying to do math so in order to use a 5553 04:40:31,520 --> 04:40:36,520 property like this we have to use 5554 04:40:33,958 --> 04:40:40,080 bracket notation so we're going to 5555 04:40:36,520 --> 04:40:44,160 replace the dot with square 5556 04:40:40,080 --> 04:40:46,760 brackets and and turn this into a 5557 04:40:44,160 --> 04:40:50,638 string now if we 5558 04:40:46,760 --> 04:40:53,200 save it no longer gives us an error and 5559 04:40:50,638 --> 04:40:56,680 because delivery time does not exist in 5560 04:40:53,200 --> 04:40:59,600 the object we get the value 5561 04:40:56,680 --> 04:41:02,280 undefined so using bracket notation we 5562 04:40:59,600 --> 04:41:06,280 can also add values to the object when 5563 04:41:02,280 --> 04:41:09,440 we create it so here we can add a 5564 04:41:06,280 --> 04:41:15,400 comma and we'll add another property 5565 04:41:09,440 --> 04:41:15,400 using bracket notation and a string 5566 04:41:16,000 --> 04:41:22,760 delivery-time and a colon and then a 5567 04:41:19,080 --> 04:41:24,878 value like the string one 5568 04:41:22,760 --> 04:41:28,680 day if we 5569 04:41:24,878 --> 04:41:32,480 save this lets us add a delivery-time 5570 04:41:28,680 --> 04:41:35,080 property to this object so as you can 5571 04:41:32,480 --> 04:41:37,680 see bracket notation lets us use 5572 04:41:35,080 --> 04:41:38,840 properties that normally don't work with 5573 04:41:37,680 --> 04:41:41,400 DOT 5574 04:41:38,840 --> 04:41:44,400 notation another feature of bracket 5575 04:41:41,400 --> 04:41:47,120 notation is that between the brackets we 5576 04:41:44,400 --> 04:41:50,480 don't just have to use a string we can 5577 04:41:47,120 --> 04:41:52,280 use a variable a calculation or anything 5578 04:41:50,480 --> 04:41:54,760 that results in a 5579 04:41:52,280 --> 04:41:58,638 value so you might be wondering which 5580 04:41:54,760 --> 04:42:02,000 one should we use do notation or bracket 5581 04:41:58,638 --> 04:42:04,520 notation we usually use do notation by 5582 04:42:02,000 --> 04:42:07,200 default because it's shorter and easier 5583 04:42:04,520 --> 04:42:10,040 to read but if we need to use properties 5584 04:42:07,200 --> 04:42:11,840 that don't work with do notation then we 5585 04:42:10,040 --> 04:42:15,000 use bracket not 5586 04:42:11,840 --> 04:42:17,840 notation now a shortcut here is when we 5587 04:42:15,000 --> 04:42:20,520 create the object if we only have a 5588 04:42:17,840 --> 04:42:23,000 string between the brackets the brackets 5589 04:42:20,520 --> 04:42:28,638 are actually optional so we can just 5590 04:42:23,000 --> 04:42:31,600 type this if we save the code will still 5591 04:42:28,638 --> 04:42:34,600 work okay the next detail we're going to 5592 04:42:31,600 --> 04:42:38,680 learn is that inside an object we can 5593 04:42:34,600 --> 04:42:41,718 save any type of value so this includes 5594 04:42:38,680 --> 04:42:44,120 numbers strings booleans 5595 04:42:41,718 --> 04:42:47,200 as well as other objects because 5596 04:42:44,120 --> 04:42:51,638 remember objects themselves are also 5597 04:42:47,200 --> 04:42:54,718 values for example we can type a comma 5598 04:42:51,638 --> 04:42:58,760 and add a property called 5599 04:42:54,718 --> 04:43:02,080 rating colon and then an object open 5600 04:42:58,760 --> 04:43:05,798 curly bracket close curly bracket and 5601 04:43:02,080 --> 04:43:12,798 inside this inner object we can add more 5602 04:43:05,798 --> 04:43:16,040 properties like stars colon 4.5 5603 04:43:12,798 --> 04:43:20,638 this is how many stars a product has and 5604 04:43:16,040 --> 04:43:22,320 then a comma and a property called count 5605 04:43:20,638 --> 04:43:25,600 colon 5606 04:43:22,320 --> 04:43:30,200 87 this is how many ratings a product 5607 04:43:25,600 --> 04:43:33,958 has so this is called a nested object or 5608 04:43:30,200 --> 04:43:36,680 an object inside an object we can use 5609 04:43:33,958 --> 04:43:39,878 nested objects to make our values more 5610 04:43:36,680 --> 04:43:42,480 organized so inside the product the 5611 04:43:39,878 --> 04:43:45,440 number of stars that has and the number 5612 04:43:42,480 --> 04:43:48,638 of ratings that it has are both related 5613 04:43:45,440 --> 04:43:51,920 to a rating so it makes sense to group 5614 04:43:48,638 --> 04:43:55,240 them together further inside the 5615 04:43:51,920 --> 04:43:57,760 product now to access values inside the 5616 04:43:55,240 --> 04:44:00,878 inner object we just use the dot 5617 04:43:57,760 --> 04:44:04,920 notation or bracket notation multiple 5618 04:44:00,878 --> 04:44:07,920 times for example at the bottom we can 5619 04:44:04,920 --> 04:44:07,920 type 5620 04:44:09,000 --> 04:44:14,638 console.log product 5621 04:44:11,760 --> 04:44:17,560 2 dot 5622 04:44:14,638 --> 04:44:21,080 rating this will get the value 5623 04:44:17,560 --> 04:44:24,600 associated with rating and this is an 5624 04:44:21,080 --> 04:44:26,160 object so we can use the dot notation 5625 04:44:24,600 --> 04:44:29,798 again 5626 04:44:26,160 --> 04:44:33,040 count so this will get the count inside 5627 04:44:29,798 --> 04:44:38,480 the rating object which is this value 5628 04:44:33,040 --> 04:44:41,638 right here so if we save it will display 5629 04:44:38,480 --> 04:44:45,400 87 and finally another type Ty of value 5630 04:44:41,638 --> 04:44:49,080 we can put in an object is a function so 5631 04:44:45,400 --> 04:44:51,000 in this object let's type comma and 5632 04:44:49,080 --> 04:44:54,520 another property 5633 04:44:51,000 --> 04:44:57,798 fun colon and we're going to create a 5634 04:44:54,520 --> 04:45:01,280 function here so let's type 5635 04:44:57,798 --> 04:45:05,798 function and we'll name the function 5636 04:45:01,280 --> 04:45:07,680 Function One brackets and curly brackets 5637 04:45:05,798 --> 04:45:11,040 and then inside the function we're going 5638 04:45:07,680 --> 04:45:12,840 to have some JavaScript code like usual 5639 04:45:11,040 --> 04:45:17,280 so 5640 04:45:12,840 --> 04:45:21,680 console. log the string 5641 04:45:17,280 --> 04:45:21,680 function inside 5642 04:45:21,718 --> 04:45:28,520 object this saves a function inside the 5643 04:45:25,360 --> 04:45:31,320 object and this works because a function 5644 04:45:28,520 --> 04:45:34,160 is actually just another type of value 5645 04:45:31,320 --> 04:45:36,878 in JavaScript but we'll learn more about 5646 04:45:34,160 --> 04:45:40,200 this feature of functions later in this 5647 04:45:36,878 --> 04:45:43,080 course for now just know that functions 5648 04:45:40,200 --> 04:45:46,760 are also values and we can save them in 5649 04:45:43,080 --> 04:45:50,560 an object to access this function we can 5650 04:45:46,760 --> 04:45:51,718 use the dot or bracket notation as usual 5651 04:45:50,560 --> 04:45:58,040 so at the 5652 04:45:51,718 --> 04:46:01,638 bottom we can type the object Product 2 5653 04:45:58,040 --> 04:46:04,840 and then a DOT and then the property 5654 04:46:01,638 --> 04:46:07,760 fun so this will access the value 5655 04:46:04,840 --> 04:46:10,958 associated with the fun property which 5656 04:46:07,760 --> 04:46:15,760 is a function and because this is a fun 5657 04:46:10,958 --> 04:46:17,798 function we can call it as usual using 5658 04:46:15,760 --> 04:46:22,000 brackets if we 5659 04:46:17,798 --> 04:46:25,120 save this will run the function here and 5660 04:46:22,000 --> 04:46:27,718 it will display function inside object 5661 04:46:25,120 --> 04:46:30,760 into the 5662 04:46:27,718 --> 04:46:34,040 console so when we save functions inside 5663 04:46:30,760 --> 04:46:36,040 an object this is called a method and 5664 04:46:34,040 --> 04:46:38,200 we've actually been using methods this 5665 04:46:36,040 --> 04:46:41,400 whole time 5666 04:46:38,200 --> 04:46:44,360 console.log is actually a method 5667 04:46:41,400 --> 04:46:48,080 console is an object provided by 5668 04:46:44,360 --> 04:46:50,558 JavaScript and log is a function that is 5669 04:46:48,080 --> 04:46:56,600 saved inside the console 5670 04:46:50,558 --> 04:46:56,600 object if we go to the bottom and type 5671 04:46:56,878 --> 04:47:04,000 console.log type of 5672 04:47:01,440 --> 04:47:07,080 console and 5673 04:47:04,000 --> 04:47:12,840 save this will confirm that console is 5674 04:47:07,080 --> 04:47:12,840 just an object and if we change this to 5675 04:47:12,958 --> 04:47:18,680 console.log and 5676 04:47:15,240 --> 04:47:22,718 save this will show us that console.log 5677 04:47:18,680 --> 04:47:26,320 is a function so log is a function that 5678 04:47:22,718 --> 04:47:29,840 is saved inside the console object so we 5679 04:47:26,320 --> 04:47:33,680 call this a method another example of a 5680 04:47:29,840 --> 04:47:36,958 method is math.random which we used in 5681 04:47:33,680 --> 04:47:40,160 the rock paper scissors project math is 5682 04:47:36,958 --> 04:47:43,400 also an object provided by JavaScript 5683 04:47:40,160 --> 04:47:47,280 and random is a function saved inside 5684 04:47:43,400 --> 04:47:50,320 the math object so we call math.random a 5685 04:47:47,280 --> 04:47:53,320 method as well so we've actually been 5686 04:47:50,320 --> 04:47:55,958 using objects and methods all throughout 5687 04:47:53,320 --> 04:47:59,160 this course and now we understand all 5688 04:47:55,958 --> 04:48:01,760 the code that we've been using so far 5689 04:47:59,160 --> 04:48:04,558 next we're going to learn about builtin 5690 04:48:01,760 --> 04:48:08,280 objects so the console object and the 5691 04:48:04,558 --> 04:48:09,958 math object are known as builtin objects 5692 04:48:08,280 --> 04:48:12,600 because they are built in to the 5693 04:48:09,958 --> 04:48:15,200 language they provided by the 5694 04:48:12,600 --> 04:48:18,798 language we're going to learn two more 5695 04:48:15,200 --> 04:48:21,920 built-in objects called Json and local 5696 04:48:18,798 --> 04:48:25,400 storage and we will use these objects to 5697 04:48:21,920 --> 04:48:28,040 improve our rock paper scissors project 5698 04:48:25,400 --> 04:48:30,878 let's start with the Json built-in 5699 04:48:28,040 --> 04:48:32,360 object this object helps us work with 5700 04:48:30,878 --> 04:48:36,440 something called 5701 04:48:32,360 --> 04:48:38,958 Json Json stands for JavaScript object 5702 04:48:36,440 --> 04:48:42,718 notation the easiest way to understand 5703 04:48:38,958 --> 04:48:45,400 Jason is it's BAS basically a syntax 5704 04:48:42,718 --> 04:48:49,040 it's a similar syntax to a JavaScript 5705 04:48:45,400 --> 04:48:51,680 object but it has less features for 5706 04:48:49,040 --> 04:48:54,920 example the code on the left uses 5707 04:48:51,680 --> 04:48:58,680 JavaScript object syntax and the code on 5708 04:48:54,920 --> 04:49:00,798 the right uses Json syntax so one 5709 04:48:58,680 --> 04:49:04,520 difference is that all properties and 5710 04:49:00,798 --> 04:49:07,400 strings must use double quotes in Json 5711 04:49:04,520 --> 04:49:09,878 Jason does not support single quotes 5712 04:49:07,400 --> 04:49:12,920 another difference is that Json does not 5713 04:49:09,878 --> 04:49:16,480 support function functions so as you can 5714 04:49:12,920 --> 04:49:19,320 see Json is basically a syntax that's 5715 04:49:16,480 --> 04:49:20,840 similar to a JavaScript object syntax 5716 04:49:19,320 --> 04:49:24,280 but with less 5717 04:49:20,840 --> 04:49:26,760 features so why would we use Json syntax 5718 04:49:24,280 --> 04:49:29,718 instead of a JavaScript object 5719 04:49:26,760 --> 04:49:31,000 syntax a JavaScript object only makes 5720 04:49:29,718 --> 04:49:34,558 sense in 5721 04:49:31,000 --> 04:49:36,878 JavaScript on the other hand Json syntax 5722 04:49:34,558 --> 04:49:40,040 can be understood by almost every 5723 04:49:36,878 --> 04:49:41,200 programming language so Json syntax is 5724 04:49:40,040 --> 04:49:44,440 more un 5725 04:49:41,200 --> 04:49:47,680 Universal for this reason we use Json 5726 04:49:44,440 --> 04:49:49,440 when we send data between two computers 5727 04:49:47,680 --> 04:49:51,680 that might use different programming 5728 04:49:49,440 --> 04:49:55,040 languages we'll learn how to do this 5729 04:49:51,680 --> 04:49:57,558 later in the course we also use Json 5730 04:49:55,040 --> 04:49:59,680 when we store data we'll learn how to 5731 04:49:57,558 --> 04:50:02,558 store data in this 5732 04:49:59,680 --> 04:50:06,120 lesson now that we understand what Json 5733 04:50:02,558 --> 04:50:09,600 is let's learn about the built in Json 5734 04:50:06,120 --> 04:50:14,040 object the Json object helps us convert 5735 04:50:09,600 --> 04:50:17,520 a JavaScript object to Json for example 5736 04:50:14,040 --> 04:50:20,120 let's convert our object product two to 5737 04:50:17,520 --> 04:50:26,558 Json so we'll create some new 5738 04:50:20,120 --> 04:50:30,440 lines and we'll type the Json object do 5739 04:50:26,558 --> 04:50:33,638 stringify and Open Bracket close bracket 5740 04:50:30,440 --> 04:50:36,440 so stringify is a method of the Json 5741 04:50:33,638 --> 04:50:39,520 object so it's a function that is saved 5742 04:50:36,440 --> 04:50:41,280 inside this object and we can call this 5743 04:50:39,520 --> 04:50:43,760 function using 5744 04:50:41,280 --> 04:50:46,440 brackets between the brackets we can 5745 04:50:43,760 --> 04:50:50,200 give it a JavaScript object we want to 5746 04:50:46,440 --> 04:50:54,638 convert like Product 5747 04:50:50,200 --> 04:50:56,958 2 this will convert the object into Json 5748 04:50:54,638 --> 04:51:00,798 so now let's 5749 04:50:56,958 --> 04:51:00,798 console. log the 5750 04:51:01,520 --> 04:51:07,160 result and 5751 04:51:04,400 --> 04:51:10,680 save and in the console it will display 5752 04:51:07,160 --> 04:51:13,080 the product using Json syntax if we we 5753 04:51:10,680 --> 04:51:15,558 scroll up you'll notice that the 5754 04:51:13,080 --> 04:51:18,120 function that we saved in the object is 5755 04:51:15,558 --> 04:51:20,840 not inside the Json and that's because 5756 04:51:18,120 --> 04:51:23,718 Json does not support 5757 04:51:20,840 --> 04:51:26,360 functions so one thing to note about 5758 04:51:23,718 --> 04:51:31,878 stringify is that it actually results in 5759 04:51:26,360 --> 04:51:33,878 a string so at the front if we type type 5760 04:51:31,878 --> 04:51:36,320 of and 5761 04:51:33,878 --> 04:51:39,040 save it will tell us that the result 5762 04:51:36,320 --> 04:51:41,718 that it gives is actually a 5763 04:51:39,040 --> 04:51:44,240 string so if we remove 5764 04:51:41,718 --> 04:51:47,878 this and 5765 04:51:44,240 --> 04:51:50,920 save now this Json string is ready to be 5766 04:51:47,878 --> 04:51:51,878 sent to another computer or to be saved 5767 04:51:50,920 --> 04:51:54,840 in 5768 04:51:51,878 --> 04:51:57,958 storage next let's learn how to convert 5769 04:51:54,840 --> 04:52:01,120 the other way from a Json back to a 5770 04:51:57,958 --> 04:52:03,878 JavaScript object to do that we'll use 5771 04:52:01,120 --> 04:52:07,240 another method in the built-in Json 5772 04:52:03,878 --> 04:52:11,718 object called json.parse 5773 04:52:07,240 --> 04:52:13,200 at the end we'll type Jon 5774 04:52:11,718 --> 04:52:15,440 dop 5775 04:52:13,200 --> 04:52:18,040 pars and 5776 04:52:15,440 --> 04:52:20,280 brackets and between the brackets we can 5777 04:52:18,040 --> 04:52:22,878 put a Json string that we want to 5778 04:52:20,280 --> 04:52:26,000 convert so we actually have a Json 5779 04:52:22,878 --> 04:52:29,280 string up here so let's just make a copy 5780 04:52:26,000 --> 04:52:31,040 of this we'll select it 5781 04:52:29,280 --> 04:52:34,280 rightclick 5782 04:52:31,040 --> 04:52:40,080 copy and let's create an extra variable 5783 04:52:34,280 --> 04:52:46,280 to make it more clear const Json string 5784 04:52:40,080 --> 04:52:46,280 and make get equal to right click and 5785 04:52:46,558 --> 04:52:52,638 paste and now let's convert this Json 5786 04:52:49,160 --> 04:52:54,798 string back into an object by putting it 5787 04:52:52,638 --> 04:52:58,638 into 5788 04:52:54,798 --> 04:53:02,120 pars and finally we console.log this 5789 04:52:58,638 --> 04:53:02,120 result so 5790 04:53:05,240 --> 04:53:11,400 console.log and 5791 04:53:07,718 --> 04:53:15,718 save and json. pars will turn the Json 5792 04:53:11,400 --> 04:53:18,920 string back into a JavaScript object so 5793 04:53:15,718 --> 04:53:21,680 that's the built-in Json object it helps 5794 04:53:18,920 --> 04:53:24,558 us convert back and forth between a 5795 04:53:21,680 --> 04:53:27,160 JavaScript object and 5796 04:53:24,558 --> 04:53:30,600 Json now we're going to learn the next 5797 04:53:27,160 --> 04:53:33,520 built-in object called local storage 5798 04:53:30,600 --> 04:53:36,440 local storage is used to save values 5799 04:53:33,520 --> 04:53:38,680 more permanently so far we've been 5800 04:53:36,440 --> 04:53:41,520 saving our values in 5801 04:53:38,680 --> 04:53:44,400 variables however variables are 5802 04:53:41,520 --> 04:53:47,718 temporary they only exist on the current 5803 04:53:44,400 --> 04:53:51,320 page if we refresh the page or close the 5804 04:53:47,718 --> 04:53:54,120 page all the variables are deleted for 5805 04:53:51,320 --> 04:53:55,160 example if we go back to our rock paper 5806 04:53:54,120 --> 04:53:58,600 scissors 5807 04:53:55,160 --> 04:54:01,840 project and open the tab for this 5808 04:53:58,600 --> 04:54:06,558 project and we play a few 5809 04:54:01,840 --> 04:54:10,638 games and play it again so we have two 5810 04:54:06,558 --> 04:54:14,120 games if we refresh the page 5811 04:54:10,638 --> 04:54:15,878 notice the score starts from zero again 5812 04:54:14,120 --> 04:54:18,718 and that's because when we refresh the 5813 04:54:15,878 --> 04:54:22,200 page all of these variables are deleted 5814 04:54:18,718 --> 04:54:25,280 and the score gets reset back to zero 5815 04:54:22,200 --> 04:54:26,680 however if we press okay and go to 5816 04:54:25,280 --> 04:54:30,520 superimple 5817 04:54:26,680 --> 04:54:31,440 dodev and we refresh the page and we 5818 04:54:30,520 --> 04:54:34,360 play the 5819 04:54:31,440 --> 04:54:36,920 game notice that the score doesn't start 5820 04:54:34,360 --> 04:54:40,600 from zero it starts from the previous 5821 04:54:36,920 --> 04:54:42,920 score even though we refreshed the page 5822 04:54:40,600 --> 04:54:45,680 and that's because this project saves 5823 04:54:42,920 --> 04:54:47,760 the score in local storage and local 5824 04:54:45,680 --> 04:54:49,120 storage doesn't get deleted when we 5825 04:54:47,760 --> 04:54:52,120 refresh the 5826 04:54:49,120 --> 04:54:56,878 page now let's learn how to use local 5827 04:54:52,120 --> 04:54:59,280 storage in our project let's press okay 5828 04:54:56,878 --> 04:55:02,840 and we'll go back to our code and we'll 5829 04:54:59,280 --> 04:55:06,878 scroll down to where we update the 5830 04:55:02,840 --> 04:55:09,920 score so here after we update the score 5831 04:55:06,878 --> 04:55:11,920 we're going to save it in local storage 5832 04:55:09,920 --> 04:55:15,360 but first first let's learn how local 5833 04:55:11,920 --> 04:55:19,718 storage works so at the bottom here 5834 04:55:15,360 --> 04:55:21,400 we're going to type the local capital S 5835 04:55:19,718 --> 04:55:24,480 Storage 5836 04:55:21,400 --> 04:55:28,760 object to save a value inside local 5837 04:55:24,480 --> 04:55:34,600 storage it has a method called set item 5838 04:55:28,760 --> 04:55:38,080 so we can type dot set capital I item 5839 04:55:34,600 --> 04:55:40,798 and brackets because this is a method or 5840 04:55:38,080 --> 04:55:43,240 a function saved inside the object 5841 04:55:40,798 --> 04:55:46,680 object and we're going to give two 5842 04:55:43,240 --> 04:55:49,520 strings to this method the first string 5843 04:55:46,680 --> 04:55:52,798 is a name this is how we're going to 5844 04:55:49,520 --> 04:55:55,280 access the value that we save later so 5845 04:55:52,798 --> 04:55:59,320 let's use the string 5846 04:55:55,280 --> 04:56:02,080 message as the name and we'll type a 5847 04:55:59,320 --> 04:56:05,320 comma and then we'll type the value that 5848 04:56:02,080 --> 04:56:08,520 we want to save into local storage so 5849 04:56:05,320 --> 04:56:11,080 local storage only supports strings so 5850 04:56:08,520 --> 04:56:13,798 let's save the string 5851 04:56:11,080 --> 04:56:15,798 hello inside local 5852 04:56:13,798 --> 04:56:19,680 storage if we 5853 04:56:15,798 --> 04:56:20,638 save and then go back to our Tab and 5854 04:56:19,680 --> 04:56:23,878 play the 5855 04:56:20,638 --> 04:56:26,920 game it will run all this code and save 5856 04:56:23,878 --> 04:56:28,958 this string into local storage now let's 5857 04:56:26,920 --> 04:56:32,638 learn how to get this string out of 5858 04:56:28,958 --> 04:56:35,080 local storage we'll press okay and we'll 5859 04:56:32,638 --> 04:56:38,360 scroll to the 5860 04:56:35,080 --> 04:56:40,000 top and when we first load the page 5861 04:56:38,360 --> 04:56:44,040 we're going to get that value out of 5862 04:56:40,000 --> 04:56:46,520 local local storage by typing local 5863 04:56:44,040 --> 04:56:49,480 capital S 5864 04:56:46,520 --> 04:56:51,878 storage. getet item this 5865 04:56:49,480 --> 04:56:55,120 time and 5866 04:56:51,878 --> 04:56:57,798 brackets so the get item method gets a 5867 04:56:55,120 --> 04:57:00,520 value out of local storage and we're 5868 04:56:57,798 --> 04:57:03,280 going to give it one string so this 5869 04:57:00,520 --> 04:57:05,760 string is the name that we used earlier 5870 04:57:03,280 --> 04:57:07,840 which was message so we'll type the 5871 04:57:05,760 --> 04:57:11,280 string 5872 04:57:07,840 --> 04:57:14,798 message now let's console log this 5873 04:57:11,280 --> 04:57:14,798 result so 5874 04:57:16,718 --> 04:57:21,958 console.log and a bracket at the 5875 04:57:19,638 --> 04:57:24,718 end and 5876 04:57:21,958 --> 04:57:28,200 save it will get the value that we saved 5877 04:57:24,718 --> 04:57:31,160 earlier out of local storage so we saved 5878 04:57:28,200 --> 04:57:33,200 the string hello and that's exactly what 5879 04:57:31,160 --> 04:57:36,200 gets displayed in the 5880 04:57:33,200 --> 04:57:38,240 console so using local storage we were 5881 04:57:36,200 --> 04:57:41,480 able to get the value that we saved 5882 04:57:38,240 --> 04:57:44,920 earlier even though we refresh the 5883 04:57:41,480 --> 04:57:47,718 page now let's save our score into local 5884 04:57:44,920 --> 04:57:52,520 storage so it doesn't get reset when we 5885 04:57:47,718 --> 04:57:52,520 refresh the page so let's scroll to the 5886 04:57:52,680 --> 04:57:57,638 bottom and instead of using the name 5887 04:57:55,160 --> 04:58:01,440 message we're going to use the name 5888 04:57:57,638 --> 04:58:02,558 score to save our score and instead of 5889 04:58:01,440 --> 04:58:06,400 the string 5890 04:58:02,558 --> 04:58:09,160 hello we're going to save the score 5891 04:58:06,400 --> 04:58:12,280 object now one problem here is that 5892 04:58:09,160 --> 04:58:15,040 local storage only supports strings so 5893 04:58:12,280 --> 04:58:17,400 we need to convert our object into a 5894 04:58:15,040 --> 04:58:20,638 string and we actually learned how to do 5895 04:58:17,400 --> 04:58:24,718 this earlier using json.stringify 5896 04:58:20,638 --> 04:58:27,120 so at the front we can type 5897 04:58:24,718 --> 04:58:30,878 json. 5898 04:58:27,120 --> 04:58:33,558 stringify and then brackets around the 5899 04:58:30,878 --> 04:58:36,798 score and this will convert the 5900 04:58:33,558 --> 04:58:39,320 JavaScript object score into a Json 5901 04:58:36,798 --> 04:58:41,320 string and then it's ready to be saved 5902 04:58:39,320 --> 04:58:45,638 in local store 5903 04:58:41,320 --> 04:58:48,718 so if we save this and then play the 5904 04:58:45,638 --> 04:58:50,760 game it will run this code and save our 5905 04:58:48,718 --> 04:58:53,040 score into local 5906 04:58:50,760 --> 04:58:57,200 storage now let's press 5907 04:58:53,040 --> 04:58:57,200 okay and then's scroll up to the 5908 04:58:57,440 --> 04:59:02,638 top and now instead of getting the 5909 04:58:59,958 --> 04:59:05,798 message we're going to get the score out 5910 04:59:02,638 --> 04:59:08,798 of local storage if we 5911 04:59:05,798 --> 04:59:11,200 save now when we load the page we're 5912 04:59:08,798 --> 04:59:13,878 able to get the score that we saved from 5913 04:59:11,200 --> 04:59:16,840 last time as a Json 5914 04:59:13,878 --> 04:59:19,480 string the last step is instead of 5915 04:59:16,840 --> 04:59:21,320 resetting the score to zero every time 5916 04:59:19,480 --> 04:59:24,920 we're just going to use the score from 5917 04:59:21,320 --> 04:59:28,040 local storage before we do that remember 5918 04:59:24,920 --> 04:59:30,760 that this is just a string so we need to 5919 04:59:28,040 --> 04:59:33,200 convert this back to an 5920 04:59:30,760 --> 04:59:38,400 object we also learned how to do this 5921 04:59:33,200 --> 04:59:41,558 earlier using json. pars so at the 5922 04:59:38,400 --> 04:59:45,798 front we can type 5923 04:59:41,558 --> 04:59:49,440 json. pars and then Open Bracket and at 5924 04:59:45,798 --> 04:59:53,200 the end a close bracket and it will 5925 04:59:49,440 --> 04:59:55,760 convert this back to an object if we 5926 04:59:53,200 --> 04:59:59,638 save it will convert the score from a 5927 04:59:55,760 --> 05:00:02,718 Json string back to an object so now we 5928 04:59:59,638 --> 05:00:06,760 can use it up here so instead of doing 5929 05:00:02,718 --> 05:00:11,600 this we'll remove it and replace it with 5930 05:00:06,760 --> 05:00:16,000 this code so right click cut 5931 05:00:11,600 --> 05:00:19,840 and then here right click and 5932 05:00:16,000 --> 05:00:19,840 paste and we'll remove 5933 05:00:21,000 --> 05:00:26,798 console.log and 5934 05:00:23,558 --> 05:00:30,160 save so now when we load the page it 5935 05:00:26,798 --> 05:00:31,600 will load the score from local storage 5936 05:00:30,160 --> 05:00:34,360 if we play the 5937 05:00:31,600 --> 05:00:36,600 game notice that the score doesn't start 5938 05:00:34,360 --> 05:00:40,040 from zero it starts from the score that 5939 05:00:36,600 --> 05:00:44,558 we had last time let's review what we 5940 05:00:40,040 --> 05:00:48,320 did step by step first when we update 5941 05:00:44,558 --> 05:00:50,600 our score in this code after we update 5942 05:00:48,320 --> 05:00:54,400 the score we're going to save it in 5943 05:00:50,600 --> 05:00:57,440 local storage using local storage. set 5944 05:00:54,400 --> 05:01:00,400 item now local storage only supports 5945 05:00:57,440 --> 05:01:05,360 strings so we have to convert our score 5946 05:01:00,400 --> 05:01:07,920 object into a Json string using json. 5947 05:01:05,360 --> 05:01:11,400 stringify and then after we've saved 5948 05:01:07,920 --> 05:01:14,798 this into local storage when we load the 5949 05:01:11,400 --> 05:01:17,160 page up here we're going to load the 5950 05:01:14,798 --> 05:01:21,958 score that we just saved from local 5951 05:01:17,160 --> 05:01:24,958 storage using local storage. getet item 5952 05:01:21,958 --> 05:01:27,120 and the score was saved as a Json string 5953 05:01:24,958 --> 05:01:30,558 so we need to convert it back into an 5954 05:01:27,120 --> 05:01:34,120 object using json.parse 5955 05:01:30,558 --> 05:01:36,480 so that's how we use the Json object and 5956 05:01:34,120 --> 05:01:38,200 the local storage object to save our 5957 05:01:36,480 --> 05:01:40,920 score more 5958 05:01:38,200 --> 05:01:43,200 permanently let's press okay 5959 05:01:40,920 --> 05:01:46,360 okay and the last feature we're going to 5960 05:01:43,200 --> 05:01:49,280 add is when we click the reset score 5961 05:01:46,360 --> 05:01:52,400 button we also need to remove the score 5962 05:01:49,280 --> 05:01:55,040 from local storage otherwise if we press 5963 05:01:52,400 --> 05:01:58,240 reset score and 5964 05:01:55,040 --> 05:02:00,400 refresh and then play the game it will 5965 05:01:58,240 --> 05:02:03,040 just grab the old score from local 5966 05:02:00,400 --> 05:02:07,600 storage so reset score doesn't really 5967 05:02:03,040 --> 05:02:11,638 work here let's press okay and fix this 5968 05:02:07,600 --> 05:02:14,638 issue so here in the set score button at 5969 05:02:11,638 --> 05:02:18,120 the end we're going to remove the score 5970 05:02:14,638 --> 05:02:19,798 from local storage so we'll type 5971 05:02:18,120 --> 05:02:24,120 local 5972 05:02:19,798 --> 05:02:27,798 storage and then a method called do 5973 05:02:24,120 --> 05:02:30,400 remove item and 5974 05:02:27,798 --> 05:02:32,638 brackets and between the brackets we're 5975 05:02:30,400 --> 05:02:34,958 going to tell local storage which value 5976 05:02:32,638 --> 05:02:37,718 we want to remove so we're going to 5977 05:02:34,958 --> 05:02:40,558 remove the score so we'll type the 5978 05:02:37,718 --> 05:02:41,638 string score 5979 05:02:40,558 --> 05:02:45,600 if we 5980 05:02:41,638 --> 05:02:48,760 save and we click the reset button and 5981 05:02:45,600 --> 05:02:49,798 then we refresh the page and try to play 5982 05:02:48,760 --> 05:02:52,718 the game 5983 05:02:49,798 --> 05:02:55,878 again unfortunately it now gives us an 5984 05:02:52,718 --> 05:02:58,040 error so what is happening here so 5985 05:02:55,878 --> 05:03:00,680 remember that we remov the score from 5986 05:02:58,040 --> 05:03:04,760 local storage so now when we load the 5987 05:03:00,680 --> 05:03:07,160 page this actually doesn't exist when 5988 05:03:04,760 --> 05:03:09,400 something doesn't exist in local storage 5989 05:03:07,160 --> 05:03:11,040 it's going to give us a value called 5990 05:03:09,400 --> 05:03:12,280 null 5991 05:03:11,040 --> 05:03:15,400 and at the 5992 05:03:12,280 --> 05:03:18,878 bottom if we try to access any property 5993 05:03:15,400 --> 05:03:21,360 on null it will give us an error so 5994 05:03:18,878 --> 05:03:24,958 that's what we see here so how do we 5995 05:03:21,360 --> 05:03:28,360 solve this problem if we scroll 5996 05:03:24,958 --> 05:03:31,400 up one way to solve this problem is that 5997 05:03:28,360 --> 05:03:34,400 if there is no score in local storage 5998 05:03:31,400 --> 05:03:37,320 we're going to give it a default value 5999 05:03:34,400 --> 05:03:39,320 let's look at how we can do that so 6000 05:03:37,320 --> 05:03:42,638 after this line we're going to check if 6001 05:03:39,320 --> 05:03:47,520 the score is null we can do that using 6002 05:03:42,638 --> 05:03:51,920 an if statement if brackets and curly 6003 05:03:47,520 --> 05:03:54,558 brackets and we'll check if score is 6004 05:03:51,920 --> 05:03:57,558 equal to 6005 05:03:54,558 --> 05:04:00,558 null and if it is equal to null we're 6006 05:03:57,558 --> 05:04:01,958 going to give this a default score so 6007 05:04:00,558 --> 05:04:06,480 inside the curly 6008 05:04:01,958 --> 05:04:10,000 brackets we'll just make the score equal 6009 05:04:06,480 --> 05:04:11,400 to the default object we had earlier 6010 05:04:10,000 --> 05:04:15,240 which was 6011 05:04:11,400 --> 05:04:18,558 wins zero and then 6012 05:04:15,240 --> 05:04:20,080 losses zero and 6013 05:04:18,558 --> 05:04:23,160 ties 6014 05:04:20,080 --> 05:04:25,680 zero and remember if we need to reassign 6015 05:04:23,160 --> 05:04:29,480 a variable we have to change it from 6016 05:04:25,680 --> 05:04:32,558 const to let if we 6017 05:04:29,480 --> 05:04:35,400 save the score will be null because we 6018 05:04:32,558 --> 05:04:38,120 removed it earlier and now this will 6019 05:04:35,400 --> 05:04:40,360 give it a default score so everything 6020 05:04:38,120 --> 05:04:41,600 should work as before 6021 05:04:40,360 --> 05:04:44,520 if we play the 6022 05:04:41,600 --> 05:04:47,120 game you'll notice that the score starts 6023 05:04:44,520 --> 05:04:49,718 from zero or this default score and 6024 05:04:47,120 --> 05:04:51,958 everything is working again so that's 6025 05:04:49,718 --> 05:04:54,558 how we create the score feature in the 6026 05:04:51,958 --> 05:04:56,400 rock paper scissors project as well as 6027 05:04:54,558 --> 05:04:59,080 the reset score 6028 05:04:56,400 --> 05:05:02,558 feature next we're going to use some 6029 05:04:59,080 --> 05:05:04,600 shortcuts to simplify our code so these 6030 05:05:02,558 --> 05:05:08,840 shortcuts are things that we learned in 6031 05:05:04,600 --> 05:05:10,320 the Boolean lesson so remember that null 6032 05:05:08,840 --> 05:05:13,040 is a falsy 6033 05:05:10,320 --> 05:05:16,440 value that means it will behave just 6034 05:05:13,040 --> 05:05:19,240 like false so a shortcut we can use here 6035 05:05:16,440 --> 05:05:24,080 is instead of checking if the score is 6036 05:05:19,240 --> 05:05:27,440 equal to null we can also remove this 6037 05:05:24,080 --> 05:05:31,120 and check if not 6038 05:05:27,440 --> 05:05:34,600 score so not score does the same thing 6039 05:05:31,120 --> 05:05:39,080 as checking if it's null if the score is 6040 05:05:34,600 --> 05:05:42,200 null not score will flip it into true so 6041 05:05:39,080 --> 05:05:45,558 both of these will return true if the 6042 05:05:42,200 --> 05:05:49,040 score exists it's an object then both of 6043 05:05:45,558 --> 05:05:52,480 these will return false so instead of 6044 05:05:49,040 --> 05:05:56,040 checking if a value is equal to null we 6045 05:05:52,480 --> 05:05:58,400 can just use the not operator as a 6046 05:05:56,040 --> 05:06:01,520 shortcut now also from the boolean's 6047 05:05:58,400 --> 05:06:05,000 lesson we have an even shorter shortcut 6048 05:06:01,520 --> 05:06:08,440 for doing this so notice here that this 6049 05:06:05,000 --> 05:06:11,680 code basically sets a default value if 6050 05:06:08,440 --> 05:06:14,360 the score doesn't exist in the boolean's 6051 05:06:11,680 --> 05:06:17,200 lesson we learned a technique called the 6052 05:06:14,360 --> 05:06:21,520 default operator which is exactly what 6053 05:06:17,200 --> 05:06:26,160 we need here so on this line we can 6054 05:06:21,520 --> 05:06:30,280 remove the semicolon and type 6055 05:06:26,160 --> 05:06:33,840 or and then a default value so we want 6056 05:06:30,280 --> 05:06:36,920 our default value to be this object so 6057 05:06:33,840 --> 05:06:40,040 let's select it and then 6058 05:06:36,920 --> 05:06:45,120 rightclick copy 6059 05:06:40,040 --> 05:06:48,558 and then here right click and 6060 05:06:45,120 --> 05:06:52,558 paste and I'll select these lines and 6061 05:06:48,558 --> 05:06:55,718 press shift tab to remove and indent and 6062 05:06:52,558 --> 05:06:58,600 I'll add a semicolon back at the 6063 05:06:55,718 --> 05:07:02,160 end so the way that the default operator 6064 05:06:58,600 --> 05:07:05,000 works is that if the left side is truthy 6065 05:07:02,160 --> 05:07:08,160 so in this case we have a score it will 6066 05:07:05,000 --> 05:07:11,600 use the left side if the left side is 6067 05:07:08,160 --> 05:07:14,120 falsy or in this case the score is null 6068 05:07:11,600 --> 05:07:17,520 then we will use the right side as a 6069 05:07:14,120 --> 05:07:21,320 default value so this does the same 6070 05:07:17,520 --> 05:07:24,480 thing as this code below but it's just a 6071 05:07:21,320 --> 05:07:27,240 shortcut so now let's comment out this 6072 05:07:24,480 --> 05:07:31,160 code using 6073 05:07:27,240 --> 05:07:33,000 slashstar and at the bottom start slash 6074 05:07:31,160 --> 05:07:35,760 so we'll leave it here just for 6075 05:07:33,000 --> 05:07:40,000 reference and then 6076 05:07:35,760 --> 05:07:41,240 save and if we click reset score again 6077 05:07:40,000 --> 05:07:43,600 and 6078 05:07:41,240 --> 05:07:47,240 refresh and play the 6079 05:07:43,600 --> 05:07:49,840 game it will still work so this is a 6080 05:07:47,240 --> 05:07:52,680 perfect situation to use the default 6081 05:07:49,840 --> 05:07:55,360 operator to simplify our 6082 05:07:52,680 --> 05:07:57,558 code so let's press 6083 05:07:55,360 --> 05:08:00,080 okay and that's all we're going to do 6084 05:07:57,558 --> 05:08:03,040 for this project in this 6085 05:08:00,080 --> 05:08:06,040 lesson now let's move on and learn more 6086 05:08:03,040 --> 05:08:09,958 details about objects the first thing 6087 05:08:06,040 --> 05:08:12,798 we'll learn is the value null so null is 6088 05:08:09,958 --> 05:08:15,840 one of the falsy values in JavaScript 6089 05:08:12,798 --> 05:08:18,000 it's similar to the value undefined they 6090 05:08:15,840 --> 05:08:20,840 both represent that something doesn't 6091 05:08:18,000 --> 05:08:24,638 have a value so what's the difference 6092 05:08:20,840 --> 05:08:26,958 between null and undefined we use null 6093 05:08:24,638 --> 05:08:30,240 when we intentionally want something to 6094 05:08:26,958 --> 05:08:33,160 be empty for example in the previous 6095 05:08:30,240 --> 05:08:35,840 lesson we learned that if a function has 6096 05:08:33,160 --> 05:08:38,840 parameters we can give the parameter a 6097 05:08:35,840 --> 05:08:40,000 default value if the parameter is 6098 05:08:38,840 --> 05:08:43,440 undefined 6099 05:08:40,000 --> 05:08:46,280 it will use the default value however if 6100 05:08:43,440 --> 05:08:48,798 we set this parameter to null it will 6101 05:08:46,280 --> 05:08:52,200 not use the default value and it will 6102 05:08:48,798 --> 05:08:55,200 just use null so null means we 6103 05:08:52,200 --> 05:08:58,120 intentionally want something to be 6104 05:08:55,200 --> 05:09:02,240 empty in most cases though null and 6105 05:08:58,120 --> 05:09:04,200 undefined work the same way okay so the 6106 05:09:02,240 --> 05:09:06,160 next detail about objects we're going to 6107 05:09:04,200 --> 05:09:09,718 learn is called 6108 05:09:06,160 --> 05:09:13,000 autoboxing let's go back to objects. HT 6109 05:09:09,718 --> 05:09:15,680 HML and open the tab for this 6110 05:09:13,000 --> 05:09:17,958 file and let's also comment out the 6111 05:09:15,680 --> 05:09:20,160 previous code so our console doesn't get 6112 05:09:17,958 --> 05:09:22,520 too messy so 6113 05:09:20,160 --> 05:09:26,520 slashstar and at the 6114 05:09:22,520 --> 05:09:29,440 bottom star slash and 6115 05:09:26,520 --> 05:09:32,240 save so let's scroll 6116 05:09:29,440 --> 05:09:35,638 down we learned in this lesson that 6117 05:09:32,240 --> 05:09:37,840 objects can have properties and methods 6118 05:09:35,638 --> 05:09:40,320 so you might be surprised to learn that 6119 05:09:37,840 --> 05:09:44,280 other values also have have properties 6120 05:09:40,320 --> 05:09:46,878 and methods for example at the bottom 6121 05:09:44,280 --> 05:09:48,600 we're going to type 6122 05:09:46,878 --> 05:09:51,718 console. 6123 05:09:48,600 --> 05:09:55,360 log the string 6124 05:09:51,718 --> 05:09:58,520 hello and then the dot notation and a 6125 05:09:55,360 --> 05:10:01,638 property called 6126 05:09:58,520 --> 05:10:04,400 length so strings can also have 6127 05:10:01,638 --> 05:10:07,120 properties the length property tells us 6128 05:10:04,400 --> 05:10:11,400 how many characters or letters are in 6129 05:10:07,120 --> 05:10:15,798 this string so hello has five letters so 6130 05:10:11,400 --> 05:10:21,760 if we save the length property will be 6131 05:10:15,798 --> 05:10:25,558 five here's another example we'll type 6132 05:10:21,760 --> 05:10:29,360 console.log the string hello and then 6133 05:10:25,558 --> 05:10:36,520 the dot notation and a method this time 6134 05:10:29,360 --> 05:10:39,638 2 capital u upper Capital C case and 6135 05:10:36,520 --> 05:10:42,000 brackets so this string also has a 6136 05:10:39,638 --> 05:10:44,600 method called two 6137 05:10:42,000 --> 05:10:47,440 uppercase and this method gives us a 6138 05:10:44,600 --> 05:10:49,240 copy of the string with all letters 6139 05:10:47,440 --> 05:10:53,080 capitalized or in 6140 05:10:49,240 --> 05:10:55,160 uppercase if we save it will give us 6141 05:10:53,080 --> 05:10:58,600 hello in capital 6142 05:10:55,160 --> 05:11:01,280 letters so as you can see simple values 6143 05:10:58,600 --> 05:11:02,440 like strings can also have properties 6144 05:11:01,280 --> 05:11:05,160 and 6145 05:11:02,440 --> 05:11:08,280 methods so now you might be wondering 6146 05:11:05,160 --> 05:11:11,878 these are strings not objects how can 6147 05:11:08,280 --> 05:11:13,920 strings have properties and methods the 6148 05:11:11,878 --> 05:11:17,718 answer is that JavaScript has a special 6149 05:11:13,920 --> 05:11:20,440 feature called Auto boxing JavaScript 6150 05:11:17,718 --> 05:11:23,718 automatically wraps this string in a 6151 05:11:20,440 --> 05:11:27,080 special object first and this object has 6152 05:11:23,718 --> 05:11:29,840 the property length and the method to 6153 05:11:27,080 --> 05:11:32,360 uppercase so this is done automatically 6154 05:11:29,840 --> 05:11:35,000 we don't see the special object that's 6155 05:11:32,360 --> 05:11:36,840 wrapped around this string so this 6156 05:11:35,000 --> 05:11:39,200 feature is called 6157 05:11:36,840 --> 05:11:41,760 autoboxing because JavaScript automat 6158 05:11:39,200 --> 05:11:43,520 automatically wraps the string in an 6159 05:11:41,760 --> 05:11:46,878 object like a 6160 05:11:43,520 --> 05:11:49,680 box so autoboxing also works with other 6161 05:11:46,878 --> 05:11:52,280 types of values like numbers and 6162 05:11:49,680 --> 05:11:55,200 booleans the only thing is that auto 6163 05:11:52,280 --> 05:11:58,520 boxing does not work with null and 6164 05:11:55,200 --> 05:12:00,680 undefined it would just give us an 6165 05:11:58,520 --> 05:12:02,840 error The Last Detail we're going to 6166 05:12:00,680 --> 05:12:07,080 learn about objects is a really 6167 05:12:02,840 --> 05:12:10,120 important detail and that is objects are 6168 05:12:07,080 --> 05:12:13,958 references to demonstrate let's create a 6169 05:12:10,120 --> 05:12:19,718 new variable with an object so 6170 05:12:13,958 --> 05:12:23,080 const object one and make it equal to an 6171 05:12:19,718 --> 05:12:24,680 object inside this object let's have a 6172 05:12:23,080 --> 05:12:29,200 property 6173 05:12:24,680 --> 05:12:31,798 message colon and the string 6174 05:12:29,200 --> 05:12:34,958 hello so you might think that when we 6175 05:12:31,798 --> 05:12:37,718 create an object like this we create a 6176 05:12:34,958 --> 05:12:40,920 value that has other values inside and 6177 05:12:37,718 --> 05:12:43,280 we save the whole thing and a variable 6178 05:12:40,920 --> 05:12:46,558 however this actually works a little 6179 05:12:43,280 --> 05:12:49,240 differently when we create this object 6180 05:12:46,558 --> 05:12:52,680 the actual value is created somewhere in 6181 05:12:49,240 --> 05:12:54,840 the computer's memory and this variable 6182 05:12:52,680 --> 05:12:56,600 actually just gets a reference that 6183 05:12:54,840 --> 05:12:59,200 points to that location in the 6184 05:12:56,600 --> 05:13:02,080 computer's memory this is like a 6185 05:12:59,200 --> 05:13:04,240 shortcut on your computer a shortcut 6186 05:13:02,080 --> 05:13:07,440 points to where the actual file or 6187 05:13:04,240 --> 05:13:09,638 folder is so there's no way to actually 6188 05:13:07,440 --> 05:13:12,160 see this reference but just just know 6189 05:13:09,638 --> 05:13:14,480 that this is a reference and the object 6190 05:13:12,160 --> 05:13:17,240 is somewhere else in the computer's 6191 05:13:14,480 --> 05:13:19,878 memory so the reason it works this way 6192 05:13:17,240 --> 05:13:23,040 is to make the language more efficient 6193 05:13:19,878 --> 05:13:27,360 for example below this line we can 6194 05:13:23,040 --> 05:13:33,200 create a variable const and call it 6195 05:13:27,360 --> 05:13:36,638 object 2 and make it equal to object 6196 05:13:33,200 --> 05:13:40,040 one so this code doesn't actually make a 6197 05:13:36,638 --> 05:13:42,840 copy of the object it this makes a copy 6198 05:13:40,040 --> 05:13:45,320 of the reference that points to the 6199 05:13:42,840 --> 05:13:48,200 object so this makes a language more 6200 05:13:45,320 --> 05:13:51,040 efficient because if we had a really big 6201 05:13:48,200 --> 05:13:54,040 object here it's not good to copy the 6202 05:13:51,040 --> 05:13:57,080 entire object over and over again 6203 05:13:54,040 --> 05:14:00,480 instead Javas just copies the reference 6204 05:13:57,080 --> 05:14:02,080 which is much faster this is called copy 6205 05:14:00,480 --> 05:14:04,240 by 6206 05:14:02,080 --> 05:14:06,920 reference now that we learned that 6207 05:14:04,240 --> 05:14:09,840 objects are actually references let's 6208 05:14:06,920 --> 05:14:13,160 learn some interesting behaviors of 6209 05:14:09,840 --> 05:14:16,200 references so at the bottom here we're 6210 05:14:13,160 --> 05:14:19,360 going to change the property message in 6211 05:14:16,200 --> 05:14:22,600 this object we can do that by typing the 6212 05:14:19,360 --> 05:14:27,000 object object 6213 05:14:22,600 --> 05:14:32,280 1. message property and make it equal to 6214 05:14:27,000 --> 05:14:35,480 something else like the string good 6215 05:14:32,280 --> 05:14:40,400 job and now if we 6216 05:14:35,480 --> 05:14:40,400 console. log object one 6217 05:14:40,520 --> 05:14:44,798 and 6218 05:14:41,878 --> 05:14:47,040 save notice that we were able to change 6219 05:14:44,798 --> 05:14:50,718 the message property even though we 6220 05:14:47,040 --> 05:14:53,120 created object one with const and that's 6221 05:14:50,718 --> 05:14:56,440 because this variable just contains a 6222 05:14:53,120 --> 05:14:58,718 reference that points to the object so 6223 05:14:56,440 --> 05:15:01,320 const prevents us from changing the 6224 05:14:58,718 --> 05:15:03,240 reference in the variable but it doesn't 6225 05:15:01,320 --> 05:15:05,480 prevent us from reaching into the 6226 05:15:03,240 --> 05:15:08,240 reference and changing the object that 6227 05:15:05,480 --> 05:15:10,600 it's pointing to so just be careful when 6228 05:15:08,240 --> 05:15:13,040 you're working with with objects even 6229 05:15:10,600 --> 05:15:16,520 though we use const we can still change 6230 05:15:13,040 --> 05:15:18,920 the values inside the object another 6231 05:15:16,520 --> 05:15:21,638 interesting behavior is that on this 6232 05:15:18,920 --> 05:15:24,638 line we're not actually copying the 6233 05:15:21,638 --> 05:15:28,160 object into this variable we're copying 6234 05:15:24,638 --> 05:15:31,040 the reference so object two actually has 6235 05:15:28,160 --> 05:15:33,240 the same reference as object one they 6236 05:15:31,040 --> 05:15:34,878 actually point to the same object in the 6237 05:15:33,240 --> 05:15:38,480 computer's 6238 05:15:34,878 --> 05:15:40,920 memory so at the bottom if we type 6239 05:15:38,480 --> 05:15:40,920 console 6240 05:15:41,040 --> 05:15:50,638 log object 2 and save you'll notice that 6241 05:15:47,480 --> 05:15:53,360 for object two the message property was 6242 05:15:50,638 --> 05:15:56,798 also changed even though we only changed 6243 05:15:53,360 --> 05:15:59,120 object one the last interesting behavior 6244 05:15:56,798 --> 05:16:02,718 is that we can't compare objects 6245 05:15:59,120 --> 05:16:06,120 directly for example let's go to the 6246 05:16:02,718 --> 05:16:07,920 bottom and we'll create a new object 6247 05:16:06,120 --> 05:16:12,200 using 6248 05:16:07,920 --> 05:16:15,718 const object 3 and we'll make it equal 6249 05:16:12,200 --> 05:16:19,040 to an object and then inside we're going 6250 05:16:15,718 --> 05:16:22,480 to have these same values as object one 6251 05:16:19,040 --> 05:16:29,400 so we'll have the property 6252 05:16:22,480 --> 05:16:33,200 message and the value is the string good 6253 05:16:29,400 --> 05:16:39,120 job so now let's try comparing object 3 6254 05:16:33,200 --> 05:16:40,280 to object one so here we'll type console 6255 05:16:39,120 --> 05:16:46,920 do 6256 05:16:40,280 --> 05:16:48,400 log and check if object 3 is equal to 6257 05:16:46,920 --> 05:16:50,520 object 6258 05:16:48,400 --> 05:16:53,120 one if we 6259 05:16:50,520 --> 05:16:56,320 save it will tell us that these two 6260 05:16:53,120 --> 05:16:59,600 objects are not equal to each other even 6261 05:16:56,320 --> 05:17:02,440 though they have the same values inside 6262 05:16:59,600 --> 05:17:06,320 and that's because remember objects are 6263 05:17:02,440 --> 05:17:09,000 references so object 3 also contains a 6264 05:17:06,320 --> 05:17:12,040 reference and this code compares the 6265 05:17:09,000 --> 05:17:15,040 references and not the values inside the 6266 05:17:12,040 --> 05:17:17,680 object so because object 3 is a 6267 05:17:15,040 --> 05:17:20,558 different reference than object one 6268 05:17:17,680 --> 05:17:22,440 that's why this code results in 6269 05:17:20,558 --> 05:17:24,400 false if we 6270 05:17:22,440 --> 05:17:30,240 did 6271 05:17:24,400 --> 05:17:33,320 console. log and we check if object 2 is 6272 05:17:30,240 --> 05:17:37,040 equal to object 6273 05:17:33,320 --> 05:17:39,440 1 object two has the same reference as 6274 05:17:37,040 --> 05:17:43,440 object one it points to the the same 6275 05:17:39,440 --> 05:17:46,000 object in the computer's memory so if we 6276 05:17:43,440 --> 05:17:48,240 save this will tell us that these two 6277 05:17:46,000 --> 05:17:50,480 variables have the same 6278 05:17:48,240 --> 05:17:52,400 reference so just remember that when 6279 05:17:50,480 --> 05:17:54,320 you're comparing two variables that 6280 05:17:52,400 --> 05:17:57,920 contain objects you're actually 6281 05:17:54,320 --> 05:18:00,520 comparing the references not the values 6282 05:17:57,920 --> 05:18:03,080 inside if we want to compare the values 6283 05:18:00,520 --> 05:18:05,760 inside we would do that manually by 6284 05:18:03,080 --> 05:18:09,040 checking the message property against 6285 05:18:05,760 --> 05:18:11,000 this message property so don't worry 6286 05:18:09,040 --> 05:18:13,320 object references are a little confusing 6287 05:18:11,000 --> 05:18:15,400 at first but the more you work with it 6288 05:18:13,320 --> 05:18:17,638 the more you just get used to 6289 05:18:15,400 --> 05:18:20,798 it the last thing we're going to learn 6290 05:18:17,638 --> 05:18:24,320 in this lesson are some shortcuts for 6291 05:18:20,798 --> 05:18:28,920 objects so first let's make a copy of 6292 05:18:24,320 --> 05:18:31,000 object 3 we'll select the code and 6293 05:18:28,920 --> 05:18:33,760 rightclick 6294 05:18:31,000 --> 05:18:37,840 copy and then at the 6295 05:18:33,760 --> 05:18:40,440 bottom we'll scroll down and right click 6296 05:18:37,840 --> 05:18:45,360 and paste 6297 05:18:40,440 --> 05:18:48,400 and let's rename this variable to object 6298 05:18:45,360 --> 05:18:52,240 4 so now let's say that we want to get 6299 05:18:48,400 --> 05:18:56,638 the message property out of this object 6300 05:18:52,240 --> 05:18:58,718 we can do that by typing object 6301 05:18:56,638 --> 05:19:01,320 4. 6302 05:18:58,718 --> 05:19:04,000 message and now let's also say that we 6303 05:19:01,320 --> 05:19:07,480 want to save this in a variable also 6304 05:19:04,000 --> 05:19:10,120 called message so at the front we'll 6305 05:19:07,480 --> 05:19:15,320 type con 6306 05:19:10,120 --> 05:19:18,680 message and make it equal to this 6307 05:19:15,320 --> 05:19:21,040 value so now if the variable name and 6308 05:19:18,680 --> 05:19:24,120 the property name are the same we 6309 05:19:21,040 --> 05:19:28,280 actually have a shortcut for this to use 6310 05:19:24,120 --> 05:19:31,000 a shortcut we're going to type const and 6311 05:19:28,280 --> 05:19:35,878 curly brackets 6312 05:19:31,000 --> 05:19:39,320 message and make it equal to object 6313 05:19:35,878 --> 05:19:41,718 4 so this shortcut is called 6314 05:19:39,320 --> 05:19:45,080 destructuring this will take the message 6315 05:19:41,718 --> 05:19:48,320 property out of object 4 and save it in 6316 05:19:45,080 --> 05:19:51,200 a variable called message so this line 6317 05:19:48,320 --> 05:19:54,240 of code does the same thing as the code 6318 05:19:51,200 --> 05:19:56,920 above so let's comment out this line 6319 05:19:54,240 --> 05:20:00,920 using double slash and we'll just keep 6320 05:19:56,920 --> 05:20:03,878 it for reference so at the bottom if we 6321 05:20:00,920 --> 05:20:03,878 type 6322 05:20:04,360 --> 05:20:09,840 console.log 6323 05:20:06,400 --> 05:20:12,680 message and Save 6324 05:20:09,840 --> 05:20:15,320 it will display good job so the 6325 05:20:12,680 --> 05:20:18,120 destructuring shortcut took the property 6326 05:20:15,320 --> 05:20:19,520 out of the object and saved it in this 6327 05:20:18,120 --> 05:20:22,040 message 6328 05:20:19,520 --> 05:20:24,558 variable next we can use this 6329 05:20:22,040 --> 05:20:28,040 destructuring shortcut for multiple 6330 05:20:24,558 --> 05:20:31,840 properties for example let's add another 6331 05:20:28,040 --> 05:20:37,638 property to this object using comma and 6332 05:20:31,840 --> 05:20:40,440 we'll add a price property colon 6333 05:20:37,638 --> 05:20:43,638 $7.99 and now we can take the price 6334 05:20:40,440 --> 05:20:48,200 property out of this object by typing 6335 05:20:43,638 --> 05:20:50,638 after message a comma and 6336 05:20:48,200 --> 05:20:53,638 price so this will take the price 6337 05:20:50,638 --> 05:20:55,878 property out of the object and save it 6338 05:20:53,638 --> 05:20:59,958 in a variable called 6339 05:20:55,878 --> 05:20:59,958 price if we 6340 05:21:00,080 --> 05:21:04,558 console.log the 6341 05:21:02,480 --> 05:21:08,958 price and 6342 05:21:04,558 --> 05:21:11,360 save it will display $799 6343 05:21:08,958 --> 05:21:13,600 so that's the destructuring shortcut 6344 05:21:11,360 --> 05:21:14,920 it's an easier way to take properties 6345 05:21:13,600 --> 05:21:17,200 out of an 6346 05:21:14,920 --> 05:21:20,680 object the next shortcut we're going to 6347 05:21:17,200 --> 05:21:23,240 learn is called the shorthand property 6348 05:21:20,680 --> 05:21:25,958 so at the bottom let's create some new 6349 05:21:23,240 --> 05:21:30,520 lines and let's say we want to create a 6350 05:21:25,958 --> 05:21:34,600 new object so we'll type const and let's 6351 05:21:30,520 --> 05:21:36,920 name this object 5 and we'll make it 6352 05:21:34,600 --> 05:21:40,040 equal to an 6353 05:21:36,920 --> 05:21:43,080 object and inside ins this object let's 6354 05:21:40,040 --> 05:21:47,638 say we want to create a message property 6355 05:21:43,080 --> 05:21:52,160 so we'll type message colon and for the 6356 05:21:47,638 --> 05:21:54,718 value let's type the variable 6357 05:21:52,160 --> 05:21:56,958 message so what this does is that it 6358 05:21:54,718 --> 05:22:00,320 takes whatever is inside the message 6359 05:21:56,958 --> 05:22:05,320 variable and substitutes it into this 6360 05:22:00,320 --> 05:22:05,320 code so at the bottom if we 6361 05:22:05,600 --> 05:22:13,080 console.log object 5 and 6362 05:22:10,200 --> 05:22:16,240 save this object will have a property 6363 05:22:13,080 --> 05:22:19,480 called message and the value is whatever 6364 05:22:16,240 --> 05:22:22,920 is inside the message variable which is 6365 05:22:19,480 --> 05:22:26,040 good job so now we actually have a 6366 05:22:22,920 --> 05:22:28,558 shortcut for this code if the property 6367 05:22:26,040 --> 05:22:32,680 and the variable name are the same we 6368 05:22:28,558 --> 05:22:35,760 can actually just type it out once like 6369 05:22:32,680 --> 05:22:38,920 message so this code does the same thing 6370 05:22:35,760 --> 05:22:41,600 as message colon message and this 6371 05:22:38,920 --> 05:22:44,718 shortcut is called the shorthand 6372 05:22:41,600 --> 05:22:46,160 property so if we comment out this line 6373 05:22:44,718 --> 05:22:48,400 using double 6374 05:22:46,160 --> 05:22:51,360 slash and 6375 05:22:48,400 --> 05:22:54,480 save you can see that this shortcut 6376 05:22:51,360 --> 05:22:56,600 creates the same object that we had 6377 05:22:54,480 --> 05:23:00,400 before the last shortcut we're going to 6378 05:22:56,600 --> 05:23:04,360 learn is called the shorthand method so 6379 05:23:00,400 --> 05:23:07,440 at the end let's create a new property 6380 05:23:04,360 --> 05:23:11,120 comma and let's name it 6381 05:23:07,440 --> 05:23:14,840 method Po in and we'll save a function 6382 05:23:11,120 --> 05:23:19,638 in this property so we'll type 6383 05:23:14,840 --> 05:23:21,958 function and let's name it Function One 6384 05:23:19,638 --> 05:23:27,638 brackets and curly 6385 05:23:21,958 --> 05:23:27,638 brackets and inside this function let's 6386 05:23:28,558 --> 05:23:31,558 console.log 6387 05:23:31,760 --> 05:23:38,440 method and now here at the 6388 05:23:35,558 --> 05:23:43,798 bottom let's run this function by Ty 6389 05:23:38,440 --> 05:23:46,718 typing object 5. method so this will 6390 05:23:43,798 --> 05:23:48,240 access this function and we'll run it by 6391 05:23:46,718 --> 05:23:50,360 typing 6392 05:23:48,240 --> 05:23:52,878 brackets if we 6393 05:23:50,360 --> 05:23:56,040 save this will run the code in the 6394 05:23:52,878 --> 05:23:58,840 function and display method in the 6395 05:23:56,040 --> 05:24:02,680 console so we actually have a shortcut 6396 05:23:58,840 --> 05:24:06,958 for writing this so at the bottom we can 6397 05:24:02,680 --> 05:24:10,360 type method again and just type brackets 6398 05:24:06,958 --> 05:24:10,360 and curly brackets 6399 05:24:11,120 --> 05:24:17,718 console.log string 6400 05:24:14,638 --> 05:24:20,958 method so this code does the same thing 6401 05:24:17,718 --> 05:24:24,080 as the code above so these brackets will 6402 05:24:20,958 --> 05:24:27,600 create a function just like here so this 6403 05:24:24,080 --> 05:24:31,878 function name is actually not needed so 6404 05:24:27,600 --> 05:24:31,878 if we comment out these lines of 6405 05:24:32,280 --> 05:24:37,718 code and 6406 05:24:34,760 --> 05:24:41,200 save it will still display method in the 6407 05:24:37,718 --> 05:24:44,080 console because this is a shortcut for 6408 05:24:41,200 --> 05:24:46,958 these lines of code so this shortcut is 6409 05:24:44,080 --> 05:24:50,160 called the shorthand method 6410 05:24:46,958 --> 05:24:52,280 syntax and that's the end of this lesson 6411 05:24:50,160 --> 05:24:55,920 in this lesson we learned another type 6412 05:24:52,280 --> 05:24:59,798 of value called objects objects group 6413 05:24:55,920 --> 05:25:02,480 related values together we added a score 6414 05:24:59,798 --> 05:25:05,160 to our rock paper scissors game we 6415 05:25:02,480 --> 05:25:07,920 learned about built-in objects and used 6416 05:25:05,160 --> 05:25:10,638 the Json and local storage objects to 6417 05:25:07,920 --> 05:25:13,000 save our our score more permanently we 6418 05:25:10,638 --> 05:25:17,040 learned more details about objects like 6419 05:25:13,000 --> 05:25:19,798 null autoboxing and references and we 6420 05:25:17,040 --> 05:25:22,320 learned some useful shortcuts like 6421 05:25:19,798 --> 05:25:24,840 destructuring shorthand properties and 6422 05:25:22,320 --> 05:25:27,320 shorthand methods here are some 6423 05:25:24,840 --> 05:25:30,760 exercises to help you practice working 6424 05:25:27,320 --> 05:25:30,760 with objects 6425 05:25:49,520 --> 05:25:53,680 in this lesson we're going to learn one 6426 05:25:51,520 --> 05:25:56,680 of the most important features of 6427 05:25:53,680 --> 05:25:59,798 JavaScript called the document object 6428 05:25:56,680 --> 05:26:02,400 model or the Dom and we're going to use 6429 05:25:59,798 --> 05:26:06,120 the Dom to improve our rock paper 6430 05:26:02,400 --> 05:26:09,360 scissors project so first we'll create a 6431 05:26:06,120 --> 05:26:13,240 new file for this lesson let's go to our 6432 05:26:09,360 --> 05:26:16,878 code editor and click this icon to show 6433 05:26:13,240 --> 05:26:20,200 our files and then click this icon to 6434 05:26:16,878 --> 05:26:24,240 create a new file and we'll name this 6435 05:26:20,200 --> 05:26:26,240 file 09 6436 05:26:24,240 --> 05:26:29,000 dd. 6437 05:26:26,240 --> 05:26:34,040 HTML and press 6438 05:26:29,000 --> 05:26:35,320 enter next let's copy objects. HTML into 6439 05:26:34,040 --> 05:26:38,040 dom. 6440 05:26:35,320 --> 05:26:41,520 HTML so we'll click here to open the 6441 05:26:38,040 --> 05:26:45,120 file file and then click inside and then 6442 05:26:41,520 --> 05:26:47,600 type contrl A or command a to select the 6443 05:26:45,120 --> 05:26:49,440 code and then 6444 05:26:47,600 --> 05:26:52,120 rightclick 6445 05:26:49,440 --> 05:26:56,360 copy and in 6446 05:26:52,120 --> 05:26:59,878 here right click and 6447 05:26:56,360 --> 05:27:03,120 paste now let's hide these files and 6448 05:26:59,878 --> 05:27:05,280 we'll prepare this file for this lesson 6449 05:27:03,120 --> 05:27:09,360 so let's scroll to the 6450 05:27:05,280 --> 05:27:12,400 top and we'll change the title to 6451 05:27:09,360 --> 05:27:15,840 Dom and let's delete all the JavaScript 6452 05:27:12,400 --> 05:27:15,840 code from the previous 6453 05:27:16,200 --> 05:27:22,920 lesson now let's save this file and 6454 05:27:19,798 --> 05:27:26,600 we'll open it in the browser by right 6455 05:27:22,920 --> 05:27:29,638 clicking open with live 6456 05:27:26,600 --> 05:27:34,440 server and let's also open the console 6457 05:27:29,638 --> 05:27:38,638 on this page we'll right click click 6458 05:27:34,440 --> 05:27:42,160 inspect and then click the console 6459 05:27:38,638 --> 05:27:46,878 and now we can close the previous 6460 05:27:42,160 --> 05:27:49,480 tabs as well as the previous code for 6461 05:27:46,878 --> 05:27:53,160 now and for this lesson we're going to 6462 05:27:49,480 --> 05:27:55,878 do an extra step in the setup inside the 6463 05:27:53,160 --> 05:28:00,120 body we're going to create a 6464 05:27:55,878 --> 05:28:04,440 button so here let's type the code less 6465 05:28:00,120 --> 05:28:07,120 than button greater than and inside the 6466 05:28:04,440 --> 05:28:09,558 button we'll have the text 6467 05:28:07,120 --> 05:28:12,718 hello now let's 6468 05:28:09,558 --> 05:28:15,520 save and we should have a button on the 6469 05:28:12,718 --> 05:28:19,520 page and now we're ready to learn the 6470 05:28:15,520 --> 05:28:22,558 document object model or the Dom in the 6471 05:28:19,520 --> 05:28:25,718 previous lesson we learned about objects 6472 05:28:22,558 --> 05:28:28,920 which group multiple values together we 6473 05:28:25,718 --> 05:28:31,320 also learned about builtin objects which 6474 05:28:28,920 --> 05:28:34,040 are objects that are provided by 6475 05:28:31,320 --> 05:28:40,440 JavaScript some examples of built-in 6476 05:28:34,040 --> 05:28:44,320 objects are console math Json and local 6477 05:28:40,440 --> 05:28:48,280 storage the Dom is another builtin 6478 05:28:44,320 --> 05:28:50,760 object called the document object so 6479 05:28:48,280 --> 05:28:52,798 let's go to our script element and we're 6480 05:28:50,760 --> 05:28:56,040 going to type 6481 05:28:52,798 --> 05:28:58,558 document so this is a built-in object 6482 05:28:56,040 --> 05:29:00,718 provided by JavaScript and we've 6483 05:28:58,558 --> 05:29:03,600 actually seen this document object 6484 05:29:00,718 --> 05:29:06,920 before in the first lesson one of the 6485 05:29:03,600 --> 05:29:14,718 first pieces of code that we learned was 6486 05:29:06,920 --> 05:29:20,160 document dot body do inner Capital 6487 05:29:14,718 --> 05:29:23,080 HTML equals the string hello and 6488 05:29:20,160 --> 05:29:25,760 semicolon so this code removes 6489 05:29:23,080 --> 05:29:29,040 everything on the page and replaces it 6490 05:29:25,760 --> 05:29:32,320 with the text hello if we 6491 05:29:29,040 --> 05:29:33,320 save it will remove the button and just 6492 05:29:32,320 --> 05:29:36,160 put 6493 05:29:33,320 --> 05:29:38,840 hello and now that we learned objects in 6494 05:29:36,160 --> 05:29:40,400 the previous lesson we can fully 6495 05:29:38,840 --> 05:29:43,638 understand this 6496 05:29:40,400 --> 05:29:47,680 syntax document here is a built-in 6497 05:29:43,638 --> 05:29:50,280 object and this is the dot notation this 6498 05:29:47,680 --> 05:29:54,840 means we're accessing a property of the 6499 05:29:50,280 --> 05:29:57,958 document object called body now body is 6500 05:29:54,840 --> 05:30:00,920 also an object so we can use the dot 6501 05:29:57,958 --> 05:30:04,160 notation again to access a property of 6502 05:30:00,920 --> 05:30:08,120 the body object called inner 6503 05:30:04,160 --> 05:30:10,798 HTML and finally we use an equal sign to 6504 05:30:08,120 --> 05:30:12,320 change the inner HTML property to the 6505 05:30:10,798 --> 05:30:15,040 string 6506 05:30:12,320 --> 05:30:17,360 hello but as you can see there's 6507 05:30:15,040 --> 05:30:20,360 something very special about the inner 6508 05:30:17,360 --> 05:30:24,878 HTML property when we change this 6509 05:30:20,360 --> 05:30:27,600 property it also changes the web page so 6510 05:30:24,878 --> 05:30:31,000 this document object and the web page 6511 05:30:27,600 --> 05:30:33,760 are linked together let's do another 6512 05:30:31,000 --> 05:30:37,240 example we'll go to the end here and 6513 05:30:33,760 --> 05:30:38,718 press enter and we'll type the document 6514 05:30:37,240 --> 05:30:41,680 object again 6515 05:30:38,718 --> 05:30:45,000 and we'll use the dot notation again and 6516 05:30:41,680 --> 05:30:47,400 this time let's access the property 6517 05:30:45,000 --> 05:30:50,878 title and we'll change the title 6518 05:30:47,400 --> 05:30:55,600 property using an equal sign and we'll 6519 05:30:50,878 --> 05:30:57,878 change it to the string good 6520 05:30:55,600 --> 05:31:01,440 job if we 6521 05:30:57,878 --> 05:31:04,080 save changing the title property we'll 6522 05:31:01,440 --> 05:31:08,280 also change the title at the top of the 6523 05:31:04,080 --> 05:31:11,798 tabs here so now this says good job 6524 05:31:08,280 --> 05:31:15,120 again as you can see the document object 6525 05:31:11,798 --> 05:31:17,798 is linked to the web page so from these 6526 05:31:15,120 --> 05:31:22,080 examples it shows us that the document 6527 05:31:17,798 --> 05:31:24,360 object represents or models the web page 6528 05:31:22,080 --> 05:31:28,558 and that's why we call this feature the 6529 05:31:24,360 --> 05:31:32,280 document object model or the 6530 05:31:28,558 --> 05:31:35,840 Dom now that we know what the Dom means 6531 05:31:32,280 --> 05:31:38,600 let's learn the syntax rules for the Dom 6532 05:31:35,840 --> 05:31:41,638 so the Dom is just this built in 6533 05:31:38,600 --> 05:31:44,718 document object and this object works 6534 05:31:41,638 --> 05:31:48,760 the same way as any other object it 6535 05:31:44,718 --> 05:31:51,520 contains properties like body and title 6536 05:31:48,760 --> 05:31:54,080 that we can access using the dot 6537 05:31:51,520 --> 05:31:57,000 notation now the special thing about the 6538 05:31:54,080 --> 05:32:00,040 document object is that it is linked to 6539 05:31:57,000 --> 05:32:02,480 the web page that means if we change 6540 05:32:00,040 --> 05:32:04,878 certain properties in this object it 6541 05:32:02,480 --> 05:32:08,480 will also change the web 6542 05:32:04,878 --> 05:32:11,280 page now in addition to properties the 6543 05:32:08,480 --> 05:32:13,958 document object also has methods we can 6544 05:32:11,280 --> 05:32:15,240 use and we'll learn about them later in 6545 05:32:13,958 --> 05:32:18,480 this 6546 05:32:15,240 --> 05:32:21,320 lesson now that we understand the syntax 6547 05:32:18,480 --> 05:32:24,320 of the document object let's learn about 6548 05:32:21,320 --> 05:32:26,920 the properties and methods we can use 6549 05:32:24,320 --> 05:32:29,878 first let's comment out this previous 6550 05:32:26,920 --> 05:32:33,638 code using 6551 05:32:29,878 --> 05:32:35,760 slashstar star slash and we'll create 6552 05:32:33,638 --> 05:32:37,958 some new lines at the 6553 05:32:35,760 --> 05:32:40,120 top so the first property will we're 6554 05:32:37,958 --> 05:32:41,718 going to learn is called 6555 05:32:40,120 --> 05:32:45,240 document.title 6556 05:32:41,718 --> 05:32:48,958 so this property will give us the title 6557 05:32:45,240 --> 05:32:53,280 at the top of the tabs here so if we 6558 05:32:48,958 --> 05:32:53,280 console.log this property 6559 05:32:55,718 --> 05:33:00,958 console.log and 6560 05:32:57,958 --> 05:33:03,680 save this will give us the text at the 6561 05:33:00,958 --> 05:33:05,440 top which is Dom and that's what gets 6562 05:33:03,680 --> 05:33:08,520 displayed in the 6563 05:33:05,440 --> 05:33:11,440 console if we change the doc do. tile 6564 05:33:08,520 --> 05:33:17,280 property it will change the text at the 6565 05:33:11,440 --> 05:33:21,320 top of the tab so here if we type 6566 05:33:17,280 --> 05:33:23,958 document.title and make it equal to the 6567 05:33:21,320 --> 05:33:26,240 string 6568 05:33:23,958 --> 05:33:29,958 changed and 6569 05:33:26,240 --> 05:33:31,280 save now the title at the top of the tab 6570 05:33:29,958 --> 05:33:34,360 will be 6571 05:33:31,280 --> 05:33:37,120 changed so as you can see the title 6572 05:33:34,360 --> 05:33:38,718 property is linked to the title at the 6573 05:33:37,120 --> 05:33:41,320 top of the tab 6574 05:33:38,718 --> 05:33:44,160 we can use it to get the title and we 6575 05:33:41,320 --> 05:33:46,798 can also use it to change the 6576 05:33:44,160 --> 05:33:49,920 title next we're going to learn another 6577 05:33:46,798 --> 05:33:52,320 property called 6578 05:33:49,920 --> 05:33:57,040 document.body so let's go into our 6579 05:33:52,320 --> 05:33:57,040 code and let's type 6580 05:33:58,120 --> 05:34:04,798 document.body now document.body is a 6581 05:34:01,240 --> 05:34:07,718 very special property so this will 6582 05:34:04,798 --> 05:34:11,440 literally get this body HTML element 6583 05:34:07,718 --> 05:34:14,280 element and put it inside our 6584 05:34:11,440 --> 05:34:19,000 JavaScript so let's actually console.log 6585 05:34:14,280 --> 05:34:19,000 this to see how it works 6586 05:34:19,280 --> 05:34:25,840 console.log Open 6587 05:34:21,240 --> 05:34:29,000 Bracket and then close bracket and 6588 05:34:25,840 --> 05:34:30,160 save and inside the console it will tell 6589 05:34:29,000 --> 05:34:34,360 us that 6590 05:34:30,160 --> 05:34:37,000 document.body is the body HTML element 6591 05:34:34,360 --> 05:34:39,520 and if I hover over this it will tell us 6592 05:34:37,000 --> 05:34:43,200 that it is a exactly the body on our 6593 05:34:39,520 --> 05:34:46,160 page if I expand this you can see that 6594 05:34:43,200 --> 05:34:49,320 this code is exactly the code that we 6595 05:34:46,160 --> 05:34:52,440 have here and this shows us one of the 6596 05:34:49,320 --> 05:34:55,958 most important features of JavaScript we 6597 05:34:52,440 --> 05:34:58,718 can have HTML elements inside 6598 05:34:55,958 --> 05:35:02,400 JavaScript the document object or the 6599 05:34:58,718 --> 05:35:05,600 Dom combines JavaScript and HTML 6600 05:35:02,400 --> 05:35:07,878 together and it gives JavaScript control 6601 05:35:05,600 --> 05:35:11,280 of the web page 6602 05:35:07,878 --> 05:35:13,558 okay so now that we have an HTML element 6603 05:35:11,280 --> 05:35:16,760 inside our JavaScript what can we do 6604 05:35:13,558 --> 05:35:20,080 with it a really important thing to know 6605 05:35:16,760 --> 05:35:23,558 is that when an HTML element is inside a 6606 05:35:20,080 --> 05:35:27,320 JavaScript like this the HTML element is 6607 05:35:23,558 --> 05:35:31,280 converted into a JavaScript object so 6608 05:35:27,320 --> 05:35:32,920 for example if we do 6609 05:35:31,280 --> 05:35:39,480 console. 6610 05:35:32,920 --> 05:35:42,040 log type of document do 6611 05:35:39,480 --> 05:35:45,760 body and 6612 05:35:42,040 --> 05:35:48,878 save it will tell us that the body HTML 6613 05:35:45,760 --> 05:35:50,120 element is just an object when it's 6614 05:35:48,878 --> 05:35:53,718 inside 6615 05:35:50,120 --> 05:35:56,760 JavaScript so just like any other object 6616 05:35:53,718 --> 05:36:00,000 this has properties and methods we can 6617 05:35:56,760 --> 05:36:03,878 use so one useful property of the body 6618 05:36:00,000 --> 05:36:09,520 element is inner HTML which we saw 6619 05:36:03,878 --> 05:36:09,520 earlier so down here let's type 6620 05:36:09,558 --> 05:36:22,160 document dot body dot inner Capital 6621 05:36:17,718 --> 05:36:26,280 HTML so the inner HTML property controls 6622 05:36:22,160 --> 05:36:28,480 all the HTML inside the body that's why 6623 05:36:26,280 --> 05:36:31,878 it's called inner 6624 05:36:28,480 --> 05:36:35,280 HTML so if we access this property it 6625 05:36:31,878 --> 05:36:38,520 will give us all the HTML inside the 6626 05:36:35,280 --> 05:36:41,520 body so let's console.log to 6627 05:36:38,520 --> 05:36:41,520 confirm 6628 05:36:43,638 --> 05:36:50,160 console.log and 6629 05:36:46,040 --> 05:36:53,480 save and inner HTML will give us all of 6630 05:36:50,160 --> 05:36:56,718 this HTML inside the body 6631 05:36:53,480 --> 05:37:00,638 element and if we change the inner HTML 6632 05:36:56,718 --> 05:37:03,920 property it will change the HTML inside 6633 05:37:00,638 --> 05:37:07,920 the body element so let's create a new 6634 05:37:03,920 --> 05:37:09,400 line and we'll type document 6635 05:37:07,920 --> 05:37:14,240 dot 6636 05:37:09,400 --> 05:37:20,160 body do inner Capital 6637 05:37:14,240 --> 05:37:23,400 HTML and we'll change it to the string 6638 05:37:20,160 --> 05:37:28,160 changed so this will replace all of the 6639 05:37:23,400 --> 05:37:29,280 HTML inside the body with a text changed 6640 05:37:28,160 --> 05:37:32,798 if we 6641 05:37:29,280 --> 05:37:35,040 save that's exactly what it does and we 6642 05:37:32,798 --> 05:37:39,160 can actually confirm this by going into 6643 05:37:35,040 --> 05:37:43,638 the elements tab so this tab is for HTML 6644 05:37:39,160 --> 05:37:47,280 and CSS we can find the body element 6645 05:37:43,638 --> 05:37:50,360 expand it and you can see all of the 6646 05:37:47,280 --> 05:37:51,360 HTML that we had before was replaced 6647 05:37:50,360 --> 05:37:54,200 with 6648 05:37:51,360 --> 05:37:57,000 changed and don't worry about this part 6649 05:37:54,200 --> 05:38:00,798 this part is just added by live server 6650 05:37:57,000 --> 05:38:04,878 after so this shows that inner HTML 6651 05:38:00,798 --> 05:38:08,878 gives us control of all the HTML inside 6652 05:38:04,878 --> 05:38:11,680 the body we can get this h HTML and we 6653 05:38:08,878 --> 05:38:15,798 can also change this 6654 05:38:11,680 --> 05:38:19,120 HTML now another feature of inner HTML 6655 05:38:15,798 --> 05:38:23,200 is that we can replace this HTML with 6656 05:38:19,120 --> 05:38:26,280 new HTML code not just text so for 6657 05:38:23,200 --> 05:38:29,920 example instead of just text we can 6658 05:38:26,280 --> 05:38:34,000 replace it with the code less than 6659 05:38:29,920 --> 05:38:37,958 button greater than and also less than 6660 05:38:34,000 --> 05:38:41,920 slash button greater than so so this is 6661 05:38:37,958 --> 05:38:45,360 HTML code for creating a button and 6662 05:38:41,920 --> 05:38:46,878 inside the button let's put the text 6663 05:38:45,360 --> 05:38:50,520 good 6664 05:38:46,878 --> 05:38:55,000 job and now this code will replace all 6665 05:38:50,520 --> 05:38:57,080 the HTML inside the body with this new 6666 05:38:55,000 --> 05:39:00,958 HTML if we 6667 05:38:57,080 --> 05:39:05,718 save it removes all the previous HTML 6668 05:39:00,958 --> 05:39:08,320 and this new HTML creates this button so 6669 05:39:05,718 --> 05:39:11,280 using this inner HTML property 6670 05:39:08,320 --> 05:39:14,440 JavaScript can replace the HTML inside 6671 05:39:11,280 --> 05:39:16,920 the body to whatever we want we have 6672 05:39:14,440 --> 05:39:19,920 full control of the web 6673 05:39:16,920 --> 05:39:22,400 page all right now we're going to learn 6674 05:39:19,920 --> 05:39:25,958 a method of the document object that we 6675 05:39:22,400 --> 05:39:29,040 can use as a reminder a method is a 6676 05:39:25,958 --> 05:39:31,600 function that is saved inside an 6677 05:39:29,040 --> 05:39:36,000 object so we're going to learn a very 6678 05:39:31,600 --> 05:39:38,718 important method called document. query 6679 05:39:36,000 --> 05:39:41,638 selector we just learned that 6680 05:39:38,718 --> 05:39:45,040 document.body lets us get the body 6681 05:39:41,638 --> 05:39:48,200 element and put it inside our 6682 05:39:45,040 --> 05:39:51,360 JavaScript document. query selector lets 6683 05:39:48,200 --> 05:39:54,680 us get any element from the page and put 6684 05:39:51,360 --> 05:39:57,840 it inside our JavaScript let's give it a 6685 05:39:54,680 --> 05:40:01,200 try so first we'll comment out the 6686 05:39:57,840 --> 05:40:03,638 previous code using 6687 05:40:01,200 --> 05:40:06,798 slashstar and star 6688 05:40:03,638 --> 05:40:08,558 slash let's create some new lines at the 6689 05:40:06,798 --> 05:40:11,680 top 6690 05:40:08,558 --> 05:40:16,520 and let's type 6691 05:40:11,680 --> 05:40:19,040 document. query capital S 6692 05:40:16,520 --> 05:40:22,120 selector and 6693 05:40:19,040 --> 05:40:24,958 brackets so document. query selector 6694 05:40:22,120 --> 05:40:27,160 lets us get any element from the page 6695 05:40:24,958 --> 05:40:29,638 and put it inside our 6696 05:40:27,160 --> 05:40:33,080 JavaScript between these brackets we're 6697 05:40:29,638 --> 05:40:36,240 going to put a string this string tells 6698 05:40:33,080 --> 05:40:37,798 JavaScript which HTML element to get 6699 05:40:36,240 --> 05:40:40,160 from the page 6700 05:40:37,798 --> 05:40:43,240 so if we type 6701 05:40:40,160 --> 05:40:46,120 button this will get the first button 6702 05:40:43,240 --> 05:40:49,600 element from the page and put it inside 6703 05:40:46,120 --> 05:40:50,440 our JavaScript so let's console.log this 6704 05:40:49,600 --> 05:40:52,360 to 6705 05:40:50,440 --> 05:40:54,760 confirm 6706 05:40:52,360 --> 05:40:58,798 console. 6707 05:40:54,760 --> 05:41:01,798 log close bracket and 6708 05:40:58,798 --> 05:41:04,520 save and let's go back to our 6709 05:41:01,798 --> 05:41:07,798 console now it will tell us that 6710 05:41:04,520 --> 05:41:11,280 document. query selector got this button 6711 05:41:07,798 --> 05:41:14,120 element and put it inside our JavaScript 6712 05:41:11,280 --> 05:41:16,920 which you can see in the console and if 6713 05:41:14,120 --> 05:41:19,558 I hover over this button element it will 6714 05:41:16,920 --> 05:41:22,840 tell us that it is exactly the button on 6715 05:41:19,558 --> 05:41:25,600 the page so that's what document. query 6716 05:41:22,840 --> 05:41:28,878 selector does it can get any element 6717 05:41:25,600 --> 05:41:31,638 from the page and put it inside our 6718 05:41:28,878 --> 05:41:35,280 JavaScript this string tells it which 6719 05:41:31,638 --> 05:41:37,320 element to get if we type button it will 6720 05:41:35,280 --> 05:41:40,320 get the first button element from from 6721 05:41:37,320 --> 05:41:43,478 the page if we type 6722 05:41:40,320 --> 05:41:46,000 body this will now get the first body 6723 05:41:43,478 --> 05:41:48,840 element from the page so it will do the 6724 05:41:46,000 --> 05:41:51,840 same thing as 6725 05:41:48,840 --> 05:41:56,120 document.body but for now let's just 6726 05:41:51,840 --> 05:41:57,478 change this back to Button as an example 6727 05:41:56,120 --> 05:42:01,600 and 6728 05:41:57,478 --> 05:42:04,958 save so remember when an HTML element is 6729 05:42:01,600 --> 05:42:08,320 inside JavaScript it's just a JavaScript 6730 05:42:04,958 --> 05:42:11,320 object so this button element 6731 05:42:08,320 --> 05:42:13,680 is also just an object and it has 6732 05:42:11,320 --> 05:42:17,320 properties and methods we can 6733 05:42:13,680 --> 05:42:19,520 use every HTML element actually has a 6734 05:42:17,320 --> 05:42:22,958 property called inner 6735 05:42:19,520 --> 05:42:26,440 HTML which lets us control the HTML 6736 05:42:22,958 --> 05:42:33,120 inside the element so for this button 6737 05:42:26,440 --> 05:42:36,638 element let's type at the end dot inner 6738 05:42:33,120 --> 05:42:40,000 HTML and this will give us the HTML 6739 05:42:36,638 --> 05:42:42,958 inside the button if we 6740 05:42:40,000 --> 05:42:46,440 save it will give us 6741 05:42:42,958 --> 05:42:50,240 hello and again if we change the inner 6742 05:42:46,440 --> 05:42:53,558 HTML property it will change the HTML 6743 05:42:50,240 --> 05:42:57,080 inside the button so let's make a copy 6744 05:42:53,558 --> 05:42:59,160 of this code we'll select it and 6745 05:42:57,080 --> 05:43:01,718 rightclick 6746 05:42:59,160 --> 05:43:05,558 copy and down 6747 05:43:01,718 --> 05:43:09,040 here right click and 6748 05:43:05,558 --> 05:43:12,920 paste and let's change the inner HTML 6749 05:43:09,040 --> 05:43:15,760 property using an equal sign and the 6750 05:43:12,920 --> 05:43:18,760 string 6751 05:43:15,760 --> 05:43:23,718 changed so this will only change the 6752 05:43:18,760 --> 05:43:26,040 HTML inside the button which is here to 6753 05:43:23,718 --> 05:43:28,920 changed if we 6754 05:43:26,040 --> 05:43:32,440 save you can see that the text inside 6755 05:43:28,920 --> 05:43:34,958 the button is now changed a little 6756 05:43:32,440 --> 05:43:37,680 syntax shortcut I'm going to show you is 6757 05:43:34,958 --> 05:43:40,360 that for the dot notation we can 6758 05:43:37,680 --> 05:43:43,360 actually put it on multiple lines so we 6759 05:43:40,360 --> 05:43:47,840 can type document. query selector and 6760 05:43:43,360 --> 05:43:50,400 then put doin HTML on a separate line to 6761 05:43:47,840 --> 05:43:52,878 make it easier to read and make sure to 6762 05:43:50,400 --> 05:43:55,600 put a tab at the front so that we know 6763 05:43:52,878 --> 05:43:59,478 it's a continuation of the previous 6764 05:43:55,600 --> 05:44:02,520 line okay so using document. query 6765 05:43:59,478 --> 05:44:05,638 selector we can get any element from the 6766 05:44:02,520 --> 05:44:08,680 page put it inside our JavaScript and 6767 05:44:05,638 --> 05:44:11,840 then we can control the h HML inside 6768 05:44:08,680 --> 05:44:15,040 that element and this allows us to only 6769 05:44:11,840 --> 05:44:18,760 change the parts of the page that we 6770 05:44:15,040 --> 05:44:22,600 need next what if we have two buttons on 6771 05:44:18,760 --> 05:44:26,600 the page so let's go up here and we'll 6772 05:44:22,600 --> 05:44:29,798 create another button using less than 6773 05:44:26,600 --> 05:44:31,600 button greater than and we'll have the 6774 05:44:29,798 --> 05:44:33,280 text 6775 05:44:31,600 --> 05:44:36,040 second 6776 05:44:33,280 --> 05:44:39,400 button and 6777 05:44:36,040 --> 05:44:42,680 Save so now we have two buttons on the 6778 05:44:39,400 --> 05:44:45,840 page but document. query selector will 6779 05:44:42,680 --> 05:44:49,040 only get the first button on the page 6780 05:44:45,840 --> 05:44:51,240 how do we get this second button the 6781 05:44:49,040 --> 05:44:53,920 solution is that we're going to add an 6782 05:44:51,240 --> 05:44:57,878 attribute to this button called the 6783 05:44:53,920 --> 05:45:02,798 class attribute so in the opening tag 6784 05:44:57,878 --> 05:45:05,920 we're going to type space class equals 6785 05:45:02,798 --> 05:45:09,600 double quotes So the class attribute 6786 05:45:05,920 --> 05:45:11,718 sort of adds a label to this button so 6787 05:45:09,600 --> 05:45:17,280 inside the double quotes we're going to 6788 05:45:11,718 --> 05:45:19,878 label this button a JS dash 6789 05:45:17,280 --> 05:45:23,440 button and now that we labeled this 6790 05:45:19,878 --> 05:45:24,440 button we can select it using document. 6791 05:45:23,440 --> 05:45:28,160 query 6792 05:45:24,440 --> 05:45:30,600 selector so at the bottom here we'll 6793 05:45:28,160 --> 05:45:35,718 type 6794 05:45:30,600 --> 05:45:39,320 document. query selector again and 6795 05:45:35,718 --> 05:45:41,200 brackets and we'll type A String inside 6796 05:45:39,320 --> 05:45:44,760 and this time we're going to start with 6797 05:45:41,200 --> 05:45:46,840 a DOT when we start with a DOT this 6798 05:45:44,760 --> 05:45:50,478 means that it's going to look for a 6799 05:45:46,840 --> 05:45:53,638 class rather than an element name so 6800 05:45:50,478 --> 05:45:55,440 we're going to look for the class 6801 05:45:53,638 --> 05:45:58,840 js- 6802 05:45:55,440 --> 05:46:02,240 button and now document. query selector 6803 05:45:58,840 --> 05:46:05,718 will select an element with the class JS 6804 05:46:02,240 --> 05:46:08,798 button which is this element here so 6805 05:46:05,718 --> 05:46:12,478 let's confirm this by 6806 05:46:08,798 --> 05:46:14,638 console. logging this 6807 05:46:12,478 --> 05:46:18,520 code and 6808 05:46:14,638 --> 05:46:22,120 save and now you will see that this code 6809 05:46:18,520 --> 05:46:24,920 gives us the second button on the page 6810 05:46:22,120 --> 05:46:27,920 so using a class attribute and then 6811 05:46:24,920 --> 05:46:30,440 starting with a dot in query selector we 6812 05:46:27,920 --> 05:46:32,718 can get any specific element from the 6813 05:46:30,440 --> 05:46:35,520 page and put it in our 6814 05:46:32,718 --> 05:46:38,120 JavaScript by the way when we start with 6815 05:46:35,520 --> 05:46:41,760 a DOT this is called called a class 6816 05:46:38,120 --> 05:46:45,120 selector and this is also used in CSS to 6817 05:46:41,760 --> 05:46:48,280 style elements with a specific class in 6818 05:46:45,120 --> 05:46:49,718 fact anything we use in CSS can also be 6819 05:46:48,280 --> 05:46:52,920 used in this 6820 05:46:49,718 --> 05:46:55,520 string and also a common practice is to 6821 05:46:52,920 --> 05:46:58,638 start the class with 6822 05:46:55,520 --> 05:47:00,520 js- to make it clear that this class is 6823 05:46:58,638 --> 05:47:04,360 being used in 6824 05:47:00,520 --> 05:47:07,840 JavaScript and again HTML elements are 6825 05:47:04,360 --> 05:47:11,360 JavaScript objects so this second button 6826 05:47:07,840 --> 05:47:14,280 is also just an object and remember 6827 05:47:11,360 --> 05:47:19,000 objects are values and we can save them 6828 05:47:14,280 --> 05:47:22,040 inside a variable so instead of 6829 05:47:19,000 --> 05:47:26,878 console.log let's create a variable 6830 05:47:22,040 --> 05:47:30,200 using const and we'll name the variable 6831 05:47:26,878 --> 05:47:33,280 button capital E 6832 05:47:30,200 --> 05:47:36,840 element we usually end the variable name 6833 05:47:33,280 --> 05:47:39,798 with element or LM to make it clear 6834 05:47:36,840 --> 05:47:40,600 clear there's an HTML element inside 6835 05:47:39,798 --> 05:47:43,558 this 6836 05:47:40,600 --> 05:47:47,000 variable and finally we'll type an equal 6837 05:47:43,558 --> 05:47:49,120 sign to save this element and we can 6838 05:47:47,000 --> 05:47:53,200 remove this extra 6839 05:47:49,120 --> 05:47:56,200 bracket and now if we console.log this 6840 05:47:53,200 --> 05:47:56,200 variable 6841 05:47:57,000 --> 05:48:00,760 console.log 6842 05:47:58,680 --> 05:48:03,120 button 6843 05:48:00,760 --> 05:48:05,638 element and 6844 05:48:03,120 --> 05:48:08,760 save it will tell us that the second 6845 05:48:05,638 --> 05:48:12,120 button was Sav saved inside this 6846 05:48:08,760 --> 05:48:15,320 variable so using the Dom we can get 6847 05:48:12,120 --> 05:48:18,200 HTML elements from the page put it 6848 05:48:15,320 --> 05:48:21,240 inside a JavaScript and then save it in 6849 05:48:18,200 --> 05:48:24,638 a variable so the Dom combines 6850 05:48:21,240 --> 05:48:28,000 JavaScript and HTML together it gives 6851 05:48:24,638 --> 05:48:30,958 JavaScript full control of the web 6852 05:48:28,000 --> 05:48:34,360 page all right now that we learned how 6853 05:48:30,958 --> 05:48:36,840 to use the Dom let's do some practical 6854 05:48:34,360 --> 05:48:39,558 examples so I've actually prepared some 6855 05:48:36,840 --> 05:48:42,280 projects that we can do for this lesson 6856 05:48:39,558 --> 05:48:45,958 to open these projects we'll go to our 6857 05:48:42,280 --> 05:48:49,798 browser and create a new tab at the 6858 05:48:45,958 --> 05:48:52,520 top and here we'll type 6859 05:48:49,798 --> 05:48:57,160 super 6860 05:48:52,520 --> 05:49:00,320 simple. SL projects 6861 05:48:57,160 --> 05:49:03,360 slod and press 6862 05:49:00,320 --> 05:49:06,280 enter on this page we'll be working on 6863 05:49:03,360 --> 05:49:08,958 three projects the YouTube subscribe 6864 05:49:06,280 --> 05:49:10,638 button the rock paper scissors project 6865 05:49:08,958 --> 05:49:14,000 we've been working on for the last few 6866 05:49:10,638 --> 05:49:16,360 lessons and this Amazon shipping 6867 05:49:14,000 --> 05:49:19,878 calculator and you'll notice that these 6868 05:49:16,360 --> 05:49:22,080 projects modify the web page so we're 6869 05:49:19,878 --> 05:49:25,240 going to be using the Dom to create 6870 05:49:22,080 --> 05:49:27,878 these projects and in the next lesson 6871 05:49:25,240 --> 05:49:30,680 we're going to use CSS to style these 6872 05:49:27,878 --> 05:49:33,280 projects and make them look nice and you 6873 05:49:30,680 --> 05:49:36,680 can preview that by clicking 6874 05:49:33,280 --> 05:49:38,760 here so using CSS we can make the sub 6875 05:49:36,680 --> 05:49:41,600 subscribe button look like the actual 6876 05:49:38,760 --> 05:49:44,040 subscribe button on YouTube and if we 6877 05:49:41,600 --> 05:49:46,718 click this link to see the rock paper 6878 05:49:44,040 --> 05:49:50,440 scissors game you'll see that we'll make 6879 05:49:46,718 --> 05:49:52,320 it look really nice with these icons but 6880 05:49:50,440 --> 05:49:55,638 for this lesson we're only going to 6881 05:49:52,320 --> 05:49:59,160 focus on the functionality and not the 6882 05:49:55,638 --> 05:50:02,958 styling so let's go back to the projects 6883 05:49:59,160 --> 05:50:07,120 we'll click back and click back 6884 05:50:02,958 --> 05:50:08,440 again until we're at projects sdom 6885 05:50:07,120 --> 05:50:11,878 and we're going to create the 6886 05:50:08,440 --> 05:50:14,440 functionality for these projects first 6887 05:50:11,878 --> 05:50:17,878 so before we start let's create a new 6888 05:50:14,440 --> 05:50:22,240 file just for these projects we'll go to 6889 05:50:17,878 --> 05:50:25,520 our code editor and click this icon and 6890 05:50:22,240 --> 05:50:31,478 then click this icon to create a new 6891 05:50:25,520 --> 05:50:31,478 file let's name this file 09 6892 05:50:31,600 --> 05:50:38,120 dd- 6893 05:50:33,638 --> 05:50:42,878 projects. HTML and press 6894 05:50:38,120 --> 05:50:46,638 enter now let's copy dom. HTML into this 6895 05:50:42,878 --> 05:50:50,760 file so we'll open it and then click in 6896 05:50:46,638 --> 05:50:53,718 here and type control a or command a to 6897 05:50:50,760 --> 05:50:55,478 select the code and then 6898 05:50:53,718 --> 05:50:57,920 rightclick 6899 05:50:55,478 --> 05:51:02,478 copy and in 6900 05:50:57,920 --> 05:51:05,478 here right click and 6901 05:51:02,478 --> 05:51:07,120 paste now let's close our files and 6902 05:51:05,478 --> 05:51:09,718 we'll prepare this file 6903 05:51:07,120 --> 05:51:13,478 so let's scroll up and we'll change the 6904 05:51:09,718 --> 05:51:17,798 title to Dom 6905 05:51:13,478 --> 05:51:20,680 projects and let's remove our HTML but 6906 05:51:17,798 --> 05:51:23,040 keep the script 6907 05:51:20,680 --> 05:51:26,080 element and we'll also remove the 6908 05:51:23,040 --> 05:51:26,080 JavaScript from 6909 05:51:26,840 --> 05:51:32,958 before finally let's save this file and 6910 05:51:30,638 --> 05:51:37,718 we'll open it in the browser by right 6911 05:51:32,958 --> 05:51:40,200 clicking open with live server 6912 05:51:37,718 --> 05:51:44,000 and let's also open the console just in 6913 05:51:40,200 --> 05:51:46,920 case right click click 6914 05:51:44,000 --> 05:51:49,958 inspect and then click the 6915 05:51:46,920 --> 05:51:52,878 console all right we're ready to start 6916 05:51:49,958 --> 05:51:54,440 with the projects let's go back to super 6917 05:51:52,878 --> 05:51:57,040 simple 6918 05:51:54,440 --> 05:51:59,718 dodev and we'll start by creating the 6919 05:51:57,040 --> 05:52:03,040 YouTube subscribe button let's actually 6920 05:51:59,718 --> 05:52:05,000 press refresh to get it back to normal 6921 05:52:03,040 --> 05:52:07,240 and when we click this button it will 6922 05:52:05,000 --> 05:52:11,160 change to subscribe 6923 05:52:07,240 --> 05:52:14,958 red first let's create the HTML elements 6924 05:52:11,160 --> 05:52:16,600 for this project we'll go to the body 6925 05:52:14,958 --> 05:52:19,958 we'll create some new 6926 05:52:16,600 --> 05:52:24,400 lines and let's create the paragraph 6927 05:52:19,958 --> 05:52:27,400 first we'll type less than P greater 6928 05:52:24,400 --> 05:52:30,000 than and inside the paragraph we'll have 6929 05:52:27,400 --> 05:52:31,920 the text 6930 05:52:30,000 --> 05:52:33,680 YouTube 6931 05:52:31,920 --> 05:52:37,280 subscribe 6932 05:52:33,680 --> 05:52:38,920 button and now let's create the button 6933 05:52:37,280 --> 05:52:43,600 so on the next 6934 05:52:38,920 --> 05:52:48,000 line we'll type less than button greater 6935 05:52:43,600 --> 05:52:51,718 than and inside the button 6936 05:52:48,000 --> 05:52:52,638 subscribe now let's save and go back to 6937 05:52:51,718 --> 05:52:56,000 our 6938 05:52:52,638 --> 05:52:58,440 Tab and now we have the HTML elements 6939 05:52:56,000 --> 05:53:01,520 that we need so now let's make this 6940 05:52:58,440 --> 05:53:04,798 interactive with JavaScript and remember 6941 05:53:01,520 --> 05:53:07,760 we have a process for this first we'll 6942 05:53:04,798 --> 05:53:10,440 figure out what steps we need to do this 6943 05:53:07,760 --> 05:53:13,798 is called an algorithm and then we'll 6944 05:53:10,440 --> 05:53:16,320 convert these steps into code so the 6945 05:53:13,798 --> 05:53:19,280 steps that we need here are when we 6946 05:53:16,320 --> 05:53:22,160 click this button if the text is 6947 05:53:19,280 --> 05:53:24,878 subscribe we'll change it to 6948 05:53:22,160 --> 05:53:28,160 subscribed otherwise we'll change it 6949 05:53:24,878 --> 05:53:31,600 back now let's convert these steps into 6950 05:53:28,160 --> 05:53:34,958 code let's go to our button element and 6951 05:53:31,600 --> 05:53:40,600 we're going to add an onclick attribute 6952 05:53:34,958 --> 05:53:44,440 we'll type space on click equals double 6953 05:53:40,600 --> 05:53:47,240 quotes and inside here we'll write some 6954 05:53:44,440 --> 05:53:50,200 JavaScript so first we need to get the 6955 05:53:47,240 --> 05:53:52,760 text inside the button and we learned 6956 05:53:50,200 --> 05:53:55,638 how to do this earlier we can use 6957 05:53:52,760 --> 05:53:58,638 document. Query selector to put this 6958 05:53:55,638 --> 05:54:03,160 button into our JavaScript and then use 6959 05:53:58,638 --> 05:54:08,360 inner HTML to get this HTML inside the 6960 05:54:03,160 --> 05:54:08,360 button so in our code let's type 6961 05:54:08,760 --> 05:54:16,478 document. query 6962 05:54:11,958 --> 05:54:18,520 selector brackets and a string and as we 6963 05:54:16,478 --> 05:54:22,558 learned we're going to use a class 6964 05:54:18,520 --> 05:54:27,920 attribute to select this specific button 6965 05:54:22,558 --> 05:54:32,400 so in the opening tag let's add a class 6966 05:54:27,920 --> 05:54:34,160 attribute and let's make the class 6967 05:54:32,400 --> 05:54:37,280 js- 6968 05:54:34,160 --> 05:54:40,478 subscribe Dash 6969 05:54:37,280 --> 05:54:43,440 button and then inside document. query 6970 05:54:40,478 --> 05:54:45,680 selector we're going to type a DOT to 6971 05:54:43,440 --> 05:54:48,200 select a class and we're going to select 6972 05:54:45,680 --> 05:54:50,240 the class 6973 05:54:48,200 --> 05:54:53,240 js- 6974 05:54:50,240 --> 05:54:56,760 subscribe Das 6975 05:54:53,240 --> 05:55:00,040 button and now let's console.log this to 6976 05:54:56,760 --> 05:55:03,280 confirm that we got this button so at 6977 05:55:00,040 --> 05:55:03,280 the front 6978 05:55:05,080 --> 05:55:10,440 console.log and 6979 05:55:07,440 --> 05:55:12,760 save and now let's click our 6980 05:55:10,440 --> 05:55:15,878 button and it will show us that 6981 05:55:12,760 --> 05:55:18,840 document. query selector got the button 6982 05:55:15,878 --> 05:55:21,798 on the page so now let's save this 6983 05:55:18,840 --> 05:55:24,558 button in a variable so we can use it 6984 05:55:21,798 --> 05:55:28,280 later instead of 6985 05:55:24,558 --> 05:55:31,558 console.log let's type const to create a 6986 05:55:28,280 --> 05:55:33,080 variable and we'll name it 6987 05:55:31,558 --> 05:55:37,798 button 6988 05:55:33,080 --> 05:55:40,400 element and make it equal to this 6989 05:55:37,798 --> 05:55:42,558 result so now that we have the button 6990 05:55:40,400 --> 05:55:46,080 inside our JavaScript we're going to 6991 05:55:42,558 --> 05:55:49,080 check if the text inside the button is 6992 05:55:46,080 --> 05:55:52,840 subscribe to check the text we can use 6993 05:55:49,080 --> 05:55:56,920 an if statement so at the bottom here 6994 05:55:52,840 --> 05:55:57,958 we're going to type if brackets and 6995 05:55:56,920 --> 05:56:01,200 curly 6996 05:55:57,958 --> 05:56:04,760 brackets and inside this condition let's 6997 05:56:01,200 --> 05:56:07,920 get the text inside the button so we'll 6998 05:56:04,760 --> 05:56:13,160 type button 6999 05:56:07,920 --> 05:56:15,680 element do inner Capital 7000 05:56:13,160 --> 05:56:19,840 HTML and we're going to check if it's 7001 05:56:15,680 --> 05:56:24,160 equal to subscribe so we'll type triple 7002 05:56:19,840 --> 05:56:27,360 equal the string 7003 05:56:24,160 --> 05:56:30,520 subscribe now if it is equal to 7004 05:56:27,360 --> 05:56:32,360 subscribe inside the curly brackets 7005 05:56:30,520 --> 05:56:34,440 we're going to change the text in the 7006 05:56:32,360 --> 05:56:38,040 button to 7007 05:56:34,440 --> 05:56:39,600 subscribed so let's type the code 7008 05:56:38,040 --> 05:56:45,878 button 7009 05:56:39,600 --> 05:56:48,920 element do inner Capital HTM L and we'll 7010 05:56:45,878 --> 05:56:51,920 change it with an equal sign to the 7011 05:56:48,920 --> 05:56:51,920 string 7012 05:56:52,400 --> 05:56:58,240 subscribed and the last step is 7013 05:56:55,120 --> 05:57:00,600 otherwise we'll change it back so we'll 7014 05:56:58,240 --> 05:57:05,520 add an else to this if 7015 05:57:00,600 --> 05:57:08,080 statement else and curly brackets and 7016 05:57:05,520 --> 05:57:10,080 inside we're going to change the text 7017 05:57:08,080 --> 05:57:13,280 back to 7018 05:57:10,080 --> 05:57:15,440 subscribe so let's type 7019 05:57:13,280 --> 05:57:19,280 button 7020 05:57:15,440 --> 05:57:25,040 element. inner Capital 7021 05:57:19,280 --> 05:57:27,320 HTML and make it equal to the string 7022 05:57:25,040 --> 05:57:30,680 subscribe and 7023 05:57:27,320 --> 05:57:32,920 save now before you try out this project 7024 05:57:30,680 --> 05:57:36,280 make sure that the text inside the 7025 05:57:32,920 --> 05:57:39,320 button has no spaces or new lines around 7026 05:57:36,280 --> 05:57:42,200 it and it looks like this and I'll show 7027 05:57:39,320 --> 05:57:45,478 you why in a second let's go to our 7028 05:57:42,200 --> 05:57:49,120 project and try it out so when we click 7029 05:57:45,478 --> 05:57:52,400 this button it will change to the text 7030 05:57:49,120 --> 05:57:55,440 subscribed if we click it again it will 7031 05:57:52,400 --> 05:57:57,440 go back to the original text so that's 7032 05:57:55,440 --> 05:58:01,120 how we create the Subscribe button 7033 05:57:57,440 --> 05:58:04,478 project using the Dom so now let's go 7034 05:58:01,120 --> 05:58:06,638 back into our code and I'll show you why 7035 05:58:04,478 --> 05:58:08,840 if we add new lines 7036 05:58:06,638 --> 05:58:11,440 and some spaces around this 7037 05:58:08,840 --> 05:58:14,320 text there's actually going to be some 7038 05:58:11,440 --> 05:58:17,558 problems so if we save this 7039 05:58:14,320 --> 05:58:20,160 code and then go back to our project and 7040 05:58:17,558 --> 05:58:23,240 click the button again you'll notice 7041 05:58:20,160 --> 05:58:26,200 that it no longer changes the text and 7042 05:58:23,240 --> 05:58:29,840 that's because now we have a bunch of 7043 05:58:26,200 --> 05:58:32,160 spaces around this text and these spaces 7044 05:58:29,840 --> 05:58:36,200 will show up in inner 7045 05:58:32,160 --> 05:58:37,400 HTML and now inner HTML will not equal 7046 05:58:36,200 --> 05:58:39,520 exactly 7047 05:58:37,400 --> 05:58:42,280 subscribe so how do we solve this 7048 05:58:39,520 --> 05:58:45,280 problem when there are spaces around our 7049 05:58:42,280 --> 05:58:49,478 text to solve this we're going to use a 7050 05:58:45,280 --> 05:58:52,680 different property of an HTML element so 7051 05:58:49,478 --> 05:58:56,478 instead of the inner HTML property we're 7052 05:58:52,680 --> 05:58:58,240 going to use the property inner capital 7053 05:58:56,478 --> 05:59:02,200 T 7054 05:58:58,240 --> 05:59:05,000 text so inner text will give us the text 7055 05:59:02,200 --> 05:59:07,240 inside this element but without all the 7056 05:59:05,000 --> 05:59:10,320 spaces around it 7057 05:59:07,240 --> 05:59:14,120 so now this will equal exactly 7058 05:59:10,320 --> 05:59:17,760 subscribe if we save and go back to our 7059 05:59:14,120 --> 05:59:20,920 project and click it again it'll start 7060 05:59:17,760 --> 05:59:24,120 working again so that's the inner text 7061 05:59:20,920 --> 05:59:26,878 property of an HTML element it gets us 7062 05:59:24,120 --> 05:59:29,400 the text without the extra 7063 05:59:26,878 --> 05:59:31,680 spaces so before we move on to the 7064 05:59:29,400 --> 05:59:33,200 second project I'm going to show you how 7065 05:59:31,680 --> 05:59:37,240 to make our code 7066 05:59:33,200 --> 05:59:40,360 cleaner right now we're mixing a HTML 7067 05:59:37,240 --> 05:59:43,160 and JavaScript together so to make our 7068 05:59:40,360 --> 05:59:45,798 code cleaner and easier to read we're 7069 05:59:43,160 --> 05:59:47,638 going to separate the JavaScript code 7070 05:59:45,798 --> 05:59:49,760 and to do that we're going to put this 7071 05:59:47,638 --> 05:59:52,798 code in a function 7072 05:59:49,760 --> 05:59:58,440 instead so inside the script 7073 05:59:52,798 --> 06:00:02,878 element let's create a function using 7074 05:59:58,440 --> 06:00:07,120 function and we'll name the function 7075 06:00:02,878 --> 06:00:09,280 subcribe brackets and curly brackets 7076 06:00:07,120 --> 06:00:12,280 and inside these curly brackets we're 7077 06:00:09,280 --> 06:00:13,680 going to move all of this code so let's 7078 06:00:12,280 --> 06:00:16,080 select this 7079 06:00:13,680 --> 06:00:17,680 code and we'll right 7080 06:00:16,080 --> 06:00:23,440 click 7081 06:00:17,680 --> 06:00:25,558 cut and in here right click and 7082 06:00:23,440 --> 06:00:28,320 paste and if we need to fix the 7083 06:00:25,558 --> 06:00:31,638 formatting we can select these lines and 7084 06:00:28,320 --> 06:00:34,080 press tab a few times and now that we 7085 06:00:31,638 --> 06:00:36,160 have this function we're going to go to 7086 06:00:34,080 --> 06:00:38,920 the onclick attribute and and we're 7087 06:00:36,160 --> 06:00:41,958 going to call this function so we'll 7088 06:00:38,920 --> 06:00:44,798 type the function name 7089 06:00:41,958 --> 06:00:48,478 subscribe and 7090 06:00:44,798 --> 06:00:51,080 brackets so this will run all the code 7091 06:00:48,478 --> 06:00:54,320 inside the function so it will do the 7092 06:00:51,080 --> 06:00:57,000 same thing as before except now our code 7093 06:00:54,320 --> 06:00:59,638 is cleaner because we grouped all the 7094 06:00:57,000 --> 06:01:02,638 JavaScript code at the bottom and we're 7095 06:00:59,638 --> 06:01:04,040 no longer mixing as much HTML and 7096 06:01:02,638 --> 06:01:07,120 JavaScript 7097 06:01:04,040 --> 06:01:10,958 together now let's move on to the second 7098 06:01:07,120 --> 06:01:13,280 project we'll go back to superimple 7099 06:01:10,958 --> 06:01:15,958 dodev and we're going to work on the 7100 06:01:13,280 --> 06:01:17,360 rock paper scissors project so let's 7101 06:01:15,958 --> 06:01:19,760 click this 7102 06:01:17,360 --> 06:01:22,718 link and it will show us the next 7103 06:01:19,760 --> 06:01:25,478 version of this project so you notice a 7104 06:01:22,718 --> 06:01:27,320 few changes here the first change is 7105 06:01:25,478 --> 06:01:31,040 that we're going to show the score on 7106 06:01:27,320 --> 06:01:33,160 the page instead of in a popup and the 7107 06:01:31,040 --> 06:01:36,120 second change is that when we click one 7108 06:01:33,160 --> 06:01:39,240 of these buttons to play the game it 7109 06:01:36,120 --> 06:01:42,120 will show us the result and the moves 7110 06:01:39,240 --> 06:01:43,080 that we picked on the page rather than 7111 06:01:42,120 --> 06:01:46,000 in a 7112 06:01:43,080 --> 06:01:48,920 popup let's create a new file for this 7113 06:01:46,000 --> 06:01:50,760 rock paper scissors game for this lesson 7114 06:01:48,920 --> 06:01:54,280 so let's go to our code 7115 06:01:50,760 --> 06:01:57,840 editor and we'll click this icon and 7116 06:01:54,280 --> 06:02:00,718 click this icon to create a new file and 7117 06:01:57,840 --> 06:02:02,600 we'll name this 7118 06:02:00,718 --> 06:02:06,958 09- 7119 06:02:02,600 --> 06:02:08,840 rock- paper Das 7120 06:02:06,958 --> 06:02:11,478 scissors. 7121 06:02:08,840 --> 06:02:14,600 HTML and press 7122 06:02:11,478 --> 06:02:17,840 enter and let's copy the previous rock 7123 06:02:14,600 --> 06:02:21,958 paper scissors which is here we'll click 7124 06:02:17,840 --> 06:02:24,680 here and type control a or command a 7125 06:02:21,958 --> 06:02:30,520 right click 7126 06:02:24,680 --> 06:02:33,680 copy and here right click and 7127 06:02:30,520 --> 06:02:37,360 paste and let's hide our 7128 06:02:33,680 --> 06:02:41,798 files and let's save this F file and we 7129 06:02:37,360 --> 06:02:43,840 can also close 08 rock paper scissors 7130 06:02:41,798 --> 06:02:49,840 and finally we'll open this in the 7131 06:02:43,840 --> 06:02:49,840 browser by right clicking open with live 7132 06:02:49,878 --> 06:02:55,360 server now we're ready to begin the 7133 06:02:52,478 --> 06:02:58,320 second project so let's go back to the 7134 06:02:55,360 --> 06:03:00,840 project at super simple Dev and we're 7135 06:02:58,320 --> 06:03:03,878 going to start by putting the score on 7136 06:03:00,840 --> 06:03:07,240 the page so to do this we're going to 7137 06:03:03,878 --> 06:03:11,280 need somewhere to put the score so let's 7138 06:03:07,240 --> 06:03:13,958 create an HTML element for this we'll 7139 06:03:11,280 --> 06:03:16,240 scroll up to our 7140 06:03:13,958 --> 06:03:20,040 HTML which is 7141 06:03:16,240 --> 06:03:22,600 here and after the scissors button let's 7142 06:03:20,040 --> 06:03:26,878 add a paragraph element where we're 7143 06:03:22,600 --> 06:03:27,958 going to put our score so less than P 7144 06:03:26,878 --> 06:03:31,080 greater 7145 06:03:27,958 --> 06:03:33,080 than and let's add a class attribute so 7146 06:03:31,080 --> 06:03:36,120 we can select this element in our 7147 06:03:33,080 --> 06:03:43,320 JavaScript so in the opening tag we'll 7148 06:03:36,120 --> 06:03:45,000 type space class equals double quotes 7149 06:03:43,320 --> 06:03:48,160 js- 7150 06:03:45,000 --> 06:03:50,440 score now that we have a class let's get 7151 06:03:48,160 --> 06:03:54,080 this element in our 7152 06:03:50,440 --> 06:03:57,320 JavaScript so let's just do it down here 7153 06:03:54,080 --> 06:03:58,840 so we'll type 7154 06:03:57,320 --> 06:04:01,080 document. 7155 06:03:58,840 --> 06:04:04,878 query 7156 06:04:01,080 --> 06:04:07,200 selector brackets and a string and then 7157 06:04:04,878 --> 06:04:12,958 we're going to type a Dot and we're 7158 06:04:07,200 --> 06:04:14,878 going to select the class js- score so 7159 06:04:12,958 --> 06:04:17,680 js- 7160 06:04:14,878 --> 06:04:21,558 score so this will get the paragraph 7161 06:04:17,680 --> 06:04:24,320 element from above and put it inside our 7162 06:04:21,558 --> 06:04:27,638 JavaScript and now let's put the score 7163 06:04:24,320 --> 06:04:28,920 inside the element to do that we can use 7164 06:04:27,638 --> 06:04:33,958 inner 7165 06:04:28,920 --> 06:04:37,718 HTML dot inner Capital 7166 06:04:33,958 --> 06:04:40,000 HTML and we'll make it equal to the 7167 06:04:37,718 --> 06:04:43,320 score if we scroll 7168 06:04:40,000 --> 06:04:46,440 down we actually create the score down 7169 06:04:43,320 --> 06:04:49,680 here so we can actually just copy this 7170 06:04:46,440 --> 06:04:51,520 string so we'll select it 7171 06:04:49,680 --> 06:04:55,080 rightclick 7172 06:04:51,520 --> 06:04:59,478 copy and let's scroll 7173 06:04:55,080 --> 06:05:02,320 up to here and I'll actually put this on 7174 06:04:59,478 --> 06:05:05,680 a separate line to make it easier to 7175 06:05:02,320 --> 06:05:08,040 read and now here we're going to type a 7176 06:05:05,680 --> 06:05:10,920 string using back 7177 06:05:08,040 --> 06:05:14,558 ticks and inside the string we're going 7178 06:05:10,920 --> 06:05:17,798 to rightclick and 7179 06:05:14,558 --> 06:05:21,160 paste so this will put the score that we 7180 06:05:17,798 --> 06:05:23,558 used to have in our popup inside this 7181 06:05:21,160 --> 06:05:25,760 element so inside this 7182 06:05:23,558 --> 06:05:28,840 paragraph if we 7183 06:05:25,760 --> 06:05:31,718 save and then go back to our 7184 06:05:28,840 --> 06:05:35,320 tab you can see that now it displays the 7185 06:05:31,718 --> 06:05:37,478 score on the page now before we move on 7186 06:05:35,320 --> 06:05:40,520 I want want to point something out with 7187 06:05:37,478 --> 06:05:43,878 HTML so remember that our reset score 7188 06:05:40,520 --> 06:05:46,958 button was beside our other buttons and 7189 06:05:43,878 --> 06:05:49,478 now it's below them so what determines 7190 06:05:46,958 --> 06:05:52,680 if an element appears beside something 7191 06:05:49,478 --> 06:05:56,760 or below something so the paragraph 7192 06:05:52,680 --> 06:05:59,440 element Here is known as a block element 7193 06:05:56,760 --> 06:06:03,040 a block element will always take up an 7194 06:05:59,440 --> 06:06:05,040 entire line by itself so this line 7195 06:06:03,040 --> 06:06:07,878 already has some buttons so the 7196 06:06:05,040 --> 06:06:10,638 paragraph will take up the entire line 7197 06:06:07,878 --> 06:06:13,478 under the buttons that's why this text 7198 06:06:10,638 --> 06:06:16,798 appears under the buttons and not beside 7199 06:06:13,478 --> 06:06:19,878 them and also because a block element 7200 06:06:16,798 --> 06:06:23,200 like this paragraph takes up this entire 7201 06:06:19,878 --> 06:06:27,478 line the next button has to appear below 7202 06:06:23,200 --> 06:06:29,600 it so that's why we get this layout here 7203 06:06:27,478 --> 06:06:31,718 so this is just a short review of 7204 06:06:29,600 --> 06:06:34,600 layouts if you want to learn this in 7205 06:06:31,718 --> 06:06:37,878 more detail you can check out my HTML 7206 06:06:34,600 --> 06:06:40,478 and CSS full course in the video 7207 06:06:37,878 --> 06:06:43,280 description all right the next feature 7208 06:06:40,478 --> 06:06:46,558 we're going to do is if we play the game 7209 06:06:43,280 --> 06:06:49,520 we also want to update the score right 7210 06:06:46,558 --> 06:06:52,878 now if we play it doesn't update it on 7211 06:06:49,520 --> 06:06:55,520 the page let's fix this in our 7212 06:06:52,878 --> 06:06:58,558 JavaScript so let's go down to where we 7213 06:06:55,520 --> 06:07:02,200 update the score so we'll scroll 7214 06:06:58,558 --> 06:07:05,600 down which is right here we update the 7215 06:07:02,200 --> 06:07:07,760 score and save it in local storage and 7216 06:07:05,600 --> 06:07:11,240 after we do this we're also going to 7217 06:07:07,760 --> 06:07:14,120 update the score on the page so we can 7218 06:07:11,240 --> 06:07:18,080 actually use the same code that we used 7219 06:07:14,120 --> 06:07:20,320 above which is here so let's make a copy 7220 06:07:18,080 --> 06:07:21,840 of this code 7221 06:07:20,320 --> 06:07:24,840 rightclick 7222 06:07:21,840 --> 06:07:29,400 copy and down 7223 06:07:24,840 --> 06:07:33,240 here right click and 7224 06:07:29,400 --> 06:07:37,320 paste and I'll add a tab at the front if 7225 06:07:33,240 --> 06:07:39,558 we save and then try out our game we'll 7226 06:07:37,320 --> 06:07:42,120 click a move press 7227 06:07:39,558 --> 06:07:45,120 okay and now you'll notice the score 7228 06:07:42,120 --> 06:07:48,200 also updates on the page so let's go 7229 06:07:45,120 --> 06:07:50,558 back into our code and do some cleanup 7230 06:07:48,200 --> 06:07:53,360 so you notice that this code is the same 7231 06:07:50,558 --> 06:07:56,240 as the code we had above and this is a 7232 06:07:53,360 --> 06:08:00,680 great situation to use a function to 7233 06:07:56,240 --> 06:08:05,798 reuse this code so at the bottom here 7234 06:08:00,680 --> 06:08:08,040 let's create a function using function 7235 06:08:05,798 --> 06:08:09,878 and we'll call it 7236 06:08:08,040 --> 06:08:11,478 update 7237 06:08:09,878 --> 06:08:14,478 score 7238 06:08:11,478 --> 06:08:17,400 element brackets and curly 7239 06:08:14,478 --> 06:08:21,160 brackets and we'll move this code into 7240 06:08:17,400 --> 06:08:22,798 the function so we'll select it right 7241 06:08:21,160 --> 06:08:28,120 click 7242 06:08:22,798 --> 06:08:30,958 copy and in here right click and 7243 06:08:28,120 --> 06:08:33,798 paste and now instead of writing this 7244 06:08:30,958 --> 06:08:36,718 code multiple times we're just going to 7245 06:08:33,798 --> 06:08:42,200 call the function 7246 06:08:36,718 --> 06:08:44,718 update score element and 7247 06:08:42,200 --> 06:08:46,878 brackets this will run all the code 7248 06:08:44,718 --> 06:08:50,440 inside the function so it does the same 7249 06:08:46,878 --> 06:08:53,600 thing as before except we can reuse the 7250 06:08:50,440 --> 06:08:56,558 code so let's scroll back 7251 06:08:53,600 --> 06:09:00,000 up and instead of typing out all this 7252 06:08:56,558 --> 06:09:01,558 code again we can just use our 7253 06:09:00,000 --> 06:09:03,200 function 7254 06:09:01,558 --> 06:09:05,718 update 7255 06:09:03,200 --> 06:09:07,120 score element 7256 06:09:05,718 --> 06:09:09,440 and 7257 06:09:07,120 --> 06:09:12,120 brackets now let's 7258 06:09:09,440 --> 06:09:15,440 save it will display the score on the 7259 06:09:12,120 --> 06:09:19,120 page at the start and when we click a 7260 06:09:15,440 --> 06:09:22,360 move and click okay it will update the 7261 06:09:19,120 --> 06:09:24,160 score on the page so the last feature 7262 06:09:22,360 --> 06:09:27,040 we're going to do in relation to the 7263 06:09:24,160 --> 06:09:29,760 score is when we click the reset score 7264 06:09:27,040 --> 06:09:30,680 button we also want to update the score 7265 06:09:29,760 --> 06:09:34,000 on the 7266 06:09:30,680 --> 06:09:36,000 page right now if we click it it doesn't 7267 06:09:34,000 --> 06:09:38,920 actually update it 7268 06:09:36,000 --> 06:09:43,280 so let's go into our code and let's go 7269 06:09:38,920 --> 06:09:46,200 to the reset button which is here and 7270 06:09:43,280 --> 06:09:49,360 after we reset the score we're going to 7271 06:09:46,200 --> 06:09:51,280 call that function again 7272 06:09:49,360 --> 06:09:54,798 update 7273 06:09:51,280 --> 06:09:57,120 score element and 7274 06:09:54,798 --> 06:10:00,798 brackets if we 7275 06:09:57,120 --> 06:10:04,600 save and now let's play the game and 7276 06:10:00,798 --> 06:10:07,440 press okay so we have a score and if we 7277 06:10:04,600 --> 06:10:11,000 click reset score it will reset 7278 06:10:07,440 --> 06:10:13,878 everything back to zero so now we 7279 06:10:11,000 --> 06:10:15,520 finished the score let's go back to 7280 06:10:13,878 --> 06:10:17,920 super simple 7281 06:10:15,520 --> 06:10:20,680 Dev and play the 7282 06:10:17,920 --> 06:10:23,520 game and now we're going to work on this 7283 06:10:20,680 --> 06:10:27,200 section we're going to show the result 7284 06:10:23,520 --> 06:10:29,478 and the moves that we picked on the page 7285 06:10:27,200 --> 06:10:32,920 so first let's create some paragraphs 7286 06:10:29,478 --> 06:10:36,400 for these as well we'll go to our 7287 06:10:32,920 --> 06:10:41,280 HTML and above the score 7288 06:10:36,400 --> 06:10:44,000 let's add a less than P greater than 7289 06:10:41,280 --> 06:10:48,320 this is going to be the result so let's 7290 06:10:44,000 --> 06:10:50,520 give it a class of 7291 06:10:48,320 --> 06:10:53,440 js- 7292 06:10:50,520 --> 06:10:57,400 result and let's create another 7293 06:10:53,440 --> 06:11:03,040 paragraph less than P greater than and 7294 06:10:57,400 --> 06:11:05,920 we'll give this one a class equals 7295 06:11:03,040 --> 06:11:09,080 js- moves 7296 06:11:05,920 --> 06:11:12,160 now let's use the Dom to put the result 7297 06:11:09,080 --> 06:11:14,080 and the moves in these paragraphs feel 7298 06:11:12,160 --> 06:11:17,558 free to pause the video if you want to 7299 06:11:14,080 --> 06:11:21,040 try it yourself first so let's scroll 7300 06:11:17,558 --> 06:11:24,920 down to where we display the 7301 06:11:21,040 --> 06:11:29,000 result which is here so we display it in 7302 06:11:24,920 --> 06:11:31,280 a popup and instead of a popup we're 7303 06:11:29,000 --> 06:11:34,520 going to get the paragraph and put this 7304 06:11:31,280 --> 06:11:36,680 result inside the paragraph so we'll 7305 06:11:34,520 --> 06:11:39,878 type 7306 06:11:36,680 --> 06:11:43,680 document. query 7307 06:11:39,878 --> 06:11:48,320 selector brackets and a string and we'll 7308 06:11:43,680 --> 06:11:50,600 type a DOT and select the class 7309 06:11:48,320 --> 06:11:56,558 js- 7310 06:11:50,600 --> 06:12:01,680 result and then dot inner Capital 7311 06:11:56,558 --> 06:12:04,440 HTML and we'll change this to 7312 06:12:01,680 --> 06:12:09,798 result and now let's also display the 7313 06:12:04,440 --> 06:12:14,760 moves so we'll type some new lines and 7314 06:12:09,798 --> 06:12:18,600 document. query selector again brackets 7315 06:12:14,760 --> 06:12:20,718 and a string dot 7316 06:12:18,600 --> 06:12:25,638 js- 7317 06:12:20,718 --> 06:12:29,360 moves do inner Capital 7318 06:12:25,638 --> 06:12:32,160 HTML and make it equal to this text 7319 06:12:29,360 --> 06:12:34,440 right here so let's type a template 7320 06:12:32,160 --> 06:12:37,558 string because we're going to insert our 7321 06:12:34,440 --> 06:12:39,120 Moves In inside the string and we'll 7322 06:12:37,558 --> 06:12:42,638 Type 7323 06:12:39,120 --> 06:12:44,520 U and we'll insert the player move 7324 06:12:42,638 --> 06:12:50,400 dollar curly 7325 06:12:44,520 --> 06:12:54,440 brackets Player move and then a dash and 7326 06:12:50,400 --> 06:12:57,520 then the computers move so dollar curly 7327 06:12:54,440 --> 06:12:59,320 brackets again and 7328 06:12:57,520 --> 06:13:03,718 computer 7329 06:12:59,320 --> 06:13:06,440 move computer at the end so now that 7330 06:13:03,718 --> 06:13:09,400 we're displaying everything on the page 7331 06:13:06,440 --> 06:13:12,760 we don't need this popup at the end so 7332 06:13:09,400 --> 06:13:15,878 we can actually delete this 7333 06:13:12,760 --> 06:13:18,958 popup all right now let's 7334 06:13:15,878 --> 06:13:21,240 save and go back to our 7335 06:13:18,958 --> 06:13:24,320 Tab and play our 7336 06:13:21,240 --> 06:13:27,120 game now it will show the result and the 7337 06:13:24,320 --> 06:13:30,120 moves that we picked on the page instead 7338 06:13:27,120 --> 06:13:33,000 of in a popup let's play it again to 7339 06:13:30,120 --> 06:13:35,840 make sure it works and finally when we 7340 06:13:33,000 --> 06:13:39,440 click reset score it it will reset 7341 06:13:35,840 --> 06:13:41,520 everything back to zero and that's it 7342 06:13:39,440 --> 06:13:43,798 now we're displaying everything on the 7343 06:13:41,520 --> 06:13:46,840 web page using the 7344 06:13:43,798 --> 06:13:50,040 Dom now let's move on to the third and 7345 06:13:46,840 --> 06:13:52,120 final project we'll go back to Super 7346 06:13:50,040 --> 06:13:55,840 simple. 7347 06:13:52,120 --> 06:13:58,360 deev and we'll click back to go back to 7348 06:13:55,840 --> 06:14:01,000 our projects and we're going to create 7349 06:13:58,360 --> 06:14:02,878 this third project here which is an 7350 06:14:01,000 --> 06:14:05,520 Amazon shipping 7351 06:14:02,878 --> 06:14:08,120 calculator the way this works is is that 7352 06:14:05,520 --> 06:14:11,638 inside this text box we're going to type 7353 06:14:08,120 --> 06:14:13,200 the cost of an order in dollars if the 7354 06:14:11,638 --> 06:14:16,240 order is under 7355 06:14:13,200 --> 06:14:18,600 $4 for example 7356 06:14:16,240 --> 06:14:22,120 $25 and we click 7357 06:14:18,600 --> 06:14:25,160 calculate it will add $10 of shipping 7358 06:14:22,120 --> 06:14:27,798 and display the total down here but if 7359 06:14:25,160 --> 06:14:32,120 we type a cost that is over 7360 06:14:27,798 --> 06:14:35,160 $40 like 50 and press 7361 06:14:32,120 --> 06:14:38,360 calculate it will be free shipping so so 7362 06:14:35,160 --> 06:14:40,600 it will display the same total down here 7363 06:14:38,360 --> 06:14:41,440 so let's learn how to create this using 7364 06:14:40,600 --> 06:14:46,240 the 7365 06:14:41,440 --> 06:14:49,200 Dom let's go back to our 09 Dom projects 7366 06:14:46,240 --> 06:14:50,558 file and we'll start by creating the 7367 06:14:49,200 --> 06:14:53,680 HTML 7368 06:14:50,558 --> 06:14:58,080 elements so under our subscribe 7369 06:14:53,680 --> 06:15:02,200 button let's create this paragraph using 7370 06:14:58,080 --> 06:15:06,120 less than P greater than 7371 06:15:02,200 --> 06:15:09,878 Amazon shipping 7372 06:15:06,120 --> 06:15:13,760 calculator and next let's create this 7373 06:15:09,878 --> 06:15:17,718 text box so the HTML element for a text 7374 06:15:13,760 --> 06:15:22,558 box is called input so we'll type less 7375 06:15:17,718 --> 06:15:24,798 than input greater than so the input 7376 06:15:22,558 --> 06:15:28,360 element is special because it doesn't 7377 06:15:24,798 --> 06:15:29,478 require a closing tag this is called a 7378 06:15:28,360 --> 06:15:32,798 void 7379 06:15:29,478 --> 06:15:37,320 element and lastly let's create the 7380 06:15:32,798 --> 06:15:41,360 button so here we create less than 7381 06:15:37,320 --> 06:15:43,600 button greater than 7382 06:15:41,360 --> 06:15:47,520 calculate now let's 7383 06:15:43,600 --> 06:15:51,360 save and open the tab for this file 7384 06:15:47,520 --> 06:15:55,120 which is right here and we have the text 7385 06:15:51,360 --> 06:15:57,400 the text box and the button that we need 7386 06:15:55,120 --> 06:16:00,478 now one other thing we're going to add 7387 06:15:57,400 --> 06:16:02,638 is that in the final project if we don't 7388 06:16:00,478 --> 06:16:05,920 have anything in the text box we're 7389 06:16:02,638 --> 06:16:08,638 going to have this gray text so this is 7390 06:16:05,920 --> 06:16:11,160 called a placeholder to add a 7391 06:16:08,638 --> 06:16:14,878 placeholder to a text box we're going to 7392 06:16:11,160 --> 06:16:18,360 use the placeholder attribute so inside 7393 06:16:14,878 --> 06:16:20,958 our input element we're going to type 7394 06:16:18,360 --> 06:16:25,160 Space Place 7395 06:16:20,958 --> 06:16:27,840 holder equals double quotes and inside 7396 06:16:25,160 --> 06:16:33,558 the quotes we'll put the text we want as 7397 06:16:27,840 --> 06:16:35,920 the placeholder which is cost of 7398 06:16:33,558 --> 06:16:39,760 order and 7399 06:16:35,920 --> 06:16:42,760 save now if we go back to our project we 7400 06:16:39,760 --> 06:16:45,200 now have a placeholder in our text box 7401 06:16:42,760 --> 06:16:48,400 that disappears when we start 7402 06:16:45,200 --> 06:16:51,760 typing so that's all the HTML that we 7403 06:16:48,400 --> 06:16:54,558 need now let's work on the JavaScript 7404 06:16:51,760 --> 06:16:58,120 and again we'll figure out what steps we 7405 06:16:54,558 --> 06:17:00,600 need to do so when we click this button 7406 06:16:58,120 --> 06:17:04,520 we're going to get the text inside the 7407 06:17:00,600 --> 06:17:07,798 text box if the amount is below $40 7408 06:17:04,520 --> 06:17:11,040 we'll add $10 of shipping and then we'll 7409 06:17:07,798 --> 06:17:14,320 display the total on the page let's 7410 06:17:11,040 --> 06:17:19,760 convert these steps into code so we'll 7411 06:17:14,320 --> 06:17:23,120 go to our button and add and onclick 7412 06:17:19,760 --> 06:17:25,680 attribute and inside here let's also 7413 06:17:23,120 --> 06:17:29,280 create a function to separate our 7414 06:17:25,680 --> 06:17:32,360 JavaScript code like we did before so in 7415 06:17:29,280 --> 06:17:34,958 the script we're going to create a 7416 06:17:32,360 --> 06:17:37,920 function function 7417 06:17:34,958 --> 06:17:42,718 and let's name it 7418 06:17:37,920 --> 06:17:46,240 calculate total brackets and curly 7419 06:17:42,718 --> 06:17:50,280 brackets and inside on click we're just 7420 06:17:46,240 --> 06:17:51,798 going to call the function so 7421 06:17:50,280 --> 06:17:53,680 calculate 7422 06:17:51,798 --> 06:17:56,478 total 7423 06:17:53,680 --> 06:18:01,080 brackets the next step is to get the 7424 06:17:56,478 --> 06:18:04,360 text inside the text box so first let's 7425 06:18:01,080 --> 06:18:07,440 get this input element and put it inside 7426 06:18:04,360 --> 06:18:11,000 our J JavaScript to do that we can use 7427 06:18:07,440 --> 06:18:13,558 document. Query selector again so we'll 7428 06:18:11,000 --> 06:18:18,400 add a class to this input so we can 7429 06:18:13,558 --> 06:18:20,360 select it class equals 7430 06:18:18,400 --> 06:18:22,080 js- 7431 06:18:20,360 --> 06:18:25,040 cost- 7432 06:18:22,080 --> 06:18:27,520 input and then inside our JavaScript 7433 06:18:25,040 --> 06:18:29,040 we'll use 7434 06:18:27,520 --> 06:18:30,558 document. 7435 06:18:29,040 --> 06:18:35,840 Query 7436 06:18:30,558 --> 06:18:39,000 selector brackets and a string and a Dot 7437 06:18:35,840 --> 06:18:40,958 and this class 7438 06:18:39,000 --> 06:18:42,878 js- 7439 06:18:40,958 --> 06:18:45,840 cost- 7440 06:18:42,878 --> 06:18:49,600 input so this code will get the input 7441 06:18:45,840 --> 06:18:53,440 element and put it inside our JavaScript 7442 06:18:49,600 --> 06:18:53,440 let's confirm by 7443 06:18:53,478 --> 06:19:00,080 console. logging the 7444 06:18:57,878 --> 06:19:03,120 result and 7445 06:19:00,080 --> 06:19:05,320 save and now when we click the calculate 7446 06:19:03,120 --> 06:19:08,600 button it will give us 7447 06:19:05,320 --> 06:19:11,120 the input element from the page let's 7448 06:19:08,600 --> 06:19:14,200 save this input element in a variable so 7449 06:19:11,120 --> 06:19:17,840 we can use it later instead of 7450 06:19:14,200 --> 06:19:20,280 console.log we'll use const and we'll 7451 06:19:17,840 --> 06:19:24,360 name the variable 7452 06:19:20,280 --> 06:19:27,160 input element and make it equal to this 7453 06:19:24,360 --> 06:19:30,638 element and remove the extra 7454 06:19:27,160 --> 06:19:33,760 bracket and next we need to get the text 7455 06:19:30,638 --> 06:19:35,520 inside the input element so so far in 7456 06:19:33,760 --> 06:19:40,000 this lesson we've been using the 7457 06:19:35,520 --> 06:19:42,840 property inner HTML to do this however 7458 06:19:40,000 --> 06:19:47,160 for input elements you notice that there 7459 06:19:42,840 --> 06:19:49,680 is no HTML inside so for input elements 7460 06:19:47,160 --> 06:19:50,798 we're going to use a different property 7461 06:19:49,680 --> 06:19:53,920 called 7462 06:19:50,798 --> 06:19:56,040 value so at the bottom here we're going 7463 06:19:53,920 --> 06:19:58,400 to type 7464 06:19:56,040 --> 06:20:00,040 input 7465 06:19:58,400 --> 06:20:03,558 element. 7466 06:20:00,040 --> 06:20:05,680 value this is how we get the text inside 7467 06:20:03,558 --> 06:20:08,680 a text box 7468 06:20:05,680 --> 06:20:11,520 so let's console.log this to confirm 7469 06:20:08,680 --> 06:20:15,160 that it works so we 7470 06:20:11,520 --> 06:20:17,440 console do 7471 06:20:15,160 --> 06:20:20,280 log and 7472 06:20:17,440 --> 06:20:25,040 save and now let's type something in the 7473 06:20:20,280 --> 06:20:28,320 text box like 50 and click 7474 06:20:25,040 --> 06:20:31,360 calculate and input. value will give us 7475 06:20:28,320 --> 06:20:34,360 the text inside the text 7476 06:20:31,360 --> 06:20:38,320 box let's save this text in a variable 7477 06:20:34,360 --> 06:20:42,600 so so we can use it later con and we'll 7478 06:20:38,320 --> 06:20:45,440 name the variable cost and make it equal 7479 06:20:42,600 --> 06:20:48,680 to the text in the text 7480 06:20:45,440 --> 06:20:50,558 box the next step is to check if this 7481 06:20:48,680 --> 06:20:53,718 cost is under 7482 06:20:50,558 --> 06:20:58,878 $40 if it is We'll add 7483 06:20:53,718 --> 06:21:00,200 $10 so down here let's type if brackets 7484 06:20:58,878 --> 06:21:03,320 and curly 7485 06:21:00,200 --> 06:21:07,760 brackets and inside the condition we'll 7486 06:21:03,320 --> 06:21:11,200 check if cost is less than 7487 06:21:07,760 --> 06:21:17,000 40 and if it is inside the curly 7488 06:21:11,200 --> 06:21:21,558 brackets we'll do cost equals itself 7489 06:21:17,000 --> 06:21:24,280 plus $10 of shipping and remember when 7490 06:21:21,558 --> 06:21:26,840 we change a variable we have to switch 7491 06:21:24,280 --> 06:21:29,798 from const to 7492 06:21:26,840 --> 06:21:32,920 let and finally we're going to display 7493 06:21:29,798 --> 06:21:35,478 the total cost on the page so let's 7494 06:21:32,920 --> 06:21:39,958 create a paragraph where we can display 7495 06:21:35,478 --> 06:21:44,440 the cost so down here we'll create a 7496 06:21:39,958 --> 06:21:48,000 paragraph less than P greater than let's 7497 06:21:44,440 --> 06:21:53,920 give it a class 7498 06:21:48,000 --> 06:21:57,320 js- total Das cost and now inside our 7499 06:21:53,920 --> 06:22:02,520 JavaScript let's use 7500 06:21:57,320 --> 06:22:05,760 document. Query selector brackets and 7501 06:22:02,520 --> 06:22:07,718 string and we'll Type A Dot and the 7502 06:22:05,760 --> 06:22:12,718 class 7503 06:22:07,718 --> 06:22:16,320 js- total Das cost and we're going to 7504 06:22:12,718 --> 06:22:21,240 put the cost inside the paragraph we can 7505 06:22:16,320 --> 06:22:24,958 use dot inner Capital 7506 06:22:21,240 --> 06:22:28,520 HTML and make it equal and let's use a 7507 06:22:24,958 --> 06:22:31,440 template string so back ticks and we'll 7508 06:22:28,520 --> 06:22:37,320 have a dollar and we're going to insert 7509 06:22:31,440 --> 06:22:41,878 the cost using dollar and curly brackets 7510 06:22:37,320 --> 06:22:44,958 cost now let's save and try out this 7511 06:22:41,878 --> 06:22:46,080 project so first we'll type a cost that 7512 06:22:44,958 --> 06:22:50,200 is over 7513 06:22:46,080 --> 06:22:53,000 $40 like 50 if we press 7514 06:22:50,200 --> 06:22:55,200 calculate we're not going to add $10 of 7515 06:22:53,000 --> 06:22:59,080 shipping and we'll display the same 7516 06:22:55,200 --> 06:23:00,040 total on the page now let's try a number 7517 06:22:59,080 --> 06:23:05,000 under 7518 06:23:00,040 --> 06:23:08,000 $40 like 25 and press calculate 7519 06:23:05,000 --> 06:23:11,080 and this number doesn't look right it's 7520 06:23:08,000 --> 06:23:11,080 saying that we have to pay 7521 06:23:11,280 --> 06:23:17,840 $2,510 so what's going on here let's go 7522 06:23:14,440 --> 06:23:21,000 into our code and let's use console.log 7523 06:23:17,840 --> 06:23:24,040 to figure out what's wrong so first 7524 06:23:21,000 --> 06:23:25,840 let's console.log the cost to make sure 7525 06:23:24,040 --> 06:23:31,080 that we're getting the right number from 7526 06:23:25,840 --> 06:23:31,080 the text box so down here 7527 06:23:31,520 --> 06:23:39,320 console.log the cost 7528 06:23:34,718 --> 06:23:40,478 and save and then let's Type 25 again 7529 06:23:39,320 --> 06:23:43,680 and press 7530 06:23:40,478 --> 06:23:45,558 calculate and it will give us 25 so 7531 06:23:43,680 --> 06:23:49,400 we're getting the correct text from the 7532 06:23:45,558 --> 06:23:52,320 text box now let's console.log down here 7533 06:23:49,400 --> 06:23:54,200 to see if the math is working so we'll 7534 06:23:52,320 --> 06:23:55,840 type 7535 06:23:54,200 --> 06:23:57,760 console. 7536 06:23:55,840 --> 06:23:59,878 log 7537 06:23:57,760 --> 06:24:04,000 cost and 7538 06:23:59,878 --> 06:24:05,600 save and let's try 25 again and 7539 06:24:04,000 --> 06:24:09,080 calculate 7540 06:24:05,600 --> 06:24:13,680 and down here when we add 10 this is 7541 06:24:09,080 --> 06:24:16,760 where things go wrong and it results in 7542 06:24:13,680 --> 06:24:20,478 20510 so it's not doing math properly 7543 06:24:16,760 --> 06:24:23,478 here so let's check if cost is a number 7544 06:24:20,478 --> 06:24:27,478 to make sure that it's doing math so 7545 06:24:23,478 --> 06:24:27,478 here we'll type 7546 06:24:27,798 --> 06:24:35,320 console.log the type of 7547 06:24:32,040 --> 06:24:40,718 cost and Save 7548 06:24:35,320 --> 06:24:44,120 let's Type 25 again and calculate and 7549 06:24:40,718 --> 06:24:47,558 aha it's telling us that the cost is a 7550 06:24:44,120 --> 06:24:50,360 string so this is actually not a number 7551 06:24:47,558 --> 06:24:53,638 it is a string that's why it's not doing 7552 06:24:50,360 --> 06:24:57,000 math so remember from the strings lesson 7553 06:24:53,638 --> 06:24:59,320 when we add a string with a number 7554 06:24:57,000 --> 06:25:02,280 JavaScript will automatically convert 7555 06:24:59,320 --> 06:25:04,920 this number into a string and combine 7556 06:25:02,280 --> 06:25:09,718 them together that's that's why it just 7557 06:25:04,920 --> 06:25:10,878 combines 25 and 10 here this is called 7558 06:25:09,718 --> 06:25:13,840 type 7559 06:25:10,878 --> 06:25:17,120 coercion so whenever we get a value from 7560 06:25:13,840 --> 06:25:20,400 the Dom like this the value will be a 7561 06:25:17,120 --> 06:25:23,280 string so in order to do math we have to 7562 06:25:20,400 --> 06:25:26,400 manually convert this string into a 7563 06:25:23,280 --> 06:25:30,638 number to do that JavaScript has a 7564 06:25:26,400 --> 06:25:35,160 builtin function called number so before 7565 06:25:30,638 --> 06:25:40,240 this code we're going to type capital n 7566 06:25:35,160 --> 06:25:42,478 number Open Bracket and close bracket so 7567 06:25:40,240 --> 06:25:44,680 the number function takes whatever's 7568 06:25:42,478 --> 06:25:49,638 between the brackets and manually 7569 06:25:44,680 --> 06:25:54,120 converts it into a number so now if we 7570 06:25:49,638 --> 06:25:57,558 save and we Type 25 and press 7571 06:25:54,120 --> 06:26:01,000 calculate the cost is now a number and 7572 06:25:57,558 --> 06:26:04,760 it will do the math properly and give us 7573 06:26:01,000 --> 06:26:07,958 35 so remember whenever we get a value 7574 06:26:04,760 --> 06:26:10,280 from the Dom the value will be a string 7575 06:26:07,958 --> 06:26:13,200 so if we want to do math with it we have 7576 06:26:10,280 --> 06:26:16,760 to manually convert this value into a 7577 06:26:13,200 --> 06:26:19,798 number first so now let's remove the 7578 06:26:16,760 --> 06:26:21,638 console. logs that we used to figure out 7579 06:26:19,798 --> 06:26:24,080 this 7580 06:26:21,638 --> 06:26:27,160 problem and 7581 06:26:24,080 --> 06:26:30,520 save okay we're going to add one more 7582 06:26:27,160 --> 06:26:32,400 feature to this project let's go back to 7583 06:26:30,520 --> 06:26:35,680 superimple 7584 06:26:32,400 --> 06:26:39,600 dodev and inside the text box we're 7585 06:26:35,680 --> 06:26:42,718 going to Type 25 and press 7586 06:26:39,600 --> 06:26:45,160 enter so pressing enter will also 7587 06:26:42,718 --> 06:26:47,798 calculate the total and display it on 7588 06:26:45,160 --> 06:26:50,760 the page so let's learn how to create 7589 06:26:47,798 --> 06:26:53,320 this feature so if we go back to our 7590 06:26:50,760 --> 06:26:55,240 code so far in this course we've been 7591 06:26:53,320 --> 06:26:57,878 using the attribute 7592 06:26:55,240 --> 06:27:02,280 onclick which runs some JavaScript when 7593 06:26:57,878 --> 06:27:06,360 we click an element HTML has another 7594 06:27:02,280 --> 06:27:09,000 attribute we can use called on key down 7595 06:27:06,360 --> 06:27:12,760 which runs JavaScript when we press a 7596 06:27:09,000 --> 06:27:16,840 key down on our keyboard so inside the 7597 06:27:12,760 --> 06:27:22,958 input this time let's add the attribute 7598 06:27:16,840 --> 06:27:26,320 on key down equals double quotes and 7599 06:27:22,958 --> 06:27:31,440 then inside here 7600 06:27:26,320 --> 06:27:34,920 console. log and the string 7601 06:27:31,440 --> 06:27:38,958 typing so now every time we type in the 7602 06:27:34,920 --> 06:27:42,478 input it will console.log this message 7603 06:27:38,958 --> 06:27:45,638 let's save it and give it a try we'll go 7604 06:27:42,478 --> 06:27:49,638 to the tab for this file and then inside 7605 06:27:45,638 --> 06:27:51,840 the text box we'll type some numbers and 7606 06:27:49,638 --> 06:27:54,718 you can see that every time we type 7607 06:27:51,840 --> 06:27:56,360 something it will run this code and 7608 06:27:54,718 --> 06:27:58,840 display this 7609 06:27:56,360 --> 06:28:02,680 message so that's how we do something 7610 06:27:58,840 --> 06:28:06,478 when we type in an element so clicks and 7611 06:28:02,680 --> 06:28:10,478 key Downs are known known as events on 7612 06:28:06,478 --> 06:28:13,760 click and on key down are known as event 7613 06:28:10,478 --> 06:28:17,400 listeners they check for events and runs 7614 06:28:13,760 --> 06:28:20,240 JavaScript when those events happen now 7615 06:28:17,400 --> 06:28:21,958 in addition to clicks and key Downs 7616 06:28:20,240 --> 06:28:23,878 there are many other events we can 7617 06:28:21,958 --> 06:28:26,680 listen for in 7618 06:28:23,878 --> 06:28:28,840 JavaScript okay so now let's figure out 7619 06:28:26,680 --> 06:28:31,920 what steps we need to do for this 7620 06:28:28,840 --> 06:28:34,920 project when we press a key down in the 7621 06:28:31,920 --> 06:28:38,200 text box we're going to check if the key 7622 06:28:34,920 --> 06:28:41,080 is the enter key if it is we'll do the 7623 06:28:38,200 --> 06:28:44,600 same thing as the calculate 7624 06:28:41,080 --> 06:28:47,958 button so how do we know what key we 7625 06:28:44,600 --> 06:28:51,120 pressed inside on key down we're 7626 06:28:47,958 --> 06:28:54,600 actually given a special object called 7627 06:28:51,120 --> 06:28:56,798 event so instead of this string let's 7628 06:28:54,600 --> 06:28:58,760 remove it and we'll 7629 06:28:56,798 --> 06:29:01,240 console.log 7630 06:28:58,760 --> 06:29:03,600 event so this is an object that's 7631 06:29:01,240 --> 06:29:06,798 provided to us by JavaScript and it 7632 06:29:03,600 --> 06:29:09,878 contains information about the event 7633 06:29:06,798 --> 06:29:15,040 every event listener including on click 7634 06:29:09,878 --> 06:29:18,558 also gets an event object if we 7635 06:29:15,040 --> 06:29:22,160 save and type in this text 7636 06:29:18,558 --> 06:29:25,798 box it will show us that event is this 7637 06:29:22,160 --> 06:29:29,320 object and inside the object it has a 7638 06:29:25,798 --> 06:29:33,040 bunch of information including the key 7639 06:29:29,320 --> 06:29:38,600 that we typed so here let's use the 7640 06:29:33,040 --> 06:29:42,520 event object and we'll get dot key and 7641 06:29:38,600 --> 06:29:45,080 save so now inside this text box if we 7642 06:29:42,520 --> 06:29:47,798 type something it will tell us the key 7643 06:29:45,080 --> 06:29:51,040 that we pressed and if we press 7644 06:29:47,798 --> 06:29:54,120 enter it will tell us that the key is 7645 06:29:51,040 --> 06:29:57,440 enter so the next step is to check if 7646 06:29:54,120 --> 06:30:00,840 the key is enter and we can do that 7647 06:29:57,440 --> 06:30:04,400 using an if statement so in our 7648 06:30:00,840 --> 06:30:08,600 code we'll type a new line 7649 06:30:04,400 --> 06:30:12,360 if brackets and curly brackets and 7650 06:30:08,600 --> 06:30:18,840 inside the condition we'll check if 7651 06:30:12,360 --> 06:30:22,040 event. key is equal to the string 7652 06:30:18,840 --> 06:30:23,680 enter if it is equal to enter we're 7653 06:30:22,040 --> 06:30:26,718 going to do the same thing as the 7654 06:30:23,680 --> 06:30:30,240 calculate button so we can actually just 7655 06:30:26,718 --> 06:30:35,440 use this function again inside the curly 7656 06:30:30,240 --> 06:30:37,798 brackets we'll type calculate 7657 06:30:35,440 --> 06:30:40,040 total and 7658 06:30:37,798 --> 06:30:43,478 brackets and 7659 06:30:40,040 --> 06:30:48,240 save so now let's go back to our project 7660 06:30:43,478 --> 06:30:51,440 and type 25 enter and it will calculate 7661 06:30:48,240 --> 06:30:53,200 the total and display it on the page so 7662 06:30:51,440 --> 06:30:57,160 that's how we create this keyboard 7663 06:30:53,200 --> 06:30:59,958 feature of this project lastly let's 7664 06:30:57,160 --> 06:31:03,360 clean up our code and move this into a 7665 06:30:59,958 --> 06:31:05,080 function like the rest of our code so 7666 06:31:03,360 --> 06:31:08,360 inside the script 7667 06:31:05,080 --> 06:31:10,160 element let's create a new function 7668 06:31:08,360 --> 06:31:13,320 using 7669 06:31:10,160 --> 06:31:19,798 function and we'll name it 7670 06:31:13,320 --> 06:31:22,638 handle cost key down brackets and curly 7671 06:31:19,798 --> 06:31:26,240 brackets and let's move this code into 7672 06:31:22,638 --> 06:31:28,040 the function we'll select it 7673 06:31:26,240 --> 06:31:34,120 rightclick 7674 06:31:28,040 --> 06:31:36,120 cut and in here right click and paste 7675 06:31:34,120 --> 06:31:39,440 if we need to fix the formatting we can 7676 06:31:36,120 --> 06:31:41,320 select these lines and press Tab and 7677 06:31:39,440 --> 06:31:44,080 let's remove this 7678 06:31:41,320 --> 06:31:45,520 console.log and now up here we're going 7679 06:31:44,080 --> 06:31:47,200 to call this 7680 06:31:45,520 --> 06:31:53,000 function 7681 06:31:47,200 --> 06:31:55,240 handle cost key down and 7682 06:31:53,000 --> 06:31:59,040 brackets now the last thing we have to 7683 06:31:55,240 --> 06:32:01,280 do is that event does not exist inside 7684 06:31:59,040 --> 06:32:04,440 this function so we have to put the 7685 06:32:01,280 --> 06:32:06,798 event object into the function 7686 06:32:04,440 --> 06:32:09,200 and remember to put a value into a 7687 06:32:06,798 --> 06:32:12,398 function we can use a 7688 06:32:09,200 --> 06:32:16,920 parameter so between these brackets we 7689 06:32:12,398 --> 06:32:18,920 can create a parameter called event and 7690 06:32:16,920 --> 06:32:21,920 now to save something inside this 7691 06:32:18,920 --> 06:32:24,798 parameter we're going to go up to these 7692 06:32:21,920 --> 06:32:27,878 brackets and type 7693 06:32:24,798 --> 06:32:30,360 event this will take the event object 7694 06:32:27,878 --> 06:32:34,040 and put it inside the function where we 7695 06:32:30,360 --> 06:32:35,120 can use it just like a variable so now 7696 06:32:34,040 --> 06:32:41,160 if we 7697 06:32:35,120 --> 06:32:44,520 save and try out the project 25 enter so 7698 06:32:41,160 --> 06:32:47,478 this still works and now we finished all 7699 06:32:44,520 --> 06:32:49,440 three projects in this lesson so in the 7700 06:32:47,478 --> 06:32:51,760 rest of this lesson we're going to learn 7701 06:32:49,440 --> 06:32:54,320 some more details about 7702 06:32:51,760 --> 06:32:57,440 JavaScript in the last project we 7703 06:32:54,320 --> 06:33:01,440 learned how to manually convert a string 7704 06:32:57,440 --> 06:33:04,120 into a number using the number function 7705 06:33:01,440 --> 06:33:07,440 we can also convert the other way from a 7706 06:33:04,120 --> 06:33:09,878 number to a string so let's go to the 7707 06:33:07,440 --> 06:33:15,240 top of the script 7708 06:33:09,878 --> 06:33:18,558 element and we can type capital S string 7709 06:33:15,240 --> 06:33:21,440 and brackets so the built-in string 7710 06:33:18,558 --> 06:33:24,440 function will convert a value into a 7711 06:33:21,440 --> 06:33:28,200 string between the brackets we can put a 7712 06:33:24,440 --> 06:33:30,680 number like 25 and this will convert the 7713 06:33:28,200 --> 06:33:33,360 number into the string 7714 06:33:30,680 --> 06:33:35,600 25 the next detail we're going to learn 7715 06:33:33,360 --> 06:33:40,200 is a special behavior of 7716 06:33:35,600 --> 06:33:45,200 strings So Below this line we can type 7717 06:33:40,200 --> 06:33:47,878 console do log the string 7718 06:33:45,200 --> 06:33:51,600 25 minus 7719 06:33:47,878 --> 06:33:55,320 5 so if a string only contains a number 7720 06:33:51,600 --> 06:33:57,440 and we subtract multiply or divide 7721 06:33:55,320 --> 06:34:01,000 JavaScript will automatically convert 7722 06:33:57,440 --> 06:34:05,760 the string into a number so this gets 7723 06:34:01,000 --> 06:34:10,280 converted into the number 25 minus 5 and 7724 06:34:05,760 --> 06:34:13,798 if we save it will do math and give us 7725 06:34:10,280 --> 06:34:17,200 20 so this is another example of type 7726 06:34:13,798 --> 06:34:19,920 coercion or automatic type 7727 06:34:17,200 --> 06:34:22,520 conversion however it's not recommended 7728 06:34:19,920 --> 06:34:25,360 to do math with strings like this 7729 06:34:22,520 --> 06:34:30,718 because adding Works differently for 7730 06:34:25,360 --> 06:34:30,718 example on a new line let's type 7731 06:34:30,840 --> 06:34:38,760 console.log the string 25 7732 06:34:34,680 --> 06:34:41,840 + 5 and 7733 06:34:38,760 --> 06:34:45,760 save this will convert the number into a 7734 06:34:41,840 --> 06:34:48,878 string and combine them together into 7735 06:34:45,760 --> 06:34:51,638 255 so because adding strings Works 7736 06:34:48,878 --> 06:34:54,840 differently the best practice is always 7737 06:34:51,638 --> 06:34:58,240 do math using numbers don't do math 7738 06:34:54,840 --> 06:35:00,600 using strings if we need to we can 7739 06:34:58,240 --> 06:35:03,920 manually convert the string into a 7740 06:35:00,600 --> 06:35:06,600 number using the number function like we 7741 06:35:03,920 --> 06:35:09,600 did here the last thing we're going to 7742 06:35:06,600 --> 06:35:13,398 learn in this lesson is another built-in 7743 06:35:09,600 --> 06:35:15,638 object called the window object so in 7744 06:35:13,398 --> 06:35:18,680 this lesson we learned about the 7745 06:35:15,638 --> 06:35:21,760 document object the document object 7746 06:35:18,680 --> 06:35:25,240 represents the web page the window 7747 06:35:21,760 --> 06:35:28,080 object represents the browser so let's 7748 06:35:25,240 --> 06:35:32,398 go into our script element and Try It 7749 06:35:28,080 --> 06:35:35,878 Out Below this we'll Type 7750 06:35:32,398 --> 06:35:39,040 window so so this is the window object 7751 06:35:35,878 --> 06:35:41,718 and it represents the browser because it 7752 06:35:39,040 --> 06:35:45,360 represents the browser everything inside 7753 06:35:41,718 --> 06:35:49,160 the browser is inside the window object 7754 06:35:45,360 --> 06:35:52,600 so let's think what's inside the browser 7755 06:35:49,160 --> 06:35:55,558 well the web page is inside the browser 7756 06:35:52,600 --> 06:35:59,600 so the document object is inside the 7757 06:35:55,558 --> 06:36:01,600 window object if we type 7758 06:35:59,600 --> 06:36:04,200 window. 7759 06:36:01,600 --> 06:36:06,600 document this will give us the same 7760 06:36:04,200 --> 06:36:10,200 document object that we've been using in 7761 06:36:06,600 --> 06:36:13,600 this lesson so what else is part of the 7762 06:36:10,200 --> 06:36:18,878 browser well the console is also part of 7763 06:36:13,600 --> 06:36:20,718 the browser so here if we type 7764 06:36:18,878 --> 06:36:23,240 window. 7765 06:36:20,718 --> 06:36:25,920 console this will give us the same 7766 06:36:23,240 --> 06:36:30,558 console object we've been using in this 7767 06:36:25,920 --> 06:36:35,040 course so we can actually do 7768 06:36:30,558 --> 06:36:36,160 console.log and the string window 7769 06:36:35,040 --> 06:36:41,000 and 7770 06:36:36,160 --> 06:36:43,360 save and this will do the same thing as 7771 06:36:41,000 --> 06:36:46,920 console.log what else is part of the 7772 06:36:43,360 --> 06:36:50,478 browser well popups are also part of the 7773 06:36:46,920 --> 06:36:52,240 browser so if we type 7774 06:36:50,478 --> 06:36:55,200 window. 7775 06:36:52,240 --> 06:36:57,200 alert this will give us the same alert 7776 06:36:55,200 --> 06:36:59,320 function that we've been using in this 7777 06:36:57,200 --> 06:37:01,920 course to create 7778 06:36:59,320 --> 06:37:05,080 popups however you might have noticed 7779 06:37:01,920 --> 06:37:07,320 that we never Type window window. doent 7780 06:37:05,080 --> 06:37:09,160 or window. 7781 06:37:07,320 --> 06:37:12,320 console.log and that's because the 7782 06:37:09,160 --> 06:37:15,478 window object has a shortcut we don't 7783 06:37:12,320 --> 06:37:18,840 have to type window dot at the front we 7784 06:37:15,478 --> 06:37:21,680 can just type document or console and 7785 06:37:18,840 --> 06:37:25,558 JavaScript will automatically add window 7786 06:37:21,680 --> 06:37:28,280 dot at the front for us so window is a 7787 06:37:25,558 --> 06:37:31,080 built-in object that represents the 7788 06:37:28,280 --> 06:37:33,840 browser we don't have to type out window 7789 06:37:31,080 --> 06:37:36,360 dot ourselves because JavaScript adds it 7790 06:37:33,840 --> 06:37:39,398 for us automatically but it's still nice 7791 06:37:36,360 --> 06:37:41,680 to know how this works behind the scenes 7792 06:37:39,398 --> 06:37:43,600 and that's the end of this lesson in 7793 06:37:41,680 --> 06:37:47,000 this lesson we learned about the 7794 06:37:43,600 --> 06:37:49,760 document object model or the Dom which 7795 06:37:47,000 --> 06:37:53,000 allows JavaScript to fully control the 7796 06:37:49,760 --> 06:37:55,638 web page we learned how to use document. 7797 06:37:53,000 --> 06:37:58,040 Query selector to get elements from the 7798 06:37:55,638 --> 06:38:00,638 web page and put them inside our 7799 06:37:58,040 --> 06:38:04,398 JavaScript we learned how to use inner 7800 06:38:00,638 --> 06:38:07,240 HTML to modify the HTML inside an 7801 06:38:04,398 --> 06:38:09,558 element we worked on three projects 7802 06:38:07,240 --> 06:38:12,120 using the Dom we learned how to make 7803 06:38:09,558 --> 06:38:15,200 elements interactive with the keyboard 7804 06:38:12,120 --> 06:38:18,280 using on key down and we learned some 7805 06:38:15,200 --> 06:38:21,398 more details about strings as well as 7806 06:38:18,280 --> 06:38:23,920 the window object which represents the 7807 06:38:21,398 --> 06:38:29,478 browser here are some exercises you can 7808 06:38:23,920 --> 06:38:29,478 do on your own to practice using the Dom 7809 06:38:54,520 --> 06:38:59,558 in this lesson we're going to add CSS to 7810 06:38:57,520 --> 06:39:02,558 all of our projects and we're going to 7811 06:38:59,558 --> 06:39:05,600 finish our rock paper scissors project 7812 06:39:02,558 --> 06:39:08,280 CSS is a different language that changes 7813 06:39:05,600 --> 06:39:10,360 the appearance of our website it's one 7814 06:39:08,280 --> 06:39:13,920 of the three languages that we use to 7815 06:39:10,360 --> 06:39:15,718 create websites so first let's look at 7816 06:39:13,920 --> 06:39:19,200 the projects we're going to work on in 7817 06:39:15,718 --> 06:39:21,558 this lesson we'll go to our browser and 7818 06:39:19,200 --> 06:39:26,120 create a new tab at the 7819 06:39:21,558 --> 06:39:29,160 top and here we're going to type 7820 06:39:26,120 --> 06:39:33,920 super simple 7821 06:39:29,160 --> 06:39:36,440 dodev SL projects slash 7822 06:39:33,920 --> 06:39:38,040 Dom Das 7823 06:39:36,440 --> 06:39:40,920 with- 7824 06:39:38,040 --> 06:39:43,478 CSS and press 7825 06:39:40,920 --> 06:39:45,600 enter so these are the same three 7826 06:39:43,478 --> 06:39:48,160 projects we created in the previous 7827 06:39:45,600 --> 06:39:52,680 lesson except this time we're going to 7828 06:39:48,160 --> 06:39:55,200 add CSS to make these projects look nice 7829 06:39:52,680 --> 06:39:58,398 let's start by creating a copy of the 7830 06:39:55,200 --> 06:40:01,000 Project's HTML file from the previous 7831 06:39:58,398 --> 06:40:04,440 lesson we'll go to our code 7832 06:40:01,000 --> 06:40:07,240 editor and click this icon to show our 7833 06:40:04,440 --> 06:40:09,000 files and then here we're going to 7834 06:40:07,240 --> 06:40:12,840 select 7835 06:40:09,000 --> 06:40:14,920 09-- projects we're going to right 7836 06:40:12,840 --> 06:40:20,080 click 7837 06:40:14,920 --> 06:40:25,080 copy and down here right click and 7838 06:40:20,080 --> 06:40:27,520 paste and let's rename this file by 7839 06:40:25,080 --> 06:40:31,600 right-clicking 7840 06:40:27,520 --> 06:40:34,080 rename at the end we're going to remove 7841 06:40:31,600 --> 06:40:38,240 copy and at the beginning 7842 06:40:34,080 --> 06:40:40,878 we're going to change it to 10 and press 7843 06:40:38,240 --> 06:40:43,920 enter now let's hide our 7844 06:40:40,878 --> 06:40:47,200 files and we're going to open 10 Dom 7845 06:40:43,920 --> 06:40:50,840 projects in the browser by 7846 06:40:47,200 --> 06:40:55,680 right-clicking open with live 7847 06:40:50,840 --> 06:40:55,680 server now we can close our previous 7848 06:40:55,920 --> 06:41:02,718 tabs as well as the previous code from 7849 06:40:58,718 --> 06:41:04,478 lesson 9 which is this file and these 7850 06:41:02,718 --> 06:41:07,240 two 7851 06:41:04,478 --> 06:41:09,718 and now we're ready to add CSS to our 7852 06:41:07,240 --> 06:41:12,760 projects we'll start with the YouTube 7853 06:41:09,718 --> 06:41:13,840 subscribe button let's go to the tab for 7854 06:41:12,760 --> 06:41:16,160 our 7855 06:41:13,840 --> 06:41:19,600 project and we're going to style this 7856 06:41:16,160 --> 06:41:22,320 button first so we reviewed CSS in 7857 06:41:19,600 --> 06:41:25,718 lesson 4 and one of the ways we can 7858 06:41:22,320 --> 06:41:29,478 write CSS code is using the style 7859 06:41:25,718 --> 06:41:32,398 element so inside the head section we're 7860 06:41:29,478 --> 06:41:37,000 going to create an element using less 7861 06:41:32,398 --> 06:41:41,040 than style greater than so the style 7862 06:41:37,000 --> 06:41:44,520 element lets us write CSS code inside so 7863 06:41:41,040 --> 06:41:48,200 inside here we're going to write 7864 06:41:44,520 --> 06:41:50,558 button open curly bracket close curly 7865 06:41:48,200 --> 06:41:54,558 bracket so the code in front of the 7866 06:41:50,558 --> 06:41:56,680 brackets is called the CSS selector this 7867 06:41:54,558 --> 06:42:00,200 tells the computer which elements we 7868 06:41:56,680 --> 06:42:03,398 want to style or change the appearance 7869 06:42:00,200 --> 06:42:06,558 so button will style all button elements 7870 06:42:03,398 --> 06:42:09,600 on the page but in this situation we 7871 06:42:06,558 --> 06:42:11,718 only want to style the Subscribe button 7872 06:42:09,600 --> 06:42:14,878 because the calculate button will be 7873 06:42:11,718 --> 06:42:18,320 styled differently so to style a 7874 06:42:14,878 --> 06:42:21,200 specific element we can use the class 7875 06:42:18,320 --> 06:42:24,760 attribute so if we look at our button 7876 06:42:21,200 --> 06:42:28,040 currently it already has a class of JS 7877 06:42:24,760 --> 06:42:31,240 subscribe button but this class is being 7878 06:42:28,040 --> 06:42:33,478 used by our JavaScript a good practice 7879 06:42:31,240 --> 06:42:34,920 is to add a different class that will be 7880 06:42:33,478 --> 06:42:37,760 used by 7881 06:42:34,920 --> 06:42:40,398 CSS so an element can have multiple 7882 06:42:37,760 --> 06:42:43,878 classes we just separate them with a 7883 06:42:40,398 --> 06:42:46,798 space so inside this class attribute 7884 06:42:43,878 --> 06:42:49,478 we're going to type space and add 7885 06:42:46,798 --> 06:42:52,360 another class 7886 06:42:49,478 --> 06:42:56,000 subcribe dash 7887 06:42:52,360 --> 06:42:58,200 button now this element has two classes 7888 06:42:56,000 --> 06:42:59,200 and we're going to use this second class 7889 06:42:58,200 --> 06:43:02,798 in our 7890 06:42:59,200 --> 06:43:05,478 CSS so to style a specific class we're 7891 06:43:02,798 --> 06:43:09,398 going to remove button and we're going 7892 06:43:05,478 --> 06:43:11,958 to type a DOT so starting with a DOT 7893 06:43:09,398 --> 06:43:14,798 means we're going to style a class 7894 06:43:11,958 --> 06:43:17,360 rather than an element name so we'll 7895 06:43:14,798 --> 06:43:20,040 style the class 7896 06:43:17,360 --> 06:43:24,040 subscribe Das 7897 06:43:20,040 --> 06:43:26,520 button which is this button right here 7898 06:43:24,040 --> 06:43:28,360 and now inside these curly brackets 7899 06:43:26,520 --> 06:43:33,520 we're going to tell the computer how to 7900 06:43:28,360 --> 06:43:36,718 style this button for example border 7901 06:43:33,520 --> 06:43:39,120 colon none and 7902 06:43:36,718 --> 06:43:42,240 semicolon if we 7903 06:43:39,120 --> 06:43:45,080 save this will remove the border from 7904 06:43:42,240 --> 06:43:47,958 the button so it no longer has a border 7905 06:43:45,080 --> 06:43:51,478 like this button so that's how we style 7906 06:43:47,958 --> 06:43:55,160 a specific element with CSS we add a 7907 06:43:51,478 --> 06:43:58,680 class to the element and then inside CSS 7908 06:43:55,160 --> 06:44:00,920 we start with a DOT and the class and 7909 06:43:58,680 --> 06:44:03,558 between the curly brackets the code on 7910 06:44:00,920 --> 06:44:05,600 the left is called the property 7911 06:44:03,558 --> 06:44:09,398 this tells a computer what we want to 7912 06:44:05,600 --> 06:44:11,160 change on the right is called the value 7913 06:44:09,398 --> 06:44:14,878 this tells a computer what we want to 7914 06:44:11,160 --> 06:44:17,840 change the property to we separate the 7915 06:44:14,878 --> 06:44:20,760 property and value with a colon and we 7916 06:44:17,840 --> 06:44:23,600 end the whole thing with a semicolon so 7917 06:44:20,760 --> 06:44:26,878 this is also called a property value 7918 06:44:23,600 --> 06:44:30,240 pair as a reminder this is just a review 7919 06:44:26,878 --> 06:44:33,200 of CSS if you want to learn CSS in more 7920 06:44:30,240 --> 06:44:36,478 detail you can check out my HTML and and 7921 06:44:33,200 --> 06:44:39,360 CSS full course in the video 7922 06:44:36,478 --> 06:44:41,798 description so we just added one style 7923 06:44:39,360 --> 06:44:44,440 to this button we're going to add more 7924 06:44:41,798 --> 06:44:47,160 Styles so that it looks like our final 7925 06:44:44,440 --> 06:44:49,760 design to make this easier I'm going to 7926 06:44:47,160 --> 06:44:52,840 rearrange my windows and I'm going to 7927 06:44:49,760 --> 06:44:55,080 put the final design at the bottom and 7928 06:44:52,840 --> 06:44:57,798 then our project at the 7929 06:44:55,080 --> 06:44:58,878 top this will make it easier to compare 7930 06:44:57,798 --> 06:45:01,600 the two 7931 06:44:58,878 --> 06:45:03,520 designs next to make it look like the 7932 06:45:01,600 --> 06:45:07,200 final design we're we're going to style 7933 06:45:03,520 --> 06:45:08,000 our element one by one let's go back to 7934 06:45:07,200 --> 06:45:12,080 our 7935 06:45:08,000 --> 06:45:15,398 code and press enter and next let's 7936 06:45:12,080 --> 06:45:18,360 change the background color to Black to 7937 06:45:15,398 --> 06:45:24,320 do that we'll type 7938 06:45:18,360 --> 06:45:26,240 background Das color colon black and 7939 06:45:24,320 --> 06:45:29,558 semicolon and 7940 06:45:26,240 --> 06:45:32,958 save now our button has a black 7941 06:45:29,558 --> 06:45:36,280 background let's keep going one by one 7942 06:45:32,958 --> 06:45:41,558 we change the text color to white next 7943 06:45:36,280 --> 06:45:45,478 so in our code we'll type color colon 7944 06:45:41,558 --> 06:45:48,040 white and semicolon and 7945 06:45:45,478 --> 06:45:50,120 save now if you're wondering how I know 7946 06:45:48,040 --> 06:45:52,958 all these properties you can usually 7947 06:45:50,120 --> 06:45:54,878 find them by searching in Google just 7948 06:45:52,958 --> 06:45:56,120 make sure you search for what you are 7949 06:45:54,878 --> 06:46:00,040 trying to 7950 06:45:56,120 --> 06:46:02,360 do let's keep going one by one the next 7951 06:46:00,040 --> 06:46:06,080 thing we'll change is we're going to add 7952 06:46:02,360 --> 06:46:09,840 spacing on the inside of the button in 7953 06:46:06,080 --> 06:46:13,440 CSS space on the inside of an element is 7954 06:46:09,840 --> 06:46:16,958 called padding so we'll create a new 7955 06:46:13,440 --> 06:46:22,398 line and we'll use the property 7956 06:46:16,958 --> 06:46:24,600 padding Das toop colon 7957 06:46:22,398 --> 06:46:27,600 10px and 7958 06:46:24,600 --> 06:46:30,360 save so this will add space on the 7959 06:46:27,600 --> 06:46:33,718 inside of the element called the padding 7960 06:46:30,360 --> 06:46:38,000 at the top of 10 PX 7961 06:46:33,718 --> 06:46:40,600 so the PX here means pixels a pixel is a 7962 06:46:38,000 --> 06:46:43,160 unit of measurement in computers your 7963 06:46:40,600 --> 06:46:45,798 screen is made up of many small little 7964 06:46:43,160 --> 06:46:49,160 squares each of these squares is called 7965 06:46:45,798 --> 06:46:51,920 a pixel so a pixel is the smallest thing 7966 06:46:49,160 --> 06:46:54,920 that you can have on your screen screen 7967 06:46:51,920 --> 06:46:58,478 sizes are measured in pixels images and 7968 06:46:54,920 --> 06:47:00,320 video sizes are also measured in pixels 7969 06:46:58,478 --> 06:47:02,520 and don't worry the more you work with 7970 06:47:00,320 --> 06:47:06,638 pixels the more you'll get used to how 7971 06:47:02,520 --> 06:47:10,200 big a pixel is so in this example 10 7972 06:47:06,638 --> 06:47:11,958 pixels is equal to this amount of space 7973 06:47:10,200 --> 06:47:15,478 and it looks pretty close to the final 7974 06:47:11,958 --> 06:47:17,920 design so let's keep going we'll add 7975 06:47:15,478 --> 06:47:20,718 space on the inside of the element at 7976 06:47:17,920 --> 06:47:27,320 the bottom this time so we'll create a 7977 06:47:20,718 --> 06:47:28,840 new line and type padding Dash bottom 7978 06:47:27,320 --> 06:47:33,440 colon 7979 06:47:28,840 --> 06:47:35,760 10px again and Save 7980 06:47:33,440 --> 06:47:37,798 and that adds space at the bottom of the 7981 06:47:35,760 --> 06:47:40,958 element of 10 7982 06:47:37,798 --> 06:47:44,478 pixels next let's increase the space on 7983 06:47:40,958 --> 06:47:52,120 the left and the right we'll go into our 7984 06:47:44,478 --> 06:47:59,600 code and add padding Das left colon 10 7985 06:47:52,120 --> 06:48:03,040 pixels and padding Das right colon 10 7986 06:47:59,600 --> 06:48:05,280 pixels let's save 7987 06:48:03,040 --> 06:48:07,798 and it added some extra space on the 7988 06:48:05,280 --> 06:48:10,040 left and right but I think our final 7989 06:48:07,798 --> 06:48:12,798 design has a little more space on the 7990 06:48:10,040 --> 06:48:16,200 left and right so let's go back and 7991 06:48:12,798 --> 06:48:19,280 increase our padding so instead of 10 7992 06:48:16,200 --> 06:48:21,520 let's try 15 on both 7993 06:48:19,280 --> 06:48:24,798 sides so we'll 7994 06:48:21,520 --> 06:48:26,398 save and now the spacing looks close 7995 06:48:24,798 --> 06:48:29,878 enough to the final 7996 06:48:26,398 --> 06:48:33,478 design so in CSS it's normal to do some 7997 06:48:29,878 --> 06:48:36,920 trial and error to get the styling right 7998 06:48:33,478 --> 06:48:40,840 let's keep styling this one by one next 7999 06:48:36,920 --> 06:48:43,920 we'll make the text thicker or bold so 8000 06:48:40,840 --> 06:48:46,200 let's go into our code and to change the 8001 06:48:43,920 --> 06:48:53,040 thickness of the text we'll use the 8002 06:48:46,200 --> 06:48:55,120 property font Das weight colon 8003 06:48:53,040 --> 06:48:58,920 bold let's 8004 06:48:55,120 --> 06:49:01,360 save and that will make our text bold so 8005 06:48:58,920 --> 06:49:03,760 we're almost there now let's make the 8006 06:49:01,360 --> 06:49:07,320 corners rounded look like in the final 8007 06:49:03,760 --> 06:49:09,680 design we'll go back to our CSS and the 8008 06:49:07,320 --> 06:49:11,360 property for making the corners round is 8009 06:49:09,680 --> 06:49:13,798 called 8010 06:49:11,360 --> 06:49:18,120 border Das 8011 06:49:13,798 --> 06:49:20,320 radius and we'll make it colon 5 8012 06:49:18,120 --> 06:49:22,920 pixels let's 8013 06:49:20,320 --> 06:49:26,398 save and you can see that it made our 8014 06:49:22,920 --> 06:49:28,398 Corners round so the pixels here tells 8015 06:49:26,398 --> 06:49:31,440 the computer how round we want the 8016 06:49:28,398 --> 06:49:33,280 corners to be the higher the pixels the 8017 06:49:31,440 --> 06:49:37,398 more round it will get 8018 06:49:33,280 --> 06:49:40,600 so if we change this to 10 pixels and 8019 06:49:37,398 --> 06:49:43,160 save it will make the corners even more 8020 06:49:40,600 --> 06:49:46,360 round so let's change this to something 8021 06:49:43,160 --> 06:49:51,280 really big to make it completely round 8022 06:49:46,360 --> 06:49:54,478 we'll change this to 50 pixels and 8023 06:49:51,280 --> 06:49:57,280 save and now our button is completely 8024 06:49:54,478 --> 06:49:59,680 round the last style we're going to add 8025 06:49:57,280 --> 06:50:02,320 is in the final design if we put our 8026 06:49:59,680 --> 06:50:06,120 Mouse over the button it turns into this 8027 06:50:02,320 --> 06:50:07,760 hand the icon so we call this a pointer 8028 06:50:06,120 --> 06:50:11,760 and we're going to do the same thing in 8029 06:50:07,760 --> 06:50:14,558 our project so let's go to our code and 8030 06:50:11,760 --> 06:50:17,080 to create that pointer icon we're going 8031 06:50:14,558 --> 06:50:20,718 to use the property 8032 06:50:17,080 --> 06:50:23,120 cursor colon 8033 06:50:20,718 --> 06:50:26,398 pointer and let's 8034 06:50:23,120 --> 06:50:28,080 save and put our Mouse over the button 8035 06:50:26,398 --> 06:50:31,718 and you'll notice it turns into this 8036 06:50:28,080 --> 06:50:34,360 hand pointer icon and that's it now our 8037 06:50:31,718 --> 06:50:37,520 subscribe button looks like the final 8038 06:50:34,360 --> 06:50:39,718 design next let's click the Subscribe 8039 06:50:37,520 --> 06:50:42,520 button you'll notice that it only 8040 06:50:39,718 --> 06:50:43,798 changes the text but if we click the 8041 06:50:42,520 --> 06:50:46,398 final 8042 06:50:43,798 --> 06:50:49,440 design it also changes the background 8043 06:50:46,398 --> 06:50:52,040 color and the text color so we're going 8044 06:50:49,440 --> 06:50:53,360 to learn how to change the CSS when we 8045 06:50:52,040 --> 06:50:56,200 click a 8046 06:50:53,360 --> 06:50:58,280 button let's go back to our code and 8047 06:50:56,200 --> 06:50:59,120 this time we're going to scroll down to 8048 06:50:58,280 --> 06:51:02,680 our 8049 06:50:59,120 --> 06:51:05,600 JavaScript which is here so we learned 8050 06:51:02,680 --> 06:51:08,040 in the previous lesson we can get HTML 8051 06:51:05,600 --> 06:51:10,558 elements from the page and put them 8052 06:51:08,040 --> 06:51:13,280 inside our JavaScript and we also 8053 06:51:10,558 --> 06:51:15,958 learned that these HTML elements are 8054 06:51:13,280 --> 06:51:19,440 just JavaScript objects and they can 8055 06:51:15,958 --> 06:51:20,958 have properties like inner text and 8056 06:51:19,440 --> 06:51:24,080 inner 8057 06:51:20,958 --> 06:51:27,638 HTML every element has another property 8058 06:51:24,080 --> 06:51:31,120 we can use called class list class list 8059 06:51:27,638 --> 06:51:33,600 gives us control of the class attribute 8060 06:51:31,120 --> 06:51:37,240 so one solution we can do is when we 8061 06:51:33,600 --> 06:51:39,680 click the button we can add a class and 8062 06:51:37,240 --> 06:51:42,040 then this class will have different 8063 06:51:39,680 --> 06:51:44,878 styles so let's see how we do that in 8064 06:51:42,040 --> 06:51:47,718 our code we'll scroll down to the 8065 06:51:44,878 --> 06:51:50,600 Subscribe function and when we click the 8066 06:51:47,718 --> 06:51:53,920 button and make the button subscribed 8067 06:51:50,600 --> 06:51:59,520 we're also going to add a class so let's 8068 06:51:53,920 --> 06:52:05,040 create a new line and we'll type button 8069 06:51:59,520 --> 06:52:09,040 element dot class capital L 8070 06:52:05,040 --> 06:52:10,798 list so class list itself is an object 8071 06:52:09,040 --> 06:52:15,000 which means it has properties and 8072 06:52:10,798 --> 06:52:19,200 methods we can use one method is called 8073 06:52:15,000 --> 06:52:25,040 add which adds a class to an element so 8074 06:52:19,200 --> 06:52:27,878 after this let's type dot add and 8075 06:52:25,040 --> 06:52:30,878 brackets and now inside these brackets 8076 06:52:27,878 --> 06:52:32,840 we're going to put a string so this 8077 06:52:30,878 --> 06:52:36,000 string is a class that we want want to 8078 06:52:32,840 --> 06:52:38,798 add when we click this button let's add 8079 06:52:36,000 --> 06:52:41,478 a class called 8080 06:52:38,798 --> 06:52:43,878 is- 8081 06:52:41,478 --> 06:52:47,080 subscribed and 8082 06:52:43,878 --> 06:52:50,440 save so now every time we click this 8083 06:52:47,080 --> 06:52:54,120 button it will add a class to the button 8084 06:52:50,440 --> 06:52:58,320 called is- subscribed let's give it a 8085 06:52:54,120 --> 06:53:00,520 try first we'll rightclick on the page 8086 06:52:58,320 --> 06:53:04,240 and then click 8087 06:53:00,520 --> 06:53:06,120 inspect and we'll go to the elements Tab 8088 06:53:04,240 --> 06:53:07,200 and I'll rearrange my windows so you can 8089 06:53:06,120 --> 06:53:10,760 see 8090 06:53:07,200 --> 06:53:14,320 it and then inside the HTML pay 8091 06:53:10,760 --> 06:53:15,718 attention to the class of the button now 8092 06:53:14,320 --> 06:53:18,680 when we click the 8093 06:53:15,718 --> 06:53:22,200 button it's going to add an extra class 8094 06:53:18,680 --> 06:53:25,320 called is- subscribed so that's what 8095 06:53:22,200 --> 06:53:28,080 this line of code does and now that this 8096 06:53:25,320 --> 06:53:31,638 button has the is subscribed class we 8097 06:53:28,080 --> 06:53:33,040 can style it in our CSS so let's go back 8098 06:53:31,638 --> 06:53:35,440 to our code 8099 06:53:33,040 --> 06:53:40,160 and we'll scroll up to our 8100 06:53:35,440 --> 06:53:44,520 CSS and here we'll type dot to style a 8101 06:53:40,160 --> 06:53:46,280 class and we'll style the class is- 8102 06:53:44,520 --> 06:53:48,200 subscribed 8103 06:53:46,280 --> 06:53:50,638 is- 8104 06:53:48,200 --> 06:53:53,160 subscribed and curly 8105 06:53:50,638 --> 06:53:55,558 brackets and now inside these curly 8106 06:53:53,160 --> 06:53:59,080 brackets let's change the background 8107 06:53:55,558 --> 06:54:00,798 color to Gray and the text color to 8108 06:53:59,080 --> 06:54:05,440 Black 8109 06:54:00,798 --> 06:54:10,680 Background Das color colon 8110 06:54:05,440 --> 06:54:13,478 gray and then color colon 8111 06:54:10,680 --> 06:54:16,398 black and make sure to type these Styles 8112 06:54:13,478 --> 06:54:18,600 below the Subscribe button Styles so 8113 06:54:16,398 --> 06:54:19,760 that these Styles will override the 8114 06:54:18,600 --> 06:54:22,398 Styles 8115 06:54:19,760 --> 06:54:25,600 above now if we 8116 06:54:22,398 --> 06:54:29,638 save and then click our 8117 06:54:25,600 --> 06:54:31,798 button it will add the is subscrib class 8118 06:54:29,638 --> 06:54:35,160 and now this button will get our new 8119 06:54:31,798 --> 06:54:38,638 styles that we added here so that's one 8120 06:54:35,160 --> 06:54:41,398 way to change the CSS when we click a 8121 06:54:38,638 --> 06:54:44,040 button all right now I'm going to move 8122 06:54:41,398 --> 06:54:47,398 this back to the 8123 06:54:44,040 --> 06:54:50,040 top and notice that our background color 8124 06:54:47,398 --> 06:54:53,680 is different from the final design the 8125 06:54:50,040 --> 06:54:56,958 final design uses a much lighter gray so 8126 06:54:53,680 --> 06:54:59,160 how do we get this exact color to do 8127 06:54:56,958 --> 06:55:03,798 that we're going to open the elements 8128 06:54:59,160 --> 06:55:06,478 tab again so on this page we're going to 8129 06:55:03,798 --> 06:55:09,520 rightclick click 8130 06:55:06,478 --> 06:55:12,398 inspect and then click the elements 8131 06:55:09,520 --> 06:55:14,798 Tab and I'll rearrange my windows so we 8132 06:55:12,398 --> 06:55:14,798 can see 8133 06:55:15,280 --> 06:55:21,200 it so in this section in the style 8134 06:55:18,398 --> 06:55:24,718 section we can see the CSS that the 8135 06:55:21,200 --> 06:55:27,798 element has so first let's actually find 8136 06:55:24,718 --> 06:55:29,040 this button which is right here and 8137 06:55:27,798 --> 06:55:33,600 we'll click 8138 06:55:29,040 --> 06:55:36,280 it and here it will show us the CSS that 8139 06:55:33,600 --> 06:55:38,958 this button has and it will show us the 8140 06:55:36,280 --> 06:55:41,360 background color that we should use so 8141 06:55:38,958 --> 06:55:44,160 let's make a copy of this color and put 8142 06:55:41,360 --> 06:55:46,000 it into our code 8143 06:55:44,160 --> 06:55:50,320 rightclick 8144 06:55:46,000 --> 06:55:53,280 copy and here we'll remove it and 8145 06:55:50,320 --> 06:55:55,958 rightclick and 8146 06:55:53,280 --> 06:55:59,680 paste now let's 8147 06:55:55,958 --> 06:56:00,760 save and go back to our project and try 8148 06:55:59,680 --> 06:56:03,320 it 8149 06:56:00,760 --> 06:56:07,320 out and now we get get the exact same 8150 06:56:03,320 --> 06:56:11,760 color as the final design so by the way 8151 06:56:07,320 --> 06:56:16,398 this color is known as an RGB value RGB 8152 06:56:11,760 --> 06:56:19,638 means red green and blue and computers 8153 06:56:16,398 --> 06:56:23,440 can create any color using a combination 8154 06:56:19,638 --> 06:56:26,000 of red green and blue so the first 8155 06:56:23,440 --> 06:56:28,558 number here is how much red we want in 8156 06:56:26,000 --> 06:56:31,000 our color the second number is how much 8157 06:56:28,558 --> 06:56:33,120 green we want and the third number is 8158 06:56:31,000 --> 06:56:35,958 how much blue we want 8159 06:56:33,120 --> 06:56:41,080 each number goes from 0 to 8160 06:56:35,958 --> 06:56:44,718 255 Zer means less color and darker 255 8161 06:56:41,080 --> 06:56:47,718 means more color and lighter so this 8162 06:56:44,718 --> 06:56:51,240 color here uses 240 for all of the 8163 06:56:47,718 --> 06:56:54,000 colors so it's close to 255 and will 8164 06:56:51,240 --> 06:56:56,000 give us a very light gray color that you 8165 06:56:54,000 --> 06:56:59,160 see 8166 06:56:56,000 --> 06:57:02,840 here next if we go back to the final 8167 06:56:59,160 --> 06:57:05,120 design and we click our button again 8168 06:57:02,840 --> 06:57:07,240 it will change back to the original 8169 06:57:05,120 --> 06:57:10,798 style so we're going to create this 8170 06:57:07,240 --> 06:57:12,760 feature in our project as well so one 8171 06:57:10,798 --> 06:57:17,040 way to do this is when we click the 8172 06:57:12,760 --> 06:57:19,798 button again we can remove this class so 8173 06:57:17,040 --> 06:57:23,280 let's scroll down to the 8174 06:57:19,798 --> 06:57:25,280 bottom to the Subscribe function and 8175 06:57:23,280 --> 06:57:28,840 when we click the button and we change 8176 06:57:25,280 --> 06:57:32,240 it back to subscribe we can remove this 8177 06:57:28,840 --> 06:57:35,760 class to do that class list has a 8178 06:57:32,240 --> 06:57:38,760 another method we can use called remove 8179 06:57:35,760 --> 06:57:40,200 so let's create a new line and we'll 8180 06:57:38,760 --> 06:57:46,200 type 8181 06:57:40,200 --> 06:57:48,638 button element do class capital L 8182 06:57:46,200 --> 06:57:51,440 list dot 8183 06:57:48,638 --> 06:57:54,600 remove and 8184 06:57:51,440 --> 06:57:58,000 brackets so this does the opposite of 8185 06:57:54,600 --> 06:58:01,040 add it will remove a class from this 8186 06:57:58,000 --> 06:58:03,718 element so inside the brackets we'll 8187 06:58:01,040 --> 06:58:06,920 type a string and the class we want to 8188 06:58:03,718 --> 06:58:09,478 remove which is 8189 06:58:06,920 --> 06:58:12,080 is- 8190 06:58:09,478 --> 06:58:15,478 subscribed and 8191 06:58:12,080 --> 06:58:18,520 save now let's try out our project we'll 8192 06:58:15,478 --> 06:58:21,878 click the button it will get the class 8193 06:58:18,520 --> 06:58:24,798 is subscribed and get the extra Styles 8194 06:58:21,878 --> 06:58:26,958 and now when we click the button again 8195 06:58:24,798 --> 06:58:29,878 it will remove the class and it will 8196 06:58:26,958 --> 06:58:33,200 remove the extra 8197 06:58:29,878 --> 06:58:36,360 Styles so notice here that when we add a 8198 06:58:33,200 --> 06:58:39,320 class we just type the class name we 8199 06:58:36,360 --> 06:58:43,160 don't type a DOT and the class name like 8200 06:58:39,320 --> 06:58:45,600 we did for query selector typing a DOT 8201 06:58:43,160 --> 06:58:48,360 only makes sense in query 8202 06:58:45,600 --> 06:58:52,398 selector next we're going to style the 8203 06:58:48,360 --> 06:58:55,798 text on the page let's scroll up to our 8204 06:58:52,398 --> 06:58:58,920 CSS and one thing we can do is to style 8205 06:58:55,798 --> 06:59:04,080 all the paragraph elements on the page 8206 06:58:58,920 --> 06:59:06,360 using this code P and curly brackets 8207 06:59:04,080 --> 06:59:08,760 however we're going to use a better 8208 06:59:06,360 --> 06:59:11,558 solution instead of styling all the 8209 06:59:08,760 --> 06:59:12,958 paragraphs we're just going to style the 8210 06:59:11,558 --> 06:59:15,520 body 8211 06:59:12,958 --> 06:59:18,398 element when we change the text in the 8212 06:59:15,520 --> 06:59:22,000 body element it will change the text in 8213 06:59:18,398 --> 06:59:25,558 the entire page so inside these curly 8214 06:59:22,000 --> 06:59:27,718 brackets let's change the text here so 8215 06:59:25,558 --> 06:59:31,798 we're going to use a different style of 8216 06:59:27,718 --> 06:59:33,958 text this is called a font to change the 8217 06:59:31,798 --> 06:59:37,600 font font we're going to use this 8218 06:59:33,958 --> 06:59:42,520 property font Das 8219 06:59:37,600 --> 06:59:45,520 family colon and we'll change it to 8220 06:59:42,520 --> 06:59:47,958 aial which is what I used in the final 8221 06:59:45,520 --> 06:59:51,638 design so let's 8222 06:59:47,958 --> 06:59:55,160 save and this will style all the text 8223 06:59:51,638 --> 06:59:58,398 inside the body and change the font to 8224 06:59:55,160 --> 07:00:01,478 aial and a good practice is to write any 8225 06:59:58,398 --> 07:00:04,638 styles for the body at the top so if we 8226 07:00:01,478 --> 07:00:07,680 need to we can override it later so 8227 07:00:04,638 --> 07:00:09,840 let's move these to the top we'll right 8228 07:00:07,680 --> 07:00:12,320 click 8229 07:00:09,840 --> 07:00:15,958 cut and then up 8230 07:00:12,320 --> 07:00:18,440 here create some new lines and right 8231 07:00:15,958 --> 07:00:20,958 click and 8232 07:00:18,440 --> 07:00:24,160 paste and we're going to add one more 8233 07:00:20,958 --> 07:00:26,558 style before moving on let's add some 8234 07:00:24,160 --> 07:00:30,080 space between the Subscribe button and 8235 07:00:26,558 --> 07:00:32,840 the second project so earlier we learned 8236 07:00:30,080 --> 07:00:36,280 that space on the inside of it element 8237 07:00:32,840 --> 07:00:40,280 is called padding space on the outside 8238 07:00:36,280 --> 07:00:43,040 of an element is called margin so inside 8239 07:00:40,280 --> 07:00:46,958 our subscribe button at the 8240 07:00:43,040 --> 07:00:54,958 bottom let's add a property called 8241 07:00:46,958 --> 07:00:58,120 margin Das bottom colon 30 pixels and 8242 07:00:54,958 --> 07:01:00,718 save so this will add space on the 8243 07:00:58,120 --> 07:01:03,120 outside of the element at the bottom of 8244 07:01:00,718 --> 07:01:05,638 30 pixels 8245 07:01:03,120 --> 07:01:07,878 now let's move on and style the Amazon 8246 07:01:05,638 --> 07:01:10,680 shipping calculator with 8247 07:01:07,878 --> 07:01:13,638 CSS again we'll add a class to these 8248 07:01:10,680 --> 07:01:17,600 elements and then style them in our 8249 07:01:13,638 --> 07:01:20,080 CSS let's scroll down to the text box 8250 07:01:17,600 --> 07:01:23,360 which is the input element and we're 8251 07:01:20,080 --> 07:01:29,680 going to add another class by typing 8252 07:01:23,360 --> 07:01:32,080 space and let's use the class cost Das 8253 07:01:29,680 --> 07:01:36,798 input and now we're going to style this 8254 07:01:32,080 --> 07:01:42,080 in CSS so let's scroll up and here we'll 8255 07:01:36,798 --> 07:01:44,360 style the class using dot cost Das 8256 07:01:42,080 --> 07:01:47,878 input and curly 8257 07:01:44,360 --> 07:01:51,080 brackets and inside we'll style this one 8258 07:01:47,878 --> 07:01:54,520 by one let's look at our final design 8259 07:01:51,080 --> 07:01:57,160 and see what styles we need so first 8260 07:01:54,520 --> 07:01:59,600 let's change the text size because our 8261 07:01:57,160 --> 07:02:02,478 text is a little smaller than the text 8262 07:01:59,600 --> 07:02:08,440 here so the change the text size 8263 07:02:02,478 --> 07:02:13,320 we're going to use Font Das size colon 8264 07:02:08,440 --> 07:02:16,840 and let's try 15 pixels and 8265 07:02:13,320 --> 07:02:18,120 save and now our text looks close enough 8266 07:02:16,840 --> 07:02:21,958 to the final 8267 07:02:18,120 --> 07:02:25,600 design next let's add some space inside 8268 07:02:21,958 --> 07:02:28,920 the text box so remember space inside an 8269 07:02:25,600 --> 07:02:30,440 element is called padding so we're going 8270 07:02:28,920 --> 07:02:32,558 to type 8271 07:02:30,440 --> 07:02:35,160 padding Das 8272 07:02:32,558 --> 07:02:41,320 top of 10 8273 07:02:35,160 --> 07:02:45,920 pixels padding Das bottom also 10 8274 07:02:41,320 --> 07:02:47,958 pixels and let's add padding to the 8275 07:02:45,920 --> 07:02:51,920 left 10 8276 07:02:47,958 --> 07:02:57,398 pixels and padding 8277 07:02:51,920 --> 07:03:00,398 dasr of 10 pixels so this will add a 8278 07:02:57,398 --> 07:03:03,520 space on the inside of our text box in 8279 07:03:00,398 --> 07:03:07,240 every direction if we 8280 07:03:03,520 --> 07:03:09,320 save now it looks just like the final 8281 07:03:07,240 --> 07:03:11,760 design now I'm going to show you a 8282 07:03:09,320 --> 07:03:15,840 shortcut for padding that will save us 8283 07:03:11,760 --> 07:03:19,200 some typing so if we create a new line 8284 07:03:15,840 --> 07:03:24,558 we can actually use a property called 8285 07:03:19,200 --> 07:03:27,680 padding and colon and then type 10 8286 07:03:24,558 --> 07:03:31,080 pixels this padding property let us add 8287 07:03:27,680 --> 07:03:34,240 padding on multiple sides at once if we 8288 07:03:31,080 --> 07:03:37,840 type one value here like 10 pixels it 8289 07:03:34,240 --> 07:03:40,680 will add 10 pixels to every side so this 8290 07:03:37,840 --> 07:03:43,600 line does the same thing as these four 8291 07:03:40,680 --> 07:03:46,440 lines above so let's actually comment 8292 07:03:43,600 --> 07:03:51,240 this out using 8293 07:03:46,440 --> 07:03:53,958 slashstar and star slash and 8294 07:03:51,240 --> 07:03:57,160 save and you can see that this one line 8295 07:03:53,958 --> 07:04:00,520 of CSS did the same thing as the other 8296 07:03:57,160 --> 07:04:02,920 four lines of CSS so this shortcut can 8297 07:04:00,520 --> 07:04:05,320 save us some typing 8298 07:04:02,920 --> 07:04:06,520 next let's move on and style the 8299 07:04:05,320 --> 07:04:09,360 calculate 8300 07:04:06,520 --> 07:04:12,638 button again we'll add a class to this 8301 07:04:09,360 --> 07:04:15,878 button so we can style it so in this 8302 07:04:12,638 --> 07:04:20,398 button let's add a class 8303 07:04:15,878 --> 07:04:23,000 attribute class equals 8304 07:04:20,398 --> 07:04:26,760 calculate Das 8305 07:04:23,000 --> 07:04:32,638 button now let's style this in our CSS 8306 07:04:26,760 --> 07:04:35,200 by scrolling up and here we'll type Dot 8307 07:04:32,638 --> 07:04:37,840 calculate dash 8308 07:04:35,200 --> 07:04:40,680 button and curly 8309 07:04:37,840 --> 07:04:43,080 brackets and inside these curly brackets 8310 07:04:40,680 --> 07:04:45,920 we're going to style this button one by 8311 07:04:43,080 --> 07:04:49,240 one so let's start with the background 8312 07:04:45,920 --> 07:04:55,120 color so we'll type 8313 07:04:49,240 --> 07:04:57,520 background Das color colon green and 8314 07:04:55,120 --> 07:05:01,200 semicolon and 8315 07:04:57,520 --> 07:05:02,320 save next let's change the text color to 8316 07:05:01,200 --> 07:05:06,840 white 8317 07:05:02,320 --> 07:05:09,000 so we'll type color colon 8318 07:05:06,840 --> 07:05:12,120 white and save 8319 07:05:09,000 --> 07:05:15,280 again okay let's keep going and we'll 8320 07:05:12,120 --> 07:05:17,638 remove the border around this button so 8321 07:05:15,280 --> 07:05:20,680 we'll type 8322 07:05:17,638 --> 07:05:22,280 border colon 8323 07:05:20,680 --> 07:05:25,200 none 8324 07:05:22,280 --> 07:05:27,360 save next let's make our text a little 8325 07:05:25,200 --> 07:05:32,478 bigger like in the final 8326 07:05:27,360 --> 07:05:35,920 design so we'll type font Das 8327 07:05:32,478 --> 07:05:38,398 size and make it 15 8328 07:05:35,920 --> 07:05:42,360 pixels and 8329 07:05:38,398 --> 07:05:44,798 save okay so now let's add the space on 8330 07:05:42,360 --> 07:05:48,520 the inside of the button so we're going 8331 07:05:44,798 --> 07:05:50,160 to use this shortcut again we'll type 8332 07:05:48,520 --> 07:05:53,360 the padding 8333 07:05:50,160 --> 07:05:56,600 property colon and this time we're going 8334 07:05:53,360 --> 07:05:58,958 to give it two values separated by 8335 07:05:56,600 --> 07:06:03,320 spaces 10 8336 07:05:58,958 --> 07:06:06,520 pixels and maybe 50 pixels so it's 8337 07:06:03,320 --> 07:06:09,680 easier to see what this does so when we 8338 07:06:06,520 --> 07:06:12,638 give two values to padding the first 8339 07:06:09,680 --> 07:06:15,718 value is the vertical padding so the top 8340 07:06:12,638 --> 07:06:19,840 and bottom the second value is the 8341 07:06:15,718 --> 07:06:21,080 horizontal padding or left and right if 8342 07:06:19,840 --> 07:06:23,638 we 8343 07:06:21,080 --> 07:06:27,280 save so you can see that the left and 8344 07:06:23,638 --> 07:06:29,840 right has a lot of padding which is 50 8345 07:06:27,280 --> 07:06:33,398 pixels so now let's make this match our 8346 07:06:29,840 --> 07:06:38,638 final design so instead of 50 pixels 8347 07:06:33,398 --> 07:06:41,798 let's bring it down to 15 pixels and 8348 07:06:38,638 --> 07:06:43,840 save okay so that looks pretty close 8349 07:06:41,798 --> 07:06:46,558 let's also increase the padding on the 8350 07:06:43,840 --> 07:06:48,840 top and bottom so that the text box and 8351 07:06:46,558 --> 07:06:51,000 the button line up with each other like 8352 07:06:48,840 --> 07:06:53,680 in the final 8353 07:06:51,000 --> 07:06:58,718 project so let's change the vertical 8354 07:06:53,680 --> 07:07:01,080 padding to maybe 12 pixels and 8355 07:06:58,718 --> 07:07:03,398 save the last thing we'll style for this 8356 07:07:01,080 --> 07:07:05,878 project is when we put our Mouse over 8357 07:07:03,398 --> 07:07:08,760 the button it turns into the pointer 8358 07:07:05,878 --> 07:07:11,280 icon so to create this we're going to 8359 07:07:08,760 --> 07:07:15,000 use the property 8360 07:07:11,280 --> 07:07:19,440 cursor colon 8361 07:07:15,000 --> 07:07:22,080 pointer and save and give it a 8362 07:07:19,440 --> 07:07:25,160 try and that's it we just finished 8363 07:07:22,080 --> 07:07:28,040 styling the Amazon shipping calculator 8364 07:07:25,160 --> 07:07:32,840 so now let's give this a try we'll Type 8365 07:07:28,040 --> 07:07:35,280 25 in the text box and press calculate 8366 07:07:32,840 --> 07:07:38,120 and at the bottom it will add $10 of 8367 07:07:35,280 --> 07:07:41,000 shipping and show 8368 07:07:38,120 --> 07:07:43,558 $35 finally we're going to style the 8369 07:07:41,000 --> 07:07:46,160 last project in this lesson which is the 8370 07:07:43,558 --> 07:07:49,520 rock paper scissors project and this 8371 07:07:46,160 --> 07:07:52,558 will finish the project let's go back to 8372 07:07:49,520 --> 07:07:55,878 superimple dodev and we'll scroll down 8373 07:07:52,558 --> 07:07:58,680 on this page and then click this link to 8374 07:07:55,878 --> 07:08:01,638 show the final design for rock paper 8375 07:07:58,680 --> 07:08:03,080 scissors so I'll rearrange my windows so 8376 07:08:01,638 --> 07:08:05,320 we can see 8377 07:08:03,080 --> 07:08:08,360 this and we're going to change the 8378 07:08:05,320 --> 07:08:11,878 buttons to these nice big icons for each 8379 07:08:08,360 --> 07:08:15,320 of the moves and when we click a move it 8380 07:08:11,878 --> 07:08:17,958 will show the result in bold and show an 8381 07:08:15,320 --> 07:08:20,320 icon of the move that we picked and the 8382 07:08:17,958 --> 07:08:22,958 move that the computer picked so we're 8383 07:08:20,320 --> 07:08:25,760 going to make this project look really 8384 07:08:22,958 --> 07:08:28,638 nice now let's get started I'm going to 8385 07:08:25,760 --> 07:08:31,440 rearrange my windows back so we have it 8386 07:08:28,638 --> 07:08:33,840 at the bottom and let's create a cop 8387 07:08:31,440 --> 07:08:37,040 copy of the rock paper scissors project 8388 07:08:33,840 --> 07:08:40,478 for this lesson we'll go to our code 8389 07:08:37,040 --> 07:08:44,160 editor and click here to show our 8390 07:08:40,478 --> 07:08:47,798 files and we're going to make a copy of 8391 07:08:44,160 --> 07:08:50,280 09- Ro paper scissors so let's click 8392 07:08:47,798 --> 07:08:52,000 this file and then 8393 07:08:50,280 --> 07:08:57,160 rightclick 8394 07:08:52,000 --> 07:09:00,240 copy and down here right click and 8395 07:08:57,160 --> 07:09:03,000 paste and we'll rename this file by 8396 07:09:00,240 --> 07:09:07,360 right-clicking 8397 07:09:03,000 --> 07:09:09,600 rename and at the end we'll remove copy 8398 07:09:07,360 --> 07:09:14,878 and at the beginning we'll change it 8399 07:09:09,600 --> 07:09:18,120 from 9 to 10 and press 8400 07:09:14,878 --> 07:09:21,798 enter now let's click here to hide the 8401 07:09:18,120 --> 07:09:25,000 files and we can close the 09 8402 07:09:21,798 --> 07:09:28,320 file and let's open the 10 rock paper 8403 07:09:25,000 --> 07:09:33,320 scissors file in the browser so we'll 8404 07:09:28,320 --> 07:09:33,320 right click and then open with live 8405 07:09:33,638 --> 07:09:41,360 server and I'll move this tab to the top 8406 07:09:37,080 --> 07:09:44,718 here so we can see it side by side so 8407 07:09:41,360 --> 07:09:46,478 let's start by styling the body we want 8408 07:09:44,718 --> 07:09:50,478 to make the background color of the 8409 07:09:46,478 --> 07:09:53,200 entire page this dark gray color we'll 8410 07:09:50,478 --> 07:09:56,440 go into our code and let's create a 8411 07:09:53,200 --> 07:10:00,798 style element so we can write some 8412 07:09:56,440 --> 07:10:01,920 CSS inside the head let's type less than 8413 07:10:00,798 --> 07:10:05,040 Style 8414 07:10:01,920 --> 07:10:08,600 greater than and then inside here we're 8415 07:10:05,040 --> 07:10:11,160 going to style the body so we'll type 8416 07:10:08,600 --> 07:10:13,958 body and curly 8417 07:10:11,160 --> 07:10:19,280 brackets now inside here we're going to 8418 07:10:13,958 --> 07:10:19,280 change the background color so 8419 07:10:19,760 --> 07:10:25,360 background-color colon and we're going 8420 07:10:22,520 --> 07:10:28,840 to get the color that we see here by 8421 07:10:25,360 --> 07:10:32,040 right clicking click 8422 07:10:28,840 --> 07:10:36,240 inspect and then go to the elements tab 8423 07:10:32,040 --> 07:10:39,840 Tab and I'll rearrange it so we can see 8424 07:10:36,240 --> 07:10:43,638 it and then here we'll find the body 8425 07:10:39,840 --> 07:10:46,638 element and click it and in this style 8426 07:10:43,638 --> 07:10:49,718 section we can find the background color 8427 07:10:46,638 --> 07:10:51,600 that we need so let's select 8428 07:10:49,718 --> 07:10:53,398 this and 8429 07:10:51,600 --> 07:10:55,440 rightclick 8430 07:10:53,398 --> 07:10:59,558 copy and in 8431 07:10:55,440 --> 07:11:04,638 here right click and 8432 07:10:59,558 --> 07:11:08,520 paste and a semi colon at the end let's 8433 07:11:04,638 --> 07:11:11,280 save and I'll move this back to the 8434 07:11:08,520 --> 07:11:14,520 corner and now our project has the same 8435 07:11:11,280 --> 07:11:18,320 background color as the final design 8436 07:11:14,520 --> 07:11:23,280 next let's make the text color white so 8437 07:11:18,320 --> 07:11:29,558 in our CSS we'll use the color property 8438 07:11:23,280 --> 07:11:32,280 and colon white and semicolon and save 8439 07:11:29,558 --> 07:11:35,240 and that will make our text White 8440 07:11:32,280 --> 07:11:40,520 next let's change the style or the font 8441 07:11:35,240 --> 07:11:43,398 of this text we'll type font 8442 07:11:40,520 --> 07:11:47,080 dasam colon 8443 07:11:43,398 --> 07:11:48,638 aial which is what the final design uses 8444 07:11:47,080 --> 07:11:51,638 and 8445 07:11:48,638 --> 07:11:54,558 save all right now let's style these 8446 07:11:51,638 --> 07:11:57,558 elements one by one to match the final 8447 07:11:54,558 --> 07:12:00,638 design we'll start with this paragraph 8448 07:11:57,558 --> 07:12:01,718 at the top so inside the paragraph 8449 07:12:00,638 --> 07:12:05,200 element 8450 07:12:01,718 --> 07:12:08,840 let's add a class so we can style it in 8451 07:12:05,200 --> 07:12:11,920 CSS we'll add class 8452 07:12:08,840 --> 07:12:15,520 equals let's call this the 8453 07:12:11,920 --> 07:12:18,680 title and then in our CSS we can style 8454 07:12:15,520 --> 07:12:19,840 this class by starting with a DOT and 8455 07:12:18,680 --> 07:12:22,638 then 8456 07:12:19,840 --> 07:12:25,840 title and curly 8457 07:12:22,638 --> 07:12:29,200 brackets so let's make our title really 8458 07:12:25,840 --> 07:12:37,080 big like the final design we'll change 8459 07:12:29,200 --> 07:12:40,680 the font Das size colon 30 pixels and 8460 07:12:37,080 --> 07:12:44,558 save so that looks close enough next 8461 07:12:40,680 --> 07:12:52,958 let's make the text bold in our CSS 8462 07:12:44,558 --> 07:12:56,240 we'll use Font D weight colon bold and 8463 07:12:52,958 --> 07:12:58,878 save now let's move on to installing the 8464 07:12:56,240 --> 07:13:01,280 buttons so first we're going to need 8465 07:12:58,878 --> 07:13:04,000 these images in our project and I'll 8466 07:13:01,280 --> 07:13:07,320 show you how to get them we'll put our 8467 07:13:04,000 --> 07:13:10,680 Mouse over the rock image and we'll 8468 07:13:07,320 --> 07:13:13,080 rightclick and we'll click save 8469 07:13:10,680 --> 07:13:16,280 image and now we're going to find the 8470 07:13:13,080 --> 07:13:18,160 folder that contains all of our code so 8471 07:13:16,280 --> 07:13:21,440 for me it's on the 8472 07:13:18,160 --> 07:13:23,958 desktop and it's this folder right here 8473 07:13:21,440 --> 07:13:26,520 so I'll double click this folder and 8474 07:13:23,958 --> 07:13:31,320 then leave the file name as the default 8475 07:13:26,520 --> 07:13:34,040 rock- emoji and click save 8476 07:13:31,320 --> 07:13:37,798 and now in our code editor if we click 8477 07:13:34,040 --> 07:13:40,680 here we should be able to see the rock 8478 07:13:37,798 --> 07:13:43,320 image let's do the same with the paper 8479 07:13:40,680 --> 07:13:47,240 and scissors image so we'll hover over 8480 07:13:43,320 --> 07:13:49,718 it rightclick save 8481 07:13:47,240 --> 07:13:52,320 image and in the same folder that 8482 07:13:49,718 --> 07:13:56,478 contains our code we're going to save 8483 07:13:52,320 --> 07:13:59,958 the paper image and finally rightclick 8484 07:13:56,478 --> 07:14:02,200 save image and we'll save the scissors 8485 07:13:59,958 --> 07:14:04,398 image 8486 07:14:02,200 --> 07:14:06,840 all right now that we have these images 8487 07:14:04,398 --> 07:14:10,000 in our code editor let's learn how to 8488 07:14:06,840 --> 07:14:13,680 load them onto the page so we can do 8489 07:14:10,000 --> 07:14:17,798 this using an HTML element so we'll go 8490 07:14:13,680 --> 07:14:21,000 back to our HTML code and we'll scroll 8491 07:14:17,798 --> 07:14:25,280 down and inside the button we're going 8492 07:14:21,000 --> 07:14:30,240 to add an image element so let's type 8493 07:14:25,280 --> 07:14:33,760 less than IMG and greater than so this 8494 07:14:30,240 --> 07:14:36,878 is the image HTML element it loads an 8495 07:14:33,760 --> 07:14:39,760 image onto the page the image element 8496 07:14:36,878 --> 07:14:42,878 doesn't require a closing tag this is 8497 07:14:39,760 --> 07:14:45,600 called a void element now to load an 8498 07:14:42,878 --> 07:14:49,360 image we're going to add an attribute to 8499 07:14:45,600 --> 07:14:53,840 this element called 8500 07:14:49,360 --> 07:14:57,958 SRC so SRC stands for Source it tells a 8501 07:14:53,840 --> 07:14:59,520 computer which file or image to load so 8502 07:14:57,958 --> 07:15:01,958 let's look in our 8503 07:14:59,520 --> 07:15:05,760 project for this button we're going to 8504 07:15:01,958 --> 07:15:10,440 load rock- emoji.png 8505 07:15:05,760 --> 07:15:12,718 inside the source attribute we'll type 8506 07:15:10,440 --> 07:15:14,558 rock- 8507 07:15:12,718 --> 07:15:17,558 emoji.png 8508 07:15:14,558 --> 07:15:21,120 so what this does is it will look for 8509 07:15:17,558 --> 07:15:24,000 the file with this name beside the HTML 8510 07:15:21,120 --> 07:15:28,040 file and it finds it right here and it 8511 07:15:24,000 --> 07:15:31,680 will load it onto the page if we 8512 07:15:28,040 --> 07:15:32,878 save now inside our button it loaded the 8513 07:15:31,680 --> 07:15:36,520 rock 8514 07:15:32,878 --> 07:15:39,398 image right now this image is too big so 8515 07:15:36,520 --> 07:15:43,280 let's use CSS to decrease the size of 8516 07:15:39,398 --> 07:15:45,280 this image we'll hide the files and 8517 07:15:43,280 --> 07:15:48,280 let's give a class attribute to the 8518 07:15:45,280 --> 07:15:52,638 image element so we can style it so 8519 07:15:48,280 --> 07:15:56,280 we'll type class equals and let's call 8520 07:15:52,638 --> 07:15:59,000 this a move Das 8521 07:15:56,280 --> 07:16:01,638 icon and I'm also just going to add some 8522 07:15:59,000 --> 07:16:06,200 new lines to make the code a little 8523 07:16:01,638 --> 07:16:09,680 easier to read so we'll scroll up to the 8524 07:16:06,200 --> 07:16:16,240 CSS and we're going to style this class 8525 07:16:09,680 --> 07:16:19,320 using dot move Dash icon and curly 8526 07:16:16,240 --> 07:16:23,360 brackets and let's decrease the height 8527 07:16:19,320 --> 07:16:29,958 of this image we can use the property 8528 07:16:23,360 --> 07:16:32,120 height colon and a pixel value 50 pixels 8529 07:16:29,958 --> 07:16:34,398 and Save 8530 07:16:32,120 --> 07:16:38,160 so this will make the height of this 8531 07:16:34,398 --> 07:16:40,320 image 50 pixels and it will also resize 8532 07:16:38,160 --> 07:16:43,878 the width automatically so that the 8533 07:16:40,320 --> 07:16:46,320 image keeps its shape so this size looks 8534 07:16:43,878 --> 07:16:47,440 pretty close to the final design so we 8535 07:16:46,320 --> 07:16:50,320 can move 8536 07:16:47,440 --> 07:16:53,040 on another feature of the source 8537 07:16:50,320 --> 07:16:56,840 attribute is that we can put our files 8538 07:16:53,040 --> 07:16:58,200 into folders so let's open the files by 8539 07:16:56,840 --> 07:17:00,440 clicking 8540 07:16:58,200 --> 07:17:04,080 here and let's say that we want to 8541 07:17:00,440 --> 07:17:06,200 organize all of our images into a folder 8542 07:17:04,080 --> 07:17:07,878 so we're going to create a folder by 8543 07:17:06,200 --> 07:17:13,120 clicking this 8544 07:17:07,878 --> 07:17:13,120 icon and let's name this folder 8545 07:17:13,760 --> 07:17:18,398 images and we're going to drag these 8546 07:17:15,920 --> 07:17:21,478 images into this 8547 07:17:18,398 --> 07:17:25,040 folder and drag this in 8548 07:17:21,478 --> 07:17:27,878 here and in 8549 07:17:25,040 --> 07:17:30,638 here now unfortunately you can see that 8550 07:17:27,878 --> 07:17:33,718 the image doesn't load anymore and 8551 07:17:30,638 --> 07:17:36,360 that's because if we look at our code 8552 07:17:33,718 --> 07:17:40,478 Source tells a computer to look for this 8553 07:17:36,360 --> 07:17:43,558 file beside this HTML 8554 07:17:40,478 --> 07:17:47,638 file and right now there is no file 8555 07:17:43,558 --> 07:17:50,440 called rock- emoji.png because we put it 8556 07:17:47,638 --> 07:17:52,878 in a folder so to tell the computer to 8557 07:17:50,440 --> 07:17:55,798 look inside this folder we're going to 8558 07:17:52,878 --> 07:17:57,280 type the folder name at the start which 8559 07:17:55,798 --> 07:17:59,680 is 8560 07:17:57,280 --> 07:18:03,478 images and then we're going to type a 8561 07:17:59,680 --> 07:18:06,638 slash to go into the images folder and 8562 07:18:03,478 --> 07:18:08,840 then look for this file which is right 8563 07:18:06,638 --> 07:18:12,040 here if we 8564 07:18:08,840 --> 07:18:15,520 save the computer is now able to find 8565 07:18:12,040 --> 07:18:18,798 this image and load it onto the web page 8566 07:18:15,520 --> 07:18:20,520 so this feature is called file paaths we 8567 07:18:18,798 --> 07:18:24,600 can tell the computer to look for a 8568 07:18:20,520 --> 07:18:27,360 folder beside the HTML file like images 8569 07:18:24,600 --> 07:18:32,520 and then use a slash to tell it to go 8570 07:18:27,360 --> 07:18:32,520 into the folder and look for a file 8571 07:18:32,558 --> 07:18:38,280 now let's use this code to load the 8572 07:18:34,718 --> 07:18:40,600 other images onto our page so for the 8573 07:18:38,280 --> 07:18:45,080 paper image we're going to go to this 8574 07:18:40,600 --> 07:18:49,600 button remove the text and then create 8575 07:18:45,080 --> 07:18:52,798 an image element less than IMG greater 8576 07:18:49,600 --> 07:18:56,958 than and we'll give it an attribute 8577 07:18:52,798 --> 07:19:01,478 Source equals and we'll locate the image 8578 07:18:56,958 --> 07:19:04,958 it is in the images folder so we'll type 8579 07:19:01,478 --> 07:19:08,440 images and then slash to go into the 8580 07:19:04,958 --> 07:19:10,280 folder and we'll load the file paper- 8581 07:19:08,440 --> 07:19:12,080 emoji.png 8582 07:19:10,280 --> 07:19:14,320 so 8583 07:19:12,080 --> 07:19:16,160 paper- 8584 07:19:14,320 --> 07:19:18,558 emoji.png 8585 07:19:16,160 --> 07:19:22,120 and 8586 07:19:18,558 --> 07:19:24,320 save so our image is really big let's 8587 07:19:22,120 --> 07:19:30,080 give it the same class that we gave the 8588 07:19:24,320 --> 07:19:35,080 rock image so we'll add a class equals 8589 07:19:30,080 --> 07:19:38,200 move Dash icon and 8590 07:19:35,080 --> 07:19:42,520 save so these Styles get applied to all 8591 07:19:38,200 --> 07:19:45,160 elements with the class move- icon so it 8592 07:19:42,520 --> 07:19:47,520 applies to both of these 8593 07:19:45,160 --> 07:19:50,878 elements now let's do the same thing for 8594 07:19:47,520 --> 07:19:55,240 the scissors button we'll remove the 8595 07:19:50,878 --> 07:19:59,040 text and we'll replace it with an image 8596 07:19:55,240 --> 07:20:02,040 element and we'll give it the source 8597 07:19:59,040 --> 07:20:03,638 attribute images 8598 07:20:02,040 --> 07:20:06,600 slash 8599 07:20:03,638 --> 07:20:08,120 scissors Das 8600 07:20:06,600 --> 07:20:12,160 emoji.png 8601 07:20:08,120 --> 07:20:14,440 and we'll also give this a 8602 07:20:12,160 --> 07:20:18,760 class of 8603 07:20:14,440 --> 07:20:20,680 move- icon to resize the images like up 8604 07:20:18,760 --> 07:20:23,760 here let's 8605 07:20:20,680 --> 07:20:25,440 save and now we have all the images that 8606 07:20:23,760 --> 07:20:28,240 we 8607 07:20:25,440 --> 07:20:31,680 need now let's make these buttons look 8608 07:20:28,240 --> 07:20:34,798 like the final design we we scroll 8609 07:20:31,680 --> 07:20:37,680 up and let's add a class to these 8610 07:20:34,798 --> 07:20:42,958 buttons so we can style them with 8611 07:20:37,680 --> 07:20:44,440 CSS so we'll add class equals and let's 8612 07:20:42,958 --> 07:20:46,280 use 8613 07:20:44,440 --> 07:20:49,160 move- 8614 07:20:46,280 --> 07:20:53,600 button and for this button We'll add the 8615 07:20:49,160 --> 07:20:55,160 same class class equals 8616 07:20:53,600 --> 07:20:58,398 move- 8617 07:20:55,160 --> 07:21:03,440 Button as well as for the third 8618 07:20:58,398 --> 07:21:07,160 button class = move- 8619 07:21:03,440 --> 07:21:10,680 button now let's scroll up to our CSS 8620 07:21:07,160 --> 07:21:16,240 and we're going to style this class 8621 07:21:10,680 --> 07:21:19,080 using move- button and curly 8622 07:21:16,240 --> 07:21:21,398 brackets so first let's change the 8623 07:21:19,080 --> 07:21:25,478 background color to match the background 8624 07:21:21,398 --> 07:21:30,040 color of the page so we'll use 8625 07:21:25,478 --> 07:21:33,440 background Das color colon and we'll use 8626 07:21:30,040 --> 07:21:35,280 a special value called 8627 07:21:33,440 --> 07:21:38,878 transparent and this will make the 8628 07:21:35,280 --> 07:21:41,798 button see- through if we 8629 07:21:38,878 --> 07:21:44,760 save now these buttons are 8630 07:21:41,798 --> 07:21:48,160 see-through next let's add this white 8631 07:21:44,760 --> 07:21:49,200 border to our buttons so we'll use the 8632 07:21:48,160 --> 07:21:52,080 Border 8633 07:21:49,200 --> 07:21:55,520 property colon and we're going to give 8634 07:21:52,080 --> 07:21:59,360 it three values the first value is our 8635 07:21:55,520 --> 07:22:03,160 border width or how thick the border is 8636 07:21:59,360 --> 07:22:07,280 let's use one pixel for now the second 8637 07:22:03,160 --> 07:22:11,360 value is the Border style and we'll use 8638 07:22:07,280 --> 07:22:14,920 solid because it's a solid border other 8639 07:22:11,360 --> 07:22:18,200 values we can use here are dotted or 8640 07:22:14,920 --> 07:22:21,240 dashed and the third value is the Border 8641 07:22:18,200 --> 07:22:26,120 color so we're going to use 8642 07:22:21,240 --> 07:22:29,680 white and semicolon at the end and 8643 07:22:26,120 --> 07:22:31,600 save and that will add a border so let's 8644 07:22:29,680 --> 07:22:34,520 make our border a little thicker like 8645 07:22:31,600 --> 07:22:37,798 the final design we'll change this first 8646 07:22:34,520 --> 07:22:40,040 value which is the Border width to three 8647 07:22:37,798 --> 07:22:43,440 pixels and 8648 07:22:40,040 --> 07:22:46,558 save okay so that looks pretty close now 8649 07:22:43,440 --> 07:22:49,398 let's change the size of our buttons 8650 07:22:46,558 --> 07:22:52,120 usually we use the padding to add space 8651 07:22:49,398 --> 07:22:55,360 on the inside of an element but in this 8652 07:22:52,120 --> 07:22:57,718 case our images have different sizes so 8653 07:22:55,360 --> 07:23:02,440 we're going to use width and height to 8654 07:22:57,718 --> 07:23:04,120 set an exact size so so in our CSS we'll 8655 07:23:02,440 --> 07:23:08,440 type 8656 07:23:04,120 --> 07:23:11,120 WID colon and let's make it 100 8657 07:23:08,440 --> 07:23:16,840 pixels and 8658 07:23:11,120 --> 07:23:19,680 height and also 100 pixels and 8659 07:23:16,840 --> 07:23:21,878 save so it looks like our final design 8660 07:23:19,680 --> 07:23:24,840 is a little bigger than what we have 8661 07:23:21,878 --> 07:23:30,878 here so let's increase the width and the 8662 07:23:24,840 --> 07:23:33,878 height so let's try 120 pixels and Save 8663 07:23:30,878 --> 07:23:36,440 all right so that looks pretty close now 8664 07:23:33,878 --> 07:23:41,600 let's make these buttons round and the 8665 07:23:36,440 --> 07:23:43,280 property for that is called border Das 8666 07:23:41,600 --> 07:23:45,920 radius 8667 07:23:43,280 --> 07:23:49,000 colon and to know how much we need to 8668 07:23:45,920 --> 07:23:52,080 make it round we can set it to half of 8669 07:23:49,000 --> 07:23:54,160 the width or the height so we'll set it 8670 07:23:52,080 --> 07:23:56,398 to 60 8671 07:23:54,160 --> 07:23:58,958 pixels and 8672 07:23:56,398 --> 07:24:01,798 save okay so it's coming along really 8673 07:23:58,958 --> 07:24:05,120 well and lastly we're going to add some 8674 07:24:01,798 --> 07:24:07,558 space between the buttons so remember 8675 07:24:05,120 --> 07:24:11,160 that space outside of an element is 8676 07:24:07,558 --> 07:24:13,760 called margin so let's add some margin 8677 07:24:11,160 --> 07:24:15,360 on the right so we'll 8678 07:24:13,760 --> 07:24:17,360 type 8679 07:24:15,360 --> 07:24:24,240 margin 8680 07:24:17,360 --> 07:24:27,520 dasr colon and maybe 10 pixels and 8681 07:24:24,240 --> 07:24:30,958 save and finally when we put our Mouse 8682 07:24:27,520 --> 07:24:37,798 over the button it turns into a pointer 8683 07:24:30,958 --> 07:24:42,000 so the property for that is cursor colon 8684 07:24:37,798 --> 07:24:44,840 pointer and save and now our buttons 8685 07:24:42,000 --> 07:24:47,680 match the final design so let's keep 8686 07:24:44,840 --> 07:24:50,240 moving and style the next part of the 8687 07:24:47,680 --> 07:24:53,120 project which is the result and the 8688 07:24:50,240 --> 07:24:55,600 moves if you don't see this here you can 8689 07:24:53,120 --> 07:24:56,478 just play the game and it'll give us a 8690 07:24:55,600 --> 07:24:59,440 new 8691 07:24:56,478 --> 07:25:01,878 result so to make this easier to style 8692 07:24:59,440 --> 07:25:04,680 we're going to go into our code we're 8693 07:25:01,878 --> 07:25:07,160 going to find the result which is here 8694 07:25:04,680 --> 07:25:10,160 and we're going to add some fake results 8695 07:25:07,160 --> 07:25:13,920 like you 8696 07:25:10,160 --> 07:25:16,718 win let's save and now we have this 8697 07:25:13,920 --> 07:25:20,398 result and we're going to style it so 8698 07:25:16,718 --> 07:25:23,080 let's give this a class of 8699 07:25:20,398 --> 07:25:27,360 result and then in our 8700 07:25:23,080 --> 07:25:31,120 CSS we're going to style the class dot 8701 07:25:27,360 --> 07:25:33,558 result and curly brackets 8702 07:25:31,120 --> 07:25:38,638 so let's make the text bigger we're 8703 07:25:33,558 --> 07:25:42,478 going to use Font Das size colon and 8704 07:25:38,638 --> 07:25:45,958 maybe 25 pixels and 8705 07:25:42,478 --> 07:25:50,878 save okay that looks pretty good let's 8706 07:25:45,958 --> 07:25:53,600 make the text bold so we use Font Das 8707 07:25:50,878 --> 07:25:55,760 weight colon 8708 07:25:53,600 --> 07:25:58,600 bold and 8709 07:25:55,760 --> 07:26:02,680 save and finally we're going to add 8710 07:25:58,600 --> 07:26:07,478 extra spaces between this and the 8711 07:26:02,680 --> 07:26:07,478 buttons so we'll add 8712 07:26:07,638 --> 07:26:16,200 margin-top and maybe 50 pixels and 8713 07:26:13,040 --> 07:26:19,958 save okay so I think that looks close 8714 07:26:16,200 --> 07:26:21,600 enough now let's style the moves and 8715 07:26:19,958 --> 07:26:24,280 again we'll scroll 8716 07:26:21,600 --> 07:26:27,680 down and we're going to add some fake 8717 07:26:24,280 --> 07:26:30,600 moves in here so it's easier to style so 8718 07:26:27,680 --> 07:26:34,878 we'll Type U 8719 07:26:30,600 --> 07:26:38,958 and then an image so let's use the less 8720 07:26:34,878 --> 07:26:41,958 than image and the source 8721 07:26:38,958 --> 07:26:44,318 attribute and let's just load the rock 8722 07:26:41,958 --> 07:26:47,520 image and the scissors image for the 8723 07:26:44,318 --> 07:26:50,760 computer so to load the rock image we'll 8724 07:26:47,520 --> 07:26:54,718 look inside the images folder slash to 8725 07:26:50,760 --> 07:26:56,520 go into the folder and then rock- 8726 07:26:54,718 --> 07:26:58,360 emoji.png 8727 07:26:56,520 --> 07:27:01,680 so 8728 07:26:58,360 --> 07:27:05,040 rock- emoji 8729 07:27:01,680 --> 07:27:10,600 PNG and greater than and we'll add 8730 07:27:05,040 --> 07:27:15,280 another image less than image source 8731 07:27:10,600 --> 07:27:19,600 equals Images slash the scissors 8732 07:27:15,280 --> 07:27:21,240 Emoji scissors Das 8733 07:27:19,600 --> 07:27:26,280 emoji.png 8734 07:27:21,240 --> 07:27:27,920 and greater than and finally the text 8735 07:27:26,280 --> 07:27:31,000 computer 8736 07:27:27,920 --> 07:27:33,600 computer let's save 8737 07:27:31,000 --> 07:27:36,318 and this will create some results 8738 07:27:33,600 --> 07:27:39,840 although the images are really big so we 8739 07:27:36,318 --> 07:27:43,280 can use the class move icon to resize 8740 07:27:39,840 --> 07:27:46,200 them to be the same size as the buttons 8741 07:27:43,280 --> 07:27:47,000 so for both of these images let's add 8742 07:27:46,200 --> 07:27:48,558 the 8743 07:27:47,000 --> 07:27:50,520 class 8744 07:27:48,558 --> 07:27:55,520 move- 8745 07:27:50,520 --> 07:28:00,360 icon and here as well class equals 8746 07:27:55,520 --> 07:28:02,478 move- icon and Save 8747 07:28:00,360 --> 07:28:06,680 and now our images look 8748 07:28:02,478 --> 07:28:09,040 good next let's style this score section 8749 07:28:06,680 --> 07:28:12,718 so the only thing we need here is to add 8750 07:28:09,040 --> 07:28:18,000 spaces between these two so in this 8751 07:28:12,718 --> 07:28:22,240 score element let's add a class of 8752 07:28:18,000 --> 07:28:26,840 score and we'll scroll up and style this 8753 07:28:22,240 --> 07:28:29,680 element using DOT score 8754 07:28:26,840 --> 07:28:31,200 brackets and let's add some margin at 8755 07:28:29,680 --> 07:28:33,760 the top 8756 07:28:31,200 --> 07:28:38,878 so 8757 07:28:33,760 --> 07:28:41,878 margin-top colon 50 pixels and 8758 07:28:38,878 --> 07:28:43,958 save and let's compare it and it looks 8759 07:28:41,878 --> 07:28:47,398 like our final design has a little more 8760 07:28:43,958 --> 07:28:51,120 spacing than this so let's change this 8761 07:28:47,398 --> 07:28:54,718 to 60 pixels and 8762 07:28:51,120 --> 07:28:57,680 save and that looks close enough and 8763 07:28:54,718 --> 07:29:00,920 finally we'll style the reset score 8764 07:28:57,680 --> 07:29:05,680 button so let's go back to our HTM 8765 07:29:00,920 --> 07:29:10,920 ml we'll add a class to this button so 8766 07:29:05,680 --> 07:29:14,398 here class equals reset 8767 07:29:10,920 --> 07:29:19,440 dcore Das 8768 07:29:14,398 --> 07:29:21,160 button so at the top we'll type dot 8769 07:29:19,440 --> 07:29:23,440 reset 8770 07:29:21,160 --> 07:29:25,398 dcore 8771 07:29:23,440 --> 07:29:27,760 Dutton curly 8772 07:29:25,398 --> 07:29:32,958 brackets and we'll make the background 8773 07:29:27,760 --> 07:29:34,718 color white so background dasc color 8774 07:29:32,958 --> 07:29:37,600 colon 8775 07:29:34,718 --> 07:29:42,920 white and let's also remove the border 8776 07:29:37,600 --> 07:29:46,920 from our button using border colon 8777 07:29:42,920 --> 07:29:49,680 none let's save and see how it 8778 07:29:46,920 --> 07:29:52,160 looks and then let's increase the size 8779 07:29:49,680 --> 07:29:56,398 of this text because the final design 8780 07:29:52,160 --> 07:30:02,360 looks a little bigger so we'll add font 8781 07:29:56,398 --> 07:30:05,520 Das size colon 15 pixels and 8782 07:30:02,360 --> 07:30:08,520 save now let's add some extra space on 8783 07:30:05,520 --> 07:30:13,120 the inside of the button so that's 8784 07:30:08,520 --> 07:30:15,958 padding padding colon and we'll give it 8785 07:30:13,120 --> 07:30:19,600 two values the first value is the 8786 07:30:15,958 --> 07:30:22,478 vertical padding let's use 10 8787 07:30:19,600 --> 07:30:27,000 pixels and the second value is the 8788 07:30:22,478 --> 07:30:31,240 horizontal padding so let's use 15 8789 07:30:27,000 --> 07:30:33,318 pixels and Save 8790 07:30:31,240 --> 07:30:36,360 and it looks like our final project is a 8791 07:30:33,318 --> 07:30:40,280 little shorter than our button so let's 8792 07:30:36,360 --> 07:30:44,558 change the vertical padding to maybe 8793 07:30:40,280 --> 07:30:50,200 eight and save and the last step is to 8794 07:30:44,558 --> 07:30:51,798 add the cursor pointer so here cursor 8795 07:30:50,200 --> 07:30:54,120 colon 8796 07:30:51,798 --> 07:30:56,680 pointer and 8797 07:30:54,120 --> 07:30:58,920 save and that's pretty much it we 8798 07:30:56,680 --> 07:31:01,760 finished styling our rock paper scissors 8799 07:30:58,920 --> 07:31:04,840 project so now I'm going to rearrange my 8800 07:31:01,760 --> 07:31:07,680 windows so we can see the entire design 8801 07:31:04,840 --> 07:31:10,840 and it looks really nice the last step 8802 07:31:07,680 --> 07:31:13,840 here is to remove these fake results and 8803 07:31:10,840 --> 07:31:17,520 use JavaScript to insert these results 8804 07:31:13,840 --> 07:31:17,520 instead so let's scroll 8805 07:31:17,558 --> 07:31:25,240 down and we'll remove this 8806 07:31:22,000 --> 07:31:26,958 result and let's actually copy this code 8807 07:31:25,240 --> 07:31:31,200 because we want our JavaScript to 8808 07:31:26,958 --> 07:31:32,680 generate this code so we'll right click 8809 07:31:31,200 --> 07:31:35,360 and 8810 07:31:32,680 --> 07:31:38,200 copy and let's scroll to the 8811 07:31:35,360 --> 07:31:41,840 bottom to where we insert the moves onto 8812 07:31:38,200 --> 07:31:45,920 the page which is here and instead of 8813 07:31:41,840 --> 07:31:49,638 this text we're going to rightclick and 8814 07:31:45,920 --> 07:31:51,840 paste the code that we had before and 8815 07:31:49,638 --> 07:31:54,318 this time instead of picking Rock every 8816 07:31:51,840 --> 07:31:58,318 time we're going to substitute this with 8817 07:31:54,318 --> 07:32:00,080 the player move so we're remove this and 8818 07:31:58,318 --> 07:32:03,040 because this is already a template 8819 07:32:00,080 --> 07:32:07,318 string we can insert a value directly 8820 07:32:03,040 --> 07:32:09,398 inside using dollar and curly brackets 8821 07:32:07,318 --> 07:32:11,160 and we'll insert the 8822 07:32:09,398 --> 07:32:14,040 player 8823 07:32:11,160 --> 07:32:17,760 move let's also insert the computer's 8824 07:32:14,040 --> 07:32:20,520 move in here so we'll remove this and 8825 07:32:17,760 --> 07:32:25,638 insert a value using dollar and curly 8826 07:32:20,520 --> 07:32:28,080 brackets and we'll insert computer 8827 07:32:25,638 --> 07:32:31,318 move and now that we're inserting this 8828 07:32:28,080 --> 07:32:33,080 with JavaScript we can scroll up and 8829 07:32:31,318 --> 07:32:37,200 remove the fake 8830 07:32:33,080 --> 07:32:38,680 result which is here so we can select it 8831 07:32:37,200 --> 07:32:41,240 and delete 8832 07:32:38,680 --> 07:32:43,280 it now let's 8833 07:32:41,240 --> 07:32:45,878 save and we're not going to have 8834 07:32:43,280 --> 07:32:49,318 anything in our results and when we 8835 07:32:45,878 --> 07:32:51,878 click a move it will show us the result 8836 07:32:49,318 --> 07:32:54,680 and also show us the moves that we 8837 07:32:51,878 --> 07:32:56,680 picked and that's it we finished the 8838 07:32:54,680 --> 07:32:58,520 rock paper scissors 8839 07:32:56,680 --> 07:33:00,878 project the last thing we're going to 8840 07:32:58,520 --> 07:33:03,160 learn in this lesson is how to organize 8841 07:33:00,878 --> 07:33:06,318 our code into different 8842 07:33:03,160 --> 07:33:10,440 files so notice that right now all of 8843 07:33:06,318 --> 07:33:13,638 our HTML CSS and JavaScript is in one 8844 07:33:10,440 --> 07:33:16,718 HTML file and this makes the code harder 8845 07:33:13,638 --> 07:33:19,920 to read because the file is bigger and 8846 07:33:16,718 --> 07:33:22,040 it mixes three different languages to 8847 07:33:19,920 --> 07:33:24,440 make our code more organized and easier 8848 07:33:22,040 --> 07:33:28,040 to read we're going to separate each 8849 07:33:24,440 --> 07:33:30,558 language into its own file so let's 8850 07:33:28,040 --> 07:33:33,558 scroll down and we'll start by 8851 07:33:30,558 --> 07:33:37,920 separating our JavaScript into its own 8852 07:33:33,558 --> 07:33:41,160 file we'll click here to show our files 8853 07:33:37,920 --> 07:33:44,798 and then click this icon to create a new 8854 07:33:41,160 --> 07:33:47,798 file and we'll name this file 8855 07:33:44,798 --> 07:33:47,798 10- 8856 07:33:48,718 --> 07:33:54,760 rock-paper- 8857 07:33:50,680 --> 07:33:57,878 scissors. JS and press 8858 07:33:54,760 --> 07:34:00,558 enter so when we end a file 8859 07:33:57,878 --> 07:34:04,440 withjs it tells the computer that this 8860 07:34:00,558 --> 07:34:08,080 file contains JavaScript code not just 8861 07:34:04,440 --> 07:34:10,760 text now let's go back to our HTML 8862 07:34:08,080 --> 07:34:14,040 file and we're going to move all of this 8863 07:34:10,760 --> 07:34:16,600 code into the Javascript file so we'll 8864 07:34:14,040 --> 07:34:16,600 select this 8865 07:34:17,520 --> 07:34:22,600 code and then we'll 8866 07:34:20,240 --> 07:34:26,680 rightclick and 8867 07:34:22,600 --> 07:34:31,638 copy and inside the Javascript file 8868 07:34:26,680 --> 07:34:31,638 we'll rightclick and paste 8869 07:34:31,840 --> 07:34:37,360 now let's 8870 07:34:33,240 --> 07:34:41,318 save and we'll go back to the HTML file 8871 07:34:37,360 --> 07:34:44,000 and we can delete all this JavaScript 8872 07:34:41,318 --> 07:34:47,520 code and now we're just going to load 8873 07:34:44,000 --> 07:34:50,360 our Javascript file on to the web page 8874 07:34:47,520 --> 07:34:55,958 to do that we can use an attribute of 8875 07:34:50,360 --> 07:34:58,718 the script element called SRC or source 8876 07:34:55,958 --> 07:35:01,440 so we'll make it equal and this does the 8877 07:34:58,718 --> 07:35:03,958 same thing as the image element it tells 8878 07:35:01,440 --> 07:35:06,840 a computer which file to 8879 07:35:03,958 --> 07:35:09,840 load if we type 8880 07:35:06,840 --> 07:35:09,840 10- 8881 07:35:11,680 --> 07:35:17,680 rock-paper-scissors 8882 07:35:14,080 --> 07:35:21,680 DJs this will look for a file called 10 8883 07:35:17,680 --> 07:35:24,920 rockpaper scissors. JS beside our HTML 8884 07:35:21,680 --> 07:35:28,600 file which is this file here and it will 8885 07:35:24,920 --> 07:35:31,240 run all the JavaScript inside this file 8886 07:35:28,600 --> 07:35:34,280 so it does the same thing as before 8887 07:35:31,240 --> 07:35:35,440 except now our JavaScript code is in its 8888 07:35:34,280 --> 07:35:38,958 own 8889 07:35:35,440 --> 07:35:41,558 file and this makes our code cleaner 8890 07:35:38,958 --> 07:35:44,318 because each file only contains one 8891 07:35:41,558 --> 07:35:45,398 language and each file also contains 8892 07:35:44,318 --> 07:35:49,558 less 8893 07:35:45,398 --> 07:35:50,638 code now let's go back to the HTML file 8894 07:35:49,558 --> 07:35:53,080 and 8895 07:35:50,638 --> 07:35:55,760 save and you'll notice that our project 8896 07:35:53,080 --> 07:35:57,760 works the same way as before we just 8897 07:35:55,760 --> 07:36:00,200 organized our code 8898 07:35:57,760 --> 07:36:02,958 better another feature of The Source 8899 07:36:00,200 --> 07:36:05,600 attribute is that it also uses file 8900 07:36:02,958 --> 07:36:09,040 paths so we can put this Javascript file 8901 07:36:05,600 --> 07:36:12,240 in a folder and we'll be able to find it 8902 07:36:09,040 --> 07:36:14,840 for example let's show our files and 8903 07:36:12,240 --> 07:36:15,760 let's create a new folder by clicking 8904 07:36:14,840 --> 07:36:20,958 this 8905 07:36:15,760 --> 07:36:20,958 icon and let's name this folder 8906 07:36:21,240 --> 07:36:29,160 scripts so this folder will contain all 8907 07:36:24,200 --> 07:36:31,760 of our JS files so we can drag this file 8908 07:36:29,160 --> 07:36:31,760 into the 8909 07:36:32,360 --> 07:36:37,920 folder and now we can locate it using 8910 07:36:35,160 --> 07:36:42,000 the source attribute by typing the 8911 07:36:37,920 --> 07:36:45,558 folder name which is scripts and then a 8912 07:36:42,000 --> 07:36:49,440 slash to go inside this folder and look 8913 07:36:45,558 --> 07:36:51,558 for this file and it will find it right 8914 07:36:49,440 --> 07:36:54,398 here if we 8915 07:36:51,558 --> 07:36:57,280 save everything works as 8916 07:36:54,398 --> 07:37:00,080 normal another feature is we can use 8917 07:36:57,280 --> 07:37:02,920 multiple script elements this lets us 8918 07:37:00,080 --> 07:37:05,200 split our JavaScript into multiple files 8919 07:37:02,920 --> 07:37:06,958 and then load them all onto the page 8920 07:37:05,200 --> 07:37:09,600 from top to 8921 07:37:06,958 --> 07:37:13,200 bottom next we're going to learn how to 8922 07:37:09,600 --> 07:37:16,280 separate our CSS into its own 8923 07:37:13,200 --> 07:37:20,318 file let's scroll up to our 8924 07:37:16,280 --> 07:37:23,318 CSS so we can do the same thing with CSS 8925 07:37:20,318 --> 07:37:26,360 so let's click the HTML 8926 07:37:23,318 --> 07:37:30,878 file and let's create a new 8927 07:37:26,360 --> 07:37:32,478 folder and we'll call this folder Style 8928 07:37:30,878 --> 07:37:35,280 and press 8929 07:37:32,478 --> 07:37:39,240 enter so this folder will contain all of 8930 07:37:35,280 --> 07:37:42,080 our CSS code let's click the folder and 8931 07:37:39,240 --> 07:37:45,958 we'll create a new file by clicking this 8932 07:37:42,080 --> 07:37:47,638 icon and let's name this file 8933 07:37:45,958 --> 07:37:49,318 10- 8934 07:37:47,638 --> 07:37:51,638 ro- 8935 07:37:49,318 --> 07:37:55,240 paper- 8936 07:37:51,638 --> 07:37:59,638 scissors. CSS and press 8937 07:37:55,240 --> 07:38:03,280 enter so ending the file with CSS means 8938 07:37:59,638 --> 07:38:05,280 this file contains CSS code and now we 8939 07:38:03,280 --> 07:38:10,040 can go back to our 8940 07:38:05,280 --> 07:38:12,760 HTML and move our CSS into its own file 8941 07:38:10,040 --> 07:38:15,120 so let's select all this 8942 07:38:12,760 --> 07:38:16,958 code and then right 8943 07:38:15,120 --> 07:38:23,520 click 8944 07:38:16,958 --> 07:38:26,280 copy and in here right click and 8945 07:38:23,520 --> 07:38:30,360 paste and save this 8946 07:38:26,280 --> 07:38:32,718 file now let's go back to our HTML 8947 07:38:30,360 --> 07:38:35,840 and we can remove this 8948 07:38:32,718 --> 07:38:37,958 code and we'll learn how to load our CSS 8949 07:38:35,840 --> 07:38:40,398 file onto our 8950 07:38:37,958 --> 07:38:43,120 website so we're not going to use the 8951 07:38:40,398 --> 07:38:46,080 style element for this we're going to 8952 07:38:43,120 --> 07:38:50,398 use a different element called the link 8953 07:38:46,080 --> 07:38:53,040 element so we'll type less than link 8954 07:38:50,398 --> 07:38:55,680 greater than and the link element 8955 07:38:53,040 --> 07:38:58,920 doesn't require a closing tag this is 8956 07:38:55,680 --> 07:39:02,558 another void element so the link element 8957 07:38:58,920 --> 07:39:05,478 uses two attributes the first attribute 8958 07:39:02,558 --> 07:39:05,478 is called 8959 07:39:05,920 --> 07:39:12,878 R means relation this tells a computer 8960 07:39:09,958 --> 07:39:15,558 what type of file we're loading so we're 8961 07:39:12,878 --> 07:39:18,878 going to set this attribute to style 8962 07:39:15,558 --> 07:39:21,638 sheet this means we're loading a CSS 8963 07:39:18,878 --> 07:39:24,840 file and then we'll add a second 8964 07:39:21,638 --> 07:39:28,360 attribute called 8965 07:39:24,840 --> 07:39:30,160 href so href tells a computer which file 8966 07:39:28,360 --> 07:39:33,638 we want to load 8967 07:39:30,160 --> 07:39:36,600 so this also supports file paaths so we 8968 07:39:33,638 --> 07:39:39,360 can type the Styles 8969 07:39:36,600 --> 07:39:42,878 folder this will look for a folder 8970 07:39:39,360 --> 07:39:45,840 beside the HTML called Styles which is 8971 07:39:42,878 --> 07:39:49,280 this one right here and then we'll type 8972 07:39:45,840 --> 07:39:53,160 slash to go inside the folder and look 8973 07:39:49,280 --> 07:40:00,878 for this CSS file which is 8974 07:39:53,160 --> 07:40:04,478 10- Rock Das paper Das scissors 8975 07:40:00,878 --> 07:40:08,200 CSS so this will load all the CSS inside 8976 07:40:04,478 --> 07:40:11,080 this file and apply it to our page so it 8977 07:40:08,200 --> 07:40:13,798 does the same thing as before except we 8978 07:40:11,080 --> 07:40:17,958 separated each language into its own 8979 07:40:13,798 --> 07:40:21,080 file if we save everything still works 8980 07:40:17,958 --> 07:40:23,558 and all the Styles get applied and just 8981 07:40:21,080 --> 07:40:26,558 like the script element we can also use 8982 07:40:23,558 --> 07:40:28,000 multiple link elements to load multiple 8983 07:40:26,558 --> 07:40:30,798 CSS 8984 07:40:28,000 --> 07:40:33,680 files so that's how we separate our code 8985 07:40:30,798 --> 07:40:36,878 into separate files using the script 8986 07:40:33,680 --> 07:40:39,398 element and the link element it makes 8987 07:40:36,878 --> 07:40:42,280 our code a lot easier to read because 8988 07:40:39,398 --> 07:40:45,718 each file contains one language and it 8989 07:40:42,280 --> 07:40:48,920 contains less code and that's the end of 8990 07:40:45,718 --> 07:40:50,958 this lesson as a final reminder the CSS 8991 07:40:48,920 --> 07:40:53,840 we learned in this lesson is just a 8992 07:40:50,958 --> 07:40:57,520 review if you want to learn CSS in more 8993 07:40:53,840 --> 07:40:59,920 detail you can check out my HTML and CSS 8994 07:40:57,520 --> 07:41:02,318 full course in the video 8995 07:40:59,920 --> 07:41:06,318 description so in this lesson we 8996 07:41:02,318 --> 07:41:09,240 reviewed CSS and added CSS to all of our 8997 07:41:06,318 --> 07:41:12,080 projects we learned about the class list 8998 07:41:09,240 --> 07:41:14,958 property which lets us control the class 8999 07:41:12,080 --> 07:41:17,318 attribute of an element we finished our 9000 07:41:14,958 --> 07:41:19,760 rock paper scissors project and we 9001 07:41:17,318 --> 07:41:23,920 learned how to organize our JavaScript 9002 07:41:19,760 --> 07:41:23,920 and CSS code into separate 9003 07:41:25,000 --> 07:41:30,040 files thanks for watching this course so 9004 07:41:27,558 --> 07:41:32,040 far if you want to support this channel 9005 07:41:30,040 --> 07:41:34,318 you can check out the premium version of 9006 07:41:32,040 --> 07:41:36,718 my courses just like this one where you 9007 07:41:34,318 --> 07:41:39,200 can get a certificate of completion and 9008 07:41:36,718 --> 07:41:41,958 a Better Learning platform if you're 9009 07:41:39,200 --> 07:41:44,520 interested in extra videos from me more 9010 07:41:41,958 --> 07:41:47,240 in-depth videos and behindth scenes 9011 07:41:44,520 --> 07:41:49,920 content you can check out these links 9012 07:41:47,240 --> 07:41:52,760 here you can also support the channel by 9013 07:41:49,920 --> 07:41:55,600 liking the video subscribing and sharing 9014 07:41:52,760 --> 07:41:59,280 this video with others thanks again and 9015 07:41:55,600 --> 07:42:01,120 let's continue with the course 9016 07:41:59,280 --> 07:42:05,280 here are some exercises to help you 9017 07:42:01,120 --> 07:42:08,280 practice using HTML CSS and JavaScript 9018 07:42:05,280 --> 07:42:08,280 together 9019 07:42:31,600 --> 07:42:36,318 in this lesson we're going to learn two 9020 07:42:33,718 --> 07:42:39,200 more features of JavaScript called 9021 07:42:36,318 --> 07:42:42,398 arrays and loops and we're going to use 9022 07:42:39,200 --> 07:42:45,718 these features to create this to-do list 9023 07:42:42,398 --> 07:42:48,798 project first let's create a new file 9024 07:42:45,718 --> 07:42:51,718 for this lesson we'll go to our code 9025 07:42:48,798 --> 07:42:54,638 editor and click up here to show our 9026 07:42:51,718 --> 07:42:57,600 files and this time make sure you have 9027 07:42:54,638 --> 07:42:59,600 one of these HTML files selected so we 9028 07:42:57,600 --> 07:43:00,638 don't create the new file in one of 9029 07:42:59,600 --> 07:43:03,160 these 9030 07:43:00,638 --> 07:43:05,680 folders so now we're going to click this 9031 07:43:03,160 --> 07:43:09,958 icon to create a new 9032 07:43:05,680 --> 07:43:16,120 file and we're going to name this file 9033 07:43:09,958 --> 07:43:17,920 11- arrays Das and Das 9034 07:43:16,120 --> 07:43:20,840 loops. 9035 07:43:17,920 --> 07:43:24,160 HTML and press 9036 07:43:20,840 --> 07:43:27,878 enter now let's copy the previous lesson 9037 07:43:24,160 --> 07:43:30,878 into this file so we'll click on 10 Dom 9038 07:43:27,878 --> 07:43:33,958 projects and and we'll type here and typ 9039 07:43:30,878 --> 07:43:36,080 control a or command a to select the 9040 07:43:33,958 --> 07:43:38,080 code and then 9041 07:43:36,080 --> 07:43:40,638 rightclick 9042 07:43:38,080 --> 07:43:44,478 copy and in 9043 07:43:40,638 --> 07:43:48,360 here right click and 9044 07:43:44,478 --> 07:43:51,318 paste now let's hide our files and we'll 9045 07:43:48,360 --> 07:43:53,360 prepare this file for this lesson so we 9046 07:43:51,318 --> 07:43:59,520 can scroll to the 9047 07:43:53,360 --> 07:44:01,360 top and we'll change the title to arrays 9048 07:43:59,520 --> 07:44:06,080 and 9049 07:44:01,360 --> 07:44:06,080 loops and let's remove all of the 9050 07:44:09,000 --> 07:44:16,878 CSS and we'll also remove the 9051 07:44:13,200 --> 07:44:16,878 HTML which is right 9052 07:44:18,718 --> 07:44:23,080 here and we'll remove all the 9053 07:44:24,840 --> 07:44:29,440 JavaScript now let's 9054 07:44:27,040 --> 07:44:35,080 save and we'll open this file in the 9055 07:44:29,440 --> 07:44:38,440 browser by right clicking open with live 9056 07:44:35,080 --> 07:44:41,840 server and let's also open the console 9057 07:44:38,440 --> 07:44:44,718 so we right click click 9058 07:44:41,840 --> 07:44:47,080 inspect and click the 9059 07:44:44,718 --> 07:44:48,398 console and now we can close the 9060 07:44:47,080 --> 07:44:53,000 previous 9061 07:44:48,398 --> 07:44:53,000 tabs and the previous code for 9062 07:44:53,398 --> 07:45:01,280 now until we just have the lesson 11 9063 07:44:57,360 --> 07:45:04,000 file now we're ready to learn arrays an 9064 07:45:01,280 --> 07:45:07,240 array is another type of value in 9065 07:45:04,000 --> 07:45:10,478 JavaScript and it represents a list of 9066 07:45:07,240 --> 07:45:13,958 other values so let's do an example and 9067 07:45:10,478 --> 07:45:17,040 create our first array we'll go into the 9068 07:45:13,958 --> 07:45:19,958 script element and we're going to type 9069 07:45:17,040 --> 07:45:22,718 open square bracket and closed square 9070 07:45:19,958 --> 07:45:26,840 bracket and inside the square brackets 9071 07:45:22,718 --> 07:45:29,240 we'll type a value like the number 10 9072 07:45:26,840 --> 07:45:32,478 and then we'll type comma and and 9073 07:45:29,240 --> 07:45:35,680 another value like the number 20 and 9074 07:45:32,478 --> 07:45:37,478 then comma again and a third value like 9075 07:45:35,680 --> 07:45:42,280 the number 9076 07:45:37,478 --> 07:45:46,958 30 so this is an array and it represents 9077 07:45:42,280 --> 07:45:51,440 a list of three numbers 10 20 and 9078 07:45:46,958 --> 07:45:53,638 30 now arrays themselves are also values 9079 07:45:51,440 --> 07:45:56,798 which means we can save them in a 9080 07:45:53,638 --> 07:45:59,760 variable so at the front we can create a 9081 07:45:56,798 --> 07:46:04,040 variable using const 9082 07:45:59,760 --> 07:46:07,600 and let's name it my array and we'll 9083 07:46:04,040 --> 07:46:11,240 make it equal to this array and we'll 9084 07:46:07,600 --> 07:46:14,280 add a semicolon at the end so now we 9085 07:46:11,240 --> 07:46:16,478 created a list of values and we saved 9086 07:46:14,280 --> 07:46:19,878 this list in this 9087 07:46:16,478 --> 07:46:22,478 variable let's create a new line and 9088 07:46:19,878 --> 07:46:22,478 we'll 9089 07:46:22,920 --> 07:46:28,958 console.log my 9090 07:46:25,760 --> 07:46:31,240 array and Save 9091 07:46:28,958 --> 07:46:34,360 and it will display the list of values 9092 07:46:31,240 --> 07:46:37,240 in the console so that's basically what 9093 07:46:34,360 --> 07:46:40,360 an array is it's a list of 9094 07:46:37,240 --> 07:46:43,840 values now to get a specific value in an 9095 07:46:40,360 --> 07:46:47,478 array we can use square brackets so 9096 07:46:43,840 --> 07:46:51,040 let's create a new line and we'll type 9097 07:46:47,478 --> 07:46:53,878 my array and square 9098 07:46:51,040 --> 07:46:55,958 brackets inside these brackets we'll put 9099 07:46:53,878 --> 07:46:59,280 a number like 9100 07:46:55,958 --> 07:47:02,920 zero this number represents the position 9101 07:46:59,280 --> 07:47:05,600 in the array so zero means the first 9102 07:47:02,920 --> 07:47:10,360 value if we 9103 07:47:05,600 --> 07:47:12,798 console do log this 9104 07:47:10,360 --> 07:47:16,760 code and 9105 07:47:12,798 --> 07:47:19,680 save my array Square braet 0 gives us 9106 07:47:16,760 --> 07:47:21,478 the first value which is 10 and that's 9107 07:47:19,680 --> 07:47:25,360 what's displayed in the 9108 07:47:21,478 --> 07:47:28,398 console now if we change this to 9109 07:47:25,360 --> 07:47:32,798 one this will get us the second value in 9110 07:47:28,398 --> 07:47:36,478 the array which is 20 if we save it 9111 07:47:32,798 --> 07:47:40,040 displays 20 so this number represents 9112 07:47:36,478 --> 07:47:44,360 the position in the array zero is the 9113 07:47:40,040 --> 07:47:48,398 first value one is the second value two 9114 07:47:44,360 --> 07:47:51,638 is the third value and so on we can also 9115 07:47:48,398 --> 07:47:55,080 change a value in an array also using 9116 07:47:51,638 --> 07:47:59,798 square brackets so let's create a new 9117 07:47:55,080 --> 07:48:02,440 line and we'll type my array 9118 07:47:59,798 --> 07:48:05,840 square brackets and let's say we want to 9119 07:48:02,440 --> 07:48:08,760 change the first value so we'll type 9120 07:48:05,840 --> 07:48:13,080 zero for the first value and we just 9121 07:48:08,760 --> 07:48:17,040 make it equal to something else like 9122 07:48:13,080 --> 07:48:17,040 99 now if we 9123 07:48:17,398 --> 07:48:23,120 console.log my array 9124 07:48:20,878 --> 07:48:26,558 again and 9125 07:48:23,120 --> 07:48:29,280 save you can see that this changed the 9126 07:48:26,558 --> 07:48:32,080 first value to 99 9127 07:48:29,280 --> 07:48:35,040 so that's basically how arrays work we 9128 07:48:32,080 --> 07:48:38,240 can access a value inside an array and 9129 07:48:35,040 --> 07:48:41,040 we can also change a value inside an 9130 07:48:38,240 --> 07:48:44,920 array now that we understand the basics 9131 07:48:41,040 --> 07:48:47,760 of arrays let's learn the syntax rules 9132 07:48:44,920 --> 07:48:50,520 to create an array we start with an open 9133 07:48:47,760 --> 07:48:54,200 square bracket and we end with a closed 9134 07:48:50,520 --> 07:48:57,240 square bracket inside we can put a list 9135 07:48:54,200 --> 07:48:59,398 of values and we separate each value 9136 07:48:57,240 --> 07:49:01,760 with a comma 9137 07:48:59,398 --> 07:49:05,840 we can also write an array on multiple 9138 07:49:01,760 --> 07:49:08,200 lines like this we just put the new 9139 07:49:05,840 --> 07:49:11,840 lines after the 9140 07:49:08,200 --> 07:49:15,080 comma Now to get a value inside an array 9141 07:49:11,840 --> 07:49:19,478 we can use square brackets and a number 9142 07:49:15,080 --> 07:49:21,958 like one this number is called the index 9143 07:49:19,478 --> 07:49:25,000 it represents the position in the array 9144 07:49:21,958 --> 07:49:28,478 that we want to get so the first value 9145 07:49:25,000 --> 07:49:33,520 is index zero the second value is index 9146 07:49:28,478 --> 07:49:36,318 one 1 third value is index 2 and so on 9147 07:49:33,520 --> 07:49:39,240 notice that the index starts at zero 9148 07:49:36,318 --> 07:49:42,958 this is called a zero indexed 9149 07:49:39,240 --> 07:49:46,798 array now inside an array we can put any 9150 07:49:42,958 --> 07:49:50,638 type of value for example let's create a 9151 07:49:46,798 --> 07:49:54,240 new line and we'll create a new array 9152 07:49:50,638 --> 07:49:58,160 using square brackets and inside we can 9153 07:49:54,240 --> 07:50:01,558 put the number one and then comma and we 9154 07:49:58,160 --> 07:50:05,600 can also put a string like 9155 07:50:01,558 --> 07:50:09,840 hello and another comma and we can put a 9156 07:50:05,600 --> 07:50:14,040 Boolean value like true and another 9157 07:50:09,840 --> 07:50:14,920 comma and we can even put an object like 9158 07:50:14,040 --> 07:50:17,718 Curly 9159 07:50:14,920 --> 07:50:20,360 brackets Name 9160 07:50:17,718 --> 07:50:24,760 colon 9161 07:50:20,360 --> 07:50:28,398 socks now an array itself is also a 9162 07:50:24,760 --> 07:50:32,638 value that means we can save arrays in 9163 07:50:28,398 --> 07:50:37,360 very variables like this or even save it 9164 07:50:32,638 --> 07:50:43,240 in an array for example in this array we 9165 07:50:37,360 --> 07:50:46,958 can add an array inside like 1 comma 9166 07:50:43,240 --> 07:50:50,360 2 next arrays have a weird behavior when 9167 07:50:46,958 --> 07:50:55,040 we try to check the type of an array so 9168 07:50:50,360 --> 07:50:55,040 on a new line let's type 9169 07:50:55,638 --> 07:51:02,958 console.log type of and then square 9170 07:50:59,638 --> 07:51:05,760 brackets one comma 9171 07:51:02,958 --> 07:51:09,600 2 and 9172 07:51:05,760 --> 07:51:13,080 save this will tell us that an array is 9173 07:51:09,600 --> 07:51:15,478 an object and that's because an array is 9174 07:51:13,080 --> 07:51:18,440 actually an object but it's just a 9175 07:51:15,478 --> 07:51:20,878 special type of object if we want to 9176 07:51:18,440 --> 07:51:25,798 check if something is specifically an 9177 07:51:20,878 --> 07:51:29,638 array we can use this code capital A 9178 07:51:25,798 --> 07:51:31,240 array dot is 9179 07:51:29,638 --> 07:51:33,840 array 9180 07:51:31,240 --> 07:51:38,040 brackets and inside the brackets we'll 9181 07:51:33,840 --> 07:51:42,360 put an array like square brackets 1 9182 07:51:38,040 --> 07:51:46,398 comma 2 so this will check if this value 9183 07:51:42,360 --> 07:51:49,760 is an array if we 9184 07:51:46,398 --> 07:51:51,958 console. log this 9185 07:51:49,760 --> 07:51:55,240 result and 9186 07:51:51,958 --> 07:51:58,680 save it will display 9187 07:51:55,240 --> 07:52:01,638 true and finally because arrays are are 9188 07:51:58,680 --> 07:52:04,878 just a type of object they also have 9189 07:52:01,638 --> 07:52:08,840 properties and methods we can use so one 9190 07:52:04,878 --> 07:52:11,958 useful property is called length so 9191 07:52:08,840 --> 07:52:11,958 we'll type 9192 07:52:12,440 --> 07:52:18,600 console.log my 9193 07:52:15,680 --> 07:52:21,440 array. 9194 07:52:18,600 --> 07:52:25,840 length this will tell us how many values 9195 07:52:21,440 --> 07:52:28,958 are in the array so my array has three 9196 07:52:25,840 --> 07:52:30,958 values so if we save 9197 07:52:28,958 --> 07:52:34,318 it will display 9198 07:52:30,958 --> 07:52:38,840 three next we'll learn a useful method 9199 07:52:34,318 --> 07:52:41,080 of an array called push this adds a 9200 07:52:38,840 --> 07:52:45,280 value to the end of the 9201 07:52:41,080 --> 07:52:47,080 array if we did my 9202 07:52:45,280 --> 07:52:48,958 array. 9203 07:52:47,080 --> 07:52:53,680 push 9204 07:52:48,958 --> 07:52:58,240 100 this will add 100 to the end of my 9205 07:52:53,680 --> 07:52:58,240 array if we console.log 9206 07:52:58,878 --> 07:53:02,878 my 9207 07:53:00,520 --> 07:53:06,718 array and 9208 07:53:02,878 --> 07:53:07,520 save you can see that 100 was added at 9209 07:53:06,718 --> 07:53:10,000 the 9210 07:53:07,520 --> 07:53:14,280 end the last method we're going to learn 9211 07:53:10,000 --> 07:53:20,080 for now is called do splice this removes 9212 07:53:14,280 --> 07:53:22,600 a value from an array for example my 9213 07:53:20,080 --> 07:53:24,558 array do 9214 07:53:22,600 --> 07:53:28,200 splice and 9215 07:53:24,558 --> 07:53:30,600 brackets and a splice takes two numbers 9216 07:53:28,200 --> 07:53:33,160 the first number is the index that we 9217 07:53:30,600 --> 07:53:37,440 want to remove so let's say we want to 9218 07:53:33,160 --> 07:53:40,798 remove the first value which is index 9219 07:53:37,440 --> 07:53:43,080 zero the second number we give to splice 9220 07:53:40,798 --> 07:53:46,760 is the number of values we want to 9221 07:53:43,080 --> 07:53:50,398 remove if we give it one it will remove 9222 07:53:46,760 --> 07:53:53,600 one value at index zero if we give it 9223 07:53:50,398 --> 07:53:57,080 two it will remove two values starting 9224 07:53:53,600 --> 07:53:59,558 at index zero so for now let's just 9225 07:53:57,080 --> 07:54:04,160 remove one value 9226 07:53:59,558 --> 07:54:04,160 and let's create a new line and 9227 07:54:04,920 --> 07:54:10,280 console.log my 9228 07:54:08,080 --> 07:54:13,840 array and 9229 07:54:10,280 --> 07:54:17,478 save you can see that splice removed the 9230 07:54:13,840 --> 07:54:18,798 first value from the array or index0 9231 07:54:17,478 --> 07:54:22,000 which was 9232 07:54:18,798 --> 07:54:25,120 99 all right now that we understand the 9233 07:54:22,000 --> 07:54:28,520 basics of arrays and the syntax rules 9234 07:54:25,120 --> 07:54:30,600 let's do some practical examples so 9235 07:54:28,520 --> 07:54:34,558 prepared a project we can do for this 9236 07:54:30,600 --> 07:54:36,318 lesson we'll go to our browser create a 9237 07:54:34,558 --> 07:54:41,718 new tab at the 9238 07:54:36,318 --> 07:54:43,360 top and here we'll type 9239 07:54:41,718 --> 07:54:51,160 superimple 9240 07:54:43,360 --> 07:54:54,160 dodev SL projects slash arrays and press 9241 07:54:51,160 --> 07:54:56,000 enter so this page contains the to-do 9242 07:54:54,160 --> 07:54:59,318 list project we're going to create in 9243 07:54:56,000 --> 07:55:02,440 this lesson so down here is the final 9244 07:54:59,318 --> 07:55:06,318 version of the project we can type a 9245 07:55:02,440 --> 07:55:10,558 name for our to-do like make 9246 07:55:06,318 --> 07:55:13,958 dinner give it a due date and then click 9247 07:55:10,558 --> 07:55:18,600 add and it will add it to our list we 9248 07:55:13,958 --> 07:55:22,280 can add another to-do like wash 9249 07:55:18,600 --> 07:55:25,120 dishes and then select a due date and 9250 07:55:22,280 --> 07:55:28,638 click add again and it will add another 9251 07:55:25,120 --> 07:55:31,760 to-do to our list and each to-do also 9252 07:55:28,638 --> 07:55:34,958 has a delete button so we can delete it 9253 07:55:31,760 --> 07:55:37,798 when we're finished the task so this is 9254 07:55:34,958 --> 07:55:40,478 the final version of the project and 9255 07:55:37,798 --> 07:55:42,280 these two up here are simpler versions 9256 07:55:40,478 --> 07:55:45,600 of the project that we're going to use 9257 07:55:42,280 --> 07:55:48,840 to work our way up so let's start with 9258 07:55:45,600 --> 07:55:51,398 the first project up here we're going to 9259 07:55:48,840 --> 07:55:53,920 open our console by 9260 07:55:51,398 --> 07:55:56,840 right-clicking click 9261 07:55:53,920 --> 07:55:59,200 inspect and click the 9262 07:55:56,840 --> 07:56:02,600 console and the way way this first 9263 07:55:59,200 --> 07:56:06,638 project works is we can type a to-do in 9264 07:56:02,600 --> 07:56:10,600 this text box like make 9265 07:56:06,638 --> 07:56:13,600 dinner and when we click add it will add 9266 07:56:10,600 --> 07:56:14,318 the to-do to an array and display it in 9267 07:56:13,600 --> 07:56:18,558 the 9268 07:56:14,318 --> 07:56:23,878 console if we type another to-do like 9269 07:56:18,558 --> 07:56:26,718 wash dishes and click add it will add it 9270 07:56:23,878 --> 07:56:28,718 to the array and display it in the 9271 07:56:26,718 --> 07:56:31,440 console so let's create create this 9272 07:56:28,718 --> 07:56:34,520 project we'll start by creating a file 9273 07:56:31,440 --> 07:56:38,000 for this project we'll go to our code 9274 07:56:34,520 --> 07:56:41,798 editor and click up here to show our 9275 07:56:38,000 --> 07:56:45,080 files and click this icon to create a 9276 07:56:41,798 --> 07:56:46,920 file and we'll name this 9277 07:56:45,080 --> 07:56:48,638 11- 9278 07:56:46,920 --> 07:56:50,200 too- 9279 07:56:48,638 --> 07:56:53,120 list. 9280 07:56:50,200 --> 07:56:56,000 HTML and press 9281 07:56:53,120 --> 07:56:58,718 enter now let's copy the code from 9282 07:56:56,000 --> 07:57:02,200 arrays and loops. HTML 9283 07:56:58,718 --> 07:57:04,680 so we'll click here control a or command 9284 07:57:02,200 --> 07:57:06,200 a and then 9285 07:57:04,680 --> 07:57:12,638 rightclick 9286 07:57:06,200 --> 07:57:15,878 copy and down here right click and 9287 07:57:12,638 --> 07:57:20,878 paste and let's close this for 9288 07:57:15,878 --> 07:57:26,160 now we'll scroll up and change the title 9289 07:57:20,878 --> 07:57:29,718 to to do list and remove all the 9290 07:57:26,160 --> 07:57:29,718 JavaScript code 9291 07:57:31,000 --> 07:57:35,638 and this time instead of writing all the 9292 07:57:33,200 --> 07:57:38,240 JavaScript in here let's actually put 9293 07:57:35,638 --> 07:57:40,958 the JavaScript in a separate file which 9294 07:57:38,240 --> 07:57:42,760 we learned in the previous lesson so 9295 07:57:40,958 --> 07:57:45,638 we'll open our files 9296 07:57:42,760 --> 07:57:49,680 again and this time we'll go to our 9297 07:57:45,638 --> 07:57:52,318 scripts folder we'll click this JS file 9298 07:57:49,680 --> 07:57:57,120 and we'll click this icon to add a new 9299 07:57:52,318 --> 07:58:02,000 file to this folder and let's name it 9300 07:57:57,120 --> 07:58:04,520 11- to-do Das 9301 07:58:02,000 --> 07:58:08,120 list.js and press 9302 07:58:04,520 --> 07:58:12,798 enter next we're going to load this file 9303 07:58:08,120 --> 07:58:15,280 into our HTML file so let's open this 9304 07:58:12,798 --> 07:58:17,840 and in the script element we can remove 9305 07:58:15,280 --> 07:58:20,318 the spaces and we're going to add an 9306 07:58:17,840 --> 07:58:23,680 attribute called 9307 07:58:20,318 --> 07:58:26,638 SRC equals and we're going to load the 9308 07:58:23,680 --> 07:58:29,600 Javascript file that we just created 9309 07:58:26,638 --> 07:58:31,638 which is inside the script folder so 9310 07:58:29,600 --> 07:58:35,200 we'll type 9311 07:58:31,638 --> 07:58:39,040 scripts and then a slash to go into this 9312 07:58:35,200 --> 07:58:40,760 folder and we'll load the file 9313 07:58:39,040 --> 07:58:43,718 11- 9314 07:58:40,760 --> 07:58:46,160 too- 9315 07:58:43,718 --> 07:58:49,600 list.js and 9316 07:58:46,160 --> 07:58:53,680 save so now I'm going to drag this file 9317 07:58:49,600 --> 07:58:56,478 down here so I can see both the HTML and 9318 07:58:53,680 --> 07:58:59,798 JavaScript at the same time close this 9319 07:58:56,478 --> 07:59:01,160 for now and I can close the other code 9320 07:58:59,798 --> 07:59:04,160 for now as 9321 07:59:01,160 --> 07:59:06,558 well and finally let's open this in our 9322 07:59:04,160 --> 07:59:10,280 browser so we'll right 9323 07:59:06,558 --> 07:59:13,160 click open with live 9324 07:59:10,280 --> 07:59:17,318 server and let's also open the console 9325 07:59:13,160 --> 07:59:20,360 on this page right click click 9326 07:59:17,318 --> 07:59:22,600 inspect and click the 9327 07:59:20,360 --> 07:59:25,638 console now one more thing I'm going to 9328 07:59:22,600 --> 07:59:28,200 do for this video is I'll rearrange my 9329 07:59:25,638 --> 07:59:30,160 windows so that we can have the the 9330 07:59:28,200 --> 07:59:34,040 final design at the 9331 07:59:30,160 --> 07:59:37,360 bottom let's start by creating the HTML 9332 07:59:34,040 --> 07:59:40,600 elements let's go to the body and we'll 9333 07:59:37,360 --> 07:59:43,520 actually create a paragraph element to 9334 07:59:40,600 --> 07:59:47,000 start inside the paragraph we'll put the 9335 07:59:43,520 --> 07:59:51,558 text to-do 9336 07:59:47,000 --> 07:59:55,520 list next let's create the text box so 9337 07:59:51,558 --> 07:59:59,520 to create a text box less than 9338 07:59:55,520 --> 08:00:05,600 input greater than and add a placeholder 9339 07:59:59,520 --> 08:00:10,520 to this text box Place holder equals to 9340 08:00:05,600 --> 08:00:14,840 do name finally let's add this 9341 08:00:10,520 --> 08:00:18,680 button so on a new line let's type less 9342 08:00:14,840 --> 08:00:21,318 than button greater than and inside the 9343 08:00:18,680 --> 08:00:23,600 button we'll have the text 9344 08:00:21,318 --> 08:00:26,840 add now let's 9345 08:00:23,600 --> 08:00:29,478 save and this creates all the HTML 9346 08:00:26,840 --> 08:00:33,120 elements that we need 9347 08:00:29,478 --> 08:00:35,798 next let's write our JavaScript so first 9348 08:00:33,120 --> 08:00:39,240 we're going to create an array to store 9349 08:00:35,798 --> 08:00:41,798 our to-dos and then when we click the 9350 08:00:39,240 --> 08:00:45,000 add button we're going to get the text 9351 08:00:41,798 --> 08:00:49,000 from the text box and add it to our 9352 08:00:45,000 --> 08:00:52,080 array and finally we'll console.log the 9353 08:00:49,000 --> 08:00:55,958 array now let's convert these steps into 9354 08:00:52,080 --> 08:00:58,440 code let's create a new array first 9355 08:00:55,958 --> 08:01:00,360 using const 9356 08:00:58,440 --> 08:01:04,440 let's name it 9357 08:01:00,360 --> 08:01:05,440 to-do list and we'll make it equal to 9358 08:01:04,440 --> 08:01:08,398 square 9359 08:01:05,440 --> 08:01:12,398 brackets so this is an array with no 9360 08:01:08,398 --> 08:01:15,680 values inside we also call this an empty 9361 08:01:12,398 --> 08:01:19,080 array next when we click the button 9362 08:01:15,680 --> 08:01:22,638 let's get the value from the text box so 9363 08:01:19,080 --> 08:01:24,760 let's go to our button and add an 9364 08:01:22,638 --> 08:01:28,240 onclick 9365 08:01:24,760 --> 08:01:32,360 attribute and then inside here let's 9366 08:01:28,240 --> 08:01:35,000 create a function for this so down here 9367 08:01:32,360 --> 08:01:40,520 let's create a 9368 08:01:35,000 --> 08:01:42,040 function we'll name it add Todo brackets 9369 08:01:40,520 --> 08:01:44,878 and curly 9370 08:01:42,040 --> 08:01:47,958 brackets and then inside the onclick 9371 08:01:44,878 --> 08:01:52,398 attribute we're just going to call this 9372 08:01:47,958 --> 08:01:55,398 function add too and 9373 08:01:52,398 --> 08:01:58,798 brackets the next step is to get the 9374 08:01:55,398 --> 08:02:02,600 text in the text box we can do that 9375 08:01:58,798 --> 08:02:04,878 using document. query selector so to use 9376 08:02:02,600 --> 08:02:08,398 Query selector we're going to put a 9377 08:02:04,878 --> 08:02:10,440 class on this input element so we'll 9378 08:02:08,398 --> 08:02:12,878 type the class 9379 08:02:10,440 --> 08:02:16,798 attribute 9380 08:02:12,878 --> 08:02:24,478 js- name Das 9381 08:02:16,798 --> 08:02:28,520 input and then in here document do query 9382 08:02:24,478 --> 08:02:32,318 selector brackets and a string and we'll 9383 08:02:28,520 --> 08:02:34,718 type a DOT to get a class and we'll get 9384 08:02:32,318 --> 08:02:36,600 this class 9385 08:02:34,718 --> 08:02:38,440 js- 9386 08:02:36,600 --> 08:02:42,120 name- 9387 08:02:38,440 --> 08:02:45,280 input as a reminder this gets an element 9388 08:02:42,120 --> 08:02:49,040 with the class JS name input which is 9389 08:02:45,280 --> 08:02:52,200 this element and puts it into our 9390 08:02:49,040 --> 08:02:56,440 JavaScript and now let's save this in a 9391 08:02:52,200 --> 08:03:01,878 variable so at the start let's type 9392 08:02:56,440 --> 08:03:03,280 const in input element and make it equal 9393 08:03:01,878 --> 08:03:06,478 to this 9394 08:03:03,280 --> 08:03:08,000 result so now we took this input and put 9395 08:03:06,478 --> 08:03:10,760 it into our 9396 08:03:08,000 --> 08:03:13,440 JavaScript to get the text out we're 9397 08:03:10,760 --> 08:03:18,478 going to use a property of the input 9398 08:03:13,440 --> 08:03:20,440 element called value so down here 9399 08:03:18,478 --> 08:03:21,958 input 9400 08:03:20,440 --> 08:03:24,920 element. 9401 08:03:21,958 --> 08:03:27,478 value and let's save the value in the 9402 08:03:24,920 --> 08:03:28,958 text box in a variable so we can use it 9403 08:03:27,478 --> 08:03:33,478 later 9404 08:03:28,958 --> 08:03:37,440 at the front const name and we'll make 9405 08:03:33,478 --> 08:03:41,638 it equal to whatever's in the text 9406 08:03:37,440 --> 08:03:45,318 box before we continue let's 9407 08:03:41,638 --> 08:03:48,840 console.log the name to check if this is 9408 08:03:45,318 --> 08:03:53,160 working so let's save this Javascript 9409 08:03:48,840 --> 08:03:56,600 file and let's also save this HTML 9410 08:03:53,160 --> 08:03:58,638 file we'll go to our browser and I'll 9411 08:03:56,600 --> 08:04:01,318 resize it so we can you can see the 9412 08:03:58,638 --> 08:04:06,760 console and now if we type something in 9413 08:04:01,318 --> 08:04:09,440 the text box like make dinner and click 9414 08:04:06,760 --> 08:04:12,478 add it will get the text from the text 9415 08:04:09,440 --> 08:04:16,120 box and then display it in the 9416 08:04:12,478 --> 08:04:19,760 console okay so it's looking good so far 9417 08:04:16,120 --> 08:04:22,398 the next step is to add this name to our 9418 08:04:19,760 --> 08:04:26,638 to-do list so we learned how to do this 9419 08:04:22,398 --> 08:04:29,760 earlier we can use a method called push 9420 08:04:26,638 --> 08:04:33,840 push will add a value to the end of the 9421 08:04:29,760 --> 08:04:35,600 array so let's remove this for now and 9422 08:04:33,840 --> 08:04:38,840 at the 9423 08:04:35,600 --> 08:04:42,478 bottom we're going to type our 9424 08:04:38,840 --> 08:04:45,398 array to do 9425 08:04:42,478 --> 08:04:48,398 list. push 9426 08:04:45,398 --> 08:04:50,920 brackets and then between the brackets 9427 08:04:48,398 --> 08:04:53,878 we're going to add our name 9428 08:04:50,920 --> 08:04:58,558 here and now the last step is to 9429 08:04:53,878 --> 08:04:58,558 console.log the entire array 9430 08:05:00,478 --> 08:05:06,478 console.log Todo 9431 08:05:03,878 --> 08:05:11,000 list and 9432 08:05:06,478 --> 08:05:15,080 save now let's give it a try or'll type 9433 08:05:11,000 --> 08:05:18,840 make dinner in the text box and click 9434 08:05:15,080 --> 08:05:22,638 add it will take this text add it to the 9435 08:05:18,840 --> 08:05:25,798 array and then console.log the array and 9436 08:05:22,638 --> 08:05:30,080 now we have our to-do list let's add 9437 08:05:25,798 --> 08:05:32,600 another to-do we'll type type wash 9438 08:05:30,080 --> 08:05:35,398 dishes and click 9439 08:05:32,600 --> 08:05:39,000 add again it will get the text in the 9440 08:05:35,398 --> 08:05:42,360 text box and then add it to the array so 9441 08:05:39,000 --> 08:05:43,958 now our array has two values and then we 9442 08:05:42,360 --> 08:05:47,160 display them in the 9443 08:05:43,958 --> 08:05:50,760 console so that's how we create this 9444 08:05:47,160 --> 08:05:52,200 first version of the to-do list project 9445 08:05:50,760 --> 08:05:55,040 now one more feature we're going to 9446 08:05:52,200 --> 08:05:58,600 create is if we add something to our 9447 08:05:55,040 --> 08:06:02,318 to-do list and click add 9448 08:05:58,600 --> 08:06:03,760 it will reset the text box to do that 9449 08:06:02,318 --> 08:06:07,040 let's go into our 9450 08:06:03,760 --> 08:06:09,478 code and we'll create some new lines and 9451 08:06:07,040 --> 08:06:12,840 remember that the value property 9452 08:06:09,478 --> 08:06:15,398 represents the text in the text box so 9453 08:06:12,840 --> 08:06:18,798 to change the text in the text box we 9454 08:06:15,398 --> 08:06:20,718 can simply change the value property so 9455 08:06:18,798 --> 08:06:22,478 we'll type 9456 08:06:20,718 --> 08:06:26,040 input 9457 08:06:22,478 --> 08:06:27,958 element. value and change it to the 9458 08:06:26,040 --> 08:06:29,958 empty string 9459 08:06:27,958 --> 08:06:33,878 so this will make the text in the text 9460 08:06:29,958 --> 08:06:37,638 box become empty if we 9461 08:06:33,878 --> 08:06:39,798 save and now type something in the text 9462 08:06:37,638 --> 08:06:43,240 box and click 9463 08:06:39,798 --> 08:06:46,160 add it will reset the text in the text 9464 08:06:43,240 --> 08:06:49,920 box let's move on to the second version 9465 08:06:46,160 --> 08:06:53,200 of the to-do list so here when we type 9466 08:06:49,920 --> 08:06:56,958 something in the text box like make 9467 08:06:53,200 --> 08:06:59,920 dinner and click add it will actually 9468 08:06:56,958 --> 08:07:04,360 display it on the page if we type 9469 08:06:59,920 --> 08:07:07,958 something else like wash dishes and 9470 08:07:04,360 --> 08:07:11,360 click add it will also add it to the 9471 08:07:07,958 --> 08:07:14,798 page so how do we take our array and 9472 08:07:11,360 --> 08:07:16,478 display each value on the page to do 9473 08:07:14,798 --> 08:07:20,398 this we're going to learn another 9474 08:07:16,478 --> 08:07:23,600 feature of JavaScript called loops loops 9475 08:07:20,398 --> 08:07:28,160 let us run some code over and over like 9476 08:07:23,600 --> 08:07:31,160 a loop so let's go back to our 11 arrays 9477 08:07:28,160 --> 08:07:33,920 and Loops file and we can close this 9478 08:07:31,160 --> 08:07:37,360 JavaScript file for now and we're going 9479 08:07:33,920 --> 08:07:41,120 to learn Loops so first let's comment 9480 08:07:37,360 --> 08:07:45,760 out the previous code by typing 9481 08:07:41,120 --> 08:07:47,878 slashstar and at the bottom star 9482 08:07:45,760 --> 08:07:51,638 slash let's scroll 9483 08:07:47,878 --> 08:07:54,878 down and we'll create our first Loop 9484 08:07:51,638 --> 08:07:57,840 we'll start by creating a variable using 9485 08:07:54,878 --> 08:08:02,520 the word let and we'll name name this 9486 08:07:57,840 --> 08:08:06,680 variable I and we'll make it equal to 9487 08:08:02,520 --> 08:08:10,718 1 next we're going to type the word 9488 08:08:06,680 --> 08:08:13,040 while and then brackets and curly 9489 08:08:10,718 --> 08:08:15,120 brackets and then inside these curly 9490 08:08:13,040 --> 08:08:18,120 brackets we're going to put some code 9491 08:08:15,120 --> 08:08:22,520 that the computer will run over and over 9492 08:08:18,120 --> 08:08:24,160 just like a loop let's put the code 9493 08:08:22,520 --> 08:08:26,000 console. 9494 08:08:24,160 --> 08:08:29,558 log 9495 08:08:26,000 --> 08:08:33,280 I so this will display the variable I in 9496 08:08:29,558 --> 08:08:35,360 the console over and over again now 9497 08:08:33,280 --> 08:08:38,280 obviously we don't want to Loop this 9498 08:08:35,360 --> 08:08:41,040 forever so how do we get this Loop to 9499 08:08:38,280 --> 08:08:44,080 stop we can do that using the round 9500 08:08:41,040 --> 08:08:46,160 brackets up here inside these round 9501 08:08:44,080 --> 08:08:49,520 brackets we're going to put some code 9502 08:08:46,160 --> 08:08:54,000 that results in true or false for 9503 08:08:49,520 --> 08:08:58,478 example we can check if I is less than 9504 08:08:54,000 --> 08:09:01,920 or equal to 5 so the way this works is 9505 08:08:58,478 --> 08:09:05,718 that if this code results in true the 9506 08:09:01,920 --> 08:09:08,840 loop will keep going over and over once 9507 08:09:05,718 --> 08:09:11,920 this results in false the loop will stop 9508 08:09:08,840 --> 08:09:14,120 and it will go to the next line now you 9509 08:09:11,920 --> 08:09:17,840 may notice that this code will never 9510 08:09:14,120 --> 08:09:21,920 result in false because I is equal to 1 9511 08:09:17,840 --> 08:09:24,200 and 1 is always less than or equal to 5 9512 08:09:21,920 --> 08:09:28,160 so when does this actually become false 9513 08:09:24,200 --> 08:09:30,840 and Stop the Loop the answer is inside 9514 08:09:28,160 --> 08:09:34,878 this loop we're also going to update 9515 08:09:30,840 --> 08:09:39,680 this variable I so we'll type I and 9516 08:09:34,878 --> 08:09:43,080 we'll make it equal to itself plus 9517 08:09:39,680 --> 08:09:47,200 1 so now every time we Loop over this 9518 08:09:43,080 --> 08:09:50,440 code we're going to increase I by 1 and 9519 08:09:47,200 --> 08:09:53,398 eventually I will be greater than 5 this 9520 08:09:50,440 --> 08:09:55,638 will return false and it will stop the 9521 08:09:53,398 --> 08:09:58,240 loop if we 9522 08:09:55,638 --> 08:10:01,160 save and then go back back to the tab 9523 08:09:58,240 --> 08:10:04,398 for this file and I'll rearrange my 9524 08:10:01,160 --> 08:10:08,240 windows so we can see the console this 9525 08:10:04,398 --> 08:10:11,398 Loop makes the computer count from 1 to 9526 08:10:08,240 --> 08:10:14,160 5 so let's go over this code step by 9527 08:10:11,398 --> 08:10:18,120 step to make sure we understand how it 9528 08:10:14,160 --> 08:10:22,360 works first we create a variable I and 9529 08:10:18,120 --> 08:10:26,680 we make it equal to one and then we do a 9530 08:10:22,360 --> 08:10:29,638 loop so if this condition is true the 9531 08:10:26,680 --> 08:10:33,080 computer will keep running this code 9532 08:10:29,638 --> 08:10:36,600 over and over again so the first time it 9533 08:10:33,080 --> 08:10:39,600 Loops I equals 1 and then it displays 9534 08:10:36,600 --> 08:10:43,600 one in the console and then we increase 9535 08:10:39,600 --> 08:10:47,200 I to two now we go back to the top and 9536 08:10:43,600 --> 08:10:50,040 we check this code 2 is less than or 9537 08:10:47,200 --> 08:10:53,680 equal to 5 this is true so we're going 9538 08:10:50,040 --> 08:10:56,958 to run this code again this time I equal 9539 08:10:53,680 --> 08:11:01,360 2 and we display it in the console and 9540 08:10:56,958 --> 08:11:04,318 then we we increase I to three and now 3 9541 08:11:01,360 --> 08:11:07,478 is still less than or equal to 5 so this 9542 08:11:04,318 --> 08:11:10,360 is true which means we keep running this 9543 08:11:07,478 --> 08:11:15,200 code and eventually this is going to 9544 08:11:10,360 --> 08:11:17,558 increase I to six once I is six this 9545 08:11:15,200 --> 08:11:20,318 will become false which will cause the 9546 08:11:17,558 --> 08:11:24,440 loop to stop and then we go to the next 9547 08:11:20,318 --> 08:11:27,638 line so that's why it counts from 1 to 5 9548 08:11:24,440 --> 08:11:32,680 and then it stops at five 9549 08:11:27,638 --> 08:11:35,200 so this is called a while loop while 9550 08:11:32,680 --> 08:11:38,000 this condition is true it will keep 9551 08:11:35,200 --> 08:11:40,920 running this code over and over again 9552 08:11:38,000 --> 08:11:44,280 just like a loop as soon as this 9553 08:11:40,920 --> 08:11:47,200 condition is false it will stop the 9554 08:11:44,280 --> 08:11:50,040 loop all right now that we understand 9555 08:11:47,200 --> 08:11:53,638 how a while loop Works let's learn the 9556 08:11:50,040 --> 08:11:56,398 syntax rules to create a while loop we 9557 08:11:53,638 --> 08:11:59,798 type the word while and then round 9558 08:11:56,398 --> 08:12:02,200 brackets and curly brackets the code 9559 08:11:59,798 --> 08:12:05,638 inside the round brackets is called the 9560 08:12:02,200 --> 08:12:09,318 loop condition the code inside the curly 9561 08:12:05,638 --> 08:12:13,200 brackets is called the loop body the 9562 08:12:09,318 --> 08:12:15,760 variable I at the top is called the loop 9563 08:12:13,200 --> 08:12:18,920 variable every time we loop we're going 9564 08:12:15,760 --> 08:12:21,958 to increase the loop variable this is 9565 08:12:18,920 --> 08:12:24,280 known as the increment step the 9566 08:12:21,958 --> 08:12:26,680 increment step makes sure that the loop 9567 08:12:24,280 --> 08:12:29,000 ends at some point without this 9568 08:12:26,680 --> 08:12:31,718 increment step the loop would just keep 9569 08:12:29,000 --> 08:12:33,360 going forever this is known as an 9570 08:12:31,718 --> 08:12:38,958 infinite 9571 08:12:33,360 --> 08:12:42,760 Loop now remember that I = I + 1 has a 9572 08:12:38,958 --> 08:12:47,878 shortcut instead of this we can just do 9573 08:12:42,760 --> 08:12:50,680 I plus = 1 and remember we actually have 9574 08:12:47,878 --> 08:12:57,240 an even shorter shortcut for plus equal 9575 08:12:50,680 --> 08:13:00,160 1 instead of this we can also do I ++ 9576 08:12:57,240 --> 08:13:04,318 so this will increase the value of I by 9577 08:13:00,160 --> 08:13:07,440 one every time just like before so those 9578 08:13:04,318 --> 08:13:10,840 are the three major parts of a loop the 9579 08:13:07,440 --> 08:13:12,200 loop variable the loop condition and the 9580 08:13:10,840 --> 08:13:16,120 increment 9581 08:13:12,200 --> 08:13:17,638 step now each time we run this Loop this 9582 08:13:16,120 --> 08:13:21,040 is called an 9583 08:13:17,638 --> 08:13:25,000 iteration so in this Loop we did five 9584 08:13:21,040 --> 08:13:28,840 iterations we ran this code five times 9585 08:13:25,000 --> 08:13:31,160 and lastly Loops create a scope any 9586 08:13:28,840 --> 08:13:34,558 variable that we create between these 9587 08:13:31,160 --> 08:13:38,000 curly brackets will only exist inside 9588 08:13:34,558 --> 08:13:42,040 the curly brackets so that's a while 9589 08:13:38,000 --> 08:13:44,600 loop while this condition is true it 9590 08:13:42,040 --> 08:13:48,040 will keep running this code over and 9591 08:13:44,600 --> 08:13:51,360 over again once this condition becomes 9592 08:13:48,040 --> 08:13:52,680 false it will stop the loop and go to 9593 08:13:51,360 --> 08:13:55,718 the next 9594 08:13:52,680 --> 08:13:57,920 line so in JavaScript there's actually 9595 08:13:55,718 --> 08:14:00,638 two types of loops 9596 08:13:57,920 --> 08:14:03,240 the first type is a while loop which we 9597 08:14:00,638 --> 08:14:07,360 just learned now we're going to learn 9598 08:14:03,240 --> 08:14:09,958 another type of loop called a for Loop 9599 08:14:07,360 --> 08:14:14,318 to create a for Loop we'll create some 9600 08:14:09,958 --> 08:14:18,360 new lines and then down here we'll type 9601 08:14:14,318 --> 08:14:21,240 four and brackets and curly 9602 08:14:18,360 --> 08:14:24,520 brackets inside these brackets we're 9603 08:14:21,240 --> 08:14:27,600 going to type three pieces of code the 9604 08:14:24,520 --> 08:14:30,000 first piece of code is a loop variable 9605 08:14:27,600 --> 08:14:36,318 which is the same thing as this line up 9606 08:14:30,000 --> 08:14:40,000 here so we'll type let I equal one and 9607 08:14:36,318 --> 08:14:43,398 then semicolon space and we'll type a 9608 08:14:40,000 --> 08:14:48,040 second piece of code which is the loop 9609 08:14:43,398 --> 08:14:52,478 condition so here we'll type I less than 9610 08:14:48,040 --> 08:14:54,558 equal to 5 and then semicolon and space 9611 08:14:52,478 --> 08:14:57,840 and the last piece of code is the 9612 08:14:54,558 --> 08:15:01,718 increment step which is this line right 9613 08:14:57,840 --> 08:15:04,680 here so we'll type 9614 08:15:01,718 --> 08:15:06,398 i++ and between these curly brackets 9615 08:15:04,680 --> 08:15:09,638 we'll write the code that we want to 9616 08:15:06,398 --> 08:15:12,798 Loop over and over so let's just type 9617 08:15:09,638 --> 08:15:12,798 the code 9618 08:15:13,120 --> 08:15:19,718 console.log I and 9619 08:15:16,958 --> 08:15:22,680 save so this for Loop does the same 9620 08:15:19,718 --> 08:15:26,520 thing as the while loop above it counts 9621 08:15:22,680 --> 08:15:29,200 from 1 to 5 and if we look at this code 9622 08:15:26,520 --> 08:15:32,558 we can see that a for Loop is just a 9623 08:15:29,200 --> 08:15:35,238 shorter version of this while loop 9624 08:15:32,558 --> 08:15:38,080 instead of writing the loop variable 9625 08:15:35,238 --> 08:15:41,638 condition and increment step separately 9626 08:15:38,080 --> 08:15:44,920 we group all of them together at the top 9627 08:15:41,638 --> 08:15:48,360 so this Loop works the same way first we 9628 08:15:44,920 --> 08:15:51,440 create a loop variable and then as long 9629 08:15:48,360 --> 08:15:55,160 as this Loop condition is true we will 9630 08:15:51,440 --> 08:15:57,680 keep running this code over and over and 9631 08:15:55,160 --> 08:16:00,798 every time we run this code we're also 9632 08:15:57,680 --> 08:16:03,638 going to increase the loop variable by 9633 08:16:00,798 --> 08:16:07,318 one so now that we learned two ways of 9634 08:16:03,638 --> 08:16:10,280 doing a loop which one should we use if 9635 08:16:07,318 --> 08:16:13,238 we're doing a standard Loop for example 9636 08:16:10,280 --> 08:16:15,840 it has a loop variable a condition and 9637 08:16:13,238 --> 08:16:18,760 an increment step every time it's 9638 08:16:15,840 --> 08:16:21,638 recommended to use a for Loop because a 9639 08:16:18,760 --> 08:16:25,040 for Loop is shorter and more organized 9640 08:16:21,638 --> 08:16:28,000 than a while loop however if it's a 9641 08:16:25,040 --> 08:16:31,398 nonstandard loop then it might be better 9642 08:16:28,000 --> 08:16:34,520 to use a while loop so what's an example 9643 08:16:31,398 --> 08:16:37,318 of a non-standard loop so let's say we 9644 08:16:34,520 --> 08:16:39,958 want to Generate random numbers until we 9645 08:16:37,318 --> 08:16:43,958 get a number that's at least 9646 08:16:39,958 --> 08:16:46,120 0.5 to create this Loop we'll create a 9647 08:16:43,958 --> 08:16:48,318 variable that will store the random 9648 08:16:46,120 --> 08:16:49,878 number using 9649 08:16:48,318 --> 08:16:51,440 let 9650 08:16:49,878 --> 08:16:54,878 random 9651 08:16:51,440 --> 08:16:58,840 number and make it equal to zero to 9652 08:16:54,878 --> 08:17:02,920 start and then we'll do a while 9653 08:16:58,840 --> 08:17:05,760 loop brackets and curly brackets now 9654 08:17:02,920 --> 08:17:07,920 inside the loop condition we want to 9655 08:17:05,760 --> 08:17:10,238 keep running this Loop until random 9656 08:17:07,920 --> 08:17:13,718 number is at least 9657 08:17:10,238 --> 08:17:19,040 0.5 so we'll check if 9658 08:17:13,718 --> 08:17:22,958 random number is less than 9659 08:17:19,040 --> 08:17:25,398 0.5 because if it is less than 0.5 we 9660 08:17:22,958 --> 08:17:28,440 want to keep running this Loop until we 9661 08:17:25,398 --> 08:17:31,238 get a random number that's at least 9662 08:17:28,440 --> 08:17:33,120 0.5 and then inside this loop we're 9663 08:17:31,238 --> 08:17:36,440 going to generate a random number 9664 08:17:33,120 --> 08:17:41,520 between 0 and 1 and then save it in the 9665 08:17:36,440 --> 08:17:46,680 variable up here so we'll type random 9666 08:17:41,520 --> 08:17:49,120 number and make it equal to capital M 9667 08:17:46,680 --> 08:17:52,840 math. 9668 08:17:49,120 --> 08:17:56,200 random so if this random number is less 9669 08:17:52,840 --> 08:17:58,840 than 0.5 it will keep generating random 9670 08:17:56,200 --> 08:18:00,280 numbers until we get a number that is at 9671 08:17:58,840 --> 08:18:04,398 least 9672 08:18:00,280 --> 08:18:07,360 0.5 so at the bottom if we 9673 08:18:04,398 --> 08:18:08,510 console. log 9674 08:18:07,360 --> 08:18:09,718 random 9675 08:18:08,510 --> 08:18:11,878 [Music] 9676 08:18:09,718 --> 08:18:14,718 number and 9677 08:18:11,878 --> 08:18:16,360 save it will give us a random number 9678 08:18:14,718 --> 08:18:19,318 that's at least 9679 08:18:16,360 --> 08:18:22,600 0.5 so notice that this is a 9680 08:18:19,318 --> 08:18:25,160 non-standard loop it doesn't really have 9681 08:18:22,600 --> 08:18:27,920 a loop variable that we increase every 9682 08:18:25,160 --> 08:18:31,440 time and it also doesn't have the 9683 08:18:27,920 --> 08:18:34,478 increment step so in this situation when 9684 08:18:31,440 --> 08:18:37,000 we have a non-standard loop it's better 9685 08:18:34,478 --> 08:18:38,318 to use a while loop because they're a 9686 08:18:37,000 --> 08:18:41,238 bit more 9687 08:18:38,318 --> 08:18:43,840 flexible all right now that we learned 9688 08:18:41,238 --> 08:18:46,398 the basics of Loops let's do some 9689 08:18:43,840 --> 08:18:49,280 practical examples we're going to learn 9690 08:18:46,398 --> 08:18:51,760 a technique called looping through an 9691 08:18:49,280 --> 08:18:54,680 array this means we're going to go 9692 08:18:51,760 --> 08:18:57,440 through each value of an array one by 9693 08:18:54,680 --> 08:19:00,680 one and do something with each each 9694 08:18:57,440 --> 08:19:04,040 value so first let's comment out the 9695 08:19:00,680 --> 08:19:09,238 previous code using 9696 08:19:04,040 --> 08:19:12,478 slashstar and star slash at the bottom 9697 08:19:09,238 --> 08:19:18,558 let's create an array of strings so 9698 08:19:12,478 --> 08:19:20,920 we'll create a variable const to do list 9699 08:19:18,558 --> 08:19:24,200 equal to an 9700 08:19:20,920 --> 08:19:27,840 array and inside this array we'll have 9701 08:19:24,200 --> 08:19:32,840 the string make 9702 08:19:27,840 --> 08:19:35,080 dinner another string wash 9703 08:19:32,840 --> 08:19:38,238 dishes and another 9704 08:19:35,080 --> 08:19:41,200 string watch 9705 08:19:38,238 --> 08:19:44,680 YouTube now let's take each value in 9706 08:19:41,200 --> 08:19:47,680 this array and display it in the console 9707 08:19:44,680 --> 08:19:50,478 to do that we're going to run a loop and 9708 08:19:47,680 --> 08:19:54,000 this Loop is going to count from zero to 9709 08:19:50,478 --> 08:19:56,160 the last index in the array so remember 9710 08:19:54,000 --> 08:19:59,360 that the index is the position of a 9711 08:19:56,160 --> 08:20:03,120 value in the array the first value is 9712 08:19:59,360 --> 08:20:07,238 index zero the second value is index one 9713 08:20:03,120 --> 08:20:11,878 third value is index 2 and so on So 9714 08:20:07,238 --> 08:20:12,878 Below this let's type four brackets and 9715 08:20:11,878 --> 08:20:15,680 curly 9716 08:20:12,878 --> 08:20:18,680 brackets inside these brackets we're 9717 08:20:15,680 --> 08:20:20,958 going to create the loop variable so 9718 08:20:18,680 --> 08:20:27,680 here we're going to count the 9719 08:20:20,958 --> 08:20:31,200 indexes let index equals zero semicolon 9720 08:20:27,680 --> 08:20:34,238 space and we'll do the loop condition so 9721 08:20:31,200 --> 08:20:38,040 we'll keep looping as long as the index 9722 08:20:34,238 --> 08:20:40,520 is less than or equal to the last index 9723 08:20:38,040 --> 08:20:43,440 so remember that the index starts from 9724 08:20:40,520 --> 08:20:46,120 zero this means that the last index in 9725 08:20:43,440 --> 08:20:50,798 an array will be the number of values in 9726 08:20:46,120 --> 08:20:52,440 the array minus one or the array. length 9727 08:20:50,798 --> 08:20:57,360 minus 9728 08:20:52,440 --> 08:20:59,878 one to do list do 9729 08:20:57,360 --> 08:21:03,958 length minus 9730 08:20:59,878 --> 08:21:06,840 1 and here we'll add another semicolon 9731 08:21:03,958 --> 08:21:11,638 and we'll add the increment step which 9732 08:21:06,840 --> 08:21:14,878 is index Plus+ so we'll increase it by 9733 08:21:11,638 --> 08:21:17,440 one every time now inside these curly 9734 08:21:14,878 --> 08:21:22,638 brackets we can do something for each 9735 08:21:17,440 --> 08:21:27,080 index of this array for now let's just 9736 08:21:22,638 --> 08:21:28,478 console. log this index 9737 08:21:27,080 --> 08:21:33,040 and 9738 08:21:28,478 --> 08:21:36,238 save so this will count from 0 to two 9739 08:21:33,040 --> 08:21:39,920 which is exactly all the indexes in this 9740 08:21:36,238 --> 08:21:43,120 array and now using this index we can 9741 08:21:39,920 --> 08:21:47,520 also access the value at this 9742 08:21:43,120 --> 08:21:52,000 index so instead of just the index here 9743 08:21:47,520 --> 08:21:56,760 we can do to-do list and then square 9744 08:21:52,000 --> 08:21:58,680 brackets and between the brackets index 9745 08:21:56,760 --> 08:22:02,318 this will take whatever the index 9746 08:21:58,680 --> 08:22:06,040 variable is and access that index in the 9747 08:22:02,318 --> 08:22:09,520 array so if the index variable is zero 9748 08:22:06,040 --> 08:22:13,080 this will access to-do list index0 which 9749 08:22:09,520 --> 08:22:16,120 is the first value when the index is one 9750 08:22:13,080 --> 08:22:19,040 it will access to-do list index one 9751 08:22:16,120 --> 08:22:22,878 which is the second value and so 9752 08:22:19,040 --> 08:22:26,958 on so for now let's save the value in a 9753 08:22:22,878 --> 08:22:31,280 variable using const value 9754 08:22:26,958 --> 08:22:33,718 and make it equal to to-do list bracket 9755 08:22:31,280 --> 08:22:38,558 index and now let's 9756 08:22:33,718 --> 08:22:41,600 console.log the value instead if we 9757 08:22:38,558 --> 08:22:45,318 save this will Loop through each index 9758 08:22:41,600 --> 08:22:47,718 of the array get the value at each index 9759 08:22:45,318 --> 08:22:50,558 and then display each value which is 9760 08:22:47,718 --> 08:22:53,160 what we see in the console so this 9761 08:22:50,558 --> 08:22:56,680 technique is called looping through an 9762 08:22:53,160 --> 08:22:59,360 array we use a loop to go through each 9763 08:22:56,680 --> 08:23:01,798 value of an array and then we can do 9764 08:22:59,360 --> 08:23:04,280 something with each 9765 08:23:01,798 --> 08:23:06,600 value now I'm going to show you a few 9766 08:23:04,280 --> 08:23:09,760 shortcuts we can use when looping 9767 08:23:06,600 --> 08:23:12,798 through an array instead of checking if 9768 08:23:09,760 --> 08:23:16,080 index is less than or equal to to-do 9769 08:23:12,798 --> 08:23:21,200 list. length minus one we can shorten 9770 08:23:16,080 --> 08:23:24,878 this to index less than to-do list. 9771 08:23:21,200 --> 08:23:27,718 length so this check does the same thing 9772 08:23:24,878 --> 08:23:30,638 as before the loop Loop will stop at 9773 08:23:27,718 --> 08:23:32,120 index equal length minus one but it 9774 08:23:30,638 --> 08:23:35,478 saves us some 9775 08:23:32,120 --> 08:23:38,360 typing another shortcut is instead of 9776 08:23:35,478 --> 08:23:41,080 naming the loop variable index we can 9777 08:23:38,360 --> 08:23:44,878 shorten this to 9778 08:23:41,080 --> 08:23:47,920 I and we'll shorten this as well and 9779 08:23:44,878 --> 08:23:52,718 this as well and that's why we often 9780 08:23:47,920 --> 08:23:55,360 name the loop variable I I means 9781 08:23:52,718 --> 08:23:57,600 index next we're going to learn a 9782 08:23:55,360 --> 08:24:00,680 technique that we can combine with 9783 08:23:57,600 --> 08:24:04,360 looping through an array called the 9784 08:24:00,680 --> 08:24:08,160 accumulator pattern so first let's 9785 08:24:04,360 --> 08:24:10,958 comment out the previous code using 9786 08:24:08,160 --> 08:24:14,040 slashstar and star 9787 08:24:10,958 --> 08:24:16,718 slash and at the 9788 08:24:14,040 --> 08:24:19,878 bottom let's say we have an array of 9789 08:24:16,718 --> 08:24:22,958 numbers like this how do we calculate 9790 08:24:19,878 --> 08:24:26,558 the total of all the numbers in the 9791 08:24:22,958 --> 08:24:31,080 array let's set up this problem let's 9792 08:24:26,558 --> 08:24:37,080 say we have an array of numbers const 9793 08:24:31,080 --> 08:24:39,040 nums equals array and then the numbers 1 9794 08:24:37,080 --> 08:24:42,280 1 and 9795 08:24:39,040 --> 08:24:44,840 3 and we want to calculate the total of 9796 08:24:42,280 --> 08:24:47,080 all the numbers in this array to 9797 08:24:44,840 --> 08:24:50,200 calculate the total we'll learn the 9798 08:24:47,080 --> 08:24:52,760 accumulator pattern the first step of 9799 08:24:50,200 --> 08:24:56,120 the accumulator pattern is to create a 9800 08:24:52,760 --> 08:25:01,200 variable to store the result so below 9801 08:24:56,120 --> 08:25:04,638 this let's create a variable const total 9802 08:25:01,200 --> 08:25:07,318 and we'll make it equal to zero and the 9803 08:25:04,638 --> 08:25:12,920 next step is to Loop through this array 9804 08:25:07,318 --> 08:25:16,398 and update the result so down here four 9805 08:25:12,920 --> 08:25:19,040 brackets and curly brackets and inside 9806 08:25:16,398 --> 08:25:25,318 the round brackets we'll create the loop 9807 08:25:19,040 --> 08:25:28,238 variable let I or index equals z 9808 08:25:25,318 --> 08:25:34,360 semicolon space and we'll create the 9809 08:25:28,238 --> 08:25:37,280 loop condition I less than nums do 9810 08:25:34,360 --> 08:25:40,718 length and then semicolon and the 9811 08:25:37,280 --> 08:25:44,440 increment step 9812 08:25:40,718 --> 08:25:48,200 i++ so as we learned earlier this Loop 9813 08:25:44,440 --> 08:25:50,160 will go through each index of this array 9814 08:25:48,200 --> 08:25:53,440 and now between these curly 9815 08:25:50,160 --> 08:25:57,040 brackets first let's use the index to 9816 08:25:53,440 --> 08:26:00,760 get each value so we'll type 9817 08:25:57,040 --> 08:26:04,440 nums and then square brackets and access 9818 08:26:00,760 --> 08:26:06,958 the index I and let's save this in a 9819 08:26:04,440 --> 08:26:11,520 variable using 9820 08:26:06,958 --> 08:26:14,520 const num equals this 9821 08:26:11,520 --> 08:26:18,478 value and finally we're going to add 9822 08:26:14,520 --> 08:26:22,120 each number to the total we can type 9823 08:26:18,478 --> 08:26:26,238 total and make it equal to 9824 08:26:22,120 --> 08:26:28,958 itself plus each number and remember 9825 08:26:26,238 --> 08:26:31,160 remember we have a shortcut for this 9826 08:26:28,958 --> 08:26:34,238 which is plus 9827 08:26:31,160 --> 08:26:36,920 equals and finally since we're changing 9828 08:26:34,238 --> 08:26:39,600 this total variable we have to change 9829 08:26:36,920 --> 08:26:43,120 from const to 9830 08:26:39,600 --> 08:26:45,120 let all right so after this Loop we 9831 08:26:43,120 --> 08:26:49,160 should have added each number in the 9832 08:26:45,120 --> 08:26:50,680 array to the total so at the bottom 9833 08:26:49,160 --> 08:26:54,280 let's 9834 08:26:50,680 --> 08:26:57,520 console. log the 9835 08:26:54,280 --> 08:27:00,398 total and Save 9836 08:26:57,520 --> 08:27:03,040 and it will display five which is the 9837 08:27:00,398 --> 08:27:06,760 exact total of all the numbers in this 9838 08:27:03,040 --> 08:27:09,200 array so that's the accumulator pattern 9839 08:27:06,760 --> 08:27:11,718 we first create a variable to store the 9840 08:27:09,200 --> 08:27:16,000 result and then we Loop through the 9841 08:27:11,718 --> 08:27:18,478 array and update the result so notice 9842 08:27:16,000 --> 08:27:21,760 that as we Loop through the array we're 9843 08:27:18,478 --> 08:27:24,280 adding or accumulating the results into 9844 08:27:21,760 --> 08:27:27,318 this variable and that's why this is 9845 08:27:24,280 --> 08:27:29,638 called the accumulator pattern pattern 9846 08:27:27,318 --> 08:27:31,558 and we call this variable the 9847 08:27:29,638 --> 08:27:34,398 accumulator 9848 08:27:31,558 --> 08:27:37,680 variable all right let's do another 9849 08:27:34,398 --> 08:27:39,718 example using the accumulator pattern 9850 08:27:37,680 --> 08:27:42,398 let's say we're given this array of 9851 08:27:39,718 --> 08:27:45,478 numbers again and we want to create a 9852 08:27:42,398 --> 08:27:50,040 copy of the array where each number is 9853 08:27:45,478 --> 08:27:54,718 doubled so if the array is 1 13 we're 9854 08:27:50,040 --> 08:27:58,200 going to create a new array with 2 9855 08:27:54,718 --> 08:28:00,958 26 so as at the bottom we'll create some 9856 08:27:58,200 --> 08:28:03,638 new lines and use the accumulator 9857 08:28:00,958 --> 08:28:08,000 pattern the first step is to create a 9858 08:28:03,638 --> 08:28:11,080 variable to store the result let's type 9859 08:28:08,000 --> 08:28:12,798 const and let's name this 9860 08:28:11,080 --> 08:28:15,958 nums 9861 08:28:12,798 --> 08:28:18,878 doubled and we'll make it equal to an 9862 08:28:15,958 --> 08:28:21,080 array this time so notice in the 9863 08:28:18,878 --> 08:28:24,558 previous example the accumulator 9864 08:28:21,080 --> 08:28:27,920 variable was a number in this example 9865 08:28:24,558 --> 08:28:30,718 the accumulator variable is an array so 9866 08:28:27,920 --> 08:28:33,398 we can use different types of values to 9867 08:28:30,718 --> 08:28:36,478 accumulate the result it all depends on 9868 08:28:33,398 --> 08:28:38,798 the problem we're trying to solve so now 9869 08:28:36,478 --> 08:28:42,238 the next step is to Loop through this 9870 08:28:38,798 --> 08:28:46,920 array and update the result at the 9871 08:28:42,238 --> 08:28:48,478 bottom we'll create a four Loop brackets 9872 08:28:46,920 --> 08:28:51,398 and curly 9873 08:28:48,478 --> 08:28:56,440 brackets inside the round brackets we'll 9874 08:28:51,398 --> 08:29:00,080 create the loop variable let I equal 9875 08:28:56,440 --> 08:29:05,558 zero and we'll create the loop condition 9876 08:29:00,080 --> 08:29:08,798 I less than nums do length and then the 9877 08:29:05,558 --> 08:29:12,398 increment step 9878 08:29:08,798 --> 08:29:14,920 i++ and now between the curly brackets 9879 08:29:12,398 --> 08:29:21,840 we're going to get the number at each 9880 08:29:14,920 --> 08:29:25,440 index so const num equals nums square 9881 08:29:21,840 --> 08:29:26,718 bracket I and then we're going to update 9882 08:29:25,440 --> 08:29:29,440 the result 9883 08:29:26,718 --> 08:29:31,920 so down here we're going to add this 9884 08:29:29,440 --> 08:29:35,238 number doubled to this 9885 08:29:31,920 --> 08:29:38,840 array so remember to add a value to an 9886 08:29:35,238 --> 08:29:40,558 array we can use the method push so 9887 08:29:38,840 --> 08:29:42,318 we'll type 9888 08:29:40,558 --> 08:29:46,000 nums 9889 08:29:42,318 --> 08:29:48,520 doubled do push 9890 08:29:46,000 --> 08:29:55,200 brackets and we're going to add to this 9891 08:29:48,520 --> 08:29:56,920 array the number num multiplied by 2 and 9892 08:29:55,200 --> 08:29:58,360 now at the bottom 9893 08:29:56,920 --> 08:30:03,638 let's 9894 08:29:58,360 --> 08:30:06,280 console. log the result nums 9895 08:30:03,638 --> 08:30:09,520 doubled and 9896 08:30:06,280 --> 08:30:12,920 save so this second Loop will create an 9897 08:30:09,520 --> 08:30:14,760 array where each number is doubled so 9898 08:30:12,920 --> 08:30:17,840 that's how we solve this 9899 08:30:14,760 --> 08:30:19,958 example all right now that we understand 9900 08:30:17,840 --> 08:30:22,798 how to Loop through an array and the 9901 08:30:19,958 --> 08:30:26,680 accumulator pattern let's go 9902 08:30:22,798 --> 08:30:29,440 back and we'll create the second version 9903 08:30:26,680 --> 08:30:32,000 of our to-do list so when we add 9904 08:30:29,440 --> 08:30:36,398 something to the list it will appear on 9905 08:30:32,000 --> 08:30:39,638 the page let's go to our code editor and 9906 08:30:36,398 --> 08:30:42,440 we'll open 11 Todo list. 9907 08:30:39,638 --> 08:30:46,238 HTML and we're also going to open the 9908 08:30:42,440 --> 08:30:48,360 Javascript file so it's this one and for 9909 08:30:46,238 --> 08:30:51,360 the video I'm going to drag it to the 9910 08:30:48,360 --> 08:30:55,080 bottom of my code editor so that I can 9911 08:30:51,360 --> 08:30:56,920 see the files side by side so I'll close 9912 08:30:55,080 --> 08:31:00,478 this for now 9913 08:30:56,920 --> 08:31:04,680 and I'll rearrange my windows a 9914 08:31:00,478 --> 08:31:07,840 bit let's open the tab for this project 9915 08:31:04,680 --> 08:31:10,440 so we'll review what we have so far we 9916 08:31:07,840 --> 08:31:16,638 can click in this text box and type a 9917 08:31:10,440 --> 08:31:19,600 to-do like make dinner and click add and 9918 08:31:16,638 --> 08:31:22,680 it will add this to-do to this array 9919 08:31:19,600 --> 08:31:27,520 which is this array right here we can 9920 08:31:22,680 --> 08:31:31,160 type another to-do like wash dishes and 9921 08:31:27,520 --> 08:31:33,958 click add and it will add it to the end 9922 08:31:31,160 --> 08:31:37,160 of the array so now that we have this 9923 08:31:33,958 --> 08:31:40,318 array of to-dos we want to display it on 9924 08:31:37,160 --> 08:31:42,200 the page to do that we're going to Loop 9925 08:31:40,318 --> 08:31:46,280 through this array and we're going to 9926 08:31:42,200 --> 08:31:49,440 create some HTML code for each to-do and 9927 08:31:46,280 --> 08:31:53,280 then put that code on the web page using 9928 08:31:49,440 --> 08:31:56,478 the Dom so first let's Loop through this 9929 08:31:53,280 --> 08:32:00,718 array to make this easier let's make a 9930 08:31:56,478 --> 08:32:05,520 copy of this array so right click 9931 08:32:00,718 --> 08:32:07,920 copy and then here we'll remove this and 9932 08:32:05,520 --> 08:32:10,680 rightclick and 9933 08:32:07,920 --> 08:32:15,200 paste so this will give us some default 9934 08:32:10,680 --> 08:32:19,238 values to work with so at the top let's 9935 08:32:15,200 --> 08:32:24,200 Loop through this array using a for Loop 9936 08:32:19,238 --> 08:32:26,600 so we'll type four brackets and curly 9937 08:32:24,200 --> 08:32:29,280 brackets and then in inside the round 9938 08:32:26,600 --> 08:32:35,280 brackets we're going to create the loop 9939 08:32:29,280 --> 08:32:39,478 variable let I or index equal zero 9940 08:32:35,280 --> 08:32:43,798 semicolon and the loop condition I less 9941 08:32:39,478 --> 08:32:47,280 than too list do 9942 08:32:43,798 --> 08:32:49,920 length semicolon and then the increment 9943 08:32:47,280 --> 08:32:53,558 step 9944 08:32:49,920 --> 08:32:57,280 i++ so this will Loop through each index 9945 08:32:53,558 --> 08:33:00,680 of this array and then inside the curly 9946 08:32:57,280 --> 08:33:03,600 brackets let's get the string for each 9947 08:33:00,680 --> 08:33:08,878 index so we'll create a variable to save 9948 08:33:03,600 --> 08:33:15,080 it const to-do and we'll make it equal 9949 08:33:08,878 --> 08:33:18,680 to to-do list square brackets and 9950 08:33:15,080 --> 08:33:22,520 I and now for each to-do we're going to 9951 08:33:18,680 --> 08:33:27,440 create some HTML code so we'll create a 9952 08:33:22,520 --> 08:33:30,958 new line and create a variable const 9953 08:33:27,440 --> 08:33:35,398 HTML and make it equal and we'll create 9954 08:33:30,958 --> 08:33:36,398 some HTML code using this to-do using a 9955 08:33:35,398 --> 08:33:39,440 template 9956 08:33:36,398 --> 08:33:43,120 string and then inside we'll create a 9957 08:33:39,440 --> 08:33:46,680 paragraph element so less than P greater 9958 08:33:43,120 --> 08:33:49,318 than and then less than SL P greater 9959 08:33:46,680 --> 08:33:52,398 than inside this paragraph we're going 9960 08:33:49,318 --> 08:33:56,440 to insert this to-do so we'll type 9961 08:33:52,398 --> 08:33:59,398 dollar and curly brackets and 9962 08:33:56,440 --> 08:34:02,958 Todo all right so now we're looping 9963 08:33:59,398 --> 08:34:06,360 through this array and for each to-do we 9964 08:34:02,958 --> 08:34:09,000 created some HTML code and now we're 9965 08:34:06,360 --> 08:34:12,798 going to combine all of this HTML code 9966 08:34:09,000 --> 08:34:15,360 together and put it on the web page to 9967 08:34:12,798 --> 08:34:18,878 combine this HTML together we're going 9968 08:34:15,360 --> 08:34:22,040 to use the accumulator pattern so at the 9969 08:34:18,878 --> 08:34:28,120 top of the loop we'll create a variable 9970 08:34:22,040 --> 08:34:29,798 to store the result let to do list 9971 08:34:28,120 --> 08:34:32,638 Capital 9972 08:34:29,798 --> 08:34:36,318 HTML and we'll make it equal to the 9973 08:34:32,638 --> 08:34:38,520 empty string at the start and then as we 9974 08:34:36,318 --> 08:34:43,280 Loop through the array we're going to 9975 08:34:38,520 --> 08:34:46,440 add this HTML to the variable up here so 9976 08:34:43,280 --> 08:34:50,798 let's create a new line and we'll do 9977 08:34:46,440 --> 08:34:54,160 to-do list Capital 9978 08:34:50,798 --> 08:34:58,238 HTML plus equals 9979 08:34:54,160 --> 08:35:02,360 HTML to combine all of this HTML code 9980 08:34:58,238 --> 08:35:08,760 together so now after the loop let's 9981 08:35:02,360 --> 08:35:13,238 console do log to do list 9982 08:35:08,760 --> 08:35:16,600 HTML to see what we get if we 9983 08:35:13,238 --> 08:35:18,840 save we took this array of strings 9984 08:35:16,600 --> 08:35:21,360 generated paragraph elements for each 9985 08:35:18,840 --> 08:35:23,958 string and then combine them all 9986 08:35:21,360 --> 08:35:27,798 together so now that we have all this 9987 08:35:23,958 --> 08:35:31,398 HTML we can put it on the page using the 9988 08:35:27,798 --> 08:35:33,360 Dom so first let's create an element 9989 08:35:31,398 --> 08:35:38,638 where we can put this 9990 08:35:33,360 --> 08:35:41,200 HTML so inside the HTML file after the 9991 08:35:38,638 --> 08:35:45,000 button we're going to create a different 9992 08:35:41,200 --> 08:35:49,200 type of element called a div element so 9993 08:35:45,000 --> 08:35:52,558 we'll type less than div greater 9994 08:35:49,200 --> 08:35:55,238 than so the div element represents a 9995 08:35:52,558 --> 08:35:58,478 container its purpose is to contain 9996 08:35:55,238 --> 08:36:00,840 other elements inside like paragraphs 9997 08:35:58,478 --> 08:36:03,920 buttons and even other 9998 08:36:00,840 --> 08:36:06,798 divs so in this situation we can't 9999 08:36:03,920 --> 08:36:10,398 actually use a paragraph element because 10000 08:36:06,798 --> 08:36:13,120 in HTML we cannot have a paragraph 10001 08:36:10,398 --> 08:36:16,440 inside another paragraph so that's why 10002 08:36:13,120 --> 08:36:18,958 we have to use a div element here for 10003 08:36:16,440 --> 08:36:22,040 now let's put some text inside this 10004 08:36:18,958 --> 08:36:26,760 element so we can learn more about it 10005 08:36:22,040 --> 08:36:29,478 we'll type text and Save 10006 08:36:26,760 --> 08:36:32,520 so like the paragraph element the div 10007 08:36:29,478 --> 08:36:35,638 element is a block element that means it 10008 08:36:32,520 --> 08:36:38,040 will take up an entire line by itself 10009 08:36:35,638 --> 08:36:41,600 and that's why it appears below the text 10010 08:36:38,040 --> 08:36:44,318 box and the button now one difference 10011 08:36:41,600 --> 08:36:47,558 between a div and a paragraph is that a 10012 08:36:44,318 --> 08:36:50,878 paragraph adds extra space above and 10013 08:36:47,558 --> 08:36:54,958 below the paragraph while a div does not 10014 08:36:50,878 --> 08:36:57,120 add any extra space above or below so it 10015 08:36:54,958 --> 08:37:00,080 doesn't have any default 10016 08:36:57,120 --> 08:37:04,878 styling all right so now that we learned 10017 08:37:00,080 --> 08:37:07,878 the div let's put this HTML code inside 10018 08:37:04,878 --> 08:37:11,040 this div first we need to get this 10019 08:37:07,878 --> 08:37:14,958 element into our JavaScript and to do 10020 08:37:11,040 --> 08:37:18,600 that we'll add a class to this element 10021 08:37:14,958 --> 08:37:22,600 class equals 10022 08:37:18,600 --> 08:37:25,878 js- too Das 10023 08:37:22,600 --> 08:37:28,638 list and then down here let's scroll 10024 08:37:25,878 --> 08:37:31,798 down and we'll put this div element 10025 08:37:28,638 --> 08:37:37,080 inside our JavaScript we can do that 10026 08:37:31,798 --> 08:37:40,840 using document do query 10027 08:37:37,080 --> 08:37:44,120 selector brackets and a string and we'll 10028 08:37:40,840 --> 08:37:47,000 type a DOT to select a class and we'll 10029 08:37:44,120 --> 08:37:49,160 select the class 10030 08:37:47,000 --> 08:37:51,000 js- 10031 08:37:49,160 --> 08:37:54,318 too- 10032 08:37:51,000 --> 08:37:56,558 list so this will get this div element 10033 08:37:54,318 --> 08:37:59,798 and put it in inside our 10034 08:37:56,558 --> 08:38:05,440 JavaScript and then every HTML element 10035 08:37:59,798 --> 08:38:09,760 has a property called dot inner Capital 10036 08:38:05,440 --> 08:38:12,798 HTML which controls the HTML inside this 10037 08:38:09,760 --> 08:38:15,958 element and now to put the to-do list 10038 08:38:12,798 --> 08:38:17,878 HTML into this div element we're going 10039 08:38:15,958 --> 08:38:21,080 to make the inner 10040 08:38:17,878 --> 08:38:25,840 HTML equal to the 10041 08:38:21,080 --> 08:38:28,280 to-do list HTML 10042 08:38:25,840 --> 08:38:32,638 and now let's save this 10043 08:38:28,280 --> 08:38:36,798 file and also save the HTML 10044 08:38:32,638 --> 08:38:39,600 file so this code will take the HTML 10045 08:38:36,798 --> 08:38:42,200 that we created using the loop and put 10046 08:38:39,600 --> 08:38:45,520 it into the div element and that's how 10047 08:38:42,200 --> 08:38:48,638 we make them show up on the page so now 10048 08:38:45,520 --> 08:38:53,638 let's try adding another to-do we'll 10049 08:38:48,638 --> 08:38:56,238 type watch YouTube and click 10050 08:38:53,638 --> 08:38:59,080 add and unfortunately 10051 08:38:56,238 --> 08:39:01,760 this new Todo doesn't show up on the 10052 08:38:59,080 --> 08:39:05,680 page and that's because when we click 10053 08:39:01,760 --> 08:39:08,120 add it will run this add Todo function 10054 08:39:05,680 --> 08:39:11,280 and update the list but we didn't 10055 08:39:08,120 --> 08:39:15,120 actually update the HTML on the 10056 08:39:11,280 --> 08:39:18,600 page so to update the HTML we need to 10057 08:39:15,120 --> 08:39:22,520 run all this code again every time we 10058 08:39:18,600 --> 08:39:25,920 add a too so let's create a function for 10059 08:39:22,520 --> 08:39:30,040 this code so we can reuse it so at the 10060 08:39:25,920 --> 08:39:33,318 top we'll create a 10061 08:39:30,040 --> 08:39:34,958 function and let's name this 10062 08:39:33,318 --> 08:39:39,840 render 10063 08:39:34,958 --> 08:39:43,200 to-do list brackets and curly 10064 08:39:39,840 --> 08:39:46,280 brackets so the word render means to 10065 08:39:43,200 --> 08:39:49,760 display something on the page so we'll 10066 08:39:46,280 --> 08:39:53,000 put all of this code into this function 10067 08:39:49,760 --> 08:39:56,318 so we can delete this curly bracket and 10068 08:39:53,000 --> 08:39:59,238 then at the end we'll type a closed 10069 08:39:56,318 --> 08:40:02,680 curly bracket and let's reformat this 10070 08:39:59,238 --> 08:40:05,440 code by selecting these lines and then 10071 08:40:02,680 --> 08:40:09,638 press tab to add an 10072 08:40:05,440 --> 08:40:12,558 indent so now let's use this function at 10073 08:40:09,638 --> 08:40:15,958 the start we'll display this to-do list 10074 08:40:12,558 --> 08:40:19,238 on the page so we'll call this function 10075 08:40:15,958 --> 08:40:22,440 by typing render 10076 08:40:19,238 --> 08:40:26,160 to-do list and 10077 08:40:22,440 --> 08:40:29,478 brackets and then let's scroll down 10078 08:40:26,160 --> 08:40:31,440 every time we add a to-do to the list 10079 08:40:29,478 --> 08:40:34,760 we're also going to display the list 10080 08:40:31,440 --> 08:40:41,040 again so at the bottom 10081 08:40:34,760 --> 08:40:42,160 render to do list and brackets again so 10082 08:40:41,040 --> 08:40:44,958 if we 10083 08:40:42,160 --> 08:40:48,000 save at the start it will display the 10084 08:40:44,958 --> 08:40:53,520 list on the page and then if we add it 10085 08:40:48,000 --> 08:40:57,238 to the list like watch YouTube and click 10086 08:40:53,520 --> 08:41:00,040 add it will add it to the list and also 10087 08:40:57,238 --> 08:41:02,398 display it on the page so that's how we 10088 08:41:00,040 --> 08:41:03,440 create the second version of the to-do 10089 08:41:02,398 --> 08:41:06,878 list 10090 08:41:03,440 --> 08:41:09,718 project by the way if we scroll up to 10091 08:41:06,878 --> 08:41:12,478 the render Todo list function this 10092 08:41:09,718 --> 08:41:16,040 technique is called generating the 10093 08:41:12,478 --> 08:41:19,718 HTML instead of writing all the HTML by 10094 08:41:16,040 --> 08:41:22,680 hand up here we looped through an array 10095 08:41:19,718 --> 08:41:25,360 and we generated the HTML using 10096 08:41:22,680 --> 08:41:26,958 JavaScript 10097 08:41:25,360 --> 08:41:29,238 so when creating websites with 10098 08:41:26,958 --> 08:41:33,120 JavaScript we usually follow a 10099 08:41:29,238 --> 08:41:36,398 three-step process first we save the 10100 08:41:33,120 --> 08:41:37,478 data in this case we saved our data as 10101 08:41:36,398 --> 08:41:41,398 an 10102 08:41:37,478 --> 08:41:45,798 array next we use the data to generate 10103 08:41:41,398 --> 08:41:48,478 the HTML which we did here and finally 10104 08:41:45,798 --> 08:41:50,398 we make the website interactive which 10105 08:41:48,478 --> 08:41:53,600 we'll do later in this 10106 08:41:50,398 --> 08:41:55,878 lesson this is the main idea of 10107 08:41:53,600 --> 08:41:58,398 JavaScript and we follow follow this 10108 08:41:55,878 --> 08:42:00,920 overall process when creating 10109 08:41:58,398 --> 08:42:03,000 websites the rock paper scissors project 10110 08:42:00,920 --> 08:42:06,238 from the previous lessons was a bit 10111 08:42:03,000 --> 08:42:08,958 different because all the HTML we needed 10112 08:42:06,238 --> 08:42:10,080 was already there we just had to modify 10113 08:42:08,958 --> 08:42:12,318 it with 10114 08:42:10,080 --> 08:42:15,360 JavaScript so this process we're using 10115 08:42:12,318 --> 08:42:18,000 for the to-do list is one step above 10116 08:42:15,360 --> 08:42:20,280 what we did for rock paper scissors we 10117 08:42:18,000 --> 08:42:23,558 need to generate some 10118 08:42:20,280 --> 08:42:25,840 HTML and this is a standard process for 10119 08:42:23,558 --> 08:42:28,040 creating websites we're going to use 10120 08:42:25,840 --> 08:42:30,798 this process for the to-do list and 10121 08:42:28,040 --> 08:42:34,920 later the Amazon Project as well as 10122 08:42:30,798 --> 08:42:34,920 websites that you build Beyond this 10123 08:42:35,080 --> 08:42:40,360 course let's take a break before 10124 08:42:37,398 --> 08:42:42,200 continuing with the rest of this lesson 10125 08:42:40,360 --> 08:42:46,958 here are some exercises that will help 10126 08:42:42,200 --> 08:42:46,958 you practice what we learned so far 10127 08:43:07,398 --> 08:43:12,478 now let's create the final version of 10128 08:43:09,398 --> 08:43:14,238 the to-do list we'll go back to 10129 08:43:12,478 --> 08:43:18,280 superimple 10130 08:43:14,238 --> 08:43:21,000 dodev and then here we'll scroll down 10131 08:43:18,280 --> 08:43:24,798 and we'll create this final version so 10132 08:43:21,000 --> 08:43:26,200 in this version we can type a too like 10133 08:43:24,798 --> 08:43:29,520 make 10134 08:43:26,200 --> 08:43:33,600 dinner and we can also add a due 10135 08:43:29,520 --> 08:43:37,318 date and when we click add each to-do 10136 08:43:33,600 --> 08:43:39,760 will now have a name and a due date and 10137 08:43:37,318 --> 08:43:43,760 they will also have a delete button that 10138 08:43:39,760 --> 08:43:46,080 we can click to remove it from the list 10139 08:43:43,760 --> 08:43:49,440 so let's build this out one feature at a 10140 08:43:46,080 --> 08:43:52,718 time we'll start by creating this delete 10141 08:43:49,440 --> 08:43:55,558 button we'll go into our code and 10142 08:43:52,718 --> 08:43:58,798 remember we're generating the HTML 10143 08:43:55,558 --> 08:44:02,680 for the to-do list so for each value in 10144 08:43:58,798 --> 08:44:05,238 the array we create a paragraph element 10145 08:44:02,680 --> 08:44:09,080 so to add a delete button we just have 10146 08:44:05,238 --> 08:44:12,638 to add a delete button inside here so 10147 08:44:09,080 --> 08:44:16,760 inside the paragraph we'll create a less 10148 08:44:12,638 --> 08:44:20,958 than button greater than and then less 10149 08:44:16,760 --> 08:44:24,040 than SLB button greater than and then 10150 08:44:20,958 --> 08:44:26,120 inside this button we'll put the text 10151 08:44:24,040 --> 08:44:27,280 delete 10152 08:44:26,120 --> 08:44:30,200 let's 10153 08:44:27,280 --> 08:44:33,398 save and in the console you'll see that 10154 08:44:30,200 --> 08:44:36,280 the HTML we generated now has a delete 10155 08:44:33,398 --> 08:44:38,000 button inside the paragraph and that's 10156 08:44:36,280 --> 08:44:41,160 what shows up on the 10157 08:44:38,000 --> 08:44:44,520 page next we're going to reformat this 10158 08:44:41,160 --> 08:44:47,398 HTML to make it a little easier to read 10159 08:44:44,520 --> 08:44:48,360 so we'll create some new lines here and 10160 08:44:47,398 --> 08:44:51,160 at the 10161 08:44:48,360 --> 08:44:55,398 end and let's put the content of the 10162 08:44:51,160 --> 08:44:57,718 paragraph on its own line like this 10163 08:44:55,398 --> 08:45:00,200 and also put the delete button on its 10164 08:44:57,718 --> 08:45:02,638 own line so remember that template 10165 08:45:00,200 --> 08:45:05,360 strings have a feature called multi-line 10166 08:45:02,638 --> 08:45:08,200 strings so this is perfect for writing 10167 08:45:05,360 --> 08:45:08,200 HTML 10168 08:45:08,318 --> 08:45:12,600 code the last step in the process is to 10169 08:45:11,080 --> 08:45:15,440 make the website 10170 08:45:12,600 --> 08:45:17,520 interactive so next let's make our 10171 08:45:15,440 --> 08:45:20,958 delete button do something when we click 10172 08:45:17,520 --> 08:45:25,520 it to do that we use the onclick 10173 08:45:20,958 --> 08:45:28,440 attribute as usual so inside this button 10174 08:45:25,520 --> 08:45:33,200 we're going to add an 10175 08:45:28,440 --> 08:45:35,478 onclick attribute equals double quotes 10176 08:45:33,200 --> 08:45:37,680 and then inside these double quotes we 10177 08:45:35,478 --> 08:45:41,238 can run some JavaScript when we click 10178 08:45:37,680 --> 08:45:44,160 this button so this works the exact same 10179 08:45:41,238 --> 08:45:47,120 way as what we've been doing so far in 10180 08:45:44,160 --> 08:45:50,238 this course the only difference now is 10181 08:45:47,120 --> 08:45:51,680 that here we're generating this HTML 10182 08:45:50,238 --> 08:45:54,360 using 10183 08:45:51,680 --> 08:45:56,760 JavaScript okay so when we click one of 10184 08:45:54,360 --> 08:46:00,040 these delete buttons we want to remove 10185 08:45:56,760 --> 08:46:02,600 this to-do from the list earlier in this 10186 08:46:00,040 --> 08:46:06,080 lesson we learned how to remove a value 10187 08:46:02,600 --> 08:46:09,600 from an array using a method called 10188 08:46:06,080 --> 08:46:15,558 splice so inside this onclick attribute 10189 08:46:09,600 --> 08:46:18,120 we're going to type the to-do list dot 10190 08:46:15,558 --> 08:46:22,040 splice and 10191 08:46:18,120 --> 08:46:24,318 brackets so splice takes two numbers the 10192 08:46:22,040 --> 08:46:25,840 first number is the index that we want 10193 08:46:24,318 --> 08:46:28,558 to remove 10194 08:46:25,840 --> 08:46:30,600 so if we click this delete button how do 10195 08:46:28,558 --> 08:46:33,160 we know which index to 10196 08:46:30,600 --> 08:46:36,318 remove well if we look at our Loop up 10197 08:46:33,160 --> 08:46:40,280 here we already have the index it's this 10198 08:46:36,318 --> 08:46:43,200 variable I so we're going to insert I 10199 08:46:40,280 --> 08:46:47,360 into this string and we can do that by 10200 08:46:43,200 --> 08:46:51,520 typing dollar curly brackets and 10201 08:46:47,360 --> 08:46:54,120 I now splice also takes another number 10202 08:46:51,520 --> 08:46:56,840 so we'll type comma and the second 10203 08:46:54,120 --> 08:47:00,000 number is how many values we want to 10204 08:46:56,840 --> 08:47:02,398 remove so here we just want to remove 10205 08:47:00,000 --> 08:47:05,680 one value at this 10206 08:47:02,398 --> 08:47:08,238 index and finally because we updated the 10207 08:47:05,680 --> 08:47:12,120 to-do list we need to display the list 10208 08:47:08,238 --> 08:47:15,760 again on the page to display the list we 10209 08:47:12,120 --> 08:47:20,520 can use this function render to do list 10210 08:47:15,760 --> 08:47:25,120 so down here we're going to type render 10211 08:47:20,520 --> 08:47:27,000 Todo list and brackets 10212 08:47:25,120 --> 08:47:30,920 now if we 10213 08:47:27,000 --> 08:47:33,760 save you can see that in the generated 10214 08:47:30,920 --> 08:47:36,878 HTML when we click the first delete 10215 08:47:33,760 --> 08:47:40,520 button it will remove the value at 10216 08:47:36,878 --> 08:47:43,160 index0 which is the first value if we 10217 08:47:40,520 --> 08:47:45,958 click the second button it will remove 10218 08:47:43,160 --> 08:47:48,200 the value at index one which is the 10219 08:47:45,958 --> 08:47:52,600 second value so we're basically 10220 08:47:48,200 --> 08:47:55,680 generating all this HTML using a loop so 10221 08:47:52,600 --> 08:47:57,958 now if we click this delete button 10222 08:47:55,680 --> 08:48:01,200 it will remove it from the list and then 10223 08:47:57,958 --> 08:48:03,440 display the updated list on the page so 10224 08:48:01,200 --> 08:48:07,600 that's how we create the delete 10225 08:48:03,440 --> 08:48:10,160 button next let's go back to superimple 10226 08:48:07,600 --> 08:48:10,920 dodev and we're going to create the due 10227 08:48:10,160 --> 08:48:14,238 date 10228 08:48:10,920 --> 08:48:16,238 feature so first let's create this date 10229 08:48:14,238 --> 08:48:20,760 selector in our 10230 08:48:16,238 --> 08:48:23,398 HTML we'll go to our HTML file and then 10231 08:48:20,760 --> 08:48:25,318 under the text box we're going to add 10232 08:48:23,398 --> 08:48:30,280 another input element 10233 08:48:25,318 --> 08:48:33,080 so less than input greater than and to 10234 08:48:30,280 --> 08:48:35,760 make it a date selector input elements 10235 08:48:33,080 --> 08:48:38,760 have an attribute called 10236 08:48:35,760 --> 08:48:42,638 type so this attribute determines what 10237 08:48:38,760 --> 08:48:44,718 kind of input it is if the type is text 10238 08:48:42,638 --> 08:48:46,238 which is the default value it's going to 10239 08:48:44,718 --> 08:48:50,318 be a 10240 08:48:46,238 --> 08:48:52,360 textbox now if we change the type to 10241 08:48:50,318 --> 08:48:56,958 date and 10242 08:48:52,360 --> 08:48:56,958 save it's going to create a date 10243 08:48:57,160 --> 08:49:02,200 selector now let's go back into our 10244 08:48:59,478 --> 08:49:05,160 JavaScript code and first we're going to 10245 08:49:02,200 --> 08:49:08,160 scroll up and remember that right now 10246 08:49:05,160 --> 08:49:11,600 our to-do list is just an array of 10247 08:49:08,160 --> 08:49:14,360 strings but this time each to-do is also 10248 08:49:11,600 --> 08:49:17,920 going to have a due date so how do we 10249 08:49:14,360 --> 08:49:21,840 group the name and the due date together 10250 08:49:17,920 --> 08:49:24,080 we can do that using an object so here 10251 08:49:21,840 --> 08:49:27,440 instead of just a string we're going to 10252 08:49:24,080 --> 08:49:31,718 create an an object and we'll give it a 10253 08:49:27,440 --> 08:49:35,878 name property which is make dinner and 10254 08:49:31,718 --> 08:49:38,160 we'll give it a due date property and 10255 08:49:35,878 --> 08:49:40,920 we'll make the due date a 10256 08:49:38,160 --> 08:49:46,958 string so you can type whatever date you 10257 08:49:40,920 --> 08:49:46,958 have on the page for me it's 20 10258 08:49:47,200 --> 08:49:52,478 22-12 10259 08:49:49,920 --> 08:49:55,440 d22 and I'll end this object with a 10260 08:49:52,478 --> 08:49:58,920 curly bracket and I'll turn this to do 10261 08:49:55,440 --> 08:50:01,920 into an object as well so open curly 10262 08:49:58,920 --> 08:50:01,920 bracket 10263 08:50:02,160 --> 08:50:07,718 name and do 10264 08:50:06,160 --> 08:50:09,238 date 10265 08:50:07,718 --> 08:50:11,280 2022 10266 08:50:09,238 --> 08:50:14,280 D12 10267 08:50:11,280 --> 08:50:17,558 d22 and closed curly 10268 08:50:14,280 --> 08:50:20,840 bracket now that each to-do is an object 10269 08:50:17,558 --> 08:50:23,238 we need to update the rest of our code 10270 08:50:20,840 --> 08:50:25,200 let's scroll down and we'll start with 10271 08:50:23,238 --> 08:50:28,638 the render function 10272 08:50:25,200 --> 08:50:32,040 so here inside the loop each to-do is no 10273 08:50:28,638 --> 08:50:36,040 longer just a string it's an object so 10274 08:50:32,040 --> 08:50:39,878 let's change this variable name to to-do 10275 08:50:36,040 --> 08:50:43,120 object to make it more clear and now we 10276 08:50:39,878 --> 08:50:48,318 need to get the name and due date out of 10277 08:50:43,120 --> 08:50:56,478 this object so we'll type const name 10278 08:50:48,318 --> 08:51:00,760 equals to do object. name and const do 10279 08:50:56,478 --> 08:51:03,360 date equals to do 10280 08:51:00,760 --> 08:51:06,638 object. do 10281 08:51:03,360 --> 08:51:08,878 date and now instead of inserting to-do 10282 08:51:06,638 --> 08:51:10,760 which no longer exists we're going to 10283 08:51:08,878 --> 08:51:14,558 insert the 10284 08:51:10,760 --> 08:51:16,440 name and we're also going to insert the 10285 08:51:14,558 --> 08:51:18,638 DU 10286 08:51:16,440 --> 08:51:22,280 date if we 10287 08:51:18,638 --> 08:51:23,238 save now each to-do will also display a 10288 08:51:22,280 --> 08:51:25,600 due 10289 08:51:23,238 --> 08:51:27,760 date before before we continue we're 10290 08:51:25,600 --> 08:51:30,120 going to review a shortcut we can use 10291 08:51:27,760 --> 08:51:32,558 with objects called 10292 08:51:30,120 --> 08:51:34,478 destructuring so here you'll notice the 10293 08:51:32,558 --> 08:51:36,878 variable name is the same as the 10294 08:51:34,478 --> 08:51:40,040 property name we actually have a 10295 08:51:36,878 --> 08:51:43,520 shortcut for this using 10296 08:51:40,040 --> 08:51:47,840 const curly brackets 10297 08:51:43,520 --> 08:51:51,520 name equals Todo 10298 08:51:47,840 --> 08:51:53,638 object so this is called destructuring 10299 08:51:51,520 --> 08:51:56,040 this takes the name property out of the 10300 08:51:53,638 --> 08:51:59,040 object and and puts it in a variable 10301 08:51:56,040 --> 08:52:02,040 also called name and this is a shortcut 10302 08:51:59,040 --> 08:52:05,558 for this line up here so we can comment 10303 08:52:02,040 --> 08:52:08,718 out this line we can also use multiple 10304 08:52:05,558 --> 08:52:11,478 properties in this shortcut to also take 10305 08:52:08,718 --> 08:52:15,478 the due date out of the object we can 10306 08:52:11,478 --> 08:52:18,760 just add a comma and do 10307 08:52:15,478 --> 08:52:21,000 date so this will do the same thing as 10308 08:52:18,760 --> 08:52:22,600 this line of code and we can comment 10309 08:52:21,000 --> 08:52:25,440 this 10310 08:52:22,600 --> 08:52:26,878 out if we save 10311 08:52:25,440 --> 08:52:30,760 everything still 10312 08:52:26,878 --> 08:52:34,440 works next let's scroll down and we're 10313 08:52:30,760 --> 08:52:36,920 going to update our add Todo function so 10314 08:52:34,440 --> 08:52:39,440 this time when we click the add button 10315 08:52:36,920 --> 08:52:42,760 we're going to get the name and the due 10316 08:52:39,440 --> 08:52:44,520 date and put it into our list so let's 10317 08:52:42,760 --> 08:52:47,478 get the due date out of this date 10318 08:52:44,520 --> 08:52:51,318 selector first we can do that using the 10319 08:52:47,478 --> 08:52:54,040 Dom like we did here so let's add a 10320 08:52:51,318 --> 08:52:56,638 class attribute so we can get it using 10321 08:52:54,040 --> 08:52:59,638 queries selector 10322 08:52:56,638 --> 08:53:01,280 js- do 10323 08:52:59,638 --> 08:53:05,318 dd- 10324 08:53:01,280 --> 08:53:06,878 input and then down here let's create a 10325 08:53:05,318 --> 08:53:12,318 variable 10326 08:53:06,878 --> 08:53:15,200 const date input element and make it 10327 08:53:12,318 --> 08:53:18,558 equal to 10328 08:53:15,200 --> 08:53:22,840 document. query 10329 08:53:18,558 --> 08:53:24,798 selector and a string Dot and then the 10330 08:53:22,840 --> 08:53:26,920 class we want to select which which is 10331 08:53:24,798 --> 08:53:28,680 this class 10332 08:53:26,920 --> 08:53:30,280 js- 10333 08:53:28,680 --> 08:53:33,080 do- 10334 08:53:30,280 --> 08:53:35,878 date- 10335 08:53:33,080 --> 08:53:38,638 input so this will get the date selector 10336 08:53:35,878 --> 08:53:41,718 element and put it into our 10337 08:53:38,638 --> 08:53:43,478 JavaScript and now below this let's get 10338 08:53:41,718 --> 08:53:46,360 the due date out of the 10339 08:53:43,478 --> 08:53:49,000 element we can do that using the value 10340 08:53:46,360 --> 08:53:54,680 property again so let's create a 10341 08:53:49,000 --> 08:54:00,558 variable first const do date and we'll 10342 08:53:54,680 --> 08:54:02,440 make it equal to the date input 10343 08:54:00,558 --> 08:54:04,958 element. 10344 08:54:02,440 --> 08:54:08,000 value so this will get the date that we 10345 08:54:04,958 --> 08:54:11,000 selected and save it in this 10346 08:54:08,000 --> 08:54:14,160 variable and now when we add a to-do to 10347 08:54:11,000 --> 08:54:17,718 our list instead of just a name we're 10348 08:54:14,160 --> 08:54:21,238 going to add an object so open curly 10349 08:54:17,718 --> 08:54:23,718 bracket We'll add a name property and 10350 08:54:21,238 --> 08:54:29,318 the value will be the name variable up 10351 08:54:23,718 --> 08:54:30,360 here so name and then comma and a doe 10352 08:54:29,318 --> 08:54:33,080 date 10353 08:54:30,360 --> 08:54:38,080 property and the value will be this due 10354 08:54:33,080 --> 08:54:40,840 date variable up here so due date and 10355 08:54:38,080 --> 08:54:43,760 then a closed curly 10356 08:54:40,840 --> 08:54:47,000 bracket and remember that objects also 10357 08:54:43,760 --> 08:54:49,440 have a shortcut for this if the property 10358 08:54:47,000 --> 08:54:52,478 and the variable name are the same we 10359 08:54:49,440 --> 08:54:55,680 can just type it out once like 10360 08:54:52,478 --> 08:54:58,558 this this will do the same thing as this 10361 08:54:55,680 --> 08:55:00,920 line above same thing with the second 10362 08:54:58,558 --> 08:55:03,558 line if the property and the variable 10363 08:55:00,920 --> 08:55:07,440 name are the same we can just type a 10364 08:55:03,558 --> 08:55:10,280 comma and type it out 10365 08:55:07,440 --> 08:55:13,120 once this is called the shorthand 10366 08:55:10,280 --> 08:55:16,238 property syntax so these two lines do 10367 08:55:13,120 --> 08:55:18,638 the same thing as these lines above so 10368 08:55:16,238 --> 08:55:21,160 we can comment this out using double 10369 08:55:18,638 --> 08:55:23,680 slash and double 10370 08:55:21,160 --> 08:55:26,878 slash so that's all the code that we 10371 08:55:23,680 --> 08:55:28,160 needed to update now let's save our 10372 08:55:26,878 --> 08:55:32,478 Javascript 10373 08:55:28,160 --> 08:55:35,958 file and we'll also save our HTML 10374 08:55:32,478 --> 08:55:40,600 file and let's go to our project We'll 10375 08:55:35,958 --> 08:55:43,680 add a new to-do like watch 10376 08:55:40,600 --> 08:55:46,080 YouTube and select a 10377 08:55:43,680 --> 08:55:49,600 date and click 10378 08:55:46,080 --> 08:55:53,280 add and scroll down you can see that it 10379 08:55:49,600 --> 08:55:55,238 added our new to-do with a name due date 10380 08:55:53,280 --> 08:55:58,120 and a delete button 10381 08:55:55,238 --> 08:56:01,680 if we click this delete button it will 10382 08:55:58,120 --> 08:56:04,040 remove that to-do if we click this one 10383 08:56:01,680 --> 08:56:06,440 it will also remove the correct 10384 08:56:04,040 --> 08:56:08,638 to-do and that's actually all the 10385 08:56:06,440 --> 08:56:11,478 functionality that we need for this 10386 08:56:08,638 --> 08:56:14,000 project so let's go into our code and 10387 08:56:11,478 --> 08:56:15,840 we're going to clean up by removing the 10388 08:56:14,000 --> 08:56:21,160 console.log 10389 08:56:15,840 --> 08:56:24,878 here and the console.log up 10390 08:56:21,160 --> 08:56:28,440 here and Save 10391 08:56:24,878 --> 08:56:31,600 finally let's add CSS to our project to 10392 08:56:28,440 --> 08:56:34,798 make it look like the final design we'll 10393 08:56:31,600 --> 08:56:37,878 go into our code editor and scroll to 10394 08:56:34,798 --> 08:56:41,160 the top and instead of writing all the 10395 08:56:37,878 --> 08:56:44,558 CSS in here let's put the CSS in a 10396 08:56:41,160 --> 08:56:47,280 separate file just like our JavaScript 10397 08:56:44,558 --> 08:56:51,040 so we'll click here to show our 10398 08:56:47,280 --> 08:56:53,760 files and then find the Styles folder 10399 08:56:51,040 --> 08:56:57,520 and we'll click the Styles folder and 10400 08:56:53,760 --> 08:57:01,440 click this icon to create a new file 10401 08:56:57,520 --> 08:57:02,970 inside and we'll name this file 10402 08:57:01,440 --> 08:57:04,318 11- 10403 08:57:02,970 --> 08:57:06,558 [Music] 10404 08:57:04,318 --> 08:57:09,478 to-do-list 10405 08:57:06,558 --> 08:57:12,360 docss and press 10406 08:57:09,478 --> 08:57:15,040 enter and for the video I'm going to 10407 08:57:12,360 --> 08:57:17,760 take this CSS file and put it at the 10408 08:57:15,040 --> 08:57:21,200 bottom of my code editor so I can see 10409 08:57:17,760 --> 08:57:22,798 these files side by side and now I'll 10410 08:57:21,200 --> 08:57:27,398 click here to close 10411 08:57:22,798 --> 08:57:30,718 it and will load this CSS into our 10412 08:57:27,398 --> 08:57:34,958 HTML to do that we can use a link 10413 08:57:30,718 --> 08:57:39,440 element so inside the head section let's 10414 08:57:34,958 --> 08:57:42,280 type less than link greater than and 10415 08:57:39,440 --> 08:57:46,798 we're going to give it two attributes 10416 08:57:42,280 --> 08:57:49,120 Rel equals style 10417 08:57:46,798 --> 08:57:53,318 sheet and 10418 08:57:49,120 --> 08:57:55,160 hre equals the file that we want to load 10419 08:57:53,318 --> 08:57:59,558 so we created this this in the Styles 10420 08:57:55,160 --> 08:58:02,798 folder so we'll type Styles and then a 10421 08:57:59,558 --> 08:58:04,878 slash to go into the folder and we load 10422 08:58:02,798 --> 08:58:06,920 this file 10423 08:58:04,878 --> 08:58:10,600 11- 10424 08:58:06,920 --> 08:58:13,160 too- list. 10425 08:58:10,600 --> 08:58:16,080 CSS and 10426 08:58:13,160 --> 08:58:19,520 save now let's style our project one by 10427 08:58:16,080 --> 08:58:22,080 one until it matches the final design 10428 08:58:19,520 --> 08:58:24,878 we'll start with the text on the page 10429 08:58:22,080 --> 08:58:27,520 we're going to change the font so in 10430 08:58:24,878 --> 08:58:30,680 here let's type 10431 08:58:27,520 --> 08:58:37,318 body curly brackets and then we'll 10432 08:58:30,680 --> 08:58:38,680 change the font Das family colon Arial 10433 08:58:37,318 --> 08:58:41,840 and 10434 08:58:38,680 --> 08:58:45,318 save so that looks pretty good now let's 10435 08:58:41,840 --> 08:58:47,958 work on styling the to-do list first 10436 08:58:45,318 --> 08:58:50,318 let's add some more to-dos to the list 10437 08:58:47,958 --> 08:58:51,920 so we'll type 10438 08:58:50,318 --> 08:58:55,360 watch 10439 08:58:51,920 --> 08:58:56,718 YouTube and then we'll select a date 10440 08:58:55,360 --> 08:58:58,840 and click 10441 08:58:56,718 --> 08:59:01,920 add and we're going to learn how to 10442 08:58:58,840 --> 08:59:05,160 style our list to look like this so 10443 08:59:01,920 --> 08:59:07,718 notice that this list creates a sort of 10444 08:59:05,160 --> 08:59:12,160 grid we're going to create this grid 10445 08:59:07,718 --> 08:59:15,280 using a feature called CSS grid so let's 10446 08:59:12,160 --> 08:59:18,080 go into our HTML 10447 08:59:15,280 --> 08:59:20,760 first we're going to add a class to this 10448 08:59:18,080 --> 08:59:26,718 div element so we can style 10449 08:59:20,760 --> 08:59:30,600 it to-do Das grid R and let's 10450 08:59:26,718 --> 08:59:36,840 save and inside our CSS we're going to 10451 08:59:30,600 --> 08:59:38,280 style this class using dot Todo Das grid 10452 08:59:36,840 --> 08:59:41,280 and curly 10453 08:59:38,280 --> 08:59:44,160 brackets and to turn this into a grid 10454 08:59:41,280 --> 08:59:47,000 we're going to use the property 10455 08:59:44,160 --> 08:59:49,398 display and 10456 08:59:47,000 --> 08:59:56,478 grid and we're going to add another 10457 08:59:49,398 --> 08:59:59,798 property called grid Das template Dash 10458 08:59:56,478 --> 09:00:02,798 columns so this basically sets how many 10459 08:59:59,798 --> 09:00:05,318 columns are in our grid the number of 10460 09:00:02,798 --> 09:00:10,280 values that we give it is the number of 10461 09:00:05,318 --> 09:00:15,958 columns for example if we give it 200 10462 09:00:10,280 --> 09:00:19,120 pixels 150 pixels and 100 10463 09:00:15,958 --> 09:00:22,318 pixels this will create three columns in 10464 09:00:19,120 --> 09:00:24,920 the grid the First Column will be 200 10465 09:00:22,318 --> 09:00:28,878 pixels the second Colum column will be 10466 09:00:24,920 --> 09:00:29,840 150 pixels and the third column will be 10467 09:00:28,878 --> 09:00:32,958 100 10468 09:00:29,840 --> 09:00:36,120 pixels and now each element inside this 10469 09:00:32,958 --> 09:00:37,798 div will go into the columns so the 10470 09:00:36,120 --> 09:00:40,200 first element will go in the First 10471 09:00:37,798 --> 09:00:42,600 Column second element will go in the 10472 09:00:40,200 --> 09:00:45,318 second column and the third element will 10473 09:00:42,600 --> 09:00:48,280 go in the third column if we have more 10474 09:00:45,318 --> 09:00:50,520 than three elements it will wrap around 10475 09:00:48,280 --> 09:00:52,760 and create a new 10476 09:00:50,520 --> 09:00:55,958 row so let's 10477 09:00:52,760 --> 09:00:58,638 save and and unfortunately that didn't 10478 09:00:55,958 --> 09:01:00,638 quite work and that's because if we go 10479 09:00:58,638 --> 09:01:04,080 back to our 10480 09:01:00,638 --> 09:01:07,080 JavaScript remember that each to-do is 10481 09:01:04,080 --> 09:01:10,040 one paragraph element so this entire 10482 09:01:07,080 --> 09:01:13,680 to-do with the name due date and delete 10483 09:01:10,040 --> 09:01:16,280 button will go inside the First Column 10484 09:01:13,680 --> 09:01:19,238 and then the next entire to-do will go 10485 09:01:16,280 --> 09:01:21,718 inside the second column but what we 10486 09:01:19,238 --> 09:01:24,520 actually want is for the name to go in 10487 09:01:21,718 --> 09:01:28,040 column 1 due date column 2 10488 09:01:24,520 --> 09:01:30,160 and delete button column 3 to do this 10489 09:01:28,040 --> 09:01:32,958 we're going to separate this element 10490 09:01:30,160 --> 09:01:35,080 into three elements so that each element 10491 09:01:32,958 --> 09:01:38,920 will go into each 10492 09:01:35,080 --> 09:01:41,520 column so instead of one 10493 09:01:38,920 --> 09:01:45,680 paragraph let's actually use a div 10494 09:01:41,520 --> 09:01:49,680 element so less than div greater than 10495 09:01:45,680 --> 09:01:55,398 and less than slash div greater than and 10496 09:01:49,680 --> 09:01:55,398 in between we'll insert the name 10497 09:01:56,200 --> 09:02:01,160 so the reason we use a div element is 10498 09:01:58,878 --> 09:02:03,760 because div elements don't have any 10499 09:02:01,160 --> 09:02:05,360 default styling so it's a little easier 10500 09:02:03,760 --> 09:02:08,840 to style with 10501 09:02:05,360 --> 09:02:12,360 CSS for the due date let's also create a 10502 09:02:08,840 --> 09:02:16,600 div element so less than 10503 09:02:12,360 --> 09:02:20,360 div and then at the end less than slash 10504 09:02:16,600 --> 09:02:23,520 div greater than and finally for the 10505 09:02:20,360 --> 09:02:26,478 delete button we already have an element 10506 09:02:23,520 --> 09:02:29,040 so so let's just reformat these lines 10507 09:02:26,478 --> 09:02:32,000 we'll select these lines and then press 10508 09:02:29,040 --> 09:02:35,318 shift tab to remove the 10509 09:02:32,000 --> 09:02:38,080 indent let's remove the extra line and 10510 09:02:35,318 --> 09:02:39,160 we basically separated each to-do into 10511 09:02:38,080 --> 09:02:41,318 three 10512 09:02:39,160 --> 09:02:44,920 elements if we 10513 09:02:41,318 --> 09:02:48,040 save now the grid is working because 10514 09:02:44,920 --> 09:02:50,600 this element goes into the First Column 10515 09:02:48,040 --> 09:02:52,760 this element goes into the second column 10516 09:02:50,600 --> 09:02:54,840 and the button goes into the third 10517 09:02:52,760 --> 09:02:57,000 column 10518 09:02:54,840 --> 09:03:01,160 and that's how we lay out these elements 10519 09:02:57,000 --> 09:03:03,638 in a grid like the final design next 10520 09:03:01,160 --> 09:03:08,040 let's also lay out the inputs and the 10521 09:03:03,638 --> 09:03:10,878 add button in a grid we'll go to our 10522 09:03:08,040 --> 09:03:14,360 HTML and let's put these three elements 10523 09:03:10,878 --> 09:03:18,080 in a div so we can use grid so we'll 10524 09:03:14,360 --> 09:03:20,760 create a less than div greater 10525 09:03:18,080 --> 09:03:24,360 than and then 10526 09:03:20,760 --> 09:03:29,600 inside let's remove this and we'll add 10527 09:03:24,360 --> 09:03:33,160 it at the end less than SL div and let's 10528 09:03:29,600 --> 09:03:36,200 reformat these lines by selecting them 10529 09:03:33,160 --> 09:03:39,360 and then press tab to add an 10530 09:03:36,200 --> 09:03:43,878 indent and now let's make this div a 10531 09:03:39,360 --> 09:03:48,238 grid as well let's go back to our CSS 10532 09:03:43,878 --> 09:03:55,120 and we'll add a class to this div class 10533 09:03:48,238 --> 09:03:57,638 equals let's call it to-do Das input d 10534 09:03:55,120 --> 09:04:02,160 dgrid and instead of writing all this 10535 09:03:57,638 --> 09:04:06,360 CSS again we can use a feature of CSS so 10536 09:04:02,160 --> 09:04:09,440 here we're going to type a comma and 10537 09:04:06,360 --> 09:04:12,840 enter so when we type a comma we can 10538 09:04:09,440 --> 09:04:17,120 style multiple classes at the same time 10539 09:04:12,840 --> 09:04:23,280 so we're also going to style dot 10540 09:04:17,120 --> 09:04:24,520 too- input Das grid if we save the CSS 10541 09:04:23,280 --> 09:04:27,718 file 10542 09:04:24,520 --> 09:04:30,878 and let's also save the HTML 10543 09:04:27,718 --> 09:04:33,360 file now the input and the add button 10544 09:04:30,878 --> 09:04:37,080 are also laid out in the same 10545 09:04:33,360 --> 09:04:39,920 grid next let's adjust the spacing 10546 09:04:37,080 --> 09:04:43,840 inside our grid so grid has some 10547 09:04:39,920 --> 09:04:47,760 properties for this we'll go to our CSS 10548 09:04:43,840 --> 09:04:52,878 press enter and we'll use the property 10549 09:04:47,760 --> 09:04:56,280 column Das Gap so this sets the spacing 10550 09:04:52,878 --> 09:05:01,120 between the Columns of the grid so let's 10551 09:04:56,280 --> 09:05:04,200 make this colon 10 pixels and 10552 09:05:01,120 --> 09:05:06,920 save so now it will add 10 pixels 10553 09:05:04,200 --> 09:05:09,718 between each column let's add some 10554 09:05:06,920 --> 09:05:13,840 spacing between the rows as well and 10555 09:05:09,718 --> 09:05:19,600 grid has another property for this row 10556 09:05:13,840 --> 09:05:23,040 Das Gap colon 10 pixels and 10557 09:05:19,600 --> 09:05:25,840 save so that will add 10 pixels of space 10558 09:05:23,040 --> 09:05:28,680 between the rows now notice it didn't 10559 09:05:25,840 --> 09:05:30,798 add space between these two rows and 10560 09:05:28,680 --> 09:05:34,120 that's because these two are separate 10561 09:05:30,798 --> 09:05:36,318 grids this is the to-do list and this is 10562 09:05:34,120 --> 09:05:39,520 the to-do input 10563 09:05:36,318 --> 09:05:41,000 grid so to add space between them we 10564 09:05:39,520 --> 09:05:46,160 just going to use 10565 09:05:41,000 --> 09:05:50,638 margin at the bottom we'll style dot 10566 09:05:46,160 --> 09:05:53,280 too- input Das 10567 09:05:50,638 --> 09:05:59,120 grid and we'll just add some margin 10568 09:05:53,280 --> 09:06:03,600 under this grid so margin Das bottom 10569 09:05:59,120 --> 09:06:06,638 colon 10 pixels and 10570 09:06:03,600 --> 09:06:09,878 save all right now let's move on to 10571 09:06:06,638 --> 09:06:13,078 styling the inputs and the buttons we'll 10572 09:06:09,878 --> 09:06:16,598 start with the inputs first let's add a 10573 09:06:13,078 --> 09:06:21,078 class so we can style them here let's 10574 09:06:16,598 --> 09:06:24,360 add a class name Das 10575 09:06:21,078 --> 09:06:26,680 input and down here let's let's also add 10576 09:06:24,360 --> 09:06:30,320 another class 10577 09:06:26,680 --> 09:06:32,680 do- dat Das 10578 09:06:30,320 --> 09:06:36,800 input let's 10579 09:06:32,680 --> 09:06:38,238 save and now inside our CSS we'll style 10580 09:06:36,800 --> 09:06:41,918 both of these 10581 09:06:38,238 --> 09:06:46,840 inputs so we'll style do 10582 09:06:41,918 --> 09:06:48,800 name- input curly brackets and first 10583 09:06:46,840 --> 09:06:54,398 let's make the text bigger like the 10584 09:06:48,800 --> 09:06:59,640 final design we'll set the font Das size 10585 09:06:54,398 --> 09:07:02,520 colon 15 pixels and save okay so that 10586 09:06:59,640 --> 09:07:07,160 looks pretty good now let's add spacing 10587 09:07:02,520 --> 09:07:11,320 on the inside of this input so we'll add 10588 09:07:07,160 --> 09:07:14,278 padding six pixels and 10589 09:07:11,320 --> 09:07:17,520 save and that looks pretty good let's 10590 09:07:14,278 --> 09:07:19,878 move on to the due date input so we 10591 09:07:17,520 --> 09:07:22,960 actually want the same Styles as the 10592 09:07:19,878 --> 09:07:27,078 name input so again we can use that 10593 09:07:22,960 --> 09:07:29,520 feature of CSS we'll type a comma and 10594 09:07:27,078 --> 09:07:31,320 then dot 10595 09:07:29,520 --> 09:07:35,520 do- 10596 09:07:31,320 --> 09:07:39,918 date- input so now these Styles will be 10597 09:07:35,520 --> 09:07:41,160 applied to this class and to this class 10598 09:07:39,918 --> 09:07:44,680 let's 10599 09:07:41,160 --> 09:07:47,200 save and that looks pretty good to me 10600 09:07:44,680 --> 09:07:51,078 let's move on and style the 10601 09:07:47,200 --> 09:07:54,360 buttons we'll go into our HTML and find 10602 09:07:51,078 --> 09:07:55,480 the add button let's add a class so we 10603 09:07:54,360 --> 09:08:00,078 can style 10604 09:07:55,480 --> 09:08:03,520 it and we'll use add- 10605 09:08:00,078 --> 09:08:07,360 too- button let's 10606 09:08:03,520 --> 09:08:12,398 save and then inside our CSS we'll style 10607 09:08:07,360 --> 09:08:16,000 that class using dot add- 10608 09:08:12,398 --> 09:08:18,238 too- button curly 10609 09:08:16,000 --> 09:08:21,160 brackets and let's start with the 10610 09:08:18,238 --> 09:08:24,122 background color we'll set the 10611 09:08:21,160 --> 09:08:25,840 background-color 10612 09:08:24,122 --> 09:08:28,122 colon 10613 09:08:25,840 --> 09:08:31,078 green and 10614 09:08:28,122 --> 09:08:35,360 save now let's do the text 10615 09:08:31,078 --> 09:08:37,758 color color colon 10616 09:08:35,360 --> 09:08:40,840 white and 10617 09:08:37,758 --> 09:08:46,122 save now let's remove the border around 10618 09:08:40,840 --> 09:08:47,960 our button using border colon 10619 09:08:46,122 --> 09:08:50,718 none 10620 09:08:47,960 --> 09:08:54,122 save and let's make the text a little 10621 09:08:50,718 --> 09:08:58,640 bigger so we'll do font 10622 09:08:54,122 --> 09:09:01,122 D size colon 15 10623 09:08:58,640 --> 09:09:03,878 pixels and 10624 09:09:01,122 --> 09:09:06,238 save and the last thing we'll add is 10625 09:09:03,878 --> 09:09:09,718 when we put our Mouse over the button it 10626 09:09:06,238 --> 09:09:11,960 turns into this pointer icon we can do 10627 09:09:09,718 --> 09:09:15,238 that using 10628 09:09:11,960 --> 09:09:18,000 cursor colon 10629 09:09:15,238 --> 09:09:22,200 pointer and 10630 09:09:18,000 --> 09:09:24,238 save next let's style the delete buttons 10631 09:09:22,200 --> 09:09:27,122 so these delete buttons are created in 10632 09:09:24,238 --> 09:09:28,598 our JavaScript we'll go into our 10633 09:09:27,122 --> 09:09:32,960 JavaScript 10634 09:09:28,598 --> 09:09:34,840 file and then here let's add a class to 10635 09:09:32,960 --> 09:09:39,758 each of the delete buttons so we can 10636 09:09:34,840 --> 09:09:44,398 style them so we'll add class equals 10637 09:09:39,758 --> 09:09:46,000 double quotes and then delete Das 10638 09:09:44,398 --> 09:09:48,278 too- 10639 09:09:46,000 --> 09:09:51,278 button let's 10640 09:09:48,278 --> 09:09:57,520 save and then inside our 10641 09:09:51,278 --> 09:10:02,278 CSS at the bottom we style dot delete 10642 09:09:57,520 --> 09:10:04,480 Das too Das button curly 10643 09:10:02,278 --> 09:10:08,758 brackets and let's start with the 10644 09:10:04,480 --> 09:10:13,520 background color so we'll set background 10645 09:10:08,758 --> 09:10:16,598 Das color colon and for this design I 10646 09:10:13,520 --> 09:10:18,758 use the color dark 10647 09:10:16,598 --> 09:10:21,718 red and 10648 09:10:18,758 --> 09:10:24,122 save so that will style our delete 10649 09:10:21,718 --> 09:10:29,680 buttons now let's change change the text 10650 09:10:24,122 --> 09:10:31,800 color to white so we'll use color colon 10651 09:10:29,680 --> 09:10:36,558 white and 10652 09:10:31,800 --> 09:10:39,320 save now let's remove the Border using 10653 09:10:36,558 --> 09:10:41,000 border colon 10654 09:10:39,320 --> 09:10:45,000 none 10655 09:10:41,000 --> 09:10:51,360 save and let's also make the text bigger 10656 09:10:45,000 --> 09:10:53,758 using font Das size colon 15 10657 09:10:51,360 --> 09:10:58,918 pixels save 10658 09:10:53,758 --> 09:11:00,758 let's add this pointer icon using cursor 10659 09:10:58,918 --> 09:11:04,758 colon 10660 09:11:00,758 --> 09:11:08,078 pointer save and let's add some padding 10661 09:11:04,758 --> 09:11:11,320 at the top and bottom of the button so 10662 09:11:08,078 --> 09:11:17,278 we'll add 10663 09:11:11,320 --> 09:11:25,078 padding-top colon 10 pixels and padding 10664 09:11:17,278 --> 09:11:27,160 Dash bottom colon 10 pixels and Save 10665 09:11:25,078 --> 09:11:29,960 so that's all the Styles we need for the 10666 09:11:27,160 --> 09:11:33,598 delete button and we can also close the 10667 09:11:29,960 --> 09:11:36,200 console to see the entire list however 10668 09:11:33,598 --> 09:11:38,598 now we have another problem which is 10669 09:11:36,200 --> 09:11:41,758 this text and this button are not 10670 09:11:38,598 --> 09:11:44,200 vertically aligned in the center to 10671 09:11:41,758 --> 09:11:47,398 vertically align in the center we're 10672 09:11:44,200 --> 09:11:48,398 going to go back to the grid Styles so 10673 09:11:47,398 --> 09:11:51,040 we'll scroll 10674 09:11:48,398 --> 09:11:54,520 up to the to-do 10675 09:11:51,040 --> 09:11:59,558 grid and CSS grid has another property 10676 09:11:54,520 --> 09:12:03,758 for vertical alignment and that is align 10677 09:11:59,558 --> 09:12:08,040 Das items Colon 10678 09:12:03,758 --> 09:12:09,160 Center to vertically align in the center 10679 09:12:08,040 --> 09:12:12,200 if we 10680 09:12:09,160 --> 09:12:15,598 save now the text is aligned with the 10681 09:12:12,200 --> 09:12:18,360 button but now we have a new problem our 10682 09:12:15,598 --> 09:12:22,040 add button is no longer stretching to be 10683 09:12:18,360 --> 09:12:24,200 the same as the inputs so by default 10684 09:12:22,040 --> 09:12:27,238 align items is 10685 09:12:24,200 --> 09:12:29,960 stretch that's why this button stretches 10686 09:12:27,238 --> 09:12:32,598 to match the inputs and now that we 10687 09:12:29,960 --> 09:12:35,360 changed it to align center it's no 10688 09:12:32,598 --> 09:12:39,000 longer going to stretch so to solve this 10689 09:12:35,360 --> 09:12:43,122 problem we just have to make this grid 10690 09:12:39,000 --> 09:12:49,320 stretch so down here in the to-do input 10691 09:12:43,122 --> 09:12:51,558 grid Styles we'll change align Das items 10692 09:12:49,320 --> 09:12:53,718 back to 10693 09:12:51,558 --> 09:12:56,398 stretch and Save 10694 09:12:53,718 --> 09:13:00,238 save and that will cause this Grid at 10695 09:12:56,398 --> 09:13:02,480 the top to stretch vertically again and 10696 09:13:00,238 --> 09:13:06,000 that's it that's all the styling that we 10697 09:13:02,480 --> 09:13:08,598 need and now we finished this to-do list 10698 09:13:06,000 --> 09:13:11,078 project in the rest of this lesson we're 10699 09:13:08,598 --> 09:13:11,960 going to learn more details about arrays 10700 09:13:11,078 --> 09:13:15,078 and 10701 09:13:11,960 --> 09:13:16,278 Loops so I'm going to put this back in 10702 09:13:15,078 --> 09:13:19,160 the same 10703 09:13:16,278 --> 09:13:22,480 window and I'll rearrange this 10704 09:13:19,160 --> 09:13:24,840 window and now let's go back to 11 10705 09:13:22,480 --> 09:13:28,598 arrays and loops. 10706 09:13:24,840 --> 09:13:31,480 HTML and we'll open the tab for that 10707 09:13:28,598 --> 09:13:33,758 file and we can also close these other 10708 09:13:31,480 --> 09:13:36,840 files for 10709 09:13:33,758 --> 09:13:39,122 now and first let's comment out the 10710 09:13:36,840 --> 09:13:41,160 previous code 10711 09:13:39,122 --> 09:13:45,840 using 10712 09:13:41,160 --> 09:13:50,278 slashstar and at the bottom star 10713 09:13:45,840 --> 09:13:52,320 slash and we'll scroll down and 10714 09:13:50,278 --> 09:13:55,718 save the first detail we're going to 10715 09:13:52,320 --> 09:13:58,238 learn is that that arrays are references 10716 09:13:55,718 --> 09:14:01,758 so in the object lesson we learned that 10717 09:13:58,238 --> 09:14:05,480 objects are references arrays work the 10718 09:14:01,758 --> 09:14:07,238 same way so for example if we create an 10719 09:14:05,480 --> 09:14:15,558 array 10720 09:14:07,238 --> 09:14:18,680 const array 1 equals the array 1 2 10721 09:14:15,558 --> 09:14:23,320 3 this variable doesn't actually contain 10722 09:14:18,680 --> 09:14:25,718 the values 1 2 3 this array is saved 10723 09:14:23,320 --> 09:14:28,558 somewhere in the computer's memory and 10724 09:14:25,718 --> 09:14:31,718 this variable only contains a reference 10725 09:14:28,558 --> 09:14:34,718 that points to where this array is in 10726 09:14:31,718 --> 09:14:37,598 memory and that means if we create a 10727 09:14:34,718 --> 09:14:43,558 copy of this variable 10728 09:14:37,598 --> 09:14:47,718 const array 2 equals array 10729 09:14:43,558 --> 09:14:50,840 1 array 2 doesn't contain a copy of the 10730 09:14:47,718 --> 09:14:53,598 values it actually just contains a copy 10731 09:14:50,840 --> 09:14:57,398 of the reference so now both of these 10732 09:14:53,598 --> 09:15:00,520 variables are pointing to the same array 10733 09:14:57,398 --> 09:15:03,840 so if we did array 10734 09:15:00,520 --> 09:15:07,758 2. push 10735 09:15:03,840 --> 09:15:07,758 for and then we 10736 09:15:08,360 --> 09:15:14,718 console.log array 1 and we 10737 09:15:15,160 --> 09:15:20,398 console.log array 10738 09:15:17,918 --> 09:15:23,278 two and 10739 09:15:20,398 --> 09:15:27,918 save it will show us that even though we 10740 09:15:23,278 --> 09:15:31,078 added four to array two array 1 and two 10741 09:15:27,918 --> 09:15:34,438 were both modified and that's because 10742 09:15:31,078 --> 09:15:37,878 arrays are references so both of these 10743 09:15:34,438 --> 09:15:40,878 point to the same array in memory to 10744 09:15:37,878 --> 09:15:43,480 avoid this Behavior we can create a copy 10745 09:15:40,878 --> 09:15:48,398 of the values in the array using a 10746 09:15:43,480 --> 09:15:49,680 method called slice so here if we did 10747 09:15:48,398 --> 09:15:53,360 array 10748 09:15:49,680 --> 09:15:56,238 1. slice instead 10749 09:15:53,360 --> 09:15:59,480 this will make a copy of the values in 10750 09:15:56,238 --> 09:16:04,078 Array one and now these two variables 10751 09:15:59,480 --> 09:16:07,758 point to two different arrays if we 10752 09:16:04,078 --> 09:16:09,520 save now you'll see that this line only 10753 09:16:07,758 --> 09:16:11,598 affected array 10754 09:16:09,520 --> 09:16:14,480 two the second detail we're going to 10755 09:16:11,598 --> 09:16:17,800 learn is a shortcut called 10756 09:16:14,480 --> 09:16:20,800 destructuring so at the bottom here 10757 09:16:17,800 --> 09:16:29,078 let's create a new array 10758 09:16:20,800 --> 09:16:32,278 const array 3 3 equals the array 1 2 10759 09:16:29,078 --> 09:16:35,398 3 and let's say we want to get the first 10760 09:16:32,278 --> 09:16:38,480 value and the second value out of this 10761 09:16:35,398 --> 09:16:42,680 array one way to do this is to use the 10762 09:16:38,480 --> 09:16:44,718 index like this another way is to use a 10763 09:16:42,680 --> 09:16:47,480 shortcut called 10764 09:16:44,718 --> 09:16:51,078 destructuring so here we're going to 10765 09:16:47,480 --> 09:16:53,800 replace this with square brackets and 10766 09:16:51,078 --> 09:16:58,320 now we can type some variable name 10767 09:16:53,800 --> 09:17:01,000 if we type first value it will take the 10768 09:16:58,320 --> 09:17:05,918 first value and save it in a variable 10769 09:17:01,000 --> 09:17:09,360 called first value if we type comma and 10770 09:17:05,918 --> 09:17:13,398 second value it will take the second 10771 09:17:09,360 --> 09:17:16,520 value and save it in this variable so 10772 09:17:13,398 --> 09:17:20,398 this is an easier way to get values out 10773 09:17:16,520 --> 09:17:22,960 of an array and save them inside a 10774 09:17:20,398 --> 09:17:26,640 variable next we're going to learn more 10775 09:17:22,960 --> 09:17:30,840 details about loops so Loops have two 10776 09:17:26,640 --> 09:17:35,360 features called break and continue break 10777 09:17:30,840 --> 09:17:37,680 lets us exit a loop early for example 10778 09:17:35,360 --> 09:17:41,480 let's create some new lines and let's 10779 09:17:37,680 --> 09:17:45,040 create a loop that counts from 1 to 10 10780 09:17:41,480 --> 09:17:48,960 so as usual we can use a four 10781 09:17:45,040 --> 09:17:53,960 Loop let I = 10782 09:17:48,960 --> 09:17:56,718 1 I less than or equal to 10 10783 09:17:53,960 --> 09:17:59,238 and I 10784 09:17:56,718 --> 09:18:04,480 ++ and we'll 10785 09:17:59,238 --> 09:18:08,640 console. log I and 10786 09:18:04,480 --> 09:18:12,800 save so this will count from 1 to 10787 09:18:08,640 --> 09:18:16,278 10 so now let's say that when I is 8 we 10788 09:18:12,800 --> 09:18:24,480 want to stop this Loop early so first 10789 09:18:16,278 --> 09:18:27,278 let's check if I is equal to 8 10790 09:18:24,480 --> 09:18:31,438 if it is equal to 8 we're going to stop 10791 09:18:27,278 --> 09:18:33,840 this Loop by typing break and 10792 09:18:31,438 --> 09:18:37,680 semicolon if we 10793 09:18:33,840 --> 09:18:40,758 save once this reaches eight break will 10794 09:18:37,680 --> 09:18:44,160 cause this Loop to stop and now it only 10795 09:18:40,758 --> 09:18:47,598 counts up to eight so that's the break 10796 09:18:44,160 --> 09:18:50,398 feature of a loop it lets us stop a loop 10797 09:18:47,598 --> 09:18:53,320 early the next feature is called 10798 09:18:50,398 --> 09:18:56,438 continue continue lets us skip one 10799 09:18:53,320 --> 09:18:59,398 iteration of the loop or one run of the 10800 09:18:56,438 --> 09:19:03,800 loop for example let's say we want to 10801 09:18:59,398 --> 09:19:10,360 skip the number three so at the top we 10802 09:19:03,800 --> 09:19:15,840 can check if I is equal to 10803 09:19:10,360 --> 09:19:18,400 three and if it is we can use the code 10804 09:19:15,840 --> 09:19:21,360 continue this will skip the rest of this 10805 09:19:18,400 --> 09:19:26,320 code and go back up to the top of the 10806 09:19:21,360 --> 09:19:31,160 loop and do i = 4 if we 10807 09:19:26,320 --> 09:19:34,000 save this will skip three in the loop so 10808 09:19:31,160 --> 09:19:36,756 continue let us Skip One iteration of 10809 09:19:34,000 --> 09:19:39,880 the loop so now let's say that we want 10810 09:19:36,756 --> 09:19:42,560 to skip a number if it's divisible by 10811 09:19:39,880 --> 09:19:45,800 three divisible by three means the 10812 09:19:42,560 --> 09:19:49,960 number can be divided evenly into three 10813 09:19:45,800 --> 09:19:54,320 for example 3 and six are both divisible 10814 09:19:49,960 --> 09:19:57,120 by three but s is not divisible by three 10815 09:19:54,320 --> 09:19:59,596 because there's one remaining this is 10816 09:19:57,120 --> 09:20:01,720 called the remainder and we can 10817 09:19:59,596 --> 09:20:05,240 calculate the remainder using the 10818 09:20:01,720 --> 09:20:07,640 remainder operator which looks like this 10819 09:20:05,240 --> 09:20:11,520 so to check if a number is divisible by 10820 09:20:07,640 --> 09:20:15,436 three we can check if the remainder is 10821 09:20:11,520 --> 09:20:19,756 zero so here we're going to replace this 10822 09:20:15,436 --> 09:20:25,520 with I remainder 3 and we'll check if 10823 09:20:19,756 --> 09:20:29,480 the remainder is equal to to Z if we 10824 09:20:25,520 --> 09:20:32,436 save this will skip three and six 10825 09:20:29,480 --> 09:20:36,120 because they're both divisible by 10826 09:20:32,436 --> 09:20:39,720 three next let's use continue in a while 10827 09:20:36,120 --> 09:20:42,520 loop so at the end here we'll create 10828 09:20:39,720 --> 09:20:46,720 some new lines and let's say we want to 10829 09:20:42,520 --> 09:20:51,480 count from 1 to 10 using a while loop so 10830 09:20:46,720 --> 09:20:55,756 we'll create a loop variable let I equal 10831 09:20:51,480 --> 09:21:01,160 1 and we'll create a while 10832 09:20:55,756 --> 09:21:05,960 loop I less than or equal to 10 curly 10833 09:21:01,160 --> 09:21:10,360 brackets and the increment step 10834 09:21:05,960 --> 09:21:15,080 i++ and inside the loop we just 10835 09:21:10,360 --> 09:21:18,916 console. log I and 10836 09:21:15,080 --> 09:21:22,120 save so this will count from 1 to 10 10837 09:21:18,916 --> 09:21:25,880 again and let's say we want to not count 10838 09:21:22,120 --> 09:21:32,200 numbers that are divisible by three so 3 10839 09:21:25,880 --> 09:21:39,080 6 and 9 so at the top here we can check 10840 09:21:32,200 --> 09:21:42,160 if I remainder 3 is equal to zero and 10841 09:21:39,080 --> 09:21:43,840 curly brackets and we might just try to 10842 09:21:42,160 --> 09:21:46,960 do 10843 09:21:43,840 --> 09:21:49,200 continue however in a while loop 10844 09:21:46,960 --> 09:21:51,880 continue will actually skip the 10845 09:21:49,200 --> 09:21:54,276 increment step and this will cause an 10846 09:21:51,880 --> 09:21:57,200 infinite Loop Loop so when using 10847 09:21:54,276 --> 09:22:00,680 continue in a while loop we have to do 10848 09:21:57,200 --> 09:22:04,520 this increment step manually so before 10849 09:22:00,680 --> 09:22:09,880 continue we have to do I 10850 09:22:04,520 --> 09:22:12,436 ++ to avoid an infinite Loop if we 10851 09:22:09,880 --> 09:22:18,000 save it will skip the numbers that are 10852 09:22:12,436 --> 09:22:20,756 divisible by three which is 3 6 and 9 so 10853 09:22:18,000 --> 09:22:23,756 remember in a while loop if we use 10854 09:22:20,756 --> 09:22:26,840 continue always do the increment step 10855 09:22:23,756 --> 09:22:29,436 manually in a for Loop however the 10856 09:22:26,840 --> 09:22:31,160 increment step is done automatically 10857 09:22:29,436 --> 09:22:33,680 after every 10858 09:22:31,160 --> 09:22:36,200 iteration all right the last detail 10859 09:22:33,680 --> 09:22:39,240 we're going to learn is how to use loops 10860 09:22:36,200 --> 09:22:42,200 with functions so let's say we have an 10861 09:22:39,240 --> 09:22:45,160 array of numbers and we want to create a 10862 09:22:42,200 --> 09:22:48,040 copy of this array where each number is 10863 09:22:45,160 --> 09:22:51,520 doubled so we already have the code for 10864 09:22:48,040 --> 09:22:53,200 this we can scroll up and the code for 10865 09:22:51,520 --> 09:22:55,680 this is here 10866 09:22:53,200 --> 09:22:58,520 so let's actually make a copy of this 10867 09:22:55,680 --> 09:23:00,480 code from before we'll 10868 09:22:58,520 --> 09:23:03,320 rightclick 10869 09:23:00,480 --> 09:23:06,400 copy and scroll down 10870 09:23:03,320 --> 09:23:10,240 here and at the 10871 09:23:06,400 --> 09:23:14,320 bottom right click and 10872 09:23:10,240 --> 09:23:15,596 paste at the top let's create an array 10873 09:23:14,320 --> 09:23:23,360 called 10874 09:23:15,596 --> 09:23:25,320 nums so const nums equals array and one 10875 09:23:23,360 --> 09:23:27,640 1 and 10876 09:23:25,320 --> 09:23:30,560 three if we 10877 09:23:27,640 --> 09:23:32,596 save this will create an array where 10878 09:23:30,560 --> 09:23:35,160 each number is 10879 09:23:32,596 --> 09:23:38,040 doubled now what if we wanted to use 10880 09:23:35,160 --> 09:23:41,640 this loop on a different array other 10881 09:23:38,040 --> 09:23:44,960 than one one and three we can do that 10882 09:23:41,640 --> 09:23:49,756 using a function so let's create a 10883 09:23:44,960 --> 09:23:54,120 function around this Loop we'll create 10884 09:23:49,756 --> 09:23:58,276 function let's name it dou full 10885 09:23:54,120 --> 09:24:01,240 array brackets and curly bracket we'll 10886 09:23:58,276 --> 09:24:05,240 remove the closed curly bracket and at 10887 09:24:01,240 --> 09:24:06,436 the bottom We'll add it back and we'll 10888 09:24:05,240 --> 09:24:08,720 select these 10889 09:24:06,436 --> 09:24:12,520 lines and press 10890 09:24:08,720 --> 09:24:15,596 tab so now instead of doing this loop on 10891 09:24:12,520 --> 09:24:20,080 the same array every time we can turn 10892 09:24:15,596 --> 09:24:23,276 this into a parameter so at the top here 10893 09:24:20,080 --> 09:24:26,960 we'll create a parameter called 10894 09:24:23,276 --> 09:24:29,840 nums and we're going to delete this 10895 09:24:26,960 --> 09:24:34,000 line and we're going to Loop over this 10896 09:24:29,840 --> 09:24:39,000 parameter instead so at the bottom we 10897 09:24:34,000 --> 09:24:40,240 can call our function using double array 10898 09:24:39,000 --> 09:24:42,960 and 10899 09:24:40,240 --> 09:24:47,080 brackets and between the brackets we're 10900 09:24:42,960 --> 09:24:53,480 going to save an array into nums so 10901 09:24:47,080 --> 09:24:56,800 square bracket 1 1 and 3 if we 10902 09:24:53,480 --> 09:24:59,400 save this will do the same thing as 10903 09:24:56,800 --> 09:25:02,276 before but now because it's in a 10904 09:24:59,400 --> 09:25:06,680 function we can use this loop with any 10905 09:25:02,276 --> 09:25:11,916 array not just one one and three so at 10906 09:25:06,680 --> 09:25:18,200 the end we can do double array 10907 09:25:11,916 --> 09:25:19,276 brackets and an array 2 2 five this time 10908 09:25:18,200 --> 09:25:22,436 and 10909 09:25:19,276 --> 09:25:25,916 save and it will create an array where 10910 09:25:22,436 --> 09:25:29,840 these numbers are doubled so using a 10911 09:25:25,916 --> 09:25:32,436 function we can reuse this Loop for any 10912 09:25:29,840 --> 09:25:34,916 value another feature of using a 10913 09:25:32,436 --> 09:25:38,320 function with a loop is that we can 10914 09:25:34,916 --> 09:25:41,520 break out of this Loop using a return 10915 09:25:38,320 --> 09:25:44,840 instead so first let's actually use a 10916 09:25:41,520 --> 09:25:47,640 return in this function instead of 10917 09:25:44,840 --> 09:25:52,320 console.log we're going to 10918 09:25:47,640 --> 09:25:52,320 return nums doubled 10919 09:25:52,880 --> 09:25:59,040 and here we're going to console.log the 10920 09:25:56,040 --> 09:25:59,040 result 10921 09:26:01,640 --> 09:26:05,960 console.log and 10922 09:26:08,720 --> 09:26:14,080 console.log and 10923 09:26:11,040 --> 09:26:17,276 save so this does the same thing as 10924 09:26:14,080 --> 09:26:20,200 before except we're returning the result 10925 09:26:17,276 --> 09:26:22,800 and console. logging outside of the 10926 09:26:20,200 --> 09:26:25,240 function now let's say that if we see 10927 09:26:22,800 --> 09:26:29,880 the number zero we want to stop this 10928 09:26:25,240 --> 09:26:35,800 Loop early to do that we can click here 10929 09:26:29,880 --> 09:26:39,800 and check if num is equal to 10930 09:26:35,800 --> 09:26:42,640 zero and if it is we can break out of 10931 09:26:39,800 --> 09:26:45,080 this Loop now when we're inside a 10932 09:26:42,640 --> 09:26:48,276 function we can also use a return 10933 09:26:45,080 --> 09:26:53,680 statement to break out of this Loop so 10934 09:26:48,276 --> 09:26:56,756 we can also do return nums 10935 09:26:53,680 --> 09:26:59,680 doubled so a return statement will end 10936 09:26:56,756 --> 09:27:01,800 this function immediately so this is 10937 09:26:59,680 --> 09:27:06,120 another way of ending the 10938 09:27:01,800 --> 09:27:10,040 loop so down here if we add the number 10939 09:27:06,120 --> 09:27:12,916 zero and then five and 10940 09:27:10,040 --> 09:27:16,480 save you'll notice that the loop stops 10941 09:27:12,916 --> 09:27:19,320 when we see the number zero so return is 10942 09:27:16,480 --> 09:27:22,436 another way of breaking out of a loop 10943 09:27:19,320 --> 09:27:25,200 when it's inside a function and that's 10944 09:27:22,436 --> 09:27:28,000 the end of this lesson in this lesson we 10945 09:27:25,200 --> 09:27:31,480 learned about arrays which represent a 10946 09:27:28,000 --> 09:27:34,520 list of values we learned about loops 10947 09:27:31,480 --> 09:27:37,240 which let us run some code over and over 10948 09:27:34,520 --> 09:27:39,120 just like a loop we learned a Technique 10949 09:27:37,240 --> 09:27:41,596 we can use with loops called the 10950 09:27:39,120 --> 09:27:44,596 accumulator pattern and we used it to 10951 09:27:41,596 --> 09:27:47,000 create a to-do list project we learned 10952 09:27:44,596 --> 09:27:49,800 that arrays are references and the 10953 09:27:47,000 --> 09:27:53,040 destructuring shortcut and we learned 10954 09:27:49,800 --> 09:27:56,560 more features of Loops like break 10955 09:27:53,040 --> 09:27:59,200 continue and using Loops inside a 10956 09:27:56,560 --> 09:28:03,960 function here are some exercises to help 10957 09:27:59,200 --> 09:28:03,960 you practice arrays and loops 10958 09:28:29,480 --> 09:28:34,240 in this lesson we're going to learn some 10959 09:28:31,480 --> 09:28:36,240 Advanced features of functions and we're 10960 09:28:34,240 --> 09:28:37,320 going to use them to improve our 10961 09:28:36,240 --> 09:28:40,560 previous 10962 09:28:37,320 --> 09:28:43,800 projects first let's create a new file 10963 09:28:40,560 --> 09:28:47,640 for this lesson we'll go to our code 10964 09:28:43,800 --> 09:28:51,320 editor and click here to show our files 10965 09:28:47,640 --> 09:28:53,880 and click here to create a new file and 10966 09:28:51,320 --> 09:28:58,160 we'll name this 10967 09:28:53,880 --> 09:28:59,800 12- Advanced D 10968 09:28:58,160 --> 09:29:02,520 functions. 10969 09:28:59,800 --> 09:29:06,680 HTML and press 10970 09:29:02,520 --> 09:29:09,880 enter now let's copy 11 arrays and Loops 10971 09:29:06,680 --> 09:29:14,120 into this file so we'll click here 10972 09:29:09,880 --> 09:29:16,160 control a or command a and then 10973 09:29:14,120 --> 09:29:18,916 rightclick 10974 09:29:16,160 --> 09:29:24,040 copy and in 10975 09:29:18,916 --> 09:29:24,040 here right click and paste 10976 09:29:24,120 --> 09:29:30,040 and we can close this for now and let's 10977 09:29:27,320 --> 09:29:31,640 prepare this file for this lesson so 10978 09:29:30,040 --> 09:29:36,756 we'll scroll 10979 09:29:31,640 --> 09:29:38,960 up and change the title to 10980 09:29:36,756 --> 09:29:41,640 Advanced 10981 09:29:38,960 --> 09:29:44,916 functions and let's remove all the 10982 09:29:41,640 --> 09:29:44,916 JavaScript code from 10983 09:29:45,640 --> 09:29:52,560 before and now let's save and open this 10984 09:29:49,680 --> 09:29:55,960 file in the browser by right click 10985 09:29:52,560 --> 09:29:58,756 clicking open with live 10986 09:29:55,960 --> 09:30:01,080 server and let's also open the console 10987 09:29:58,756 --> 09:30:03,040 on this page so we'll 10988 09:30:01,080 --> 09:30:06,000 rightclick 10989 09:30:03,040 --> 09:30:08,480 inspect and open the 10990 09:30:06,000 --> 09:30:10,756 console and now we can close the 10991 09:30:08,480 --> 09:30:15,120 previous 10992 09:30:10,756 --> 09:30:15,120 tabs as well as the previous 10993 09:30:16,000 --> 09:30:21,640 files and now we're ready to begin so 10994 09:30:19,596 --> 09:30:22,520 first we're going to review how to use 10995 09:30:21,640 --> 09:30:25,480 fun 10996 09:30:22,520 --> 09:30:29,080 functions inside the script element 10997 09:30:25,480 --> 09:30:32,720 let's create a function using the word 10998 09:30:29,080 --> 09:30:36,960 function will give it a name 10999 09:30:32,720 --> 09:30:41,960 greeting brackets and curly brackets and 11000 09:30:36,960 --> 09:30:41,960 inside the function we'll type the code 11001 09:30:42,400 --> 09:30:49,360 console.log the string 11002 09:30:45,960 --> 09:30:52,960 hello so this is a function and it lets 11003 09:30:49,360 --> 09:30:56,080 us reuse code to run the code inside 11004 09:30:52,960 --> 09:31:00,880 this function we can type the function 11005 09:30:56,080 --> 09:31:03,160 name which is greeing and then 11006 09:31:00,880 --> 09:31:06,000 brackets if we 11007 09:31:03,160 --> 09:31:09,680 save this line of code we'll run the 11008 09:31:06,000 --> 09:31:12,680 code inside the function and display 11009 09:31:09,680 --> 09:31:15,240 hello this is known as calling the 11010 09:31:12,680 --> 09:31:17,560 function or running the 11011 09:31:15,240 --> 09:31:19,520 function so now that we understand 11012 09:31:17,560 --> 09:31:22,840 functions we're going to learn an 11013 09:31:19,520 --> 09:31:26,596 advanced feature of functions which is 11014 09:31:22,840 --> 09:31:29,200 functions are values so we learned many 11015 09:31:26,596 --> 09:31:32,640 types of values in this course like 11016 09:31:29,200 --> 09:31:36,680 numbers strings and booleans and we can 11017 09:31:32,640 --> 09:31:39,276 save a value in a variable for example 11018 09:31:36,680 --> 09:31:45,520 let's create some new lines and we'll 11019 09:31:39,276 --> 09:31:49,840 create a variable const num equal 11020 09:31:45,520 --> 09:31:52,200 2 so this will save the value two inside 11021 09:31:49,840 --> 09:31:55,400 the variable num 11022 09:31:52,200 --> 09:31:58,360 now functions are also values and this 11023 09:31:55,400 --> 09:32:01,800 means just like a number we can save a 11024 09:31:58,360 --> 09:32:05,080 function inside a variable so let's give 11025 09:32:01,800 --> 09:32:06,520 it a try we'll create a new line and 11026 09:32:05,080 --> 09:32:11,000 type 11027 09:32:06,520 --> 09:32:14,360 const function one and we'll make this 11028 09:32:11,000 --> 09:32:16,040 equal to a function this time so we'll 11029 09:32:14,360 --> 09:32:19,200 just type 11030 09:32:16,040 --> 09:32:22,520 function and a name 11031 09:32:19,200 --> 09:32:25,480 greeting brackets and curly 11032 09:32:22,520 --> 09:32:27,000 brackets and inside the function we'll 11033 09:32:25,480 --> 09:32:31,960 type 11034 09:32:27,000 --> 09:32:35,520 console. log the string hello 11035 09:32:31,960 --> 09:32:37,276 2 and we'll put a semicolon at the end 11036 09:32:35,520 --> 09:32:40,916 since we're saving in a 11037 09:32:37,276 --> 09:32:43,400 variable so this variable now contains a 11038 09:32:40,916 --> 09:32:46,080 function we're using the exact same 11039 09:32:43,400 --> 09:32:48,720 syntax as before except we're just 11040 09:32:46,080 --> 09:32:52,800 taking this function and saving it in a 11041 09:32:48,720 --> 09:32:55,080 variable because functions are values 11042 09:32:52,800 --> 09:32:59,000 so now that this function is inside this 11043 09:32:55,080 --> 09:33:02,080 variable let's actually console.log it 11044 09:32:59,000 --> 09:33:02,080 so we 11045 09:33:02,520 --> 09:33:10,040 console.log function one and 11046 09:33:07,596 --> 09:33:13,240 save and this will show us that the 11047 09:33:10,040 --> 09:33:14,240 variable function one actually contains 11048 09:33:13,240 --> 09:33:17,800 this 11049 09:33:14,240 --> 09:33:21,720 function now on a new line we can also 11050 09:33:17,800 --> 09:33:21,720 do console.log 11051 09:33:21,840 --> 09:33:31,520 and check the type of function one and 11052 09:33:28,840 --> 09:33:35,080 save and it will tell us that the type 11053 09:33:31,520 --> 09:33:38,240 of this variable is a function so as you 11054 09:33:35,080 --> 09:33:42,040 can see a function is just another type 11055 09:33:38,240 --> 09:33:44,960 of value and we can save it in a 11056 09:33:42,040 --> 09:33:46,200 variable we can also run the function 11057 09:33:44,960 --> 09:33:49,960 inside this 11058 09:33:46,200 --> 09:33:53,840 variable by typing the variable name 11059 09:33:49,960 --> 09:33:56,720 function 1 and because this contains a 11060 09:33:53,840 --> 09:33:58,040 function we can actually just call it 11061 09:33:56,720 --> 09:34:01,080 using 11062 09:33:58,040 --> 09:34:05,080 brackets so this will run the code 11063 09:34:01,080 --> 09:34:07,000 inside this function if we save it 11064 09:34:05,080 --> 09:34:09,916 displays hello 11065 09:34:07,000 --> 09:34:13,240 to now let's do some Cleanup in this 11066 09:34:09,916 --> 09:34:16,160 code in JavaScript as long as there's a 11067 09:34:13,240 --> 09:34:20,200 way to access a function this function 11068 09:34:16,160 --> 09:34:23,240 name is not needed so here we can access 11069 09:34:20,200 --> 09:34:25,596 this function using this variable so 11070 09:34:23,240 --> 09:34:29,160 this function name is not needed and we 11071 09:34:25,596 --> 09:34:32,000 can actually just delete it like this 11072 09:34:29,160 --> 09:34:34,640 and now this is called an anonymous 11073 09:34:32,000 --> 09:34:35,596 function which means a function without 11074 09:34:34,640 --> 09:34:38,640 a 11075 09:34:35,596 --> 09:34:40,436 name now an interesting thing to know is 11076 09:34:38,640 --> 09:34:43,596 that the function syntax that we've been 11077 09:34:40,436 --> 09:34:47,240 using this entire course up here is 11078 09:34:43,596 --> 09:34:50,000 actually a shortcut for this code now 11079 09:34:47,240 --> 09:34:53,040 even though this syntax uses VAR and we 11080 09:34:50,000 --> 09:34:55,916 usually don't recommend VAR this syntax 11081 09:34:53,040 --> 09:34:59,160 actually has a couple advantages number 11082 09:34:55,916 --> 09:35:02,160 one it's easier to read and number two 11083 09:34:59,160 --> 09:35:05,160 it gives us a feature called hoisting 11084 09:35:02,160 --> 09:35:08,916 which means we can call this function 11085 09:35:05,160 --> 09:35:11,880 greeting brackets before we create it in 11086 09:35:08,916 --> 09:35:14,720 the code so using hoisting we don't have 11087 09:35:11,880 --> 09:35:17,320 to worry about what order we write this 11088 09:35:14,720 --> 09:35:19,436 code hoisting doesn't work when we 11089 09:35:17,320 --> 09:35:23,360 create our function and save it in a 11090 09:35:19,436 --> 09:35:26,200 variable like this so in JavaScript 11091 09:35:23,360 --> 09:35:29,360 functions are values and just like any 11092 09:35:26,200 --> 09:35:32,480 other value we can save it in a 11093 09:35:29,360 --> 09:35:35,880 variable now let's think what else can 11094 09:35:32,480 --> 09:35:39,756 we do with a value well we can save a 11095 09:35:35,880 --> 09:35:40,680 value in an object for example let's 11096 09:35:39,756 --> 09:35:44,400 scroll 11097 09:35:40,680 --> 09:35:47,320 down and create some new lines and we'll 11098 09:35:44,400 --> 09:35:52,840 create an object 11099 09:35:47,320 --> 09:35:55,840 const object 1 equals curly 11100 09:35:52,840 --> 09:36:00,200 brackets and inside this object we'll 11101 09:35:55,840 --> 09:36:04,040 have a property num with the value 11102 09:36:00,200 --> 09:36:07,116 two so this saves the number two inside 11103 09:36:04,040 --> 09:36:10,240 this object now because functions are 11104 09:36:07,116 --> 09:36:14,320 values we can also save a function 11105 09:36:10,240 --> 09:36:18,560 inside this object so we'll type comma 11106 09:36:14,320 --> 09:36:21,690 and then a property fun colon and we'll 11107 09:36:18,560 --> 09:36:23,916 create a function this time function 11108 09:36:21,690 --> 09:36:27,756 [Music] 11109 09:36:23,916 --> 09:36:30,436 greeting brackets and curly brackets and 11110 09:36:27,756 --> 09:36:36,160 inside we type 11111 09:36:30,436 --> 09:36:40,320 console. log the string hello 11112 09:36:36,160 --> 09:36:44,160 3 so this saves a function inside this 11113 09:36:40,320 --> 09:36:46,840 object under the property fun now to 11114 09:36:44,160 --> 09:36:53,240 access this function we just use the dot 11115 09:36:46,840 --> 09:36:57,000 notation as normal so object one dot 11116 09:36:53,240 --> 09:36:59,880 fun and because this value is a function 11117 09:36:57,000 --> 09:37:02,200 we can call it using 11118 09:36:59,880 --> 09:37:05,240 brackets if we 11119 09:37:02,200 --> 09:37:10,276 save this will run the code inside the 11120 09:37:05,240 --> 09:37:13,200 function and display hello 3 so remember 11121 09:37:10,276 --> 09:37:16,116 this is called a method when we have a 11122 09:37:13,200 --> 09:37:19,276 function saved inside an 11123 09:37:16,116 --> 09:37:22,200 object again as long as there's a way to 11124 09:37:19,276 --> 09:37:25,880 access this function for example using 11125 09:37:22,200 --> 09:37:29,640 object one. fun then we don't need this 11126 09:37:25,880 --> 09:37:31,400 function name so we'll delete it and use 11127 09:37:29,640 --> 09:37:34,480 an anonymous 11128 09:37:31,400 --> 09:37:38,160 function now let's think what else can 11129 09:37:34,480 --> 09:37:43,200 we do with a value we can pass a value 11130 09:37:38,160 --> 09:37:46,320 into a function for example down here 11131 09:37:43,200 --> 09:37:47,840 let's create a function 11132 09:37:46,320 --> 09:37:51,680 function 11133 09:37:47,840 --> 09:37:54,720 display brackets and curly brackets and 11134 09:37:51,680 --> 09:37:57,436 we'll give this function a parameter so 11135 09:37:54,720 --> 09:37:59,276 in here we'll create a parameter and 11136 09:37:57,436 --> 09:38:02,520 name it 11137 09:37:59,276 --> 09:38:08,200 param and inside the function we can use 11138 09:38:02,520 --> 09:38:08,200 a parameter just like a variable so we 11139 09:38:08,240 --> 09:38:14,800 console.log the 11140 09:38:11,200 --> 09:38:17,680 param and then down here let's call this 11141 09:38:14,800 --> 09:38:21,960 function using 11142 09:38:17,680 --> 09:38:24,880 display brackets and two 11143 09:38:21,960 --> 09:38:27,720 this will take the value two save it 11144 09:38:24,880 --> 09:38:30,916 inside Pam and then we can use it just 11145 09:38:27,720 --> 09:38:34,520 like a variable if we 11146 09:38:30,916 --> 09:38:38,160 save this will display two in the 11147 09:38:34,520 --> 09:38:42,080 console so this is known as passing a 11148 09:38:38,160 --> 09:38:45,756 value into a function now because 11149 09:38:42,080 --> 09:38:49,200 functions are values we can also pass a 11150 09:38:45,756 --> 09:38:52,040 function into a function so let's give 11151 09:38:49,200 --> 09:38:55,276 it a try down here 11152 09:38:52,040 --> 09:38:59,916 we'll create a new function 11153 09:38:55,276 --> 09:39:02,520 function run brackets and curly 11154 09:38:59,916 --> 09:39:06,160 brackets and this function will also 11155 09:39:02,520 --> 09:39:10,116 have a parameter let's call it 11156 09:39:06,160 --> 09:39:13,200 param and down here we'll call this 11157 09:39:10,116 --> 09:39:16,000 function run 11158 09:39:13,200 --> 09:39:18,840 brackets and between the brackets we're 11159 09:39:16,000 --> 09:39:21,756 going to give it a function this time so 11160 09:39:18,840 --> 09:39:25,080 we'll type function 11161 09:39:21,756 --> 09:39:29,080 brackets and curly brackets and inside 11162 09:39:25,080 --> 09:39:29,080 the function we'll type 11163 09:39:29,276 --> 09:39:37,916 console.log the string hello 11164 09:39:34,000 --> 09:39:42,400 4 so this will take this function and 11165 09:39:37,916 --> 09:39:46,040 save it inside Pam and now up here we 11166 09:39:42,400 --> 09:39:47,800 can use Pam just like a variable so we 11167 09:39:46,040 --> 09:39:52,040 can type 11168 09:39:47,800 --> 09:39:55,596 Pam and because Pam contains a function 11169 09:39:52,040 --> 09:39:57,840 we can actually just call it using 11170 09:39:55,596 --> 09:40:01,360 brackets if we 11171 09:39:57,840 --> 09:40:04,276 save this code will run the function 11172 09:40:01,360 --> 09:40:08,480 that we created down here and display 11173 09:40:04,276 --> 09:40:11,080 hello 4 so this is called passing a 11174 09:40:08,480 --> 09:40:13,520 function into another 11175 09:40:11,080 --> 09:40:17,840 function the function that we're passing 11176 09:40:13,520 --> 09:40:21,436 in is called a callback function or a 11177 09:40:17,840 --> 09:40:24,756 callback so to summarize functions are 11178 09:40:21,436 --> 09:40:26,840 values anything we can do with a value 11179 09:40:24,756 --> 09:40:29,400 we can also do with a 11180 09:40:26,840 --> 09:40:33,116 function now let's do some practical 11181 09:40:29,400 --> 09:40:35,200 examples using functions as values we're 11182 09:40:33,116 --> 09:40:39,040 going to learn a feature of JavaScript 11183 09:40:35,200 --> 09:40:41,680 called set timeout set timeout allows us 11184 09:40:39,040 --> 09:40:44,436 to run a function in the 11185 09:40:41,680 --> 09:40:48,320 future first we'll comment out the 11186 09:40:44,436 --> 09:40:51,596 previous code using 11187 09:40:48,320 --> 09:40:55,080 slashstar and at the bottom 11188 09:40:51,596 --> 09:40:58,400 star slash and let's create some new 11189 09:40:55,080 --> 09:41:02,360 lines and we're going to type the code 11190 09:40:58,400 --> 09:41:05,916 set capital T time 11191 09:41:02,360 --> 09:41:10,000 out so set timeout is a built-in 11192 09:41:05,916 --> 09:41:14,080 function so we can type brackets to call 11193 09:41:10,000 --> 09:41:17,160 this function Now set timeout takes two 11194 09:41:14,080 --> 09:41:19,560 parameters inside these brackets the 11195 09:41:17,160 --> 09:41:22,276 first parameter is a function that we 11196 09:41:19,560 --> 09:41:24,800 want to run in the future 11197 09:41:22,276 --> 09:41:29,040 let's create a 11198 09:41:24,800 --> 09:41:33,756 function brackets and curly brackets and 11199 09:41:29,040 --> 09:41:33,756 inside this function let's type 11200 09:41:33,960 --> 09:41:42,080 console.log the string time 11201 09:41:38,436 --> 09:41:45,680 out so here we're using a function as a 11202 09:41:42,080 --> 09:41:49,480 value and we're passing it into another 11203 09:41:45,680 --> 09:41:52,116 function at the end we'll type a comma 11204 09:41:49,480 --> 09:41:54,800 and we'll give set timeout a second 11205 09:41:52,116 --> 09:41:57,360 parameter the second parameter is how 11206 09:41:54,800 --> 09:42:00,320 long to wait before running this 11207 09:41:57,360 --> 09:42:03,200 function so this is a number in 11208 09:42:00,320 --> 09:42:08,240 milliseconds 1,000 milliseconds is equal 11209 09:42:03,200 --> 09:42:12,000 to 1 second so if we type 3,000 11210 09:42:08,240 --> 09:42:15,520 milliseconds this is equal to 3 seconds 11211 09:42:12,000 --> 09:42:19,116 so set timeout will wait for 3 seconds 11212 09:42:15,520 --> 09:42:21,640 and then call this function if we 11213 09:42:19,116 --> 09:42:24,080 save after 3 seconds 11214 09:42:21,640 --> 09:42:28,040 it's going to display timeout in the 11215 09:42:24,080 --> 09:42:30,160 console so that's set timeout we give it 11216 09:42:28,040 --> 09:42:32,960 a function and it will call this 11217 09:42:30,160 --> 09:42:35,360 function after a certain amount of 11218 09:42:32,960 --> 09:42:38,160 time so now we're going to learn an 11219 09:42:35,360 --> 09:42:44,916 important concept called a synchronous 11220 09:42:38,160 --> 09:42:44,916 code so after set timeout let's type 11221 09:42:45,000 --> 09:42:52,560 console.log the string next 11222 09:42:49,436 --> 09:42:54,960 line and Save 11223 09:42:52,560 --> 09:42:57,720 so notice that it displays next line 11224 09:42:54,960 --> 09:43:00,436 first and then after 3 seconds it 11225 09:42:57,720 --> 09:43:04,116 displays timeout so what's happening 11226 09:43:00,436 --> 09:43:07,436 here why are they reversed set timeout 11227 09:43:04,116 --> 09:43:10,560 sets up a timer and after 3 seconds it 11228 09:43:07,436 --> 09:43:12,800 will call this function however it 11229 09:43:10,560 --> 09:43:15,960 doesn't actually wait for the 3 seconds 11230 09:43:12,800 --> 09:43:18,756 to finish it just sets up the timer and 11231 09:43:15,960 --> 09:43:22,200 immediately goes to the next line so 11232 09:43:18,756 --> 09:43:25,436 that's why this line is displayed first 11233 09:43:22,200 --> 09:43:27,880 so this is called asynchronous code 11234 09:43:25,436 --> 09:43:30,596 asynchronous code means the computer 11235 09:43:27,880 --> 09:43:33,520 won't wait for this line to finish 11236 09:43:30,596 --> 09:43:36,480 before going on to the next line so for 11237 09:43:33,520 --> 09:43:39,640 set timeout it'll just set up the timer 11238 09:43:36,480 --> 09:43:42,080 and then immediately go to the next line 11239 09:43:39,640 --> 09:43:43,916 synchronous code on the other hand means 11240 09:43:42,080 --> 09:43:47,560 the computer will wait for one line to 11241 09:43:43,916 --> 09:43:49,116 finish before going to the next line so 11242 09:43:47,560 --> 09:43:53,160 all the code we've written in this 11243 09:43:49,116 --> 09:43:56,000 course so far has been synchronous code 11244 09:43:53,160 --> 09:43:59,680 in addition all the code inside this 11245 09:43:56,000 --> 09:44:02,880 function is also synchronous code so if 11246 09:43:59,680 --> 09:44:02,880 we added 11247 09:44:02,916 --> 09:44:09,756 console.log the string time out 11248 09:44:07,560 --> 09:44:12,916 to and 11249 09:44:09,756 --> 09:44:16,160 save the code inside the function will 11250 09:44:12,916 --> 09:44:18,560 still run line by line it will wait for 11251 09:44:16,160 --> 09:44:21,720 each line to finish before going to the 11252 09:44:18,560 --> 09:44:24,916 next line and after 3 seconds it will 11253 09:44:21,720 --> 09:44:28,116 display timeout followed by timeout 11254 09:44:24,916 --> 09:44:30,960 2 so don't worry all the code that we 11255 09:44:28,116 --> 09:44:34,560 normally write will be synchronous code 11256 09:44:30,960 --> 09:44:37,360 it will run line by line as usual the 11257 09:44:34,560 --> 09:44:39,320 only time that code becomes asynchronous 11258 09:44:37,360 --> 09:44:42,756 is when we use certain features of 11259 09:44:39,320 --> 09:44:45,756 JavaScript like set timeout so set 11260 09:44:42,756 --> 09:44:48,160 timeout is the only part that is 11261 09:44:45,756 --> 09:44:50,720 asynchronous now the advantage of set 11262 09:44:48,160 --> 09:44:52,880 timeout being asynchronous is that it 11263 09:44:50,720 --> 09:44:55,840 does doesn't block our code for 3 11264 09:44:52,880 --> 09:44:57,520 seconds waiting for this timer to finish 11265 09:44:55,840 --> 09:44:59,840 it just sets up the timer in the 11266 09:44:57,520 --> 09:45:02,596 background and then goes to the next 11267 09:44:59,840 --> 09:45:04,960 line of code so this allows our code to 11268 09:45:02,596 --> 09:45:07,320 do other things while this timer is 11269 09:45:04,960 --> 09:45:09,960 running in the background and this is 11270 09:45:07,320 --> 09:45:12,360 similar to real life if you set up a 11271 09:45:09,960 --> 09:45:14,520 timer on your alarm clock you're not 11272 09:45:12,360 --> 09:45:16,720 going to stop and wait for the timer to 11273 09:45:14,520 --> 09:45:19,800 finish you're going to go and do 11274 09:45:16,720 --> 09:45:22,596 something else asynchronous code is the 11275 09:45:19,800 --> 09:45:24,596 exact same concept 11276 09:45:22,596 --> 09:45:28,080 next we're going to learn a similar 11277 09:45:24,596 --> 09:45:31,756 function to set timeout called set 11278 09:45:28,080 --> 09:45:36,596 interval so at the bottom let's type the 11279 09:45:31,756 --> 09:45:39,360 code set capital I 11280 09:45:36,596 --> 09:45:42,080 interval and set interval is another 11281 09:45:39,360 --> 09:45:43,720 built-in function so we can call it 11282 09:45:42,080 --> 09:45:46,640 using 11283 09:45:43,720 --> 09:45:50,000 brackets and set interval takes these 11284 09:45:46,640 --> 09:45:52,200 same two parameters as set timeout the 11285 09:45:50,000 --> 09:45:56,560 first parameter is a function that we 11286 09:45:52,200 --> 09:46:00,436 want to run in the future so let's type 11287 09:45:56,560 --> 09:46:03,160 function brackets and curly brackets and 11288 09:46:00,436 --> 09:46:08,916 inside we'll type 11289 09:46:03,160 --> 09:46:08,916 console. log the string 11290 09:46:09,160 --> 09:46:15,436 interval now let's add a comma and the 11291 09:46:12,596 --> 09:46:18,200 second parameter is also a number in 11292 09:46:15,436 --> 09:46:21,916 milliseconds so we'll type 3,000 11293 09:46:18,200 --> 09:46:24,560 milliseconds or 3 seconds so set 11294 09:46:21,916 --> 09:46:27,276 interval will run this function 3 11295 09:46:24,560 --> 09:46:30,200 seconds in the future but this time it 11296 09:46:27,276 --> 09:46:32,756 will keep running the function every 3 11297 09:46:30,200 --> 09:46:36,400 seconds if we 11298 09:46:32,756 --> 09:46:39,200 save in the console every 3 seconds it 11299 09:46:36,400 --> 09:46:42,916 will run the function and display 11300 09:46:39,200 --> 09:46:48,200 interval so set interval is also 11301 09:46:42,916 --> 09:46:51,756 asynchronous below it if we type 11302 09:46:48,200 --> 09:46:53,320 console.log the string next 11303 09:46:51,756 --> 09:46:55,360 line 11304 09:46:53,320 --> 09:46:58,200 two and 11305 09:46:55,360 --> 09:47:01,000 save it will set up the interval and 11306 09:46:58,200 --> 09:47:04,400 then immediately go to the next line and 11307 09:47:01,000 --> 09:47:07,480 display next line two so that's set 11308 09:47:04,400 --> 09:47:10,000 interval we give it a function and it 11309 09:47:07,480 --> 09:47:13,520 will keep running this function after a 11310 09:47:10,000 --> 09:47:16,840 certain period of time so now let's do a 11311 09:47:13,520 --> 09:47:19,520 project using what we learned so far 11312 09:47:16,840 --> 09:47:21,720 let's go to our browser and we'll create 11313 09:47:19,520 --> 09:47:26,720 a new tab at the top 11314 09:47:21,720 --> 09:47:28,240 top and here let's type 11315 09:47:26,720 --> 09:47:36,116 superimple 11316 09:47:28,240 --> 09:47:38,720 dodev slash projects slash Advanced Das 11317 09:47:36,116 --> 09:47:41,800 functions and press 11318 09:47:38,720 --> 09:47:43,960 enter so this is basically the rock 11319 09:47:41,800 --> 09:47:47,116 paper scissors game that we created 11320 09:47:43,960 --> 09:47:50,480 earlier except this time it has an extra 11321 09:47:47,116 --> 09:47:51,276 button called autop playay if we click 11322 09:47:50,480 --> 09:47:54,320 this Buton 11323 09:47:51,276 --> 09:47:55,436 button every second the computer will 11324 09:47:54,320 --> 09:47:58,000 play by 11325 09:47:55,436 --> 09:47:59,800 itself so this feature will help us 11326 09:47:58,000 --> 09:48:02,800 practice using set 11327 09:47:59,800 --> 09:48:06,360 interval so first let's make a copy of 11328 09:48:02,800 --> 09:48:09,916 the project files we'll go into our code 11329 09:48:06,360 --> 09:48:13,916 editor and click this icon to show our 11330 09:48:09,916 --> 09:48:17,596 files and let's make a copy of 10 rock 11331 09:48:13,916 --> 09:48:20,960 paper scissors for this lesson so we'll 11332 09:48:17,596 --> 09:48:25,640 rightclick copy 11333 09:48:20,960 --> 09:48:29,640 and down here rightclick and 11334 09:48:25,640 --> 09:48:32,360 paste and I'll stop this for now so here 11335 09:48:29,640 --> 09:48:34,480 we're going to change the name by right 11336 09:48:32,360 --> 09:48:38,680 clicking 11337 09:48:34,480 --> 09:48:42,080 rename we'll remove copy at the end and 11338 09:48:38,680 --> 09:48:43,916 at the beginning we'll change it to 12 11339 09:48:42,080 --> 09:48:47,436 and press 11340 09:48:43,916 --> 09:48:50,916 enter and let's also make a copy of the 11341 09:48:47,436 --> 09:48:54,560 JavaScript and CSS files so we'll click 11342 09:48:50,916 --> 09:48:58,040 this file and right click 11343 09:48:54,560 --> 09:48:59,960 copy and then click the folder right 11344 09:48:58,040 --> 09:49:03,400 click 11345 09:48:59,960 --> 09:49:05,240 paste and we'll rename this file 11346 09:49:03,400 --> 09:49:09,400 rightclick 11347 09:49:05,240 --> 09:49:12,880 rename remove copy at the end and at the 11348 09:49:09,400 --> 09:49:15,756 beginning change it to 12 and press 11349 09:49:12,880 --> 09:49:21,756 enter and let's also make a copy of the 11350 09:49:15,756 --> 09:49:26,276 CSS file select it rightclick copy 11351 09:49:21,756 --> 09:49:30,436 click the folder right click and 11352 09:49:26,276 --> 09:49:34,720 paste and right click 11353 09:49:30,436 --> 09:49:38,756 rename remove copy and at the front 11354 09:49:34,720 --> 09:49:42,640 change it to 12 and press 11355 09:49:38,756 --> 09:49:44,160 enter now let's scroll down to 12 rock 11356 09:49:42,640 --> 09:49:47,160 paper 11357 09:49:44,160 --> 09:49:50,360 scissors and I'll close this for 11358 09:49:47,160 --> 09:49:54,720 now and inside the head let's change 11359 09:49:50,360 --> 09:49:56,116 this to load 12 rock paper scissors and 11360 09:49:54,720 --> 09:49:59,360 we'll scroll to the 11361 09:49:56,116 --> 09:50:03,360 bottom and here instead of the lesson 10 11362 09:49:59,360 --> 09:50:07,240 file we'll load The Lesson 12 Javascript 11363 09:50:03,360 --> 09:50:09,240 file let's save and we'll open this in 11364 09:50:07,240 --> 09:50:12,436 the browser by 11365 09:50:09,240 --> 09:50:16,080 right-clicking open with live 11366 09:50:12,436 --> 09:50:18,880 server so now we're ready to begin let's 11367 09:50:16,080 --> 09:50:20,840 go back to superimple 11368 09:50:18,880 --> 09:50:24,240 dodev and the first thing thing we're 11369 09:50:20,840 --> 09:50:26,840 going to create is this autoplay button 11370 09:50:24,240 --> 09:50:30,276 so beside the reset score button which 11371 09:50:26,840 --> 09:50:34,960 is here we're going to create a new 11372 09:50:30,276 --> 09:50:38,800 button less than button greater than and 11373 09:50:34,960 --> 09:50:40,800 inside we'll have the text auto 11374 09:50:38,800 --> 09:50:43,800 play let's 11375 09:50:40,800 --> 09:50:47,640 save and go back to our 11376 09:50:43,800 --> 09:50:50,840 Tab and now we have this button so next 11377 09:50:47,640 --> 09:50:52,436 let's style this button with CSS so 11378 09:50:50,840 --> 09:50:56,916 we'll give it a 11379 09:50:52,436 --> 09:50:59,520 class class equals 11380 09:50:56,916 --> 09:51:01,596 auto-play Das 11381 09:50:59,520 --> 09:51:06,000 button and 11382 09:51:01,596 --> 09:51:08,640 save and now let's open the CSS 11383 09:51:06,000 --> 09:51:11,480 file I'm going to take this file and 11384 09:51:08,640 --> 09:51:14,880 then drag it to the bottom so I can see 11385 09:51:11,480 --> 09:51:18,840 it side by side so I'll close this for 11386 09:51:14,880 --> 09:51:21,400 now and rearrange this a little bit and 11387 09:51:18,840 --> 09:51:23,756 then down here and we're going to style 11388 09:51:21,400 --> 09:51:25,916 the autop playay button so let's just 11389 09:51:23,756 --> 09:51:29,640 make the style the same as the reset 11390 09:51:25,916 --> 09:51:33,880 score button so in the CSS we'll scroll 11391 09:51:29,640 --> 09:51:37,360 down to the reset score button Styles so 11392 09:51:33,880 --> 09:51:40,040 we'll add a comma at the end and type 11393 09:51:37,360 --> 09:51:44,040 dot 11394 09:51:40,040 --> 09:51:46,720 auto-play dasb button so now these 11395 09:51:44,040 --> 09:51:50,320 Styles will apply to the class reset 11396 09:51:46,720 --> 09:51:51,436 score button and the auto playay button 11397 09:51:50,320 --> 09:51:55,756 if if we 11398 09:51:51,436 --> 09:51:58,680 save now we styled our project so next 11399 09:51:55,756 --> 09:52:03,916 let's make this button interactive so at 11400 09:51:58,680 --> 09:52:06,320 the top here we'll add an onclick 11401 09:52:03,916 --> 09:52:09,276 attribute and then in here we're going 11402 09:52:06,320 --> 09:52:11,436 to run some JavaScript code let's 11403 09:52:09,276 --> 09:52:13,840 actually create a function so we can 11404 09:52:11,436 --> 09:52:18,880 separate the JavaScript into its own 11405 09:52:13,840 --> 09:52:21,276 file so we'll run a function called auto 11406 09:52:18,880 --> 09:52:23,756 play and 11407 09:52:21,276 --> 09:52:26,680 brackets so now let's 11408 09:52:23,756 --> 09:52:29,360 save and right now this function doesn't 11409 09:52:26,680 --> 09:52:32,436 exist so we have to create this function 11410 09:52:29,360 --> 09:52:35,520 in the Javascript file so for me the 11411 09:52:32,436 --> 09:52:37,916 file is right here I'll select it and 11412 09:52:35,520 --> 09:52:41,880 then drag it down here so I can see it 11413 09:52:37,916 --> 09:52:43,756 side by side so let's scroll down and 11414 09:52:41,880 --> 09:52:47,276 I'm going to create it just above the 11415 09:52:43,756 --> 09:52:51,720 play game function so here we'll create 11416 09:52:47,276 --> 09:52:55,960 some new lines and type function 11417 09:52:51,720 --> 09:52:59,000 auto play brackets and curly 11418 09:52:55,960 --> 09:53:02,200 brackets and now inside this function we 11419 09:52:59,000 --> 09:53:05,276 want to play the game every second so we 11420 09:53:02,200 --> 09:53:10,756 can use set interval to run some code 11421 09:53:05,276 --> 09:53:12,320 every second so let's type set 11422 09:53:10,756 --> 09:53:15,160 interval 11423 09:53:12,320 --> 09:53:18,880 brackets and inside the brackets will 11424 09:53:15,160 --> 09:53:21,520 give it a function that we want to run 11425 09:53:18,880 --> 09:53:24,480 so brackets and curly 11426 09:53:21,520 --> 09:53:27,840 brackets and then at the end let's type 11427 09:53:24,480 --> 09:53:30,596 a comma and we'll give it 11428 09:53:27,840 --> 09:53:34,560 1,000 so this means it will run this 11429 09:53:30,596 --> 09:53:37,560 function every 1,000 milliseconds or 1 11430 09:53:34,560 --> 09:53:39,640 second and now inside this function 11431 09:53:37,560 --> 09:53:42,596 we're going to automatically play the 11432 09:53:39,640 --> 09:53:46,116 game and to play the game we can use 11433 09:53:42,596 --> 09:53:51,360 this function right here play game so 11434 09:53:46,116 --> 09:53:53,640 here we'll type play game 11435 09:53:51,360 --> 09:53:57,200 brackets now we need to provide this 11436 09:53:53,640 --> 09:53:59,880 function with a Player move and remember 11437 09:53:57,200 --> 09:54:03,880 we have another function pick computer 11438 09:53:59,880 --> 09:54:08,400 move which will give us a random move so 11439 09:54:03,880 --> 09:54:09,916 above this line let's create a variable 11440 09:54:08,400 --> 09:54:14,200 const 11441 09:54:09,916 --> 09:54:18,960 Player move and we'll make it equal and 11442 09:54:14,200 --> 09:54:21,200 we'll pick a random move using pick 11443 09:54:18,960 --> 09:54:22,720 computer move 11444 09:54:21,200 --> 09:54:25,360 and 11445 09:54:22,720 --> 09:54:28,240 brackets and finally we'll take this 11446 09:54:25,360 --> 09:54:32,200 move that we picked and pass it into 11447 09:54:28,240 --> 09:54:35,480 this function so Player 11448 09:54:32,200 --> 09:54:39,880 move so now when we click this button it 11449 09:54:35,480 --> 09:54:43,276 should play the game every second let's 11450 09:54:39,880 --> 09:54:46,200 save and click 11451 09:54:43,276 --> 09:54:49,480 autoplay and it works the computer is 11452 09:54:46,200 --> 09:54:51,276 playing against itself every second so 11453 09:54:49,480 --> 09:54:54,596 now let's make this button button stop 11454 09:54:51,276 --> 09:54:57,916 playing when we click it again so at the 11455 09:54:54,596 --> 09:55:01,040 top let's create a variable to keep 11456 09:54:57,916 --> 09:55:07,756 track of whether or not we're playing so 11457 09:55:01,040 --> 09:55:11,320 we use let is auto playing and at the 11458 09:55:07,756 --> 09:55:13,400 start we'll make it equal to 11459 09:55:11,320 --> 09:55:17,080 false and 11460 09:55:13,400 --> 09:55:20,756 save and now inside this function we'll 11461 09:55:17,080 --> 09:55:21,680 check using an if statement brackets and 11462 09:55:20,756 --> 09:55:24,880 curly 11463 09:55:21,680 --> 09:55:30,880 brackets and inside this condition let's 11464 09:55:24,880 --> 09:55:33,000 check if we're not playing so not is 11465 09:55:30,880 --> 09:55:36,040 auto 11466 09:55:33,000 --> 09:55:38,880 playing so if we're not playing then we 11467 09:55:36,040 --> 09:55:42,116 want to start playing the game which is 11468 09:55:38,880 --> 09:55:45,916 this code down here so let's select this 11469 09:55:42,116 --> 09:55:51,840 code and then right click 11470 09:55:45,916 --> 09:55:55,040 cut and in here right click and 11471 09:55:51,840 --> 09:55:57,160 paste and also after we start playing 11472 09:55:55,040 --> 09:56:03,520 the game we're going to change this 11473 09:55:57,160 --> 09:56:07,160 variable to true so is auto playing and 11474 09:56:03,520 --> 09:56:10,840 we'll make it equal to true because now 11475 09:56:07,160 --> 09:56:12,840 we are autop playing so now let's add 11476 09:56:10,840 --> 09:56:16,360 the else Branch to this 11477 09:56:12,840 --> 09:56:19,640 statement else curly 11478 09:56:16,360 --> 09:56:23,596 brackets so now we need to stop this 11479 09:56:19,640 --> 09:56:26,320 interval so how do we do that the answer 11480 09:56:23,596 --> 09:56:29,880 is that set interval actually returns a 11481 09:56:26,320 --> 09:56:33,800 number and this number is like an ID we 11482 09:56:29,880 --> 09:56:37,040 can use this ID to stop the interval so 11483 09:56:33,800 --> 09:56:39,960 at the front let's create a variable to 11484 09:56:37,040 --> 09:56:41,960 save this ID 11485 09:56:39,960 --> 09:56:45,480 const 11486 09:56:41,960 --> 09:56:48,276 interval ID and 11487 09:56:45,480 --> 09:56:51,276 equals however there's another problem 11488 09:56:48,276 --> 09:56:54,320 here every time we run the function 11489 09:56:51,276 --> 09:56:57,360 we're going to get a different ID so in 11490 09:56:54,320 --> 09:57:00,080 order to save the ID from last time we 11491 09:56:57,360 --> 09:57:03,596 need to put this variable outside the 11492 09:57:00,080 --> 09:57:08,080 function so let's select this and remove 11493 09:57:03,596 --> 09:57:12,480 it for now and we'll scroll up and here 11494 09:57:08,080 --> 09:57:16,436 we'll create the variable let 11495 09:57:12,480 --> 09:57:19,960 interval ID and we'll leave it undefined 11496 09:57:16,436 --> 09:57:22,840 to start and now down here we're going 11497 09:57:19,960 --> 09:57:25,880 to save the return value into this 11498 09:57:22,840 --> 09:57:30,276 variable by typing 11499 09:57:25,880 --> 09:57:33,240 interval ID equals set 11500 09:57:30,276 --> 09:57:36,720 interval and now at the bottom we're 11501 09:57:33,240 --> 09:57:40,040 going to use the interval ID to stop the 11502 09:57:36,720 --> 09:57:42,880 interval let's scroll down and to stop 11503 09:57:40,040 --> 09:57:47,436 an interval we can use a function called 11504 09:57:42,880 --> 09:57:50,840 clear interval so we'll type clear 11505 09:57:47,436 --> 09:57:52,080 capital I interval 11506 09:57:50,840 --> 09:57:54,800 and 11507 09:57:52,080 --> 09:57:57,040 brackets and between the brackets we're 11508 09:57:54,800 --> 09:58:00,596 going to give it an ID that we want to 11509 09:57:57,040 --> 09:58:02,640 stop so we'll give it this interval 11510 09:58:00,596 --> 09:58:04,320 ID 11511 09:58:02,640 --> 09:58:07,800 interval 11512 09:58:04,320 --> 09:58:11,200 ID and then at the bottom let's also 11513 09:58:07,800 --> 09:58:17,756 change is autop playing to false because 11514 09:58:11,200 --> 09:58:20,436 we just stopped it so is auto playing 11515 09:58:17,756 --> 09:58:21,680 equals false 11516 09:58:20,436 --> 09:58:25,080 and 11517 09:58:21,680 --> 09:58:27,840 save so now if we try out this 11518 09:58:25,080 --> 09:58:30,640 button it will start playing the game by 11519 09:58:27,840 --> 09:58:34,040 itself every second and if we click the 11520 09:58:30,640 --> 09:58:37,360 button again it will stop playing the 11521 09:58:34,040 --> 09:58:40,520 game so that's how we stop an interval 11522 09:58:37,360 --> 09:58:44,960 we need to save the ID and then call 11523 09:58:40,520 --> 09:58:47,160 clear interval with this ID so now I'll 11524 09:58:44,960 --> 09:58:50,800 clean up the code a little by removing 11525 09:58:47,160 --> 09:58:53,080 this extra line and Save 11526 09:58:50,800 --> 09:58:57,116 and now we're finished with this 11527 09:58:53,080 --> 09:59:00,436 feature so next let's go back to our 11528 09:58:57,116 --> 09:59:04,596 Advanced functions file so I'll click up 11529 09:59:00,436 --> 09:59:06,480 here and then at the bottom I'll open 12 11530 09:59:04,596 --> 09:59:10,240 Advanced 11531 09:59:06,480 --> 09:59:12,400 functions and hide the files for now and 11532 09:59:10,240 --> 09:59:16,080 we're going to learn some more features 11533 09:59:12,400 --> 09:59:20,400 of functions as values so let's actually 11534 09:59:16,080 --> 09:59:23,000 close the other files that we opened 11535 09:59:20,400 --> 09:59:25,080 and first let's comment out the previous 11536 09:59:23,000 --> 09:59:29,720 code with 11537 09:59:25,080 --> 09:59:33,240 slashstar and at the bottom star 11538 09:59:29,720 --> 09:59:37,800 slash let's scroll down and 11539 09:59:33,240 --> 09:59:40,880 save and let's open the tab for this 11540 09:59:37,800 --> 09:59:44,560 file so now we're going to learn another 11541 09:59:40,880 --> 09:59:47,756 way to Loop through an array first let's 11542 09:59:44,560 --> 09:59:50,916 create an array we'll type square 11543 09:59:47,756 --> 09:59:54,840 brackets and inside the array we'll have 11544 09:59:50,916 --> 10:00:00,000 the string make 11545 09:59:54,840 --> 10:00:02,200 dinner and another string wash 11546 10:00:00,000 --> 10:00:06,276 dishes and another 11547 10:00:02,200 --> 10:00:09,160 string watch 11548 10:00:06,276 --> 10:00:11,840 YouTube so in the previous lesson we 11549 10:00:09,160 --> 10:00:15,596 learned how to Loop through an array 11550 10:00:11,840 --> 10:00:18,436 using a for Loop or a while loop another 11551 10:00:15,596 --> 10:00:22,720 way to Loop through an array is using a 11552 10:00:18,436 --> 10:00:27,596 method of the array called four each so 11553 10:00:22,720 --> 10:00:31,200 at the bottom let's type dot for capital 11554 10:00:27,596 --> 10:00:34,916 E each and 11555 10:00:31,200 --> 10:00:37,680 brackets so this is the four each method 11556 10:00:34,916 --> 10:00:40,520 and it allows us to Loop through this 11557 10:00:37,680 --> 10:00:43,756 array inside these brackets we're going 11558 10:00:40,520 --> 10:00:45,720 to give for each a function so we'll 11559 10:00:43,756 --> 10:00:50,040 type 11560 10:00:45,720 --> 10:00:53,240 function brackets and curly brackets so 11561 10:00:50,040 --> 10:00:54,436 again we're just passing a function into 11562 10:00:53,240 --> 10:00:57,400 another 11563 10:00:54,436 --> 10:00:59,520 function however this function will be a 11564 10:00:57,400 --> 10:01:03,560 little different we're going to give it 11565 10:00:59,520 --> 10:01:06,360 a parameter so up here let's create a 11566 10:01:03,560 --> 10:01:09,880 parameter called 11567 10:01:06,360 --> 10:01:12,840 value so what four each does is that it 11568 10:01:09,880 --> 10:01:15,480 Loops through the array and for each of 11569 10:01:12,840 --> 10:01:19,360 these values it will save the value in 11570 10:01:15,480 --> 10:01:21,840 the parameter and run the function so 11571 10:01:19,360 --> 10:01:21,840 let's 11572 10:01:22,840 --> 10:01:27,320 console.log 11573 10:01:24,880 --> 10:01:30,756 value and 11574 10:01:27,320 --> 10:01:34,360 save so this will Loop through the array 11575 10:01:30,756 --> 10:01:37,116 and display each value in the console so 11576 10:01:34,360 --> 10:01:40,040 let's go through this step bystep so 11577 10:01:37,116 --> 10:01:43,320 it'll take the first value save it in 11578 10:01:40,040 --> 10:01:45,840 this parameter and run the function then 11579 10:01:43,320 --> 10:01:49,000 it will take the second value save it in 11580 10:01:45,840 --> 10:01:51,436 the parameter and run the function then 11581 10:01:49,000 --> 10:01:54,240 it will take the Third value save it in 11582 10:01:51,436 --> 10:01:57,080 the parameter and run the 11583 10:01:54,240 --> 10:01:59,160 function so the for each method is 11584 10:01:57,080 --> 10:02:02,040 actually the preferred way to Loop 11585 10:01:59,160 --> 10:02:05,880 through an array because this is easier 11586 10:02:02,040 --> 10:02:09,320 to read than a for Loop like this and 11587 10:02:05,880 --> 10:02:12,400 for each also has another feature we can 11588 10:02:09,320 --> 10:02:15,276 give this function a second parameter so 11589 10:02:12,400 --> 10:02:17,276 we can type comma and the second 11590 10:02:15,276 --> 10:02:20,240 parameter is the 11591 10:02:17,276 --> 10:02:23,840 index or the position of the value in 11592 10:02:20,240 --> 10:02:26,880 the array so for the first value index 11593 10:02:23,840 --> 10:02:29,800 will be zero for the second value index 11594 10:02:26,880 --> 10:02:35,080 will be one third value index will be 11595 10:02:29,800 --> 10:02:38,080 two and so on so above this line let's 11596 10:02:35,080 --> 10:02:38,080 also 11597 10:02:38,200 --> 10:02:42,880 console.log the 11598 10:02:40,640 --> 10:02:45,680 index and 11599 10:02:42,880 --> 10:02:47,680 save and this will also display the 11600 10:02:45,680 --> 10:02:50,960 index of each 11601 10:02:47,680 --> 10:02:54,240 value all right now let's do a practical 11602 10:02:50,960 --> 10:02:56,040 example using the for each method we're 11603 10:02:54,240 --> 10:02:58,560 going to go back to the to-do list 11604 10:02:56,040 --> 10:03:01,360 project that we created in the previous 11605 10:02:58,560 --> 10:03:02,640 lesson so we'll click up here to show 11606 10:03:01,360 --> 10:03:06,960 our 11607 10:03:02,640 --> 10:03:09,880 files and let's scroll up and let's open 11608 10:03:06,960 --> 10:03:13,000 11 too 11609 10:03:09,880 --> 10:03:15,480 list.js so in this code we used a for 11610 10:03:13,000 --> 10:03:18,160 Loop to display the to-do list on the 11611 10:03:15,480 --> 10:03:21,800 page we're going to switch this to using 11612 10:03:18,160 --> 10:03:24,000 a for each method instead said so first 11613 10:03:21,800 --> 10:03:27,596 let's actually make a copy of the files 11614 10:03:24,000 --> 10:03:32,916 for this project we'll click up 11615 10:03:27,596 --> 10:03:34,880 here and let's click 11 to-do list and 11616 10:03:32,916 --> 10:03:37,680 rightclick 11617 10:03:34,880 --> 10:03:41,640 copy and let's click the script 11618 10:03:37,680 --> 10:03:45,320 folder rightclick and 11619 10:03:41,640 --> 10:03:47,680 paste and now we'll rename this by 11620 10:03:45,320 --> 10:03:51,240 right-clicking 11621 10:03:47,680 --> 10:03:56,240 rename remove copy from the end and 11622 10:03:51,240 --> 10:03:59,720 change the beginning to 12 and press 11623 10:03:56,240 --> 10:04:02,916 enter let's also make a copy of the CSS 11624 10:03:59,720 --> 10:04:07,800 and HTML file for this project so we'll 11625 10:04:02,916 --> 10:04:12,040 click the CSS file right click 11626 10:04:07,800 --> 10:04:13,640 copy click the Styles folder right click 11627 10:04:12,040 --> 10:04:17,596 and 11628 10:04:13,640 --> 10:04:20,400 paste rightclick 11629 10:04:17,596 --> 10:04:23,880 rename remove copy 11630 10:04:20,400 --> 10:04:25,000 and at the front change it to 12 and 11631 10:04:23,880 --> 10:04:28,480 press 11632 10:04:25,000 --> 10:04:30,880 enter and finally we'll scroll down 11633 10:04:28,480 --> 10:04:32,436 click 11 Todo list. 11634 10:04:30,880 --> 10:04:34,276 HTML 11635 10:04:32,436 --> 10:04:39,520 rightclick 11636 10:04:34,276 --> 10:04:43,080 copy and then down here right click and 11637 10:04:39,520 --> 10:04:46,756 paste right click 11638 10:04:43,080 --> 10:04:51,436 rename remove copy at the end and at the 11639 10:04:46,756 --> 10:04:54,680 beginning change it to 12 and and press 11640 10:04:51,436 --> 10:04:57,720 enter now we can hide these 11641 10:04:54,680 --> 10:05:00,756 files and we can close the lesson 11 11642 10:04:57,720 --> 10:05:04,756 files and now here we're going to change 11643 10:05:00,756 --> 10:05:07,596 the CSS file that we load to 12 to-do 11644 10:05:04,756 --> 10:05:09,520 list and we'll scroll down and we'll 11645 10:05:07,596 --> 10:05:14,320 also change the Javascript file that we 11646 10:05:09,520 --> 10:05:17,520 load to 12 to-do list now let's 11647 10:05:14,320 --> 10:05:20,000 save and we'll open this in the browser 11648 10:05:17,520 --> 10:05:22,560 by right clicking 11649 10:05:20,000 --> 10:05:26,040 open with live 11650 10:05:22,560 --> 10:05:29,276 server okay now let's go back to the JS 11651 10:05:26,040 --> 10:05:30,240 file so I'll take this file and drag it 11652 10:05:29,276 --> 10:05:33,276 to the 11653 10:05:30,240 --> 10:05:36,400 bottom and I'll rearrange the windows 11654 10:05:33,276 --> 10:05:38,680 here we're going to scroll down and 11655 10:05:36,400 --> 10:05:42,360 we'll change this for Loop to use a for 11656 10:05:38,680 --> 10:05:45,400 each method so first let's type above 11657 10:05:42,360 --> 10:05:48,560 this for Loop and we'll type the array 11658 10:05:45,400 --> 10:05:53,596 that we want to Loop through which is 11659 10:05:48,560 --> 10:05:57,400 Todo list and we'll type Dot and use the 11660 10:05:53,596 --> 10:05:59,916 four each method and 11661 10:05:57,400 --> 10:06:02,160 brackets inside the brackets we'll give 11662 10:05:59,916 --> 10:06:05,756 it a function that we want to run for 11663 10:06:02,160 --> 10:06:09,080 each to do so we'll type 11664 10:06:05,756 --> 10:06:12,400 function brackets and curly 11665 10:06:09,080 --> 10:06:14,720 brackets and this function can have two 11666 10:06:12,400 --> 10:06:18,000 parameters the first parameter will 11667 10:06:14,720 --> 10:06:19,560 contain each value in the array so we'll 11668 10:06:18,000 --> 10:06:20,960 call it too 11669 10:06:19,560 --> 10:06:24,560 [Music] 11670 10:06:20,960 --> 10:06:27,080 object we'll type a comma and the second 11671 10:06:24,560 --> 10:06:30,360 parameter contains each index in the 11672 10:06:27,080 --> 10:06:33,680 array so we'll just call it 11673 10:06:30,360 --> 10:06:35,720 index and now inside this Loop we 11674 10:06:33,680 --> 10:06:39,116 basically want to do everything that we 11675 10:06:35,720 --> 10:06:40,000 did here so let's just make a copy of 11676 10:06:39,116 --> 10:06:43,720 this 11677 10:06:40,000 --> 10:06:48,200 code right click 11678 10:06:43,720 --> 10:06:51,000 copy and then up here inside for each 11679 10:06:48,200 --> 10:06:54,916 right click and and 11680 10:06:51,000 --> 10:06:57,840 paste so now if we scroll up we already 11681 10:06:54,916 --> 10:07:00,080 have the to-do object up here so we 11682 10:06:57,840 --> 10:07:04,160 don't need to get it from the array like 11683 10:07:00,080 --> 10:07:07,320 before so we can remove this line and 11684 10:07:04,160 --> 10:07:07,320 let's also remove these 11685 10:07:08,160 --> 10:07:12,680 comments the other thing we have to 11686 10:07:10,160 --> 10:07:16,040 change is that previously we were using 11687 10:07:12,680 --> 10:07:19,520 I for the index but now the index is 11688 10:07:16,040 --> 10:07:21,916 called index so we just change this to 11689 10:07:19,520 --> 10:07:25,000 to 11690 10:07:21,916 --> 10:07:28,080 index so that's how we use a for each 11691 10:07:25,000 --> 10:07:30,880 method instead of a for Loop if we 11692 10:07:28,080 --> 10:07:33,800 compare this code to the four Loop that 11693 10:07:30,880 --> 10:07:36,480 we had before the for each method is 11694 10:07:33,800 --> 10:07:39,160 easier to read so for the rest of this 11695 10:07:36,480 --> 10:07:42,520 course we're going to prefer to use four 11696 10:07:39,160 --> 10:07:45,880 each instead of a for Loop so we're 11697 10:07:42,520 --> 10:07:45,880 going to remove this for 11698 10:07:48,160 --> 10:07:56,360 Loop let's scroll back up and 11699 10:07:52,520 --> 10:07:58,680 save and the project still works so 11700 10:07:56,360 --> 10:08:01,916 that's a practical example of using the 11701 10:07:58,680 --> 10:08:05,560 for each method now let's go back and 11702 10:08:01,916 --> 10:08:08,680 learn some more details about for each 11703 10:08:05,560 --> 10:08:12,960 we'll close the Javascript file for now 11704 10:08:08,680 --> 10:08:15,880 and go back to Advanced functions. HTML 11705 10:08:12,960 --> 10:08:18,720 and open the tab for this 11706 10:08:15,880 --> 10:08:21,200 file so in the previous lesson we 11707 10:08:18,720 --> 10:08:23,916 learned a feature of Loops called 11708 10:08:21,200 --> 10:08:26,840 continue continue lets us skip to the 11709 10:08:23,916 --> 10:08:29,756 next iteration or the next run of the 11710 10:08:26,840 --> 10:08:31,960 loop for each methods don't have 11711 10:08:29,756 --> 10:08:35,080 continue but we can do the same thing 11712 10:08:31,960 --> 10:08:38,160 with a return statement so let's say in 11713 10:08:35,080 --> 10:08:40,840 this Loop we don't like washing dishes 11714 10:08:38,160 --> 10:08:43,720 if the value is wash dishes we're going 11715 10:08:40,840 --> 10:08:47,880 to skip it and not display it in the 11716 10:08:43,720 --> 10:08:50,160 console so above we'll create an if 11717 10:08:47,880 --> 10:08:52,116 statement 11718 10:08:50,160 --> 10:08:57,720 and then inside the condition we'll 11719 10:08:52,116 --> 10:08:59,436 check if value is equal to the string 11720 10:08:57,720 --> 10:09:02,520 wash 11721 10:08:59,436 --> 10:09:06,680 dishes and then in the curly brackets if 11722 10:09:02,520 --> 10:09:09,520 it is equal will return and 11723 10:09:06,680 --> 10:09:12,080 semicolon so this return statement will 11724 10:09:09,520 --> 10:09:15,000 end this function early and it will go 11725 10:09:12,080 --> 10:09:17,916 on to the next function so it basically 11726 10:09:15,000 --> 10:09:20,000 does the same thing as continue in a for 11727 10:09:17,916 --> 10:09:23,880 Loop if we 11728 10:09:20,000 --> 10:09:26,680 save you'll notice that it skipped wash 11729 10:09:23,880 --> 10:09:29,800 dishes in the previous lesson we also 11730 10:09:26,680 --> 10:09:32,960 learned a feature of Loops called break 11731 10:09:29,800 --> 10:09:35,116 break lets us exit a loop early 11732 10:09:32,960 --> 10:09:38,596 unfortunately there's not an easy way to 11733 10:09:35,116 --> 10:09:42,000 break in a 4 each Loop so if we need to 11734 10:09:38,596 --> 10:09:46,160 use break to exit this Loop early then 11735 10:09:42,000 --> 10:09:46,160 it's better to use a regular for 11736 10:09:46,200 --> 10:09:51,480 Loop let's take a break before 11737 10:09:48,520 --> 10:09:53,320 continuing with the rest of this lesson 11738 10:09:51,480 --> 10:09:58,080 here are some exercises that will help 11739 10:09:53,320 --> 10:09:58,080 you practice what we learned so far 11740 10:10:25,400 --> 10:10:31,320 so next we're going to move on and learn 11741 10:10:28,116 --> 10:10:34,080 a shorter way to write functions called 11742 10:10:31,320 --> 10:10:36,680 Arrow functions so let's go to the 11743 10:10:34,080 --> 10:10:41,520 bottom of this code we'll create some 11744 10:10:36,680 --> 10:10:46,640 new lines and let's create a variable 11745 10:10:41,520 --> 10:10:50,276 const and we'll name the variable Arrow 11746 10:10:46,640 --> 10:10:55,400 function and we'll make it equal to to 11747 10:10:50,276 --> 10:10:57,240 brackets equal greater than or an arrow 11748 10:10:55,400 --> 10:10:59,960 and then curly 11749 10:10:57,240 --> 10:11:03,436 brackets inside the curly brackets we'll 11750 10:10:59,960 --> 10:11:03,436 type some code 11751 10:11:03,640 --> 10:11:11,116 console.log brackets and the string 11752 10:11:07,880 --> 10:11:14,916 hello and we'll put a semicolon at the 11753 10:11:11,116 --> 10:11:18,360 end so this is an arrow function it 11754 10:11:14,916 --> 10:11:21,240 works the same way as a regular function 11755 10:11:18,360 --> 10:11:25,480 for example we can call this Arrow 11756 10:11:21,240 --> 10:11:27,160 function by typing the variable name 11757 10:11:25,480 --> 10:11:29,480 Arrow 11758 10:11:27,160 --> 10:11:31,800 function and 11759 10:11:29,480 --> 10:11:35,000 brackets if we 11760 10:11:31,800 --> 10:11:39,116 save this line of code will run the code 11761 10:11:35,000 --> 10:11:42,400 inside this Arrow function and display 11762 10:11:39,116 --> 10:11:45,800 hello so Arrow functions mostly work the 11763 10:11:42,400 --> 10:11:48,360 same way as regular functions so to 11764 10:11:45,800 --> 10:11:51,480 compare them let's actually type above 11765 10:11:48,360 --> 10:11:53,240 this code we'll create some new lines 11766 10:11:51,480 --> 10:11:54,800 and type 11767 10:11:53,240 --> 10:11:56,756 const 11768 10:11:54,800 --> 10:12:00,840 regular 11769 10:11:56,756 --> 10:12:06,756 function equals the word 11770 10:12:00,840 --> 10:12:06,756 function brackets and curly brackets 11771 10:12:07,040 --> 10:12:14,640 console.log string 11772 10:12:10,840 --> 10:12:17,160 hello so these two syntaxes do the same 11773 10:12:14,640 --> 10:12:19,800 thing the difference is that instead of 11774 10:12:17,160 --> 10:12:22,040 using the word function an arrow 11775 10:12:19,800 --> 10:12:25,116 function just uses an 11776 10:12:22,040 --> 10:12:27,756 arrow now parameters work the same way 11777 10:12:25,116 --> 10:12:30,640 in an arrow function we just type the 11778 10:12:27,756 --> 10:12:33,720 parameter between these round brackets 11779 10:12:30,640 --> 10:12:37,116 so here let's type 11780 10:12:33,720 --> 10:12:40,360 param and here as 11781 10:12:37,116 --> 10:12:43,756 well if we want multiple parameters it 11782 10:12:40,360 --> 10:12:49,960 also works the same way up here we can 11783 10:12:43,756 --> 10:12:54,756 type comma param 2 and down here comma 11784 10:12:49,960 --> 10:12:58,360 Pam 2 and finally return statements also 11785 10:12:54,756 --> 10:13:02,520 work the same way so in this function we 11786 10:12:58,360 --> 10:13:06,520 can return five and in the arrow 11787 10:13:02,520 --> 10:13:09,436 function we can also return 11788 10:13:06,520 --> 10:13:12,400 five so now let's learn some differences 11789 10:13:09,436 --> 10:13:14,960 between these two Arrow functions have a 11790 10:13:12,400 --> 10:13:19,560 few shortcuts that regular functions 11791 10:13:14,960 --> 10:13:22,960 don't have for example at the bottom 11792 10:13:19,560 --> 10:13:27,040 let's create an arrow function using 11793 10:13:22,960 --> 10:13:30,000 const and we'll name it one 11794 10:13:27,040 --> 10:13:35,160 perm and make it equal to an arrow 11795 10:13:30,000 --> 10:13:37,960 function brackets arrow and curly 11796 10:13:35,160 --> 10:13:41,160 brackets now in this Arrow function 11797 10:13:37,960 --> 10:13:46,720 we're going to give it one parameter 11798 10:13:41,160 --> 10:13:46,720 param and inside the function will 11799 10:13:46,960 --> 10:13:53,800 console.log param plus 11800 10:13:50,680 --> 10:13:56,840 one so when an arrow function only has 11801 10:13:53,800 --> 10:14:00,400 one parameter these round brackets are 11802 10:13:56,840 --> 10:14:08,520 optional so we can actually remove 11803 10:14:00,400 --> 10:14:13,080 them if we do one param brackets 2 if we 11804 10:14:08,520 --> 10:14:16,436 save it will display 2 + 1 which is 11805 10:14:13,080 --> 10:14:22,276 3 let's learn another shortcut we'll 11806 10:14:16,436 --> 10:14:25,520 create some new lines and type const one 11807 10:14:22,276 --> 10:14:30,680 line and make it equal to an arrow 11808 10:14:25,520 --> 10:14:37,916 function brackets arrow and curly 11809 10:14:30,680 --> 10:14:41,560 brackets and inside will return 2 + 11810 10:14:37,916 --> 10:14:45,520 3 so when an arrow function only has one 11811 10:14:41,560 --> 10:14:47,960 line like this we can actually put it on 11812 10:14:45,520 --> 10:14:50,520 the same line as the 11813 10:14:47,960 --> 10:14:53,080 arrow and now that everything's on the 11814 10:14:50,520 --> 10:14:56,560 same line the curly brackets are 11815 10:14:53,080 --> 10:15:00,680 actually optional so we can remove them 11816 10:14:56,560 --> 10:15:00,680 and we can also remove the return 11817 10:15:01,276 --> 10:15:06,840 statement so this is a oneline arrow 11818 10:15:04,160 --> 10:15:09,400 function if we just have an arrow it 11819 10:15:06,840 --> 10:15:14,520 will automatically return the result on 11820 10:15:09,400 --> 10:15:19,720 the right so at the bottom we can 11821 10:15:14,520 --> 10:15:21,720 console. log and call the one line 11822 10:15:19,720 --> 10:15:24,080 function with 11823 10:15:21,720 --> 10:15:27,320 brackets if we 11824 10:15:24,080 --> 10:15:30,596 save this function will return to plus 11825 10:15:27,320 --> 10:15:33,560 three and it will display five in the 11826 10:15:30,596 --> 10:15:35,640 console so that's Arrow functions it's a 11827 10:15:33,560 --> 10:15:38,240 shorter way of writing a regular 11828 10:15:35,640 --> 10:15:40,756 function and it has a few 11829 10:15:38,240 --> 10:15:44,116 shortcuts for now let's get some 11830 10:15:40,756 --> 10:15:47,916 practice using Arrow functions so let's 11831 10:15:44,116 --> 10:15:51,080 scroll up in this code to the for each 11832 10:15:47,916 --> 10:15:54,240 method so let's use an arrow function 11833 10:15:51,080 --> 10:15:55,680 here instead we just have to remove the 11834 10:15:54,240 --> 10:15:58,916 word 11835 10:15:55,680 --> 10:16:04,116 function and after the round brackets 11836 10:15:58,916 --> 10:16:08,040 add an arrow equals greater than if we 11837 10:16:04,116 --> 10:16:11,080 save the loop still works so when we're 11838 10:16:08,040 --> 10:16:14,040 passing a function into another function 11839 10:16:11,080 --> 10:16:15,916 like for each it's recommended to use 11840 10:16:14,040 --> 10:16:19,080 Arrow functions because it makes our 11841 10:16:15,916 --> 10:16:21,480 code easier to read so now let's use use 11842 10:16:19,080 --> 10:16:24,080 Arrow functions in some of our previous 11843 10:16:21,480 --> 10:16:27,400 projects we'll start with the rock paper 11844 10:16:24,080 --> 10:16:28,840 scissors game let's open our files by 11845 10:16:27,400 --> 10:16:32,756 clicking 11846 10:16:28,840 --> 10:16:35,276 here and we'll find the 12 rock paper 11847 10:16:32,756 --> 10:16:38,480 scissors. JS 11848 10:16:35,276 --> 10:16:41,880 file and then inside this file we're 11849 10:16:38,480 --> 10:16:45,000 going to scroll down to the autoplay 11850 10:16:41,880 --> 10:16:48,320 function so here we're passing a 11851 10:16:45,000 --> 10:16:50,720 function into another function so it's 11852 10:16:48,320 --> 10:16:53,596 recommended to use an arrow 11853 10:16:50,720 --> 10:16:57,960 function let's remove the word 11854 10:16:53,596 --> 10:17:01,276 function and add an arrow so now this 11855 10:16:57,960 --> 10:17:04,360 code is a little easier to read now what 11856 10:17:01,276 --> 10:17:07,116 about this function up here function 11857 10:17:04,360 --> 10:17:10,116 autoplay should we also switch this to 11858 10:17:07,116 --> 10:17:13,680 an arrow function so this situation is 11859 10:17:10,116 --> 10:17:15,800 more personal preference so above this 11860 10:17:13,680 --> 10:17:19,520 let's actually create an arrow function 11861 10:17:15,800 --> 10:17:26,160 version of this const 11862 10:17:19,520 --> 10:17:27,596 auto play equals brackets and arrow and 11863 10:17:26,160 --> 10:17:30,840 curly 11864 10:17:27,596 --> 10:17:33,960 brackets so even though we could create 11865 10:17:30,840 --> 10:17:36,080 an arrow function version of this I 11866 10:17:33,960 --> 10:17:39,916 actually prefer the regular function 11867 10:17:36,080 --> 10:17:43,756 syntax here for two reasons number one I 11868 10:17:39,916 --> 10:17:46,560 think this is easier to read than this 11869 10:17:43,756 --> 10:17:49,436 and number two this function syntax 11870 10:17:46,560 --> 10:17:52,200 enables hoisting which means we can call 11871 10:17:49,436 --> 10:17:54,436 this function before we create it and we 11872 10:17:52,200 --> 10:17:57,560 don't have to worry about which order we 11873 10:17:54,436 --> 10:18:00,596 write the code so for these reasons I 11874 10:17:57,560 --> 10:18:03,480 actually prefer this situation to use a 11875 10:18:00,596 --> 10:18:06,360 regular function syntax and not an arrow 11876 10:18:03,480 --> 10:18:08,800 function so we'll comment out the arrow 11877 10:18:06,360 --> 10:18:11,200 function using double 11878 10:18:08,800 --> 10:18:13,756 slash and double 11879 10:18:11,200 --> 10:18:17,200 slash now let's 11880 10:18:13,756 --> 10:18:20,160 save and let's open the tab for this 11881 10:18:17,200 --> 10:18:22,080 project and we'll give give it a try so 11882 10:18:20,160 --> 10:18:24,680 click 11883 10:18:22,080 --> 10:18:27,800 autoplay and this still works after 11884 10:18:24,680 --> 10:18:30,880 replacing with an arrow 11885 10:18:27,800 --> 10:18:34,436 function next let's go to the to-do list 11886 10:18:30,880 --> 10:18:37,916 project and switch to Arrow functions so 11887 10:18:34,436 --> 10:18:42,400 we'll go to our code editor and find the 11888 10:18:37,916 --> 10:18:45,200 to-do list project which is 11889 10:18:42,400 --> 10:18:48,880 here and let's scroll 11890 10:18:45,200 --> 10:18:51,520 down so inside render to-do list we're 11891 10:18:48,880 --> 10:18:54,480 using four each and we're passing this 11892 10:18:51,520 --> 10:18:56,680 function into another function so it's 11893 10:18:54,480 --> 10:18:59,916 recommended to use an arrow function 11894 10:18:56,680 --> 10:19:02,916 here so we'll remove the word 11895 10:18:59,916 --> 10:19:04,960 function and after the brackets We'll 11896 10:19:02,916 --> 10:19:08,000 add an 11897 10:19:04,960 --> 10:19:10,520 arrow and again I'll leave this function 11898 10:19:08,000 --> 10:19:13,960 syntax as a regular function because 11899 10:19:10,520 --> 10:19:15,276 it's easier to read and enables hoisting 11900 10:19:13,960 --> 10:19:19,400 so let's 11901 10:19:15,276 --> 10:19:21,200 save and go back to our to-do list 11902 10:19:19,400 --> 10:19:26,400 and everything still 11903 10:19:21,200 --> 10:19:29,040 works next let's go back to 12 Advanced 11904 10:19:26,400 --> 10:19:32,720 functions. 11905 10:19:29,040 --> 10:19:36,276 HTML and we'll scroll to the 11906 10:19:32,720 --> 10:19:39,040 bottom and we'll open the tab for this 11907 10:19:36,276 --> 10:19:41,360 file and we'll review another place we 11908 10:19:39,040 --> 10:19:45,720 could use Arrow functions which is 11909 10:19:41,360 --> 10:19:49,960 inside an object so at the bottom here 11910 10:19:45,720 --> 10:19:53,640 let's create a new object con 11911 10:19:49,960 --> 10:19:57,040 object 2 equals 11912 10:19:53,640 --> 10:20:01,360 object and then inside let's type the 11913 10:19:57,040 --> 10:20:06,000 property method colon and an arrow 11914 10:20:01,360 --> 10:20:08,960 function brackets arrow and curly 11915 10:20:06,000 --> 10:20:11,520 brackets so even though we could save an 11916 10:20:08,960 --> 10:20:15,596 arrow function inside an object like 11917 10:20:11,520 --> 10:20:19,080 this objects already have a shortcut for 11918 10:20:15,596 --> 10:20:20,596 functions so at the bottom we could just 11919 10:20:19,080 --> 10:20:23,960 type 11920 10:20:20,596 --> 10:20:26,916 method brackets and curly 11921 10:20:23,960 --> 10:20:30,116 brackets so this syntax is called the 11922 10:20:26,916 --> 10:20:32,400 shorthand method syntax so this 11923 10:20:30,116 --> 10:20:35,520 shorthand method syntax is actually 11924 10:20:32,400 --> 10:20:38,520 easier to read than an arrow function so 11925 10:20:35,520 --> 10:20:41,520 when saving a function in an object it's 11926 10:20:38,520 --> 10:20:44,080 actually recommended to use this syntax 11927 10:20:41,520 --> 10:20:46,160 instead there's also another difference 11928 10:20:44,080 --> 10:20:48,400 between regular functions and arrow 11929 10:20:46,160 --> 10:20:50,640 functions but we'll learn about it later 11930 10:20:48,400 --> 10:20:52,116 in in this course because it's a more 11931 10:20:50,640 --> 10:20:54,436 advanced 11932 10:20:52,116 --> 10:20:56,640 feature now let's move on and we're 11933 10:20:54,436 --> 10:20:59,360 going to learn another Improvement we 11934 10:20:56,640 --> 10:21:03,160 can make to our projects so let's 11935 10:20:59,360 --> 10:21:08,560 comment out this code we'll scroll up 11936 10:21:03,160 --> 10:21:12,680 and type SL star and at the 11937 10:21:08,560 --> 10:21:15,160 bottom star slash and 11938 10:21:12,680 --> 10:21:19,040 save and next we're actually going to 11939 10:21:15,160 --> 10:21:21,040 scroll up to the very top of this file 11940 10:21:19,040 --> 10:21:25,480 and we're going to create a button on 11941 10:21:21,040 --> 10:21:29,680 the page so inside the body we'll type 11942 10:21:25,480 --> 10:21:33,276 less than button greater than and inside 11943 10:21:29,680 --> 10:21:36,040 the button we'll have the text 11944 10:21:33,276 --> 10:21:38,756 click now let's say that we want to run 11945 10:21:36,040 --> 10:21:43,720 some JavaScript code when we click this 11946 10:21:38,756 --> 10:21:46,560 button so here we can type on click 11947 10:21:43,720 --> 10:21:48,720 equals double quotes and write some 11948 10:21:46,560 --> 10:21:51,240 JavaScript inside this double quotes 11949 10:21:48,720 --> 10:21:54,320 quot now we're going to learn a better 11950 10:21:51,240 --> 10:21:58,000 way of doing this called add event 11951 10:21:54,320 --> 10:21:59,880 listener to use add event listener first 11952 10:21:58,000 --> 10:22:03,276 we're going to get this button and put 11953 10:21:59,880 --> 10:22:07,240 it into our JavaScript using document. 11954 10:22:03,276 --> 10:22:10,240 query selector so let's add a class to 11955 10:22:07,240 --> 10:22:12,360 this button so we can select it and 11956 10:22:10,240 --> 10:22:14,160 we'll set it to 11957 10:22:12,360 --> 10:22:17,400 js- 11958 10:22:14,160 --> 10:22:20,320 button and now inside the script element 11959 10:22:17,400 --> 10:22:23,436 we're going to use 11960 10:22:20,320 --> 10:22:29,680 document. query 11961 10:22:23,436 --> 10:22:31,756 selector brackets and the string do 11962 10:22:29,680 --> 10:22:34,400 js- 11963 10:22:31,756 --> 10:22:37,756 button so now let's save this button in 11964 10:22:34,400 --> 10:22:40,520 a variable so we can use it later at the 11965 10:22:37,756 --> 10:22:46,560 front we'll type 11966 10:22:40,520 --> 10:22:50,160 const button element equals this 11967 10:22:46,560 --> 10:22:53,916 result now every HT H ml element has a 11968 10:22:50,160 --> 10:22:56,800 method called add event listener add 11969 10:22:53,916 --> 10:22:59,560 event listener let us run some code when 11970 10:22:56,800 --> 10:23:02,040 we interact with the element so it sort 11971 10:22:59,560 --> 10:23:06,520 of does the same thing as 11972 10:23:02,040 --> 10:23:08,040 onclick so at the bottom let's type 11973 10:23:06,520 --> 10:23:14,200 button 11974 10:23:08,040 --> 10:23:15,720 element dot add capital E Event capital 11975 10:23:14,200 --> 10:23:19,436 L 11976 10:23:15,720 --> 10:23:22,880 listener and brackets 11977 10:23:19,436 --> 10:23:25,720 so add event listener has two parameters 11978 10:23:22,880 --> 10:23:28,320 the first parameter is the event or what 11979 10:23:25,720 --> 10:23:32,360 type of interaction we want to listen 11980 10:23:28,320 --> 10:23:34,360 for so between these brackets let's type 11981 10:23:32,360 --> 10:23:37,560 the string 11982 10:23:34,360 --> 10:23:40,596 click so this will run some code when we 11983 10:23:37,560 --> 10:23:44,000 click the button the way to find these 11984 10:23:40,596 --> 10:23:46,640 event names is to look at this attribute 11985 10:23:44,000 --> 10:23:49,960 the word after on is the 11986 10:23:46,640 --> 10:23:52,400 event next the second parameter is a 11987 10:23:49,960 --> 10:23:57,080 function that we want to run when we 11988 10:23:52,400 --> 10:23:59,436 click this element so we'll type a comma 11989 10:23:57,080 --> 10:24:01,960 and because we're passing a function 11990 10:23:59,436 --> 10:24:05,360 into another function we're going to use 11991 10:24:01,960 --> 10:24:08,480 an arrow function so we'll type 11992 10:24:05,360 --> 10:24:13,360 brackets arrow and curly 11993 10:24:08,480 --> 10:24:18,880 brackets so inside here let's just do 11994 10:24:13,360 --> 10:24:20,520 console. log string click 11995 10:24:18,880 --> 10:24:24,520 and 11996 10:24:20,520 --> 10:24:28,116 save and now when we click this button 11997 10:24:24,520 --> 10:24:32,040 it will run this function and display 11998 10:24:28,116 --> 10:24:35,840 click so this code does the same thing 11999 10:24:32,040 --> 10:24:38,360 as the onclick attribute however add 12000 10:24:35,840 --> 10:24:42,080 event listener has some advantages over 12001 10:24:38,360 --> 10:24:45,436 using an attribute first it lets us add 12002 10:24:42,080 --> 10:24:48,480 multiple event listeners for an event 12003 10:24:45,436 --> 10:24:51,840 for example let's add another click 12004 10:24:48,480 --> 10:24:54,080 event listener we can select this 12005 10:24:51,840 --> 10:24:56,320 code and 12006 10:24:54,080 --> 10:24:59,320 rightclick 12007 10:24:56,320 --> 10:25:03,000 copy and then down 12008 10:24:59,320 --> 10:25:07,160 here rightclick and 12009 10:25:03,000 --> 10:25:09,400 paste and let's change this to log click 12010 10:25:07,160 --> 10:25:11,960 to if we 12011 10:25:09,400 --> 10:25:15,916 save and click the 12012 10:25:11,960 --> 10:25:18,436 button it will run both these functions 12013 10:25:15,916 --> 10:25:22,040 so add event listener let us add 12014 10:25:18,436 --> 10:25:24,276 multiple listeners to one event another 12015 10:25:22,040 --> 10:25:27,276 advantage of doing this is that we can 12016 10:25:24,276 --> 10:25:29,520 remove an event listener using a method 12017 10:25:27,276 --> 10:25:34,960 called remove event 12018 10:25:29,520 --> 10:25:36,640 listener so under this code we can type 12019 10:25:34,960 --> 10:25:42,520 button 12020 10:25:36,640 --> 10:25:46,320 element do remove capital E Event 12021 10:25:42,520 --> 10:25:49,840 capital L listener and 12022 10:25:46,320 --> 10:25:50,960 brackets and remove EV listener also has 12023 10:25:49,840 --> 10:25:53,720 two 12024 10:25:50,960 --> 10:25:58,520 parameters the first parameter is the 12025 10:25:53,720 --> 10:26:01,800 event so in this case it is the string 12026 10:25:58,520 --> 10:26:04,480 click and we'll type a comma and the 12027 10:26:01,800 --> 10:26:06,756 second parameter is the function that we 12028 10:26:04,480 --> 10:26:09,680 want to remove so that when we click 12029 10:26:06,756 --> 10:26:13,040 this button it will no longer run that 12030 10:26:09,680 --> 10:26:15,480 function now we can't just type a copy 12031 10:26:13,040 --> 10:26:18,560 of this function we need to use this 12032 10:26:15,480 --> 10:26:21,640 exact function inside remove event 12033 10:26:18,560 --> 10:26:25,160 listener to do that we'll save this in a 12034 10:26:21,640 --> 10:26:26,276 variable first so above this let's 12035 10:26:25,160 --> 10:26:27,880 create a 12036 10:26:26,276 --> 10:26:31,040 variable 12037 10:26:27,880 --> 10:26:34,960 const event 12038 10:26:31,040 --> 10:26:38,436 listener equals and we'll select this 12039 10:26:34,960 --> 10:26:44,276 code right click 12040 10:26:38,436 --> 10:26:47,720 copy and in here right click and 12041 10:26:44,276 --> 10:26:48,756 paste and now we'll replace this with 12042 10:26:47,720 --> 10:26:52,116 the VAR 12043 10:26:48,756 --> 10:26:55,360 variable event 12044 10:26:52,116 --> 10:26:56,320 listener and down here we'll remove this 12045 10:26:55,360 --> 10:27:00,240 same 12046 10:26:56,320 --> 10:27:02,320 variable event 12047 10:27:00,240 --> 10:27:06,160 listener if we 12048 10:27:02,320 --> 10:27:09,756 save and click the button again it will 12049 10:27:06,160 --> 10:27:12,240 only display click two because we added 12050 10:27:09,756 --> 10:27:14,436 this event listener and then we removed 12051 10:27:12,240 --> 10:27:17,596 it so it will no longer run this 12052 10:27:14,436 --> 10:27:18,720 function when we click the button so as 12053 10:27:17,596 --> 10:27:21,320 you can see 12054 10:27:18,720 --> 10:27:24,400 add event listener gives us more control 12055 10:27:21,320 --> 10:27:27,680 over event listeners we can add multiple 12056 10:27:24,400 --> 10:27:30,840 listeners to an event and we can also 12057 10:27:27,680 --> 10:27:32,840 remove a listener from an event so 12058 10:27:30,840 --> 10:27:35,640 because of these advantages the best 12059 10:27:32,840 --> 10:27:39,116 practice is to use add event listener 12060 10:27:35,640 --> 10:27:41,640 instead of an onclick attribute so let's 12061 10:27:39,116 --> 10:27:44,840 go back into our projects and switch to 12062 10:27:41,640 --> 10:27:47,756 using add event listener we'll start 12063 10:27:44,840 --> 10:27:49,276 with the rock paper scissors project so 12064 10:27:47,756 --> 10:27:52,596 we'll open our 12065 10:27:49,276 --> 10:27:55,840 files and we'll open 12 rock paper 12066 10:27:52,596 --> 10:28:00,360 scissors and click here and 12067 10:27:55,840 --> 10:28:04,520 save and we'll scroll up to 12 rockpaper 12068 10:28:00,360 --> 10:28:07,436 scissors. JS and for the video I'll drag 12069 10:28:04,520 --> 10:28:08,880 this down to the bottom and I'll hide 12070 10:28:07,436 --> 10:28:12,640 the files for 12071 10:28:08,880 --> 10:28:15,880 now and then rearrange the 12072 10:28:12,640 --> 10:28:18,240 windows and finally open the tab for 12073 10:28:15,880 --> 10:28:22,116 this file 12074 10:28:18,240 --> 10:28:27,160 now let's go to the HTML we'll scroll up 12075 10:28:22,116 --> 10:28:30,596 to these move buttons which is up 12076 10:28:27,160 --> 10:28:32,596 here so here we're using the onclick 12077 10:28:30,596 --> 10:28:36,360 attribute and we're going to switch it 12078 10:28:32,596 --> 10:28:38,320 to add event listener so first let's get 12079 10:28:36,360 --> 10:28:41,160 this button into our 12080 10:28:38,320 --> 10:28:43,640 JavaScript We'll add a class so we can 12081 10:28:41,160 --> 10:28:45,640 select it 12082 10:28:43,640 --> 10:28:48,160 js- 12083 10:28:45,640 --> 10:28:49,160 rock- button 12084 10:28:48,160 --> 10:28:51,360 and 12085 10:28:49,160 --> 10:28:54,756 save and then down 12086 10:28:51,360 --> 10:28:58,200 here let's go below the autoplay 12087 10:28:54,756 --> 10:29:01,040 function right here and we'll use add 12088 10:28:58,200 --> 10:29:04,436 event listener so we'll get this button 12089 10:29:01,040 --> 10:29:08,080 into our JavaScript using 12090 10:29:04,436 --> 10:29:13,200 document do query 12091 10:29:08,080 --> 10:29:14,880 selector brackets and a string dot 12092 10:29:13,200 --> 10:29:16,756 js- 12093 10:29:14,880 --> 10:29:21,756 ro- 12094 10:29:16,756 --> 10:29:23,640 button and we're going to use dot add 12095 10:29:21,756 --> 10:29:25,400 event 12096 10:29:23,640 --> 10:29:29,320 listener 12097 10:29:25,400 --> 10:29:32,960 brackets so we're going to listen for a 12098 10:29:29,320 --> 10:29:36,276 click and a comma and we'll give it a 12099 10:29:32,960 --> 10:29:38,680 function that we want to run so a common 12100 10:29:36,276 --> 10:29:44,640 mistake here is to actually run the 12101 10:29:38,680 --> 10:29:46,680 function for example play game brackets 12102 10:29:44,640 --> 10:29:49,200 and the string 12103 10:29:46,680 --> 10:29:53,320 Rock so we're supposed to give a 12104 10:29:49,200 --> 10:29:56,400 function to add event listener however 12105 10:29:53,320 --> 10:29:59,436 this doesn't result in a function this 12106 10:29:56,400 --> 10:30:02,916 will actually run play game and give us 12107 10:29:59,436 --> 10:30:05,080 the return value which is undefined so 12108 10:30:02,916 --> 10:30:06,960 this code will actually result in 12109 10:30:05,080 --> 10:30:10,160 undefined and then we're giving 12110 10:30:06,960 --> 10:30:11,360 undefined to add event listener so it 12111 10:30:10,160 --> 10:30:14,276 won't 12112 10:30:11,360 --> 10:30:17,360 work so in order to give add event 12113 10:30:14,276 --> 10:30:18,680 listener a function we need to create a 12114 10:30:17,360 --> 10:30:21,040 function here 12115 10:30:18,680 --> 10:30:25,756 so let's remove this code and we'll 12116 10:30:21,040 --> 10:30:27,200 create a function using brackets arrow 12117 10:30:25,756 --> 10:30:30,436 and curly 12118 10:30:27,200 --> 10:30:33,436 brackets and then inside this function 12119 10:30:30,436 --> 10:30:40,116 we're going to call play game so we'll 12120 10:30:33,436 --> 10:30:43,436 type play game brackets and 12121 10:30:40,116 --> 10:30:46,596 rock so now when we click the rock 12122 10:30:43,436 --> 10:30:49,840 button it will run this function and 12123 10:30:46,596 --> 10:30:51,680 play the game so let's remove the 12124 10:30:49,840 --> 10:30:55,320 onclick 12125 10:30:51,680 --> 10:30:58,360 attribute and save the 12126 10:30:55,320 --> 10:31:01,116 HTML and also save the 12127 10:30:58,360 --> 10:31:04,436 JavaScript and then go to our project 12128 10:31:01,116 --> 10:31:06,756 and click the rock button you can see 12129 10:31:04,436 --> 10:31:10,116 that it works except this time we're 12130 10:31:06,756 --> 10:31:12,160 using add event listener so let's do the 12131 10:31:10,116 --> 10:31:14,480 same thing for the paper and the 12132 10:31:12,160 --> 10:31:16,840 scissors button feel free to pause the 12133 10:31:14,480 --> 10:31:20,400 video if you want to try it 12134 10:31:16,840 --> 10:31:23,320 yourself so we'll scroll down here and 12135 10:31:20,400 --> 10:31:25,800 let's add a class to the paper button so 12136 10:31:23,320 --> 10:31:27,720 we can select it 12137 10:31:25,800 --> 10:31:31,680 js- 12138 10:31:27,720 --> 10:31:35,160 paper- button and then in our JavaScript 12139 10:31:31,680 --> 10:31:37,160 we'll make a copy of this code so right 12140 10:31:35,160 --> 10:31:40,276 click 12141 10:31:37,160 --> 10:31:44,680 copy and then down 12142 10:31:40,276 --> 10:31:47,276 here right click and 12143 10:31:44,680 --> 10:31:48,800 paste and now we're going to replace the 12144 10:31:47,276 --> 10:31:50,520 rock button 12145 10:31:48,800 --> 10:31:53,360 with 12146 10:31:50,520 --> 10:31:56,360 paper and when we click this button 12147 10:31:53,360 --> 10:31:58,596 we're going to play the game with 12148 10:31:56,360 --> 10:32:03,000 paper now let's 12149 10:31:58,596 --> 10:32:06,320 save and we'll remove the onclick 12150 10:32:03,000 --> 10:32:09,960 attribute and save the 12151 10:32:06,320 --> 10:32:11,276 HTML if we click the paper button it 12152 10:32:09,960 --> 10:32:14,276 works as 12153 10:32:11,276 --> 10:32:18,560 before finally let's do the scissors 12154 10:32:14,276 --> 10:32:22,200 button so we'll add a class Js 12155 10:32:18,560 --> 10:32:25,200 s- scissors dasb 12156 10:32:22,200 --> 10:32:30,080 button and then inside our JavaScript 12157 10:32:25,200 --> 10:32:33,840 we'll make a copy of this rightclick 12158 10:32:30,080 --> 10:32:37,640 copy and then down here create some new 12159 10:32:33,840 --> 10:32:41,596 lines rightclick and 12160 10:32:37,640 --> 10:32:45,840 paste and now switch to the scissors 12161 10:32:41,596 --> 10:32:48,840 button and play game with 12162 10:32:45,840 --> 10:32:51,916 scissors let's save 12163 10:32:48,840 --> 10:32:54,160 and remove the onclick 12164 10:32:51,916 --> 10:32:56,960 attribute save the 12165 10:32:54,160 --> 10:32:59,680 HTML and try the scissors 12166 10:32:56,960 --> 10:33:02,480 button so everything still 12167 10:32:59,680 --> 10:33:05,596 works and I'll leave switching the reset 12168 10:33:02,480 --> 10:33:06,596 score button and the autop playay button 12169 10:33:05,596 --> 10:33:09,240 as 12170 10:33:06,596 --> 10:33:11,840 exercises so now let's try add event 12171 10:33:09,240 --> 10:33:14,960 listener with a different event we'll 12172 10:33:11,840 --> 10:33:17,240 use it with key down so let's say that 12173 10:33:14,960 --> 10:33:19,880 in our project we want to make it so we 12174 10:33:17,240 --> 10:33:23,436 can play the the game with our keyboard 12175 10:33:19,880 --> 10:33:26,840 if we Type R it will play Rock if we 12176 10:33:23,436 --> 10:33:29,240 type P it will play paper and if we Type 12177 10:33:26,840 --> 10:33:31,756 S it will play 12178 10:33:29,240 --> 10:33:34,200 scissors let's go into our 12179 10:33:31,756 --> 10:33:36,596 JavaScript and we'll scroll 12180 10:33:34,200 --> 10:33:39,480 down and we're going to add an event 12181 10:33:36,596 --> 10:33:41,916 listener to the body this time so that 12182 10:33:39,480 --> 10:33:44,800 if we type anywhere on the page we can 12183 10:33:41,916 --> 10:33:49,560 run some code so we're going to get the 12184 10:33:44,800 --> 10:33:51,276 body element using document 12185 10:33:49,560 --> 10:33:57,756 body and 12186 10:33:51,276 --> 10:34:00,520 then add event listener and 12187 10:33:57,756 --> 10:34:04,916 brackets this time the first parameter 12188 10:34:00,520 --> 10:34:08,276 or the event will be the string key 12189 10:34:04,916 --> 10:34:12,080 down and let's type comma and give it a 12190 10:34:08,276 --> 10:34:14,480 function that we want to run brackets 12191 10:34:12,080 --> 10:34:19,640 arrow and curly 12192 10:34:14,480 --> 10:34:19,640 brackets inside here let's just type 12193 10:34:20,200 --> 10:34:28,880 console.log key down for now and 12194 10:34:25,640 --> 10:34:31,240 save so now if we go to the project and 12195 10:34:28,880 --> 10:34:33,840 we open the console 12196 10:34:31,240 --> 10:34:35,880 rightclick and click 12197 10:34:33,840 --> 10:34:39,520 inspect click the 12198 10:34:35,880 --> 10:34:41,960 console and then click on the page if we 12199 10:34:39,520 --> 10:34:44,960 type a key on our 12200 10:34:41,960 --> 10:34:48,680 keyboard it will run this function and 12201 10:34:44,960 --> 10:34:52,000 display key down so now we have to check 12202 10:34:48,680 --> 10:34:54,360 what key was pressed and play the game 12203 10:34:52,000 --> 10:34:57,240 so how do we know which key that we 12204 10:34:54,360 --> 10:35:00,436 pressed earlier in this course we used 12205 10:34:57,240 --> 10:35:03,160 the attribute on key down and we learned 12206 10:35:00,436 --> 10:35:05,916 that it gets a special object called 12207 10:35:03,160 --> 10:35:09,520 event and the event object contains 12208 10:35:05,916 --> 10:35:12,240 which key was pressed add event listener 12209 10:35:09,520 --> 10:35:14,640 also provides this event object but it 12210 10:35:12,240 --> 10:35:17,800 provides it as a parameter to this 12211 10:35:14,640 --> 10:35:20,480 function so inside the round brackets 12212 10:35:17,800 --> 10:35:23,320 let's add a parameter 12213 10:35:20,480 --> 10:35:26,000 event now every time we type on our 12214 10:35:23,320 --> 10:35:29,520 keyboard add event listener will save 12215 10:35:26,000 --> 10:35:32,240 the event object in here and run the 12216 10:35:29,520 --> 10:35:34,000 function and this event object contains 12217 10:35:32,240 --> 10:35:37,240 the key that we 12218 10:35:34,000 --> 10:35:42,840 pressed so in the console.log let's 12219 10:35:37,240 --> 10:35:46,160 display event. key this time and 12220 10:35:42,840 --> 10:35:48,436 save now if we click on the page and 12221 10:35:46,160 --> 10:35:53,680 type R 12222 10:35:48,436 --> 10:35:57,080 event. key will contain R if we press P 12223 10:35:53,680 --> 10:36:00,000 event. key will contain P so that's how 12224 10:35:57,080 --> 10:36:03,640 we know which key we pressed so now 12225 10:36:00,000 --> 10:36:06,596 let's close this and instead of 12226 10:36:03,640 --> 10:36:10,200 console.log we'll create an if statement 12227 10:36:06,596 --> 10:36:12,360 to check which key we pressed so if 12228 10:36:10,200 --> 10:36:17,276 brackets and curly 12229 10:36:12,360 --> 10:36:18,560 brackets and here we'll check if event. 12230 10:36:17,276 --> 10:36:23,916 key 12231 10:36:18,560 --> 10:36:27,240 is equal to the string R if it is we're 12232 10:36:23,916 --> 10:36:30,160 going to play the game with rock so 12233 10:36:27,240 --> 10:36:31,360 inside here we can just run this play 12234 10:36:30,160 --> 10:36:36,560 game 12235 10:36:31,360 --> 10:36:40,116 function play game and brackets and 12236 10:36:36,560 --> 10:36:44,080 inside we'll type the string 12237 10:36:40,116 --> 10:36:48,480 Rock now let's check if we pressed P so 12238 10:36:44,080 --> 10:36:51,840 we'll add another Branch else if 12239 10:36:48,480 --> 10:36:55,840 brackets and curly brackets and in the 12240 10:36:51,840 --> 10:37:01,680 second condition we'll check if event. 12241 10:36:55,840 --> 10:37:05,116 key is equal to the string P if it is 12242 10:37:01,680 --> 10:37:10,960 we'll play the game with paper so inside 12243 10:37:05,116 --> 10:37:14,916 here we'll run play game brackets and 12244 10:37:10,960 --> 10:37:18,840 the string paper and finally we'll check 12245 10:37:14,916 --> 10:37:23,840 if we pressed s so at the end We'll add 12246 10:37:18,840 --> 10:37:28,240 another else if brackets curly brackets 12247 10:37:23,840 --> 10:37:34,436 and inside here we'll check if event. 12248 10:37:28,240 --> 10:37:37,040 key is equal to the string s if it is 12249 10:37:34,436 --> 10:37:40,880 we'll play the 12250 10:37:37,040 --> 10:37:43,160 game with the string 12251 10:37:40,880 --> 10:37:48,200 scissors now let's 12252 10:37:43,160 --> 10:37:51,240 save click our page and type R 12253 10:37:48,200 --> 10:37:55,200 and it plays the game with rock if we 12254 10:37:51,240 --> 10:37:58,800 type P it will play the game with paper 12255 10:37:55,200 --> 10:37:59,680 and if we Type S it will play the game 12256 10:37:58,800 --> 10:38:02,520 with 12257 10:37:59,680 --> 10:38:04,880 scissors so that's an example of using 12258 10:38:02,520 --> 10:38:08,640 add event listener with a different type 12259 10:38:04,880 --> 10:38:11,080 of event with a key down if we need the 12260 10:38:08,640 --> 10:38:14,320 event object we can get it using a 12261 10:38:11,080 --> 10:38:16,116 parameter up here so those are all the 12262 10:38:14,320 --> 10:38:18,400 features we're going to create for our 12263 10:38:16,116 --> 10:38:20,400 rock paper scissors project 12264 10:38:18,400 --> 10:38:23,960 now let's move on to the to-do list 12265 10:38:20,400 --> 10:38:27,160 project and switch to add event listener 12266 10:38:23,960 --> 10:38:32,240 we'll go into our code editor click up 12267 10:38:27,160 --> 10:38:35,756 here and find 12 to-do list. 12268 10:38:32,240 --> 10:38:38,640 HTML we'll also scroll up to the scripts 12269 10:38:35,756 --> 10:38:41,800 folder find 12 to-do 12270 10:38:38,640 --> 10:38:43,960 list.js and open this file and I'm going 12271 10:38:41,800 --> 10:38:46,916 to put it at the bottom here so I can 12272 10:38:43,960 --> 10:38:47,880 see it side by side I'll close this for 12273 10:38:46,916 --> 10:38:50,000 now 12274 10:38:47,880 --> 10:38:54,000 and close rock paper 12275 10:38:50,000 --> 10:38:57,116 scissors and then open the tab for this 12276 10:38:54,000 --> 10:39:00,840 project okay so let's start by switching 12277 10:38:57,116 --> 10:39:04,240 the add button to use add event listener 12278 10:39:00,840 --> 10:39:07,240 we'll go into our HTML and find the add 12279 10:39:04,240 --> 10:39:10,116 button which is here and we're going to 12280 10:39:07,240 --> 10:39:12,160 add a class so we can select it in 12281 10:39:10,116 --> 10:39:13,880 JavaScript 12282 10:39:12,160 --> 10:39:18,520 js- 12283 10:39:13,880 --> 10:39:21,320 add- too dasb button 12284 10:39:18,520 --> 10:39:22,400 now let's go to our JavaScript and 12285 10:39:21,320 --> 10:39:25,756 scroll 12286 10:39:22,400 --> 10:39:28,480 down above the add Todo 12287 10:39:25,756 --> 10:39:31,436 function we'll get this button into our 12288 10:39:28,480 --> 10:39:34,560 JavaScript using 12289 10:39:31,436 --> 10:39:39,320 document. query 12290 10:39:34,560 --> 10:39:42,436 selector brackets and then a string Dot 12291 10:39:39,320 --> 10:39:48,240 and the class we want to get 12292 10:39:42,436 --> 10:39:55,200 js- add- too dasb button 12293 10:39:48,240 --> 10:39:57,720 and then after this we use do add event 12294 10:39:55,200 --> 10:40:00,240 listener 12295 10:39:57,720 --> 10:40:05,560 brackets inside these brackets we'll 12296 10:40:00,240 --> 10:40:07,840 give it the event the string click comma 12297 10:40:05,560 --> 10:40:12,400 and then a function to run when this 12298 10:40:07,840 --> 10:40:13,520 event happens so brackets arrow and 12299 10:40:12,400 --> 10:40:16,320 curly 12300 10:40:13,520 --> 10:40:19,596 brackets and in here we just want to do 12301 10:40:16,320 --> 10:40:25,720 the same thing as the onclick attribute 12302 10:40:19,596 --> 10:40:26,640 which is ADD too and call this function 12303 10:40:25,720 --> 10:40:30,320 let's 12304 10:40:26,640 --> 10:40:32,916 save and then in the HTML let's remove 12305 10:40:30,320 --> 10:40:35,200 the onclick 12306 10:40:32,916 --> 10:40:38,720 attribute and 12307 10:40:35,200 --> 10:40:39,596 save and let's try it out so we'll type 12308 10:40:38,720 --> 10:40:42,360 A 12309 10:40:39,596 --> 10:40:45,040 too watch 12310 10:40:42,360 --> 10:40:48,680 YouTube select a due 12311 10:40:45,040 --> 10:40:51,520 date and then click add 12312 10:40:48,680 --> 10:40:53,720 and the add button still works as before 12313 10:40:51,520 --> 10:40:56,560 but we're just using add event 12314 10:40:53,720 --> 10:40:59,916 listener next let's switch all the 12315 10:40:56,560 --> 10:41:03,240 delete buttons let's scroll up to where 12316 10:40:59,916 --> 10:41:06,596 we create these delete buttons which is 12317 10:41:03,240 --> 10:41:08,800 here and first We'll add a class to all 12318 10:41:06,596 --> 10:41:09,756 the delete buttons so we can select them 12319 10:41:08,800 --> 10:41:11,720 in 12320 10:41:09,756 --> 10:41:18,596 JavaScript 12321 10:41:11,720 --> 10:41:20,560 js- delete Das too dasb button 12322 10:41:18,596 --> 10:41:23,596 and now we're going to use document. 12323 10:41:20,560 --> 10:41:26,720 Query selector and add an event listener 12324 10:41:23,596 --> 10:41:29,840 to all of these buttons however there's 12325 10:41:26,720 --> 10:41:33,240 two problems here first is that at this 12326 10:41:29,840 --> 10:41:35,560 point in the code this is just a string 12327 10:41:33,240 --> 10:41:39,040 we haven't actually put this button on 12328 10:41:35,560 --> 10:41:41,640 the page yet we do that down here in 12329 10:41:39,040 --> 10:41:45,436 this line of code we put the HTML that 12330 10:41:41,640 --> 10:41:48,000 we generated onto the page so in order 12331 10:41:45,436 --> 10:41:51,360 to add an event listener we need to put 12332 10:41:48,000 --> 10:41:56,240 the HTML on the page first so we have to 12333 10:41:51,360 --> 10:41:58,400 do it after this code so down here we're 12334 10:41:56,240 --> 10:42:01,560 going to type 12335 10:41:58,400 --> 10:42:06,640 document. query 12336 10:42:01,560 --> 10:42:09,200 selector brackets and the string Dot and 12337 10:42:06,640 --> 10:42:15,080 we'll select this class 12338 10:42:09,200 --> 10:42:17,800 js- delete Das too 12339 10:42:15,080 --> 10:42:20,240 Dutton however there's also a second 12340 10:42:17,800 --> 10:42:22,200 problem here which is we're going to 12341 10:42:20,240 --> 10:42:25,560 have multiple delete 12342 10:42:22,200 --> 10:42:28,640 buttons and query selector only gives us 12343 10:42:25,560 --> 10:42:31,000 the first one so how do we select all 12344 10:42:28,640 --> 10:42:34,720 the buttons on the page that match this 12345 10:42:31,000 --> 10:42:37,436 criteria instead of just the first one 12346 10:42:34,720 --> 10:42:40,276 to do that the document object has 12347 10:42:37,436 --> 10:42:41,840 another method called query 12348 10:42:40,276 --> 10:42:45,400 selector 12349 10:42:41,840 --> 10:42:47,756 all so query selector all will give us 12350 10:42:45,400 --> 10:42:52,080 all the elements on the page that have 12351 10:42:47,756 --> 10:42:55,360 the class JS delete Todo button so down 12352 10:42:52,080 --> 10:42:57,880 here let's console.log this result to 12353 10:42:55,360 --> 10:42:59,520 see what it looks like 12354 10:42:57,880 --> 10:43:03,960 [Music] 12355 10:42:59,520 --> 10:43:05,160 console.log brackets and close bracket 12356 10:43:03,960 --> 10:43:08,560 and 12357 10:43:05,160 --> 10:43:10,360 save and then here 12358 10:43:08,560 --> 10:43:12,960 rightclick 12359 10:43:10,360 --> 10:43:17,160 inspect and the 12360 10:43:12,960 --> 10:43:20,320 console and expand this you'll see that 12361 10:43:17,160 --> 10:43:23,720 document. query selector all gives us a 12362 10:43:20,320 --> 10:43:27,800 list of elements and this list works 12363 10:43:23,720 --> 10:43:31,080 just like an array this is index zero or 12364 10:43:27,800 --> 10:43:34,200 the first button and this is index one 12365 10:43:31,080 --> 10:43:36,916 which is the second button so now let's 12366 10:43:34,200 --> 10:43:36,916 remove 12367 10:43:37,960 --> 10:43:42,400 console.log and add event listeners to 12368 10:43:40,880 --> 10:43:45,080 all of these 12369 10:43:42,400 --> 10:43:47,756 buttons to do that we're going to Loop 12370 10:43:45,080 --> 10:43:50,960 through this list of delete buttons 12371 10:43:47,756 --> 10:43:53,960 so again this list works like an array 12372 10:43:50,960 --> 10:43:58,000 so we can use a for each method to Loop 12373 10:43:53,960 --> 10:44:06,360 through it so at the end let's type 12374 10:43:58,000 --> 10:44:09,840 enter and tab and then dot for each and 12375 10:44:06,360 --> 10:44:12,480 brackets as usual we'll give for each a 12376 10:44:09,840 --> 10:44:16,596 function that we want to run for each of 12377 10:44:12,480 --> 10:44:18,800 these values so we'll type brackets 12378 10:44:16,596 --> 10:44:21,480 Arrow and curly 12379 10:44:18,800 --> 10:44:24,240 brackets and now every time we Loop 12380 10:44:21,480 --> 10:44:27,200 through the list for each will give us 12381 10:44:24,240 --> 10:44:29,960 two parameters the first one is the 12382 10:44:27,200 --> 10:44:35,080 value in the list which is a delete 12383 10:44:29,960 --> 10:44:38,116 button element so let's name it delete 12384 10:44:35,080 --> 10:44:42,320 button and we'll type comma and the 12385 10:44:38,116 --> 10:44:45,320 second parameter is the index so 12386 10:44:42,320 --> 10:44:49,040 index so now we want to add an event 12387 10:44:45,320 --> 10:44:56,596 listener to each delete button so inside 12388 10:44:49,040 --> 10:45:00,276 here we'll type delete button dot add 12389 10:44:56,596 --> 10:45:02,680 event listener and 12390 10:45:00,276 --> 10:45:05,436 brackets inside these brackets we'll 12391 10:45:02,680 --> 10:45:10,520 tell it what event we want to listen to 12392 10:45:05,436 --> 10:45:13,000 which is the string click and comma and 12393 10:45:10,520 --> 10:45:17,680 then a function that we want to run when 12394 10:45:13,000 --> 10:45:19,276 this event happens so brackets Arrow and 12395 10:45:17,680 --> 10:45:22,160 curly 12396 10:45:19,276 --> 10:45:24,680 brackets so now when we click a delete 12397 10:45:22,160 --> 10:45:28,436 button we want to do the same thing as 12398 10:45:24,680 --> 10:45:29,560 this onclick attribute above so let's 12399 10:45:28,436 --> 10:45:31,480 select this 12400 10:45:29,560 --> 10:45:33,520 code we'll 12401 10:45:31,480 --> 10:45:36,436 rightclick 12402 10:45:33,520 --> 10:45:40,400 copy and then down 12403 10:45:36,436 --> 10:45:42,840 here right click and 12404 10:45:40,400 --> 10:45:45,916 paste and now we can remove these 12405 10:45:42,840 --> 10:45:48,640 characters because we're not in a string 12406 10:45:45,916 --> 10:45:51,880 anymore and that's all we need to 12407 10:45:48,640 --> 10:45:55,000 do so to summarize we used query 12408 10:45:51,880 --> 10:45:57,640 selector all to get a list of all the 12409 10:45:55,000 --> 10:46:01,320 delete buttons on the page and then we 12410 10:45:57,640 --> 10:46:04,000 looped through the list using four each 12411 10:46:01,320 --> 10:46:06,520 and for each delete button we added a 12412 10:46:04,000 --> 10:46:09,756 click event listener and ran the same 12413 10:46:06,520 --> 10:46:14,916 code that we did before so now we can 12414 10:46:09,756 --> 10:46:18,400 scroll up and remove this onclick 12415 10:46:14,916 --> 10:46:22,040 attribute and Save 12416 10:46:18,400 --> 10:46:23,756 go back to the page close a console and 12417 10:46:22,040 --> 10:46:26,400 try out the delete 12418 10:46:23,756 --> 10:46:29,040 button and you can see that it still 12419 10:46:26,400 --> 10:46:31,596 works so that's how we switch the to-do 12420 10:46:29,040 --> 10:46:34,436 list project to use add event 12421 10:46:31,596 --> 10:46:35,640 listener so now we're finished improving 12422 10:46:34,436 --> 10:46:38,240 all of our 12423 10:46:35,640 --> 10:46:39,960 projects in the rest of this lesson 12424 10:46:38,240 --> 10:46:42,040 we're going to learn more features that 12425 10:46:39,960 --> 10:46:45,756 use functions as 12426 10:46:42,040 --> 10:46:47,360 values so let's close this for now and 12427 10:46:45,756 --> 10:46:51,320 we'll go back to 12428 10:46:47,360 --> 10:46:54,800 12 Advanced functions. 12429 10:46:51,320 --> 10:46:57,880 HTML and we'll open the tab for this 12430 10:46:54,800 --> 10:47:02,116 file and let's also comment out this 12431 10:46:57,880 --> 10:47:05,480 JavaScript code using 12432 10:47:02,116 --> 10:47:07,560 slashstar and star 12433 10:47:05,480 --> 10:47:10,680 slash let's 12434 10:47:07,560 --> 10:47:12,080 save and now let's scroll to the bottom 12435 10:47:10,680 --> 10:47:15,840 of this 12436 10:47:12,080 --> 10:47:18,640 code and create some new 12437 10:47:15,840 --> 10:47:22,080 lines so in this lesson we learned the 12438 10:47:18,640 --> 10:47:24,200 array method for each which lets us Loop 12439 10:47:22,080 --> 10:47:27,000 through an array now we're going to 12440 10:47:24,200 --> 10:47:30,800 learn two more array methods called 12441 10:47:27,000 --> 10:47:33,560 filter and map so let's start with 12442 10:47:30,800 --> 10:47:36,520 Filter let's say we have an array of 12443 10:47:33,560 --> 10:47:39,880 numbers and we want to create a copy of 12444 10:47:36,520 --> 10:47:43,116 this array with all the negative numbers 12445 10:47:39,880 --> 10:47:49,240 removed so let's go up here and create 12446 10:47:43,116 --> 10:47:54,756 an array of the number one comma -3 12447 10:47:49,240 --> 10:47:56,880 comma 5 and type the method do 12448 10:47:54,756 --> 10:48:00,080 filter and 12449 10:47:56,880 --> 10:48:03,840 brackets so Filter Works the same way as 12450 10:48:00,080 --> 10:48:08,000 for each we give it a function so 12451 10:48:03,840 --> 10:48:11,680 bracket arrow and curly bracket and this 12452 10:48:08,000 --> 10:48:14,200 function has two parameters the 12453 10:48:11,680 --> 10:48:16,800 value and the 12454 10:48:14,200 --> 10:48:19,800 index so filter will take the first 12455 10:48:16,800 --> 10:48:21,960 first value save it in here and run the 12456 10:48:19,800 --> 10:48:25,200 function then it will take the second 12457 10:48:21,960 --> 10:48:28,240 value save it in here run the function 12458 10:48:25,200 --> 10:48:31,040 and the third value save it and run the 12459 10:48:28,240 --> 10:48:33,480 function now the only difference is that 12460 10:48:31,040 --> 10:48:37,160 this time this inner function will 12461 10:48:33,480 --> 10:48:39,960 return a value so we'll type 12462 10:48:37,160 --> 10:48:42,320 return and we're going to return a 12463 10:48:39,960 --> 10:48:46,200 Boolean like 12464 10:48:42,320 --> 10:48:49,000 true so now the return value matters so 12465 10:48:46,200 --> 10:48:52,400 what filter does is it creates a new 12466 10:48:49,000 --> 10:48:55,480 array if the inner function returns true 12467 10:48:52,400 --> 10:48:58,000 it will put this value in the new array 12468 10:48:55,480 --> 10:49:01,480 if the inner function returns false it 12469 10:48:58,000 --> 10:49:04,520 will not put this value in the new array 12470 10:49:01,480 --> 10:49:07,680 for example at the front let's 12471 10:49:04,520 --> 10:49:10,200 console.log the result of this so we'll 12472 10:49:07,680 --> 10:49:14,320 type 12473 10:49:10,200 --> 10:49:17,960 console.log Open Bracket and down here 12474 10:49:14,320 --> 10:49:20,400 close bracket and Save 12475 10:49:17,960 --> 10:49:23,596 so in this example the inner function 12476 10:49:20,400 --> 10:49:26,880 always returns true so all three values 12477 10:49:23,596 --> 10:49:31,916 will be in the new array if we change 12478 10:49:26,880 --> 10:49:35,320 the return value to false every time and 12479 10:49:31,916 --> 10:49:36,560 save all three values will not be added 12480 10:49:35,320 --> 10:49:39,720 to the new 12481 10:49:36,560 --> 10:49:42,320 array so now let's use this to solve our 12482 10:49:39,720 --> 10:49:45,560 problem we want to remove the negative 12483 10:49:42,320 --> 10:49:47,880 numbers from this array so inside this 12484 10:49:45,560 --> 10:49:50,276 inner function we're going to return 12485 10:49:47,880 --> 10:49:52,800 true if the number is positive and 12486 10:49:50,276 --> 10:49:57,756 return false if the number is 12487 10:49:52,800 --> 10:50:00,240 negative so we'll check if value is 12488 10:49:57,756 --> 10:50:05,960 greater than or equal to 12489 10:50:00,240 --> 10:50:07,680 zero curly brackets will return true and 12490 10:50:05,960 --> 10:50:09,756 include it in the new 12491 10:50:07,680 --> 10:50:14,880 array 12492 10:50:09,756 --> 10:50:19,240 else will return false and not include 12493 10:50:14,880 --> 10:50:20,240 it in the new array so let's remove this 12494 10:50:19,240 --> 10:50:23,436 and 12495 10:50:20,240 --> 10:50:26,916 save so now only values that are greater 12496 10:50:23,436 --> 10:50:29,596 than zero will be in the new copy so 12497 10:50:26,916 --> 10:50:32,800 using this we can sort of filter out 12498 10:50:29,596 --> 10:50:34,880 values based on a criteria that's why 12499 10:50:32,800 --> 10:50:38,436 this method is called 12500 10:50:34,880 --> 10:50:43,240 filter now we can shorten this logic by 12501 10:50:38,436 --> 10:50:47,520 using this code return value greater 12502 10:50:43,240 --> 10:50:49,960 than or equal to zero so this comparison 12503 10:50:47,520 --> 10:50:52,840 will return true if it's greater than or 12504 10:50:49,960 --> 10:50:55,436 equal to zero and it will return false 12505 10:50:52,840 --> 10:50:59,276 otherwise so this line does the same 12506 10:50:55,436 --> 10:51:03,400 thing as this code up here so we can 12507 10:50:59,276 --> 10:51:05,880 comment this out with slashstar and star 12508 10:51:03,400 --> 10:51:09,040 slash and 12509 10:51:05,880 --> 10:51:12,080 save and as always we can return a 12510 10:51:09,040 --> 10:51:13,560 truthy or falsy value instead of just 12511 10:51:12,080 --> 10:51:15,916 true or 12512 10:51:13,560 --> 10:51:19,640 false the last array method we're going 12513 10:51:15,916 --> 10:51:22,720 to learn is called map so let's create 12514 10:51:19,640 --> 10:51:25,880 some new lines and scroll 12515 10:51:22,720 --> 10:51:33,116 down and let's say we have an array of 12516 10:51:25,880 --> 10:51:35,756 numbers again like the array 1 1 3 and 12517 10:51:33,116 --> 10:51:37,640 we want to make a copy of this array 12518 10:51:35,756 --> 10:51:41,040 with each number 12519 10:51:37,640 --> 10:51:43,756 doubled so at the end we'll 12520 10:51:41,040 --> 10:51:46,560 type map 12521 10:51:43,756 --> 10:51:49,880 brackets and Map works just like for 12522 10:51:46,560 --> 10:51:54,000 each and filter between the brackets 12523 10:51:49,880 --> 10:51:57,040 we'll give it a function bracket arrow 12524 10:51:54,000 --> 10:51:58,520 and curly brackets and this function 12525 10:51:57,040 --> 10:52:01,596 will get two 12526 10:51:58,520 --> 10:52:05,080 parameters the first parameter is each 12527 10:52:01,596 --> 10:52:08,800 value in the array and the second 12528 10:52:05,080 --> 10:52:12,360 parameter comma is the 12529 10:52:08,800 --> 10:52:14,520 index so map will take the first value 12530 10:52:12,360 --> 10:52:16,960 save it in this parameter and run the 12531 10:52:14,520 --> 10:52:18,680 function take the second value 12532 10:52:16,960 --> 10:52:22,200 save it in this parameter run the 12533 10:52:18,680 --> 10:52:24,960 function third value save it and run the 12534 10:52:22,200 --> 10:52:27,840 function and inside this function we're 12535 10:52:24,960 --> 10:52:31,276 going to return a 12536 10:52:27,840 --> 10:52:34,360 value so what map does is it creates a 12537 10:52:31,276 --> 10:52:37,720 new array and then whatever we return 12538 10:52:34,360 --> 10:52:38,880 will be added to the new array so if we 12539 10:52:37,720 --> 10:52:43,320 return 12540 10:52:38,880 --> 10:52:46,320 10 and then we console.log this result 12541 10:52:43,320 --> 10:52:46,320 console.log 12542 10:52:47,080 --> 10:52:52,720 and close bracket and 12543 10:52:49,756 --> 10:52:54,800 save it will create an array where each 12544 10:52:52,720 --> 10:52:57,680 value becomes 12545 10:52:54,800 --> 10:53:00,880 10 now the interesting thing is that we 12546 10:52:57,680 --> 10:53:08,240 can actually use the value itself in the 12547 10:53:00,880 --> 10:53:11,080 return so here if we did value + 10 and 12548 10:53:08,240 --> 10:53:14,596 save it will take each value in the 12549 10:53:11,080 --> 10:53:17,116 original array add a 10 and put it in 12550 10:53:14,596 --> 10:53:20,240 the new array 12551 10:53:17,116 --> 10:53:23,880 so map will transform an array into 12552 10:53:20,240 --> 10:53:26,116 another array based on the return value 12553 10:53:23,880 --> 10:53:28,880 so if we want to return an array where 12554 10:53:26,116 --> 10:53:34,560 each number is doubled we just have to 12555 10:53:28,880 --> 10:53:38,080 multiply each value by two and save and 12556 10:53:34,560 --> 10:53:40,680 this will give us 2 12557 10:53:38,080 --> 10:53:43,000 26 next we're going to review some 12558 10:53:40,680 --> 10:53:44,480 shortcuts of Arrow functions to make 12559 10:53:43,000 --> 10:53:48,240 this code more 12560 10:53:44,480 --> 10:53:51,800 compact first let's make a copy of this 12561 10:53:48,240 --> 10:53:54,000 code rightclick 12562 10:53:51,800 --> 10:53:57,680 copy and down 12563 10:53:54,000 --> 10:54:00,596 here rightclick and 12564 10:53:57,680 --> 10:54:03,400 paste so notice in the inner function 12565 10:54:00,596 --> 10:54:06,000 we're not using index so let's just 12566 10:54:03,400 --> 10:54:08,800 remove it for now and now that this 12567 10:54:06,000 --> 10:54:11,720 Arrow function only has one parameter 12568 10:54:08,800 --> 10:54:14,720 these round brackets are optional so we 12569 10:54:11,720 --> 10:54:18,400 can also remove these to make the code 12570 10:54:14,720 --> 10:54:21,080 more compact another shortcut is if we 12571 10:54:18,400 --> 10:54:24,800 only have one line of code in an arrow 12572 10:54:21,080 --> 10:54:26,000 function we can put the code on one line 12573 10:54:24,800 --> 10:54:29,276 like 12574 10:54:26,000 --> 10:54:32,080 this and now the curly brackets and the 12575 10:54:29,276 --> 10:54:35,960 return are optional so we can actually 12576 10:54:32,080 --> 10:54:35,960 just remove all of this 12577 10:54:36,436 --> 10:54:42,840 code so if we have just an arrow it will 12578 10:54:39,916 --> 10:54:45,320 automatically return this result on the 12579 10:54:42,840 --> 10:54:47,040 right and we don't have to type return 12580 10:54:45,320 --> 10:54:49,640 ourselves 12581 10:54:47,040 --> 10:54:52,596 so this code does the same thing as the 12582 10:54:49,640 --> 10:54:55,916 code above but as you can see these 12583 10:54:52,596 --> 10:54:59,560 shortcuts make our code very Compact and 12584 10:54:55,916 --> 10:55:01,560 clean so let's save and this will give 12585 10:54:59,560 --> 10:55:03,400 us the same 12586 10:55:01,560 --> 10:55:05,520 result the last thing we're going to 12587 10:55:03,400 --> 10:55:08,916 learn in this lesson is a feature of 12588 10:55:05,520 --> 10:55:11,596 functions called a closure a closure 12589 10:55:08,916 --> 10:55:15,000 means if a function has access to a 12590 10:55:11,596 --> 10:55:18,756 value it will always have access to that 12591 10:55:15,000 --> 10:55:20,840 value so let's take a look at an example 12592 10:55:18,756 --> 10:55:24,276 we're going to go back to the to-do list 12593 10:55:20,840 --> 10:55:28,480 project so we'll click up here and then 12594 10:55:24,276 --> 10:55:31,916 scroll up to 12 to-do 12595 10:55:28,480 --> 10:55:35,720 list.js and close this and then open the 12596 10:55:31,916 --> 10:55:40,116 tab for the to-do list which is here and 12597 10:55:35,720 --> 10:55:43,080 let's scroll down to the query selector 12598 10:55:40,116 --> 10:55:46,000 all so we're going to focus on this 12599 10:55:43,080 --> 10:55:50,160 inner function here so notice that this 12600 10:55:46,000 --> 10:55:52,640 fun function uses index from above now 12601 10:55:50,160 --> 10:55:55,680 the interesting thing about index is 12602 10:55:52,640 --> 10:55:59,400 that as soon as this for each ends index 12603 10:55:55,680 --> 10:56:02,880 gets deleted for example if we add some 12604 10:55:59,400 --> 10:56:04,436 new lines and we try to 12605 10:56:02,880 --> 10:56:06,040 console. 12606 10:56:04,436 --> 10:56:08,240 log 12607 10:56:06,040 --> 10:56:12,040 index and 12608 10:56:08,240 --> 10:56:13,800 save and then go into our project 12609 10:56:12,040 --> 10:56:17,880 rightclick 12610 10:56:13,800 --> 10:56:20,160 inspect and the con 12611 10:56:17,880 --> 10:56:23,000 it'll tell us that the index is not 12612 10:56:20,160 --> 10:56:25,960 defined so as soon as we end the for 12613 10:56:23,000 --> 10:56:29,640 each Loop index doesn't exist 12614 10:56:25,960 --> 10:56:36,320 anymore however if we move this into the 12615 10:56:29,640 --> 10:56:36,320 inner function so inside here and we 12616 10:56:36,520 --> 10:56:40,756 console.log 12617 10:56:38,240 --> 10:56:44,680 index and 12618 10:56:40,756 --> 10:56:48,560 save and we click a delete button it 12619 10:56:44,680 --> 10:56:50,596 will tell us that the index is zero so 12620 10:56:48,560 --> 10:56:53,480 even though index gets deleted right 12621 10:56:50,596 --> 10:56:57,596 away down here if we click one of these 12622 10:56:53,480 --> 10:57:01,480 buttons later on it still has access to 12623 10:56:57,596 --> 10:57:04,360 Index this feature is called a closure 12624 10:57:01,480 --> 10:57:07,640 if a function has access to a value it 12625 10:57:04,360 --> 10:57:10,640 will always have access to that value 12626 10:57:07,640 --> 10:57:13,840 the value like index gets packaged 12627 10:57:10,640 --> 10:57:16,800 together or enclosed with the function 12628 10:57:13,840 --> 10:57:19,640 that's why we call it a closure 12629 10:57:16,800 --> 10:57:22,320 so even if the value index gets deleted 12630 10:57:19,640 --> 10:57:25,596 after the loop we can click the delete 12631 10:57:22,320 --> 10:57:28,200 button 5 seconds later or even 5 minutes 12632 10:57:25,596 --> 10:57:29,756 later and this function will always have 12633 10:57:28,200 --> 10:57:32,436 access to 12634 10:57:29,756 --> 10:57:34,840 index so closures are something that 12635 10:57:32,436 --> 10:57:37,000 naturally happens as we write JavaScript 12636 10:57:34,840 --> 10:57:39,720 code and it's nice to know how this 12637 10:57:37,000 --> 10:57:41,800 feature works there are some Advanced 12638 10:57:39,720 --> 10:57:44,000 Techniques that we can do with closures 12639 10:57:41,800 --> 10:57:46,160 but they're not used that often so I 12640 10:57:44,000 --> 10:57:49,276 won't be covering them in this course 12641 10:57:46,160 --> 10:57:50,840 but maybe in a different video now let's 12642 10:57:49,276 --> 10:57:53,520 remove 12643 10:57:50,840 --> 10:57:56,520 console.log and 12644 10:57:53,520 --> 10:57:59,000 save and that's the end of this lesson 12645 10:57:56,520 --> 10:58:02,400 in this lesson we learned that functions 12646 10:57:59,000 --> 10:58:04,840 are values we learned set time out and 12647 10:58:02,400 --> 10:58:07,560 set interval which lets us run a 12648 10:58:04,840 --> 10:58:10,436 function in the future we learned an 12649 10:58:07,560 --> 10:58:13,480 array method called for each which is a 12650 10:58:10,436 --> 10:58:16,160 cleaner way of looping through an array 12651 10:58:13,480 --> 10:58:18,960 we used four each Arrow functions and 12652 10:58:16,160 --> 10:58:21,680 and add event listener to improve our 12653 10:58:18,960 --> 10:58:25,640 projects we learned two more array 12654 10:58:21,680 --> 10:58:28,160 methods filter and map and we learned a 12655 10:58:25,640 --> 10:58:30,756 feature of functions called 12656 10:58:28,160 --> 10:58:32,720 closures here are some exercises to help 12657 10:58:30,756 --> 10:58:35,720 you practice what we learned in this 12658 10:58:32,720 --> 10:58:35,720 lesson 12659 10:59:00,960 --> 10:59:05,560 in this lesson we're going to start the 12660 10:59:03,240 --> 10:59:08,480 Amazon Project and we're also going to 12661 10:59:05,560 --> 10:59:11,520 learn a very useful technology called 12662 10:59:08,480 --> 10:59:14,840 git so first let's take a look at the 12663 10:59:11,520 --> 10:59:16,880 Amazon Project to see how it works we're 12664 10:59:14,840 --> 10:59:20,116 going to go to our browser 12665 10:59:16,880 --> 10:59:25,640 and create a new tab at the top and 12666 10:59:20,116 --> 10:59:32,680 we're going to type super simple dodev 12667 10:59:25,640 --> 10:59:32,680 SL projects SL Amazon and press 12668 10:59:33,040 --> 10:59:38,596 enter so this is going to be the project 12669 10:59:36,200 --> 10:59:41,040 we're going to build in this course this 12670 10:59:38,596 --> 10:59:43,596 is an e-commerce website that looks like 12671 10:59:41,040 --> 10:59:46,436 Amazon where we can see a list of 12672 10:59:43,596 --> 10:59:49,640 products on the homepage and then we can 12673 10:59:46,436 --> 10:59:51,680 add these products to a cart by clicking 12674 10:59:49,640 --> 10:59:54,560 add to 12675 10:59:51,680 --> 10:59:57,436 cart and then we can see our cart by 12676 10:59:54,560 --> 10:59:57,436 clicking the top 12677 10:59:58,596 --> 11:00:04,200 right and on this page it will show us 12678 11:00:01,596 --> 11:00:07,200 which products are in our cart and it 12679 11:00:04,200 --> 11:00:11,360 will also show us the cost of our order 12680 11:00:07,200 --> 11:00:13,720 including shipping and taxes and once 12681 11:00:11,360 --> 11:00:17,040 we're ready to create an order we can 12682 11:00:13,720 --> 11:00:19,276 click this button to create the order 12683 11:00:17,040 --> 11:00:22,840 and it will take us to this page which 12684 11:00:19,276 --> 11:00:25,640 shows us a list of our past orders we 12685 11:00:22,840 --> 11:00:27,560 can also track our order by clicking 12686 11:00:25,640 --> 11:00:30,400 this track package 12687 11:00:27,560 --> 11:00:32,960 button and it will take us to this page 12688 11:00:30,400 --> 11:00:35,520 which tells us the progress of our order 12689 11:00:32,960 --> 11:00:36,520 whether it's preparing it's shipped and 12690 11:00:35,520 --> 11:00:39,116 it's 12691 11:00:36,520 --> 11:00:41,916 delivered so that's an overview of the 12692 11:00:39,116 --> 11:00:44,596 project we're going to built this is a 12693 11:00:41,916 --> 11:00:46,960 complex multi-page website where the 12694 11:00:44,596 --> 11:00:50,160 different pages interact with each other 12695 11:00:46,960 --> 11:00:52,160 to create a full app the first thing 12696 11:00:50,160 --> 11:00:54,916 we're going to do is to download the 12697 11:00:52,160 --> 11:00:57,116 starting code for this project we're 12698 11:00:54,916 --> 11:01:01,800 going to create a new 12699 11:00:57,116 --> 11:01:04,800 tab and here we're going to type 12700 11:01:01,800 --> 11:01:04,800 github.com 12701 11:01:05,680 --> 11:01:13,800 slup 12702 11:01:07,200 --> 11:01:17,840 simple Dev SL JavaScript D Amazon d 12703 11:01:13,800 --> 11:01:17,840 project and press enter 12704 11:01:19,840 --> 11:01:25,840 you can also find the starting code in 12705 11:01:21,800 --> 11:01:28,360 the description by clicking 12706 11:01:25,840 --> 11:01:30,596 here and then going to the links for 12707 11:01:28,360 --> 11:01:30,596 this 12708 11:01:35,916 --> 11:01:42,640 lesson and on this page we can see the 12709 11:01:38,960 --> 11:01:46,240 starting code for the Amazon Project so 12710 11:01:42,640 --> 11:01:48,320 this code contains all the HTML and CSS 12711 11:01:46,240 --> 11:01:50,240 that we need the reason we're going to 12712 11:01:48,320 --> 11:01:52,596 use some starting code instead of 12713 11:01:50,240 --> 11:01:55,520 writing all of this ourselves is because 12714 11:01:52,596 --> 11:01:59,240 this course is focused on JavaScript not 12715 11:01:55,520 --> 11:02:01,276 HTML and CSS if we were to write all of 12716 11:01:59,240 --> 11:02:03,520 this code ourselves it would just take 12717 11:02:01,276 --> 11:02:06,200 up too much time and it would be an 12718 11:02:03,520 --> 11:02:08,200 entire course by itself if you want to 12719 11:02:06,200 --> 11:02:11,840 learn how to create this code you can 12720 11:02:08,200 --> 11:02:13,400 check out my HTML and CSS full course 12721 11:02:11,840 --> 11:02:16,000 which would teach you all the skills 12722 11:02:13,400 --> 11:02:18,240 that you need but for this JavaScript 12723 11:02:16,000 --> 11:02:21,040 course we're going to start with the 12724 11:02:18,240 --> 11:02:24,240 HTML and CSS here and we're just going 12725 11:02:21,040 --> 11:02:27,240 to focus on adding JavaScript to this 12726 11:02:24,240 --> 11:02:29,960 website so now let's download all of 12727 11:02:27,240 --> 11:02:33,040 this code we can do that by clicking 12728 11:02:29,960 --> 11:02:36,000 this green code button here and then 12729 11:02:33,040 --> 11:02:36,000 click download 12730 11:02:36,240 --> 11:02:40,320 zip once that's finished downloading 12731 11:02:38,840 --> 11:02:43,880 we're going to go to our downloads 12732 11:02:40,320 --> 11:02:47,240 folder so for me it's this folder right 12733 11:02:43,880 --> 11:02:50,240 here called downloads 12734 11:02:47,240 --> 11:02:51,840 and then we're going to unzip this file 12735 11:02:50,240 --> 11:02:54,520 by double clicking 12736 11:02:51,840 --> 11:02:56,880 it and this will give us a folder that 12737 11:02:54,520 --> 11:03:01,480 contains all the code so we're going to 12738 11:02:56,880 --> 11:03:01,480 select this folder and move it to our 12739 11:03:03,520 --> 11:03:09,640 desktop next let's go to our desktop so 12740 11:03:07,116 --> 11:03:11,276 we're going to close this for now and 12741 11:03:09,640 --> 11:03:14,116 I'll hide the 12742 11:03:11,276 --> 11:03:16,560 browser and on the desktop we're going 12743 11:03:14,116 --> 11:03:19,116 to select the folder that we downloaded 12744 11:03:16,560 --> 11:03:24,276 and we're going to rename this so at the 12745 11:03:19,116 --> 11:03:27,000 end we'll remove Main and press enter to 12746 11:03:24,276 --> 11:03:29,276 rename it to JavaScript Amazon 12747 11:03:27,000 --> 11:03:33,160 Project next we're going to take this 12748 11:03:29,276 --> 11:03:34,960 folder and open it in our code editor so 12749 11:03:33,160 --> 11:03:38,200 let's open vs 12750 11:03:34,960 --> 11:03:41,040 code and we're going to click 12751 11:03:38,200 --> 11:03:42,916 file open 12752 11:03:41,040 --> 11:03:45,480 folder and we're going to find the 12753 11:03:42,916 --> 11:03:47,880 folder that we downloaded for me it's on 12754 11:03:45,480 --> 11:03:51,360 the desktop and it's this folder right 12755 11:03:47,880 --> 11:03:54,200 here so we'll click this folder and then 12756 11:03:51,360 --> 11:03:56,436 click select 12757 11:03:54,200 --> 11:03:57,840 folder and then we can close this 12758 11:03:56,436 --> 11:04:00,680 welcome 12759 11:03:57,840 --> 11:04:03,320 message okay so now we have our starting 12760 11:04:00,680 --> 11:04:07,240 code and we're going to set up the 12761 11:04:03,320 --> 11:04:10,880 project so first let's open our browser 12762 11:04:07,240 --> 11:04:13,436 again and we can close the GitHub 12763 11:04:10,880 --> 11:04:16,040 page as well as the tabs for the 12764 11:04:13,436 --> 11:04:17,320 previous lessons so we'll close these 12765 11:04:16,040 --> 11:04:20,000 tabs right 12766 11:04:17,320 --> 11:04:21,080 here but we'll leave the final Amazon 12767 11:04:20,000 --> 11:04:24,840 Project 12768 11:04:21,080 --> 11:04:27,320 open next let's go back to vs 12769 11:04:24,840 --> 11:04:29,640 code and we're going to open this 12770 11:04:27,320 --> 11:04:32,360 project in our browser so we're going to 12771 11:04:29,640 --> 11:04:35,840 select this file amazon. 12772 11:04:32,360 --> 11:04:39,680 HTML so this file corresponds to the 12773 11:04:35,840 --> 11:04:40,960 homepage here so let's select this file 12774 11:04:39,680 --> 11:04:45,916 and then 12775 11:04:40,960 --> 11:04:47,276 rightclick open with live server 12776 11:04:45,916 --> 11:04:51,436 so I'm just going to rearrange my 12777 11:04:47,276 --> 11:04:55,000 windows here so we can see these side by 12778 11:04:51,436 --> 11:04:57,000 side and now we're ready to begin so 12779 11:04:55,000 --> 11:05:00,360 let's take a look at the code that we 12780 11:04:57,000 --> 11:05:03,596 have to start with as I mentioned all we 12781 11:05:00,360 --> 11:05:06,080 have here is the HTML and CSS for this 12782 11:05:03,596 --> 11:05:07,800 website there's currently no JavaScript 12783 11:05:06,080 --> 11:05:10,520 at all on this 12784 11:05:07,800 --> 11:05:12,800 page so one thing you'll notice is that 12785 11:05:10,520 --> 11:05:17,000 we can actually create almost all of the 12786 11:05:12,800 --> 11:05:19,040 website using just HTML and CSS we 12787 11:05:17,000 --> 11:05:21,520 actually don't even need JavaScript to 12788 11:05:19,040 --> 11:05:24,320 create all of this we're able to create 12789 11:05:21,520 --> 11:05:27,360 the header at the top as well as three 12790 11:05:24,320 --> 11:05:30,916 products to start with and this shows 12791 11:05:27,360 --> 11:05:33,520 that HTML and CSS can pretty much create 12792 11:05:30,916 --> 11:05:36,276 everything that we see visually on the 12793 11:05:33,520 --> 11:05:39,160 website where JavaScript comes in is 12794 11:05:36,276 --> 11:05:42,080 that JavaScript makes this website 12795 11:05:39,160 --> 11:05:45,596 interactive so right now if we click the 12796 11:05:42,080 --> 11:05:48,000 add to card button it looks pretty nice 12797 11:05:45,596 --> 11:05:49,560 but it doesn't actually do anything and 12798 11:05:48,000 --> 11:05:52,480 that's because we haven't added 12799 11:05:49,560 --> 11:05:55,240 JavaScript yet so the role of JavaScript 12800 11:05:52,480 --> 11:05:58,640 is to make our website interactive after 12801 11:05:55,240 --> 11:06:01,200 we created all the content and The 12802 11:05:58,640 --> 11:06:03,560 Styling now before we begin adding 12803 11:06:01,200 --> 11:06:05,240 JavaScript to this website we're 12804 11:06:03,560 --> 11:06:07,960 actually going to take some time and 12805 11:06:05,240 --> 11:06:11,880 learn another very useful technology 12806 11:06:07,960 --> 11:06:15,320 called git so git is a technology that 12807 11:06:11,880 --> 11:06:18,360 lets us track changes in our code for 12808 11:06:15,320 --> 11:06:20,320 examp example do not follow along here 12809 11:06:18,360 --> 11:06:24,276 but let's say that I'm working on this 12810 11:06:20,320 --> 11:06:26,960 website and I go into my code editor and 12811 11:06:24,276 --> 11:06:29,480 I change some code here so let's say 12812 11:06:26,960 --> 11:06:32,640 that I add some code 12813 11:06:29,480 --> 11:06:34,720 here and then in this file I'm going to 12814 11:06:32,640 --> 11:06:39,756 delete some code so I just select some 12815 11:06:34,720 --> 11:06:42,480 code and delete it and then 12816 11:06:39,756 --> 11:06:46,756 save and then in this file I'm going to 12817 11:06:42,480 --> 11:06:46,756 change some code at the bottom 12818 11:06:47,116 --> 11:06:52,360 so now let's say that I made a mistake 12819 11:06:50,116 --> 11:06:55,436 and I want to undo all the changes that 12820 11:06:52,360 --> 11:06:57,400 I just made so how would I do that how 12821 11:06:55,436 --> 11:07:00,640 would I remember all the code that I 12822 11:06:57,400 --> 11:07:02,840 changed in the different files so right 12823 11:07:00,640 --> 11:07:05,200 now this is really difficult and this is 12824 11:07:02,840 --> 11:07:07,680 a problem that we run into in bigger 12825 11:07:05,200 --> 11:07:09,680 projects we're changing lots of code in 12826 11:07:07,680 --> 11:07:13,200 different files and it's really hard to 12827 11:07:09,680 --> 11:07:16,436 keep track of what code has changed and 12828 11:07:13,200 --> 11:07:19,116 this is where git comes in git lets us 12829 11:07:16,436 --> 11:07:21,040 track changes in our code and it helps 12830 11:07:19,116 --> 11:07:23,680 us solve this 12831 11:07:21,040 --> 11:07:27,080 problem so here's what it looks like if 12832 11:07:23,680 --> 11:07:30,040 I have git set up in my project I can go 12833 11:07:27,080 --> 11:07:33,596 into this section of VSS code this 12834 11:07:30,040 --> 11:07:36,520 section is for git and then here git 12835 11:07:33,596 --> 11:07:38,720 will tell me which files I changed and 12836 11:07:36,520 --> 11:07:41,400 if I click these files it will tell me 12837 11:07:38,720 --> 11:07:45,040 which lines I changed and exactly which 12838 11:07:41,400 --> 11:07:46,000 code I changed if I want to I can click 12839 11:07:45,040 --> 11:07:49,520 this button 12840 11:07:46,000 --> 11:07:52,720 to reverse all the changes in this 12841 11:07:49,520 --> 11:07:56,360 file I can also click here to reverse 12842 11:07:52,720 --> 11:08:00,116 all the changes in all the 12843 11:07:56,360 --> 11:08:03,200 files so as you can see kit helps us 12844 11:08:00,116 --> 11:08:06,520 track changes in our code and it lets us 12845 11:08:03,200 --> 11:08:09,596 easily undo our changes if we need 12846 11:08:06,520 --> 11:08:12,240 to and this technology is really useful 12847 11:08:09,596 --> 11:08:14,240 in bigger projects like this Amazon 12848 11:08:12,240 --> 11:08:17,400 Project where we're going to be changing 12849 11:08:14,240 --> 11:08:19,640 lots of code in in many different files 12850 11:08:17,400 --> 11:08:22,596 so before we start this project we're 12851 11:08:19,640 --> 11:08:25,520 going to learn how to set up and use 12852 11:08:22,596 --> 11:08:29,800 git the first thing we need to do is to 12853 11:08:25,520 --> 11:08:33,000 install git to do that we can go to this 12854 11:08:29,800 --> 11:08:35,800 section of vs code called Source 12855 11:08:33,000 --> 11:08:39,276 control now if this section looks like 12856 11:08:35,800 --> 11:08:42,520 this with a button initialize repository 12857 11:08:39,276 --> 11:08:45,436 that means you already have git 12858 11:08:42,520 --> 11:08:49,276 installed otherwise if you're on Windows 12859 11:08:45,436 --> 11:08:53,720 you can click this button and then click 12860 11:08:49,276 --> 11:08:56,200 open or you can go to this website 12861 11:08:53,720 --> 11:08:58,080 directly and then click this link to 12862 11:08:56,200 --> 11:09:01,000 download 12863 11:08:58,080 --> 11:09:04,040 git once it's finished downloading we're 12864 11:09:01,000 --> 11:09:06,400 going to run the installer and then just 12865 11:09:04,040 --> 11:09:09,400 go with the default 12866 11:09:06,400 --> 11:09:10,680 options if you're using a Mac you can 12867 11:09:09,400 --> 11:09:13,080 click up 12868 11:09:10,680 --> 11:09:17,000 here and then type 12869 11:09:13,080 --> 11:09:19,756 terminal and press enter 12870 11:09:17,000 --> 11:09:21,080 and inside here you're going to type git 12871 11:09:19,756 --> 11:09:24,200 and press 12872 11:09:21,080 --> 11:09:26,840 enter and then click this link and 12873 11:09:24,200 --> 11:09:26,840 install 12874 11:09:30,520 --> 11:09:37,880 git all right after installing git we're 12875 11:09:34,116 --> 11:09:37,880 going to restart vs 12876 11:09:39,640 --> 11:09:43,720 code and make sure you have the Amazon 12877 11:09:42,240 --> 11:09:47,320 Project 12878 11:09:43,720 --> 11:09:50,436 open and now if if we go into the git 12879 11:09:47,320 --> 11:09:52,840 section it should look like this with 12880 11:09:50,436 --> 11:09:55,320 the button initialize 12881 11:09:52,840 --> 11:09:58,360 repository so now that we've installed 12882 11:09:55,320 --> 11:10:01,116 git we're going to set up git in our 12883 11:09:58,360 --> 11:10:04,640 Amazon Project we can do that by 12884 11:10:01,116 --> 11:10:04,640 clicking initialize 12885 11:10:05,596 --> 11:10:10,400 repository if this button doesn't work 12886 11:10:07,840 --> 11:10:15,720 for you you can also try going to the 12887 11:10:10,400 --> 11:10:19,320 terminal menu and click new terminal 12888 11:10:15,720 --> 11:10:22,880 and then inside this terminal type kit 12889 11:10:19,320 --> 11:10:22,880 in it and press 12890 11:10:25,040 --> 11:10:31,916 enter so a repository is just a folder 12891 11:10:29,000 --> 11:10:34,560 where changes are being tracked by 12892 11:10:31,916 --> 11:10:37,116 clicking initialize repository this 12893 11:10:34,560 --> 11:10:40,240 turns the current folder which is 12894 11:10:37,116 --> 11:10:42,756 Javascript Amazon Project into a 12895 11:10:40,240 --> 11:10:45,436 repository and now all the changes 12896 11:10:42,756 --> 11:10:48,680 inside this folder will be tracked by 12897 11:10:45,436 --> 11:10:52,080 git so to start off git will tell us 12898 11:10:48,680 --> 11:10:54,400 that all of these files are new files 12899 11:10:52,080 --> 11:10:56,960 and that's because we just set up this 12900 11:10:54,400 --> 11:10:59,080 repository so all of these files are 12901 11:10:56,960 --> 11:11:01,200 considered new changes that we're 12902 11:10:59,080 --> 11:11:05,080 starting to keep track 12903 11:11:01,200 --> 11:11:08,720 of next we need to configure a username 12904 11:11:05,080 --> 11:11:11,840 and email with Git now don't worry this 12905 11:11:08,720 --> 11:11:14,400 information is not used anywhere online 12906 11:11:11,840 --> 11:11:17,436 this is just forget to add an author to 12907 11:11:14,400 --> 11:11:20,000 the changes that we make so to configure 12908 11:11:17,436 --> 11:11:21,040 a username and email we're going to go 12909 11:11:20,000 --> 11:11:25,200 to the 12910 11:11:21,040 --> 11:11:26,880 top and open the terminal menu and click 12911 11:11:25,200 --> 11:11:29,960 new 12912 11:11:26,880 --> 11:11:35,880 terminal and then inside 12913 11:11:29,960 --> 11:11:39,000 here we're going to type get config 12914 11:11:35,880 --> 11:11:41,680 user.name and double 12915 11:11:39,000 --> 11:11:43,680 quotes now if this looks familiar to you 12916 11:11:41,680 --> 11:11:46,436 and you've already done this before you 12917 11:11:43,680 --> 11:11:49,160 can skip this step and and the next 12918 11:11:46,436 --> 11:11:51,240 step so between these double quotes 12919 11:11:49,160 --> 11:11:58,160 we'll pick a username that we want to 12920 11:11:51,240 --> 11:12:00,200 use so for me I'll use super simple Dev 12921 11:11:58,160 --> 11:12:03,640 and press 12922 11:12:00,200 --> 11:12:10,756 enter and we'll also configure an email 12923 11:12:03,640 --> 11:12:13,480 by typing get config user. Emil double 12924 11:12:10,756 --> 11:12:19,680 quotes and between these double quotes 12925 11:12:13,480 --> 11:12:21,276 we'll put an email so I'll put Simon at 12926 11:12:19,680 --> 11:12:24,680 superimple 12927 11:12:21,276 --> 11:12:27,160 dodev and press 12928 11:12:24,680 --> 11:12:31,880 enter so now that we configured a 12929 11:12:27,160 --> 11:12:31,880 username and email we can close this 12930 11:12:33,276 --> 11:12:39,320 terminal next we're going to save these 12931 11:12:36,200 --> 11:12:43,000 changes into git so that git will start 12932 11:12:39,320 --> 11:12:44,916 tracking any new changes that we make to 12933 11:12:43,000 --> 11:12:47,756 save these changes we're just going to 12934 11:12:44,916 --> 11:12:50,596 type type a message here describing what 12935 11:12:47,756 --> 11:12:55,000 we changed so let's just type the 12936 11:12:50,596 --> 11:12:57,840 message add the starting 12937 11:12:55,000 --> 11:13:00,480 files and then we're going to click 12938 11:12:57,840 --> 11:13:02,756 commit and if this popup shows up we're 12939 11:13:00,480 --> 11:13:05,960 going to click 12940 11:13:02,756 --> 11:13:09,520 yes so that will take all of our changes 12941 11:13:05,960 --> 11:13:12,720 and save them into git so now that we've 12942 11:13:09,520 --> 11:13:14,800 saved those changes into git any new 12943 11:13:12,720 --> 11:13:18,160 changes that we add will start showing 12944 11:13:14,800 --> 11:13:20,596 up here so let's do an example and let's 12945 11:13:18,160 --> 11:13:23,680 change some code in a couple different 12946 11:13:20,596 --> 11:13:27,400 files for practice so let's click this 12947 11:13:23,680 --> 11:13:29,840 icon up here to show our code and let's 12948 11:13:27,400 --> 11:13:33,840 go into amazon. 12949 11:13:29,840 --> 11:13:37,756 HTML and we'll just change this title to 12950 11:13:33,840 --> 11:13:42,000 intro to git and 12951 11:13:37,756 --> 11:13:45,160 save and then let's go into checkout. 12952 11:13:42,000 --> 11:13:49,276 HTML and at the top let's actually 12953 11:13:45,160 --> 11:13:51,436 remove the title and save and then let's 12954 11:13:49,276 --> 11:13:54,880 open orders. 12955 11:13:51,436 --> 11:13:57,400 HTML and at the top let's just add 12956 11:13:54,880 --> 11:13:59,756 something to the title like 12957 11:13:57,400 --> 11:14:02,800 page if we 12958 11:13:59,756 --> 11:14:04,880 save and then go into the source control 12959 11:14:02,800 --> 11:14:07,596 section or the git 12960 11:14:04,880 --> 11:14:09,880 section it will tell us all the changes 12961 11:14:07,596 --> 11:14:12,960 that we just made in our code in the 12962 11:14:09,880 --> 11:14:16,080 three different files if we click one of 12963 11:14:12,960 --> 11:14:19,116 these files it will actually tell us 12964 11:14:16,080 --> 11:14:21,840 exactly which code that we 12965 11:14:19,116 --> 11:14:23,960 changed now if your git section looks 12966 11:14:21,840 --> 11:14:26,560 like this where the changes are on top 12967 11:14:23,960 --> 11:14:28,800 of each other instead of side by side if 12968 11:14:26,560 --> 11:14:31,160 you want to you can change this by 12969 11:14:28,800 --> 11:14:34,320 clicking these three dots here making 12970 11:14:31,160 --> 11:14:36,916 sure that inline view is unchecked and 12971 11:14:34,320 --> 11:14:39,640 also unchecking use inline view when 12972 11:14:36,916 --> 11:14:39,640 space is 12973 11:14:40,040 --> 11:14:45,436 limited so now it's very easy to undo 12974 11:14:43,360 --> 11:14:47,916 this code we can just take the code on 12975 11:14:45,436 --> 11:14:51,240 the left and then copy paste it on the 12976 11:14:47,916 --> 11:14:54,116 right to undo this change we can also 12977 11:14:51,240 --> 11:14:56,400 click here to undo all the changes in 12978 11:14:54,116 --> 11:14:56,400 this 12979 11:14:56,480 --> 11:15:03,320 file and up here we can also click this 12980 11:15:00,040 --> 11:15:05,680 button to undo all the changes in all 12981 11:15:03,320 --> 11:15:09,240 the different 12982 11:15:05,680 --> 11:15:12,276 files so that's an intro to Git it helps 12983 11:15:09,240 --> 11:15:14,680 us keep track of changes in our code and 12984 11:15:12,276 --> 11:15:16,960 it's very useful for bigger projects 12985 11:15:14,680 --> 11:15:19,160 like this Amazon Project where we're 12986 11:15:16,960 --> 11:15:21,640 going to be changing lots of code in 12987 11:15:19,160 --> 11:15:24,000 many different files if you want to 12988 11:15:21,640 --> 11:15:26,880 learn about git in more detail you can 12989 11:15:24,000 --> 11:15:29,160 check out my git and GitHub full course 12990 11:15:26,880 --> 11:15:32,116 in the video 12991 11:15:29,160 --> 11:15:35,640 description so now that we've setup git 12992 11:15:32,116 --> 11:15:38,480 let's start the Amazon Project so first 12993 11:15:35,640 --> 11:15:40,680 make sure that this section is empty and 12994 11:15:38,480 --> 11:15:42,480 we've reversed all the changes that we 12995 11:15:40,680 --> 11:15:45,720 made when practicing 12996 11:15:42,480 --> 11:15:46,960 git next let's click the this icon to 12997 11:15:45,720 --> 11:15:51,916 show our 12998 11:15:46,960 --> 11:15:51,916 files and let's remove the previous 12999 11:15:51,960 --> 11:15:58,200 code and we're going to start in the 13000 11:15:54,436 --> 11:15:58,200 amazon. HTML 13001 11:15:59,596 --> 11:16:06,320 file and let's close the git page and 13002 11:16:03,520 --> 11:16:09,520 then open the tab for this file it 13003 11:16:06,320 --> 11:16:12,560 should say amazon. HTML up 13004 11:16:09,520 --> 11:16:15,880 here so we'll start by creating the list 13005 11:16:12,560 --> 11:16:18,840 of products on the homepage so right now 13006 11:16:15,880 --> 11:16:21,720 we start with three products if we go to 13007 11:16:18,840 --> 11:16:24,436 the final project we're going to add the 13008 11:16:21,720 --> 11:16:25,756 rest of the products that you see here 13009 11:16:24,436 --> 11:16:28,200 and we're also going to make this 13010 11:16:25,756 --> 11:16:31,320 interactive so that when we click the 13011 11:16:28,200 --> 11:16:33,480 add to card button it will add this 13012 11:16:31,320 --> 11:16:36,680 product to our 13013 11:16:33,480 --> 11:16:39,520 cart so let's go back to our 13014 11:16:36,680 --> 11:16:42,800 project and in order to create this 13015 11:16:39,520 --> 11:16:44,916 we're going to learn the main idea of 13016 11:16:42,800 --> 11:16:47,880 JavaScript so when using JavaScript 13017 11:16:44,916 --> 11:16:51,320 script we usually do a three-step 13018 11:16:47,880 --> 11:16:54,840 process the first step is to save the 13019 11:16:51,320 --> 11:16:58,480 data and then we generate the 13020 11:16:54,840 --> 11:17:01,040 HTML and finally we make it 13021 11:16:58,480 --> 11:17:03,320 interactive let's go through each step 13022 11:17:01,040 --> 11:17:06,040 to create this list of 13023 11:17:03,320 --> 11:17:08,596 products so right now we have three 13024 11:17:06,040 --> 11:17:11,080 products on our page to start with and 13025 11:17:08,596 --> 11:17:14,840 we're going to add more products to this 13026 11:17:11,080 --> 11:17:17,276 page so let's start by finding the HTML 13027 11:17:14,840 --> 11:17:19,200 for one of these products because we can 13028 11:17:17,276 --> 11:17:21,596 always just copy and paste this to 13029 11:17:19,200 --> 11:17:24,040 create another one now we're going to 13030 11:17:21,596 --> 11:17:27,080 learn a very important skill for bigger 13031 11:17:24,040 --> 11:17:29,800 projects which is how to find the code 13032 11:17:27,080 --> 11:17:32,080 for something on this page so let's say 13033 11:17:29,800 --> 11:17:35,320 that we want to find the code for one of 13034 11:17:32,080 --> 11:17:38,200 these products we're going to 13035 11:17:35,320 --> 11:17:40,880 rightclick and click 13036 11:17:38,200 --> 11:17:43,116 inspect and this will bring up the 13037 11:17:40,880 --> 11:17:46,956 console and I'm actually going to click 13038 11:17:43,116 --> 11:17:49,320 these three dots and click this icon to 13039 11:17:46,956 --> 11:17:52,276 move the console to a separate window so 13040 11:17:49,320 --> 11:17:54,240 that we can see the whole web page so 13041 11:17:52,276 --> 11:17:55,640 now we're going to click this icon in 13042 11:17:54,240 --> 11:17:58,596 the top 13043 11:17:55,640 --> 11:18:00,800 left and we're going to hover over and 13044 11:17:58,596 --> 11:18:03,480 click the thing that we want to get the 13045 11:18:00,800 --> 11:18:06,560 code for so I want to get the code for 13046 11:18:03,480 --> 11:18:07,680 this entire first product so I'll click 13047 11:18:06,560 --> 11:18:10,040 in this 13048 11:18:07,680 --> 11:18:13,596 space and this will tell us that the 13049 11:18:10,040 --> 11:18:15,840 code for the product is this code a div 13050 11:18:13,596 --> 11:18:18,040 with the class product - 13051 11:18:15,840 --> 11:18:21,080 container so now we're going to take 13052 11:18:18,040 --> 11:18:24,640 this class and then search for it in our 13053 11:18:21,080 --> 11:18:27,880 code editor so let's open vs 13054 11:18:24,640 --> 11:18:31,756 code and we're going to type contrl f on 13055 11:18:27,880 --> 11:18:34,956 Windows or command F on Mac to search 13056 11:18:31,756 --> 11:18:38,040 for code in this file so we're going to 13057 11:18:34,956 --> 11:18:40,480 search for the class that we saw earlier 13058 11:18:38,040 --> 11:18:43,040 which is 13059 11:18:40,480 --> 11:18:45,596 product- 13060 11:18:43,040 --> 11:18:49,276 container and this will take us to the 13061 11:18:45,596 --> 11:18:51,916 code for one of these products so we can 13062 11:18:49,276 --> 11:18:54,680 actually click the arrow on the left to 13063 11:18:51,916 --> 11:18:57,080 collapse this code and we'll see another 13064 11:18:54,680 --> 11:18:59,480 product container which corresponds to 13065 11:18:57,080 --> 11:19:02,160 the second product we can click this 13066 11:18:59,480 --> 11:19:04,840 Arrow again and we'll see a third 13067 11:19:02,160 --> 11:19:05,956 product container which corresponds to 13068 11:19:04,840 --> 11:19:08,756 the third 13069 11:19:05,956 --> 11:19:11,756 product so that's how we find the code 13070 11:19:08,756 --> 11:19:14,160 for something on the page now to add 13071 11:19:11,756 --> 11:19:17,640 another product to this page one thing 13072 11:19:14,160 --> 11:19:20,596 we can do is to take this HTML and just 13073 11:19:17,640 --> 11:19:24,200 make a copy of it at the bottom of the 13074 11:19:20,596 --> 11:19:26,680 page however this is not a good idea 13075 11:19:24,200 --> 11:19:29,916 because if we add lots of products we're 13076 11:19:26,680 --> 11:19:33,560 going to be copying this HTML over and 13077 11:19:29,916 --> 11:19:36,880 over and if we expand this you can see 13078 11:19:33,560 --> 11:19:38,720 that each product has a lot of HTML code 13079 11:19:36,880 --> 11:19:41,080 so we don't want to be copying all of 13080 11:19:38,720 --> 11:19:42,800 this over and over again it's going to 13081 11:19:41,080 --> 11:19:45,916 be a lot of code and it's going to be 13082 11:19:42,800 --> 11:19:49,840 hard to manage in instead another 13083 11:19:45,916 --> 11:19:52,800 strategy is to generate this HTML code 13084 11:19:49,840 --> 11:19:56,116 using JavaScript so let's learn how to 13085 11:19:52,800 --> 11:19:58,840 do that before we can do that we need to 13086 11:19:56,116 --> 11:20:02,160 add JavaScript to this page so we're 13087 11:19:58,840 --> 11:20:04,436 going to create a Javascript file so 13088 11:20:02,160 --> 11:20:08,160 let's click up here to show our 13089 11:20:04,436 --> 11:20:11,360 files and first let's create a new 13090 11:20:08,160 --> 11:20:14,840 folder to contain all of our JavaScript 13091 11:20:11,360 --> 11:20:17,436 code so let's name this folder SC 13092 11:20:14,840 --> 11:20:21,000 scripts and press 13093 11:20:17,436 --> 11:20:23,240 enter next let's click on our folder and 13094 11:20:21,000 --> 11:20:27,680 we're going to click this icon to create 13095 11:20:23,240 --> 11:20:32,720 a new file and we'll name this file 13096 11:20:27,680 --> 11:20:36,360 amazon. JS and press enter so this will 13097 11:20:32,720 --> 11:20:40,720 be the Javascript file for this HTML 13098 11:20:36,360 --> 11:20:43,560 file next inside this Javascript file 13099 11:20:40,720 --> 11:20:45,480 let's type the code 13100 11:20:43,560 --> 11:20:47,080 console. 13101 11:20:45,480 --> 11:20:50,360 log 13102 11:20:47,080 --> 11:20:51,840 hello just to make sure that this works 13103 11:20:50,360 --> 11:20:54,436 and 13104 11:20:51,840 --> 11:20:58,040 save next we're going to load this 13105 11:20:54,436 --> 11:21:01,160 Javascript file in our HTML file so 13106 11:20:58,040 --> 11:21:04,360 let's go back to amazon. 13107 11:21:01,160 --> 11:21:07,040 HTML and then at the bottom of the code 13108 11:21:04,360 --> 11:21:10,320 we're going to scroll down and then 13109 11:21:07,040 --> 11:21:14,756 right above the closing tag for the body 13110 11:21:10,320 --> 11:21:16,520 we're going to add a script element 13111 11:21:14,756 --> 11:21:21,160 and we're going to give this element an 13112 11:21:16,520 --> 11:21:23,596 attribute SRC or Source equals double 13113 11:21:21,160 --> 11:21:26,916 quotes and we're going to load this 13114 11:21:23,596 --> 11:21:30,680 Javascript file so this Javascript file 13115 11:21:26,916 --> 11:21:33,080 is in a folder called scripts so here 13116 11:21:30,680 --> 11:21:36,276 we're going to type the folder name 13117 11:21:33,080 --> 11:21:39,720 scripts and then a forward slash to go 13118 11:21:36,276 --> 11:21:43,080 into the folder and we'll load the file 13119 11:21:39,720 --> 11:21:46,040 amazon. JS and 13120 11:21:43,080 --> 11:21:49,400 save so now let let's go back to the 13121 11:21:46,040 --> 11:21:51,080 page and we'll open the console right 13122 11:21:49,400 --> 11:21:53,480 click 13123 11:21:51,080 --> 11:21:56,956 inspect and then click the 13124 11:21:53,480 --> 11:21:59,116 console and it should display hello so 13125 11:21:56,956 --> 11:22:03,040 now we're loading JavaScript on this 13126 11:21:59,116 --> 11:22:05,916 page so let's open up vs code 13127 11:22:03,040 --> 11:22:10,276 again and we're going to generate all of 13128 11:22:05,916 --> 11:22:13,040 this HTML using JavaScript instead so 13129 11:22:10,276 --> 11:22:17,720 the first step in our process is to save 13130 11:22:13,040 --> 11:22:21,040 the data what does this mean data means 13131 11:22:17,720 --> 11:22:24,200 information so in this case the data is 13132 11:22:21,040 --> 11:22:27,680 the information about our products for 13133 11:22:24,200 --> 11:22:31,640 example the products name the price 13134 11:22:27,680 --> 11:22:34,116 ratings and the image so right now the 13135 11:22:31,640 --> 11:22:36,756 information about the products or the 13136 11:22:34,116 --> 11:22:41,160 data is inside our 13137 11:22:36,756 --> 11:22:44,080 HTML so in our code if we click here to 13138 11:22:41,160 --> 11:22:44,956 expand this element you can see that the 13139 11:22:44,080 --> 11:22:48,080 inform 13140 11:22:44,956 --> 11:22:51,360 about the products for example the name 13141 11:22:48,080 --> 11:22:54,680 and the image is inside the 13142 11:22:51,360 --> 11:22:57,560 HTML so what we need to do is to take 13143 11:22:54,680 --> 11:23:00,080 this data and save it inside our 13144 11:22:57,560 --> 11:23:03,800 JavaScript so that our JavaScript can 13145 11:23:00,080 --> 11:23:06,596 use it so let's go to our Javascript 13146 11:23:03,800 --> 11:23:09,916 file and I'm just going to put it down 13147 11:23:06,596 --> 11:23:13,000 here so we can see it side by 13148 11:23:09,916 --> 11:23:14,956 side and we're going to move this data 13149 11:23:13,000 --> 11:23:16,720 into our JavaScript 13150 11:23:14,956 --> 11:23:19,436 so let's remove 13151 11:23:16,720 --> 11:23:23,880 console.log and let's create a variable 13152 11:23:19,436 --> 11:23:25,680 to save the data so we'll type const and 13153 11:23:23,880 --> 11:23:27,720 let's name it 13154 11:23:25,680 --> 11:23:30,360 products and make it 13155 11:23:27,720 --> 11:23:34,000 equal and now we want to create 13156 11:23:30,360 --> 11:23:36,756 something that closely matches our data 13157 11:23:34,000 --> 11:23:39,800 so in this case we have a list of 13158 11:23:36,756 --> 11:23:43,240 products so in here we're going to 13159 11:23:39,800 --> 11:23:46,320 create an array because an array 13160 11:23:43,240 --> 11:23:46,320 represents a list 13161 11:23:46,400 --> 11:23:53,320 and now each product in the list has 13162 11:23:49,116 --> 11:23:56,800 many values like an image a name and a 13163 11:23:53,320 --> 11:24:00,200 price so we're going to use an object to 13164 11:23:56,800 --> 11:24:04,040 represent each product because an object 13165 11:24:00,200 --> 11:24:07,800 lets us group multiple values 13166 11:24:04,040 --> 11:24:11,720 together so inside this array we're 13167 11:24:07,800 --> 11:24:14,916 going to create an object using curly 13168 11:24:11,720 --> 11:24:16,880 brackets and now inside this object 13169 11:24:14,916 --> 11:24:20,080 we're going to save the data for the 13170 11:24:16,880 --> 11:24:23,320 first product so first let's find the 13171 11:24:20,080 --> 11:24:26,200 data in our HTML we're going to search 13172 11:24:23,320 --> 11:24:30,560 for the product's name by typing contrl 13173 11:24:26,200 --> 11:24:33,400 F or command F and we'll search for 13174 11:24:30,560 --> 11:24:35,880 black and 13175 11:24:33,400 --> 11:24:39,000 gray and this will take us to the code 13176 11:24:35,880 --> 11:24:41,596 for the first product so this is the 13177 11:24:39,000 --> 11:24:44,200 name of the first product so let's 13178 11:24:41,596 --> 11:24:47,200 actually scroll up and we're going to 13179 11:24:44,200 --> 11:24:49,040 get get the image for the first product 13180 11:24:47,200 --> 11:24:52,240 and we're going to take this and save it 13181 11:24:49,040 --> 11:24:55,276 in our JavaScript so let's select this 13182 11:24:52,240 --> 11:24:57,276 code we're going to 13183 11:24:55,276 --> 11:25:00,520 rightclick 13184 11:24:57,276 --> 11:25:05,596 copy and then inside this object let's 13185 11:25:00,520 --> 11:25:09,436 create a property called image colon and 13186 11:25:05,596 --> 11:25:13,520 then here right click and 13187 11:25:09,436 --> 11:25:16,360 paste and let's save this as a string so 13188 11:25:13,520 --> 11:25:19,480 at the front we'll type a single quote 13189 11:25:16,360 --> 11:25:22,720 and at the end we'll type a single 13190 11:25:19,480 --> 11:25:23,596 quote so we just saved the image inside 13191 11:25:22,720 --> 11:25:26,160 our 13192 11:25:23,596 --> 11:25:29,240 JavaScript let's move on and save the 13193 11:25:26,160 --> 11:25:32,840 name inside our JavaScript so we'll 13194 11:25:29,240 --> 11:25:34,000 scroll down we'll select this code and 13195 11:25:32,840 --> 11:25:35,916 then 13196 11:25:34,000 --> 11:25:40,080 rightclick 13197 11:25:35,916 --> 11:25:43,956 copy and in this object we'll type comma 13198 11:25:40,080 --> 11:25:45,480 and a property called name col in a 13199 11:25:43,956 --> 11:25:50,200 string 13200 11:25:45,480 --> 11:25:50,200 and we'll right click and 13201 11:25:50,756 --> 11:25:57,160 paste next we're going to save the 13202 11:25:53,320 --> 11:26:00,916 rating into our JavaScript so we'll type 13203 11:25:57,160 --> 11:26:02,480 comma and let's create a property called 13204 11:26:00,916 --> 11:26:05,080 rating 13205 11:26:02,480 --> 11:26:08,436 colon for the rating there's actually 13206 11:26:05,080 --> 11:26:11,200 two parts the stars and the number of 13207 11:26:08,436 --> 11:26:14,240 ratings that it has so let's actually 13208 11:26:11,200 --> 11:26:16,360 group these two values into an object 13209 11:26:14,240 --> 11:26:20,116 because these values are related to each 13210 11:26:16,360 --> 11:26:22,000 other so in here we'll make the rating 13211 11:26:20,116 --> 11:26:26,720 and 13212 11:26:22,000 --> 11:26:30,160 object and each rating will have the 13213 11:26:26,720 --> 11:26:33,956 stars and for the socks it's 13214 11:26:30,160 --> 11:26:35,040 4.5 comma and the rating will also have 13215 11:26:33,956 --> 11:26:39,640 a 13216 11:26:35,040 --> 11:26:43,560 count and for the first product it's 13217 11:26:39,640 --> 11:26:44,840 87 next let's save the price into our 13218 11:26:43,560 --> 11:26:49,560 JavaScript 13219 11:26:44,840 --> 11:26:52,276 so we'll create another property called 13220 11:26:49,560 --> 11:26:55,000 price and for the first product the 13221 11:26:52,276 --> 11:26:55,000 price is 13222 11:26:55,756 --> 11:27:01,320 $10.90 however JavaScript has problems 13223 11:26:58,916 --> 11:27:04,720 doing math with decimal numbers or 13224 11:27:01,320 --> 11:27:07,436 floats like this so a best practice when 13225 11:27:04,720 --> 11:27:10,560 calculating money is to calculate in 13226 11:27:07,436 --> 11:27:13,200 cents instead of dollars so for this 13227 11:27:10,560 --> 11:27:17,800 reason we're going to save the price as 13228 11:27:13,200 --> 11:27:22,596 cents so we'll save it as 13229 11:27:17,800 --> 11:27:25,640 1,90 as a reminder $1 is equal to 100 13230 11:27:22,596 --> 11:27:28,840 cents and because we're saving in cents 13231 11:27:25,640 --> 11:27:31,080 it's helpful to rename this property to 13232 11:27:28,840 --> 11:27:34,916 price cents 13233 11:27:31,080 --> 11:27:37,956 instead so now if we scroll up we just 13234 11:27:34,916 --> 11:27:41,436 saved all the information or the data 13235 11:27:37,956 --> 11:27:45,000 about the first product into our 13236 11:27:41,436 --> 11:27:49,200 JavaScript next let's go to the end here 13237 11:27:45,000 --> 11:27:51,200 and type comma and create another object 13238 11:27:49,200 --> 11:27:54,720 and we're going to save the data for the 13239 11:27:51,200 --> 11:27:56,520 second product into this object so feel 13240 11:27:54,720 --> 11:27:58,640 free to pause the video if you want to 13241 11:27:56,520 --> 11:28:01,956 try it yourself 13242 11:27:58,640 --> 11:28:05,916 first so again let's find this product 13243 11:28:01,956 --> 11:28:09,200 in our HTML we'll go to our HTML and 13244 11:28:05,916 --> 11:28:12,320 type contrl f or command F to search for 13245 11:28:09,200 --> 11:28:15,320 code and we'll search for the name 13246 11:28:12,320 --> 11:28:15,320 intermediate 13247 11:28:15,400 --> 11:28:21,320 size basketball so now this is the code 13248 11:28:18,956 --> 11:28:24,276 for the second product we're going to 13249 11:28:21,320 --> 11:28:26,880 scroll up a bit and find the image for 13250 11:28:24,276 --> 11:28:29,880 this product and we're going to select 13251 11:28:26,880 --> 11:28:33,436 it rightclick 13252 11:28:29,880 --> 11:28:36,956 copy and then inside the second object 13253 11:28:33,436 --> 11:28:38,520 we'll create an image property just like 13254 11:28:36,956 --> 11:28:42,276 we did up 13255 11:28:38,520 --> 11:28:46,240 here and we'll save it as a string and 13256 11:28:42,276 --> 11:28:49,436 we're going to rightclick and 13257 11:28:46,240 --> 11:28:53,680 paste and let's type comma and we'll 13258 11:28:49,436 --> 11:28:57,800 save the name inside the second object 13259 11:28:53,680 --> 11:29:01,800 so we'll scroll down and select the 13260 11:28:57,800 --> 11:29:06,000 name and then right click 13261 11:29:01,800 --> 11:29:08,956 copy and then down here create a string 13262 11:29:06,000 --> 11:29:12,276 and rightclick and 13263 11:29:08,956 --> 11:29:15,000 paste and we'll type a comma and we'll 13264 11:29:12,276 --> 11:29:16,756 save the rating 13265 11:29:15,000 --> 11:29:19,360 colon 13266 11:29:16,756 --> 11:29:21,720 object and we'll save the number of 13267 11:29:19,360 --> 11:29:24,956 stars which is 13268 11:29:21,720 --> 11:29:28,756 four and the number of ratings that it 13269 11:29:24,956 --> 11:29:32,800 has count colon 13270 11:29:28,756 --> 11:29:36,800 127 and finally we'll type a comma and 13271 11:29:32,800 --> 11:29:41,200 we'll save the price of the product in 13272 11:29:36,800 --> 11:29:41,200 cents colon 13273 11:29:41,596 --> 11:29:47,080 295 so now the data for these second 13274 11:29:44,320 --> 11:29:50,116 product is saved inside our 13275 11:29:47,080 --> 11:29:53,756 JavaScript let's go to the end and type 13276 11:29:50,116 --> 11:29:55,596 comma and create another object and 13277 11:29:53,756 --> 11:29:58,840 we're going to save the data for the 13278 11:29:55,596 --> 11:30:02,040 third product inside again feel free to 13279 11:29:58,840 --> 11:30:04,840 pause the video to try it yourself first 13280 11:30:02,040 --> 11:30:08,240 so let's save the image we'll find the 13281 11:30:04,840 --> 11:30:11,800 code for this product in our HTML so up 13282 11:30:08,240 --> 11:30:15,400 here we'll typ contrl F or command 13283 11:30:11,800 --> 11:30:17,480 F and we'll search for the product name 13284 11:30:15,400 --> 11:30:20,320 which is 13285 11:30:17,480 --> 11:30:23,560 adults plain cotton 13286 11:30:20,320 --> 11:30:25,800 t-shirt and scroll up and we're going to 13287 11:30:23,560 --> 11:30:31,040 save the image first so let's select 13288 11:30:25,800 --> 11:30:34,480 this right click copy and then down here 13289 11:30:31,040 --> 11:30:38,640 create an image property colon and a 13290 11:30:34,480 --> 11:30:41,840 string and then right click and 13291 11:30:38,640 --> 11:30:45,756 paste and we'll create a comma and let's 13292 11:30:41,840 --> 11:30:48,116 save the name in our Javas script next 13293 11:30:45,756 --> 11:30:51,240 create a string and then we'll scroll 13294 11:30:48,116 --> 11:30:53,040 down here we'll select the 13295 11:30:51,240 --> 11:30:54,756 name right 13296 11:30:53,040 --> 11:30:59,720 click 13297 11:30:54,756 --> 11:31:03,800 copy and then in here right click and 13298 11:30:59,720 --> 11:31:07,640 paste and comma and we'll save the 13299 11:31:03,800 --> 11:31:11,720 rating colon and object and we have the 13300 11:31:07,640 --> 11:31:15,160 number of stars for this one it's 13301 11:31:11,720 --> 11:31:20,080 4.5 colon and the 13302 11:31:15,160 --> 11:31:20,080 count and for this one it is 13303 11:31:20,480 --> 11:31:27,756 56 and lastly type a comma and we'll 13304 11:31:24,640 --> 11:31:31,880 save the price in 13305 11:31:27,756 --> 11:31:35,956 cents colon 13306 11:31:31,880 --> 11:31:39,080 799 and now let's save our Javascript 13307 11:31:35,956 --> 11:31:41,276 file now one really important thing is 13308 11:31:39,080 --> 11:31:44,560 to make sure that in each of these 13309 11:31:41,276 --> 11:31:47,080 objects we have the same property so we 13310 11:31:44,560 --> 11:31:49,840 have the image property the name 13311 11:31:47,080 --> 11:31:52,360 property and the same rating property 13312 11:31:49,840 --> 11:31:54,800 with the same structure we want all of 13313 11:31:52,360 --> 11:31:57,360 our objects to have the same structure 13314 11:31:54,800 --> 11:31:58,596 so that our code can handle each product 13315 11:31:57,360 --> 11:32:01,680 the 13316 11:31:58,596 --> 11:32:04,840 same so that's what it means to save the 13317 11:32:01,680 --> 11:32:07,080 data we create something in JavaScript 13318 11:32:04,840 --> 11:32:10,800 that closely matches the data that we 13319 11:32:07,080 --> 11:32:13,480 have on the Page by the way this is 13320 11:32:10,800 --> 11:32:15,680 called a data structure because its 13321 11:32:13,480 --> 11:32:18,840 structure structures or organizes the 13322 11:32:15,680 --> 11:32:21,640 data and this represents a list of 13323 11:32:18,840 --> 11:32:24,560 products so we usually use a combination 13324 11:32:21,640 --> 11:32:26,080 of objects and arrays to create a data 13325 11:32:24,560 --> 11:32:28,720 structure in 13326 11:32:26,080 --> 11:32:32,360 JavaScript so now that we've saved the 13327 11:32:28,720 --> 11:32:35,880 data the next step is to use this data 13328 11:32:32,360 --> 11:32:38,436 to generate the HTML instead of writing 13329 11:32:35,880 --> 11:32:41,840 the HTML 13330 11:32:38,436 --> 11:32:45,000 manually to generate the HTML we can 13331 11:32:41,840 --> 11:32:47,276 Loop through this array and for each of 13332 11:32:45,000 --> 11:32:48,080 these products we're going to create 13333 11:32:47,276 --> 11:32:51,116 some 13334 11:32:48,080 --> 11:32:55,116 HTML so let's go ahead and loop through 13335 11:32:51,116 --> 11:32:56,240 this array first we'll scroll down to 13336 11:32:55,116 --> 11:32:58,680 the 13337 11:32:56,240 --> 11:33:02,360 bottom and we'll type the name of the 13338 11:32:58,680 --> 11:33:05,480 array which is products and then we'll 13339 11:33:02,360 --> 11:33:11,756 Loop through this array using a for each 13340 11:33:05,480 --> 11:33:15,320 method so we'll type Dot 4 Capital each 13341 11:33:11,756 --> 11:33:19,360 brackets and inside we create a function 13342 11:33:15,320 --> 11:33:22,276 using brackets arrow and curly 13343 11:33:19,360 --> 11:33:24,400 brackets and we'll give it a parameter 13344 11:33:22,276 --> 11:33:26,640 called 13345 11:33:24,400 --> 11:33:29,116 Product so I'm going to move this back 13346 11:33:26,640 --> 11:33:32,680 to the top here so we can see more of 13347 11:33:29,116 --> 11:33:35,596 the code so the way that for each works 13348 11:33:32,680 --> 11:33:37,880 is that it takes each object saves it in 13349 11:33:35,596 --> 11:33:40,720 this parameter called Product and then 13350 11:33:37,880 --> 11:33:43,436 runs the function so it takes the first 13351 11:33:40,720 --> 11:33:45,756 object saves it in product runs the 13352 11:33:43,436 --> 11:33:48,640 function function takes a second object 13353 11:33:45,756 --> 11:33:51,840 saves a product runs a function and so 13354 11:33:48,640 --> 11:33:56,320 on so for each of these products or each 13355 11:33:51,840 --> 11:33:59,276 of these objects we want to create some 13356 11:33:56,320 --> 11:34:03,436 HTML so inside this function let's 13357 11:33:59,276 --> 11:34:06,956 create a variable const 13358 11:34:03,436 --> 11:34:09,720 HTML equals and we'll create a template 13359 11:34:06,956 --> 11:34:12,160 string using back 13360 11:34:09,720 --> 11:34:14,040 ticks and now inside this template 13361 11:34:12,160 --> 11:34:17,160 string we're basically going going to 13362 11:34:14,040 --> 11:34:21,040 copy all the HTML for one of these 13363 11:34:17,160 --> 11:34:24,640 products so let's go back to our HTML 13364 11:34:21,040 --> 11:34:27,320 file and how do we find the HTML for one 13365 11:34:24,640 --> 11:34:28,680 product so we learned this earlier we 13366 11:34:27,320 --> 11:34:31,040 can 13367 11:34:28,680 --> 11:34:34,116 rightclick 13368 11:34:31,040 --> 11:34:36,880 inspect and then click the top left 13369 11:34:34,116 --> 11:34:41,360 icon and click the element we want to 13370 11:34:36,880 --> 11:34:43,880 get the HTML for so this HTML represents 13371 11:34:41,360 --> 11:34:47,916 one product and we can search for this 13372 11:34:43,880 --> 11:34:50,080 class product- container in our code so 13373 11:34:47,916 --> 11:34:54,080 we'll go back to our vs 13374 11:34:50,080 --> 11:34:57,360 code inside the HTML file we can contrl 13375 11:34:54,080 --> 11:35:03,240 f or command F to search for code and 13376 11:34:57,360 --> 11:35:05,800 we'll search for the class product Das 13377 11:35:03,240 --> 11:35:08,560 container and let's go to the first 13378 11:35:05,800 --> 11:35:12,040 result so we'll go to the first result 13379 11:35:08,560 --> 11:35:15,436 which is here and let's copy all of the 13380 11:35:12,040 --> 11:35:17,560 code inside this ele element one trick 13381 11:35:15,436 --> 11:35:21,080 we can do is to click the arrow on the 13382 11:35:17,560 --> 11:35:24,880 left to collapse it and then we'll 13383 11:35:21,080 --> 11:35:26,840 select all this code and we'll 13384 11:35:24,880 --> 11:35:30,800 rightclick 13385 11:35:26,840 --> 11:35:33,916 copy and then inside our Javascript file 13386 11:35:30,800 --> 11:35:37,080 inside the string we're just going to 13387 11:35:33,916 --> 11:35:39,240 rightclick and paste the 13388 11:35:37,080 --> 11:35:42,520 HTML and if we need to fix the 13389 11:35:39,240 --> 11:35:46,520 formatting we can select all of this 13390 11:35:42,520 --> 11:35:49,916 code and then press tab a few times to 13391 11:35:46,520 --> 11:35:52,880 add some indexs and here we can remove 13392 11:35:49,916 --> 11:35:56,000 the indent and let's 13393 11:35:52,880 --> 11:35:59,320 save so now we're basically looping 13394 11:35:56,000 --> 11:36:02,200 through the array of products and for 13395 11:35:59,320 --> 11:36:05,000 each product we're creating all of the 13396 11:36:02,200 --> 11:36:08,520 HTML that we had before so we're 13397 11:36:05,000 --> 11:36:11,360 generating this HTML using 13398 11:36:08,520 --> 11:36:14,360 JavaScript now let's console.log this 13399 11:36:11,360 --> 11:36:16,800 HTML to make sure that it's working 13400 11:36:14,360 --> 11:36:21,000 we'll scroll down to the 13401 11:36:16,800 --> 11:36:24,400 bottom and here let's 13402 11:36:21,000 --> 11:36:26,756 console.log the HTML that we just 13403 11:36:24,400 --> 11:36:30,240 created if we 13404 11:36:26,756 --> 11:36:32,800 save and then go back to our page right 13405 11:36:30,240 --> 11:36:35,240 click and 13406 11:36:32,800 --> 11:36:38,160 inspect and click the 13407 11:36:35,240 --> 11:36:41,360 console you can see in the console that 13408 11:36:38,160 --> 11:36:44,200 we just generated all this HTML and we 13409 11:36:41,360 --> 11:36:46,080 generated three of these 13410 11:36:44,200 --> 11:36:49,840 let's go back to our 13411 11:36:46,080 --> 11:36:49,840 code and scroll 13412 11:36:50,596 --> 11:36:56,640 up now one problem here is that in the 13413 11:36:54,000 --> 11:36:59,240 HTML that we generate we don't want to 13414 11:36:56,640 --> 11:37:01,880 have the name black and gray socks every 13415 11:36:59,240 --> 11:37:03,916 time for the second product we want to 13416 11:37:01,880 --> 11:37:06,160 have a different name and a different 13417 11:37:03,916 --> 11:37:09,360 image and for the third product we want 13418 11:37:06,160 --> 11:37:12,400 to have a different name and a different 13419 11:37:09,360 --> 11:37:15,160 image so how do we create slightly 13420 11:37:12,400 --> 11:37:17,520 different HTML for each 13421 11:37:15,160 --> 11:37:20,160 product to do that we're going to 13422 11:37:17,520 --> 11:37:24,080 replace this part of the code with 13423 11:37:20,160 --> 11:37:26,956 whatever the image property is so for 13424 11:37:24,080 --> 11:37:30,320 here we're going to delete it and we're 13425 11:37:26,956 --> 11:37:34,040 going to insert the property image into 13426 11:37:30,320 --> 11:37:37,596 this string so to insert values into a 13427 11:37:34,040 --> 11:37:40,956 template string like this we can use 13428 11:37:37,596 --> 11:37:43,800 Dollar open curly bracket Clos curly 13429 11:37:40,956 --> 11:37:47,080 bracket and then inside here we're going 13430 11:37:43,800 --> 11:37:50,400 to insert the 13431 11:37:47,080 --> 11:37:52,720 product. image property so it's going to 13432 11:37:50,400 --> 11:37:56,596 take whatever the image property is of 13433 11:37:52,720 --> 11:37:59,756 the product and put it inside this HTML 13434 11:37:56,596 --> 11:38:02,436 code same thing for the name instead of 13435 11:37:59,756 --> 11:38:05,720 black and gray socks every time we're 13436 11:38:02,436 --> 11:38:09,160 going to delete this and then insert a 13437 11:38:05,720 --> 11:38:12,956 value we're going to insert the 13438 11:38:09,160 --> 11:38:18,000 product Dot and we'll insert the name 13439 11:38:12,956 --> 11:38:20,640 property this time into this HTML so dot 13440 11:38:18,000 --> 11:38:23,916 name and we'll do the same thing for the 13441 11:38:20,640 --> 11:38:26,640 other pieces of data so for each product 13442 11:38:23,916 --> 11:38:30,080 the number of stars could be different 13443 11:38:26,640 --> 11:38:33,720 so instead of 4.5 every time let's 13444 11:38:30,080 --> 11:38:39,956 delete this and we're going to insert 13445 11:38:33,720 --> 11:38:41,800 the products rating and stars so 13446 11:38:39,956 --> 11:38:45,360 product. 13447 11:38:41,800 --> 11:38:45,360 rating. Stars 13448 11:38:46,680 --> 11:38:52,756 one thing we have to watch out here for 13449 11:38:49,160 --> 11:38:55,596 is that this image has to exist so right 13450 11:38:52,756 --> 11:38:58,800 now if we substitute the product. 13451 11:38:55,596 --> 11:39:03,080 ratings. Stars into this we will get 13452 11:38:58,800 --> 11:39:06,916 this image and if we go into our files 13453 11:39:03,080 --> 11:39:10,200 by clicking here and open the images 13454 11:39:06,916 --> 11:39:12,720 folder and then open the ratings 13455 11:39:10,200 --> 11:39:14,680 folder we can see that there's no image 13456 11:39:12,720 --> 11:39:17,400 called rating 13457 11:39:14,680 --> 11:39:18,640 -4.5 instead there's an image called 13458 11:39:17,400 --> 11:39:22,756 rating 13459 11:39:18,640 --> 11:39:26,400 -45 so if the rating is 4.5 we should 13460 11:39:22,756 --> 11:39:27,520 convert it into 45 so that this image 13461 11:39:26,400 --> 11:39:31,436 actually 13462 11:39:27,520 --> 11:39:34,360 exists similarly if the rating is four 13463 11:39:31,436 --> 11:39:38,596 we should convert it into 40 so that 13464 11:39:34,360 --> 11:39:43,596 this image actually exists so in here we 13465 11:39:38,596 --> 11:39:47,000 should actually multiply this by 10 so 13466 11:39:43,596 --> 11:39:48,956 that this points to an actual image that 13467 11:39:47,000 --> 11:39:52,840 exists 13468 11:39:48,956 --> 11:39:52,840 here next let's move 13469 11:39:53,240 --> 11:40:01,320 on and we'll work on replacing the count 13470 11:39:57,400 --> 11:40:05,916 so we'll remove this and dollar curly 13471 11:40:01,320 --> 11:40:11,756 brackets and we'll replace it with the 13472 11:40:05,916 --> 11:40:14,400 products. rating Dot and the count 13473 11:40:11,756 --> 11:40:17,276 property so 13474 11:40:14,400 --> 11:40:20,520 count and finally we're going to replace 13475 11:40:17,276 --> 11:40:23,720 the price for each product so in here 13476 11:40:20,520 --> 11:40:25,720 we're going to remove this and 13477 11:40:23,720 --> 11:40:28,596 substitute 13478 11:40:25,720 --> 11:40:31,680 product. price 13479 11:40:28,596 --> 11:40:34,400 sense and remember that this is a number 13480 11:40:31,680 --> 11:40:37,840 in cents but when we display it on the 13481 11:40:34,400 --> 11:40:41,640 page we want to display it as dollars so 13482 11:40:37,840 --> 11:40:45,000 we're going to divide this by 100 13483 11:40:41,640 --> 11:40:48,200 because there are 100 cents in each 13484 11:40:45,000 --> 11:40:51,040 dollar so now in the HTML that we're 13485 11:40:48,200 --> 11:40:55,116 generating we're actually inserting the 13486 11:40:51,040 --> 11:40:58,276 details about each product if we 13487 11:40:55,116 --> 11:40:59,596 save and then go back to the console 13488 11:40:58,276 --> 11:41:01,916 right click 13489 11:40:59,596 --> 11:41:05,000 inspect and 13490 11:41:01,916 --> 11:41:07,560 console and look at the HTML that we 13491 11:41:05,000 --> 11:41:10,680 generated you'll see that it's different 13492 11:41:07,560 --> 11:41:13,560 for each of the products so that's how 13493 11:41:10,680 --> 11:41:15,680 we use JavaScript to generate all the 13494 11:41:13,560 --> 11:41:18,680 HTML 13495 11:41:15,680 --> 11:41:21,956 here so now the last step is we're going 13496 11:41:18,680 --> 11:41:25,756 to combine all this HTML together into 13497 11:41:21,956 --> 11:41:28,080 one string and put it on the web page 13498 11:41:25,756 --> 11:41:31,596 let's go back into VSS 13499 11:41:28,080 --> 11:41:34,400 code and to combine all this HTML 13500 11:41:31,596 --> 11:41:37,040 together let's create a variable at the 13501 11:41:34,400 --> 11:41:41,916 top for combining all the strings 13502 11:41:37,040 --> 11:41:44,400 together so we'll type let let's call it 13503 11:41:41,916 --> 11:41:47,596 products cap Capital 13504 11:41:44,400 --> 11:41:51,200 HTML and make it equal to an empty 13505 11:41:47,596 --> 11:41:53,640 string to start and then each time we go 13506 11:41:51,200 --> 11:41:57,480 through the loop we're going to add this 13507 11:41:53,640 --> 11:42:00,160 HTML string up here so instead of 13508 11:41:57,480 --> 11:42:03,840 creating a new variable we're just going 13509 11:42:00,160 --> 11:42:08,840 to add products 13510 11:42:03,840 --> 11:42:13,200 HTML equals itself products 13511 11:42:08,840 --> 11:42:18,040 HTML plus this string and remember a 13512 11:42:13,200 --> 11:42:21,520 shortcut for this is to just type plus 13513 11:42:18,040 --> 11:42:24,756 equals so this is called the accumulator 13514 11:42:21,520 --> 11:42:27,680 pattern we Loop through an array and 13515 11:42:24,756 --> 11:42:31,080 each time we're adding to the result so 13516 11:42:27,680 --> 11:42:34,116 we're accumulating the result and now 13517 11:42:31,080 --> 11:42:37,276 that we combine all the HTML into this 13518 11:42:34,116 --> 11:42:40,000 variable we can scroll to the 13519 11:42:37,276 --> 11:42:43,360 bottom and let's remove this line 13520 11:42:40,000 --> 11:42:47,596 because this doesn't exist anymore 13521 11:42:43,360 --> 11:42:52,160 and then after the loop let's 13522 11:42:47,596 --> 11:42:54,680 console.log the products 13523 11:42:52,160 --> 11:42:58,560 HTML and 13524 11:42:54,680 --> 11:43:02,436 save and now if we go into our 13525 11:42:58,560 --> 11:43:04,640 console we combined all the HTML for all 13526 11:43:02,436 --> 11:43:07,320 the products 13527 11:43:04,640 --> 11:43:10,800 together and now the last step is to 13528 11:43:07,320 --> 11:43:13,756 take this HTML and put it on the page 13529 11:43:10,800 --> 11:43:16,200 and we can do that using the Dom 13530 11:43:13,756 --> 11:43:19,360 so let's go back into our 13531 11:43:16,200 --> 11:43:22,116 code and to use the Dom we're first 13532 11:43:19,360 --> 11:43:24,916 going to get an HTML element from the 13533 11:43:22,116 --> 11:43:29,240 page and put it inside our 13534 11:43:24,916 --> 11:43:32,520 JavaScript so first let's go to our HTML 13535 11:43:29,240 --> 11:43:34,800 file and remember that a product- 13536 11:43:32,520 --> 11:43:37,880 container represents one of these 13537 11:43:34,800 --> 11:43:41,956 products here so we're going to look for 13538 11:43:37,880 --> 11:43:45,160 this element here a div products- grid 13539 11:43:41,956 --> 11:43:47,436 so this element contains all of our 13540 11:43:45,160 --> 11:43:50,276 products so we're going to use the Dom 13541 11:43:47,436 --> 11:43:54,160 to take this element put it into our 13542 11:43:50,276 --> 11:43:56,756 JavaScript and replace all the HTML 13543 11:43:54,160 --> 11:43:59,200 inside so to put this element in our 13544 11:43:56,756 --> 11:44:03,200 JavaScript we're going to give it a 13545 11:43:59,200 --> 11:44:05,276 class so let's add a class 13546 11:44:03,200 --> 11:44:08,840 js- 13547 11:44:05,276 --> 11:44:12,276 products- grid and 13548 11:44:08,840 --> 11:44:13,596 save and let's go into our JavaScript so 13549 11:44:12,276 --> 11:44:15,916 I'm just going to move this this down to 13550 11:44:13,596 --> 11:44:17,956 the bottom again so we can see it side 13551 11:44:15,916 --> 11:44:21,520 by 13552 11:44:17,956 --> 11:44:24,596 side and then inside our JavaScript at 13553 11:44:21,520 --> 11:44:26,276 the bottom we're going to use the Dom by 13554 11:44:24,596 --> 11:44:31,000 typing 13555 11:44:26,276 --> 11:44:33,840 document. query selector brackets and a 13556 11:44:31,000 --> 11:44:37,360 string and we're going to type dot to 13557 11:44:33,840 --> 11:44:39,040 select a class and we're going to select 13558 11:44:37,360 --> 11:44:41,360 js- 13559 11:44:39,040 --> 11:44:44,000 products- 13560 11:44:41,360 --> 11:44:47,560 grid so the DOT means we're going to 13561 11:44:44,000 --> 11:44:49,560 look for a class called JS products grid 13562 11:44:47,560 --> 11:44:53,480 which is right here and it's going to 13563 11:44:49,560 --> 11:44:56,400 take this element and put it inside our 13564 11:44:53,480 --> 11:45:00,436 JavaScript and now we can change the 13565 11:44:56,400 --> 11:45:05,640 HTML inside the element to do that we'll 13566 11:45:00,436 --> 11:45:09,276 use the property do inner Capital 13567 11:45:05,640 --> 11:45:13,320 HTML and we'll change it to the products 13568 11:45:09,276 --> 11:45:16,436 HTML that we created Above So for 13569 11:45:13,320 --> 11:45:18,956 products Capital 13570 11:45:16,436 --> 11:45:21,436 HTML and 13571 11:45:18,956 --> 11:45:23,436 save so it's not going to look any 13572 11:45:21,436 --> 11:45:26,916 different but now we're actually 13573 11:45:23,436 --> 11:45:30,240 generating all the HTML with JavaScript 13574 11:45:26,916 --> 11:45:33,480 and then using the Dom to put this HTML 13575 11:45:30,240 --> 11:45:35,800 inside here so now we can actually 13576 11:45:33,480 --> 11:45:39,080 delete all of this because we're 13577 11:45:35,800 --> 11:45:39,080 generating this with 13578 11:45:40,320 --> 11:45:46,200 JavaScript if we save this file 13579 11:45:43,720 --> 11:45:48,880 our products will still be here because 13580 11:45:46,200 --> 11:45:51,116 even though we deleted the HTML we're 13581 11:45:48,880 --> 11:45:53,640 now generating it with 13582 11:45:51,116 --> 11:45:56,756 JavaScript now one problem that I see 13583 11:45:53,640 --> 11:45:59,040 here is that the price shows up with one 13584 11:45:56,756 --> 11:46:01,040 decimal but we actually wanted to show 13585 11:45:59,040 --> 11:46:04,116 up as 13586 11:46:01,040 --> 11:46:07,436 10.90 so let's scroll up to this part in 13587 11:46:04,116 --> 11:46:10,080 the code so we'll scroll up 13588 11:46:07,436 --> 11:46:12,560 here and look for Price 13589 11:46:10,080 --> 11:46:14,680 sense and the reason this happens is 13590 11:46:12,560 --> 11:46:18,040 because price sense is 13591 11:46:14,680 --> 11:46:20,436 1,90 so if we divide that by 100 the 13592 11:46:18,040 --> 11:46:23,200 result is actually 13593 11:46:20,436 --> 11:46:25,720 10.9 if we want to force this to show 13594 11:46:23,200 --> 11:46:29,000 two decimals like 13595 11:46:25,720 --> 11:46:33,240 10.90 we actually have a special method 13596 11:46:29,000 --> 11:46:35,520 that we can use with a number so here we 13597 11:46:33,240 --> 11:46:38,400 can add brackets around this to make 13598 11:46:35,520 --> 11:46:41,880 sure we calculate this number first and 13599 11:46:38,400 --> 11:46:47,520 numbers have a special method called dot 13600 11:46:41,880 --> 11:46:50,756 2 Capital fixed and brackets so to fixed 13601 11:46:47,520 --> 11:46:53,360 we'll convert a number into a string but 13602 11:46:50,756 --> 11:46:56,800 we can tell it how many decimals we want 13603 11:46:53,360 --> 11:47:00,680 between the brackets if we type two it 13604 11:46:56,800 --> 11:47:03,720 will show two decimals so if we save now 13605 11:47:00,680 --> 11:47:06,040 it shows correctly on the page if you're 13606 11:47:03,720 --> 11:47:08,840 wondering how to find this method you 13607 11:47:06,040 --> 11:47:11,040 can find it by searching in Google or 13608 11:47:08,840 --> 11:47:14,000 using an AI 13609 11:47:11,040 --> 11:47:16,400 tool okay let's let's take a break here 13610 11:47:14,000 --> 11:47:19,040 and take a look at git to see all the 13611 11:47:16,400 --> 11:47:21,480 changes that we made so I'm going to 13612 11:47:19,040 --> 11:47:23,520 expand this here and we're going to 13613 11:47:21,480 --> 11:47:25,596 click the git 13614 11:47:23,520 --> 11:47:28,400 section and it's going to tell us that 13615 11:47:25,596 --> 11:47:30,840 we changed two files if we click the 13616 11:47:28,400 --> 11:47:33,840 HTML 13617 11:47:30,840 --> 11:47:36,880 file git will tell us that we deleted 13618 11:47:33,840 --> 11:47:39,596 all of the HTML code for the products 13619 11:47:36,880 --> 11:47:42,880 which is correct if we open the 13620 11:47:39,596 --> 11:47:45,956 Javascript file it will tell us that we 13621 11:47:42,880 --> 11:47:48,480 added all of this new JavaScript code 13622 11:47:45,956 --> 11:47:50,276 including the data and the loop for 13623 11:47:48,480 --> 11:47:53,560 generating the 13624 11:47:50,276 --> 11:47:56,400 HTML so git helps us track all of the 13625 11:47:53,560 --> 11:47:58,436 changes that we made so now let's 13626 11:47:56,400 --> 11:48:01,680 actually take these changes and save 13627 11:47:58,436 --> 11:48:05,000 them into git so we can track new 13628 11:48:01,680 --> 11:48:07,880 changes at the top let's type a message 13629 11:48:05,000 --> 11:48:09,880 to describe the changes that we made so 13630 11:48:07,880 --> 11:48:14,240 let's type 13631 11:48:09,880 --> 11:48:16,480 generate HTML with 13632 11:48:14,240 --> 11:48:21,080 JavaScript and click 13633 11:48:16,480 --> 11:48:21,080 commit and yes if this shows 13634 11:48:25,160 --> 11:48:30,040 up so now let's talk about the benefit 13635 11:48:28,360 --> 11:48:34,320 of generating the 13636 11:48:30,040 --> 11:48:36,080 HTML so let's close git for now and I'll 13637 11:48:34,320 --> 11:48:39,040 move this to the side 13638 11:48:36,080 --> 11:48:41,916 here so the benefit of generating the 13639 11:48:39,040 --> 11:48:44,880 HTML is that if we want to add another 13640 11:48:41,916 --> 11:48:48,720 product we don't have to copy paste all 13641 11:48:44,880 --> 11:48:52,240 of this HTML again all we need to do is 13642 11:48:48,720 --> 11:48:55,916 to go up to the data and add the data 13643 11:48:52,240 --> 11:48:59,640 for a new product so let's do an example 13644 11:48:55,916 --> 11:49:02,956 and add a fourth product to our list so 13645 11:48:59,640 --> 11:49:05,596 at the end here let's type a comma and 13646 11:49:02,956 --> 11:49:09,116 create another 13647 11:49:05,596 --> 11:49:12,320 object and let's go to the final 13648 11:49:09,116 --> 11:49:14,560 project and we'll scroll down to this 13649 11:49:12,320 --> 11:49:17,720 four fourth product here so we're going 13650 11:49:14,560 --> 11:49:21,360 to take the data about this product and 13651 11:49:17,720 --> 11:49:26,400 save it in our JavaScript so first let's 13652 11:49:21,360 --> 11:49:28,276 create a property image colon string and 13653 11:49:26,400 --> 11:49:31,720 we're going to take this image and put 13654 11:49:28,276 --> 11:49:34,680 it in here to do that we can rightclick 13655 11:49:31,720 --> 11:49:37,000 the image 13656 11:49:34,680 --> 11:49:39,596 inspect and then we're going to copy 13657 11:49:37,000 --> 11:49:43,000 this Source attribute so we can double 13658 11:49:39,596 --> 11:49:44,596 click it and we can select this 13659 11:49:43,000 --> 11:49:46,436 attribute 13660 11:49:44,596 --> 11:49:49,840 rightclick 13661 11:49:46,436 --> 11:49:53,800 copy and then inside our JavaScript 13662 11:49:49,840 --> 11:49:57,400 we're going to right click and 13663 11:49:53,800 --> 11:49:59,880 paste and at the end we'll type a comma 13664 11:49:57,400 --> 11:50:03,000 and create the name 13665 11:49:59,880 --> 11:50:04,360 property and a string and we're going to 13666 11:50:03,000 --> 11:50:09,480 select this 13667 11:50:04,360 --> 11:50:11,436 name rightclick copy and then in here 13668 11:50:09,480 --> 11:50:15,756 right click 13669 11:50:11,436 --> 11:50:20,000 paste copy comma and a rating property 13670 11:50:15,756 --> 11:50:23,116 colon and object we have these Stars 13671 11:50:20,000 --> 11:50:26,916 which is five and the 13672 11:50:23,116 --> 11:50:26,916 count which is 13673 11:50:27,956 --> 11:50:38,200 2,197 and at the end we're going to add 13674 11:50:31,160 --> 11:50:38,200 the price in cents colon 13675 11:50:39,360 --> 11:50:46,116 1,899 so now if we save this JavaScript 13676 11:50:42,916 --> 11:50:49,560 file and then go back to our 13677 11:50:46,116 --> 11:50:52,320 project and we scroll down you'll notice 13678 11:50:49,560 --> 11:50:55,436 that it automatically added the fourth 13679 11:50:52,320 --> 11:50:58,596 product and all we had to do was to add 13680 11:50:55,436 --> 11:51:01,756 another object to our array and then our 13681 11:50:58,596 --> 11:51:04,720 Loop will handle generating all the HTML 13682 11:51:01,756 --> 11:51:08,240 for us so you can see that generating 13683 11:51:04,720 --> 11:51:11,756 the HTML with JavaScript is a lot easier 13684 11:51:08,240 --> 11:51:14,200 than copy pasting this HTML every time 13685 11:51:11,756 --> 11:51:17,000 and that's why we use this 13686 11:51:14,200 --> 11:51:19,560 technique next we're going to add the 13687 11:51:17,000 --> 11:51:22,400 rest of the products in the final 13688 11:51:19,560 --> 11:51:25,400 project now one thing we could do is to 13689 11:51:22,400 --> 11:51:27,916 manually copy this over one by one but 13690 11:51:25,400 --> 11:51:30,640 to make this easier inside the starting 13691 11:51:27,916 --> 11:51:35,080 code I've actually created a file that 13692 11:51:30,640 --> 11:51:37,116 contains all the data so if we scroll up 13693 11:51:35,080 --> 11:51:40,840 and close this for now and we're going 13694 11:51:37,116 --> 11:51:43,320 to go to this data folder and open it 13695 11:51:40,840 --> 11:51:46,956 and inside this folder I've provided a 13696 11:51:43,320 --> 11:51:49,756 Javascript file called products. JS and 13697 11:51:46,956 --> 11:51:52,436 in here it contains an array with the 13698 11:51:49,756 --> 11:51:55,880 data for all the products in the final 13699 11:51:52,436 --> 11:51:58,800 project so we don't have to create it 13700 11:51:55,880 --> 11:52:02,080 ourselves so now let's use this products 13701 11:51:58,800 --> 11:52:05,720 array in our code so first we need to 13702 11:52:02,080 --> 11:52:09,480 load this file on our page we can go to 13703 11:52:05,720 --> 11:52:11,000 our amazon. HTML file and then scroll 13704 11:52:09,480 --> 11:52:14,320 down to the 13705 11:52:11,000 --> 11:52:16,840 bottom and then at the bottom in the 13706 11:52:14,320 --> 11:52:19,916 script element we're going to add 13707 11:52:16,840 --> 11:52:23,160 another script element above it so we 13708 11:52:19,916 --> 11:52:26,840 type script and then the source is going 13709 11:52:23,160 --> 11:52:30,640 to be equal to this Javascript file so 13710 11:52:26,840 --> 11:52:33,596 we'll type the folder called data and 13711 11:52:30,640 --> 11:52:36,200 then a slash to go into the folder and 13712 11:52:33,596 --> 11:52:37,800 load the file 13713 11:52:36,200 --> 11:52:40,840 products. 13714 11:52:37,800 --> 11:52:43,436 JS so what this does is that it will run 13715 11:52:40,840 --> 11:52:46,276 the files one by one 13716 11:52:43,436 --> 11:52:48,840 first it will run the products. JS file 13717 11:52:46,276 --> 11:52:53,880 and create the array and then it will 13718 11:52:48,840 --> 11:52:57,640 run our amazon. JS file and generate the 13719 11:52:53,880 --> 11:53:02,116 HTML so back here let's 13720 11:52:57,640 --> 11:53:05,560 save and then go back into amazon. 13721 11:53:02,116 --> 11:53:08,080 JS so now instead of using our own 13722 11:53:05,560 --> 11:53:10,520 products array let's just use the 13723 11:53:08,080 --> 11:53:14,040 products array in this 13724 11:53:10,520 --> 11:53:16,956 file so we'll go back to to amazon. 13725 11:53:14,040 --> 11:53:20,000 JS and we're just going to delete this 13726 11:53:16,956 --> 11:53:23,240 array so over here we can click this 13727 11:53:20,000 --> 11:53:27,040 Arrow to collapse the code and I'm going 13728 11:53:23,240 --> 11:53:29,400 to select it and then just delete 13729 11:53:27,040 --> 11:53:33,200 it and 13730 11:53:29,400 --> 11:53:37,200 save so now this variable will be coming 13731 11:53:33,200 --> 11:53:40,480 from this file if we go back to our 13732 11:53:37,200 --> 11:53:44,360 project and scroll down you can see that 13733 11:53:40,480 --> 11:53:47,400 we created all the products in our final 13734 11:53:44,360 --> 11:53:49,640 project so the way this works is that 13735 11:53:47,400 --> 11:53:53,640 inside our amazon. 13736 11:53:49,640 --> 11:53:55,756 HTML we're loading two JavaScript files 13737 11:53:53,640 --> 11:53:58,560 this Javascript file creates all the 13738 11:53:55,756 --> 11:54:00,916 products and then this Javascript file 13739 11:53:58,560 --> 11:54:02,480 Loops through the products and creates 13740 11:54:00,916 --> 11:54:05,000 all the 13741 11:54:02,480 --> 11:54:07,800 HTML so keep in mind that script 13742 11:54:05,000 --> 11:54:10,880 elements are loaded one by one so the 13743 11:54:07,800 --> 11:54:13,200 order matters we have to first create 13744 11:54:10,880 --> 11:54:15,956 the products array and then we can use 13745 11:54:13,200 --> 11:54:18,756 it if these two were reversed then the 13746 11:54:15,956 --> 11:54:22,116 code wouldn't 13747 11:54:18,756 --> 11:54:26,080 work so this is how a real world website 13748 11:54:22,116 --> 11:54:29,080 is created we don't write all the HTML 13749 11:54:26,080 --> 11:54:31,756 manually instead we save the data for 13750 11:54:29,080 --> 11:54:35,116 the website in a data structure like 13751 11:54:31,756 --> 11:54:39,480 this and then we use JavaScript to 13752 11:54:35,116 --> 11:54:41,916 generate the HTML using the data so now 13753 11:54:39,480 --> 11:54:43,080 let's go into get and look at our most 13754 11:54:41,916 --> 11:54:45,560 recent 13755 11:54:43,080 --> 11:54:49,320 changes so here it will tell us that we 13756 11:54:45,560 --> 11:54:52,560 changed two files in the amazon. HTML 13757 11:54:49,320 --> 11:54:56,360 file we added a script element and then 13758 11:54:52,560 --> 11:54:59,680 in the amazon. JS file we deleted the 13759 11:54:56,360 --> 11:55:02,560 products array that we created on our 13760 11:54:59,680 --> 11:55:09,000 own so let's type a message to describe 13761 11:55:02,560 --> 11:55:14,240 our changes use products. JS file and 13762 11:55:09,000 --> 11:55:14,240 then click commit and yes 13763 11:55:14,956 --> 11:55:19,360 now one other thing I want to show you 13764 11:55:16,640 --> 11:55:23,840 about git is if we go back to our files 13765 11:55:19,360 --> 11:55:26,596 here and then open amazon. JS git has a 13766 11:55:23,840 --> 11:55:29,880 really useful feature called a history 13767 11:55:26,596 --> 11:55:33,520 so we can open this timeline section and 13768 11:55:29,880 --> 11:55:35,480 then click this filter icon and only 13769 11:55:33,520 --> 11:55:37,840 check git 13770 11:55:35,480 --> 11:55:40,240 history and now you can see that we 13771 11:55:37,840 --> 11:55:43,800 actually have a history of all of our 13772 11:55:40,240 --> 11:55:46,400 changes for this file so if we click the 13773 11:55:43,800 --> 11:55:49,916 first line in the history it will show 13774 11:55:46,400 --> 11:55:52,400 us that here we added all of the code 13775 11:55:49,916 --> 11:55:55,880 and then the second line in the history 13776 11:55:52,400 --> 11:55:58,840 we deleted this part of the code so this 13777 11:55:55,880 --> 11:56:02,480 gives us a step-by-step history of all 13778 11:55:58,840 --> 11:56:05,880 the changes in our code and in git this 13779 11:56:02,480 --> 11:56:08,080 is called a version history you can also 13780 11:56:05,880 --> 11:56:11,320 see that the messages that we created 13781 11:56:08,080 --> 11:56:13,680 when we commit show up here and these 13782 11:56:11,320 --> 11:56:16,400 messages help us us understand what was 13783 11:56:13,680 --> 11:56:19,000 changed in each version in the 13784 11:56:16,400 --> 11:56:21,276 history all right so let's close this 13785 11:56:19,000 --> 11:56:21,276 for 13786 11:56:21,680 --> 11:56:26,956 now and the last step in the process is 13787 11:56:24,560 --> 11:56:29,840 to make it 13788 11:56:26,956 --> 11:56:30,916 interactive so let's take a look at our 13789 11:56:29,840 --> 11:56:35,436 current 13790 11:56:30,916 --> 11:56:38,160 project so we'll go here and open this 13791 11:56:35,436 --> 11:56:41,756 tab and let's scroll 13792 11:56:38,160 --> 11:56:45,116 up so right now if we click the add to 13793 11:56:41,756 --> 11:56:46,956 cart button Buton it doesn't do anything 13794 11:56:45,116 --> 11:56:49,596 so we're going to add some JavaScript to 13795 11:56:46,956 --> 11:56:53,520 make this interactive and actually add 13796 11:56:49,596 --> 11:56:56,916 this product to a cart so first let's 13797 11:56:53,520 --> 11:57:01,200 find the add to cart button in our code 13798 11:56:56,916 --> 11:57:02,680 we'll go back to our amazon. JS file 13799 11:57:01,200 --> 11:57:05,720 close this for 13800 11:57:02,680 --> 11:57:08,800 now and to find the code we can actually 13801 11:57:05,720 --> 11:57:12,240 just search the text in the button so 13802 11:57:08,800 --> 11:57:14,680 inside here let's typ contrl F or 13803 11:57:12,240 --> 11:57:18,480 command F and we're going to search for 13804 11:57:14,680 --> 11:57:21,400 the text add to 13805 11:57:18,480 --> 11:57:24,080 cart and this will take us to the code 13806 11:57:21,400 --> 11:57:26,880 for this button so now we're going to 13807 11:57:24,080 --> 11:57:29,956 add an event listener to this button and 13808 11:57:26,880 --> 11:57:32,116 make it do something when we click it to 13809 11:57:29,956 --> 11:57:33,480 add an event listener we're first going 13810 11:57:32,116 --> 11:57:35,436 to add a 13811 11:57:33,480 --> 11:57:37,720 class 13812 11:57:35,436 --> 11:57:39,720 js- 13813 11:57:37,720 --> 11:57:41,840 add-2 13814 11:57:39,720 --> 11:57:45,720 dcart and then we're going to use the 13815 11:57:41,840 --> 11:57:48,200 dawn to add an event listener to this 13816 11:57:45,720 --> 11:57:51,596 button so down 13817 11:57:48,200 --> 11:57:55,240 here after we've put this HTML on the 13818 11:57:51,596 --> 11:57:58,240 page we're going to use 13819 11:57:55,240 --> 11:58:03,276 document. Query 13820 11:57:58,240 --> 11:58:06,400 selector all brackets and then a string 13821 11:58:03,276 --> 11:58:10,000 a DOT and we're going to select all the 13822 11:58:06,400 --> 11:58:15,520 js- add to cart so 13823 11:58:10,000 --> 11:58:18,480 js- add-2 to- cart so this will give us 13824 11:58:15,520 --> 11:58:21,436 a list of all the add to cart buttons on 13825 11:58:18,480 --> 11:58:25,320 the page and then we can Loop through 13826 11:58:21,436 --> 11:58:28,080 each of the buttons so we can type dot 13827 11:58:25,320 --> 11:58:30,560 for each 13828 11:58:28,080 --> 11:58:34,880 brackets and then in here we'll give it 13829 11:58:30,560 --> 11:58:38,240 a function bracket arrow and curly 13830 11:58:34,880 --> 11:58:40,596 brackets and a parameter so the 13831 11:58:38,240 --> 11:58:44,400 parameter is going to be the button 13832 11:58:40,596 --> 11:58:46,520 element that we got from the page so now 13833 11:58:44,400 --> 11:58:48,916 for each of these buttons we're going to 13834 11:58:46,520 --> 11:58:51,800 add an event listener so we can do 13835 11:58:48,916 --> 11:58:54,276 something when we click the button to do 13836 11:58:51,800 --> 11:58:58,040 that we'll type 13837 11:58:54,276 --> 11:59:01,596 button. add event 13838 11:58:58,040 --> 11:59:04,560 listener brackets and then the event 13839 11:59:01,596 --> 11:59:05,720 that we want to listen for which is the 13840 11:59:04,560 --> 11:59:08,200 click 13841 11:59:05,720 --> 11:59:11,276 event and then a function that we want 13842 11:59:08,200 --> 11:59:14,276 to run when we click this button so 13843 11:59:11,276 --> 11:59:16,596 brackets arrow and curly 13844 11:59:14,276 --> 11:59:19,200 brackets so for now let's just 13845 11:59:16,596 --> 11:59:23,520 console.log a message to make sure that 13846 11:59:19,200 --> 11:59:23,520 this is working so we'll 13847 11:59:23,916 --> 11:59:29,520 console.log added 13848 11:59:26,756 --> 11:59:32,276 product and 13849 11:59:29,520 --> 11:59:35,116 save and we can also remove this line 13850 11:59:32,276 --> 11:59:38,200 for now because we're not using it and 13851 11:59:35,116 --> 11:59:40,276 save again and then now on our page 13852 11:59:38,200 --> 11:59:44,000 we're going to click add it to 13853 11:59:40,276 --> 11:59:45,956 cart and then look inside our console so 13854 11:59:44,000 --> 11:59:48,276 rightclick 13855 11:59:45,956 --> 11:59:52,040 inspect and click the 13856 11:59:48,276 --> 11:59:53,720 console and it displays added product so 13857 11:59:52,040 --> 11:59:57,436 now whenever we click one of these 13858 11:59:53,720 --> 12:00:01,000 buttons it will display this message so 13859 11:59:57,436 --> 12:00:04,000 that means that our code here is working 13860 12:00:01,000 --> 12:00:07,360 correctly next we need to figure out how 13861 12:00:04,000 --> 12:00:10,436 to add the product to a cart and we need 13862 12:00:07,360 --> 12:00:13,360 to figure out what this looks like if we 13863 12:00:10,436 --> 12:00:16,800 think about a cart a cart is basically 13864 12:00:13,360 --> 12:00:19,320 just a list and inside this list we have 13865 12:00:16,800 --> 12:00:21,800 the product that we want to buy and the 13866 12:00:19,320 --> 12:00:24,080 quantity or the number of this product 13867 12:00:21,800 --> 12:00:28,200 that we want to buy so we can actually 13868 12:00:24,080 --> 12:00:30,080 represent this in JavaScript as an array 13869 12:00:28,200 --> 12:00:32,520 inside the array we can have some 13870 12:00:30,080 --> 12:00:35,116 objects and each object is going to 13871 12:00:32,520 --> 12:00:37,640 contain the product and a 13872 12:00:35,116 --> 12:00:41,116 quantity so we can use something like 13873 12:00:37,640 --> 12:00:44,400 this to represent our cart so let's go 13874 12:00:41,116 --> 12:00:46,596 ahead and create create this cart array 13875 12:00:44,400 --> 12:00:49,560 now to keep things cleaner let's 13876 12:00:46,596 --> 12:00:52,880 actually separate the cart into its own 13877 12:00:49,560 --> 12:00:55,480 file so we'll click up here to show our 13878 12:00:52,880 --> 12:00:58,596 files and then we'll go into the data 13879 12:00:55,480 --> 12:01:01,116 folder and click this folder and click 13880 12:00:58,596 --> 12:01:06,520 this icon to create a new 13881 12:01:01,116 --> 12:01:08,116 file and we'll name this file cart. JS 13882 12:01:06,520 --> 12:01:11,000 and press 13883 12:01:08,116 --> 12:01:14,200 enter so this file will contain all the 13884 12:01:11,000 --> 12:01:16,480 code related to the cart and the reason 13885 12:01:14,200 --> 12:01:19,360 we separate it is to keep our code 13886 12:01:16,480 --> 12:01:22,916 organized each file will focus on one 13887 12:01:19,360 --> 12:01:27,116 particular thing so inside 13888 12:01:22,916 --> 12:01:31,916 C.J let's create our cart so we can just 13889 12:01:27,116 --> 12:01:35,320 create a variable const cart and make it 13890 12:01:31,916 --> 12:01:39,080 equal to an array so we'll start off 13891 12:01:35,320 --> 12:01:42,200 with an MD cart and let's save this 13892 12:01:39,080 --> 12:01:43,480 file and now we need to load this file 13893 12:01:42,200 --> 12:01:47,436 into our 13894 12:01:43,480 --> 12:01:50,756 HTML so we can open amazon. 13895 12:01:47,436 --> 12:01:53,880 HTML and again we can just add another 13896 12:01:50,756 --> 12:01:56,880 script element so let's add it at the 13897 12:01:53,880 --> 12:01:59,560 top to make sure we load the cart before 13898 12:01:56,880 --> 12:02:01,880 running the rest of our code so we'll 13899 12:01:59,560 --> 12:02:04,720 type less than 13900 12:02:01,880 --> 12:02:07,880 script and the attribute 13901 12:02:04,720 --> 12:02:12,596 source and we'll load the data folder 13902 12:02:07,880 --> 12:02:13,800 slash to go inside cart. Js 13903 12:02:12,596 --> 12:02:17,400 and 13904 12:02:13,800 --> 12:02:20,116 save so now it will run this file at the 13905 12:02:17,400 --> 12:02:23,360 start and it will create an array 13906 12:02:20,116 --> 12:02:25,956 representing our cart so now that we 13907 12:02:23,360 --> 12:02:28,840 have a cart we need to figure out what 13908 12:02:25,956 --> 12:02:32,800 happens when we click a button how do we 13909 12:02:28,840 --> 12:02:36,116 add a product to this cart so let's go 13910 12:02:32,800 --> 12:02:40,000 back into amazon. JS and we'll figure 13911 12:02:36,116 --> 12:02:42,320 this out together so down here this is 13912 12:02:40,000 --> 12:02:45,160 the function that is run when we click 13913 12:02:42,320 --> 12:02:48,040 the button so in here we have to figure 13914 12:02:45,160 --> 12:02:51,320 out how to add this product to the 13915 12:02:48,040 --> 12:02:54,800 cart one way to do this is to just add 13916 12:02:51,320 --> 12:02:57,400 an object to the cart like this we have 13917 12:02:54,800 --> 12:03:00,596 the product's name and the quantity that 13918 12:02:57,400 --> 12:03:03,080 we want however when we click this 13919 12:03:00,596 --> 12:03:05,956 button how do we know which product 13920 12:03:03,080 --> 12:03:08,000 we're supposed to add to the cart to 13921 12:03:05,956 --> 12:03:11,320 solve this problem we're going to learn 13922 12:03:08,000 --> 12:03:12,840 a feature of HTML called a data 13923 12:03:11,320 --> 12:03:16,596 attribute 13924 12:03:12,840 --> 12:03:19,276 a data attribute is just another HTML 13925 12:03:16,596 --> 12:03:22,200 attribute except the purpose of a data 13926 12:03:19,276 --> 12:03:26,436 attribute is that it allows us to attach 13927 12:03:22,200 --> 12:03:30,520 any information to an HTML element so 13928 12:03:26,436 --> 12:03:33,560 let's do an example and create a data 13929 12:03:30,520 --> 12:03:36,916 attribute we're going to scroll up to 13930 12:03:33,560 --> 12:03:39,640 the add to card button here and then at 13931 12:03:36,916 --> 12:03:42,240 the end of the opening tag after the 13932 12:03:39,640 --> 12:03:46,320 class we'll type enter 13933 12:03:42,240 --> 12:03:48,560 and then we're going to type data Dash 13934 12:03:46,320 --> 12:03:51,480 and then we can type any name that we 13935 12:03:48,560 --> 12:03:55,596 want like 13936 12:03:51,480 --> 12:03:56,480 product-name and we'll make it equal to 13937 12:03:55,596 --> 12:03:59,436 double 13938 12:03:56,480 --> 12:04:01,916 quotes and then inside the double quotes 13939 12:03:59,436 --> 12:04:05,200 we're going to put the product's name 13940 12:04:01,916 --> 12:04:08,080 like a basketball or a t-shirt so to do 13941 12:04:05,200 --> 12:04:09,436 that we're going to insert using dollar 13942 12:04:08,080 --> 12:04:12,880 and curly 13943 12:04:09,436 --> 12:04:15,436 brackets product 13944 12:04:12,880 --> 12:04:19,320 AME and 13945 12:04:15,436 --> 12:04:22,400 save so now let's inspect the HTML on 13946 12:04:19,320 --> 12:04:25,596 the page we're going to rightclick on 13947 12:04:22,400 --> 12:04:28,400 this add to card button and 13948 12:04:25,596 --> 12:04:31,840 inspect and then in the elements tab it 13949 12:04:28,400 --> 12:04:33,436 will show us the HTML for the button and 13950 12:04:31,840 --> 12:04:36,800 now you can see that each of these 13951 12:04:33,436 --> 12:04:40,680 buttons now has a data attribute called 13952 12:04:36,800 --> 12:04:43,436 data- product-name and it's equal to the 13953 12:04:40,680 --> 12:04:45,800 product name 13954 12:04:43,436 --> 12:04:48,200 so before we continue let's learn the 13955 12:04:45,800 --> 12:04:51,520 syntax rules for the data 13956 12:04:48,200 --> 12:04:55,040 attribute a data attribute is just an 13957 12:04:51,520 --> 12:04:58,320 HTML attribute that means it follows the 13958 12:04:55,040 --> 12:05:01,240 same syntax rules we have an attribute 13959 12:04:58,320 --> 12:05:04,276 name on the left and an attribute value 13960 12:05:01,240 --> 12:05:08,200 on the right the one difference is that 13961 12:05:04,276 --> 12:05:10,840 data attributes have to start with data 13962 12:05:08,200 --> 12:05:14,320 Dash and then we can give it any name 13963 12:05:10,840 --> 12:05:16,436 that we want like product- name but we 13964 12:05:14,320 --> 12:05:21,276 have to make sure to separate the words 13965 12:05:16,436 --> 12:05:24,596 with a dash this is also known as Kabob 13966 12:05:21,276 --> 12:05:27,200 case so the purpose of a data attribute 13967 12:05:24,596 --> 12:05:30,320 is that we can attach any information to 13968 12:05:27,200 --> 12:05:32,840 an element so here we attached the 13969 12:05:30,320 --> 12:05:35,360 product name to this button but we can 13970 12:05:32,840 --> 12:05:39,360 also attach the product's image or the 13971 12:05:35,360 --> 12:05:41,956 price and so on so now that we attached 13972 12:05:39,360 --> 12:05:44,596 the product's name to this button when 13973 12:05:41,956 --> 12:05:47,000 we click this button we can actually get 13974 12:05:44,596 --> 12:05:49,160 the name and this will tell us which 13975 12:05:47,000 --> 12:05:51,880 product we should add to the 13976 12:05:49,160 --> 12:05:55,436 cart so let's see what this looks like 13977 12:05:51,880 --> 12:05:58,840 in our code so we'll go to our 13978 12:05:55,436 --> 12:06:01,000 code and then when we click the button 13979 12:05:58,840 --> 12:06:04,320 we're first going to get the product 13980 12:06:01,000 --> 12:06:07,360 name from the button to do that we're 13981 12:06:04,320 --> 12:06:12,880 going to type button and then the 13982 12:06:07,360 --> 12:06:15,640 property dot data set 13983 12:06:12,880 --> 12:06:18,240 so the data set property basically gives 13984 12:06:15,640 --> 12:06:20,720 us all the data attributes that are 13985 12:06:18,240 --> 12:06:23,800 attached to this button so now we're 13986 12:06:20,720 --> 12:06:26,200 able to get the product name so let's 13987 12:06:23,800 --> 12:06:28,956 console.log this to see what it looks 13988 12:06:26,200 --> 12:06:32,480 like 13989 12:06:28,956 --> 12:06:33,800 console.log Open Bracket close bracket 13990 12:06:32,480 --> 12:06:37,240 and 13991 12:06:33,800 --> 12:06:39,320 save and then in here let's click the 13992 12:06:37,240 --> 12:06:42,360 basketballs add to cart 13993 12:06:39,320 --> 12:06:45,000 button and we'll go into our console 13994 12:06:42,360 --> 12:06:47,916 right click 13995 12:06:45,000 --> 12:06:50,116 inspect and 13996 12:06:47,916 --> 12:06:52,480 console and I'm just going to move this 13997 12:06:50,116 --> 12:06:54,040 to the left so we can see it side by 13998 12:06:52,480 --> 12:06:56,756 side with our 13999 12:06:54,040 --> 12:06:59,640 code so this is what the data set 14000 12:06:56,756 --> 12:07:02,000 property looks like it gives us all the 14001 12:06:59,640 --> 12:07:04,520 data attributes that are attached to the 14002 12:07:02,000 --> 12:07:08,480 element so in our case we have the 14003 12:07:04,520 --> 12:07:11,756 product name now this works just like an 14004 12:07:08,480 --> 12:07:17,916 object so to access the product name we 14005 12:07:11,756 --> 12:07:20,360 just have to type dot product capital N 14006 12:07:17,916 --> 12:07:24,276 name so notice that the name gets 14007 12:07:20,360 --> 12:07:26,116 converted from Kebab case to camo case 14008 12:07:24,276 --> 12:07:29,400 so now if we 14009 12:07:26,116 --> 12:07:32,880 save and let's go back to our 14010 12:07:29,400 --> 12:07:35,840 website and click the add to cardart 14011 12:07:32,880 --> 12:07:38,720 button it will get that data attribute 14012 12:07:35,840 --> 12:07:42,320 from the button and display it in the 14013 12:07:38,720 --> 12:07:45,080 console if we click the socks at to cart 14014 12:07:42,320 --> 12:07:48,240 button this will tell us which product 14015 12:07:45,080 --> 12:07:50,916 we should add to the cart so now that we 14016 12:07:48,240 --> 12:07:55,200 have the product name let's actually put 14017 12:07:50,916 --> 12:07:58,720 this into our cart so let's go into our 14018 12:07:55,200 --> 12:08:03,040 code and here let's save the product 14019 12:07:58,720 --> 12:08:06,480 name in a variable called const 14020 12:08:03,040 --> 12:08:09,840 Product name 14021 12:08:06,480 --> 12:08:12,480 equals and then here we're going to add 14022 12:08:09,840 --> 12:08:14,956 this to the cart 14023 12:08:12,480 --> 12:08:17,360 so we already created a cart variable 14024 12:08:14,956 --> 12:08:20,840 here so we're just going to type the 14025 12:08:17,360 --> 12:08:24,360 variable cart and we're going to dot 14026 12:08:20,840 --> 12:08:26,560 push into the array and let's push an 14027 12:08:24,360 --> 12:08:28,400 object because we said we wanted a 14028 12:08:26,560 --> 12:08:31,040 product and the 14029 12:08:28,400 --> 12:08:35,800 quantity so we'll add an object 14030 12:08:31,040 --> 12:08:39,240 containing the product name which is 14031 12:08:35,800 --> 12:08:42,520 this variable product 14032 12:08:39,240 --> 12:08:43,916 name and a quantity 14033 12:08:42,520 --> 12:08:47,436 of 14034 12:08:43,916 --> 12:08:49,880 one and that's pretty much it we first 14035 12:08:47,436 --> 12:08:52,916 attached the product name to the button 14036 12:08:49,880 --> 12:08:56,240 using a data attribute and then when we 14037 12:08:52,916 --> 12:08:59,400 click the button we got the product name 14038 12:08:56,240 --> 12:09:03,436 out and then we add the product to the 14039 12:08:59,400 --> 12:09:06,080 cart so at the bottom let's 14040 12:09:03,436 --> 12:09:08,756 console.log thee cart to see what it 14041 12:09:06,080 --> 12:09:11,916 looks like if we 14042 12:09:08,756 --> 12:09:17,320 save and then go here and open the 14043 12:09:11,916 --> 12:09:20,240 console right click inspect 14044 12:09:17,320 --> 12:09:23,520 console and let's add a basketball to 14045 12:09:20,240 --> 12:09:27,360 our cart so we'll click here and then in 14046 12:09:23,520 --> 12:09:30,880 our cart we now have a basketball with a 14047 12:09:27,360 --> 12:09:33,436 quantity of one so now let's add another 14048 12:09:30,880 --> 12:09:36,520 product to our cart we'll go to the 14049 12:09:33,436 --> 12:09:39,880 t-shirt and click add to 14050 12:09:36,520 --> 12:09:44,080 cart and now we have two products in our 14051 12:09:39,880 --> 12:09:46,800 cart one b basketball and one 14052 12:09:44,080 --> 12:09:50,956 t-shirt so now we're successfully adding 14053 12:09:46,800 --> 12:09:54,596 products to a cart next let's try adding 14054 12:09:50,956 --> 12:09:58,040 one t-shirt again if we click add to 14055 12:09:54,596 --> 12:10:01,400 cart and unfortunately this doesn't look 14056 12:09:58,040 --> 12:10:04,160 quite right the T-shirt product is 14057 12:10:01,400 --> 12:10:07,160 repeated twice and each of them has a 14058 12:10:04,160 --> 12:10:10,040 quantity of one what we actually want to 14059 12:10:07,160 --> 12:10:12,040 do is we want one t-shirt product with a 14060 12:10:10,040 --> 12:10:13,560 quantity of two 14061 12:10:12,040 --> 12:10:16,800 because this is how we're going to 14062 12:10:13,560 --> 12:10:19,320 display it on the cart page so let's 14063 12:10:16,800 --> 12:10:23,160 figure out how to achieve this and we'll 14064 12:10:19,320 --> 12:10:26,360 do this step by step first we'll check 14065 12:10:23,160 --> 12:10:28,880 if the product is already in the cart if 14066 12:10:26,360 --> 12:10:31,916 it is in the cart we'll just increase 14067 12:10:28,880 --> 12:10:35,276 the quantity by one if it's not in the 14068 12:10:31,916 --> 12:10:38,680 cart We'll add it to the cart so let's 14069 12:10:35,276 --> 12:10:41,360 convert these steps into code so the 14070 12:10:38,680 --> 12:10:45,200 first step is to check if a product is 14071 12:10:41,360 --> 12:10:47,080 already in the cart so how do we do that 14072 12:10:45,200 --> 12:10:50,800 let's go back to our 14073 12:10:47,080 --> 12:10:53,720 code and one way to check this is we can 14074 12:10:50,800 --> 12:10:58,116 Loop through our cart and look for a 14075 12:10:53,720 --> 12:11:01,400 particular product name so up here let's 14076 12:10:58,116 --> 12:11:05,040 create some new lines and let's check if 14077 12:11:01,400 --> 12:11:09,400 this product name is already in the cart 14078 12:11:05,040 --> 12:11:14,520 array so we'll type cart and we'll Loop 14079 12:11:09,400 --> 12:11:17,240 through the cart using dot for each 14080 12:11:14,520 --> 12:11:20,916 brackets and then inside we'll give it a 14081 12:11:17,240 --> 12:11:24,080 function brackets arrow and curly 14082 12:11:20,916 --> 12:11:27,720 brackets and we'll give this a parameter 14083 12:11:24,080 --> 12:11:31,276 and let's just call it an item so an 14084 12:11:27,720 --> 12:11:33,720 item will contain a product name and a 14085 12:11:31,276 --> 12:11:36,480 quantity so we want to see if this 14086 12:11:33,720 --> 12:11:40,720 product name is already in the 14087 12:11:36,480 --> 12:11:42,400 card so we're going to check if the 14088 12:11:40,720 --> 12:11:46,640 product 14089 12:11:42,400 --> 12:11:51,480 name over here is equal to this item's 14090 12:11:46,640 --> 12:11:53,000 product name so we'll type equals the 14091 12:11:51,480 --> 12:11:56,756 item. 14092 12:11:53,000 --> 12:12:00,320 product name and curly 14093 12:11:56,756 --> 12:12:02,560 brackets so if the product names match 14094 12:12:00,320 --> 12:12:03,520 that means this product is already in 14095 12:12:02,560 --> 12:12:06,756 the 14096 12:12:03,520 --> 12:12:10,160 cart so let's save this matching item in 14097 12:12:06,756 --> 12:12:11,916 a variable so we can use it later so 14098 12:12:10,160 --> 12:12:15,040 we're going to create the variable 14099 12:12:11,916 --> 12:12:20,276 outside the for each Loop and outside of 14100 12:12:15,040 --> 12:12:23,400 its scope so we'll type let matching 14101 12:12:20,276 --> 12:12:26,680 capital I item and we'll make it 14102 12:12:23,400 --> 12:12:30,000 undefined to start and if we find a 14103 12:12:26,680 --> 12:12:33,276 matching item we're going to save it in 14104 12:12:30,000 --> 12:12:37,116 this variable so 14105 12:12:33,276 --> 12:12:40,116 matching item equals 14106 12:12:37,116 --> 12:12:43,200 item so this is how we figure out if a 14107 12:12:40,116 --> 12:12:46,160 product is already already in the cart 14108 12:12:43,200 --> 12:12:48,596 now the next step is If the product is 14109 12:12:46,160 --> 12:12:53,000 in the cart we'll just increase its 14110 12:12:48,596 --> 12:12:54,800 quantity by one so we'll type 14111 12:12:53,000 --> 12:12:59,480 if 14112 12:12:54,800 --> 12:13:02,200 matching item and curly brackets so we 14113 12:12:59,480 --> 12:13:04,956 can just type matching item here because 14114 12:13:02,200 --> 12:13:09,160 if we did find a matching item it will 14115 12:13:04,956 --> 12:13:11,480 be an object which is a truthy value so 14116 12:13:09,160 --> 12:13:15,880 in here we're going to increase the 14117 12:13:11,480 --> 12:13:17,400 matching items quantity by one using 14118 12:13:15,880 --> 12:13:22,640 matching 14119 12:13:17,400 --> 12:13:26,116 item. quantity and we'll plus equal 14120 12:13:22,640 --> 12:13:29,200 1 and finally If the product is not in 14121 12:13:26,116 --> 12:13:32,276 the cart We'll add it to the cart which 14122 12:13:29,200 --> 12:13:36,720 is basically what we do here so at the 14123 12:13:32,276 --> 12:13:41,276 end we can just type else if we did not 14124 12:13:36,720 --> 12:13:45,000 find a matching item curly brackets and 14125 12:13:41,276 --> 12:13:46,596 move this code so we'll select it right 14126 12:13:45,000 --> 12:13:52,560 click 14127 12:13:46,596 --> 12:13:57,520 cut and then in here rightclick and 14128 12:13:52,560 --> 12:14:00,756 paste and remove the new lines and 14129 12:13:57,520 --> 12:14:01,720 save so now let's go into our project 14130 12:14:00,756 --> 12:14:04,956 we'll 14131 12:14:01,720 --> 12:14:07,880 rightclick inspect the 14132 12:14:04,956 --> 12:14:10,640 console and let's click the add to card 14133 12:14:07,880 --> 12:14:11,680 button for the basketball so we'll click 14134 12:14:10,640 --> 12:14:14,160 this 14135 12:14:11,680 --> 12:14:17,480 and then in our cart we have a 14136 12:14:14,160 --> 12:14:19,916 basketball with a quantity of 14137 12:14:17,480 --> 12:14:23,720 one let's add a 14138 12:14:19,916 --> 12:14:26,840 T-shirt and now we have one T-shirt with 14139 12:14:23,720 --> 12:14:28,596 a quantity of one and if we click add to 14140 12:14:26,840 --> 12:14:31,956 cart on the T-shirt 14141 12:14:28,596 --> 12:14:34,800 again now we have a t-shirt with a 14142 12:14:31,956 --> 12:14:37,560 quantity of two so that's how we solve 14143 12:14:34,800 --> 12:14:40,000 the problem of combining the quantities 14144 12:14:37,560 --> 12:14:41,880 together now there's one last problem 14145 12:14:40,000 --> 12:14:43,916 that we have to solve here 14146 12:14:41,880 --> 12:14:46,720 which is it's not good to use the 14147 12:14:43,916 --> 12:14:49,240 product name to identify the product in 14148 12:14:46,720 --> 12:14:51,756 the cart because in an e-commerce 14149 12:14:49,240 --> 12:14:54,400 website we might actually have two 14150 12:14:51,756 --> 12:14:57,640 products that have the same name but 14151 12:14:54,400 --> 12:15:01,160 from different brands so to fix this we 14152 12:14:57,640 --> 12:15:04,320 should give each product an ID and this 14153 12:15:01,160 --> 12:15:08,596 ID should be unique so no two products 14154 12:15:04,320 --> 12:15:11,000 will have the same ID so let's use an ID 14155 12:15:08,596 --> 12:15:14,956 to identify these products instead of 14156 12:15:11,000 --> 12:15:19,000 using the name we'll go back to our 14157 12:15:14,956 --> 12:15:21,800 code and if we open our files here and 14158 12:15:19,000 --> 12:15:24,276 go to data products. 14159 12:15:21,800 --> 12:15:27,800 JS you'll notice that I've actually 14160 12:15:24,276 --> 12:15:31,080 added an ID for each of these products 14161 12:15:27,800 --> 12:15:33,040 and this ID is unique so I added it here 14162 12:15:31,080 --> 12:15:35,116 to save us a bit of work so we don't 14163 12:15:33,040 --> 12:15:38,360 have to create the IDS 14164 12:15:35,116 --> 12:15:44,000 ourselves so now let's just use this ID 14165 12:15:38,360 --> 12:15:47,956 in our code so we'll go back to amazon. 14166 12:15:44,000 --> 12:15:50,436 JS and then in here instead of using the 14167 12:15:47,956 --> 12:15:54,680 product name we're going to use the 14168 12:15:50,436 --> 12:15:57,160 products ID to identify it in the cart 14169 12:15:54,680 --> 12:16:00,520 but first we need to attach the ID to 14170 12:15:57,160 --> 12:16:03,640 the button so we'll scroll up and 14171 12:16:00,520 --> 12:16:07,596 instead of a data- product name 14172 12:16:03,640 --> 12:16:10,320 attribute we're going to use a data 14173 12:16:07,596 --> 12:16:11,756 product-id attribute and we're going to 14174 12:16:10,320 --> 12:16:14,240 save the 14175 12:16:11,756 --> 12:16:17,200 products ID 14176 12:16:14,240 --> 12:16:21,436 instead and now down here we're going to 14177 12:16:17,200 --> 12:16:26,320 grab the ID from the button to do that 14178 12:16:21,436 --> 12:16:29,000 we'll change this to product ID so 14179 12:16:26,320 --> 12:16:32,480 remember this gets converted from Kebab 14180 12:16:29,000 --> 12:16:35,956 case to cam case and we'll rename this 14181 12:16:32,480 --> 12:16:35,956 variable product 14182 12:16:36,640 --> 12:16:46,360 ID and then here we'll compare product 14183 12:16:41,160 --> 12:16:50,116 ID is equal to the items product 14184 12:16:46,360 --> 12:16:53,640 ID and lastly when we add to the cart 14185 12:16:50,116 --> 12:16:55,956 we're just going to add the product ID 14186 12:16:53,640 --> 12:16:55,956 to the 14187 12:16:57,160 --> 12:17:03,436 cart and 14188 12:17:00,200 --> 12:17:08,520 save so now if we go to our website and 14189 12:17:03,436 --> 12:17:08,520 click add to cart and check our 14190 12:17:09,436 --> 12:17:16,756 console and expand this you can see that 14191 12:17:13,480 --> 12:17:18,956 we're saving the product ID so that two 14192 12:17:16,756 --> 12:17:21,000 products with the same name will be able 14193 12:17:18,956 --> 12:17:24,360 to be added to the 14194 12:17:21,000 --> 12:17:26,560 cart okay so that's how we make the add 14195 12:17:24,360 --> 12:17:28,916 to cart button 14196 12:17:26,560 --> 12:17:32,436 interactive and now we finished all 14197 12:17:28,916 --> 12:17:35,520 three steps first we save the data for 14198 12:17:32,436 --> 12:17:38,596 the website in our JavaScript and then 14199 12:17:35,520 --> 12:17:41,916 we use this data to generate the 14200 12:17:38,596 --> 12:17:45,320 HTML and finally we made it 14201 12:17:41,916 --> 12:17:48,000 interactive and this is the main idea of 14202 12:17:45,320 --> 12:17:51,240 JavaScript this is generally how we use 14203 12:17:48,000 --> 12:17:53,916 JavaScript on a real 14204 12:17:51,240 --> 12:17:57,000 website now let's go back to git so 14205 12:17:53,916 --> 12:18:00,116 we'll open our code editor and open the 14206 12:17:57,000 --> 12:18:03,680 git section and review the changes that 14207 12:18:00,116 --> 12:18:08,116 we made so we'll expand it and then in 14208 12:18:03,680 --> 12:18:11,436 the HTML file we just added the cart. JS 14209 12:18:08,116 --> 12:18:15,436 file and then in here we just created 14210 12:18:11,436 --> 12:18:19,200 the cart array and in our amazon. JS 14211 12:18:15,436 --> 12:18:22,956 file we basically added a data attribute 14212 12:18:19,200 --> 12:18:25,596 to the button and then down here we made 14213 12:18:22,956 --> 12:18:27,160 the button interactive by adding an 14214 12:18:25,596 --> 12:18:30,080 event 14215 12:18:27,160 --> 12:18:33,160 listener so now let's save these changes 14216 12:18:30,080 --> 12:18:34,320 into git we'll type a message to 14217 12:18:33,160 --> 12:18:39,360 describe our 14218 12:18:34,320 --> 12:18:41,680 changes make add to cart button 14219 12:18:39,360 --> 12:18:44,400 Interactive 14220 12:18:41,680 --> 12:18:44,400 and click 14221 12:18:46,080 --> 12:18:52,480 commit all right so let's close this for 14222 12:18:49,880 --> 12:18:55,160 now and the last feature we're going to 14223 12:18:52,480 --> 12:18:58,360 create in this lesson is we're going to 14224 12:18:55,160 --> 12:19:01,596 make this cart quantity in the top right 14225 12:18:58,360 --> 12:19:04,756 interactive so we'll do this step by 14226 12:19:01,596 --> 12:19:07,360 step first we need to calculate the 14227 12:19:04,756 --> 12:19:10,160 quantity or the total number of products 14228 12:19:07,360 --> 12:19:11,200 in our cart and then we're going to put 14229 12:19:10,160 --> 12:19:14,360 that quantity 14230 12:19:11,200 --> 12:19:17,436 on the page here so how do we calculate 14231 12:19:14,360 --> 12:19:20,000 the total quantity of the cart so 14232 12:19:17,436 --> 12:19:23,480 remember that the cart is an array of 14233 12:19:20,000 --> 12:19:25,916 objects and each object has a quantity 14234 12:19:23,480 --> 12:19:28,756 so to calculate the total quantity we 14235 12:19:25,916 --> 12:19:32,596 can Loop through this array and add the 14236 12:19:28,756 --> 12:19:36,916 quantities together let's go into our 14237 12:19:32,596 --> 12:19:38,360 code and we'll open amazon. JS and let's 14238 12:19:36,916 --> 12:19:42,200 scroll to the 14239 12:19:38,360 --> 12:19:44,560 bottom so here after we update our cart 14240 12:19:42,200 --> 12:19:47,680 we're going to calculate the total 14241 12:19:44,560 --> 12:19:50,116 quantity so let's create some new lines 14242 12:19:47,680 --> 12:19:53,720 and as we saw earlier we can Loop 14243 12:19:50,116 --> 12:19:59,680 through the cart array so we'll type 14244 12:19:53,720 --> 12:20:03,560 cart dot for each brackets and a 14245 12:19:59,680 --> 12:20:05,916 function arrow and curly brackets and 14246 12:20:03,560 --> 12:20:08,756 we'll give this a parameter 14247 12:20:05,916 --> 12:20:11,240 item so this is going to Loop through 14248 12:20:08,756 --> 12:20:13,916 each object in the cart 14249 12:20:11,240 --> 12:20:16,480 next we need a variable to store the 14250 12:20:13,916 --> 12:20:20,400 total quantity so let's create a 14251 12:20:16,480 --> 12:20:22,596 variable up here we'll type let and name 14252 12:20:20,400 --> 12:20:26,080 it cart 14253 12:20:22,596 --> 12:20:28,680 quantity and make it equal to zero to 14254 12:20:26,080 --> 12:20:31,400 start and then as we Loop through this 14255 12:20:28,680 --> 12:20:35,400 array we're going to add the items 14256 12:20:31,400 --> 12:20:40,040 quantity to this variable up here so 14257 12:20:35,400 --> 12:20:42,160 here we can type cart quantity plus 14258 12:20:40,040 --> 12:20:44,480 equals the 14259 12:20:42,160 --> 12:20:46,720 items. 14260 12:20:44,480 --> 12:20:49,596 quantity so this will add up all the 14261 12:20:46,720 --> 12:20:53,480 quantities and save it in this 14262 12:20:49,596 --> 12:20:56,800 variable so at the bottom let's 14263 12:20:53,480 --> 12:20:59,840 console.log the cart quantity to make 14264 12:20:56,800 --> 12:21:03,240 sure that this is working so we'll 14265 12:20:59,840 --> 12:21:08,360 save and go back to our page and open 14266 12:21:03,240 --> 12:21:11,596 the console right click inspect and 14267 12:21:08,360 --> 12:21:15,116 console when we click add to cart on the 14268 12:21:11,596 --> 12:21:18,080 basketball the quantity will be one if 14269 12:21:15,116 --> 12:21:22,080 we click add to cart on the T-shirt the 14270 12:21:18,080 --> 12:21:25,040 quantity will be two if we click again 14271 12:21:22,080 --> 12:21:28,116 the quantity will be three so looking at 14272 12:21:25,040 --> 12:21:31,400 the cart it basically added one 14273 12:21:28,116 --> 12:21:34,240 basketball and two T-shirts to get a 14274 12:21:31,400 --> 12:21:37,956 total of three so that's how we can 14275 12:21:34,240 --> 12:21:40,160 calculate the total quantity of the cart 14276 12:21:37,956 --> 12:21:42,360 so now that we have the total quantity 14277 12:21:40,160 --> 12:21:46,596 we're going going to put it on the page 14278 12:21:42,360 --> 12:21:49,276 here and we can do this using the Dom so 14279 12:21:46,596 --> 12:21:52,916 let's go back to our 14280 12:21:49,276 --> 12:21:55,436 code and to use the Dom we first need an 14281 12:21:52,916 --> 12:21:58,520 HTML element where we're going to put 14282 12:21:55,436 --> 12:22:01,480 this quantity so let's find the HTML 14283 12:21:58,520 --> 12:22:03,840 element for this number here we can go 14284 12:22:01,480 --> 12:22:08,000 back to our page we're going to 14285 12:22:03,840 --> 12:22:11,040 rightclick this number and then 14286 12:22:08,000 --> 12:22:12,320 inspect and then we can click this icon 14287 12:22:11,040 --> 12:22:16,240 in the top 14288 12:22:12,320 --> 12:22:19,040 left hover over this number and click 14289 12:22:16,240 --> 12:22:22,360 it so this will tell us that the code 14290 12:22:19,040 --> 12:22:25,756 for the number is this code right here a 14291 12:22:22,360 --> 12:22:27,480 div with a class cart quantity so now 14292 12:22:25,756 --> 12:22:30,956 we're going to search for this class 14293 12:22:27,480 --> 12:22:32,560 cart quantity in our code so let's go 14294 12:22:30,956 --> 12:22:36,436 back to our 14295 12:22:32,560 --> 12:22:39,320 code and let's go to the amazon. HTML 14296 12:22:36,436 --> 12:22:43,000 file because the header is still in this 14297 12:22:39,320 --> 12:22:46,000 file and we'll type contrl f or command 14298 12:22:43,000 --> 12:22:49,040 F to search for code and we'll search 14299 12:22:46,000 --> 12:22:51,240 for cart Das 14300 12:22:49,040 --> 12:22:54,916 quantity and it will tell us that it's 14301 12:22:51,240 --> 12:22:58,400 this line right here so next to use the 14302 12:22:54,916 --> 12:23:01,400 Dom we need to get this element into our 14303 12:22:58,400 --> 12:23:04,276 JavaScript to do that we're going to add 14304 12:23:01,400 --> 12:23:05,756 a class to this element so let's add the 14305 12:23:04,276 --> 12:23:11,320 class 14306 12:23:05,756 --> 12:23:14,480 js- cart D quantity and say 14307 12:23:11,320 --> 12:23:16,800 save now let's go back to our JavaScript 14308 12:23:14,480 --> 12:23:20,756 file and I'm just going to move it down 14309 12:23:16,800 --> 12:23:20,756 here so we can see it side by 14310 12:23:22,520 --> 12:23:28,800 side and we'll scroll down after we 14311 12:23:26,480 --> 12:23:31,520 calculate the quantity let's get this 14312 12:23:28,800 --> 12:23:35,480 element into our JavaScript using the 14313 12:23:31,520 --> 12:23:38,116 Dom so at the bottom we'll type 14314 12:23:35,480 --> 12:23:41,800 document. query 14315 12:23:38,116 --> 12:23:44,756 selector brackets and a string we start 14316 12:23:41,800 --> 12:23:48,320 with a DOT and the class 14317 12:23:44,756 --> 12:23:50,436 js- cart D 14318 12:23:48,320 --> 12:23:54,360 quantity and now that we have this 14319 12:23:50,436 --> 12:23:59,880 element we can change the HTML inside 14320 12:23:54,360 --> 12:24:04,320 using the property do inner Capital 14321 12:23:59,880 --> 12:24:08,160 HTML and we'll make it equal to the cart 14322 12:24:04,320 --> 12:24:09,116 quantity that we calculated above so 14323 12:24:08,160 --> 12:24:12,640 let's 14324 12:24:09,116 --> 12:24:14,520 save and go back to our website and 14325 12:24:12,640 --> 12:24:15,800 let's click the add to card button for 14326 12:24:14,520 --> 12:24:17,800 the 14327 12:24:15,800 --> 12:24:20,956 basketball now it will update the 14328 12:24:17,800 --> 12:24:23,680 quantity to be one let's click the add 14329 12:24:20,956 --> 12:24:26,560 to cart for the T-shirt it will update 14330 12:24:23,680 --> 12:24:29,596 the quantity again and it will be two if 14331 12:24:26,560 --> 12:24:32,800 we click it again it will increase to 14332 12:24:29,596 --> 12:24:34,360 three so now the cart quantity up here 14333 12:24:32,800 --> 12:24:37,116 is 14334 12:24:34,360 --> 12:24:38,880 interactive the last thing we need to do 14335 12:24:37,116 --> 12:24:42,160 is we'll go back to our 14336 12:24:38,880 --> 12:24:45,640 HTML and in instead of starting at three 14337 12:24:42,160 --> 12:24:47,040 the card quantity should start at zero 14338 12:24:45,640 --> 12:24:50,320 so let's 14339 12:24:47,040 --> 12:24:53,480 save and now the cart quantity starts at 14340 12:24:50,320 --> 12:24:56,956 zero if we click add to cart it will 14341 12:24:53,480 --> 12:24:59,756 increase to one so that's how we create 14342 12:24:56,956 --> 12:25:02,240 this cart quantity feature in the final 14343 12:24:59,756 --> 12:25:04,756 project so now we're finished all the 14344 12:25:02,240 --> 12:25:07,956 features that we're going to do in this 14345 12:25:04,756 --> 12:25:09,596 lesson let's go back to our JavaScript 14346 12:25:07,956 --> 12:25:10,916 and I'm just going to move it back up 14347 12:25:09,596 --> 12:25:13,400 here 14348 12:25:10,916 --> 12:25:15,480 and let's clean up this code by removing 14349 12:25:13,400 --> 12:25:17,480 these console. 14350 12:25:15,480 --> 12:25:20,640 logs and 14351 12:25:17,480 --> 12:25:22,840 save and finally let's check git one 14352 12:25:20,640 --> 12:25:26,840 more time to see the changes that we 14353 12:25:22,840 --> 12:25:28,200 made so we can expand this and go to the 14354 12:25:26,840 --> 12:25:32,200 git 14355 12:25:28,200 --> 12:25:35,160 section if we click amazon. HTML it will 14356 12:25:32,200 --> 12:25:38,560 tell us we added this class and if we 14357 12:25:35,160 --> 12:25:40,840 click amazon. JS it will show us the 14358 12:25:38,560 --> 12:25:44,756 code that we added for C calculating the 14359 12:25:40,840 --> 12:25:47,956 card quantity and putting it on the 14360 12:25:44,756 --> 12:25:49,480 page so finally let's type a message to 14361 12:25:47,956 --> 12:25:56,080 describe our 14362 12:25:49,480 --> 12:25:59,560 changes make the cart quantity 14363 12:25:56,080 --> 12:25:59,560 interactive and 14364 12:26:00,040 --> 12:26:06,560 commit so now we can close 14365 12:26:03,040 --> 12:26:09,160 this and that's the end of this lesson 14366 12:26:06,560 --> 12:26:10,756 in this lesson we started the final 14367 12:26:09,160 --> 12:26:13,400 Amazon Project 14368 12:26:10,756 --> 12:26:17,040 we set up and learned how to use git to 14369 12:26:13,400 --> 12:26:20,480 track changes we learned the main idea 14370 12:26:17,040 --> 12:26:23,756 of JavaScript which is save the data 14371 12:26:20,480 --> 12:26:26,200 generate the HTML and make it 14372 12:26:23,756 --> 12:26:29,360 interactive we created a list of 14373 12:26:26,200 --> 12:26:32,756 products on the homepage we made the add 14374 12:26:29,360 --> 12:26:36,200 to cart button interactive and we made 14375 12:26:32,756 --> 12:26:36,200 the cart quantity 14376 12:26:37,800 --> 12:26:42,040 interactive thanks for watching this 14377 12:26:39,680 --> 12:26:43,756 course so so far if you want to support 14378 12:26:42,040 --> 12:26:46,080 this channel you can check out the 14379 12:26:43,756 --> 12:26:48,160 premium version of my courses just like 14380 12:26:46,080 --> 12:26:50,640 this one where you can get a certificate 14381 12:26:48,160 --> 12:26:53,000 of completion and a Better Learning 14382 12:26:50,640 --> 12:26:56,436 platform if you're interested in extra 14383 12:26:53,000 --> 12:26:58,560 videos from me more in-depth videos and 14384 12:26:56,436 --> 12:27:01,560 behind the scenes content you can check 14385 12:26:58,560 --> 12:27:04,000 out these links here you can also 14386 12:27:01,560 --> 12:27:06,436 support the channel by liking the video 14387 12:27:04,000 --> 12:27:09,080 subscribing and sharing this video with 14388 12:27:06,436 --> 12:27:11,956 others thanks again and let's continue 14389 12:27:09,080 --> 12:27:13,916 with the course 14390 12:27:11,956 --> 12:27:18,840 here are some exercises to help you 14391 12:27:13,916 --> 12:27:18,840 practice what we learned in this lesson 14392 12:28:04,000 --> 12:28:08,640 in this lesson we're going to learn a 14393 12:28:05,840 --> 12:28:11,276 feature of JavaScript called modules 14394 12:28:08,640 --> 12:28:14,240 which is a better way to organize our 14395 12:28:11,276 --> 12:28:17,360 code so first let's take a look at a 14396 12:28:14,240 --> 12:28:19,880 problem that we have in our current code 14397 12:28:17,360 --> 12:28:22,480 we're going to go to our files here and 14398 12:28:19,880 --> 12:28:24,680 we're going to open amazon. 14399 12:28:22,480 --> 12:28:28,596 HTML and then we're going to scroll to 14400 12:28:24,680 --> 12:28:31,560 the bottom of this file to our script 14401 12:28:28,596 --> 12:28:33,956 tags so right now we're using three 14402 12:28:31,560 --> 12:28:37,000 script tags to run all of our 14403 12:28:33,956 --> 12:28:40,200 JavaScript so this runs all the code in 14404 12:28:37,000 --> 12:28:43,480 cart. JS and then it runs all the code 14405 12:28:40,200 --> 12:28:47,040 in products. JS and finally it runs all 14406 12:28:43,480 --> 12:28:49,640 the code in amazon. JS so this is 14407 12:28:47,040 --> 12:28:52,840 basically the same thing as combining 14408 12:28:49,640 --> 12:28:56,160 all the files together into one big file 14409 12:28:52,840 --> 12:28:58,160 and then running all of our code however 14410 12:28:56,160 --> 12:29:01,436 there's a big problem with using script 14411 12:28:58,160 --> 12:29:04,756 tags like this and that problem is it 14412 12:29:01,436 --> 12:29:07,480 can cause naming conflicts so in our 14413 12:29:04,756 --> 12:29:10,800 situation here we created a variable 14414 12:29:07,480 --> 12:29:12,680 named cart in cart. Js 14415 12:29:10,800 --> 12:29:16,240 this means we can't use the variable 14416 12:29:12,680 --> 12:29:19,240 name cart in any of our other files so 14417 12:29:16,240 --> 12:29:22,400 for example if we went to amazon. 14418 12:29:19,240 --> 12:29:25,360 JS and then we scroll to the top 14419 12:29:22,400 --> 12:29:28,116 here and let's say that we try to create 14420 12:29:25,360 --> 12:29:32,276 another variable named cart so at the 14421 12:29:28,116 --> 12:29:34,680 top we type const cart equals square 14422 12:29:32,276 --> 12:29:37,596 brackets and 14423 12:29:34,680 --> 12:29:40,000 save and now if we go back to our 14424 12:29:37,596 --> 12:29:44,116 project you'll notice that the website 14425 12:29:40,000 --> 12:29:46,800 stops working and if we rightclick 14426 12:29:44,116 --> 12:29:49,116 inspect and open the 14427 12:29:46,800 --> 12:29:51,720 console it will tell us that we got an 14428 12:29:49,116 --> 12:29:54,480 error and it says that cart has already 14429 12:29:51,720 --> 12:29:56,560 been declared so this is happening 14430 12:29:54,480 --> 12:29:59,800 because we created a variable called 14431 12:29:56,560 --> 12:30:02,840 cart in cart. JS and then later in 14432 12:29:59,800 --> 12:30:05,160 amazon. JS we created another variable 14433 12:30:02,840 --> 12:30:06,840 named cart now we can't have two 14434 12:30:05,160 --> 12:30:09,520 variables with the same name in 14435 12:30:06,840 --> 12:30:12,116 JavaScript and this causes an error and 14436 12:30:09,520 --> 12:30:14,840 it causes our website to break so this 14437 12:30:12,116 --> 12:30:17,040 is called a naming conflict when we have 14438 12:30:14,840 --> 12:30:20,160 two variables with the same 14439 12:30:17,040 --> 12:30:23,480 name so the big problem with script tags 14440 12:30:20,160 --> 12:30:23,480 is if we go back to our 14441 12:30:25,160 --> 12:30:30,040 code you can see here that it's really 14442 12:30:27,756 --> 12:30:32,800 hard to tell which variables have been 14443 12:30:30,040 --> 12:30:35,360 created in other files and it's really 14444 12:30:32,800 --> 12:30:38,680 easy to accidentally create a naming 14445 12:30:35,360 --> 12:30:40,956 conflict like this so that's the problem 14446 12:30:38,680 --> 12:30:44,000 with using script tags like this and 14447 12:30:40,956 --> 12:30:46,116 running our code one file at a time it's 14448 12:30:44,000 --> 12:30:48,200 very easy to get naming 14449 12:30:46,116 --> 12:30:50,520 conflicts so now we're going to learn 14450 12:30:48,200 --> 12:30:53,000 how to solve this problem using a 14451 12:30:50,520 --> 12:30:56,480 feature of JavaScript called 14452 12:30:53,000 --> 12:30:59,720 modules so a module basically contains a 14453 12:30:56,480 --> 12:31:02,200 variable inside a file so now this 14454 12:30:59,720 --> 12:31:05,560 variable is not going to cause conflicts 14455 12:31:02,200 --> 12:31:08,200 with anything outside the file for 14456 12:31:05,560 --> 12:31:10,596 example in our project we created a 14457 12:31:08,200 --> 12:31:13,596 variable called cart and we want to 14458 12:31:10,596 --> 12:31:16,680 contain this variable inside the cart. 14459 12:31:13,596 --> 12:31:20,200 JS file so that it doesn't conflict with 14460 12:31:16,680 --> 12:31:23,956 the cart variable inside the amazon. JS 14461 12:31:20,200 --> 12:31:27,000 file so we can do this using a module so 14462 12:31:23,956 --> 12:31:30,956 now let's learn how to create our first 14463 12:31:27,000 --> 12:31:34,436 module we're going to go back to amazon. 14464 12:31:30,956 --> 12:31:36,560 HTML and then here to create a module 14465 12:31:34,436 --> 12:31:39,436 we're simply going to delete this line 14466 12:31:36,560 --> 12:31:43,596 of code that uses a script tag to load 14467 12:31:39,436 --> 12:31:46,200 cart. JS so we'll delete it and 14468 12:31:43,596 --> 12:31:50,116 save and you'll see that the website is 14469 12:31:46,200 --> 12:31:52,720 working again so how does this work when 14470 12:31:50,116 --> 12:31:56,596 we use a script tag it will run all the 14471 12:31:52,720 --> 12:31:58,956 code inside cart. JS so this code will 14472 12:31:56,596 --> 12:32:02,000 create a card variable and this will 14473 12:31:58,956 --> 12:32:04,360 cause a naming Conflict by removing the 14474 12:32:02,000 --> 12:32:07,800 script tag we're no longer running the 14475 12:32:04,360 --> 12:32:10,480 code inside cart. JS so we're no longer 14476 12:32:07,800 --> 12:32:13,916 creating a cart variable and it will no 14477 12:32:10,480 --> 12:32:13,916 longer cause a naming 14478 12:32:13,956 --> 12:32:21,596 conflict so to create a module we simply 14479 12:32:17,160 --> 12:32:24,596 do two steps number one we create a file 14480 12:32:21,596 --> 12:32:27,520 and write our code inside the file and 14481 12:32:24,596 --> 12:32:31,240 number two don't load the file with a 14482 12:32:27,520 --> 12:32:33,800 script tag by doing this any variables 14483 12:32:31,240 --> 12:32:36,640 we create inside the file will be 14484 12:32:33,800 --> 12:32:39,360 contained inside the file and it won't 14485 12:32:36,640 --> 12:32:42,360 cause naming conflicts so that's 14486 12:32:39,360 --> 12:32:45,480 basically how we create a 14487 12:32:42,360 --> 12:32:49,320 module okay so now we run into another 14488 12:32:45,480 --> 12:32:52,560 problem in our code if we open amazon. 14489 12:32:49,320 --> 12:32:55,116 JS we created this card variable as an 14490 12:32:52,560 --> 12:32:57,880 example but what we actually want to do 14491 12:32:55,116 --> 12:33:00,880 is to keep the cart variable inside 14492 12:32:57,880 --> 12:33:03,560 cart. JS because we want to keep all the 14493 12:33:00,880 --> 12:33:07,520 code that's related to the cart inside 14494 12:33:03,560 --> 12:33:07,520 this file so in 14495 12:33:10,880 --> 12:33:15,596 and if we 14496 12:33:11,880 --> 12:33:18,116 save and then go back to our project and 14497 12:33:15,596 --> 12:33:20,800 click add to cart you'll notice that it 14498 12:33:18,116 --> 12:33:23,756 no longer works so let's look at the 14499 12:33:20,800 --> 12:33:26,200 console again to see what error we get 14500 12:33:23,756 --> 12:33:28,560 we'll right click 14501 12:33:26,200 --> 12:33:31,000 inspect and open the 14502 12:33:28,560 --> 12:33:34,276 console and it's going to tell us that 14503 12:33:31,000 --> 12:33:37,520 cart is not defined so the problem that 14504 12:33:34,276 --> 12:33:40,916 we have here is that in this file we're 14505 12:33:37,520 --> 12:33:43,320 using the variable cart but now because 14506 12:33:40,916 --> 12:33:46,080 we're not loading cart. JS using a 14507 12:33:43,320 --> 12:33:48,956 script tag the cart variable doesn't 14508 12:33:46,080 --> 12:33:54,200 exist anymore it's contained inside 14509 12:33:48,956 --> 12:33:54,200 cart. JS and we can't access it from the 14510 12:33:54,436 --> 12:34:00,400 outside so now we're going to learn how 14511 12:33:56,840 --> 12:34:03,040 to get a variable out of a file or a 14512 12:34:00,400 --> 12:34:06,276 module to do that we're going to do 14513 12:34:03,040 --> 12:34:09,276 three steps the first step is to add a 14514 12:34:06,276 --> 12:34:12,360 type equals module attribute the second 14515 12:34:09,276 --> 12:34:16,116 step is to export and the third step is 14516 12:34:12,360 --> 12:34:19,276 to import let's learn these steps one by 14517 12:34:16,116 --> 12:34:22,160 one so the first step is to add a type 14518 12:34:19,276 --> 12:34:24,840 equals module attribute we're going to 14519 12:34:22,160 --> 12:34:27,160 open amazon. 14520 12:34:24,840 --> 12:34:31,080 HTML and we're going to scroll to the 14521 12:34:27,160 --> 12:34:33,956 bottom and here in the last script tag 14522 12:34:31,080 --> 12:34:37,160 where we load amazon. JS we're going to 14523 12:34:33,956 --> 12:34:42,000 add an HTML attribute here so we're 14524 12:34:37,160 --> 12:34:43,596 going to type type equal equal module 14525 12:34:42,000 --> 12:34:47,080 and 14526 12:34:43,596 --> 12:34:49,916 save so the type equals module attribute 14527 12:34:47,080 --> 12:34:53,116 basically lets this file get variables 14528 12:34:49,916 --> 12:34:56,800 out of other files it basically enables 14529 12:34:53,116 --> 12:35:00,200 this feature now the second step is to 14530 12:34:56,800 --> 12:35:02,840 export so we're going to go to cart. 14531 12:35:00,200 --> 12:35:05,800 JS and now we're going to choose which 14532 12:35:02,840 --> 12:35:08,880 variables can be accessed outside of 14533 12:35:05,800 --> 12:35:11,520 this file so for example we want to 14534 12:35:08,880 --> 12:35:14,720 access the VAR variable cart outside of 14535 12:35:11,520 --> 12:35:16,680 this file to do that in front of the 14536 12:35:14,720 --> 12:35:22,360 variable we're just going to type the 14537 12:35:16,680 --> 12:35:24,916 word export and space and save so now 14538 12:35:22,360 --> 12:35:26,000 this variable can be used outside of 14539 12:35:24,916 --> 12:35:29,800 cart. 14540 12:35:26,000 --> 12:35:32,320 JS the last step is to 14541 12:35:29,800 --> 12:35:34,840 import so we're going to go to the place 14542 12:35:32,320 --> 12:35:37,040 where we're going to use this variable 14543 12:35:34,840 --> 12:35:40,400 which is inside amazon. 14544 12:35:37,040 --> 12:35:42,880 JS and now at the top we're going to 14545 12:35:40,400 --> 12:35:47,320 tell the computer which variable we want 14546 12:35:42,880 --> 12:35:50,320 to get from another file so we'll type 14547 12:35:47,320 --> 12:35:53,040 import open curly bracket close curly 14548 12:35:50,320 --> 12:35:55,160 bracket and between these brackets we're 14549 12:35:53,040 --> 12:35:58,800 going to give the name of the variable 14550 12:35:55,160 --> 12:36:00,560 we want to get which is the cart 14551 12:35:58,800 --> 12:36:03,320 variable and then we'll tell the 14552 12:36:00,560 --> 12:36:07,160 computer which file we want to get this 14553 12:36:03,320 --> 12:36:10,840 variable from so we'll type 14554 12:36:07,160 --> 12:36:13,436 from single quotes and and 14555 12:36:10,840 --> 12:36:16,116 semicolon and now inside these quotes 14556 12:36:13,436 --> 12:36:19,560 we're going to locate the file using a 14557 12:36:16,116 --> 12:36:22,320 file path so let's open up our files 14558 12:36:19,560 --> 12:36:25,880 here so it's easy to see what files we 14559 12:36:22,320 --> 12:36:29,080 have so right now we're in amazon. JS 14560 12:36:25,880 --> 12:36:31,400 and we want to locate cart. JS so the 14561 12:36:29,080 --> 12:36:33,480 first step is we actually need to get 14562 12:36:31,400 --> 12:36:35,596 out of the scripts folder we're in the 14563 12:36:33,480 --> 12:36:38,640 scripts folder and we need to get out of 14564 12:36:35,596 --> 12:36:40,560 it and into the data folder so to get 14565 12:36:38,640 --> 12:36:45,080 out of the current Cent folder that this 14566 12:36:40,560 --> 12:36:47,200 file is in we're going to type dot dot 14567 12:36:45,080 --> 12:36:50,360 so dot dot basically represents the 14568 12:36:47,200 --> 12:36:52,116 folder outside of this current folder 14569 12:36:50,360 --> 12:36:54,840 which is this folder right here 14570 12:36:52,116 --> 12:36:57,720 JavaScript Amazon projects and then 14571 12:36:54,840 --> 12:37:00,956 we're going to type a forward slash to 14572 12:36:57,720 --> 12:37:02,596 go outside of the scripts folder so now 14573 12:37:00,956 --> 12:37:06,040 that we're outside of this folder we're 14574 12:37:02,596 --> 12:37:08,880 going to go into the data folder so here 14575 12:37:06,040 --> 12:37:11,276 we're going to type data and then a 14576 12:37:08,880 --> 12:37:15,800 slash to go into this folder and we're 14577 12:37:11,276 --> 12:37:18,640 going to locate the file cart. JS 14578 12:37:15,800 --> 12:37:21,680 C.J and 14579 12:37:18,640 --> 12:37:24,596 save so what this code does is that it's 14580 12:37:21,680 --> 12:37:28,320 going to take the variable cart out of 14581 12:37:24,596 --> 12:37:31,400 the file cart. JS and we use this file 14582 12:37:28,320 --> 12:37:34,000 path here to locate that file so as a 14583 12:37:31,400 --> 12:37:36,360 reminder dot dot means to go outside of 14584 12:37:34,000 --> 12:37:38,756 the current folder outside of scripts 14585 12:37:36,360 --> 12:37:41,240 into here and then we go into the data 14586 12:37:38,756 --> 12:37:44,436 folder and then inside the data folder 14587 12:37:41,240 --> 12:37:44,436 we find cart. 14588 12:37:45,160 --> 12:37:52,320 JS so those are the three steps to get a 14589 12:37:48,200 --> 12:37:55,000 variable out of a module first we add a 14590 12:37:52,320 --> 12:37:58,596 type equals module attribute to enable 14591 12:37:55,000 --> 12:38:01,480 this feature and then we export the 14592 12:37:58,596 --> 12:38:04,720 variables that we want to get out and 14593 12:38:01,480 --> 12:38:06,436 finally we import the variables when we 14594 12:38:04,720 --> 12:38:08,756 need 14595 12:38:06,436 --> 12:38:12,200 them now there's two more things that we 14596 12:38:08,756 --> 12:38:14,116 need to know about modules number one we 14597 12:38:12,200 --> 12:38:15,840 have to put all of our Imports at the 14598 12:38:14,116 --> 12:38:19,080 top of the 14599 12:38:15,840 --> 12:38:22,560 file and number two in order for modules 14600 12:38:19,080 --> 12:38:25,080 to work we need to use live server so 14601 12:38:22,560 --> 12:38:27,000 for example if I open the folder that 14602 12:38:25,080 --> 12:38:31,520 contains amazon. 14603 12:38:27,000 --> 12:38:34,756 HTML so I open JavaScript Amazon Project 14604 12:38:31,520 --> 12:38:38,360 and then I select this HTML file and 14605 12:38:34,756 --> 12:38:41,956 rightclick open with Google 14606 12:38:38,360 --> 12:38:44,240 Chrome the website won't work and that's 14607 12:38:41,956 --> 12:38:47,680 because modules don't work if we open 14608 12:38:44,240 --> 12:38:49,240 the HTML file directly like this so 14609 12:38:47,680 --> 12:38:52,360 let's close this for 14610 12:38:49,240 --> 12:38:55,200 now and to get modules to work we need 14611 12:38:52,360 --> 12:38:57,160 to open our code editor and then 14612 12:38:55,200 --> 12:39:01,480 rightclick the HTML 14613 12:38:57,160 --> 12:39:01,480 file and open with live 14614 12:39:01,720 --> 12:39:07,040 server so in this course in order for 14615 12:39:04,436 --> 12:39:11,320 modules to work we need to use live 14616 12:39:07,040 --> 12:39:11,320 server to open our HTML files 14617 12:39:11,916 --> 12:39:17,520 so now that we learned how to use a 14618 12:39:13,956 --> 12:39:20,400 module let's talk about the benefits of 14619 12:39:17,520 --> 12:39:23,720 modules the first big benefit is that it 14620 12:39:20,400 --> 12:39:26,040 helps us avoid naming conflicts the only 14621 12:39:23,720 --> 12:39:28,800 time that this causes naming conflicts 14622 12:39:26,040 --> 12:39:32,040 is if we import this variable and we 14623 12:39:28,800 --> 12:39:34,240 have another variable named cart inside 14624 12:39:32,040 --> 12:39:37,436 this file like 14625 12:39:34,240 --> 12:39:40,480 this so this will again create a naming 14626 12:39:37,436 --> 12:39:43,160 conflict if I save this the website will 14627 12:39:40,480 --> 12:39:45,520 stop working but at least now we have 14628 12:39:43,160 --> 12:39:47,916 control over when naming conflicts 14629 12:39:45,520 --> 12:39:50,720 happen they will only happen if we 14630 12:39:47,916 --> 12:39:52,720 import the same variable name now 14631 12:39:50,720 --> 12:39:55,880 modules have another feature that allows 14632 12:39:52,720 --> 12:39:58,680 us to avoid this problem as well so 14633 12:39:55,880 --> 12:40:02,756 after the variable name that we import 14634 12:39:58,680 --> 12:40:07,756 we can type as and then a different name 14635 12:40:02,756 --> 12:40:10,800 such as my cart and save so this will 14636 12:40:07,756 --> 12:40:13,720 basically take the input and rename it 14637 12:40:10,800 --> 12:40:16,840 to my cart so that this will no longer 14638 12:40:13,720 --> 12:40:18,756 conflict with this variable name so as 14639 12:40:16,840 --> 12:40:21,160 you can see modules have a lot of 14640 12:40:18,756 --> 12:40:23,560 features that help us avoid naming 14641 12:40:21,160 --> 12:40:26,400 conflicts when we have lots of files in 14642 12:40:23,560 --> 12:40:29,360 our project so now I'm just going to 14643 12:40:26,400 --> 12:40:32,520 delete this and put the code back 14644 12:40:29,360 --> 12:40:35,240 because that was just an example and 14645 12:40:32,520 --> 12:40:37,240 save so the second big benefit of 14646 12:40:35,240 --> 12:40:40,840 modules is that we don't have to worry 14647 12:40:37,240 --> 12:40:42,596 about the order of our files 14648 12:40:40,840 --> 12:40:45,116 before when we loaded everything with 14649 12:40:42,596 --> 12:40:48,840 script tags we had to make sure that we 14650 12:40:45,116 --> 12:40:51,276 load C.J first because we need the cart 14651 12:40:48,840 --> 12:40:53,596 variable to be created and then we can 14652 12:40:51,276 --> 12:40:56,520 use it in amazon. 14653 12:40:53,596 --> 12:40:58,720 JS so the order of our script tags 14654 12:40:56,520 --> 12:41:01,040 really matter and it becomes really hard 14655 12:40:58,720 --> 12:41:02,560 to manage when we have lots of files in 14656 12:41:01,040 --> 12:41:05,680 a big 14657 12:41:02,560 --> 12:41:08,480 project but now with modules we can just 14658 12:41:05,680 --> 12:41:10,560 import the variables that we need and we 14659 12:41:08,480 --> 12:41:12,040 don't have to worry about the order that 14660 12:41:10,560 --> 12:41:15,480 we're loading our 14661 12:41:12,040 --> 12:41:18,040 files so as you can see modules are a 14662 12:41:15,480 --> 12:41:20,880 better way to organize our code 14663 12:41:18,040 --> 12:41:24,116 especially in bigger projects so now 14664 12:41:20,880 --> 12:41:26,720 let's use git and save our changes so 14665 12:41:24,116 --> 12:41:29,436 we'll go to the git section here and it 14666 12:41:26,720 --> 12:41:32,680 will tell us we changed three files in 14667 12:41:29,436 --> 12:41:36,880 amazon. JS we removed the script tag and 14668 12:41:32,680 --> 12:41:39,916 added type equals module in cart. JS we 14669 12:41:36,880 --> 12:41:43,800 exported the cart variable and in 14670 12:41:39,916 --> 12:41:47,000 amazon. JS we imported the cart variable 14671 12:41:43,800 --> 12:41:50,400 from cart. JS so now let's save these 14672 12:41:47,000 --> 12:41:52,276 into get so here we'll type a message 14673 12:41:50,400 --> 12:41:58,520 use 14674 12:41:52,276 --> 12:42:00,916 modules and import cart from cart. JS 14675 12:41:58,520 --> 12:42:00,916 and 14676 12:42:03,240 --> 12:42:09,436 commit now that we learned modules let's 14677 12:42:06,276 --> 12:42:12,436 use them in more places in our project 14678 12:42:09,436 --> 12:42:15,200 so let's close this for now and we're 14679 12:42:12,436 --> 12:42:17,916 going to open amazon. 14680 12:42:15,200 --> 12:42:21,680 HTML and next we're going to turn 14681 12:42:17,916 --> 12:42:23,840 products. JS into a module as well so 14682 12:42:21,680 --> 12:42:26,000 we'll start off by deleting the script 14683 12:42:23,840 --> 12:42:29,080 tag because the script tag will just 14684 12:42:26,000 --> 12:42:31,880 load all the code onto the page so we'll 14685 12:42:29,080 --> 12:42:35,160 delete this and 14686 12:42:31,880 --> 12:42:38,756 save so notice that our website stops 14687 12:42:35,160 --> 12:42:40,880 working and now inside amazon. JS we 14688 12:42:38,756 --> 12:42:43,756 need to to access products from 14689 12:42:40,880 --> 12:42:47,080 products. JS so again we're going to do 14690 12:42:43,756 --> 12:42:49,680 three steps the first step is to add a 14691 12:42:47,080 --> 12:42:51,956 type equals module attribute which we 14692 12:42:49,680 --> 12:42:55,276 already have so we don't need to do this 14693 12:42:51,956 --> 12:42:58,080 again the second step is to export the 14694 12:42:55,276 --> 12:43:00,200 variables that we want to get out so in 14695 12:42:58,080 --> 12:43:04,436 this case we want to get the products 14696 12:43:00,200 --> 12:43:07,720 variable out of the products. JS file so 14697 12:43:04,436 --> 12:43:09,320 let's open our files and open products. 14698 12:43:07,720 --> 12:43:11,436 Js 14699 12:43:09,320 --> 12:43:14,916 and we'll scroll to the 14700 12:43:11,436 --> 12:43:17,756 top so to get the products variable out 14701 12:43:14,916 --> 12:43:22,756 of this file in front of the variable 14702 12:43:17,756 --> 12:43:26,040 we're going to type export and space and 14703 12:43:22,756 --> 12:43:29,720 save and the last step is to import the 14704 12:43:26,040 --> 12:43:33,720 variable that we need so inside amazon. 14705 12:43:29,720 --> 12:43:37,080 JS we need the products variable so at 14706 12:43:33,720 --> 12:43:39,720 the top we're going to type 14707 12:43:37,080 --> 12:43:41,640 import curly brackets 14708 12:43:39,720 --> 12:43:44,000 and then inside here we're going to type 14709 12:43:41,640 --> 12:43:46,080 the variable name that we want to get 14710 12:43:44,000 --> 12:43:48,276 which is 14711 12:43:46,080 --> 12:43:53,116 products and we're going to get this 14712 12:43:48,276 --> 12:43:55,160 variable from single quotes semicolon 14713 12:43:53,116 --> 12:43:58,400 and then inside here we're going to 14714 12:43:55,160 --> 12:44:01,956 locate the products. JS file so we're 14715 12:43:58,400 --> 12:44:04,400 going to type dot dot and slash to go 14716 12:44:01,956 --> 12:44:06,400 outside of the current folder so outside 14717 12:44:04,400 --> 12:44:09,956 of the scripts folder and then we're 14718 12:44:06,400 --> 12:44:13,240 going to go into the data folder so type 14719 12:44:09,956 --> 12:44:16,360 data and a slash to go into the folder 14720 12:44:13,240 --> 12:44:19,880 and we're going to locate the file 14721 12:44:16,360 --> 12:44:22,400 products. JS and 14722 12:44:19,880 --> 12:44:25,080 save so now that we're importing this 14723 12:44:22,400 --> 12:44:27,240 variable from this file the rest of our 14724 12:44:25,080 --> 12:44:28,596 code will work and our website will 14725 12:44:27,240 --> 12:44:32,200 start working 14726 12:44:28,596 --> 12:44:35,400 again so that's how we turn products. JS 14727 12:44:32,200 --> 12:44:37,360 into a module as well so let's go into 14728 12:44:35,400 --> 12:44:41,480 get and take a look at our 14729 12:44:37,360 --> 12:44:45,840 changes so in amazon. HTML we deleted 14730 12:44:41,480 --> 12:44:48,240 the script tag and then in products. JS 14731 12:44:45,840 --> 12:44:52,320 we exported the variables that we wanted 14732 12:44:48,240 --> 12:44:54,800 to get out and then in amazon. JS we 14733 12:44:52,320 --> 12:44:56,916 imported the variables that we need from 14734 12:44:54,800 --> 12:44:59,720 the 14735 12:44:56,916 --> 12:45:05,000 file so let's type a message and save 14736 12:44:59,720 --> 12:45:10,916 these changes into git import products 14737 12:45:05,000 --> 12:45:10,916 from products. JS and 14738 12:45:13,276 --> 12:45:19,756 commit let's close 14739 12:45:15,800 --> 12:45:22,640 this and let's open amazon. 14740 12:45:19,756 --> 12:45:25,116 HTML now the last thing is you might be 14741 12:45:22,640 --> 12:45:28,956 wondering should we also delete this 14742 12:45:25,116 --> 12:45:31,840 script tag that loads amazon. JS and the 14743 12:45:28,956 --> 12:45:35,520 answer is no because we still need one 14744 12:45:31,840 --> 12:45:37,640 Javascript file to load our code so this 14745 12:45:35,520 --> 12:45:39,560 file will import from all the other 14746 12:45:37,640 --> 12:45:41,880 files that we created 14747 12:45:39,560 --> 12:45:43,200 and this is how we organize our code in 14748 12:45:41,880 --> 12:45:48,276 a real 14749 12:45:43,200 --> 12:45:48,276 website so this file is called the entry 14750 12:45:48,320 --> 12:45:52,756 point so now that we've seen some 14751 12:45:50,400 --> 12:45:56,480 examples of modules we're going to use 14752 12:45:52,756 --> 12:45:59,080 modules to organize our code better so 14753 12:45:56,480 --> 12:46:01,320 let's go into amazon. 14754 12:45:59,080 --> 12:46:04,480 JS and then we're going to scroll down 14755 12:46:01,320 --> 12:46:06,276 to the code for the add to card button 14756 12:46:04,480 --> 12:46:09,680 so inside this file we're going to 14757 12:46:06,276 --> 12:46:12,640 scroll down here all the way down to 14758 12:46:09,680 --> 12:46:15,840 this code here so this is a code that we 14759 12:46:12,640 --> 12:46:17,436 run when we click add it to cart and 14760 12:46:15,840 --> 12:46:20,040 you'll notice that we're running a lot 14761 12:46:17,436 --> 12:46:22,200 of code here when we click that button 14762 12:46:20,040 --> 12:46:25,040 we're running code that adds the product 14763 12:46:22,200 --> 12:46:27,160 to the cart and we're also running code 14764 12:46:25,040 --> 12:46:29,360 that calculates the quantity and updates 14765 12:46:27,160 --> 12:46:31,520 the page so a best practice in 14766 12:46:29,360 --> 12:46:33,956 programming is that when we have a lot 14767 12:46:31,520 --> 12:46:35,840 of code here that does different things 14768 12:46:33,956 --> 12:46:38,200 it's better to split these up into 14769 12:46:35,840 --> 12:46:42,240 smaller functions to make our code 14770 12:46:38,200 --> 12:46:45,560 easier to read so here this part of the 14771 12:46:42,240 --> 12:46:48,240 code takes the product ID and then adds 14772 12:46:45,560 --> 12:46:51,436 it to the cart so it makes sense to 14773 12:46:48,240 --> 12:46:54,756 split this into its own function so 14774 12:46:51,436 --> 12:46:57,400 let's scroll up and at the top here 14775 12:46:54,756 --> 12:47:01,320 let's create a function for this code so 14776 12:46:57,400 --> 12:47:06,640 we'll type function and let's call it 14777 12:47:01,320 --> 12:47:08,116 add to cart brackets and curly brackets 14778 12:47:06,640 --> 12:47:10,956 and then we're just going to move all 14779 12:47:08,116 --> 12:47:12,560 this code into the function so we'll 14780 12:47:10,956 --> 12:47:14,956 select this 14781 12:47:12,560 --> 12:47:16,596 code and then 14782 12:47:14,956 --> 12:47:19,240 rightclick 14783 12:47:16,596 --> 12:47:23,276 cut and then inside this 14784 12:47:19,240 --> 12:47:25,880 function rightclick and 14785 12:47:23,276 --> 12:47:30,000 paste and then down here we're going to 14786 12:47:25,880 --> 12:47:33,596 run this function inside here so when we 14787 12:47:30,000 --> 12:47:37,160 click add to cart we're going to run add 14788 12:47:33,596 --> 12:47:40,320 to cart function that we just created 14789 12:47:37,160 --> 12:47:42,720 and Save 14790 12:47:40,320 --> 12:47:44,436 so now let's give this a try so we'll 14791 12:47:42,720 --> 12:47:47,116 click add to 14792 12:47:44,436 --> 12:47:49,160 cart and unfortunately it doesn't work 14793 12:47:47,116 --> 12:47:51,956 anymore so let's take a look at the 14794 12:47:49,160 --> 12:47:54,480 console to see if there's any errors 14795 12:47:51,956 --> 12:47:56,956 we'll rightclick 14796 12:47:54,480 --> 12:47:59,400 inspect and then open the 14797 12:47:56,956 --> 12:48:02,756 console and it's telling us that product 14798 12:47:59,400 --> 12:48:03,840 ID is not defined so if we look at our 14799 12:48:02,756 --> 12:48:07,000 code 14800 12:48:03,840 --> 12:48:09,436 here and scroll up in the function 14801 12:48:07,000 --> 12:48:11,640 you'll notice that we're using product 14802 12:48:09,436 --> 12:48:14,240 but it's no longer anywhere in the 14803 12:48:11,640 --> 12:48:16,320 function and that's because product ID 14804 12:48:14,240 --> 12:48:18,400 is outside the 14805 12:48:16,320 --> 12:48:20,596 function so we actually want to keep 14806 12:48:18,400 --> 12:48:23,596 this code outside because it's using 14807 12:48:20,596 --> 12:48:25,956 this button so instead we're going to 14808 12:48:23,596 --> 12:48:29,520 take product ID and put it into a 14809 12:48:25,956 --> 12:48:31,916 function using a parameter so at the top 14810 12:48:29,520 --> 12:48:34,276 of the function we're going to add a 14811 12:48:31,916 --> 12:48:36,916 parameter product 14812 12:48:34,276 --> 12:48:39,756 ID and then when we call this function 14813 12:48:36,916 --> 12:48:42,360 we're going to pass it the product 14814 12:48:39,756 --> 12:48:45,400 ID so we're going to take this value put 14815 12:48:42,360 --> 12:48:48,240 it into the function and save it in here 14816 12:48:45,400 --> 12:48:51,040 and then the function can run as normal 14817 12:48:48,240 --> 12:48:53,916 so now if we save and then go to our 14818 12:48:51,040 --> 12:48:56,720 project and click add to cart you can 14819 12:48:53,916 --> 12:49:01,200 see that it's working 14820 12:48:56,720 --> 12:49:03,360 again next let's go back and scroll 14821 12:49:01,200 --> 12:49:06,040 down and we're going to put the rest of 14822 12:49:03,360 --> 12:49:08,800 our code into its own function as well 14823 12:49:06,040 --> 12:49:11,400 to make it more organized so at the top 14824 12:49:08,800 --> 12:49:13,116 here let's create some new lines and 14825 12:49:11,400 --> 12:49:16,116 create a 14826 12:49:13,116 --> 12:49:19,436 function and this code updates the card 14827 12:49:16,116 --> 12:49:23,200 quantity on the page 14828 12:49:19,436 --> 12:49:28,040 here so let's name this function 14829 12:49:23,200 --> 12:49:30,596 update cart quantity brackets and curly 14830 12:49:28,040 --> 12:49:34,160 brackets and we'll move this code into 14831 12:49:30,596 --> 12:49:38,800 the function so let's select this code 14832 12:49:34,160 --> 12:49:40,880 and then rightclick cut and then in here 14833 12:49:38,800 --> 12:49:44,400 rightclick and 14834 12:49:40,880 --> 12:49:50,116 paste and then down here we'll call the 14835 12:49:44,400 --> 12:49:53,000 function using update cart quantity and 14836 12:49:50,116 --> 12:49:56,160 brackets and 14837 12:49:53,000 --> 12:49:58,560 save so as you can see by splitting up 14838 12:49:56,160 --> 12:50:01,640 our code into smaller functions it makes 14839 12:49:58,560 --> 12:50:04,200 our code a lot easier to read when we 14840 12:50:01,640 --> 12:50:06,680 click add to cart it's really easy to 14841 12:50:04,200 --> 12:50:09,116 understand what's going on first we're 14842 12:50:06,680 --> 12:50:11,080 going to add a product to our cart and 14843 12:50:09,116 --> 12:50:14,360 then we're going to update the card 14844 12:50:11,080 --> 12:50:17,436 quantity so now let's save and then go 14845 12:50:14,360 --> 12:50:20,080 back to our project and try it out and 14846 12:50:17,436 --> 12:50:22,276 you can see that everything is working 14847 12:50:20,080 --> 12:50:25,040 again so we're going to do two more 14848 12:50:22,276 --> 12:50:27,560 things to better organize our code so 14849 12:50:25,040 --> 12:50:30,276 let's go back to our code and the first 14850 12:50:27,560 --> 12:50:33,360 thing is we're going to scroll up to the 14851 12:50:30,276 --> 12:50:35,436 add to cart function and I made a small 14852 12:50:33,360 --> 12:50:37,840 mistake in the previous lesson I 14853 12:50:35,436 --> 12:50:41,956 actually want to rename this parameter 14854 12:50:37,840 --> 12:50:44,000 from item to cart item instead to make 14855 12:50:41,956 --> 12:50:47,840 it a little more clear so we're just 14856 12:50:44,000 --> 12:50:53,160 going to rename this to cart item and 14857 12:50:47,840 --> 12:50:56,040 here as well we name it to cart item and 14858 12:50:53,160 --> 12:50:57,400 save and we'll scroll down here and 14859 12:50:56,040 --> 12:51:00,756 we're going to do the same thing for 14860 12:50:57,400 --> 12:51:06,720 this parameter so we'll rename it to 14861 12:51:00,756 --> 12:51:09,720 cart item and then here cart item and 14862 12:51:06,720 --> 12:51:09,720 Save 14863 12:51:10,276 --> 12:51:14,680 and the last thing we'll do is let's 14864 12:51:12,436 --> 12:51:17,320 scroll up to the add to cart 14865 12:51:14,680 --> 12:51:20,080 function so you'll notice that this code 14866 12:51:17,320 --> 12:51:22,720 manages our cart so it's actually a best 14867 12:51:20,080 --> 12:51:26,276 practice to move this function into 14868 12:51:22,720 --> 12:51:29,116 cart. JS because cart. JS contains all 14869 12:51:26,276 --> 12:51:31,560 the code that's related to the cart a 14870 12:51:29,116 --> 12:51:34,880 best practice is to group related code 14871 12:51:31,560 --> 12:51:38,240 together into its own 14872 12:51:34,880 --> 12:51:41,160 file so here let's take the function add 14873 12:51:38,240 --> 12:51:43,520 to cart we'll select it and then 14874 12:51:41,160 --> 12:51:47,640 rightclick 14875 12:51:43,520 --> 12:51:52,200 cut and then inside cart. JS we're going 14876 12:51:47,640 --> 12:51:54,756 to rightclick and paste and just move 14877 12:51:52,200 --> 12:51:56,840 that function into this file because 14878 12:51:54,756 --> 12:51:59,680 this file contains all the code that's 14879 12:51:56,840 --> 12:52:02,276 related to the cart so now that this 14880 12:51:59,680 --> 12:52:05,400 function is inside the file we need to 14881 12:52:02,276 --> 12:52:08,560 access it from amazon. 14882 12:52:05,400 --> 12:52:12,360 JS and we can do that using modules 14883 12:52:08,560 --> 12:52:16,480 using the features export and import so 14884 12:52:12,360 --> 12:52:19,160 inside cart. JS in front of the function 14885 12:52:16,480 --> 12:52:22,560 we're going to export it so we can use 14886 12:52:19,160 --> 12:52:26,276 it in other files so let's save this 14887 12:52:22,560 --> 12:52:28,400 file and then inside amazon. JS we're 14888 12:52:26,276 --> 12:52:30,840 going to scroll to the 14889 12:52:28,400 --> 12:52:34,200 top and you'll notice that we're already 14890 12:52:30,840 --> 12:52:35,956 importing something from cart. JS so we 14891 12:52:34,200 --> 12:52:38,756 actually don't need to write import 14892 12:52:35,956 --> 12:52:42,240 again here we can actually import 14893 12:52:38,756 --> 12:52:45,240 multiple things from this file by typing 14894 12:52:42,240 --> 12:52:47,680 comma and space and then something else 14895 12:52:45,240 --> 12:52:49,800 that we want to import so in our case 14896 12:52:47,680 --> 12:52:54,956 we're going to import the function add 14897 12:52:49,800 --> 12:52:58,596 to cart so here we'll type comma add to 14898 12:52:54,956 --> 12:53:01,040 cart so now it will import both the cart 14899 12:52:58,596 --> 12:53:03,756 and the add to cart function from this 14900 12:53:01,040 --> 12:53:05,520 file so Imports can import multiple 14901 12:53:03,756 --> 12:53:09,200 things from a 14902 12:53:05,520 --> 12:53:12,840 file Imports have another syntax import 14903 12:53:09,200 --> 12:53:16,200 Star as this Imports everything from a 14904 12:53:12,840 --> 12:53:18,640 file and groups it together inside this 14905 12:53:16,200 --> 12:53:22,880 object and then we can access each 14906 12:53:18,640 --> 12:53:22,880 import as a property or a 14907 12:53:24,680 --> 12:53:29,000 method so lastly I'm just going to 14908 12:53:26,880 --> 12:53:31,276 scroll down to my code 14909 12:53:29,000 --> 12:53:34,916 here and I'm just going to remove this 14910 12:53:31,276 --> 12:53:34,916 extra new line and 14911 12:53:34,956 --> 12:53:39,880 save now you might be wondering if we 14912 12:53:37,320 --> 12:53:41,640 should move update cart quantity into 14913 12:53:39,880 --> 12:53:44,640 cart. JS as 14914 12:53:41,640 --> 12:53:46,800 well so this function actually handles 14915 12:53:44,640 --> 12:53:48,916 updating the web page rather than 14916 12:53:46,800 --> 12:53:52,040 managing the cart so we're going to keep 14917 12:53:48,916 --> 12:53:55,160 this function inside this file for 14918 12:53:52,040 --> 12:53:58,800 now so now let's go into get and save 14919 12:53:55,160 --> 12:54:02,360 our changes so we'll go here and inside 14920 12:53:58,800 --> 12:54:05,320 cart. JS we moved the add to cart 14921 12:54:02,360 --> 12:54:08,800 function and then we exported it and 14922 12:54:05,320 --> 12:54:10,000 then inside amazon. JS we imported the 14923 12:54:08,800 --> 12:54:13,240 add to card 14924 12:54:10,000 --> 12:54:15,720 function and then down here we took the 14925 12:54:13,240 --> 12:54:18,756 big block of code that we had before and 14926 12:54:15,720 --> 12:54:21,840 we separated it into two functions add 14927 12:54:18,756 --> 12:54:24,800 to cart and update cart quantity and 14928 12:54:21,840 --> 12:54:27,360 this made our code a lot easier to 14929 12:54:24,800 --> 12:54:32,480 read so now let's type a message 14930 12:54:27,360 --> 12:54:38,436 describing our changes move add to cart 14931 12:54:32,480 --> 12:54:38,436 into cart. JS and commit 14932 12:54:41,436 --> 12:54:45,480 next we're going to create the checkout 14933 12:54:43,436 --> 12:54:47,640 page in our final project and we're 14934 12:54:45,480 --> 12:54:51,080 going to use modules to organize our 14935 12:54:47,640 --> 12:54:53,240 code again so let's open the final 14936 12:54:51,080 --> 12:54:55,240 project and we're going to click this 14937 12:54:53,240 --> 12:54:57,956 cart icon in the top 14938 12:54:55,240 --> 12:55:00,596 right and this will take us to another 14939 12:54:57,956 --> 12:55:02,480 page which is the checkout page so on 14940 12:55:00,596 --> 12:55:03,840 this page we can see the products that 14941 12:55:02,480 --> 12:55:05,916 are in our 14942 12:55:03,840 --> 12:55:08,756 cart so if you're wondering how that 14943 12:55:05,916 --> 12:55:12,080 cart icon works it basically uses is an 14944 12:55:08,756 --> 12:55:14,800 HTML element called a link element which 14945 12:55:12,080 --> 12:55:18,040 looks like this link elements have a 14946 12:55:14,800 --> 12:55:21,200 special attribute called href when we 14947 12:55:18,040 --> 12:55:23,680 click a link element whatever is inside 14948 12:55:21,200 --> 12:55:27,840 the href attribute it will open it in 14949 12:55:23,680 --> 12:55:30,560 the page so if we put youtube.com inside 14950 12:55:27,840 --> 12:55:33,916 the href and we click the link it will 14951 12:55:30,560 --> 12:55:37,240 open youtube.com in our browser if we 14952 12:55:33,916 --> 12:55:39,880 put a file like checkout. HTML inside 14953 12:55:37,240 --> 12:55:44,480 href it will look for a file called 14954 12:55:39,880 --> 12:55:44,480 checkout. HTML and open that 14955 12:55:44,596 --> 12:55:50,680 file so in our project if we look at our 14956 12:55:48,240 --> 12:55:53,520 code and open our 14957 12:55:50,680 --> 12:55:56,436 files inside amazon. 14958 12:55:53,520 --> 12:55:59,200 HTML we're basically just using a link 14959 12:55:56,436 --> 12:56:03,116 element or an a element and this has an 14960 12:55:59,200 --> 12:56:05,040 hre of checkout. HTML so what this does 14961 12:56:03,116 --> 12:56:08,640 is it's going to look for a file called 14962 12:56:05,040 --> 12:56:13,160 checkout. HTML beside the current file 14963 12:56:08,640 --> 12:56:15,400 amazon. HTML so here's amazon. HTML and 14964 12:56:13,160 --> 12:56:17,720 it's going to look for checkout. HTML 14965 12:56:15,400 --> 12:56:20,436 beside it and then open that in our 14966 12:56:17,720 --> 12:56:23,200 browser which is how that cart icon 14967 12:56:20,436 --> 12:56:26,880 works so in our browser at the top you 14968 12:56:23,200 --> 12:56:28,680 can see that it opened checkout. HTML so 14969 12:56:26,880 --> 12:56:30,916 that's basically how a link element 14970 12:56:28,680 --> 12:56:33,680 works and how we can navigate from one 14971 12:56:30,916 --> 12:56:36,756 page to another you can learn more about 14972 12:56:33,680 --> 12:56:39,116 link elements in my HTML and CSS full 14973 12:56:36,756 --> 12:56:41,360 course but for now now we're going to 14974 12:56:39,116 --> 12:56:42,436 learn how to build the checkout page 14975 12:56:41,360 --> 12:56:44,800 using 14976 12:56:42,436 --> 12:56:48,320 JavaScript so first let's take a look at 14977 12:56:44,800 --> 12:56:50,436 the code for this page so again the code 14978 12:56:48,320 --> 12:56:53,680 is inside checkout. 14979 12:56:50,436 --> 12:56:56,480 HTML so in our code editor we're going 14980 12:56:53,680 --> 12:56:59,756 to open checkout. 14981 12:56:56,480 --> 12:57:02,640 HTML and this is a code for the page 14982 12:56:59,756 --> 12:57:05,880 that we saw earlier that contains our 14983 12:57:02,640 --> 12:57:08,200 cart so just like with the homepage 14984 12:57:05,880 --> 12:57:10,480 instead of writing all this HTML 14985 12:57:08,200 --> 12:57:12,360 manually we're going to use JavaScript 14986 12:57:10,480 --> 12:57:15,640 to generate this 14987 12:57:12,360 --> 12:57:18,360 HTML so first we need to add JavaScript 14988 12:57:15,640 --> 12:57:19,640 to this page so let's scroll down to the 14989 12:57:18,360 --> 12:57:23,200 very bottom 14990 12:57:19,640 --> 12:57:26,200 here and right before the end of the 14991 12:57:23,200 --> 12:57:28,480 body we're going to create a script 14992 12:57:26,200 --> 12:57:30,276 element to load some 14993 12:57:28,480 --> 12:57:33,956 JavaScript and we'll give it an 14994 12:57:30,276 --> 12:57:36,680 attribute Source equals double quotes 14995 12:57:33,956 --> 12:57:38,640 and then a file we want to load so let's 14996 12:57:36,680 --> 12:57:42,756 create a new Javascript file file for 14997 12:57:38,640 --> 12:57:45,320 this page so inside our scripts folder 14998 12:57:42,756 --> 12:57:48,320 let's click amazon. JS and then click 14999 12:57:45,320 --> 12:57:53,360 this icon to create a new file and let's 15000 12:57:48,320 --> 12:57:55,480 name this file checkout. JS and press 15001 12:57:53,360 --> 12:57:57,956 enter and now we're going to load this 15002 12:57:55,480 --> 12:58:01,840 page inside checkout. 15003 12:57:57,956 --> 12:58:04,596 HTML so here in the source attribute 15004 12:58:01,840 --> 12:58:07,080 we're going to locate this file so first 15005 12:58:04,596 --> 12:58:08,640 we need to go into this scripts folder 15006 12:58:07,080 --> 12:58:11,436 so we'll type 15007 12:58:08,640 --> 12:58:15,320 scripts and then a slash to go into the 15008 12:58:11,436 --> 12:58:17,276 folder and then we'll load the file 15009 12:58:15,320 --> 12:58:20,240 checkout. 15010 12:58:17,276 --> 12:58:25,400 JS and next we're going to add another 15011 12:58:20,240 --> 12:58:27,956 attribute type equals double quotes 15012 12:58:25,400 --> 12:58:30,840 module so remember from earlier in the 15013 12:58:27,956 --> 12:58:33,916 lesson this attribute enables module 15014 12:58:30,840 --> 12:58:38,276 features for this file so now this file 15015 12:58:33,916 --> 12:58:39,680 can import from other JavaScript files 15016 12:58:38,276 --> 12:58:44,160 so now let's 15017 12:58:39,680 --> 12:58:46,720 save and let's open checkout. JS and 15018 12:58:44,160 --> 12:58:49,000 we'll start creating this page with 15019 12:58:46,720 --> 12:58:51,680 JavaScript so let's go back to our page 15020 12:58:49,000 --> 12:58:54,756 first and take a look at the structure 15021 12:58:51,680 --> 12:58:57,640 of this page so you can see on this page 15022 12:58:54,756 --> 12:59:00,000 we actually have two sections we have 15023 12:58:57,640 --> 12:59:03,040 the cart section on the left which shows 15024 12:59:00,000 --> 12:59:05,520 the products in our cart and we also 15025 12:59:03,040 --> 12:59:08,480 have the payment section on the right 15026 12:59:05,520 --> 12:59:10,800 which calculates the cost of our order 15027 12:59:08,480 --> 12:59:14,276 so let's use JavaScript to create the 15028 12:59:10,800 --> 12:59:17,160 cart section first so remember the main 15029 12:59:14,276 --> 12:59:19,596 idea of JavaScript is first we're going 15030 12:59:17,160 --> 12:59:22,800 to save the data and then we're going to 15031 12:59:19,596 --> 12:59:23,756 generate the HTML and finally we'll make 15032 12:59:22,800 --> 12:59:26,160 it 15033 12:59:23,756 --> 12:59:28,436 interactive let's start by saving the 15034 12:59:26,160 --> 12:59:31,200 data that we need for this section of 15035 12:59:28,436 --> 12:59:33,916 the page so for this section we're 15036 12:59:31,200 --> 12:59:36,200 basically displaying a list of products 15037 12:59:33,916 --> 12:59:38,320 along with the quantity of each product 15038 12:59:36,200 --> 12:59:40,680 that we want to buy and and if you 15039 12:59:38,320 --> 12:59:43,400 remember we already created a data 15040 12:59:40,680 --> 12:59:46,800 structure to represent this which is our 15041 12:59:43,400 --> 12:59:49,240 cart inside our cart we have the ID of 15042 12:59:46,800 --> 12:59:52,160 the product as well as the quantity that 15043 12:59:49,240 --> 12:59:55,756 we want to buy so we can just reuse this 15044 12:59:52,160 --> 12:59:57,480 data structure to create this section so 15045 12:59:55,756 --> 13:00:01,116 let's go into our 15046 12:59:57,480 --> 13:00:04,240 code and we're going to open cart. 15047 13:00:01,116 --> 13:00:06,720 JS so inside here we have an array 15048 13:00:04,240 --> 13:00:08,720 called cart that's supposed to represent 15049 13:00:06,720 --> 13:00:12,200 our data here 15050 13:00:08,720 --> 13:00:14,596 so right now our card is empty so to 15051 13:00:12,200 --> 13:00:16,840 make it easier for us to develop we're 15052 13:00:14,596 --> 13:00:20,520 just going to add some default values 15053 13:00:16,840 --> 13:00:23,640 into this array so inside here let's 15054 13:00:20,520 --> 13:00:26,640 create an object and put one product 15055 13:00:23,640 --> 13:00:29,520 into our card first so looking at this 15056 13:00:26,640 --> 13:00:32,880 page the product that we want to put is 15057 13:00:29,520 --> 13:00:34,840 the socks so we'll go back into our code 15058 13:00:32,880 --> 13:00:38,000 and I'll rearrange my windows so we can 15059 13:00:34,840 --> 13:00:40,596 see it side by side and we're going to 15060 13:00:38,000 --> 13:00:42,880 get the product ID for the socks here so 15061 13:00:40,596 --> 13:00:46,000 we're going to go into products. 15062 13:00:42,880 --> 13:00:47,956 JS and then here's the socks we just 15063 13:00:46,000 --> 13:00:51,480 going to take this 15064 13:00:47,956 --> 13:00:54,080 ID right click 15065 13:00:51,480 --> 13:00:57,240 copy and then inside the 15066 13:00:54,080 --> 13:01:00,800 cart and remember we structured our data 15067 13:00:57,240 --> 13:01:02,436 so that each card item has a product ID 15068 13:01:00,800 --> 13:01:05,040 as well as a 15069 13:01:02,436 --> 13:01:07,400 quantity so here we're going to create 15070 13:01:05,040 --> 13:01:08,640 the same structure that our current code 15071 13:01:07,400 --> 13:01:11,436 uses 15072 13:01:08,640 --> 13:01:16,880 so we're going to give each object a 15073 13:01:11,436 --> 13:01:19,040 product ID colon and single quotes and 15074 13:01:16,880 --> 13:01:22,680 we're just going to rightclick and 15075 13:01:19,040 --> 13:01:25,000 paste the ID that we copied earlier and 15076 13:01:22,680 --> 13:01:26,520 then we'll type a comma and we'll give 15077 13:01:25,000 --> 13:01:30,160 it a 15078 13:01:26,520 --> 13:01:33,240 quantity colon and for the first item 15079 13:01:30,160 --> 13:01:35,160 we're going to give it a quantity of two 15080 13:01:33,240 --> 13:01:38,436 so we'll just create a cart that kind of 15081 13:01:35,160 --> 13:01:41,560 matches what we see on this page 15082 13:01:38,436 --> 13:01:44,880 next let's create a second card item to 15083 13:01:41,560 --> 13:01:48,560 represent the basketball so we'll type 15084 13:01:44,880 --> 13:01:53,200 comma curly brackets we'll give it a 15085 13:01:48,560 --> 13:01:55,240 product ID colon and single quotes and 15086 13:01:53,200 --> 13:01:59,200 then we're going to get the ID for this 15087 13:01:55,240 --> 13:02:01,880 basketball so inside products. JS we 15088 13:01:59,200 --> 13:02:04,436 going to scroll down to the basketball 15089 13:02:01,880 --> 13:02:08,360 here and we're going to copy this 15090 13:02:04,436 --> 13:02:10,040 ID right click copy 15091 13:02:08,360 --> 13:02:14,480 and then inside cart. 15092 13:02:10,040 --> 13:02:17,720 JS in here right click and 15093 13:02:14,480 --> 13:02:21,240 paste and we'll give this one a 15094 13:02:17,720 --> 13:02:23,840 quantity colon one and 15095 13:02:21,240 --> 13:02:25,756 save so what we're doing here is we're 15096 13:02:23,840 --> 13:02:28,720 just creating some default values that 15097 13:02:25,756 --> 13:02:31,116 we can use to write the code to generate 15098 13:02:28,720 --> 13:02:33,080 this page so this is just for 15099 13:02:31,116 --> 13:02:36,520 development when we're actually using 15100 13:02:33,080 --> 13:02:39,720 the website the cart will start off as 15101 13:02:36,520 --> 13:02:42,200 empty so so if we compare our data to 15102 13:02:39,720 --> 13:02:44,400 our page you might notice that there's a 15103 13:02:42,200 --> 13:02:47,756 few things that are missing such as the 15104 13:02:44,400 --> 13:02:50,160 product's image the name and the price 15105 13:02:47,756 --> 13:02:53,040 so how come we're not saving these into 15106 13:02:50,160 --> 13:02:55,560 our data and that's because we're using 15107 13:02:53,040 --> 13:02:58,116 a special technique we're just saving 15108 13:02:55,560 --> 13:03:00,756 the product's ID and then we can use 15109 13:02:58,116 --> 13:03:04,520 this ID to search for this product 15110 13:03:00,756 --> 13:03:07,400 inside the products array here so using 15111 13:03:04,520 --> 13:03:10,240 the ID we can find all the other details 15112 13:03:07,400 --> 13:03:13,436 of the product like the image the name 15113 13:03:10,240 --> 13:03:15,880 and the price so there's no need to save 15114 13:03:13,436 --> 13:03:19,560 this data twice inside the products 15115 13:03:15,880 --> 13:03:22,000 array and inside the cart array all we 15116 13:03:19,560 --> 13:03:24,916 have to do is save the ID of the product 15117 13:03:22,000 --> 13:03:27,720 that we want and then search for it so 15118 13:03:24,916 --> 13:03:31,320 this technique is called duplicating the 15119 13:03:27,720 --> 13:03:34,480 data or normalizing the data and this is 15120 13:03:31,320 --> 13:03:37,200 a very common technique in software 15121 13:03:34,480 --> 13:03:38,956 engineering okay so the one other thing 15122 13:03:37,200 --> 13:03:41,480 that we probably need to save in our 15123 13:03:38,956 --> 13:03:43,596 data is the delivery times and the 15124 13:03:41,480 --> 13:03:45,520 delivery options but we're going to 15125 13:03:43,596 --> 13:03:47,160 leave this for the next lesson because 15126 13:03:45,520 --> 13:03:48,200 it requires some more features that 15127 13:03:47,160 --> 13:03:51,400 we're going to 15128 13:03:48,200 --> 13:03:54,596 learn so for now we finished saving the 15129 13:03:51,400 --> 13:03:58,040 data that we need the next step is to 15130 13:03:54,596 --> 13:04:01,596 use this data to generate the 15131 13:03:58,040 --> 13:04:03,916 HTML so let's open checkout. 15132 13:04:01,596 --> 13:04:06,720 JS so I'm just going to drag it to the 15133 13:04:03,916 --> 13:04:09,640 bottom here so we can see it side by 15134 13:04:06,720 --> 13:04:09,640 side 15135 13:04:10,680 --> 13:04:16,080 so the first step we need to do is to 15136 13:04:12,796 --> 13:04:20,520 take the cart array and import it into 15137 13:04:16,080 --> 13:04:25,200 checkout. JS so we can use it so in here 15138 13:04:20,520 --> 13:04:29,320 let's import curly brackets the cart 15139 13:04:25,200 --> 13:04:31,880 array and we're going to import it from 15140 13:04:29,320 --> 13:04:33,880 single quotes and semicolon and we're 15141 13:04:31,880 --> 13:04:37,720 going to use a file path between the 15142 13:04:33,880 --> 13:04:41,200 quotes to locate cart. JS so let's open 15143 13:04:37,720 --> 13:04:44,080 our files again and to get from here to 15144 13:04:41,200 --> 13:04:47,680 here we need to go out of the scripts 15145 13:04:44,080 --> 13:04:51,276 folder so to do that we can type dot dot 15146 13:04:47,680 --> 13:04:53,240 and then slash to go to the outer folder 15147 13:04:51,276 --> 13:04:56,640 and then we're going to go to the data 15148 13:04:53,240 --> 13:04:59,480 folder so we'll type data slash to go 15149 13:04:56,640 --> 13:05:05,840 into this folder and we'll locate cart. 15150 13:04:59,480 --> 13:05:07,916 JS so cart. JS and save and next we're 15151 13:05:05,840 --> 13:05:10,956 going to Loop through this cart and 15152 13:05:07,916 --> 13:05:11,796 generate the HTML just like we did for 15153 13:05:10,956 --> 13:05:15,276 the 15154 13:05:11,796 --> 13:05:17,840 homepage so let's close this for now and 15155 13:05:15,276 --> 13:05:23,160 here we're going to create a loop over 15156 13:05:17,840 --> 13:05:26,200 the cart so we'll type cart. for each 15157 13:05:23,160 --> 13:05:28,200 brackets and a 15158 13:05:26,200 --> 13:05:31,560 function and then we'll give it a 15159 13:05:28,200 --> 13:05:34,400 parameter let's call it cart 15160 13:05:31,560 --> 13:05:36,560 item and then for each cart item here 15161 13:05:34,400 --> 13:05:39,796 we're going to generate this 15162 13:05:36,560 --> 13:05:43,360 HTML so in here let's create a template 15163 13:05:39,796 --> 13:05:45,400 string for the HTML using back ticks and 15164 13:05:43,360 --> 13:05:47,756 we'll create some new lines and then 15165 13:05:45,400 --> 13:05:51,520 here we're going to copy and paste the 15166 13:05:47,756 --> 13:05:53,756 HTML for one of these products so first 15167 13:05:51,520 --> 13:05:56,720 let's find the HTML for one of these 15168 13:05:53,756 --> 13:05:58,560 products and to do that we can 15169 13:05:56,720 --> 13:06:01,520 rightclick 15170 13:05:58,560 --> 13:06:04,240 inspect and then click this top left 15171 13:06:01,520 --> 13:06:05,956 icon and then click the element we want 15172 13:06:04,240 --> 13:06:09,640 to get the HTML 15173 13:06:05,956 --> 13:06:11,680 for so here's the HTML for one of these 15174 13:06:09,640 --> 13:06:14,360 products and we're going to search for 15175 13:06:11,680 --> 13:06:17,796 this class card item container and our 15176 13:06:14,360 --> 13:06:21,880 code so inside our 15177 13:06:17,796 --> 13:06:24,796 code let's open checkout. 15178 13:06:21,880 --> 13:06:27,320 HTML and we'll type control F or command 15179 13:06:24,796 --> 13:06:32,520 F to search for code and we'll search 15180 13:06:27,320 --> 13:06:35,956 for the class cart D item D 15181 13:06:32,520 --> 13:06:39,796 container which is this code right here 15182 13:06:35,956 --> 13:06:42,000 so if we collapse it and collapse this 15183 13:06:39,796 --> 13:06:45,200 this is the code for these two products 15184 13:06:42,000 --> 13:06:46,796 here so we're going to take this HTML 15185 13:06:45,200 --> 13:06:49,400 and we're going to move it into our 15186 13:06:46,796 --> 13:06:53,116 JavaScript so we can generate it with 15187 13:06:49,400 --> 13:06:57,520 JavaScript so we'll right click 15188 13:06:53,116 --> 13:06:59,520 copy and then in here right click and 15189 13:06:57,520 --> 13:07:02,200 paste and if we need to fix the 15190 13:06:59,520 --> 13:07:05,520 formatting we can select all of these 15191 13:07:02,200 --> 13:07:07,680 lines and then press shift tab to remove 15192 13:07:05,520 --> 13:07:10,956 some indents 15193 13:07:07,680 --> 13:07:13,596 and I'll add an indent here next when 15194 13:07:10,956 --> 13:07:16,276 we're generating this HTML we want to 15195 13:07:13,596 --> 13:07:18,956 replace this with the actual products 15196 13:07:16,276 --> 13:07:21,240 image and this with the actual products 15197 13:07:18,956 --> 13:07:23,840 name so we're going to take this card 15198 13:07:21,240 --> 13:07:27,560 item and use it to get the data that we 15199 13:07:23,840 --> 13:07:29,916 need so let's open card. JS to see what 15200 13:07:27,560 --> 13:07:32,720 each cart item looks like so we have the 15201 13:07:29,916 --> 13:07:35,480 product ID and the 15202 13:07:32,720 --> 13:07:38,116 quantity so first we need to use this 15203 13:07:35,480 --> 13:07:40,360 product ID to search for the product in 15204 13:07:38,116 --> 13:07:42,720 order to get the other details like the 15205 13:07:40,360 --> 13:07:45,680 image the name and the 15206 13:07:42,720 --> 13:07:49,160 price so to do that let's get the 15207 13:07:45,680 --> 13:07:53,680 product ID out of the cart item first so 15208 13:07:49,160 --> 13:07:59,080 we'll create a variable const product ID 15209 13:07:53,680 --> 13:08:02,040 equals the cart item do product 15210 13:07:59,080 --> 13:08:05,560 ID and now we need to use this to search 15211 13:08:02,040 --> 13:08:07,796 for the full product so the full product 15212 13:08:05,560 --> 13:08:09,160 if we open our files is INS inside 15213 13:08:07,796 --> 13:08:13,116 products. 15214 13:08:09,160 --> 13:08:14,560 JS which is this array right here so I'm 15215 13:08:13,116 --> 13:08:16,916 just going to move this up here so we 15216 13:08:14,560 --> 13:08:20,720 can see it side by 15217 13:08:16,916 --> 13:08:23,720 side so to search inside this array we 15218 13:08:20,720 --> 13:08:26,840 actually need to import products into 15219 13:08:23,720 --> 13:08:31,160 checkout. JS so let's use some module 15220 13:08:26,840 --> 13:08:32,116 features again at the top we'll import 15221 13:08:31,160 --> 13:08:35,916 curly 15222 13:08:32,116 --> 13:08:39,200 brackets the products array and we'll 15223 13:08:35,916 --> 13:08:42,400 import it from single quotes and 15224 13:08:39,200 --> 13:08:45,880 semicolon and then here we'll locate the 15225 13:08:42,400 --> 13:08:49,116 products. JS file so we'll type do dot 15226 13:08:45,880 --> 13:08:51,756 slash to go out of the scripts folder 15227 13:08:49,116 --> 13:08:54,480 and then we'll go into the data folder 15228 13:08:51,756 --> 13:08:58,480 data and then a slash to go into this 15229 13:08:54,480 --> 13:08:59,596 folder and we import from products. JS 15230 13:08:58,480 --> 13:09:04,360 so 15231 13:08:59,596 --> 13:09:07,040 products. JS and save so now we have 15232 13:09:04,360 --> 13:09:09,080 access to this products array here and 15233 13:09:07,040 --> 13:09:12,560 we'll search the array for the full 15234 13:09:09,080 --> 13:09:15,840 product details so down 15235 13:09:12,560 --> 13:09:19,040 here let's create a variable to save the 15236 13:09:15,840 --> 13:09:22,680 result so we'll type let and we'll name 15237 13:09:19,040 --> 13:09:25,320 it matching 15238 13:09:22,680 --> 13:09:28,640 product and then down here we'll Loop 15239 13:09:25,320 --> 13:09:34,320 through the products array so we'll type 15240 13:09:28,640 --> 13:09:36,360 products. for each brackets and a 15241 13:09:34,320 --> 13:09:38,756 function and then we'll give it a 15242 13:09:36,360 --> 13:09:42,040 parameter 15243 13:09:38,756 --> 13:09:44,680 product and now for each product here 15244 13:09:42,040 --> 13:09:48,640 we're going to check if the ID property 15245 13:09:44,680 --> 13:09:51,880 is equal to our product ID here so down 15246 13:09:48,640 --> 13:09:56,596 here we'll check if the 15247 13:09:51,880 --> 13:10:00,640 product. ID property is equal to our 15248 13:09:56,596 --> 13:10:03,240 product ID up here and then if they're 15249 13:10:00,640 --> 13:10:05,160 equal that means we found the matching 15250 13:10:03,240 --> 13:10:09,200 product and we're going to save it in 15251 13:10:05,160 --> 13:10:12,560 this variable so matching 15252 13:10:09,200 --> 13:10:13,480 product equals this product that We're 15253 13:10:12,560 --> 13:10:15,916 looping 15254 13:10:13,480 --> 13:10:18,756 through so now that we have the matching 15255 13:10:15,916 --> 13:10:21,240 product we have access to the image the 15256 13:10:18,756 --> 13:10:24,720 name as well as the price that we can 15257 13:10:21,240 --> 13:10:27,240 use to generate this HTML so this is a 15258 13:10:24,720 --> 13:10:30,276 good example of duplicating or 15259 13:10:27,240 --> 13:10:32,640 normalizing our data so now at the 15260 13:10:30,276 --> 13:10:35,436 bottom let's 15261 13:10:32,640 --> 13:10:39,276 console.log our matching product to see 15262 13:10:35,436 --> 13:10:41,956 if it works so matching 15263 13:10:39,276 --> 13:10:45,320 product and 15264 13:10:41,956 --> 13:10:49,596 save now if we go into our project and 15265 13:10:45,320 --> 13:10:52,160 we rightclick inspect and check our 15266 13:10:49,596 --> 13:10:54,720 console you can see that we were able to 15267 13:10:52,160 --> 13:10:58,480 get the full product details for each of 15268 13:10:54,720 --> 13:10:59,756 these products using the product ID so 15269 13:10:58,480 --> 13:11:02,880 now that we have the full product 15270 13:10:59,756 --> 13:11:06,080 details we can use this to generate all 15271 13:11:02,880 --> 13:11:06,080 this HTML 15272 13:11:07,756 --> 13:11:13,560 so down here instead of generating the 15273 13:11:11,400 --> 13:11:16,276 same image every time we're going to use 15274 13:11:13,560 --> 13:11:18,080 the image in the matching product so 15275 13:11:16,276 --> 13:11:22,116 let's get rid of this 15276 13:11:18,080 --> 13:11:24,040 code and here we're going to remove this 15277 13:11:22,116 --> 13:11:25,596 and we're going to substitute using 15278 13:11:24,040 --> 13:11:29,436 dollar curly 15279 13:11:25,596 --> 13:11:31,436 brackets the matching 15280 13:11:29,436 --> 13:11:34,796 product. 15281 13:11:31,436 --> 13:11:36,880 image and we'll scroll down and instead 15282 13:11:34,796 --> 13:11:39,240 of the same product name every time 15283 13:11:36,880 --> 13:11:42,320 we're going we're going to replace this 15284 13:11:39,240 --> 13:11:43,880 and we're going to substitute the 15285 13:11:42,320 --> 13:11:46,756 matching 15286 13:11:43,880 --> 13:11:49,400 product. name so each time we're going 15287 13:11:46,756 --> 13:11:51,916 to generate HTML with a different image 15288 13:11:49,400 --> 13:11:55,240 and a different name and down here the 15289 13:11:51,916 --> 13:12:00,000 same thing remove the price and we'll 15290 13:11:55,240 --> 13:12:01,560 replace it with matching product Dot and 15291 13:12:00,000 --> 13:12:05,916 then we'll just check here what the 15292 13:12:01,560 --> 13:12:07,880 property is it's called a price sense 15293 13:12:05,916 --> 13:12:10,480 and remember this is in 15294 13:12:07,880 --> 13:12:15,160 and to display it as dollars we have to 15295 13:12:10,480 --> 13:12:17,796 convert it by dividing by 100 and 15296 13:12:15,160 --> 13:12:21,160 save and lastly we're going to replace 15297 13:12:17,796 --> 13:12:23,840 the quantity here so if we scroll down 15298 13:12:21,160 --> 13:12:28,000 we can see that this is the quantity so 15299 13:12:23,840 --> 13:12:30,796 instead of two every time we'll 15300 13:12:28,000 --> 13:12:33,200 substitute and for the quantity remember 15301 13:12:30,796 --> 13:12:36,480 it's not part of a product it's part of 15302 13:12:33,200 --> 13:12:38,956 our cart so each cart item has a 15303 13:12:36,480 --> 13:12:43,796 property called called quantity so down 15304 13:12:38,956 --> 13:12:45,400 here we're going to substitute the cart 15305 13:12:43,796 --> 13:12:48,116 item. 15306 13:12:45,400 --> 13:12:51,320 quantity and 15307 13:12:48,116 --> 13:12:55,320 save so now we're generating all this 15308 13:12:51,320 --> 13:12:57,596 HTML by looping through our cart so the 15309 13:12:55,320 --> 13:13:00,360 next step is we're going to combine all 15310 13:12:57,596 --> 13:13:02,756 this HTML together and then put it on 15311 13:13:00,360 --> 13:13:06,040 the page so I'm just going to move this 15312 13:13:02,756 --> 13:13:09,000 up here so we can see more of the 15313 13:13:06,040 --> 13:13:11,840 code so to combine all this HTML 15314 13:13:09,000 --> 13:13:15,880 together let's create a variable up here 15315 13:13:11,840 --> 13:13:20,560 to store the result so we'll type let 15316 13:13:15,880 --> 13:13:25,000 and let's call it the cart summary 15317 13:13:20,560 --> 13:13:27,160 HTML equals an empty string and now each 15318 13:13:25,000 --> 13:13:29,956 time we Loop through the cart we're 15319 13:13:27,160 --> 13:13:34,200 going to add this HTML up here so that 15320 13:13:29,956 --> 13:13:35,756 we combine it so here we'll type cart 15321 13:13:34,200 --> 13:13:37,640 summary 15322 13:13:35,756 --> 13:13:40,796 HTML Plus 15323 13:13:37,640 --> 13:13:43,040 equals this HTML that we 15324 13:13:40,796 --> 13:13:44,796 generated and at the bottom I'll just 15325 13:13:43,040 --> 13:13:47,880 add a 15326 13:13:44,796 --> 13:13:49,080 semicolon and then let's console.log the 15327 13:13:47,880 --> 13:13:50,956 cart summary 15328 13:13:49,080 --> 13:13:55,956 HTML 15329 13:13:50,956 --> 13:13:59,276 console.log cart summary HTML to make 15330 13:13:55,956 --> 13:14:02,240 sure that it worked so we'll save and 15331 13:13:59,276 --> 13:14:04,276 then in here we'll check the console 15332 13:14:02,240 --> 13:14:07,916 right click 15333 13:14:04,276 --> 13:14:09,756 inspect the console 15334 13:14:07,916 --> 13:14:13,240 and now you can see that we generated 15335 13:14:09,756 --> 13:14:15,640 the HTML and each cart item is different 15336 13:14:13,240 --> 13:14:17,880 depending on the product that is being 15337 13:14:15,640 --> 13:14:20,640 displayed so that's how we generate all 15338 13:14:17,880 --> 13:14:23,200 the HTML for this section and the last 15339 13:14:20,640 --> 13:14:26,916 step is to take this and put it on the 15340 13:14:23,200 --> 13:14:29,560 page using the Dom so first we need an 15341 13:14:26,916 --> 13:14:34,400 element that we can put the HTML inside 15342 13:14:29,560 --> 13:14:38,200 of to find this element let's rightclick 15343 13:14:34,400 --> 13:14:39,680 inspect and click the top left left and 15344 13:14:38,200 --> 13:14:42,560 click one of these 15345 13:14:39,680 --> 13:14:45,160 products and let's find this order 15346 13:14:42,560 --> 13:14:47,840 summary element because this contains 15347 13:14:45,160 --> 13:14:50,680 both of our products here so we're going 15348 13:14:47,840 --> 13:14:53,276 to replace all the HTML inside here with 15349 13:14:50,680 --> 13:14:56,240 what we generated so let's search for 15350 13:14:53,276 --> 13:14:59,400 the class order summary in our code so 15351 13:14:56,240 --> 13:15:02,040 we'll go back to our code and let's open 15352 13:14:59,400 --> 13:15:04,956 checkout. 15353 13:15:02,040 --> 13:15:07,276 HTML and we'll type contrl f or command 15354 13:15:04,956 --> 13:15:09,276 F to search for code and we'll search 15355 13:15:07,276 --> 13:15:10,916 for the class 15356 13:15:09,276 --> 13:15:13,436 order- 15357 13:15:10,916 --> 13:15:16,000 summary and then it's right 15358 13:15:13,436 --> 13:15:20,520 here and we're going to add a class so 15359 13:15:16,000 --> 13:15:23,720 we can select it using the Dom so js- 15360 13:15:20,520 --> 13:15:26,436 order- summary and 15361 13:15:23,720 --> 13:15:29,436 save so now let's use the Dom and put 15362 13:15:26,436 --> 13:15:30,980 this element in our JavaScript so inside 15363 13:15:29,436 --> 13:15:32,116 here at the 15364 13:15:30,980 --> 13:15:34,596 [Music] 15365 13:15:32,116 --> 13:15:38,560 bottom we use 15366 13:15:34,596 --> 13:15:41,000 document. Query select VOR 15367 13:15:38,560 --> 13:15:44,040 brackets and the class we just 15368 13:15:41,000 --> 13:15:46,240 created js- 15369 13:15:44,040 --> 13:15:49,400 order- 15370 13:15:46,240 --> 13:15:51,756 summary and we'll use the property. 15371 13:15:49,400 --> 13:15:56,480 inner Capital 15372 13:15:51,756 --> 13:15:58,400 HTML and we'll change the HTML to cart 15373 13:15:56,480 --> 13:16:01,756 summary 15374 13:15:58,400 --> 13:16:04,080 HTML and we can remove this 15375 13:16:01,756 --> 13:16:06,640 line and now if we 15376 13:16:04,080 --> 13:16:09,320 save we're basically generating all this 15377 13:16:06,640 --> 13:16:12,436 h HTML with JavaScript and then putting 15378 13:16:09,320 --> 13:16:13,796 it on the page so now we can go back to 15379 13:16:12,436 --> 13:16:17,000 checkout. 15380 13:16:13,796 --> 13:16:19,796 HTML and we can actually remove the code 15381 13:16:17,000 --> 13:16:21,720 inside this order summary so we can 15382 13:16:19,796 --> 13:16:25,640 delete this 15383 13:16:21,720 --> 13:16:28,360 code and save and everything will still 15384 13:16:25,640 --> 13:16:30,320 work because this is now being generated 15385 13:16:28,360 --> 13:16:32,640 by 15386 13:16:30,320 --> 13:16:35,276 JavaScript so that's how we create the 15387 13:16:32,640 --> 13:16:38,596 cart section of this page with 15388 13:16:35,276 --> 13:16:41,436 JavaScript let's go into get and save 15389 13:16:38,596 --> 13:16:43,916 our changes so we'll open the git 15390 13:16:41,436 --> 13:16:48,040 section and then it will tell us that we 15391 13:16:43,916 --> 13:16:51,756 changed three files in checkout. HTML we 15392 13:16:48,040 --> 13:16:53,956 removed all the HTML that was written 15393 13:16:51,756 --> 13:16:56,240 manually and then at the bottom we 15394 13:16:53,956 --> 13:16:58,840 created a script tag to load our 15395 13:16:56,240 --> 13:17:02,040 JavaScript and then we also changed 15396 13:16:58,840 --> 13:17:04,480 cart. JS and added some default values 15397 13:17:02,040 --> 13:17:07,160 to work with and then we created 15398 13:17:04,480 --> 13:17:10,880 checkout. JS which loops through the 15399 13:17:07,160 --> 13:17:13,480 cart creates the HTML and puts it on the 15400 13:17:10,880 --> 13:17:14,560 page so let's create a message to 15401 13:17:13,480 --> 13:17:17,596 describe our 15402 13:17:14,560 --> 13:17:24,640 changes generate 15403 13:17:17,596 --> 13:17:24,640 HTML for the order summary and 15404 13:17:27,360 --> 13:17:34,160 commit next let's open our project 15405 13:17:31,000 --> 13:17:36,956 again and before we continue we actually 15406 13:17:34,160 --> 13:17:39,040 have two issues that we need to fix the 15407 13:17:36,956 --> 13:17:41,360 first issue is that the price is 15408 13:17:39,040 --> 13:17:44,480 displaying as one decimal here instead 15409 13:17:41,360 --> 13:17:46,520 of two and the second issue is that 15410 13:17:44,480 --> 13:17:49,320 these delivery options are actually 15411 13:17:46,520 --> 13:17:51,596 shared between the two products for 15412 13:17:49,320 --> 13:17:54,720 example if I click this delivery option 15413 13:17:51,596 --> 13:17:57,160 here it will actually unselect for the 15414 13:17:54,720 --> 13:17:58,436 second product so they're sort of shared 15415 13:17:57,160 --> 13:18:00,956 between the two 15416 13:17:58,436 --> 13:18:03,160 products but what we actually want is 15417 13:18:00,956 --> 13:18:05,596 for each product to be able to select 15418 13:18:03,160 --> 13:18:07,520 its own delivery options so we're going 15419 13:18:05,596 --> 13:18:11,040 to fix these two issues 15420 13:18:07,520 --> 13:18:13,040 next first let's focus on the price and 15421 13:18:11,040 --> 13:18:15,560 we want to display it in two 15422 13:18:13,040 --> 13:18:18,560 decimals so we actually learn how to fix 15423 13:18:15,560 --> 13:18:20,520 this on the homepage using the method to 15424 13:18:18,560 --> 13:18:24,436 fixed so we're going to use the same 15425 13:18:20,520 --> 13:18:28,040 code here let's go into our 15426 13:18:24,436 --> 13:18:30,560 code and we'll open our files and let's 15427 13:18:28,040 --> 13:18:33,240 open checkout. JS 15428 13:18:30,560 --> 13:18:35,000 again so here we're going to scroll down 15429 13:18:33,240 --> 13:18:38,840 to where we display the 15430 13:18:35,000 --> 13:18:41,400 price which is here price sense and to 15431 13:18:38,840 --> 13:18:43,320 show this in two decimal places we're 15432 13:18:41,400 --> 13:18:45,160 going to surround this with brackets to 15433 13:18:43,320 --> 13:18:48,276 make sure we calculate it 15434 13:18:45,160 --> 13:18:53,080 first and then use the method 15435 13:18:48,276 --> 13:18:57,276 do2 fixed brackets and then two to show 15436 13:18:53,080 --> 13:18:59,000 two decimal places if we save and then 15437 13:18:57,276 --> 13:19:02,276 open our 15438 13:18:59,000 --> 13:19:04,596 project now the price is displaying 15439 13:19:02,276 --> 13:19:07,000 correctly before we move on we're going 15440 13:19:04,596 --> 13:19:09,240 to learn how to better organize this 15441 13:19:07,000 --> 13:19:11,756 code so if we go 15442 13:19:09,240 --> 13:19:14,916 back so notice that we're using this 15443 13:19:11,756 --> 13:19:19,160 code inside the checkout page as well as 15444 13:19:14,916 --> 13:19:21,160 inside amazon. JS which is the homepage 15445 13:19:19,160 --> 13:19:23,116 so we can actually share this code 15446 13:19:21,160 --> 13:19:26,596 between the two files so we don't have 15447 13:19:23,116 --> 13:19:30,756 to repeat it and to do that we can use 15448 13:19:26,596 --> 13:19:32,360 modules so let's open checkout. JS again 15449 13:19:30,756 --> 13:19:34,360 and what we'll do is we're going to 15450 13:19:32,360 --> 13:19:37,116 create a function that does the same 15451 13:19:34,360 --> 13:19:40,360 thing as this code and then we can share 15452 13:19:37,116 --> 13:19:42,160 this function between the two files so 15453 13:19:40,360 --> 13:19:45,080 let's create a file that's going to 15454 13:19:42,160 --> 13:19:47,720 contain this shared function so we're 15455 13:19:45,080 --> 13:19:50,160 going to go to our scripts folder here 15456 13:19:47,720 --> 13:19:53,080 and inside we're going to create a new 15457 13:19:50,160 --> 13:19:54,956 folder this time and let's call this 15458 13:19:53,080 --> 13:19:57,360 folder 15459 13:19:54,956 --> 13:20:00,276 utils and press 15460 13:19:57,360 --> 13:20:02,520 enter so we're naming it utils because 15461 13:20:00,276 --> 13:20:04,360 this code is sort of like a useful 15462 13:20:02,520 --> 13:20:07,320 utility that we want to 15463 13:20:04,360 --> 13:20:10,200 share now inside this folder we're going 15464 13:20:07,320 --> 13:20:11,720 to have different types of utils so 15465 13:20:10,200 --> 13:20:17,480 let's create a new 15466 13:20:11,720 --> 13:20:20,116 file and let's name it money. JJs so 15467 13:20:17,480 --> 13:20:22,596 this file is going to contain utilities 15468 13:20:20,116 --> 13:20:25,880 that are related to money which is this 15469 13:20:22,596 --> 13:20:28,320 code right here so let's press 15470 13:20:25,880 --> 13:20:32,436 enter and I'm just going to move this to 15471 13:20:28,320 --> 13:20:32,436 the bottom so we can see it side by 15472 13:20:33,160 --> 13:20:38,436 side so now inside this UIL file we're 15473 13:20:36,756 --> 13:20:40,916 going to create a function that does the 15474 13:20:38,436 --> 13:20:42,880 same thing as here and then we can share 15475 13:20:40,916 --> 13:20:47,640 it between the two 15476 13:20:42,880 --> 13:20:51,640 files so here we'll create a function 15477 13:20:47,640 --> 13:20:54,160 and let's name it format currency 15478 13:20:51,640 --> 13:20:56,720 because that's what this code is doing 15479 13:20:54,160 --> 13:20:59,240 and then brackets and curly 15480 13:20:56,720 --> 13:21:02,320 brackets so this function is basically 15481 13:20:59,240 --> 13:21:05,080 going to do the same thing as here so 15482 13:21:02,320 --> 13:21:07,276 we're going to add a parameter here 15483 13:21:05,080 --> 13:21:09,360 price sense 15484 13:21:07,276 --> 13:21:11,400 because we want to use this function for 15485 13:21:09,360 --> 13:21:14,200 any amount of price 15486 13:21:11,400 --> 13:21:18,916 sents and then in here we're going to do 15487 13:21:14,200 --> 13:21:21,680 the same thing which is price sents 15488 13:21:18,916 --> 13:21:23,720 divided 100 and then we're going to 15489 13:21:21,680 --> 13:21:25,276 surround this with brackets to make sure 15490 13:21:23,720 --> 13:21:28,116 we calculate this 15491 13:21:25,276 --> 13:21:33,520 first and then display it in two decimal 15492 13:21:28,116 --> 13:21:38,520 places using dot 2 fixed brackets and 15493 13:21:33,520 --> 13:21:38,520 two and finally let's return this result 15494 13:21:38,560 --> 13:21:44,480 return and 15495 13:21:41,000 --> 13:21:46,436 save so what we just did is we created a 15496 13:21:44,480 --> 13:21:48,400 function that does the same thing as 15497 13:21:46,436 --> 13:21:51,480 this code here and now we're going to 15498 13:21:48,400 --> 13:21:55,116 share this function between checkout. JS 15499 13:21:51,480 --> 13:21:58,360 and amazon. JS so first in order to 15500 13:21:55,116 --> 13:22:01,240 share it we need to export this function 15501 13:21:58,360 --> 13:22:03,276 so at the front we're going to type 15502 13:22:01,240 --> 13:22:06,040 export and 15503 13:22:03,276 --> 13:22:08,160 save and now inside each of these files 15504 13:22:06,040 --> 13:22:12,320 we're going to to import this function 15505 13:22:08,160 --> 13:22:16,520 and use it so let's scroll to the 15506 13:22:12,320 --> 13:22:19,400 top and here we're going to import curly 15507 13:22:16,520 --> 13:22:21,956 brackets we're going to import the 15508 13:22:19,400 --> 13:22:24,560 function format 15509 13:22:21,956 --> 13:22:27,320 currency and we're going to import it 15510 13:22:24,560 --> 13:22:29,956 from single quotes and 15511 13:22:27,320 --> 13:22:33,720 semicolon and then inside here we're 15512 13:22:29,956 --> 13:22:36,160 going to locate the file money. JS so 15513 13:22:33,720 --> 13:22:39,116 here we're in the checkout. JS file 15514 13:22:36,160 --> 13:22:42,160 which is right here and to go here we're 15515 13:22:39,116 --> 13:22:45,360 first going to go into the UTS folder so 15516 13:22:42,160 --> 13:22:48,000 we're going to Type U toils and then a 15517 13:22:45,360 --> 13:22:51,680 slash to go into this folder and we're 15518 13:22:48,000 --> 13:22:53,520 going to locate the file money. JS so 15519 13:22:51,680 --> 13:22:56,560 money. 15520 13:22:53,520 --> 13:22:59,320 JS now for modules we have to do one 15521 13:22:56,560 --> 13:23:02,240 extra thing at the front of this file 15522 13:22:59,320 --> 13:23:05,320 path we need to type dot 15523 13:23:02,240 --> 13:23:07,240 slash so remember that dot dot 15524 13:23:05,320 --> 13:23:10,720 represents the folder outside the 15525 13:23:07,240 --> 13:23:12,756 current folder so for checkout. JS if we 15526 13:23:10,720 --> 13:23:14,680 go outside of this folder we're going to 15527 13:23:12,756 --> 13:23:17,320 go outside the scripts 15528 13:23:14,680 --> 13:23:20,200 folder now a single dot on the other 15529 13:23:17,320 --> 13:23:22,000 hand means the current folder so this 15530 13:23:20,200 --> 13:23:24,680 dot means we're going to go into the 15531 13:23:22,000 --> 13:23:26,756 current folder which is the scripts 15532 13:23:24,680 --> 13:23:29,080 folder and then we're going to go into 15533 13:23:26,756 --> 13:23:32,520 the utils folder and then we're going to 15534 13:23:29,080 --> 13:23:35,756 go and locate money. 15535 13:23:32,520 --> 13:23:38,200 JS so for modules if we want to start in 15536 13:23:35,756 --> 13:23:41,560 the current f folder our file paths have 15537 13:23:38,200 --> 13:23:44,160 to start with Slash so that's one extra 15538 13:23:41,560 --> 13:23:46,320 thing that we have to do so now that 15539 13:23:44,160 --> 13:23:48,916 we've imported the format currency 15540 13:23:46,320 --> 13:23:51,680 function let's use it in the rest of our 15541 13:23:48,916 --> 13:23:55,116 code so we're going to scroll 15542 13:23:51,680 --> 13:23:57,116 down to here and instead of writing this 15543 13:23:55,116 --> 13:24:00,640 all over again we're just going to use 15544 13:23:57,116 --> 13:24:03,596 the function format 15545 13:24:00,640 --> 13:24:05,360 currency and then brackets and we're 15546 13:24:03,596 --> 13:24:10,436 going to remove the rest of the code 15547 13:24:05,360 --> 13:24:13,040 here and and close bracket and 15548 13:24:10,436 --> 13:24:15,560 save so we're basically running the same 15549 13:24:13,040 --> 13:24:18,880 code except we're using this shared 15550 13:24:15,560 --> 13:24:20,436 function that's located in another file 15551 13:24:18,880 --> 13:24:23,240 so let's open our 15552 13:24:20,436 --> 13:24:24,680 project and we can see that the price is 15553 13:24:23,240 --> 13:24:26,880 still displaying 15554 13:24:24,680 --> 13:24:28,796 correctly so now let's go back into our 15555 13:24:26,880 --> 13:24:32,480 code and we're going to share this 15556 13:24:28,796 --> 13:24:34,720 function inside amazon. JS as well so 15557 13:24:32,480 --> 13:24:38,000 let's open amazon. 15558 13:24:34,720 --> 13:24:40,276 JS and then we'll scroll to where we 15559 13:24:38,000 --> 13:24:42,756 show the price which is here and we're 15560 13:24:40,276 --> 13:24:44,840 going to use this function again so 15561 13:24:42,756 --> 13:24:49,720 first we have to import it so we'll 15562 13:24:44,840 --> 13:24:53,200 scroll up to the top and import curly 15563 13:24:49,720 --> 13:24:54,956 brackets we'll import the function 15564 13:24:53,200 --> 13:24:59,240 format 15565 13:24:54,956 --> 13:25:02,160 currency and we'll import it from single 15566 13:24:59,240 --> 13:25:05,560 quotes semicolon and then here we'll 15567 13:25:02,160 --> 13:25:07,956 locate the file money. JS so remember 15568 13:25:05,560 --> 13:25:12,160 for module if we want to start in the 15569 13:25:07,956 --> 13:25:14,956 current folder we have to type slash and 15570 13:25:12,160 --> 13:25:18,680 then we're going to go to the UIL folder 15571 13:25:14,956 --> 13:25:21,320 so utils and then a slash to go inside 15572 13:25:18,680 --> 13:25:24,160 the folder and locate the 15573 13:25:21,320 --> 13:25:26,360 file money. 15574 13:25:24,160 --> 13:25:29,160 JS and now we're going to use this 15575 13:25:26,360 --> 13:25:31,400 function so let's scroll 15576 13:25:29,160 --> 13:25:36,436 down and we're going to replace this 15577 13:25:31,400 --> 13:25:38,720 code and use format currency and 15578 13:25:36,436 --> 13:25:41,680 brackets so we can remove the rest of 15579 13:25:38,720 --> 13:25:44,640 the code here and 15580 13:25:41,680 --> 13:25:47,200 save and now let's look at our 15581 13:25:44,640 --> 13:25:48,360 project and we'll click up here to go 15582 13:25:47,200 --> 13:25:50,880 back to the 15583 13:25:48,360 --> 13:25:53,840 homepage and you can see that the prices 15584 13:25:50,880 --> 13:25:56,680 are displaying as two decimal 15585 13:25:53,840 --> 13:25:59,756 places and that's it if we look at our 15586 13:25:56,680 --> 13:26:03,040 code again we're essentially taking this 15587 13:25:59,756 --> 13:26:06,320 function and sharing it across two files 15588 13:26:03,040 --> 13:26:10,200 using modules so using modules we can re 15589 13:26:06,320 --> 13:26:12,916 use a lot of our code and reduce 15590 13:26:10,200 --> 13:26:15,520 repetition next let's work on the second 15591 13:26:12,916 --> 13:26:18,276 issue that we saw earlier so we'll go 15592 13:26:15,520 --> 13:26:21,160 back to our project and click the top 15593 13:26:18,276 --> 13:26:23,756 right to go back to the checkout page 15594 13:26:21,160 --> 13:26:26,680 and then here the delivery options are 15595 13:26:23,756 --> 13:26:28,720 shared between the two products but we 15596 13:26:26,680 --> 13:26:30,320 want each product to choose its own 15597 13:26:28,720 --> 13:26:33,000 delivery 15598 13:26:30,320 --> 13:26:36,276 options so to fix that let's go back 15599 13:26:33,000 --> 13:26:39,640 into our code and just going to resize a 15600 13:26:36,276 --> 13:26:43,796 here so we can see it side by 15601 13:26:39,640 --> 13:26:47,040 side and we'll open checkout. JS again 15602 13:26:43,796 --> 13:26:47,040 and we can close this for 15603 13:26:48,240 --> 13:26:55,200 now so this is an HTML element called a 15604 13:26:52,160 --> 13:26:58,116 radial selector and here's the code for 15605 13:26:55,200 --> 13:27:01,200 a radial selector so the way that radial 15606 13:26:58,116 --> 13:27:03,720 selectors work is that if a set of radio 15607 13:27:01,200 --> 13:27:06,880 selectors have the same name then we can 15608 13:27:03,720 --> 13:27:08,916 only select one of them so for example 15609 13:27:06,880 --> 13:27:11,436 if these three radio selectors have the 15610 13:27:08,916 --> 13:27:14,436 same name attribute then we can only 15611 13:27:11,436 --> 13:27:16,916 select one of them now if the three at 15612 13:27:14,436 --> 13:27:19,320 the top have one name and the three at 15613 13:27:16,916 --> 13:27:21,880 the bottom have a different name then we 15614 13:27:19,320 --> 13:27:25,276 can select one from the first three as 15615 13:27:21,880 --> 13:27:27,640 well as one from the second 15616 13:27:25,276 --> 13:27:30,720 three now what's the problem with the 15617 13:27:27,640 --> 13:27:33,596 radial selectors here well let's take a 15618 13:27:30,720 --> 13:27:35,436 look at the code for these selectors to 15619 13:27:33,596 --> 13:27:38,320 do that we're going to rightclick one of 15620 13:27:35,436 --> 13:27:41,916 them and then 15621 13:27:38,320 --> 13:27:43,840 inspect and then click the top left and 15622 13:27:41,916 --> 13:27:47,320 click one of these selectors to get the 15623 13:27:43,840 --> 13:27:49,756 HTML code so here we have the code for 15624 13:27:47,320 --> 13:27:52,080 the radio selector and you can see that 15625 13:27:49,756 --> 13:27:54,796 the name of the selector is delivery 15626 13:27:52,080 --> 13:27:58,276 option one and now if we click the top 15627 13:27:54,796 --> 13:28:00,560 left again and find one of these bottom 15628 13:27:58,276 --> 13:28:03,956 selectors you'll notice that these also 15629 13:28:00,560 --> 13:28:06,080 have the name delivery option one so all 15630 13:28:03,956 --> 13:28:08,400 six of these have the same name at 15631 13:28:06,080 --> 13:28:11,796 attribute that's why we can only select 15632 13:28:08,400 --> 13:28:14,276 one of these six at a time so what we 15633 13:28:11,796 --> 13:28:17,040 actually want to do is we want the first 15634 13:28:14,276 --> 13:28:19,000 three selectors to have one name and the 15635 13:28:17,040 --> 13:28:21,640 second three selectors to have a 15636 13:28:19,000 --> 13:28:25,040 different name so let's figure out how 15637 13:28:21,640 --> 13:28:27,956 to give these a different name so inside 15638 13:28:25,040 --> 13:28:30,840 the console we can see the code for this 15639 13:28:27,956 --> 13:28:32,720 selector and we have a name attribute 15640 13:28:30,840 --> 13:28:35,320 delivery D 15641 13:28:32,720 --> 13:28:38,480 option-1 so let's actually find this 15642 13:28:35,320 --> 13:28:41,756 name in our code so we'll go to our code 15643 13:28:38,480 --> 13:28:45,756 editor and contrl F or command F to 15644 13:28:41,756 --> 13:28:49,520 search for code and we'll search for 15645 13:28:45,756 --> 13:28:52,160 delivery Das 15646 13:28:49,520 --> 13:28:54,720 option-1 and if we scroll 15647 13:28:52,160 --> 13:28:57,116 down you'll notice that every time we're 15648 13:28:54,720 --> 13:28:59,360 generating these radio selectors we're 15649 13:28:57,116 --> 13:29:02,400 always using the same name delivery 15650 13:28:59,360 --> 13:29:04,200 option one so every product is going to 15651 13:29:02,400 --> 13:29:06,320 have the same name for the radio 15652 13:29:04,200 --> 13:29:09,640 selectors and all of these are going to 15653 13:29:06,320 --> 13:29:11,796 be shared so what we actually want to do 15654 13:29:09,640 --> 13:29:14,840 is instead of using the name delivery 15655 13:29:11,796 --> 13:29:17,000 option one every time each product is 15656 13:29:14,840 --> 13:29:17,756 going to use a different name for these 15657 13:29:17,000 --> 13:29:20,840 three 15658 13:29:17,756 --> 13:29:23,436 selectors so one way we can do that is 15659 13:29:20,840 --> 13:29:26,080 instead of the number one every time we 15660 13:29:23,436 --> 13:29:28,880 can replace it with the product's ID 15661 13:29:26,080 --> 13:29:32,200 because the ID is always unique for each 15662 13:29:28,880 --> 13:29:33,560 product so let's substitute using dollar 15663 13:29:32,200 --> 13:29:37,116 and curly 15664 13:29:33,560 --> 13:29:40,360 brackets the product 15665 13:29:37,116 --> 13:29:42,360 ID and then down here as well instead of 15666 13:29:40,360 --> 13:29:43,596 using the name one every time we're 15667 13:29:42,360 --> 13:29:46,400 going to 15668 13:29:43,596 --> 13:29:48,956 substitute 15669 13:29:46,400 --> 13:29:52,720 product. and 15670 13:29:48,956 --> 13:29:56,000 actually we use the name matching 15671 13:29:52,720 --> 13:29:59,956 product so we'll scroll down and just 15672 13:29:56,000 --> 13:30:04,756 update this to matching 15673 13:29:59,956 --> 13:30:07,796 product and then down here matching 15674 13:30:04,756 --> 13:30:09,756 product and and as well down here 15675 13:30:07,796 --> 13:30:15,640 instead of one every 15676 13:30:09,756 --> 13:30:17,480 time substitute the matching product. ID 15677 13:30:15,640 --> 13:30:21,040 and 15678 13:30:17,480 --> 13:30:23,520 save so now these three radial selectors 15679 13:30:21,040 --> 13:30:26,160 will have a different name attribute 15680 13:30:23,520 --> 13:30:28,436 than these three radial selectors that 15681 13:30:26,160 --> 13:30:33,436 means we can select one out of these 15682 13:30:28,436 --> 13:30:36,520 three and we can select one out of these 15683 13:30:33,436 --> 13:30:38,756 three so that's how we fix that error 15684 13:30:36,520 --> 13:30:40,796 instead of using the same name delivery 15685 13:30:38,756 --> 13:30:44,436 option one every time we're going to 15686 13:30:40,796 --> 13:30:46,916 generate a name using the product's ID 15687 13:30:44,436 --> 13:30:51,276 so let's go into get and save our 15688 13:30:46,916 --> 13:30:53,880 changes so in amazon. JS we imported 15689 13:30:51,276 --> 13:30:56,956 this shared function and we used it to 15690 13:30:53,880 --> 13:30:59,956 format the price in two decimal places 15691 13:30:56,956 --> 13:31:01,680 and then in checkout. JS we also used 15692 13:30:59,956 --> 13:31:06,240 the shared function 15693 13:31:01,680 --> 13:31:08,720 here and we fix the radio selectors 15694 13:31:06,240 --> 13:31:10,680 and then in this file 15695 13:31:08,720 --> 13:31:13,116 moneyjar 15696 13:31:10,680 --> 13:31:17,796 function so let's create a message to 15697 13:31:13,116 --> 13:31:21,680 describe our changes fix price 15698 13:31:17,796 --> 13:31:25,040 formatting and radio 15699 13:31:21,680 --> 13:31:25,040 selectors and 15700 13:31:28,160 --> 13:31:33,000 commit the last step in our process is 15701 13:31:30,756 --> 13:31:35,040 to make it 15702 13:31:33,000 --> 13:31:37,756 interactive so I'm just going to close 15703 13:31:35,040 --> 13:31:40,000 these for now 15704 13:31:37,756 --> 13:31:42,796 and going to move it to the right again 15705 13:31:40,000 --> 13:31:42,796 so we can see our 15706 13:31:46,880 --> 13:31:51,160 project and for the order summary 15707 13:31:49,400 --> 13:31:53,596 there's a few things we can make 15708 13:31:51,160 --> 13:31:56,320 interactive for now we're going to start 15709 13:31:53,596 --> 13:31:58,720 with this delete link when we click this 15710 13:31:56,320 --> 13:32:00,680 link it should remove this product from 15711 13:31:58,720 --> 13:32:01,756 the cart so we're going to learn how to 15712 13:32:00,680 --> 13:32:05,360 build 15713 13:32:01,756 --> 13:32:08,080 this so let's go back to our code and 15714 13:32:05,360 --> 13:32:11,116 first let's add event listeners to all 15715 13:32:08,080 --> 13:32:12,956 of these delete links to do that we're 15716 13:32:11,116 --> 13:32:17,000 going to find the code for these delete 15717 13:32:12,956 --> 13:32:19,880 links so we'll rightclick 15718 13:32:17,000 --> 13:32:21,840 inspect and then in here this is the 15719 13:32:19,880 --> 13:32:26,160 code so we're going to search for this 15720 13:32:21,840 --> 13:32:30,436 class in our code editor delete quantity 15721 13:32:26,160 --> 13:32:33,240 dlink so in here let's open checkout. JS 15722 13:32:30,436 --> 13:32:38,000 and we're going to contrl f or command F 15723 13:32:33,240 --> 13:32:40,160 and search for delete - quantity 15724 13:32:38,000 --> 13:32:43,080 dlink which is right 15725 13:32:40,160 --> 13:32:45,276 here next we're going to add a class to 15726 13:32:43,080 --> 13:32:48,320 these elements so we can add event 15727 13:32:45,276 --> 13:32:50,000 listeners so at the end here let's add a 15728 13:32:48,320 --> 13:32:52,720 class 15729 13:32:50,000 --> 13:32:55,436 js- delete 15730 13:32:52,720 --> 13:32:58,240 daslink and 15731 13:32:55,436 --> 13:33:01,040 save and then at the bottom we're going 15732 13:32:58,240 --> 13:33:02,796 to scroll down and then out here we're 15733 13:33:01,040 --> 13:33:05,480 going to add the event 15734 13:33:02,796 --> 13:33:09,796 listeners so first we're going to select 15735 13:33:05,480 --> 13:33:15,480 all the delete links on the page using 15736 13:33:09,796 --> 13:33:17,436 document. query selector all brackets 15737 13:33:15,480 --> 13:33:20,840 and then a string and we're going to 15738 13:33:17,436 --> 13:33:24,520 select a class using Dot and the class 15739 13:33:20,840 --> 13:33:26,596 js- delete 15740 13:33:24,520 --> 13:33:31,520 daslink and then we're going to Loop 15741 13:33:26,596 --> 13:33:34,480 through all these links so dot for each 15742 13:33:31,520 --> 13:33:37,320 brackets and a 15743 13:33:34,480 --> 13:33:40,400 function and and in here it will give us 15744 13:33:37,320 --> 13:33:42,360 the link every time and then for each of 15745 13:33:40,400 --> 13:33:44,400 these links we're going to add an event 15746 13:33:42,360 --> 13:33:46,040 listener so 15747 13:33:44,400 --> 13:33:47,880 link. 15748 13:33:46,040 --> 13:33:49,400 addevent 15749 13:33:47,880 --> 13:33:51,520 listener 15750 13:33:49,400 --> 13:33:53,520 brackets and here we're going to give a 15751 13:33:51,520 --> 13:33:56,000 string and the event we want to listen 15752 13:33:53,520 --> 13:33:58,436 for which is a 15753 13:33:56,000 --> 13:34:01,956 click and then a function that we want 15754 13:33:58,436 --> 13:34:04,640 to run when we click these links so for 15755 13:34:01,956 --> 13:34:07,480 now let's just 15756 13:34:04,640 --> 13:34:10,276 console.log string delete to make sure 15757 13:34:07,480 --> 13:34:13,480 that it's working so we'll save 15758 13:34:10,276 --> 13:34:16,640 this and then in here we click these 15759 13:34:13,480 --> 13:34:19,040 delete links and then open the console 15760 13:34:16,640 --> 13:34:20,796 right click 15761 13:34:19,040 --> 13:34:24,240 inspect 15762 13:34:20,796 --> 13:34:27,320 console we can see that it's working now 15763 13:34:24,240 --> 13:34:27,320 so now let's go back to our 15764 13:34:27,840 --> 13:34:33,080 code so when we click delete we're going 15765 13:34:30,436 --> 13:34:34,680 to do two steps the first step is we're 15766 13:34:33,080 --> 13:34:37,360 going to remove the product from the 15767 13:34:34,680 --> 13:34:40,436 cart and the second step is we're also 15768 13:34:37,360 --> 13:34:43,040 going to update our HTML and remove the 15769 13:34:40,436 --> 13:34:45,480 product from the page so let's start 15770 13:34:43,040 --> 13:34:48,080 with the first step so when we click 15771 13:34:45,480 --> 13:34:50,520 delete how do we know which product we 15772 13:34:48,080 --> 13:34:53,080 need to remove from the 15773 13:34:50,520 --> 13:34:55,840 cart so we learned how to do this for 15774 13:34:53,080 --> 13:34:59,560 the added to cart button we can attach 15775 13:34:55,840 --> 13:35:01,956 the product's ID to the link element so 15776 13:34:59,560 --> 13:35:06,116 let's scroll up again to where the 15777 13:35:01,956 --> 13:35:08,520 delete link is which is right here and 15778 13:35:06,116 --> 13:35:12,080 we're going to attach the products ID to 15779 13:35:08,520 --> 13:35:15,320 this element using a data attribute so 15780 13:35:12,080 --> 13:35:17,956 at the end here we're going to add 15781 13:35:15,320 --> 13:35:21,400 data- 15782 13:35:17,956 --> 13:35:23,840 product-id equals double quotes and then 15783 13:35:21,400 --> 13:35:27,520 here we'll 15784 13:35:23,840 --> 13:35:30,840 substitute the matching 15785 13:35:27,520 --> 13:35:33,040 product. ID and 15786 13:35:30,840 --> 13:35:35,880 save and now that we attached the 15787 13:35:33,040 --> 13:35:40,436 product ID to this element we can get it 15788 13:35:35,880 --> 13:35:43,680 in our JavaScript below so let's scroll 15789 13:35:40,436 --> 13:35:47,160 down and then here we're going to delete 15790 13:35:43,680 --> 13:35:51,640 this line and using the link element we 15791 13:35:47,160 --> 13:35:54,956 can get that product ID using link. dat 15792 13:35:51,640 --> 13:35:58,360 set to access the data attributes and 15793 13:35:54,956 --> 13:36:01,200 we're going to access product 15794 13:35:58,360 --> 13:36:04,880 ID and let's save this in a variable so 15795 13:36:01,200 --> 13:36:07,040 we can use it later so const product ID 15796 13:36:04,880 --> 13:36:09,400 equals this 15797 13:36:07,040 --> 13:36:13,116 value and let's 15798 13:36:09,400 --> 13:36:15,116 console.log this to see if it 15799 13:36:13,116 --> 13:36:18,520 works and 15800 13:36:15,116 --> 13:36:20,520 save so now if we go to the project and 15801 13:36:18,520 --> 13:36:23,200 click the delete link for the first 15802 13:36:20,520 --> 13:36:26,560 product and then look at the console 15803 13:36:23,200 --> 13:36:27,796 inspect and console it will show us the 15804 13:36:26,560 --> 13:36:30,520 products 15805 13:36:27,796 --> 13:36:33,436 ID if we click the second products 15806 13:36:30,520 --> 13:36:36,916 delete link it will show us the second 15807 13:36:33,436 --> 13:36:39,916 products ID so now now we have the ID 15808 13:36:36,916 --> 13:36:42,596 that we need to remove from the cart so 15809 13:36:39,916 --> 13:36:45,200 now how do we actually do that so before 15810 13:36:42,596 --> 13:36:47,400 we begin figuring that out instead of 15811 13:36:45,200 --> 13:36:51,560 writing all the code down here let's 15812 13:36:47,400 --> 13:36:54,240 actually create a function inside C.J to 15813 13:36:51,560 --> 13:36:57,360 do this task the reason we're going to 15814 13:36:54,240 --> 13:36:59,840 do this is because cart. JS is supposed 15815 13:36:57,360 --> 13:37:02,000 to contain all the code that's related 15816 13:36:59,840 --> 13:37:04,916 to the cart so this will help us 15817 13:37:02,000 --> 13:37:07,040 organize our code better so let's open 15818 13:37:04,916 --> 13:37:09,400 our files again again and we're going to 15819 13:37:07,040 --> 13:37:12,320 open cart. 15820 13:37:09,400 --> 13:37:14,560 JS and we're going to scroll to the 15821 13:37:12,320 --> 13:37:17,480 bottom and we're going to create a 15822 13:37:14,560 --> 13:37:21,320 function for removing a product from the 15823 13:37:17,480 --> 13:37:26,596 cart so let's type function and let's 15824 13:37:21,320 --> 13:37:27,680 name it remove from cart brackets and 15825 13:37:26,596 --> 13:37:30,436 curly 15826 13:37:27,680 --> 13:37:33,320 brackets so this function we'll take a 15827 13:37:30,436 --> 13:37:35,916 product ID which we have and remove it 15828 13:37:33,320 --> 13:37:40,400 from the cart so here let's let's create 15829 13:37:35,916 --> 13:37:42,436 a parameter for the ID product ID and 15830 13:37:40,400 --> 13:37:45,756 now in here we're going to figure out 15831 13:37:42,436 --> 13:37:49,276 how to remove it so here's one way we 15832 13:37:45,756 --> 13:37:51,956 can do this first we can create a new 15833 13:37:49,276 --> 13:37:54,956 array and then we can Loop through our 15834 13:37:51,956 --> 13:37:59,160 cart and then add each product to the 15835 13:37:54,956 --> 13:38:02,436 new array except for this product ID so 15836 13:37:59,160 --> 13:38:04,596 now let's convert these steps into code 15837 13:38:02,436 --> 13:38:08,200 so inside this function here let's 15838 13:38:04,596 --> 13:38:13,480 create a new new array using const we'll 15839 13:38:08,200 --> 13:38:15,880 call it new cart equals an empty 15840 13:38:13,480 --> 13:38:19,000 array the next step is we're going to 15841 13:38:15,880 --> 13:38:24,796 Loop through our cart so we'll type 15842 13:38:19,000 --> 13:38:27,680 cart. for each brackets and a 15843 13:38:24,796 --> 13:38:31,400 function and we'll give it a parameter 15844 13:38:27,680 --> 13:38:33,680 cart item and the last step is that 15845 13:38:31,400 --> 13:38:36,796 we're going to add each card item to the 15846 13:38:33,680 --> 13:38:41,080 new array except if it has this product 15847 13:38:36,796 --> 13:38:42,640 ID so we can do that by checking if the 15848 13:38:41,080 --> 13:38:48,276 cart 15849 13:38:42,640 --> 13:38:50,720 item. product ID is not equal to the 15850 13:38:48,276 --> 13:38:52,596 product ID that we're looking for 15851 13:38:50,720 --> 13:38:55,680 product 15852 13:38:52,596 --> 13:38:57,436 ID so if it's not equal to the product 15853 13:38:55,680 --> 13:39:03,116 we're trying to remove we're going to 15854 13:38:57,436 --> 13:39:06,116 add it to the new cart so new cart. push 15855 13:39:03,116 --> 13:39:08,436 cart item 15856 13:39:06,116 --> 13:39:11,400 so the result of this Loop is it's going 15857 13:39:08,436 --> 13:39:13,756 to contain all the cart items that don't 15858 13:39:11,400 --> 13:39:16,000 match this product ID and that's the 15859 13:39:13,756 --> 13:39:19,160 same thing as removing this from the 15860 13:39:16,000 --> 13:39:21,756 cart so now the last step is we're going 15861 13:39:19,160 --> 13:39:24,640 to take our new cart and replace the 15862 13:39:21,756 --> 13:39:28,756 cart up here so we'll scroll 15863 13:39:24,640 --> 13:39:31,956 down and then here we'll make cart equal 15864 13:39:28,756 --> 13:39:34,360 to our new 15865 13:39:31,956 --> 13:39:37,720 cart because we're reassigning this 15866 13:39:34,360 --> 13:39:42,480 variable we have to scroll up and change 15867 13:39:37,720 --> 13:39:45,200 it from const to let and 15868 13:39:42,480 --> 13:39:48,360 save so now that we created this 15869 13:39:45,200 --> 13:39:50,756 function let's use it in our checkout 15870 13:39:48,360 --> 13:39:54,956 page first we're going to use the 15871 13:39:50,756 --> 13:39:56,080 modules feature and Export this function 15872 13:39:54,956 --> 13:39:59,040 and 15873 13:39:56,080 --> 13:40:01,400 save and then inside checkout. JS we're 15874 13:39:59,040 --> 13:40:02,560 going to import this function so I'm 15875 13:40:01,400 --> 13:40:06,080 just going to move this down to the 15876 13:40:02,560 --> 13:40:09,400 bottom so we can see it side by side 15877 13:40:06,080 --> 13:40:12,840 so here so in checkout. JS we're going 15878 13:40:09,400 --> 13:40:15,240 to scroll up and we're already importing 15879 13:40:12,840 --> 13:40:18,480 from cart. JS so we're just going to 15880 13:40:15,240 --> 13:40:20,956 import one additional Thing by typing 15881 13:40:18,480 --> 13:40:26,116 comma and we're going to import this 15882 13:40:20,956 --> 13:40:28,160 function remove from cart and 15883 13:40:26,116 --> 13:40:31,880 save and then we're going to use this 15884 13:40:28,160 --> 13:40:36,200 function at the bottom so we'll scroll 15885 13:40:31,880 --> 13:40:40,400 down and then here we're going to 15886 13:40:36,200 --> 13:40:42,520 remove from cart brackets and we're 15887 13:40:40,400 --> 13:40:46,400 going to give it the product ID we want 15888 13:40:42,520 --> 13:40:50,200 to remove so product 15889 13:40:46,400 --> 13:40:52,720 ID and lastly let's just console.log the 15890 13:40:50,200 --> 13:40:56,160 cart to make sure that this works so 15891 13:40:52,720 --> 13:40:59,116 console.log the card and 15892 13:40:56,160 --> 13:41:01,520 save so now let's go into our 15893 13:40:59,116 --> 13:41:04,240 project and we're going to click this 15894 13:41:01,520 --> 13:41:07,560 delete link for the first product so 15895 13:41:04,240 --> 13:41:11,320 delete and let's open the console right 15896 13:41:07,560 --> 13:41:13,200 click inspect the console and now it's 15897 13:41:11,320 --> 13:41:15,880 going to show us that we have one 15898 13:41:13,200 --> 13:41:18,200 product remaining in the cart and this 15899 13:41:15,880 --> 13:41:20,640 product has a quantity of one which is 15900 13:41:18,200 --> 13:41:23,520 the second product here so by clicking 15901 13:41:20,640 --> 13:41:26,840 that delete link We successfully removed 15902 13:41:23,520 --> 13:41:29,480 this product from the cart so now if we 15903 13:41:26,840 --> 13:41:30,400 refresh and tried clicking the second 15904 13:41:29,480 --> 13:41:33,596 delete 15905 13:41:30,400 --> 13:41:36,916 link and look at the 15906 13:41:33,596 --> 13:41:38,956 console and then EXP expand the cart it 15907 13:41:36,916 --> 13:41:41,200 will tell us that the remaining product 15908 13:41:38,956 --> 13:41:44,000 has a quantity of two which is this 15909 13:41:41,200 --> 13:41:45,956 product so we successfully removed the 15910 13:41:44,000 --> 13:41:48,200 second product from the cart when we 15911 13:41:45,956 --> 13:41:51,160 clicked 15912 13:41:48,200 --> 13:41:54,040 delete so now let's go into get and look 15913 13:41:51,160 --> 13:41:56,756 at our changes so we'll go into this 15914 13:41:54,040 --> 13:41:59,880 section well I can close this for now 15915 13:41:56,756 --> 13:42:03,640 and we'll open git and it will tell us 15916 13:41:59,880 --> 13:42:06,116 that in C.J we change it to let and we 15917 13:42:03,640 --> 13:42:09,596 created this new remove from cart 15918 13:42:06,116 --> 13:42:12,756 function and then in checkout. JS we 15919 13:42:09,596 --> 13:42:15,596 import that function and then at the 15920 13:42:12,756 --> 13:42:17,320 bottom when we click the delete link 15921 13:42:15,596 --> 13:42:21,160 we're going to remove this product from 15922 13:42:17,320 --> 13:42:24,240 the cart now finally we can remove this 15923 13:42:21,160 --> 13:42:26,880 console.log save and we're going to put 15924 13:42:24,240 --> 13:42:28,560 these changes into git so we'll type a 15925 13:42:26,880 --> 13:42:34,436 message to describe our 15926 13:42:28,560 --> 13:42:35,720 changes delete products from cart when 15927 13:42:34,436 --> 13:42:39,160 clicking 15928 13:42:35,720 --> 13:42:39,160 delete and 15929 13:42:41,040 --> 13:42:46,640 commit the second thing we have to do 15930 13:42:43,400 --> 13:42:49,560 when we click delete is to update our 15931 13:42:46,640 --> 13:42:52,840 HTML so if we go back to our 15932 13:42:49,560 --> 13:42:56,400 project when we click delete we want to 15933 13:42:52,840 --> 13:42:59,200 remove this product from the page so one 15934 13:42:56,400 --> 13:43:01,560 way we can do this is first we're going 15935 13:42:59,200 --> 13:43:04,040 to use the Dom to get the element we 15936 13:43:01,560 --> 13:43:05,840 want to remove and then we can use a 15937 13:43:04,040 --> 13:43:08,276 method called 15938 13:43:05,840 --> 13:43:11,560 remove so every element that we get from 15939 13:43:08,276 --> 13:43:15,276 the Dom has a method called remove which 15940 13:43:11,560 --> 13:43:17,640 removes it from the page so first of all 15941 13:43:15,276 --> 13:43:20,116 how do we know which element to get from 15942 13:43:17,640 --> 13:43:22,956 the page so let's actually look at the 15943 13:43:20,116 --> 13:43:24,040 HTML for one of these products we're 15944 13:43:22,956 --> 13:43:25,916 going to 15945 13:43:24,040 --> 13:43:29,520 rightclick 15946 13:43:25,916 --> 13:43:31,916 inspect and then click the top left and 15947 13:43:29,520 --> 13:43:35,276 then click one of these products 15948 13:43:31,916 --> 13:43:37,240 here and this will give us the HTML so 15949 13:43:35,276 --> 13:43:40,200 these two products are represented by 15950 13:43:37,240 --> 13:43:41,840 these two divs here with the class 15951 13:43:40,200 --> 13:43:44,680 cart-- 15952 13:43:41,840 --> 13:43:47,320 container so one way to identify which 15953 13:43:44,680 --> 13:43:49,756 product this is for is to add a special 15954 13:43:47,320 --> 13:43:51,880 class to this element and this class 15955 13:43:49,756 --> 13:43:55,720 will contain the product 15956 13:43:51,880 --> 13:43:59,756 ID and then if we go back to our code 15957 13:43:55,720 --> 13:44:01,116 and we open checkout. JS and scroll to 15958 13:43:59,756 --> 13:44:04,520 the 15959 13:44:01,116 --> 13:44:07,400 bottom when we click delete we have the 15960 13:44:04,520 --> 13:44:09,276 product ID here so we can use this to 15961 13:44:07,400 --> 13:44:12,680 select that special class that we're 15962 13:44:09,276 --> 13:44:15,680 going to add so first let's add that 15963 13:44:12,680 --> 13:44:18,360 special class to each of these elements 15964 13:44:15,680 --> 13:44:20,756 so we're going to scroll up to the 15965 13:44:18,360 --> 13:44:24,680 HTML and we're going to go to the top 15966 13:44:20,756 --> 13:44:27,080 here to the div cart-- 15967 13:44:24,680 --> 13:44:29,116 container and then here we'll add a 15968 13:44:27,080 --> 13:44:35,560 special class 15969 13:44:29,116 --> 13:44:38,880 js- cart Das item Das container Das and 15970 13:44:35,560 --> 13:44:40,796 then we'll substitute the product's ID 15971 13:44:38,880 --> 13:44:42,560 so we'll type the 15972 13:44:40,796 --> 13:44:44,520 matching 15973 13:44:42,560 --> 13:44:46,796 product. 15974 13:44:44,520 --> 13:44:48,956 ID and let's put this class on a 15975 13:44:46,796 --> 13:44:51,116 separate line so the line is not too 15976 13:44:48,956 --> 13:44:53,276 long now let's 15977 13:44:51,116 --> 13:44:56,116 save and now we're going to scroll to 15978 13:44:53,276 --> 13:44:58,190 the bottom and use our code to get this 15979 13:44:56,116 --> 13:45:00,080 class so we'll scroll to the 15980 13:44:58,190 --> 13:45:02,480 [Music] 15981 13:45:00,080 --> 13:45:05,640 bottom and here we're going to use the 15982 13:45:02,480 --> 13:45:08,880 Dom as well as this product ID to get 15983 13:45:05,640 --> 13:45:12,756 that special class that we just added 15984 13:45:08,880 --> 13:45:17,320 above so here we can type 15985 13:45:12,756 --> 13:45:20,116 document. query selector brackets and 15986 13:45:17,320 --> 13:45:24,160 then inside we'll type a string and 15987 13:45:20,116 --> 13:45:29,480 we'll select the class using a DOT js- 15988 13:45:24,160 --> 13:45:32,680 cart D item D container Dash and then 15989 13:45:29,480 --> 13:45:34,360 we're going to substitute the product ID 15990 13:45:32,680 --> 13:45:36,560 so because we want to insert something 15991 13:45:34,360 --> 13:45:38,796 in here we're going to switch this to a 15992 13:45:36,560 --> 13:45:42,240 template string using 15993 13:45:38,796 --> 13:45:44,880 backtick and now we can substitute this 15994 13:45:42,240 --> 13:45:47,840 product ID into this 15995 13:45:44,880 --> 13:45:51,276 string product 15996 13:45:47,840 --> 13:45:53,200 ID so by creating the string like this 15997 13:45:51,276 --> 13:45:56,400 we're able to select the specific 15998 13:45:53,200 --> 13:45:58,360 container that we need so now let's save 15999 13:45:56,400 --> 13:46:03,000 this in a variable so we can use it 16000 13:45:58,360 --> 13:46:05,400 later so const container equals this 16001 13:46:03,000 --> 13:46:09,880 result so for now let's let's actually 16002 13:46:05,400 --> 13:46:12,116 console.log this to see if it works 16003 13:46:09,880 --> 13:46:14,320 console.log 16004 13:46:12,116 --> 13:46:16,480 container and 16005 13:46:14,320 --> 13:46:19,160 save and then we'll go back to our 16006 13:46:16,480 --> 13:46:22,400 project so just move this 16007 13:46:19,160 --> 13:46:25,040 here and then in our project if we click 16008 13:46:22,400 --> 13:46:28,320 delete on the first product and then 16009 13:46:25,040 --> 13:46:31,040 check the console right click inspect 16010 13:46:28,320 --> 13:46:32,756 and the console you can see that we're 16011 13:46:31,040 --> 13:46:36,080 getting the correct element that we 16012 13:46:32,756 --> 13:46:39,756 should delete if we go out here and 16013 13:46:36,080 --> 13:46:42,276 click this delete link and check the 16014 13:46:39,756 --> 13:46:44,240 console you can see that this is also 16015 13:46:42,276 --> 13:46:45,796 getting us the correct element that we 16016 13:46:44,240 --> 13:46:48,240 want to 16017 13:46:45,796 --> 13:46:50,880 delete so now that we have the element 16018 13:46:48,240 --> 13:46:54,680 we want to delete the next step is to 16019 13:46:50,880 --> 13:46:57,040 use the do remove method so again every 16020 13:46:54,680 --> 13:46:59,360 element we get with the Dom has a method 16021 13:46:57,040 --> 13:47:02,680 called do remove which will remove it 16022 13:46:59,360 --> 13:47:06,436 from the page so if we go back into our 16023 13:47:02,680 --> 13:47:08,956 code down here all we have to do is type 16024 13:47:06,436 --> 13:47:12,880 out the element 16025 13:47:08,956 --> 13:47:16,040 container. remove and brackets and this 16026 13:47:12,880 --> 13:47:20,000 will remove it from the page if we 16027 13:47:16,040 --> 13:47:22,640 save and try it out so we'll go in here 16028 13:47:20,000 --> 13:47:24,796 we'll click delete on the first product 16029 13:47:22,640 --> 13:47:27,360 and it will remove it from the 16030 13:47:24,796 --> 13:47:30,200 page if we click delete on the second 16031 13:47:27,360 --> 13:47:33,756 product it will also remove it from the 16032 13:47:30,200 --> 13:47:35,400 page so that's it now let's refresh to 16033 13:47:33,756 --> 13:47:37,360 bring the products back 16034 13:47:35,400 --> 13:47:39,640 and that's how we make the delete link 16035 13:47:37,360 --> 13:47:42,116 interactive and remove the products from 16036 13:47:39,640 --> 13:47:44,796 the cart as well as from the 16037 13:47:42,116 --> 13:47:48,680 page so now let's save our changes into 16038 13:47:44,796 --> 13:47:48,680 git we'll go to the git 16039 13:47:48,956 --> 13:47:54,916 section and it will tell us that in 16040 13:47:51,276 --> 13:47:57,596 checkout. JS at the top we added that 16041 13:47:54,916 --> 13:48:01,480 special class which contains the product 16042 13:47:57,596 --> 13:48:04,200 ID and if we scroll down to the 16043 13:48:01,480 --> 13:48:06,080 bottom we created the string which will 16044 13:48:04,200 --> 13:48:08,520 get us these specific element that we 16045 13:48:06,080 --> 13:48:11,240 want to remove and then we use the do 16046 13:48:08,520 --> 13:48:13,796 remove method so now let's create a 16047 13:48:11,240 --> 13:48:18,720 message to describe our 16048 13:48:13,796 --> 13:48:25,160 changes delete products from 16049 13:48:18,720 --> 13:48:25,160 HTML when clicking delete and 16050 13:48:28,160 --> 13:48:32,240 commit the last thing we're going to do 16051 13:48:30,200 --> 13:48:35,400 in this lesson is we're going to add 16052 13:48:32,240 --> 13:48:36,756 local storage to our cart so if we go 16053 13:48:35,400 --> 13:48:39,560 back to our 16054 13:48:36,756 --> 13:48:41,680 project if we delete some products in 16055 13:48:39,560 --> 13:48:44,360 our cart and then 16056 13:48:41,680 --> 13:48:47,240 refresh notice that our cart goes back 16057 13:48:44,360 --> 13:48:50,480 to what it was before and that's because 16058 13:48:47,240 --> 13:48:52,520 right now if we look at our code and we 16059 13:48:50,480 --> 13:48:56,400 open cart. 16060 13:48:52,520 --> 13:48:58,680 JS our cart is just a variable and 16061 13:48:56,400 --> 13:49:02,080 variables are reset when we refresh the 16062 13:48:58,680 --> 13:49:04,840 page or when we go to a different page 16063 13:49:02,080 --> 13:49:06,480 so again if we go back to our project 16064 13:49:04,840 --> 13:49:09,320 and we go to a different Page by 16065 13:49:06,480 --> 13:49:12,040 clicking the top left so on this 16066 13:49:09,320 --> 13:49:15,276 homepage we can add as many products as 16067 13:49:12,040 --> 13:49:18,276 we want to our cart and if we go back to 16068 13:49:15,276 --> 13:49:22,040 the checkout page our cart will just get 16069 13:49:18,276 --> 13:49:24,000 reset to whatever it is here so that's a 16070 13:49:22,040 --> 13:49:26,400 problem with just using a variable to 16071 13:49:24,000 --> 13:49:29,320 save our cart it gets reset when we 16072 13:49:26,400 --> 13:49:31,880 refresh the page or we go to a different 16073 13:49:29,320 --> 13:49:35,240 page to solve this problem we're going 16074 13:49:31,880 --> 13:49:37,400 to use local storage to save our cart 16075 13:49:35,240 --> 13:49:40,200 so first let's create a function for 16076 13:49:37,400 --> 13:49:43,160 saving the cart to local storage because 16077 13:49:40,200 --> 13:49:47,240 we might want to reuse this code so down 16078 13:49:43,160 --> 13:49:52,680 here let's create a function we'll call 16079 13:49:47,240 --> 13:49:54,640 it save to storage brackets and curler 16080 13:49:52,680 --> 13:49:58,200 brackets and we're just going to save 16081 13:49:54,640 --> 13:50:02,360 the cart to local storage using local 16082 13:49:58,200 --> 13:50:05,400 storage do set item 16083 13:50:02,360 --> 13:50:08,596 brackets and set item takes two 16084 13:50:05,400 --> 13:50:10,720 strings the first string is the name of 16085 13:50:08,596 --> 13:50:13,400 whatever we want to save so we're going 16086 13:50:10,720 --> 13:50:15,840 to save our cart so we're just going to 16087 13:50:13,400 --> 13:50:19,040 call this the cart and then the second 16088 13:50:15,840 --> 13:50:21,400 string is the data that we want to save 16089 13:50:19,040 --> 13:50:24,596 so remember that local storage can only 16090 13:50:21,400 --> 13:50:27,796 save strings so to save our cart we need 16091 13:50:24,596 --> 13:50:30,320 to convert it into a string first and we 16092 13:50:27,796 --> 13:50:32,240 can do that using 16093 13:50:30,320 --> 13:50:35,560 json. 16094 13:50:32,240 --> 13:50:38,596 stringify brackets and then inside we're 16095 13:50:35,560 --> 13:50:41,160 going to give it the cart so this will 16096 13:50:38,596 --> 13:50:44,520 convert the cart into a string and now 16097 13:50:41,160 --> 13:50:47,240 we can save it in local storage so now 16098 13:50:44,520 --> 13:50:49,360 let's use this function whenever we 16099 13:50:47,240 --> 13:50:51,720 update the cart we need to save it to 16100 13:50:49,360 --> 13:50:55,080 local storage so it doesn't get reset 16101 13:50:51,720 --> 13:50:58,400 when we refresh the page so here when we 16102 13:50:55,080 --> 13:51:00,596 add it to the cart at the bottom after 16103 13:50:58,400 --> 13:51:04,640 we update the cart we're going to use 16104 13:51:00,596 --> 13:51:07,956 that function we created above save to 16105 13:51:04,640 --> 13:51:10,840 to storage and 16106 13:51:07,956 --> 13:51:13,596 brackets and also in the function remove 16107 13:51:10,840 --> 13:51:16,560 from cart so this function is also 16108 13:51:13,596 --> 13:51:22,320 updating the cart so after we update the 16109 13:51:16,560 --> 13:51:25,436 cart we're also going to save to storage 16110 13:51:22,320 --> 13:51:28,640 and brackets and 16111 13:51:25,436 --> 13:51:31,080 save so now whenever we update the cart 16112 13:51:28,640 --> 13:51:33,796 we're going to save it in local 16113 13:51:31,080 --> 13:51:36,480 storage the last thing we need to do is 16114 13:51:33,796 --> 13:51:38,400 at the top at the beginning of our code 16115 13:51:36,480 --> 13:51:41,116 we need to get the cart from local 16116 13:51:38,400 --> 13:51:43,596 storage instead of using this default 16117 13:51:41,116 --> 13:51:46,276 value so let's add some new lines here 16118 13:51:43,596 --> 13:51:48,000 to separate the code for a bit and here 16119 13:51:46,276 --> 13:51:53,040 we're going to get the cart from local 16120 13:51:48,000 --> 13:51:57,916 storage and we can do that using local 16121 13:51:53,040 --> 13:52:00,796 storage. get item and brackets so get 16122 13:51:57,916 --> 13:52:03,240 item takes one string and that is the 16123 13:52:00,796 --> 13:52:04,916 name of what we saved earlier which is 16124 13:52:03,240 --> 13:52:06,200 the cart so we're going to to give it 16125 13:52:04,916 --> 13:52:08,840 the string 16126 13:52:06,200 --> 13:52:09,720 cart so this will give us the cart that 16127 13:52:08,840 --> 13:52:12,040 we 16128 13:52:09,720 --> 13:52:15,160 saved however remember that local 16129 13:52:12,040 --> 13:52:17,560 storage can only save strings so it will 16130 13:52:15,160 --> 13:52:19,680 give us the string version of our cart 16131 13:52:17,560 --> 13:52:22,480 and we need to convert it back into an 16132 13:52:19,680 --> 13:52:24,916 array to convert it back to an array we 16133 13:52:22,480 --> 13:52:31,360 can use the code 16134 13:52:24,916 --> 13:52:31,360 json. pars and brackets and close 16135 13:52:32,040 --> 13:52:37,360 bracket the last thing we need to do is 16136 13:52:34,956 --> 13:52:40,360 when we first use the website we might 16137 13:52:37,360 --> 13:52:42,796 not have a cart in local storage so if 16138 13:52:40,360 --> 13:52:46,160 we don't have a cart saved local storage 16139 13:52:42,796 --> 13:52:48,480 will give us null so in this situation 16140 13:52:46,160 --> 13:52:51,560 if this value is null we want to give 16141 13:52:48,480 --> 13:52:54,640 the cart a default value which is this 16142 13:52:51,560 --> 13:52:57,596 value below so to give the cart a 16143 13:52:54,640 --> 13:53:01,160 default value if it's empty we can use 16144 13:52:57,596 --> 13:53:04,720 an if statement by typing if brackets 16145 13:53:01,160 --> 13:53:07,436 and curly brackets and then here we can 16146 13:53:04,720 --> 13:53:10,240 check if not 16147 13:53:07,436 --> 13:53:12,560 cart so if we don't have a cart in local 16148 13:53:10,240 --> 13:53:15,756 storage this will give us 16149 13:53:12,560 --> 13:53:18,040 null in this case not null will become 16150 13:53:15,756 --> 13:53:19,520 truthy and we're going to run the code 16151 13:53:18,040 --> 13:53:22,560 in this if 16152 13:53:19,520 --> 13:53:25,640 statement so if the cart is empty we're 16153 13:53:22,560 --> 13:53:28,240 going to make the cart equal this 16154 13:53:25,640 --> 13:53:32,360 default value below so let's just select 16155 13:53:28,240 --> 13:53:35,796 this code and we'll right click cut and 16156 13:53:32,360 --> 13:53:40,200 then in here right click and 16157 13:53:35,796 --> 13:53:43,200 paste and remove the extra line and 16158 13:53:40,200 --> 13:53:46,436 save so now if the cart is empty we just 16159 13:53:43,200 --> 13:53:49,596 give it a default value if we go back to 16160 13:53:46,436 --> 13:53:52,160 our project and try out our code so 16161 13:53:49,596 --> 13:53:55,116 let's delete the first product and 16162 13:53:52,160 --> 13:53:58,276 refresh the page notice that the cart 16163 13:53:55,116 --> 13:54:01,840 gets saved and the first product remains 16164 13:53:58,276 --> 13:54:04,560 deleted if we go to the homepage and we 16165 13:54:01,840 --> 13:54:06,160 add some new products 16166 13:54:04,560 --> 13:54:09,116 and then go to our 16167 13:54:06,160 --> 13:54:12,596 cart notice that our cart is saved 16168 13:54:09,116 --> 13:54:14,680 between pages so by using local storage 16169 13:54:12,596 --> 13:54:18,200 we can save our cart even when we 16170 13:54:14,680 --> 13:54:20,596 refresh the page or go between different 16171 13:54:18,200 --> 13:54:23,520 pages so that's all we're going to build 16172 13:54:20,596 --> 13:54:26,276 in this lesson so to clean up let's 16173 13:54:23,520 --> 13:54:28,796 delete all these products in the 16174 13:54:26,276 --> 13:54:32,400 cart and then go to the 16175 13:54:28,796 --> 13:54:34,680 homepage and we'll add back two socks 16176 13:54:32,400 --> 13:54:36,360 and one basketball which which is the 16177 13:54:34,680 --> 13:54:39,680 default value of the 16178 13:54:36,360 --> 13:54:42,320 cart let's go back to the checkout 16179 13:54:39,680 --> 13:54:45,320 page and now we're done with this part 16180 13:54:42,320 --> 13:54:48,520 of the page so finally let's save our 16181 13:54:45,320 --> 13:54:51,276 changes into git we'll go to the git 16182 13:54:48,520 --> 13:54:54,400 section and it will tell us that inside 16183 13:54:51,276 --> 13:54:57,400 cart. JS we're now loading the cart from 16184 13:54:54,400 --> 13:54:58,956 local storage when we open the page if 16185 13:54:57,400 --> 13:55:01,880 the cart doesn't exist we're going to 16186 13:54:58,956 --> 13:55:04,000 give it a default value and we created a 16187 13:55:01,880 --> 13:55:04,840 function to save the cart into local 16188 13:55:04,000 --> 13:55:07,400 store 16189 13:55:04,840 --> 13:55:08,520 stage so now let's add a message to 16190 13:55:07,400 --> 13:55:17,640 describe our 16191 13:55:08,520 --> 13:55:17,640 changes save cart in local storage and 16192 13:55:19,880 --> 13:55:25,796 commit and that's the end of this lesson 16193 13:55:22,916 --> 13:55:28,080 in this lesson we learned about modules 16194 13:55:25,796 --> 13:55:31,116 which is a better way to organize our 16195 13:55:28,080 --> 13:55:34,560 code we created the checkout page in our 16196 13:55:31,116 --> 13:55:37,956 final project we learned about HTML link 16197 13:55:34,560 --> 13:55:41,116 elements and radio selectors we made the 16198 13:55:37,956 --> 13:55:43,956 delete link interactive and we saved the 16199 13:55:41,116 --> 13:55:46,596 cart in local 16200 13:55:43,956 --> 13:55:50,596 storage here are some exercises to help 16201 13:55:46,596 --> 13:55:50,596 you practice what we learned in this 16202 13:56:03,240 --> 13:56:07,240 lesson e 16203 13:56:38,040 --> 13:56:42,000 in this lesson we're going to learn 16204 13:56:39,720 --> 13:56:44,596 about external libraries and we're going 16205 13:56:42,000 --> 13:56:47,680 to finish the checkout page in our final 16206 13:56:44,596 --> 13:56:50,756 project an external library is basically 16207 13:56:47,680 --> 13:56:53,680 just code that is outside of our 16208 13:56:50,756 --> 13:56:56,276 project so far we've only been using 16209 13:56:53,680 --> 13:56:57,360 Code that is inside our project like 16210 13:56:56,276 --> 13:57:00,276 these 16211 13:56:57,360 --> 13:57:02,756 files well a lot of developers write 16212 13:57:00,276 --> 13:57:05,596 JavaScript code and then put their code 16213 13:57:02,756 --> 13:57:08,520 on the internet and we can actually load 16214 13:57:05,596 --> 13:57:11,796 their code from the internet into our 16215 13:57:08,520 --> 13:57:14,520 project and use it we call this code an 16216 13:57:11,796 --> 13:57:17,756 external library because it's code that 16217 13:57:14,520 --> 13:57:20,680 is outside of our project Let's do an 16218 13:57:17,756 --> 13:57:22,840 example and load our first external 16219 13:57:20,680 --> 13:57:24,720 Library we're going to go to the video 16220 13:57:22,840 --> 13:57:26,956 description and we're going to click 16221 13:57:24,720 --> 13:57:26,956 this 16222 13:57:29,720 --> 13:57:35,276 link for the hello external Library 16223 13:57:37,680 --> 13:57:42,596 and it will open up a page that contains 16224 13:57:40,080 --> 13:57:44,756 JavaScript code so I actually wrote this 16225 13:57:42,596 --> 13:57:47,916 code myself and you'll notice that this 16226 13:57:44,756 --> 13:57:50,320 is just normal JavaScript code we create 16227 13:57:47,916 --> 13:57:53,956 a function called hello and it just 16228 13:57:50,320 --> 13:57:56,640 console.log hello so this is called an 16229 13:57:53,956 --> 13:57:59,640 external Library it's code that is 16230 13:57:56,640 --> 13:58:02,436 outside of our project so the cool thing 16231 13:57:59,640 --> 13:58:06,000 is we can actually load this code from 16232 13:58:02,436 --> 13:58:09,276 the internet into our project so let's 16233 13:58:06,000 --> 13:58:11,240 give that a try we're going to go to our 16234 13:58:09,276 --> 13:58:12,916 project and we're going to open 16235 13:58:11,240 --> 13:58:15,840 checkout. 16236 13:58:12,916 --> 13:58:18,956 HTML and we're going to scroll to the 16237 13:58:15,840 --> 13:58:21,360 bottom and above the script tag we're 16238 13:58:18,956 --> 13:58:24,320 going to add another script tag to load 16239 13:58:21,360 --> 13:58:27,520 some code so 16240 13:58:24,320 --> 13:58:30,276 script and we'll give it a source 16241 13:58:27,520 --> 13:58:33,200 attribute and then in here instead of 16242 13:58:30,276 --> 13:58:35,880 using a fth like we've done before we're 16243 13:58:33,200 --> 13:58:38,640 going to give this a a URL of a website 16244 13:58:35,880 --> 13:58:40,680 that contains JavaScript code so if we 16245 13:58:38,640 --> 13:58:44,400 go back to our 16246 13:58:40,680 --> 13:58:47,560 browser and here the URL of this page is 16247 13:58:44,400 --> 13:58:50,596 up here so we're just going to copy this 16248 13:58:47,560 --> 13:58:51,596 right click copy and then paste it into 16249 13:58:50,596 --> 13:58:55,276 our 16250 13:58:51,596 --> 13:58:58,240 code right click and 16251 13:58:55,276 --> 13:59:00,880 paste so now the computer will get all 16252 13:58:58,240 --> 13:59:02,320 the JavaScript code from this URL or 16253 13:59:00,880 --> 13:59:04,880 from this 16254 13:59:02,320 --> 13:59:07,320 page so it's going to get all the code 16255 13:59:04,880 --> 13:59:11,560 from this page and load it into our 16256 13:59:07,320 --> 13:59:13,200 project like normal code so let's save 16257 13:59:11,560 --> 13:59:15,916 and let's give this a 16258 13:59:13,200 --> 13:59:18,320 try so this code creates a function 16259 13:59:15,916 --> 13:59:21,276 called hello so let's try running this 16260 13:59:18,320 --> 13:59:25,000 hello function in our project we'll go 16261 13:59:21,276 --> 13:59:28,000 to checkout. JS and then scroll to the 16262 13:59:25,000 --> 13:59:33,240 top and then here let's just run the 16263 13:59:28,000 --> 13:59:35,276 function hello so hello and brackets and 16264 13:59:33,240 --> 13:59:36,916 Save 16265 13:59:35,276 --> 13:59:40,240 now if we go back to our 16266 13:59:36,916 --> 13:59:44,276 project go to the checkout page and 16267 13:59:40,240 --> 13:59:46,680 check the console right click inspect 16268 13:59:44,276 --> 13:59:48,560 and the console you'll see that it 16269 13:59:46,680 --> 13:59:51,276 displayed the message 16270 13:59:48,560 --> 13:59:54,240 hello so that's how an external Library 16271 13:59:51,276 --> 13:59:57,276 works it's just code that is outside of 16272 13:59:54,240 --> 14:00:00,640 our project and we can load this code 16273 13:59:57,276 --> 14:00:03,080 using a script tag like this we just 16274 14:00:00,640 --> 14:00:04,680 give it the URL of where this code is 16275 14:00:03,080 --> 14:00:05,880 located 16276 14:00:04,680 --> 14:00:07,956 so there's two things you might be 16277 14:00:05,880 --> 14:00:10,916 wondering about how come we put the 16278 14:00:07,956 --> 14:00:13,560 script tag at the top so generally we 16279 14:00:10,916 --> 14:00:16,480 load external libraries first and then 16280 14:00:13,560 --> 14:00:19,360 we can use the library in our own code 16281 14:00:16,480 --> 14:00:22,640 below and the second thing is how do you 16282 14:00:19,360 --> 14:00:24,720 put code on the internet like this to 16283 14:00:22,640 --> 14:00:27,200 learn this you can check out my video on 16284 14:00:24,720 --> 14:00:29,360 how to put a website on the internet 16285 14:00:27,200 --> 14:00:33,000 it'll show you how to put any file like 16286 14:00:29,360 --> 14:00:34,640 HTML CSS and JavaScript files onto a 16287 14:00:33,000 --> 14:00:37,756 website like this 16288 14:00:34,640 --> 14:00:40,680 this next let's talk about why we use 16289 14:00:37,756 --> 14:00:43,756 external libraries external libraries 16290 14:00:40,680 --> 14:00:46,596 let us share code instead of writing all 16291 14:00:43,756 --> 14:00:48,756 the code ourselves we can use code that 16292 14:00:46,596 --> 14:00:52,160 other people wrote and this helps us 16293 14:00:48,756 --> 14:00:54,040 save time and avoid duplicating work 16294 14:00:52,160 --> 14:00:56,596 right now we're loading a very simple 16295 14:00:54,040 --> 14:00:58,956 Library so let's load a more complex 16296 14:00:56,596 --> 14:01:02,116 library to show how we can save a lot of 16297 14:00:58,956 --> 14:01:05,756 time and work let's go back to our 16298 14:01:02,116 --> 14:01:08,680 project and go to the checkout 16299 14:01:05,756 --> 14:01:11,400 page here we have a section for choosing 16300 14:01:08,680 --> 14:01:13,276 a delivery option for this product and 16301 14:01:11,400 --> 14:01:15,560 you'll notice that each option has a 16302 14:01:13,276 --> 14:01:18,360 date so this is the date that the 16303 14:01:15,560 --> 14:01:20,360 product should be delivered by so the 16304 14:01:18,360 --> 14:01:24,400 way this works is that the cheapest 16305 14:01:20,360 --> 14:01:27,880 option here takes 7 days to deliver the 16306 14:01:24,400 --> 14:01:28,560 $4.99 option takes 3 days to deliver and 16307 14:01:27,880 --> 14:01:32,400 the 16308 14:01:28,560 --> 14:01:35,000 $9.99 option takes one day to deliver so 16309 14:01:32,400 --> 14:01:37,916 to get these dates here we need to do 16310 14:01:35,000 --> 14:01:41,080 three things first we need to somehow 16311 14:01:37,916 --> 14:01:43,480 get today's date in our JavaScript 16312 14:01:41,080 --> 14:01:46,400 number two we need to do calculations 16313 14:01:43,480 --> 14:01:49,680 with the date such as ADD seven days or 16314 14:01:46,400 --> 14:01:52,000 add 3 days and number three we need to 16315 14:01:49,680 --> 14:01:53,680 display the date in an easyto read 16316 14:01:52,000 --> 14:01:56,160 format like 16317 14:01:53,680 --> 14:01:58,400 this now we could try to figure out how 16318 14:01:56,160 --> 14:02:00,596 to do this ourselves by writing our own 16319 14:01:58,400 --> 14:02:03,400 code but this is going to be a lot of 16320 14:02:00,596 --> 14:02:06,756 work instead we can use an external 16321 14:02:03,400 --> 14:02:08,276 Library to help us work with dates so in 16322 14:02:06,756 --> 14:02:11,796 this project we're going to use an 16323 14:02:08,276 --> 14:02:14,320 external Library called DJs this is a 16324 14:02:11,796 --> 14:02:19,080 popular library that helps us work with 16325 14:02:14,320 --> 14:02:21,320 dates so first let's load DJs into our 16326 14:02:19,080 --> 14:02:25,436 project we're going to go to the video 16327 14:02:21,320 --> 14:02:25,436 description and we'll click this 16328 14:02:28,596 --> 14:02:34,436 link for the DJs external Library 16329 14:02:35,596 --> 14:02:40,116 and when the page opens you'll see a 16330 14:02:37,596 --> 14:02:42,320 bunch of random code here now you might 16331 14:02:40,116 --> 14:02:44,436 think that this is some special code but 16332 14:02:42,320 --> 14:02:47,080 this is actually just regular JavaScript 16333 14:02:44,436 --> 14:02:49,240 code that has been compressed so 16334 14:02:47,080 --> 14:02:51,480 basically all the extra spaces have been 16335 14:02:49,240 --> 14:02:53,116 removed and the variable names have been 16336 14:02:51,480 --> 14:02:56,160 shortened to one 16337 14:02:53,116 --> 14:02:58,640 letter we can press JavaScript code like 16338 14:02:56,160 --> 14:03:02,000 this because we need to load this code 16339 14:02:58,640 --> 14:03:04,116 from the internet into our project by 16340 14:03:02,000 --> 14:03:06,956 compressing it it makes this code 16341 14:03:04,116 --> 14:03:09,680 smaller so it will load into our project 16342 14:03:06,956 --> 14:03:14,116 faster so this compression process is 16343 14:03:09,680 --> 14:03:17,000 called minification or we Minify the 16344 14:03:14,116 --> 14:03:20,480 code okay so this is the code for the 16345 14:03:17,000 --> 14:03:23,400 external Library DJs now let's load this 16346 14:03:20,480 --> 14:03:24,480 code into our project we'll go back to 16347 14:03:23,400 --> 14:03:27,276 our 16348 14:03:24,480 --> 14:03:29,796 code and then go to checkout. 16349 14:03:27,276 --> 14:03:33,916 HTML and then here we're going to add 16350 14:03:29,796 --> 14:03:36,200 another script tag less than script and 16351 14:03:33,916 --> 14:03:38,480 the source 16352 14:03:36,200 --> 14:03:41,040 attribute and then here we're going to 16353 14:03:38,480 --> 14:03:43,916 give it the URL of the code we want to 16354 14:03:41,040 --> 14:03:46,560 load so let's go back to the code for 16355 14:03:43,916 --> 14:03:49,080 DJs and then up here we're going to 16356 14:03:46,560 --> 14:03:52,360 rightclick and copy this 16357 14:03:49,080 --> 14:03:53,720 URL and then inside our code in the 16358 14:03:52,360 --> 14:03:56,000 source attribute we're going to 16359 14:03:53,720 --> 14:03:57,956 rightclick and paste the 16360 14:03:56,000 --> 14:04:00,680 URL and 16361 14:03:57,956 --> 14:04:03,160 save so again this will take all the 16362 14:04:00,680 --> 14:04:05,840 code that's located on this page and 16363 14:04:03,160 --> 14:04:09,400 then load it into our 16364 14:04:05,840 --> 14:04:11,756 project now let's give it a try the DJs 16365 14:04:09,400 --> 14:04:12,796 external Library creates a function 16366 14:04:11,756 --> 14:04:16,840 called 16367 14:04:12,796 --> 14:04:20,756 DJs so we'll go to checkout. JS and then 16368 14:04:16,840 --> 14:04:22,680 at the top we're going to run the JJs 16369 14:04:20,756 --> 14:04:24,640 function and 16370 14:04:22,680 --> 14:04:26,596 brackets now make sure this is all 16371 14:04:24,640 --> 14:04:29,276 lowercase cuz that's the name that the 16372 14:04:26,596 --> 14:04:32,436 library used so now let's actually 16373 14:04:29,276 --> 14:04:35,436 console.log the result of this function 16374 14:04:32,436 --> 14:04:35,436 console.log 16375 14:04:36,596 --> 14:04:41,276 and 16376 14:04:37,840 --> 14:04:42,916 save and we'll go back to our project in 16377 14:04:41,276 --> 14:04:45,116 the checkout page we're going to check 16378 14:04:42,916 --> 14:04:47,520 our 16379 14:04:45,116 --> 14:04:50,320 console and you'll see that the DJs 16380 14:04:47,520 --> 14:04:52,796 function gives us an object and this 16381 14:04:50,320 --> 14:04:56,640 object represents the current date and 16382 14:04:52,796 --> 14:04:59,560 time so we know that DJs is working and 16383 14:04:56,640 --> 14:05:02,200 that's it we successfully loaded the DJs 16384 14:04:59,560 --> 14:05:05,080 external Library into our 16385 14:05:02,200 --> 14:05:08,200 project now let's learn how to use 16386 14:05:05,080 --> 14:05:10,360 DJs external libraries usually have a 16387 14:05:08,200 --> 14:05:12,680 documentation page that shows us how to 16388 14:05:10,360 --> 14:05:14,520 use the library we can find the 16389 14:05:12,680 --> 14:05:18,400 documentation Page by searching on 16390 14:05:14,520 --> 14:05:21,240 Google the library name like DJs and 16391 14:05:18,400 --> 14:05:23,796 then documentation or we can ask an AI 16392 14:05:21,240 --> 14:05:25,840 tool how to use the library to make 16393 14:05:23,796 --> 14:05:27,916 things easier in this video I'm going to 16394 14:05:25,840 --> 14:05:29,520 show you the features that we need and 16395 14:05:27,916 --> 14:05:32,640 you can go through the documentation 16396 14:05:29,520 --> 14:05:35,240 later if you want so earlier we said we 16397 14:05:32,640 --> 14:05:38,040 needed to do three things with the date 16398 14:05:35,240 --> 14:05:40,116 first we want to get today's date number 16399 14:05:38,040 --> 14:05:42,720 two we want to do calculations with the 16400 14:05:40,116 --> 14:05:46,200 date and number three we want to display 16401 14:05:42,720 --> 14:05:49,360 the date in an easyto read format DJs 16402 14:05:46,200 --> 14:05:52,560 helps us do all of these things so first 16403 14:05:49,360 --> 14:05:54,756 let's use DJs to get today's date 16404 14:05:52,560 --> 14:05:57,276 according to the documentation to get 16405 14:05:54,756 --> 14:06:01,000 the current date and time we can just 16406 14:05:57,276 --> 14:06:04,160 use the DJs function like 16407 14:06:01,000 --> 14:06:07,200 this so if we go back to our code 16408 14:06:04,160 --> 14:06:08,720 and that's what we have here we call DJs 16409 14:06:07,200 --> 14:06:11,796 and it gives us an object that 16410 14:06:08,720 --> 14:06:13,720 represents the current date and time the 16411 14:06:11,796 --> 14:06:16,560 second thing we need to do is to do 16412 14:06:13,720 --> 14:06:18,680 calculations with the date so for the 16413 14:06:16,560 --> 14:06:22,360 delivery options here we need to 16414 14:06:18,680 --> 14:06:25,956 calculate today's date plus 7 days or 16415 14:06:22,360 --> 14:06:28,596 today's date plus 3 days so again the 16416 14:06:25,956 --> 14:06:31,320 DJs Library can help us do 16417 14:06:28,596 --> 14:06:33,916 this so let's go back to our 16418 14:06:31,320 --> 14:06:37,000 console and look at the object that we 16419 14:06:33,916 --> 14:06:40,000 get back from DJs so this object 16420 14:06:37,000 --> 14:06:43,520 represents today's date and time and 16421 14:06:40,000 --> 14:06:45,880 this object also has a method called add 16422 14:06:43,520 --> 14:06:48,720 which adds a certain amount of time to 16423 14:06:45,880 --> 14:06:51,436 this date so this is what we need for 16424 14:06:48,720 --> 14:06:53,756 the project to calculate these dates 16425 14:06:51,436 --> 14:06:57,000 here let's go to our 16426 14:06:53,756 --> 14:07:00,596 code and first let's save this result in 16427 14:06:57,000 --> 14:07:04,200 a variable so we're going to remove this 16428 14:07:00,596 --> 14:07:06,320 and create a variable const today 16429 14:07:04,200 --> 14:07:08,840 equals 16430 14:07:06,320 --> 14:07:12,160 DJs and then down here we're going to 16431 14:07:08,840 --> 14:07:14,916 use the add method so we'll type 16432 14:07:12,160 --> 14:07:18,200 today. add and 16433 14:07:14,916 --> 14:07:20,840 brackets now the add method takes two 16434 14:07:18,200 --> 14:07:23,276 parameters the first one is the number 16435 14:07:20,840 --> 14:07:25,360 of time that we want to add so if we 16436 14:07:23,276 --> 14:07:28,880 want to add seven days we're going to 16437 14:07:25,360 --> 14:07:30,880 type seven and then the second parameter 16438 14:07:28,880 --> 14:07:33,916 is the length of time that we want to 16439 14:07:30,880 --> 14:07:37,680 add so if we want to add seven days 16440 14:07:33,916 --> 14:07:41,160 we're going to give it a string days so 16441 14:07:37,680 --> 14:07:43,916 this will add seven days to today's 16442 14:07:41,160 --> 14:07:49,200 date so let's save this result in a 16443 14:07:43,916 --> 14:07:51,840 variable const delivery date equals this 16444 14:07:49,200 --> 14:07:55,080 result and we'll console.log it to make 16445 14:07:51,840 --> 14:07:59,680 sure it's working correctly 16446 14:07:55,080 --> 14:08:02,796 console.log the delivery date and 16447 14:07:59,680 --> 14:08:04,840 save now if we go back to our project 16448 14:08:02,796 --> 14:08:07,436 and open the 16449 14:08:04,840 --> 14:08:10,880 console it will give us an object that 16450 14:08:07,436 --> 14:08:14,000 represents 7 days after to date or 7 16451 14:08:10,880 --> 14:08:16,916 days after when you're watching this 16452 14:08:14,000 --> 14:08:19,240 video so that's how we use DJs to do 16453 14:08:16,916 --> 14:08:21,796 calculations with the date it gives us 16454 14:08:19,240 --> 14:08:25,080 methods like add to add a certain amount 16455 14:08:21,796 --> 14:08:28,240 of time to the date finally we're going 16456 14:08:25,080 --> 14:08:31,040 to use DJs to display this date in an 16457 14:08:28,240 --> 14:08:34,360 easyto read format like 16458 14:08:31,040 --> 14:08:36,560 this so according to the document 16459 14:08:34,360 --> 14:08:39,520 this date object has another method 16460 14:08:36,560 --> 14:08:43,000 called format which will display the 16461 14:08:39,520 --> 14:08:45,276 date in an easyto read format so let's 16462 14:08:43,000 --> 14:08:48,160 go back to our 16463 14:08:45,276 --> 14:08:52,276 code and we'll remove the 16464 14:08:48,160 --> 14:08:55,240 console.log and we'll type delivery 16465 14:08:52,276 --> 14:08:58,560 date. 16466 14:08:55,240 --> 14:09:01,040 format and then inside these brackets 16467 14:08:58,560 --> 14:09:03,560 we're going to give this method a string 16468 14:09:01,040 --> 14:09:06,720 so this string tells DJs what kind kind 16469 14:09:03,560 --> 14:09:09,160 of format we want for the date so in the 16470 14:09:06,720 --> 14:09:11,560 documentation it has a table showing us 16471 14:09:09,160 --> 14:09:14,560 some special characters we can put into 16472 14:09:11,560 --> 14:09:17,000 this string for example if we want to 16473 14:09:14,560 --> 14:09:20,956 show the day of the week like Monday or 16474 14:09:17,000 --> 14:09:23,436 Tuesday we can type four D's into the 16475 14:09:20,956 --> 14:09:25,596 string and this will give us a string 16476 14:09:23,436 --> 14:09:27,160 where this text is replaced by the 16477 14:09:25,596 --> 14:09:30,240 actual day of the 16478 14:09:27,160 --> 14:09:32,720 week if we want to add a comma and a 16479 14:09:30,240 --> 14:09:35,080 space to this format we can just add a 16480 14:09:32,720 --> 14:09:37,916 comma and space and this will show up 16481 14:09:35,080 --> 14:09:40,000 directly in the result now let's say 16482 14:09:37,916 --> 14:09:43,000 that after this we want to show the 16483 14:09:40,000 --> 14:09:47,116 month according to the documentation to 16484 14:09:43,000 --> 14:09:50,480 show the month we can type for Capital 16485 14:09:47,116 --> 14:09:54,436 M's and it will replace this with the 16486 14:09:50,480 --> 14:09:56,640 month and finally we'll type a space and 16487 14:09:54,436 --> 14:09:59,080 according to the documentation if we 16488 14:09:56,640 --> 14:10:03,560 want to insert the day of the month into 16489 14:09:59,080 --> 14:10:06,000 the string we can type a capital D 16490 14:10:03,560 --> 14:10:08,840 so this method takes this date and 16491 14:10:06,000 --> 14:10:11,680 converts it into a string with this 16492 14:10:08,840 --> 14:10:15,640 format so now let's console.log this to 16493 14:10:11,680 --> 14:10:17,480 see what it looks like well 16494 14:10:15,640 --> 14:10:19,560 console.log and 16495 14:10:17,480 --> 14:10:22,840 brackets and 16496 14:10:19,560 --> 14:10:24,596 save and then go into our project and 16497 14:10:22,840 --> 14:10:27,320 open the 16498 14:10:24,596 --> 14:10:30,436 console and now you should see the date 16499 14:10:27,320 --> 14:10:33,436 7 days after today but displayed in an 16500 14:10:30,436 --> 14:10:36,000 easyto read format 16501 14:10:33,436 --> 14:10:39,320 so that's how we use the DJs external 16502 14:10:36,000 --> 14:10:41,720 library to help us work with dates and 16503 14:10:39,320 --> 14:10:43,880 as you can see it saves us a lot of time 16504 14:10:41,720 --> 14:10:46,240 and work we don't have to figure out how 16505 14:10:43,880 --> 14:10:49,000 to work with dates ourselves we can just 16506 14:10:46,240 --> 14:10:51,040 use someone else's code to solve this 16507 14:10:49,000 --> 14:10:53,436 problem so a best practice in 16508 14:10:51,040 --> 14:10:55,956 programming is when we need to do 16509 14:10:53,436 --> 14:10:58,680 something complicated try to find an 16510 14:10:55,956 --> 14:10:59,956 external Library first before writing 16511 14:10:58,680 --> 14:11:03,080 the code 16512 14:10:59,956 --> 14:11:05,796 ourselves to find external libraries we 16513 14:11:03,080 --> 14:11:07,596 can you can search in Google JavaScript 16514 14:11:05,796 --> 14:11:10,520 and then what kind of Library we're 16515 14:11:07,596 --> 14:11:13,756 looking for like date 16516 14:11:10,520 --> 14:11:16,720 Library we can also use an AI tool to 16517 14:11:13,756 --> 14:11:18,916 help us find external 16518 14:11:16,720 --> 14:11:21,560 libraries next we're going to learn how 16519 14:11:18,916 --> 14:11:24,916 to use external libraries and JavaScript 16520 14:11:21,560 --> 14:11:28,200 modules together right now if we look at 16521 14:11:24,916 --> 14:11:31,200 our code and go to checkout. 16522 14:11:28,200 --> 14:11:34,080 HTML we're loading these libraries using 16523 14:11:31,200 --> 14:11:36,596 script tags the problem with script tags 16524 14:11:34,080 --> 14:11:39,360 is that it runs this code directly on 16525 14:11:36,596 --> 14:11:42,560 the page and this might cause naming 16526 14:11:39,360 --> 14:11:45,320 conflicts for example the hello Library 16527 14:11:42,560 --> 14:11:47,520 creates a function called hello so if 16528 14:11:45,320 --> 14:11:50,520 something else is called hello in any of 16529 14:11:47,520 --> 14:11:53,276 our other code it might cause a naming 16530 14:11:50,520 --> 14:11:56,596 conflict a better way to load external 16531 14:11:53,276 --> 14:11:59,116 libraries is to use JavaScript modules 16532 14:11:56,596 --> 14:12:02,240 because a module will contain this code 16533 14:11:59,116 --> 14:12:04,796 inside a file and any variables that are 16534 14:12:02,240 --> 14:12:08,276 created inside inside the file will not 16535 14:12:04,796 --> 14:12:11,160 conflict with anything outside of the 16536 14:12:08,276 --> 14:12:13,276 file to use external libraries and 16537 14:12:11,160 --> 14:12:16,200 JavaScript modules together we're going 16538 14:12:13,276 --> 14:12:20,000 to use a special version of the library 16539 14:12:16,200 --> 14:12:23,080 called an esm version esm stands for 16540 14:12:20,000 --> 14:12:26,400 ecmascript modules ecmascript is just 16541 14:12:23,080 --> 14:12:28,956 another name for JavaScript so the esm 16542 14:12:26,400 --> 14:12:31,560 version of a library is just a version 16543 14:12:28,956 --> 14:12:34,796 that works with JavaScript 16544 14:12:31,560 --> 14:12:37,040 modules so let's do an example we're 16545 14:12:34,796 --> 14:12:41,680 going to go to the video description and 16546 14:12:37,040 --> 14:12:41,680 we're going to open the esm version of 16547 14:12:48,080 --> 14:12:53,040 hello.js so you can see that it's the 16548 14:12:50,240 --> 14:12:56,320 exact same code as before we're creating 16549 14:12:53,040 --> 14:12:58,916 a function called hello except this time 16550 14:12:56,320 --> 14:13:01,640 I just added the word export in front of 16551 14:12:58,916 --> 14:13:04,840 function so this turns the library into 16552 14:13:01,640 --> 14:13:08,116 an esm version and now it will work with 16553 14:13:04,840 --> 14:13:10,560 JavaScript modules so let's use the esm 16554 14:13:08,116 --> 14:13:11,756 version in our code we're going to go 16555 14:13:10,560 --> 14:13:14,116 back to our 16556 14:13:11,756 --> 14:13:16,160 code and we're not going to use script 16557 14:13:14,116 --> 14:13:17,400 tags to load this anymore so let's 16558 14:13:16,160 --> 14:13:19,520 delete 16559 14:13:17,400 --> 14:13:24,320 this and 16560 14:13:19,520 --> 14:13:26,880 save and then in checkout. JS at the top 16561 14:13:24,320 --> 14:13:29,520 we're going to use an import to import 16562 14:13:26,880 --> 14:13:33,840 this function into our code so we'll 16563 14:13:29,520 --> 14:13:35,756 type import curly brackets we'll import 16564 14:13:33,840 --> 14:13:38,840 the function 16565 14:13:35,756 --> 14:13:42,276 hello and we're going to import it from 16566 14:13:38,840 --> 14:13:44,916 a string and then this time instead of 16567 14:13:42,276 --> 14:13:47,796 giving it a file path like before we're 16568 14:13:44,916 --> 14:13:51,080 going to give it a URL that contains our 16569 14:13:47,796 --> 14:13:54,756 code so on the web page we're going to 16570 14:13:51,080 --> 14:13:57,840 rightclick and copy the URL at the top 16571 14:13:54,756 --> 14:13:59,956 and then inside our code in here we're 16572 14:13:57,840 --> 14:14:02,640 going to rightclick and 16573 14:13:59,956 --> 14:14:05,320 paste so this import works the same way 16574 14:14:02,640 --> 14:14:07,640 as before before except now the codee is 16575 14:14:05,320 --> 14:14:10,360 coming from the internet rather than one 16576 14:14:07,640 --> 14:14:14,240 of our files so let's 16577 14:14:10,360 --> 14:14:15,680 save and then go back to our project and 16578 14:14:14,240 --> 14:14:17,956 check the 16579 14:14:15,680 --> 14:14:19,200 console and you can see that everything 16580 14:14:17,956 --> 14:14:21,796 is still 16581 14:14:19,200 --> 14:14:23,916 working so that's how we use external 16582 14:14:21,796 --> 14:14:27,160 libraries and JavaScript modules 16583 14:14:23,916 --> 14:14:30,320 together we just load the esm version of 16584 14:14:27,160 --> 14:14:32,596 the library and we use import as normal 16585 14:14:30,320 --> 14:14:35,436 except this time we're importing from 16586 14:14:32,596 --> 14:14:38,560 code that is on the 16587 14:14:35,436 --> 14:14:41,200 internet next let's use DJs with 16588 14:14:38,560 --> 14:14:44,840 JavaScript modules to do that we're 16589 14:14:41,200 --> 14:14:45,956 going to load the esm version of DJs so 16590 14:14:44,840 --> 14:14:48,480 we're going to go to the video 16591 14:14:45,956 --> 14:14:51,840 description and click this link for the 16592 14:14:48,480 --> 14:14:51,840 DJs esm 16593 14:14:58,436 --> 14:15:03,840 version and now we're going to load this 16594 14:15:00,880 --> 14:15:07,160 code into our project so let's go back 16595 14:15:03,840 --> 14:15:08,640 into our codee and first let's open 16596 14:15:07,160 --> 14:15:10,956 checkout. 16597 14:15:08,640 --> 14:15:12,720 HTML and we're going to remove this 16598 14:15:10,956 --> 14:15:15,320 script tag because we're going to load 16599 14:15:12,720 --> 14:15:19,116 this with JavaScript modules so let's 16600 14:15:15,320 --> 14:15:21,756 save and then go into checkout. JS and 16601 14:15:19,116 --> 14:15:25,880 then at the top we're going to import 16602 14:15:21,756 --> 14:15:28,276 from the DJs esm version so we'll 16603 14:15:25,880 --> 14:15:30,360 import and we're going to use a slightly 16604 14:15:28,276 --> 14:15:31,916 different syntax we're just going to 16605 14:15:30,360 --> 14:15:35,480 type 16606 14:15:31,916 --> 14:15:38,400 DJs without the curly brackets and then 16607 14:15:35,480 --> 14:15:40,400 we'll type from a 16608 14:15:38,400 --> 14:15:43,240 string and we're going to give it the 16609 14:15:40,400 --> 14:15:44,080 URL that contains the code so we'll go 16610 14:15:43,240 --> 14:15:46,680 to our 16611 14:15:44,080 --> 14:15:50,080 browser and then click the URL at the 16612 14:15:46,680 --> 14:15:53,480 top we're going to right click 16613 14:15:50,080 --> 14:15:56,680 copy and then in our code inside these 16614 14:15:53,480 --> 14:15:59,436 quotes rightclick and 16615 14:15:56,680 --> 14:16:02,320 paste and 16616 14:15:59,436 --> 14:16:03,840 save so now let's open our project and 16617 14:16:02,320 --> 14:16:07,840 take a look at the the console to make 16618 14:16:03,840 --> 14:16:09,840 sure it works so we'll right click 16619 14:16:07,840 --> 14:16:12,916 inspect the 16620 14:16:09,840 --> 14:16:15,520 console and everything is still working 16621 14:16:12,916 --> 14:16:18,596 so that's how we use the DJs external 16622 14:16:15,520 --> 14:16:20,640 library with JavaScript modules so 16623 14:16:18,596 --> 14:16:23,560 before we continue we're going to learn 16624 14:16:20,640 --> 14:16:26,276 about this new syntax that we used here 16625 14:16:23,560 --> 14:16:29,320 so this syntax is called a default 16626 14:16:26,276 --> 14:16:32,320 export a default export is another way 16627 14:16:29,320 --> 14:16:34,640 of exporting something from a file we 16628 14:16:32,320 --> 14:16:37,160 can use it when we only want to export 16629 14:16:34,640 --> 14:16:39,240 one thing from a file and it makes the 16630 14:16:37,160 --> 14:16:42,160 syntax a little bit cleaner because we 16631 14:16:39,240 --> 14:16:44,596 don't have to type the curly brackets so 16632 14:16:42,160 --> 14:16:47,400 let's do an example of a default export 16633 14:16:44,596 --> 14:16:50,756 to see how it works we're going to open 16634 14:16:47,400 --> 14:16:53,276 the utils folder and open money. 16635 14:16:50,756 --> 14:16:57,200 JS and at the bottom we're going to 16636 14:16:53,276 --> 14:17:00,756 create a default export by typing 16637 14:16:57,200 --> 14:17:03,240 export default and then whatever we want 16638 14:17:00,756 --> 14:17:05,756 to export so let's say we want to export 16639 14:17:03,240 --> 14:17:09,320 this function here so we just type the 16640 14:17:05,756 --> 14:17:11,880 name of the function format 16641 14:17:09,320 --> 14:17:14,956 currency and 16642 14:17:11,880 --> 14:17:17,320 save and now when we import this we can 16643 14:17:14,956 --> 14:17:20,116 just import the name without the curly 16644 14:17:17,320 --> 14:17:23,160 brackets so the syntax is a little bit 16645 14:17:20,116 --> 14:17:25,756 cleaner now each file can only have one 16646 14:17:23,160 --> 14:17:28,916 default export so if we only want to 16647 14:17:25,756 --> 14:17:31,116 export one thing from a file this is 16648 14:17:28,916 --> 14:17:34,116 another syntax that we can 16649 14:17:31,116 --> 14:17:36,436 use so some people like using default 16650 14:17:34,116 --> 14:17:38,756 exports because they feel that exporting 16651 14:17:36,436 --> 14:17:41,840 only one thing from a file makes the 16652 14:17:38,756 --> 14:17:44,116 code cleaner however in your own code 16653 14:17:41,840 --> 14:17:45,880 it's up to you which version of export 16654 14:17:44,116 --> 14:17:49,276 you want to 16655 14:17:45,880 --> 14:17:53,320 use let's go back to checkout. 16656 14:17:49,276 --> 14:17:56,040 JS and then here the DJs Library only 16657 14:17:53,320 --> 14:17:59,040 exports one thing which is the DJs 16658 14:17:56,040 --> 14:18:01,596 function so they chose to use a default 16659 14:17:59,040 --> 14:18:03,360 export instead of a normal export that 16660 14:18:01,596 --> 14:18:05,480 we were using before 16661 14:18:03,360 --> 14:18:08,596 by the way the syntax up here with the 16662 14:18:05,480 --> 14:18:11,596 curly brackets is called a named 16663 14:18:08,596 --> 14:18:14,080 export so when we use external libraries 16664 14:18:11,596 --> 14:18:16,596 in the future some libraries will use 16665 14:18:14,080 --> 14:18:19,116 named exports like this and other 16666 14:18:16,596 --> 14:18:22,240 libraries will use a default export like 16667 14:18:19,116 --> 14:18:24,560 this so it's useful to know both 16668 14:18:22,240 --> 14:18:26,596 syntaxes so that's how we use external 16669 14:18:24,560 --> 14:18:29,560 libraries and JavaScript modules 16670 14:18:26,596 --> 14:18:32,436 together we just import from the esm 16671 14:18:29,560 --> 14:18:34,596 version of the library now keep in mind 16672 14:18:32,436 --> 14:18:37,040 that not every library has an esm 16673 14:18:34,596 --> 14:18:39,000 version so for some libraries we still 16674 14:18:37,040 --> 14:18:41,916 have to use a script 16675 14:18:39,000 --> 14:18:44,520 tag okay now that we learned external 16676 14:18:41,916 --> 14:18:46,596 libraries let's do some practical 16677 14:18:44,520 --> 14:18:49,756 examples we're going to go back to the 16678 14:18:46,596 --> 14:18:52,680 checkout page and we're going to use DJs 16679 14:18:49,756 --> 14:18:54,880 to create the delivery options here so 16680 14:18:52,680 --> 14:18:57,480 the way this works is that we can select 16681 14:18:54,880 --> 14:18:59,720 one of three delivery options when we 16682 14:18:57,480 --> 14:19:02,240 click an option it should change the 16683 14:18:59,720 --> 14:19:05,400 delivery date up here and it should 16684 14:19:02,240 --> 14:19:07,796 change the prices on the right side so 16685 14:19:05,400 --> 14:19:10,480 let's get started as always we're going 16686 14:19:07,796 --> 14:19:12,480 to do three steps first we're going to 16687 14:19:10,480 --> 14:19:15,276 save the data and then we're going to 16688 14:19:12,480 --> 14:19:18,320 generate the HTML and finally we're 16689 14:19:15,276 --> 14:19:20,756 going to make it interactive so first 16690 14:19:18,320 --> 14:19:23,720 what data do we need to save for this 16691 14:19:20,756 --> 14:19:27,840 section well each delivery option has a 16692 14:19:23,720 --> 14:19:29,840 delivery time like 7 days or 3 days and 16693 14:19:27,840 --> 14:19:33,116 each option also has a 16694 14:19:29,840 --> 14:19:35,596 price so right now here's what our cart 16695 14:19:33,116 --> 14:19:38,276 looks like now we could save the 16696 14:19:35,596 --> 14:19:41,360 delivery option details inside the cart 16697 14:19:38,276 --> 14:19:43,916 like this however notice that this data 16698 14:19:41,360 --> 14:19:46,436 gets duplicated for each product in the 16699 14:19:43,916 --> 14:19:47,956 cart instead we're going to save the 16700 14:19:46,436 --> 14:19:50,956 delivery options 16701 14:19:47,956 --> 14:19:53,360 separately and then just save an ID that 16702 14:19:50,956 --> 14:19:56,200 points to the delivery 16703 14:19:53,360 --> 14:19:59,200 option as a reminder this technique is 16704 14:19:56,200 --> 14:20:01,560 called normalizing the data we save the 16705 14:19:59,200 --> 14:20:03,240 delivery options separately and then we 16706 14:20:01,560 --> 14:20:06,000 just save an ID 16707 14:20:03,240 --> 14:20:08,640 that points to the full delivery 16708 14:20:06,000 --> 14:20:12,480 option so let's create the data for the 16709 14:20:08,640 --> 14:20:15,040 delivery options we'll go into our code 16710 14:20:12,480 --> 14:20:17,680 and then in the data folder let's create 16711 14:20:15,040 --> 14:20:22,916 a new file for this and we're going to 16712 14:20:17,680 --> 14:20:23,916 name this file delivery options. JS and 16713 14:20:22,916 --> 14:20:26,880 press 16714 14:20:23,916 --> 14:20:29,720 enter and in here we'll create a list of 16715 14:20:26,880 --> 14:20:33,520 delivery options that we can pick from 16716 14:20:29,720 --> 14:20:36,916 we'll create a variable const delivery 16717 14:20:33,520 --> 14:20:39,840 options equals and array because we want 16718 14:20:36,916 --> 14:20:42,040 a list and then each delivery option is 16719 14:20:39,840 --> 14:20:45,116 going to have multiple values like the 16720 14:20:42,040 --> 14:20:46,520 time to deliver as well as the price so 16721 14:20:45,116 --> 14:20:50,160 we're going to make each of them an 16722 14:20:46,520 --> 14:20:52,040 object to group these values together so 16723 14:20:50,160 --> 14:20:53,880 I'm going to rearrange my windows here 16724 14:20:52,040 --> 14:20:56,360 so that we can see the data that we're 16725 14:20:53,880 --> 14:21:00,360 trying to save and we can actually close 16726 14:20:56,360 --> 14:21:00,360 these tabs for now 16727 14:21:03,400 --> 14:21:06,956 so here let's save the data for the 16728 14:21:05,436 --> 14:21:10,040 first delivery 16729 14:21:06,956 --> 14:21:12,360 option first let's give it an ID 16730 14:21:10,040 --> 14:21:14,956 property because as we mentioned we're 16731 14:21:12,360 --> 14:21:16,916 going to save the ID in the cart and 16732 14:21:14,956 --> 14:21:20,160 then we're going to use that ID to get 16733 14:21:16,916 --> 14:21:22,040 the full delivery option here so to keep 16734 14:21:20,160 --> 14:21:24,520 things simple let's just give it a 16735 14:21:22,040 --> 14:21:26,560 string and an ID of 16736 14:21:24,520 --> 14:21:28,756 one and then we're going to give it 16737 14:21:26,560 --> 14:21:31,436 another property and we're going to save 16738 14:21:28,756 --> 14:21:33,840 the delivery time so for the first 16739 14:21:31,436 --> 14:21:37,040 option it's going to take seven days to 16740 14:21:33,840 --> 14:21:42,596 deliver so let's create a property 16741 14:21:37,040 --> 14:21:45,116 delivery days colon 7 and comma and 16742 14:21:42,596 --> 14:21:47,080 we're also going to save the price so 16743 14:21:45,116 --> 14:21:52,680 this one is free so we're going to save 16744 14:21:47,080 --> 14:21:54,520 the price cents and zero remember that a 16745 14:21:52,680 --> 14:21:56,680 best practice when calculating money is 16746 14:21:54,520 --> 14:21:59,480 to save it in 16747 14:21:56,680 --> 14:22:01,796 cents and next let's save the data for 16748 14:21:59,480 --> 14:22:04,796 the second delivery option here we'll 16749 14:22:01,796 --> 14:22:09,160 type a comma and another object and 16750 14:22:04,796 --> 14:22:11,000 let's give it an ID of two this time and 16751 14:22:09,160 --> 14:22:15,116 this one is going to take three days to 16752 14:22:11,000 --> 14:22:19,840 deliver so we'll type delivery days 16753 14:22:15,116 --> 14:22:21,400 colon 3 and we'll give it a price cents 16754 14:22:19,840 --> 14:22:23,956 and this one is 16755 14:22:21,400 --> 14:22:26,080 $4.99 so 16756 14:22:23,956 --> 14:22:27,756 $4.99 and then finally we're going to 16757 14:22:26,080 --> 14:22:33,480 save the data for the third delivery 16758 14:22:27,756 --> 14:22:35,596 option comma another object ID of three 16759 14:22:33,480 --> 14:22:39,640 and this one is going to take one day to 16760 14:22:35,596 --> 14:22:43,880 deliver so we'll type delivery days 16761 14:22:39,640 --> 14:22:50,160 colon 1 comma and the price sense is 16762 14:22:43,880 --> 14:22:52,596 $9.99 so price sense 999 and 16763 14:22:50,160 --> 14:22:55,240 save so now that we've created our 16764 14:22:52,596 --> 14:22:58,080 delivery options for each product in the 16765 14:22:55,240 --> 14:23:00,680 cart we're going to save the ID of the 16766 14:22:58,080 --> 14:23:02,640 delivery option that's selected so for 16767 14:23:00,680 --> 14:23:05,596 example here we selected the first First 16768 14:23:02,640 --> 14:23:08,596 Option we're going to save an ID of one 16769 14:23:05,596 --> 14:23:13,000 in our cart so let's go back into our 16770 14:23:08,596 --> 14:23:13,000 code and we're going to open the 16771 14:23:14,200 --> 14:23:23,080 cart so here in the default cart we're 16772 14:23:17,680 --> 14:23:25,400 going to save the delivery option ID 16773 14:23:23,080 --> 14:23:27,640 colon and let's say that the first 16774 14:23:25,400 --> 14:23:31,956 product selected the first delivery 16775 14:23:27,640 --> 14:23:34,116 option so we're going to save the ID one 16776 14:23:31,956 --> 14:23:38,756 and then for the second product we're 16777 14:23:34,116 --> 14:23:40,720 also going to save a delivery option ID 16778 14:23:38,756 --> 14:23:42,680 colon and then let's say that for the 16779 14:23:40,720 --> 14:23:45,320 second product we chose the second 16780 14:23:42,680 --> 14:23:48,116 delivery option here so here we're going 16781 14:23:45,320 --> 14:23:51,116 to give it an ID of 16782 14:23:48,116 --> 14:23:53,480 two next when we add a product to the 16783 14:23:51,116 --> 14:23:56,480 cart we also need to give it a delivery 16784 14:23:53,480 --> 14:23:59,640 option ID so if we scroll down to the 16785 14:23:56,480 --> 14:24:02,040 add to cart function here we're adding a 16786 14:23:59,640 --> 14:24:03,956 product to the cart so when we add a new 16787 14:24:02,040 --> 14:24:06,840 product we also want to give it a 16788 14:24:03,956 --> 14:24:11,796 delivery option ID so we'll type 16789 14:24:06,840 --> 14:24:13,640 delivery option ID colon and for new 16790 14:24:11,796 --> 14:24:16,160 products let's give it a default 16791 14:24:13,640 --> 14:24:19,436 delivery option of one so for new 16792 14:24:16,160 --> 14:24:20,560 products to select the first one and now 16793 14:24:19,436 --> 14:24:23,720 let's 16794 14:24:20,560 --> 14:24:26,520 save and lastly we might have a cart 16795 14:24:23,720 --> 14:24:29,040 that is saved in local storage so we'll 16796 14:24:26,520 --> 14:24:31,640 need to add the delivery option ID to 16797 14:24:29,040 --> 14:24:34,360 local storage as well but to keep things 16798 14:24:31,640 --> 14:24:36,116 simple let's just do a shortcut we're 16799 14:24:34,360 --> 14:24:38,200 going to delete the cart in local 16800 14:24:36,116 --> 14:24:40,520 storage and then it will go back to 16801 14:24:38,200 --> 14:24:42,000 using this default value which has the 16802 14:24:40,520 --> 14:24:44,596 delivery option 16803 14:24:42,000 --> 14:24:47,436 IDs so to delete the cart from local 16804 14:24:44,596 --> 14:24:51,160 storage we're going to go into our page 16805 14:24:47,436 --> 14:24:53,796 and open the console right click 16806 14:24:51,160 --> 14:24:58,080 inspect the 16807 14:24:53,796 --> 14:25:02,680 console and we're going to type local 16808 14:24:58,080 --> 14:25:05,040 storage. remove item brackets and the 16809 14:25:02,680 --> 14:25:07,720 cart and press 16810 14:25:05,040 --> 14:25:09,680 enter so this will erase the cart from 16811 14:25:07,720 --> 14:25:12,480 local storage and it will use those 16812 14:25:09,680 --> 14:25:14,720 default values so now let's go to our 16813 14:25:12,480 --> 14:25:16,916 page and 16814 14:25:14,720 --> 14:25:19,680 refresh and it should be using the 16815 14:25:16,916 --> 14:25:22,880 default cart so that's how we save the 16816 14:25:19,680 --> 14:25:25,596 data for the delivery 16817 14:25:22,880 --> 14:25:27,796 options by the way in the rest of this 16818 14:25:25,596 --> 14:25:30,956 course if you get an error in your 16819 14:25:27,796 --> 14:25:33,756 project but your code looks correct you 16820 14:25:30,956 --> 14:25:37,160 may have some bad data saved in local 16821 14:25:33,756 --> 14:25:40,640 storage to fix this you can try running 16822 14:25:37,160 --> 14:25:42,640 local storage. CLE in your console to 16823 14:25:40,640 --> 14:25:46,796 remove everything from local 16824 14:25:42,640 --> 14:25:46,796 storage and then refresh the 16825 14:25:48,360 --> 14:25:55,840 page the next step is to generate the 16826 14:25:52,840 --> 14:25:58,880 HTML first let's find the HTML for the 16827 14:25:55,840 --> 14:26:01,520 delivery options here to do that we can 16828 14:25:58,880 --> 14:26:03,360 rightclick this part of the page and 16829 14:26:01,520 --> 14:26:05,720 inspect 16830 14:26:03,360 --> 14:26:08,276 and this will give us the HTML for this 16831 14:26:05,720 --> 14:26:10,520 part so we're going to find the element 16832 14:26:08,276 --> 14:26:12,596 that represents this part of the page 16833 14:26:10,520 --> 14:26:15,560 and that's this one right here it's a 16834 14:26:12,596 --> 14:26:17,276 div with the class delivery Das options 16835 14:26:15,560 --> 14:26:19,956 and to find this in our code we're going 16836 14:26:17,276 --> 14:26:21,360 to search for this class so let's go 16837 14:26:19,956 --> 14:26:23,560 back to our 16838 14:26:21,360 --> 14:26:26,840 code and then we're going to open 16839 14:26:23,560 --> 14:26:30,040 checkout. JS which is right 16840 14:26:26,840 --> 14:26:32,320 here and we'll press crlf or command F 16841 14:26:30,040 --> 14:26:35,916 to find code and we're going to look for 16842 14:26:32,320 --> 14:26:38,596 the class delivery Das 16843 14:26:35,916 --> 14:26:41,560 options and it will give us this code 16844 14:26:38,596 --> 14:26:42,796 right here so this is the HTML for the 16845 14:26:41,560 --> 14:26:45,160 delivery 16846 14:26:42,796 --> 14:26:47,680 options so now instead of writing this 16847 14:26:45,160 --> 14:26:48,840 HTML directly we're going to generate 16848 14:26:47,680 --> 14:26:51,400 this with 16849 14:26:48,840 --> 14:26:53,560 JavaScript to keep our code organized 16850 14:26:51,400 --> 14:26:54,360 let's create a function for generating 16851 14:26:53,560 --> 14:26:57,040 this 16852 14:26:54,360 --> 14:27:01,116 HTML we'll scroll to the 16853 14:26:57,040 --> 14:27:04,640 bottom and then here we'll create a 16854 14:27:01,116 --> 14:27:06,320 function and let's call it delivery 16855 14:27:04,640 --> 14:27:10,000 options 16856 14:27:06,320 --> 14:27:12,560 HTML brackets and curly 16857 14:27:10,000 --> 14:27:15,200 brackets and now inside here we're going 16858 14:27:12,560 --> 14:27:17,200 to do three steps first we're going to 16859 14:27:15,200 --> 14:27:20,080 Loop through the delivery options we 16860 14:27:17,200 --> 14:27:22,756 created earlier and then for each option 16861 14:27:20,080 --> 14:27:24,596 we're going to generate some HTML and 16862 14:27:22,756 --> 14:27:28,276 then finally we're going to combine all 16863 14:27:24,596 --> 14:27:30,880 the HTML together so first let's import 16864 14:27:28,276 --> 14:27:34,200 the delivery options at the top so we'll 16865 14:27:30,880 --> 14:27:39,116 scroll up and we're going to 16866 14:27:34,200 --> 14:27:40,640 import curly brackets from quotes and 16867 14:27:39,116 --> 14:27:43,436 we're going to locate the delivery 16868 14:27:40,640 --> 14:27:45,520 options file which is inside the data 16869 14:27:43,436 --> 14:27:47,756 folder so first we need to get out of 16870 14:27:45,520 --> 14:27:51,040 the scripts folder we'll type dot do 16871 14:27:47,756 --> 14:27:55,840 slash and then go into the data folder 16872 14:27:51,040 --> 14:27:59,436 slash delivery options. JS and we're 16873 14:27:55,840 --> 14:28:02,160 going to import the delivery 16874 14:27:59,436 --> 14:28:04,756 options now one thing I forgot to do is 16875 14:28:02,160 --> 14:28:06,916 that inside these delivery options we 16876 14:28:04,756 --> 14:28:10,520 need to make sure that we export this 16877 14:28:06,916 --> 14:28:11,596 variable so we're going to type export 16878 14:28:10,520 --> 14:28:15,320 and 16879 14:28:11,596 --> 14:28:17,520 save and we'll go back to checkout. JS 16880 14:28:15,320 --> 14:28:19,880 and then here we're going to Loop 16881 14:28:17,520 --> 14:28:21,560 through the delivery options below so 16882 14:28:19,880 --> 14:28:24,360 we'll scroll back 16883 14:28:21,560 --> 14:28:30,200 down to this new 16884 14:28:24,360 --> 14:28:35,080 function and type delivery options. for 16885 14:28:30,200 --> 14:28:35,080 each brackets and a function 16886 14:28:35,200 --> 14:28:40,360 inside and we'll name the parameter 16887 14:28:38,000 --> 14:28:43,360 delivery 16888 14:28:40,360 --> 14:28:46,320 option so for each delivery option we're 16889 14:28:43,360 --> 14:28:48,720 going to generate some HTML so let's use 16890 14:28:46,320 --> 14:28:51,796 a template string to save the 16891 14:28:48,720 --> 14:28:53,596 HTML and then we're going to scroll up 16892 14:28:51,796 --> 14:28:57,116 and we're going to find the class 16893 14:28:53,596 --> 14:28:59,480 delivery Das option so this is the HTML 16894 14:28:57,116 --> 14:29:02,040 for one delivery option so we're just 16895 14:28:59,480 --> 14:29:06,276 going to copy this HTML into our Java 16896 14:29:02,040 --> 14:29:10,436 JavaScript below so we'll right click 16897 14:29:06,276 --> 14:29:13,560 copy and then down here inside the 16898 14:29:10,436 --> 14:29:15,840 string give some new lines and 16899 14:29:13,560 --> 14:29:18,720 rightclick and 16900 14:29:15,840 --> 14:29:22,160 paste and if we need to reformat we can 16901 14:29:18,720 --> 14:29:22,160 select these lines and press 16902 14:29:26,116 --> 14:29:31,720 tab okay now when we're generating the 16903 14:29:29,116 --> 14:29:34,320 HTML instead of using the same date 16904 14:29:31,720 --> 14:29:38,040 every time we're going to calculate the 16905 14:29:34,320 --> 14:29:40,560 date using DJs and then insert it into 16906 14:29:38,040 --> 14:29:43,480 here to calculate the date we're going 16907 14:29:40,560 --> 14:29:47,040 to get today's date first using 16908 14:29:43,480 --> 14:29:49,756 DJs so up here let's create a variable 16909 14:29:47,040 --> 14:29:51,720 const today 16910 14:29:49,756 --> 14:29:54,400 equals and according to the 16911 14:29:51,720 --> 14:29:58,160 documentation to get today's date we can 16912 14:29:54,400 --> 14:30:00,640 just call the DJs function so we'll type 16913 14:29:58,160 --> 14:30:02,560 DJs and 16914 14:30:00,640 --> 14:30:04,480 brackets next 16915 14:30:02,560 --> 14:30:07,160 each delivery option takes a certain 16916 14:30:04,480 --> 14:30:10,640 amount of days to deliver so we need to 16917 14:30:07,160 --> 14:30:14,560 add that to today's date to do that we 16918 14:30:10,640 --> 14:30:16,560 have a method called add So Below this 16919 14:30:14,560 --> 14:30:21,680 let's create a variable to save the 16920 14:30:16,560 --> 14:30:23,360 result const delivery date 16921 14:30:21,680 --> 14:30:27,000 equals 16922 14:30:23,360 --> 14:30:29,116 today. add 16923 14:30:27,000 --> 14:30:31,480 brackets and we're going to give it two 16924 14:30:29,116 --> 14:30:34,040 parameters the first parameter is how 16925 14:30:31,480 --> 14:30:37,240 many dat days we want to add and this is 16926 14:30:34,040 --> 14:30:41,520 saved in the delivery option so here 16927 14:30:37,240 --> 14:30:44,400 we'll give it the delivery option dot 16928 14:30:41,520 --> 14:30:46,796 delivery days 16929 14:30:44,400 --> 14:30:50,956 comma and then we'll give it the length 16930 14:30:46,796 --> 14:30:53,720 of time we want to add which is a string 16931 14:30:50,956 --> 14:30:56,560 days now that we calculated the correct 16932 14:30:53,720 --> 14:31:00,160 delivery date we need to display it in 16933 14:30:56,560 --> 14:31:03,116 an easyto read format and again DJs has 16934 14:31:00,160 --> 14:31:06,640 a method called format that Let's us do 16935 14:31:03,116 --> 14:31:10,756 this so below let's create a variable 16936 14:31:06,640 --> 14:31:14,000 const and we'll call it date string and 16937 14:31:10,756 --> 14:31:15,640 make it equal to delivery 16938 14:31:14,000 --> 14:31:19,880 date. 16939 14:31:15,640 --> 14:31:23,200 format brackets and will give it a 16940 14:31:19,880 --> 14:31:26,160 string so the format we want is we want 16941 14:31:23,200 --> 14:31:28,000 to insert the day of the week so 16942 14:31:26,160 --> 14:31:29,200 according to the documentation we can 16943 14:31:28,000 --> 14:31:33,276 use 16944 14:31:29,200 --> 14:31:35,520 4ds and then a comma and a space and 16945 14:31:33,276 --> 14:31:38,160 then we want to insert the month and 16946 14:31:35,520 --> 14:31:39,080 according to the documentation that's 4 16947 14:31:38,160 --> 14:31:41,756 Capital 16948 14:31:39,080 --> 14:31:44,796 M's and finally we're going to insert 16949 14:31:41,756 --> 14:31:47,560 the day of month which is capital 16950 14:31:44,796 --> 14:31:50,160 D so now that we calculated this date 16951 14:31:47,560 --> 14:31:52,880 string we're going to insert it into the 16952 14:31:50,160 --> 14:31:56,040 HTML instead of using the same date 16953 14:31:52,880 --> 14:31:59,436 every time so we'll remove this and then 16954 14:31:56,040 --> 14:32:02,480 insert the date 16955 14:31:59,436 --> 14:32:03,596 string next let's insert the price 16956 14:32:02,480 --> 14:32:06,720 because it's going to be different 16957 14:32:03,596 --> 14:32:10,160 prices every time so at the 16958 14:32:06,720 --> 14:32:14,276 top let's create a variable const and 16959 14:32:10,160 --> 14:32:17,116 we'll call it price string and make it 16960 14:32:14,276 --> 14:32:19,916 equals and remember each delivery option 16961 14:32:17,116 --> 14:32:22,200 has a property price sense so if the 16962 14:32:19,916 --> 14:32:25,200 price ense is zero we want to display 16963 14:32:22,200 --> 14:32:27,436 the text free if the price sense is not 16964 14:32:25,200 --> 14:32:29,080 zero we want to display the price as 16965 14:32:27,436 --> 14:32:32,160 dollars with a 16966 14:32:29,080 --> 14:32:35,520 dash so to do that let's practice using 16967 14:32:32,160 --> 14:32:38,880 a Turner operator so first we'll check 16968 14:32:35,520 --> 14:32:44,080 if the price sense is zero so we'll type 16969 14:32:38,880 --> 14:32:45,756 delivery option. price sense is triple 16970 14:32:44,080 --> 14:32:48,560 equals to 16971 14:32:45,756 --> 14:32:51,520 Z and then we'll type a new line and 16972 14:32:48,560 --> 14:32:54,640 question mark and also a new line and 16973 14:32:51,520 --> 14:32:57,880 colon so remember the way that a Turner 16974 14:32:54,640 --> 14:33:00,880 operator works is that if the first part 16975 14:32:57,880 --> 14:33:03,480 returns true the value is whatever is 16976 14:33:00,880 --> 14:33:05,956 after this question Mark if the first 16977 14:33:03,480 --> 14:33:08,520 part is false then the value is whatever 16978 14:33:05,956 --> 14:33:10,720 is after the colon so it's sort of like 16979 14:33:08,520 --> 14:33:12,200 an if statement except we can save the 16980 14:33:10,720 --> 14:33:15,436 result in a 16981 14:33:12,200 --> 14:33:17,480 variable so if the price sense is zero 16982 14:33:15,436 --> 14:33:20,360 that means it's free so we want to 16983 14:33:17,480 --> 14:33:22,240 display the text free so we'll create a 16984 14:33:20,360 --> 14:33:25,840 string 16985 14:33:22,240 --> 14:33:29,200 free and if the price sense is not zero 16986 14:33:25,840 --> 14:33:31,116 we want to display the price as dollars 16987 14:33:29,200 --> 14:33:34,116 so here we're going to type a template 16988 14:33:31,116 --> 14:33:37,200 string a dollar sign and we're going to 16989 14:33:34,116 --> 14:33:40,916 insert the price sents as 16990 14:33:37,200 --> 14:33:44,880 dollars so here we'll type delivery 16991 14:33:40,916 --> 14:33:46,916 option. price sense and to display this 16992 14:33:44,880 --> 14:33:50,116 as dollars we already have a function 16993 14:33:46,916 --> 14:33:52,436 for this called format currency so at 16994 14:33:50,116 --> 14:33:54,276 the front we're just going to type 16995 14:33:52,436 --> 14:33:58,116 format 16996 14:33:54,276 --> 14:34:00,796 currency Open Bracket and close 16997 14:33:58,116 --> 14:34:05,000 bracket and then at the end let's add a 16998 14:34:00,796 --> 14:34:06,680 dash so here we'll add space and dash 16999 14:34:05,000 --> 14:34:09,160 and 17000 14:34:06,680 --> 14:34:12,040 semicolon so that's how we create the 17001 14:34:09,160 --> 14:34:15,400 price string for each delivery option so 17002 14:34:12,040 --> 14:34:20,320 now let's substitute this into the HTML 17003 14:34:15,400 --> 14:34:25,080 so we'll remove this and substitute the 17004 14:34:20,320 --> 14:34:25,080 price string and 17005 14:34:25,320 --> 14:34:30,880 save so now instead of using these same 17006 14:34:28,320 --> 14:34:32,720 values every time we're substituting the 17007 14:34:30,880 --> 14:34:36,360 values in into the 17008 14:34:32,720 --> 14:34:38,360 HTML finally let's combine all this HTML 17009 14:34:36,360 --> 14:34:41,520 together so we're going to scroll up to 17010 14:34:38,360 --> 14:34:43,276 the top and outside of this loop we're 17011 14:34:41,520 --> 14:34:47,680 going to create a variable to save the 17012 14:34:43,276 --> 14:34:50,400 result so let HTML equals empty string 17013 14:34:47,680 --> 14:34:53,116 to start and then every time we Loop 17014 14:34:50,400 --> 14:34:56,560 through we're going to add this HTML to 17015 14:34:53,116 --> 14:34:58,756 this result so here we're going to type 17016 14:34:56,560 --> 14:35:01,756 HTML plus 17017 14:34:58,756 --> 14:35:04,000 equals and finally after we've created 17018 14:35:01,756 --> 14:35:07,200 this HTML we're going to return it from 17019 14:35:04,000 --> 14:35:09,756 this function so at the bottom we're 17020 14:35:07,200 --> 14:35:12,680 just going to return the 17021 14:35:09,756 --> 14:35:15,796 HTML and 17022 14:35:12,680 --> 14:35:18,880 save now that we generated the HTML for 17023 14:35:15,796 --> 14:35:21,000 the delivery options let's insert this 17024 14:35:18,880 --> 14:35:23,680 into the HTML 17025 14:35:21,000 --> 14:35:28,436 above so instead of creating this HTML 17026 14:35:23,680 --> 14:35:28,436 directly we're going to delete it 17027 14:35:32,240 --> 14:35:37,040 and then here we're going to insert and 17028 14:35:35,200 --> 14:35:40,360 we're going to run this function to 17029 14:35:37,040 --> 14:35:45,436 generate it so here we're going to type 17030 14:35:40,360 --> 14:35:48,560 delivery options HTML and 17031 14:35:45,436 --> 14:35:50,640 brackets now before we save one thing 17032 14:35:48,560 --> 14:35:53,796 that we're missing in this function is 17033 14:35:50,640 --> 14:35:56,956 that if we scroll down here we're using 17034 14:35:53,796 --> 14:35:59,360 a variable called matching product but 17035 14:35:56,956 --> 14:36:01,880 this variable is not accessible inside 17036 14:35:59,360 --> 14:36:05,116 this function so let's actually pass the 17037 14:36:01,880 --> 14:36:09,000 matching product into here so at the top 17038 14:36:05,116 --> 14:36:11,480 we're going to add a parameter matching 17039 14:36:09,000 --> 14:36:14,720 product and then when we call this 17040 14:36:11,480 --> 14:36:19,080 function up here we're going to pass in 17041 14:36:14,720 --> 14:36:20,956 the matching product so matching product 17042 14:36:19,080 --> 14:36:24,680 so now let's 17043 14:36:20,956 --> 14:36:27,040 save and check our page and now we're 17044 14:36:24,680 --> 14:36:29,320 generating the HTML for these delivery 17045 14:36:27,040 --> 14:36:31,520 options and you'll notice that your 17046 14:36:29,320 --> 14:36:34,116 delivery dates here will be based on 17047 14:36:31,520 --> 14:36:37,276 today's date or when you're watching 17048 14:36:34,116 --> 14:36:39,796 this video so this will be 7 days after 17049 14:36:37,276 --> 14:36:42,480 today or 7 days after when you're 17050 14:36:39,796 --> 14:36:45,160 watching this video this will be 3 days 17051 14:36:42,480 --> 14:36:47,480 after today or 3 days after when you're 17052 14:36:45,160 --> 14:36:49,720 watching this video and this will be one 17053 14:36:47,480 --> 14:36:52,276 day after 17054 14:36:49,720 --> 14:36:54,916 today now one thing we need to fix here 17055 14:36:52,276 --> 14:36:57,040 is that these selectors are empty 17056 14:36:54,916 --> 14:37:00,436 however we want the delivery option that 17057 14:36:57,040 --> 14:37:02,680 is saved in the cart to be checked to do 17058 14:37:00,436 --> 14:37:05,360 that let's go back to our 17059 14:37:02,680 --> 14:37:08,840 code and in order to make one of these 17060 14:37:05,360 --> 14:37:11,640 selectors selected we're going to scroll 17061 14:37:08,840 --> 14:37:13,596 down to the input 17062 14:37:11,640 --> 14:37:16,480 element and we're going to add an 17063 14:37:13,596 --> 14:37:19,000 attribute called 17064 14:37:16,480 --> 14:37:20,276 checked and this attribute makes a 17065 14:37:19,000 --> 14:37:22,840 selector 17066 14:37:20,276 --> 14:37:25,240 checked however we don't want all of 17067 14:37:22,840 --> 14:37:27,680 these delivery options to be checked we 17068 14:37:25,240 --> 14:37:30,240 only want it to be checked if it matches 17069 14:37:27,680 --> 14:37:32,956 the delivery option ID that is saved in 17070 14:37:30,240 --> 14:37:35,160 the cart so let's write some code up 17071 14:37:32,956 --> 14:37:37,360 here to figure out which delivery option 17072 14:37:35,160 --> 14:37:41,240 should be checked we're going to create 17073 14:37:37,360 --> 14:37:43,160 a variable const is 17074 14:37:41,240 --> 14:37:46,080 checked 17075 14:37:43,160 --> 14:37:49,596 equals and we only want it to be checked 17076 14:37:46,080 --> 14:37:53,360 if this delivery options ID matches the 17077 14:37:49,596 --> 14:37:59,756 delivery option ID in the cart so we'll 17078 14:37:53,360 --> 14:38:02,200 test if delivery option. ID is equal to 17079 14:37:59,756 --> 14:38:06,276 the cart item 17080 14:38:02,200 --> 14:38:09,080 do delivery option 17081 14:38:06,276 --> 14:38:12,276 ID now inside this function we actually 17082 14:38:09,080 --> 14:38:16,160 don't have access to cart item so let's 17083 14:38:12,276 --> 14:38:19,320 actually pass it in as a parameter cart 17084 14:38:16,160 --> 14:38:22,116 item up here and then when we call the 17085 14:38:19,320 --> 14:38:24,916 function this outer function has access 17086 14:38:22,116 --> 14:38:28,680 to the cart item so we're going to give 17087 14:38:24,916 --> 14:38:30,596 it the cart item so we're going to put 17088 14:38:28,680 --> 14:38:32,360 it into this function and then we can 17089 14:38:30,596 --> 14:38:34,956 use it down here 17090 14:38:32,360 --> 14:38:37,520 so now that we have this 17091 14:38:34,956 --> 14:38:40,360 variable instead of being checked all 17092 14:38:37,520 --> 14:38:42,916 the time we're going to remove this and 17093 14:38:40,360 --> 14:38:46,560 insert and we're going to use a tary 17094 14:38:42,916 --> 14:38:50,160 operator here is 17095 14:38:46,560 --> 14:38:52,240 checked question mark and colon so we 17096 14:38:50,160 --> 14:38:55,916 can also use a ternary operator when 17097 14:38:52,240 --> 14:38:58,000 we're inserting into a string so again 17098 14:38:55,916 --> 14:39:00,796 if this is true it's going to get the 17099 14:38:58,000 --> 14:39:02,720 value after the question mark So if it 17100 14:39:00,796 --> 14:39:05,080 is checked we're going to give it the 17101 14:39:02,720 --> 14:39:08,040 value 17102 14:39:05,080 --> 14:39:11,116 checked if this is false we're going to 17103 14:39:08,040 --> 14:39:13,200 give it the value of empty string so 17104 14:39:11,116 --> 14:39:16,240 it's the same thing as if this attribute 17105 14:39:13,200 --> 14:39:18,480 didn't exist so now if we 17106 14:39:16,240 --> 14:39:20,756 save you'll notice that the correct 17107 14:39:18,480 --> 14:39:23,116 delivery option is checked for each 17108 14:39:20,756 --> 14:39:25,200 product the first product we saved 17109 14:39:23,116 --> 14:39:28,000 delivery option one and the second 17110 14:39:25,200 --> 14:39:31,560 product we say delivery option 17111 14:39:28,000 --> 14:39:33,916 two finally let's replace this date at 17112 14:39:31,560 --> 14:39:35,080 the top with the delivery date that we 17113 14:39:33,916 --> 14:39:37,480 selected 17114 14:39:35,080 --> 14:39:41,560 here we'll go back to our 17115 14:39:37,480 --> 14:39:44,756 code and we'll scroll to the 17116 14:39:41,560 --> 14:39:47,040 top until we see this date in our code 17117 14:39:44,756 --> 14:39:49,360 here so now we're going to calculate 17118 14:39:47,040 --> 14:39:52,200 this date using the delivery option that 17119 14:39:49,360 --> 14:39:54,680 we picked so remember that inside the 17120 14:39:52,200 --> 14:39:57,480 cart we only saved the delivery option 17121 14:39:54,680 --> 14:39:58,680 ID so let's use this to get the full 17122 14:39:57,480 --> 14:40:01,880 delivery 17123 14:39:58,680 --> 14:40:06,040 option so at the top here let's get the 17124 14:40:01,880 --> 14:40:11,916 delivery option ID out of the cart const 17125 14:40:06,040 --> 14:40:15,640 delivery option ID equals cart item. 17126 14:40:11,916 --> 14:40:15,640 delivery option 17127 14:40:16,756 --> 14:40:22,276 ID next we're going to use this ID to 17128 14:40:19,640 --> 14:40:25,276 find the full delivery option now we did 17129 14:40:22,276 --> 14:40:26,796 something similar with the product ID we 17130 14:40:25,276 --> 14:40:28,880 Loop through the products found a 17131 14:40:26,796 --> 14:40:31,796 matching ID and then saved it in a 17132 14:40:28,880 --> 14:40:33,560 variable up here so we'll do the same 17133 14:40:31,796 --> 14:40:35,596 let's create a variable to store the 17134 14:40:33,560 --> 14:40:38,520 result let 17135 14:40:35,596 --> 14:40:41,400 delivery option and then we're going to 17136 14:40:38,520 --> 14:40:46,720 Loop through the delivery options so 17137 14:40:41,400 --> 14:40:47,916 delivery options. for each brackets and 17138 14:40:46,720 --> 14:40:51,000 a 17139 14:40:47,916 --> 14:40:53,520 function and let's name the parameter 17140 14:40:51,000 --> 14:40:56,400 option because we use the name delivery 17141 14:40:53,520 --> 14:41:01,480 option up here so in here we're going to 17142 14:40:56,400 --> 14:41:07,680 look for a matching ID so if this option 17143 14:41:01,480 --> 14:41:07,680 ID is equal to the delivery option ID 17144 14:41:09,360 --> 14:41:16,956 here we're going to save this inside 17145 14:41:12,200 --> 14:41:19,560 this variable so delivery option equals 17146 14:41:16,956 --> 14:41:21,916 option so now we have the full delivery 17147 14:41:19,560 --> 14:41:24,200 option in our code and we can use it to 17148 14:41:21,916 --> 14:41:26,320 get the delivery days property and then 17149 14:41:24,200 --> 14:41:29,436 calculate the day that we need 17150 14:41:26,320 --> 14:41:32,240 here to calculate this state we already 17151 14:41:29,436 --> 14:41:34,320 wrote some code for this at the bottom 17152 14:41:32,240 --> 14:41:37,040 if we scroll down in our 17153 14:41:34,320 --> 14:41:39,916 code when we created the delivery 17154 14:41:37,040 --> 14:41:42,720 options we calculated the date for each 17155 14:41:39,916 --> 14:41:46,520 delivery option so we can reuse this 17156 14:41:42,720 --> 14:41:47,720 code above so let's select this code and 17157 14:41:46,520 --> 14:41:49,560 then right 17158 14:41:47,720 --> 14:41:52,916 click 17159 14:41:49,560 --> 14:41:56,040 copy and then at the 17160 14:41:52,916 --> 14:41:58,720 top after we get the delivery option 17161 14:41:56,040 --> 14:42:02,200 we're just going to rightclick and paste 17162 14:41:58,720 --> 14:42:02,200 that code again 17163 14:42:02,400 --> 14:42:07,480 and if we need to we can reformat by 17164 14:42:04,480 --> 14:42:10,480 selecting these lines and press shift 17165 14:42:07,480 --> 14:42:12,956 tab so this code will take the delivery 17166 14:42:10,480 --> 14:42:14,916 option that we selected and calculate 17167 14:42:12,956 --> 14:42:18,000 the delivery date that we need to show 17168 14:42:14,916 --> 14:42:20,160 down here and now instead of using the 17169 14:42:18,000 --> 14:42:23,320 same date every time we're going to 17170 14:42:20,160 --> 14:42:28,916 remove this and insert the date string 17171 14:42:23,320 --> 14:42:32,400 that we created Above So date string and 17172 14:42:28,916 --> 14:42:34,756 save so now this date string up here 17173 14:42:32,400 --> 14:42:38,160 will match the delivery date that we 17174 14:42:34,756 --> 14:42:40,436 chose on the right side so that's how we 17175 14:42:38,160 --> 14:42:44,520 generate the HTML for the delivery 17176 14:42:40,436 --> 14:42:46,276 options using the DJs external Library 17177 14:42:44,520 --> 14:42:50,320 so now let's take a look at our changes 17178 14:42:46,276 --> 14:42:52,560 in git we'll go to our code and open the 17179 14:42:50,320 --> 14:42:52,560 git 17180 14:42:54,080 --> 14:43:00,400 section and here it'll tell us in cart. 17181 14:42:57,320 --> 14:43:01,596 JS we added delivery option IDs to the 17182 14:43:00,400 --> 14:43:03,596 cart 17183 14:43:01,596 --> 14:43:06,756 as well as when we add to the 17184 14:43:03,596 --> 14:43:09,880 cart and then in this file we created 17185 14:43:06,756 --> 14:43:09,880 the list of delivery 17186 14:43:11,116 --> 14:43:16,840 options and in checkout. JS instead of 17187 14:43:14,200 --> 14:43:20,000 writing this HTML directly we generated 17188 14:43:16,840 --> 14:43:23,116 it and then in money. JS we learned 17189 14:43:20,000 --> 14:43:26,796 about default exports so let's type a 17190 14:43:23,116 --> 14:43:33,000 message to describe our changes save 17191 14:43:26,796 --> 14:43:36,400 data and generate HTML for for delivery 17192 14:43:33,000 --> 14:43:36,400 options and 17193 14:43:39,360 --> 14:43:46,240 commit the last step is to make it 17194 14:43:43,520 --> 14:43:49,756 interactive so when we click a delivery 17195 14:43:46,240 --> 14:43:52,240 option we need to do two things first we 17196 14:43:49,756 --> 14:43:55,916 need to update the delivery option ID in 17197 14:43:52,240 --> 14:43:58,000 the cart array and number two we need to 17198 14:43:55,916 --> 14:44:00,520 update the page so we need to update 17199 14:43:58,000 --> 14:44:02,360 this date here to match the date that we 17200 14:44:00,520 --> 14:44:04,596 select 17201 14:44:02,360 --> 14:44:06,400 let's start with the first step let's 17202 14:44:04,596 --> 14:44:09,000 create a function for updating the 17203 14:44:06,400 --> 14:44:10,200 delivery option in the cart we're going 17204 14:44:09,000 --> 14:44:13,680 to go to our 17205 14:44:10,200 --> 14:44:15,840 code and we're going to open the file 17206 14:44:13,680 --> 14:44:19,240 called cart. 17207 14:44:15,840 --> 14:44:21,400 JS so because we're modifying the cart a 17208 14:44:19,240 --> 14:44:23,480 best practice is to put the code inside 17209 14:44:21,400 --> 14:44:26,560 this file this file should contain all 17210 14:44:23,480 --> 14:44:28,680 the code that manages the cart so we're 17211 14:44:26,560 --> 14:44:31,000 going to scroll down to the bottom and 17212 14:44:28,680 --> 14:44:34,520 we're going to add a new function so 17213 14:44:31,000 --> 14:44:36,320 let's type function and let's name it 17214 14:44:34,520 --> 14:44:40,160 update 17215 14:44:36,320 --> 14:44:42,640 delivery option brackets and curly 17216 14:44:40,160 --> 14:44:45,000 brackets so when we update a delivery 17217 14:44:42,640 --> 14:44:46,840 option we need to know the product that 17218 14:44:45,000 --> 14:44:49,160 we want to update as well as the 17219 14:44:46,840 --> 14:44:50,840 delivery option that we chose so we're 17220 14:44:49,160 --> 14:44:56,596 going to need two things for this 17221 14:44:50,840 --> 14:44:59,320 function the product ID and the delivery 17222 14:44:56,596 --> 14:44:59,320 option 17223 14:44:59,916 --> 14:45:05,040 ID so the steps to do this is first 17224 14:45:03,160 --> 14:45:07,596 we're going to Loop through the cart and 17225 14:45:05,040 --> 14:45:10,880 find the product and then we're going to 17226 14:45:07,596 --> 14:45:13,560 update the delivery option ID of that 17227 14:45:10,880 --> 14:45:16,116 product so to find the product using the 17228 14:45:13,560 --> 14:45:20,240 product ID we already have some code for 17229 14:45:16,116 --> 14:45:22,756 this if we scroll up to the add to cart 17230 14:45:20,240 --> 14:45:25,116 function we have this code which takes a 17231 14:45:22,756 --> 14:45:27,756 product ID and finds the matching 17232 14:45:25,116 --> 14:45:29,796 product in the cart so let's just make a 17233 14:45:27,756 --> 14:45:31,956 copy of this code we're going to select 17234 14:45:29,796 --> 14:45:34,400 it right click 17235 14:45:31,956 --> 14:45:39,756 copy and then paste it into our new 17236 14:45:34,400 --> 14:45:42,636 function so down here right click and 17237 14:45:39,756 --> 14:45:44,796 paste so this will give us the card item 17238 14:45:42,636 --> 14:45:45,680 that matches this product ID and save it 17239 14:45:44,796 --> 14:45:49,276 in this 17240 14:45:45,680 --> 14:45:51,956 variable next each card item has a 17241 14:45:49,276 --> 14:45:54,436 property called delivery option ID so 17242 14:45:51,956 --> 14:45:57,276 we're just going to update that property 17243 14:45:54,436 --> 14:46:02,480 so down here we can just type the code 17244 14:45:57,276 --> 14:46:05,596 matching item. delivery op option ID and 17245 14:46:02,480 --> 14:46:07,916 make it equal to the delivery option ID 17246 14:46:05,596 --> 14:46:10,160 that we give to this function so 17247 14:46:07,916 --> 14:46:13,000 delivery option 17248 14:46:10,160 --> 14:46:15,636 ID and lastly because we updated the 17249 14:46:13,000 --> 14:46:18,040 cart we should save it to local 17250 14:46:15,636 --> 14:46:22,040 storage so we can use this function we 17251 14:46:18,040 --> 14:46:26,240 created earlier save to 17252 14:46:22,040 --> 14:46:28,756 storage and brackets now let's 17253 14:46:26,240 --> 14:46:31,480 save now that we have this function 17254 14:46:28,756 --> 14:46:33,436 let's use it on the checkout page so 17255 14:46:31,480 --> 14:46:36,080 first let's export it so we can use this 17256 14:46:33,436 --> 14:46:36,756 in another file we're going to export 17257 14:46:36,080 --> 14:46:39,080 this 17258 14:46:36,756 --> 14:46:43,200 function and 17259 14:46:39,080 --> 14:46:46,436 save and then open checkout. 17260 14:46:43,200 --> 14:46:48,360 JS and then in our project we need to 17261 14:46:46,436 --> 14:46:50,320 add event listeners to each of these 17262 14:46:48,360 --> 14:46:53,116 delivery options so we run some code 17263 14:46:50,320 --> 14:46:55,480 when we click them so inside our code 17264 14:46:53,116 --> 14:46:58,480 here we're going to scroll to the HTML 17265 14:46:55,480 --> 14:47:01,080 for each delivery option which is right 17266 14:46:58,480 --> 14:47:04,840 here and then we're going to add a class 17267 14:47:01,080 --> 14:47:08,436 so we can select it using the Dom js- 17268 14:47:04,840 --> 14:47:11,160 delivery D option and then we'll add an 17269 14:47:08,436 --> 14:47:13,720 event listener to these so we'll scroll 17270 14:47:11,160 --> 14:47:15,956 down and down here we'll add the event 17271 14:47:13,720 --> 14:47:20,880 listener using 17272 14:47:15,956 --> 14:47:23,040 document. query selector all brackets 17273 14:47:20,880 --> 14:47:28,360 and a string will'll select that class 17274 14:47:23,040 --> 14:47:31,000 we just created using dot js- delivery D 17275 14:47:28,360 --> 14:47:37,320 option 17276 14:47:31,000 --> 14:47:37,320 and then dot for each brackets and a 17277 14:47:37,756 --> 14:47:42,360 function so for each option we're going 17278 14:47:40,040 --> 14:47:44,840 to add an event listener and listen for 17279 14:47:42,360 --> 14:47:47,240 clicks so let's just call each of the 17280 14:47:44,840 --> 14:47:49,796 options an 17281 14:47:47,240 --> 14:47:53,000 element so for each of these elements 17282 14:47:49,796 --> 14:47:56,756 we're going to element. 17283 14:47:53,000 --> 14:47:59,796 addevent listener brackets and we're 17284 14:47:56,756 --> 14:48:01,596 going to listen for clicks comma and 17285 14:47:59,796 --> 14:48:02,796 we're going to run this function when we 17286 14:48:01,596 --> 14:48:05,596 click 17287 14:48:02,796 --> 14:48:08,480 it so as we said the first thing we'll 17288 14:48:05,596 --> 14:48:11,240 do is to update the delivery option ID 17289 14:48:08,480 --> 14:48:13,756 in the cart so now let's import that 17290 14:48:11,240 --> 14:48:16,200 function that we created earlier we're 17291 14:48:13,756 --> 14:48:18,880 going to scroll to the 17292 14:48:16,200 --> 14:48:21,080 top and we're already importing from the 17293 14:48:18,880 --> 14:48:23,240 cart so to import something else we're 17294 14:48:21,080 --> 14:48:27,560 going to add a comma and we're going to 17295 14:48:23,240 --> 14:48:30,436 import that function update delivery 17296 14:48:27,560 --> 14:48:32,720 option so now let's scroll back down and 17297 14:48:30,436 --> 14:48:35,480 use use it in our 17298 14:48:32,720 --> 14:48:39,080 code so here when we click a delivery 17299 14:48:35,480 --> 14:48:42,240 option we're going to update delivery 17300 14:48:39,080 --> 14:48:44,756 option brackets now we said we need to 17301 14:48:42,240 --> 14:48:48,116 give this function two things the 17302 14:48:44,756 --> 14:48:51,956 product ID that we want to updated for 17303 14:48:48,116 --> 14:48:56,040 as well as the delivery option ID that 17304 14:48:51,956 --> 14:48:59,436 we picked but how do we get these two 17305 14:48:56,040 --> 14:49:02,560 values and the answer is if we scroll 17306 14:48:59,436 --> 14:49:05,636 up here here we have access to the 17307 14:49:02,560 --> 14:49:07,880 product ID and we also have access to 17308 14:49:05,636 --> 14:49:10,796 the delivery option 17309 14:49:07,880 --> 14:49:13,320 ID so we can use data attributes to 17310 14:49:10,796 --> 14:49:17,080 attach it to this element 17311 14:49:13,320 --> 14:49:19,680 here so let's create a data attribute 17312 14:49:17,080 --> 14:49:24,320 data- 17313 14:49:19,680 --> 14:49:28,680 product-id equals double quotes and 17314 14:49:24,320 --> 14:49:32,040 insert the product ID which is matching 17315 14:49:28,680 --> 14:49:35,080 product. ID and we're also going to use 17316 14:49:32,040 --> 14:49:39,520 a data attribute for the delivery option 17317 14:49:35,080 --> 14:49:41,000 data- delivery D option- ID equals 17318 14:49:39,520 --> 14:49:43,360 double 17319 14:49:41,000 --> 14:49:49,436 quotes and we're going to insert the 17320 14:49:43,360 --> 14:49:52,796 delivery option here so delivery 17321 14:49:49,436 --> 14:49:52,796 option. and 17322 14:49:53,000 --> 14:49:59,320 save next let's scroll back 17323 14:49:55,796 --> 14:50:01,116 down and then here we're going to get 17324 14:49:59,320 --> 14:50:05,400 those values out of the the data 17325 14:50:01,116 --> 14:50:08,116 attributes by using const and let's use 17326 14:50:05,400 --> 14:50:13,160 the shorthand property here for practice 17327 14:50:08,116 --> 14:50:15,840 curly brackets equals the element. data 17328 14:50:13,160 --> 14:50:20,320 set and then out of the data attributes 17329 14:50:15,840 --> 14:50:24,596 we're going to get the product ID comma 17330 14:50:20,320 --> 14:50:27,636 as well as the delivery option 17331 14:50:24,596 --> 14:50:29,520 ID as a reminder this is a shorthand 17332 14:50:27,636 --> 14:50:32,200 property it does the same thing as the 17333 14:50:29,520 --> 14:50:34,436 code down here but it's just a 17334 14:50:32,200 --> 14:50:38,040 shortcut now let's 17335 14:50:34,436 --> 14:50:41,200 save and try out the project we'll go 17336 14:50:38,040 --> 14:50:44,400 back and here if we click the second 17337 14:50:41,200 --> 14:50:47,000 delivery option and then refresh the 17338 14:50:44,400 --> 14:50:50,116 page you'll notice that now this one is 17339 14:50:47,000 --> 14:50:52,116 checked and the date here is 17340 14:50:50,116 --> 14:50:54,436 correct so that's how we make the 17341 14:50:52,116 --> 14:50:57,116 section interactive we just update the 17342 14:50:54,436 --> 14:50:59,720 cart and then when we refresh the page 17343 14:50:57,116 --> 14:51:02,080 it will regenerate the HTML based on the 17344 14:50:59,720 --> 14:51:04,840 new cart 17345 14:51:02,080 --> 14:51:08,400 now let's save our code into git we'll 17346 14:51:04,840 --> 14:51:10,756 go back and open the git 17347 14:51:08,400 --> 14:51:13,756 section and then it will tell us in 17348 14:51:10,756 --> 14:51:15,680 cart. JS we added this function for 17349 14:51:13,756 --> 14:51:19,040 updating the delivery 17350 14:51:15,680 --> 14:51:22,080 option and in checkout. JS we imported 17351 14:51:19,040 --> 14:51:24,240 that function and then at the bottom we 17352 14:51:22,080 --> 14:51:26,720 added some event listeners and data 17353 14:51:24,240 --> 14:51:28,240 attributes and we use that function to 17354 14:51:26,720 --> 14:51:30,520 update the 17355 14:51:28,240 --> 14:51:34,796 card now let's add a message describe 17356 14:51:30,520 --> 14:51:36,560 describing our changes make delivery 17357 14:51:34,796 --> 14:51:39,916 options 17358 14:51:36,560 --> 14:51:39,916 interactive and 17359 14:51:43,040 --> 14:51:49,436 commit next when we select a delivery 17360 14:51:45,796 --> 14:51:53,240 option we need to update the 17361 14:51:49,436 --> 14:51:55,720 page so right now in our project when we 17362 14:51:53,240 --> 14:51:58,596 click a delivery option we need to 17363 14:51:55,720 --> 14:52:00,840 refresh the page for the text to be 17364 14:51:58,596 --> 14:52:03,200 updated so what we're going to work on 17365 14:52:00,840 --> 14:52:04,680 now is when we click this we want the 17366 14:52:03,200 --> 14:52:06,956 page to update 17367 14:52:04,680 --> 14:52:09,480 immediately so far in this course the 17368 14:52:06,956 --> 14:52:11,596 way we solve this problem is once we 17369 14:52:09,480 --> 14:52:14,160 click this we're going to use a Dom to 17370 14:52:11,596 --> 14:52:16,320 get this element here and then update 17371 14:52:14,160 --> 14:52:18,680 the text 17372 14:52:16,320 --> 14:52:20,436 directly however the problem with this 17373 14:52:18,680 --> 14:52:23,880 approach is that we need to update the 17374 14:52:20,436 --> 14:52:25,880 page one by one and later there might be 17375 14:52:23,880 --> 14:52:28,956 lots of places on the page that we need 17376 14:52:25,880 --> 14:52:31,480 to update for example when we change a 17377 14:52:28,956 --> 14:52:34,680 delivery option we also need to change 17378 14:52:31,480 --> 14:52:36,400 the numbers here on the right so there's 17379 14:52:34,680 --> 14:52:39,080 potentially a lot of things that we need 17380 14:52:36,400 --> 14:52:41,840 to update one by one and it's easy to 17381 14:52:39,080 --> 14:52:43,956 make a mistake or forget something so 17382 14:52:41,840 --> 14:52:47,880 now we're going to learn a better way to 17383 14:52:43,956 --> 14:52:52,756 update the page we'll go back to our 17384 14:52:47,880 --> 14:52:56,040 code and let's open checkout. JS 17385 14:52:52,756 --> 14:53:00,200 again and notice if we scroll 17386 14:52:56,040 --> 14:53:03,360 up here we already have all of this code 17387 14:53:00,200 --> 14:53:06,796 that takes our data and generates the 17388 14:53:03,360 --> 14:53:09,320 HTML so another way to update the page 17389 14:53:06,796 --> 14:53:12,560 is after we update our data we just need 17390 14:53:09,320 --> 14:53:16,436 to rerun all this code and regenerate 17391 14:53:12,560 --> 14:53:18,160 all this HTML so let's give this a try 17392 14:53:16,436 --> 14:53:21,240 first we're going to put all of this 17393 14:53:18,160 --> 14:53:24,276 code inside a function so we can rerun 17394 14:53:21,240 --> 14:53:27,436 it we're going to scroll to the 17395 14:53:24,276 --> 14:53:31,636 top and let's put this in a function by 17396 14:53:27,436 --> 14:53:36,636 typing function and let's call call it 17397 14:53:31,636 --> 14:53:39,080 render order summary brackets and curly 17398 14:53:36,636 --> 14:53:41,080 brackets so the word render just means 17399 14:53:39,080 --> 14:53:43,276 to display on the 17400 14:53:41,080 --> 14:53:44,880 page and we're going to call this 17401 14:53:43,276 --> 14:53:47,480 section the order 17402 14:53:44,880 --> 14:53:50,840 summary so this function should display 17403 14:53:47,480 --> 14:53:53,160 the order summary on the page next we're 17404 14:53:50,840 --> 14:53:55,680 going to put all of this code inside our 17405 14:53:53,160 --> 14:53:57,560 new function so let's just remove this 17406 14:53:55,680 --> 14:54:00,040 curly bracket 17407 14:53:57,560 --> 14:54:02,040 here and let's actually select all of 17408 14:54:00,040 --> 14:54:05,400 this code and we're just going to 17409 14:54:02,040 --> 14:54:07,796 reformat it by pressing Tab and adding 17410 14:54:05,400 --> 14:54:09,880 an indent and then at the end we're 17411 14:54:07,796 --> 14:54:12,520 going to add back that closed curly 17412 14:54:09,880 --> 14:54:15,360 bracket to finish the 17413 14:54:12,520 --> 14:54:17,720 function so all we did was we put all of 17414 14:54:15,360 --> 14:54:20,880 our code inside a 17415 14:54:17,720 --> 14:54:22,680 function and then at the bottom let's 17416 14:54:20,880 --> 14:54:25,200 run this function because we still need 17417 14:54:22,680 --> 14:54:28,956 to run all this code at the start of our 17418 14:54:25,200 --> 14:54:31,200 page so we're going to type render the 17419 14:54:28,956 --> 14:54:33,720 order summary 17420 14:54:31,200 --> 14:54:35,320 and brackets so this shouldn't change 17421 14:54:33,720 --> 14:54:37,276 anything because we're just running the 17422 14:54:35,320 --> 14:54:41,400 same code but now it's just in a 17423 14:54:37,276 --> 14:54:43,956 function let's save and 17424 14:54:41,400 --> 14:54:46,400 check and everything's still working so 17425 14:54:43,956 --> 14:54:47,400 that's good so now let's go back into 17426 14:54:46,400 --> 14:54:50,956 our 17427 14:54:47,400 --> 14:54:52,916 code and this time to update the page 17428 14:54:50,956 --> 14:54:55,756 instead of using the Dom to update the 17429 14:54:52,916 --> 14:54:58,360 page directly like we did up here we're 17430 14:54:55,756 --> 14:55:02,240 just going to rerun all of this code and 17431 14:54:58,360 --> 14:55:02,240 regenerate all the HTML 17432 14:55:02,436 --> 14:55:08,756 so after we update the data we're going 17433 14:55:04,400 --> 14:55:10,720 to rerun the code using render order 17434 14:55:08,756 --> 14:55:13,000 summary and 17435 14:55:10,720 --> 14:55:15,320 brackets now let's 17436 14:55:13,000 --> 14:55:17,956 save and try it 17437 14:55:15,320 --> 14:55:20,276 out so here let's say we start off with 17438 14:55:17,956 --> 14:55:23,040 the first delivery option if we click 17439 14:55:20,276 --> 14:55:25,240 the third delivery option you'll notice 17440 14:55:23,040 --> 14:55:27,080 that the date up here updates 17441 14:55:25,240 --> 14:55:29,916 automatically if we click the second 17442 14:55:27,080 --> 14:55:31,436 delivery option it updates automatically 17443 14:55:29,916 --> 14:55:33,596 as well 17444 14:55:31,436 --> 14:55:36,360 so that's a better way to keep the page 17445 14:55:33,596 --> 14:55:40,276 up to dat instead of using the Dom and 17446 14:55:36,360 --> 14:55:43,000 changing the HTML directly one by one we 17447 14:55:40,276 --> 14:55:45,560 just update the data and then regenerate 17448 14:55:43,000 --> 14:55:45,560 all of this 17449 14:55:45,880 --> 14:55:50,636 HTML so let's go back into our code and 17450 14:55:48,956 --> 14:55:54,240 there's two things that I want to point 17451 14:55:50,636 --> 14:55:57,360 out here so notice that we put our event 17452 14:55:54,240 --> 14:55:59,116 listeners into this big function and 17453 14:55:57,360 --> 14:56:01,880 that's because when we're regenerating 17454 14:55:59,116 --> 14:56:04,956 all the HTML we're essentially deleting 17455 14:56:01,880 --> 14:56:08,000 the previous HTML and replacing it so we 17456 14:56:04,956 --> 14:56:10,320 need to add the event listeners again 17457 14:56:08,000 --> 14:56:12,680 and the second thing I want to point out 17458 14:56:10,320 --> 14:56:15,360 is that inside the render order summary 17459 14:56:12,680 --> 14:56:16,636 function we can call render order 17460 14:56:15,360 --> 14:56:19,840 summary 17461 14:56:16,636 --> 14:56:23,560 again so a function can actually call 17462 14:56:19,840 --> 14:56:26,200 itself or rerun itself and this feature 17463 14:56:23,560 --> 14:56:30,636 is called recursion it's useful when a 17464 14:56:26,200 --> 14:56:33,560 function needs to rerun all of its code 17465 14:56:30,636 --> 14:56:35,916 so now let's save our changes into git 17466 14:56:33,560 --> 14:56:38,880 we'll open the git section and it will 17467 14:56:35,916 --> 14:56:42,360 tell us in checkout. JS we basically 17468 14:56:38,880 --> 14:56:45,200 moved all of our code into this function 17469 14:56:42,360 --> 14:56:47,840 and then at the bottom we call the 17470 14:56:45,200 --> 14:56:49,840 function at the start of the page and we 17471 14:56:47,840 --> 14:56:52,240 also call the function when we select a 17472 14:56:49,840 --> 14:56:53,956 delivery option so now let's add a 17473 14:56:52,240 --> 14:56:55,840 message describing our 17474 14:56:53,956 --> 14:56:57,520 changes 17475 14:56:55,840 --> 14:57:00,756 regenerate 17476 14:56:57,520 --> 14:57:05,160 HTML when updating 17477 14:57:00,756 --> 14:57:05,160 delivery option and 17478 14:57:07,880 --> 14:57:12,240 commit so the technique we just used 17479 14:57:10,400 --> 14:57:15,360 where we update the data and then 17480 14:57:12,240 --> 14:57:16,720 regenerate all the HTML this technique 17481 14:57:15,360 --> 14:57:21,160 is called 17482 14:57:16,720 --> 14:57:23,636 MVC MVC stands for model view controller 17483 14:57:21,160 --> 14:57:26,796 and is a popular technique in software 17484 14:57:23,636 --> 14:57:29,480 engineering in MVC we split our code 17485 14:57:26,796 --> 14:57:32,400 into three parts the first part is the 17486 14:57:29,480 --> 14:57:34,116 model this is all the code that saves 17487 14:57:32,400 --> 14:57:37,560 and manages the 17488 14:57:34,116 --> 14:57:40,480 data so in our project all the code in 17489 14:57:37,560 --> 14:57:43,956 this data folder is called the model 17490 14:57:40,480 --> 14:57:48,160 because this code saves and manages our 17491 14:57:43,956 --> 14:57:50,320 data the second part of MVC is the view 17492 14:57:48,160 --> 14:57:53,040 this is code that takes the data and 17493 14:57:50,320 --> 14:57:56,276 displays it on the page so in our 17494 14:57:53,040 --> 14:58:00,116 project if we open checkout. JS and 17495 14:57:56,276 --> 14:58:02,560 scroll to the top here we have code that 17496 14:58:00,116 --> 14:58:06,040 takes our data and then generates all 17497 14:58:02,560 --> 14:58:09,000 the HTML so this code is called the 17498 14:58:06,040 --> 14:58:11,840 view the last part of MVC is the 17499 14:58:09,000 --> 14:58:14,116 controller so this runs some code when 17500 14:58:11,840 --> 14:58:17,480 we interact with the 17501 14:58:14,116 --> 14:58:20,320 page so in our project at the 17502 14:58:17,480 --> 14:58:23,160 bottom these event listeners here would 17503 14:58:20,320 --> 14:58:25,360 be called the controller because they do 17504 14:58:23,160 --> 14:58:26,916 something when we interact with the page 17505 14:58:25,360 --> 14:58:30,400 or with the 17506 14:58:26,916 --> 14:58:32,636 view so in MVC we divide our code Lo 17507 14:58:30,400 --> 14:58:34,880 into these three parts and then they 17508 14:58:32,636 --> 14:58:38,200 interact with each other in a 17509 14:58:34,880 --> 14:58:39,916 loop first we use the model to generate 17510 14:58:38,200 --> 14:58:42,720 the 17511 14:58:39,916 --> 14:58:45,636 view so in our code that's what we did 17512 14:58:42,720 --> 14:58:48,520 here we took the model or the data which 17513 14:58:45,636 --> 14:58:52,400 is our cart and then we generated the 17514 14:58:48,520 --> 14:58:55,956 HTML or the view then when we interact 17515 14:58:52,400 --> 14:58:55,956 with the view it will run the 17516 14:58:56,240 --> 14:59:00,636 controller so that's what we did at the 17517 14:58:58,720 --> 14:59:02,636 bottom here 17518 14:59:00,636 --> 14:59:03,680 when we interact with the page we run 17519 14:59:02,636 --> 14:59:08,680 some 17520 14:59:03,680 --> 14:59:08,680 code then the controller will update the 17521 14:59:08,756 --> 14:59:14,596 model so that's what we did here inside 17522 14:59:12,040 --> 14:59:16,720 the controller we just updated our data 17523 14:59:14,596 --> 14:59:19,756 using this 17524 14:59:16,720 --> 14:59:23,320 function and finally we use the updated 17525 14:59:19,756 --> 14:59:26,276 model to regenerate The 17526 14:59:23,320 --> 14:59:28,360 View so that's what this code does it 17527 14:59:26,276 --> 14:59:30,636 takes the updated data and then 17528 14:59:28,360 --> 14:59:32,796 regenerates all the HTM ml or 17529 14:59:30,636 --> 14:59:36,360 regenerates The 17530 14:59:32,796 --> 14:59:38,916 View so that's how MVC Works instead of 17531 14:59:36,360 --> 14:59:40,680 updating the page directly with the Dom 17532 14:59:38,916 --> 14:59:43,956 we just update our data and then 17533 14:59:40,680 --> 14:59:43,956 regenerate all the 17534 14:59:44,480 --> 14:59:51,000 HTML so we use MVC because doing this 17535 14:59:48,520 --> 14:59:51,916 makes sure that the page always matches 17536 14:59:51,000 --> 14:59:56,436 the 17537 14:59:51,916 --> 14:59:59,000 data so MVC is known as a design pattern 17538 14:59:56,436 --> 15:00:01,520 it's a way to organize and design our 17539 14:59:59,000 --> 15:00:03,840 code in the future you might use a 17540 15:00:01,520 --> 15:00:06,200 technology called a JavaScript framework 17541 15:00:03,840 --> 15:00:09,000 to build websites many JavaScript 17542 15:00:06,200 --> 15:00:11,720 Frameworks are based on MVC so it's 17543 15:00:09,000 --> 15:00:15,240 useful to know how it 17544 15:00:11,720 --> 15:00:17,796 works okay now that we learned the MVC 17545 15:00:15,240 --> 15:00:19,520 design pattern let's do some practical 17546 15:00:17,796 --> 15:00:21,956 examples using 17547 15:00:19,520 --> 15:00:24,916 MBC we're going to open our project 17548 15:00:21,956 --> 15:00:27,200 again to the checkout page and we're 17549 15:00:24,916 --> 15:00:30,520 going to create this payment section on 17550 15:00:27,200 --> 15:00:34,040 the right now before we start if we look 17551 15:00:30,520 --> 15:00:35,880 at our code in this checkout. JS file 17552 15:00:34,040 --> 15:00:37,400 you'll notice that we already have a lot 17553 15:00:35,880 --> 15:00:40,240 of code 17554 15:00:37,400 --> 15:00:42,520 here instead of adding even more code to 17555 15:00:40,240 --> 15:00:44,796 this file let's actually split this up 17556 15:00:42,520 --> 15:00:47,560 into two different 17557 15:00:44,796 --> 15:00:50,160 files so one file will be responsible 17558 15:00:47,560 --> 15:00:52,116 for this left section and then one file 17559 15:00:50,160 --> 15:00:54,320 will be responsible for this right 17560 15:00:52,116 --> 15:00:56,436 section we're doing this to keep our 17561 15:00:54,320 --> 15:01:00,000 code more organized and so that we don't 17562 15:00:56,436 --> 15:01:02,916 have too much code in one file 17563 15:01:00,000 --> 15:01:05,796 so let's go back and let's create two 17564 15:01:02,916 --> 15:01:08,636 new files for the checkout page so up 17565 15:01:05,796 --> 15:01:14,240 here we'll create a file and let's call 17566 15:01:08,636 --> 15:01:17,840 this one the order summary. JS and press 17567 15:01:14,240 --> 15:01:20,276 enter and let's create another file for 17568 15:01:17,840 --> 15:01:23,360 the right section and let's call this 17569 15:01:20,276 --> 15:01:27,320 one the payment 17570 15:01:23,360 --> 15:01:29,916 summary. JS and press 17571 15:01:27,320 --> 15:01:32,276 enter now both of these files are 17572 15:01:29,916 --> 15:01:35,200 related to the checkout page so let's 17573 15:01:32,276 --> 15:01:38,116 create a folder and group them together 17574 15:01:35,200 --> 15:01:42,320 we'll create a new folder up here and 17575 15:01:38,116 --> 15:01:43,880 let's call it checkout and press enter 17576 15:01:42,320 --> 15:01:47,840 and we're going to move these two files 17577 15:01:43,880 --> 15:01:50,360 into this new folder so we move that and 17578 15:01:47,840 --> 15:01:50,360 we'll move 17579 15:01:51,880 --> 15:01:58,040 that next let's open checkout. JS and 17580 15:01:55,880 --> 15:02:01,000 let's move all of this code into the 17581 15:01:58,040 --> 15:02:04,636 order summary so we'll press control a 17582 15:02:01,000 --> 15:02:05,480 or command a to select all the code and 17583 15:02:04,636 --> 15:02:07,200 then 17584 15:02:05,480 --> 15:02:10,840 rightclick 17585 15:02:07,200 --> 15:02:14,040 cut and then in here we're going to 17586 15:02:10,840 --> 15:02:16,240 rightclick and 17587 15:02:14,040 --> 15:02:19,880 paste now there's a few things that we 17588 15:02:16,240 --> 15:02:23,680 need to fix here so first our checkout. 17589 15:02:19,880 --> 15:02:25,796 HTML file is still loading checkout. JS 17590 15:02:23,680 --> 15:02:28,436 so we actually need to run this function 17591 15:02:25,796 --> 15:02:31,480 inside checkout. JS instead of inside 17592 15:02:28,436 --> 15:02:35,080 here so let's actually remove 17593 15:02:31,480 --> 15:02:36,240 this and save and we're going to scroll 17594 15:02:35,080 --> 15:02:39,320 to the 17595 15:02:36,240 --> 15:02:42,160 top and let's export this function so we 17596 15:02:39,320 --> 15:02:47,240 can run it inside here so we'll type 17597 15:02:42,160 --> 15:02:51,200 export and save and then inside checkout 17598 15:02:47,240 --> 15:02:54,360 we're going to import curly brackets the 17599 15:02:51,200 --> 15:02:58,596 render order summary 17600 15:02:54,360 --> 15:03:02,520 function from a string and to locate 17601 15:02:58,596 --> 15:03:04,000 this file we're going to type A do slir 17602 15:03:02,520 --> 15:03:06,160 that means to start from the current 17603 15:03:04,000 --> 15:03:10,200 folder and we'll go into the checkout 17604 15:03:06,160 --> 15:03:13,200 folder checkout SL orderers 17605 15:03:10,200 --> 15:03:13,200 summary. 17606 15:03:13,916 --> 15:03:19,796 JS and finally we're going to run this 17607 15:03:16,916 --> 15:03:25,080 function when we load the page so render 17608 15:03:19,796 --> 15:03:25,080 order summary and brackets and 17609 15:03:25,480 --> 15:03:32,160 save another thing we need to fix is if 17610 15:03:28,360 --> 15:03:35,400 we open order summary .js we moved this 17611 15:03:32,160 --> 15:03:38,240 file into a folder so now all of these 17612 15:03:35,400 --> 15:03:41,360 file paths need to be updated do do 17613 15:03:38,240 --> 15:03:43,636 slash is now inside the scripts folder 17614 15:03:41,360 --> 15:03:46,200 and we can't find a data folder anymore 17615 15:03:43,636 --> 15:03:48,520 so this is going to cause an error so to 17616 15:03:46,200 --> 15:03:50,560 find the data folder we need to get out 17617 15:03:48,520 --> 15:03:53,116 of the checkout folder and then we need 17618 15:03:50,560 --> 15:03:55,436 to get out of the scripts folder so we 17619 15:03:53,116 --> 15:03:58,756 need to get out of two folders so we'll 17620 15:03:55,436 --> 15:04:00,400 just add an additional do dot slash and 17621 15:03:58,756 --> 15:04:04,276 then same thing for this we'll add 17622 15:04:00,400 --> 15:04:05,796 another dot dot slash and for the utils 17623 15:04:04,276 --> 15:04:08,636 now we need to get out of the checkout 17624 15:04:05,796 --> 15:04:11,560 folder and then into the utils folder so 17625 15:04:08,636 --> 15:04:13,840 we'll change this to do/ 17626 15:04:11,560 --> 15:04:16,276 utils and then for here we're going to 17627 15:04:13,840 --> 15:04:19,160 go out an additional folder so another 17628 15:04:16,276 --> 15:04:21,596 dot dot slash and 17629 15:04:19,160 --> 15:04:24,756 save so now let's check if this still 17630 15:04:21,596 --> 15:04:28,200 works we'll go to our project and 17631 15:04:24,756 --> 15:04:28,200 refresh and try it 17632 15:04:28,240 --> 15:04:34,160 out and the p still works because we're 17633 15:04:31,756 --> 15:04:37,680 still running all of the same code but 17634 15:04:34,160 --> 15:04:39,680 we just organized it into its own file 17635 15:04:37,680 --> 15:04:41,880 so now let's open git and save our 17636 15:04:39,680 --> 15:04:44,560 changes so it's going to tell us that in 17637 15:04:41,880 --> 15:04:47,080 here we removed all this code and then 17638 15:04:44,560 --> 15:04:49,636 we separated it into the order summary 17639 15:04:47,080 --> 15:04:51,916 file and the payment summary file which 17640 15:04:49,636 --> 15:04:54,680 we're going to work on next so let's 17641 15:04:51,916 --> 15:04:57,520 type a message describing our changes 17642 15:04:54,680 --> 15:05:03,636 move order 17643 15:04:57,520 --> 15:05:03,636 summary into its own own file and 17644 15:05:06,320 --> 15:05:11,880 commit now let's start creating the 17645 15:05:08,916 --> 15:05:16,956 payment summary on the right side so in 17646 15:05:11,880 --> 15:05:16,956 our code let's open payment summary. 17647 15:05:18,080 --> 15:05:23,720 JS and just like the order summary let's 17648 15:05:21,040 --> 15:05:27,040 put all our code in a function so we can 17649 15:05:23,720 --> 15:05:30,916 regenerate the HTML if we need to so 17650 15:05:27,040 --> 15:05:34,916 we'll type function and let's call it 17651 15:05:30,916 --> 15:05:36,116 render payment summary brackets and 17652 15:05:34,916 --> 15:05:39,276 curly 17653 15:05:36,116 --> 15:05:43,596 brackets and let's just 17654 15:05:39,276 --> 15:05:47,040 console.log string payment summary just 17655 15:05:43,596 --> 15:05:49,080 to make sure it's working so we'll save 17656 15:05:47,040 --> 15:05:53,400 and let's export this function and then 17657 15:05:49,080 --> 15:05:57,560 run it inside checkout. JS so export 17658 15:05:53,400 --> 15:06:01,360 save and then in checkout. JS at the top 17659 15:05:57,560 --> 15:06:06,796 we're going to import curly brackets the 17660 15:06:01,360 --> 15:06:09,916 render payment summary function from a 17661 15:06:06,796 --> 15:06:13,756 string the current folder go into the 17662 15:06:09,916 --> 15:06:16,680 checkout folder and the payment summary. 17663 15:06:13,756 --> 15:06:19,276 JS file and then at the bottom we're 17664 15:06:16,680 --> 15:06:23,720 going to run the function so render 17665 15:06:19,276 --> 15:06:26,520 payment summary brackets and 17666 15:06:23,720 --> 15:06:30,480 save let's go back to our project and 17667 15:06:26,520 --> 15:06:32,680 open the console rightclick inspect the 17668 15:06:30,480 --> 15:06:35,756 console and we'll see the message 17669 15:06:32,680 --> 15:06:38,360 payment summary so now we know that our 17670 15:06:35,756 --> 15:06:40,720 code here is working and the export is 17671 15:06:38,360 --> 15:06:40,720 working 17672 15:06:40,796 --> 15:06:46,240 correctly now let's remove this code and 17673 15:06:43,956 --> 15:06:48,480 actually build the payment 17674 15:06:46,240 --> 15:06:51,720 summary as always we're going to do 17675 15:06:48,480 --> 15:06:55,116 three steps save the data generate the 17676 15:06:51,720 --> 15:06:58,680 HTML and make it interactive so let's 17677 15:06:55,116 --> 15:07:01,080 start with saving the data in MVC this 17678 15:06:58,680 --> 15:07:03,480 is called the 17679 15:07:01,080 --> 15:07:06,360 model so if we go to our 17680 15:07:03,480 --> 15:07:08,796 project for this section the data that 17681 15:07:06,360 --> 15:07:10,276 we need is just to calculate these 17682 15:07:08,796 --> 15:07:13,080 numbers 17683 15:07:10,276 --> 15:07:15,160 here so let's start by calculating this 17684 15:07:13,080 --> 15:07:18,276 first number here which is the cost of 17685 15:07:15,160 --> 15:07:20,680 the products to calculate this we can 17686 15:07:18,276 --> 15:07:22,956 Loop through the cart and for each 17687 15:07:20,680 --> 15:07:26,436 product in the cart we can multiply the 17688 15:07:22,956 --> 15:07:28,916 products price times the quantity and 17689 15:07:26,436 --> 15:07:31,840 then add everything together now let's 17690 15:07:28,916 --> 15:07:34,436 convert these steps into code we'll go 17691 15:07:31,840 --> 15:07:35,956 to our project and inside the payment 17692 15:07:34,436 --> 15:07:38,840 summary 17693 15:07:35,956 --> 15:07:42,480 file first we need to get the cart so 17694 15:07:38,840 --> 15:07:43,360 let's import it import curly brackets 17695 15:07:42,480 --> 15:07:47,400 the 17696 15:07:43,360 --> 15:07:49,080 cart from the string and we'll locate 17697 15:07:47,400 --> 15:07:51,840 cart. 17698 15:07:49,080 --> 15:07:54,880 JS so first we'll go out of the checkout 17699 15:07:51,840 --> 15:07:57,840 folder do do slash and then go out of 17700 15:07:54,880 --> 15:08:01,436 the scripts folder do do slash and then 17701 15:07:57,840 --> 15:08:01,436 into the data folder 17702 15:08:02,080 --> 15:08:06,880 data.js and 17703 15:08:04,596 --> 15:08:12,520 save and now we're going to Loop through 17704 15:08:06,880 --> 15:08:15,240 the cart so we can type cart. for each 17705 15:08:12,520 --> 15:08:18,116 brackets and a 17706 15:08:15,240 --> 15:08:22,596 function and let's name the parameter 17707 15:08:18,116 --> 15:08:25,520 cart item so for each cart item we have 17708 15:08:22,596 --> 15:08:27,720 the product ID and the quantity so we 17709 15:08:25,520 --> 15:08:30,480 need to multiply the product's price 17710 15:08:27,720 --> 15:08:32,756 times the quantity so first we need to 17711 15:08:30,480 --> 15:08:35,200 use the product ID and get the full 17712 15:08:32,756 --> 15:08:39,480 product details and we already have some 17713 15:08:35,200 --> 15:08:42,720 code for this inside orders summary. JS 17714 15:08:39,480 --> 15:08:45,040 if we scroll down here we have some code 17715 15:08:42,720 --> 15:08:46,400 that takes a product ID and finds the 17716 15:08:45,040 --> 15:08:49,160 matching 17717 15:08:46,400 --> 15:08:51,916 product now instead of copy and pasting 17718 15:08:49,160 --> 15:08:54,360 this code let's actually put this into a 17719 15:08:51,916 --> 15:08:57,160 function so we can reuse this and we'll 17720 15:08:54,360 --> 15:09:01,360 practice some modules so let's select 17721 15:08:57,160 --> 15:09:03,680 this code and rightclick and copy it 17722 15:09:01,360 --> 15:09:05,520 first and then let's go into the 17723 15:09:03,680 --> 15:09:08,116 products. JS 17724 15:09:05,520 --> 15:09:10,480 file and at the top we're going to 17725 15:09:08,116 --> 15:09:14,240 create a function so we can reuse that 17726 15:09:10,480 --> 15:09:18,756 code so let's type function and let's 17727 15:09:14,240 --> 15:09:20,636 name it get product brackets and curer 17728 15:09:18,756 --> 15:09:23,680 brackets and we're going to give this 17729 15:09:20,636 --> 15:09:26,160 function the product ID that we're 17730 15:09:23,680 --> 15:09:31,040 looking for and then let's paste the 17731 15:09:26,160 --> 15:09:33,840 code we copied earlier right click and 17732 15:09:31,040 --> 15:09:36,436 paste so this finds the matching product 17733 15:09:33,840 --> 15:09:39,360 using the product ID and then at the end 17734 15:09:36,436 --> 15:09:42,756 of the function let's return the 17735 15:09:39,360 --> 15:09:45,400 matching product so we can use this 17736 15:09:42,756 --> 15:09:47,360 outside of the function so now let's 17737 15:09:45,400 --> 15:09:51,320 export this function so we can share it 17738 15:09:47,360 --> 15:09:55,000 across files so export and 17739 15:09:51,320 --> 15:09:56,796 save first we'll go to the order summary 17740 15:09:55,000 --> 15:09:58,956 and at the top let's import that 17741 15:09:56,796 --> 15:10:01,116 function so we're already importing from 17742 15:09:58,956 --> 15:10:04,796 product .js we're going to import 17743 15:10:01,116 --> 15:10:07,916 something else using a comma the get 17744 15:10:04,796 --> 15:10:10,680 product function and then down 17745 15:10:07,916 --> 15:10:13,436 here instead of all this 17746 15:10:10,680 --> 15:10:18,000 code we're going to run that function so 17747 15:10:13,436 --> 15:10:20,680 we make this equal to get product and 17748 15:10:18,000 --> 15:10:22,840 run the function and we need to give the 17749 15:10:20,680 --> 15:10:25,840 function the product ID so let's just 17750 15:10:22,840 --> 15:10:28,560 give that here product 17751 15:10:25,840 --> 15:10:31,400 ID and because we're no longer updating 17752 15:10:28,560 --> 15:10:33,756 this variable we can change from let to 17753 15:10:31,400 --> 15:10:36,000 const and 17754 15:10:33,756 --> 15:10:38,080 save let's go to our project to make 17755 15:10:36,000 --> 15:10:39,360 sure everything's still working so that 17756 15:10:38,080 --> 15:10:42,160 looks 17757 15:10:39,360 --> 15:10:45,680 good and now let's use this function 17758 15:10:42,160 --> 15:10:49,560 inside payment summary. JS so the same 17759 15:10:45,680 --> 15:10:53,636 thing we will import curly brackets the 17760 15:10:49,560 --> 15:10:56,720 get product function from a 17761 15:10:53,636 --> 15:11:00,000 string and we'll go to the data folder 17762 15:10:56,720 --> 15:11:03,000 do SL do do SL data SL 17763 15:11:00,000 --> 15:11:05,560 and products. 17764 15:11:03,000 --> 15:11:10,080 JS and let's run that function to get 17765 15:11:05,560 --> 15:11:12,240 the full product so get product brackets 17766 15:11:10,080 --> 15:11:13,840 and then each cart item has the product 17767 15:11:12,240 --> 15:11:18,596 ID so we're going to give it to the 17768 15:11:13,840 --> 15:11:21,360 function cart item. product 17769 15:11:18,596 --> 15:11:23,636 ID and let's save this product in a 17770 15:11:21,360 --> 15:11:27,796 variable so we can use it for our 17771 15:11:23,636 --> 15:11:30,116 calculations con product equals get 17772 15:11:27,796 --> 15:11:32,520 product so now now as we said we're 17773 15:11:30,116 --> 15:11:36,160 going to multiply the product's price by 17774 15:11:32,520 --> 15:11:38,796 the quantity so down here we can do 17775 15:11:36,160 --> 15:11:42,240 product and it has a property 17776 15:11:38,796 --> 15:11:46,320 called price sense and we're going to 17777 15:11:42,240 --> 15:11:49,436 multiply it by the cart item and this 17778 15:11:46,320 --> 15:11:52,240 has a property do 17779 15:11:49,436 --> 15:11:54,276 quantity and the last step is to add all 17780 15:11:52,240 --> 15:11:56,956 of these 17781 15:11:54,276 --> 15:11:59,276 together so outside of the loop let's 17782 15:11:56,956 --> 15:12:02,880 create a variable to save the result of 17783 15:11:59,276 --> 15:12:08,160 adding them together let's type let and 17784 15:12:02,880 --> 15:12:11,200 we'll name it product price sense equals 17785 15:12:08,160 --> 15:12:13,160 and we'll make it zero to start and then 17786 15:12:11,200 --> 15:12:15,796 every time we Loop through the cart 17787 15:12:13,160 --> 15:12:20,916 we're going to add this to this variable 17788 15:12:15,796 --> 15:12:23,400 so we'll type product price sense plus 17789 15:12:20,916 --> 15:12:25,636 equals this value 17790 15:12:23,400 --> 15:12:28,680 here and that's how we calculate the 17791 15:12:25,636 --> 15:12:30,560 cost of the products so at the bottom 17792 15:12:28,680 --> 15:12:35,040 let's 17793 15:12:30,560 --> 15:12:38,080 console.log the product price sense just 17794 15:12:35,040 --> 15:12:41,240 to make sure that this is working we'll 17795 15:12:38,080 --> 15:12:42,160 save and in our project we're going to 17796 15:12:41,240 --> 15:12:44,756 check the 17797 15:12:42,160 --> 15:12:47,360 console and it will give us this number 17798 15:12:44,756 --> 15:12:50,560 here and this number should match the 17799 15:12:47,360 --> 15:12:53,080 number on the page except it's in sense 17800 15:12:50,560 --> 15:12:56,320 and assuming you also have two socks and 17801 15:12:53,080 --> 15:12:58,796 one basketball in your 17802 15:12:56,320 --> 15:13:01,160 cart okay so that's how we calculate 17803 15:12:58,796 --> 15:13:03,680 this first number here now let's 17804 15:13:01,160 --> 15:13:06,680 calculate the second number which is the 17805 15:13:03,680 --> 15:13:08,720 cost of shipping so to calculate this 17806 15:13:06,680 --> 15:13:11,796 we're going to Loop through the cart and 17807 15:13:08,720 --> 15:13:14,276 add all the shipping costs 17808 15:13:11,796 --> 15:13:17,040 together so let's go into our 17809 15:13:14,276 --> 15:13:19,400 code and here we're already looping 17810 15:13:17,040 --> 15:13:21,636 through the cart so instead of creating 17811 15:13:19,400 --> 15:13:25,080 another loop let's just calculate the 17812 15:13:21,636 --> 15:13:26,276 shipping cost in the same Loop so let's 17813 15:13:25,080 --> 15:13:29,596 create some new 17814 15:13:26,276 --> 15:13:32,320 lines and each card item has a property 17815 15:13:29,596 --> 15:13:35,040 delivery option ID so we need to use 17816 15:13:32,320 --> 15:13:38,720 this ID to get the full delivery option 17817 15:13:35,040 --> 15:13:41,080 which has the price so in the order 17818 15:13:38,720 --> 15:13:43,480 summary we also have this code that 17819 15:13:41,080 --> 15:13:46,080 takes the delivery option ID and finds 17820 15:13:43,480 --> 15:13:48,560 the full delivery option so again let's 17821 15:13:46,080 --> 15:13:49,720 create a function to share and reuse 17822 15:13:48,560 --> 15:13:52,680 this 17823 15:13:49,720 --> 15:13:55,680 code so first let's select this code and 17824 15:13:52,680 --> 15:13:58,080 we'll write click and 17825 15:13:55,680 --> 15:13:59,720 copy and let's go to the delivery 17826 15:13:58,080 --> 15:14:02,636 options. Js 17827 15:13:59,720 --> 15:14:03,756 file and then at the bottom we'll create 17828 15:14:02,636 --> 15:14:07,916 a 17829 15:14:03,756 --> 15:14:11,360 function let's call it get delivery 17830 15:14:07,916 --> 15:14:14,400 option brackets and curly brackets and 17831 15:14:11,360 --> 15:14:15,956 we'll give this function the delivery 17832 15:14:14,400 --> 15:14:19,756 option 17833 15:14:15,956 --> 15:14:23,116 ID and inside here we'll rightclick and 17834 15:14:19,756 --> 15:14:25,080 paste the code that we copied earlier so 17835 15:14:23,116 --> 15:14:28,360 this will take this ID and get the 17836 15:14:25,080 --> 15:14:30,720 delivery option from this array and 17837 15:14:28,360 --> 15:14:34,720 let's return return this delivery 17838 15:14:30,720 --> 15:14:37,840 option so we can use it outside of this 17839 15:14:34,720 --> 15:14:40,436 function and just to be safe let's also 17840 15:14:37,840 --> 15:14:43,560 give this a default value if we don't 17841 15:14:40,436 --> 15:14:45,840 find a delivery option so we'll use the 17842 15:14:43,560 --> 15:14:48,880 default operator 17843 15:14:45,840 --> 15:14:53,240 or and let's make the default value the 17844 15:14:48,880 --> 15:14:58,320 first delivery option so delivery 17845 15:14:53,240 --> 15:14:58,320 options square bracket 0 17846 15:15:00,276 --> 15:15:06,160 so now let's export this and 17847 15:15:03,436 --> 15:15:08,956 save and then inside order summary we'll 17848 15:15:06,160 --> 15:15:11,400 use this function so at the 17849 15:15:08,956 --> 15:15:14,720 top we're already importing from 17850 15:15:11,400 --> 15:15:17,796 delivery options so we'll type a comma 17851 15:15:14,720 --> 15:15:20,596 and get delivery 17852 15:15:17,796 --> 15:15:23,956 option and then down here instead of 17853 15:15:20,596 --> 15:15:26,840 running all this code we'll select it 17854 15:15:23,956 --> 15:15:29,880 and make this equal and run the function 17855 15:15:26,840 --> 15:15:32,400 get delivery option 17856 15:15:29,880 --> 15:15:36,636 brackets and we'll give it this delivery 17857 15:15:32,400 --> 15:15:40,080 option ID delivery option 17858 15:15:36,636 --> 15:15:42,596 ID and we can change from let to const 17859 15:15:40,080 --> 15:15:45,116 since we're not changing this variable 17860 15:15:42,596 --> 15:15:48,116 so now let's save and let's use this 17861 15:15:45,116 --> 15:15:52,636 function inside payment summary as well 17862 15:15:48,116 --> 15:15:57,320 so in here at the top we'll import curly 17863 15:15:52,636 --> 15:16:00,320 brackets the get delivery option 17864 15:15:57,320 --> 15:16:04,636 function from 17865 15:16:00,320 --> 15:16:11,480 quotes the data folder do do slash do 17866 15:16:04,636 --> 15:16:11,480 data slash and the delivery options. JS 17867 15:16:11,636 --> 15:16:18,160 file and now we'll run this function get 17868 15:16:15,520 --> 15:16:20,680 delivery option and 17869 15:16:18,160 --> 15:16:23,200 brackets and each cart item has a 17870 15:16:20,680 --> 15:16:28,240 delivery option ID so we'll put that 17871 15:16:23,200 --> 15:16:29,596 into the function cart item. delivery 17872 15:16:28,240 --> 15:16:32,520 option 17873 15:16:29,596 --> 15:16:38,480 ID and let's save this in a 17874 15:16:32,520 --> 15:16:41,160 variable con delivery option 17875 15:16:38,480 --> 15:16:45,400 equals and now we can get the price for 17876 15:16:41,160 --> 15:16:49,240 the delivery option using delivery 17877 15:16:45,400 --> 15:16:51,560 option price sense 17878 15:16:49,240 --> 15:16:54,276 property and finally we're going to add 17879 15:16:51,560 --> 15:16:57,796 all these together so at the top let's 17880 15:16:54,276 --> 15:16:59,636 create a variable to save the result let 17881 15:16:57,796 --> 15:17:04,956 we'll call it ship 17882 15:16:59,636 --> 15:17:07,480 shipping price cents equals zero and 17883 15:17:04,956 --> 15:17:08,436 when we Loop through the cart we're 17884 15:17:07,480 --> 15:17:14,636 going to 17885 15:17:08,436 --> 15:17:17,596 do shipping price sents plus equals this 17886 15:17:14,636 --> 15:17:17,596 delivery options 17887 15:17:17,880 --> 15:17:26,080 price and let's also console.log this to 17888 15:17:21,680 --> 15:17:29,436 make sure it's working so shipping price 17889 15:17:26,080 --> 15:17:31,636 cents and Save 17890 15:17:29,436 --> 15:17:33,796 now let's go to our project and check 17891 15:17:31,636 --> 15:17:33,796 the 17892 15:17:34,796 --> 15:17:40,116 console and this value 17893 15:17:37,400 --> 15:17:43,756 here should be the same as adding these 17894 15:17:40,116 --> 15:17:43,756 two numbers together in 17895 15:17:44,116 --> 15:17:50,040 sense so that's how we calculate the 17896 15:17:46,596 --> 15:17:51,040 second number the shipping price so note 17897 15:17:50,040 --> 15:17:54,080 that this 17898 15:17:51,040 --> 15:17:56,360 $4.99 number is just a practice number 17899 15:17:54,080 --> 15:17:58,520 right now the actual shipping price 17900 15:17:56,360 --> 15:18:01,636 should be the total of adding all these 17901 15:17:58,520 --> 15:18:01,636 sh shipping prices 17902 15:18:01,756 --> 15:18:06,596 together the next number we need to 17903 15:18:03,796 --> 15:18:09,276 calculate is the total before tax so let 17904 15:18:06,596 --> 15:18:11,160 just adding these two numbers together 17905 15:18:09,276 --> 15:18:14,840 inside our 17906 15:18:11,160 --> 15:18:20,400 code let's remove these console. logs 17907 15:18:14,840 --> 15:18:23,680 and we'll create const total before tax 17908 15:18:20,400 --> 15:18:28,116 sense and make it equal and we'll add 17909 15:18:23,680 --> 15:18:32,000 these two together product price sense 17910 15:18:28,116 --> 15:18:36,276 Plus shipping price 17911 15:18:32,000 --> 15:18:39,080 sense and the next number is the tax so 17912 15:18:36,276 --> 15:18:41,880 we're going to calculate a 10% tax on 17913 15:18:39,080 --> 15:18:45,880 this number here to calculate 10% of 17914 15:18:41,880 --> 15:18:50,720 something we can multiply this by 10 / 17915 15:18:45,880 --> 15:18:52,840 100 that's what percent means so 10 ID 17916 15:18:50,720 --> 15:18:55,720 100 is also equal to 17917 15:18:52,840 --> 15:18:57,080 0.1 so we can also multiply this number 17918 15:18:55,720 --> 15:19:00,596 by 17919 15:18:57,080 --> 15:19:05,276 0.1 let's go into code and we'll create 17920 15:19:00,596 --> 15:19:11,880 a variable const tax sense make it equal 17921 15:19:05,276 --> 15:19:13,520 to the total before tax sense times 0.1 17922 15:19:11,880 --> 15:19:16,720 or 17923 15:19:13,520 --> 15:19:19,680 10% finally to calculate the total we're 17924 15:19:16,720 --> 15:19:25,720 going to add these two numbers together 17925 15:19:19,680 --> 15:19:31,360 so const total sense equals the total 17926 15:19:25,720 --> 15:19:34,080 before tax sense Plus the tax 17927 15:19:31,360 --> 15:19:36,520 sense so that's how we calculate all the 17928 15:19:34,080 --> 15:19:39,796 numbers in the payment summary and now 17929 15:19:36,520 --> 15:19:43,480 we have all the data that we need next 17930 15:19:39,796 --> 15:19:46,040 we're going to generate the HTML in MVC 17931 15:19:43,480 --> 15:19:49,596 this is called The 17932 15:19:46,040 --> 15:19:51,680 View so at the bottom here let's create 17933 15:19:49,596 --> 15:19:57,240 a variable to store the 17934 15:19:51,680 --> 15:20:00,000 HTML const payment summary 17935 15:19:57,240 --> 15:20:01,480 HTML and we'll make make it equal to a 17936 15:20:00,000 --> 15:20:03,756 template 17937 15:20:01,480 --> 15:20:05,840 string and then inside here we're going 17938 15:20:03,756 --> 15:20:09,480 to copy and paste the 17939 15:20:05,840 --> 15:20:12,680 HTML so let's open checkout. 17940 15:20:09,480 --> 15:20:16,000 HTML and we're going to scroll up until 17941 15:20:12,680 --> 15:20:18,240 we find the class payment D summary so 17942 15:20:16,000 --> 15:20:21,000 we're going to generate all the HTML 17943 15:20:18,240 --> 15:20:23,560 inside here so let's actually select 17944 15:20:21,000 --> 15:20:26,596 this HTML and move it into our 17945 15:20:23,560 --> 15:20:29,956 JavaScript so we'll select it up until 17946 15:20:26,596 --> 15:20:32,720 this button and then we we can 17947 15:20:29,956 --> 15:20:37,320 rightclick 17948 15:20:32,720 --> 15:20:40,040 cut and remove the new line save and 17949 15:20:37,320 --> 15:20:43,000 then inside the payment summary inside 17950 15:20:40,040 --> 15:20:45,160 the screen we'll rightclick and paste 17951 15:20:43,000 --> 15:20:45,160 the 17952 15:20:45,276 --> 15:20:50,436 HTML and if we need to reformat we can 17953 15:20:48,160 --> 15:20:53,400 select these 17954 15:20:50,436 --> 15:20:55,916 lines and press tab a few times to 17955 15:20:53,400 --> 15:20:55,916 reformat 17956 15:20:57,360 --> 15:21:03,680 it and and 17957 15:21:00,680 --> 15:21:06,320 save next instead of using these same 17958 15:21:03,680 --> 15:21:08,720 numbers every time we're going to take 17959 15:21:06,320 --> 15:21:12,200 the numbers we calculated up here and 17960 15:21:08,720 --> 15:21:15,276 insert it into this HTML string so 17961 15:21:12,200 --> 15:21:19,956 instead of 42.75 every time we're going 17962 15:21:15,276 --> 15:21:22,400 to insert the product price 17963 15:21:19,956 --> 15:21:24,840 sense and let's give it some new lines 17964 15:21:22,400 --> 15:21:27,756 to make it a little easier to 17965 15:21:24,840 --> 15:21:30,360 read now remember that this number is in 17966 15:21:27,756 --> 15:21:33,320 sense and we want to display it as 17967 15:21:30,360 --> 15:21:37,160 dollars to do that we already have a 17968 15:21:33,320 --> 15:21:40,040 function for this so in the utils money. 17969 15:21:37,160 --> 15:21:43,116 JS file we have a function format 17970 15:21:40,040 --> 15:21:45,956 currency that displays it in dollars so 17971 15:21:43,116 --> 15:21:48,720 let's use this function inside payment 17972 15:21:45,956 --> 15:21:49,756 summary so at the top let's import the 17973 15:21:48,720 --> 15:21:54,916 function 17974 15:21:49,756 --> 15:21:59,560 first import curly brackets the format 17975 15:21:54,916 --> 15:22:01,680 currency function from a Str 17976 15:21:59,560 --> 15:22:05,520 and to locate this folder we'll go out 17977 15:22:01,680 --> 15:22:10,360 of the checkout folder do do/ the UIL 17978 15:22:05,520 --> 15:22:10,360 folder SL money.jpg 17979 15:22:28,596 --> 15:22:34,916 numbers here so this one is the shipping 17980 15:22:31,596 --> 15:22:40,080 price so we can 17981 15:22:34,916 --> 15:22:43,840 insert format currency brackets the 17982 15:22:40,080 --> 15:22:45,596 shipping price sents and let's give it 17983 15:22:43,840 --> 15:22:47,796 some new lines to make it a little 17984 15:22:45,596 --> 15:22:51,116 easier to 17985 15:22:47,796 --> 15:22:54,880 read next let's insert the total before 17986 15:22:51,116 --> 15:22:59,276 tax so we'll remove this number and 17987 15:22:54,880 --> 15:23:02,596 insert format currency bracket gets the 17988 15:22:59,276 --> 15:23:06,160 total before tax 17989 15:23:02,596 --> 15:23:06,160 sents and new 17990 15:23:07,320 --> 15:23:12,756 lines and next is the tax we'll remove 17991 15:23:11,276 --> 15:23:19,480 this and 17992 15:23:12,756 --> 15:23:23,040 insert format currency brackets the tax 17993 15:23:19,480 --> 15:23:26,636 sents and we'll enter some new lines and 17994 15:23:23,040 --> 15:23:28,840 finally the total so we remove this 17995 15:23:26,636 --> 15:23:34,916 insert 17996 15:23:28,840 --> 15:23:37,520 format currency brackets total sense and 17997 15:23:34,916 --> 15:23:37,520 add some new 17998 15:23:37,636 --> 15:23:43,276 lines and 17999 15:23:40,320 --> 15:23:45,636 save now before we continue I actually 18000 15:23:43,276 --> 15:23:48,080 learned of a small issue with the format 18001 15:23:45,636 --> 15:23:50,080 currency function so let's open the 18002 15:23:48,080 --> 15:23:52,796 money. JS 18003 15:23:50,080 --> 15:23:56,160 file and I recently learned that this 18004 15:23:52,796 --> 15:23:58,956 two fixed method has a small issue with 18005 15:23:56,160 --> 15:24:02,436 rounding so for some numbers that end in 18006 15:23:58,956 --> 15:24:05,680 five it won't round the number 18007 15:24:02,436 --> 15:24:08,840 properly one way to fix this issue is to 18008 15:24:05,680 --> 15:24:11,796 round the price sense first so that this 18009 15:24:08,840 --> 15:24:15,796 method doesn't have to do any rounding 18010 15:24:11,796 --> 15:24:19,796 so at the front here let's just type 18011 15:24:15,796 --> 15:24:22,360 math.round Open Bracket and close 18012 15:24:19,796 --> 15:24:24,480 bracket so this is just a tricky issue 18013 15:24:22,360 --> 15:24:26,680 that I discovered with the help of AI 18014 15:24:24,480 --> 15:24:29,080 recently we just need to add a little 18015 15:24:26,680 --> 15:24:30,720 bit of code here to fix it 18016 15:24:29,080 --> 15:24:33,840 so now let's save this 18017 15:24:30,720 --> 15:24:36,360 file and go back to payment 18018 15:24:33,840 --> 15:24:39,040 summary so now that we generated all 18019 15:24:36,360 --> 15:24:43,240 this HTML and inserted our numbers into 18020 15:24:39,040 --> 15:24:46,320 it let's put this HTML on the page so 18021 15:24:43,240 --> 15:24:46,320 inside checkout. 18022 15:24:46,520 --> 15:24:50,880 HTML let's give this payment summary 18023 15:24:48,956 --> 15:24:54,916 another class 18024 15:24:50,880 --> 15:24:57,796 js- payment Das summary and then we'll 18025 15:24:54,916 --> 15:24:59,956 select this using the Dom so now let's 18026 15:24:57,796 --> 15:25:03,756 save this 18027 15:24:59,956 --> 15:25:06,880 file inside payment summary at the 18028 15:25:03,756 --> 15:25:11,680 bottom after we create the HTML we're 18029 15:25:06,880 --> 15:25:15,680 going to document. query selector 18030 15:25:11,680 --> 15:25:21,116 brackets and a string and we'll select 18031 15:25:15,680 --> 15:25:24,276 js- payment D summary to get that 18032 15:25:21,116 --> 15:25:29,040 element and we'll change the HTML inside 18033 15:25:24,276 --> 15:25:32,400 that element using do inner Capital H 18034 15:25:29,040 --> 15:25:35,040 HTML and we'll make it equal to the 18035 15:25:32,400 --> 15:25:39,320 payment summary 18036 15:25:35,040 --> 15:25:42,956 HTML that we just created so now let's 18037 15:25:39,320 --> 15:25:45,680 save and check our 18038 15:25:42,956 --> 15:25:47,956 project and now these numbers on the 18039 15:25:45,680 --> 15:25:51,596 right should match what we have in the 18040 15:25:47,956 --> 15:25:54,000 cart and the delivery options that we 18041 15:25:51,596 --> 15:25:56,560 selected so that's how we generate the 18042 15:25:54,000 --> 15:26:00,116 HTML for this section 18043 15:25:56,560 --> 15:26:01,320 here now let's save our changes into git 18044 15:26:00,116 --> 15:26:03,956 we'll open 18045 15:26:01,320 --> 15:26:08,080 git and it will tell us in checkout. 18046 15:26:03,956 --> 15:26:11,080 HTML we removed the HTML we created 18047 15:26:08,080 --> 15:26:13,320 manually in delivery options. JS we 18048 15:26:11,080 --> 15:26:15,520 created this shared function for finding 18049 15:26:13,320 --> 15:26:17,636 the full delivery option and the same 18050 15:26:15,520 --> 15:26:18,880 thing in products. JS for finding the 18051 15:26:17,636 --> 15:26:21,796 full 18052 15:26:18,880 --> 15:26:24,596 product inside checkout. JS we're now 18053 15:26:21,796 --> 15:26:27,200 running all the code inside payment 18054 15:26:24,596 --> 15:26:29,276 summary in order summary we're just 18055 15:26:27,200 --> 15:26:32,520 using those shared functions that we 18056 15:26:29,276 --> 15:26:35,040 created and then in payment summary we 18057 15:26:32,520 --> 15:26:36,720 calculated the numbers and generated all 18058 15:26:35,040 --> 15:26:40,720 the 18059 15:26:36,720 --> 15:26:43,796 HTML and finally in money. JS we fixed a 18060 15:26:40,720 --> 15:26:47,320 small bug related to rounding let's type 18061 15:26:43,796 --> 15:26:51,200 a message to describe our changes save 18062 15:26:47,320 --> 15:26:55,360 data and generate 18063 15:26:51,200 --> 15:26:59,756 HTML for payment 18064 15:26:55,360 --> 15:26:59,756 summary and commit 18065 15:27:03,160 --> 15:27:09,240 the last step is to make it interactive 18066 15:27:06,160 --> 15:27:11,956 in MVC this is called the 18067 15:27:09,240 --> 15:27:14,320 controller let's go to our 18068 15:27:11,956 --> 15:27:16,680 project and one thing we can make 18069 15:27:14,320 --> 15:27:19,596 interactive in this section is the place 18070 15:27:16,680 --> 15:27:22,880 order button however we haven't figured 18071 15:27:19,596 --> 15:27:25,880 out how orders work yet so we'll work on 18072 15:27:22,880 --> 15:27:28,796 this in the next lesson another thing we 18073 15:27:25,880 --> 15:27:32,080 can make interactive however is when we 18074 15:27:28,796 --> 15:27:35,276 change the cart on the left these prices 18075 15:27:32,080 --> 15:27:37,880 should also update so let's make this 18076 15:27:35,276 --> 15:27:41,720 interactive first when we delete a 18077 15:27:37,880 --> 15:27:44,916 product the prices here should update so 18078 15:27:41,720 --> 15:27:46,360 let's actually practice using MVC to 18079 15:27:44,916 --> 15:27:49,240 create this 18080 15:27:46,360 --> 15:27:52,400 functionality according to MVC when we 18081 15:27:49,240 --> 15:27:55,160 click delete we should update the data 18082 15:27:52,400 --> 15:27:58,116 and then regenerate all of this 18083 15:27:55,160 --> 15:28:01,040 HTML so let's get started and go to the 18084 15:27:58,116 --> 15:28:04,916 code for the delete link so this is in 18085 15:28:01,040 --> 15:28:08,160 the order summary section so in our code 18086 15:28:04,916 --> 15:28:11,520 let's open the orders summary. JS 18087 15:28:08,160 --> 15:28:14,276 file and we'll scroll down and find the 18088 15:28:11,520 --> 15:28:16,840 event listener for the delete link which 18089 15:28:14,276 --> 15:28:19,840 is right 18090 15:28:16,840 --> 15:28:22,116 here when we click delete first we're 18091 15:28:19,840 --> 15:28:25,520 going to update the 18092 15:28:22,116 --> 15:28:29,000 data so we already update the data using 18093 15:28:25,520 --> 15:28:31,680 this function remove from cart 18094 15:28:29,000 --> 15:28:34,360 and now the next step according to MBC 18095 15:28:31,680 --> 15:28:37,680 is to just regenerate all the 18096 15:28:34,360 --> 15:28:40,720 HTML so to regenerate the HTML for the 18097 15:28:37,680 --> 15:28:43,480 payment summary we created this function 18098 15:28:40,720 --> 15:28:46,160 at the top render payment 18099 15:28:43,480 --> 15:28:50,160 summary so this will recalculate all the 18100 15:28:46,160 --> 15:28:53,000 numbers and then regenerate the 18101 15:28:50,160 --> 15:28:56,320 HTML so let's import this 18102 15:28:53,000 --> 15:28:58,596 function in order summary. JS we'll 18103 15:28:56,320 --> 15:29:02,040 scroll to the top 18104 15:28:58,596 --> 15:29:07,160 and down here we can import curly 18105 15:29:02,040 --> 15:29:11,320 brackets the render payment summary 18106 15:29:07,160 --> 15:29:14,116 function from a string and we'll locate 18107 15:29:11,320 --> 15:29:16,160 the payment summary file so it's in the 18108 15:29:14,116 --> 15:29:19,680 same folder so we're going to start with 18109 15:29:16,160 --> 15:29:24,596 a slash which is the current folder the 18110 15:29:19,680 --> 15:29:27,320 payment summary. JS file and now let's 18111 15:29:24,596 --> 15:29:29,916 use this function in the event listener 18112 15:29:27,320 --> 15:29:33,276 so let's scroll 18113 15:29:29,916 --> 15:29:36,240 down when we click delete we're going to 18114 15:29:33,276 --> 15:29:42,636 update the data and then regenerate all 18115 15:29:36,240 --> 15:29:47,880 the HTML using render payment summary 18116 15:29:42,636 --> 15:29:50,400 brackets now let's save and go to our 18117 15:29:47,880 --> 15:29:53,160 project focus on the numbers on the 18118 15:29:50,400 --> 15:29:55,400 right here and now let's click 18119 15:29:53,160 --> 15:29:59,720 delete and you'll notice that all of 18120 15:29:55,400 --> 15:30:03,080 these numbers updated to match our new 18121 15:29:59,720 --> 15:30:05,240 so that's another example of using MVC 18122 15:30:03,080 --> 15:30:07,756 when we click delete instead of using 18123 15:30:05,240 --> 15:30:10,880 the Dom to change these numbers one by 18124 15:30:07,756 --> 15:30:13,040 one we just update the data and then 18125 15:30:10,880 --> 15:30:17,116 regenerate all of the 18126 15:30:13,040 --> 15:30:20,360 HTML MVC makes sure that the HTML always 18127 15:30:17,116 --> 15:30:22,480 matches the data or that the view always 18128 15:30:20,360 --> 15:30:25,956 matches the 18129 15:30:22,480 --> 15:30:28,560 model let's do one last example of NBC 18130 15:30:25,956 --> 15:30:31,400 for this lesson when we change the 18131 15:30:28,560 --> 15:30:34,436 delivery options here we also want these 18132 15:30:31,400 --> 15:30:37,040 numbers on the right to update so let's 18133 15:30:34,436 --> 15:30:38,956 find the code for the delivery options 18134 15:30:37,040 --> 15:30:42,320 again this is in the order summary 18135 15:30:38,956 --> 15:30:47,040 section so in our code we'll open orders 18136 15:30:42,320 --> 15:30:49,276 summary. JS scroll down and then here we 18137 15:30:47,040 --> 15:30:52,080 updated the data and then we're just 18138 15:30:49,276 --> 15:30:56,720 going to regenerate the HTML for the 18139 15:30:52,080 --> 15:30:58,880 payment summary using render payment 18140 15:30:56,720 --> 15:31:01,000 summary and and 18141 15:30:58,880 --> 15:31:04,200 brackets now let's 18142 15:31:01,000 --> 15:31:06,680 save and then look at our project and 18143 15:31:04,200 --> 15:31:08,840 let's try changing the delivery option 18144 15:31:06,680 --> 15:31:11,560 here and you'll notice the numbers on 18145 15:31:08,840 --> 15:31:14,636 the right update automatically to match 18146 15:31:11,560 --> 15:31:17,200 the cart and that's it we finished the 18147 15:31:14,636 --> 15:31:20,436 checkout page so now let's save our code 18148 15:31:17,200 --> 15:31:24,956 into git we'll go into the git 18149 15:31:20,436 --> 15:31:27,160 section here and inside order summary we 18150 15:31:24,956 --> 15:31:28,636 basically imported the render payment 18151 15:31:27,160 --> 15:31:30,720 summary function 18152 15:31:28,636 --> 15:31:34,080 and then at the 18153 15:31:30,720 --> 15:31:37,916 bottom we used MVC so when we click 18154 15:31:34,080 --> 15:31:40,916 delete we update the data or the model 18155 15:31:37,916 --> 15:31:44,160 and then we regenerate all the HTML or 18156 15:31:40,916 --> 15:31:46,480 regenerate The View down here same thing 18157 15:31:44,160 --> 15:31:49,276 when we click a delivery option we 18158 15:31:46,480 --> 15:31:50,636 update the data and then regenerate all 18159 15:31:49,276 --> 15:31:53,596 the 18160 15:31:50,636 --> 15:31:57,636 HTML the last thing we're going to do is 18161 15:31:53,596 --> 15:31:57,636 at the top of this file 18162 15:31:58,040 --> 15:32:03,480 we're going to remove this practice code 18163 15:32:00,160 --> 15:32:07,320 that we use to learn external libraries 18164 15:32:03,480 --> 15:32:07,320 so let's open orders summary. 18165 15:32:07,480 --> 15:32:12,956 JS and at the top we're going to remove 18166 15:32:10,880 --> 15:32:15,636 this 18167 15:32:12,956 --> 15:32:19,636 code and 18168 15:32:15,636 --> 15:32:19,636 save now let's go back into 18169 15:32:20,000 --> 15:32:26,480 get and that looks good finally let's 18170 15:32:24,160 --> 15:32:28,880 type a message describing our 18171 15:32:26,480 --> 15:32:30,400 changes make 18172 15:32:28,880 --> 15:32:32,520 payment 18173 15:32:30,400 --> 15:32:35,956 summary 18174 15:32:32,520 --> 15:32:35,956 interactive and 18175 15:32:39,400 --> 15:32:44,956 commit and that's the end of this lesson 18176 15:32:42,560 --> 15:32:47,756 in this lesson we learned about external 18177 15:32:44,956 --> 15:32:51,116 libraries which is code that is outside 18178 15:32:47,756 --> 15:32:53,756 of our project we learned about the DJs 18179 15:32:51,116 --> 15:32:56,636 external Library which helps us work 18180 15:32:53,756 --> 15:32:58,720 with dates we also learned how to use 18181 15:32:56,636 --> 15:33:01,596 external libraries with JavaScript 18182 15:32:58,720 --> 15:33:05,000 modules together as well as the default 18183 15:33:01,596 --> 15:33:07,796 export syntax we created the delivery 18184 15:33:05,000 --> 15:33:10,720 section of the checkout page we learned 18185 15:33:07,796 --> 15:33:14,000 MBC which is a better way to design our 18186 15:33:10,720 --> 15:33:16,756 code and we created the payment section 18187 15:33:14,000 --> 15:33:16,756 of the checkout 18188 15:33:17,480 --> 15:33:22,520 page thanks for watching this course so 18189 15:33:20,000 --> 15:33:24,480 far if you want to support this channel 18190 15:33:22,520 --> 15:33:26,796 you can check out the premium version of 18191 15:33:24,480 --> 15:33:29,240 my courses just like this one where you 18192 15:33:26,796 --> 15:33:31,636 can get a certificate of completion and 18193 15:33:29,240 --> 15:33:34,400 a Better Learning platform if you're 18194 15:33:31,636 --> 15:33:36,956 interested in extra videos from me more 18195 15:33:34,400 --> 15:33:39,720 in-depth videos and behind the scenes 18196 15:33:36,956 --> 15:33:42,400 content you can check out these links 18197 15:33:39,720 --> 15:33:45,240 here you can also support the channel by 18198 15:33:42,400 --> 15:33:48,040 liking the video subscribing and sharing 18199 15:33:45,240 --> 15:33:50,720 this video with others thanks again and 18200 15:33:48,040 --> 15:33:53,320 let's continue with the 18201 15:33:50,720 --> 15:33:56,636 course here are some exercises to help 18202 15:33:53,320 --> 15:34:00,680 you practice what we learned in this 18203 15:33:56,636 --> 15:34:00,680 lesson e 18204 15:34:40,720 --> 15:34:45,560 in the rest of this course if you get an 18205 15:34:43,116 --> 15:34:48,560 error in your project but your code 18206 15:34:45,560 --> 15:34:52,080 looks correct you may have some bad data 18207 15:34:48,560 --> 15:34:55,400 saved in local storage to fix this you 18208 15:34:52,080 --> 15:34:57,916 can try running local storage. CLE in 18209 15:34:55,400 --> 15:34:58,720 your console to remove everything from 18210 15:34:57,916 --> 15:35:02,880 local 18211 15:34:58,720 --> 15:35:02,880 storage and then refresh the 18212 15:35:03,240 --> 15:35:09,116 page if you see an error like this 18213 15:35:06,400 --> 15:35:11,796 object object that means that you saved 18214 15:35:09,116 --> 15:35:15,080 something directly into local storage 18215 15:35:11,796 --> 15:35:16,720 but you have to json.stringify it first 18216 15:35:15,080 --> 15:35:19,116 so check your code to make sure that 18217 15:35:16,720 --> 15:35:21,720 you're always stringifying before saving 18218 15:35:19,116 --> 15:35:25,680 to local storage and then clear your 18219 15:35:21,720 --> 15:35:25,680 local storage and refresh the 18220 15:35:26,560 --> 15:35:32,040 page in this lesson we're going to learn 18221 15:35:29,360 --> 15:35:34,796 how to test our code so when we're 18222 15:35:32,040 --> 15:35:37,756 writing code it's important to test our 18223 15:35:34,796 --> 15:35:39,116 code to see if it's working correctly 18224 15:35:37,756 --> 15:35:41,796 and if you're working at a company in 18225 15:35:39,116 --> 15:35:44,596 the future you'll also be expected to 18226 15:35:41,796 --> 15:35:46,000 test your code so let's learn how to do 18227 15:35:44,596 --> 15:35:48,956 that in this 18228 15:35:46,000 --> 15:35:51,596 lesson the easiest way to test our code 18229 15:35:48,956 --> 15:35:55,956 is to just open the website and try out 18230 15:35:51,596 --> 15:36:01,756 the code for example in our code here 18231 15:35:55,956 --> 15:36:01,756 let's open the file utils money.jpg 18232 15:36:30,436 --> 15:36:36,636 server so on this website we have a 18233 15:36:33,320 --> 15:36:38,840 couple of prices here as well as here 18234 15:36:36,636 --> 15:36:40,560 and we created these prices using the 18235 15:36:38,840 --> 15:36:42,520 function format 18236 15:36:40,560 --> 15:36:44,480 currency so if we just look at the 18237 15:36:42,520 --> 15:36:47,000 website we can see that these prices 18238 15:36:44,480 --> 15:36:49,756 look correct and this tells us that the 18239 15:36:47,000 --> 15:36:52,320 function format currency is working 18240 15:36:49,756 --> 15:36:54,796 correctly so that's one way to test that 18241 15:36:52,320 --> 15:36:58,436 function we just open the website and 18242 15:36:54,796 --> 15:37:00,756 try it out so this is called manual 18243 15:36:58,436 --> 15:37:03,956 testing when we manually open the 18244 15:37:00,756 --> 15:37:06,320 website and try our code so manual 18245 15:37:03,956 --> 15:37:08,956 testing is really useful for quickly 18246 15:37:06,320 --> 15:37:11,320 checking to see if everything is 18247 15:37:08,956 --> 15:37:14,756 working so now let's talk about the 18248 15:37:11,320 --> 15:37:18,200 disadvantages of manual testing one 18249 15:37:14,756 --> 15:37:21,480 disadvantage is it's hard to test every 18250 15:37:18,200 --> 15:37:23,436 situation for example here we just 18251 15:37:21,480 --> 15:37:26,956 happen to have the number 18252 15:37:23,436 --> 15:37:28,560 295 and we test with that however what 18253 15:37:26,956 --> 15:37:31,520 if we want to test with different 18254 15:37:28,560 --> 15:37:35,000 numbers like the number zero or a number 18255 15:37:31,520 --> 15:37:37,400 that requires rounding like 18256 15:37:35,000 --> 15:37:39,720 2.5 in order to test all these 18257 15:37:37,400 --> 15:37:42,436 situations we have to manually set up 18258 15:37:39,720 --> 15:37:46,560 our website so that our code runs with 18259 15:37:42,436 --> 15:37:49,360 those numbers so here we ran with 2095 18260 15:37:46,560 --> 15:37:52,756 as well as zero so we covered those 18261 15:37:49,360 --> 15:37:55,560 situations however we did not cover 18262 15:37:52,756 --> 15:37:58,000 2.5 so we have to do a bunch of setup in 18263 15:37:55,560 --> 15:38:00,200 order to test that situation 18264 15:37:58,000 --> 15:38:02,636 so as you can see it takes a lot of time 18265 15:38:00,200 --> 15:38:05,560 and effort to test different situations 18266 15:38:02,636 --> 15:38:07,840 in our code the second disadvantage of 18267 15:38:05,560 --> 15:38:11,560 manual testing is that it's hard to 18268 15:38:07,840 --> 15:38:14,636 retest the code for example if we change 18269 15:38:11,560 --> 15:38:16,880 some of our code how do we retest format 18270 15:38:14,636 --> 15:38:18,240 currency and make sure that it's still 18271 15:38:16,880 --> 15:38:20,360 working 18272 15:38:18,240 --> 15:38:22,916 correctly well we would have to open the 18273 15:38:20,360 --> 15:38:25,636 website and set up all the situations 18274 15:38:22,916 --> 15:38:28,240 again and retest all those situations 18275 15:38:25,636 --> 15:38:30,796 manually so this takes too much time and 18276 15:38:28,240 --> 15:38:33,796 effort to retest the 18277 15:38:30,796 --> 15:38:35,796 code to solve these two problems we're 18278 15:38:33,796 --> 15:38:38,360 going to learn another way to test our 18279 15:38:35,796 --> 15:38:41,080 code called automated 18280 15:38:38,360 --> 15:38:45,040 testing automated testing basically 18281 15:38:41,080 --> 15:38:47,080 means using code to test code instead of 18282 15:38:45,040 --> 15:38:49,720 manually opening the website and 18283 15:38:47,080 --> 15:38:51,596 clicking around to try our code we're 18284 15:38:49,720 --> 15:38:54,276 going to tell the computer to do these 18285 15:38:51,596 --> 15:38:57,680 tests for us and this will save a lot of 18286 15:38:54,276 --> 15:39:01,160 time and effort so let's go ahead and 18287 15:38:57,680 --> 15:39:02,560 create our first automated test we're 18288 15:39:01,160 --> 15:39:06,080 going to go back to our 18289 15:39:02,560 --> 15:39:09,240 code and again let's test this format 18290 15:39:06,080 --> 15:39:11,520 currency function to create an automated 18291 15:39:09,240 --> 15:39:14,436 test we're just going to write some code 18292 15:39:11,520 --> 15:39:17,000 to test this function so first let's 18293 15:39:14,436 --> 15:39:19,480 create a folder to group all of our test 18294 15:39:17,000 --> 15:39:22,636 code together we're going to click this 18295 15:39:19,480 --> 15:39:27,520 icon to create a folder and let's just 18296 15:39:22,636 --> 15:39:29,360 name this tests and press enter 18297 15:39:27,520 --> 15:39:32,160 and we're going to move this folder so 18298 15:39:29,360 --> 15:39:35,400 we'll drag it to the top level here 18299 15:39:32,160 --> 15:39:35,400 beside the HTML 18300 15:39:35,720 --> 15:39:40,596 files so we want the test folder to be 18301 15:39:38,436 --> 15:39:43,436 beside the scripts 18302 15:39:40,596 --> 15:39:45,596 folder next let's click on the test 18303 15:39:43,436 --> 15:39:48,796 folder and we're going to create our 18304 15:39:45,596 --> 15:39:51,320 first test file we'll click 18305 15:39:48,796 --> 15:39:56,200 this and we're going to test format 18306 15:39:51,320 --> 15:39:56,200 currency which is inside the money.jpg 18307 15:39:57,720 --> 15:40:02,720 money 18308 15:40:00,480 --> 15:40:06,360 test.js so we know that this file 18309 15:40:02,720 --> 15:40:08,596 contains tests for money. JS so let's 18310 15:40:06,360 --> 15:40:08,596 press 18311 15:40:08,840 --> 15:40:14,160 enter and inside this file we're going 18312 15:40:11,400 --> 15:40:16,880 to write some code to test format 18313 15:40:14,160 --> 15:40:20,400 currency so first let's import format 18314 15:40:16,880 --> 15:40:25,320 currency so we can test it at the top 18315 15:40:20,400 --> 15:40:27,360 we'll type import curly brackets format 18316 15:40:25,320 --> 15:40:30,636 currency 18317 15:40:27,360 --> 15:40:34,200 from a string and from here we're going 18318 15:40:30,636 --> 15:40:36,796 to locate money. JS so we start out in 18319 15:40:34,200 --> 15:40:39,796 money test.js and we need to get out of 18320 15:40:36,796 --> 15:40:42,520 the test folder so we'll type do do 18321 15:40:39,796 --> 15:40:46,200 slash to get out of this folder and then 18322 15:40:42,520 --> 15:40:49,956 we'll go into the scripts folder scripts 18323 15:40:46,200 --> 15:40:53,400 slash and then into the utils folder 18324 15:40:49,956 --> 15:40:57,400 utils slash and locat 18325 15:40:53,400 --> 15:40:57,400 money. Js 18326 15:40:57,956 --> 15:41:04,956 so as a reminder format currency takes a 18327 15:41:00,916 --> 15:41:08,880 number in cents like 295 cents and then 18328 15:41:04,956 --> 15:41:08,880 converts it into Dollars like 18329 15:41:09,680 --> 15:41:16,596 $20.95 so first let's test this function 18330 15:41:12,400 --> 15:41:20,400 using that number 2095 as an example 18331 15:41:16,596 --> 15:41:24,796 we're going to run the function format 18332 15:41:20,400 --> 15:41:27,276 currency brackets and give it 18333 15:41:24,796 --> 15:41:30,636 295 and now we're going to check if the 18334 15:41:27,276 --> 15:41:34,000 result of this function is correct to do 18335 15:41:30,636 --> 15:41:38,916 that we can just use an if statement so 18336 15:41:34,000 --> 15:41:41,560 we'll type if Open Bracket close 18337 15:41:38,916 --> 15:41:42,796 bracket and we said we expect format 18338 15:41:41,560 --> 15:41:47,080 currency 18339 15:41:42,796 --> 15:41:47,080 295 to give us the string 18340 15:41:53,916 --> 15:41:59,436 20959 and curly brackets and and if 18341 15:41:57,480 --> 15:42:02,480 these two are equal that means the code 18342 15:41:59,436 --> 15:42:07,400 is working correctly so we're going to 18343 15:42:02,480 --> 15:42:11,680 console. log the string 18344 15:42:07,400 --> 15:42:15,240 passed to say that this test has passed 18345 15:42:11,680 --> 15:42:15,240 now if it is not equal to 18346 15:42:26,116 --> 15:42:30,400 20.95% 18347 15:42:28,040 --> 15:42:33,916 failed and 18348 15:42:30,400 --> 15:42:36,680 save and that's it this is our first 18349 15:42:33,916 --> 15:42:39,756 automated test it checks if the result 18350 15:42:36,680 --> 15:42:42,200 of this function is correct if it is 18351 15:42:39,756 --> 15:42:43,916 correct we're going to display past and 18352 15:42:42,200 --> 15:42:45,880 if it's not correct we're going to 18353 15:42:43,916 --> 15:42:49,636 display 18354 15:42:45,880 --> 15:42:52,480 failed now let's run this test code so 18355 15:42:49,636 --> 15:42:55,200 remember we can't run JavaScript files 18356 15:42:52,480 --> 15:42:59,040 directly we need to load this Javascript 18357 15:42:55,200 --> 15:43:03,360 file using an HTML file so let's create 18358 15:42:59,040 --> 15:43:05,916 an HTML file to run this test inside the 18359 15:43:03,360 --> 15:43:07,560 test folder we're going to create a new 18360 15:43:05,916 --> 15:43:11,480 file by clicking up 18361 15:43:07,560 --> 15:43:13,080 here and let's name this file 18362 15:43:11,480 --> 15:43:16,240 tests. 18363 15:43:13,080 --> 15:43:18,756 HTML and press 18364 15:43:16,240 --> 15:43:22,680 enter and in here we're going to start 18365 15:43:18,756 --> 15:43:25,320 by creating a basic HTML structure so as 18366 15:43:22,680 --> 15:43:27,636 a reminder at the top we'll type less 18367 15:43:25,320 --> 15:43:32,520 than exclamation 18368 15:43:27,636 --> 15:43:35,160 dock type space HTML greater than and 18369 15:43:32,520 --> 15:43:38,276 we'll create an HTML 18370 15:43:35,160 --> 15:43:39,400 element and then inside we'll create a 18371 15:43:38,276 --> 15:43:44,040 head 18372 15:43:39,400 --> 15:43:46,520 element as well as a body 18373 15:43:44,040 --> 15:43:49,240 element and then inside the body we're 18374 15:43:46,520 --> 15:43:50,436 going to create a script tag to run 18375 15:43:49,240 --> 15:43:52,520 JavaScript 18376 15:43:50,436 --> 15:43:57,040 code and we're going to give the script 18377 15:43:52,520 --> 15:43:59,200 tag an attribute called SRC or source 18378 15:43:57,040 --> 15:44:01,680 equals double quotes and we're going to 18379 15:43:59,200 --> 15:44:05,720 give it this JavaScript file to 18380 15:44:01,680 --> 15:44:05,720 run money 18381 15:44:06,116 --> 15:44:12,796 test.js and also because this file uses 18382 15:44:09,320 --> 15:44:17,720 import we also need to add an attribute 18383 15:44:12,796 --> 15:44:19,756 called type equals modules to give this 18384 15:44:17,720 --> 15:44:21,320 file access to JavaScript module 18385 15:44:19,756 --> 15:44:23,840 features like 18386 15:44:21,320 --> 15:44:26,560 import so now let's 18387 15:44:23,840 --> 15:44:29,200 save and we're going to run this file in 18388 15:44:26,560 --> 15:44:30,240 live server so we're going to click it 18389 15:44:29,200 --> 15:44:33,636 and then 18390 15:44:30,240 --> 15:44:35,636 rightclick open with live 18391 15:44:33,636 --> 15:44:37,680 server and then we're going to check the 18392 15:44:35,636 --> 15:44:39,796 console by right 18393 15:44:37,680 --> 15:44:42,956 clicking 18394 15:44:39,796 --> 15:44:46,200 inspect and open the 18395 15:44:42,956 --> 15:44:48,840 console and our code display the text 18396 15:44:46,200 --> 15:44:52,000 passed so if we look at our 18397 15:44:48,840 --> 15:44:54,400 code and open money 18398 15:44:52,000 --> 15:44:56,560 test.js it's telling us that the result 18399 15:44:54,400 --> 15:44:57,596 of this function matched what we 18400 15:44:56,560 --> 15:44:59,116 expected 18401 15:44:57,596 --> 15:45:02,080 and this test 18402 15:44:59,116 --> 15:45:05,360 passed so that's how we run an automated 18403 15:45:02,080 --> 15:45:08,320 test we just load it like normal 18404 15:45:05,360 --> 15:45:10,756 JavaScript so as you can see automated 18405 15:45:08,320 --> 15:45:14,520 tests are pretty straightforward we're 18406 15:45:10,756 --> 15:45:14,520 just using code to test 18407 15:45:15,436 --> 15:45:22,040 code okay so I mentioned earlier that a 18408 15:45:18,880 --> 15:45:24,080 disadvantage of manual testing is it's 18409 15:45:22,040 --> 15:45:26,436 hard to test every 18410 15:45:24,080 --> 15:45:28,360 situation now let's solve this problem 18411 15:45:26,436 --> 15:45:31,276 using automated 18412 15:45:28,360 --> 15:45:33,680 tests so inside money 18413 15:45:31,276 --> 15:45:36,560 test.js if we want to test format 18414 15:45:33,680 --> 15:45:39,040 currency in different situations or with 18415 15:45:36,560 --> 15:45:41,756 different numbers we can just add some 18416 15:45:39,040 --> 15:45:44,040 more tests at the bottom so at the 18417 15:45:41,756 --> 15:45:47,480 bottom let's create some new lines and 18418 15:45:44,040 --> 15:45:49,360 we'll just add another test using IF 18419 15:45:47,480 --> 15:45:52,360 brackets and curly 18420 15:45:49,360 --> 15:45:55,480 brackets and inside here let's test 18421 15:45:52,360 --> 15:45:57,596 format currency with the number zero so 18422 15:45:55,480 --> 15:46:02,360 we'll type format 18423 15:45:57,596 --> 15:46:07,240 currency brackets 0 and we expect this 18424 15:46:02,360 --> 15:46:12,840 result to be the string 0.0 0 so we'll 18425 15:46:07,240 --> 15:46:12,840 check if this triple equals the string 18426 15:46:12,880 --> 15:46:21,680 0. if this result matches we're going to 18427 15:46:17,880 --> 15:46:25,636 console.log the string 18428 15:46:21,680 --> 15:46:29,636 passed and if this result does not match 18429 15:46:25,636 --> 15:46:31,756 we're going to type else curly brackets 18430 15:46:29,636 --> 15:46:35,240 and 18431 15:46:31,756 --> 15:46:37,756 console.log the string 18432 15:46:35,240 --> 15:46:40,400 failed so that's how we test format 18433 15:46:37,756 --> 15:46:43,840 currency in a different situation with 18434 15:46:40,400 --> 15:46:47,400 the number zero so now let's 18435 15:46:43,840 --> 15:46:50,320 save and then go back to our test page 18436 15:46:47,400 --> 15:46:52,040 which is here and then open the console 18437 15:46:50,320 --> 15:46:53,956 right click 18438 15:46:52,040 --> 15:46:56,560 inspect the 18439 15:46:53,956 --> 15:46:58,636 console and it will tell us that both of 18440 15:46:56,560 --> 15:47:02,116 our automated tests 18441 15:46:58,636 --> 15:47:04,000 passed so as you can see automated tests 18442 15:47:02,116 --> 15:47:07,116 help us test our code in different 18443 15:47:04,000 --> 15:47:09,880 situations very easily this is a lot 18444 15:47:07,116 --> 15:47:13,080 faster than manually opening our website 18445 15:47:09,880 --> 15:47:16,040 and setting it up to test with different 18446 15:47:13,080 --> 15:47:19,756 numbers so let's go back to our 18447 15:47:16,040 --> 15:47:22,680 code and by the way another name for a 18448 15:47:19,756 --> 15:47:26,000 situation that we're testing is called a 18449 15:47:22,680 --> 15:47:28,796 test case so here instead of saying 18450 15:47:26,000 --> 15:47:32,880 we're testing two different situations 18451 15:47:28,796 --> 15:47:35,956 we usually say we're testing two test 18452 15:47:32,880 --> 15:47:39,596 cases okay now let's get more practice 18453 15:47:35,956 --> 15:47:41,596 and add another test case at the bottom 18454 15:47:39,596 --> 15:47:44,956 of this file we're going to create 18455 15:47:41,596 --> 15:47:46,276 another if statement if brackets and 18456 15:47:44,956 --> 15:47:51,596 curly 18457 15:47:46,276 --> 15:47:54,680 brackets and this time let's test format 18458 15:47:51,596 --> 15:47:57,560 currency brackets and we'll give it a 18459 15:47:54,680 --> 15:48:00,080 number that requires some rounding 18460 15:47:57,560 --> 15:48:03,200 so when we calculate the 10% tax on our 18461 15:48:00,080 --> 15:48:06,400 website the result could be a decimal 18462 15:48:03,200 --> 15:48:06,400 like the number 18463 15:48:06,916 --> 15:48:15,680 2000.5 if we use the number 18464 15:48:10,276 --> 15:48:18,756 2.5 this will convert into $20 and 18465 15:48:15,680 --> 15:48:22,000 0.5 however we can't have 18466 15:48:18,756 --> 15:48:27,160 0.5 so we're going to round this up to 1 18467 15:48:22,000 --> 15:48:29,720 cent so we're going to have $20 And1 18468 15:48:27,160 --> 15:48:33,560 so let's check if this result triple 18469 15:48:29,720 --> 15:48:38,956 equals the string 18470 15:48:33,560 --> 15:48:40,880 20.1 and if it is equal we're going to 18471 15:48:38,956 --> 15:48:45,796 console.log 18472 15:48:40,880 --> 15:48:48,040 past and if it is not equal else curly 18473 15:48:45,796 --> 15:48:50,000 brackets 18474 15:48:48,040 --> 15:48:52,520 console.log 18475 15:48:50,000 --> 15:48:56,360 failed and 18476 15:48:52,520 --> 15:48:58,636 save and open our test page and open the 18477 15:48:56,360 --> 15:48:58,636 con 18478 15:48:59,400 --> 15:49:04,956 and you can see that all three of our 18479 15:49:01,240 --> 15:49:07,636 test cases passed and that's it we just 18480 15:49:04,956 --> 15:49:10,796 created three test cases for format 18481 15:49:07,636 --> 15:49:14,720 currency using automated 18482 15:49:10,796 --> 15:49:14,720 tests now let's go back to our 18483 15:49:15,240 --> 15:49:20,956 code and currently we have three test 18484 15:49:18,360 --> 15:49:24,596 cases and you might be wondering how 18485 15:49:20,956 --> 15:49:28,320 many test cases should we have generally 18486 15:49:24,596 --> 15:49:30,680 we create two types of test cases basic 18487 15:49:28,320 --> 15:49:34,040 test cases and edge 18488 15:49:30,680 --> 15:49:37,596 cases a basic test case just tests if 18489 15:49:34,040 --> 15:49:40,956 the code is working or not so here our 18490 15:49:37,596 --> 15:49:43,680 first test case is a basic test case we 18491 15:49:40,956 --> 15:49:46,436 use a normal number like 18492 15:49:43,680 --> 15:49:47,916 2095 and we see if format currency is 18493 15:49:46,436 --> 15:49:50,400 working 18494 15:49:47,916 --> 15:49:53,796 correctly the second type of test cases 18495 15:49:50,400 --> 15:49:56,000 that we should have are edge cases this 18496 15:49:53,796 --> 15:49:58,080 means we test a code with values that 18497 15:49:56,000 --> 15:50:01,360 are a little bit bit tricky they're on 18498 15:49:58,080 --> 15:50:04,080 the edge of what our code can handle for 18499 15:50:01,360 --> 15:50:07,796 example in our test here the last two 18500 15:50:04,080 --> 15:50:10,240 test cases are edge cases zero is sort 18501 15:50:07,796 --> 15:50:13,000 of a special number that's not positive 18502 15:50:10,240 --> 15:50:15,796 or negative and 18503 15:50:13,000 --> 15:50:18,636 2.5 is a number that requires some 18504 15:50:15,796 --> 15:50:21,116 rounding so these numbers are acceptable 18505 15:50:18,636 --> 15:50:23,200 we can use them with format currency but 18506 15:50:21,116 --> 15:50:27,360 they're a little bit tricky and on the 18507 15:50:23,200 --> 15:50:30,000 edge of what our code can handle 18508 15:50:27,360 --> 15:50:32,956 so when creating test cases make sure to 18509 15:50:30,000 --> 15:50:36,916 create basic test cases as well as edge 18510 15:50:32,956 --> 15:50:36,916 cases if there are any edge 18511 15:50:37,000 --> 15:50:43,680 cases now we could have added another 18512 15:50:39,840 --> 15:50:47,276 basic test case like format currency 18513 15:50:43,680 --> 15:50:49,756 1095 however this is very similar to 18514 15:50:47,276 --> 15:50:52,436 2095 it's basically testing the same 18515 15:50:49,756 --> 15:50:54,956 thing so we don't need another test case 18516 15:50:52,436 --> 15:50:57,956 like this generally we should try to 18517 15:50:54,956 --> 15:51:01,080 test something different in each test 18518 15:50:57,956 --> 15:51:04,436 case for example an edge case that we 18519 15:51:01,080 --> 15:51:07,080 have not tested is a number 18520 15:51:04,436 --> 15:51:11,360 200.4 so this would check if the code 18521 15:51:07,080 --> 15:51:13,880 rounds down correctly to $20 18522 15:51:11,360 --> 15:51:16,880 and0 however I'll leave creating this 18523 15:51:13,880 --> 15:51:19,560 test case as an 18524 15:51:16,880 --> 15:51:22,276 exercise now let's move on and talk 18525 15:51:19,560 --> 15:51:25,520 about the second disadvantage of manual 18526 15:51:22,276 --> 15:51:28,680 tests which is it's hard to retest the 18527 15:51:25,520 --> 15:51:31,560 code automated test also solve this 18528 15:51:28,680 --> 15:51:34,636 problem for example let's say that we 18529 15:51:31,560 --> 15:51:38,956 Chang the format currency function so 18530 15:51:34,636 --> 15:51:38,956 inside UIL money.jpg 18531 15:51:56,756 --> 15:52:01,520 we would have to open up the website set 18532 15:51:59,276 --> 15:52:03,756 up everything manually and then click 18533 15:52:01,520 --> 15:52:06,956 around to see if it's 18534 15:52:03,756 --> 15:52:09,880 working however with automated testing 18535 15:52:06,956 --> 15:52:12,360 all we need to do is to rerun all of 18536 15:52:09,880 --> 15:52:15,720 this test code and it will instantly 18537 15:52:12,360 --> 15:52:19,796 retest all of these cases so let's go 18538 15:52:15,720 --> 15:52:23,360 back to money. JS and save this 18539 15:52:19,796 --> 15:52:27,796 code and then go into our test 18540 15:52:23,360 --> 15:52:30,040 page and check our console right click 18541 15:52:27,796 --> 15:52:32,080 inspect the 18542 15:52:30,040 --> 15:52:35,000 console and you can see that it 18543 15:52:32,080 --> 15:52:37,116 instantly reruns all the test cases and 18544 15:52:35,000 --> 15:52:39,956 it tells us that the last test case 18545 15:52:37,116 --> 15:52:42,956 failed so automated tests make it really 18546 15:52:39,956 --> 15:52:44,796 easy to retest our code after we make 18547 15:52:42,956 --> 15:52:48,240 any 18548 15:52:44,796 --> 15:52:50,680 changes okay now we have another problem 18549 15:52:48,240 --> 15:52:53,320 here we can see that the test Cas is 18550 15:52:50,680 --> 15:52:55,756 passed passed and failed but it's not 18551 15:52:53,320 --> 15:52:58,756 clear which ones passed and which ones 18552 15:52:55,756 --> 15:53:00,636 failed we don't really have a name for 18553 15:52:58,756 --> 15:53:02,560 these tests so we don't know what 18554 15:53:00,636 --> 15:53:04,840 they're doing so that's what we're going 18555 15:53:02,560 --> 15:53:07,756 to add next we're going to give each of 18556 15:53:04,840 --> 15:53:08,880 these tests a name so let's go back to 18557 15:53:07,756 --> 15:53:12,360 our 18558 15:53:08,880 --> 15:53:14,436 code and open money 18559 15:53:12,360 --> 15:53:17,796 test.js and now we're going to give each 18560 15:53:14,436 --> 15:53:19,160 of these test cases a name so above the 18561 15:53:17,796 --> 15:53:21,480 first test 18562 15:53:19,160 --> 15:53:23,360 case let's just 18563 15:53:21,480 --> 15:53:26,720 console. 18564 15:53:23,360 --> 15:53:29,360 log a string and we're going to give 18565 15:53:26,720 --> 15:53:32,680 this test a name so the name should 18566 15:53:29,360 --> 15:53:36,720 describe what this test is doing so in 18567 15:53:32,680 --> 15:53:39,596 here let's just use the name converts 18568 15:53:36,720 --> 15:53:41,840 cents into 18569 15:53:39,596 --> 15:53:44,956 dollars and that's how we give a test 18570 15:53:41,840 --> 15:53:48,840 case a name let's do the same thing with 18571 15:53:44,956 --> 15:53:52,596 the other two test cases so down here 18572 15:53:48,840 --> 15:53:56,756 we're going to console. 18573 15:53:52,596 --> 15:53:59,636 log brackets and a string and we give 18574 15:53:56,756 --> 15:54:02,560 the second test case a name so this one 18575 15:53:59,636 --> 15:54:08,116 tests the number zero so let's use the 18576 15:54:02,560 --> 15:54:11,720 name works with zero so again the name 18577 15:54:08,116 --> 15:54:13,956 just describes what the test is 18578 15:54:11,720 --> 15:54:17,320 doing and let's do the same thing for 18579 15:54:13,956 --> 15:54:21,080 the third test case we 18580 15:54:17,320 --> 15:54:23,756 console.log a string and here we're 18581 15:54:21,080 --> 15:54:27,560 testing if the function rounds up to the 18582 15:54:23,756 --> 15:54:32,360 nearest scent so let's use the name 18583 15:54:27,560 --> 15:54:34,956 rounds up to the nearest 18584 15:54:32,360 --> 15:54:37,400 scent and 18585 15:54:34,956 --> 15:54:39,560 save if you're wondering how I came up 18586 15:54:37,400 --> 15:54:42,520 with these names a naming convention 18587 15:54:39,560 --> 15:54:46,200 that we use is to describe what the code 18588 15:54:42,520 --> 15:54:49,240 is doing for example the code works with 18589 15:54:46,200 --> 15:54:52,520 zero or the code rounds up to the 18590 15:54:49,240 --> 15:54:54,916 nearest scent and then we remove the 18591 15:54:52,520 --> 15:54:58,880 code some people also like to use the 18592 15:54:54,916 --> 15:54:58,880 word it instead of the 18593 15:54:59,360 --> 15:55:05,916 code so now each of these test cases has 18594 15:55:02,400 --> 15:55:08,880 a name above it if we open up our test 18595 15:55:05,916 --> 15:55:11,840 page and open the 18596 15:55:08,880 --> 15:55:14,756 console now we can see exactly which 18597 15:55:11,840 --> 15:55:17,480 test case passed and which test case 18598 15:55:14,756 --> 15:55:20,520 failed so this failed test case tells us 18599 15:55:17,480 --> 15:55:23,360 we have a rounding problem in our code 18600 15:55:20,520 --> 15:55:26,756 so now we can go back to our code into 18601 15:55:23,360 --> 15:55:29,200 money. JS and investigate the rounding 18602 15:55:26,756 --> 15:55:31,840 problem so of course we removed 18603 15:55:29,200 --> 15:55:34,880 math.round as an example we're just 18604 15:55:31,840 --> 15:55:38,680 going to add it back to fix that failing 18605 15:55:34,880 --> 15:55:42,880 test capital m 18606 15:55:38,680 --> 15:55:45,240 math.round Open Bracket and close 18607 15:55:42,880 --> 15:55:48,240 bracket and 18608 15:55:45,240 --> 15:55:51,840 save and then open the console for our 18609 15:55:48,240 --> 15:55:54,636 test which is here and now all of our 18610 15:55:51,840 --> 15:55:57,720 test cases 18611 15:55:54,636 --> 15:56:00,560 passed next another thing we do when 18612 15:55:57,720 --> 15:56:04,436 naming our tests is to group related 18613 15:56:00,560 --> 15:56:07,200 tests together for example these three 18614 15:56:04,436 --> 15:56:09,636 test cases are for the format currency 18615 15:56:07,200 --> 15:56:12,636 function so we're going to group these 18616 15:56:09,636 --> 15:56:16,436 tests together and give this group a 18617 15:56:12,636 --> 15:56:19,680 name by the way a group of related tests 18618 15:56:16,436 --> 15:56:19,680 is called a test 18619 15:56:20,160 --> 15:56:26,320 Suite so let's go back to our code and 18620 15:56:23,160 --> 15:56:28,680 open Money test.js 18621 15:56:26,320 --> 15:56:32,320 and at the top let's give this group of 18622 15:56:28,680 --> 15:56:34,796 tests or this test Suite A Name by 18623 15:56:32,320 --> 15:56:38,596 typing 18624 15:56:34,796 --> 15:56:45,360 console.log brackets and a string we're 18625 15:56:38,596 --> 15:56:47,916 going to name it test Suite colon format 18626 15:56:45,360 --> 15:56:50,596 currency and 18627 15:56:47,916 --> 15:56:52,560 save so now let's open the console for 18628 15:56:50,596 --> 15:56:55,720 our 18629 15:56:52,560 --> 15:56:58,840 tests and now at the top it will show us 18630 15:56:55,720 --> 15:57:01,916 test site format currency so this makes 18631 15:56:58,840 --> 15:57:04,116 it clear that this group of tests here 18632 15:57:01,916 --> 15:57:06,880 are for the format currency 18633 15:57:04,116 --> 15:57:09,720 function in the future if we are testing 18634 15:57:06,880 --> 15:57:12,560 some other code for example a calculated 18635 15:57:09,720 --> 15:57:16,560 tax function we can create another group 18636 15:57:12,560 --> 15:57:18,480 of tests or another test Suite like this 18637 15:57:16,560 --> 15:57:21,756 and this makes it clear that the second 18638 15:57:18,480 --> 15:57:24,360 group of tests is for the calculate tax 18639 15:57:21,756 --> 15:57:26,200 function so this is how we name our 18640 15:57:24,360 --> 15:57:28,436 tests and group our tests together 18641 15:57:26,200 --> 15:57:31,276 together to make it more clear what is 18642 15:57:28,436 --> 15:57:33,160 being tested and which tests have passed 18643 15:57:31,276 --> 15:57:36,160 or 18644 15:57:33,160 --> 15:57:39,636 failed and now we created our first set 18645 15:57:36,160 --> 15:57:42,560 of automated tests as you can see 18646 15:57:39,636 --> 15:57:45,116 automated tests are basically using code 18647 15:57:42,560 --> 15:57:47,956 to test code and they make it really 18648 15:57:45,116 --> 15:57:50,636 easy to check if our code is working 18649 15:57:47,956 --> 15:57:54,040 correctly so now let's save this into 18650 15:57:50,636 --> 15:57:57,200 git we're going to open the git 18651 15:57:54,040 --> 15:58:00,680 section and our first change in money 18652 15:57:57,200 --> 15:58:03,756 test.js we created the tests for format 18653 15:58:00,680 --> 15:58:06,320 currency and then we created an HTML 18654 15:58:03,756 --> 15:58:08,680 file to load our test 18655 15:58:06,320 --> 15:58:10,040 code so let's create a message 18656 15:58:08,680 --> 15:58:15,916 describing these 18657 15:58:10,040 --> 15:58:17,520 changes create automated tests for 18658 15:58:15,916 --> 15:58:20,956 format 18659 15:58:17,520 --> 15:58:20,956 currency and 18660 15:58:24,080 --> 15:58:29,000 commit next we're going to learn how to 18661 15:58:26,596 --> 15:58:32,520 use a testing framework to write 18662 15:58:29,000 --> 15:58:34,956 automated tests so what is a testing 18663 15:58:32,520 --> 15:58:37,636 framework in the previous lesson we 18664 15:58:34,956 --> 15:58:40,560 learned about external libraries which 18665 15:58:37,636 --> 15:58:43,276 is code that is outside of our project 18666 15:58:40,560 --> 15:58:46,000 that someone else wrote and we can load 18667 15:58:43,276 --> 15:58:47,480 external libraries into our project and 18668 15:58:46,000 --> 15:58:50,520 use 18669 15:58:47,480 --> 15:58:53,040 them so a testing framework is an 18670 15:58:50,520 --> 15:58:54,240 external library that helps us write 18671 15:58:53,040 --> 15:58:57,680 tests 18672 15:58:54,240 --> 15:59:00,756 easier let's go back to our code and 18673 15:58:57,680 --> 15:59:03,796 we're going to open the money test.js 18674 15:59:00,756 --> 15:59:06,436 file and notice that in our tests we 18675 15:59:03,796 --> 15:59:08,756 need to do several things first we 18676 15:59:06,436 --> 15:59:11,680 create a test suite and we give it a 18677 15:59:08,756 --> 15:59:14,880 name and then we create some tests and 18678 15:59:11,680 --> 15:59:17,200 for each test we give it a name and then 18679 15:59:14,880 --> 15:59:20,116 we compare the values and display the 18680 15:59:17,200 --> 15:59:22,436 result in the console so a testing 18681 15:59:20,116 --> 15:59:25,000 framework helps us do all of these 18682 15:59:22,436 --> 15:59:27,596 things automatically and gives us many 18683 15:59:25,000 --> 15:59:29,240 other features so now let's learn our 18684 15:59:27,596 --> 15:59:31,480 first testing 18685 15:59:29,240 --> 15:59:34,276 framework in this course we're going to 18686 15:59:31,480 --> 15:59:36,200 use a popular testing framework called 18687 15:59:34,276 --> 15:59:38,160 Jasmine now if you're working at a 18688 15:59:36,200 --> 15:59:40,520 company in the future you'll definitely 18689 15:59:38,160 --> 15:59:43,240 be using a testing framework but it 18690 15:59:40,520 --> 15:59:45,720 might not always be Jasmine and that's 18691 15:59:43,240 --> 15:59:48,560 okay because most testing Frameworks are 18692 15:59:45,720 --> 15:59:50,520 pretty similar so by learning Jasmine 18693 15:59:48,560 --> 15:59:53,796 you'll be able to easily use other 18694 15:59:50,520 --> 15:59:53,796 testing Frameworks in the 18695 15:59:53,840 --> 15:59:58,916 future to load Jasmine into our project 18696 15:59:57,240 --> 16:00:00,916 we're going to go into the video 18697 15:59:58,916 --> 16:00:04,636 description and we're going to click 18698 16:00:00,916 --> 16:00:04,636 this link to download 18699 16:00:13,116 --> 16:00:18,200 Jasmine so once that link opens we're 18700 16:00:16,200 --> 16:00:20,916 going to be on this page and we're going 18701 16:00:18,200 --> 16:00:23,916 to scroll down and we're going to click 18702 16:00:20,916 --> 16:00:27,320 this Jasmine DST standalone. zip file to 18703 16:00:23,916 --> 16:00:29,040 download it 18704 16:00:27,320 --> 16:00:32,560 now once that's finished downloading 18705 16:00:29,040 --> 16:00:32,560 we're going to open our downloads 18706 16:00:34,160 --> 16:00:40,360 folder and if you're on Windows we're 18707 16:00:36,796 --> 16:00:41,916 going to rightclick the zip file and 18708 16:00:40,360 --> 16:00:45,160 click extract 18709 16:00:41,916 --> 16:00:47,116 all and click 18710 16:00:45,160 --> 16:00:49,636 extract now if you don't have that 18711 16:00:47,116 --> 16:00:52,436 extract all option you can also create a 18712 16:00:49,636 --> 16:00:54,680 folder with this name and then double 18713 16:00:52,436 --> 16:00:57,040 click the zip file and drag everything 18714 16:00:54,680 --> 16:00:58,840 into that folder 18715 16:00:57,040 --> 16:01:01,956 if you're using a Mac you can just 18716 16:00:58,840 --> 16:01:01,956 doubleclick the zip 18717 16:01:03,240 --> 16:01:08,080 file so this folder contains the code 18718 16:01:05,880 --> 16:01:11,400 for Jasmine and we're going to drag this 18719 16:01:08,080 --> 16:01:13,596 folder into our project so let's open 18720 16:01:11,400 --> 16:01:15,720 the code for our 18721 16:01:13,596 --> 16:01:19,720 project and then we're just going to 18722 16:01:15,720 --> 16:01:19,720 drag this folder down 18723 16:01:19,756 --> 16:01:23,720 here and click copy 18724 16:01:23,756 --> 16:01:29,080 folder so just make sure that this fold 18725 16:01:25,880 --> 16:01:31,596 folder is beside the scripts and images 18726 16:01:29,080 --> 16:01:35,436 folder next we're going to rename this 18727 16:01:31,596 --> 16:01:38,720 folder so we'll right click 18728 16:01:35,436 --> 16:01:41,880 rename and let's rename it to 18729 16:01:38,720 --> 16:01:44,360 test- Jasmine to make it clear this 18730 16:01:41,880 --> 16:01:48,160 folder contains test created with 18731 16:01:44,360 --> 16:01:48,160 Jasmine so now let's press 18732 16:01:48,520 --> 16:01:55,480 enter and now we have Jasmine inside our 18733 16:01:51,916 --> 16:01:58,040 project so inside the lip folder we have 18734 16:01:55,480 --> 16:02:00,080 some JavaScript files here and this is 18735 16:01:58,040 --> 16:02:02,840 the code for the Jasmine testing 18736 16:02:00,080 --> 16:02:05,840 framework so this is another way to load 18737 16:02:02,840 --> 16:02:07,796 an external Library into our project we 18738 16:02:05,840 --> 16:02:10,360 can just download all the code and put 18739 16:02:07,796 --> 16:02:12,400 it into our project and this is useful 18740 16:02:10,360 --> 16:02:14,560 if an external library has lots of 18741 16:02:12,400 --> 16:02:17,560 different files like 18742 16:02:14,560 --> 16:02:20,040 this before we continue let's save the 18743 16:02:17,560 --> 16:02:22,240 changes into git so if we make any 18744 16:02:20,040 --> 16:02:25,880 changes later it will be easier to see 18745 16:02:22,240 --> 16:02:28,040 them we'll open the git section 18746 16:02:25,880 --> 16:02:31,400 and then we just added all the code for 18747 16:02:28,040 --> 16:02:36,680 Jasmine so at the top let's type the 18748 16:02:31,400 --> 16:02:40,000 message add Jasmin testing 18749 16:02:36,680 --> 16:02:40,000 framework and 18750 16:02:42,636 --> 16:02:48,360 commit now that we loaded Jasmine into 18751 16:02:45,200 --> 16:02:50,400 our project let's learn how to use it so 18752 16:02:48,360 --> 16:02:52,480 Jasmine has a documentation page that 18753 16:02:50,400 --> 16:02:55,080 shows you how to use the testing 18754 16:02:52,480 --> 16:02:56,480 framework however to save time I'm just 18755 16:02:55,080 --> 16:02:58,680 going to show you the features that we 18756 16:02:56,480 --> 16:03:01,680 need and you can read the documentation 18757 16:02:58,680 --> 16:03:03,916 later if you want so first let's learn 18758 16:03:01,680 --> 16:03:07,160 how to run tests using 18759 16:03:03,916 --> 16:03:09,956 Jasmine I'm going to close this for now 18760 16:03:07,160 --> 16:03:13,320 and then in our code we're going to open 18761 16:03:09,956 --> 16:03:15,840 the test- Jasmin folder and at the 18762 16:03:13,320 --> 16:03:17,720 bottom we're going to open this spec 18763 16:03:15,840 --> 16:03:21,116 runner. HTML 18764 16:03:17,720 --> 16:03:23,956 file so this file loads all of the 18765 16:03:21,116 --> 16:03:27,880 tests so this does the same thing as our 18766 16:03:23,956 --> 16:03:31,916 test.html file from before and by the 18767 16:03:27,880 --> 16:03:35,116 way in Jasmine spec is another name for 18768 16:03:31,916 --> 16:03:38,080 test so spec Runner just means test 18769 16:03:35,116 --> 16:03:41,276 Runner this file runs all the 18770 16:03:38,080 --> 16:03:44,916 tests so let's actually rename this file 18771 16:03:41,276 --> 16:03:48,000 to test.html so we can match what we had 18772 16:03:44,916 --> 16:03:51,160 before and compare the two so here we're 18773 16:03:48,000 --> 16:03:53,040 going to right click and 18774 16:03:51,160 --> 16:03:57,000 rename 18775 16:03:53,040 --> 16:03:59,840 tests. HTML and press 18776 16:03:57,000 --> 16:04:01,636 enter and now let's open this file and 18777 16:03:59,840 --> 16:04:05,680 take a look at the code that's 18778 16:04:01,636 --> 16:04:08,040 inside so here at the top this is the 18779 16:04:05,680 --> 16:04:11,436 code for the Jasmine testing framework 18780 16:04:08,040 --> 16:04:15,480 so we're just loading the framework up 18781 16:04:11,436 --> 16:04:18,680 here below that here we have the code 18782 16:04:15,480 --> 16:04:21,040 that we want to test so this code is 18783 16:04:18,680 --> 16:04:22,116 sort of like the money JS file that we 18784 16:04:21,040 --> 16:04:25,116 were testing 18785 16:04:22,116 --> 16:04:28,276 earlier and then at the bottom these 18786 16:04:25,116 --> 16:04:30,880 files contain the tests so these files 18787 16:04:28,276 --> 16:04:33,200 are sort of like the money test.js file 18788 16:04:30,880 --> 16:04:35,520 that we had earlier and you'll notice 18789 16:04:33,200 --> 16:04:39,080 that these are in a spec folder so as a 18790 16:04:35,520 --> 16:04:41,880 reminder in Jasmine spec is another name 18791 16:04:39,080 --> 16:04:46,240 for test so this is basically the test 18792 16:04:41,880 --> 16:04:49,680 folder this folder contains all of our 18793 16:04:46,240 --> 16:04:51,720 tests so now let's run these tests and 18794 16:04:49,680 --> 16:04:54,956 to do that we just need to open this 18795 16:04:51,720 --> 16:04:56,436 HTML file in live server just like we 18796 16:04:54,956 --> 16:04:59,480 did before 18797 16:04:56,436 --> 16:05:03,636 so for this file we're going to right 18798 16:04:59,480 --> 16:05:03,636 click and open with live 18799 16:05:05,360 --> 16:05:10,680 server and I'm just going to make this a 18800 16:05:07,480 --> 16:05:12,436 little bigger by pressing control equals 18801 16:05:10,680 --> 16:05:15,276 so it's easier to see in the 18802 16:05:12,436 --> 16:05:19,080 video so this is what Jasmine tests look 18803 16:05:15,276 --> 16:05:21,276 like up here we have five Green Dots 18804 16:05:19,080 --> 16:05:23,560 this means we have five tests that we 18805 16:05:21,276 --> 16:05:27,276 ran and the green color means that all 18806 16:05:23,560 --> 16:05:29,636 of them passed so that here each test 18807 16:05:27,276 --> 16:05:32,636 has a Green Dot and you'll notice that 18808 16:05:29,636 --> 16:05:35,116 each test also has a name next to it 18809 16:05:32,636 --> 16:05:36,956 which tells us what the test does and 18810 16:05:35,116 --> 16:05:39,520 don't worry if this is in a different 18811 16:05:36,956 --> 16:05:40,796 order for you Jasmine randomizes the 18812 16:05:39,520 --> 16:05:43,276 order of the 18813 16:05:40,796 --> 16:05:46,560 tests also notice that some of these 18814 16:05:43,276 --> 16:05:49,360 tests are grouped together into a test 18815 16:05:46,560 --> 16:05:52,636 suite and up here we have the name of 18816 16:05:49,360 --> 16:05:55,040 the test Suite so as you can see a 18817 16:05:52,636 --> 16:05:57,720 testing framework helps us do all of the 18818 16:05:55,040 --> 16:05:59,756 things we were doing before and it also 18819 16:05:57,720 --> 16:06:02,636 provides a nice looking website where we 18820 16:05:59,756 --> 16:06:05,436 can see the test results so a testing 18821 16:06:02,636 --> 16:06:09,116 framework is just an external library 18822 16:06:05,436 --> 16:06:09,116 that helps us write tests 18823 16:06:11,320 --> 16:06:16,636 easier now that we know what Jasmine and 18824 16:06:14,000 --> 16:06:19,200 a testing framework is let's learn how 18825 16:06:16,636 --> 16:06:21,400 to write our tests using 18826 16:06:19,200 --> 16:06:23,200 Jasmine first we're going to go back to 18827 16:06:21,400 --> 16:06:25,880 our 18828 16:06:23,200 --> 16:06:28,240 code and we're going to remove the 18829 16:06:25,880 --> 16:06:31,400 example code provided by 18830 16:06:28,240 --> 16:06:34,080 Jasmine so down here we'll remove these 18831 16:06:31,400 --> 16:06:34,080 four script 18832 16:06:34,360 --> 16:06:41,520 texts and let's also remove the source 18833 16:06:37,400 --> 16:06:41,520 folder so we'll right click 18834 16:06:41,636 --> 16:06:47,200 delete as well as the spec folder right 18835 16:06:44,636 --> 16:06:49,916 click 18836 16:06:47,200 --> 16:06:53,000 delete but make sure we keep this lib 18837 16:06:49,916 --> 16:06:56,756 folder because this contains a code for 18838 16:06:53,000 --> 16:06:59,756 Jasmine and now inside test Jasmin we're 18839 16:06:56,756 --> 16:07:01,436 going to create a new file for our test 18840 16:06:59,756 --> 16:07:04,000 so we'll create a new 18841 16:07:01,436 --> 16:07:06,756 file and let's say that we're testing 18842 16:07:04,000 --> 16:07:11,320 money. JS again so let's name it the 18843 16:07:06,756 --> 16:07:14,956 same thing here money 18844 16:07:11,320 --> 16:07:18,276 test.js and press 18845 16:07:14,956 --> 16:07:21,956 enter and now let's create the same test 18846 16:07:18,276 --> 16:07:24,200 we had before by using Jasmine this time 18847 16:07:21,956 --> 16:07:26,720 so to make it easier for the video I'm 18848 16:07:24,200 --> 16:07:29,636 just going to take this file and drag it 18849 16:07:26,720 --> 16:07:32,160 to the top here so we can see these two 18850 16:07:29,636 --> 16:07:32,160 side by 18851 16:07:34,000 --> 16:07:39,840 side so like before we're going to 18852 16:07:36,320 --> 16:07:41,720 create tests for format currency first 18853 16:07:39,840 --> 16:07:45,276 we'll need to import this function at 18854 16:07:41,720 --> 16:07:48,480 the top so let's just actually select 18855 16:07:45,276 --> 16:07:53,916 this and right click 18856 16:07:48,480 --> 16:07:56,276 copy and then down here right click and 18857 16:07:53,916 --> 16:07:59,436 paste and just make sure that this file 18858 16:07:56,276 --> 16:08:03,436 path still works so our money test.js 18859 16:07:59,436 --> 16:08:05,040 file has to be inside test- Jasmin and 18860 16:08:03,436 --> 16:08:07,756 this folder should be beside the 18861 16:08:05,040 --> 16:08:07,756 previous test 18862 16:08:10,000 --> 16:08:16,116 folder next we're going to create a test 18863 16:08:12,840 --> 16:08:18,636 suite and name the test Suite so in 18864 16:08:16,116 --> 16:08:22,596 Jasmine to create a test Suite we're 18865 16:08:18,636 --> 16:08:22,596 going to use a function called 18866 16:08:22,756 --> 16:08:28,040 describe so this describe function is 18867 16:08:25,520 --> 16:08:30,116 provided by Jasmine and it creates a 18868 16:08:28,040 --> 16:08:33,080 test 18869 16:08:30,116 --> 16:08:35,480 Suite next we're going to give a name to 18870 16:08:33,080 --> 16:08:37,720 the test Suite so to name a test Suite 18871 16:08:35,480 --> 16:08:40,680 in Jasmine we're going to give describe 18872 16:08:37,720 --> 16:08:43,000 a string and this string is the name of 18873 16:08:40,680 --> 16:08:52,116 the test Suite so let's name it the same 18874 16:08:43,000 --> 16:08:52,116 thing as before test Suite colon format 18875 16:08:52,916 --> 16:08:59,400 currency next we're going to add some 18876 16:08:55,596 --> 16:09:01,436 tests inside the test Suite to do that 18877 16:08:59,400 --> 16:09:05,636 we're going to give describe a second 18878 16:09:01,436 --> 16:09:09,080 parameter which is a function brackets 18879 16:09:05,636 --> 16:09:09,080 arrow and curly 18880 16:09:09,956 --> 16:09:16,116 brackets and inside here we're going to 18881 16:09:12,840 --> 16:09:18,400 create a test and give the test a name 18882 16:09:16,116 --> 16:09:23,040 to create a test in Jasmine we're going 18883 16:09:18,400 --> 16:09:26,200 to use another function called it and 18884 16:09:23,040 --> 16:09:30,480 brackets so it is a another function 18885 16:09:26,200 --> 16:09:34,000 provided by Jasmine and this creates a 18886 16:09:30,480 --> 16:09:36,880 test next let's give a name to our test 18887 16:09:34,000 --> 16:09:40,116 so that we know what it's doing to name 18888 16:09:36,880 --> 16:09:42,596 a test we're going to give it a string 18889 16:09:40,116 --> 16:09:44,720 and this is going to be the test name so 18890 16:09:42,596 --> 16:09:46,240 let's name it the same thing we had 18891 16:09:44,720 --> 16:09:51,520 before 18892 16:09:46,240 --> 16:09:51,520 converts sense into 18893 16:09:51,840 --> 16:09:57,116 dollars and now we're going to add some 18894 16:09:54,080 --> 16:09:59,956 code into our tests to do that in 18895 16:09:57,116 --> 16:10:03,000 Jasmine we're going to give it a second 18896 16:09:59,956 --> 16:10:06,360 parameter which is another function 18897 16:10:03,000 --> 16:10:09,796 brackets arrow and curly 18898 16:10:06,360 --> 16:10:12,956 brackets so this is the code inside the 18899 16:10:09,796 --> 16:10:16,240 test and in this test we need to compare 18900 16:10:12,956 --> 16:10:18,680 these two values and display the result 18901 16:10:16,240 --> 16:10:20,840 in Jasmine instead of creating an if 18902 16:10:18,680 --> 16:10:23,360 statement and displaying the result 18903 16:10:20,840 --> 16:10:26,116 ourselves Jasmine provides us another 18904 16:10:23,360 --> 16:10:31,276 function to do all of this 18905 16:10:26,116 --> 16:10:34,520 and that function is called expect and 18906 16:10:31,276 --> 16:10:38,636 brackets so expect lets us compare a 18907 16:10:34,520 --> 16:10:40,880 value to another value for example let's 18908 16:10:38,636 --> 16:10:44,636 give expect the first value that we want 18909 16:10:40,880 --> 16:10:48,480 to compare which is format currency 18910 16:10:44,636 --> 16:10:52,276 2095 so we'll type format 18911 16:10:48,480 --> 16:10:55,400 currency brackets 18912 16:10:52,276 --> 16:10:59,276 295 and now to compare this value to 18913 16:10:55,400 --> 16:11:02,596 another value expect gives us an 18914 16:10:59,276 --> 16:11:04,436 object and this object has many methods 18915 16:11:02,596 --> 16:11:06,756 we can use to do 18916 16:11:04,436 --> 16:11:13,360 comparisons so one of the methods we can 18917 16:11:06,756 --> 16:11:16,240 use in this object is dot 2 equal 18918 16:11:13,360 --> 16:11:18,360 brackets and then inside these brackets 18919 16:11:16,240 --> 16:11:21,080 we'll put another value that we want to 18920 16:11:18,360 --> 16:11:25,880 compare this to so we want to compare 18921 16:11:21,080 --> 16:11:25,880 this to the string 2095 18922 16:11:26,116 --> 16:11:32,596 and that's it this checks if this value 18923 16:11:29,436 --> 16:11:35,360 is equal to this value and then displays 18924 16:11:32,596 --> 16:11:37,596 the result on the page so it does 18925 16:11:35,360 --> 16:11:40,360 everything that we did before except 18926 16:11:37,596 --> 16:11:43,160 with one line of code and you'll also 18927 16:11:40,360 --> 16:11:46,240 notice that jasmin's code is designed so 18928 16:11:43,160 --> 16:11:48,720 that it reads like English for example 18929 16:11:46,240 --> 16:11:52,880 we expect format currency 18930 16:11:48,720 --> 16:11:56,000 2095 to equal this string and this makes 18931 16:11:52,880 --> 16:11:58,796 a test code easier to read 18932 16:11:56,000 --> 16:12:01,756 so we just created our first test suite 18933 16:11:58,796 --> 16:12:05,116 and our first test using 18934 16:12:01,756 --> 16:12:07,480 Jasmine now let's run this test to do 18935 16:12:05,116 --> 16:12:10,480 that we just need to run this Javascript 18936 16:12:07,480 --> 16:12:13,400 file inside test.html 18937 16:12:10,480 --> 16:12:16,720 so first let's save this 18938 16:12:13,400 --> 16:12:20,796 file and then inside test.html we're 18939 16:12:16,720 --> 16:12:23,240 going to load this file so in here at 18940 16:12:20,796 --> 16:12:26,636 the bottom we're going to create a 18941 16:12:23,240 --> 16:12:26,636 script tag 18942 16:12:26,796 --> 16:12:31,636 and we'll give it a source attribute SRC 18943 16:12:29,880 --> 16:12:35,680 equals double 18944 16:12:31,636 --> 16:12:38,680 quotes and we're going to load money 18945 16:12:35,680 --> 16:12:38,680 test.js 18946 16:12:39,080 --> 16:12:45,480 test.js and remember our test file uses 18947 16:12:42,200 --> 16:12:49,680 import so we also need to give it the 18948 16:12:45,480 --> 16:12:52,320 attribute type equals module so this 18949 16:12:49,680 --> 16:12:56,320 file has access to JavaScript module 18950 16:12:52,320 --> 16:12:59,276 features so now let's save 18951 16:12:56,320 --> 16:13:01,956 and we'll open this file in our browser 18952 16:12:59,276 --> 16:13:04,400 so we should have it open from before if 18953 16:13:01,956 --> 16:13:06,200 you don't you can write click and open 18954 16:13:04,400 --> 16:13:09,680 with live 18955 16:13:06,200 --> 16:13:11,756 server so we'll open the test page and 18956 16:13:09,680 --> 16:13:13,916 now you can see that we have our one 18957 16:13:11,756 --> 16:13:16,796 test that we created here and it's 18958 16:13:13,916 --> 16:13:20,040 passing so it's green you can see here 18959 16:13:16,796 --> 16:13:22,880 that we created a test Suite with a name 18960 16:13:20,040 --> 16:13:26,240 and we also created a test with this 18961 16:13:22,880 --> 16:13:28,956 name so we just created the first test 18962 16:13:26,240 --> 16:13:31,160 that we had before except using Jasmine 18963 16:13:28,956 --> 16:13:33,520 this time and you can see that Jasmine 18964 16:13:31,160 --> 16:13:36,000 also creates a nice website for us to 18965 16:13:33,520 --> 16:13:38,636 see the test 18966 16:13:36,000 --> 16:13:41,436 results okay now that we learned how to 18967 16:13:38,636 --> 16:13:43,560 use Jasmine to create a test let's get 18968 16:13:41,436 --> 16:13:47,240 some practice and create the rest of our 18969 16:13:43,560 --> 16:13:51,400 tests from before we'll go back to our 18970 16:13:47,240 --> 16:13:51,400 code and open money 18971 16:13:51,436 --> 16:13:57,796 test.js and here we're going to create 18972 16:13:54,436 --> 16:14:01,400 the other two tests that we had here so 18973 16:13:57,796 --> 16:14:03,636 at the bottom inside the same test Suite 18974 16:14:01,400 --> 16:14:07,520 we're going to create another test using 18975 16:14:03,636 --> 16:14:11,000 the function it brackets and we'll give 18976 16:14:07,520 --> 16:14:12,756 it a string which is the test name and 18977 16:14:11,000 --> 16:14:18,720 let's name it the same as 18978 16:14:12,756 --> 16:14:20,636 before works with zero comma brackets 18979 16:14:18,720 --> 16:14:22,840 arrow and curly 18980 16:14:20,636 --> 16:14:25,916 brackets and now we can put something 18981 16:14:22,840 --> 16:14:28,560 inside this test so in the second test 18982 16:14:25,916 --> 16:14:30,636 we're going to compare these two values 18983 16:14:28,560 --> 16:14:34,040 to compare values we're going to use the 18984 16:14:30,636 --> 16:14:36,916 function expect 18985 16:14:34,040 --> 16:14:39,956 brackets and the first value is going to 18986 16:14:36,916 --> 16:14:39,956 be format 18987 16:14:40,320 --> 16:14:46,636 currency brackets 18988 16:14:42,956 --> 16:14:49,596 zero and then expect gives us an 18989 16:14:46,636 --> 16:14:53,320 object and this object has a method 18990 16:14:49,596 --> 16:14:55,916 called dot to equal which lets us 18991 16:14:53,320 --> 16:14:58,756 compare this value to another value 18992 16:14:55,916 --> 16:15:03,796 so we'll call this method using brackets 18993 16:14:58,756 --> 16:15:06,400 and compare to the string the string 0 0 18994 16:15:03,796 --> 16:15:09,160 0 and now let's 18995 16:15:06,400 --> 16:15:11,436 save and live server will update our web 18996 16:15:09,160 --> 16:15:14,560 page automatically so we'll open the 18997 16:15:11,436 --> 16:15:17,116 test page and now we have two tests and 18998 16:15:14,560 --> 16:15:20,400 they both passed so that's how we create 18999 16:15:17,116 --> 16:15:22,040 the second test using Jasmine and again 19000 16:15:20,400 --> 16:15:24,400 don't worry if this is in a different 19001 16:15:22,040 --> 16:15:25,720 order for you Jasmine randomizes the 19002 16:15:24,400 --> 16:15:28,360 order of the 19003 16:15:25,720 --> 16:15:30,880 tests finally let's create that third 19004 16:15:28,360 --> 16:15:34,480 test that we had before we'll go back to 19005 16:15:30,880 --> 16:15:36,756 our code and then in our old money test 19006 16:15:34,480 --> 16:15:39,240 file we're going to create this third 19007 16:15:36,756 --> 16:15:40,880 test feel free to pause the video if you 19008 16:15:39,240 --> 16:15:44,276 want to give it a try 19009 16:15:40,880 --> 16:15:46,436 yourself so we'll scroll down and we'll 19010 16:15:44,276 --> 16:15:50,796 create another test inside this test 19011 16:15:46,436 --> 16:15:53,720 Suite using the function it brackets and 19012 16:15:50,796 --> 16:15:55,840 a string and we'll give it the same test 19013 16:15:53,720 --> 16:15:59,276 name as before 19014 16:15:55,840 --> 16:16:04,636 rounds up to the 19015 16:15:59,276 --> 16:16:05,796 nearest set comma brackets arrow and 19016 16:16:04,636 --> 16:16:07,956 curly 19017 16:16:05,796 --> 16:16:13,200 brackets and we're going to compare 19018 16:16:07,956 --> 16:16:16,560 these two values so we use exp brackets 19019 16:16:13,200 --> 16:16:19,756 and give it the first value format 19020 16:16:16,560 --> 16:16:22,360 currency brackets 19021 16:16:19,756 --> 16:16:23,840 2000.5 and to compare it we'll use the 19022 16:16:22,360 --> 16:16:31,240 method 19023 16:16:23,840 --> 16:16:33,840 do2 equal brackets and compare it to 19024 16:16:31,240 --> 16:16:37,240 20.01 and 19025 16:16:33,840 --> 16:16:39,596 save and let's go back to our test page 19026 16:16:37,240 --> 16:16:43,000 and now we have three tests and all 19027 16:16:39,596 --> 16:16:45,596 three of them passed so we recreated the 19028 16:16:43,000 --> 16:16:48,116 tests that we had 19029 16:16:45,596 --> 16:16:50,956 before now let's try making one of these 19030 16:16:48,116 --> 16:16:53,680 tests fail to see what it looks like in 19031 16:16:50,956 --> 16:16:59,436 Jasmine we'll open our 19032 16:16:53,680 --> 16:16:59,436 code and and let's open the file utils 19033 16:17:12,480 --> 16:17:15,480 money.jpg 19034 16:17:21,276 --> 16:17:26,436 test so it tells us the test Suite name 19035 16:17:24,276 --> 16:17:28,240 and the test name that failed and it 19036 16:17:26,436 --> 16:17:31,436 will also tell us the two values that 19037 16:17:28,240 --> 16:17:33,916 are not equal to each other and here it 19038 16:17:31,436 --> 16:17:35,796 will tell us where this test failed so 19039 16:17:33,916 --> 16:17:38,840 it tell us that it failed in money 19040 16:17:35,796 --> 16:17:41,840 test.js at line 19041 16:17:38,840 --> 16:17:45,840 13 so if we go back to our 19042 16:17:41,840 --> 16:17:48,880 code and we open the Jasmine money 19043 16:17:45,840 --> 16:17:50,720 test.js line 13 is right here so just 19044 16:17:48,880 --> 16:17:53,720 telling us that this line failed the 19045 16:17:50,720 --> 16:17:53,720 test 19046 16:17:55,956 --> 16:18:00,480 here we can see the full list of tests 19047 16:17:58,756 --> 16:18:04,040 and it will tell us that we have three 19048 16:18:00,480 --> 16:18:06,360 tests two of them passed and one of them 19049 16:18:04,040 --> 16:18:09,116 failed so Jasmine gives us a lot of 19050 16:18:06,360 --> 16:18:10,796 details on where a test failed to help 19051 16:18:09,116 --> 16:18:14,276 us fix the 19052 16:18:10,796 --> 16:18:17,040 issue now let's add back math.round to 19053 16:18:14,276 --> 16:18:23,480 make everything pass again we'll open 19054 16:18:17,040 --> 16:18:23,480 our code and open UTS money.jpg 19055 16:18:26,400 --> 16:18:32,040 math.round Open Bracket and close 19056 16:18:30,080 --> 16:18:35,320 bracket and 19057 16:18:32,040 --> 16:18:37,596 save and if we check our test page 19058 16:18:35,320 --> 16:18:40,240 everything passes 19059 16:18:37,596 --> 16:18:43,756 again so this is how we use a testing 19060 16:18:40,240 --> 16:18:46,080 framework like Jasmine to create tests 19061 16:18:43,756 --> 16:18:49,916 the testing framework gives us a bunch 19062 16:18:46,080 --> 16:18:52,520 of functions such as describe it and 19063 16:18:49,916 --> 16:18:55,596 expect and then we use these functions 19064 16:18:52,520 --> 16:18:58,360 to create our test so so these functions 19065 16:18:55,596 --> 16:19:01,040 have a lot of features inside them they 19066 16:18:58,360 --> 16:19:04,200 do all of the setup for us and they also 19067 16:19:01,040 --> 16:19:06,480 display the test on a nice web page for 19068 16:19:04,200 --> 16:19:09,720 us now there's one more thing I want to 19069 16:19:06,480 --> 16:19:12,880 mention which is we can use describe 19070 16:19:09,720 --> 16:19:15,636 inside describe this helps us organize 19071 16:19:12,880 --> 16:19:15,636 our tests 19072 16:19:16,480 --> 16:19:22,160 further so now let's save our changes 19073 16:19:18,956 --> 16:19:24,956 into git we'll open the git 19074 16:19:22,160 --> 16:19:25,956 section and let's close this file at the 19075 16:19:24,956 --> 16:19:30,200 top for 19076 16:19:25,956 --> 16:19:32,276 now so inside money test.js we created 19077 16:19:30,200 --> 16:19:35,320 all the tests that we had before for 19078 16:19:32,276 --> 16:19:38,520 format currency by using Jasmine this 19079 16:19:35,320 --> 16:19:42,756 time and for spec Runner we rename this 19080 16:19:38,520 --> 16:19:46,160 file to test.html and finally we deleted 19081 16:19:42,756 --> 16:19:48,116 all the test code that came with Jasmine 19082 16:19:46,160 --> 16:19:48,796 so now let's add a message to describe 19083 16:19:48,116 --> 16:19:50,360 our 19084 16:19:48,796 --> 16:19:55,320 changes 19085 16:19:50,360 --> 16:20:00,080 remove default tests and add 19086 16:19:55,320 --> 16:20:00,080 money tests and 19087 16:20:04,436 --> 16:20:09,956 commit now let's test a different 19088 16:20:06,756 --> 16:20:11,636 function using Jasmine and along the way 19089 16:20:09,956 --> 16:20:13,240 we're going to learn some more features 19090 16:20:11,636 --> 16:20:16,200 of testing 19091 16:20:13,240 --> 16:20:18,320 Frameworks let's go back to our 19092 16:20:16,200 --> 16:20:23,560 code and we're going to scroll to the 19093 16:20:18,320 --> 16:20:26,560 top and open the data/ cart. JS 19094 16:20:23,560 --> 16:20:29,596 file so inside this file we have a 19095 16:20:26,560 --> 16:20:31,560 function called add to cart so we're 19096 16:20:29,596 --> 16:20:32,560 going to create tests for the add to 19097 16:20:31,560 --> 16:20:35,636 cart 19098 16:20:32,560 --> 16:20:38,116 function first since this function is in 19099 16:20:35,636 --> 16:20:41,520 a different file we're going to create a 19100 16:20:38,116 --> 16:20:44,436 new test file so we're going to scroll 19101 16:20:41,520 --> 16:20:47,000 down and let's close the test folder so 19102 16:20:44,436 --> 16:20:50,116 we don't get confused and then inside 19103 16:20:47,000 --> 16:20:53,916 test- Jasmine we're going to click in 19104 16:20:50,116 --> 16:20:58,320 this folder and then create a new file 19105 16:20:53,916 --> 16:21:00,956 and let's name it cart 19106 16:20:58,320 --> 16:21:04,276 test.js and press 19107 16:21:00,956 --> 16:21:06,720 enter so we name it the original file 19108 16:21:04,276 --> 16:21:08,636 name which is cart and then just add 19109 16:21:06,720 --> 16:21:11,796 test at the 19110 16:21:08,636 --> 16:21:15,000 end and to better organize our test 19111 16:21:11,796 --> 16:21:18,360 files we usually try to match the folder 19112 16:21:15,000 --> 16:21:22,720 structure of the code so for example in 19113 16:21:18,360 --> 16:21:25,796 our code money. JS is inside this UIL 19114 16:21:22,720 --> 16:21:28,240 folder so inside our tests we should 19115 16:21:25,796 --> 16:21:31,200 also create a utils folder and put this 19116 16:21:28,240 --> 16:21:34,880 file inside so we'll create a new folder 19117 16:21:31,200 --> 16:21:37,000 by clicking up here and let's name it 19118 16:21:34,880 --> 16:21:40,080 utils and press 19119 16:21:37,000 --> 16:21:42,596 enter and then move the money test.js 19120 16:21:40,080 --> 16:21:42,596 file 19121 16:21:43,636 --> 16:21:48,560 inside if it asks you if you want to 19122 16:21:46,040 --> 16:21:49,880 update Imports automatically you can 19123 16:21:48,560 --> 16:21:53,436 click 19124 16:21:49,880 --> 16:21:56,080 yes if you check your code scroll to the 19125 16:21:53,436 --> 16:21:58,956 top make sure that your file path starts 19126 16:21:56,080 --> 16:22:01,000 with do do slash do do slash because now 19127 16:21:58,956 --> 16:22:03,916 we're inside another 19128 16:22:01,000 --> 16:22:06,436 folder VSS code sometimes updates the 19129 16:22:03,916 --> 16:22:08,880 file paths for us automatically but if 19130 16:22:06,436 --> 16:22:12,080 it doesn't we can just add an extra dot 19131 16:22:08,880 --> 16:22:12,080 do slash in front 19132 16:22:12,240 --> 16:22:19,240 ourselves and we're also going to open 19133 16:22:14,880 --> 16:22:24,560 money test.js and save this 19134 16:22:19,240 --> 16:22:28,040 file next if we scroll up cart. JS is in 19135 16:22:24,560 --> 16:22:30,720 inside a data folder so inside our tests 19136 16:22:28,040 --> 16:22:32,160 we're also going to create a data folder 19137 16:22:30,720 --> 16:22:35,756 let's click cart 19138 16:22:32,160 --> 16:22:38,956 test.js we'll create a new folder and 19139 16:22:35,756 --> 16:22:40,080 let's name this the data folder and 19140 16:22:38,956 --> 16:22:45,560 press 19141 16:22:40,080 --> 16:22:45,560 enter and then move card test.js inside 19142 16:22:48,000 --> 16:22:52,560 here now you'll notice that this folder 19143 16:22:50,360 --> 16:22:54,956 structure doesn't perfectly match the 19144 16:22:52,560 --> 16:22:57,276 folder structure that we have up here 19145 16:22:54,956 --> 16:22:59,840 because utils is inside the scripts 19146 16:22:57,276 --> 16:23:02,436 folder but usually this data folder 19147 16:22:59,840 --> 16:23:04,200 should also be inside the scripts folder 19148 16:23:02,436 --> 16:23:05,080 I just made a mistake when structuring 19149 16:23:04,200 --> 16:23:07,480 the 19150 16:23:05,080 --> 16:23:10,480 project so let's just use this folder 19151 16:23:07,480 --> 16:23:10,480 structure in our 19152 16:23:10,880 --> 16:23:17,320 tests okay so now let's open cart 19153 16:23:14,560 --> 16:23:18,560 test.js and now we're ready to test the 19154 16:23:17,320 --> 16:23:22,040 add to cart 19155 16:23:18,560 --> 16:23:25,560 function so first let's import add to 19156 16:23:22,040 --> 16:23:29,080 cart at the top we'll import 19157 16:23:25,560 --> 16:23:31,916 curly brackets add to 19158 16:23:29,080 --> 16:23:34,680 cart from a 19159 16:23:31,916 --> 16:23:37,596 string and we're going to locate cart. 19160 16:23:34,680 --> 16:23:41,240 JS so first we need to get out of the 19161 16:23:37,596 --> 16:23:45,116 data folder using do slash and then get 19162 16:23:41,240 --> 16:23:48,080 out of the test djine folder do do slash 19163 16:23:45,116 --> 16:23:51,880 and we'll go into this data folder data 19164 16:23:48,080 --> 16:23:51,880 SL cart. 19165 16:23:51,916 --> 16:23:57,956 JS next let's create a test suite for 19166 16:23:55,320 --> 16:24:00,480 this function so we'll create some new 19167 16:23:57,956 --> 16:24:03,596 lines and to create a test Suite in 19168 16:24:00,480 --> 16:24:05,480 Jasmine we'll use the function 19169 16:24:03,596 --> 16:24:07,796 describe 19170 16:24:05,480 --> 16:24:10,756 brackets and we'll give this test Suite 19171 16:24:07,796 --> 16:24:15,916 A Name by giving it a 19172 16:24:10,756 --> 16:24:18,596 string so let's use the name test Suite 19173 16:24:15,916 --> 16:24:20,916 colon add to 19174 16:24:18,596 --> 16:24:24,560 cart and we'll give it a second 19175 16:24:20,916 --> 16:24:27,360 parameter which is a function brackets 19176 16:24:24,560 --> 16:24:30,400 arrow and curly 19177 16:24:27,360 --> 16:24:33,040 brackets and now inside this test Suite 19178 16:24:30,400 --> 16:24:35,480 we're going to add some tests so let's 19179 16:24:33,040 --> 16:24:37,436 look at the code for add to cart to see 19180 16:24:35,480 --> 16:24:41,560 what kind of tests we should 19181 16:24:37,436 --> 16:24:45,240 add so we'll open data/ cart. 19182 16:24:41,560 --> 16:24:48,160 JS and then inside add to cart you'll 19183 16:24:45,240 --> 16:24:51,320 notice that this function uses an if 19184 16:24:48,160 --> 16:24:53,680 statement a best practice in testing is 19185 16:24:51,320 --> 16:24:54,956 to test each condition of the if 19186 16:24:53,680 --> 16:24:57,880 statement 19187 16:24:54,956 --> 16:25:00,200 this is known as test coverage test 19188 16:24:57,880 --> 16:25:03,320 coverage means how much of the code is 19189 16:25:00,200 --> 16:25:07,040 being tested and a best practice is we 19190 16:25:03,320 --> 16:25:09,916 try to maximize test 19191 16:25:07,040 --> 16:25:12,240 coverage so for this if statement we'll 19192 16:25:09,916 --> 16:25:14,200 create a test for the first condition 19193 16:25:12,240 --> 16:25:17,000 which is when the product is already in 19194 16:25:14,200 --> 16:25:19,916 the cart and we'll create a test for the 19195 16:25:17,000 --> 16:25:22,240 else condition when the product is not 19196 16:25:19,916 --> 16:25:22,240 in the 19197 16:25:22,956 --> 16:25:27,480 cart let's go back to cart 19198 16:25:28,040 --> 16:25:33,480 test.js and inside this test Suite we're 19199 16:25:31,360 --> 16:25:37,520 going to create a test using the 19200 16:25:33,480 --> 16:25:41,080 function it brackets and we'll give it a 19201 16:25:37,520 --> 16:25:43,080 string and this will be the test name so 19202 16:25:41,080 --> 16:25:46,840 for the video I'm just going to scroll 19203 16:25:43,080 --> 16:25:49,560 up and take this cart. JS file and put 19204 16:25:46,840 --> 16:25:52,880 it at the top here so we can see our 19205 16:25:49,560 --> 16:25:55,596 code and our test side by side so I'm 19206 16:25:52,880 --> 16:25:57,680 going to scroll down to the add to card 19207 16:25:55,596 --> 16:26:00,436 function and we mentioned that we're 19208 16:25:57,680 --> 16:26:03,116 going to test each condition of this if 19209 16:26:00,436 --> 16:26:05,116 statement so the first test is going to 19210 16:26:03,116 --> 16:26:11,276 be when the product is already in the 19211 16:26:05,116 --> 16:26:16,160 cart so that here let's name it ads and 19212 16:26:11,276 --> 16:26:19,240 existing product to the cart comma 19213 16:26:16,160 --> 16:26:22,000 brackets arrow and curly 19214 16:26:19,240 --> 16:26:23,916 brackets and let's add another test for 19215 16:26:22,000 --> 16:26:27,480 when the product is not in the cart so 19216 16:26:23,916 --> 16:26:31,200 this else condition so down here we'll 19217 16:26:27,480 --> 16:26:36,200 create another test using it brackets 19218 16:26:31,200 --> 16:26:41,840 and a string and the test name adds a 19219 16:26:36,200 --> 16:26:46,720 new product to the cart comma and a 19220 16:26:41,840 --> 16:26:49,080 function brackets arrow and curly 19221 16:26:46,720 --> 16:26:51,796 brackets so these are the two tests that 19222 16:26:49,080 --> 16:26:53,116 we need for the two conditions of this 19223 16:26:51,796 --> 16:26:55,480 if 19224 16:26:53,116 --> 16:26:58,080 statement let's actually start with 19225 16:26:55,480 --> 16:27:00,276 adding a new product to the cart since 19226 16:26:58,080 --> 16:27:03,240 it's a little easier to 19227 16:27:00,276 --> 16:27:06,276 test now unlike the format currency 19228 16:27:03,240 --> 16:27:09,200 function add to cart doesn't return a 19229 16:27:06,276 --> 16:27:11,436 value so we can't really compare it to 19230 16:27:09,200 --> 16:27:14,116 another value using 19231 16:27:11,436 --> 16:27:16,916 expect instead we're going to call add 19232 16:27:14,116 --> 16:27:20,560 to cart to modify the cart and then 19233 16:27:16,916 --> 16:27:24,240 check if this cart looks correct to do 19234 16:27:20,560 --> 16:27:28,916 that inside here let's call the function 19235 16:27:24,240 --> 16:27:32,636 add to cart 19236 16:27:28,916 --> 16:27:35,796 brackets and this function takes one 19237 16:27:32,636 --> 16:27:39,880 string which is the product 19238 16:27:35,796 --> 16:27:42,200 ID so inside here let's give it a string 19239 16:27:39,880 --> 16:27:45,000 and to find a product ID that we can 19240 16:27:42,200 --> 16:27:47,520 test with we're going to go to the left 19241 16:27:45,000 --> 16:27:50,956 and scroll up and we're going to open 19242 16:27:47,520 --> 16:27:54,160 the data/ products. JS 19243 16:27:50,956 --> 16:27:56,596 file and inside here we have a list of 19244 16:27:54,160 --> 16:27:59,756 products so let's just take the first 19245 16:27:56,596 --> 16:28:04,956 product ID and use it in our test we're 19246 16:27:59,756 --> 16:28:08,436 going to select this ID and right click 19247 16:28:04,956 --> 16:28:11,436 copy and then inside cart 19248 16:28:08,436 --> 16:28:14,436 test.js in this string we're going to 19249 16:28:11,436 --> 16:28:17,560 rightclick and 19250 16:28:14,436 --> 16:28:20,596 paste so this will add this product ID 19251 16:28:17,560 --> 16:28:22,880 to the cart and now to check if this 19252 16:28:20,596 --> 16:28:25,436 code is working correctly we're going to 19253 16:28:22,880 --> 16:28:28,040 check what the cart look looks like so 19254 16:28:25,436 --> 16:28:30,360 first we need to import the cart we're 19255 16:28:28,040 --> 16:28:33,480 going to scroll up and since we're 19256 16:28:30,360 --> 16:28:36,916 already importing from cart. JS to UT 19257 16:28:33,480 --> 16:28:40,200 the cart we can type a comma 19258 16:28:36,916 --> 16:28:42,040 cart and then at the bottom here we're 19259 16:28:40,200 --> 16:28:45,596 going to create a new 19260 16:28:42,040 --> 16:28:48,596 line and first let's check if the card. 19261 16:28:45,596 --> 16:28:51,560 length is correct so if we assume that 19262 16:28:48,596 --> 16:28:54,200 we start with an empty cart when we add 19263 16:28:51,560 --> 16:28:56,636 a product the cart. length should should 19264 16:28:54,200 --> 16:29:00,040 be one so we can compare these two 19265 16:28:56,636 --> 16:29:01,720 values cart. length and one so to 19266 16:29:00,040 --> 16:29:05,956 compare values we're going to use the 19267 16:29:01,720 --> 16:29:07,480 function expect brackets and we'll 19268 16:29:05,956 --> 16:29:09,560 compare the 19269 16:29:07,480 --> 16:29:13,360 cart. 19270 16:29:09,560 --> 16:29:17,840 length and then here we'll use the 19271 16:29:13,360 --> 16:29:20,560 method2 equal brackets 19272 16:29:17,840 --> 16:29:23,720 1 so now let's save this 19273 16:29:20,560 --> 16:29:25,596 file and we're going to run these tests 19274 16:29:23,720 --> 16:29:30,480 to run them we're just going to load 19275 16:29:25,596 --> 16:29:33,320 this file inside test.html so we'll open 19276 16:29:30,480 --> 16:29:35,116 this and then at the bottom we'll create 19277 16:29:33,320 --> 16:29:38,796 a script 19278 16:29:35,116 --> 16:29:42,160 tag give it a source attribute double 19279 16:29:38,796 --> 16:29:44,680 quotes and we're going to load this file 19280 16:29:42,160 --> 16:29:48,240 so this file is inside the data folder 19281 16:29:44,680 --> 16:29:54,320 so we need to type data SL to go into 19282 16:29:48,240 --> 16:29:57,080 the folder and then load cart test.js 19283 16:29:54,320 --> 16:29:59,520 and this file also uses import so we're 19284 16:29:57,080 --> 16:30:02,916 going to give it the attribute type 19285 16:29:59,520 --> 16:30:03,840 equals module to enable JavaScript 19286 16:30:02,916 --> 16:30:06,040 module 19287 16:30:03,840 --> 16:30:10,200 features and one thing that I forgot to 19288 16:30:06,040 --> 16:30:12,520 do is we moved money test into the util 19289 16:30:10,200 --> 16:30:15,916 folder so we need to update the source 19290 16:30:12,520 --> 16:30:18,400 attribute to add the utils folder and 19291 16:30:15,916 --> 16:30:20,160 then slash to go into the folder and 19292 16:30:18,400 --> 16:30:25,040 then locate money 19293 16:30:20,160 --> 16:30:28,756 test.js so now let's save this HTML file 19294 16:30:25,040 --> 16:30:28,756 and open this test page in our 19295 16:30:31,436 --> 16:30:35,796 browser and now we should have two 19296 16:30:33,680 --> 16:30:38,400 additional tests and the test that we 19297 16:30:35,796 --> 16:30:40,200 just created should fail if it doesn't 19298 16:30:38,400 --> 16:30:43,916 fail for you you can go into your 19299 16:30:40,200 --> 16:30:47,596 console and run local storage. CLE and 19300 16:30:43,916 --> 16:30:50,240 then refresh the page and it should 19301 16:30:47,596 --> 16:30:53,000 fail so this tells us that the failure 19302 16:30:50,240 --> 16:30:55,880 happened in the test suite at Dart in 19303 16:30:53,000 --> 16:30:58,200 the test adds a new product to the cart 19304 16:30:55,880 --> 16:31:00,596 which is the one we just 19305 16:30:58,200 --> 16:31:03,476 created so it's telling us that the 19306 16:31:00,596 --> 16:31:06,200 cart. length is not equal to one it's 19307 16:31:03,476 --> 16:31:09,796 equal to a different number so why is 19308 16:31:06,200 --> 16:31:13,000 this happening let's go back to our 19309 16:31:09,796 --> 16:31:16,240 test open cart 19310 16:31:13,000 --> 16:31:19,276 test.js and in here remember that we 19311 16:31:16,240 --> 16:31:21,596 assumed the cart starts as empty and if 19312 16:31:19,276 --> 16:31:24,720 we add a product the card dot length 19313 16:31:21,596 --> 16:31:27,720 would be one however the cart doesn't 19314 16:31:24,720 --> 16:31:27,720 actually start as 19315 16:31:27,840 --> 16:31:34,720 empty in cart. JS at the top here we're 19316 16:31:31,916 --> 16:31:36,636 loading the cart from local storage if 19317 16:31:34,720 --> 16:31:40,320 there's nothing in local storage we use 19318 16:31:36,636 --> 16:31:43,400 this default cart so our starting cart 19319 16:31:40,320 --> 16:31:46,040 depends on what's inside local storage 19320 16:31:43,400 --> 16:31:48,840 and this is a big problem when testing 19321 16:31:46,040 --> 16:31:50,840 because local storage can change if 19322 16:31:48,840 --> 16:31:54,360 there's an empty card saved in local 19323 16:31:50,840 --> 16:31:56,520 storage our test will pass but if 19324 16:31:54,360 --> 16:32:00,200 there's not an empty cart in local 19325 16:31:56,520 --> 16:32:03,200 storage then this test will fail this is 19326 16:32:00,200 --> 16:32:05,796 known as a flaky test a test that 19327 16:32:03,200 --> 16:32:07,956 sometimes passes and sometimes fails 19328 16:32:05,796 --> 16:32:10,880 even if we don't change the 19329 16:32:07,956 --> 16:32:13,520 code now to solve this problem we're 19330 16:32:10,880 --> 16:32:17,200 going to use a feature of Jasmine called 19331 16:32:13,520 --> 16:32:20,116 mocks a mock lets us replace a method 19332 16:32:17,200 --> 16:32:23,400 with a fake version and then we can make 19333 16:32:20,116 --> 16:32:24,520 the fake version do anything we want for 19334 16:32:23,400 --> 16:32:27,436 example 19335 16:32:24,520 --> 16:32:30,880 when we load the cart from local storage 19336 16:32:27,436 --> 16:32:33,560 we're using local storage. getet item so 19337 16:32:30,880 --> 16:32:36,400 we can use a mock to create a fake 19338 16:32:33,560 --> 16:32:38,680 version of get item and then we can make 19339 16:32:36,400 --> 16:32:40,636 this fake version do anything we want 19340 16:32:38,680 --> 16:32:43,560 like return an empty 19341 16:32:40,636 --> 16:32:44,596 array so let's go ahead and create our 19342 16:32:43,560 --> 16:32:47,400 first 19343 16:32:44,596 --> 16:32:50,916 mock we'll go back to cart 19344 16:32:47,400 --> 16:32:52,880 test.js and at the top of this test 19345 16:32:50,916 --> 16:32:56,796 we're going to create a mock using 19346 16:32:52,880 --> 16:32:59,436 another function of Jasmine called spy 19347 16:32:56,796 --> 16:33:02,636 on and 19348 16:32:59,436 --> 16:33:05,240 brackets and we'll give spy on two 19349 16:33:02,636 --> 16:33:08,080 parameters the first parameter is the 19350 16:33:05,240 --> 16:33:12,436 object that we want to mock in our case 19351 16:33:08,080 --> 16:33:15,560 it's local storage so we'll type local 19352 16:33:12,436 --> 16:33:18,000 storage comma and then we'll give it a 19353 16:33:15,560 --> 16:33:21,200 string and this string will be the 19354 16:33:18,000 --> 16:33:24,000 method that we want to Mark in our case 19355 16:33:21,200 --> 16:33:28,756 we want to Mark the get item method so 19356 16:33:24,000 --> 16:33:31,240 here we'll type get item and make sure I 19357 16:33:28,756 --> 16:33:32,476 is uppercase so that we exactly match 19358 16:33:31,240 --> 16:33:35,916 the method 19359 16:33:32,476 --> 16:33:39,796 name so now this will replace local 19360 16:33:35,916 --> 16:33:41,756 storage. getet item with a fake version 19361 16:33:39,796 --> 16:33:45,880 and we can make this fake version do 19362 16:33:41,756 --> 16:33:49,000 anything we want to do that spy on gives 19363 16:33:45,880 --> 16:33:50,680 us an object and this object has a 19364 16:33:49,000 --> 16:33:54,400 property we can 19365 16:33:50,680 --> 16:33:57,160 use so at the end we'll type Dot 19366 16:33:54,400 --> 16:34:01,160 and the property called 19367 16:33:57,160 --> 16:34:05,916 an and this result is also an object and 19368 16:34:01,160 --> 16:34:09,916 this object has a method called dot call 19369 16:34:05,916 --> 16:34:12,796 fake brackets so don't worry about all 19370 16:34:09,916 --> 16:34:16,000 the Dots here Jasmine just organizes its 19371 16:34:12,796 --> 16:34:18,560 code so that it reads like English so 19372 16:34:16,000 --> 16:34:22,116 we're spying on local storage. getet 19373 16:34:18,560 --> 16:34:26,276 item and call fake so next we're going 19374 16:34:22,116 --> 16:34:29,040 to give call fake a function brackets 19375 16:34:26,276 --> 16:34:32,080 arrow and curly 19376 16:34:29,040 --> 16:34:34,796 brackets and this function is what we 19377 16:34:32,080 --> 16:34:37,636 want get item to do so we're essentially 19378 16:34:34,796 --> 16:34:40,200 overwriting the original get item with 19379 16:34:37,636 --> 16:34:42,916 whatever is inside this 19380 16:34:40,200 --> 16:34:45,436 function so earlier we said we wanted 19381 16:34:42,916 --> 16:34:48,916 get item to return the empty array for 19382 16:34:45,436 --> 16:34:52,560 this test so inside this fake function 19383 16:34:48,916 --> 16:34:54,360 let's make get item return the empty 19384 16:34:52,560 --> 16:34:56,276 array 19385 16:34:54,360 --> 16:34:58,720 now remember that local storage only 19386 16:34:56,276 --> 16:35:01,840 supports strings so we need to return 19387 16:34:58,720 --> 16:35:08,160 this as a string to do that we're going 19388 16:35:01,840 --> 16:35:12,476 to use the code Json all uppercase do 19389 16:35:08,160 --> 16:35:14,840 stringify Open Bracket and close bracket 19390 16:35:12,476 --> 16:35:17,796 so this will convert whatever is in the 19391 16:35:14,840 --> 16:35:19,360 brackets to a Json string and that's 19392 16:35:17,796 --> 16:35:22,116 what we need for local 19393 16:35:19,360 --> 16:35:24,240 storage so now let's test out this mock 19394 16:35:22,116 --> 16:35:27,680 to see if it works 19395 16:35:24,240 --> 16:35:29,436 below this we're going to 19396 16:35:27,680 --> 16:35:32,916 console.log 19397 16:35:29,436 --> 16:35:38,840 brackets and run local 19398 16:35:32,916 --> 16:35:41,040 storage doget capital I item brackets 19399 16:35:38,840 --> 16:35:45,000 the string 19400 16:35:41,040 --> 16:35:47,596 cart let's save this file and then open 19401 16:35:45,000 --> 16:35:49,560 the test page in our 19402 16:35:47,596 --> 16:35:51,320 browser and we're going to open the 19403 16:35:49,560 --> 16:35:54,276 console so 19404 16:35:51,320 --> 16:35:56,276 rightclick inspect 19405 16:35:54,276 --> 16:35:59,276 and the 19406 16:35:56,276 --> 16:36:02,040 console and you'll see that Here Local 19407 16:35:59,276 --> 16:36:04,400 storage. getet item is no longer loading 19408 16:36:02,040 --> 16:36:07,116 from the actual local storage it's 19409 16:36:04,400 --> 16:36:12,400 giving us an empty array every 19410 16:36:07,116 --> 16:36:12,400 time so now we have mocked the get item 19411 16:36:12,796 --> 16:36:18,436 method however you'll also notice that 19412 16:36:15,636 --> 16:36:19,476 our test is still failing so how come it 19413 16:36:18,436 --> 16:36:22,360 still 19414 16:36:19,476 --> 16:36:25,240 fails to understand what's happening 19415 16:36:22,360 --> 16:36:28,116 let's take a closer look at our code 19416 16:36:25,240 --> 16:36:28,116 we'll go back to our 19417 16:36:28,160 --> 16:36:34,916 code and inside our tests here we're 19418 16:36:31,840 --> 16:36:38,560 mocking local storage. getet item but 19419 16:36:34,916 --> 16:36:41,596 notice that above this if we scroll up 19420 16:36:38,560 --> 16:36:45,000 we import the cart at the top so the 19421 16:36:41,596 --> 16:36:47,840 order of this code matters first we load 19422 16:36:45,000 --> 16:36:50,880 the cart from local storage and after 19423 16:36:47,840 --> 16:36:52,720 we're mocking local storage. getet item 19424 16:36:50,880 --> 16:36:54,916 so the card is already loaded at this 19425 16:36:52,720 --> 16:36:56,200 point and this code doesn't have the 19426 16:36:54,916 --> 16:37:00,040 effect that we 19427 16:36:56,200 --> 16:37:02,796 want so one way to solve this problem is 19428 16:37:00,040 --> 16:37:06,880 after we mock local storage here we 19429 16:37:02,796 --> 16:37:09,596 should reload the cart so inside cart. 19430 16:37:06,880 --> 16:37:13,880 JS just going to give it a little more 19431 16:37:09,596 --> 16:37:17,040 space to reload this cart after we mock 19432 16:37:13,880 --> 16:37:18,880 we just need to rerun all of this code 19433 16:37:17,040 --> 16:37:22,756 so let's create a function so we can 19434 16:37:18,880 --> 16:37:25,596 rerun this code at the bottom here we'll 19435 16:37:22,756 --> 16:37:30,476 create a 19436 16:37:25,596 --> 16:37:33,956 function and we'll call it load from 19437 16:37:30,476 --> 16:37:36,040 Storage brackets and curly 19438 16:37:33,956 --> 16:37:39,116 brackets and we're just going to move 19439 16:37:36,040 --> 16:37:42,276 all of this code inside the function so 19440 16:37:39,116 --> 16:37:44,636 let's select all the code up to cart 19441 16:37:42,276 --> 16:37:47,000 equals we'll 19442 16:37:44,636 --> 16:37:50,040 rightclick 19443 16:37:47,000 --> 16:37:54,000 cut and then in the function we'll 19444 16:37:50,040 --> 16:37:56,476 rightclick and paste 19445 16:37:54,000 --> 16:38:01,160 and at the top let's create the cart 19446 16:37:56,476 --> 16:38:04,116 variable again let cart and 19447 16:38:01,160 --> 16:38:06,720 semicolon and lastly when we load this 19448 16:38:04,116 --> 16:38:09,796 file we need to run this function so 19449 16:38:06,720 --> 16:38:12,880 that we run this code at least 19450 16:38:09,796 --> 16:38:17,636 once so down here we'll just run this 19451 16:38:12,880 --> 16:38:20,360 function load from storage and 19452 16:38:17,636 --> 16:38:22,796 brackets so let's 19453 16:38:20,360 --> 16:38:25,520 save so we're basically running the 19454 16:38:22,796 --> 16:38:28,400 exact same code as before except this 19455 16:38:25,520 --> 16:38:31,436 code is now inside a 19456 16:38:28,400 --> 16:38:34,840 function and now in our 19457 16:38:31,436 --> 16:38:37,560 test after we mock local storage. getet 19458 16:38:34,840 --> 16:38:40,040 item we're going to reload the cart 19459 16:38:37,560 --> 16:38:42,796 using this function so first let's 19460 16:38:40,040 --> 16:38:44,476 export it so we can use it in our test 19461 16:38:42,796 --> 16:38:48,320 we'll type 19462 16:38:44,476 --> 16:38:51,160 export and save this file and Insider 19463 16:38:48,320 --> 16:38:53,680 test let's scroll up to the top we're 19464 16:38:51,160 --> 16:38:59,680 already importing from cart. Js so we'll 19465 16:38:53,680 --> 16:39:02,040 type a comma and we'll import load from 19466 16:38:59,680 --> 16:39:05,956 storage and then we'll scroll down back 19467 16:39:02,040 --> 16:39:10,116 to our test and here after we mock local 19468 16:39:05,956 --> 16:39:15,916 storage. getet item we're going to load 19469 16:39:10,116 --> 16:39:19,560 from Storage brackets and 19470 16:39:15,916 --> 16:39:22,000 save so this time we mock local storage. 19471 16:39:19,560 --> 16:39:25,560 getet item first to return an empty 19472 16:39:22,000 --> 16:39:28,040 array and then when we reload the cart 19473 16:39:25,560 --> 16:39:30,840 this is going to be an empty array and 19474 16:39:28,040 --> 16:39:33,240 the cart will be empty and now in our 19475 16:39:30,840 --> 16:39:35,796 test when we add a product to an empty 19476 16:39:33,240 --> 16:39:39,000 cart the cart length will be equal to 19477 16:39:35,796 --> 16:39:43,596 one so this test will pass so we can 19478 16:39:39,000 --> 16:39:46,200 actually remove the console.log here and 19479 16:39:43,596 --> 16:39:50,680 save and check our test 19480 16:39:46,200 --> 16:39:53,720 page and now this test should be 19481 16:39:50,680 --> 16:39:56,796 passing next let's go back to our code 19482 16:39:53,720 --> 16:39:58,720 Cod and if we look at the add to cart 19483 16:39:56,796 --> 16:40:03,636 function in cart. 19484 16:39:58,720 --> 16:40:05,916 JS so in here at the bottom of this 19485 16:40:03,636 --> 16:40:08,360 function you'll notice that we're saving 19486 16:40:05,916 --> 16:40:11,916 the cart to local storage so this is 19487 16:40:08,360 --> 16:40:14,560 going to call local storage. set 19488 16:40:11,916 --> 16:40:16,796 item however we don't actually want to 19489 16:40:14,560 --> 16:40:19,160 save to local storage because this is 19490 16:40:16,796 --> 16:40:21,956 just test code we don't want our test 19491 16:40:19,160 --> 16:40:24,200 code to be modifying local storage or 19492 16:40:21,956 --> 16:40:26,560 affecting our real code 19493 16:40:24,200 --> 16:40:28,720 to prevent this we're also going to mock 19494 16:40:26,560 --> 16:40:31,840 local storage. set 19495 16:40:28,720 --> 16:40:34,160 item so down here we're going to create 19496 16:40:31,840 --> 16:40:36,116 some new lines at the top of the test 19497 16:40:34,160 --> 16:40:40,796 and we're going to mock local storage. 19498 16:40:36,116 --> 16:40:44,320 set item using the function spy on 19499 16:40:40,796 --> 16:40:48,200 brackets and we'll give it local 19500 16:40:44,320 --> 16:40:51,796 storage comma a string and the method we 19501 16:40:48,200 --> 16:40:54,756 want to mock which is set 19502 16:40:51,796 --> 16:40:57,636 item and 19503 16:40:54,756 --> 16:41:00,000 save and remember the order of the code 19504 16:40:57,636 --> 16:41:03,040 matters so we want to mock local 19505 16:41:00,000 --> 16:41:06,080 storage. set item first and then we call 19506 16:41:03,040 --> 16:41:08,916 add to cart so now set item will be 19507 16:41:06,080 --> 16:41:12,596 replaced with a fake version and this 19508 16:41:08,916 --> 16:41:12,596 will no longer save to local 19509 16:41:13,200 --> 16:41:19,916 storage next spyon has another useful 19510 16:41:16,796 --> 16:41:23,200 feature which is it records every time a 19511 16:41:19,916 --> 16:41:25,636 method is used for example what if we 19512 16:41:23,200 --> 16:41:28,596 want to make sure that add to cart saves 19513 16:41:25,636 --> 16:41:31,880 the cart to local storage at the end in 19514 16:41:28,596 --> 16:41:33,956 our tests set item is mocked so we can't 19515 16:41:31,880 --> 16:41:37,080 really check what's inside the local 19516 16:41:33,956 --> 16:41:40,596 storage instead we can just check if add 19517 16:41:37,080 --> 16:41:44,596 to cart calls set item at some 19518 16:41:40,596 --> 16:41:47,756 point to check if set item was called at 19519 16:41:44,596 --> 16:41:49,476 the bottom we can use the function 19520 16:41:47,756 --> 16:41:52,000 expect 19521 16:41:49,476 --> 16:41:56,840 brackets and we're going to give this 19522 16:41:52,000 --> 16:42:00,160 the method that we loed which is local 19523 16:41:56,840 --> 16:42:03,080 storage. set 19524 16:42:00,160 --> 16:42:05,680 item and remember that expect gives us 19525 16:42:03,080 --> 16:42:08,596 an object and this object has many 19526 16:42:05,680 --> 16:42:09,756 useful methods to check stuff like dot 19527 16:42:08,596 --> 16:42:13,320 to 19528 16:42:09,756 --> 16:42:21,916 equal so another method we get from 19529 16:42:13,320 --> 16:42:23,520 expect is dot 2 have been called times 19530 16:42:21,916 --> 16:42:25,000 brackets 19531 16:42:23,520 --> 16:42:28,200 so make sure that you write this method 19532 16:42:25,000 --> 16:42:31,116 in cam case two is lowercase and every 19533 16:42:28,200 --> 16:42:33,956 word after starts with a 19534 16:42:31,116 --> 16:42:37,276 capital so this method checks how many 19535 16:42:33,956 --> 16:42:40,680 times local storage. set item was called 19536 16:42:37,276 --> 16:42:43,436 in the code above so we expect it to be 19537 16:42:40,680 --> 16:42:45,560 called once inside add to cart so we're 19538 16:42:43,436 --> 16:42:48,680 going to give it the number 19539 16:42:45,560 --> 16:42:51,360 one so keep in mind that this only works 19540 16:42:48,680 --> 16:42:53,636 if this method has been mocked with spy 19541 16:42:51,360 --> 16:42:56,160 on so now let's 19542 16:42:53,636 --> 16:42:58,360 save and open our test 19543 16:42:56,160 --> 16:43:02,160 page and you can see that all of our 19544 16:42:58,360 --> 16:43:03,560 tests passed and local storage. set item 19545 16:43:02,160 --> 16:43:07,956 was called 19546 16:43:03,560 --> 16:43:10,636 once so after we mock a method we can 19547 16:43:07,956 --> 16:43:13,880 check how many times this method was 19548 16:43:10,636 --> 16:43:16,160 called and we can even check what values 19549 16:43:13,880 --> 16:43:17,720 this method received but I'll leave that 19550 16:43:16,160 --> 16:43:19,720 as an 19551 16:43:17,720 --> 16:43:22,520 exercise another thing you'll notice 19552 16:43:19,720 --> 16:43:23,956 here is that each test can have multiple 19553 16:43:22,520 --> 16:43:26,400 expectations 19554 16:43:23,956 --> 16:43:29,596 and the test will only pass if all of 19555 16:43:26,400 --> 16:43:31,916 its expectations 19556 16:43:29,596 --> 16:43:35,240 pass now let's add a few more 19557 16:43:31,916 --> 16:43:37,840 expectations to this test for practice 19558 16:43:35,240 --> 16:43:40,360 so one thing we can check is we can 19559 16:43:37,840 --> 16:43:42,796 check the first product in the cart and 19560 16:43:40,360 --> 16:43:46,756 make sure that its product ID matches 19561 16:43:42,796 --> 16:43:50,796 this string here so below this let's use 19562 16:43:46,756 --> 16:43:53,200 another expect brackets and we're going 19563 16:43:50,796 --> 16:43:56,560 to check the first product in the cart 19564 16:43:53,200 --> 16:44:00,476 using cart square bracket 19565 16:43:56,560 --> 16:44:03,476 0 so this is going to be an object and 19566 16:44:00,476 --> 16:44:07,360 this object is going to have a property 19567 16:44:03,476 --> 16:44:11,160 called product ID so that here we'll get 19568 16:44:07,360 --> 16:44:13,436 the dot product 19569 16:44:11,160 --> 16:44:16,916 ID and we're going to check if this 19570 16:44:13,436 --> 16:44:21,916 product ID is equal to this string so to 19571 16:44:16,916 --> 16:44:24,596 do that we'll use the dot 2 equal method 19572 16:44:21,916 --> 16:44:27,320 brackets and we'll give it this string 19573 16:44:24,596 --> 16:44:29,636 here so let's just select this and 19574 16:44:27,320 --> 16:44:34,520 rightclick 19575 16:44:29,636 --> 16:44:37,240 copy and here right click and 19576 16:44:34,520 --> 16:44:40,080 paste another thing that we can check is 19577 16:44:37,240 --> 16:44:42,320 the quantity when we add a new product 19578 16:44:40,080 --> 16:44:46,040 to the cart the quantity should start 19579 16:44:42,320 --> 16:44:47,720 out as one so down here we're going to 19580 16:44:46,040 --> 16:44:49,520 add another 19581 16:44:47,720 --> 16:44:52,116 expect 19582 16:44:49,520 --> 16:44:53,436 brackets and we're going to check the 19583 16:44:52,116 --> 16:44:55,276 first product 19584 16:44:53,436 --> 16:44:57,636 cart square bracket 19585 16:44:55,276 --> 16:45:04,596 0. 19586 16:44:57,636 --> 16:45:07,956 quantity. 2 equal brackets one and 19587 16:45:04,596 --> 16:45:10,400 save now let's open our test 19588 16:45:07,956 --> 16:45:13,320 page and you can see that all of those 19589 16:45:10,400 --> 16:45:16,040 expectations 19590 16:45:13,320 --> 16:45:18,476 passed now let's move on and create the 19591 16:45:16,040 --> 16:45:23,040 test for adding an existing product to 19592 16:45:18,476 --> 16:45:26,756 the cart so we'll go back to our code 19593 16:45:23,040 --> 16:45:29,916 and scroll up to the first test 19594 16:45:26,756 --> 16:45:32,360 here so first we need to set up the cart 19595 16:45:29,916 --> 16:45:35,116 so that it already contains the product 19596 16:45:32,360 --> 16:45:38,000 that we want to add to do that we're 19597 16:45:35,116 --> 16:45:41,360 going to Mark local storage. getet item 19598 16:45:38,000 --> 16:45:44,596 again so down here we can just reuse 19599 16:45:41,360 --> 16:45:47,080 this code so we're going to select this 19600 16:45:44,596 --> 16:45:49,276 code and right 19601 16:45:47,080 --> 16:45:53,720 click 19602 16:45:49,276 --> 16:45:57,360 copy and then in the first test here 19603 16:45:53,720 --> 16:45:57,360 rightclick and 19604 16:45:57,436 --> 16:46:02,680 paste and this time do set up the test 19605 16:46:00,720 --> 16:46:04,956 we're going to put a product into the 19606 16:46:02,680 --> 16:46:07,680 cart at the beginning and then we're 19607 16:46:04,956 --> 16:46:11,320 going to add the same product to the 19608 16:46:07,680 --> 16:46:12,200 cart so inside this cart array let's add 19609 16:46:11,320 --> 16:46:15,240 an 19610 16:46:12,200 --> 16:46:18,320 object and each product has a property 19611 16:46:15,240 --> 16:46:23,360 called product ID so we're going to add 19612 16:46:18,320 --> 16:46:25,756 this property product ID colon 19613 16:46:23,360 --> 16:46:29,000 and let's just give the same product ID 19614 16:46:25,756 --> 16:46:33,080 that we used down here so we'll select 19615 16:46:29,000 --> 16:46:36,360 this and then right click 19616 16:46:33,080 --> 16:46:38,796 copy and then up here we're going to 19617 16:46:36,360 --> 16:46:40,436 right click and 19618 16:46:38,796 --> 16:46:43,080 paste 19619 16:46:40,436 --> 16:46:46,436 comma and each product also has a 19620 16:46:43,080 --> 16:46:51,000 quantity so we'll give it a quantity 19621 16:46:46,436 --> 16:46:55,436 colon and let's just give it one comma 19622 16:46:51,000 --> 16:46:59,436 and also a delivery option so delivery 19623 16:46:55,436 --> 16:47:00,956 option ID colon and let's just give it 19624 16:46:59,436 --> 16:47:03,956 the string 19625 16:47:00,956 --> 16:47:06,840 one and 19626 16:47:03,956 --> 16:47:09,840 save so now when we do local storage. 19627 16:47:06,840 --> 16:47:12,680 getet item it will give back this array 19628 16:47:09,840 --> 16:47:16,160 as the starting cart and then we need to 19629 16:47:12,680 --> 16:47:20,476 make sure that we reload the cart from 19630 16:47:16,160 --> 16:47:23,000 Storage next let's scroll down and let's 19631 16:47:20,476 --> 16:47:25,276 copy the rest of the tests because it's 19632 16:47:23,000 --> 16:47:28,636 going to be very similar so we're going 19633 16:47:25,276 --> 16:47:30,680 to select this code and 19634 16:47:28,636 --> 16:47:34,400 rightclick 19635 16:47:30,680 --> 16:47:39,276 copy and then up here create some new 19636 16:47:34,400 --> 16:47:41,680 lines and right click and 19637 16:47:39,276 --> 16:47:45,436 paste and also one thing I forgot to 19638 16:47:41,680 --> 16:47:47,596 copy over is to mock local storage. set 19639 16:47:45,436 --> 16:47:49,756 item so at the 19640 16:47:47,596 --> 16:47:54,080 top let's 19641 16:47:49,756 --> 16:47:55,400 also spy on brackets 19642 16:47:54,080 --> 16:48:00,160 local 19643 16:47:55,400 --> 16:48:04,400 storage comma a string set 19644 16:48:00,160 --> 16:48:06,680 item so a mock only lasts for one test 19645 16:48:04,400 --> 16:48:09,436 once that test is finished the method is 19646 16:48:06,680 --> 16:48:12,756 no longer mocked so that's why in this 19647 16:48:09,436 --> 16:48:14,596 test we also have to mock local storage. 19648 16:48:12,756 --> 16:48:17,796 set 19649 16:48:14,596 --> 16:48:20,436 item and now in this test we're just 19650 16:48:17,796 --> 16:48:23,200 going to modify these expectations so 19651 16:48:20,436 --> 16:48:25,116 that they make sense for this test 19652 16:48:23,200 --> 16:48:28,636 if we add an existing product to the 19653 16:48:25,116 --> 16:48:31,476 cart the cart. length will still equal 19654 16:48:28,636 --> 16:48:33,400 one because these product IDs match and 19655 16:48:31,476 --> 16:48:35,840 we're just increasing the 19656 16:48:33,400 --> 16:48:38,436 quantity we're still going to call local 19657 16:48:35,840 --> 16:48:39,956 storage. set item once so this is 19658 16:48:38,436 --> 16:48:42,756 correct 19659 16:48:39,956 --> 16:48:45,476 still the first item in the cart is 19660 16:48:42,756 --> 16:48:47,796 going to have this product ID and the 19661 16:48:45,476 --> 16:48:50,436 only thing we're going to change is that 19662 16:48:47,796 --> 16:48:54,360 the quantity is now going to increase by 19663 16:48:50,436 --> 16:48:56,080 one so it's going to go from one to two 19664 16:48:54,360 --> 16:48:59,160 this 19665 16:48:56,080 --> 16:49:01,360 time so everything is the same except if 19666 16:48:59,160 --> 16:49:05,080 we add an existing product we're just 19667 16:49:01,360 --> 16:49:08,200 increasing the quantity now let's 19668 16:49:05,080 --> 16:49:12,040 save and check the test 19669 16:49:08,200 --> 16:49:13,160 page and now the existing product test 19670 16:49:12,040 --> 16:49:16,160 is 19671 16:49:13,160 --> 16:49:18,756 passing and that's it we created tests 19672 16:49:16,160 --> 16:49:21,596 for the add to cart 19673 16:49:18,756 --> 16:49:23,476 function so now let's do some cleanup 19674 16:49:21,596 --> 16:49:25,880 and we're actually going to remove 19675 16:49:23,476 --> 16:49:28,400 everything from local storage since we 19676 16:49:25,880 --> 16:49:30,880 were modifying it earlier in the test 19677 16:49:28,400 --> 16:49:32,756 and we might have messed it up so on 19678 16:49:30,880 --> 16:49:34,560 this page we're going to 19679 16:49:32,756 --> 16:49:38,000 rightclick 19680 16:49:34,560 --> 16:49:40,400 inspect and open the console and to 19681 16:49:38,000 --> 16:49:43,520 remove everything from local storage 19682 16:49:40,400 --> 16:49:45,720 we're going to type the code local 19683 16:49:43,520 --> 16:49:51,276 capital S 19684 16:49:45,720 --> 16:49:54,636 storage. clear and brackets and press 19685 16:49:51,276 --> 16:49:57,796 enter and then we'll refresh the 19686 16:49:54,636 --> 16:50:01,756 page now finally let's go into get and 19687 16:49:57,796 --> 16:50:04,040 save our changes so we'll open our code 19688 16:50:01,756 --> 16:50:07,596 and we can close this one for 19689 16:50:04,040 --> 16:50:12,320 now and we'll open the get 19690 16:50:07,596 --> 16:50:14,520 Section so in cart. JS at the top we 19691 16:50:12,320 --> 16:50:17,160 basically put all the code that loads 19692 16:50:14,520 --> 16:50:18,400 the cart into a function so we can use 19693 16:50:17,160 --> 16:50:21,560 it in our 19694 16:50:18,400 --> 16:50:26,320 tests and then here we moved money 19695 16:50:21,560 --> 16:50:31,160 test.js into a utils folder in test.html 19696 16:50:26,320 --> 16:50:34,476 we're loading the new cart test.js file 19697 16:50:31,160 --> 16:50:37,956 and then inside cart test.js we added 19698 16:50:34,476 --> 16:50:40,080 some tests for the add to cart function 19699 16:50:37,956 --> 16:50:41,116 so now let's create a message describing 19700 16:50:40,080 --> 16:50:42,636 these 19701 16:50:41,116 --> 16:50:44,240 changes 19702 16:50:42,636 --> 16:50:50,276 create 19703 16:50:44,240 --> 16:50:53,276 automated tests for add to cart and 19704 16:50:50,276 --> 16:50:53,276 commit 19705 16:50:56,956 --> 16:51:00,360 the last thing we're going to learn in 19706 16:50:58,200 --> 16:51:01,636 this lesson is how to create more 19707 16:51:00,360 --> 16:51:05,276 complicated 19708 16:51:01,636 --> 16:51:08,636 tests so far we've only been testing a 19709 16:51:05,276 --> 16:51:11,436 single function for example we tested 19710 16:51:08,636 --> 16:51:14,436 the add to card function as well as the 19711 16:51:11,436 --> 16:51:17,240 format currency function so these are 19712 16:51:14,436 --> 16:51:20,560 called Unit tests because we're only 19713 16:51:17,240 --> 16:51:22,956 testing one unit or one piece of the 19714 16:51:20,560 --> 16:51:26,400 code now what if we want to test t a 19715 16:51:22,956 --> 16:51:28,916 whole page or part of a page and this 19716 16:51:26,400 --> 16:51:30,560 requires a more complicated test called 19717 16:51:28,916 --> 16:51:34,080 an integration 19718 16:51:30,560 --> 16:51:38,400 test an integration test tests many 19719 16:51:34,080 --> 16:51:41,596 units of code or pieces of code working 19720 16:51:38,400 --> 16:51:44,636 together for example in our code we're 19721 16:51:41,596 --> 16:51:46,956 going to go to scripts checkout thee 19722 16:51:44,636 --> 16:51:49,720 orders summary. JS 19723 16:51:46,956 --> 16:51:52,916 file and we're going to test this render 19724 16:51:49,720 --> 16:51:55,320 order summary function so this function 19725 16:51:52,916 --> 16:51:57,560 creates a whole section on the checkout 19726 16:51:55,320 --> 16:51:59,956 page and it uses many different 19727 16:51:57,560 --> 16:52:02,720 functions and libraries to create the 19728 16:51:59,956 --> 16:52:05,436 page so testing this function will be 19729 16:52:02,720 --> 16:52:07,956 considered an integration test because 19730 16:52:05,436 --> 16:52:11,400 it uses many different pieces of code or 19731 16:52:07,956 --> 16:52:14,596 units of code together so let's start by 19732 16:52:11,400 --> 16:52:16,956 creating a test file for this function 19733 16:52:14,596 --> 16:52:21,080 so this is in the orders summary. JS 19734 16:52:16,956 --> 16:52:22,596 file inside our test here let's match 19735 16:52:21,080 --> 16:52:25,200 the file structure that we have here 19736 16:52:22,596 --> 16:52:27,680 here so we're going to click test- 19737 16:52:25,200 --> 16:52:30,116 Jasmin and first we're going to create a 19738 16:52:27,680 --> 16:52:31,360 checkout folder so we'll click here to 19739 16:52:30,116 --> 16:52:34,840 create a 19740 16:52:31,360 --> 16:52:37,320 folder and we're going to name it 19741 16:52:34,840 --> 16:52:39,720 checkout and press 19742 16:52:37,320 --> 16:52:42,476 enter and then inside the checkout 19743 16:52:39,720 --> 16:52:44,636 folder we're going to create a new file 19744 16:52:42,476 --> 16:52:48,720 so we'll click this to create a new 19745 16:52:44,636 --> 16:52:53,680 file and we'll name it the same as above 19746 16:52:48,720 --> 16:52:56,520 order summary and ADD test 19747 16:52:53,680 --> 16:52:59,680 .js and press 19748 16:52:56,520 --> 16:53:02,200 enter so by matching the file structure 19749 16:52:59,680 --> 16:53:04,720 it's clear that this test file is going 19750 16:53:02,200 --> 16:53:07,680 to test ORD summary. 19751 16:53:04,720 --> 16:53:10,596 JS and again for the video I'm just 19752 16:53:07,680 --> 16:53:14,880 going to drag this file to the top here 19753 16:53:10,596 --> 16:53:14,880 so we can see this and the test side by 19754 16:53:15,276 --> 16:53:20,200 side and now we're going to test this 19755 16:53:17,436 --> 16:53:22,360 render order summary function so first 19756 16:53:20,200 --> 16:53:26,756 we're going to import this function into 19757 16:53:22,360 --> 16:53:31,956 to our test file at the top we'll import 19758 16:53:26,756 --> 16:53:35,320 curly brackets the render order summary 19759 16:53:31,956 --> 16:53:37,756 function from a string and we're going 19760 16:53:35,320 --> 16:53:40,160 to locate this file so first we need to 19761 16:53:37,756 --> 16:53:42,880 get out of the checkout folder and out 19762 16:53:40,160 --> 16:53:47,000 of the test Jasmine folder so we'll use 19763 16:53:42,880 --> 16:53:48,360 two do do slash do do slash and then 19764 16:53:47,000 --> 16:53:51,320 we're going to go into the scripts 19765 16:53:48,360 --> 16:53:55,916 folder and into the checkout folder 19766 16:53:51,320 --> 16:53:59,680 Scripts slash checkout slash and we'll 19767 16:53:55,916 --> 16:53:59,680 load order 19768 16:54:07,840 --> 16:54:10,840 summary.jpg 19769 16:54:19,590 --> 16:54:23,276 [Music] 19770 16:54:20,756 --> 16:54:27,040 order summary 19771 16:54:23,276 --> 16:54:28,476 a comma and a function brackets arrow 19772 16:54:27,040 --> 16:54:31,636 and curler 19773 16:54:28,476 --> 16:54:35,436 brackets and now what test cases should 19774 16:54:31,636 --> 16:54:38,436 we create render order summary creates a 19775 16:54:35,436 --> 16:54:41,680 part of the page so when we're testing a 19776 16:54:38,436 --> 16:54:44,880 page the two things we need to test is 19777 16:54:41,680 --> 16:54:47,360 how the page looks and how the page 19778 16:54:44,880 --> 16:54:50,880 behaves let's start by testing how the 19779 16:54:47,360 --> 16:54:55,596 page looks inside our test Suite we're 19780 16:54:50,880 --> 16:54:57,956 going to create a test using it brackets 19781 16:54:55,596 --> 16:55:00,400 and we'll name this test with a 19782 16:54:57,956 --> 16:55:03,080 string and this will just be a basic 19783 16:55:00,400 --> 16:55:07,000 test case so we'll describe what this 19784 16:55:03,080 --> 16:55:09,200 function does which is 19785 16:55:07,000 --> 16:55:12,720 displays the 19786 16:55:09,200 --> 16:55:16,560 cart comma and we'll give it a function 19787 16:55:12,720 --> 16:55:19,360 brackets arrow and curly brackets and 19788 16:55:16,560 --> 16:55:22,436 now we're ready to create the 19789 16:55:19,360 --> 16:55:26,276 test so render order summary usually 19790 16:55:22,436 --> 16:55:29,400 displays the cart on the page however in 19791 16:55:26,276 --> 16:55:32,000 our tests where does this cart get 19792 16:55:29,400 --> 16:55:33,360 displayed so if we look at the code in 19793 16:55:32,000 --> 16:55:36,720 this 19794 16:55:33,360 --> 16:55:39,116 function and we scroll down here we 19795 16:55:36,720 --> 16:55:42,476 generate the HTML for the 19796 16:55:39,116 --> 16:55:46,840 cart and then after we generate the 19797 16:55:42,476 --> 16:55:50,360 HTML we put the HTML inside an element 19798 16:55:46,840 --> 16:55:53,000 with the class js- order- 19799 16:55:50,360 --> 16:55:56,040 summary however we don't have this 19800 16:55:53,000 --> 16:55:57,916 element in our tests so to fix this 19801 16:55:56,040 --> 16:56:00,840 we're just going to create an element 19802 16:55:57,916 --> 16:56:03,360 with the class js- order- 19803 16:56:00,840 --> 16:56:05,320 summary to do this we're going to scroll 19804 16:56:03,360 --> 16:56:07,436 down and we're going to open the 19805 16:56:05,320 --> 16:56:10,880 test.html 19806 16:56:07,436 --> 16:56:14,080 file and then at the bottom we're going 19807 16:56:10,880 --> 16:56:17,596 to go to the body and then inside here 19808 16:56:14,080 --> 16:56:19,240 we're going to create a div so less than 19809 16:56:17,596 --> 16:56:21,880 div and greater 19810 16:56:19,240 --> 16:56:23,756 than so remember how we said we need to 19811 16:56:21,880 --> 16:56:27,200 create create an element with the class 19812 16:56:23,756 --> 16:56:30,636 js- order- summary we're going to create 19813 16:56:27,200 --> 16:56:33,240 that element and put it inside this div 19814 16:56:30,636 --> 16:56:36,000 so this div is going to act like a 19815 16:56:33,240 --> 16:56:37,796 container the reason we use a container 19816 16:56:36,000 --> 16:56:40,040 is that when we create elements in our 19817 16:56:37,796 --> 16:56:42,160 test we don't want to be modifying the 19818 16:56:40,040 --> 16:56:44,956 entire body because the body might 19819 16:56:42,160 --> 16:56:46,840 contain our test results from jasine so 19820 16:56:44,956 --> 16:56:49,360 we put everything inside a 19821 16:56:46,840 --> 16:56:52,360 container so now let's give this div a 19822 16:56:49,360 --> 16:56:54,476 class so we can use it in our JavaScript 19823 16:56:52,360 --> 16:56:56,636 we're going to give it a class equals 19824 16:56:54,476 --> 16:57:00,916 double quotes and we're going to call 19825 16:56:56,636 --> 16:57:03,400 this the js- test- 19826 16:57:00,916 --> 16:57:05,756 container to make it clear this is a 19827 16:57:03,400 --> 16:57:08,796 container for all of our test 19828 16:57:05,756 --> 16:57:11,320 code and 19829 16:57:08,796 --> 16:57:14,720 save and now let's get this container 19830 16:57:11,320 --> 16:57:17,476 into our tests we can do that using 19831 16:57:14,720 --> 16:57:20,880 document. query 19832 16:57:17,476 --> 16:57:27,160 selector brackets and a string and we'll 19833 16:57:20,880 --> 16:57:30,116 select the class using using a js- test- 19834 16:57:27,160 --> 16:57:31,956 container and then inside this container 19835 16:57:30,116 --> 16:57:36,200 we're going to create an element with 19836 16:57:31,956 --> 16:57:38,320 the class js- order- summary so to put 19837 16:57:36,200 --> 16:57:42,080 something inside this element we're 19838 16:57:38,320 --> 16:57:43,240 going to use the property do inner 19839 16:57:42,080 --> 16:57:47,720 Capital 19840 16:57:43,240 --> 16:57:47,720 HTML equals a template 19841 16:57:49,956 --> 16:57:56,320 string and inside this template 19842 16:57:52,756 --> 16:57:59,720 let's create a div element less than div 19843 16:57:56,320 --> 16:58:02,320 greater than less than SL div greater 19844 16:57:59,720 --> 16:58:06,880 than and as we mentioned we'll give this 19845 16:58:02,320 --> 16:58:10,080 element a class equals double quotes of 19846 16:58:06,880 --> 16:58:12,796 js- order- 19847 16:58:10,080 --> 16:58:15,436 summary so this basically takes this 19848 16:58:12,796 --> 16:58:19,276 HTML and puts it inside our test 19849 16:58:15,436 --> 16:58:21,360 container so it will put it inside here 19850 16:58:19,276 --> 16:58:24,080 and now we have the element that we need 19851 16:58:21,360 --> 16:58:25,756 for render order summary so when we call 19852 16:58:24,080 --> 16:58:28,916 this function it's going to create the 19853 16:58:25,756 --> 16:58:32,520 cart and display it in this element 19854 16:58:28,916 --> 16:58:35,276 here next if we look at the code for 19855 16:58:32,520 --> 16:58:37,240 render order summary and we scroll to 19856 16:58:35,276 --> 16:58:40,116 the 19857 16:58:37,240 --> 16:58:43,116 top you'll notice that it takes the cart 19858 16:58:40,116 --> 16:58:45,880 and then displays it on the page but 19859 16:58:43,116 --> 16:58:48,636 remember that by default we load this 19860 16:58:45,880 --> 16:58:51,116 cart from local storage and this can 19861 16:58:48,636 --> 16:58:53,956 cause problems in our test depending on 19862 16:58:51,116 --> 16:58:56,520 what is inside local storage so to fix 19863 16:58:53,956 --> 16:58:59,320 this we're going to mock local storage. 19864 16:58:56,520 --> 16:59:02,320 getet item again to control exactly 19865 16:58:59,320 --> 16:59:05,520 what's inside this cart so let's 19866 16:59:02,320 --> 16:59:08,160 actually open the cart test that we had 19867 16:59:05,520 --> 16:59:10,916 before and we're just going to copy 19868 16:59:08,160 --> 16:59:13,756 paste this mock that we created so we're 19869 16:59:10,916 --> 16:59:16,436 going to start from here this function 19870 16:59:13,756 --> 16:59:19,840 and we're going to select the code up to 19871 16:59:16,436 --> 16:59:22,436 spy on get item and then 19872 16:59:19,840 --> 16:59:25,436 rightclick copy 19873 16:59:22,436 --> 16:59:28,276 and then down here we're going to 19874 16:59:25,436 --> 16:59:30,756 rightclick and 19875 16:59:28,276 --> 16:59:33,320 paste and one other thing we need to do 19876 16:59:30,756 --> 16:59:35,200 here is we don't have access to load 19877 16:59:33,320 --> 16:59:38,160 from Storage right now so we need to 19878 16:59:35,200 --> 16:59:41,276 import it at the 19879 16:59:38,160 --> 16:59:44,840 top let's also copy the import that we 19880 16:59:41,276 --> 16:59:47,320 had here so we're going to select this 19881 16:59:44,840 --> 16:59:50,116 and then right click 19882 16:59:47,320 --> 16:59:52,596 copy and then down here we're going to 19883 16:59:50,116 --> 16:59:54,916 rightclick and paste the 19884 16:59:52,596 --> 16:59:58,520 import and we just need to make sure 19885 16:59:54,916 --> 17:00:00,880 that this file path locates cart. JS so 19886 16:59:58,520 --> 17:00:03,880 do/ do slash it's going to go outside 19887 17:00:00,880 --> 17:00:06,636 checkout and outside test- Jasmin and 19888 17:00:03,880 --> 17:00:09,636 then it will go inside the data folder 19889 17:00:06,636 --> 17:00:12,360 and then locate card. JS so this file 19890 17:00:09,636 --> 17:00:14,916 path is correct and currently we're not 19891 17:00:12,360 --> 17:00:17,276 using these two values so we can just 19892 17:00:14,916 --> 17:00:17,276 remove 19893 17:00:19,476 --> 17:00:25,916 them and now let's set up this cart for 19894 17:00:22,756 --> 17:00:28,436 this test so we could just use this cart 19895 17:00:25,916 --> 17:00:33,436 here however to make our test a little 19896 17:00:28,436 --> 17:00:36,880 more detailed let's open the cart. JS 19897 17:00:33,436 --> 17:00:39,680 file and at the top here we're going to 19898 17:00:36,880 --> 17:00:41,240 use this default card instead because it 19899 17:00:39,680 --> 17:00:44,560 has two products with different 19900 17:00:41,240 --> 17:00:46,240 quantities so we can test more things so 19901 17:00:44,560 --> 17:00:49,796 we're just going to select this card 19902 17:00:46,240 --> 17:00:52,840 here and then right click 19903 17:00:49,796 --> 17:00:53,916 copy and then down here open order 19904 17:00:52,840 --> 17:00:57,400 summary 19905 17:00:53,916 --> 17:00:58,520 test and we're going to select this card 19906 17:00:57,400 --> 17:01:01,116 and remove 19907 17:00:58,520 --> 17:01:05,520 it and inside these brackets we're going 19908 17:01:01,116 --> 17:01:09,796 to right click and paste that default 19909 17:01:05,520 --> 17:01:12,240 cart and now that the HTML and the cart 19910 17:01:09,796 --> 17:01:16,320 is set up for this function at the 19911 17:01:12,240 --> 17:01:19,840 bottom we can call the function render 19912 17:01:16,320 --> 17:01:23,840 order summary and 19913 17:01:19,840 --> 17:01:23,840 brackets and Save 19914 17:01:25,116 --> 17:01:31,000 so now let's run this test file we're 19915 17:01:28,116 --> 17:01:34,200 going to open test.html 19916 17:01:31,000 --> 17:01:37,636 and then beneath these tests we're going 19917 17:01:34,200 --> 17:01:42,956 to add another script Tech less than 19918 17:01:37,636 --> 17:01:49,040 script Source equals and we'll load the 19919 17:01:42,956 --> 17:01:51,000 checkout slash order summary test.js 19920 17:01:49,040 --> 17:01:53,520 file and we need to give it the 19921 17:01:51,000 --> 17:01:57,680 attribute type Ty 19922 17:01:53,520 --> 17:02:00,840 equals module because we used 19923 17:01:57,680 --> 17:02:03,436 Imports and let's save so we run this 19924 17:02:00,840 --> 17:02:06,400 test so we'll open our test 19925 17:02:03,436 --> 17:02:10,000 page and you can see that the cart was 19926 17:02:06,400 --> 17:02:11,796 rendered onto this test page and we 19927 17:02:10,000 --> 17:02:14,756 still have our Jasmine results at the 19928 17:02:11,796 --> 17:02:18,276 bottom here so you notice that when we 19929 17:02:14,756 --> 17:02:21,320 render this it didn't load any CSS or 19930 17:02:18,276 --> 17:02:23,840 images and this is okay because we can't 19931 17:02:21,320 --> 17:02:27,116 really use code to check if the page 19932 17:02:23,840 --> 17:02:29,160 visually looks correct however we can 19933 17:02:27,116 --> 17:02:31,520 use this to check the contents of the 19934 17:02:29,160 --> 17:02:33,116 page like if the products and the 19935 17:02:31,520 --> 17:02:36,400 quantities are 19936 17:02:33,116 --> 17:02:38,956 correct so first our cart has two 19937 17:02:36,400 --> 17:02:41,360 products in it so we can check if there 19938 17:02:38,956 --> 17:02:45,560 were two products that were rendered on 19939 17:02:41,360 --> 17:02:48,360 the page let's go back to our 19940 17:02:45,560 --> 17:02:52,880 tests and let's take a look at the 19941 17:02:48,360 --> 17:02:52,880 orderers summary. JS file 19942 17:02:54,200 --> 17:03:00,400 and when we render the order summary we 19943 17:02:56,796 --> 17:03:02,880 scroll down for each product in the cart 19944 17:03:00,400 --> 17:03:04,400 we create an element with the class 19945 17:03:02,880 --> 17:03:06,560 cart-- 19946 17:03:04,400 --> 17:03:09,476 container since we have two products in 19947 17:03:06,560 --> 17:03:12,636 the cart let's check that we created two 19948 17:03:09,476 --> 17:03:17,116 of these elements on the page so we're 19949 17:03:12,636 --> 17:03:17,116 going to open the order summary test.js 19950 17:03:18,360 --> 17:03:24,520 file and then in the first test at the 19951 17:03:21,840 --> 17:03:27,080 bottom of this test we're going to add 19952 17:03:24,520 --> 17:03:29,880 some lines and we're going to check if 19953 17:03:27,080 --> 17:03:32,680 we have two of these elements to do that 19954 17:03:29,880 --> 17:03:38,160 we can use the Dom using 19955 17:03:32,680 --> 17:03:41,276 document. query selector all brackets 19956 17:03:38,160 --> 17:03:44,596 and a string and we could select the 19957 17:03:41,276 --> 17:03:47,720 class card- item D container however 19958 17:03:44,596 --> 17:03:48,916 this class is used for CSS so remember 19959 17:03:47,720 --> 17:03:53,240 if we're going to use a class in 19960 17:03:48,916 --> 17:03:55,200 JavaScript we start with js- 19961 17:03:53,240 --> 17:03:58,680 let's add a new class to this element 19962 17:03:55,200 --> 17:04:03,476 that we can use for the test we'll add 19963 17:03:58,680 --> 17:04:05,680 js- cart D item D 19964 17:04:03,476 --> 17:04:08,560 container so now we're going to select 19965 17:04:05,680 --> 17:04:14,636 all the elements with this class 19966 17:04:08,560 --> 17:04:17,320 using js- card- item D 19967 17:04:14,636 --> 17:04:20,400 container and let's save the ORD 19968 17:04:17,320 --> 17:04:20,400 summary. JS 19969 17:04:20,636 --> 17:04:25,720 file and then down here we're going to 19970 17:04:23,360 --> 17:04:28,400 check if we have two of these elements 19971 17:04:25,720 --> 17:04:30,360 on the page so again to check something 19972 17:04:28,400 --> 17:04:34,680 in our tests we're going to use the 19973 17:04:30,360 --> 17:04:37,000 function expect Open Bracket and let's 19974 17:04:34,680 --> 17:04:40,116 create a new line to make it a little 19975 17:04:37,000 --> 17:04:41,916 cleaner and at the end we'll give a Clos 19976 17:04:40,116 --> 17:04:45,476 bracket like 19977 17:04:41,916 --> 17:04:48,040 this and this code gives us an array of 19978 17:04:45,476 --> 17:04:52,080 elements so here we're going to check if 19979 17:04:48,040 --> 17:04:55,276 the dot length of this is equal to to 19980 17:04:52,080 --> 17:05:01,080 two so expect gives us an object which 19981 17:04:55,276 --> 17:05:04,756 has a method called dot 2 equal brackets 19982 17:05:01,080 --> 17:05:08,240 and two so this checks if we have two of 19983 17:05:04,756 --> 17:05:11,080 these elements on the page so let's 19984 17:05:08,240 --> 17:05:14,276 save and then check the test 19985 17:05:11,080 --> 17:05:16,160 page and scroll to the bottom and you'll 19986 17:05:14,276 --> 17:05:18,756 see that this test 19987 17:05:16,160 --> 17:05:22,880 passed so that's how we check if we 19988 17:05:18,756 --> 17:05:22,880 render the page correctly 19989 17:05:23,200 --> 17:05:28,240 next let's do another check of what we 19990 17:05:25,756 --> 17:05:30,520 rendered on the page so another thing we 19991 17:05:28,240 --> 17:05:33,680 can test is If the product quantities 19992 17:05:30,520 --> 17:05:37,000 are correct so first we need to get this 19993 17:05:33,680 --> 17:05:38,520 quantity element into our tests so we'll 19994 17:05:37,000 --> 17:05:42,240 go back to our 19995 17:05:38,520 --> 17:05:45,320 code and then inside the HTML of the 19996 17:05:42,240 --> 17:05:48,560 order summary we're going to scroll 19997 17:05:45,320 --> 17:05:51,040 down until we see a div with the class 19998 17:05:48,560 --> 17:05:53,000 product- quantity and we're displaying 19999 17:05:51,040 --> 17:05:55,276 the Quant 20000 17:05:53,000 --> 17:05:58,240 here so now we're going to take this 20001 17:05:55,276 --> 17:06:00,880 element put it into our test and check 20002 17:05:58,240 --> 17:06:04,000 that the quantity here is 20003 17:06:00,880 --> 17:06:06,116 correct so to do that let's give another 20004 17:06:04,000 --> 17:06:09,116 class to this element so we can select 20005 17:06:06,116 --> 17:06:11,636 it using JavaScript so here we'll add 20006 17:06:09,116 --> 17:06:13,476 another class 20007 17:06:11,636 --> 17:06:15,756 js- 20008 17:06:13,476 --> 17:06:17,720 product- 20009 17:06:15,756 --> 17:06:20,520 quantity and we want to check the 20010 17:06:17,720 --> 17:06:23,436 quantity for each product so we need to 20011 17:06:20,520 --> 17:06:25,596 identify which product this is for we 20012 17:06:23,436 --> 17:06:28,240 can do that by adding a dash to this 20013 17:06:25,596 --> 17:06:32,000 class and then inserting using dollar 20014 17:06:28,240 --> 17:06:36,040 and curly brackets the products ID which 20015 17:06:32,000 --> 17:06:40,476 is the matching product matching 20016 17:06:36,040 --> 17:06:43,320 product. ID and 20017 17:06:40,476 --> 17:06:47,796 save and now let's get this element into 20018 17:06:43,320 --> 17:06:50,436 our test so down here we're going to 20019 17:06:47,796 --> 17:06:52,400 document. query 20020 17:06:50,436 --> 17:06:54,680 selector 20021 17:06:52,400 --> 17:06:56,560 brackets and we'll give it a template 20022 17:06:54,680 --> 17:06:58,916 string because we're going to insert a 20023 17:06:56,560 --> 17:07:05,360 product ID so we're going to select this 20024 17:06:58,916 --> 17:07:09,116 class using a js- product- quantity Dash 20025 17:07:05,360 --> 17:07:11,560 and then insert the product ID so in our 20026 17:07:09,116 --> 17:07:14,596 cart we have two products and let's 20027 17:07:11,560 --> 17:07:17,000 start with the first product ID now to 20028 17:07:14,596 --> 17:07:19,840 make this easier let's put this string 20029 17:07:17,000 --> 17:07:22,840 inside a variable so we can reuse it 20030 17:07:19,840 --> 17:07:27,680 below so above the this let's create a 20031 17:07:22,840 --> 17:07:31,040 variable with const call it product 20032 17:07:27,680 --> 17:07:35,080 id1 equals and we're going to select 20033 17:07:31,040 --> 17:07:40,476 this ID and then right click 20034 17:07:35,080 --> 17:07:40,476 cut and then up here rightclick and 20035 17:07:40,680 --> 17:07:48,160 paste and then we're just going to use 20036 17:07:43,080 --> 17:07:50,720 this variable down here product 20037 17:07:48,160 --> 17:07:53,200 id1 and then when we're getting this 20038 17:07:50,720 --> 17:07:56,796 element into our our code we're also 20039 17:07:53,200 --> 17:07:59,756 going to insert the product ID which is 20040 17:07:56,796 --> 17:08:02,400 product 20041 17:07:59,756 --> 17:08:05,916 id1 but now we're going to check if the 20042 17:08:02,400 --> 17:08:09,080 text inside is correct so for the first 20043 17:08:05,916 --> 17:08:11,476 product we have a quantity of two so the 20044 17:08:09,080 --> 17:08:14,680 text inside this element should say 20045 17:08:11,476 --> 17:08:18,040 Quantity colon 20046 17:08:14,680 --> 17:08:20,636 2 so to check this at the bottom of our 20047 17:08:18,040 --> 17:08:21,956 test as usual we're going to use the 20048 17:08:20,636 --> 17:08:25,720 function 20049 17:08:21,956 --> 17:08:28,040 expect Open Bracket and a new line and 20050 17:08:25,720 --> 17:08:30,000 then here we can remove the semicolon 20051 17:08:28,040 --> 17:08:32,560 and close 20052 17:08:30,000 --> 17:08:34,916 bracket and we're going to get the text 20053 17:08:32,560 --> 17:08:38,520 inside this element now we're not going 20054 17:08:34,916 --> 17:08:41,320 to usein HTML because this element 20055 17:08:38,520 --> 17:08:44,840 contains a bunch of HTML inside but we 20056 17:08:41,320 --> 17:08:50,320 only want the text to get just the text 20057 17:08:44,840 --> 17:08:53,360 we're going to use the property do inner 20058 17:08:50,320 --> 17:08:56,636 text so we expect the text inside this 20059 17:08:53,360 --> 17:08:59,360 element to contain quantity colon 20060 17:08:56,636 --> 17:09:01,756 2 however this element also has some 20061 17:08:59,360 --> 17:09:04,080 other text like update and 20062 17:09:01,756 --> 17:09:06,880 delete so we just want to check if 20063 17:09:04,080 --> 17:09:10,200 quantity 2 is somewhere inside this 20064 17:09:06,880 --> 17:09:11,636 element so how do we check that to do 20065 17:09:10,200 --> 17:09:17,240 that we're going to use a different 20066 17:09:11,636 --> 17:09:19,636 method of expect called dot to 20067 17:09:17,240 --> 17:09:21,756 contain and 20068 17:09:19,636 --> 17:09:23,956 brackets and we're going to give it the 20069 17:09:21,756 --> 17:09:29,680 string that we want to check for which 20070 17:09:23,956 --> 17:09:33,436 is capital Q quantity colon 20071 17:09:29,680 --> 17:09:36,756 2 so the two contain method checks if 20072 17:09:33,436 --> 17:09:39,320 this value contains this string so they 20073 17:09:36,756 --> 17:09:40,520 don't have to be an exact match like 20074 17:09:39,320 --> 17:09:43,956 with two 20075 17:09:40,520 --> 17:09:46,436 equal as long as quantity colon 2 is 20076 17:09:43,956 --> 17:09:49,360 somewhere in this element then this 20077 17:09:46,436 --> 17:09:51,840 expectation will pass and this is 20078 17:09:49,360 --> 17:09:53,796 exactly what we need when this El has 20079 17:09:51,840 --> 17:09:55,160 some extra text that we don't really 20080 17:09:53,796 --> 17:09:58,276 need to 20081 17:09:55,160 --> 17:09:59,560 check so now let's click our test file 20082 17:09:58,276 --> 17:10:02,680 and 20083 17:09:59,560 --> 17:10:05,956 save and open the test 20084 17:10:02,680 --> 17:10:08,796 page and scroll to the bottom and you'll 20085 17:10:05,956 --> 17:10:10,756 notice that the test passed so for the 20086 17:10:08,796 --> 17:10:13,636 first product we're displaying the 20087 17:10:10,756 --> 17:10:16,200 quantity of two 20088 17:10:13,636 --> 17:10:18,840 correctly let's also check the quantity 20089 17:10:16,200 --> 17:10:20,080 of the second product we'll go back to 20090 17:10:18,840 --> 17:10:23,560 our 20091 17:10:20,080 --> 17:10:27,560 tests and and just like before let's 20092 17:10:23,560 --> 17:10:30,840 create a variable to store this ID so 20093 17:10:27,560 --> 17:10:32,476 we'll create a variable with const 20094 17:10:30,840 --> 17:10:36,916 product 20095 17:10:32,476 --> 17:10:39,916 id2 equals and we'll select this 20096 17:10:36,916 --> 17:10:44,796 ID and right click 20097 17:10:39,916 --> 17:10:47,520 copy and then here right click and 20098 17:10:44,796 --> 17:10:53,240 paste and then we'll remove 20099 17:10:47,520 --> 17:10:53,240 this and use the variable product 20100 17:10:53,596 --> 17:10:58,360 id2 and then at the bottom we're going 20101 17:10:56,116 --> 17:11:01,400 to get the quantity element for Product 20102 17:10:58,360 --> 17:11:04,596 2 so let's just copy this code because 20103 17:11:01,400 --> 17:11:07,880 it's very similar we'll select it right 20104 17:11:04,596 --> 17:11:11,200 click copy and then down 20105 17:11:07,880 --> 17:11:13,596 here right click and 20106 17:11:11,200 --> 17:11:17,320 paste so this time we're going to get 20107 17:11:13,596 --> 17:11:20,796 the quantity element for product ID 20108 17:11:17,320 --> 17:11:24,436 2 and in our cart product two has a 20109 17:11:20,796 --> 17:11:27,360 quantity of one so down here we're going 20110 17:11:24,436 --> 17:11:31,040 to check if the text contains quantity 20111 17:11:27,360 --> 17:11:33,560 colon 1 and 20112 17:11:31,040 --> 17:11:36,000 save and let's open our test 20113 17:11:33,560 --> 17:11:39,436 page and scroll to the 20114 17:11:36,000 --> 17:11:41,720 bottom and this test passed so we're 20115 17:11:39,436 --> 17:11:44,276 displaying the quantity of both products 20116 17:11:41,720 --> 17:11:47,040 correctly on the 20117 17:11:44,276 --> 17:11:50,880 page the second thing we can test in an 20118 17:11:47,040 --> 17:11:53,796 integration test is how the page behaves 20119 17:11:50,880 --> 17:11:56,720 for for example on this page we can 20120 17:11:53,796 --> 17:11:59,476 click the delete link to remove a 20121 17:11:56,720 --> 17:12:01,756 product from the page so let's create a 20122 17:11:59,476 --> 17:12:03,596 new test to make sure that the delete 20123 17:12:01,756 --> 17:12:06,636 link behaves 20124 17:12:03,596 --> 17:12:09,840 correctly we'll open our test 20125 17:12:06,636 --> 17:12:13,636 code and at the bottom we're going to 20126 17:12:09,840 --> 17:12:14,680 create a new test using the function it 20127 17:12:13,636 --> 17:12:18,436 and 20128 17:12:14,680 --> 17:12:21,240 brackets we'll give it a name and let's 20129 17:12:18,436 --> 17:12:24,956 describe what the behavior should be 20130 17:12:21,240 --> 17:12:29,116 which is removes a 20131 17:12:24,956 --> 17:12:32,240 product comma and a function brackets 20132 17:12:29,116 --> 17:12:34,796 arrow and curler 20133 17:12:32,240 --> 17:12:37,320 brackets now let's do the same setup as 20134 17:12:34,796 --> 17:12:40,880 the first test so we start with two 20135 17:12:37,320 --> 17:12:43,840 products in the cart so we can select 20136 17:12:40,880 --> 17:12:47,200 from load from storage and we'll select 20137 17:12:43,840 --> 17:12:49,200 all of this code up here and then 20138 17:12:47,200 --> 17:12:52,400 rightclick 20139 17:12:49,200 --> 17:12:56,040 copy and at the bottom 20140 17:12:52,400 --> 17:12:59,276 we're going to right click and 20141 17:12:56,040 --> 17:13:01,436 paste so this will set up the HTML 20142 17:12:59,276 --> 17:13:03,636 element that we need for the order 20143 17:13:01,436 --> 17:13:07,200 summary as well as the 20144 17:13:03,636 --> 17:13:11,320 cart and after this let's render the 20145 17:13:07,200 --> 17:13:16,040 order summary using the function render 20146 17:13:11,320 --> 17:13:16,040 order summary and 20147 17:13:16,160 --> 17:13:20,756 brackets so once we've displayed this on 20148 17:13:18,560 --> 17:13:24,160 the page we're going to get the delete 20149 17:13:20,756 --> 17:13:27,160 link and click it so first let's get the 20150 17:13:24,160 --> 17:13:30,796 delete link into our code so we'll go to 20151 17:13:27,160 --> 17:13:33,240 the orders summary. JS file and inside 20152 17:13:30,796 --> 17:13:37,000 the HTML we're going to scroll down a 20153 17:13:33,240 --> 17:13:39,560 bit till we see the delete quantity link 20154 17:13:37,000 --> 17:13:41,160 and let's test the delete link for the 20155 17:13:39,560 --> 17:13:44,596 first product in the 20156 17:13:41,160 --> 17:13:47,000 cart so first we need to add a class to 20157 17:13:44,596 --> 17:13:50,080 this element so we know which product 20158 17:13:47,000 --> 17:13:53,720 this is for so here we're going to add a 20159 17:13:50,080 --> 17:13:58,200 class JF s- delete 20160 17:13:53,720 --> 17:14:01,240 dink Dash and insert the product ID 20161 17:13:58,200 --> 17:14:01,240 which is the matching 20162 17:14:01,476 --> 17:14:06,080 product 20163 17:14:03,476 --> 17:14:09,680 doid and I'll add a new line to make the 20164 17:14:06,080 --> 17:14:12,240 code a little bit cleaner and now we can 20165 17:14:09,680 --> 17:14:14,320 save and then down here we're going to 20166 17:14:12,240 --> 17:14:19,320 get the delete link for the first 20167 17:14:14,320 --> 17:14:21,916 product ID so at the bottom we use 20168 17:14:19,320 --> 17:14:23,596 document do query 20169 17:14:21,916 --> 17:14:26,040 selector 20170 17:14:23,596 --> 17:14:28,116 brackets and we'll give it a template 20171 17:14:26,040 --> 17:14:31,840 string because we're going to insert the 20172 17:14:28,116 --> 17:14:35,636 product ID and we'll select this class 20173 17:14:31,840 --> 17:14:40,080 using js- delete 20174 17:14:35,636 --> 17:14:44,520 dlink D and then insert dollar curly 20175 17:14:40,080 --> 17:14:47,680 brackets product 20176 17:14:44,520 --> 17:14:50,320 id1 and now we need to click this delete 20177 17:14:47,680 --> 17:14:53,720 link to remove the first product from 20178 17:14:50,320 --> 17:14:56,080 the page and from the cart to click an 20179 17:14:53,720 --> 17:14:59,200 element using Code we can just get it 20180 17:14:56,080 --> 17:15:02,200 using the Dom and then use the method 20181 17:14:59,200 --> 17:15:04,796 dot click and 20182 17:15:02,200 --> 17:15:08,720 brackets so this will click delete on 20183 17:15:04,796 --> 17:15:12,680 the first product and remove it so let's 20184 17:15:08,720 --> 17:15:16,720 save and take a look at our test 20185 17:15:12,680 --> 17:15:18,596 page and unfortunately this test failed 20186 17:15:16,720 --> 17:15:22,160 so let's take a look at the details of 20187 17:15:18,596 --> 17:15:24,116 the failure to figure out how to fix it 20188 17:15:22,160 --> 17:15:27,000 so it's telling us that we cannot set 20189 17:15:24,116 --> 17:15:29,320 properties of null this means that a 20190 17:15:27,000 --> 17:15:32,400 value in our code is null and we're 20191 17:15:29,320 --> 17:15:32,400 trying to give it a 20192 17:15:32,476 --> 17:15:38,240 property and here it will tell us which 20193 17:15:35,000 --> 17:15:41,080 line we got this error from so this 20194 17:15:38,240 --> 17:15:43,436 error happened in our test file and the 20195 17:15:41,080 --> 17:15:45,916 line that created this error was in Ord 20196 17:15:43,436 --> 17:15:48,840 summary. js on line 20197 17:15:45,916 --> 17:15:51,436 132 or whatever line that you see here 20198 17:15:48,840 --> 17:15:54,116 on your page 20199 17:15:51,436 --> 17:15:55,796 so inside our code we're going to open 20200 17:15:54,116 --> 17:15:59,116 orders summary. 20201 17:15:55,796 --> 17:16:01,560 JS and I'm going to scroll down to line 20202 17:15:59,116 --> 17:16:03,796 132 and you can scroll down to the line 20203 17:16:01,560 --> 17:16:07,796 that you got on your 20204 17:16:03,796 --> 17:16:10,436 page so this line of code is actually 20205 17:16:07,796 --> 17:16:13,040 part of the code that we run when we 20206 17:16:10,436 --> 17:16:16,000 click delete so remember that when we 20207 17:16:13,040 --> 17:16:18,680 click delete we also update the payment 20208 17:16:16,000 --> 17:16:21,240 summary on the right side of the page 20209 17:16:18,680 --> 17:16:24,240 and that's what this line does now the 20210 17:16:21,240 --> 17:16:27,360 reason this is not working is if we open 20211 17:16:24,240 --> 17:16:30,596 the payment summary JS file and we look 20212 17:16:27,360 --> 17:16:34,436 at what this does at the bottom after we 20213 17:16:30,596 --> 17:16:38,040 generate the HTML we try to put the HTML 20214 17:16:34,436 --> 17:16:41,000 in an element with a class js- payment D 20215 17:16:38,040 --> 17:16:44,320 summary and this element does not exist 20216 17:16:41,000 --> 17:16:48,240 in our tests so one way to fix this is 20217 17:16:44,320 --> 17:16:48,240 to add this element to our 20218 17:16:48,276 --> 17:16:54,436 test so let's open our test again 20219 17:16:51,916 --> 17:16:56,880 we'll open the order summary 20220 17:16:54,436 --> 17:16:59,956 test.js and then we'll scroll 20221 17:16:56,880 --> 17:17:02,080 up and then here when we create the 20222 17:16:59,956 --> 17:17:04,680 order summary element we're also going 20223 17:17:02,080 --> 17:17:08,080 to create the payment summary 20224 17:17:04,680 --> 17:17:11,116 element so we'll create a less than div 20225 17:17:08,080 --> 17:17:14,880 greater than less than SL div greater 20226 17:17:11,116 --> 17:17:18,040 than and here we'll give it a class 20227 17:17:14,880 --> 17:17:20,360 equals double quotes and we'll give the 20228 17:17:18,040 --> 17:17:21,520 class that the payment summary needs 20229 17:17:20,360 --> 17:17:24,720 which is 20230 17:17:21,520 --> 17:17:28,200 js- payment D 20231 17:17:24,720 --> 17:17:30,636 summary and 20232 17:17:28,200 --> 17:17:33,636 save so now that the payment summary 20233 17:17:30,636 --> 17:17:36,436 element is set up we can open our test 20234 17:17:33,636 --> 17:17:39,080 page and our test should no longer have 20235 17:17:36,436 --> 17:17:40,956 a failure so you also notice that the 20236 17:17:39,080 --> 17:17:43,200 payment summary got rendered or 20237 17:17:40,956 --> 17:17:46,476 displayed on the 20238 17:17:43,200 --> 17:17:48,636 page okay so in our tests we only 20239 17:17:46,476 --> 17:17:50,916 clicked the delete link for the first 20240 17:17:48,636 --> 17:17:52,476 product but we didn't actually check if 20241 17:17:50,916 --> 17:17:54,476 the page looks 20242 17:17:52,476 --> 17:17:57,160 correct and that's what it shows here 20243 17:17:54,476 --> 17:17:59,560 with this yellow dot this test has no 20244 17:17:57,160 --> 17:18:02,916 expectations 20245 17:17:59,560 --> 17:18:05,956 yet so let's go back to our test and add 20246 17:18:02,916 --> 17:18:09,240 some expectations at the bottom so we'll 20247 17:18:05,956 --> 17:18:12,200 scroll down and down 20248 17:18:09,240 --> 17:18:14,840 here after we click delete for the first 20249 17:18:12,200 --> 17:18:17,756 product we expect that there's only one 20250 17:18:14,840 --> 17:18:20,880 element left on the page for the 20251 17:18:17,756 --> 17:18:24,520 cart so in our first test we actually 20252 17:18:20,880 --> 17:18:27,840 had a check for this so we can scroll up 20253 17:18:24,520 --> 17:18:30,840 and here we check if we have two cart 20254 17:18:27,840 --> 17:18:32,880 item containers on the page so let's 20255 17:18:30,840 --> 17:18:37,200 just reuse this check because it's very 20256 17:18:32,880 --> 17:18:39,720 similar we'll select it right click 20257 17:18:37,200 --> 17:18:44,240 copy and then down 20258 17:18:39,720 --> 17:18:47,476 here we're going to right click and 20259 17:18:44,240 --> 17:18:49,840 paste and this time we only expect one 20260 17:18:47,476 --> 17:18:53,116 cart item container to be left on the 20261 17:18:49,840 --> 17:18:54,880 page so so we'll change this to one so 20262 17:18:53,116 --> 17:18:58,840 we'll 20263 17:18:54,880 --> 17:19:01,756 save open the test page scroll down and 20264 17:18:58,840 --> 17:19:01,756 now this test 20265 17:19:02,560 --> 17:19:07,200 passed let's also do a more detailed 20266 17:19:05,116 --> 17:19:11,160 check so we're going to check that the 20267 17:19:07,200 --> 17:19:13,240 first product is no longer on the page 20268 17:19:11,160 --> 17:19:16,520 we'll go back to our 20269 17:19:13,240 --> 17:19:18,880 tests and then down here we're going to 20270 17:19:16,520 --> 17:19:21,520 get the card item container for the 20271 17:19:18,880 --> 17:19:25,360 first product we use 20272 17:19:21,520 --> 17:19:27,840 document. query selector 20273 17:19:25,360 --> 17:19:30,520 brackets and to get the element for the 20274 17:19:27,840 --> 17:19:35,756 first product we're going to open orders 20275 17:19:30,520 --> 17:19:39,080 summary. JS and then scroll up in the 20276 17:19:35,756 --> 17:19:40,200 HTML until we find the cart item 20277 17:19:39,080 --> 17:19:42,276 container 20278 17:19:40,200 --> 17:19:44,476 element and we're going to look for this 20279 17:19:42,276 --> 17:19:48,320 class right here which tells us which 20280 17:19:44,476 --> 17:19:50,240 product this element is for so in here 20281 17:19:48,320 --> 17:19:52,796 we'll give it a template string because 20282 17:19:50,240 --> 17:19:57,160 we'll in insert the product ID and we'll 20283 17:19:52,796 --> 17:20:03,116 look for this class using a 20284 17:19:57,160 --> 17:20:08,200 js-- item Das container Dash and insert 20285 17:20:03,116 --> 17:20:08,200 the first product so product 20286 17:20:09,436 --> 17:20:15,400 id1 and because we removed this product 20287 17:20:12,560 --> 17:20:20,116 from the page we expect this result to 20288 17:20:15,400 --> 17:20:23,360 be null so we'll use the function expect 20289 17:20:20,116 --> 17:20:26,560 open bracket and a new line and then 20290 17:20:23,360 --> 17:20:28,116 down here a close bracket and we'll use 20291 17:20:26,560 --> 17:20:32,756 the method 20292 17:20:28,116 --> 17:20:36,916 do2 equal brackets and we expect this to 20293 17:20:32,756 --> 17:20:39,596 be null and 20294 17:20:36,916 --> 17:20:42,040 save let's also check that the second 20295 17:20:39,596 --> 17:20:43,756 product is still on the page because we 20296 17:20:42,040 --> 17:20:46,436 didn't delete 20297 17:20:43,756 --> 17:20:51,116 it so let's just make a copy of this 20298 17:20:46,436 --> 17:20:52,160 check we'll select it right click copy 20299 17:20:51,116 --> 17:20:56,596 and then 20300 17:20:52,160 --> 17:20:56,596 here right click and 20301 17:20:56,680 --> 17:21:02,916 paste and we'll get the element for 20302 17:20:59,276 --> 17:21:05,276 product ID 2 this time and we want to 20303 17:21:02,916 --> 17:21:08,756 make sure that this exists so that it is 20304 17:21:05,276 --> 17:21:10,360 not equal to null in Jasmine to check if 20305 17:21:08,756 --> 17:21:13,400 something is not equal 20306 17:21:10,360 --> 17:21:17,160 to expect gives us another property we 20307 17:21:13,400 --> 17:21:20,956 can use called dot 20308 17:21:17,160 --> 17:21:24,000 not so if we use theot property it will 20309 17:21:20,956 --> 17:21:27,000 check the opposite of whatever is next 20310 17:21:24,000 --> 17:21:30,276 so this will check if this value is not 20311 17:21:27,000 --> 17:21:33,756 equal to null so remember the code for 20312 17:21:30,276 --> 17:21:37,916 Jasmine is designed to read like 20313 17:21:33,756 --> 17:21:40,276 English let's save and check the test 20314 17:21:37,916 --> 17:21:43,116 page and scroll 20315 17:21:40,276 --> 17:21:45,200 down and this test passed so we're 20316 17:21:43,116 --> 17:21:48,040 displaying the correct products on the 20317 17:21:45,200 --> 17:21:48,040 page after 20318 17:21:48,240 --> 17:21:52,596 deleting now there's one more thing that 20319 17:21:50,200 --> 17:21:56,080 we need to check here which is after 20320 17:21:52,596 --> 17:21:58,756 deleting is the cart array itself 20321 17:21:56,080 --> 17:21:59,796 updated so we'll go back to our code and 20322 17:21:58,756 --> 17:22:02,520 add that 20323 17:21:59,796 --> 17:22:05,240 check so first we need to import the 20324 17:22:02,520 --> 17:22:06,756 cart so we can use it in this test we're 20325 17:22:05,240 --> 17:22:09,240 going to scroll up to the 20326 17:22:06,756 --> 17:22:13,200 top and we're already importing from 20327 17:22:09,240 --> 17:22:14,360 cart. JS so we'll add a comma and import 20328 17:22:13,200 --> 17:22:17,160 the 20329 17:22:14,360 --> 17:22:19,400 cart and then at the 20330 17:22:17,160 --> 17:22:22,436 bottom we're going to check if the card 20331 17:22:19,400 --> 17:22:22,436 is correct 20332 17:22:22,796 --> 17:22:27,880 so the cart started out with two 20333 17:22:24,796 --> 17:22:30,680 products inside after removing the first 20334 17:22:27,880 --> 17:22:33,400 product the card should have a length of 20335 17:22:30,680 --> 17:22:36,680 one and the remaining product should 20336 17:22:33,400 --> 17:22:39,200 have product id2 so let's add both of 20337 17:22:36,680 --> 17:22:41,756 these checks at the bottom so we'll 20338 17:22:39,200 --> 17:22:41,756 scroll back 20339 17:22:42,200 --> 17:22:51,756 down and we'll use expect 20340 17:22:46,200 --> 17:22:55,840 brackets and we'll check if cart. length 20341 17:22:51,756 --> 17:23:00,276 do 2 equal brackets 20342 17:22:55,840 --> 17:23:02,000 1 and we'll also expect brackets and 20343 17:23:00,276 --> 17:23:06,400 we'll check the first product in the 20344 17:23:02,000 --> 17:23:13,000 cart which is cart square bracket 0 and 20345 17:23:06,400 --> 17:23:17,040 we'll check its product ID do2 20346 17:23:13,000 --> 17:23:21,000 equal and it should equal product ID 2 20347 17:23:17,040 --> 17:23:22,040 so product id2 20348 17:23:21,000 --> 17:23:24,636 and 20349 17:23:22,040 --> 17:23:28,360 save so let's open our test 20350 17:23:24,636 --> 17:23:30,956 page and scroll to the bottom and this 20351 17:23:28,360 --> 17:23:34,200 test still passed so once we click 20352 17:23:30,956 --> 17:23:36,680 delete the card array is also 20353 17:23:34,200 --> 17:23:39,720 correct so that's how we create an 20354 17:23:36,680 --> 17:23:43,476 integration test it tests many units of 20355 17:23:39,720 --> 17:23:46,596 code or pieces of code working together 20356 17:23:43,476 --> 17:23:50,636 and it lets us test how our page looks 20357 17:23:46,596 --> 17:23:50,636 and how our page behaves 20358 17:23:51,000 --> 17:23:55,560 now there's one thing that I forgot to 20359 17:23:52,476 --> 17:23:56,756 do when we created these tests if we 20360 17:23:55,560 --> 17:23:59,436 look at our 20361 17:23:56,756 --> 17:24:01,116 code and I'm going to open orders 20362 17:23:59,436 --> 17:24:06,040 summary. 20363 17:24:01,116 --> 17:24:09,276 JS when we click delete at the bottom 20364 17:24:06,040 --> 17:24:10,240 here it uses a function called remove 20365 17:24:09,276 --> 17:24:13,560 from 20366 17:24:10,240 --> 17:24:16,160 cart and if I look at the code for this 20367 17:24:13,560 --> 17:24:18,636 function which is right here remove from 20368 17:24:16,160 --> 17:24:20,840 cart this function actually saves the 20369 17:24:18,636 --> 17:24:23,116 cart to local storage and and calls 20370 17:24:20,840 --> 17:24:26,240 local storage. set 20371 17:24:23,116 --> 17:24:29,796 item however it's recommended not to 20372 17:24:26,240 --> 17:24:32,956 modify local storage in our test so 20373 17:24:29,796 --> 17:24:36,360 inside the order summary test file we 20374 17:24:32,956 --> 17:24:40,200 should also mock local storage. set item 20375 17:24:36,360 --> 17:24:43,240 in this test so we'll scroll to the 20376 17:24:40,200 --> 17:24:48,360 top and at the start of the test let's 20377 17:24:43,240 --> 17:24:51,160 mock set item using spy on 20378 17:24:48,360 --> 17:24:55,880 brackets local 20379 17:24:51,160 --> 17:24:58,160 storage comma the string set 20380 17:24:55,880 --> 17:25:01,756 item and 20381 17:24:58,160 --> 17:25:04,360 save and we'll open the test page and 20382 17:25:01,756 --> 17:25:05,756 make sure that the test still passes and 20383 17:25:04,360 --> 17:25:08,880 we might have messed up our local 20384 17:25:05,756 --> 17:25:11,720 storage because of that mistake so let's 20385 17:25:08,880 --> 17:25:14,360 reset local storage again we'll right 20386 17:25:11,720 --> 17:25:17,520 click on this page 20387 17:25:14,360 --> 17:25:21,880 inspect open the 20388 17:25:17,520 --> 17:25:26,276 console and we'll type the code local 20389 17:25:21,880 --> 17:25:30,476 storage. CLE brackets and press 20390 17:25:26,276 --> 17:25:32,636 enter and refresh the page just to be 20391 17:25:30,476 --> 17:25:35,320 safe now another thing that we might 20392 17:25:32,636 --> 17:25:37,680 want to add is that when we render the 20393 17:25:35,320 --> 17:25:39,756 order summary on the page it's taking up 20394 17:25:37,680 --> 17:25:42,436 a lot of space and it pushes the test 20395 17:25:39,756 --> 17:25:45,160 results to the bottom so what we can do 20396 17:25:42,436 --> 17:25:46,400 is after our test we can actually remove 20397 17:25:45,160 --> 17:25:50,520 all of this 20398 17:25:46,400 --> 17:25:53,040 HTML so let's go back to our code 20399 17:25:50,520 --> 17:25:55,476 in the order summary test file at the 20400 17:25:53,040 --> 17:25:58,636 end of the first test we'll create some 20401 17:25:55,476 --> 17:26:02,796 new lines and remove the HTML that we 20402 17:25:58,636 --> 17:26:06,636 created to do that we can scroll up to 20403 17:26:02,796 --> 17:26:09,956 this code so to remove the HTML we can 20404 17:26:06,636 --> 17:26:13,596 just set this element inner HTML to the 20405 17:26:09,956 --> 17:26:15,720 empty string so we'll select this code 20406 17:26:13,596 --> 17:26:20,200 right click 20407 17:26:15,720 --> 17:26:21,956 copy and then down here right click and 20408 17:26:20,200 --> 17:26:25,240 and 20409 17:26:21,956 --> 17:26:28,160 paste so in our test all the HTML we 20410 17:26:25,240 --> 17:26:30,360 created was placed inside this test 20411 17:26:28,160 --> 17:26:31,796 container and we're just going to remove 20412 17:26:30,360 --> 17:26:35,200 all that 20413 17:26:31,796 --> 17:26:38,040 HTML by setting the inner HTML to the 20414 17:26:35,200 --> 17:26:40,680 empty string and we'll do the same thing 20415 17:26:38,040 --> 17:26:43,436 for the second test so we'll select this 20416 17:26:40,680 --> 17:26:47,320 code rightclick 20417 17:26:43,436 --> 17:26:50,720 copy and then down here at the 20418 17:26:47,320 --> 17:26:53,160 bottom we'll right click and 20419 17:26:50,720 --> 17:26:56,720 paste so this is just some cleanup we 20420 17:26:53,160 --> 17:26:59,436 can do at the end of each test let's 20421 17:26:56,720 --> 17:27:02,240 save and look at our test 20422 17:26:59,436 --> 17:27:04,756 page and now all the HTML that we 20423 17:27:02,240 --> 17:27:07,796 generated is gone and we can see the 20424 17:27:04,756 --> 17:27:07,796 test results more 20425 17:27:08,520 --> 17:27:14,240 clearly lastly we're going to learn a 20426 17:27:11,000 --> 17:27:18,200 shortcut we can use in Jasmine called 20427 17:27:14,240 --> 17:27:21,276 hooks a hook lets us run some code for 20428 17:27:18,200 --> 17:27:24,200 each test for example 20429 17:27:21,276 --> 17:27:26,680 if we go back to our code notice that 20430 17:27:24,200 --> 17:27:29,916 before each of our tests we do a bunch 20431 17:27:26,680 --> 17:27:32,956 of setup here so we can actually share 20432 17:27:29,916 --> 17:27:36,596 all of this code between our two tests 20433 17:27:32,956 --> 17:27:40,320 using a hook so to create a hook we're 20434 17:27:36,596 --> 17:27:41,160 going to scroll up to outside of both of 20435 17:27:40,320 --> 17:27:44,680 these 20436 17:27:41,160 --> 17:27:46,436 tests and at the top of the test Suite 20437 17:27:44,680 --> 17:27:50,360 we're going to use another function 20438 17:27:46,436 --> 17:27:52,040 provided by Jasmine called before 20439 17:27:50,360 --> 17:27:56,160 capital E 20440 17:27:52,040 --> 17:28:00,360 each brackets and we'll give this a 20441 17:27:56,160 --> 17:28:04,080 function brackets arrow and curler 20442 17:28:00,360 --> 17:28:07,400 brackets so this is called a before each 20443 17:28:04,080 --> 17:28:09,240 hook and it will run this function 20444 17:28:07,400 --> 17:28:12,240 before each of our 20445 17:28:09,240 --> 17:28:14,796 tests so inside this function we can put 20446 17:28:12,240 --> 17:28:17,000 our setup code so that we run the setup 20447 17:28:14,796 --> 17:28:19,560 code before each of our 20448 17:28:17,000 --> 17:28:22,040 tests let's actually take the setup code 20449 17:28:19,560 --> 17:28:23,240 for the second test because it's a 20450 17:28:22,040 --> 17:28:25,880 little more 20451 17:28:23,240 --> 17:28:27,796 detailed so down here we're going to 20452 17:28:25,880 --> 17:28:31,040 select the code starting from render 20453 17:28:27,796 --> 17:28:34,276 order summary all the way up to mocking 20454 17:28:31,040 --> 17:28:37,320 local storage. set item and we'll 20455 17:28:34,276 --> 17:28:40,680 rightclick and cut this 20456 17:28:37,320 --> 17:28:44,436 code and remove these new 20457 17:28:40,680 --> 17:28:50,200 lines and then at the top inside before 20458 17:28:44,436 --> 17:28:52,636 each we're going to rightclick and paste 20459 17:28:50,200 --> 17:28:54,400 so now before each of our tests it's 20460 17:28:52,636 --> 17:28:57,520 going to run this function and it's 20461 17:28:54,400 --> 17:28:59,680 going to run all of our setup code so 20462 17:28:57,520 --> 17:29:02,400 this is a great way to share code 20463 17:28:59,680 --> 17:29:04,476 between our tests and remove 20464 17:29:02,400 --> 17:29:07,360 duplication so now that we're running 20465 17:29:04,476 --> 17:29:09,680 this inside before each if we scroll 20466 17:29:07,360 --> 17:29:12,240 down to the first test we don't have to 20467 17:29:09,680 --> 17:29:13,560 run this code again so we can actually 20468 17:29:12,240 --> 17:29:18,116 remove 20469 17:29:13,560 --> 17:29:20,520 it and we'll remove this Mock and we'll 20470 17:29:18,116 --> 17:29:23,320 remove the render order summary as 20471 17:29:20,520 --> 17:29:26,436 W and all we're left with are the 20472 17:29:23,320 --> 17:29:29,560 expectations for our 20473 17:29:26,436 --> 17:29:32,756 test now before we save we need to fix 20474 17:29:29,560 --> 17:29:36,276 one thing with this code here so right 20475 17:29:32,756 --> 17:29:39,520 now these two variables are inside this 20476 17:29:36,276 --> 17:29:42,320 function and because of scope these will 20477 17:29:39,520 --> 17:29:44,796 be contained inside this function and we 20478 17:29:42,320 --> 17:29:47,720 can't use it down here in our 20479 17:29:44,796 --> 17:29:50,276 tests so to fix this we can just move 20480 17:29:47,720 --> 17:29:52,720 these variables outside of this function 20481 17:29:50,276 --> 17:29:56,240 up here so we're going to select these 20482 17:29:52,720 --> 17:29:59,476 lines and then right click 20483 17:29:56,240 --> 17:30:04,160 cut and up 20484 17:29:59,476 --> 17:30:07,916 here we're going to right click and 20485 17:30:04,160 --> 17:30:10,840 paste so now any code inside this outer 20486 17:30:07,916 --> 17:30:12,520 function can use these two variables 20487 17:30:10,840 --> 17:30:15,000 including our 20488 17:30:12,520 --> 17:30:18,000 tests so now let's 20489 17:30:15,000 --> 17:30:20,520 save and check the test 20490 17:30:18,000 --> 17:30:23,796 page and everything is still still 20491 17:30:20,520 --> 17:30:27,360 passing so that's how we use a hook to 20492 17:30:23,796 --> 17:30:29,840 run some code before each test now 20493 17:30:27,360 --> 17:30:32,956 before each is not the only hook that we 20494 17:30:29,840 --> 17:30:35,000 can use Jasmine provides several hooks 20495 17:30:32,956 --> 17:30:38,880 that we can use in our 20496 17:30:35,000 --> 17:30:41,596 tests another common Hook is after each 20497 17:30:38,880 --> 17:30:44,000 which lets us run a function after each 20498 17:30:41,596 --> 17:30:47,276 test so this is really useful for 20499 17:30:44,000 --> 17:30:48,560 cleanup code like we have down here when 20500 17:30:47,276 --> 17:30:52,956 we remove the 20501 17:30:48,560 --> 17:30:55,840 HTML all leave using after each as an 20502 17:30:52,956 --> 17:30:59,000 exercise now let's go into git and save 20503 17:30:55,840 --> 17:31:01,320 our changes and I can close these three 20504 17:30:59,000 --> 17:31:04,160 files at the top for 20505 17:31:01,320 --> 17:31:08,000 now so we'll open the git 20506 17:31:04,160 --> 17:31:11,000 section and then inside orders summary. 20507 17:31:08,000 --> 17:31:13,000 JS we added some classes that we're 20508 17:31:11,000 --> 17:31:17,560 going to use in our 20509 17:31:13,000 --> 17:31:20,520 tests inside test.html we are loading 20510 17:31:17,560 --> 17:31:23,000 our order summary test file and we also 20511 17:31:20,520 --> 17:31:26,636 created a test container where we're 20512 17:31:23,000 --> 17:31:26,636 going to put our HTML that we 20513 17:31:26,720 --> 17:31:32,360 generate and then for the order summary 20514 17:31:29,200 --> 17:31:34,360 test file this contains the test that we 20515 17:31:32,360 --> 17:31:36,796 created for the order 20516 17:31:34,360 --> 17:31:38,080 summary now let's create a message 20517 17:31:36,796 --> 17:31:46,320 describing our 20518 17:31:38,080 --> 17:31:50,840 changes create automated tests for order 20519 17:31:46,320 --> 17:31:50,840 summary and commit 20520 17:31:54,956 --> 17:31:59,116 now that we've created all these tests 20521 17:31:56,880 --> 17:32:02,040 for our project when we make some 20522 17:31:59,116 --> 17:32:05,116 changes to our code we can just open the 20523 17:32:02,040 --> 17:32:06,276 testing page and it will rerun all of 20524 17:32:05,116 --> 17:32:08,916 our 20525 17:32:06,276 --> 17:32:12,200 tests so here's the process that we do 20526 17:32:08,916 --> 17:32:15,880 when we write code first we make changes 20527 17:32:12,200 --> 17:32:18,756 to our code and then we rerun the tests 20528 17:32:15,880 --> 17:32:22,560 to make sure that everything still works 20529 17:32:18,756 --> 17:32:25,116 and finally we save our changes into 20530 17:32:22,560 --> 17:32:27,956 git now before we end the lesson we're 20531 17:32:25,116 --> 17:32:29,436 going to do one more cleanup we'll go 20532 17:32:27,956 --> 17:32:33,040 back to our 20533 17:32:29,436 --> 17:32:36,796 code and we'll open our 20534 17:32:33,040 --> 17:32:40,840 files and here we usually organize all 20535 17:32:36,796 --> 17:32:43,080 of our tests into a folder called tests 20536 17:32:40,840 --> 17:32:46,476 however in this lesson we named this 20537 17:32:43,080 --> 17:32:48,840 folder test- Jasmin because we wanted to 20538 17:32:46,476 --> 17:32:52,000 compare this folder to tests that were 20539 17:32:48,840 --> 17:32:54,040 created without a testing framework but 20540 17:32:52,000 --> 17:32:57,840 now that the lesson's over we're going 20541 17:32:54,040 --> 17:32:59,000 to rename test- Jasmine to just test to 20542 17:32:57,840 --> 17:33:01,320 make it more 20543 17:32:59,000 --> 17:33:04,116 standard first we need to rename this 20544 17:33:01,320 --> 17:33:06,476 folder up here so we'll right 20545 17:33:04,116 --> 17:33:10,956 click 20546 17:33:06,476 --> 17:33:13,276 rename and let's rename it to test- 20547 17:33:10,956 --> 17:33:16,596 simple because this folder contains 20548 17:33:13,276 --> 17:33:19,880 tests that were created without a 20549 17:33:16,596 --> 17:33:22,560 framework let's press enter 20550 17:33:19,880 --> 17:33:26,476 and now we can rename test- Jasmine to 20551 17:33:22,560 --> 17:33:32,200 test by right clicking 20552 17:33:26,476 --> 17:33:32,200 rename and rename it to tests and press 20553 17:33:32,520 --> 17:33:38,956 enter another thing we can do is we're 20554 17:33:35,400 --> 17:33:41,520 going to drag test- simple into the test 20555 17:33:38,956 --> 17:33:44,520 folder so that all of our tests are 20556 17:33:41,520 --> 17:33:47,040 contained in one 20557 17:33:44,520 --> 17:33:51,360 folder so if it asks you to update the 20558 17:33:47,040 --> 17:33:54,200 Imports you can click yes 20559 17:33:51,360 --> 17:33:56,840 and we also need to open money test.js 20560 17:33:54,200 --> 17:34:01,636 in the test- simple folder because it 20561 17:33:56,840 --> 17:34:01,636 updated the Imports and then save this 20562 17:34:03,720 --> 17:34:08,560 file finally we're going to run all of 20563 17:34:06,476 --> 17:34:12,000 our tests again to make sure everything 20564 17:34:08,560 --> 17:34:15,756 still works after we move things around 20565 17:34:12,000 --> 17:34:18,320 so because test.html is inside a 20566 17:34:15,756 --> 17:34:20,240 different folder now we need to reopen 20567 17:34:18,320 --> 17:34:23,400 this with live server 20568 17:34:20,240 --> 17:34:25,680 so we'll rightclick and click open with 20569 17:34:23,400 --> 17:34:25,680 live 20570 17:34:26,240 --> 17:34:31,276 server and it'll show us that all the 20571 17:34:28,680 --> 17:34:32,320 tests are still passing so our code is 20572 17:34:31,276 --> 17:34:34,756 still 20573 17:34:32,320 --> 17:34:37,560 correct so now we can close some of 20574 17:34:34,756 --> 17:34:37,560 these extra tabs 20575 17:34:37,756 --> 17:34:44,880 here and save our changes into git we'll 20576 17:34:42,160 --> 17:34:47,436 open the git 20577 17:34:44,880 --> 17:34:50,320 section and all of these changes are 20578 17:34:47,436 --> 17:34:52,320 related to renaming the folders 20579 17:34:50,320 --> 17:34:58,080 so we'll give a message at the 20580 17:34:52,320 --> 17:35:00,520 top rename test- Jasmine 20581 17:34:58,080 --> 17:35:03,956 folder to 20582 17:35:00,520 --> 17:35:03,956 tests and 20583 17:35:08,040 --> 17:35:13,400 commit and that's the end of this lesson 20584 17:35:11,160 --> 17:35:16,636 in this lesson we learned how to test 20585 17:35:13,400 --> 17:35:19,276 our code using manual and automated 20586 17:35:16,636 --> 17:35:23,476 tests we learned how to organize our 20587 17:35:19,276 --> 17:35:25,880 test using test cases and test Suites we 20588 17:35:23,476 --> 17:35:28,636 learned how to use a testing framework 20589 17:35:25,880 --> 17:35:32,476 like Jasmine to help us write tests 20590 17:35:28,636 --> 17:35:35,680 easier we learned how to Mock and spy on 20591 17:35:32,476 --> 17:35:38,476 methods we learned how to test web pages 20592 17:35:35,680 --> 17:35:41,436 using integration tests and we learned 20593 17:35:38,476 --> 17:35:43,916 about hooks which lets us share code 20594 17:35:41,436 --> 17:35:46,596 between our 20595 17:35:43,916 --> 17:35:48,680 tests here are some exercises to help 20596 17:35:46,596 --> 17:35:52,560 you practice what we learned in this 20597 17:35:48,680 --> 17:35:52,560 lesson e 20598 17:36:33,596 --> 17:36:38,436 in the rest of this course if you get an 20599 17:36:36,000 --> 17:36:41,436 error in your project but your code 20600 17:36:38,436 --> 17:36:44,956 looks correct you may have some bad data 20601 17:36:41,436 --> 17:36:48,276 saved in local storage to fix this you 20602 17:36:44,956 --> 17:36:50,756 can try running local storage. CLE in 20603 17:36:48,276 --> 17:36:51,596 your console to remove everything from 20604 17:36:50,756 --> 17:36:55,796 local 20605 17:36:51,596 --> 17:36:55,796 storage and then refresh the 20606 17:36:56,116 --> 17:37:02,000 page if you see an error like this 20607 17:36:59,276 --> 17:37:04,720 object object that means that you saved 20608 17:37:02,000 --> 17:37:06,956 something directly into local storage 20609 17:37:04,720 --> 17:37:09,476 but you have to jason. stringify it 20610 17:37:06,956 --> 17:37:11,636 first so check your code to make sure 20611 17:37:09,476 --> 17:37:14,360 that you're always stringifying before 20612 17:37:11,636 --> 17:37:19,560 saving to local storage and then clear 20613 17:37:14,360 --> 17:37:19,560 your local storage and refresh the page 20614 17:37:20,400 --> 17:37:24,436 in this lesson we're going to learn 20615 17:37:22,756 --> 17:37:27,116 objectoriented 20616 17:37:24,436 --> 17:37:31,160 programming objectoriented programming 20617 17:37:27,116 --> 17:37:33,160 or oop is another style of programming 20618 17:37:31,160 --> 17:37:36,080 so it's another way that we write our 20619 17:37:33,160 --> 17:37:39,840 code it's useful to know because many 20620 17:37:36,080 --> 17:37:39,840 programming languages use 20621 17:37:40,080 --> 17:37:46,680 oop now how do we use objectoriented 20622 17:37:43,916 --> 17:37:49,476 programming object-oriented programming 20623 17:37:46,680 --> 17:37:50,796 is basically organizing our code into 20624 17:37:49,476 --> 17:37:53,080 into 20625 17:37:50,796 --> 17:37:56,000 objects let's look at an 20626 17:37:53,080 --> 17:37:59,200 example we're going to go to our 20627 17:37:56,000 --> 17:38:03,276 code and we're going to scroll up and 20628 17:37:59,200 --> 17:38:03,276 open data/ cart. 20629 17:38:03,636 --> 17:38:09,200 JS inside this file we have the code for 20630 17:38:06,720 --> 17:38:12,560 our cart and you'll notice that for the 20631 17:38:09,200 --> 17:38:14,796 cart we have some data at the top and 20632 17:38:12,560 --> 17:38:18,040 then we have a bunch of functions at the 20633 17:38:14,796 --> 17:38:19,796 bottom that modify the cart and these 20634 17:38:18,040 --> 17:38:22,360 are separate from each other the data 20635 17:38:19,796 --> 17:38:23,840 and the functions so this style of 20636 17:38:22,360 --> 17:38:25,956 programming that we've been using 20637 17:38:23,840 --> 17:38:29,880 throughout this course is called 20638 17:38:25,956 --> 17:38:32,040 procedural programming procedure means a 20639 17:38:29,880 --> 17:38:35,200 set of step-by-step 20640 17:38:32,040 --> 17:38:38,520 instructions and this is basically a 20641 17:38:35,200 --> 17:38:40,796 function so in procedural programming we 20642 17:38:38,520 --> 17:38:43,200 organize our code into separate 20643 17:38:40,796 --> 17:38:46,596 functions like we did 20644 17:38:43,200 --> 17:38:49,596 here on the other hand in objectoriented 20645 17:38:46,596 --> 17:38:52,320 programming we organize all of this code 20646 17:38:49,596 --> 17:38:55,276 into an object so we put everything 20647 17:38:52,320 --> 17:38:58,160 together into an 20648 17:38:55,276 --> 17:39:00,796 object so let's do an example and 20649 17:38:58,160 --> 17:39:02,400 convert this cart code into 20650 17:39:00,796 --> 17:39:05,040 objectoriented 20651 17:39:02,400 --> 17:39:08,400 programming we're going to click cart. 20652 17:39:05,040 --> 17:39:12,276 JS and inside this folder we're going to 20653 17:39:08,400 --> 17:39:15,756 create a new file by clicking 20654 17:39:12,276 --> 17:39:19,840 here and let's name this file 20655 17:39:15,756 --> 17:39:24,560 C- oop or objectoriented 20656 17:39:19,840 --> 17:39:27,276 programming. JS and press 20657 17:39:24,560 --> 17:39:30,956 enter and now we're going to copy all of 20658 17:39:27,276 --> 17:39:34,160 the code in cart into this new file so 20659 17:39:30,956 --> 17:39:36,916 let's open this and we'll click in this 20660 17:39:34,160 --> 17:39:40,160 file and then we'll type control a or 20661 17:39:36,916 --> 17:39:43,200 command a to select all the code and 20662 17:39:40,160 --> 17:39:47,636 then right click 20663 17:39:43,200 --> 17:39:51,240 copy and inside C- oop we're going to 20664 17:39:47,636 --> 17:39:51,240 rightclick and paste 20665 17:39:52,476 --> 17:39:57,160 and let's scroll back up to the 20666 17:39:54,400 --> 17:39:59,360 top and now to convert this code into 20667 17:39:57,160 --> 17:40:02,240 objectoriented programming we're going 20668 17:39:59,360 --> 17:40:07,116 to group all the data and the functions 20669 17:40:02,240 --> 17:40:09,320 together into an object first at the top 20670 17:40:07,116 --> 17:40:10,796 let's create an object that we can put 20671 17:40:09,320 --> 17:40:14,956 all of these inside 20672 17:40:10,796 --> 17:40:20,160 of so we'll create a variable const 20673 17:40:14,956 --> 17:40:22,756 let's call it cart equals and object 20674 17:40:20,160 --> 17:40:25,720 and now we're going to group our data 20675 17:40:22,756 --> 17:40:28,596 and all of our functions into this 20676 17:40:25,720 --> 17:40:30,520 object so let's do it step by step and 20677 17:40:28,596 --> 17:40:34,400 we'll start with this cart 20678 17:40:30,520 --> 17:40:37,956 variable as a reminder cart semicolon is 20679 17:40:34,400 --> 17:40:40,360 actually a shortcut for cart equals 20680 17:40:37,956 --> 17:40:42,436 undefined to make things easier to 20681 17:40:40,360 --> 17:40:45,636 understand let's actually write this out 20682 17:40:42,436 --> 17:40:51,240 in full instead of using the shortcut so 20683 17:40:45,636 --> 17:40:51,240 we'll type cart equals undefined 20684 17:40:51,436 --> 17:40:56,956 and now we're going to move this into 20685 17:40:53,596 --> 17:40:59,320 the object so we'll select this 20686 17:40:56,956 --> 17:41:01,560 line and then 20687 17:40:59,320 --> 17:41:05,200 rightclick 20688 17:41:01,560 --> 17:41:09,520 cut and then inside the object we're 20689 17:41:05,200 --> 17:41:09,520 going to rightclick and 20690 17:41:09,636 --> 17:41:15,080 paste now inside an object we can't use 20691 17:41:12,880 --> 17:41:18,476 the word export or 20692 17:41:15,080 --> 17:41:20,240 let so to move a variable into an object 20693 17:41:18,476 --> 17:41:23,320 we're going to convert it into a 20694 17:41:20,240 --> 17:41:24,360 property and a value so we'll remove 20695 17:41:23,320 --> 17:41:29,360 these 20696 17:41:24,360 --> 17:41:32,040 words and then type cart and colon 20697 17:41:29,360 --> 17:41:34,560 undefined and remove the semicolon and 20698 17:41:32,040 --> 17:41:37,796 replace it with a 20699 17:41:34,560 --> 17:41:40,796 comma so this is the same thing as cart 20700 17:41:37,796 --> 17:41:42,240 equals undefined but we move that value 20701 17:41:40,796 --> 17:41:45,360 into an 20702 17:41:42,240 --> 17:41:48,000 object now one problem here is that if 20703 17:41:45,360 --> 17:41:50,956 we access this cart property we're going 20704 17:41:48,000 --> 17:41:53,476 to do cart. cart which can be a bit 20705 17:41:50,956 --> 17:41:58,840 confusing so let's actually rename this 20706 17:41:53,476 --> 17:42:00,916 property to cart capital I items instead 20707 17:41:58,840 --> 17:42:03,720 to make it a bit more clear that this 20708 17:42:00,916 --> 17:42:06,796 contains the items in the cart or the 20709 17:42:03,720 --> 17:42:06,796 products that are in the 20710 17:42:07,720 --> 17:42:14,436 cart next we're going to group all of 20711 17:42:10,636 --> 17:42:16,400 our functions into this object as well 20712 17:42:14,436 --> 17:42:18,560 so let's create some new 20713 17:42:16,400 --> 17:42:21,796 lines and we're going to move this 20714 17:42:18,560 --> 17:42:24,000 function inside here so let's select 20715 17:42:21,796 --> 17:42:24,000 this 20716 17:42:24,080 --> 17:42:28,520 function and then 20717 17:42:26,360 --> 17:42:31,040 rightclick 20718 17:42:28,520 --> 17:42:33,240 cut and inside the 20719 17:42:31,040 --> 17:42:36,320 object we're going to 20720 17:42:33,240 --> 17:42:39,160 rightclick and 20721 17:42:36,320 --> 17:42:41,880 paste and again we can't use the word 20722 17:42:39,160 --> 17:42:44,160 export inside an object so we're going 20723 17:42:41,880 --> 17:42:45,436 to convert this into a property and 20724 17:42:44,160 --> 17:42:49,596 value as 20725 17:42:45,436 --> 17:42:51,796 well so let's remove these words 20726 17:42:49,596 --> 17:42:55,400 and let's make the property name load 20727 17:42:51,796 --> 17:42:58,080 from storage and then we'll type a colon 20728 17:42:55,400 --> 17:43:00,956 and we'll make the value a 20729 17:42:58,080 --> 17:43:03,276 function brackets and curly 20730 17:43:00,956 --> 17:43:06,756 brackets so that's how we move a 20731 17:43:03,276 --> 17:43:09,040 function into this object now make sure 20732 17:43:06,756 --> 17:43:11,840 that you use a regular function syntax 20733 17:43:09,040 --> 17:43:14,080 here and not an arrow function and I'll 20734 17:43:11,840 --> 17:43:17,200 explain why later in this 20735 17:43:14,080 --> 17:43:20,080 lesson now as a reminder when we have a 20736 17:43:17,200 --> 17:43:22,040 function inside an object 20737 17:43:20,080 --> 17:43:25,360 we call this a 20738 17:43:22,040 --> 17:43:29,040 method also JavaScript has a shortcut 20739 17:43:25,360 --> 17:43:32,276 for methods called the shorthand method 20740 17:43:29,040 --> 17:43:34,680 syntax so instead of typing colon and 20741 17:43:32,276 --> 17:43:36,000 function we can actually just remove 20742 17:43:34,680 --> 17:43:38,720 this 20743 17:43:36,000 --> 17:43:41,596 code and just type brackets and curly 20744 17:43:38,720 --> 17:43:44,080 brackets after the function name and 20745 17:43:41,596 --> 17:43:46,040 this is a shortcut for the code that we 20746 17:43:44,080 --> 17:43:49,320 had 20747 17:43:46,040 --> 17:43:52,116 before now the code inside this function 20748 17:43:49,320 --> 17:43:55,680 doesn't actually work anymore because 20749 17:43:52,116 --> 17:43:58,436 cart was actually renamed to cart items 20750 17:43:55,680 --> 17:44:01,160 so we have to rename this variable so 20751 17:43:58,436 --> 17:44:03,436 instead of cart we're going to do cart 20752 17:44:01,160 --> 17:44:07,000 dot cart 20753 17:44:03,436 --> 17:44:10,436 items to access this value and same 20754 17:44:07,000 --> 17:44:14,080 thing here cart do cart 20755 17:44:10,436 --> 17:44:17,080 items and here cart 20756 17:44:14,080 --> 17:44:17,080 doart 20757 17:44:17,756 --> 17:44:24,160 items so so this code Works however we 20758 17:44:21,200 --> 17:44:28,000 have a small problem here if we change 20759 17:44:24,160 --> 17:44:29,756 the name of the object up here this code 20760 17:44:28,000 --> 17:44:32,956 will no longer 20761 17:44:29,756 --> 17:44:37,000 work to solve this problem JavaScript 20762 17:44:32,956 --> 17:44:40,116 has a feature called this this gives us 20763 17:44:37,000 --> 17:44:43,636 the object that contains this function 20764 17:44:40,116 --> 17:44:45,916 so it gives us this outer object up here 20765 17:44:43,636 --> 17:44:49,080 so to improve our code we're going to 20766 17:44:45,916 --> 17:44:54,400 change from using a variable named cart 20767 17:44:49,080 --> 17:44:58,636 to this and here as well this. cart 20768 17:44:54,400 --> 17:45:01,956 items and this Dot card 20769 17:44:58,636 --> 17:45:04,560 items so this will give us the outer 20770 17:45:01,956 --> 17:45:06,636 object and now this code will always 20771 17:45:04,560 --> 17:45:08,160 work and it doesn't matter what the 20772 17:45:06,636 --> 17:45:10,320 variable name 20773 17:45:08,160 --> 17:45:12,320 is the last thing we're going to change 20774 17:45:10,320 --> 17:45:15,000 in this function is we're currently 20775 17:45:12,320 --> 17:45:18,080 loading from local storage 20776 17:45:15,000 --> 17:45:21,360 cart however our original cart was also 20777 17:45:18,080 --> 17:45:25,916 loading from local storage cart so let's 20778 17:45:21,360 --> 17:45:29,840 actually change this to C- oop so that 20779 17:45:25,916 --> 17:45:29,840 we don't affect our original 20780 17:45:32,240 --> 17:45:37,596 cart let's move on and we're going to 20781 17:45:34,880 --> 17:45:40,160 move the other functions into this 20782 17:45:37,596 --> 17:45:43,000 object so let's go down to the next 20783 17:45:40,160 --> 17:45:45,796 function which is save to storage we're 20784 17:45:43,000 --> 17:45:47,840 going to select this and then 20785 17:45:45,796 --> 17:45:51,476 rightclick 20786 17:45:47,840 --> 17:45:54,000 cut and then inside this object at the 20787 17:45:51,476 --> 17:45:54,880 end we're going to add a comma and some 20788 17:45:54,000 --> 17:46:00,160 new 20789 17:45:54,880 --> 17:46:02,756 lines and we're going to rightclick and 20790 17:46:00,160 --> 17:46:06,160 paste and we can't use the word function 20791 17:46:02,756 --> 17:46:09,200 at the front here so let's remove this 20792 17:46:06,160 --> 17:46:14,756 word and just use the shorthand method 20793 17:46:09,200 --> 17:46:14,756 syntax so this is a shortcut for colon 20794 17:46:15,080 --> 17:46:19,720 function and here we're saving to local 20795 17:46:18,040 --> 17:46:23,680 storage cart 20796 17:46:19,720 --> 17:46:26,720 so let's also change this to C- oop so 20797 17:46:23,680 --> 17:46:31,360 we don't affect our original cart so 20798 17:46:26,720 --> 17:46:31,360 we'll change it to card- 20799 17:46:31,520 --> 17:46:37,956 oop another thing we need to change is 20800 17:46:34,320 --> 17:46:41,276 that the cart was renamed to cart 20801 17:46:37,956 --> 17:46:45,240 items so here we're going to change from 20802 17:46:41,276 --> 17:46:49,796 cart to this which gives us the outer 20803 17:46:45,240 --> 17:46:52,116 object and then dot cart items 20804 17:46:49,796 --> 17:46:54,360 which is this property so it should do 20805 17:46:52,116 --> 17:46:56,796 the same thing as before except this 20806 17:46:54,360 --> 17:46:59,636 function is now inside an 20807 17:46:56,796 --> 17:47:02,080 object let's continue and move another 20808 17:46:59,636 --> 17:47:05,080 function into the object so we're going 20809 17:47:02,080 --> 17:47:07,680 to do add to cart this time so let's 20810 17:47:05,080 --> 17:47:07,680 select this 20811 17:47:07,720 --> 17:47:11,880 code and then 20812 17:47:10,040 --> 17:47:15,160 rightclick 20813 17:47:11,880 --> 17:47:18,080 cut and inside the object we're going to 20814 17:47:15,160 --> 17:47:20,840 add a comma and some new lines and then 20815 17:47:18,080 --> 17:47:23,636 rightclick and 20816 17:47:20,840 --> 17:47:26,240 paste so again instead of using export 20817 17:47:23,636 --> 17:47:30,200 function we're just going to remove this 20818 17:47:26,240 --> 17:47:33,116 code and use the shorthand method syntax 20819 17:47:30,200 --> 17:47:35,636 which is a shortcut for this 20820 17:47:33,116 --> 17:47:38,560 code and now let's update the code 20821 17:47:35,636 --> 17:47:42,000 inside this function as well so instead 20822 17:47:38,560 --> 17:47:46,916 of the cart we rename this to this. cart 20823 17:47:42,000 --> 17:47:48,956 items so we'll change it to this do cart 20824 17:47:46,916 --> 17:47:51,276 items 20825 17:47:48,956 --> 17:47:56,916 and then down here we're going to change 20826 17:47:51,276 --> 17:47:59,840 from cart to this. cart 20827 17:47:56,916 --> 17:48:03,720 items and also the function save to 20828 17:47:59,840 --> 17:48:06,000 storage was moved inside the object so 20829 17:48:03,720 --> 17:48:09,840 now to access this function we're going 20830 17:48:06,000 --> 17:48:13,080 to get the outer object again using this 20831 17:48:09,840 --> 17:48:18,320 and then dot save to storage so down 20832 17:48:13,080 --> 17:48:21,840 here we'll add this dot in front to 20833 17:48:18,320 --> 17:48:25,276 access the function inside the 20834 17:48:21,840 --> 17:48:27,956 object and now let's move on and move 20835 17:48:25,276 --> 17:48:29,360 the next function into the object so 20836 17:48:27,956 --> 17:48:31,320 we'll select this 20837 17:48:29,360 --> 17:48:33,320 code and then 20838 17:48:31,320 --> 17:48:36,476 rightclick 20839 17:48:33,320 --> 17:48:40,680 cut inside the object add a comma and 20840 17:48:36,476 --> 17:48:44,400 some new lines and right click and 20841 17:48:40,680 --> 17:48:47,436 paste we'll remove export 20842 17:48:44,400 --> 17:48:50,560 function and use the shorthand method 20843 17:48:47,436 --> 17:48:54,000 syntax and again we'll change from cart 20844 17:48:50,560 --> 17:48:57,400 to this. cart 20845 17:48:54,000 --> 17:49:00,680 items we're going to change cart here to 20846 17:48:57,400 --> 17:49:04,956 this.c cart 20847 17:49:00,680 --> 17:49:06,360 items and change save to storage to this 20848 17:49:04,956 --> 17:49:09,596 do save to 20849 17:49:06,360 --> 17:49:11,956 storage so we access save to storage 20850 17:49:09,596 --> 17:49:14,840 inside this 20851 17:49:11,956 --> 17:49:17,840 object next let's move this last 20852 17:49:14,840 --> 17:49:21,956 function into the object so we'll select 20853 17:49:17,840 --> 17:49:24,880 the code and then rightclick 20854 17:49:21,956 --> 17:49:28,160 cut and inside the object we're going to 20855 17:49:24,880 --> 17:49:30,956 type a comma and some new lines right 20856 17:49:28,160 --> 17:49:34,560 click and 20857 17:49:30,956 --> 17:49:36,276 paste we'll remove export function and 20858 17:49:34,560 --> 17:49:38,796 use the shorthand method 20859 17:49:36,276 --> 17:49:42,956 syntax and update the code in this 20860 17:49:38,796 --> 17:49:44,596 method so we'll change cart to this do 20861 17:49:42,956 --> 17:49:49,436 cart 20862 17:49:44,596 --> 17:49:50,840 items and save to storage to this Dove 20863 17:49:49,436 --> 17:49:55,240 to 20864 17:49:50,840 --> 17:49:55,240 storage now let's remove the extra 20865 17:49:56,080 --> 17:50:01,040 lines and the last thing we need to 20866 17:49:58,116 --> 17:50:04,596 update is that load from Storage was 20867 17:50:01,040 --> 17:50:07,436 moved inside the cart object so to 20868 17:50:04,596 --> 17:50:09,400 access this function at the front we're 20869 17:50:07,436 --> 17:50:14,880 going to type 20870 17:50:09,400 --> 17:50:18,240 cart. load from storage and save this 20871 17:50:14,880 --> 17:50:22,000 file and that's it this is the basic 20872 17:50:18,240 --> 17:50:25,596 idea behind objectoriented programming 20873 17:50:22,000 --> 17:50:28,360 we organize our code into objects so we 20874 17:50:25,596 --> 17:50:33,000 group the data and the functions 20875 17:50:28,360 --> 17:50:33,000 together into an object like 20876 17:50:33,320 --> 17:50:38,436 this now let's talk about why do we use 20877 17:50:36,916 --> 17:50:41,240 objectoriented 20878 17:50:38,436 --> 17:50:44,240 programming objectoriented programming 20879 17:50:41,240 --> 17:50:47,840 tries to represent the real 20880 17:50:44,240 --> 17:50:49,956 world in the real world a cart is a 20881 17:50:47,840 --> 17:50:52,560 physical object object so in 20882 17:50:49,956 --> 17:50:55,756 objectoriented programming we represent 20883 17:50:52,560 --> 17:50:58,476 this physical object as a digital object 20884 17:50:55,756 --> 17:51:01,796 or in this case a JavaScript 20885 17:50:58,476 --> 17:51:05,276 object a physical cart can have products 20886 17:51:01,796 --> 17:51:07,596 inside so our JavaScript object can also 20887 17:51:05,276 --> 17:51:11,160 have products 20888 17:51:07,596 --> 17:51:14,476 inside a physical cart also has actions 20889 17:51:11,160 --> 17:51:20,320 we can take to modify the cart like we 20890 17:51:14,476 --> 17:51:23,400 can add a product or remove a product 20891 17:51:20,320 --> 17:51:27,160 our JavaScript object also has actions 20892 17:51:23,400 --> 17:51:30,476 we can take to modify the cart like the 20893 17:51:27,160 --> 17:51:31,400 add to cart function or the remove from 20894 17:51:30,476 --> 17:51:34,200 cart 20895 17:51:31,400 --> 17:51:36,560 function so as you can see 20896 17:51:34,200 --> 17:51:39,560 objectoriented programming tries to 20897 17:51:36,560 --> 17:51:42,160 represent the real world we take a 20898 17:51:39,560 --> 17:51:45,200 physical object like a cart and 20899 17:51:42,160 --> 17:51:46,720 represent it as a digital object like a 20900 17:51:45,200 --> 17:51:48,956 JavaScript 20901 17:51:46,720 --> 17:51:51,520 object and some people feel feel that 20902 17:51:48,956 --> 17:51:54,596 this makes the code more intuitive or 20903 17:51:51,520 --> 17:51:54,596 easier to 20904 17:51:55,160 --> 17:52:00,840 understand now let's try out the code in 20905 17:51:58,040 --> 17:52:04,080 this object to make sure it actually 20906 17:52:00,840 --> 17:52:06,200 works we're going to scroll down to the 20907 17:52:04,080 --> 17:52:10,476 bottom and 20908 17:52:06,200 --> 17:52:12,756 here we're just going to type 20909 17:52:10,476 --> 17:52:15,796 console.log the 20910 17:52:12,756 --> 17:52:19,000 cart and save this 20911 17:52:15,796 --> 17:52:22,160 file and now let's load this code into 20912 17:52:19,000 --> 17:52:24,956 the project to do that we're going to 20913 17:52:22,160 --> 17:52:27,796 open the scripts folder and open the 20914 17:52:24,956 --> 17:52:31,720 file checkout. 20915 17:52:27,796 --> 17:52:34,116 JS and at the top below the Imports 20916 17:52:31,720 --> 17:52:36,000 we're going to import our new file so 20917 17:52:34,116 --> 17:52:38,320 let's type 20918 17:52:36,000 --> 17:52:40,560 import and we're going to use a slightly 20919 17:52:38,320 --> 17:52:41,680 different syntax we're just going to 20920 17:52:40,560 --> 17:52:44,040 type a 20921 17:52:41,680 --> 17:52:49,360 string and give it the file that we want 20922 17:52:44,040 --> 17:52:51,200 to load so we need to load C- oop. Js 20923 17:52:49,360 --> 17:52:54,756 so first we need to get out of the 20924 17:52:51,200 --> 17:52:58,360 scripts folder using do slash and then 20925 17:52:54,756 --> 17:53:00,956 go into the data folder data slash and 20926 17:52:58,360 --> 17:53:02,840 load C- 20927 17:53:00,956 --> 17:53:06,520 oop. 20928 17:53:02,840 --> 17:53:10,200 JS so this is another syntax we can use 20929 17:53:06,520 --> 17:53:13,000 for imports this just runs all the code 20930 17:53:10,200 --> 17:53:15,840 inside this file without importing 20931 17:53:13,000 --> 17:53:20,360 anything which is what we need in this 20932 17:53:15,840 --> 17:53:20,360 situation so let's save this file 20933 17:53:20,400 --> 17:53:25,320 and then open this page in the browser 20934 17:53:23,160 --> 17:53:28,320 so if you don't have this page open you 20935 17:53:25,320 --> 17:53:31,560 can scroll down to checkout. HTML and 20936 17:53:28,320 --> 17:53:35,160 then right click and open with live 20937 17:53:31,560 --> 17:53:37,080 server I already have this page open so 20938 17:53:35,160 --> 17:53:40,400 I'm just going to open that 20939 17:53:37,080 --> 17:53:43,080 Tab and if the page is too big we can 20940 17:53:40,400 --> 17:53:45,720 zoom out by pressing control minus or 20941 17:53:43,080 --> 17:53:48,476 command minus a few 20942 17:53:45,720 --> 17:53:51,040 times and now let's check the console to 20943 17:53:48,476 --> 17:53:52,916 see what the cart looks like on this 20944 17:53:51,040 --> 17:53:55,796 page we're going to 20945 17:53:52,916 --> 17:53:59,116 rightclick and click 20946 17:53:55,796 --> 17:54:02,116 inspect and then open the 20947 17:53:59,116 --> 17:54:05,756 console and in the console we'll see 20948 17:54:02,116 --> 17:54:08,240 this cart object so inside this object 20949 17:54:05,756 --> 17:54:11,160 we have the data as well as all the 20950 17:54:08,240 --> 17:54:14,000 functions for our cart but it's grouped 20951 17:54:11,160 --> 17:54:17,240 inside an object and you'll see that the 20952 17:54:14,000 --> 17:54:20,276 code still works the same as before cart 20953 17:54:17,240 --> 17:54:24,200 items has a the two default 20954 17:54:20,276 --> 17:54:25,880 products if we look at our code and open 20955 17:54:24,200 --> 17:54:29,596 card- 20956 17:54:25,880 --> 17:54:32,436 o at the beginning we run cart. load 20957 17:54:29,596 --> 17:54:35,520 from storage and then inside this 20958 17:54:32,436 --> 17:54:38,276 function if we scroll up it will give 20959 17:54:35,520 --> 17:54:40,956 the cart items this default cart with 20960 17:54:38,276 --> 17:54:44,080 two products inside and that's what we 20961 17:54:40,956 --> 17:54:46,840 see in the console 20962 17:54:44,080 --> 17:54:48,880 here so we know that the code inside 20963 17:54:46,840 --> 17:54:51,240 this object still works works the same 20964 17:54:48,880 --> 17:54:54,040 way as 20965 17:54:51,240 --> 17:54:56,596 before so now let's try another method 20966 17:54:54,040 --> 17:55:00,116 to make sure that it's working so let's 20967 17:54:56,596 --> 17:55:03,756 try using the add to cart method to add 20968 17:55:00,116 --> 17:55:05,080 a product to the cart items up here so 20969 17:55:03,756 --> 17:55:08,436 we'll scroll 20970 17:55:05,080 --> 17:55:11,796 down to the bottom and then above 20971 17:55:08,436 --> 17:55:16,040 console.log cart we're going to call add 20972 17:55:11,796 --> 17:55:19,200 to cart now add to cart was saved inside 20973 17:55:16,040 --> 17:55:21,560 this cart object so to access it we're 20974 17:55:19,200 --> 17:55:26,680 going to type 20975 17:55:21,560 --> 17:55:29,520 cart. add to cart and 20976 17:55:26,680 --> 17:55:32,796 brackets now add to cart takes one 20977 17:55:29,520 --> 17:55:36,400 parameter which is a string and this is 20978 17:55:32,796 --> 17:55:39,200 the product ID that we want to add so to 20979 17:55:36,400 --> 17:55:43,560 find a product ID we can go to the data 20980 17:55:39,200 --> 17:55:47,040 folder and products. JS and then inside 20981 17:55:43,560 --> 17:55:49,436 here we have this list of products so 20982 17:55:47,040 --> 17:55:52,276 let's actually scroll down to the third 20983 17:55:49,436 --> 17:55:54,916 product which is right here so this 20984 17:55:52,276 --> 17:55:58,000 t-shirt product and we're going to add 20985 17:55:54,916 --> 17:56:01,320 this product ID to the cart so let's 20986 17:55:58,000 --> 17:56:03,360 select this ID we're going to 20987 17:56:01,320 --> 17:56:06,796 rightclick 20988 17:56:03,360 --> 17:56:09,436 copy and then inside C- 20989 17:56:06,796 --> 17:56:12,796 oop inside the string we're going to 20990 17:56:09,436 --> 17:56:15,880 rightclick and paste that product 20991 17:56:12,796 --> 17:56:18,880 ID and let's 20992 17:56:15,880 --> 17:56:20,720 save and let's check the console 20993 17:56:18,880 --> 17:56:24,320 so I'll open the console 20994 17:56:20,720 --> 17:56:28,000 Here and Now inside the cart object if 20995 17:56:24,320 --> 17:56:30,400 we expand it cart items now has three 20996 17:56:28,000 --> 17:56:33,400 products because we just added a new 20997 17:56:30,400 --> 17:56:35,680 product to the cart so this shows that 20998 17:56:33,400 --> 17:56:39,360 all of the code is working the exact 20999 17:56:35,680 --> 17:56:44,756 same way as before we just organized it 21000 17:56:39,360 --> 17:56:46,680 into an object and that's objectoriented 21001 17:56:44,756 --> 17:56:49,160 programming now that we're done with 21002 17:56:46,680 --> 17:56:50,796 this example let's remove these two 21003 17:56:49,160 --> 17:56:54,320 lines of 21004 17:56:50,796 --> 17:56:54,320 code and 21005 17:56:56,040 --> 17:57:02,080 save next another reason we use 21006 17:56:59,320 --> 17:57:06,240 objectoriented programming is it's easy 21007 17:57:02,080 --> 17:57:09,520 to create multiple objects for example 21008 17:57:06,240 --> 17:57:11,956 the real amazon.com has a feature called 21009 17:57:09,520 --> 17:57:14,200 Amazon business where you can have a 21010 17:57:11,956 --> 17:57:16,756 separate cart for your business 21011 17:57:14,200 --> 17:57:19,880 purchases so how do we create two 21012 17:57:16,756 --> 17:57:23,476 separate carts in our code with 21013 17:57:19,880 --> 17:57:26,320 objectoriented programming this is easy 21014 17:57:23,476 --> 17:57:29,400 we can just make a copy of the cart 21015 17:57:26,320 --> 17:57:32,116 object so we can scroll up in this 21016 17:57:29,400 --> 17:57:34,880 code and I'm just going to click this 21017 17:57:32,116 --> 17:57:37,796 Arrow here beside the card variable to 21018 17:57:34,880 --> 17:57:40,520 collapse this code and to create a 21019 17:57:37,796 --> 17:57:43,720 separate cart for business purchases 21020 17:57:40,520 --> 17:57:46,636 we're just going to select this code and 21021 17:57:43,720 --> 17:57:48,956 then right click 21022 17:57:46,636 --> 17:57:53,000 copy and at the 21023 17:57:48,956 --> 17:57:57,240 bottom right click and 21024 17:57:53,000 --> 17:57:59,320 paste and now we have two separate cards 21025 17:57:57,240 --> 17:58:03,840 so let's scroll up to the top of the 21026 17:57:59,320 --> 17:58:05,560 second cart which is right here and we 21027 17:58:03,840 --> 17:58:08,796 said that we wanted this card for 21028 17:58:05,560 --> 17:58:10,596 business purchases so let's rename this 21029 17:58:08,796 --> 17:58:13,636 to 21030 17:58:10,596 --> 17:58:17,160 business Capital C 21031 17:58:13,636 --> 17:58:21,840 cart and when we load from Storage let's 21032 17:58:17,160 --> 17:58:23,636 load it from cart Das business instead 21033 17:58:21,840 --> 17:58:24,796 so that it doesn't conflict with the 21034 17:58:23,636 --> 17:58:28,160 cart up 21035 17:58:24,796 --> 17:58:31,476 here and if we scroll down same thing in 21036 17:58:28,160 --> 17:58:34,200 save to storage let's save this cart to 21037 17:58:31,476 --> 17:58:36,796 cart Das 21038 17:58:34,200 --> 17:58:38,596 business and finally let's scroll down 21039 17:58:36,796 --> 17:58:41,720 to the 21040 17:58:38,596 --> 17:58:43,956 bottom and here instead of cart. load 21041 17:58:41,720 --> 17:58:50,040 from Storage we're going to change this 21042 17:58:43,956 --> 17:58:50,040 to business cart. load from Storage 21043 17:58:50,116 --> 17:58:55,360 and that's it we just created a 21044 17:58:52,636 --> 17:58:58,796 completely separate cart simply by 21045 17:58:55,360 --> 17:59:01,476 copying this object so object-oriented 21046 17:58:58,796 --> 17:59:04,240 programming makes it easy to create 21047 17:59:01,476 --> 17:59:04,240 multiple 21048 17:59:05,000 --> 17:59:10,880 objects so now let's console.log both of 21049 17:59:08,000 --> 17:59:12,520 these cards to see what they look like 21050 17:59:10,880 --> 17:59:16,756 we're going to scroll to the 21051 17:59:12,520 --> 17:59:18,880 bottom and here we're going to 21052 17:59:16,756 --> 17:59:21,320 console.log the 21053 17:59:18,880 --> 17:59:28,200 cart and 21054 17:59:21,320 --> 17:59:30,956 console.log the business cart and 21055 17:59:28,200 --> 17:59:32,080 save and if we open the console for the 21056 17:59:30,956 --> 17:59:35,240 checkout 21057 17:59:32,080 --> 17:59:38,560 page it will show us that we have two 21058 17:59:35,240 --> 17:59:41,560 carts the regular cart has three 21059 17:59:38,560 --> 17:59:44,116 products inside and the business cart 21060 17:59:41,560 --> 17:59:46,116 has two products inside and these are 21061 17:59:44,116 --> 17:59:48,720 just the default products that we get 21062 17:59:46,116 --> 17:59:51,080 when we first load the cart 21063 17:59:48,720 --> 17:59:53,116 so you can see that these two cards are 21064 17:59:51,080 --> 17:59:56,320 completely separate from each 21065 17:59:53,116 --> 17:59:58,160 other in the real world we can have two 21066 17:59:56,320 --> 18:00:00,080 physical cards that are completely 21067 17:59:58,160 --> 18:00:02,840 separate from each other and in 21068 18:00:00,080 --> 18:00:04,476 programming we can also have two cards 21069 18:00:02,840 --> 18:00:08,720 that are completely separate from each 21070 18:00:04,476 --> 18:00:11,560 other by simply making a copy of the 21071 18:00:08,720 --> 18:00:13,916 object so again object-oriented 21072 18:00:11,560 --> 18:00:16,916 programming tries to represent the real 21073 18:00:13,916 --> 18:00:16,916 world 21074 18:00:19,160 --> 18:00:24,200 now currently there's an obvious problem 21075 18:00:21,680 --> 18:00:27,200 with how we're creating multiple objects 21076 18:00:24,200 --> 18:00:29,840 which is we're copy pasting a lot of 21077 18:00:27,200 --> 18:00:32,476 code and this can make our code messy 21078 18:00:29,840 --> 18:00:34,520 and hard to manage so now we're going to 21079 18:00:32,476 --> 18:00:37,560 make our code cleaner by using a 21080 18:00:34,520 --> 18:00:40,240 function to create multiple 21081 18:00:37,560 --> 18:00:43,200 objects so first let's delete this 21082 18:00:40,240 --> 18:00:46,000 business card that we copy pasted so 21083 18:00:43,200 --> 18:00:49,000 we're going to select this 21084 18:00:46,000 --> 18:00:51,520 object and then up to here the business 21085 18:00:49,000 --> 18:00:54,320 card we're going to remove 21086 18:00:51,520 --> 18:00:56,840 it and now instead of copy pasting this 21087 18:00:54,320 --> 18:00:59,840 object we're going to use a function to 21088 18:00:56,840 --> 18:01:03,276 generate these objects so at the top of 21089 18:00:59,840 --> 18:01:06,360 this file let's create a new 21090 18:01:03,276 --> 18:01:10,560 function and we'll name it capital c 21091 18:01:06,360 --> 18:01:10,560 cart brackets and curly 21092 18:01:10,956 --> 18:01:16,400 brackets so by the way in objectoriented 21093 18:01:14,000 --> 18:01:19,276 programming a naming convention is to 21094 18:01:16,400 --> 18:01:22,796 use Pascal case for things that create 21095 18:01:19,276 --> 18:01:24,956 or generate objects in Pascal case we 21096 18:01:22,796 --> 18:01:27,240 start every word with a capital 21097 18:01:24,956 --> 18:01:29,520 including the first word so that's why 21098 18:01:27,240 --> 18:01:32,276 we started cart with a Capital 21099 18:01:29,520 --> 18:01:34,080 C now inside this function we're going 21100 18:01:32,276 --> 18:01:36,436 to create cart 21101 18:01:34,080 --> 18:01:38,720 objects so let's actually move this 21102 18:01:36,436 --> 18:01:41,116 object into the function so we can 21103 18:01:38,720 --> 18:01:44,520 create it using the function so we're 21104 18:01:41,116 --> 18:01:45,720 going to select this object to here and 21105 18:01:44,520 --> 18:01:49,000 then we'll right 21106 18:01:45,720 --> 18:01:51,200 click and cut 21107 18:01:49,000 --> 18:01:53,000 and then move it into the function by 21108 18:01:51,200 --> 18:01:56,400 clicking in the function and then 21109 18:01:53,000 --> 18:01:56,400 rightclick and 21110 18:01:56,436 --> 18:02:01,916 paste so now every time we run this 21111 18:01:59,360 --> 18:02:04,560 function it will generate a new cart 21112 18:02:01,916 --> 18:02:06,680 object and we don't have to copy paste 21113 18:02:04,560 --> 18:02:07,796 this code over and over to create 21114 18:02:06,680 --> 18:02:10,360 multiple 21115 18:02:07,796 --> 18:02:12,240 objects so at the bottom I'm going to 21116 18:02:10,360 --> 18:02:14,476 click this Arrow to collapse this object 21117 18:02:12,240 --> 18:02:18,000 for now and at the bottom of this 21118 18:02:14,476 --> 18:02:22,400 function we're going to return this C 21119 18:02:18,000 --> 18:02:22,400 object so we can use it outside the 21120 18:02:22,476 --> 18:02:28,000 function okay now let's give this a try 21121 18:02:25,636 --> 18:02:31,360 and we'll use this function to generate 21122 18:02:28,000 --> 18:02:35,720 cart objects so first let's create the 21123 18:02:31,360 --> 18:02:38,320 normal cart using const cart make it 21124 18:02:35,720 --> 18:02:43,320 equal and we'll run this function to 21125 18:02:38,320 --> 18:02:45,596 generate an object Capital C cart and 21126 18:02:43,320 --> 18:02:49,200 brackets and let's generate another 21127 18:02:45,596 --> 18:02:52,400 object using the function using const 21128 18:02:49,200 --> 18:02:57,796 and we'll name it business 21129 18:02:52,400 --> 18:03:00,116 cart equals Capital C cart and 21130 18:02:57,796 --> 18:03:02,040 brackets and we'll still load from 21131 18:03:00,116 --> 18:03:03,720 storage for both of these and then 21132 18:03:02,040 --> 18:03:06,320 console log both of 21133 18:03:03,720 --> 18:03:08,040 these so basically instead of copy 21134 18:03:06,320 --> 18:03:10,956 pasting all the time we're going to use 21135 18:03:08,040 --> 18:03:14,040 a function to generate 21136 18:03:10,956 --> 18:03:16,476 objects so now let's 21137 18:03:14,040 --> 18:03:18,956 save and then open the 21138 18:03:16,476 --> 18:03:20,116 console and and now we have two cart 21139 18:03:18,956 --> 18:03:23,080 objects 21140 18:03:20,116 --> 18:03:25,160 again however now both of these carts 21141 18:03:23,080 --> 18:03:28,160 have three products 21142 18:03:25,160 --> 18:03:30,400 inside but before the regular card had 21143 18:03:28,160 --> 18:03:32,040 three products and the business card had 21144 18:03:30,400 --> 18:03:33,916 two 21145 18:03:32,040 --> 18:03:37,596 products so what 21146 18:03:33,916 --> 18:03:40,040 happened if we go back to our code and 21147 18:03:37,596 --> 18:03:42,200 look inside this cart 21148 18:03:40,040 --> 18:03:45,000 object you'll notice that we're loading 21149 18:03:42,200 --> 18:03:48,956 from the same key in local storage which 21150 18:03:45,000 --> 18:03:50,916 is C- o that means every cart that we 21151 18:03:48,956 --> 18:03:53,520 generate will be getting its data from 21152 18:03:50,916 --> 18:03:56,116 the same place in local 21153 18:03:53,520 --> 18:04:00,040 storage so to fix this we're going to 21154 18:03:56,116 --> 18:04:02,520 replace C- oop with a parameter so 21155 18:04:00,040 --> 18:04:06,360 different carts can use a different key 21156 18:04:02,520 --> 18:04:11,560 in local storage so at the top let's 21157 18:04:06,360 --> 18:04:14,116 create a parameter called local storage 21158 18:04:11,560 --> 18:04:16,756 key and then instead of using the same 21159 18:04:14,116 --> 18:04:19,916 string every time we're going to use the 21160 18:04:16,756 --> 18:04:23,320 parameter local storage 21161 18:04:19,916 --> 18:04:25,680 key and the same thing for save to 21162 18:04:23,320 --> 18:04:27,636 storage instead of saving to the same 21163 18:04:25,680 --> 18:04:33,476 key every time we're going to replace 21164 18:04:27,636 --> 18:04:36,320 this with the parameter local storage 21165 18:04:33,476 --> 18:04:39,756 key and finally when we run this 21166 18:04:36,320 --> 18:04:42,240 function at the bottom 21167 18:04:39,756 --> 18:04:45,200 here we need to give the function a 21168 18:04:42,240 --> 18:04:49,956 local storage key so for our regular 21169 18:04:45,200 --> 18:04:53,240 cart let's use the string cart C- 21170 18:04:49,956 --> 18:04:57,880 oop and for the business cart we can use 21171 18:04:53,240 --> 18:05:00,276 a different key like the string cart Das 21172 18:04:57,880 --> 18:05:02,400 business so now these two carts will 21173 18:05:00,276 --> 18:05:05,320 load from different keys in local 21174 18:05:02,400 --> 18:05:09,436 storage and they can have different data 21175 18:05:05,320 --> 18:05:12,000 so let's save and then check the 21176 18:05:09,436 --> 18:05:14,040 console and now our regular card has 21177 18:05:12,000 --> 18:05:16,560 three products and our business card has 21178 18:05:14,040 --> 18:05:20,000 two products like 21179 18:05:16,560 --> 18:05:22,720 before so let's go back to our code and 21180 18:05:20,000 --> 18:05:25,276 this is a better way to create multiple 21181 18:05:22,720 --> 18:05:27,840 objects instead of copy pasting a bunch 21182 18:05:25,276 --> 18:05:31,560 of code every time we can create a 21183 18:05:27,840 --> 18:05:31,560 function that generates 21184 18:05:31,840 --> 18:05:37,436 objects so now let's save this example 21185 18:05:34,320 --> 18:05:40,840 into git we'll go into our git 21186 18:05:37,436 --> 18:05:43,276 section and then at the top here we 21187 18:05:40,840 --> 18:05:46,636 created a function to generate these 21188 18:05:43,276 --> 18:05:50,596 objects and in checkout. JS we are 21189 18:05:46,636 --> 18:05:52,880 loading that c - oop practice file so 21190 18:05:50,596 --> 18:05:58,200 we'll type a message to describe these 21191 18:05:52,880 --> 18:06:04,596 changes create oop or objectoriented 21192 18:05:58,200 --> 18:06:04,596 programming version of cart and 21193 18:06:10,240 --> 18:06:17,956 commit next we're going to learn an even 21194 18:06:13,240 --> 18:06:17,956 better way to generate objects 21195 18:06:18,360 --> 18:06:23,636 object-oriented programming has a 21196 18:06:20,116 --> 18:06:26,160 feature called a Class A Class is 21197 18:06:23,636 --> 18:06:31,636 specifically designed for generating 21198 18:06:26,160 --> 18:06:31,636 objects a class is basically an object 21199 18:06:32,276 --> 18:06:38,720 generator So currently if we open our 21200 18:06:35,476 --> 18:06:42,040 code and then open C- 21201 18:06:38,720 --> 18:06:45,560 oop we learn how to use a function to 21202 18:06:42,040 --> 18:06:47,880 generate objects in objectoriented 21203 18:06:45,560 --> 18:06:50,680 programming so now we're going to switch 21204 18:06:47,880 --> 18:06:53,400 this to using a class to generate these 21205 18:06:50,680 --> 18:06:55,596 objects instead because classes are 21206 18:06:53,400 --> 18:06:58,636 cleaner and have more features than 21207 18:06:55,596 --> 18:07:00,756 using a function so first let's create a 21208 18:06:58,636 --> 18:07:03,680 new file for learning 21209 18:07:00,756 --> 18:07:07,000 classes inside the data folder we're 21210 18:07:03,680 --> 18:07:08,160 going to click C- oop and then create a 21211 18:07:07,000 --> 18:07:12,956 new 21212 18:07:08,160 --> 18:07:18,080 file and we'll name this file cart DCL 21213 18:07:12,956 --> 18:07:20,360 class. JS and press enter 21214 18:07:18,080 --> 18:07:23,720 so first let's copy over all the code so 21215 18:07:20,360 --> 18:07:26,520 we can compare it we're going to open C- 21216 18:07:23,720 --> 18:07:29,796 oop click this file and then press 21217 18:07:26,520 --> 18:07:31,000 contrl a or command a to select the code 21218 18:07:29,796 --> 18:07:32,840 and right 21219 18:07:31,000 --> 18:07:35,040 click 21220 18:07:32,840 --> 18:07:40,240 copy and in 21221 18:07:35,040 --> 18:07:43,436 C-Class we're going to right click and 21222 18:07:40,240 --> 18:07:46,596 paste and we'll scroll to the 21223 18:07:43,436 --> 18:07:50,756 top and at the top we're going to create 21224 18:07:46,596 --> 18:07:53,320 our first class class using the word 21225 18:07:50,756 --> 18:07:56,160 class and now we're going to give a name 21226 18:07:53,320 --> 18:08:02,796 to this class so let's name it the same 21227 18:07:56,160 --> 18:08:05,400 as here Capital C cart and then curly 21228 18:08:02,796 --> 18:08:08,040 brackets as a reminder a naming 21229 18:08:05,400 --> 18:08:10,400 convention is to use Pascal case for 21230 18:08:08,040 --> 18:08:12,880 things that generate objects so that's 21231 18:08:10,400 --> 18:08:17,000 why we start this cart with a Capital 21232 18:08:12,880 --> 18:08:20,000 C so this is a class A Class is 21233 18:08:17,000 --> 18:08:23,040 basically an object 21234 18:08:20,000 --> 18:08:25,520 generator so now inside the class we're 21235 18:08:23,040 --> 18:08:28,116 going to put the properties and methods 21236 18:08:25,520 --> 18:08:30,560 that we want for each object that we 21237 18:08:28,116 --> 18:08:32,320 generate so here we have our properties 21238 18:08:30,560 --> 18:08:34,276 and methods we're just going to move 21239 18:08:32,320 --> 18:08:36,680 them into the 21240 18:08:34,276 --> 18:08:37,756 class so first let's select the card 21241 18:08:36,680 --> 18:08:41,520 items 21242 18:08:37,756 --> 18:08:44,916 property we're going to right click 21243 18:08:41,520 --> 18:08:47,720 cut and then move it into the class by 21244 18:08:44,916 --> 18:08:49,840 clicking in here and then right click 21245 18:08:47,720 --> 18:08:52,636 and 21246 18:08:49,840 --> 18:08:55,840 paste and classes use a slightly 21247 18:08:52,636 --> 18:08:58,116 different syntax than objects so to 21248 18:08:55,840 --> 18:09:00,160 create a property called cart items 21249 18:08:58,116 --> 18:09:02,040 we're going to type the property name 21250 18:09:00,160 --> 18:09:05,116 and instead of a colon we're just going 21251 18:09:02,040 --> 18:09:07,436 to make it equal to undefined and 21252 18:09:05,116 --> 18:09:09,680 instead of a comma at the end we're 21253 18:09:07,436 --> 18:09:11,956 going to put a 21254 18:09:09,680 --> 18:09:15,276 semicolon so that's how we add a 21255 18:09:11,956 --> 18:09:17,956 property to a class and now every object 21256 18:09:15,276 --> 18:09:19,400 that we generate will have this property 21257 18:09:17,956 --> 18:09:22,520 just like we did 21258 18:09:19,400 --> 18:09:25,400 before next let's move the methods into 21259 18:09:22,520 --> 18:09:29,240 the class so we'll delete these lines 21260 18:09:25,400 --> 18:09:33,276 for now and we'll move load from Storage 21261 18:09:29,240 --> 18:09:35,360 so we'll select this method and then 21262 18:09:33,276 --> 18:09:38,840 rightclick and 21263 18:09:35,360 --> 18:09:43,360 cut and inside the class we're going to 21264 18:09:38,840 --> 18:09:45,916 move it by right clicking and 21265 18:09:43,360 --> 18:09:48,116 paste and classes use a slightly 21266 18:09:45,916 --> 18:09:50,956 different syntax to create methods as 21267 18:09:48,116 --> 18:09:54,756 well so instead of having a comma at the 21268 18:09:50,956 --> 18:09:56,720 end we can just remove it and every 21269 18:09:54,756 --> 18:09:57,520 object that we generate will now have 21270 18:09:56,720 --> 18:09:59,880 this 21271 18:09:57,520 --> 18:10:02,436 method so now let's move the rest of the 21272 18:09:59,880 --> 18:10:05,080 methods into this class we're going to 21273 18:10:02,436 --> 18:10:08,240 scroll down and we'll move save to 21274 18:10:05,080 --> 18:10:10,756 storage so we'll select the method and 21275 18:10:08,240 --> 18:10:14,200 right click 21276 18:10:10,756 --> 18:10:18,000 cut and at the bottom of the class we're 21277 18:10:14,200 --> 18:10:19,880 going to right click and paste 21278 18:10:18,000 --> 18:10:23,476 and remove the comma at the 21279 18:10:19,880 --> 18:10:25,636 end same with add to cut we'll select 21280 18:10:23,476 --> 18:10:28,240 this 21281 18:10:25,636 --> 18:10:30,436 method and then right 21282 18:10:28,240 --> 18:10:34,116 click 21283 18:10:30,436 --> 18:10:37,200 cut and inside the bottom of the class 21284 18:10:34,116 --> 18:10:41,000 we're going to rightclick and 21285 18:10:37,200 --> 18:10:44,436 paste and remove the comma at the 21286 18:10:41,000 --> 18:10:45,636 end as well as remove from cart so we'll 21287 18:10:44,436 --> 18:10:48,840 select this 21288 18:10:45,636 --> 18:10:52,756 method right click 21289 18:10:48,840 --> 18:10:54,436 cut and at the bottom of the class right 21290 18:10:52,756 --> 18:10:57,916 click and 21291 18:10:54,436 --> 18:11:00,596 paste and remove the comma at the 21292 18:10:57,916 --> 18:11:03,320 end and finally we'll move the last 21293 18:11:00,596 --> 18:11:07,200 method update delivery 21294 18:11:03,320 --> 18:11:10,796 option and right click 21295 18:11:07,200 --> 18:11:13,756 cut inside this class we're going to 21296 18:11:10,796 --> 18:11:16,276 right click and 21297 18:11:13,756 --> 18:11:19,116 paste so this method did not have a 21298 18:11:16,276 --> 18:11:22,636 comma at the end so this looks 21299 18:11:19,116 --> 18:11:24,756 good so that's how we create a class we 21300 18:11:22,636 --> 18:11:27,436 basically give it some properties and 21301 18:11:24,756 --> 18:11:29,756 methods and now when we generate objects 21302 18:11:27,436 --> 18:11:34,276 using this class every object that we 21303 18:11:29,756 --> 18:11:34,276 generate will have these properties and 21304 18:11:34,916 --> 18:11:40,116 methods now one thing we need to fix 21305 18:11:37,200 --> 18:11:43,276 here is that local storage key doesn't 21306 18:11:40,116 --> 18:11:46,756 exist anymore to fix this we're going to 21307 18:11:43,276 --> 18:11:47,636 save this in a property instead so at 21308 18:11:46,756 --> 18:11:49,636 the top 21309 18:11:47,636 --> 18:11:52,796 we're going to give a new property to 21310 18:11:49,636 --> 18:11:58,360 every object that we generate called 21311 18:11:52,796 --> 18:12:00,596 local storage key and make it equal to 21312 18:11:58,360 --> 18:12:03,476 undefined to 21313 18:12:00,596 --> 18:12:07,596 start and then inside the methods we're 21314 18:12:03,476 --> 18:12:10,956 going to switch this to this. local 21315 18:12:07,596 --> 18:12:12,956 storage key so this is going to point to 21316 18:12:10,956 --> 18:12:16,156 the object that we generate just like 21317 18:12:12,956 --> 18:12:19,116 before and then local storage key will 21318 18:12:16,156 --> 18:12:19,116 access this property 21319 18:12:19,400 --> 18:12:24,436 and another place we need to fix this is 21320 18:12:21,600 --> 18:12:27,196 inside save to storage so instead of 21321 18:12:24,436 --> 18:12:30,720 using a variable local storage key we're 21322 18:12:27,196 --> 18:12:33,080 going to use the property this. local 21323 18:12:30,720 --> 18:12:37,400 storage 21324 18:12:33,080 --> 18:12:40,196 key and that's it so if we scroll 21325 18:12:37,400 --> 18:12:43,516 down we basically converted this whole 21326 18:12:40,196 --> 18:12:46,876 function into a class so now we can 21327 18:12:43,516 --> 18:12:46,876 remove it 21328 18:12:49,156 --> 18:12:55,556 and the final step here is now that we 21329 18:12:52,156 --> 18:12:57,196 have this class let's use it to generate 21330 18:12:55,556 --> 18:13:01,476 these two objects 21331 18:12:57,196 --> 18:13:04,400 again to do that instead of this code 21332 18:13:01,476 --> 18:13:06,400 we're going to type the word new and 21333 18:13:04,400 --> 18:13:10,720 then type the class name which is 21334 18:13:06,400 --> 18:13:13,640 capital c cart and 21335 18:13:10,720 --> 18:13:16,360 brackets so you see that a class uses a 21336 18:13:13,640 --> 18:13:19,476 similar syntax as a function except we 21337 18:13:16,360 --> 18:13:23,360 use the word new in front of 21338 18:13:19,476 --> 18:13:25,000 it so this generates a new object using 21339 18:13:23,360 --> 18:13:27,796 our 21340 18:13:25,000 --> 18:13:30,680 class and this new object will have all 21341 18:13:27,796 --> 18:13:33,116 the properties and methods that we added 21342 18:13:30,680 --> 18:13:35,156 above so let's do the same thing for the 21343 18:13:33,116 --> 18:13:38,116 business card we'll use the class to 21344 18:13:35,156 --> 18:13:41,756 generate it we'll remove this 21345 18:13:38,116 --> 18:13:46,360 code and then we'll type new and the 21346 18:13:41,756 --> 18:13:50,600 class name Capital C cart and brackets 21347 18:13:46,360 --> 18:13:52,640 to generate another object using this 21348 18:13:50,600 --> 18:13:55,360 class now there's one thing we need to 21349 18:13:52,640 --> 18:13:58,080 fix here each of these objects has a 21350 18:13:55,360 --> 18:14:00,040 property called local storage key and 21351 18:13:58,080 --> 18:14:02,196 they're currently undefined so we need 21352 18:14:00,040 --> 18:14:05,436 to set this 21353 18:14:02,196 --> 18:14:07,836 property so at the bottom here we're 21354 18:14:05,436 --> 18:14:12,720 going to type 21355 18:14:07,836 --> 18:14:14,556 cart. local storage key and we'll make 21356 18:14:12,720 --> 18:14:17,196 it 21357 18:14:14,556 --> 18:14:21,244 equal and we'll use the same string as 21358 18:14:17,196 --> 18:14:23,516 before which is C- 21359 18:14:21,244 --> 18:14:26,600 oop and we'll do the same thing for the 21360 18:14:23,516 --> 18:14:28,796 business cart we'll type the 21361 18:14:26,600 --> 18:14:32,796 business 21362 18:14:28,796 --> 18:14:35,876 cart. local storage 21363 18:14:32,796 --> 18:14:41,756 key and make it equal the same string 21364 18:14:35,876 --> 18:14:41,756 that we had before which is cart Das 21365 18:14:42,080 --> 18:14:47,320 business and then we'll load the cart 21366 18:14:44,600 --> 18:14:50,400 from Storage as well as the business C 21367 18:14:47,320 --> 18:14:53,756 from storage and we'll console.log 21368 18:14:50,400 --> 18:14:55,876 them so we have the same code as before 21369 18:14:53,756 --> 18:14:57,244 except we converted it from a function 21370 18:14:55,876 --> 18:14:59,756 to a 21371 18:14:57,244 --> 18:15:03,360 class so now let's 21372 18:14:59,756 --> 18:15:06,080 save and we're going to run this code So 21373 18:15:03,360 --> 18:15:07,720 currently if we go into our code and 21374 18:15:06,080 --> 18:15:11,000 open checkout. 21375 18:15:07,720 --> 18:15:13,516 JS we're still loading C- 21376 18:15:11,000 --> 18:15:14,756 oop so we're just going to update this 21377 18:15:13,516 --> 18:15:19,080 and load 21378 18:15:14,756 --> 18:15:22,400 C-Class instead and 21379 18:15:19,080 --> 18:15:26,080 save and we'll open cart Das class 21380 18:15:22,400 --> 18:15:28,796 again and now if we open the 21381 18:15:26,080 --> 18:15:31,796 console you can see that we created two 21382 18:15:28,796 --> 18:15:34,244 cart objects just like we did 21383 18:15:31,796 --> 18:15:37,000 before and the regular card has three 21384 18:15:34,244 --> 18:15:39,360 products inside and the business card 21385 18:15:37,000 --> 18:15:42,796 has two products 21386 18:15:39,360 --> 18:15:45,640 inside and if we click this Arrow beside 21387 18:15:42,796 --> 18:15:48,000 prototype we can see the methods that 21388 18:15:45,640 --> 18:15:49,960 each of these objects have 21389 18:15:48,000 --> 18:15:52,600 so we're basically creating the same 21390 18:15:49,960 --> 18:15:54,796 objects that we had before except this 21391 18:15:52,600 --> 18:15:58,280 time we used a 21392 18:15:54,796 --> 18:16:00,876 class by the way each object that we 21393 18:15:58,280 --> 18:16:02,876 generate from a class is called an 21394 18:16:00,876 --> 18:16:06,476 instance of the 21395 18:16:02,876 --> 18:16:10,920 class we can also check if an object is 21396 18:16:06,476 --> 18:16:16,116 an instance of a class using the code 21397 18:16:10,920 --> 18:16:19,080 business cart and then instance of and 21398 18:16:16,116 --> 18:16:21,920 then the class name name Capital C 21399 18:16:19,080 --> 18:16:25,320 cart so this will check if this object 21400 18:16:21,920 --> 18:16:28,360 was generated from this class so we can 21401 18:16:25,320 --> 18:16:32,720 console.log this result 21402 18:16:28,360 --> 18:16:34,876 console.log and close bracket and 21403 18:16:32,720 --> 18:16:37,320 save and then check the 21404 18:16:34,876 --> 18:16:41,156 console and it will tell us that this is 21405 18:16:37,320 --> 18:16:43,436 true so business card was generated from 21406 18:16:41,156 --> 18:16:43,436 this 21407 18:16:43,796 --> 18:16:50,196 class okay don't worry if class are a 21408 18:16:47,516 --> 18:16:52,796 bit confusing at first just remember 21409 18:16:50,196 --> 18:16:56,080 that objectoriented programming means 21410 18:16:52,796 --> 18:16:59,116 organizing our code into objects because 21411 18:16:56,080 --> 18:17:02,280 this tries to represent the real 21412 18:16:59,116 --> 18:17:05,360 world and then classes are a feature 21413 18:17:02,280 --> 18:17:08,680 that help us generate these objects so 21414 18:17:05,360 --> 18:17:12,196 they make it easier to create these 21415 18:17:08,680 --> 18:17:15,196 objects a class is basically an object 21416 18:17:12,196 --> 18:17:15,196 generator 21417 18:17:17,156 --> 18:17:21,796 now that we learned how classes work 21418 18:17:19,320 --> 18:17:24,796 let's talk about the benefits of 21419 18:17:21,796 --> 18:17:28,000 classes if we scroll 21420 18:17:24,796 --> 18:17:30,320 up when we generate an object it's going 21421 18:17:28,000 --> 18:17:33,116 to have these properties and these 21422 18:17:30,320 --> 18:17:37,400 methods and you'll notice that a class 21423 18:17:33,116 --> 18:17:37,400 actually looks like the object that we 21424 18:17:37,756 --> 18:17:44,876 generate and this is cleaner than using 21425 18:17:40,436 --> 18:17:44,876 a function like we had before 21426 18:17:46,836 --> 18:17:53,640 another benefit of classes is that they 21427 18:17:49,080 --> 18:17:53,640 have extra features for object-oriented 21428 18:17:54,080 --> 18:17:59,196 programming the first feature is called 21429 18:17:56,516 --> 18:18:02,516 a Constructor which lets us run some 21430 18:17:59,196 --> 18:18:06,796 setup code after creating an 21431 18:18:02,516 --> 18:18:06,796 object so if we scroll down in our 21432 18:18:06,836 --> 18:18:13,516 code you'll notice that after we create 21433 18:18:09,680 --> 18:18:16,116 these objects we run some setup code a 21434 18:18:13,516 --> 18:18:19,000 Constructor lets us put the setup code 21435 18:18:16,116 --> 18:18:21,556 inside the class and this makes our code 21436 18:18:19,000 --> 18:18:24,000 cleaner because we can group everything 21437 18:18:21,556 --> 18:18:26,796 together inside the 21438 18:18:24,000 --> 18:18:30,196 class to create a Constructor we're 21439 18:18:26,796 --> 18:18:33,196 going to scroll up in the 21440 18:18:30,196 --> 18:18:37,400 class and Below these properties we're 21441 18:18:33,196 --> 18:18:41,640 going to create a new method called 21442 18:18:37,400 --> 18:18:44,796 Constructor brackets and curly 21443 18:18:41,640 --> 18:18:47,516 brackets so the Constructor method works 21444 18:18:44,796 --> 18:18:49,836 like a normal method we can put code 21445 18:18:47,516 --> 18:18:52,156 inside and it will run the 21446 18:18:49,836 --> 18:18:54,680 code but the special thing about the 21447 18:18:52,156 --> 18:18:57,516 Constructor is that when we generate an 21448 18:18:54,680 --> 18:18:59,680 object it will run this Constructor 21449 18:18:57,516 --> 18:19:01,796 automatically so this is a great place 21450 18:18:59,680 --> 18:19:05,600 to put our setup 21451 18:19:01,796 --> 18:19:08,600 code so let's scroll down to the 21452 18:19:05,600 --> 18:19:11,196 bottom and here instead of running the 21453 18:19:08,600 --> 18:19:13,600 setup code outside the class we're just 21454 18:19:11,196 --> 18:19:16,244 going to move this into the class and 21455 18:19:13,600 --> 18:19:18,080 into the Constructor so we'll select 21456 18:19:16,244 --> 18:19:21,196 this code 21457 18:19:18,080 --> 18:19:24,360 and then right click 21458 18:19:21,196 --> 18:19:27,080 cut and scroll to the 21459 18:19:24,360 --> 18:19:30,960 top and inside the Constructor we're 21460 18:19:27,080 --> 18:19:30,960 going to rightclick and 21461 18:19:31,476 --> 18:19:38,320 paste so now after we create an object 21462 18:19:35,680 --> 18:19:41,244 it will run the code in the Constructor 21463 18:19:38,320 --> 18:19:43,320 and set up the 21464 18:19:41,244 --> 18:19:46,400 object so there's a few things that we 21465 18:19:43,320 --> 18:19:48,156 need to fix here the first thing is each 21466 18:19:46,400 --> 18:19:51,196 object object that we create is going to 21467 18:19:48,156 --> 18:19:54,040 run the Constructor so we only need one 21468 18:19:51,196 --> 18:19:57,400 of each of these lines so let's remove 21469 18:19:54,040 --> 18:19:57,400 the setup for the business 21470 18:20:02,320 --> 18:20:08,680 card so now the setup code has two steps 21471 18:20:06,116 --> 18:20:12,244 we set the local storage key and then we 21472 18:20:08,680 --> 18:20:14,756 load from Storage the next problem is 21473 18:20:12,244 --> 18:20:18,000 that the object that we create is not 21474 18:20:14,756 --> 18:20:21,476 going to be called cart every time 21475 18:20:18,000 --> 18:20:25,836 to solve this we can replace cart with 21476 18:20:21,476 --> 18:20:29,680 this and here as well this. load from 21477 18:20:25,836 --> 18:20:32,360 Storage so as a reminder this points to 21478 18:20:29,680 --> 18:20:34,196 the object that we generate so whatever 21479 18:20:32,360 --> 18:20:37,360 object that we generate we're going to 21480 18:20:34,196 --> 18:20:39,040 get its local storage key and set it and 21481 18:20:37,360 --> 18:20:42,600 we're going to get its load from Storage 21482 18:20:39,040 --> 18:20:42,600 method and run that 21483 18:20:42,836 --> 18:20:47,836 method now the second problem here is 21484 18:20:45,720 --> 18:20:50,360 that we don't want local storage key to 21485 18:20:47,836 --> 18:20:53,640 be card- oop every 21486 18:20:50,360 --> 18:20:56,280 time to fix this the Constructor can 21487 18:20:53,640 --> 18:20:58,920 also have parameters let's give the 21488 18:20:56,280 --> 18:21:00,196 Constructor a parameter and we'll name 21489 18:20:58,920 --> 18:21:04,796 it 21490 18:21:00,196 --> 18:21:07,280 local storage key and instead of the 21491 18:21:04,796 --> 18:21:11,436 same string every time we're going to 21492 18:21:07,280 --> 18:21:14,516 use local storage 21493 18:21:11,436 --> 18:21:17,960 key now the last thing is how do we save 21494 18:21:14,516 --> 18:21:21,040 a value into this parameter 21495 18:21:17,960 --> 18:21:21,040 so if we scroll down to the 21496 18:21:22,244 --> 18:21:27,960 bottom when we call the class between 21497 18:21:25,556 --> 18:21:31,196 these brackets we can put the parameters 21498 18:21:27,960 --> 18:21:33,040 for the Constructor so for the cart we 21499 18:21:31,196 --> 18:21:38,640 want the local storage key to be the 21500 18:21:33,040 --> 18:21:40,876 string C- oop and for the business cart 21501 18:21:38,640 --> 18:21:44,280 we want the local storage key to be the 21502 18:21:40,876 --> 18:21:47,796 string cart Das 21503 18:21:44,280 --> 18:21:49,960 business so the values inside here are 21504 18:21:47,796 --> 18:21:53,720 saved in the parameters in the 21505 18:21:49,960 --> 18:21:56,080 Constructor so C- oop will be saved 21506 18:21:53,720 --> 18:21:58,920 inside local storage key and then we use 21507 18:21:56,080 --> 18:22:02,756 it and then the same thing for cart D 21508 18:21:58,920 --> 18:22:06,720 business so now let's save this 21509 18:22:02,756 --> 18:22:06,720 code and then open the 21510 18:22:07,280 --> 18:22:12,040 console and you'll see that the cart 21511 18:22:09,280 --> 18:22:15,476 objects are correct so after we 21512 18:22:12,040 --> 18:22:18,360 generated each object we also ran this 21513 18:22:15,476 --> 18:22:21,360 setup code so that's how the Constructor 21514 18:22:18,360 --> 18:22:24,516 works every time we generate an object 21515 18:22:21,360 --> 18:22:27,080 it will run the code inside here so this 21516 18:22:24,516 --> 18:22:29,836 is a great place to put setup code and 21517 18:22:27,080 --> 18:22:29,836 make our code 21518 18:22:30,000 --> 18:22:33,720 cleaner so here are a few more details 21519 18:22:32,720 --> 18:22:36,360 about the 21520 18:22:33,720 --> 18:22:38,600 Constructor the method has to be named 21521 18:22:36,360 --> 18:22:41,436 Constructor we can't just use any name 21522 18:22:38,600 --> 18:22:44,516 that we want we also should not return 21523 18:22:41,436 --> 18:22:44,516 anything from a 21524 18:22:44,960 --> 18:22:50,720 Constructor so last ly a shortcut that 21525 18:22:47,796 --> 18:22:53,436 we can use in a class is when we have a 21526 18:22:50,720 --> 18:22:56,756 property and it's equal undefined we can 21527 18:22:53,436 --> 18:22:59,360 actually remove equal undefined and just 21528 18:22:56,756 --> 18:23:02,796 use a semicolon so this is the same 21529 18:22:59,360 --> 18:23:06,476 thing as the code before and same thing 21530 18:23:02,796 --> 18:23:11,000 here we can remove equal undefined and 21531 18:23:06,476 --> 18:23:13,756 just use a semicolon and 21532 18:23:11,000 --> 18:23:16,796 save so we finished converting our code 21533 18:23:13,756 --> 18:23:19,436 from using a function to using a class 21534 18:23:16,796 --> 18:23:22,156 and as you can see a class is just a 21535 18:23:19,436 --> 18:23:25,156 better way to generate objects in 21536 18:23:22,156 --> 18:23:25,156 objectoriented 21537 18:23:25,680 --> 18:23:31,680 programming so now let's save our code 21538 18:23:27,920 --> 18:23:34,796 into git we'll open the git section 21539 18:23:31,680 --> 18:23:38,836 inside cart Das class we created our 21540 18:23:34,796 --> 18:23:41,116 cart class and inside checkout. JS we're 21541 18:23:38,836 --> 18:23:43,796 basically loading the cart Das class 21542 18:23:41,116 --> 18:23:47,040 file so we'll type a message to describe 21543 18:23:43,796 --> 18:23:48,556 this change create 21544 18:23:47,040 --> 18:23:54,000 class 21545 18:23:48,556 --> 18:23:54,000 version of cart and 21546 18:23:59,400 --> 18:24:04,556 commit next we're going to learn an 21547 18:24:01,920 --> 18:24:07,400 important feature of classes called 21548 18:24:04,556 --> 18:24:10,556 private properties and 21549 18:24:07,400 --> 18:24:12,836 methods so let's open our code again and 21550 18:24:10,556 --> 18:24:15,196 open 21551 18:24:12,836 --> 18:24:17,876 C-Class and you'll see that this class 21552 18:24:15,196 --> 18:24:20,920 has a property called local storage 21553 18:24:17,876 --> 18:24:23,400 key so this property is useful inside 21554 18:24:20,920 --> 18:24:27,516 the class but it's not really meant to 21555 18:24:23,400 --> 18:24:28,720 be used outside the class for example at 21556 18:24:27,516 --> 18:24:31,920 the bottom 21557 18:24:28,720 --> 18:24:35,116 here we could take this 21558 18:24:31,920 --> 18:24:41,400 object cart and then change the local 21559 18:24:35,116 --> 18:24:43,796 storage key using dot local storage key 21560 18:24:41,400 --> 18:24:46,756 equals something 21561 18:24:43,796 --> 18:24:49,476 else now this is a problem because it 21562 18:24:46,756 --> 18:24:51,320 will mess up how this object works and 21563 18:24:49,476 --> 18:24:54,436 it will start saving to a different 21564 18:24:51,320 --> 18:24:56,960 local storage key so this is a common 21565 18:24:54,436 --> 18:24:59,320 problem in programming where if you're 21566 18:24:56,960 --> 18:25:01,116 working on a team somebody might 21567 18:24:59,320 --> 18:25:04,436 accidentally change properties that 21568 18:25:01,116 --> 18:25:06,796 they're not supposed to to prevent this 21569 18:25:04,436 --> 18:25:09,720 classes let us make a property or a 21570 18:25:06,796 --> 18:25:13,476 method private which means it can only 21571 18:25:09,720 --> 18:25:13,476 be accessed inside the 21572 18:25:14,000 --> 18:25:18,244 class so let's remove this line 21573 18:25:18,600 --> 18:25:24,436 and scroll 21574 18:25:20,400 --> 18:25:27,080 up and to make local storage key private 21575 18:25:24,436 --> 18:25:28,796 we're just going to add a number sign in 21576 18:25:27,080 --> 18:25:31,756 front or a 21577 18:25:28,796 --> 18:25:34,320 hash and also whenever we access this 21578 18:25:31,756 --> 18:25:37,116 property inside the class we're going to 21579 18:25:34,320 --> 18:25:41,876 add the hash in front so we'll type 21580 18:25:37,116 --> 18:25:46,920 this. has local storage key this.h local 21581 18:25:41,876 --> 18:25:49,556 storage key and inside save to storage 21582 18:25:46,920 --> 18:25:53,600 this.h local storage 21583 18:25:49,556 --> 18:25:56,556 key so this is called a private property 21584 18:25:53,600 --> 18:25:58,960 this means it can only be used inside 21585 18:25:56,556 --> 18:26:02,280 this class or inside these curly 21586 18:25:58,960 --> 18:26:05,516 brackets it cannot be used outside the 21587 18:26:02,280 --> 18:26:08,476 class a property without a hash in front 21588 18:26:05,516 --> 18:26:11,040 is called a public property meaning it 21589 18:26:08,476 --> 18:26:13,720 can be accessed 21590 18:26:11,040 --> 18:26:18,156 anywhere so now let's 21591 18:26:13,720 --> 18:26:18,156 save and open the console 21592 18:26:19,320 --> 18:26:22,436 and the code still 21593 18:26:22,476 --> 18:26:28,556 works so we made this property private 21594 18:26:25,720 --> 18:26:31,040 and we're able to access it inside the 21595 18:26:28,556 --> 18:26:33,280 class so now let's try accessing this 21596 18:26:31,040 --> 18:26:36,960 property outside the class to see what 21597 18:26:33,280 --> 18:26:41,000 happens we're going to scroll 21598 18:26:36,960 --> 18:26:46,876 down and we're going to try to access 21599 18:26:41,000 --> 18:26:50,080 cart. hash local storage key 21600 18:26:46,876 --> 18:26:52,796 and change it to something else like 21601 18:26:50,080 --> 18:26:55,836 test and 21602 18:26:52,796 --> 18:26:58,436 save and if we open our 21603 18:26:55,836 --> 18:27:01,196 console you'll see that when we try to 21604 18:26:58,436 --> 18:27:04,156 access a private property here it will 21605 18:27:01,196 --> 18:27:07,436 give us a syntax error saying this is a 21606 18:27:04,156 --> 18:27:10,436 private field and by the way a field is 21607 18:27:07,436 --> 18:27:10,436 the same thing as a 21608 18:27:10,796 --> 18:27:15,196 property so that's how a private 21609 18:27:13,000 --> 18:27:16,756 property works it prevents us from 21610 18:27:15,196 --> 18:27:19,640 accessing this property 21611 18:27:16,756 --> 18:27:23,876 outside the object and messing it 21612 18:27:19,640 --> 18:27:26,960 up we can also have private 21613 18:27:23,876 --> 18:27:30,680 methods so let's remove this 21614 18:27:26,960 --> 18:27:30,680 line and scroll 21615 18:27:31,244 --> 18:27:37,516 up and the method load from Storage 21616 18:27:35,040 --> 18:27:40,080 should also only be used inside this 21617 18:27:37,516 --> 18:27:43,080 class there's really no reason for code 21618 18:27:40,080 --> 18:27:45,320 outside to be calling load from Storage 21619 18:27:43,080 --> 18:27:47,436 so to make our code safer we're going to 21620 18:27:45,320 --> 18:27:50,436 make this a private 21621 18:27:47,436 --> 18:27:52,720 method to make a method private we're 21622 18:27:50,436 --> 18:27:54,876 going to do the same thing in front of 21623 18:27:52,720 --> 18:27:58,320 the method we're going to add a number 21624 18:27:54,876 --> 18:28:00,400 sign or a hash and we're going to update 21625 18:27:58,320 --> 18:28:04,556 the rest of our code so we're going to 21626 18:28:00,400 --> 18:28:07,156 do this dot hash load from 21627 18:28:04,556 --> 18:28:10,080 storage and now we can 21628 18:28:07,156 --> 18:28:12,876 save and check the console to make sure 21629 18:28:10,080 --> 18:28:16,556 that everything is still working so 21630 18:28:12,876 --> 18:28:18,680 everything still looks good 21631 18:28:16,556 --> 18:28:21,876 because we're only using this private 21632 18:28:18,680 --> 18:28:25,436 method inside this 21633 18:28:21,876 --> 18:28:27,720 class so a class Let's us create private 21634 18:28:25,436 --> 18:28:30,244 properties and methods and this is 21635 18:28:27,720 --> 18:28:32,756 useful on a bigger project where people 21636 18:28:30,244 --> 18:28:34,920 might not be sure which properties and 21637 18:28:32,756 --> 18:28:37,876 methods they're supposed to use outside 21638 18:28:34,920 --> 18:28:40,876 the class and which ones to 21639 18:28:37,876 --> 18:28:44,680 avoid so now let's save our changes into 21640 18:28:40,876 --> 18:28:47,720 git we'll go into the git section and 21641 18:28:44,680 --> 18:28:50,876 inside here we we made local storage key 21642 18:28:47,720 --> 18:28:53,080 and load from Storage 21643 18:28:50,876 --> 18:28:57,516 private we'll create a message to 21644 18:28:53,080 --> 18:29:03,960 describe these changes make local 21645 18:28:57,516 --> 18:29:07,320 storage key and load from Storage 21646 18:29:03,960 --> 18:29:07,320 private and 21647 18:29:12,476 --> 18:29:16,280 commit now that we learned 21648 18:29:14,360 --> 18:29:19,720 objectoriented programming and and 21649 18:29:16,280 --> 18:29:22,796 classes we're going to use it in our 21650 18:29:19,720 --> 18:29:25,280 project so one thing we could do is to 21651 18:29:22,796 --> 18:29:28,436 take this card class and replace 21652 18:29:25,280 --> 18:29:30,876 everywhere we use cart in our project 21653 18:29:28,436 --> 18:29:32,876 however we use cart in a lot of places 21654 18:29:30,876 --> 18:29:35,080 and this would take too much time so I'm 21655 18:29:32,876 --> 18:29:36,244 going to leave replacing the cart as a 21656 18:29:35,080 --> 18:29:39,080 challenge 21657 18:29:36,244 --> 18:29:41,320 exercise instead we're going to work on 21658 18:29:39,080 --> 18:29:44,556 the 21659 18:29:41,320 --> 18:29:47,960 homepage so first let's go back to our 21660 18:29:44,556 --> 18:29:50,756 code and we're going to open checkout. 21661 18:29:47,960 --> 18:29:53,196 JS and we're just going to comment out 21662 18:29:50,756 --> 18:29:56,556 this import because we're just using 21663 18:29:53,196 --> 18:29:59,000 this file for practice so let's save 21664 18:29:56,556 --> 18:30:01,960 this and then let's open the checkout 21665 18:29:59,000 --> 18:30:04,156 page so I already have it open if you 21666 18:30:01,960 --> 18:30:09,400 don't have it open you can rightclick 21667 18:30:04,156 --> 18:30:09,400 checkout. HTML and open with live 21668 18:30:11,040 --> 18:30:16,516 server on the checkout page we're going 21669 18:30:13,556 --> 18:30:18,920 to click this icon in the top left to go 21670 18:30:16,516 --> 18:30:18,920 to the 21671 18:30:19,080 --> 18:30:24,640 homepage on this page we have a list of 21672 18:30:22,600 --> 18:30:28,476 products 21673 18:30:24,640 --> 18:30:30,400 here and the code for these products is 21674 18:30:28,476 --> 18:30:33,244 if we go back to vs 21675 18:30:30,400 --> 18:30:34,640 code and on the left side we're going to 21676 18:30:33,244 --> 18:30:37,640 open 21677 18:30:34,640 --> 18:30:37,640 dataproducts 21678 18:30:38,080 --> 18:30:44,516 DJs so here is the code or the data for 21679 18:30:42,000 --> 18:30:47,516 all the products on the homepage and 21680 18:30:44,516 --> 18:30:49,960 you'll notice that each product is an 21681 18:30:47,516 --> 18:30:54,196 object we're going to learn a technique 21682 18:30:49,960 --> 18:30:57,156 called converting an object into a 21683 18:30:54,196 --> 18:30:59,836 class so what this means is instead of 21684 18:30:57,156 --> 18:31:02,476 using regular objects in this array 21685 18:30:59,836 --> 18:31:06,244 we're going to use a class to generate 21686 18:31:02,476 --> 18:31:10,720 these objects so Above This array let's 21687 18:31:06,244 --> 18:31:14,516 create a new class using the word class 21688 18:31:10,720 --> 18:31:17,116 and let's call it capital P product and 21689 18:31:14,516 --> 18:31:20,360 curly brackets 21690 18:31:17,116 --> 18:31:23,040 because this class is meant to generate 21691 18:31:20,360 --> 18:31:25,040 products so now each of our products is 21692 18:31:23,040 --> 18:31:28,436 going to have some properties like the 21693 18:31:25,040 --> 18:31:31,960 ID image and the name so let's add these 21694 18:31:28,436 --> 18:31:34,960 to the class We'll add the ID property 21695 18:31:31,960 --> 18:31:38,400 and a semicolon so that it starts off as 21696 18:31:34,960 --> 18:31:40,000 undefined and then an image property and 21697 18:31:38,400 --> 18:31:42,556 a name 21698 18:31:40,000 --> 18:31:47,920 property and each product will also have 21699 18:31:42,556 --> 18:31:49,876 a rating and the price sets rating and 21700 18:31:47,920 --> 18:31:52,600 price 21701 18:31:49,876 --> 18:31:55,516 set and you'll notice each object here 21702 18:31:52,600 --> 18:31:57,280 also has a property called keywords now 21703 18:31:55,516 --> 18:31:59,280 we're not currently using this in our 21704 18:31:57,280 --> 18:32:01,920 project so we're going to leave it out 21705 18:31:59,280 --> 18:32:04,876 of the class for now and if we need to 21706 18:32:01,920 --> 18:32:07,516 use it later we can always add it back 21707 18:32:04,876 --> 18:32:10,516 in so now we have all the properties 21708 18:32:07,516 --> 18:32:13,080 that we want for each product so now 21709 18:32:10,516 --> 18:32:16,280 let's test this by generating a product 21710 18:32:13,080 --> 18:32:20,244 using this class so that here 21711 18:32:16,280 --> 18:32:24,080 let's create a variable const product 21712 18:32:20,244 --> 18:32:26,796 one equals and to generate an object 21713 18:32:24,080 --> 18:32:32,796 we're going to use the word new and then 21714 18:32:26,796 --> 18:32:35,600 the class name capital P product and 21715 18:32:32,796 --> 18:32:38,556 brackets and let's console.log this to 21716 18:32:35,600 --> 18:32:43,836 see if it worked 21717 18:32:38,556 --> 18:32:46,196 console.log product one and 21718 18:32:43,836 --> 18:32:50,196 save so now we're going to open the web 21719 18:32:46,196 --> 18:32:52,320 page and then open the console so right 21720 18:32:50,196 --> 18:32:54,876 click and 21721 18:32:52,320 --> 18:32:57,436 inspect and open the 21722 18:32:54,876 --> 18:33:01,196 console and inside here you'll see that 21723 18:32:57,436 --> 18:33:03,720 we created a product object using the 21724 18:33:01,196 --> 18:33:06,280 class so right now all of these 21725 18:33:03,720 --> 18:33:09,640 properties are undefined so now let's 21726 18:33:06,280 --> 18:33:11,196 set the values of these properties we'll 21727 18:33:09,640 --> 18:33:14,876 go back to our 21728 18:33:11,196 --> 18:33:16,920 code don't follow along here but one way 21729 18:33:14,876 --> 18:33:19,156 to set set these prop properties is to 21730 18:33:16,920 --> 18:33:23,516 access them outside the 21731 18:33:19,156 --> 18:33:28,000 class for example product 21732 18:33:23,516 --> 18:33:32,000 1. equals something and then do product 21733 18:33:28,000 --> 18:33:35,516 1. image equals something and then 21734 18:33:32,000 --> 18:33:38,360 product one. name and so 21735 18:33:35,516 --> 18:33:40,320 on so this is not a good way to set 21736 18:33:38,360 --> 18:33:42,836 these properties because we have to do 21737 18:33:40,320 --> 18:33:46,116 this for each product that we create so 21738 18:33:42,836 --> 18:33:48,600 it makes our code repetitive and messy 21739 18:33:46,116 --> 18:33:51,400 instead notice that when we create a 21740 18:33:48,600 --> 18:33:53,920 product this is kind of like the setup 21741 18:33:51,400 --> 18:33:55,436 code we create a product and then set 21742 18:33:53,920 --> 18:33:58,400 all of its 21743 18:33:55,436 --> 18:34:01,196 properties and remember classes have a 21744 18:33:58,400 --> 18:34:03,000 specific feature for setup code called 21745 18:34:01,196 --> 18:34:05,836 The 21746 18:34:03,000 --> 18:34:08,960 Constructor and now you can follow 21747 18:34:05,836 --> 18:34:12,244 along so inside this class we're going 21748 18:34:08,960 --> 18:34:16,876 to create a 21749 18:34:12,244 --> 18:34:19,360 Constructor brackets and curly brackets 21750 18:34:16,876 --> 18:34:21,360 so whenever we generate an object it's 21751 18:34:19,360 --> 18:34:24,040 going to automatically run this 21752 18:34:21,360 --> 18:34:26,516 Constructor so this Constructor is where 21753 18:34:24,040 --> 18:34:30,116 we should put our setup 21754 18:34:26,516 --> 18:34:33,796 code so let's remove this 21755 18:34:30,116 --> 18:34:36,516 code and then do all of that inside the 21756 18:34:33,796 --> 18:34:41,756 Constructor so first we want to set the 21757 18:34:36,516 --> 18:34:45,080 ID property we're going to do this do ID 21758 18:34:41,756 --> 18:34:48,920 and make it equal and now what value do 21759 18:34:45,080 --> 18:34:51,640 we set the ID to so the value of the ID 21760 18:34:48,920 --> 18:34:54,920 is inside each of these 21761 18:34:51,640 --> 18:34:57,640 objects so when we create a product up 21762 18:34:54,920 --> 18:34:59,796 here we're going to put this object 21763 18:34:57,640 --> 18:35:01,836 inside these 21764 18:34:59,796 --> 18:35:05,080 brackets and then that's going to go 21765 18:35:01,836 --> 18:35:07,436 into the Constructor in here so in the 21766 18:35:05,080 --> 18:35:13,156 Constructor let's create a parameter to 21767 18:35:07,436 --> 18:35:15,556 save this data so we'll name it product 21768 18:35:13,156 --> 18:35:17,876 details and now to save something inside 21769 18:35:15,556 --> 18:35:20,116 ins this parameter we're going to put it 21770 18:35:17,876 --> 18:35:22,244 between these 21771 18:35:20,116 --> 18:35:25,920 brackets so let's actually give this 21772 18:35:22,244 --> 18:35:25,920 entire object to the 21773 18:35:26,040 --> 18:35:32,196 Constructor so we're going to right 21774 18:35:28,796 --> 18:35:32,196 click and 21775 18:35:32,920 --> 18:35:39,476 copy and then between these brackets 21776 18:35:35,400 --> 18:35:41,920 we're going to rightclick and 21777 18:35:39,476 --> 18:35:44,000 paste so whenever we create a new 21778 18:35:41,920 --> 18:35:46,080 product we're going to give it this 21779 18:35:44,000 --> 18:35:48,680 object with all the details 21780 18:35:46,080 --> 18:35:50,680 it's going to get saved in here and now 21781 18:35:48,680 --> 18:35:53,680 we can set the 21782 18:35:50,680 --> 18:35:56,836 properties so this. ID is going to be 21783 18:35:53,680 --> 18:36:00,360 equal to the product details. ID 21784 18:35:56,836 --> 18:36:02,600 property so product 21785 18:36:00,360 --> 18:36:06,436 details. 21786 18:36:02,600 --> 18:36:12,600 ID and for the image we're going to do 21787 18:36:06,436 --> 18:36:15,556 this. image equals the product details 21788 18:36:12,600 --> 18:36:18,244 which is this object that we passed in 21789 18:36:15,556 --> 18:36:21,680 dot the image 21790 18:36:18,244 --> 18:36:26,080 property same thing for the name so this 21791 18:36:21,680 --> 18:36:29,720 do name equals product 21792 18:36:26,080 --> 18:36:33,640 details. name which is this property 21793 18:36:29,720 --> 18:36:39,040 here same thing for the rating this. 21794 18:36:33,640 --> 18:36:41,920 rating equals product details. 21795 18:36:39,040 --> 18:36:47,720 rating and finally the price 21796 18:36:41,920 --> 18:36:49,516 sense this. price sense equals the 21797 18:36:47,720 --> 18:36:52,516 product 21798 18:36:49,516 --> 18:36:56,040 details. Price 21799 18:36:52,516 --> 18:37:00,476 sets so this is called converting an 21800 18:36:56,040 --> 18:37:02,836 object into a class what we're doing is 21801 18:37:00,476 --> 18:37:07,116 we're basically taking a regular object 21802 18:37:02,836 --> 18:37:09,960 like this we put it inside a class and 21803 18:37:07,116 --> 18:37:12,280 then we copy over the 21804 18:37:09,960 --> 18:37:15,196 properties you can also visualize it 21805 18:37:12,280 --> 18:37:17,920 like this we have a regular object and 21806 18:37:15,196 --> 18:37:20,640 we're basically wrapping it inside a 21807 18:37:17,920 --> 18:37:23,640 class and the reason we do this is that 21808 18:37:20,640 --> 18:37:26,244 classes have extra features we can use 21809 18:37:23,640 --> 18:37:29,280 like we can add methods and we can also 21810 18:37:26,244 --> 18:37:31,920 have private properties and private 21811 18:37:29,280 --> 18:37:34,640 methods so we basically have these same 21812 18:37:31,920 --> 18:37:38,000 object as before except we have an 21813 18:37:34,640 --> 18:37:41,556 enhanced version of that object it has 21814 18:37:38,000 --> 18:37:41,556 some extra features from 21815 18:37:41,920 --> 18:37:49,156 classes so for now let's save this code 21816 18:37:46,556 --> 18:37:51,436 and then down here we console.log 21817 18:37:49,156 --> 18:37:54,244 product one so let's take a look at it 21818 18:37:51,436 --> 18:37:56,476 in our console so we'll open the 21819 18:37:54,244 --> 18:37:59,516 console and now we can see that we 21820 18:37:56,476 --> 18:38:02,556 created a product object so it's the 21821 18:37:59,516 --> 18:38:07,400 same object as before except now it's 21822 18:38:02,556 --> 18:38:07,400 enhanced it has some extra features from 21823 18:38:08,960 --> 18:38:14,360 classes so next we're going to take this 21824 18:38:11,720 --> 18:38:17,040 technique and apply it to this entire 21825 18:38:14,360 --> 18:38:19,360 array so instead of having an array of 21826 18:38:17,040 --> 18:38:23,116 regular objects we're going to convert 21827 18:38:19,360 --> 18:38:26,600 each of these objects into the product 21828 18:38:23,116 --> 18:38:28,756 class now one way we could do this is 21829 18:38:26,600 --> 18:38:33,800 that in front of the object we can just 21830 18:38:28,756 --> 18:38:35,832 type new product Open Bracket and then 21831 18:38:33,800 --> 18:38:38,440 close bracket at the 21832 18:38:35,832 --> 18:38:41,360 bottom and this will essentially convert 21833 18:38:38,440 --> 18:38:43,512 this regular object into the product 21834 18:38:41,360 --> 18:38:46,360 class however we're going to have to 21835 18:38:43,512 --> 18:38:48,832 repeat this code for every object in the 21836 18:38:46,360 --> 18:38:50,160 array and it's going to be a lot of work 21837 18:38:48,832 --> 18:38:53,360 and 21838 18:38:50,160 --> 18:38:56,080 repetition so instead let's just Loop 21839 18:38:53,360 --> 18:38:58,960 through this array and then use code to 21840 18:38:56,080 --> 18:39:00,960 convert each of these objects so instead 21841 18:38:58,960 --> 18:39:03,480 of typing this out manually we're going 21842 18:39:00,960 --> 18:39:06,360 to remove this and remove the round 21843 18:39:03,480 --> 18:39:09,720 bracket at the end and then I'm going to 21844 18:39:06,360 --> 18:39:10,872 click this Arrow beside the array to 21845 18:39:09,720 --> 18:39:13,832 collapse 21846 18:39:10,872 --> 18:39:17,512 it and at the end of the array we're 21847 18:39:13,832 --> 18:39:20,080 going to use an array method called do 21848 18:39:17,512 --> 18:39:23,120 map and 21849 18:39:20,080 --> 18:39:26,552 brackets so map basically Loops through 21850 18:39:23,120 --> 18:39:28,920 an array and for each value it runs a 21851 18:39:26,552 --> 18:39:31,360 function so we're going to give this a 21852 18:39:28,920 --> 18:39:34,480 function that we want to run for each 21853 18:39:31,360 --> 18:39:36,832 value so arrow and curly 21854 18:39:34,480 --> 18:39:39,040 brackets and this inner function is 21855 18:39:36,832 --> 18:39:42,000 going to get a 21856 18:39:39,040 --> 18:39:43,760 parameter so this parameter is basically 21857 18:39:42,000 --> 18:39:46,400 each value in the 21858 18:39:43,760 --> 18:39:49,000 array so it's going to take the first 21859 18:39:46,400 --> 18:39:51,360 value and then save it inside this 21860 18:39:49,000 --> 18:39:53,600 parameter run the function and then take 21861 18:39:51,360 --> 18:39:57,080 the second value save it in here run the 21862 18:39:53,600 --> 18:40:00,080 function third value save it and run the 21863 18:39:57,080 --> 18:40:02,640 function so let's name this parameter 21864 18:40:00,080 --> 18:40:05,160 the product 21865 18:40:02,640 --> 18:40:08,440 details because each object in this 21866 18:40:05,160 --> 18:40:11,192 array contains product details so now 21867 18:40:08,440 --> 18:40:15,512 it's going to take each object save it 21868 18:40:11,192 --> 18:40:17,360 in the parameter and run the function 21869 18:40:15,512 --> 18:40:20,720 and here we're going to convert the 21870 18:40:17,360 --> 18:40:24,720 object into a class to do that we're 21871 18:40:20,720 --> 18:40:26,720 going to use the code new capital P 21872 18:40:24,720 --> 18:40:29,192 product 21873 18:40:26,720 --> 18:40:34,832 brackets and we're going to give it this 21874 18:40:29,192 --> 18:40:34,832 regular object which is the product 21875 18:40:35,760 --> 18:40:43,360 details and now there's a second part to 21876 18:40:38,440 --> 18:40:45,600 how Map works so map creates a new array 21877 18:40:43,360 --> 18:40:49,552 and whatever we return from this inner 21878 18:40:45,600 --> 18:40:54,360 function is going to go inside that new 21879 18:40:49,552 --> 18:40:57,360 array so here we're going to return this 21880 18:40:54,360 --> 18:41:00,480 class so this result is going to be put 21881 18:40:57,360 --> 18:41:04,120 inside a new array so you can think of 21882 18:41:00,480 --> 18:41:06,512 map as we take each value in an array we 21883 18:41:04,120 --> 18:41:10,832 run this function on it and we transform 21884 18:41:06,512 --> 18:41:10,832 it and then put it inside a new 21885 18:41:10,920 --> 18:41:15,920 array so here we're essentially 21886 18:41:13,552 --> 18:41:19,280 transforming each of these products or 21887 18:41:15,920 --> 18:41:23,600 these regular objects into a 21888 18:41:19,280 --> 18:41:26,320 class so at the bottom let's 21889 18:41:23,600 --> 18:41:29,760 console.log the products array to 21890 18:41:26,320 --> 18:41:32,600 confirm this so we'll 21891 18:41:29,760 --> 18:41:35,280 save and open the 21892 18:41:32,600 --> 18:41:38,192 console and now you can see we have an 21893 18:41:35,280 --> 18:41:40,960 array and each of the values is a 21894 18:41:38,192 --> 18:41:43,960 product class instead of just a regular 21895 18:41:40,960 --> 18:41:43,960 object 21896 18:41:45,512 --> 18:41:51,832 so we converted all of our products from 21897 18:41:47,960 --> 18:41:54,120 regular objects into this product class 21898 18:41:51,832 --> 18:41:55,720 and now let's open the web page to make 21899 18:41:54,120 --> 18:41:58,080 sure that everything still 21900 18:41:55,720 --> 18:42:00,440 works so if we open the 21901 18:41:58,080 --> 18:42:01,680 page you notice that everything is still 21902 18:42:00,440 --> 18:42:04,320 working 21903 18:42:01,680 --> 18:42:07,720 correctly and that's because we have the 21904 18:42:04,320 --> 18:42:11,720 exact same objects as before except 21905 18:42:07,720 --> 18:42:14,720 they're now just sort of enhanced 21906 18:42:11,720 --> 18:42:17,920 objects next let's use those extra 21907 18:42:14,720 --> 18:42:21,320 features of classes to simplify our code 21908 18:42:17,920 --> 18:42:25,120 for this page we'll go back to our 21909 18:42:21,320 --> 18:42:26,832 code and on the left side let's open 21910 18:42:25,120 --> 18:42:30,680 amazon. 21911 18:42:26,832 --> 18:42:33,120 JS so this is the code that creates the 21912 18:42:30,680 --> 18:42:35,872 homepage so in here we're going to 21913 18:42:33,120 --> 18:42:38,512 scroll down until we see the rating 21914 18:42:35,872 --> 18:42:40,320 section where we calculate the stars and 21915 18:42:38,512 --> 18:42:44,360 the rating 21916 18:42:40,320 --> 18:42:47,440 account so notice that inside our HTML 21917 18:42:44,360 --> 18:42:49,600 we're constru in this URL here for the 21918 18:42:47,440 --> 18:42:52,440 number of stars a product 21919 18:42:49,600 --> 18:42:55,040 has and we're also creating the price 21920 18:42:52,440 --> 18:42:57,680 that we want to display on the 21921 18:42:55,040 --> 18:43:01,400 page now instead of doing this all in 21922 18:42:57,680 --> 18:43:04,920 the HTML our code would be cleaner if we 21923 18:43:01,400 --> 18:43:07,160 moved this logic into our class because 21924 18:43:04,920 --> 18:43:09,400 we want to group this related code 21925 18:43:07,160 --> 18:43:12,120 together so let's use the features of 21926 18:43:09,400 --> 18:43:17,040 classes to make this code a little bit 21927 18:43:12,120 --> 18:43:17,040 cleaner we'll open products. Js 21928 18:43:17,120 --> 18:43:24,160 and we'll scroll up to the product class 21929 18:43:21,040 --> 18:43:27,080 and inside here let's add some methods 21930 18:43:24,160 --> 18:43:29,920 for calculating the stars and the 21931 18:43:27,080 --> 18:43:37,192 price so we're going to add a method and 21932 18:43:29,920 --> 18:43:40,192 call it get Stars URL brackets and curly 21933 18:43:37,192 --> 18:43:44,760 brackets and let's create another method 21934 18:43:40,192 --> 18:43:47,040 called get price brackets and curly 21935 18:43:44,760 --> 18:43:47,040 brackets 21936 18:43:47,160 --> 18:43:52,720 and now we're going to move the code 21937 18:43:48,832 --> 18:43:55,000 from our HTML into this class so we'll 21938 18:43:52,720 --> 18:43:58,240 open amazon. 21939 18:43:55,000 --> 18:44:01,000 JS and then here we're creating the URL 21940 18:43:58,240 --> 18:44:01,960 of the Stars so let's actually select 21941 18:44:01,000 --> 18:44:04,552 all this 21942 18:44:01,960 --> 18:44:06,320 code and 21943 18:44:04,552 --> 18:44:09,192 rightclick 21944 18:44:06,320 --> 18:44:12,760 copy and inside products. 21945 18:44:09,192 --> 18:44:16,960 JS inside get Stars URL we're going to 21946 18:44:12,760 --> 18:44:16,960 rightclick and paste 21947 18:44:17,832 --> 18:44:23,120 and this is going to be a string so at 21948 18:44:20,040 --> 18:44:25,080 the front let's add a back tick so we're 21949 18:44:23,120 --> 18:44:28,120 going to use a template string because 21950 18:44:25,080 --> 18:44:30,872 we're going to insert a value inside and 21951 18:44:28,120 --> 18:44:33,552 at the end we'll add another back 21952 18:44:30,872 --> 18:44:35,360 tick so now that we created the string 21953 18:44:33,552 --> 18:44:37,440 we're going to return it from this 21954 18:44:35,360 --> 18:44:40,512 method so 21955 18:44:37,440 --> 18:44:43,832 return and semicolon at the 21956 18:44:40,512 --> 18:44:47,480 end and one thing we need to fix is that 21957 18:44:43,832 --> 18:44:50,160 product is not defined anywhere so to 21958 18:44:47,480 --> 18:44:53,872 get the rating property of whatever 21959 18:44:50,160 --> 18:44:56,832 object this is we're going to use this. 21960 18:44:53,872 --> 18:44:56,832 ratings. 21961 18:44:57,960 --> 18:45:05,240 Stars so now each product is able to 21962 18:45:01,080 --> 18:45:07,160 construct its own Stars URL here so 21963 18:45:05,240 --> 18:45:10,552 let's save this 21964 18:45:07,160 --> 18:45:13,160 file and then inside amazon. 21965 18:45:10,552 --> 18:45:15,600 JS instead of doing this calculation 21966 18:45:13,160 --> 18:45:18,120 directly inside the HTML 21967 18:45:15,600 --> 18:45:21,440 we're going to delete it and we're going 21968 18:45:18,120 --> 18:45:27,552 to insert the 21969 18:45:21,440 --> 18:45:29,640 product. getet Stars URL method and 21970 18:45:27,552 --> 18:45:32,360 brackets so you'll notice that we have 21971 18:45:29,640 --> 18:45:35,640 the same product object as before but 21972 18:45:32,360 --> 18:45:38,552 now it's sort of an enhanced product we 21973 18:45:35,640 --> 18:45:42,320 can add methods to group related code 21974 18:45:38,552 --> 18:45:44,920 together and make our code 21975 18:45:42,320 --> 18:45:47,400 cleaner let's do the same thing with the 21976 18:45:44,920 --> 18:45:49,512 price down here we're going to calculate 21977 18:45:47,400 --> 18:45:50,800 this using the method instead of 21978 18:45:49,512 --> 18:45:53,960 directly in the 21979 18:45:50,800 --> 18:45:57,080 HTML so we're going to select this code 21980 18:45:53,960 --> 18:45:59,872 and then right click 21981 18:45:57,080 --> 18:46:03,512 copy inside products. 21982 18:45:59,872 --> 18:46:05,040 JS inside the get price method we're 21983 18:46:03,512 --> 18:46:08,480 going to 21984 18:46:05,040 --> 18:46:08,480 rightclick and 21985 18:46:08,600 --> 18:46:14,160 paste and again we want to return the 21986 18:46:11,280 --> 18:46:16,320 price as a string so at the front we'll 21987 18:46:14,160 --> 18:46:17,480 create a template string because we're 21988 18:46:16,320 --> 18:46:20,512 inserting a 21989 18:46:17,480 --> 18:46:23,872 value and at the back a template string 21990 18:46:20,512 --> 18:46:26,360 or a back tick and we're going to return 21991 18:46:23,872 --> 18:46:28,832 this from the 21992 18:46:26,360 --> 18:46:31,800 method and another thing we need to fix 21993 18:46:28,832 --> 18:46:33,800 is that product is undefined so to get 21994 18:46:31,800 --> 18:46:37,680 the price sets property we're going to 21995 18:46:33,800 --> 18:46:40,192 use this Price 21996 18:46:37,680 --> 18:46:42,120 sets now one extra thing we need to do 21997 18:46:40,192 --> 18:46:44,440 here is that we're using the format 21998 18:46:42,120 --> 18:46:47,640 currency function but we didn't import 21999 18:46:44,440 --> 18:46:50,400 it so we're going to scroll to the 22000 18:46:47,640 --> 18:46:54,872 top and import format 22001 18:46:50,400 --> 18:46:59,280 currency import curly brackets the 22002 18:46:54,872 --> 18:47:02,000 format currency function from a 22003 18:46:59,280 --> 18:47:04,760 string and here we're going to locate 22004 18:47:02,000 --> 18:47:06,552 this function so we're inside the data 22005 18:47:04,760 --> 18:47:10,240 folder we need to get out of the data 22006 18:47:06,552 --> 18:47:13,120 folder using do slash and then go into 22007 18:47:10,240 --> 18:47:17,440 the scripts folder into the UTS folder 22008 18:47:13,120 --> 18:47:20,440 and load money. JS so scripts SL 22009 18:47:17,440 --> 18:47:20,440 utils 22010 18:47:26,000 --> 18:47:29,000 money.jpg 22011 18:47:30,680 --> 18:47:36,960 and instead of calculating the price 22012 18:47:33,872 --> 18:47:41,160 inside the HTML we're going to remove 22013 18:47:36,960 --> 18:47:45,680 this and then insert the 22014 18:47:41,160 --> 18:47:48,280 product. getet price method and 22015 18:47:45,680 --> 18:47:51,192 brackets so now let's 22016 18:47:48,280 --> 18:47:53,000 save and let's open the homepage to make 22017 18:47:51,192 --> 18:47:55,480 sure that it's 22018 18:47:53,000 --> 18:47:59,192 working so now you see that everything 22019 18:47:55,480 --> 18:48:01,440 is still working including the stars and 22020 18:47:59,192 --> 18:48:01,440 the 22021 18:48:03,040 --> 18:48:08,960 prices so we'll go back to our 22022 18:48:06,040 --> 18:48:12,040 code and now that we have these methods 22023 18:48:08,960 --> 18:48:14,720 let's use them in our other page which 22024 18:48:12,040 --> 18:48:16,400 is the checkout page so we'll open 22025 18:48:14,720 --> 18:48:18,080 scripts 22026 18:48:16,400 --> 18:48:20,720 checkout. 22027 18:48:18,080 --> 18:48:24,040 JS and this basically displays the 22028 18:48:20,720 --> 18:48:28,040 products on the checkout 22029 18:48:24,040 --> 18:48:31,040 page if we scroll to the HTML and find 22030 18:48:28,040 --> 18:48:33,680 the delivery date the product image and 22031 18:48:31,040 --> 18:48:35,920 the product name we're going to see that 22032 18:48:33,680 --> 18:48:39,512 here we're also calculating the 22033 18:48:35,920 --> 18:48:42,360 product's price directly in the 22034 18:48:39,512 --> 18:48:45,080 HTML so instead of doing this let's use 22035 18:48:42,360 --> 18:48:50,160 the method we just created so select 22036 18:48:45,080 --> 18:48:52,872 this code and remove it and insert the 22037 18:48:50,160 --> 18:48:54,480 matching product this time so 22038 18:48:52,872 --> 18:48:57,280 matching 22039 18:48:54,480 --> 18:49:00,080 product and remember we converted all of 22040 18:48:57,280 --> 18:49:02,680 our products into classes so this is not 22041 18:49:00,080 --> 18:49:05,720 a regular object anymore it's more of an 22042 18:49:02,680 --> 18:49:09,040 enhanced object it's a product 22043 18:49:05,720 --> 18:49:16,600 class and that means this will have the 22044 18:49:09,040 --> 18:49:18,872 method. get price and brackets and Save 22045 18:49:16,600 --> 18:49:21,720 so now let's open the checkout page to 22046 18:49:18,872 --> 18:49:24,000 see if this works we'll open the web 22047 18:49:21,720 --> 18:49:25,640 page and then at the top right we're 22048 18:49:24,000 --> 18:49:27,872 going to click the 22049 18:49:25,640 --> 18:49:30,192 cart and now you can see that on the 22050 18:49:27,872 --> 18:49:32,960 checkout page the price is still 22051 18:49:30,192 --> 18:49:32,960 displayed 22052 18:49:33,360 --> 18:49:37,192 correctly and that's it if we go back to 22053 18:49:36,320 --> 18:49:39,920 our 22054 18:49:37,192 --> 18:49:42,680 code you'll see that by converting our 22055 18:49:39,920 --> 18:49:46,000 objects into classes we're able to use 22056 18:49:42,680 --> 18:49:48,080 extra features of classes like methods 22057 18:49:46,000 --> 18:49:50,400 to make our code 22058 18:49:48,080 --> 18:49:53,400 cleaner so now let's make sure that all 22059 18:49:50,400 --> 18:49:56,040 of our tests are passing we'll go to the 22060 18:49:53,400 --> 18:49:59,280 left and scroll down and we're going to 22061 18:49:56,040 --> 18:50:02,040 open test.html in the browser so if you 22062 18:49:59,280 --> 18:50:05,080 don't have it open you can right click 22063 18:50:02,040 --> 18:50:07,640 and open with live server but for me I 22064 18:50:05,080 --> 18:50:10,240 already have it open so I'm going to 22065 18:50:07,640 --> 18:50:13,280 open the test page and all of our tests 22066 18:50:10,240 --> 18:50:15,120 are still passing so our code is still 22067 18:50:13,280 --> 18:50:19,240 correct and now now let's save our 22068 18:50:15,120 --> 18:50:22,920 changes into git we'll open the git 22069 18:50:19,240 --> 18:50:26,400 section and in products. JS we created 22070 18:50:22,920 --> 18:50:28,832 the products class and at the bottom we 22071 18:50:26,400 --> 18:50:30,480 converted all of our objects into the 22072 18:50:28,832 --> 18:50:32,872 products 22073 18:50:30,480 --> 18:50:36,320 class so actually one thing I forgot to 22074 18:50:32,872 --> 18:50:38,320 do is to clean up these console. logs so 22075 18:50:36,320 --> 18:50:42,640 let's do that right now we're going to 22076 18:50:38,320 --> 18:50:45,920 open our code and go into products. 22077 18:50:42,640 --> 18:50:49,120 JS and we'll scroll down right after the 22078 18:50:45,920 --> 18:50:52,720 product class and let's remove this 22079 18:50:49,120 --> 18:50:54,920 product one because it was just an 22080 18:50:52,720 --> 18:50:57,240 example and we'll also remove the 22081 18:50:54,920 --> 18:50:59,920 console.log products at the 22082 18:50:57,240 --> 18:51:04,080 end and 22083 18:50:59,920 --> 18:51:07,280 save so now if we go back into git and 22084 18:51:04,080 --> 18:51:10,120 open products. JS we just have the code 22085 18:51:07,280 --> 18:51:12,552 where we convert our objects into a 22086 18:51:10,120 --> 18:51:15,240 class in amazon. 22087 18:51:12,552 --> 18:51:18,120 JS we use the feature of classes called 22088 18:51:15,240 --> 18:51:22,000 Methods to make our code cleaner in 22089 18:51:18,120 --> 18:51:24,760 checkout. JS we stop loading our cart 22090 18:51:22,000 --> 18:51:27,832 class practice file and in orders 22091 18:51:24,760 --> 18:51:30,280 summary. JS we also use a method to make 22092 18:51:27,832 --> 18:51:32,800 our code 22093 18:51:30,280 --> 18:51:34,600 cleaner so now let's create a message to 22094 18:51:32,800 --> 18:51:36,512 describe these 22095 18:51:34,600 --> 18:51:44,960 changes 22096 18:51:36,512 --> 18:51:44,960 convert products into classes and commit 22097 18:51:50,832 --> 18:51:55,480 next we're going to learn an important 22098 18:51:52,832 --> 18:51:56,720 feature of object-oriented programming 22099 18:51:55,480 --> 18:51:59,800 called 22100 18:51:56,720 --> 18:52:01,680 inheritance inheritance lets us reuse 22101 18:51:59,800 --> 18:52:04,832 code between 22102 18:52:01,680 --> 18:52:06,000 classes so first let's learn the concept 22103 18:52:04,832 --> 18:52:08,280 of 22104 18:52:06,000 --> 18:52:09,760 inheritance to do that we're going to 22105 18:52:08,280 --> 18:52:13,600 open our 22106 18:52:09,760 --> 18:52:15,160 project so we'll open this page here to 22107 18:52:13,600 --> 18:52:17,240 our checkout page 22108 18:52:15,160 --> 18:52:20,120 and we're going to go to the homepage by 22109 18:52:17,240 --> 18:52:20,120 clicking the top 22110 18:52:20,280 --> 18:52:26,240 left and on this page we have a bunch of 22111 18:52:23,800 --> 18:52:28,720 products but you also notice that we 22112 18:52:26,240 --> 18:52:30,832 have different types of products for 22113 18:52:28,720 --> 18:52:34,400 example we have clothing like this 22114 18:52:30,832 --> 18:52:38,040 t-shirt and we have appliances like this 22115 18:52:34,400 --> 18:52:41,440 toaster so clothing and appliances are 22116 18:52:38,040 --> 18:52:44,552 still products they have a name a price 22117 18:52:41,440 --> 18:52:48,512 and a rating but they are a more 22118 18:52:44,552 --> 18:52:50,720 specific type of product for example 22119 18:52:48,512 --> 18:52:53,512 clothing can have all the properties of 22120 18:52:50,720 --> 18:52:56,360 a regular product but it can also have 22121 18:52:53,512 --> 18:52:59,080 specific properties like a size 22122 18:52:56,360 --> 18:53:01,872 chart so how do we represent this kind 22123 18:52:59,080 --> 18:53:04,512 of relationship in our code we have a 22124 18:53:01,872 --> 18:53:05,920 product and then we have a more specific 22125 18:53:04,512 --> 18:53:08,680 type of 22126 18:53:05,920 --> 18:53:11,960 product one way to represent this is to 22127 18:53:08,680 --> 18:53:13,832 create a new class called clothing and 22128 18:53:11,960 --> 18:53:16,400 this class will have all the properties 22129 18:53:13,832 --> 18:53:18,320 of a regular product but also more 22130 18:53:16,400 --> 18:53:20,400 specific 22131 18:53:18,320 --> 18:53:22,760 properties however there's a problem 22132 18:53:20,400 --> 18:53:24,720 with this which is we're duplicating a 22133 18:53:22,760 --> 18:53:28,192 lot of 22134 18:53:24,720 --> 18:53:30,760 code instead objectoriented programming 22135 18:53:28,192 --> 18:53:33,600 has a feature called inheritance that 22136 18:53:30,760 --> 18:53:35,920 allows one class to get all the 22137 18:53:33,600 --> 18:53:38,552 properties and methods from another 22138 18:53:35,920 --> 18:53:39,512 class so we don't have to copy paste 22139 18:53:38,552 --> 18:53:42,832 this 22140 18:53:39,512 --> 18:53:46,240 code for example we can make a clothing 22141 18:53:42,832 --> 18:53:48,552 class and then use inheritance to get 22142 18:53:46,240 --> 18:53:49,832 all the properties and methods from the 22143 18:53:48,552 --> 18:53:52,920 product 22144 18:53:49,832 --> 18:53:55,512 class this is similar to how a child 22145 18:53:52,920 --> 18:53:57,832 inherits things from their parents like 22146 18:53:55,512 --> 18:54:01,192 their hair color or the shape of their 22147 18:53:57,832 --> 18:54:03,552 eyes in fact we call the product class 22148 18:54:01,192 --> 18:54:07,832 the parent class and we call the 22149 18:54:03,552 --> 18:54:07,832 clothing class the child 22150 18:54:08,080 --> 18:54:13,920 class so now let's learn how to use 22151 18:54:10,720 --> 18:54:16,040 inheritance in our code we're going to 22152 18:54:13,920 --> 18:54:19,832 open VSS 22153 18:54:16,040 --> 18:54:22,832 code and open our code on the left and 22154 18:54:19,832 --> 18:54:26,320 then open products. 22155 18:54:22,832 --> 18:54:29,800 JS and we're going to scroll to the 22156 18:54:26,320 --> 18:54:31,920 product class and then below this class 22157 18:54:29,800 --> 18:54:35,080 we're going to create a more specific 22158 18:54:31,920 --> 18:54:38,512 type of product called 22159 18:54:35,080 --> 18:54:41,480 class and capital c 22160 18:54:38,512 --> 18:54:44,480 clothing and curly 22161 18:54:41,480 --> 18:54:47,800 brackets so we said that clothing is is 22162 18:54:44,480 --> 18:54:50,040 just a specific type of product so that 22163 18:54:47,800 --> 18:54:52,600 means clothing should have all the 22164 18:54:50,040 --> 18:54:56,280 properties and methods of a 22165 18:54:52,600 --> 18:54:59,080 product so first let's inherit all these 22166 18:54:56,280 --> 18:55:02,760 properties and methods into the clothing 22167 18:54:59,080 --> 18:55:06,800 class to use inheritance after the class 22168 18:55:02,760 --> 18:55:08,960 name we're going to use the word 22169 18:55:06,800 --> 18:55:12,080 extends and then we're going to give it 22170 18:55:08,960 --> 18:55:15,192 the class that we want to inherit from 22171 18:55:12,080 --> 18:55:18,280 which is the product class 22172 18:55:15,192 --> 18:55:20,960 Above So this means that clothing will 22173 18:55:18,280 --> 18:55:24,360 get all the properties and methods of 22174 18:55:20,960 --> 18:55:26,720 this class or it will inherit all the 22175 18:55:24,360 --> 18:55:29,720 properties and 22176 18:55:26,720 --> 18:55:33,512 methods to demonstrate this let's leave 22177 18:55:29,720 --> 18:55:35,600 this empty and create a new clothing 22178 18:55:33,512 --> 18:55:39,080 object so down 22179 18:55:35,600 --> 18:55:42,552 here let's create a variable first using 22180 18:55:39,080 --> 18:55:45,400 const and we'll name it a 22181 18:55:42,552 --> 18:55:48,872 t-shirt make it equal 22182 18:55:45,400 --> 18:55:52,400 and we'll generate a new clothing object 22183 18:55:48,872 --> 18:55:57,440 to do that we'll use the word New 22184 18:55:52,400 --> 18:56:00,000 Capital C clothing and 22185 18:55:57,440 --> 18:56:02,640 brackets and next we're going to expand 22186 18:56:00,000 --> 18:56:06,080 the products array and we're going to 22187 18:56:02,640 --> 18:56:07,760 give the product details to this class 22188 18:56:06,080 --> 18:56:11,920 so we're going to scroll down to the 22189 18:56:07,760 --> 18:56:13,872 third product which is the 22190 18:56:11,920 --> 18:56:14,960 t-shirt and we're going to select this 22191 18:56:13,872 --> 18:56:16,320 object 22192 18:56:14,960 --> 18:56:18,240 and then right 22193 18:56:16,320 --> 18:56:20,872 click 22194 18:56:18,240 --> 18:56:24,000 copy and scroll 22195 18:56:20,872 --> 18:56:27,360 up and now between these brackets we're 22196 18:56:24,000 --> 18:56:30,400 going to right click and 22197 18:56:27,360 --> 18:56:33,240 paste so now we created a new closing 22198 18:56:30,400 --> 18:56:36,320 class we gave it all the data that it 22199 18:56:33,240 --> 18:56:38,872 needs and let's console.log it to see 22200 18:56:36,320 --> 18:56:40,760 what it looks like so down here we're 22201 18:56:38,872 --> 18:56:43,920 going to 22202 18:56:40,760 --> 18:56:48,000 console.log brackets and the variable 22203 18:56:43,920 --> 18:56:51,360 name which is t-shirt so here 22204 18:56:48,000 --> 18:56:54,480 t-shirt and 22205 18:56:51,360 --> 18:56:57,320 save and now let's open the console so 22206 18:56:54,480 --> 18:56:59,192 we'll open our web page and then on this 22207 18:56:57,320 --> 18:57:01,400 page we're going to right 22208 18:56:59,192 --> 18:57:04,080 click 22209 18:57:01,400 --> 18:57:06,360 inspect and open the 22210 18:57:04,080 --> 18:57:07,832 console and you'll see that we created a 22211 18:57:06,360 --> 18:57:11,000 new clothing 22212 18:57:07,832 --> 18:57:13,120 object and if we open it you'll see that 22213 18:57:11,000 --> 18:57:14,640 this has the same properties as the 22214 18:57:13,120 --> 18:57:17,640 product class 22215 18:57:14,640 --> 18:57:18,640 so this inherited all the properties 22216 18:57:17,640 --> 18:57:21,400 from 22217 18:57:18,640 --> 18:57:24,480 product so let's see if we inherited the 22218 18:57:21,400 --> 18:57:28,120 methods as well we'll go to our 22219 18:57:24,480 --> 18:57:30,080 code and let's try the method get price 22220 18:57:28,120 --> 18:57:31,920 so we'll 22221 18:57:30,080 --> 18:57:36,040 console.log 22222 18:57:31,920 --> 18:57:38,920 t-shirt. getet price and 22223 18:57:36,040 --> 18:57:42,280 brackets and 22224 18:57:38,920 --> 18:57:44,960 save and now if we open the console 22225 18:57:42,280 --> 18:57:48,760 again you'll see that get price also 22226 18:57:44,960 --> 18:57:51,512 works so clothing inherited the methods 22227 18:57:48,760 --> 18:57:56,320 from product as well so even though this 22228 18:57:51,512 --> 18:57:59,720 is empty this class gets or inherits all 22229 18:57:56,320 --> 18:58:02,640 the properties and methods of 22230 18:57:59,720 --> 18:58:05,512 product so we use inheritance when we 22231 18:58:02,640 --> 18:58:08,552 have one class which is a more specific 22232 18:58:05,512 --> 18:58:11,640 type of another class for example 22233 18:58:08,552 --> 18:58:14,400 clothing is a more specific type of 22234 18:58:11,640 --> 18:58:16,760 product we want all the properties and 22235 18:58:14,400 --> 18:58:19,080 methods of the product still but we just 22236 18:58:16,760 --> 18:58:22,280 don't want to copy paste all this code 22237 18:58:19,080 --> 18:58:25,720 down here so inheritance lets us reuse 22238 18:58:22,280 --> 18:58:25,720 code between 22239 18:58:29,240 --> 18:58:33,600 classes so now that we have this 22240 18:58:31,192 --> 18:58:37,160 clothing class we can add properties and 22241 18:58:33,600 --> 18:58:40,760 methods that are more specific to 22242 18:58:37,160 --> 18:58:42,440 clothing for example clothing can have a 22243 18:58:40,760 --> 18:58:45,552 size 22244 18:58:42,440 --> 18:58:48,280 chart so for con convenience inside this 22245 18:58:45,552 --> 18:58:52,080 t-shirt object I've actually added a 22246 18:58:48,280 --> 18:58:55,360 size chart link so let's add this to our 22247 18:58:52,080 --> 18:58:56,400 clothing class so up here we're going to 22248 18:58:55,360 --> 18:59:00,800 add a 22249 18:58:56,400 --> 18:59:03,680 property size chart 22250 18:59:00,800 --> 18:59:06,800 link and now to set this value we're 22251 18:59:03,680 --> 18:59:11,360 going to use the Constructor like we did 22252 18:59:06,800 --> 18:59:15,960 before so down here we're going to use a 22253 18:59:11,360 --> 18:59:15,960 Constructor brackets and curly brackets 22254 18:59:16,512 --> 18:59:22,760 and we're going to give this a parameter 22255 18:59:18,512 --> 18:59:22,760 called Product 22256 18:59:24,000 --> 18:59:29,800 details so when we create the clothing 22257 18:59:26,720 --> 18:59:31,480 we save this object into product details 22258 18:59:29,800 --> 18:59:35,240 and then we can use it to set the 22259 18:59:31,480 --> 18:59:40,480 properties up here so we're going to do 22260 18:59:35,240 --> 18:59:44,640 this do size chart link and make it 22261 18:59:40,480 --> 18:59:46,800 equal to the product details 22262 18:59:44,640 --> 18:59:49,512 which is this object and we're going to 22263 18:59:46,800 --> 18:59:55,040 get the size chart link 22264 18:59:49,512 --> 18:59:58,192 property dot size chart 22265 18:59:55,040 --> 19:00:00,552 link now one problem we have here is 22266 18:59:58,192 --> 19:00:04,000 that clothing inherits all the 22267 19:00:00,552 --> 19:00:07,160 properties from the product so we also 22268 19:00:04,000 --> 19:00:11,480 need to set the ID the image the name 22269 19:00:07,160 --> 19:00:16,400 and so on so if we scroll down we could 22270 19:00:11,480 --> 19:00:18,960 do something like this . ID equals 22271 19:00:16,400 --> 19:00:22,080 product details. 22272 19:00:18,960 --> 19:00:25,120 ID however instead of doing this one by 22273 19:00:22,080 --> 19:00:28,640 one all over again inheritance gives us 22274 19:00:25,120 --> 19:00:33,160 a shortcut we can actually just use the 22275 19:00:28,640 --> 19:00:36,920 Constructor in the parent class up 22276 19:00:33,160 --> 19:00:38,760 here so to call the parents Constructor 22277 19:00:36,920 --> 19:00:44,320 we're going to use a special feature of 22278 19:00:38,760 --> 19:00:47,720 classes called super and brackets 22279 19:00:44,320 --> 19:00:50,440 so super basically calls the Constructor 22280 19:00:47,720 --> 19:00:52,920 of the parent class which is the product 22281 19:00:50,440 --> 19:00:55,440 so this will call the Constructor up 22282 19:00:52,920 --> 19:00:58,512 here and we also need to give this the 22283 19:00:55,440 --> 19:01:04,680 product details so down here we're going 22284 19:00:58,512 --> 19:01:04,680 to call Super and give it the product 22285 19:01:05,360 --> 19:01:09,440 details so this will call the 22286 19:01:07,280 --> 19:01:12,320 Constructor of the parent class which is 22287 19:01:09,440 --> 19:01:14,832 product and that will set the ID the 22288 19:01:12,320 --> 19:01:16,512 name and the rating 22289 19:01:14,832 --> 19:01:20,280 so now let's save 22290 19:01:16,512 --> 19:01:23,600 this and check our console 22291 19:01:20,280 --> 19:01:25,800 again and open up the clothing object 22292 19:01:23,600 --> 19:01:27,680 and you'll see that now we have a size 22293 19:01:25,800 --> 19:01:31,040 chart link 22294 19:01:27,680 --> 19:01:34,480 property so that's how inheritance Works 22295 19:01:31,040 --> 19:01:37,120 a class can get or inherit all the 22296 19:01:34,480 --> 19:01:40,280 properties and methods of another class 22297 19:01:37,120 --> 19:01:42,160 and reuse that code and this class can 22298 19:01:40,280 --> 19:01:46,192 also have more specific properties and 22299 19:01:42,160 --> 19:01:46,192 methods like a size chart 22300 19:01:46,960 --> 19:01:52,320 link one last thing to know is that if 22301 19:01:49,552 --> 19:01:55,120 we don't create a Constructor by default 22302 19:01:52,320 --> 19:01:57,280 it will run the parents Constructor so 22303 19:01:55,120 --> 19:01:59,920 that's why when this class was empty the 22304 19:01:57,280 --> 19:01:59,920 code still 22305 19:02:02,480 --> 19:02:08,832 worked now that we have this clothing 22306 19:02:04,832 --> 19:02:08,832 class we're going to use it in our 22307 19:02:09,000 --> 19:02:14,192 project so right now if we scroll down 22308 19:02:11,832 --> 19:02:16,760 to our products array we can converted 22309 19:02:14,192 --> 19:02:19,000 all of these into a product 22310 19:02:16,760 --> 19:02:22,040 class however we're going to convert 22311 19:02:19,000 --> 19:02:25,120 some of them like the t-shirt into a 22312 19:02:22,040 --> 19:02:27,832 clothing class instead to make it more 22313 19:02:25,120 --> 19:02:30,240 specific so how do we know which objects 22314 19:02:27,832 --> 19:02:33,360 to convert into a product and which ones 22315 19:02:30,240 --> 19:02:36,920 to convert into clothing to help us do 22316 19:02:33,360 --> 19:02:38,920 that I included a property called type 22317 19:02:36,920 --> 19:02:42,360 so type tells us which class we should 22318 19:02:38,920 --> 19:02:44,832 use when we're converting this into a 22319 19:02:42,360 --> 19:02:47,680 class this is sometimes called a 22320 19:02:44,832 --> 19:02:50,480 discriminator property it tells us which 22321 19:02:47,680 --> 19:02:52,640 class we should convert this 22322 19:02:50,480 --> 19:02:55,280 to so we're going to go down to the 22323 19:02:52,640 --> 19:02:57,552 bottom of this code so I'm going to do a 22324 19:02:55,280 --> 19:03:00,480 trick here I'm going to scroll up to the 22325 19:02:57,552 --> 19:03:03,600 products array and click this Arrow to 22326 19:03:00,480 --> 19:03:05,760 collapse it and then at the bottom we're 22327 19:03:03,600 --> 19:03:09,960 going to use that type property to 22328 19:03:05,760 --> 19:03:12,360 decide which class to use so inside this 22329 19:03:09,960 --> 19:03:15,120 function we're going to use an if 22330 19:03:12,360 --> 19:03:17,960 statement bracket ET and curler 22331 19:03:15,120 --> 19:03:19,160 brackets and inside we're going to check 22332 19:03:17,960 --> 19:03:22,240 the type 22333 19:03:19,160 --> 19:03:24,120 property product 22334 19:03:22,240 --> 19:03:28,400 details. 22335 19:03:24,120 --> 19:03:31,440 type and we'll check if it is equal to 22336 19:03:28,400 --> 19:03:31,440 the string 22337 19:03:32,512 --> 19:03:37,160 clothing if it is equal to clothing 22338 19:03:35,120 --> 19:03:41,960 we're going to convert this object into 22339 19:03:37,160 --> 19:03:47,160 a clothing class instead so let's 22340 19:03:41,960 --> 19:03:49,440 return new Capital C clothing and 22341 19:03:47,160 --> 19:03:53,720 brackets and we're going to give the 22342 19:03:49,440 --> 19:03:53,720 Constructor product 22343 19:03:55,360 --> 19:04:02,512 details so now our array will contain a 22344 19:03:58,320 --> 19:04:05,872 mix of regular products and 22345 19:04:02,512 --> 19:04:07,720 clothing next for each of our clothing 22346 19:04:05,872 --> 19:04:10,832 we're going to display the size chart 22347 19:04:07,720 --> 19:04:13,440 link on the 22348 19:04:10,832 --> 19:04:16,360 page but first let's make sure that we 22349 19:04:13,440 --> 19:04:19,640 have this image here the closing size 22350 19:04:16,360 --> 19:04:22,440 chart so if we go into our images 22351 19:04:19,640 --> 19:04:24,320 folder you might see the image here 22352 19:04:22,440 --> 19:04:26,552 called clothing siiz 22353 19:04:24,320 --> 19:04:29,760 chart.png but currently I don't have 22354 19:04:26,552 --> 19:04:31,760 this so I need to download this first to 22355 19:04:29,760 --> 19:04:34,120 do that we're going to click this link 22356 19:04:31,760 --> 19:04:34,120 in the 22357 19:04:38,720 --> 19:04:43,920 description which will open the size 22358 19:04:40,920 --> 19:04:43,920 chart 22359 19:04:46,552 --> 19:04:51,480 and then on this image we're going to 22360 19:04:48,320 --> 19:04:53,960 right click save image 22361 19:04:51,480 --> 19:04:56,320 as and then find the folder that 22362 19:04:53,960 --> 19:04:57,680 contains all our code so for me it's on 22363 19:04:56,320 --> 19:05:01,192 the 22364 19:04:57,680 --> 19:05:04,800 desktop inside a JavaScript Amazon 22365 19:05:01,192 --> 19:05:08,600 Project and inside the images 22366 19:05:04,800 --> 19:05:08,600 folder and we'll save it 22367 19:05:10,552 --> 19:05:16,480 here and if we go back to our code we 22368 19:05:13,680 --> 19:05:19,640 should have the closing size chart image 22369 19:05:16,480 --> 19:05:22,480 inside the images 22370 19:05:19,640 --> 19:05:24,320 folder now let's display the size chart 22371 19:05:22,480 --> 19:05:26,832 on the 22372 19:05:24,320 --> 19:05:29,360 page so if I go to the 22373 19:05:26,832 --> 19:05:31,800 homepage whenever we have a clothing 22374 19:05:29,360 --> 19:05:35,160 product like this t-shirt we're going to 22375 19:05:31,800 --> 19:05:38,240 display a size chart 22376 19:05:35,160 --> 19:05:42,040 underneath let's go back to our 22377 19:05:38,240 --> 19:05:43,680 code and open amazon. JS which creates 22378 19:05:42,040 --> 19:05:45,280 the homepage 22379 19:05:43,680 --> 19:05:47,872 which is right 22380 19:05:45,280 --> 19:05:51,320 here and we're going to scroll down the 22381 19:05:47,872 --> 19:05:56,600 HTML below the product price and the 22382 19:05:51,320 --> 19:05:56,600 quantity selector here and underneath 22383 19:05:57,040 --> 19:06:02,760 this if we have a clothing product we're 22384 19:06:00,000 --> 19:06:06,440 going to display a size chart link under 22385 19:06:02,760 --> 19:06:06,440 the quantity selector like 22386 19:06:06,872 --> 19:06:12,232 this so one way we can do this is to use 22387 19:06:10,040 --> 19:06:16,120 something like an if statement or a 22388 19:06:12,232 --> 19:06:18,760 turnery like a this if it's a clothing 22389 19:06:16,120 --> 19:06:22,360 then display some HTML if it's not a 22390 19:06:18,760 --> 19:06:22,360 clothing then just display 22391 19:06:23,120 --> 19:06:28,080 nothing however we're going to learn a 22392 19:06:25,552 --> 19:06:30,440 different way to do this using 22393 19:06:28,080 --> 19:06:32,872 inheritance so first we're going to 22394 19:06:30,440 --> 19:06:35,760 create a new method on the clothing 22395 19:06:32,872 --> 19:06:38,400 class that's going to generate the HTML 22396 19:06:35,760 --> 19:06:41,760 that we need here so we're going to go 22397 19:06:38,400 --> 19:06:44,400 to our code and scroll up and open 22398 19:06:41,760 --> 19:06:46,232 products. Js 22399 19:06:44,400 --> 19:06:47,320 and we're going to scroll up to the 22400 19:06:46,232 --> 19:06:49,800 clothing 22401 19:06:47,320 --> 19:06:53,600 class and at the bottom we're going to 22402 19:06:49,800 --> 19:06:55,232 create a new method and let's call it 22403 19:06:53,600 --> 19:06:56,960 extra 22404 19:06:55,232 --> 19:07:01,320 info 22405 19:06:56,960 --> 19:07:01,320 HTML brackets and Cur 22406 19:07:02,000 --> 19:07:07,040 brackets so this method will generate 22407 19:07:04,512 --> 19:07:09,872 some HTML that contains some extra 22408 19:07:07,040 --> 19:07:11,080 information about this product like the 22409 19:07:09,872 --> 19:07:16,000 size 22410 19:07:11,080 --> 19:07:18,360 chart so here we're going to return some 22411 19:07:16,000 --> 19:07:20,160 HTML and we use a template string 22412 19:07:18,360 --> 19:07:24,320 because we're going to insert some 22413 19:07:20,160 --> 19:07:27,480 values inside and inside here let's 22414 19:07:24,320 --> 19:07:30,040 create some HTML for a 22415 19:07:27,480 --> 19:07:34,040 link to create a link we're going to use 22416 19:07:30,040 --> 19:07:38,400 the code less than a greater than less 22417 19:07:34,040 --> 19:07:41,480 than SL a greater than so this HTML 22418 19:07:38,400 --> 19:07:44,360 element creates a link when we click 22419 19:07:41,480 --> 19:07:48,000 this link it will open another page 22420 19:07:44,360 --> 19:07:51,080 page inside this link let's put the text 22421 19:07:48,000 --> 19:07:52,832 size chart so this is what's going to 22422 19:07:51,080 --> 19:07:56,160 show up on the 22423 19:07:52,832 --> 19:07:59,512 page and Link elements have two 22424 19:07:56,160 --> 19:08:01,920 attributes the first attribute is called 22425 19:07:59,512 --> 19:08:04,720 hre Equals double 22426 19:08:01,920 --> 19:08:08,080 quotes So this tells the computer which 22427 19:08:04,720 --> 19:08:11,232 page this should open when we click this 22428 19:08:08,080 --> 19:08:14,600 link inside here we're going to insert 22429 19:08:11,232 --> 19:08:20,440 the size chart link so we're going to 22430 19:08:14,600 --> 19:08:23,400 insert this. siiz chart 22431 19:08:20,440 --> 19:08:27,512 link so when we click this it will open 22432 19:08:23,400 --> 19:08:30,720 a page containing the size chart 22433 19:08:27,512 --> 19:08:33,552 image we'll scroll back 22434 19:08:30,720 --> 19:08:37,552 up and Link elements can have another 22435 19:08:33,552 --> 19:08:39,600 attribute called Target equals double 22436 19:08:37,552 --> 19:08:43,680 quotes and we're going to give it a 22437 19:08:39,600 --> 19:08:45,680 value of underscore blank 22438 19:08:43,680 --> 19:08:48,680 and I'll add some new lines here to make 22439 19:08:45,680 --> 19:08:51,920 it a little easier to 22440 19:08:48,680 --> 19:08:54,280 read so Target equals blank tells the 22441 19:08:51,920 --> 19:08:57,400 link to open in a new 22442 19:08:54,280 --> 19:09:00,400 tab so now let's save this 22443 19:08:57,400 --> 19:09:04,680 file and we'll go back to the homepage 22444 19:09:00,400 --> 19:09:07,920 so I'll scroll down here to amazon. 22445 19:09:04,680 --> 19:09:09,872 JS and below the quantity 22446 19:09:07,920 --> 19:09:12,400 selector let's 22447 19:09:09,872 --> 19:09:15,832 insert and we'll call the method that we 22448 19:09:12,400 --> 19:09:19,872 just created which is the 22449 19:09:15,832 --> 19:09:22,800 product. extra info 22450 19:09:19,872 --> 19:09:26,232 HTML and 22451 19:09:22,800 --> 19:09:28,480 brackets so this will ins some HTML 22452 19:09:26,232 --> 19:09:30,640 containing extra information about the 22453 19:09:28,480 --> 19:09:33,320 product like the size 22454 19:09:30,640 --> 19:09:35,680 chart now before we run this code 22455 19:09:33,320 --> 19:09:38,800 there's one thing that we need to fix 22456 19:09:35,680 --> 19:09:39,960 currently only the clothing class has 22457 19:09:38,800 --> 19:09:42,960 this 22458 19:09:39,960 --> 19:09:44,832 method if it's just a regular product 22459 19:09:42,960 --> 19:09:47,360 it's not going to have this method and 22460 19:09:44,832 --> 19:09:50,160 it's going to cause an 22461 19:09:47,360 --> 19:09:53,720 error to fix this we're also going to 22462 19:09:50,160 --> 19:09:55,512 create this method on the product class 22463 19:09:53,720 --> 19:09:58,800 so we'll go back to our 22464 19:09:55,512 --> 19:10:01,920 code and open products. 22465 19:09:58,800 --> 19:10:05,000 JS and we're going to scroll up to the 22466 19:10:01,920 --> 19:10:08,440 products class here and at the bottom 22467 19:10:05,000 --> 19:10:08,440 we're going to add the same 22468 19:10:10,192 --> 19:10:17,680 method extra info HTML brackets and 22469 19:10:15,600 --> 19:10:20,600 curly 22470 19:10:17,680 --> 19:10:22,680 brackets and for regular products we 22471 19:10:20,600 --> 19:10:27,600 don't have any extra info we want to 22472 19:10:22,680 --> 19:10:30,120 display so let's just return and empty 22473 19:10:27,600 --> 19:10:34,192 string and 22474 19:10:30,120 --> 19:10:36,512 save so now if we call extra info HTML 22475 19:10:34,192 --> 19:10:39,232 it doesn't matter if it's a product or a 22476 19:10:36,512 --> 19:10:40,760 clothing both of these will have this 22477 19:10:39,232 --> 19:10:44,512 method 22478 19:10:40,760 --> 19:10:47,400 defined so we'll go back to amazon. 22479 19:10:44,512 --> 19:10:51,832 JS and now this should always 22480 19:10:47,400 --> 19:10:54,800 work so let's click this file and 22481 19:10:51,832 --> 19:10:55,960 save and then open the web page to see 22482 19:10:54,800 --> 19:10:58,960 the 22483 19:10:55,960 --> 19:11:01,920 result so now you notice that products 22484 19:10:58,960 --> 19:11:04,480 that are clothing have a size chart 22485 19:11:01,920 --> 19:11:06,680 underneath and regular products don't 22486 19:11:04,480 --> 19:11:08,872 have any extra 22487 19:11:06,680 --> 19:11:12,160 information and if we click the size 22488 19:11:08,872 --> 19:11:15,160 chart link it will open a new tab to the 22489 19:11:12,160 --> 19:11:15,160 size chart image 22490 19:11:15,600 --> 19:11:22,120 so that's how we create this size chart 22491 19:11:17,920 --> 19:11:22,120 link for only the clothing 22492 19:11:24,232 --> 19:11:28,512 products let's go back to our code and 22493 19:11:26,872 --> 19:11:31,552 we'll learn more about this new 22494 19:11:28,512 --> 19:11:34,232 technique that we just 22495 19:11:31,552 --> 19:11:37,320 used so on the left we'll go back to 22496 19:11:34,232 --> 19:11:37,320 products. JS 22497 19:11:38,160 --> 19:11:44,552 first so notice that the product class 22498 19:11:41,192 --> 19:11:47,040 has a method called extra info HT ML and 22499 19:11:44,552 --> 19:11:49,320 then clothing extends this 22500 19:11:47,040 --> 19:11:53,600 class that means clothing will 22501 19:11:49,320 --> 19:11:56,832 automatically get the extra info HTML 22502 19:11:53,600 --> 19:11:58,512 method but then we Define this method 22503 19:11:56,832 --> 19:12:01,552 again inside 22504 19:11:58,512 --> 19:12:04,960 clothing so this will actually override 22505 19:12:01,552 --> 19:12:08,800 or replace the parents method and this 22506 19:12:04,960 --> 19:12:08,800 technique is called method 22507 19:12:09,320 --> 19:12:14,400 overwriting one more thing to know about 22508 19:12:11,552 --> 19:12:17,512 method overwriting is that if we really 22509 19:12:14,400 --> 19:12:20,832 need access to the parents method 22510 19:12:17,512 --> 19:12:24,080 there's a feature that we can use called 22511 19:12:20,832 --> 19:12:25,280 super so super gives us access to the 22512 19:12:24,080 --> 19:12:27,760 parent 22513 19:12:25,280 --> 19:12:32,552 class so if we want to call this method 22514 19:12:27,760 --> 19:12:35,512 up here we can do super and then extra 22515 19:12:32,552 --> 19:12:35,512 info 22516 19:12:36,080 --> 19:12:42,080 HTML so it's the same super as we used 22517 19:12:39,160 --> 19:12:45,040 up here except this calls the parents 22518 19:12:42,080 --> 19:12:47,360 Constructor and this calls one of the 22519 19:12:45,040 --> 19:12:49,800 parents 22520 19:12:47,360 --> 19:12:52,480 methods so let's comment this out since 22521 19:12:49,800 --> 19:12:52,480 it's just an 22522 19:12:52,720 --> 19:13:00,832 example and let's 22523 19:12:55,720 --> 19:13:04,080 save and lastly let's go back to amazon. 22524 19:13:00,832 --> 19:13:07,040 JS and we'll take a look at this code 22525 19:13:04,080 --> 19:13:10,552 and notice that when we call extra info 22526 19:13:07,040 --> 19:13:13,120 HTML we don't know what class this is 22527 19:13:10,552 --> 19:13:15,320 this might be a clothing class or a 22528 19:13:13,120 --> 19:13:18,680 might just be a product 22529 19:13:15,320 --> 19:13:20,872 class this technique is called 22530 19:13:18,680 --> 19:13:23,720 polymorphism which means we can use a 22531 19:13:20,872 --> 19:13:25,480 method without needing to know exactly 22532 19:13:23,720 --> 19:13:28,280 what class this 22533 19:13:25,480 --> 19:13:31,192 is polymorphism is sort of like an 22534 19:13:28,280 --> 19:13:33,960 alternative to doing if statements here 22535 19:13:31,192 --> 19:13:36,160 instead of doing if it's clothing do 22536 19:13:33,960 --> 19:13:38,640 this if it's a regular product do 22537 19:13:36,160 --> 19:13:41,640 something else we can just have both of 22538 19:13:38,640 --> 19:13:43,680 the classes use the same method and then 22539 19:13:41,640 --> 19:13:45,512 the class itself will Det determine what 22540 19:13:43,680 --> 19:13:48,760 this method 22541 19:13:45,512 --> 19:13:51,120 does if we add a new class like an 22542 19:13:48,760 --> 19:13:53,552 appliance class we don't need to change 22543 19:13:51,120 --> 19:13:58,120 any of this code the appliance class 22544 19:13:53,552 --> 19:13:58,120 will determine what extra info HTML 22545 19:13:59,400 --> 19:14:05,120 does and that's it we learned about 22546 19:14:02,360 --> 19:14:07,920 inheritance which lets us reuse code 22547 19:14:05,120 --> 19:14:10,720 between classes and we learn some of the 22548 19:14:07,920 --> 19:14:13,080 techniques we can do with inheritance 22549 19:14:10,720 --> 19:14:16,960 like method overwriting and polymorph 22550 19:14:13,080 --> 19:14:19,280 PRM which can make our code a little bit 22551 19:14:16,960 --> 19:14:21,720 cleaner now let's check our test to make 22552 19:14:19,280 --> 19:14:24,280 sure that everything still works so 22553 19:14:21,720 --> 19:14:26,960 we'll open the test page and make sure 22554 19:14:24,280 --> 19:14:29,280 that everything is still passing and 22555 19:14:26,960 --> 19:14:32,720 then we'll go back to our code and we'll 22556 19:14:29,280 --> 19:14:36,640 save this into git open the git 22557 19:14:32,720 --> 19:14:36,640 section and in products. 22558 19:14:37,920 --> 19:14:43,760 JS we created the clothing class which 22559 19:14:41,160 --> 19:14:45,640 inherits from product 22560 19:14:43,760 --> 19:14:47,640 and one thing I forgot to do is to 22561 19:14:45,640 --> 19:14:50,440 remove this practice 22562 19:14:47,640 --> 19:14:53,320 code so let's actually go back to our 22563 19:14:50,440 --> 19:14:57,160 code in products. 22564 19:14:53,320 --> 19:14:59,760 JS and below the clothing let's remove 22565 19:14:57,160 --> 19:14:59,760 this practice 22566 19:14:59,800 --> 19:15:04,512 code and 22567 19:15:02,040 --> 19:15:08,872 save we'll go back to 22568 19:15:04,512 --> 19:15:11,552 git and just make sure that it's been 22569 19:15:08,872 --> 19:15:13,280 removed and then we added the size chart 22570 19:15:11,552 --> 19:15:16,872 image 22571 19:15:13,280 --> 19:15:19,552 and in amazon. JS we used polymorphism 22572 19:15:16,872 --> 19:15:21,960 to add some extra info below each 22573 19:15:19,552 --> 19:15:23,360 product so let's create a message 22574 19:15:21,960 --> 19:15:27,080 describing our 22575 19:15:23,360 --> 19:15:30,600 changes use 22576 19:15:27,080 --> 19:15:35,760 inheritance and 22577 19:15:30,600 --> 19:15:38,760 polymorphism to add size chart and 22578 19:15:35,760 --> 19:15:38,760 commit 22579 19:15:43,120 --> 19:15:48,160 in the rest of this lesson we're going 22580 19:15:44,832 --> 19:15:51,680 to learn more details about classes the 22581 19:15:48,160 --> 19:15:54,000 first detail is how to test classes so 22582 19:15:51,680 --> 19:15:55,512 testing classes is the same as writing 22583 19:15:54,000 --> 19:15:58,552 normal 22584 19:15:55,512 --> 19:16:01,680 tests so we generate an object using a 22585 19:15:58,552 --> 19:16:03,800 class and then we test if its properties 22586 19:16:01,680 --> 19:16:06,512 and methods are 22587 19:16:03,800 --> 19:16:10,000 correct I'll leave writing tests as an 22588 19:16:06,512 --> 19:16:10,000 exercise at the end of this 22589 19:16:10,040 --> 19:16:14,480 lesson the second detail we're going to 22590 19:16:12,120 --> 19:16:16,680 learn is built in 22591 19:16:14,480 --> 19:16:19,680 classes these are classes that are 22592 19:16:16,680 --> 19:16:19,680 provided by the 22593 19:16:20,192 --> 19:16:26,720 language so let's go back to our 22594 19:16:23,232 --> 19:16:29,160 code and open products. JS 22595 19:16:26,720 --> 19:16:30,640 again and we're going to practice using 22596 19:16:29,160 --> 19:16:35,480 a buil-in 22597 19:16:30,640 --> 19:16:38,552 class an example of a built-in class is 22598 19:16:35,480 --> 19:16:42,960 date this class generates an object that 22599 19:16:38,552 --> 19:16:42,960 represents the current date 22600 19:16:44,120 --> 19:16:48,440 below the classes that we created we're 22601 19:16:46,512 --> 19:16:50,360 going to create some new lines and we're 22602 19:16:48,440 --> 19:16:51,960 going to practice using the built-in 22603 19:16:50,360 --> 19:16:55,360 date 22604 19:16:51,960 --> 19:16:59,440 class first we'll generate a new date 22605 19:16:55,360 --> 19:17:04,320 object using the code new and then the 22606 19:16:59,440 --> 19:17:06,872 class capital D date and 22607 19:17:04,320 --> 19:17:10,080 brackets and now let's save this object 22608 19:17:06,872 --> 19:17:13,512 in a variable so at the front we'll use 22609 19:17:10,080 --> 19:17:16,640 const and we'll call it date and make it 22610 19:17:13,512 --> 19:17:19,920 equal to this object that we just 22611 19:17:16,640 --> 19:17:22,552 generated and below this let's 22612 19:17:19,920 --> 19:17:25,232 console.log the date to see what it 22613 19:17:22,552 --> 19:17:27,680 looks like so we'll 22614 19:17:25,232 --> 19:17:30,160 save and then open the 22615 19:17:27,680 --> 19:17:32,000 page and we'll go back to our project 22616 19:17:30,160 --> 19:17:35,160 here and we're going to check the 22617 19:17:32,000 --> 19:17:38,280 console so right click 22618 19:17:35,160 --> 19:17:40,920 inspect and the 22619 19:17:38,280 --> 19:17:43,552 console and we can see that the date 22620 19:17:40,920 --> 19:17:45,960 object represents the current date and 22621 19:17:43,552 --> 19:17:49,280 time so this is the current date and 22622 19:17:45,960 --> 19:17:49,280 time that I'm recording this 22623 19:17:49,320 --> 19:17:54,760 video so this date object has properties 22624 19:17:52,600 --> 19:17:56,192 and methods that we can use that are 22625 19:17:54,760 --> 19:18:00,320 related to the 22626 19:17:56,192 --> 19:18:03,440 date for example we'll go back to our 22627 19:18:00,320 --> 19:18:06,960 code and every date object has a method 22628 19:18:03,440 --> 19:18:09,960 called two local time string this 22629 19:18:06,960 --> 19:18:12,720 basically gives us the current 22630 19:18:09,960 --> 19:18:16,720 time so let's try it out we'll create 22631 19:18:12,720 --> 19:18:24,232 create a new line and type the code date 22632 19:18:16,720 --> 19:18:26,760 dot to local time string and brackets so 22633 19:18:24,232 --> 19:18:29,440 make sure that this is in cam case two 22634 19:18:26,760 --> 19:18:30,552 is lowercase and every other word starts 22635 19:18:29,440 --> 19:18:33,280 with an 22636 19:18:30,552 --> 19:18:35,720 uppercase and we'll console.log this to 22637 19:18:33,280 --> 19:18:40,192 see what it looks like 22638 19:18:35,720 --> 19:18:40,192 console. log and 22639 19:18:40,320 --> 19:18:46,680 brackets so let's save this file 22640 19:18:44,080 --> 19:18:50,000 and open the 22641 19:18:46,680 --> 19:18:52,320 console and that method gave us the time 22642 19:18:50,000 --> 19:18:54,872 so for you you should see your current 22643 19:18:52,320 --> 19:18:54,872 time 22644 19:18:55,720 --> 19:19:01,680 here so that's the built-in date class 22645 19:18:59,360 --> 19:19:04,080 it lets us create objects that represent 22646 19:19:01,680 --> 19:19:06,832 the current date and 22647 19:19:04,080 --> 19:19:10,040 time an interesting thing to know is 22648 19:19:06,832 --> 19:19:13,680 that we used the external Library DJs 22649 19:19:10,040 --> 19:19:15,552 earlier in the project DJs uses this 22650 19:19:13,680 --> 19:19:18,800 date class behind the 22651 19:19:15,552 --> 19:19:22,192 scenes however DJs gives us a lot of 22652 19:19:18,800 --> 19:19:26,040 extra features so we usually use DJs 22653 19:19:22,192 --> 19:19:26,040 instead of using the date class 22654 19:19:26,360 --> 19:19:31,400 directly so now let's comment out this 22655 19:19:28,680 --> 19:19:33,720 code because it was just an example so 22656 19:19:31,400 --> 19:19:38,760 at the top we'll type 22657 19:19:33,720 --> 19:19:38,760 slashstar and below it star 22658 19:19:39,440 --> 19:19:44,960 slash the last thing we're going to 22659 19:19:41,232 --> 19:19:46,800 learn is more detail dets about 22660 19:19:44,960 --> 19:19:49,760 this as a 22661 19:19:46,800 --> 19:19:53,400 reminder inside an object we can use the 22662 19:19:49,760 --> 19:19:54,720 value this and this will point to the 22663 19:19:53,400 --> 19:19:57,720 outer 22664 19:19:54,720 --> 19:20:00,832 object so this feature lets an object 22665 19:19:57,720 --> 19:20:04,400 access its own 22666 19:20:00,832 --> 19:20:07,120 properties now in JavaScript this can 22667 19:20:04,400 --> 19:20:10,280 actually be used anywhere in our 22668 19:20:07,120 --> 19:20:13,120 code for example we're going to create 22669 19:20:10,280 --> 19:20:17,552 some new lines 22670 19:20:13,120 --> 19:20:20,512 and here we can just directly 22671 19:20:17,552 --> 19:20:23,600 console.log 22672 19:20:20,512 --> 19:20:27,480 this so here we're using this but we're 22673 19:20:23,600 --> 19:20:30,600 using it outside of any object in this 22674 19:20:27,480 --> 19:20:34,720 situation there's no object for this to 22675 19:20:30,600 --> 19:20:36,512 point to so this will be undefined which 22676 19:20:34,720 --> 19:20:38,512 actually makes 22677 19:20:36,512 --> 19:20:43,080 sense if we 22678 19:20:38,512 --> 19:20:45,512 save and we check our console 22679 19:20:43,080 --> 19:20:49,040 this will equal 22680 19:20:45,512 --> 19:20:51,552 undefined now originally this actually 22681 19:20:49,040 --> 19:20:54,832 equaled the window 22682 19:20:51,552 --> 19:20:56,760 object however this Behavior caused 22683 19:20:54,832 --> 19:21:00,480 confusion so when they release 22684 19:20:56,760 --> 19:21:04,832 JavaScript modules inside a module this 22685 19:21:00,480 --> 19:21:04,832 is now undefined which makes more 22686 19:21:06,080 --> 19:21:11,600 sense let's go back to our code and 22687 19:21:09,440 --> 19:21:14,680 another place we can use this is when 22688 19:21:11,600 --> 19:21:17,192 we're creating an object so let's go 22689 19:21:14,680 --> 19:21:20,920 down here and we're going to create a 22690 19:21:17,192 --> 19:21:24,680 new object using const we'll call it 22691 19:21:20,920 --> 19:21:26,552 object two and make it equal to curly 22692 19:21:24,680 --> 19:21:29,872 brackets and we're going to give this 22693 19:21:26,552 --> 19:21:33,720 object a property let's just call it a 22694 19:21:29,872 --> 19:21:36,640 colon give it a value of two comma and 22695 19:21:33,720 --> 19:21:39,232 add another property B 22696 19:21:36,640 --> 19:21:43,600 colon and we'll set the value of this 22697 19:21:39,232 --> 19:21:46,600 property to this Dot 22698 19:21:43,600 --> 19:21:50,040 a now you might think that because we're 22699 19:21:46,600 --> 19:21:54,120 inside this object this will point to 22700 19:21:50,040 --> 19:21:57,920 object 2 however at this point in the 22701 19:21:54,120 --> 19:22:00,320 code object 2 has not been created yet 22702 19:21:57,920 --> 19:22:04,160 so this doesn't have anything to point 22703 19:22:00,320 --> 19:22:04,160 to and this will be 22704 19:22:05,360 --> 19:22:10,960 undefined if we 22705 19:22:07,600 --> 19:22:14,080 save and then open the 22706 19:22:10,960 --> 19:22:16,832 console it will give us an error because 22707 19:22:14,080 --> 19:22:16,832 this is 22708 19:22:17,192 --> 19:22:23,680 undefined let's go back to our 22709 19:22:19,640 --> 19:22:26,080 code so so far if we're inside a method 22710 19:22:23,680 --> 19:22:29,040 we can use this and it will point to the 22711 19:22:26,080 --> 19:22:31,760 object if we're outside of a method 22712 19:22:29,040 --> 19:22:32,920 there's no object to point to and this 22713 19:22:31,760 --> 19:22:37,000 will be 22714 19:22:32,920 --> 19:22:41,280 undefined and everything makes sense so 22715 19:22:37,000 --> 19:22:46,320 far so let's comment this out using SL 22716 19:22:41,280 --> 19:22:46,320 star and and at the bottom start 22717 19:22:46,960 --> 19:22:54,080 slash now the last important place we 22718 19:22:49,680 --> 19:22:54,080 can use this is inside a 22719 19:22:54,552 --> 19:23:00,160 function so at the bottom here we're 22720 19:22:57,440 --> 19:23:00,160 going to create a 22721 19:23:00,192 --> 19:23:06,440 function and let's call it log this 22722 19:23:04,552 --> 19:23:09,512 brackets and curly 22723 19:23:06,440 --> 19:23:14,360 brackets and inside we're just going to 22724 19:23:09,512 --> 19:23:14,360 console. log this 22725 19:23:14,760 --> 19:23:21,920 so again we're not inside of any object 22726 19:23:18,480 --> 19:23:24,640 so there's nothing for this to point2 22727 19:23:21,920 --> 19:23:27,760 and in this situation this will be 22728 19:23:24,640 --> 19:23:27,760 undefined as 22729 19:23:28,000 --> 19:23:36,080 well below this if we run the function 22730 19:23:31,600 --> 19:23:38,440 log this and brackets and 22731 19:23:36,080 --> 19:23:41,280 save and check the 22732 19:23:38,440 --> 19:23:43,920 console it will tell us that this is 22733 19:23:41,280 --> 19:23:45,512 undefined when it's just inside a 22734 19:23:43,920 --> 19:23:48,800 regular 22735 19:23:45,512 --> 19:23:49,832 function next this actually has a 22736 19:23:48,800 --> 19:23:52,760 special 22737 19:23:49,832 --> 19:23:56,440 feature so inside a function we can 22738 19:23:52,760 --> 19:24:01,400 change this to whatever we want to do 22739 19:23:56,440 --> 19:24:04,960 that functions have a method called 22740 19:24:01,400 --> 19:24:09,600 call So Below this line let's type the 22741 19:24:04,960 --> 19:24:13,800 function name log this and use a method 22742 19:24:09,600 --> 19:24:17,160 do call and brackets 22743 19:24:13,800 --> 19:24:20,080 so call runs this function so it does 22744 19:24:17,160 --> 19:24:22,440 the same thing as the line above except 22745 19:24:20,080 --> 19:24:25,760 we can set the value of this to be 22746 19:24:22,440 --> 19:24:28,000 whatever we want so the first value that 22747 19:24:25,760 --> 19:24:31,440 we give to do call is going to be the 22748 19:24:28,000 --> 19:24:34,232 value of this for example we can give it 22749 19:24:31,440 --> 19:24:37,480 the string 22750 19:24:34,232 --> 19:24:40,872 hello and now this will run the function 22751 19:24:37,480 --> 19:24:42,960 and this will be equal to hello if we 22752 19:24:40,872 --> 19:24:44,360 save 22753 19:24:42,960 --> 19:24:47,320 and open the 22754 19:24:44,360 --> 19:24:50,232 console you can see that we set this to 22755 19:24:47,320 --> 19:24:53,360 be equal to 22756 19:24:50,232 --> 19:24:57,080 hello so that's a special feature of 22757 19:24:53,360 --> 19:25:00,512 this inside just a function we can 22758 19:24:57,080 --> 19:25:03,872 change this to whatever we want using a 22759 19:25:00,512 --> 19:25:07,512 method like 22760 19:25:03,872 --> 19:25:10,120 call so call is the same as calling or 22761 19:25:07,512 --> 19:25:13,192 running the function except we add an 22762 19:25:10,120 --> 19:25:16,960 extra parameter in front this first 22763 19:25:13,192 --> 19:25:16,960 parameter is the value of 22764 19:25:18,400 --> 19:25:23,800 this the last important thing to know 22765 19:25:21,040 --> 19:25:26,400 about this is that Arrow functions do 22766 19:25:23,800 --> 19:25:30,192 not change the value of 22767 19:25:26,400 --> 19:25:32,800 this for example below this let's create 22768 19:25:30,192 --> 19:25:36,040 a method by using an arrow function this 22769 19:25:32,800 --> 19:25:38,360 time so we'll create some new lines and 22770 19:25:36,040 --> 19:25:39,552 first we're going to create an object 22771 19:25:38,360 --> 19:25:44,360 using 22772 19:25:39,552 --> 19:25:45,760 const we'll call it object three equals 22773 19:25:44,360 --> 19:25:49,280 curly 22774 19:25:45,760 --> 19:25:52,160 brackets and inside will give it a 22775 19:25:49,280 --> 19:25:54,040 method and colon and we're going to 22776 19:25:52,160 --> 19:25:57,832 create this method using an arrow 22777 19:25:54,040 --> 19:26:01,800 function this time brackets arrow and 22778 19:25:57,832 --> 19:26:06,120 curly brackets and inside we'll just 22779 19:26:01,800 --> 19:26:10,320 console. log 22780 19:26:06,120 --> 19:26:12,832 this so usually this will point to the 22781 19:26:10,320 --> 19:26:15,280 object itself 22782 19:26:12,832 --> 19:26:17,600 however Arrow functions do not change 22783 19:26:15,280 --> 19:26:22,040 the value of 22784 19:26:17,600 --> 19:26:24,480 this here this will have the same value 22785 19:26:22,040 --> 19:26:27,080 as outside the arrow 22786 19:26:24,480 --> 19:26:28,872 function outside the arrow function 22787 19:26:27,080 --> 19:26:33,680 let's type 22788 19:26:28,872 --> 19:26:35,760 this here this has no object to point to 22789 19:26:33,680 --> 19:26:38,360 so this will be 22790 19:26:35,760 --> 19:26:42,232 undefined and then inside the arrow 22791 19:26:38,360 --> 19:26:45,160 function it will also be undefined 22792 19:26:42,232 --> 19:26:48,920 basically this keeps the value that it 22793 19:26:45,160 --> 19:26:48,920 had outside the arrow 22794 19:26:50,552 --> 19:26:58,600 function as a test below this we'll run 22795 19:26:54,640 --> 19:27:00,680 object 3. method and 22796 19:26:58,600 --> 19:27:04,280 brackets if we 22797 19:27:00,680 --> 19:27:04,280 save and check the 22798 19:27:06,160 --> 19:27:13,080 console it will show us that inside here 22799 19:27:09,832 --> 19:27:15,872 this is also undefined 22800 19:27:13,080 --> 19:27:20,360 so in an arrow function this will be 22801 19:27:15,872 --> 19:27:20,360 whatever the value is outside the arrow 22802 19:27:20,720 --> 19:27:27,080 function now you might be wondering why 22803 19:27:23,400 --> 19:27:29,920 are Arrow functions designed this 22804 19:27:27,080 --> 19:27:33,720 way so JavaScript had a common problem 22805 19:27:29,920 --> 19:27:36,680 in the past when we are inside a method 22806 19:27:33,720 --> 19:27:40,280 this points to the object but if we try 22807 19:27:36,680 --> 19:27:42,800 to do a for each Loop inside the for 22808 19:27:40,280 --> 19:27:46,512 each Loop this does not point to the 22809 19:27:42,800 --> 19:27:48,320 object anymore it points to undefined 22810 19:27:46,512 --> 19:27:51,920 and that's because we created a whole 22811 19:27:48,320 --> 19:27:55,720 new function here and remember inside a 22812 19:27:51,920 --> 19:27:58,080 regular function this becomes 22813 19:27:55,720 --> 19:28:00,192 undefined so this caused problems 22814 19:27:58,080 --> 19:28:02,760 because we no longer had access to the 22815 19:28:00,192 --> 19:28:05,400 outer object up 22816 19:28:02,760 --> 19:28:07,512 here so that's why we created Arrow 22817 19:28:05,400 --> 19:28:10,400 functions if we replace this with an 22818 19:28:07,512 --> 19:28:12,440 arrow function this would be equal to 22819 19:28:10,400 --> 19:28:15,360 whatever it is outside the Arrow 22820 19:28:12,440 --> 19:28:17,120 function Arrow functions do not change 22821 19:28:15,360 --> 19:28:20,640 the value of 22822 19:28:17,120 --> 19:28:24,640 this so outside for each this points to 22823 19:28:20,640 --> 19:28:26,280 the object and inside for each this also 22824 19:28:24,640 --> 19:28:29,000 points to the 22825 19:28:26,280 --> 19:28:32,120 object so that's why Arrow functions are 22826 19:28:29,000 --> 19:28:34,800 designed this way to avoid accidentally 22827 19:28:32,120 --> 19:28:37,760 overwriting 22828 19:28:34,800 --> 19:28:41,832 this so let's summarize what we learned 22829 19:28:37,760 --> 19:28:45,960 about this inside a method this points 22830 19:28:41,832 --> 19:28:48,720 to to the outer object inside a function 22831 19:28:45,960 --> 19:28:51,800 this is undefined but we can change it 22832 19:28:48,720 --> 19:28:54,832 to whatever we want using something like 22833 19:28:51,800 --> 19:28:57,080 call and arrow functions do not change 22834 19:28:54,832 --> 19:28:59,800 the value of 22835 19:28:57,080 --> 19:29:02,192 this don't worry if these rules are a 22836 19:28:59,800 --> 19:29:03,480 bit confusing at first they are actually 22837 19:29:02,192 --> 19:29:05,680 sort of 22838 19:29:03,480 --> 19:29:08,640 reasonable if there's an object to point 22839 19:29:05,680 --> 19:29:10,872 to this will point to that object if 22840 19:29:08,640 --> 19:29:14,720 there's no object to point to this will 22841 19:29:10,872 --> 19:29:16,600 be undefined and we have tools like call 22842 19:29:14,720 --> 19:29:19,000 and arrow functions to kind of 22843 19:29:16,600 --> 19:29:21,760 manipulate the value of this to be what 22844 19:29:19,000 --> 19:29:25,280 we want the more that you work with it 22845 19:29:21,760 --> 19:29:25,280 the more you just get used to 22846 19:29:27,080 --> 19:29:33,192 it so now let's comment out this 22847 19:29:29,600 --> 19:29:37,960 practice code at the top we'll type 22848 19:29:33,192 --> 19:29:39,680 slashstar and at the bottom star slash 22849 19:29:37,960 --> 19:29:42,872 and 22850 19:29:39,680 --> 19:29:44,552 save and we'll save to get 22851 19:29:42,872 --> 19:29:48,000 we'll open the git 22852 19:29:44,552 --> 19:29:50,552 section inside products. JS we added 22853 19:29:48,000 --> 19:29:53,480 some practice code to learn more details 22854 19:29:50,552 --> 19:29:57,040 about classes and 22855 19:29:53,480 --> 19:30:03,600 this let's type a message describing our 22856 19:29:57,040 --> 19:30:09,120 changes learn more details about classes 22857 19:30:03,600 --> 19:30:09,120 and this and commit 22858 19:30:13,360 --> 19:30:18,400 now that we learned object-oriented 22859 19:30:15,160 --> 19:30:21,232 programming you might notice that it's a 22860 19:30:18,400 --> 19:30:23,920 very different style of programming than 22861 19:30:21,232 --> 19:30:26,552 just writing out a bunch of functions 22862 19:30:23,920 --> 19:30:29,040 directly so you might be wondering which 22863 19:30:26,552 --> 19:30:31,872 style of programming should we use 22864 19:30:29,040 --> 19:30:34,080 objectoriented programming or procedural 22865 19:30:31,872 --> 19:30:37,512 programming which is the step-by-step 22866 19:30:34,080 --> 19:30:41,192 style that we used in the earlier 22867 19:30:37,512 --> 19:30:43,440 lessons the answer is it's up to you I 22868 19:30:41,192 --> 19:30:45,872 personally prefer procedural programming 22869 19:30:43,440 --> 19:30:48,480 in JavaScript because I think it's 22870 19:30:45,872 --> 19:30:50,832 simpler I only use objectoriented 22871 19:30:48,480 --> 19:30:54,552 programming if I'm working on a team 22872 19:30:50,832 --> 19:30:56,552 that also uses it also object-oriented 22873 19:30:54,552 --> 19:30:59,600 programming is much more popular in 22874 19:30:56,552 --> 19:31:02,232 other languages so it's still useful to 22875 19:30:59,600 --> 19:31:03,800 know object-oriented programming in case 22876 19:31:02,232 --> 19:31:06,720 you work with other programming 22877 19:31:03,800 --> 19:31:06,720 languages in the 22878 19:31:06,960 --> 19:31:12,120 future and that's the end of this lesson 22879 19:31:10,192 --> 19:31:14,600 in this lesson we learned about 22880 19:31:12,120 --> 19:31:18,160 objectoriented programming which means 22881 19:31:14,600 --> 19:31:20,040 we organize our code into objects we 22882 19:31:18,160 --> 19:31:22,920 learned how to use a function to 22883 19:31:20,040 --> 19:31:26,360 generate objects and we improve that by 22884 19:31:22,920 --> 19:31:28,552 using a class to generate objects we 22885 19:31:26,360 --> 19:31:31,360 learned that classes can have private 22886 19:31:28,552 --> 19:31:34,192 properties and methods we learned about 22887 19:31:31,360 --> 19:31:37,160 inheritance which lets us reuse code 22888 19:31:34,192 --> 19:31:40,360 between classes and we learned about 22889 19:31:37,160 --> 19:31:42,440 method overwriting and polymorphism 22890 19:31:40,360 --> 19:31:45,320 which lets us call a method without 22891 19:31:42,440 --> 19:31:47,832 needing to know what the class is and 22892 19:31:45,320 --> 19:31:52,680 lastly we learned more details about 22893 19:31:47,832 --> 19:31:52,680 this and how to control the value of 22894 19:31:54,360 --> 19:31:59,400 this thanks for watching this course so 22895 19:31:56,920 --> 19:32:01,400 far if you want to support this channel 22896 19:31:59,400 --> 19:32:03,680 you can check out the premium version of 22897 19:32:01,400 --> 19:32:06,120 my courses just like this one where you 22898 19:32:03,680 --> 19:32:08,552 can get a certificate of completion and 22899 19:32:06,120 --> 19:32:11,280 a Better Learning platform if you're 22900 19:32:08,552 --> 19:32:13,832 interested in extra videos from me more 22901 19:32:11,280 --> 19:32:16,552 in-depth videos and behind the scenes 22902 19:32:13,832 --> 19:32:19,280 content you can check out these links 22903 19:32:16,552 --> 19:32:22,120 here you can also support the channel by 22904 19:32:19,280 --> 19:32:24,920 liking the video subscribing and sharing 22905 19:32:22,120 --> 19:32:27,600 this video with others thanks again and 22906 19:32:24,920 --> 19:32:30,280 let's continue with the 22907 19:32:27,600 --> 19:32:32,400 course here are some exercises to help 22908 19:32:30,280 --> 19:32:35,400 you practice what we learned in this 22909 19:32:32,400 --> 19:32:35,400 lesson 22910 19:33:03,000 --> 19:33:07,480 in this lesson we're going to learn 22911 19:33:04,680 --> 19:33:10,160 about backend and this will prepare you 22912 19:33:07,480 --> 19:33:12,320 for the next step after finishing this 22913 19:33:10,160 --> 19:33:15,872 course first of all 22914 19:33:12,320 --> 19:33:18,400 what is backend a backend is just 22915 19:33:15,872 --> 19:33:19,872 another computer that manages the data 22916 19:33:18,400 --> 19:33:22,680 of a 22917 19:33:19,872 --> 19:33:25,280 website for example in our Amazon 22918 19:33:22,680 --> 19:33:28,192 Project we created this website where we 22919 19:33:25,280 --> 19:33:31,080 can pick which products we want but how 22920 19:33:28,192 --> 19:33:32,800 does Amazon the company know which 22921 19:33:31,080 --> 19:33:35,800 products that we 22922 19:33:32,800 --> 19:33:37,920 ordered in this situation there has to 22923 19:33:35,800 --> 19:33:39,320 be a second computer somewhere that's 22924 19:33:37,920 --> 19:33:42,480 owned by 22925 19:33:39,320 --> 19:33:44,960 Amazon and then our computer sends the 22926 19:33:42,480 --> 19:33:47,760 order information to this second 22927 19:33:44,960 --> 19:33:48,960 computer and now Amazon will know what 22928 19:33:47,760 --> 19:33:52,040 we 22929 19:33:48,960 --> 19:33:55,232 ordered so this second computer is 22930 19:33:52,040 --> 19:33:59,080 called the backend computer or just the 22931 19:33:55,232 --> 19:34:02,440 backend it manages the data of a 22932 19:33:59,080 --> 19:34:04,960 website on the other hand our computer 22933 19:34:02,440 --> 19:34:07,160 or the web page where we can see and 22934 19:34:04,960 --> 19:34:11,192 interact with the products this is 22935 19:34:07,160 --> 19:34:11,192 called the front end 22936 19:34:12,280 --> 19:34:18,360 now that we know what a backend is how 22937 19:34:14,960 --> 19:34:22,000 does our computer or the front end send 22938 19:34:18,360 --> 19:34:24,872 information to the back end when two 22939 19:34:22,000 --> 19:34:27,832 computers are connected to the internet 22940 19:34:24,872 --> 19:34:31,080 they can actually send messages to each 22941 19:34:27,832 --> 19:34:34,800 other using a feature called 22942 19:34:31,080 --> 19:34:38,160 HTTP or hypertext transfer 22943 19:34:34,800 --> 19:34:40,680 protocol in an HTTP message we can 22944 19:34:38,160 --> 19:34:42,640 actually attach information like our 22945 19:34:40,680 --> 19:34:45,720 Amazon order 22946 19:34:42,640 --> 19:34:50,512 when Amazon's backend receives this HTTP 22947 19:34:45,720 --> 19:34:54,480 message now Amazon will know what we 22948 19:34:50,512 --> 19:34:58,800 ordered now that we know what HTTP is 22949 19:34:54,480 --> 19:35:02,040 let's send our first HTTP message using 22950 19:34:58,800 --> 19:35:03,872 JavaScript so we'll go to our code and 22951 19:35:02,040 --> 19:35:06,760 on the left we're going to create a new 22952 19:35:03,872 --> 19:35:09,720 file just for practicing backend so 22953 19:35:06,760 --> 19:35:12,040 we'll scroll up to the data folder and 22954 19:35:09,720 --> 19:35:13,512 we'll click inside this folder and then 22955 19:35:12,040 --> 19:35:17,832 create a new 22956 19:35:13,512 --> 19:35:19,720 file and let's name this file backend 22957 19:35:17,832 --> 19:35:24,440 Das 22958 19:35:19,720 --> 19:35:24,440 practice. JS and press 22959 19:35:24,640 --> 19:35:32,120 enter and now to send an HTTP message 22960 19:35:28,680 --> 19:35:36,680 we're going to use a class called XML 22961 19:35:32,120 --> 19:35:39,480 HTTP request so this is a built in class 22962 19:35:36,680 --> 19:35:42,600 meaning it's provided by 22963 19:35:39,480 --> 19:35:46,320 JavaScript first we gener at an object 22964 19:35:42,600 --> 19:35:49,440 using this class using the code 22965 19:35:46,320 --> 19:35:53,960 new and the class name 22966 19:35:49,440 --> 19:36:00,080 XML all capital capital 22967 19:35:53,960 --> 19:36:00,080 HTTP capital r request and 22968 19:36:00,280 --> 19:36:05,800 brackets so this creates a new HTTP 22969 19:36:03,512 --> 19:36:10,512 message that we want to send to the 22970 19:36:05,800 --> 19:36:13,640 backend a message is also known as a 22971 19:36:10,512 --> 19:36:16,640 request so now let's save this request 22972 19:36:13,640 --> 19:36:20,120 in a variable at the front we'll create 22973 19:36:16,640 --> 19:36:24,440 a variable with const and name it 22974 19:36:20,120 --> 19:36:27,800 xhr equals this 22975 19:36:24,440 --> 19:36:31,360 request next we need to set up this 22976 19:36:27,800 --> 19:36:35,280 request so we'll create a new line and 22977 19:36:31,360 --> 19:36:38,640 to set it up we'll do xhr 22978 19:36:35,280 --> 19:36:38,640 doop and 22979 19:36:38,800 --> 19:36:45,080 brackets and to set up this request 22980 19:36:42,040 --> 19:36:48,040 we're going to give open two 22981 19:36:45,080 --> 19:36:50,920 parameters the first parameter is what 22982 19:36:48,040 --> 19:36:54,360 type of HTTP message to 22983 19:36:50,920 --> 19:36:56,832 send so one type of message we can send 22984 19:36:54,360 --> 19:36:59,960 is the string 22985 19:36:56,832 --> 19:37:01,960 get get means we want to get some 22986 19:36:59,960 --> 19:37:04,680 information from the back 22987 19:37:01,960 --> 19:37:07,040 end there are several common types of 22988 19:37:04,680 --> 19:37:11,680 requests that we can give to the backend 22989 19:37:07,040 --> 19:37:11,680 but we're going to start by learning get 22990 19:37:12,360 --> 19:37:18,832 next we're going to give open a second 22991 19:37:16,120 --> 19:37:23,280 parameter and this parameter tells a 22992 19:37:18,832 --> 19:37:27,000 computer where to send this HTTP message 22993 19:37:23,280 --> 19:37:29,600 so using HTTP we can send a message to 22994 19:37:27,000 --> 19:37:32,440 any computer that's connected to the 22995 19:37:29,600 --> 19:37:35,680 internet now to locate another computer 22996 19:37:32,440 --> 19:37:39,720 on the internet we need to use a 22997 19:37:35,680 --> 19:37:42,832 url url stands for uniform resource 22998 19:37:39,720 --> 19:37:46,192 locator and it's kind of like an address 22999 19:37:42,832 --> 19:37:49,960 but for the internet a URL helps us 23000 19:37:46,192 --> 19:37:54,160 locate another computer on the 23001 19:37:49,960 --> 19:37:57,400 internet so URLs look like this this is 23002 19:37:54,160 --> 19:38:01,232 the URL for Amazon YouTube and my 23003 19:37:57,400 --> 19:38:04,920 website the https in front means we're 23004 19:38:01,232 --> 19:38:08,512 using HTTP to communicate with this 23005 19:38:04,920 --> 19:38:13,192 computer the S after the http means 23006 19:38:08,512 --> 19:38:15,640 we're using a secure version of http 23007 19:38:13,192 --> 19:38:18,920 the second part of a URL is called a 23008 19:38:15,640 --> 19:38:21,552 domain name this is like an address it 23009 19:38:18,920 --> 19:38:25,280 points to another computer on the 23010 19:38:21,552 --> 19:38:28,552 internet so amazon.com points to one of 23011 19:38:25,280 --> 19:38:31,040 Amazon's backend computers youtube.com 23012 19:38:28,552 --> 19:38:34,480 points to one of YouTube's backend 23013 19:38:31,040 --> 19:38:37,080 computers and superimple dodev points to 23014 19:38:34,480 --> 19:38:39,192 one of my backend 23015 19:38:37,080 --> 19:38:41,400 computers if you want to learn more 23016 19:38:39,192 --> 19:38:43,720 about domain names and how the internet 23017 19:38:41,400 --> 19:38:47,080 works you can check out my video on how 23018 19:38:43,720 --> 19:38:50,040 to put a website on the 23019 19:38:47,080 --> 19:38:52,480 internet now for this lesson I actually 23020 19:38:50,040 --> 19:38:56,192 set up a backend computer that we can 23021 19:38:52,480 --> 19:38:58,120 practice with so my backend is located 23022 19:38:56,192 --> 19:39:02,280 at the URL 23023 19:38:58,120 --> 19:39:06,000 https super simple backend 23024 19:39:02,280 --> 19:39:07,720 dodev so in the second parameter of open 23025 19:39:06,000 --> 19:39:09,872 we're going to give it a 23026 19:39:07,720 --> 19:39:12,400 string and we're going to give it the 23027 19:39:09,872 --> 19:39:13,800 URL of the back end end that we want to 23028 19:39:12,400 --> 19:39:18,400 send the message 23029 19:39:13,800 --> 19:39:25,960 to so the URL for my backend is 23030 19:39:18,400 --> 19:39:28,600 https colon SL SL super simple 23031 19:39:25,960 --> 19:39:31,640 backend. 23032 19:39:28,600 --> 19:39:34,280 death so that's how we set up an HTTP 23033 19:39:31,640 --> 19:39:37,552 message we give it the type of message 23034 19:39:34,280 --> 19:39:42,232 we want to send and also the URL which 23035 19:39:37,552 --> 19:39:42,232 is where to send this message to 23036 19:39:42,360 --> 19:39:48,040 the last step is to just send this 23037 19:39:44,720 --> 19:39:51,232 message so we'll create a new line and 23038 19:39:48,040 --> 19:39:56,120 we'll type xhr 23039 19:39:51,232 --> 19:39:56,120 doent brackets and 23040 19:39:56,680 --> 19:40:03,872 save so this creates a new HTTP message 23041 19:40:00,760 --> 19:40:06,360 sets it up and then sends this message 23042 19:40:03,872 --> 19:40:09,832 across the internet to my backend 23043 19:40:06,360 --> 19:40:13,160 computer located at superimple backend. 23044 19:40:09,832 --> 19:40:15,760 deev so now let's actually run this code 23045 19:40:13,160 --> 19:40:18,440 and see the results we're going to go to 23046 19:40:15,760 --> 19:40:20,232 the left and we're going to open 23047 19:40:18,440 --> 19:40:23,800 checkout. 23048 19:40:20,232 --> 19:40:25,960 JS and in here just like we did before 23049 19:40:23,800 --> 19:40:28,320 we're going to load that practice file 23050 19:40:25,960 --> 19:40:32,280 that we just created so we'll create a 23051 19:40:28,320 --> 19:40:35,600 new line and we're going to import a 23052 19:40:32,280 --> 19:40:37,480 file or a string and we'll locate that 23053 19:40:35,600 --> 19:40:39,960 practice 23054 19:40:37,480 --> 19:40:45,232 file so first we need to go to the data 23055 19:40:39,960 --> 19:40:47,192 folder using using do dot SL dat slash 23056 19:40:45,232 --> 19:40:49,832 and load 23057 19:40:47,192 --> 19:40:54,720 backend D 23058 19:40:49,832 --> 19:40:54,720 practice. JS and 23059 19:40:55,280 --> 19:41:01,360 save so we'll go back to the practice 23060 19:40:58,440 --> 19:41:04,720 file and now when we open the website it 23061 19:41:01,360 --> 19:41:07,440 will run this code and send the HTTP 23062 19:41:04,720 --> 19:41:10,680 message to see that let's open the 23063 19:41:07,440 --> 19:41:12,600 website so we'll scroll down and if you 23064 19:41:10,680 --> 19:41:15,120 don't have the website open you can open 23065 19:41:12,600 --> 19:41:18,080 it again by right clicking checkout. 23066 19:41:15,120 --> 19:41:21,120 HTML and then open with live 23067 19:41:18,080 --> 19:41:22,920 server I already have the website open 23068 19:41:21,120 --> 19:41:25,120 so I'm going to open 23069 19:41:22,920 --> 19:41:26,920 it and then I'm going to go to the 23070 19:41:25,120 --> 19:41:29,120 checkout 23071 19:41:26,920 --> 19:41:32,040 page and now we're going to look at the 23072 19:41:29,120 --> 19:41:35,320 console so we'll right 23073 19:41:32,040 --> 19:41:37,832 click and 23074 19:41:35,320 --> 19:41:40,360 inspect and this time instead of going 23075 19:41:37,832 --> 19:41:42,480 to the console tab we're going to go to 23076 19:41:40,360 --> 19:41:44,832 a new tab 23077 19:41:42,480 --> 19:41:47,680 called the network 23078 19:41:44,832 --> 19:41:50,480 tab if you don't see this tab you can 23079 19:41:47,680 --> 19:41:52,320 make this window wider or you might find 23080 19:41:50,480 --> 19:41:56,640 it under these 23081 19:41:52,320 --> 19:41:56,640 arrows so we'll open the network 23082 19:41:59,920 --> 19:42:06,680 Tab and in here this will show us all 23083 19:42:03,600 --> 19:42:08,720 the HTTP messages that are coming in and 23084 19:42:06,680 --> 19:42:11,000 out of our 23085 19:42:08,720 --> 19:42:13,480 computer now before we start looking at 23086 19:42:11,000 --> 19:42:15,080 the the HTTP messages there's one 23087 19:42:13,480 --> 19:42:18,440 important thing to know about the 23088 19:42:15,080 --> 19:42:22,080 network Tab and that is messages will 23089 19:42:18,440 --> 19:42:25,160 only appear here after we open the 23090 19:42:22,080 --> 19:42:27,320 console if we send some messages and 23091 19:42:25,160 --> 19:42:28,920 then we open the console it's not going 23092 19:42:27,320 --> 19:42:32,720 to show up 23093 19:42:28,920 --> 19:42:35,512 here so to make sure our HTTP messages 23094 19:42:32,720 --> 19:42:38,080 are recorded here we need to open the 23095 19:42:35,512 --> 19:42:42,920 network tab first and then we're going 23096 19:42:38,080 --> 19:42:45,760 to refresh the page to resend those 23097 19:42:42,920 --> 19:42:48,872 messages and now if we open the network 23098 19:42:45,760 --> 19:42:52,040 tab we should be able to see the HTTP 23099 19:42:48,872 --> 19:42:52,040 messages at the 23100 19:42:52,480 --> 19:42:58,192 bottom okay so inside this list we 23101 19:42:56,192 --> 19:43:02,400 should be able to see that we sent a 23102 19:42:58,192 --> 19:43:04,832 message to superimple backend. deev if 23103 19:43:02,400 --> 19:43:08,872 you don't see it we can also search this 23104 19:43:04,832 --> 19:43:12,080 list by using the text box up 23105 19:43:08,872 --> 19:43:15,080 here so in here we can type part of the 23106 19:43:12,080 --> 19:43:18,640 URL that we sent the message to for 23107 19:43:15,080 --> 19:43:21,192 example super 23108 19:43:18,640 --> 19:43:24,440 simple and the message that we sent 23109 19:43:21,192 --> 19:43:28,000 should appear here so now we can click 23110 19:43:24,440 --> 19:43:31,120 this message to get more details about 23111 19:43:28,000 --> 19:43:34,640 it on the right side we have several 23112 19:43:31,120 --> 19:43:38,600 tabs here the headers tab gives us 23113 19:43:34,640 --> 19:43:41,360 details about our message or about our 23114 19:43:38,600 --> 19:43:44,440 request so it tells us which type of 23115 19:43:41,360 --> 19:43:47,512 requests that we sent which is get and 23116 19:43:44,440 --> 19:43:49,960 the URL that we sent it to which is 23117 19:43:47,512 --> 19:43:53,320 super simple backend. 23118 19:43:49,960 --> 19:43:55,320 Dev this sends the request across the 23119 19:43:53,320 --> 19:43:58,232 internet to my 23120 19:43:55,320 --> 19:44:01,360 backend and then when my backend 23121 19:43:58,232 --> 19:44:04,320 receives this message or this request it 23122 19:44:01,360 --> 19:44:06,192 will send back a message which is called 23123 19:44:04,320 --> 19:44:09,040 a 23124 19:44:06,192 --> 19:44:12,872 response to see the response from my 23125 19:44:09,040 --> 19:44:15,600 backend we can open the the response 23126 19:44:12,872 --> 19:44:18,320 Tab and you can see that my backend 23127 19:44:15,600 --> 19:44:20,720 responded with hello this is super 23128 19:44:18,320 --> 19:44:23,080 simple dev's back end and a 23129 19:44:20,720 --> 19:44:26,760 documentation page that we can check 23130 19:44:23,080 --> 19:44:30,552 out so we have successfully sent an HTTP 23131 19:44:26,760 --> 19:44:33,080 message or a request to my backend and 23132 19:44:30,552 --> 19:44:36,080 we got back a 23133 19:44:33,080 --> 19:44:38,760 response another useful tab here is the 23134 19:44:36,080 --> 19:44:41,400 preview tab it basically takes a 23135 19:44:38,760 --> 19:44:43,440 response and displays it in an easy to 23136 19:44:41,400 --> 19:44:46,680 read 23137 19:44:43,440 --> 19:44:49,192 format so now let's learn some 23138 19:44:46,680 --> 19:44:52,192 terminology when we send a message to 23139 19:44:49,192 --> 19:44:54,920 the backend this message is called a 23140 19:44:52,192 --> 19:44:55,640 request we are requesting something from 23141 19:44:54,920 --> 19:44:58,400 the 23142 19:44:55,640 --> 19:45:01,800 backend when the backend receives our 23143 19:44:58,400 --> 19:45:04,872 request it will send an HTTP message 23144 19:45:01,800 --> 19:45:06,080 back to us and this message is called 23145 19:45:04,872 --> 19:45:09,040 the 23146 19:45:06,080 --> 19:45:12,000 response so each request that we make 23147 19:45:09,040 --> 19:45:15,120 will get one response from the back 23148 19:45:12,000 --> 19:45:18,512 end and this is called a request 23149 19:45:15,120 --> 19:45:20,872 response cycle it's always one request 23150 19:45:18,512 --> 19:45:20,872 one 23151 19:45:22,600 --> 19:45:28,800 response next currently we're viewing 23152 19:45:25,760 --> 19:45:31,512 the response in the network tab but we 23153 19:45:28,800 --> 19:45:34,192 can actually get this response in our 23154 19:45:31,512 --> 19:45:37,872 code and then do something with this 23155 19:45:34,192 --> 19:45:40,960 response so let's go back to our 23156 19:45:37,872 --> 19:45:43,600 code and here after we send the requ 23157 19:45:40,960 --> 19:45:48,960 Quest we can get the response using a 23158 19:45:43,600 --> 19:45:48,960 property called xhr do 23159 19:45:49,000 --> 19:45:54,800 response however there's a slight 23160 19:45:51,600 --> 19:45:57,800 problem with doing this when we send a 23161 19:45:54,800 --> 19:46:00,000 request to the backend it takes time for 23162 19:45:57,800 --> 19:46:02,760 the request to travel across the 23163 19:46:00,000 --> 19:46:04,440 internet to the back end and for the 23164 19:46:02,760 --> 19:46:07,680 response to come 23165 19:46:04,440 --> 19:46:11,320 back so the response is not available 23166 19:46:07,680 --> 19:46:15,832 right away and xr. response will be 23167 19:46:11,320 --> 19:46:19,280 undefined at first so xr. send is known 23168 19:46:15,832 --> 19:46:21,920 as asynchronous code asynchronous code 23169 19:46:19,280 --> 19:46:25,192 means it does not wait for this line of 23170 19:46:21,920 --> 19:46:27,552 code to finish it just sends the request 23171 19:46:25,192 --> 19:46:30,320 and then immediately goes to the next 23172 19:46:27,552 --> 19:46:32,512 line the response might come back later 23173 19:46:30,320 --> 19:46:34,600 in the future but we don't have access 23174 19:46:32,512 --> 19:46:38,552 to it 23175 19:46:34,600 --> 19:46:41,600 here so in this situation in order to 23176 19:46:38,552 --> 19:46:44,360 get this response we need to wait for 23177 19:46:41,600 --> 19:46:46,400 the response to come back first and then 23178 19:46:44,360 --> 19:46:49,040 we can access do 23179 19:46:46,400 --> 19:46:52,600 response in order to wait for the 23180 19:46:49,040 --> 19:46:53,872 response to come back at the top after 23181 19:46:52,600 --> 19:46:58,040 we create the 23182 19:46:53,872 --> 19:47:04,800 xhr we're going to use this code 23183 19:46:58,040 --> 19:47:07,800 xhr do add capital E Event capital L 23184 19:47:04,800 --> 19:47:11,640 listener and 23185 19:47:07,800 --> 19:47:15,192 brackets so this method listens or waits 23186 19:47:11,640 --> 19:47:17,872 for an event and then it can run a 23187 19:47:15,192 --> 19:47:20,400 function so this method takes two 23188 19:47:17,872 --> 19:47:23,000 parameters the first parameter is the 23189 19:47:20,400 --> 19:47:26,280 event that we want to listen for or to 23190 19:47:23,000 --> 19:47:28,800 wait for in this case we want to wait 23191 19:47:26,280 --> 19:47:32,600 for the response to come back so we're 23192 19:47:28,800 --> 19:47:36,192 going to give it the string load load 23193 19:47:32,600 --> 19:47:36,192 means the response has 23194 19:47:36,440 --> 19:47:41,232 loaded the second parameter is a 23195 19:47:39,120 --> 19:47:43,360 function that we want to run 23196 19:47:41,232 --> 19:47:45,760 after this event 23197 19:47:43,360 --> 19:47:48,552 happens so in this case after the 23198 19:47:45,760 --> 19:47:54,000 response has loaded we're going to run a 23199 19:47:48,552 --> 19:47:54,000 function brackets arrow and curly 23200 19:47:54,040 --> 19:48:00,192 brackets so because this function runs 23201 19:47:56,960 --> 19:48:04,400 after the response loaded inside here we 23202 19:48:00,192 --> 19:48:07,512 can access xhr do 23203 19:48:04,400 --> 19:48:09,512 response and this will not be undefined 23204 19:48:07,512 --> 19:48:11,280 it will contain the response that we saw 23205 19:48:09,512 --> 19:48:14,160 earlier in the network 23206 19:48:11,280 --> 19:48:17,080 tab so let's console.log this to see 23207 19:48:14,160 --> 19:48:19,640 what it looks like 23208 19:48:17,080 --> 19:48:22,232 console.log and 23209 19:48:19,640 --> 19:48:23,400 brackets and we'll remove this line at 23210 19:48:22,232 --> 19:48:26,600 the 23211 19:48:23,400 --> 19:48:29,480 end and 23212 19:48:26,600 --> 19:48:32,640 save so now let's go back and check the 23213 19:48:29,480 --> 19:48:33,800 console for the response so we'll open 23214 19:48:32,640 --> 19:48:37,640 the 23215 19:48:33,800 --> 19:48:40,320 console and then open the console 23216 19:48:37,640 --> 19:48:43,192 Tab and here you can see that we have 23217 19:48:40,320 --> 19:48:45,040 the response in our code and we console. 23218 19:48:43,192 --> 19:48:47,400 logged 23219 19:48:45,040 --> 19:48:51,280 it so this 23220 19:48:47,400 --> 19:48:53,640 response is a string and now we can save 23221 19:48:51,280 --> 19:48:57,080 this in a variable or do whatever we 23222 19:48:53,640 --> 19:48:57,080 want with it in our 23223 19:48:57,400 --> 19:49:01,832 code now one thing you might be 23224 19:48:59,440 --> 19:49:05,512 wondering is why do we put the event 23225 19:49:01,832 --> 19:49:08,440 listener up here at the top so first we 23226 19:49:05,512 --> 19:49:11,480 need to set up the event listener and 23227 19:49:08,440 --> 19:49:13,872 then trigger the event or send the 23228 19:49:11,480 --> 19:49:16,960 request this is the same as setting up 23229 19:49:13,872 --> 19:49:18,800 an event listener for clicking a button 23230 19:49:16,960 --> 19:49:22,760 we need to set up the event listener 23231 19:49:18,800 --> 19:49:25,600 first and then we click the button 23232 19:49:22,760 --> 19:49:28,760 after so that's how we send a request to 23233 19:49:25,600 --> 19:49:32,680 the back end wait for the response and 23234 19:49:28,760 --> 19:49:32,680 then save the response in our 23235 19:49:33,160 --> 19:49:39,512 code next we can send different messages 23236 19:49:36,680 --> 19:49:41,160 or different requests to the backend 23237 19:49:39,512 --> 19:49:45,040 using URL 23238 19:49:41,160 --> 19:49:48,280 paths a URL path is the part that comes 23239 19:49:45,040 --> 19:49:53,600 after the domain name for example in 23240 19:49:48,280 --> 19:49:58,232 this URL the URL path is /hello in this 23241 19:49:53,600 --> 19:50:02,720 URL the URL path is/ products 23242 19:49:58,232 --> 19:50:06,000 slir in this URL if there is no URL path 23243 19:50:02,720 --> 19:50:09,640 the URL path is 23244 19:50:06,000 --> 19:50:12,832 slash we can send a request to these URL 23245 19:50:09,640 --> 19:50:14,480 paths and each URL path will give us a 23246 19:50:12,832 --> 19:50:17,120 different 23247 19:50:14,480 --> 19:50:19,872 response so let's try sending requests 23248 19:50:17,120 --> 19:50:22,920 to different URL paths and see what 23249 19:50:19,872 --> 19:50:26,160 different responses that we 23250 19:50:22,920 --> 19:50:29,600 get so for this example let's send the 23251 19:50:26,160 --> 19:50:36,232 request to super simple backend. deev 23252 19:50:29,600 --> 19:50:39,000 and then the URL path SL hello and 23253 19:50:36,232 --> 19:50:42,232 save and now let's open our console to 23254 19:50:39,000 --> 19:50:45,120 see the response 23255 19:50:42,232 --> 19:50:48,760 and here you can see that this URL path 23256 19:50:45,120 --> 19:50:52,192 gave us a different response than last 23257 19:50:48,760 --> 19:50:55,232 time let's go back and try another URL 23258 19:50:52,192 --> 19:50:58,680 path to see what it looks like so we'll 23259 19:50:55,232 --> 19:51:02,000 delete this and then send the request to 23260 19:50:58,680 --> 19:51:04,800 slash products 23261 19:51:02,000 --> 19:51:07,480 slir and 23262 19:51:04,800 --> 19:51:09,160 save and we'll open the console to view 23263 19:51:07,480 --> 19:51:12,720 the 23264 19:51:09,160 --> 19:51:15,600 response and here here this URL path 23265 19:51:12,720 --> 19:51:18,040 give us a Json and this contains the 23266 19:51:15,600 --> 19:51:20,280 details of the first product in our 23267 19:51:18,040 --> 19:51:23,160 Amazon 23268 19:51:20,280 --> 19:51:25,872 Project so we can send requests to 23269 19:51:23,160 --> 19:51:29,600 different URL paths and we will get 23270 19:51:25,872 --> 19:51:29,600 different responses from the 23271 19:51:29,832 --> 19:51:37,480 backend next a backend only supports a 23272 19:51:33,600 --> 19:51:40,080 certain set of URL paths for example 23273 19:51:37,480 --> 19:51:44,280 when I created my backend super simple 23274 19:51:40,080 --> 19:51:47,232 backend .d I had to decide which URL 23275 19:51:44,280 --> 19:51:51,320 paths are supported and how my backend 23276 19:51:47,232 --> 19:51:54,360 would respond to each URL path if we 23277 19:51:51,320 --> 19:51:56,760 send a request to a URL path that is not 23278 19:51:54,360 --> 19:51:59,000 supported the back end will respond with 23279 19:51:56,760 --> 19:51:59,000 an 23280 19:51:59,080 --> 19:52:04,920 error let's go back to our 23281 19:52:01,680 --> 19:52:06,552 code and we'll try a URL path that is 23282 19:52:04,920 --> 19:52:08,872 not 23283 19:52:06,552 --> 19:52:11,232 supported so let's remove this at the 23284 19:52:08,872 --> 19:52:14,120 end 23285 19:52:11,232 --> 19:52:16,920 and we'll try sending the request to 23286 19:52:14,120 --> 19:52:20,080 not- 23287 19:52:16,920 --> 19:52:23,720 supported and 23288 19:52:20,080 --> 19:52:26,000 save and now if we look at our 23289 19:52:23,720 --> 19:52:28,800 console you'll see that the backend 23290 19:52:26,000 --> 19:52:31,120 responded with an error message saying 23291 19:52:28,800 --> 19:52:33,160 this URL path is not 23292 19:52:31,120 --> 19:52:36,000 supported the browser will also 23293 19:52:33,160 --> 19:52:38,800 console.log this red error 23294 19:52:36,000 --> 19:52:42,440 message this red message is displayed 23295 19:52:38,800 --> 19:52:42,440 automatically when there's an error 23296 19:52:43,552 --> 19:52:49,600 so this message tells us the type of 23297 19:52:45,960 --> 19:52:53,120 request that we sent which is get and 23298 19:52:49,600 --> 19:52:55,960 the URL that we sent it to and also this 23299 19:52:53,120 --> 19:52:58,720 number which is called the status 23300 19:52:55,960 --> 19:53:01,320 code whenever we get a response from the 23301 19:52:58,720 --> 19:53:04,360 backend the backend also gives us a 23302 19:53:01,320 --> 19:53:06,512 status code which tells us if a request 23303 19:53:04,360 --> 19:53:09,160 succeeded or 23304 19:53:06,512 --> 19:53:12,120 failed a status code that starts with 23305 19:53:09,160 --> 19:53:15,000 four or five means the response 23306 19:53:12,120 --> 19:53:18,800 failed starting with four means it was 23307 19:53:15,000 --> 19:53:20,320 our problem we sent the request to a URL 23308 19:53:18,800 --> 19:53:23,040 path that was not 23309 19:53:20,320 --> 19:53:25,680 supported starting with five means it 23310 19:53:23,040 --> 19:53:27,552 was the backends problem for example the 23311 19:53:25,680 --> 19:53:30,040 backend 23312 19:53:27,552 --> 19:53:33,832 crashed a status code starting with a 23313 19:53:30,040 --> 19:53:33,832 two means the response was 23314 19:53:34,040 --> 19:53:38,920 successful so we can also see the status 23315 19:53:36,640 --> 19:53:42,800 code in the network 23316 19:53:38,920 --> 19:53:45,440 tab so we can search for our request and 23317 19:53:42,800 --> 19:53:47,800 then when we click the request in the 23318 19:53:45,440 --> 19:53:49,400 headers tab it will show us the status 23319 19:53:47,800 --> 19:53:52,192 code in this 23320 19:53:49,400 --> 19:53:54,720 row so this tells us if our request 23321 19:53:52,192 --> 19:53:57,512 succeeded or 23322 19:53:54,720 --> 19:54:00,960 failed so now you might be wondering how 23323 19:53:57,512 --> 19:54:02,280 do we know which URL paths are supported 23324 19:54:00,960 --> 19:54:05,040 and which are 23325 19:54:02,280 --> 19:54:08,280 not so there's actually no way to figure 23326 19:54:05,040 --> 19:54:11,080 out all the URL paths that are supported 23327 19:54:08,280 --> 19:54:13,640 and this is for security reasons 23328 19:54:11,080 --> 19:54:16,552 however some backends do provide a 23329 19:54:13,640 --> 19:54:18,920 documentation page that lists the URL 23330 19:54:16,552 --> 19:54:21,832 paths that are supported and also what 23331 19:54:18,920 --> 19:54:24,832 kind of response that they give for my 23332 19:54:21,832 --> 19:54:27,000 backend I created a documentation page 23333 19:54:24,832 --> 19:54:28,720 and we're going to take a look at that 23334 19:54:27,000 --> 19:54:30,920 so we'll open the 23335 19:54:28,720 --> 19:54:32,440 browser and at the top we're going to 23336 19:54:30,920 --> 19:54:35,360 create a new 23337 19:54:32,440 --> 19:54:39,000 tab and to see the documentation for my 23338 19:54:35,360 --> 19:54:44,800 backend we're going to go to super 23339 19:54:39,000 --> 19:54:48,360 simple back back end dodev SL 23340 19:54:44,800 --> 19:54:48,360 documentation and press 23341 19:54:49,000 --> 19:54:55,440 enter so this is the documentation page 23342 19:54:52,232 --> 19:54:58,680 for my backend it gives us a list of 23343 19:54:55,440 --> 19:55:01,872 which URL paths that are supported and 23344 19:54:58,680 --> 19:55:04,760 what each URL path does so when we make 23345 19:55:01,872 --> 19:55:07,552 a request to slash it will give us this 23346 19:55:04,760 --> 19:55:10,360 message which we saw earlier if we make 23347 19:55:07,552 --> 19:55:14,000 a request to/ hello it will give us this 23348 19:55:10,360 --> 19:55:14,000 message which we also saw 23349 19:55:14,160 --> 19:55:20,552 earlier so by the way the list of all 23350 19:55:17,872 --> 19:55:23,280 the URL paths that are supported is 23351 19:55:20,552 --> 19:55:27,160 called the backend 23352 19:55:23,280 --> 19:55:29,720 API API means application programming 23353 19:55:27,160 --> 19:55:33,000 interface interface means how we 23354 19:55:29,720 --> 19:55:35,920 interact with something so this list of 23355 19:55:33,000 --> 19:55:38,832 URL paths are all the ways that we can 23356 19:55:35,920 --> 19:55:43,000 interact with the backend and that's why 23357 19:55:38,832 --> 19:55:43,000 we call this the backend 23358 19:55:43,480 --> 19:55:50,040 API next the backend can respond with 23359 19:55:46,760 --> 19:55:53,480 different types of data so one type of 23360 19:55:50,040 --> 19:55:56,320 data the back end can respond with is 23361 19:55:53,480 --> 19:56:00,320 text for example when we sent the 23362 19:55:56,320 --> 19:56:01,760 request to get/ hello it gave us some 23363 19:56:00,320 --> 19:56:03,872 text 23364 19:56:01,760 --> 19:56:08,192 back so in our 23365 19:56:03,872 --> 19:56:10,640 code if we change this URL path to 23366 19:56:08,192 --> 19:56:11,920 /hello 23367 19:56:10,640 --> 19:56:15,000 and 23368 19:56:11,920 --> 19:56:18,000 save and we open the 23369 19:56:15,000 --> 19:56:21,360 console and click the console 23370 19:56:18,000 --> 19:56:23,800 tab it gives us back some 23371 19:56:21,360 --> 19:56:26,280 text we can also see what type of 23372 19:56:23,800 --> 19:56:29,800 response that we got using the network 23373 19:56:26,280 --> 19:56:31,360 tab so we'll open the network Tab and 23374 19:56:29,800 --> 19:56:34,920 then search for the 23375 19:56:31,360 --> 19:56:38,800 request and then here when we click 23376 19:56:34,920 --> 19:56:40,960 it inside the headers tab if we scroll 23377 19:56:38,800 --> 19:56:44,040 down to the response 23378 19:56:40,960 --> 19:56:47,320 section here it will give us the content 23379 19:56:44,040 --> 19:56:50,800 type or the type of response that we got 23380 19:56:47,320 --> 19:56:55,120 back so in this example /hello gave us 23381 19:56:50,800 --> 19:56:55,120 some text and the type is text 23382 19:56:55,160 --> 19:57:00,640 slpl another type of data the backend 23383 19:56:57,760 --> 19:57:04,280 can respond with is 23384 19:57:00,640 --> 19:57:08,600 Json so if we go back to our code and 23385 19:57:04,280 --> 19:57:11,760 change the url path to products 23386 19:57:08,600 --> 19:57:15,120 slir and 23387 19:57:11,760 --> 19:57:18,120 save and then look at our 23388 19:57:15,120 --> 19:57:21,512 console in the network tab we'll open 23389 19:57:18,120 --> 19:57:23,832 this request and inside the headers if 23390 19:57:21,512 --> 19:57:26,552 we scroll 23391 19:57:23,832 --> 19:57:30,232 down it will tell us that the type of 23392 19:57:26,552 --> 19:57:33,920 the response is application SL 23393 19:57:30,232 --> 19:57:37,680 Json so the backend sent us back some 23394 19:57:33,920 --> 19:57:41,512 Json if we open the console tab we can 23395 19:57:37,680 --> 19:57:43,720 see the Json string here 23396 19:57:41,512 --> 19:57:47,552 so once we get this Json from the 23397 19:57:43,720 --> 19:57:50,480 backend we can use json.parse to convert 23398 19:57:47,552 --> 19:57:53,760 this back into a JavaScript 23399 19:57:50,480 --> 19:57:56,480 object basically this allows us to send 23400 19:57:53,760 --> 19:57:58,640 JavaScript objects across the internet 23401 19:57:56,480 --> 19:58:01,360 to the back 23402 19:57:58,640 --> 19:58:05,000 end another type of data the back end 23403 19:58:01,360 --> 19:58:07,832 can respond with is 23404 19:58:05,000 --> 19:58:14,512 HTML we'll go back to our 23405 19:58:07,832 --> 19:58:16,360 code and we'll change the URL path to 23406 19:58:14,512 --> 19:58:19,600 documentation so this was the 23407 19:58:16,360 --> 19:58:21,160 documentation page that we saw earlier 23408 19:58:19,600 --> 19:58:24,872 if we 23409 19:58:21,160 --> 19:58:24,872 save and then open the 23410 19:58:25,440 --> 19:58:29,872 console you'll see that the back end 23411 19:58:27,640 --> 19:58:33,680 responded with a bunch of 23412 19:58:29,872 --> 19:58:36,280 HTML if we look at the network Tab and 23413 19:58:33,680 --> 19:58:39,480 search for the request and open the 23414 19:58:36,280 --> 19:58:40,960 request here and in the headers tab we 23415 19:58:39,480 --> 19:58:43,760 scroll down 23416 19:58:40,960 --> 19:58:49,400 to the content type it will tell us that 23417 19:58:43,760 --> 19:58:49,400 the type of the response was text/html 23418 19:58:50,320 --> 19:58:56,760 finally another type of data the backend 23419 19:58:53,160 --> 19:58:59,960 can respond with is an 23420 19:58:56,760 --> 19:59:06,600 image for example we'll go back to our 23421 19:58:59,960 --> 19:59:06,600 code and change the url path to 23422 19:59:06,832 --> 19:59:11,192 imagesapple 23423 19:59:08,440 --> 19:59:13,872 jpg and say 23424 19:59:11,192 --> 19:59:17,192 save and we'll open the 23425 19:59:13,872 --> 19:59:19,440 console and in the network tab we can 23426 19:59:17,192 --> 19:59:22,440 open this 23427 19:59:19,440 --> 19:59:25,160 request and in the headers tab we can 23428 19:59:22,440 --> 19:59:29,160 scroll down and it will tell us that the 23429 19:59:25,160 --> 19:59:29,160 type of response was an 23430 19:59:29,480 --> 19:59:35,160 image/jpeg as you can see the backend 23431 19:59:32,160 --> 19:59:37,680 can respond with different types of data 23432 19:59:35,160 --> 19:59:37,680 not just 23433 19:59:38,680 --> 19:59:44,160 text okay so we got this image back from 23434 19:59:42,120 --> 19:59:48,120 the back end but what does this look 23435 19:59:44,160 --> 19:59:50,360 like in the console we'll open the 23436 19:59:48,120 --> 19:59:53,040 console and we'll see that we just get a 23437 19:59:50,360 --> 19:59:54,720 bunch of random characters here and 23438 19:59:53,040 --> 19:59:57,640 that's because the console is trying to 23439 19:59:54,720 --> 19:59:58,872 display the image as text but the image 23440 19:59:57,640 --> 20:00:02,040 is not really 23441 19:59:58,872 --> 20:00:03,720 text this is the image's raw data so 23442 20:00:02,040 --> 20:00:05,480 that's why it shows all these random 23443 20:00:03,720 --> 20:00:08,400 characters 23444 20:00:05,480 --> 20:00:11,000 here however it would be better to 23445 20:00:08,400 --> 20:00:12,640 display this as an actual ual image 23446 20:00:11,000 --> 20:00:16,232 instead of raw 23447 20:00:12,640 --> 20:00:17,360 data we can display this as an image by 23448 20:00:16,232 --> 20:00:20,480 using the 23449 20:00:17,360 --> 20:00:24,040 browser so if we open the 23450 20:00:20,480 --> 20:00:27,040 browser when we type a URL at the top of 23451 20:00:24,040 --> 20:00:30,000 the browser it actually sends a get 23452 20:00:27,040 --> 20:00:33,400 request to that 23453 20:00:30,000 --> 20:00:37,360 URL so typing in here actually does the 23454 20:00:33,400 --> 20:00:40,000 same thing as our code it makes a get 23455 20:00:37,360 --> 20:00:42,232 request the one big difference is that 23456 20:00:40,000 --> 20:00:44,440 that in the browser when it gets back 23457 20:00:42,232 --> 20:00:46,440 the response it will display the 23458 20:00:44,440 --> 20:00:50,000 response on the 23459 20:00:46,440 --> 20:00:54,600 page for example if we enter superimple 23460 20:00:50,000 --> 20:00:59,232 backend dodev and change the url path to 23461 20:00:54,600 --> 20:01:02,872 just nothing or slash and press 23462 20:00:59,232 --> 20:01:06,320 enter this actually sends a get request 23463 20:01:02,872 --> 20:01:09,400 to my back end the backend responds with 23464 20:01:06,320 --> 20:01:11,280 some text and it displays this response 23465 20:01:09,400 --> 20:01:14,232 on the 23466 20:01:11,280 --> 20:01:17,760 page so using the browser is the same 23467 20:01:14,232 --> 20:01:17,760 thing as making a get 23468 20:01:18,080 --> 20:01:23,800 request now the cool thing about this is 23469 20:01:21,000 --> 20:01:26,400 that if the browser gets back an image 23470 20:01:23,800 --> 20:01:29,040 it will display it as an actual image 23471 20:01:26,400 --> 20:01:32,600 instead of raw data that we saw in the 23472 20:01:29,040 --> 20:01:33,552 console so here if we change the url 23473 20:01:32,600 --> 20:01:36,760 path 23474 20:01:33,552 --> 20:01:41,832 to/ images 23475 20:01:36,760 --> 20:01:41,832 slapple jpg and press enter 23476 20:01:41,960 --> 20:01:46,320 it will get back the same image as 23477 20:01:43,832 --> 20:01:50,080 before except the browser will display 23478 20:01:46,320 --> 20:01:52,800 it as an actual image on the 23479 20:01:50,080 --> 20:01:54,320 page same thing happens with the backend 23480 20:01:52,800 --> 20:01:58,160 responds with 23481 20:01:54,320 --> 20:02:01,800 HTML so at the Top If we change the url 23482 20:01:58,160 --> 20:02:05,120 path to slash 23483 20:02:01,800 --> 20:02:07,872 documentation and press 23484 20:02:05,120 --> 20:02:11,080 enter if the response from the back end 23485 20:02:07,872 --> 20:02:14,440 is HTML theow browser will display this 23486 20:02:11,080 --> 20:02:16,440 HTML as an actual web page instead of 23487 20:02:14,440 --> 20:02:19,800 raw 23488 20:02:16,440 --> 20:02:22,680 HTML so again using the browser is the 23489 20:02:19,800 --> 20:02:25,440 same thing as making a get 23490 20:02:22,680 --> 20:02:27,872 request and depending on the type of 23491 20:02:25,440 --> 20:02:31,600 response the browser can display the 23492 20:02:27,872 --> 20:02:34,552 response in a more useful way on the 23493 20:02:31,600 --> 20:02:36,920 page and that's it we learn the basics 23494 20:02:34,552 --> 20:02:39,000 of how a backend Works how to send 23495 20:02:36,920 --> 20:02:41,232 requests to a backend and how to send 23496 20:02:39,000 --> 20:02:44,552 requests to different different URL 23497 20:02:41,232 --> 20:02:49,232 paths so now let's go back into our 23498 20:02:44,552 --> 20:02:52,080 code and we'll remove this URL path and 23499 20:02:49,232 --> 20:02:56,080 just dodev so that we don't keep console 23500 20:02:52,080 --> 20:02:58,800 logging and image and then 23501 20:02:56,080 --> 20:03:03,280 save and let's save this practice code 23502 20:02:58,800 --> 20:03:03,280 into git we'll open the kit 23503 20:03:03,320 --> 20:03:09,120 section in backend practice we learn how 23504 20:03:06,320 --> 20:03:12,280 to make requests to the backend and in 23505 20:03:09,120 --> 20:03:13,480 checkout. as we loaded that backend 23506 20:03:12,280 --> 20:03:16,232 practice 23507 20:03:13,480 --> 20:03:17,480 file so let's type a message describing 23508 20:03:16,232 --> 20:03:21,720 our 23509 20:03:17,480 --> 20:03:26,320 changes add backend 23510 20:03:21,720 --> 20:03:26,320 practice file and 23511 20:03:31,080 --> 20:03:35,640 commit now that we learned what a 23512 20:03:33,232 --> 20:03:38,720 backend is and how to communicate with a 23513 20:03:35,640 --> 20:03:40,192 backend let's use a backend in our 23514 20:03:38,720 --> 20:03:43,400 project 23515 20:03:40,192 --> 20:03:44,832 so first we're going to open our code 23516 20:03:43,400 --> 20:03:46,000 and we're actually going to open 23517 20:03:44,832 --> 20:03:48,720 checkout. 23518 20:03:46,000 --> 20:03:50,832 JS and we're going to comment out this 23519 20:03:48,720 --> 20:03:53,480 import statement because we're not going 23520 20:03:50,832 --> 20:03:55,800 to run the practice file anymore so 23521 20:03:53,480 --> 20:03:59,080 we'll type double slash to comment that 23522 20:03:55,800 --> 20:03:59,080 out and 23523 20:03:59,440 --> 20:04:06,360 save next super simple backend dodev has 23524 20:04:03,192 --> 20:04:09,320 a URL path called slash 23525 20:04:06,360 --> 20:04:12,920 products this gives us a list of all the 23526 20:04:09,320 --> 20:04:16,192 products in the Amazon Project so first 23527 20:04:12,920 --> 20:04:19,360 let's open our browser and visit this 23528 20:04:16,192 --> 20:04:23,400 URL path to see what the response looks 23529 20:04:19,360 --> 20:04:26,080 like we'll open this tab to super simple 23530 20:04:23,400 --> 20:04:30,000 backend dodev and we're going to change 23531 20:04:26,080 --> 20:04:33,552 the url path at the end to slash 23532 20:04:30,000 --> 20:04:33,552 products and press 23533 20:04:33,920 --> 20:04:40,680 enter and you can see that this URL path 23534 20:04:37,400 --> 20:04:43,400 gives us a Json response so this 23535 20:04:40,680 --> 20:04:45,160 contains all the details of the products 23536 20:04:43,400 --> 20:04:48,192 for our 23537 20:04:45,160 --> 20:04:50,872 project now instead of using a file on 23538 20:04:48,192 --> 20:04:54,832 our computer to load the products let's 23539 20:04:50,872 --> 20:04:54,832 use the backend to load the 23540 20:04:54,960 --> 20:05:01,872 products so let's go back into our 23541 20:04:58,192 --> 20:05:05,400 code and on the left we're going to open 23542 20:05:01,872 --> 20:05:08,800 data/ products. 23543 20:05:05,400 --> 20:05:12,040 JS and we're going to scroll down to the 23544 20:05:08,800 --> 20:05:13,800 array of prodct S at the end so now 23545 20:05:12,040 --> 20:05:15,552 instead of loading the products from 23546 20:05:13,800 --> 20:05:18,320 this file we're going to load the 23547 20:05:15,552 --> 20:05:21,120 products from the back end so first 23548 20:05:18,320 --> 20:05:23,440 let's comment out all this code if you 23549 20:05:21,120 --> 20:05:26,320 need to you can click this arrow on the 23550 20:05:23,440 --> 20:05:29,000 left to collapse this array and we're 23551 20:05:26,320 --> 20:05:32,920 going to comment this out using slash 23552 20:05:29,000 --> 20:05:36,000 star and at the end star 23553 20:05:32,920 --> 20:05:39,720 slash and above this we're going to load 23554 20:05:36,000 --> 20:05:41,640 the products from the back end 23555 20:05:39,720 --> 20:05:45,640 first let's create a variable to save 23556 20:05:41,640 --> 20:05:49,800 the products again so we use let 23557 20:05:45,640 --> 20:05:52,120 products equal and empty array and let's 23558 20:05:49,800 --> 20:05:55,160 export this so we can use it outside of 23559 20:05:52,120 --> 20:05:58,080 this file like before at the front we'll 23560 20:05:55,160 --> 20:05:58,080 type 23561 20:05:58,600 --> 20:06:04,832 export next let's create a function to 23562 20:06:01,960 --> 20:06:06,400 load these products from the back end 23563 20:06:04,832 --> 20:06:10,640 we'll create a 23564 20:06:06,400 --> 20:06:13,960 function and let's name it load products 23565 20:06:10,640 --> 20:06:13,960 brackets and curly 23566 20:06:14,080 --> 20:06:20,280 brackets and inside here we're going to 23567 20:06:16,600 --> 20:06:22,040 create an XML HTTP request again to make 23568 20:06:20,280 --> 20:06:24,832 a request to my 23569 20:06:22,040 --> 20:06:31,192 backend so to create a request we're 23570 20:06:24,832 --> 20:06:34,800 going to use new in the class XML HTTP 23571 20:06:31,192 --> 20:06:38,232 request and brackets so this will 23572 20:06:34,800 --> 20:06:40,120 generate a new request object and let's 23573 20:06:38,232 --> 20:06:42,800 save this in a variable 23574 20:06:40,120 --> 20:06:47,920 so at the front we'll use const and 23575 20:06:42,800 --> 20:06:50,760 we'll name it xhr equals this 23576 20:06:47,920 --> 20:06:56,040 object next we're going to set up the 23577 20:06:50,760 --> 20:06:58,960 request using xhr doop and 23578 20:06:56,040 --> 20:07:00,872 brackets so open takes two parameters 23579 20:06:58,960 --> 20:07:03,512 the first parameter is the type of 23580 20:07:00,872 --> 20:07:05,800 request that we want to send in this 23581 20:07:03,512 --> 20:07:09,640 case the type of request will be the 23582 20:07:05,800 --> 20:07:11,280 string get 23583 20:07:09,640 --> 20:07:13,760 and we're going to give open a second 23584 20:07:11,280 --> 20:07:16,920 parameter which is the URL that we want 23585 20:07:13,760 --> 20:07:22,040 to send this request to so we'll give it 23586 20:07:16,920 --> 20:07:30,232 a string and the URL of my backend is 23587 20:07:22,040 --> 20:07:33,680 https colon slash super simple backend 23588 20:07:30,232 --> 20:07:37,440 dodev and then we'll add the URL path 23589 20:07:33,680 --> 20:07:37,440 which is slash 23590 20:07:37,480 --> 20:07:47,872 products finally we're going to send 23591 20:07:40,440 --> 20:07:47,872 this request using the code xr. send and 23592 20:07:48,512 --> 20:07:54,960 brackets so this will create and set up 23593 20:07:51,160 --> 20:07:57,872 a request and then send it to my backend 23594 20:07:54,960 --> 20:07:59,720 now remember that do send is 23595 20:07:57,872 --> 20:08:02,360 asynchronous that means it will just 23596 20:07:59,720 --> 20:08:04,280 send the request but it will not wait 23597 20:08:02,360 --> 20:08:07,960 for a response to come 23598 20:08:04,280 --> 20:08:10,232 back in order to wait for a response 23599 20:08:07,960 --> 20:08:12,832 above this code we're going to create 23600 20:08:10,232 --> 20:08:15,160 some new lines and set up an event 23601 20:08:12,832 --> 20:08:21,640 listener using 23602 20:08:15,160 --> 20:08:24,552 xr. add event listener and 23603 20:08:21,640 --> 20:08:26,192 brackets and this takes two parameters 23604 20:08:24,552 --> 20:08:29,680 the first one is the event we want to 23605 20:08:26,192 --> 20:08:32,680 listen for in this case it's the string 23606 20:08:29,680 --> 20:08:35,120 load which means the response has 23607 20:08:32,680 --> 20:08:38,160 loaded next we're going to give it a 23608 20:08:35,120 --> 20:08:40,600 second parameter which is a function so 23609 20:08:38,160 --> 20:08:43,232 brackets Arrow and curly 23610 20:08:40,600 --> 20:08:47,600 brackets and this is a function that we 23611 20:08:43,232 --> 20:08:51,360 want to run after the response has 23612 20:08:47,600 --> 20:08:57,192 loaded so inside here after the response 23613 20:08:51,360 --> 20:09:00,120 has loaded we can access it using xhr do 23614 20:08:57,192 --> 20:09:04,232 response for now let's console.log this 23615 20:09:00,120 --> 20:09:08,760 to see what it looks like so we console. 23616 20:09:04,232 --> 20:09:08,760 log and brackets 23617 20:09:09,920 --> 20:09:16,160 finally let's run this function so that 23618 20:09:12,640 --> 20:09:20,360 we run all this code at the bottom we're 23619 20:09:16,160 --> 20:09:23,192 going to load products and 23620 20:09:20,360 --> 20:09:26,040 brackets and 23621 20:09:23,192 --> 20:09:28,080 save so this will send a request to my 23622 20:09:26,040 --> 20:09:32,120 backend and then once we get the 23623 20:09:28,080 --> 20:09:34,920 response it will display it in the 23624 20:09:32,120 --> 20:09:37,760 console so now let's go to our 23625 20:09:34,920 --> 20:09:39,400 website we're going to open the project 23626 20:09:37,760 --> 20:09:41,232 tab 23627 20:09:39,400 --> 20:09:44,120 and don't worry if the page is blank for 23628 20:09:41,232 --> 20:09:46,400 you we're going to fix this in a bit for 23629 20:09:44,120 --> 20:09:47,192 now we're going to open the console by 23630 20:09:46,400 --> 20:09:48,720 right 23631 20:09:47,192 --> 20:09:52,400 clicking 23632 20:09:48,720 --> 20:09:52,400 inspect and open the 23633 20:09:52,720 --> 20:09:58,872 console and in here you'll see that xr. 23634 20:09:55,640 --> 20:10:01,120 response gave us this Json and this 23635 20:09:58,872 --> 20:10:03,720 represents all the products in our 23636 20:10:01,120 --> 20:10:03,720 Amazon 23637 20:10:05,280 --> 20:10:10,440 Project now that we have all this Json 23638 20:10:08,400 --> 20:10:13,000 we're going to convert this back into a 23639 20:10:10,440 --> 20:10:16,720 JavaScript object and then use it in the 23640 20:10:13,000 --> 20:10:20,080 rest of our code let's go back to our 23641 20:10:16,720 --> 20:10:22,680 code and here the response is a 23642 20:10:20,080 --> 20:10:25,552 Json we're going to convert it back into 23643 20:10:22,680 --> 20:10:29,160 a JavaScript object or in this case a 23644 20:10:25,552 --> 20:10:33,800 JavaScript array using the code 23645 20:10:29,160 --> 20:10:33,800 json. pars and 23646 20:10:34,192 --> 20:10:38,920 brackets so once we' converted this back 23647 20:10:36,720 --> 20:10:41,480 to an array we're going to replace the 23648 20:10:38,920 --> 20:10:44,640 product up here with what we loaded from 23649 20:10:41,480 --> 20:10:48,120 the back end to do that we'll do 23650 20:10:44,640 --> 20:10:51,760 products and make it equal to this 23651 20:10:48,120 --> 20:10:51,760 result that we got from the back 23652 20:10:51,872 --> 20:10:57,552 end now one more thing we need to do is 23653 20:10:54,960 --> 20:11:00,640 previously we converted the product 23654 20:10:57,552 --> 20:11:03,680 objects into a class so we're just going 23655 20:11:00,640 --> 20:11:08,232 to do that again with this 23656 20:11:03,680 --> 20:11:10,360 array so we'll select the code from map 23657 20:11:08,232 --> 20:11:13,680 all the way to the end 23658 20:11:10,360 --> 20:11:16,360 and then right click 23659 20:11:13,680 --> 20:11:19,192 copy and 23660 20:11:16,360 --> 20:11:21,320 here after this products array that we 23661 20:11:19,192 --> 20:11:24,600 got from the back 23662 20:11:21,320 --> 20:11:27,680 end right click 23663 20:11:24,600 --> 20:11:31,720 paste we're also going to transform this 23664 20:11:27,680 --> 20:11:35,000 array from objects into these 23665 20:11:31,720 --> 20:11:36,960 classes and at the end let's console.log 23666 20:11:35,000 --> 20:11:39,720 this products array to see what it looks 23667 20:11:36,960 --> 20:11:42,280 like now so we'll console 23668 20:11:39,720 --> 20:11:45,080 . log 23669 20:11:42,280 --> 20:11:48,232 products and 23670 20:11:45,080 --> 20:11:49,080 save and now if we open the console for 23671 20:11:48,232 --> 20:11:51,920 our 23672 20:11:49,080 --> 20:11:55,800 project you'll see that products is now 23673 20:11:51,920 --> 20:11:58,552 an array of product classes and clothing 23674 20:11:55,800 --> 20:12:02,920 classes so we converted the response 23675 20:11:58,552 --> 20:12:05,960 from a Json into a JavaScript array and 23676 20:12:02,920 --> 20:12:05,960 then into these 23677 20:12:06,440 --> 20:12:11,320 classes so that's how we load data from 23678 20:12:09,000 --> 20:12:14,280 the the back end and then convert it 23679 20:12:11,320 --> 20:12:17,400 into a JavaScript object or array that 23680 20:12:14,280 --> 20:12:17,400 we can use in our 23681 20:12:18,160 --> 20:12:24,720 code next let's go back to our code and 23682 20:12:22,920 --> 20:12:26,720 instead of console. loging the products 23683 20:12:24,720 --> 20:12:28,720 every time which will display a lot of 23684 20:12:26,720 --> 20:12:31,040 stuff in the console let's just 23685 20:12:28,720 --> 20:12:33,872 console.log a message to make sure that 23686 20:12:31,040 --> 20:12:39,680 this is working so we'll change this to 23687 20:12:33,872 --> 20:12:39,680 the string load products 23688 20:12:40,080 --> 20:12:45,832 and now that we have this function let's 23689 20:12:42,400 --> 20:12:48,640 use it in each of our 23690 20:12:45,832 --> 20:12:51,232 Pages first we're going to remove 23691 20:12:48,640 --> 20:12:53,360 calling this function for now and we're 23692 20:12:51,232 --> 20:12:57,640 going to export this function so we can 23693 20:12:53,360 --> 20:13:01,800 use it outside of this file so 23694 20:12:57,640 --> 20:13:01,800 export and save this 23695 20:13:01,872 --> 20:13:07,512 file and we're going to start from the 23696 20:13:05,440 --> 20:13:11,040 homepage so we'll scroll down to the 23697 20:13:07,512 --> 20:13:11,040 bottom to amazon. 23698 20:13:11,280 --> 20:13:19,280 JS and we're going to scroll to the 23699 20:13:15,232 --> 20:13:21,800 top and first we're going to import load 23700 20:13:19,280 --> 20:13:25,360 products we're already importing from 23701 20:13:21,800 --> 20:13:28,720 products. JS so we'll just add a comma 23702 20:13:25,360 --> 20:13:31,680 and load 23703 20:13:28,720 --> 20:13:35,720 products and now before we run all of 23704 20:13:31,680 --> 20:13:40,720 this code at the top we're going to load 23705 20:13:35,720 --> 20:13:42,680 the products first and brackets 23706 20:13:40,720 --> 20:13:46,000 so now let's 23707 20:13:42,680 --> 20:13:48,400 save and open the project in our 23708 20:13:46,000 --> 20:13:51,400 browser and we'll click the top left 23709 20:13:48,400 --> 20:13:53,080 icon to go to the 23710 20:13:51,400 --> 20:13:55,960 homepage and you'll see that the 23711 20:13:53,080 --> 20:13:57,120 homepage is actually empty so what went 23712 20:13:55,960 --> 20:14:01,440 wrong 23713 20:13:57,120 --> 20:14:04,960 here remember that HTTP requests are 23714 20:14:01,440 --> 20:14:08,160 asynchronous if we go back into our 23715 20:14:04,960 --> 20:14:09,800 code load products will send a request 23716 20:14:08,160 --> 20:14:11,872 to my backend 23717 20:14:09,800 --> 20:14:15,160 but it takes time for the request to 23718 20:14:11,872 --> 20:14:17,512 travel across the internet to my backend 23719 20:14:15,160 --> 20:14:20,120 and to travel 23720 20:14:17,512 --> 20:14:22,680 back so here it will just send the 23721 20:14:20,120 --> 20:14:26,000 request and at the next line the 23722 20:14:22,680 --> 20:14:28,120 response has not loaded yet so the 23723 20:14:26,000 --> 20:14:30,832 products array is still 23724 20:14:28,120 --> 20:14:35,120 empty and when we run this code with an 23725 20:14:30,832 --> 20:14:35,120 empty array the page will be 23726 20:14:35,400 --> 20:14:41,480 empty to solve this problem we need to 23727 20:14:38,440 --> 20:14:44,480 wait for this HTTP request to finish 23728 20:14:41,480 --> 20:14:46,320 first and for the response to come back 23729 20:14:44,480 --> 20:14:47,040 and then we're going to run the rest of 23730 20:14:46,320 --> 20:14:49,960 this 23731 20:14:47,040 --> 20:14:52,800 code so how do we do 23732 20:14:49,960 --> 20:14:55,440 that so we're going to go to the left 23733 20:14:52,800 --> 20:14:56,920 and we're going to scroll up to data/ 23734 20:14:55,440 --> 20:14:59,360 products. 23735 20:14:56,920 --> 20:15:03,832 JS and I'm going to drag this file to 23736 20:14:59,360 --> 20:15:07,232 the top so we can see the code side by 23737 20:15:03,832 --> 20:15:10,040 side and I'll scroll down to the HTTP 23738 20:15:07,232 --> 20:15:13,040 request 23739 20:15:10,040 --> 20:15:13,040 which is right 23740 20:15:13,192 --> 20:15:18,360 here and remember that we used an event 23741 20:15:16,232 --> 20:15:21,512 listener to wait for the response to 23742 20:15:18,360 --> 20:15:24,320 come back once the response is loaded 23743 20:15:21,512 --> 20:15:27,800 we're going to run this 23744 20:15:24,320 --> 20:15:31,600 function so basically we want to run all 23745 20:15:27,800 --> 20:15:36,000 of this code at the bottom of this event 23746 20:15:31,600 --> 20:15:38,232 listener so after we've loaded our 23747 20:15:36,000 --> 20:15:41,080 products so one way to solve this 23748 20:15:38,232 --> 20:15:43,320 problem is maybe we can just copy all 23749 20:15:41,080 --> 20:15:45,512 this code and then paste it at the 23750 20:15:43,320 --> 20:15:47,960 bottom 23751 20:15:45,512 --> 20:15:50,040 here now obviously this doesn't make 23752 20:15:47,960 --> 20:15:52,600 sense because we're copy pasting a bunch 23753 20:15:50,040 --> 20:15:54,512 of code and then we can't really reuse 23754 20:15:52,600 --> 20:15:57,600 this 23755 20:15:54,512 --> 20:16:00,400 function a better solution is we're 23756 20:15:57,600 --> 20:16:03,040 going to put all this code in a 23757 20:16:00,400 --> 20:16:06,512 function and then we can give this 23758 20:16:03,040 --> 20:16:09,160 function to load products let's do this 23759 20:16:06,512 --> 20:16:11,680 step by step 23760 20:16:09,160 --> 20:16:14,320 so first at the top let's create a 23761 20:16:11,680 --> 20:16:17,440 function to group all of this code 23762 20:16:14,320 --> 20:16:21,872 together we'll use the word 23763 20:16:17,440 --> 20:16:26,280 function and let's name it render 23764 20:16:21,872 --> 20:16:28,552 products grid brackets and curly 23765 20:16:26,280 --> 20:16:30,760 brackets and now we're going to move all 23766 20:16:28,552 --> 20:16:33,480 of this code into the 23767 20:16:30,760 --> 20:16:36,280 function so we can do that by removing 23768 20:16:33,480 --> 20:16:38,872 this closing curly 23769 20:16:36,280 --> 20:16:41,280 bracket and then at the bottom we we're 23770 20:16:38,872 --> 20:16:44,832 going to scroll down all the way to the 23771 20:16:41,280 --> 20:16:47,440 bottom here we're going to add back the 23772 20:16:44,832 --> 20:16:50,400 closing curler bracket to put all this 23773 20:16:47,440 --> 20:16:53,400 code inside the function and now let's 23774 20:16:50,400 --> 20:16:57,040 also select this code so we can fix the 23775 20:16:53,400 --> 20:16:58,360 index so we'll select it all the way up 23776 20:16:57,040 --> 20:17:03,160 to 23777 20:16:58,360 --> 20:17:06,160 here and then press tab to add an indet 23778 20:17:03,160 --> 20:17:09,600 so now we put all the code inside render 23779 20:17:06,160 --> 20:17:09,600 products grid 23780 20:17:09,960 --> 20:17:17,000 next we're going to put this function 23781 20:17:12,920 --> 20:17:19,120 inside load products using a parameter 23782 20:17:17,000 --> 20:17:20,832 so between these brackets we're going to 23783 20:17:19,120 --> 20:17:22,400 give it this 23784 20:17:20,832 --> 20:17:26,360 function 23785 20:17:22,400 --> 20:17:29,720 render products 23786 20:17:26,360 --> 20:17:32,960 grid so remember in JavaScript functions 23787 20:17:29,720 --> 20:17:33,800 are values and we can use a function as 23788 20:17:32,960 --> 20:17:36,960 a 23789 20:17:33,800 --> 20:17:41,120 parameter so now inside load products if 23790 20:17:36,960 --> 20:17:44,040 we go up here at the top this function 23791 20:17:41,120 --> 20:17:46,280 will be saved in a parameter so let's 23792 20:17:44,040 --> 20:17:50,080 create a parameter at the top and we'll 23793 20:17:46,280 --> 20:17:53,192 name it fun to say that this parameter 23794 20:17:50,080 --> 20:17:53,192 will contain a 23795 20:17:53,360 --> 20:18:00,640 function so we basically saved this 23796 20:17:56,800 --> 20:18:02,760 function inside this parameter and now 23797 20:18:00,640 --> 20:18:05,360 after we load the response we're going 23798 20:18:02,760 --> 20:18:05,360 to run the 23799 20:18:05,440 --> 20:18:11,400 function so at the bottom here after 23800 20:18:08,280 --> 20:18:16,680 we've created the products array we're 23801 20:18:11,400 --> 20:18:16,680 going to run font and 23802 20:18:16,872 --> 20:18:22,680 brackets so that's how we run all of 23803 20:18:19,600 --> 20:18:25,440 this code after we've loaded the 23804 20:18:22,680 --> 20:18:27,872 products we group this code into a 23805 20:18:25,440 --> 20:18:31,552 function and then we save the function 23806 20:18:27,872 --> 20:18:34,040 up here after we load the response we're 23807 20:18:31,552 --> 20:18:37,192 going to run the 23808 20:18:34,040 --> 20:18:40,000 function so let's save these two files 23809 20:18:37,192 --> 20:18:43,512 we'll save products. Js 23810 20:18:40,000 --> 20:18:45,920 and then down here we'll save amazon. 23811 20:18:43,512 --> 20:18:48,192 JS and then open our 23812 20:18:45,920 --> 20:18:50,760 website and you'll see that the products 23813 20:18:48,192 --> 20:18:53,320 loaded again because this time we're 23814 20:18:50,760 --> 20:18:56,400 waiting for the response to come back 23815 20:18:53,320 --> 20:18:56,400 and then we run our 23816 20:18:56,872 --> 20:19:04,360 code so that's how we wait for an HTTP 23817 20:19:00,832 --> 20:19:06,680 request to finish first and then we run 23818 20:19:04,360 --> 20:19:06,680 some 23819 20:19:07,400 --> 20:19:13,800 code by the the way this function that 23820 20:19:10,440 --> 20:19:17,040 we provide to load products this is 23821 20:19:13,800 --> 20:19:19,232 known as a call back because we're 23822 20:19:17,040 --> 20:19:22,960 giving it a function to run in the 23823 20:19:19,232 --> 20:19:27,920 future or to call in the future so this 23824 20:19:22,960 --> 20:19:30,440 function will be called back in the 23825 20:19:27,920 --> 20:19:33,640 future and we've actually used call 23826 20:19:30,440 --> 20:19:36,360 backs before when we wrote the code set 23827 20:19:33,640 --> 20:19:38,960 timeout we provided set timeout a 23828 20:19:36,360 --> 20:19:42,040 function to run in the future or or to 23829 20:19:38,960 --> 20:19:44,760 call in the future so this function is 23830 20:19:42,040 --> 20:19:47,640 also known as a call 23831 20:19:44,760 --> 20:19:50,232 back don't worry if this technique is a 23832 20:19:47,640 --> 20:19:52,280 little complicated at first the more you 23833 20:19:50,232 --> 20:19:54,192 work with callbacks the more you'll get 23834 20:19:52,280 --> 20:19:57,192 used to 23835 20:19:54,192 --> 20:19:59,960 it let's get some more practice and use 23836 20:19:57,192 --> 20:20:02,800 the backend and callbacks on the 23837 20:19:59,960 --> 20:20:07,120 checkout page so we're going to open 23838 20:20:02,800 --> 20:20:09,600 checkout. JS which is the checkout page 23839 20:20:07,120 --> 20:20:13,480 and first we're going to in import load 23840 20:20:09,600 --> 20:20:18,480 products so at the top we'll create an 23841 20:20:13,480 --> 20:20:21,640 import curly brackets load 23842 20:20:18,480 --> 20:20:25,000 products from a 23843 20:20:21,640 --> 20:20:28,080 string and we'll locate products. JS so 23844 20:20:25,000 --> 20:20:30,552 inside here we're in the scripts folder 23845 20:20:28,080 --> 20:20:32,800 we're going to get out of it using do do 23846 20:20:30,552 --> 20:20:35,232 slash and then we're going to go into 23847 20:20:32,800 --> 20:20:35,232 the data 23848 20:20:35,720 --> 20:20:44,320 folder and then we'll add slash data 23849 20:20:39,760 --> 20:20:44,320 slash and we'll load products. 23850 20:20:45,960 --> 20:20:54,192 JS and then down here we're also going 23851 20:20:49,160 --> 20:20:56,600 to load products and 23852 20:20:54,192 --> 20:20:59,600 brackets and again we're going to give 23853 20:20:56,600 --> 20:21:02,720 load products a call back or a function 23854 20:20:59,600 --> 20:21:05,400 that we want to run in the 23855 20:21:02,720 --> 20:21:07,440 future this time instead of creating a 23856 20:21:05,400 --> 20:21:09,192 whole new function we're just going to 23857 20:21:07,440 --> 20:21:15,400 create a function in inside these 23858 20:21:09,192 --> 20:21:15,400 brackets using brackets arrow and curly 23859 20:21:15,480 --> 20:21:20,400 brackets so we don't need to create a 23860 20:21:17,800 --> 20:21:23,232 function name every time we can also 23861 20:21:20,400 --> 20:21:25,280 just create an anonymous function or a 23862 20:21:23,232 --> 20:21:28,512 function without a 23863 20:21:25,280 --> 20:21:29,920 name and then give this to load products 23864 20:21:28,512 --> 20:21:32,920 to run in the 23865 20:21:29,920 --> 20:21:34,872 future now inside this Anonymous 23866 20:21:32,920 --> 20:21:38,760 function we're just going to move this 23867 20:21:34,872 --> 20:21:41,552 code into here so we'll select this code 23868 20:21:38,760 --> 20:21:44,800 and then rightclick 23869 20:21:41,552 --> 20:21:47,960 cut and then inside here we're going to 23870 20:21:44,800 --> 20:21:52,440 rightclick and 23871 20:21:47,960 --> 20:21:55,480 paste and if you need to you can fix the 23872 20:21:52,440 --> 20:21:59,280 formatting so again this is going to 23873 20:21:55,480 --> 20:22:02,192 save this function inside the parameter 23874 20:21:59,280 --> 20:22:05,680 fun and then we're going to call fun 23875 20:22:02,192 --> 20:22:09,360 after we've loaded all the 23876 20:22:05,680 --> 20:22:10,920 products so let's save this file 23877 20:22:09,360 --> 20:22:13,440 and open our 23878 20:22:10,920 --> 20:22:16,512 website and we'll click the cart in the 23879 20:22:13,440 --> 20:22:20,400 top right to go to the checkout page and 23880 20:22:16,512 --> 20:22:23,680 now the checkout page is loading 23881 20:22:20,400 --> 20:22:27,512 again so that's how we use a backend in 23882 20:22:23,680 --> 20:22:31,040 our project we need to send a request 23883 20:22:27,512 --> 20:22:35,192 and then use a call back to wait for a 23884 20:22:31,040 --> 20:22:38,192 response and then run the rest of our 23885 20:22:35,192 --> 20:22:39,552 code so now let's save our changes into 23886 20:22:38,192 --> 20:22:41,360 G 23887 20:22:39,552 --> 20:22:45,440 we'll go to the git 23888 20:22:41,360 --> 20:22:45,440 section and in products. 23889 20:22:49,320 --> 20:22:56,760 JS we created the load products 23890 20:22:54,160 --> 20:23:00,360 function in amazon. 23891 20:22:56,760 --> 20:23:01,872 JS we used load products and gave it a 23892 20:23:00,360 --> 20:23:06,000 call 23893 20:23:01,872 --> 20:23:09,040 back and in checkout. JS we also used 23894 20:23:06,000 --> 20:23:11,320 load products and we gave it a call back 23895 20:23:09,040 --> 20:23:13,872 using an anonymous function or a 23896 20:23:11,320 --> 20:23:16,552 function without a 23897 20:23:13,872 --> 20:23:18,160 name let's create a message describing 23898 20:23:16,552 --> 20:23:25,000 our 23899 20:23:18,160 --> 20:23:27,360 changes load products from the backend 23900 20:23:25,000 --> 20:23:27,360 and 23901 20:23:30,872 --> 20:23:36,040 commit next we're going to learn testing 23902 20:23:34,000 --> 20:23:38,512 with a 23903 20:23:36,040 --> 20:23:41,800 backend now that we're using a back back 23904 20:23:38,512 --> 20:23:45,360 end in our code we need to update our 23905 20:23:41,800 --> 20:23:47,920 tests first let's go to our code and 23906 20:23:45,360 --> 20:23:50,512 we're going to run our tests so we'll 23907 20:23:47,920 --> 20:23:52,872 scroll to the bottom and if you don't 23908 20:23:50,512 --> 20:23:56,320 have the tests open we can run them by 23909 20:23:52,872 --> 20:23:59,552 right clicking test.html and then click 23910 20:23:56,320 --> 20:24:02,080 open with live server now I already have 23911 20:23:59,552 --> 20:24:04,760 the test open in my browser so I'm just 23912 20:24:02,080 --> 20:24:07,832 going to open that 23913 20:24:04,760 --> 20:24:10,160 Tab and for the video I'll zoom in using 23914 20:24:07,832 --> 20:24:13,040 control equals or command 23915 20:24:10,160 --> 20:24:16,160 equals and we have some failing tests 23916 20:24:13,040 --> 20:24:18,600 now so if you did the exercises from the 23917 20:24:16,160 --> 20:24:20,640 previous few lessons you might have more 23918 20:24:18,600 --> 20:24:23,320 than two failing tests but we're going 23919 20:24:20,640 --> 20:24:25,232 to focus on fixing these two tests here 23920 20:24:23,320 --> 20:24:28,192 for the 23921 20:24:25,232 --> 20:24:31,960 video so first let's open the code for 23922 20:24:28,192 --> 20:24:34,600 our order summary tests we'll open vs 23923 20:24:31,960 --> 20:24:37,920 code and then on the left we're going to 23924 20:24:34,600 --> 20:24:40,960 go into the test folder SL checkout SL 23925 20:24:37,920 --> 20:24:40,960 Ord summary 23926 20:24:42,552 --> 20:24:48,512 test.js so now we're going to scroll 23927 20:24:44,760 --> 20:24:51,800 down in this test and notice that here 23928 20:24:48,512 --> 20:24:55,552 we are rendering the order summary which 23929 20:24:51,800 --> 20:24:58,760 creates the cart on the checkout 23930 20:24:55,552 --> 20:25:01,832 page however this card requires the 23931 20:24:58,760 --> 20:25:03,760 products to be loaded from the back end 23932 20:25:01,832 --> 20:25:06,480 and currently we're not loading the 23933 20:25:03,760 --> 20:25:09,160 products in our tests so that's why the 23934 20:25:06,480 --> 20:25:11,040 tests are failing we need to to load the 23935 20:25:09,160 --> 20:25:14,720 products 23936 20:25:11,040 --> 20:25:17,400 first to do that let's import the load 23937 20:25:14,720 --> 20:25:18,360 products function at the top so we'll 23938 20:25:17,400 --> 20:25:21,552 scroll 23939 20:25:18,360 --> 20:25:26,232 up and we're going to add another 23940 20:25:21,552 --> 20:25:30,400 import curly brackets the load products 23941 20:25:26,232 --> 20:25:33,040 function from a string and we'll locate 23942 20:25:30,400 --> 20:25:36,080 products. JS so we need to get out of 23943 20:25:33,040 --> 20:25:38,920 checkout and out of tests using do do 23944 20:25:36,080 --> 20:25:43,280 slash do do slash and and then we'll go 23945 20:25:38,920 --> 20:25:46,280 into the data folder data/ and load 23946 20:25:43,280 --> 20:25:46,280 products. 23947 20:25:46,400 --> 20:25:53,000 JS and now we could call load products 23948 20:25:50,400 --> 20:25:55,760 inside before each but this will load 23949 20:25:53,000 --> 20:25:58,192 the products before each of our tests so 23950 20:25:55,760 --> 20:26:00,440 it will load it multiple times and we 23951 20:25:58,192 --> 20:26:02,832 don't really need to do this we only 23952 20:26:00,440 --> 20:26:04,872 need to load the products once for all 23953 20:26:02,832 --> 20:26:07,832 of our 23954 20:26:04,872 --> 20:26:11,080 tests to do that we're going to use 23955 20:26:07,832 --> 20:26:13,040 another hook provided by Jasmine called 23956 20:26:11,080 --> 20:26:15,872 before 23957 20:26:13,040 --> 20:26:18,872 all so above before 23958 20:26:15,872 --> 20:26:23,160 each we're going to use the function 23959 20:26:18,872 --> 20:26:26,800 before all and 23960 20:26:23,160 --> 20:26:30,120 brackets so this is the before all hook 23961 20:26:26,800 --> 20:26:32,680 it will run a function before all of our 23962 20:26:30,120 --> 20:26:35,680 tests inside these brackets we're going 23963 20:26:32,680 --> 20:26:39,640 to give it a function to run so brackets 23964 20:26:35,680 --> 20:26:42,280 arrow and curly brackets 23965 20:26:39,640 --> 20:26:46,800 and now before all of our tests we're 23966 20:26:42,280 --> 20:26:49,680 going to load the products so let's load 23967 20:26:46,800 --> 20:26:52,280 products and 23968 20:26:49,680 --> 20:26:55,920 brackets and 23969 20:26:52,280 --> 20:26:58,872 save and now let's open our tests in the 23970 20:26:55,920 --> 20:27:00,192 browser and unfortunately the tests are 23971 20:26:58,872 --> 20:27:02,800 still 23972 20:27:00,192 --> 20:27:06,680 failing let's go back to our code and 23973 20:27:02,800 --> 20:27:09,232 see what the problem is we'll open vs 23974 20:27:06,680 --> 20:27:12,232 code and here 23975 20:27:09,232 --> 20:27:14,872 remember that load products is 23976 20:27:12,232 --> 20:27:17,040 asynchronous that means it just sends a 23977 20:27:14,872 --> 20:27:20,160 request to my back end but it doesn't 23978 20:27:17,040 --> 20:27:22,192 wait for the response to come back so it 23979 20:27:20,160 --> 20:27:25,480 would just send a request and then 23980 20:27:22,192 --> 20:27:28,920 continue with the rest of the test 23981 20:27:25,480 --> 20:27:31,920 code and unfortunately at this point the 23982 20:27:28,920 --> 20:27:34,160 response has not come back yet products 23983 20:27:31,920 --> 20:27:37,232 is still an empty 23984 20:27:34,160 --> 20:27:40,400 array to fix this issue we need to wait 23985 20:27:37,232 --> 20:27:44,440 for a load products to finish first and 23986 20:27:40,400 --> 20:27:48,000 then continue with the rest of the 23987 20:27:44,440 --> 20:27:50,480 test to do that Jasmine has a feature 23988 20:27:48,000 --> 20:27:53,552 for waiting for some code to finish 23989 20:27:50,480 --> 20:27:53,552 called a done 23990 20:27:53,600 --> 20:28:02,232 function so in before all we can give 23991 20:27:56,920 --> 20:28:05,680 this inner Function One parameter called 23992 20:28:02,232 --> 20:28:09,120 done so done is a function and it's 23993 20:28:05,680 --> 20:28:12,480 provided by Jasmine when we we add this 23994 20:28:09,120 --> 20:28:15,640 done parameter before all will not 23995 20:28:12,480 --> 20:28:18,280 automatically go to the next step now 23996 20:28:15,640 --> 20:28:20,800 it's just going to wait and it will only 23997 20:28:18,280 --> 20:28:24,552 go to the next step when we call this 23998 20:28:20,800 --> 20:28:28,080 done function so when we do done and 23999 20:28:24,552 --> 20:28:31,680 brackets then it will go to the next 24000 20:28:28,080 --> 20:28:34,800 step if we don't call done then before 24001 20:28:31,680 --> 20:28:38,160 all we'll just keep waiting forever so 24002 20:28:34,800 --> 20:28:40,640 this done function allows us to control 24003 20:28:38,160 --> 20:28:43,280 exactly when before all finishes and 24004 20:28:40,640 --> 20:28:46,080 goes to the next 24005 20:28:43,280 --> 20:28:48,800 step and this is what we need to fix the 24006 20:28:46,080 --> 20:28:51,320 test we need to wait for load products 24007 20:28:48,800 --> 20:28:56,160 to finish first and then we're going to 24008 20:28:51,320 --> 20:28:56,160 call done and then go to the next 24009 20:28:56,192 --> 20:29:02,320 step to wait for load products to finish 24010 20:29:00,280 --> 20:29:04,920 remember we're going to give it a 24011 20:29:02,320 --> 20:29:08,192 function so between these brackets we'll 24012 20:29:04,920 --> 20:29:10,640 create a function using brackets Arrow 24013 20:29:08,192 --> 20:29:13,320 and curly 24014 20:29:10,640 --> 20:29:15,720 brackets now once the products have 24015 20:29:13,320 --> 20:29:19,120 finished loading it will run the 24016 20:29:15,720 --> 20:29:22,400 function inside here we're going to call 24017 20:29:19,120 --> 20:29:24,400 done and 24018 20:29:22,400 --> 20:29:26,960 brackets so this makes sure that the 24019 20:29:24,400 --> 20:29:30,680 products load first and then we call 24020 20:29:26,960 --> 20:29:32,960 done and then it goes to the next step 24021 20:29:30,680 --> 20:29:35,280 which is the before each hook and then 24022 20:29:32,960 --> 20:29:38,280 eventually it runs the tests at the 24023 20:29:35,280 --> 20:29:38,280 bottom 24024 20:29:38,320 --> 20:29:43,872 so now let's 24025 20:29:40,000 --> 20:29:45,600 save and take a look at our test page 24026 20:29:43,872 --> 20:29:48,400 and you'll see that those order summary 24027 20:29:45,600 --> 20:29:52,320 tests are now passing if you have any 24028 20:29:48,400 --> 20:29:56,080 other fail tests I'll leave that as an 24029 20:29:52,320 --> 20:29:56,080 exercise let's go back to our 24030 20:29:56,960 --> 20:30:03,120 code and that's how we test with a 24031 20:30:00,160 --> 20:30:05,680 backend we wait until the backend code 24032 20:30:03,120 --> 20:30:08,800 is finished and then we use the done 24033 20:30:05,680 --> 20:30:12,232 function to control exactly when to go 24034 20:30:08,800 --> 20:30:15,600 to the next step by the way we can also 24035 20:30:12,232 --> 20:30:20,360 use done inside before each and inside 24036 20:30:15,600 --> 20:30:20,360 it as well to wait for backend code to 24037 20:30:20,640 --> 20:30:26,040 finish now let's save our changes into 24038 20:30:23,192 --> 20:30:28,920 git we'll open the get section and 24039 20:30:26,040 --> 20:30:31,960 inside order summary test we imported 24040 20:30:28,920 --> 20:30:34,760 load products and then we called load 24041 20:30:31,960 --> 20:30:36,320 products and use the done function to 24042 20:30:34,760 --> 20:30:39,800 make sure that the products are loaded 24043 20:30:36,320 --> 20:30:42,512 first and then we go to The Next 24044 20:30:39,800 --> 20:30:43,872 Step let's create a message describing 24045 20:30:42,512 --> 20:30:51,552 our 24046 20:30:43,872 --> 20:30:53,920 changes load products in before all hook 24047 20:30:51,552 --> 20:30:53,920 and 24048 20:30:58,720 --> 20:31:03,552 commit next we're going to learn 24049 20:31:01,320 --> 20:31:06,832 promises which are a better way to 24050 20:31:03,552 --> 20:31:09,480 handle asynchronous code promises are 24051 20:31:06,832 --> 20:31:12,080 similar to Jasmine done function that we 24052 20:31:09,480 --> 20:31:14,960 just learned they let us wait for some 24053 20:31:12,080 --> 20:31:17,552 asynchronous code to finish before going 24054 20:31:14,960 --> 20:31:17,552 to the next 24055 20:31:18,600 --> 20:31:25,640 step so now let's create our first 24056 20:31:22,160 --> 20:31:27,600 promise we're going to open our code and 24057 20:31:25,640 --> 20:31:30,440 on the left we're going to go to 24058 20:31:27,600 --> 20:31:30,440 checkout. 24059 20:31:31,512 --> 20:31:38,640 JS and in here at the top we're going to 24060 20:31:35,832 --> 20:31:40,960 practice promises 24061 20:31:38,640 --> 20:31:46,120 promise is a class so we're going to 24062 20:31:40,960 --> 20:31:49,600 type the code new capital P promise and 24063 20:31:46,120 --> 20:31:52,360 brackets to create a new promise 24064 20:31:49,600 --> 20:31:55,920 object now inside these brackets we're 24065 20:31:52,360 --> 20:31:58,720 going to give it a function so brackets 24066 20:31:55,920 --> 20:32:01,440 arrow and curly 24067 20:31:58,720 --> 20:32:04,440 brackets so that's just how promises are 24068 20:32:01,440 --> 20:32:06,920 designed it's a built-in class and when 24069 20:32:04,440 --> 20:32:09,760 we create a promise we need to give it a 24070 20:32:06,920 --> 20:32:09,760 function 24071 20:32:10,080 --> 20:32:15,832 so when we create This Promise it's 24072 20:32:12,280 --> 20:32:15,832 going to run this function 24073 20:32:16,872 --> 20:32:23,960 immediately so inside here let's 24074 20:32:21,040 --> 20:32:27,160 console.log the string 24075 20:32:23,960 --> 20:32:30,000 promise to show that it runs 24076 20:32:27,160 --> 20:32:32,800 immediately so we'll save this 24077 20:32:30,000 --> 20:32:35,360 file and then go to the 24078 20:32:32,800 --> 20:32:38,000 website and we'll open the tab for our 24079 20:32:35,360 --> 20:32:39,640 project 24080 20:32:38,000 --> 20:32:43,080 and I'm going to zoom out a bit by 24081 20:32:39,640 --> 20:32:43,080 pressing control or command 24082 20:32:43,232 --> 20:32:48,440 minus and then open the console right 24083 20:32:46,720 --> 20:32:51,400 click 24084 20:32:48,440 --> 20:32:55,512 inspect and the 24085 20:32:51,400 --> 20:32:55,512 console and you'll see a logged 24086 20:32:55,600 --> 20:33:02,872 promise so when we create a promise it 24087 20:32:59,080 --> 20:33:02,872 runs this inner function 24088 20:33:03,512 --> 20:33:09,512 immediately now this inner function gets 24089 20:33:06,440 --> 20:33:13,600 a parameter called called 24090 20:33:09,512 --> 20:33:16,480 resolve and resolve is a function and it 24091 20:33:13,600 --> 20:33:19,920 works very similar to Jasmine's done 24092 20:33:16,480 --> 20:33:22,960 function so resolve lets us control when 24093 20:33:19,920 --> 20:33:22,960 to go to the next 24094 20:33:23,192 --> 20:33:29,000 step for example let's add some 24095 20:33:26,192 --> 20:33:32,360 asynchronous code into this 24096 20:33:29,000 --> 20:33:34,800 promise we'll remove the 24097 20:33:32,360 --> 20:33:37,760 console.log and we're going to run load 24098 20:33:34,800 --> 20:33:43,480 products which is asynchronous 24099 20:33:37,760 --> 20:33:45,760 so here we'll type load products and 24100 20:33:43,480 --> 20:33:48,360 brackets and now we're going to give 24101 20:33:45,760 --> 20:33:50,320 load products a callback function like 24102 20:33:48,360 --> 20:33:54,960 we did down 24103 20:33:50,320 --> 20:33:57,760 here brackets arrow and curly 24104 20:33:54,960 --> 20:34:00,160 brackets so it will run this function 24105 20:33:57,760 --> 20:34:02,552 after load products is 24106 20:34:00,160 --> 20:34:05,232 finished once this is finished we want 24107 20:34:02,552 --> 20:34:11,360 to go to the next step so here we're 24108 20:34:05,232 --> 20:34:13,832 going to call resolve so resolve and 24109 20:34:11,360 --> 20:34:16,440 brackets so you can see that the syntax 24110 20:34:13,832 --> 20:34:19,640 here is very similar to jasmin's done 24111 20:34:16,440 --> 20:34:21,680 function we run some asynchronous code 24112 20:34:19,640 --> 20:34:26,232 and then we wait for it to finish and 24113 20:34:21,680 --> 20:34:26,232 then we call resolve to go to the next 24114 20:34:26,600 --> 20:34:33,552 step now what is the next step to This 24115 20:34:31,120 --> 20:34:36,360 Promise so you might think that this 24116 20:34:33,552 --> 20:34:39,720 code down here is the next 24117 20:34:36,360 --> 20:34:42,640 step however a promise actually creates 24118 20:34:39,720 --> 20:34:44,120 a separate line of code or a separate 24119 20:34:42,640 --> 20:34:48,040 thread of 24120 20:34:44,120 --> 20:34:51,040 code so here these two groups of code 24121 20:34:48,040 --> 20:34:52,832 are sort of running at the same time 24122 20:34:51,040 --> 20:34:55,600 feel free to pause the video to get a 24123 20:34:52,832 --> 20:34:58,440 longer look at this 24124 20:34:55,600 --> 20:35:01,320 diagram the reason promises are designed 24125 20:34:58,440 --> 20:35:04,960 this way is it allows JavaScript to do 24126 20:35:01,320 --> 20:35:08,160 multiple things at the same 24127 20:35:04,960 --> 20:35:11,872 time so when the promise finishes it can 24128 20:35:08,160 --> 20:35:13,800 do a Next Step but this next step is 24129 20:35:11,872 --> 20:35:17,000 going to be separate from the rest of 24130 20:35:13,800 --> 20:35:19,512 the code it sort of does the next step 24131 20:35:17,000 --> 20:35:19,512 on the 24132 20:35:19,600 --> 20:35:26,160 side so right now this promise creates a 24133 20:35:23,760 --> 20:35:28,360 separate thread of code but this 24134 20:35:26,160 --> 20:35:32,400 separate thread doesn't actually have a 24135 20:35:28,360 --> 20:35:32,400 next step so we're going to add 24136 20:35:33,320 --> 20:35:39,872 one to add a next step to a promise at 24137 20:35:37,192 --> 20:35:44,552 the end and we're going to use a method 24138 20:35:39,872 --> 20:35:47,760 called dot then and 24139 20:35:44,552 --> 20:35:50,832 brackets and we need to give then a 24140 20:35:47,760 --> 20:35:52,480 function so we're going to type brackets 24141 20:35:50,832 --> 20:35:56,960 arrow and Cur 24142 20:35:52,480 --> 20:36:01,760 brackets and inside here let's just 24143 20:35:56,960 --> 20:36:05,440 console.log the string Next 24144 20:36:01,760 --> 20:36:08,640 Step so first we run this asynchronous 24145 20:36:05,440 --> 20:36:11,280 code and then we wait for it to finish 24146 20:36:08,640 --> 20:36:14,440 and then we run resolve and resolve 24147 20:36:11,280 --> 20:36:14,440 makes it go to the next 24148 20:36:14,480 --> 20:36:19,640 step to make this easier to see in the 24149 20:36:16,960 --> 20:36:23,040 console let's actually add some console. 24150 20:36:19,640 --> 20:36:26,160 logs to track the progress of this code 24151 20:36:23,040 --> 20:36:30,360 so at the top let's 24152 20:36:26,160 --> 20:36:35,640 console.log the string start 24153 20:36:30,360 --> 20:36:38,320 promise and then in here let's 24154 20:36:35,640 --> 20:36:40,832 console.log the string 24155 20:36:38,320 --> 20:36:43,760 finished 24156 20:36:40,832 --> 20:36:45,832 loading and we'll also comment out the 24157 20:36:43,760 --> 20:36:48,480 code that we had 24158 20:36:45,832 --> 20:36:52,720 before so we'll type 24159 20:36:48,480 --> 20:36:54,192 slashstar and at the bottom star slash 24160 20:36:52,720 --> 20:36:57,872 and now let's 24161 20:36:54,192 --> 20:36:57,872 save and check our 24162 20:37:00,760 --> 20:37:06,720 console so let's go through this step by 24163 20:37:03,680 --> 20:37:07,960 step first we start the promise which we 24164 20:37:06,720 --> 20:37:10,480 see here 24165 20:37:07,960 --> 20:37:13,720 and then we call load products which we 24166 20:37:10,480 --> 20:37:16,832 see here once the products are finished 24167 20:37:13,720 --> 20:37:19,600 loading it's going to run this function 24168 20:37:16,832 --> 20:37:21,720 and then we'll display finish loading 24169 20:37:19,600 --> 20:37:24,512 and then we're going to call resolve 24170 20:37:21,720 --> 20:37:28,280 which goes to the next step which runs 24171 20:37:24,512 --> 20:37:32,000 this function and we display Next 24172 20:37:28,280 --> 20:37:33,800 Step so resolve lets us control when to 24173 20:37:32,000 --> 20:37:37,160 go to the next 24174 20:37:33,800 --> 20:37:39,720 step in this case we waited until load 24175 20:37:37,160 --> 20:37:42,872 product prods was finished and then we 24176 20:37:39,720 --> 20:37:46,040 called resolve to go to the next 24177 20:37:42,872 --> 20:37:48,640 step so that's how promises work they 24178 20:37:46,040 --> 20:37:51,640 let us split our code into separate 24179 20:37:48,640 --> 20:37:56,000 steps and then we can wait for one step 24180 20:37:51,640 --> 20:37:59,232 to finish before going to the next 24181 20:37:56,000 --> 20:38:01,192 step now that we understand promises 24182 20:37:59,232 --> 20:38:06,160 let's convert the code that we had 24183 20:38:01,192 --> 20:38:06,160 before from a call back to using a 24184 20:38:06,360 --> 20:38:11,120 promise so first let's remove these 24185 20:38:08,680 --> 20:38:13,640 console. logs to make our code a little 24186 20:38:11,120 --> 20:38:13,640 easier to 24187 20:38:15,512 --> 20:38:21,800 read now in our previous code we 24188 20:38:18,872 --> 20:38:25,080 actually had two steps the first step is 24189 20:38:21,800 --> 20:38:28,400 to call load products and then once this 24190 20:38:25,080 --> 20:38:31,960 is finished the second step is to call 24191 20:38:28,400 --> 20:38:34,440 render order summary and render payment 24192 20:38:31,960 --> 20:38:37,000 summary and in our promise we have the 24193 20:38:34,440 --> 20:38:38,400 same thing our first step is load 24194 20:38:37,000 --> 20:38:40,000 product 24195 20:38:38,400 --> 20:38:42,832 once this is finished we're going to 24196 20:38:40,000 --> 20:38:45,680 call resolve and then go to the Second 24197 20:38:42,832 --> 20:38:49,920 Step so we're just going to copy this 24198 20:38:45,680 --> 20:38:52,832 second step into the second step up here 24199 20:38:49,920 --> 20:38:54,640 so we'll select this code and then right 24200 20:38:52,832 --> 20:38:58,320 click 24201 20:38:54,640 --> 20:39:01,760 copy and in do then we're going to right 24202 20:38:58,320 --> 20:39:01,760 click and 24203 20:39:04,440 --> 20:39:10,320 paste and now let's save 24204 20:39:08,080 --> 20:39:13,120 and let's check the web page so we'll 24205 20:39:10,320 --> 20:39:16,320 open the project and you can see that 24206 20:39:13,120 --> 20:39:16,320 everything is loading 24207 20:39:18,280 --> 20:39:23,600 correctly So This Promise does the same 24208 20:39:20,960 --> 20:39:25,960 thing as a call back that we had before 24209 20:39:23,600 --> 20:39:29,280 both of them have two steps the first 24210 20:39:25,960 --> 20:39:32,320 step is to load the products and then we 24211 20:39:29,280 --> 20:39:34,480 wait for the products to finish loading 24212 20:39:32,320 --> 20:39:38,360 and then we do the Second Step which is 24213 20:39:34,480 --> 20:39:38,360 to render the page 24214 20:39:40,360 --> 20:39:45,320 so now you might be wondering why do we 24215 20:39:42,760 --> 20:39:48,360 use promises it looks like it's more 24216 20:39:45,320 --> 20:39:51,960 work and more code than just using a 24217 20:39:48,360 --> 20:39:54,680 callback so callbacks have a big problem 24218 20:39:51,960 --> 20:39:56,920 which is multiple callbacks cause a lot 24219 20:39:54,680 --> 20:39:56,920 of 24220 20:39:57,232 --> 20:40:02,640 nesting nesting means having code inside 24221 20:40:00,872 --> 20:40:05,872 code like 24222 20:40:02,640 --> 20:40:08,280 this to see why this is a problem let's 24223 20:40:05,872 --> 20:40:11,800 say that we want to load the cart from 24224 20:40:08,280 --> 20:40:14,800 the back end as well I actually set up a 24225 20:40:11,800 --> 20:40:17,000 URL path that we can use to practice 24226 20:40:14,800 --> 20:40:21,160 called SLC 24227 20:40:17,000 --> 20:40:24,832 cart so first let's go into products. 24228 20:40:21,160 --> 20:40:27,080 JS and we're going to copy load products 24229 20:40:24,832 --> 20:40:30,040 and change it to load the 24230 20:40:27,080 --> 20:40:32,400 cart so we'll select this 24231 20:40:30,040 --> 20:40:34,440 function and then 24232 20:40:32,400 --> 20:40:38,120 rightclick 24233 20:40:34,440 --> 20:40:39,872 copy and let's open cart. Js 24234 20:40:38,120 --> 20:40:42,800 since we're going to load the cart from 24235 20:40:39,872 --> 20:40:44,960 the back end and let's scroll down to 24236 20:40:42,800 --> 20:40:44,960 the 24237 20:40:46,000 --> 20:40:53,160 bottom and here we'll right click and 24238 20:40:50,440 --> 20:40:56,080 paste that 24239 20:40:53,160 --> 20:40:59,192 code and now instead of loading the 24240 20:40:56,080 --> 20:41:01,440 products let's load the cart from the 24241 20:40:59,192 --> 20:41:07,320 backend so first let's rename this 24242 20:41:01,440 --> 20:41:08,872 function to load Capital C cart 24243 20:41:07,320 --> 20:41:12,040 and the first thing we need to change is 24244 20:41:08,872 --> 20:41:15,600 the URL path so instead of loading SL 24245 20:41:12,040 --> 20:41:16,872 products we're going to load slash cart 24246 20:41:15,600 --> 20:41:20,920 from my 24247 20:41:16,872 --> 20:41:23,360 backend so normally when we load SL cart 24248 20:41:20,920 --> 20:41:25,640 the backend will give us a cart 24249 20:41:23,360 --> 20:41:28,192 array however since we're just 24250 20:41:25,640 --> 20:41:32,232 practicing my backend will just give us 24251 20:41:28,192 --> 20:41:32,232 some text when we load SL 24252 20:41:33,800 --> 20:41:40,080 cart next we don't need this products 24253 20:41:36,680 --> 20:41:41,160 code anymore so we can select it and 24254 20:41:40,080 --> 20:41:48,160 then delete 24255 20:41:41,160 --> 20:41:50,552 it and instead let's just console. log 24256 20:41:48,160 --> 20:41:56,000 brackets and we just display the 24257 20:41:50,552 --> 20:42:00,400 response that we get back xhr do 24258 20:41:56,000 --> 20:42:00,400 response and we can remove this other 24259 20:42:00,552 --> 20:42:04,000 console.log and 24260 20:42:05,232 --> 20:42:10,280 save so now let's use load cart in our 24261 20:42:08,512 --> 20:42:13,400 previous 24262 20:42:10,280 --> 20:42:15,552 example on the left we'll scroll down 24263 20:42:13,400 --> 20:42:20,040 and open checkout. 24264 20:42:15,552 --> 20:42:25,400 JS and at the top let's import load cart 24265 20:42:20,040 --> 20:42:29,480 so here we'll import curly brackets load 24266 20:42:25,400 --> 20:42:32,400 cart from a string and we'll go to the 24267 20:42:29,480 --> 20:42:38,480 data folder which is do 24268 20:42:32,400 --> 20:42:38,480 dotdata slash and we'll load C.J 24269 20:42:40,280 --> 20:42:46,872 and now we're going to use this in our 24270 20:42:42,600 --> 20:42:49,640 callback code to show the problem with 24271 20:42:46,872 --> 20:42:54,080 callbacks first let's comment out this 24272 20:42:49,640 --> 20:42:57,832 promise so we can focus on the Callback 24273 20:42:54,080 --> 20:43:00,640 code at the top we'll type 24274 20:42:57,832 --> 20:43:03,000 slashstar and here star 24275 20:43:00,640 --> 20:43:05,120 slash and we'll remove the comments for 24276 20:43:03,000 --> 20:43:08,280 the 24277 20:43:05,120 --> 20:43:10,640 Callback and after we load the products 24278 20:43:08,280 --> 20:43:13,400 we're also going to load the 24279 20:43:10,640 --> 20:43:17,832 cart so inside this function we're going 24280 20:43:13,400 --> 20:43:20,720 to type load cart brackets and we're 24281 20:43:17,832 --> 20:43:20,720 going to give it a 24282 20:43:22,280 --> 20:43:27,440 function so this function is going to 24283 20:43:24,512 --> 20:43:29,640 run after the cart has 24284 20:43:27,440 --> 20:43:32,080 loaded so now let's say that we want to 24285 20:43:29,640 --> 20:43:35,280 wait for the products and the cart to 24286 20:43:32,080 --> 20:43:38,280 load before we render the 24287 20:43:35,280 --> 20:43:42,120 page to do that we need to move this 24288 20:43:38,280 --> 20:43:45,120 code inside this inner 24289 20:43:42,120 --> 20:43:47,040 function so we'll select this and then 24290 20:43:45,120 --> 20:43:50,320 right click 24291 20:43:47,040 --> 20:43:52,800 cut inside load cart we're going to 24292 20:43:50,320 --> 20:43:56,552 right click and 24293 20:43:52,800 --> 20:43:59,680 paste and remove this extra 24294 20:43:56,552 --> 20:44:02,720 line so now this code is going to load 24295 20:43:59,680 --> 20:44:05,000 the products wait for it to finish and 24296 20:44:02,720 --> 20:44:07,480 then it's going to load the cart and 24297 20:44:05,000 --> 20:44:09,512 then wait for the cart to finish 24298 20:44:07,480 --> 20:44:13,120 and then it will render the page or 24299 20:44:09,512 --> 20:44:16,640 display the page so let's 24300 20:44:13,120 --> 20:44:19,320 save and check the 24301 20:44:16,640 --> 20:44:22,552 console and now you can see that we 24302 20:44:19,320 --> 20:44:25,800 loaded the products and then we loaded 24303 20:44:22,552 --> 20:44:29,480 the cart and if we open the 24304 20:44:25,800 --> 20:44:32,872 page everything was displayed 24305 20:44:29,480 --> 20:44:35,512 properly so now let's go back to our 24306 20:44:32,872 --> 20:44:38,192 code and notice that when we added 24307 20:44:35,512 --> 20:44:39,600 another call back we we created another 24308 20:44:38,192 --> 20:44:43,480 layer of 24309 20:44:39,600 --> 20:44:45,360 nesting so we have code inside code 24310 20:44:43,480 --> 20:44:48,720 inside 24311 20:44:45,360 --> 20:44:51,680 code the problem with this is that each 24312 20:44:48,720 --> 20:44:54,680 layer of nesting adds some indents at 24313 20:44:51,680 --> 20:44:57,360 the front or spaces at the 24314 20:44:54,680 --> 20:45:00,080 front this means if we have lots of 24315 20:44:57,360 --> 20:45:02,552 callbacks our code will become more and 24316 20:45:00,080 --> 20:45:05,552 more nested like 24317 20:45:02,552 --> 20:45:08,440 this this is a big problem with 24318 20:45:05,552 --> 20:45:11,680 callbacks multi multiple callbacks cause 24319 20:45:08,440 --> 20:45:15,440 a lot of nesting and all these indents 24320 20:45:11,680 --> 20:45:15,440 make our code hard to work 24321 20:45:16,760 --> 20:45:23,512 with the promises solve this problem by 24322 20:45:20,232 --> 20:45:27,000 letting us flatten our 24323 20:45:23,512 --> 20:45:29,512 code for example let's load the products 24324 20:45:27,000 --> 20:45:31,232 and the carts again but this time using 24325 20:45:29,512 --> 20:45:33,512 our promise 24326 20:45:31,232 --> 20:45:35,640 instead first let's comment out the 24327 20:45:33,512 --> 20:45:38,920 Callback using 24328 20:45:35,640 --> 20:45:41,960 slashstar and and at the bottom star 24329 20:45:38,920 --> 20:45:46,280 slash and we're going to uncomment the 24330 20:45:41,960 --> 20:45:46,280 promise so here we'll remove the 24331 20:45:48,720 --> 20:45:54,920 comments and then in the next 24332 20:45:51,920 --> 20:45:58,120 step instead of rendering the 24333 20:45:54,920 --> 20:46:03,280 page we're going to load the cart like 24334 20:45:58,120 --> 20:46:05,000 we did down here so we'll type load cart 24335 20:46:03,280 --> 20:46:08,160 and 24336 20:46:05,000 --> 20:46:11,120 brackets now we have a problem here we 24337 20:46:08,160 --> 20:46:15,000 want to wait for load card to finish and 24338 20:46:11,120 --> 20:46:17,720 then go to the next step however we 24339 20:46:15,000 --> 20:46:21,400 usually do this using resolve but we 24340 20:46:17,720 --> 20:46:25,160 don't have resolve inside this 24341 20:46:21,400 --> 20:46:28,480 function to solve this problem inside. 24342 20:46:25,160 --> 20:46:32,512 then we can actually return a new 24343 20:46:28,480 --> 20:46:38,280 promise so at the top we're going to 24344 20:46:32,512 --> 20:46:40,360 return a new promise and 24345 20:46:38,280 --> 20:46:45,000 brackets so we're going to give this 24346 20:46:40,360 --> 20:46:48,552 promise a function just like we did up 24347 20:46:45,000 --> 20:46:49,800 here so we'll type brackets arrow and 24348 20:46:48,552 --> 20:46:52,400 curly 24349 20:46:49,800 --> 20:46:56,360 brackets and this function gets one 24350 20:46:52,400 --> 20:46:56,360 parameter called 24351 20:46:56,680 --> 20:47:03,600 resolve so inside do then if we want to 24352 20:47:00,920 --> 20:47:08,760 use resolve to wait for some code to 24353 20:47:03,600 --> 20:47:08,760 finish we can return a new promise 24354 20:47:09,160 --> 20:47:14,832 so now let's move our code into this new 24355 20:47:11,440 --> 20:47:19,160 promise so we can use resolve so we'll 24356 20:47:14,832 --> 20:47:23,360 select this code and then right click 24357 20:47:19,160 --> 20:47:25,232 cut and inside here we're going to right 24358 20:47:23,360 --> 20:47:28,720 click and 24359 20:47:25,232 --> 20:47:31,360 paste and remove these new 24360 20:47:28,720 --> 20:47:34,232 lines and we can give load card a 24361 20:47:31,360 --> 20:47:38,600 function to run when it's finished so 24362 20:47:34,232 --> 20:47:41,800 we'll type brackets arrow and and curler 24363 20:47:38,600 --> 20:47:44,800 brackets once load card is finished it 24364 20:47:41,800 --> 20:47:47,080 runs this function and then inside here 24365 20:47:44,800 --> 20:47:52,160 we're going to call resolve so that we 24366 20:47:47,080 --> 20:47:55,640 move to the next step so here we'll call 24367 20:47:52,160 --> 20:47:55,640 resolve and 24368 20:47:55,872 --> 20:48:01,760 brackets so now this will go to the next 24369 20:47:59,232 --> 20:48:04,640 step and we currently don't have a next 24370 20:48:01,760 --> 20:48:09,000 step to This Promise so let's add The 24371 20:48:04,640 --> 20:48:12,872 Next Step at the end to add another step 24372 20:48:09,000 --> 20:48:16,080 we're going to use dot then 24373 20:48:12,872 --> 20:48:20,360 brackets and we'll give this a function 24374 20:48:16,080 --> 20:48:20,360 brackets arrow and curler 24375 20:48:20,920 --> 20:48:26,512 brackets so the third step is to render 24376 20:48:24,600 --> 20:48:29,280 the page so we're going to do the same 24377 20:48:26,512 --> 20:48:33,800 thing inside 24378 20:48:29,280 --> 20:48:37,080 here so let's select this code and we'll 24379 20:48:33,800 --> 20:48:39,160 rightclick copy 24380 20:48:37,080 --> 20:48:42,232 inside the third step we're going to 24381 20:48:39,160 --> 20:48:45,000 right click and 24382 20:48:42,232 --> 20:48:46,280 paste and adjust the formatting if you 24383 20:48:45,000 --> 20:48:49,832 need 24384 20:48:46,280 --> 20:48:53,552 to so that's how we do these three steps 24385 20:48:49,832 --> 20:48:57,000 in the Callback using a 24386 20:48:53,552 --> 20:49:00,000 promise we do the first step wait for it 24387 20:48:57,000 --> 20:49:03,480 to finish call resolve and then it goes 24388 20:49:00,000 --> 20:49:06,720 to the second step we do the Second Step 24389 20:49:03,480 --> 20:49:10,040 wait for it to finish call resolve and 24390 20:49:06,720 --> 20:49:10,040 and then we go to the third 24391 20:49:10,080 --> 20:49:16,920 step so we promise let us have as many 24392 20:49:13,552 --> 20:49:19,920 steps as we want and we can use resolve 24393 20:49:16,920 --> 20:49:22,800 to wait for each step to finish before 24394 20:49:19,920 --> 20:49:26,600 going to the next 24395 20:49:22,800 --> 20:49:29,120 step so let's save this and check the 24396 20:49:26,600 --> 20:49:30,360 console to make sure it worked so we'll 24397 20:49:29,120 --> 20:49:33,832 open the 24398 20:49:30,360 --> 20:49:37,320 console and inside here we have the same 24399 20:49:33,832 --> 20:49:40,080 messages as last time first we load the 24400 20:49:37,320 --> 20:49:42,600 products we wait for it to finish and 24401 20:49:40,080 --> 20:49:43,400 then go to the next step then we load 24402 20:49:42,600 --> 20:49:46,360 the 24403 20:49:43,400 --> 20:49:49,192 cart wait for it to finish and then go 24404 20:49:46,360 --> 20:49:53,000 to the next step and then we render or 24405 20:49:49,192 --> 20:49:56,920 display the page if we open the page 24406 20:49:53,000 --> 20:49:56,920 you'll see that everything displayed 24407 20:49:57,512 --> 20:50:02,960 properly so now let's go back and 24408 20:49:59,872 --> 20:50:05,400 compare promises and 24409 20:50:02,960 --> 20:50:08,480 callbacks so even though promises 24410 20:50:05,400 --> 20:50:11,320 require a bunch of setup code you can 24411 20:50:08,480 --> 20:50:14,400 see that it keeps our code relatively 24412 20:50:11,320 --> 20:50:18,512 flat the third step doesn't get more and 24413 20:50:14,400 --> 20:50:18,512 more nested like in 24414 20:50:19,000 --> 20:50:24,832 callbacks so if we have lots of steps in 24415 20:50:22,080 --> 20:50:27,872 our promise this is what the code will 24416 20:50:24,832 --> 20:50:31,000 look like you can see that even when we 24417 20:50:27,872 --> 20:50:32,640 add more steps the code does not become 24418 20:50:31,000 --> 20:50:35,920 more and more 24419 20:50:32,640 --> 20:50:37,040 nested so promises help keep our code 24420 20:50:35,920 --> 20:50:39,320 flat 24421 20:50:37,040 --> 20:50:42,320 and avoid too much 24422 20:50:39,320 --> 20:50:46,440 nesting so that's why it's recommended 24423 20:50:42,320 --> 20:50:50,600 to use promises instead of callbacks 24424 20:50:46,440 --> 20:50:50,600 promises will keep our code more 24425 20:50:52,920 --> 20:50:58,120 flat so let's actually add some new 24426 20:50:55,512 --> 20:51:01,760 lines here as well as 24427 20:50:58,120 --> 20:51:05,800 here to better separate these steps so 24428 20:51:01,760 --> 20:51:05,800 it's a little easier to read 24429 20:51:07,160 --> 20:51:10,512 lastly we're going to learn two more 24430 20:51:09,000 --> 20:51:13,640 features of 24431 20:51:10,512 --> 20:51:17,832 promises the first feature is we can 24432 20:51:13,640 --> 20:51:21,080 give resolve a value so inside here 24433 20:51:17,832 --> 20:51:23,192 let's give it a value like the string 24434 20:51:21,080 --> 20:51:26,480 value 24435 20:51:23,192 --> 20:51:29,512 one so whatever we give to resolve is 24436 20:51:26,480 --> 20:51:32,832 going to be saved in a parameter inside 24437 20:51:29,512 --> 20:51:36,552 doth so inside this function we can add 24438 20:51:32,832 --> 20:51:39,232 a parameter called value 24439 20:51:36,552 --> 20:51:42,080 and now whatever we give to resolve is 24440 20:51:39,232 --> 20:51:43,480 going to be saved inside here and now we 24441 20:51:42,080 --> 20:51:47,120 can use 24442 20:51:43,480 --> 20:51:50,400 it so this lets us share a value between 24443 20:51:47,120 --> 20:51:50,400 two steps of a 24444 20:51:50,512 --> 20:51:57,160 promise so in here let's check if this 24445 20:51:53,680 --> 20:51:59,040 worked using 24446 20:51:57,160 --> 20:52:01,280 console.log 24447 20:51:59,040 --> 20:52:04,760 value and 24448 20:52:01,280 --> 20:52:07,280 save and if we look inside our 24449 20:52:04,760 --> 20:52:08,192 console you'll see that it displayed 24450 20:52:07,280 --> 20:52:11,192 value 24451 20:52:08,192 --> 20:52:14,280 one so whatever we give to resolve will 24452 20:52:11,192 --> 20:52:14,280 be saved in this 24453 20:52:15,800 --> 20:52:22,640 parameter the second feature is we can 24454 20:52:18,760 --> 20:52:25,832 run multiple Promises at the same time 24455 20:52:22,640 --> 20:52:29,040 currently we wait for things to load one 24456 20:52:25,832 --> 20:52:31,800 at a time for example we wait for the 24457 20:52:29,040 --> 20:52:34,552 products to load and then we wait for 24458 20:52:31,800 --> 20:52:37,960 the cart to load and then we go to the 24459 20:52:34,552 --> 20:52:40,760 next step to make this more efficient we 24460 20:52:37,960 --> 20:52:42,800 can actually load the products and the 24461 20:52:40,760 --> 20:52:45,800 cart at the same 24462 20:52:42,800 --> 20:52:47,400 time to do that we can use a feature 24463 20:52:45,800 --> 20:52:50,360 called 24464 20:52:47,400 --> 20:52:53,320 promise.all promise.all lets us run 24465 20:52:50,360 --> 20:52:57,040 multiple Promises at the same time and 24466 20:52:53,320 --> 20:52:57,040 wait for all of them to 24467 20:52:57,640 --> 20:53:02,960 finish so let's scroll to the top and 24468 20:53:01,000 --> 20:53:07,080 we'll do an 24469 20:53:02,960 --> 20:53:10,480 example so here we'll type the code 24470 20:53:07,080 --> 20:53:10,480 capital P 24471 20:53:10,680 --> 20:53:16,480 promise.all and 24472 20:53:13,720 --> 20:53:18,872 brackets inside these brackets we're 24473 20:53:16,480 --> 20:53:22,080 going to give this an 24474 20:53:18,872 --> 20:53:25,120 array inside this array we can give it 24475 20:53:22,080 --> 20:53:27,960 multiple promises to wait 24476 20:53:25,120 --> 20:53:31,552 for so let's just copy paste the two 24477 20:53:27,960 --> 20:53:34,552 promises that we have here into this 24478 20:53:31,552 --> 20:53:36,960 array so we'll start from here at the 24479 20:53:34,552 --> 20:53:41,280 end of the first promise and we'll 24480 20:53:36,960 --> 20:53:44,000 select this code right click 24481 20:53:41,280 --> 20:53:47,440 copy and in this array we're going to 24482 20:53:44,000 --> 20:53:47,440 right click and 24483 20:53:48,512 --> 20:53:54,640 paste and remove this line so we have 24484 20:53:51,680 --> 20:53:56,920 our first promise and at the end we're 24485 20:53:54,640 --> 20:53:59,800 going to add a comma and we're going to 24486 20:53:56,920 --> 20:54:02,920 give it a second promise so we'll scroll 24487 20:53:59,800 --> 20:54:05,512 down to our second promise here and 24488 20:54:02,920 --> 20:54:06,920 we're going to select this code starting 24489 20:54:05,512 --> 20:54:11,600 from here 24490 20:54:06,920 --> 20:54:15,192 and select the promise right click 24491 20:54:11,600 --> 20:54:18,600 copy and at the top we're going to right 24492 20:54:15,192 --> 20:54:18,600 click and 24493 20:54:19,320 --> 20:54:25,120 paste so we basically created an array 24494 20:54:22,552 --> 20:54:26,960 of promises and then we're going to give 24495 20:54:25,120 --> 20:54:29,040 this array to 24496 20:54:26,960 --> 20:54:31,640 promise.all and it's going to wait for 24497 20:54:29,040 --> 20:54:33,680 all of the promises to finish before 24498 20:54:31,640 --> 20:54:36,760 going to the next 24499 20:54:33,680 --> 20:54:40,832 step so now let's add a next 24500 20:54:36,760 --> 20:54:45,160 Step at the end to add a Next Step we're 24501 20:54:40,832 --> 20:54:47,480 going to use dot then and brackets and 24502 20:54:45,160 --> 20:54:49,232 we'll add a new line to make it a little 24503 20:54:47,480 --> 20:54:53,232 easier to 24504 20:54:49,232 --> 20:54:55,512 read so as usual dot then adds a next 24505 20:54:53,232 --> 20:54:58,160 step to a 24506 20:54:55,512 --> 20:55:02,872 promise it will give it a function to 24507 20:54:58,160 --> 20:55:02,872 run so brackets arrow and curling 24508 20:55:03,160 --> 20:55:09,480 brackets after we load the products and 24509 20:55:06,400 --> 20:55:12,320 load the cart we're going to render the 24510 20:55:09,480 --> 20:55:15,552 page so let's copy paste that code 24511 20:55:12,320 --> 20:55:19,512 inside this next step so we'll scroll 24512 20:55:15,552 --> 20:55:22,600 down to the render code and we'll select 24513 20:55:19,512 --> 20:55:26,832 it right click 24514 20:55:22,600 --> 20:55:30,232 copy and at the top inside then we'll 24515 20:55:26,832 --> 20:55:30,232 right click and 24516 20:55:30,920 --> 20:55:38,192 paste so now let's comment out our old 24517 20:55:33,832 --> 20:55:41,160 promise and try out promise do 24518 20:55:38,192 --> 20:55:42,600 all above our old promise we're going to 24519 20:55:41,160 --> 20:55:45,872 type 24520 20:55:42,600 --> 20:55:47,832 slashstar and below it we're going to 24521 20:55:45,872 --> 20:55:51,160 type star 24522 20:55:47,832 --> 20:55:54,000 slash and let's 24523 20:55:51,160 --> 20:55:56,232 save we'll go to our 24524 20:55:54,000 --> 20:55:59,600 console and you can see that it did the 24525 20:55:56,232 --> 20:56:01,720 same thing it loaded the products and it 24526 20:55:59,600 --> 20:56:04,760 loaded the cart and it's going to wait 24527 20:56:01,720 --> 20:56:07,160 for both of them to call resolve before 24528 20:56:04,760 --> 20:56:10,280 going to the next step which is to 24529 20:56:07,160 --> 20:56:12,400 render or display the page if we open 24530 20:56:10,280 --> 20:56:14,832 the page you'll see that everything 24531 20:56:12,400 --> 20:56:17,192 displayed 24532 20:56:14,832 --> 20:56:20,280 properly so that's 24533 20:56:17,192 --> 20:56:23,000 promise.all we can give it an array of 24534 20:56:20,280 --> 20:56:25,760 promises and it will run both of them at 24535 20:56:23,000 --> 20:56:28,960 the same time instead of waiting for 24536 20:56:25,760 --> 20:56:32,400 each promise one by 24537 20:56:28,960 --> 20:56:35,800 one also the values that we give to 24538 20:56:32,400 --> 20:56:38,920 resolve are saved in a parameter so 24539 20:56:35,800 --> 20:56:40,400 inside do then we can add a parameter 24540 20:56:38,920 --> 20:56:43,192 called 24541 20:56:40,400 --> 20:56:45,440 values and then these values that we 24542 20:56:43,192 --> 20:56:49,920 give to resolve are going to be saved 24543 20:56:45,440 --> 20:56:51,552 inside here so if we 24544 20:56:49,920 --> 20:56:53,480 console.log 24545 20:56:51,552 --> 20:56:57,040 values and 24546 20:56:53,480 --> 20:56:59,800 save and check the 24547 20:56:57,040 --> 20:57:03,320 console and it gives us an array of 24548 20:56:59,800 --> 20:57:06,160 values the first value is from the first 24549 20:57:03,320 --> 20:57:09,000 promise and the second value is from the 24550 20:57:06,160 --> 20:57:11,512 the second promise so here we didn't 24551 20:57:09,000 --> 20:57:14,320 give anything to resolve so the second 24552 20:57:11,512 --> 20:57:14,320 value is 24553 20:57:14,400 --> 20:57:18,920 undefined so that's an overview of 24554 20:57:16,832 --> 20:57:21,800 promises and 24555 20:57:18,920 --> 20:57:24,960 promise.all promises are a better way to 24556 20:57:21,800 --> 20:57:26,040 wait for asynchronous code to finish 24557 20:57:24,960 --> 20:57:28,440 versus 24558 20:57:26,040 --> 20:57:32,120 callbacks because they help us avoid 24559 20:57:28,440 --> 20:57:35,360 nesting and keep our code relatively 24560 20:57:32,120 --> 20:57:38,440 flat promises also have more features 24561 20:57:35,360 --> 20:57:41,400 like promise all which lets us run 24562 20:57:38,440 --> 20:57:45,192 multiple Promises at the same 24563 20:57:41,400 --> 20:57:47,512 time now let's save our changes into git 24564 20:57:45,192 --> 20:57:50,552 we're going to open the git 24565 20:57:47,512 --> 20:57:53,512 section inside cart. 24566 20:57:50,552 --> 20:57:55,640 JS we added a function to practice 24567 20:57:53,512 --> 20:57:58,440 loading the cart from the back 24568 20:57:55,640 --> 20:58:02,080 end and then in checkout. 24569 20:57:58,440 --> 20:58:06,800 JS we learn a promises and converted our 24570 20:58:02,080 --> 20:58:09,512 code from a callback to a promise 24571 20:58:06,800 --> 20:58:12,600 let's type a message describing our 24572 20:58:09,512 --> 20:58:19,320 changes learn 24573 20:58:12,600 --> 20:58:21,720 promises and convert code to promises 24574 20:58:19,320 --> 20:58:21,720 and 24575 20:58:26,360 --> 20:58:30,872 commit now that we learned promises 24576 20:58:29,040 --> 20:58:32,080 we're going to learn another feature 24577 20:58:30,872 --> 20:58:36,000 called 24578 20:58:32,080 --> 20:58:38,512 fetch fetch is a better way to make htd 24579 20:58:36,000 --> 20:58:38,512 PP 24580 20:58:39,040 --> 20:58:45,512 requests let's go back to our 24581 20:58:41,600 --> 20:58:48,232 code and we're going to open products. 24582 20:58:45,512 --> 20:58:50,512 JS and we're going to scroll down to the 24583 20:58:48,232 --> 20:58:54,640 load products 24584 20:58:50,512 --> 20:58:58,232 function currently we're using XML HTTP 24585 20:58:54,640 --> 20:59:00,512 request to make requests to the back end 24586 20:58:58,232 --> 20:59:04,120 and this uses a call 24587 20:59:00,512 --> 20:59:08,232 back fetch also lets us make requests to 24588 20:59:04,120 --> 20:59:11,232 the backend but fetch uses a 24589 20:59:08,232 --> 20:59:14,360 promise so let's do an example and we'll 24590 20:59:11,232 --> 20:59:15,232 make the same request except using fetch 24591 20:59:14,360 --> 20:59:19,600 this 24592 20:59:15,232 --> 20:59:22,512 time we'll scroll up and above load 24593 20:59:19,600 --> 20:59:23,512 products let's create another function 24594 20:59:22,512 --> 20:59:29,040 using 24595 20:59:23,512 --> 20:59:33,760 function and we'll name it load products 24596 20:59:29,040 --> 20:59:36,480 Fetch and brackets and curly 24597 20:59:33,760 --> 20:59:41,400 brackets and to use fetch we're going to 24598 20:59:36,480 --> 20:59:45,640 use the built-in function Fetch and 24599 20:59:41,400 --> 20:59:48,480 brackets so this makes an HTTP request 24600 20:59:45,640 --> 20:59:52,480 just like we did down here so let's make 24601 20:59:48,480 --> 20:59:52,480 the same request as we did in this 24602 20:59:53,000 --> 20:59:59,192 function at the bottom we made a get 24603 20:59:56,400 --> 21:00:03,552 request to this 24604 20:59:59,192 --> 21:00:05,920 URL so by default fetch will make a get 24605 21:00:03,552 --> 21:00:09,080 request so we just need to give it the 24606 21:00:05,920 --> 21:00:13,800 the URL that we want to make the request 24607 21:00:09,080 --> 21:00:16,320 to so at the bottom let's select this 24608 21:00:13,800 --> 21:00:18,400 URL and we're just going to 24609 21:00:16,320 --> 21:00:22,320 rightclick 24610 21:00:18,400 --> 21:00:26,000 copy and at the top inside fetch we're 24611 21:00:22,320 --> 21:00:28,920 going to right click and 24612 21:00:26,000 --> 21:00:31,832 paste so this will make the exact same 24613 21:00:28,920 --> 21:00:34,400 request as we did 24614 21:00:31,832 --> 21:00:38,512 below so you can see that fetch is a lot 24615 21:00:34,400 --> 21:00:41,192 more simple than XML HTTP request we 24616 21:00:38,512 --> 21:00:44,000 don't have to create a new object or do 24617 21:00:41,192 --> 21:00:48,192 any setup we can just call Fetch and 24618 21:00:44,000 --> 21:00:48,192 tell it where we want the request to 24619 21:00:48,280 --> 21:00:54,232 go so this will send a request to the 24620 21:00:51,280 --> 21:00:57,320 back end and now how do we get the 24621 21:00:54,232 --> 21:01:01,920 response so instead of using a call back 24622 21:00:57,320 --> 21:01:05,080 to get the response fetch uses a 24623 21:01:01,920 --> 21:01:08,192 promise when we call fetch this is going 24624 21:01:05,080 --> 21:01:13,552 to create a promise and we can add a 24625 21:01:08,192 --> 21:01:14,800 next step to This Promise using dot then 24626 21:01:13,552 --> 21:01:17,320 and 24627 21:01:14,800 --> 21:01:22,000 brackets so we'll give dot then a 24628 21:01:17,320 --> 21:01:24,760 function brackets arrow and curly 24629 21:01:22,000 --> 21:01:26,960 brackets so the way this works is that 24630 21:01:24,760 --> 21:01:29,960 fetch is going to send a request to the 24631 21:01:26,960 --> 21:01:32,232 backend when we get a response it's 24632 21:01:29,960 --> 21:01:34,480 going to go to the next 24633 21:01:32,232 --> 21:01:37,360 step it's also going to save the 24634 21:01:34,480 --> 21:01:41,552 response inside inside a parameter in 24635 21:01:37,360 --> 21:01:43,640 here so we'll add a parameter called 24636 21:01:41,552 --> 21:01:45,800 response and this will contain the 24637 21:01:43,640 --> 21:01:49,160 response from the back 24638 21:01:45,800 --> 21:01:52,512 end so instead of using a call back to 24639 21:01:49,160 --> 21:01:54,640 wait for the response fetch uses a 24640 21:01:52,512 --> 21:01:58,320 promise to wait for the 24641 21:01:54,640 --> 21:02:00,920 response so here let's 24642 21:01:58,320 --> 21:02:03,192 console.log the response to see what it 24643 21:02:00,920 --> 21:02:03,192 looks 24644 21:02:03,400 --> 21:02:07,640 like and now let's run this code 24645 21:02:06,832 --> 21:02:11,120 at the 24646 21:02:07,640 --> 21:02:15,600 bottom we'll run this function using 24647 21:02:11,120 --> 21:02:18,280 load products Fetch and 24648 21:02:15,600 --> 21:02:22,512 brackets so now let's 24649 21:02:18,280 --> 21:02:22,512 save and take a look at the 24650 21:02:22,600 --> 21:02:29,680 console so here's the response that we 24651 21:02:25,232 --> 21:02:31,800 get from Fetch if we open it you can see 24652 21:02:29,680 --> 21:02:34,832 it gives us a bunch of information about 24653 21:02:31,800 --> 21:02:36,440 the response like the status or whether 24654 21:02:34,832 --> 21:02:39,360 it succeeded or 24655 21:02:36,440 --> 21:02:41,440 failed as well as the URL that we made 24656 21:02:39,360 --> 21:02:44,192 the request 24657 21:02:41,440 --> 21:02:46,512 to so how do we get the data for the 24658 21:02:44,192 --> 21:02:50,360 products from this 24659 21:02:46,512 --> 21:02:53,552 response to do that we'll go back to our 24660 21:02:50,360 --> 21:02:54,512 code and to get data that's attached to 24661 21:02:53,552 --> 21:02:58,232 this 24662 21:02:54,512 --> 21:03:02,120 response we're going to use 24663 21:02:58,232 --> 21:03:05,360 response. Json and 24664 21:03:02,120 --> 21:03:08,832 brackets so this gives us the Json or 24665 21:03:05,360 --> 21:03:12,360 the data attached to the response in 24666 21:03:08,832 --> 21:03:17,040 this case it's our products 24667 21:03:12,360 --> 21:03:20,800 data next response. Jon is actually 24668 21:03:17,040 --> 21:03:23,232 asynchronous it returns a 24669 21:03:20,800 --> 21:03:25,832 promise so we need to wait for this 24670 21:03:23,232 --> 21:03:28,080 promise to finish before we continue to 24671 21:03:25,832 --> 21:03:32,400 the next 24672 21:03:28,080 --> 21:03:36,160 step to do that inside. then we can 24673 21:03:32,400 --> 21:03:40,920 actually return another promise so here 24674 21:03:36,160 --> 21:03:44,320 we're going to return this 24675 21:03:40,920 --> 21:03:46,760 promise when we return a promise it will 24676 21:03:44,320 --> 21:03:48,680 wait for this promise to finish before 24677 21:03:46,760 --> 21:03:51,512 going to the next 24678 21:03:48,680 --> 21:03:56,832 step so now let's add another step to 24679 21:03:51,512 --> 21:03:59,480 This Promise using dot then brackets and 24680 21:03:56,832 --> 21:04:03,192 we'll give this a function again so 24681 21:03:59,480 --> 21:04:06,640 brackets arrow and curly 24682 21:04:03,192 --> 21:04:08,280 brackets when response. Json finishes 24683 21:04:06,640 --> 21:04:10,600 it's going to give us the data that's 24684 21:04:08,280 --> 21:04:14,160 attached to the response and it's going 24685 21:04:10,600 --> 21:04:16,600 to save it inside this 24686 21:04:14,160 --> 21:04:19,720 parameter so this response should 24687 21:04:16,600 --> 21:04:20,480 contain our products data so let's name 24688 21:04:19,720 --> 21:04:24,720 this 24689 21:04:20,480 --> 21:04:26,832 parameter products data and let's 24690 21:04:24,720 --> 21:04:29,920 console.log this to see what it looks 24691 21:04:26,832 --> 21:04:32,872 like so here we 24692 21:04:29,920 --> 21:04:34,400 console. log the 24693 21:04:32,872 --> 21:04:36,960 products 24694 21:04:34,400 --> 21:04:40,720 data and and 24695 21:04:36,960 --> 21:04:43,280 save and let's open the 24696 21:04:40,720 --> 21:04:46,872 console and you see here that the 24697 21:04:43,280 --> 21:04:49,720 products data is the array of products 24698 21:04:46,872 --> 21:04:52,400 that we're going to use in our Amazon 24699 21:04:49,720 --> 21:04:55,320 Project you'll also notice that instead 24700 21:04:52,400 --> 21:04:58,512 of giving us just a big Json string like 24701 21:04:55,320 --> 21:05:02,640 before it actually converted the Json 24702 21:04:58,512 --> 21:05:05,680 into an array so basically it did json. 24703 21:05:02,640 --> 21:05:08,160 pars on the response for us so this this 24704 21:05:05,680 --> 21:05:10,832 saved us a little bit of 24705 21:05:08,160 --> 21:05:13,040 code finally now that we have the 24706 21:05:10,832 --> 21:05:16,512 products array let's do the same thing 24707 21:05:13,040 --> 21:05:19,400 that we did down here so we'll scroll 24708 21:05:16,512 --> 21:05:21,832 down and select all of this 24709 21:05:19,400 --> 21:05:23,640 code and we'll right 24710 21:05:21,832 --> 21:05:26,000 click 24711 21:05:23,640 --> 21:05:28,552 copy and then up 24712 21:05:26,000 --> 21:05:32,832 here we'll remove 24713 21:05:28,552 --> 21:05:35,232 this and rightclick and 24714 21:05:32,832 --> 21:05:36,040 paste so we'll do the same steps as 24715 21:05:35,232 --> 21:05:38,720 before 24716 21:05:36,040 --> 21:05:42,160 except using a promise this 24717 21:05:38,720 --> 21:05:44,640 time first instead of doing json. pars 24718 21:05:42,160 --> 21:05:49,040 on the response we already have the 24719 21:05:44,640 --> 21:05:53,320 array of products so we can remove this 24720 21:05:49,040 --> 21:05:55,600 and replace it with products 24721 21:05:53,320 --> 21:05:58,920 data and then we're still going to 24722 21:05:55,600 --> 21:06:02,552 convert this from objects to classes 24723 21:05:58,920 --> 21:06:06,512 like before we'll keep this console.log 24724 21:06:02,552 --> 21:06:09,320 so we can track the progress 24725 21:06:06,512 --> 21:06:12,832 this last line of code let us do 24726 21:06:09,320 --> 21:06:15,800 something after we finish loading the 24727 21:06:12,832 --> 21:06:18,552 products however instead of using a call 24728 21:06:15,800 --> 21:06:19,640 back like before we're going to remove 24729 21:06:18,552 --> 21:06:23,920 this 24730 21:06:19,640 --> 21:06:27,232 line and to do something after this step 24731 21:06:23,920 --> 21:06:30,192 we can actually return this entire 24732 21:06:27,232 --> 21:06:33,360 promise so to make it easier to read 24733 21:06:30,192 --> 21:06:36,040 let's save this entire promise into a 24734 21:06:33,360 --> 21:06:41,512 variable using const 24735 21:06:36,040 --> 21:06:45,872 let's call it promise equals 24736 21:06:41,512 --> 21:06:48,832 fetch so let's add some new lines here 24737 21:06:45,872 --> 21:06:50,832 as well as here to make the code a 24738 21:06:48,832 --> 21:06:53,920 little easier to 24739 21:06:50,832 --> 21:06:56,680 read so we're basically calling fetch 24740 21:06:53,920 --> 21:07:01,480 which creates a promise and then we're 24741 21:06:56,680 --> 21:07:01,480 going to save this promise inside this 24742 21:07:01,760 --> 21:07:06,192 variable and then at the end of this 24743 21:07:04,040 --> 21:07:08,960 function we can just 24744 21:07:06,192 --> 21:07:12,120 return the promise so we're just going 24745 21:07:08,960 --> 21:07:14,920 to return this whole promise out of the 24746 21:07:12,120 --> 21:07:17,552 function and then we can add even more 24747 21:07:14,920 --> 21:07:21,080 steps after this 24748 21:07:17,552 --> 21:07:23,600 promise so down here this is going to 24749 21:07:21,080 --> 21:07:28,320 give us the promise and then we can 24750 21:07:23,600 --> 21:07:31,120 attach another step using dot then 24751 21:07:28,320 --> 21:07:31,920 brackets and inside here we can give it 24752 21:07:31,120 --> 21:07:35,000 a 24753 21:07:31,920 --> 21:07:37,440 function brackets arrow and curly 24754 21:07:35,000 --> 21:07:37,440 brackets 24755 21:07:39,040 --> 21:07:44,800 so this attaches another step to the end 24756 21:07:42,360 --> 21:07:46,360 of This Promise so basically attaches 24757 21:07:44,800 --> 21:07:49,512 another step 24758 21:07:46,360 --> 21:07:52,552 here so after we load all the products 24759 21:07:49,512 --> 21:07:54,960 let's just 24760 21:07:52,552 --> 21:07:57,480 console.log next 24761 21:07:54,960 --> 21:08:00,552 step and 24762 21:07:57,480 --> 21:08:03,680 save so we'll check the 24763 21:08:00,552 --> 21:08:08,960 console and we'll see that we loaded the 24764 21:08:03,680 --> 21:08:08,960 products and then after we did The Next 24765 21:08:10,760 --> 21:08:16,512 Step so we can actually return a promise 24766 21:08:14,280 --> 21:08:19,600 out of a function and then keep 24767 21:08:16,512 --> 21:08:23,192 attaching more steps to that 24768 21:08:19,600 --> 21:08:26,680 promise so that's how we use fetch it's 24769 21:08:23,192 --> 21:08:30,232 a better way to make HTTP requests 24770 21:08:26,680 --> 21:08:30,232 because it uses promises 24771 21:08:31,872 --> 21:08:38,160 directly so now let's use fetch in our 24772 21:08:34,640 --> 21:08:41,080 project instead of using XML HTTP 24773 21:08:38,160 --> 21:08:43,440 request so first let's comment out this 24774 21:08:41,080 --> 21:08:44,960 code because we were just practicing 24775 21:08:43,440 --> 21:08:48,800 we'll type 24776 21:08:44,960 --> 21:08:52,040 slashstar and at the bottom star 24777 21:08:48,800 --> 21:08:54,920 slash and let's scroll up to load 24778 21:08:52,040 --> 21:08:55,872 products Fetch and we're going to export 24779 21:08:54,920 --> 21:08:59,400 this 24780 21:08:55,872 --> 21:09:02,232 function so we can use it outside this 24781 21:08:59,400 --> 21:09:05,512 file let's 24782 21:09:02,232 --> 21:09:08,552 save we'll go to the left 24783 21:09:05,512 --> 21:09:11,920 and open checkout. 24784 21:09:08,552 --> 21:09:14,640 JS and here instead of using load 24785 21:09:11,920 --> 21:09:18,280 products which uses callbacks let's 24786 21:09:14,640 --> 21:09:20,512 switch to load products fetch which uses 24787 21:09:18,280 --> 21:09:22,920 promises we'll scroll 24788 21:09:20,512 --> 21:09:25,720 up and we're going to import load 24789 21:09:22,920 --> 21:09:28,760 products fetch so here we're already 24790 21:09:25,720 --> 21:09:33,832 importing from products. JS we'll type a 24791 21:09:28,760 --> 21:09:37,832 comma load products 24792 21:09:33,832 --> 21:09:40,232 Fetch and now will replace load products 24793 21:09:37,832 --> 21:09:43,512 so remember that we need to give this a 24794 21:09:40,232 --> 21:09:47,080 promise and also remember that fetch 24795 21:09:43,512 --> 21:09:51,552 returns a promise so all we need to do 24796 21:09:47,080 --> 21:09:56,720 is to remove this code and just call 24797 21:09:51,552 --> 21:10:00,160 load products Fetch and 24798 21:09:56,720 --> 21:10:02,000 brackets this will return a promise and 24799 21:10:00,160 --> 21:10:05,680 we can use it with 24800 21:10:02,000 --> 21:10:07,720 promise.all so now let's save 24801 21:10:05,680 --> 21:10:09,720 we'll check our 24802 21:10:07,720 --> 21:10:11,040 website and you'll see that everything 24803 21:10:09,720 --> 21:10:14,000 is still 24804 21:10:11,040 --> 21:10:16,480 working so fetch helps us make our code 24805 21:10:14,000 --> 21:10:18,720 a lot cleaner because it can return a 24806 21:10:16,480 --> 21:10:22,080 promise directly and we don't have to 24807 21:10:18,720 --> 21:10:26,640 type out all of this extra 24808 21:10:22,080 --> 21:10:30,080 code finally let's use fetch inside our 24809 21:10:26,640 --> 21:10:32,440 tests on the left we'll open order 24810 21:10:30,080 --> 21:10:35,720 summary 24811 21:10:32,440 --> 21:10:38,920 test.js and remember that inside here we 24812 21:10:35,720 --> 21:10:42,480 used load products so let's switch this 24813 21:10:38,920 --> 21:10:45,080 to using load products fetch instead 24814 21:10:42,480 --> 21:10:48,192 first we'll import it at the top we're 24815 21:10:45,080 --> 21:10:54,232 importing from products. JS so we'll 24816 21:10:48,192 --> 21:10:57,680 type comma and import load products 24817 21:10:54,232 --> 21:11:02,760 Fetch and instead of load products we're 24818 21:10:57,680 --> 21:11:06,360 going to call load products 24819 21:11:02,760 --> 21:11:09,600 Fetch and brackets 24820 21:11:06,360 --> 21:11:11,920 so remember that this returns a promise 24821 21:11:09,600 --> 21:11:16,872 and we can attach more steps to This 24822 21:11:11,920 --> 21:11:19,640 Promise using the method dot then and 24823 21:11:16,872 --> 21:11:24,920 brackets and we'll give then a function 24824 21:11:19,640 --> 21:11:24,920 to run so brackets arrow and curly 24825 21:11:25,280 --> 21:11:31,400 brackets once the products are finished 24826 21:11:27,832 --> 21:11:34,360 loading it's going to run then and 24827 21:11:31,400 --> 21:11:38,120 inside here we're going to call done to 24828 21:11:34,360 --> 21:11:41,440 go to the next step of the test so here 24829 21:11:38,120 --> 21:11:44,872 we'll just call done and 24830 21:11:41,440 --> 21:11:47,832 brackets and we'll remove the old 24831 21:11:44,872 --> 21:11:48,760 code so now let's save to make sure this 24832 21:11:47,832 --> 21:11:51,920 is 24833 21:11:48,760 --> 21:11:55,040 working and we'll open the tests go to 24834 21:11:51,920 --> 21:11:57,960 the browser and open the 24835 21:11:55,040 --> 21:11:59,960 test and the order summary test should 24836 21:11:57,960 --> 21:12:02,512 be 24837 21:11:59,960 --> 21:12:06,120 passing so now let's save our changes 24838 21:12:02,512 --> 21:12:09,760 into git we'll open the git set 24839 21:12:06,120 --> 21:12:13,360 section inside products. JS we learned 24840 21:12:09,760 --> 21:12:17,480 how to use fetch which returns a promise 24841 21:12:13,360 --> 21:12:21,160 directly inside checkout. JS we used 24842 21:12:17,480 --> 21:12:22,872 load products fetch inside our project 24843 21:12:21,160 --> 21:12:26,000 and in order summary 24844 21:12:22,872 --> 21:12:29,360 test.js we use load products fetch 24845 21:12:26,000 --> 21:12:30,872 inside our tests let's create a message 24846 21:12:29,360 --> 21:12:35,680 describing these 24847 21:12:30,872 --> 21:12:43,192 changes convert load products 24848 21:12:35,680 --> 21:12:45,552 from xhr or XML HTTP request to fetch 24849 21:12:43,192 --> 21:12:45,552 and 24850 21:12:49,440 --> 21:12:55,280 commit next we're going to learn async 24851 21:12:52,360 --> 21:12:58,600 await which is an even better way to 24852 21:12:55,280 --> 21:12:58,600 handle asynchronous 24853 21:12:58,680 --> 21:13:06,440 code let's go back to our 24854 21:13:01,000 --> 21:13:06,440 code and we'll open checkout. Js 24855 21:13:06,640 --> 21:13:11,920 currently we're using promises to handle 24856 21:13:09,232 --> 21:13:14,400 asynchronous code but the problem with 24857 21:13:11,920 --> 21:13:17,512 promises is that it creates a lot of 24858 21:13:14,400 --> 21:13:20,920 extra code for example we need to create 24859 21:13:17,512 --> 21:13:23,360 a new promise we need to use resolve and 24860 21:13:20,920 --> 21:13:26,872 we need to use do 24861 21:13:23,360 --> 21:13:30,680 that async await is a shortcut for 24862 21:13:26,872 --> 21:13:33,512 promises and it removes all this extra 24863 21:13:30,680 --> 21:13:38,040 code so let's do our first example of 24864 21:13:33,512 --> 21:13:41,360 asyn a weit we're going to scroll 24865 21:13:38,040 --> 21:13:44,080 up and at the top we're going to create 24866 21:13:41,360 --> 21:13:48,640 a function using 24867 21:13:44,080 --> 21:13:51,832 function and let's name it load page 24868 21:13:48,640 --> 21:13:51,832 brackets and curly 24869 21:13:51,960 --> 21:14:00,832 brackets and inside we're going to 24870 21:13:55,872 --> 21:14:03,512 console.log the string load 24871 21:14:00,832 --> 21:14:06,280 page and now we'll learn the first part 24872 21:14:03,512 --> 21:14:09,280 of async await which is 24873 21:14:06,280 --> 21:14:13,400 async in front of the word function 24874 21:14:09,280 --> 21:14:18,120 we're going to add the word async and 24875 21:14:13,400 --> 21:14:21,600 space so async makes a function return a 24876 21:14:18,120 --> 21:14:25,000 promise so this code is a shortcut for 24877 21:14:21,600 --> 21:14:27,280 this code it basically wraps the code in 24878 21:14:25,000 --> 21:14:27,280 a 24879 21:14:28,760 --> 21:14:33,440 promise so now let's run this function 24880 21:14:31,480 --> 21:14:35,120 to show that it actually returns a 24881 21:14:33,440 --> 21:14:39,872 promise 24882 21:14:35,120 --> 21:14:42,440 So Below this we'll call load page and 24883 21:14:39,872 --> 21:14:45,760 brackets and because this returns a 24884 21:14:42,440 --> 21:14:50,280 promise we can add a next step to This 24885 21:14:45,760 --> 21:14:54,160 Promise using dot then 24886 21:14:50,280 --> 21:14:56,552 brackets and we'll give then a function 24887 21:14:54,160 --> 21:15:01,160 brackets arrow and curly 24888 21:14:56,552 --> 21:15:06,920 brackets and inside here let's just 24889 21:15:01,160 --> 21:15:08,360 console.log the string Next Step 24890 21:15:06,920 --> 21:15:11,320 now let's 24891 21:15:08,360 --> 21:15:12,960 save and we'll check the console so 24892 21:15:11,320 --> 21:15:15,360 we'll open the 24893 21:15:12,960 --> 21:15:19,000 console and you'll see that we loaded 24894 21:15:15,360 --> 21:15:23,040 the page and then in the next step we 24895 21:15:19,000 --> 21:15:25,680 displayed Next Step so when we put a 24896 21:15:23,040 --> 21:15:28,080 sync in front of function it makes this 24897 21:15:25,680 --> 21:15:31,512 function return a 24898 21:15:28,080 --> 21:15:34,512 promise this code is a shortcut for this 24899 21:15:31,512 --> 21:15:34,512 code 24900 21:15:36,160 --> 21:15:41,800 next if we return something from this 24901 21:15:38,720 --> 21:15:46,552 function for example we're going to 24902 21:15:41,800 --> 21:15:50,192 return a value like the string value 24903 21:15:46,552 --> 21:15:52,552 two this gets converted into resolve 24904 21:15:50,192 --> 21:15:52,552 value 24905 21:15:54,512 --> 21:16:01,720 two so this value is going to be saved 24906 21:15:57,800 --> 21:16:05,360 in a parameter in The Next Step so here 24907 21:16:01,720 --> 21:16:09,120 we'll add a parameter value and value 24908 21:16:05,360 --> 21:16:10,800 two is going to be saved inside here if 24909 21:16:09,120 --> 21:16:14,800 we 24910 21:16:10,800 --> 21:16:17,360 console.log the value and 24911 21:16:14,800 --> 21:16:21,080 save and then check the 24912 21:16:17,360 --> 21:16:23,360 console we'll see that value is equal to 24913 21:16:21,080 --> 21:16:23,360 this 24914 21:16:23,640 --> 21:16:30,920 string and that's it for async it just 24915 21:16:27,360 --> 21:16:33,360 makes a function return a 24916 21:16:30,920 --> 21:16:36,000 promise now you might be wondering 24917 21:16:33,360 --> 21:16:38,960 what's the point of this feature 24918 21:16:36,000 --> 21:16:43,872 so the reason we use async is because it 24919 21:16:38,960 --> 21:16:43,872 lets us use the second feature called 24920 21:16:44,960 --> 21:16:52,640 await await lets us wait for a promise 24921 21:16:48,280 --> 21:16:52,640 to finish before going to the next 24922 21:16:53,512 --> 21:17:02,280 line so let's do an example inside here 24923 21:16:58,000 --> 21:17:02,280 let's run some code that creates a 24924 21:17:02,320 --> 21:17:12,440 promise let's run load products fetch so 24925 21:17:06,680 --> 21:17:15,640 we'll type load products Fetch and 24926 21:17:12,440 --> 21:17:19,600 brackets so this loads the products from 24927 21:17:15,640 --> 21:17:22,232 the back end and usually this returns a 24928 21:17:19,600 --> 21:17:26,280 promise so one way to wait for this to 24929 21:17:22,232 --> 21:17:27,920 finish is to use do then and then give 24930 21:17:26,280 --> 21:17:31,160 it a 24931 21:17:27,920 --> 21:17:35,160 function so this runs this function 24932 21:17:31,160 --> 21:17:38,400 after load products has finished 24933 21:17:35,160 --> 21:17:41,080 now await gives us another way to wait 24934 21:17:38,400 --> 21:17:43,080 for this promise to finish instead of 24935 21:17:41,080 --> 21:17:48,480 using 24936 21:17:43,080 --> 21:17:48,480 then at the front we can just type 24937 21:17:50,320 --> 21:17:57,552 await so await lets us write 24938 21:17:53,400 --> 21:18:00,440 asynchronous code like normal code so 24939 21:17:57,552 --> 21:18:03,440 now it will wait for this line to finish 24940 21:18:00,440 --> 21:18:06,192 and get the response from the back end 24941 21:18:03,440 --> 21:18:09,280 before going to the next next line so we 24942 21:18:06,192 --> 21:18:13,360 don't have to do any more do then we 24943 21:18:09,280 --> 21:18:18,800 just write it like normal code line by 24944 21:18:13,360 --> 21:18:18,800 line this code is a shortcut for this 24945 21:18:21,552 --> 21:18:28,800 code and this makes our code a lot 24946 21:18:24,120 --> 21:18:28,800 easier to read than what we have down 24947 21:18:28,960 --> 21:18:35,232 here so now let's see what this looks 24948 21:18:31,360 --> 21:18:37,440 like in the console before we save let's 24949 21:18:35,232 --> 21:18:39,320 comment out this promise at the bottom 24950 21:18:37,440 --> 21:18:42,280 so we don't get 24951 21:18:39,320 --> 21:18:46,232 confused so we'll type 24952 21:18:42,280 --> 21:18:49,832 slashstar and at the bottom star 24953 21:18:46,232 --> 21:18:52,440 slash and now we're going to save this 24954 21:18:49,832 --> 21:18:53,640 code and check the console to see what 24955 21:18:52,440 --> 21:18:57,080 it 24956 21:18:53,640 --> 21:18:59,480 did so you see that we loaded the page 24957 21:18:57,080 --> 21:19:02,280 and then notice that it finish loading 24958 21:18:59,480 --> 21:19:05,160 the products before going to the next 24959 21:19:02,280 --> 21:19:08,080 step so this actually Waits for this 24960 21:19:05,160 --> 21:19:10,760 line to finish before going to the next 24961 21:19:08,080 --> 21:19:13,192 line in the code and this makes our code 24962 21:19:10,760 --> 21:19:15,760 a lot 24963 21:19:13,192 --> 21:19:19,080 cleaner now one very important thing to 24964 21:19:15,760 --> 21:19:22,080 know is that we can only use a weight 24965 21:19:19,080 --> 21:19:24,440 when we're inside an async 24966 21:19:22,080 --> 21:19:27,800 function that's actually the main reason 24967 21:19:24,440 --> 21:19:30,832 we use async so that we can use a weight 24968 21:19:27,800 --> 21:19:30,832 to make our code 24969 21:19:32,360 --> 21:19:38,400 cleaner now that we learned asyn wait 24970 21:19:35,320 --> 21:19:41,800 let's use it in our 24971 21:19:38,400 --> 21:19:44,800 project so previously we loaded the 24972 21:19:41,800 --> 21:19:47,800 products and then we loaded the cart and 24973 21:19:44,800 --> 21:19:50,600 then we rendered the page so let's 24974 21:19:47,800 --> 21:19:54,120 recreate this using async 24975 21:19:50,600 --> 21:19:56,000 await first we want to load the products 24976 21:19:54,120 --> 21:19:58,960 which we did here and we're going to 24977 21:19:56,000 --> 21:20:01,160 wait for this to finish and then we're 24978 21:19:58,960 --> 21:20:04,760 going to load the 24979 21:20:01,160 --> 21:20:08,480 cart so to load the cart let's use this 24980 21:20:04,760 --> 21:20:10,800 promise down here asyn await can only be 24981 21:20:08,480 --> 21:20:13,872 used with promises it doesn't do 24982 21:20:10,800 --> 21:20:13,872 anything with a call 24983 21:20:14,400 --> 21:20:20,320 back so we'll select this promise and 24984 21:20:18,512 --> 21:20:23,512 rightclick 24985 21:20:20,320 --> 21:20:26,552 copy and then up here we're going to 24986 21:20:23,512 --> 21:20:29,080 load the cart with a promise by right 24987 21:20:26,552 --> 21:20:32,000 clicking and 24988 21:20:29,080 --> 21:20:34,640 paste so now we want to wait for this 24989 21:20:32,000 --> 21:20:38,320 promise to finish before going to the 24990 21:20:34,640 --> 21:20:41,552 next line to do that at the front we can 24991 21:20:38,320 --> 21:20:41,552 simply type 24992 21:20:43,440 --> 21:20:49,480 await finally after we load the products 24993 21:20:47,440 --> 21:20:53,440 and we load the cart we're going to 24994 21:20:49,480 --> 21:20:56,232 render the page so we'll scroll down and 24995 21:20:53,440 --> 21:20:56,232 we'll select this 24996 21:20:56,680 --> 21:21:05,160 code and we're going to right click 24997 21:21:00,640 --> 21:21:05,160 copy and at the top 24998 21:21:05,640 --> 21:21:10,080 we're going to rightclick and 24999 21:21:10,512 --> 21:21:16,832 paste and that's it you can see that our 25000 21:21:13,552 --> 21:21:19,040 asynchronous code is a lot cleaner now 25001 21:21:16,832 --> 21:21:21,400 we load the products and then we just 25002 21:21:19,040 --> 21:21:24,760 wait for it to finish then we load the 25003 21:21:21,400 --> 21:21:28,120 cart and we also wait for it to finish 25004 21:21:24,760 --> 21:21:30,872 and then we run the rest of our code so 25005 21:21:28,120 --> 21:21:34,480 we can write asynchronous code like 25006 21:21:30,872 --> 21:21:34,480 normal code 25007 21:21:35,512 --> 21:21:38,920 so now let's remove the console.log at 25008 21:21:38,000 --> 21:21:42,192 the 25009 21:21:38,920 --> 21:21:43,872 top and also remove this return value 25010 21:21:42,192 --> 21:21:49,000 because we don't need 25011 21:21:43,872 --> 21:21:49,000 it and we'll remove do then at the 25012 21:21:50,192 --> 21:21:56,160 end and let's 25013 21:21:53,192 --> 21:21:59,872 save so now if we open our 25014 21:21:56,160 --> 21:21:59,872 website and open the 25015 21:22:01,280 --> 21:22:06,120 project you can see that everything 25016 21:22:03,320 --> 21:22:07,720 loaded properly 25017 21:22:06,120 --> 21:22:10,040 inside the 25018 21:22:07,720 --> 21:22:13,480 console you can see that we loaded the 25019 21:22:10,040 --> 21:22:17,000 products first wait for it to finish 25020 21:22:13,480 --> 21:22:20,320 then load the cart wait for it to finish 25021 21:22:17,000 --> 21:22:23,832 and then we displayed the page so that's 25022 21:22:20,320 --> 21:22:28,040 how we use a syn8 to make our code a lot 25023 21:22:23,832 --> 21:22:28,040 cleaner than what we had 25024 21:22:29,920 --> 21:22:35,000 before so now we'll learn some more 25025 21:22:32,080 --> 21:22:37,480 details about Ayn a weit 25026 21:22:35,000 --> 21:22:41,160 as I mentioned we can only use a weight 25027 21:22:37,480 --> 21:22:44,512 inside an async 25028 21:22:41,160 --> 21:22:46,280 function also the closest function has 25029 21:22:44,512 --> 21:22:49,400 to be 25030 21:22:46,280 --> 21:22:52,440 async for example here we have an async 25031 21:22:49,400 --> 21:22:55,320 function at the top and inside we have a 25032 21:22:52,440 --> 21:22:57,920 normal function so we can't use a weight 25033 21:22:55,320 --> 21:23:01,080 inside the normal function we have to 25034 21:22:57,920 --> 21:23:01,080 make this one 25035 21:23:03,080 --> 21:23:09,192 async another another detail is if a 25036 21:23:06,320 --> 21:23:12,872 promise resolves with a value for 25037 21:23:09,192 --> 21:23:16,440 example here we give resolve a value 25038 21:23:12,872 --> 21:23:19,552 like the string value 25039 21:23:16,440 --> 21:23:24,000 three normally this value will be saved 25040 21:23:19,552 --> 21:23:26,640 in The Next Step so if we did do then 25041 21:23:24,000 --> 21:23:26,640 and gave it a 25042 21:23:27,400 --> 21:23:32,440 function this value would be saved in 25043 21:23:30,232 --> 21:23:36,000 this 25044 21:23:32,440 --> 21:23:38,640 parameter however if we use await this 25045 21:23:36,000 --> 21:23:42,040 value just gets returned and we can save 25046 21:23:38,640 --> 21:23:45,080 it in a variable so instead of using 25047 21:23:42,040 --> 21:23:49,720 then we can Simply Save whatever is 25048 21:23:45,080 --> 21:23:52,680 resolved inside a variable using const 25049 21:23:49,720 --> 21:23:52,680 value 25050 21:23:52,800 --> 21:23:58,080 equals and this makes our code a lot 25051 21:23:55,160 --> 21:24:01,280 easier to read instead of having to use 25052 21:23:58,080 --> 21:24:05,960 then we can simply save this inside a 25053 21:24:01,280 --> 21:24:05,960 variable like normal code 25054 21:24:06,160 --> 21:24:12,040 and finally we can use await with 25055 21:24:09,360 --> 21:24:14,120 promise.all as well but I'll leave that 25056 21:24:12,040 --> 21:24:17,920 as an 25057 21:24:14,120 --> 21:24:20,920 exercise so that's async await it's a 25058 21:24:17,920 --> 21:24:25,800 shortcut for promises and it lets us 25059 21:24:20,920 --> 21:24:25,800 write asynchronous code like normal 25060 21:24:26,000 --> 21:24:32,000 code so when working with asynchronous 25061 21:24:28,920 --> 21:24:35,400 code a best practice is to use asyn 25062 21:24:32,000 --> 21:24:38,360 await over promises and callbacks 25063 21:24:35,400 --> 21:24:38,360 because they are much 25064 21:24:38,440 --> 21:24:44,680 cleaner so now let's save our changes 25065 21:24:40,872 --> 21:24:48,160 into git we'll open the git section in 25066 21:24:44,680 --> 21:24:51,872 checkout. JS we created the load page 25067 21:24:48,160 --> 21:24:54,600 function where we learned async and 25068 21:24:51,872 --> 21:24:56,760 await let's create a message describing 25069 21:24:54,600 --> 21:24:56,760 these 25070 21:24:56,960 --> 21:25:07,040 changes convert from promises to async 25071 21:25:03,192 --> 21:25:07,040 await and 25072 21:25:11,480 --> 21:25:17,320 commit now we're going to learn about 25073 21:25:13,920 --> 21:25:19,960 error handling when we're sending HTTP 25074 21:25:17,320 --> 21:25:23,440 requests across the internet we could 25075 21:25:19,960 --> 21:25:26,232 get unexpected errors for example the 25076 21:25:23,440 --> 21:25:27,960 internet connection might stop working 25077 21:25:26,232 --> 21:25:31,000 so we're going to learn how to handle 25078 21:25:27,960 --> 21:25:34,400 these kinds of unexpected 25079 21:25:31,000 --> 21:25:39,280 errors let's go back to our code and 25080 21:25:34,400 --> 21:25:41,600 we're going to open data/ products. 25081 21:25:39,280 --> 21:25:46,360 JS and we're going to 25082 21:25:41,600 --> 21:25:46,360 scroll to the load products 25083 21:25:46,640 --> 21:25:53,280 function so this uses a call back to 25084 21:25:49,800 --> 21:25:55,760 send a request and get a 25085 21:25:53,280 --> 21:25:58,920 response now what if there's an error 25086 21:25:55,760 --> 21:26:03,160 along the way for callbacks we usually 25087 21:25:58,920 --> 21:26:06,920 set up a separate callback just for 25088 21:26:03,160 --> 21:26:09,360 errors for example below this we're 25089 21:26:06,920 --> 21:26:16,760 going to add another event listener 25090 21:26:09,360 --> 21:26:20,280 using xhr do add event listener 25091 21:26:16,760 --> 21:26:22,552 brackets and the first parameter is the 25092 21:26:20,280 --> 21:26:25,120 event this time we're going to listen 25093 21:26:22,552 --> 21:26:29,440 for the error 25094 21:26:25,120 --> 21:26:29,440 event and then we're going to give it a 25095 21:26:31,640 --> 21:26:38,360 function so now if we get an unexpected 25096 21:26:35,040 --> 21:26:40,920 error it's going to run this function so 25097 21:26:38,360 --> 21:26:44,280 if we get an error let's just console 25098 21:26:40,920 --> 21:26:47,832 log a message 25099 21:26:44,280 --> 21:26:50,360 console.log a string 25100 21:26:47,832 --> 21:26:53,192 unexpected 25101 21:26:50,360 --> 21:26:56,000 error and usually when we get an error 25102 21:26:53,192 --> 21:27:01,920 we give some instructions on what to 25103 21:26:56,000 --> 21:27:04,232 do here let's just type please try again 25104 21:27:01,920 --> 21:27:06,400 later 25105 21:27:04,232 --> 21:27:10,000 and 25106 21:27:06,400 --> 21:27:12,552 save so this is called error handling if 25107 21:27:10,000 --> 21:27:15,192 we get an error during the request we 25108 21:27:12,552 --> 21:27:17,160 can handle it with this function so we 25109 21:27:15,192 --> 21:27:20,320 just display a 25110 21:27:17,160 --> 21:27:22,680 message so now let's try out this code 25111 21:27:20,320 --> 21:27:25,232 to simulate an error we're going to 25112 21:27:22,680 --> 21:27:28,040 change the url of the 25113 21:27:25,232 --> 21:27:32,280 request to 25114 21:27:28,040 --> 21:27:35,000 error. superimple backend. deev so this 25115 21:27:32,280 --> 21:27:37,000 will create an error because this URL 25116 21:27:35,000 --> 21:27:40,800 doesn't 25117 21:27:37,000 --> 21:27:43,160 exist so now down here let's call this 25118 21:27:40,800 --> 21:27:47,760 function so that we run this code and 25119 21:27:43,160 --> 21:27:50,000 get the error so we'll type load 25120 21:27:47,760 --> 21:27:53,120 products and 25121 21:27:50,000 --> 21:27:56,800 brackets and 25122 21:27:53,120 --> 21:27:56,800 save let's check the 25123 21:27:57,280 --> 21:28:02,720 console and you'll see that we displayed 25124 21:28:00,040 --> 21:28:06,640 this error 25125 21:28:02,720 --> 21:28:09,320 message so we got an error and it ran 25126 21:28:06,640 --> 21:28:12,640 this error function and it did not run 25127 21:28:09,320 --> 21:28:12,640 the load function 25128 21:28:13,280 --> 21:28:21,872 above also usually this callback gets a 25129 21:28:17,720 --> 21:28:24,800 parameter called error and this contains 25130 21:28:21,872 --> 21:28:27,400 some information about the 25131 21:28:24,800 --> 21:28:30,440 error so that's how we handle errors 25132 21:28:27,400 --> 21:28:35,232 when using callbacks we usually set up a 25133 21:28:30,440 --> 21:28:37,680 separate callback just for errors 25134 21:28:35,232 --> 21:28:38,872 so now let's remove this line since it 25135 21:28:37,680 --> 21:28:42,680 was just for 25136 21:28:38,872 --> 21:28:45,512 practice and we'll remove error from the 25137 21:28:42,680 --> 21:28:49,720 URL and change it back to super simple 25138 21:28:45,512 --> 21:28:49,720 backend dodev and 25139 21:28:51,000 --> 21:28:55,040 save next we'll learn how to handle 25140 21:28:53,552 --> 21:28:59,040 errors in 25141 21:28:55,040 --> 21:29:00,760 promises so we'll scroll up to the load 25142 21:28:59,040 --> 21:29:03,480 products fetch 25143 21:29:00,760 --> 21:29:04,512 function and this makes the same request 25144 21:29:03,480 --> 21:29:06,680 as below 25145 21:29:04,512 --> 21:29:10,120 but it uses 25146 21:29:06,680 --> 21:29:14,160 promises to handle an error in promises 25147 21:29:10,120 --> 21:29:19,480 promises give us two methods do then and 25148 21:29:14,160 --> 21:29:22,720 another method called dot catch brackets 25149 21:29:19,480 --> 21:29:27,000 and we're going to give catch a function 25150 21:29:22,720 --> 21:29:27,000 brackets arrow and curly 25151 21:29:27,040 --> 21:29:32,832 brackets so now if this request has an 25152 21:29:30,040 --> 21:29:35,800 error it's going to go to this catch 25153 21:29:32,832 --> 21:29:38,760 method and it's going to run this error 25154 21:29:35,800 --> 21:29:41,600 function so inside here let's also 25155 21:29:38,760 --> 21:29:43,160 display a message so let's just copy the 25156 21:29:41,600 --> 21:29:46,760 message that we had 25157 21:29:43,160 --> 21:29:47,920 below we're going to select this message 25158 21:29:46,760 --> 21:29:49,872 and then 25159 21:29:47,920 --> 21:29:53,360 rightclick 25160 21:29:49,872 --> 21:29:57,320 copy and then inside catch we're going 25161 21:29:53,360 --> 21:29:57,320 to right click and 25162 21:29:57,480 --> 21:30:03,440 paste and again to simulate an error 25163 21:30:01,000 --> 21:30:04,680 we're going to scroll up and change the 25164 21:30:03,440 --> 21:30:08,480 url 25165 21:30:04,680 --> 21:30:11,280 so we're going to add error at the front 25166 21:30:08,480 --> 21:30:14,360 do superimple backend 25167 21:30:11,280 --> 21:30:19,080 dodev so now let's call this function 25168 21:30:14,360 --> 21:30:25,160 and test out this error code at the 25169 21:30:19,080 --> 21:30:26,120 bottom we'll type load products Fetch 25170 21:30:25,160 --> 21:30:29,120 and 25171 21:30:26,120 --> 21:30:32,192 brackets and 25172 21:30:29,120 --> 21:30:34,800 save so now let's open the 25173 21:30:32,192 --> 21:30:37,960 console and you'll see in here that we 25174 21:30:34,800 --> 21:30:40,552 displayed the error message so we're 25175 21:30:37,960 --> 21:30:45,440 successfully creating an error it's 25176 21:30:40,552 --> 21:30:45,440 going inside catch and it displays this 25177 21:30:45,872 --> 21:30:52,920 message so just like callbacks catch 25178 21:30:49,232 --> 21:30:55,320 gets a parameter called error and this 25179 21:30:52,920 --> 21:30:57,680 contains information about the error if 25180 21:30:55,320 --> 21:31:00,832 we need 25181 21:30:57,680 --> 21:31:03,480 it so that's how we handle errors with 25182 21:31:00,832 --> 21:31:06,920 promises we have another method called 25183 21:31:03,480 --> 21:31:07,872 Dot catch that will run this function if 25184 21:31:06,920 --> 21:31:12,320 there's an 25185 21:31:07,872 --> 21:31:15,120 error so now let's remove this practice 25186 21:31:12,320 --> 21:31:21,600 code and switch back the 25187 21:31:15,120 --> 21:31:21,600 URL so remove error and Dot and 25188 21:31:23,080 --> 21:31:27,640 save lastly we'll learn how to handle 25189 21:31:25,640 --> 21:31:30,480 errors in async 25190 21:31:27,640 --> 21:31:32,800 await so let's go to the left and we're 25191 21:31:30,480 --> 21:31:36,552 going to open the checkout file so we'll 25192 21:31:32,800 --> 21:31:40,232 scroll down and open checkout. 25193 21:31:36,552 --> 21:31:42,960 JS and inside here we're using async 25194 21:31:40,232 --> 21:31:45,400 await so the catch errors when using 25195 21:31:42,960 --> 21:31:48,192 async await we're going to use some new 25196 21:31:45,400 --> 21:31:51,232 syntax called try 25197 21:31:48,192 --> 21:31:55,960 catch above this we're going to type the 25198 21:31:51,232 --> 21:31:55,960 word try and curly 25199 21:31:56,120 --> 21:32:02,512 brackets and now we're going to put the 25200 21:31:58,232 --> 21:32:04,680 code that could cause an error into try 25201 21:32:02,512 --> 21:32:07,040 so here we're making a request to the 25202 21:32:04,680 --> 21:32:09,960 back end and here we're also making a 25203 21:32:07,040 --> 21:32:12,080 request to the back end so this code 25204 21:32:09,960 --> 21:32:15,960 could cause an error so we're going to 25205 21:32:12,080 --> 21:32:17,872 move it inside try so we'll select it 25206 21:32:15,960 --> 21:32:20,872 right click 25207 21:32:17,872 --> 21:32:23,920 cut and then inside here we'll right 25208 21:32:20,872 --> 21:32:27,760 click and 25209 21:32:23,920 --> 21:32:32,040 paste and now if any of this code inside 25210 21:32:27,760 --> 21:32:34,960 a try gets an error we can catch it 25211 21:32:32,040 --> 21:32:36,280 using this code at at the bottom we'll 25212 21:32:34,960 --> 21:32:40,360 type 25213 21:32:36,280 --> 21:32:43,320 catch brackets and curly 25214 21:32:40,360 --> 21:32:47,680 brackets Now Catch is going to get one 25215 21:32:43,320 --> 21:32:49,512 parameter in these brackets called error 25216 21:32:47,680 --> 21:32:51,800 and this contains information about the 25217 21:32:49,512 --> 21:32:57,360 error if we need 25218 21:32:51,800 --> 21:33:01,192 it so catch Works similarly to catch for 25219 21:32:57,360 --> 21:33:03,232 promises if there's an error inside here 25220 21:33:01,192 --> 21:33:05,320 it's going to catch the error or it's 25221 21:33:03,232 --> 21:33:08,872 going to hand handle the error and run 25222 21:33:05,320 --> 21:33:11,960 the code inside catch so in here we can 25223 21:33:08,872 --> 21:33:14,232 put our error message so let's go back 25224 21:33:11,960 --> 21:33:17,600 to products. 25225 21:33:14,232 --> 21:33:22,232 JS and let's copy the error message so 25226 21:33:17,600 --> 21:33:24,832 we'll select this and right click 25227 21:33:22,232 --> 21:33:29,320 copy and back to 25228 21:33:24,832 --> 21:33:32,720 checkout inside catch we'll right click 25229 21:33:29,320 --> 21:33:32,720 and paste 25230 21:33:34,400 --> 21:33:39,680 so now I'll remove this new line and add 25231 21:33:37,760 --> 21:33:41,600 a new line here to make it a little 25232 21:33:39,680 --> 21:33:44,960 easier to 25233 21:33:41,600 --> 21:33:48,192 read and let's test out this 25234 21:33:44,960 --> 21:33:50,760 code to do that we'll go inside 25235 21:33:48,192 --> 21:33:54,000 products. JS again and we're going to 25236 21:33:50,760 --> 21:33:58,000 modify the URL again so at the front 25237 21:33:54,000 --> 21:34:00,920 here we're going to add error do super 25238 21:33:58,000 --> 21:34:04,280 simple backend and also we're going to 25239 21:34:00,920 --> 21:34:06,680 comment out the dot catch because right 25240 21:34:04,280 --> 21:34:08,960 now the error is going to be handled 25241 21:34:06,680 --> 21:34:12,232 here and it's not going to be handled 25242 21:34:08,960 --> 21:34:15,400 outside in the try catch so we're going 25243 21:34:12,232 --> 21:34:17,440 to comment this out using 25244 21:34:15,400 --> 21:34:22,040 slashstar and at the 25245 21:34:17,440 --> 21:34:26,440 end star slash and 25246 21:34:22,040 --> 21:34:30,800 save so we'll go back to checkout. 25247 21:34:26,440 --> 21:34:35,400 JS and here we'll also 25248 21:34:30,800 --> 21:34:38,080 save and now let's open the console 25249 21:34:35,400 --> 21:34:41,720 and you'll see that we had an error here 25250 21:34:38,080 --> 21:34:44,832 we caught the error and it ran this code 25251 21:34:41,720 --> 21:34:47,480 and displayed the error 25252 21:34:44,832 --> 21:34:50,920 message so that's how we handle errors 25253 21:34:47,480 --> 21:34:55,080 with async await we put our code that 25254 21:34:50,920 --> 21:34:57,232 can cause an error inside try and if 25255 21:34:55,080 --> 21:35:00,552 there is an error it's going to run the 25256 21:34:57,232 --> 21:35:00,552 code inside 25257 21:35:00,720 --> 21:35:08,920 catch so now let's go back to products 25258 21:35:04,680 --> 21:35:13,480 and remove error from the URL and the 25259 21:35:08,920 --> 21:35:16,872 dot and we'll also uncomment 25260 21:35:13,480 --> 21:35:16,872 catch and 25261 21:35:18,000 --> 21:35:23,872 save next we're going to learn some more 25262 21:35:20,552 --> 21:35:25,552 details about try catch so we'll open 25263 21:35:23,872 --> 21:35:29,120 checkout. 25264 21:35:25,552 --> 21:35:31,232 JS and one thing to know about TR catch 25265 21:35:29,120 --> 21:35:35,040 is that we don't have to use it with 25266 21:35:31,232 --> 21:35:39,192 async await we can actually use TR catch 25267 21:35:35,040 --> 21:35:42,640 with synchronous code or normal code for 25268 21:35:39,192 --> 21:35:45,360 example here we try to run a function 25269 21:35:42,640 --> 21:35:49,760 that does not exist and this will cause 25270 21:35:45,360 --> 21:35:53,160 an error try catch will catch this error 25271 21:35:49,760 --> 21:35:56,360 and display a message so notice that 25272 21:35:53,160 --> 21:36:00,160 we're not using async O8 here this is 25273 21:35:56,360 --> 21:36:04,640 just normal code so we can use try catch 25274 21:36:00,160 --> 21:36:07,120 to catch errors in normal code as well 25275 21:36:04,640 --> 21:36:10,680 another detail is whenever we get an 25276 21:36:07,120 --> 21:36:13,832 error it will skip the rest of the code 25277 21:36:10,680 --> 21:36:17,192 so here it will not console.log next 25278 21:36:13,832 --> 21:36:20,720 line it will skip the rest of this code 25279 21:36:17,192 --> 21:36:20,720 and go directly into 25280 21:36:21,920 --> 21:36:26,512 catch now one thing you might be 25281 21:36:23,920 --> 21:36:29,480 wondering is why don't we just use TR 25282 21:36:26,512 --> 21:36:32,720 catch everywhere and prevent all of our 25283 21:36:29,480 --> 21:36:36,040 errors so try catch and any error 25284 21:36:32,720 --> 21:36:39,192 handling is meant to handle unexpected 25285 21:36:36,040 --> 21:36:41,640 errors that means our code is correct 25286 21:36:39,192 --> 21:36:43,600 but something outside our control caused 25287 21:36:41,640 --> 21:36:45,800 the error so we can give some 25288 21:36:43,600 --> 21:36:48,960 instructions on what to 25289 21:36:45,800 --> 21:36:52,000 do so that's why we don't use try catch 25290 21:36:48,960 --> 21:36:56,600 everywhere we only use it for unexpected 25291 21:36:52,000 --> 21:36:56,600 errors or errors outside of our 25292 21:36:58,720 --> 21:37:03,512 control so the last thing to know about 25293 21:37:01,232 --> 21:37:05,192 error handling is that we can man 25294 21:37:03,512 --> 21:37:08,600 manually create 25295 21:37:05,192 --> 21:37:11,960 errors to manually create an error in TR 25296 21:37:08,600 --> 21:37:13,872 catch at the top we're going to use the 25297 21:37:11,960 --> 21:37:18,160 word 25298 21:37:13,872 --> 21:37:20,040 throw so throw creates a new error or it 25299 21:37:18,160 --> 21:37:21,080 throws an error that we're going to 25300 21:37:20,040 --> 21:37:23,800 catch a 25301 21:37:21,080 --> 21:37:24,760 later and now we need to give this error 25302 21:37:23,800 --> 21:37:27,720 a 25303 21:37:24,760 --> 21:37:31,320 value so we can use any type of value 25304 21:37:27,720 --> 21:37:33,872 for an error like a string number or an 25305 21:37:31,320 --> 21:37:37,552 object for an example we're just going 25306 21:37:33,872 --> 21:37:40,720 to use a string error 25307 21:37:37,552 --> 21:37:43,760 one so this will manually create an 25308 21:37:40,720 --> 21:37:46,400 error when we get an error it's going to 25309 21:37:43,760 --> 21:37:48,960 skip the rest of the code inside these 25310 21:37:46,400 --> 21:37:52,760 curly brackets and it's going to go 25311 21:37:48,960 --> 21:37:55,160 straight into catch so this error value 25312 21:37:52,760 --> 21:37:57,512 is going to be saved inside this 25313 21:37:55,160 --> 21:37:59,360 parameter and it's going to run the code 25314 21:37:57,512 --> 21:38:01,760 inside 25315 21:37:59,360 --> 21:38:04,600 catch so if we 25316 21:38:01,760 --> 21:38:07,832 save and then check the 25317 21:38:04,600 --> 21:38:12,192 console throw creates an error and it's 25318 21:38:07,832 --> 21:38:12,192 going to display this error message up 25319 21:38:14,512 --> 21:38:21,552 here so now let's comment out this 25320 21:38:18,080 --> 21:38:21,552 example and 25321 21:38:21,600 --> 21:38:29,552 save next if we're using promises there 25322 21:38:25,040 --> 21:38:29,552 are two ways to manually create an 25323 21:38:29,600 --> 21:38:35,800 error so inside this promise if we want 25324 21:38:33,440 --> 21:38:40,280 want to manually create an error we can 25325 21:38:35,800 --> 21:38:41,552 also just throw an error like the string 25326 21:38:40,280 --> 21:38:44,640 error 25327 21:38:41,552 --> 21:38:49,080 2 and this will throw an error and go 25328 21:38:44,640 --> 21:38:52,320 inside dot catch by the way if we await 25329 21:38:49,080 --> 21:38:55,120 a promise instead of going into catch 25330 21:38:52,320 --> 21:38:58,600 it's going to go inside catch 25331 21:38:55,120 --> 21:39:02,160 here so await makes it behave sort of 25332 21:38:58,600 --> 21:39:04,640 like synchronous code or normal code if 25333 21:39:02,160 --> 21:39:07,232 we save 25334 21:39:04,640 --> 21:39:07,232 and check the 25335 21:39:07,400 --> 21:39:13,512 console it will get an error it will not 25336 21:39:10,552 --> 21:39:15,920 load the cart and it will display this 25337 21:39:13,512 --> 21:39:19,600 error 25338 21:39:15,920 --> 21:39:21,960 message so now let's comment out this 25339 21:39:19,600 --> 21:39:24,440 example and we'll learn the second way 25340 21:39:21,960 --> 21:39:27,400 to create an error in a 25341 21:39:24,440 --> 21:39:30,280 promise inside a promise if we need to 25342 21:39:27,400 --> 21:39:34,280 create an error in the future then we 25343 21:39:30,280 --> 21:39:37,232 need to use different code for example 25344 21:39:34,280 --> 21:39:40,600 here we load the cart and then once it's 25345 21:39:37,232 --> 21:39:44,720 finished loading we're going to run this 25346 21:39:40,600 --> 21:39:47,920 function so this function runs in the 25347 21:39:44,720 --> 21:39:51,040 future inside this function throw does 25348 21:39:47,920 --> 21:39:52,320 not work because throw does not work in 25349 21:39:51,040 --> 21:39:55,920 the 25350 21:39:52,320 --> 21:39:59,640 future instead promises give us another 25351 21:39:55,920 --> 21:40:02,800 way to create an error when we create a 25352 21:39:59,640 --> 21:40:04,720 new promise it gives us a second 25353 21:40:02,800 --> 21:40:06,680 parameter 25354 21:40:04,720 --> 21:40:10,360 called 25355 21:40:06,680 --> 21:40:14,232 reject reject is a function and it lets 25356 21:40:10,360 --> 21:40:14,232 us create an error in the 25357 21:40:14,280 --> 21:40:22,552 future so here after we load the cart in 25358 21:40:18,192 --> 21:40:29,440 the future let's create an error using 25359 21:40:22,552 --> 21:40:32,760 reject brackets and a string error 25360 21:40:29,440 --> 21:40:34,552 three so let's comment out resolve as an 25361 21:40:32,760 --> 21:40:36,440 example 25362 21:40:34,552 --> 21:40:40,040 so now let's 25363 21:40:36,440 --> 21:40:40,040 save and check the 25364 21:40:40,080 --> 21:40:45,080 console and you'll see that reject 25365 21:40:42,480 --> 21:40:47,920 created an error in the 25366 21:40:45,080 --> 21:40:51,120 future and then it goes down here and 25367 21:40:47,920 --> 21:40:53,720 runs the code inside catch and creates 25368 21:40:51,120 --> 21:40:53,720 the error 25369 21:40:55,720 --> 21:41:01,832 message so the manually create an error 25370 21:40:58,480 --> 21:41:04,000 in a promise we have two ways we can use 25371 21:41:01,832 --> 21:41:07,320 throw if we're creating the error 25372 21:41:04,000 --> 21:41:10,480 synchronously or right away or we can 25373 21:41:07,320 --> 21:41:13,800 use reject if we're creating the error 25374 21:41:10,480 --> 21:41:17,360 asynchronously or in the 25375 21:41:13,800 --> 21:41:23,512 future so now let's comment out 25376 21:41:17,360 --> 21:41:23,512 reject and uncomment resolve and 25377 21:41:23,720 --> 21:41:29,800 save so that's error handling it lets us 25378 21:41:27,160 --> 21:41:32,120 handle unexpected errors and it also 25379 21:41:29,800 --> 21:41:34,872 lets us create our own 25380 21:41:32,120 --> 21:41:39,720 errors so now let's save these changes 25381 21:41:34,872 --> 21:41:43,120 into git we'll open git inside products. 25382 21:41:39,720 --> 21:41:46,400 JS we added. catch for 25383 21:41:43,120 --> 21:41:49,760 promises and an error function for 25384 21:41:46,400 --> 21:41:53,872 callbacks and then in checkout. JS we 25385 21:41:49,760 --> 21:41:56,160 added try catch for async await and we 25386 21:41:53,872 --> 21:41:57,080 also learned how to manually create our 25387 21:41:56,160 --> 21:41:59,320 own 25388 21:41:57,080 --> 21:42:00,760 errors so we'll type a message 25389 21:41:59,320 --> 21:42:04,920 describing our 25390 21:42:00,760 --> 21:42:08,440 changes learn and error 25391 21:42:04,920 --> 21:42:08,440 handling and 25392 21:42:12,552 --> 21:42:18,440 commit now that we learned backend 25393 21:42:15,232 --> 21:42:20,640 promises and async await let's use them 25394 21:42:18,440 --> 21:42:23,552 in our 25395 21:42:20,640 --> 21:42:27,192 project we'll open our 25396 21:42:23,552 --> 21:42:29,192 project and open the tab for the project 25397 21:42:27,192 --> 21:42:31,320 and we're going to go to the checkout 25398 21:42:29,192 --> 21:42:34,360 page so the feature we're going to work 25399 21:42:31,320 --> 21:42:37,320 on is in the checkout page we're going 25400 21:42:34,360 --> 21:42:40,040 to make this place order button 25401 21:42:37,320 --> 21:42:42,600 interactive when we click this button 25402 21:42:40,040 --> 21:42:44,040 it's going to take our cart and turn it 25403 21:42:42,600 --> 21:42:46,960 into an 25404 21:42:44,040 --> 21:42:49,320 order this time instead of writing this 25405 21:42:46,960 --> 21:42:53,000 code ourselves we're going to use the 25406 21:42:49,320 --> 21:42:53,000 back end to create an 25407 21:42:53,400 --> 21:42:59,000 order so first let's set up an event 25408 21:42:56,192 --> 21:43:00,512 listener for this button we'll go back 25409 21:42:59,000 --> 21:43:03,960 to our 25410 21:43:00,512 --> 21:43:05,920 code and open our files 25411 21:43:03,960 --> 21:43:07,640 and that button is in a section called 25412 21:43:05,920 --> 21:43:10,480 the payment 25413 21:43:07,640 --> 21:43:15,512 summary so we're going to open scripts 25414 21:43:10,480 --> 21:43:15,512 SL checkout payment summary.jpg 25415 21:43:34,120 --> 21:43:39,960 your order and now we have the code for 25416 21:43:37,552 --> 21:43:42,320 the button right here so we're going to 25417 21:43:39,960 --> 21:43:43,800 set up an event listener so we can do 25418 21:43:42,320 --> 21:43:46,872 something when we click 25419 21:43:43,800 --> 21:43:49,160 it first we'll add a class to the button 25420 21:43:46,872 --> 21:43:51,872 so we can get it in our 25421 21:43:49,160 --> 21:43:55,600 JavaScript at the end of the class let's 25422 21:43:51,872 --> 21:43:59,360 create a new line and add a class 25423 21:43:55,600 --> 21:44:03,040 js- Place Das 25424 21:43:59,360 --> 21:44:06,760 order and now after we put this HTML on 25425 21:44:03,040 --> 21:44:09,320 the page we're going to set up an event 25426 21:44:06,760 --> 21:44:13,760 listener to do that we're going to get 25427 21:44:09,320 --> 21:44:16,832 this button into our JavaScript using 25428 21:44:13,760 --> 21:44:18,480 document. query 25429 21:44:16,832 --> 21:44:21,960 selector 25430 21:44:18,480 --> 21:44:24,960 brackets and a string and to select this 25431 21:44:21,960 --> 21:44:28,440 class we're going to start with a 25432 21:44:24,960 --> 21:44:31,160 js- place- 25433 21:44:28,440 --> 21:44:33,000 order and now that we have this button 25434 21:44:31,160 --> 21:44:36,872 we're going to add an event listener to 25435 21:44:33,000 --> 21:44:41,760 to it so we'll create a new line and use 25436 21:44:36,872 --> 21:44:43,440 the method do add event 25437 21:44:41,760 --> 21:44:45,720 listener 25438 21:44:43,440 --> 21:44:48,232 brackets and we'll give this two 25439 21:44:45,720 --> 21:44:52,000 parameters the first parameter is the 25440 21:44:48,232 --> 21:44:54,640 event we want to listen for which is 25441 21:44:52,000 --> 21:44:56,600 click and the second parameter is a 25442 21:44:54,640 --> 21:45:00,192 function we want to run when we click 25443 21:44:56,600 --> 21:45:03,192 this button so we'll type brackets arrow 25444 21:45:00,192 --> 21:45:05,400 and curler brackets 25445 21:45:03,192 --> 21:45:07,720 and now when we click this button we're 25446 21:45:05,400 --> 21:45:10,512 going to make a request to the back end 25447 21:45:07,720 --> 21:45:10,512 to create the 25448 21:45:10,960 --> 21:45:18,080 order so for this example I set up a URL 25449 21:45:14,872 --> 21:45:20,000 path in my backend for creating orders 25450 21:45:18,080 --> 21:45:22,832 called slash 25451 21:45:20,000 --> 21:45:26,040 orders so inside here we're going to 25452 21:45:22,832 --> 21:45:27,600 make a request to the backend using 25453 21:45:26,040 --> 21:45:29,512 fetch 25454 21:45:27,600 --> 21:45:34,232 brackets and then we're going to give it 25455 21:45:29,512 --> 21:45:36,120 the URL of the request which is H ttps 25456 21:45:34,232 --> 21:45:40,192 colon double 25457 21:45:36,120 --> 21:45:47,040 slash super simple 25458 21:45:40,192 --> 21:45:47,040 backend. Dev and the URL path slash 25459 21:45:47,512 --> 21:45:53,320 orders however this time we need to send 25460 21:45:50,680 --> 21:45:56,512 some data to the back end so we need to 25461 21:45:53,320 --> 21:45:59,280 send our cart to the backend in order to 25462 21:45:56,512 --> 21:45:59,280 create an 25463 21:45:59,960 --> 21:46:07,080 order to send data in a request we need 25464 21:46:03,480 --> 21:46:10,192 to use a different type of requests so 25465 21:46:07,080 --> 21:46:14,760 there are four common types of requests 25466 21:46:10,192 --> 21:46:17,640 get post put and delete get means we 25467 21:46:14,760 --> 21:46:19,872 want to get something from the back end 25468 21:46:17,640 --> 21:46:23,192 post means we want the back end to 25469 21:46:19,872 --> 21:46:25,680 create something put means we want it to 25470 21:46:23,192 --> 21:46:29,080 update something and delete means we 25471 21:46:25,680 --> 21:46:32,640 want to delete something so far we only 25472 21:46:29,080 --> 21:46:34,000 use get request which let us get data 25473 21:46:32,640 --> 21:46:37,320 from from the back 25474 21:46:34,000 --> 21:46:41,480 end however get requests don't really 25475 21:46:37,320 --> 21:46:41,480 let us send data to the back 25476 21:46:42,232 --> 21:46:48,080 end so in this situation we want to 25477 21:46:45,552 --> 21:46:52,080 create an order so we're going to use 25478 21:46:48,080 --> 21:46:55,552 post to create something post lets us 25479 21:46:52,080 --> 21:46:55,552 send data to the 25480 21:46:56,600 --> 21:47:02,232 backend so to create a post request 25481 21:47:00,000 --> 21:47:04,400 instead of a get request we're going to 25482 21:47:02,232 --> 21:47:07,440 give fetch a second 25483 21:47:04,400 --> 21:47:10,320 parameter which is an 25484 21:47:07,440 --> 21:47:13,280 object and inside this object we're 25485 21:47:10,320 --> 21:47:14,920 going to give it a property called 25486 21:47:13,280 --> 21:47:17,400 method 25487 21:47:14,920 --> 21:47:19,800 colon and this is the type of the 25488 21:47:17,400 --> 21:47:22,000 request in this case we'll use the 25489 21:47:19,800 --> 21:47:25,320 string 25490 21:47:22,000 --> 21:47:27,760 post and after this we're going to give 25491 21:47:25,320 --> 21:47:31,120 another property called 25492 21:47:27,760 --> 21:47:33,400 headers colon and this is going to be an 25493 21:47:31,120 --> 21:47:35,680 object 25494 21:47:33,400 --> 21:47:38,920 headers gives the backend more 25495 21:47:35,680 --> 21:47:41,080 information about our request and this 25496 21:47:38,920 --> 21:47:42,360 is needed when we're sending data to the 25497 21:47:41,080 --> 21:47:45,232 back 25498 21:47:42,360 --> 21:47:50,720 end inside this object we'll add the 25499 21:47:45,232 --> 21:47:52,480 property string Capital C content Das 25500 21:47:50,720 --> 21:47:54,120 capital T 25501 21:47:52,480 --> 21:47:58,720 type 25502 21:47:54,120 --> 21:48:03,760 colon and the value will be the string 25503 21:47:58,720 --> 21:48:06,120 application slash Json 25504 21:48:03,760 --> 21:48:09,400 this tells the back endend what type of 25505 21:48:06,120 --> 21:48:11,640 data we're sending in our request here 25506 21:48:09,400 --> 21:48:14,192 we're going to send some Jon which is 25507 21:48:11,640 --> 21:48:16,872 basically a JavaScript 25508 21:48:14,192 --> 21:48:20,120 object next we're going to add another 25509 21:48:16,872 --> 21:48:23,080 property called body 25510 21:48:20,120 --> 21:48:25,192 colon and this is the actual data we're 25511 21:48:23,080 --> 21:48:28,120 going to send to the back 25512 21:48:25,192 --> 21:48:30,320 end according to the documentation we 25513 21:48:28,120 --> 21:48:35,280 need to send an object with a property 25514 21:48:30,320 --> 21:48:35,280 called cart and this contains our cart 25515 21:48:36,192 --> 21:48:44,400 array so in the body let's send an 25516 21:48:39,720 --> 21:48:46,080 object with a property called cart colon 25517 21:48:44,400 --> 21:48:48,720 and give it the cart 25518 21:48:46,080 --> 21:48:53,120 array so we should have the cart array 25519 21:48:48,720 --> 21:48:57,000 in this file and it should be called 25520 21:48:53,120 --> 21:48:59,280 cart and finally we can't send an object 25521 21:48:57,000 --> 21:49:04,160 directly in our request we need to 25522 21:48:59,280 --> 21:49:04,160 convert it into a Json string 25523 21:49:05,400 --> 21:49:11,400 to do that at the front we'll use 25524 21:49:09,640 --> 21:49:16,080 json. 25525 21:49:11,400 --> 21:49:18,680 stringify Open Bracket and close bracket 25526 21:49:16,080 --> 21:49:21,512 so this will convert this data into a 25527 21:49:18,680 --> 21:49:23,872 Json string and now we can send it to 25528 21:49:21,512 --> 21:49:23,872 the back 25529 21:49:24,080 --> 21:49:30,360 end okay so after we send this request 25530 21:49:27,720 --> 21:49:34,320 we need to wait for the response to come 25531 21:49:30,360 --> 21:49:37,192 back to do that fetch returns a 25532 21:49:34,320 --> 21:49:40,680 promise however instead of using dot 25533 21:49:37,192 --> 21:49:43,872 then to add a Next Step let's use async 25534 21:49:40,680 --> 21:49:46,232 a weit because it's cleaner so first we 25535 21:49:43,872 --> 21:49:49,680 need to make this function 25536 21:49:46,232 --> 21:49:52,000 async remember we can only use await 25537 21:49:49,680 --> 21:49:54,120 inside an async 25538 21:49:52,000 --> 21:49:58,120 function so at the front of this 25539 21:49:54,120 --> 21:49:58,120 function we'll type the word 25540 21:49:58,760 --> 21:50:04,552 async and now in front of fetch we can 25541 21:50:01,800 --> 21:50:04,552 use 25542 21:50:04,800 --> 21:50:10,400 await this is going to wait for fetch to 25543 21:50:07,920 --> 21:50:12,760 finish or for fetch to get a response 25544 21:50:10,400 --> 21:50:14,280 from the back end and then go to the 25545 21:50:12,760 --> 21:50:17,552 next 25546 21:50:14,280 --> 21:50:21,192 line now because we used await we can 25547 21:50:17,552 --> 21:50:24,680 save the response in a variable so at 25548 21:50:21,192 --> 21:50:28,680 the front we'll create a variable const 25549 21:50:24,680 --> 21:50:32,600 and call it response and make it equal 25550 21:50:28,680 --> 21:50:32,600 to this result 25551 21:50:33,360 --> 21:50:38,280 next remember that to get the data 25552 21:50:36,000 --> 21:50:40,640 that's attached to the response we need 25553 21:50:38,280 --> 21:50:45,080 to use response. 25554 21:50:40,640 --> 21:50:48,720 Json so down here we're going to use 25555 21:50:45,080 --> 21:50:51,400 response. Json and 25556 21:50:48,720 --> 21:50:55,192 brackets now remember that response. 25557 21:50:51,400 --> 21:50:58,440 Json is also a promise so at the front 25558 21:50:55,192 --> 21:51:00,760 we can use await to wait for this 25559 21:50:58,440 --> 21:51:03,280 promise to finish before going to the 25560 21:51:00,760 --> 21:51:05,440 next line 25561 21:51:03,280 --> 21:51:08,320 so this will give us the data that's 25562 21:51:05,440 --> 21:51:11,600 attached to the response which should be 25563 21:51:08,320 --> 21:51:14,920 the order that was created by the 25564 21:51:11,600 --> 21:51:20,000 backet so let's save this in a variable 25565 21:51:14,920 --> 21:51:20,000 using const order 25566 21:51:21,400 --> 21:51:28,720 equals and finally let's 25567 21:51:25,480 --> 21:51:32,960 console.log the order to see what it 25568 21:51:28,720 --> 21:51:35,192 looks like so now let's save 25569 21:51:32,960 --> 21:51:37,680 and then we'll open the 25570 21:51:35,192 --> 21:51:39,160 project and make sure you have some 25571 21:51:37,680 --> 21:51:43,160 products in your 25572 21:51:39,160 --> 21:51:45,760 cart and then we'll click place 25573 21:51:43,160 --> 21:51:49,080 order and now we can check our console 25574 21:51:45,760 --> 21:51:49,080 to see what the order looks 25575 21:51:52,160 --> 21:51:58,400 like and you'll see that we got an order 25576 21:51:55,080 --> 21:52:03,800 object from the back end it has an order 25577 21:51:58,400 --> 21:52:07,400 ID an order time and the products in our 25578 21:52:03,800 --> 21:52:09,680 order including a delivery time and the 25579 21:52:07,400 --> 21:52:12,960 quantity that we 25580 21:52:09,680 --> 21:52:14,872 want so we successfully use the back end 25581 21:52:12,960 --> 21:52:17,920 to create a new 25582 21:52:14,872 --> 21:52:20,800 order let's also check the network tab 25583 21:52:17,920 --> 21:52:23,360 up here and open 25584 21:52:20,800 --> 21:52:26,320 network so I'll resize this to make it a 25585 21:52:23,360 --> 21:52:28,960 little easier to read and we'll search 25586 21:52:26,320 --> 21:52:31,720 for the super simple 25587 21:52:28,960 --> 21:52:36,232 URL and then we'll find this request 25588 21:52:31,720 --> 21:52:39,920 orders with a status 200 and we'll click 25589 21:52:36,232 --> 21:52:44,080 it so inside the details you'll see that 25590 21:52:39,920 --> 21:52:47,960 it was a post request sent to/ orders 25591 21:52:44,080 --> 21:52:51,800 and the status code was 200 so it was 25592 21:52:47,960 --> 21:52:54,120 successful inside the payload tab this 25593 21:52:51,800 --> 21:52:55,480 is what we sent to the back end which is 25594 21:52:54,120 --> 21:52:58,552 our 25595 21:52:55,480 --> 21:53:02,640 cart in the preview tab this is what the 25596 21:52:58,552 --> 21:53:04,920 backend sent back an order object 25597 21:53:02,640 --> 21:53:08,720 and the response tab is the same thing 25598 21:53:04,920 --> 21:53:08,720 we got the order object 25599 21:53:09,440 --> 21:53:15,440 back so that's how a real world website 25600 21:53:12,720 --> 21:53:19,400 works instead of creating the order on 25601 21:53:15,440 --> 21:53:23,000 the web page or on our computer we send 25602 21:53:19,400 --> 21:53:25,120 an HTTP request to the backend to create 25603 21:53:23,000 --> 21:53:25,120 the 25604 21:53:30,120 --> 21:53:36,800 order okay now that we created an order 25605 21:53:33,872 --> 21:53:39,600 let's save it 25606 21:53:36,800 --> 21:53:43,400 somewhere we'll go back to our 25607 21:53:39,600 --> 21:53:45,872 code and on the left inside the data 25608 21:53:43,400 --> 21:53:49,232 folder let's create a file for our 25609 21:53:45,872 --> 21:53:52,360 orders so we'll click inside this folder 25610 21:53:49,232 --> 21:53:54,720 and then click here to create a file and 25611 21:53:52,360 --> 21:53:58,832 let's name it 25612 21:53:54,720 --> 21:54:01,832 orders. JS and press 25613 21:53:58,832 --> 21:54:05,232 enter at the top let's create an array 25614 21:54:01,832 --> 21:54:10,080 to contain rain all of our orders we use 25615 21:54:05,232 --> 21:54:12,400 const orders equals and empty array and 25616 21:54:10,080 --> 21:54:15,320 let's export this so we can use it in 25617 21:54:12,400 --> 21:54:18,512 other files 25618 21:54:15,320 --> 21:54:21,552 export next let's create a function for 25619 21:54:18,512 --> 21:54:23,080 adding an order to this array so we'll 25620 21:54:21,552 --> 21:54:25,512 create a 25621 21:54:23,080 --> 21:54:29,232 function add 25622 21:54:25,512 --> 21:54:33,080 order brackets and curly brackets and 25623 21:54:29,232 --> 21:54:35,720 we'll give this function an order object 25624 21:54:33,080 --> 21:54:38,920 and we're just going to add it to the 25625 21:54:35,720 --> 21:54:41,680 array now for orders we usually want the 25626 21:54:38,920 --> 21:54:43,960 most recent order at the top so we're 25627 21:54:41,680 --> 21:54:47,040 going to add this order to the front of 25628 21:54:43,960 --> 21:54:52,680 the array to do that we'll type the 25629 21:54:47,040 --> 21:54:54,192 array name orders and use the method do 25630 21:54:52,680 --> 21:54:57,552 unshift 25631 21:54:54,192 --> 21:55:00,232 brackets and we'll give it the 25632 21:54:57,552 --> 21:55:03,192 order so this will add the order to the 25633 21:55:00,232 --> 21:55:06,832 front of the array instead of the 25634 21:55:03,192 --> 21:55:09,800 back finally let's save our orders into 25635 21:55:06,832 --> 21:55:12,720 local storage so we'll create a function 25636 21:55:09,800 --> 21:55:16,120 to save to local storage 25637 21:55:12,720 --> 21:55:19,920 function save to 25638 21:55:16,120 --> 21:55:22,920 storage brackets and curly brackets and 25639 21:55:19,920 --> 21:55:28,600 we'll run local 25640 21:55:22,920 --> 21:55:33,872 storage. set item brackets and a string 25641 21:55:28,600 --> 21:55:36,600 we'll use a local storage key orders 25642 21:55:33,872 --> 21:55:39,640 comma and remember local storage only 25643 21:55:36,600 --> 21:55:41,192 supports strings so we'll use 25644 21:55:39,640 --> 21:55:43,480 json. 25645 21:55:41,192 --> 21:55:45,720 stringify 25646 21:55:43,480 --> 21:55:49,320 brackets 25647 21:55:45,720 --> 21:55:56,160 orders and now when we modify this array 25648 21:55:49,320 --> 21:55:56,160 we're going to save to storage and 25649 21:55:57,040 --> 21:56:02,720 brackets finally at the top let's load 25650 21:56:00,512 --> 21:56:04,192 the orders from Storage 25651 21:56:02,720 --> 21:56:08,160 so 25652 21:56:04,192 --> 21:56:14,832 here we use local 25653 21:56:08,160 --> 21:56:17,552 storage. getet item brackets the string 25654 21:56:14,832 --> 21:56:20,000 orders and remember that this will be a 25655 21:56:17,552 --> 21:56:25,640 string so we need to convert it back 25656 21:56:20,000 --> 21:56:28,160 into an array using the code json. pars 25657 21:56:25,640 --> 21:56:28,160 and 25658 21:56:28,232 --> 21:56:34,552 brackets now in the beginning we're not 25659 21:56:30,832 --> 21:56:37,440 going to have any orders saved in local 25660 21:56:34,552 --> 21:56:43,040 storage so we can give this a default 25661 21:56:37,440 --> 21:56:45,120 value using or and an empty array so if 25662 21:56:43,040 --> 21:56:47,512 there's nothing in local storage it's 25663 21:56:45,120 --> 21:56:51,080 going to use this empty array as a 25664 21:56:47,512 --> 21:56:51,080 default and 25665 21:56:53,120 --> 21:56:59,640 save so now let's use add order in the 25666 21:56:56,440 --> 21:57:00,832 payment summary so first we'll export 25667 21:56:59,640 --> 21:57:03,480 this 25668 21:57:00,832 --> 21:57:07,232 function and and 25669 21:57:03,480 --> 21:57:10,160 save and then open the payment summary 25670 21:57:07,232 --> 21:57:13,552 again and at the top we're going to 25671 21:57:10,160 --> 21:57:18,040 import that function so here we'll 25672 21:57:13,552 --> 21:57:21,040 import curly brackets add 25673 21:57:18,040 --> 21:57:24,320 order from a 25674 21:57:21,040 --> 21:57:26,872 string and we'll go into the data folder 25675 21:57:24,320 --> 21:57:30,872 which is do do slash 25676 21:57:26,872 --> 21:57:34,192 dotdata SL orders 25677 21:57:30,872 --> 21:57:37,160 doj and and we'll scroll back 25678 21:57:34,192 --> 21:57:39,400 down and now after we create an order 25679 21:57:37,160 --> 21:57:42,040 from the back end we're going to add it 25680 21:57:39,400 --> 21:57:47,760 to the array and save it in local 25681 21:57:42,040 --> 21:57:51,232 storage using the function add order 25682 21:57:47,760 --> 21:57:54,232 brackets the order 25683 21:57:51,232 --> 21:57:58,320 object and 25684 21:57:54,232 --> 21:58:00,160 save now if we go to our web page and 25685 21:57:58,320 --> 21:58:02,320 click place 25686 21:58:00,160 --> 21:58:05,040 order this should create an order order 25687 21:58:02,320 --> 21:58:08,480 from the back end and save it in local 25688 21:58:05,040 --> 21:58:12,192 storage so now we can open our 25689 21:58:08,480 --> 21:58:15,280 console and go to the console Tab and 25690 21:58:12,192 --> 21:58:18,480 let's check if it's inside local storage 25691 21:58:15,280 --> 21:58:22,872 using local 25692 21:58:18,480 --> 21:58:24,720 storage. getet item brackets and the 25693 21:58:22,872 --> 21:58:28,280 string 25694 21:58:24,720 --> 21:58:28,280 orders and press 25695 21:58:28,400 --> 21:58:34,040 enter and you'll see that we saved an 25696 21:58:30,760 --> 21:58:34,040 order in local store 25697 21:58:34,960 --> 21:58:40,000 storage if you ever want to remove the 25698 21:58:37,360 --> 21:58:41,512 orders from local storage you can use 25699 21:58:40,000 --> 21:58:44,360 this 25700 21:58:41,512 --> 21:58:49,232 code local 25701 21:58:44,360 --> 21:58:51,512 storage. remove item brackets and the 25702 21:58:49,232 --> 21:58:56,760 string 25703 21:58:51,512 --> 21:58:56,760 orders press enter and then refresh the 25704 21:58:59,160 --> 21:59:05,192 page let's go back to our code 25705 21:59:02,920 --> 21:59:08,640 and let's get some practice and add some 25706 21:59:05,192 --> 21:59:11,680 error handling to this code so at the 25707 21:59:08,640 --> 21:59:16,360 top for asyn of weight we're going to 25708 21:59:11,680 --> 21:59:21,000 use try catch so we'll type try curly 25709 21:59:16,360 --> 21:59:23,832 bracket and catch brackets and curly 25710 21:59:21,000 --> 21:59:25,800 brackets and catch gets a parameter 25711 21:59:23,832 --> 21:59:27,832 called 25712 21:59:25,800 --> 21:59:32,080 error so now we're going to move our 25713 21:59:27,832 --> 21:59:34,680 code inside a try we'll select this code 25714 21:59:32,080 --> 21:59:40,640 and then right click 25715 21:59:34,680 --> 21:59:44,192 cut inside try right click and 25716 21:59:40,640 --> 21:59:46,512 paste and I'll create a new line and now 25717 21:59:44,192 --> 21:59:49,832 if we make a request and there's some 25718 21:59:46,512 --> 21:59:53,280 sort of network error that error will go 25719 21:59:49,832 --> 21:59:56,760 down here and we'll run the code inside 25720 21:59:53,280 --> 21:59:58,512 catch so in here let's display a 25721 21:59:56,760 --> 22:00:02,440 message 25722 21:59:58,512 --> 22:00:05,440 console. log the string 25723 22:00:02,440 --> 22:00:05,440 unexpected 25724 22:00:06,280 --> 22:00:11,480 error try again 25725 22:00:12,512 --> 22:00:19,680 later the last step is after we create 25726 22:00:15,920 --> 22:00:24,000 an order we should go to the orders 25727 22:00:19,680 --> 22:00:28,400 page to do that at the bottom 25728 22:00:24,000 --> 22:00:31,192 here we're going to use an object called 25729 22:00:28,400 --> 22:00:33,640 window.location window.location is a 25730 22:00:31,192 --> 22:00:36,232 special object object provided by 25731 22:00:33,640 --> 22:00:39,760 JavaScript and it lets us control the 25732 22:00:36,232 --> 22:00:43,320 URL at the top of the 25733 22:00:39,760 --> 22:00:46,832 browser if we change the location object 25734 22:00:43,320 --> 22:00:50,360 it will change the url at the top so 25735 22:00:46,832 --> 22:00:54,920 let's give this a try we'll go into our 25736 22:00:50,360 --> 22:00:54,920 code and we'll type 25737 22:00:56,000 --> 22:00:59,320 window.location Now location has a 25738 22:00:58,232 --> 22:01:02,080 property 25739 22:00:59,320 --> 22:01:06,160 called href 25740 22:01:02,080 --> 22:01:08,960 this gives us the URL at the top of the 25741 22:01:06,160 --> 22:01:12,040 browser and if we change the href 25742 22:01:08,960 --> 22:01:15,320 property it will change the url at the 25743 22:01:12,040 --> 22:01:19,512 top of the browser so we'll try that in 25744 22:01:15,320 --> 22:01:25,960 our code we're going to change. href and 25745 22:01:19,512 --> 22:01:25,960 make it equal to the string orders. 25746 22:01:27,160 --> 22:01:35,000 HTML so this will replace everything 25747 22:01:30,512 --> 22:01:38,872 after the slash here with orders. 25748 22:01:35,000 --> 22:01:42,000 HTML and this will open the orders. HTML 25749 22:01:38,872 --> 22:01:45,480 file which is at the bottom 25750 22:01:42,000 --> 22:01:51,080 here and this will open the orders 25751 22:01:45,480 --> 22:01:55,040 Page by the way orders. HTML is a file 25752 22:01:51,080 --> 22:01:56,760 path the current file is checkout. HTML 25753 22:01:55,040 --> 22:01:59,480 because this code is running on the 25754 22:01:56,760 --> 22:02:03,000 checkout page so it starts from 25755 22:01:59,480 --> 22:02:07,000 checkout. HTML and then looks for a file 25756 22:02:03,000 --> 22:02:10,280 called orders. HTML beside it which is 25757 22:02:07,000 --> 22:02:12,440 this file here and it loads this file in 25758 22:02:10,280 --> 22:02:12,440 the 25759 22:02:12,512 --> 22:02:19,680 browser so now let's 25760 22:02:16,192 --> 22:02:22,680 save we'll open the project and then 25761 22:02:19,680 --> 22:02:22,680 click place 25762 22:02:23,280 --> 22:02:28,832 order so it should create a new order 25763 22:02:26,040 --> 22:02:30,720 using the backend and it will take us to 25764 22:02:28,832 --> 22:02:33,400 the orders 25765 22:02:30,720 --> 22:02:36,360 page so right now we have a list of 25766 22:02:33,400 --> 22:02:39,120 order objects and that's all the data 25767 22:02:36,360 --> 22:02:42,000 that we need to create this page I'll 25768 22:02:39,120 --> 22:02:45,872 leave generating the HTML and making 25769 22:02:42,000 --> 22:02:45,872 this page interactive as 25770 22:02:46,640 --> 22:02:50,720 exercises so now let's check our tests 25771 22:02:49,120 --> 22:02:54,120 to make sure that everything is still 25772 22:02:50,720 --> 22:02:56,552 working we'll open the tab for our tests 25773 22:02:54,120 --> 22:02:58,832 and I'll refresh just in case and it 25774 22:02:56,552 --> 22:03:03,720 looks good and we'll save our changes 25775 22:02:58,832 --> 22:03:03,720 into git we'll go to the git section 25776 22:03:04,760 --> 22:03:11,192 inside orders. JS we created some 25777 22:03:07,960 --> 22:03:13,872 functions for saving our orders and in 25778 22:03:11,192 --> 22:03:18,232 payment summary. 25779 22:03:13,872 --> 22:03:20,760 JS we made the place order button 25780 22:03:18,232 --> 22:03:24,120 interactive and we use the backend to 25781 22:03:20,760 --> 22:03:26,160 create an order let's create a message 25782 22:03:24,120 --> 22:03:33,160 describing these 25783 22:03:26,160 --> 22:03:35,640 changes create order using the back end 25784 22:03:33,160 --> 22:03:35,640 and 25785 22:03:39,960 --> 22:03:44,872 commit so we're going to learn one more 25786 22:03:42,360 --> 22:03:47,680 thing in this lesson and this is needed 25787 22:03:44,872 --> 22:03:51,680 to create the tracking page we're going 25788 22:03:47,680 --> 22:03:51,680 to learn how to use URL 25789 22:03:52,360 --> 22:03:59,552 parameters so first let's open our 25790 22:03:55,040 --> 22:04:03,000 project and we'll go back to the orders 25791 22:03:59,552 --> 22:04:06,872 page and on this page we have these 25792 22:04:03,000 --> 22:04:10,440 track package links beside the products 25793 22:04:06,872 --> 22:04:14,192 if we click this link it will take us to 25794 22:04:10,440 --> 22:04:17,720 the tracking page at tracking. 25795 22:04:14,192 --> 22:04:20,480 HTML however right now this page is just 25796 22:04:17,720 --> 22:04:24,512 plain HTML there's no 25797 22:04:20,480 --> 22:04:24,512 JavaScript so if we go back to our 25798 22:04:24,680 --> 22:04:31,160 orders and then track a different 25799 22:04:28,960 --> 22:04:33,160 product you'll notice that all of the 25800 22:04:31,160 --> 22:04:35,832 tracking pag pages are the 25801 22:04:33,160 --> 22:04:38,400 same so we need to make each tracking 25802 22:04:35,832 --> 22:04:41,480 page different depending on the order 25803 22:04:38,400 --> 22:04:44,040 and the product so how do we know which 25804 22:04:41,480 --> 22:04:47,192 product to track on this 25805 22:04:44,040 --> 22:04:48,920 page to create this we can use a feature 25806 22:04:47,192 --> 22:04:52,080 called URL 25807 22:04:48,920 --> 22:04:55,872 parameters URL parameters let us save 25808 22:04:52,080 --> 22:04:55,872 data directly in the 25809 22:04:55,920 --> 22:05:03,800 URL to create a URL parameter at the top 25810 22:05:00,920 --> 22:05:10,400 after this URL we're going to type 25811 22:05:03,800 --> 22:05:16,600 question mark and then order capital i d 25812 22:05:10,400 --> 22:05:16,600 equals 1 2 3 for example and press 25813 22:05:16,680 --> 22:05:22,400 enter so the question mark means we're 25814 22:05:19,440 --> 22:05:26,040 adding a URL parameter to this 25815 22:05:22,400 --> 22:05:29,360 URL so a parameter is a property value 25816 22:05:26,040 --> 22:05:31,920 pair the left side is a property and the 25817 22:05:29,360 --> 22:05:36,400 right side is a value 25818 22:05:31,920 --> 22:05:36,400 this is sort of like an object like 25819 22:05:37,080 --> 22:05:45,440 this so using URL parameters we can save 25820 22:05:41,040 --> 22:05:45,440 some data in the URL like 25821 22:05:45,480 --> 22:05:53,320 this and then we can use JavaScript to 25822 22:05:48,832 --> 22:05:55,040 get this data out of the URL to do that 25823 22:05:53,320 --> 22:05:58,640 we'll go back to our 25824 22:05:55,040 --> 22:06:01,000 code and we'll open our files here and 25825 22:05:58,640 --> 22:06:02,872 we'll scroll down to the bottom to the 25826 22:06:01,000 --> 22:06:05,480 tracking 25827 22:06:02,872 --> 22:06:07,040 page and let's scroll all the way down 25828 22:06:05,480 --> 22:06:10,400 to the 25829 22:06:07,040 --> 22:06:12,832 bottom and at the bottom of the body 25830 22:06:10,400 --> 22:06:15,960 let's add a script tag to run some 25831 22:06:12,832 --> 22:06:18,280 JavaScript so we'll type less than 25832 22:06:15,960 --> 22:06:21,232 script greater 25833 22:06:18,280 --> 22:06:24,440 than and to get the parameters out of 25834 22:06:21,232 --> 22:06:26,080 the URL we can use a built-in class 25835 22:06:24,440 --> 22:06:29,640 called 25836 22:06:26,080 --> 22:06:32,000 URL first let's generate an object using 25837 22:06:29,640 --> 22:06:36,440 this class we'll type 25838 22:06:32,000 --> 22:06:39,080 New Capital URL and 25839 22:06:36,440 --> 22:06:41,280 brackets and we'll give it the URL that 25840 22:06:39,080 --> 22:06:46,280 we want to get the parameters 25841 22:06:41,280 --> 22:06:46,280 from so in here we'll type 25842 22:06:47,320 --> 22:06:52,040 window.location.href 25843 22:06:49,960 --> 22:06:55,320 remember that 25844 22:06:52,040 --> 22:06:58,080 window.location.href gets the URL at the 25845 22:06:55,320 --> 22:06:58,080 top of the 25846 22:06:58,120 --> 22:07:02,512 browser and now let's save this object 25847 22:07:00,800 --> 22:07:06,800 in a variable 25848 22:07:02,512 --> 22:07:09,872 at the front we'll create const URL 25849 22:07:06,800 --> 22:07:13,920 equals this 25850 22:07:09,872 --> 22:07:16,552 object so this URL class analyzes the 25851 22:07:13,920 --> 22:07:19,360 URL that we give it and breaks it down 25852 22:07:16,552 --> 22:07:21,320 into different parts one of the parts 25853 22:07:19,360 --> 22:07:23,960 are the URL 25854 22:07:21,320 --> 22:07:26,800 parameters to get the parameters we're 25855 22:07:23,960 --> 22:07:32,640 going to use a property 25856 22:07:26,800 --> 22:07:35,720 url. search capital p perams 25857 22:07:32,640 --> 22:07:37,440 by the way URL parameters are also 25858 22:07:35,720 --> 22:07:39,480 called search 25859 22:07:37,440 --> 22:07:43,040 parameters because they're used for 25860 22:07:39,480 --> 22:07:43,040 features like YouTube 25861 22:07:44,800 --> 22:07:51,280 search so search params will let us get 25862 22:07:48,512 --> 22:07:53,640 the URL parameters up 25863 22:07:51,280 --> 22:07:55,232 here so let's say that we wanted the 25864 22:07:53,640 --> 22:07:57,400 order ID 25865 22:07:55,232 --> 22:08:01,000 parameter inside our 25866 22:07:57,400 --> 22:08:01,872 code search params has a method called 25867 22:08:01,000 --> 22:08:05,512 Dot 25868 22:08:01,872 --> 22:08:08,680 get brackets and this lets us get 25869 22:08:05,512 --> 22:08:11,720 individual parameters so inside here 25870 22:08:08,680 --> 22:08:17,280 we'll give it a string and we'll get the 25871 22:08:11,720 --> 22:08:22,280 order capital i d parameter out of the 25872 22:08:17,280 --> 22:08:25,872 URL so let's console.log this 25873 22:08:22,280 --> 22:08:28,480 console.log and 25874 22:08:25,872 --> 22:08:32,192 brackets if we 25875 22:08:28,480 --> 22:08:34,720 save and then open our project and go to 25876 22:08:32,192 --> 22:08:38,192 the tracking page and then open the 25877 22:08:34,720 --> 22:08:40,600 console right click 25878 22:08:38,192 --> 22:08:43,280 inspect the 25879 22:08:40,600 --> 22:08:47,512 console you'll see that it gave us the 25880 22:08:43,280 --> 22:08:51,360 order ID that we saved up 25881 22:08:47,512 --> 22:08:55,120 here so using that code we can get URL 25882 22:08:51,360 --> 22:08:58,920 parameters out of the URL at the top and 25883 22:08:55,120 --> 22:08:58,920 then use them to create this 25884 22:08:59,680 --> 22:09:05,920 page we can also say save multiple 25885 22:09:02,360 --> 22:09:09,400 parameters in the URL to save another 25886 22:09:05,920 --> 22:09:12,080 parameter at the end we can just type 25887 22:09:09,400 --> 22:09:17,000 and and now we can give it another 25888 22:09:12,080 --> 22:09:19,512 property value pair like product Capital 25889 22:09:17,000 --> 22:09:23,120 ID equal 25890 22:09:19,512 --> 22:09:23,120 456 and press 25891 22:09:23,160 --> 22:09:30,120 enter and now inside our code we can get 25892 22:09:26,232 --> 22:09:30,120 this second parameter 25893 22:09:31,360 --> 22:09:38,480 so inside here we're going to make a 25894 22:09:33,800 --> 22:09:43,640 copy of this line select it right click 25895 22:09:38,480 --> 22:09:46,192 copy and then down here right click 25896 22:09:43,640 --> 22:09:47,120 paste and this time we're going to get 25897 22:09:46,192 --> 22:09:53,680 the 25898 22:09:47,120 --> 22:09:53,680 parameter product Capital ID and 25899 22:09:54,040 --> 22:10:00,832 save if we open the page and open the 25900 22:09:57,832 --> 22:10:00,832 console 25901 22:10:01,552 --> 22:10:09,320 we got the order ID as well as the 25902 22:10:05,280 --> 22:10:09,320 product ID out of the 25903 22:10:09,872 --> 22:10:16,720 URL so URL parameters lets us save 25904 22:10:13,320 --> 22:10:19,000 different data in each 25905 22:10:16,720 --> 22:10:21,680 URL and we can actually go back and 25906 22:10:19,000 --> 22:10:26,040 forth between them so if we click the 25907 22:10:21,680 --> 22:10:28,320 back button it will go to this URL and 25908 22:10:26,040 --> 22:10:32,280 this URL has different data or 25909 22:10:28,320 --> 22:10:34,760 parameters saved inside if we click 25910 22:10:32,280 --> 22:10:37,512 forward this URL has different 25911 22:10:34,760 --> 22:10:39,920 parameters or data saved 25912 22:10:37,512 --> 22:10:42,600 inside and this is how YouTube search 25913 22:10:39,920 --> 22:10:46,160 works if we search for something our 25914 22:10:42,600 --> 22:10:48,600 search is saved as a URL parameter if we 25915 22:10:46,160 --> 22:10:51,920 search for something else our search is 25916 22:10:48,600 --> 22:10:55,640 also saved as a URL parameter and we can 25917 22:10:51,920 --> 22:10:55,640 go back and forth between the 25918 22:10:59,680 --> 22:11:05,040 two so this is actually all the data 25919 22:11:02,400 --> 22:11:07,720 that we need to create the tracking page 25920 22:11:05,040 --> 22:11:09,600 we need to know which order it is and 25921 22:11:07,720 --> 22:11:12,360 which product it 25922 22:11:09,600 --> 22:11:15,512 is now there's one more thing to know to 25923 22:11:12,360 --> 22:11:18,040 create this page we'll go back to our 25924 22:11:15,512 --> 22:11:20,440 code and we're going to open the orders 25925 22:11:18,040 --> 22:11:23,232 page at orders. 25926 22:11:20,440 --> 22:11:26,040 HTML and we're going to find the track 25927 22:11:23,232 --> 22:11:29,120 package button using control F or 25928 22:11:26,040 --> 22:11:33,120 command F and we'll type the text in 25929 22:11:29,120 --> 22:11:33,120 that button track 25930 22:11:33,600 --> 22:11:39,400 package so notice that these buttons are 25931 22:11:36,600 --> 22:11:40,920 just a link to the tracking page or 25932 22:11:39,400 --> 22:11:43,920 tracking. 25933 22:11:40,920 --> 22:11:46,832 HTML so when you're generating this 25934 22:11:43,920 --> 22:11:49,872 HTML make sure that you add URL 25935 22:11:46,832 --> 22:11:54,192 parameters after this tracking page 25936 22:11:49,872 --> 22:11:57,640 URL using question mark the order 25937 22:11:54,192 --> 22:12:01,680 Capital ID equals and then you 25938 22:11:57,640 --> 22:12:06,280 substitute the order ID like 1 2 3 25939 22:12:01,680 --> 22:12:11,040 and then and also attach the product ID 25940 22:12:06,280 --> 22:12:14,680 product Capital ID equals 25941 22:12:11,040 --> 22:12:18,120 456 so when generating this HTML make 25942 22:12:14,680 --> 22:12:22,160 sure you substitute the order ID and 25943 22:12:18,120 --> 22:12:24,640 product ID into this URL so that when it 25944 22:12:22,160 --> 22:12:29,760 opens the tracking page you know which 25945 22:12:24,640 --> 22:12:29,760 order and which product to track 25946 22:12:31,320 --> 22:12:37,280 I'll leave generating the HTML and 25947 22:12:33,800 --> 22:12:40,720 making the tracking page interactive as 25948 22:12:37,280 --> 22:12:44,160 exercises so now let's save this 25949 22:12:40,720 --> 22:12:47,800 file and let's check our tests so we'll 25950 22:12:44,160 --> 22:12:49,872 open the browser and open the test Tab 25951 22:12:47,800 --> 22:12:53,120 and everything is still working and 25952 22:12:49,872 --> 22:12:54,440 we'll save these changes into git open 25953 22:12:53,120 --> 22:12:58,320 the git 25954 22:12:54,440 --> 22:12:58,320 section in orders. 25955 22:12:59,760 --> 22:13:07,320 HTML we added an example of adding URL 25956 22:13:03,440 --> 22:13:11,232 parameters to the tracking page 25957 22:13:07,320 --> 22:13:14,160 Link in tracking. HTML we added an 25958 22:13:11,232 --> 22:13:16,640 example of getting the parameters out of 25959 22:13:14,160 --> 22:13:19,512 the URL at the 25960 22:13:16,640 --> 22:13:20,400 top let's create a message describing 25961 22:13:19,512 --> 22:13:25,040 these 25962 22:13:20,400 --> 22:13:26,680 changes add URL 25963 22:13:25,040 --> 22:13:31,120 parameters 25964 22:13:26,680 --> 22:13:31,120 example and commit 25965 22:13:33,920 --> 22:13:39,280 and that's the end of this lesson in 25966 22:13:36,400 --> 22:13:42,000 this lesson we learned what a vend is 25967 22:13:39,280 --> 22:13:46,120 and how to make HTTP requests to the 25968 22:13:42,000 --> 22:13:47,800 back end we learned how to use XML HTTP 25969 22:13:46,120 --> 22:13:49,920 request and 25970 22:13:47,800 --> 22:13:52,800 fetch we learned how to handle 25971 22:13:49,920 --> 22:13:56,120 asynchronous code using callbacks 25972 22:13:52,800 --> 22:13:59,280 promises and async await we learned how 25973 22:13:56,120 --> 22:14:02,360 to test asynchronous code we learned 25974 22:13:59,280 --> 22:14:06,080 about error handling for unexpected 25975 22:14:02,360 --> 22:14:09,440 errors we use the backend in our project 25976 22:14:06,080 --> 22:14:11,400 and we learn URL 25977 22:14:09,440 --> 22:14:14,160 parameters before we look at the 25978 22:14:11,400 --> 22:14:17,600 exercises you might be wondering what's 25979 22:14:14,160 --> 22:14:20,680 the next step after this course instead 25980 22:14:17,600 --> 22:14:24,232 of just using a backend the next step is 25981 22:14:20,680 --> 22:14:26,320 to learn how to create our own backend 25982 22:14:24,232 --> 22:14:28,680 to do that you need to learn the command 25983 22:14:26,320 --> 22:14:31,920 line and after that you'll learn a 25984 22:14:28,680 --> 22:14:34,232 technology called nodejs which lets us 25985 22:14:31,920 --> 22:14:37,640 create our own 25986 22:14:34,232 --> 22:14:39,872 backend also after this course you can 25987 22:14:37,640 --> 22:14:44,400 learn how to put a website on the 25988 22:14:39,872 --> 22:14:44,400 internet using this video right 25989 22:14:45,160 --> 22:14:49,680 here here are some exercises that'll 25990 22:14:47,800 --> 22:14:52,000 help you practice what we learned in 25991 22:14:49,680 --> 22:14:52,000 this 25992 22:14:59,920 --> 22:15:03,920 lesson e 25993 22:15:38,960 --> 22:15:43,080 thanks for watching here are some other 25994 22:15:41,040 --> 22:15:45,232 videos and courses that you might be 25995 22:15:43,080 --> 22:15:47,512 interested in if you want to support 25996 22:15:45,232 --> 22:15:50,160 this channel you can click like 25997 22:15:47,512 --> 22:15:52,720 subscribe and share this video with 25998 22:15:50,160 --> 22:15:56,120 others thanks again and I'll see you in 25999 22:15:52,720 --> 22:15:56,120 the next one1908411

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.