YouTube2Text

YouTube transcript (EerdGm-ehJQ) — Transcript

165,390 words · 25,999 segments · language en · Watch on YouTube

Full transcript

  1. 0:00welcome to the complete JavaScript
  2. 0:02course in this course we're going to
  3. 0:04learn how to build complex websites
  4. 0:06using JavaScript from a beginner to a
  5. 0:09professional level and by the end of
  6. 0:11this course we're going to build
  7. 0:14amazon.com so this is an interactive
  8. 0:17multi-page website where we can add
  9. 0:20products to our cart create an order and
  10. 0:24even track our
  11. 0:26order along the way we're also going to
  12. 0:29build some smaller projects like a rock
  13. 0:32paper scissors game a to-do list and a
  14. 0:36calculator now you don't need any
  15. 0:38previous coding or technical experience
  16. 0:41to follow along this course will cover
  17. 0:43everything that you need to learn
  18. 0:45JavaScript we're going to start from the
  19. 0:48very basics of JavaScript and we're
  20. 0:50going to build our way up step by step
  21. 0:53we're going to learn the major features
  22. 0:54of JavaScript and also how to use
  23. 0:57JavaScript together with other
  24. 0:59Technologies like HTML and
  25. 1:02CSS we're also going to learn more
  26. 1:05advanced features like objectoriented
  27. 1:08programming backend callbacks promises
  28. 1:12and async await and we'll use these
  29. 1:15features together to build a complex
  30. 1:18project which is the Amazon
  31. 1:20Project this will let you practice what
  32. 1:23you learn and deepen your
  33. 1:26understanding you can find the different
  34. 1:28lessons of this course course below the
  35. 1:30video
  36. 1:32here and
  37. 1:34here if you need to you can adjust the
  38. 1:37speed of the video
  39. 1:40here and after each lesson I'm going to
  40. 1:43give you a bunch of exercises that you
  41. 1:45can do on your own to practice the
  42. 1:47skills that we learned in total this
  43. 1:50course will have over
  44. 1:52250 exercises so with that said let's
  45. 1:56get started
  46. 2:04before we begin if something in this
  47. 2:06course is not working properly make sure
  48. 2:08to check the first comment below the
  49. 2:10video for a list of
  50. 2:18solutions I'm going to be doing this
  51. 2:20course on Windows but the steps are the
  52. 2:23same if you're using a Mac we're going
  53. 2:26to start from the very basics of
  54. 2:28JavaScript and build our way up step by
  55. 2:31step first of all what is Javascript
  56. 2:35JavaScript is a technology that we use
  57. 2:37to create websites a website is like
  58. 2:41youtube.com or
  59. 2:43amazon.com it's how we access YouTube
  60. 2:46and Amazon on our computer if we were
  61. 2:49using a phone we usually use an app but
  62. 2:53on our computer we use a
  63. 2:56website so there's actually three
  64. 2:58technologies that we use to create
  65. 3:00websites HTML CSS and JavaScript each of
  66. 3:06these Technologies has a specific
  67. 3:08purpose when creating a website HTML
  68. 3:12creates the content of a website it
  69. 3:14creates all the buttons the text and the
  70. 3:18images CSS changes the appearance of the
  71. 3:21website by adding CSS we can make our
  72. 3:25website look nice and finally JavaScript
  73. 3:28makes our website
  74. 3:30interactive so if we only have HTML and
  75. 3:34CSS and we click one of these buttons
  76. 3:37our website doesn't do anything but once
  77. 3:40we add JavaScript we can make our
  78. 3:42website do something when we click a
  79. 3:44button such as add a product to our cart
  80. 3:48or create an
  81. 3:50order so this course is focused on
  82. 3:54JavaScript if you want to learn HTML and
  83. 3:57CSS in more detail I have a full whole
  84. 4:00course just like this one that you can
  85. 4:02find in the video
  86. 4:03description let's get started with
  87. 4:06JavaScript the first thing we need to do
  88. 4:09is to install a piece of software called
  89. 4:11a web browser a web browser lets us view
  90. 4:15websites that we create in this course
  91. 4:17the most popular web browser for web
  92. 4:19development is Google Chrome so I
  93. 4:22recommend using that if you don't have
  94. 4:25Google Chrome you can open the default
  95. 4:27web browser on your computer for windows
  96. 4:30it's Microsoft Edge and for Mac it's
  97. 4:33Safari type in google.com at the top and
  98. 4:37press enter search for Google
  99. 4:41Chrome click the result and follow the
  100. 4:45instructions to download and install
  101. 4:48once we've installed Chrome we're ready
  102. 4:50to
  103. 4:51begin we're going to start by learning
  104. 4:54what exactly is Javascript and how does
  105. 4:57it work the easiest way to understand
  106. 5:00JavaScript is we're basically giving
  107. 5:02instructions to a computer and then the
  108. 5:05computer follows our
  109. 5:07instructions I find the best way to
  110. 5:09learn this is to jump in and give it a
  111. 5:11try so first let's open Google
  112. 5:17Chrome next we're going to click at the
  113. 5:20top
  114. 5:21here and type
  115. 5:24super
  116. 5:25simple
  117. 5:27dodev slj
  118. 5:30s- Basics and press
  119. 5:35enter and now we'll learn how to give
  120. 5:37our computer some instructions to follow
  121. 5:40to do that we're going to go to a blank
  122. 5:43area of the web page we're going to
  123. 5:46rightclick click
  124. 5:49inspect and then click the console
  125. 5:53tab now if your console doesn't show up
  126. 5:56at the bottom like this you can click
  127. 5:58these three dots here here and then
  128. 6:01select this
  129. 6:03icon okay so inside the console we can
  130. 6:07give some instructions for our computer
  131. 6:10let's learn our first instruction we're
  132. 6:13going to type here and we're going to
  133. 6:15type
  134. 6:17alert Open Bracket single quote hello
  135. 6:22single quote close bracket and
  136. 6:26semicolon make sure to type this out
  137. 6:28exactly as I have it here in JavaScript
  138. 6:31if you don't type things out correctly
  139. 6:33it won't work so this is an instruction
  140. 6:37to create a popup with the text hello
  141. 6:40inside to get the computer to follow our
  142. 6:43instruction we can just press
  143. 6:46enter and as you can see the computer
  144. 6:48does exactly what we tell it to do it
  145. 6:51creates a popup with a text hello
  146. 6:55inside now let's press
  147. 6:57okay and let's give another instruction
  148. 7:00to the computer so we're going to type
  149. 7:03here again and we're going to type alert
  150. 7:07again Open Bracket single quote but this
  151. 7:11time we're going to type a different
  152. 7:13message in the
  153. 7:14popup good job and single quote close
  154. 7:20bracket and
  155. 7:22semicolon so this is an instruction to
  156. 7:24create a popup again but this time with
  157. 7:27a text good job in inside the popup if
  158. 7:31we press
  159. 7:32enter the computer does exactly what we
  160. 7:35tell it to do and it creates a popup
  161. 7:37with the text good job inside and that's
  162. 7:41basically the main idea behind
  163. 7:43JavaScript we're giving instructions to
  164. 7:46a computer and then the computer follows
  165. 7:48our
  166. 7:49instructions so note that we can open
  167. 7:52and use this console on any website
  168. 7:55we're just using superimple dodev as an
  169. 7:58example
  170. 7:59let's press
  171. 8:01okay and we're going to learn some
  172. 8:03terminology or
  173. 8:07naming the instructions we give to our
  174. 8:10computer these instructions are called
  175. 8:13code when we press enter and the
  176. 8:15computer follows our instructions this
  177. 8:18is called running the code now there are
  178. 8:21many different languages we can use to
  179. 8:23write code called programming languages
  180. 8:27it's similar to how we have many human
  181. 8:29Lang languages that we can use to
  182. 8:31communicate some examples of programming
  183. 8:33languages are JavaScript Python and Java
  184. 8:38so here we're writing our code using the
  185. 8:41JavaScript programming language so we
  186. 8:44call this JavaScript
  187. 8:46code now that we understand the basic
  188. 8:49terminology let's look at some more
  189. 8:51examples of what we can do with
  190. 8:53JavaScript code another thing we can do
  191. 8:56is
  192. 8:57math let's go back to the console and
  193. 9:00type here and we're going to type 2 + 2
  194. 9:07so this is an instruction or code that
  195. 9:10tells the computer to do some math to
  196. 9:13calculate 2 + 2 when we press
  197. 9:17enter the computer follows our
  198. 9:19instruction or runs this code calculates
  199. 9:222+ 2 and gives us the result which is
  200. 9:26four let's do another example we're
  201. 9:29going to type 10 minus 3 and press enter
  202. 9:35to run this code again the computer does
  203. 9:38this math calculates the result and
  204. 9:41gives us seven so as you can see
  205. 9:44JavaScript can do many different things
  206. 9:47like create popups and do math let's
  207. 9:51look at one more example of what
  208. 9:52JavaScript can do we're going to go to
  209. 9:55the bottom and type out this code
  210. 10:02document.body do inner Capital
  211. 10:08HTML equals single quilt hello single
  212. 10:13quilt and
  213. 10:15semicolon make sure to write this out
  214. 10:17exactly as I have it here with the dots
  215. 10:20and the capital letters because
  216. 10:22JavaScript is case
  217. 10:24sensitive let's press enter to run this
  218. 10:27code what this code does is that it
  219. 10:30removes everything on the page and
  220. 10:32replaces it with the text hello in this
  221. 10:36example we use JavaScript to modify the
  222. 10:39web page modifying the web page is one
  223. 10:42of the most important features of
  224. 10:44JavaScript and we're going to be using
  225. 10:46this feature throughout this course and
  226. 10:49that's the basics of JavaScript we're
  227. 10:52giving instructions to a computer these
  228. 10:55instructions are called code and then
  229. 10:57the computer follows our instru
  230. 10:59instuctions this is called running the
  231. 11:02code and also don't worry about
  232. 11:04memorizing all this code you can find a
  233. 11:07copy of all the code that we write in
  234. 11:09this course in the video
  235. 11:14description now that we understand the
  236. 11:17basics of JavaScript let's learn more
  237. 11:19about the JavaScript language
  238. 11:22itself if we take a look at this code
  239. 11:26alert and hello you might be wondering
  240. 11:29ing what the brackets and the quote mean
  241. 11:33all of these characters have a special
  242. 11:35meaning in the language this is
  243. 11:37something called
  244. 11:38syntax syntax are the rules that we have
  245. 11:41to follow when using a programming
  246. 11:43language like JavaScript this is sort of
  247. 11:46like in English we also have rules that
  248. 11:49we follow called grammar the difference
  249. 11:52is that in English we don't have to
  250. 11:54follow the rules of grammar perfectly
  251. 11:57people can still understand us but in
  252. 12:00programming we have to follow the rules
  253. 12:02of syntax exactly otherwise the computer
  254. 12:05won't understand our code for example if
  255. 12:09we scroll down and we type this code
  256. 12:13alert Open Bracket quote hello quote
  257. 12:19close bracket and semicolon but this
  258. 12:22time we remove the first bracket and
  259. 12:25press
  260. 12:27enter it will give us a syntax error
  261. 12:30we're not following the rules of syntax
  262. 12:33so the computer doesn't understand this
  263. 12:36code now JavaScript has a lot of syntax
  264. 12:39rules and we can't learn them all at
  265. 12:41once right now instead we're going to
  266. 12:44learn the syntax rules one by one as we
  267. 12:47go through the course for now we'll just
  268. 12:50learn enough syntax to do the exercises
  269. 12:53at the end of this lesson what we need
  270. 12:56to know is that if we scroll up
  271. 13:00this part of the code tells a computer
  272. 13:02to create a popup the text between these
  273. 13:06quotes is the text inside the popup if
  274. 13:09we change this text we can change the
  275. 13:12text inside the popup same thing for
  276. 13:15this code down
  277. 13:17here if we change the text between the
  278. 13:20quotes it will change the text that
  279. 13:22appears on the page and that's all we
  280. 13:24need to know about JavaScript Syntax for
  281. 13:27now we'll learn exact ly what the other
  282. 13:30characters like the brackets quotes and
  283. 13:32semicolon mean later on in this course
  284. 13:36so in this lesson we learned that
  285. 13:38JavaScript is basically giving
  286. 13:40instructions to a computer we wrote our
  287. 13:44first few lines of JavaScript code we
  288. 13:47learned how to run our code using the
  289. 13:49console in our web browser and we
  290. 13:52learned about syntax which are the rules
  291. 13:55that we have to follow when writing our
  292. 13:57code
  293. 14:01thanks for watching this course so far
  294. 14:03if you want to support this channel you
  295. 14:05can check out the premium version of my
  296. 14:07courses just like this one where you can
  297. 14:09get a certificate of completion and a
  298. 14:11Better Learning platform if you're
  299. 14:14interested in extra videos from me more
  300. 14:16in-depth videos and behind the scenes
  301. 14:19content you can check out these links
  302. 14:22here you can also support the channel by
  303. 14:24liking the video subscribing and sharing
  304. 14:27this video with others
  305. 14:29thanks again and let's continue with the
  306. 14:33course here are some exercises you can
  307. 14:35try on your own to practice what we
  308. 14:38learned in this
  309. 14:40lesson you can find the solutions to
  310. 14:42these exercises in the video
  311. 14:52description now that we understand the
  312. 14:54basics of JavaScript we're going to
  313. 14:56learn about each feature of the language
  314. 14:59language in this lesson we're going to
  315. 15:01focus on numbers and math first we can
  316. 15:05close the console for
  317. 15:07now so I've actually prepared a project
  318. 15:10that we can use for this lesson to open
  319. 15:13this project we're going to go to our
  320. 15:15browser click up here and type
  321. 15:20super simple
  322. 15:23dodev SL
  323. 15:26projects SL Amazon
  324. 15:29and press
  325. 15:31enter so this is actually the final
  326. 15:34project we're going to create in this
  327. 15:36course this is an e-commerce website
  328. 15:40similar to Amazon where we can add
  329. 15:42products to our cart and then view our
  330. 15:45cart by clicking the top
  331. 15:48right and on this page we can see the
  332. 15:51products in our cart modify our cart and
  333. 15:55create an
  334. 15:56order for this lesson we're going to
  335. 15:59focus on this right section which
  336. 16:01calculates the cost of our cart
  337. 16:04including the cost of the products
  338. 16:07shipping and
  339. 16:09taxes this section is a great way to
  340. 16:12learn numbers and math in
  341. 16:15JavaScript first let's open the console
  342. 16:18again so we can write JavaScript code
  343. 16:21we're going to find a blank area of the
  344. 16:23web page rightclick click
  345. 16:27inspect and then click the
  346. 16:31console so we already saw some examples
  347. 16:34of math in the previous lesson for
  348. 16:37example we wrote the code 2 + 2 if we
  349. 16:43press
  350. 16:44enter the computer adds these two
  351. 16:47numbers and results in
  352. 16:49four we also did 10 minus 3 if we press
  353. 16:56enter the computer subtracts these two
  354. 16:59numbers and gives us
  355. 17:01seven in addition to adding and
  356. 17:04subtracting we can also multiply and
  357. 17:07divide to multiply we're going to type
  358. 17:1110 star 3 and press
  359. 17:16enter so star is the character for
  360. 17:19multiplying to divide we're going to
  361. 17:22type
  362. 17:2310
  363. 17:25slash2 and press enter
  364. 17:29so forward slash is the character for
  365. 17:32dividing now let's learn the syntax
  366. 17:35rules for numbers and math the Syntax
  367. 17:39for math is pretty straightforward in
  368. 17:41JavaScript we just write it out like
  369. 17:44normal math we can also do math with
  370. 17:47more than two numbers for example we can
  371. 17:51add two plus 2 and then plus another two
  372. 17:58if if we press enter the computer will
  373. 18:01add these three numbers together and
  374. 18:04result in six and finally JavaScript can
  375. 18:08handle decimal numbers like
  376. 18:122.2 plus
  377. 18:152.2 if we press enter it will calculate
  378. 18:19these decimal numbers and result in
  379. 18:234.4 now let's do some practical examples
  380. 18:27we're going to calculate the number
  381. 18:28numers here in our final project but
  382. 18:31first let's set up our cart so we have
  383. 18:34the same products in our cart so make
  384. 18:37sure in your cart you have two socks
  385. 18:42with free shipping and you have one
  386. 18:45basketball with
  387. 18:48$4.99 of shipping and nothing else in
  388. 18:51your cart if you need to you can modify
  389. 18:55your cart by clicking update or delete
  390. 18:58or or by going to the homepage by
  391. 19:00clicking in the top left and adding any
  392. 19:03products that you're
  393. 19:05missing once you have the same products
  394. 19:08as me in your cart and you have the same
  395. 19:11numbers on the right we're ready to
  396. 19:14begin let's start by calculating this
  397. 19:17number up here this is the cost of the
  398. 19:20products before shipping and taxes to
  399. 19:24calculate this number using JavaScript
  400. 19:27we're just going to add the cost of the
  401. 19:29products so in our console we're going
  402. 19:32to add the cost of the socks which is
  403. 19:35$10 and
  404. 19:3890 we have two of them so we're going to
  405. 19:42multiply this by
  406. 19:44two and then we're going to add the cost
  407. 19:47of the basketball so plus
  408. 19:53$20.95 if we press
  409. 19:55enter the computer will do this math
  410. 19:58calculation
  411. 19:59and give us this number and if we scroll
  412. 20:02to the top of our
  413. 20:04project you'll see that the number that
  414. 20:06we got matches what we have on the page
  415. 20:10so that's how we use JavaScript to
  416. 20:12calculate this number here let's do
  417. 20:15another example we're going to calculate
  418. 20:18this third number which is the cost of
  419. 20:21the products plus shipping feel free to
  420. 20:25pause the video if you want to try it
  421. 20:26yourself first otherwise we'll do it
  422. 20:30together let's go back to our
  423. 20:32console and we're going to add the cost
  424. 20:35of the products again which is
  425. 20:39$10.90 * 2 plus
  426. 20:46$20.95 but this time we're going to add
  427. 20:49the cost of shipping which is
  428. 20:53$4.99 so
  429. 20:564.99 and press and
  430. 20:59enter the computer will do this
  431. 21:02calculation and give us this number
  432. 21:05which matches what we see in the final
  433. 21:08project next we're going to learn a very
  434. 21:11important feature of math called the
  435. 21:14order of
  436. 21:15operations adding subtracting
  437. 21:18multiplying and dividing these are
  438. 21:21called operations and the characters in
  439. 21:24the middle are called
  440. 21:26operators now there's a certain order
  441. 21:29that we do these operations for example
  442. 21:32if we go back to our console and type
  443. 21:35the calculation 1 + 1 * 3 and press
  444. 21:43enter notice that it multiplies 1 * 3
  445. 21:47first which equals 3 and then adds 1
  446. 21:52which equals
  447. 21:534 notice that it does not add 1 + 1
  448. 21:58first
  449. 21:59which equal 2 and then 2 * 3 =
  450. 22:046 the way math normally works is that
  451. 22:07multiply and divide are done first add
  452. 22:11and subtract are done after this is
  453. 22:14called the order of operations and
  454. 22:17JavaScript follows the same rules
  455. 22:20multiply and divide are done first add
  456. 22:24and subtract are done after the order of
  457. 22:27operations is also called operator
  458. 22:30precedence in
  459. 22:32JavaScript one thing to note is that
  460. 22:35multiply and divide have the same
  461. 22:38priority so if we have both in a
  462. 22:40calculation it will calculate from left
  463. 22:43to right add and subtract also have the
  464. 22:47same priority if we have both in the
  465. 22:50calculation it will also calculate from
  466. 22:53left to right another feature of the
  467. 22:56order of operations is that we can use
  468. 22:59brackets to control which part of the
  469. 23:02calculation gets done first for example
  470. 23:06let's type out this calculation again 1
  471. 23:10+ 1 * 3 but this time we're going to add
  472. 23:17an Open
  473. 23:18Bracket and a Clos bracket around 1 + 1
  474. 23:24if we press
  475. 23:26enter now the calculation inside the
  476. 23:29brackets will be done first so it will
  477. 23:32add 1 + 1 = 2 and then 2 * 3 = 6 so when
  478. 23:40we use brackets JavaScript will always
  479. 23:43do the calculation inside the brackets
  480. 23:46first and then do the rest of the
  481. 23:49calculation brackets have the highest
  482. 23:51priority in the order of
  483. 23:54operations now let's learn the syntax
  484. 23:57rules for brackets
  485. 23:59so we just have to have a matching Open
  486. 24:02Bracket and close bracket so something
  487. 24:05like this doesn't work because we have
  488. 24:08two Open brackets and no Clos bracket we
  489. 24:12also need to have a complete calculation
  490. 24:15in the brackets so this also doesn't
  491. 24:18work because 1 + 1 multiply is not a
  492. 24:23complete
  493. 24:25calculation now let's do some practical
  494. 24:27examples using the order of
  495. 24:30operations first we're going to do some
  496. 24:33more setup in the final project so we're
  497. 24:36actually going to remove all the
  498. 24:38products in our
  499. 24:40cart and then once our cart is empty
  500. 24:43we'll go to the homepage by clicking the
  501. 24:46top
  502. 24:47left and this time we're going to add
  503. 24:50some new products so we'll add one
  504. 24:55basketball and two T-shirts
  505. 25:00now let's go back to our cart by
  506. 25:01clicking the top
  507. 25:05right we'll scroll down and we're going
  508. 25:08to calculate the numbers in this section
  509. 25:11again let's calculate this number at the
  510. 25:14top which is the cost of the products
  511. 25:17we'll go back to our
  512. 25:19console and add the cost of the
  513. 25:22basketball which is
  514. 25:26$20.95 Plus the cost of the
  515. 25:30t-shirts which is
  516. 25:34$799 and we have two of them so we'll
  517. 25:38multiply this by two if we press
  518. 25:42enter this will give us the number that
  519. 25:45we see in the final project here notice
  520. 25:49that in this calculation we're taking
  521. 25:52advantage of the order of
  522. 25:54operations even though multiply is
  523. 25:57written last
  524. 25:58we still multiply these two numbers
  525. 26:01first so we calculate the cost of two
  526. 26:04T-shirts first and then we add the cost
  527. 26:08of the basketball
  528. 26:10after okay now let's try something a
  529. 26:13little more difficult we're going to
  530. 26:15learn how to calculate this 10% tax here
  531. 26:19so how do we calculate 10% of something
  532. 26:22unfortunately we can't just multiply by
  533. 26:2510% in JavaScript instead
  534. 26:28we have to calculate the percent
  535. 26:30manually so how do we do that 1% is
  536. 26:35actually equal to 1 / 100 that's what
  537. 26:40percent means so 10% is equal to 10 /
  538. 26:46100 which equals
  539. 26:480.1 so to calculate 10% manually we just
  540. 26:53multiply this number by
  541. 26:550.1 so we'll type 30 3
  542. 27:006.93 multiplied by 10% or
  543. 27:050.1 if we press
  544. 27:07enter it will give us a number that is
  545. 27:10close to what we have in the final
  546. 27:12project it's the same amount of money
  547. 27:15but our number is more accurate than we
  548. 27:18need so later in this lesson we'll learn
  549. 27:21how to round this number down to exactly
  550. 27:24what we have on the web page for now
  551. 27:28let's do some more practice with the
  552. 27:30order of
  553. 27:31operations let's calculate the tax again
  554. 27:35except instead of using the final number
  555. 27:3836.9 3 Let's calculate this number
  556. 27:41individually like here so at the bottom
  557. 27:45we're going to add the cost of the
  558. 27:48basketball
  559. 27:5720.95% 2 and then multiply this by the
  560. 28:01tax percent time
  561. 28:050.1 and press
  562. 28:08enter unfortunately this number doesn't
  563. 28:11match what we got up here and that's
  564. 28:14because if we look at this calculation
  565. 28:17according to the order of operations
  566. 28:20multiply is done first so it will
  567. 28:23multiply these three numbers first and
  568. 28:25then add 20.95%
  569. 28:29after however this is not what we want
  570. 28:33we actually want to calculate this
  571. 28:35number first this is the cost of the
  572. 28:38products so how do we get the computer
  573. 28:40to calculate this first and then
  574. 28:43multiply by the tax percent
  575. 28:460.1 to do this we can use brackets to
  576. 28:49control which part of the calculation
  577. 28:52gets done first so let's create this
  578. 28:55calculation again 20
  579. 28:59.95 +
  580. 29:027.99 * 2 but this time we're going to
  581. 29:07add brackets around this part of the
  582. 29:10calculation to make sure that this gets
  583. 29:13calculated first and then at the end
  584. 29:17we'll multiply it by the tax percent
  585. 29:210.1 and now if we press
  586. 29:23enter we will get the same number that
  587. 29:26we had before
  588. 29:29so that's how we use math and the Order
  589. 29:31of Operations to calculate the numbers
  590. 29:34in this section of the final project so
  591. 29:38don't worry you don't have to be good at
  592. 29:40math to learn JavaScript this is just to
  593. 29:42show you how math Works in
  594. 29:45JavaScript in the rest of this lesson
  595. 29:48we're going to learn some more details
  596. 29:50about numbers and math the first detail
  597. 29:53we're going to learn is a weird behavior
  598. 29:56of math in JavaScript
  599. 29:58first we'll need to learn some
  600. 30:00terminology in programming whole numbers
  601. 30:04like 2 3 and 4 are called integers
  602. 30:08decimal numbers like
  603. 30:112.2 and 2.5 are called floating Point
  604. 30:15numbers or floats and unfortunately
  605. 30:19computers have a problem working with
  606. 30:22floats for example if we go back to our
  607. 30:25console and type the calculation
  608. 30:300.1 plus
  609. 30:330.2 and press
  610. 30:35enter it will give us this weird number
  611. 30:38here that's close to 0.3 but a little
  612. 30:42bit
  613. 30:43inaccurate so this problem is not
  614. 30:45specific to JavaScript many programming
  615. 30:48languages also have this problem the
  616. 30:51reason this happens is because of how
  617. 30:53computers store numbers I'll give a
  618. 30:56simplified explan
  619. 30:59computers can only store zeros and ones
  620. 31:02while humans can count from 0 to 9 so
  621. 31:06there's sort of a mismatch between our
  622. 31:08number systems for integers this is not
  623. 31:12a problem the computer can just use a
  624. 31:14bunch of zeros and ones to store it
  625. 31:18however for decimal numbers or floats
  626. 31:22sometimes the computer can't store the
  627. 31:24number properly for example here's how
  628. 31:27the computer computer tries to store
  629. 31:290.2 using only zeros and ones at the top
  630. 31:34is what the computer stores at the
  631. 31:37bottom is what this is equal to in human
  632. 31:40numbers we can see that the computer
  633. 31:42gets closer and closer to 0.2 but never
  634. 31:46actually reaches it and this is why we
  635. 31:49get these inaccuracies when calculating
  636. 31:52with floats because computers can't
  637. 31:55store some floats properly note that
  638. 31:58this doesn't happen with all floats for
  639. 32:01example computers can store
  640. 32:050.25 without any problems so these
  641. 32:08inaccuracies only happen with some
  642. 32:10floats but not all of them however when
  643. 32:14calculating money we definitely want to
  644. 32:17avoid any inaccuracies so let's learn
  645. 32:20how to do that first we're going to set
  646. 32:23up our cart again so make sure in your
  647. 32:26cart you have one basketball with free
  648. 32:29shipping and this time you have one
  649. 32:32t-shirt so we're going to click
  650. 32:35update change this to one and click
  651. 32:40save and this t-shirt also has free
  652. 32:44shipping now let's scroll up and we're
  653. 32:46going to calculate this number at the
  654. 32:49top again which is the cost of the
  655. 32:51products let's go into our
  656. 32:54console and we're going to add the cost
  657. 32:56of the basketball
  658. 33:00$20.95 plus the cost of the
  659. 33:04t-shirt which is
  660. 33:08$7.99 if we press
  661. 33:11enter calculating with these floats
  662. 33:14causes the inaccuracies that we saw
  663. 33:17earlier so how do we avoid this problem
  664. 33:20the best practice when calculating money
  665. 33:23is to calculate in cents instead of
  666. 33:26dollars so instead of
  667. 33:29$20.95 we're going to calculate
  668. 33:342,95 plus
  669. 33:38$7.99 notice we're not using floats
  670. 33:41anymore so we avoid the
  671. 33:44inaccuracies but now this number is in
  672. 33:46cents so we need to convert back to
  673. 33:49Dollars to do that we'll surround this
  674. 33:52calculation with brackets to make sure
  675. 33:56we calculate this first and then at the
  676. 33:58end we'll divide by 100 to convert back
  677. 34:04to dollars because there are 100 cents
  678. 34:07in each dollar if we press
  679. 34:11enter and scroll
  680. 34:13up that's how we get the exact number
  681. 34:16that we see on the page so remember in
  682. 34:20programming calculations with floats are
  683. 34:23sometimes inaccurate and when working
  684. 34:25with money the best practice is to do
  685. 34:28the calculation in cents and then
  686. 34:31convert back to dollars at the
  687. 34:34end the next detail we're going to learn
  688. 34:37is how to round a number in JavaScript
  689. 34:40rounding just means taking a number and
  690. 34:43moving it to the nearest integer to
  691. 34:46round a number we can use this
  692. 34:49code capital m
  693. 34:52math. round Open Bracket close bracket
  694. 34:57so make sure you use a capital M because
  695. 35:01JavaScript is case
  696. 35:03sensitive now between the brackets we
  697. 35:06can put a number that we want to round
  698. 35:09like
  699. 35:112.2 if we press enter it will round this
  700. 35:15number down to
  701. 35:17two if we do capital M math do round
  702. 35:23Open Bracket close bracket and between
  703. 35:26the brackets we put
  704. 35:282.8 and press
  705. 35:30enter it will round 2.8 up to three now
  706. 35:36let's do a practical example with
  707. 35:40math.round we're going to calculate the
  708. 35:42tax again but this time we're going to
  709. 35:45round the result to exactly this number
  710. 35:48on the page so first let's calculate the
  711. 35:52tax we'll go into our
  712. 35:55console and add the cost of the product
  713. 35:57products in cents so
  714. 36:012,95 for the
  715. 36:04basketball and plus the cost of the
  716. 36:07t-shirt which is
  717. 36:10$7.99 and we'll surround this in
  718. 36:12Brackets to make sure we calculate this
  719. 36:16first and then we're going to multiply
  720. 36:18this by the tax percent or
  721. 36:230.1 and finally we'll convert this back
  722. 36:26to dollars so let's surround this in
  723. 36:29Brackets as well to make sure we
  724. 36:32calculate this first and at the end
  725. 36:35we'll divide by 100 to convert back to
  726. 36:41dollars so notice here we can have
  727. 36:44brackets inside brackets in this
  728. 36:47situation JavaScript will calculate the
  729. 36:50inner brackets first and then the outer
  730. 36:53brackets and then everything else if we
  731. 36:56press enter
  732. 36:58here this gives us
  733. 37:012.89 which is close to the number on the
  734. 37:05page it's the same amount of money but
  735. 37:08our number is more accurate than we need
  736. 37:12so now let's learn how to use math.round
  737. 37:15to round this number to exactly
  738. 37:182.89 first let's create a copy of this
  739. 37:22calculation so we'll select it right
  740. 37:26click copy
  741. 37:29and down here right click and
  742. 37:34paste by the way if you paste in the
  743. 37:36console you might see this warning
  744. 37:39message to fix this you can type allow
  745. 37:45pasting and press enter and now you can
  746. 37:52paste so let's continue and we were
  747. 37:55trying to round 2.89 4 to exactly
  748. 38:022.89 so one thing we might try to do is
  749. 38:05to round this entire
  750. 38:08calculation however this will round 2.89
  751. 38:124 to 3 because it rounds to the nearest
  752. 38:16integer instead what we want to round is
  753. 38:20this number here so remember that this
  754. 38:23number is the result in cents which is
  755. 38:28289.50
  756. 38:30if we round this number it will round
  757. 38:33down to
  758. 38:36289 which is exactly
  759. 38:40$289 so to round this calculation at the
  760. 38:44front we'll type capital M
  761. 38:50math.round so we already have brackets
  762. 38:53around this so we don't need extra
  763. 38:56brackets so this this will round to
  764. 39:00289 and finally we'll convert this back
  765. 39:03to dollars at the End by dividing by 100
  766. 39:10if we press
  767. 39:11enter that's how we rounded this
  768. 39:13calculation to get the exact number on
  769. 39:16the
  770. 39:17page so to summarize we first calculate
  771. 39:20the result in cents and then we round
  772. 39:24this to the nearest scent and then at
  773. 39:27the end and we convert back to
  774. 39:29Dollars again don't worry you don't have
  775. 39:32to be good at math to learn JavaScript
  776. 39:35these examples are just for learning
  777. 39:38almost all the math we do in JavaScript
  778. 39:40is more simple than this I'll give you
  779. 39:43some exercises at the end of this lesson
  780. 39:46to help you get more practice the last
  781. 39:49thing we're going to learn in this
  782. 39:50lesson is how to use Google when you're
  783. 39:53learning to code so far I've been giving
  784. 39:56you different pieces pces of code like
  785. 39:59alert and
  786. 40:01math.round you might be wondering where
  787. 40:03do I find this code these pieces of code
  788. 40:07are features of JavaScript and I find
  789. 40:10this code by searching on Google so
  790. 40:13we're going to learn how to use Google
  791. 40:15to learn new features of JavaScript on
  792. 40:18our own let's go to our browser and
  793. 40:21create a new tab at the
  794. 40:24top and we'll click here and type
  795. 40:30google.com and press
  796. 40:33enter and we'll search in Google so the
  797. 40:37trick to searching in Google is to
  798. 40:39search for what we are trying to do for
  799. 40:42example if we want to round a number we
  800. 40:46would search for
  801. 40:49JavaScript how to round a
  802. 40:54number if we press enter
  803. 40:58sometimes you might get the code right
  804. 41:00away in the first result and sometimes
  805. 41:03you might have to go into the other
  806. 41:05results to find the code that you need
  807. 41:08in my example I see the code math.round
  808. 41:12in the first result so I can click into
  809. 41:15this result read about what this code
  810. 41:18does and also see some examples of how
  811. 41:21to use it one thing to keep in mind is
  812. 41:24that when you're searching in Google you
  813. 41:26might not understand understand all the
  814. 41:28code that you get in your results for
  815. 41:31example here there's a lot of code that
  816. 41:33we haven't learned yet and actually
  817. 41:36that's okay we just need to look for the
  818. 41:39pieces of code that look familiar to us
  819. 41:42like this part of the code we can then
  820. 41:45use this part of the code in our own
  821. 41:48code we don't have to understand every
  822. 41:50bit of code that we get from Google and
  823. 41:54don't worry as we move through this
  824. 41:56course we we will understand more and
  825. 41:58more of this code that we get from
  826. 42:01Google
  827. 42:02results these days we also have some
  828. 42:05artificial intelligence or AI tools that
  829. 42:08can help us search for code I'll provide
  830. 42:11links to some of these tools in the
  831. 42:13video
  832. 42:15description by the way to find the links
  833. 42:18mentioned in each lesson you can click
  834. 42:20here in the
  835. 42:26description and this page contains all
  836. 42:28the links mentioned in each
  837. 42:33lesson and that's the end of this lesson
  838. 42:36in this lesson we learned how to work
  839. 42:38with numbers and math we learned the
  840. 42:42order of operations and brackets we
  841. 42:45calculated the numbers in the final
  842. 42:47Amazon Project we learned that
  843. 42:50calculations using floats can be
  844. 42:52inaccurate and how to avoid this problem
  845. 42:54when working with money we learned how
  846. 42:57to round numbers using
  847. 42:59math.round and we learned how to search
  848. 43:02for code on our own here are some
  849. 43:05exercises to help you practice numbers
  850. 43:07and math
  851. 43:31in the previous lesson we learned how to
  852. 43:33work with numbers and math in this
  853. 43:36lesson we're going to learn how to work
  854. 43:38with text and we're going to create the
  855. 43:41text in this section of the final
  856. 43:43project first make sure you have the
  857. 43:46final project open and you're on the
  858. 43:48checkout page also make sure you have
  859. 43:51the console open on this page if not you
  860. 43:55can go to a blank area
  861. 43:58right click click
  862. 44:00inspect and then click the
  863. 44:04console now we're going to learn a
  864. 44:07feature of JavaScript called strings a
  865. 44:10string represents text let's go to our
  866. 44:14console and create our first string
  867. 44:17we're going to type single quote hello
  868. 44:21and single quote and press
  869. 44:25enter this is a string and it represents
  870. 44:29the text hello and we've actually seen
  871. 44:32this code before in the first lesson the
  872. 44:35first piece of code that we learned was
  873. 44:39alert Open Bracket single quote hello
  874. 44:44single quote close bracket and
  875. 44:47semicolon this code creates a popup on
  876. 44:50the page if we press
  877. 44:53enter it creates a popup now in inside
  878. 44:57these brackets we actually created a
  879. 45:00string this string is the text that
  880. 45:03appears in the popup so as you can see a
  881. 45:07string represents
  882. 45:09text let's press
  883. 45:11okay and now let's learn the syntax
  884. 45:14rules for strings to create a string we
  885. 45:17just write some text like hello and then
  886. 45:21surround this text with single quotes
  887. 45:24one at the beginning and one at the end
  888. 45:27we can also add strings together for
  889. 45:30example let's type the string
  890. 45:35sum plus the string
  891. 45:39text and press
  892. 45:42enter when we add two strings it
  893. 45:45combines the strings together into a
  894. 45:47bigger string and this makes sense we're
  895. 45:50adding two pieces of text together we
  896. 45:54can also add more than two strings at a
  897. 45:56time
  898. 45:57for example we can type the string
  899. 46:02sum plus the string
  900. 46:06more plus the string
  901. 46:11text if we press
  902. 46:13enter the computer will combine all
  903. 46:16three strings into one big string by the
  904. 46:20way this is called concatenation when we
  905. 46:24combine strings together now in in the
  906. 46:27previous lesson we learned about numbers
  907. 46:30numbers and strings are two different
  908. 46:33types of values in JavaScript they
  909. 46:36represent different things and we can
  910. 46:39actually check what type of value
  911. 46:41something is using this code type of and
  912. 46:47then space and then a value like the
  913. 46:50number two if we press
  914. 46:53enter type of will tell us that two
  915. 46:57is a number if we do type of quote hello
  916. 47:05quote and press enter it will tell us
  917. 47:09that hello is a
  918. 47:11string now what if we added a string and
  919. 47:14a number together for example let's type
  920. 47:18the string
  921. 47:21hello plus the number three and press
  922. 47:25enter
  923. 47:27so when we add a string and a number
  924. 47:31JavaScript will automatically convert
  925. 47:33this number into a string so this gets
  926. 47:37converted into the string hello plus the
  927. 47:41string
  928. 47:42three and then it combines them together
  929. 47:45into the string hello 3 this feature is
  930. 47:49called type coersion or automatic type
  931. 47:54conversion okay now that we understand
  932. 47:56understand the basics of strings let's
  933. 47:59do some practical examples we're going
  934. 48:02to use strings and numbers to create the
  935. 48:05text in this section of the project
  936. 48:08first make sure that in your cart you
  937. 48:11have one basketball with free shipping
  938. 48:15and you have one t-shirt also with free
  939. 48:19shipping and you have no other
  940. 48:22products let's scroll up and we'll start
  941. 48:26by creating this text in the top right
  942. 48:29so this is the cost of the products but
  943. 48:32this time we're going to have a dollar
  944. 48:34sign so instead of just a number this
  945. 48:37will be
  946. 48:38text so one thing we can do to create
  947. 48:41this text is just type out the string
  948. 48:44directly like quote doll
  949. 48:4928.94 and
  950. 48:51quote however let's actually calculate
  951. 48:54this number using math
  952. 48:57remember the advantage of JavaScript is
  953. 48:59that it's able to do
  954. 49:01calculations so instead of typing out
  955. 49:0528.94 we're just going to create the
  956. 49:08string Dollar Sign Plus and we're going
  957. 49:12to calculate this number using math so
  958. 49:16we're going to add the cost of the
  959. 49:18basketball which is
  960. 49:22$2.95
  961. 49:25plus the cost of of the t-shirt which is
  962. 49:31$7.99 if we press
  963. 49:33enter unfortunately this doesn't look
  964. 49:36right instead of doing math and adding
  965. 49:39these two numbers together it just sort
  966. 49:41of put the numbers one after another
  967. 49:44into the string so why is this happening
  968. 49:48JavaScript adds from left to right so
  969. 49:51the first step is to add these two
  970. 49:54values and as we learned if we add a
  971. 49:57string and a number JavaScript will
  972. 50:00automatically convert this number into a
  973. 50:03string so the result of this calculation
  974. 50:06will be the string doll
  975. 50:112.95 and
  976. 50:13quote the next step is to add
  977. 50:177.99 so it will add
  978. 50:227.99 and now we're adding again a string
  979. 50:25and a number
  980. 50:27so JavaScript will automatically convert
  981. 50:29this number into a string and combine
  982. 50:33them together that's why it puts
  983. 50:367.99 at the end of this string instead
  984. 50:39of doing
  985. 50:40math so what we actually want to do here
  986. 50:44is to add these two numbers first and
  987. 50:47then combine the total with the string
  988. 50:50so how do we do this the solution is
  989. 50:53that strings also follow the order of
  990. 50:57operations just like numbers and
  991. 51:00remember in the order of operations we
  992. 51:02can use brackets to control which part
  993. 51:05of a calculation gets done first so
  994. 51:09let's type this calculation out again
  995. 51:11the string Dollar Sign Plus 20.95%
  996. 51:26close bracket around this
  997. 51:29calculation this tells the computer to
  998. 51:32calculate this number first using math
  999. 51:35and then combine this result with the
  1000. 51:38string after if we press
  1001. 51:41enter now this is doing math and it
  1002. 51:44solves our original problem
  1003. 51:47unfortunately now we have another
  1004. 51:49problem this math is inaccurate so
  1005. 51:53remember from the previous lesson doing
  1006. 51:55calculations with floats can sometimes
  1007. 51:58cause
  1008. 51:59inaccuracies and the best practice when
  1009. 52:01calculating money is always calculate in
  1010. 52:04cents and then convert back to dollars
  1011. 52:07at the end so at the bottom here we're
  1012. 52:11going to add the string
  1013. 52:14Dollar Plus open bracket and instead of
  1014. 52:20$20.95 we're going to add
  1015. 52:242,95 Plus
  1016. 52:28$7.99 and close bracket and then at the
  1017. 52:32end we're going to convert this number
  1018. 52:34back to Dollars by dividing by 100 if we
  1019. 52:40press
  1020. 52:42enter this will give us the text that we
  1021. 52:44see in the final
  1022. 52:46project again strings follow the same
  1023. 52:50order of operations as numbers brackets
  1024. 52:54are always calculated first multiply and
  1025. 52:57divide are done next and add and
  1026. 53:00subtract are done after so adding this
  1027. 53:03result to the string is the last
  1028. 53:08step now let's do a more complicated
  1029. 53:11example let's use strings and numbers to
  1030. 53:14create this entire line of text not just
  1031. 53:18the dollar text to create this we're
  1032. 53:21going to go from left to right let's go
  1033. 53:24back in our console and we're going to
  1034. 53:27start with the string
  1035. 53:30items Open Bracket and here we could
  1036. 53:34just type two and close bracket but
  1037. 53:37let's actually calculate this number as
  1038. 53:40well using math so this number is the
  1039. 53:44quantity or the number of products in
  1040. 53:46our cart let's go back to our console
  1041. 53:50we're going to end the string here and
  1042. 53:53we're going to add and we're going to
  1043. 53:55calculate the quantity
  1044. 53:57so we have one basketball plus one
  1045. 54:01t-shirt again we run into the same
  1046. 54:04problem as before this gets calculated
  1047. 54:07first and results in a string and then
  1048. 54:11it will just combine the one at the end
  1049. 54:13of the string instead of doing math to
  1050. 54:17solve this we're going to use brackets
  1051. 54:19again so around the 1 + 1 we're going to
  1052. 54:23add Open Bracket and close
  1053. 54:26bracket this tells the computer to add
  1054. 54:30the numbers first and then combine it
  1055. 54:33with a
  1056. 54:34string all right let's keep going from
  1057. 54:37left to right we're going to add the
  1058. 54:40rest of the string quote close bracket
  1059. 54:44colon and let's just use a space to
  1060. 54:47represent the space between these two so
  1061. 54:50we'll type a space dollar sign and then
  1062. 54:54we're going to calculate this number
  1063. 54:56again and add it to the string so we'll
  1064. 55:00end the string and then plus and we
  1065. 55:04already did this calculation up here so
  1066. 55:07we can select this and then
  1067. 55:11rightclick
  1068. 55:12copy and here right click and
  1069. 55:17paste and press
  1070. 55:20enter by the way if you paste in the
  1071. 55:23console you might see this warning
  1072. 55:25message
  1073. 55:26to fix this you can type allow
  1074. 55:32pasting and press
  1075. 55:34enter and now you can
  1076. 55:39paste and that's how we create this
  1077. 55:42first line of text using strings and
  1078. 55:45numbers we break up the string into
  1079. 55:47Parts calculate the numbers and then add
  1080. 55:51the parts back
  1081. 55:52together all right now let's take these
  1082. 55:55strings that we created Creed and do
  1083. 55:57something with them let's create a popup
  1084. 56:01and display this first line of text
  1085. 56:03inside the popup we'll go back to our
  1086. 56:06console and type the code for creating a
  1087. 56:10popup
  1088. 56:11alert Open Bracket close bracket
  1089. 56:15semicolon and then inside the popup
  1090. 56:18we're going to display this string so
  1091. 56:21let's just make a copy of this code
  1092. 56:24we'll select it right click
  1093. 56:28copy and between the brackets right
  1094. 56:31click and
  1095. 56:34paste if we press
  1096. 56:36enter the computer displays a popup with
  1097. 56:39the first line of text of the final
  1098. 56:41project
  1099. 56:43inside so that's how we use strings to
  1100. 56:46create the text in the final project and
  1101. 56:48once we create the strings we can use
  1102. 56:51them in other JavaScript
  1103. 56:55code in in the rest of this lesson we're
  1104. 56:57going to learn more details about
  1105. 57:00strings in JavaScript there are actually
  1106. 57:03three ways to create a string the first
  1107. 57:06way is to use single quotes which we
  1108. 57:09already learned for example we can type
  1109. 57:13single quote hello and single quote and
  1110. 57:17press
  1111. 57:19enter the second way to create a string
  1112. 57:22is to use double quotes for example
  1113. 57:26double quote hello and double quote and
  1114. 57:30press
  1115. 57:32enter this creates the exact same string
  1116. 57:35as single quotes so which one should we
  1117. 57:38use to create a string single quotes or
  1118. 57:42double quotes in JavaScript I recommend
  1119. 57:45using single quotes by default they're a
  1120. 57:48little easier to read and type there is
  1121. 57:52one situation where double quotes might
  1122. 57:54be useful and that is is if we have a
  1123. 57:57single quote inside the string for
  1124. 58:00example if we type the
  1125. 58:03string I'm
  1126. 58:06learning
  1127. 58:09JavaScript and single quote and press
  1128. 58:13enter this doesn't work because there's
  1129. 58:15a single quote inside the string and
  1130. 58:18this single quote will end the string
  1131. 58:21early to solve this we can create the
  1132. 58:24string using double quotes for example
  1133. 58:28double quote I'm
  1134. 58:31learning
  1135. 58:33JavaScript and double quote and press
  1136. 58:37enter so now this works because we
  1137. 58:41created the string using double quotes
  1138. 58:44the single quote no longer has a special
  1139. 58:46meaning and it will no longer end the
  1140. 58:48string early so this is a situation
  1141. 58:52where double quotes might be useful
  1142. 58:54otherwise we use single quotes by
  1143. 58:57default now there's actually another
  1144. 58:59solution to this problem other than
  1145. 59:01using double quotes and that's by using
  1146. 59:04an escape character so a character is
  1147. 59:08one letter number or symbol in a piece
  1148. 59:12of textt for example the text hello has
  1149. 59:17five characters h e l l
  1150. 59:22o a character can be a letter in number
  1151. 59:26or also a symbol these are all
  1152. 59:29considered
  1153. 59:31characters now in addition to these
  1154. 59:33characters we have a special set of
  1155. 59:36characters we can also use in a string
  1156. 59:39called Escape characters an escape
  1157. 59:42character looks like this a backslash
  1158. 59:46and then another character like single
  1159. 59:49quote this combination actually counts
  1160. 59:52as one character in a string this
  1161. 59:55character creat creat a single quote
  1162. 59:57that is just text it doesn't have a
  1163. 1:00:00special meaning and it doesn't start or
  1164. 1:00:02end strings for example we can create
  1165. 1:00:07this string with single quotes again so
  1166. 1:00:10single quote I and this time we'll use
  1167. 1:00:14the Escape character back slash single
  1168. 1:00:17quote and then M
  1169. 1:00:21learning
  1170. 1:00:23JavaScript and single quote and press
  1171. 1:00:27enter and now this works because the
  1172. 1:00:31Escape character back SL single quote
  1173. 1:00:34creates a single quote that is just text
  1174. 1:00:37it doesn't start or end the
  1175. 1:00:39string another Escape character that's
  1176. 1:00:42useful to know is back SL double quote
  1177. 1:00:46this creates a double quote that is just
  1178. 1:00:48text it doesn't start or end a
  1179. 1:00:52string the last Escape character we'll
  1180. 1:00:54learn in this lesson is back
  1181. 1:00:58sln this is called the new line
  1182. 1:01:01character this Escape character doesn't
  1183. 1:01:04create the letter N anymore it creates a
  1184. 1:01:07new line of text for example let's
  1185. 1:01:11create a popup using
  1186. 1:01:15alert Open Bracket close bracket and
  1187. 1:01:18semicolon and then inside the brackets
  1188. 1:01:21we're going to create a string sum and
  1189. 1:01:25then back
  1190. 1:01:26sln text and single quote if we press
  1191. 1:01:32enter you'll notice that this Escape
  1192. 1:01:35character created a new line of text in
  1193. 1:01:39the popup and that's why we call this
  1194. 1:01:41the new line character so that's how
  1195. 1:01:44Escape characters work they are a set of
  1196. 1:01:47special characters that start with back
  1197. 1:01:49slash and we can use them in a
  1198. 1:01:52string now let's press okay
  1199. 1:01:56and we're going to learn the Third Way
  1200. 1:01:58of creating a string in JavaScript which
  1201. 1:02:01is using back ticks for example let's
  1202. 1:02:04type in our console and we'll type
  1203. 1:02:08backtick hello and backtick and press
  1204. 1:02:13enter this creates the same string as
  1205. 1:02:17single quotes and double quotes strings
  1206. 1:02:20created with backx actually have a name
  1207. 1:02:23we call these template strings and they
  1208. 1:02:26have a name because they have some
  1209. 1:02:28special features the first special
  1210. 1:02:30feature of template strings is called
  1211. 1:02:34interpolation interpolation lets us
  1212. 1:02:36insert a value directly into a string
  1213. 1:02:40for example let's say we want to create
  1214. 1:02:43the first line of text in our project
  1215. 1:02:46earlier in this lesson we created this
  1216. 1:02:48text with concatenation which means we
  1217. 1:02:51broke it up into parts and added them
  1218. 1:02:54back together in interpolation gives us
  1219. 1:02:57a much easier way to do this we'll go to
  1220. 1:03:00our console and we'll create this text
  1221. 1:03:03using a template string because
  1222. 1:03:05interpolation is only a feature of
  1223. 1:03:07template strings so we'll type backtick
  1224. 1:03:12items Open Bracket close bracket and
  1225. 1:03:16back tick and now between these brackets
  1226. 1:03:19we want to insert the quantity or the
  1227. 1:03:22number of products in our cart to insert
  1228. 1:03:25this with interpolation we're going to
  1229. 1:03:27type Dollar open curly bracket close
  1230. 1:03:31curly bracket so this combination of
  1231. 1:03:34characters allows us to insert values
  1232. 1:03:37directly into the string between these
  1233. 1:03:40curly brackets we can calculate the
  1234. 1:03:42quantity which is one basketball plus
  1235. 1:03:47one
  1236. 1:03:48t-shirt this will add 1 + 1 which equals
  1237. 1:03:522 and then insert it directly into the
  1238. 1:03:55string
  1239. 1:03:56let's keep going and create the rest of
  1240. 1:03:59this line so we're going to type at the
  1241. 1:04:02end a Colin space and dollar and now
  1242. 1:04:07we're going to insert another value into
  1243. 1:04:09the string which is the cost of the
  1244. 1:04:12products to insert another value we're
  1245. 1:04:15going to type Dollar open curly bracket
  1246. 1:04:18close curly bracket again in between the
  1247. 1:04:21curly brackets we're going to calculate
  1248. 1:04:24the cost of the products
  1249. 1:04:26which is
  1250. 1:04:302,95
  1251. 1:04:33plus
  1252. 1:04:35$7.99 and we'll surround this in
  1253. 1:04:38Brackets to make sure we calculate this
  1254. 1:04:40first and at the end we're going to
  1255. 1:04:43convert back to Dollars by dividing by
  1256. 1:04:47100 if we press
  1257. 1:04:50enter this creates the first line of
  1258. 1:04:53text again in our final project
  1259. 1:04:57if we compare this code which uses
  1260. 1:04:59interpolation to our previous code which
  1261. 1:05:02uses concatenation you can see that
  1262. 1:05:05interpolation is a much cleaner way of
  1263. 1:05:08inserting values into a string so
  1264. 1:05:10interpolation is a recommended
  1265. 1:05:13solution next template strings have
  1266. 1:05:16another special feature called
  1267. 1:05:18multi-line strings let's go to our
  1268. 1:05:22console and we're going to type a
  1269. 1:05:24backtick
  1270. 1:05:26sum and then press
  1271. 1:05:29enter and text and back tick notice that
  1272. 1:05:35this string is on multiple lines so we
  1273. 1:05:38call this a multi-line string and this
  1274. 1:05:41feature is only available for template
  1275. 1:05:44strings if we press
  1276. 1:05:47enter you can see that the new line in
  1277. 1:05:50the string just creates the new line
  1278. 1:05:52Escape character that we learned earlier
  1279. 1:05:55so that's the multi-line string feature
  1280. 1:05:58of template strings we're going to use
  1281. 1:06:00multi-line strings a lot later in this
  1282. 1:06:03course but for now I just wanted to
  1283. 1:06:06introduce you to this feature now you
  1284. 1:06:08might be wondering again what should we
  1285. 1:06:11use to create a string single quotes or
  1286. 1:06:14template strings even though template
  1287. 1:06:17strings have more features I find that
  1288. 1:06:19most JavaScript code still uses single
  1289. 1:06:22quotes by default so my personal
  1290. 1:06:24recommendation is to use single quotes
  1291. 1:06:27by default and if we need to insert a
  1292. 1:06:30value into the string or type the string
  1293. 1:06:33on multiple lines then use template
  1294. 1:06:36strings and that's the end of this
  1295. 1:06:38lesson in this lesson we learned about
  1296. 1:06:42strings a string represents text we
  1297. 1:06:46learned how to use strings and numbers
  1298. 1:06:48to create the text in our final project
  1299. 1:06:51we learned three different ways of
  1300. 1:06:54creating a string single quotes double
  1301. 1:06:56quotes and back ticks or template
  1302. 1:06:59strings we learned about Escape
  1303. 1:07:02characters and we learned two useful
  1304. 1:07:04features of template strings
  1305. 1:07:06interpolation and multi-line
  1306. 1:07:09strings here are some exercises to help
  1307. 1:07:12you practice working with strings
  1308. 1:07:35so far in this course we've learned some
  1309. 1:07:37basic features of JavaScript like
  1310. 1:07:40numbers and strings in this lesson we're
  1311. 1:07:44going to learn how to use HTML CSS and
  1312. 1:07:48JavaScript together because as I
  1313. 1:07:50mentioned at the beginning of this
  1314. 1:07:52course we use all three Technologies to
  1315. 1:07:55build build a full
  1316. 1:07:57website HTML creates the content of a
  1317. 1:08:00website like the buttons and the text
  1318. 1:08:04CSS changes the appearance of the
  1319. 1:08:07website and JavaScript makes the website
  1320. 1:08:11interactive the first thing we're going
  1321. 1:08:13to do is review the basics of HTML and
  1322. 1:08:17CSS that we will need for the rest of
  1323. 1:08:19this
  1324. 1:08:20course now in order to write HTML and
  1325. 1:08:24CSS code we need to install another
  1326. 1:08:27piece of software called a code editor a
  1327. 1:08:31code editor helps us write and organize
  1328. 1:08:34our code the most popular code editor
  1329. 1:08:37for web development is called vs code or
  1330. 1:08:41Visual Studio code to install vs code
  1331. 1:08:45you can go to google.com in your
  1332. 1:08:49browser search for VSS
  1333. 1:08:53code click the result
  1334. 1:08:56and follow the instructions to download
  1335. 1:08:58and
  1336. 1:09:00install once we've installed VSS code
  1337. 1:09:03we're ready to review the basics of HTML
  1338. 1:09:06and
  1339. 1:09:07CSS we'll start with
  1340. 1:09:10HTML HTML stands for hyper text markup
  1341. 1:09:15language and just like JavaScript HTML
  1342. 1:09:18is also giving instructions to a
  1343. 1:09:21computer now unlike JavaScript we can't
  1344. 1:09:24use the console to write and run HTML
  1345. 1:09:28code instead we write HTML code inside a
  1346. 1:09:32file using our code editor let's do that
  1347. 1:09:37right now we'll start by going to our
  1348. 1:09:39desktop so I'm going to close my browser
  1349. 1:09:42to go to my
  1350. 1:09:44desktop and then we're going to create a
  1351. 1:09:46new folder so we can right click new
  1352. 1:09:52folder this folder is going to contain
  1353. 1:09:55all the code that we write in this
  1354. 1:09:57course let's name this folder JavaScript
  1355. 1:10:03dasc next we're going to open this
  1356. 1:10:06folder in our code editor so let's open
  1357. 1:10:09vs
  1358. 1:10:11code and then we'll click
  1359. 1:10:14file open
  1360. 1:10:17folder and find the folder that we just
  1361. 1:10:20created for me it's on the
  1362. 1:10:23desktop and it's this folder right right
  1363. 1:10:25here so we're going to select the folder
  1364. 1:10:28and then click select
  1365. 1:10:32folder now we can close this startup
  1366. 1:10:35message and we're going to create an
  1367. 1:10:38HTML file we're going to click this icon
  1368. 1:10:41to create a new
  1369. 1:10:44file and we're going to name this file
  1370. 1:10:48website do
  1371. 1:10:50HTML and press
  1372. 1:10:53enter ending the file name name with
  1373. 1:10:56HTML tells a computer that this file
  1374. 1:10:59contains HTML code rather than just
  1375. 1:11:03text now let's review some basic HTML
  1376. 1:11:07code we're going to type the code less
  1377. 1:11:10than button greater than and we also
  1378. 1:11:15need to type less than slash button
  1379. 1:11:17greater than but our code editor might
  1380. 1:11:20autocomplete this for us so this code
  1381. 1:11:23tells a computer to create a button and
  1382. 1:11:27inside the button we're going to have
  1383. 1:11:29the text
  1384. 1:11:32hello now let's save this file by
  1385. 1:11:35clicking
  1386. 1:11:36file
  1387. 1:11:38save or by typing contrl s on Windows or
  1388. 1:11:42command s on
  1389. 1:11:45Mac now to run this HTML code we're
  1390. 1:11:48going to open this file in our browser
  1391. 1:11:52let's go back to our desktop
  1392. 1:11:55we're going to open the folder that we
  1393. 1:11:59created find the HTML file select it and
  1394. 1:12:04then right click open with Google
  1395. 1:12:10Chrome as you can see we have a website
  1396. 1:12:13and we created a button with the text
  1397. 1:12:16hello inside and if we look back at our
  1398. 1:12:21code that's exactly what we told the
  1399. 1:12:23computer to do
  1400. 1:12:25so just like JavaScript HTML is also
  1401. 1:12:29giving instructions to a
  1402. 1:12:32computer let's review some more HTML
  1403. 1:12:35code first I'm just going to rearrange
  1404. 1:12:38my windows here so that it's easy to see
  1405. 1:12:40the website side by
  1406. 1:12:44side next we're going to click the end
  1407. 1:12:48of the first line and press enter to
  1408. 1:12:50create a new line and we're going to
  1409. 1:12:52type the code less than P greater than
  1410. 1:12:57and we also need to type less than SLP
  1411. 1:13:00greater than so this code creates a
  1412. 1:13:04paragraph and inside the paragraph we're
  1413. 1:13:07going to put some text for example
  1414. 1:13:11paragraph of text now let's save our
  1415. 1:13:15file by typing contrl s on Windows or
  1416. 1:13:18command s on
  1417. 1:13:20Mac and to update our website to the new
  1418. 1:13:23code we're going to go to our website
  1419. 1:13:25and click refresh or typ controlr on
  1420. 1:13:29Windows or command r on
  1421. 1:13:33Mac and now you can see that the
  1422. 1:13:35computer created a paragraph of text
  1423. 1:13:39just like we told it to do and that's
  1424. 1:13:42the basics of HTML the computer reads
  1425. 1:13:45our code and then follows our
  1426. 1:13:47instructions one by one first it creates
  1427. 1:13:50a button and then it creates a paragraph
  1428. 1:13:54of text
  1429. 1:13:56now let's review the syntax rules for
  1430. 1:14:00HTML each of these things that were
  1431. 1:14:02displaying on a web page these are
  1432. 1:14:04called HTML elements so a button is a
  1433. 1:14:08type of HTML element and a paragraph is
  1434. 1:14:11another type of HTML element to create
  1435. 1:14:15an element we start with a less than
  1436. 1:14:18symbol and then the element name like a
  1437. 1:14:21button and then the greater than symbol
  1438. 1:14:25so this whole thing is called an HTML
  1439. 1:14:28tag this tells a computer to create a
  1440. 1:14:32button
  1441. 1:14:33element every HTML tag needs a matching
  1442. 1:14:37tag the matching tag follows the same
  1443. 1:14:40syntax except it has a slash in front of
  1444. 1:14:43the element name so this is called the
  1445. 1:14:47opening tag and this is called the
  1446. 1:14:49closing tag you can think of the opening
  1447. 1:14:52tag as the start of the button and the
  1448. 1:14:54closing tag tag as the end of the button
  1449. 1:14:57and in between we have the contents
  1450. 1:15:00inside the button in this case it's the
  1451. 1:15:03text
  1452. 1:15:04hello in addition to text we can also
  1453. 1:15:07have elements inside an element for
  1454. 1:15:10example we can go to our paragraph
  1455. 1:15:13element and we can add a button inside
  1456. 1:15:17so less than button greater than and we
  1457. 1:15:21also need less than SL button greater
  1458. 1:15:24than and then inside the button we'll
  1459. 1:15:26also have the text
  1460. 1:15:28hello if we save this file and refresh
  1461. 1:15:33the
  1462. 1:15:34page now our paragraph element has a
  1463. 1:15:37button element
  1464. 1:15:39inside by the way this is called nesting
  1465. 1:15:42when we have an element inside another
  1466. 1:15:46element the last syntax rule we have to
  1467. 1:15:48review is that in HTML if we have
  1468. 1:15:52multiple spaces for example if we go
  1469. 1:15:55here and type multiple
  1470. 1:15:58spaces multiple spaces are combined into
  1471. 1:16:01one space on the web page so if we save
  1472. 1:16:05this code and
  1473. 1:16:07refresh you'll notice that all of the
  1474. 1:16:09spaces that we added only show up as one
  1475. 1:16:12space on the web page in addition in
  1476. 1:16:16HTML new lines also count as spaces so
  1477. 1:16:20if we go back to our
  1478. 1:16:22code and we type here and press enter to
  1479. 1:16:26create a new line and then save and
  1480. 1:16:32refresh notice that the new line doesn't
  1481. 1:16:34appear on the web page and that's
  1482. 1:16:37because this new line here is the same
  1483. 1:16:40as spaces in HTML so all of these get
  1484. 1:16:44combined into one space on the web
  1485. 1:16:47page at first this might seem a little
  1486. 1:16:50weird but this feature gives us a lot of
  1487. 1:16:52flexibility when organizing our h ml
  1488. 1:16:55code for example we can go into our code
  1489. 1:16:59and add a new line here to separate
  1490. 1:17:03these two elements and make our code
  1491. 1:17:04easier to read another thing we usually
  1492. 1:17:07do to make our code easier to read is to
  1493. 1:17:10put the contents of an element on its
  1494. 1:17:13own line for example we're going to type
  1495. 1:17:16here and press enter and here and press
  1496. 1:17:21enter
  1497. 1:17:22again to put the contents of the button
  1498. 1:17:24Buton on its own line now it's a lot
  1499. 1:17:27easier to see where this button starts
  1500. 1:17:30and where it ends we can do the same
  1501. 1:17:33thing with the paragraph We can type
  1502. 1:17:35here and press enter and type here as
  1503. 1:17:39well and press enter and then remove
  1504. 1:17:42some of the Extra
  1505. 1:17:44Spaces to put the content of the
  1506. 1:17:47paragraph on its own line and now if we
  1507. 1:17:50save our
  1508. 1:17:51file and refresh
  1509. 1:17:55you notice that none of the new lines
  1510. 1:17:57that we added appear on the web page by
  1511. 1:18:00the way a web page is a single page like
  1512. 1:18:03the homepage or the cart page together
  1513. 1:18:07we call this a
  1514. 1:18:09website so that's the basics of HTML and
  1515. 1:18:13HTML
  1516. 1:18:15syntax now let's review CSS CSS stands
  1517. 1:18:19for cascading style sheets and CSS
  1518. 1:18:23changes the appearance of our website
  1519. 1:18:26let's review some CSS code we're going
  1520. 1:18:29to write our CSS code inside our HTML
  1521. 1:18:33file first let's type here and create
  1522. 1:18:37some new lines at the top and then at
  1523. 1:18:40the top we're going to create a special
  1524. 1:18:43HTML element called the style element so
  1525. 1:18:47let's type less than style greater
  1526. 1:18:52than and we also need the closing tag
  1527. 1:18:55less than SL style greater than so the
  1528. 1:18:58style element is unique because it
  1529. 1:19:01doesn't appear visibly on the page
  1530. 1:19:04instead the purpose of the style element
  1531. 1:19:07is that it lets us write CSS code
  1532. 1:19:10inside so inside here let's press enter
  1533. 1:19:14to create a new line and we're going to
  1534. 1:19:17type
  1535. 1:19:18button open curly bracket close curly
  1536. 1:19:22bracket and then inside the curly
  1537. 1:19:24brackets we're also going to press enter
  1538. 1:19:27to create a new line so this is CSS code
  1539. 1:19:32and this code tells a computer to change
  1540. 1:19:34the appearance of all buttons on the
  1541. 1:19:37page and then inside these curly
  1542. 1:19:39brackets we can tell the computer how to
  1543. 1:19:42change the appearance for example we can
  1544. 1:19:45type
  1545. 1:19:47background- color colon red and
  1546. 1:19:52semicolon if we save
  1547. 1:19:55and
  1548. 1:19:57refresh you can see that this CSS code
  1549. 1:20:00changes the appearance of all buttons on
  1550. 1:20:02the page and changes their background
  1551. 1:20:05color to
  1552. 1:20:07Red let's do another example we're going
  1553. 1:20:10to go back to our CSS code going to type
  1554. 1:20:13here and press enter to create a new
  1555. 1:20:16line and we're going to add color colon
  1556. 1:20:21white and semicolon let's save
  1557. 1:20:25and
  1558. 1:20:28refresh and this CSS code changes the
  1559. 1:20:31text color of all buttons on the page to
  1560. 1:20:35White let's do one more example we're
  1561. 1:20:38going to type the
  1562. 1:20:40code order Colon none and
  1563. 1:20:45semicolon let's save this and
  1564. 1:20:49refresh and this CSS code removes the
  1565. 1:20:52border for all buttons on the page
  1566. 1:20:56and that's how CSS works we tell the
  1567. 1:20:58computer which elements we want to
  1568. 1:21:00change and then how to change the
  1569. 1:21:03appearance of these
  1570. 1:21:05elements now let's review the syntax
  1571. 1:21:08rules for
  1572. 1:21:09CSS this part of the CSS code is called
  1573. 1:21:12the CSS selector it tells a computer
  1574. 1:21:16which elements we want to change in
  1575. 1:21:19other words it selects which elements to
  1576. 1:21:22change inside the curly brackets we have
  1577. 1:21:25some CSS Styles this tells a computer
  1578. 1:21:28how to change the
  1579. 1:21:30appearance for each style the left side
  1580. 1:21:34is called the CSS property this tells a
  1581. 1:21:37computer what we're changing the right
  1582. 1:21:40side is called the CSS value this tells
  1583. 1:21:43a computer what we're changing the
  1584. 1:21:45property to for example this style
  1585. 1:21:49changes the background color to
  1586. 1:21:52Red we use a colon to separate the
  1587. 1:21:55property and the value and a semicolon
  1588. 1:21:58at the end to tell the computer that
  1589. 1:22:01this is the end of a style so that's the
  1590. 1:22:04basic syntax of CSS we tell the computer
  1591. 1:22:08which elements we want to change and
  1592. 1:22:10then how to change the appearance of
  1593. 1:22:12these
  1594. 1:22:14elements the next thing we're going to
  1595. 1:22:16review is HTML
  1596. 1:22:18attributes right now this CSS selector
  1597. 1:22:22changes the appearance of all but
  1598. 1:22:24buttons on the page however what if we
  1599. 1:22:27only want to change the appearance of
  1600. 1:22:29this one button and we want to style the
  1601. 1:22:32other button differently how do we do
  1602. 1:22:34that we're going to review a feature of
  1603. 1:22:36HTML called attributes attributes change
  1604. 1:22:40the behavior of an element for example
  1605. 1:22:44we're going to go to the opening tag of
  1606. 1:22:46the first button and then here we're
  1607. 1:22:49going to add an HTML attribute by typing
  1608. 1:22:53space
  1609. 1:22:55title equals double quotes and then in
  1610. 1:22:59between the double quotes we're going to
  1611. 1:23:02type tool
  1612. 1:23:05tip so this is an HTML attribute and
  1613. 1:23:09this attribute adds a tool tip to this
  1614. 1:23:12button if you're wondering what a tool
  1615. 1:23:14tip is if we hover over some code in our
  1616. 1:23:17code editor it'll show us this sort of
  1617. 1:23:20popup with more
  1618. 1:23:21information this is called a tool tip
  1619. 1:23:24when we hover over something and this
  1620. 1:23:26shows up so if we save our
  1621. 1:23:29file and refresh the web page and now we
  1622. 1:23:34hover over our button we can see that
  1623. 1:23:37the HTML attribute added a tool tip to
  1624. 1:23:40our button and then inside the double
  1625. 1:23:43quotes is the text inside the tool tip
  1626. 1:23:47for example we can actually change this
  1627. 1:23:49to good
  1628. 1:23:52job and if we save save and
  1629. 1:23:57refresh and hover over our button the
  1630. 1:24:00text between the double quotes is what
  1631. 1:24:02shows up in our tool tip so that's an
  1632. 1:24:05example of an HTML
  1633. 1:24:08attribute now let's review the syntax
  1634. 1:24:11rules for
  1635. 1:24:12attributes we always put attributes on
  1636. 1:24:15the opening tag and we add a space
  1637. 1:24:18between the element name and the
  1638. 1:24:20attribute to separate them on the left
  1639. 1:24:23side is the attribute name this tells
  1640. 1:24:26the computer what we're changing about
  1641. 1:24:28this element on the right side is the
  1642. 1:24:31attribute value this tells a computer
  1643. 1:24:34what we're changing this attribute to
  1644. 1:24:37for example here we're changing the tool
  1645. 1:24:40tip of this element to good job we
  1646. 1:24:44separate the name and value with an
  1647. 1:24:47equal sign and make sure there are no
  1648. 1:24:49spaces around the equal sign and we
  1649. 1:24:52surround the value with double quotes
  1650. 1:24:56now let's go back to the original
  1651. 1:24:58problem how do we style these buttons
  1652. 1:25:01differently we can do this using a very
  1653. 1:25:03important attribute called the class
  1654. 1:25:06attribute let's review that right now
  1655. 1:25:09we're going to go back to our button and
  1656. 1:25:11then here we're going to type space
  1657. 1:25:16class equals double quotes and then
  1658. 1:25:20inside the double quotes we're going to
  1659. 1:25:21type red Das
  1660. 1:25:25button so the class attribute sort of
  1661. 1:25:28adds a label to an element so here we
  1662. 1:25:32labeled this button as a red button and
  1663. 1:25:36now that we labeled this element we can
  1664. 1:25:38select it in
  1665. 1:25:40CSS to do that we're going to go to our
  1666. 1:25:42CSS code we're going to remove the
  1667. 1:25:46button
  1668. 1:25:46selector and replace it
  1669. 1:25:49witht
  1670. 1:25:51red-
  1671. 1:25:52button when we start the selector with a
  1672. 1:25:55DOT this means that now we're looking
  1673. 1:25:58for a class so here we're going to look
  1674. 1:26:01for all elements with the class red dash
  1675. 1:26:04button which is only this element if we
  1676. 1:26:09save and
  1677. 1:26:12refresh now only the elements with the
  1678. 1:26:15class red button gets these Styles we
  1679. 1:26:18can also add the same class to multiple
  1680. 1:26:21elements for example we can go to to our
  1681. 1:26:24other
  1682. 1:26:25button and add the class equals
  1683. 1:26:31red-
  1684. 1:26:32button if we save and
  1685. 1:26:37refresh now these Styles get applied to
  1686. 1:26:40both buttons again because they both
  1687. 1:26:42have the class red-b button if we want
  1688. 1:26:45to style this button differently we just
  1689. 1:26:48give it a different class for example we
  1690. 1:26:51can change this to Yellow yellow dash
  1691. 1:26:55button and then in our CSS we can add
  1692. 1:26:59another selector so we can type dot this
  1693. 1:27:03means we're going to select a class and
  1694. 1:27:06we're going to select the yellow Das
  1695. 1:27:10button class open curly bracket close
  1696. 1:27:13curly bracket and then inside here we
  1697. 1:27:17can add some styles for the yellow
  1698. 1:27:19button for
  1699. 1:27:21example background Das color colon
  1700. 1:27:26yellow and
  1701. 1:27:28semicolon now let's save and
  1702. 1:27:33refresh and that's how we use the class
  1703. 1:27:36attribute in HTML to modify specific
  1704. 1:27:40elements the final thing we need to
  1705. 1:27:42review is something called the HTML
  1706. 1:27:45structure let's go back to our website
  1707. 1:27:48we're going to find a blank area and
  1708. 1:27:51then
  1709. 1:27:51rightclick and then click inspect
  1710. 1:27:56so we already learned about the console
  1711. 1:28:00tab here we can write and run JavaScript
  1712. 1:28:03code now we're going to go to the
  1713. 1:28:06elements tab which is for HTML and CSS
  1714. 1:28:10so in the elements tab we can see all
  1715. 1:28:13the HTML on the page as well as all the
  1716. 1:28:17CSS on the page and we can play around
  1717. 1:28:20with the CSS if we
  1718. 1:28:22want now if we go back to to the
  1719. 1:28:25HTML you'll notice that our web page has
  1720. 1:28:28some elements that we didn't add before
  1721. 1:28:31the HTML element the head element and
  1722. 1:28:34the body
  1723. 1:28:36element so this is the structure that
  1724. 1:28:38we're supposed to follow when writing
  1725. 1:28:41HTML in our code we didn't follow the
  1726. 1:28:44structure so the browser actually adds
  1727. 1:28:47it for us
  1728. 1:28:48automatically however it's still best
  1729. 1:28:50practice to follow this structure in our
  1730. 1:28:53code because it gives us access to some
  1731. 1:28:55more features of
  1732. 1:28:57HTML let's review the HTML structure and
  1733. 1:29:00add it to our
  1734. 1:29:02code we're going to type up here and
  1735. 1:29:05we're going to create some new
  1736. 1:29:07lines and now every HTML file is going
  1737. 1:29:11to start with less than
  1738. 1:29:14exclamation dock type in capitals and
  1739. 1:29:18then space
  1740. 1:29:20HTML and greater
  1741. 1:29:22than this is not an HTML element it's a
  1742. 1:29:27special line of code that tells a
  1743. 1:29:28browser to use a modern version of
  1744. 1:29:31HTML if we don't have this the browser
  1745. 1:29:34might fall back to an older version of
  1746. 1:29:36HTML that has less
  1747. 1:29:39features next we're going to create an
  1748. 1:29:42HTML element so less than
  1749. 1:29:46HTML and greater
  1750. 1:29:49than the HTML element represents the
  1751. 1:29:52entire web page
  1752. 1:29:54so everything on the web page should go
  1753. 1:29:57inside this
  1754. 1:29:58element and then inside the HTML element
  1755. 1:30:02we're also going to split it up into two
  1756. 1:30:05sections the head
  1757. 1:30:08element and the body
  1758. 1:30:13element so the body element is supposed
  1759. 1:30:16to contain everything that is visible on
  1760. 1:30:18the page in our example that would be
  1761. 1:30:22the buttons and the parag graph so let's
  1762. 1:30:25actually select this
  1763. 1:30:27code and we're going to
  1764. 1:30:29rightclick and
  1765. 1:30:32cut and then inside the
  1766. 1:30:35body we're going to right click and
  1767. 1:30:41paste the head element contains
  1768. 1:30:45information about the page in general
  1769. 1:30:48everything that is not visible on the
  1770. 1:30:50page goes inside the
  1771. 1:30:52head in our examp example that would be
  1772. 1:30:55the style element so let's select this
  1773. 1:30:58as
  1774. 1:30:59well we're going to rightclick and
  1775. 1:31:04cut and then inside the
  1776. 1:31:07head we're going to right click and
  1777. 1:31:12paste and at the bottom let's just clean
  1778. 1:31:15up by removing all of the extra new
  1779. 1:31:18lines so this is a structure that we're
  1780. 1:31:21supposed to follow in all of our HTML
  1781. 1:31:24files as you can see this structure uses
  1782. 1:31:27a lot of nesting or elements inside
  1783. 1:31:30elements to organize our
  1784. 1:31:33code okay so what are the benefits of
  1785. 1:31:36following this structure one benefit is
  1786. 1:31:40that there are other elements that we
  1787. 1:31:41can use inside the head for example
  1788. 1:31:45let's type here press enter and we're
  1789. 1:31:48going to add the title element so the
  1790. 1:31:53title element sets the text in the
  1791. 1:31:56tabs inside we can type HTML CSS review
  1792. 1:32:03if we save and refresh the title will
  1793. 1:32:07show up in the
  1794. 1:32:09tab so this is an example of an element
  1795. 1:32:12that we can now add in the
  1796. 1:32:14head another benefit that we get from
  1797. 1:32:17this structure is the ability to
  1798. 1:32:19automatically refresh our web page when
  1799. 1:32:22we change our code
  1800. 1:32:24so far every time we change our code we
  1801. 1:32:28have to save and then manually refresh
  1802. 1:32:31the web page we can actually automate
  1803. 1:32:34this by following the structure and then
  1804. 1:32:37installing a VSS code
  1805. 1:32:40extension so let's click the extension
  1806. 1:32:43area and we're going to search for the
  1807. 1:32:46extension live
  1808. 1:32:49server and click install
  1809. 1:32:54once we have that installed we're going
  1810. 1:32:55to change a
  1811. 1:32:57setting let's open the settings by
  1812. 1:33:00clicking this icon in the bottom left
  1813. 1:33:03and then click
  1814. 1:33:06settings at the top we're going to
  1815. 1:33:08search for live
  1816. 1:33:12server and we're going to look for a
  1817. 1:33:14setting called live server custom
  1818. 1:33:18browser and we're going to change this
  1819. 1:33:21to
  1820. 1:33:22Chrome now now if Google Chrome is
  1821. 1:33:25already your default web browser you
  1822. 1:33:27don't need to do this step but it's nice
  1823. 1:33:29to
  1824. 1:33:30know now let's close the
  1825. 1:33:33settings and go back to our
  1826. 1:33:37code next instead of opening this file
  1827. 1:33:40directly in the browser like before
  1828. 1:33:43we're going to use live server to open
  1829. 1:33:46this file to do that we're going to
  1830. 1:33:49rightclick this file in our code editor
  1831. 1:33:52and then click open open with live
  1832. 1:33:56server if we open a file with live
  1833. 1:33:58server this web page will automatically
  1834. 1:34:01refresh when we change our
  1835. 1:34:04code for example we can go here and
  1836. 1:34:08change the background color to Green if
  1837. 1:34:12we save live server will automatically
  1838. 1:34:15refresh the web page so we don't have to
  1839. 1:34:18do that back and forth
  1840. 1:34:20anymore let's just change it back to Red
  1841. 1:34:23so that the class last name makes sense
  1842. 1:34:25and
  1843. 1:34:26save and we can also close the previous
  1844. 1:34:31tab so this is the HTML structure it
  1845. 1:34:35gives us access to more features of
  1846. 1:34:38HTML and this is all we need to review
  1847. 1:34:41about HTML and CSS for now if you want
  1848. 1:34:45to learn more you can check out my HTML
  1849. 1:34:47and CSS full course in the video
  1850. 1:34:52description by the way to find the links
  1851. 1:34:54mentioned in each lesson you can click
  1852. 1:34:57here in the
  1853. 1:35:02description and this page contains all
  1854. 1:35:05the links mentioned in each
  1855. 1:35:10lesson before we add JavaScript to our
  1856. 1:35:12website we're going to review some VSS
  1857. 1:35:15code
  1858. 1:35:16setup so you'll notice that many of
  1859. 1:35:18these lines have spaces at the front
  1860. 1:35:22these spaces are called in indents and
  1861. 1:35:24indents make our code easier to read for
  1862. 1:35:27example we can easily tell that this
  1863. 1:35:30code is inside the brackets because it
  1864. 1:35:33has an extra indent in the
  1865. 1:35:36front if we didn't have indents here's
  1866. 1:35:39what the code would look like it's a lot
  1867. 1:35:41harder to
  1868. 1:35:42read to add an indent we can type at the
  1869. 1:35:46beginning of the line and press
  1870. 1:35:49tab to remove an indent we can press
  1871. 1:35:52shift tab or just use
  1872. 1:35:55backspace now by default VSS code uses
  1873. 1:35:59four spaces per indet however in HTML
  1874. 1:36:03CSS and JavaScript we usually use two
  1875. 1:36:06spaces per indet so we're going to
  1876. 1:36:09update this setting let's go to our
  1877. 1:36:11settings by clicking this icon in the
  1878. 1:36:14bottom
  1879. 1:36:15left and then click
  1880. 1:36:18settings and at the top we're going to
  1881. 1:36:21search for tab
  1882. 1:36:24and we're going to change the tab size
  1883. 1:36:27from 4 to
  1884. 1:36:302 now let's close the
  1885. 1:36:32settings and we're also going to change
  1886. 1:36:34the indents for this file we're going to
  1887. 1:36:37go down here and click select
  1888. 1:36:41indent and we're going to choose indent
  1889. 1:36:43using
  1890. 1:36:44spaces and then click
  1891. 1:36:48two and now if we type at the start of a
  1892. 1:36:50line and press tab it will only add two
  1893. 1:36:54spaces per
  1894. 1:36:56indent so let's reformat our code to use
  1895. 1:36:59two spaces per indent instead of four
  1896. 1:37:03first we're going to remove all the
  1897. 1:37:04indents in our code a shortcut for this
  1898. 1:37:07is to select all of these
  1899. 1:37:10lines and press shift tab to remove one
  1900. 1:37:14indent for each line so we're going to
  1901. 1:37:17keep pressing shift tab until we remove
  1902. 1:37:20all the
  1903. 1:37:22indents next next we're going to add the
  1904. 1:37:24indents back using a similar shortcut
  1905. 1:37:28we're going to select the head
  1906. 1:37:30element and press tab to add one indent
  1907. 1:37:34for each line and then select everything
  1908. 1:37:37inside the head and press tab to add
  1909. 1:37:40another indent and then select
  1910. 1:37:43everything inside style and press Tab
  1911. 1:37:47and then inside each of these brackets
  1912. 1:37:49we're going to select the lines press
  1913. 1:37:51Tab and then here press tab we'll do the
  1914. 1:37:55same thing for the body element we'll
  1915. 1:37:58select all these lines and press tab
  1916. 1:38:02we'll select everything inside the body
  1917. 1:38:05press Tab and then inside each of these
  1918. 1:38:08elements we're going to press Tab and
  1919. 1:38:11press
  1920. 1:38:12tab now if we save our website will look
  1921. 1:38:16the same because all we did was adjust
  1922. 1:38:19the
  1923. 1:38:20indents so there's one other setting in
  1924. 1:38:23vs code that we're going to set up which
  1925. 1:38:25is line wrapping if you look at this
  1926. 1:38:28line of code you can see that it's
  1927. 1:38:30really long and we need to scroll
  1928. 1:38:33horizontally to see the rest of the line
  1929. 1:38:36instead we're going to make this line
  1930. 1:38:38wrap around if it gets too long let's go
  1931. 1:38:41back into our settings by clicking here
  1932. 1:38:44and then click the
  1933. 1:38:46settings and then up here we're going to
  1934. 1:38:49search for
  1935. 1:38:51wrap and we're going to look for editor
  1936. 1:38:54word wrap and we're going to turn this
  1937. 1:38:58on now if we close our settings and go
  1938. 1:39:01back to our
  1939. 1:39:02code this line which is really long will
  1940. 1:39:05wrap around to the next line instead of
  1941. 1:39:08us having to horizontally
  1942. 1:39:10scroll so that's the vs code setup that
  1943. 1:39:13we need two spaces per indent and line
  1944. 1:39:18wrapping finally we're going to add
  1945. 1:39:20JavaScript to this website so we learned
  1946. 1:39:23that we can use the console to write and
  1947. 1:39:26run JavaScript well there's actually
  1948. 1:39:28another way to run JavaScript which is
  1949. 1:39:31inside an HTML file let's learn how to
  1950. 1:39:34do that so we learned that HTML has a
  1951. 1:39:38special element that runs CSS code
  1952. 1:39:40called the style element while HTML has
  1953. 1:39:44another special element that runs
  1954. 1:39:46JavaScript code and this element is
  1955. 1:39:49called the script
  1956. 1:39:51element so inside
  1957. 1:39:54body at the bottom of the body we're
  1958. 1:39:57going to add less than script greater
  1959. 1:40:01than and the closing tag less than SL
  1960. 1:40:05script greater than and inside the
  1961. 1:40:08script element we can put some
  1962. 1:40:09JavaScript code we're going to add the
  1963. 1:40:12code alert Open Bracket quote hello
  1964. 1:40:18quote close bracket and semicolon if we
  1965. 1:40:22save
  1966. 1:40:24live server will refresh the page and
  1967. 1:40:26now you notice that the computer now
  1968. 1:40:28runs our JavaScript code just like it
  1969. 1:40:31did in the console it creates a popup
  1970. 1:40:34with the text hello
  1971. 1:40:37inside and we usually put the script
  1972. 1:40:39element at the bottom of the body
  1973. 1:40:42because we want the web page to be
  1974. 1:40:43created first and then we use JavaScript
  1975. 1:40:46to make it
  1976. 1:40:48interactive for the rest of this course
  1977. 1:40:51we're going to run our JavaScript code
  1978. 1:40:53using using an HTML file the console
  1979. 1:40:56that we used in the previous lessons is
  1980. 1:40:58more of a way to try out JavaScript code
  1981. 1:41:02we wouldn't use a console for a real
  1982. 1:41:05website in addition to the script
  1983. 1:41:07element we have another way to run
  1984. 1:41:10JavaScript code inside HTML and that's
  1985. 1:41:13by using an attribute let's go back to
  1986. 1:41:17our first button and here we're going to
  1987. 1:41:20add another attribute so let's type
  1988. 1:41:23space and this time we're going to add
  1989. 1:41:26the
  1990. 1:41:28onclick attribute equals double
  1991. 1:41:32quotes So this attribute will run
  1992. 1:41:36JavaScript code whenever we click this
  1993. 1:41:38button that's what onclick means between
  1994. 1:41:42the double quotes we can write some
  1995. 1:41:45JavaScript for example let's press enter
  1996. 1:41:49enter and then here we're going to type
  1997. 1:41:52alert
  1998. 1:41:53Open Bracket
  1999. 1:41:55quote good job quote close bracket and
  2000. 1:42:02semicolon now if we
  2001. 1:42:04save live server will refresh the page
  2002. 1:42:07and run this line of JavaScript code
  2003. 1:42:10let's press okay and now if we click
  2004. 1:42:14this
  2005. 1:42:15button it will run this line of
  2006. 1:42:17JavaScript code and create a popup with
  2007. 1:42:20a text good job inside
  2008. 1:42:25so those are two places where we can run
  2009. 1:42:27JavaScript code the script element and
  2010. 1:42:31the onclick attribute the code in the
  2011. 1:42:34script element runs first when the page
  2012. 1:42:37is loaded the code in the onclick
  2013. 1:42:41attribute runs after when we click the
  2014. 1:42:44button on the
  2015. 1:42:46page and that's how we use HTML CSS and
  2016. 1:42:51JavaScript together on a website
  2017. 1:42:54notice that everything starts from
  2018. 1:42:56HTML HTML first creates the website and
  2019. 1:43:01then inside the HTML we can run CSS code
  2020. 1:43:05to change the appearance and we can run
  2021. 1:43:09JavaScript code to make the website
  2022. 1:43:13interactive now let's learn some new
  2023. 1:43:15features of JavaScript that we haven't
  2024. 1:43:17seen yet in this course the next feature
  2025. 1:43:20we're going to learn is called comments
  2026. 1:43:23comments are pieces of code that the
  2027. 1:43:25computer ignores so let's create our
  2028. 1:43:27first comment we're going to go down to
  2029. 1:43:30the script element
  2030. 1:43:32here and type forward slash forward
  2031. 1:43:36slash so this is a comment anything
  2032. 1:43:40after the double slash will be ignored
  2033. 1:43:42by the computer so we can type anything
  2034. 1:43:45we want like this is a
  2035. 1:43:50comment if we save it ignores this line
  2036. 1:43:55and creates the popup in the next
  2037. 1:43:57line now let's close this popup and
  2038. 1:44:00learn why we use
  2039. 1:44:02comments comments are useful for
  2040. 1:44:05providing more information for people
  2041. 1:44:07reading our code for example we can
  2042. 1:44:11create a comment above this code like
  2043. 1:44:16this code creates a
  2044. 1:44:20popup this comment helps others and
  2045. 1:44:23ourselves understand what this code does
  2046. 1:44:27so that's one way we use
  2047. 1:44:29comments another way we use comments is
  2048. 1:44:32if we don't want to run some code
  2049. 1:44:34anymore but we also don't want to delete
  2050. 1:44:37it for example this code creates a popup
  2051. 1:44:42every time we refresh the page and this
  2052. 1:44:45can be
  2053. 1:44:46annoying so I want to stop running this
  2054. 1:44:49code but I also don't want to delete it
  2055. 1:44:52because you might want to review this
  2056. 1:44:53code
  2057. 1:44:55later so what we can do here is to add a
  2058. 1:44:58double slash in front of this line to
  2059. 1:45:01turn this code into a
  2060. 1:45:04comment now if we save the computer will
  2061. 1:45:08ignore this line of code and not create
  2062. 1:45:10the
  2063. 1:45:12popup this is called commenting out the
  2064. 1:45:15code it's useful if we don't want to run
  2065. 1:45:18some code anymore but we also don't want
  2066. 1:45:20to delete it for informational purposes
  2067. 1:45:24so JavaScript has another Syntax for
  2068. 1:45:27writing comments and that syntax is
  2069. 1:45:31slash star and star
  2070. 1:45:34slash here anything between the stars is
  2071. 1:45:38a comment this is called a multi-line
  2072. 1:45:42comment because between the Stars we can
  2073. 1:45:44write a comment on multiple lines for
  2074. 1:45:48example we can type multi enter line
  2075. 1:45:53Line enter
  2076. 1:45:56comment on the other hand double slash
  2077. 1:45:59is known as a single line
  2078. 1:46:02comment now just like JavaScript HTML
  2079. 1:46:06and CSS also have comments but they use
  2080. 1:46:10a different
  2081. 1:46:11syntax if we scroll up to our
  2082. 1:46:15HTML here we can type less than
  2083. 1:46:19exclamation
  2084. 1:46:21d-h and we also need dh- greater than so
  2085. 1:46:25anything between these double dashes is
  2086. 1:46:28a comment for example this is a
  2087. 1:46:34comment in CSS to create a comment we
  2088. 1:46:38can type slash star star slash so it's
  2089. 1:46:44the same multi-line comment as
  2090. 1:46:47JavaScript between the Stars we can add
  2091. 1:46:50a comment so this
  2092. 1:46:53is a
  2093. 1:46:55comment if we save our
  2094. 1:46:57file nothing changes on our web page
  2095. 1:47:01because comments are ignored by the
  2096. 1:47:03computer the last feature of JavaScript
  2097. 1:47:06we're going to learn is called
  2098. 1:47:08console.log let's go back to our
  2099. 1:47:11code and at the bottom in the script
  2100. 1:47:15element we're going to type the code 2 +
  2101. 1:47:202 so we learned that this code in Java
  2102. 1:47:23script does some math however if we save
  2103. 1:47:26this
  2104. 1:47:28file where is the result of this math so
  2105. 1:47:32we might think that the result will show
  2106. 1:47:34up in the console but if we
  2107. 1:47:37rightclick click
  2108. 1:47:40inspect and then click the
  2109. 1:47:42console you'll notice that the result
  2110. 1:47:45doesn't show up here either and that's
  2111. 1:47:48because before we were running 2 + 2
  2112. 1:47:52inside the console so the result shows
  2113. 1:47:56up in the console but now we're not
  2114. 1:47:59running this code in the console anymore
  2115. 1:48:01we're running this code on the web page
  2116. 1:48:04so how do we see this
  2117. 1:48:07result the solution is we're going to
  2118. 1:48:09redirect this result back to the console
  2119. 1:48:13to do that we're going to use this piece
  2120. 1:48:16of
  2121. 1:48:17code
  2122. 1:48:20console.log Open Bracket close bracket
  2123. 1:48:23and
  2124. 1:48:25semicolon console.log will display
  2125. 1:48:28whatever's between the brackets back in
  2126. 1:48:31the console so if we move this code
  2127. 1:48:34between the brackets so we select this
  2128. 1:48:38and then right click cut and then
  2129. 1:48:41between the brackets right click paste
  2130. 1:48:45and
  2131. 1:48:47save the result of this code is
  2132. 1:48:49displayed back in the console
  2133. 1:48:53and console.log works for any code that
  2134. 1:48:56we learned so far for example we can use
  2135. 1:49:00strings so let's create a new line and
  2136. 1:49:04type
  2137. 1:49:07console.log Open Bracket close bracket
  2138. 1:49:10semicolon and then between the brackets
  2139. 1:49:13let's type some
  2140. 1:49:14strings quote Su quote plus quote text
  2141. 1:49:22quote
  2142. 1:49:23and save this
  2143. 1:49:25file again it will display the result of
  2144. 1:49:28whatever's between the brackets back in
  2145. 1:49:31the
  2146. 1:49:32console so that's how we see the result
  2147. 1:49:35of JavaScript code that is running in a
  2148. 1:49:37file we use
  2149. 1:49:40console.log and that's the end of this
  2150. 1:49:43lesson in this lesson we reviewed the
  2151. 1:49:46basics of HTML and CSS we set up our
  2152. 1:49:50code editor vs code we learned how to
  2153. 1:49:54load JavaScript inside an HTML file
  2154. 1:49:58using the script element and the onclick
  2155. 1:50:01attribute we learned how to create
  2156. 1:50:04comments and we learned how to use
  2157. 1:50:10console.log thanks for watching this
  2158. 1:50:12course so far if you want to support
  2159. 1:50:14this channel you can check out the
  2160. 1:50:16premium version of my courses just like
  2161. 1:50:18this one where you can get a certificate
  2162. 1:50:21of completion and a learning platform if
  2163. 1:50:24you're interested in extra videos from
  2164. 1:50:26me more in-depth videos and behind thes
  2165. 1:50:29scenes content you can check out these
  2166. 1:50:32links here you can also support the
  2167. 1:50:34channel by liking the video subscribing
  2168. 1:50:37and sharing this video with others
  2169. 1:50:40thanks again and let's continue with the
  2170. 1:50:44course here are some exercises you can
  2171. 1:50:46try on your own to practice what we
  2172. 1:50:49learned in this lesson
  2173. 1:51:10in this lesson we're going to learn the
  2174. 1:51:12next feature of JavaScript called
  2175. 1:51:14variables and we're going to use
  2176. 1:51:17variables to build a simple version of
  2177. 1:51:19the cart quantity feature of the final
  2178. 1:51:21project
  2179. 1:51:23first let's create a new HTML file just
  2180. 1:51:27for learning variables we're going to go
  2181. 1:51:30to our code editor and click this icon
  2182. 1:51:33to show our files and then click this
  2183. 1:51:36icon to create a new
  2184. 1:51:39file and we're going to name this file
  2185. 1:51:4405-
  2186. 1:51:46variables.
  2187. 1:51:48HTML and press
  2188. 1:51:50enter so the 05 just means that this
  2189. 1:51:53file is for lesson
  2190. 1:51:55five let's also add the lesson number to
  2191. 1:51:58website.
  2192. 1:52:00HTML we'll select this file and then
  2193. 1:52:04rightclick
  2194. 1:52:06rename and then at the front We'll add
  2195. 1:52:1004- and press
  2196. 1:52:13enter next we're going to copy all the
  2197. 1:52:16code in website. HTML to variables.
  2198. 1:52:21HTML so let's click in this file and
  2199. 1:52:24we're going to select all of this code
  2200. 1:52:26by typing control a on Windows or
  2201. 1:52:30command a on
  2202. 1:52:32Mac and then we're going to
  2203. 1:52:35rightclick
  2204. 1:52:36copy and then inside variables.
  2205. 1:52:40HTML rightclick and
  2206. 1:52:44paste let's click this icon to hide our
  2207. 1:52:47files for now and we'll prepare this
  2208. 1:52:50file for this lesson so let's scroll up
  2209. 1:52:53to the top and first we're going to
  2210. 1:52:56change the title to
  2211. 1:53:00variables next we're going to delete all
  2212. 1:53:03the CSS because we're going to start
  2213. 1:53:05over and we're going to delete all the
  2214. 1:53:08HTML except for the script
  2215. 1:53:13element and let's delete all the
  2216. 1:53:15JavaScript from the previous
  2217. 1:53:18lesson and finally let's save
  2218. 1:53:22and we'll open this file in our browser
  2219. 1:53:25by right clicking and then open with
  2220. 1:53:28live
  2221. 1:53:30server and now we can close the previous
  2222. 1:53:32tab and the previous code for
  2223. 1:53:36now now we're ready to learn
  2224. 1:53:39variables first of all what is a
  2225. 1:53:43variable a variable is like a container
  2226. 1:53:46we can save a value like a number or a
  2227. 1:53:49string inside a variable and then then
  2228. 1:53:52use it later Let's do an example and
  2229. 1:53:56create our first
  2230. 1:53:57variable inside the script element we're
  2231. 1:54:00going to type the code let and space the
  2232. 1:54:05word let creates a new variable and now
  2233. 1:54:09we're going to choose a name for our new
  2234. 1:54:11variable let's name this variable
  2235. 1:54:16one so this creates a variable or a
  2236. 1:54:19container named variable one and now we
  2237. 1:54:22can save a value inside this variable to
  2238. 1:54:27do that we're going to type space equals
  2239. 1:54:31space and then a value like a number or
  2240. 1:54:34a string so let's save the number three
  2241. 1:54:38inside and we'll type a semicolon at the
  2242. 1:54:41end so here we use the let to create a
  2243. 1:54:45new variable we named this variable
  2244. 1:54:49variable one and we saved the value
  2245. 1:54:51three inside this
  2246. 1:54:54variable now that we saved this value we
  2247. 1:54:57can use it later for example let's type
  2248. 1:55:01a new line and type the code
  2249. 1:55:05console. log Open Bracket close bracket
  2250. 1:55:09and
  2251. 1:55:10semicolon remember from the previous
  2252. 1:55:12lesson that this code displays
  2253. 1:55:14whatever's inside the brackets in the
  2254. 1:55:17console so far we put a value inside
  2255. 1:55:21like the number
  2256. 1:55:22two if we save and then go to our
  2257. 1:55:26website and open the console so right
  2258. 1:55:29click
  2259. 1:55:31inspect and then click the
  2260. 1:55:34console we can see that the number two
  2261. 1:55:37was displayed in the
  2262. 1:55:39console so now let's try replacing this
  2263. 1:55:41value with the variable that we created
  2264. 1:55:44above so instead of two we're going to
  2265. 1:55:48console.log variable one
  2266. 1:55:52if we
  2267. 1:55:53save notice that it now displays three
  2268. 1:55:58so it's displaying whatever value is
  2269. 1:56:00saved inside the variable as you can see
  2270. 1:56:04we can save a value inside a variable
  2271. 1:56:07and then use that variable later
  2272. 1:56:10on let's do another example we're going
  2273. 1:56:13to create another variable so we're
  2274. 1:56:16going to create some new lines and we'll
  2275. 1:56:18type
  2276. 1:56:20let and this time let's name this
  2277. 1:56:23variable
  2278. 1:56:25calculation and we're going to type
  2279. 1:56:27space equals space to save a value
  2280. 1:56:31inside this new variable and this time
  2281. 1:56:34instead of just a number we're going to
  2282. 1:56:36save a
  2283. 1:56:37calculation 2 + 2 and semicolon so this
  2284. 1:56:43will calculate 2 + 2 and then save the
  2285. 1:56:46result which is four into the
  2286. 1:56:50variable if we call
  2287. 1:56:53console. log Open
  2288. 1:56:56Bracket
  2289. 1:56:59calculation and semicolon and
  2290. 1:57:03save it will show us that the value for
  2291. 1:57:06is saved inside
  2292. 1:57:08calculation now let's try this code
  2293. 1:57:11we're going to type a new line and then
  2294. 1:57:16console. log Open Bracket and
  2295. 1:57:19calculation again
  2296. 1:57:22and plus 2 and
  2297. 1:57:26semicolon so here we're using a variable
  2298. 1:57:29inside a
  2299. 1:57:31calculation what this does is that it
  2300. 1:57:33takes whatever value is saved inside the
  2301. 1:57:36variable right now it is four and then
  2302. 1:57:39substitutes that value into the
  2303. 1:57:41calculation so this will be 4 + 2 which
  2304. 1:57:45equals
  2305. 1:57:46six if we
  2306. 1:57:48save we can see that in the console it
  2307. 1:57:51display blade
  2308. 1:57:53six so we can use a variable wherever we
  2309. 1:57:57use a value and JavaScript will
  2310. 1:57:59substitute whatever is inside the
  2311. 1:58:02variable into the
  2312. 1:58:04code let's do another example we're
  2313. 1:58:07going to type some new lines and create
  2314. 1:58:09a new variable again using
  2315. 1:58:13let and we're going to name this
  2316. 1:58:15variable
  2317. 1:58:17result and we're going to make it equal
  2318. 1:58:20to calcul
  2319. 1:58:23ation plus 2 and
  2320. 1:58:28semicolon so this does the same thing as
  2321. 1:58:31a code before it will take whatever
  2322. 1:58:33value is inside calculation which is
  2323. 1:58:36four and substitute it into this code so
  2324. 1:58:39we get 4 + 2 =
  2325. 1:58:426 but this time we have an extra step
  2326. 1:58:46we're going to save this result which is
  2327. 1:58:48six into another
  2328. 1:58:50variable so so if we
  2329. 1:58:55console.log
  2330. 1:58:58result and
  2331. 1:59:00save we can see that six was saved into
  2332. 1:59:04the result
  2333. 1:59:06variable again we can use a variable
  2334. 1:59:09wherever we use a value now in addition
  2335. 1:59:13to numbers we can save any type of value
  2336. 1:59:15in a variable including
  2337. 1:59:18strings for example let's create a new
  2338. 1:59:21variable using
  2339. 1:59:23let and let's name this
  2340. 1:59:27message and we're going to save inside
  2341. 1:59:29this variable a string this time so
  2342. 1:59:33quote hello quote and
  2343. 1:59:37semicolon again we're going to
  2344. 1:59:39console.log this variable so
  2345. 1:59:44console.log and
  2346. 1:59:47message and a semicolon at the end if we
  2347. 1:59:50save
  2348. 1:59:52it will show us that the string hello is
  2349. 1:59:54saved inside this
  2350. 1:59:57variable okay now that we learned how
  2351. 2:00:00variables work let's learn the syntax
  2352. 2:00:02rules for
  2353. 2:00:04variables the word let creates a new
  2354. 2:00:07variable and then we give the variable a
  2355. 2:00:10name so we can name variables almost
  2356. 2:00:13anything we want except for a few
  2357. 2:00:15restrictions number one we can't use
  2358. 2:00:18special words like let as a variable
  2359. 2:00:21name
  2360. 2:00:22that's because let already has a special
  2361. 2:00:25meaning in JavaScript it creates a
  2362. 2:00:27variable so let is a reserved word
  2363. 2:00:31however we can use let one or let two as
  2364. 2:00:34variable names we just can't use
  2365. 2:00:37let number two we can't start a variable
  2366. 2:00:41name with a number if we start with a
  2367. 2:00:44number JavaScript will think this is a
  2368. 2:00:46number instead of a variable name
  2369. 2:00:49however we can use numbers in the middle
  2370. 2:00:52or the
  2371. 2:00:53end lastly we can't use most special
  2372. 2:00:56characters like these or a space in a
  2373. 2:01:00variable name however there are two
  2374. 2:01:03special characters we can use dollar and
  2375. 2:01:06underscore so those are the rules for
  2376. 2:01:09naming
  2377. 2:01:10variables and then to save something
  2378. 2:01:13inside a variable we just use the equal
  2379. 2:01:15sign and then the value we want to save
  2380. 2:01:19and that's it after we create a variable
  2381. 2:01:22we can start using it in the rest of our
  2382. 2:01:24code now there's one last part of the
  2383. 2:01:27syntax that we haven't learned which is
  2384. 2:01:29the semicolon we've seen semicolons in
  2385. 2:01:32many places already in JavaScript a
  2386. 2:01:36semicolon means this is the end of an
  2387. 2:01:39instruction it's similar to a period in
  2388. 2:01:42English in English a period means this
  2389. 2:01:45is the end of a
  2390. 2:01:47sentence so here this semicolon tells
  2391. 2:01:51Java script that this is the end of an
  2392. 2:01:54instruction and now after this we can
  2393. 2:01:57add another instruction like
  2394. 2:02:01console. log Open Bracket and then the
  2395. 2:02:05string semicolon and a semicolon at the
  2396. 2:02:09end if we
  2397. 2:02:11save it will run these two instructions
  2398. 2:02:14one after another without any
  2399. 2:02:17problems so if we didn't have the
  2400. 2:02:19semicolon here and and we save it
  2401. 2:02:23wouldn't work because JavaScript thinks
  2402. 2:02:26that this entire line is one
  2403. 2:02:29instruction so we need semicolons to
  2404. 2:02:32separate different instructions in
  2405. 2:02:35JavaScript if we
  2406. 2:02:37save everything goes back to
  2407. 2:02:39normal so JavaScript is a little special
  2408. 2:02:43because it has a feature called
  2409. 2:02:45semicolon
  2410. 2:02:46insertion that means it will try to
  2411. 2:02:48insert the last semicolon in a line of
  2412. 2:02:51code automat automatically for example
  2413. 2:02:54if we removed the last
  2414. 2:02:56semicolon and
  2415. 2:02:59save this code will work without that
  2416. 2:03:01semicolon and that's because JavaScript
  2417. 2:03:04inserts the last semicolon
  2418. 2:03:07automatically however a lot of companies
  2419. 2:03:10still prefer to use semicolons in their
  2420. 2:03:12JavaScript code because semicolon
  2421. 2:03:14insertion can sometimes put the
  2422. 2:03:16semicolon in the wrong place for this
  2423. 2:03:19reason we're also going to use sem
  2424. 2:03:21colons in this course to help you get
  2425. 2:03:24used to JavaScript code with
  2426. 2:03:27semicolons next we're going to learn how
  2427. 2:03:29to change the value that is saved inside
  2428. 2:03:32a variable for example here we save the
  2429. 2:03:36number three inside variable one now
  2430. 2:03:39we're going to change the value saved
  2431. 2:03:41inside variable one to do that we're
  2432. 2:03:44going to go to the bottom and create
  2433. 2:03:47some new
  2434. 2:03:48lines and we're going to type the
  2435. 2:03:50variable name again so
  2436. 2:03:53variable one and now to change the value
  2437. 2:03:56saved inside we're just going to type
  2438. 2:03:59the equal sign and then a different
  2439. 2:04:02value like five and
  2440. 2:04:06semicolon and now if we console.log this
  2441. 2:04:09variable
  2442. 2:04:13console.log variable 1 and semicolon and
  2443. 2:04:18save it will show us that five is now
  2444. 2:04:21now saved inside variable
  2445. 2:04:23one notice also that if we scroll up to
  2446. 2:04:27the top the first console.log still
  2447. 2:04:30displays the value three and that's
  2448. 2:04:33because at this point in the code
  2449. 2:04:35variable one still contained three we
  2450. 2:04:38didn't update the variable yet so when
  2451. 2:04:41we first saved a value inside this
  2452. 2:04:43variable this is called assigning a
  2453. 2:04:46value to a
  2454. 2:04:48variable then when we change the value
  2455. 2:04:50inside this varable iable this is called
  2456. 2:04:53reassigning a value to a
  2457. 2:04:56variable now let's learn the syntax
  2458. 2:04:58rules for reassigning a value notice
  2459. 2:05:02that we don't use the word let when
  2460. 2:05:04reassigning that's because let creates a
  2461. 2:05:07new
  2462. 2:05:08variable so if we try to use let again
  2463. 2:05:12it would try to create a new variable
  2464. 2:05:14named variable one however variable one
  2465. 2:05:18already exists and we can't create two
  2466. 2:05:21VAR variables with the same name so this
  2467. 2:05:24would cause an error instead to reassign
  2468. 2:05:28a variable we just type out the variable
  2469. 2:05:31name and make it equal to something else
  2470. 2:05:35so that's the Syntax for reassigning a
  2471. 2:05:39variable now we're going to do one last
  2472. 2:05:41example before working on the project
  2473. 2:05:44let's go to the end of our code we're
  2474. 2:05:47going to type some new
  2475. 2:05:49lines and this time we're going to re
  2476. 2:05:51reassign variable one again so we can
  2477. 2:05:54reassign a variable as many times as we
  2478. 2:05:57want so let's type variable 1 and then
  2479. 2:06:03equals and we're going to type variable
  2480. 2:06:071 + 1 and
  2481. 2:06:11semicolon so we learned earlier that we
  2482. 2:06:14can use a variable inside a calculation
  2483. 2:06:17and it will substitute whatever value is
  2484. 2:06:20inside the variable
  2485. 2:06:22here we're also using a variable inside
  2486. 2:06:24the calculation but we're using this
  2487. 2:06:27variable
  2488. 2:06:28itself so it's essentially taking this
  2489. 2:06:31variable increasing it by one and then
  2490. 2:06:34saving it
  2491. 2:06:36back if we
  2492. 2:06:39console.log this variable variable
  2493. 2:06:43one and
  2494. 2:06:46save variable one now contains six so it
  2495. 2:06:50took the previous value in variable one
  2496. 2:06:53which was five added one and then saved
  2497. 2:06:56it
  2498. 2:06:57back so this is how we increase the
  2499. 2:07:00value of a variable by a certain
  2500. 2:07:04number okay now that we know how to
  2501. 2:07:06create and reassign variables we're
  2502. 2:07:09ready to work on a project so I actually
  2503. 2:07:12prepared a project that we can do for
  2504. 2:07:14this lesson and we can see this project
  2505. 2:07:17by going to our browser clicking at the
  2506. 2:07:20top and and typing
  2507. 2:07:23super simple
  2508. 2:07:26dodev SL projects SLV
  2509. 2:07:31variables and press
  2510. 2:07:34enter so we're going to work on the cart
  2511. 2:07:37quantity feature of the final project so
  2512. 2:07:39in the final project we have a number in
  2513. 2:07:42the top right corner that shows how many
  2514. 2:07:44products are in our cart when we click
  2515. 2:07:47the add to cart button this quantity
  2516. 2:07:49will increase
  2517. 2:07:52so what you see here is a simple version
  2518. 2:07:54of the cart quantity feature that we can
  2519. 2:07:57create right now let's go over how it
  2520. 2:08:00works first make sure you have the
  2521. 2:08:02console open on this page now when we
  2522. 2:08:06click the show quantity button it will
  2523. 2:08:09show us that there are zero products in
  2524. 2:08:11our cart if we click the add to cart
  2525. 2:08:15button it will increase the quantity by
  2526. 2:08:17one and display it in the console
  2527. 2:08:22if we click add to cart again it will
  2528. 2:08:24increase the quantity by one again now
  2529. 2:08:28in the final project we can also add
  2530. 2:08:30more than one product at a time using
  2531. 2:08:33this
  2532. 2:08:35drop-down for now we're going to build a
  2533. 2:08:37simpler version of that we're going to
  2534. 2:08:40use the plus two and plus three buttons
  2535. 2:08:42to add multiple products so when we
  2536. 2:08:45click the plus two button it will
  2537. 2:08:48increase the quantity by two and display
  2538. 2:08:50it in the console
  2539. 2:08:52and when we click the plus three button
  2540. 2:08:54it will increase the quantity by
  2541. 2:08:57three and finally when we click the
  2542. 2:09:00reset cart button it will reset the
  2543. 2:09:03quantity back to zero and display this
  2544. 2:09:06message in the
  2545. 2:09:08console so now that we understand how
  2546. 2:09:11this project Works let's build this step
  2547. 2:09:13by step first let's create a new HTML
  2548. 2:09:18file just for this project we're going
  2549. 2:09:21to go to our code editor and click this
  2550. 2:09:24icon to show our files and then click
  2551. 2:09:27this icon to create a new
  2552. 2:09:31file let's name this file 05
  2553. 2:09:35dcart D
  2554. 2:09:38quantity.
  2555. 2:09:40HTML and press
  2556. 2:09:42enter next let's open variables. HTML
  2557. 2:09:47and we're going to copy all the code
  2558. 2:09:49here into our new
  2559. 2:09:52file let's type control a on Windows or
  2560. 2:09:56command a on Mac to select all this code
  2561. 2:10:00and then
  2562. 2:10:01rightclick
  2563. 2:10:03copy and then in our new
  2564. 2:10:05file right click and
  2565. 2:10:10paste let's close this for
  2566. 2:10:13now and in the new file we're going to
  2567. 2:10:16change the title to cart quantity
  2568. 2:10:22and let's remove all of our JavaScript
  2569. 2:10:24code but keep the script element because
  2570. 2:10:26we're going to write new JavaScript
  2571. 2:10:30code now we're ready to begin let's
  2572. 2:10:33start by creating these
  2573. 2:10:35buttons let's go to the body element
  2574. 2:10:39going to create a new
  2575. 2:10:40line and here as we learned in the
  2576. 2:10:44previous lesson we can create a button
  2577. 2:10:47using this HTML code less than button
  2578. 2:10:52greater than and the closing tag less
  2579. 2:10:54than SLB button greater
  2580. 2:10:57than inside this button we're going to
  2581. 2:10:59have the text show
  2582. 2:11:03quantity and let's
  2583. 2:11:05save now let's open this file in live
  2584. 2:11:08server by right clicking and then open
  2585. 2:11:11with live
  2586. 2:11:14server and we can see that we created
  2587. 2:11:16the first button now let's create the
  2588. 2:11:19other buttons here feel free to pause
  2589. 2:11:22the video if you want to try it
  2590. 2:11:25yourself we're going to go back to our
  2591. 2:11:27HTML and create another
  2592. 2:11:31button inside this one we're going to
  2593. 2:11:33have the text add to
  2594. 2:11:38cart let's create another
  2595. 2:11:41button this one is going to have the
  2596. 2:11:43text plus
  2597. 2:11:45two and we'll create another
  2598. 2:11:49button this one is going to be + 3 and
  2599. 2:11:53finally we'll create the last
  2600. 2:11:57button and this one is
  2601. 2:12:00reset
  2602. 2:12:02cart and now let's
  2603. 2:12:04save and if we go back to our
  2604. 2:12:07tab we've created all the
  2605. 2:12:10buttons now let's make this web page
  2606. 2:12:13interactive with
  2607. 2:12:15JavaScript first of all we need
  2608. 2:12:17somewhere to save the cart quantity so
  2609. 2:12:20as we learned in this lesson variables
  2610. 2:12:22are the perfect solution for this
  2611. 2:12:25variables let us save a value so let's
  2612. 2:12:28go into the script element and we're
  2613. 2:12:31going to create a variable by typing
  2614. 2:12:34let and this variable is going to save
  2615. 2:12:37the cart quantity so let's just name it
  2616. 2:12:41cart capital Q
  2617. 2:12:45quantity so the card quantity will start
  2618. 2:12:47out at zero so we're going to type equal
  2619. 2:12:52Z and
  2620. 2:12:54semicolon next when we click the show
  2621. 2:12:57quantity button we want to display the
  2622. 2:12:59quantity in the console to do this we
  2623. 2:13:03learned a special HTML attribute in the
  2624. 2:13:05previous lesson called the onclick
  2625. 2:13:08attribute so we're going to type in this
  2626. 2:13:11opening tag here space
  2627. 2:13:15onclick equals and double quotes So the
  2628. 2:13:19onclick attribute run some JavaScript
  2629. 2:13:22when we click this
  2630. 2:13:24button and between the double quotes we
  2631. 2:13:27can write JavaScript code so let's press
  2632. 2:13:30enter
  2633. 2:13:31enter And now when we click this button
  2634. 2:13:34we want to display the card quantity in
  2635. 2:13:37the console so let's type
  2636. 2:13:43console.log Open Bracket close bracket
  2637. 2:13:46semicolon and then between the brackets
  2638. 2:13:49we're going to display a message
  2639. 2:13:52now if we go back to Super
  2640. 2:13:55simple. and we click show
  2641. 2:13:57quantity we want to display this message
  2642. 2:14:00in the
  2643. 2:14:01console so in our code between the
  2644. 2:14:04brackets we're going to type the string
  2645. 2:14:07quote cart
  2646. 2:14:10quantity colon
  2647. 2:14:130 now let's save and go back to our tab
  2648. 2:14:18to try it
  2649. 2:14:19out so we're going to open the console
  2650. 2:14:21first by right clicking and then click
  2651. 2:14:25inspect and then click the
  2652. 2:14:28console and now when we press the show
  2653. 2:14:30quantity button it should display that
  2654. 2:14:33message in the
  2655. 2:14:36console okay so we just made our project
  2656. 2:14:39interactive however we obviously don't
  2657. 2:14:42want to display zero every time we want
  2658. 2:14:44to display the value inside cart
  2659. 2:14:47quantity so we need to insert this value
  2660. 2:14:50in inside this
  2661. 2:14:52string remember from the strings lesson
  2662. 2:14:55when we want to insert a value into a
  2663. 2:14:57string we're going to use a template
  2664. 2:14:59string so let's switch the single quotes
  2665. 2:15:03to back
  2666. 2:15:05ticks to create a template
  2667. 2:15:08string and now instead of zero we're
  2668. 2:15:11going to insert a value using Dollar
  2669. 2:15:15open curly bracket and close curly
  2670. 2:15:18bracket and in between these brackets
  2671. 2:15:21we're going to insert this
  2672. 2:15:23variable
  2673. 2:15:25cart capital Q
  2674. 2:15:29quantity remember that JavaScript is
  2675. 2:15:31case sensitive so make sure you type the
  2676. 2:15:34variable name exactly the
  2677. 2:15:37same now let's
  2678. 2:15:40save and click show quantity
  2679. 2:15:43again and now it displays the value
  2680. 2:15:46inside card quantity in the console so
  2681. 2:15:49notice that variables also work with
  2682. 2:15:52string
  2683. 2:15:53interpolation again we can use a
  2684. 2:15:55variable wherever we use a value and it
  2685. 2:15:58will just substitute whatever value is
  2686. 2:16:01saved inside this variable into the
  2687. 2:16:04code now let's make the rest of the
  2688. 2:16:07buttons
  2689. 2:16:08interactive so when we click the add to
  2690. 2:16:10cart button we're going to add one to
  2691. 2:16:13this cart quantity and display it in the
  2692. 2:16:16console so again we're going to type
  2693. 2:16:19here and type space
  2694. 2:16:21on click equals double quotes and
  2695. 2:16:26between the double quotes we're going to
  2696. 2:16:28add
  2697. 2:16:29JavaScript let's also add some new lines
  2698. 2:16:32here to make our code easier to
  2699. 2:16:35read okay so in the onclick attribute
  2700. 2:16:39we're going to do two things the first
  2701. 2:16:42one is increase the card quantity by
  2702. 2:16:45one we can do that by reassigning the
  2703. 2:16:48card quantity variable
  2704. 2:16:51so we're going to type just the variable
  2705. 2:16:52name cart
  2706. 2:16:55quantity and we're going to make it
  2707. 2:16:57equal to
  2708. 2:17:01itself plus one and
  2709. 2:17:06semicolon next we're going to display
  2710. 2:17:08the updated quantity in the console so
  2711. 2:17:11let's press
  2712. 2:17:13enter and we're going to display this
  2713. 2:17:15message again so we can actually just
  2714. 2:17:18copy this code so we'll select it and
  2715. 2:17:20then right click and
  2716. 2:17:23copy and then
  2717. 2:17:25here right click and
  2718. 2:17:29paste now if we
  2719. 2:17:32save and click the add to card
  2720. 2:17:36button we'll see that it increased the
  2721. 2:17:38quantity by one and displayed the
  2722. 2:17:40updated quantity in the console let's
  2723. 2:17:43press it again to make sure it
  2724. 2:17:45works so it increases it by one again
  2725. 2:17:48and then displays it in the console
  2726. 2:17:51so that's basically how the cart
  2727. 2:17:52quantity feature works in the final
  2728. 2:17:55project first we need a variable to save
  2729. 2:17:57the quantity and then when we click a
  2730. 2:18:00button we're going to update the
  2731. 2:18:03quantity so now let's make the rest of
  2732. 2:18:06these buttons interactive feel free to
  2733. 2:18:09pause the video if you want to try it
  2734. 2:18:12yourself for the plus two button we're
  2735. 2:18:14going to add the onclick attribute again
  2736. 2:18:18onclick equals double quote
  2737. 2:18:22and inside we're going to increase the
  2738. 2:18:24cart quantity by two so cart quantity
  2739. 2:18:30and then we'll just make it equal to
  2740. 2:18:34itself plus two and
  2741. 2:18:39semicolon and again we're going to
  2742. 2:18:41display the updated quantity in the
  2743. 2:18:43console so we can create a new line and
  2744. 2:18:45just copy this code so select it right
  2745. 2:18:49click
  2746. 2:18:51copy and here right click and
  2747. 2:18:56paste let's save and click plus two a
  2748. 2:19:00few times and we can see that it adds
  2749. 2:19:03two to the card quantity and displays it
  2750. 2:19:06in the
  2751. 2:19:07console for the plus three button we'll
  2752. 2:19:10do the
  2753. 2:19:11same let's type an onclick
  2754. 2:19:14attribute click equals double
  2755. 2:19:17quotes and here we're going to increase
  2756. 2:19:19the card quantity by three three so cart
  2757. 2:19:23quantity equals
  2758. 2:19:26itself plus 3 and
  2759. 2:19:30semicolon and we're going to display
  2760. 2:19:32this in the console again so we'll
  2761. 2:19:34select this right click copy and here
  2762. 2:19:39rightclick and paste let's save again
  2763. 2:19:44and try it out to make sure it works so+
  2764. 2:19:47three +
  2765. 2:19:49three and finally let's do the reset
  2766. 2:19:52card
  2767. 2:19:53button so we'll add the onclick
  2768. 2:19:56attribute again on click equals double
  2769. 2:20:00quotes and inside the double quotes
  2770. 2:20:03we're going to do something a little
  2771. 2:20:04different this time instead of
  2772. 2:20:07increasing the cart quantity we'll set
  2773. 2:20:09it back to
  2774. 2:20:10zero to do this we can just reassign the
  2775. 2:20:14cart quantity variable so cart
  2776. 2:20:18quantity and just make it equal to Z and
  2777. 2:20:23semicolon now if we go back to
  2778. 2:20:25superimple
  2779. 2:20:27dodev and try out this reset cart
  2780. 2:20:30button you'll notice that it displays
  2781. 2:20:32two messages the first message is cart
  2782. 2:20:35was reset so we're going to display that
  2783. 2:20:37message in our code
  2784. 2:20:39first
  2785. 2:20:43console.log and the string cart was
  2786. 2:20:48reset
  2787. 2:20:52and the second message is the same
  2788. 2:20:54message as the other buttons so we can
  2789. 2:20:57just select
  2790. 2:20:58this and then right click
  2791. 2:21:02copy and then here right click and
  2792. 2:21:09paste and now let's save and go back to
  2793. 2:21:12our Tab and give this a
  2794. 2:21:15try so the cart quantity starts at zero
  2795. 2:21:18when we press add to cart increase in es
  2796. 2:21:20by one this increases by two this
  2797. 2:21:24increases by three and when we click
  2798. 2:21:27reset cart it'll display cart was reset
  2799. 2:21:31and reset the cart quantity back to
  2800. 2:21:34zero and that's it we just created a
  2801. 2:21:37simple version of the cart quantity
  2802. 2:21:38feature of our final project using
  2803. 2:21:41everything that we learned in this
  2804. 2:21:42course so
  2805. 2:21:44far now we're going to learn some
  2806. 2:21:46shortcuts for reassigning
  2807. 2:21:48variables so if we look at this line of
  2808. 2:21:52code here we're increasing the value of
  2809. 2:21:56this variable by two and then saving it
  2810. 2:21:59back well there's actually a shortcut
  2811. 2:22:02for this and that shortcut is cart
  2812. 2:22:07quantity plus equal 2 and
  2813. 2:22:11semicolon so this does the same thing as
  2814. 2:22:14the line above but it's just shorter the
  2815. 2:22:17plus means we're going to add two to
  2816. 2:22:19cart quantity
  2817. 2:22:22and equals means we're going to save
  2818. 2:22:23that result back to card quantity so we
  2819. 2:22:27can actually delete this
  2820. 2:22:29line and the code will work the same way
  2821. 2:22:32if we
  2822. 2:22:34save and try it
  2823. 2:22:36out it will still add a two and then
  2824. 2:22:39save it back in card
  2825. 2:22:42quantity let's scroll up and practice by
  2826. 2:22:46using this shortcut for card quantity +
  2827. 2:22:48one so instead of this line we can type
  2828. 2:22:52cart
  2829. 2:22:54quantity plus equal 1 and
  2830. 2:22:59semicolon so it turns out that in
  2831. 2:23:01programming plus equals 1 is so common
  2832. 2:23:04that we have an even shorter shortcut
  2833. 2:23:06for plus equals 1 and that shortcut is
  2834. 2:23:11cart
  2835. 2:23:12quantity plus plus and
  2836. 2:23:16semicolon so this is the same thing as
  2837. 2:23:19plus equal 1
  2838. 2:23:21and all three lines do the same
  2839. 2:23:23thing so we can remove these
  2840. 2:23:27lines and the code will work the same
  2841. 2:23:30because it's just a
  2842. 2:23:32shortcut if we
  2843. 2:23:33save so if we click add to
  2844. 2:23:37cart it will add one every
  2845. 2:23:40time so those are some shortcuts for
  2846. 2:23:43reassigning
  2847. 2:23:44variables we also have these shortcuts
  2848. 2:23:46for the other operators like minus
  2849. 2:23:49equals multiply equals divide equals and
  2850. 2:23:53minus minus I'll leave some exercises
  2851. 2:23:56for these other shortcuts at the end of
  2852. 2:23:58this
  2853. 2:24:00lesson next we're going to learn some
  2854. 2:24:02best practices for naming variables so
  2855. 2:24:05if we scroll
  2856. 2:24:08down notice that our variable cart
  2857. 2:24:11quantity has two words in it cart and
  2858. 2:24:15quantity we learned earlier that we
  2859. 2:24:17can't have spaces in a variable name so
  2860. 2:24:20if we want to have multiple words we
  2861. 2:24:22have to combine them together like this
  2862. 2:24:25now you might be wondering why we wrote
  2863. 2:24:27it this way with a lowercase c and a
  2864. 2:24:30capital
  2865. 2:24:31Q This is actually a naming convention
  2866. 2:24:34called cam case in cam case we combine
  2867. 2:24:38the words together and capitalize every
  2868. 2:24:41word except the first
  2869. 2:24:43word so for cart quantity we capitalize
  2870. 2:24:46quantity but we keep cart lowercase
  2871. 2:24:49because it's the first
  2872. 2:24:51word cam case is actually the standard
  2873. 2:24:54naming convention for JavaScript all of
  2874. 2:24:57our variable names should use cam
  2875. 2:25:00case now in programming there are other
  2876. 2:25:02naming conventions that exist another
  2877. 2:25:05one is called Pascal case Pascal case is
  2878. 2:25:09the same as camel case except we
  2879. 2:25:11capitalize the first word so for cart
  2880. 2:25:14quantity Pascal case would be Capital
  2881. 2:25:17cart and capital quantity in JavaScript
  2882. 2:25:21there is one feature where we use Pascal
  2883. 2:25:24case which we'll learn later in this
  2884. 2:25:26course for everything else we use camel
  2885. 2:25:29case another naming convention is Kebab
  2886. 2:25:32case here we keep the words lowercase
  2887. 2:25:36and combine them with a dash like cart
  2888. 2:25:39Dash
  2889. 2:25:40quantity Kebab case doesn't work in
  2890. 2:25:42JavaScript because the dash is already a
  2891. 2:25:45minus symbol however we use Kebab case
  2892. 2:25:49in HTML and CSS and also in our file
  2893. 2:25:52names as you can see
  2894. 2:25:55here and finally we have snake case
  2895. 2:25:59where we keep the words lowercase and
  2896. 2:26:01combine them with underscores snake case
  2897. 2:26:04is used in other languages but it's not
  2898. 2:26:06really used in
  2899. 2:26:08JavaScript so those are the common
  2900. 2:26:10naming conventions that we use in
  2901. 2:26:14programming now one more thing I want to
  2902. 2:26:16note is that when we name our variables
  2903. 2:26:19try to pick a name that is not too short
  2904. 2:26:21or too long for example instead of cart
  2905. 2:26:24quantity we could have named this
  2906. 2:26:26variable
  2907. 2:26:28C but this name is too short it's hard
  2908. 2:26:31to understand what c means we could also
  2909. 2:26:35use a name like this quantity of
  2910. 2:26:40products in the cart but this name is
  2911. 2:26:44too long it's hard to read this in the
  2912. 2:26:47code so try to have a balance between a
  2913. 2:26:49name that is understandable but not too
  2914. 2:26:52long like cart
  2915. 2:26:54quantity the last thing we're going to
  2916. 2:26:56learn in this lesson is there are three
  2917. 2:26:58ways to create variables in JavaScript
  2918. 2:27:02let's go back to our variables. HTML
  2919. 2:27:05file and we're going to learn those
  2920. 2:27:07three
  2921. 2:27:09ways so the first way is to use let
  2922. 2:27:12which we already learned the second way
  2923. 2:27:14to create a variable is to use the word
  2924. 2:27:17const so at the bottom we can type const
  2925. 2:27:22space and then a variable name like
  2926. 2:27:25variable 2 and make it equal to
  2927. 2:27:30three so const creates a variable just
  2928. 2:27:33like let except we can't change its
  2929. 2:27:36value later this value stays constant
  2930. 2:27:40that's why it's called const if we try
  2931. 2:27:43to change its value like variable 2 = 5
  2932. 2:27:49and then and we save this and we open
  2933. 2:27:52this file in the browser by right
  2934. 2:27:55clicking and then open with live
  2935. 2:27:59server and then open the console so
  2936. 2:28:03rightclick
  2937. 2:28:05inspect and then click the
  2938. 2:28:10console it will give us an error when we
  2939. 2:28:12try to change this variable so this
  2940. 2:28:15doesn't work let's remove this line and
  2941. 2:28:19Save
  2942. 2:28:21so why would we ever use const instead
  2943. 2:28:23of let well const makes our code safer
  2944. 2:28:26and easier to understand when we create
  2945. 2:28:29a variable with const we know for sure
  2946. 2:28:32that this variable will always contain
  2947. 2:28:35three for let however if we create a
  2948. 2:28:38variable it's hard to know what value is
  2949. 2:28:41inside this variable because later on we
  2950. 2:28:44can change this value in our
  2951. 2:28:47code so in order to keep our code safer
  2952. 2:28:50it's actually a best practice to use
  2953. 2:28:52const by default and only use let when
  2954. 2:28:55we know that we need to change the
  2955. 2:28:57variable so here it's actually best
  2956. 2:29:00practice to use const to create this
  2957. 2:29:03calculation variable because we're not
  2958. 2:29:05changing it later same thing for the
  2959. 2:29:08result
  2960. 2:29:10variable and the message
  2961. 2:29:13variable now for variable one we have to
  2962. 2:29:16use let because we're changing it later
  2963. 2:29:19in this code
  2964. 2:29:21so that's const it creates a variable
  2965. 2:29:23that can't be changed
  2966. 2:29:25later finally the Third Way of creating
  2967. 2:29:28a variable is to use another word VAR so
  2968. 2:29:32at the bottom we can type VAR space and
  2969. 2:29:37then a variable name like variable 3 and
  2970. 2:29:42make it equal to
  2971. 2:29:44three so VAR creates a variable just
  2972. 2:29:47like let and this variable can be
  2973. 2:29:49changed
  2974. 2:29:50later VAR is actually the original way
  2975. 2:29:53to create variables in JavaScript VAR
  2976. 2:29:57means
  2977. 2:29:58variable however VAR has some issues
  2978. 2:30:01that we'll learn later in this course
  2979. 2:30:04and because of these issues we don't use
  2980. 2:30:06VAR in new JavaScript code you might see
  2981. 2:30:09VAR in older JavaScript code so it's
  2982. 2:30:12still useful to know so those are three
  2983. 2:30:15ways to create variables in JavaScript
  2984. 2:30:18let constant and VAR we use const by
  2985. 2:30:23default and if we have to change a
  2986. 2:30:25variable then use
  2987. 2:30:28let so one more thing we're going to
  2988. 2:30:30learn is that we can use type of with
  2989. 2:30:34variables so remember that type of tells
  2990. 2:30:37us what type a value is for example if
  2991. 2:30:42we
  2992. 2:30:43wrote
  2993. 2:30:45console.
  2994. 2:30:47log type of
  2995. 2:30:51three and
  2996. 2:30:53save type of will tell us that three is
  2997. 2:30:57a
  2998. 2:30:58number well we can actually use type of
  2999. 2:31:01with a
  3000. 2:31:02variable so if we change this to
  3001. 2:31:06variable 2 and
  3002. 2:31:10save type of will tell us the type of
  3003. 2:31:13value inside the variable which is a
  3004. 2:31:17number and that's what we see in the
  3005. 2:31:19console
  3006. 2:31:21now let's check the type of this
  3007. 2:31:24variable message so message contains a
  3008. 2:31:28string and if we do
  3009. 2:31:33console.log type of
  3010. 2:31:38message and
  3011. 2:31:41save this will tell us that the value
  3012. 2:31:44inside the message variable is a string
  3013. 2:31:48so we can use type of to check the type
  3014. 2:31:50of value that is saved inside a variable
  3015. 2:31:54and that's the end of this lesson in
  3016. 2:31:57this lesson we learned about variables
  3017. 2:31:59which are a way to save values and use
  3018. 2:32:02them later we learned how to reassign a
  3019. 2:32:05variable we created the cart quantity
  3020. 2:32:08feature of the final project we learned
  3021. 2:32:11some shortcuts for reassigning a
  3022. 2:32:13variable we learned some naming
  3023. 2:32:15conventions and best practices for
  3024. 2:32:17variable names and we learned learned
  3025. 2:32:20three ways to create a variable let
  3026. 2:32:24const and
  3027. 2:32:25VAR here are some exercises to help you
  3028. 2:32:28practice using variables
  3029. 2:32:58in this lesson we're going to learn two
  3030. 2:33:00more features of JavaScript called
  3031. 2:33:03booleans and if statements and we're
  3032. 2:33:06going to use these features to build
  3033. 2:33:08this rock paper scissors
  3034. 2:33:11project let's start by creating a new
  3035. 2:33:14HTML file for this lesson we're going to
  3036. 2:33:17go to our code editor and click this
  3037. 2:33:20icon to show our files and then click
  3038. 2:33:23this icon to create a new file we're
  3039. 2:33:26going to name this file
  3040. 2:33:3006-
  3041. 2:33:32booleans
  3042. 2:33:34HTML and press
  3043. 2:33:36enter let's open variables.
  3044. 2:33:40HTML and we're going to copy all this
  3045. 2:33:43code into our new file let's select the
  3046. 2:33:46code using control a on Windows or
  3047. 2:33:49command a on Mac and then
  3048. 2:33:52rightclick
  3049. 2:33:54copy and in
  3050. 2:33:56here rightclick and
  3051. 2:34:00paste let's close this for now and
  3052. 2:34:04prepare this file for this lesson so
  3053. 2:34:06we'll scroll to the top and change this
  3054. 2:34:09to
  3055. 2:34:11booleans and then remove all the
  3056. 2:34:14JavaScript code but keep the script
  3057. 2:34:16element
  3058. 2:34:20now let's
  3059. 2:34:22save and open this file in live server
  3060. 2:34:25by right clicking and then open with
  3061. 2:34:27live
  3062. 2:34:29server and we can close the previous
  3063. 2:34:34tabs as well as the previous code for
  3064. 2:34:39now we'll start by learning what are
  3065. 2:34:42booleans so far in this course we learn
  3066. 2:34:44two types of values in JavaScript
  3067. 2:34:47numbers and strings booleans are another
  3068. 2:34:50type of value in JavaScript but booleans
  3069. 2:34:53are special because there are only two
  3070. 2:34:56Boolean values true and
  3071. 2:34:59false let's click in the script element
  3072. 2:35:02and type true press enter and type
  3073. 2:35:08false so these are the only two Boolean
  3074. 2:35:11values that exist true and false so
  3075. 2:35:15what's the purpose of Boolean values a
  3076. 2:35:18Boolean value represents whether
  3077. 2:35:20something is true or
  3078. 2:35:23false for example in JavaScript we can
  3079. 2:35:26compare two numbers let's create some
  3080. 2:35:29new lines and type the code 3 less than
  3081. 2:35:355 so this code checks whether the number
  3082. 2:35:38three is less than five and of course
  3083. 2:35:41this is true so if we console.log this
  3084. 2:35:45code console.log
  3085. 2:35:50and
  3086. 2:35:51save and then go to our website and open
  3087. 2:35:55the console rightclick
  3088. 2:35:58inspect and
  3089. 2:36:00console it will show us that the result
  3090. 2:36:02of this code is the Boolean value
  3091. 2:36:06true let's try another
  3092. 2:36:08example let's delete this less than
  3093. 2:36:11symbol and replace it with a greater
  3094. 2:36:14than symbol so now this code checks
  3095. 2:36:17whether three is greater than five
  3096. 2:36:20and we know that three is not greater
  3097. 2:36:21than five so if we
  3098. 2:36:24save the computer will tell us that the
  3099. 2:36:26result of this comparison is the Boolean
  3100. 2:36:29value false so as you can see a Boolean
  3101. 2:36:32value represents whether something is
  3102. 2:36:35true or
  3103. 2:36:37false now let's learn the syntax rules
  3104. 2:36:39for booleans to create a Boolean we just
  3105. 2:36:43type true or false note that we don't
  3106. 2:36:46surround these with quotes like this if
  3107. 2:36:49we surround them with quotes this is now
  3108. 2:36:51a string not a
  3109. 2:36:53Boolean we can use type of to check this
  3110. 2:36:57so at the front we can type
  3111. 2:37:02console.log type of
  3112. 2:37:06true if we
  3113. 2:37:09save this will tell us that this is a
  3114. 2:37:12string now if we remove the
  3115. 2:37:16quotes and Save
  3116. 2:37:20this will tell us that this is now a
  3117. 2:37:24Boolean so when creating a Boolean don't
  3118. 2:37:27put quotes around true or
  3119. 2:37:30false let's
  3120. 2:37:32save now one way to create a Boolean is
  3121. 2:37:36to compare two numbers for example 3
  3122. 2:37:39greater than 5 so this greater than
  3123. 2:37:43symbol is called a comparison operator
  3124. 2:37:46and there are many other comparison
  3125. 2:37:48operators we can use
  3126. 2:37:50we saw less than earlier and we also
  3127. 2:37:53have greater than or equal to less than
  3128. 2:37:56or equal to Triple equals which checks
  3129. 2:37:59if two values are equal to each other
  3130. 2:38:02and exclamation double equals which
  3131. 2:38:04checks if two values are not equal to
  3132. 2:38:07each other now JavaScript is a little
  3133. 2:38:10special because it has two ways to check
  3134. 2:38:12if two values are equal triple equals
  3135. 2:38:16and double equals the difference is that
  3136. 2:38:19double equals tries to convert both
  3137. 2:38:21values into the same type for example in
  3138. 2:38:25our code let's create some new lines and
  3139. 2:38:30console.
  3140. 2:38:32log and let's check if the number five
  3141. 2:38:36is equal to the string
  3142. 2:38:415. if we
  3143. 2:38:43save it will tell us that these two
  3144. 2:38:45values are equal to each other even
  3145. 2:38:48though one of of them is a number and
  3146. 2:38:50the other is a string and that's because
  3147. 2:38:53the double equals converts both values
  3148. 2:38:56into the same type so converts both of
  3149. 2:38:59these into the number five and then
  3150. 2:39:02Compares them so that's why they are
  3151. 2:39:05equal however this is not a good idea
  3152. 2:39:08because this value is a number and this
  3153. 2:39:10value is a string it's a piece of text
  3154. 2:39:14so they shouldn't really be equal to
  3155. 2:39:16each other that's why in JavaScript we
  3156. 2:39:19always use the triple equals to check if
  3157. 2:39:22two values are the same so that we avoid
  3158. 2:39:24the conversion behavior of double equals
  3159. 2:39:28so if we
  3160. 2:39:29save now it will tell us that these two
  3161. 2:39:32values are not equal to each other
  3162. 2:39:35because one value is a number and the
  3163. 2:39:37other value is a
  3164. 2:39:39string so this rule also applies to not
  3165. 2:39:42equal to in JavaScript we always use
  3166. 2:39:45exclamation double equals instead of
  3167. 2:39:48exclamation equals to avoid the
  3168. 2:39:51conversion
  3169. 2:39:52Behavior All right so those are
  3170. 2:39:55comparison operators in the order of
  3171. 2:39:58operations comparison operators have a
  3172. 2:40:01lower priority than math so if we did
  3173. 2:40:04something like 5 and then minus
  3174. 2:40:085 this would calculate 5 - 5 first which
  3175. 2:40:12is zero and then compare the two numbers
  3176. 2:40:16so three is now greater than zero so if
  3177. 2:40:19we
  3178. 2:40:19save the first comparison will now be
  3179. 2:40:23true so comparison operators have a
  3180. 2:40:26lower priority than math
  3181. 2:40:29operators now that we know what Boolean
  3182. 2:40:31values are we're going to combine them
  3183. 2:40:34with a really useful feature called if
  3184. 2:40:36statements an if statement lets us write
  3185. 2:40:39multiple groups of code and then decide
  3186. 2:40:42which code to run Let's do an example
  3187. 2:40:45and create our first if statement we're
  3188. 2:40:48going to go to the the
  3189. 2:40:50bottom and create some new
  3190. 2:40:53lines and type if and Open Bracket Clos
  3191. 2:40:58bracket and open curly bracket closed
  3192. 2:41:01curly
  3193. 2:41:03bracket between these round brackets
  3194. 2:41:06we're going to put a Boolean value like
  3195. 2:41:09true and in between the curly brackets
  3196. 2:41:12we're going to put some code to run for
  3197. 2:41:15example console.log
  3198. 2:41:21hello so the way that an if statement
  3199. 2:41:23works is that if this Boolean value is
  3200. 2:41:27true then we're going to run the code in
  3201. 2:41:29the curly brackets if this Boolean value
  3202. 2:41:32is false then we're not going to run the
  3203. 2:41:35code if we
  3204. 2:41:37save the value between the brackets is
  3205. 2:41:40true so we're going to run this code and
  3206. 2:41:43display
  3207. 2:41:44hello now if we change this to false and
  3208. 2:41:51save it will not run this
  3209. 2:41:54code so an if statement lets us decide
  3210. 2:41:57whether or not to run some code based on
  3211. 2:42:00this Boolean
  3212. 2:42:01value now if statements have another
  3213. 2:42:04useful feature called
  3214. 2:42:06else let's go to the end of this if
  3215. 2:42:09statement and
  3216. 2:42:10type
  3217. 2:42:12else open curly bracket close curly
  3218. 2:42:15bracket and then inside these curly
  3219. 2:42:18brackets we're going to put some
  3220. 2:42:20different code like
  3221. 2:42:24console.log
  3222. 2:42:27else the way else works is if this
  3223. 2:42:31Boolean value is true the computer will
  3224. 2:42:33run this code otherwise we will run the
  3225. 2:42:37code inside
  3226. 2:42:38else let's give it a try we'll change
  3227. 2:42:42this Boolean value to true and
  3228. 2:42:45save and because it is true it will run
  3229. 2:42:48this code
  3230. 2:42:49and display
  3231. 2:42:51hello now if this Boolean value is false
  3232. 2:42:56and
  3233. 2:42:57save because it is false it will not run
  3234. 2:43:00this code and it will run the code
  3235. 2:43:02inside else and display
  3236. 2:43:05else so using IF and else we can write
  3237. 2:43:09two groups of code and then decide which
  3238. 2:43:11code to
  3239. 2:43:13run now let's do a practical example
  3240. 2:43:16we're going to write some code to check
  3241. 2:43:18if someone is old enough to
  3242. 2:43:20drive let's go to the bottom and create
  3243. 2:43:23some new
  3244. 2:43:24lines and we're going to type if
  3245. 2:43:28brackets and curly
  3246. 2:43:30brackets and inside here we're going to
  3247. 2:43:33compare two numbers the first number is
  3248. 2:43:36a person's age let's pretend we have a
  3249. 2:43:40person who is 30 years old so we're
  3250. 2:43:42going to type
  3251. 2:43:4430 then we're going to check if this age
  3252. 2:43:48is greater than or equal to the legal
  3253. 2:43:51driving age Let's Pretend the legal
  3254. 2:43:54driving age is
  3255. 2:43:5716 notice that between the brackets we
  3256. 2:43:59don't have to just put a Boolean value
  3257. 2:44:03we can put any code that results in a
  3258. 2:44:05Boolean
  3259. 2:44:06value so this comparison results in the
  3260. 2:44:09Boolean value true that means the
  3261. 2:44:12computer will run the code between the
  3262. 2:44:14curly brackets so in here let's type
  3263. 2:44:18console
  3264. 2:44:19.
  3265. 2:44:22log you can
  3266. 2:44:27drive now if this is not true let's run
  3267. 2:44:30some different code at the bottom we'll
  3268. 2:44:33type
  3269. 2:44:34else and curly brackets and then inside
  3270. 2:44:38these brackets we'll type
  3271. 2:44:43console.log you canot drive
  3272. 2:44:49now let's
  3273. 2:44:50save and it will tell me that this
  3274. 2:44:53person who is 30 years old can
  3275. 2:44:56drive if we go back and change this to a
  3276. 2:44:59person that is 15 years old and
  3277. 2:45:04save this comparison will result in
  3278. 2:45:06false so it will display you cannot
  3279. 2:45:10drive so that's how we use if statements
  3280. 2:45:13we can write multiple groups of code for
  3281. 2:45:16different situations and then decide
  3282. 2:45:18which Cod code to
  3283. 2:45:21run now let's learn the syntax rules for
  3284. 2:45:24if statements to create an if statement
  3285. 2:45:27we type if and then round brackets with
  3286. 2:45:30a Boolean value inside this is called
  3287. 2:45:33the condition if this condition is true
  3288. 2:45:37we will run the code inside the curly
  3289. 2:45:40brackets we can also add an else
  3290. 2:45:43statement this code will run if the
  3291. 2:45:45condition is false the else statement is
  3292. 2:45:49optional we don't have to have
  3293. 2:45:51one so these curly brackets and the code
  3294. 2:45:54inside these are called branches because
  3295. 2:45:57it's like a tree branch the code is
  3296. 2:45:59splitting off into two directions and we
  3297. 2:46:02either run this code or we run this
  3298. 2:46:06code one more thing to know is in a
  3299. 2:46:09branch if we only have one line of code
  3300. 2:46:12the curly brackets are actually optional
  3301. 2:46:15so we can delete these and the code will
  3302. 2:46:17still work
  3303. 2:46:19however if we have more than one line of
  3304. 2:46:21code in this Branch for example if we
  3305. 2:46:24add another line
  3306. 2:46:28console.log
  3307. 2:46:31congrats now this branch has two lines
  3308. 2:46:34of code so the curly brackets are
  3309. 2:46:36mandatory so we have to add them
  3310. 2:46:42back now we're going to learn another
  3311. 2:46:44feature of if statements which is we can
  3312. 2:46:46have more than one condition for example
  3313. 2:46:50we're going to go to the end of this if
  3314. 2:46:52branch and type else if and then
  3315. 2:46:58brackets and curly
  3316. 2:47:00brackets else if lets us add another
  3317. 2:47:03Branch to our if
  3318. 2:47:05statement between these round brackets
  3319. 2:47:08we can add another condition for example
  3320. 2:47:11if a person is almost old enough to
  3321. 2:47:13drive let's display a different message
  3322. 2:47:17so inside this condition
  3323. 2:47:19let's check if this person's age which
  3324. 2:47:21is 15 is greater than or equal to 14 so
  3325. 2:47:27we're checking if they are almost old
  3326. 2:47:29enough to drive and now inside the curly
  3327. 2:47:32brackets we're going to display a
  3328. 2:47:34different
  3329. 2:47:35message
  3330. 2:47:40console.log almost
  3331. 2:47:44there so the way this works is that the
  3332. 2:47:47if statement checks the condition one by
  3333. 2:47:50one first it checks if this is true if
  3334. 2:47:53it is it will run this Branch then it
  3335. 2:47:56checks if this is true if it is it will
  3336. 2:47:58run this branch and if none of the
  3337. 2:48:01conditions are true it will run the else
  3338. 2:48:04Branch so here this condition is false
  3339. 2:48:08so we're not going to run this code but
  3340. 2:48:10now the second condition is true so
  3341. 2:48:13we're going to run this code if we
  3342. 2:48:16save we display almost there
  3343. 2:48:19in the
  3344. 2:48:20console so using else if branches we can
  3345. 2:48:24add as many conditions as we want to an
  3346. 2:48:26if
  3347. 2:48:27statement the next thing we're going to
  3348. 2:48:29learn is that we can combine if
  3349. 2:48:31statements with variables from the
  3350. 2:48:33previous
  3351. 2:48:34lesson for example here we wrote the
  3352. 2:48:38person's age two times so we can
  3353. 2:48:40actually save this age in a variable and
  3354. 2:48:43then reuse that
  3355. 2:48:45value so above the if statement
  3356. 2:48:49let's create a variable using const and
  3357. 2:48:54let's name this variable H and we'll
  3358. 2:48:57make it equal to
  3359. 2:49:0015 remember the best practice for
  3360. 2:49:02creating a variable is to use const by
  3361. 2:49:05default and only use let if we're going
  3362. 2:49:08to change this value
  3363. 2:49:10later and now that we saved this number
  3364. 2:49:13in a variable we can use the variable
  3365. 2:49:15inside the if statement so we'll replace
  3366. 2:49:1915 with age and here as well with
  3367. 2:49:25age so this will take whatever value is
  3368. 2:49:28inside the variable and substitute it
  3369. 2:49:31into the
  3370. 2:49:32code if we
  3371. 2:49:34save the if statement works the same way
  3372. 2:49:37as before and we run this
  3373. 2:49:40line okay now we're ready to use
  3374. 2:49:43booleans and if statements to do a
  3375. 2:49:45project we're going to build a rock
  3376. 2:49:47paper scissors gate G first let's take a
  3377. 2:49:51look at the project we're going to go to
  3378. 2:49:53our browser and at the top create a new
  3379. 2:49:57tab and we're going to type here
  3380. 2:50:02superimple dodev SL
  3381. 2:50:06projects SL
  3382. 2:50:08booleans and press
  3383. 2:50:11enter here we can see a simple version
  3384. 2:50:14of a rock paper scissors game and if we
  3385. 2:50:16click this link it will take us to the
  3386. 2:50:19final version of this game so in this
  3387. 2:50:22game we can pick a
  3388. 2:50:24move and then the computer will pick a
  3389. 2:50:27random move and it will show us the
  3390. 2:50:30result and we also have a score of how
  3391. 2:50:33many times we won lost and tied so we're
  3392. 2:50:37going to build this as we go through the
  3393. 2:50:39course but for now let's go
  3394. 2:50:42back and we're going to start by
  3395. 2:50:44building this simple version of the game
  3396. 2:50:47so how this simple version works is we
  3397. 2:50:49click one of these buttons to pick a
  3398. 2:50:51move so let's pick
  3399. 2:50:53Rock then the computer will randomly
  3400. 2:50:56pick a move in this case it's paper but
  3401. 2:50:59for you it might be different and it
  3402. 2:51:01will compare the two moves and display
  3403. 2:51:03the results in this
  3404. 2:51:05popup so the rules of rock paper
  3405. 2:51:07scissors are rock beats scissors paper
  3406. 2:51:11beats Rock and scissors beats
  3407. 2:51:15paper let's press okay and we'll get
  3408. 2:51:18started on this
  3409. 2:51:19project first we'll create a new HTML
  3410. 2:51:22file just for this project let's go to
  3411. 2:51:25our code editor and click here and
  3412. 2:51:28create a new file and we're going to
  3413. 2:51:30name this file
  3414. 2:51:3306-
  3415. 2:51:39rock-paper-scissors HTML and press
  3416. 2:51:43enter next let's copy all the code from
  3417. 2:51:46booleans HTML into this new file so
  3418. 2:51:50we'll open this again and then control a
  3419. 2:51:53or command a to select all the code and
  3420. 2:51:56then right click
  3421. 2:51:58copy and in here right click and
  3422. 2:52:03paste let's close this and prepare this
  3423. 2:52:07file so we'll change this to rock
  3424. 2:52:12paper
  3425. 2:52:14scissors and remove all of the
  3426. 2:52:17JavaScript code that we had
  3427. 2:52:22before finally let's save and open this
  3428. 2:52:26file in the browser by right clicking
  3429. 2:52:29and then open with live
  3430. 2:52:32server let's take a look at the project
  3431. 2:52:35again so we're going to start by
  3432. 2:52:38creating the text first because this
  3433. 2:52:40text is in the final version of the
  3434. 2:52:44project so in our
  3435. 2:52:46HTML we're going to create a paragraph
  3436. 2:52:49element for this text so less than P
  3437. 2:52:53greater than and the closing tag less
  3438. 2:52:56than /p greater than inside our
  3439. 2:52:59paragraph we're going to have the text
  3440. 2:53:02rock paper
  3441. 2:53:05scissors next let's create the
  3442. 2:53:10buttons so we'll go to our
  3443. 2:53:12HTML and create a
  3444. 2:53:16button and inside this first button
  3445. 2:53:18we'll have the text
  3446. 2:53:21Rock Let's create another
  3447. 2:53:25button this one will be
  3448. 2:53:28paper and we'll create the last
  3449. 2:53:31button this one will be
  3450. 2:53:35scissors now let's save and check our
  3451. 2:53:39new
  3452. 2:53:40tab and now we created all the elements
  3453. 2:53:43that we
  3454. 2:53:44need next let's make these elements
  3455. 2:53:47interactive with Javas JavaScript so
  3456. 2:53:49when writing JavaScript a good strategy
  3457. 2:53:52is to think about what steps we need to
  3458. 2:53:54do and then convert those steps into
  3459. 2:53:57code so here when we click a button the
  3460. 2:54:01first step is the computer will randomly
  3461. 2:54:03select a move and then we're going to
  3462. 2:54:05compare the moves to get the result and
  3463. 2:54:08finally we'll display the result in a
  3464. 2:54:11popup by the way this is called an
  3465. 2:54:14algorithm an algorithm is a set of steps
  3466. 2:54:17to complete delete a task or to solve a
  3467. 2:54:20problem now that we know what steps we
  3468. 2:54:23need to do we're going to convert these
  3469. 2:54:25steps or this algorithm into
  3470. 2:54:28code first we're going to run some
  3471. 2:54:30JavaScript when we click these buttons
  3472. 2:54:33so let's use the onclick attribute again
  3473. 2:54:36we'll go to the opening tag of the
  3474. 2:54:38button and type
  3475. 2:54:41onclick equals double
  3476. 2:54:44quotes and then inside these double
  3477. 2:54:46quotes we can add our JavaScript
  3478. 2:54:51so now we have to figure out how to
  3479. 2:54:52randomly select a move for the computer
  3480. 2:54:56to do this we need to learn a new piece
  3481. 2:54:58of JavaScript
  3482. 2:54:59code so here we're going to type capital
  3483. 2:55:03m
  3484. 2:55:05math.random Open Bracket close bracket
  3485. 2:55:09math.random generates a random number
  3486. 2:55:12between 0o and 1 so if we console.log
  3487. 2:55:16this console
  3488. 2:55:21log and
  3489. 2:55:24save and click this
  3490. 2:55:27button and then check in our console by
  3491. 2:55:30right-clicking
  3492. 2:55:33inspect and the
  3493. 2:55:35console it will display a random number
  3494. 2:55:38between Z and 1 if we click this button
  3495. 2:55:42again it will generate a different
  3496. 2:55:44random number so every time we run MTH
  3497. 2:55:48random it generates a different random
  3498. 2:55:51number between 0 and one now to be more
  3499. 2:55:54precise it actually generates a number
  3500. 2:55:57greater than equal to zero and less than
  3501. 2:56:00one now let's save this random number in
  3502. 2:56:03a variable so we can use it later so
  3503. 2:56:06we'll replace the
  3504. 2:56:08console.log with
  3505. 2:56:10const and then a variable name let's
  3506. 2:56:13just name it random capital N number
  3507. 2:56:19and then
  3508. 2:56:21equals so we saved this random number
  3509. 2:56:23inside this
  3510. 2:56:25variable next we need a way to convert
  3511. 2:56:28this random number into a move like rock
  3512. 2:56:31paper or scissors to do this let's
  3513. 2:56:34imagine the space between 0 and 1 and
  3514. 2:56:38then divide the space into three equal
  3515. 2:56:40parts each part represents a move if we
  3516. 2:56:44generate a random number and it's
  3517. 2:56:46between 0 and 1/3 the move will be Rock
  3518. 2:56:50if it's between 1/3 and 2/3 the move
  3519. 2:56:53will be paper and if it's between 2/3
  3520. 2:56:56and one the move will be scissors this
  3521. 2:57:00is how we can convert the random number
  3522. 2:57:02into a move and this will give the
  3523. 2:57:04computer an equal chance of picking rock
  3524. 2:57:08paper or
  3525. 2:57:09scissors now let's convert these steps
  3526. 2:57:11into code and an if statement is the
  3527. 2:57:14perfect solution for this this already
  3528. 2:57:17looks like an if statement so in our
  3529. 2:57:20onclick attribute let's add some new
  3530. 2:57:23lines and type if brackets and curly
  3531. 2:57:28brackets for this first condition we're
  3532. 2:57:31going to check if the random number is
  3533. 2:57:33between 0 and 1/3 so let's check if
  3534. 2:57:38random number is greater than or equal
  3535. 2:57:42to
  3536. 2:57:43zero but in this case we also need to
  3537. 2:57:46check that random number is less than
  3538. 2:57:531/3 because we're checking if the number
  3539. 2:57:55is between 0 and 1/3 but how do we do
  3540. 2:57:59this how do we check that two
  3541. 2:58:01comparisons are both true to do this
  3542. 2:58:05we're going to learn another type of
  3543. 2:58:07operator in JavaScript called logical
  3544. 2:58:10operators logical operators let us
  3545. 2:58:13combine Boolean values which is what we
  3546. 2:58:15need to do here let's go back into
  3547. 2:58:18booleans
  3548. 2:58:20HTML and we're going to learn logical
  3549. 2:58:23operators so first let's open the tab
  3550. 2:58:26for this
  3551. 2:58:27file and we're going to comment out all
  3552. 2:58:30the previous code so our console doesn't
  3553. 2:58:32get too messy
  3554. 2:58:34slashstar and then at the
  3555. 2:58:37bottom star
  3556. 2:58:39slash now let's create some new
  3557. 2:58:42lines and the first logical operator
  3558. 2:58:45we're going to learn is called the and
  3559. 2:58:47operator
  3560. 2:58:49let's type
  3561. 2:58:51console. log a Boolean value
  3562. 2:58:55true and another Boolean value
  3563. 2:59:00true so this is the and operator and it
  3564. 2:59:03checks if two Boolean values are both
  3565. 2:59:06true so it checks if the left side is
  3566. 2:59:09true and the right side is true that's
  3567. 2:59:12why it's called the and operator so here
  3568. 2:59:16the left side is true and the right side
  3569. 2:59:18is also true so the and operator will
  3570. 2:59:21result in true if we
  3571. 2:59:24save this displays true now if one of
  3572. 2:59:28these sides is false for example if we
  3573. 2:59:31change this to
  3574. 2:59:34false now let's check is the left side
  3575. 2:59:37and the right side both true in this
  3576. 2:59:40case no so and will now result in false
  3577. 2:59:44if we save it now displays false
  3578. 2:59:49so that's the logical and operator it
  3579. 2:59:52checks if the left side is true and the
  3580. 2:59:55right side is also true so this is
  3581. 2:59:58exactly what we need for our project for
  3582. 3:00:01example let's create a new line and type
  3583. 3:00:08console.log and let's say that we
  3584. 3:00:10generated a random number like
  3585. 3:00:130.2 and we want to check if it's between
  3586. 3:00:160 and 1/3 so we can do
  3587. 3:00:190.2 is greater than or equal to 0 and
  3588. 3:00:25we're also going to check if
  3589. 3:00:270.2 is less than
  3590. 3:00:301/3 so we're using the logical and
  3591. 3:00:33operator to check if the left side is
  3592. 3:00:36true and the right side is also true so
  3593. 3:00:40in this example both sides are true 0.2
  3594. 3:00:43is between 0 and 1/3 so if we save
  3595. 3:00:48this whole thing will display true now
  3596. 3:00:51in the order of operations logical
  3597. 3:00:54operators like and have a lower priority
  3598. 3:00:57than math and comparisons so in this
  3599. 3:01:00example it will calculate the math first
  3600. 3:01:03and then it will calculate the
  3601. 3:01:05comparison on the left and then the
  3602. 3:01:07comparison on the right and then at the
  3603. 3:01:09end the and operator checks if both
  3604. 3:01:12sides result in
  3605. 3:01:14true now let's use the and operator in
  3606. 3:01:17our project let's go back to the file
  3607. 3:01:20for our
  3608. 3:01:21project and then inside this first
  3609. 3:01:24condition we're going to check if the
  3610. 3:01:26random number is greater than or equal
  3611. 3:01:28to zero and at the same time is the
  3612. 3:01:32random number less than 1/3 so this is
  3613. 3:01:35how we check if a number is between 0
  3614. 3:01:38and
  3615. 3:01:391/3 in this case the computer will pick
  3616. 3:01:42Rock so between the curly brackets let's
  3617. 3:01:46just type con
  3618. 3:01:48cons. log
  3619. 3:01:52Rock let's get some practice and create
  3620. 3:01:55the other two branches for picking paper
  3621. 3:01:57and scissors feel free to pause this
  3622. 3:02:00video if you want to try it yourself
  3623. 3:02:02first so here we're going to type else
  3624. 3:02:07if to add another branch and then
  3625. 3:02:10brackets and curly brackets and in this
  3626. 3:02:13condition we're going to check if random
  3627. 3:02:18number is greater than or equal to
  3628. 3:02:231/3
  3629. 3:02:24and
  3630. 3:02:26random number is less than
  3631. 3:02:332/3 so if this random number is between
  3632. 3:02:361/3 and 2/3 inside here we're going to
  3633. 3:02:40console do log
  3634. 3:02:44paper and finally let's create another
  3635. 3:02:47Branch using else
  3636. 3:02:51if random
  3637. 3:02:54number greater than or equal to
  3638. 3:02:572/3 and random number less than
  3639. 3:03:041 and
  3640. 3:03:08console.log
  3641. 3:03:11scissors if we
  3642. 3:03:13save and then go back to our rock paper
  3643. 3:03:16scissors tab
  3644. 3:03:18and then click the rock button a few
  3645. 3:03:21times it will pick some random moves for
  3646. 3:03:24the
  3647. 3:03:25computer so that's how we generate the
  3648. 3:03:27computer's move one thing I want to
  3649. 3:03:30point out is that I use the less than
  3650. 3:03:32comparison instead of less than or equal
  3651. 3:03:35to I just did this so that the
  3652. 3:03:37comparisons don't
  3653. 3:03:40overlap before we continue in the
  3654. 3:03:42project we're going to learn two other
  3655. 3:03:44logical operators we can use let's go
  3656. 3:03:47back to booleans
  3657. 3:03:50HTML and let's also open the tab for
  3658. 3:03:52this
  3659. 3:03:55file the next logical operator is called
  3660. 3:03:58the or operator for example let's type
  3661. 3:04:02some new lines and type
  3662. 3:04:07console.log true or
  3663. 3:04:11false so this is the logical or operator
  3664. 3:04:15it checks if at least one side is true
  3665. 3:04:19so it checks if the left side is true or
  3666. 3:04:22the right side is true that's why it's
  3667. 3:04:24called the or
  3668. 3:04:26operator so here one of these sides is
  3669. 3:04:29true so the or operator will result in
  3670. 3:04:32true if we
  3671. 3:04:34save this displays
  3672. 3:04:37true if both sides are true it will also
  3673. 3:04:40result in true because at least one side
  3674. 3:04:43is
  3675. 3:04:44true the last logical operator we're
  3676. 3:04:47going to learn is called the not
  3677. 3:04:49operator for example let's create a new
  3678. 3:04:52line and type
  3679. 3:04:55console. log exclamation
  3680. 3:04:59true so the exclamation is called the
  3681. 3:05:03not operator the not operator only uses
  3682. 3:05:06one Boolean value and it flips it into
  3683. 3:05:09the opposite value so if this value is
  3684. 3:05:13true the not operator will flip it into
  3685. 3:05:16false so if we
  3686. 3:05:18save this will result in false now if
  3687. 3:05:22you think about it if we say that
  3688. 3:05:25something is not true that's the same
  3689. 3:05:28thing as saying that it is false so
  3690. 3:05:30that's why this is called the not
  3691. 3:05:33operator now if we change the value to
  3692. 3:05:38false again the not operator will flip
  3693. 3:05:41this into true so if we
  3694. 3:05:44save this will now result in true
  3695. 3:05:48so those are the logical or and not
  3696. 3:05:51operators we don't need to use these in
  3697. 3:05:54our project right now but I'll leave
  3698. 3:05:56some exercises for these at the end of
  3699. 3:05:58this lesson now let's go back and
  3700. 3:06:01continue the project let's open the code
  3701. 3:06:05and open the
  3702. 3:06:07tab the next step is to compare the
  3703. 3:06:11computer's move to our move which is
  3704. 3:06:14Rock so first let's save the computer's
  3705. 3:06:16move in a variable so we can compare it
  3706. 3:06:19later so instead of
  3707. 3:06:22console.log we're going to create a
  3708. 3:06:23variable with
  3709. 3:06:25const
  3710. 3:06:27computer capital M move and make it
  3711. 3:06:31equal to the string
  3712. 3:06:33Rock and we'll do the same thing
  3713. 3:06:36here const computer move equals
  3714. 3:06:43paper and
  3715. 3:06:45here const comp futter move equals
  3716. 3:06:52scissors and now at the bottom let's
  3717. 3:06:57console.log the computer move variable
  3718. 3:07:00to make sure that it
  3719. 3:07:02worked if we save and then click our
  3720. 3:07:05Rock
  3721. 3:07:07button unfortunately it gives us an
  3722. 3:07:09error telling us that computer move is
  3723. 3:07:12not defined even though we created it
  3724. 3:07:15right here so what is happening here if
  3725. 3:07:19statements have a special feature they
  3726. 3:07:21create something called a scope a scope
  3727. 3:07:25limits where a variable exists basically
  3728. 3:07:29whenever we have these curly brackets
  3729. 3:07:31like this any variable we create inside
  3730. 3:07:35the curly brackets will only exist
  3731. 3:07:37inside the curly brackets and we can't
  3732. 3:07:40use these variables outside the curly
  3733. 3:07:42brackets so this is called a scope
  3734. 3:07:46Scopes are a feature of many programming
  3735. 3:07:48languages and they help us avoid naming
  3736. 3:07:51conflicts for example if I create a
  3737. 3:07:55variable here called random number
  3738. 3:08:00equals
  3739. 3:08:010.5 and this didn't create a scope so if
  3740. 3:08:05I delete these curly
  3741. 3:08:07brackets this variable name will now
  3742. 3:08:10conflict with the variable name up here
  3743. 3:08:12because we can't have two variables with
  3744. 3:08:14the same name so you can imagine that if
  3745. 3:08:17we didn't have scope and we had hundreds
  3746. 3:08:20or even thousands of lines of code we
  3747. 3:08:22would quickly start to run out of
  3748. 3:08:23variable names so by creating a new
  3749. 3:08:26scope here all the variable names inside
  3750. 3:08:30the scope only exist between the curly
  3751. 3:08:33brackets and it won't affect anything
  3752. 3:08:35outside in the code so Scopes help us
  3753. 3:08:39avoid naming
  3754. 3:08:41conflicts okay so I'll delete this
  3755. 3:08:44example and we'll learn how to access
  3756. 3:08:46this variable
  3757. 3:08:48outside of this scope so to access this
  3758. 3:08:51variable outside of the if statement we
  3759. 3:08:54have to create this variable outside of
  3760. 3:08:57the if statement so above
  3761. 3:09:00here we can create this
  3762. 3:09:03variable
  3763. 3:09:05const
  3764. 3:09:07computer move and let's just make it
  3765. 3:09:10equal to the empty string for
  3766. 3:09:13now so this variable was not created
  3767. 3:09:16inside any curly brackets so it's
  3768. 3:09:19accessible from all of this code inside
  3769. 3:09:22the onclick attribute including inside
  3770. 3:09:25the if
  3771. 3:09:27statement and then inside the if
  3772. 3:09:29statement instead of creating a new
  3773. 3:09:31variable we're just going to save the
  3774. 3:09:33move inside this variable up here
  3775. 3:09:36because we can access this variable
  3776. 3:09:38anywhere else in the
  3777. 3:09:40code so instead of using const we're
  3778. 3:09:43just going to reassign computer move and
  3779. 3:09:46we're going to move it here as well and
  3780. 3:09:49here as
  3781. 3:09:51well and remember when we reassign a
  3782. 3:09:54variable we have to switch from const to
  3783. 3:09:59let and now if we
  3784. 3:10:01save and then click the rock
  3785. 3:10:04button we're able to console.log
  3786. 3:10:07computer move because computer move is
  3787. 3:10:10no longer inside this scope it's up here
  3788. 3:10:14so we can access it in this line of code
  3789. 3:10:17so that's how scope Works any variable
  3790. 3:10:20that we create inside the curly brackets
  3791. 3:10:23will only exist inside the curly
  3792. 3:10:26brackets so remember in the previous
  3793. 3:10:29lesson we learned a third way of
  3794. 3:10:31creating variables called VAR the issue
  3795. 3:10:34with VAR is that it doesn't really
  3796. 3:10:35follow the rules of scope for example
  3797. 3:10:39here if we create a variable with VAR
  3798. 3:10:43like VAR and
  3799. 3:10:45random number
  3800. 3:10:48equals
  3801. 3:10:500.5 this variable will conflict with the
  3802. 3:10:53variable up here with the same name even
  3803. 3:10:56though it's inside a scope so that's why
  3804. 3:10:59we don't use VAR anymore and we just use
  3805. 3:11:02const and let because of VAR doesn't
  3806. 3:11:04really follow the rules of scope so we
  3807. 3:11:08can remove this for now and move on to
  3808. 3:11:10the next
  3809. 3:11:13step so we generated a random move for
  3810. 3:11:16the computer
  3811. 3:11:18now we have to compare our move which is
  3812. 3:11:20Rock to the computer's move to get the
  3813. 3:11:23result and see who wins to do that we'll
  3814. 3:11:26use another if statement so at the
  3815. 3:11:29bottom here let's type if and then
  3816. 3:11:33brackets and curly
  3817. 3:11:35brackets and then inside this condition
  3818. 3:11:38let's check if the
  3819. 3:11:41computer move is equal to rock
  3820. 3:11:48so remember in JavaScript we always use
  3821. 3:11:50triple equals instead of double equals
  3822. 3:11:53to avoid the conversion
  3823. 3:11:54behavior and now if the computer's move
  3824. 3:11:57is rock and our move is Rock then inside
  3825. 3:12:01the curly brackets the result is going
  3826. 3:12:03to be a
  3827. 3:12:06tie and let's save the string in a
  3828. 3:12:08variable so we can use it later again we
  3829. 3:12:12have the same situation as before we
  3830. 3:12:14don't want to create the variable inside
  3831. 3:12:16here because then it only exists in this
  3832. 3:12:20scope between the curly brackets instead
  3833. 3:12:23we're going to create the variable above
  3834. 3:12:25outside the if statement so we'll use
  3835. 3:12:28let to create this variable because
  3836. 3:12:31we're going to reassign it in the if
  3837. 3:12:33statement let's name this variable
  3838. 3:12:36result and just make it equal to the
  3839. 3:12:39empty string at the top and then in the
  3840. 3:12:42if statement we'll save the result into
  3841. 3:12:46this variable
  3842. 3:12:48so
  3843. 3:12:49result equals
  3844. 3:12:53tie now let's create the other branches
  3845. 3:12:56of this if statement so here we'll add
  3846. 3:13:00else if brackets and curly
  3847. 3:13:04brackets and we'll check if the
  3848. 3:13:07computer move is equal to paper this
  3849. 3:13:12time if the computer move is paper and
  3850. 3:13:16we picked Rock
  3851. 3:13:17then we lose so the result is equal to
  3852. 3:13:22you
  3853. 3:13:25lose and finally we'll add another else
  3854. 3:13:29if brackets and curly brackets and we'll
  3855. 3:13:33check if the
  3856. 3:13:35computer move is equal to
  3857. 3:13:40scissors and if the computer picked
  3858. 3:13:43scissors and we pick Rock the result is
  3859. 3:13:47is that you
  3860. 3:13:51win so that's how we compare our move to
  3861. 3:13:54the computer's random move using an if
  3862. 3:13:57statement the last step is to display
  3863. 3:14:00this result in the
  3864. 3:14:03popup so at the bottom let's create a
  3865. 3:14:06popup using alert Open Bracket close
  3866. 3:14:11bracket and let's go back to super
  3867. 3:14:14simple Dev to see the message in the
  3868. 3:14:16popup so we'll click this and in the
  3869. 3:14:20popup it shows our move the computer's
  3870. 3:14:22move and then the result so we can
  3871. 3:14:25actually just copy
  3872. 3:14:27this rightclick
  3873. 3:14:31copy and then in here right click and
  3874. 3:14:36paste so obviously the computer is not
  3875. 3:14:39going to pick this move every time and
  3876. 3:14:41this is not going to be the same result
  3877. 3:14:43every time instead we want to insert
  3878. 3:14:46computer move into here and we want to
  3879. 3:14:49insert result into here so because we
  3880. 3:14:52want to insert values we're going to
  3881. 3:14:54turn this into a template string using
  3882. 3:14:57back
  3883. 3:14:59ticks and now instead of paper we're
  3884. 3:15:03going to insert a value using dollar and
  3885. 3:15:05curly brackets and we're going to insert
  3886. 3:15:08the variable
  3887. 3:15:10computer
  3888. 3:15:12move and instead of this text every time
  3889. 3:15:16we're going to
  3890. 3:15:17insert the variable
  3891. 3:15:21result and now if we
  3892. 3:15:24save and we can click okay to close this
  3893. 3:15:28and go back to our Tab and click the
  3894. 3:15:31rock
  3895. 3:15:32button we're going to pick Rock the
  3896. 3:15:34computer will pick a random move and it
  3897. 3:15:37will show us the result in a
  3898. 3:15:39popup so that's how we create this
  3899. 3:15:41simple version of rock paper scissors in
  3900. 3:15:45our code we did things step by step
  3901. 3:15:48first we generated a random number we
  3902. 3:15:51converted it into a move for the
  3903. 3:15:54computer and then we compared our move
  3904. 3:15:57to the computer move and we displayed
  3905. 3:15:59the result in a
  3906. 3:16:01popup so as you can see when we're
  3907. 3:16:04writing JavaScript we first figure out
  3908. 3:16:06what steps we need to do this is called
  3909. 3:16:09an algorithm and then we convert these
  3910. 3:16:12steps or this algorithm into
  3911. 3:16:15code so now now we can actually remove
  3912. 3:16:18this console.log because we don't need
  3913. 3:16:20it
  3914. 3:16:22anymore and the last step is to make the
  3915. 3:16:25other buttons here interactive so feel
  3916. 3:16:28free to pause this video if you want to
  3917. 3:16:29try it yourself
  3918. 3:16:31first for the paper button let's add
  3919. 3:16:35some new lines to make it a little
  3920. 3:16:36easier to read and again we'll add the
  3921. 3:16:41onclick
  3922. 3:16:43attribute and then inside here we can
  3923. 3:16:46actually make a copy of all the
  3924. 3:16:48JavaScript we wrote so we can select all
  3925. 3:16:51of
  3926. 3:16:53this and then write click
  3927. 3:16:57copy and then in here right click and
  3928. 3:17:04paste if the formatting doesn't look
  3929. 3:17:07right we can select all of these
  3930. 3:17:10lines and press tab to add indents until
  3931. 3:17:15the formatting looks good
  3932. 3:17:19the one thing we have to change here is
  3933. 3:17:21that now we picked paper so the result
  3934. 3:17:24is going to be different if the computer
  3935. 3:17:26picks Rock and we pick paper that means
  3936. 3:17:30you
  3937. 3:17:32win if the computer picks paper now it
  3938. 3:17:36means that we tie and if the computer
  3939. 3:17:40picks scissors that means you
  3940. 3:17:45lose and here what will also change up
  3941. 3:17:48pick Rock to
  3942. 3:17:51paper and that's
  3943. 3:17:55it the last step is to make the scissors
  3944. 3:17:58button interactive so again
  3945. 3:18:02onclick equals double
  3946. 3:18:05quotes and then we'll make a copy of the
  3947. 3:18:07code so I already have it so just write
  3948. 3:18:10click and
  3949. 3:18:13paste and then again select the code
  3950. 3:18:16that we need to fix the formatting for
  3951. 3:18:19and press
  3952. 3:18:21Tab and here we pick scissors so if the
  3953. 3:18:25computer picks Rock then the result is
  3954. 3:18:28you lose if the computer picks paper the
  3955. 3:18:32result is you win and if the computer
  3956. 3:18:36also picks scissors the result is a
  3957. 3:18:41tie and we'll change you pick Rock to
  3958. 3:18:45scissors
  3959. 3:18:47now if we save and try out our game if
  3960. 3:18:51we click Rock we're going to pick Rock
  3961. 3:18:54computer picks a random move and it will
  3962. 3:18:56show us the result here let's press okay
  3963. 3:19:00and try
  3964. 3:19:01paper this also works correctly let's
  3965. 3:19:05press
  3966. 3:19:06okay and click
  3967. 3:19:08scissors and this also works
  3968. 3:19:11correctly and that's it we just finished
  3969. 3:19:14creating a simple version of this rock
  3970. 3:19:16rock paper scissors
  3971. 3:19:18game in the rest of this lesson we're
  3972. 3:19:21going to learn more details about
  3973. 3:19:22booleans and if
  3974. 3:19:24statements let's go back to booleans
  3975. 3:19:28HTML and open the tab for this
  3976. 3:19:31file and let's comment out the previous
  3977. 3:19:34code using
  3978. 3:19:37slashstar and star slash and create some
  3979. 3:19:41new
  3980. 3:19:43lines the next thing we're going to
  3981. 3:19:44learn is called truthy and and falsy
  3982. 3:19:48values so if statements don't just work
  3983. 3:19:51with Boolean values like true and false
  3984. 3:19:55they actually work with any type of
  3985. 3:19:57value including numbers and strings for
  3986. 3:20:01example let's type if brackets and curly
  3987. 3:20:06brackets and inside this condition
  3988. 3:20:09instead of putting a Boolean value we're
  3989. 3:20:12going to put a number like five and then
  3990. 3:20:16in inside the curly brackets let's
  3991. 3:20:21console.log the string
  3992. 3:20:24truthy if we
  3993. 3:20:27save the number five behaves just like
  3994. 3:20:30true it causes the if statement to run
  3995. 3:20:33this code so we call this value a truthy
  3996. 3:20:37value because it behaves just like true
  3997. 3:20:40now let's change this value to a
  3998. 3:20:43different number zero if we say
  3999. 3:20:48the number zero behaves just like false
  4000. 3:20:51it causes the if statement to not run
  4001. 3:20:53this code we call this value zero a
  4002. 3:20:57falsy value because it behaves just like
  4003. 3:21:00false so how do we know if a value is
  4004. 3:21:03truthy or falsy in JavaScript the list
  4005. 3:21:07of falsy values are false the number
  4006. 3:21:11zero the empty string Nan undefined and
  4007. 3:21:16null
  4008. 3:21:17any value that is not on this list is a
  4009. 3:21:20truthy
  4010. 3:21:21value so why do we use truthy and falsy
  4011. 3:21:25values one way we use them is like a
  4012. 3:21:28shortcut in our code for example let's
  4013. 3:21:32create a variable at the bottom using
  4014. 3:21:36const and let's call it cart
  4015. 3:21:40quantity and make it equal to
  4016. 3:21:44five next let's create an if statement
  4017. 3:21:48if brackets and curly brackets and
  4018. 3:21:52inside the curly brackets let's type
  4019. 3:21:57console.log the string cart has
  4020. 3:22:03products now let's say that we only want
  4021. 3:22:06to display this message if our cart has
  4022. 3:22:09products usually inside this condition
  4023. 3:22:12we would check if cart quantity
  4024. 3:22:17is greater than zero but now instead of
  4025. 3:22:20doing this comparison we can actually
  4026. 3:22:23just type cart quantity and it will
  4027. 3:22:26behave the same way if the card quantity
  4028. 3:22:29is not zero they will both behave like
  4029. 3:22:32true if the card quantity is zero they
  4030. 3:22:36will both behave like false so here the
  4031. 3:22:39card quantity is five which is a truthy
  4032. 3:22:42value if we
  4033. 3:22:45save this caus the if statement to run
  4034. 3:22:48this code as you can see instead of
  4035. 3:22:51doing comparisons we can use truthy and
  4036. 3:22:54falsey values as
  4037. 3:22:56shortcuts truthy and falsy values also
  4038. 3:22:59work with logical operators for example
  4039. 3:23:03if we
  4040. 3:23:07console.log the not operator and the
  4041. 3:23:10number
  4042. 3:23:11zero zero is a falsy value it behaves
  4043. 3:23:15just like false so the not operator will
  4044. 3:23:18flip it into true if we
  4045. 3:23:21save this will display
  4046. 3:23:24true now that we understand truthy and
  4047. 3:23:27falsy values let's go over the list of
  4048. 3:23:30falsy values because there are some that
  4049. 3:23:32we haven't learned yet we already
  4050. 3:23:34learned false zero and the empty string
  4051. 3:23:39the next falsy value is Nan which means
  4052. 3:23:42not a
  4053. 3:23:43number we get Nan if we do some invalid
  4054. 3:23:47math for example at the bottom let's
  4055. 3:23:51type
  4056. 3:23:54console.log the string
  4057. 3:23:57text divided by the number
  4058. 3:24:00five so obviously this is not valid math
  4059. 3:24:04if we save JavaScript will tell us that
  4060. 3:24:08the result of this calculation is not a
  4061. 3:24:11number or
  4062. 3:24:12Nan the next falsy value is undefined
  4063. 3:24:16undefined represents that something
  4064. 3:24:18doesn't have a value for example at the
  4065. 3:24:22bottom if we create a variable with
  4066. 3:24:26let variable one and we don't give it a
  4067. 3:24:30value and then we
  4068. 3:24:33console. log this
  4069. 3:24:37variable and we
  4070. 3:24:39save it will show us that variable one
  4071. 3:24:42contains the value undefined so
  4072. 3:24:45undefined represents that this variable
  4073. 3:24:48doesn't have a
  4074. 3:24:50value one thing to note with undefined
  4075. 3:24:53is that we can only use this syntax with
  4076. 3:24:55let we can't use it with
  4077. 3:24:58const if we really want to use this with
  4078. 3:25:01const we have to type out equals
  4079. 3:25:05undefined
  4080. 3:25:08directly the last faly value is null
  4081. 3:25:11we'll need some extra knowledge to
  4082. 3:25:13understand null so we'll learn about
  4083. 3:25:15null later in in this
  4084. 3:25:17course the last thing we're going to
  4085. 3:25:19learn in this lesson are some shortcuts
  4086. 3:25:22for if statements called the Turner
  4087. 3:25:24operator the guard operator and the
  4088. 3:25:27default
  4089. 3:25:29operator first let's comment out this
  4090. 3:25:31code so our console doesn't get too
  4091. 3:25:33messy SL star and then at the bottom
  4092. 3:25:38star
  4093. 3:25:40slash and let's scroll down and we'll
  4094. 3:25:43start by learning the Turner operator
  4095. 3:25:46we're going to type a Boolean value like
  4096. 3:25:49true and question mark another value for
  4097. 3:25:53example the string
  4098. 3:25:56truthy and then colon and another value
  4099. 3:26:00like the string
  4100. 3:26:02falsy so this is the Turner operator
  4101. 3:26:06this is similar to an if else statement
  4102. 3:26:10the first value is like the condition if
  4103. 3:26:13it's truthy the result of this code is a
  4104. 3:26:15value after the question mark if the
  4105. 3:26:18first value is falsy the result of this
  4106. 3:26:21code is the value after the colon so you
  4107. 3:26:25can think of the question mark as the if
  4108. 3:26:27branch and the colon as the else Branch
  4109. 3:26:31this is sort of like a shortcut for an
  4110. 3:26:34if statement like
  4111. 3:26:36this now an advantage of the Turner
  4112. 3:26:39operator over an if statement is that we
  4113. 3:26:41can save a tary operator in a variable
  4114. 3:26:45so at the front
  4115. 3:26:46we can do
  4116. 3:26:48const result
  4117. 3:26:52equals and now if we
  4118. 3:26:56console.log the
  4119. 3:26:58result and
  4120. 3:27:01save the first value is true so the
  4121. 3:27:04value after the question mark gets saved
  4122. 3:27:07in the
  4123. 3:27:08variable as always we can use truthy and
  4124. 3:27:11falsy values instead of just true and
  4125. 3:27:14false so here if we change the first
  4126. 3:27:18value to a falsy value like zero this
  4127. 3:27:22will cause the value after the colon to
  4128. 3:27:25get saved inside the variable if we
  4129. 3:27:29save that's exactly what happens so this
  4130. 3:27:33is the turnery operator it's sort of a
  4131. 3:27:35shortcut for an if else statement like
  4132. 3:27:40this the next shortcut is called the
  4133. 3:27:43guard operator earlier in this lesson we
  4134. 3:27:47learned the and operator which checks if
  4135. 3:27:50two sides are both
  4136. 3:27:52truthy the and operator has a special
  4137. 3:27:55feature let's say we have an and
  4138. 3:27:57operator and the left side is
  4139. 3:28:00false in this situation we already know
  4140. 3:28:04it's impossible for both sides to be
  4141. 3:28:06truthy so we don't even need to check
  4142. 3:28:09the right side so the and operator
  4143. 3:28:12actually stops early and doesn't even
  4144. 3:28:15run the code on the right this is called
  4145. 3:28:18a shortcircuit
  4146. 3:28:19evaluation for example let's go into our
  4147. 3:28:23code and create some new lines and we'll
  4148. 3:28:26type
  4149. 3:28:28false
  4150. 3:28:29and
  4151. 3:28:31console.
  4152. 3:28:33log
  4153. 3:28:36hello if we
  4154. 3:28:38save the left side is falsy so the and
  4155. 3:28:42operator stops early or short circuits
  4156. 3:28:45and it doesn't even run the code on the
  4157. 3:28:47right so as you can see we can use the
  4158. 3:28:51value on the left to block or guard the
  4159. 3:28:54code on the right so when we use the and
  4160. 3:28:57operator like this we call it the guard
  4161. 3:29:01operator this is sort of like a shortcut
  4162. 3:29:04for an if statement like
  4163. 3:29:07this and just like the Turner operator
  4164. 3:29:10we can save the guard operator in a
  4165. 3:29:12variable for example let's create a
  4166. 3:29:16variable const
  4167. 3:29:19message and make it equal to
  4168. 3:29:23false and the string
  4169. 3:29:27hello if the first value is falsy the
  4170. 3:29:31guard operator will stop early and the
  4171. 3:29:34result will be the first value if we
  4172. 3:29:38console. log this
  4173. 3:29:42message and
  4174. 3:29:44save the value f false will be saved
  4175. 3:29:47inside this
  4176. 3:29:48variable now let's change this value to
  4177. 3:29:51a truthy value like the number five now
  4178. 3:29:55it will not stop early and it will give
  4179. 3:29:58us the second value as the result if we
  4180. 3:30:01save the string hello is now saved
  4181. 3:30:05inside this
  4182. 3:30:06variable this is sort of like a shortcut
  4183. 3:30:09for an if statement like this so don't
  4184. 3:30:12worry if it's a bit confusing all of
  4185. 3:30:15these are just shortcuts for an if
  4186. 3:30:17statement they're sort of like an if
  4187. 3:30:19statement that we can write on a single
  4188. 3:30:23line the last shortcut is called the
  4189. 3:30:26default operator which is similar to the
  4190. 3:30:28guard operator but uses
  4191. 3:30:31or earlier in this lesson we learned the
  4192. 3:30:34or operator which checks if at least one
  4193. 3:30:37side is truthy let's imagine we have an
  4194. 3:30:41or operator and the left side is
  4195. 3:30:44true in this situation we already know
  4196. 3:30:47the result of the or operator because
  4197. 3:30:50the left side is already truthy we don't
  4198. 3:30:53even need to check the right side so the
  4199. 3:30:56or operator also stops early or short
  4200. 3:30:59circuits if the first value is truthy
  4201. 3:31:03for example let's create some new lines
  4202. 3:31:07and let's say that in our final Amazon
  4203. 3:31:09Project we can choose a currency to pay
  4204. 3:31:11for our order let's create a variable
  4205. 3:31:15with
  4206. 3:31:16const currency and make it equal to the
  4207. 3:31:20string e or
  4208. 3:31:23Euros now let's say that choosing a
  4209. 3:31:26currency is optional if we don't choose
  4210. 3:31:29a currency we'll use a default value of
  4211. 3:31:32USD or United States dollars to create a
  4212. 3:31:36default value we can go to the end and
  4213. 3:31:39type or the string
  4214. 3:31:44USD so here
  4215. 3:31:46the left side is truthy so the or
  4216. 3:31:49operator will stop early and it will
  4217. 3:31:52result in the value on the
  4218. 3:31:54left if we
  4219. 3:31:58console. log the
  4220. 3:32:01currency and
  4221. 3:32:03save it will display
  4222. 3:32:06Euros now let's say that we didn't
  4223. 3:32:08choose a currency because it's optional
  4224. 3:32:11so let's go here and we're going to
  4225. 3:32:13change this currency to
  4226. 3:32:17undefined now the left side is falsy so
  4227. 3:32:21the or operator does not short circuit
  4228. 3:32:24it will continue and the result will be
  4229. 3:32:26the value on the right if we
  4230. 3:32:30save now the string USD is saved inside
  4231. 3:32:34currency as you can see we can use the
  4232. 3:32:37or operator to set a default value so
  4233. 3:32:41when we use the or operator like this we
  4234. 3:32:43call it the default operator
  4235. 3:32:47the default operator is sort of a
  4236. 3:32:49shortcut for an if statement like
  4237. 3:32:52this so don't worry remember that
  4238. 3:32:55everything that we learned in this last
  4239. 3:32:57section are basically some shortcuts for
  4240. 3:33:00if statements we don't have to use them
  4241. 3:33:02but they can save us some
  4242. 3:33:05typing and that's the end of this lesson
  4243. 3:33:08in this lesson we learned about booleans
  4244. 3:33:10and if statements Boolean values
  4245. 3:33:13represent whether something is true or
  4246. 3:33:16false we learned how to use if
  4247. 3:33:18statements to make decisions in our code
  4248. 3:33:21we learn how to use comparison and
  4249. 3:33:23logical operators we learned about
  4250. 3:33:26algorithms and created a simple version
  4251. 3:33:28of rock paper scissors we learned about
  4252. 3:33:32truthy and falsey values and we learned
  4253. 3:33:35some shortcuts for if statements the
  4254. 3:33:38turnery operator the guard operator and
  4255. 3:33:41the default operator here are some
  4256. 3:33:44exercises to help you practice booleans
  4257. 3:33:46and if
  4258. 3:34:14statements in this lesson we're going to
  4259. 3:34:16learn the next feature of JavaScript
  4260. 3:34:18called functions and we're going to use
  4261. 3:34:21functions to improve our rock paper
  4262. 3:34:24scissors project first let's create a
  4263. 3:34:27new file for this lesson we'll go to our
  4264. 3:34:30code
  4265. 3:34:31editor and create a new
  4266. 3:34:34file and let's name this file
  4267. 3:34:3807-
  4268. 3:34:41functions.
  4269. 3:34:43HTML and press enter
  4270. 3:34:46next let's copy booleans HTML to
  4271. 3:34:50functions. HTML so we'll click this file
  4272. 3:34:55and then select all this code using
  4273. 3:34:57control a or command
  4274. 3:34:59a and then right click
  4275. 3:35:03copy and in
  4276. 3:35:05here rightclick and
  4277. 3:35:10paste and we'll hide our files and
  4278. 3:35:13prepare this file for this lesson
  4279. 3:35:16so at the top we'll change the title to
  4280. 3:35:21functions and we'll remove all the code
  4281. 3:35:24in the script
  4282. 3:35:28element and let's
  4283. 3:35:30save and we'll open this file in live
  4284. 3:35:33server by
  4285. 3:35:34right-clicking open with live
  4286. 3:35:38server and we're going to open the
  4287. 3:35:40console as well so right
  4288. 3:35:42click inspect
  4289. 3:35:45and then click the
  4290. 3:35:47console and finally we can close the
  4291. 3:35:50previous
  4292. 3:35:53tabs as well as the previous
  4293. 3:35:58code okay now we're ready to learn
  4294. 3:36:02functions first of all what is a
  4295. 3:36:05function a function lets us reuse code
  4296. 3:36:10let's create our first function we're
  4297. 3:36:13going to go to the script element
  4298. 3:36:16and type the word
  4299. 3:36:18function and
  4300. 3:36:20space so this creates a new function and
  4301. 3:36:25then we're going to give a name to our
  4302. 3:36:27function we can name it almost anything
  4303. 3:36:29we want but for now let's just name it
  4304. 3:36:33function one and then we're going to
  4305. 3:36:36type round brackets and curly
  4306. 3:36:40brackets then inside these curly
  4307. 3:36:43brackets we're going to create a new
  4308. 3:36:45line
  4309. 3:36:46and we're going to type some code so
  4310. 3:36:52console.log the string
  4311. 3:36:55hello and we'll also type
  4312. 3:37:00console.log 2 +
  4313. 3:37:032 and
  4314. 3:37:06save so this creates a function now
  4315. 3:37:09let's learn how to use this function
  4316. 3:37:12we're going to type at the bottom
  4317. 3:37:16the function name so
  4318. 3:37:18function one and then Open Bracket close
  4319. 3:37:22bracket and
  4320. 3:37:24semicolon when we type the function name
  4321. 3:37:27with brackets this will run the code
  4322. 3:37:30inside the function if we
  4323. 3:37:33save notice that it runs the code inside
  4324. 3:37:36the function and displays the two lines
  4325. 3:37:39in the
  4326. 3:37:40console so why do we use functions a
  4327. 3:37:43function lets us reuse code so if we
  4328. 3:37:47wanted to run both these lines of code
  4329. 3:37:49again all we have to do is type the
  4330. 3:37:52function name again with brackets so
  4331. 3:37:55function one and brackets again if we
  4332. 3:38:01save this will run the code inside the
  4333. 3:38:03function two times and we can reuse this
  4334. 3:38:07code as many times as we want by typing
  4335. 3:38:10the function name with brackets so
  4336. 3:38:13that's what a function is it lets us
  4337. 3:38:16reuse code now let's learn the syntax
  4338. 3:38:19rules for functions the word function
  4339. 3:38:22creates a new
  4340. 3:38:24function and then we choose a name for
  4341. 3:38:27our function the rules for function
  4342. 3:38:29names are the same as variable names we
  4343. 3:38:32can't use special words like function we
  4344. 3:38:36can't start with a number and we can't
  4345. 3:38:39use special characters although dollar
  4346. 3:38:41sign and underscore are allowed and just
  4347. 3:38:44like variable names the best practice is
  4348. 3:38:47to use camo case for function
  4349. 3:38:50names and inside these curly brackets we
  4350. 3:38:53put the code that we want to run when we
  4351. 3:38:56use the function this is called the
  4352. 3:38:58function body now one very important
  4353. 3:39:02thing to remember is that this code only
  4354. 3:39:05creates a function it doesn't run the
  4355. 3:39:08code in the function or do anything else
  4356. 3:39:12in order to actually run the code we
  4357. 3:39:14have to use the function like below by
  4358. 3:39:18typing the function name with
  4359. 3:39:20brackets this is known as calling the
  4360. 3:39:23function it's also known as running the
  4361. 3:39:26function or executing the
  4362. 3:39:28function now that we learned the syntax
  4363. 3:39:31rules for functions let's do a practical
  4364. 3:39:34example we're going to use functions to
  4365. 3:39:37improve the code of our rock paper
  4366. 3:39:39scissors project first let's click here
  4367. 3:39:42to show our files and we're actually
  4368. 3:39:45going to make a copy of the project just
  4369. 3:39:48for this lesson so we're going to
  4370. 3:39:50rightclick this
  4371. 3:39:52file
  4372. 3:39:54copy and then at the bottom right click
  4373. 3:39:58and
  4374. 3:39:59paste and we're going to rename this
  4375. 3:40:02file so rightclick
  4376. 3:40:05rename remove copy at the end and then
  4377. 3:40:09at the beginning we'll change this to
  4378. 3:40:1307 and press enter
  4379. 3:40:16next we're going to open this file in
  4380. 3:40:19live server by right clicking and then
  4381. 3:40:23open with live
  4382. 3:40:25server and let's also open the console
  4383. 3:40:28on this page we'll right click
  4384. 3:40:32inspect and then click the
  4385. 3:40:36console all right now if we look at the
  4386. 3:40:39code for generating a computer move
  4387. 3:40:43you'll notice that this code is the same
  4388. 3:40:45for all three
  4389. 3:40:47buttons so this is a perfect situation
  4390. 3:40:50to use a function because a function
  4391. 3:40:53lets us reuse code so let's scroll down
  4392. 3:40:57to the script element and we're going to
  4393. 3:41:00make a function here so let's type
  4394. 3:41:04function and then a function name let's
  4395. 3:41:08name this function
  4396. 3:41:10pick
  4397. 3:41:12computer move and then brackets and
  4398. 3:41:15curly brackets so one recommendation for
  4399. 3:41:19function names is to use a verb or an
  4400. 3:41:22action in the name such as
  4401. 3:41:25pick so next we're going to move all the
  4402. 3:41:28code for picking a computer move into
  4403. 3:41:31our new function so let's create a new
  4404. 3:41:34line here and we'll scroll up to the
  4405. 3:41:38scissors button because it's the closest
  4406. 3:41:40one and we'll select all of this code
  4407. 3:41:43for picking a computer move and then
  4408. 3:41:46rightclick
  4409. 3:41:48cut and inside the
  4410. 3:41:51function right click and
  4411. 3:41:56paste if we need to fix the formatting
  4412. 3:41:59we can select these lines and press tab
  4413. 3:42:02to add
  4414. 3:42:04indents so the reason we created this
  4415. 3:42:06function in the script element is
  4416. 3:42:08because remember that the script element
  4417. 3:42:10runs when the page is loaded so we want
  4418. 3:42:13to make sure that we create the function
  4419. 3:42:16first and then we use it in the buttons
  4420. 3:42:19after now remember this code just
  4421. 3:42:22creates a function it doesn't actually
  4422. 3:42:25run the code inside to run the code
  4423. 3:42:27inside we're going to call this function
  4424. 3:42:31so let's scroll up to the scissors
  4425. 3:42:33button and we're going to call this
  4426. 3:42:36function by typing the function name
  4427. 3:42:39pick computer move Open Bracket close
  4428. 3:42:44bracket ET and
  4429. 3:42:46semicolon if we
  4430. 3:42:49save and then click the scissors
  4431. 3:42:52button unfortunately it now gives us an
  4432. 3:42:55error saying computer move is not
  4433. 3:42:58defined now this might seem weird
  4434. 3:43:00because down here we actually create the
  4435. 3:43:03computer move so what's going on the
  4436. 3:43:06reason this happens is because functions
  4437. 3:43:09create a scope in the previous lesson we
  4438. 3:43:12learned about Scopes which limit weight
  4439. 3:43:15where a variable exists we learned about
  4440. 3:43:18if statements and that if statements
  4441. 3:43:20create a scope between the curly
  4442. 3:43:22brackets this means that any variable
  4443. 3:43:25created between the curly brackets only
  4444. 3:43:28exist between the curly
  4445. 3:43:30brackets functions also create a scope
  4446. 3:43:34any variable that is created between
  4447. 3:43:37these curly brackets only exists inside
  4448. 3:43:41the curly brackets and we can't use it
  4449. 3:43:43outside the curly Brack ETS like we do
  4450. 3:43:46here so that's why we're getting this
  4451. 3:43:49error so how do we solve this problem
  4452. 3:43:53remember that with if statements we
  4453. 3:43:55solve this by creating the variable
  4454. 3:43:57outside of the if statement like this we
  4455. 3:44:01can use a similar solution with
  4456. 3:44:03functions to make this variable exist
  4457. 3:44:06outside the function we just have to
  4458. 3:44:08create it outside the function so let's
  4459. 3:44:11select this code we're going to
  4460. 3:44:14rightclick
  4461. 3:44:16and
  4462. 3:44:17cut and remove these lines and then
  4463. 3:44:21outside the function we're going to
  4464. 3:44:24rightclick and
  4465. 3:44:27paste so now this variable is no longer
  4466. 3:44:31inside the function's scope and it can
  4467. 3:44:34be used anywhere else in the code so
  4468. 3:44:37variables like this that can be accessed
  4469. 3:44:39anywhere are called Global
  4470. 3:44:42variables if we save
  4471. 3:44:45and now click the scissors
  4472. 3:44:47button everything is working again
  4473. 3:44:51because this code above can now access
  4474. 3:44:53this
  4475. 3:44:55variable now let's press
  4476. 3:44:57okay and let's use this function to
  4477. 3:45:00reuse this code so we'll scroll up to
  4478. 3:45:04the paper
  4479. 3:45:06button and now instead of all this code
  4480. 3:45:09again we're just going to remove it and
  4481. 3:45:12call the function pick
  4482. 3:45:16computer move Open Bracket close bracket
  4483. 3:45:20semicolon and also with the rock button
  4484. 3:45:24we'll select all this
  4485. 3:45:26code and remove it and just call the
  4486. 3:45:29function
  4487. 3:45:31pick
  4488. 3:45:32computer move brackets and
  4489. 3:45:36semicolon now if we
  4490. 3:45:39save and click the rock
  4491. 3:45:42button it works just like before
  4492. 3:45:45we'll press okay and click the paper
  4493. 3:45:48button it also works just like before so
  4494. 3:45:52as you can see functions let us reuse
  4495. 3:45:55code and they make our code a lot
  4496. 3:45:57cleaner by removing all the duplication
  4497. 3:46:00that we had another benefit of removing
  4498. 3:46:03duplication is that before if we ever
  4499. 3:46:06wanted to update the code for picking a
  4500. 3:46:09computer move we had to update it in
  4501. 3:46:11three places in each of the buttons but
  4502. 3:46:15now if we scroll
  4503. 3:46:17down we only have to update it in one
  4504. 3:46:20place inside this function so functions
  4505. 3:46:23also make our code easier to update in
  4506. 3:46:26the
  4507. 3:46:27future next we're going to learn a
  4508. 3:46:29feature of functions called a return
  4509. 3:46:32statement a return statement lets us get
  4510. 3:46:35a value out of a function so at the end
  4511. 3:46:39of this function let's type some new
  4512. 3:46:43lines and we're going to type type
  4513. 3:46:46return and then a value like the number
  4514. 3:46:50five and
  4515. 3:46:52semicolon now whenever we call this
  4516. 3:46:55function it will result in a value the
  4517. 3:46:58number five so up
  4518. 3:47:02here when we call this function this
  4519. 3:47:05will result in the number five so we can
  4520. 3:47:08actually console.log this console.log
  4521. 3:47:16and then
  4522. 3:47:18save and click the scissors
  4523. 3:47:21button and it will show us that calling
  4524. 3:47:24the function now results in the number
  4525. 3:47:26five and we display it in the
  4526. 3:47:29console so that's how a return statement
  4527. 3:47:32works it lets us get a value out of this
  4528. 3:47:36function let's press okay and do another
  4529. 3:47:40example we'll scroll
  4530. 3:47:42down and this time let's change CH this
  4531. 3:47:45value to the string
  4532. 3:47:48Rock now whenever we call this function
  4533. 3:47:51it will result in the string Rock if we
  4534. 3:47:55save and then click
  4535. 3:47:59scissors and scroll up
  4536. 3:48:01here calling this function now results
  4537. 3:48:04in the string Rock and then we display
  4538. 3:48:07it in the
  4539. 3:48:09console so now let's press okay and
  4540. 3:48:12we'll learn the syntax rules for a
  4541. 3:48:14return
  4542. 3:48:16statement so we just have to type the
  4543. 3:48:19word return and then a value and it will
  4544. 3:48:22get this value out of the function now
  4545. 3:48:26instead of just a value we can also
  4546. 3:48:28return a calculation or a variable or
  4547. 3:48:31anything that results in a
  4548. 3:48:34value so this is known as returning a
  4549. 3:48:37value from a function and this value is
  4550. 3:48:41known as the return value we also Al
  4551. 3:48:44don't have to return anything from a
  4552. 3:48:47function if a function doesn't have a
  4553. 3:48:49return statement or we return without a
  4554. 3:48:52value like this this will return the
  4555. 3:48:56value
  4556. 3:48:57undefined so if we
  4557. 3:49:00save and then click scissors
  4558. 3:49:03again the function will return
  4559. 3:49:07undefined and lastly when we use a
  4560. 3:49:10return statement it ends the function
  4561. 3:49:12immediately so after the the return
  4562. 3:49:15statement if we type
  4563. 3:49:20console.log the string
  4564. 3:49:24after and press okay here and save this
  4565. 3:49:29file and then click the scissors
  4566. 3:49:32button notice that it doesn't run this
  4567. 3:49:35code and that's because once we return
  4568. 3:49:38the code returns back to where we called
  4569. 3:49:41the function that's why it's called a
  4570. 3:49:43return statement so it's not going to
  4571. 3:49:45run anything after the return so we can
  4572. 3:49:49remove this
  4573. 3:49:51code and press
  4574. 3:49:54okay so now that we understand return
  4575. 3:49:56statements we're going to learn a better
  4576. 3:49:59way of getting the computer move out of
  4577. 3:50:02this function so first let's actually
  4578. 3:50:05move this variable back into the
  4579. 3:50:07function so we right click and
  4580. 3:50:11cut and then in
  4581. 3:50:13here right right click and
  4582. 3:50:18paste and delete these
  4583. 3:50:22lines so now we're back to the original
  4584. 3:50:25problem how do we access computer move
  4585. 3:50:28outside of the function so another
  4586. 3:50:31solution is to return this variable
  4587. 3:50:34because remember a return statement lets
  4588. 3:50:37us get a value out of the function so
  4589. 3:50:40down
  4590. 3:50:41here we're going to return the
  4591. 3:50:45computer
  4592. 3:50:47move so this will take whatever value is
  4593. 3:50:50saved inside computer move which is one
  4594. 3:50:53of these values and return it out of the
  4595. 3:50:56function if we
  4596. 3:50:58save and then click these scissors
  4597. 3:51:02button we were able to get the move
  4598. 3:51:04scissors outside of this function so
  4599. 3:51:08what happened here is that we return
  4600. 3:51:10this move and then up
  4601. 3:51:13here we took the move and displayed it
  4602. 3:51:15in the console to get
  4603. 3:51:18scissors so that's another way of
  4604. 3:51:20getting the computer move out of a
  4605. 3:51:22function using the return
  4606. 3:51:26statement however now we're back to this
  4607. 3:51:29problem with computer move not being
  4608. 3:51:32defined because we moved it back into
  4609. 3:51:34the function's scope so let's scroll up
  4610. 3:51:38and remember that this return value is
  4611. 3:51:42just a value we can use it like any
  4612. 3:51:45other value for example we can
  4613. 3:51:48console.log it or we can also save this
  4614. 3:51:51in a variable so let's do that right now
  4615. 3:51:55we're going to remove
  4616. 3:51:57this and create a new variable with
  4617. 3:52:01const and let's just name this variable
  4618. 3:52:05computer move to match what we use down
  4619. 3:52:09here and we use equals to save the
  4620. 3:52:13return value inside inside this
  4621. 3:52:15variable so note that even though we use
  4622. 3:52:18computer move here as well as here
  4623. 3:52:22there're different variables this one is
  4624. 3:52:25inside the function's scope so it
  4625. 3:52:27prevents it from conflicting with
  4626. 3:52:29variables outside the function's scope
  4627. 3:52:32that's one of the main benefits of
  4628. 3:52:35Scopes so now that we saved the return
  4629. 3:52:38value into this variable and we're using
  4630. 3:52:41this variable down here the code should
  4631. 3:52:44now work work if we
  4632. 3:52:46save and click
  4633. 3:52:49scissors everything is back to
  4634. 3:52:52normal and let's press okay so don't
  4635. 3:52:55worry functions can be a little
  4636. 3:52:57confusing at first because the code is
  4637. 3:52:59no longer simply going from top to
  4638. 3:53:02bottom it sort of jumps around a little
  4639. 3:53:05bit so let's go through this step by
  4640. 3:53:08step when we click this button we're
  4641. 3:53:11going to run this function so the code
  4642. 3:53:14is going going to jump down here and
  4643. 3:53:16then it's going to run all of this code
  4644. 3:53:18from top to
  4645. 3:53:20bottom at the end we're going to return
  4646. 3:53:24whatever is inside computer move back to
  4647. 3:53:27where we called this function so we're
  4648. 3:53:30going to return all the way back here
  4649. 3:53:33and then we're going to save that return
  4650. 3:53:35value into a new variable computer move
  4651. 3:53:39and then use computer move down
  4652. 3:53:42here so following the code line by line
  4653. 3:53:45like this is called tracing the code
  4654. 3:53:48it's a useful technique to understand
  4655. 3:53:50exactly what the code is
  4656. 3:53:52doing now let's compare the two
  4657. 3:53:54solutions that we used a global variable
  4658. 3:53:58and returning a
  4659. 3:54:01variable so returning a variable like
  4660. 3:54:04this is actually preferred over using a
  4661. 3:54:07global variable and that's because a
  4662. 3:54:10scope can help us prevent naming
  4663. 3:54:12conflicts so all the variables inside
  4664. 3:54:15here will not conflict with anything
  4665. 3:54:18outside the scope so generally it's a
  4666. 3:54:21best practice to keep things inside a
  4667. 3:54:23scope if we
  4668. 3:54:26can now let's move on and use this
  4669. 3:54:28function in the other buttons feel free
  4670. 3:54:31to pause the video if you want to try it
  4671. 3:54:34yourself
  4672. 3:54:35first so we'll scroll up to the paper
  4673. 3:54:39button and then here we're going to save
  4674. 3:54:43The Returned computer move into a
  4675. 3:54:46variable so
  4676. 3:54:48const
  4677. 3:54:50computer move so that it matches the
  4678. 3:54:53code down here make it equal to whatever
  4679. 3:54:57is returned and then same thing for the
  4680. 3:55:00rock button we're going to save the
  4681. 3:55:02return value in a variable const
  4682. 3:55:06computer move
  4683. 3:55:09equals if we
  4684. 3:55:12save and click the rock button
  4685. 3:55:16it
  4686. 3:55:17works let's press okay and click the
  4687. 3:55:20paper button and this also works and
  4688. 3:55:24let's press okay so that's how we use a
  4689. 3:55:27return statement to take advantage of
  4690. 3:55:29the benefits of
  4691. 3:55:31scope next if we look at the rest of
  4692. 3:55:35this code where we compare our move and
  4693. 3:55:38display the result you'll notice that
  4694. 3:55:40this code is very similar for each of
  4695. 3:55:43the buttons you the only thing that's
  4696. 3:55:44different is the result and the message
  4697. 3:55:48that we display so we can actually use
  4698. 3:55:51functions here as well to reuse code
  4699. 3:55:54that has small differences to do this
  4700. 3:55:57we're going to need another feature of
  4701. 3:55:59functions called
  4702. 3:56:01parameters so let's go back into
  4703. 3:56:03functions.
  4704. 3:56:05HTML and let's open the tab for this
  4705. 3:56:10file and let's also comment out the
  4706. 3:56:12previous code by typing
  4707. 3:56:14slash star and at the bottom star
  4708. 3:56:20slash and create some new
  4709. 3:56:23lines and now we're ready to learn
  4710. 3:56:25parameters so parameters are sort of the
  4711. 3:56:28opposite of a return statement a return
  4712. 3:56:31statement gets a value out of a function
  4713. 3:56:35a parameter puts a value into a function
  4714. 3:56:39so let's do an example let's say that
  4715. 3:56:41we're working on the final Amazon
  4716. 3:56:43Project
  4717. 3:56:44and we need to calculate the tax for our
  4718. 3:56:47order let's create a function to
  4719. 3:56:50calculate the tax so
  4720. 3:56:53function let's name it
  4721. 3:56:57calculate tax brackets and curly
  4722. 3:57:01brackets and then inside the curly
  4723. 3:57:03brackets let's say that the cost of our
  4724. 3:57:06products is
  4725. 3:57:081,000 or $10 and the tax is
  4726. 3:57:1210% to calc calculate the tax we can do
  4727. 3:57:161,000 *
  4728. 3:57:190.1 which is
  4729. 3:57:2110% let's also display this number in
  4730. 3:57:24the console so at the front we'll type
  4731. 3:57:30console.log and
  4732. 3:57:34brackets now let's save and remember
  4733. 3:57:38this code only creates a function it
  4734. 3:57:40doesn't run the code inside to run this
  4735. 3:57:43code we're going to call this function
  4736. 3:57:46so down here we're going to type the
  4737. 3:57:48function name calculate tax and then
  4738. 3:57:54brackets if we
  4739. 3:57:56save it runs this code and then it
  4740. 3:57:59displays the tax which is 100 cents or
  4741. 3:58:04$1 however right now this function only
  4742. 3:58:07calculates the tax for
  4743. 3:58:101,000 what if we want to calculate the
  4744. 3:58:12tax for a different amount how would we
  4745. 3:58:15modify this function to work with any
  4746. 3:58:18number to do this we're going to learn a
  4747. 3:58:21feature of functions called parameters a
  4748. 3:58:24parameter lets us put a value into a
  4749. 3:58:27function so let's finally create our
  4750. 3:58:30first parameter so inside these round
  4751. 3:58:33brackets we're going to choose a name
  4752. 3:58:36for our parameter so let's just name
  4753. 3:58:38this
  4754. 3:58:40parameter
  4755. 3:58:421 a parameter one works the same way as
  4756. 3:58:45a variable we can save a value inside
  4757. 3:58:49this parameter and then use it just like
  4758. 3:58:51a
  4759. 3:58:52variable so first let's save a value
  4760. 3:58:55inside the parameter to do that we're
  4761. 3:58:58going to go to where we call the
  4762. 3:59:00function and then inside these brackets
  4763. 3:59:03we're going to put a value for example
  4764. 3:59:082,000 this will save this value into
  4765. 3:59:12this parameter
  4766. 3:59:14and now we can use it just like a
  4767. 3:59:16variable so instead of
  4768. 3:59:181,000 we're going to type
  4769. 3:59:21parameter 1 and it will take whatever
  4770. 3:59:25value is inside parameter 1 in this case
  4771. 3:59:292,000 and substitute it into the code if
  4772. 3:59:33we
  4773. 3:59:34save it now calculates 2,000 *
  4774. 3:59:380.1 to give us 200 cents as the tax
  4775. 3:59:44now every time we call the function we
  4776. 3:59:46can save a different value into the
  4777. 3:59:49parameter for example let's create a new
  4778. 3:59:52line and call the function
  4779. 3:59:56calculate
  4780. 3:59:59tax and this time we're going to save
  4781. 4:00:02the value
  4782. 4:00:035,000 into parameter 1 if we
  4783. 4:00:08save this line of code will calculate
  4784. 4:00:11the tax for 5,000
  4785. 4:00:14and result in 500 cents so as you can
  4786. 4:00:17see a parameter lets us put values into
  4787. 4:00:21a function and this allows our function
  4788. 4:00:24to work with any
  4789. 4:00:26value now let's learn the syntax rules
  4790. 4:00:29for parameters to create a parameter we
  4791. 4:00:33just type the parameter name between the
  4792. 4:00:36brackets parameter names follow the same
  4793. 4:00:38rules as variable names we can't use
  4794. 4:00:42special words we can't start with a
  4795. 4:00:44number and we can't use special
  4796. 4:00:46characters other than dollar sign and
  4797. 4:00:50underscore and the best practice is to
  4798. 4:00:52use cam
  4799. 4:00:53case let's actually change this
  4800. 4:00:56parameter name to cost so the code makes
  4801. 4:00:59more
  4802. 4:01:00sense and
  4803. 4:01:03save next when a function has a
  4804. 4:01:06parameter we usually say this function
  4805. 4:01:09takes a parameter if this parameter
  4806. 4:01:12should be a number we say this function
  4807. 4:01:15takes a
  4808. 4:01:16number when we put a value into the
  4809. 4:01:19function like this we call this passing
  4810. 4:01:22a value into the function another name
  4811. 4:01:25for this value is called the
  4812. 4:01:28argument and believe it or not we've
  4813. 4:01:31actually used functions and parameters
  4814. 4:01:33this whole time the first code that we
  4815. 4:01:36learned in this course was alert alert
  4816. 4:01:40is actually a function you can tell it's
  4817. 4:01:43a function because of the round brackets
  4818. 4:01:46and between the brackets we put a string
  4819. 4:01:49so this is an example of a
  4820. 4:01:51parameter and lastly a parameter only
  4821. 4:01:55exists inside the function's scope so we
  4822. 4:01:58can only use this parameter between the
  4823. 4:02:01curly brackets we can't use it outside
  4824. 4:02:04the
  4825. 4:02:05function next a function can actually
  4826. 4:02:08have more than one parameter for example
  4827. 4:02:12right now our calculate TX function can
  4828. 4:02:14only calculate a 10% tax but what if
  4829. 4:02:18we're in a different country and we want
  4830. 4:02:20to calculate a different tax percent to
  4831. 4:02:23solve this we can add a second parameter
  4832. 4:02:27by typing after the first parameter a
  4833. 4:02:30comma and then another parameter name
  4834. 4:02:34let's name this tax
  4835. 4:02:38percent and now to save a value into the
  4836. 4:02:42second parameter we'll go go to where we
  4837. 4:02:44call this function and also type a comma
  4838. 4:02:48and a second value like
  4839. 4:02:510.2 or a 20% tax so the first value gets
  4840. 4:02:57saved in the first parameter and the
  4841. 4:02:59second value gets saved in the second
  4842. 4:03:02parameter and now we can use our second
  4843. 4:03:05parameter just like a variable so
  4844. 4:03:08instead of 0.1 every time we're going to
  4845. 4:03:11calculate it with tax
  4846. 4:03:15percent and it will substitute whatever
  4847. 4:03:18value is inside tax percent into the
  4848. 4:03:21code if we
  4849. 4:03:23save this first line will multiply 2,000
  4850. 4:03:27by
  4851. 4:03:280.2 and give us 400 cents as the
  4852. 4:03:32result so we can have as many parameters
  4853. 4:03:35as we want in a function we just have to
  4854. 4:03:38separate them with a
  4855. 4:03:40comma Now notice that the second time we
  4856. 4:03:42call this function we didn't give a
  4857. 4:03:45value for the second parameter if we
  4858. 4:03:48don't save a value into the parameter
  4859. 4:03:50the parameter will get the value
  4860. 4:03:53undefined so here if we
  4861. 4:03:57console. log the second parameter tax
  4862. 4:04:03percent and
  4863. 4:04:05save it will show us that the second
  4864. 4:04:08time tax percent is
  4865. 4:04:11undefined and then multiplying the cost
  4866. 4:04:15time undefined is not valid math so we
  4867. 4:04:18get the result Nan or not a number so
  4868. 4:04:23now let's remove the
  4869. 4:04:25console.log and we'll learn how to fix
  4870. 4:04:28this so to fix this we can either pass a
  4871. 4:04:31second value into the function like this
  4872. 4:04:34line or we can also set a default value
  4873. 4:04:38for the parameter to set a default value
  4874. 4:04:41we can just type after the parameter
  4875. 4:04:45and then add equal sign and a default
  4876. 4:04:48value like
  4877. 4:04:520.1 if we
  4878. 4:04:54save the second function call Will
  4879. 4:04:57multiply 5,000 by the default value
  4880. 4:05:010.1 and give us
  4881. 4:05:03500 so that's how we add a default value
  4882. 4:05:07for a
  4883. 4:05:08parameter all right now that we learned
  4884. 4:05:11parameters we're going to go back to the
  4885. 4:05:13rock paper scissors project and reuse
  4886. 4:05:16even more code so let's go back to the
  4887. 4:05:19file for the project and also the tab
  4888. 4:05:23for this
  4889. 4:05:24project and now if we look at the code
  4890. 4:05:27inside each of the buttons you'll notice
  4891. 4:05:30that the code is basically the same for
  4892. 4:05:33all three buttons the only small
  4893. 4:05:35difference is the move that we picked so
  4894. 4:05:39this is a perfect situation to use a
  4895. 4:05:42parameter so so inside the script
  4896. 4:05:44element let's create a new function to
  4897. 4:05:48reuse all of this code we'll type
  4898. 4:05:53function and let's name this function
  4899. 4:05:56play game brackets and curly
  4900. 4:06:01brackets next let's move all of this
  4901. 4:06:04code inside the function so we can reuse
  4902. 4:06:07it we'll select this
  4903. 4:06:10code and then
  4904. 4:06:12rightclick
  4905. 4:06:14cut and inside the
  4906. 4:06:17function right click and
  4907. 4:06:22paste and if we need to reformat we can
  4908. 4:06:25select these lines and press
  4909. 4:06:30tab so the value that is different for
  4910. 4:06:32each button is the move that we picked
  4911. 4:06:36so we're going to turn this move into a
  4912. 4:06:39parameter so at the top between the
  4913. 4:06:42brackets we're going to create a new
  4914. 4:06:45parameter let's name it Player
  4915. 4:06:50move and now let's save a value into
  4916. 4:06:53this parameter so inside our scissors
  4917. 4:06:57button we're going to call this function
  4918. 4:06:59and run all this code so let's type play
  4919. 4:07:04game and call this function and for the
  4920. 4:07:08scissors button let's save the string
  4921. 4:07:11scissors into Player move to do that
  4922. 4:07:14we'll type between the brackets here and
  4923. 4:07:18type the string
  4924. 4:07:21scissors so as we learned this will save
  4925. 4:07:24scissors into Player move and now we can
  4926. 4:07:28use Player move just like a variable in
  4927. 4:07:30the
  4928. 4:07:32function now let's modify the code based
  4929. 4:07:35on the parameter so here this code
  4930. 4:07:38calculates the result however this only
  4931. 4:07:41works if the player move is scissors so
  4932. 4:07:45we're going to put this in an if
  4933. 4:07:47statement so we'll type
  4934. 4:07:51if
  4935. 4:07:52Player move equals
  4936. 4:07:57scissors and curly brackets inside this
  4937. 4:08:01if statement we're going to put this if
  4938. 4:08:03statement so let's select this and then
  4939. 4:08:07right click
  4940. 4:08:09cut and then inside
  4941. 4:08:12here right click click and
  4942. 4:08:16paste and select these lines and press
  4943. 4:08:20tab so notice that we can put an if
  4944. 4:08:23statement inside another if statement
  4945. 4:08:26and we did this because this code only
  4946. 4:08:29makes sense if the player move is
  4947. 4:08:32scissors and finally let's modify the
  4948. 4:08:35code that displays the message so here
  4949. 4:08:39instead of displaying scissors every
  4950. 4:08:41time we're going to display whatever is
  4951. 4:08:43saved inside Player
  4952. 4:08:45move so we'll replace it with Dollar
  4953. 4:08:49open curly bracket close curly bracket
  4954. 4:08:52and in between we'll insert Player
  4955. 4:08:57move and that's it we modify this
  4956. 4:09:00function to use a parameter instead of
  4957. 4:09:03always using
  4958. 4:09:04scissors now let's save and click the
  4959. 4:09:08scissors
  4960. 4:09:09button and you'll notice that it works
  4961. 4:09:12we displayed the move scissors and we
  4962. 4:09:15calculated the result
  4963. 4:09:18correctly let's press okay and we're
  4964. 4:09:21going to use this function to reuse the
  4965. 4:09:23code for the other buttons so we'll
  4966. 4:09:26scroll up and for the paper button we're
  4967. 4:09:30going to use a different if statement to
  4968. 4:09:33calculate the result so let's actually
  4969. 4:09:36copy this into the function we're going
  4970. 4:09:39to
  4971. 4:09:40rightclick click copy
  4972. 4:09:43and then inside the
  4973. 4:09:45function we're going to create another
  4974. 4:09:48Branch if the player move is paper so
  4975. 4:09:52here let's type else if brackets and
  4976. 4:09:57curly
  4977. 4:09:58brackets inside this condition we're
  4978. 4:10:01going to check if the
  4979. 4:10:03player move is equal to paper this time
  4980. 4:10:10and then inside these curly brackets
  4981. 4:10:12we're going going to paste the if
  4982. 4:10:14statement we copied earlier so right
  4983. 4:10:17click and
  4984. 4:10:20paste and we can select these lines and
  4985. 4:10:23press tab to
  4986. 4:10:26reformat so now we have the code that
  4987. 4:10:28determines the result for paper down
  4988. 4:10:32here we don't need to modify this code
  4989. 4:10:35because we're always displaying whatever
  4990. 4:10:37is inside Player
  4991. 4:10:38move finally we can scroll up to the
  4992. 4:10:42paper button
  4993. 4:10:43and remove all this duplicated code and
  4994. 4:10:47just use our function play
  4995. 4:10:51game and we're going to pass paper
  4996. 4:10:56inside so now paper will get saved
  4997. 4:10:59inside Player move and it's going to run
  4998. 4:11:03this if statement
  4999. 4:11:04instead if we
  5000. 4:11:07save and click the paper
  5001. 4:11:10button in this example I pick paper
  5002. 4:11:14computer pick Rock and it calculated the
  5003. 4:11:16result
  5004. 4:11:18correctly let's press okay and use this
  5005. 4:11:22function for the rock button feel free
  5006. 4:11:25to pause the video if you want to try it
  5007. 4:11:27yourself
  5008. 4:11:28first so let's scroll up to the Rock
  5009. 4:11:31button and first we need to copy this if
  5010. 4:11:34statement into the function so let's
  5011. 4:11:38rightclick
  5012. 4:11:40copy and then inside the function
  5013. 4:11:44let's create a branch for rock else if
  5014. 4:11:50Player move is equal to
  5015. 4:11:55rock and then curly brackets and inside
  5016. 4:11:58here we're going to rightclick and paste
  5017. 4:12:02the if statement we copied
  5018. 4:12:04earlier and now let's select these lines
  5019. 4:12:07and press tab a few times to
  5020. 4:12:10reformat and again this doesn't need
  5021. 4:12:13need to
  5022. 4:12:14change the last step is to scroll up to
  5023. 4:12:17our Rock button and instead of
  5024. 4:12:20duplicating all of this code we're going
  5025. 4:12:23to remove it and just call our function
  5026. 4:12:27instead play game brackets and then
  5027. 4:12:32inside we'll have the string
  5028. 4:12:36Rock now let's
  5029. 4:12:38save and click the rock
  5030. 4:12:41button so I picked Rock the computer
  5031. 4:12:45picked paper and we calculated the
  5032. 4:12:47result
  5033. 4:12:48correctly let's press okay so by using a
  5034. 4:12:53function with a parameter we were able
  5035. 4:12:55to reuse all of the code in our buttons
  5036. 4:12:59and you can see that it made our code a
  5037. 4:13:01lot cleaner so lastly notice that inside
  5038. 4:13:05a function we can call other functions
  5039. 4:13:09let's go through the code step by step
  5040. 4:13:11so that we understand how how it works
  5041. 4:13:14so here when we call the play game
  5042. 4:13:18function we go inside here and then we
  5043. 4:13:21call the pick computer move function so
  5044. 4:13:24now we're going to go inside this
  5045. 4:13:27function and run all the code and then
  5046. 4:13:30when this function returns we're going
  5047. 4:13:33to return back to where we called it and
  5048. 4:13:37then keep going and run the rest of the
  5049. 4:13:40code and then when the play game
  5050. 4:13:42function finishes it's going to return
  5051. 4:13:45back to
  5052. 4:13:47here so inside a function we can call
  5053. 4:13:51other
  5054. 4:13:52functions now one last thing we're going
  5055. 4:13:54to do for this code is we'll scroll
  5056. 4:13:58down and we're just going to add some
  5057. 4:14:00new lines to this if statement to
  5058. 4:14:03separate the branches and make the code
  5059. 4:14:05a little easier to
  5060. 4:14:09read and
  5061. 4:14:11save and let's scroll
  5062. 4:14:14up and that's the end of this
  5063. 4:14:16lesson in this lesson we learned about
  5064. 4:14:19functions which let us reuse code we
  5065. 4:14:23learned how to get a value out of a
  5066. 4:14:25function using return we learn how to
  5067. 4:14:28put values into a function using
  5068. 4:14:31parameters and we improve the code for
  5069. 4:14:34our rock paper scissors project by
  5070. 4:14:37reusing the code and removing
  5071. 4:14:39duplication
  5072. 4:14:43thanks for watching this course so far
  5073. 4:14:45if you want to support this channel you
  5074. 4:14:47can check out the premium version of my
  5075. 4:14:49courses just like this one where you can
  5076. 4:14:51get a certificate of completion and a
  5077. 4:14:54Better Learning platform if you're
  5078. 4:14:56interested in extra videos from me more
  5079. 4:14:59in-depth videos and behind the scenes
  5080. 4:15:01content you can check out these links
  5081. 4:15:04here you can also support the channel by
  5082. 4:15:07liking the video subscribing and sharing
  5083. 4:15:09this video with others thanks again and
  5084. 4:15:12let's go continue with the
  5085. 4:15:15course here are some exercises you can
  5086. 4:15:17do on your own to practice using
  5087. 4:15:20functions
  5088. 4:15:48in this lesson we're going to learn
  5089. 4:15:49another type of value in JavaScript
  5090. 4:15:52called objects and we're going to use
  5091. 4:15:55objects to create a score in our rock
  5092. 4:15:58paper scissors project first let's
  5093. 4:16:01create a new file for this lesson we'll
  5094. 4:16:04go to our code editor and click this
  5095. 4:16:08icon and then click this icon to create
  5096. 4:16:11a new file and we'll name this file
  5097. 4:16:1608-
  5098. 4:16:18objects.
  5099. 4:16:19HTML and press
  5100. 4:16:22enter next we'll copy all the code in
  5101. 4:16:25functions. HTML to objects. HTML so
  5102. 4:16:30we'll type here and press contrl a or
  5103. 4:16:33command a to select the code and then
  5104. 4:16:36right click
  5105. 4:16:38copy and in
  5106. 4:16:41here right click
  5107. 4:16:43and
  5108. 4:16:44paste now let's hide the files for now
  5109. 4:16:48and we'll prepare this file for this
  5110. 4:16:50lesson so we can change this to objects
  5111. 4:16:55and remove all the code in the script
  5112. 4:16:59element finally let's save and open this
  5113. 4:17:04in live server by right clicking open
  5114. 4:17:07with live
  5115. 4:17:09server and let's also open the console
  5116. 4:17:12on on this page so we'll right click
  5117. 4:17:16click
  5118. 4:17:17inspect and click the
  5119. 4:17:21console let's close our previous
  5120. 4:17:24tabs and also the previous
  5121. 4:17:29code and now we're ready to learn
  5122. 4:17:31objects so what is an object an object
  5123. 4:17:36groups multiple values together let's do
  5124. 4:17:39an example and create our first object
  5125. 4:17:43we'll go to the script element and we'll
  5126. 4:17:45create a variable using const and we'll
  5127. 4:17:50name this variable product and make it
  5128. 4:17:53equal to open curly bracket close curly
  5129. 4:17:57bracket and semic it so this is an
  5130. 4:18:02object inside this object we can add
  5131. 4:18:05some values so we'll type enter and then
  5132. 4:18:10Name colon and then a value like the
  5133. 4:18:14string
  5134. 4:18:16socks and at the end we'll type a
  5135. 4:18:20comma and then price colon and another
  5136. 4:18:25value like the number
  5137. 4:18:291,90 so inside our object we just added
  5138. 4:18:32two values so at the bottom here let's
  5139. 4:18:36try console. logging this object
  5140. 4:18:39console.log
  5141. 4:18:44product and
  5142. 4:18:46save and it will display both values
  5143. 4:18:50into the
  5144. 4:18:51console so as you can see an object lets
  5145. 4:18:55us group multiple values together so for
  5146. 4:18:58each of these values we have some code
  5147. 4:19:01on the left this code is called the
  5148. 4:19:04property and using the property we can
  5149. 4:19:06access specific values in the object for
  5150. 4:19:10example let's type and enter and
  5151. 4:19:16console.log we're going to type the
  5152. 4:19:19object product and then a DOT and then a
  5153. 4:19:23property like
  5154. 4:19:26name so dot name Will access the value
  5155. 4:19:30that is associated with the name
  5156. 4:19:33property which is the string socks so if
  5157. 4:19:36we
  5158. 4:19:37save it will display the string socks in
  5159. 4:19:41the console
  5160. 4:19:42let's do another example and we'll
  5161. 4:19:44access this second value using the
  5162. 4:19:47property price so we'll type enter
  5163. 4:19:52console. log the object product and then
  5164. 4:19:58a DOT and then a property
  5165. 4:20:02price so this will access the value
  5166. 4:20:05associated with price which is
  5167. 4:20:091,090 if we save
  5168. 4:20:12that's what gets displayed in the
  5169. 4:20:15console now we can also use the property
  5170. 4:20:18to change a value inside an object to do
  5171. 4:20:22that we're going to type the object's
  5172. 4:20:25name product and then Dot and a property
  5173. 4:20:30like name and just make it equal to
  5174. 4:20:34something else like the string cotton
  5175. 4:20:40socks so this code will change change
  5176. 4:20:42the value associated with the name
  5177. 4:20:45property to cotton socks so at the
  5178. 4:20:49bottom if we type
  5179. 4:20:53console.log the product
  5180. 4:20:56again and
  5181. 4:20:58save you'll notice the value associated
  5182. 4:21:01with the name property was changed to
  5183. 4:21:03cotton
  5184. 4:21:04socks so that's how an object works we
  5185. 4:21:08group multiple values together and then
  5186. 4:21:10we can access or change these values
  5187. 4:21:13using the properties on the left now
  5188. 4:21:16let's learn the syntax rules for objects
  5189. 4:21:20to create an object we start with an
  5190. 4:21:22open curly bracket and end with a closed
  5191. 4:21:25curly bracket inside the object we can
  5192. 4:21:28put multiple
  5193. 4:21:30values for each value the code on the
  5194. 4:21:33left is called the property this is how
  5195. 4:21:36we access the value inside the object we
  5196. 4:21:40separate the property and value with a
  5197. 4:21:43colon and this is called a property
  5198. 4:21:46value pair we can have many property
  5199. 4:21:49value pairs in an object and we separate
  5200. 4:21:52them with a
  5201. 4:21:54comma to access a value inside the
  5202. 4:21:57object we type the object's name and
  5203. 4:22:00then Dot and then the property we want
  5204. 4:22:03to access like name this will give us
  5205. 4:22:06the value associated with the name
  5206. 4:22:10property this syntax is called do
  5207. 4:22:13notation if we access a property that
  5208. 4:22:16doesn't exist the value will be
  5209. 4:22:19undefined to change a value in an object
  5210. 4:22:22we can use the dot notation again so
  5211. 4:22:26product. name and just make it equal to
  5212. 4:22:29something else now using the syntax we
  5213. 4:22:32can also add a value to an object we can
  5214. 4:22:36type a property that doesn't exist for
  5215. 4:22:39example here we can type
  5216. 4:22:43product do new
  5217. 4:22:47property and make this equal to
  5218. 4:22:49something like the Boolean value
  5219. 4:22:53true if we
  5220. 4:22:56console.log the product
  5221. 4:22:59again and
  5222. 4:23:02save it will add this value to the
  5223. 4:23:04object along with the new
  5224. 4:23:07property we can also remove a value from
  5225. 4:23:10an object by typing
  5226. 4:23:14delete and then the object
  5227. 4:23:17product Dot and then the property we
  5228. 4:23:20want to delete like new
  5229. 4:23:25property if we
  5230. 4:23:29console.log the product
  5231. 4:23:32again and
  5232. 4:23:35save this code will delete new property
  5233. 4:23:38from the
  5234. 4:23:39object and lastly notice that then an
  5235. 4:23:42object is just another type of value we
  5236. 4:23:45can save an object inside a variable and
  5237. 4:23:49we can also console.log objects if we
  5238. 4:23:53check the type of this value so type of
  5239. 4:23:58product and
  5240. 4:24:00save and scroll up in the
  5241. 4:24:02console it will tell us that product is
  5242. 4:24:06an object so let's remove
  5243. 4:24:09this and Save
  5244. 4:24:13and now we'll learn why we use objects
  5245. 4:24:16so objects make our code more
  5246. 4:24:19organized instead of creating a bunch of
  5247. 4:24:21variables for the products values like
  5248. 4:24:24the name and the price and other details
  5249. 4:24:28we can just group all these related
  5250. 4:24:30values together into one object objects
  5251. 4:24:34also allow us to use multiple values
  5252. 4:24:37together instead of console. logging
  5253. 4:24:40each of these values individually we can
  5254. 4:24:42just console log the whole object and
  5255. 4:24:45display all the values at
  5256. 4:24:48once so objects let us group multiple
  5257. 4:24:51values together and let us use multiple
  5258. 4:24:54values
  5259. 4:24:56together next let's do a practical
  5260. 4:24:59example with objects we're going to add
  5261. 4:25:01a score to our rock paper scissors game
  5262. 4:25:05so I've actually prepared a project we
  5263. 4:25:07can use for this lesson to see this
  5264. 4:25:09project we're going to go to our website
  5265. 4:25:12and then create a new
  5266. 4:25:14tab and then up here we're going to type
  5267. 4:25:19super simple dodev SL projects
  5268. 4:25:26slobs and press
  5269. 4:25:29enter so this is our simplified rock
  5270. 4:25:32paper scissors game again except in this
  5271. 4:25:34project when we play the
  5272. 4:25:36game it's also going to keep track of a
  5273. 4:25:39score so how many times we win lose and
  5274. 4:25:43tie and also if we click
  5275. 4:25:47okay we have a reset score button so if
  5276. 4:25:51we play the game a few
  5277. 4:25:54times and add to our
  5278. 4:25:57score and then click the reset score
  5279. 4:26:00button this will reset the score back to
  5280. 4:26:03zero so if we play the game again the
  5281. 4:26:06score starts from zero
  5282. 4:26:08again so we're going to learn how to use
  5283. 4:26:11objects to create create this feature
  5284. 4:26:14let's press
  5285. 4:26:15okay and first let's create a copy of
  5286. 4:26:19our project just for this lesson we'll
  5287. 4:26:22go to our code editor and click this
  5288. 4:26:25icon to show our files and we'll make a
  5289. 4:26:28copy of the rock paper scissors project
  5290. 4:26:30from the previous lesson so we'll
  5291. 4:26:33rightclick and
  5292. 4:26:35copy and down here right click and
  5293. 4:26:40paste and we we right click and rename
  5294. 4:26:45this
  5295. 4:26:45file we'll remove the copy at the end
  5296. 4:26:49and at the front we'll change this to 08
  5297. 4:26:54and press
  5298. 4:26:55enter now let's hide these files and we
  5299. 4:26:59can close the 07 file for now and let's
  5300. 4:27:03open this in live server by right
  5301. 4:27:06clicking and open with live
  5302. 4:27:10server let's also open the console on
  5303. 4:27:13this page so
  5304. 4:27:15rightclick
  5305. 4:27:16inspect and the
  5306. 4:27:19console all right now let's go into our
  5307. 4:27:22code and add a score to our game so
  5308. 4:27:26remember when writing JavaScript a good
  5309. 4:27:28strategy is to think about what steps we
  5310. 4:27:31need to do this is called an algorithm
  5311. 4:27:35and then convert those steps into code
  5312. 4:27:38so we already had an algorithm for the
  5313. 4:27:41rock paper scissors game
  5314. 4:27:43let's update this algorithm to include
  5315. 4:27:45the score so first the computer randomly
  5316. 4:27:49selects a move this step will stay the
  5317. 4:27:52same number two we're going to compare
  5318. 4:27:54the moves to get the result this step
  5319. 4:27:57will also be the same but now we're
  5320. 4:28:00going to add an extra step after getting
  5321. 4:28:03the result we're going to update a score
  5322. 4:28:06and finally we'll display the result and
  5323. 4:28:09the score in a popup now let's convert
  5324. 4:28:13these updated steps or this algorithm
  5325. 4:28:15into code the first thing we need to
  5326. 4:28:17change is we added a third step we're
  5327. 4:28:21going to save and update a score so
  5328. 4:28:24first we need somewhere to save our
  5329. 4:28:26score so one place we can save it is
  5330. 4:28:29inside a variable let's go to our script
  5331. 4:28:34element and at the top we're going to
  5332. 4:28:37create a variable for our score using
  5333. 4:28:41const
  5334. 4:28:42score and make it equal to an
  5335. 4:28:46object and inside this object we're
  5336. 4:28:50going to have the property
  5337. 4:28:53wins so this will keep track of how many
  5338. 4:28:55wins that we have we'll type colon and
  5339. 4:28:59the number zero because the score starts
  5340. 4:29:02at zero and then a comma and then
  5341. 4:29:06another property
  5342. 4:29:08losses this will be how many losses we
  5343. 4:29:11have and then colon and zero and then
  5344. 4:29:15another comma and ties colon and zero so
  5345. 4:29:21we use an object here because these
  5346. 4:29:23values are related to each other you
  5347. 4:29:26could use separate variables but we're
  5348. 4:29:28using an object for
  5349. 4:29:30convenience another thing you'll notice
  5350. 4:29:33is that we're creating this variable
  5351. 4:29:35outside of a function and outside of a
  5352. 4:29:39scope and that's because every time we
  5353. 4:29:42make a move we need to update the score
  5354. 4:29:45from last time so in order to save the
  5355. 4:29:48score from last time we need to keep it
  5356. 4:29:51outside the function if the score was
  5357. 4:29:53inside the function the function would
  5358. 4:29:56create a new score every time which is
  5359. 4:29:59not what we want so in this situation in
  5360. 4:30:02order to save a score from last time
  5361. 4:30:04we're going to keep this variable
  5362. 4:30:07outside now every time we play the game
  5363. 4:30:11after we calc calate the result we're
  5364. 4:30:13going to update the score so let's
  5365. 4:30:15scroll down to where we calculate the
  5366. 4:30:18result which is here and then below it
  5367. 4:30:22we're going to type some new
  5368. 4:30:24lines and type the code for updating the
  5369. 4:30:27score so we can do that with an if
  5370. 4:30:29statement so we'll type if brackets and
  5371. 4:30:34curly brackets and inside the condition
  5372. 4:30:37we're going to check if the result is a
  5373. 4:30:39win if it is a win
  5374. 4:30:42we're going to update the winds property
  5375. 4:30:44and increase it by one so down
  5376. 4:30:48here if the
  5377. 4:30:51result is equal to the string you
  5378. 4:30:57win and make sure that this string
  5379. 4:31:00matches the string up here inside the
  5380. 4:31:03curly brackets we're going to update
  5381. 4:31:07score. wins and we'll make it equal to
  5382. 4:31:13itself plus
  5383. 4:31:16one so this is how we increase the winds
  5384. 4:31:19property in the score object by one now
  5385. 4:31:23remember from the variables lesson that
  5386. 4:31:25we have a shortcut for increasing
  5387. 4:31:27something by one and that shortcut
  5388. 4:31:31is plus =
  5389. 4:31:351 next let's add another Branch to this
  5390. 4:31:39if statement for the other two results
  5391. 4:31:42so else if brackets and curly brackets
  5392. 4:31:48and in the second condition we're going
  5393. 4:31:50to check if result is equal to the
  5394. 4:31:55string you
  5395. 4:31:57lose and make sure this string matches
  5396. 4:32:00the string up here so if the result is
  5397. 4:32:04you lose we're going to update score do
  5398. 4:32:09losses property and increase it by 1 so
  5399. 4:32:13plus equal
  5400. 4:32:151 and finally we'll check if the result
  5401. 4:32:19is a tie else if brackets and curly
  5402. 4:32:25brackets
  5403. 4:32:27result equals to the string
  5404. 4:32:32tie and here we'll update
  5405. 4:32:35score. ties plus equal
  5406. 4:32:401 so we converted this step in the
  5407. 4:32:42algorithm to code the score starts at
  5408. 4:32:46zero and every time we play the game
  5409. 4:32:49after we calculate the result we're
  5410. 4:32:51going to update the score using this
  5411. 4:32:55code now let's convert the last step
  5412. 4:32:58into code we're going to display the
  5413. 4:33:00result and the score in the popup at the
  5414. 4:33:03end so let's actually go back to the
  5415. 4:33:06super simple dodev
  5416. 4:33:08project and we'll see what the popup
  5417. 4:33:10looks like we'll click one of these
  5418. 4:33:13buttons and in this popup on the second
  5419. 4:33:16line we display the score so in our
  5420. 4:33:20code this is the code that displays that
  5421. 4:33:23popup so how do we add a second line to
  5422. 4:33:27the popup remember that template strings
  5423. 4:33:30have another special feature called
  5424. 4:33:32multi-line strings so we can type at the
  5425. 4:33:35end of this template string and press
  5426. 4:33:38enter to add another line to this Str
  5427. 4:33:42so here we can type winds colon and
  5428. 4:33:47we're going to insert a value using
  5429. 4:33:50Dollar open curly bracket Clos curly
  5430. 4:33:53bracket and here we're going to insert
  5431. 4:33:56the number of wins that we have which is
  5432. 4:33:59score. wins so we'll type score
  5433. 4:34:05dowins next we're going to show the
  5434. 4:34:08number of losses that we have so after
  5435. 4:34:11this
  5436. 4:34:12let's type a comma and losses colon and
  5437. 4:34:17insert another
  5438. 4:34:19value here we'll insert
  5439. 4:34:22score.
  5440. 4:34:24losses and finally we'll type a comma
  5441. 4:34:28and we'll display ties colon and
  5442. 4:34:32inserted value score dot
  5443. 4:34:37ties and this will display our score in
  5444. 4:34:40the popup so let's save this and give it
  5445. 4:34:43a try we'll click okay and then go back
  5446. 4:34:47to our Tab and play the
  5447. 4:34:51game so now on the second line we will
  5448. 4:34:54display the score one small issue here
  5449. 4:34:57is that we have a lot of spaces on the
  5450. 4:34:59second line so these spaces correspond
  5451. 4:35:02to the spaces here when we use
  5452. 4:35:05multi-line strings these Extra Spaces
  5453. 4:35:08will show up in the popup so to remove
  5454. 4:35:11these SP cases we can just remove them
  5455. 4:35:13in the code like this now if we
  5456. 4:35:17save press
  5457. 4:35:19okay and play the game it will show our
  5458. 4:35:23score on the second line without those
  5459. 4:35:25Extra Spaces at the front let's press
  5460. 4:35:29okay we're going to add one more feature
  5461. 4:35:32to this project if we go back to Super
  5462. 4:35:35simple. Dev we have a reset score button
  5463. 4:35:39which will reset the score back to zero
  5464. 4:35:41when we click it let's add this button
  5465. 4:35:44in our project so we'll scroll
  5466. 4:35:47up and inside the
  5467. 4:35:50HTML We'll add a button with the code
  5468. 4:35:54less than button greater than and inside
  5469. 4:35:58the button we'll have the text reset
  5470. 4:36:02score and now when we click this button
  5471. 4:36:06we'll run some code using the onclick
  5472. 4:36:09attribute equals double quotes and then
  5473. 4:36:13in here we're going to write some code
  5474. 4:36:16to reset this score back to
  5475. 4:36:19zero so one way we can do it is because
  5476. 4:36:22this score is an object we can use dot
  5477. 4:36:25notation to change everything back to
  5478. 4:36:27zero so we can type
  5479. 4:36:31score. wins and make it equal to zero
  5480. 4:36:36we'll also type
  5481. 4:36:38score. losses and make it equal to Z and
  5482. 4:36:43finally
  5483. 4:36:45score. ties and make it equal to
  5484. 4:36:49zero now let's
  5485. 4:36:51save and go back to our
  5486. 4:36:54Tab and play the game a few times to get
  5487. 4:36:58a
  5488. 4:37:01score let's press okay and press reset
  5489. 4:37:06score and then play the game again and
  5490. 4:37:10you'll notice that the score started
  5491. 4:37:12from zero again so that's how we create
  5492. 4:37:15the reset score button we just set all
  5493. 4:37:18of these properties back to
  5494. 4:37:20zero and that's it that's how we add a
  5495. 4:37:23score feature to our rock paper scissors
  5496. 4:37:26project using an object to save the
  5497. 4:37:30score let's press
  5498. 4:37:32okay and we're going to learn more
  5499. 4:37:34details about objects let's go back to
  5500. 4:37:38objects.
  5501. 4:37:40HTML and what open the tab for this
  5502. 4:37:43file and let's also comment out the
  5503. 4:37:46previous code using
  5504. 4:37:49slashstar and at the
  5505. 4:37:51bottom star
  5506. 4:37:53slash and
  5507. 4:37:55save now let's scroll to the bottom and
  5508. 4:37:58we'll do some
  5509. 4:38:00setup so first let's create a variable
  5510. 4:38:03with const we'll name it Product 2 and
  5511. 4:38:08make it equal to another object
  5512. 4:38:12inside this object we have the property
  5513. 4:38:15Name colon and the string
  5514. 4:38:19shirt and we're going to console.log
  5515. 4:38:22this object so
  5516. 4:38:26console.log Product 2 and
  5517. 4:38:31save so the next detail we're going to
  5518. 4:38:34learn about objects is another way to
  5519. 4:38:37access the values
  5520. 4:38:38inside we previously learned the
  5521. 4:38:41notation to do this for example
  5522. 4:38:47console.log product
  5523. 4:38:50to.
  5524. 4:38:53name this will access the value
  5525. 4:38:56associated with the name
  5526. 4:38:58property in JavaScript we have another
  5527. 4:39:01way of accessing values inside objects
  5528. 4:39:04which is using square brackets so we'll
  5529. 4:39:07create a new line and type console
  5530. 4:39:11do
  5531. 4:39:13log product 2 and this time we'll type
  5532. 4:39:18square
  5533. 4:39:19brackets and then inside the square
  5534. 4:39:21brackets we'll have a string with
  5535. 4:39:25name so this is called bracket notation
  5536. 4:39:30and this does the same thing as dot
  5537. 4:39:32notation up here it gets the value
  5538. 4:39:35associated with the name property if we
  5539. 4:39:40save these two lines of code both access
  5540. 4:39:43the string shirt which we see here so
  5541. 4:39:47when do we use the bracket notation
  5542. 4:39:49instead of the dot notation bracket
  5543. 4:39:52notation lets us use properties that
  5544. 4:39:55normally don't work with do notation for
  5545. 4:39:59example at the bottom we can type
  5546. 4:40:05console.log Product 2 and then use a DOT
  5547. 4:40:10and try to access a property called
  5548. 4:40:16delivery-time if we
  5549. 4:40:19save this property will give us an error
  5550. 4:40:23and that's because JavaScript thinks
  5551. 4:40:25that this Dash is a minus sign and it's
  5552. 4:40:27trying to do math so in order to use a
  5553. 4:40:31property like this we have to use
  5554. 4:40:33bracket notation so we're going to
  5555. 4:40:36replace the dot with square
  5556. 4:40:40brackets and and turn this into a
  5557. 4:40:44string now if we
  5558. 4:40:46save it no longer gives us an error and
  5559. 4:40:50because delivery time does not exist in
  5560. 4:40:53the object we get the value
  5561. 4:40:56undefined so using bracket notation we
  5562. 4:40:59can also add values to the object when
  5563. 4:41:02we create it so here we can add a
  5564. 4:41:06comma and we'll add another property
  5565. 4:41:09using bracket notation and a string
  5566. 4:41:16delivery-time and a colon and then a
  5567. 4:41:19value like the string one
  5568. 4:41:22day if we
  5569. 4:41:24save this lets us add a delivery-time
  5570. 4:41:28property to this object so as you can
  5571. 4:41:32see bracket notation lets us use
  5572. 4:41:35properties that normally don't work with
  5573. 4:41:37DOT
  5574. 4:41:38notation another feature of bracket
  5575. 4:41:41notation is that between the brackets we
  5576. 4:41:44don't just have to use a string we can
  5577. 4:41:47use a variable a calculation or anything
  5578. 4:41:50that results in a
  5579. 4:41:52value so you might be wondering which
  5580. 4:41:54one should we use do notation or bracket
  5581. 4:41:58notation we usually use do notation by
  5582. 4:42:02default because it's shorter and easier
  5583. 4:42:04to read but if we need to use properties
  5584. 4:42:07that don't work with do notation then we
  5585. 4:42:10use bracket not
  5586. 4:42:11notation now a shortcut here is when we
  5587. 4:42:15create the object if we only have a
  5588. 4:42:17string between the brackets the brackets
  5589. 4:42:20are actually optional so we can just
  5590. 4:42:23type this if we save the code will still
  5591. 4:42:28work okay the next detail we're going to
  5592. 4:42:31learn is that inside an object we can
  5593. 4:42:34save any type of value so this includes
  5594. 4:42:38numbers strings booleans
  5595. 4:42:41as well as other objects because
  5596. 4:42:44remember objects themselves are also
  5597. 4:42:47values for example we can type a comma
  5598. 4:42:51and add a property called
  5599. 4:42:54rating colon and then an object open
  5600. 4:42:58curly bracket close curly bracket and
  5601. 4:43:02inside this inner object we can add more
  5602. 4:43:05properties like stars colon 4.5
  5603. 4:43:12this is how many stars a product has and
  5604. 4:43:16then a comma and a property called count
  5605. 4:43:20colon
  5606. 4:43:2287 this is how many ratings a product
  5607. 4:43:25has so this is called a nested object or
  5608. 4:43:30an object inside an object we can use
  5609. 4:43:33nested objects to make our values more
  5610. 4:43:36organized so inside the product the
  5611. 4:43:39number of stars that has and the number
  5612. 4:43:42of ratings that it has are both related
  5613. 4:43:45to a rating so it makes sense to group
  5614. 4:43:48them together further inside the
  5615. 4:43:51product now to access values inside the
  5616. 4:43:55inner object we just use the dot
  5617. 4:43:57notation or bracket notation multiple
  5618. 4:44:00times for example at the bottom we can
  5619. 4:44:04type
  5620. 4:44:09console.log product
  5621. 4:44:112 dot
  5622. 4:44:14rating this will get the value
  5623. 4:44:17associated with rating and this is an
  5624. 4:44:21object so we can use the dot notation
  5625. 4:44:24again
  5626. 4:44:26count so this will get the count inside
  5627. 4:44:29the rating object which is this value
  5628. 4:44:33right here so if we save it will display
  5629. 4:44:3887 and finally another type Ty of value
  5630. 4:44:41we can put in an object is a function so
  5631. 4:44:45in this object let's type comma and
  5632. 4:44:49another property
  5633. 4:44:51fun colon and we're going to create a
  5634. 4:44:54function here so let's type
  5635. 4:44:57function and we'll name the function
  5636. 4:45:01Function One brackets and curly brackets
  5637. 4:45:05and then inside the function we're going
  5638. 4:45:07to have some JavaScript code like usual
  5639. 4:45:11so
  5640. 4:45:12console. log the string
  5641. 4:45:17function inside
  5642. 4:45:21object this saves a function inside the
  5643. 4:45:25object and this works because a function
  5644. 4:45:28is actually just another type of value
  5645. 4:45:31in JavaScript but we'll learn more about
  5646. 4:45:34this feature of functions later in this
  5647. 4:45:36course for now just know that functions
  5648. 4:45:40are also values and we can save them in
  5649. 4:45:43an object to access this function we can
  5650. 4:45:46use the dot or bracket notation as usual
  5651. 4:45:50so at the
  5652. 4:45:51bottom we can type the object Product 2
  5653. 4:45:58and then a DOT and then the property
  5654. 4:46:01fun so this will access the value
  5655. 4:46:04associated with the fun property which
  5656. 4:46:07is a function and because this is a fun
  5657. 4:46:10function we can call it as usual using
  5658. 4:46:15brackets if we
  5659. 4:46:17save this will run the function here and
  5660. 4:46:22it will display function inside object
  5661. 4:46:25into the
  5662. 4:46:27console so when we save functions inside
  5663. 4:46:30an object this is called a method and
  5664. 4:46:34we've actually been using methods this
  5665. 4:46:36whole time
  5666. 4:46:38console.log is actually a method
  5667. 4:46:41console is an object provided by
  5668. 4:46:44JavaScript and log is a function that is
  5669. 4:46:48saved inside the console
  5670. 4:46:50object if we go to the bottom and type
  5671. 4:46:56console.log type of
  5672. 4:47:01console and
  5673. 4:47:04save this will confirm that console is
  5674. 4:47:07just an object and if we change this to
  5675. 4:47:12console.log and
  5676. 4:47:15save this will show us that console.log
  5677. 4:47:18is a function so log is a function that
  5678. 4:47:22is saved inside the console object so we
  5679. 4:47:26call this a method another example of a
  5680. 4:47:29method is math.random which we used in
  5681. 4:47:33the rock paper scissors project math is
  5682. 4:47:36also an object provided by JavaScript
  5683. 4:47:40and random is a function saved inside
  5684. 4:47:43the math object so we call math.random a
  5685. 4:47:47method as well so we've actually been
  5686. 4:47:50using objects and methods all throughout
  5687. 4:47:53this course and now we understand all
  5688. 4:47:55the code that we've been using so far
  5689. 4:47:59next we're going to learn about builtin
  5690. 4:48:01objects so the console object and the
  5691. 4:48:04math object are known as builtin objects
  5692. 4:48:08because they are built in to the
  5693. 4:48:09language they provided by the
  5694. 4:48:12language we're going to learn two more
  5695. 4:48:15built-in objects called Json and local
  5696. 4:48:18storage and we will use these objects to
  5697. 4:48:21improve our rock paper scissors project
  5698. 4:48:25let's start with the Json built-in
  5699. 4:48:28object this object helps us work with
  5700. 4:48:30something called
  5701. 4:48:32Json Json stands for JavaScript object
  5702. 4:48:36notation the easiest way to understand
  5703. 4:48:38Jason is it's BAS basically a syntax
  5704. 4:48:42it's a similar syntax to a JavaScript
  5705. 4:48:45object but it has less features for
  5706. 4:48:49example the code on the left uses
  5707. 4:48:51JavaScript object syntax and the code on
  5708. 4:48:54the right uses Json syntax so one
  5709. 4:48:58difference is that all properties and
  5710. 4:49:00strings must use double quotes in Json
  5711. 4:49:04Jason does not support single quotes
  5712. 4:49:07another difference is that Json does not
  5713. 4:49:09support function functions so as you can
  5714. 4:49:12see Json is basically a syntax that's
  5715. 4:49:16similar to a JavaScript object syntax
  5716. 4:49:19but with less
  5717. 4:49:20features so why would we use Json syntax
  5718. 4:49:24instead of a JavaScript object
  5719. 4:49:26syntax a JavaScript object only makes
  5720. 4:49:29sense in
  5721. 4:49:31JavaScript on the other hand Json syntax
  5722. 4:49:34can be understood by almost every
  5723. 4:49:36programming language so Json syntax is
  5724. 4:49:40more un
  5725. 4:49:41Universal for this reason we use Json
  5726. 4:49:44when we send data between two computers
  5727. 4:49:47that might use different programming
  5728. 4:49:49languages we'll learn how to do this
  5729. 4:49:51later in the course we also use Json
  5730. 4:49:55when we store data we'll learn how to
  5731. 4:49:57store data in this
  5732. 4:49:59lesson now that we understand what Json
  5733. 4:50:02is let's learn about the built in Json
  5734. 4:50:06object the Json object helps us convert
  5735. 4:50:09a JavaScript object to Json for example
  5736. 4:50:14let's convert our object product two to
  5737. 4:50:17Json so we'll create some new
  5738. 4:50:20lines and we'll type the Json object do
  5739. 4:50:26stringify and Open Bracket close bracket
  5740. 4:50:30so stringify is a method of the Json
  5741. 4:50:33object so it's a function that is saved
  5742. 4:50:36inside this object and we can call this
  5743. 4:50:39function using
  5744. 4:50:41brackets between the brackets we can
  5745. 4:50:43give it a JavaScript object we want to
  5746. 4:50:46convert like Product
  5747. 4:50:502 this will convert the object into Json
  5748. 4:50:54so now let's
  5749. 4:50:56console. log the
  5750. 4:51:01result and
  5751. 4:51:04save and in the console it will display
  5752. 4:51:07the product using Json syntax if we we
  5753. 4:51:10scroll up you'll notice that the
  5754. 4:51:13function that we saved in the object is
  5755. 4:51:15not inside the Json and that's because
  5756. 4:51:18Json does not support
  5757. 4:51:20functions so one thing to note about
  5758. 4:51:23stringify is that it actually results in
  5759. 4:51:26a string so at the front if we type type
  5760. 4:51:31of and
  5761. 4:51:33save it will tell us that the result
  5762. 4:51:36that it gives is actually a
  5763. 4:51:39string so if we remove
  5764. 4:51:41this and
  5765. 4:51:44save now this Json string is ready to be
  5766. 4:51:47sent to another computer or to be saved
  5767. 4:51:50in
  5768. 4:51:51storage next let's learn how to convert
  5769. 4:51:54the other way from a Json back to a
  5770. 4:51:57JavaScript object to do that we'll use
  5771. 4:52:01another method in the built-in Json
  5772. 4:52:03object called json.parse
  5773. 4:52:07at the end we'll type Jon
  5774. 4:52:11dop
  5775. 4:52:13pars and
  5776. 4:52:15brackets and between the brackets we can
  5777. 4:52:18put a Json string that we want to
  5778. 4:52:20convert so we actually have a Json
  5779. 4:52:22string up here so let's just make a copy
  5780. 4:52:26of this we'll select it
  5781. 4:52:29rightclick
  5782. 4:52:31copy and let's create an extra variable
  5783. 4:52:34to make it more clear const Json string
  5784. 4:52:40and make get equal to right click and
  5785. 4:52:46paste and now let's convert this Json
  5786. 4:52:49string back into an object by putting it
  5787. 4:52:52into
  5788. 4:52:54pars and finally we console.log this
  5789. 4:52:58result so
  5790. 4:53:05console.log and
  5791. 4:53:07save and json. pars will turn the Json
  5792. 4:53:11string back into a JavaScript object so
  5793. 4:53:15that's the built-in Json object it helps
  5794. 4:53:18us convert back and forth between a
  5795. 4:53:21JavaScript object and
  5796. 4:53:24Json now we're going to learn the next
  5797. 4:53:27built-in object called local storage
  5798. 4:53:30local storage is used to save values
  5799. 4:53:33more permanently so far we've been
  5800. 4:53:36saving our values in
  5801. 4:53:38variables however variables are
  5802. 4:53:41temporary they only exist on the current
  5803. 4:53:44page if we refresh the page or close the
  5804. 4:53:47page all the variables are deleted for
  5805. 4:53:51example if we go back to our rock paper
  5806. 4:53:54scissors
  5807. 4:53:55project and open the tab for this
  5808. 4:53:58project and we play a few
  5809. 4:54:01games and play it again so we have two
  5810. 4:54:06games if we refresh the page
  5811. 4:54:10notice the score starts from zero again
  5812. 4:54:14and that's because when we refresh the
  5813. 4:54:15page all of these variables are deleted
  5814. 4:54:18and the score gets reset back to zero
  5815. 4:54:22however if we press okay and go to
  5816. 4:54:25superimple
  5817. 4:54:26dodev and we refresh the page and we
  5818. 4:54:30play the
  5819. 4:54:31game notice that the score doesn't start
  5820. 4:54:34from zero it starts from the previous
  5821. 4:54:36score even though we refreshed the page
  5822. 4:54:40and that's because this project saves
  5823. 4:54:42the score in local storage and local
  5824. 4:54:45storage doesn't get deleted when we
  5825. 4:54:47refresh the
  5826. 4:54:49page now let's learn how to use local
  5827. 4:54:52storage in our project let's press okay
  5828. 4:54:56and we'll go back to our code and we'll
  5829. 4:54:59scroll down to where we update the
  5830. 4:55:02score so here after we update the score
  5831. 4:55:06we're going to save it in local storage
  5832. 4:55:09but first first let's learn how local
  5833. 4:55:11storage works so at the bottom here
  5834. 4:55:15we're going to type the local capital S
  5835. 4:55:19Storage
  5836. 4:55:21object to save a value inside local
  5837. 4:55:24storage it has a method called set item
  5838. 4:55:28so we can type dot set capital I item
  5839. 4:55:34and brackets because this is a method or
  5840. 4:55:38a function saved inside the object
  5841. 4:55:40object and we're going to give two
  5842. 4:55:43strings to this method the first string
  5843. 4:55:46is a name this is how we're going to
  5844. 4:55:49access the value that we save later so
  5845. 4:55:52let's use the string
  5846. 4:55:55message as the name and we'll type a
  5847. 4:55:59comma and then we'll type the value that
  5848. 4:56:02we want to save into local storage so
  5849. 4:56:05local storage only supports strings so
  5850. 4:56:08let's save the string
  5851. 4:56:11hello inside local
  5852. 4:56:13storage if we
  5853. 4:56:15save and then go back to our Tab and
  5854. 4:56:19play the
  5855. 4:56:20game it will run all this code and save
  5856. 4:56:23this string into local storage now let's
  5857. 4:56:26learn how to get this string out of
  5858. 4:56:28local storage we'll press okay and we'll
  5859. 4:56:32scroll to the
  5860. 4:56:35top and when we first load the page
  5861. 4:56:38we're going to get that value out of
  5862. 4:56:40local local storage by typing local
  5863. 4:56:44capital S
  5864. 4:56:46storage. getet item this
  5865. 4:56:49time and
  5866. 4:56:51brackets so the get item method gets a
  5867. 4:56:55value out of local storage and we're
  5868. 4:56:57going to give it one string so this
  5869. 4:57:00string is the name that we used earlier
  5870. 4:57:03which was message so we'll type the
  5871. 4:57:05string
  5872. 4:57:07message now let's console log this
  5873. 4:57:11result so
  5874. 4:57:16console.log and a bracket at the
  5875. 4:57:19end and
  5876. 4:57:21save it will get the value that we saved
  5877. 4:57:24earlier out of local storage so we saved
  5878. 4:57:28the string hello and that's exactly what
  5879. 4:57:31gets displayed in the
  5880. 4:57:33console so using local storage we were
  5881. 4:57:36able to get the value that we saved
  5882. 4:57:38earlier even though we refresh the
  5883. 4:57:41page now let's save our score into local
  5884. 4:57:44storage so it doesn't get reset when we
  5885. 4:57:47refresh the page so let's scroll to the
  5886. 4:57:52bottom and instead of using the name
  5887. 4:57:55message we're going to use the name
  5888. 4:57:57score to save our score and instead of
  5889. 4:58:01the string
  5890. 4:58:02hello we're going to save the score
  5891. 4:58:06object now one problem here is that
  5892. 4:58:09local storage only supports strings so
  5893. 4:58:12we need to convert our object into a
  5894. 4:58:15string and we actually learned how to do
  5895. 4:58:17this earlier using json.stringify
  5896. 4:58:20so at the front we can type
  5897. 4:58:24json.
  5898. 4:58:27stringify and then brackets around the
  5899. 4:58:30score and this will convert the
  5900. 4:58:33JavaScript object score into a Json
  5901. 4:58:36string and then it's ready to be saved
  5902. 4:58:39in local store
  5903. 4:58:41so if we save this and then play the
  5904. 4:58:45game it will run this code and save our
  5905. 4:58:48score into local
  5906. 4:58:50storage now let's press
  5907. 4:58:53okay and then's scroll up to the
  5908. 4:58:57top and now instead of getting the
  5909. 4:58:59message we're going to get the score out
  5910. 4:59:02of local storage if we
  5911. 4:59:05save now when we load the page we're
  5912. 4:59:08able to get the score that we saved from
  5913. 4:59:11last time as a Json
  5914. 4:59:13string the last step is instead of
  5915. 4:59:16resetting the score to zero every time
  5916. 4:59:19we're just going to use the score from
  5917. 4:59:21local storage before we do that remember
  5918. 4:59:24that this is just a string so we need to
  5919. 4:59:28convert this back to an
  5920. 4:59:30object we also learned how to do this
  5921. 4:59:33earlier using json. pars so at the
  5922. 4:59:38front we can type
  5923. 4:59:41json. pars and then Open Bracket and at
  5924. 4:59:45the end a close bracket and it will
  5925. 4:59:49convert this back to an object if we
  5926. 4:59:53save it will convert the score from a
  5927. 4:59:55Json string back to an object so now we
  5928. 4:59:59can use it up here so instead of doing
  5929. 5:00:02this we'll remove it and replace it with
  5930. 5:00:06this code so right click cut
  5931. 5:00:11and then here right click and
  5932. 5:00:16paste and we'll remove
  5933. 5:00:21console.log and
  5934. 5:00:23save so now when we load the page it
  5935. 5:00:26will load the score from local storage
  5936. 5:00:30if we play the
  5937. 5:00:31game notice that the score doesn't start
  5938. 5:00:34from zero it starts from the score that
  5939. 5:00:36we had last time let's review what we
  5940. 5:00:40did step by step first when we update
  5941. 5:00:44our score in this code after we update
  5942. 5:00:48the score we're going to save it in
  5943. 5:00:50local storage using local storage. set
  5944. 5:00:54item now local storage only supports
  5945. 5:00:57strings so we have to convert our score
  5946. 5:01:00object into a Json string using json.
  5947. 5:01:05stringify and then after we've saved
  5948. 5:01:07this into local storage when we load the
  5949. 5:01:11page up here we're going to load the
  5950. 5:01:14score that we just saved from local
  5951. 5:01:17storage using local storage. getet item
  5952. 5:01:21and the score was saved as a Json string
  5953. 5:01:24so we need to convert it back into an
  5954. 5:01:27object using json.parse
  5955. 5:01:30so that's how we use the Json object and
  5956. 5:01:34the local storage object to save our
  5957. 5:01:36score more
  5958. 5:01:38permanently let's press okay
  5959. 5:01:40okay and the last feature we're going to
  5960. 5:01:43add is when we click the reset score
  5961. 5:01:46button we also need to remove the score
  5962. 5:01:49from local storage otherwise if we press
  5963. 5:01:52reset score and
  5964. 5:01:55refresh and then play the game it will
  5965. 5:01:58just grab the old score from local
  5966. 5:02:00storage so reset score doesn't really
  5967. 5:02:03work here let's press okay and fix this
  5968. 5:02:07issue so here in the set score button at
  5969. 5:02:11the end we're going to remove the score
  5970. 5:02:14from local storage so we'll type
  5971. 5:02:18local
  5972. 5:02:19storage and then a method called do
  5973. 5:02:24remove item and
  5974. 5:02:27brackets and between the brackets we're
  5975. 5:02:30going to tell local storage which value
  5976. 5:02:32we want to remove so we're going to
  5977. 5:02:34remove the score so we'll type the
  5978. 5:02:37string score
  5979. 5:02:40if we
  5980. 5:02:41save and we click the reset button and
  5981. 5:02:45then we refresh the page and try to play
  5982. 5:02:48the game
  5983. 5:02:49again unfortunately it now gives us an
  5984. 5:02:52error so what is happening here so
  5985. 5:02:55remember that we remov the score from
  5986. 5:02:58local storage so now when we load the
  5987. 5:03:00page this actually doesn't exist when
  5988. 5:03:04something doesn't exist in local storage
  5989. 5:03:07it's going to give us a value called
  5990. 5:03:09null
  5991. 5:03:11and at the
  5992. 5:03:12bottom if we try to access any property
  5993. 5:03:15on null it will give us an error so
  5994. 5:03:18that's what we see here so how do we
  5995. 5:03:21solve this problem if we scroll
  5996. 5:03:24up one way to solve this problem is that
  5997. 5:03:28if there is no score in local storage
  5998. 5:03:31we're going to give it a default value
  5999. 5:03:34let's look at how we can do that so
  6000. 5:03:37after this line we're going to check if
  6001. 5:03:39the score is null we can do that using
  6002. 5:03:42an if statement if brackets and curly
  6003. 5:03:47brackets and we'll check if score is
  6004. 5:03:51equal to
  6005. 5:03:54null and if it is equal to null we're
  6006. 5:03:57going to give this a default score so
  6007. 5:04:00inside the curly
  6008. 5:04:01brackets we'll just make the score equal
  6009. 5:04:06to the default object we had earlier
  6010. 5:04:10which was
  6011. 5:04:11wins zero and then
  6012. 5:04:15losses zero and
  6013. 5:04:18ties
  6014. 5:04:20zero and remember if we need to reassign
  6015. 5:04:23a variable we have to change it from
  6016. 5:04:25const to let if we
  6017. 5:04:29save the score will be null because we
  6018. 5:04:32removed it earlier and now this will
  6019. 5:04:35give it a default score so everything
  6020. 5:04:38should work as before
  6021. 5:04:40if we play the
  6022. 5:04:41game you'll notice that the score starts
  6023. 5:04:44from zero or this default score and
  6024. 5:04:47everything is working again so that's
  6025. 5:04:49how we create the score feature in the
  6026. 5:04:51rock paper scissors project as well as
  6027. 5:04:54the reset score
  6028. 5:04:56feature next we're going to use some
  6029. 5:04:59shortcuts to simplify our code so these
  6030. 5:05:02shortcuts are things that we learned in
  6031. 5:05:04the Boolean lesson so remember that null
  6032. 5:05:08is a falsy
  6033. 5:05:10value that means it will behave just
  6034. 5:05:13like false so a shortcut we can use here
  6035. 5:05:16is instead of checking if the score is
  6036. 5:05:19equal to null we can also remove this
  6037. 5:05:24and check if not
  6038. 5:05:27score so not score does the same thing
  6039. 5:05:31as checking if it's null if the score is
  6040. 5:05:34null not score will flip it into true so
  6041. 5:05:39both of these will return true if the
  6042. 5:05:42score exists it's an object then both of
  6043. 5:05:45these will return false so instead of
  6044. 5:05:49checking if a value is equal to null we
  6045. 5:05:52can just use the not operator as a
  6046. 5:05:56shortcut now also from the boolean's
  6047. 5:05:58lesson we have an even shorter shortcut
  6048. 5:06:01for doing this so notice here that this
  6049. 5:06:05code basically sets a default value if
  6050. 5:06:08the score doesn't exist in the boolean's
  6051. 5:06:11lesson we learned a technique called the
  6052. 5:06:14default operator which is exactly what
  6053. 5:06:17we need here so on this line we can
  6054. 5:06:21remove the semicolon and type
  6055. 5:06:26or and then a default value so we want
  6056. 5:06:30our default value to be this object so
  6057. 5:06:33let's select it and then
  6058. 5:06:36rightclick copy
  6059. 5:06:40and then here right click and
  6060. 5:06:45paste and I'll select these lines and
  6061. 5:06:48press shift tab to remove and indent and
  6062. 5:06:52I'll add a semicolon back at the
  6063. 5:06:55end so the way that the default operator
  6064. 5:06:58works is that if the left side is truthy
  6065. 5:07:02so in this case we have a score it will
  6066. 5:07:05use the left side if the left side is
  6067. 5:07:08falsy or in this case the score is null
  6068. 5:07:11then we will use the right side as a
  6069. 5:07:14default value so this does the same
  6070. 5:07:17thing as this code below but it's just a
  6071. 5:07:21shortcut so now let's comment out this
  6072. 5:07:24code using
  6073. 5:07:27slashstar and at the bottom start slash
  6074. 5:07:31so we'll leave it here just for
  6075. 5:07:33reference and then
  6076. 5:07:35save and if we click reset score again
  6077. 5:07:40and
  6078. 5:07:41refresh and play the
  6079. 5:07:43game it will still work so this is a
  6080. 5:07:47perfect situation to use the default
  6081. 5:07:49operator to simplify our
  6082. 5:07:52code so let's press
  6083. 5:07:55okay and that's all we're going to do
  6084. 5:07:57for this project in this
  6085. 5:08:00lesson now let's move on and learn more
  6086. 5:08:03details about objects the first thing
  6087. 5:08:06we'll learn is the value null so null is
  6088. 5:08:09one of the falsy values in JavaScript
  6089. 5:08:12it's similar to the value undefined they
  6090. 5:08:15both represent that something doesn't
  6091. 5:08:18have a value so what's the difference
  6092. 5:08:20between null and undefined we use null
  6093. 5:08:24when we intentionally want something to
  6094. 5:08:26be empty for example in the previous
  6095. 5:08:30lesson we learned that if a function has
  6096. 5:08:33parameters we can give the parameter a
  6097. 5:08:35default value if the parameter is
  6098. 5:08:38undefined
  6099. 5:08:40it will use the default value however if
  6100. 5:08:43we set this parameter to null it will
  6101. 5:08:46not use the default value and it will
  6102. 5:08:48just use null so null means we
  6103. 5:08:52intentionally want something to be
  6104. 5:08:55empty in most cases though null and
  6105. 5:08:58undefined work the same way okay so the
  6106. 5:09:02next detail about objects we're going to
  6107. 5:09:04learn is called
  6108. 5:09:06autoboxing let's go back to objects. HT
  6109. 5:09:09HML and open the tab for this
  6110. 5:09:13file and let's also comment out the
  6111. 5:09:15previous code so our console doesn't get
  6112. 5:09:17too messy so
  6113. 5:09:20slashstar and at the
  6114. 5:09:22bottom star slash and
  6115. 5:09:26save so let's scroll
  6116. 5:09:29down we learned in this lesson that
  6117. 5:09:32objects can have properties and methods
  6118. 5:09:35so you might be surprised to learn that
  6119. 5:09:37other values also have have properties
  6120. 5:09:40and methods for example at the bottom
  6121. 5:09:44we're going to type
  6122. 5:09:46console.
  6123. 5:09:48log the string
  6124. 5:09:51hello and then the dot notation and a
  6125. 5:09:55property called
  6126. 5:09:58length so strings can also have
  6127. 5:10:01properties the length property tells us
  6128. 5:10:04how many characters or letters are in
  6129. 5:10:07this string so hello has five letters so
  6130. 5:10:11if we save the length property will be
  6131. 5:10:15five here's another example we'll type
  6132. 5:10:21console.log the string hello and then
  6133. 5:10:25the dot notation and a method this time
  6134. 5:10:292 capital u upper Capital C case and
  6135. 5:10:36brackets so this string also has a
  6136. 5:10:39method called two
  6137. 5:10:42uppercase and this method gives us a
  6138. 5:10:44copy of the string with all letters
  6139. 5:10:47capitalized or in
  6140. 5:10:49uppercase if we save it will give us
  6141. 5:10:53hello in capital
  6142. 5:10:55letters so as you can see simple values
  6143. 5:10:58like strings can also have properties
  6144. 5:11:01and
  6145. 5:11:02methods so now you might be wondering
  6146. 5:11:05these are strings not objects how can
  6147. 5:11:08strings have properties and methods the
  6148. 5:11:11answer is that JavaScript has a special
  6149. 5:11:13feature called Auto boxing JavaScript
  6150. 5:11:17automatically wraps this string in a
  6151. 5:11:20special object first and this object has
  6152. 5:11:23the property length and the method to
  6153. 5:11:27uppercase so this is done automatically
  6154. 5:11:29we don't see the special object that's
  6155. 5:11:32wrapped around this string so this
  6156. 5:11:35feature is called
  6157. 5:11:36autoboxing because JavaScript automat
  6158. 5:11:39automatically wraps the string in an
  6159. 5:11:41object like a
  6160. 5:11:43box so autoboxing also works with other
  6161. 5:11:46types of values like numbers and
  6162. 5:11:49booleans the only thing is that auto
  6163. 5:11:52boxing does not work with null and
  6164. 5:11:55undefined it would just give us an
  6165. 5:11:58error The Last Detail we're going to
  6166. 5:12:00learn about objects is a really
  6167. 5:12:02important detail and that is objects are
  6168. 5:12:07references to demonstrate let's create a
  6169. 5:12:10new variable with an object so
  6170. 5:12:13const object one and make it equal to an
  6171. 5:12:19object inside this object let's have a
  6172. 5:12:23property
  6173. 5:12:24message colon and the string
  6174. 5:12:29hello so you might think that when we
  6175. 5:12:31create an object like this we create a
  6176. 5:12:34value that has other values inside and
  6177. 5:12:37we save the whole thing and a variable
  6178. 5:12:40however this actually works a little
  6179. 5:12:43differently when we create this object
  6180. 5:12:46the actual value is created somewhere in
  6181. 5:12:49the computer's memory and this variable
  6182. 5:12:52actually just gets a reference that
  6183. 5:12:54points to that location in the
  6184. 5:12:56computer's memory this is like a
  6185. 5:12:59shortcut on your computer a shortcut
  6186. 5:13:02points to where the actual file or
  6187. 5:13:04folder is so there's no way to actually
  6188. 5:13:07see this reference but just just know
  6189. 5:13:09that this is a reference and the object
  6190. 5:13:12is somewhere else in the computer's
  6191. 5:13:14memory so the reason it works this way
  6192. 5:13:17is to make the language more efficient
  6193. 5:13:19for example below this line we can
  6194. 5:13:23create a variable const and call it
  6195. 5:13:27object 2 and make it equal to object
  6196. 5:13:33one so this code doesn't actually make a
  6197. 5:13:36copy of the object it this makes a copy
  6198. 5:13:40of the reference that points to the
  6199. 5:13:42object so this makes a language more
  6200. 5:13:45efficient because if we had a really big
  6201. 5:13:48object here it's not good to copy the
  6202. 5:13:51entire object over and over again
  6203. 5:13:54instead Javas just copies the reference
  6204. 5:13:57which is much faster this is called copy
  6205. 5:14:00by
  6206. 5:14:02reference now that we learned that
  6207. 5:14:04objects are actually references let's
  6208. 5:14:06learn some interesting behaviors of
  6209. 5:14:09references so at the bottom here we're
  6210. 5:14:13going to change the property message in
  6211. 5:14:16this object we can do that by typing the
  6212. 5:14:19object object
  6213. 5:14:221. message property and make it equal to
  6214. 5:14:27something else like the string good
  6215. 5:14:32job and now if we
  6216. 5:14:35console. log object one
  6217. 5:14:40and
  6218. 5:14:41save notice that we were able to change
  6219. 5:14:44the message property even though we
  6220. 5:14:47created object one with const and that's
  6221. 5:14:50because this variable just contains a
  6222. 5:14:53reference that points to the object so
  6223. 5:14:56const prevents us from changing the
  6224. 5:14:58reference in the variable but it doesn't
  6225. 5:15:01prevent us from reaching into the
  6226. 5:15:03reference and changing the object that
  6227. 5:15:05it's pointing to so just be careful when
  6228. 5:15:08you're working with with objects even
  6229. 5:15:10though we use const we can still change
  6230. 5:15:13the values inside the object another
  6231. 5:15:16interesting behavior is that on this
  6232. 5:15:18line we're not actually copying the
  6233. 5:15:21object into this variable we're copying
  6234. 5:15:24the reference so object two actually has
  6235. 5:15:28the same reference as object one they
  6236. 5:15:31actually point to the same object in the
  6237. 5:15:33computer's
  6238. 5:15:34memory so at the bottom if we type
  6239. 5:15:38console
  6240. 5:15:41log object 2 and save you'll notice that
  6241. 5:15:47for object two the message property was
  6242. 5:15:50also changed even though we only changed
  6243. 5:15:53object one the last interesting behavior
  6244. 5:15:56is that we can't compare objects
  6245. 5:15:59directly for example let's go to the
  6246. 5:16:02bottom and we'll create a new object
  6247. 5:16:06using
  6248. 5:16:07const object 3 and we'll make it equal
  6249. 5:16:12to an object and then inside we're going
  6250. 5:16:15to have these same values as object one
  6251. 5:16:19so we'll have the property
  6252. 5:16:22message and the value is the string good
  6253. 5:16:29job so now let's try comparing object 3
  6254. 5:16:33to object one so here we'll type console
  6255. 5:16:39do
  6256. 5:16:40log and check if object 3 is equal to
  6257. 5:16:46object
  6258. 5:16:48one if we
  6259. 5:16:50save it will tell us that these two
  6260. 5:16:53objects are not equal to each other even
  6261. 5:16:56though they have the same values inside
  6262. 5:16:59and that's because remember objects are
  6263. 5:17:02references so object 3 also contains a
  6264. 5:17:06reference and this code compares the
  6265. 5:17:09references and not the values inside the
  6266. 5:17:12object so because object 3 is a
  6267. 5:17:15different reference than object one
  6268. 5:17:17that's why this code results in
  6269. 5:17:20false if we
  6270. 5:17:22did
  6271. 5:17:24console. log and we check if object 2 is
  6272. 5:17:30equal to object
  6273. 5:17:331 object two has the same reference as
  6274. 5:17:37object one it points to the the same
  6275. 5:17:39object in the computer's memory so if we
  6276. 5:17:43save this will tell us that these two
  6277. 5:17:46variables have the same
  6278. 5:17:48reference so just remember that when
  6279. 5:17:50you're comparing two variables that
  6280. 5:17:52contain objects you're actually
  6281. 5:17:54comparing the references not the values
  6282. 5:17:57inside if we want to compare the values
  6283. 5:18:00inside we would do that manually by
  6284. 5:18:03checking the message property against
  6285. 5:18:05this message property so don't worry
  6286. 5:18:09object references are a little confusing
  6287. 5:18:11at first but the more you work with it
  6288. 5:18:13the more you just get used to
  6289. 5:18:15it the last thing we're going to learn
  6290. 5:18:17in this lesson are some shortcuts for
  6291. 5:18:20objects so first let's make a copy of
  6292. 5:18:24object 3 we'll select the code and
  6293. 5:18:28rightclick
  6294. 5:18:31copy and then at the
  6295. 5:18:33bottom we'll scroll down and right click
  6296. 5:18:37and paste
  6297. 5:18:40and let's rename this variable to object
  6298. 5:18:454 so now let's say that we want to get
  6299. 5:18:48the message property out of this object
  6300. 5:18:52we can do that by typing object
  6301. 5:18:564.
  6302. 5:18:58message and now let's also say that we
  6303. 5:19:01want to save this in a variable also
  6304. 5:19:04called message so at the front we'll
  6305. 5:19:07type con
  6306. 5:19:10message and make it equal to this
  6307. 5:19:15value so now if the variable name and
  6308. 5:19:18the property name are the same we
  6309. 5:19:21actually have a shortcut for this to use
  6310. 5:19:24a shortcut we're going to type const and
  6311. 5:19:28curly brackets
  6312. 5:19:31message and make it equal to object
  6313. 5:19:354 so this shortcut is called
  6314. 5:19:39destructuring this will take the message
  6315. 5:19:41property out of object 4 and save it in
  6316. 5:19:45a variable called message so this line
  6317. 5:19:48of code does the same thing as the code
  6318. 5:19:51above so let's comment out this line
  6319. 5:19:54using double slash and we'll just keep
  6320. 5:19:56it for reference so at the bottom if we
  6321. 5:20:00type
  6322. 5:20:04console.log
  6323. 5:20:06message and Save
  6324. 5:20:09it will display good job so the
  6325. 5:20:12destructuring shortcut took the property
  6326. 5:20:15out of the object and saved it in this
  6327. 5:20:18message
  6328. 5:20:19variable next we can use this
  6329. 5:20:22destructuring shortcut for multiple
  6330. 5:20:24properties for example let's add another
  6331. 5:20:28property to this object using comma and
  6332. 5:20:31we'll add a price property colon
  6333. 5:20:37$7.99 and now we can take the price
  6334. 5:20:40property out of this object by typing
  6335. 5:20:43after message a comma and
  6336. 5:20:48price so this will take the price
  6337. 5:20:50property out of the object and save it
  6338. 5:20:53in a variable called
  6339. 5:20:55price if we
  6340. 5:21:00console.log the
  6341. 5:21:02price and
  6342. 5:21:04save it will display $799
  6343. 5:21:08so that's the destructuring shortcut
  6344. 5:21:11it's an easier way to take properties
  6345. 5:21:13out of an
  6346. 5:21:14object the next shortcut we're going to
  6347. 5:21:17learn is called the shorthand property
  6348. 5:21:20so at the bottom let's create some new
  6349. 5:21:23lines and let's say we want to create a
  6350. 5:21:25new object so we'll type const and let's
  6351. 5:21:30name this object 5 and we'll make it
  6352. 5:21:34equal to an
  6353. 5:21:36object and inside ins this object let's
  6354. 5:21:40say we want to create a message property
  6355. 5:21:43so we'll type message colon and for the
  6356. 5:21:47value let's type the variable
  6357. 5:21:52message so what this does is that it
  6358. 5:21:54takes whatever is inside the message
  6359. 5:21:56variable and substitutes it into this
  6360. 5:22:00code so at the bottom if we
  6361. 5:22:05console.log object 5 and
  6362. 5:22:10save this object will have a property
  6363. 5:22:13called message and the value is whatever
  6364. 5:22:16is inside the message variable which is
  6365. 5:22:19good job so now we actually have a
  6366. 5:22:22shortcut for this code if the property
  6367. 5:22:26and the variable name are the same we
  6368. 5:22:28can actually just type it out once like
  6369. 5:22:32message so this code does the same thing
  6370. 5:22:35as message colon message and this
  6371. 5:22:38shortcut is called the shorthand
  6372. 5:22:41property so if we comment out this line
  6373. 5:22:44using double
  6374. 5:22:46slash and
  6375. 5:22:48save you can see that this shortcut
  6376. 5:22:51creates the same object that we had
  6377. 5:22:54before the last shortcut we're going to
  6378. 5:22:56learn is called the shorthand method so
  6379. 5:23:00at the end let's create a new property
  6380. 5:23:04comma and let's name it
  6381. 5:23:07method Po in and we'll save a function
  6382. 5:23:11in this property so we'll type
  6383. 5:23:14function and let's name it Function One
  6384. 5:23:19brackets and curly
  6385. 5:23:21brackets and inside this function let's
  6386. 5:23:28console.log
  6387. 5:23:31method and now here at the
  6388. 5:23:35bottom let's run this function by Ty
  6389. 5:23:38typing object 5. method so this will
  6390. 5:23:43access this function and we'll run it by
  6391. 5:23:46typing
  6392. 5:23:48brackets if we
  6393. 5:23:50save this will run the code in the
  6394. 5:23:52function and display method in the
  6395. 5:23:56console so we actually have a shortcut
  6396. 5:23:58for writing this so at the bottom we can
  6397. 5:24:02type method again and just type brackets
  6398. 5:24:06and curly brackets
  6399. 5:24:11console.log string
  6400. 5:24:14method so this code does the same thing
  6401. 5:24:17as the code above so these brackets will
  6402. 5:24:20create a function just like here so this
  6403. 5:24:24function name is actually not needed so
  6404. 5:24:27if we comment out these lines of
  6405. 5:24:32code and
  6406. 5:24:34save it will still display method in the
  6407. 5:24:37console because this is a shortcut for
  6408. 5:24:41these lines of code so this shortcut is
  6409. 5:24:44called the shorthand method
  6410. 5:24:46syntax and that's the end of this lesson
  6411. 5:24:50in this lesson we learned another type
  6412. 5:24:52of value called objects objects group
  6413. 5:24:55related values together we added a score
  6414. 5:24:59to our rock paper scissors game we
  6415. 5:25:02learned about built-in objects and used
  6416. 5:25:05the Json and local storage objects to
  6417. 5:25:07save our our score more permanently we
  6418. 5:25:10learned more details about objects like
  6419. 5:25:13null autoboxing and references and we
  6420. 5:25:17learned some useful shortcuts like
  6421. 5:25:19destructuring shorthand properties and
  6422. 5:25:22shorthand methods here are some
  6423. 5:25:24exercises to help you practice working
  6424. 5:25:27with objects
  6425. 5:25:49in this lesson we're going to learn one
  6426. 5:25:51of the most important features of
  6427. 5:25:53JavaScript called the document object
  6428. 5:25:56model or the Dom and we're going to use
  6429. 5:25:59the Dom to improve our rock paper
  6430. 5:26:02scissors project so first we'll create a
  6431. 5:26:06new file for this lesson let's go to our
  6432. 5:26:09code editor and click this icon to show
  6433. 5:26:13our files and then click this icon to
  6434. 5:26:16create a new file and we'll name this
  6435. 5:26:20file 09
  6436. 5:26:24dd.
  6437. 5:26:26HTML and press
  6438. 5:26:29enter next let's copy objects. HTML into
  6439. 5:26:34dom.
  6440. 5:26:35HTML so we'll click here to open the
  6441. 5:26:38file file and then click inside and then
  6442. 5:26:41type contrl A or command a to select the
  6443. 5:26:45code and then
  6444. 5:26:47rightclick
  6445. 5:26:49copy and in
  6446. 5:26:52here right click and
  6447. 5:26:56paste now let's hide these files and
  6448. 5:26:59we'll prepare this file for this lesson
  6449. 5:27:03so let's scroll to the
  6450. 5:27:05top and we'll change the title to
  6451. 5:27:09Dom and let's delete all the JavaScript
  6452. 5:27:12code from the previous
  6453. 5:27:16lesson now let's save this file and
  6454. 5:27:19we'll open it in the browser by right
  6455. 5:27:22clicking open with live
  6456. 5:27:26server and let's also open the console
  6457. 5:27:29on this page we'll right click click
  6458. 5:27:34inspect and then click the console
  6459. 5:27:38and now we can close the previous
  6460. 5:27:42tabs as well as the previous code for
  6461. 5:27:46now and for this lesson we're going to
  6462. 5:27:49do an extra step in the setup inside the
  6463. 5:27:53body we're going to create a
  6464. 5:27:55button so here let's type the code less
  6465. 5:28:00than button greater than and inside the
  6466. 5:28:04button we'll have the text
  6467. 5:28:07hello now let's
  6468. 5:28:09save and we should have a button on the
  6469. 5:28:12page and now we're ready to learn the
  6470. 5:28:15document object model or the Dom in the
  6471. 5:28:19previous lesson we learned about objects
  6472. 5:28:22which group multiple values together we
  6473. 5:28:25also learned about builtin objects which
  6474. 5:28:28are objects that are provided by
  6475. 5:28:31JavaScript some examples of built-in
  6476. 5:28:34objects are console math Json and local
  6477. 5:28:40storage the Dom is another builtin
  6478. 5:28:44object called the document object so
  6479. 5:28:48let's go to our script element and we're
  6480. 5:28:50going to type
  6481. 5:28:52document so this is a built-in object
  6482. 5:28:56provided by JavaScript and we've
  6483. 5:28:58actually seen this document object
  6484. 5:29:00before in the first lesson one of the
  6485. 5:29:03first pieces of code that we learned was
  6486. 5:29:06document dot body do inner Capital
  6487. 5:29:14HTML equals the string hello and
  6488. 5:29:20semicolon so this code removes
  6489. 5:29:23everything on the page and replaces it
  6490. 5:29:25with the text hello if we
  6491. 5:29:29save it will remove the button and just
  6492. 5:29:32put
  6493. 5:29:33hello and now that we learned objects in
  6494. 5:29:36the previous lesson we can fully
  6495. 5:29:38understand this
  6496. 5:29:40syntax document here is a built-in
  6497. 5:29:43object and this is the dot notation this
  6498. 5:29:47means we're accessing a property of the
  6499. 5:29:50document object called body now body is
  6500. 5:29:54also an object so we can use the dot
  6501. 5:29:57notation again to access a property of
  6502. 5:30:00the body object called inner
  6503. 5:30:04HTML and finally we use an equal sign to
  6504. 5:30:08change the inner HTML property to the
  6505. 5:30:10string
  6506. 5:30:12hello but as you can see there's
  6507. 5:30:15something very special about the inner
  6508. 5:30:17HTML property when we change this
  6509. 5:30:20property it also changes the web page so
  6510. 5:30:24this document object and the web page
  6511. 5:30:27are linked together let's do another
  6512. 5:30:31example we'll go to the end here and
  6513. 5:30:33press enter and we'll type the document
  6514. 5:30:37object again
  6515. 5:30:38and we'll use the dot notation again and
  6516. 5:30:41this time let's access the property
  6517. 5:30:45title and we'll change the title
  6518. 5:30:47property using an equal sign and we'll
  6519. 5:30:50change it to the string good
  6520. 5:30:55job if we
  6521. 5:30:57save changing the title property we'll
  6522. 5:31:01also change the title at the top of the
  6523. 5:31:04tabs here so now this says good job
  6524. 5:31:08again as you can see the document object
  6525. 5:31:11is linked to the web page so from these
  6526. 5:31:15examples it shows us that the document
  6527. 5:31:17object represents or models the web page
  6528. 5:31:22and that's why we call this feature the
  6529. 5:31:24document object model or the
  6530. 5:31:28Dom now that we know what the Dom means
  6531. 5:31:32let's learn the syntax rules for the Dom
  6532. 5:31:35so the Dom is just this built in
  6533. 5:31:38document object and this object works
  6534. 5:31:41the same way as any other object it
  6535. 5:31:44contains properties like body and title
  6536. 5:31:48that we can access using the dot
  6537. 5:31:51notation now the special thing about the
  6538. 5:31:54document object is that it is linked to
  6539. 5:31:57the web page that means if we change
  6540. 5:32:00certain properties in this object it
  6541. 5:32:02will also change the web
  6542. 5:32:04page now in addition to properties the
  6543. 5:32:08document object also has methods we can
  6544. 5:32:11use and we'll learn about them later in
  6545. 5:32:13this
  6546. 5:32:15lesson now that we understand the syntax
  6547. 5:32:18of the document object let's learn about
  6548. 5:32:21the properties and methods we can use
  6549. 5:32:24first let's comment out this previous
  6550. 5:32:26code using
  6551. 5:32:29slashstar star slash and we'll create
  6552. 5:32:33some new lines at the
  6553. 5:32:35top so the first property will we're
  6554. 5:32:37going to learn is called
  6555. 5:32:40document.title
  6556. 5:32:41so this property will give us the title
  6557. 5:32:45at the top of the tabs here so if we
  6558. 5:32:48console.log this property
  6559. 5:32:55console.log and
  6560. 5:32:57save this will give us the text at the
  6561. 5:33:00top which is Dom and that's what gets
  6562. 5:33:03displayed in the
  6563. 5:33:05console if we change the doc do. tile
  6564. 5:33:08property it will change the text at the
  6565. 5:33:11top of the tab so here if we type
  6566. 5:33:17document.title and make it equal to the
  6567. 5:33:21string
  6568. 5:33:23changed and
  6569. 5:33:26save now the title at the top of the tab
  6570. 5:33:29will be
  6571. 5:33:31changed so as you can see the title
  6572. 5:33:34property is linked to the title at the
  6573. 5:33:37top of the tab
  6574. 5:33:38we can use it to get the title and we
  6575. 5:33:41can also use it to change the
  6576. 5:33:44title next we're going to learn another
  6577. 5:33:46property called
  6578. 5:33:49document.body so let's go into our
  6579. 5:33:52code and let's type
  6580. 5:33:58document.body now document.body is a
  6581. 5:34:01very special property so this will
  6582. 5:34:04literally get this body HTML element
  6583. 5:34:07element and put it inside our
  6584. 5:34:11JavaScript so let's actually console.log
  6585. 5:34:14this to see how it works
  6586. 5:34:19console.log Open
  6587. 5:34:21Bracket and then close bracket and
  6588. 5:34:25save and inside the console it will tell
  6589. 5:34:29us that
  6590. 5:34:30document.body is the body HTML element
  6591. 5:34:34and if I hover over this it will tell us
  6592. 5:34:37that it is a exactly the body on our
  6593. 5:34:39page if I expand this you can see that
  6594. 5:34:43this code is exactly the code that we
  6595. 5:34:46have here and this shows us one of the
  6596. 5:34:49most important features of JavaScript we
  6597. 5:34:52can have HTML elements inside
  6598. 5:34:55JavaScript the document object or the
  6599. 5:34:58Dom combines JavaScript and HTML
  6600. 5:35:02together and it gives JavaScript control
  6601. 5:35:05of the web page
  6602. 5:35:07okay so now that we have an HTML element
  6603. 5:35:11inside our JavaScript what can we do
  6604. 5:35:13with it a really important thing to know
  6605. 5:35:16is that when an HTML element is inside a
  6606. 5:35:20JavaScript like this the HTML element is
  6607. 5:35:23converted into a JavaScript object so
  6608. 5:35:27for example if we do
  6609. 5:35:31console.
  6610. 5:35:32log type of document do
  6611. 5:35:39body and
  6612. 5:35:42save it will tell us that the body HTML
  6613. 5:35:45element is just an object when it's
  6614. 5:35:48inside
  6615. 5:35:50JavaScript so just like any other object
  6616. 5:35:53this has properties and methods we can
  6617. 5:35:56use so one useful property of the body
  6618. 5:36:00element is inner HTML which we saw
  6619. 5:36:03earlier so down here let's type
  6620. 5:36:09document dot body dot inner Capital
  6621. 5:36:17HTML so the inner HTML property controls
  6622. 5:36:22all the HTML inside the body that's why
  6623. 5:36:26it's called inner
  6624. 5:36:28HTML so if we access this property it
  6625. 5:36:31will give us all the HTML inside the
  6626. 5:36:35body so let's console.log to
  6627. 5:36:38confirm
  6628. 5:36:43console.log and
  6629. 5:36:46save and inner HTML will give us all of
  6630. 5:36:50this HTML inside the body
  6631. 5:36:53element and if we change the inner HTML
  6632. 5:36:56property it will change the HTML inside
  6633. 5:37:00the body element so let's create a new
  6634. 5:37:03line and we'll type document
  6635. 5:37:07dot
  6636. 5:37:09body do inner Capital
  6637. 5:37:14HTML and we'll change it to the string
  6638. 5:37:20changed so this will replace all of the
  6639. 5:37:23HTML inside the body with a text changed
  6640. 5:37:28if we
  6641. 5:37:29save that's exactly what it does and we
  6642. 5:37:32can actually confirm this by going into
  6643. 5:37:35the elements tab so this tab is for HTML
  6644. 5:37:39and CSS we can find the body element
  6645. 5:37:43expand it and you can see all of the
  6646. 5:37:47HTML that we had before was replaced
  6647. 5:37:50with
  6648. 5:37:51changed and don't worry about this part
  6649. 5:37:54this part is just added by live server
  6650. 5:37:57after so this shows that inner HTML
  6651. 5:38:00gives us control of all the HTML inside
  6652. 5:38:04the body we can get this h HTML and we
  6653. 5:38:08can also change this
  6654. 5:38:11HTML now another feature of inner HTML
  6655. 5:38:15is that we can replace this HTML with
  6656. 5:38:19new HTML code not just text so for
  6657. 5:38:23example instead of just text we can
  6658. 5:38:26replace it with the code less than
  6659. 5:38:29button greater than and also less than
  6660. 5:38:34slash button greater than so so this is
  6661. 5:38:37HTML code for creating a button and
  6662. 5:38:41inside the button let's put the text
  6663. 5:38:45good
  6664. 5:38:46job and now this code will replace all
  6665. 5:38:50the HTML inside the body with this new
  6666. 5:38:55HTML if we
  6667. 5:38:57save it removes all the previous HTML
  6668. 5:39:00and this new HTML creates this button so
  6669. 5:39:05using this inner HTML property
  6670. 5:39:08JavaScript can replace the HTML inside
  6671. 5:39:11the body to whatever we want we have
  6672. 5:39:14full control of the web
  6673. 5:39:16page all right now we're going to learn
  6674. 5:39:19a method of the document object that we
  6675. 5:39:22can use as a reminder a method is a
  6676. 5:39:25function that is saved inside an
  6677. 5:39:29object so we're going to learn a very
  6678. 5:39:31important method called document. query
  6679. 5:39:36selector we just learned that
  6680. 5:39:38document.body lets us get the body
  6681. 5:39:41element and put it inside our
  6682. 5:39:45JavaScript document. query selector lets
  6683. 5:39:48us get any element from the page and put
  6684. 5:39:51it inside our JavaScript let's give it a
  6685. 5:39:54try so first we'll comment out the
  6686. 5:39:57previous code using
  6687. 5:40:01slashstar and star
  6688. 5:40:03slash let's create some new lines at the
  6689. 5:40:06top
  6690. 5:40:08and let's type
  6691. 5:40:11document. query capital S
  6692. 5:40:16selector and
  6693. 5:40:19brackets so document. query selector
  6694. 5:40:22lets us get any element from the page
  6695. 5:40:24and put it inside our
  6696. 5:40:27JavaScript between these brackets we're
  6697. 5:40:29going to put a string this string tells
  6698. 5:40:33JavaScript which HTML element to get
  6699. 5:40:36from the page
  6700. 5:40:37so if we type
  6701. 5:40:40button this will get the first button
  6702. 5:40:43element from the page and put it inside
  6703. 5:40:46our JavaScript so let's console.log this
  6704. 5:40:49to
  6705. 5:40:50confirm
  6706. 5:40:52console.
  6707. 5:40:54log close bracket and
  6708. 5:40:58save and let's go back to our
  6709. 5:41:01console now it will tell us that
  6710. 5:41:04document. query selector got this button
  6711. 5:41:07element and put it inside our JavaScript
  6712. 5:41:11which you can see in the console and if
  6713. 5:41:14I hover over this button element it will
  6714. 5:41:16tell us that it is exactly the button on
  6715. 5:41:19the page so that's what document. query
  6716. 5:41:22selector does it can get any element
  6717. 5:41:25from the page and put it inside our
  6718. 5:41:28JavaScript this string tells it which
  6719. 5:41:31element to get if we type button it will
  6720. 5:41:35get the first button element from from
  6721. 5:41:37the page if we type
  6722. 5:41:40body this will now get the first body
  6723. 5:41:43element from the page so it will do the
  6724. 5:41:46same thing as
  6725. 5:41:48document.body but for now let's just
  6726. 5:41:51change this back to Button as an example
  6727. 5:41:56and
  6728. 5:41:57save so remember when an HTML element is
  6729. 5:42:01inside JavaScript it's just a JavaScript
  6730. 5:42:04object so this button element
  6731. 5:42:08is also just an object and it has
  6732. 5:42:11properties and methods we can
  6733. 5:42:13use every HTML element actually has a
  6734. 5:42:17property called inner
  6735. 5:42:19HTML which lets us control the HTML
  6736. 5:42:22inside the element so for this button
  6737. 5:42:26element let's type at the end dot inner
  6738. 5:42:33HTML and this will give us the HTML
  6739. 5:42:36inside the button if we
  6740. 5:42:40save it will give us
  6741. 5:42:42hello and again if we change the inner
  6742. 5:42:46HTML property it will change the HTML
  6743. 5:42:50inside the button so let's make a copy
  6744. 5:42:53of this code we'll select it and
  6745. 5:42:57rightclick
  6746. 5:42:59copy and down
  6747. 5:43:01here right click and
  6748. 5:43:05paste and let's change the inner HTML
  6749. 5:43:09property using an equal sign and the
  6750. 5:43:12string
  6751. 5:43:15changed so this will only change the
  6752. 5:43:18HTML inside the button which is here to
  6753. 5:43:23changed if we
  6754. 5:43:26save you can see that the text inside
  6755. 5:43:28the button is now changed a little
  6756. 5:43:32syntax shortcut I'm going to show you is
  6757. 5:43:34that for the dot notation we can
  6758. 5:43:37actually put it on multiple lines so we
  6759. 5:43:40can type document. query selector and
  6760. 5:43:43then put doin HTML on a separate line to
  6761. 5:43:47make it easier to read and make sure to
  6762. 5:43:50put a tab at the front so that we know
  6763. 5:43:52it's a continuation of the previous
  6764. 5:43:55line okay so using document. query
  6765. 5:43:59selector we can get any element from the
  6766. 5:44:02page put it inside our JavaScript and
  6767. 5:44:05then we can control the h HML inside
  6768. 5:44:08that element and this allows us to only
  6769. 5:44:11change the parts of the page that we
  6770. 5:44:15need next what if we have two buttons on
  6771. 5:44:18the page so let's go up here and we'll
  6772. 5:44:22create another button using less than
  6773. 5:44:26button greater than and we'll have the
  6774. 5:44:29text
  6775. 5:44:31second
  6776. 5:44:33button and
  6777. 5:44:36Save so now we have two buttons on the
  6778. 5:44:39page but document. query selector will
  6779. 5:44:42only get the first button on the page
  6780. 5:44:45how do we get this second button the
  6781. 5:44:49solution is that we're going to add an
  6782. 5:44:51attribute to this button called the
  6783. 5:44:53class attribute so in the opening tag
  6784. 5:44:57we're going to type space class equals
  6785. 5:45:02double quotes So the class attribute
  6786. 5:45:05sort of adds a label to this button so
  6787. 5:45:09inside the double quotes we're going to
  6788. 5:45:11label this button a JS dash
  6789. 5:45:17button and now that we labeled this
  6790. 5:45:19button we can select it using document.
  6791. 5:45:23query
  6792. 5:45:24selector so at the bottom here we'll
  6793. 5:45:28type
  6794. 5:45:30document. query selector again and
  6795. 5:45:35brackets and we'll type A String inside
  6796. 5:45:39and this time we're going to start with
  6797. 5:45:41a DOT when we start with a DOT this
  6798. 5:45:44means that it's going to look for a
  6799. 5:45:46class rather than an element name so
  6800. 5:45:50we're going to look for the class
  6801. 5:45:53js-
  6802. 5:45:55button and now document. query selector
  6803. 5:45:58will select an element with the class JS
  6804. 5:46:02button which is this element here so
  6805. 5:46:05let's confirm this by
  6806. 5:46:08console. logging this
  6807. 5:46:12code and
  6808. 5:46:14save and now you will see that this code
  6809. 5:46:18gives us the second button on the page
  6810. 5:46:22so using a class attribute and then
  6811. 5:46:24starting with a dot in query selector we
  6812. 5:46:27can get any specific element from the
  6813. 5:46:30page and put it in our
  6814. 5:46:32JavaScript by the way when we start with
  6815. 5:46:35a DOT this is called called a class
  6816. 5:46:38selector and this is also used in CSS to
  6817. 5:46:41style elements with a specific class in
  6818. 5:46:45fact anything we use in CSS can also be
  6819. 5:46:48used in this
  6820. 5:46:49string and also a common practice is to
  6821. 5:46:52start the class with
  6822. 5:46:55js- to make it clear that this class is
  6823. 5:46:58being used in
  6824. 5:47:00JavaScript and again HTML elements are
  6825. 5:47:04JavaScript objects so this second button
  6826. 5:47:07is also just an object and remember
  6827. 5:47:11objects are values and we can save them
  6828. 5:47:14inside a variable so instead of
  6829. 5:47:19console.log let's create a variable
  6830. 5:47:22using const and we'll name the variable
  6831. 5:47:26button capital E
  6832. 5:47:30element we usually end the variable name
  6833. 5:47:33with element or LM to make it clear
  6834. 5:47:36clear there's an HTML element inside
  6835. 5:47:39this
  6836. 5:47:40variable and finally we'll type an equal
  6837. 5:47:43sign to save this element and we can
  6838. 5:47:47remove this extra
  6839. 5:47:49bracket and now if we console.log this
  6840. 5:47:53variable
  6841. 5:47:57console.log
  6842. 5:47:58button
  6843. 5:48:00element and
  6844. 5:48:03save it will tell us that the second
  6845. 5:48:05button was Sav saved inside this
  6846. 5:48:08variable so using the Dom we can get
  6847. 5:48:12HTML elements from the page put it
  6848. 5:48:15inside a JavaScript and then save it in
  6849. 5:48:18a variable so the Dom combines
  6850. 5:48:21JavaScript and HTML together it gives
  6851. 5:48:24JavaScript full control of the web
  6852. 5:48:28page all right now that we learned how
  6853. 5:48:30to use the Dom let's do some practical
  6854. 5:48:34examples so I've actually prepared some
  6855. 5:48:36projects that we can do for this lesson
  6856. 5:48:39to open these projects we'll go to our
  6857. 5:48:42browser and create a new tab at the
  6858. 5:48:45top and here we'll type
  6859. 5:48:49super
  6860. 5:48:52simple. SL projects
  6861. 5:48:57slod and press
  6862. 5:49:00enter on this page we'll be working on
  6863. 5:49:03three projects the YouTube subscribe
  6864. 5:49:06button the rock paper scissors project
  6865. 5:49:08we've been working on for the last few
  6866. 5:49:10lessons and this Amazon shipping
  6867. 5:49:14calculator and you'll notice that these
  6868. 5:49:16projects modify the web page so we're
  6869. 5:49:19going to be using the Dom to create
  6870. 5:49:22these projects and in the next lesson
  6871. 5:49:25we're going to use CSS to style these
  6872. 5:49:27projects and make them look nice and you
  6873. 5:49:30can preview that by clicking
  6874. 5:49:33here so using CSS we can make the sub
  6875. 5:49:36subscribe button look like the actual
  6876. 5:49:38subscribe button on YouTube and if we
  6877. 5:49:41click this link to see the rock paper
  6878. 5:49:44scissors game you'll see that we'll make
  6879. 5:49:46it look really nice with these icons but
  6880. 5:49:50for this lesson we're only going to
  6881. 5:49:52focus on the functionality and not the
  6882. 5:49:55styling so let's go back to the projects
  6883. 5:49:59we'll click back and click back
  6884. 5:50:02again until we're at projects sdom
  6885. 5:50:07and we're going to create the
  6886. 5:50:08functionality for these projects first
  6887. 5:50:11so before we start let's create a new
  6888. 5:50:14file just for these projects we'll go to
  6889. 5:50:17our code editor and click this icon and
  6890. 5:50:22then click this icon to create a new
  6891. 5:50:25file let's name this file 09
  6892. 5:50:31dd-
  6893. 5:50:33projects. HTML and press
  6894. 5:50:38enter now let's copy dom. HTML into this
  6895. 5:50:42file so we'll open it and then click in
  6896. 5:50:46here and type control a or command a to
  6897. 5:50:50select the code and then
  6898. 5:50:53rightclick
  6899. 5:50:55copy and in
  6900. 5:50:57here right click and
  6901. 5:51:02paste now let's close our files and
  6902. 5:51:05we'll prepare this file
  6903. 5:51:07so let's scroll up and we'll change the
  6904. 5:51:09title to Dom
  6905. 5:51:13projects and let's remove our HTML but
  6906. 5:51:17keep the script
  6907. 5:51:20element and we'll also remove the
  6908. 5:51:23JavaScript from
  6909. 5:51:26before finally let's save this file and
  6910. 5:51:30we'll open it in the browser by right
  6911. 5:51:32clicking open with live server
  6912. 5:51:37and let's also open the console just in
  6913. 5:51:40case right click click
  6914. 5:51:44inspect and then click the
  6915. 5:51:46console all right we're ready to start
  6916. 5:51:49with the projects let's go back to super
  6917. 5:51:52simple
  6918. 5:51:54dodev and we'll start by creating the
  6919. 5:51:57YouTube subscribe button let's actually
  6920. 5:51:59press refresh to get it back to normal
  6921. 5:52:03and when we click this button it will
  6922. 5:52:05change to subscribe
  6923. 5:52:07red first let's create the HTML elements
  6924. 5:52:11for this project we'll go to the body
  6925. 5:52:14we'll create some new
  6926. 5:52:16lines and let's create the paragraph
  6927. 5:52:19first we'll type less than P greater
  6928. 5:52:24than and inside the paragraph we'll have
  6929. 5:52:27the text
  6930. 5:52:30YouTube
  6931. 5:52:31subscribe
  6932. 5:52:33button and now let's create the button
  6933. 5:52:37so on the next
  6934. 5:52:38line we'll type less than button greater
  6935. 5:52:43than and inside the button
  6936. 5:52:48subscribe now let's save and go back to
  6937. 5:52:51our
  6938. 5:52:52Tab and now we have the HTML elements
  6939. 5:52:56that we need so now let's make this
  6940. 5:52:58interactive with JavaScript and remember
  6941. 5:53:01we have a process for this first we'll
  6942. 5:53:04figure out what steps we need to do this
  6943. 5:53:07is called an algorithm and then we'll
  6944. 5:53:10convert these steps into code so the
  6945. 5:53:13steps that we need here are when we
  6946. 5:53:16click this button if the text is
  6947. 5:53:19subscribe we'll change it to
  6948. 5:53:22subscribed otherwise we'll change it
  6949. 5:53:24back now let's convert these steps into
  6950. 5:53:28code let's go to our button element and
  6951. 5:53:31we're going to add an onclick attribute
  6952. 5:53:34we'll type space on click equals double
  6953. 5:53:40quotes and inside here we'll write some
  6954. 5:53:44JavaScript so first we need to get the
  6955. 5:53:47text inside the button and we learned
  6956. 5:53:50how to do this earlier we can use
  6957. 5:53:52document. Query selector to put this
  6958. 5:53:55button into our JavaScript and then use
  6959. 5:53:58inner HTML to get this HTML inside the
  6960. 5:54:03button so in our code let's type
  6961. 5:54:08document. query
  6962. 5:54:11selector brackets and a string and as we
  6963. 5:54:16learned we're going to use a class
  6964. 5:54:18attribute to select this specific button
  6965. 5:54:22so in the opening tag let's add a class
  6966. 5:54:27attribute and let's make the class
  6967. 5:54:32js-
  6968. 5:54:34subscribe Dash
  6969. 5:54:37button and then inside document. query
  6970. 5:54:40selector we're going to type a DOT to
  6971. 5:54:43select a class and we're going to select
  6972. 5:54:45the class
  6973. 5:54:48js-
  6974. 5:54:50subscribe Das
  6975. 5:54:53button and now let's console.log this to
  6976. 5:54:56confirm that we got this button so at
  6977. 5:55:00the front
  6978. 5:55:05console.log and
  6979. 5:55:07save and now let's click our
  6980. 5:55:10button and it will show us that
  6981. 5:55:12document. query selector got the button
  6982. 5:55:15on the page so now let's save this
  6983. 5:55:18button in a variable so we can use it
  6984. 5:55:21later instead of
  6985. 5:55:24console.log let's type const to create a
  6986. 5:55:28variable and we'll name it
  6987. 5:55:31button
  6988. 5:55:33element and make it equal to this
  6989. 5:55:37result so now that we have the button
  6990. 5:55:40inside our JavaScript we're going to
  6991. 5:55:42check if the text inside the button is
  6992. 5:55:46subscribe to check the text we can use
  6993. 5:55:49an if statement so at the bottom here
  6994. 5:55:52we're going to type if brackets and
  6995. 5:55:56curly
  6996. 5:55:57brackets and inside this condition let's
  6997. 5:56:01get the text inside the button so we'll
  6998. 5:56:04type button
  6999. 5:56:07element do inner Capital
  7000. 5:56:13HTML and we're going to check if it's
  7001. 5:56:15equal to subscribe so we'll type triple
  7002. 5:56:19equal the string
  7003. 5:56:24subscribe now if it is equal to
  7004. 5:56:27subscribe inside the curly brackets
  7005. 5:56:30we're going to change the text in the
  7006. 5:56:32button to
  7007. 5:56:34subscribed so let's type the code
  7008. 5:56:38button
  7009. 5:56:39element do inner Capital HTM L and we'll
  7010. 5:56:45change it with an equal sign to the
  7011. 5:56:48string
  7012. 5:56:52subscribed and the last step is
  7013. 5:56:55otherwise we'll change it back so we'll
  7014. 5:56:58add an else to this if
  7015. 5:57:00statement else and curly brackets and
  7016. 5:57:05inside we're going to change the text
  7017. 5:57:08back to
  7018. 5:57:10subscribe so let's type
  7019. 5:57:13button
  7020. 5:57:15element. inner Capital
  7021. 5:57:19HTML and make it equal to the string
  7022. 5:57:25subscribe and
  7023. 5:57:27save now before you try out this project
  7024. 5:57:30make sure that the text inside the
  7025. 5:57:32button has no spaces or new lines around
  7026. 5:57:36it and it looks like this and I'll show
  7027. 5:57:39you why in a second let's go to our
  7028. 5:57:42project and try it out so when we click
  7029. 5:57:45this button it will change to the text
  7030. 5:57:49subscribed if we click it again it will
  7031. 5:57:52go back to the original text so that's
  7032. 5:57:55how we create the Subscribe button
  7033. 5:57:57project using the Dom so now let's go
  7034. 5:58:01back into our code and I'll show you why
  7035. 5:58:04if we add new lines
  7036. 5:58:06and some spaces around this
  7037. 5:58:08text there's actually going to be some
  7038. 5:58:11problems so if we save this
  7039. 5:58:14code and then go back to our project and
  7040. 5:58:17click the button again you'll notice
  7041. 5:58:20that it no longer changes the text and
  7042. 5:58:23that's because now we have a bunch of
  7043. 5:58:26spaces around this text and these spaces
  7044. 5:58:29will show up in inner
  7045. 5:58:32HTML and now inner HTML will not equal
  7046. 5:58:36exactly
  7047. 5:58:37subscribe so how do we solve this
  7048. 5:58:39problem when there are spaces around our
  7049. 5:58:42text to solve this we're going to use a
  7050. 5:58:45different property of an HTML element so
  7051. 5:58:49instead of the inner HTML property we're
  7052. 5:58:52going to use the property inner capital
  7053. 5:58:56T
  7054. 5:58:58text so inner text will give us the text
  7055. 5:59:02inside this element but without all the
  7056. 5:59:05spaces around it
  7057. 5:59:07so now this will equal exactly
  7058. 5:59:10subscribe if we save and go back to our
  7059. 5:59:14project and click it again it'll start
  7060. 5:59:17working again so that's the inner text
  7061. 5:59:20property of an HTML element it gets us
  7062. 5:59:24the text without the extra
  7063. 5:59:26spaces so before we move on to the
  7064. 5:59:29second project I'm going to show you how
  7065. 5:59:31to make our code
  7066. 5:59:33cleaner right now we're mixing a HTML
  7067. 5:59:37and JavaScript together so to make our
  7068. 5:59:40code cleaner and easier to read we're
  7069. 5:59:43going to separate the JavaScript code
  7070. 5:59:45and to do that we're going to put this
  7071. 5:59:47code in a function
  7072. 5:59:49instead so inside the script
  7073. 5:59:52element let's create a function using
  7074. 5:59:58function and we'll name the function
  7075. 6:00:02subcribe brackets and curly brackets
  7076. 6:00:07and inside these curly brackets we're
  7077. 6:00:09going to move all of this code so let's
  7078. 6:00:12select this
  7079. 6:00:13code and we'll right
  7080. 6:00:16click
  7081. 6:00:17cut and in here right click and
  7082. 6:00:23paste and if we need to fix the
  7083. 6:00:25formatting we can select these lines and
  7084. 6:00:28press tab a few times and now that we
  7085. 6:00:31have this function we're going to go to
  7086. 6:00:34the onclick attribute and and we're
  7087. 6:00:36going to call this function so we'll
  7088. 6:00:38type the function name
  7089. 6:00:41subscribe and
  7090. 6:00:44brackets so this will run all the code
  7091. 6:00:48inside the function so it will do the
  7092. 6:00:51same thing as before except now our code
  7093. 6:00:54is cleaner because we grouped all the
  7094. 6:00:57JavaScript code at the bottom and we're
  7095. 6:00:59no longer mixing as much HTML and
  7096. 6:01:02JavaScript
  7097. 6:01:04together now let's move on to the second
  7098. 6:01:07project we'll go back to superimple
  7099. 6:01:10dodev and we're going to work on the
  7100. 6:01:13rock paper scissors project so let's
  7101. 6:01:15click this
  7102. 6:01:17link and it will show us the next
  7103. 6:01:19version of this project so you notice a
  7104. 6:01:22few changes here the first change is
  7105. 6:01:25that we're going to show the score on
  7106. 6:01:27the page instead of in a popup and the
  7107. 6:01:31second change is that when we click one
  7108. 6:01:33of these buttons to play the game it
  7109. 6:01:36will show us the result and the moves
  7110. 6:01:39that we picked on the page rather than
  7111. 6:01:42in a
  7112. 6:01:43popup let's create a new file for this
  7113. 6:01:46rock paper scissors game for this lesson
  7114. 6:01:48so let's go to our code
  7115. 6:01:50editor and we'll click this icon and
  7116. 6:01:54click this icon to create a new file and
  7117. 6:01:57we'll name this
  7118. 6:02:0009-
  7119. 6:02:02rock- paper Das
  7120. 6:02:06scissors.
  7121. 6:02:08HTML and press
  7122. 6:02:11enter and let's copy the previous rock
  7123. 6:02:14paper scissors which is here we'll click
  7124. 6:02:17here and type control a or command a
  7125. 6:02:21right click
  7126. 6:02:24copy and here right click and
  7127. 6:02:30paste and let's hide our
  7128. 6:02:33files and let's save this F file and we
  7129. 6:02:37can also close 08 rock paper scissors
  7130. 6:02:41and finally we'll open this in the
  7131. 6:02:43browser by right clicking open with live
  7132. 6:02:49server now we're ready to begin the
  7133. 6:02:52second project so let's go back to the
  7134. 6:02:55project at super simple Dev and we're
  7135. 6:02:58going to start by putting the score on
  7136. 6:03:00the page so to do this we're going to
  7137. 6:03:03need somewhere to put the score so let's
  7138. 6:03:07create an HTML element for this we'll
  7139. 6:03:11scroll up to our
  7140. 6:03:13HTML which is
  7141. 6:03:16here and after the scissors button let's
  7142. 6:03:20add a paragraph element where we're
  7143. 6:03:22going to put our score so less than P
  7144. 6:03:26greater
  7145. 6:03:27than and let's add a class attribute so
  7146. 6:03:31we can select this element in our
  7147. 6:03:33JavaScript so in the opening tag we'll
  7148. 6:03:36type space class equals double quotes
  7149. 6:03:43js-
  7150. 6:03:45score now that we have a class let's get
  7151. 6:03:48this element in our
  7152. 6:03:50JavaScript so let's just do it down here
  7153. 6:03:54so we'll type
  7154. 6:03:57document.
  7155. 6:03:58query
  7156. 6:04:01selector brackets and a string and then
  7157. 6:04:04we're going to type a Dot and we're
  7158. 6:04:07going to select the class js- score so
  7159. 6:04:12js-
  7160. 6:04:14score so this will get the paragraph
  7161. 6:04:17element from above and put it inside our
  7162. 6:04:21JavaScript and now let's put the score
  7163. 6:04:24inside the element to do that we can use
  7164. 6:04:27inner
  7165. 6:04:28HTML dot inner Capital
  7166. 6:04:33HTML and we'll make it equal to the
  7167. 6:04:37score if we scroll
  7168. 6:04:40down we actually create the score down
  7169. 6:04:43here so we can actually just copy this
  7170. 6:04:46string so we'll select it
  7171. 6:04:49rightclick
  7172. 6:04:51copy and let's scroll
  7173. 6:04:55up to here and I'll actually put this on
  7174. 6:04:59a separate line to make it easier to
  7175. 6:05:02read and now here we're going to type a
  7176. 6:05:05string using back
  7177. 6:05:08ticks and inside the string we're going
  7178. 6:05:10to rightclick and
  7179. 6:05:14paste so this will put the score that we
  7180. 6:05:17used to have in our popup inside this
  7181. 6:05:21element so inside this
  7182. 6:05:23paragraph if we
  7183. 6:05:25save and then go back to our
  7184. 6:05:28tab you can see that now it displays the
  7185. 6:05:31score on the page now before we move on
  7186. 6:05:35I want want to point something out with
  7187. 6:05:37HTML so remember that our reset score
  7188. 6:05:40button was beside our other buttons and
  7189. 6:05:43now it's below them so what determines
  7190. 6:05:46if an element appears beside something
  7191. 6:05:49or below something so the paragraph
  7192. 6:05:52element Here is known as a block element
  7193. 6:05:56a block element will always take up an
  7194. 6:05:59entire line by itself so this line
  7195. 6:06:03already has some buttons so the
  7196. 6:06:05paragraph will take up the entire line
  7197. 6:06:07under the buttons that's why this text
  7198. 6:06:10appears under the buttons and not beside
  7199. 6:06:13them and also because a block element
  7200. 6:06:16like this paragraph takes up this entire
  7201. 6:06:19line the next button has to appear below
  7202. 6:06:23it so that's why we get this layout here
  7203. 6:06:27so this is just a short review of
  7204. 6:06:29layouts if you want to learn this in
  7205. 6:06:31more detail you can check out my HTML
  7206. 6:06:34and CSS full course in the video
  7207. 6:06:37description all right the next feature
  7208. 6:06:40we're going to do is if we play the game
  7209. 6:06:43we also want to update the score right
  7210. 6:06:46now if we play it doesn't update it on
  7211. 6:06:49the page let's fix this in our
  7212. 6:06:52JavaScript so let's go down to where we
  7213. 6:06:55update the score so we'll scroll
  7214. 6:06:58down which is right here we update the
  7215. 6:07:02score and save it in local storage and
  7216. 6:07:05after we do this we're also going to
  7217. 6:07:07update the score on the page so we can
  7218. 6:07:11actually use the same code that we used
  7219. 6:07:14above which is here so let's make a copy
  7220. 6:07:18of this code
  7221. 6:07:20rightclick
  7222. 6:07:21copy and down
  7223. 6:07:24here right click and
  7224. 6:07:29paste and I'll add a tab at the front if
  7225. 6:07:33we save and then try out our game we'll
  7226. 6:07:37click a move press
  7227. 6:07:39okay and now you'll notice the score
  7228. 6:07:42also updates on the page so let's go
  7229. 6:07:45back into our code and do some cleanup
  7230. 6:07:48so you notice that this code is the same
  7231. 6:07:50as the code we had above and this is a
  7232. 6:07:53great situation to use a function to
  7233. 6:07:56reuse this code so at the bottom here
  7234. 6:08:00let's create a function using function
  7235. 6:08:05and we'll call it
  7236. 6:08:08update
  7237. 6:08:09score
  7238. 6:08:11element brackets and curly
  7239. 6:08:14brackets and we'll move this code into
  7240. 6:08:17the function so we'll select it right
  7241. 6:08:21click
  7242. 6:08:22copy and in here right click and
  7243. 6:08:28paste and now instead of writing this
  7244. 6:08:30code multiple times we're just going to
  7245. 6:08:33call the function
  7246. 6:08:36update score element and
  7247. 6:08:42brackets this will run all the code
  7248. 6:08:44inside the function so it does the same
  7249. 6:08:46thing as before except we can reuse the
  7250. 6:08:50code so let's scroll back
  7251. 6:08:53up and instead of typing out all this
  7252. 6:08:56code again we can just use our
  7253. 6:09:00function
  7254. 6:09:01update
  7255. 6:09:03score element
  7256. 6:09:05and
  7257. 6:09:07brackets now let's
  7258. 6:09:09save it will display the score on the
  7259. 6:09:12page at the start and when we click a
  7260. 6:09:15move and click okay it will update the
  7261. 6:09:19score on the page so the last feature
  7262. 6:09:22we're going to do in relation to the
  7263. 6:09:24score is when we click the reset score
  7264. 6:09:27button we also want to update the score
  7265. 6:09:29on the
  7266. 6:09:30page right now if we click it it doesn't
  7267. 6:09:34actually update it
  7268. 6:09:36so let's go into our code and let's go
  7269. 6:09:38to the reset button which is here and
  7270. 6:09:43after we reset the score we're going to
  7271. 6:09:46call that function again
  7272. 6:09:49update
  7273. 6:09:51score element and
  7274. 6:09:54brackets if we
  7275. 6:09:57save and now let's play the game and
  7276. 6:10:00press okay so we have a score and if we
  7277. 6:10:04click reset score it will reset
  7278. 6:10:07everything back to zero so now we
  7279. 6:10:11finished the score let's go back to
  7280. 6:10:13super simple
  7281. 6:10:15Dev and play the
  7282. 6:10:17game and now we're going to work on this
  7283. 6:10:20section we're going to show the result
  7284. 6:10:23and the moves that we picked on the page
  7285. 6:10:27so first let's create some paragraphs
  7286. 6:10:29for these as well we'll go to our
  7287. 6:10:32HTML and above the score
  7288. 6:10:36let's add a less than P greater than
  7289. 6:10:41this is going to be the result so let's
  7290. 6:10:44give it a class of
  7291. 6:10:48js-
  7292. 6:10:50result and let's create another
  7293. 6:10:53paragraph less than P greater than and
  7294. 6:10:57we'll give this one a class equals
  7295. 6:11:03js- moves
  7296. 6:11:05now let's use the Dom to put the result
  7297. 6:11:09and the moves in these paragraphs feel
  7298. 6:11:12free to pause the video if you want to
  7299. 6:11:14try it yourself first so let's scroll
  7300. 6:11:17down to where we display the
  7301. 6:11:21result which is here so we display it in
  7302. 6:11:24a popup and instead of a popup we're
  7303. 6:11:29going to get the paragraph and put this
  7304. 6:11:31result inside the paragraph so we'll
  7305. 6:11:34type
  7306. 6:11:36document. query
  7307. 6:11:39selector brackets and a string and we'll
  7308. 6:11:43type a DOT and select the class
  7309. 6:11:48js-
  7310. 6:11:50result and then dot inner Capital
  7311. 6:11:56HTML and we'll change this to
  7312. 6:12:01result and now let's also display the
  7313. 6:12:04moves so we'll type some new lines and
  7314. 6:12:09document. query selector again brackets
  7315. 6:12:14and a string dot
  7316. 6:12:18js-
  7317. 6:12:20moves do inner Capital
  7318. 6:12:25HTML and make it equal to this text
  7319. 6:12:29right here so let's type a template
  7320. 6:12:32string because we're going to insert our
  7321. 6:12:34Moves In inside the string and we'll
  7322. 6:12:37Type
  7323. 6:12:39U and we'll insert the player move
  7324. 6:12:42dollar curly
  7325. 6:12:44brackets Player move and then a dash and
  7326. 6:12:50then the computers move so dollar curly
  7327. 6:12:54brackets again and
  7328. 6:12:57computer
  7329. 6:12:59move computer at the end so now that
  7330. 6:13:03we're displaying everything on the page
  7331. 6:13:06we don't need this popup at the end so
  7332. 6:13:09we can actually delete this
  7333. 6:13:12popup all right now let's
  7334. 6:13:15save and go back to our
  7335. 6:13:18Tab and play our
  7336. 6:13:21game now it will show the result and the
  7337. 6:13:24moves that we picked on the page instead
  7338. 6:13:27of in a popup let's play it again to
  7339. 6:13:30make sure it works and finally when we
  7340. 6:13:33click reset score it it will reset
  7341. 6:13:35everything back to zero and that's it
  7342. 6:13:39now we're displaying everything on the
  7343. 6:13:41web page using the
  7344. 6:13:43Dom now let's move on to the third and
  7345. 6:13:46final project we'll go back to Super
  7346. 6:13:50simple.
  7347. 6:13:52deev and we'll click back to go back to
  7348. 6:13:55our projects and we're going to create
  7349. 6:13:58this third project here which is an
  7350. 6:14:01Amazon shipping
  7351. 6:14:02calculator the way this works is is that
  7352. 6:14:05inside this text box we're going to type
  7353. 6:14:08the cost of an order in dollars if the
  7354. 6:14:11order is under
  7355. 6:14:13$4 for example
  7356. 6:14:16$25 and we click
  7357. 6:14:18calculate it will add $10 of shipping
  7358. 6:14:22and display the total down here but if
  7359. 6:14:25we type a cost that is over
  7360. 6:14:27$40 like 50 and press
  7361. 6:14:32calculate it will be free shipping so so
  7362. 6:14:35it will display the same total down here
  7363. 6:14:38so let's learn how to create this using
  7364. 6:14:40the
  7365. 6:14:41Dom let's go back to our 09 Dom projects
  7366. 6:14:46file and we'll start by creating the
  7367. 6:14:49HTML
  7368. 6:14:50elements so under our subscribe
  7369. 6:14:53button let's create this paragraph using
  7370. 6:14:58less than P greater than
  7371. 6:15:02Amazon shipping
  7372. 6:15:06calculator and next let's create this
  7373. 6:15:09text box so the HTML element for a text
  7374. 6:15:13box is called input so we'll type less
  7375. 6:15:17than input greater than so the input
  7376. 6:15:22element is special because it doesn't
  7377. 6:15:24require a closing tag this is called a
  7378. 6:15:28void
  7379. 6:15:29element and lastly let's create the
  7380. 6:15:32button so here we create less than
  7381. 6:15:37button greater than
  7382. 6:15:41calculate now let's
  7383. 6:15:43save and open the tab for this file
  7384. 6:15:47which is right here and we have the text
  7385. 6:15:51the text box and the button that we need
  7386. 6:15:55now one other thing we're going to add
  7387. 6:15:57is that in the final project if we don't
  7388. 6:16:00have anything in the text box we're
  7389. 6:16:02going to have this gray text so this is
  7390. 6:16:05called a placeholder to add a
  7391. 6:16:08placeholder to a text box we're going to
  7392. 6:16:11use the placeholder attribute so inside
  7393. 6:16:14our input element we're going to type
  7394. 6:16:18Space Place
  7395. 6:16:20holder equals double quotes and inside
  7396. 6:16:25the quotes we'll put the text we want as
  7397. 6:16:27the placeholder which is cost of
  7398. 6:16:33order and
  7399. 6:16:35save now if we go back to our project we
  7400. 6:16:39now have a placeholder in our text box
  7401. 6:16:42that disappears when we start
  7402. 6:16:45typing so that's all the HTML that we
  7403. 6:16:48need now let's work on the JavaScript
  7404. 6:16:51and again we'll figure out what steps we
  7405. 6:16:54need to do so when we click this button
  7406. 6:16:58we're going to get the text inside the
  7407. 6:17:00text box if the amount is below $40
  7408. 6:17:04we'll add $10 of shipping and then we'll
  7409. 6:17:07display the total on the page let's
  7410. 6:17:11convert these steps into code so we'll
  7411. 6:17:14go to our button and add and onclick
  7412. 6:17:19attribute and inside here let's also
  7413. 6:17:23create a function to separate our
  7414. 6:17:25JavaScript code like we did before so in
  7415. 6:17:29the script we're going to create a
  7416. 6:17:32function function
  7417. 6:17:34and let's name it
  7418. 6:17:37calculate total brackets and curly
  7419. 6:17:42brackets and inside on click we're just
  7420. 6:17:46going to call the function so
  7421. 6:17:50calculate
  7422. 6:17:51total
  7423. 6:17:53brackets the next step is to get the
  7424. 6:17:56text inside the text box so first let's
  7425. 6:18:01get this input element and put it inside
  7426. 6:18:04our J JavaScript to do that we can use
  7427. 6:18:07document. Query selector again so we'll
  7428. 6:18:11add a class to this input so we can
  7429. 6:18:13select it class equals
  7430. 6:18:18js-
  7431. 6:18:20cost-
  7432. 6:18:22input and then inside our JavaScript
  7433. 6:18:25we'll use
  7434. 6:18:27document.
  7435. 6:18:29Query
  7436. 6:18:30selector brackets and a string and a Dot
  7437. 6:18:35and this class
  7438. 6:18:39js-
  7439. 6:18:40cost-
  7440. 6:18:42input so this code will get the input
  7441. 6:18:45element and put it inside our JavaScript
  7442. 6:18:49let's confirm by
  7443. 6:18:53console. logging the
  7444. 6:18:57result and
  7445. 6:19:00save and now when we click the calculate
  7446. 6:19:03button it will give us
  7447. 6:19:05the input element from the page let's
  7448. 6:19:08save this input element in a variable so
  7449. 6:19:11we can use it later instead of
  7450. 6:19:14console.log we'll use const and we'll
  7451. 6:19:17name the variable
  7452. 6:19:20input element and make it equal to this
  7453. 6:19:24element and remove the extra
  7454. 6:19:27bracket and next we need to get the text
  7455. 6:19:30inside the input element so so far in
  7456. 6:19:33this lesson we've been using the
  7457. 6:19:35property inner HTML to do this however
  7458. 6:19:40for input elements you notice that there
  7459. 6:19:42is no HTML inside so for input elements
  7460. 6:19:47we're going to use a different property
  7461. 6:19:49called
  7462. 6:19:50value so at the bottom here we're going
  7463. 6:19:53to type
  7464. 6:19:56input
  7465. 6:19:58element.
  7466. 6:20:00value this is how we get the text inside
  7467. 6:20:03a text box
  7468. 6:20:05so let's console.log this to confirm
  7469. 6:20:08that it works so we
  7470. 6:20:11console do
  7471. 6:20:15log and
  7472. 6:20:17save and now let's type something in the
  7473. 6:20:20text box like 50 and click
  7474. 6:20:25calculate and input. value will give us
  7475. 6:20:28the text inside the text
  7476. 6:20:31box let's save this text in a variable
  7477. 6:20:34so so we can use it later con and we'll
  7478. 6:20:38name the variable cost and make it equal
  7479. 6:20:42to the text in the text
  7480. 6:20:45box the next step is to check if this
  7481. 6:20:48cost is under
  7482. 6:20:50$40 if it is We'll add
  7483. 6:20:53$10 so down here let's type if brackets
  7484. 6:20:58and curly
  7485. 6:21:00brackets and inside the condition we'll
  7486. 6:21:03check if cost is less than
  7487. 6:21:0740 and if it is inside the curly
  7488. 6:21:11brackets we'll do cost equals itself
  7489. 6:21:17plus $10 of shipping and remember when
  7490. 6:21:21we change a variable we have to switch
  7491. 6:21:24from const to
  7492. 6:21:26let and finally we're going to display
  7493. 6:21:29the total cost on the page so let's
  7494. 6:21:32create a paragraph where we can display
  7495. 6:21:35the cost so down here we'll create a
  7496. 6:21:39paragraph less than P greater than let's
  7497. 6:21:44give it a class
  7498. 6:21:48js- total Das cost and now inside our
  7499. 6:21:53JavaScript let's use
  7500. 6:21:57document. Query selector brackets and
  7501. 6:22:02string and we'll Type A Dot and the
  7502. 6:22:05class
  7503. 6:22:07js- total Das cost and we're going to
  7504. 6:22:12put the cost inside the paragraph we can
  7505. 6:22:16use dot inner Capital
  7506. 6:22:21HTML and make it equal and let's use a
  7507. 6:22:24template string so back ticks and we'll
  7508. 6:22:28have a dollar and we're going to insert
  7509. 6:22:31the cost using dollar and curly brackets
  7510. 6:22:37cost now let's save and try out this
  7511. 6:22:41project so first we'll type a cost that
  7512. 6:22:44is over
  7513. 6:22:46$40 like 50 if we press
  7514. 6:22:50calculate we're not going to add $10 of
  7515. 6:22:53shipping and we'll display the same
  7516. 6:22:55total on the page now let's try a number
  7517. 6:22:59under
  7518. 6:23:00$40 like 25 and press calculate
  7519. 6:23:05and this number doesn't look right it's
  7520. 6:23:08saying that we have to pay
  7521. 6:23:11$2,510 so what's going on here let's go
  7522. 6:23:14into our code and let's use console.log
  7523. 6:23:17to figure out what's wrong so first
  7524. 6:23:21let's console.log the cost to make sure
  7525. 6:23:24that we're getting the right number from
  7526. 6:23:25the text box so down here
  7527. 6:23:31console.log the cost
  7528. 6:23:34and save and then let's Type 25 again
  7529. 6:23:39and press
  7530. 6:23:40calculate and it will give us 25 so
  7531. 6:23:43we're getting the correct text from the
  7532. 6:23:45text box now let's console.log down here
  7533. 6:23:49to see if the math is working so we'll
  7534. 6:23:52type
  7535. 6:23:54console.
  7536. 6:23:55log
  7537. 6:23:57cost and
  7538. 6:23:59save and let's try 25 again and
  7539. 6:24:04calculate
  7540. 6:24:05and down here when we add 10 this is
  7541. 6:24:09where things go wrong and it results in
  7542. 6:24:1320510 so it's not doing math properly
  7543. 6:24:16here so let's check if cost is a number
  7544. 6:24:20to make sure that it's doing math so
  7545. 6:24:23here we'll type
  7546. 6:24:27console.log the type of
  7547. 6:24:32cost and Save
  7548. 6:24:35let's Type 25 again and calculate and
  7549. 6:24:40aha it's telling us that the cost is a
  7550. 6:24:44string so this is actually not a number
  7551. 6:24:47it is a string that's why it's not doing
  7552. 6:24:50math so remember from the strings lesson
  7553. 6:24:53when we add a string with a number
  7554. 6:24:57JavaScript will automatically convert
  7555. 6:24:59this number into a string and combine
  7556. 6:25:02them together that's that's why it just
  7557. 6:25:04combines 25 and 10 here this is called
  7558. 6:25:09type
  7559. 6:25:10coercion so whenever we get a value from
  7560. 6:25:13the Dom like this the value will be a
  7561. 6:25:17string so in order to do math we have to
  7562. 6:25:20manually convert this string into a
  7563. 6:25:23number to do that JavaScript has a
  7564. 6:25:26builtin function called number so before
  7565. 6:25:30this code we're going to type capital n
  7566. 6:25:35number Open Bracket and close bracket so
  7567. 6:25:40the number function takes whatever's
  7568. 6:25:42between the brackets and manually
  7569. 6:25:44converts it into a number so now if we
  7570. 6:25:49save and we Type 25 and press
  7571. 6:25:54calculate the cost is now a number and
  7572. 6:25:57it will do the math properly and give us
  7573. 6:26:0135 so remember whenever we get a value
  7574. 6:26:04from the Dom the value will be a string
  7575. 6:26:07so if we want to do math with it we have
  7576. 6:26:10to manually convert this value into a
  7577. 6:26:13number first so now let's remove the
  7578. 6:26:16console. logs that we used to figure out
  7579. 6:26:19this
  7580. 6:26:21problem and
  7581. 6:26:24save okay we're going to add one more
  7582. 6:26:27feature to this project let's go back to
  7583. 6:26:30superimple
  7584. 6:26:32dodev and inside the text box we're
  7585. 6:26:35going to Type 25 and press
  7586. 6:26:39enter so pressing enter will also
  7587. 6:26:42calculate the total and display it on
  7588. 6:26:45the page so let's learn how to create
  7589. 6:26:47this feature so if we go back to our
  7590. 6:26:50code so far in this course we've been
  7591. 6:26:53using the attribute
  7592. 6:26:55onclick which runs some JavaScript when
  7593. 6:26:57we click an element HTML has another
  7594. 6:27:02attribute we can use called on key down
  7595. 6:27:06which runs JavaScript when we press a
  7596. 6:27:09key down on our keyboard so inside the
  7597. 6:27:12input this time let's add the attribute
  7598. 6:27:16on key down equals double quotes and
  7599. 6:27:22then inside here
  7600. 6:27:26console. log and the string
  7601. 6:27:31typing so now every time we type in the
  7602. 6:27:34input it will console.log this message
  7603. 6:27:38let's save it and give it a try we'll go
  7604. 6:27:42to the tab for this file and then inside
  7605. 6:27:45the text box we'll type some numbers and
  7606. 6:27:49you can see that every time we type
  7607. 6:27:51something it will run this code and
  7608. 6:27:54display this
  7609. 6:27:56message so that's how we do something
  7610. 6:27:58when we type in an element so clicks and
  7611. 6:28:02key Downs are known known as events on
  7612. 6:28:06click and on key down are known as event
  7613. 6:28:10listeners they check for events and runs
  7614. 6:28:13JavaScript when those events happen now
  7615. 6:28:17in addition to clicks and key Downs
  7616. 6:28:20there are many other events we can
  7617. 6:28:21listen for in
  7618. 6:28:23JavaScript okay so now let's figure out
  7619. 6:28:26what steps we need to do for this
  7620. 6:28:28project when we press a key down in the
  7621. 6:28:31text box we're going to check if the key
  7622. 6:28:34is the enter key if it is we'll do the
  7623. 6:28:38same thing as the calculate
  7624. 6:28:41button so how do we know what key we
  7625. 6:28:44pressed inside on key down we're
  7626. 6:28:47actually given a special object called
  7627. 6:28:51event so instead of this string let's
  7628. 6:28:54remove it and we'll
  7629. 6:28:56console.log
  7630. 6:28:58event so this is an object that's
  7631. 6:29:01provided to us by JavaScript and it
  7632. 6:29:03contains information about the event
  7633. 6:29:06every event listener including on click
  7634. 6:29:09also gets an event object if we
  7635. 6:29:15save and type in this text
  7636. 6:29:18box it will show us that event is this
  7637. 6:29:22object and inside the object it has a
  7638. 6:29:25bunch of information including the key
  7639. 6:29:29that we typed so here let's use the
  7640. 6:29:33event object and we'll get dot key and
  7641. 6:29:38save so now inside this text box if we
  7642. 6:29:42type something it will tell us the key
  7643. 6:29:45that we pressed and if we press
  7644. 6:29:47enter it will tell us that the key is
  7645. 6:29:51enter so the next step is to check if
  7646. 6:29:54the key is enter and we can do that
  7647. 6:29:57using an if statement so in our
  7648. 6:30:00code we'll type a new line
  7649. 6:30:04if brackets and curly brackets and
  7650. 6:30:08inside the condition we'll check if
  7651. 6:30:12event. key is equal to the string
  7652. 6:30:18enter if it is equal to enter we're
  7653. 6:30:22going to do the same thing as the
  7654. 6:30:23calculate button so we can actually just
  7655. 6:30:26use this function again inside the curly
  7656. 6:30:30brackets we'll type calculate
  7657. 6:30:35total and
  7658. 6:30:37brackets and
  7659. 6:30:40save so now let's go back to our project
  7660. 6:30:43and type 25 enter and it will calculate
  7661. 6:30:48the total and display it on the page so
  7662. 6:30:51that's how we create this keyboard
  7663. 6:30:53feature of this project lastly let's
  7664. 6:30:57clean up our code and move this into a
  7665. 6:30:59function like the rest of our code so
  7666. 6:31:03inside the script
  7667. 6:31:05element let's create a new function
  7668. 6:31:08using
  7669. 6:31:10function and we'll name it
  7670. 6:31:13handle cost key down brackets and curly
  7671. 6:31:19brackets and let's move this code into
  7672. 6:31:22the function we'll select it
  7673. 6:31:26rightclick
  7674. 6:31:28cut and in here right click and paste
  7675. 6:31:34if we need to fix the formatting we can
  7676. 6:31:36select these lines and press Tab and
  7677. 6:31:39let's remove this
  7678. 6:31:41console.log and now up here we're going
  7679. 6:31:44to call this
  7680. 6:31:45function
  7681. 6:31:47handle cost key down and
  7682. 6:31:53brackets now the last thing we have to
  7683. 6:31:55do is that event does not exist inside
  7684. 6:31:59this function so we have to put the
  7685. 6:32:01event object into the function
  7686. 6:32:04and remember to put a value into a
  7687. 6:32:06function we can use a
  7688. 6:32:09parameter so between these brackets we
  7689. 6:32:12can create a parameter called event and
  7690. 6:32:16now to save something inside this
  7691. 6:32:18parameter we're going to go up to these
  7692. 6:32:21brackets and type
  7693. 6:32:24event this will take the event object
  7694. 6:32:27and put it inside the function where we
  7695. 6:32:30can use it just like a variable so now
  7696. 6:32:34if we
  7697. 6:32:35save and try out the project 25 enter so
  7698. 6:32:41this still works and now we finished all
  7699. 6:32:44three projects in this lesson so in the
  7700. 6:32:47rest of this lesson we're going to learn
  7701. 6:32:49some more details about
  7702. 6:32:51JavaScript in the last project we
  7703. 6:32:54learned how to manually convert a string
  7704. 6:32:57into a number using the number function
  7705. 6:33:01we can also convert the other way from a
  7706. 6:33:04number to a string so let's go to the
  7707. 6:33:07top of the script
  7708. 6:33:09element and we can type capital S string
  7709. 6:33:15and brackets so the built-in string
  7710. 6:33:18function will convert a value into a
  7711. 6:33:21string between the brackets we can put a
  7712. 6:33:24number like 25 and this will convert the
  7713. 6:33:28number into the string
  7714. 6:33:3025 the next detail we're going to learn
  7715. 6:33:33is a special behavior of
  7716. 6:33:35strings So Below this line we can type
  7717. 6:33:40console do log the string
  7718. 6:33:4525 minus
  7719. 6:33:475 so if a string only contains a number
  7720. 6:33:51and we subtract multiply or divide
  7721. 6:33:55JavaScript will automatically convert
  7722. 6:33:57the string into a number so this gets
  7723. 6:34:01converted into the number 25 minus 5 and
  7724. 6:34:05if we save it will do math and give us
  7725. 6:34:1020 so this is another example of type
  7726. 6:34:13coercion or automatic type
  7727. 6:34:17conversion however it's not recommended
  7728. 6:34:19to do math with strings like this
  7729. 6:34:22because adding Works differently for
  7730. 6:34:25example on a new line let's type
  7731. 6:34:30console.log the string 25
  7732. 6:34:34+ 5 and
  7733. 6:34:38save this will convert the number into a
  7734. 6:34:41string and combine them together into
  7735. 6:34:45255 so because adding strings Works
  7736. 6:34:48differently the best practice is always
  7737. 6:34:51do math using numbers don't do math
  7738. 6:34:54using strings if we need to we can
  7739. 6:34:58manually convert the string into a
  7740. 6:35:00number using the number function like we
  7741. 6:35:03did here the last thing we're going to
  7742. 6:35:06learn in this lesson is another built-in
  7743. 6:35:09object called the window object so in
  7744. 6:35:13this lesson we learned about the
  7745. 6:35:15document object the document object
  7746. 6:35:18represents the web page the window
  7747. 6:35:21object represents the browser so let's
  7748. 6:35:25go into our script element and Try It
  7749. 6:35:28Out Below this we'll Type
  7750. 6:35:32window so so this is the window object
  7751. 6:35:35and it represents the browser because it
  7752. 6:35:39represents the browser everything inside
  7753. 6:35:41the browser is inside the window object
  7754. 6:35:45so let's think what's inside the browser
  7755. 6:35:49well the web page is inside the browser
  7756. 6:35:52so the document object is inside the
  7757. 6:35:55window object if we type
  7758. 6:35:59window.
  7759. 6:36:01document this will give us the same
  7760. 6:36:04document object that we've been using in
  7761. 6:36:06this lesson so what else is part of the
  7762. 6:36:10browser well the console is also part of
  7763. 6:36:13the browser so here if we type
  7764. 6:36:18window.
  7765. 6:36:20console this will give us the same
  7766. 6:36:23console object we've been using in this
  7767. 6:36:25course so we can actually do
  7768. 6:36:30console.log and the string window
  7769. 6:36:35and
  7770. 6:36:36save and this will do the same thing as
  7771. 6:36:41console.log what else is part of the
  7772. 6:36:43browser well popups are also part of the
  7773. 6:36:46browser so if we type
  7774. 6:36:50window.
  7775. 6:36:52alert this will give us the same alert
  7776. 6:36:55function that we've been using in this
  7777. 6:36:57course to create
  7778. 6:36:59popups however you might have noticed
  7779. 6:37:01that we never Type window window. doent
  7780. 6:37:05or window.
  7781. 6:37:07console.log and that's because the
  7782. 6:37:09window object has a shortcut we don't
  7783. 6:37:12have to type window dot at the front we
  7784. 6:37:15can just type document or console and
  7785. 6:37:18JavaScript will automatically add window
  7786. 6:37:21dot at the front for us so window is a
  7787. 6:37:25built-in object that represents the
  7788. 6:37:28browser we don't have to type out window
  7789. 6:37:31dot ourselves because JavaScript adds it
  7790. 6:37:33for us automatically but it's still nice
  7791. 6:37:36to know how this works behind the scenes
  7792. 6:37:39and that's the end of this lesson in
  7793. 6:37:41this lesson we learned about the
  7794. 6:37:43document object model or the Dom which
  7795. 6:37:47allows JavaScript to fully control the
  7796. 6:37:49web page we learned how to use document.
  7797. 6:37:53Query selector to get elements from the
  7798. 6:37:55web page and put them inside our
  7799. 6:37:58JavaScript we learned how to use inner
  7800. 6:38:00HTML to modify the HTML inside an
  7801. 6:38:04element we worked on three projects
  7802. 6:38:07using the Dom we learned how to make
  7803. 6:38:09elements interactive with the keyboard
  7804. 6:38:12using on key down and we learned some
  7805. 6:38:15more details about strings as well as
  7806. 6:38:18the window object which represents the
  7807. 6:38:21browser here are some exercises you can
  7808. 6:38:23do on your own to practice using the Dom
  7809. 6:38:54in this lesson we're going to add CSS to
  7810. 6:38:57all of our projects and we're going to
  7811. 6:38:59finish our rock paper scissors project
  7812. 6:39:02CSS is a different language that changes
  7813. 6:39:05the appearance of our website it's one
  7814. 6:39:08of the three languages that we use to
  7815. 6:39:10create websites so first let's look at
  7816. 6:39:13the projects we're going to work on in
  7817. 6:39:15this lesson we'll go to our browser and
  7818. 6:39:19create a new tab at the
  7819. 6:39:21top and here we're going to type
  7820. 6:39:26super simple
  7821. 6:39:29dodev SL projects slash
  7822. 6:39:33Dom Das
  7823. 6:39:36with-
  7824. 6:39:38CSS and press
  7825. 6:39:40enter so these are the same three
  7826. 6:39:43projects we created in the previous
  7827. 6:39:45lesson except this time we're going to
  7828. 6:39:48add CSS to make these projects look nice
  7829. 6:39:52let's start by creating a copy of the
  7830. 6:39:55Project's HTML file from the previous
  7831. 6:39:58lesson we'll go to our code
  7832. 6:40:01editor and click this icon to show our
  7833. 6:40:04files and then here we're going to
  7834. 6:40:07select
  7835. 6:40:0909-- projects we're going to right
  7836. 6:40:12click
  7837. 6:40:14copy and down here right click and
  7838. 6:40:20paste and let's rename this file by
  7839. 6:40:25right-clicking
  7840. 6:40:27rename at the end we're going to remove
  7841. 6:40:31copy and at the beginning
  7842. 6:40:34we're going to change it to 10 and press
  7843. 6:40:38enter now let's hide our
  7844. 6:40:40files and we're going to open 10 Dom
  7845. 6:40:43projects in the browser by
  7846. 6:40:47right-clicking open with live
  7847. 6:40:50server now we can close our previous
  7848. 6:40:55tabs as well as the previous code from
  7849. 6:40:58lesson 9 which is this file and these
  7850. 6:41:02two
  7851. 6:41:04and now we're ready to add CSS to our
  7852. 6:41:07projects we'll start with the YouTube
  7853. 6:41:09subscribe button let's go to the tab for
  7854. 6:41:12our
  7855. 6:41:13project and we're going to style this
  7856. 6:41:16button first so we reviewed CSS in
  7857. 6:41:19lesson 4 and one of the ways we can
  7858. 6:41:22write CSS code is using the style
  7859. 6:41:25element so inside the head section we're
  7860. 6:41:29going to create an element using less
  7861. 6:41:32than style greater than so the style
  7862. 6:41:37element lets us write CSS code inside so
  7863. 6:41:41inside here we're going to write
  7864. 6:41:44button open curly bracket close curly
  7865. 6:41:48bracket so the code in front of the
  7866. 6:41:50brackets is called the CSS selector this
  7867. 6:41:54tells the computer which elements we
  7868. 6:41:56want to style or change the appearance
  7869. 6:42:00so button will style all button elements
  7870. 6:42:03on the page but in this situation we
  7871. 6:42:06only want to style the Subscribe button
  7872. 6:42:09because the calculate button will be
  7873. 6:42:11styled differently so to style a
  7874. 6:42:14specific element we can use the class
  7875. 6:42:18attribute so if we look at our button
  7876. 6:42:21currently it already has a class of JS
  7877. 6:42:24subscribe button but this class is being
  7878. 6:42:28used by our JavaScript a good practice
  7879. 6:42:31is to add a different class that will be
  7880. 6:42:33used by
  7881. 6:42:34CSS so an element can have multiple
  7882. 6:42:37classes we just separate them with a
  7883. 6:42:40space so inside this class attribute
  7884. 6:42:43we're going to type space and add
  7885. 6:42:46another class
  7886. 6:42:49subcribe dash
  7887. 6:42:52button now this element has two classes
  7888. 6:42:56and we're going to use this second class
  7889. 6:42:58in our
  7890. 6:42:59CSS so to style a specific class we're
  7891. 6:43:02going to remove button and we're going
  7892. 6:43:05to type a DOT so starting with a DOT
  7893. 6:43:09means we're going to style a class
  7894. 6:43:11rather than an element name so we'll
  7895. 6:43:14style the class
  7896. 6:43:17subscribe Das
  7897. 6:43:20button which is this button right here
  7898. 6:43:24and now inside these curly brackets
  7899. 6:43:26we're going to tell the computer how to
  7900. 6:43:28style this button for example border
  7901. 6:43:33colon none and
  7902. 6:43:36semicolon if we
  7903. 6:43:39save this will remove the border from
  7904. 6:43:42the button so it no longer has a border
  7905. 6:43:45like this button so that's how we style
  7906. 6:43:47a specific element with CSS we add a
  7907. 6:43:51class to the element and then inside CSS
  7908. 6:43:55we start with a DOT and the class and
  7909. 6:43:58between the curly brackets the code on
  7910. 6:44:00the left is called the property
  7911. 6:44:03this tells a computer what we want to
  7912. 6:44:05change on the right is called the value
  7913. 6:44:09this tells a computer what we want to
  7914. 6:44:11change the property to we separate the
  7915. 6:44:14property and value with a colon and we
  7916. 6:44:17end the whole thing with a semicolon so
  7917. 6:44:20this is also called a property value
  7918. 6:44:23pair as a reminder this is just a review
  7919. 6:44:26of CSS if you want to learn CSS in more
  7920. 6:44:30detail you can check out my HTML and and
  7921. 6:44:33CSS full course in the video
  7922. 6:44:36description so we just added one style
  7923. 6:44:39to this button we're going to add more
  7924. 6:44:41Styles so that it looks like our final
  7925. 6:44:44design to make this easier I'm going to
  7926. 6:44:47rearrange my windows and I'm going to
  7927. 6:44:49put the final design at the bottom and
  7928. 6:44:52then our project at the
  7929. 6:44:55top this will make it easier to compare
  7930. 6:44:57the two
  7931. 6:44:58designs next to make it look like the
  7932. 6:45:01final design we're we're going to style
  7933. 6:45:03our element one by one let's go back to
  7934. 6:45:07our
  7935. 6:45:08code and press enter and next let's
  7936. 6:45:12change the background color to Black to
  7937. 6:45:15do that we'll type
  7938. 6:45:18background Das color colon black and
  7939. 6:45:24semicolon and
  7940. 6:45:26save now our button has a black
  7941. 6:45:29background let's keep going one by one
  7942. 6:45:32we change the text color to white next
  7943. 6:45:36so in our code we'll type color colon
  7944. 6:45:41white and semicolon and
  7945. 6:45:45save now if you're wondering how I know
  7946. 6:45:48all these properties you can usually
  7947. 6:45:50find them by searching in Google just
  7948. 6:45:52make sure you search for what you are
  7949. 6:45:54trying to
  7950. 6:45:56do let's keep going one by one the next
  7951. 6:46:00thing we'll change is we're going to add
  7952. 6:46:02spacing on the inside of the button in
  7953. 6:46:06CSS space on the inside of an element is
  7954. 6:46:09called padding so we'll create a new
  7955. 6:46:13line and we'll use the property
  7956. 6:46:16padding Das toop colon
  7957. 6:46:2210px and
  7958. 6:46:24save so this will add space on the
  7959. 6:46:27inside of the element called the padding
  7960. 6:46:30at the top of 10 PX
  7961. 6:46:33so the PX here means pixels a pixel is a
  7962. 6:46:38unit of measurement in computers your
  7963. 6:46:40screen is made up of many small little
  7964. 6:46:43squares each of these squares is called
  7965. 6:46:45a pixel so a pixel is the smallest thing
  7966. 6:46:49that you can have on your screen screen
  7967. 6:46:51sizes are measured in pixels images and
  7968. 6:46:54video sizes are also measured in pixels
  7969. 6:46:58and don't worry the more you work with
  7970. 6:47:00pixels the more you'll get used to how
  7971. 6:47:02big a pixel is so in this example 10
  7972. 6:47:06pixels is equal to this amount of space
  7973. 6:47:10and it looks pretty close to the final
  7974. 6:47:11design so let's keep going we'll add
  7975. 6:47:15space on the inside of the element at
  7976. 6:47:17the bottom this time so we'll create a
  7977. 6:47:20new line and type padding Dash bottom
  7978. 6:47:27colon
  7979. 6:47:2810px again and Save
  7980. 6:47:33and that adds space at the bottom of the
  7981. 6:47:35element of 10
  7982. 6:47:37pixels next let's increase the space on
  7983. 6:47:40the left and the right we'll go into our
  7984. 6:47:44code and add padding Das left colon 10
  7985. 6:47:52pixels and padding Das right colon 10
  7986. 6:47:59pixels let's save
  7987. 6:48:03and it added some extra space on the
  7988. 6:48:05left and right but I think our final
  7989. 6:48:07design has a little more space on the
  7990. 6:48:10left and right so let's go back and
  7991. 6:48:12increase our padding so instead of 10
  7992. 6:48:16let's try 15 on both
  7993. 6:48:19sides so we'll
  7994. 6:48:21save and now the spacing looks close
  7995. 6:48:24enough to the final
  7996. 6:48:26design so in CSS it's normal to do some
  7997. 6:48:29trial and error to get the styling right
  7998. 6:48:33let's keep styling this one by one next
  7999. 6:48:36we'll make the text thicker or bold so
  8000. 6:48:40let's go into our code and to change the
  8001. 6:48:43thickness of the text we'll use the
  8002. 6:48:46property font Das weight colon
  8003. 6:48:53bold let's
  8004. 6:48:55save and that will make our text bold so
  8005. 6:48:58we're almost there now let's make the
  8006. 6:49:01corners rounded look like in the final
  8007. 6:49:03design we'll go back to our CSS and the
  8008. 6:49:07property for making the corners round is
  8009. 6:49:09called
  8010. 6:49:11border Das
  8011. 6:49:13radius and we'll make it colon 5
  8012. 6:49:18pixels let's
  8013. 6:49:20save and you can see that it made our
  8014. 6:49:22Corners round so the pixels here tells
  8015. 6:49:26the computer how round we want the
  8016. 6:49:28corners to be the higher the pixels the
  8017. 6:49:31more round it will get
  8018. 6:49:33so if we change this to 10 pixels and
  8019. 6:49:37save it will make the corners even more
  8020. 6:49:40round so let's change this to something
  8021. 6:49:43really big to make it completely round
  8022. 6:49:46we'll change this to 50 pixels and
  8023. 6:49:51save and now our button is completely
  8024. 6:49:54round the last style we're going to add
  8025. 6:49:57is in the final design if we put our
  8026. 6:49:59Mouse over the button it turns into this
  8027. 6:50:02hand the icon so we call this a pointer
  8028. 6:50:06and we're going to do the same thing in
  8029. 6:50:07our project so let's go to our code and
  8030. 6:50:11to create that pointer icon we're going
  8031. 6:50:14to use the property
  8032. 6:50:17cursor colon
  8033. 6:50:20pointer and let's
  8034. 6:50:23save and put our Mouse over the button
  8035. 6:50:26and you'll notice it turns into this
  8036. 6:50:28hand pointer icon and that's it now our
  8037. 6:50:31subscribe button looks like the final
  8038. 6:50:34design next let's click the Subscribe
  8039. 6:50:37button you'll notice that it only
  8040. 6:50:39changes the text but if we click the
  8041. 6:50:42final
  8042. 6:50:43design it also changes the background
  8043. 6:50:46color and the text color so we're going
  8044. 6:50:49to learn how to change the CSS when we
  8045. 6:50:52click a
  8046. 6:50:53button let's go back to our code and
  8047. 6:50:56this time we're going to scroll down to
  8048. 6:50:58our
  8049. 6:50:59JavaScript which is here so we learned
  8050. 6:51:02in the previous lesson we can get HTML
  8051. 6:51:05elements from the page and put them
  8052. 6:51:08inside our JavaScript and we also
  8053. 6:51:10learned that these HTML elements are
  8054. 6:51:13just JavaScript objects and they can
  8055. 6:51:15have properties like inner text and
  8056. 6:51:19inner
  8057. 6:51:20HTML every element has another property
  8058. 6:51:24we can use called class list class list
  8059. 6:51:27gives us control of the class attribute
  8060. 6:51:31so one solution we can do is when we
  8061. 6:51:33click the button we can add a class and
  8062. 6:51:37then this class will have different
  8063. 6:51:39styles so let's see how we do that in
  8064. 6:51:42our code we'll scroll down to the
  8065. 6:51:44Subscribe function and when we click the
  8066. 6:51:47button and make the button subscribed
  8067. 6:51:50we're also going to add a class so let's
  8068. 6:51:53create a new line and we'll type button
  8069. 6:51:59element dot class capital L
  8070. 6:52:05list so class list itself is an object
  8071. 6:52:09which means it has properties and
  8072. 6:52:10methods we can use one method is called
  8073. 6:52:15add which adds a class to an element so
  8074. 6:52:19after this let's type dot add and
  8075. 6:52:25brackets and now inside these brackets
  8076. 6:52:27we're going to put a string so this
  8077. 6:52:30string is a class that we want want to
  8078. 6:52:32add when we click this button let's add
  8079. 6:52:36a class called
  8080. 6:52:38is-
  8081. 6:52:41subscribed and
  8082. 6:52:43save so now every time we click this
  8083. 6:52:47button it will add a class to the button
  8084. 6:52:50called is- subscribed let's give it a
  8085. 6:52:54try first we'll rightclick on the page
  8086. 6:52:58and then click
  8087. 6:53:00inspect and we'll go to the elements Tab
  8088. 6:53:04and I'll rearrange my windows so you can
  8089. 6:53:06see
  8090. 6:53:07it and then inside the HTML pay
  8091. 6:53:10attention to the class of the button now
  8092. 6:53:14when we click the
  8093. 6:53:15button it's going to add an extra class
  8094. 6:53:18called is- subscribed so that's what
  8095. 6:53:22this line of code does and now that this
  8096. 6:53:25button has the is subscribed class we
  8097. 6:53:28can style it in our CSS so let's go back
  8098. 6:53:31to our code
  8099. 6:53:33and we'll scroll up to our
  8100. 6:53:35CSS and here we'll type dot to style a
  8101. 6:53:40class and we'll style the class is-
  8102. 6:53:44subscribed
  8103. 6:53:46is-
  8104. 6:53:48subscribed and curly
  8105. 6:53:50brackets and now inside these curly
  8106. 6:53:53brackets let's change the background
  8107. 6:53:55color to Gray and the text color to
  8108. 6:53:59Black
  8109. 6:54:00Background Das color colon
  8110. 6:54:05gray and then color colon
  8111. 6:54:10black and make sure to type these Styles
  8112. 6:54:13below the Subscribe button Styles so
  8113. 6:54:16that these Styles will override the
  8114. 6:54:18Styles
  8115. 6:54:19above now if we
  8116. 6:54:22save and then click our
  8117. 6:54:25button it will add the is subscrib class
  8118. 6:54:29and now this button will get our new
  8119. 6:54:31styles that we added here so that's one
  8120. 6:54:35way to change the CSS when we click a
  8121. 6:54:38button all right now I'm going to move
  8122. 6:54:41this back to the
  8123. 6:54:44top and notice that our background color
  8124. 6:54:47is different from the final design the
  8125. 6:54:50final design uses a much lighter gray so
  8126. 6:54:53how do we get this exact color to do
  8127. 6:54:56that we're going to open the elements
  8128. 6:54:59tab again so on this page we're going to
  8129. 6:55:03rightclick click
  8130. 6:55:06inspect and then click the elements
  8131. 6:55:09Tab and I'll rearrange my windows so we
  8132. 6:55:12can see
  8133. 6:55:15it so in this section in the style
  8134. 6:55:18section we can see the CSS that the
  8135. 6:55:21element has so first let's actually find
  8136. 6:55:24this button which is right here and
  8137. 6:55:27we'll click
  8138. 6:55:29it and here it will show us the CSS that
  8139. 6:55:33this button has and it will show us the
  8140. 6:55:36background color that we should use so
  8141. 6:55:38let's make a copy of this color and put
  8142. 6:55:41it into our code
  8143. 6:55:44rightclick
  8144. 6:55:46copy and here we'll remove it and
  8145. 6:55:50rightclick and
  8146. 6:55:53paste now let's
  8147. 6:55:55save and go back to our project and try
  8148. 6:55:59it
  8149. 6:56:00out and now we get get the exact same
  8150. 6:56:03color as the final design so by the way
  8151. 6:56:07this color is known as an RGB value RGB
  8152. 6:56:11means red green and blue and computers
  8153. 6:56:16can create any color using a combination
  8154. 6:56:19of red green and blue so the first
  8155. 6:56:23number here is how much red we want in
  8156. 6:56:26our color the second number is how much
  8157. 6:56:28green we want and the third number is
  8158. 6:56:31how much blue we want
  8159. 6:56:33each number goes from 0 to
  8160. 6:56:35255 Zer means less color and darker 255
  8161. 6:56:41means more color and lighter so this
  8162. 6:56:44color here uses 240 for all of the
  8163. 6:56:47colors so it's close to 255 and will
  8164. 6:56:51give us a very light gray color that you
  8165. 6:56:54see
  8166. 6:56:56here next if we go back to the final
  8167. 6:56:59design and we click our button again
  8168. 6:57:02it will change back to the original
  8169. 6:57:05style so we're going to create this
  8170. 6:57:07feature in our project as well so one
  8171. 6:57:10way to do this is when we click the
  8172. 6:57:12button again we can remove this class so
  8173. 6:57:17let's scroll down to the
  8174. 6:57:19bottom to the Subscribe function and
  8175. 6:57:23when we click the button and we change
  8176. 6:57:25it back to subscribe we can remove this
  8177. 6:57:28class to do that class list has a
  8178. 6:57:32another method we can use called remove
  8179. 6:57:35so let's create a new line and we'll
  8180. 6:57:38type
  8181. 6:57:40button element do class capital L
  8182. 6:57:46list dot
  8183. 6:57:48remove and
  8184. 6:57:51brackets so this does the opposite of
  8185. 6:57:54add it will remove a class from this
  8186. 6:57:58element so inside the brackets we'll
  8187. 6:58:01type a string and the class we want to
  8188. 6:58:03remove which is
  8189. 6:58:06is-
  8190. 6:58:09subscribed and
  8191. 6:58:12save now let's try out our project we'll
  8192. 6:58:15click the button it will get the class
  8193. 6:58:18is subscribed and get the extra Styles
  8194. 6:58:21and now when we click the button again
  8195. 6:58:24it will remove the class and it will
  8196. 6:58:26remove the extra
  8197. 6:58:29Styles so notice here that when we add a
  8198. 6:58:33class we just type the class name we
  8199. 6:58:36don't type a DOT and the class name like
  8200. 6:58:39we did for query selector typing a DOT
  8201. 6:58:43only makes sense in query
  8202. 6:58:45selector next we're going to style the
  8203. 6:58:48text on the page let's scroll up to our
  8204. 6:58:52CSS and one thing we can do is to style
  8205. 6:58:55all the paragraph elements on the page
  8206. 6:58:58using this code P and curly brackets
  8207. 6:59:04however we're going to use a better
  8208. 6:59:06solution instead of styling all the
  8209. 6:59:08paragraphs we're just going to style the
  8210. 6:59:11body
  8211. 6:59:12element when we change the text in the
  8212. 6:59:15body element it will change the text in
  8213. 6:59:18the entire page so inside these curly
  8214. 6:59:22brackets let's change the text here so
  8215. 6:59:25we're going to use a different style of
  8216. 6:59:27text this is called a font to change the
  8217. 6:59:31font font we're going to use this
  8218. 6:59:33property font Das
  8219. 6:59:37family colon and we'll change it to
  8220. 6:59:42aial which is what I used in the final
  8221. 6:59:45design so let's
  8222. 6:59:47save and this will style all the text
  8223. 6:59:51inside the body and change the font to
  8224. 6:59:55aial and a good practice is to write any
  8225. 6:59:58styles for the body at the top so if we
  8226. 7:00:01need to we can override it later so
  8227. 7:00:04let's move these to the top we'll right
  8228. 7:00:07click
  8229. 7:00:09cut and then up
  8230. 7:00:12here create some new lines and right
  8231. 7:00:15click and
  8232. 7:00:18paste and we're going to add one more
  8233. 7:00:20style before moving on let's add some
  8234. 7:00:24space between the Subscribe button and
  8235. 7:00:26the second project so earlier we learned
  8236. 7:00:30that space on the inside of it element
  8237. 7:00:32is called padding space on the outside
  8238. 7:00:36of an element is called margin so inside
  8239. 7:00:40our subscribe button at the
  8240. 7:00:43bottom let's add a property called
  8241. 7:00:46margin Das bottom colon 30 pixels and
  8242. 7:00:54save so this will add space on the
  8243. 7:00:58outside of the element at the bottom of
  8244. 7:01:0030 pixels
  8245. 7:01:03now let's move on and style the Amazon
  8246. 7:01:05shipping calculator with
  8247. 7:01:07CSS again we'll add a class to these
  8248. 7:01:10elements and then style them in our
  8249. 7:01:13CSS let's scroll down to the text box
  8250. 7:01:17which is the input element and we're
  8251. 7:01:20going to add another class by typing
  8252. 7:01:23space and let's use the class cost Das
  8253. 7:01:29input and now we're going to style this
  8254. 7:01:32in CSS so let's scroll up and here we'll
  8255. 7:01:36style the class using dot cost Das
  8256. 7:01:42input and curly
  8257. 7:01:44brackets and inside we'll style this one
  8258. 7:01:47by one let's look at our final design
  8259. 7:01:51and see what styles we need so first
  8260. 7:01:54let's change the text size because our
  8261. 7:01:57text is a little smaller than the text
  8262. 7:01:59here so the change the text size
  8263. 7:02:02we're going to use Font Das size colon
  8264. 7:02:08and let's try 15 pixels and
  8265. 7:02:13save and now our text looks close enough
  8266. 7:02:16to the final
  8267. 7:02:18design next let's add some space inside
  8268. 7:02:21the text box so remember space inside an
  8269. 7:02:25element is called padding so we're going
  8270. 7:02:28to type
  8271. 7:02:30padding Das
  8272. 7:02:32top of 10
  8273. 7:02:35pixels padding Das bottom also 10
  8274. 7:02:41pixels and let's add padding to the
  8275. 7:02:45left 10
  8276. 7:02:47pixels and padding
  8277. 7:02:51dasr of 10 pixels so this will add a
  8278. 7:02:57space on the inside of our text box in
  8279. 7:03:00every direction if we
  8280. 7:03:03save now it looks just like the final
  8281. 7:03:07design now I'm going to show you a
  8282. 7:03:09shortcut for padding that will save us
  8283. 7:03:11some typing so if we create a new line
  8284. 7:03:15we can actually use a property called
  8285. 7:03:19padding and colon and then type 10
  8286. 7:03:24pixels this padding property let us add
  8287. 7:03:27padding on multiple sides at once if we
  8288. 7:03:31type one value here like 10 pixels it
  8289. 7:03:34will add 10 pixels to every side so this
  8290. 7:03:37line does the same thing as these four
  8291. 7:03:40lines above so let's actually comment
  8292. 7:03:43this out using
  8293. 7:03:46slashstar and star slash and
  8294. 7:03:51save and you can see that this one line
  8295. 7:03:53of CSS did the same thing as the other
  8296. 7:03:57four lines of CSS so this shortcut can
  8297. 7:04:00save us some typing
  8298. 7:04:02next let's move on and style the
  8299. 7:04:05calculate
  8300. 7:04:06button again we'll add a class to this
  8301. 7:04:09button so we can style it so in this
  8302. 7:04:12button let's add a class
  8303. 7:04:15attribute class equals
  8304. 7:04:20calculate Das
  8305. 7:04:23button now let's style this in our CSS
  8306. 7:04:26by scrolling up and here we'll type Dot
  8307. 7:04:32calculate dash
  8308. 7:04:35button and curly
  8309. 7:04:37brackets and inside these curly brackets
  8310. 7:04:40we're going to style this button one by
  8311. 7:04:43one so let's start with the background
  8312. 7:04:45color so we'll type
  8313. 7:04:49background Das color colon green and
  8314. 7:04:55semicolon and
  8315. 7:04:57save next let's change the text color to
  8316. 7:05:01white
  8317. 7:05:02so we'll type color colon
  8318. 7:05:06white and save
  8319. 7:05:09again okay let's keep going and we'll
  8320. 7:05:12remove the border around this button so
  8321. 7:05:15we'll type
  8322. 7:05:17border colon
  8323. 7:05:20none
  8324. 7:05:22save next let's make our text a little
  8325. 7:05:25bigger like in the final
  8326. 7:05:27design so we'll type font Das
  8327. 7:05:32size and make it 15
  8328. 7:05:35pixels and
  8329. 7:05:38save okay so now let's add the space on
  8330. 7:05:42the inside of the button so we're going
  8331. 7:05:44to use this shortcut again we'll type
  8332. 7:05:48the padding
  8333. 7:05:50property colon and this time we're going
  8334. 7:05:53to give it two values separated by
  8335. 7:05:56spaces 10
  8336. 7:05:58pixels and maybe 50 pixels so it's
  8337. 7:06:03easier to see what this does so when we
  8338. 7:06:06give two values to padding the first
  8339. 7:06:09value is the vertical padding so the top
  8340. 7:06:12and bottom the second value is the
  8341. 7:06:15horizontal padding or left and right if
  8342. 7:06:19we
  8343. 7:06:21save so you can see that the left and
  8344. 7:06:23right has a lot of padding which is 50
  8345. 7:06:27pixels so now let's make this match our
  8346. 7:06:29final design so instead of 50 pixels
  8347. 7:06:33let's bring it down to 15 pixels and
  8348. 7:06:38save okay so that looks pretty close
  8349. 7:06:41let's also increase the padding on the
  8350. 7:06:43top and bottom so that the text box and
  8351. 7:06:46the button line up with each other like
  8352. 7:06:48in the final
  8353. 7:06:51project so let's change the vertical
  8354. 7:06:53padding to maybe 12 pixels and
  8355. 7:06:58save the last thing we'll style for this
  8356. 7:07:01project is when we put our Mouse over
  8357. 7:07:03the button it turns into the pointer
  8358. 7:07:05icon so to create this we're going to
  8359. 7:07:08use the property
  8360. 7:07:11cursor colon
  8361. 7:07:15pointer and save and give it a
  8362. 7:07:19try and that's it we just finished
  8363. 7:07:22styling the Amazon shipping calculator
  8364. 7:07:25so now let's give this a try we'll Type
  8365. 7:07:2825 in the text box and press calculate
  8366. 7:07:32and at the bottom it will add $10 of
  8367. 7:07:35shipping and show
  8368. 7:07:38$35 finally we're going to style the
  8369. 7:07:41last project in this lesson which is the
  8370. 7:07:43rock paper scissors project and this
  8371. 7:07:46will finish the project let's go back to
  8372. 7:07:49superimple dodev and we'll scroll down
  8373. 7:07:52on this page and then click this link to
  8374. 7:07:55show the final design for rock paper
  8375. 7:07:58scissors so I'll rearrange my windows so
  8376. 7:08:01we can see
  8377. 7:08:03this and we're going to change the
  8378. 7:08:05buttons to these nice big icons for each
  8379. 7:08:08of the moves and when we click a move it
  8380. 7:08:11will show the result in bold and show an
  8381. 7:08:15icon of the move that we picked and the
  8382. 7:08:17move that the computer picked so we're
  8383. 7:08:20going to make this project look really
  8384. 7:08:22nice now let's get started I'm going to
  8385. 7:08:25rearrange my windows back so we have it
  8386. 7:08:28at the bottom and let's create a cop
  8387. 7:08:31copy of the rock paper scissors project
  8388. 7:08:33for this lesson we'll go to our code
  8389. 7:08:37editor and click here to show our
  8390. 7:08:40files and we're going to make a copy of
  8391. 7:08:4409- Ro paper scissors so let's click
  8392. 7:08:47this file and then
  8393. 7:08:50rightclick
  8394. 7:08:52copy and down here right click and
  8395. 7:08:57paste and we'll rename this file by
  8396. 7:09:00right-clicking
  8397. 7:09:03rename and at the end we'll remove copy
  8398. 7:09:07and at the beginning we'll change it
  8399. 7:09:09from 9 to 10 and press
  8400. 7:09:14enter now let's click here to hide the
  8401. 7:09:18files and we can close the 09
  8402. 7:09:21file and let's open the 10 rock paper
  8403. 7:09:25scissors file in the browser so we'll
  8404. 7:09:28right click and then open with live
  8405. 7:09:33server and I'll move this tab to the top
  8406. 7:09:37here so we can see it side by side so
  8407. 7:09:41let's start by styling the body we want
  8408. 7:09:44to make the background color of the
  8409. 7:09:46entire page this dark gray color we'll
  8410. 7:09:50go into our code and let's create a
  8411. 7:09:53style element so we can write some
  8412. 7:09:56CSS inside the head let's type less than
  8413. 7:10:00Style
  8414. 7:10:01greater than and then inside here we're
  8415. 7:10:05going to style the body so we'll type
  8416. 7:10:08body and curly
  8417. 7:10:11brackets now inside here we're going to
  8418. 7:10:13change the background color so
  8419. 7:10:19background-color colon and we're going
  8420. 7:10:22to get the color that we see here by
  8421. 7:10:25right clicking click
  8422. 7:10:28inspect and then go to the elements tab
  8423. 7:10:32Tab and I'll rearrange it so we can see
  8424. 7:10:36it and then here we'll find the body
  8425. 7:10:39element and click it and in this style
  8426. 7:10:43section we can find the background color
  8427. 7:10:46that we need so let's select
  8428. 7:10:49this and
  8429. 7:10:51rightclick
  8430. 7:10:53copy and in
  8431. 7:10:55here right click and
  8432. 7:10:59paste and a semi colon at the end let's
  8433. 7:11:04save and I'll move this back to the
  8434. 7:11:08corner and now our project has the same
  8435. 7:11:11background color as the final design
  8436. 7:11:14next let's make the text color white so
  8437. 7:11:18in our CSS we'll use the color property
  8438. 7:11:23and colon white and semicolon and save
  8439. 7:11:29and that will make our text White
  8440. 7:11:32next let's change the style or the font
  8441. 7:11:35of this text we'll type font
  8442. 7:11:40dasam colon
  8443. 7:11:43aial which is what the final design uses
  8444. 7:11:47and
  8445. 7:11:48save all right now let's style these
  8446. 7:11:51elements one by one to match the final
  8447. 7:11:54design we'll start with this paragraph
  8448. 7:11:57at the top so inside the paragraph
  8449. 7:12:00element
  8450. 7:12:01let's add a class so we can style it in
  8451. 7:12:05CSS we'll add class
  8452. 7:12:08equals let's call this the
  8453. 7:12:11title and then in our CSS we can style
  8454. 7:12:15this class by starting with a DOT and
  8455. 7:12:18then
  8456. 7:12:19title and curly
  8457. 7:12:22brackets so let's make our title really
  8458. 7:12:25big like the final design we'll change
  8459. 7:12:29the font Das size colon 30 pixels and
  8460. 7:12:37save so that looks close enough next
  8461. 7:12:40let's make the text bold in our CSS
  8462. 7:12:44we'll use Font D weight colon bold and
  8463. 7:12:52save now let's move on to installing the
  8464. 7:12:56buttons so first we're going to need
  8465. 7:12:58these images in our project and I'll
  8466. 7:13:01show you how to get them we'll put our
  8467. 7:13:04Mouse over the rock image and we'll
  8468. 7:13:07rightclick and we'll click save
  8469. 7:13:10image and now we're going to find the
  8470. 7:13:13folder that contains all of our code so
  8471. 7:13:16for me it's on the
  8472. 7:13:18desktop and it's this folder right here
  8473. 7:13:21so I'll double click this folder and
  8474. 7:13:23then leave the file name as the default
  8475. 7:13:26rock- emoji and click save
  8476. 7:13:31and now in our code editor if we click
  8477. 7:13:34here we should be able to see the rock
  8478. 7:13:37image let's do the same with the paper
  8479. 7:13:40and scissors image so we'll hover over
  8480. 7:13:43it rightclick save
  8481. 7:13:47image and in the same folder that
  8482. 7:13:49contains our code we're going to save
  8483. 7:13:52the paper image and finally rightclick
  8484. 7:13:56save image and we'll save the scissors
  8485. 7:13:59image
  8486. 7:14:02all right now that we have these images
  8487. 7:14:04in our code editor let's learn how to
  8488. 7:14:06load them onto the page so we can do
  8489. 7:14:10this using an HTML element so we'll go
  8490. 7:14:13back to our HTML code and we'll scroll
  8491. 7:14:17down and inside the button we're going
  8492. 7:14:21to add an image element so let's type
  8493. 7:14:25less than IMG and greater than so this
  8494. 7:14:30is the image HTML element it loads an
  8495. 7:14:33image onto the page the image element
  8496. 7:14:36doesn't require a closing tag this is
  8497. 7:14:39called a void element now to load an
  8498. 7:14:42image we're going to add an attribute to
  8499. 7:14:45this element called
  8500. 7:14:49SRC so SRC stands for Source it tells a
  8501. 7:14:53computer which file or image to load so
  8502. 7:14:57let's look in our
  8503. 7:14:59project for this button we're going to
  8504. 7:15:01load rock- emoji.png
  8505. 7:15:05inside the source attribute we'll type
  8506. 7:15:10rock-
  8507. 7:15:12emoji.png
  8508. 7:15:14so what this does is it will look for
  8509. 7:15:17the file with this name beside the HTML
  8510. 7:15:21file and it finds it right here and it
  8511. 7:15:24will load it onto the page if we
  8512. 7:15:28save now inside our button it loaded the
  8513. 7:15:31rock
  8514. 7:15:32image right now this image is too big so
  8515. 7:15:36let's use CSS to decrease the size of
  8516. 7:15:39this image we'll hide the files and
  8517. 7:15:43let's give a class attribute to the
  8518. 7:15:45image element so we can style it so
  8519. 7:15:48we'll type class equals and let's call
  8520. 7:15:52this a move Das
  8521. 7:15:56icon and I'm also just going to add some
  8522. 7:15:59new lines to make the code a little
  8523. 7:16:01easier to read so we'll scroll up to the
  8524. 7:16:06CSS and we're going to style this class
  8525. 7:16:09using dot move Dash icon and curly
  8526. 7:16:16brackets and let's decrease the height
  8527. 7:16:19of this image we can use the property
  8528. 7:16:23height colon and a pixel value 50 pixels
  8529. 7:16:29and Save
  8530. 7:16:32so this will make the height of this
  8531. 7:16:34image 50 pixels and it will also resize
  8532. 7:16:38the width automatically so that the
  8533. 7:16:40image keeps its shape so this size looks
  8534. 7:16:43pretty close to the final design so we
  8535. 7:16:46can move
  8536. 7:16:47on another feature of the source
  8537. 7:16:50attribute is that we can put our files
  8538. 7:16:53into folders so let's open the files by
  8539. 7:16:56clicking
  8540. 7:16:58here and let's say that we want to
  8541. 7:17:00organize all of our images into a folder
  8542. 7:17:04so we're going to create a folder by
  8543. 7:17:06clicking this
  8544. 7:17:07icon and let's name this folder
  8545. 7:17:13images and we're going to drag these
  8546. 7:17:15images into this
  8547. 7:17:18folder and drag this in
  8548. 7:17:21here and in
  8549. 7:17:25here now unfortunately you can see that
  8550. 7:17:27the image doesn't load anymore and
  8551. 7:17:30that's because if we look at our code
  8552. 7:17:33Source tells a computer to look for this
  8553. 7:17:36file beside this HTML
  8554. 7:17:40file and right now there is no file
  8555. 7:17:43called rock- emoji.png because we put it
  8556. 7:17:47in a folder so to tell the computer to
  8557. 7:17:50look inside this folder we're going to
  8558. 7:17:52type the folder name at the start which
  8559. 7:17:55is
  8560. 7:17:57images and then we're going to type a
  8561. 7:17:59slash to go into the images folder and
  8562. 7:18:03then look for this file which is right
  8563. 7:18:06here if we
  8564. 7:18:08save the computer is now able to find
  8565. 7:18:12this image and load it onto the web page
  8566. 7:18:15so this feature is called file paaths we
  8567. 7:18:18can tell the computer to look for a
  8568. 7:18:20folder beside the HTML file like images
  8569. 7:18:24and then use a slash to tell it to go
  8570. 7:18:27into the folder and look for a file
  8571. 7:18:32now let's use this code to load the
  8572. 7:18:34other images onto our page so for the
  8573. 7:18:38paper image we're going to go to this
  8574. 7:18:40button remove the text and then create
  8575. 7:18:45an image element less than IMG greater
  8576. 7:18:49than and we'll give it an attribute
  8577. 7:18:52Source equals and we'll locate the image
  8578. 7:18:56it is in the images folder so we'll type
  8579. 7:19:01images and then slash to go into the
  8580. 7:19:04folder and we'll load the file paper-
  8581. 7:19:08emoji.png
  8582. 7:19:10so
  8583. 7:19:12paper-
  8584. 7:19:14emoji.png
  8585. 7:19:16and
  8586. 7:19:18save so our image is really big let's
  8587. 7:19:22give it the same class that we gave the
  8588. 7:19:24rock image so we'll add a class equals
  8589. 7:19:30move Dash icon and
  8590. 7:19:35save so these Styles get applied to all
  8591. 7:19:38elements with the class move- icon so it
  8592. 7:19:42applies to both of these
  8593. 7:19:45elements now let's do the same thing for
  8594. 7:19:47the scissors button we'll remove the
  8595. 7:19:50text and we'll replace it with an image
  8596. 7:19:55element and we'll give it the source
  8597. 7:19:59attribute images
  8598. 7:20:02slash
  8599. 7:20:03scissors Das
  8600. 7:20:06emoji.png
  8601. 7:20:08and we'll also give this a
  8602. 7:20:12class of
  8603. 7:20:14move- icon to resize the images like up
  8604. 7:20:18here let's
  8605. 7:20:20save and now we have all the images that
  8606. 7:20:23we
  8607. 7:20:25need now let's make these buttons look
  8608. 7:20:28like the final design we we scroll
  8609. 7:20:31up and let's add a class to these
  8610. 7:20:34buttons so we can style them with
  8611. 7:20:37CSS so we'll add class equals and let's
  8612. 7:20:42use
  8613. 7:20:44move-
  8614. 7:20:46button and for this button We'll add the
  8615. 7:20:49same class class equals
  8616. 7:20:53move-
  8617. 7:20:55Button as well as for the third
  8618. 7:20:58button class = move-
  8619. 7:21:03button now let's scroll up to our CSS
  8620. 7:21:07and we're going to style this class
  8621. 7:21:10using move- button and curly
  8622. 7:21:16brackets so first let's change the
  8623. 7:21:19background color to match the background
  8624. 7:21:21color of the page so we'll use
  8625. 7:21:25background Das color colon and we'll use
  8626. 7:21:30a special value called
  8627. 7:21:33transparent and this will make the
  8628. 7:21:35button see- through if we
  8629. 7:21:38save now these buttons are
  8630. 7:21:41see-through next let's add this white
  8631. 7:21:44border to our buttons so we'll use the
  8632. 7:21:48Border
  8633. 7:21:49property colon and we're going to give
  8634. 7:21:52it three values the first value is our
  8635. 7:21:55border width or how thick the border is
  8636. 7:21:59let's use one pixel for now the second
  8637. 7:22:03value is the Border style and we'll use
  8638. 7:22:07solid because it's a solid border other
  8639. 7:22:11values we can use here are dotted or
  8640. 7:22:14dashed and the third value is the Border
  8641. 7:22:18color so we're going to use
  8642. 7:22:21white and semicolon at the end and
  8643. 7:22:26save and that will add a border so let's
  8644. 7:22:29make our border a little thicker like
  8645. 7:22:31the final design we'll change this first
  8646. 7:22:34value which is the Border width to three
  8647. 7:22:37pixels and
  8648. 7:22:40save okay so that looks pretty close now
  8649. 7:22:43let's change the size of our buttons
  8650. 7:22:46usually we use the padding to add space
  8651. 7:22:49on the inside of an element but in this
  8652. 7:22:52case our images have different sizes so
  8653. 7:22:55we're going to use width and height to
  8654. 7:22:57set an exact size so so in our CSS we'll
  8655. 7:23:02type
  8656. 7:23:04WID colon and let's make it 100
  8657. 7:23:08pixels and
  8658. 7:23:11height and also 100 pixels and
  8659. 7:23:16save so it looks like our final design
  8660. 7:23:19is a little bigger than what we have
  8661. 7:23:21here so let's increase the width and the
  8662. 7:23:24height so let's try 120 pixels and Save
  8663. 7:23:30all right so that looks pretty close now
  8664. 7:23:33let's make these buttons round and the
  8665. 7:23:36property for that is called border Das
  8666. 7:23:41radius
  8667. 7:23:43colon and to know how much we need to
  8668. 7:23:45make it round we can set it to half of
  8669. 7:23:49the width or the height so we'll set it
  8670. 7:23:52to 60
  8671. 7:23:54pixels and
  8672. 7:23:56save okay so it's coming along really
  8673. 7:23:58well and lastly we're going to add some
  8674. 7:24:01space between the buttons so remember
  8675. 7:24:05that space outside of an element is
  8676. 7:24:07called margin so let's add some margin
  8677. 7:24:11on the right so we'll
  8678. 7:24:13type
  8679. 7:24:15margin
  8680. 7:24:17dasr colon and maybe 10 pixels and
  8681. 7:24:24save and finally when we put our Mouse
  8682. 7:24:27over the button it turns into a pointer
  8683. 7:24:30so the property for that is cursor colon
  8684. 7:24:37pointer and save and now our buttons
  8685. 7:24:42match the final design so let's keep
  8686. 7:24:44moving and style the next part of the
  8687. 7:24:47project which is the result and the
  8688. 7:24:50moves if you don't see this here you can
  8689. 7:24:53just play the game and it'll give us a
  8690. 7:24:55new
  8691. 7:24:56result so to make this easier to style
  8692. 7:24:59we're going to go into our code we're
  8693. 7:25:01going to find the result which is here
  8694. 7:25:04and we're going to add some fake results
  8695. 7:25:07like you
  8696. 7:25:10win let's save and now we have this
  8697. 7:25:13result and we're going to style it so
  8698. 7:25:16let's give this a class of
  8699. 7:25:20result and then in our
  8700. 7:25:23CSS we're going to style the class dot
  8701. 7:25:27result and curly brackets
  8702. 7:25:31so let's make the text bigger we're
  8703. 7:25:33going to use Font Das size colon and
  8704. 7:25:38maybe 25 pixels and
  8705. 7:25:42save okay that looks pretty good let's
  8706. 7:25:45make the text bold so we use Font Das
  8707. 7:25:50weight colon
  8708. 7:25:53bold and
  8709. 7:25:55save and finally we're going to add
  8710. 7:25:58extra spaces between this and the
  8711. 7:26:02buttons so we'll add
  8712. 7:26:07margin-top and maybe 50 pixels and
  8713. 7:26:13save okay so I think that looks close
  8714. 7:26:16enough now let's style the moves and
  8715. 7:26:19again we'll scroll
  8716. 7:26:21down and we're going to add some fake
  8717. 7:26:24moves in here so it's easier to style so
  8718. 7:26:27we'll Type U
  8719. 7:26:30and then an image so let's use the less
  8720. 7:26:34than image and the source
  8721. 7:26:38attribute and let's just load the rock
  8722. 7:26:41image and the scissors image for the
  8723. 7:26:44computer so to load the rock image we'll
  8724. 7:26:47look inside the images folder slash to
  8725. 7:26:50go into the folder and then rock-
  8726. 7:26:54emoji.png
  8727. 7:26:56so
  8728. 7:26:58rock- emoji
  8729. 7:27:01PNG and greater than and we'll add
  8730. 7:27:05another image less than image source
  8731. 7:27:10equals Images slash the scissors
  8732. 7:27:15Emoji scissors Das
  8733. 7:27:19emoji.png
  8734. 7:27:21and greater than and finally the text
  8735. 7:27:26computer
  8736. 7:27:27computer let's save
  8737. 7:27:31and this will create some results
  8738. 7:27:33although the images are really big so we
  8739. 7:27:36can use the class move icon to resize
  8740. 7:27:39them to be the same size as the buttons
  8741. 7:27:43so for both of these images let's add
  8742. 7:27:46the
  8743. 7:27:47class
  8744. 7:27:48move-
  8745. 7:27:50icon and here as well class equals
  8746. 7:27:55move- icon and Save
  8747. 7:28:00and now our images look
  8748. 7:28:02good next let's style this score section
  8749. 7:28:06so the only thing we need here is to add
  8750. 7:28:09spaces between these two so in this
  8751. 7:28:12score element let's add a class of
  8752. 7:28:18score and we'll scroll up and style this
  8753. 7:28:22element using DOT score
  8754. 7:28:26brackets and let's add some margin at
  8755. 7:28:29the top
  8756. 7:28:31so
  8757. 7:28:33margin-top colon 50 pixels and
  8758. 7:28:38save and let's compare it and it looks
  8759. 7:28:41like our final design has a little more
  8760. 7:28:43spacing than this so let's change this
  8761. 7:28:47to 60 pixels and
  8762. 7:28:51save and that looks close enough and
  8763. 7:28:54finally we'll style the reset score
  8764. 7:28:57button so let's go back to our HTM
  8765. 7:29:00ml we'll add a class to this button so
  8766. 7:29:05here class equals reset
  8767. 7:29:10dcore Das
  8768. 7:29:14button so at the top we'll type dot
  8769. 7:29:19reset
  8770. 7:29:21dcore
  8771. 7:29:23Dutton curly
  8772. 7:29:25brackets and we'll make the background
  8773. 7:29:27color white so background dasc color
  8774. 7:29:32colon
  8775. 7:29:34white and let's also remove the border
  8776. 7:29:37from our button using border colon
  8777. 7:29:42none let's save and see how it
  8778. 7:29:46looks and then let's increase the size
  8779. 7:29:49of this text because the final design
  8780. 7:29:52looks a little bigger so we'll add font
  8781. 7:29:56Das size colon 15 pixels and
  8782. 7:30:02save now let's add some extra space on
  8783. 7:30:05the inside of the button so that's
  8784. 7:30:08padding padding colon and we'll give it
  8785. 7:30:13two values the first value is the
  8786. 7:30:15vertical padding let's use 10
  8787. 7:30:19pixels and the second value is the
  8788. 7:30:22horizontal padding so let's use 15
  8789. 7:30:27pixels and Save
  8790. 7:30:31and it looks like our final project is a
  8791. 7:30:33little shorter than our button so let's
  8792. 7:30:36change the vertical padding to maybe
  8793. 7:30:40eight and save and the last step is to
  8794. 7:30:44add the cursor pointer so here cursor
  8795. 7:30:50colon
  8796. 7:30:51pointer and
  8797. 7:30:54save and that's pretty much it we
  8798. 7:30:56finished styling our rock paper scissors
  8799. 7:30:58project so now I'm going to rearrange my
  8800. 7:31:01windows so we can see the entire design
  8801. 7:31:04and it looks really nice the last step
  8802. 7:31:07here is to remove these fake results and
  8803. 7:31:10use JavaScript to insert these results
  8804. 7:31:13instead so let's scroll
  8805. 7:31:17down and we'll remove this
  8806. 7:31:22result and let's actually copy this code
  8807. 7:31:25because we want our JavaScript to
  8808. 7:31:26generate this code so we'll right click
  8809. 7:31:31and
  8810. 7:31:32copy and let's scroll to the
  8811. 7:31:35bottom to where we insert the moves onto
  8812. 7:31:38the page which is here and instead of
  8813. 7:31:41this text we're going to rightclick and
  8814. 7:31:45paste the code that we had before and
  8815. 7:31:49this time instead of picking Rock every
  8816. 7:31:51time we're going to substitute this with
  8817. 7:31:54the player move so we're remove this and
  8818. 7:31:58because this is already a template
  8819. 7:32:00string we can insert a value directly
  8820. 7:32:03inside using dollar and curly brackets
  8821. 7:32:07and we'll insert the
  8822. 7:32:09player
  8823. 7:32:11move let's also insert the computer's
  8824. 7:32:14move in here so we'll remove this and
  8825. 7:32:17insert a value using dollar and curly
  8826. 7:32:20brackets and we'll insert computer
  8827. 7:32:25move and now that we're inserting this
  8828. 7:32:28with JavaScript we can scroll up and
  8829. 7:32:31remove the fake
  8830. 7:32:33result which is here so we can select it
  8831. 7:32:37and delete
  8832. 7:32:38it now let's
  8833. 7:32:41save and we're not going to have
  8834. 7:32:43anything in our results and when we
  8835. 7:32:45click a move it will show us the result
  8836. 7:32:49and also show us the moves that we
  8837. 7:32:51picked and that's it we finished the
  8838. 7:32:54rock paper scissors
  8839. 7:32:56project the last thing we're going to
  8840. 7:32:58learn in this lesson is how to organize
  8841. 7:33:00our code into different
  8842. 7:33:03files so notice that right now all of
  8843. 7:33:06our HTML CSS and JavaScript is in one
  8844. 7:33:10HTML file and this makes the code harder
  8845. 7:33:13to read because the file is bigger and
  8846. 7:33:16it mixes three different languages to
  8847. 7:33:19make our code more organized and easier
  8848. 7:33:22to read we're going to separate each
  8849. 7:33:24language into its own file so let's
  8850. 7:33:28scroll down and we'll start by
  8851. 7:33:30separating our JavaScript into its own
  8852. 7:33:33file we'll click here to show our files
  8853. 7:33:37and then click this icon to create a new
  8854. 7:33:41file and we'll name this file
  8855. 7:33:4410-
  8856. 7:33:48rock-paper-
  8857. 7:33:50scissors. JS and press
  8858. 7:33:54enter so when we end a file
  8859. 7:33:57withjs it tells the computer that this
  8860. 7:34:00file contains JavaScript code not just
  8861. 7:34:04text now let's go back to our HTML
  8862. 7:34:08file and we're going to move all of this
  8863. 7:34:10code into the Javascript file so we'll
  8864. 7:34:14select this
  8865. 7:34:17code and then we'll
  8866. 7:34:20rightclick and
  8867. 7:34:22copy and inside the Javascript file
  8868. 7:34:26we'll rightclick and paste
  8869. 7:34:31now let's
  8870. 7:34:33save and we'll go back to the HTML file
  8871. 7:34:37and we can delete all this JavaScript
  8872. 7:34:41code and now we're just going to load
  8873. 7:34:44our Javascript file on to the web page
  8874. 7:34:47to do that we can use an attribute of
  8875. 7:34:50the script element called SRC or source
  8876. 7:34:55so we'll make it equal and this does the
  8877. 7:34:58same thing as the image element it tells
  8878. 7:35:01a computer which file to
  8879. 7:35:03load if we type
  8880. 7:35:0610-
  8881. 7:35:11rock-paper-scissors
  8882. 7:35:14DJs this will look for a file called 10
  8883. 7:35:17rockpaper scissors. JS beside our HTML
  8884. 7:35:21file which is this file here and it will
  8885. 7:35:24run all the JavaScript inside this file
  8886. 7:35:28so it does the same thing as before
  8887. 7:35:31except now our JavaScript code is in its
  8888. 7:35:34own
  8889. 7:35:35file and this makes our code cleaner
  8890. 7:35:38because each file only contains one
  8891. 7:35:41language and each file also contains
  8892. 7:35:44less
  8893. 7:35:45code now let's go back to the HTML file
  8894. 7:35:49and
  8895. 7:35:50save and you'll notice that our project
  8896. 7:35:53works the same way as before we just
  8897. 7:35:55organized our code
  8898. 7:35:57better another feature of The Source
  8899. 7:36:00attribute is that it also uses file
  8900. 7:36:02paths so we can put this Javascript file
  8901. 7:36:05in a folder and we'll be able to find it
  8902. 7:36:09for example let's show our files and
  8903. 7:36:12let's create a new folder by clicking
  8904. 7:36:14this
  8905. 7:36:15icon and let's name this folder
  8906. 7:36:21scripts so this folder will contain all
  8907. 7:36:24of our JS files so we can drag this file
  8908. 7:36:29into the
  8909. 7:36:32folder and now we can locate it using
  8910. 7:36:35the source attribute by typing the
  8911. 7:36:37folder name which is scripts and then a
  8912. 7:36:42slash to go inside this folder and look
  8913. 7:36:45for this file and it will find it right
  8914. 7:36:49here if we
  8915. 7:36:51save everything works as
  8916. 7:36:54normal another feature is we can use
  8917. 7:36:57multiple script elements this lets us
  8918. 7:37:00split our JavaScript into multiple files
  8919. 7:37:02and then load them all onto the page
  8920. 7:37:05from top to
  8921. 7:37:06bottom next we're going to learn how to
  8922. 7:37:09separate our CSS into its own
  8923. 7:37:13file let's scroll up to our
  8924. 7:37:16CSS so we can do the same thing with CSS
  8925. 7:37:20so let's click the HTML
  8926. 7:37:23file and let's create a new
  8927. 7:37:26folder and we'll call this folder Style
  8928. 7:37:30and press
  8929. 7:37:32enter so this folder will contain all of
  8930. 7:37:35our CSS code let's click the folder and
  8931. 7:37:39we'll create a new file by clicking this
  8932. 7:37:42icon and let's name this file
  8933. 7:37:4510-
  8934. 7:37:47ro-
  8935. 7:37:49paper-
  8936. 7:37:51scissors. CSS and press
  8937. 7:37:55enter so ending the file with CSS means
  8938. 7:37:59this file contains CSS code and now we
  8939. 7:38:03can go back to our
  8940. 7:38:05HTML and move our CSS into its own file
  8941. 7:38:10so let's select all this
  8942. 7:38:12code and then right
  8943. 7:38:15click
  8944. 7:38:16copy and in here right click and
  8945. 7:38:23paste and save this
  8946. 7:38:26file now let's go back to our HTML
  8947. 7:38:30and we can remove this
  8948. 7:38:32code and we'll learn how to load our CSS
  8949. 7:38:35file onto our
  8950. 7:38:37website so we're not going to use the
  8951. 7:38:40style element for this we're going to
  8952. 7:38:43use a different element called the link
  8953. 7:38:46element so we'll type less than link
  8954. 7:38:50greater than and the link element
  8955. 7:38:53doesn't require a closing tag this is
  8956. 7:38:55another void element so the link element
  8957. 7:38:58uses two attributes the first attribute
  8958. 7:39:02is called
  8959. 7:39:05R means relation this tells a computer
  8960. 7:39:09what type of file we're loading so we're
  8961. 7:39:12going to set this attribute to style
  8962. 7:39:15sheet this means we're loading a CSS
  8963. 7:39:18file and then we'll add a second
  8964. 7:39:21attribute called
  8965. 7:39:24href so href tells a computer which file
  8966. 7:39:28we want to load
  8967. 7:39:30so this also supports file paaths so we
  8968. 7:39:33can type the Styles
  8969. 7:39:36folder this will look for a folder
  8970. 7:39:39beside the HTML called Styles which is
  8971. 7:39:42this one right here and then we'll type
  8972. 7:39:45slash to go inside the folder and look
  8973. 7:39:49for this CSS file which is
  8974. 7:39:5310- Rock Das paper Das scissors
  8975. 7:40:00CSS so this will load all the CSS inside
  8976. 7:40:04this file and apply it to our page so it
  8977. 7:40:08does the same thing as before except we
  8978. 7:40:11separated each language into its own
  8979. 7:40:13file if we save everything still works
  8980. 7:40:17and all the Styles get applied and just
  8981. 7:40:21like the script element we can also use
  8982. 7:40:23multiple link elements to load multiple
  8983. 7:40:26CSS
  8984. 7:40:28files so that's how we separate our code
  8985. 7:40:30into separate files using the script
  8986. 7:40:33element and the link element it makes
  8987. 7:40:36our code a lot easier to read because
  8988. 7:40:39each file contains one language and it
  8989. 7:40:42contains less code and that's the end of
  8990. 7:40:45this lesson as a final reminder the CSS
  8991. 7:40:48we learned in this lesson is just a
  8992. 7:40:50review if you want to learn CSS in more
  8993. 7:40:53detail you can check out my HTML and CSS
  8994. 7:40:57full course in the video
  8995. 7:40:59description so in this lesson we
  8996. 7:41:02reviewed CSS and added CSS to all of our
  8997. 7:41:06projects we learned about the class list
  8998. 7:41:09property which lets us control the class
  8999. 7:41:12attribute of an element we finished our
  9000. 7:41:14rock paper scissors project and we
  9001. 7:41:17learned how to organize our JavaScript
  9002. 7:41:19and CSS code into separate
  9003. 7:41:25files thanks for watching this course so
  9004. 7:41:27far if you want to support this channel
  9005. 7:41:30you can check out the premium version of
  9006. 7:41:32my courses just like this one where you
  9007. 7:41:34can get a certificate of completion and
  9008. 7:41:36a Better Learning platform if you're
  9009. 7:41:39interested in extra videos from me more
  9010. 7:41:41in-depth videos and behindth scenes
  9011. 7:41:44content you can check out these links
  9012. 7:41:47here you can also support the channel by
  9013. 7:41:49liking the video subscribing and sharing
  9014. 7:41:52this video with others thanks again and
  9015. 7:41:55let's continue with the course
  9016. 7:41:59here are some exercises to help you
  9017. 7:42:01practice using HTML CSS and JavaScript
  9018. 7:42:05together
  9019. 7:42:31in this lesson we're going to learn two
  9020. 7:42:33more features of JavaScript called
  9021. 7:42:36arrays and loops and we're going to use
  9022. 7:42:39these features to create this to-do list
  9023. 7:42:42project first let's create a new file
  9024. 7:42:45for this lesson we'll go to our code
  9025. 7:42:48editor and click up here to show our
  9026. 7:42:51files and this time make sure you have
  9027. 7:42:54one of these HTML files selected so we
  9028. 7:42:57don't create the new file in one of
  9029. 7:42:59these
  9030. 7:43:00folders so now we're going to click this
  9031. 7:43:03icon to create a new
  9032. 7:43:05file and we're going to name this file
  9033. 7:43:0911- arrays Das and Das
  9034. 7:43:16loops.
  9035. 7:43:17HTML and press
  9036. 7:43:20enter now let's copy the previous lesson
  9037. 7:43:24into this file so we'll click on 10 Dom
  9038. 7:43:27projects and and we'll type here and typ
  9039. 7:43:30control a or command a to select the
  9040. 7:43:33code and then
  9041. 7:43:36rightclick
  9042. 7:43:38copy and in
  9043. 7:43:40here right click and
  9044. 7:43:44paste now let's hide our files and we'll
  9045. 7:43:48prepare this file for this lesson so we
  9046. 7:43:51can scroll to the
  9047. 7:43:53top and we'll change the title to arrays
  9048. 7:43:59and
  9049. 7:44:01loops and let's remove all of the
  9050. 7:44:09CSS and we'll also remove the
  9051. 7:44:13HTML which is right
  9052. 7:44:18here and we'll remove all the
  9053. 7:44:24JavaScript now let's
  9054. 7:44:27save and we'll open this file in the
  9055. 7:44:29browser by right clicking open with live
  9056. 7:44:35server and let's also open the console
  9057. 7:44:38so we right click click
  9058. 7:44:41inspect and click the
  9059. 7:44:44console and now we can close the
  9060. 7:44:47previous
  9061. 7:44:48tabs and the previous code for
  9062. 7:44:53now until we just have the lesson 11
  9063. 7:44:57file now we're ready to learn arrays an
  9064. 7:45:01array is another type of value in
  9065. 7:45:04JavaScript and it represents a list of
  9066. 7:45:07other values so let's do an example and
  9067. 7:45:10create our first array we'll go into the
  9068. 7:45:13script element and we're going to type
  9069. 7:45:17open square bracket and closed square
  9070. 7:45:19bracket and inside the square brackets
  9071. 7:45:22we'll type a value like the number 10
  9072. 7:45:26and then we'll type comma and and
  9073. 7:45:29another value like the number 20 and
  9074. 7:45:32then comma again and a third value like
  9075. 7:45:35the number
  9076. 7:45:3730 so this is an array and it represents
  9077. 7:45:42a list of three numbers 10 20 and
  9078. 7:45:4630 now arrays themselves are also values
  9079. 7:45:51which means we can save them in a
  9080. 7:45:53variable so at the front we can create a
  9081. 7:45:56variable using const
  9082. 7:45:59and let's name it my array and we'll
  9083. 7:46:04make it equal to this array and we'll
  9084. 7:46:07add a semicolon at the end so now we
  9085. 7:46:11created a list of values and we saved
  9086. 7:46:14this list in this
  9087. 7:46:16variable let's create a new line and
  9088. 7:46:19we'll
  9089. 7:46:22console.log my
  9090. 7:46:25array and Save
  9091. 7:46:28and it will display the list of values
  9092. 7:46:31in the console so that's basically what
  9093. 7:46:34an array is it's a list of
  9094. 7:46:37values now to get a specific value in an
  9095. 7:46:40array we can use square brackets so
  9096. 7:46:43let's create a new line and we'll type
  9097. 7:46:47my array and square
  9098. 7:46:51brackets inside these brackets we'll put
  9099. 7:46:53a number like
  9100. 7:46:55zero this number represents the position
  9101. 7:46:59in the array so zero means the first
  9102. 7:47:02value if we
  9103. 7:47:05console do log this
  9104. 7:47:10code and
  9105. 7:47:12save my array Square braet 0 gives us
  9106. 7:47:16the first value which is 10 and that's
  9107. 7:47:19what's displayed in the
  9108. 7:47:21console now if we change this to
  9109. 7:47:25one this will get us the second value in
  9110. 7:47:28the array which is 20 if we save it
  9111. 7:47:32displays 20 so this number represents
  9112. 7:47:36the position in the array zero is the
  9113. 7:47:40first value one is the second value two
  9114. 7:47:44is the third value and so on we can also
  9115. 7:47:48change a value in an array also using
  9116. 7:47:51square brackets so let's create a new
  9117. 7:47:55line and we'll type my array
  9118. 7:47:59square brackets and let's say we want to
  9119. 7:48:02change the first value so we'll type
  9120. 7:48:05zero for the first value and we just
  9121. 7:48:08make it equal to something else like
  9122. 7:48:1399 now if we
  9123. 7:48:17console.log my array
  9124. 7:48:20again and
  9125. 7:48:23save you can see that this changed the
  9126. 7:48:26first value to 99
  9127. 7:48:29so that's basically how arrays work we
  9128. 7:48:32can access a value inside an array and
  9129. 7:48:35we can also change a value inside an
  9130. 7:48:38array now that we understand the basics
  9131. 7:48:41of arrays let's learn the syntax rules
  9132. 7:48:44to create an array we start with an open
  9133. 7:48:47square bracket and we end with a closed
  9134. 7:48:50square bracket inside we can put a list
  9135. 7:48:54of values and we separate each value
  9136. 7:48:57with a comma
  9137. 7:48:59we can also write an array on multiple
  9138. 7:49:01lines like this we just put the new
  9139. 7:49:05lines after the
  9140. 7:49:08comma Now to get a value inside an array
  9141. 7:49:11we can use square brackets and a number
  9142. 7:49:15like one this number is called the index
  9143. 7:49:19it represents the position in the array
  9144. 7:49:21that we want to get so the first value
  9145. 7:49:25is index zero the second value is index
  9146. 7:49:28one 1 third value is index 2 and so on
  9147. 7:49:33notice that the index starts at zero
  9148. 7:49:36this is called a zero indexed
  9149. 7:49:39array now inside an array we can put any
  9150. 7:49:42type of value for example let's create a
  9151. 7:49:46new line and we'll create a new array
  9152. 7:49:50using square brackets and inside we can
  9153. 7:49:54put the number one and then comma and we
  9154. 7:49:58can also put a string like
  9155. 7:50:01hello and another comma and we can put a
  9156. 7:50:05Boolean value like true and another
  9157. 7:50:09comma and we can even put an object like
  9158. 7:50:14Curly
  9159. 7:50:14brackets Name
  9160. 7:50:17colon
  9161. 7:50:20socks now an array itself is also a
  9162. 7:50:24value that means we can save arrays in
  9163. 7:50:28very variables like this or even save it
  9164. 7:50:32in an array for example in this array we
  9165. 7:50:37can add an array inside like 1 comma
  9166. 7:50:432 next arrays have a weird behavior when
  9167. 7:50:46we try to check the type of an array so
  9168. 7:50:50on a new line let's type
  9169. 7:50:55console.log type of and then square
  9170. 7:50:59brackets one comma
  9171. 7:51:022 and
  9172. 7:51:05save this will tell us that an array is
  9173. 7:51:09an object and that's because an array is
  9174. 7:51:13actually an object but it's just a
  9175. 7:51:15special type of object if we want to
  9176. 7:51:18check if something is specifically an
  9177. 7:51:20array we can use this code capital A
  9178. 7:51:25array dot is
  9179. 7:51:29array
  9180. 7:51:31brackets and inside the brackets we'll
  9181. 7:51:33put an array like square brackets 1
  9182. 7:51:38comma 2 so this will check if this value
  9183. 7:51:42is an array if we
  9184. 7:51:46console. log this
  9185. 7:51:49result and
  9186. 7:51:51save it will display
  9187. 7:51:55true and finally because arrays are are
  9188. 7:51:58just a type of object they also have
  9189. 7:52:01properties and methods we can use so one
  9190. 7:52:04useful property is called length so
  9191. 7:52:08we'll type
  9192. 7:52:12console.log my
  9193. 7:52:15array.
  9194. 7:52:18length this will tell us how many values
  9195. 7:52:21are in the array so my array has three
  9196. 7:52:25values so if we save
  9197. 7:52:28it will display
  9198. 7:52:30three next we'll learn a useful method
  9199. 7:52:34of an array called push this adds a
  9200. 7:52:38value to the end of the
  9201. 7:52:41array if we did my
  9202. 7:52:45array.
  9203. 7:52:47push
  9204. 7:52:48100 this will add 100 to the end of my
  9205. 7:52:53array if we console.log
  9206. 7:52:58my
  9207. 7:53:00array and
  9208. 7:53:02save you can see that 100 was added at
  9209. 7:53:06the
  9210. 7:53:07end the last method we're going to learn
  9211. 7:53:10for now is called do splice this removes
  9212. 7:53:14a value from an array for example my
  9213. 7:53:20array do
  9214. 7:53:22splice and
  9215. 7:53:24brackets and a splice takes two numbers
  9216. 7:53:28the first number is the index that we
  9217. 7:53:30want to remove so let's say we want to
  9218. 7:53:33remove the first value which is index
  9219. 7:53:37zero the second number we give to splice
  9220. 7:53:40is the number of values we want to
  9221. 7:53:43remove if we give it one it will remove
  9222. 7:53:46one value at index zero if we give it
  9223. 7:53:50two it will remove two values starting
  9224. 7:53:53at index zero so for now let's just
  9225. 7:53:57remove one value
  9226. 7:53:59and let's create a new line and
  9227. 7:54:04console.log my
  9228. 7:54:08array and
  9229. 7:54:10save you can see that splice removed the
  9230. 7:54:13first value from the array or index0
  9231. 7:54:17which was
  9232. 7:54:1899 all right now that we understand the
  9233. 7:54:22basics of arrays and the syntax rules
  9234. 7:54:25let's do some practical examples so
  9235. 7:54:28prepared a project we can do for this
  9236. 7:54:30lesson we'll go to our browser create a
  9237. 7:54:34new tab at the
  9238. 7:54:36top and here we'll type
  9239. 7:54:41superimple
  9240. 7:54:43dodev SL projects slash arrays and press
  9241. 7:54:51enter so this page contains the to-do
  9242. 7:54:54list project we're going to create in
  9243. 7:54:56this lesson so down here is the final
  9244. 7:54:59version of the project we can type a
  9245. 7:55:02name for our to-do like make
  9246. 7:55:06dinner give it a due date and then click
  9247. 7:55:10add and it will add it to our list we
  9248. 7:55:13can add another to-do like wash
  9249. 7:55:18dishes and then select a due date and
  9250. 7:55:22click add again and it will add another
  9251. 7:55:25to-do to our list and each to-do also
  9252. 7:55:28has a delete button so we can delete it
  9253. 7:55:31when we're finished the task so this is
  9254. 7:55:34the final version of the project and
  9255. 7:55:37these two up here are simpler versions
  9256. 7:55:40of the project that we're going to use
  9257. 7:55:42to work our way up so let's start with
  9258. 7:55:45the first project up here we're going to
  9259. 7:55:48open our console by
  9260. 7:55:51right-clicking click
  9261. 7:55:53inspect and click the
  9262. 7:55:56console and the way way this first
  9263. 7:55:59project works is we can type a to-do in
  9264. 7:56:02this text box like make
  9265. 7:56:06dinner and when we click add it will add
  9266. 7:56:10the to-do to an array and display it in
  9267. 7:56:13the
  9268. 7:56:14console if we type another to-do like
  9269. 7:56:18wash dishes and click add it will add it
  9270. 7:56:23to the array and display it in the
  9271. 7:56:26console so let's create create this
  9272. 7:56:28project we'll start by creating a file
  9273. 7:56:31for this project we'll go to our code
  9274. 7:56:34editor and click up here to show our
  9275. 7:56:38files and click this icon to create a
  9276. 7:56:41file and we'll name this
  9277. 7:56:4511-
  9278. 7:56:46too-
  9279. 7:56:48list.
  9280. 7:56:50HTML and press
  9281. 7:56:53enter now let's copy the code from
  9282. 7:56:56arrays and loops. HTML
  9283. 7:56:58so we'll click here control a or command
  9284. 7:57:02a and then
  9285. 7:57:04rightclick
  9286. 7:57:06copy and down here right click and
  9287. 7:57:12paste and let's close this for
  9288. 7:57:15now we'll scroll up and change the title
  9289. 7:57:20to to do list and remove all the
  9290. 7:57:26JavaScript code
  9291. 7:57:31and this time instead of writing all the
  9292. 7:57:33JavaScript in here let's actually put
  9293. 7:57:35the JavaScript in a separate file which
  9294. 7:57:38we learned in the previous lesson so
  9295. 7:57:40we'll open our files
  9296. 7:57:42again and this time we'll go to our
  9297. 7:57:45scripts folder we'll click this JS file
  9298. 7:57:49and we'll click this icon to add a new
  9299. 7:57:52file to this folder and let's name it
  9300. 7:57:5711- to-do Das
  9301. 7:58:02list.js and press
  9302. 7:58:04enter next we're going to load this file
  9303. 7:58:08into our HTML file so let's open this
  9304. 7:58:12and in the script element we can remove
  9305. 7:58:15the spaces and we're going to add an
  9306. 7:58:17attribute called
  9307. 7:58:20SRC equals and we're going to load the
  9308. 7:58:23Javascript file that we just created
  9309. 7:58:26which is inside the script folder so
  9310. 7:58:29we'll type
  9311. 7:58:31scripts and then a slash to go into this
  9312. 7:58:35folder and we'll load the file
  9313. 7:58:3911-
  9314. 7:58:40too-
  9315. 7:58:43list.js and
  9316. 7:58:46save so now I'm going to drag this file
  9317. 7:58:49down here so I can see both the HTML and
  9318. 7:58:53JavaScript at the same time close this
  9319. 7:58:56for now and I can close the other code
  9320. 7:58:59for now as
  9321. 7:59:01well and finally let's open this in our
  9322. 7:59:04browser so we'll right
  9323. 7:59:06click open with live
  9324. 7:59:10server and let's also open the console
  9325. 7:59:13on this page right click click
  9326. 7:59:17inspect and click the
  9327. 7:59:20console now one more thing I'm going to
  9328. 7:59:22do for this video is I'll rearrange my
  9329. 7:59:25windows so that we can have the the
  9330. 7:59:28final design at the
  9331. 7:59:30bottom let's start by creating the HTML
  9332. 7:59:34elements let's go to the body and we'll
  9333. 7:59:37actually create a paragraph element to
  9334. 7:59:40start inside the paragraph we'll put the
  9335. 7:59:43text to-do
  9336. 7:59:47list next let's create the text box so
  9337. 7:59:51to create a text box less than
  9338. 7:59:55input greater than and add a placeholder
  9339. 7:59:59to this text box Place holder equals to
  9340. 8:00:05do name finally let's add this
  9341. 8:00:10button so on a new line let's type less
  9342. 8:00:14than button greater than and inside the
  9343. 8:00:18button we'll have the text
  9344. 8:00:21add now let's
  9345. 8:00:23save and this creates all the HTML
  9346. 8:00:26elements that we need
  9347. 8:00:29next let's write our JavaScript so first
  9348. 8:00:33we're going to create an array to store
  9349. 8:00:35our to-dos and then when we click the
  9350. 8:00:39add button we're going to get the text
  9351. 8:00:41from the text box and add it to our
  9352. 8:00:45array and finally we'll console.log the
  9353. 8:00:49array now let's convert these steps into
  9354. 8:00:52code let's create a new array first
  9355. 8:00:55using const
  9356. 8:00:58let's name it
  9357. 8:01:00to-do list and we'll make it equal to
  9358. 8:01:04square
  9359. 8:01:05brackets so this is an array with no
  9360. 8:01:08values inside we also call this an empty
  9361. 8:01:12array next when we click the button
  9362. 8:01:15let's get the value from the text box so
  9363. 8:01:19let's go to our button and add an
  9364. 8:01:22onclick
  9365. 8:01:24attribute and then inside here let's
  9366. 8:01:28create a function for this so down here
  9367. 8:01:32let's create a
  9368. 8:01:35function we'll name it add Todo brackets
  9369. 8:01:40and curly
  9370. 8:01:42brackets and then inside the onclick
  9371. 8:01:44attribute we're just going to call this
  9372. 8:01:47function add too and
  9373. 8:01:52brackets the next step is to get the
  9374. 8:01:55text in the text box we can do that
  9375. 8:01:58using document. query selector so to use
  9376. 8:02:02Query selector we're going to put a
  9377. 8:02:04class on this input element so we'll
  9378. 8:02:08type the class
  9379. 8:02:10attribute
  9380. 8:02:12js- name Das
  9381. 8:02:16input and then in here document do query
  9382. 8:02:24selector brackets and a string and we'll
  9383. 8:02:28type a DOT to get a class and we'll get
  9384. 8:02:32this class
  9385. 8:02:34js-
  9386. 8:02:36name-
  9387. 8:02:38input as a reminder this gets an element
  9388. 8:02:42with the class JS name input which is
  9389. 8:02:45this element and puts it into our
  9390. 8:02:49JavaScript and now let's save this in a
  9391. 8:02:52variable so at the start let's type
  9392. 8:02:56const in input element and make it equal
  9393. 8:03:01to this
  9394. 8:03:03result so now we took this input and put
  9395. 8:03:06it into our
  9396. 8:03:08JavaScript to get the text out we're
  9397. 8:03:10going to use a property of the input
  9398. 8:03:13element called value so down here
  9399. 8:03:18input
  9400. 8:03:20element.
  9401. 8:03:21value and let's save the value in the
  9402. 8:03:24text box in a variable so we can use it
  9403. 8:03:27later
  9404. 8:03:28at the front const name and we'll make
  9405. 8:03:33it equal to whatever's in the text
  9406. 8:03:37box before we continue let's
  9407. 8:03:41console.log the name to check if this is
  9408. 8:03:45working so let's save this Javascript
  9409. 8:03:48file and let's also save this HTML
  9410. 8:03:53file we'll go to our browser and I'll
  9411. 8:03:56resize it so we can you can see the
  9412. 8:03:58console and now if we type something in
  9413. 8:04:01the text box like make dinner and click
  9414. 8:04:06add it will get the text from the text
  9415. 8:04:09box and then display it in the
  9416. 8:04:12console okay so it's looking good so far
  9417. 8:04:16the next step is to add this name to our
  9418. 8:04:19to-do list so we learned how to do this
  9419. 8:04:22earlier we can use a method called push
  9420. 8:04:26push will add a value to the end of the
  9421. 8:04:29array so let's remove this for now and
  9422. 8:04:33at the
  9423. 8:04:35bottom we're going to type our
  9424. 8:04:38array to do
  9425. 8:04:42list. push
  9426. 8:04:45brackets and then between the brackets
  9427. 8:04:48we're going to add our name
  9428. 8:04:50here and now the last step is to
  9429. 8:04:53console.log the entire array
  9430. 8:05:00console.log Todo
  9431. 8:05:03list and
  9432. 8:05:06save now let's give it a try or'll type
  9433. 8:05:11make dinner in the text box and click
  9434. 8:05:15add it will take this text add it to the
  9435. 8:05:18array and then console.log the array and
  9436. 8:05:22now we have our to-do list let's add
  9437. 8:05:25another to-do we'll type type wash
  9438. 8:05:30dishes and click
  9439. 8:05:32add again it will get the text in the
  9440. 8:05:35text box and then add it to the array so
  9441. 8:05:39now our array has two values and then we
  9442. 8:05:42display them in the
  9443. 8:05:43console so that's how we create this
  9444. 8:05:47first version of the to-do list project
  9445. 8:05:50now one more feature we're going to
  9446. 8:05:52create is if we add something to our
  9447. 8:05:55to-do list and click add
  9448. 8:05:58it will reset the text box to do that
  9449. 8:06:02let's go into our
  9450. 8:06:03code and we'll create some new lines and
  9451. 8:06:07remember that the value property
  9452. 8:06:09represents the text in the text box so
  9453. 8:06:12to change the text in the text box we
  9454. 8:06:15can simply change the value property so
  9455. 8:06:18we'll type
  9456. 8:06:20input
  9457. 8:06:22element. value and change it to the
  9458. 8:06:26empty string
  9459. 8:06:27so this will make the text in the text
  9460. 8:06:29box become empty if we
  9461. 8:06:33save and now type something in the text
  9462. 8:06:37box and click
  9463. 8:06:39add it will reset the text in the text
  9464. 8:06:43box let's move on to the second version
  9465. 8:06:46of the to-do list so here when we type
  9466. 8:06:49something in the text box like make
  9467. 8:06:53dinner and click add it will actually
  9468. 8:06:56display it on the page if we type
  9469. 8:06:59something else like wash dishes and
  9470. 8:07:04click add it will also add it to the
  9471. 8:07:07page so how do we take our array and
  9472. 8:07:11display each value on the page to do
  9473. 8:07:14this we're going to learn another
  9474. 8:07:16feature of JavaScript called loops loops
  9475. 8:07:20let us run some code over and over like
  9476. 8:07:23a loop so let's go back to our 11 arrays
  9477. 8:07:28and Loops file and we can close this
  9478. 8:07:31JavaScript file for now and we're going
  9479. 8:07:33to learn Loops so first let's comment
  9480. 8:07:37out the previous code by typing
  9481. 8:07:41slashstar and at the bottom star
  9482. 8:07:45slash let's scroll
  9483. 8:07:47down and we'll create our first Loop
  9484. 8:07:51we'll start by creating a variable using
  9485. 8:07:54the word let and we'll name name this
  9486. 8:07:57variable I and we'll make it equal to
  9487. 8:08:021 next we're going to type the word
  9488. 8:08:06while and then brackets and curly
  9489. 8:08:10brackets and then inside these curly
  9490. 8:08:13brackets we're going to put some code
  9491. 8:08:15that the computer will run over and over
  9492. 8:08:18just like a loop let's put the code
  9493. 8:08:22console.
  9494. 8:08:24log
  9495. 8:08:26I so this will display the variable I in
  9496. 8:08:29the console over and over again now
  9497. 8:08:33obviously we don't want to Loop this
  9498. 8:08:35forever so how do we get this Loop to
  9499. 8:08:38stop we can do that using the round
  9500. 8:08:41brackets up here inside these round
  9501. 8:08:44brackets we're going to put some code
  9502. 8:08:46that results in true or false for
  9503. 8:08:49example we can check if I is less than
  9504. 8:08:54or equal to 5 so the way this works is
  9505. 8:08:58that if this code results in true the
  9506. 8:09:01loop will keep going over and over once
  9507. 8:09:05this results in false the loop will stop
  9508. 8:09:08and it will go to the next line now you
  9509. 8:09:11may notice that this code will never
  9510. 8:09:14result in false because I is equal to 1
  9511. 8:09:17and 1 is always less than or equal to 5
  9512. 8:09:21so when does this actually become false
  9513. 8:09:24and Stop the Loop the answer is inside
  9514. 8:09:28this loop we're also going to update
  9515. 8:09:30this variable I so we'll type I and
  9516. 8:09:34we'll make it equal to itself plus
  9517. 8:09:391 so now every time we Loop over this
  9518. 8:09:43code we're going to increase I by 1 and
  9519. 8:09:47eventually I will be greater than 5 this
  9520. 8:09:50will return false and it will stop the
  9521. 8:09:53loop if we
  9522. 8:09:55save and then go back back to the tab
  9523. 8:09:58for this file and I'll rearrange my
  9524. 8:10:01windows so we can see the console this
  9525. 8:10:04Loop makes the computer count from 1 to
  9526. 8:10:085 so let's go over this code step by
  9527. 8:10:11step to make sure we understand how it
  9528. 8:10:14works first we create a variable I and
  9529. 8:10:18we make it equal to one and then we do a
  9530. 8:10:22loop so if this condition is true the
  9531. 8:10:26computer will keep running this code
  9532. 8:10:29over and over again so the first time it
  9533. 8:10:33Loops I equals 1 and then it displays
  9534. 8:10:36one in the console and then we increase
  9535. 8:10:39I to two now we go back to the top and
  9536. 8:10:43we check this code 2 is less than or
  9537. 8:10:47equal to 5 this is true so we're going
  9538. 8:10:50to run this code again this time I equal
  9539. 8:10:532 and we display it in the console and
  9540. 8:10:56then we we increase I to three and now 3
  9541. 8:11:01is still less than or equal to 5 so this
  9542. 8:11:04is true which means we keep running this
  9543. 8:11:07code and eventually this is going to
  9544. 8:11:10increase I to six once I is six this
  9545. 8:11:15will become false which will cause the
  9546. 8:11:17loop to stop and then we go to the next
  9547. 8:11:20line so that's why it counts from 1 to 5
  9548. 8:11:24and then it stops at five
  9549. 8:11:27so this is called a while loop while
  9550. 8:11:32this condition is true it will keep
  9551. 8:11:35running this code over and over again
  9552. 8:11:38just like a loop as soon as this
  9553. 8:11:40condition is false it will stop the
  9554. 8:11:44loop all right now that we understand
  9555. 8:11:47how a while loop Works let's learn the
  9556. 8:11:50syntax rules to create a while loop we
  9557. 8:11:53type the word while and then round
  9558. 8:11:56brackets and curly brackets the code
  9559. 8:11:59inside the round brackets is called the
  9560. 8:12:02loop condition the code inside the curly
  9561. 8:12:05brackets is called the loop body the
  9562. 8:12:09variable I at the top is called the loop
  9563. 8:12:13variable every time we loop we're going
  9564. 8:12:15to increase the loop variable this is
  9565. 8:12:18known as the increment step the
  9566. 8:12:21increment step makes sure that the loop
  9567. 8:12:24ends at some point without this
  9568. 8:12:26increment step the loop would just keep
  9569. 8:12:29going forever this is known as an
  9570. 8:12:31infinite
  9571. 8:12:33Loop now remember that I = I + 1 has a
  9572. 8:12:38shortcut instead of this we can just do
  9573. 8:12:42I plus = 1 and remember we actually have
  9574. 8:12:47an even shorter shortcut for plus equal
  9575. 8:12:501 instead of this we can also do I ++
  9576. 8:12:57so this will increase the value of I by
  9577. 8:13:00one every time just like before so those
  9578. 8:13:04are the three major parts of a loop the
  9579. 8:13:07loop variable the loop condition and the
  9580. 8:13:10increment
  9581. 8:13:12step now each time we run this Loop this
  9582. 8:13:16is called an
  9583. 8:13:17iteration so in this Loop we did five
  9584. 8:13:21iterations we ran this code five times
  9585. 8:13:25and lastly Loops create a scope any
  9586. 8:13:28variable that we create between these
  9587. 8:13:31curly brackets will only exist inside
  9588. 8:13:34the curly brackets so that's a while
  9589. 8:13:38loop while this condition is true it
  9590. 8:13:42will keep running this code over and
  9591. 8:13:44over again once this condition becomes
  9592. 8:13:48false it will stop the loop and go to
  9593. 8:13:51the next
  9594. 8:13:52line so in JavaScript there's actually
  9595. 8:13:55two types of loops
  9596. 8:13:57the first type is a while loop which we
  9597. 8:14:00just learned now we're going to learn
  9598. 8:14:03another type of loop called a for Loop
  9599. 8:14:07to create a for Loop we'll create some
  9600. 8:14:09new lines and then down here we'll type
  9601. 8:14:14four and brackets and curly
  9602. 8:14:18brackets inside these brackets we're
  9603. 8:14:21going to type three pieces of code the
  9604. 8:14:24first piece of code is a loop variable
  9605. 8:14:27which is the same thing as this line up
  9606. 8:14:30here so we'll type let I equal one and
  9607. 8:14:36then semicolon space and we'll type a
  9608. 8:14:40second piece of code which is the loop
  9609. 8:14:43condition so here we'll type I less than
  9610. 8:14:48equal to 5 and then semicolon and space
  9611. 8:14:52and the last piece of code is the
  9612. 8:14:54increment step which is this line right
  9613. 8:14:57here so we'll type
  9614. 8:15:01i++ and between these curly brackets
  9615. 8:15:04we'll write the code that we want to
  9616. 8:15:06Loop over and over so let's just type
  9617. 8:15:09the code
  9618. 8:15:13console.log I and
  9619. 8:15:16save so this for Loop does the same
  9620. 8:15:19thing as the while loop above it counts
  9621. 8:15:22from 1 to 5 and if we look at this code
  9622. 8:15:26we can see that a for Loop is just a
  9623. 8:15:29shorter version of this while loop
  9624. 8:15:32instead of writing the loop variable
  9625. 8:15:35condition and increment step separately
  9626. 8:15:38we group all of them together at the top
  9627. 8:15:41so this Loop works the same way first we
  9628. 8:15:44create a loop variable and then as long
  9629. 8:15:48as this Loop condition is true we will
  9630. 8:15:51keep running this code over and over and
  9631. 8:15:55every time we run this code we're also
  9632. 8:15:57going to increase the loop variable by
  9633. 8:16:00one so now that we learned two ways of
  9634. 8:16:03doing a loop which one should we use if
  9635. 8:16:07we're doing a standard Loop for example
  9636. 8:16:10it has a loop variable a condition and
  9637. 8:16:13an increment step every time it's
  9638. 8:16:15recommended to use a for Loop because a
  9639. 8:16:18for Loop is shorter and more organized
  9640. 8:16:21than a while loop however if it's a
  9641. 8:16:25nonstandard loop then it might be better
  9642. 8:16:28to use a while loop so what's an example
  9643. 8:16:31of a non-standard loop so let's say we
  9644. 8:16:34want to Generate random numbers until we
  9645. 8:16:37get a number that's at least
  9646. 8:16:390.5 to create this Loop we'll create a
  9647. 8:16:43variable that will store the random
  9648. 8:16:46number using
  9649. 8:16:48let
  9650. 8:16:49random
  9651. 8:16:51number and make it equal to zero to
  9652. 8:16:54start and then we'll do a while
  9653. 8:16:58loop brackets and curly brackets now
  9654. 8:17:02inside the loop condition we want to
  9655. 8:17:05keep running this Loop until random
  9656. 8:17:07number is at least
  9657. 8:17:100.5 so we'll check if
  9658. 8:17:13random number is less than
  9659. 8:17:190.5 because if it is less than 0.5 we
  9660. 8:17:22want to keep running this Loop until we
  9661. 8:17:25get a random number that's at least
  9662. 8:17:280.5 and then inside this loop we're
  9663. 8:17:31going to generate a random number
  9664. 8:17:33between 0 and 1 and then save it in the
  9665. 8:17:36variable up here so we'll type random
  9666. 8:17:41number and make it equal to capital M
  9667. 8:17:46math.
  9668. 8:17:49random so if this random number is less
  9669. 8:17:52than 0.5 it will keep generating random
  9670. 8:17:56numbers until we get a number that is at
  9671. 8:17:58least
  9672. 8:18:000.5 so at the bottom if we
  9673. 8:18:04console. log
  9674. 8:18:07random
  9675. 8:18:08[Music]
  9676. 8:18:09number and
  9677. 8:18:11save it will give us a random number
  9678. 8:18:14that's at least
  9679. 8:18:160.5 so notice that this is a
  9680. 8:18:19non-standard loop it doesn't really have
  9681. 8:18:22a loop variable that we increase every
  9682. 8:18:25time and it also doesn't have the
  9683. 8:18:27increment step so in this situation when
  9684. 8:18:31we have a non-standard loop it's better
  9685. 8:18:34to use a while loop because they're a
  9686. 8:18:37bit more
  9687. 8:18:38flexible all right now that we learned
  9688. 8:18:41the basics of Loops let's do some
  9689. 8:18:43practical examples we're going to learn
  9690. 8:18:46a technique called looping through an
  9691. 8:18:49array this means we're going to go
  9692. 8:18:51through each value of an array one by
  9693. 8:18:54one and do something with each each
  9694. 8:18:57value so first let's comment out the
  9695. 8:19:00previous code using
  9696. 8:19:04slashstar and star slash at the bottom
  9697. 8:19:09let's create an array of strings so
  9698. 8:19:12we'll create a variable const to do list
  9699. 8:19:18equal to an
  9700. 8:19:20array and inside this array we'll have
  9701. 8:19:24the string make
  9702. 8:19:27dinner another string wash
  9703. 8:19:32dishes and another
  9704. 8:19:35string watch
  9705. 8:19:38YouTube now let's take each value in
  9706. 8:19:41this array and display it in the console
  9707. 8:19:44to do that we're going to run a loop and
  9708. 8:19:47this Loop is going to count from zero to
  9709. 8:19:50the last index in the array so remember
  9710. 8:19:54that the index is the position of a
  9711. 8:19:56value in the array the first value is
  9712. 8:19:59index zero the second value is index one
  9713. 8:20:03third value is index 2 and so on So
  9714. 8:20:07Below this let's type four brackets and
  9715. 8:20:11curly
  9716. 8:20:12brackets inside these brackets we're
  9717. 8:20:15going to create the loop variable so
  9718. 8:20:18here we're going to count the
  9719. 8:20:20indexes let index equals zero semicolon
  9720. 8:20:27space and we'll do the loop condition so
  9721. 8:20:31we'll keep looping as long as the index
  9722. 8:20:34is less than or equal to the last index
  9723. 8:20:38so remember that the index starts from
  9724. 8:20:40zero this means that the last index in
  9725. 8:20:43an array will be the number of values in
  9726. 8:20:46the array minus one or the array. length
  9727. 8:20:50minus
  9728. 8:20:52one to do list do
  9729. 8:20:57length minus
  9730. 8:20:591 and here we'll add another semicolon
  9731. 8:21:03and we'll add the increment step which
  9732. 8:21:06is index Plus+ so we'll increase it by
  9733. 8:21:11one every time now inside these curly
  9734. 8:21:14brackets we can do something for each
  9735. 8:21:17index of this array for now let's just
  9736. 8:21:22console. log this index
  9737. 8:21:27and
  9738. 8:21:28save so this will count from 0 to two
  9739. 8:21:33which is exactly all the indexes in this
  9740. 8:21:36array and now using this index we can
  9741. 8:21:39also access the value at this
  9742. 8:21:43index so instead of just the index here
  9743. 8:21:47we can do to-do list and then square
  9744. 8:21:52brackets and between the brackets index
  9745. 8:21:56this will take whatever the index
  9746. 8:21:58variable is and access that index in the
  9747. 8:22:02array so if the index variable is zero
  9748. 8:22:06this will access to-do list index0 which
  9749. 8:22:09is the first value when the index is one
  9750. 8:22:13it will access to-do list index one
  9751. 8:22:16which is the second value and so
  9752. 8:22:19on so for now let's save the value in a
  9753. 8:22:22variable using const value
  9754. 8:22:26and make it equal to to-do list bracket
  9755. 8:22:31index and now let's
  9756. 8:22:33console.log the value instead if we
  9757. 8:22:38save this will Loop through each index
  9758. 8:22:41of the array get the value at each index
  9759. 8:22:45and then display each value which is
  9760. 8:22:47what we see in the console so this
  9761. 8:22:50technique is called looping through an
  9762. 8:22:53array we use a loop to go through each
  9763. 8:22:56value of an array and then we can do
  9764. 8:22:59something with each
  9765. 8:23:01value now I'm going to show you a few
  9766. 8:23:04shortcuts we can use when looping
  9767. 8:23:06through an array instead of checking if
  9768. 8:23:09index is less than or equal to to-do
  9769. 8:23:12list. length minus one we can shorten
  9770. 8:23:16this to index less than to-do list.
  9771. 8:23:21length so this check does the same thing
  9772. 8:23:24as before the loop Loop will stop at
  9773. 8:23:27index equal length minus one but it
  9774. 8:23:30saves us some
  9775. 8:23:32typing another shortcut is instead of
  9776. 8:23:35naming the loop variable index we can
  9777. 8:23:38shorten this to
  9778. 8:23:41I and we'll shorten this as well and
  9779. 8:23:44this as well and that's why we often
  9780. 8:23:47name the loop variable I I means
  9781. 8:23:52index next we're going to learn a
  9782. 8:23:55technique that we can combine with
  9783. 8:23:57looping through an array called the
  9784. 8:24:00accumulator pattern so first let's
  9785. 8:24:04comment out the previous code using
  9786. 8:24:08slashstar and star
  9787. 8:24:10slash and at the
  9788. 8:24:14bottom let's say we have an array of
  9789. 8:24:16numbers like this how do we calculate
  9790. 8:24:19the total of all the numbers in the
  9791. 8:24:22array let's set up this problem let's
  9792. 8:24:26say we have an array of numbers const
  9793. 8:24:31nums equals array and then the numbers 1
  9794. 8:24:371 and
  9795. 8:24:393 and we want to calculate the total of
  9796. 8:24:42all the numbers in this array to
  9797. 8:24:44calculate the total we'll learn the
  9798. 8:24:47accumulator pattern the first step of
  9799. 8:24:50the accumulator pattern is to create a
  9800. 8:24:52variable to store the result so below
  9801. 8:24:56this let's create a variable const total
  9802. 8:25:01and we'll make it equal to zero and the
  9803. 8:25:04next step is to Loop through this array
  9804. 8:25:07and update the result so down here four
  9805. 8:25:12brackets and curly brackets and inside
  9806. 8:25:16the round brackets we'll create the loop
  9807. 8:25:19variable let I or index equals z
  9808. 8:25:25semicolon space and we'll create the
  9809. 8:25:28loop condition I less than nums do
  9810. 8:25:34length and then semicolon and the
  9811. 8:25:37increment step
  9812. 8:25:40i++ so as we learned earlier this Loop
  9813. 8:25:44will go through each index of this array
  9814. 8:25:48and now between these curly
  9815. 8:25:50brackets first let's use the index to
  9816. 8:25:53get each value so we'll type
  9817. 8:25:57nums and then square brackets and access
  9818. 8:26:00the index I and let's save this in a
  9819. 8:26:04variable using
  9820. 8:26:06const num equals this
  9821. 8:26:11value and finally we're going to add
  9822. 8:26:14each number to the total we can type
  9823. 8:26:18total and make it equal to
  9824. 8:26:22itself plus each number and remember
  9825. 8:26:26remember we have a shortcut for this
  9826. 8:26:28which is plus
  9827. 8:26:31equals and finally since we're changing
  9828. 8:26:34this total variable we have to change
  9829. 8:26:36from const to
  9830. 8:26:39let all right so after this Loop we
  9831. 8:26:43should have added each number in the
  9832. 8:26:45array to the total so at the bottom
  9833. 8:26:49let's
  9834. 8:26:50console. log the
  9835. 8:26:54total and Save
  9836. 8:26:57and it will display five which is the
  9837. 8:27:00exact total of all the numbers in this
  9838. 8:27:03array so that's the accumulator pattern
  9839. 8:27:06we first create a variable to store the
  9840. 8:27:09result and then we Loop through the
  9841. 8:27:11array and update the result so notice
  9842. 8:27:16that as we Loop through the array we're
  9843. 8:27:18adding or accumulating the results into
  9844. 8:27:21this variable and that's why this is
  9845. 8:27:24called the accumulator pattern pattern
  9846. 8:27:27and we call this variable the
  9847. 8:27:29accumulator
  9848. 8:27:31variable all right let's do another
  9849. 8:27:34example using the accumulator pattern
  9850. 8:27:37let's say we're given this array of
  9851. 8:27:39numbers again and we want to create a
  9852. 8:27:42copy of the array where each number is
  9853. 8:27:45doubled so if the array is 1 13 we're
  9854. 8:27:50going to create a new array with 2
  9855. 8:27:5426 so as at the bottom we'll create some
  9856. 8:27:58new lines and use the accumulator
  9857. 8:28:00pattern the first step is to create a
  9858. 8:28:03variable to store the result let's type
  9859. 8:28:08const and let's name this
  9860. 8:28:11nums
  9861. 8:28:12doubled and we'll make it equal to an
  9862. 8:28:15array this time so notice in the
  9863. 8:28:18previous example the accumulator
  9864. 8:28:21variable was a number in this example
  9865. 8:28:24the accumulator variable is an array so
  9866. 8:28:27we can use different types of values to
  9867. 8:28:30accumulate the result it all depends on
  9868. 8:28:33the problem we're trying to solve so now
  9869. 8:28:36the next step is to Loop through this
  9870. 8:28:38array and update the result at the
  9871. 8:28:42bottom we'll create a four Loop brackets
  9872. 8:28:46and curly
  9873. 8:28:48brackets inside the round brackets we'll
  9874. 8:28:51create the loop variable let I equal
  9875. 8:28:56zero and we'll create the loop condition
  9876. 8:29:00I less than nums do length and then the
  9877. 8:29:05increment step
  9878. 8:29:08i++ and now between the curly brackets
  9879. 8:29:12we're going to get the number at each
  9880. 8:29:14index so const num equals nums square
  9881. 8:29:21bracket I and then we're going to update
  9882. 8:29:25the result
  9883. 8:29:26so down here we're going to add this
  9884. 8:29:29number doubled to this
  9885. 8:29:31array so remember to add a value to an
  9886. 8:29:35array we can use the method push so
  9887. 8:29:38we'll type
  9888. 8:29:40nums
  9889. 8:29:42doubled do push
  9890. 8:29:46brackets and we're going to add to this
  9891. 8:29:48array the number num multiplied by 2 and
  9892. 8:29:55now at the bottom
  9893. 8:29:56let's
  9894. 8:29:58console. log the result nums
  9895. 8:30:03doubled and
  9896. 8:30:06save so this second Loop will create an
  9897. 8:30:09array where each number is doubled so
  9898. 8:30:12that's how we solve this
  9899. 8:30:14example all right now that we understand
  9900. 8:30:17how to Loop through an array and the
  9901. 8:30:19accumulator pattern let's go
  9902. 8:30:22back and we'll create the second version
  9903. 8:30:26of our to-do list so when we add
  9904. 8:30:29something to the list it will appear on
  9905. 8:30:32the page let's go to our code editor and
  9906. 8:30:36we'll open 11 Todo list.
  9907. 8:30:39HTML and we're also going to open the
  9908. 8:30:42Javascript file so it's this one and for
  9909. 8:30:46the video I'm going to drag it to the
  9910. 8:30:48bottom of my code editor so that I can
  9911. 8:30:51see the files side by side so I'll close
  9912. 8:30:55this for now
  9913. 8:30:56and I'll rearrange my windows a
  9914. 8:31:00bit let's open the tab for this project
  9915. 8:31:04so we'll review what we have so far we
  9916. 8:31:07can click in this text box and type a
  9917. 8:31:10to-do like make dinner and click add and
  9918. 8:31:16it will add this to-do to this array
  9919. 8:31:19which is this array right here we can
  9920. 8:31:22type another to-do like wash dishes and
  9921. 8:31:27click add and it will add it to the end
  9922. 8:31:31of the array so now that we have this
  9923. 8:31:33array of to-dos we want to display it on
  9924. 8:31:37the page to do that we're going to Loop
  9925. 8:31:40through this array and we're going to
  9926. 8:31:42create some HTML code for each to-do and
  9927. 8:31:46then put that code on the web page using
  9928. 8:31:49the Dom so first let's Loop through this
  9929. 8:31:53array to make this easier let's make a
  9930. 8:31:56copy of this array so right click
  9931. 8:32:00copy and then here we'll remove this and
  9932. 8:32:05rightclick and
  9933. 8:32:07paste so this will give us some default
  9934. 8:32:10values to work with so at the top let's
  9935. 8:32:15Loop through this array using a for Loop
  9936. 8:32:19so we'll type four brackets and curly
  9937. 8:32:24brackets and then in inside the round
  9938. 8:32:26brackets we're going to create the loop
  9939. 8:32:29variable let I or index equal zero
  9940. 8:32:35semicolon and the loop condition I less
  9941. 8:32:39than too list do
  9942. 8:32:43length semicolon and then the increment
  9943. 8:32:47step
  9944. 8:32:49i++ so this will Loop through each index
  9945. 8:32:53of this array and then inside the curly
  9946. 8:32:57brackets let's get the string for each
  9947. 8:33:00index so we'll create a variable to save
  9948. 8:33:03it const to-do and we'll make it equal
  9949. 8:33:08to to-do list square brackets and
  9950. 8:33:15I and now for each to-do we're going to
  9951. 8:33:18create some HTML code so we'll create a
  9952. 8:33:22new line and create a variable const
  9953. 8:33:27HTML and make it equal and we'll create
  9954. 8:33:30some HTML code using this to-do using a
  9955. 8:33:35template
  9956. 8:33:36string and then inside we'll create a
  9957. 8:33:39paragraph element so less than P greater
  9958. 8:33:43than and then less than SL P greater
  9959. 8:33:46than inside this paragraph we're going
  9960. 8:33:49to insert this to-do so we'll type
  9961. 8:33:52dollar and curly brackets and
  9962. 8:33:56Todo all right so now we're looping
  9963. 8:33:59through this array and for each to-do we
  9964. 8:34:02created some HTML code and now we're
  9965. 8:34:06going to combine all of this HTML code
  9966. 8:34:09together and put it on the web page to
  9967. 8:34:12combine this HTML together we're going
  9968. 8:34:15to use the accumulator pattern so at the
  9969. 8:34:18top of the loop we'll create a variable
  9970. 8:34:22to store the result let to do list
  9971. 8:34:28Capital
  9972. 8:34:29HTML and we'll make it equal to the
  9973. 8:34:32empty string at the start and then as we
  9974. 8:34:36Loop through the array we're going to
  9975. 8:34:38add this HTML to the variable up here so
  9976. 8:34:43let's create a new line and we'll do
  9977. 8:34:46to-do list Capital
  9978. 8:34:50HTML plus equals
  9979. 8:34:54HTML to combine all of this HTML code
  9980. 8:34:58together so now after the loop let's
  9981. 8:35:02console do log to do list
  9982. 8:35:08HTML to see what we get if we
  9983. 8:35:13save we took this array of strings
  9984. 8:35:16generated paragraph elements for each
  9985. 8:35:18string and then combine them all
  9986. 8:35:21together so now that we have all this
  9987. 8:35:23HTML we can put it on the page using the
  9988. 8:35:27Dom so first let's create an element
  9989. 8:35:31where we can put this
  9990. 8:35:33HTML so inside the HTML file after the
  9991. 8:35:38button we're going to create a different
  9992. 8:35:41type of element called a div element so
  9993. 8:35:45we'll type less than div greater
  9994. 8:35:49than so the div element represents a
  9995. 8:35:52container its purpose is to contain
  9996. 8:35:55other elements inside like paragraphs
  9997. 8:35:58buttons and even other
  9998. 8:36:00divs so in this situation we can't
  9999. 8:36:03actually use a paragraph element because
  10000. 8:36:06in HTML we cannot have a paragraph
  10001. 8:36:10inside another paragraph so that's why
  10002. 8:36:13we have to use a div element here for
  10003. 8:36:16now let's put some text inside this
  10004. 8:36:18element so we can learn more about it
  10005. 8:36:22we'll type text and Save
  10006. 8:36:26so like the paragraph element the div
  10007. 8:36:29element is a block element that means it
  10008. 8:36:32will take up an entire line by itself
  10009. 8:36:35and that's why it appears below the text
  10010. 8:36:38box and the button now one difference
  10011. 8:36:41between a div and a paragraph is that a
  10012. 8:36:44paragraph adds extra space above and
  10013. 8:36:47below the paragraph while a div does not
  10014. 8:36:50add any extra space above or below so it
  10015. 8:36:54doesn't have any default
  10016. 8:36:57styling all right so now that we learned
  10017. 8:37:00the div let's put this HTML code inside
  10018. 8:37:04this div first we need to get this
  10019. 8:37:07element into our JavaScript and to do
  10020. 8:37:11that we'll add a class to this element
  10021. 8:37:14class equals
  10022. 8:37:18js- too Das
  10023. 8:37:22list and then down here let's scroll
  10024. 8:37:25down and we'll put this div element
  10025. 8:37:28inside our JavaScript we can do that
  10026. 8:37:31using document do query
  10027. 8:37:37selector brackets and a string and we'll
  10028. 8:37:40type a DOT to select a class and we'll
  10029. 8:37:44select the class
  10030. 8:37:47js-
  10031. 8:37:49too-
  10032. 8:37:51list so this will get this div element
  10033. 8:37:54and put it in inside our
  10034. 8:37:56JavaScript and then every HTML element
  10035. 8:37:59has a property called dot inner Capital
  10036. 8:38:05HTML which controls the HTML inside this
  10037. 8:38:09element and now to put the to-do list
  10038. 8:38:12HTML into this div element we're going
  10039. 8:38:15to make the inner
  10040. 8:38:17HTML equal to the
  10041. 8:38:21to-do list HTML
  10042. 8:38:25and now let's save this
  10043. 8:38:28file and also save the HTML
  10044. 8:38:32file so this code will take the HTML
  10045. 8:38:36that we created using the loop and put
  10046. 8:38:39it into the div element and that's how
  10047. 8:38:42we make them show up on the page so now
  10048. 8:38:45let's try adding another to-do we'll
  10049. 8:38:48type watch YouTube and click
  10050. 8:38:53add and unfortunately
  10051. 8:38:56this new Todo doesn't show up on the
  10052. 8:38:59page and that's because when we click
  10053. 8:39:01add it will run this add Todo function
  10054. 8:39:05and update the list but we didn't
  10055. 8:39:08actually update the HTML on the
  10056. 8:39:11page so to update the HTML we need to
  10057. 8:39:15run all this code again every time we
  10058. 8:39:18add a too so let's create a function for
  10059. 8:39:22this code so we can reuse it so at the
  10060. 8:39:25top we'll create a
  10061. 8:39:30function and let's name this
  10062. 8:39:33render
  10063. 8:39:34to-do list brackets and curly
  10064. 8:39:39brackets so the word render means to
  10065. 8:39:43display something on the page so we'll
  10066. 8:39:46put all of this code into this function
  10067. 8:39:49so we can delete this curly bracket and
  10068. 8:39:53then at the end we'll type a closed
  10069. 8:39:56curly bracket and let's reformat this
  10070. 8:39:59code by selecting these lines and then
  10071. 8:40:02press tab to add an
  10072. 8:40:05indent so now let's use this function at
  10073. 8:40:09the start we'll display this to-do list
  10074. 8:40:12on the page so we'll call this function
  10075. 8:40:15by typing render
  10076. 8:40:19to-do list and
  10077. 8:40:22brackets and then let's scroll down
  10078. 8:40:26every time we add a to-do to the list
  10079. 8:40:29we're also going to display the list
  10080. 8:40:31again so at the bottom
  10081. 8:40:34render to do list and brackets again so
  10082. 8:40:41if we
  10083. 8:40:42save at the start it will display the
  10084. 8:40:44list on the page and then if we add it
  10085. 8:40:48to the list like watch YouTube and click
  10086. 8:40:53add it will add it to the list and also
  10087. 8:40:57display it on the page so that's how we
  10088. 8:41:00create the second version of the to-do
  10089. 8:41:02list
  10090. 8:41:03project by the way if we scroll up to
  10091. 8:41:06the render Todo list function this
  10092. 8:41:09technique is called generating the
  10093. 8:41:12HTML instead of writing all the HTML by
  10094. 8:41:16hand up here we looped through an array
  10095. 8:41:19and we generated the HTML using
  10096. 8:41:22JavaScript
  10097. 8:41:25so when creating websites with
  10098. 8:41:26JavaScript we usually follow a
  10099. 8:41:29three-step process first we save the
  10100. 8:41:33data in this case we saved our data as
  10101. 8:41:36an
  10102. 8:41:37array next we use the data to generate
  10103. 8:41:41the HTML which we did here and finally
  10104. 8:41:45we make the website interactive which
  10105. 8:41:48we'll do later in this
  10106. 8:41:50lesson this is the main idea of
  10107. 8:41:53JavaScript and we follow follow this
  10108. 8:41:55overall process when creating
  10109. 8:41:58websites the rock paper scissors project
  10110. 8:42:00from the previous lessons was a bit
  10111. 8:42:03different because all the HTML we needed
  10112. 8:42:06was already there we just had to modify
  10113. 8:42:08it with
  10114. 8:42:10JavaScript so this process we're using
  10115. 8:42:12for the to-do list is one step above
  10116. 8:42:15what we did for rock paper scissors we
  10117. 8:42:18need to generate some
  10118. 8:42:20HTML and this is a standard process for
  10119. 8:42:23creating websites we're going to use
  10120. 8:42:25this process for the to-do list and
  10121. 8:42:28later the Amazon Project as well as
  10122. 8:42:30websites that you build Beyond this
  10123. 8:42:35course let's take a break before
  10124. 8:42:37continuing with the rest of this lesson
  10125. 8:42:40here are some exercises that will help
  10126. 8:42:42you practice what we learned so far
  10127. 8:43:07now let's create the final version of
  10128. 8:43:09the to-do list we'll go back to
  10129. 8:43:12superimple
  10130. 8:43:14dodev and then here we'll scroll down
  10131. 8:43:18and we'll create this final version so
  10132. 8:43:21in this version we can type a too like
  10133. 8:43:24make
  10134. 8:43:26dinner and we can also add a due
  10135. 8:43:29date and when we click add each to-do
  10136. 8:43:33will now have a name and a due date and
  10137. 8:43:37they will also have a delete button that
  10138. 8:43:39we can click to remove it from the list
  10139. 8:43:43so let's build this out one feature at a
  10140. 8:43:46time we'll start by creating this delete
  10141. 8:43:49button we'll go into our code and
  10142. 8:43:52remember we're generating the HTML
  10143. 8:43:55for the to-do list so for each value in
  10144. 8:43:58the array we create a paragraph element
  10145. 8:44:02so to add a delete button we just have
  10146. 8:44:05to add a delete button inside here so
  10147. 8:44:09inside the paragraph we'll create a less
  10148. 8:44:12than button greater than and then less
  10149. 8:44:16than SLB button greater than and then
  10150. 8:44:20inside this button we'll put the text
  10151. 8:44:24delete
  10152. 8:44:26let's
  10153. 8:44:27save and in the console you'll see that
  10154. 8:44:30the HTML we generated now has a delete
  10155. 8:44:33button inside the paragraph and that's
  10156. 8:44:36what shows up on the
  10157. 8:44:38page next we're going to reformat this
  10158. 8:44:41HTML to make it a little easier to read
  10159. 8:44:44so we'll create some new lines here and
  10160. 8:44:47at the
  10161. 8:44:48end and let's put the content of the
  10162. 8:44:51paragraph on its own line like this
  10163. 8:44:55and also put the delete button on its
  10164. 8:44:57own line so remember that template
  10165. 8:45:00strings have a feature called multi-line
  10166. 8:45:02strings so this is perfect for writing
  10167. 8:45:05HTML
  10168. 8:45:08code the last step in the process is to
  10169. 8:45:11make the website
  10170. 8:45:12interactive so next let's make our
  10171. 8:45:15delete button do something when we click
  10172. 8:45:17it to do that we use the onclick
  10173. 8:45:20attribute as usual so inside this button
  10174. 8:45:25we're going to add an
  10175. 8:45:28onclick attribute equals double quotes
  10176. 8:45:33and then inside these double quotes we
  10177. 8:45:35can run some JavaScript when we click
  10178. 8:45:37this button so this works the exact same
  10179. 8:45:41way as what we've been doing so far in
  10180. 8:45:44this course the only difference now is
  10181. 8:45:47that here we're generating this HTML
  10182. 8:45:50using
  10183. 8:45:51JavaScript okay so when we click one of
  10184. 8:45:54these delete buttons we want to remove
  10185. 8:45:56this to-do from the list earlier in this
  10186. 8:46:00lesson we learned how to remove a value
  10187. 8:46:02from an array using a method called
  10188. 8:46:06splice so inside this onclick attribute
  10189. 8:46:09we're going to type the to-do list dot
  10190. 8:46:15splice and
  10191. 8:46:18brackets so splice takes two numbers the
  10192. 8:46:22first number is the index that we want
  10193. 8:46:24to remove
  10194. 8:46:25so if we click this delete button how do
  10195. 8:46:28we know which index to
  10196. 8:46:30remove well if we look at our Loop up
  10197. 8:46:33here we already have the index it's this
  10198. 8:46:36variable I so we're going to insert I
  10199. 8:46:40into this string and we can do that by
  10200. 8:46:43typing dollar curly brackets and
  10201. 8:46:47I now splice also takes another number
  10202. 8:46:51so we'll type comma and the second
  10203. 8:46:54number is how many values we want to
  10204. 8:46:56remove so here we just want to remove
  10205. 8:47:00one value at this
  10206. 8:47:02index and finally because we updated the
  10207. 8:47:05to-do list we need to display the list
  10208. 8:47:08again on the page to display the list we
  10209. 8:47:12can use this function render to do list
  10210. 8:47:15so down here we're going to type render
  10211. 8:47:20Todo list and brackets
  10212. 8:47:25now if we
  10213. 8:47:27save you can see that in the generated
  10214. 8:47:30HTML when we click the first delete
  10215. 8:47:33button it will remove the value at
  10216. 8:47:36index0 which is the first value if we
  10217. 8:47:40click the second button it will remove
  10218. 8:47:43the value at index one which is the
  10219. 8:47:45second value so we're basically
  10220. 8:47:48generating all this HTML using a loop so
  10221. 8:47:52now if we click this delete button
  10222. 8:47:55it will remove it from the list and then
  10223. 8:47:57display the updated list on the page so
  10224. 8:48:01that's how we create the delete
  10225. 8:48:03button next let's go back to superimple
  10226. 8:48:07dodev and we're going to create the due
  10227. 8:48:10date
  10228. 8:48:10feature so first let's create this date
  10229. 8:48:14selector in our
  10230. 8:48:16HTML we'll go to our HTML file and then
  10231. 8:48:20under the text box we're going to add
  10232. 8:48:23another input element
  10233. 8:48:25so less than input greater than and to
  10234. 8:48:30make it a date selector input elements
  10235. 8:48:33have an attribute called
  10236. 8:48:35type so this attribute determines what
  10237. 8:48:38kind of input it is if the type is text
  10238. 8:48:42which is the default value it's going to
  10239. 8:48:44be a
  10240. 8:48:46textbox now if we change the type to
  10241. 8:48:50date and
  10242. 8:48:52save it's going to create a date
  10243. 8:48:57selector now let's go back into our
  10244. 8:48:59JavaScript code and first we're going to
  10245. 8:49:02scroll up and remember that right now
  10246. 8:49:05our to-do list is just an array of
  10247. 8:49:08strings but this time each to-do is also
  10248. 8:49:11going to have a due date so how do we
  10249. 8:49:14group the name and the due date together
  10250. 8:49:17we can do that using an object so here
  10251. 8:49:21instead of just a string we're going to
  10252. 8:49:24create an an object and we'll give it a
  10253. 8:49:27name property which is make dinner and
  10254. 8:49:31we'll give it a due date property and
  10255. 8:49:35we'll make the due date a
  10256. 8:49:38string so you can type whatever date you
  10257. 8:49:40have on the page for me it's 20
  10258. 8:49:4722-12
  10259. 8:49:49d22 and I'll end this object with a
  10260. 8:49:52curly bracket and I'll turn this to do
  10261. 8:49:55into an object as well so open curly
  10262. 8:49:58bracket
  10263. 8:50:02name and do
  10264. 8:50:06date
  10265. 8:50:072022
  10266. 8:50:09D12
  10267. 8:50:11d22 and closed curly
  10268. 8:50:14bracket now that each to-do is an object
  10269. 8:50:17we need to update the rest of our code
  10270. 8:50:20let's scroll down and we'll start with
  10271. 8:50:23the render function
  10272. 8:50:25so here inside the loop each to-do is no
  10273. 8:50:28longer just a string it's an object so
  10274. 8:50:32let's change this variable name to to-do
  10275. 8:50:36object to make it more clear and now we
  10276. 8:50:39need to get the name and due date out of
  10277. 8:50:43this object so we'll type const name
  10278. 8:50:48equals to do object. name and const do
  10279. 8:50:56date equals to do
  10280. 8:51:00object. do
  10281. 8:51:03date and now instead of inserting to-do
  10282. 8:51:06which no longer exists we're going to
  10283. 8:51:08insert the
  10284. 8:51:10name and we're also going to insert the
  10285. 8:51:14DU
  10286. 8:51:16date if we
  10287. 8:51:18save now each to-do will also display a
  10288. 8:51:22due
  10289. 8:51:23date before before we continue we're
  10290. 8:51:25going to review a shortcut we can use
  10291. 8:51:27with objects called
  10292. 8:51:30destructuring so here you'll notice the
  10293. 8:51:32variable name is the same as the
  10294. 8:51:34property name we actually have a
  10295. 8:51:36shortcut for this using
  10296. 8:51:40const curly brackets
  10297. 8:51:43name equals Todo
  10298. 8:51:47object so this is called destructuring
  10299. 8:51:51this takes the name property out of the
  10300. 8:51:53object and and puts it in a variable
  10301. 8:51:56also called name and this is a shortcut
  10302. 8:51:59for this line up here so we can comment
  10303. 8:52:02out this line we can also use multiple
  10304. 8:52:05properties in this shortcut to also take
  10305. 8:52:08the due date out of the object we can
  10306. 8:52:11just add a comma and do
  10307. 8:52:15date so this will do the same thing as
  10308. 8:52:18this line of code and we can comment
  10309. 8:52:21this
  10310. 8:52:22out if we save
  10311. 8:52:25everything still
  10312. 8:52:26works next let's scroll down and we're
  10313. 8:52:30going to update our add Todo function so
  10314. 8:52:34this time when we click the add button
  10315. 8:52:36we're going to get the name and the due
  10316. 8:52:39date and put it into our list so let's
  10317. 8:52:42get the due date out of this date
  10318. 8:52:44selector first we can do that using the
  10319. 8:52:47Dom like we did here so let's add a
  10320. 8:52:51class attribute so we can get it using
  10321. 8:52:54queries selector
  10322. 8:52:56js- do
  10323. 8:52:59dd-
  10324. 8:53:01input and then down here let's create a
  10325. 8:53:05variable
  10326. 8:53:06const date input element and make it
  10327. 8:53:12equal to
  10328. 8:53:15document. query
  10329. 8:53:18selector and a string Dot and then the
  10330. 8:53:22class we want to select which which is
  10331. 8:53:24this class
  10332. 8:53:26js-
  10333. 8:53:28do-
  10334. 8:53:30date-
  10335. 8:53:33input so this will get the date selector
  10336. 8:53:35element and put it into our
  10337. 8:53:38JavaScript and now below this let's get
  10338. 8:53:41the due date out of the
  10339. 8:53:43element we can do that using the value
  10340. 8:53:46property again so let's create a
  10341. 8:53:49variable first const do date and we'll
  10342. 8:53:54make it equal to the date input
  10343. 8:54:00element.
  10344. 8:54:02value so this will get the date that we
  10345. 8:54:04selected and save it in this
  10346. 8:54:08variable and now when we add a to-do to
  10347. 8:54:11our list instead of just a name we're
  10348. 8:54:14going to add an object so open curly
  10349. 8:54:17bracket We'll add a name property and
  10350. 8:54:21the value will be the name variable up
  10351. 8:54:23here so name and then comma and a doe
  10352. 8:54:29date
  10353. 8:54:30property and the value will be this due
  10354. 8:54:33date variable up here so due date and
  10355. 8:54:38then a closed curly
  10356. 8:54:40bracket and remember that objects also
  10357. 8:54:43have a shortcut for this if the property
  10358. 8:54:47and the variable name are the same we
  10359. 8:54:49can just type it out once like
  10360. 8:54:52this this will do the same thing as this
  10361. 8:54:55line above same thing with the second
  10362. 8:54:58line if the property and the variable
  10363. 8:55:00name are the same we can just type a
  10364. 8:55:03comma and type it out
  10365. 8:55:07once this is called the shorthand
  10366. 8:55:10property syntax so these two lines do
  10367. 8:55:13the same thing as these lines above so
  10368. 8:55:16we can comment this out using double
  10369. 8:55:18slash and double
  10370. 8:55:21slash so that's all the code that we
  10371. 8:55:23needed to update now let's save our
  10372. 8:55:26Javascript
  10373. 8:55:28file and we'll also save our HTML
  10374. 8:55:32file and let's go to our project We'll
  10375. 8:55:35add a new to-do like watch
  10376. 8:55:40YouTube and select a
  10377. 8:55:43date and click
  10378. 8:55:46add and scroll down you can see that it
  10379. 8:55:49added our new to-do with a name due date
  10380. 8:55:53and a delete button
  10381. 8:55:55if we click this delete button it will
  10382. 8:55:58remove that to-do if we click this one
  10383. 8:56:01it will also remove the correct
  10384. 8:56:04to-do and that's actually all the
  10385. 8:56:06functionality that we need for this
  10386. 8:56:08project so let's go into our code and
  10387. 8:56:11we're going to clean up by removing the
  10388. 8:56:14console.log
  10389. 8:56:15here and the console.log up
  10390. 8:56:21here and Save
  10391. 8:56:24finally let's add CSS to our project to
  10392. 8:56:28make it look like the final design we'll
  10393. 8:56:31go into our code editor and scroll to
  10394. 8:56:34the top and instead of writing all the
  10395. 8:56:37CSS in here let's put the CSS in a
  10396. 8:56:41separate file just like our JavaScript
  10397. 8:56:44so we'll click here to show our
  10398. 8:56:47files and then find the Styles folder
  10399. 8:56:51and we'll click the Styles folder and
  10400. 8:56:53click this icon to create a new file
  10401. 8:56:57inside and we'll name this file
  10402. 8:57:0111-
  10403. 8:57:02[Music]
  10404. 8:57:04to-do-list
  10405. 8:57:06docss and press
  10406. 8:57:09enter and for the video I'm going to
  10407. 8:57:12take this CSS file and put it at the
  10408. 8:57:15bottom of my code editor so I can see
  10409. 8:57:17these files side by side and now I'll
  10410. 8:57:21click here to close
  10411. 8:57:22it and will load this CSS into our
  10412. 8:57:27HTML to do that we can use a link
  10413. 8:57:30element so inside the head section let's
  10414. 8:57:34type less than link greater than and
  10415. 8:57:39we're going to give it two attributes
  10416. 8:57:42Rel equals style
  10417. 8:57:46sheet and
  10418. 8:57:49hre equals the file that we want to load
  10419. 8:57:53so we created this this in the Styles
  10420. 8:57:55folder so we'll type Styles and then a
  10421. 8:57:59slash to go into the folder and we load
  10422. 8:58:02this file
  10423. 8:58:0411-
  10424. 8:58:06too- list.
  10425. 8:58:10CSS and
  10426. 8:58:13save now let's style our project one by
  10427. 8:58:16one until it matches the final design
  10428. 8:58:19we'll start with the text on the page
  10429. 8:58:22we're going to change the font so in
  10430. 8:58:24here let's type
  10431. 8:58:27body curly brackets and then we'll
  10432. 8:58:30change the font Das family colon Arial
  10433. 8:58:37and
  10434. 8:58:38save so that looks pretty good now let's
  10435. 8:58:41work on styling the to-do list first
  10436. 8:58:45let's add some more to-dos to the list
  10437. 8:58:47so we'll type
  10438. 8:58:50watch
  10439. 8:58:51YouTube and then we'll select a date
  10440. 8:58:55and click
  10441. 8:58:56add and we're going to learn how to
  10442. 8:58:58style our list to look like this so
  10443. 8:59:01notice that this list creates a sort of
  10444. 8:59:05grid we're going to create this grid
  10445. 8:59:07using a feature called CSS grid so let's
  10446. 8:59:12go into our HTML
  10447. 8:59:15first we're going to add a class to this
  10448. 8:59:18div element so we can style
  10449. 8:59:20it to-do Das grid R and let's
  10450. 8:59:26save and inside our CSS we're going to
  10451. 8:59:30style this class using dot Todo Das grid
  10452. 8:59:36and curly
  10453. 8:59:38brackets and to turn this into a grid
  10454. 8:59:41we're going to use the property
  10455. 8:59:44display and
  10456. 8:59:47grid and we're going to add another
  10457. 8:59:49property called grid Das template Dash
  10458. 8:59:56columns so this basically sets how many
  10459. 8:59:59columns are in our grid the number of
  10460. 9:00:02values that we give it is the number of
  10461. 9:00:05columns for example if we give it 200
  10462. 9:00:10pixels 150 pixels and 100
  10463. 9:00:15pixels this will create three columns in
  10464. 9:00:19the grid the First Column will be 200
  10465. 9:00:22pixels the second Colum column will be
  10466. 9:00:24150 pixels and the third column will be
  10467. 9:00:28100
  10468. 9:00:29pixels and now each element inside this
  10469. 9:00:32div will go into the columns so the
  10470. 9:00:36first element will go in the First
  10471. 9:00:37Column second element will go in the
  10472. 9:00:40second column and the third element will
  10473. 9:00:42go in the third column if we have more
  10474. 9:00:45than three elements it will wrap around
  10475. 9:00:48and create a new
  10476. 9:00:50row so let's
  10477. 9:00:52save and and unfortunately that didn't
  10478. 9:00:55quite work and that's because if we go
  10479. 9:00:58back to our
  10480. 9:01:00JavaScript remember that each to-do is
  10481. 9:01:04one paragraph element so this entire
  10482. 9:01:07to-do with the name due date and delete
  10483. 9:01:10button will go inside the First Column
  10484. 9:01:13and then the next entire to-do will go
  10485. 9:01:16inside the second column but what we
  10486. 9:01:19actually want is for the name to go in
  10487. 9:01:21column 1 due date column 2
  10488. 9:01:24and delete button column 3 to do this
  10489. 9:01:28we're going to separate this element
  10490. 9:01:30into three elements so that each element
  10491. 9:01:32will go into each
  10492. 9:01:35column so instead of one
  10493. 9:01:38paragraph let's actually use a div
  10494. 9:01:41element so less than div greater than
  10495. 9:01:45and less than slash div greater than and
  10496. 9:01:49in between we'll insert the name
  10497. 9:01:56so the reason we use a div element is
  10498. 9:01:58because div elements don't have any
  10499. 9:02:01default styling so it's a little easier
  10500. 9:02:03to style with
  10501. 9:02:05CSS for the due date let's also create a
  10502. 9:02:08div element so less than
  10503. 9:02:12div and then at the end less than slash
  10504. 9:02:16div greater than and finally for the
  10505. 9:02:20delete button we already have an element
  10506. 9:02:23so so let's just reformat these lines
  10507. 9:02:26we'll select these lines and then press
  10508. 9:02:29shift tab to remove the
  10509. 9:02:32indent let's remove the extra line and
  10510. 9:02:35we basically separated each to-do into
  10511. 9:02:38three
  10512. 9:02:39elements if we
  10513. 9:02:41save now the grid is working because
  10514. 9:02:44this element goes into the First Column
  10515. 9:02:48this element goes into the second column
  10516. 9:02:50and the button goes into the third
  10517. 9:02:52column
  10518. 9:02:54and that's how we lay out these elements
  10519. 9:02:57in a grid like the final design next
  10520. 9:03:01let's also lay out the inputs and the
  10521. 9:03:03add button in a grid we'll go to our
  10522. 9:03:08HTML and let's put these three elements
  10523. 9:03:10in a div so we can use grid so we'll
  10524. 9:03:14create a less than div greater
  10525. 9:03:18than and then
  10526. 9:03:20inside let's remove this and we'll add
  10527. 9:03:24it at the end less than SL div and let's
  10528. 9:03:29reformat these lines by selecting them
  10529. 9:03:33and then press tab to add an
  10530. 9:03:36indent and now let's make this div a
  10531. 9:03:39grid as well let's go back to our CSS
  10532. 9:03:43and we'll add a class to this div class
  10533. 9:03:48equals let's call it to-do Das input d
  10534. 9:03:55dgrid and instead of writing all this
  10535. 9:03:57CSS again we can use a feature of CSS so
  10536. 9:04:02here we're going to type a comma and
  10537. 9:04:06enter so when we type a comma we can
  10538. 9:04:09style multiple classes at the same time
  10539. 9:04:12so we're also going to style dot
  10540. 9:04:17too- input Das grid if we save the CSS
  10541. 9:04:23file
  10542. 9:04:24and let's also save the HTML
  10543. 9:04:27file now the input and the add button
  10544. 9:04:30are also laid out in the same
  10545. 9:04:33grid next let's adjust the spacing
  10546. 9:04:37inside our grid so grid has some
  10547. 9:04:39properties for this we'll go to our CSS
  10548. 9:04:43press enter and we'll use the property
  10549. 9:04:47column Das Gap so this sets the spacing
  10550. 9:04:52between the Columns of the grid so let's
  10551. 9:04:56make this colon 10 pixels and
  10552. 9:05:01save so now it will add 10 pixels
  10553. 9:05:04between each column let's add some
  10554. 9:05:06spacing between the rows as well and
  10555. 9:05:09grid has another property for this row
  10556. 9:05:13Das Gap colon 10 pixels and
  10557. 9:05:19save so that will add 10 pixels of space
  10558. 9:05:23between the rows now notice it didn't
  10559. 9:05:25add space between these two rows and
  10560. 9:05:28that's because these two are separate
  10561. 9:05:30grids this is the to-do list and this is
  10562. 9:05:34the to-do input
  10563. 9:05:36grid so to add space between them we
  10564. 9:05:39just going to use
  10565. 9:05:41margin at the bottom we'll style dot
  10566. 9:05:46too- input Das
  10567. 9:05:50grid and we'll just add some margin
  10568. 9:05:53under this grid so margin Das bottom
  10569. 9:05:59colon 10 pixels and
  10570. 9:06:03save all right now let's move on to
  10571. 9:06:06styling the inputs and the buttons we'll
  10572. 9:06:09start with the inputs first let's add a
  10573. 9:06:13class so we can style them here let's
  10574. 9:06:16add a class name Das
  10575. 9:06:21input and down here let's let's also add
  10576. 9:06:24another class
  10577. 9:06:26do- dat Das
  10578. 9:06:30input let's
  10579. 9:06:32save and now inside our CSS we'll style
  10580. 9:06:36both of these
  10581. 9:06:38inputs so we'll style do
  10582. 9:06:41name- input curly brackets and first
  10583. 9:06:46let's make the text bigger like the
  10584. 9:06:48final design we'll set the font Das size
  10585. 9:06:54colon 15 pixels and save okay so that
  10586. 9:06:59looks pretty good now let's add spacing
  10587. 9:07:02on the inside of this input so we'll add
  10588. 9:07:07padding six pixels and
  10589. 9:07:11save and that looks pretty good let's
  10590. 9:07:14move on to the due date input so we
  10591. 9:07:17actually want the same Styles as the
  10592. 9:07:19name input so again we can use that
  10593. 9:07:22feature of CSS we'll type a comma and
  10594. 9:07:27then dot
  10595. 9:07:29do-
  10596. 9:07:31date- input so now these Styles will be
  10597. 9:07:35applied to this class and to this class
  10598. 9:07:39let's
  10599. 9:07:41save and that looks pretty good to me
  10600. 9:07:44let's move on and style the
  10601. 9:07:47buttons we'll go into our HTML and find
  10602. 9:07:51the add button let's add a class so we
  10603. 9:07:54can style
  10604. 9:07:55it and we'll use add-
  10605. 9:08:00too- button let's
  10606. 9:08:03save and then inside our CSS we'll style
  10607. 9:08:07that class using dot add-
  10608. 9:08:12too- button curly
  10609. 9:08:16brackets and let's start with the
  10610. 9:08:18background color we'll set the
  10611. 9:08:21background-color
  10612. 9:08:24colon
  10613. 9:08:25green and
  10614. 9:08:28save now let's do the text
  10615. 9:08:31color color colon
  10616. 9:08:35white and
  10617. 9:08:37save now let's remove the border around
  10618. 9:08:40our button using border colon
  10619. 9:08:46none
  10620. 9:08:47save and let's make the text a little
  10621. 9:08:50bigger so we'll do font
  10622. 9:08:54D size colon 15
  10623. 9:08:58pixels and
  10624. 9:09:01save and the last thing we'll add is
  10625. 9:09:03when we put our Mouse over the button it
  10626. 9:09:06turns into this pointer icon we can do
  10627. 9:09:09that using
  10628. 9:09:11cursor colon
  10629. 9:09:15pointer and
  10630. 9:09:18save next let's style the delete buttons
  10631. 9:09:22so these delete buttons are created in
  10632. 9:09:24our JavaScript we'll go into our
  10633. 9:09:27JavaScript
  10634. 9:09:28file and then here let's add a class to
  10635. 9:09:32each of the delete buttons so we can
  10636. 9:09:34style them so we'll add class equals
  10637. 9:09:39double quotes and then delete Das
  10638. 9:09:44too-
  10639. 9:09:46button let's
  10640. 9:09:48save and then inside our
  10641. 9:09:51CSS at the bottom we style dot delete
  10642. 9:09:57Das too Das button curly
  10643. 9:10:02brackets and let's start with the
  10644. 9:10:04background color so we'll set background
  10645. 9:10:08Das color colon and for this design I
  10646. 9:10:13use the color dark
  10647. 9:10:16red and
  10648. 9:10:18save so that will style our delete
  10649. 9:10:21buttons now let's change change the text
  10650. 9:10:24color to white so we'll use color colon
  10651. 9:10:29white and
  10652. 9:10:31save now let's remove the Border using
  10653. 9:10:36border colon
  10654. 9:10:39none
  10655. 9:10:41save and let's also make the text bigger
  10656. 9:10:45using font Das size colon 15
  10657. 9:10:51pixels save
  10658. 9:10:53let's add this pointer icon using cursor
  10659. 9:10:58colon
  10660. 9:11:00pointer save and let's add some padding
  10661. 9:11:04at the top and bottom of the button so
  10662. 9:11:08we'll add
  10663. 9:11:11padding-top colon 10 pixels and padding
  10664. 9:11:17Dash bottom colon 10 pixels and Save
  10665. 9:11:25so that's all the Styles we need for the
  10666. 9:11:27delete button and we can also close the
  10667. 9:11:29console to see the entire list however
  10668. 9:11:33now we have another problem which is
  10669. 9:11:36this text and this button are not
  10670. 9:11:38vertically aligned in the center to
  10671. 9:11:41vertically align in the center we're
  10672. 9:11:44going to go back to the grid Styles so
  10673. 9:11:47we'll scroll
  10674. 9:11:48up to the to-do
  10675. 9:11:51grid and CSS grid has another property
  10676. 9:11:54for vertical alignment and that is align
  10677. 9:11:59Das items Colon
  10678. 9:12:03Center to vertically align in the center
  10679. 9:12:08if we
  10680. 9:12:09save now the text is aligned with the
  10681. 9:12:12button but now we have a new problem our
  10682. 9:12:15add button is no longer stretching to be
  10683. 9:12:18the same as the inputs so by default
  10684. 9:12:22align items is
  10685. 9:12:24stretch that's why this button stretches
  10686. 9:12:27to match the inputs and now that we
  10687. 9:12:29changed it to align center it's no
  10688. 9:12:32longer going to stretch so to solve this
  10689. 9:12:35problem we just have to make this grid
  10690. 9:12:39stretch so down here in the to-do input
  10691. 9:12:43grid Styles we'll change align Das items
  10692. 9:12:49back to
  10693. 9:12:51stretch and Save
  10694. 9:12:53save and that will cause this Grid at
  10695. 9:12:56the top to stretch vertically again and
  10696. 9:13:00that's it that's all the styling that we
  10697. 9:13:02need and now we finished this to-do list
  10698. 9:13:06project in the rest of this lesson we're
  10699. 9:13:08going to learn more details about arrays
  10700. 9:13:11and
  10701. 9:13:11Loops so I'm going to put this back in
  10702. 9:13:15the same
  10703. 9:13:16window and I'll rearrange this
  10704. 9:13:19window and now let's go back to 11
  10705. 9:13:22arrays and loops.
  10706. 9:13:24HTML and we'll open the tab for that
  10707. 9:13:28file and we can also close these other
  10708. 9:13:31files for
  10709. 9:13:33now and first let's comment out the
  10710. 9:13:36previous code
  10711. 9:13:39using
  10712. 9:13:41slashstar and at the bottom star
  10713. 9:13:45slash and we'll scroll down and
  10714. 9:13:50save the first detail we're going to
  10715. 9:13:52learn is that that arrays are references
  10716. 9:13:55so in the object lesson we learned that
  10717. 9:13:58objects are references arrays work the
  10718. 9:14:01same way so for example if we create an
  10719. 9:14:05array
  10720. 9:14:07const array 1 equals the array 1 2
  10721. 9:14:153 this variable doesn't actually contain
  10722. 9:14:18the values 1 2 3 this array is saved
  10723. 9:14:23somewhere in the computer's memory and
  10724. 9:14:25this variable only contains a reference
  10725. 9:14:28that points to where this array is in
  10726. 9:14:31memory and that means if we create a
  10727. 9:14:34copy of this variable
  10728. 9:14:37const array 2 equals array
  10729. 9:14:431 array 2 doesn't contain a copy of the
  10730. 9:14:47values it actually just contains a copy
  10731. 9:14:50of the reference so now both of these
  10732. 9:14:53variables are pointing to the same array
  10733. 9:14:57so if we did array
  10734. 9:15:002. push
  10735. 9:15:03for and then we
  10736. 9:15:08console.log array 1 and we
  10737. 9:15:15console.log array
  10738. 9:15:17two and
  10739. 9:15:20save it will show us that even though we
  10740. 9:15:23added four to array two array 1 and two
  10741. 9:15:27were both modified and that's because
  10742. 9:15:31arrays are references so both of these
  10743. 9:15:34point to the same array in memory to
  10744. 9:15:37avoid this Behavior we can create a copy
  10745. 9:15:40of the values in the array using a
  10746. 9:15:43method called slice so here if we did
  10747. 9:15:48array
  10748. 9:15:491. slice instead
  10749. 9:15:53this will make a copy of the values in
  10750. 9:15:56Array one and now these two variables
  10751. 9:15:59point to two different arrays if we
  10752. 9:16:04save now you'll see that this line only
  10753. 9:16:07affected array
  10754. 9:16:09two the second detail we're going to
  10755. 9:16:11learn is a shortcut called
  10756. 9:16:14destructuring so at the bottom here
  10757. 9:16:17let's create a new array
  10758. 9:16:20const array 3 3 equals the array 1 2
  10759. 9:16:293 and let's say we want to get the first
  10760. 9:16:32value and the second value out of this
  10761. 9:16:35array one way to do this is to use the
  10762. 9:16:38index like this another way is to use a
  10763. 9:16:42shortcut called
  10764. 9:16:44destructuring so here we're going to
  10765. 9:16:47replace this with square brackets and
  10766. 9:16:51now we can type some variable name
  10767. 9:16:53if we type first value it will take the
  10768. 9:16:58first value and save it in a variable
  10769. 9:17:01called first value if we type comma and
  10770. 9:17:05second value it will take the second
  10771. 9:17:09value and save it in this variable so
  10772. 9:17:13this is an easier way to get values out
  10773. 9:17:16of an array and save them inside a
  10774. 9:17:20variable next we're going to learn more
  10775. 9:17:22details about loops so Loops have two
  10776. 9:17:26features called break and continue break
  10777. 9:17:30lets us exit a loop early for example
  10778. 9:17:35let's create some new lines and let's
  10779. 9:17:37create a loop that counts from 1 to 10
  10780. 9:17:41so as usual we can use a four
  10781. 9:17:45Loop let I =
  10782. 9:17:481 I less than or equal to 10
  10783. 9:17:53and I
  10784. 9:17:56++ and we'll
  10785. 9:17:59console. log I and
  10786. 9:18:04save so this will count from 1 to
  10787. 9:18:0810 so now let's say that when I is 8 we
  10788. 9:18:12want to stop this Loop early so first
  10789. 9:18:16let's check if I is equal to 8
  10790. 9:18:24if it is equal to 8 we're going to stop
  10791. 9:18:27this Loop by typing break and
  10792. 9:18:31semicolon if we
  10793. 9:18:33save once this reaches eight break will
  10794. 9:18:37cause this Loop to stop and now it only
  10795. 9:18:40counts up to eight so that's the break
  10796. 9:18:44feature of a loop it lets us stop a loop
  10797. 9:18:47early the next feature is called
  10798. 9:18:50continue continue lets us skip one
  10799. 9:18:53iteration of the loop or one run of the
  10800. 9:18:56loop for example let's say we want to
  10801. 9:18:59skip the number three so at the top we
  10802. 9:19:03can check if I is equal to
  10803. 9:19:10three and if it is we can use the code
  10804. 9:19:15continue this will skip the rest of this
  10805. 9:19:18code and go back up to the top of the
  10806. 9:19:21loop and do i = 4 if we
  10807. 9:19:26save this will skip three in the loop so
  10808. 9:19:31continue let us Skip One iteration of
  10809. 9:19:34the loop so now let's say that we want
  10810. 9:19:36to skip a number if it's divisible by
  10811. 9:19:39three divisible by three means the
  10812. 9:19:42number can be divided evenly into three
  10813. 9:19:45for example 3 and six are both divisible
  10814. 9:19:49by three but s is not divisible by three
  10815. 9:19:54because there's one remaining this is
  10816. 9:19:57called the remainder and we can
  10817. 9:19:59calculate the remainder using the
  10818. 9:20:01remainder operator which looks like this
  10819. 9:20:05so to check if a number is divisible by
  10820. 9:20:07three we can check if the remainder is
  10821. 9:20:11zero so here we're going to replace this
  10822. 9:20:15with I remainder 3 and we'll check if
  10823. 9:20:19the remainder is equal to to Z if we
  10824. 9:20:25save this will skip three and six
  10825. 9:20:29because they're both divisible by
  10826. 9:20:32three next let's use continue in a while
  10827. 9:20:36loop so at the end here we'll create
  10828. 9:20:39some new lines and let's say we want to
  10829. 9:20:42count from 1 to 10 using a while loop so
  10830. 9:20:46we'll create a loop variable let I equal
  10831. 9:20:511 and we'll create a while
  10832. 9:20:55loop I less than or equal to 10 curly
  10833. 9:21:01brackets and the increment step
  10834. 9:21:05i++ and inside the loop we just
  10835. 9:21:10console. log I and
  10836. 9:21:15save so this will count from 1 to 10
  10837. 9:21:18again and let's say we want to not count
  10838. 9:21:22numbers that are divisible by three so 3
  10839. 9:21:256 and 9 so at the top here we can check
  10840. 9:21:32if I remainder 3 is equal to zero and
  10841. 9:21:39curly brackets and we might just try to
  10842. 9:21:42do
  10843. 9:21:43continue however in a while loop
  10844. 9:21:46continue will actually skip the
  10845. 9:21:49increment step and this will cause an
  10846. 9:21:51infinite Loop Loop so when using
  10847. 9:21:54continue in a while loop we have to do
  10848. 9:21:57this increment step manually so before
  10849. 9:22:00continue we have to do I
  10850. 9:22:04++ to avoid an infinite Loop if we
  10851. 9:22:09save it will skip the numbers that are
  10852. 9:22:12divisible by three which is 3 6 and 9 so
  10853. 9:22:18remember in a while loop if we use
  10854. 9:22:20continue always do the increment step
  10855. 9:22:23manually in a for Loop however the
  10856. 9:22:26increment step is done automatically
  10857. 9:22:29after every
  10858. 9:22:31iteration all right the last detail
  10859. 9:22:33we're going to learn is how to use loops
  10860. 9:22:36with functions so let's say we have an
  10861. 9:22:39array of numbers and we want to create a
  10862. 9:22:42copy of this array where each number is
  10863. 9:22:45doubled so we already have the code for
  10864. 9:22:48this we can scroll up and the code for
  10865. 9:22:51this is here
  10866. 9:22:53so let's actually make a copy of this
  10867. 9:22:55code from before we'll
  10868. 9:22:58rightclick
  10869. 9:23:00copy and scroll down
  10870. 9:23:03here and at the
  10871. 9:23:06bottom right click and
  10872. 9:23:10paste at the top let's create an array
  10873. 9:23:14called
  10874. 9:23:15nums so const nums equals array and one
  10875. 9:23:231 and
  10876. 9:23:25three if we
  10877. 9:23:27save this will create an array where
  10878. 9:23:30each number is
  10879. 9:23:32doubled now what if we wanted to use
  10880. 9:23:35this loop on a different array other
  10881. 9:23:38than one one and three we can do that
  10882. 9:23:41using a function so let's create a
  10883. 9:23:44function around this Loop we'll create
  10884. 9:23:49function let's name it dou full
  10885. 9:23:54array brackets and curly bracket we'll
  10886. 9:23:58remove the closed curly bracket and at
  10887. 9:24:01the bottom We'll add it back and we'll
  10888. 9:24:05select these
  10889. 9:24:06lines and press
  10890. 9:24:08tab so now instead of doing this loop on
  10891. 9:24:12the same array every time we can turn
  10892. 9:24:15this into a parameter so at the top here
  10893. 9:24:20we'll create a parameter called
  10894. 9:24:23nums and we're going to delete this
  10895. 9:24:26line and we're going to Loop over this
  10896. 9:24:29parameter instead so at the bottom we
  10897. 9:24:34can call our function using double array
  10898. 9:24:39and
  10899. 9:24:40brackets and between the brackets we're
  10900. 9:24:42going to save an array into nums so
  10901. 9:24:47square bracket 1 1 and 3 if we
  10902. 9:24:53save this will do the same thing as
  10903. 9:24:56before but now because it's in a
  10904. 9:24:59function we can use this loop with any
  10905. 9:25:02array not just one one and three so at
  10906. 9:25:06the end we can do double array
  10907. 9:25:11brackets and an array 2 2 five this time
  10908. 9:25:18and
  10909. 9:25:19save and it will create an array where
  10910. 9:25:22these numbers are doubled so using a
  10911. 9:25:25function we can reuse this Loop for any
  10912. 9:25:29value another feature of using a
  10913. 9:25:32function with a loop is that we can
  10914. 9:25:34break out of this Loop using a return
  10915. 9:25:38instead so first let's actually use a
  10916. 9:25:41return in this function instead of
  10917. 9:25:44console.log we're going to
  10918. 9:25:47return nums doubled
  10919. 9:25:52and here we're going to console.log the
  10920. 9:25:56result
  10921. 9:26:01console.log and
  10922. 9:26:08console.log and
  10923. 9:26:11save so this does the same thing as
  10924. 9:26:14before except we're returning the result
  10925. 9:26:17and console. logging outside of the
  10926. 9:26:20function now let's say that if we see
  10927. 9:26:22the number zero we want to stop this
  10928. 9:26:25Loop early to do that we can click here
  10929. 9:26:29and check if num is equal to
  10930. 9:26:35zero and if it is we can break out of
  10931. 9:26:39this Loop now when we're inside a
  10932. 9:26:42function we can also use a return
  10933. 9:26:45statement to break out of this Loop so
  10934. 9:26:48we can also do return nums
  10935. 9:26:53doubled so a return statement will end
  10936. 9:26:56this function immediately so this is
  10937. 9:26:59another way of ending the
  10938. 9:27:01loop so down here if we add the number
  10939. 9:27:06zero and then five and
  10940. 9:27:10save you'll notice that the loop stops
  10941. 9:27:12when we see the number zero so return is
  10942. 9:27:16another way of breaking out of a loop
  10943. 9:27:19when it's inside a function and that's
  10944. 9:27:22the end of this lesson in this lesson we
  10945. 9:27:25learned about arrays which represent a
  10946. 9:27:28list of values we learned about loops
  10947. 9:27:31which let us run some code over and over
  10948. 9:27:34just like a loop we learned a Technique
  10949. 9:27:37we can use with loops called the
  10950. 9:27:39accumulator pattern and we used it to
  10951. 9:27:41create a to-do list project we learned
  10952. 9:27:44that arrays are references and the
  10953. 9:27:47destructuring shortcut and we learned
  10954. 9:27:49more features of Loops like break
  10955. 9:27:53continue and using Loops inside a
  10956. 9:27:56function here are some exercises to help
  10957. 9:27:59you practice arrays and loops
  10958. 9:28:29in this lesson we're going to learn some
  10959. 9:28:31Advanced features of functions and we're
  10960. 9:28:34going to use them to improve our
  10961. 9:28:36previous
  10962. 9:28:37projects first let's create a new file
  10963. 9:28:40for this lesson we'll go to our code
  10964. 9:28:43editor and click here to show our files
  10965. 9:28:47and click here to create a new file and
  10966. 9:28:51we'll name this
  10967. 9:28:5312- Advanced D
  10968. 9:28:58functions.
  10969. 9:28:59HTML and press
  10970. 9:29:02enter now let's copy 11 arrays and Loops
  10971. 9:29:06into this file so we'll click here
  10972. 9:29:09control a or command a and then
  10973. 9:29:14rightclick
  10974. 9:29:16copy and in
  10975. 9:29:18here right click and paste
  10976. 9:29:24and we can close this for now and let's
  10977. 9:29:27prepare this file for this lesson so
  10978. 9:29:30we'll scroll
  10979. 9:29:31up and change the title to
  10980. 9:29:36Advanced
  10981. 9:29:38functions and let's remove all the
  10982. 9:29:41JavaScript code from
  10983. 9:29:45before and now let's save and open this
  10984. 9:29:49file in the browser by right click
  10985. 9:29:52clicking open with live
  10986. 9:29:55server and let's also open the console
  10987. 9:29:58on this page so we'll
  10988. 9:30:01rightclick
  10989. 9:30:03inspect and open the
  10990. 9:30:06console and now we can close the
  10991. 9:30:08previous
  10992. 9:30:10tabs as well as the previous
  10993. 9:30:16files and now we're ready to begin so
  10994. 9:30:19first we're going to review how to use
  10995. 9:30:21fun
  10996. 9:30:22functions inside the script element
  10997. 9:30:25let's create a function using the word
  10998. 9:30:29function will give it a name
  10999. 9:30:32greeting brackets and curly brackets and
  11000. 9:30:36inside the function we'll type the code
  11001. 9:30:42console.log the string
  11002. 9:30:45hello so this is a function and it lets
  11003. 9:30:49us reuse code to run the code inside
  11004. 9:30:52this function we can type the function
  11005. 9:30:56name which is greeing and then
  11006. 9:31:00brackets if we
  11007. 9:31:03save this line of code we'll run the
  11008. 9:31:06code inside the function and display
  11009. 9:31:09hello this is known as calling the
  11010. 9:31:12function or running the
  11011. 9:31:15function so now that we understand
  11012. 9:31:17functions we're going to learn an
  11013. 9:31:19advanced feature of functions which is
  11014. 9:31:22functions are values so we learned many
  11015. 9:31:26types of values in this course like
  11016. 9:31:29numbers strings and booleans and we can
  11017. 9:31:32save a value in a variable for example
  11018. 9:31:36let's create some new lines and we'll
  11019. 9:31:39create a variable const num equal
  11020. 9:31:452 so this will save the value two inside
  11021. 9:31:49the variable num
  11022. 9:31:52now functions are also values and this
  11023. 9:31:55means just like a number we can save a
  11024. 9:31:58function inside a variable so let's give
  11025. 9:32:01it a try we'll create a new line and
  11026. 9:32:05type
  11027. 9:32:06const function one and we'll make this
  11028. 9:32:11equal to a function this time so we'll
  11029. 9:32:14just type
  11030. 9:32:16function and a name
  11031. 9:32:19greeting brackets and curly
  11032. 9:32:22brackets and inside the function we'll
  11033. 9:32:25type
  11034. 9:32:27console. log the string hello
  11035. 9:32:312 and we'll put a semicolon at the end
  11036. 9:32:35since we're saving in a
  11037. 9:32:37variable so this variable now contains a
  11038. 9:32:40function we're using the exact same
  11039. 9:32:43syntax as before except we're just
  11040. 9:32:46taking this function and saving it in a
  11041. 9:32:48variable because functions are values
  11042. 9:32:52so now that this function is inside this
  11043. 9:32:55variable let's actually console.log it
  11044. 9:32:59so we
  11045. 9:33:02console.log function one and
  11046. 9:33:07save and this will show us that the
  11047. 9:33:10variable function one actually contains
  11048. 9:33:13this
  11049. 9:33:14function now on a new line we can also
  11050. 9:33:17do console.log
  11051. 9:33:21and check the type of function one and
  11052. 9:33:28save and it will tell us that the type
  11053. 9:33:31of this variable is a function so as you
  11054. 9:33:35can see a function is just another type
  11055. 9:33:38of value and we can save it in a
  11056. 9:33:42variable we can also run the function
  11057. 9:33:44inside this
  11058. 9:33:46variable by typing the variable name
  11059. 9:33:49function 1 and because this contains a
  11060. 9:33:53function we can actually just call it
  11061. 9:33:56using
  11062. 9:33:58brackets so this will run the code
  11063. 9:34:01inside this function if we save it
  11064. 9:34:05displays hello
  11065. 9:34:07to now let's do some Cleanup in this
  11066. 9:34:09code in JavaScript as long as there's a
  11067. 9:34:13way to access a function this function
  11068. 9:34:16name is not needed so here we can access
  11069. 9:34:20this function using this variable so
  11070. 9:34:23this function name is not needed and we
  11071. 9:34:25can actually just delete it like this
  11072. 9:34:29and now this is called an anonymous
  11073. 9:34:32function which means a function without
  11074. 9:34:34a
  11075. 9:34:35name now an interesting thing to know is
  11076. 9:34:38that the function syntax that we've been
  11077. 9:34:40using this entire course up here is
  11078. 9:34:43actually a shortcut for this code now
  11079. 9:34:47even though this syntax uses VAR and we
  11080. 9:34:50usually don't recommend VAR this syntax
  11081. 9:34:53actually has a couple advantages number
  11082. 9:34:55one it's easier to read and number two
  11083. 9:34:59it gives us a feature called hoisting
  11084. 9:35:02which means we can call this function
  11085. 9:35:05greeting brackets before we create it in
  11086. 9:35:08the code so using hoisting we don't have
  11087. 9:35:11to worry about what order we write this
  11088. 9:35:14code hoisting doesn't work when we
  11089. 9:35:17create our function and save it in a
  11090. 9:35:19variable like this so in JavaScript
  11091. 9:35:23functions are values and just like any
  11092. 9:35:26other value we can save it in a
  11093. 9:35:29variable now let's think what else can
  11094. 9:35:32we do with a value well we can save a
  11095. 9:35:35value in an object for example let's
  11096. 9:35:39scroll
  11097. 9:35:40down and create some new lines and we'll
  11098. 9:35:44create an object
  11099. 9:35:47const object 1 equals curly
  11100. 9:35:52brackets and inside this object we'll
  11101. 9:35:55have a property num with the value
  11102. 9:36:00two so this saves the number two inside
  11103. 9:36:04this object now because functions are
  11104. 9:36:07values we can also save a function
  11105. 9:36:10inside this object so we'll type comma
  11106. 9:36:14and then a property fun colon and we'll
  11107. 9:36:18create a function this time function
  11108. 9:36:21[Music]
  11109. 9:36:23greeting brackets and curly brackets and
  11110. 9:36:27inside we type
  11111. 9:36:30console. log the string hello
  11112. 9:36:363 so this saves a function inside this
  11113. 9:36:40object under the property fun now to
  11114. 9:36:44access this function we just use the dot
  11115. 9:36:46notation as normal so object one dot
  11116. 9:36:53fun and because this value is a function
  11117. 9:36:57we can call it using
  11118. 9:36:59brackets if we
  11119. 9:37:02save this will run the code inside the
  11120. 9:37:05function and display hello 3 so remember
  11121. 9:37:10this is called a method when we have a
  11122. 9:37:13function saved inside an
  11123. 9:37:16object again as long as there's a way to
  11124. 9:37:19access this function for example using
  11125. 9:37:22object one. fun then we don't need this
  11126. 9:37:25function name so we'll delete it and use
  11127. 9:37:29an anonymous
  11128. 9:37:31function now let's think what else can
  11129. 9:37:34we do with a value we can pass a value
  11130. 9:37:38into a function for example down here
  11131. 9:37:43let's create a function
  11132. 9:37:46function
  11133. 9:37:47display brackets and curly brackets and
  11134. 9:37:51we'll give this function a parameter so
  11135. 9:37:54in here we'll create a parameter and
  11136. 9:37:57name it
  11137. 9:37:59param and inside the function we can use
  11138. 9:38:02a parameter just like a variable so we
  11139. 9:38:08console.log the
  11140. 9:38:11param and then down here let's call this
  11141. 9:38:14function using
  11142. 9:38:17display brackets and two
  11143. 9:38:21this will take the value two save it
  11144. 9:38:24inside Pam and then we can use it just
  11145. 9:38:27like a variable if we
  11146. 9:38:30save this will display two in the
  11147. 9:38:34console so this is known as passing a
  11148. 9:38:38value into a function now because
  11149. 9:38:42functions are values we can also pass a
  11150. 9:38:45function into a function so let's give
  11151. 9:38:49it a try down here
  11152. 9:38:52we'll create a new function
  11153. 9:38:55function run brackets and curly
  11154. 9:38:59brackets and this function will also
  11155. 9:39:02have a parameter let's call it
  11156. 9:39:06param and down here we'll call this
  11157. 9:39:10function run
  11158. 9:39:13brackets and between the brackets we're
  11159. 9:39:16going to give it a function this time so
  11160. 9:39:18we'll type function
  11161. 9:39:21brackets and curly brackets and inside
  11162. 9:39:25the function we'll type
  11163. 9:39:29console.log the string hello
  11164. 9:39:344 so this will take this function and
  11165. 9:39:37save it inside Pam and now up here we
  11166. 9:39:42can use Pam just like a variable so we
  11167. 9:39:46can type
  11168. 9:39:47Pam and because Pam contains a function
  11169. 9:39:52we can actually just call it using
  11170. 9:39:55brackets if we
  11171. 9:39:57save this code will run the function
  11172. 9:40:01that we created down here and display
  11173. 9:40:04hello 4 so this is called passing a
  11174. 9:40:08function into another
  11175. 9:40:11function the function that we're passing
  11176. 9:40:13in is called a callback function or a
  11177. 9:40:17callback so to summarize functions are
  11178. 9:40:21values anything we can do with a value
  11179. 9:40:24we can also do with a
  11180. 9:40:26function now let's do some practical
  11181. 9:40:29examples using functions as values we're
  11182. 9:40:33going to learn a feature of JavaScript
  11183. 9:40:35called set timeout set timeout allows us
  11184. 9:40:39to run a function in the
  11185. 9:40:41future first we'll comment out the
  11186. 9:40:44previous code using
  11187. 9:40:48slashstar and at the bottom
  11188. 9:40:51star slash and let's create some new
  11189. 9:40:55lines and we're going to type the code
  11190. 9:40:58set capital T time
  11191. 9:41:02out so set timeout is a built-in
  11192. 9:41:05function so we can type brackets to call
  11193. 9:41:10this function Now set timeout takes two
  11194. 9:41:14parameters inside these brackets the
  11195. 9:41:17first parameter is a function that we
  11196. 9:41:19want to run in the future
  11197. 9:41:22let's create a
  11198. 9:41:24function brackets and curly brackets and
  11199. 9:41:29inside this function let's type
  11200. 9:41:33console.log the string time
  11201. 9:41:38out so here we're using a function as a
  11202. 9:41:42value and we're passing it into another
  11203. 9:41:45function at the end we'll type a comma
  11204. 9:41:49and we'll give set timeout a second
  11205. 9:41:52parameter the second parameter is how
  11206. 9:41:54long to wait before running this
  11207. 9:41:57function so this is a number in
  11208. 9:42:00milliseconds 1,000 milliseconds is equal
  11209. 9:42:03to 1 second so if we type 3,000
  11210. 9:42:08milliseconds this is equal to 3 seconds
  11211. 9:42:12so set timeout will wait for 3 seconds
  11212. 9:42:15and then call this function if we
  11213. 9:42:19save after 3 seconds
  11214. 9:42:21it's going to display timeout in the
  11215. 9:42:24console so that's set timeout we give it
  11216. 9:42:28a function and it will call this
  11217. 9:42:30function after a certain amount of
  11218. 9:42:32time so now we're going to learn an
  11219. 9:42:35important concept called a synchronous
  11220. 9:42:38code so after set timeout let's type
  11221. 9:42:45console.log the string next
  11222. 9:42:49line and Save
  11223. 9:42:52so notice that it displays next line
  11224. 9:42:54first and then after 3 seconds it
  11225. 9:42:57displays timeout so what's happening
  11226. 9:43:00here why are they reversed set timeout
  11227. 9:43:04sets up a timer and after 3 seconds it
  11228. 9:43:07will call this function however it
  11229. 9:43:10doesn't actually wait for the 3 seconds
  11230. 9:43:12to finish it just sets up the timer and
  11231. 9:43:15immediately goes to the next line so
  11232. 9:43:18that's why this line is displayed first
  11233. 9:43:22so this is called asynchronous code
  11234. 9:43:25asynchronous code means the computer
  11235. 9:43:27won't wait for this line to finish
  11236. 9:43:30before going on to the next line so for
  11237. 9:43:33set timeout it'll just set up the timer
  11238. 9:43:36and then immediately go to the next line
  11239. 9:43:39synchronous code on the other hand means
  11240. 9:43:42the computer will wait for one line to
  11241. 9:43:43finish before going to the next line so
  11242. 9:43:47all the code we've written in this
  11243. 9:43:49course so far has been synchronous code
  11244. 9:43:53in addition all the code inside this
  11245. 9:43:56function is also synchronous code so if
  11246. 9:43:59we added
  11247. 9:44:02console.log the string time out
  11248. 9:44:07to and
  11249. 9:44:09save the code inside the function will
  11250. 9:44:12still run line by line it will wait for
  11251. 9:44:16each line to finish before going to the
  11252. 9:44:18next line and after 3 seconds it will
  11253. 9:44:21display timeout followed by timeout
  11254. 9:44:242 so don't worry all the code that we
  11255. 9:44:28normally write will be synchronous code
  11256. 9:44:30it will run line by line as usual the
  11257. 9:44:34only time that code becomes asynchronous
  11258. 9:44:37is when we use certain features of
  11259. 9:44:39JavaScript like set timeout so set
  11260. 9:44:42timeout is the only part that is
  11261. 9:44:45asynchronous now the advantage of set
  11262. 9:44:48timeout being asynchronous is that it
  11263. 9:44:50does doesn't block our code for 3
  11264. 9:44:52seconds waiting for this timer to finish
  11265. 9:44:55it just sets up the timer in the
  11266. 9:44:57background and then goes to the next
  11267. 9:44:59line of code so this allows our code to
  11268. 9:45:02do other things while this timer is
  11269. 9:45:04running in the background and this is
  11270. 9:45:07similar to real life if you set up a
  11271. 9:45:09timer on your alarm clock you're not
  11272. 9:45:12going to stop and wait for the timer to
  11273. 9:45:14finish you're going to go and do
  11274. 9:45:16something else asynchronous code is the
  11275. 9:45:19exact same concept
  11276. 9:45:22next we're going to learn a similar
  11277. 9:45:24function to set timeout called set
  11278. 9:45:28interval so at the bottom let's type the
  11279. 9:45:31code set capital I
  11280. 9:45:36interval and set interval is another
  11281. 9:45:39built-in function so we can call it
  11282. 9:45:42using
  11283. 9:45:43brackets and set interval takes these
  11284. 9:45:46same two parameters as set timeout the
  11285. 9:45:50first parameter is a function that we
  11286. 9:45:52want to run in the future so let's type
  11287. 9:45:56function brackets and curly brackets and
  11288. 9:46:00inside we'll type
  11289. 9:46:03console. log the string
  11290. 9:46:09interval now let's add a comma and the
  11291. 9:46:12second parameter is also a number in
  11292. 9:46:15milliseconds so we'll type 3,000
  11293. 9:46:18milliseconds or 3 seconds so set
  11294. 9:46:21interval will run this function 3
  11295. 9:46:24seconds in the future but this time it
  11296. 9:46:27will keep running the function every 3
  11297. 9:46:30seconds if we
  11298. 9:46:32save in the console every 3 seconds it
  11299. 9:46:36will run the function and display
  11300. 9:46:39interval so set interval is also
  11301. 9:46:42asynchronous below it if we type
  11302. 9:46:48console.log the string next
  11303. 9:46:51line
  11304. 9:46:53two and
  11305. 9:46:55save it will set up the interval and
  11306. 9:46:58then immediately go to the next line and
  11307. 9:47:01display next line two so that's set
  11308. 9:47:04interval we give it a function and it
  11309. 9:47:07will keep running this function after a
  11310. 9:47:10certain period of time so now let's do a
  11311. 9:47:13project using what we learned so far
  11312. 9:47:16let's go to our browser and we'll create
  11313. 9:47:19a new tab at the top
  11314. 9:47:21top and here let's type
  11315. 9:47:26superimple
  11316. 9:47:28dodev slash projects slash Advanced Das
  11317. 9:47:36functions and press
  11318. 9:47:38enter so this is basically the rock
  11319. 9:47:41paper scissors game that we created
  11320. 9:47:43earlier except this time it has an extra
  11321. 9:47:47button called autop playay if we click
  11322. 9:47:50this Buton
  11323. 9:47:51button every second the computer will
  11324. 9:47:54play by
  11325. 9:47:55itself so this feature will help us
  11326. 9:47:58practice using set
  11327. 9:47:59interval so first let's make a copy of
  11328. 9:48:02the project files we'll go into our code
  11329. 9:48:06editor and click this icon to show our
  11330. 9:48:09files and let's make a copy of 10 rock
  11331. 9:48:13paper scissors for this lesson so we'll
  11332. 9:48:17rightclick copy
  11333. 9:48:20and down here rightclick and
  11334. 9:48:25paste and I'll stop this for now so here
  11335. 9:48:29we're going to change the name by right
  11336. 9:48:32clicking
  11337. 9:48:34rename we'll remove copy at the end and
  11338. 9:48:38at the beginning we'll change it to 12
  11339. 9:48:42and press
  11340. 9:48:43enter and let's also make a copy of the
  11341. 9:48:47JavaScript and CSS files so we'll click
  11342. 9:48:50this file and right click
  11343. 9:48:54copy and then click the folder right
  11344. 9:48:58click
  11345. 9:48:59paste and we'll rename this file
  11346. 9:49:03rightclick
  11347. 9:49:05rename remove copy at the end and at the
  11348. 9:49:09beginning change it to 12 and press
  11349. 9:49:12enter and let's also make a copy of the
  11350. 9:49:15CSS file select it rightclick copy
  11351. 9:49:21click the folder right click and
  11352. 9:49:26paste and right click
  11353. 9:49:30rename remove copy and at the front
  11354. 9:49:34change it to 12 and press
  11355. 9:49:38enter now let's scroll down to 12 rock
  11356. 9:49:42paper
  11357. 9:49:44scissors and I'll close this for
  11358. 9:49:47now and inside the head let's change
  11359. 9:49:50this to load 12 rock paper scissors and
  11360. 9:49:54we'll scroll to the
  11361. 9:49:56bottom and here instead of the lesson 10
  11362. 9:49:59file we'll load The Lesson 12 Javascript
  11363. 9:50:03file let's save and we'll open this in
  11364. 9:50:07the browser by
  11365. 9:50:09right-clicking open with live
  11366. 9:50:12server so now we're ready to begin let's
  11367. 9:50:16go back to superimple
  11368. 9:50:18dodev and the first thing thing we're
  11369. 9:50:20going to create is this autoplay button
  11370. 9:50:24so beside the reset score button which
  11371. 9:50:26is here we're going to create a new
  11372. 9:50:30button less than button greater than and
  11373. 9:50:34inside we'll have the text auto
  11374. 9:50:38play let's
  11375. 9:50:40save and go back to our
  11376. 9:50:43Tab and now we have this button so next
  11377. 9:50:47let's style this button with CSS so
  11378. 9:50:50we'll give it a
  11379. 9:50:52class class equals
  11380. 9:50:56auto-play Das
  11381. 9:50:59button and
  11382. 9:51:01save and now let's open the CSS
  11383. 9:51:06file I'm going to take this file and
  11384. 9:51:08then drag it to the bottom so I can see
  11385. 9:51:11it side by side so I'll close this for
  11386. 9:51:14now and rearrange this a little bit and
  11387. 9:51:18then down here and we're going to style
  11388. 9:51:21the autop playay button so let's just
  11389. 9:51:23make the style the same as the reset
  11390. 9:51:25score button so in the CSS we'll scroll
  11391. 9:51:29down to the reset score button Styles so
  11392. 9:51:33we'll add a comma at the end and type
  11393. 9:51:37dot
  11394. 9:51:40auto-play dasb button so now these
  11395. 9:51:44Styles will apply to the class reset
  11396. 9:51:46score button and the auto playay button
  11397. 9:51:50if if we
  11398. 9:51:51save now we styled our project so next
  11399. 9:51:55let's make this button interactive so at
  11400. 9:51:58the top here we'll add an onclick
  11401. 9:52:03attribute and then in here we're going
  11402. 9:52:06to run some JavaScript code let's
  11403. 9:52:09actually create a function so we can
  11404. 9:52:11separate the JavaScript into its own
  11405. 9:52:13file so we'll run a function called auto
  11406. 9:52:18play and
  11407. 9:52:21brackets so now let's
  11408. 9:52:23save and right now this function doesn't
  11409. 9:52:26exist so we have to create this function
  11410. 9:52:29in the Javascript file so for me the
  11411. 9:52:32file is right here I'll select it and
  11412. 9:52:35then drag it down here so I can see it
  11413. 9:52:37side by side so let's scroll down and
  11414. 9:52:41I'm going to create it just above the
  11415. 9:52:43play game function so here we'll create
  11416. 9:52:47some new lines and type function
  11417. 9:52:51auto play brackets and curly
  11418. 9:52:55brackets and now inside this function we
  11419. 9:52:59want to play the game every second so we
  11420. 9:53:02can use set interval to run some code
  11421. 9:53:05every second so let's type set
  11422. 9:53:10interval
  11423. 9:53:12brackets and inside the brackets will
  11424. 9:53:15give it a function that we want to run
  11425. 9:53:18so brackets and curly
  11426. 9:53:21brackets and then at the end let's type
  11427. 9:53:24a comma and we'll give it
  11428. 9:53:271,000 so this means it will run this
  11429. 9:53:30function every 1,000 milliseconds or 1
  11430. 9:53:34second and now inside this function
  11431. 9:53:37we're going to automatically play the
  11432. 9:53:39game and to play the game we can use
  11433. 9:53:42this function right here play game so
  11434. 9:53:46here we'll type play game
  11435. 9:53:51brackets now we need to provide this
  11436. 9:53:53function with a Player move and remember
  11437. 9:53:57we have another function pick computer
  11438. 9:53:59move which will give us a random move so
  11439. 9:54:03above this line let's create a variable
  11440. 9:54:08const
  11441. 9:54:09Player move and we'll make it equal and
  11442. 9:54:14we'll pick a random move using pick
  11443. 9:54:18computer move
  11444. 9:54:21and
  11445. 9:54:22brackets and finally we'll take this
  11446. 9:54:25move that we picked and pass it into
  11447. 9:54:28this function so Player
  11448. 9:54:32move so now when we click this button it
  11449. 9:54:35should play the game every second let's
  11450. 9:54:39save and click
  11451. 9:54:43autoplay and it works the computer is
  11452. 9:54:46playing against itself every second so
  11453. 9:54:49now let's make this button button stop
  11454. 9:54:51playing when we click it again so at the
  11455. 9:54:54top let's create a variable to keep
  11456. 9:54:57track of whether or not we're playing so
  11457. 9:55:01we use let is auto playing and at the
  11458. 9:55:07start we'll make it equal to
  11459. 9:55:11false and
  11460. 9:55:13save and now inside this function we'll
  11461. 9:55:17check using an if statement brackets and
  11462. 9:55:20curly
  11463. 9:55:21brackets and inside this condition let's
  11464. 9:55:24check if we're not playing so not is
  11465. 9:55:30auto
  11466. 9:55:33playing so if we're not playing then we
  11467. 9:55:36want to start playing the game which is
  11468. 9:55:38this code down here so let's select this
  11469. 9:55:42code and then right click
  11470. 9:55:45cut and in here right click and
  11471. 9:55:51paste and also after we start playing
  11472. 9:55:55the game we're going to change this
  11473. 9:55:57variable to true so is auto playing and
  11474. 9:56:03we'll make it equal to true because now
  11475. 9:56:07we are autop playing so now let's add
  11476. 9:56:10the else Branch to this
  11477. 9:56:12statement else curly
  11478. 9:56:16brackets so now we need to stop this
  11479. 9:56:19interval so how do we do that the answer
  11480. 9:56:23is that set interval actually returns a
  11481. 9:56:26number and this number is like an ID we
  11482. 9:56:29can use this ID to stop the interval so
  11483. 9:56:33at the front let's create a variable to
  11484. 9:56:37save this ID
  11485. 9:56:39const
  11486. 9:56:41interval ID and
  11487. 9:56:45equals however there's another problem
  11488. 9:56:48here every time we run the function
  11489. 9:56:51we're going to get a different ID so in
  11490. 9:56:54order to save the ID from last time we
  11491. 9:56:57need to put this variable outside the
  11492. 9:57:00function so let's select this and remove
  11493. 9:57:03it for now and we'll scroll up and here
  11494. 9:57:08we'll create the variable let
  11495. 9:57:12interval ID and we'll leave it undefined
  11496. 9:57:16to start and now down here we're going
  11497. 9:57:19to save the return value into this
  11498. 9:57:22variable by typing
  11499. 9:57:25interval ID equals set
  11500. 9:57:30interval and now at the bottom we're
  11501. 9:57:33going to use the interval ID to stop the
  11502. 9:57:36interval let's scroll down and to stop
  11503. 9:57:40an interval we can use a function called
  11504. 9:57:42clear interval so we'll type clear
  11505. 9:57:47capital I interval
  11506. 9:57:50and
  11507. 9:57:52brackets and between the brackets we're
  11508. 9:57:54going to give it an ID that we want to
  11509. 9:57:57stop so we'll give it this interval
  11510. 9:58:00ID
  11511. 9:58:02interval
  11512. 9:58:04ID and then at the bottom let's also
  11513. 9:58:07change is autop playing to false because
  11514. 9:58:11we just stopped it so is auto playing
  11515. 9:58:17equals false
  11516. 9:58:20and
  11517. 9:58:21save so now if we try out this
  11518. 9:58:25button it will start playing the game by
  11519. 9:58:27itself every second and if we click the
  11520. 9:58:30button again it will stop playing the
  11521. 9:58:34game so that's how we stop an interval
  11522. 9:58:37we need to save the ID and then call
  11523. 9:58:40clear interval with this ID so now I'll
  11524. 9:58:44clean up the code a little by removing
  11525. 9:58:47this extra line and Save
  11526. 9:58:50and now we're finished with this
  11527. 9:58:53feature so next let's go back to our
  11528. 9:58:57Advanced functions file so I'll click up
  11529. 9:59:00here and then at the bottom I'll open 12
  11530. 9:59:04Advanced
  11531. 9:59:06functions and hide the files for now and
  11532. 9:59:10we're going to learn some more features
  11533. 9:59:12of functions as values so let's actually
  11534. 9:59:16close the other files that we opened
  11535. 9:59:20and first let's comment out the previous
  11536. 9:59:23code with
  11537. 9:59:25slashstar and at the bottom star
  11538. 9:59:29slash let's scroll down and
  11539. 9:59:33save and let's open the tab for this
  11540. 9:59:37file so now we're going to learn another
  11541. 9:59:40way to Loop through an array first let's
  11542. 9:59:44create an array we'll type square
  11543. 9:59:47brackets and inside the array we'll have
  11544. 9:59:50the string make
  11545. 9:59:54dinner and another string wash
  11546. 10:00:00dishes and another
  11547. 10:00:02string watch
  11548. 10:00:06YouTube so in the previous lesson we
  11549. 10:00:09learned how to Loop through an array
  11550. 10:00:11using a for Loop or a while loop another
  11551. 10:00:15way to Loop through an array is using a
  11552. 10:00:18method of the array called four each so
  11553. 10:00:22at the bottom let's type dot for capital
  11554. 10:00:27E each and
  11555. 10:00:31brackets so this is the four each method
  11556. 10:00:34and it allows us to Loop through this
  11557. 10:00:37array inside these brackets we're going
  11558. 10:00:40to give for each a function so we'll
  11559. 10:00:43type
  11560. 10:00:45function brackets and curly brackets so
  11561. 10:00:50again we're just passing a function into
  11562. 10:00:53another
  11563. 10:00:54function however this function will be a
  11564. 10:00:57little different we're going to give it
  11565. 10:00:59a parameter so up here let's create a
  11566. 10:01:03parameter called
  11567. 10:01:06value so what four each does is that it
  11568. 10:01:09Loops through the array and for each of
  11569. 10:01:12these values it will save the value in
  11570. 10:01:15the parameter and run the function so
  11571. 10:01:19let's
  11572. 10:01:22console.log
  11573. 10:01:24value and
  11574. 10:01:27save so this will Loop through the array
  11575. 10:01:30and display each value in the console so
  11576. 10:01:34let's go through this step bystep so
  11577. 10:01:37it'll take the first value save it in
  11578. 10:01:40this parameter and run the function then
  11579. 10:01:43it will take the second value save it in
  11580. 10:01:45the parameter and run the function then
  11581. 10:01:49it will take the Third value save it in
  11582. 10:01:51the parameter and run the
  11583. 10:01:54function so the for each method is
  11584. 10:01:57actually the preferred way to Loop
  11585. 10:01:59through an array because this is easier
  11586. 10:02:02to read than a for Loop like this and
  11587. 10:02:05for each also has another feature we can
  11588. 10:02:09give this function a second parameter so
  11589. 10:02:12we can type comma and the second
  11590. 10:02:15parameter is the
  11591. 10:02:17index or the position of the value in
  11592. 10:02:20the array so for the first value index
  11593. 10:02:23will be zero for the second value index
  11594. 10:02:26will be one third value index will be
  11595. 10:02:29two and so on so above this line let's
  11596. 10:02:35also
  11597. 10:02:38console.log the
  11598. 10:02:40index and
  11599. 10:02:42save and this will also display the
  11600. 10:02:45index of each
  11601. 10:02:47value all right now let's do a practical
  11602. 10:02:50example using the for each method we're
  11603. 10:02:54going to go back to the to-do list
  11604. 10:02:56project that we created in the previous
  11605. 10:02:58lesson so we'll click up here to show
  11606. 10:03:01our
  11607. 10:03:02files and let's scroll up and let's open
  11608. 10:03:0611 too
  11609. 10:03:09list.js so in this code we used a for
  11610. 10:03:13Loop to display the to-do list on the
  11611. 10:03:15page we're going to switch this to using
  11612. 10:03:18a for each method instead said so first
  11613. 10:03:21let's actually make a copy of the files
  11614. 10:03:24for this project we'll click up
  11615. 10:03:27here and let's click 11 to-do list and
  11616. 10:03:32rightclick
  11617. 10:03:34copy and let's click the script
  11618. 10:03:37folder rightclick and
  11619. 10:03:41paste and now we'll rename this by
  11620. 10:03:45right-clicking
  11621. 10:03:47rename remove copy from the end and
  11622. 10:03:51change the beginning to 12 and press
  11623. 10:03:56enter let's also make a copy of the CSS
  11624. 10:03:59and HTML file for this project so we'll
  11625. 10:04:02click the CSS file right click
  11626. 10:04:07copy click the Styles folder right click
  11627. 10:04:12and
  11628. 10:04:13paste rightclick
  11629. 10:04:17rename remove copy
  11630. 10:04:20and at the front change it to 12 and
  11631. 10:04:23press
  11632. 10:04:25enter and finally we'll scroll down
  11633. 10:04:28click 11 Todo list.
  11634. 10:04:30HTML
  11635. 10:04:32rightclick
  11636. 10:04:34copy and then down here right click and
  11637. 10:04:39paste right click
  11638. 10:04:43rename remove copy at the end and at the
  11639. 10:04:46beginning change it to 12 and and press
  11640. 10:04:51enter now we can hide these
  11641. 10:04:54files and we can close the lesson 11
  11642. 10:04:57files and now here we're going to change
  11643. 10:05:00the CSS file that we load to 12 to-do
  11644. 10:05:04list and we'll scroll down and we'll
  11645. 10:05:07also change the Javascript file that we
  11646. 10:05:09load to 12 to-do list now let's
  11647. 10:05:14save and we'll open this in the browser
  11648. 10:05:17by right clicking
  11649. 10:05:20open with live
  11650. 10:05:22server okay now let's go back to the JS
  11651. 10:05:26file so I'll take this file and drag it
  11652. 10:05:29to the
  11653. 10:05:30bottom and I'll rearrange the windows
  11654. 10:05:33here we're going to scroll down and
  11655. 10:05:36we'll change this for Loop to use a for
  11656. 10:05:38each method so first let's type above
  11657. 10:05:42this for Loop and we'll type the array
  11658. 10:05:45that we want to Loop through which is
  11659. 10:05:48Todo list and we'll type Dot and use the
  11660. 10:05:53four each method and
  11661. 10:05:57brackets inside the brackets we'll give
  11662. 10:05:59it a function that we want to run for
  11663. 10:06:02each to do so we'll type
  11664. 10:06:05function brackets and curly
  11665. 10:06:09brackets and this function can have two
  11666. 10:06:12parameters the first parameter will
  11667. 10:06:14contain each value in the array so we'll
  11668. 10:06:18call it too
  11669. 10:06:19[Music]
  11670. 10:06:20object we'll type a comma and the second
  11671. 10:06:24parameter contains each index in the
  11672. 10:06:27array so we'll just call it
  11673. 10:06:30index and now inside this Loop we
  11674. 10:06:33basically want to do everything that we
  11675. 10:06:35did here so let's just make a copy of
  11676. 10:06:39this
  11677. 10:06:40code right click
  11678. 10:06:43copy and then up here inside for each
  11679. 10:06:48right click and and
  11680. 10:06:51paste so now if we scroll up we already
  11681. 10:06:54have the to-do object up here so we
  11682. 10:06:57don't need to get it from the array like
  11683. 10:07:00before so we can remove this line and
  11684. 10:07:04let's also remove these
  11685. 10:07:08comments the other thing we have to
  11686. 10:07:10change is that previously we were using
  11687. 10:07:12I for the index but now the index is
  11688. 10:07:16called index so we just change this to
  11689. 10:07:19to
  11690. 10:07:21index so that's how we use a for each
  11691. 10:07:25method instead of a for Loop if we
  11692. 10:07:28compare this code to the four Loop that
  11693. 10:07:30we had before the for each method is
  11694. 10:07:33easier to read so for the rest of this
  11695. 10:07:36course we're going to prefer to use four
  11696. 10:07:39each instead of a for Loop so we're
  11697. 10:07:42going to remove this for
  11698. 10:07:48Loop let's scroll back up and
  11699. 10:07:52save and the project still works so
  11700. 10:07:56that's a practical example of using the
  11701. 10:07:58for each method now let's go back and
  11702. 10:08:01learn some more details about for each
  11703. 10:08:05we'll close the Javascript file for now
  11704. 10:08:08and go back to Advanced functions. HTML
  11705. 10:08:12and open the tab for this
  11706. 10:08:15file so in the previous lesson we
  11707. 10:08:18learned a feature of Loops called
  11708. 10:08:21continue continue lets us skip to the
  11709. 10:08:23next iteration or the next run of the
  11710. 10:08:26loop for each methods don't have
  11711. 10:08:29continue but we can do the same thing
  11712. 10:08:31with a return statement so let's say in
  11713. 10:08:35this Loop we don't like washing dishes
  11714. 10:08:38if the value is wash dishes we're going
  11715. 10:08:40to skip it and not display it in the
  11716. 10:08:43console so above we'll create an if
  11717. 10:08:47statement
  11718. 10:08:50and then inside the condition we'll
  11719. 10:08:52check if value is equal to the string
  11720. 10:08:57wash
  11721. 10:08:59dishes and then in the curly brackets if
  11722. 10:09:02it is equal will return and
  11723. 10:09:06semicolon so this return statement will
  11724. 10:09:09end this function early and it will go
  11725. 10:09:12on to the next function so it basically
  11726. 10:09:15does the same thing as continue in a for
  11727. 10:09:17Loop if we
  11728. 10:09:20save you'll notice that it skipped wash
  11729. 10:09:23dishes in the previous lesson we also
  11730. 10:09:26learned a feature of Loops called break
  11731. 10:09:29break lets us exit a loop early
  11732. 10:09:32unfortunately there's not an easy way to
  11733. 10:09:35break in a 4 each Loop so if we need to
  11734. 10:09:38use break to exit this Loop early then
  11735. 10:09:42it's better to use a regular for
  11736. 10:09:46Loop let's take a break before
  11737. 10:09:48continuing with the rest of this lesson
  11738. 10:09:51here are some exercises that will help
  11739. 10:09:53you practice what we learned so far
  11740. 10:10:25so next we're going to move on and learn
  11741. 10:10:28a shorter way to write functions called
  11742. 10:10:31Arrow functions so let's go to the
  11743. 10:10:34bottom of this code we'll create some
  11744. 10:10:36new lines and let's create a variable
  11745. 10:10:41const and we'll name the variable Arrow
  11746. 10:10:46function and we'll make it equal to to
  11747. 10:10:50brackets equal greater than or an arrow
  11748. 10:10:55and then curly
  11749. 10:10:57brackets inside the curly brackets we'll
  11750. 10:10:59type some code
  11751. 10:11:03console.log brackets and the string
  11752. 10:11:07hello and we'll put a semicolon at the
  11753. 10:11:11end so this is an arrow function it
  11754. 10:11:14works the same way as a regular function
  11755. 10:11:18for example we can call this Arrow
  11756. 10:11:21function by typing the variable name
  11757. 10:11:25Arrow
  11758. 10:11:27function and
  11759. 10:11:29brackets if we
  11760. 10:11:31save this line of code will run the code
  11761. 10:11:35inside this Arrow function and display
  11762. 10:11:39hello so Arrow functions mostly work the
  11763. 10:11:42same way as regular functions so to
  11764. 10:11:45compare them let's actually type above
  11765. 10:11:48this code we'll create some new lines
  11766. 10:11:51and type
  11767. 10:11:53const
  11768. 10:11:54regular
  11769. 10:11:56function equals the word
  11770. 10:12:00function brackets and curly brackets
  11771. 10:12:07console.log string
  11772. 10:12:10hello so these two syntaxes do the same
  11773. 10:12:14thing the difference is that instead of
  11774. 10:12:17using the word function an arrow
  11775. 10:12:19function just uses an
  11776. 10:12:22arrow now parameters work the same way
  11777. 10:12:25in an arrow function we just type the
  11778. 10:12:27parameter between these round brackets
  11779. 10:12:30so here let's type
  11780. 10:12:33param and here as
  11781. 10:12:37well if we want multiple parameters it
  11782. 10:12:40also works the same way up here we can
  11783. 10:12:43type comma param 2 and down here comma
  11784. 10:12:49Pam 2 and finally return statements also
  11785. 10:12:54work the same way so in this function we
  11786. 10:12:58can return five and in the arrow
  11787. 10:13:02function we can also return
  11788. 10:13:06five so now let's learn some differences
  11789. 10:13:09between these two Arrow functions have a
  11790. 10:13:12few shortcuts that regular functions
  11791. 10:13:14don't have for example at the bottom
  11792. 10:13:19let's create an arrow function using
  11793. 10:13:22const and we'll name it one
  11794. 10:13:27perm and make it equal to an arrow
  11795. 10:13:30function brackets arrow and curly
  11796. 10:13:35brackets now in this Arrow function
  11797. 10:13:37we're going to give it one parameter
  11798. 10:13:41param and inside the function will
  11799. 10:13:46console.log param plus
  11800. 10:13:50one so when an arrow function only has
  11801. 10:13:53one parameter these round brackets are
  11802. 10:13:56optional so we can actually remove
  11803. 10:14:00them if we do one param brackets 2 if we
  11804. 10:14:08save it will display 2 + 1 which is
  11805. 10:14:133 let's learn another shortcut we'll
  11806. 10:14:16create some new lines and type const one
  11807. 10:14:22line and make it equal to an arrow
  11808. 10:14:25function brackets arrow and curly
  11809. 10:14:30brackets and inside will return 2 +
  11810. 10:14:373 so when an arrow function only has one
  11811. 10:14:41line like this we can actually put it on
  11812. 10:14:45the same line as the
  11813. 10:14:47arrow and now that everything's on the
  11814. 10:14:50same line the curly brackets are
  11815. 10:14:53actually optional so we can remove them
  11816. 10:14:56and we can also remove the return
  11817. 10:15:01statement so this is a oneline arrow
  11818. 10:15:04function if we just have an arrow it
  11819. 10:15:06will automatically return the result on
  11820. 10:15:09the right so at the bottom we can
  11821. 10:15:14console. log and call the one line
  11822. 10:15:19function with
  11823. 10:15:21brackets if we
  11824. 10:15:24save this function will return to plus
  11825. 10:15:27three and it will display five in the
  11826. 10:15:30console so that's Arrow functions it's a
  11827. 10:15:33shorter way of writing a regular
  11828. 10:15:35function and it has a few
  11829. 10:15:38shortcuts for now let's get some
  11830. 10:15:40practice using Arrow functions so let's
  11831. 10:15:44scroll up in this code to the for each
  11832. 10:15:47method so let's use an arrow function
  11833. 10:15:51here instead we just have to remove the
  11834. 10:15:54word
  11835. 10:15:55function and after the round brackets
  11836. 10:15:58add an arrow equals greater than if we
  11837. 10:16:04save the loop still works so when we're
  11838. 10:16:08passing a function into another function
  11839. 10:16:11like for each it's recommended to use
  11840. 10:16:14Arrow functions because it makes our
  11841. 10:16:15code easier to read so now let's use use
  11842. 10:16:19Arrow functions in some of our previous
  11843. 10:16:21projects we'll start with the rock paper
  11844. 10:16:24scissors game let's open our files by
  11845. 10:16:27clicking
  11846. 10:16:28here and we'll find the 12 rock paper
  11847. 10:16:32scissors. JS
  11848. 10:16:35file and then inside this file we're
  11849. 10:16:38going to scroll down to the autoplay
  11850. 10:16:41function so here we're passing a
  11851. 10:16:45function into another function so it's
  11852. 10:16:48recommended to use an arrow
  11853. 10:16:50function let's remove the word
  11854. 10:16:53function and add an arrow so now this
  11855. 10:16:57code is a little easier to read now what
  11856. 10:17:01about this function up here function
  11857. 10:17:04autoplay should we also switch this to
  11858. 10:17:07an arrow function so this situation is
  11859. 10:17:10more personal preference so above this
  11860. 10:17:13let's actually create an arrow function
  11861. 10:17:15version of this const
  11862. 10:17:19auto play equals brackets and arrow and
  11863. 10:17:26curly
  11864. 10:17:27brackets so even though we could create
  11865. 10:17:30an arrow function version of this I
  11866. 10:17:33actually prefer the regular function
  11867. 10:17:36syntax here for two reasons number one I
  11868. 10:17:39think this is easier to read than this
  11869. 10:17:43and number two this function syntax
  11870. 10:17:46enables hoisting which means we can call
  11871. 10:17:49this function before we create it and we
  11872. 10:17:52don't have to worry about which order we
  11873. 10:17:54write the code so for these reasons I
  11874. 10:17:57actually prefer this situation to use a
  11875. 10:18:00regular function syntax and not an arrow
  11876. 10:18:03function so we'll comment out the arrow
  11877. 10:18:06function using double
  11878. 10:18:08slash and double
  11879. 10:18:11slash now let's
  11880. 10:18:13save and let's open the tab for this
  11881. 10:18:17project and we'll give give it a try so
  11882. 10:18:20click
  11883. 10:18:22autoplay and this still works after
  11884. 10:18:24replacing with an arrow
  11885. 10:18:27function next let's go to the to-do list
  11886. 10:18:30project and switch to Arrow functions so
  11887. 10:18:34we'll go to our code editor and find the
  11888. 10:18:37to-do list project which is
  11889. 10:18:42here and let's scroll
  11890. 10:18:45down so inside render to-do list we're
  11891. 10:18:48using four each and we're passing this
  11892. 10:18:51function into another function so it's
  11893. 10:18:54recommended to use an arrow function
  11894. 10:18:56here so we'll remove the word
  11895. 10:18:59function and after the brackets We'll
  11896. 10:19:02add an
  11897. 10:19:04arrow and again I'll leave this function
  11898. 10:19:08syntax as a regular function because
  11899. 10:19:10it's easier to read and enables hoisting
  11900. 10:19:13so let's
  11901. 10:19:15save and go back to our to-do list
  11902. 10:19:19and everything still
  11903. 10:19:21works next let's go back to 12 Advanced
  11904. 10:19:26functions.
  11905. 10:19:29HTML and we'll scroll to the
  11906. 10:19:32bottom and we'll open the tab for this
  11907. 10:19:36file and we'll review another place we
  11908. 10:19:39could use Arrow functions which is
  11909. 10:19:41inside an object so at the bottom here
  11910. 10:19:45let's create a new object con
  11911. 10:19:49object 2 equals
  11912. 10:19:53object and then inside let's type the
  11913. 10:19:57property method colon and an arrow
  11914. 10:20:01function brackets arrow and curly
  11915. 10:20:06brackets so even though we could save an
  11916. 10:20:08arrow function inside an object like
  11917. 10:20:11this objects already have a shortcut for
  11918. 10:20:15functions so at the bottom we could just
  11919. 10:20:19type
  11920. 10:20:20method brackets and curly
  11921. 10:20:23brackets so this syntax is called the
  11922. 10:20:26shorthand method syntax so this
  11923. 10:20:30shorthand method syntax is actually
  11924. 10:20:32easier to read than an arrow function so
  11925. 10:20:35when saving a function in an object it's
  11926. 10:20:38actually recommended to use this syntax
  11927. 10:20:41instead there's also another difference
  11928. 10:20:44between regular functions and arrow
  11929. 10:20:46functions but we'll learn about it later
  11930. 10:20:48in in this course because it's a more
  11931. 10:20:50advanced
  11932. 10:20:52feature now let's move on and we're
  11933. 10:20:54going to learn another Improvement we
  11934. 10:20:56can make to our projects so let's
  11935. 10:20:59comment out this code we'll scroll up
  11936. 10:21:03and type SL star and at the
  11937. 10:21:08bottom star slash and
  11938. 10:21:12save and next we're actually going to
  11939. 10:21:15scroll up to the very top of this file
  11940. 10:21:19and we're going to create a button on
  11941. 10:21:21the page so inside the body we'll type
  11942. 10:21:25less than button greater than and inside
  11943. 10:21:29the button we'll have the text
  11944. 10:21:33click now let's say that we want to run
  11945. 10:21:36some JavaScript code when we click this
  11946. 10:21:38button so here we can type on click
  11947. 10:21:43equals double quotes and write some
  11948. 10:21:46JavaScript inside this double quotes
  11949. 10:21:48quot now we're going to learn a better
  11950. 10:21:51way of doing this called add event
  11951. 10:21:54listener to use add event listener first
  11952. 10:21:58we're going to get this button and put
  11953. 10:21:59it into our JavaScript using document.
  11954. 10:22:03query selector so let's add a class to
  11955. 10:22:07this button so we can select it and
  11956. 10:22:10we'll set it to
  11957. 10:22:12js-
  11958. 10:22:14button and now inside the script element
  11959. 10:22:17we're going to use
  11960. 10:22:20document. query
  11961. 10:22:23selector brackets and the string do
  11962. 10:22:29js-
  11963. 10:22:31button so now let's save this button in
  11964. 10:22:34a variable so we can use it later at the
  11965. 10:22:37front we'll type
  11966. 10:22:40const button element equals this
  11967. 10:22:46result now every HT H ml element has a
  11968. 10:22:50method called add event listener add
  11969. 10:22:53event listener let us run some code when
  11970. 10:22:56we interact with the element so it sort
  11971. 10:22:59of does the same thing as
  11972. 10:23:02onclick so at the bottom let's type
  11973. 10:23:06button
  11974. 10:23:08element dot add capital E Event capital
  11975. 10:23:14L
  11976. 10:23:15listener and brackets
  11977. 10:23:19so add event listener has two parameters
  11978. 10:23:22the first parameter is the event or what
  11979. 10:23:25type of interaction we want to listen
  11980. 10:23:28for so between these brackets let's type
  11981. 10:23:32the string
  11982. 10:23:34click so this will run some code when we
  11983. 10:23:37click the button the way to find these
  11984. 10:23:40event names is to look at this attribute
  11985. 10:23:44the word after on is the
  11986. 10:23:46event next the second parameter is a
  11987. 10:23:49function that we want to run when we
  11988. 10:23:52click this element so we'll type a comma
  11989. 10:23:57and because we're passing a function
  11990. 10:23:59into another function we're going to use
  11991. 10:24:01an arrow function so we'll type
  11992. 10:24:05brackets arrow and curly
  11993. 10:24:08brackets so inside here let's just do
  11994. 10:24:13console. log string click
  11995. 10:24:18and
  11996. 10:24:20save and now when we click this button
  11997. 10:24:24it will run this function and display
  11998. 10:24:28click so this code does the same thing
  11999. 10:24:32as the onclick attribute however add
  12000. 10:24:35event listener has some advantages over
  12001. 10:24:38using an attribute first it lets us add
  12002. 10:24:42multiple event listeners for an event
  12003. 10:24:45for example let's add another click
  12004. 10:24:48event listener we can select this
  12005. 10:24:51code and
  12006. 10:24:54rightclick
  12007. 10:24:56copy and then down
  12008. 10:24:59here rightclick and
  12009. 10:25:03paste and let's change this to log click
  12010. 10:25:07to if we
  12011. 10:25:09save and click the
  12012. 10:25:11button it will run both these functions
  12013. 10:25:15so add event listener let us add
  12014. 10:25:18multiple listeners to one event another
  12015. 10:25:22advantage of doing this is that we can
  12016. 10:25:24remove an event listener using a method
  12017. 10:25:27called remove event
  12018. 10:25:29listener so under this code we can type
  12019. 10:25:34button
  12020. 10:25:36element do remove capital E Event
  12021. 10:25:42capital L listener and
  12022. 10:25:46brackets and remove EV listener also has
  12023. 10:25:49two
  12024. 10:25:50parameters the first parameter is the
  12025. 10:25:53event so in this case it is the string
  12026. 10:25:58click and we'll type a comma and the
  12027. 10:26:01second parameter is the function that we
  12028. 10:26:04want to remove so that when we click
  12029. 10:26:06this button it will no longer run that
  12030. 10:26:09function now we can't just type a copy
  12031. 10:26:13of this function we need to use this
  12032. 10:26:15exact function inside remove event
  12033. 10:26:18listener to do that we'll save this in a
  12034. 10:26:21variable first so above this let's
  12035. 10:26:25create a
  12036. 10:26:26variable
  12037. 10:26:27const event
  12038. 10:26:31listener equals and we'll select this
  12039. 10:26:34code right click
  12040. 10:26:38copy and in here right click and
  12041. 10:26:44paste and now we'll replace this with
  12042. 10:26:47the VAR
  12043. 10:26:48variable event
  12044. 10:26:52listener and down here we'll remove this
  12045. 10:26:55same
  12046. 10:26:56variable event
  12047. 10:27:00listener if we
  12048. 10:27:02save and click the button again it will
  12049. 10:27:06only display click two because we added
  12050. 10:27:09this event listener and then we removed
  12051. 10:27:12it so it will no longer run this
  12052. 10:27:14function when we click the button so as
  12053. 10:27:17you can see
  12054. 10:27:18add event listener gives us more control
  12055. 10:27:21over event listeners we can add multiple
  12056. 10:27:24listeners to an event and we can also
  12057. 10:27:27remove a listener from an event so
  12058. 10:27:30because of these advantages the best
  12059. 10:27:32practice is to use add event listener
  12060. 10:27:35instead of an onclick attribute so let's
  12061. 10:27:39go back into our projects and switch to
  12062. 10:27:41using add event listener we'll start
  12063. 10:27:44with the rock paper scissors project so
  12064. 10:27:47we'll open our
  12065. 10:27:49files and we'll open 12 rock paper
  12066. 10:27:52scissors and click here and
  12067. 10:27:55save and we'll scroll up to 12 rockpaper
  12068. 10:28:00scissors. JS and for the video I'll drag
  12069. 10:28:04this down to the bottom and I'll hide
  12070. 10:28:07the files for
  12071. 10:28:08now and then rearrange the
  12072. 10:28:12windows and finally open the tab for
  12073. 10:28:15this file
  12074. 10:28:18now let's go to the HTML we'll scroll up
  12075. 10:28:22to these move buttons which is up
  12076. 10:28:27here so here we're using the onclick
  12077. 10:28:30attribute and we're going to switch it
  12078. 10:28:32to add event listener so first let's get
  12079. 10:28:36this button into our
  12080. 10:28:38JavaScript We'll add a class so we can
  12081. 10:28:41select it
  12082. 10:28:43js-
  12083. 10:28:45rock- button
  12084. 10:28:48and
  12085. 10:28:49save and then down
  12086. 10:28:51here let's go below the autoplay
  12087. 10:28:54function right here and we'll use add
  12088. 10:28:58event listener so we'll get this button
  12089. 10:29:01into our JavaScript using
  12090. 10:29:04document do query
  12091. 10:29:08selector brackets and a string dot
  12092. 10:29:13js-
  12093. 10:29:14ro-
  12094. 10:29:16button and we're going to use dot add
  12095. 10:29:21event
  12096. 10:29:23listener
  12097. 10:29:25brackets so we're going to listen for a
  12098. 10:29:29click and a comma and we'll give it a
  12099. 10:29:32function that we want to run so a common
  12100. 10:29:36mistake here is to actually run the
  12101. 10:29:38function for example play game brackets
  12102. 10:29:44and the string
  12103. 10:29:46Rock so we're supposed to give a
  12104. 10:29:49function to add event listener however
  12105. 10:29:53this doesn't result in a function this
  12106. 10:29:56will actually run play game and give us
  12107. 10:29:59the return value which is undefined so
  12108. 10:30:02this code will actually result in
  12109. 10:30:05undefined and then we're giving
  12110. 10:30:06undefined to add event listener so it
  12111. 10:30:10won't
  12112. 10:30:11work so in order to give add event
  12113. 10:30:14listener a function we need to create a
  12114. 10:30:17function here
  12115. 10:30:18so let's remove this code and we'll
  12116. 10:30:21create a function using brackets arrow
  12117. 10:30:25and curly
  12118. 10:30:27brackets and then inside this function
  12119. 10:30:30we're going to call play game so we'll
  12120. 10:30:33type play game brackets and
  12121. 10:30:40rock so now when we click the rock
  12122. 10:30:43button it will run this function and
  12123. 10:30:46play the game so let's remove the
  12124. 10:30:49onclick
  12125. 10:30:51attribute and save the
  12126. 10:30:55HTML and also save the
  12127. 10:30:58JavaScript and then go to our project
  12128. 10:31:01and click the rock button you can see
  12129. 10:31:04that it works except this time we're
  12130. 10:31:06using add event listener so let's do the
  12131. 10:31:10same thing for the paper and the
  12132. 10:31:12scissors button feel free to pause the
  12133. 10:31:14video if you want to try it
  12134. 10:31:16yourself so we'll scroll down here and
  12135. 10:31:20let's add a class to the paper button so
  12136. 10:31:23we can select it
  12137. 10:31:25js-
  12138. 10:31:27paper- button and then in our JavaScript
  12139. 10:31:31we'll make a copy of this code so right
  12140. 10:31:35click
  12141. 10:31:37copy and then down
  12142. 10:31:40here right click and
  12143. 10:31:44paste and now we're going to replace the
  12144. 10:31:47rock button
  12145. 10:31:48with
  12146. 10:31:50paper and when we click this button
  12147. 10:31:53we're going to play the game with
  12148. 10:31:56paper now let's
  12149. 10:31:58save and we'll remove the onclick
  12150. 10:32:03attribute and save the
  12151. 10:32:06HTML if we click the paper button it
  12152. 10:32:09works as
  12153. 10:32:11before finally let's do the scissors
  12154. 10:32:14button so we'll add a class Js
  12155. 10:32:18s- scissors dasb
  12156. 10:32:22button and then inside our JavaScript
  12157. 10:32:25we'll make a copy of this rightclick
  12158. 10:32:30copy and then down here create some new
  12159. 10:32:33lines rightclick and
  12160. 10:32:37paste and now switch to the scissors
  12161. 10:32:41button and play game with
  12162. 10:32:45scissors let's save
  12163. 10:32:48and remove the onclick
  12164. 10:32:51attribute save the
  12165. 10:32:54HTML and try the scissors
  12166. 10:32:56button so everything still
  12167. 10:32:59works and I'll leave switching the reset
  12168. 10:33:02score button and the autop playay button
  12169. 10:33:05as
  12170. 10:33:06exercises so now let's try add event
  12171. 10:33:09listener with a different event we'll
  12172. 10:33:11use it with key down so let's say that
  12173. 10:33:14in our project we want to make it so we
  12174. 10:33:17can play the the game with our keyboard
  12175. 10:33:19if we Type R it will play Rock if we
  12176. 10:33:23type P it will play paper and if we Type
  12177. 10:33:26S it will play
  12178. 10:33:29scissors let's go into our
  12179. 10:33:31JavaScript and we'll scroll
  12180. 10:33:34down and we're going to add an event
  12181. 10:33:36listener to the body this time so that
  12182. 10:33:39if we type anywhere on the page we can
  12183. 10:33:41run some code so we're going to get the
  12184. 10:33:44body element using document
  12185. 10:33:49body and
  12186. 10:33:51then add event listener and
  12187. 10:33:57brackets this time the first parameter
  12188. 10:34:00or the event will be the string key
  12189. 10:34:04down and let's type comma and give it a
  12190. 10:34:08function that we want to run brackets
  12191. 10:34:12arrow and curly
  12192. 10:34:14brackets inside here let's just type
  12193. 10:34:20console.log key down for now and
  12194. 10:34:25save so now if we go to the project and
  12195. 10:34:28we open the console
  12196. 10:34:31rightclick and click
  12197. 10:34:33inspect click the
  12198. 10:34:35console and then click on the page if we
  12199. 10:34:39type a key on our
  12200. 10:34:41keyboard it will run this function and
  12201. 10:34:44display key down so now we have to check
  12202. 10:34:48what key was pressed and play the game
  12203. 10:34:52so how do we know which key that we
  12204. 10:34:54pressed earlier in this course we used
  12205. 10:34:57the attribute on key down and we learned
  12206. 10:35:00that it gets a special object called
  12207. 10:35:03event and the event object contains
  12208. 10:35:05which key was pressed add event listener
  12209. 10:35:09also provides this event object but it
  12210. 10:35:12provides it as a parameter to this
  12211. 10:35:14function so inside the round brackets
  12212. 10:35:17let's add a parameter
  12213. 10:35:20event now every time we type on our
  12214. 10:35:23keyboard add event listener will save
  12215. 10:35:26the event object in here and run the
  12216. 10:35:29function and this event object contains
  12217. 10:35:32the key that we
  12218. 10:35:34pressed so in the console.log let's
  12219. 10:35:37display event. key this time and
  12220. 10:35:42save now if we click on the page and
  12221. 10:35:46type R
  12222. 10:35:48event. key will contain R if we press P
  12223. 10:35:53event. key will contain P so that's how
  12224. 10:35:57we know which key we pressed so now
  12225. 10:36:00let's close this and instead of
  12226. 10:36:03console.log we'll create an if statement
  12227. 10:36:06to check which key we pressed so if
  12228. 10:36:10brackets and curly
  12229. 10:36:12brackets and here we'll check if event.
  12230. 10:36:17key
  12231. 10:36:18is equal to the string R if it is we're
  12232. 10:36:23going to play the game with rock so
  12233. 10:36:27inside here we can just run this play
  12234. 10:36:30game
  12235. 10:36:31function play game and brackets and
  12236. 10:36:36inside we'll type the string
  12237. 10:36:40Rock now let's check if we pressed P so
  12238. 10:36:44we'll add another Branch else if
  12239. 10:36:48brackets and curly brackets and in the
  12240. 10:36:51second condition we'll check if event.
  12241. 10:36:55key is equal to the string P if it is
  12242. 10:37:01we'll play the game with paper so inside
  12243. 10:37:05here we'll run play game brackets and
  12244. 10:37:10the string paper and finally we'll check
  12245. 10:37:14if we pressed s so at the end We'll add
  12246. 10:37:18another else if brackets curly brackets
  12247. 10:37:23and inside here we'll check if event.
  12248. 10:37:28key is equal to the string s if it is
  12249. 10:37:34we'll play the
  12250. 10:37:37game with the string
  12251. 10:37:40scissors now let's
  12252. 10:37:43save click our page and type R
  12253. 10:37:48and it plays the game with rock if we
  12254. 10:37:51type P it will play the game with paper
  12255. 10:37:55and if we Type S it will play the game
  12256. 10:37:58with
  12257. 10:37:59scissors so that's an example of using
  12258. 10:38:02add event listener with a different type
  12259. 10:38:04of event with a key down if we need the
  12260. 10:38:08event object we can get it using a
  12261. 10:38:11parameter up here so those are all the
  12262. 10:38:14features we're going to create for our
  12263. 10:38:16rock paper scissors project
  12264. 10:38:18now let's move on to the to-do list
  12265. 10:38:20project and switch to add event listener
  12266. 10:38:23we'll go into our code editor click up
  12267. 10:38:27here and find 12 to-do list.
  12268. 10:38:32HTML we'll also scroll up to the scripts
  12269. 10:38:35folder find 12 to-do
  12270. 10:38:38list.js and open this file and I'm going
  12271. 10:38:41to put it at the bottom here so I can
  12272. 10:38:43see it side by side I'll close this for
  12273. 10:38:46now
  12274. 10:38:47and close rock paper
  12275. 10:38:50scissors and then open the tab for this
  12276. 10:38:54project okay so let's start by switching
  12277. 10:38:57the add button to use add event listener
  12278. 10:39:00we'll go into our HTML and find the add
  12279. 10:39:04button which is here and we're going to
  12280. 10:39:07add a class so we can select it in
  12281. 10:39:10JavaScript
  12282. 10:39:12js-
  12283. 10:39:13add- too dasb button
  12284. 10:39:18now let's go to our JavaScript and
  12285. 10:39:21scroll
  12286. 10:39:22down above the add Todo
  12287. 10:39:25function we'll get this button into our
  12288. 10:39:28JavaScript using
  12289. 10:39:31document. query
  12290. 10:39:34selector brackets and then a string Dot
  12291. 10:39:39and the class we want to get
  12292. 10:39:42js- add- too dasb button
  12293. 10:39:48and then after this we use do add event
  12294. 10:39:55listener
  12295. 10:39:57brackets inside these brackets we'll
  12296. 10:40:00give it the event the string click comma
  12297. 10:40:05and then a function to run when this
  12298. 10:40:07event happens so brackets arrow and
  12299. 10:40:12curly
  12300. 10:40:13brackets and in here we just want to do
  12301. 10:40:16the same thing as the onclick attribute
  12302. 10:40:19which is ADD too and call this function
  12303. 10:40:25let's
  12304. 10:40:26save and then in the HTML let's remove
  12305. 10:40:30the onclick
  12306. 10:40:32attribute and
  12307. 10:40:35save and let's try it out so we'll type
  12308. 10:40:38A
  12309. 10:40:39too watch
  12310. 10:40:42YouTube select a due
  12311. 10:40:45date and then click add
  12312. 10:40:48and the add button still works as before
  12313. 10:40:51but we're just using add event
  12314. 10:40:53listener next let's switch all the
  12315. 10:40:56delete buttons let's scroll up to where
  12316. 10:40:59we create these delete buttons which is
  12317. 10:41:03here and first We'll add a class to all
  12318. 10:41:06the delete buttons so we can select them
  12319. 10:41:08in
  12320. 10:41:09JavaScript
  12321. 10:41:11js- delete Das too dasb button
  12322. 10:41:18and now we're going to use document.
  12323. 10:41:20Query selector and add an event listener
  12324. 10:41:23to all of these buttons however there's
  12325. 10:41:26two problems here first is that at this
  12326. 10:41:29point in the code this is just a string
  12327. 10:41:33we haven't actually put this button on
  12328. 10:41:35the page yet we do that down here in
  12329. 10:41:39this line of code we put the HTML that
  12330. 10:41:41we generated onto the page so in order
  12331. 10:41:45to add an event listener we need to put
  12332. 10:41:48the HTML on the page first so we have to
  12333. 10:41:51do it after this code so down here we're
  12334. 10:41:56going to type
  12335. 10:41:58document. query
  12336. 10:42:01selector brackets and the string Dot and
  12337. 10:42:06we'll select this class
  12338. 10:42:09js- delete Das too
  12339. 10:42:15Dutton however there's also a second
  12340. 10:42:17problem here which is we're going to
  12341. 10:42:20have multiple delete
  12342. 10:42:22buttons and query selector only gives us
  12343. 10:42:25the first one so how do we select all
  12344. 10:42:28the buttons on the page that match this
  12345. 10:42:31criteria instead of just the first one
  12346. 10:42:34to do that the document object has
  12347. 10:42:37another method called query
  12348. 10:42:40selector
  12349. 10:42:41all so query selector all will give us
  12350. 10:42:45all the elements on the page that have
  12351. 10:42:47the class JS delete Todo button so down
  12352. 10:42:52here let's console.log this result to
  12353. 10:42:55see what it looks like
  12354. 10:42:57[Music]
  12355. 10:42:59console.log brackets and close bracket
  12356. 10:43:03and
  12357. 10:43:05save and then here
  12358. 10:43:08rightclick
  12359. 10:43:10inspect and the
  12360. 10:43:12console and expand this you'll see that
  12361. 10:43:17document. query selector all gives us a
  12362. 10:43:20list of elements and this list works
  12363. 10:43:23just like an array this is index zero or
  12364. 10:43:27the first button and this is index one
  12365. 10:43:31which is the second button so now let's
  12366. 10:43:34remove
  12367. 10:43:37console.log and add event listeners to
  12368. 10:43:40all of these
  12369. 10:43:42buttons to do that we're going to Loop
  12370. 10:43:45through this list of delete buttons
  12371. 10:43:47so again this list works like an array
  12372. 10:43:50so we can use a for each method to Loop
  12373. 10:43:53through it so at the end let's type
  12374. 10:43:58enter and tab and then dot for each and
  12375. 10:44:06brackets as usual we'll give for each a
  12376. 10:44:09function that we want to run for each of
  12377. 10:44:12these values so we'll type brackets
  12378. 10:44:16Arrow and curly
  12379. 10:44:18brackets and now every time we Loop
  12380. 10:44:21through the list for each will give us
  12381. 10:44:24two parameters the first one is the
  12382. 10:44:27value in the list which is a delete
  12383. 10:44:29button element so let's name it delete
  12384. 10:44:35button and we'll type comma and the
  12385. 10:44:38second parameter is the index so
  12386. 10:44:42index so now we want to add an event
  12387. 10:44:45listener to each delete button so inside
  12388. 10:44:49here we'll type delete button dot add
  12389. 10:44:56event listener and
  12390. 10:45:00brackets inside these brackets we'll
  12391. 10:45:02tell it what event we want to listen to
  12392. 10:45:05which is the string click and comma and
  12393. 10:45:10then a function that we want to run when
  12394. 10:45:13this event happens so brackets Arrow and
  12395. 10:45:17curly
  12396. 10:45:19brackets so now when we click a delete
  12397. 10:45:22button we want to do the same thing as
  12398. 10:45:24this onclick attribute above so let's
  12399. 10:45:28select this
  12400. 10:45:29code we'll
  12401. 10:45:31rightclick
  12402. 10:45:33copy and then down
  12403. 10:45:36here right click and
  12404. 10:45:40paste and now we can remove these
  12405. 10:45:42characters because we're not in a string
  12406. 10:45:45anymore and that's all we need to
  12407. 10:45:48do so to summarize we used query
  12408. 10:45:51selector all to get a list of all the
  12409. 10:45:55delete buttons on the page and then we
  12410. 10:45:57looped through the list using four each
  12411. 10:46:01and for each delete button we added a
  12412. 10:46:04click event listener and ran the same
  12413. 10:46:06code that we did before so now we can
  12414. 10:46:09scroll up and remove this onclick
  12415. 10:46:14attribute and Save
  12416. 10:46:18go back to the page close a console and
  12417. 10:46:22try out the delete
  12418. 10:46:23button and you can see that it still
  12419. 10:46:26works so that's how we switch the to-do
  12420. 10:46:29list project to use add event
  12421. 10:46:31listener so now we're finished improving
  12422. 10:46:34all of our
  12423. 10:46:35projects in the rest of this lesson
  12424. 10:46:38we're going to learn more features that
  12425. 10:46:39use functions as
  12426. 10:46:42values so let's close this for now and
  12427. 10:46:45we'll go back to
  12428. 10:46:4712 Advanced functions.
  12429. 10:46:51HTML and we'll open the tab for this
  12430. 10:46:54file and let's also comment out this
  12431. 10:46:57JavaScript code using
  12432. 10:47:02slashstar and star
  12433. 10:47:05slash let's
  12434. 10:47:07save and now let's scroll to the bottom
  12435. 10:47:10of this
  12436. 10:47:12code and create some new
  12437. 10:47:15lines so in this lesson we learned the
  12438. 10:47:18array method for each which lets us Loop
  12439. 10:47:22through an array now we're going to
  12440. 10:47:24learn two more array methods called
  12441. 10:47:27filter and map so let's start with
  12442. 10:47:30Filter let's say we have an array of
  12443. 10:47:33numbers and we want to create a copy of
  12444. 10:47:36this array with all the negative numbers
  12445. 10:47:39removed so let's go up here and create
  12446. 10:47:43an array of the number one comma -3
  12447. 10:47:49comma 5 and type the method do
  12448. 10:47:54filter and
  12449. 10:47:56brackets so Filter Works the same way as
  12450. 10:48:00for each we give it a function so
  12451. 10:48:03bracket arrow and curly bracket and this
  12452. 10:48:08function has two parameters the
  12453. 10:48:11value and the
  12454. 10:48:14index so filter will take the first
  12455. 10:48:16first value save it in here and run the
  12456. 10:48:19function then it will take the second
  12457. 10:48:21value save it in here run the function
  12458. 10:48:25and the third value save it and run the
  12459. 10:48:28function now the only difference is that
  12460. 10:48:31this time this inner function will
  12461. 10:48:33return a value so we'll type
  12462. 10:48:37return and we're going to return a
  12463. 10:48:39Boolean like
  12464. 10:48:42true so now the return value matters so
  12465. 10:48:46what filter does is it creates a new
  12466. 10:48:49array if the inner function returns true
  12467. 10:48:52it will put this value in the new array
  12468. 10:48:55if the inner function returns false it
  12469. 10:48:58will not put this value in the new array
  12470. 10:49:01for example at the front let's
  12471. 10:49:04console.log the result of this so we'll
  12472. 10:49:07type
  12473. 10:49:10console.log Open Bracket and down here
  12474. 10:49:14close bracket and Save
  12475. 10:49:17so in this example the inner function
  12476. 10:49:20always returns true so all three values
  12477. 10:49:23will be in the new array if we change
  12478. 10:49:26the return value to false every time and
  12479. 10:49:31save all three values will not be added
  12480. 10:49:35to the new
  12481. 10:49:36array so now let's use this to solve our
  12482. 10:49:39problem we want to remove the negative
  12483. 10:49:42numbers from this array so inside this
  12484. 10:49:45inner function we're going to return
  12485. 10:49:47true if the number is positive and
  12486. 10:49:50return false if the number is
  12487. 10:49:52negative so we'll check if value is
  12488. 10:49:57greater than or equal to
  12489. 10:50:00zero curly brackets will return true and
  12490. 10:50:05include it in the new
  12491. 10:50:07array
  12492. 10:50:09else will return false and not include
  12493. 10:50:14it in the new array so let's remove this
  12494. 10:50:19and
  12495. 10:50:20save so now only values that are greater
  12496. 10:50:23than zero will be in the new copy so
  12497. 10:50:26using this we can sort of filter out
  12498. 10:50:29values based on a criteria that's why
  12499. 10:50:32this method is called
  12500. 10:50:34filter now we can shorten this logic by
  12501. 10:50:38using this code return value greater
  12502. 10:50:43than or equal to zero so this comparison
  12503. 10:50:47will return true if it's greater than or
  12504. 10:50:49equal to zero and it will return false
  12505. 10:50:52otherwise so this line does the same
  12506. 10:50:55thing as this code up here so we can
  12507. 10:50:59comment this out with slashstar and star
  12508. 10:51:03slash and
  12509. 10:51:05save and as always we can return a
  12510. 10:51:09truthy or falsy value instead of just
  12511. 10:51:12true or
  12512. 10:51:13false the last array method we're going
  12513. 10:51:15to learn is called map so let's create
  12514. 10:51:19some new lines and scroll
  12515. 10:51:22down and let's say we have an array of
  12516. 10:51:25numbers again like the array 1 1 3 and
  12517. 10:51:33we want to make a copy of this array
  12518. 10:51:35with each number
  12519. 10:51:37doubled so at the end we'll
  12520. 10:51:41type map
  12521. 10:51:43brackets and Map works just like for
  12522. 10:51:46each and filter between the brackets
  12523. 10:51:49we'll give it a function bracket arrow
  12524. 10:51:54and curly brackets and this function
  12525. 10:51:57will get two
  12526. 10:51:58parameters the first parameter is each
  12527. 10:52:01value in the array and the second
  12528. 10:52:05parameter comma is the
  12529. 10:52:08index so map will take the first value
  12530. 10:52:12save it in this parameter and run the
  12531. 10:52:14function take the second value
  12532. 10:52:16save it in this parameter run the
  12533. 10:52:18function third value save it and run the
  12534. 10:52:22function and inside this function we're
  12535. 10:52:24going to return a
  12536. 10:52:27value so what map does is it creates a
  12537. 10:52:31new array and then whatever we return
  12538. 10:52:34will be added to the new array so if we
  12539. 10:52:37return
  12540. 10:52:3810 and then we console.log this result
  12541. 10:52:43console.log
  12542. 10:52:47and close bracket and
  12543. 10:52:49save it will create an array where each
  12544. 10:52:52value becomes
  12545. 10:52:5410 now the interesting thing is that we
  12546. 10:52:57can actually use the value itself in the
  12547. 10:53:00return so here if we did value + 10 and
  12548. 10:53:08save it will take each value in the
  12549. 10:53:11original array add a 10 and put it in
  12550. 10:53:14the new array
  12551. 10:53:17so map will transform an array into
  12552. 10:53:20another array based on the return value
  12553. 10:53:23so if we want to return an array where
  12554. 10:53:26each number is doubled we just have to
  12555. 10:53:28multiply each value by two and save and
  12556. 10:53:34this will give us 2
  12557. 10:53:3826 next we're going to review some
  12558. 10:53:40shortcuts of Arrow functions to make
  12559. 10:53:43this code more
  12560. 10:53:44compact first let's make a copy of this
  12561. 10:53:48code rightclick
  12562. 10:53:51copy and down
  12563. 10:53:54here rightclick and
  12564. 10:53:57paste so notice in the inner function
  12565. 10:54:00we're not using index so let's just
  12566. 10:54:03remove it for now and now that this
  12567. 10:54:06Arrow function only has one parameter
  12568. 10:54:08these round brackets are optional so we
  12569. 10:54:11can also remove these to make the code
  12570. 10:54:14more compact another shortcut is if we
  12571. 10:54:18only have one line of code in an arrow
  12572. 10:54:21function we can put the code on one line
  12573. 10:54:24like
  12574. 10:54:26this and now the curly brackets and the
  12575. 10:54:29return are optional so we can actually
  12576. 10:54:32just remove all of this
  12577. 10:54:36code so if we have just an arrow it will
  12578. 10:54:39automatically return this result on the
  12579. 10:54:42right and we don't have to type return
  12580. 10:54:45ourselves
  12581. 10:54:47so this code does the same thing as the
  12582. 10:54:49code above but as you can see these
  12583. 10:54:52shortcuts make our code very Compact and
  12584. 10:54:55clean so let's save and this will give
  12585. 10:54:59us the same
  12586. 10:55:01result the last thing we're going to
  12587. 10:55:03learn in this lesson is a feature of
  12588. 10:55:05functions called a closure a closure
  12589. 10:55:08means if a function has access to a
  12590. 10:55:11value it will always have access to that
  12591. 10:55:15value so let's take a look at an example
  12592. 10:55:18we're going to go back to the to-do list
  12593. 10:55:20project so we'll click up here and then
  12594. 10:55:24scroll up to 12 to-do
  12595. 10:55:28list.js and close this and then open the
  12596. 10:55:31tab for the to-do list which is here and
  12597. 10:55:35let's scroll down to the query selector
  12598. 10:55:40all so we're going to focus on this
  12599. 10:55:43inner function here so notice that this
  12600. 10:55:46fun function uses index from above now
  12601. 10:55:50the interesting thing about index is
  12602. 10:55:52that as soon as this for each ends index
  12603. 10:55:55gets deleted for example if we add some
  12604. 10:55:59new lines and we try to
  12605. 10:56:02console.
  12606. 10:56:04log
  12607. 10:56:06index and
  12608. 10:56:08save and then go into our project
  12609. 10:56:12rightclick
  12610. 10:56:13inspect and the con
  12611. 10:56:17it'll tell us that the index is not
  12612. 10:56:20defined so as soon as we end the for
  12613. 10:56:23each Loop index doesn't exist
  12614. 10:56:25anymore however if we move this into the
  12615. 10:56:29inner function so inside here and we
  12616. 10:56:36console.log
  12617. 10:56:38index and
  12618. 10:56:40save and we click a delete button it
  12619. 10:56:44will tell us that the index is zero so
  12620. 10:56:48even though index gets deleted right
  12621. 10:56:50away down here if we click one of these
  12622. 10:56:53buttons later on it still has access to
  12623. 10:56:57Index this feature is called a closure
  12624. 10:57:01if a function has access to a value it
  12625. 10:57:04will always have access to that value
  12626. 10:57:07the value like index gets packaged
  12627. 10:57:10together or enclosed with the function
  12628. 10:57:13that's why we call it a closure
  12629. 10:57:16so even if the value index gets deleted
  12630. 10:57:19after the loop we can click the delete
  12631. 10:57:22button 5 seconds later or even 5 minutes
  12632. 10:57:25later and this function will always have
  12633. 10:57:28access to
  12634. 10:57:29index so closures are something that
  12635. 10:57:32naturally happens as we write JavaScript
  12636. 10:57:34code and it's nice to know how this
  12637. 10:57:37feature works there are some Advanced
  12638. 10:57:39Techniques that we can do with closures
  12639. 10:57:41but they're not used that often so I
  12640. 10:57:44won't be covering them in this course
  12641. 10:57:46but maybe in a different video now let's
  12642. 10:57:49remove
  12643. 10:57:50console.log and
  12644. 10:57:53save and that's the end of this lesson
  12645. 10:57:56in this lesson we learned that functions
  12646. 10:57:59are values we learned set time out and
  12647. 10:58:02set interval which lets us run a
  12648. 10:58:04function in the future we learned an
  12649. 10:58:07array method called for each which is a
  12650. 10:58:10cleaner way of looping through an array
  12651. 10:58:13we used four each Arrow functions and
  12652. 10:58:16and add event listener to improve our
  12653. 10:58:18projects we learned two more array
  12654. 10:58:21methods filter and map and we learned a
  12655. 10:58:25feature of functions called
  12656. 10:58:28closures here are some exercises to help
  12657. 10:58:30you practice what we learned in this
  12658. 10:58:32lesson
  12659. 10:59:00in this lesson we're going to start the
  12660. 10:59:03Amazon Project and we're also going to
  12661. 10:59:05learn a very useful technology called
  12662. 10:59:08git so first let's take a look at the
  12663. 10:59:11Amazon Project to see how it works we're
  12664. 10:59:14going to go to our browser
  12665. 10:59:16and create a new tab at the top and
  12666. 10:59:20we're going to type super simple dodev
  12667. 10:59:25SL projects SL Amazon and press
  12668. 10:59:33enter so this is going to be the project
  12669. 10:59:36we're going to build in this course this
  12670. 10:59:38is an e-commerce website that looks like
  12671. 10:59:41Amazon where we can see a list of
  12672. 10:59:43products on the homepage and then we can
  12673. 10:59:46add these products to a cart by clicking
  12674. 10:59:49add to
  12675. 10:59:51cart and then we can see our cart by
  12676. 10:59:54clicking the top
  12677. 10:59:58right and on this page it will show us
  12678. 11:00:01which products are in our cart and it
  12679. 11:00:04will also show us the cost of our order
  12680. 11:00:07including shipping and taxes and once
  12681. 11:00:11we're ready to create an order we can
  12682. 11:00:13click this button to create the order
  12683. 11:00:17and it will take us to this page which
  12684. 11:00:19shows us a list of our past orders we
  12685. 11:00:22can also track our order by clicking
  12686. 11:00:25this track package
  12687. 11:00:27button and it will take us to this page
  12688. 11:00:30which tells us the progress of our order
  12689. 11:00:32whether it's preparing it's shipped and
  12690. 11:00:35it's
  12691. 11:00:36delivered so that's an overview of the
  12692. 11:00:39project we're going to built this is a
  12693. 11:00:41complex multi-page website where the
  12694. 11:00:44different pages interact with each other
  12695. 11:00:46to create a full app the first thing
  12696. 11:00:50we're going to do is to download the
  12697. 11:00:52starting code for this project we're
  12698. 11:00:54going to create a new
  12699. 11:00:57tab and here we're going to type
  12700. 11:01:01github.com
  12701. 11:01:05slup
  12702. 11:01:07simple Dev SL JavaScript D Amazon d
  12703. 11:01:13project and press enter
  12704. 11:01:19you can also find the starting code in
  12705. 11:01:21the description by clicking
  12706. 11:01:25here and then going to the links for
  12707. 11:01:28this
  12708. 11:01:35lesson and on this page we can see the
  12709. 11:01:38starting code for the Amazon Project so
  12710. 11:01:42this code contains all the HTML and CSS
  12711. 11:01:46that we need the reason we're going to
  12712. 11:01:48use some starting code instead of
  12713. 11:01:50writing all of this ourselves is because
  12714. 11:01:52this course is focused on JavaScript not
  12715. 11:01:55HTML and CSS if we were to write all of
  12716. 11:01:59this code ourselves it would just take
  12717. 11:02:01up too much time and it would be an
  12718. 11:02:03entire course by itself if you want to
  12719. 11:02:06learn how to create this code you can
  12720. 11:02:08check out my HTML and CSS full course
  12721. 11:02:11which would teach you all the skills
  12722. 11:02:13that you need but for this JavaScript
  12723. 11:02:16course we're going to start with the
  12724. 11:02:18HTML and CSS here and we're just going
  12725. 11:02:21to focus on adding JavaScript to this
  12726. 11:02:24website so now let's download all of
  12727. 11:02:27this code we can do that by clicking
  12728. 11:02:29this green code button here and then
  12729. 11:02:33click download
  12730. 11:02:36zip once that's finished downloading
  12731. 11:02:38we're going to go to our downloads
  12732. 11:02:40folder so for me it's this folder right
  12733. 11:02:43here called downloads
  12734. 11:02:47and then we're going to unzip this file
  12735. 11:02:50by double clicking
  12736. 11:02:51it and this will give us a folder that
  12737. 11:02:54contains all the code so we're going to
  12738. 11:02:56select this folder and move it to our
  12739. 11:03:03desktop next let's go to our desktop so
  12740. 11:03:07we're going to close this for now and
  12741. 11:03:09I'll hide the
  12742. 11:03:11browser and on the desktop we're going
  12743. 11:03:14to select the folder that we downloaded
  12744. 11:03:16and we're going to rename this so at the
  12745. 11:03:19end we'll remove Main and press enter to
  12746. 11:03:24rename it to JavaScript Amazon
  12747. 11:03:27Project next we're going to take this
  12748. 11:03:29folder and open it in our code editor so
  12749. 11:03:33let's open vs
  12750. 11:03:34code and we're going to click
  12751. 11:03:38file open
  12752. 11:03:41folder and we're going to find the
  12753. 11:03:42folder that we downloaded for me it's on
  12754. 11:03:45the desktop and it's this folder right
  12755. 11:03:47here so we'll click this folder and then
  12756. 11:03:51click select
  12757. 11:03:54folder and then we can close this
  12758. 11:03:56welcome
  12759. 11:03:57message okay so now we have our starting
  12760. 11:04:00code and we're going to set up the
  12761. 11:04:03project so first let's open our browser
  12762. 11:04:07again and we can close the GitHub
  12763. 11:04:10page as well as the tabs for the
  12764. 11:04:13previous lessons so we'll close these
  12765. 11:04:16tabs right
  12766. 11:04:17here but we'll leave the final Amazon
  12767. 11:04:20Project
  12768. 11:04:21open next let's go back to vs
  12769. 11:04:24code and we're going to open this
  12770. 11:04:27project in our browser so we're going to
  12771. 11:04:29select this file amazon.
  12772. 11:04:32HTML so this file corresponds to the
  12773. 11:04:35homepage here so let's select this file
  12774. 11:04:39and then
  12775. 11:04:40rightclick open with live server
  12776. 11:04:45so I'm just going to rearrange my
  12777. 11:04:47windows here so we can see these side by
  12778. 11:04:51side and now we're ready to begin so
  12779. 11:04:55let's take a look at the code that we
  12780. 11:04:57have to start with as I mentioned all we
  12781. 11:05:00have here is the HTML and CSS for this
  12782. 11:05:03website there's currently no JavaScript
  12783. 11:05:06at all on this
  12784. 11:05:07page so one thing you'll notice is that
  12785. 11:05:10we can actually create almost all of the
  12786. 11:05:12website using just HTML and CSS we
  12787. 11:05:17actually don't even need JavaScript to
  12788. 11:05:19create all of this we're able to create
  12789. 11:05:21the header at the top as well as three
  12790. 11:05:24products to start with and this shows
  12791. 11:05:27that HTML and CSS can pretty much create
  12792. 11:05:30everything that we see visually on the
  12793. 11:05:33website where JavaScript comes in is
  12794. 11:05:36that JavaScript makes this website
  12795. 11:05:39interactive so right now if we click the
  12796. 11:05:42add to card button it looks pretty nice
  12797. 11:05:45but it doesn't actually do anything and
  12798. 11:05:48that's because we haven't added
  12799. 11:05:49JavaScript yet so the role of JavaScript
  12800. 11:05:52is to make our website interactive after
  12801. 11:05:55we created all the content and The
  12802. 11:05:58Styling now before we begin adding
  12803. 11:06:01JavaScript to this website we're
  12804. 11:06:03actually going to take some time and
  12805. 11:06:05learn another very useful technology
  12806. 11:06:07called git so git is a technology that
  12807. 11:06:11lets us track changes in our code for
  12808. 11:06:15examp example do not follow along here
  12809. 11:06:18but let's say that I'm working on this
  12810. 11:06:20website and I go into my code editor and
  12811. 11:06:24I change some code here so let's say
  12812. 11:06:26that I add some code
  12813. 11:06:29here and then in this file I'm going to
  12814. 11:06:32delete some code so I just select some
  12815. 11:06:34code and delete it and then
  12816. 11:06:39save and then in this file I'm going to
  12817. 11:06:42change some code at the bottom
  12818. 11:06:47so now let's say that I made a mistake
  12819. 11:06:50and I want to undo all the changes that
  12820. 11:06:52I just made so how would I do that how
  12821. 11:06:55would I remember all the code that I
  12822. 11:06:57changed in the different files so right
  12823. 11:07:00now this is really difficult and this is
  12824. 11:07:02a problem that we run into in bigger
  12825. 11:07:05projects we're changing lots of code in
  12826. 11:07:07different files and it's really hard to
  12827. 11:07:09keep track of what code has changed and
  12828. 11:07:13this is where git comes in git lets us
  12829. 11:07:16track changes in our code and it helps
  12830. 11:07:19us solve this
  12831. 11:07:21problem so here's what it looks like if
  12832. 11:07:23I have git set up in my project I can go
  12833. 11:07:27into this section of VSS code this
  12834. 11:07:30section is for git and then here git
  12835. 11:07:33will tell me which files I changed and
  12836. 11:07:36if I click these files it will tell me
  12837. 11:07:38which lines I changed and exactly which
  12838. 11:07:41code I changed if I want to I can click
  12839. 11:07:45this button
  12840. 11:07:46to reverse all the changes in this
  12841. 11:07:49file I can also click here to reverse
  12842. 11:07:52all the changes in all the
  12843. 11:07:56files so as you can see kit helps us
  12844. 11:08:00track changes in our code and it lets us
  12845. 11:08:03easily undo our changes if we need
  12846. 11:08:06to and this technology is really useful
  12847. 11:08:09in bigger projects like this Amazon
  12848. 11:08:12Project where we're going to be changing
  12849. 11:08:14lots of code in in many different files
  12850. 11:08:17so before we start this project we're
  12851. 11:08:19going to learn how to set up and use
  12852. 11:08:22git the first thing we need to do is to
  12853. 11:08:25install git to do that we can go to this
  12854. 11:08:29section of vs code called Source
  12855. 11:08:33control now if this section looks like
  12856. 11:08:35this with a button initialize repository
  12857. 11:08:39that means you already have git
  12858. 11:08:42installed otherwise if you're on Windows
  12859. 11:08:45you can click this button and then click
  12860. 11:08:49open or you can go to this website
  12861. 11:08:53directly and then click this link to
  12862. 11:08:56download
  12863. 11:08:58git once it's finished downloading we're
  12864. 11:09:01going to run the installer and then just
  12865. 11:09:04go with the default
  12866. 11:09:06options if you're using a Mac you can
  12867. 11:09:09click up
  12868. 11:09:10here and then type
  12869. 11:09:13terminal and press enter
  12870. 11:09:17and inside here you're going to type git
  12871. 11:09:19and press
  12872. 11:09:21enter and then click this link and
  12873. 11:09:24install
  12874. 11:09:30git all right after installing git we're
  12875. 11:09:34going to restart vs
  12876. 11:09:39code and make sure you have the Amazon
  12877. 11:09:42Project
  12878. 11:09:43open and now if if we go into the git
  12879. 11:09:47section it should look like this with
  12880. 11:09:50the button initialize
  12881. 11:09:52repository so now that we've installed
  12882. 11:09:55git we're going to set up git in our
  12883. 11:09:58Amazon Project we can do that by
  12884. 11:10:01clicking initialize
  12885. 11:10:05repository if this button doesn't work
  12886. 11:10:07for you you can also try going to the
  12887. 11:10:10terminal menu and click new terminal
  12888. 11:10:15and then inside this terminal type kit
  12889. 11:10:19in it and press
  12890. 11:10:25enter so a repository is just a folder
  12891. 11:10:29where changes are being tracked by
  12892. 11:10:31clicking initialize repository this
  12893. 11:10:34turns the current folder which is
  12894. 11:10:37Javascript Amazon Project into a
  12895. 11:10:40repository and now all the changes
  12896. 11:10:42inside this folder will be tracked by
  12897. 11:10:45git so to start off git will tell us
  12898. 11:10:48that all of these files are new files
  12899. 11:10:52and that's because we just set up this
  12900. 11:10:54repository so all of these files are
  12901. 11:10:56considered new changes that we're
  12902. 11:10:59starting to keep track
  12903. 11:11:01of next we need to configure a username
  12904. 11:11:05and email with Git now don't worry this
  12905. 11:11:08information is not used anywhere online
  12906. 11:11:11this is just forget to add an author to
  12907. 11:11:14the changes that we make so to configure
  12908. 11:11:17a username and email we're going to go
  12909. 11:11:20to the
  12910. 11:11:21top and open the terminal menu and click
  12911. 11:11:25new
  12912. 11:11:26terminal and then inside
  12913. 11:11:29here we're going to type get config
  12914. 11:11:35user.name and double
  12915. 11:11:39quotes now if this looks familiar to you
  12916. 11:11:41and you've already done this before you
  12917. 11:11:43can skip this step and and the next
  12918. 11:11:46step so between these double quotes
  12919. 11:11:49we'll pick a username that we want to
  12920. 11:11:51use so for me I'll use super simple Dev
  12921. 11:11:58and press
  12922. 11:12:00enter and we'll also configure an email
  12923. 11:12:03by typing get config user. Emil double
  12924. 11:12:10quotes and between these double quotes
  12925. 11:12:13we'll put an email so I'll put Simon at
  12926. 11:12:19superimple
  12927. 11:12:21dodev and press
  12928. 11:12:24enter so now that we configured a
  12929. 11:12:27username and email we can close this
  12930. 11:12:33terminal next we're going to save these
  12931. 11:12:36changes into git so that git will start
  12932. 11:12:39tracking any new changes that we make to
  12933. 11:12:43save these changes we're just going to
  12934. 11:12:44type type a message here describing what
  12935. 11:12:47we changed so let's just type the
  12936. 11:12:50message add the starting
  12937. 11:12:55files and then we're going to click
  12938. 11:12:57commit and if this popup shows up we're
  12939. 11:13:00going to click
  12940. 11:13:02yes so that will take all of our changes
  12941. 11:13:05and save them into git so now that we've
  12942. 11:13:09saved those changes into git any new
  12943. 11:13:12changes that we add will start showing
  12944. 11:13:14up here so let's do an example and let's
  12945. 11:13:18change some code in a couple different
  12946. 11:13:20files for practice so let's click this
  12947. 11:13:23icon up here to show our code and let's
  12948. 11:13:27go into amazon.
  12949. 11:13:29HTML and we'll just change this title to
  12950. 11:13:33intro to git and
  12951. 11:13:37save and then let's go into checkout.
  12952. 11:13:42HTML and at the top let's actually
  12953. 11:13:45remove the title and save and then let's
  12954. 11:13:49open orders.
  12955. 11:13:51HTML and at the top let's just add
  12956. 11:13:54something to the title like
  12957. 11:13:57page if we
  12958. 11:13:59save and then go into the source control
  12959. 11:14:02section or the git
  12960. 11:14:04section it will tell us all the changes
  12961. 11:14:07that we just made in our code in the
  12962. 11:14:09three different files if we click one of
  12963. 11:14:12these files it will actually tell us
  12964. 11:14:16exactly which code that we
  12965. 11:14:19changed now if your git section looks
  12966. 11:14:21like this where the changes are on top
  12967. 11:14:23of each other instead of side by side if
  12968. 11:14:26you want to you can change this by
  12969. 11:14:28clicking these three dots here making
  12970. 11:14:31sure that inline view is unchecked and
  12971. 11:14:34also unchecking use inline view when
  12972. 11:14:36space is
  12973. 11:14:40limited so now it's very easy to undo
  12974. 11:14:43this code we can just take the code on
  12975. 11:14:45the left and then copy paste it on the
  12976. 11:14:47right to undo this change we can also
  12977. 11:14:51click here to undo all the changes in
  12978. 11:14:54this
  12979. 11:14:56file and up here we can also click this
  12980. 11:15:00button to undo all the changes in all
  12981. 11:15:03the different
  12982. 11:15:05files so that's an intro to Git it helps
  12983. 11:15:09us keep track of changes in our code and
  12984. 11:15:12it's very useful for bigger projects
  12985. 11:15:14like this Amazon Project where we're
  12986. 11:15:16going to be changing lots of code in
  12987. 11:15:19many different files if you want to
  12988. 11:15:21learn about git in more detail you can
  12989. 11:15:24check out my git and GitHub full course
  12990. 11:15:26in the video
  12991. 11:15:29description so now that we've setup git
  12992. 11:15:32let's start the Amazon Project so first
  12993. 11:15:35make sure that this section is empty and
  12994. 11:15:38we've reversed all the changes that we
  12995. 11:15:40made when practicing
  12996. 11:15:42git next let's click the this icon to
  12997. 11:15:45show our
  12998. 11:15:46files and let's remove the previous
  12999. 11:15:51code and we're going to start in the
  13000. 11:15:54amazon. HTML
  13001. 11:15:59file and let's close the git page and
  13002. 11:16:03then open the tab for this file it
  13003. 11:16:06should say amazon. HTML up
  13004. 11:16:09here so we'll start by creating the list
  13005. 11:16:12of products on the homepage so right now
  13006. 11:16:15we start with three products if we go to
  13007. 11:16:18the final project we're going to add the
  13008. 11:16:21rest of the products that you see here
  13009. 11:16:24and we're also going to make this
  13010. 11:16:25interactive so that when we click the
  13011. 11:16:28add to card button it will add this
  13012. 11:16:31product to our
  13013. 11:16:33cart so let's go back to our
  13014. 11:16:36project and in order to create this
  13015. 11:16:39we're going to learn the main idea of
  13016. 11:16:42JavaScript so when using JavaScript
  13017. 11:16:44script we usually do a three-step
  13018. 11:16:47process the first step is to save the
  13019. 11:16:51data and then we generate the
  13020. 11:16:54HTML and finally we make it
  13021. 11:16:58interactive let's go through each step
  13022. 11:17:01to create this list of
  13023. 11:17:03products so right now we have three
  13024. 11:17:06products on our page to start with and
  13025. 11:17:08we're going to add more products to this
  13026. 11:17:11page so let's start by finding the HTML
  13027. 11:17:14for one of these products because we can
  13028. 11:17:17always just copy and paste this to
  13029. 11:17:19create another one now we're going to
  13030. 11:17:21learn a very important skill for bigger
  13031. 11:17:24projects which is how to find the code
  13032. 11:17:27for something on this page so let's say
  13033. 11:17:29that we want to find the code for one of
  13034. 11:17:32these products we're going to
  13035. 11:17:35rightclick and click
  13036. 11:17:38inspect and this will bring up the
  13037. 11:17:40console and I'm actually going to click
  13038. 11:17:43these three dots and click this icon to
  13039. 11:17:46move the console to a separate window so
  13040. 11:17:49that we can see the whole web page so
  13041. 11:17:52now we're going to click this icon in
  13042. 11:17:54the top
  13043. 11:17:55left and we're going to hover over and
  13044. 11:17:58click the thing that we want to get the
  13045. 11:18:00code for so I want to get the code for
  13046. 11:18:03this entire first product so I'll click
  13047. 11:18:06in this
  13048. 11:18:07space and this will tell us that the
  13049. 11:18:10code for the product is this code a div
  13050. 11:18:13with the class product -
  13051. 11:18:15container so now we're going to take
  13052. 11:18:18this class and then search for it in our
  13053. 11:18:21code editor so let's open vs
  13054. 11:18:24code and we're going to type contrl f on
  13055. 11:18:27Windows or command F on Mac to search
  13056. 11:18:31for code in this file so we're going to
  13057. 11:18:34search for the class that we saw earlier
  13058. 11:18:38which is
  13059. 11:18:40product-
  13060. 11:18:43container and this will take us to the
  13061. 11:18:45code for one of these products so we can
  13062. 11:18:49actually click the arrow on the left to
  13063. 11:18:51collapse this code and we'll see another
  13064. 11:18:54product container which corresponds to
  13065. 11:18:57the second product we can click this
  13066. 11:18:59Arrow again and we'll see a third
  13067. 11:19:02product container which corresponds to
  13068. 11:19:04the third
  13069. 11:19:05product so that's how we find the code
  13070. 11:19:08for something on the page now to add
  13071. 11:19:11another product to this page one thing
  13072. 11:19:14we can do is to take this HTML and just
  13073. 11:19:17make a copy of it at the bottom of the
  13074. 11:19:20page however this is not a good idea
  13075. 11:19:24because if we add lots of products we're
  13076. 11:19:26going to be copying this HTML over and
  13077. 11:19:29over and if we expand this you can see
  13078. 11:19:33that each product has a lot of HTML code
  13079. 11:19:36so we don't want to be copying all of
  13080. 11:19:38this over and over again it's going to
  13081. 11:19:41be a lot of code and it's going to be
  13082. 11:19:42hard to manage in instead another
  13083. 11:19:45strategy is to generate this HTML code
  13084. 11:19:49using JavaScript so let's learn how to
  13085. 11:19:52do that before we can do that we need to
  13086. 11:19:56add JavaScript to this page so we're
  13087. 11:19:58going to create a Javascript file so
  13088. 11:20:02let's click up here to show our
  13089. 11:20:04files and first let's create a new
  13090. 11:20:08folder to contain all of our JavaScript
  13091. 11:20:11code so let's name this folder SC
  13092. 11:20:14scripts and press
  13093. 11:20:17enter next let's click on our folder and
  13094. 11:20:21we're going to click this icon to create
  13095. 11:20:23a new file and we'll name this file
  13096. 11:20:27amazon. JS and press enter so this will
  13097. 11:20:32be the Javascript file for this HTML
  13098. 11:20:36file next inside this Javascript file
  13099. 11:20:40let's type the code
  13100. 11:20:43console.
  13101. 11:20:45log
  13102. 11:20:47hello just to make sure that this works
  13103. 11:20:50and
  13104. 11:20:51save next we're going to load this
  13105. 11:20:54Javascript file in our HTML file so
  13106. 11:20:58let's go back to amazon.
  13107. 11:21:01HTML and then at the bottom of the code
  13108. 11:21:04we're going to scroll down and then
  13109. 11:21:07right above the closing tag for the body
  13110. 11:21:10we're going to add a script element
  13111. 11:21:14and we're going to give this element an
  13112. 11:21:16attribute SRC or Source equals double
  13113. 11:21:21quotes and we're going to load this
  13114. 11:21:23Javascript file so this Javascript file
  13115. 11:21:26is in a folder called scripts so here
  13116. 11:21:30we're going to type the folder name
  13117. 11:21:33scripts and then a forward slash to go
  13118. 11:21:36into the folder and we'll load the file
  13119. 11:21:39amazon. JS and
  13120. 11:21:43save so now let let's go back to the
  13121. 11:21:46page and we'll open the console right
  13122. 11:21:49click
  13123. 11:21:51inspect and then click the
  13124. 11:21:53console and it should display hello so
  13125. 11:21:56now we're loading JavaScript on this
  13126. 11:21:59page so let's open up vs code
  13127. 11:22:03again and we're going to generate all of
  13128. 11:22:05this HTML using JavaScript instead so
  13129. 11:22:10the first step in our process is to save
  13130. 11:22:13the data what does this mean data means
  13131. 11:22:17information so in this case the data is
  13132. 11:22:21the information about our products for
  13133. 11:22:24example the products name the price
  13134. 11:22:27ratings and the image so right now the
  13135. 11:22:31information about the products or the
  13136. 11:22:34data is inside our
  13137. 11:22:36HTML so in our code if we click here to
  13138. 11:22:41expand this element you can see that the
  13139. 11:22:44inform
  13140. 11:22:44about the products for example the name
  13141. 11:22:48and the image is inside the
  13142. 11:22:51HTML so what we need to do is to take
  13143. 11:22:54this data and save it inside our
  13144. 11:22:57JavaScript so that our JavaScript can
  13145. 11:23:00use it so let's go to our Javascript
  13146. 11:23:03file and I'm just going to put it down
  13147. 11:23:06here so we can see it side by
  13148. 11:23:09side and we're going to move this data
  13149. 11:23:13into our JavaScript
  13150. 11:23:14so let's remove
  13151. 11:23:16console.log and let's create a variable
  13152. 11:23:19to save the data so we'll type const and
  13153. 11:23:23let's name it
  13154. 11:23:25products and make it
  13155. 11:23:27equal and now we want to create
  13156. 11:23:30something that closely matches our data
  13157. 11:23:34so in this case we have a list of
  13158. 11:23:36products so in here we're going to
  13159. 11:23:39create an array because an array
  13160. 11:23:43represents a list
  13161. 11:23:46and now each product in the list has
  13162. 11:23:49many values like an image a name and a
  13163. 11:23:53price so we're going to use an object to
  13164. 11:23:56represent each product because an object
  13165. 11:24:00lets us group multiple values
  13166. 11:24:04together so inside this array we're
  13167. 11:24:07going to create an object using curly
  13168. 11:24:11brackets and now inside this object
  13169. 11:24:14we're going to save the data for the
  13170. 11:24:16first product so first let's find the
  13171. 11:24:20data in our HTML we're going to search
  13172. 11:24:23for the product's name by typing contrl
  13173. 11:24:26F or command F and we'll search for
  13174. 11:24:30black and
  13175. 11:24:33gray and this will take us to the code
  13176. 11:24:35for the first product so this is the
  13177. 11:24:39name of the first product so let's
  13178. 11:24:41actually scroll up and we're going to
  13179. 11:24:44get get the image for the first product
  13180. 11:24:47and we're going to take this and save it
  13181. 11:24:49in our JavaScript so let's select this
  13182. 11:24:52code we're going to
  13183. 11:24:55rightclick
  13184. 11:24:57copy and then inside this object let's
  13185. 11:25:00create a property called image colon and
  13186. 11:25:05then here right click and
  13187. 11:25:09paste and let's save this as a string so
  13188. 11:25:13at the front we'll type a single quote
  13189. 11:25:16and at the end we'll type a single
  13190. 11:25:19quote so we just saved the image inside
  13191. 11:25:22our
  13192. 11:25:23JavaScript let's move on and save the
  13193. 11:25:26name inside our JavaScript so we'll
  13194. 11:25:29scroll down we'll select this code and
  13195. 11:25:32then
  13196. 11:25:34rightclick
  13197. 11:25:35copy and in this object we'll type comma
  13198. 11:25:40and a property called name col in a
  13199. 11:25:43string
  13200. 11:25:45and we'll right click and
  13201. 11:25:50paste next we're going to save the
  13202. 11:25:53rating into our JavaScript so we'll type
  13203. 11:25:57comma and let's create a property called
  13204. 11:26:00rating
  13205. 11:26:02colon for the rating there's actually
  13206. 11:26:05two parts the stars and the number of
  13207. 11:26:08ratings that it has so let's actually
  13208. 11:26:11group these two values into an object
  13209. 11:26:14because these values are related to each
  13210. 11:26:16other so in here we'll make the rating
  13211. 11:26:20and
  13212. 11:26:22object and each rating will have the
  13213. 11:26:26stars and for the socks it's
  13214. 11:26:304.5 comma and the rating will also have
  13215. 11:26:33a
  13216. 11:26:35count and for the first product it's
  13217. 11:26:3987 next let's save the price into our
  13218. 11:26:43JavaScript
  13219. 11:26:44so we'll create another property called
  13220. 11:26:49price and for the first product the
  13221. 11:26:52price is
  13222. 11:26:55$10.90 however JavaScript has problems
  13223. 11:26:58doing math with decimal numbers or
  13224. 11:27:01floats like this so a best practice when
  13225. 11:27:04calculating money is to calculate in
  13226. 11:27:07cents instead of dollars so for this
  13227. 11:27:10reason we're going to save the price as
  13228. 11:27:13cents so we'll save it as
  13229. 11:27:171,90 as a reminder $1 is equal to 100
  13230. 11:27:22cents and because we're saving in cents
  13231. 11:27:25it's helpful to rename this property to
  13232. 11:27:28price cents
  13233. 11:27:31instead so now if we scroll up we just
  13234. 11:27:34saved all the information or the data
  13235. 11:27:37about the first product into our
  13236. 11:27:41JavaScript next let's go to the end here
  13237. 11:27:45and type comma and create another object
  13238. 11:27:49and we're going to save the data for the
  13239. 11:27:51second product into this object so feel
  13240. 11:27:54free to pause the video if you want to
  13241. 11:27:56try it yourself
  13242. 11:27:58first so again let's find this product
  13243. 11:28:01in our HTML we'll go to our HTML and
  13244. 11:28:05type contrl f or command F to search for
  13245. 11:28:09code and we'll search for the name
  13246. 11:28:12intermediate
  13247. 11:28:15size basketball so now this is the code
  13248. 11:28:18for the second product we're going to
  13249. 11:28:21scroll up a bit and find the image for
  13250. 11:28:24this product and we're going to select
  13251. 11:28:26it rightclick
  13252. 11:28:29copy and then inside the second object
  13253. 11:28:33we'll create an image property just like
  13254. 11:28:36we did up
  13255. 11:28:38here and we'll save it as a string and
  13256. 11:28:42we're going to rightclick and
  13257. 11:28:46paste and let's type comma and we'll
  13258. 11:28:49save the name inside the second object
  13259. 11:28:53so we'll scroll down and select the
  13260. 11:28:57name and then right click
  13261. 11:29:01copy and then down here create a string
  13262. 11:29:06and rightclick and
  13263. 11:29:08paste and we'll type a comma and we'll
  13264. 11:29:12save the rating
  13265. 11:29:15colon
  13266. 11:29:16object and we'll save the number of
  13267. 11:29:19stars which is
  13268. 11:29:21four and the number of ratings that it
  13269. 11:29:24has count colon
  13270. 11:29:28127 and finally we'll type a comma and
  13271. 11:29:32we'll save the price of the product in
  13272. 11:29:36cents colon
  13273. 11:29:41295 so now the data for these second
  13274. 11:29:44product is saved inside our
  13275. 11:29:47JavaScript let's go to the end and type
  13276. 11:29:50comma and create another object and
  13277. 11:29:53we're going to save the data for the
  13278. 11:29:55third product inside again feel free to
  13279. 11:29:58pause the video to try it yourself first
  13280. 11:30:02so let's save the image we'll find the
  13281. 11:30:04code for this product in our HTML so up
  13282. 11:30:08here we'll typ contrl F or command
  13283. 11:30:11F and we'll search for the product name
  13284. 11:30:15which is
  13285. 11:30:17adults plain cotton
  13286. 11:30:20t-shirt and scroll up and we're going to
  13287. 11:30:23save the image first so let's select
  13288. 11:30:25this right click copy and then down here
  13289. 11:30:31create an image property colon and a
  13290. 11:30:34string and then right click and
  13291. 11:30:38paste and we'll create a comma and let's
  13292. 11:30:41save the name in our Javas script next
  13293. 11:30:45create a string and then we'll scroll
  13294. 11:30:48down here we'll select the
  13295. 11:30:51name right
  13296. 11:30:53click
  13297. 11:30:54copy and then in here right click and
  13298. 11:30:59paste and comma and we'll save the
  13299. 11:31:03rating colon and object and we have the
  13300. 11:31:07number of stars for this one it's
  13301. 11:31:114.5 colon and the
  13302. 11:31:15count and for this one it is
  13303. 11:31:2056 and lastly type a comma and we'll
  13304. 11:31:24save the price in
  13305. 11:31:27cents colon
  13306. 11:31:31799 and now let's save our Javascript
  13307. 11:31:35file now one really important thing is
  13308. 11:31:39to make sure that in each of these
  13309. 11:31:41objects we have the same property so we
  13310. 11:31:44have the image property the name
  13311. 11:31:47property and the same rating property
  13312. 11:31:49with the same structure we want all of
  13313. 11:31:52our objects to have the same structure
  13314. 11:31:54so that our code can handle each product
  13315. 11:31:57the
  13316. 11:31:58same so that's what it means to save the
  13317. 11:32:01data we create something in JavaScript
  13318. 11:32:04that closely matches the data that we
  13319. 11:32:07have on the Page by the way this is
  13320. 11:32:10called a data structure because its
  13321. 11:32:13structure structures or organizes the
  13322. 11:32:15data and this represents a list of
  13323. 11:32:18products so we usually use a combination
  13324. 11:32:21of objects and arrays to create a data
  13325. 11:32:24structure in
  13326. 11:32:26JavaScript so now that we've saved the
  13327. 11:32:28data the next step is to use this data
  13328. 11:32:32to generate the HTML instead of writing
  13329. 11:32:35the HTML
  13330. 11:32:38manually to generate the HTML we can
  13331. 11:32:41Loop through this array and for each of
  13332. 11:32:45these products we're going to create
  13333. 11:32:47some
  13334. 11:32:48HTML so let's go ahead and loop through
  13335. 11:32:51this array first we'll scroll down to
  13336. 11:32:55the
  13337. 11:32:56bottom and we'll type the name of the
  13338. 11:32:58array which is products and then we'll
  13339. 11:33:02Loop through this array using a for each
  13340. 11:33:05method so we'll type Dot 4 Capital each
  13341. 11:33:11brackets and inside we create a function
  13342. 11:33:15using brackets arrow and curly
  13343. 11:33:19brackets and we'll give it a parameter
  13344. 11:33:22called
  13345. 11:33:24Product so I'm going to move this back
  13346. 11:33:26to the top here so we can see more of
  13347. 11:33:29the code so the way that for each works
  13348. 11:33:32is that it takes each object saves it in
  13349. 11:33:35this parameter called Product and then
  13350. 11:33:37runs the function so it takes the first
  13351. 11:33:40object saves it in product runs the
  13352. 11:33:43function function takes a second object
  13353. 11:33:45saves a product runs a function and so
  13354. 11:33:48on so for each of these products or each
  13355. 11:33:51of these objects we want to create some
  13356. 11:33:56HTML so inside this function let's
  13357. 11:33:59create a variable const
  13358. 11:34:03HTML equals and we'll create a template
  13359. 11:34:06string using back
  13360. 11:34:09ticks and now inside this template
  13361. 11:34:12string we're basically going going to
  13362. 11:34:14copy all the HTML for one of these
  13363. 11:34:17products so let's go back to our HTML
  13364. 11:34:21file and how do we find the HTML for one
  13365. 11:34:24product so we learned this earlier we
  13366. 11:34:27can
  13367. 11:34:28rightclick
  13368. 11:34:31inspect and then click the top left
  13369. 11:34:34icon and click the element we want to
  13370. 11:34:36get the HTML for so this HTML represents
  13371. 11:34:41one product and we can search for this
  13372. 11:34:43class product- container in our code so
  13373. 11:34:47we'll go back to our vs
  13374. 11:34:50code inside the HTML file we can contrl
  13375. 11:34:54f or command F to search for code and
  13376. 11:34:57we'll search for the class product Das
  13377. 11:35:03container and let's go to the first
  13378. 11:35:05result so we'll go to the first result
  13379. 11:35:08which is here and let's copy all of the
  13380. 11:35:12code inside this ele element one trick
  13381. 11:35:15we can do is to click the arrow on the
  13382. 11:35:17left to collapse it and then we'll
  13383. 11:35:21select all this code and we'll
  13384. 11:35:24rightclick
  13385. 11:35:26copy and then inside our Javascript file
  13386. 11:35:30inside the string we're just going to
  13387. 11:35:33rightclick and paste the
  13388. 11:35:37HTML and if we need to fix the
  13389. 11:35:39formatting we can select all of this
  13390. 11:35:42code and then press tab a few times to
  13391. 11:35:46add some indexs and here we can remove
  13392. 11:35:49the indent and let's
  13393. 11:35:52save so now we're basically looping
  13394. 11:35:56through the array of products and for
  13395. 11:35:59each product we're creating all of the
  13396. 11:36:02HTML that we had before so we're
  13397. 11:36:05generating this HTML using
  13398. 11:36:08JavaScript now let's console.log this
  13399. 11:36:11HTML to make sure that it's working
  13400. 11:36:14we'll scroll down to the
  13401. 11:36:16bottom and here let's
  13402. 11:36:21console.log the HTML that we just
  13403. 11:36:24created if we
  13404. 11:36:26save and then go back to our page right
  13405. 11:36:30click and
  13406. 11:36:32inspect and click the
  13407. 11:36:35console you can see in the console that
  13408. 11:36:38we just generated all this HTML and we
  13409. 11:36:41generated three of these
  13410. 11:36:44let's go back to our
  13411. 11:36:46code and scroll
  13412. 11:36:50up now one problem here is that in the
  13413. 11:36:54HTML that we generate we don't want to
  13414. 11:36:56have the name black and gray socks every
  13415. 11:36:59time for the second product we want to
  13416. 11:37:01have a different name and a different
  13417. 11:37:03image and for the third product we want
  13418. 11:37:06to have a different name and a different
  13419. 11:37:09image so how do we create slightly
  13420. 11:37:12different HTML for each
  13421. 11:37:15product to do that we're going to
  13422. 11:37:17replace this part of the code with
  13423. 11:37:20whatever the image property is so for
  13424. 11:37:24here we're going to delete it and we're
  13425. 11:37:26going to insert the property image into
  13426. 11:37:30this string so to insert values into a
  13427. 11:37:34template string like this we can use
  13428. 11:37:37Dollar open curly bracket Clos curly
  13429. 11:37:40bracket and then inside here we're going
  13430. 11:37:43to insert the
  13431. 11:37:47product. image property so it's going to
  13432. 11:37:50take whatever the image property is of
  13433. 11:37:52the product and put it inside this HTML
  13434. 11:37:56code same thing for the name instead of
  13435. 11:37:59black and gray socks every time we're
  13436. 11:38:02going to delete this and then insert a
  13437. 11:38:05value we're going to insert the
  13438. 11:38:09product Dot and we'll insert the name
  13439. 11:38:12property this time into this HTML so dot
  13440. 11:38:18name and we'll do the same thing for the
  13441. 11:38:20other pieces of data so for each product
  13442. 11:38:23the number of stars could be different
  13443. 11:38:26so instead of 4.5 every time let's
  13444. 11:38:30delete this and we're going to insert
  13445. 11:38:33the products rating and stars so
  13446. 11:38:39product.
  13447. 11:38:41rating. Stars
  13448. 11:38:46one thing we have to watch out here for
  13449. 11:38:49is that this image has to exist so right
  13450. 11:38:52now if we substitute the product.
  13451. 11:38:55ratings. Stars into this we will get
  13452. 11:38:58this image and if we go into our files
  13453. 11:39:03by clicking here and open the images
  13454. 11:39:06folder and then open the ratings
  13455. 11:39:10folder we can see that there's no image
  13456. 11:39:12called rating
  13457. 11:39:14-4.5 instead there's an image called
  13458. 11:39:17rating
  13459. 11:39:18-45 so if the rating is 4.5 we should
  13460. 11:39:22convert it into 45 so that this image
  13461. 11:39:26actually
  13462. 11:39:27exists similarly if the rating is four
  13463. 11:39:31we should convert it into 40 so that
  13464. 11:39:34this image actually exists so in here we
  13465. 11:39:38should actually multiply this by 10 so
  13466. 11:39:43that this points to an actual image that
  13467. 11:39:47exists
  13468. 11:39:48here next let's move
  13469. 11:39:53on and we'll work on replacing the count
  13470. 11:39:57so we'll remove this and dollar curly
  13471. 11:40:01brackets and we'll replace it with the
  13472. 11:40:05products. rating Dot and the count
  13473. 11:40:11property so
  13474. 11:40:14count and finally we're going to replace
  13475. 11:40:17the price for each product so in here
  13476. 11:40:20we're going to remove this and
  13477. 11:40:23substitute
  13478. 11:40:25product. price
  13479. 11:40:28sense and remember that this is a number
  13480. 11:40:31in cents but when we display it on the
  13481. 11:40:34page we want to display it as dollars so
  13482. 11:40:37we're going to divide this by 100
  13483. 11:40:41because there are 100 cents in each
  13484. 11:40:45dollar so now in the HTML that we're
  13485. 11:40:48generating we're actually inserting the
  13486. 11:40:51details about each product if we
  13487. 11:40:55save and then go back to the console
  13488. 11:40:58right click
  13489. 11:40:59inspect and
  13490. 11:41:01console and look at the HTML that we
  13491. 11:41:05generated you'll see that it's different
  13492. 11:41:07for each of the products so that's how
  13493. 11:41:10we use JavaScript to generate all the
  13494. 11:41:13HTML
  13495. 11:41:15here so now the last step is we're going
  13496. 11:41:18to combine all this HTML together into
  13497. 11:41:21one string and put it on the web page
  13498. 11:41:25let's go back into VSS
  13499. 11:41:28code and to combine all this HTML
  13500. 11:41:31together let's create a variable at the
  13501. 11:41:34top for combining all the strings
  13502. 11:41:37together so we'll type let let's call it
  13503. 11:41:41products cap Capital
  13504. 11:41:44HTML and make it equal to an empty
  13505. 11:41:47string to start and then each time we go
  13506. 11:41:51through the loop we're going to add this
  13507. 11:41:53HTML string up here so instead of
  13508. 11:41:57creating a new variable we're just going
  13509. 11:42:00to add products
  13510. 11:42:03HTML equals itself products
  13511. 11:42:08HTML plus this string and remember a
  13512. 11:42:13shortcut for this is to just type plus
  13513. 11:42:18equals so this is called the accumulator
  13514. 11:42:21pattern we Loop through an array and
  13515. 11:42:24each time we're adding to the result so
  13516. 11:42:27we're accumulating the result and now
  13517. 11:42:31that we combine all the HTML into this
  13518. 11:42:34variable we can scroll to the
  13519. 11:42:37bottom and let's remove this line
  13520. 11:42:40because this doesn't exist anymore
  13521. 11:42:43and then after the loop let's
  13522. 11:42:47console.log the products
  13523. 11:42:52HTML and
  13524. 11:42:54save and now if we go into our
  13525. 11:42:58console we combined all the HTML for all
  13526. 11:43:02the products
  13527. 11:43:04together and now the last step is to
  13528. 11:43:07take this HTML and put it on the page
  13529. 11:43:10and we can do that using the Dom
  13530. 11:43:13so let's go back into our
  13531. 11:43:16code and to use the Dom we're first
  13532. 11:43:19going to get an HTML element from the
  13533. 11:43:22page and put it inside our
  13534. 11:43:24JavaScript so first let's go to our HTML
  13535. 11:43:29file and remember that a product-
  13536. 11:43:32container represents one of these
  13537. 11:43:34products here so we're going to look for
  13538. 11:43:37this element here a div products- grid
  13539. 11:43:41so this element contains all of our
  13540. 11:43:45products so we're going to use the Dom
  13541. 11:43:47to take this element put it into our
  13542. 11:43:50JavaScript and replace all the HTML
  13543. 11:43:54inside so to put this element in our
  13544. 11:43:56JavaScript we're going to give it a
  13545. 11:43:59class so let's add a class
  13546. 11:44:03js-
  13547. 11:44:05products- grid and
  13548. 11:44:08save and let's go into our JavaScript so
  13549. 11:44:12I'm just going to move this this down to
  13550. 11:44:13the bottom again so we can see it side
  13551. 11:44:15by
  13552. 11:44:17side and then inside our JavaScript at
  13553. 11:44:21the bottom we're going to use the Dom by
  13554. 11:44:24typing
  13555. 11:44:26document. query selector brackets and a
  13556. 11:44:31string and we're going to type dot to
  13557. 11:44:33select a class and we're going to select
  13558. 11:44:37js-
  13559. 11:44:39products-
  13560. 11:44:41grid so the DOT means we're going to
  13561. 11:44:44look for a class called JS products grid
  13562. 11:44:47which is right here and it's going to
  13563. 11:44:49take this element and put it inside our
  13564. 11:44:53JavaScript and now we can change the
  13565. 11:44:56HTML inside the element to do that we'll
  13566. 11:45:00use the property do inner Capital
  13567. 11:45:05HTML and we'll change it to the products
  13568. 11:45:09HTML that we created Above So for
  13569. 11:45:13products Capital
  13570. 11:45:16HTML and
  13571. 11:45:18save so it's not going to look any
  13572. 11:45:21different but now we're actually
  13573. 11:45:23generating all the HTML with JavaScript
  13574. 11:45:26and then using the Dom to put this HTML
  13575. 11:45:30inside here so now we can actually
  13576. 11:45:33delete all of this because we're
  13577. 11:45:35generating this with
  13578. 11:45:40JavaScript if we save this file
  13579. 11:45:43our products will still be here because
  13580. 11:45:46even though we deleted the HTML we're
  13581. 11:45:48now generating it with
  13582. 11:45:51JavaScript now one problem that I see
  13583. 11:45:53here is that the price shows up with one
  13584. 11:45:56decimal but we actually wanted to show
  13585. 11:45:59up as
  13586. 11:46:0110.90 so let's scroll up to this part in
  13587. 11:46:04the code so we'll scroll up
  13588. 11:46:07here and look for Price
  13589. 11:46:10sense and the reason this happens is
  13590. 11:46:12because price sense is
  13591. 11:46:141,90 so if we divide that by 100 the
  13592. 11:46:18result is actually
  13593. 11:46:2010.9 if we want to force this to show
  13594. 11:46:23two decimals like
  13595. 11:46:2510.90 we actually have a special method
  13596. 11:46:29that we can use with a number so here we
  13597. 11:46:33can add brackets around this to make
  13598. 11:46:35sure we calculate this number first and
  13599. 11:46:38numbers have a special method called dot
  13600. 11:46:412 Capital fixed and brackets so to fixed
  13601. 11:46:47we'll convert a number into a string but
  13602. 11:46:50we can tell it how many decimals we want
  13603. 11:46:53between the brackets if we type two it
  13604. 11:46:56will show two decimals so if we save now
  13605. 11:47:00it shows correctly on the page if you're
  13606. 11:47:03wondering how to find this method you
  13607. 11:47:06can find it by searching in Google or
  13608. 11:47:08using an AI
  13609. 11:47:11tool okay let's let's take a break here
  13610. 11:47:14and take a look at git to see all the
  13611. 11:47:16changes that we made so I'm going to
  13612. 11:47:19expand this here and we're going to
  13613. 11:47:21click the git
  13614. 11:47:23section and it's going to tell us that
  13615. 11:47:25we changed two files if we click the
  13616. 11:47:28HTML
  13617. 11:47:30file git will tell us that we deleted
  13618. 11:47:33all of the HTML code for the products
  13619. 11:47:36which is correct if we open the
  13620. 11:47:39Javascript file it will tell us that we
  13621. 11:47:42added all of this new JavaScript code
  13622. 11:47:45including the data and the loop for
  13623. 11:47:48generating the
  13624. 11:47:50HTML so git helps us track all of the
  13625. 11:47:53changes that we made so now let's
  13626. 11:47:56actually take these changes and save
  13627. 11:47:58them into git so we can track new
  13628. 11:48:01changes at the top let's type a message
  13629. 11:48:05to describe the changes that we made so
  13630. 11:48:07let's type
  13631. 11:48:09generate HTML with
  13632. 11:48:14JavaScript and click
  13633. 11:48:16commit and yes if this shows
  13634. 11:48:25up so now let's talk about the benefit
  13635. 11:48:28of generating the
  13636. 11:48:30HTML so let's close git for now and I'll
  13637. 11:48:34move this to the side
  13638. 11:48:36here so the benefit of generating the
  13639. 11:48:39HTML is that if we want to add another
  13640. 11:48:41product we don't have to copy paste all
  13641. 11:48:44of this HTML again all we need to do is
  13642. 11:48:48to go up to the data and add the data
  13643. 11:48:52for a new product so let's do an example
  13644. 11:48:55and add a fourth product to our list so
  13645. 11:48:59at the end here let's type a comma and
  13646. 11:49:02create another
  13647. 11:49:05object and let's go to the final
  13648. 11:49:09project and we'll scroll down to this
  13649. 11:49:12four fourth product here so we're going
  13650. 11:49:14to take the data about this product and
  13651. 11:49:17save it in our JavaScript so first let's
  13652. 11:49:21create a property image colon string and
  13653. 11:49:26we're going to take this image and put
  13654. 11:49:28it in here to do that we can rightclick
  13655. 11:49:31the image
  13656. 11:49:34inspect and then we're going to copy
  13657. 11:49:37this Source attribute so we can double
  13658. 11:49:39click it and we can select this
  13659. 11:49:43attribute
  13660. 11:49:44rightclick
  13661. 11:49:46copy and then inside our JavaScript
  13662. 11:49:49we're going to right click and
  13663. 11:49:53paste and at the end we'll type a comma
  13664. 11:49:57and create the name
  13665. 11:49:59property and a string and we're going to
  13666. 11:50:03select this
  13667. 11:50:04name rightclick copy and then in here
  13668. 11:50:09right click
  13669. 11:50:11paste copy comma and a rating property
  13670. 11:50:15colon and object we have these Stars
  13671. 11:50:20which is five and the
  13672. 11:50:23count which is
  13673. 11:50:272,197 and at the end we're going to add
  13674. 11:50:31the price in cents colon
  13675. 11:50:391,899 so now if we save this JavaScript
  13676. 11:50:42file and then go back to our
  13677. 11:50:46project and we scroll down you'll notice
  13678. 11:50:49that it automatically added the fourth
  13679. 11:50:52product and all we had to do was to add
  13680. 11:50:55another object to our array and then our
  13681. 11:50:58Loop will handle generating all the HTML
  13682. 11:51:01for us so you can see that generating
  13683. 11:51:04the HTML with JavaScript is a lot easier
  13684. 11:51:08than copy pasting this HTML every time
  13685. 11:51:11and that's why we use this
  13686. 11:51:14technique next we're going to add the
  13687. 11:51:17rest of the products in the final
  13688. 11:51:19project now one thing we could do is to
  13689. 11:51:22manually copy this over one by one but
  13690. 11:51:25to make this easier inside the starting
  13691. 11:51:27code I've actually created a file that
  13692. 11:51:30contains all the data so if we scroll up
  13693. 11:51:35and close this for now and we're going
  13694. 11:51:37to go to this data folder and open it
  13695. 11:51:40and inside this folder I've provided a
  13696. 11:51:43Javascript file called products. JS and
  13697. 11:51:46in here it contains an array with the
  13698. 11:51:49data for all the products in the final
  13699. 11:51:52project so we don't have to create it
  13700. 11:51:55ourselves so now let's use this products
  13701. 11:51:58array in our code so first we need to
  13702. 11:52:02load this file on our page we can go to
  13703. 11:52:05our amazon. HTML file and then scroll
  13704. 11:52:09down to the
  13705. 11:52:11bottom and then at the bottom in the
  13706. 11:52:14script element we're going to add
  13707. 11:52:16another script element above it so we
  13708. 11:52:19type script and then the source is going
  13709. 11:52:23to be equal to this Javascript file so
  13710. 11:52:26we'll type the folder called data and
  13711. 11:52:30then a slash to go into the folder and
  13712. 11:52:33load the file
  13713. 11:52:36products.
  13714. 11:52:37JS so what this does is that it will run
  13715. 11:52:40the files one by one
  13716. 11:52:43first it will run the products. JS file
  13717. 11:52:46and create the array and then it will
  13718. 11:52:48run our amazon. JS file and generate the
  13719. 11:52:53HTML so back here let's
  13720. 11:52:57save and then go back into amazon.
  13721. 11:53:02JS so now instead of using our own
  13722. 11:53:05products array let's just use the
  13723. 11:53:08products array in this
  13724. 11:53:10file so we'll go back to to amazon.
  13725. 11:53:14JS and we're just going to delete this
  13726. 11:53:16array so over here we can click this
  13727. 11:53:20Arrow to collapse the code and I'm going
  13728. 11:53:23to select it and then just delete
  13729. 11:53:27it and
  13730. 11:53:29save so now this variable will be coming
  13731. 11:53:33from this file if we go back to our
  13732. 11:53:37project and scroll down you can see that
  13733. 11:53:40we created all the products in our final
  13734. 11:53:44project so the way this works is that
  13735. 11:53:47inside our amazon.
  13736. 11:53:49HTML we're loading two JavaScript files
  13737. 11:53:53this Javascript file creates all the
  13738. 11:53:55products and then this Javascript file
  13739. 11:53:58Loops through the products and creates
  13740. 11:54:00all the
  13741. 11:54:02HTML so keep in mind that script
  13742. 11:54:05elements are loaded one by one so the
  13743. 11:54:07order matters we have to first create
  13744. 11:54:10the products array and then we can use
  13745. 11:54:13it if these two were reversed then the
  13746. 11:54:15code wouldn't
  13747. 11:54:18work so this is how a real world website
  13748. 11:54:22is created we don't write all the HTML
  13749. 11:54:26manually instead we save the data for
  13750. 11:54:29the website in a data structure like
  13751. 11:54:31this and then we use JavaScript to
  13752. 11:54:35generate the HTML using the data so now
  13753. 11:54:39let's go into get and look at our most
  13754. 11:54:41recent
  13755. 11:54:43changes so here it will tell us that we
  13756. 11:54:45changed two files in the amazon. HTML
  13757. 11:54:49file we added a script element and then
  13758. 11:54:52in the amazon. JS file we deleted the
  13759. 11:54:56products array that we created on our
  13760. 11:54:59own so let's type a message to describe
  13761. 11:55:02our changes use products. JS file and
  13762. 11:55:09then click commit and yes
  13763. 11:55:14now one other thing I want to show you
  13764. 11:55:16about git is if we go back to our files
  13765. 11:55:19here and then open amazon. JS git has a
  13766. 11:55:23really useful feature called a history
  13767. 11:55:26so we can open this timeline section and
  13768. 11:55:29then click this filter icon and only
  13769. 11:55:33check git
  13770. 11:55:35history and now you can see that we
  13771. 11:55:37actually have a history of all of our
  13772. 11:55:40changes for this file so if we click the
  13773. 11:55:43first line in the history it will show
  13774. 11:55:46us that here we added all of the code
  13775. 11:55:49and then the second line in the history
  13776. 11:55:52we deleted this part of the code so this
  13777. 11:55:55gives us a step-by-step history of all
  13778. 11:55:58the changes in our code and in git this
  13779. 11:56:02is called a version history you can also
  13780. 11:56:05see that the messages that we created
  13781. 11:56:08when we commit show up here and these
  13782. 11:56:11messages help us us understand what was
  13783. 11:56:13changed in each version in the
  13784. 11:56:16history all right so let's close this
  13785. 11:56:19for
  13786. 11:56:21now and the last step in the process is
  13787. 11:56:24to make it
  13788. 11:56:26interactive so let's take a look at our
  13789. 11:56:29current
  13790. 11:56:30project so we'll go here and open this
  13791. 11:56:35tab and let's scroll
  13792. 11:56:38up so right now if we click the add to
  13793. 11:56:41cart button Buton it doesn't do anything
  13794. 11:56:45so we're going to add some JavaScript to
  13795. 11:56:46make this interactive and actually add
  13796. 11:56:49this product to a cart so first let's
  13797. 11:56:53find the add to cart button in our code
  13798. 11:56:56we'll go back to our amazon. JS file
  13799. 11:57:01close this for
  13800. 11:57:02now and to find the code we can actually
  13801. 11:57:05just search the text in the button so
  13802. 11:57:08inside here let's typ contrl F or
  13803. 11:57:12command F and we're going to search for
  13804. 11:57:14the text add to
  13805. 11:57:18cart and this will take us to the code
  13806. 11:57:21for this button so now we're going to
  13807. 11:57:24add an event listener to this button and
  13808. 11:57:26make it do something when we click it to
  13809. 11:57:29add an event listener we're first going
  13810. 11:57:32to add a
  13811. 11:57:33class
  13812. 11:57:35js-
  13813. 11:57:37add-2
  13814. 11:57:39dcart and then we're going to use the
  13815. 11:57:41dawn to add an event listener to this
  13816. 11:57:45button so down
  13817. 11:57:48here after we've put this HTML on the
  13818. 11:57:51page we're going to use
  13819. 11:57:55document. Query
  13820. 11:57:58selector all brackets and then a string
  13821. 11:58:03a DOT and we're going to select all the
  13822. 11:58:06js- add to cart so
  13823. 11:58:10js- add-2 to- cart so this will give us
  13824. 11:58:15a list of all the add to cart buttons on
  13825. 11:58:18the page and then we can Loop through
  13826. 11:58:21each of the buttons so we can type dot
  13827. 11:58:25for each
  13828. 11:58:28brackets and then in here we'll give it
  13829. 11:58:30a function bracket arrow and curly
  13830. 11:58:34brackets and a parameter so the
  13831. 11:58:38parameter is going to be the button
  13832. 11:58:40element that we got from the page so now
  13833. 11:58:44for each of these buttons we're going to
  13834. 11:58:46add an event listener so we can do
  13835. 11:58:48something when we click the button to do
  13836. 11:58:51that we'll type
  13837. 11:58:54button. add event
  13838. 11:58:58listener brackets and then the event
  13839. 11:59:01that we want to listen for which is the
  13840. 11:59:04click
  13841. 11:59:05event and then a function that we want
  13842. 11:59:08to run when we click this button so
  13843. 11:59:11brackets arrow and curly
  13844. 11:59:14brackets so for now let's just
  13845. 11:59:16console.log a message to make sure that
  13846. 11:59:19this is working so we'll
  13847. 11:59:23console.log added
  13848. 11:59:26product and
  13849. 11:59:29save and we can also remove this line
  13850. 11:59:32for now because we're not using it and
  13851. 11:59:35save again and then now on our page
  13852. 11:59:38we're going to click add it to
  13853. 11:59:40cart and then look inside our console so
  13854. 11:59:44rightclick
  13855. 11:59:45inspect and click the
  13856. 11:59:48console and it displays added product so
  13857. 11:59:52now whenever we click one of these
  13858. 11:59:53buttons it will display this message so
  13859. 11:59:57that means that our code here is working
  13860. 12:00:01correctly next we need to figure out how
  13861. 12:00:04to add the product to a cart and we need
  13862. 12:00:07to figure out what this looks like if we
  13863. 12:00:10think about a cart a cart is basically
  13864. 12:00:13just a list and inside this list we have
  13865. 12:00:16the product that we want to buy and the
  13866. 12:00:19quantity or the number of this product
  13867. 12:00:21that we want to buy so we can actually
  13868. 12:00:24represent this in JavaScript as an array
  13869. 12:00:28inside the array we can have some
  13870. 12:00:30objects and each object is going to
  13871. 12:00:32contain the product and a
  13872. 12:00:35quantity so we can use something like
  13873. 12:00:37this to represent our cart so let's go
  13874. 12:00:41ahead and create create this cart array
  13875. 12:00:44now to keep things cleaner let's
  13876. 12:00:46actually separate the cart into its own
  13877. 12:00:49file so we'll click up here to show our
  13878. 12:00:52files and then we'll go into the data
  13879. 12:00:55folder and click this folder and click
  13880. 12:00:58this icon to create a new
  13881. 12:01:01file and we'll name this file cart. JS
  13882. 12:01:06and press
  13883. 12:01:08enter so this file will contain all the
  13884. 12:01:11code related to the cart and the reason
  13885. 12:01:14we separate it is to keep our code
  13886. 12:01:16organized each file will focus on one
  13887. 12:01:19particular thing so inside
  13888. 12:01:22C.J let's create our cart so we can just
  13889. 12:01:27create a variable const cart and make it
  13890. 12:01:31equal to an array so we'll start off
  13891. 12:01:35with an MD cart and let's save this
  13892. 12:01:39file and now we need to load this file
  13893. 12:01:42into our
  13894. 12:01:43HTML so we can open amazon.
  13895. 12:01:47HTML and again we can just add another
  13896. 12:01:50script element so let's add it at the
  13897. 12:01:53top to make sure we load the cart before
  13898. 12:01:56running the rest of our code so we'll
  13899. 12:01:59type less than
  13900. 12:02:01script and the attribute
  13901. 12:02:04source and we'll load the data folder
  13902. 12:02:07slash to go inside cart. Js
  13903. 12:02:12and
  13904. 12:02:13save so now it will run this file at the
  13905. 12:02:17start and it will create an array
  13906. 12:02:20representing our cart so now that we
  13907. 12:02:23have a cart we need to figure out what
  13908. 12:02:25happens when we click a button how do we
  13909. 12:02:28add a product to this cart so let's go
  13910. 12:02:32back into amazon. JS and we'll figure
  13911. 12:02:36this out together so down here this is
  13912. 12:02:40the function that is run when we click
  13913. 12:02:42the button so in here we have to figure
  13914. 12:02:45out how to add this product to the
  13915. 12:02:48cart one way to do this is to just add
  13916. 12:02:51an object to the cart like this we have
  13917. 12:02:54the product's name and the quantity that
  13918. 12:02:57we want however when we click this
  13919. 12:03:00button how do we know which product
  13920. 12:03:03we're supposed to add to the cart to
  13921. 12:03:05solve this problem we're going to learn
  13922. 12:03:08a feature of HTML called a data
  13923. 12:03:11attribute
  13924. 12:03:12a data attribute is just another HTML
  13925. 12:03:16attribute except the purpose of a data
  13926. 12:03:19attribute is that it allows us to attach
  13927. 12:03:22any information to an HTML element so
  13928. 12:03:26let's do an example and create a data
  13929. 12:03:30attribute we're going to scroll up to
  13930. 12:03:33the add to card button here and then at
  13931. 12:03:36the end of the opening tag after the
  13932. 12:03:39class we'll type enter
  13933. 12:03:42and then we're going to type data Dash
  13934. 12:03:46and then we can type any name that we
  13935. 12:03:48want like
  13936. 12:03:51product-name and we'll make it equal to
  13937. 12:03:55double
  13938. 12:03:56quotes and then inside the double quotes
  13939. 12:03:59we're going to put the product's name
  13940. 12:04:01like a basketball or a t-shirt so to do
  13941. 12:04:05that we're going to insert using dollar
  13942. 12:04:08and curly
  13943. 12:04:09brackets product
  13944. 12:04:12AME and
  13945. 12:04:15save so now let's inspect the HTML on
  13946. 12:04:19the page we're going to rightclick on
  13947. 12:04:22this add to card button and
  13948. 12:04:25inspect and then in the elements tab it
  13949. 12:04:28will show us the HTML for the button and
  13950. 12:04:31now you can see that each of these
  13951. 12:04:33buttons now has a data attribute called
  13952. 12:04:36data- product-name and it's equal to the
  13953. 12:04:40product name
  13954. 12:04:43so before we continue let's learn the
  13955. 12:04:45syntax rules for the data
  13956. 12:04:48attribute a data attribute is just an
  13957. 12:04:51HTML attribute that means it follows the
  13958. 12:04:55same syntax rules we have an attribute
  13959. 12:04:58name on the left and an attribute value
  13960. 12:05:01on the right the one difference is that
  13961. 12:05:04data attributes have to start with data
  13962. 12:05:08Dash and then we can give it any name
  13963. 12:05:10that we want like product- name but we
  13964. 12:05:14have to make sure to separate the words
  13965. 12:05:16with a dash this is also known as Kabob
  13966. 12:05:21case so the purpose of a data attribute
  13967. 12:05:24is that we can attach any information to
  13968. 12:05:27an element so here we attached the
  13969. 12:05:30product name to this button but we can
  13970. 12:05:32also attach the product's image or the
  13971. 12:05:35price and so on so now that we attached
  13972. 12:05:39the product's name to this button when
  13973. 12:05:41we click this button we can actually get
  13974. 12:05:44the name and this will tell us which
  13975. 12:05:47product we should add to the
  13976. 12:05:49cart so let's see what this looks like
  13977. 12:05:51in our code so we'll go to our
  13978. 12:05:55code and then when we click the button
  13979. 12:05:58we're first going to get the product
  13980. 12:06:01name from the button to do that we're
  13981. 12:06:04going to type button and then the
  13982. 12:06:07property dot data set
  13983. 12:06:12so the data set property basically gives
  13984. 12:06:15us all the data attributes that are
  13985. 12:06:18attached to this button so now we're
  13986. 12:06:20able to get the product name so let's
  13987. 12:06:23console.log this to see what it looks
  13988. 12:06:26like
  13989. 12:06:28console.log Open Bracket close bracket
  13990. 12:06:32and
  13991. 12:06:33save and then in here let's click the
  13992. 12:06:37basketballs add to cart
  13993. 12:06:39button and we'll go into our console
  13994. 12:06:42right click
  13995. 12:06:45inspect and
  13996. 12:06:47console and I'm just going to move this
  13997. 12:06:50to the left so we can see it side by
  13998. 12:06:52side with our
  13999. 12:06:54code so this is what the data set
  14000. 12:06:56property looks like it gives us all the
  14001. 12:06:59data attributes that are attached to the
  14002. 12:07:02element so in our case we have the
  14003. 12:07:04product name now this works just like an
  14004. 12:07:08object so to access the product name we
  14005. 12:07:11just have to type dot product capital N
  14006. 12:07:17name so notice that the name gets
  14007. 12:07:20converted from Kebab case to camo case
  14008. 12:07:24so now if we
  14009. 12:07:26save and let's go back to our
  14010. 12:07:29website and click the add to cardart
  14011. 12:07:32button it will get that data attribute
  14012. 12:07:35from the button and display it in the
  14013. 12:07:38console if we click the socks at to cart
  14014. 12:07:42button this will tell us which product
  14015. 12:07:45we should add to the cart so now that we
  14016. 12:07:48have the product name let's actually put
  14017. 12:07:50this into our cart so let's go into our
  14018. 12:07:55code and here let's save the product
  14019. 12:07:58name in a variable called const
  14020. 12:08:03Product name
  14021. 12:08:06equals and then here we're going to add
  14022. 12:08:09this to the cart
  14023. 12:08:12so we already created a cart variable
  14024. 12:08:14here so we're just going to type the
  14025. 12:08:17variable cart and we're going to dot
  14026. 12:08:20push into the array and let's push an
  14027. 12:08:24object because we said we wanted a
  14028. 12:08:26product and the
  14029. 12:08:28quantity so we'll add an object
  14030. 12:08:31containing the product name which is
  14031. 12:08:35this variable product
  14032. 12:08:39name and a quantity
  14033. 12:08:42of
  14034. 12:08:43one and that's pretty much it we first
  14035. 12:08:47attached the product name to the button
  14036. 12:08:49using a data attribute and then when we
  14037. 12:08:52click the button we got the product name
  14038. 12:08:56out and then we add the product to the
  14039. 12:08:59cart so at the bottom let's
  14040. 12:09:03console.log thee cart to see what it
  14041. 12:09:06looks like if we
  14042. 12:09:08save and then go here and open the
  14043. 12:09:11console right click inspect
  14044. 12:09:17console and let's add a basketball to
  14045. 12:09:20our cart so we'll click here and then in
  14046. 12:09:23our cart we now have a basketball with a
  14047. 12:09:27quantity of one so now let's add another
  14048. 12:09:30product to our cart we'll go to the
  14049. 12:09:33t-shirt and click add to
  14050. 12:09:36cart and now we have two products in our
  14051. 12:09:39cart one b basketball and one
  14052. 12:09:44t-shirt so now we're successfully adding
  14053. 12:09:46products to a cart next let's try adding
  14054. 12:09:50one t-shirt again if we click add to
  14055. 12:09:54cart and unfortunately this doesn't look
  14056. 12:09:58quite right the T-shirt product is
  14057. 12:10:01repeated twice and each of them has a
  14058. 12:10:04quantity of one what we actually want to
  14059. 12:10:07do is we want one t-shirt product with a
  14060. 12:10:10quantity of two
  14061. 12:10:12because this is how we're going to
  14062. 12:10:13display it on the cart page so let's
  14063. 12:10:16figure out how to achieve this and we'll
  14064. 12:10:19do this step by step first we'll check
  14065. 12:10:23if the product is already in the cart if
  14066. 12:10:26it is in the cart we'll just increase
  14067. 12:10:28the quantity by one if it's not in the
  14068. 12:10:31cart We'll add it to the cart so let's
  14069. 12:10:35convert these steps into code so the
  14070. 12:10:38first step is to check if a product is
  14071. 12:10:41already in the cart so how do we do that
  14072. 12:10:45let's go back to our
  14073. 12:10:47code and one way to check this is we can
  14074. 12:10:50Loop through our cart and look for a
  14075. 12:10:53particular product name so up here let's
  14076. 12:10:58create some new lines and let's check if
  14077. 12:11:01this product name is already in the cart
  14078. 12:11:05array so we'll type cart and we'll Loop
  14079. 12:11:09through the cart using dot for each
  14080. 12:11:14brackets and then inside we'll give it a
  14081. 12:11:17function brackets arrow and curly
  14082. 12:11:20brackets and we'll give this a parameter
  14083. 12:11:24and let's just call it an item so an
  14084. 12:11:27item will contain a product name and a
  14085. 12:11:31quantity so we want to see if this
  14086. 12:11:33product name is already in the
  14087. 12:11:36card so we're going to check if the
  14088. 12:11:40product
  14089. 12:11:42name over here is equal to this item's
  14090. 12:11:46product name so we'll type equals the
  14091. 12:11:51item.
  14092. 12:11:53product name and curly
  14093. 12:11:56brackets so if the product names match
  14094. 12:12:00that means this product is already in
  14095. 12:12:02the
  14096. 12:12:03cart so let's save this matching item in
  14097. 12:12:06a variable so we can use it later so
  14098. 12:12:10we're going to create the variable
  14099. 12:12:11outside the for each Loop and outside of
  14100. 12:12:15its scope so we'll type let matching
  14101. 12:12:20capital I item and we'll make it
  14102. 12:12:23undefined to start and if we find a
  14103. 12:12:26matching item we're going to save it in
  14104. 12:12:30this variable so
  14105. 12:12:33matching item equals
  14106. 12:12:37item so this is how we figure out if a
  14107. 12:12:40product is already already in the cart
  14108. 12:12:43now the next step is If the product is
  14109. 12:12:46in the cart we'll just increase its
  14110. 12:12:48quantity by one so we'll type
  14111. 12:12:53if
  14112. 12:12:54matching item and curly brackets so we
  14113. 12:12:59can just type matching item here because
  14114. 12:13:02if we did find a matching item it will
  14115. 12:13:04be an object which is a truthy value so
  14116. 12:13:09in here we're going to increase the
  14117. 12:13:11matching items quantity by one using
  14118. 12:13:15matching
  14119. 12:13:17item. quantity and we'll plus equal
  14120. 12:13:221 and finally If the product is not in
  14121. 12:13:26the cart We'll add it to the cart which
  14122. 12:13:29is basically what we do here so at the
  14123. 12:13:32end we can just type else if we did not
  14124. 12:13:36find a matching item curly brackets and
  14125. 12:13:41move this code so we'll select it right
  14126. 12:13:45click
  14127. 12:13:46cut and then in here rightclick and
  14128. 12:13:52paste and remove the new lines and
  14129. 12:13:57save so now let's go into our project
  14130. 12:14:00we'll
  14131. 12:14:01rightclick inspect the
  14132. 12:14:04console and let's click the add to card
  14133. 12:14:07button for the basketball so we'll click
  14134. 12:14:10this
  14135. 12:14:11and then in our cart we have a
  14136. 12:14:14basketball with a quantity of
  14137. 12:14:17one let's add a
  14138. 12:14:19T-shirt and now we have one T-shirt with
  14139. 12:14:23a quantity of one and if we click add to
  14140. 12:14:26cart on the T-shirt
  14141. 12:14:28again now we have a t-shirt with a
  14142. 12:14:31quantity of two so that's how we solve
  14143. 12:14:34the problem of combining the quantities
  14144. 12:14:37together now there's one last problem
  14145. 12:14:40that we have to solve here
  14146. 12:14:41which is it's not good to use the
  14147. 12:14:43product name to identify the product in
  14148. 12:14:46the cart because in an e-commerce
  14149. 12:14:49website we might actually have two
  14150. 12:14:51products that have the same name but
  14151. 12:14:54from different brands so to fix this we
  14152. 12:14:57should give each product an ID and this
  14153. 12:15:01ID should be unique so no two products
  14154. 12:15:04will have the same ID so let's use an ID
  14155. 12:15:08to identify these products instead of
  14156. 12:15:11using the name we'll go back to our
  14157. 12:15:14code and if we open our files here and
  14158. 12:15:19go to data products.
  14159. 12:15:21JS you'll notice that I've actually
  14160. 12:15:24added an ID for each of these products
  14161. 12:15:27and this ID is unique so I added it here
  14162. 12:15:31to save us a bit of work so we don't
  14163. 12:15:33have to create the IDS
  14164. 12:15:35ourselves so now let's just use this ID
  14165. 12:15:38in our code so we'll go back to amazon.
  14166. 12:15:44JS and then in here instead of using the
  14167. 12:15:47product name we're going to use the
  14168. 12:15:50products ID to identify it in the cart
  14169. 12:15:54but first we need to attach the ID to
  14170. 12:15:57the button so we'll scroll up and
  14171. 12:16:00instead of a data- product name
  14172. 12:16:03attribute we're going to use a data
  14173. 12:16:07product-id attribute and we're going to
  14174. 12:16:10save the
  14175. 12:16:11products ID
  14176. 12:16:14instead and now down here we're going to
  14177. 12:16:17grab the ID from the button to do that
  14178. 12:16:21we'll change this to product ID so
  14179. 12:16:26remember this gets converted from Kebab
  14180. 12:16:29case to cam case and we'll rename this
  14181. 12:16:32variable product
  14182. 12:16:36ID and then here we'll compare product
  14183. 12:16:41ID is equal to the items product
  14184. 12:16:46ID and lastly when we add to the cart
  14185. 12:16:50we're just going to add the product ID
  14186. 12:16:53to the
  14187. 12:16:57cart and
  14188. 12:17:00save so now if we go to our website and
  14189. 12:17:03click add to cart and check our
  14190. 12:17:09console and expand this you can see that
  14191. 12:17:13we're saving the product ID so that two
  14192. 12:17:16products with the same name will be able
  14193. 12:17:18to be added to the
  14194. 12:17:21cart okay so that's how we make the add
  14195. 12:17:24to cart button
  14196. 12:17:26interactive and now we finished all
  14197. 12:17:28three steps first we save the data for
  14198. 12:17:32the website in our JavaScript and then
  14199. 12:17:35we use this data to generate the
  14200. 12:17:38HTML and finally we made it
  14201. 12:17:41interactive and this is the main idea of
  14202. 12:17:45JavaScript this is generally how we use
  14203. 12:17:48JavaScript on a real
  14204. 12:17:51website now let's go back to git so
  14205. 12:17:53we'll open our code editor and open the
  14206. 12:17:57git section and review the changes that
  14207. 12:18:00we made so we'll expand it and then in
  14208. 12:18:03the HTML file we just added the cart. JS
  14209. 12:18:08file and then in here we just created
  14210. 12:18:11the cart array and in our amazon. JS
  14211. 12:18:15file we basically added a data attribute
  14212. 12:18:19to the button and then down here we made
  14213. 12:18:22the button interactive by adding an
  14214. 12:18:25event
  14215. 12:18:27listener so now let's save these changes
  14216. 12:18:30into git we'll type a message to
  14217. 12:18:33describe our
  14218. 12:18:34changes make add to cart button
  14219. 12:18:39Interactive
  14220. 12:18:41and click
  14221. 12:18:46commit all right so let's close this for
  14222. 12:18:49now and the last feature we're going to
  14223. 12:18:52create in this lesson is we're going to
  14224. 12:18:55make this cart quantity in the top right
  14225. 12:18:58interactive so we'll do this step by
  14226. 12:19:01step first we need to calculate the
  14227. 12:19:04quantity or the total number of products
  14228. 12:19:07in our cart and then we're going to put
  14229. 12:19:10that quantity
  14230. 12:19:11on the page here so how do we calculate
  14231. 12:19:14the total quantity of the cart so
  14232. 12:19:17remember that the cart is an array of
  14233. 12:19:20objects and each object has a quantity
  14234. 12:19:23so to calculate the total quantity we
  14235. 12:19:25can Loop through this array and add the
  14236. 12:19:28quantities together let's go into our
  14237. 12:19:32code and we'll open amazon. JS and let's
  14238. 12:19:36scroll to the
  14239. 12:19:38bottom so here after we update our cart
  14240. 12:19:42we're going to calculate the total
  14241. 12:19:44quantity so let's create some new lines
  14242. 12:19:47and as we saw earlier we can Loop
  14243. 12:19:50through the cart array so we'll type
  14244. 12:19:53cart dot for each brackets and a
  14245. 12:19:59function arrow and curly brackets and
  14246. 12:20:03we'll give this a parameter
  14247. 12:20:05item so this is going to Loop through
  14248. 12:20:08each object in the cart
  14249. 12:20:11next we need a variable to store the
  14250. 12:20:13total quantity so let's create a
  14251. 12:20:16variable up here we'll type let and name
  14252. 12:20:20it cart
  14253. 12:20:22quantity and make it equal to zero to
  14254. 12:20:26start and then as we Loop through this
  14255. 12:20:28array we're going to add the items
  14256. 12:20:31quantity to this variable up here so
  14257. 12:20:35here we can type cart quantity plus
  14258. 12:20:40equals the
  14259. 12:20:42items.
  14260. 12:20:44quantity so this will add up all the
  14261. 12:20:46quantities and save it in this
  14262. 12:20:49variable so at the bottom let's
  14263. 12:20:53console.log the cart quantity to make
  14264. 12:20:56sure that this is working so we'll
  14265. 12:20:59save and go back to our page and open
  14266. 12:21:03the console right click inspect and
  14267. 12:21:08console when we click add to cart on the
  14268. 12:21:11basketball the quantity will be one if
  14269. 12:21:15we click add to cart on the T-shirt the
  14270. 12:21:18quantity will be two if we click again
  14271. 12:21:22the quantity will be three so looking at
  14272. 12:21:25the cart it basically added one
  14273. 12:21:28basketball and two T-shirts to get a
  14274. 12:21:31total of three so that's how we can
  14275. 12:21:34calculate the total quantity of the cart
  14276. 12:21:37so now that we have the total quantity
  14277. 12:21:40we're going going to put it on the page
  14278. 12:21:42here and we can do this using the Dom so
  14279. 12:21:46let's go back to our
  14280. 12:21:49code and to use the Dom we first need an
  14281. 12:21:52HTML element where we're going to put
  14282. 12:21:55this quantity so let's find the HTML
  14283. 12:21:58element for this number here we can go
  14284. 12:22:01back to our page we're going to
  14285. 12:22:03rightclick this number and then
  14286. 12:22:08inspect and then we can click this icon
  14287. 12:22:11in the top
  14288. 12:22:12left hover over this number and click
  14289. 12:22:16it so this will tell us that the code
  14290. 12:22:19for the number is this code right here a
  14291. 12:22:22div with a class cart quantity so now
  14292. 12:22:25we're going to search for this class
  14293. 12:22:27cart quantity in our code so let's go
  14294. 12:22:30back to our
  14295. 12:22:32code and let's go to the amazon. HTML
  14296. 12:22:36file because the header is still in this
  14297. 12:22:39file and we'll type contrl f or command
  14298. 12:22:43F to search for code and we'll search
  14299. 12:22:46for cart Das
  14300. 12:22:49quantity and it will tell us that it's
  14301. 12:22:51this line right here so next to use the
  14302. 12:22:54Dom we need to get this element into our
  14303. 12:22:58JavaScript to do that we're going to add
  14304. 12:23:01a class to this element so let's add the
  14305. 12:23:04class
  14306. 12:23:05js- cart D quantity and say
  14307. 12:23:11save now let's go back to our JavaScript
  14308. 12:23:14file and I'm just going to move it down
  14309. 12:23:16here so we can see it side by
  14310. 12:23:22side and we'll scroll down after we
  14311. 12:23:26calculate the quantity let's get this
  14312. 12:23:28element into our JavaScript using the
  14313. 12:23:31Dom so at the bottom we'll type
  14314. 12:23:35document. query
  14315. 12:23:38selector brackets and a string we start
  14316. 12:23:41with a DOT and the class
  14317. 12:23:44js- cart D
  14318. 12:23:48quantity and now that we have this
  14319. 12:23:50element we can change the HTML inside
  14320. 12:23:54using the property do inner Capital
  14321. 12:23:59HTML and we'll make it equal to the cart
  14322. 12:24:04quantity that we calculated above so
  14323. 12:24:08let's
  14324. 12:24:09save and go back to our website and
  14325. 12:24:12let's click the add to card button for
  14326. 12:24:14the
  14327. 12:24:15basketball now it will update the
  14328. 12:24:17quantity to be one let's click the add
  14329. 12:24:20to cart for the T-shirt it will update
  14330. 12:24:23the quantity again and it will be two if
  14331. 12:24:26we click it again it will increase to
  14332. 12:24:29three so now the cart quantity up here
  14333. 12:24:32is
  14334. 12:24:34interactive the last thing we need to do
  14335. 12:24:37is we'll go back to our
  14336. 12:24:38HTML and in instead of starting at three
  14337. 12:24:42the card quantity should start at zero
  14338. 12:24:45so let's
  14339. 12:24:47save and now the cart quantity starts at
  14340. 12:24:50zero if we click add to cart it will
  14341. 12:24:53increase to one so that's how we create
  14342. 12:24:56this cart quantity feature in the final
  14343. 12:24:59project so now we're finished all the
  14344. 12:25:02features that we're going to do in this
  14345. 12:25:04lesson let's go back to our JavaScript
  14346. 12:25:07and I'm just going to move it back up
  14347. 12:25:09here
  14348. 12:25:10and let's clean up this code by removing
  14349. 12:25:13these console.
  14350. 12:25:15logs and
  14351. 12:25:17save and finally let's check git one
  14352. 12:25:20more time to see the changes that we
  14353. 12:25:22made so we can expand this and go to the
  14354. 12:25:26git
  14355. 12:25:28section if we click amazon. HTML it will
  14356. 12:25:32tell us we added this class and if we
  14357. 12:25:35click amazon. JS it will show us the
  14358. 12:25:38code that we added for C calculating the
  14359. 12:25:40card quantity and putting it on the
  14360. 12:25:44page so finally let's type a message to
  14361. 12:25:47describe our
  14362. 12:25:49changes make the cart quantity
  14363. 12:25:56interactive and
  14364. 12:26:00commit so now we can close
  14365. 12:26:03this and that's the end of this lesson
  14366. 12:26:06in this lesson we started the final
  14367. 12:26:09Amazon Project
  14368. 12:26:10we set up and learned how to use git to
  14369. 12:26:13track changes we learned the main idea
  14370. 12:26:17of JavaScript which is save the data
  14371. 12:26:20generate the HTML and make it
  14372. 12:26:23interactive we created a list of
  14373. 12:26:26products on the homepage we made the add
  14374. 12:26:29to cart button interactive and we made
  14375. 12:26:32the cart quantity
  14376. 12:26:37interactive thanks for watching this
  14377. 12:26:39course so so far if you want to support
  14378. 12:26:42this channel you can check out the
  14379. 12:26:43premium version of my courses just like
  14380. 12:26:46this one where you can get a certificate
  14381. 12:26:48of completion and a Better Learning
  14382. 12:26:50platform if you're interested in extra
  14383. 12:26:53videos from me more in-depth videos and
  14384. 12:26:56behind the scenes content you can check
  14385. 12:26:58out these links here you can also
  14386. 12:27:01support the channel by liking the video
  14387. 12:27:04subscribing and sharing this video with
  14388. 12:27:06others thanks again and let's continue
  14389. 12:27:09with the course
  14390. 12:27:11here are some exercises to help you
  14391. 12:27:13practice what we learned in this lesson
  14392. 12:28:04in this lesson we're going to learn a
  14393. 12:28:05feature of JavaScript called modules
  14394. 12:28:08which is a better way to organize our
  14395. 12:28:11code so first let's take a look at a
  14396. 12:28:14problem that we have in our current code
  14397. 12:28:17we're going to go to our files here and
  14398. 12:28:19we're going to open amazon.
  14399. 12:28:22HTML and then we're going to scroll to
  14400. 12:28:24the bottom of this file to our script
  14401. 12:28:28tags so right now we're using three
  14402. 12:28:31script tags to run all of our
  14403. 12:28:33JavaScript so this runs all the code in
  14404. 12:28:37cart. JS and then it runs all the code
  14405. 12:28:40in products. JS and finally it runs all
  14406. 12:28:43the code in amazon. JS so this is
  14407. 12:28:47basically the same thing as combining
  14408. 12:28:49all the files together into one big file
  14409. 12:28:52and then running all of our code however
  14410. 12:28:56there's a big problem with using script
  14411. 12:28:58tags like this and that problem is it
  14412. 12:29:01can cause naming conflicts so in our
  14413. 12:29:04situation here we created a variable
  14414. 12:29:07named cart in cart. Js
  14415. 12:29:10this means we can't use the variable
  14416. 12:29:12name cart in any of our other files so
  14417. 12:29:16for example if we went to amazon.
  14418. 12:29:19JS and then we scroll to the top
  14419. 12:29:22here and let's say that we try to create
  14420. 12:29:25another variable named cart so at the
  14421. 12:29:28top we type const cart equals square
  14422. 12:29:32brackets and
  14423. 12:29:34save and now if we go back to our
  14424. 12:29:37project you'll notice that the website
  14425. 12:29:40stops working and if we rightclick
  14426. 12:29:44inspect and open the
  14427. 12:29:46console it will tell us that we got an
  14428. 12:29:49error and it says that cart has already
  14429. 12:29:51been declared so this is happening
  14430. 12:29:54because we created a variable called
  14431. 12:29:56cart in cart. JS and then later in
  14432. 12:29:59amazon. JS we created another variable
  14433. 12:30:02named cart now we can't have two
  14434. 12:30:05variables with the same name in
  14435. 12:30:06JavaScript and this causes an error and
  14436. 12:30:09it causes our website to break so this
  14437. 12:30:12is called a naming conflict when we have
  14438. 12:30:14two variables with the same
  14439. 12:30:17name so the big problem with script tags
  14440. 12:30:20is if we go back to our
  14441. 12:30:25code you can see here that it's really
  14442. 12:30:27hard to tell which variables have been
  14443. 12:30:30created in other files and it's really
  14444. 12:30:32easy to accidentally create a naming
  14445. 12:30:35conflict like this so that's the problem
  14446. 12:30:38with using script tags like this and
  14447. 12:30:40running our code one file at a time it's
  14448. 12:30:44very easy to get naming
  14449. 12:30:46conflicts so now we're going to learn
  14450. 12:30:48how to solve this problem using a
  14451. 12:30:50feature of JavaScript called
  14452. 12:30:53modules so a module basically contains a
  14453. 12:30:56variable inside a file so now this
  14454. 12:30:59variable is not going to cause conflicts
  14455. 12:31:02with anything outside the file for
  14456. 12:31:05example in our project we created a
  14457. 12:31:08variable called cart and we want to
  14458. 12:31:10contain this variable inside the cart.
  14459. 12:31:13JS file so that it doesn't conflict with
  14460. 12:31:16the cart variable inside the amazon. JS
  14461. 12:31:20file so we can do this using a module so
  14462. 12:31:23now let's learn how to create our first
  14463. 12:31:27module we're going to go back to amazon.
  14464. 12:31:30HTML and then here to create a module
  14465. 12:31:34we're simply going to delete this line
  14466. 12:31:36of code that uses a script tag to load
  14467. 12:31:39cart. JS so we'll delete it and
  14468. 12:31:43save and you'll see that the website is
  14469. 12:31:46working again so how does this work when
  14470. 12:31:50we use a script tag it will run all the
  14471. 12:31:52code inside cart. JS so this code will
  14472. 12:31:56create a card variable and this will
  14473. 12:31:58cause a naming Conflict by removing the
  14474. 12:32:02script tag we're no longer running the
  14475. 12:32:04code inside cart. JS so we're no longer
  14476. 12:32:07creating a cart variable and it will no
  14477. 12:32:10longer cause a naming
  14478. 12:32:13conflict so to create a module we simply
  14479. 12:32:17do two steps number one we create a file
  14480. 12:32:21and write our code inside the file and
  14481. 12:32:24number two don't load the file with a
  14482. 12:32:27script tag by doing this any variables
  14483. 12:32:31we create inside the file will be
  14484. 12:32:33contained inside the file and it won't
  14485. 12:32:36cause naming conflicts so that's
  14486. 12:32:39basically how we create a
  14487. 12:32:42module okay so now we run into another
  14488. 12:32:45problem in our code if we open amazon.
  14489. 12:32:49JS we created this card variable as an
  14490. 12:32:52example but what we actually want to do
  14491. 12:32:55is to keep the cart variable inside
  14492. 12:32:57cart. JS because we want to keep all the
  14493. 12:33:00code that's related to the cart inside
  14494. 12:33:03this file so in
  14495. 12:33:10and if we
  14496. 12:33:11save and then go back to our project and
  14497. 12:33:15click add to cart you'll notice that it
  14498. 12:33:18no longer works so let's look at the
  14499. 12:33:20console again to see what error we get
  14500. 12:33:23we'll right click
  14501. 12:33:26inspect and open the
  14502. 12:33:28console and it's going to tell us that
  14503. 12:33:31cart is not defined so the problem that
  14504. 12:33:34we have here is that in this file we're
  14505. 12:33:37using the variable cart but now because
  14506. 12:33:40we're not loading cart. JS using a
  14507. 12:33:43script tag the cart variable doesn't
  14508. 12:33:46exist anymore it's contained inside
  14509. 12:33:48cart. JS and we can't access it from the
  14510. 12:33:54outside so now we're going to learn how
  14511. 12:33:56to get a variable out of a file or a
  14512. 12:34:00module to do that we're going to do
  14513. 12:34:03three steps the first step is to add a
  14514. 12:34:06type equals module attribute the second
  14515. 12:34:09step is to export and the third step is
  14516. 12:34:12to import let's learn these steps one by
  14517. 12:34:16one so the first step is to add a type
  14518. 12:34:19equals module attribute we're going to
  14519. 12:34:22open amazon.
  14520. 12:34:24HTML and we're going to scroll to the
  14521. 12:34:27bottom and here in the last script tag
  14522. 12:34:31where we load amazon. JS we're going to
  14523. 12:34:33add an HTML attribute here so we're
  14524. 12:34:37going to type type equal equal module
  14525. 12:34:42and
  14526. 12:34:43save so the type equals module attribute
  14527. 12:34:47basically lets this file get variables
  14528. 12:34:49out of other files it basically enables
  14529. 12:34:53this feature now the second step is to
  14530. 12:34:56export so we're going to go to cart.
  14531. 12:35:00JS and now we're going to choose which
  14532. 12:35:02variables can be accessed outside of
  14533. 12:35:05this file so for example we want to
  14534. 12:35:08access the VAR variable cart outside of
  14535. 12:35:11this file to do that in front of the
  14536. 12:35:14variable we're just going to type the
  14537. 12:35:16word export and space and save so now
  14538. 12:35:22this variable can be used outside of
  14539. 12:35:24cart.
  14540. 12:35:26JS the last step is to
  14541. 12:35:29import so we're going to go to the place
  14542. 12:35:32where we're going to use this variable
  14543. 12:35:34which is inside amazon.
  14544. 12:35:37JS and now at the top we're going to
  14545. 12:35:40tell the computer which variable we want
  14546. 12:35:42to get from another file so we'll type
  14547. 12:35:47import open curly bracket close curly
  14548. 12:35:50bracket and between these brackets we're
  14549. 12:35:53going to give the name of the variable
  14550. 12:35:55we want to get which is the cart
  14551. 12:35:58variable and then we'll tell the
  14552. 12:36:00computer which file we want to get this
  14553. 12:36:03variable from so we'll type
  14554. 12:36:07from single quotes and and
  14555. 12:36:10semicolon and now inside these quotes
  14556. 12:36:13we're going to locate the file using a
  14557. 12:36:16file path so let's open up our files
  14558. 12:36:19here so it's easy to see what files we
  14559. 12:36:22have so right now we're in amazon. JS
  14560. 12:36:25and we want to locate cart. JS so the
  14561. 12:36:29first step is we actually need to get
  14562. 12:36:31out of the scripts folder we're in the
  14563. 12:36:33scripts folder and we need to get out of
  14564. 12:36:35it and into the data folder so to get
  14565. 12:36:38out of the current Cent folder that this
  14566. 12:36:40file is in we're going to type dot dot
  14567. 12:36:45so dot dot basically represents the
  14568. 12:36:47folder outside of this current folder
  14569. 12:36:50which is this folder right here
  14570. 12:36:52JavaScript Amazon projects and then
  14571. 12:36:54we're going to type a forward slash to
  14572. 12:36:57go outside of the scripts folder so now
  14573. 12:37:00that we're outside of this folder we're
  14574. 12:37:02going to go into the data folder so here
  14575. 12:37:06we're going to type data and then a
  14576. 12:37:08slash to go into this folder and we're
  14577. 12:37:11going to locate the file cart. JS
  14578. 12:37:15C.J and
  14579. 12:37:18save so what this code does is that it's
  14580. 12:37:21going to take the variable cart out of
  14581. 12:37:24the file cart. JS and we use this file
  14582. 12:37:28path here to locate that file so as a
  14583. 12:37:31reminder dot dot means to go outside of
  14584. 12:37:34the current folder outside of scripts
  14585. 12:37:36into here and then we go into the data
  14586. 12:37:38folder and then inside the data folder
  14587. 12:37:41we find cart.
  14588. 12:37:45JS so those are the three steps to get a
  14589. 12:37:48variable out of a module first we add a
  14590. 12:37:52type equals module attribute to enable
  14591. 12:37:55this feature and then we export the
  14592. 12:37:58variables that we want to get out and
  14593. 12:38:01finally we import the variables when we
  14594. 12:38:04need
  14595. 12:38:06them now there's two more things that we
  14596. 12:38:08need to know about modules number one we
  14597. 12:38:12have to put all of our Imports at the
  14598. 12:38:14top of the
  14599. 12:38:15file and number two in order for modules
  14600. 12:38:19to work we need to use live server so
  14601. 12:38:22for example if I open the folder that
  14602. 12:38:25contains amazon.
  14603. 12:38:27HTML so I open JavaScript Amazon Project
  14604. 12:38:31and then I select this HTML file and
  14605. 12:38:34rightclick open with Google
  14606. 12:38:38Chrome the website won't work and that's
  14607. 12:38:41because modules don't work if we open
  14608. 12:38:44the HTML file directly like this so
  14609. 12:38:47let's close this for
  14610. 12:38:49now and to get modules to work we need
  14611. 12:38:52to open our code editor and then
  14612. 12:38:55rightclick the HTML
  14613. 12:38:57file and open with live
  14614. 12:39:01server so in this course in order for
  14615. 12:39:04modules to work we need to use live
  14616. 12:39:07server to open our HTML files
  14617. 12:39:11so now that we learned how to use a
  14618. 12:39:13module let's talk about the benefits of
  14619. 12:39:17modules the first big benefit is that it
  14620. 12:39:20helps us avoid naming conflicts the only
  14621. 12:39:23time that this causes naming conflicts
  14622. 12:39:26is if we import this variable and we
  14623. 12:39:28have another variable named cart inside
  14624. 12:39:32this file like
  14625. 12:39:34this so this will again create a naming
  14626. 12:39:37conflict if I save this the website will
  14627. 12:39:40stop working but at least now we have
  14628. 12:39:43control over when naming conflicts
  14629. 12:39:45happen they will only happen if we
  14630. 12:39:47import the same variable name now
  14631. 12:39:50modules have another feature that allows
  14632. 12:39:52us to avoid this problem as well so
  14633. 12:39:55after the variable name that we import
  14634. 12:39:58we can type as and then a different name
  14635. 12:40:02such as my cart and save so this will
  14636. 12:40:07basically take the input and rename it
  14637. 12:40:10to my cart so that this will no longer
  14638. 12:40:13conflict with this variable name so as
  14639. 12:40:16you can see modules have a lot of
  14640. 12:40:18features that help us avoid naming
  14641. 12:40:21conflicts when we have lots of files in
  14642. 12:40:23our project so now I'm just going to
  14643. 12:40:26delete this and put the code back
  14644. 12:40:29because that was just an example and
  14645. 12:40:32save so the second big benefit of
  14646. 12:40:35modules is that we don't have to worry
  14647. 12:40:37about the order of our files
  14648. 12:40:40before when we loaded everything with
  14649. 12:40:42script tags we had to make sure that we
  14650. 12:40:45load C.J first because we need the cart
  14651. 12:40:48variable to be created and then we can
  14652. 12:40:51use it in amazon.
  14653. 12:40:53JS so the order of our script tags
  14654. 12:40:56really matter and it becomes really hard
  14655. 12:40:58to manage when we have lots of files in
  14656. 12:41:01a big
  14657. 12:41:02project but now with modules we can just
  14658. 12:41:05import the variables that we need and we
  14659. 12:41:08don't have to worry about the order that
  14660. 12:41:10we're loading our
  14661. 12:41:12files so as you can see modules are a
  14662. 12:41:15better way to organize our code
  14663. 12:41:18especially in bigger projects so now
  14664. 12:41:20let's use git and save our changes so
  14665. 12:41:24we'll go to the git section here and it
  14666. 12:41:26will tell us we changed three files in
  14667. 12:41:29amazon. JS we removed the script tag and
  14668. 12:41:32added type equals module in cart. JS we
  14669. 12:41:36exported the cart variable and in
  14670. 12:41:39amazon. JS we imported the cart variable
  14671. 12:41:43from cart. JS so now let's save these
  14672. 12:41:47into get so here we'll type a message
  14673. 12:41:50use
  14674. 12:41:52modules and import cart from cart. JS
  14675. 12:41:58and
  14676. 12:42:03commit now that we learned modules let's
  14677. 12:42:06use them in more places in our project
  14678. 12:42:09so let's close this for now and we're
  14679. 12:42:12going to open amazon.
  14680. 12:42:15HTML and next we're going to turn
  14681. 12:42:17products. JS into a module as well so
  14682. 12:42:21we'll start off by deleting the script
  14683. 12:42:23tag because the script tag will just
  14684. 12:42:26load all the code onto the page so we'll
  14685. 12:42:29delete this and
  14686. 12:42:31save so notice that our website stops
  14687. 12:42:35working and now inside amazon. JS we
  14688. 12:42:38need to to access products from
  14689. 12:42:40products. JS so again we're going to do
  14690. 12:42:43three steps the first step is to add a
  14691. 12:42:47type equals module attribute which we
  14692. 12:42:49already have so we don't need to do this
  14693. 12:42:51again the second step is to export the
  14694. 12:42:55variables that we want to get out so in
  14695. 12:42:58this case we want to get the products
  14696. 12:43:00variable out of the products. JS file so
  14697. 12:43:04let's open our files and open products.
  14698. 12:43:07Js
  14699. 12:43:09and we'll scroll to the
  14700. 12:43:11top so to get the products variable out
  14701. 12:43:14of this file in front of the variable
  14702. 12:43:17we're going to type export and space and
  14703. 12:43:22save and the last step is to import the
  14704. 12:43:26variable that we need so inside amazon.
  14705. 12:43:29JS we need the products variable so at
  14706. 12:43:33the top we're going to type
  14707. 12:43:37import curly brackets
  14708. 12:43:39and then inside here we're going to type
  14709. 12:43:41the variable name that we want to get
  14710. 12:43:44which is
  14711. 12:43:46products and we're going to get this
  14712. 12:43:48variable from single quotes semicolon
  14713. 12:43:53and then inside here we're going to
  14714. 12:43:55locate the products. JS file so we're
  14715. 12:43:58going to type dot dot and slash to go
  14716. 12:44:01outside of the current folder so outside
  14717. 12:44:04of the scripts folder and then we're
  14718. 12:44:06going to go into the data folder so type
  14719. 12:44:09data and a slash to go into the folder
  14720. 12:44:13and we're going to locate the file
  14721. 12:44:16products. JS and
  14722. 12:44:19save so now that we're importing this
  14723. 12:44:22variable from this file the rest of our
  14724. 12:44:25code will work and our website will
  14725. 12:44:27start working
  14726. 12:44:28again so that's how we turn products. JS
  14727. 12:44:32into a module as well so let's go into
  14728. 12:44:35get and take a look at our
  14729. 12:44:37changes so in amazon. HTML we deleted
  14730. 12:44:41the script tag and then in products. JS
  14731. 12:44:45we exported the variables that we wanted
  14732. 12:44:48to get out and then in amazon. JS we
  14733. 12:44:52imported the variables that we need from
  14734. 12:44:54the
  14735. 12:44:56file so let's type a message and save
  14736. 12:44:59these changes into git import products
  14737. 12:45:05from products. JS and
  14738. 12:45:13commit let's close
  14739. 12:45:15this and let's open amazon.
  14740. 12:45:19HTML now the last thing is you might be
  14741. 12:45:22wondering should we also delete this
  14742. 12:45:25script tag that loads amazon. JS and the
  14743. 12:45:28answer is no because we still need one
  14744. 12:45:31Javascript file to load our code so this
  14745. 12:45:35file will import from all the other
  14746. 12:45:37files that we created
  14747. 12:45:39and this is how we organize our code in
  14748. 12:45:41a real
  14749. 12:45:43website so this file is called the entry
  14750. 12:45:48point so now that we've seen some
  14751. 12:45:50examples of modules we're going to use
  14752. 12:45:52modules to organize our code better so
  14753. 12:45:56let's go into amazon.
  14754. 12:45:59JS and then we're going to scroll down
  14755. 12:46:01to the code for the add to card button
  14756. 12:46:04so inside this file we're going to
  14757. 12:46:06scroll down here all the way down to
  14758. 12:46:09this code here so this is a code that we
  14759. 12:46:12run when we click add it to cart and
  14760. 12:46:15you'll notice that we're running a lot
  14761. 12:46:17of code here when we click that button
  14762. 12:46:20we're running code that adds the product
  14763. 12:46:22to the cart and we're also running code
  14764. 12:46:25that calculates the quantity and updates
  14765. 12:46:27the page so a best practice in
  14766. 12:46:29programming is that when we have a lot
  14767. 12:46:31of code here that does different things
  14768. 12:46:33it's better to split these up into
  14769. 12:46:35smaller functions to make our code
  14770. 12:46:38easier to read so here this part of the
  14771. 12:46:42code takes the product ID and then adds
  14772. 12:46:45it to the cart so it makes sense to
  14773. 12:46:48split this into its own function so
  14774. 12:46:51let's scroll up and at the top here
  14775. 12:46:54let's create a function for this code so
  14776. 12:46:57we'll type function and let's call it
  14777. 12:47:01add to cart brackets and curly brackets
  14778. 12:47:06and then we're just going to move all
  14779. 12:47:08this code into the function so we'll
  14780. 12:47:10select this
  14781. 12:47:12code and then
  14782. 12:47:14rightclick
  14783. 12:47:16cut and then inside this
  14784. 12:47:19function rightclick and
  14785. 12:47:23paste and then down here we're going to
  14786. 12:47:25run this function inside here so when we
  14787. 12:47:30click add to cart we're going to run add
  14788. 12:47:33to cart function that we just created
  14789. 12:47:37and Save
  14790. 12:47:40so now let's give this a try so we'll
  14791. 12:47:42click add to
  14792. 12:47:44cart and unfortunately it doesn't work
  14793. 12:47:47anymore so let's take a look at the
  14794. 12:47:49console to see if there's any errors
  14795. 12:47:51we'll rightclick
  14796. 12:47:54inspect and then open the
  14797. 12:47:56console and it's telling us that product
  14798. 12:47:59ID is not defined so if we look at our
  14799. 12:48:02code
  14800. 12:48:03here and scroll up in the function
  14801. 12:48:07you'll notice that we're using product
  14802. 12:48:09but it's no longer anywhere in the
  14803. 12:48:11function and that's because product ID
  14804. 12:48:14is outside the
  14805. 12:48:16function so we actually want to keep
  14806. 12:48:18this code outside because it's using
  14807. 12:48:20this button so instead we're going to
  14808. 12:48:23take product ID and put it into a
  14809. 12:48:25function using a parameter so at the top
  14810. 12:48:29of the function we're going to add a
  14811. 12:48:31parameter product
  14812. 12:48:34ID and then when we call this function
  14813. 12:48:36we're going to pass it the product
  14814. 12:48:39ID so we're going to take this value put
  14815. 12:48:42it into the function and save it in here
  14816. 12:48:45and then the function can run as normal
  14817. 12:48:48so now if we save and then go to our
  14818. 12:48:51project and click add to cart you can
  14819. 12:48:53see that it's working
  14820. 12:48:56again next let's go back and scroll
  14821. 12:49:01down and we're going to put the rest of
  14822. 12:49:03our code into its own function as well
  14823. 12:49:06to make it more organized so at the top
  14824. 12:49:08here let's create some new lines and
  14825. 12:49:11create a
  14826. 12:49:13function and this code updates the card
  14827. 12:49:16quantity on the page
  14828. 12:49:19here so let's name this function
  14829. 12:49:23update cart quantity brackets and curly
  14830. 12:49:28brackets and we'll move this code into
  14831. 12:49:30the function so let's select this code
  14832. 12:49:34and then rightclick cut and then in here
  14833. 12:49:38rightclick and
  14834. 12:49:40paste and then down here we'll call the
  14835. 12:49:44function using update cart quantity and
  14836. 12:49:50brackets and
  14837. 12:49:53save so as you can see by splitting up
  14838. 12:49:56our code into smaller functions it makes
  14839. 12:49:58our code a lot easier to read when we
  14840. 12:50:01click add to cart it's really easy to
  14841. 12:50:04understand what's going on first we're
  14842. 12:50:06going to add a product to our cart and
  14843. 12:50:09then we're going to update the card
  14844. 12:50:11quantity so now let's save and then go
  14845. 12:50:14back to our project and try it out and
  14846. 12:50:17you can see that everything is working
  14847. 12:50:20again so we're going to do two more
  14848. 12:50:22things to better organize our code so
  14849. 12:50:25let's go back to our code and the first
  14850. 12:50:27thing is we're going to scroll up to the
  14851. 12:50:30add to cart function and I made a small
  14852. 12:50:33mistake in the previous lesson I
  14853. 12:50:35actually want to rename this parameter
  14854. 12:50:37from item to cart item instead to make
  14855. 12:50:41it a little more clear so we're just
  14856. 12:50:44going to rename this to cart item and
  14857. 12:50:47here as well we name it to cart item and
  14858. 12:50:53save and we'll scroll down here and
  14859. 12:50:56we're going to do the same thing for
  14860. 12:50:57this parameter so we'll rename it to
  14861. 12:51:00cart item and then here cart item and
  14862. 12:51:06Save
  14863. 12:51:10and the last thing we'll do is let's
  14864. 12:51:12scroll up to the add to cart
  14865. 12:51:14function so you'll notice that this code
  14866. 12:51:17manages our cart so it's actually a best
  14867. 12:51:20practice to move this function into
  14868. 12:51:22cart. JS because cart. JS contains all
  14869. 12:51:26the code that's related to the cart a
  14870. 12:51:29best practice is to group related code
  14871. 12:51:31together into its own
  14872. 12:51:34file so here let's take the function add
  14873. 12:51:38to cart we'll select it and then
  14874. 12:51:41rightclick
  14875. 12:51:43cut and then inside cart. JS we're going
  14876. 12:51:47to rightclick and paste and just move
  14877. 12:51:52that function into this file because
  14878. 12:51:54this file contains all the code that's
  14879. 12:51:56related to the cart so now that this
  14880. 12:51:59function is inside the file we need to
  14881. 12:52:02access it from amazon.
  14882. 12:52:05JS and we can do that using modules
  14883. 12:52:08using the features export and import so
  14884. 12:52:12inside cart. JS in front of the function
  14885. 12:52:16we're going to export it so we can use
  14886. 12:52:19it in other files so let's save this
  14887. 12:52:22file and then inside amazon. JS we're
  14888. 12:52:26going to scroll to the
  14889. 12:52:28top and you'll notice that we're already
  14890. 12:52:30importing something from cart. JS so we
  14891. 12:52:34actually don't need to write import
  14892. 12:52:35again here we can actually import
  14893. 12:52:38multiple things from this file by typing
  14894. 12:52:42comma and space and then something else
  14895. 12:52:45that we want to import so in our case
  14896. 12:52:47we're going to import the function add
  14897. 12:52:49to cart so here we'll type comma add to
  14898. 12:52:54cart so now it will import both the cart
  14899. 12:52:58and the add to cart function from this
  14900. 12:53:01file so Imports can import multiple
  14901. 12:53:03things from a
  14902. 12:53:05file Imports have another syntax import
  14903. 12:53:09Star as this Imports everything from a
  14904. 12:53:12file and groups it together inside this
  14905. 12:53:16object and then we can access each
  14906. 12:53:18import as a property or a
  14907. 12:53:24method so lastly I'm just going to
  14908. 12:53:26scroll down to my code
  14909. 12:53:29here and I'm just going to remove this
  14910. 12:53:31extra new line and
  14911. 12:53:34save now you might be wondering if we
  14912. 12:53:37should move update cart quantity into
  14913. 12:53:39cart. JS as
  14914. 12:53:41well so this function actually handles
  14915. 12:53:44updating the web page rather than
  14916. 12:53:46managing the cart so we're going to keep
  14917. 12:53:48this function inside this file for
  14918. 12:53:52now so now let's go into get and save
  14919. 12:53:55our changes so we'll go here and inside
  14920. 12:53:58cart. JS we moved the add to cart
  14921. 12:54:02function and then we exported it and
  14922. 12:54:05then inside amazon. JS we imported the
  14923. 12:54:08add to card
  14924. 12:54:10function and then down here we took the
  14925. 12:54:13big block of code that we had before and
  14926. 12:54:15we separated it into two functions add
  14927. 12:54:18to cart and update cart quantity and
  14928. 12:54:21this made our code a lot easier to
  14929. 12:54:24read so now let's type a message
  14930. 12:54:27describing our changes move add to cart
  14931. 12:54:32into cart. JS and commit
  14932. 12:54:41next we're going to create the checkout
  14933. 12:54:43page in our final project and we're
  14934. 12:54:45going to use modules to organize our
  14935. 12:54:47code again so let's open the final
  14936. 12:54:51project and we're going to click this
  14937. 12:54:53cart icon in the top
  14938. 12:54:55right and this will take us to another
  14939. 12:54:57page which is the checkout page so on
  14940. 12:55:00this page we can see the products that
  14941. 12:55:02are in our
  14942. 12:55:03cart so if you're wondering how that
  14943. 12:55:05cart icon works it basically uses is an
  14944. 12:55:08HTML element called a link element which
  14945. 12:55:12looks like this link elements have a
  14946. 12:55:14special attribute called href when we
  14947. 12:55:18click a link element whatever is inside
  14948. 12:55:21the href attribute it will open it in
  14949. 12:55:23the page so if we put youtube.com inside
  14950. 12:55:27the href and we click the link it will
  14951. 12:55:30open youtube.com in our browser if we
  14952. 12:55:33put a file like checkout. HTML inside
  14953. 12:55:37href it will look for a file called
  14954. 12:55:39checkout. HTML and open that
  14955. 12:55:44file so in our project if we look at our
  14956. 12:55:48code and open our
  14957. 12:55:50files inside amazon.
  14958. 12:55:53HTML we're basically just using a link
  14959. 12:55:56element or an a element and this has an
  14960. 12:55:59hre of checkout. HTML so what this does
  14961. 12:56:03is it's going to look for a file called
  14962. 12:56:05checkout. HTML beside the current file
  14963. 12:56:08amazon. HTML so here's amazon. HTML and
  14964. 12:56:13it's going to look for checkout. HTML
  14965. 12:56:15beside it and then open that in our
  14966. 12:56:17browser which is how that cart icon
  14967. 12:56:20works so in our browser at the top you
  14968. 12:56:23can see that it opened checkout. HTML so
  14969. 12:56:26that's basically how a link element
  14970. 12:56:28works and how we can navigate from one
  14971. 12:56:30page to another you can learn more about
  14972. 12:56:33link elements in my HTML and CSS full
  14973. 12:56:36course but for now now we're going to
  14974. 12:56:39learn how to build the checkout page
  14975. 12:56:41using
  14976. 12:56:42JavaScript so first let's take a look at
  14977. 12:56:44the code for this page so again the code
  14978. 12:56:48is inside checkout.
  14979. 12:56:50HTML so in our code editor we're going
  14980. 12:56:53to open checkout.
  14981. 12:56:56HTML and this is a code for the page
  14982. 12:56:59that we saw earlier that contains our
  14983. 12:57:02cart so just like with the homepage
  14984. 12:57:05instead of writing all this HTML
  14985. 12:57:08manually we're going to use JavaScript
  14986. 12:57:10to generate this
  14987. 12:57:12HTML so first we need to add JavaScript
  14988. 12:57:15to this page so let's scroll down to the
  14989. 12:57:18very bottom
  14990. 12:57:19here and right before the end of the
  14991. 12:57:23body we're going to create a script
  14992. 12:57:26element to load some
  14993. 12:57:28JavaScript and we'll give it an
  14994. 12:57:30attribute Source equals double quotes
  14995. 12:57:33and then a file we want to load so let's
  14996. 12:57:36create a new Javascript file file for
  14997. 12:57:38this page so inside our scripts folder
  14998. 12:57:42let's click amazon. JS and then click
  14999. 12:57:45this icon to create a new file and let's
  15000. 12:57:48name this file checkout. JS and press
  15001. 12:57:53enter and now we're going to load this
  15002. 12:57:55page inside checkout.
  15003. 12:57:57HTML so here in the source attribute
  15004. 12:58:01we're going to locate this file so first
  15005. 12:58:04we need to go into this scripts folder
  15006. 12:58:07so we'll type
  15007. 12:58:08scripts and then a slash to go into the
  15008. 12:58:11folder and then we'll load the file
  15009. 12:58:15checkout.
  15010. 12:58:17JS and next we're going to add another
  15011. 12:58:20attribute type equals double quotes
  15012. 12:58:25module so remember from earlier in the
  15013. 12:58:27lesson this attribute enables module
  15014. 12:58:30features for this file so now this file
  15015. 12:58:33can import from other JavaScript files
  15016. 12:58:38so now let's
  15017. 12:58:39save and let's open checkout. JS and
  15018. 12:58:44we'll start creating this page with
  15019. 12:58:46JavaScript so let's go back to our page
  15020. 12:58:49first and take a look at the structure
  15021. 12:58:51of this page so you can see on this page
  15022. 12:58:54we actually have two sections we have
  15023. 12:58:57the cart section on the left which shows
  15024. 12:59:00the products in our cart and we also
  15025. 12:59:03have the payment section on the right
  15026. 12:59:05which calculates the cost of our order
  15027. 12:59:08so let's use JavaScript to create the
  15028. 12:59:10cart section first so remember the main
  15029. 12:59:14idea of JavaScript is first we're going
  15030. 12:59:17to save the data and then we're going to
  15031. 12:59:19generate the HTML and finally we'll make
  15032. 12:59:22it
  15033. 12:59:23interactive let's start by saving the
  15034. 12:59:26data that we need for this section of
  15035. 12:59:28the page so for this section we're
  15036. 12:59:31basically displaying a list of products
  15037. 12:59:33along with the quantity of each product
  15038. 12:59:36that we want to buy and and if you
  15039. 12:59:38remember we already created a data
  15040. 12:59:40structure to represent this which is our
  15041. 12:59:43cart inside our cart we have the ID of
  15042. 12:59:46the product as well as the quantity that
  15043. 12:59:49we want to buy so we can just reuse this
  15044. 12:59:52data structure to create this section so
  15045. 12:59:55let's go into our
  15046. 12:59:57code and we're going to open cart.
  15047. 13:00:01JS so inside here we have an array
  15048. 13:00:04called cart that's supposed to represent
  15049. 13:00:06our data here
  15050. 13:00:08so right now our card is empty so to
  15051. 13:00:12make it easier for us to develop we're
  15052. 13:00:14just going to add some default values
  15053. 13:00:16into this array so inside here let's
  15054. 13:00:20create an object and put one product
  15055. 13:00:23into our card first so looking at this
  15056. 13:00:26page the product that we want to put is
  15057. 13:00:29the socks so we'll go back into our code
  15058. 13:00:32and I'll rearrange my windows so we can
  15059. 13:00:34see it side by side and we're going to
  15060. 13:00:38get the product ID for the socks here so
  15061. 13:00:40we're going to go into products.
  15062. 13:00:42JS and then here's the socks we just
  15063. 13:00:46going to take this
  15064. 13:00:47ID right click
  15065. 13:00:51copy and then inside the
  15066. 13:00:54cart and remember we structured our data
  15067. 13:00:57so that each card item has a product ID
  15068. 13:01:00as well as a
  15069. 13:01:02quantity so here we're going to create
  15070. 13:01:05the same structure that our current code
  15071. 13:01:07uses
  15072. 13:01:08so we're going to give each object a
  15073. 13:01:11product ID colon and single quotes and
  15074. 13:01:16we're just going to rightclick and
  15075. 13:01:19paste the ID that we copied earlier and
  15076. 13:01:22then we'll type a comma and we'll give
  15077. 13:01:25it a
  15078. 13:01:26quantity colon and for the first item
  15079. 13:01:30we're going to give it a quantity of two
  15080. 13:01:33so we'll just create a cart that kind of
  15081. 13:01:35matches what we see on this page
  15082. 13:01:38next let's create a second card item to
  15083. 13:01:41represent the basketball so we'll type
  15084. 13:01:44comma curly brackets we'll give it a
  15085. 13:01:48product ID colon and single quotes and
  15086. 13:01:53then we're going to get the ID for this
  15087. 13:01:55basketball so inside products. JS we
  15088. 13:01:59going to scroll down to the basketball
  15089. 13:02:01here and we're going to copy this
  15090. 13:02:04ID right click copy
  15091. 13:02:08and then inside cart.
  15092. 13:02:10JS in here right click and
  15093. 13:02:14paste and we'll give this one a
  15094. 13:02:17quantity colon one and
  15095. 13:02:21save so what we're doing here is we're
  15096. 13:02:23just creating some default values that
  15097. 13:02:25we can use to write the code to generate
  15098. 13:02:28this page so this is just for
  15099. 13:02:31development when we're actually using
  15100. 13:02:33the website the cart will start off as
  15101. 13:02:36empty so so if we compare our data to
  15102. 13:02:39our page you might notice that there's a
  15103. 13:02:42few things that are missing such as the
  15104. 13:02:44product's image the name and the price
  15105. 13:02:47so how come we're not saving these into
  15106. 13:02:50our data and that's because we're using
  15107. 13:02:53a special technique we're just saving
  15108. 13:02:55the product's ID and then we can use
  15109. 13:02:58this ID to search for this product
  15110. 13:03:00inside the products array here so using
  15111. 13:03:04the ID we can find all the other details
  15112. 13:03:07of the product like the image the name
  15113. 13:03:10and the price so there's no need to save
  15114. 13:03:13this data twice inside the products
  15115. 13:03:15array and inside the cart array all we
  15116. 13:03:19have to do is save the ID of the product
  15117. 13:03:22that we want and then search for it so
  15118. 13:03:24this technique is called duplicating the
  15119. 13:03:27data or normalizing the data and this is
  15120. 13:03:31a very common technique in software
  15121. 13:03:34engineering okay so the one other thing
  15122. 13:03:37that we probably need to save in our
  15123. 13:03:38data is the delivery times and the
  15124. 13:03:41delivery options but we're going to
  15125. 13:03:43leave this for the next lesson because
  15126. 13:03:45it requires some more features that
  15127. 13:03:47we're going to
  15128. 13:03:48learn so for now we finished saving the
  15129. 13:03:51data that we need the next step is to
  15130. 13:03:54use this data to generate the
  15131. 13:03:58HTML so let's open checkout.
  15132. 13:04:01JS so I'm just going to drag it to the
  15133. 13:04:03bottom here so we can see it side by
  15134. 13:04:06side
  15135. 13:04:10so the first step we need to do is to
  15136. 13:04:12take the cart array and import it into
  15137. 13:04:16checkout. JS so we can use it so in here
  15138. 13:04:20let's import curly brackets the cart
  15139. 13:04:25array and we're going to import it from
  15140. 13:04:29single quotes and semicolon and we're
  15141. 13:04:31going to use a file path between the
  15142. 13:04:33quotes to locate cart. JS so let's open
  15143. 13:04:37our files again and to get from here to
  15144. 13:04:41here we need to go out of the scripts
  15145. 13:04:44folder so to do that we can type dot dot
  15146. 13:04:47and then slash to go to the outer folder
  15147. 13:04:51and then we're going to go to the data
  15148. 13:04:53folder so we'll type data slash to go
  15149. 13:04:56into this folder and we'll locate cart.
  15150. 13:04:59JS so cart. JS and save and next we're
  15151. 13:05:05going to Loop through this cart and
  15152. 13:05:07generate the HTML just like we did for
  15153. 13:05:10the
  15154. 13:05:11homepage so let's close this for now and
  15155. 13:05:15here we're going to create a loop over
  15156. 13:05:17the cart so we'll type cart. for each
  15157. 13:05:23brackets and a
  15158. 13:05:26function and then we'll give it a
  15159. 13:05:28parameter let's call it cart
  15160. 13:05:31item and then for each cart item here
  15161. 13:05:34we're going to generate this
  15162. 13:05:36HTML so in here let's create a template
  15163. 13:05:39string for the HTML using back ticks and
  15164. 13:05:43we'll create some new lines and then
  15165. 13:05:45here we're going to copy and paste the
  15166. 13:05:47HTML for one of these products so first
  15167. 13:05:51let's find the HTML for one of these
  15168. 13:05:53products and to do that we can
  15169. 13:05:56rightclick
  15170. 13:05:58inspect and then click this top left
  15171. 13:06:01icon and then click the element we want
  15172. 13:06:04to get the HTML
  15173. 13:06:05for so here's the HTML for one of these
  15174. 13:06:09products and we're going to search for
  15175. 13:06:11this class card item container and our
  15176. 13:06:14code so inside our
  15177. 13:06:17code let's open checkout.
  15178. 13:06:21HTML and we'll type control F or command
  15179. 13:06:24F to search for code and we'll search
  15180. 13:06:27for the class cart D item D
  15181. 13:06:32container which is this code right here
  15182. 13:06:35so if we collapse it and collapse this
  15183. 13:06:39this is the code for these two products
  15184. 13:06:42here so we're going to take this HTML
  15185. 13:06:45and we're going to move it into our
  15186. 13:06:46JavaScript so we can generate it with
  15187. 13:06:49JavaScript so we'll right click
  15188. 13:06:53copy and then in here right click and
  15189. 13:06:57paste and if we need to fix the
  15190. 13:06:59formatting we can select all of these
  15191. 13:07:02lines and then press shift tab to remove
  15192. 13:07:05some indents
  15193. 13:07:07and I'll add an indent here next when
  15194. 13:07:10we're generating this HTML we want to
  15195. 13:07:13replace this with the actual products
  15196. 13:07:16image and this with the actual products
  15197. 13:07:18name so we're going to take this card
  15198. 13:07:21item and use it to get the data that we
  15199. 13:07:23need so let's open card. JS to see what
  15200. 13:07:27each cart item looks like so we have the
  15201. 13:07:29product ID and the
  15202. 13:07:32quantity so first we need to use this
  15203. 13:07:35product ID to search for the product in
  15204. 13:07:38order to get the other details like the
  15205. 13:07:40image the name and the
  15206. 13:07:42price so to do that let's get the
  15207. 13:07:45product ID out of the cart item first so
  15208. 13:07:49we'll create a variable const product ID
  15209. 13:07:53equals the cart item do product
  15210. 13:07:59ID and now we need to use this to search
  15211. 13:08:02for the full product so the full product
  15212. 13:08:05if we open our files is INS inside
  15213. 13:08:07products.
  15214. 13:08:09JS which is this array right here so I'm
  15215. 13:08:13just going to move this up here so we
  15216. 13:08:14can see it side by
  15217. 13:08:16side so to search inside this array we
  15218. 13:08:20actually need to import products into
  15219. 13:08:23checkout. JS so let's use some module
  15220. 13:08:26features again at the top we'll import
  15221. 13:08:31curly
  15222. 13:08:32brackets the products array and we'll
  15223. 13:08:35import it from single quotes and
  15224. 13:08:39semicolon and then here we'll locate the
  15225. 13:08:42products. JS file so we'll type do dot
  15226. 13:08:45slash to go out of the scripts folder
  15227. 13:08:49and then we'll go into the data folder
  15228. 13:08:51data and then a slash to go into this
  15229. 13:08:54folder and we import from products. JS
  15230. 13:08:58so
  15231. 13:08:59products. JS and save so now we have
  15232. 13:09:04access to this products array here and
  15233. 13:09:07we'll search the array for the full
  15234. 13:09:09product details so down
  15235. 13:09:12here let's create a variable to save the
  15236. 13:09:15result so we'll type let and we'll name
  15237. 13:09:19it matching
  15238. 13:09:22product and then down here we'll Loop
  15239. 13:09:25through the products array so we'll type
  15240. 13:09:28products. for each brackets and a
  15241. 13:09:34function and then we'll give it a
  15242. 13:09:36parameter
  15243. 13:09:38product and now for each product here
  15244. 13:09:42we're going to check if the ID property
  15245. 13:09:44is equal to our product ID here so down
  15246. 13:09:48here we'll check if the
  15247. 13:09:51product. ID property is equal to our
  15248. 13:09:56product ID up here and then if they're
  15249. 13:10:00equal that means we found the matching
  15250. 13:10:03product and we're going to save it in
  15251. 13:10:05this variable so matching
  15252. 13:10:09product equals this product that We're
  15253. 13:10:12looping
  15254. 13:10:13through so now that we have the matching
  15255. 13:10:15product we have access to the image the
  15256. 13:10:18name as well as the price that we can
  15257. 13:10:21use to generate this HTML so this is a
  15258. 13:10:24good example of duplicating or
  15259. 13:10:27normalizing our data so now at the
  15260. 13:10:30bottom let's
  15261. 13:10:32console.log our matching product to see
  15262. 13:10:35if it works so matching
  15263. 13:10:39product and
  15264. 13:10:41save now if we go into our project and
  15265. 13:10:45we rightclick inspect and check our
  15266. 13:10:49console you can see that we were able to
  15267. 13:10:52get the full product details for each of
  15268. 13:10:54these products using the product ID so
  15269. 13:10:58now that we have the full product
  15270. 13:10:59details we can use this to generate all
  15271. 13:11:02this HTML
  15272. 13:11:07so down here instead of generating the
  15273. 13:11:11same image every time we're going to use
  15274. 13:11:13the image in the matching product so
  15275. 13:11:16let's get rid of this
  15276. 13:11:18code and here we're going to remove this
  15277. 13:11:22and we're going to substitute using
  15278. 13:11:24dollar curly
  15279. 13:11:25brackets the matching
  15280. 13:11:29product.
  15281. 13:11:31image and we'll scroll down and instead
  15282. 13:11:34of the same product name every time
  15283. 13:11:36we're going we're going to replace this
  15284. 13:11:39and we're going to substitute the
  15285. 13:11:42matching
  15286. 13:11:43product. name so each time we're going
  15287. 13:11:46to generate HTML with a different image
  15288. 13:11:49and a different name and down here the
  15289. 13:11:51same thing remove the price and we'll
  15290. 13:11:55replace it with matching product Dot and
  15291. 13:12:00then we'll just check here what the
  15292. 13:12:01property is it's called a price sense
  15293. 13:12:05and remember this is in
  15294. 13:12:07and to display it as dollars we have to
  15295. 13:12:10convert it by dividing by 100 and
  15296. 13:12:15save and lastly we're going to replace
  15297. 13:12:17the quantity here so if we scroll down
  15298. 13:12:21we can see that this is the quantity so
  15299. 13:12:23instead of two every time we'll
  15300. 13:12:28substitute and for the quantity remember
  15301. 13:12:30it's not part of a product it's part of
  15302. 13:12:33our cart so each cart item has a
  15303. 13:12:36property called called quantity so down
  15304. 13:12:38here we're going to substitute the cart
  15305. 13:12:43item.
  15306. 13:12:45quantity and
  15307. 13:12:48save so now we're generating all this
  15308. 13:12:51HTML by looping through our cart so the
  15309. 13:12:55next step is we're going to combine all
  15310. 13:12:57this HTML together and then put it on
  15311. 13:13:00the page so I'm just going to move this
  15312. 13:13:02up here so we can see more of the
  15313. 13:13:06code so to combine all this HTML
  15314. 13:13:09together let's create a variable up here
  15315. 13:13:11to store the result so we'll type let
  15316. 13:13:15and let's call it the cart summary
  15317. 13:13:20HTML equals an empty string and now each
  15318. 13:13:25time we Loop through the cart we're
  15319. 13:13:27going to add this HTML up here so that
  15320. 13:13:29we combine it so here we'll type cart
  15321. 13:13:34summary
  15322. 13:13:35HTML Plus
  15323. 13:13:37equals this HTML that we
  15324. 13:13:40generated and at the bottom I'll just
  15325. 13:13:43add a
  15326. 13:13:44semicolon and then let's console.log the
  15327. 13:13:47cart summary
  15328. 13:13:49HTML
  15329. 13:13:50console.log cart summary HTML to make
  15330. 13:13:55sure that it worked so we'll save and
  15331. 13:13:59then in here we'll check the console
  15332. 13:14:02right click
  15333. 13:14:04inspect the console
  15334. 13:14:07and now you can see that we generated
  15335. 13:14:09the HTML and each cart item is different
  15336. 13:14:13depending on the product that is being
  15337. 13:14:15displayed so that's how we generate all
  15338. 13:14:17the HTML for this section and the last
  15339. 13:14:20step is to take this and put it on the
  15340. 13:14:23page using the Dom so first we need an
  15341. 13:14:26element that we can put the HTML inside
  15342. 13:14:29of to find this element let's rightclick
  15343. 13:14:34inspect and click the top left left and
  15344. 13:14:38click one of these
  15345. 13:14:39products and let's find this order
  15346. 13:14:42summary element because this contains
  15347. 13:14:45both of our products here so we're going
  15348. 13:14:47to replace all the HTML inside here with
  15349. 13:14:50what we generated so let's search for
  15350. 13:14:53the class order summary in our code so
  15351. 13:14:56we'll go back to our code and let's open
  15352. 13:14:59checkout.
  15353. 13:15:02HTML and we'll type contrl f or command
  15354. 13:15:04F to search for code and we'll search
  15355. 13:15:07for the class
  15356. 13:15:09order-
  15357. 13:15:10summary and then it's right
  15358. 13:15:13here and we're going to add a class so
  15359. 13:15:16we can select it using the Dom so js-
  15360. 13:15:20order- summary and
  15361. 13:15:23save so now let's use the Dom and put
  15362. 13:15:26this element in our JavaScript so inside
  15363. 13:15:29here at the
  15364. 13:15:30[Music]
  15365. 13:15:32bottom we use
  15366. 13:15:34document. Query select VOR
  15367. 13:15:38brackets and the class we just
  15368. 13:15:41created js-
  15369. 13:15:44order-
  15370. 13:15:46summary and we'll use the property.
  15371. 13:15:49inner Capital
  15372. 13:15:51HTML and we'll change the HTML to cart
  15373. 13:15:56summary
  15374. 13:15:58HTML and we can remove this
  15375. 13:16:01line and now if we
  15376. 13:16:04save we're basically generating all this
  15377. 13:16:06h HTML with JavaScript and then putting
  15378. 13:16:09it on the page so now we can go back to
  15379. 13:16:12checkout.
  15380. 13:16:13HTML and we can actually remove the code
  15381. 13:16:17inside this order summary so we can
  15382. 13:16:19delete this
  15383. 13:16:21code and save and everything will still
  15384. 13:16:25work because this is now being generated
  15385. 13:16:28by
  15386. 13:16:30JavaScript so that's how we create the
  15387. 13:16:32cart section of this page with
  15388. 13:16:35JavaScript let's go into get and save
  15389. 13:16:38our changes so we'll open the git
  15390. 13:16:41section and then it will tell us that we
  15391. 13:16:43changed three files in checkout. HTML we
  15392. 13:16:48removed all the HTML that was written
  15393. 13:16:51manually and then at the bottom we
  15394. 13:16:53created a script tag to load our
  15395. 13:16:56JavaScript and then we also changed
  15396. 13:16:58cart. JS and added some default values
  15397. 13:17:02to work with and then we created
  15398. 13:17:04checkout. JS which loops through the
  15399. 13:17:07cart creates the HTML and puts it on the
  15400. 13:17:10page so let's create a message to
  15401. 13:17:13describe our
  15402. 13:17:14changes generate
  15403. 13:17:17HTML for the order summary and
  15404. 13:17:27commit next let's open our project
  15405. 13:17:31again and before we continue we actually
  15406. 13:17:34have two issues that we need to fix the
  15407. 13:17:36first issue is that the price is
  15408. 13:17:39displaying as one decimal here instead
  15409. 13:17:41of two and the second issue is that
  15410. 13:17:44these delivery options are actually
  15411. 13:17:46shared between the two products for
  15412. 13:17:49example if I click this delivery option
  15413. 13:17:51here it will actually unselect for the
  15414. 13:17:54second product so they're sort of shared
  15415. 13:17:57between the two
  15416. 13:17:58products but what we actually want is
  15417. 13:18:00for each product to be able to select
  15418. 13:18:03its own delivery options so we're going
  15419. 13:18:05to fix these two issues
  15420. 13:18:07next first let's focus on the price and
  15421. 13:18:11we want to display it in two
  15422. 13:18:13decimals so we actually learn how to fix
  15423. 13:18:15this on the homepage using the method to
  15424. 13:18:18fixed so we're going to use the same
  15425. 13:18:20code here let's go into our
  15426. 13:18:24code and we'll open our files and let's
  15427. 13:18:28open checkout. JS
  15428. 13:18:30again so here we're going to scroll down
  15429. 13:18:33to where we display the
  15430. 13:18:35price which is here price sense and to
  15431. 13:18:38show this in two decimal places we're
  15432. 13:18:41going to surround this with brackets to
  15433. 13:18:43make sure we calculate it
  15434. 13:18:45first and then use the method
  15435. 13:18:48do2 fixed brackets and then two to show
  15436. 13:18:53two decimal places if we save and then
  15437. 13:18:57open our
  15438. 13:18:59project now the price is displaying
  15439. 13:19:02correctly before we move on we're going
  15440. 13:19:04to learn how to better organize this
  15441. 13:19:07code so if we go
  15442. 13:19:09back so notice that we're using this
  15443. 13:19:11code inside the checkout page as well as
  15444. 13:19:14inside amazon. JS which is the homepage
  15445. 13:19:19so we can actually share this code
  15446. 13:19:21between the two files so we don't have
  15447. 13:19:23to repeat it and to do that we can use
  15448. 13:19:26modules so let's open checkout. JS again
  15449. 13:19:30and what we'll do is we're going to
  15450. 13:19:32create a function that does the same
  15451. 13:19:34thing as this code and then we can share
  15452. 13:19:37this function between the two files so
  15453. 13:19:40let's create a file that's going to
  15454. 13:19:42contain this shared function so we're
  15455. 13:19:45going to go to our scripts folder here
  15456. 13:19:47and inside we're going to create a new
  15457. 13:19:50folder this time and let's call this
  15458. 13:19:53folder
  15459. 13:19:54utils and press
  15460. 13:19:57enter so we're naming it utils because
  15461. 13:20:00this code is sort of like a useful
  15462. 13:20:02utility that we want to
  15463. 13:20:04share now inside this folder we're going
  15464. 13:20:07to have different types of utils so
  15465. 13:20:10let's create a new
  15466. 13:20:11file and let's name it money. JJs so
  15467. 13:20:17this file is going to contain utilities
  15468. 13:20:20that are related to money which is this
  15469. 13:20:22code right here so let's press
  15470. 13:20:25enter and I'm just going to move this to
  15471. 13:20:28the bottom so we can see it side by
  15472. 13:20:33side so now inside this UIL file we're
  15473. 13:20:36going to create a function that does the
  15474. 13:20:38same thing as here and then we can share
  15475. 13:20:40it between the two
  15476. 13:20:42files so here we'll create a function
  15477. 13:20:47and let's name it format currency
  15478. 13:20:51because that's what this code is doing
  15479. 13:20:54and then brackets and curly
  15480. 13:20:56brackets so this function is basically
  15481. 13:20:59going to do the same thing as here so
  15482. 13:21:02we're going to add a parameter here
  15483. 13:21:05price sense
  15484. 13:21:07because we want to use this function for
  15485. 13:21:09any amount of price
  15486. 13:21:11sents and then in here we're going to do
  15487. 13:21:14the same thing which is price sents
  15488. 13:21:18divided 100 and then we're going to
  15489. 13:21:21surround this with brackets to make sure
  15490. 13:21:23we calculate this
  15491. 13:21:25first and then display it in two decimal
  15492. 13:21:28places using dot 2 fixed brackets and
  15493. 13:21:33two and finally let's return this result
  15494. 13:21:38return and
  15495. 13:21:41save so what we just did is we created a
  15496. 13:21:44function that does the same thing as
  15497. 13:21:46this code here and now we're going to
  15498. 13:21:48share this function between checkout. JS
  15499. 13:21:51and amazon. JS so first in order to
  15500. 13:21:55share it we need to export this function
  15501. 13:21:58so at the front we're going to type
  15502. 13:22:01export and
  15503. 13:22:03save and now inside each of these files
  15504. 13:22:06we're going to to import this function
  15505. 13:22:08and use it so let's scroll to the
  15506. 13:22:12top and here we're going to import curly
  15507. 13:22:16brackets we're going to import the
  15508. 13:22:19function format
  15509. 13:22:21currency and we're going to import it
  15510. 13:22:24from single quotes and
  15511. 13:22:27semicolon and then inside here we're
  15512. 13:22:29going to locate the file money. JS so
  15513. 13:22:33here we're in the checkout. JS file
  15514. 13:22:36which is right here and to go here we're
  15515. 13:22:39first going to go into the UTS folder so
  15516. 13:22:42we're going to Type U toils and then a
  15517. 13:22:45slash to go into this folder and we're
  15518. 13:22:48going to locate the file money. JS so
  15519. 13:22:51money.
  15520. 13:22:53JS now for modules we have to do one
  15521. 13:22:56extra thing at the front of this file
  15522. 13:22:59path we need to type dot
  15523. 13:23:02slash so remember that dot dot
  15524. 13:23:05represents the folder outside the
  15525. 13:23:07current folder so for checkout. JS if we
  15526. 13:23:10go outside of this folder we're going to
  15527. 13:23:12go outside the scripts
  15528. 13:23:14folder now a single dot on the other
  15529. 13:23:17hand means the current folder so this
  15530. 13:23:20dot means we're going to go into the
  15531. 13:23:22current folder which is the scripts
  15532. 13:23:24folder and then we're going to go into
  15533. 13:23:26the utils folder and then we're going to
  15534. 13:23:29go and locate money.
  15535. 13:23:32JS so for modules if we want to start in
  15536. 13:23:35the current f folder our file paths have
  15537. 13:23:38to start with Slash so that's one extra
  15538. 13:23:41thing that we have to do so now that
  15539. 13:23:44we've imported the format currency
  15540. 13:23:46function let's use it in the rest of our
  15541. 13:23:48code so we're going to scroll
  15542. 13:23:51down to here and instead of writing this
  15543. 13:23:55all over again we're just going to use
  15544. 13:23:57the function format
  15545. 13:24:00currency and then brackets and we're
  15546. 13:24:03going to remove the rest of the code
  15547. 13:24:05here and and close bracket and
  15548. 13:24:10save so we're basically running the same
  15549. 13:24:13code except we're using this shared
  15550. 13:24:15function that's located in another file
  15551. 13:24:18so let's open our
  15552. 13:24:20project and we can see that the price is
  15553. 13:24:23still displaying
  15554. 13:24:24correctly so now let's go back into our
  15555. 13:24:26code and we're going to share this
  15556. 13:24:28function inside amazon. JS as well so
  15557. 13:24:32let's open amazon.
  15558. 13:24:34JS and then we'll scroll to where we
  15559. 13:24:38show the price which is here and we're
  15560. 13:24:40going to use this function again so
  15561. 13:24:42first we have to import it so we'll
  15562. 13:24:44scroll up to the top and import curly
  15563. 13:24:49brackets we'll import the function
  15564. 13:24:53format
  15565. 13:24:54currency and we'll import it from single
  15566. 13:24:59quotes semicolon and then here we'll
  15567. 13:25:02locate the file money. JS so remember
  15568. 13:25:05for module if we want to start in the
  15569. 13:25:07current folder we have to type slash and
  15570. 13:25:12then we're going to go to the UIL folder
  15571. 13:25:14so utils and then a slash to go inside
  15572. 13:25:18the folder and locate the
  15573. 13:25:21file money.
  15574. 13:25:24JS and now we're going to use this
  15575. 13:25:26function so let's scroll
  15576. 13:25:29down and we're going to replace this
  15577. 13:25:31code and use format currency and
  15578. 13:25:36brackets so we can remove the rest of
  15579. 13:25:38the code here and
  15580. 13:25:41save and now let's look at our
  15581. 13:25:44project and we'll click up here to go
  15582. 13:25:47back to the
  15583. 13:25:48homepage and you can see that the prices
  15584. 13:25:50are displaying as two decimal
  15585. 13:25:53places and that's it if we look at our
  15586. 13:25:56code again we're essentially taking this
  15587. 13:25:59function and sharing it across two files
  15588. 13:26:03using modules so using modules we can re
  15589. 13:26:06use a lot of our code and reduce
  15590. 13:26:10repetition next let's work on the second
  15591. 13:26:12issue that we saw earlier so we'll go
  15592. 13:26:15back to our project and click the top
  15593. 13:26:18right to go back to the checkout page
  15594. 13:26:21and then here the delivery options are
  15595. 13:26:23shared between the two products but we
  15596. 13:26:26want each product to choose its own
  15597. 13:26:28delivery
  15598. 13:26:30options so to fix that let's go back
  15599. 13:26:33into our code and just going to resize a
  15600. 13:26:36here so we can see it side by
  15601. 13:26:39side and we'll open checkout. JS again
  15602. 13:26:43and we can close this for
  15603. 13:26:48now so this is an HTML element called a
  15604. 13:26:52radial selector and here's the code for
  15605. 13:26:55a radial selector so the way that radial
  15606. 13:26:58selectors work is that if a set of radio
  15607. 13:27:01selectors have the same name then we can
  15608. 13:27:03only select one of them so for example
  15609. 13:27:06if these three radio selectors have the
  15610. 13:27:08same name attribute then we can only
  15611. 13:27:11select one of them now if the three at
  15612. 13:27:14the top have one name and the three at
  15613. 13:27:16the bottom have a different name then we
  15614. 13:27:19can select one from the first three as
  15615. 13:27:21well as one from the second
  15616. 13:27:25three now what's the problem with the
  15617. 13:27:27radial selectors here well let's take a
  15618. 13:27:30look at the code for these selectors to
  15619. 13:27:33do that we're going to rightclick one of
  15620. 13:27:35them and then
  15621. 13:27:38inspect and then click the top left and
  15622. 13:27:41click one of these selectors to get the
  15623. 13:27:43HTML code so here we have the code for
  15624. 13:27:47the radio selector and you can see that
  15625. 13:27:49the name of the selector is delivery
  15626. 13:27:52option one and now if we click the top
  15627. 13:27:54left again and find one of these bottom
  15628. 13:27:58selectors you'll notice that these also
  15629. 13:28:00have the name delivery option one so all
  15630. 13:28:03six of these have the same name at
  15631. 13:28:06attribute that's why we can only select
  15632. 13:28:08one of these six at a time so what we
  15633. 13:28:11actually want to do is we want the first
  15634. 13:28:14three selectors to have one name and the
  15635. 13:28:17second three selectors to have a
  15636. 13:28:19different name so let's figure out how
  15637. 13:28:21to give these a different name so inside
  15638. 13:28:25the console we can see the code for this
  15639. 13:28:27selector and we have a name attribute
  15640. 13:28:30delivery D
  15641. 13:28:32option-1 so let's actually find this
  15642. 13:28:35name in our code so we'll go to our code
  15643. 13:28:38editor and contrl F or command F to
  15644. 13:28:41search for code and we'll search for
  15645. 13:28:45delivery Das
  15646. 13:28:49option-1 and if we scroll
  15647. 13:28:52down you'll notice that every time we're
  15648. 13:28:54generating these radio selectors we're
  15649. 13:28:57always using the same name delivery
  15650. 13:28:59option one so every product is going to
  15651. 13:29:02have the same name for the radio
  15652. 13:29:04selectors and all of these are going to
  15653. 13:29:06be shared so what we actually want to do
  15654. 13:29:09is instead of using the name delivery
  15655. 13:29:11option one every time each product is
  15656. 13:29:14going to use a different name for these
  15657. 13:29:17three
  15658. 13:29:17selectors so one way we can do that is
  15659. 13:29:20instead of the number one every time we
  15660. 13:29:23can replace it with the product's ID
  15661. 13:29:26because the ID is always unique for each
  15662. 13:29:28product so let's substitute using dollar
  15663. 13:29:32and curly
  15664. 13:29:33brackets the product
  15665. 13:29:37ID and then down here as well instead of
  15666. 13:29:40using the name one every time we're
  15667. 13:29:42going to
  15668. 13:29:43substitute
  15669. 13:29:46product. and
  15670. 13:29:48actually we use the name matching
  15671. 13:29:52product so we'll scroll down and just
  15672. 13:29:56update this to matching
  15673. 13:29:59product and then down here matching
  15674. 13:30:04product and and as well down here
  15675. 13:30:07instead of one every
  15676. 13:30:09time substitute the matching product. ID
  15677. 13:30:15and
  15678. 13:30:17save so now these three radial selectors
  15679. 13:30:21will have a different name attribute
  15680. 13:30:23than these three radial selectors that
  15681. 13:30:26means we can select one out of these
  15682. 13:30:28three and we can select one out of these
  15683. 13:30:33three so that's how we fix that error
  15684. 13:30:36instead of using the same name delivery
  15685. 13:30:38option one every time we're going to
  15686. 13:30:40generate a name using the product's ID
  15687. 13:30:44so let's go into get and save our
  15688. 13:30:46changes so in amazon. JS we imported
  15689. 13:30:51this shared function and we used it to
  15690. 13:30:53format the price in two decimal places
  15691. 13:30:56and then in checkout. JS we also used
  15692. 13:30:59the shared function
  15693. 13:31:01here and we fix the radio selectors
  15694. 13:31:06and then in this file
  15695. 13:31:08moneyjar
  15696. 13:31:10function so let's create a message to
  15697. 13:31:13describe our changes fix price
  15698. 13:31:17formatting and radio
  15699. 13:31:21selectors and
  15700. 13:31:28commit the last step in our process is
  15701. 13:31:30to make it
  15702. 13:31:33interactive so I'm just going to close
  15703. 13:31:35these for now
  15704. 13:31:37and going to move it to the right again
  15705. 13:31:40so we can see our
  15706. 13:31:46project and for the order summary
  15707. 13:31:49there's a few things we can make
  15708. 13:31:51interactive for now we're going to start
  15709. 13:31:53with this delete link when we click this
  15710. 13:31:56link it should remove this product from
  15711. 13:31:58the cart so we're going to learn how to
  15712. 13:32:00build
  15713. 13:32:01this so let's go back to our code and
  15714. 13:32:05first let's add event listeners to all
  15715. 13:32:08of these delete links to do that we're
  15716. 13:32:11going to find the code for these delete
  15717. 13:32:12links so we'll rightclick
  15718. 13:32:17inspect and then in here this is the
  15719. 13:32:19code so we're going to search for this
  15720. 13:32:21class in our code editor delete quantity
  15721. 13:32:26dlink so in here let's open checkout. JS
  15722. 13:32:30and we're going to contrl f or command F
  15723. 13:32:33and search for delete - quantity
  15724. 13:32:38dlink which is right
  15725. 13:32:40here next we're going to add a class to
  15726. 13:32:43these elements so we can add event
  15727. 13:32:45listeners so at the end here let's add a
  15728. 13:32:48class
  15729. 13:32:50js- delete
  15730. 13:32:52daslink and
  15731. 13:32:55save and then at the bottom we're going
  15732. 13:32:58to scroll down and then out here we're
  15733. 13:33:01going to add the event
  15734. 13:33:02listeners so first we're going to select
  15735. 13:33:05all the delete links on the page using
  15736. 13:33:09document. query selector all brackets
  15737. 13:33:15and then a string and we're going to
  15738. 13:33:17select a class using Dot and the class
  15739. 13:33:20js- delete
  15740. 13:33:24daslink and then we're going to Loop
  15741. 13:33:26through all these links so dot for each
  15742. 13:33:31brackets and a
  15743. 13:33:34function and and in here it will give us
  15744. 13:33:37the link every time and then for each of
  15745. 13:33:40these links we're going to add an event
  15746. 13:33:42listener so
  15747. 13:33:44link.
  15748. 13:33:46addevent
  15749. 13:33:47listener
  15750. 13:33:49brackets and here we're going to give a
  15751. 13:33:51string and the event we want to listen
  15752. 13:33:53for which is a
  15753. 13:33:56click and then a function that we want
  15754. 13:33:58to run when we click these links so for
  15755. 13:34:01now let's just
  15756. 13:34:04console.log string delete to make sure
  15757. 13:34:07that it's working so we'll save
  15758. 13:34:10this and then in here we click these
  15759. 13:34:13delete links and then open the console
  15760. 13:34:16right click
  15761. 13:34:19inspect
  15762. 13:34:20console we can see that it's working now
  15763. 13:34:24so now let's go back to our
  15764. 13:34:27code so when we click delete we're going
  15765. 13:34:30to do two steps the first step is we're
  15766. 13:34:33going to remove the product from the
  15767. 13:34:34cart and the second step is we're also
  15768. 13:34:37going to update our HTML and remove the
  15769. 13:34:40product from the page so let's start
  15770. 13:34:43with the first step so when we click
  15771. 13:34:45delete how do we know which product we
  15772. 13:34:48need to remove from the
  15773. 13:34:50cart so we learned how to do this for
  15774. 13:34:53the added to cart button we can attach
  15775. 13:34:55the product's ID to the link element so
  15776. 13:34:59let's scroll up again to where the
  15777. 13:35:01delete link is which is right here and
  15778. 13:35:06we're going to attach the products ID to
  15779. 13:35:08this element using a data attribute so
  15780. 13:35:12at the end here we're going to add
  15781. 13:35:15data-
  15782. 13:35:17product-id equals double quotes and then
  15783. 13:35:21here we'll
  15784. 13:35:23substitute the matching
  15785. 13:35:27product. ID and
  15786. 13:35:30save and now that we attached the
  15787. 13:35:33product ID to this element we can get it
  15788. 13:35:35in our JavaScript below so let's scroll
  15789. 13:35:40down and then here we're going to delete
  15790. 13:35:43this line and using the link element we
  15791. 13:35:47can get that product ID using link. dat
  15792. 13:35:51set to access the data attributes and
  15793. 13:35:54we're going to access product
  15794. 13:35:58ID and let's save this in a variable so
  15795. 13:36:01we can use it later so const product ID
  15796. 13:36:04equals this
  15797. 13:36:07value and let's
  15798. 13:36:09console.log this to see if it
  15799. 13:36:13works and
  15800. 13:36:15save so now if we go to the project and
  15801. 13:36:18click the delete link for the first
  15802. 13:36:20product and then look at the console
  15803. 13:36:23inspect and console it will show us the
  15804. 13:36:26products
  15805. 13:36:27ID if we click the second products
  15806. 13:36:30delete link it will show us the second
  15807. 13:36:33products ID so now now we have the ID
  15808. 13:36:36that we need to remove from the cart so
  15809. 13:36:39now how do we actually do that so before
  15810. 13:36:42we begin figuring that out instead of
  15811. 13:36:45writing all the code down here let's
  15812. 13:36:47actually create a function inside C.J to
  15813. 13:36:51do this task the reason we're going to
  15814. 13:36:54do this is because cart. JS is supposed
  15815. 13:36:57to contain all the code that's related
  15816. 13:36:59to the cart so this will help us
  15817. 13:37:02organize our code better so let's open
  15818. 13:37:04our files again again and we're going to
  15819. 13:37:07open cart.
  15820. 13:37:09JS and we're going to scroll to the
  15821. 13:37:12bottom and we're going to create a
  15822. 13:37:14function for removing a product from the
  15823. 13:37:17cart so let's type function and let's
  15824. 13:37:21name it remove from cart brackets and
  15825. 13:37:26curly
  15826. 13:37:27brackets so this function we'll take a
  15827. 13:37:30product ID which we have and remove it
  15828. 13:37:33from the cart so here let's let's create
  15829. 13:37:35a parameter for the ID product ID and
  15830. 13:37:40now in here we're going to figure out
  15831. 13:37:42how to remove it so here's one way we
  15832. 13:37:45can do this first we can create a new
  15833. 13:37:49array and then we can Loop through our
  15834. 13:37:51cart and then add each product to the
  15835. 13:37:54new array except for this product ID so
  15836. 13:37:59now let's convert these steps into code
  15837. 13:38:02so inside this function here let's
  15838. 13:38:04create a new new array using const we'll
  15839. 13:38:08call it new cart equals an empty
  15840. 13:38:13array the next step is we're going to
  15841. 13:38:15Loop through our cart so we'll type
  15842. 13:38:19cart. for each brackets and a
  15843. 13:38:24function and we'll give it a parameter
  15844. 13:38:27cart item and the last step is that
  15845. 13:38:31we're going to add each card item to the
  15846. 13:38:33new array except if it has this product
  15847. 13:38:36ID so we can do that by checking if the
  15848. 13:38:41cart
  15849. 13:38:42item. product ID is not equal to the
  15850. 13:38:48product ID that we're looking for
  15851. 13:38:50product
  15852. 13:38:52ID so if it's not equal to the product
  15853. 13:38:55we're trying to remove we're going to
  15854. 13:38:57add it to the new cart so new cart. push
  15855. 13:39:03cart item
  15856. 13:39:06so the result of this Loop is it's going
  15857. 13:39:08to contain all the cart items that don't
  15858. 13:39:11match this product ID and that's the
  15859. 13:39:13same thing as removing this from the
  15860. 13:39:16cart so now the last step is we're going
  15861. 13:39:19to take our new cart and replace the
  15862. 13:39:21cart up here so we'll scroll
  15863. 13:39:24down and then here we'll make cart equal
  15864. 13:39:28to our new
  15865. 13:39:31cart because we're reassigning this
  15866. 13:39:34variable we have to scroll up and change
  15867. 13:39:37it from const to let and
  15868. 13:39:42save so now that we created this
  15869. 13:39:45function let's use it in our checkout
  15870. 13:39:48page first we're going to use the
  15871. 13:39:50modules feature and Export this function
  15872. 13:39:54and
  15873. 13:39:56save and then inside checkout. JS we're
  15874. 13:39:59going to import this function so I'm
  15875. 13:40:01just going to move this down to the
  15876. 13:40:02bottom so we can see it side by side
  15877. 13:40:06so here so in checkout. JS we're going
  15878. 13:40:09to scroll up and we're already importing
  15879. 13:40:12from cart. JS so we're just going to
  15880. 13:40:15import one additional Thing by typing
  15881. 13:40:18comma and we're going to import this
  15882. 13:40:20function remove from cart and
  15883. 13:40:26save and then we're going to use this
  15884. 13:40:28function at the bottom so we'll scroll
  15885. 13:40:31down and then here we're going to
  15886. 13:40:36remove from cart brackets and we're
  15887. 13:40:40going to give it the product ID we want
  15888. 13:40:42to remove so product
  15889. 13:40:46ID and lastly let's just console.log the
  15890. 13:40:50cart to make sure that this works so
  15891. 13:40:52console.log the card and
  15892. 13:40:56save so now let's go into our
  15893. 13:40:59project and we're going to click this
  15894. 13:41:01delete link for the first product so
  15895. 13:41:04delete and let's open the console right
  15896. 13:41:07click inspect the console and now it's
  15897. 13:41:11going to show us that we have one
  15898. 13:41:13product remaining in the cart and this
  15899. 13:41:15product has a quantity of one which is
  15900. 13:41:18the second product here so by clicking
  15901. 13:41:20that delete link We successfully removed
  15902. 13:41:23this product from the cart so now if we
  15903. 13:41:26refresh and tried clicking the second
  15904. 13:41:29delete
  15905. 13:41:30link and look at the
  15906. 13:41:33console and then EXP expand the cart it
  15907. 13:41:36will tell us that the remaining product
  15908. 13:41:38has a quantity of two which is this
  15909. 13:41:41product so we successfully removed the
  15910. 13:41:44second product from the cart when we
  15911. 13:41:45clicked
  15912. 13:41:48delete so now let's go into get and look
  15913. 13:41:51at our changes so we'll go into this
  15914. 13:41:54section well I can close this for now
  15915. 13:41:56and we'll open git and it will tell us
  15916. 13:41:59that in C.J we change it to let and we
  15917. 13:42:03created this new remove from cart
  15918. 13:42:06function and then in checkout. JS we
  15919. 13:42:09import that function and then at the
  15920. 13:42:12bottom when we click the delete link
  15921. 13:42:15we're going to remove this product from
  15922. 13:42:17the cart now finally we can remove this
  15923. 13:42:21console.log save and we're going to put
  15924. 13:42:24these changes into git so we'll type a
  15925. 13:42:26message to describe our
  15926. 13:42:28changes delete products from cart when
  15927. 13:42:34clicking
  15928. 13:42:35delete and
  15929. 13:42:41commit the second thing we have to do
  15930. 13:42:43when we click delete is to update our
  15931. 13:42:46HTML so if we go back to our
  15932. 13:42:49project when we click delete we want to
  15933. 13:42:52remove this product from the page so one
  15934. 13:42:56way we can do this is first we're going
  15935. 13:42:59to use the Dom to get the element we
  15936. 13:43:01want to remove and then we can use a
  15937. 13:43:04method called
  15938. 13:43:05remove so every element that we get from
  15939. 13:43:08the Dom has a method called remove which
  15940. 13:43:11removes it from the page so first of all
  15941. 13:43:15how do we know which element to get from
  15942. 13:43:17the page so let's actually look at the
  15943. 13:43:20HTML for one of these products we're
  15944. 13:43:22going to
  15945. 13:43:24rightclick
  15946. 13:43:25inspect and then click the top left and
  15947. 13:43:29then click one of these products
  15948. 13:43:31here and this will give us the HTML so
  15949. 13:43:35these two products are represented by
  15950. 13:43:37these two divs here with the class
  15951. 13:43:40cart--
  15952. 13:43:41container so one way to identify which
  15953. 13:43:44product this is for is to add a special
  15954. 13:43:47class to this element and this class
  15955. 13:43:49will contain the product
  15956. 13:43:51ID and then if we go back to our code
  15957. 13:43:55and we open checkout. JS and scroll to
  15958. 13:43:59the
  15959. 13:44:01bottom when we click delete we have the
  15960. 13:44:04product ID here so we can use this to
  15961. 13:44:07select that special class that we're
  15962. 13:44:09going to add so first let's add that
  15963. 13:44:12special class to each of these elements
  15964. 13:44:15so we're going to scroll up to the
  15965. 13:44:18HTML and we're going to go to the top
  15966. 13:44:20here to the div cart--
  15967. 13:44:24container and then here we'll add a
  15968. 13:44:27special class
  15969. 13:44:29js- cart Das item Das container Das and
  15970. 13:44:35then we'll substitute the product's ID
  15971. 13:44:38so we'll type the
  15972. 13:44:40matching
  15973. 13:44:42product.
  15974. 13:44:44ID and let's put this class on a
  15975. 13:44:46separate line so the line is not too
  15976. 13:44:48long now let's
  15977. 13:44:51save and now we're going to scroll to
  15978. 13:44:53the bottom and use our code to get this
  15979. 13:44:56class so we'll scroll to the
  15980. 13:44:58[Music]
  15981. 13:45:00bottom and here we're going to use the
  15982. 13:45:02Dom as well as this product ID to get
  15983. 13:45:05that special class that we just added
  15984. 13:45:08above so here we can type
  15985. 13:45:12document. query selector brackets and
  15986. 13:45:17then inside we'll type a string and
  15987. 13:45:20we'll select the class using a DOT js-
  15988. 13:45:24cart D item D container Dash and then
  15989. 13:45:29we're going to substitute the product ID
  15990. 13:45:32so because we want to insert something
  15991. 13:45:34in here we're going to switch this to a
  15992. 13:45:36template string using
  15993. 13:45:38backtick and now we can substitute this
  15994. 13:45:42product ID into this
  15995. 13:45:44string product
  15996. 13:45:47ID so by creating the string like this
  15997. 13:45:51we're able to select the specific
  15998. 13:45:53container that we need so now let's save
  15999. 13:45:56this in a variable so we can use it
  16000. 13:45:58later so const container equals this
  16001. 13:46:03result so for now let's let's actually
  16002. 13:46:05console.log this to see if it works
  16003. 13:46:09console.log
  16004. 13:46:12container and
  16005. 13:46:14save and then we'll go back to our
  16006. 13:46:16project so just move this
  16007. 13:46:19here and then in our project if we click
  16008. 13:46:22delete on the first product and then
  16009. 13:46:25check the console right click inspect
  16010. 13:46:28and the console you can see that we're
  16011. 13:46:31getting the correct element that we
  16012. 13:46:32should delete if we go out here and
  16013. 13:46:36click this delete link and check the
  16014. 13:46:39console you can see that this is also
  16015. 13:46:42getting us the correct element that we
  16016. 13:46:44want to
  16017. 13:46:45delete so now that we have the element
  16018. 13:46:48we want to delete the next step is to
  16019. 13:46:50use the do remove method so again every
  16020. 13:46:54element we get with the Dom has a method
  16021. 13:46:57called do remove which will remove it
  16022. 13:46:59from the page so if we go back into our
  16023. 13:47:02code down here all we have to do is type
  16024. 13:47:06out the element
  16025. 13:47:08container. remove and brackets and this
  16026. 13:47:12will remove it from the page if we
  16027. 13:47:16save and try it out so we'll go in here
  16028. 13:47:20we'll click delete on the first product
  16029. 13:47:22and it will remove it from the
  16030. 13:47:24page if we click delete on the second
  16031. 13:47:27product it will also remove it from the
  16032. 13:47:30page so that's it now let's refresh to
  16033. 13:47:33bring the products back
  16034. 13:47:35and that's how we make the delete link
  16035. 13:47:37interactive and remove the products from
  16036. 13:47:39the cart as well as from the
  16037. 13:47:42page so now let's save our changes into
  16038. 13:47:44git we'll go to the git
  16039. 13:47:48section and it will tell us that in
  16040. 13:47:51checkout. JS at the top we added that
  16041. 13:47:54special class which contains the product
  16042. 13:47:57ID and if we scroll down to the
  16043. 13:48:01bottom we created the string which will
  16044. 13:48:04get us these specific element that we
  16045. 13:48:06want to remove and then we use the do
  16046. 13:48:08remove method so now let's create a
  16047. 13:48:11message to describe our
  16048. 13:48:13changes delete products from
  16049. 13:48:18HTML when clicking delete and
  16050. 13:48:28commit the last thing we're going to do
  16051. 13:48:30in this lesson is we're going to add
  16052. 13:48:32local storage to our cart so if we go
  16053. 13:48:35back to our
  16054. 13:48:36project if we delete some products in
  16055. 13:48:39our cart and then
  16056. 13:48:41refresh notice that our cart goes back
  16057. 13:48:44to what it was before and that's because
  16058. 13:48:47right now if we look at our code and we
  16059. 13:48:50open cart.
  16060. 13:48:52JS our cart is just a variable and
  16061. 13:48:56variables are reset when we refresh the
  16062. 13:48:58page or when we go to a different page
  16063. 13:49:02so again if we go back to our project
  16064. 13:49:04and we go to a different Page by
  16065. 13:49:06clicking the top left so on this
  16066. 13:49:09homepage we can add as many products as
  16067. 13:49:12we want to our cart and if we go back to
  16068. 13:49:15the checkout page our cart will just get
  16069. 13:49:18reset to whatever it is here so that's a
  16070. 13:49:22problem with just using a variable to
  16071. 13:49:24save our cart it gets reset when we
  16072. 13:49:26refresh the page or we go to a different
  16073. 13:49:29page to solve this problem we're going
  16074. 13:49:31to use local storage to save our cart
  16075. 13:49:35so first let's create a function for
  16076. 13:49:37saving the cart to local storage because
  16077. 13:49:40we might want to reuse this code so down
  16078. 13:49:43here let's create a function we'll call
  16079. 13:49:47it save to storage brackets and curler
  16080. 13:49:52brackets and we're just going to save
  16081. 13:49:54the cart to local storage using local
  16082. 13:49:58storage do set item
  16083. 13:50:02brackets and set item takes two
  16084. 13:50:05strings the first string is the name of
  16085. 13:50:08whatever we want to save so we're going
  16086. 13:50:10to save our cart so we're just going to
  16087. 13:50:13call this the cart and then the second
  16088. 13:50:15string is the data that we want to save
  16089. 13:50:19so remember that local storage can only
  16090. 13:50:21save strings so to save our cart we need
  16091. 13:50:24to convert it into a string first and we
  16092. 13:50:27can do that using
  16093. 13:50:30json.
  16094. 13:50:32stringify brackets and then inside we're
  16095. 13:50:35going to give it the cart so this will
  16096. 13:50:38convert the cart into a string and now
  16097. 13:50:41we can save it in local storage so now
  16098. 13:50:44let's use this function whenever we
  16099. 13:50:47update the cart we need to save it to
  16100. 13:50:49local storage so it doesn't get reset
  16101. 13:50:51when we refresh the page so here when we
  16102. 13:50:55add it to the cart at the bottom after
  16103. 13:50:58we update the cart we're going to use
  16104. 13:51:00that function we created above save to
  16105. 13:51:04to storage and
  16106. 13:51:07brackets and also in the function remove
  16107. 13:51:10from cart so this function is also
  16108. 13:51:13updating the cart so after we update the
  16109. 13:51:16cart we're also going to save to storage
  16110. 13:51:22and brackets and
  16111. 13:51:25save so now whenever we update the cart
  16112. 13:51:28we're going to save it in local
  16113. 13:51:31storage the last thing we need to do is
  16114. 13:51:33at the top at the beginning of our code
  16115. 13:51:36we need to get the cart from local
  16116. 13:51:38storage instead of using this default
  16117. 13:51:41value so let's add some new lines here
  16118. 13:51:43to separate the code for a bit and here
  16119. 13:51:46we're going to get the cart from local
  16120. 13:51:48storage and we can do that using local
  16121. 13:51:53storage. get item and brackets so get
  16122. 13:51:57item takes one string and that is the
  16123. 13:52:00name of what we saved earlier which is
  16124. 13:52:03the cart so we're going to to give it
  16125. 13:52:04the string
  16126. 13:52:06cart so this will give us the cart that
  16127. 13:52:08we
  16128. 13:52:09saved however remember that local
  16129. 13:52:12storage can only save strings so it will
  16130. 13:52:15give us the string version of our cart
  16131. 13:52:17and we need to convert it back into an
  16132. 13:52:19array to convert it back to an array we
  16133. 13:52:22can use the code
  16134. 13:52:24json. pars and brackets and close
  16135. 13:52:32bracket the last thing we need to do is
  16136. 13:52:34when we first use the website we might
  16137. 13:52:37not have a cart in local storage so if
  16138. 13:52:40we don't have a cart saved local storage
  16139. 13:52:42will give us null so in this situation
  16140. 13:52:46if this value is null we want to give
  16141. 13:52:48the cart a default value which is this
  16142. 13:52:51value below so to give the cart a
  16143. 13:52:54default value if it's empty we can use
  16144. 13:52:57an if statement by typing if brackets
  16145. 13:53:01and curly brackets and then here we can
  16146. 13:53:04check if not
  16147. 13:53:07cart so if we don't have a cart in local
  16148. 13:53:10storage this will give us
  16149. 13:53:12null in this case not null will become
  16150. 13:53:15truthy and we're going to run the code
  16151. 13:53:18in this if
  16152. 13:53:19statement so if the cart is empty we're
  16153. 13:53:22going to make the cart equal this
  16154. 13:53:25default value below so let's just select
  16155. 13:53:28this code and we'll right click cut and
  16156. 13:53:32then in here right click and
  16157. 13:53:35paste and remove the extra line and
  16158. 13:53:40save so now if the cart is empty we just
  16159. 13:53:43give it a default value if we go back to
  16160. 13:53:46our project and try out our code so
  16161. 13:53:49let's delete the first product and
  16162. 13:53:52refresh the page notice that the cart
  16163. 13:53:55gets saved and the first product remains
  16164. 13:53:58deleted if we go to the homepage and we
  16165. 13:54:01add some new products
  16166. 13:54:04and then go to our
  16167. 13:54:06cart notice that our cart is saved
  16168. 13:54:09between pages so by using local storage
  16169. 13:54:12we can save our cart even when we
  16170. 13:54:14refresh the page or go between different
  16171. 13:54:18pages so that's all we're going to build
  16172. 13:54:20in this lesson so to clean up let's
  16173. 13:54:23delete all these products in the
  16174. 13:54:26cart and then go to the
  16175. 13:54:28homepage and we'll add back two socks
  16176. 13:54:32and one basketball which which is the
  16177. 13:54:34default value of the
  16178. 13:54:36cart let's go back to the checkout
  16179. 13:54:39page and now we're done with this part
  16180. 13:54:42of the page so finally let's save our
  16181. 13:54:45changes into git we'll go to the git
  16182. 13:54:48section and it will tell us that inside
  16183. 13:54:51cart. JS we're now loading the cart from
  16184. 13:54:54local storage when we open the page if
  16185. 13:54:57the cart doesn't exist we're going to
  16186. 13:54:58give it a default value and we created a
  16187. 13:55:01function to save the cart into local
  16188. 13:55:04store
  16189. 13:55:04stage so now let's add a message to
  16190. 13:55:07describe our
  16191. 13:55:08changes save cart in local storage and
  16192. 13:55:19commit and that's the end of this lesson
  16193. 13:55:22in this lesson we learned about modules
  16194. 13:55:25which is a better way to organize our
  16195. 13:55:28code we created the checkout page in our
  16196. 13:55:31final project we learned about HTML link
  16197. 13:55:34elements and radio selectors we made the
  16198. 13:55:37delete link interactive and we saved the
  16199. 13:55:41cart in local
  16200. 13:55:43storage here are some exercises to help
  16201. 13:55:46you practice what we learned in this
  16202. 13:56:03lesson e
  16203. 13:56:38in this lesson we're going to learn
  16204. 13:56:39about external libraries and we're going
  16205. 13:56:42to finish the checkout page in our final
  16206. 13:56:44project an external library is basically
  16207. 13:56:47just code that is outside of our
  16208. 13:56:50project so far we've only been using
  16209. 13:56:53Code that is inside our project like
  16210. 13:56:56these
  16211. 13:56:57files well a lot of developers write
  16212. 13:57:00JavaScript code and then put their code
  16213. 13:57:02on the internet and we can actually load
  16214. 13:57:05their code from the internet into our
  16215. 13:57:08project and use it we call this code an
  16216. 13:57:11external library because it's code that
  16217. 13:57:14is outside of our project Let's do an
  16218. 13:57:17example and load our first external
  16219. 13:57:20Library we're going to go to the video
  16220. 13:57:22description and we're going to click
  16221. 13:57:24this
  16222. 13:57:29link for the hello external Library
  16223. 13:57:37and it will open up a page that contains
  16224. 13:57:40JavaScript code so I actually wrote this
  16225. 13:57:42code myself and you'll notice that this
  16226. 13:57:44is just normal JavaScript code we create
  16227. 13:57:47a function called hello and it just
  16228. 13:57:50console.log hello so this is called an
  16229. 13:57:53external Library it's code that is
  16230. 13:57:56outside of our project so the cool thing
  16231. 13:57:59is we can actually load this code from
  16232. 13:58:02the internet into our project so let's
  16233. 13:58:06give that a try we're going to go to our
  16234. 13:58:09project and we're going to open
  16235. 13:58:11checkout.
  16236. 13:58:12HTML and we're going to scroll to the
  16237. 13:58:15bottom and above the script tag we're
  16238. 13:58:18going to add another script tag to load
  16239. 13:58:21some code so
  16240. 13:58:24script and we'll give it a source
  16241. 13:58:27attribute and then in here instead of
  16242. 13:58:30using a fth like we've done before we're
  16243. 13:58:33going to give this a a URL of a website
  16244. 13:58:35that contains JavaScript code so if we
  16245. 13:58:38go back to our
  16246. 13:58:40browser and here the URL of this page is
  16247. 13:58:44up here so we're just going to copy this
  16248. 13:58:47right click copy and then paste it into
  16249. 13:58:50our
  16250. 13:58:51code right click and
  16251. 13:58:55paste so now the computer will get all
  16252. 13:58:58the JavaScript code from this URL or
  16253. 13:59:00from this
  16254. 13:59:02page so it's going to get all the code
  16255. 13:59:04from this page and load it into our
  16256. 13:59:07project like normal code so let's save
  16257. 13:59:11and let's give this a
  16258. 13:59:13try so this code creates a function
  16259. 13:59:15called hello so let's try running this
  16260. 13:59:18hello function in our project we'll go
  16261. 13:59:21to checkout. JS and then scroll to the
  16262. 13:59:25top and then here let's just run the
  16263. 13:59:28function hello so hello and brackets and
  16264. 13:59:33Save
  16265. 13:59:35now if we go back to our
  16266. 13:59:36project go to the checkout page and
  16267. 13:59:40check the console right click inspect
  16268. 13:59:44and the console you'll see that it
  16269. 13:59:46displayed the message
  16270. 13:59:48hello so that's how an external Library
  16271. 13:59:51works it's just code that is outside of
  16272. 13:59:54our project and we can load this code
  16273. 13:59:57using a script tag like this we just
  16274. 14:00:00give it the URL of where this code is
  16275. 14:00:03located
  16276. 14:00:04so there's two things you might be
  16277. 14:00:05wondering about how come we put the
  16278. 14:00:07script tag at the top so generally we
  16279. 14:00:10load external libraries first and then
  16280. 14:00:13we can use the library in our own code
  16281. 14:00:16below and the second thing is how do you
  16282. 14:00:19put code on the internet like this to
  16283. 14:00:22learn this you can check out my video on
  16284. 14:00:24how to put a website on the internet
  16285. 14:00:27it'll show you how to put any file like
  16286. 14:00:29HTML CSS and JavaScript files onto a
  16287. 14:00:33website like this
  16288. 14:00:34this next let's talk about why we use
  16289. 14:00:37external libraries external libraries
  16290. 14:00:40let us share code instead of writing all
  16291. 14:00:43the code ourselves we can use code that
  16292. 14:00:46other people wrote and this helps us
  16293. 14:00:48save time and avoid duplicating work
  16294. 14:00:52right now we're loading a very simple
  16295. 14:00:54Library so let's load a more complex
  16296. 14:00:56library to show how we can save a lot of
  16297. 14:00:58time and work let's go back to our
  16298. 14:01:02project and go to the checkout
  16299. 14:01:05page here we have a section for choosing
  16300. 14:01:08a delivery option for this product and
  16301. 14:01:11you'll notice that each option has a
  16302. 14:01:13date so this is the date that the
  16303. 14:01:15product should be delivered by so the
  16304. 14:01:18way this works is that the cheapest
  16305. 14:01:20option here takes 7 days to deliver the
  16306. 14:01:24$4.99 option takes 3 days to deliver and
  16307. 14:01:27the
  16308. 14:01:28$9.99 option takes one day to deliver so
  16309. 14:01:32to get these dates here we need to do
  16310. 14:01:35three things first we need to somehow
  16311. 14:01:37get today's date in our JavaScript
  16312. 14:01:41number two we need to do calculations
  16313. 14:01:43with the date such as ADD seven days or
  16314. 14:01:46add 3 days and number three we need to
  16315. 14:01:49display the date in an easyto read
  16316. 14:01:52format like
  16317. 14:01:53this now we could try to figure out how
  16318. 14:01:56to do this ourselves by writing our own
  16319. 14:01:58code but this is going to be a lot of
  16320. 14:02:00work instead we can use an external
  16321. 14:02:03Library to help us work with dates so in
  16322. 14:02:06this project we're going to use an
  16323. 14:02:08external Library called DJs this is a
  16324. 14:02:11popular library that helps us work with
  16325. 14:02:14dates so first let's load DJs into our
  16326. 14:02:19project we're going to go to the video
  16327. 14:02:21description and we'll click this
  16328. 14:02:28link for the DJs external Library
  16329. 14:02:35and when the page opens you'll see a
  16330. 14:02:37bunch of random code here now you might
  16331. 14:02:40think that this is some special code but
  16332. 14:02:42this is actually just regular JavaScript
  16333. 14:02:44code that has been compressed so
  16334. 14:02:47basically all the extra spaces have been
  16335. 14:02:49removed and the variable names have been
  16336. 14:02:51shortened to one
  16337. 14:02:53letter we can press JavaScript code like
  16338. 14:02:56this because we need to load this code
  16339. 14:02:58from the internet into our project by
  16340. 14:03:02compressing it it makes this code
  16341. 14:03:04smaller so it will load into our project
  16342. 14:03:06faster so this compression process is
  16343. 14:03:09called minification or we Minify the
  16344. 14:03:14code okay so this is the code for the
  16345. 14:03:17external Library DJs now let's load this
  16346. 14:03:20code into our project we'll go back to
  16347. 14:03:23our
  16348. 14:03:24code and then go to checkout.
  16349. 14:03:27HTML and then here we're going to add
  16350. 14:03:29another script tag less than script and
  16351. 14:03:33the source
  16352. 14:03:36attribute and then here we're going to
  16353. 14:03:38give it the URL of the code we want to
  16354. 14:03:41load so let's go back to the code for
  16355. 14:03:43DJs and then up here we're going to
  16356. 14:03:46rightclick and copy this
  16357. 14:03:49URL and then inside our code in the
  16358. 14:03:52source attribute we're going to
  16359. 14:03:53rightclick and paste the
  16360. 14:03:56URL and
  16361. 14:03:57save so again this will take all the
  16362. 14:04:00code that's located on this page and
  16363. 14:04:03then load it into our
  16364. 14:04:05project now let's give it a try the DJs
  16365. 14:04:09external Library creates a function
  16366. 14:04:11called
  16367. 14:04:12DJs so we'll go to checkout. JS and then
  16368. 14:04:16at the top we're going to run the JJs
  16369. 14:04:20function and
  16370. 14:04:22brackets now make sure this is all
  16371. 14:04:24lowercase cuz that's the name that the
  16372. 14:04:26library used so now let's actually
  16373. 14:04:29console.log the result of this function
  16374. 14:04:32console.log
  16375. 14:04:36and
  16376. 14:04:37save and we'll go back to our project in
  16377. 14:04:41the checkout page we're going to check
  16378. 14:04:42our
  16379. 14:04:45console and you'll see that the DJs
  16380. 14:04:47function gives us an object and this
  16381. 14:04:50object represents the current date and
  16382. 14:04:52time so we know that DJs is working and
  16383. 14:04:56that's it we successfully loaded the DJs
  16384. 14:04:59external Library into our
  16385. 14:05:02project now let's learn how to use
  16386. 14:05:05DJs external libraries usually have a
  16387. 14:05:08documentation page that shows us how to
  16388. 14:05:10use the library we can find the
  16389. 14:05:12documentation Page by searching on
  16390. 14:05:14Google the library name like DJs and
  16391. 14:05:18then documentation or we can ask an AI
  16392. 14:05:21tool how to use the library to make
  16393. 14:05:23things easier in this video I'm going to
  16394. 14:05:25show you the features that we need and
  16395. 14:05:27you can go through the documentation
  16396. 14:05:29later if you want so earlier we said we
  16397. 14:05:32needed to do three things with the date
  16398. 14:05:35first we want to get today's date number
  16399. 14:05:38two we want to do calculations with the
  16400. 14:05:40date and number three we want to display
  16401. 14:05:42the date in an easyto read format DJs
  16402. 14:05:46helps us do all of these things so first
  16403. 14:05:49let's use DJs to get today's date
  16404. 14:05:52according to the documentation to get
  16405. 14:05:54the current date and time we can just
  16406. 14:05:57use the DJs function like
  16407. 14:06:01this so if we go back to our code
  16408. 14:06:04and that's what we have here we call DJs
  16409. 14:06:07and it gives us an object that
  16410. 14:06:08represents the current date and time the
  16411. 14:06:11second thing we need to do is to do
  16412. 14:06:13calculations with the date so for the
  16413. 14:06:16delivery options here we need to
  16414. 14:06:18calculate today's date plus 7 days or
  16415. 14:06:22today's date plus 3 days so again the
  16416. 14:06:25DJs Library can help us do
  16417. 14:06:28this so let's go back to our
  16418. 14:06:31console and look at the object that we
  16419. 14:06:33get back from DJs so this object
  16420. 14:06:37represents today's date and time and
  16421. 14:06:40this object also has a method called add
  16422. 14:06:43which adds a certain amount of time to
  16423. 14:06:45this date so this is what we need for
  16424. 14:06:48the project to calculate these dates
  16425. 14:06:51here let's go to our
  16426. 14:06:53code and first let's save this result in
  16427. 14:06:57a variable so we're going to remove this
  16428. 14:07:00and create a variable const today
  16429. 14:07:04equals
  16430. 14:07:06DJs and then down here we're going to
  16431. 14:07:08use the add method so we'll type
  16432. 14:07:12today. add and
  16433. 14:07:14brackets now the add method takes two
  16434. 14:07:18parameters the first one is the number
  16435. 14:07:20of time that we want to add so if we
  16436. 14:07:23want to add seven days we're going to
  16437. 14:07:25type seven and then the second parameter
  16438. 14:07:28is the length of time that we want to
  16439. 14:07:30add so if we want to add seven days
  16440. 14:07:33we're going to give it a string days so
  16441. 14:07:37this will add seven days to today's
  16442. 14:07:41date so let's save this result in a
  16443. 14:07:43variable const delivery date equals this
  16444. 14:07:49result and we'll console.log it to make
  16445. 14:07:51sure it's working correctly
  16446. 14:07:55console.log the delivery date and
  16447. 14:07:59save now if we go back to our project
  16448. 14:08:02and open the
  16449. 14:08:04console it will give us an object that
  16450. 14:08:07represents 7 days after to date or 7
  16451. 14:08:10days after when you're watching this
  16452. 14:08:14video so that's how we use DJs to do
  16453. 14:08:16calculations with the date it gives us
  16454. 14:08:19methods like add to add a certain amount
  16455. 14:08:21of time to the date finally we're going
  16456. 14:08:25to use DJs to display this date in an
  16457. 14:08:28easyto read format like
  16458. 14:08:31this so according to the document
  16459. 14:08:34this date object has another method
  16460. 14:08:36called format which will display the
  16461. 14:08:39date in an easyto read format so let's
  16462. 14:08:43go back to our
  16463. 14:08:45code and we'll remove the
  16464. 14:08:48console.log and we'll type delivery
  16465. 14:08:52date.
  16466. 14:08:55format and then inside these brackets
  16467. 14:08:58we're going to give this method a string
  16468. 14:09:01so this string tells DJs what kind kind
  16469. 14:09:03of format we want for the date so in the
  16470. 14:09:06documentation it has a table showing us
  16471. 14:09:09some special characters we can put into
  16472. 14:09:11this string for example if we want to
  16473. 14:09:14show the day of the week like Monday or
  16474. 14:09:17Tuesday we can type four D's into the
  16475. 14:09:20string and this will give us a string
  16476. 14:09:23where this text is replaced by the
  16477. 14:09:25actual day of the
  16478. 14:09:27week if we want to add a comma and a
  16479. 14:09:30space to this format we can just add a
  16480. 14:09:32comma and space and this will show up
  16481. 14:09:35directly in the result now let's say
  16482. 14:09:37that after this we want to show the
  16483. 14:09:40month according to the documentation to
  16484. 14:09:43show the month we can type for Capital
  16485. 14:09:47M's and it will replace this with the
  16486. 14:09:50month and finally we'll type a space and
  16487. 14:09:54according to the documentation if we
  16488. 14:09:56want to insert the day of the month into
  16489. 14:09:59the string we can type a capital D
  16490. 14:10:03so this method takes this date and
  16491. 14:10:06converts it into a string with this
  16492. 14:10:08format so now let's console.log this to
  16493. 14:10:11see what it looks like well
  16494. 14:10:15console.log and
  16495. 14:10:17brackets and
  16496. 14:10:19save and then go into our project and
  16497. 14:10:22open the
  16498. 14:10:24console and now you should see the date
  16499. 14:10:277 days after today but displayed in an
  16500. 14:10:30easyto read format
  16501. 14:10:33so that's how we use the DJs external
  16502. 14:10:36library to help us work with dates and
  16503. 14:10:39as you can see it saves us a lot of time
  16504. 14:10:41and work we don't have to figure out how
  16505. 14:10:43to work with dates ourselves we can just
  16506. 14:10:46use someone else's code to solve this
  16507. 14:10:49problem so a best practice in
  16508. 14:10:51programming is when we need to do
  16509. 14:10:53something complicated try to find an
  16510. 14:10:55external Library first before writing
  16511. 14:10:58the code
  16512. 14:10:59ourselves to find external libraries we
  16513. 14:11:03can you can search in Google JavaScript
  16514. 14:11:05and then what kind of Library we're
  16515. 14:11:07looking for like date
  16516. 14:11:10Library we can also use an AI tool to
  16517. 14:11:13help us find external
  16518. 14:11:16libraries next we're going to learn how
  16519. 14:11:18to use external libraries and JavaScript
  16520. 14:11:21modules together right now if we look at
  16521. 14:11:24our code and go to checkout.
  16522. 14:11:28HTML we're loading these libraries using
  16523. 14:11:31script tags the problem with script tags
  16524. 14:11:34is that it runs this code directly on
  16525. 14:11:36the page and this might cause naming
  16526. 14:11:39conflicts for example the hello Library
  16527. 14:11:42creates a function called hello so if
  16528. 14:11:45something else is called hello in any of
  16529. 14:11:47our other code it might cause a naming
  16530. 14:11:50conflict a better way to load external
  16531. 14:11:53libraries is to use JavaScript modules
  16532. 14:11:56because a module will contain this code
  16533. 14:11:59inside a file and any variables that are
  16534. 14:12:02created inside inside the file will not
  16535. 14:12:04conflict with anything outside of the
  16536. 14:12:08file to use external libraries and
  16537. 14:12:11JavaScript modules together we're going
  16538. 14:12:13to use a special version of the library
  16539. 14:12:16called an esm version esm stands for
  16540. 14:12:20ecmascript modules ecmascript is just
  16541. 14:12:23another name for JavaScript so the esm
  16542. 14:12:26version of a library is just a version
  16543. 14:12:28that works with JavaScript
  16544. 14:12:31modules so let's do an example we're
  16545. 14:12:34going to go to the video description and
  16546. 14:12:37we're going to open the esm version of
  16547. 14:12:48hello.js so you can see that it's the
  16548. 14:12:50exact same code as before we're creating
  16549. 14:12:53a function called hello except this time
  16550. 14:12:56I just added the word export in front of
  16551. 14:12:58function so this turns the library into
  16552. 14:13:01an esm version and now it will work with
  16553. 14:13:04JavaScript modules so let's use the esm
  16554. 14:13:08version in our code we're going to go
  16555. 14:13:10back to our
  16556. 14:13:11code and we're not going to use script
  16557. 14:13:14tags to load this anymore so let's
  16558. 14:13:16delete
  16559. 14:13:17this and
  16560. 14:13:19save and then in checkout. JS at the top
  16561. 14:13:24we're going to use an import to import
  16562. 14:13:26this function into our code so we'll
  16563. 14:13:29type import curly brackets we'll import
  16564. 14:13:33the function
  16565. 14:13:35hello and we're going to import it from
  16566. 14:13:38a string and then this time instead of
  16567. 14:13:42giving it a file path like before we're
  16568. 14:13:44going to give it a URL that contains our
  16569. 14:13:47code so on the web page we're going to
  16570. 14:13:51rightclick and copy the URL at the top
  16571. 14:13:54and then inside our code in here we're
  16572. 14:13:57going to rightclick and
  16573. 14:13:59paste so this import works the same way
  16574. 14:14:02as before before except now the codee is
  16575. 14:14:05coming from the internet rather than one
  16576. 14:14:07of our files so let's
  16577. 14:14:10save and then go back to our project and
  16578. 14:14:14check the
  16579. 14:14:15console and you can see that everything
  16580. 14:14:17is still
  16581. 14:14:19working so that's how we use external
  16582. 14:14:21libraries and JavaScript modules
  16583. 14:14:23together we just load the esm version of
  16584. 14:14:27the library and we use import as normal
  16585. 14:14:30except this time we're importing from
  16586. 14:14:32code that is on the
  16587. 14:14:35internet next let's use DJs with
  16588. 14:14:38JavaScript modules to do that we're
  16589. 14:14:41going to load the esm version of DJs so
  16590. 14:14:44we're going to go to the video
  16591. 14:14:45description and click this link for the
  16592. 14:14:48DJs esm
  16593. 14:14:58version and now we're going to load this
  16594. 14:15:00code into our project so let's go back
  16595. 14:15:03into our codee and first let's open
  16596. 14:15:07checkout.
  16597. 14:15:08HTML and we're going to remove this
  16598. 14:15:10script tag because we're going to load
  16599. 14:15:12this with JavaScript modules so let's
  16600. 14:15:15save and then go into checkout. JS and
  16601. 14:15:19then at the top we're going to import
  16602. 14:15:21from the DJs esm version so we'll
  16603. 14:15:25import and we're going to use a slightly
  16604. 14:15:28different syntax we're just going to
  16605. 14:15:30type
  16606. 14:15:31DJs without the curly brackets and then
  16607. 14:15:35we'll type from a
  16608. 14:15:38string and we're going to give it the
  16609. 14:15:40URL that contains the code so we'll go
  16610. 14:15:43to our
  16611. 14:15:44browser and then click the URL at the
  16612. 14:15:46top we're going to right click
  16613. 14:15:50copy and then in our code inside these
  16614. 14:15:53quotes rightclick and
  16615. 14:15:56paste and
  16616. 14:15:59save so now let's open our project and
  16617. 14:16:02take a look at the the console to make
  16618. 14:16:03sure it works so we'll right click
  16619. 14:16:07inspect the
  16620. 14:16:09console and everything is still working
  16621. 14:16:12so that's how we use the DJs external
  16622. 14:16:15library with JavaScript modules so
  16623. 14:16:18before we continue we're going to learn
  16624. 14:16:20about this new syntax that we used here
  16625. 14:16:23so this syntax is called a default
  16626. 14:16:26export a default export is another way
  16627. 14:16:29of exporting something from a file we
  16628. 14:16:32can use it when we only want to export
  16629. 14:16:34one thing from a file and it makes the
  16630. 14:16:37syntax a little bit cleaner because we
  16631. 14:16:39don't have to type the curly brackets so
  16632. 14:16:42let's do an example of a default export
  16633. 14:16:44to see how it works we're going to open
  16634. 14:16:47the utils folder and open money.
  16635. 14:16:50JS and at the bottom we're going to
  16636. 14:16:53create a default export by typing
  16637. 14:16:57export default and then whatever we want
  16638. 14:17:00to export so let's say we want to export
  16639. 14:17:03this function here so we just type the
  16640. 14:17:05name of the function format
  16641. 14:17:09currency and
  16642. 14:17:11save and now when we import this we can
  16643. 14:17:14just import the name without the curly
  16644. 14:17:17brackets so the syntax is a little bit
  16645. 14:17:20cleaner now each file can only have one
  16646. 14:17:23default export so if we only want to
  16647. 14:17:25export one thing from a file this is
  16648. 14:17:28another syntax that we can
  16649. 14:17:31use so some people like using default
  16650. 14:17:34exports because they feel that exporting
  16651. 14:17:36only one thing from a file makes the
  16652. 14:17:38code cleaner however in your own code
  16653. 14:17:41it's up to you which version of export
  16654. 14:17:44you want to
  16655. 14:17:45use let's go back to checkout.
  16656. 14:17:49JS and then here the DJs Library only
  16657. 14:17:53exports one thing which is the DJs
  16658. 14:17:56function so they chose to use a default
  16659. 14:17:59export instead of a normal export that
  16660. 14:18:01we were using before
  16661. 14:18:03by the way the syntax up here with the
  16662. 14:18:05curly brackets is called a named
  16663. 14:18:08export so when we use external libraries
  16664. 14:18:11in the future some libraries will use
  16665. 14:18:14named exports like this and other
  16666. 14:18:16libraries will use a default export like
  16667. 14:18:19this so it's useful to know both
  16668. 14:18:22syntaxes so that's how we use external
  16669. 14:18:24libraries and JavaScript modules
  16670. 14:18:26together we just import from the esm
  16671. 14:18:29version of the library now keep in mind
  16672. 14:18:32that not every library has an esm
  16673. 14:18:34version so for some libraries we still
  16674. 14:18:37have to use a script
  16675. 14:18:39tag okay now that we learned external
  16676. 14:18:41libraries let's do some practical
  16677. 14:18:44examples we're going to go back to the
  16678. 14:18:46checkout page and we're going to use DJs
  16679. 14:18:49to create the delivery options here so
  16680. 14:18:52the way this works is that we can select
  16681. 14:18:54one of three delivery options when we
  16682. 14:18:57click an option it should change the
  16683. 14:18:59delivery date up here and it should
  16684. 14:19:02change the prices on the right side so
  16685. 14:19:05let's get started as always we're going
  16686. 14:19:07to do three steps first we're going to
  16687. 14:19:10save the data and then we're going to
  16688. 14:19:12generate the HTML and finally we're
  16689. 14:19:15going to make it interactive so first
  16690. 14:19:18what data do we need to save for this
  16691. 14:19:20section well each delivery option has a
  16692. 14:19:23delivery time like 7 days or 3 days and
  16693. 14:19:27each option also has a
  16694. 14:19:29price so right now here's what our cart
  16695. 14:19:33looks like now we could save the
  16696. 14:19:35delivery option details inside the cart
  16697. 14:19:38like this however notice that this data
  16698. 14:19:41gets duplicated for each product in the
  16699. 14:19:43cart instead we're going to save the
  16700. 14:19:46delivery options
  16701. 14:19:47separately and then just save an ID that
  16702. 14:19:50points to the delivery
  16703. 14:19:53option as a reminder this technique is
  16704. 14:19:56called normalizing the data we save the
  16705. 14:19:59delivery options separately and then we
  16706. 14:20:01just save an ID
  16707. 14:20:03that points to the full delivery
  16708. 14:20:06option so let's create the data for the
  16709. 14:20:08delivery options we'll go into our code
  16710. 14:20:12and then in the data folder let's create
  16711. 14:20:15a new file for this and we're going to
  16712. 14:20:17name this file delivery options. JS and
  16713. 14:20:22press
  16714. 14:20:23enter and in here we'll create a list of
  16715. 14:20:26delivery options that we can pick from
  16716. 14:20:29we'll create a variable const delivery
  16717. 14:20:33options equals and array because we want
  16718. 14:20:36a list and then each delivery option is
  16719. 14:20:39going to have multiple values like the
  16720. 14:20:42time to deliver as well as the price so
  16721. 14:20:45we're going to make each of them an
  16722. 14:20:46object to group these values together so
  16723. 14:20:50I'm going to rearrange my windows here
  16724. 14:20:52so that we can see the data that we're
  16725. 14:20:53trying to save and we can actually close
  16726. 14:20:56these tabs for now
  16727. 14:21:03so here let's save the data for the
  16728. 14:21:05first delivery
  16729. 14:21:06option first let's give it an ID
  16730. 14:21:10property because as we mentioned we're
  16731. 14:21:12going to save the ID in the cart and
  16732. 14:21:14then we're going to use that ID to get
  16733. 14:21:16the full delivery option here so to keep
  16734. 14:21:20things simple let's just give it a
  16735. 14:21:22string and an ID of
  16736. 14:21:24one and then we're going to give it
  16737. 14:21:26another property and we're going to save
  16738. 14:21:28the delivery time so for the first
  16739. 14:21:31option it's going to take seven days to
  16740. 14:21:33deliver so let's create a property
  16741. 14:21:37delivery days colon 7 and comma and
  16742. 14:21:42we're also going to save the price so
  16743. 14:21:45this one is free so we're going to save
  16744. 14:21:47the price cents and zero remember that a
  16745. 14:21:52best practice when calculating money is
  16746. 14:21:54to save it in
  16747. 14:21:56cents and next let's save the data for
  16748. 14:21:59the second delivery option here we'll
  16749. 14:22:01type a comma and another object and
  16750. 14:22:04let's give it an ID of two this time and
  16751. 14:22:09this one is going to take three days to
  16752. 14:22:11deliver so we'll type delivery days
  16753. 14:22:15colon 3 and we'll give it a price cents
  16754. 14:22:19and this one is
  16755. 14:22:21$4.99 so
  16756. 14:22:23$4.99 and then finally we're going to
  16757. 14:22:26save the data for the third delivery
  16758. 14:22:27option comma another object ID of three
  16759. 14:22:33and this one is going to take one day to
  16760. 14:22:35deliver so we'll type delivery days
  16761. 14:22:39colon 1 comma and the price sense is
  16762. 14:22:43$9.99 so price sense 999 and
  16763. 14:22:50save so now that we've created our
  16764. 14:22:52delivery options for each product in the
  16765. 14:22:55cart we're going to save the ID of the
  16766. 14:22:58delivery option that's selected so for
  16767. 14:23:00example here we selected the first First
  16768. 14:23:02Option we're going to save an ID of one
  16769. 14:23:05in our cart so let's go back into our
  16770. 14:23:08code and we're going to open the
  16771. 14:23:14cart so here in the default cart we're
  16772. 14:23:17going to save the delivery option ID
  16773. 14:23:23colon and let's say that the first
  16774. 14:23:25product selected the first delivery
  16775. 14:23:27option so we're going to save the ID one
  16776. 14:23:31and then for the second product we're
  16777. 14:23:34also going to save a delivery option ID
  16778. 14:23:38colon and then let's say that for the
  16779. 14:23:40second product we chose the second
  16780. 14:23:42delivery option here so here we're going
  16781. 14:23:45to give it an ID of
  16782. 14:23:48two next when we add a product to the
  16783. 14:23:51cart we also need to give it a delivery
  16784. 14:23:53option ID so if we scroll down to the
  16785. 14:23:56add to cart function here we're adding a
  16786. 14:23:59product to the cart so when we add a new
  16787. 14:24:02product we also want to give it a
  16788. 14:24:03delivery option ID so we'll type
  16789. 14:24:06delivery option ID colon and for new
  16790. 14:24:11products let's give it a default
  16791. 14:24:13delivery option of one so for new
  16792. 14:24:16products to select the first one and now
  16793. 14:24:19let's
  16794. 14:24:20save and lastly we might have a cart
  16795. 14:24:23that is saved in local storage so we'll
  16796. 14:24:26need to add the delivery option ID to
  16797. 14:24:29local storage as well but to keep things
  16798. 14:24:31simple let's just do a shortcut we're
  16799. 14:24:34going to delete the cart in local
  16800. 14:24:36storage and then it will go back to
  16801. 14:24:38using this default value which has the
  16802. 14:24:40delivery option
  16803. 14:24:42IDs so to delete the cart from local
  16804. 14:24:44storage we're going to go into our page
  16805. 14:24:47and open the console right click
  16806. 14:24:51inspect the
  16807. 14:24:53console and we're going to type local
  16808. 14:24:58storage. remove item brackets and the
  16809. 14:25:02cart and press
  16810. 14:25:05enter so this will erase the cart from
  16811. 14:25:07local storage and it will use those
  16812. 14:25:09default values so now let's go to our
  16813. 14:25:12page and
  16814. 14:25:14refresh and it should be using the
  16815. 14:25:16default cart so that's how we save the
  16816. 14:25:19data for the delivery
  16817. 14:25:22options by the way in the rest of this
  16818. 14:25:25course if you get an error in your
  16819. 14:25:27project but your code looks correct you
  16820. 14:25:30may have some bad data saved in local
  16821. 14:25:33storage to fix this you can try running
  16822. 14:25:37local storage. CLE in your console to
  16823. 14:25:40remove everything from local
  16824. 14:25:42storage and then refresh the
  16825. 14:25:48page the next step is to generate the
  16826. 14:25:52HTML first let's find the HTML for the
  16827. 14:25:55delivery options here to do that we can
  16828. 14:25:58rightclick this part of the page and
  16829. 14:26:01inspect
  16830. 14:26:03and this will give us the HTML for this
  16831. 14:26:05part so we're going to find the element
  16832. 14:26:08that represents this part of the page
  16833. 14:26:10and that's this one right here it's a
  16834. 14:26:12div with the class delivery Das options
  16835. 14:26:15and to find this in our code we're going
  16836. 14:26:17to search for this class so let's go
  16837. 14:26:19back to our
  16838. 14:26:21code and then we're going to open
  16839. 14:26:23checkout. JS which is right
  16840. 14:26:26here and we'll press crlf or command F
  16841. 14:26:30to find code and we're going to look for
  16842. 14:26:32the class delivery Das
  16843. 14:26:35options and it will give us this code
  16844. 14:26:38right here so this is the HTML for the
  16845. 14:26:41delivery
  16846. 14:26:42options so now instead of writing this
  16847. 14:26:45HTML directly we're going to generate
  16848. 14:26:47this with
  16849. 14:26:48JavaScript to keep our code organized
  16850. 14:26:51let's create a function for generating
  16851. 14:26:53this
  16852. 14:26:54HTML we'll scroll to the
  16853. 14:26:57bottom and then here we'll create a
  16854. 14:27:01function and let's call it delivery
  16855. 14:27:04options
  16856. 14:27:06HTML brackets and curly
  16857. 14:27:10brackets and now inside here we're going
  16858. 14:27:12to do three steps first we're going to
  16859. 14:27:15Loop through the delivery options we
  16860. 14:27:17created earlier and then for each option
  16861. 14:27:20we're going to generate some HTML and
  16862. 14:27:22then finally we're going to combine all
  16863. 14:27:24the HTML together so first let's import
  16864. 14:27:28the delivery options at the top so we'll
  16865. 14:27:30scroll up and we're going to
  16866. 14:27:34import curly brackets from quotes and
  16867. 14:27:39we're going to locate the delivery
  16868. 14:27:40options file which is inside the data
  16869. 14:27:43folder so first we need to get out of
  16870. 14:27:45the scripts folder we'll type dot do
  16871. 14:27:47slash and then go into the data folder
  16872. 14:27:51slash delivery options. JS and we're
  16873. 14:27:55going to import the delivery
  16874. 14:27:59options now one thing I forgot to do is
  16875. 14:28:02that inside these delivery options we
  16876. 14:28:04need to make sure that we export this
  16877. 14:28:06variable so we're going to type export
  16878. 14:28:10and
  16879. 14:28:11save and we'll go back to checkout. JS
  16880. 14:28:15and then here we're going to Loop
  16881. 14:28:17through the delivery options below so
  16882. 14:28:19we'll scroll back
  16883. 14:28:21down to this new
  16884. 14:28:24function and type delivery options. for
  16885. 14:28:30each brackets and a function
  16886. 14:28:35inside and we'll name the parameter
  16887. 14:28:38delivery
  16888. 14:28:40option so for each delivery option we're
  16889. 14:28:43going to generate some HTML so let's use
  16890. 14:28:46a template string to save the
  16891. 14:28:48HTML and then we're going to scroll up
  16892. 14:28:51and we're going to find the class
  16893. 14:28:53delivery Das option so this is the HTML
  16894. 14:28:57for one delivery option so we're just
  16895. 14:28:59going to copy this HTML into our Java
  16896. 14:29:02JavaScript below so we'll right click
  16897. 14:29:06copy and then down here inside the
  16898. 14:29:10string give some new lines and
  16899. 14:29:13rightclick and
  16900. 14:29:15paste and if we need to reformat we can
  16901. 14:29:18select these lines and press
  16902. 14:29:26tab okay now when we're generating the
  16903. 14:29:29HTML instead of using the same date
  16904. 14:29:31every time we're going to calculate the
  16905. 14:29:34date using DJs and then insert it into
  16906. 14:29:38here to calculate the date we're going
  16907. 14:29:40to get today's date first using
  16908. 14:29:43DJs so up here let's create a variable
  16909. 14:29:47const today
  16910. 14:29:49equals and according to the
  16911. 14:29:51documentation to get today's date we can
  16912. 14:29:54just call the DJs function so we'll type
  16913. 14:29:58DJs and
  16914. 14:30:00brackets next
  16915. 14:30:02each delivery option takes a certain
  16916. 14:30:04amount of days to deliver so we need to
  16917. 14:30:07add that to today's date to do that we
  16918. 14:30:10have a method called add So Below this
  16919. 14:30:14let's create a variable to save the
  16920. 14:30:16result const delivery date
  16921. 14:30:21equals
  16922. 14:30:23today. add
  16923. 14:30:27brackets and we're going to give it two
  16924. 14:30:29parameters the first parameter is how
  16925. 14:30:31many dat days we want to add and this is
  16926. 14:30:34saved in the delivery option so here
  16927. 14:30:37we'll give it the delivery option dot
  16928. 14:30:41delivery days
  16929. 14:30:44comma and then we'll give it the length
  16930. 14:30:46of time we want to add which is a string
  16931. 14:30:50days now that we calculated the correct
  16932. 14:30:53delivery date we need to display it in
  16933. 14:30:56an easyto read format and again DJs has
  16934. 14:31:00a method called format that Let's us do
  16935. 14:31:03this so below let's create a variable
  16936. 14:31:06const and we'll call it date string and
  16937. 14:31:10make it equal to delivery
  16938. 14:31:14date.
  16939. 14:31:15format brackets and will give it a
  16940. 14:31:19string so the format we want is we want
  16941. 14:31:23to insert the day of the week so
  16942. 14:31:26according to the documentation we can
  16943. 14:31:28use
  16944. 14:31:294ds and then a comma and a space and
  16945. 14:31:33then we want to insert the month and
  16946. 14:31:35according to the documentation that's 4
  16947. 14:31:38Capital
  16948. 14:31:39M's and finally we're going to insert
  16949. 14:31:41the day of month which is capital
  16950. 14:31:44D so now that we calculated this date
  16951. 14:31:47string we're going to insert it into the
  16952. 14:31:50HTML instead of using the same date
  16953. 14:31:52every time so we'll remove this and then
  16954. 14:31:56insert the date
  16955. 14:31:59string next let's insert the price
  16956. 14:32:02because it's going to be different
  16957. 14:32:03prices every time so at the
  16958. 14:32:06top let's create a variable const and
  16959. 14:32:10we'll call it price string and make it
  16960. 14:32:14equals and remember each delivery option
  16961. 14:32:17has a property price sense so if the
  16962. 14:32:19price ense is zero we want to display
  16963. 14:32:22the text free if the price sense is not
  16964. 14:32:25zero we want to display the price as
  16965. 14:32:27dollars with a
  16966. 14:32:29dash so to do that let's practice using
  16967. 14:32:32a Turner operator so first we'll check
  16968. 14:32:35if the price sense is zero so we'll type
  16969. 14:32:38delivery option. price sense is triple
  16970. 14:32:44equals to
  16971. 14:32:45Z and then we'll type a new line and
  16972. 14:32:48question mark and also a new line and
  16973. 14:32:51colon so remember the way that a Turner
  16974. 14:32:54operator works is that if the first part
  16975. 14:32:57returns true the value is whatever is
  16976. 14:33:00after this question Mark if the first
  16977. 14:33:03part is false then the value is whatever
  16978. 14:33:05is after the colon so it's sort of like
  16979. 14:33:08an if statement except we can save the
  16980. 14:33:10result in a
  16981. 14:33:12variable so if the price sense is zero
  16982. 14:33:15that means it's free so we want to
  16983. 14:33:17display the text free so we'll create a
  16984. 14:33:20string
  16985. 14:33:22free and if the price sense is not zero
  16986. 14:33:25we want to display the price as dollars
  16987. 14:33:29so here we're going to type a template
  16988. 14:33:31string a dollar sign and we're going to
  16989. 14:33:34insert the price sents as
  16990. 14:33:37dollars so here we'll type delivery
  16991. 14:33:40option. price sense and to display this
  16992. 14:33:44as dollars we already have a function
  16993. 14:33:46for this called format currency so at
  16994. 14:33:50the front we're just going to type
  16995. 14:33:52format
  16996. 14:33:54currency Open Bracket and close
  16997. 14:33:58bracket and then at the end let's add a
  16998. 14:34:00dash so here we'll add space and dash
  16999. 14:34:05and
  17000. 14:34:06semicolon so that's how we create the
  17001. 14:34:09price string for each delivery option so
  17002. 14:34:12now let's substitute this into the HTML
  17003. 14:34:15so we'll remove this and substitute the
  17004. 14:34:20price string and
  17005. 14:34:25save so now instead of using these same
  17006. 14:34:28values every time we're substituting the
  17007. 14:34:30values in into the
  17008. 14:34:32HTML finally let's combine all this HTML
  17009. 14:34:36together so we're going to scroll up to
  17010. 14:34:38the top and outside of this loop we're
  17011. 14:34:41going to create a variable to save the
  17012. 14:34:43result so let HTML equals empty string
  17013. 14:34:47to start and then every time we Loop
  17014. 14:34:50through we're going to add this HTML to
  17015. 14:34:53this result so here we're going to type
  17016. 14:34:56HTML plus
  17017. 14:34:58equals and finally after we've created
  17018. 14:35:01this HTML we're going to return it from
  17019. 14:35:04this function so at the bottom we're
  17020. 14:35:07just going to return the
  17021. 14:35:09HTML and
  17022. 14:35:12save now that we generated the HTML for
  17023. 14:35:15the delivery options let's insert this
  17024. 14:35:18into the HTML
  17025. 14:35:21above so instead of creating this HTML
  17026. 14:35:23directly we're going to delete it
  17027. 14:35:32and then here we're going to insert and
  17028. 14:35:35we're going to run this function to
  17029. 14:35:37generate it so here we're going to type
  17030. 14:35:40delivery options HTML and
  17031. 14:35:45brackets now before we save one thing
  17032. 14:35:48that we're missing in this function is
  17033. 14:35:50that if we scroll down here we're using
  17034. 14:35:53a variable called matching product but
  17035. 14:35:56this variable is not accessible inside
  17036. 14:35:59this function so let's actually pass the
  17037. 14:36:01matching product into here so at the top
  17038. 14:36:05we're going to add a parameter matching
  17039. 14:36:09product and then when we call this
  17040. 14:36:11function up here we're going to pass in
  17041. 14:36:14the matching product so matching product
  17042. 14:36:19so now let's
  17043. 14:36:20save and check our page and now we're
  17044. 14:36:24generating the HTML for these delivery
  17045. 14:36:27options and you'll notice that your
  17046. 14:36:29delivery dates here will be based on
  17047. 14:36:31today's date or when you're watching
  17048. 14:36:34this video so this will be 7 days after
  17049. 14:36:37today or 7 days after when you're
  17050. 14:36:39watching this video this will be 3 days
  17051. 14:36:42after today or 3 days after when you're
  17052. 14:36:45watching this video and this will be one
  17053. 14:36:47day after
  17054. 14:36:49today now one thing we need to fix here
  17055. 14:36:52is that these selectors are empty
  17056. 14:36:54however we want the delivery option that
  17057. 14:36:57is saved in the cart to be checked to do
  17058. 14:37:00that let's go back to our
  17059. 14:37:02code and in order to make one of these
  17060. 14:37:05selectors selected we're going to scroll
  17061. 14:37:08down to the input
  17062. 14:37:11element and we're going to add an
  17063. 14:37:13attribute called
  17064. 14:37:16checked and this attribute makes a
  17065. 14:37:19selector
  17066. 14:37:20checked however we don't want all of
  17067. 14:37:22these delivery options to be checked we
  17068. 14:37:25only want it to be checked if it matches
  17069. 14:37:27the delivery option ID that is saved in
  17070. 14:37:30the cart so let's write some code up
  17071. 14:37:32here to figure out which delivery option
  17072. 14:37:35should be checked we're going to create
  17073. 14:37:37a variable const is
  17074. 14:37:41checked
  17075. 14:37:43equals and we only want it to be checked
  17076. 14:37:46if this delivery options ID matches the
  17077. 14:37:49delivery option ID in the cart so we'll
  17078. 14:37:53test if delivery option. ID is equal to
  17079. 14:37:59the cart item
  17080. 14:38:02do delivery option
  17081. 14:38:06ID now inside this function we actually
  17082. 14:38:09don't have access to cart item so let's
  17083. 14:38:12actually pass it in as a parameter cart
  17084. 14:38:16item up here and then when we call the
  17085. 14:38:19function this outer function has access
  17086. 14:38:22to the cart item so we're going to give
  17087. 14:38:24it the cart item so we're going to put
  17088. 14:38:28it into this function and then we can
  17089. 14:38:30use it down here
  17090. 14:38:32so now that we have this
  17091. 14:38:34variable instead of being checked all
  17092. 14:38:37the time we're going to remove this and
  17093. 14:38:40insert and we're going to use a tary
  17094. 14:38:42operator here is
  17095. 14:38:46checked question mark and colon so we
  17096. 14:38:50can also use a ternary operator when
  17097. 14:38:52we're inserting into a string so again
  17098. 14:38:55if this is true it's going to get the
  17099. 14:38:58value after the question mark So if it
  17100. 14:39:00is checked we're going to give it the
  17101. 14:39:02value
  17102. 14:39:05checked if this is false we're going to
  17103. 14:39:08give it the value of empty string so
  17104. 14:39:11it's the same thing as if this attribute
  17105. 14:39:13didn't exist so now if we
  17106. 14:39:16save you'll notice that the correct
  17107. 14:39:18delivery option is checked for each
  17108. 14:39:20product the first product we saved
  17109. 14:39:23delivery option one and the second
  17110. 14:39:25product we say delivery option
  17111. 14:39:28two finally let's replace this date at
  17112. 14:39:31the top with the delivery date that we
  17113. 14:39:33selected
  17114. 14:39:35here we'll go back to our
  17115. 14:39:37code and we'll scroll to the
  17116. 14:39:41top until we see this date in our code
  17117. 14:39:44here so now we're going to calculate
  17118. 14:39:47this date using the delivery option that
  17119. 14:39:49we picked so remember that inside the
  17120. 14:39:52cart we only saved the delivery option
  17121. 14:39:54ID so let's use this to get the full
  17122. 14:39:57delivery
  17123. 14:39:58option so at the top here let's get the
  17124. 14:40:01delivery option ID out of the cart const
  17125. 14:40:06delivery option ID equals cart item.
  17126. 14:40:11delivery option
  17127. 14:40:16ID next we're going to use this ID to
  17128. 14:40:19find the full delivery option now we did
  17129. 14:40:22something similar with the product ID we
  17130. 14:40:25Loop through the products found a
  17131. 14:40:26matching ID and then saved it in a
  17132. 14:40:28variable up here so we'll do the same
  17133. 14:40:31let's create a variable to store the
  17134. 14:40:33result let
  17135. 14:40:35delivery option and then we're going to
  17136. 14:40:38Loop through the delivery options so
  17137. 14:40:41delivery options. for each brackets and
  17138. 14:40:46a
  17139. 14:40:47function and let's name the parameter
  17140. 14:40:51option because we use the name delivery
  17141. 14:40:53option up here so in here we're going to
  17142. 14:40:56look for a matching ID so if this option
  17143. 14:41:01ID is equal to the delivery option ID
  17144. 14:41:09here we're going to save this inside
  17145. 14:41:12this variable so delivery option equals
  17146. 14:41:16option so now we have the full delivery
  17147. 14:41:19option in our code and we can use it to
  17148. 14:41:21get the delivery days property and then
  17149. 14:41:24calculate the day that we need
  17150. 14:41:26here to calculate this state we already
  17151. 14:41:29wrote some code for this at the bottom
  17152. 14:41:32if we scroll down in our
  17153. 14:41:34code when we created the delivery
  17154. 14:41:37options we calculated the date for each
  17155. 14:41:39delivery option so we can reuse this
  17156. 14:41:42code above so let's select this code and
  17157. 14:41:46then right
  17158. 14:41:47click
  17159. 14:41:49copy and then at the
  17160. 14:41:52top after we get the delivery option
  17161. 14:41:56we're just going to rightclick and paste
  17162. 14:41:58that code again
  17163. 14:42:02and if we need to we can reformat by
  17164. 14:42:04selecting these lines and press shift
  17165. 14:42:07tab so this code will take the delivery
  17166. 14:42:10option that we selected and calculate
  17167. 14:42:12the delivery date that we need to show
  17168. 14:42:14down here and now instead of using the
  17169. 14:42:18same date every time we're going to
  17170. 14:42:20remove this and insert the date string
  17171. 14:42:23that we created Above So date string and
  17172. 14:42:28save so now this date string up here
  17173. 14:42:32will match the delivery date that we
  17174. 14:42:34chose on the right side so that's how we
  17175. 14:42:38generate the HTML for the delivery
  17176. 14:42:40options using the DJs external Library
  17177. 14:42:44so now let's take a look at our changes
  17178. 14:42:46in git we'll go to our code and open the
  17179. 14:42:50git
  17180. 14:42:54section and here it'll tell us in cart.
  17181. 14:42:57JS we added delivery option IDs to the
  17182. 14:43:00cart
  17183. 14:43:01as well as when we add to the
  17184. 14:43:03cart and then in this file we created
  17185. 14:43:06the list of delivery
  17186. 14:43:11options and in checkout. JS instead of
  17187. 14:43:14writing this HTML directly we generated
  17188. 14:43:16it and then in money. JS we learned
  17189. 14:43:20about default exports so let's type a
  17190. 14:43:23message to describe our changes save
  17191. 14:43:26data and generate HTML for for delivery
  17192. 14:43:33options and
  17193. 14:43:39commit the last step is to make it
  17194. 14:43:43interactive so when we click a delivery
  17195. 14:43:46option we need to do two things first we
  17196. 14:43:49need to update the delivery option ID in
  17197. 14:43:52the cart array and number two we need to
  17198. 14:43:55update the page so we need to update
  17199. 14:43:58this date here to match the date that we
  17200. 14:44:00select
  17201. 14:44:02let's start with the first step let's
  17202. 14:44:04create a function for updating the
  17203. 14:44:06delivery option in the cart we're going
  17204. 14:44:09to go to our
  17205. 14:44:10code and we're going to open the file
  17206. 14:44:13called cart.
  17207. 14:44:15JS so because we're modifying the cart a
  17208. 14:44:19best practice is to put the code inside
  17209. 14:44:21this file this file should contain all
  17210. 14:44:23the code that manages the cart so we're
  17211. 14:44:26going to scroll down to the bottom and
  17212. 14:44:28we're going to add a new function so
  17213. 14:44:31let's type function and let's name it
  17214. 14:44:34update
  17215. 14:44:36delivery option brackets and curly
  17216. 14:44:40brackets so when we update a delivery
  17217. 14:44:42option we need to know the product that
  17218. 14:44:45we want to update as well as the
  17219. 14:44:46delivery option that we chose so we're
  17220. 14:44:49going to need two things for this
  17221. 14:44:50function the product ID and the delivery
  17222. 14:44:56option
  17223. 14:44:59ID so the steps to do this is first
  17224. 14:45:03we're going to Loop through the cart and
  17225. 14:45:05find the product and then we're going to
  17226. 14:45:07update the delivery option ID of that
  17227. 14:45:10product so to find the product using the
  17228. 14:45:13product ID we already have some code for
  17229. 14:45:16this if we scroll up to the add to cart
  17230. 14:45:20function we have this code which takes a
  17231. 14:45:22product ID and finds the matching
  17232. 14:45:25product in the cart so let's just make a
  17233. 14:45:27copy of this code we're going to select
  17234. 14:45:29it right click
  17235. 14:45:31copy and then paste it into our new
  17236. 14:45:34function so down here right click and
  17237. 14:45:39paste so this will give us the card item
  17238. 14:45:42that matches this product ID and save it
  17239. 14:45:44in this
  17240. 14:45:45variable next each card item has a
  17241. 14:45:49property called delivery option ID so
  17242. 14:45:51we're just going to update that property
  17243. 14:45:54so down here we can just type the code
  17244. 14:45:57matching item. delivery op option ID and
  17245. 14:46:02make it equal to the delivery option ID
  17246. 14:46:05that we give to this function so
  17247. 14:46:07delivery option
  17248. 14:46:10ID and lastly because we updated the
  17249. 14:46:13cart we should save it to local
  17250. 14:46:15storage so we can use this function we
  17251. 14:46:18created earlier save to
  17252. 14:46:22storage and brackets now let's
  17253. 14:46:26save now that we have this function
  17254. 14:46:28let's use it on the checkout page so
  17255. 14:46:31first let's export it so we can use this
  17256. 14:46:33in another file we're going to export
  17257. 14:46:36this
  17258. 14:46:36function and
  17259. 14:46:39save and then open checkout.
  17260. 14:46:43JS and then in our project we need to
  17261. 14:46:46add event listeners to each of these
  17262. 14:46:48delivery options so we run some code
  17263. 14:46:50when we click them so inside our code
  17264. 14:46:53here we're going to scroll to the HTML
  17265. 14:46:55for each delivery option which is right
  17266. 14:46:58here and then we're going to add a class
  17267. 14:47:01so we can select it using the Dom js-
  17268. 14:47:04delivery D option and then we'll add an
  17269. 14:47:08event listener to these so we'll scroll
  17270. 14:47:11down and down here we'll add the event
  17271. 14:47:13listener using
  17272. 14:47:15document. query selector all brackets
  17273. 14:47:20and a string will'll select that class
  17274. 14:47:23we just created using dot js- delivery D
  17275. 14:47:28option
  17276. 14:47:31and then dot for each brackets and a
  17277. 14:47:37function so for each option we're going
  17278. 14:47:40to add an event listener and listen for
  17279. 14:47:42clicks so let's just call each of the
  17280. 14:47:44options an
  17281. 14:47:47element so for each of these elements
  17282. 14:47:49we're going to element.
  17283. 14:47:53addevent listener brackets and we're
  17284. 14:47:56going to listen for clicks comma and
  17285. 14:47:59we're going to run this function when we
  17286. 14:48:01click
  17287. 14:48:02it so as we said the first thing we'll
  17288. 14:48:05do is to update the delivery option ID
  17289. 14:48:08in the cart so now let's import that
  17290. 14:48:11function that we created earlier we're
  17291. 14:48:13going to scroll to the
  17292. 14:48:16top and we're already importing from the
  17293. 14:48:18cart so to import something else we're
  17294. 14:48:21going to add a comma and we're going to
  17295. 14:48:23import that function update delivery
  17296. 14:48:27option so now let's scroll back down and
  17297. 14:48:30use use it in our
  17298. 14:48:32code so here when we click a delivery
  17299. 14:48:35option we're going to update delivery
  17300. 14:48:39option brackets now we said we need to
  17301. 14:48:42give this function two things the
  17302. 14:48:44product ID that we want to updated for
  17303. 14:48:48as well as the delivery option ID that
  17304. 14:48:51we picked but how do we get these two
  17305. 14:48:56values and the answer is if we scroll
  17306. 14:48:59up here here we have access to the
  17307. 14:49:02product ID and we also have access to
  17308. 14:49:05the delivery option
  17309. 14:49:07ID so we can use data attributes to
  17310. 14:49:10attach it to this element
  17311. 14:49:13here so let's create a data attribute
  17312. 14:49:17data-
  17313. 14:49:19product-id equals double quotes and
  17314. 14:49:24insert the product ID which is matching
  17315. 14:49:28product. ID and we're also going to use
  17316. 14:49:32a data attribute for the delivery option
  17317. 14:49:35data- delivery D option- ID equals
  17318. 14:49:39double
  17319. 14:49:41quotes and we're going to insert the
  17320. 14:49:43delivery option here so delivery
  17321. 14:49:49option. and
  17322. 14:49:53save next let's scroll back
  17323. 14:49:55down and then here we're going to get
  17324. 14:49:59those values out of the the data
  17325. 14:50:01attributes by using const and let's use
  17326. 14:50:05the shorthand property here for practice
  17327. 14:50:08curly brackets equals the element. data
  17328. 14:50:13set and then out of the data attributes
  17329. 14:50:15we're going to get the product ID comma
  17330. 14:50:20as well as the delivery option
  17331. 14:50:24ID as a reminder this is a shorthand
  17332. 14:50:27property it does the same thing as the
  17333. 14:50:29code down here but it's just a
  17334. 14:50:32shortcut now let's
  17335. 14:50:34save and try out the project we'll go
  17336. 14:50:38back and here if we click the second
  17337. 14:50:41delivery option and then refresh the
  17338. 14:50:44page you'll notice that now this one is
  17339. 14:50:47checked and the date here is
  17340. 14:50:50correct so that's how we make the
  17341. 14:50:52section interactive we just update the
  17342. 14:50:54cart and then when we refresh the page
  17343. 14:50:57it will regenerate the HTML based on the
  17344. 14:50:59new cart
  17345. 14:51:02now let's save our code into git we'll
  17346. 14:51:04go back and open the git
  17347. 14:51:08section and then it will tell us in
  17348. 14:51:10cart. JS we added this function for
  17349. 14:51:13updating the delivery
  17350. 14:51:15option and in checkout. JS we imported
  17351. 14:51:19that function and then at the bottom we
  17352. 14:51:22added some event listeners and data
  17353. 14:51:24attributes and we use that function to
  17354. 14:51:26update the
  17355. 14:51:28card now let's add a message describe
  17356. 14:51:30describing our changes make delivery
  17357. 14:51:34options
  17358. 14:51:36interactive and
  17359. 14:51:43commit next when we select a delivery
  17360. 14:51:45option we need to update the
  17361. 14:51:49page so right now in our project when we
  17362. 14:51:53click a delivery option we need to
  17363. 14:51:55refresh the page for the text to be
  17364. 14:51:58updated so what we're going to work on
  17365. 14:52:00now is when we click this we want the
  17366. 14:52:03page to update
  17367. 14:52:04immediately so far in this course the
  17368. 14:52:06way we solve this problem is once we
  17369. 14:52:09click this we're going to use a Dom to
  17370. 14:52:11get this element here and then update
  17371. 14:52:14the text
  17372. 14:52:16directly however the problem with this
  17373. 14:52:18approach is that we need to update the
  17374. 14:52:20page one by one and later there might be
  17375. 14:52:23lots of places on the page that we need
  17376. 14:52:25to update for example when we change a
  17377. 14:52:28delivery option we also need to change
  17378. 14:52:31the numbers here on the right so there's
  17379. 14:52:34potentially a lot of things that we need
  17380. 14:52:36to update one by one and it's easy to
  17381. 14:52:39make a mistake or forget something so
  17382. 14:52:41now we're going to learn a better way to
  17383. 14:52:43update the page we'll go back to our
  17384. 14:52:47code and let's open checkout. JS
  17385. 14:52:52again and notice if we scroll
  17386. 14:52:56up here we already have all of this code
  17387. 14:53:00that takes our data and generates the
  17388. 14:53:03HTML so another way to update the page
  17389. 14:53:06is after we update our data we just need
  17390. 14:53:09to rerun all this code and regenerate
  17391. 14:53:12all this HTML so let's give this a try
  17392. 14:53:16first we're going to put all of this
  17393. 14:53:18code inside a function so we can rerun
  17394. 14:53:21it we're going to scroll to the
  17395. 14:53:24top and let's put this in a function by
  17396. 14:53:27typing function and let's call call it
  17397. 14:53:31render order summary brackets and curly
  17398. 14:53:36brackets so the word render just means
  17399. 14:53:39to display on the
  17400. 14:53:41page and we're going to call this
  17401. 14:53:43section the order
  17402. 14:53:44summary so this function should display
  17403. 14:53:47the order summary on the page next we're
  17404. 14:53:50going to put all of this code inside our
  17405. 14:53:53new function so let's just remove this
  17406. 14:53:55curly bracket
  17407. 14:53:57here and let's actually select all of
  17408. 14:54:00this code and we're just going to
  17409. 14:54:02reformat it by pressing Tab and adding
  17410. 14:54:05an indent and then at the end we're
  17411. 14:54:07going to add back that closed curly
  17412. 14:54:09bracket to finish the
  17413. 14:54:12function so all we did was we put all of
  17414. 14:54:15our code inside a
  17415. 14:54:17function and then at the bottom let's
  17416. 14:54:20run this function because we still need
  17417. 14:54:22to run all this code at the start of our
  17418. 14:54:25page so we're going to type render the
  17419. 14:54:28order summary
  17420. 14:54:31and brackets so this shouldn't change
  17421. 14:54:33anything because we're just running the
  17422. 14:54:35same code but now it's just in a
  17423. 14:54:37function let's save and
  17424. 14:54:41check and everything's still working so
  17425. 14:54:43that's good so now let's go back into
  17426. 14:54:46our
  17427. 14:54:47code and this time to update the page
  17428. 14:54:50instead of using the Dom to update the
  17429. 14:54:52page directly like we did up here we're
  17430. 14:54:55just going to rerun all of this code and
  17431. 14:54:58regenerate all the HTML
  17432. 14:55:02so after we update the data we're going
  17433. 14:55:04to rerun the code using render order
  17434. 14:55:08summary and
  17435. 14:55:10brackets now let's
  17436. 14:55:13save and try it
  17437. 14:55:15out so here let's say we start off with
  17438. 14:55:17the first delivery option if we click
  17439. 14:55:20the third delivery option you'll notice
  17440. 14:55:23that the date up here updates
  17441. 14:55:25automatically if we click the second
  17442. 14:55:27delivery option it updates automatically
  17443. 14:55:29as well
  17444. 14:55:31so that's a better way to keep the page
  17445. 14:55:33up to dat instead of using the Dom and
  17446. 14:55:36changing the HTML directly one by one we
  17447. 14:55:40just update the data and then regenerate
  17448. 14:55:43all of this
  17449. 14:55:45HTML so let's go back into our code and
  17450. 14:55:48there's two things that I want to point
  17451. 14:55:50out here so notice that we put our event
  17452. 14:55:54listeners into this big function and
  17453. 14:55:57that's because when we're regenerating
  17454. 14:55:59all the HTML we're essentially deleting
  17455. 14:56:01the previous HTML and replacing it so we
  17456. 14:56:04need to add the event listeners again
  17457. 14:56:08and the second thing I want to point out
  17458. 14:56:10is that inside the render order summary
  17459. 14:56:12function we can call render order
  17460. 14:56:15summary
  17461. 14:56:16again so a function can actually call
  17462. 14:56:19itself or rerun itself and this feature
  17463. 14:56:23is called recursion it's useful when a
  17464. 14:56:26function needs to rerun all of its code
  17465. 14:56:30so now let's save our changes into git
  17466. 14:56:33we'll open the git section and it will
  17467. 14:56:35tell us in checkout. JS we basically
  17468. 14:56:38moved all of our code into this function
  17469. 14:56:42and then at the bottom we call the
  17470. 14:56:45function at the start of the page and we
  17471. 14:56:47also call the function when we select a
  17472. 14:56:49delivery option so now let's add a
  17473. 14:56:52message describing our
  17474. 14:56:53changes
  17475. 14:56:55regenerate
  17476. 14:56:57HTML when updating
  17477. 14:57:00delivery option and
  17478. 14:57:07commit so the technique we just used
  17479. 14:57:10where we update the data and then
  17480. 14:57:12regenerate all the HTML this technique
  17481. 14:57:15is called
  17482. 14:57:16MVC MVC stands for model view controller
  17483. 14:57:21and is a popular technique in software
  17484. 14:57:23engineering in MVC we split our code
  17485. 14:57:26into three parts the first part is the
  17486. 14:57:29model this is all the code that saves
  17487. 14:57:32and manages the
  17488. 14:57:34data so in our project all the code in
  17489. 14:57:37this data folder is called the model
  17490. 14:57:40because this code saves and manages our
  17491. 14:57:43data the second part of MVC is the view
  17492. 14:57:48this is code that takes the data and
  17493. 14:57:50displays it on the page so in our
  17494. 14:57:53project if we open checkout. JS and
  17495. 14:57:56scroll to the top here we have code that
  17496. 14:58:00takes our data and then generates all
  17497. 14:58:02the HTML so this code is called the
  17498. 14:58:06view the last part of MVC is the
  17499. 14:58:09controller so this runs some code when
  17500. 14:58:11we interact with the
  17501. 14:58:14page so in our project at the
  17502. 14:58:17bottom these event listeners here would
  17503. 14:58:20be called the controller because they do
  17504. 14:58:23something when we interact with the page
  17505. 14:58:25or with the
  17506. 14:58:26view so in MVC we divide our code Lo
  17507. 14:58:30into these three parts and then they
  17508. 14:58:32interact with each other in a
  17509. 14:58:34loop first we use the model to generate
  17510. 14:58:38the
  17511. 14:58:39view so in our code that's what we did
  17512. 14:58:42here we took the model or the data which
  17513. 14:58:45is our cart and then we generated the
  17514. 14:58:48HTML or the view then when we interact
  17515. 14:58:52with the view it will run the
  17516. 14:58:56controller so that's what we did at the
  17517. 14:58:58bottom here
  17518. 14:59:00when we interact with the page we run
  17519. 14:59:02some
  17520. 14:59:03code then the controller will update the
  17521. 14:59:08model so that's what we did here inside
  17522. 14:59:12the controller we just updated our data
  17523. 14:59:14using this
  17524. 14:59:16function and finally we use the updated
  17525. 14:59:19model to regenerate The
  17526. 14:59:23View so that's what this code does it
  17527. 14:59:26takes the updated data and then
  17528. 14:59:28regenerates all the HTM ml or
  17529. 14:59:30regenerates The
  17530. 14:59:32View so that's how MVC Works instead of
  17531. 14:59:36updating the page directly with the Dom
  17532. 14:59:38we just update our data and then
  17533. 14:59:40regenerate all the
  17534. 14:59:44HTML so we use MVC because doing this
  17535. 14:59:48makes sure that the page always matches
  17536. 14:59:51the
  17537. 14:59:51data so MVC is known as a design pattern
  17538. 14:59:56it's a way to organize and design our
  17539. 14:59:59code in the future you might use a
  17540. 15:00:01technology called a JavaScript framework
  17541. 15:00:03to build websites many JavaScript
  17542. 15:00:06Frameworks are based on MVC so it's
  17543. 15:00:09useful to know how it
  17544. 15:00:11works okay now that we learned the MVC
  17545. 15:00:15design pattern let's do some practical
  17546. 15:00:17examples using
  17547. 15:00:19MBC we're going to open our project
  17548. 15:00:21again to the checkout page and we're
  17549. 15:00:24going to create this payment section on
  17550. 15:00:27the right now before we start if we look
  17551. 15:00:30at our code in this checkout. JS file
  17552. 15:00:34you'll notice that we already have a lot
  17553. 15:00:35of code
  17554. 15:00:37here instead of adding even more code to
  17555. 15:00:40this file let's actually split this up
  17556. 15:00:42into two different
  17557. 15:00:44files so one file will be responsible
  17558. 15:00:47for this left section and then one file
  17559. 15:00:50will be responsible for this right
  17560. 15:00:52section we're doing this to keep our
  17561. 15:00:54code more organized and so that we don't
  17562. 15:00:56have too much code in one file
  17563. 15:01:00so let's go back and let's create two
  17564. 15:01:02new files for the checkout page so up
  17565. 15:01:05here we'll create a file and let's call
  17566. 15:01:08this one the order summary. JS and press
  17567. 15:01:14enter and let's create another file for
  17568. 15:01:17the right section and let's call this
  17569. 15:01:20one the payment
  17570. 15:01:23summary. JS and press
  17571. 15:01:27enter now both of these files are
  17572. 15:01:29related to the checkout page so let's
  17573. 15:01:32create a folder and group them together
  17574. 15:01:35we'll create a new folder up here and
  17575. 15:01:38let's call it checkout and press enter
  17576. 15:01:42and we're going to move these two files
  17577. 15:01:43into this new folder so we move that and
  17578. 15:01:47we'll move
  17579. 15:01:51that next let's open checkout. JS and
  17580. 15:01:55let's move all of this code into the
  17581. 15:01:58order summary so we'll press control a
  17582. 15:02:01or command a to select all the code and
  17583. 15:02:04then
  17584. 15:02:05rightclick
  17585. 15:02:07cut and then in here we're going to
  17586. 15:02:10rightclick and
  17587. 15:02:14paste now there's a few things that we
  17588. 15:02:16need to fix here so first our checkout.
  17589. 15:02:19HTML file is still loading checkout. JS
  17590. 15:02:23so we actually need to run this function
  17591. 15:02:25inside checkout. JS instead of inside
  17592. 15:02:28here so let's actually remove
  17593. 15:02:31this and save and we're going to scroll
  17594. 15:02:35to the
  17595. 15:02:36top and let's export this function so we
  17596. 15:02:39can run it inside here so we'll type
  17597. 15:02:42export and save and then inside checkout
  17598. 15:02:47we're going to import curly brackets the
  17599. 15:02:51render order summary
  17600. 15:02:54function from a string and to locate
  17601. 15:02:58this file we're going to type A do slir
  17602. 15:03:02that means to start from the current
  17603. 15:03:04folder and we'll go into the checkout
  17604. 15:03:06folder checkout SL orderers
  17605. 15:03:10summary.
  17606. 15:03:13JS and finally we're going to run this
  17607. 15:03:16function when we load the page so render
  17608. 15:03:19order summary and brackets and
  17609. 15:03:25save another thing we need to fix is if
  17610. 15:03:28we open order summary .js we moved this
  17611. 15:03:32file into a folder so now all of these
  17612. 15:03:35file paths need to be updated do do
  17613. 15:03:38slash is now inside the scripts folder
  17614. 15:03:41and we can't find a data folder anymore
  17615. 15:03:43so this is going to cause an error so to
  17616. 15:03:46find the data folder we need to get out
  17617. 15:03:48of the checkout folder and then we need
  17618. 15:03:50to get out of the scripts folder so we
  17619. 15:03:53need to get out of two folders so we'll
  17620. 15:03:55just add an additional do dot slash and
  17621. 15:03:58then same thing for this we'll add
  17622. 15:04:00another dot dot slash and for the utils
  17623. 15:04:04now we need to get out of the checkout
  17624. 15:04:05folder and then into the utils folder so
  17625. 15:04:08we'll change this to do/
  17626. 15:04:11utils and then for here we're going to
  17627. 15:04:13go out an additional folder so another
  17628. 15:04:16dot dot slash and
  17629. 15:04:19save so now let's check if this still
  17630. 15:04:21works we'll go to our project and
  17631. 15:04:24refresh and try it
  17632. 15:04:28out and the p still works because we're
  17633. 15:04:31still running all of the same code but
  17634. 15:04:34we just organized it into its own file
  17635. 15:04:37so now let's open git and save our
  17636. 15:04:39changes so it's going to tell us that in
  17637. 15:04:41here we removed all this code and then
  17638. 15:04:44we separated it into the order summary
  17639. 15:04:47file and the payment summary file which
  17640. 15:04:49we're going to work on next so let's
  17641. 15:04:51type a message describing our changes
  17642. 15:04:54move order
  17643. 15:04:57summary into its own own file and
  17644. 15:05:06commit now let's start creating the
  17645. 15:05:08payment summary on the right side so in
  17646. 15:05:11our code let's open payment summary.
  17647. 15:05:18JS and just like the order summary let's
  17648. 15:05:21put all our code in a function so we can
  17649. 15:05:23regenerate the HTML if we need to so
  17650. 15:05:27we'll type function and let's call it
  17651. 15:05:30render payment summary brackets and
  17652. 15:05:34curly
  17653. 15:05:36brackets and let's just
  17654. 15:05:39console.log string payment summary just
  17655. 15:05:43to make sure it's working so we'll save
  17656. 15:05:47and let's export this function and then
  17657. 15:05:49run it inside checkout. JS so export
  17658. 15:05:53save and then in checkout. JS at the top
  17659. 15:05:57we're going to import curly brackets the
  17660. 15:06:01render payment summary function from a
  17661. 15:06:06string the current folder go into the
  17662. 15:06:09checkout folder and the payment summary.
  17663. 15:06:13JS file and then at the bottom we're
  17664. 15:06:16going to run the function so render
  17665. 15:06:19payment summary brackets and
  17666. 15:06:23save let's go back to our project and
  17667. 15:06:26open the console rightclick inspect the
  17668. 15:06:30console and we'll see the message
  17669. 15:06:32payment summary so now we know that our
  17670. 15:06:35code here is working and the export is
  17671. 15:06:38working
  17672. 15:06:40correctly now let's remove this code and
  17673. 15:06:43actually build the payment
  17674. 15:06:46summary as always we're going to do
  17675. 15:06:48three steps save the data generate the
  17676. 15:06:51HTML and make it interactive so let's
  17677. 15:06:55start with saving the data in MVC this
  17678. 15:06:58is called the
  17679. 15:07:01model so if we go to our
  17680. 15:07:03project for this section the data that
  17681. 15:07:06we need is just to calculate these
  17682. 15:07:08numbers
  17683. 15:07:10here so let's start by calculating this
  17684. 15:07:13first number here which is the cost of
  17685. 15:07:15the products to calculate this we can
  17686. 15:07:18Loop through the cart and for each
  17687. 15:07:20product in the cart we can multiply the
  17688. 15:07:22products price times the quantity and
  17689. 15:07:26then add everything together now let's
  17690. 15:07:28convert these steps into code we'll go
  17691. 15:07:31to our project and inside the payment
  17692. 15:07:34summary
  17693. 15:07:35file first we need to get the cart so
  17694. 15:07:38let's import it import curly brackets
  17695. 15:07:42the
  17696. 15:07:43cart from the string and we'll locate
  17697. 15:07:47cart.
  17698. 15:07:49JS so first we'll go out of the checkout
  17699. 15:07:51folder do do slash and then go out of
  17700. 15:07:54the scripts folder do do slash and then
  17701. 15:07:57into the data folder
  17702. 15:08:02data.js and
  17703. 15:08:04save and now we're going to Loop through
  17704. 15:08:06the cart so we can type cart. for each
  17705. 15:08:12brackets and a
  17706. 15:08:15function and let's name the parameter
  17707. 15:08:18cart item so for each cart item we have
  17708. 15:08:22the product ID and the quantity so we
  17709. 15:08:25need to multiply the product's price
  17710. 15:08:27times the quantity so first we need to
  17711. 15:08:30use the product ID and get the full
  17712. 15:08:32product details and we already have some
  17713. 15:08:35code for this inside orders summary. JS
  17714. 15:08:39if we scroll down here we have some code
  17715. 15:08:42that takes a product ID and finds the
  17716. 15:08:45matching
  17717. 15:08:46product now instead of copy and pasting
  17718. 15:08:49this code let's actually put this into a
  17719. 15:08:51function so we can reuse this and we'll
  17720. 15:08:54practice some modules so let's select
  17721. 15:08:57this code and rightclick and copy it
  17722. 15:09:01first and then let's go into the
  17723. 15:09:03products. JS
  17724. 15:09:05file and at the top we're going to
  17725. 15:09:08create a function so we can reuse that
  17726. 15:09:10code so let's type function and let's
  17727. 15:09:14name it get product brackets and curer
  17728. 15:09:18brackets and we're going to give this
  17729. 15:09:20function the product ID that we're
  17730. 15:09:23looking for and then let's paste the
  17731. 15:09:26code we copied earlier right click and
  17732. 15:09:31paste so this finds the matching product
  17733. 15:09:33using the product ID and then at the end
  17734. 15:09:36of the function let's return the
  17735. 15:09:39matching product so we can use this
  17736. 15:09:42outside of the function so now let's
  17737. 15:09:45export this function so we can share it
  17738. 15:09:47across files so export and
  17739. 15:09:51save first we'll go to the order summary
  17740. 15:09:55and at the top let's import that
  17741. 15:09:56function so we're already importing from
  17742. 15:09:58product .js we're going to import
  17743. 15:10:01something else using a comma the get
  17744. 15:10:04product function and then down
  17745. 15:10:07here instead of all this
  17746. 15:10:10code we're going to run that function so
  17747. 15:10:13we make this equal to get product and
  17748. 15:10:18run the function and we need to give the
  17749. 15:10:20function the product ID so let's just
  17750. 15:10:22give that here product
  17751. 15:10:25ID and because we're no longer updating
  17752. 15:10:28this variable we can change from let to
  17753. 15:10:31const and
  17754. 15:10:33save let's go to our project to make
  17755. 15:10:36sure everything's still working so that
  17756. 15:10:38looks
  17757. 15:10:39good and now let's use this function
  17758. 15:10:42inside payment summary. JS so the same
  17759. 15:10:45thing we will import curly brackets the
  17760. 15:10:49get product function from a
  17761. 15:10:53string and we'll go to the data folder
  17762. 15:10:56do SL do do SL data SL
  17763. 15:11:00and products.
  17764. 15:11:03JS and let's run that function to get
  17765. 15:11:05the full product so get product brackets
  17766. 15:11:10and then each cart item has the product
  17767. 15:11:12ID so we're going to give it to the
  17768. 15:11:13function cart item. product
  17769. 15:11:18ID and let's save this product in a
  17770. 15:11:21variable so we can use it for our
  17771. 15:11:23calculations con product equals get
  17772. 15:11:27product so now now as we said we're
  17773. 15:11:30going to multiply the product's price by
  17774. 15:11:32the quantity so down here we can do
  17775. 15:11:36product and it has a property
  17776. 15:11:38called price sense and we're going to
  17777. 15:11:42multiply it by the cart item and this
  17778. 15:11:46has a property do
  17779. 15:11:49quantity and the last step is to add all
  17780. 15:11:52of these
  17781. 15:11:54together so outside of the loop let's
  17782. 15:11:56create a variable to save the result of
  17783. 15:11:59adding them together let's type let and
  17784. 15:12:02we'll name it product price sense equals
  17785. 15:12:08and we'll make it zero to start and then
  17786. 15:12:11every time we Loop through the cart
  17787. 15:12:13we're going to add this to this variable
  17788. 15:12:15so we'll type product price sense plus
  17789. 15:12:20equals this value
  17790. 15:12:23here and that's how we calculate the
  17791. 15:12:25cost of the products so at the bottom
  17792. 15:12:28let's
  17793. 15:12:30console.log the product price sense just
  17794. 15:12:35to make sure that this is working we'll
  17795. 15:12:38save and in our project we're going to
  17796. 15:12:41check the
  17797. 15:12:42console and it will give us this number
  17798. 15:12:44here and this number should match the
  17799. 15:12:47number on the page except it's in sense
  17800. 15:12:50and assuming you also have two socks and
  17801. 15:12:53one basketball in your
  17802. 15:12:56cart okay so that's how we calculate
  17803. 15:12:58this first number here now let's
  17804. 15:13:01calculate the second number which is the
  17805. 15:13:03cost of shipping so to calculate this
  17806. 15:13:06we're going to Loop through the cart and
  17807. 15:13:08add all the shipping costs
  17808. 15:13:11together so let's go into our
  17809. 15:13:14code and here we're already looping
  17810. 15:13:17through the cart so instead of creating
  17811. 15:13:19another loop let's just calculate the
  17812. 15:13:21shipping cost in the same Loop so let's
  17813. 15:13:25create some new
  17814. 15:13:26lines and each card item has a property
  17815. 15:13:29delivery option ID so we need to use
  17816. 15:13:32this ID to get the full delivery option
  17817. 15:13:35which has the price so in the order
  17818. 15:13:38summary we also have this code that
  17819. 15:13:41takes the delivery option ID and finds
  17820. 15:13:43the full delivery option so again let's
  17821. 15:13:46create a function to share and reuse
  17822. 15:13:48this
  17823. 15:13:49code so first let's select this code and
  17824. 15:13:52we'll write click and
  17825. 15:13:55copy and let's go to the delivery
  17826. 15:13:58options. Js
  17827. 15:13:59file and then at the bottom we'll create
  17828. 15:14:02a
  17829. 15:14:03function let's call it get delivery
  17830. 15:14:07option brackets and curly brackets and
  17831. 15:14:11we'll give this function the delivery
  17832. 15:14:14option
  17833. 15:14:15ID and inside here we'll rightclick and
  17834. 15:14:19paste the code that we copied earlier so
  17835. 15:14:23this will take this ID and get the
  17836. 15:14:25delivery option from this array and
  17837. 15:14:28let's return return this delivery
  17838. 15:14:30option so we can use it outside of this
  17839. 15:14:34function and just to be safe let's also
  17840. 15:14:37give this a default value if we don't
  17841. 15:14:40find a delivery option so we'll use the
  17842. 15:14:43default operator
  17843. 15:14:45or and let's make the default value the
  17844. 15:14:48first delivery option so delivery
  17845. 15:14:53options square bracket 0
  17846. 15:15:00so now let's export this and
  17847. 15:15:03save and then inside order summary we'll
  17848. 15:15:06use this function so at the
  17849. 15:15:08top we're already importing from
  17850. 15:15:11delivery options so we'll type a comma
  17851. 15:15:14and get delivery
  17852. 15:15:17option and then down here instead of
  17853. 15:15:20running all this code we'll select it
  17854. 15:15:23and make this equal and run the function
  17855. 15:15:26get delivery option
  17856. 15:15:29brackets and we'll give it this delivery
  17857. 15:15:32option ID delivery option
  17858. 15:15:36ID and we can change from let to const
  17859. 15:15:40since we're not changing this variable
  17860. 15:15:42so now let's save and let's use this
  17861. 15:15:45function inside payment summary as well
  17862. 15:15:48so in here at the top we'll import curly
  17863. 15:15:52brackets the get delivery option
  17864. 15:15:57function from
  17865. 15:16:00quotes the data folder do do slash do
  17866. 15:16:04data slash and the delivery options. JS
  17867. 15:16:11file and now we'll run this function get
  17868. 15:16:15delivery option and
  17869. 15:16:18brackets and each cart item has a
  17870. 15:16:20delivery option ID so we'll put that
  17871. 15:16:23into the function cart item. delivery
  17872. 15:16:28option
  17873. 15:16:29ID and let's save this in a
  17874. 15:16:32variable con delivery option
  17875. 15:16:38equals and now we can get the price for
  17876. 15:16:41the delivery option using delivery
  17877. 15:16:45option price sense
  17878. 15:16:49property and finally we're going to add
  17879. 15:16:51all these together so at the top let's
  17880. 15:16:54create a variable to save the result let
  17881. 15:16:57we'll call it ship
  17882. 15:16:59shipping price cents equals zero and
  17883. 15:17:04when we Loop through the cart we're
  17884. 15:17:07going to
  17885. 15:17:08do shipping price sents plus equals this
  17886. 15:17:14delivery options
  17887. 15:17:17price and let's also console.log this to
  17888. 15:17:21make sure it's working so shipping price
  17889. 15:17:26cents and Save
  17890. 15:17:29now let's go to our project and check
  17891. 15:17:31the
  17892. 15:17:34console and this value
  17893. 15:17:37here should be the same as adding these
  17894. 15:17:40two numbers together in
  17895. 15:17:44sense so that's how we calculate the
  17896. 15:17:46second number the shipping price so note
  17897. 15:17:50that this
  17898. 15:17:51$4.99 number is just a practice number
  17899. 15:17:54right now the actual shipping price
  17900. 15:17:56should be the total of adding all these
  17901. 15:17:58sh shipping prices
  17902. 15:18:01together the next number we need to
  17903. 15:18:03calculate is the total before tax so let
  17904. 15:18:06just adding these two numbers together
  17905. 15:18:09inside our
  17906. 15:18:11code let's remove these console. logs
  17907. 15:18:14and we'll create const total before tax
  17908. 15:18:20sense and make it equal and we'll add
  17909. 15:18:23these two together product price sense
  17910. 15:18:28Plus shipping price
  17911. 15:18:32sense and the next number is the tax so
  17912. 15:18:36we're going to calculate a 10% tax on
  17913. 15:18:39this number here to calculate 10% of
  17914. 15:18:41something we can multiply this by 10 /
  17915. 15:18:45100 that's what percent means so 10 ID
  17916. 15:18:50100 is also equal to
  17917. 15:18:520.1 so we can also multiply this number
  17918. 15:18:55by
  17919. 15:18:570.1 let's go into code and we'll create
  17920. 15:19:00a variable const tax sense make it equal
  17921. 15:19:05to the total before tax sense times 0.1
  17922. 15:19:11or
  17923. 15:19:1310% finally to calculate the total we're
  17924. 15:19:16going to add these two numbers together
  17925. 15:19:19so const total sense equals the total
  17926. 15:19:25before tax sense Plus the tax
  17927. 15:19:31sense so that's how we calculate all the
  17928. 15:19:34numbers in the payment summary and now
  17929. 15:19:36we have all the data that we need next
  17930. 15:19:39we're going to generate the HTML in MVC
  17931. 15:19:43this is called The
  17932. 15:19:46View so at the bottom here let's create
  17933. 15:19:49a variable to store the
  17934. 15:19:51HTML const payment summary
  17935. 15:19:57HTML and we'll make make it equal to a
  17936. 15:20:00template
  17937. 15:20:01string and then inside here we're going
  17938. 15:20:03to copy and paste the
  17939. 15:20:05HTML so let's open checkout.
  17940. 15:20:09HTML and we're going to scroll up until
  17941. 15:20:12we find the class payment D summary so
  17942. 15:20:16we're going to generate all the HTML
  17943. 15:20:18inside here so let's actually select
  17944. 15:20:21this HTML and move it into our
  17945. 15:20:23JavaScript so we'll select it up until
  17946. 15:20:26this button and then we we can
  17947. 15:20:29rightclick
  17948. 15:20:32cut and remove the new line save and
  17949. 15:20:37then inside the payment summary inside
  17950. 15:20:40the screen we'll rightclick and paste
  17951. 15:20:43the
  17952. 15:20:45HTML and if we need to reformat we can
  17953. 15:20:48select these
  17954. 15:20:50lines and press tab a few times to
  17955. 15:20:53reformat
  17956. 15:20:57it and and
  17957. 15:21:00save next instead of using these same
  17958. 15:21:03numbers every time we're going to take
  17959. 15:21:06the numbers we calculated up here and
  17960. 15:21:08insert it into this HTML string so
  17961. 15:21:12instead of 42.75 every time we're going
  17962. 15:21:15to insert the product price
  17963. 15:21:19sense and let's give it some new lines
  17964. 15:21:22to make it a little easier to
  17965. 15:21:24read now remember that this number is in
  17966. 15:21:27sense and we want to display it as
  17967. 15:21:30dollars to do that we already have a
  17968. 15:21:33function for this so in the utils money.
  17969. 15:21:37JS file we have a function format
  17970. 15:21:40currency that displays it in dollars so
  17971. 15:21:43let's use this function inside payment
  17972. 15:21:45summary so at the top let's import the
  17973. 15:21:48function
  17974. 15:21:49first import curly brackets the format
  17975. 15:21:54currency function from a Str
  17976. 15:21:59and to locate this folder we'll go out
  17977. 15:22:01of the checkout folder do do/ the UIL
  17978. 15:22:05folder SL money.jpg
  17979. 15:22:28numbers here so this one is the shipping
  17980. 15:22:31price so we can
  17981. 15:22:34insert format currency brackets the
  17982. 15:22:40shipping price sents and let's give it
  17983. 15:22:43some new lines to make it a little
  17984. 15:22:45easier to
  17985. 15:22:47read next let's insert the total before
  17986. 15:22:51tax so we'll remove this number and
  17987. 15:22:54insert format currency bracket gets the
  17988. 15:22:59total before tax
  17989. 15:23:02sents and new
  17990. 15:23:07lines and next is the tax we'll remove
  17991. 15:23:11this and
  17992. 15:23:12insert format currency brackets the tax
  17993. 15:23:19sents and we'll enter some new lines and
  17994. 15:23:23finally the total so we remove this
  17995. 15:23:26insert
  17996. 15:23:28format currency brackets total sense and
  17997. 15:23:34add some new
  17998. 15:23:37lines and
  17999. 15:23:40save now before we continue I actually
  18000. 15:23:43learned of a small issue with the format
  18001. 15:23:45currency function so let's open the
  18002. 15:23:48money. JS
  18003. 15:23:50file and I recently learned that this
  18004. 15:23:52two fixed method has a small issue with
  18005. 15:23:56rounding so for some numbers that end in
  18006. 15:23:58five it won't round the number
  18007. 15:24:02properly one way to fix this issue is to
  18008. 15:24:05round the price sense first so that this
  18009. 15:24:08method doesn't have to do any rounding
  18010. 15:24:11so at the front here let's just type
  18011. 15:24:15math.round Open Bracket and close
  18012. 15:24:19bracket so this is just a tricky issue
  18013. 15:24:22that I discovered with the help of AI
  18014. 15:24:24recently we just need to add a little
  18015. 15:24:26bit of code here to fix it
  18016. 15:24:29so now let's save this
  18017. 15:24:30file and go back to payment
  18018. 15:24:33summary so now that we generated all
  18019. 15:24:36this HTML and inserted our numbers into
  18020. 15:24:39it let's put this HTML on the page so
  18021. 15:24:43inside checkout.
  18022. 15:24:46HTML let's give this payment summary
  18023. 15:24:48another class
  18024. 15:24:50js- payment Das summary and then we'll
  18025. 15:24:54select this using the Dom so now let's
  18026. 15:24:57save this
  18027. 15:24:59file inside payment summary at the
  18028. 15:25:03bottom after we create the HTML we're
  18029. 15:25:06going to document. query selector
  18030. 15:25:11brackets and a string and we'll select
  18031. 15:25:15js- payment D summary to get that
  18032. 15:25:21element and we'll change the HTML inside
  18033. 15:25:24that element using do inner Capital H
  18034. 15:25:29HTML and we'll make it equal to the
  18035. 15:25:32payment summary
  18036. 15:25:35HTML that we just created so now let's
  18037. 15:25:39save and check our
  18038. 15:25:42project and now these numbers on the
  18039. 15:25:45right should match what we have in the
  18040. 15:25:47cart and the delivery options that we
  18041. 15:25:51selected so that's how we generate the
  18042. 15:25:54HTML for this section
  18043. 15:25:56here now let's save our changes into git
  18044. 15:26:00we'll open
  18045. 15:26:01git and it will tell us in checkout.
  18046. 15:26:03HTML we removed the HTML we created
  18047. 15:26:08manually in delivery options. JS we
  18048. 15:26:11created this shared function for finding
  18049. 15:26:13the full delivery option and the same
  18050. 15:26:15thing in products. JS for finding the
  18051. 15:26:17full
  18052. 15:26:18product inside checkout. JS we're now
  18053. 15:26:21running all the code inside payment
  18054. 15:26:24summary in order summary we're just
  18055. 15:26:27using those shared functions that we
  18056. 15:26:29created and then in payment summary we
  18057. 15:26:32calculated the numbers and generated all
  18058. 15:26:35the
  18059. 15:26:36HTML and finally in money. JS we fixed a
  18060. 15:26:40small bug related to rounding let's type
  18061. 15:26:43a message to describe our changes save
  18062. 15:26:47data and generate
  18063. 15:26:51HTML for payment
  18064. 15:26:55summary and commit
  18065. 15:27:03the last step is to make it interactive
  18066. 15:27:06in MVC this is called the
  18067. 15:27:09controller let's go to our
  18068. 15:27:11project and one thing we can make
  18069. 15:27:14interactive in this section is the place
  18070. 15:27:16order button however we haven't figured
  18071. 15:27:19out how orders work yet so we'll work on
  18072. 15:27:22this in the next lesson another thing we
  18073. 15:27:25can make interactive however is when we
  18074. 15:27:28change the cart on the left these prices
  18075. 15:27:32should also update so let's make this
  18076. 15:27:35interactive first when we delete a
  18077. 15:27:37product the prices here should update so
  18078. 15:27:41let's actually practice using MVC to
  18079. 15:27:44create this
  18080. 15:27:46functionality according to MVC when we
  18081. 15:27:49click delete we should update the data
  18082. 15:27:52and then regenerate all of this
  18083. 15:27:55HTML so let's get started and go to the
  18084. 15:27:58code for the delete link so this is in
  18085. 15:28:01the order summary section so in our code
  18086. 15:28:04let's open the orders summary. JS
  18087. 15:28:08file and we'll scroll down and find the
  18088. 15:28:11event listener for the delete link which
  18089. 15:28:14is right
  18090. 15:28:16here when we click delete first we're
  18091. 15:28:19going to update the
  18092. 15:28:22data so we already update the data using
  18093. 15:28:25this function remove from cart
  18094. 15:28:29and now the next step according to MBC
  18095. 15:28:31is to just regenerate all the
  18096. 15:28:34HTML so to regenerate the HTML for the
  18097. 15:28:37payment summary we created this function
  18098. 15:28:40at the top render payment
  18099. 15:28:43summary so this will recalculate all the
  18100. 15:28:46numbers and then regenerate the
  18101. 15:28:50HTML so let's import this
  18102. 15:28:53function in order summary. JS we'll
  18103. 15:28:56scroll to the top
  18104. 15:28:58and down here we can import curly
  18105. 15:29:02brackets the render payment summary
  18106. 15:29:07function from a string and we'll locate
  18107. 15:29:11the payment summary file so it's in the
  18108. 15:29:14same folder so we're going to start with
  18109. 15:29:16a slash which is the current folder the
  18110. 15:29:19payment summary. JS file and now let's
  18111. 15:29:24use this function in the event listener
  18112. 15:29:27so let's scroll
  18113. 15:29:29down when we click delete we're going to
  18114. 15:29:33update the data and then regenerate all
  18115. 15:29:36the HTML using render payment summary
  18116. 15:29:42brackets now let's save and go to our
  18117. 15:29:47project focus on the numbers on the
  18118. 15:29:50right here and now let's click
  18119. 15:29:53delete and you'll notice that all of
  18120. 15:29:55these numbers updated to match our new
  18121. 15:29:59so that's another example of using MVC
  18122. 15:30:03when we click delete instead of using
  18123. 15:30:05the Dom to change these numbers one by
  18124. 15:30:07one we just update the data and then
  18125. 15:30:10regenerate all of the
  18126. 15:30:13HTML MVC makes sure that the HTML always
  18127. 15:30:17matches the data or that the view always
  18128. 15:30:20matches the
  18129. 15:30:22model let's do one last example of NBC
  18130. 15:30:25for this lesson when we change the
  18131. 15:30:28delivery options here we also want these
  18132. 15:30:31numbers on the right to update so let's
  18133. 15:30:34find the code for the delivery options
  18134. 15:30:37again this is in the order summary
  18135. 15:30:38section so in our code we'll open orders
  18136. 15:30:42summary. JS scroll down and then here we
  18137. 15:30:47updated the data and then we're just
  18138. 15:30:49going to regenerate the HTML for the
  18139. 15:30:52payment summary using render payment
  18140. 15:30:56summary and and
  18141. 15:30:58brackets now let's
  18142. 15:31:01save and then look at our project and
  18143. 15:31:04let's try changing the delivery option
  18144. 15:31:06here and you'll notice the numbers on
  18145. 15:31:08the right update automatically to match
  18146. 15:31:11the cart and that's it we finished the
  18147. 15:31:14checkout page so now let's save our code
  18148. 15:31:17into git we'll go into the git
  18149. 15:31:20section here and inside order summary we
  18150. 15:31:24basically imported the render payment
  18151. 15:31:27summary function
  18152. 15:31:28and then at the
  18153. 15:31:30bottom we used MVC so when we click
  18154. 15:31:34delete we update the data or the model
  18155. 15:31:37and then we regenerate all the HTML or
  18156. 15:31:40regenerate The View down here same thing
  18157. 15:31:44when we click a delivery option we
  18158. 15:31:46update the data and then regenerate all
  18159. 15:31:49the
  18160. 15:31:50HTML the last thing we're going to do is
  18161. 15:31:53at the top of this file
  18162. 15:31:58we're going to remove this practice code
  18163. 15:32:00that we use to learn external libraries
  18164. 15:32:03so let's open orders summary.
  18165. 15:32:07JS and at the top we're going to remove
  18166. 15:32:10this
  18167. 15:32:12code and
  18168. 15:32:15save now let's go back into
  18169. 15:32:20get and that looks good finally let's
  18170. 15:32:24type a message describing our
  18171. 15:32:26changes make
  18172. 15:32:28payment
  18173. 15:32:30summary
  18174. 15:32:32interactive and
  18175. 15:32:39commit and that's the end of this lesson
  18176. 15:32:42in this lesson we learned about external
  18177. 15:32:44libraries which is code that is outside
  18178. 15:32:47of our project we learned about the DJs
  18179. 15:32:51external Library which helps us work
  18180. 15:32:53with dates we also learned how to use
  18181. 15:32:56external libraries with JavaScript
  18182. 15:32:58modules together as well as the default
  18183. 15:33:01export syntax we created the delivery
  18184. 15:33:05section of the checkout page we learned
  18185. 15:33:07MBC which is a better way to design our
  18186. 15:33:10code and we created the payment section
  18187. 15:33:14of the checkout
  18188. 15:33:17page thanks for watching this course so
  18189. 15:33:20far if you want to support this channel
  18190. 15:33:22you can check out the premium version of
  18191. 15:33:24my courses just like this one where you
  18192. 15:33:26can get a certificate of completion and
  18193. 15:33:29a Better Learning platform if you're
  18194. 15:33:31interested in extra videos from me more
  18195. 15:33:34in-depth videos and behind the scenes
  18196. 15:33:36content you can check out these links
  18197. 15:33:39here you can also support the channel by
  18198. 15:33:42liking the video subscribing and sharing
  18199. 15:33:45this video with others thanks again and
  18200. 15:33:48let's continue with the
  18201. 15:33:50course here are some exercises to help
  18202. 15:33:53you practice what we learned in this
  18203. 15:33:56lesson e
  18204. 15:34:40in the rest of this course if you get an
  18205. 15:34:43error in your project but your code
  18206. 15:34:45looks correct you may have some bad data
  18207. 15:34:48saved in local storage to fix this you
  18208. 15:34:52can try running local storage. CLE in
  18209. 15:34:55your console to remove everything from
  18210. 15:34:57local
  18211. 15:34:58storage and then refresh the
  18212. 15:35:03page if you see an error like this
  18213. 15:35:06object object that means that you saved
  18214. 15:35:09something directly into local storage
  18215. 15:35:11but you have to json.stringify it first
  18216. 15:35:15so check your code to make sure that
  18217. 15:35:16you're always stringifying before saving
  18218. 15:35:19to local storage and then clear your
  18219. 15:35:21local storage and refresh the
  18220. 15:35:26page in this lesson we're going to learn
  18221. 15:35:29how to test our code so when we're
  18222. 15:35:32writing code it's important to test our
  18223. 15:35:34code to see if it's working correctly
  18224. 15:35:37and if you're working at a company in
  18225. 15:35:39the future you'll also be expected to
  18226. 15:35:41test your code so let's learn how to do
  18227. 15:35:44that in this
  18228. 15:35:46lesson the easiest way to test our code
  18229. 15:35:48is to just open the website and try out
  18230. 15:35:51the code for example in our code here
  18231. 15:35:55let's open the file utils money.jpg
  18232. 15:36:30server so on this website we have a
  18233. 15:36:33couple of prices here as well as here
  18234. 15:36:36and we created these prices using the
  18235. 15:36:38function format
  18236. 15:36:40currency so if we just look at the
  18237. 15:36:42website we can see that these prices
  18238. 15:36:44look correct and this tells us that the
  18239. 15:36:47function format currency is working
  18240. 15:36:49correctly so that's one way to test that
  18241. 15:36:52function we just open the website and
  18242. 15:36:54try it out so this is called manual
  18243. 15:36:58testing when we manually open the
  18244. 15:37:00website and try our code so manual
  18245. 15:37:03testing is really useful for quickly
  18246. 15:37:06checking to see if everything is
  18247. 15:37:08working so now let's talk about the
  18248. 15:37:11disadvantages of manual testing one
  18249. 15:37:14disadvantage is it's hard to test every
  18250. 15:37:18situation for example here we just
  18251. 15:37:21happen to have the number
  18252. 15:37:23295 and we test with that however what
  18253. 15:37:26if we want to test with different
  18254. 15:37:28numbers like the number zero or a number
  18255. 15:37:31that requires rounding like
  18256. 15:37:352.5 in order to test all these
  18257. 15:37:37situations we have to manually set up
  18258. 15:37:39our website so that our code runs with
  18259. 15:37:42those numbers so here we ran with 2095
  18260. 15:37:46as well as zero so we covered those
  18261. 15:37:49situations however we did not cover
  18262. 15:37:522.5 so we have to do a bunch of setup in
  18263. 15:37:55order to test that situation
  18264. 15:37:58so as you can see it takes a lot of time
  18265. 15:38:00and effort to test different situations
  18266. 15:38:02in our code the second disadvantage of
  18267. 15:38:05manual testing is that it's hard to
  18268. 15:38:07retest the code for example if we change
  18269. 15:38:11some of our code how do we retest format
  18270. 15:38:14currency and make sure that it's still
  18271. 15:38:16working
  18272. 15:38:18correctly well we would have to open the
  18273. 15:38:20website and set up all the situations
  18274. 15:38:22again and retest all those situations
  18275. 15:38:25manually so this takes too much time and
  18276. 15:38:28effort to retest the
  18277. 15:38:30code to solve these two problems we're
  18278. 15:38:33going to learn another way to test our
  18279. 15:38:35code called automated
  18280. 15:38:38testing automated testing basically
  18281. 15:38:41means using code to test code instead of
  18282. 15:38:45manually opening the website and
  18283. 15:38:47clicking around to try our code we're
  18284. 15:38:49going to tell the computer to do these
  18285. 15:38:51tests for us and this will save a lot of
  18286. 15:38:54time and effort so let's go ahead and
  18287. 15:38:57create our first automated test we're
  18288. 15:39:01going to go back to our
  18289. 15:39:02code and again let's test this format
  18290. 15:39:06currency function to create an automated
  18291. 15:39:09test we're just going to write some code
  18292. 15:39:11to test this function so first let's
  18293. 15:39:14create a folder to group all of our test
  18294. 15:39:17code together we're going to click this
  18295. 15:39:19icon to create a folder and let's just
  18296. 15:39:22name this tests and press enter
  18297. 15:39:27and we're going to move this folder so
  18298. 15:39:29we'll drag it to the top level here
  18299. 15:39:32beside the HTML
  18300. 15:39:35files so we want the test folder to be
  18301. 15:39:38beside the scripts
  18302. 15:39:40folder next let's click on the test
  18303. 15:39:43folder and we're going to create our
  18304. 15:39:45first test file we'll click
  18305. 15:39:48this and we're going to test format
  18306. 15:39:51currency which is inside the money.jpg
  18307. 15:39:57money
  18308. 15:40:00test.js so we know that this file
  18309. 15:40:02contains tests for money. JS so let's
  18310. 15:40:06press
  18311. 15:40:08enter and inside this file we're going
  18312. 15:40:11to write some code to test format
  18313. 15:40:14currency so first let's import format
  18314. 15:40:16currency so we can test it at the top
  18315. 15:40:20we'll type import curly brackets format
  18316. 15:40:25currency
  18317. 15:40:27from a string and from here we're going
  18318. 15:40:30to locate money. JS so we start out in
  18319. 15:40:34money test.js and we need to get out of
  18320. 15:40:36the test folder so we'll type do do
  18321. 15:40:39slash to get out of this folder and then
  18322. 15:40:42we'll go into the scripts folder scripts
  18323. 15:40:46slash and then into the utils folder
  18324. 15:40:49utils slash and locat
  18325. 15:40:53money. Js
  18326. 15:40:57so as a reminder format currency takes a
  18327. 15:41:00number in cents like 295 cents and then
  18328. 15:41:04converts it into Dollars like
  18329. 15:41:09$20.95 so first let's test this function
  18330. 15:41:12using that number 2095 as an example
  18331. 15:41:16we're going to run the function format
  18332. 15:41:20currency brackets and give it
  18333. 15:41:24295 and now we're going to check if the
  18334. 15:41:27result of this function is correct to do
  18335. 15:41:30that we can just use an if statement so
  18336. 15:41:34we'll type if Open Bracket close
  18337. 15:41:38bracket and we said we expect format
  18338. 15:41:41currency
  18339. 15:41:42295 to give us the string
  18340. 15:41:5320959 and curly brackets and and if
  18341. 15:41:57these two are equal that means the code
  18342. 15:41:59is working correctly so we're going to
  18343. 15:42:02console. log the string
  18344. 15:42:07passed to say that this test has passed
  18345. 15:42:11now if it is not equal to
  18346. 15:42:2620.95%
  18347. 15:42:28failed and
  18348. 15:42:30save and that's it this is our first
  18349. 15:42:33automated test it checks if the result
  18350. 15:42:36of this function is correct if it is
  18351. 15:42:39correct we're going to display past and
  18352. 15:42:42if it's not correct we're going to
  18353. 15:42:43display
  18354. 15:42:45failed now let's run this test code so
  18355. 15:42:49remember we can't run JavaScript files
  18356. 15:42:52directly we need to load this Javascript
  18357. 15:42:55file using an HTML file so let's create
  18358. 15:42:59an HTML file to run this test inside the
  18359. 15:43:03test folder we're going to create a new
  18360. 15:43:05file by clicking up
  18361. 15:43:07here and let's name this file
  18362. 15:43:11tests.
  18363. 15:43:13HTML and press
  18364. 15:43:16enter and in here we're going to start
  18365. 15:43:18by creating a basic HTML structure so as
  18366. 15:43:22a reminder at the top we'll type less
  18367. 15:43:25than exclamation
  18368. 15:43:27dock type space HTML greater than and
  18369. 15:43:32we'll create an HTML
  18370. 15:43:35element and then inside we'll create a
  18371. 15:43:38head
  18372. 15:43:39element as well as a body
  18373. 15:43:44element and then inside the body we're
  18374. 15:43:46going to create a script tag to run
  18375. 15:43:49JavaScript
  18376. 15:43:50code and we're going to give the script
  18377. 15:43:52tag an attribute called SRC or source
  18378. 15:43:57equals double quotes and we're going to
  18379. 15:43:59give it this JavaScript file to
  18380. 15:44:01run money
  18381. 15:44:06test.js and also because this file uses
  18382. 15:44:09import we also need to add an attribute
  18383. 15:44:12called type equals modules to give this
  18384. 15:44:17file access to JavaScript module
  18385. 15:44:19features like
  18386. 15:44:21import so now let's
  18387. 15:44:23save and we're going to run this file in
  18388. 15:44:26live server so we're going to click it
  18389. 15:44:29and then
  18390. 15:44:30rightclick open with live
  18391. 15:44:33server and then we're going to check the
  18392. 15:44:35console by right
  18393. 15:44:37clicking
  18394. 15:44:39inspect and open the
  18395. 15:44:42console and our code display the text
  18396. 15:44:46passed so if we look at our
  18397. 15:44:48code and open money
  18398. 15:44:52test.js it's telling us that the result
  18399. 15:44:54of this function matched what we
  18400. 15:44:56expected
  18401. 15:44:57and this test
  18402. 15:44:59passed so that's how we run an automated
  18403. 15:45:02test we just load it like normal
  18404. 15:45:05JavaScript so as you can see automated
  18405. 15:45:08tests are pretty straightforward we're
  18406. 15:45:10just using code to test
  18407. 15:45:15code okay so I mentioned earlier that a
  18408. 15:45:18disadvantage of manual testing is it's
  18409. 15:45:22hard to test every
  18410. 15:45:24situation now let's solve this problem
  18411. 15:45:26using automated
  18412. 15:45:28tests so inside money
  18413. 15:45:31test.js if we want to test format
  18414. 15:45:33currency in different situations or with
  18415. 15:45:36different numbers we can just add some
  18416. 15:45:39more tests at the bottom so at the
  18417. 15:45:41bottom let's create some new lines and
  18418. 15:45:44we'll just add another test using IF
  18419. 15:45:47brackets and curly
  18420. 15:45:49brackets and inside here let's test
  18421. 15:45:52format currency with the number zero so
  18422. 15:45:55we'll type format
  18423. 15:45:57currency brackets 0 and we expect this
  18424. 15:46:02result to be the string 0.0 0 so we'll
  18425. 15:46:07check if this triple equals the string
  18426. 15:46:120. if this result matches we're going to
  18427. 15:46:17console.log the string
  18428. 15:46:21passed and if this result does not match
  18429. 15:46:25we're going to type else curly brackets
  18430. 15:46:29and
  18431. 15:46:31console.log the string
  18432. 15:46:35failed so that's how we test format
  18433. 15:46:37currency in a different situation with
  18434. 15:46:40the number zero so now let's
  18435. 15:46:43save and then go back to our test page
  18436. 15:46:47which is here and then open the console
  18437. 15:46:50right click
  18438. 15:46:52inspect the
  18439. 15:46:53console and it will tell us that both of
  18440. 15:46:56our automated tests
  18441. 15:46:58passed so as you can see automated tests
  18442. 15:47:02help us test our code in different
  18443. 15:47:04situations very easily this is a lot
  18444. 15:47:07faster than manually opening our website
  18445. 15:47:09and setting it up to test with different
  18446. 15:47:13numbers so let's go back to our
  18447. 15:47:16code and by the way another name for a
  18448. 15:47:19situation that we're testing is called a
  18449. 15:47:22test case so here instead of saying
  18450. 15:47:26we're testing two different situations
  18451. 15:47:28we usually say we're testing two test
  18452. 15:47:32cases okay now let's get more practice
  18453. 15:47:35and add another test case at the bottom
  18454. 15:47:39of this file we're going to create
  18455. 15:47:41another if statement if brackets and
  18456. 15:47:44curly
  18457. 15:47:46brackets and this time let's test format
  18458. 15:47:51currency brackets and we'll give it a
  18459. 15:47:54number that requires some rounding
  18460. 15:47:57so when we calculate the 10% tax on our
  18461. 15:48:00website the result could be a decimal
  18462. 15:48:03like the number
  18463. 15:48:062000.5 if we use the number
  18464. 15:48:102.5 this will convert into $20 and
  18465. 15:48:150.5 however we can't have
  18466. 15:48:180.5 so we're going to round this up to 1
  18467. 15:48:22cent so we're going to have $20 And1
  18468. 15:48:27so let's check if this result triple
  18469. 15:48:29equals the string
  18470. 15:48:3320.1 and if it is equal we're going to
  18471. 15:48:38console.log
  18472. 15:48:40past and if it is not equal else curly
  18473. 15:48:45brackets
  18474. 15:48:48console.log
  18475. 15:48:50failed and
  18476. 15:48:52save and open our test page and open the
  18477. 15:48:56con
  18478. 15:48:59and you can see that all three of our
  18479. 15:49:01test cases passed and that's it we just
  18480. 15:49:04created three test cases for format
  18481. 15:49:07currency using automated
  18482. 15:49:10tests now let's go back to our
  18483. 15:49:15code and currently we have three test
  18484. 15:49:18cases and you might be wondering how
  18485. 15:49:20many test cases should we have generally
  18486. 15:49:24we create two types of test cases basic
  18487. 15:49:28test cases and edge
  18488. 15:49:30cases a basic test case just tests if
  18489. 15:49:34the code is working or not so here our
  18490. 15:49:37first test case is a basic test case we
  18491. 15:49:40use a normal number like
  18492. 15:49:432095 and we see if format currency is
  18493. 15:49:46working
  18494. 15:49:47correctly the second type of test cases
  18495. 15:49:50that we should have are edge cases this
  18496. 15:49:53means we test a code with values that
  18497. 15:49:56are a little bit bit tricky they're on
  18498. 15:49:58the edge of what our code can handle for
  18499. 15:50:01example in our test here the last two
  18500. 15:50:04test cases are edge cases zero is sort
  18501. 15:50:07of a special number that's not positive
  18502. 15:50:10or negative and
  18503. 15:50:132.5 is a number that requires some
  18504. 15:50:15rounding so these numbers are acceptable
  18505. 15:50:18we can use them with format currency but
  18506. 15:50:21they're a little bit tricky and on the
  18507. 15:50:23edge of what our code can handle
  18508. 15:50:27so when creating test cases make sure to
  18509. 15:50:30create basic test cases as well as edge
  18510. 15:50:32cases if there are any edge
  18511. 15:50:37cases now we could have added another
  18512. 15:50:39basic test case like format currency
  18513. 15:50:431095 however this is very similar to
  18514. 15:50:472095 it's basically testing the same
  18515. 15:50:49thing so we don't need another test case
  18516. 15:50:52like this generally we should try to
  18517. 15:50:54test something different in each test
  18518. 15:50:57case for example an edge case that we
  18519. 15:51:01have not tested is a number
  18520. 15:51:04200.4 so this would check if the code
  18521. 15:51:07rounds down correctly to $20
  18522. 15:51:11and0 however I'll leave creating this
  18523. 15:51:13test case as an
  18524. 15:51:16exercise now let's move on and talk
  18525. 15:51:19about the second disadvantage of manual
  18526. 15:51:22tests which is it's hard to retest the
  18527. 15:51:25code automated test also solve this
  18528. 15:51:28problem for example let's say that we
  18529. 15:51:31Chang the format currency function so
  18530. 15:51:34inside UIL money.jpg
  18531. 15:51:56we would have to open up the website set
  18532. 15:51:59up everything manually and then click
  18533. 15:52:01around to see if it's
  18534. 15:52:03working however with automated testing
  18535. 15:52:06all we need to do is to rerun all of
  18536. 15:52:09this test code and it will instantly
  18537. 15:52:12retest all of these cases so let's go
  18538. 15:52:15back to money. JS and save this
  18539. 15:52:19code and then go into our test
  18540. 15:52:23page and check our console right click
  18541. 15:52:27inspect the
  18542. 15:52:30console and you can see that it
  18543. 15:52:32instantly reruns all the test cases and
  18544. 15:52:35it tells us that the last test case
  18545. 15:52:37failed so automated tests make it really
  18546. 15:52:39easy to retest our code after we make
  18547. 15:52:42any
  18548. 15:52:44changes okay now we have another problem
  18549. 15:52:48here we can see that the test Cas is
  18550. 15:52:50passed passed and failed but it's not
  18551. 15:52:53clear which ones passed and which ones
  18552. 15:52:55failed we don't really have a name for
  18553. 15:52:58these tests so we don't know what
  18554. 15:53:00they're doing so that's what we're going
  18555. 15:53:02to add next we're going to give each of
  18556. 15:53:04these tests a name so let's go back to
  18557. 15:53:07our
  18558. 15:53:08code and open money
  18559. 15:53:12test.js and now we're going to give each
  18560. 15:53:14of these test cases a name so above the
  18561. 15:53:17first test
  18562. 15:53:19case let's just
  18563. 15:53:21console.
  18564. 15:53:23log a string and we're going to give
  18565. 15:53:26this test a name so the name should
  18566. 15:53:29describe what this test is doing so in
  18567. 15:53:32here let's just use the name converts
  18568. 15:53:36cents into
  18569. 15:53:39dollars and that's how we give a test
  18570. 15:53:41case a name let's do the same thing with
  18571. 15:53:44the other two test cases so down here
  18572. 15:53:48we're going to console.
  18573. 15:53:52log brackets and a string and we give
  18574. 15:53:56the second test case a name so this one
  18575. 15:53:59tests the number zero so let's use the
  18576. 15:54:02name works with zero so again the name
  18577. 15:54:08just describes what the test is
  18578. 15:54:11doing and let's do the same thing for
  18579. 15:54:13the third test case we
  18580. 15:54:17console.log a string and here we're
  18581. 15:54:21testing if the function rounds up to the
  18582. 15:54:23nearest scent so let's use the name
  18583. 15:54:27rounds up to the nearest
  18584. 15:54:32scent and
  18585. 15:54:34save if you're wondering how I came up
  18586. 15:54:37with these names a naming convention
  18587. 15:54:39that we use is to describe what the code
  18588. 15:54:42is doing for example the code works with
  18589. 15:54:46zero or the code rounds up to the
  18590. 15:54:49nearest scent and then we remove the
  18591. 15:54:52code some people also like to use the
  18592. 15:54:54word it instead of the
  18593. 15:54:59code so now each of these test cases has
  18594. 15:55:02a name above it if we open up our test
  18595. 15:55:05page and open the
  18596. 15:55:08console now we can see exactly which
  18597. 15:55:11test case passed and which test case
  18598. 15:55:14failed so this failed test case tells us
  18599. 15:55:17we have a rounding problem in our code
  18600. 15:55:20so now we can go back to our code into
  18601. 15:55:23money. JS and investigate the rounding
  18602. 15:55:26problem so of course we removed
  18603. 15:55:29math.round as an example we're just
  18604. 15:55:31going to add it back to fix that failing
  18605. 15:55:34test capital m
  18606. 15:55:38math.round Open Bracket and close
  18607. 15:55:42bracket and
  18608. 15:55:45save and then open the console for our
  18609. 15:55:48test which is here and now all of our
  18610. 15:55:51test cases
  18611. 15:55:54passed next another thing we do when
  18612. 15:55:57naming our tests is to group related
  18613. 15:56:00tests together for example these three
  18614. 15:56:04test cases are for the format currency
  18615. 15:56:07function so we're going to group these
  18616. 15:56:09tests together and give this group a
  18617. 15:56:12name by the way a group of related tests
  18618. 15:56:16is called a test
  18619. 15:56:20Suite so let's go back to our code and
  18620. 15:56:23open Money test.js
  18621. 15:56:26and at the top let's give this group of
  18622. 15:56:28tests or this test Suite A Name by
  18623. 15:56:32typing
  18624. 15:56:34console.log brackets and a string we're
  18625. 15:56:38going to name it test Suite colon format
  18626. 15:56:45currency and
  18627. 15:56:47save so now let's open the console for
  18628. 15:56:50our
  18629. 15:56:52tests and now at the top it will show us
  18630. 15:56:55test site format currency so this makes
  18631. 15:56:58it clear that this group of tests here
  18632. 15:57:01are for the format currency
  18633. 15:57:04function in the future if we are testing
  18634. 15:57:06some other code for example a calculated
  18635. 15:57:09tax function we can create another group
  18636. 15:57:12of tests or another test Suite like this
  18637. 15:57:16and this makes it clear that the second
  18638. 15:57:18group of tests is for the calculate tax
  18639. 15:57:21function so this is how we name our
  18640. 15:57:24tests and group our tests together
  18641. 15:57:26together to make it more clear what is
  18642. 15:57:28being tested and which tests have passed
  18643. 15:57:31or
  18644. 15:57:33failed and now we created our first set
  18645. 15:57:36of automated tests as you can see
  18646. 15:57:39automated tests are basically using code
  18647. 15:57:42to test code and they make it really
  18648. 15:57:45easy to check if our code is working
  18649. 15:57:47correctly so now let's save this into
  18650. 15:57:50git we're going to open the git
  18651. 15:57:54section and our first change in money
  18652. 15:57:57test.js we created the tests for format
  18653. 15:58:00currency and then we created an HTML
  18654. 15:58:03file to load our test
  18655. 15:58:06code so let's create a message
  18656. 15:58:08describing these
  18657. 15:58:10changes create automated tests for
  18658. 15:58:15format
  18659. 15:58:17currency and
  18660. 15:58:24commit next we're going to learn how to
  18661. 15:58:26use a testing framework to write
  18662. 15:58:29automated tests so what is a testing
  18663. 15:58:32framework in the previous lesson we
  18664. 15:58:34learned about external libraries which
  18665. 15:58:37is code that is outside of our project
  18666. 15:58:40that someone else wrote and we can load
  18667. 15:58:43external libraries into our project and
  18668. 15:58:46use
  18669. 15:58:47them so a testing framework is an
  18670. 15:58:50external library that helps us write
  18671. 15:58:53tests
  18672. 15:58:54easier let's go back to our code and
  18673. 15:58:57we're going to open the money test.js
  18674. 15:59:00file and notice that in our tests we
  18675. 15:59:03need to do several things first we
  18676. 15:59:06create a test suite and we give it a
  18677. 15:59:08name and then we create some tests and
  18678. 15:59:11for each test we give it a name and then
  18679. 15:59:14we compare the values and display the
  18680. 15:59:17result in the console so a testing
  18681. 15:59:20framework helps us do all of these
  18682. 15:59:22things automatically and gives us many
  18683. 15:59:25other features so now let's learn our
  18684. 15:59:27first testing
  18685. 15:59:29framework in this course we're going to
  18686. 15:59:31use a popular testing framework called
  18687. 15:59:34Jasmine now if you're working at a
  18688. 15:59:36company in the future you'll definitely
  18689. 15:59:38be using a testing framework but it
  18690. 15:59:40might not always be Jasmine and that's
  18691. 15:59:43okay because most testing Frameworks are
  18692. 15:59:45pretty similar so by learning Jasmine
  18693. 15:59:48you'll be able to easily use other
  18694. 15:59:50testing Frameworks in the
  18695. 15:59:53future to load Jasmine into our project
  18696. 15:59:57we're going to go into the video
  18697. 15:59:58description and we're going to click
  18698. 16:00:00this link to download
  18699. 16:00:13Jasmine so once that link opens we're
  18700. 16:00:16going to be on this page and we're going
  18701. 16:00:18to scroll down and we're going to click
  18702. 16:00:20this Jasmine DST standalone. zip file to
  18703. 16:00:23download it
  18704. 16:00:27now once that's finished downloading
  18705. 16:00:29we're going to open our downloads
  18706. 16:00:34folder and if you're on Windows we're
  18707. 16:00:36going to rightclick the zip file and
  18708. 16:00:40click extract
  18709. 16:00:41all and click
  18710. 16:00:45extract now if you don't have that
  18711. 16:00:47extract all option you can also create a
  18712. 16:00:49folder with this name and then double
  18713. 16:00:52click the zip file and drag everything
  18714. 16:00:54into that folder
  18715. 16:00:57if you're using a Mac you can just
  18716. 16:00:58doubleclick the zip
  18717. 16:01:03file so this folder contains the code
  18718. 16:01:05for Jasmine and we're going to drag this
  18719. 16:01:08folder into our project so let's open
  18720. 16:01:11the code for our
  18721. 16:01:13project and then we're just going to
  18722. 16:01:15drag this folder down
  18723. 16:01:19here and click copy
  18724. 16:01:23folder so just make sure that this fold
  18725. 16:01:25folder is beside the scripts and images
  18726. 16:01:29folder next we're going to rename this
  18727. 16:01:31folder so we'll right click
  18728. 16:01:35rename and let's rename it to
  18729. 16:01:38test- Jasmine to make it clear this
  18730. 16:01:41folder contains test created with
  18731. 16:01:44Jasmine so now let's press
  18732. 16:01:48enter and now we have Jasmine inside our
  18733. 16:01:51project so inside the lip folder we have
  18734. 16:01:55some JavaScript files here and this is
  18735. 16:01:58the code for the Jasmine testing
  18736. 16:02:00framework so this is another way to load
  18737. 16:02:02an external Library into our project we
  18738. 16:02:05can just download all the code and put
  18739. 16:02:07it into our project and this is useful
  18740. 16:02:10if an external library has lots of
  18741. 16:02:12different files like
  18742. 16:02:14this before we continue let's save the
  18743. 16:02:17changes into git so if we make any
  18744. 16:02:20changes later it will be easier to see
  18745. 16:02:22them we'll open the git section
  18746. 16:02:25and then we just added all the code for
  18747. 16:02:28Jasmine so at the top let's type the
  18748. 16:02:31message add Jasmin testing
  18749. 16:02:36framework and
  18750. 16:02:42commit now that we loaded Jasmine into
  18751. 16:02:45our project let's learn how to use it so
  18752. 16:02:48Jasmine has a documentation page that
  18753. 16:02:50shows you how to use the testing
  18754. 16:02:52framework however to save time I'm just
  18755. 16:02:55going to show you the features that we
  18756. 16:02:56need and you can read the documentation
  18757. 16:02:58later if you want so first let's learn
  18758. 16:03:01how to run tests using
  18759. 16:03:03Jasmine I'm going to close this for now
  18760. 16:03:07and then in our code we're going to open
  18761. 16:03:09the test- Jasmin folder and at the
  18762. 16:03:13bottom we're going to open this spec
  18763. 16:03:15runner. HTML
  18764. 16:03:17file so this file loads all of the
  18765. 16:03:21tests so this does the same thing as our
  18766. 16:03:23test.html file from before and by the
  18767. 16:03:27way in Jasmine spec is another name for
  18768. 16:03:31test so spec Runner just means test
  18769. 16:03:35Runner this file runs all the
  18770. 16:03:38tests so let's actually rename this file
  18771. 16:03:41to test.html so we can match what we had
  18772. 16:03:44before and compare the two so here we're
  18773. 16:03:48going to right click and
  18774. 16:03:51rename
  18775. 16:03:53tests. HTML and press
  18776. 16:03:57enter and now let's open this file and
  18777. 16:03:59take a look at the code that's
  18778. 16:04:01inside so here at the top this is the
  18779. 16:04:05code for the Jasmine testing framework
  18780. 16:04:08so we're just loading the framework up
  18781. 16:04:11here below that here we have the code
  18782. 16:04:15that we want to test so this code is
  18783. 16:04:18sort of like the money JS file that we
  18784. 16:04:21were testing
  18785. 16:04:22earlier and then at the bottom these
  18786. 16:04:25files contain the tests so these files
  18787. 16:04:28are sort of like the money test.js file
  18788. 16:04:30that we had earlier and you'll notice
  18789. 16:04:33that these are in a spec folder so as a
  18790. 16:04:35reminder in Jasmine spec is another name
  18791. 16:04:39for test so this is basically the test
  18792. 16:04:41folder this folder contains all of our
  18793. 16:04:46tests so now let's run these tests and
  18794. 16:04:49to do that we just need to open this
  18795. 16:04:51HTML file in live server just like we
  18796. 16:04:54did before
  18797. 16:04:56so for this file we're going to right
  18798. 16:04:59click and open with live
  18799. 16:05:05server and I'm just going to make this a
  18800. 16:05:07little bigger by pressing control equals
  18801. 16:05:10so it's easier to see in the
  18802. 16:05:12video so this is what Jasmine tests look
  18803. 16:05:15like up here we have five Green Dots
  18804. 16:05:19this means we have five tests that we
  18805. 16:05:21ran and the green color means that all
  18806. 16:05:23of them passed so that here each test
  18807. 16:05:27has a Green Dot and you'll notice that
  18808. 16:05:29each test also has a name next to it
  18809. 16:05:32which tells us what the test does and
  18810. 16:05:35don't worry if this is in a different
  18811. 16:05:36order for you Jasmine randomizes the
  18812. 16:05:39order of the
  18813. 16:05:40tests also notice that some of these
  18814. 16:05:43tests are grouped together into a test
  18815. 16:05:46suite and up here we have the name of
  18816. 16:05:49the test Suite so as you can see a
  18817. 16:05:52testing framework helps us do all of the
  18818. 16:05:55things we were doing before and it also
  18819. 16:05:57provides a nice looking website where we
  18820. 16:05:59can see the test results so a testing
  18821. 16:06:02framework is just an external library
  18822. 16:06:05that helps us write tests
  18823. 16:06:11easier now that we know what Jasmine and
  18824. 16:06:14a testing framework is let's learn how
  18825. 16:06:16to write our tests using
  18826. 16:06:19Jasmine first we're going to go back to
  18827. 16:06:21our
  18828. 16:06:23code and we're going to remove the
  18829. 16:06:25example code provided by
  18830. 16:06:28Jasmine so down here we'll remove these
  18831. 16:06:31four script
  18832. 16:06:34texts and let's also remove the source
  18833. 16:06:37folder so we'll right click
  18834. 16:06:41delete as well as the spec folder right
  18835. 16:06:44click
  18836. 16:06:47delete but make sure we keep this lib
  18837. 16:06:49folder because this contains a code for
  18838. 16:06:53Jasmine and now inside test Jasmin we're
  18839. 16:06:56going to create a new file for our test
  18840. 16:06:59so we'll create a new
  18841. 16:07:01file and let's say that we're testing
  18842. 16:07:04money. JS again so let's name it the
  18843. 16:07:06same thing here money
  18844. 16:07:11test.js and press
  18845. 16:07:14enter and now let's create the same test
  18846. 16:07:18we had before by using Jasmine this time
  18847. 16:07:21so to make it easier for the video I'm
  18848. 16:07:24just going to take this file and drag it
  18849. 16:07:26to the top here so we can see these two
  18850. 16:07:29side by
  18851. 16:07:34side so like before we're going to
  18852. 16:07:36create tests for format currency first
  18853. 16:07:39we'll need to import this function at
  18854. 16:07:41the top so let's just actually select
  18855. 16:07:45this and right click
  18856. 16:07:48copy and then down here right click and
  18857. 16:07:53paste and just make sure that this file
  18858. 16:07:56path still works so our money test.js
  18859. 16:07:59file has to be inside test- Jasmin and
  18860. 16:08:03this folder should be beside the
  18861. 16:08:05previous test
  18862. 16:08:10folder next we're going to create a test
  18863. 16:08:12suite and name the test Suite so in
  18864. 16:08:16Jasmine to create a test Suite we're
  18865. 16:08:18going to use a function called
  18866. 16:08:22describe so this describe function is
  18867. 16:08:25provided by Jasmine and it creates a
  18868. 16:08:28test
  18869. 16:08:30Suite next we're going to give a name to
  18870. 16:08:33the test Suite so to name a test Suite
  18871. 16:08:35in Jasmine we're going to give describe
  18872. 16:08:37a string and this string is the name of
  18873. 16:08:40the test Suite so let's name it the same
  18874. 16:08:43thing as before test Suite colon format
  18875. 16:08:52currency next we're going to add some
  18876. 16:08:55tests inside the test Suite to do that
  18877. 16:08:59we're going to give describe a second
  18878. 16:09:01parameter which is a function brackets
  18879. 16:09:05arrow and curly
  18880. 16:09:09brackets and inside here we're going to
  18881. 16:09:12create a test and give the test a name
  18882. 16:09:16to create a test in Jasmine we're going
  18883. 16:09:18to use another function called it and
  18884. 16:09:23brackets so it is a another function
  18885. 16:09:26provided by Jasmine and this creates a
  18886. 16:09:30test next let's give a name to our test
  18887. 16:09:34so that we know what it's doing to name
  18888. 16:09:36a test we're going to give it a string
  18889. 16:09:40and this is going to be the test name so
  18890. 16:09:42let's name it the same thing we had
  18891. 16:09:44before
  18892. 16:09:46converts sense into
  18893. 16:09:51dollars and now we're going to add some
  18894. 16:09:54code into our tests to do that in
  18895. 16:09:57Jasmine we're going to give it a second
  18896. 16:09:59parameter which is another function
  18897. 16:10:03brackets arrow and curly
  18898. 16:10:06brackets so this is the code inside the
  18899. 16:10:09test and in this test we need to compare
  18900. 16:10:12these two values and display the result
  18901. 16:10:16in Jasmine instead of creating an if
  18902. 16:10:18statement and displaying the result
  18903. 16:10:20ourselves Jasmine provides us another
  18904. 16:10:23function to do all of this
  18905. 16:10:26and that function is called expect and
  18906. 16:10:31brackets so expect lets us compare a
  18907. 16:10:34value to another value for example let's
  18908. 16:10:38give expect the first value that we want
  18909. 16:10:40to compare which is format currency
  18910. 16:10:442095 so we'll type format
  18911. 16:10:48currency brackets
  18912. 16:10:52295 and now to compare this value to
  18913. 16:10:55another value expect gives us an
  18914. 16:10:59object and this object has many methods
  18915. 16:11:02we can use to do
  18916. 16:11:04comparisons so one of the methods we can
  18917. 16:11:06use in this object is dot 2 equal
  18918. 16:11:13brackets and then inside these brackets
  18919. 16:11:16we'll put another value that we want to
  18920. 16:11:18compare this to so we want to compare
  18921. 16:11:21this to the string 2095
  18922. 16:11:26and that's it this checks if this value
  18923. 16:11:29is equal to this value and then displays
  18924. 16:11:32the result on the page so it does
  18925. 16:11:35everything that we did before except
  18926. 16:11:37with one line of code and you'll also
  18927. 16:11:40notice that jasmin's code is designed so
  18928. 16:11:43that it reads like English for example
  18929. 16:11:46we expect format currency
  18930. 16:11:482095 to equal this string and this makes
  18931. 16:11:52a test code easier to read
  18932. 16:11:56so we just created our first test suite
  18933. 16:11:58and our first test using
  18934. 16:12:01Jasmine now let's run this test to do
  18935. 16:12:05that we just need to run this Javascript
  18936. 16:12:07file inside test.html
  18937. 16:12:10so first let's save this
  18938. 16:12:13file and then inside test.html we're
  18939. 16:12:16going to load this file so in here at
  18940. 16:12:20the bottom we're going to create a
  18941. 16:12:23script tag
  18942. 16:12:26and we'll give it a source attribute SRC
  18943. 16:12:29equals double
  18944. 16:12:31quotes and we're going to load money
  18945. 16:12:35test.js
  18946. 16:12:39test.js and remember our test file uses
  18947. 16:12:42import so we also need to give it the
  18948. 16:12:45attribute type equals module so this
  18949. 16:12:49file has access to JavaScript module
  18950. 16:12:52features so now let's save
  18951. 16:12:56and we'll open this file in our browser
  18952. 16:12:59so we should have it open from before if
  18953. 16:13:01you don't you can write click and open
  18954. 16:13:04with live
  18955. 16:13:06server so we'll open the test page and
  18956. 16:13:09now you can see that we have our one
  18957. 16:13:11test that we created here and it's
  18958. 16:13:13passing so it's green you can see here
  18959. 16:13:16that we created a test Suite with a name
  18960. 16:13:20and we also created a test with this
  18961. 16:13:22name so we just created the first test
  18962. 16:13:26that we had before except using Jasmine
  18963. 16:13:28this time and you can see that Jasmine
  18964. 16:13:31also creates a nice website for us to
  18965. 16:13:33see the test
  18966. 16:13:36results okay now that we learned how to
  18967. 16:13:38use Jasmine to create a test let's get
  18968. 16:13:41some practice and create the rest of our
  18969. 16:13:43tests from before we'll go back to our
  18970. 16:13:47code and open money
  18971. 16:13:51test.js and here we're going to create
  18972. 16:13:54the other two tests that we had here so
  18973. 16:13:57at the bottom inside the same test Suite
  18974. 16:14:01we're going to create another test using
  18975. 16:14:03the function it brackets and we'll give
  18976. 16:14:07it a string which is the test name and
  18977. 16:14:11let's name it the same as
  18978. 16:14:12before works with zero comma brackets
  18979. 16:14:18arrow and curly
  18980. 16:14:20brackets and now we can put something
  18981. 16:14:22inside this test so in the second test
  18982. 16:14:25we're going to compare these two values
  18983. 16:14:28to compare values we're going to use the
  18984. 16:14:30function expect
  18985. 16:14:34brackets and the first value is going to
  18986. 16:14:36be format
  18987. 16:14:40currency brackets
  18988. 16:14:42zero and then expect gives us an
  18989. 16:14:46object and this object has a method
  18990. 16:14:49called dot to equal which lets us
  18991. 16:14:53compare this value to another value
  18992. 16:14:55so we'll call this method using brackets
  18993. 16:14:58and compare to the string the string 0 0
  18994. 16:15:030 and now let's
  18995. 16:15:06save and live server will update our web
  18996. 16:15:09page automatically so we'll open the
  18997. 16:15:11test page and now we have two tests and
  18998. 16:15:14they both passed so that's how we create
  18999. 16:15:17the second test using Jasmine and again
  19000. 16:15:20don't worry if this is in a different
  19001. 16:15:22order for you Jasmine randomizes the
  19002. 16:15:24order of the
  19003. 16:15:25tests finally let's create that third
  19004. 16:15:28test that we had before we'll go back to
  19005. 16:15:30our code and then in our old money test
  19006. 16:15:34file we're going to create this third
  19007. 16:15:36test feel free to pause the video if you
  19008. 16:15:39want to give it a try
  19009. 16:15:40yourself so we'll scroll down and we'll
  19010. 16:15:44create another test inside this test
  19011. 16:15:46Suite using the function it brackets and
  19012. 16:15:50a string and we'll give it the same test
  19013. 16:15:53name as before
  19014. 16:15:55rounds up to the
  19015. 16:15:59nearest set comma brackets arrow and
  19016. 16:16:04curly
  19017. 16:16:05brackets and we're going to compare
  19018. 16:16:07these two values so we use exp brackets
  19019. 16:16:13and give it the first value format
  19020. 16:16:16currency brackets
  19021. 16:16:192000.5 and to compare it we'll use the
  19022. 16:16:22method
  19023. 16:16:23do2 equal brackets and compare it to
  19024. 16:16:3120.01 and
  19025. 16:16:33save and let's go back to our test page
  19026. 16:16:37and now we have three tests and all
  19027. 16:16:39three of them passed so we recreated the
  19028. 16:16:43tests that we had
  19029. 16:16:45before now let's try making one of these
  19030. 16:16:48tests fail to see what it looks like in
  19031. 16:16:50Jasmine we'll open our
  19032. 16:16:53code and and let's open the file utils
  19033. 16:17:12money.jpg
  19034. 16:17:21test so it tells us the test Suite name
  19035. 16:17:24and the test name that failed and it
  19036. 16:17:26will also tell us the two values that
  19037. 16:17:28are not equal to each other and here it
  19038. 16:17:31will tell us where this test failed so
  19039. 16:17:33it tell us that it failed in money
  19040. 16:17:35test.js at line
  19041. 16:17:3813 so if we go back to our
  19042. 16:17:41code and we open the Jasmine money
  19043. 16:17:45test.js line 13 is right here so just
  19044. 16:17:48telling us that this line failed the
  19045. 16:17:50test
  19046. 16:17:55here we can see the full list of tests
  19047. 16:17:58and it will tell us that we have three
  19048. 16:18:00tests two of them passed and one of them
  19049. 16:18:04failed so Jasmine gives us a lot of
  19050. 16:18:06details on where a test failed to help
  19051. 16:18:09us fix the
  19052. 16:18:10issue now let's add back math.round to
  19053. 16:18:14make everything pass again we'll open
  19054. 16:18:17our code and open UTS money.jpg
  19055. 16:18:26math.round Open Bracket and close
  19056. 16:18:30bracket and
  19057. 16:18:32save and if we check our test page
  19058. 16:18:35everything passes
  19059. 16:18:37again so this is how we use a testing
  19060. 16:18:40framework like Jasmine to create tests
  19061. 16:18:43the testing framework gives us a bunch
  19062. 16:18:46of functions such as describe it and
  19063. 16:18:49expect and then we use these functions
  19064. 16:18:52to create our test so so these functions
  19065. 16:18:55have a lot of features inside them they
  19066. 16:18:58do all of the setup for us and they also
  19067. 16:19:01display the test on a nice web page for
  19068. 16:19:04us now there's one more thing I want to
  19069. 16:19:06mention which is we can use describe
  19070. 16:19:09inside describe this helps us organize
  19071. 16:19:12our tests
  19072. 16:19:16further so now let's save our changes
  19073. 16:19:18into git we'll open the git
  19074. 16:19:22section and let's close this file at the
  19075. 16:19:24top for
  19076. 16:19:25now so inside money test.js we created
  19077. 16:19:30all the tests that we had before for
  19078. 16:19:32format currency by using Jasmine this
  19079. 16:19:35time and for spec Runner we rename this
  19080. 16:19:38file to test.html and finally we deleted
  19081. 16:19:42all the test code that came with Jasmine
  19082. 16:19:46so now let's add a message to describe
  19083. 16:19:48our
  19084. 16:19:48changes
  19085. 16:19:50remove default tests and add
  19086. 16:19:55money tests and
  19087. 16:20:04commit now let's test a different
  19088. 16:20:06function using Jasmine and along the way
  19089. 16:20:09we're going to learn some more features
  19090. 16:20:11of testing
  19091. 16:20:13Frameworks let's go back to our
  19092. 16:20:16code and we're going to scroll to the
  19093. 16:20:18top and open the data/ cart. JS
  19094. 16:20:23file so inside this file we have a
  19095. 16:20:26function called add to cart so we're
  19096. 16:20:29going to create tests for the add to
  19097. 16:20:31cart
  19098. 16:20:32function first since this function is in
  19099. 16:20:35a different file we're going to create a
  19100. 16:20:38new test file so we're going to scroll
  19101. 16:20:41down and let's close the test folder so
  19102. 16:20:44we don't get confused and then inside
  19103. 16:20:47test- Jasmine we're going to click in
  19104. 16:20:50this folder and then create a new file
  19105. 16:20:53and let's name it cart
  19106. 16:20:58test.js and press
  19107. 16:21:00enter so we name it the original file
  19108. 16:21:04name which is cart and then just add
  19109. 16:21:06test at the
  19110. 16:21:08end and to better organize our test
  19111. 16:21:11files we usually try to match the folder
  19112. 16:21:15structure of the code so for example in
  19113. 16:21:18our code money. JS is inside this UIL
  19114. 16:21:22folder so inside our tests we should
  19115. 16:21:25also create a utils folder and put this
  19116. 16:21:28file inside so we'll create a new folder
  19117. 16:21:31by clicking up here and let's name it
  19118. 16:21:34utils and press
  19119. 16:21:37enter and then move the money test.js
  19120. 16:21:40file
  19121. 16:21:43inside if it asks you if you want to
  19122. 16:21:46update Imports automatically you can
  19123. 16:21:48click
  19124. 16:21:49yes if you check your code scroll to the
  19125. 16:21:53top make sure that your file path starts
  19126. 16:21:56with do do slash do do slash because now
  19127. 16:21:58we're inside another
  19128. 16:22:01folder VSS code sometimes updates the
  19129. 16:22:03file paths for us automatically but if
  19130. 16:22:06it doesn't we can just add an extra dot
  19131. 16:22:08do slash in front
  19132. 16:22:12ourselves and we're also going to open
  19133. 16:22:14money test.js and save this
  19134. 16:22:19file next if we scroll up cart. JS is in
  19135. 16:22:24inside a data folder so inside our tests
  19136. 16:22:28we're also going to create a data folder
  19137. 16:22:30let's click cart
  19138. 16:22:32test.js we'll create a new folder and
  19139. 16:22:35let's name this the data folder and
  19140. 16:22:38press
  19141. 16:22:40enter and then move card test.js inside
  19142. 16:22:48here now you'll notice that this folder
  19143. 16:22:50structure doesn't perfectly match the
  19144. 16:22:52folder structure that we have up here
  19145. 16:22:54because utils is inside the scripts
  19146. 16:22:57folder but usually this data folder
  19147. 16:22:59should also be inside the scripts folder
  19148. 16:23:02I just made a mistake when structuring
  19149. 16:23:04the
  19150. 16:23:05project so let's just use this folder
  19151. 16:23:07structure in our
  19152. 16:23:10tests okay so now let's open cart
  19153. 16:23:14test.js and now we're ready to test the
  19154. 16:23:17add to cart
  19155. 16:23:18function so first let's import add to
  19156. 16:23:22cart at the top we'll import
  19157. 16:23:25curly brackets add to
  19158. 16:23:29cart from a
  19159. 16:23:31string and we're going to locate cart.
  19160. 16:23:34JS so first we need to get out of the
  19161. 16:23:37data folder using do slash and then get
  19162. 16:23:41out of the test djine folder do do slash
  19163. 16:23:45and we'll go into this data folder data
  19164. 16:23:48SL cart.
  19165. 16:23:51JS next let's create a test suite for
  19166. 16:23:55this function so we'll create some new
  19167. 16:23:57lines and to create a test Suite in
  19168. 16:24:00Jasmine we'll use the function
  19169. 16:24:03describe
  19170. 16:24:05brackets and we'll give this test Suite
  19171. 16:24:07A Name by giving it a
  19172. 16:24:10string so let's use the name test Suite
  19173. 16:24:15colon add to
  19174. 16:24:18cart and we'll give it a second
  19175. 16:24:20parameter which is a function brackets
  19176. 16:24:24arrow and curly
  19177. 16:24:27brackets and now inside this test Suite
  19178. 16:24:30we're going to add some tests so let's
  19179. 16:24:33look at the code for add to cart to see
  19180. 16:24:35what kind of tests we should
  19181. 16:24:37add so we'll open data/ cart.
  19182. 16:24:41JS and then inside add to cart you'll
  19183. 16:24:45notice that this function uses an if
  19184. 16:24:48statement a best practice in testing is
  19185. 16:24:51to test each condition of the if
  19186. 16:24:53statement
  19187. 16:24:54this is known as test coverage test
  19188. 16:24:57coverage means how much of the code is
  19189. 16:25:00being tested and a best practice is we
  19190. 16:25:03try to maximize test
  19191. 16:25:07coverage so for this if statement we'll
  19192. 16:25:09create a test for the first condition
  19193. 16:25:12which is when the product is already in
  19194. 16:25:14the cart and we'll create a test for the
  19195. 16:25:17else condition when the product is not
  19196. 16:25:19in the
  19197. 16:25:22cart let's go back to cart
  19198. 16:25:28test.js and inside this test Suite we're
  19199. 16:25:31going to create a test using the
  19200. 16:25:33function it brackets and we'll give it a
  19201. 16:25:37string and this will be the test name so
  19202. 16:25:41for the video I'm just going to scroll
  19203. 16:25:43up and take this cart. JS file and put
  19204. 16:25:46it at the top here so we can see our
  19205. 16:25:49code and our test side by side so I'm
  19206. 16:25:52going to scroll down to the add to card
  19207. 16:25:55function and we mentioned that we're
  19208. 16:25:57going to test each condition of this if
  19209. 16:26:00statement so the first test is going to
  19210. 16:26:03be when the product is already in the
  19211. 16:26:05cart so that here let's name it ads and
  19212. 16:26:11existing product to the cart comma
  19213. 16:26:16brackets arrow and curly
  19214. 16:26:19brackets and let's add another test for
  19215. 16:26:22when the product is not in the cart so
  19216. 16:26:23this else condition so down here we'll
  19217. 16:26:27create another test using it brackets
  19218. 16:26:31and a string and the test name adds a
  19219. 16:26:36new product to the cart comma and a
  19220. 16:26:41function brackets arrow and curly
  19221. 16:26:46brackets so these are the two tests that
  19222. 16:26:49we need for the two conditions of this
  19223. 16:26:51if
  19224. 16:26:53statement let's actually start with
  19225. 16:26:55adding a new product to the cart since
  19226. 16:26:58it's a little easier to
  19227. 16:27:00test now unlike the format currency
  19228. 16:27:03function add to cart doesn't return a
  19229. 16:27:06value so we can't really compare it to
  19230. 16:27:09another value using
  19231. 16:27:11expect instead we're going to call add
  19232. 16:27:14to cart to modify the cart and then
  19233. 16:27:16check if this cart looks correct to do
  19234. 16:27:20that inside here let's call the function
  19235. 16:27:24add to cart
  19236. 16:27:28brackets and this function takes one
  19237. 16:27:32string which is the product
  19238. 16:27:35ID so inside here let's give it a string
  19239. 16:27:39and to find a product ID that we can
  19240. 16:27:42test with we're going to go to the left
  19241. 16:27:45and scroll up and we're going to open
  19242. 16:27:47the data/ products. JS
  19243. 16:27:50file and inside here we have a list of
  19244. 16:27:54products so let's just take the first
  19245. 16:27:56product ID and use it in our test we're
  19246. 16:27:59going to select this ID and right click
  19247. 16:28:04copy and then inside cart
  19248. 16:28:08test.js in this string we're going to
  19249. 16:28:11rightclick and
  19250. 16:28:14paste so this will add this product ID
  19251. 16:28:17to the cart and now to check if this
  19252. 16:28:20code is working correctly we're going to
  19253. 16:28:22check what the cart look looks like so
  19254. 16:28:25first we need to import the cart we're
  19255. 16:28:28going to scroll up and since we're
  19256. 16:28:30already importing from cart. JS to UT
  19257. 16:28:33the cart we can type a comma
  19258. 16:28:36cart and then at the bottom here we're
  19259. 16:28:40going to create a new
  19260. 16:28:42line and first let's check if the card.
  19261. 16:28:45length is correct so if we assume that
  19262. 16:28:48we start with an empty cart when we add
  19263. 16:28:51a product the cart. length should should
  19264. 16:28:54be one so we can compare these two
  19265. 16:28:56values cart. length and one so to
  19266. 16:29:00compare values we're going to use the
  19267. 16:29:01function expect brackets and we'll
  19268. 16:29:05compare the
  19269. 16:29:07cart.
  19270. 16:29:09length and then here we'll use the
  19271. 16:29:13method2 equal brackets
  19272. 16:29:171 so now let's save this
  19273. 16:29:20file and we're going to run these tests
  19274. 16:29:23to run them we're just going to load
  19275. 16:29:25this file inside test.html so we'll open
  19276. 16:29:30this and then at the bottom we'll create
  19277. 16:29:33a script
  19278. 16:29:35tag give it a source attribute double
  19279. 16:29:38quotes and we're going to load this file
  19280. 16:29:42so this file is inside the data folder
  19281. 16:29:44so we need to type data SL to go into
  19282. 16:29:48the folder and then load cart test.js
  19283. 16:29:54and this file also uses import so we're
  19284. 16:29:57going to give it the attribute type
  19285. 16:29:59equals module to enable JavaScript
  19286. 16:30:02module
  19287. 16:30:03features and one thing that I forgot to
  19288. 16:30:06do is we moved money test into the util
  19289. 16:30:10folder so we need to update the source
  19290. 16:30:12attribute to add the utils folder and
  19291. 16:30:15then slash to go into the folder and
  19292. 16:30:18then locate money
  19293. 16:30:20test.js so now let's save this HTML file
  19294. 16:30:25and open this test page in our
  19295. 16:30:31browser and now we should have two
  19296. 16:30:33additional tests and the test that we
  19297. 16:30:35just created should fail if it doesn't
  19298. 16:30:38fail for you you can go into your
  19299. 16:30:40console and run local storage. CLE and
  19300. 16:30:43then refresh the page and it should
  19301. 16:30:47fail so this tells us that the failure
  19302. 16:30:50happened in the test suite at Dart in
  19303. 16:30:53the test adds a new product to the cart
  19304. 16:30:55which is the one we just
  19305. 16:30:58created so it's telling us that the
  19306. 16:31:00cart. length is not equal to one it's
  19307. 16:31:03equal to a different number so why is
  19308. 16:31:06this happening let's go back to our
  19309. 16:31:09test open cart
  19310. 16:31:13test.js and in here remember that we
  19311. 16:31:16assumed the cart starts as empty and if
  19312. 16:31:19we add a product the card dot length
  19313. 16:31:21would be one however the cart doesn't
  19314. 16:31:24actually start as
  19315. 16:31:27empty in cart. JS at the top here we're
  19316. 16:31:31loading the cart from local storage if
  19317. 16:31:34there's nothing in local storage we use
  19318. 16:31:36this default cart so our starting cart
  19319. 16:31:40depends on what's inside local storage
  19320. 16:31:43and this is a big problem when testing
  19321. 16:31:46because local storage can change if
  19322. 16:31:48there's an empty card saved in local
  19323. 16:31:50storage our test will pass but if
  19324. 16:31:54there's not an empty cart in local
  19325. 16:31:56storage then this test will fail this is
  19326. 16:32:00known as a flaky test a test that
  19327. 16:32:03sometimes passes and sometimes fails
  19328. 16:32:05even if we don't change the
  19329. 16:32:07code now to solve this problem we're
  19330. 16:32:10going to use a feature of Jasmine called
  19331. 16:32:13mocks a mock lets us replace a method
  19332. 16:32:17with a fake version and then we can make
  19333. 16:32:20the fake version do anything we want for
  19334. 16:32:23example
  19335. 16:32:24when we load the cart from local storage
  19336. 16:32:27we're using local storage. getet item so
  19337. 16:32:30we can use a mock to create a fake
  19338. 16:32:33version of get item and then we can make
  19339. 16:32:36this fake version do anything we want
  19340. 16:32:38like return an empty
  19341. 16:32:40array so let's go ahead and create our
  19342. 16:32:43first
  19343. 16:32:44mock we'll go back to cart
  19344. 16:32:47test.js and at the top of this test
  19345. 16:32:50we're going to create a mock using
  19346. 16:32:52another function of Jasmine called spy
  19347. 16:32:56on and
  19348. 16:32:59brackets and we'll give spy on two
  19349. 16:33:02parameters the first parameter is the
  19350. 16:33:05object that we want to mock in our case
  19351. 16:33:08it's local storage so we'll type local
  19352. 16:33:12storage comma and then we'll give it a
  19353. 16:33:15string and this string will be the
  19354. 16:33:18method that we want to Mark in our case
  19355. 16:33:21we want to Mark the get item method so
  19356. 16:33:24here we'll type get item and make sure I
  19357. 16:33:28is uppercase so that we exactly match
  19358. 16:33:31the method
  19359. 16:33:32name so now this will replace local
  19360. 16:33:35storage. getet item with a fake version
  19361. 16:33:39and we can make this fake version do
  19362. 16:33:41anything we want to do that spy on gives
  19363. 16:33:45us an object and this object has a
  19364. 16:33:49property we can
  19365. 16:33:50use so at the end we'll type Dot
  19366. 16:33:54and the property called
  19367. 16:33:57an and this result is also an object and
  19368. 16:34:01this object has a method called dot call
  19369. 16:34:05fake brackets so don't worry about all
  19370. 16:34:09the Dots here Jasmine just organizes its
  19371. 16:34:12code so that it reads like English so
  19372. 16:34:16we're spying on local storage. getet
  19373. 16:34:18item and call fake so next we're going
  19374. 16:34:22to give call fake a function brackets
  19375. 16:34:26arrow and curly
  19376. 16:34:29brackets and this function is what we
  19377. 16:34:32want get item to do so we're essentially
  19378. 16:34:34overwriting the original get item with
  19379. 16:34:37whatever is inside this
  19380. 16:34:40function so earlier we said we wanted
  19381. 16:34:42get item to return the empty array for
  19382. 16:34:45this test so inside this fake function
  19383. 16:34:48let's make get item return the empty
  19384. 16:34:52array
  19385. 16:34:54now remember that local storage only
  19386. 16:34:56supports strings so we need to return
  19387. 16:34:58this as a string to do that we're going
  19388. 16:35:01to use the code Json all uppercase do
  19389. 16:35:08stringify Open Bracket and close bracket
  19390. 16:35:12so this will convert whatever is in the
  19391. 16:35:14brackets to a Json string and that's
  19392. 16:35:17what we need for local
  19393. 16:35:19storage so now let's test out this mock
  19394. 16:35:22to see if it works
  19395. 16:35:24below this we're going to
  19396. 16:35:27console.log
  19397. 16:35:29brackets and run local
  19398. 16:35:32storage doget capital I item brackets
  19399. 16:35:38the string
  19400. 16:35:41cart let's save this file and then open
  19401. 16:35:45the test page in our
  19402. 16:35:47browser and we're going to open the
  19403. 16:35:49console so
  19404. 16:35:51rightclick inspect
  19405. 16:35:54and the
  19406. 16:35:56console and you'll see that Here Local
  19407. 16:35:59storage. getet item is no longer loading
  19408. 16:36:02from the actual local storage it's
  19409. 16:36:04giving us an empty array every
  19410. 16:36:07time so now we have mocked the get item
  19411. 16:36:12method however you'll also notice that
  19412. 16:36:15our test is still failing so how come it
  19413. 16:36:18still
  19414. 16:36:19fails to understand what's happening
  19415. 16:36:22let's take a closer look at our code
  19416. 16:36:25we'll go back to our
  19417. 16:36:28code and inside our tests here we're
  19418. 16:36:31mocking local storage. getet item but
  19419. 16:36:34notice that above this if we scroll up
  19420. 16:36:38we import the cart at the top so the
  19421. 16:36:41order of this code matters first we load
  19422. 16:36:45the cart from local storage and after
  19423. 16:36:47we're mocking local storage. getet item
  19424. 16:36:50so the card is already loaded at this
  19425. 16:36:52point and this code doesn't have the
  19426. 16:36:54effect that we
  19427. 16:36:56want so one way to solve this problem is
  19428. 16:37:00after we mock local storage here we
  19429. 16:37:02should reload the cart so inside cart.
  19430. 16:37:06JS just going to give it a little more
  19431. 16:37:09space to reload this cart after we mock
  19432. 16:37:13we just need to rerun all of this code
  19433. 16:37:17so let's create a function so we can
  19434. 16:37:18rerun this code at the bottom here we'll
  19435. 16:37:22create a
  19436. 16:37:25function and we'll call it load from
  19437. 16:37:30Storage brackets and curly
  19438. 16:37:33brackets and we're just going to move
  19439. 16:37:36all of this code inside the function so
  19440. 16:37:39let's select all the code up to cart
  19441. 16:37:42equals we'll
  19442. 16:37:44rightclick
  19443. 16:37:47cut and then in the function we'll
  19444. 16:37:50rightclick and paste
  19445. 16:37:54and at the top let's create the cart
  19446. 16:37:56variable again let cart and
  19447. 16:38:01semicolon and lastly when we load this
  19448. 16:38:04file we need to run this function so
  19449. 16:38:06that we run this code at least
  19450. 16:38:09once so down here we'll just run this
  19451. 16:38:12function load from storage and
  19452. 16:38:17brackets so let's
  19453. 16:38:20save so we're basically running the
  19454. 16:38:22exact same code as before except this
  19455. 16:38:25code is now inside a
  19456. 16:38:28function and now in our
  19457. 16:38:31test after we mock local storage. getet
  19458. 16:38:34item we're going to reload the cart
  19459. 16:38:37using this function so first let's
  19460. 16:38:40export it so we can use it in our test
  19461. 16:38:42we'll type
  19462. 16:38:44export and save this file and Insider
  19463. 16:38:48test let's scroll up to the top we're
  19464. 16:38:51already importing from cart. Js so we'll
  19465. 16:38:53type a comma and we'll import load from
  19466. 16:38:59storage and then we'll scroll down back
  19467. 16:39:02to our test and here after we mock local
  19468. 16:39:05storage. getet item we're going to load
  19469. 16:39:10from Storage brackets and
  19470. 16:39:15save so this time we mock local storage.
  19471. 16:39:19getet item first to return an empty
  19472. 16:39:22array and then when we reload the cart
  19473. 16:39:25this is going to be an empty array and
  19474. 16:39:28the cart will be empty and now in our
  19475. 16:39:30test when we add a product to an empty
  19476. 16:39:33cart the cart length will be equal to
  19477. 16:39:35one so this test will pass so we can
  19478. 16:39:39actually remove the console.log here and
  19479. 16:39:43save and check our test
  19480. 16:39:46page and now this test should be
  19481. 16:39:50passing next let's go back to our code
  19482. 16:39:53Cod and if we look at the add to cart
  19483. 16:39:56function in cart.
  19484. 16:39:58JS so in here at the bottom of this
  19485. 16:40:03function you'll notice that we're saving
  19486. 16:40:05the cart to local storage so this is
  19487. 16:40:08going to call local storage. set
  19488. 16:40:11item however we don't actually want to
  19489. 16:40:14save to local storage because this is
  19490. 16:40:16just test code we don't want our test
  19491. 16:40:19code to be modifying local storage or
  19492. 16:40:21affecting our real code
  19493. 16:40:24to prevent this we're also going to mock
  19494. 16:40:26local storage. set
  19495. 16:40:28item so down here we're going to create
  19496. 16:40:31some new lines at the top of the test
  19497. 16:40:34and we're going to mock local storage.
  19498. 16:40:36set item using the function spy on
  19499. 16:40:40brackets and we'll give it local
  19500. 16:40:44storage comma a string and the method we
  19501. 16:40:48want to mock which is set
  19502. 16:40:51item and
  19503. 16:40:54save and remember the order of the code
  19504. 16:40:57matters so we want to mock local
  19505. 16:41:00storage. set item first and then we call
  19506. 16:41:03add to cart so now set item will be
  19507. 16:41:06replaced with a fake version and this
  19508. 16:41:08will no longer save to local
  19509. 16:41:13storage next spyon has another useful
  19510. 16:41:16feature which is it records every time a
  19511. 16:41:19method is used for example what if we
  19512. 16:41:23want to make sure that add to cart saves
  19513. 16:41:25the cart to local storage at the end in
  19514. 16:41:28our tests set item is mocked so we can't
  19515. 16:41:31really check what's inside the local
  19516. 16:41:33storage instead we can just check if add
  19517. 16:41:37to cart calls set item at some
  19518. 16:41:40point to check if set item was called at
  19519. 16:41:44the bottom we can use the function
  19520. 16:41:47expect
  19521. 16:41:49brackets and we're going to give this
  19522. 16:41:52the method that we loed which is local
  19523. 16:41:56storage. set
  19524. 16:42:00item and remember that expect gives us
  19525. 16:42:03an object and this object has many
  19526. 16:42:05useful methods to check stuff like dot
  19527. 16:42:08to
  19528. 16:42:09equal so another method we get from
  19529. 16:42:13expect is dot 2 have been called times
  19530. 16:42:21brackets
  19531. 16:42:23so make sure that you write this method
  19532. 16:42:25in cam case two is lowercase and every
  19533. 16:42:28word after starts with a
  19534. 16:42:31capital so this method checks how many
  19535. 16:42:33times local storage. set item was called
  19536. 16:42:37in the code above so we expect it to be
  19537. 16:42:40called once inside add to cart so we're
  19538. 16:42:43going to give it the number
  19539. 16:42:45one so keep in mind that this only works
  19540. 16:42:48if this method has been mocked with spy
  19541. 16:42:51on so now let's
  19542. 16:42:53save and open our test
  19543. 16:42:56page and you can see that all of our
  19544. 16:42:58tests passed and local storage. set item
  19545. 16:43:02was called
  19546. 16:43:03once so after we mock a method we can
  19547. 16:43:07check how many times this method was
  19548. 16:43:10called and we can even check what values
  19549. 16:43:13this method received but I'll leave that
  19550. 16:43:16as an
  19551. 16:43:17exercise another thing you'll notice
  19552. 16:43:19here is that each test can have multiple
  19553. 16:43:22expectations
  19554. 16:43:23and the test will only pass if all of
  19555. 16:43:26its expectations
  19556. 16:43:29pass now let's add a few more
  19557. 16:43:31expectations to this test for practice
  19558. 16:43:35so one thing we can check is we can
  19559. 16:43:37check the first product in the cart and
  19560. 16:43:40make sure that its product ID matches
  19561. 16:43:42this string here so below this let's use
  19562. 16:43:46another expect brackets and we're going
  19563. 16:43:50to check the first product in the cart
  19564. 16:43:53using cart square bracket
  19565. 16:43:560 so this is going to be an object and
  19566. 16:44:00this object is going to have a property
  19567. 16:44:03called product ID so that here we'll get
  19568. 16:44:07the dot product
  19569. 16:44:11ID and we're going to check if this
  19570. 16:44:13product ID is equal to this string so to
  19571. 16:44:16do that we'll use the dot 2 equal method
  19572. 16:44:21brackets and we'll give it this string
  19573. 16:44:24here so let's just select this and
  19574. 16:44:27rightclick
  19575. 16:44:29copy and here right click and
  19576. 16:44:34paste another thing that we can check is
  19577. 16:44:37the quantity when we add a new product
  19578. 16:44:40to the cart the quantity should start
  19579. 16:44:42out as one so down here we're going to
  19580. 16:44:46add another
  19581. 16:44:47expect
  19582. 16:44:49brackets and we're going to check the
  19583. 16:44:52first product
  19584. 16:44:53cart square bracket
  19585. 16:44:550.
  19586. 16:44:57quantity. 2 equal brackets one and
  19587. 16:45:04save now let's open our test
  19588. 16:45:07page and you can see that all of those
  19589. 16:45:10expectations
  19590. 16:45:13passed now let's move on and create the
  19591. 16:45:16test for adding an existing product to
  19592. 16:45:18the cart so we'll go back to our code
  19593. 16:45:23and scroll up to the first test
  19594. 16:45:26here so first we need to set up the cart
  19595. 16:45:29so that it already contains the product
  19596. 16:45:32that we want to add to do that we're
  19597. 16:45:35going to Mark local storage. getet item
  19598. 16:45:38again so down here we can just reuse
  19599. 16:45:41this code so we're going to select this
  19600. 16:45:44code and right
  19601. 16:45:47click
  19602. 16:45:49copy and then in the first test here
  19603. 16:45:53rightclick and
  19604. 16:45:57paste and this time do set up the test
  19605. 16:46:00we're going to put a product into the
  19606. 16:46:02cart at the beginning and then we're
  19607. 16:46:04going to add the same product to the
  19608. 16:46:07cart so inside this cart array let's add
  19609. 16:46:11an
  19610. 16:46:12object and each product has a property
  19611. 16:46:15called product ID so we're going to add
  19612. 16:46:18this property product ID colon
  19613. 16:46:23and let's just give the same product ID
  19614. 16:46:25that we used down here so we'll select
  19615. 16:46:29this and then right click
  19616. 16:46:33copy and then up here we're going to
  19617. 16:46:36right click and
  19618. 16:46:38paste
  19619. 16:46:40comma and each product also has a
  19620. 16:46:43quantity so we'll give it a quantity
  19621. 16:46:46colon and let's just give it one comma
  19622. 16:46:51and also a delivery option so delivery
  19623. 16:46:55option ID colon and let's just give it
  19624. 16:46:59the string
  19625. 16:47:00one and
  19626. 16:47:03save so now when we do local storage.
  19627. 16:47:06getet item it will give back this array
  19628. 16:47:09as the starting cart and then we need to
  19629. 16:47:12make sure that we reload the cart from
  19630. 16:47:16Storage next let's scroll down and let's
  19631. 16:47:20copy the rest of the tests because it's
  19632. 16:47:23going to be very similar so we're going
  19633. 16:47:25to select this code and
  19634. 16:47:28rightclick
  19635. 16:47:30copy and then up here create some new
  19636. 16:47:34lines and right click and
  19637. 16:47:39paste and also one thing I forgot to
  19638. 16:47:41copy over is to mock local storage. set
  19639. 16:47:45item so at the
  19640. 16:47:47top let's
  19641. 16:47:49also spy on brackets
  19642. 16:47:54local
  19643. 16:47:55storage comma a string set
  19644. 16:48:00item so a mock only lasts for one test
  19645. 16:48:04once that test is finished the method is
  19646. 16:48:06no longer mocked so that's why in this
  19647. 16:48:09test we also have to mock local storage.
  19648. 16:48:12set
  19649. 16:48:14item and now in this test we're just
  19650. 16:48:17going to modify these expectations so
  19651. 16:48:20that they make sense for this test
  19652. 16:48:23if we add an existing product to the
  19653. 16:48:25cart the cart. length will still equal
  19654. 16:48:28one because these product IDs match and
  19655. 16:48:31we're just increasing the
  19656. 16:48:33quantity we're still going to call local
  19657. 16:48:35storage. set item once so this is
  19658. 16:48:38correct
  19659. 16:48:39still the first item in the cart is
  19660. 16:48:42going to have this product ID and the
  19661. 16:48:45only thing we're going to change is that
  19662. 16:48:47the quantity is now going to increase by
  19663. 16:48:50one so it's going to go from one to two
  19664. 16:48:54this
  19665. 16:48:56time so everything is the same except if
  19666. 16:48:59we add an existing product we're just
  19667. 16:49:01increasing the quantity now let's
  19668. 16:49:05save and check the test
  19669. 16:49:08page and now the existing product test
  19670. 16:49:12is
  19671. 16:49:13passing and that's it we created tests
  19672. 16:49:16for the add to cart
  19673. 16:49:18function so now let's do some cleanup
  19674. 16:49:21and we're actually going to remove
  19675. 16:49:23everything from local storage since we
  19676. 16:49:25were modifying it earlier in the test
  19677. 16:49:28and we might have messed it up so on
  19678. 16:49:30this page we're going to
  19679. 16:49:32rightclick
  19680. 16:49:34inspect and open the console and to
  19681. 16:49:38remove everything from local storage
  19682. 16:49:40we're going to type the code local
  19683. 16:49:43capital S
  19684. 16:49:45storage. clear and brackets and press
  19685. 16:49:51enter and then we'll refresh the
  19686. 16:49:54page now finally let's go into get and
  19687. 16:49:57save our changes so we'll open our code
  19688. 16:50:01and we can close this one for
  19689. 16:50:04now and we'll open the get
  19690. 16:50:07Section so in cart. JS at the top we
  19691. 16:50:12basically put all the code that loads
  19692. 16:50:14the cart into a function so we can use
  19693. 16:50:17it in our
  19694. 16:50:18tests and then here we moved money
  19695. 16:50:21test.js into a utils folder in test.html
  19696. 16:50:26we're loading the new cart test.js file
  19697. 16:50:31and then inside cart test.js we added
  19698. 16:50:34some tests for the add to cart function
  19699. 16:50:37so now let's create a message describing
  19700. 16:50:40these
  19701. 16:50:41changes
  19702. 16:50:42create
  19703. 16:50:44automated tests for add to cart and
  19704. 16:50:50commit
  19705. 16:50:56the last thing we're going to learn in
  19706. 16:50:58this lesson is how to create more
  19707. 16:51:00complicated
  19708. 16:51:01tests so far we've only been testing a
  19709. 16:51:05single function for example we tested
  19710. 16:51:08the add to card function as well as the
  19711. 16:51:11format currency function so these are
  19712. 16:51:14called Unit tests because we're only
  19713. 16:51:17testing one unit or one piece of the
  19714. 16:51:20code now what if we want to test t a
  19715. 16:51:22whole page or part of a page and this
  19716. 16:51:26requires a more complicated test called
  19717. 16:51:28an integration
  19718. 16:51:30test an integration test tests many
  19719. 16:51:34units of code or pieces of code working
  19720. 16:51:38together for example in our code we're
  19721. 16:51:41going to go to scripts checkout thee
  19722. 16:51:44orders summary. JS
  19723. 16:51:46file and we're going to test this render
  19724. 16:51:49order summary function so this function
  19725. 16:51:52creates a whole section on the checkout
  19726. 16:51:55page and it uses many different
  19727. 16:51:57functions and libraries to create the
  19728. 16:51:59page so testing this function will be
  19729. 16:52:02considered an integration test because
  19730. 16:52:05it uses many different pieces of code or
  19731. 16:52:07units of code together so let's start by
  19732. 16:52:11creating a test file for this function
  19733. 16:52:14so this is in the orders summary. JS
  19734. 16:52:16file inside our test here let's match
  19735. 16:52:21the file structure that we have here
  19736. 16:52:22here so we're going to click test-
  19737. 16:52:25Jasmin and first we're going to create a
  19738. 16:52:27checkout folder so we'll click here to
  19739. 16:52:30create a
  19740. 16:52:31folder and we're going to name it
  19741. 16:52:34checkout and press
  19742. 16:52:37enter and then inside the checkout
  19743. 16:52:39folder we're going to create a new file
  19744. 16:52:42so we'll click this to create a new
  19745. 16:52:44file and we'll name it the same as above
  19746. 16:52:48order summary and ADD test
  19747. 16:52:53.js and press
  19748. 16:52:56enter so by matching the file structure
  19749. 16:52:59it's clear that this test file is going
  19750. 16:53:02to test ORD summary.
  19751. 16:53:04JS and again for the video I'm just
  19752. 16:53:07going to drag this file to the top here
  19753. 16:53:10so we can see this and the test side by
  19754. 16:53:15side and now we're going to test this
  19755. 16:53:17render order summary function so first
  19756. 16:53:20we're going to import this function into
  19757. 16:53:22to our test file at the top we'll import
  19758. 16:53:26curly brackets the render order summary
  19759. 16:53:31function from a string and we're going
  19760. 16:53:35to locate this file so first we need to
  19761. 16:53:37get out of the checkout folder and out
  19762. 16:53:40of the test Jasmine folder so we'll use
  19763. 16:53:42two do do slash do do slash and then
  19764. 16:53:47we're going to go into the scripts
  19765. 16:53:48folder and into the checkout folder
  19766. 16:53:51Scripts slash checkout slash and we'll
  19767. 16:53:55load order
  19768. 16:54:07summary.jpg
  19769. 16:54:19[Music]
  19770. 16:54:20order summary
  19771. 16:54:23a comma and a function brackets arrow
  19772. 16:54:27and curler
  19773. 16:54:28brackets and now what test cases should
  19774. 16:54:31we create render order summary creates a
  19775. 16:54:35part of the page so when we're testing a
  19776. 16:54:38page the two things we need to test is
  19777. 16:54:41how the page looks and how the page
  19778. 16:54:44behaves let's start by testing how the
  19779. 16:54:47page looks inside our test Suite we're
  19780. 16:54:50going to create a test using it brackets
  19781. 16:54:55and we'll name this test with a
  19782. 16:54:57string and this will just be a basic
  19783. 16:55:00test case so we'll describe what this
  19784. 16:55:03function does which is
  19785. 16:55:07displays the
  19786. 16:55:09cart comma and we'll give it a function
  19787. 16:55:12brackets arrow and curly brackets and
  19788. 16:55:16now we're ready to create the
  19789. 16:55:19test so render order summary usually
  19790. 16:55:22displays the cart on the page however in
  19791. 16:55:26our tests where does this cart get
  19792. 16:55:29displayed so if we look at the code in
  19793. 16:55:32this
  19794. 16:55:33function and we scroll down here we
  19795. 16:55:36generate the HTML for the
  19796. 16:55:39cart and then after we generate the
  19797. 16:55:42HTML we put the HTML inside an element
  19798. 16:55:46with the class js- order-
  19799. 16:55:50summary however we don't have this
  19800. 16:55:53element in our tests so to fix this
  19801. 16:55:56we're just going to create an element
  19802. 16:55:57with the class js- order-
  19803. 16:56:00summary to do this we're going to scroll
  19804. 16:56:03down and we're going to open the
  19805. 16:56:05test.html
  19806. 16:56:07file and then at the bottom we're going
  19807. 16:56:10to go to the body and then inside here
  19808. 16:56:14we're going to create a div so less than
  19809. 16:56:17div and greater
  19810. 16:56:19than so remember how we said we need to
  19811. 16:56:21create create an element with the class
  19812. 16:56:23js- order- summary we're going to create
  19813. 16:56:27that element and put it inside this div
  19814. 16:56:30so this div is going to act like a
  19815. 16:56:33container the reason we use a container
  19816. 16:56:36is that when we create elements in our
  19817. 16:56:37test we don't want to be modifying the
  19818. 16:56:40entire body because the body might
  19819. 16:56:42contain our test results from jasine so
  19820. 16:56:44we put everything inside a
  19821. 16:56:46container so now let's give this div a
  19822. 16:56:49class so we can use it in our JavaScript
  19823. 16:56:52we're going to give it a class equals
  19824. 16:56:54double quotes and we're going to call
  19825. 16:56:56this the js- test-
  19826. 16:57:00container to make it clear this is a
  19827. 16:57:03container for all of our test
  19828. 16:57:05code and
  19829. 16:57:08save and now let's get this container
  19830. 16:57:11into our tests we can do that using
  19831. 16:57:14document. query
  19832. 16:57:17selector brackets and a string and we'll
  19833. 16:57:20select the class using using a js- test-
  19834. 16:57:27container and then inside this container
  19835. 16:57:30we're going to create an element with
  19836. 16:57:31the class js- order- summary so to put
  19837. 16:57:36something inside this element we're
  19838. 16:57:38going to use the property do inner
  19839. 16:57:42Capital
  19840. 16:57:43HTML equals a template
  19841. 16:57:49string and inside this template
  19842. 16:57:52let's create a div element less than div
  19843. 16:57:56greater than less than SL div greater
  19844. 16:57:59than and as we mentioned we'll give this
  19845. 16:58:02element a class equals double quotes of
  19846. 16:58:06js- order-
  19847. 16:58:10summary so this basically takes this
  19848. 16:58:12HTML and puts it inside our test
  19849. 16:58:15container so it will put it inside here
  19850. 16:58:19and now we have the element that we need
  19851. 16:58:21for render order summary so when we call
  19852. 16:58:24this function it's going to create the
  19853. 16:58:25cart and display it in this element
  19854. 16:58:28here next if we look at the code for
  19855. 16:58:32render order summary and we scroll to
  19856. 16:58:35the
  19857. 16:58:37top you'll notice that it takes the cart
  19858. 16:58:40and then displays it on the page but
  19859. 16:58:43remember that by default we load this
  19860. 16:58:45cart from local storage and this can
  19861. 16:58:48cause problems in our test depending on
  19862. 16:58:51what is inside local storage so to fix
  19863. 16:58:53this we're going to mock local storage.
  19864. 16:58:56getet item again to control exactly
  19865. 16:58:59what's inside this cart so let's
  19866. 16:59:02actually open the cart test that we had
  19867. 16:59:05before and we're just going to copy
  19868. 16:59:08paste this mock that we created so we're
  19869. 16:59:10going to start from here this function
  19870. 16:59:13and we're going to select the code up to
  19871. 16:59:16spy on get item and then
  19872. 16:59:19rightclick copy
  19873. 16:59:22and then down here we're going to
  19874. 16:59:25rightclick and
  19875. 16:59:28paste and one other thing we need to do
  19876. 16:59:30here is we don't have access to load
  19877. 16:59:33from Storage right now so we need to
  19878. 16:59:35import it at the
  19879. 16:59:38top let's also copy the import that we
  19880. 16:59:41had here so we're going to select this
  19881. 16:59:44and then right click
  19882. 16:59:47copy and then down here we're going to
  19883. 16:59:50rightclick and paste the
  19884. 16:59:52import and we just need to make sure
  19885. 16:59:54that this file path locates cart. JS so
  19886. 16:59:58do/ do slash it's going to go outside
  19887. 17:00:00checkout and outside test- Jasmin and
  19888. 17:00:03then it will go inside the data folder
  19889. 17:00:06and then locate card. JS so this file
  19890. 17:00:09path is correct and currently we're not
  19891. 17:00:12using these two values so we can just
  19892. 17:00:14remove
  19893. 17:00:19them and now let's set up this cart for
  19894. 17:00:22this test so we could just use this cart
  19895. 17:00:25here however to make our test a little
  19896. 17:00:28more detailed let's open the cart. JS
  19897. 17:00:33file and at the top here we're going to
  19898. 17:00:36use this default card instead because it
  19899. 17:00:39has two products with different
  19900. 17:00:41quantities so we can test more things so
  19901. 17:00:44we're just going to select this card
  19902. 17:00:46here and then right click
  19903. 17:00:49copy and then down here open order
  19904. 17:00:52summary
  19905. 17:00:53test and we're going to select this card
  19906. 17:00:57and remove
  19907. 17:00:58it and inside these brackets we're going
  19908. 17:01:01to right click and paste that default
  19909. 17:01:05cart and now that the HTML and the cart
  19910. 17:01:09is set up for this function at the
  19911. 17:01:12bottom we can call the function render
  19912. 17:01:16order summary and
  19913. 17:01:19brackets and Save
  19914. 17:01:25so now let's run this test file we're
  19915. 17:01:28going to open test.html
  19916. 17:01:31and then beneath these tests we're going
  19917. 17:01:34to add another script Tech less than
  19918. 17:01:37script Source equals and we'll load the
  19919. 17:01:42checkout slash order summary test.js
  19920. 17:01:49file and we need to give it the
  19921. 17:01:51attribute type Ty
  19922. 17:01:53equals module because we used
  19923. 17:01:57Imports and let's save so we run this
  19924. 17:02:00test so we'll open our test
  19925. 17:02:03page and you can see that the cart was
  19926. 17:02:06rendered onto this test page and we
  19927. 17:02:10still have our Jasmine results at the
  19928. 17:02:11bottom here so you notice that when we
  19929. 17:02:14render this it didn't load any CSS or
  19930. 17:02:18images and this is okay because we can't
  19931. 17:02:21really use code to check if the page
  19932. 17:02:23visually looks correct however we can
  19933. 17:02:27use this to check the contents of the
  19934. 17:02:29page like if the products and the
  19935. 17:02:31quantities are
  19936. 17:02:33correct so first our cart has two
  19937. 17:02:36products in it so we can check if there
  19938. 17:02:38were two products that were rendered on
  19939. 17:02:41the page let's go back to our
  19940. 17:02:45tests and let's take a look at the
  19941. 17:02:48orderers summary. JS file
  19942. 17:02:54and when we render the order summary we
  19943. 17:02:56scroll down for each product in the cart
  19944. 17:03:00we create an element with the class
  19945. 17:03:02cart--
  19946. 17:03:04container since we have two products in
  19947. 17:03:06the cart let's check that we created two
  19948. 17:03:09of these elements on the page so we're
  19949. 17:03:12going to open the order summary test.js
  19950. 17:03:18file and then in the first test at the
  19951. 17:03:21bottom of this test we're going to add
  19952. 17:03:24some lines and we're going to check if
  19953. 17:03:27we have two of these elements to do that
  19954. 17:03:29we can use the Dom using
  19955. 17:03:32document. query selector all brackets
  19956. 17:03:38and a string and we could select the
  19957. 17:03:41class card- item D container however
  19958. 17:03:44this class is used for CSS so remember
  19959. 17:03:47if we're going to use a class in
  19960. 17:03:48JavaScript we start with js-
  19961. 17:03:53let's add a new class to this element
  19962. 17:03:55that we can use for the test we'll add
  19963. 17:03:58js- cart D item D
  19964. 17:04:03container so now we're going to select
  19965. 17:04:05all the elements with this class
  19966. 17:04:08using js- card- item D
  19967. 17:04:14container and let's save the ORD
  19968. 17:04:17summary. JS
  19969. 17:04:20file and then down here we're going to
  19970. 17:04:23check if we have two of these elements
  19971. 17:04:25on the page so again to check something
  19972. 17:04:28in our tests we're going to use the
  19973. 17:04:30function expect Open Bracket and let's
  19974. 17:04:34create a new line to make it a little
  19975. 17:04:37cleaner and at the end we'll give a Clos
  19976. 17:04:40bracket like
  19977. 17:04:41this and this code gives us an array of
  19978. 17:04:45elements so here we're going to check if
  19979. 17:04:48the dot length of this is equal to to
  19980. 17:04:52two so expect gives us an object which
  19981. 17:04:55has a method called dot 2 equal brackets
  19982. 17:05:01and two so this checks if we have two of
  19983. 17:05:04these elements on the page so let's
  19984. 17:05:08save and then check the test
  19985. 17:05:11page and scroll to the bottom and you'll
  19986. 17:05:14see that this test
  19987. 17:05:16passed so that's how we check if we
  19988. 17:05:18render the page correctly
  19989. 17:05:23next let's do another check of what we
  19990. 17:05:25rendered on the page so another thing we
  19991. 17:05:28can test is If the product quantities
  19992. 17:05:30are correct so first we need to get this
  19993. 17:05:33quantity element into our tests so we'll
  19994. 17:05:37go back to our
  19995. 17:05:38code and then inside the HTML of the
  19996. 17:05:42order summary we're going to scroll
  19997. 17:05:45down until we see a div with the class
  19998. 17:05:48product- quantity and we're displaying
  19999. 17:05:51the Quant
  20000. 17:05:53here so now we're going to take this
  20001. 17:05:55element put it into our test and check
  20002. 17:05:58that the quantity here is
  20003. 17:06:00correct so to do that let's give another
  20004. 17:06:04class to this element so we can select
  20005. 17:06:06it using JavaScript so here we'll add
  20006. 17:06:09another class
  20007. 17:06:11js-
  20008. 17:06:13product-
  20009. 17:06:15quantity and we want to check the
  20010. 17:06:17quantity for each product so we need to
  20011. 17:06:20identify which product this is for we
  20012. 17:06:23can do that by adding a dash to this
  20013. 17:06:25class and then inserting using dollar
  20014. 17:06:28and curly brackets the products ID which
  20015. 17:06:32is the matching product matching
  20016. 17:06:36product. ID and
  20017. 17:06:40save and now let's get this element into
  20018. 17:06:43our test so down here we're going to
  20019. 17:06:47document. query
  20020. 17:06:50selector
  20021. 17:06:52brackets and we'll give it a template
  20022. 17:06:54string because we're going to insert a
  20023. 17:06:56product ID so we're going to select this
  20024. 17:06:58class using a js- product- quantity Dash
  20025. 17:07:05and then insert the product ID so in our
  20026. 17:07:09cart we have two products and let's
  20027. 17:07:11start with the first product ID now to
  20028. 17:07:14make this easier let's put this string
  20029. 17:07:17inside a variable so we can reuse it
  20030. 17:07:19below so above the this let's create a
  20031. 17:07:22variable with const call it product
  20032. 17:07:27id1 equals and we're going to select
  20033. 17:07:31this ID and then right click
  20034. 17:07:35cut and then up here rightclick and
  20035. 17:07:40paste and then we're just going to use
  20036. 17:07:43this variable down here product
  20037. 17:07:48id1 and then when we're getting this
  20038. 17:07:50element into our our code we're also
  20039. 17:07:53going to insert the product ID which is
  20040. 17:07:56product
  20041. 17:07:59id1 but now we're going to check if the
  20042. 17:08:02text inside is correct so for the first
  20043. 17:08:05product we have a quantity of two so the
  20044. 17:08:09text inside this element should say
  20045. 17:08:11Quantity colon
  20046. 17:08:142 so to check this at the bottom of our
  20047. 17:08:18test as usual we're going to use the
  20048. 17:08:20function
  20049. 17:08:21expect Open Bracket and a new line and
  20050. 17:08:25then here we can remove the semicolon
  20051. 17:08:28and close
  20052. 17:08:30bracket and we're going to get the text
  20053. 17:08:32inside this element now we're not going
  20054. 17:08:34to usein HTML because this element
  20055. 17:08:38contains a bunch of HTML inside but we
  20056. 17:08:41only want the text to get just the text
  20057. 17:08:44we're going to use the property do inner
  20058. 17:08:50text so we expect the text inside this
  20059. 17:08:53element to contain quantity colon
  20060. 17:08:562 however this element also has some
  20061. 17:08:59other text like update and
  20062. 17:09:01delete so we just want to check if
  20063. 17:09:04quantity 2 is somewhere inside this
  20064. 17:09:06element so how do we check that to do
  20065. 17:09:10that we're going to use a different
  20066. 17:09:11method of expect called dot to
  20067. 17:09:17contain and
  20068. 17:09:19brackets and we're going to give it the
  20069. 17:09:21string that we want to check for which
  20070. 17:09:23is capital Q quantity colon
  20071. 17:09:292 so the two contain method checks if
  20072. 17:09:33this value contains this string so they
  20073. 17:09:36don't have to be an exact match like
  20074. 17:09:39with two
  20075. 17:09:40equal as long as quantity colon 2 is
  20076. 17:09:43somewhere in this element then this
  20077. 17:09:46expectation will pass and this is
  20078. 17:09:49exactly what we need when this El has
  20079. 17:09:51some extra text that we don't really
  20080. 17:09:53need to
  20081. 17:09:55check so now let's click our test file
  20082. 17:09:58and
  20083. 17:09:59save and open the test
  20084. 17:10:02page and scroll to the bottom and you'll
  20085. 17:10:05notice that the test passed so for the
  20086. 17:10:08first product we're displaying the
  20087. 17:10:10quantity of two
  20088. 17:10:13correctly let's also check the quantity
  20089. 17:10:16of the second product we'll go back to
  20090. 17:10:18our
  20091. 17:10:20tests and and just like before let's
  20092. 17:10:23create a variable to store this ID so
  20093. 17:10:27we'll create a variable with const
  20094. 17:10:30product
  20095. 17:10:32id2 equals and we'll select this
  20096. 17:10:36ID and right click
  20097. 17:10:39copy and then here right click and
  20098. 17:10:44paste and then we'll remove
  20099. 17:10:47this and use the variable product
  20100. 17:10:53id2 and then at the bottom we're going
  20101. 17:10:56to get the quantity element for Product
  20102. 17:10:582 so let's just copy this code because
  20103. 17:11:01it's very similar we'll select it right
  20104. 17:11:04click copy and then down
  20105. 17:11:07here right click and
  20106. 17:11:11paste so this time we're going to get
  20107. 17:11:13the quantity element for product ID
  20108. 17:11:172 and in our cart product two has a
  20109. 17:11:20quantity of one so down here we're going
  20110. 17:11:24to check if the text contains quantity
  20111. 17:11:27colon 1 and
  20112. 17:11:31save and let's open our test
  20113. 17:11:33page and scroll to the
  20114. 17:11:36bottom and this test passed so we're
  20115. 17:11:39displaying the quantity of both products
  20116. 17:11:41correctly on the
  20117. 17:11:44page the second thing we can test in an
  20118. 17:11:47integration test is how the page behaves
  20119. 17:11:50for for example on this page we can
  20120. 17:11:53click the delete link to remove a
  20121. 17:11:56product from the page so let's create a
  20122. 17:11:59new test to make sure that the delete
  20123. 17:12:01link behaves
  20124. 17:12:03correctly we'll open our test
  20125. 17:12:06code and at the bottom we're going to
  20126. 17:12:09create a new test using the function it
  20127. 17:12:13and
  20128. 17:12:14brackets we'll give it a name and let's
  20129. 17:12:18describe what the behavior should be
  20130. 17:12:21which is removes a
  20131. 17:12:24product comma and a function brackets
  20132. 17:12:29arrow and curler
  20133. 17:12:32brackets now let's do the same setup as
  20134. 17:12:34the first test so we start with two
  20135. 17:12:37products in the cart so we can select
  20136. 17:12:40from load from storage and we'll select
  20137. 17:12:43all of this code up here and then
  20138. 17:12:47rightclick
  20139. 17:12:49copy and at the bottom
  20140. 17:12:52we're going to right click and
  20141. 17:12:56paste so this will set up the HTML
  20142. 17:12:59element that we need for the order
  20143. 17:13:01summary as well as the
  20144. 17:13:03cart and after this let's render the
  20145. 17:13:07order summary using the function render
  20146. 17:13:11order summary and
  20147. 17:13:16brackets so once we've displayed this on
  20148. 17:13:18the page we're going to get the delete
  20149. 17:13:20link and click it so first let's get the
  20150. 17:13:24delete link into our code so we'll go to
  20151. 17:13:27the orders summary. JS file and inside
  20152. 17:13:30the HTML we're going to scroll down a
  20153. 17:13:33bit till we see the delete quantity link
  20154. 17:13:37and let's test the delete link for the
  20155. 17:13:39first product in the
  20156. 17:13:41cart so first we need to add a class to
  20157. 17:13:44this element so we know which product
  20158. 17:13:47this is for so here we're going to add a
  20159. 17:13:50class JF s- delete
  20160. 17:13:53dink Dash and insert the product ID
  20161. 17:13:58which is the matching
  20162. 17:14:01product
  20163. 17:14:03doid and I'll add a new line to make the
  20164. 17:14:06code a little bit cleaner and now we can
  20165. 17:14:09save and then down here we're going to
  20166. 17:14:12get the delete link for the first
  20167. 17:14:14product ID so at the bottom we use
  20168. 17:14:19document do query
  20169. 17:14:21selector
  20170. 17:14:23brackets and we'll give it a template
  20171. 17:14:26string because we're going to insert the
  20172. 17:14:28product ID and we'll select this class
  20173. 17:14:31using js- delete
  20174. 17:14:35dlink D and then insert dollar curly
  20175. 17:14:40brackets product
  20176. 17:14:44id1 and now we need to click this delete
  20177. 17:14:47link to remove the first product from
  20178. 17:14:50the page and from the cart to click an
  20179. 17:14:53element using Code we can just get it
  20180. 17:14:56using the Dom and then use the method
  20181. 17:14:59dot click and
  20182. 17:15:02brackets so this will click delete on
  20183. 17:15:04the first product and remove it so let's
  20184. 17:15:08save and take a look at our test
  20185. 17:15:12page and unfortunately this test failed
  20186. 17:15:16so let's take a look at the details of
  20187. 17:15:18the failure to figure out how to fix it
  20188. 17:15:22so it's telling us that we cannot set
  20189. 17:15:24properties of null this means that a
  20190. 17:15:27value in our code is null and we're
  20191. 17:15:29trying to give it a
  20192. 17:15:32property and here it will tell us which
  20193. 17:15:35line we got this error from so this
  20194. 17:15:38error happened in our test file and the
  20195. 17:15:41line that created this error was in Ord
  20196. 17:15:43summary. js on line
  20197. 17:15:45132 or whatever line that you see here
  20198. 17:15:48on your page
  20199. 17:15:51so inside our code we're going to open
  20200. 17:15:54orders summary.
  20201. 17:15:55JS and I'm going to scroll down to line
  20202. 17:15:59132 and you can scroll down to the line
  20203. 17:16:01that you got on your
  20204. 17:16:03page so this line of code is actually
  20205. 17:16:07part of the code that we run when we
  20206. 17:16:10click delete so remember that when we
  20207. 17:16:13click delete we also update the payment
  20208. 17:16:16summary on the right side of the page
  20209. 17:16:18and that's what this line does now the
  20210. 17:16:21reason this is not working is if we open
  20211. 17:16:24the payment summary JS file and we look
  20212. 17:16:27at what this does at the bottom after we
  20213. 17:16:30generate the HTML we try to put the HTML
  20214. 17:16:34in an element with a class js- payment D
  20215. 17:16:38summary and this element does not exist
  20216. 17:16:41in our tests so one way to fix this is
  20217. 17:16:44to add this element to our
  20218. 17:16:48test so let's open our test again
  20219. 17:16:51we'll open the order summary
  20220. 17:16:54test.js and then we'll scroll
  20221. 17:16:56up and then here when we create the
  20222. 17:16:59order summary element we're also going
  20223. 17:17:02to create the payment summary
  20224. 17:17:04element so we'll create a less than div
  20225. 17:17:08greater than less than SL div greater
  20226. 17:17:11than and here we'll give it a class
  20227. 17:17:14equals double quotes and we'll give the
  20228. 17:17:18class that the payment summary needs
  20229. 17:17:20which is
  20230. 17:17:21js- payment D
  20231. 17:17:24summary and
  20232. 17:17:28save so now that the payment summary
  20233. 17:17:30element is set up we can open our test
  20234. 17:17:33page and our test should no longer have
  20235. 17:17:36a failure so you also notice that the
  20236. 17:17:39payment summary got rendered or
  20237. 17:17:40displayed on the
  20238. 17:17:43page okay so in our tests we only
  20239. 17:17:46clicked the delete link for the first
  20240. 17:17:48product but we didn't actually check if
  20241. 17:17:50the page looks
  20242. 17:17:52correct and that's what it shows here
  20243. 17:17:54with this yellow dot this test has no
  20244. 17:17:57expectations
  20245. 17:17:59yet so let's go back to our test and add
  20246. 17:18:02some expectations at the bottom so we'll
  20247. 17:18:05scroll down and down
  20248. 17:18:09here after we click delete for the first
  20249. 17:18:12product we expect that there's only one
  20250. 17:18:14element left on the page for the
  20251. 17:18:17cart so in our first test we actually
  20252. 17:18:20had a check for this so we can scroll up
  20253. 17:18:24and here we check if we have two cart
  20254. 17:18:27item containers on the page so let's
  20255. 17:18:30just reuse this check because it's very
  20256. 17:18:32similar we'll select it right click
  20257. 17:18:37copy and then down
  20258. 17:18:39here we're going to right click and
  20259. 17:18:44paste and this time we only expect one
  20260. 17:18:47cart item container to be left on the
  20261. 17:18:49page so so we'll change this to one so
  20262. 17:18:53we'll
  20263. 17:18:54save open the test page scroll down and
  20264. 17:18:58now this test
  20265. 17:19:02passed let's also do a more detailed
  20266. 17:19:05check so we're going to check that the
  20267. 17:19:07first product is no longer on the page
  20268. 17:19:11we'll go back to our
  20269. 17:19:13tests and then down here we're going to
  20270. 17:19:16get the card item container for the
  20271. 17:19:18first product we use
  20272. 17:19:21document. query selector
  20273. 17:19:25brackets and to get the element for the
  20274. 17:19:27first product we're going to open orders
  20275. 17:19:30summary. JS and then scroll up in the
  20276. 17:19:35HTML until we find the cart item
  20277. 17:19:39container
  20278. 17:19:40element and we're going to look for this
  20279. 17:19:42class right here which tells us which
  20280. 17:19:44product this element is for so in here
  20281. 17:19:48we'll give it a template string because
  20282. 17:19:50we'll in insert the product ID and we'll
  20283. 17:19:52look for this class using a
  20284. 17:19:57js-- item Das container Dash and insert
  20285. 17:20:03the first product so product
  20286. 17:20:09id1 and because we removed this product
  20287. 17:20:12from the page we expect this result to
  20288. 17:20:15be null so we'll use the function expect
  20289. 17:20:20open bracket and a new line and then
  20290. 17:20:23down here a close bracket and we'll use
  20291. 17:20:26the method
  20292. 17:20:28do2 equal brackets and we expect this to
  20293. 17:20:32be null and
  20294. 17:20:36save let's also check that the second
  20295. 17:20:39product is still on the page because we
  20296. 17:20:42didn't delete
  20297. 17:20:43it so let's just make a copy of this
  20298. 17:20:46check we'll select it right click copy
  20299. 17:20:51and then
  20300. 17:20:52here right click and
  20301. 17:20:56paste and we'll get the element for
  20302. 17:20:59product ID 2 this time and we want to
  20303. 17:21:02make sure that this exists so that it is
  20304. 17:21:05not equal to null in Jasmine to check if
  20305. 17:21:08something is not equal
  20306. 17:21:10to expect gives us another property we
  20307. 17:21:13can use called dot
  20308. 17:21:17not so if we use theot property it will
  20309. 17:21:20check the opposite of whatever is next
  20310. 17:21:24so this will check if this value is not
  20311. 17:21:27equal to null so remember the code for
  20312. 17:21:30Jasmine is designed to read like
  20313. 17:21:33English let's save and check the test
  20314. 17:21:37page and scroll
  20315. 17:21:40down and this test passed so we're
  20316. 17:21:43displaying the correct products on the
  20317. 17:21:45page after
  20318. 17:21:48deleting now there's one more thing that
  20319. 17:21:50we need to check here which is after
  20320. 17:21:52deleting is the cart array itself
  20321. 17:21:56updated so we'll go back to our code and
  20322. 17:21:58add that
  20323. 17:21:59check so first we need to import the
  20324. 17:22:02cart so we can use it in this test we're
  20325. 17:22:05going to scroll up to the
  20326. 17:22:06top and we're already importing from
  20327. 17:22:09cart. JS so we'll add a comma and import
  20328. 17:22:13the
  20329. 17:22:14cart and then at the
  20330. 17:22:17bottom we're going to check if the card
  20331. 17:22:19is correct
  20332. 17:22:22so the cart started out with two
  20333. 17:22:24products inside after removing the first
  20334. 17:22:27product the card should have a length of
  20335. 17:22:30one and the remaining product should
  20336. 17:22:33have product id2 so let's add both of
  20337. 17:22:36these checks at the bottom so we'll
  20338. 17:22:39scroll back
  20339. 17:22:42down and we'll use expect
  20340. 17:22:46brackets and we'll check if cart. length
  20341. 17:22:51do 2 equal brackets
  20342. 17:22:551 and we'll also expect brackets and
  20343. 17:23:00we'll check the first product in the
  20344. 17:23:02cart which is cart square bracket 0 and
  20345. 17:23:06we'll check its product ID do2
  20346. 17:23:13equal and it should equal product ID 2
  20347. 17:23:17so product id2
  20348. 17:23:21and
  20349. 17:23:22save so let's open our test
  20350. 17:23:24page and scroll to the bottom and this
  20351. 17:23:28test still passed so once we click
  20352. 17:23:30delete the card array is also
  20353. 17:23:34correct so that's how we create an
  20354. 17:23:36integration test it tests many units of
  20355. 17:23:39code or pieces of code working together
  20356. 17:23:43and it lets us test how our page looks
  20357. 17:23:46and how our page behaves
  20358. 17:23:51now there's one thing that I forgot to
  20359. 17:23:52do when we created these tests if we
  20360. 17:23:55look at our
  20361. 17:23:56code and I'm going to open orders
  20362. 17:23:59summary.
  20363. 17:24:01JS when we click delete at the bottom
  20364. 17:24:06here it uses a function called remove
  20365. 17:24:09from
  20366. 17:24:10cart and if I look at the code for this
  20367. 17:24:13function which is right here remove from
  20368. 17:24:16cart this function actually saves the
  20369. 17:24:18cart to local storage and and calls
  20370. 17:24:20local storage. set
  20371. 17:24:23item however it's recommended not to
  20372. 17:24:26modify local storage in our test so
  20373. 17:24:29inside the order summary test file we
  20374. 17:24:32should also mock local storage. set item
  20375. 17:24:36in this test so we'll scroll to the
  20376. 17:24:40top and at the start of the test let's
  20377. 17:24:43mock set item using spy on
  20378. 17:24:48brackets local
  20379. 17:24:51storage comma the string set
  20380. 17:24:55item and
  20381. 17:24:58save and we'll open the test page and
  20382. 17:25:01make sure that the test still passes and
  20383. 17:25:04we might have messed up our local
  20384. 17:25:05storage because of that mistake so let's
  20385. 17:25:08reset local storage again we'll right
  20386. 17:25:11click on this page
  20387. 17:25:14inspect open the
  20388. 17:25:17console and we'll type the code local
  20389. 17:25:21storage. CLE brackets and press
  20390. 17:25:26enter and refresh the page just to be
  20391. 17:25:30safe now another thing that we might
  20392. 17:25:32want to add is that when we render the
  20393. 17:25:35order summary on the page it's taking up
  20394. 17:25:37a lot of space and it pushes the test
  20395. 17:25:39results to the bottom so what we can do
  20396. 17:25:42is after our test we can actually remove
  20397. 17:25:45all of this
  20398. 17:25:46HTML so let's go back to our code
  20399. 17:25:50in the order summary test file at the
  20400. 17:25:53end of the first test we'll create some
  20401. 17:25:55new lines and remove the HTML that we
  20402. 17:25:58created to do that we can scroll up to
  20403. 17:26:02this code so to remove the HTML we can
  20404. 17:26:06just set this element inner HTML to the
  20405. 17:26:09empty string so we'll select this code
  20406. 17:26:13right click
  20407. 17:26:15copy and then down here right click and
  20408. 17:26:20and
  20409. 17:26:21paste so in our test all the HTML we
  20410. 17:26:25created was placed inside this test
  20411. 17:26:28container and we're just going to remove
  20412. 17:26:30all that
  20413. 17:26:31HTML by setting the inner HTML to the
  20414. 17:26:35empty string and we'll do the same thing
  20415. 17:26:38for the second test so we'll select this
  20416. 17:26:40code rightclick
  20417. 17:26:43copy and then down here at the
  20418. 17:26:47bottom we'll right click and
  20419. 17:26:50paste so this is just some cleanup we
  20420. 17:26:53can do at the end of each test let's
  20421. 17:26:56save and look at our test
  20422. 17:26:59page and now all the HTML that we
  20423. 17:27:02generated is gone and we can see the
  20424. 17:27:04test results more
  20425. 17:27:08clearly lastly we're going to learn a
  20426. 17:27:11shortcut we can use in Jasmine called
  20427. 17:27:14hooks a hook lets us run some code for
  20428. 17:27:18each test for example
  20429. 17:27:21if we go back to our code notice that
  20430. 17:27:24before each of our tests we do a bunch
  20431. 17:27:26of setup here so we can actually share
  20432. 17:27:29all of this code between our two tests
  20433. 17:27:32using a hook so to create a hook we're
  20434. 17:27:36going to scroll up to outside of both of
  20435. 17:27:40these
  20436. 17:27:41tests and at the top of the test Suite
  20437. 17:27:44we're going to use another function
  20438. 17:27:46provided by Jasmine called before
  20439. 17:27:50capital E
  20440. 17:27:52each brackets and we'll give this a
  20441. 17:27:56function brackets arrow and curler
  20442. 17:28:00brackets so this is called a before each
  20443. 17:28:04hook and it will run this function
  20444. 17:28:07before each of our
  20445. 17:28:09tests so inside this function we can put
  20446. 17:28:12our setup code so that we run the setup
  20447. 17:28:14code before each of our
  20448. 17:28:17tests let's actually take the setup code
  20449. 17:28:19for the second test because it's a
  20450. 17:28:22little more
  20451. 17:28:23detailed so down here we're going to
  20452. 17:28:25select the code starting from render
  20453. 17:28:27order summary all the way up to mocking
  20454. 17:28:31local storage. set item and we'll
  20455. 17:28:34rightclick and cut this
  20456. 17:28:37code and remove these new
  20457. 17:28:40lines and then at the top inside before
  20458. 17:28:44each we're going to rightclick and paste
  20459. 17:28:50so now before each of our tests it's
  20460. 17:28:52going to run this function and it's
  20461. 17:28:54going to run all of our setup code so
  20462. 17:28:57this is a great way to share code
  20463. 17:28:59between our tests and remove
  20464. 17:29:02duplication so now that we're running
  20465. 17:29:04this inside before each if we scroll
  20466. 17:29:07down to the first test we don't have to
  20467. 17:29:09run this code again so we can actually
  20468. 17:29:12remove
  20469. 17:29:13it and we'll remove this Mock and we'll
  20470. 17:29:18remove the render order summary as
  20471. 17:29:20W and all we're left with are the
  20472. 17:29:23expectations for our
  20473. 17:29:26test now before we save we need to fix
  20474. 17:29:29one thing with this code here so right
  20475. 17:29:32now these two variables are inside this
  20476. 17:29:36function and because of scope these will
  20477. 17:29:39be contained inside this function and we
  20478. 17:29:42can't use it down here in our
  20479. 17:29:44tests so to fix this we can just move
  20480. 17:29:47these variables outside of this function
  20481. 17:29:50up here so we're going to select these
  20482. 17:29:52lines and then right click
  20483. 17:29:56cut and up
  20484. 17:29:59here we're going to right click and
  20485. 17:30:04paste so now any code inside this outer
  20486. 17:30:07function can use these two variables
  20487. 17:30:10including our
  20488. 17:30:12tests so now let's
  20489. 17:30:15save and check the test
  20490. 17:30:18page and everything is still still
  20491. 17:30:20passing so that's how we use a hook to
  20492. 17:30:23run some code before each test now
  20493. 17:30:27before each is not the only hook that we
  20494. 17:30:29can use Jasmine provides several hooks
  20495. 17:30:32that we can use in our
  20496. 17:30:35tests another common Hook is after each
  20497. 17:30:38which lets us run a function after each
  20498. 17:30:41test so this is really useful for
  20499. 17:30:44cleanup code like we have down here when
  20500. 17:30:47we remove the
  20501. 17:30:48HTML all leave using after each as an
  20502. 17:30:52exercise now let's go into git and save
  20503. 17:30:55our changes and I can close these three
  20504. 17:30:59files at the top for
  20505. 17:31:01now so we'll open the git
  20506. 17:31:04section and then inside orders summary.
  20507. 17:31:08JS we added some classes that we're
  20508. 17:31:11going to use in our
  20509. 17:31:13tests inside test.html we are loading
  20510. 17:31:17our order summary test file and we also
  20511. 17:31:20created a test container where we're
  20512. 17:31:23going to put our HTML that we
  20513. 17:31:26generate and then for the order summary
  20514. 17:31:29test file this contains the test that we
  20515. 17:31:32created for the order
  20516. 17:31:34summary now let's create a message
  20517. 17:31:36describing our
  20518. 17:31:38changes create automated tests for order
  20519. 17:31:46summary and commit
  20520. 17:31:54now that we've created all these tests
  20521. 17:31:56for our project when we make some
  20522. 17:31:59changes to our code we can just open the
  20523. 17:32:02testing page and it will rerun all of
  20524. 17:32:05our
  20525. 17:32:06tests so here's the process that we do
  20526. 17:32:08when we write code first we make changes
  20527. 17:32:12to our code and then we rerun the tests
  20528. 17:32:15to make sure that everything still works
  20529. 17:32:18and finally we save our changes into
  20530. 17:32:22git now before we end the lesson we're
  20531. 17:32:25going to do one more cleanup we'll go
  20532. 17:32:27back to our
  20533. 17:32:29code and we'll open our
  20534. 17:32:33files and here we usually organize all
  20535. 17:32:36of our tests into a folder called tests
  20536. 17:32:40however in this lesson we named this
  20537. 17:32:43folder test- Jasmin because we wanted to
  20538. 17:32:46compare this folder to tests that were
  20539. 17:32:48created without a testing framework but
  20540. 17:32:52now that the lesson's over we're going
  20541. 17:32:54to rename test- Jasmine to just test to
  20542. 17:32:57make it more
  20543. 17:32:59standard first we need to rename this
  20544. 17:33:01folder up here so we'll right
  20545. 17:33:04click
  20546. 17:33:06rename and let's rename it to test-
  20547. 17:33:10simple because this folder contains
  20548. 17:33:13tests that were created without a
  20549. 17:33:16framework let's press enter
  20550. 17:33:19and now we can rename test- Jasmine to
  20551. 17:33:22test by right clicking
  20552. 17:33:26rename and rename it to tests and press
  20553. 17:33:32enter another thing we can do is we're
  20554. 17:33:35going to drag test- simple into the test
  20555. 17:33:38folder so that all of our tests are
  20556. 17:33:41contained in one
  20557. 17:33:44folder so if it asks you to update the
  20558. 17:33:47Imports you can click yes
  20559. 17:33:51and we also need to open money test.js
  20560. 17:33:54in the test- simple folder because it
  20561. 17:33:56updated the Imports and then save this
  20562. 17:34:03file finally we're going to run all of
  20563. 17:34:06our tests again to make sure everything
  20564. 17:34:08still works after we move things around
  20565. 17:34:12so because test.html is inside a
  20566. 17:34:15different folder now we need to reopen
  20567. 17:34:18this with live server
  20568. 17:34:20so we'll rightclick and click open with
  20569. 17:34:23live
  20570. 17:34:26server and it'll show us that all the
  20571. 17:34:28tests are still passing so our code is
  20572. 17:34:31still
  20573. 17:34:32correct so now we can close some of
  20574. 17:34:34these extra tabs
  20575. 17:34:37here and save our changes into git we'll
  20576. 17:34:42open the git
  20577. 17:34:44section and all of these changes are
  20578. 17:34:47related to renaming the folders
  20579. 17:34:50so we'll give a message at the
  20580. 17:34:52top rename test- Jasmine
  20581. 17:34:58folder to
  20582. 17:35:00tests and
  20583. 17:35:08commit and that's the end of this lesson
  20584. 17:35:11in this lesson we learned how to test
  20585. 17:35:13our code using manual and automated
  20586. 17:35:16tests we learned how to organize our
  20587. 17:35:19test using test cases and test Suites we
  20588. 17:35:23learned how to use a testing framework
  20589. 17:35:25like Jasmine to help us write tests
  20590. 17:35:28easier we learned how to Mock and spy on
  20591. 17:35:32methods we learned how to test web pages
  20592. 17:35:35using integration tests and we learned
  20593. 17:35:38about hooks which lets us share code
  20594. 17:35:41between our
  20595. 17:35:43tests here are some exercises to help
  20596. 17:35:46you practice what we learned in this
  20597. 17:35:48lesson e
  20598. 17:36:33in the rest of this course if you get an
  20599. 17:36:36error in your project but your code
  20600. 17:36:38looks correct you may have some bad data
  20601. 17:36:41saved in local storage to fix this you
  20602. 17:36:44can try running local storage. CLE in
  20603. 17:36:48your console to remove everything from
  20604. 17:36:50local
  20605. 17:36:51storage and then refresh the
  20606. 17:36:56page if you see an error like this
  20607. 17:36:59object object that means that you saved
  20608. 17:37:02something directly into local storage
  20609. 17:37:04but you have to jason. stringify it
  20610. 17:37:06first so check your code to make sure
  20611. 17:37:09that you're always stringifying before
  20612. 17:37:11saving to local storage and then clear
  20613. 17:37:14your local storage and refresh the page
  20614. 17:37:20in this lesson we're going to learn
  20615. 17:37:22objectoriented
  20616. 17:37:24programming objectoriented programming
  20617. 17:37:27or oop is another style of programming
  20618. 17:37:31so it's another way that we write our
  20619. 17:37:33code it's useful to know because many
  20620. 17:37:36programming languages use
  20621. 17:37:40oop now how do we use objectoriented
  20622. 17:37:43programming object-oriented programming
  20623. 17:37:46is basically organizing our code into
  20624. 17:37:49into
  20625. 17:37:50objects let's look at an
  20626. 17:37:53example we're going to go to our
  20627. 17:37:56code and we're going to scroll up and
  20628. 17:37:59open data/ cart.
  20629. 17:38:03JS inside this file we have the code for
  20630. 17:38:06our cart and you'll notice that for the
  20631. 17:38:09cart we have some data at the top and
  20632. 17:38:12then we have a bunch of functions at the
  20633. 17:38:14bottom that modify the cart and these
  20634. 17:38:18are separate from each other the data
  20635. 17:38:19and the functions so this style of
  20636. 17:38:22programming that we've been using
  20637. 17:38:23throughout this course is called
  20638. 17:38:25procedural programming procedure means a
  20639. 17:38:29set of step-by-step
  20640. 17:38:32instructions and this is basically a
  20641. 17:38:35function so in procedural programming we
  20642. 17:38:38organize our code into separate
  20643. 17:38:40functions like we did
  20644. 17:38:43here on the other hand in objectoriented
  20645. 17:38:46programming we organize all of this code
  20646. 17:38:49into an object so we put everything
  20647. 17:38:52together into an
  20648. 17:38:55object so let's do an example and
  20649. 17:38:58convert this cart code into
  20650. 17:39:00objectoriented
  20651. 17:39:02programming we're going to click cart.
  20652. 17:39:05JS and inside this folder we're going to
  20653. 17:39:08create a new file by clicking
  20654. 17:39:12here and let's name this file
  20655. 17:39:15C- oop or objectoriented
  20656. 17:39:19programming. JS and press
  20657. 17:39:24enter and now we're going to copy all of
  20658. 17:39:27the code in cart into this new file so
  20659. 17:39:30let's open this and we'll click in this
  20660. 17:39:34file and then we'll type control a or
  20661. 17:39:36command a to select all the code and
  20662. 17:39:40then right click
  20663. 17:39:43copy and inside C- oop we're going to
  20664. 17:39:47rightclick and paste
  20665. 17:39:52and let's scroll back up to the
  20666. 17:39:54top and now to convert this code into
  20667. 17:39:57objectoriented programming we're going
  20668. 17:39:59to group all the data and the functions
  20669. 17:40:02together into an object first at the top
  20670. 17:40:07let's create an object that we can put
  20671. 17:40:09all of these inside
  20672. 17:40:10of so we'll create a variable const
  20673. 17:40:14let's call it cart equals and object
  20674. 17:40:20and now we're going to group our data
  20675. 17:40:22and all of our functions into this
  20676. 17:40:25object so let's do it step by step and
  20677. 17:40:28we'll start with this cart
  20678. 17:40:30variable as a reminder cart semicolon is
  20679. 17:40:34actually a shortcut for cart equals
  20680. 17:40:37undefined to make things easier to
  20681. 17:40:40understand let's actually write this out
  20682. 17:40:42in full instead of using the shortcut so
  20683. 17:40:45we'll type cart equals undefined
  20684. 17:40:51and now we're going to move this into
  20685. 17:40:53the object so we'll select this
  20686. 17:40:56line and then
  20687. 17:40:59rightclick
  20688. 17:41:01cut and then inside the object we're
  20689. 17:41:05going to rightclick and
  20690. 17:41:09paste now inside an object we can't use
  20691. 17:41:12the word export or
  20692. 17:41:15let so to move a variable into an object
  20693. 17:41:18we're going to convert it into a
  20694. 17:41:20property and a value so we'll remove
  20695. 17:41:23these
  20696. 17:41:24words and then type cart and colon
  20697. 17:41:29undefined and remove the semicolon and
  20698. 17:41:32replace it with a
  20699. 17:41:34comma so this is the same thing as cart
  20700. 17:41:37equals undefined but we move that value
  20701. 17:41:40into an
  20702. 17:41:42object now one problem here is that if
  20703. 17:41:45we access this cart property we're going
  20704. 17:41:48to do cart. cart which can be a bit
  20705. 17:41:50confusing so let's actually rename this
  20706. 17:41:53property to cart capital I items instead
  20707. 17:41:58to make it a bit more clear that this
  20708. 17:42:00contains the items in the cart or the
  20709. 17:42:03products that are in the
  20710. 17:42:07cart next we're going to group all of
  20711. 17:42:10our functions into this object as well
  20712. 17:42:14so let's create some new
  20713. 17:42:16lines and we're going to move this
  20714. 17:42:18function inside here so let's select
  20715. 17:42:21this
  20716. 17:42:24function and then
  20717. 17:42:26rightclick
  20718. 17:42:28cut and inside the
  20719. 17:42:31object we're going to
  20720. 17:42:33rightclick and
  20721. 17:42:36paste and again we can't use the word
  20722. 17:42:39export inside an object so we're going
  20723. 17:42:41to convert this into a property and
  20724. 17:42:44value as
  20725. 17:42:45well so let's remove these words
  20726. 17:42:49and let's make the property name load
  20727. 17:42:51from storage and then we'll type a colon
  20728. 17:42:55and we'll make the value a
  20729. 17:42:58function brackets and curly
  20730. 17:43:00brackets so that's how we move a
  20731. 17:43:03function into this object now make sure
  20732. 17:43:06that you use a regular function syntax
  20733. 17:43:09here and not an arrow function and I'll
  20734. 17:43:11explain why later in this
  20735. 17:43:14lesson now as a reminder when we have a
  20736. 17:43:17function inside an object
  20737. 17:43:20we call this a
  20738. 17:43:22method also JavaScript has a shortcut
  20739. 17:43:25for methods called the shorthand method
  20740. 17:43:29syntax so instead of typing colon and
  20741. 17:43:32function we can actually just remove
  20742. 17:43:34this
  20743. 17:43:36code and just type brackets and curly
  20744. 17:43:38brackets after the function name and
  20745. 17:43:41this is a shortcut for the code that we
  20746. 17:43:44had
  20747. 17:43:46before now the code inside this function
  20748. 17:43:49doesn't actually work anymore because
  20749. 17:43:52cart was actually renamed to cart items
  20750. 17:43:55so we have to rename this variable so
  20751. 17:43:58instead of cart we're going to do cart
  20752. 17:44:01dot cart
  20753. 17:44:03items to access this value and same
  20754. 17:44:07thing here cart do cart
  20755. 17:44:10items and here cart
  20756. 17:44:14doart
  20757. 17:44:17items so so this code Works however we
  20758. 17:44:21have a small problem here if we change
  20759. 17:44:24the name of the object up here this code
  20760. 17:44:28will no longer
  20761. 17:44:29work to solve this problem JavaScript
  20762. 17:44:32has a feature called this this gives us
  20763. 17:44:37the object that contains this function
  20764. 17:44:40so it gives us this outer object up here
  20765. 17:44:43so to improve our code we're going to
  20766. 17:44:45change from using a variable named cart
  20767. 17:44:49to this and here as well this. cart
  20768. 17:44:54items and this Dot card
  20769. 17:44:58items so this will give us the outer
  20770. 17:45:01object and now this code will always
  20771. 17:45:04work and it doesn't matter what the
  20772. 17:45:06variable name
  20773. 17:45:08is the last thing we're going to change
  20774. 17:45:10in this function is we're currently
  20775. 17:45:12loading from local storage
  20776. 17:45:15cart however our original cart was also
  20777. 17:45:18loading from local storage cart so let's
  20778. 17:45:21actually change this to C- oop so that
  20779. 17:45:25we don't affect our original
  20780. 17:45:32cart let's move on and we're going to
  20781. 17:45:34move the other functions into this
  20782. 17:45:37object so let's go down to the next
  20783. 17:45:40function which is save to storage we're
  20784. 17:45:43going to select this and then
  20785. 17:45:45rightclick
  20786. 17:45:47cut and then inside this object at the
  20787. 17:45:51end we're going to add a comma and some
  20788. 17:45:54new
  20789. 17:45:54lines and we're going to rightclick and
  20790. 17:46:00paste and we can't use the word function
  20791. 17:46:02at the front here so let's remove this
  20792. 17:46:06word and just use the shorthand method
  20793. 17:46:09syntax so this is a shortcut for colon
  20794. 17:46:15function and here we're saving to local
  20795. 17:46:18storage cart
  20796. 17:46:19so let's also change this to C- oop so
  20797. 17:46:23we don't affect our original cart so
  20798. 17:46:26we'll change it to card-
  20799. 17:46:31oop another thing we need to change is
  20800. 17:46:34that the cart was renamed to cart
  20801. 17:46:37items so here we're going to change from
  20802. 17:46:41cart to this which gives us the outer
  20803. 17:46:45object and then dot cart items
  20804. 17:46:49which is this property so it should do
  20805. 17:46:52the same thing as before except this
  20806. 17:46:54function is now inside an
  20807. 17:46:56object let's continue and move another
  20808. 17:46:59function into the object so we're going
  20809. 17:47:02to do add to cart this time so let's
  20810. 17:47:05select this
  20811. 17:47:07code and then
  20812. 17:47:10rightclick
  20813. 17:47:11cut and inside the object we're going to
  20814. 17:47:15add a comma and some new lines and then
  20815. 17:47:18rightclick and
  20816. 17:47:20paste so again instead of using export
  20817. 17:47:23function we're just going to remove this
  20818. 17:47:26code and use the shorthand method syntax
  20819. 17:47:30which is a shortcut for this
  20820. 17:47:33code and now let's update the code
  20821. 17:47:35inside this function as well so instead
  20822. 17:47:38of the cart we rename this to this. cart
  20823. 17:47:42items so we'll change it to this do cart
  20824. 17:47:46items
  20825. 17:47:48and then down here we're going to change
  20826. 17:47:51from cart to this. cart
  20827. 17:47:56items and also the function save to
  20828. 17:47:59storage was moved inside the object so
  20829. 17:48:03now to access this function we're going
  20830. 17:48:06to get the outer object again using this
  20831. 17:48:09and then dot save to storage so down
  20832. 17:48:13here we'll add this dot in front to
  20833. 17:48:18access the function inside the
  20834. 17:48:21object and now let's move on and move
  20835. 17:48:25the next function into the object so
  20836. 17:48:27we'll select this
  20837. 17:48:29code and then
  20838. 17:48:31rightclick
  20839. 17:48:33cut inside the object add a comma and
  20840. 17:48:36some new lines and right click and
  20841. 17:48:40paste we'll remove export
  20842. 17:48:44function and use the shorthand method
  20843. 17:48:47syntax and again we'll change from cart
  20844. 17:48:50to this. cart
  20845. 17:48:54items we're going to change cart here to
  20846. 17:48:57this.c cart
  20847. 17:49:00items and change save to storage to this
  20848. 17:49:04do save to
  20849. 17:49:06storage so we access save to storage
  20850. 17:49:09inside this
  20851. 17:49:11object next let's move this last
  20852. 17:49:14function into the object so we'll select
  20853. 17:49:17the code and then rightclick
  20854. 17:49:21cut and inside the object we're going to
  20855. 17:49:24type a comma and some new lines right
  20856. 17:49:28click and
  20857. 17:49:30paste we'll remove export function and
  20858. 17:49:34use the shorthand method
  20859. 17:49:36syntax and update the code in this
  20860. 17:49:38method so we'll change cart to this do
  20861. 17:49:42cart
  20862. 17:49:44items and save to storage to this Dove
  20863. 17:49:49to
  20864. 17:49:50storage now let's remove the extra
  20865. 17:49:56lines and the last thing we need to
  20866. 17:49:58update is that load from Storage was
  20867. 17:50:01moved inside the cart object so to
  20868. 17:50:04access this function at the front we're
  20869. 17:50:07going to type
  20870. 17:50:09cart. load from storage and save this
  20871. 17:50:14file and that's it this is the basic
  20872. 17:50:18idea behind objectoriented programming
  20873. 17:50:22we organize our code into objects so we
  20874. 17:50:25group the data and the functions
  20875. 17:50:28together into an object like
  20876. 17:50:33this now let's talk about why do we use
  20877. 17:50:36objectoriented
  20878. 17:50:38programming objectoriented programming
  20879. 17:50:41tries to represent the real
  20880. 17:50:44world in the real world a cart is a
  20881. 17:50:47physical object object so in
  20882. 17:50:49objectoriented programming we represent
  20883. 17:50:52this physical object as a digital object
  20884. 17:50:55or in this case a JavaScript
  20885. 17:50:58object a physical cart can have products
  20886. 17:51:01inside so our JavaScript object can also
  20887. 17:51:05have products
  20888. 17:51:07inside a physical cart also has actions
  20889. 17:51:11we can take to modify the cart like we
  20890. 17:51:14can add a product or remove a product
  20891. 17:51:20our JavaScript object also has actions
  20892. 17:51:23we can take to modify the cart like the
  20893. 17:51:27add to cart function or the remove from
  20894. 17:51:30cart
  20895. 17:51:31function so as you can see
  20896. 17:51:34objectoriented programming tries to
  20897. 17:51:36represent the real world we take a
  20898. 17:51:39physical object like a cart and
  20899. 17:51:42represent it as a digital object like a
  20900. 17:51:45JavaScript
  20901. 17:51:46object and some people feel feel that
  20902. 17:51:48this makes the code more intuitive or
  20903. 17:51:51easier to
  20904. 17:51:55understand now let's try out the code in
  20905. 17:51:58this object to make sure it actually
  20906. 17:52:00works we're going to scroll down to the
  20907. 17:52:04bottom and
  20908. 17:52:06here we're just going to type
  20909. 17:52:10console.log the
  20910. 17:52:12cart and save this
  20911. 17:52:15file and now let's load this code into
  20912. 17:52:19the project to do that we're going to
  20913. 17:52:22open the scripts folder and open the
  20914. 17:52:24file checkout.
  20915. 17:52:27JS and at the top below the Imports
  20916. 17:52:31we're going to import our new file so
  20917. 17:52:34let's type
  20918. 17:52:36import and we're going to use a slightly
  20919. 17:52:38different syntax we're just going to
  20920. 17:52:40type a
  20921. 17:52:41string and give it the file that we want
  20922. 17:52:44to load so we need to load C- oop. Js
  20923. 17:52:49so first we need to get out of the
  20924. 17:52:51scripts folder using do slash and then
  20925. 17:52:54go into the data folder data slash and
  20926. 17:52:58load C-
  20927. 17:53:00oop.
  20928. 17:53:02JS so this is another syntax we can use
  20929. 17:53:06for imports this just runs all the code
  20930. 17:53:10inside this file without importing
  20931. 17:53:13anything which is what we need in this
  20932. 17:53:15situation so let's save this file
  20933. 17:53:20and then open this page in the browser
  20934. 17:53:23so if you don't have this page open you
  20935. 17:53:25can scroll down to checkout. HTML and
  20936. 17:53:28then right click and open with live
  20937. 17:53:31server I already have this page open so
  20938. 17:53:35I'm just going to open that
  20939. 17:53:37Tab and if the page is too big we can
  20940. 17:53:40zoom out by pressing control minus or
  20941. 17:53:43command minus a few
  20942. 17:53:45times and now let's check the console to
  20943. 17:53:48see what the cart looks like on this
  20944. 17:53:51page we're going to
  20945. 17:53:52rightclick and click
  20946. 17:53:55inspect and then open the
  20947. 17:53:59console and in the console we'll see
  20948. 17:54:02this cart object so inside this object
  20949. 17:54:05we have the data as well as all the
  20950. 17:54:08functions for our cart but it's grouped
  20951. 17:54:11inside an object and you'll see that the
  20952. 17:54:14code still works the same as before cart
  20953. 17:54:17items has a the two default
  20954. 17:54:20products if we look at our code and open
  20955. 17:54:24card-
  20956. 17:54:25o at the beginning we run cart. load
  20957. 17:54:29from storage and then inside this
  20958. 17:54:32function if we scroll up it will give
  20959. 17:54:35the cart items this default cart with
  20960. 17:54:38two products inside and that's what we
  20961. 17:54:40see in the console
  20962. 17:54:44here so we know that the code inside
  20963. 17:54:46this object still works works the same
  20964. 17:54:48way as
  20965. 17:54:51before so now let's try another method
  20966. 17:54:54to make sure that it's working so let's
  20967. 17:54:56try using the add to cart method to add
  20968. 17:55:00a product to the cart items up here so
  20969. 17:55:03we'll scroll
  20970. 17:55:05down to the bottom and then above
  20971. 17:55:08console.log cart we're going to call add
  20972. 17:55:11to cart now add to cart was saved inside
  20973. 17:55:16this cart object so to access it we're
  20974. 17:55:19going to type
  20975. 17:55:21cart. add to cart and
  20976. 17:55:26brackets now add to cart takes one
  20977. 17:55:29parameter which is a string and this is
  20978. 17:55:32the product ID that we want to add so to
  20979. 17:55:36find a product ID we can go to the data
  20980. 17:55:39folder and products. JS and then inside
  20981. 17:55:43here we have this list of products so
  20982. 17:55:47let's actually scroll down to the third
  20983. 17:55:49product which is right here so this
  20984. 17:55:52t-shirt product and we're going to add
  20985. 17:55:54this product ID to the cart so let's
  20986. 17:55:58select this ID we're going to
  20987. 17:56:01rightclick
  20988. 17:56:03copy and then inside C-
  20989. 17:56:06oop inside the string we're going to
  20990. 17:56:09rightclick and paste that product
  20991. 17:56:12ID and let's
  20992. 17:56:15save and let's check the console
  20993. 17:56:18so I'll open the console
  20994. 17:56:20Here and Now inside the cart object if
  20995. 17:56:24we expand it cart items now has three
  20996. 17:56:28products because we just added a new
  20997. 17:56:30product to the cart so this shows that
  20998. 17:56:33all of the code is working the exact
  20999. 17:56:35same way as before we just organized it
  21000. 17:56:39into an object and that's objectoriented
  21001. 17:56:44programming now that we're done with
  21002. 17:56:46this example let's remove these two
  21003. 17:56:49lines of
  21004. 17:56:50code and
  21005. 17:56:56save next another reason we use
  21006. 17:56:59objectoriented programming is it's easy
  21007. 17:57:02to create multiple objects for example
  21008. 17:57:06the real amazon.com has a feature called
  21009. 17:57:09Amazon business where you can have a
  21010. 17:57:11separate cart for your business
  21011. 17:57:14purchases so how do we create two
  21012. 17:57:16separate carts in our code with
  21013. 17:57:19objectoriented programming this is easy
  21014. 17:57:23we can just make a copy of the cart
  21015. 17:57:26object so we can scroll up in this
  21016. 17:57:29code and I'm just going to click this
  21017. 17:57:32Arrow here beside the card variable to
  21018. 17:57:34collapse this code and to create a
  21019. 17:57:37separate cart for business purchases
  21020. 17:57:40we're just going to select this code and
  21021. 17:57:43then right click
  21022. 17:57:46copy and at the
  21023. 17:57:48bottom right click and
  21024. 17:57:53paste and now we have two separate cards
  21025. 17:57:57so let's scroll up to the top of the
  21026. 17:57:59second cart which is right here and we
  21027. 17:58:03said that we wanted this card for
  21028. 17:58:05business purchases so let's rename this
  21029. 17:58:08to
  21030. 17:58:10business Capital C
  21031. 17:58:13cart and when we load from Storage let's
  21032. 17:58:17load it from cart Das business instead
  21033. 17:58:21so that it doesn't conflict with the
  21034. 17:58:23cart up
  21035. 17:58:24here and if we scroll down same thing in
  21036. 17:58:28save to storage let's save this cart to
  21037. 17:58:31cart Das
  21038. 17:58:34business and finally let's scroll down
  21039. 17:58:36to the
  21040. 17:58:38bottom and here instead of cart. load
  21041. 17:58:41from Storage we're going to change this
  21042. 17:58:43to business cart. load from Storage
  21043. 17:58:50and that's it we just created a
  21044. 17:58:52completely separate cart simply by
  21045. 17:58:55copying this object so object-oriented
  21046. 17:58:58programming makes it easy to create
  21047. 17:59:01multiple
  21048. 17:59:05objects so now let's console.log both of
  21049. 17:59:08these cards to see what they look like
  21050. 17:59:10we're going to scroll to the
  21051. 17:59:12bottom and here we're going to
  21052. 17:59:16console.log the
  21053. 17:59:18cart and
  21054. 17:59:21console.log the business cart and
  21055. 17:59:28save and if we open the console for the
  21056. 17:59:30checkout
  21057. 17:59:32page it will show us that we have two
  21058. 17:59:35carts the regular cart has three
  21059. 17:59:38products inside and the business cart
  21060. 17:59:41has two products inside and these are
  21061. 17:59:44just the default products that we get
  21062. 17:59:46when we first load the cart
  21063. 17:59:48so you can see that these two cards are
  21064. 17:59:51completely separate from each
  21065. 17:59:53other in the real world we can have two
  21066. 17:59:56physical cards that are completely
  21067. 17:59:58separate from each other and in
  21068. 18:00:00programming we can also have two cards
  21069. 18:00:02that are completely separate from each
  21070. 18:00:04other by simply making a copy of the
  21071. 18:00:08object so again object-oriented
  21072. 18:00:11programming tries to represent the real
  21073. 18:00:13world
  21074. 18:00:19now currently there's an obvious problem
  21075. 18:00:21with how we're creating multiple objects
  21076. 18:00:24which is we're copy pasting a lot of
  21077. 18:00:27code and this can make our code messy
  21078. 18:00:29and hard to manage so now we're going to
  21079. 18:00:32make our code cleaner by using a
  21080. 18:00:34function to create multiple
  21081. 18:00:37objects so first let's delete this
  21082. 18:00:40business card that we copy pasted so
  21083. 18:00:43we're going to select this
  21084. 18:00:46object and then up to here the business
  21085. 18:00:49card we're going to remove
  21086. 18:00:51it and now instead of copy pasting this
  21087. 18:00:54object we're going to use a function to
  21088. 18:00:56generate these objects so at the top of
  21089. 18:00:59this file let's create a new
  21090. 18:01:03function and we'll name it capital c
  21091. 18:01:06cart brackets and curly
  21092. 18:01:10brackets so by the way in objectoriented
  21093. 18:01:14programming a naming convention is to
  21094. 18:01:16use Pascal case for things that create
  21095. 18:01:19or generate objects in Pascal case we
  21096. 18:01:22start every word with a capital
  21097. 18:01:24including the first word so that's why
  21098. 18:01:27we started cart with a Capital
  21099. 18:01:29C now inside this function we're going
  21100. 18:01:32to create cart
  21101. 18:01:34objects so let's actually move this
  21102. 18:01:36object into the function so we can
  21103. 18:01:38create it using the function so we're
  21104. 18:01:41going to select this object to here and
  21105. 18:01:44then we'll right
  21106. 18:01:45click and cut
  21107. 18:01:49and then move it into the function by
  21108. 18:01:51clicking in the function and then
  21109. 18:01:53rightclick and
  21110. 18:01:56paste so now every time we run this
  21111. 18:01:59function it will generate a new cart
  21112. 18:02:01object and we don't have to copy paste
  21113. 18:02:04this code over and over to create
  21114. 18:02:06multiple
  21115. 18:02:07objects so at the bottom I'm going to
  21116. 18:02:10click this Arrow to collapse this object
  21117. 18:02:12for now and at the bottom of this
  21118. 18:02:14function we're going to return this C
  21119. 18:02:18object so we can use it outside the
  21120. 18:02:22function okay now let's give this a try
  21121. 18:02:25and we'll use this function to generate
  21122. 18:02:28cart objects so first let's create the
  21123. 18:02:31normal cart using const cart make it
  21124. 18:02:35equal and we'll run this function to
  21125. 18:02:38generate an object Capital C cart and
  21126. 18:02:43brackets and let's generate another
  21127. 18:02:45object using the function using const
  21128. 18:02:49and we'll name it business
  21129. 18:02:52cart equals Capital C cart and
  21130. 18:02:57brackets and we'll still load from
  21131. 18:03:00storage for both of these and then
  21132. 18:03:02console log both of
  21133. 18:03:03these so basically instead of copy
  21134. 18:03:06pasting all the time we're going to use
  21135. 18:03:08a function to generate
  21136. 18:03:10objects so now let's
  21137. 18:03:14save and then open the
  21138. 18:03:16console and and now we have two cart
  21139. 18:03:18objects
  21140. 18:03:20again however now both of these carts
  21141. 18:03:23have three products
  21142. 18:03:25inside but before the regular card had
  21143. 18:03:28three products and the business card had
  21144. 18:03:30two
  21145. 18:03:32products so what
  21146. 18:03:33happened if we go back to our code and
  21147. 18:03:37look inside this cart
  21148. 18:03:40object you'll notice that we're loading
  21149. 18:03:42from the same key in local storage which
  21150. 18:03:45is C- o that means every cart that we
  21151. 18:03:48generate will be getting its data from
  21152. 18:03:50the same place in local
  21153. 18:03:53storage so to fix this we're going to
  21154. 18:03:56replace C- oop with a parameter so
  21155. 18:04:00different carts can use a different key
  21156. 18:04:02in local storage so at the top let's
  21157. 18:04:06create a parameter called local storage
  21158. 18:04:11key and then instead of using the same
  21159. 18:04:14string every time we're going to use the
  21160. 18:04:16parameter local storage
  21161. 18:04:19key and the same thing for save to
  21162. 18:04:23storage instead of saving to the same
  21163. 18:04:25key every time we're going to replace
  21164. 18:04:27this with the parameter local storage
  21165. 18:04:33key and finally when we run this
  21166. 18:04:36function at the bottom
  21167. 18:04:39here we need to give the function a
  21168. 18:04:42local storage key so for our regular
  21169. 18:04:45cart let's use the string cart C-
  21170. 18:04:49oop and for the business cart we can use
  21171. 18:04:53a different key like the string cart Das
  21172. 18:04:57business so now these two carts will
  21173. 18:05:00load from different keys in local
  21174. 18:05:02storage and they can have different data
  21175. 18:05:05so let's save and then check the
  21176. 18:05:09console and now our regular card has
  21177. 18:05:12three products and our business card has
  21178. 18:05:14two products like
  21179. 18:05:16before so let's go back to our code and
  21180. 18:05:20this is a better way to create multiple
  21181. 18:05:22objects instead of copy pasting a bunch
  21182. 18:05:25of code every time we can create a
  21183. 18:05:27function that generates
  21184. 18:05:31objects so now let's save this example
  21185. 18:05:34into git we'll go into our git
  21186. 18:05:37section and then at the top here we
  21187. 18:05:40created a function to generate these
  21188. 18:05:43objects and in checkout. JS we are
  21189. 18:05:46loading that c - oop practice file so
  21190. 18:05:50we'll type a message to describe these
  21191. 18:05:52changes create oop or objectoriented
  21192. 18:05:58programming version of cart and
  21193. 18:06:10commit next we're going to learn an even
  21194. 18:06:13better way to generate objects
  21195. 18:06:18object-oriented programming has a
  21196. 18:06:20feature called a Class A Class is
  21197. 18:06:23specifically designed for generating
  21198. 18:06:26objects a class is basically an object
  21199. 18:06:32generator So currently if we open our
  21200. 18:06:35code and then open C-
  21201. 18:06:38oop we learn how to use a function to
  21202. 18:06:42generate objects in objectoriented
  21203. 18:06:45programming so now we're going to switch
  21204. 18:06:47this to using a class to generate these
  21205. 18:06:50objects instead because classes are
  21206. 18:06:53cleaner and have more features than
  21207. 18:06:55using a function so first let's create a
  21208. 18:06:58new file for learning
  21209. 18:07:00classes inside the data folder we're
  21210. 18:07:03going to click C- oop and then create a
  21211. 18:07:07new
  21212. 18:07:08file and we'll name this file cart DCL
  21213. 18:07:12class. JS and press enter
  21214. 18:07:18so first let's copy over all the code so
  21215. 18:07:20we can compare it we're going to open C-
  21216. 18:07:23oop click this file and then press
  21217. 18:07:26contrl a or command a to select the code
  21218. 18:07:29and right
  21219. 18:07:31click
  21220. 18:07:32copy and in
  21221. 18:07:35C-Class we're going to right click and
  21222. 18:07:40paste and we'll scroll to the
  21223. 18:07:43top and at the top we're going to create
  21224. 18:07:46our first class class using the word
  21225. 18:07:50class and now we're going to give a name
  21226. 18:07:53to this class so let's name it the same
  21227. 18:07:56as here Capital C cart and then curly
  21228. 18:08:02brackets as a reminder a naming
  21229. 18:08:05convention is to use Pascal case for
  21230. 18:08:08things that generate objects so that's
  21231. 18:08:10why we start this cart with a Capital
  21232. 18:08:12C so this is a class A Class is
  21233. 18:08:17basically an object
  21234. 18:08:20generator so now inside the class we're
  21235. 18:08:23going to put the properties and methods
  21236. 18:08:25that we want for each object that we
  21237. 18:08:28generate so here we have our properties
  21238. 18:08:30and methods we're just going to move
  21239. 18:08:32them into the
  21240. 18:08:34class so first let's select the card
  21241. 18:08:36items
  21242. 18:08:37property we're going to right click
  21243. 18:08:41cut and then move it into the class by
  21244. 18:08:44clicking in here and then right click
  21245. 18:08:47and
  21246. 18:08:49paste and classes use a slightly
  21247. 18:08:52different syntax than objects so to
  21248. 18:08:55create a property called cart items
  21249. 18:08:58we're going to type the property name
  21250. 18:09:00and instead of a colon we're just going
  21251. 18:09:02to make it equal to undefined and
  21252. 18:09:05instead of a comma at the end we're
  21253. 18:09:07going to put a
  21254. 18:09:09semicolon so that's how we add a
  21255. 18:09:11property to a class and now every object
  21256. 18:09:15that we generate will have this property
  21257. 18:09:17just like we did
  21258. 18:09:19before next let's move the methods into
  21259. 18:09:22the class so we'll delete these lines
  21260. 18:09:25for now and we'll move load from Storage
  21261. 18:09:29so we'll select this method and then
  21262. 18:09:33rightclick and
  21263. 18:09:35cut and inside the class we're going to
  21264. 18:09:38move it by right clicking and
  21265. 18:09:43paste and classes use a slightly
  21266. 18:09:45different syntax to create methods as
  21267. 18:09:48well so instead of having a comma at the
  21268. 18:09:50end we can just remove it and every
  21269. 18:09:54object that we generate will now have
  21270. 18:09:56this
  21271. 18:09:57method so now let's move the rest of the
  21272. 18:09:59methods into this class we're going to
  21273. 18:10:02scroll down and we'll move save to
  21274. 18:10:05storage so we'll select the method and
  21275. 18:10:08right click
  21276. 18:10:10cut and at the bottom of the class we're
  21277. 18:10:14going to right click and paste
  21278. 18:10:18and remove the comma at the
  21279. 18:10:19end same with add to cut we'll select
  21280. 18:10:23this
  21281. 18:10:25method and then right
  21282. 18:10:28click
  21283. 18:10:30cut and inside the bottom of the class
  21284. 18:10:34we're going to rightclick and
  21285. 18:10:37paste and remove the comma at the
  21286. 18:10:41end as well as remove from cart so we'll
  21287. 18:10:44select this
  21288. 18:10:45method right click
  21289. 18:10:48cut and at the bottom of the class right
  21290. 18:10:52click and
  21291. 18:10:54paste and remove the comma at the
  21292. 18:10:57end and finally we'll move the last
  21293. 18:11:00method update delivery
  21294. 18:11:03option and right click
  21295. 18:11:07cut inside this class we're going to
  21296. 18:11:10right click and
  21297. 18:11:13paste so this method did not have a
  21298. 18:11:16comma at the end so this looks
  21299. 18:11:19good so that's how we create a class we
  21300. 18:11:22basically give it some properties and
  21301. 18:11:24methods and now when we generate objects
  21302. 18:11:27using this class every object that we
  21303. 18:11:29generate will have these properties and
  21304. 18:11:34methods now one thing we need to fix
  21305. 18:11:37here is that local storage key doesn't
  21306. 18:11:40exist anymore to fix this we're going to
  21307. 18:11:43save this in a property instead so at
  21308. 18:11:46the top
  21309. 18:11:47we're going to give a new property to
  21310. 18:11:49every object that we generate called
  21311. 18:11:52local storage key and make it equal to
  21312. 18:11:58undefined to
  21313. 18:12:00start and then inside the methods we're
  21314. 18:12:03going to switch this to this. local
  21315. 18:12:07storage key so this is going to point to
  21316. 18:12:10the object that we generate just like
  21317. 18:12:12before and then local storage key will
  21318. 18:12:16access this property
  21319. 18:12:19and another place we need to fix this is
  21320. 18:12:21inside save to storage so instead of
  21321. 18:12:24using a variable local storage key we're
  21322. 18:12:27going to use the property this. local
  21323. 18:12:30storage
  21324. 18:12:33key and that's it so if we scroll
  21325. 18:12:37down we basically converted this whole
  21326. 18:12:40function into a class so now we can
  21327. 18:12:43remove it
  21328. 18:12:49and the final step here is now that we
  21329. 18:12:52have this class let's use it to generate
  21330. 18:12:55these two objects
  21331. 18:12:57again to do that instead of this code
  21332. 18:13:01we're going to type the word new and
  21333. 18:13:04then type the class name which is
  21334. 18:13:06capital c cart and
  21335. 18:13:10brackets so you see that a class uses a
  21336. 18:13:13similar syntax as a function except we
  21337. 18:13:16use the word new in front of
  21338. 18:13:19it so this generates a new object using
  21339. 18:13:23our
  21340. 18:13:25class and this new object will have all
  21341. 18:13:27the properties and methods that we added
  21342. 18:13:30above so let's do the same thing for the
  21343. 18:13:33business card we'll use the class to
  21344. 18:13:35generate it we'll remove this
  21345. 18:13:38code and then we'll type new and the
  21346. 18:13:41class name Capital C cart and brackets
  21347. 18:13:46to generate another object using this
  21348. 18:13:50class now there's one thing we need to
  21349. 18:13:52fix here each of these objects has a
  21350. 18:13:55property called local storage key and
  21351. 18:13:58they're currently undefined so we need
  21352. 18:14:00to set this
  21353. 18:14:02property so at the bottom here we're
  21354. 18:14:05going to type
  21355. 18:14:07cart. local storage key and we'll make
  21356. 18:14:12it
  21357. 18:14:14equal and we'll use the same string as
  21358. 18:14:17before which is C-
  21359. 18:14:21oop and we'll do the same thing for the
  21360. 18:14:23business cart we'll type the
  21361. 18:14:26business
  21362. 18:14:28cart. local storage
  21363. 18:14:32key and make it equal the same string
  21364. 18:14:35that we had before which is cart Das
  21365. 18:14:42business and then we'll load the cart
  21366. 18:14:44from Storage as well as the business C
  21367. 18:14:47from storage and we'll console.log
  21368. 18:14:50them so we have the same code as before
  21369. 18:14:53except we converted it from a function
  21370. 18:14:55to a
  21371. 18:14:57class so now let's
  21372. 18:14:59save and we're going to run this code So
  21373. 18:15:03currently if we go into our code and
  21374. 18:15:06open checkout.
  21375. 18:15:07JS we're still loading C-
  21376. 18:15:11oop so we're just going to update this
  21377. 18:15:13and load
  21378. 18:15:14C-Class instead and
  21379. 18:15:19save and we'll open cart Das class
  21380. 18:15:22again and now if we open the
  21381. 18:15:26console you can see that we created two
  21382. 18:15:28cart objects just like we did
  21383. 18:15:31before and the regular card has three
  21384. 18:15:34products inside and the business card
  21385. 18:15:37has two products
  21386. 18:15:39inside and if we click this Arrow beside
  21387. 18:15:42prototype we can see the methods that
  21388. 18:15:45each of these objects have
  21389. 18:15:48so we're basically creating the same
  21390. 18:15:49objects that we had before except this
  21391. 18:15:52time we used a
  21392. 18:15:54class by the way each object that we
  21393. 18:15:58generate from a class is called an
  21394. 18:16:00instance of the
  21395. 18:16:02class we can also check if an object is
  21396. 18:16:06an instance of a class using the code
  21397. 18:16:10business cart and then instance of and
  21398. 18:16:16then the class name name Capital C
  21399. 18:16:19cart so this will check if this object
  21400. 18:16:21was generated from this class so we can
  21401. 18:16:25console.log this result
  21402. 18:16:28console.log and close bracket and
  21403. 18:16:32save and then check the
  21404. 18:16:34console and it will tell us that this is
  21405. 18:16:37true so business card was generated from
  21406. 18:16:41this
  21407. 18:16:43class okay don't worry if class are a
  21408. 18:16:47bit confusing at first just remember
  21409. 18:16:50that objectoriented programming means
  21410. 18:16:52organizing our code into objects because
  21411. 18:16:56this tries to represent the real
  21412. 18:16:59world and then classes are a feature
  21413. 18:17:02that help us generate these objects so
  21414. 18:17:05they make it easier to create these
  21415. 18:17:08objects a class is basically an object
  21416. 18:17:12generator
  21417. 18:17:17now that we learned how classes work
  21418. 18:17:19let's talk about the benefits of
  21419. 18:17:21classes if we scroll
  21420. 18:17:24up when we generate an object it's going
  21421. 18:17:28to have these properties and these
  21422. 18:17:30methods and you'll notice that a class
  21423. 18:17:33actually looks like the object that we
  21424. 18:17:37generate and this is cleaner than using
  21425. 18:17:40a function like we had before
  21426. 18:17:46another benefit of classes is that they
  21427. 18:17:49have extra features for object-oriented
  21428. 18:17:54programming the first feature is called
  21429. 18:17:56a Constructor which lets us run some
  21430. 18:17:59setup code after creating an
  21431. 18:18:02object so if we scroll down in our
  21432. 18:18:06code you'll notice that after we create
  21433. 18:18:09these objects we run some setup code a
  21434. 18:18:13Constructor lets us put the setup code
  21435. 18:18:16inside the class and this makes our code
  21436. 18:18:19cleaner because we can group everything
  21437. 18:18:21together inside the
  21438. 18:18:24class to create a Constructor we're
  21439. 18:18:26going to scroll up in the
  21440. 18:18:30class and Below these properties we're
  21441. 18:18:33going to create a new method called
  21442. 18:18:37Constructor brackets and curly
  21443. 18:18:41brackets so the Constructor method works
  21444. 18:18:44like a normal method we can put code
  21445. 18:18:47inside and it will run the
  21446. 18:18:49code but the special thing about the
  21447. 18:18:52Constructor is that when we generate an
  21448. 18:18:54object it will run this Constructor
  21449. 18:18:57automatically so this is a great place
  21450. 18:18:59to put our setup
  21451. 18:19:01code so let's scroll down to the
  21452. 18:19:05bottom and here instead of running the
  21453. 18:19:08setup code outside the class we're just
  21454. 18:19:11going to move this into the class and
  21455. 18:19:13into the Constructor so we'll select
  21456. 18:19:16this code
  21457. 18:19:18and then right click
  21458. 18:19:21cut and scroll to the
  21459. 18:19:24top and inside the Constructor we're
  21460. 18:19:27going to rightclick and
  21461. 18:19:31paste so now after we create an object
  21462. 18:19:35it will run the code in the Constructor
  21463. 18:19:38and set up the
  21464. 18:19:41object so there's a few things that we
  21465. 18:19:43need to fix here the first thing is each
  21466. 18:19:46object object that we create is going to
  21467. 18:19:48run the Constructor so we only need one
  21468. 18:19:51of each of these lines so let's remove
  21469. 18:19:54the setup for the business
  21470. 18:20:02card so now the setup code has two steps
  21471. 18:20:06we set the local storage key and then we
  21472. 18:20:08load from Storage the next problem is
  21473. 18:20:12that the object that we create is not
  21474. 18:20:14going to be called cart every time
  21475. 18:20:18to solve this we can replace cart with
  21476. 18:20:21this and here as well this. load from
  21477. 18:20:25Storage so as a reminder this points to
  21478. 18:20:29the object that we generate so whatever
  21479. 18:20:32object that we generate we're going to
  21480. 18:20:34get its local storage key and set it and
  21481. 18:20:37we're going to get its load from Storage
  21482. 18:20:39method and run that
  21483. 18:20:42method now the second problem here is
  21484. 18:20:45that we don't want local storage key to
  21485. 18:20:47be card- oop every
  21486. 18:20:50time to fix this the Constructor can
  21487. 18:20:53also have parameters let's give the
  21488. 18:20:56Constructor a parameter and we'll name
  21489. 18:20:58it
  21490. 18:21:00local storage key and instead of the
  21491. 18:21:04same string every time we're going to
  21492. 18:21:07use local storage
  21493. 18:21:11key now the last thing is how do we save
  21494. 18:21:14a value into this parameter
  21495. 18:21:17so if we scroll down to the
  21496. 18:21:22bottom when we call the class between
  21497. 18:21:25these brackets we can put the parameters
  21498. 18:21:27for the Constructor so for the cart we
  21499. 18:21:31want the local storage key to be the
  21500. 18:21:33string C- oop and for the business cart
  21501. 18:21:38we want the local storage key to be the
  21502. 18:21:40string cart Das
  21503. 18:21:44business so the values inside here are
  21504. 18:21:47saved in the parameters in the
  21505. 18:21:49Constructor so C- oop will be saved
  21506. 18:21:53inside local storage key and then we use
  21507. 18:21:56it and then the same thing for cart D
  21508. 18:21:58business so now let's save this
  21509. 18:22:02code and then open the
  21510. 18:22:07console and you'll see that the cart
  21511. 18:22:09objects are correct so after we
  21512. 18:22:12generated each object we also ran this
  21513. 18:22:15setup code so that's how the Constructor
  21514. 18:22:18works every time we generate an object
  21515. 18:22:21it will run the code inside here so this
  21516. 18:22:24is a great place to put setup code and
  21517. 18:22:27make our code
  21518. 18:22:30cleaner so here are a few more details
  21519. 18:22:32about the
  21520. 18:22:33Constructor the method has to be named
  21521. 18:22:36Constructor we can't just use any name
  21522. 18:22:38that we want we also should not return
  21523. 18:22:41anything from a
  21524. 18:22:44Constructor so last ly a shortcut that
  21525. 18:22:47we can use in a class is when we have a
  21526. 18:22:50property and it's equal undefined we can
  21527. 18:22:53actually remove equal undefined and just
  21528. 18:22:56use a semicolon so this is the same
  21529. 18:22:59thing as the code before and same thing
  21530. 18:23:02here we can remove equal undefined and
  21531. 18:23:06just use a semicolon and
  21532. 18:23:11save so we finished converting our code
  21533. 18:23:13from using a function to using a class
  21534. 18:23:16and as you can see a class is just a
  21535. 18:23:19better way to generate objects in
  21536. 18:23:22objectoriented
  21537. 18:23:25programming so now let's save our code
  21538. 18:23:27into git we'll open the git section
  21539. 18:23:31inside cart Das class we created our
  21540. 18:23:34cart class and inside checkout. JS we're
  21541. 18:23:38basically loading the cart Das class
  21542. 18:23:41file so we'll type a message to describe
  21543. 18:23:43this change create
  21544. 18:23:47class
  21545. 18:23:48version of cart and
  21546. 18:23:59commit next we're going to learn an
  21547. 18:24:01important feature of classes called
  21548. 18:24:04private properties and
  21549. 18:24:07methods so let's open our code again and
  21550. 18:24:10open
  21551. 18:24:12C-Class and you'll see that this class
  21552. 18:24:15has a property called local storage
  21553. 18:24:17key so this property is useful inside
  21554. 18:24:20the class but it's not really meant to
  21555. 18:24:23be used outside the class for example at
  21556. 18:24:27the bottom
  21557. 18:24:28here we could take this
  21558. 18:24:31object cart and then change the local
  21559. 18:24:35storage key using dot local storage key
  21560. 18:24:41equals something
  21561. 18:24:43else now this is a problem because it
  21562. 18:24:46will mess up how this object works and
  21563. 18:24:49it will start saving to a different
  21564. 18:24:51local storage key so this is a common
  21565. 18:24:54problem in programming where if you're
  21566. 18:24:56working on a team somebody might
  21567. 18:24:59accidentally change properties that
  21568. 18:25:01they're not supposed to to prevent this
  21569. 18:25:04classes let us make a property or a
  21570. 18:25:06method private which means it can only
  21571. 18:25:09be accessed inside the
  21572. 18:25:14class so let's remove this line
  21573. 18:25:18and scroll
  21574. 18:25:20up and to make local storage key private
  21575. 18:25:24we're just going to add a number sign in
  21576. 18:25:27front or a
  21577. 18:25:28hash and also whenever we access this
  21578. 18:25:31property inside the class we're going to
  21579. 18:25:34add the hash in front so we'll type
  21580. 18:25:37this. has local storage key this.h local
  21581. 18:25:41storage key and inside save to storage
  21582. 18:25:46this.h local storage
  21583. 18:25:49key so this is called a private property
  21584. 18:25:53this means it can only be used inside
  21585. 18:25:56this class or inside these curly
  21586. 18:25:58brackets it cannot be used outside the
  21587. 18:26:02class a property without a hash in front
  21588. 18:26:05is called a public property meaning it
  21589. 18:26:08can be accessed
  21590. 18:26:11anywhere so now let's
  21591. 18:26:13save and open the console
  21592. 18:26:19and the code still
  21593. 18:26:22works so we made this property private
  21594. 18:26:25and we're able to access it inside the
  21595. 18:26:28class so now let's try accessing this
  21596. 18:26:31property outside the class to see what
  21597. 18:26:33happens we're going to scroll
  21598. 18:26:36down and we're going to try to access
  21599. 18:26:41cart. hash local storage key
  21600. 18:26:46and change it to something else like
  21601. 18:26:50test and
  21602. 18:26:52save and if we open our
  21603. 18:26:55console you'll see that when we try to
  21604. 18:26:58access a private property here it will
  21605. 18:27:01give us a syntax error saying this is a
  21606. 18:27:04private field and by the way a field is
  21607. 18:27:07the same thing as a
  21608. 18:27:10property so that's how a private
  21609. 18:27:13property works it prevents us from
  21610. 18:27:15accessing this property
  21611. 18:27:16outside the object and messing it
  21612. 18:27:19up we can also have private
  21613. 18:27:23methods so let's remove this
  21614. 18:27:26line and scroll
  21615. 18:27:31up and the method load from Storage
  21616. 18:27:35should also only be used inside this
  21617. 18:27:37class there's really no reason for code
  21618. 18:27:40outside to be calling load from Storage
  21619. 18:27:43so to make our code safer we're going to
  21620. 18:27:45make this a private
  21621. 18:27:47method to make a method private we're
  21622. 18:27:50going to do the same thing in front of
  21623. 18:27:52the method we're going to add a number
  21624. 18:27:54sign or a hash and we're going to update
  21625. 18:27:58the rest of our code so we're going to
  21626. 18:28:00do this dot hash load from
  21627. 18:28:04storage and now we can
  21628. 18:28:07save and check the console to make sure
  21629. 18:28:10that everything is still working so
  21630. 18:28:12everything still looks good
  21631. 18:28:16because we're only using this private
  21632. 18:28:18method inside this
  21633. 18:28:21class so a class Let's us create private
  21634. 18:28:25properties and methods and this is
  21635. 18:28:27useful on a bigger project where people
  21636. 18:28:30might not be sure which properties and
  21637. 18:28:32methods they're supposed to use outside
  21638. 18:28:34the class and which ones to
  21639. 18:28:37avoid so now let's save our changes into
  21640. 18:28:40git we'll go into the git section and
  21641. 18:28:44inside here we we made local storage key
  21642. 18:28:47and load from Storage
  21643. 18:28:50private we'll create a message to
  21644. 18:28:53describe these changes make local
  21645. 18:28:57storage key and load from Storage
  21646. 18:29:03private and
  21647. 18:29:12commit now that we learned
  21648. 18:29:14objectoriented programming and and
  21649. 18:29:16classes we're going to use it in our
  21650. 18:29:19project so one thing we could do is to
  21651. 18:29:22take this card class and replace
  21652. 18:29:25everywhere we use cart in our project
  21653. 18:29:28however we use cart in a lot of places
  21654. 18:29:30and this would take too much time so I'm
  21655. 18:29:32going to leave replacing the cart as a
  21656. 18:29:35challenge
  21657. 18:29:36exercise instead we're going to work on
  21658. 18:29:39the
  21659. 18:29:41homepage so first let's go back to our
  21660. 18:29:44code and we're going to open checkout.
  21661. 18:29:47JS and we're just going to comment out
  21662. 18:29:50this import because we're just using
  21663. 18:29:53this file for practice so let's save
  21664. 18:29:56this and then let's open the checkout
  21665. 18:29:59page so I already have it open if you
  21666. 18:30:01don't have it open you can rightclick
  21667. 18:30:04checkout. HTML and open with live
  21668. 18:30:11server on the checkout page we're going
  21669. 18:30:13to click this icon in the top left to go
  21670. 18:30:16to the
  21671. 18:30:19homepage on this page we have a list of
  21672. 18:30:22products
  21673. 18:30:24here and the code for these products is
  21674. 18:30:28if we go back to vs
  21675. 18:30:30code and on the left side we're going to
  21676. 18:30:33open
  21677. 18:30:34dataproducts
  21678. 18:30:38DJs so here is the code or the data for
  21679. 18:30:42all the products on the homepage and
  21680. 18:30:44you'll notice that each product is an
  21681. 18:30:47object we're going to learn a technique
  21682. 18:30:49called converting an object into a
  21683. 18:30:54class so what this means is instead of
  21684. 18:30:57using regular objects in this array
  21685. 18:30:59we're going to use a class to generate
  21686. 18:31:02these objects so Above This array let's
  21687. 18:31:06create a new class using the word class
  21688. 18:31:10and let's call it capital P product and
  21689. 18:31:14curly brackets
  21690. 18:31:17because this class is meant to generate
  21691. 18:31:20products so now each of our products is
  21692. 18:31:23going to have some properties like the
  21693. 18:31:25ID image and the name so let's add these
  21694. 18:31:28to the class We'll add the ID property
  21695. 18:31:31and a semicolon so that it starts off as
  21696. 18:31:34undefined and then an image property and
  21697. 18:31:38a name
  21698. 18:31:40property and each product will also have
  21699. 18:31:42a rating and the price sets rating and
  21700. 18:31:47price
  21701. 18:31:49set and you'll notice each object here
  21702. 18:31:52also has a property called keywords now
  21703. 18:31:55we're not currently using this in our
  21704. 18:31:57project so we're going to leave it out
  21705. 18:31:59of the class for now and if we need to
  21706. 18:32:01use it later we can always add it back
  21707. 18:32:04in so now we have all the properties
  21708. 18:32:07that we want for each product so now
  21709. 18:32:10let's test this by generating a product
  21710. 18:32:13using this class so that here
  21711. 18:32:16let's create a variable const product
  21712. 18:32:20one equals and to generate an object
  21713. 18:32:24we're going to use the word new and then
  21714. 18:32:26the class name capital P product and
  21715. 18:32:32brackets and let's console.log this to
  21716. 18:32:35see if it worked
  21717. 18:32:38console.log product one and
  21718. 18:32:43save so now we're going to open the web
  21719. 18:32:46page and then open the console so right
  21720. 18:32:50click and
  21721. 18:32:52inspect and open the
  21722. 18:32:54console and inside here you'll see that
  21723. 18:32:57we created a product object using the
  21724. 18:33:01class so right now all of these
  21725. 18:33:03properties are undefined so now let's
  21726. 18:33:06set the values of these properties we'll
  21727. 18:33:09go back to our
  21728. 18:33:11code don't follow along here but one way
  21729. 18:33:14to set set these prop properties is to
  21730. 18:33:16access them outside the
  21731. 18:33:19class for example product
  21732. 18:33:231. equals something and then do product
  21733. 18:33:281. image equals something and then
  21734. 18:33:32product one. name and so
  21735. 18:33:35on so this is not a good way to set
  21736. 18:33:38these properties because we have to do
  21737. 18:33:40this for each product that we create so
  21738. 18:33:42it makes our code repetitive and messy
  21739. 18:33:46instead notice that when we create a
  21740. 18:33:48product this is kind of like the setup
  21741. 18:33:51code we create a product and then set
  21742. 18:33:53all of its
  21743. 18:33:55properties and remember classes have a
  21744. 18:33:58specific feature for setup code called
  21745. 18:34:01The
  21746. 18:34:03Constructor and now you can follow
  21747. 18:34:05along so inside this class we're going
  21748. 18:34:08to create a
  21749. 18:34:12Constructor brackets and curly brackets
  21750. 18:34:16so whenever we generate an object it's
  21751. 18:34:19going to automatically run this
  21752. 18:34:21Constructor so this Constructor is where
  21753. 18:34:24we should put our setup
  21754. 18:34:26code so let's remove this
  21755. 18:34:30code and then do all of that inside the
  21756. 18:34:33Constructor so first we want to set the
  21757. 18:34:36ID property we're going to do this do ID
  21758. 18:34:41and make it equal and now what value do
  21759. 18:34:45we set the ID to so the value of the ID
  21760. 18:34:48is inside each of these
  21761. 18:34:51objects so when we create a product up
  21762. 18:34:54here we're going to put this object
  21763. 18:34:57inside these
  21764. 18:34:59brackets and then that's going to go
  21765. 18:35:01into the Constructor in here so in the
  21766. 18:35:05Constructor let's create a parameter to
  21767. 18:35:07save this data so we'll name it product
  21768. 18:35:13details and now to save something inside
  21769. 18:35:15ins this parameter we're going to put it
  21770. 18:35:17between these
  21771. 18:35:20brackets so let's actually give this
  21772. 18:35:22entire object to the
  21773. 18:35:26Constructor so we're going to right
  21774. 18:35:28click and
  21775. 18:35:32copy and then between these brackets
  21776. 18:35:35we're going to rightclick and
  21777. 18:35:39paste so whenever we create a new
  21778. 18:35:41product we're going to give it this
  21779. 18:35:44object with all the details
  21780. 18:35:46it's going to get saved in here and now
  21781. 18:35:48we can set the
  21782. 18:35:50properties so this. ID is going to be
  21783. 18:35:53equal to the product details. ID
  21784. 18:35:56property so product
  21785. 18:36:00details.
  21786. 18:36:02ID and for the image we're going to do
  21787. 18:36:06this. image equals the product details
  21788. 18:36:12which is this object that we passed in
  21789. 18:36:15dot the image
  21790. 18:36:18property same thing for the name so this
  21791. 18:36:21do name equals product
  21792. 18:36:26details. name which is this property
  21793. 18:36:29here same thing for the rating this.
  21794. 18:36:33rating equals product details.
  21795. 18:36:39rating and finally the price
  21796. 18:36:41sense this. price sense equals the
  21797. 18:36:47product
  21798. 18:36:49details. Price
  21799. 18:36:52sets so this is called converting an
  21800. 18:36:56object into a class what we're doing is
  21801. 18:37:00we're basically taking a regular object
  21802. 18:37:02like this we put it inside a class and
  21803. 18:37:07then we copy over the
  21804. 18:37:09properties you can also visualize it
  21805. 18:37:12like this we have a regular object and
  21806. 18:37:15we're basically wrapping it inside a
  21807. 18:37:17class and the reason we do this is that
  21808. 18:37:20classes have extra features we can use
  21809. 18:37:23like we can add methods and we can also
  21810. 18:37:26have private properties and private
  21811. 18:37:29methods so we basically have these same
  21812. 18:37:31object as before except we have an
  21813. 18:37:34enhanced version of that object it has
  21814. 18:37:38some extra features from
  21815. 18:37:41classes so for now let's save this code
  21816. 18:37:46and then down here we console.log
  21817. 18:37:49product one so let's take a look at it
  21818. 18:37:51in our console so we'll open the
  21819. 18:37:54console and now we can see that we
  21820. 18:37:56created a product object so it's the
  21821. 18:37:59same object as before except now it's
  21822. 18:38:02enhanced it has some extra features from
  21823. 18:38:08classes so next we're going to take this
  21824. 18:38:11technique and apply it to this entire
  21825. 18:38:14array so instead of having an array of
  21826. 18:38:17regular objects we're going to convert
  21827. 18:38:19each of these objects into the product
  21828. 18:38:23class now one way we could do this is
  21829. 18:38:26that in front of the object we can just
  21830. 18:38:28type new product Open Bracket and then
  21831. 18:38:33close bracket at the
  21832. 18:38:35bottom and this will essentially convert
  21833. 18:38:38this regular object into the product
  21834. 18:38:41class however we're going to have to
  21835. 18:38:43repeat this code for every object in the
  21836. 18:38:46array and it's going to be a lot of work
  21837. 18:38:48and
  21838. 18:38:50repetition so instead let's just Loop
  21839. 18:38:53through this array and then use code to
  21840. 18:38:56convert each of these objects so instead
  21841. 18:38:58of typing this out manually we're going
  21842. 18:39:00to remove this and remove the round
  21843. 18:39:03bracket at the end and then I'm going to
  21844. 18:39:06click this Arrow beside the array to
  21845. 18:39:09collapse
  21846. 18:39:10it and at the end of the array we're
  21847. 18:39:13going to use an array method called do
  21848. 18:39:17map and
  21849. 18:39:20brackets so map basically Loops through
  21850. 18:39:23an array and for each value it runs a
  21851. 18:39:26function so we're going to give this a
  21852. 18:39:28function that we want to run for each
  21853. 18:39:31value so arrow and curly
  21854. 18:39:34brackets and this inner function is
  21855. 18:39:36going to get a
  21856. 18:39:39parameter so this parameter is basically
  21857. 18:39:42each value in the
  21858. 18:39:43array so it's going to take the first
  21859. 18:39:46value and then save it inside this
  21860. 18:39:49parameter run the function and then take
  21861. 18:39:51the second value save it in here run the
  21862. 18:39:53function third value save it and run the
  21863. 18:39:57function so let's name this parameter
  21864. 18:40:00the product
  21865. 18:40:02details because each object in this
  21866. 18:40:05array contains product details so now
  21867. 18:40:08it's going to take each object save it
  21868. 18:40:11in the parameter and run the function
  21869. 18:40:15and here we're going to convert the
  21870. 18:40:17object into a class to do that we're
  21871. 18:40:20going to use the code new capital P
  21872. 18:40:24product
  21873. 18:40:26brackets and we're going to give it this
  21874. 18:40:29regular object which is the product
  21875. 18:40:35details and now there's a second part to
  21876. 18:40:38how Map works so map creates a new array
  21877. 18:40:43and whatever we return from this inner
  21878. 18:40:45function is going to go inside that new
  21879. 18:40:49array so here we're going to return this
  21880. 18:40:54class so this result is going to be put
  21881. 18:40:57inside a new array so you can think of
  21882. 18:41:00map as we take each value in an array we
  21883. 18:41:04run this function on it and we transform
  21884. 18:41:06it and then put it inside a new
  21885. 18:41:10array so here we're essentially
  21886. 18:41:13transforming each of these products or
  21887. 18:41:15these regular objects into a
  21888. 18:41:19class so at the bottom let's
  21889. 18:41:23console.log the products array to
  21890. 18:41:26confirm this so we'll
  21891. 18:41:29save and open the
  21892. 18:41:32console and now you can see we have an
  21893. 18:41:35array and each of the values is a
  21894. 18:41:38product class instead of just a regular
  21895. 18:41:40object
  21896. 18:41:45so we converted all of our products from
  21897. 18:41:47regular objects into this product class
  21898. 18:41:51and now let's open the web page to make
  21899. 18:41:54sure that everything still
  21900. 18:41:55works so if we open the
  21901. 18:41:58page you notice that everything is still
  21902. 18:42:00working
  21903. 18:42:01correctly and that's because we have the
  21904. 18:42:04exact same objects as before except
  21905. 18:42:07they're now just sort of enhanced
  21906. 18:42:11objects next let's use those extra
  21907. 18:42:14features of classes to simplify our code
  21908. 18:42:17for this page we'll go back to our
  21909. 18:42:21code and on the left side let's open
  21910. 18:42:25amazon.
  21911. 18:42:26JS so this is the code that creates the
  21912. 18:42:30homepage so in here we're going to
  21913. 18:42:33scroll down until we see the rating
  21914. 18:42:35section where we calculate the stars and
  21915. 18:42:38the rating
  21916. 18:42:40account so notice that inside our HTML
  21917. 18:42:44we're constru in this URL here for the
  21918. 18:42:47number of stars a product
  21919. 18:42:49has and we're also creating the price
  21920. 18:42:52that we want to display on the
  21921. 18:42:55page now instead of doing this all in
  21922. 18:42:57the HTML our code would be cleaner if we
  21923. 18:43:01moved this logic into our class because
  21924. 18:43:04we want to group this related code
  21925. 18:43:07together so let's use the features of
  21926. 18:43:09classes to make this code a little bit
  21927. 18:43:12cleaner we'll open products. Js
  21928. 18:43:17and we'll scroll up to the product class
  21929. 18:43:21and inside here let's add some methods
  21930. 18:43:24for calculating the stars and the
  21931. 18:43:27price so we're going to add a method and
  21932. 18:43:29call it get Stars URL brackets and curly
  21933. 18:43:37brackets and let's create another method
  21934. 18:43:40called get price brackets and curly
  21935. 18:43:44brackets
  21936. 18:43:47and now we're going to move the code
  21937. 18:43:48from our HTML into this class so we'll
  21938. 18:43:52open amazon.
  21939. 18:43:55JS and then here we're creating the URL
  21940. 18:43:58of the Stars so let's actually select
  21941. 18:44:01all this
  21942. 18:44:01code and
  21943. 18:44:04rightclick
  21944. 18:44:06copy and inside products.
  21945. 18:44:09JS inside get Stars URL we're going to
  21946. 18:44:12rightclick and paste
  21947. 18:44:17and this is going to be a string so at
  21948. 18:44:20the front let's add a back tick so we're
  21949. 18:44:23going to use a template string because
  21950. 18:44:25we're going to insert a value inside and
  21951. 18:44:28at the end we'll add another back
  21952. 18:44:30tick so now that we created the string
  21953. 18:44:33we're going to return it from this
  21954. 18:44:35method so
  21955. 18:44:37return and semicolon at the
  21956. 18:44:40end and one thing we need to fix is that
  21957. 18:44:43product is not defined anywhere so to
  21958. 18:44:47get the rating property of whatever
  21959. 18:44:50object this is we're going to use this.
  21960. 18:44:53ratings.
  21961. 18:44:57Stars so now each product is able to
  21962. 18:45:01construct its own Stars URL here so
  21963. 18:45:05let's save this
  21964. 18:45:07file and then inside amazon.
  21965. 18:45:10JS instead of doing this calculation
  21966. 18:45:13directly inside the HTML
  21967. 18:45:15we're going to delete it and we're going
  21968. 18:45:18to insert the
  21969. 18:45:21product. getet Stars URL method and
  21970. 18:45:27brackets so you'll notice that we have
  21971. 18:45:29the same product object as before but
  21972. 18:45:32now it's sort of an enhanced product we
  21973. 18:45:35can add methods to group related code
  21974. 18:45:38together and make our code
  21975. 18:45:42cleaner let's do the same thing with the
  21976. 18:45:44price down here we're going to calculate
  21977. 18:45:47this using the method instead of
  21978. 18:45:49directly in the
  21979. 18:45:50HTML so we're going to select this code
  21980. 18:45:53and then right click
  21981. 18:45:57copy inside products.
  21982. 18:45:59JS inside the get price method we're
  21983. 18:46:03going to
  21984. 18:46:05rightclick and
  21985. 18:46:08paste and again we want to return the
  21986. 18:46:11price as a string so at the front we'll
  21987. 18:46:14create a template string because we're
  21988. 18:46:16inserting a
  21989. 18:46:17value and at the back a template string
  21990. 18:46:20or a back tick and we're going to return
  21991. 18:46:23this from the
  21992. 18:46:26method and another thing we need to fix
  21993. 18:46:28is that product is undefined so to get
  21994. 18:46:31the price sets property we're going to
  21995. 18:46:33use this Price
  21996. 18:46:37sets now one extra thing we need to do
  21997. 18:46:40here is that we're using the format
  21998. 18:46:42currency function but we didn't import
  21999. 18:46:44it so we're going to scroll to the
  22000. 18:46:47top and import format
  22001. 18:46:50currency import curly brackets the
  22002. 18:46:54format currency function from a
  22003. 18:46:59string and here we're going to locate
  22004. 18:47:02this function so we're inside the data
  22005. 18:47:04folder we need to get out of the data
  22006. 18:47:06folder using do slash and then go into
  22007. 18:47:10the scripts folder into the UTS folder
  22008. 18:47:13and load money. JS so scripts SL
  22009. 18:47:17utils
  22010. 18:47:26money.jpg
  22011. 18:47:30and instead of calculating the price
  22012. 18:47:33inside the HTML we're going to remove
  22013. 18:47:36this and then insert the
  22014. 18:47:41product. getet price method and
  22015. 18:47:45brackets so now let's
  22016. 18:47:48save and let's open the homepage to make
  22017. 18:47:51sure that it's
  22018. 18:47:53working so now you see that everything
  22019. 18:47:55is still working including the stars and
  22020. 18:47:59the
  22021. 18:48:03prices so we'll go back to our
  22022. 18:48:06code and now that we have these methods
  22023. 18:48:08let's use them in our other page which
  22024. 18:48:12is the checkout page so we'll open
  22025. 18:48:14scripts
  22026. 18:48:16checkout.
  22027. 18:48:18JS and this basically displays the
  22028. 18:48:20products on the checkout
  22029. 18:48:24page if we scroll to the HTML and find
  22030. 18:48:28the delivery date the product image and
  22031. 18:48:31the product name we're going to see that
  22032. 18:48:33here we're also calculating the
  22033. 18:48:35product's price directly in the
  22034. 18:48:39HTML so instead of doing this let's use
  22035. 18:48:42the method we just created so select
  22036. 18:48:45this code and remove it and insert the
  22037. 18:48:50matching product this time so
  22038. 18:48:52matching
  22039. 18:48:54product and remember we converted all of
  22040. 18:48:57our products into classes so this is not
  22041. 18:49:00a regular object anymore it's more of an
  22042. 18:49:02enhanced object it's a product
  22043. 18:49:05class and that means this will have the
  22044. 18:49:09method. get price and brackets and Save
  22045. 18:49:16so now let's open the checkout page to
  22046. 18:49:18see if this works we'll open the web
  22047. 18:49:21page and then at the top right we're
  22048. 18:49:24going to click the
  22049. 18:49:25cart and now you can see that on the
  22050. 18:49:27checkout page the price is still
  22051. 18:49:30displayed
  22052. 18:49:33correctly and that's it if we go back to
  22053. 18:49:36our
  22054. 18:49:37code you'll see that by converting our
  22055. 18:49:39objects into classes we're able to use
  22056. 18:49:42extra features of classes like methods
  22057. 18:49:46to make our code
  22058. 18:49:48cleaner so now let's make sure that all
  22059. 18:49:50of our tests are passing we'll go to the
  22060. 18:49:53left and scroll down and we're going to
  22061. 18:49:56open test.html in the browser so if you
  22062. 18:49:59don't have it open you can right click
  22063. 18:50:02and open with live server but for me I
  22064. 18:50:05already have it open so I'm going to
  22065. 18:50:07open the test page and all of our tests
  22066. 18:50:10are still passing so our code is still
  22067. 18:50:13correct and now now let's save our
  22068. 18:50:15changes into git we'll open the git
  22069. 18:50:19section and in products. JS we created
  22070. 18:50:22the products class and at the bottom we
  22071. 18:50:26converted all of our objects into the
  22072. 18:50:28products
  22073. 18:50:30class so actually one thing I forgot to
  22074. 18:50:32do is to clean up these console. logs so
  22075. 18:50:36let's do that right now we're going to
  22076. 18:50:38open our code and go into products.
  22077. 18:50:42JS and we'll scroll down right after the
  22078. 18:50:45product class and let's remove this
  22079. 18:50:49product one because it was just an
  22080. 18:50:52example and we'll also remove the
  22081. 18:50:54console.log products at the
  22082. 18:50:57end and
  22083. 18:50:59save so now if we go back into git and
  22084. 18:51:04open products. JS we just have the code
  22085. 18:51:07where we convert our objects into a
  22086. 18:51:10class in amazon.
  22087. 18:51:12JS we use the feature of classes called
  22088. 18:51:15Methods to make our code cleaner in
  22089. 18:51:18checkout. JS we stop loading our cart
  22090. 18:51:22class practice file and in orders
  22091. 18:51:24summary. JS we also use a method to make
  22092. 18:51:27our code
  22093. 18:51:30cleaner so now let's create a message to
  22094. 18:51:32describe these
  22095. 18:51:34changes
  22096. 18:51:36convert products into classes and commit
  22097. 18:51:50next we're going to learn an important
  22098. 18:51:52feature of object-oriented programming
  22099. 18:51:55called
  22100. 18:51:56inheritance inheritance lets us reuse
  22101. 18:51:59code between
  22102. 18:52:01classes so first let's learn the concept
  22103. 18:52:04of
  22104. 18:52:06inheritance to do that we're going to
  22105. 18:52:08open our
  22106. 18:52:09project so we'll open this page here to
  22107. 18:52:13our checkout page
  22108. 18:52:15and we're going to go to the homepage by
  22109. 18:52:17clicking the top
  22110. 18:52:20left and on this page we have a bunch of
  22111. 18:52:23products but you also notice that we
  22112. 18:52:26have different types of products for
  22113. 18:52:28example we have clothing like this
  22114. 18:52:30t-shirt and we have appliances like this
  22115. 18:52:34toaster so clothing and appliances are
  22116. 18:52:38still products they have a name a price
  22117. 18:52:41and a rating but they are a more
  22118. 18:52:44specific type of product for example
  22119. 18:52:48clothing can have all the properties of
  22120. 18:52:50a regular product but it can also have
  22121. 18:52:53specific properties like a size
  22122. 18:52:56chart so how do we represent this kind
  22123. 18:52:59of relationship in our code we have a
  22124. 18:53:01product and then we have a more specific
  22125. 18:53:04type of
  22126. 18:53:05product one way to represent this is to
  22127. 18:53:08create a new class called clothing and
  22128. 18:53:11this class will have all the properties
  22129. 18:53:13of a regular product but also more
  22130. 18:53:16specific
  22131. 18:53:18properties however there's a problem
  22132. 18:53:20with this which is we're duplicating a
  22133. 18:53:22lot of
  22134. 18:53:24code instead objectoriented programming
  22135. 18:53:28has a feature called inheritance that
  22136. 18:53:30allows one class to get all the
  22137. 18:53:33properties and methods from another
  22138. 18:53:35class so we don't have to copy paste
  22139. 18:53:38this
  22140. 18:53:39code for example we can make a clothing
  22141. 18:53:42class and then use inheritance to get
  22142. 18:53:46all the properties and methods from the
  22143. 18:53:48product
  22144. 18:53:49class this is similar to how a child
  22145. 18:53:52inherits things from their parents like
  22146. 18:53:55their hair color or the shape of their
  22147. 18:53:57eyes in fact we call the product class
  22148. 18:54:01the parent class and we call the
  22149. 18:54:03clothing class the child
  22150. 18:54:08class so now let's learn how to use
  22151. 18:54:10inheritance in our code we're going to
  22152. 18:54:13open VSS
  22153. 18:54:16code and open our code on the left and
  22154. 18:54:19then open products.
  22155. 18:54:22JS and we're going to scroll to the
  22156. 18:54:26product class and then below this class
  22157. 18:54:29we're going to create a more specific
  22158. 18:54:31type of product called
  22159. 18:54:35class and capital c
  22160. 18:54:38clothing and curly
  22161. 18:54:41brackets so we said that clothing is is
  22162. 18:54:44just a specific type of product so that
  22163. 18:54:47means clothing should have all the
  22164. 18:54:50properties and methods of a
  22165. 18:54:52product so first let's inherit all these
  22166. 18:54:56properties and methods into the clothing
  22167. 18:54:59class to use inheritance after the class
  22168. 18:55:02name we're going to use the word
  22169. 18:55:06extends and then we're going to give it
  22170. 18:55:08the class that we want to inherit from
  22171. 18:55:12which is the product class
  22172. 18:55:15Above So this means that clothing will
  22173. 18:55:18get all the properties and methods of
  22174. 18:55:20this class or it will inherit all the
  22175. 18:55:24properties and
  22176. 18:55:26methods to demonstrate this let's leave
  22177. 18:55:29this empty and create a new clothing
  22178. 18:55:33object so down
  22179. 18:55:35here let's create a variable first using
  22180. 18:55:39const and we'll name it a
  22181. 18:55:42t-shirt make it equal
  22182. 18:55:45and we'll generate a new clothing object
  22183. 18:55:48to do that we'll use the word New
  22184. 18:55:52Capital C clothing and
  22185. 18:55:57brackets and next we're going to expand
  22186. 18:56:00the products array and we're going to
  22187. 18:56:02give the product details to this class
  22188. 18:56:06so we're going to scroll down to the
  22189. 18:56:07third product which is the
  22190. 18:56:11t-shirt and we're going to select this
  22191. 18:56:13object
  22192. 18:56:14and then right
  22193. 18:56:16click
  22194. 18:56:18copy and scroll
  22195. 18:56:20up and now between these brackets we're
  22196. 18:56:24going to right click and
  22197. 18:56:27paste so now we created a new closing
  22198. 18:56:30class we gave it all the data that it
  22199. 18:56:33needs and let's console.log it to see
  22200. 18:56:36what it looks like so down here we're
  22201. 18:56:38going to
  22202. 18:56:40console.log brackets and the variable
  22203. 18:56:43name which is t-shirt so here
  22204. 18:56:48t-shirt and
  22205. 18:56:51save and now let's open the console so
  22206. 18:56:54we'll open our web page and then on this
  22207. 18:56:57page we're going to right
  22208. 18:56:59click
  22209. 18:57:01inspect and open the
  22210. 18:57:04console and you'll see that we created a
  22211. 18:57:06new clothing
  22212. 18:57:07object and if we open it you'll see that
  22213. 18:57:11this has the same properties as the
  22214. 18:57:13product class
  22215. 18:57:14so this inherited all the properties
  22216. 18:57:17from
  22217. 18:57:18product so let's see if we inherited the
  22218. 18:57:21methods as well we'll go to our
  22219. 18:57:24code and let's try the method get price
  22220. 18:57:28so we'll
  22221. 18:57:30console.log
  22222. 18:57:31t-shirt. getet price and
  22223. 18:57:36brackets and
  22224. 18:57:38save and now if we open the console
  22225. 18:57:42again you'll see that get price also
  22226. 18:57:44works so clothing inherited the methods
  22227. 18:57:48from product as well so even though this
  22228. 18:57:51is empty this class gets or inherits all
  22229. 18:57:56the properties and methods of
  22230. 18:57:59product so we use inheritance when we
  22231. 18:58:02have one class which is a more specific
  22232. 18:58:05type of another class for example
  22233. 18:58:08clothing is a more specific type of
  22234. 18:58:11product we want all the properties and
  22235. 18:58:14methods of the product still but we just
  22236. 18:58:16don't want to copy paste all this code
  22237. 18:58:19down here so inheritance lets us reuse
  22238. 18:58:22code between
  22239. 18:58:29classes so now that we have this
  22240. 18:58:31clothing class we can add properties and
  22241. 18:58:33methods that are more specific to
  22242. 18:58:37clothing for example clothing can have a
  22243. 18:58:40size
  22244. 18:58:42chart so for con convenience inside this
  22245. 18:58:45t-shirt object I've actually added a
  22246. 18:58:48size chart link so let's add this to our
  22247. 18:58:52clothing class so up here we're going to
  22248. 18:58:55add a
  22249. 18:58:56property size chart
  22250. 18:59:00link and now to set this value we're
  22251. 18:59:03going to use the Constructor like we did
  22252. 18:59:06before so down here we're going to use a
  22253. 18:59:11Constructor brackets and curly brackets
  22254. 18:59:16and we're going to give this a parameter
  22255. 18:59:18called Product
  22256. 18:59:24details so when we create the clothing
  22257. 18:59:26we save this object into product details
  22258. 18:59:29and then we can use it to set the
  22259. 18:59:31properties up here so we're going to do
  22260. 18:59:35this do size chart link and make it
  22261. 18:59:40equal to the product details
  22262. 18:59:44which is this object and we're going to
  22263. 18:59:46get the size chart link
  22264. 18:59:49property dot size chart
  22265. 18:59:55link now one problem we have here is
  22266. 18:59:58that clothing inherits all the
  22267. 19:00:00properties from the product so we also
  22268. 19:00:04need to set the ID the image the name
  22269. 19:00:07and so on so if we scroll down we could
  22270. 19:00:11do something like this . ID equals
  22271. 19:00:16product details.
  22272. 19:00:18ID however instead of doing this one by
  22273. 19:00:22one all over again inheritance gives us
  22274. 19:00:25a shortcut we can actually just use the
  22275. 19:00:28Constructor in the parent class up
  22276. 19:00:33here so to call the parents Constructor
  22277. 19:00:36we're going to use a special feature of
  22278. 19:00:38classes called super and brackets
  22279. 19:00:44so super basically calls the Constructor
  22280. 19:00:47of the parent class which is the product
  22281. 19:00:50so this will call the Constructor up
  22282. 19:00:52here and we also need to give this the
  22283. 19:00:55product details so down here we're going
  22284. 19:00:58to call Super and give it the product
  22285. 19:01:05details so this will call the
  22286. 19:01:07Constructor of the parent class which is
  22287. 19:01:09product and that will set the ID the
  22288. 19:01:12name and the rating
  22289. 19:01:14so now let's save
  22290. 19:01:16this and check our console
  22291. 19:01:20again and open up the clothing object
  22292. 19:01:23and you'll see that now we have a size
  22293. 19:01:25chart link
  22294. 19:01:27property so that's how inheritance Works
  22295. 19:01:31a class can get or inherit all the
  22296. 19:01:34properties and methods of another class
  22297. 19:01:37and reuse that code and this class can
  22298. 19:01:40also have more specific properties and
  22299. 19:01:42methods like a size chart
  22300. 19:01:46link one last thing to know is that if
  22301. 19:01:49we don't create a Constructor by default
  22302. 19:01:52it will run the parents Constructor so
  22303. 19:01:55that's why when this class was empty the
  22304. 19:01:57code still
  22305. 19:02:02worked now that we have this clothing
  22306. 19:02:04class we're going to use it in our
  22307. 19:02:09project so right now if we scroll down
  22308. 19:02:11to our products array we can converted
  22309. 19:02:14all of these into a product
  22310. 19:02:16class however we're going to convert
  22311. 19:02:19some of them like the t-shirt into a
  22312. 19:02:22clothing class instead to make it more
  22313. 19:02:25specific so how do we know which objects
  22314. 19:02:27to convert into a product and which ones
  22315. 19:02:30to convert into clothing to help us do
  22316. 19:02:33that I included a property called type
  22317. 19:02:36so type tells us which class we should
  22318. 19:02:38use when we're converting this into a
  22319. 19:02:42class this is sometimes called a
  22320. 19:02:44discriminator property it tells us which
  22321. 19:02:47class we should convert this
  22322. 19:02:50to so we're going to go down to the
  22323. 19:02:52bottom of this code so I'm going to do a
  22324. 19:02:55trick here I'm going to scroll up to the
  22325. 19:02:57products array and click this Arrow to
  22326. 19:03:00collapse it and then at the bottom we're
  22327. 19:03:03going to use that type property to
  22328. 19:03:05decide which class to use so inside this
  22329. 19:03:09function we're going to use an if
  22330. 19:03:12statement bracket ET and curler
  22331. 19:03:15brackets and inside we're going to check
  22332. 19:03:17the type
  22333. 19:03:19property product
  22334. 19:03:22details.
  22335. 19:03:24type and we'll check if it is equal to
  22336. 19:03:28the string
  22337. 19:03:32clothing if it is equal to clothing
  22338. 19:03:35we're going to convert this object into
  22339. 19:03:37a clothing class instead so let's
  22340. 19:03:41return new Capital C clothing and
  22341. 19:03:47brackets and we're going to give the
  22342. 19:03:49Constructor product
  22343. 19:03:55details so now our array will contain a
  22344. 19:03:58mix of regular products and
  22345. 19:04:02clothing next for each of our clothing
  22346. 19:04:05we're going to display the size chart
  22347. 19:04:07link on the
  22348. 19:04:10page but first let's make sure that we
  22349. 19:04:13have this image here the closing size
  22350. 19:04:16chart so if we go into our images
  22351. 19:04:19folder you might see the image here
  22352. 19:04:22called clothing siiz
  22353. 19:04:24chart.png but currently I don't have
  22354. 19:04:26this so I need to download this first to
  22355. 19:04:29do that we're going to click this link
  22356. 19:04:31in the
  22357. 19:04:38description which will open the size
  22358. 19:04:40chart
  22359. 19:04:46and then on this image we're going to
  22360. 19:04:48right click save image
  22361. 19:04:51as and then find the folder that
  22362. 19:04:53contains all our code so for me it's on
  22363. 19:04:56the
  22364. 19:04:57desktop inside a JavaScript Amazon
  22365. 19:05:01Project and inside the images
  22366. 19:05:04folder and we'll save it
  22367. 19:05:10here and if we go back to our code we
  22368. 19:05:13should have the closing size chart image
  22369. 19:05:16inside the images
  22370. 19:05:19folder now let's display the size chart
  22371. 19:05:22on the
  22372. 19:05:24page so if I go to the
  22373. 19:05:26homepage whenever we have a clothing
  22374. 19:05:29product like this t-shirt we're going to
  22375. 19:05:31display a size chart
  22376. 19:05:35underneath let's go back to our
  22377. 19:05:38code and open amazon. JS which creates
  22378. 19:05:42the homepage
  22379. 19:05:43which is right
  22380. 19:05:45here and we're going to scroll down the
  22381. 19:05:47HTML below the product price and the
  22382. 19:05:51quantity selector here and underneath
  22383. 19:05:57this if we have a clothing product we're
  22384. 19:06:00going to display a size chart link under
  22385. 19:06:02the quantity selector like
  22386. 19:06:06this so one way we can do this is to use
  22387. 19:06:10something like an if statement or a
  22388. 19:06:12turnery like a this if it's a clothing
  22389. 19:06:16then display some HTML if it's not a
  22390. 19:06:18clothing then just display
  22391. 19:06:23nothing however we're going to learn a
  22392. 19:06:25different way to do this using
  22393. 19:06:28inheritance so first we're going to
  22394. 19:06:30create a new method on the clothing
  22395. 19:06:32class that's going to generate the HTML
  22396. 19:06:35that we need here so we're going to go
  22397. 19:06:38to our code and scroll up and open
  22398. 19:06:41products. Js
  22399. 19:06:44and we're going to scroll up to the
  22400. 19:06:46clothing
  22401. 19:06:47class and at the bottom we're going to
  22402. 19:06:49create a new method and let's call it
  22403. 19:06:53extra
  22404. 19:06:55info
  22405. 19:06:56HTML brackets and Cur
  22406. 19:07:02brackets so this method will generate
  22407. 19:07:04some HTML that contains some extra
  22408. 19:07:07information about this product like the
  22409. 19:07:09size
  22410. 19:07:11chart so here we're going to return some
  22411. 19:07:16HTML and we use a template string
  22412. 19:07:18because we're going to insert some
  22413. 19:07:20values inside and inside here let's
  22414. 19:07:24create some HTML for a
  22415. 19:07:27link to create a link we're going to use
  22416. 19:07:30the code less than a greater than less
  22417. 19:07:34than SL a greater than so this HTML
  22418. 19:07:38element creates a link when we click
  22419. 19:07:41this link it will open another page
  22420. 19:07:44page inside this link let's put the text
  22421. 19:07:48size chart so this is what's going to
  22422. 19:07:51show up on the
  22423. 19:07:52page and Link elements have two
  22424. 19:07:56attributes the first attribute is called
  22425. 19:07:59hre Equals double
  22426. 19:08:01quotes So this tells the computer which
  22427. 19:08:04page this should open when we click this
  22428. 19:08:08link inside here we're going to insert
  22429. 19:08:11the size chart link so we're going to
  22430. 19:08:14insert this. siiz chart
  22431. 19:08:20link so when we click this it will open
  22432. 19:08:23a page containing the size chart
  22433. 19:08:27image we'll scroll back
  22434. 19:08:30up and Link elements can have another
  22435. 19:08:33attribute called Target equals double
  22436. 19:08:37quotes and we're going to give it a
  22437. 19:08:39value of underscore blank
  22438. 19:08:43and I'll add some new lines here to make
  22439. 19:08:45it a little easier to
  22440. 19:08:48read so Target equals blank tells the
  22441. 19:08:51link to open in a new
  22442. 19:08:54tab so now let's save this
  22443. 19:08:57file and we'll go back to the homepage
  22444. 19:09:00so I'll scroll down here to amazon.
  22445. 19:09:04JS and below the quantity
  22446. 19:09:07selector let's
  22447. 19:09:09insert and we'll call the method that we
  22448. 19:09:12just created which is the
  22449. 19:09:15product. extra info
  22450. 19:09:19HTML and
  22451. 19:09:22brackets so this will ins some HTML
  22452. 19:09:26containing extra information about the
  22453. 19:09:28product like the size
  22454. 19:09:30chart now before we run this code
  22455. 19:09:33there's one thing that we need to fix
  22456. 19:09:35currently only the clothing class has
  22457. 19:09:38this
  22458. 19:09:39method if it's just a regular product
  22459. 19:09:42it's not going to have this method and
  22460. 19:09:44it's going to cause an
  22461. 19:09:47error to fix this we're also going to
  22462. 19:09:50create this method on the product class
  22463. 19:09:53so we'll go back to our
  22464. 19:09:55code and open products.
  22465. 19:09:58JS and we're going to scroll up to the
  22466. 19:10:01products class here and at the bottom
  22467. 19:10:05we're going to add the same
  22468. 19:10:10method extra info HTML brackets and
  22469. 19:10:15curly
  22470. 19:10:17brackets and for regular products we
  22471. 19:10:20don't have any extra info we want to
  22472. 19:10:22display so let's just return and empty
  22473. 19:10:27string and
  22474. 19:10:30save so now if we call extra info HTML
  22475. 19:10:34it doesn't matter if it's a product or a
  22476. 19:10:36clothing both of these will have this
  22477. 19:10:39method
  22478. 19:10:40defined so we'll go back to amazon.
  22479. 19:10:44JS and now this should always
  22480. 19:10:47work so let's click this file and
  22481. 19:10:51save and then open the web page to see
  22482. 19:10:54the
  22483. 19:10:55result so now you notice that products
  22484. 19:10:58that are clothing have a size chart
  22485. 19:11:01underneath and regular products don't
  22486. 19:11:04have any extra
  22487. 19:11:06information and if we click the size
  22488. 19:11:08chart link it will open a new tab to the
  22489. 19:11:12size chart image
  22490. 19:11:15so that's how we create this size chart
  22491. 19:11:17link for only the clothing
  22492. 19:11:24products let's go back to our code and
  22493. 19:11:26we'll learn more about this new
  22494. 19:11:28technique that we just
  22495. 19:11:31used so on the left we'll go back to
  22496. 19:11:34products. JS
  22497. 19:11:38first so notice that the product class
  22498. 19:11:41has a method called extra info HT ML and
  22499. 19:11:44then clothing extends this
  22500. 19:11:47class that means clothing will
  22501. 19:11:49automatically get the extra info HTML
  22502. 19:11:53method but then we Define this method
  22503. 19:11:56again inside
  22504. 19:11:58clothing so this will actually override
  22505. 19:12:01or replace the parents method and this
  22506. 19:12:04technique is called method
  22507. 19:12:09overwriting one more thing to know about
  22508. 19:12:11method overwriting is that if we really
  22509. 19:12:14need access to the parents method
  22510. 19:12:17there's a feature that we can use called
  22511. 19:12:20super so super gives us access to the
  22512. 19:12:24parent
  22513. 19:12:25class so if we want to call this method
  22514. 19:12:27up here we can do super and then extra
  22515. 19:12:32info
  22516. 19:12:36HTML so it's the same super as we used
  22517. 19:12:39up here except this calls the parents
  22518. 19:12:42Constructor and this calls one of the
  22519. 19:12:45parents
  22520. 19:12:47methods so let's comment this out since
  22521. 19:12:49it's just an
  22522. 19:12:52example and let's
  22523. 19:12:55save and lastly let's go back to amazon.
  22524. 19:13:00JS and we'll take a look at this code
  22525. 19:13:04and notice that when we call extra info
  22526. 19:13:07HTML we don't know what class this is
  22527. 19:13:10this might be a clothing class or a
  22528. 19:13:13might just be a product
  22529. 19:13:15class this technique is called
  22530. 19:13:18polymorphism which means we can use a
  22531. 19:13:20method without needing to know exactly
  22532. 19:13:23what class this
  22533. 19:13:25is polymorphism is sort of like an
  22534. 19:13:28alternative to doing if statements here
  22535. 19:13:31instead of doing if it's clothing do
  22536. 19:13:33this if it's a regular product do
  22537. 19:13:36something else we can just have both of
  22538. 19:13:38the classes use the same method and then
  22539. 19:13:41the class itself will Det determine what
  22540. 19:13:43this method
  22541. 19:13:45does if we add a new class like an
  22542. 19:13:48appliance class we don't need to change
  22543. 19:13:51any of this code the appliance class
  22544. 19:13:53will determine what extra info HTML
  22545. 19:13:59does and that's it we learned about
  22546. 19:14:02inheritance which lets us reuse code
  22547. 19:14:05between classes and we learn some of the
  22548. 19:14:07techniques we can do with inheritance
  22549. 19:14:10like method overwriting and polymorph
  22550. 19:14:13PRM which can make our code a little bit
  22551. 19:14:16cleaner now let's check our test to make
  22552. 19:14:19sure that everything still works so
  22553. 19:14:21we'll open the test page and make sure
  22554. 19:14:24that everything is still passing and
  22555. 19:14:26then we'll go back to our code and we'll
  22556. 19:14:29save this into git open the git
  22557. 19:14:32section and in products.
  22558. 19:14:37JS we created the clothing class which
  22559. 19:14:41inherits from product
  22560. 19:14:43and one thing I forgot to do is to
  22561. 19:14:45remove this practice
  22562. 19:14:47code so let's actually go back to our
  22563. 19:14:50code in products.
  22564. 19:14:53JS and below the clothing let's remove
  22565. 19:14:57this practice
  22566. 19:14:59code and
  22567. 19:15:02save we'll go back to
  22568. 19:15:04git and just make sure that it's been
  22569. 19:15:08removed and then we added the size chart
  22570. 19:15:11image
  22571. 19:15:13and in amazon. JS we used polymorphism
  22572. 19:15:16to add some extra info below each
  22573. 19:15:19product so let's create a message
  22574. 19:15:21describing our
  22575. 19:15:23changes use
  22576. 19:15:27inheritance and
  22577. 19:15:30polymorphism to add size chart and
  22578. 19:15:35commit
  22579. 19:15:43in the rest of this lesson we're going
  22580. 19:15:44to learn more details about classes the
  22581. 19:15:48first detail is how to test classes so
  22582. 19:15:51testing classes is the same as writing
  22583. 19:15:54normal
  22584. 19:15:55tests so we generate an object using a
  22585. 19:15:58class and then we test if its properties
  22586. 19:16:01and methods are
  22587. 19:16:03correct I'll leave writing tests as an
  22588. 19:16:06exercise at the end of this
  22589. 19:16:10lesson the second detail we're going to
  22590. 19:16:12learn is built in
  22591. 19:16:14classes these are classes that are
  22592. 19:16:16provided by the
  22593. 19:16:20language so let's go back to our
  22594. 19:16:23code and open products. JS
  22595. 19:16:26again and we're going to practice using
  22596. 19:16:29a buil-in
  22597. 19:16:30class an example of a built-in class is
  22598. 19:16:35date this class generates an object that
  22599. 19:16:38represents the current date
  22600. 19:16:44below the classes that we created we're
  22601. 19:16:46going to create some new lines and we're
  22602. 19:16:48going to practice using the built-in
  22603. 19:16:50date
  22604. 19:16:51class first we'll generate a new date
  22605. 19:16:55object using the code new and then the
  22606. 19:16:59class capital D date and
  22607. 19:17:04brackets and now let's save this object
  22608. 19:17:06in a variable so at the front we'll use
  22609. 19:17:10const and we'll call it date and make it
  22610. 19:17:13equal to this object that we just
  22611. 19:17:16generated and below this let's
  22612. 19:17:19console.log the date to see what it
  22613. 19:17:22looks like so we'll
  22614. 19:17:25save and then open the
  22615. 19:17:27page and we'll go back to our project
  22616. 19:17:30here and we're going to check the
  22617. 19:17:32console so right click
  22618. 19:17:35inspect and the
  22619. 19:17:38console and we can see that the date
  22620. 19:17:40object represents the current date and
  22621. 19:17:43time so this is the current date and
  22622. 19:17:45time that I'm recording this
  22623. 19:17:49video so this date object has properties
  22624. 19:17:52and methods that we can use that are
  22625. 19:17:54related to the
  22626. 19:17:56date for example we'll go back to our
  22627. 19:18:00code and every date object has a method
  22628. 19:18:03called two local time string this
  22629. 19:18:06basically gives us the current
  22630. 19:18:09time so let's try it out we'll create
  22631. 19:18:12create a new line and type the code date
  22632. 19:18:16dot to local time string and brackets so
  22633. 19:18:24make sure that this is in cam case two
  22634. 19:18:26is lowercase and every other word starts
  22635. 19:18:29with an
  22636. 19:18:30uppercase and we'll console.log this to
  22637. 19:18:33see what it looks like
  22638. 19:18:35console. log and
  22639. 19:18:40brackets so let's save this file
  22640. 19:18:44and open the
  22641. 19:18:46console and that method gave us the time
  22642. 19:18:50so for you you should see your current
  22643. 19:18:52time
  22644. 19:18:55here so that's the built-in date class
  22645. 19:18:59it lets us create objects that represent
  22646. 19:19:01the current date and
  22647. 19:19:04time an interesting thing to know is
  22648. 19:19:06that we used the external Library DJs
  22649. 19:19:10earlier in the project DJs uses this
  22650. 19:19:13date class behind the
  22651. 19:19:15scenes however DJs gives us a lot of
  22652. 19:19:18extra features so we usually use DJs
  22653. 19:19:22instead of using the date class
  22654. 19:19:26directly so now let's comment out this
  22655. 19:19:28code because it was just an example so
  22656. 19:19:31at the top we'll type
  22657. 19:19:33slashstar and below it star
  22658. 19:19:39slash the last thing we're going to
  22659. 19:19:41learn is more detail dets about
  22660. 19:19:44this as a
  22661. 19:19:46reminder inside an object we can use the
  22662. 19:19:49value this and this will point to the
  22663. 19:19:53outer
  22664. 19:19:54object so this feature lets an object
  22665. 19:19:57access its own
  22666. 19:20:00properties now in JavaScript this can
  22667. 19:20:04actually be used anywhere in our
  22668. 19:20:07code for example we're going to create
  22669. 19:20:10some new lines
  22670. 19:20:13and here we can just directly
  22671. 19:20:17console.log
  22672. 19:20:20this so here we're using this but we're
  22673. 19:20:23using it outside of any object in this
  22674. 19:20:27situation there's no object for this to
  22675. 19:20:30point to so this will be undefined which
  22676. 19:20:34actually makes
  22677. 19:20:36sense if we
  22678. 19:20:38save and we check our console
  22679. 19:20:43this will equal
  22680. 19:20:45undefined now originally this actually
  22681. 19:20:49equaled the window
  22682. 19:20:51object however this Behavior caused
  22683. 19:20:54confusion so when they release
  22684. 19:20:56JavaScript modules inside a module this
  22685. 19:21:00is now undefined which makes more
  22686. 19:21:06sense let's go back to our code and
  22687. 19:21:09another place we can use this is when
  22688. 19:21:11we're creating an object so let's go
  22689. 19:21:14down here and we're going to create a
  22690. 19:21:17new object using const we'll call it
  22691. 19:21:20object two and make it equal to curly
  22692. 19:21:24brackets and we're going to give this
  22693. 19:21:26object a property let's just call it a
  22694. 19:21:29colon give it a value of two comma and
  22695. 19:21:33add another property B
  22696. 19:21:36colon and we'll set the value of this
  22697. 19:21:39property to this Dot
  22698. 19:21:43a now you might think that because we're
  22699. 19:21:46inside this object this will point to
  22700. 19:21:50object 2 however at this point in the
  22701. 19:21:54code object 2 has not been created yet
  22702. 19:21:57so this doesn't have anything to point
  22703. 19:22:00to and this will be
  22704. 19:22:05undefined if we
  22705. 19:22:07save and then open the
  22706. 19:22:10console it will give us an error because
  22707. 19:22:14this is
  22708. 19:22:17undefined let's go back to our
  22709. 19:22:19code so so far if we're inside a method
  22710. 19:22:23we can use this and it will point to the
  22711. 19:22:26object if we're outside of a method
  22712. 19:22:29there's no object to point to and this
  22713. 19:22:31will be
  22714. 19:22:32undefined and everything makes sense so
  22715. 19:22:37far so let's comment this out using SL
  22716. 19:22:41star and and at the bottom start
  22717. 19:22:46slash now the last important place we
  22718. 19:22:49can use this is inside a
  22719. 19:22:54function so at the bottom here we're
  22720. 19:22:57going to create a
  22721. 19:23:00function and let's call it log this
  22722. 19:23:04brackets and curly
  22723. 19:23:06brackets and inside we're just going to
  22724. 19:23:09console. log this
  22725. 19:23:14so again we're not inside of any object
  22726. 19:23:18so there's nothing for this to point2
  22727. 19:23:21and in this situation this will be
  22728. 19:23:24undefined as
  22729. 19:23:28well below this if we run the function
  22730. 19:23:31log this and brackets and
  22731. 19:23:36save and check the
  22732. 19:23:38console it will tell us that this is
  22733. 19:23:41undefined when it's just inside a
  22734. 19:23:43regular
  22735. 19:23:45function next this actually has a
  22736. 19:23:48special
  22737. 19:23:49feature so inside a function we can
  22738. 19:23:52change this to whatever we want to do
  22739. 19:23:56that functions have a method called
  22740. 19:24:01call So Below this line let's type the
  22741. 19:24:04function name log this and use a method
  22742. 19:24:09do call and brackets
  22743. 19:24:13so call runs this function so it does
  22744. 19:24:17the same thing as the line above except
  22745. 19:24:20we can set the value of this to be
  22746. 19:24:22whatever we want so the first value that
  22747. 19:24:25we give to do call is going to be the
  22748. 19:24:28value of this for example we can give it
  22749. 19:24:31the string
  22750. 19:24:34hello and now this will run the function
  22751. 19:24:37and this will be equal to hello if we
  22752. 19:24:40save
  22753. 19:24:42and open the
  22754. 19:24:44console you can see that we set this to
  22755. 19:24:47be equal to
  22756. 19:24:50hello so that's a special feature of
  22757. 19:24:53this inside just a function we can
  22758. 19:24:57change this to whatever we want using a
  22759. 19:25:00method like
  22760. 19:25:03call so call is the same as calling or
  22761. 19:25:07running the function except we add an
  22762. 19:25:10extra parameter in front this first
  22763. 19:25:13parameter is the value of
  22764. 19:25:18this the last important thing to know
  22765. 19:25:21about this is that Arrow functions do
  22766. 19:25:23not change the value of
  22767. 19:25:26this for example below this let's create
  22768. 19:25:30a method by using an arrow function this
  22769. 19:25:32time so we'll create some new lines and
  22770. 19:25:36first we're going to create an object
  22771. 19:25:38using
  22772. 19:25:39const we'll call it object three equals
  22773. 19:25:44curly
  22774. 19:25:45brackets and inside will give it a
  22775. 19:25:49method and colon and we're going to
  22776. 19:25:52create this method using an arrow
  22777. 19:25:54function this time brackets arrow and
  22778. 19:25:57curly brackets and inside we'll just
  22779. 19:26:01console. log
  22780. 19:26:06this so usually this will point to the
  22781. 19:26:10object itself
  22782. 19:26:12however Arrow functions do not change
  22783. 19:26:15the value of
  22784. 19:26:17this here this will have the same value
  22785. 19:26:22as outside the arrow
  22786. 19:26:24function outside the arrow function
  22787. 19:26:27let's type
  22788. 19:26:28this here this has no object to point to
  22789. 19:26:33so this will be
  22790. 19:26:35undefined and then inside the arrow
  22791. 19:26:38function it will also be undefined
  22792. 19:26:42basically this keeps the value that it
  22793. 19:26:45had outside the arrow
  22794. 19:26:50function as a test below this we'll run
  22795. 19:26:54object 3. method and
  22796. 19:26:58brackets if we
  22797. 19:27:00save and check the
  22798. 19:27:06console it will show us that inside here
  22799. 19:27:09this is also undefined
  22800. 19:27:13so in an arrow function this will be
  22801. 19:27:15whatever the value is outside the arrow
  22802. 19:27:20function now you might be wondering why
  22803. 19:27:23are Arrow functions designed this
  22804. 19:27:27way so JavaScript had a common problem
  22805. 19:27:29in the past when we are inside a method
  22806. 19:27:33this points to the object but if we try
  22807. 19:27:36to do a for each Loop inside the for
  22808. 19:27:40each Loop this does not point to the
  22809. 19:27:42object anymore it points to undefined
  22810. 19:27:46and that's because we created a whole
  22811. 19:27:48new function here and remember inside a
  22812. 19:27:51regular function this becomes
  22813. 19:27:55undefined so this caused problems
  22814. 19:27:58because we no longer had access to the
  22815. 19:28:00outer object up
  22816. 19:28:02here so that's why we created Arrow
  22817. 19:28:05functions if we replace this with an
  22818. 19:28:07arrow function this would be equal to
  22819. 19:28:10whatever it is outside the Arrow
  22820. 19:28:12function Arrow functions do not change
  22821. 19:28:15the value of
  22822. 19:28:17this so outside for each this points to
  22823. 19:28:20the object and inside for each this also
  22824. 19:28:24points to the
  22825. 19:28:26object so that's why Arrow functions are
  22826. 19:28:29designed this way to avoid accidentally
  22827. 19:28:32overwriting
  22828. 19:28:34this so let's summarize what we learned
  22829. 19:28:37about this inside a method this points
  22830. 19:28:41to to the outer object inside a function
  22831. 19:28:45this is undefined but we can change it
  22832. 19:28:48to whatever we want using something like
  22833. 19:28:51call and arrow functions do not change
  22834. 19:28:54the value of
  22835. 19:28:57this don't worry if these rules are a
  22836. 19:28:59bit confusing at first they are actually
  22837. 19:29:02sort of
  22838. 19:29:03reasonable if there's an object to point
  22839. 19:29:05to this will point to that object if
  22840. 19:29:08there's no object to point to this will
  22841. 19:29:10be undefined and we have tools like call
  22842. 19:29:14and arrow functions to kind of
  22843. 19:29:16manipulate the value of this to be what
  22844. 19:29:19we want the more that you work with it
  22845. 19:29:21the more you just get used to
  22846. 19:29:27it so now let's comment out this
  22847. 19:29:29practice code at the top we'll type
  22848. 19:29:33slashstar and at the bottom star slash
  22849. 19:29:37and
  22850. 19:29:39save and we'll save to get
  22851. 19:29:42we'll open the git
  22852. 19:29:44section inside products. JS we added
  22853. 19:29:48some practice code to learn more details
  22854. 19:29:50about classes and
  22855. 19:29:53this let's type a message describing our
  22856. 19:29:57changes learn more details about classes
  22857. 19:30:03and this and commit
  22858. 19:30:13now that we learned object-oriented
  22859. 19:30:15programming you might notice that it's a
  22860. 19:30:18very different style of programming than
  22861. 19:30:21just writing out a bunch of functions
  22862. 19:30:23directly so you might be wondering which
  22863. 19:30:26style of programming should we use
  22864. 19:30:29objectoriented programming or procedural
  22865. 19:30:31programming which is the step-by-step
  22866. 19:30:34style that we used in the earlier
  22867. 19:30:37lessons the answer is it's up to you I
  22868. 19:30:41personally prefer procedural programming
  22869. 19:30:43in JavaScript because I think it's
  22870. 19:30:45simpler I only use objectoriented
  22871. 19:30:48programming if I'm working on a team
  22872. 19:30:50that also uses it also object-oriented
  22873. 19:30:54programming is much more popular in
  22874. 19:30:56other languages so it's still useful to
  22875. 19:30:59know object-oriented programming in case
  22876. 19:31:02you work with other programming
  22877. 19:31:03languages in the
  22878. 19:31:06future and that's the end of this lesson
  22879. 19:31:10in this lesson we learned about
  22880. 19:31:12objectoriented programming which means
  22881. 19:31:14we organize our code into objects we
  22882. 19:31:18learned how to use a function to
  22883. 19:31:20generate objects and we improve that by
  22884. 19:31:22using a class to generate objects we
  22885. 19:31:26learned that classes can have private
  22886. 19:31:28properties and methods we learned about
  22887. 19:31:31inheritance which lets us reuse code
  22888. 19:31:34between classes and we learned about
  22889. 19:31:37method overwriting and polymorphism
  22890. 19:31:40which lets us call a method without
  22891. 19:31:42needing to know what the class is and
  22892. 19:31:45lastly we learned more details about
  22893. 19:31:47this and how to control the value of
  22894. 19:31:54this thanks for watching this course so
  22895. 19:31:56far if you want to support this channel
  22896. 19:31:59you can check out the premium version of
  22897. 19:32:01my courses just like this one where you
  22898. 19:32:03can get a certificate of completion and
  22899. 19:32:06a Better Learning platform if you're
  22900. 19:32:08interested in extra videos from me more
  22901. 19:32:11in-depth videos and behind the scenes
  22902. 19:32:13content you can check out these links
  22903. 19:32:16here you can also support the channel by
  22904. 19:32:19liking the video subscribing and sharing
  22905. 19:32:22this video with others thanks again and
  22906. 19:32:24let's continue with the
  22907. 19:32:27course here are some exercises to help
  22908. 19:32:30you practice what we learned in this
  22909. 19:32:32lesson
  22910. 19:33:03in this lesson we're going to learn
  22911. 19:33:04about backend and this will prepare you
  22912. 19:33:07for the next step after finishing this
  22913. 19:33:10course first of all
  22914. 19:33:12what is backend a backend is just
  22915. 19:33:15another computer that manages the data
  22916. 19:33:18of a
  22917. 19:33:19website for example in our Amazon
  22918. 19:33:22Project we created this website where we
  22919. 19:33:25can pick which products we want but how
  22920. 19:33:28does Amazon the company know which
  22921. 19:33:31products that we
  22922. 19:33:32ordered in this situation there has to
  22923. 19:33:35be a second computer somewhere that's
  22924. 19:33:37owned by
  22925. 19:33:39Amazon and then our computer sends the
  22926. 19:33:42order information to this second
  22927. 19:33:44computer and now Amazon will know what
  22928. 19:33:47we
  22929. 19:33:48ordered so this second computer is
  22930. 19:33:52called the backend computer or just the
  22931. 19:33:55backend it manages the data of a
  22932. 19:33:59website on the other hand our computer
  22933. 19:34:02or the web page where we can see and
  22934. 19:34:04interact with the products this is
  22935. 19:34:07called the front end
  22936. 19:34:12now that we know what a backend is how
  22937. 19:34:14does our computer or the front end send
  22938. 19:34:18information to the back end when two
  22939. 19:34:22computers are connected to the internet
  22940. 19:34:24they can actually send messages to each
  22941. 19:34:27other using a feature called
  22942. 19:34:31HTTP or hypertext transfer
  22943. 19:34:34protocol in an HTTP message we can
  22944. 19:34:38actually attach information like our
  22945. 19:34:40Amazon order
  22946. 19:34:42when Amazon's backend receives this HTTP
  22947. 19:34:45message now Amazon will know what we
  22948. 19:34:50ordered now that we know what HTTP is
  22949. 19:34:54let's send our first HTTP message using
  22950. 19:34:58JavaScript so we'll go to our code and
  22951. 19:35:02on the left we're going to create a new
  22952. 19:35:03file just for practicing backend so
  22953. 19:35:06we'll scroll up to the data folder and
  22954. 19:35:09we'll click inside this folder and then
  22955. 19:35:12create a new
  22956. 19:35:13file and let's name this file backend
  22957. 19:35:17Das
  22958. 19:35:19practice. JS and press
  22959. 19:35:24enter and now to send an HTTP message
  22960. 19:35:28we're going to use a class called XML
  22961. 19:35:32HTTP request so this is a built in class
  22962. 19:35:36meaning it's provided by
  22963. 19:35:39JavaScript first we gener at an object
  22964. 19:35:42using this class using the code
  22965. 19:35:46new and the class name
  22966. 19:35:49XML all capital capital
  22967. 19:35:53HTTP capital r request and
  22968. 19:36:00brackets so this creates a new HTTP
  22969. 19:36:03message that we want to send to the
  22970. 19:36:05backend a message is also known as a
  22971. 19:36:10request so now let's save this request
  22972. 19:36:13in a variable at the front we'll create
  22973. 19:36:16a variable with const and name it
  22974. 19:36:20xhr equals this
  22975. 19:36:24request next we need to set up this
  22976. 19:36:27request so we'll create a new line and
  22977. 19:36:31to set it up we'll do xhr
  22978. 19:36:35doop and
  22979. 19:36:38brackets and to set up this request
  22980. 19:36:42we're going to give open two
  22981. 19:36:45parameters the first parameter is what
  22982. 19:36:48type of HTTP message to
  22983. 19:36:50send so one type of message we can send
  22984. 19:36:54is the string
  22985. 19:36:56get get means we want to get some
  22986. 19:36:59information from the back
  22987. 19:37:01end there are several common types of
  22988. 19:37:04requests that we can give to the backend
  22989. 19:37:07but we're going to start by learning get
  22990. 19:37:12next we're going to give open a second
  22991. 19:37:16parameter and this parameter tells a
  22992. 19:37:18computer where to send this HTTP message
  22993. 19:37:23so using HTTP we can send a message to
  22994. 19:37:27any computer that's connected to the
  22995. 19:37:29internet now to locate another computer
  22996. 19:37:32on the internet we need to use a
  22997. 19:37:35url url stands for uniform resource
  22998. 19:37:39locator and it's kind of like an address
  22999. 19:37:42but for the internet a URL helps us
  23000. 19:37:46locate another computer on the
  23001. 19:37:49internet so URLs look like this this is
  23002. 19:37:54the URL for Amazon YouTube and my
  23003. 19:37:57website the https in front means we're
  23004. 19:38:01using HTTP to communicate with this
  23005. 19:38:04computer the S after the http means
  23006. 19:38:08we're using a secure version of http
  23007. 19:38:13the second part of a URL is called a
  23008. 19:38:15domain name this is like an address it
  23009. 19:38:18points to another computer on the
  23010. 19:38:21internet so amazon.com points to one of
  23011. 19:38:25Amazon's backend computers youtube.com
  23012. 19:38:28points to one of YouTube's backend
  23013. 19:38:31computers and superimple dodev points to
  23014. 19:38:34one of my backend
  23015. 19:38:37computers if you want to learn more
  23016. 19:38:39about domain names and how the internet
  23017. 19:38:41works you can check out my video on how
  23018. 19:38:43to put a website on the
  23019. 19:38:47internet now for this lesson I actually
  23020. 19:38:50set up a backend computer that we can
  23021. 19:38:52practice with so my backend is located
  23022. 19:38:56at the URL
  23023. 19:38:58https super simple backend
  23024. 19:39:02dodev so in the second parameter of open
  23025. 19:39:06we're going to give it a
  23026. 19:39:07string and we're going to give it the
  23027. 19:39:09URL of the back end end that we want to
  23028. 19:39:12send the message
  23029. 19:39:13to so the URL for my backend is
  23030. 19:39:18https colon SL SL super simple
  23031. 19:39:25backend.
  23032. 19:39:28death so that's how we set up an HTTP
  23033. 19:39:31message we give it the type of message
  23034. 19:39:34we want to send and also the URL which
  23035. 19:39:37is where to send this message to
  23036. 19:39:42the last step is to just send this
  23037. 19:39:44message so we'll create a new line and
  23038. 19:39:48we'll type xhr
  23039. 19:39:51doent brackets and
  23040. 19:39:56save so this creates a new HTTP message
  23041. 19:40:00sets it up and then sends this message
  23042. 19:40:03across the internet to my backend
  23043. 19:40:06computer located at superimple backend.
  23044. 19:40:09deev so now let's actually run this code
  23045. 19:40:13and see the results we're going to go to
  23046. 19:40:15the left and we're going to open
  23047. 19:40:18checkout.
  23048. 19:40:20JS and in here just like we did before
  23049. 19:40:23we're going to load that practice file
  23050. 19:40:25that we just created so we'll create a
  23051. 19:40:28new line and we're going to import a
  23052. 19:40:32file or a string and we'll locate that
  23053. 19:40:35practice
  23054. 19:40:37file so first we need to go to the data
  23055. 19:40:39folder using using do dot SL dat slash
  23056. 19:40:45and load
  23057. 19:40:47backend D
  23058. 19:40:49practice. JS and
  23059. 19:40:55save so we'll go back to the practice
  23060. 19:40:58file and now when we open the website it
  23061. 19:41:01will run this code and send the HTTP
  23062. 19:41:04message to see that let's open the
  23063. 19:41:07website so we'll scroll down and if you
  23064. 19:41:10don't have the website open you can open
  23065. 19:41:12it again by right clicking checkout.
  23066. 19:41:15HTML and then open with live
  23067. 19:41:18server I already have the website open
  23068. 19:41:21so I'm going to open
  23069. 19:41:22it and then I'm going to go to the
  23070. 19:41:25checkout
  23071. 19:41:26page and now we're going to look at the
  23072. 19:41:29console so we'll right
  23073. 19:41:32click and
  23074. 19:41:35inspect and this time instead of going
  23075. 19:41:37to the console tab we're going to go to
  23076. 19:41:40a new tab
  23077. 19:41:42called the network
  23078. 19:41:44tab if you don't see this tab you can
  23079. 19:41:47make this window wider or you might find
  23080. 19:41:50it under these
  23081. 19:41:52arrows so we'll open the network
  23082. 19:41:59Tab and in here this will show us all
  23083. 19:42:03the HTTP messages that are coming in and
  23084. 19:42:06out of our
  23085. 19:42:08computer now before we start looking at
  23086. 19:42:11the the HTTP messages there's one
  23087. 19:42:13important thing to know about the
  23088. 19:42:15network Tab and that is messages will
  23089. 19:42:18only appear here after we open the
  23090. 19:42:22console if we send some messages and
  23091. 19:42:25then we open the console it's not going
  23092. 19:42:27to show up
  23093. 19:42:28here so to make sure our HTTP messages
  23094. 19:42:32are recorded here we need to open the
  23095. 19:42:35network tab first and then we're going
  23096. 19:42:38to refresh the page to resend those
  23097. 19:42:42messages and now if we open the network
  23098. 19:42:45tab we should be able to see the HTTP
  23099. 19:42:48messages at the
  23100. 19:42:52bottom okay so inside this list we
  23101. 19:42:56should be able to see that we sent a
  23102. 19:42:58message to superimple backend. deev if
  23103. 19:43:02you don't see it we can also search this
  23104. 19:43:04list by using the text box up
  23105. 19:43:08here so in here we can type part of the
  23106. 19:43:12URL that we sent the message to for
  23107. 19:43:15example super
  23108. 19:43:18simple and the message that we sent
  23109. 19:43:21should appear here so now we can click
  23110. 19:43:24this message to get more details about
  23111. 19:43:28it on the right side we have several
  23112. 19:43:31tabs here the headers tab gives us
  23113. 19:43:34details about our message or about our
  23114. 19:43:38request so it tells us which type of
  23115. 19:43:41requests that we sent which is get and
  23116. 19:43:44the URL that we sent it to which is
  23117. 19:43:47super simple backend.
  23118. 19:43:49Dev this sends the request across the
  23119. 19:43:53internet to my
  23120. 19:43:55backend and then when my backend
  23121. 19:43:58receives this message or this request it
  23122. 19:44:01will send back a message which is called
  23123. 19:44:04a
  23124. 19:44:06response to see the response from my
  23125. 19:44:09backend we can open the the response
  23126. 19:44:12Tab and you can see that my backend
  23127. 19:44:15responded with hello this is super
  23128. 19:44:18simple dev's back end and a
  23129. 19:44:20documentation page that we can check
  23130. 19:44:23out so we have successfully sent an HTTP
  23131. 19:44:26message or a request to my backend and
  23132. 19:44:30we got back a
  23133. 19:44:33response another useful tab here is the
  23134. 19:44:36preview tab it basically takes a
  23135. 19:44:38response and displays it in an easy to
  23136. 19:44:41read
  23137. 19:44:43format so now let's learn some
  23138. 19:44:46terminology when we send a message to
  23139. 19:44:49the backend this message is called a
  23140. 19:44:52request we are requesting something from
  23141. 19:44:54the
  23142. 19:44:55backend when the backend receives our
  23143. 19:44:58request it will send an HTTP message
  23144. 19:45:01back to us and this message is called
  23145. 19:45:04the
  23146. 19:45:06response so each request that we make
  23147. 19:45:09will get one response from the back
  23148. 19:45:12end and this is called a request
  23149. 19:45:15response cycle it's always one request
  23150. 19:45:18one
  23151. 19:45:22response next currently we're viewing
  23152. 19:45:25the response in the network tab but we
  23153. 19:45:28can actually get this response in our
  23154. 19:45:31code and then do something with this
  23155. 19:45:34response so let's go back to our
  23156. 19:45:37code and here after we send the requ
  23157. 19:45:40Quest we can get the response using a
  23158. 19:45:43property called xhr do
  23159. 19:45:49response however there's a slight
  23160. 19:45:51problem with doing this when we send a
  23161. 19:45:54request to the backend it takes time for
  23162. 19:45:57the request to travel across the
  23163. 19:46:00internet to the back end and for the
  23164. 19:46:02response to come
  23165. 19:46:04back so the response is not available
  23166. 19:46:07right away and xr. response will be
  23167. 19:46:11undefined at first so xr. send is known
  23168. 19:46:15as asynchronous code asynchronous code
  23169. 19:46:19means it does not wait for this line of
  23170. 19:46:21code to finish it just sends the request
  23171. 19:46:25and then immediately goes to the next
  23172. 19:46:27line the response might come back later
  23173. 19:46:30in the future but we don't have access
  23174. 19:46:32to it
  23175. 19:46:34here so in this situation in order to
  23176. 19:46:38get this response we need to wait for
  23177. 19:46:41the response to come back first and then
  23178. 19:46:44we can access do
  23179. 19:46:46response in order to wait for the
  23180. 19:46:49response to come back at the top after
  23181. 19:46:52we create the
  23182. 19:46:53xhr we're going to use this code
  23183. 19:46:58xhr do add capital E Event capital L
  23184. 19:47:04listener and
  23185. 19:47:07brackets so this method listens or waits
  23186. 19:47:11for an event and then it can run a
  23187. 19:47:15function so this method takes two
  23188. 19:47:17parameters the first parameter is the
  23189. 19:47:20event that we want to listen for or to
  23190. 19:47:23wait for in this case we want to wait
  23191. 19:47:26for the response to come back so we're
  23192. 19:47:28going to give it the string load load
  23193. 19:47:32means the response has
  23194. 19:47:36loaded the second parameter is a
  23195. 19:47:39function that we want to run
  23196. 19:47:41after this event
  23197. 19:47:43happens so in this case after the
  23198. 19:47:45response has loaded we're going to run a
  23199. 19:47:48function brackets arrow and curly
  23200. 19:47:54brackets so because this function runs
  23201. 19:47:56after the response loaded inside here we
  23202. 19:48:00can access xhr do
  23203. 19:48:04response and this will not be undefined
  23204. 19:48:07it will contain the response that we saw
  23205. 19:48:09earlier in the network
  23206. 19:48:11tab so let's console.log this to see
  23207. 19:48:14what it looks like
  23208. 19:48:17console.log and
  23209. 19:48:19brackets and we'll remove this line at
  23210. 19:48:22the
  23211. 19:48:23end and
  23212. 19:48:26save so now let's go back and check the
  23213. 19:48:29console for the response so we'll open
  23214. 19:48:32the
  23215. 19:48:33console and then open the console
  23216. 19:48:37Tab and here you can see that we have
  23217. 19:48:40the response in our code and we console.
  23218. 19:48:43logged
  23219. 19:48:45it so this
  23220. 19:48:47response is a string and now we can save
  23221. 19:48:51this in a variable or do whatever we
  23222. 19:48:53want with it in our
  23223. 19:48:57code now one thing you might be
  23224. 19:48:59wondering is why do we put the event
  23225. 19:49:01listener up here at the top so first we
  23226. 19:49:05need to set up the event listener and
  23227. 19:49:08then trigger the event or send the
  23228. 19:49:11request this is the same as setting up
  23229. 19:49:13an event listener for clicking a button
  23230. 19:49:16we need to set up the event listener
  23231. 19:49:18first and then we click the button
  23232. 19:49:22after so that's how we send a request to
  23233. 19:49:25the back end wait for the response and
  23234. 19:49:28then save the response in our
  23235. 19:49:33code next we can send different messages
  23236. 19:49:36or different requests to the backend
  23237. 19:49:39using URL
  23238. 19:49:41paths a URL path is the part that comes
  23239. 19:49:45after the domain name for example in
  23240. 19:49:48this URL the URL path is /hello in this
  23241. 19:49:53URL the URL path is/ products
  23242. 19:49:58slir in this URL if there is no URL path
  23243. 19:50:02the URL path is
  23244. 19:50:06slash we can send a request to these URL
  23245. 19:50:09paths and each URL path will give us a
  23246. 19:50:12different
  23247. 19:50:14response so let's try sending requests
  23248. 19:50:17to different URL paths and see what
  23249. 19:50:19different responses that we
  23250. 19:50:22get so for this example let's send the
  23251. 19:50:26request to super simple backend. deev
  23252. 19:50:29and then the URL path SL hello and
  23253. 19:50:36save and now let's open our console to
  23254. 19:50:39see the response
  23255. 19:50:42and here you can see that this URL path
  23256. 19:50:45gave us a different response than last
  23257. 19:50:48time let's go back and try another URL
  23258. 19:50:52path to see what it looks like so we'll
  23259. 19:50:55delete this and then send the request to
  23260. 19:50:58slash products
  23261. 19:51:02slir and
  23262. 19:51:04save and we'll open the console to view
  23263. 19:51:07the
  23264. 19:51:09response and here here this URL path
  23265. 19:51:12give us a Json and this contains the
  23266. 19:51:15details of the first product in our
  23267. 19:51:18Amazon
  23268. 19:51:20Project so we can send requests to
  23269. 19:51:23different URL paths and we will get
  23270. 19:51:25different responses from the
  23271. 19:51:29backend next a backend only supports a
  23272. 19:51:33certain set of URL paths for example
  23273. 19:51:37when I created my backend super simple
  23274. 19:51:40backend .d I had to decide which URL
  23275. 19:51:44paths are supported and how my backend
  23276. 19:51:47would respond to each URL path if we
  23277. 19:51:51send a request to a URL path that is not
  23278. 19:51:54supported the back end will respond with
  23279. 19:51:56an
  23280. 19:51:59error let's go back to our
  23281. 19:52:01code and we'll try a URL path that is
  23282. 19:52:04not
  23283. 19:52:06supported so let's remove this at the
  23284. 19:52:08end
  23285. 19:52:11and we'll try sending the request to
  23286. 19:52:14not-
  23287. 19:52:16supported and
  23288. 19:52:20save and now if we look at our
  23289. 19:52:23console you'll see that the backend
  23290. 19:52:26responded with an error message saying
  23291. 19:52:28this URL path is not
  23292. 19:52:31supported the browser will also
  23293. 19:52:33console.log this red error
  23294. 19:52:36message this red message is displayed
  23295. 19:52:38automatically when there's an error
  23296. 19:52:43so this message tells us the type of
  23297. 19:52:45request that we sent which is get and
  23298. 19:52:49the URL that we sent it to and also this
  23299. 19:52:53number which is called the status
  23300. 19:52:55code whenever we get a response from the
  23301. 19:52:58backend the backend also gives us a
  23302. 19:53:01status code which tells us if a request
  23303. 19:53:04succeeded or
  23304. 19:53:06failed a status code that starts with
  23305. 19:53:09four or five means the response
  23306. 19:53:12failed starting with four means it was
  23307. 19:53:15our problem we sent the request to a URL
  23308. 19:53:18path that was not
  23309. 19:53:20supported starting with five means it
  23310. 19:53:23was the backends problem for example the
  23311. 19:53:25backend
  23312. 19:53:27crashed a status code starting with a
  23313. 19:53:30two means the response was
  23314. 19:53:34successful so we can also see the status
  23315. 19:53:36code in the network
  23316. 19:53:38tab so we can search for our request and
  23317. 19:53:42then when we click the request in the
  23318. 19:53:45headers tab it will show us the status
  23319. 19:53:47code in this
  23320. 19:53:49row so this tells us if our request
  23321. 19:53:52succeeded or
  23322. 19:53:54failed so now you might be wondering how
  23323. 19:53:57do we know which URL paths are supported
  23324. 19:54:00and which are
  23325. 19:54:02not so there's actually no way to figure
  23326. 19:54:05out all the URL paths that are supported
  23327. 19:54:08and this is for security reasons
  23328. 19:54:11however some backends do provide a
  23329. 19:54:13documentation page that lists the URL
  23330. 19:54:16paths that are supported and also what
  23331. 19:54:18kind of response that they give for my
  23332. 19:54:21backend I created a documentation page
  23333. 19:54:24and we're going to take a look at that
  23334. 19:54:27so we'll open the
  23335. 19:54:28browser and at the top we're going to
  23336. 19:54:30create a new
  23337. 19:54:32tab and to see the documentation for my
  23338. 19:54:35backend we're going to go to super
  23339. 19:54:39simple back back end dodev SL
  23340. 19:54:44documentation and press
  23341. 19:54:49enter so this is the documentation page
  23342. 19:54:52for my backend it gives us a list of
  23343. 19:54:55which URL paths that are supported and
  23344. 19:54:58what each URL path does so when we make
  23345. 19:55:01a request to slash it will give us this
  23346. 19:55:04message which we saw earlier if we make
  23347. 19:55:07a request to/ hello it will give us this
  23348. 19:55:10message which we also saw
  23349. 19:55:14earlier so by the way the list of all
  23350. 19:55:17the URL paths that are supported is
  23351. 19:55:20called the backend
  23352. 19:55:23API API means application programming
  23353. 19:55:27interface interface means how we
  23354. 19:55:29interact with something so this list of
  23355. 19:55:33URL paths are all the ways that we can
  23356. 19:55:35interact with the backend and that's why
  23357. 19:55:38we call this the backend
  23358. 19:55:43API next the backend can respond with
  23359. 19:55:46different types of data so one type of
  23360. 19:55:50data the back end can respond with is
  23361. 19:55:53text for example when we sent the
  23362. 19:55:56request to get/ hello it gave us some
  23363. 19:56:00text
  23364. 19:56:01back so in our
  23365. 19:56:03code if we change this URL path to
  23366. 19:56:08/hello
  23367. 19:56:10and
  23368. 19:56:11save and we open the
  23369. 19:56:15console and click the console
  23370. 19:56:18tab it gives us back some
  23371. 19:56:21text we can also see what type of
  23372. 19:56:23response that we got using the network
  23373. 19:56:26tab so we'll open the network Tab and
  23374. 19:56:29then search for the
  23375. 19:56:31request and then here when we click
  23376. 19:56:34it inside the headers tab if we scroll
  23377. 19:56:38down to the response
  23378. 19:56:40section here it will give us the content
  23379. 19:56:44type or the type of response that we got
  23380. 19:56:47back so in this example /hello gave us
  23381. 19:56:50some text and the type is text
  23382. 19:56:55slpl another type of data the backend
  23383. 19:56:57can respond with is
  23384. 19:57:00Json so if we go back to our code and
  23385. 19:57:04change the url path to products
  23386. 19:57:08slir and
  23387. 19:57:11save and then look at our
  23388. 19:57:15console in the network tab we'll open
  23389. 19:57:18this request and inside the headers if
  23390. 19:57:21we scroll
  23391. 19:57:23down it will tell us that the type of
  23392. 19:57:26the response is application SL
  23393. 19:57:30Json so the backend sent us back some
  23394. 19:57:33Json if we open the console tab we can
  23395. 19:57:37see the Json string here
  23396. 19:57:41so once we get this Json from the
  23397. 19:57:43backend we can use json.parse to convert
  23398. 19:57:47this back into a JavaScript
  23399. 19:57:50object basically this allows us to send
  23400. 19:57:53JavaScript objects across the internet
  23401. 19:57:56to the back
  23402. 19:57:58end another type of data the back end
  23403. 19:58:01can respond with is
  23404. 19:58:05HTML we'll go back to our
  23405. 19:58:07code and we'll change the URL path to
  23406. 19:58:14documentation so this was the
  23407. 19:58:16documentation page that we saw earlier
  23408. 19:58:19if we
  23409. 19:58:21save and then open the
  23410. 19:58:25console you'll see that the back end
  23411. 19:58:27responded with a bunch of
  23412. 19:58:29HTML if we look at the network Tab and
  23413. 19:58:33search for the request and open the
  23414. 19:58:36request here and in the headers tab we
  23415. 19:58:39scroll down
  23416. 19:58:40to the content type it will tell us that
  23417. 19:58:43the type of the response was text/html
  23418. 19:58:50finally another type of data the backend
  23419. 19:58:53can respond with is an
  23420. 19:58:56image for example we'll go back to our
  23421. 19:58:59code and change the url path to
  23422. 19:59:06imagesapple
  23423. 19:59:08jpg and say
  23424. 19:59:11save and we'll open the
  23425. 19:59:13console and in the network tab we can
  23426. 19:59:17open this
  23427. 19:59:19request and in the headers tab we can
  23428. 19:59:22scroll down and it will tell us that the
  23429. 19:59:25type of response was an
  23430. 19:59:29image/jpeg as you can see the backend
  23431. 19:59:32can respond with different types of data
  23432. 19:59:35not just
  23433. 19:59:38text okay so we got this image back from
  23434. 19:59:42the back end but what does this look
  23435. 19:59:44like in the console we'll open the
  23436. 19:59:48console and we'll see that we just get a
  23437. 19:59:50bunch of random characters here and
  23438. 19:59:53that's because the console is trying to
  23439. 19:59:54display the image as text but the image
  23440. 19:59:57is not really
  23441. 19:59:58text this is the image's raw data so
  23442. 20:00:02that's why it shows all these random
  23443. 20:00:03characters
  23444. 20:00:05here however it would be better to
  23445. 20:00:08display this as an actual ual image
  23446. 20:00:11instead of raw
  23447. 20:00:12data we can display this as an image by
  23448. 20:00:16using the
  23449. 20:00:17browser so if we open the
  23450. 20:00:20browser when we type a URL at the top of
  23451. 20:00:24the browser it actually sends a get
  23452. 20:00:27request to that
  23453. 20:00:30URL so typing in here actually does the
  23454. 20:00:33same thing as our code it makes a get
  23455. 20:00:37request the one big difference is that
  23456. 20:00:40that in the browser when it gets back
  23457. 20:00:42the response it will display the
  23458. 20:00:44response on the
  23459. 20:00:46page for example if we enter superimple
  23460. 20:00:50backend dodev and change the url path to
  23461. 20:00:54just nothing or slash and press
  23462. 20:00:59enter this actually sends a get request
  23463. 20:01:02to my back end the backend responds with
  23464. 20:01:06some text and it displays this response
  23465. 20:01:09on the
  23466. 20:01:11page so using the browser is the same
  23467. 20:01:14thing as making a get
  23468. 20:01:18request now the cool thing about this is
  23469. 20:01:21that if the browser gets back an image
  23470. 20:01:23it will display it as an actual image
  23471. 20:01:26instead of raw data that we saw in the
  23472. 20:01:29console so here if we change the url
  23473. 20:01:32path
  23474. 20:01:33to/ images
  23475. 20:01:36slapple jpg and press enter
  23476. 20:01:41it will get back the same image as
  23477. 20:01:43before except the browser will display
  23478. 20:01:46it as an actual image on the
  23479. 20:01:50page same thing happens with the backend
  23480. 20:01:52responds with
  23481. 20:01:54HTML so at the Top If we change the url
  23482. 20:01:58path to slash
  23483. 20:02:01documentation and press
  23484. 20:02:05enter if the response from the back end
  23485. 20:02:07is HTML theow browser will display this
  23486. 20:02:11HTML as an actual web page instead of
  23487. 20:02:14raw
  23488. 20:02:16HTML so again using the browser is the
  23489. 20:02:19same thing as making a get
  23490. 20:02:22request and depending on the type of
  23491. 20:02:25response the browser can display the
  23492. 20:02:27response in a more useful way on the
  23493. 20:02:31page and that's it we learn the basics
  23494. 20:02:34of how a backend Works how to send
  23495. 20:02:36requests to a backend and how to send
  23496. 20:02:39requests to different different URL
  23497. 20:02:41paths so now let's go back into our
  23498. 20:02:44code and we'll remove this URL path and
  23499. 20:02:49just dodev so that we don't keep console
  23500. 20:02:52logging and image and then
  23501. 20:02:56save and let's save this practice code
  23502. 20:02:58into git we'll open the kit
  23503. 20:03:03section in backend practice we learn how
  23504. 20:03:06to make requests to the backend and in
  23505. 20:03:09checkout. as we loaded that backend
  23506. 20:03:12practice
  23507. 20:03:13file so let's type a message describing
  23508. 20:03:16our
  23509. 20:03:17changes add backend
  23510. 20:03:21practice file and
  23511. 20:03:31commit now that we learned what a
  23512. 20:03:33backend is and how to communicate with a
  23513. 20:03:35backend let's use a backend in our
  23514. 20:03:38project
  23515. 20:03:40so first we're going to open our code
  23516. 20:03:43and we're actually going to open
  23517. 20:03:44checkout.
  23518. 20:03:46JS and we're going to comment out this
  23519. 20:03:48import statement because we're not going
  23520. 20:03:50to run the practice file anymore so
  23521. 20:03:53we'll type double slash to comment that
  23522. 20:03:55out and
  23523. 20:03:59save next super simple backend dodev has
  23524. 20:04:03a URL path called slash
  23525. 20:04:06products this gives us a list of all the
  23526. 20:04:09products in the Amazon Project so first
  23527. 20:04:12let's open our browser and visit this
  23528. 20:04:16URL path to see what the response looks
  23529. 20:04:19like we'll open this tab to super simple
  23530. 20:04:23backend dodev and we're going to change
  23531. 20:04:26the url path at the end to slash
  23532. 20:04:30products and press
  23533. 20:04:33enter and you can see that this URL path
  23534. 20:04:37gives us a Json response so this
  23535. 20:04:40contains all the details of the products
  23536. 20:04:43for our
  23537. 20:04:45project now instead of using a file on
  23538. 20:04:48our computer to load the products let's
  23539. 20:04:50use the backend to load the
  23540. 20:04:54products so let's go back into our
  23541. 20:04:58code and on the left we're going to open
  23542. 20:05:01data/ products.
  23543. 20:05:05JS and we're going to scroll down to the
  23544. 20:05:08array of prodct S at the end so now
  23545. 20:05:12instead of loading the products from
  23546. 20:05:13this file we're going to load the
  23547. 20:05:15products from the back end so first
  23548. 20:05:18let's comment out all this code if you
  23549. 20:05:21need to you can click this arrow on the
  23550. 20:05:23left to collapse this array and we're
  23551. 20:05:26going to comment this out using slash
  23552. 20:05:29star and at the end star
  23553. 20:05:32slash and above this we're going to load
  23554. 20:05:36the products from the back end
  23555. 20:05:39first let's create a variable to save
  23556. 20:05:41the products again so we use let
  23557. 20:05:45products equal and empty array and let's
  23558. 20:05:49export this so we can use it outside of
  23559. 20:05:52this file like before at the front we'll
  23560. 20:05:55type
  23561. 20:05:58export next let's create a function to
  23562. 20:06:01load these products from the back end
  23563. 20:06:04we'll create a
  23564. 20:06:06function and let's name it load products
  23565. 20:06:10brackets and curly
  23566. 20:06:14brackets and inside here we're going to
  23567. 20:06:16create an XML HTTP request again to make
  23568. 20:06:20a request to my
  23569. 20:06:22backend so to create a request we're
  23570. 20:06:24going to use new in the class XML HTTP
  23571. 20:06:31request and brackets so this will
  23572. 20:06:34generate a new request object and let's
  23573. 20:06:38save this in a variable
  23574. 20:06:40so at the front we'll use const and
  23575. 20:06:42we'll name it xhr equals this
  23576. 20:06:47object next we're going to set up the
  23577. 20:06:50request using xhr doop and
  23578. 20:06:56brackets so open takes two parameters
  23579. 20:06:58the first parameter is the type of
  23580. 20:07:00request that we want to send in this
  23581. 20:07:03case the type of request will be the
  23582. 20:07:05string get
  23583. 20:07:09and we're going to give open a second
  23584. 20:07:11parameter which is the URL that we want
  23585. 20:07:13to send this request to so we'll give it
  23586. 20:07:16a string and the URL of my backend is
  23587. 20:07:22https colon slash super simple backend
  23588. 20:07:30dodev and then we'll add the URL path
  23589. 20:07:33which is slash
  23590. 20:07:37products finally we're going to send
  23591. 20:07:40this request using the code xr. send and
  23592. 20:07:48brackets so this will create and set up
  23593. 20:07:51a request and then send it to my backend
  23594. 20:07:54now remember that do send is
  23595. 20:07:57asynchronous that means it will just
  23596. 20:07:59send the request but it will not wait
  23597. 20:08:02for a response to come
  23598. 20:08:04back in order to wait for a response
  23599. 20:08:07above this code we're going to create
  23600. 20:08:10some new lines and set up an event
  23601. 20:08:12listener using
  23602. 20:08:15xr. add event listener and
  23603. 20:08:21brackets and this takes two parameters
  23604. 20:08:24the first one is the event we want to
  23605. 20:08:26listen for in this case it's the string
  23606. 20:08:29load which means the response has
  23607. 20:08:32loaded next we're going to give it a
  23608. 20:08:35second parameter which is a function so
  23609. 20:08:38brackets Arrow and curly
  23610. 20:08:40brackets and this is a function that we
  23611. 20:08:43want to run after the response has
  23612. 20:08:47loaded so inside here after the response
  23613. 20:08:51has loaded we can access it using xhr do
  23614. 20:08:57response for now let's console.log this
  23615. 20:09:00to see what it looks like so we console.
  23616. 20:09:04log and brackets
  23617. 20:09:09finally let's run this function so that
  23618. 20:09:12we run all this code at the bottom we're
  23619. 20:09:16going to load products and
  23620. 20:09:20brackets and
  23621. 20:09:23save so this will send a request to my
  23622. 20:09:26backend and then once we get the
  23623. 20:09:28response it will display it in the
  23624. 20:09:32console so now let's go to our
  23625. 20:09:34website we're going to open the project
  23626. 20:09:37tab
  23627. 20:09:39and don't worry if the page is blank for
  23628. 20:09:41you we're going to fix this in a bit for
  23629. 20:09:44now we're going to open the console by
  23630. 20:09:46right
  23631. 20:09:47clicking
  23632. 20:09:48inspect and open the
  23633. 20:09:52console and in here you'll see that xr.
  23634. 20:09:55response gave us this Json and this
  23635. 20:09:58represents all the products in our
  23636. 20:10:01Amazon
  23637. 20:10:05Project now that we have all this Json
  23638. 20:10:08we're going to convert this back into a
  23639. 20:10:10JavaScript object and then use it in the
  23640. 20:10:13rest of our code let's go back to our
  23641. 20:10:16code and here the response is a
  23642. 20:10:20Json we're going to convert it back into
  23643. 20:10:22a JavaScript object or in this case a
  23644. 20:10:25JavaScript array using the code
  23645. 20:10:29json. pars and
  23646. 20:10:34brackets so once we' converted this back
  23647. 20:10:36to an array we're going to replace the
  23648. 20:10:38product up here with what we loaded from
  23649. 20:10:41the back end to do that we'll do
  23650. 20:10:44products and make it equal to this
  23651. 20:10:48result that we got from the back
  23652. 20:10:51end now one more thing we need to do is
  23653. 20:10:54previously we converted the product
  23654. 20:10:57objects into a class so we're just going
  23655. 20:11:00to do that again with this
  23656. 20:11:03array so we'll select the code from map
  23657. 20:11:08all the way to the end
  23658. 20:11:10and then right click
  23659. 20:11:13copy and
  23660. 20:11:16here after this products array that we
  23661. 20:11:19got from the back
  23662. 20:11:21end right click
  23663. 20:11:24paste we're also going to transform this
  23664. 20:11:27array from objects into these
  23665. 20:11:31classes and at the end let's console.log
  23666. 20:11:35this products array to see what it looks
  23667. 20:11:36like now so we'll console
  23668. 20:11:39. log
  23669. 20:11:42products and
  23670. 20:11:45save and now if we open the console for
  23671. 20:11:48our
  23672. 20:11:49project you'll see that products is now
  23673. 20:11:51an array of product classes and clothing
  23674. 20:11:55classes so we converted the response
  23675. 20:11:58from a Json into a JavaScript array and
  23676. 20:12:02then into these
  23677. 20:12:06classes so that's how we load data from
  23678. 20:12:09the the back end and then convert it
  23679. 20:12:11into a JavaScript object or array that
  23680. 20:12:14we can use in our
  23681. 20:12:18code next let's go back to our code and
  23682. 20:12:22instead of console. loging the products
  23683. 20:12:24every time which will display a lot of
  23684. 20:12:26stuff in the console let's just
  23685. 20:12:28console.log a message to make sure that
  23686. 20:12:31this is working so we'll change this to
  23687. 20:12:33the string load products
  23688. 20:12:40and now that we have this function let's
  23689. 20:12:42use it in each of our
  23690. 20:12:45Pages first we're going to remove
  23691. 20:12:48calling this function for now and we're
  23692. 20:12:51going to export this function so we can
  23693. 20:12:53use it outside of this file so
  23694. 20:12:57export and save this
  23695. 20:13:01file and we're going to start from the
  23696. 20:13:05homepage so we'll scroll down to the
  23697. 20:13:07bottom to amazon.
  23698. 20:13:11JS and we're going to scroll to the
  23699. 20:13:15top and first we're going to import load
  23700. 20:13:19products we're already importing from
  23701. 20:13:21products. JS so we'll just add a comma
  23702. 20:13:25and load
  23703. 20:13:28products and now before we run all of
  23704. 20:13:31this code at the top we're going to load
  23705. 20:13:35the products first and brackets
  23706. 20:13:40so now let's
  23707. 20:13:42save and open the project in our
  23708. 20:13:46browser and we'll click the top left
  23709. 20:13:48icon to go to the
  23710. 20:13:51homepage and you'll see that the
  23711. 20:13:53homepage is actually empty so what went
  23712. 20:13:55wrong
  23713. 20:13:57here remember that HTTP requests are
  23714. 20:14:01asynchronous if we go back into our
  23715. 20:14:04code load products will send a request
  23716. 20:14:08to my backend
  23717. 20:14:09but it takes time for the request to
  23718. 20:14:11travel across the internet to my backend
  23719. 20:14:15and to travel
  23720. 20:14:17back so here it will just send the
  23721. 20:14:20request and at the next line the
  23722. 20:14:22response has not loaded yet so the
  23723. 20:14:26products array is still
  23724. 20:14:28empty and when we run this code with an
  23725. 20:14:30empty array the page will be
  23726. 20:14:35empty to solve this problem we need to
  23727. 20:14:38wait for this HTTP request to finish
  23728. 20:14:41first and for the response to come back
  23729. 20:14:44and then we're going to run the rest of
  23730. 20:14:46this
  23731. 20:14:47code so how do we do
  23732. 20:14:49that so we're going to go to the left
  23733. 20:14:52and we're going to scroll up to data/
  23734. 20:14:55products.
  23735. 20:14:56JS and I'm going to drag this file to
  23736. 20:14:59the top so we can see the code side by
  23737. 20:15:03side and I'll scroll down to the HTTP
  23738. 20:15:07request
  23739. 20:15:10which is right
  23740. 20:15:13here and remember that we used an event
  23741. 20:15:16listener to wait for the response to
  23742. 20:15:18come back once the response is loaded
  23743. 20:15:21we're going to run this
  23744. 20:15:24function so basically we want to run all
  23745. 20:15:27of this code at the bottom of this event
  23746. 20:15:31listener so after we've loaded our
  23747. 20:15:36products so one way to solve this
  23748. 20:15:38problem is maybe we can just copy all
  23749. 20:15:41this code and then paste it at the
  23750. 20:15:43bottom
  23751. 20:15:45here now obviously this doesn't make
  23752. 20:15:47sense because we're copy pasting a bunch
  23753. 20:15:50of code and then we can't really reuse
  23754. 20:15:52this
  23755. 20:15:54function a better solution is we're
  23756. 20:15:57going to put all this code in a
  23757. 20:16:00function and then we can give this
  23758. 20:16:03function to load products let's do this
  23759. 20:16:06step by step
  23760. 20:16:09so first at the top let's create a
  23761. 20:16:11function to group all of this code
  23762. 20:16:14together we'll use the word
  23763. 20:16:17function and let's name it render
  23764. 20:16:21products grid brackets and curly
  23765. 20:16:26brackets and now we're going to move all
  23766. 20:16:28of this code into the
  23767. 20:16:30function so we can do that by removing
  23768. 20:16:33this closing curly
  23769. 20:16:36bracket and then at the bottom we we're
  23770. 20:16:38going to scroll down all the way to the
  23771. 20:16:41bottom here we're going to add back the
  23772. 20:16:44closing curler bracket to put all this
  23773. 20:16:47code inside the function and now let's
  23774. 20:16:50also select this code so we can fix the
  23775. 20:16:53index so we'll select it all the way up
  23776. 20:16:57to
  23777. 20:16:58here and then press tab to add an indet
  23778. 20:17:03so now we put all the code inside render
  23779. 20:17:06products grid
  23780. 20:17:09next we're going to put this function
  23781. 20:17:12inside load products using a parameter
  23782. 20:17:17so between these brackets we're going to
  23783. 20:17:19give it this
  23784. 20:17:20function
  23785. 20:17:22render products
  23786. 20:17:26grid so remember in JavaScript functions
  23787. 20:17:29are values and we can use a function as
  23788. 20:17:32a
  23789. 20:17:33parameter so now inside load products if
  23790. 20:17:36we go up here at the top this function
  23791. 20:17:41will be saved in a parameter so let's
  23792. 20:17:44create a parameter at the top and we'll
  23793. 20:17:46name it fun to say that this parameter
  23794. 20:17:50will contain a
  23795. 20:17:53function so we basically saved this
  23796. 20:17:56function inside this parameter and now
  23797. 20:18:00after we load the response we're going
  23798. 20:18:02to run the
  23799. 20:18:05function so at the bottom here after
  23800. 20:18:08we've created the products array we're
  23801. 20:18:11going to run font and
  23802. 20:18:16brackets so that's how we run all of
  23803. 20:18:19this code after we've loaded the
  23804. 20:18:22products we group this code into a
  23805. 20:18:25function and then we save the function
  23806. 20:18:27up here after we load the response we're
  23807. 20:18:31going to run the
  23808. 20:18:34function so let's save these two files
  23809. 20:18:37we'll save products. Js
  23810. 20:18:40and then down here we'll save amazon.
  23811. 20:18:43JS and then open our
  23812. 20:18:45website and you'll see that the products
  23813. 20:18:48loaded again because this time we're
  23814. 20:18:50waiting for the response to come back
  23815. 20:18:53and then we run our
  23816. 20:18:56code so that's how we wait for an HTTP
  23817. 20:19:00request to finish first and then we run
  23818. 20:19:04some
  23819. 20:19:07code by the the way this function that
  23820. 20:19:10we provide to load products this is
  23821. 20:19:13known as a call back because we're
  23822. 20:19:17giving it a function to run in the
  23823. 20:19:19future or to call in the future so this
  23824. 20:19:22function will be called back in the
  23825. 20:19:27future and we've actually used call
  23826. 20:19:30backs before when we wrote the code set
  23827. 20:19:33timeout we provided set timeout a
  23828. 20:19:36function to run in the future or or to
  23829. 20:19:38call in the future so this function is
  23830. 20:19:42also known as a call
  23831. 20:19:44back don't worry if this technique is a
  23832. 20:19:47little complicated at first the more you
  23833. 20:19:50work with callbacks the more you'll get
  23834. 20:19:52used to
  23835. 20:19:54it let's get some more practice and use
  23836. 20:19:57the backend and callbacks on the
  23837. 20:19:59checkout page so we're going to open
  23838. 20:20:02checkout. JS which is the checkout page
  23839. 20:20:07and first we're going to in import load
  23840. 20:20:09products so at the top we'll create an
  23841. 20:20:13import curly brackets load
  23842. 20:20:18products from a
  23843. 20:20:21string and we'll locate products. JS so
  23844. 20:20:25inside here we're in the scripts folder
  23845. 20:20:28we're going to get out of it using do do
  23846. 20:20:30slash and then we're going to go into
  23847. 20:20:32the data
  23848. 20:20:35folder and then we'll add slash data
  23849. 20:20:39slash and we'll load products.
  23850. 20:20:45JS and then down here we're also going
  23851. 20:20:49to load products and
  23852. 20:20:54brackets and again we're going to give
  23853. 20:20:56load products a call back or a function
  23854. 20:20:59that we want to run in the
  23855. 20:21:02future this time instead of creating a
  23856. 20:21:05whole new function we're just going to
  23857. 20:21:07create a function in inside these
  23858. 20:21:09brackets using brackets arrow and curly
  23859. 20:21:15brackets so we don't need to create a
  23860. 20:21:17function name every time we can also
  23861. 20:21:20just create an anonymous function or a
  23862. 20:21:23function without a
  23863. 20:21:25name and then give this to load products
  23864. 20:21:28to run in the
  23865. 20:21:29future now inside this Anonymous
  23866. 20:21:32function we're just going to move this
  23867. 20:21:34code into here so we'll select this code
  23868. 20:21:38and then rightclick
  23869. 20:21:41cut and then inside here we're going to
  23870. 20:21:44rightclick and
  23871. 20:21:47paste and if you need to you can fix the
  23872. 20:21:52formatting so again this is going to
  23873. 20:21:55save this function inside the parameter
  23874. 20:21:59fun and then we're going to call fun
  23875. 20:22:02after we've loaded all the
  23876. 20:22:05products so let's save this file
  23877. 20:22:09and open our
  23878. 20:22:10website and we'll click the cart in the
  23879. 20:22:13top right to go to the checkout page and
  23880. 20:22:16now the checkout page is loading
  23881. 20:22:20again so that's how we use a backend in
  23882. 20:22:23our project we need to send a request
  23883. 20:22:27and then use a call back to wait for a
  23884. 20:22:31response and then run the rest of our
  23885. 20:22:35code so now let's save our changes into
  23886. 20:22:38G
  23887. 20:22:39we'll go to the git
  23888. 20:22:41section and in products.
  23889. 20:22:49JS we created the load products
  23890. 20:22:54function in amazon.
  23891. 20:22:56JS we used load products and gave it a
  23892. 20:23:00call
  23893. 20:23:01back and in checkout. JS we also used
  23894. 20:23:06load products and we gave it a call back
  23895. 20:23:09using an anonymous function or a
  23896. 20:23:11function without a
  23897. 20:23:13name let's create a message describing
  23898. 20:23:16our
  23899. 20:23:18changes load products from the backend
  23900. 20:23:25and
  23901. 20:23:30commit next we're going to learn testing
  23902. 20:23:34with a
  23903. 20:23:36backend now that we're using a back back
  23904. 20:23:38end in our code we need to update our
  23905. 20:23:41tests first let's go to our code and
  23906. 20:23:45we're going to run our tests so we'll
  23907. 20:23:47scroll to the bottom and if you don't
  23908. 20:23:50have the tests open we can run them by
  23909. 20:23:52right clicking test.html and then click
  23910. 20:23:56open with live server now I already have
  23911. 20:23:59the test open in my browser so I'm just
  23912. 20:24:02going to open that
  23913. 20:24:04Tab and for the video I'll zoom in using
  23914. 20:24:07control equals or command
  23915. 20:24:10equals and we have some failing tests
  23916. 20:24:13now so if you did the exercises from the
  23917. 20:24:16previous few lessons you might have more
  23918. 20:24:18than two failing tests but we're going
  23919. 20:24:20to focus on fixing these two tests here
  23920. 20:24:23for the
  23921. 20:24:25video so first let's open the code for
  23922. 20:24:28our order summary tests we'll open vs
  23923. 20:24:31code and then on the left we're going to
  23924. 20:24:34go into the test folder SL checkout SL
  23925. 20:24:37Ord summary
  23926. 20:24:42test.js so now we're going to scroll
  23927. 20:24:44down in this test and notice that here
  23928. 20:24:48we are rendering the order summary which
  23929. 20:24:51creates the cart on the checkout
  23930. 20:24:55page however this card requires the
  23931. 20:24:58products to be loaded from the back end
  23932. 20:25:01and currently we're not loading the
  23933. 20:25:03products in our tests so that's why the
  23934. 20:25:06tests are failing we need to to load the
  23935. 20:25:09products
  23936. 20:25:11first to do that let's import the load
  23937. 20:25:14products function at the top so we'll
  23938. 20:25:17scroll
  23939. 20:25:18up and we're going to add another
  23940. 20:25:21import curly brackets the load products
  23941. 20:25:26function from a string and we'll locate
  23942. 20:25:30products. JS so we need to get out of
  23943. 20:25:33checkout and out of tests using do do
  23944. 20:25:36slash do do slash and and then we'll go
  23945. 20:25:38into the data folder data/ and load
  23946. 20:25:43products.
  23947. 20:25:46JS and now we could call load products
  23948. 20:25:50inside before each but this will load
  23949. 20:25:53the products before each of our tests so
  23950. 20:25:55it will load it multiple times and we
  23951. 20:25:58don't really need to do this we only
  23952. 20:26:00need to load the products once for all
  23953. 20:26:02of our
  23954. 20:26:04tests to do that we're going to use
  23955. 20:26:07another hook provided by Jasmine called
  23956. 20:26:11before
  23957. 20:26:13all so above before
  23958. 20:26:15each we're going to use the function
  23959. 20:26:18before all and
  23960. 20:26:23brackets so this is the before all hook
  23961. 20:26:26it will run a function before all of our
  23962. 20:26:30tests inside these brackets we're going
  23963. 20:26:32to give it a function to run so brackets
  23964. 20:26:35arrow and curly brackets
  23965. 20:26:39and now before all of our tests we're
  23966. 20:26:42going to load the products so let's load
  23967. 20:26:46products and
  23968. 20:26:49brackets and
  23969. 20:26:52save and now let's open our tests in the
  23970. 20:26:55browser and unfortunately the tests are
  23971. 20:26:58still
  23972. 20:27:00failing let's go back to our code and
  23973. 20:27:02see what the problem is we'll open vs
  23974. 20:27:06code and here
  23975. 20:27:09remember that load products is
  23976. 20:27:12asynchronous that means it just sends a
  23977. 20:27:14request to my back end but it doesn't
  23978. 20:27:17wait for the response to come back so it
  23979. 20:27:20would just send a request and then
  23980. 20:27:22continue with the rest of the test
  23981. 20:27:25code and unfortunately at this point the
  23982. 20:27:28response has not come back yet products
  23983. 20:27:31is still an empty
  23984. 20:27:34array to fix this issue we need to wait
  23985. 20:27:37for a load products to finish first and
  23986. 20:27:40then continue with the rest of the
  23987. 20:27:44test to do that Jasmine has a feature
  23988. 20:27:48for waiting for some code to finish
  23989. 20:27:50called a done
  23990. 20:27:53function so in before all we can give
  23991. 20:27:56this inner Function One parameter called
  23992. 20:28:02done so done is a function and it's
  23993. 20:28:05provided by Jasmine when we we add this
  23994. 20:28:09done parameter before all will not
  23995. 20:28:12automatically go to the next step now
  23996. 20:28:15it's just going to wait and it will only
  23997. 20:28:18go to the next step when we call this
  23998. 20:28:20done function so when we do done and
  23999. 20:28:24brackets then it will go to the next
  24000. 20:28:28step if we don't call done then before
  24001. 20:28:31all we'll just keep waiting forever so
  24002. 20:28:34this done function allows us to control
  24003. 20:28:38exactly when before all finishes and
  24004. 20:28:40goes to the next
  24005. 20:28:43step and this is what we need to fix the
  24006. 20:28:46test we need to wait for load products
  24007. 20:28:48to finish first and then we're going to
  24008. 20:28:51call done and then go to the next
  24009. 20:28:56step to wait for load products to finish
  24010. 20:29:00remember we're going to give it a
  24011. 20:29:02function so between these brackets we'll
  24012. 20:29:04create a function using brackets Arrow
  24013. 20:29:08and curly
  24014. 20:29:10brackets now once the products have
  24015. 20:29:13finished loading it will run the
  24016. 20:29:15function inside here we're going to call
  24017. 20:29:19done and
  24018. 20:29:22brackets so this makes sure that the
  24019. 20:29:24products load first and then we call
  24020. 20:29:26done and then it goes to the next step
  24021. 20:29:30which is the before each hook and then
  24022. 20:29:32eventually it runs the tests at the
  24023. 20:29:35bottom
  24024. 20:29:38so now let's
  24025. 20:29:40save and take a look at our test page
  24026. 20:29:43and you'll see that those order summary
  24027. 20:29:45tests are now passing if you have any
  24028. 20:29:48other fail tests I'll leave that as an
  24029. 20:29:52exercise let's go back to our
  24030. 20:29:56code and that's how we test with a
  24031. 20:30:00backend we wait until the backend code
  24032. 20:30:03is finished and then we use the done
  24033. 20:30:05function to control exactly when to go
  24034. 20:30:08to the next step by the way we can also
  24035. 20:30:12use done inside before each and inside
  24036. 20:30:15it as well to wait for backend code to
  24037. 20:30:20finish now let's save our changes into
  24038. 20:30:23git we'll open the get section and
  24039. 20:30:26inside order summary test we imported
  24040. 20:30:28load products and then we called load
  24041. 20:30:31products and use the done function to
  24042. 20:30:34make sure that the products are loaded
  24043. 20:30:36first and then we go to The Next
  24044. 20:30:39Step let's create a message describing
  24045. 20:30:42our
  24046. 20:30:43changes load products in before all hook
  24047. 20:30:51and
  24048. 20:30:58commit next we're going to learn
  24049. 20:31:01promises which are a better way to
  24050. 20:31:03handle asynchronous code promises are
  24051. 20:31:06similar to Jasmine done function that we
  24052. 20:31:09just learned they let us wait for some
  24053. 20:31:12asynchronous code to finish before going
  24054. 20:31:14to the next
  24055. 20:31:18step so now let's create our first
  24056. 20:31:22promise we're going to open our code and
  24057. 20:31:25on the left we're going to go to
  24058. 20:31:27checkout.
  24059. 20:31:31JS and in here at the top we're going to
  24060. 20:31:35practice promises
  24061. 20:31:38promise is a class so we're going to
  24062. 20:31:40type the code new capital P promise and
  24063. 20:31:46brackets to create a new promise
  24064. 20:31:49object now inside these brackets we're
  24065. 20:31:52going to give it a function so brackets
  24066. 20:31:55arrow and curly
  24067. 20:31:58brackets so that's just how promises are
  24068. 20:32:01designed it's a built-in class and when
  24069. 20:32:04we create a promise we need to give it a
  24070. 20:32:06function
  24071. 20:32:10so when we create This Promise it's
  24072. 20:32:12going to run this function
  24073. 20:32:16immediately so inside here let's
  24074. 20:32:21console.log the string
  24075. 20:32:23promise to show that it runs
  24076. 20:32:27immediately so we'll save this
  24077. 20:32:30file and then go to the
  24078. 20:32:32website and we'll open the tab for our
  24079. 20:32:35project
  24080. 20:32:38and I'm going to zoom out a bit by
  24081. 20:32:39pressing control or command
  24082. 20:32:43minus and then open the console right
  24083. 20:32:46click
  24084. 20:32:48inspect and the
  24085. 20:32:51console and you'll see a logged
  24086. 20:32:55promise so when we create a promise it
  24087. 20:32:59runs this inner function
  24088. 20:33:03immediately now this inner function gets
  24089. 20:33:06a parameter called called
  24090. 20:33:09resolve and resolve is a function and it
  24091. 20:33:13works very similar to Jasmine's done
  24092. 20:33:16function so resolve lets us control when
  24093. 20:33:19to go to the next
  24094. 20:33:23step for example let's add some
  24095. 20:33:26asynchronous code into this
  24096. 20:33:29promise we'll remove the
  24097. 20:33:32console.log and we're going to run load
  24098. 20:33:34products which is asynchronous
  24099. 20:33:37so here we'll type load products and
  24100. 20:33:43brackets and now we're going to give
  24101. 20:33:45load products a callback function like
  24102. 20:33:48we did down
  24103. 20:33:50here brackets arrow and curly
  24104. 20:33:54brackets so it will run this function
  24105. 20:33:57after load products is
  24106. 20:34:00finished once this is finished we want
  24107. 20:34:02to go to the next step so here we're
  24108. 20:34:05going to call resolve so resolve and
  24109. 20:34:11brackets so you can see that the syntax
  24110. 20:34:13here is very similar to jasmin's done
  24111. 20:34:16function we run some asynchronous code
  24112. 20:34:19and then we wait for it to finish and
  24113. 20:34:21then we call resolve to go to the next
  24114. 20:34:26step now what is the next step to This
  24115. 20:34:31Promise so you might think that this
  24116. 20:34:33code down here is the next
  24117. 20:34:36step however a promise actually creates
  24118. 20:34:39a separate line of code or a separate
  24119. 20:34:42thread of
  24120. 20:34:44code so here these two groups of code
  24121. 20:34:48are sort of running at the same time
  24122. 20:34:51feel free to pause the video to get a
  24123. 20:34:52longer look at this
  24124. 20:34:55diagram the reason promises are designed
  24125. 20:34:58this way is it allows JavaScript to do
  24126. 20:35:01multiple things at the same
  24127. 20:35:04time so when the promise finishes it can
  24128. 20:35:08do a Next Step but this next step is
  24129. 20:35:11going to be separate from the rest of
  24130. 20:35:13the code it sort of does the next step
  24131. 20:35:17on the
  24132. 20:35:19side so right now this promise creates a
  24133. 20:35:23separate thread of code but this
  24134. 20:35:26separate thread doesn't actually have a
  24135. 20:35:28next step so we're going to add
  24136. 20:35:33one to add a next step to a promise at
  24137. 20:35:37the end and we're going to use a method
  24138. 20:35:39called dot then and
  24139. 20:35:44brackets and we need to give then a
  24140. 20:35:47function so we're going to type brackets
  24141. 20:35:50arrow and Cur
  24142. 20:35:52brackets and inside here let's just
  24143. 20:35:56console.log the string Next
  24144. 20:36:01Step so first we run this asynchronous
  24145. 20:36:05code and then we wait for it to finish
  24146. 20:36:08and then we run resolve and resolve
  24147. 20:36:11makes it go to the next
  24148. 20:36:14step to make this easier to see in the
  24149. 20:36:16console let's actually add some console.
  24150. 20:36:19logs to track the progress of this code
  24151. 20:36:23so at the top let's
  24152. 20:36:26console.log the string start
  24153. 20:36:30promise and then in here let's
  24154. 20:36:35console.log the string
  24155. 20:36:38finished
  24156. 20:36:40loading and we'll also comment out the
  24157. 20:36:43code that we had
  24158. 20:36:45before so we'll type
  24159. 20:36:48slashstar and at the bottom star slash
  24160. 20:36:52and now let's
  24161. 20:36:54save and check our
  24162. 20:37:00console so let's go through this step by
  24163. 20:37:03step first we start the promise which we
  24164. 20:37:06see here
  24165. 20:37:07and then we call load products which we
  24166. 20:37:10see here once the products are finished
  24167. 20:37:13loading it's going to run this function
  24168. 20:37:16and then we'll display finish loading
  24169. 20:37:19and then we're going to call resolve
  24170. 20:37:21which goes to the next step which runs
  24171. 20:37:24this function and we display Next
  24172. 20:37:28Step so resolve lets us control when to
  24173. 20:37:32go to the next
  24174. 20:37:33step in this case we waited until load
  24175. 20:37:37product prods was finished and then we
  24176. 20:37:39called resolve to go to the next
  24177. 20:37:42step so that's how promises work they
  24178. 20:37:46let us split our code into separate
  24179. 20:37:48steps and then we can wait for one step
  24180. 20:37:51to finish before going to the next
  24181. 20:37:56step now that we understand promises
  24182. 20:37:59let's convert the code that we had
  24183. 20:38:01before from a call back to using a
  24184. 20:38:06promise so first let's remove these
  24185. 20:38:08console. logs to make our code a little
  24186. 20:38:11easier to
  24187. 20:38:15read now in our previous code we
  24188. 20:38:18actually had two steps the first step is
  24189. 20:38:21to call load products and then once this
  24190. 20:38:25is finished the second step is to call
  24191. 20:38:28render order summary and render payment
  24192. 20:38:31summary and in our promise we have the
  24193. 20:38:34same thing our first step is load
  24194. 20:38:37product
  24195. 20:38:38once this is finished we're going to
  24196. 20:38:40call resolve and then go to the Second
  24197. 20:38:42Step so we're just going to copy this
  24198. 20:38:45second step into the second step up here
  24199. 20:38:49so we'll select this code and then right
  24200. 20:38:52click
  24201. 20:38:54copy and in do then we're going to right
  24202. 20:38:58click and
  24203. 20:39:04paste and now let's save
  24204. 20:39:08and let's check the web page so we'll
  24205. 20:39:10open the project and you can see that
  24206. 20:39:13everything is loading
  24207. 20:39:18correctly So This Promise does the same
  24208. 20:39:20thing as a call back that we had before
  24209. 20:39:23both of them have two steps the first
  24210. 20:39:25step is to load the products and then we
  24211. 20:39:29wait for the products to finish loading
  24212. 20:39:32and then we do the Second Step which is
  24213. 20:39:34to render the page
  24214. 20:39:40so now you might be wondering why do we
  24215. 20:39:42use promises it looks like it's more
  24216. 20:39:45work and more code than just using a
  24217. 20:39:48callback so callbacks have a big problem
  24218. 20:39:51which is multiple callbacks cause a lot
  24219. 20:39:54of
  24220. 20:39:57nesting nesting means having code inside
  24221. 20:40:00code like
  24222. 20:40:02this to see why this is a problem let's
  24223. 20:40:05say that we want to load the cart from
  24224. 20:40:08the back end as well I actually set up a
  24225. 20:40:11URL path that we can use to practice
  24226. 20:40:14called SLC
  24227. 20:40:17cart so first let's go into products.
  24228. 20:40:21JS and we're going to copy load products
  24229. 20:40:24and change it to load the
  24230. 20:40:27cart so we'll select this
  24231. 20:40:30function and then
  24232. 20:40:32rightclick
  24233. 20:40:34copy and let's open cart. Js
  24234. 20:40:38since we're going to load the cart from
  24235. 20:40:39the back end and let's scroll down to
  24236. 20:40:42the
  24237. 20:40:46bottom and here we'll right click and
  24238. 20:40:50paste that
  24239. 20:40:53code and now instead of loading the
  24240. 20:40:56products let's load the cart from the
  24241. 20:40:59backend so first let's rename this
  24242. 20:41:01function to load Capital C cart
  24243. 20:41:07and the first thing we need to change is
  24244. 20:41:08the URL path so instead of loading SL
  24245. 20:41:12products we're going to load slash cart
  24246. 20:41:15from my
  24247. 20:41:16backend so normally when we load SL cart
  24248. 20:41:20the backend will give us a cart
  24249. 20:41:23array however since we're just
  24250. 20:41:25practicing my backend will just give us
  24251. 20:41:28some text when we load SL
  24252. 20:41:33cart next we don't need this products
  24253. 20:41:36code anymore so we can select it and
  24254. 20:41:40then delete
  24255. 20:41:41it and instead let's just console. log
  24256. 20:41:48brackets and we just display the
  24257. 20:41:50response that we get back xhr do
  24258. 20:41:56response and we can remove this other
  24259. 20:42:00console.log and
  24260. 20:42:05save so now let's use load cart in our
  24261. 20:42:08previous
  24262. 20:42:10example on the left we'll scroll down
  24263. 20:42:13and open checkout.
  24264. 20:42:15JS and at the top let's import load cart
  24265. 20:42:20so here we'll import curly brackets load
  24266. 20:42:25cart from a string and we'll go to the
  24267. 20:42:29data folder which is do
  24268. 20:42:32dotdata slash and we'll load C.J
  24269. 20:42:40and now we're going to use this in our
  24270. 20:42:42callback code to show the problem with
  24271. 20:42:46callbacks first let's comment out this
  24272. 20:42:49promise so we can focus on the Callback
  24273. 20:42:54code at the top we'll type
  24274. 20:42:57slashstar and here star
  24275. 20:43:00slash and we'll remove the comments for
  24276. 20:43:03the
  24277. 20:43:05Callback and after we load the products
  24278. 20:43:08we're also going to load the
  24279. 20:43:10cart so inside this function we're going
  24280. 20:43:13to type load cart brackets and we're
  24281. 20:43:17going to give it a
  24282. 20:43:22function so this function is going to
  24283. 20:43:24run after the cart has
  24284. 20:43:27loaded so now let's say that we want to
  24285. 20:43:29wait for the products and the cart to
  24286. 20:43:32load before we render the
  24287. 20:43:35page to do that we need to move this
  24288. 20:43:38code inside this inner
  24289. 20:43:42function so we'll select this and then
  24290. 20:43:45right click
  24291. 20:43:47cut inside load cart we're going to
  24292. 20:43:50right click and
  24293. 20:43:52paste and remove this extra
  24294. 20:43:56line so now this code is going to load
  24295. 20:43:59the products wait for it to finish and
  24296. 20:44:02then it's going to load the cart and
  24297. 20:44:05then wait for the cart to finish
  24298. 20:44:07and then it will render the page or
  24299. 20:44:09display the page so let's
  24300. 20:44:13save and check the
  24301. 20:44:16console and now you can see that we
  24302. 20:44:19loaded the products and then we loaded
  24303. 20:44:22the cart and if we open the
  24304. 20:44:25page everything was displayed
  24305. 20:44:29properly so now let's go back to our
  24306. 20:44:32code and notice that when we added
  24307. 20:44:35another call back we we created another
  24308. 20:44:38layer of
  24309. 20:44:39nesting so we have code inside code
  24310. 20:44:43inside
  24311. 20:44:45code the problem with this is that each
  24312. 20:44:48layer of nesting adds some indents at
  24313. 20:44:51the front or spaces at the
  24314. 20:44:54front this means if we have lots of
  24315. 20:44:57callbacks our code will become more and
  24316. 20:45:00more nested like
  24317. 20:45:02this this is a big problem with
  24318. 20:45:05callbacks multi multiple callbacks cause
  24319. 20:45:08a lot of nesting and all these indents
  24320. 20:45:11make our code hard to work
  24321. 20:45:16with the promises solve this problem by
  24322. 20:45:20letting us flatten our
  24323. 20:45:23code for example let's load the products
  24324. 20:45:27and the carts again but this time using
  24325. 20:45:29our promise
  24326. 20:45:31instead first let's comment out the
  24327. 20:45:33Callback using
  24328. 20:45:35slashstar and and at the bottom star
  24329. 20:45:38slash and we're going to uncomment the
  24330. 20:45:41promise so here we'll remove the
  24331. 20:45:48comments and then in the next
  24332. 20:45:51step instead of rendering the
  24333. 20:45:54page we're going to load the cart like
  24334. 20:45:58we did down here so we'll type load cart
  24335. 20:46:03and
  24336. 20:46:05brackets now we have a problem here we
  24337. 20:46:08want to wait for load card to finish and
  24338. 20:46:11then go to the next step however we
  24339. 20:46:15usually do this using resolve but we
  24340. 20:46:17don't have resolve inside this
  24341. 20:46:21function to solve this problem inside.
  24342. 20:46:25then we can actually return a new
  24343. 20:46:28promise so at the top we're going to
  24344. 20:46:32return a new promise and
  24345. 20:46:38brackets so we're going to give this
  24346. 20:46:40promise a function just like we did up
  24347. 20:46:45here so we'll type brackets arrow and
  24348. 20:46:48curly
  24349. 20:46:49brackets and this function gets one
  24350. 20:46:52parameter called
  24351. 20:46:56resolve so inside do then if we want to
  24352. 20:47:00use resolve to wait for some code to
  24353. 20:47:03finish we can return a new promise
  24354. 20:47:09so now let's move our code into this new
  24355. 20:47:11promise so we can use resolve so we'll
  24356. 20:47:14select this code and then right click
  24357. 20:47:19cut and inside here we're going to right
  24358. 20:47:23click and
  24359. 20:47:25paste and remove these new
  24360. 20:47:28lines and we can give load card a
  24361. 20:47:31function to run when it's finished so
  24362. 20:47:34we'll type brackets arrow and and curler
  24363. 20:47:38brackets once load card is finished it
  24364. 20:47:41runs this function and then inside here
  24365. 20:47:44we're going to call resolve so that we
  24366. 20:47:47move to the next step so here we'll call
  24367. 20:47:52resolve and
  24368. 20:47:55brackets so now this will go to the next
  24369. 20:47:59step and we currently don't have a next
  24370. 20:48:01step to This Promise so let's add The
  24371. 20:48:04Next Step at the end to add another step
  24372. 20:48:09we're going to use dot then
  24373. 20:48:12brackets and we'll give this a function
  24374. 20:48:16brackets arrow and curler
  24375. 20:48:20brackets so the third step is to render
  24376. 20:48:24the page so we're going to do the same
  24377. 20:48:26thing inside
  24378. 20:48:29here so let's select this code and we'll
  24379. 20:48:33rightclick copy
  24380. 20:48:37inside the third step we're going to
  24381. 20:48:39right click and
  24382. 20:48:42paste and adjust the formatting if you
  24383. 20:48:45need
  24384. 20:48:46to so that's how we do these three steps
  24385. 20:48:49in the Callback using a
  24386. 20:48:53promise we do the first step wait for it
  24387. 20:48:57to finish call resolve and then it goes
  24388. 20:49:00to the second step we do the Second Step
  24389. 20:49:03wait for it to finish call resolve and
  24390. 20:49:06and then we go to the third
  24391. 20:49:10step so we promise let us have as many
  24392. 20:49:13steps as we want and we can use resolve
  24393. 20:49:16to wait for each step to finish before
  24394. 20:49:19going to the next
  24395. 20:49:22step so let's save this and check the
  24396. 20:49:26console to make sure it worked so we'll
  24397. 20:49:29open the
  24398. 20:49:30console and inside here we have the same
  24399. 20:49:33messages as last time first we load the
  24400. 20:49:37products we wait for it to finish and
  24401. 20:49:40then go to the next step then we load
  24402. 20:49:42the
  24403. 20:49:43cart wait for it to finish and then go
  24404. 20:49:46to the next step and then we render or
  24405. 20:49:49display the page if we open the page
  24406. 20:49:53you'll see that everything displayed
  24407. 20:49:57properly so now let's go back and
  24408. 20:49:59compare promises and
  24409. 20:50:02callbacks so even though promises
  24410. 20:50:05require a bunch of setup code you can
  24411. 20:50:08see that it keeps our code relatively
  24412. 20:50:11flat the third step doesn't get more and
  24413. 20:50:14more nested like in
  24414. 20:50:19callbacks so if we have lots of steps in
  24415. 20:50:22our promise this is what the code will
  24416. 20:50:24look like you can see that even when we
  24417. 20:50:27add more steps the code does not become
  24418. 20:50:31more and more
  24419. 20:50:32nested so promises help keep our code
  24420. 20:50:35flat
  24421. 20:50:37and avoid too much
  24422. 20:50:39nesting so that's why it's recommended
  24423. 20:50:42to use promises instead of callbacks
  24424. 20:50:46promises will keep our code more
  24425. 20:50:52flat so let's actually add some new
  24426. 20:50:55lines here as well as
  24427. 20:50:58here to better separate these steps so
  24428. 20:51:01it's a little easier to read
  24429. 20:51:07lastly we're going to learn two more
  24430. 20:51:09features of
  24431. 20:51:10promises the first feature is we can
  24432. 20:51:13give resolve a value so inside here
  24433. 20:51:17let's give it a value like the string
  24434. 20:51:21value
  24435. 20:51:23one so whatever we give to resolve is
  24436. 20:51:26going to be saved in a parameter inside
  24437. 20:51:29doth so inside this function we can add
  24438. 20:51:32a parameter called value
  24439. 20:51:36and now whatever we give to resolve is
  24440. 20:51:39going to be saved inside here and now we
  24441. 20:51:42can use
  24442. 20:51:43it so this lets us share a value between
  24443. 20:51:47two steps of a
  24444. 20:51:50promise so in here let's check if this
  24445. 20:51:53worked using
  24446. 20:51:57console.log
  24447. 20:51:59value and
  24448. 20:52:01save and if we look inside our
  24449. 20:52:04console you'll see that it displayed
  24450. 20:52:07value
  24451. 20:52:08one so whatever we give to resolve will
  24452. 20:52:11be saved in this
  24453. 20:52:15parameter the second feature is we can
  24454. 20:52:18run multiple Promises at the same time
  24455. 20:52:22currently we wait for things to load one
  24456. 20:52:25at a time for example we wait for the
  24457. 20:52:29products to load and then we wait for
  24458. 20:52:31the cart to load and then we go to the
  24459. 20:52:34next step to make this more efficient we
  24460. 20:52:37can actually load the products and the
  24461. 20:52:40cart at the same
  24462. 20:52:42time to do that we can use a feature
  24463. 20:52:45called
  24464. 20:52:47promise.all promise.all lets us run
  24465. 20:52:50multiple Promises at the same time and
  24466. 20:52:53wait for all of them to
  24467. 20:52:57finish so let's scroll to the top and
  24468. 20:53:01we'll do an
  24469. 20:53:02example so here we'll type the code
  24470. 20:53:07capital P
  24471. 20:53:10promise.all and
  24472. 20:53:13brackets inside these brackets we're
  24473. 20:53:16going to give this an
  24474. 20:53:18array inside this array we can give it
  24475. 20:53:22multiple promises to wait
  24476. 20:53:25for so let's just copy paste the two
  24477. 20:53:27promises that we have here into this
  24478. 20:53:31array so we'll start from here at the
  24479. 20:53:34end of the first promise and we'll
  24480. 20:53:36select this code right click
  24481. 20:53:41copy and in this array we're going to
  24482. 20:53:44right click and
  24483. 20:53:48paste and remove this line so we have
  24484. 20:53:51our first promise and at the end we're
  24485. 20:53:54going to add a comma and we're going to
  24486. 20:53:56give it a second promise so we'll scroll
  24487. 20:53:59down to our second promise here and
  24488. 20:54:02we're going to select this code starting
  24489. 20:54:05from here
  24490. 20:54:06and select the promise right click
  24491. 20:54:11copy and at the top we're going to right
  24492. 20:54:15click and
  24493. 20:54:19paste so we basically created an array
  24494. 20:54:22of promises and then we're going to give
  24495. 20:54:25this array to
  24496. 20:54:26promise.all and it's going to wait for
  24497. 20:54:29all of the promises to finish before
  24498. 20:54:31going to the next
  24499. 20:54:33step so now let's add a next
  24500. 20:54:36Step at the end to add a Next Step we're
  24501. 20:54:40going to use dot then and brackets and
  24502. 20:54:45we'll add a new line to make it a little
  24503. 20:54:47easier to
  24504. 20:54:49read so as usual dot then adds a next
  24505. 20:54:53step to a
  24506. 20:54:55promise it will give it a function to
  24507. 20:54:58run so brackets arrow and curling
  24508. 20:55:03brackets after we load the products and
  24509. 20:55:06load the cart we're going to render the
  24510. 20:55:09page so let's copy paste that code
  24511. 20:55:12inside this next step so we'll scroll
  24512. 20:55:15down to the render code and we'll select
  24513. 20:55:19it right click
  24514. 20:55:22copy and at the top inside then we'll
  24515. 20:55:26right click and
  24516. 20:55:30paste so now let's comment out our old
  24517. 20:55:33promise and try out promise do
  24518. 20:55:38all above our old promise we're going to
  24519. 20:55:41type
  24520. 20:55:42slashstar and below it we're going to
  24521. 20:55:45type star
  24522. 20:55:47slash and let's
  24523. 20:55:51save we'll go to our
  24524. 20:55:54console and you can see that it did the
  24525. 20:55:56same thing it loaded the products and it
  24526. 20:55:59loaded the cart and it's going to wait
  24527. 20:56:01for both of them to call resolve before
  24528. 20:56:04going to the next step which is to
  24529. 20:56:07render or display the page if we open
  24530. 20:56:10the page you'll see that everything
  24531. 20:56:12displayed
  24532. 20:56:14properly so that's
  24533. 20:56:17promise.all we can give it an array of
  24534. 20:56:20promises and it will run both of them at
  24535. 20:56:23the same time instead of waiting for
  24536. 20:56:25each promise one by
  24537. 20:56:28one also the values that we give to
  24538. 20:56:32resolve are saved in a parameter so
  24539. 20:56:35inside do then we can add a parameter
  24540. 20:56:38called
  24541. 20:56:40values and then these values that we
  24542. 20:56:43give to resolve are going to be saved
  24543. 20:56:45inside here so if we
  24544. 20:56:49console.log
  24545. 20:56:51values and
  24546. 20:56:53save and check the
  24547. 20:56:57console and it gives us an array of
  24548. 20:56:59values the first value is from the first
  24549. 20:57:03promise and the second value is from the
  24550. 20:57:06the second promise so here we didn't
  24551. 20:57:09give anything to resolve so the second
  24552. 20:57:11value is
  24553. 20:57:14undefined so that's an overview of
  24554. 20:57:16promises and
  24555. 20:57:18promise.all promises are a better way to
  24556. 20:57:21wait for asynchronous code to finish
  24557. 20:57:24versus
  24558. 20:57:26callbacks because they help us avoid
  24559. 20:57:28nesting and keep our code relatively
  24560. 20:57:32flat promises also have more features
  24561. 20:57:35like promise all which lets us run
  24562. 20:57:38multiple Promises at the same
  24563. 20:57:41time now let's save our changes into git
  24564. 20:57:45we're going to open the git
  24565. 20:57:47section inside cart.
  24566. 20:57:50JS we added a function to practice
  24567. 20:57:53loading the cart from the back
  24568. 20:57:55end and then in checkout.
  24569. 20:57:58JS we learn a promises and converted our
  24570. 20:58:02code from a callback to a promise
  24571. 20:58:06let's type a message describing our
  24572. 20:58:09changes learn
  24573. 20:58:12promises and convert code to promises
  24574. 20:58:19and
  24575. 20:58:26commit now that we learned promises
  24576. 20:58:29we're going to learn another feature
  24577. 20:58:30called
  24578. 20:58:32fetch fetch is a better way to make htd
  24579. 20:58:36PP
  24580. 20:58:39requests let's go back to our
  24581. 20:58:41code and we're going to open products.
  24582. 20:58:45JS and we're going to scroll down to the
  24583. 20:58:48load products
  24584. 20:58:50function currently we're using XML HTTP
  24585. 20:58:54request to make requests to the back end
  24586. 20:58:58and this uses a call
  24587. 20:59:00back fetch also lets us make requests to
  24588. 20:59:04the backend but fetch uses a
  24589. 20:59:08promise so let's do an example and we'll
  24590. 20:59:11make the same request except using fetch
  24591. 20:59:14this
  24592. 20:59:15time we'll scroll up and above load
  24593. 20:59:19products let's create another function
  24594. 20:59:22using
  24595. 20:59:23function and we'll name it load products
  24596. 20:59:29Fetch and brackets and curly
  24597. 20:59:33brackets and to use fetch we're going to
  24598. 20:59:36use the built-in function Fetch and
  24599. 20:59:41brackets so this makes an HTTP request
  24600. 20:59:45just like we did down here so let's make
  24601. 20:59:48the same request as we did in this
  24602. 20:59:53function at the bottom we made a get
  24603. 20:59:56request to this
  24604. 20:59:59URL so by default fetch will make a get
  24605. 21:00:03request so we just need to give it the
  24606. 21:00:05the URL that we want to make the request
  24607. 21:00:09to so at the bottom let's select this
  24608. 21:00:13URL and we're just going to
  24609. 21:00:16rightclick
  24610. 21:00:18copy and at the top inside fetch we're
  24611. 21:00:22going to right click and
  24612. 21:00:26paste so this will make the exact same
  24613. 21:00:28request as we did
  24614. 21:00:31below so you can see that fetch is a lot
  24615. 21:00:34more simple than XML HTTP request we
  24616. 21:00:38don't have to create a new object or do
  24617. 21:00:41any setup we can just call Fetch and
  24618. 21:00:44tell it where we want the request to
  24619. 21:00:48go so this will send a request to the
  24620. 21:00:51back end and now how do we get the
  24621. 21:00:54response so instead of using a call back
  24622. 21:00:57to get the response fetch uses a
  24623. 21:01:01promise when we call fetch this is going
  24624. 21:01:05to create a promise and we can add a
  24625. 21:01:08next step to This Promise using dot then
  24626. 21:01:13and
  24627. 21:01:14brackets so we'll give dot then a
  24628. 21:01:17function brackets arrow and curly
  24629. 21:01:22brackets so the way this works is that
  24630. 21:01:24fetch is going to send a request to the
  24631. 21:01:26backend when we get a response it's
  24632. 21:01:29going to go to the next
  24633. 21:01:32step it's also going to save the
  24634. 21:01:34response inside inside a parameter in
  24635. 21:01:37here so we'll add a parameter called
  24636. 21:01:41response and this will contain the
  24637. 21:01:43response from the back
  24638. 21:01:45end so instead of using a call back to
  24639. 21:01:49wait for the response fetch uses a
  24640. 21:01:52promise to wait for the
  24641. 21:01:54response so here let's
  24642. 21:01:58console.log the response to see what it
  24643. 21:02:00looks
  24644. 21:02:03like and now let's run this code
  24645. 21:02:06at the
  24646. 21:02:07bottom we'll run this function using
  24647. 21:02:11load products Fetch and
  24648. 21:02:15brackets so now let's
  24649. 21:02:18save and take a look at the
  24650. 21:02:22console so here's the response that we
  24651. 21:02:25get from Fetch if we open it you can see
  24652. 21:02:29it gives us a bunch of information about
  24653. 21:02:31the response like the status or whether
  24654. 21:02:34it succeeded or
  24655. 21:02:36failed as well as the URL that we made
  24656. 21:02:39the request
  24657. 21:02:41to so how do we get the data for the
  24658. 21:02:44products from this
  24659. 21:02:46response to do that we'll go back to our
  24660. 21:02:50code and to get data that's attached to
  24661. 21:02:53this
  24662. 21:02:54response we're going to use
  24663. 21:02:58response. Json and
  24664. 21:03:02brackets so this gives us the Json or
  24665. 21:03:05the data attached to the response in
  24666. 21:03:08this case it's our products
  24667. 21:03:12data next response. Jon is actually
  24668. 21:03:17asynchronous it returns a
  24669. 21:03:20promise so we need to wait for this
  24670. 21:03:23promise to finish before we continue to
  24671. 21:03:25the next
  24672. 21:03:28step to do that inside. then we can
  24673. 21:03:32actually return another promise so here
  24674. 21:03:36we're going to return this
  24675. 21:03:40promise when we return a promise it will
  24676. 21:03:44wait for this promise to finish before
  24677. 21:03:46going to the next
  24678. 21:03:48step so now let's add another step to
  24679. 21:03:51This Promise using dot then brackets and
  24680. 21:03:56we'll give this a function again so
  24681. 21:03:59brackets arrow and curly
  24682. 21:04:03brackets when response. Json finishes
  24683. 21:04:06it's going to give us the data that's
  24684. 21:04:08attached to the response and it's going
  24685. 21:04:10to save it inside this
  24686. 21:04:14parameter so this response should
  24687. 21:04:16contain our products data so let's name
  24688. 21:04:19this
  24689. 21:04:20parameter products data and let's
  24690. 21:04:24console.log this to see what it looks
  24691. 21:04:26like so here we
  24692. 21:04:29console. log the
  24693. 21:04:32products
  24694. 21:04:34data and and
  24695. 21:04:36save and let's open the
  24696. 21:04:40console and you see here that the
  24697. 21:04:43products data is the array of products
  24698. 21:04:46that we're going to use in our Amazon
  24699. 21:04:49Project you'll also notice that instead
  24700. 21:04:52of giving us just a big Json string like
  24701. 21:04:55before it actually converted the Json
  24702. 21:04:58into an array so basically it did json.
  24703. 21:05:02pars on the response for us so this this
  24704. 21:05:05saved us a little bit of
  24705. 21:05:08code finally now that we have the
  24706. 21:05:10products array let's do the same thing
  24707. 21:05:13that we did down here so we'll scroll
  24708. 21:05:16down and select all of this
  24709. 21:05:19code and we'll right
  24710. 21:05:21click
  24711. 21:05:23copy and then up
  24712. 21:05:26here we'll remove
  24713. 21:05:28this and rightclick and
  24714. 21:05:32paste so we'll do the same steps as
  24715. 21:05:35before
  24716. 21:05:36except using a promise this
  24717. 21:05:38time first instead of doing json. pars
  24718. 21:05:42on the response we already have the
  24719. 21:05:44array of products so we can remove this
  24720. 21:05:49and replace it with products
  24721. 21:05:53data and then we're still going to
  24722. 21:05:55convert this from objects to classes
  24723. 21:05:58like before we'll keep this console.log
  24724. 21:06:02so we can track the progress
  24725. 21:06:06this last line of code let us do
  24726. 21:06:09something after we finish loading the
  24727. 21:06:12products however instead of using a call
  24728. 21:06:15back like before we're going to remove
  24729. 21:06:18this
  24730. 21:06:19line and to do something after this step
  24731. 21:06:23we can actually return this entire
  24732. 21:06:27promise so to make it easier to read
  24733. 21:06:30let's save this entire promise into a
  24734. 21:06:33variable using const
  24735. 21:06:36let's call it promise equals
  24736. 21:06:41fetch so let's add some new lines here
  24737. 21:06:45as well as here to make the code a
  24738. 21:06:48little easier to
  24739. 21:06:50read so we're basically calling fetch
  24740. 21:06:53which creates a promise and then we're
  24741. 21:06:56going to save this promise inside this
  24742. 21:07:01variable and then at the end of this
  24743. 21:07:04function we can just
  24744. 21:07:06return the promise so we're just going
  24745. 21:07:08to return this whole promise out of the
  24746. 21:07:12function and then we can add even more
  24747. 21:07:14steps after this
  24748. 21:07:17promise so down here this is going to
  24749. 21:07:21give us the promise and then we can
  24750. 21:07:23attach another step using dot then
  24751. 21:07:28brackets and inside here we can give it
  24752. 21:07:31a
  24753. 21:07:31function brackets arrow and curly
  24754. 21:07:35brackets
  24755. 21:07:39so this attaches another step to the end
  24756. 21:07:42of This Promise so basically attaches
  24757. 21:07:44another step
  24758. 21:07:46here so after we load all the products
  24759. 21:07:49let's just
  24760. 21:07:52console.log next
  24761. 21:07:54step and
  24762. 21:07:57save so we'll check the
  24763. 21:08:00console and we'll see that we loaded the
  24764. 21:08:03products and then after we did The Next
  24765. 21:08:10Step so we can actually return a promise
  24766. 21:08:14out of a function and then keep
  24767. 21:08:16attaching more steps to that
  24768. 21:08:19promise so that's how we use fetch it's
  24769. 21:08:23a better way to make HTTP requests
  24770. 21:08:26because it uses promises
  24771. 21:08:31directly so now let's use fetch in our
  24772. 21:08:34project instead of using XML HTTP
  24773. 21:08:38request so first let's comment out this
  24774. 21:08:41code because we were just practicing
  24775. 21:08:43we'll type
  24776. 21:08:44slashstar and at the bottom star
  24777. 21:08:48slash and let's scroll up to load
  24778. 21:08:52products Fetch and we're going to export
  24779. 21:08:54this
  24780. 21:08:55function so we can use it outside this
  24781. 21:08:59file let's
  24782. 21:09:02save we'll go to the left
  24783. 21:09:05and open checkout.
  24784. 21:09:08JS and here instead of using load
  24785. 21:09:11products which uses callbacks let's
  24786. 21:09:14switch to load products fetch which uses
  24787. 21:09:18promises we'll scroll
  24788. 21:09:20up and we're going to import load
  24789. 21:09:22products fetch so here we're already
  24790. 21:09:25importing from products. JS we'll type a
  24791. 21:09:28comma load products
  24792. 21:09:33Fetch and now will replace load products
  24793. 21:09:37so remember that we need to give this a
  24794. 21:09:40promise and also remember that fetch
  24795. 21:09:43returns a promise so all we need to do
  24796. 21:09:47is to remove this code and just call
  24797. 21:09:51load products Fetch and
  24798. 21:09:56brackets this will return a promise and
  24799. 21:10:00we can use it with
  24800. 21:10:02promise.all so now let's save
  24801. 21:10:05we'll check our
  24802. 21:10:07website and you'll see that everything
  24803. 21:10:09is still
  24804. 21:10:11working so fetch helps us make our code
  24805. 21:10:14a lot cleaner because it can return a
  24806. 21:10:16promise directly and we don't have to
  24807. 21:10:18type out all of this extra
  24808. 21:10:22code finally let's use fetch inside our
  24809. 21:10:26tests on the left we'll open order
  24810. 21:10:30summary
  24811. 21:10:32test.js and remember that inside here we
  24812. 21:10:35used load products so let's switch this
  24813. 21:10:38to using load products fetch instead
  24814. 21:10:42first we'll import it at the top we're
  24815. 21:10:45importing from products. JS so we'll
  24816. 21:10:48type comma and import load products
  24817. 21:10:54Fetch and instead of load products we're
  24818. 21:10:57going to call load products
  24819. 21:11:02Fetch and brackets
  24820. 21:11:06so remember that this returns a promise
  24821. 21:11:09and we can attach more steps to This
  24822. 21:11:11Promise using the method dot then and
  24823. 21:11:16brackets and we'll give then a function
  24824. 21:11:19to run so brackets arrow and curly
  24825. 21:11:25brackets once the products are finished
  24826. 21:11:27loading it's going to run then and
  24827. 21:11:31inside here we're going to call done to
  24828. 21:11:34go to the next step of the test so here
  24829. 21:11:38we'll just call done and
  24830. 21:11:41brackets and we'll remove the old
  24831. 21:11:44code so now let's save to make sure this
  24832. 21:11:47is
  24833. 21:11:48working and we'll open the tests go to
  24834. 21:11:51the browser and open the
  24835. 21:11:55test and the order summary test should
  24836. 21:11:57be
  24837. 21:11:59passing so now let's save our changes
  24838. 21:12:02into git we'll open the git set
  24839. 21:12:06section inside products. JS we learned
  24840. 21:12:09how to use fetch which returns a promise
  24841. 21:12:13directly inside checkout. JS we used
  24842. 21:12:17load products fetch inside our project
  24843. 21:12:21and in order summary
  24844. 21:12:22test.js we use load products fetch
  24845. 21:12:26inside our tests let's create a message
  24846. 21:12:29describing these
  24847. 21:12:30changes convert load products
  24848. 21:12:35from xhr or XML HTTP request to fetch
  24849. 21:12:43and
  24850. 21:12:49commit next we're going to learn async
  24851. 21:12:52await which is an even better way to
  24852. 21:12:55handle asynchronous
  24853. 21:12:58code let's go back to our
  24854. 21:13:01code and we'll open checkout. Js
  24855. 21:13:06currently we're using promises to handle
  24856. 21:13:09asynchronous code but the problem with
  24857. 21:13:11promises is that it creates a lot of
  24858. 21:13:14extra code for example we need to create
  24859. 21:13:17a new promise we need to use resolve and
  24860. 21:13:20we need to use do
  24861. 21:13:23that async await is a shortcut for
  24862. 21:13:26promises and it removes all this extra
  24863. 21:13:30code so let's do our first example of
  24864. 21:13:33asyn a weit we're going to scroll
  24865. 21:13:38up and at the top we're going to create
  24866. 21:13:41a function using
  24867. 21:13:44function and let's name it load page
  24868. 21:13:48brackets and curly
  24869. 21:13:51brackets and inside we're going to
  24870. 21:13:55console.log the string load
  24871. 21:14:00page and now we'll learn the first part
  24872. 21:14:03of async await which is
  24873. 21:14:06async in front of the word function
  24874. 21:14:09we're going to add the word async and
  24875. 21:14:13space so async makes a function return a
  24876. 21:14:18promise so this code is a shortcut for
  24877. 21:14:21this code it basically wraps the code in
  24878. 21:14:25a
  24879. 21:14:28promise so now let's run this function
  24880. 21:14:31to show that it actually returns a
  24881. 21:14:33promise
  24882. 21:14:35So Below this we'll call load page and
  24883. 21:14:39brackets and because this returns a
  24884. 21:14:42promise we can add a next step to This
  24885. 21:14:45Promise using dot then
  24886. 21:14:50brackets and we'll give then a function
  24887. 21:14:54brackets arrow and curly
  24888. 21:14:56brackets and inside here let's just
  24889. 21:15:01console.log the string Next Step
  24890. 21:15:06now let's
  24891. 21:15:08save and we'll check the console so
  24892. 21:15:11we'll open the
  24893. 21:15:12console and you'll see that we loaded
  24894. 21:15:15the page and then in the next step we
  24895. 21:15:19displayed Next Step so when we put a
  24896. 21:15:23sync in front of function it makes this
  24897. 21:15:25function return a
  24898. 21:15:28promise this code is a shortcut for this
  24899. 21:15:31code
  24900. 21:15:36next if we return something from this
  24901. 21:15:38function for example we're going to
  24902. 21:15:41return a value like the string value
  24903. 21:15:46two this gets converted into resolve
  24904. 21:15:50value
  24905. 21:15:54two so this value is going to be saved
  24906. 21:15:57in a parameter in The Next Step so here
  24907. 21:16:01we'll add a parameter value and value
  24908. 21:16:05two is going to be saved inside here if
  24909. 21:16:09we
  24910. 21:16:10console.log the value and
  24911. 21:16:14save and then check the
  24912. 21:16:17console we'll see that value is equal to
  24913. 21:16:21this
  24914. 21:16:23string and that's it for async it just
  24915. 21:16:27makes a function return a
  24916. 21:16:30promise now you might be wondering
  24917. 21:16:33what's the point of this feature
  24918. 21:16:36so the reason we use async is because it
  24919. 21:16:38lets us use the second feature called
  24920. 21:16:44await await lets us wait for a promise
  24921. 21:16:48to finish before going to the next
  24922. 21:16:53line so let's do an example inside here
  24923. 21:16:58let's run some code that creates a
  24924. 21:17:02promise let's run load products fetch so
  24925. 21:17:06we'll type load products Fetch and
  24926. 21:17:12brackets so this loads the products from
  24927. 21:17:15the back end and usually this returns a
  24928. 21:17:19promise so one way to wait for this to
  24929. 21:17:22finish is to use do then and then give
  24930. 21:17:26it a
  24931. 21:17:27function so this runs this function
  24932. 21:17:31after load products has finished
  24933. 21:17:35now await gives us another way to wait
  24934. 21:17:38for this promise to finish instead of
  24935. 21:17:41using
  24936. 21:17:43then at the front we can just type
  24937. 21:17:50await so await lets us write
  24938. 21:17:53asynchronous code like normal code so
  24939. 21:17:57now it will wait for this line to finish
  24940. 21:18:00and get the response from the back end
  24941. 21:18:03before going to the next next line so we
  24942. 21:18:06don't have to do any more do then we
  24943. 21:18:09just write it like normal code line by
  24944. 21:18:13line this code is a shortcut for this
  24945. 21:18:21code and this makes our code a lot
  24946. 21:18:24easier to read than what we have down
  24947. 21:18:28here so now let's see what this looks
  24948. 21:18:31like in the console before we save let's
  24949. 21:18:35comment out this promise at the bottom
  24950. 21:18:37so we don't get
  24951. 21:18:39confused so we'll type
  24952. 21:18:42slashstar and at the bottom star
  24953. 21:18:46slash and now we're going to save this
  24954. 21:18:49code and check the console to see what
  24955. 21:18:52it
  24956. 21:18:53did so you see that we loaded the page
  24957. 21:18:57and then notice that it finish loading
  24958. 21:18:59the products before going to the next
  24959. 21:19:02step so this actually Waits for this
  24960. 21:19:05line to finish before going to the next
  24961. 21:19:08line in the code and this makes our code
  24962. 21:19:10a lot
  24963. 21:19:13cleaner now one very important thing to
  24964. 21:19:15know is that we can only use a weight
  24965. 21:19:19when we're inside an async
  24966. 21:19:22function that's actually the main reason
  24967. 21:19:24we use async so that we can use a weight
  24968. 21:19:27to make our code
  24969. 21:19:32cleaner now that we learned asyn wait
  24970. 21:19:35let's use it in our
  24971. 21:19:38project so previously we loaded the
  24972. 21:19:41products and then we loaded the cart and
  24973. 21:19:44then we rendered the page so let's
  24974. 21:19:47recreate this using async
  24975. 21:19:50await first we want to load the products
  24976. 21:19:54which we did here and we're going to
  24977. 21:19:56wait for this to finish and then we're
  24978. 21:19:58going to load the
  24979. 21:20:01cart so to load the cart let's use this
  24980. 21:20:04promise down here asyn await can only be
  24981. 21:20:08used with promises it doesn't do
  24982. 21:20:10anything with a call
  24983. 21:20:14back so we'll select this promise and
  24984. 21:20:18rightclick
  24985. 21:20:20copy and then up here we're going to
  24986. 21:20:23load the cart with a promise by right
  24987. 21:20:26clicking and
  24988. 21:20:29paste so now we want to wait for this
  24989. 21:20:32promise to finish before going to the
  24990. 21:20:34next line to do that at the front we can
  24991. 21:20:38simply type
  24992. 21:20:43await finally after we load the products
  24993. 21:20:47and we load the cart we're going to
  24994. 21:20:49render the page so we'll scroll down and
  24995. 21:20:53we'll select this
  24996. 21:20:56code and we're going to right click
  24997. 21:21:00copy and at the top
  24998. 21:21:05we're going to rightclick and
  24999. 21:21:10paste and that's it you can see that our
  25000. 21:21:13asynchronous code is a lot cleaner now
  25001. 21:21:16we load the products and then we just
  25002. 21:21:19wait for it to finish then we load the
  25003. 21:21:21cart and we also wait for it to finish
  25004. 21:21:24and then we run the rest of our code so
  25005. 21:21:28we can write asynchronous code like
  25006. 21:21:30normal code
  25007. 21:21:35so now let's remove the console.log at
  25008. 21:21:38the
  25009. 21:21:38top and also remove this return value
  25010. 21:21:42because we don't need
  25011. 21:21:43it and we'll remove do then at the
  25012. 21:21:50end and let's
  25013. 21:21:53save so now if we open our
  25014. 21:21:56website and open the
  25015. 21:22:01project you can see that everything
  25016. 21:22:03loaded properly
  25017. 21:22:06inside the
  25018. 21:22:07console you can see that we loaded the
  25019. 21:22:10products first wait for it to finish
  25020. 21:22:13then load the cart wait for it to finish
  25021. 21:22:17and then we displayed the page so that's
  25022. 21:22:20how we use a syn8 to make our code a lot
  25023. 21:22:23cleaner than what we had
  25024. 21:22:29before so now we'll learn some more
  25025. 21:22:32details about Ayn a weit
  25026. 21:22:35as I mentioned we can only use a weight
  25027. 21:22:37inside an async
  25028. 21:22:41function also the closest function has
  25029. 21:22:44to be
  25030. 21:22:46async for example here we have an async
  25031. 21:22:49function at the top and inside we have a
  25032. 21:22:52normal function so we can't use a weight
  25033. 21:22:55inside the normal function we have to
  25034. 21:22:57make this one
  25035. 21:23:03async another another detail is if a
  25036. 21:23:06promise resolves with a value for
  25037. 21:23:09example here we give resolve a value
  25038. 21:23:12like the string value
  25039. 21:23:16three normally this value will be saved
  25040. 21:23:19in The Next Step so if we did do then
  25041. 21:23:24and gave it a
  25042. 21:23:27function this value would be saved in
  25043. 21:23:30this
  25044. 21:23:32parameter however if we use await this
  25045. 21:23:36value just gets returned and we can save
  25046. 21:23:38it in a variable so instead of using
  25047. 21:23:42then we can Simply Save whatever is
  25048. 21:23:45resolved inside a variable using const
  25049. 21:23:49value
  25050. 21:23:52equals and this makes our code a lot
  25051. 21:23:55easier to read instead of having to use
  25052. 21:23:58then we can simply save this inside a
  25053. 21:24:01variable like normal code
  25054. 21:24:06and finally we can use await with
  25055. 21:24:09promise.all as well but I'll leave that
  25056. 21:24:12as an
  25057. 21:24:14exercise so that's async await it's a
  25058. 21:24:17shortcut for promises and it lets us
  25059. 21:24:20write asynchronous code like normal
  25060. 21:24:26code so when working with asynchronous
  25061. 21:24:28code a best practice is to use asyn
  25062. 21:24:32await over promises and callbacks
  25063. 21:24:35because they are much
  25064. 21:24:38cleaner so now let's save our changes
  25065. 21:24:40into git we'll open the git section in
  25066. 21:24:44checkout. JS we created the load page
  25067. 21:24:48function where we learned async and
  25068. 21:24:51await let's create a message describing
  25069. 21:24:54these
  25070. 21:24:56changes convert from promises to async
  25071. 21:25:03await and
  25072. 21:25:11commit now we're going to learn about
  25073. 21:25:13error handling when we're sending HTTP
  25074. 21:25:17requests across the internet we could
  25075. 21:25:19get unexpected errors for example the
  25076. 21:25:23internet connection might stop working
  25077. 21:25:26so we're going to learn how to handle
  25078. 21:25:27these kinds of unexpected
  25079. 21:25:31errors let's go back to our code and
  25080. 21:25:34we're going to open data/ products.
  25081. 21:25:39JS and we're going to
  25082. 21:25:41scroll to the load products
  25083. 21:25:46function so this uses a call back to
  25084. 21:25:49send a request and get a
  25085. 21:25:53response now what if there's an error
  25086. 21:25:55along the way for callbacks we usually
  25087. 21:25:58set up a separate callback just for
  25088. 21:26:03errors for example below this we're
  25089. 21:26:06going to add another event listener
  25090. 21:26:09using xhr do add event listener
  25091. 21:26:16brackets and the first parameter is the
  25092. 21:26:20event this time we're going to listen
  25093. 21:26:22for the error
  25094. 21:26:25event and then we're going to give it a
  25095. 21:26:31function so now if we get an unexpected
  25096. 21:26:35error it's going to run this function so
  25097. 21:26:38if we get an error let's just console
  25098. 21:26:40log a message
  25099. 21:26:44console.log a string
  25100. 21:26:47unexpected
  25101. 21:26:50error and usually when we get an error
  25102. 21:26:53we give some instructions on what to
  25103. 21:26:56do here let's just type please try again
  25104. 21:27:01later
  25105. 21:27:04and
  25106. 21:27:06save so this is called error handling if
  25107. 21:27:10we get an error during the request we
  25108. 21:27:12can handle it with this function so we
  25109. 21:27:15just display a
  25110. 21:27:17message so now let's try out this code
  25111. 21:27:20to simulate an error we're going to
  25112. 21:27:22change the url of the
  25113. 21:27:25request to
  25114. 21:27:28error. superimple backend. deev so this
  25115. 21:27:32will create an error because this URL
  25116. 21:27:35doesn't
  25117. 21:27:37exist so now down here let's call this
  25118. 21:27:40function so that we run this code and
  25119. 21:27:43get the error so we'll type load
  25120. 21:27:47products and
  25121. 21:27:50brackets and
  25122. 21:27:53save let's check the
  25123. 21:27:57console and you'll see that we displayed
  25124. 21:28:00this error
  25125. 21:28:02message so we got an error and it ran
  25126. 21:28:06this error function and it did not run
  25127. 21:28:09the load function
  25128. 21:28:13above also usually this callback gets a
  25129. 21:28:17parameter called error and this contains
  25130. 21:28:21some information about the
  25131. 21:28:24error so that's how we handle errors
  25132. 21:28:27when using callbacks we usually set up a
  25133. 21:28:30separate callback just for errors
  25134. 21:28:35so now let's remove this line since it
  25135. 21:28:37was just for
  25136. 21:28:38practice and we'll remove error from the
  25137. 21:28:42URL and change it back to super simple
  25138. 21:28:45backend dodev and
  25139. 21:28:51save next we'll learn how to handle
  25140. 21:28:53errors in
  25141. 21:28:55promises so we'll scroll up to the load
  25142. 21:28:59products fetch
  25143. 21:29:00function and this makes the same request
  25144. 21:29:03as below
  25145. 21:29:04but it uses
  25146. 21:29:06promises to handle an error in promises
  25147. 21:29:10promises give us two methods do then and
  25148. 21:29:14another method called dot catch brackets
  25149. 21:29:19and we're going to give catch a function
  25150. 21:29:22brackets arrow and curly
  25151. 21:29:27brackets so now if this request has an
  25152. 21:29:30error it's going to go to this catch
  25153. 21:29:32method and it's going to run this error
  25154. 21:29:35function so inside here let's also
  25155. 21:29:38display a message so let's just copy the
  25156. 21:29:41message that we had
  25157. 21:29:43below we're going to select this message
  25158. 21:29:46and then
  25159. 21:29:47rightclick
  25160. 21:29:49copy and then inside catch we're going
  25161. 21:29:53to right click and
  25162. 21:29:57paste and again to simulate an error
  25163. 21:30:01we're going to scroll up and change the
  25164. 21:30:03url
  25165. 21:30:04so we're going to add error at the front
  25166. 21:30:08do superimple backend
  25167. 21:30:11dodev so now let's call this function
  25168. 21:30:14and test out this error code at the
  25169. 21:30:19bottom we'll type load products Fetch
  25170. 21:30:25and
  25171. 21:30:26brackets and
  25172. 21:30:29save so now let's open the
  25173. 21:30:32console and you'll see in here that we
  25174. 21:30:34displayed the error message so we're
  25175. 21:30:37successfully creating an error it's
  25176. 21:30:40going inside catch and it displays this
  25177. 21:30:45message so just like callbacks catch
  25178. 21:30:49gets a parameter called error and this
  25179. 21:30:52contains information about the error if
  25180. 21:30:55we need
  25181. 21:30:57it so that's how we handle errors with
  25182. 21:31:00promises we have another method called
  25183. 21:31:03Dot catch that will run this function if
  25184. 21:31:06there's an
  25185. 21:31:07error so now let's remove this practice
  25186. 21:31:12code and switch back the
  25187. 21:31:15URL so remove error and Dot and
  25188. 21:31:23save lastly we'll learn how to handle
  25189. 21:31:25errors in async
  25190. 21:31:27await so let's go to the left and we're
  25191. 21:31:30going to open the checkout file so we'll
  25192. 21:31:32scroll down and open checkout.
  25193. 21:31:36JS and inside here we're using async
  25194. 21:31:40await so the catch errors when using
  25195. 21:31:42async await we're going to use some new
  25196. 21:31:45syntax called try
  25197. 21:31:48catch above this we're going to type the
  25198. 21:31:51word try and curly
  25199. 21:31:56brackets and now we're going to put the
  25200. 21:31:58code that could cause an error into try
  25201. 21:32:02so here we're making a request to the
  25202. 21:32:04back end and here we're also making a
  25203. 21:32:07request to the back end so this code
  25204. 21:32:09could cause an error so we're going to
  25205. 21:32:12move it inside try so we'll select it
  25206. 21:32:15right click
  25207. 21:32:17cut and then inside here we'll right
  25208. 21:32:20click and
  25209. 21:32:23paste and now if any of this code inside
  25210. 21:32:27a try gets an error we can catch it
  25211. 21:32:32using this code at at the bottom we'll
  25212. 21:32:34type
  25213. 21:32:36catch brackets and curly
  25214. 21:32:40brackets Now Catch is going to get one
  25215. 21:32:43parameter in these brackets called error
  25216. 21:32:47and this contains information about the
  25217. 21:32:49error if we need
  25218. 21:32:51it so catch Works similarly to catch for
  25219. 21:32:57promises if there's an error inside here
  25220. 21:33:01it's going to catch the error or it's
  25221. 21:33:03going to hand handle the error and run
  25222. 21:33:05the code inside catch so in here we can
  25223. 21:33:08put our error message so let's go back
  25224. 21:33:11to products.
  25225. 21:33:14JS and let's copy the error message so
  25226. 21:33:17we'll select this and right click
  25227. 21:33:22copy and back to
  25228. 21:33:24checkout inside catch we'll right click
  25229. 21:33:29and paste
  25230. 21:33:34so now I'll remove this new line and add
  25231. 21:33:37a new line here to make it a little
  25232. 21:33:39easier to
  25233. 21:33:41read and let's test out this
  25234. 21:33:44code to do that we'll go inside
  25235. 21:33:48products. JS again and we're going to
  25236. 21:33:50modify the URL again so at the front
  25237. 21:33:54here we're going to add error do super
  25238. 21:33:58simple backend and also we're going to
  25239. 21:34:00comment out the dot catch because right
  25240. 21:34:04now the error is going to be handled
  25241. 21:34:06here and it's not going to be handled
  25242. 21:34:08outside in the try catch so we're going
  25243. 21:34:12to comment this out using
  25244. 21:34:15slashstar and at the
  25245. 21:34:17end star slash and
  25246. 21:34:22save so we'll go back to checkout.
  25247. 21:34:26JS and here we'll also
  25248. 21:34:30save and now let's open the console
  25249. 21:34:35and you'll see that we had an error here
  25250. 21:34:38we caught the error and it ran this code
  25251. 21:34:41and displayed the error
  25252. 21:34:44message so that's how we handle errors
  25253. 21:34:47with async await we put our code that
  25254. 21:34:50can cause an error inside try and if
  25255. 21:34:55there is an error it's going to run the
  25256. 21:34:57code inside
  25257. 21:35:00catch so now let's go back to products
  25258. 21:35:04and remove error from the URL and the
  25259. 21:35:08dot and we'll also uncomment
  25260. 21:35:13catch and
  25261. 21:35:18save next we're going to learn some more
  25262. 21:35:20details about try catch so we'll open
  25263. 21:35:23checkout.
  25264. 21:35:25JS and one thing to know about TR catch
  25265. 21:35:29is that we don't have to use it with
  25266. 21:35:31async await we can actually use TR catch
  25267. 21:35:35with synchronous code or normal code for
  25268. 21:35:39example here we try to run a function
  25269. 21:35:42that does not exist and this will cause
  25270. 21:35:45an error try catch will catch this error
  25271. 21:35:49and display a message so notice that
  25272. 21:35:53we're not using async O8 here this is
  25273. 21:35:56just normal code so we can use try catch
  25274. 21:36:00to catch errors in normal code as well
  25275. 21:36:04another detail is whenever we get an
  25276. 21:36:07error it will skip the rest of the code
  25277. 21:36:10so here it will not console.log next
  25278. 21:36:13line it will skip the rest of this code
  25279. 21:36:17and go directly into
  25280. 21:36:21catch now one thing you might be
  25281. 21:36:23wondering is why don't we just use TR
  25282. 21:36:26catch everywhere and prevent all of our
  25283. 21:36:29errors so try catch and any error
  25284. 21:36:32handling is meant to handle unexpected
  25285. 21:36:36errors that means our code is correct
  25286. 21:36:39but something outside our control caused
  25287. 21:36:41the error so we can give some
  25288. 21:36:43instructions on what to
  25289. 21:36:45do so that's why we don't use try catch
  25290. 21:36:48everywhere we only use it for unexpected
  25291. 21:36:52errors or errors outside of our
  25292. 21:36:58control so the last thing to know about
  25293. 21:37:01error handling is that we can man
  25294. 21:37:03manually create
  25295. 21:37:05errors to manually create an error in TR
  25296. 21:37:08catch at the top we're going to use the
  25297. 21:37:11word
  25298. 21:37:13throw so throw creates a new error or it
  25299. 21:37:18throws an error that we're going to
  25300. 21:37:20catch a
  25301. 21:37:21later and now we need to give this error
  25302. 21:37:23a
  25303. 21:37:24value so we can use any type of value
  25304. 21:37:27for an error like a string number or an
  25305. 21:37:31object for an example we're just going
  25306. 21:37:33to use a string error
  25307. 21:37:37one so this will manually create an
  25308. 21:37:40error when we get an error it's going to
  25309. 21:37:43skip the rest of the code inside these
  25310. 21:37:46curly brackets and it's going to go
  25311. 21:37:48straight into catch so this error value
  25312. 21:37:52is going to be saved inside this
  25313. 21:37:55parameter and it's going to run the code
  25314. 21:37:57inside
  25315. 21:37:59catch so if we
  25316. 21:38:01save and then check the
  25317. 21:38:04console throw creates an error and it's
  25318. 21:38:07going to display this error message up
  25319. 21:38:14here so now let's comment out this
  25320. 21:38:18example and
  25321. 21:38:21save next if we're using promises there
  25322. 21:38:25are two ways to manually create an
  25323. 21:38:29error so inside this promise if we want
  25324. 21:38:33want to manually create an error we can
  25325. 21:38:35also just throw an error like the string
  25326. 21:38:40error
  25327. 21:38:412 and this will throw an error and go
  25328. 21:38:44inside dot catch by the way if we await
  25329. 21:38:49a promise instead of going into catch
  25330. 21:38:52it's going to go inside catch
  25331. 21:38:55here so await makes it behave sort of
  25332. 21:38:58like synchronous code or normal code if
  25333. 21:39:02we save
  25334. 21:39:04and check the
  25335. 21:39:07console it will get an error it will not
  25336. 21:39:10load the cart and it will display this
  25337. 21:39:13error
  25338. 21:39:15message so now let's comment out this
  25339. 21:39:19example and we'll learn the second way
  25340. 21:39:21to create an error in a
  25341. 21:39:24promise inside a promise if we need to
  25342. 21:39:27create an error in the future then we
  25343. 21:39:30need to use different code for example
  25344. 21:39:34here we load the cart and then once it's
  25345. 21:39:37finished loading we're going to run this
  25346. 21:39:40function so this function runs in the
  25347. 21:39:44future inside this function throw does
  25348. 21:39:47not work because throw does not work in
  25349. 21:39:51the
  25350. 21:39:52future instead promises give us another
  25351. 21:39:55way to create an error when we create a
  25352. 21:39:59new promise it gives us a second
  25353. 21:40:02parameter
  25354. 21:40:04called
  25355. 21:40:06reject reject is a function and it lets
  25356. 21:40:10us create an error in the
  25357. 21:40:14future so here after we load the cart in
  25358. 21:40:18the future let's create an error using
  25359. 21:40:22reject brackets and a string error
  25360. 21:40:29three so let's comment out resolve as an
  25361. 21:40:32example
  25362. 21:40:34so now let's
  25363. 21:40:36save and check the
  25364. 21:40:40console and you'll see that reject
  25365. 21:40:42created an error in the
  25366. 21:40:45future and then it goes down here and
  25367. 21:40:47runs the code inside catch and creates
  25368. 21:40:51the error
  25369. 21:40:55message so the manually create an error
  25370. 21:40:58in a promise we have two ways we can use
  25371. 21:41:01throw if we're creating the error
  25372. 21:41:04synchronously or right away or we can
  25373. 21:41:07use reject if we're creating the error
  25374. 21:41:10asynchronously or in the
  25375. 21:41:13future so now let's comment out
  25376. 21:41:17reject and uncomment resolve and
  25377. 21:41:23save so that's error handling it lets us
  25378. 21:41:27handle unexpected errors and it also
  25379. 21:41:29lets us create our own
  25380. 21:41:32errors so now let's save these changes
  25381. 21:41:34into git we'll open git inside products.
  25382. 21:41:39JS we added. catch for
  25383. 21:41:43promises and an error function for
  25384. 21:41:46callbacks and then in checkout. JS we
  25385. 21:41:49added try catch for async await and we
  25386. 21:41:53also learned how to manually create our
  25387. 21:41:56own
  25388. 21:41:57errors so we'll type a message
  25389. 21:41:59describing our
  25390. 21:42:00changes learn and error
  25391. 21:42:04handling and
  25392. 21:42:12commit now that we learned backend
  25393. 21:42:15promises and async await let's use them
  25394. 21:42:18in our
  25395. 21:42:20project we'll open our
  25396. 21:42:23project and open the tab for the project
  25397. 21:42:27and we're going to go to the checkout
  25398. 21:42:29page so the feature we're going to work
  25399. 21:42:31on is in the checkout page we're going
  25400. 21:42:34to make this place order button
  25401. 21:42:37interactive when we click this button
  25402. 21:42:40it's going to take our cart and turn it
  25403. 21:42:42into an
  25404. 21:42:44order this time instead of writing this
  25405. 21:42:46code ourselves we're going to use the
  25406. 21:42:49back end to create an
  25407. 21:42:53order so first let's set up an event
  25408. 21:42:56listener for this button we'll go back
  25409. 21:42:59to our
  25410. 21:43:00code and open our files
  25411. 21:43:03and that button is in a section called
  25412. 21:43:05the payment
  25413. 21:43:07summary so we're going to open scripts
  25414. 21:43:10SL checkout payment summary.jpg
  25415. 21:43:34your order and now we have the code for
  25416. 21:43:37the button right here so we're going to
  25417. 21:43:39set up an event listener so we can do
  25418. 21:43:42something when we click
  25419. 21:43:43it first we'll add a class to the button
  25420. 21:43:46so we can get it in our
  25421. 21:43:49JavaScript at the end of the class let's
  25422. 21:43:51create a new line and add a class
  25423. 21:43:55js- Place Das
  25424. 21:43:59order and now after we put this HTML on
  25425. 21:44:03the page we're going to set up an event
  25426. 21:44:06listener to do that we're going to get
  25427. 21:44:09this button into our JavaScript using
  25428. 21:44:13document. query
  25429. 21:44:16selector
  25430. 21:44:18brackets and a string and to select this
  25431. 21:44:21class we're going to start with a
  25432. 21:44:24js- place-
  25433. 21:44:28order and now that we have this button
  25434. 21:44:31we're going to add an event listener to
  25435. 21:44:33to it so we'll create a new line and use
  25436. 21:44:36the method do add event
  25437. 21:44:41listener
  25438. 21:44:43brackets and we'll give this two
  25439. 21:44:45parameters the first parameter is the
  25440. 21:44:48event we want to listen for which is
  25441. 21:44:52click and the second parameter is a
  25442. 21:44:54function we want to run when we click
  25443. 21:44:56this button so we'll type brackets arrow
  25444. 21:45:00and curler brackets
  25445. 21:45:03and now when we click this button we're
  25446. 21:45:05going to make a request to the back end
  25447. 21:45:07to create the
  25448. 21:45:10order so for this example I set up a URL
  25449. 21:45:14path in my backend for creating orders
  25450. 21:45:18called slash
  25451. 21:45:20orders so inside here we're going to
  25452. 21:45:22make a request to the backend using
  25453. 21:45:26fetch
  25454. 21:45:27brackets and then we're going to give it
  25455. 21:45:29the URL of the request which is H ttps
  25456. 21:45:34colon double
  25457. 21:45:36slash super simple
  25458. 21:45:40backend. Dev and the URL path slash
  25459. 21:45:47orders however this time we need to send
  25460. 21:45:50some data to the back end so we need to
  25461. 21:45:53send our cart to the backend in order to
  25462. 21:45:56create an
  25463. 21:45:59order to send data in a request we need
  25464. 21:46:03to use a different type of requests so
  25465. 21:46:07there are four common types of requests
  25466. 21:46:10get post put and delete get means we
  25467. 21:46:14want to get something from the back end
  25468. 21:46:17post means we want the back end to
  25469. 21:46:19create something put means we want it to
  25470. 21:46:23update something and delete means we
  25471. 21:46:25want to delete something so far we only
  25472. 21:46:29use get request which let us get data
  25473. 21:46:32from from the back
  25474. 21:46:34end however get requests don't really
  25475. 21:46:37let us send data to the back
  25476. 21:46:42end so in this situation we want to
  25477. 21:46:45create an order so we're going to use
  25478. 21:46:48post to create something post lets us
  25479. 21:46:52send data to the
  25480. 21:46:56backend so to create a post request
  25481. 21:47:00instead of a get request we're going to
  25482. 21:47:02give fetch a second
  25483. 21:47:04parameter which is an
  25484. 21:47:07object and inside this object we're
  25485. 21:47:10going to give it a property called
  25486. 21:47:13method
  25487. 21:47:14colon and this is the type of the
  25488. 21:47:17request in this case we'll use the
  25489. 21:47:19string
  25490. 21:47:22post and after this we're going to give
  25491. 21:47:25another property called
  25492. 21:47:27headers colon and this is going to be an
  25493. 21:47:31object
  25494. 21:47:33headers gives the backend more
  25495. 21:47:35information about our request and this
  25496. 21:47:38is needed when we're sending data to the
  25497. 21:47:41back
  25498. 21:47:42end inside this object we'll add the
  25499. 21:47:45property string Capital C content Das
  25500. 21:47:50capital T
  25501. 21:47:52type
  25502. 21:47:54colon and the value will be the string
  25503. 21:47:58application slash Json
  25504. 21:48:03this tells the back endend what type of
  25505. 21:48:06data we're sending in our request here
  25506. 21:48:09we're going to send some Jon which is
  25507. 21:48:11basically a JavaScript
  25508. 21:48:14object next we're going to add another
  25509. 21:48:16property called body
  25510. 21:48:20colon and this is the actual data we're
  25511. 21:48:23going to send to the back
  25512. 21:48:25end according to the documentation we
  25513. 21:48:28need to send an object with a property
  25514. 21:48:30called cart and this contains our cart
  25515. 21:48:36array so in the body let's send an
  25516. 21:48:39object with a property called cart colon
  25517. 21:48:44and give it the cart
  25518. 21:48:46array so we should have the cart array
  25519. 21:48:48in this file and it should be called
  25520. 21:48:53cart and finally we can't send an object
  25521. 21:48:57directly in our request we need to
  25522. 21:48:59convert it into a Json string
  25523. 21:49:05to do that at the front we'll use
  25524. 21:49:09json.
  25525. 21:49:11stringify Open Bracket and close bracket
  25526. 21:49:16so this will convert this data into a
  25527. 21:49:18Json string and now we can send it to
  25528. 21:49:21the back
  25529. 21:49:24end okay so after we send this request
  25530. 21:49:27we need to wait for the response to come
  25531. 21:49:30back to do that fetch returns a
  25532. 21:49:34promise however instead of using dot
  25533. 21:49:37then to add a Next Step let's use async
  25534. 21:49:40a weit because it's cleaner so first we
  25535. 21:49:43need to make this function
  25536. 21:49:46async remember we can only use await
  25537. 21:49:49inside an async
  25538. 21:49:52function so at the front of this
  25539. 21:49:54function we'll type the word
  25540. 21:49:58async and now in front of fetch we can
  25541. 21:50:01use
  25542. 21:50:04await this is going to wait for fetch to
  25543. 21:50:07finish or for fetch to get a response
  25544. 21:50:10from the back end and then go to the
  25545. 21:50:12next
  25546. 21:50:14line now because we used await we can
  25547. 21:50:17save the response in a variable so at
  25548. 21:50:21the front we'll create a variable const
  25549. 21:50:24and call it response and make it equal
  25550. 21:50:28to this result
  25551. 21:50:33next remember that to get the data
  25552. 21:50:36that's attached to the response we need
  25553. 21:50:38to use response.
  25554. 21:50:40Json so down here we're going to use
  25555. 21:50:45response. Json and
  25556. 21:50:48brackets now remember that response.
  25557. 21:50:51Json is also a promise so at the front
  25558. 21:50:55we can use await to wait for this
  25559. 21:50:58promise to finish before going to the
  25560. 21:51:00next line
  25561. 21:51:03so this will give us the data that's
  25562. 21:51:05attached to the response which should be
  25563. 21:51:08the order that was created by the
  25564. 21:51:11backet so let's save this in a variable
  25565. 21:51:14using const order
  25566. 21:51:21equals and finally let's
  25567. 21:51:25console.log the order to see what it
  25568. 21:51:28looks like so now let's save
  25569. 21:51:32and then we'll open the
  25570. 21:51:35project and make sure you have some
  25571. 21:51:37products in your
  25572. 21:51:39cart and then we'll click place
  25573. 21:51:43order and now we can check our console
  25574. 21:51:45to see what the order looks
  25575. 21:51:52like and you'll see that we got an order
  25576. 21:51:55object from the back end it has an order
  25577. 21:51:58ID an order time and the products in our
  25578. 21:52:03order including a delivery time and the
  25579. 21:52:07quantity that we
  25580. 21:52:09want so we successfully use the back end
  25581. 21:52:12to create a new
  25582. 21:52:14order let's also check the network tab
  25583. 21:52:17up here and open
  25584. 21:52:20network so I'll resize this to make it a
  25585. 21:52:23little easier to read and we'll search
  25586. 21:52:26for the super simple
  25587. 21:52:28URL and then we'll find this request
  25588. 21:52:31orders with a status 200 and we'll click
  25589. 21:52:36it so inside the details you'll see that
  25590. 21:52:39it was a post request sent to/ orders
  25591. 21:52:44and the status code was 200 so it was
  25592. 21:52:47successful inside the payload tab this
  25593. 21:52:51is what we sent to the back end which is
  25594. 21:52:54our
  25595. 21:52:55cart in the preview tab this is what the
  25596. 21:52:58backend sent back an order object
  25597. 21:53:02and the response tab is the same thing
  25598. 21:53:04we got the order object
  25599. 21:53:09back so that's how a real world website
  25600. 21:53:12works instead of creating the order on
  25601. 21:53:15the web page or on our computer we send
  25602. 21:53:19an HTTP request to the backend to create
  25603. 21:53:23the
  25604. 21:53:30order okay now that we created an order
  25605. 21:53:33let's save it
  25606. 21:53:36somewhere we'll go back to our
  25607. 21:53:39code and on the left inside the data
  25608. 21:53:43folder let's create a file for our
  25609. 21:53:45orders so we'll click inside this folder
  25610. 21:53:49and then click here to create a file and
  25611. 21:53:52let's name it
  25612. 21:53:54orders. JS and press
  25613. 21:53:58enter at the top let's create an array
  25614. 21:54:01to contain rain all of our orders we use
  25615. 21:54:05const orders equals and empty array and
  25616. 21:54:10let's export this so we can use it in
  25617. 21:54:12other files
  25618. 21:54:15export next let's create a function for
  25619. 21:54:18adding an order to this array so we'll
  25620. 21:54:21create a
  25621. 21:54:23function add
  25622. 21:54:25order brackets and curly brackets and
  25623. 21:54:29we'll give this function an order object
  25624. 21:54:33and we're just going to add it to the
  25625. 21:54:35array now for orders we usually want the
  25626. 21:54:38most recent order at the top so we're
  25627. 21:54:41going to add this order to the front of
  25628. 21:54:43the array to do that we'll type the
  25629. 21:54:47array name orders and use the method do
  25630. 21:54:52unshift
  25631. 21:54:54brackets and we'll give it the
  25632. 21:54:57order so this will add the order to the
  25633. 21:55:00front of the array instead of the
  25634. 21:55:03back finally let's save our orders into
  25635. 21:55:06local storage so we'll create a function
  25636. 21:55:09to save to local storage
  25637. 21:55:12function save to
  25638. 21:55:16storage brackets and curly brackets and
  25639. 21:55:19we'll run local
  25640. 21:55:22storage. set item brackets and a string
  25641. 21:55:28we'll use a local storage key orders
  25642. 21:55:33comma and remember local storage only
  25643. 21:55:36supports strings so we'll use
  25644. 21:55:39json.
  25645. 21:55:41stringify
  25646. 21:55:43brackets
  25647. 21:55:45orders and now when we modify this array
  25648. 21:55:49we're going to save to storage and
  25649. 21:55:57brackets finally at the top let's load
  25650. 21:56:00the orders from Storage
  25651. 21:56:02so
  25652. 21:56:04here we use local
  25653. 21:56:08storage. getet item brackets the string
  25654. 21:56:14orders and remember that this will be a
  25655. 21:56:17string so we need to convert it back
  25656. 21:56:20into an array using the code json. pars
  25657. 21:56:25and
  25658. 21:56:28brackets now in the beginning we're not
  25659. 21:56:30going to have any orders saved in local
  25660. 21:56:34storage so we can give this a default
  25661. 21:56:37value using or and an empty array so if
  25662. 21:56:43there's nothing in local storage it's
  25663. 21:56:45going to use this empty array as a
  25664. 21:56:47default and
  25665. 21:56:53save so now let's use add order in the
  25666. 21:56:56payment summary so first we'll export
  25667. 21:56:59this
  25668. 21:57:00function and and
  25669. 21:57:03save and then open the payment summary
  25670. 21:57:07again and at the top we're going to
  25671. 21:57:10import that function so here we'll
  25672. 21:57:13import curly brackets add
  25673. 21:57:18order from a
  25674. 21:57:21string and we'll go into the data folder
  25675. 21:57:24which is do do slash
  25676. 21:57:26dotdata SL orders
  25677. 21:57:30doj and and we'll scroll back
  25678. 21:57:34down and now after we create an order
  25679. 21:57:37from the back end we're going to add it
  25680. 21:57:39to the array and save it in local
  25681. 21:57:42storage using the function add order
  25682. 21:57:47brackets the order
  25683. 21:57:51object and
  25684. 21:57:54save now if we go to our web page and
  25685. 21:57:58click place
  25686. 21:58:00order this should create an order order
  25687. 21:58:02from the back end and save it in local
  25688. 21:58:05storage so now we can open our
  25689. 21:58:08console and go to the console Tab and
  25690. 21:58:12let's check if it's inside local storage
  25691. 21:58:15using local
  25692. 21:58:18storage. getet item brackets and the
  25693. 21:58:22string
  25694. 21:58:24orders and press
  25695. 21:58:28enter and you'll see that we saved an
  25696. 21:58:30order in local store
  25697. 21:58:34storage if you ever want to remove the
  25698. 21:58:37orders from local storage you can use
  25699. 21:58:40this
  25700. 21:58:41code local
  25701. 21:58:44storage. remove item brackets and the
  25702. 21:58:49string
  25703. 21:58:51orders press enter and then refresh the
  25704. 21:58:59page let's go back to our code
  25705. 21:59:02and let's get some practice and add some
  25706. 21:59:05error handling to this code so at the
  25707. 21:59:08top for asyn of weight we're going to
  25708. 21:59:11use try catch so we'll type try curly
  25709. 21:59:16bracket and catch brackets and curly
  25710. 21:59:21brackets and catch gets a parameter
  25711. 21:59:23called
  25712. 21:59:25error so now we're going to move our
  25713. 21:59:27code inside a try we'll select this code
  25714. 21:59:32and then right click
  25715. 21:59:34cut inside try right click and
  25716. 21:59:40paste and I'll create a new line and now
  25717. 21:59:44if we make a request and there's some
  25718. 21:59:46sort of network error that error will go
  25719. 21:59:49down here and we'll run the code inside
  25720. 21:59:53catch so in here let's display a
  25721. 21:59:56message
  25722. 21:59:58console. log the string
  25723. 22:00:02unexpected
  25724. 22:00:06error try again
  25725. 22:00:12later the last step is after we create
  25726. 22:00:15an order we should go to the orders
  25727. 22:00:19page to do that at the bottom
  25728. 22:00:24here we're going to use an object called
  25729. 22:00:28window.location window.location is a
  25730. 22:00:31special object object provided by
  25731. 22:00:33JavaScript and it lets us control the
  25732. 22:00:36URL at the top of the
  25733. 22:00:39browser if we change the location object
  25734. 22:00:43it will change the url at the top so
  25735. 22:00:46let's give this a try we'll go into our
  25736. 22:00:50code and we'll type
  25737. 22:00:56window.location Now location has a
  25738. 22:00:58property
  25739. 22:00:59called href
  25740. 22:01:02this gives us the URL at the top of the
  25741. 22:01:06browser and if we change the href
  25742. 22:01:08property it will change the url at the
  25743. 22:01:12top of the browser so we'll try that in
  25744. 22:01:15our code we're going to change. href and
  25745. 22:01:19make it equal to the string orders.
  25746. 22:01:27HTML so this will replace everything
  25747. 22:01:30after the slash here with orders.
  25748. 22:01:35HTML and this will open the orders. HTML
  25749. 22:01:38file which is at the bottom
  25750. 22:01:42here and this will open the orders
  25751. 22:01:45Page by the way orders. HTML is a file
  25752. 22:01:51path the current file is checkout. HTML
  25753. 22:01:55because this code is running on the
  25754. 22:01:56checkout page so it starts from
  25755. 22:01:59checkout. HTML and then looks for a file
  25756. 22:02:03called orders. HTML beside it which is
  25757. 22:02:07this file here and it loads this file in
  25758. 22:02:10the
  25759. 22:02:12browser so now let's
  25760. 22:02:16save we'll open the project and then
  25761. 22:02:19click place
  25762. 22:02:23order so it should create a new order
  25763. 22:02:26using the backend and it will take us to
  25764. 22:02:28the orders
  25765. 22:02:30page so right now we have a list of
  25766. 22:02:33order objects and that's all the data
  25767. 22:02:36that we need to create this page I'll
  25768. 22:02:39leave generating the HTML and making
  25769. 22:02:42this page interactive as
  25770. 22:02:46exercises so now let's check our tests
  25771. 22:02:49to make sure that everything is still
  25772. 22:02:50working we'll open the tab for our tests
  25773. 22:02:54and I'll refresh just in case and it
  25774. 22:02:56looks good and we'll save our changes
  25775. 22:02:58into git we'll go to the git section
  25776. 22:03:04inside orders. JS we created some
  25777. 22:03:07functions for saving our orders and in
  25778. 22:03:11payment summary.
  25779. 22:03:13JS we made the place order button
  25780. 22:03:18interactive and we use the backend to
  25781. 22:03:20create an order let's create a message
  25782. 22:03:24describing these
  25783. 22:03:26changes create order using the back end
  25784. 22:03:33and
  25785. 22:03:39commit so we're going to learn one more
  25786. 22:03:42thing in this lesson and this is needed
  25787. 22:03:44to create the tracking page we're going
  25788. 22:03:47to learn how to use URL
  25789. 22:03:52parameters so first let's open our
  25790. 22:03:55project and we'll go back to the orders
  25791. 22:03:59page and on this page we have these
  25792. 22:04:03track package links beside the products
  25793. 22:04:06if we click this link it will take us to
  25794. 22:04:10the tracking page at tracking.
  25795. 22:04:14HTML however right now this page is just
  25796. 22:04:17plain HTML there's no
  25797. 22:04:20JavaScript so if we go back to our
  25798. 22:04:24orders and then track a different
  25799. 22:04:28product you'll notice that all of the
  25800. 22:04:31tracking pag pages are the
  25801. 22:04:33same so we need to make each tracking
  25802. 22:04:35page different depending on the order
  25803. 22:04:38and the product so how do we know which
  25804. 22:04:41product to track on this
  25805. 22:04:44page to create this we can use a feature
  25806. 22:04:47called URL
  25807. 22:04:48parameters URL parameters let us save
  25808. 22:04:52data directly in the
  25809. 22:04:55URL to create a URL parameter at the top
  25810. 22:05:00after this URL we're going to type
  25811. 22:05:03question mark and then order capital i d
  25812. 22:05:10equals 1 2 3 for example and press
  25813. 22:05:16enter so the question mark means we're
  25814. 22:05:19adding a URL parameter to this
  25815. 22:05:22URL so a parameter is a property value
  25816. 22:05:26pair the left side is a property and the
  25817. 22:05:29right side is a value
  25818. 22:05:31this is sort of like an object like
  25819. 22:05:37this so using URL parameters we can save
  25820. 22:05:41some data in the URL like
  25821. 22:05:45this and then we can use JavaScript to
  25822. 22:05:48get this data out of the URL to do that
  25823. 22:05:53we'll go back to our
  25824. 22:05:55code and we'll open our files here and
  25825. 22:05:58we'll scroll down to the bottom to the
  25826. 22:06:01tracking
  25827. 22:06:02page and let's scroll all the way down
  25828. 22:06:05to the
  25829. 22:06:07bottom and at the bottom of the body
  25830. 22:06:10let's add a script tag to run some
  25831. 22:06:12JavaScript so we'll type less than
  25832. 22:06:15script greater
  25833. 22:06:18than and to get the parameters out of
  25834. 22:06:21the URL we can use a built-in class
  25835. 22:06:24called
  25836. 22:06:26URL first let's generate an object using
  25837. 22:06:29this class we'll type
  25838. 22:06:32New Capital URL and
  25839. 22:06:36brackets and we'll give it the URL that
  25840. 22:06:39we want to get the parameters
  25841. 22:06:41from so in here we'll type
  25842. 22:06:47window.location.href
  25843. 22:06:49remember that
  25844. 22:06:52window.location.href gets the URL at the
  25845. 22:06:55top of the
  25846. 22:06:58browser and now let's save this object
  25847. 22:07:00in a variable
  25848. 22:07:02at the front we'll create const URL
  25849. 22:07:06equals this
  25850. 22:07:09object so this URL class analyzes the
  25851. 22:07:13URL that we give it and breaks it down
  25852. 22:07:16into different parts one of the parts
  25853. 22:07:19are the URL
  25854. 22:07:21parameters to get the parameters we're
  25855. 22:07:23going to use a property
  25856. 22:07:26url. search capital p perams
  25857. 22:07:32by the way URL parameters are also
  25858. 22:07:35called search
  25859. 22:07:37parameters because they're used for
  25860. 22:07:39features like YouTube
  25861. 22:07:44search so search params will let us get
  25862. 22:07:48the URL parameters up
  25863. 22:07:51here so let's say that we wanted the
  25864. 22:07:53order ID
  25865. 22:07:55parameter inside our
  25866. 22:07:57code search params has a method called
  25867. 22:08:01Dot
  25868. 22:08:01get brackets and this lets us get
  25869. 22:08:05individual parameters so inside here
  25870. 22:08:08we'll give it a string and we'll get the
  25871. 22:08:11order capital i d parameter out of the
  25872. 22:08:17URL so let's console.log this
  25873. 22:08:22console.log and
  25874. 22:08:25brackets if we
  25875. 22:08:28save and then open our project and go to
  25876. 22:08:32the tracking page and then open the
  25877. 22:08:34console right click
  25878. 22:08:38inspect the
  25879. 22:08:40console you'll see that it gave us the
  25880. 22:08:43order ID that we saved up
  25881. 22:08:47here so using that code we can get URL
  25882. 22:08:51parameters out of the URL at the top and
  25883. 22:08:55then use them to create this
  25884. 22:08:59page we can also say save multiple
  25885. 22:09:02parameters in the URL to save another
  25886. 22:09:05parameter at the end we can just type
  25887. 22:09:09and and now we can give it another
  25888. 22:09:12property value pair like product Capital
  25889. 22:09:17ID equal
  25890. 22:09:19456 and press
  25891. 22:09:23enter and now inside our code we can get
  25892. 22:09:26this second parameter
  25893. 22:09:31so inside here we're going to make a
  25894. 22:09:33copy of this line select it right click
  25895. 22:09:38copy and then down here right click
  25896. 22:09:43paste and this time we're going to get
  25897. 22:09:46the
  25898. 22:09:47parameter product Capital ID and
  25899. 22:09:54save if we open the page and open the
  25900. 22:09:57console
  25901. 22:10:01we got the order ID as well as the
  25902. 22:10:05product ID out of the
  25903. 22:10:09URL so URL parameters lets us save
  25904. 22:10:13different data in each
  25905. 22:10:16URL and we can actually go back and
  25906. 22:10:19forth between them so if we click the
  25907. 22:10:21back button it will go to this URL and
  25908. 22:10:26this URL has different data or
  25909. 22:10:28parameters saved inside if we click
  25910. 22:10:32forward this URL has different
  25911. 22:10:34parameters or data saved
  25912. 22:10:37inside and this is how YouTube search
  25913. 22:10:39works if we search for something our
  25914. 22:10:42search is saved as a URL parameter if we
  25915. 22:10:46search for something else our search is
  25916. 22:10:48also saved as a URL parameter and we can
  25917. 22:10:51go back and forth between the
  25918. 22:10:59two so this is actually all the data
  25919. 22:11:02that we need to create the tracking page
  25920. 22:11:05we need to know which order it is and
  25921. 22:11:07which product it
  25922. 22:11:09is now there's one more thing to know to
  25923. 22:11:12create this page we'll go back to our
  25924. 22:11:15code and we're going to open the orders
  25925. 22:11:18page at orders.
  25926. 22:11:20HTML and we're going to find the track
  25927. 22:11:23package button using control F or
  25928. 22:11:26command F and we'll type the text in
  25929. 22:11:29that button track
  25930. 22:11:33package so notice that these buttons are
  25931. 22:11:36just a link to the tracking page or
  25932. 22:11:39tracking.
  25933. 22:11:40HTML so when you're generating this
  25934. 22:11:43HTML make sure that you add URL
  25935. 22:11:46parameters after this tracking page
  25936. 22:11:49URL using question mark the order
  25937. 22:11:54Capital ID equals and then you
  25938. 22:11:57substitute the order ID like 1 2 3
  25939. 22:12:01and then and also attach the product ID
  25940. 22:12:06product Capital ID equals
  25941. 22:12:11456 so when generating this HTML make
  25942. 22:12:14sure you substitute the order ID and
  25943. 22:12:18product ID into this URL so that when it
  25944. 22:12:22opens the tracking page you know which
  25945. 22:12:24order and which product to track
  25946. 22:12:31I'll leave generating the HTML and
  25947. 22:12:33making the tracking page interactive as
  25948. 22:12:37exercises so now let's save this
  25949. 22:12:40file and let's check our tests so we'll
  25950. 22:12:44open the browser and open the test Tab
  25951. 22:12:47and everything is still working and
  25952. 22:12:49we'll save these changes into git open
  25953. 22:12:53the git
  25954. 22:12:54section in orders.
  25955. 22:12:59HTML we added an example of adding URL
  25956. 22:13:03parameters to the tracking page
  25957. 22:13:07Link in tracking. HTML we added an
  25958. 22:13:11example of getting the parameters out of
  25959. 22:13:14the URL at the
  25960. 22:13:16top let's create a message describing
  25961. 22:13:19these
  25962. 22:13:20changes add URL
  25963. 22:13:25parameters
  25964. 22:13:26example and commit
  25965. 22:13:33and that's the end of this lesson in
  25966. 22:13:36this lesson we learned what a vend is
  25967. 22:13:39and how to make HTTP requests to the
  25968. 22:13:42back end we learned how to use XML HTTP
  25969. 22:13:46request and
  25970. 22:13:47fetch we learned how to handle
  25971. 22:13:49asynchronous code using callbacks
  25972. 22:13:52promises and async await we learned how
  25973. 22:13:56to test asynchronous code we learned
  25974. 22:13:59about error handling for unexpected
  25975. 22:14:02errors we use the backend in our project
  25976. 22:14:06and we learn URL
  25977. 22:14:09parameters before we look at the
  25978. 22:14:11exercises you might be wondering what's
  25979. 22:14:14the next step after this course instead
  25980. 22:14:17of just using a backend the next step is
  25981. 22:14:20to learn how to create our own backend
  25982. 22:14:24to do that you need to learn the command
  25983. 22:14:26line and after that you'll learn a
  25984. 22:14:28technology called nodejs which lets us
  25985. 22:14:31create our own
  25986. 22:14:34backend also after this course you can
  25987. 22:14:37learn how to put a website on the
  25988. 22:14:39internet using this video right
  25989. 22:14:45here here are some exercises that'll
  25990. 22:14:47help you practice what we learned in
  25991. 22:14:49this
  25992. 22:14:59lesson e
  25993. 22:15:38thanks for watching here are some other
  25994. 22:15:41videos and courses that you might be
  25995. 22:15:43interested in if you want to support
  25996. 22:15:45this channel you can click like
  25997. 22:15:47subscribe and share this video with
  25998. 22:15:50others thanks again and I'll see you in
  25999. 22:15:52the next one

About this transcript

This page contains the full transcript of YouTube transcript (EerdGm-ehJQ) , generated from the public captions YouTube serves with the video. The transcript has 165,390 words across 25,999 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.