1 00:00:00,000 --> 00:00:00,890 2 00:00:00,890 --> 00:00:03,835 >> [MUSIC PLAYING] 3 00:00:03,835 --> 00:00:10,597 4 00:00:10,597 --> 00:00:12,030 >> SPEAKER 1: All right, everyone. 5 00:00:12,030 --> 00:00:14,330 Welcome to your last section with me. 6 00:00:14,330 --> 00:00:16,160 It's been quite the semester. 7 00:00:16,160 --> 00:00:21,290 I can't believe this is, like, week 12 or 11, something like that. 8 00:00:21,290 --> 00:00:24,520 But because it's our last section, we have lots of candy. 9 00:00:24,520 --> 00:00:27,090 And I'm going to try and prep you as well as I 10 00:00:27,090 --> 00:00:30,270 can for your quiz on Wednesday. 11 00:00:30,270 --> 00:00:34,060 So probably not the best way to, like, end section with quiz review. 12 00:00:34,060 --> 00:00:35,630 But we'll try and make it fun. 13 00:00:35,630 --> 00:00:36,720 And there's always candy. 14 00:00:36,720 --> 00:00:39,070 So hopefully, that'll make it better. 15 00:00:39,070 --> 00:00:43,800 >> Your quiz is going to be pretty much the same thing as Quiz Zero. 16 00:00:43,800 --> 00:00:46,840 It's going to be the same format, same length. 17 00:00:46,840 --> 00:00:50,065 You'll be in rooms scattered across campus, because there 18 00:00:50,065 --> 00:00:54,660 are close to 900 of you, and we can't all hold you in rooms like this. 19 00:00:54,660 --> 00:00:58,610 So be sure to look at all of the Quiz One documentation 20 00:00:58,610 --> 00:01:02,410 online at cs50.harvard.edu tonight or tomorrow night. 21 00:01:02,410 --> 00:01:06,070 Make sure you know where you're going, what you need to bring. 22 00:01:06,070 --> 00:01:10,052 Really, you just need to bring yourself and a pencil, and you should be fine. 23 00:01:10,052 --> 00:01:11,760 The questions will be very much the same. 24 00:01:11,760 --> 00:01:15,250 There will probably be a couple multiple choice, a couple short answer, 25 00:01:15,250 --> 00:01:22,610 some coding, maybe some true/false, matching sorts of things. 26 00:01:22,610 --> 00:01:27,790 So you shouldn't be too surprised by the format. 27 00:01:27,790 --> 00:01:31,380 >> What can be surprising is the length, which, in previous years, 28 00:01:31,380 --> 00:01:32,970 has been much longer. 29 00:01:32,970 --> 00:01:38,060 In my year, I don't have a single friend that I remember finishing the exam. 30 00:01:38,060 --> 00:01:41,100 There were like multiple pages left blank. 31 00:01:41,100 --> 00:01:43,710 So the length can be kind of disorienting 32 00:01:43,710 --> 00:01:47,554 just because in some years, it has been much longer than Quiz One. 33 00:01:47,554 --> 00:01:49,720 I have no idea what it's going to be like this year, 34 00:01:49,720 --> 00:01:51,640 but it's something to be aware of. 35 00:01:51,640 --> 00:01:53,390 >> Additionally, the difficulty-- people tend 36 00:01:53,390 --> 00:01:56,660 to find Quiz One much more difficult than Quiz Zero. 37 00:01:56,660 --> 00:01:58,590 Again, you know, if you're kind of walking 38 00:01:58,590 --> 00:02:01,590 out feeling a little uneasy because you didn't do as well as you thought 39 00:02:01,590 --> 00:02:04,430 you would or you feel like you didn't do as well as you did on Quiz One, 40 00:02:04,430 --> 00:02:07,429 remember that there are probably a lot of other people feeling that way. 41 00:02:07,429 --> 00:02:09,800 I was definitely one of those people. 42 00:02:09,800 --> 00:02:11,770 Quiz one, like, threw me for a loop. 43 00:02:11,770 --> 00:02:14,540 And my friends have this great picture of them at JP Licks 44 00:02:14,540 --> 00:02:17,834 kind of with these huge sundaes, being like, Quiz One, 45 00:02:17,834 --> 00:02:19,250 and looking like they're in tears. 46 00:02:19,250 --> 00:02:21,860 >> So it's a general feeling. 47 00:02:21,860 --> 00:02:25,257 It's a tough test, it's a tough quiz. 48 00:02:25,257 --> 00:02:28,340 Obviously, if you come out and you feel like you rocked it, great for you. 49 00:02:28,340 --> 00:02:32,050 But know that you're probably not-- you are almost most definitely not 50 00:02:32,050 --> 00:02:36,020 alone if you walk out feeling like, eh, probably could have gotten better. 51 00:02:36,020 --> 00:02:37,003 It's tough. 52 00:02:37,003 --> 00:02:37,503 Mm-hm. 53 00:02:37,503 --> 00:02:39,120 >> AUDIENCE: How much is it worth in relation to [INAUDIBLE]? 54 00:02:39,120 --> 00:02:40,411 >> SPEAKER 1: It's worth the same. 55 00:02:40,411 --> 00:02:41,730 Yeah. 56 00:02:41,730 --> 00:02:45,150 But again, it's also going to be affected by how the rest of the class 57 00:02:45,150 --> 00:02:48,766 does, how your section does, how you've improved over time. 58 00:02:48,766 --> 00:02:50,390 Grading is a little bit of a black box. 59 00:02:50,390 --> 00:02:54,310 So what the actual number or percentage you get 60 00:02:54,310 --> 00:02:56,790 is not necessarily like directly correlated with the grade 61 00:02:56,790 --> 00:02:57,790 you're going to get. 62 00:02:57,790 --> 00:03:00,210 There's a lot of factors that go into it. 63 00:03:00,210 --> 00:03:04,090 >> So with that, we're going to go through some quiz review. 64 00:03:04,090 --> 00:03:07,600 I'm just going to run this like last time for you guys. 65 00:03:07,600 --> 00:03:10,960 I have all the answers to Quiz One of last year. 66 00:03:10,960 --> 00:03:14,260 So if you guys want to pull that up on your own computers, 67 00:03:14,260 --> 00:03:18,100 if you've taken this exam before and you have questions, 68 00:03:18,100 --> 00:03:20,090 I was thinking we could just go through some 69 00:03:20,090 --> 00:03:22,380 of the more difficult ones on that. 70 00:03:22,380 --> 00:03:25,810 So we can talk about it, maybe get a little more insight 71 00:03:25,810 --> 00:03:29,260 into the intuition behind some of these questions, how to approach them. 72 00:03:29,260 --> 00:03:34,420 >> As well, like last time, I have a 100-slide deck 73 00:03:34,420 --> 00:03:37,760 that has basically all of the slides that I 74 00:03:37,760 --> 00:03:40,360 have prepared for previous sections since your last quiz-- I 75 00:03:40,360 --> 00:03:41,540 have in this deck. 76 00:03:41,540 --> 00:03:43,960 So we can go through pretty much anything 77 00:03:43,960 --> 00:03:46,450 that we've talked about beforehand that maybe you 78 00:03:46,450 --> 00:03:49,130 want a little more clarification on. 79 00:03:49,130 --> 00:03:50,320 We could do that first. 80 00:03:50,320 --> 00:03:52,567 >> But otherwise, I'm super happy just to kind of work 81 00:03:52,567 --> 00:03:53,900 through these problems with you. 82 00:03:53,900 --> 00:03:58,700 And I'm really here to focus on what you guys think 83 00:03:58,700 --> 00:04:01,466 would be most important to focus on. 84 00:04:01,466 --> 00:04:03,466 Like, obviously, all of this stuff is important. 85 00:04:03,466 --> 00:04:07,444 86 00:04:07,444 --> 00:04:10,610 Especially as we go through Quiz One, I'll definitely point out things like, 87 00:04:10,610 --> 00:04:13,380 oh, this might be something you really want to know 88 00:04:13,380 --> 00:04:15,170 or you want to have on your cheat sheet. 89 00:04:15,170 --> 00:04:18,100 I'll be telling you things as we kind of go through. 90 00:04:18,100 --> 00:04:22,079 >> So on that note, are there any things that you guys would like to go over? 91 00:04:22,079 --> 00:04:26,050 Also, if I don't have it in my deck, we can pull up something on the web, 92 00:04:26,050 --> 00:04:28,340 or we can try and make up an example on the fly. 93 00:04:28,340 --> 00:04:32,756 But any concepts that you guys are a little fuzzy on. 94 00:04:32,756 --> 00:04:33,880 [? Brian, ?] did you have-- 95 00:04:33,880 --> 00:04:36,134 >> AUDIENCE: Oh, is it cumulative? 96 00:04:36,134 --> 00:04:38,300 Like, is the stuff on Quiz Zero going to be on here? 97 00:04:38,300 --> 00:04:41,100 >> SPEAKER 1: It's cumulative, but it's much more 98 00:04:41,100 --> 00:04:44,330 of an emphasis on things since your last quiz. 99 00:04:44,330 --> 00:04:48,410 So obviously, you guys have learned about variables, and loops, 100 00:04:48,410 --> 00:04:49,380 and conditionals. 101 00:04:49,380 --> 00:04:52,030 And it's not like those just go away, because they're 102 00:04:52,030 --> 00:04:55,210 obviously inherent to everything else that we've learned now. 103 00:04:55,210 --> 00:04:58,560 104 00:04:58,560 --> 00:05:01,260 >> There might be a question on the sorts, or there 105 00:05:01,260 --> 00:05:03,710 might be a question on different searches. 106 00:05:03,710 --> 00:05:07,000 Or there might be a couple questions on maybe something you 107 00:05:07,000 --> 00:05:09,230 having to do with compiling or different errors. 108 00:05:09,230 --> 00:05:12,360 But if you've looked at Quiz One from last year, 109 00:05:12,360 --> 00:05:15,870 you'll notice that it is very much focused on things 110 00:05:15,870 --> 00:05:18,710 that have happened since your last quiz. 111 00:05:18,710 --> 00:05:20,270 >> Why is that door always locked? 112 00:05:20,270 --> 00:05:21,555 It really bothers me. 113 00:05:21,555 --> 00:05:25,310 Because I always try to go through it. 114 00:05:25,310 --> 00:05:28,450 >> So that's pretty much how the quiz will be. 115 00:05:28,450 --> 00:05:32,360 So are there any concepts that you guys would like to go over? 116 00:05:32,360 --> 00:05:35,210 Or we can jump right into just looking at Quiz One 117 00:05:35,210 --> 00:05:39,570 from last year and kind of going through practice questions there. 118 00:05:39,570 --> 00:05:41,220 We can also switch back and forth. 119 00:05:41,220 --> 00:05:43,130 This is not a one-time sort of thing. 120 00:05:43,130 --> 00:05:46,367 121 00:05:46,367 --> 00:05:46,950 Any questions? 122 00:05:46,950 --> 00:05:49,688 123 00:05:49,688 --> 00:05:50,188 Yes. 124 00:05:50,188 --> 00:05:51,950 >> AUDIENCE: Could I ask a question about another-- 125 00:05:51,950 --> 00:05:52,950 >> SPEAKER 1: Another one? 126 00:05:52,950 --> 00:05:54,500 >> AUDIENCE: This is from another year. 127 00:05:54,500 --> 00:05:54,756 >> SPEAKER 1: Yeah. 128 00:05:54,756 --> 00:05:56,068 We can also do another year. 129 00:05:56,068 --> 00:05:57,820 >> AUDIENCE: OK. 130 00:05:57,820 --> 00:05:58,710 So on 2011. 131 00:05:58,710 --> 00:06:00,135 >> SPEAKER 1: 2011. 132 00:06:00,135 --> 00:06:02,880 Ooh, that was the year before me. 133 00:06:02,880 --> 00:06:04,032 Let me pull this up. 134 00:06:04,032 --> 00:06:04,934 >> AUDIENCE: I think. 135 00:06:04,934 --> 00:06:07,920 136 00:06:07,920 --> 00:06:12,842 >> SPEAKER 1: This aspect ratio is throwing me off. 137 00:06:12,842 --> 00:06:14,435 >> AUDIENCE: 11 and 12. 138 00:06:14,435 --> 00:06:16,637 >> SPEAKER 1: 11 and 12. 139 00:06:16,637 --> 00:06:17,415 OK. 140 00:06:17,415 --> 00:06:18,790 Well, here, why don't we do this? 141 00:06:18,790 --> 00:06:22,390 You guys-- everyone just take a couple of minutes to look through quizzes. 142 00:06:22,390 --> 00:06:23,759 And we'll compile a list. 143 00:06:23,759 --> 00:06:25,050 And then we'll go through them. 144 00:06:25,050 --> 00:06:25,990 All right? 145 00:06:25,990 --> 00:06:28,530 While I get a bunch of this stuff set up. 146 00:06:28,530 --> 00:06:29,440 OK. 147 00:06:29,440 --> 00:06:31,850 I'm just going to write things down on paper. 148 00:06:31,850 --> 00:06:47,460 149 00:06:47,460 --> 00:07:36,009 >> 2011, 11 and 12? 150 00:07:36,009 --> 00:07:38,880 I'm just going to change this display. 151 00:07:38,880 --> 00:07:39,380 OK. 152 00:07:39,380 --> 00:07:40,320 That's better. 153 00:07:40,320 --> 00:07:45,490 154 00:07:45,490 --> 00:07:47,700 This over. 155 00:07:47,700 --> 00:08:04,632 156 00:08:04,632 --> 00:08:06,130 All right. 157 00:08:06,130 --> 00:08:08,590 >> Anyone else have any that they'd like to start with? 158 00:08:08,590 --> 00:08:11,270 159 00:08:11,270 --> 00:08:20,360 Also, forgot to say, if you want more of a bunch of people talking at you 160 00:08:20,360 --> 00:08:23,182 and just giving you a crack course in the last six weeks, 161 00:08:23,182 --> 00:08:24,890 we are running a review session from 7:00 162 00:08:24,890 --> 00:08:29,580 to 8:30 tonight in Northwest B103, which also happened 163 00:08:29,580 --> 00:08:34,219 last time, where it's going to be me, Hannah, Daven, with Gabe 164 00:08:34,219 --> 00:08:38,799 and Rob kind of helping from the sidelines of just kind of throwing all 165 00:08:38,799 --> 00:08:41,630 the information that you might want to have on your cheat sheet, 166 00:08:41,630 --> 00:08:43,101 slash, know in general there. 167 00:08:43,101 --> 00:08:44,600 AUDIENCE: That's also online, right? 168 00:08:44,600 --> 00:08:45,766 SPEAKER 1: It's also online. 169 00:08:45,766 --> 00:08:48,500 So you can also stay out of this icky weather, go to your dorm, 170 00:08:48,500 --> 00:08:50,620 and live-stream it if you'd like. 171 00:08:50,620 --> 00:08:52,420 I'm sure a lot of people-- I know I would 172 00:08:52,420 --> 00:08:55,541 be doing that if I were in your shoes. 173 00:08:55,541 --> 00:08:56,040 OK. 174 00:08:56,040 --> 00:08:59,152 So why don't we start with-- while you guys-- hopefully, 175 00:08:59,152 --> 00:09:00,110 you're looking through. 176 00:09:00,110 --> 00:09:03,068 If not, I might just start working through different things on Quiz One 177 00:09:03,068 --> 00:09:05,490 from last year unless you have specific questions. 178 00:09:05,490 --> 00:09:09,740 >> We'll start with this question here about stacks. 179 00:09:09,740 --> 00:09:17,940 So does everyone remember kind of what a stack is? 180 00:09:17,940 --> 00:09:21,760 How we tend to-- can anyone just give me a general description of a stack? 181 00:09:21,760 --> 00:09:23,300 A type of data structure. 182 00:09:23,300 --> 00:09:23,900 OK. 183 00:09:23,900 --> 00:09:25,650 So it's one of the data structures that we 184 00:09:25,650 --> 00:09:30,620 expect you to be familiar with-- like stacks, arrays, queues, so forth. 185 00:09:30,620 --> 00:09:31,120 Yes. 186 00:09:31,120 --> 00:09:33,005 >> AUDIENCE: Like First In, Last Out, something. 187 00:09:33,005 --> 00:09:34,810 >> SPEAKER 1: It's First In, Last Out. 188 00:09:34,810 --> 00:09:35,310 Exactly. 189 00:09:35,310 --> 00:09:37,830 So a stack is exactly what we tend to think 190 00:09:37,830 --> 00:09:39,720 about when we think of a stack of things. 191 00:09:39,720 --> 00:09:41,024 So a stack of plates. 192 00:09:41,024 --> 00:09:43,440 The first plate you put down is going to be at the bottom, 193 00:09:43,440 --> 00:09:45,981 and it's going to be the last one that you remove at the end. 194 00:09:45,981 --> 00:09:47,180 Because they stack up. 195 00:09:47,180 --> 00:09:50,390 >> And you take the one out of the bottom, typically, things crash and break. 196 00:09:50,390 --> 00:09:53,150 So stacks you can think of just like a stack of plates. 197 00:09:53,150 --> 00:09:57,707 Or the example we always like to use is in the D halls, you have the trays, 198 00:09:57,707 --> 00:09:59,790 they're all stacked up, and you take from the top. 199 00:09:59,790 --> 00:10:02,880 So the last one in is the first one out. 200 00:10:02,880 --> 00:10:03,580 OK. 201 00:10:03,580 --> 00:10:08,850 >> So in the case here, we see we have-- I wonder 202 00:10:08,850 --> 00:10:14,050 if I can actually zoom in a little bit. 203 00:10:14,050 --> 00:10:15,730 So we have this struct here. 204 00:10:15,730 --> 00:10:16,520 Right? 205 00:10:16,520 --> 00:10:20,160 So you have some struct that has some int numbers of capacity. 206 00:10:20,160 --> 00:10:22,960 So what do we think that represents? 207 00:10:22,960 --> 00:10:26,190 This is an array with size CAPACITY. 208 00:10:26,190 --> 00:10:31,750 >> So this really represents our stack in general. 209 00:10:31,750 --> 00:10:35,980 Our stack is really just an array with some set number 210 00:10:35,980 --> 00:10:39,040 that is its max, that is, its CAPACITY. 211 00:10:39,040 --> 00:10:43,050 And then we have some int size, which will track the size of our stack 212 00:10:43,050 --> 00:10:44,990 throughout the course of when we're using it. 213 00:10:44,990 --> 00:10:45,490 OK? 214 00:10:45,490 --> 00:10:50,620 215 00:10:50,620 --> 00:10:55,850 >> In this case right here, we say that our stack size has been initialized to 0. 216 00:10:55,850 --> 00:10:58,190 So we have an empty stack right now. 217 00:10:58,190 --> 00:11:01,760 And then we want to complete the implementation of pop 218 00:11:01,760 --> 00:11:05,520 such that it returns the int at the top. 219 00:11:05,520 --> 00:11:08,150 And then if it's empty, it returns negative 1. 220 00:11:08,150 --> 00:11:09,320 OK? 221 00:11:09,320 --> 00:11:12,646 So I want you guys just to think about what we might do there. 222 00:11:12,646 --> 00:11:15,020 What might be one of the first things we'd want to check? 223 00:11:15,020 --> 00:11:17,986 224 00:11:17,986 --> 00:11:19,110 There's a great thing here. 225 00:11:19,110 --> 00:11:24,700 Whenever it says-- if there's ever some If statement, like "If stack is empty," 226 00:11:24,700 --> 00:11:29,330 or "If this returns null," or "If this is whatever"-- sorry. 227 00:11:29,330 --> 00:11:30,710 >> AUDIENCE: Write an If statement. 228 00:11:30,710 --> 00:11:31,000 >> SPEAKER 1: Right. 229 00:11:31,000 --> 00:11:31,500 You could write an If statement. 230 00:11:31,500 --> 00:11:34,100 And that's probably one of the first things you want to check. 231 00:11:34,100 --> 00:11:34,599 OK. 232 00:11:34,599 --> 00:11:36,940 Whenever they give you kind of like a corner case 233 00:11:36,940 --> 00:11:39,150 like this, that's typically one of the first things 234 00:11:39,150 --> 00:11:41,200 you're going to check in your code. 235 00:11:41,200 --> 00:11:44,220 >> Also, something to recognize for the quiz 236 00:11:44,220 --> 00:11:48,870 is if you have an inefficient way to do something, write it down. 237 00:11:48,870 --> 00:11:52,220 Because we're not grading you on design, we're 238 00:11:52,220 --> 00:11:54,840 grading you on correctness for quizzes. 239 00:11:54,840 --> 00:11:59,970 So even if you know that it's a super bad way to do something but it works, 240 00:11:59,970 --> 00:12:03,290 write it down, OK? 241 00:12:03,290 --> 00:12:06,520 >> Literally, I've seen kids last year-- if you look at last year's one, 242 00:12:06,520 --> 00:12:08,180 they're like, do the Mario pyramid. 243 00:12:08,180 --> 00:12:12,482 And they literally just did "print, hash, print, hash, hash, print, hash, 244 00:12:12,482 --> 00:12:13,242 hash, hash." 245 00:12:13,242 --> 00:12:14,040 But it's correct. 246 00:12:14,040 --> 00:12:18,490 It does what we told it to, so you can't take points off. 247 00:12:18,490 --> 00:12:20,487 If you're down to the wire, obviously, you 248 00:12:20,487 --> 00:12:23,820 want to get a more elegant solution, and the more elegant solution will probably 249 00:12:23,820 --> 00:12:27,292 be less code, which means you probably have a higher chance of being right. 250 00:12:27,292 --> 00:12:29,750 But if you're like, you know what, no, I don't have time, I 251 00:12:29,750 --> 00:12:32,640 know this is a solution, it's not the best, write it. 252 00:12:32,640 --> 00:12:36,130 And you can even write on the side, I know this is terrible, 253 00:12:36,130 --> 00:12:38,135 but I know it works. 254 00:12:38,135 --> 00:12:39,320 So just a heads-up. 255 00:12:39,320 --> 00:12:39,820 OK. 256 00:12:39,820 --> 00:12:42,690 >> So obviously, first thing is this If statement. 257 00:12:42,690 --> 00:12:45,320 So what do we think this If statement is going to be? 258 00:12:45,320 --> 00:12:50,860 And actually, let me open up my great Sublime editor. 259 00:12:50,860 --> 00:12:55,480 I'm going to code this and then show you guys when it's done. 260 00:12:55,480 --> 00:12:57,370 So int pop. 261 00:12:57,370 --> 00:13:02,260 262 00:13:02,260 --> 00:13:03,240 OK. 263 00:13:03,240 --> 00:13:07,330 So if we have this int pop void-- so obviously, 264 00:13:07,330 --> 00:13:13,270 this is some function that takes in nothing and returns an integer, right? 265 00:13:13,270 --> 00:13:18,930 So it's just going to talk about this stack that we've created. 266 00:13:18,930 --> 00:13:23,360 >> So just translating this last sentence, "if stack is empty, 267 00:13:23,360 --> 00:13:28,380 pop should return negative 1," how might we do that? 268 00:13:28,380 --> 00:13:30,250 So If what? 269 00:13:30,250 --> 00:13:35,080 How do we know that the stack is empty? 270 00:13:35,080 --> 00:13:36,800 It's just size equals 0. 271 00:13:36,800 --> 00:13:40,300 Stack.size equals 0. 272 00:13:40,300 --> 00:13:42,840 Also, I'm kind of writing blind here. 273 00:13:42,840 --> 00:13:49,550 So if I make any typos-- oh, also forgot to mention this. 274 00:13:49,550 --> 00:13:54,310 >> Last week, I totally figured out what our problem was with JavaScript. 275 00:13:54,310 --> 00:13:57,270 Don't capitalize I and D. You should only 276 00:13:57,270 --> 00:14:00,070 capitalize I and get element by Id. 277 00:14:00,070 --> 00:14:02,170 And that was the entire error. 278 00:14:02,170 --> 00:14:05,700 So if you guys go back and actually look through my code, 279 00:14:05,700 --> 00:14:09,590 if you just make everything a lowercase d, "forget element by Id," 280 00:14:09,590 --> 00:14:10,410 it will work. 281 00:14:10,410 --> 00:14:12,823 So as I promised, I was very close. 282 00:14:12,823 --> 00:14:15,570 I just capitalized something I wasn't supposed to, which 283 00:14:15,570 --> 00:14:19,231 I'm sure you guys had in your last PSET. 284 00:14:19,231 --> 00:14:22,750 >> So if our size equals 0, what are we supposed to do? 285 00:14:22,750 --> 00:14:25,100 >> AUDIENCE: Return negative 1. 286 00:14:25,100 --> 00:14:28,644 >> SPEAKER 1: Return negative 1. 287 00:14:28,644 --> 00:14:29,450 Cool. 288 00:14:29,450 --> 00:14:33,050 So now what do we do? 289 00:14:33,050 --> 00:14:39,420 290 00:14:39,420 --> 00:14:41,730 We want to pop something off. 291 00:14:41,730 --> 00:14:45,090 Now this is the case where we have something. 292 00:14:45,090 --> 00:14:48,686 Our size is greater than 1 or greater than 0, right? 293 00:14:48,686 --> 00:14:50,060 We actually have something there. 294 00:14:50,060 --> 00:14:54,390 So what we want to do is we want to return right here. 295 00:14:54,390 --> 00:14:58,860 We're going to say we want to return top of stack. 296 00:14:58,860 --> 00:15:00,940 OK? 297 00:15:00,940 --> 00:15:03,840 >> So how might we do that? 298 00:15:03,840 --> 00:15:06,410 How do we return the top of the stack? 299 00:15:06,410 --> 00:15:09,700 Remember, our stack is just an array, right? 300 00:15:09,700 --> 00:15:12,690 It has some capacity that we've given it. 301 00:15:12,690 --> 00:15:17,840 As we've put things on-- or actually, we're going to write "push" after this. 302 00:15:17,840 --> 00:15:21,563 If things are in there-- maybe a picture will help. 303 00:15:21,563 --> 00:15:30,845 304 00:15:30,845 --> 00:15:32,120 Hm, I like big chalk better. 305 00:15:32,120 --> 00:15:35,920 306 00:15:35,920 --> 00:15:41,000 >> So if-- and you can just think of this as our stack. 307 00:15:41,000 --> 00:15:48,510 So this is 0, 1, 2. 308 00:15:48,510 --> 00:15:51,175 I don't know why my boxes are getting smaller. 309 00:15:51,175 --> 00:15:52,540 But there. 310 00:15:52,540 --> 00:15:53,180 OK. 311 00:15:53,180 --> 00:15:59,960 >> So this is some stack with a capacity of 5. 312 00:15:59,960 --> 00:16:04,470 And let's say we have something here and we have something here. 313 00:16:04,470 --> 00:16:10,840 So at this point, our size is equal to what? 314 00:16:10,840 --> 00:16:12,720 2. 315 00:16:12,720 --> 00:16:15,370 And if we want to pop something off of it, 316 00:16:15,370 --> 00:16:18,720 that means we want to get rid of this right here. 317 00:16:18,720 --> 00:16:21,300 OK? 318 00:16:21,300 --> 00:16:25,633 So how would we return this value? 319 00:16:25,633 --> 00:16:27,397 >> AUDIENCE: Size minus 1. 320 00:16:27,397 --> 00:16:28,230 >> SPEAKER 1: Right. 321 00:16:28,230 --> 00:16:35,010 So size minus 1, or you could do size minus minus. 322 00:16:35,010 --> 00:16:36,740 But we can't just do size. 323 00:16:36,740 --> 00:16:38,560 It's inside a struct, right? 324 00:16:38,560 --> 00:16:42,690 So stack.struct. 325 00:16:42,690 --> 00:16:44,510 or stack.size. 326 00:16:44,510 --> 00:16:47,100 That's our index right there. 327 00:16:47,100 --> 00:16:48,070 OK? 328 00:16:48,070 --> 00:16:56,240 >> And then if we look back at our struct here, 329 00:16:56,240 --> 00:16:59,770 we notice that to actually access the array, 330 00:16:59,770 --> 00:17:04,920 we need to do stack.numbers to actually access that array. 331 00:17:04,920 --> 00:17:09,859 332 00:17:09,859 --> 00:17:13,700 So that returns the top of the stack. 333 00:17:13,700 --> 00:17:18,950 >> But something that-- if you guys look at the answer key, this is all they 334 00:17:18,950 --> 00:17:21,470 give you. 335 00:17:21,470 --> 00:17:22,640 So this is it. 336 00:17:22,640 --> 00:17:27,450 But I might argue that they're miss-- ooh, not what I wanted. 337 00:17:27,450 --> 00:17:30,864 I might argue that they're missing something, because-- yes. 338 00:17:30,864 --> 00:17:32,030 AUDIENCE: Decrease the size. 339 00:17:32,030 --> 00:17:32,738 SPEAKER 1: Right. 340 00:17:32,738 --> 00:17:34,634 We need to decrease our size now. 341 00:17:34,634 --> 00:17:36,550 So what's tricky about this is once you return 342 00:17:36,550 --> 00:17:38,780 something, your function's going exit, right? 343 00:17:38,780 --> 00:17:44,050 So what we might do is actually decrease our size first. 344 00:17:44,050 --> 00:17:47,110 And then we can just do stack.size. 345 00:17:47,110 --> 00:17:52,110 >> We can just use stack.size as our index, couldn't we? 346 00:17:52,110 --> 00:17:55,650 Because if stack.size is 2, we decrement it. 347 00:17:55,650 --> 00:17:58,520 And so it's 1, which is our actual indice. 348 00:17:58,520 --> 00:18:03,320 Once we get rid of this, we actually only have one thing in our array. 349 00:18:03,320 --> 00:18:04,204 So it does both. 350 00:18:04,204 --> 00:18:05,120 AUDIENCE: [INAUDIBLE]? 351 00:18:05,120 --> 00:18:09,186 352 00:18:09,186 --> 00:18:14,750 >> SPEAKER 1: Well, in this case, we just returned the element 353 00:18:14,750 --> 00:18:19,500 at the top that is at our size that's at the top. 354 00:18:19,500 --> 00:18:25,530 But we haven't done anything to the size after we removed that element. 355 00:18:25,530 --> 00:18:29,970 This hasn't changed our size yet. 356 00:18:29,970 --> 00:18:32,410 This does actually inherently change our size. 357 00:18:32,410 --> 00:18:39,840 >> But to make it a little clearer, you could do stack.size minus minus. 358 00:18:39,840 --> 00:18:46,960 For me, it makes it, I don't know, a little more clear that-- ah, 359 00:18:46,960 --> 00:18:51,080 that you're actually changing the size and returning it. 360 00:18:51,080 --> 00:18:54,050 The other way was fine, because you were changing and returning 361 00:18:54,050 --> 00:18:55,700 at the same time. 362 00:18:55,700 --> 00:19:00,792 But this is just another way to write it that I wanted to show you guys. 363 00:19:00,792 --> 00:19:02,190 Does that make sense? 364 00:19:02,190 --> 00:19:04,940 365 00:19:04,940 --> 00:19:06,847 Stacks, I think, are much easier than queues. 366 00:19:06,847 --> 00:19:08,188 [LAUGHS] 367 00:19:08,188 --> 00:19:09,481 368 00:19:09,481 --> 00:19:09,980 OK. 369 00:19:09,980 --> 00:19:12,000 Did you have any specific questions about 11 and 12, or just-- 370 00:19:12,000 --> 00:19:13,736 >> AUDIENCE: I think it was the stack. 371 00:19:13,736 --> 00:19:14,910 >> SPEAKER 1: The stack size? 372 00:19:14,910 --> 00:19:15,430 >> AUDIENCE: Yeah. 373 00:19:15,430 --> 00:19:15,980 >> SPEAKER 1: Gotcha. 374 00:19:15,980 --> 00:19:16,490 OK. 375 00:19:16,490 --> 00:19:19,630 Yeah, you could do it in one of these two ways. 376 00:19:19,630 --> 00:19:20,320 OK. 377 00:19:20,320 --> 00:19:22,790 >> So now we have the other one. 378 00:19:22,790 --> 00:19:30,250 379 00:19:30,250 --> 00:19:34,220 This is really weird, like not having this on my screen. 380 00:19:34,220 --> 00:19:46,480 381 00:19:46,480 --> 00:19:51,030 So if we look here, we now want to complete the implementation of push, 382 00:19:51,030 --> 00:19:57,030 which actually puts something, goes and inserts something into our queue, 383 00:19:57,030 --> 00:19:59,390 or into our stack. 384 00:19:59,390 --> 00:20:00,140 OK? 385 00:20:00,140 --> 00:20:03,560 >> So if we notice, we have a couple of things. 386 00:20:03,560 --> 00:20:07,730 We have our great-- if all else, whenever we have one of these, 387 00:20:07,730 --> 00:20:10,610 you guys should at least get half a point, because all you have to do 388 00:20:10,610 --> 00:20:15,760 is convert this into C. And they give you half of it. 389 00:20:15,760 --> 00:20:18,560 So "if a stack is full or it is negative, 390 00:20:18,560 --> 00:20:20,940 then push should return false." 391 00:20:20,940 --> 00:20:21,890 OK? 392 00:20:21,890 --> 00:20:27,900 >> So we only want positive integers to be on our stack. 393 00:20:27,900 --> 00:20:28,430 All right? 394 00:20:28,430 --> 00:20:31,135 And this is going to either return true or false. 395 00:20:31,135 --> 00:20:36,410 396 00:20:36,410 --> 00:20:37,765 So we should [INAUDIBLE] bool. 397 00:20:37,765 --> 00:20:43,000 398 00:20:43,000 --> 00:20:43,660 OK. 399 00:20:43,660 --> 00:20:45,725 So translating. 400 00:20:45,725 --> 00:20:47,146 >> AUDIENCE: [INAUDIBLE]. 401 00:20:47,146 --> 00:20:48,318 >> SPEAKER 1: Oh, thanks. 402 00:20:48,318 --> 00:20:49,572 Yeah. 403 00:20:49,572 --> 00:20:52,834 This is why I have you guys. 404 00:20:52,834 --> 00:20:53,810 Int n. 405 00:20:53,810 --> 00:20:54,310 Perfect. 406 00:20:54,310 --> 00:20:56,590 Because we're putting something on to our stack. 407 00:20:56,590 --> 00:20:58,750 We actually need some number. 408 00:20:58,750 --> 00:20:59,250 OK. 409 00:20:59,250 --> 00:21:05,669 >> So just taking that last line that they gave us, our kind of corner cases, 410 00:21:05,669 --> 00:21:06,960 what do you think we have here? 411 00:21:06,960 --> 00:21:08,730 Obviously, we're going to have some If. 412 00:21:08,730 --> 00:21:09,520 OK. 413 00:21:09,520 --> 00:21:14,380 So we said, If our stack is full or if n is negative. 414 00:21:14,380 --> 00:21:18,850 So probably easiest to start with, so if n is less than 0 415 00:21:18,850 --> 00:21:23,960 or-- remember your nice AND and OR operators there. 416 00:21:23,960 --> 00:21:27,340 >> How would we check to see if our stack is full? 417 00:21:27,340 --> 00:21:28,368 What happens? 418 00:21:28,368 --> 00:21:30,080 >> AUDIENCE: Size equal CAPACITY. 419 00:21:30,080 --> 00:21:30,871 >> SPEAKER 1: Exactly. 420 00:21:30,871 --> 00:21:35,430 421 00:21:35,430 --> 00:21:36,817 Perfect. 422 00:21:36,817 --> 00:21:37,316 OK. 423 00:21:37,316 --> 00:21:40,672 And what do we want to do there? 424 00:21:40,672 --> 00:21:41,740 >> AUDIENCE: Return false. 425 00:21:41,740 --> 00:21:43,990 >> SPEAKER 1: Return false. 426 00:21:43,990 --> 00:21:44,960 Perfect. 427 00:21:44,960 --> 00:21:47,000 So halfway there, right? 428 00:21:47,000 --> 00:21:50,370 At the very least, you guys will have half of this. 429 00:21:50,370 --> 00:21:53,280 So let's think about this. 430 00:21:53,280 --> 00:21:57,100 431 00:21:57,100 --> 00:21:59,030 >> So this is the stack we start with. 432 00:21:59,030 --> 00:22:02,340 We're trying to put some element on here. 433 00:22:02,340 --> 00:22:05,580 >> We have a size that's equal to 2 right now. 434 00:22:05,580 --> 00:22:09,060 We have a capacity that equals to 5. 435 00:22:09,060 --> 00:22:13,470 And let's say we have some n that's equal to 3. 436 00:22:13,470 --> 00:22:18,170 So we need to input 3 into this space. 437 00:22:18,170 --> 00:22:22,320 And we need to input it into index 2. 438 00:22:22,320 --> 00:22:22,820 Right? 439 00:22:22,820 --> 00:22:31,730 440 00:22:31,730 --> 00:22:35,130 >> So how might we do this? 441 00:22:35,130 --> 00:22:46,580 Trying to assign-- we want to access our array. 442 00:22:46,580 --> 00:22:49,300 So stack.numbers. 443 00:22:49,300 --> 00:22:52,932 But at what indice would we want this to be? 444 00:22:52,932 --> 00:22:54,408 >> AUDIENCE: [INAUDIBLE]. 445 00:22:54,408 --> 00:22:56,806 >> SPEAKER 1: Yeah. 446 00:22:56,806 --> 00:22:57,305 Stack.size. 447 00:22:57,305 --> 00:23:02,720 448 00:23:02,720 --> 00:23:04,830 And we want that to equal n, right? 449 00:23:04,830 --> 00:23:05,752 >> AUDIENCE: [INAUDIBLE]. 450 00:23:05,752 --> 00:23:07,710 SPEAKER 1: I was actually just looking at that. 451 00:23:07,710 --> 00:23:09,865 I think that is wrong. 452 00:23:09,865 --> 00:23:14,564 >> AUDIENCE: Don't you want the space past the current size? 453 00:23:14,564 --> 00:23:18,842 >> SPEAKER 1: Well, if we look here, our size at this point is 2. 454 00:23:18,842 --> 00:23:25,600 We want it to go into index 2, because this is the top of our stack. 455 00:23:25,600 --> 00:23:29,480 So I would say that it should be stack.size, 456 00:23:29,480 --> 00:23:32,380 and then you would increase stack.size. 457 00:23:32,380 --> 00:23:33,820 Does everyone agree? 458 00:23:33,820 --> 00:23:35,780 I think I agree with that. 459 00:23:35,780 --> 00:23:38,180 You guys can argue with me if you'd like. 460 00:23:38,180 --> 00:23:40,078 Or not argue, but we can discuss this. 461 00:23:40,078 --> 00:23:42,518 >> AUDIENCE: [INAUDIBLE]. 462 00:23:42,518 --> 00:23:47,650 >> SPEAKER 1: Mm-hm, is where it's open because of zero indexing. 463 00:23:47,650 --> 00:23:50,027 So whereas in the first part, I thought they were wrong, 464 00:23:50,027 --> 00:23:53,910 and they were actually right, this part, I actually think they were wrong. 465 00:23:53,910 --> 00:23:55,990 I would say you should definitely input it here. 466 00:23:55,990 --> 00:23:58,540 I think we all agree we have a size of 2. 467 00:23:58,540 --> 00:24:01,480 That's exactly where we want our new number to go. 468 00:24:01,480 --> 00:24:05,120 So stack.numbers at stack.size should equal n. 469 00:24:05,120 --> 00:24:09,510 And then we would increment the stack.size. 470 00:24:09,510 --> 00:24:11,099 ++. 471 00:24:11,099 --> 00:24:12,390 And then what do we want to do? 472 00:24:12,390 --> 00:24:17,400 There's one last thing we have to do if we successfully inserted this? 473 00:24:17,400 --> 00:24:19,190 Return true. 474 00:24:19,190 --> 00:24:20,071 And now you're done. 475 00:24:20,071 --> 00:24:23,440 476 00:24:23,440 --> 00:24:27,765 So I feel like it was the whole minus minus stack.size and stack.size ++ that 477 00:24:27,765 --> 00:24:29,440 messed things up. 478 00:24:29,440 --> 00:24:29,940 Ugh. 479 00:24:29,940 --> 00:24:34,400 I keep-- I have this knee-jerk tendency to always hit 480 00:24:34,400 --> 00:24:38,060 Control, S, because I never want to lose any of my work. 481 00:24:38,060 --> 00:24:39,670 But I don't need to do that now. 482 00:24:39,670 --> 00:24:40,170 OK. 483 00:24:40,170 --> 00:24:41,650 So that was stack. 484 00:24:41,650 --> 00:24:44,460 485 00:24:44,460 --> 00:24:46,300 Cool. 486 00:24:46,300 --> 00:24:48,970 >> Is there anything? 487 00:24:48,970 --> 00:24:51,220 Did you guys find any other questions that you'd 488 00:24:51,220 --> 00:24:52,810 like to go over from previous quizzes? 489 00:24:52,810 --> 00:24:57,660 If not, we can kind of start talking through Quiz One from last year. 490 00:24:57,660 --> 00:24:58,160 Yes. 491 00:24:58,160 --> 00:25:00,035 >> AUDIENCE: For the first part, I didn't really 492 00:25:00,035 --> 00:25:02,438 understand how you deleted the number. 493 00:25:02,438 --> 00:25:03,600 Isn't it still there? 494 00:25:03,600 --> 00:25:05,100 >> SPEAKER 1: The number's still there. 495 00:25:05,100 --> 00:25:11,350 But what happens is that because you've told it that the top of your stack 496 00:25:11,350 --> 00:25:16,180 is here, when you go to add something onto the stack, it'll just replace it. 497 00:25:16,180 --> 00:25:17,084 >> AUDIENCE: Oh, OK. 498 00:25:17,084 --> 00:25:17,750 SPEAKER 1: Yeah. 499 00:25:17,750 --> 00:25:21,334 So it's like he forgot and it's there type of thing even though it's there. 500 00:25:21,334 --> 00:25:22,750 AUDIENCE: [INAUDIBLE] or anything? 501 00:25:22,750 --> 00:25:23,333 SPEAKER 1: No. 502 00:25:23,333 --> 00:25:25,090 Because we're in an array, you don't have 503 00:25:25,090 --> 00:25:28,850 to worry about-- you'll just override it when you need it. 504 00:25:28,850 --> 00:25:30,560 All right. 505 00:25:30,560 --> 00:25:31,560 Any other questions? 506 00:25:31,560 --> 00:25:34,843 If not, we're just going to start working through. 507 00:25:34,843 --> 00:25:35,343 Yeah. 508 00:25:35,343 --> 00:25:40,905 >> AUDIENCE: Well, this one is on the 2013, but just going through the SQL stuff. 509 00:25:40,905 --> 00:25:41,530 SPEAKER 1: SQL? 510 00:25:41,530 --> 00:25:42,030 OK. 511 00:25:42,030 --> 00:25:45,290 512 00:25:45,290 --> 00:25:46,925 So this is 26 through 29. 513 00:25:46,925 --> 00:25:47,550 AUDIENCE: Yeah. 514 00:25:47,550 --> 00:25:48,120 SPEAKER 1: OK. 515 00:25:48,120 --> 00:25:48,620 Lovely. 516 00:25:48,620 --> 00:25:50,230 Let's pull those up. 517 00:25:50,230 --> 00:25:57,560 518 00:25:57,560 --> 00:25:59,506 Oh, look. 519 00:25:59,506 --> 00:26:02,180 Did you guys enjoy the Steve Ballmer talk, by the way? 520 00:26:02,180 --> 00:26:06,350 It was pretty hilarious, in my opinion. 521 00:26:06,350 --> 00:26:09,930 I love that every time I bring up CS50's website during section, it's like, oh, 522 00:26:09,930 --> 00:26:10,730 watch section. 523 00:26:10,730 --> 00:26:11,575 And I'm like, mm. 524 00:26:11,575 --> 00:26:13,100 >> AUDIENCE: Can we do it? 525 00:26:13,100 --> 00:26:15,060 >> SPEAKER 1: We'll do it at the end. 526 00:26:15,060 --> 00:26:15,560 How's that? 527 00:26:15,560 --> 00:26:17,476 Because I know you guys have always wanted to. 528 00:26:17,476 --> 00:26:18,573 So we'll do it at the end. 529 00:26:18,573 --> 00:26:21,884 530 00:26:21,884 --> 00:26:22,800 One questions. 531 00:26:22,800 --> 00:26:24,502 >> AUDIENCE: [INAUDIBLE]. 532 00:26:24,502 --> 00:26:25,960 SPEAKER 1: It'll be a meta section. 533 00:26:25,960 --> 00:26:27,332 AUDIENCE: It's like 5 seconds. 534 00:26:27,332 --> 00:26:28,805 AUDIENCE: Really? 535 00:26:28,805 --> 00:26:30,567 AUDIENCE: It's like 20 seconds. 536 00:26:30,567 --> 00:26:33,260 [INTERPOSING VOICES] 537 00:26:33,260 --> 00:26:35,756 [LAUGHTER] 538 00:26:35,756 --> 00:26:35,931 539 00:26:35,931 --> 00:26:37,680 SPEAKER 1: I like that you've already done 540 00:26:37,680 --> 00:26:40,130 it, which is what makes me happier. 541 00:26:40,130 --> 00:26:42,340 OK. 542 00:26:42,340 --> 00:26:42,840 26. 543 00:26:42,840 --> 00:26:43,340 OK. 544 00:26:43,340 --> 00:26:46,050 545 00:26:46,050 --> 00:26:48,930 Actually, we're just going to mirror this now, because I can't. 546 00:26:48,930 --> 00:26:49,640 This is annoying. 547 00:26:49,640 --> 00:26:56,200 548 00:26:56,200 --> 00:26:56,700 OK. 549 00:26:56,700 --> 00:27:00,254 Now I can see everything. 550 00:27:00,254 --> 00:27:01,230 Lovely. 551 00:27:01,230 --> 00:27:02,500 OK. 552 00:27:02,500 --> 00:27:07,300 >> So obviously, this should bring back great memories from [? CSG ?] Finance. 553 00:27:07,300 --> 00:27:09,090 Half of you about have yours graded now. 554 00:27:09,090 --> 00:27:13,370 The other half I will get to very, very soon, I promise. 555 00:27:13,370 --> 00:27:17,110 >> So we have some SQL table that's Accounts here. 556 00:27:17,110 --> 00:27:19,830 Probably similar to your guys' user tables. 557 00:27:19,830 --> 00:27:22,350 And we have some number that was a PRIMARY key 558 00:27:22,350 --> 00:27:24,750 and represents a 12-digit account number. 559 00:27:24,750 --> 00:27:27,140 And balance is how much money they have. 560 00:27:27,140 --> 00:27:30,530 And the default value is 100. 561 00:27:30,530 --> 00:27:33,470 And they give them $100 any time they open an account. 562 00:27:33,470 --> 00:27:36,132 I wish my bank did that. 563 00:27:36,132 --> 00:27:37,710 OK. 564 00:27:37,710 --> 00:27:41,110 >> So we want to complete the schema by specifying next to number in balance 565 00:27:41,110 --> 00:27:43,900 the appropriate SQL type. 566 00:27:43,900 --> 00:27:50,240 So what do you guys think would be kind of like your knee-jerk reaction 567 00:27:50,240 --> 00:27:51,380 for types for these two? 568 00:27:51,380 --> 00:27:52,180 >> AUDIENCE: Int. 569 00:27:52,180 --> 00:27:53,450 >> SPEAKER 1: An int. 570 00:27:53,450 --> 00:27:54,230 OK. 571 00:27:54,230 --> 00:27:57,490 And then for a balance? 572 00:27:57,490 --> 00:27:57,990 Decimal. 573 00:27:57,990 --> 00:27:58,490 OK. 574 00:27:58,490 --> 00:27:59,830 So decimal's right. 575 00:27:59,830 --> 00:28:01,450 Int is close. 576 00:28:01,450 --> 00:28:08,610 Kind of the big thing is this underlined 12-digit account number. 577 00:28:08,610 --> 00:28:11,460 Whenever we have something that's larger than eight digits, 578 00:28:11,460 --> 00:28:15,522 they want you to use big int, which I think is-- 579 00:28:15,522 --> 00:28:18,370 580 00:28:18,370 --> 00:28:21,605 >> AUDIENCE: Is that just SQL? 581 00:28:21,605 --> 00:28:27,290 >> SPEAKER 1: It's the only time I've heard of it is SQL-- like using a big int. 582 00:28:27,290 --> 00:28:29,330 It's just to give you a little more precision, 583 00:28:29,330 --> 00:28:33,070 making sure you're not overflowing type thing. 584 00:28:33,070 --> 00:28:35,910 In all honesty, I think this is definitely one of the ones 585 00:28:35,910 --> 00:28:38,480 where it's like, oh, it's meant for people 586 00:28:38,480 --> 00:28:41,090 who have a little more knowledge of SQL. 587 00:28:41,090 --> 00:28:44,710 We never talked about big ints in your PSET. 588 00:28:44,710 --> 00:28:48,210 So it's kind of like I'm pretty sure if you put int, it was half credit. 589 00:28:48,210 --> 00:28:51,450 >> And as I said before, there are going to be some questions that are just 590 00:28:51,450 --> 00:28:55,550 meant to trip you up, so that not everyone gets 100. 591 00:28:55,550 --> 00:28:58,326 Because of course, there are some people in CS50 592 00:28:58,326 --> 00:29:00,950 who have been doing this for like eight years, and you're like, 593 00:29:00,950 --> 00:29:01,741 what are you doing? 594 00:29:01,741 --> 00:29:03,290 But yeah. 595 00:29:03,290 --> 00:29:05,650 >> Decimal for balance was the obvious one. 596 00:29:05,650 --> 00:29:10,910 And then big int for number just because of that, like, underlined 12-digit. 597 00:29:10,910 --> 00:29:16,760 Whenever anything is in bold or it's underlined, like, pay attention to it. 598 00:29:16,760 --> 00:29:18,940 We're not doing that just because. 599 00:29:18,940 --> 00:29:20,290 OK. 600 00:29:20,290 --> 00:29:24,400 >> So "suppose that the bank imposes a $20 monthly fee on all accounts. 601 00:29:24,400 --> 00:29:27,850 With what query could the bank deduct $20 from every account 602 00:29:27,850 --> 00:29:31,490 even if it results from some negative balances?" 603 00:29:31,490 --> 00:29:35,220 So let's start simple. 604 00:29:35,220 --> 00:29:39,240 We have four main commands for SQL, which are? 605 00:29:39,240 --> 00:29:42,620 Do you guys remember the four big ones that I told you? 606 00:29:42,620 --> 00:29:45,710 >> UPDATE, DELETE, SELECT, and INSERT. 607 00:29:45,710 --> 00:29:46,430 Perfect. 608 00:29:46,430 --> 00:29:46,930 Got it. 609 00:29:46,930 --> 00:29:52,910 So what do we think might be most useful for this one? 610 00:29:52,910 --> 00:29:54,231 We are-- UPDATE. 611 00:29:54,231 --> 00:29:54,730 Exactly. 612 00:29:54,730 --> 00:29:59,330 So UPDATE, because we're looking at accounts that already exist. 613 00:29:59,330 --> 00:30:03,570 >> So remember our general sort of thing is update. 614 00:30:03,570 --> 00:30:06,300 And then what comes after UPDATE? 615 00:30:06,300 --> 00:30:09,780 We have some table that we're referencing. 616 00:30:09,780 --> 00:30:17,130 So if I pull up Sublime again-- UPDATE. 617 00:30:17,130 --> 00:30:18,820 So this is a general format. 618 00:30:18,820 --> 00:30:20,800 So we have UPDATE table. 619 00:30:20,800 --> 00:30:23,203 And then what's the next thing we have? 620 00:30:23,203 --> 00:30:26,230 >> You can do WHERE. 621 00:30:26,230 --> 00:30:30,530 And then we have some qualifier here. 622 00:30:30,530 --> 00:30:32,300 And then what do we have next? 623 00:30:32,300 --> 00:30:38,020 We have some SET-- something equal to something. 624 00:30:38,020 --> 00:30:38,870 Blah equals blah. 625 00:30:38,870 --> 00:30:41,859 626 00:30:41,859 --> 00:30:42,400 Updated blah. 627 00:30:42,400 --> 00:30:45,655 628 00:30:45,655 --> 00:30:47,050 Right? 629 00:30:47,050 --> 00:30:52,280 So that's like the general idea for some update. 630 00:30:52,280 --> 00:30:53,060 OK? 631 00:30:53,060 --> 00:30:56,890 >> So let's start filling this in with what it should be. 632 00:30:56,890 --> 00:30:59,640 So what table are we talking about? 633 00:30:59,640 --> 00:31:02,190 Accounts. 634 00:31:02,190 --> 00:31:05,360 And then in this case, what's really cool 635 00:31:05,360 --> 00:31:08,620 is are we talking about specific users within our account, 636 00:31:08,620 --> 00:31:11,240 or are we talking about all users? 637 00:31:11,240 --> 00:31:12,490 We're talking about all users. 638 00:31:12,490 --> 00:31:16,870 So in that case, we can just delete this WHERE qualifier. 639 00:31:16,870 --> 00:31:20,500 The WHERE is only when you want to limit what you're changing. 640 00:31:20,500 --> 00:31:23,500 If you want to change the entire table, you can just drop that. 641 00:31:23,500 --> 00:31:27,390 642 00:31:27,390 --> 00:31:27,960 OK. 643 00:31:27,960 --> 00:31:29,430 >> So now we're at our SET part. 644 00:31:29,430 --> 00:31:31,514 What do we want to change? 645 00:31:31,514 --> 00:31:33,502 >> AUDIENCE: Balance. 646 00:31:33,502 --> 00:31:34,747 >> SPEAKER 1: Balance. 647 00:31:34,747 --> 00:31:37,080 And we're going to-- how are we going to change balance? 648 00:31:37,080 --> 00:31:40,230 649 00:31:40,230 --> 00:31:43,370 We have a $20 fee, right? 650 00:31:43,370 --> 00:31:45,210 Minus 20. 651 00:31:45,210 --> 00:31:48,130 And they said it's OK if we have negative balances. 652 00:31:48,130 --> 00:31:51,630 So in this case, you don't need to do any further error checking. 653 00:31:51,630 --> 00:31:53,040 OK? 654 00:31:53,040 --> 00:31:56,130 That is literally all you have to do. 655 00:31:56,130 --> 00:31:57,990 Cool? 656 00:31:57,990 --> 00:31:59,260 OK. 657 00:31:59,260 --> 00:32:01,460 >> So let's go through the next one. 658 00:32:01,460 --> 00:32:04,510 "With what SQL query could the bank retrieve the account numbers 659 00:32:04,510 --> 00:32:08,300 of its richest customers with balances greater than $1,000?" 660 00:32:08,300 --> 00:32:11,820 If your bank and your richest customers are just the ones with over $1,000, 661 00:32:11,820 --> 00:32:14,130 I feel like you're doing something wrong. 662 00:32:14,130 --> 00:32:16,080 Like, how does a bank survive on that? 663 00:32:16,080 --> 00:32:18,930 Like, that interest, not quite enough. 664 00:32:18,930 --> 00:32:20,911 OK. 665 00:32:20,911 --> 00:32:23,660 With this sort of thing, what do you think, of our four functions, 666 00:32:23,660 --> 00:32:25,970 do you think we're going to use? 667 00:32:25,970 --> 00:32:27,048 SELECT. 668 00:32:27,048 --> 00:32:27,548 Perfect. 669 00:32:27,548 --> 00:32:31,300 670 00:32:31,300 --> 00:32:32,460 SELECT. 671 00:32:32,460 --> 00:32:33,300 OK. 672 00:32:33,300 --> 00:32:40,150 So we're selecting some identifier FROM table. 673 00:32:40,150 --> 00:32:44,070 And then WHERE condition. 674 00:32:44,070 --> 00:32:46,110 So as we go through these, I'm just going 675 00:32:46,110 --> 00:32:49,680 to give you guys a general refresher on the general format of all of these. 676 00:32:49,680 --> 00:32:54,600 >> So we're selecting some sort of [? thing. ?] In this case, 677 00:32:54,600 --> 00:32:59,050 this is going to be typically some column. 678 00:32:59,050 --> 00:33:02,870 So in our case, we have number, balance. 679 00:33:02,870 --> 00:33:06,020 In your guys' case, maybe it was ID, name, password. 680 00:33:06,020 --> 00:33:10,760 Any of those columns, that's typically what we're selecting. 681 00:33:10,760 --> 00:33:14,785 >> So in this case here, which kind of column, what identifier 682 00:33:14,785 --> 00:33:17,700 are we pulling from our table? 683 00:33:17,700 --> 00:33:21,020 What does it ask us for? 684 00:33:21,020 --> 00:33:24,510 It asks us for our numbers, right? 685 00:33:24,510 --> 00:33:26,236 The account numbers. 686 00:33:26,236 --> 00:33:30,500 So remember, just number in this case. 687 00:33:30,500 --> 00:33:33,910 If you guys ever had the error, that bright orange box would come up. 688 00:33:33,910 --> 00:33:37,040 And it was like, unexpected row in, like, whatever. 689 00:33:37,040 --> 00:33:39,840 That's because you called your table wrong, right? 690 00:33:39,840 --> 00:33:43,660 So be sure that you are calling the column correctly. 691 00:33:43,660 --> 00:33:48,670 >> So from table, our table is called accounts. 692 00:33:48,670 --> 00:33:51,460 Again, make sure you're referencing your table correctly. 693 00:33:51,460 --> 00:33:52,990 And then what's our condition here? 694 00:33:52,990 --> 00:33:55,672 What are we looking for? 695 00:33:55,672 --> 00:33:56,680 We want our rich people. 696 00:33:56,680 --> 00:33:57,180 Yes. 697 00:33:57,180 --> 00:34:00,580 So balance greater than 1,000. 698 00:34:00,580 --> 00:34:02,020 There you go. 699 00:34:02,020 --> 00:34:03,760 That's your query. 700 00:34:03,760 --> 00:34:06,430 Not too bad, right? 701 00:34:06,430 --> 00:34:07,160 OK. 702 00:34:07,160 --> 00:34:09,350 >> So now for our last one. 703 00:34:09,350 --> 00:34:11,370 With that, we're breezing through. 704 00:34:11,370 --> 00:34:12,170 OK. 705 00:34:12,170 --> 00:34:16,280 So now "with what query could the bank close, i.e., delete"-- 706 00:34:16,280 --> 00:34:19,760 don't you love how they just gave you DELETE-- 707 00:34:19,760 --> 00:34:24,060 "every account that has a balance of 0?" 708 00:34:24,060 --> 00:34:26,899 I think we can all agree we're probably going to be using DELETE. 709 00:34:26,899 --> 00:34:29,330 I'm welcome to take any discussion around that. 710 00:34:29,330 --> 00:34:33,560 But I think it's pretty safe to say that we could use DELETE. 711 00:34:33,560 --> 00:34:42,600 >> So we're going to DELETE some users from table, WHERE condition. 712 00:34:42,600 --> 00:34:45,480 So same sort of thing as SELECT. 713 00:34:45,480 --> 00:34:48,389 714 00:34:48,389 --> 00:34:49,840 So in this case-- oh, sorry. 715 00:34:49,840 --> 00:34:51,570 I should DELETE FROM. 716 00:34:51,570 --> 00:34:54,320 Because DELETE will delete the entire row. 717 00:34:54,320 --> 00:34:58,300 So we don't need to talk about which specific things about a user we're 718 00:34:58,300 --> 00:34:59,280 deleting. 719 00:34:59,280 --> 00:35:02,870 If we delete a user, we're deleting everything about them. 720 00:35:02,870 --> 00:35:07,230 >> So in this case, we just need to fill in our WHERE and our condition. 721 00:35:07,230 --> 00:35:13,050 So our table is just our accounts table, as we've been doing. 722 00:35:13,050 --> 00:35:14,931 And then what is our condition here? 723 00:35:14,931 --> 00:35:16,655 >> AUDIENCE: Balance. 724 00:35:16,655 --> 00:35:18,330 >> SPEAKER 1: Balance equals 0. 725 00:35:18,330 --> 00:35:19,401 Correct. 726 00:35:19,401 --> 00:35:19,900 All good. 727 00:35:19,900 --> 00:35:20,322 Yes. 728 00:35:20,322 --> 00:35:21,238 >> AUDIENCE: [INAUDIBLE]. 729 00:35:21,238 --> 00:35:25,745 730 00:35:25,745 --> 00:35:30,530 >> SPEAKER 1: So DELETE inherently deletes everything. 731 00:35:30,530 --> 00:35:42,380 So whereas with SELECT, SELECT takes in certain columns that you want returned. 732 00:35:42,380 --> 00:35:46,680 If you wanted everything about a user returned, you use star. 733 00:35:46,680 --> 00:35:49,040 Star says, return to me everything from this user. 734 00:35:49,040 --> 00:35:54,430 Or if you put WHERE star, that means just give me everything. 735 00:35:54,430 --> 00:35:56,520 >> But DELETE inherently has that. 736 00:35:56,520 --> 00:36:01,569 So if you say, DELETE from accounts where balance equals 0, 737 00:36:01,569 --> 00:36:02,610 it's going to go through. 738 00:36:02,610 --> 00:36:05,890 And everywhere that balance is equal to 0, it inherently does that. 739 00:36:05,890 --> 00:36:07,470 It'll delete everything. 740 00:36:07,470 --> 00:36:15,120 The star is more for-- with SELECT or UPDATE, 741 00:36:15,120 --> 00:36:20,320 it's talking more about the individual fields corresponding to a user. 742 00:36:20,320 --> 00:36:25,530 >> So when you do SELECT star from table WHERE ID 743 00:36:25,530 --> 00:36:31,130 equals 1, that returns to you everything about the user with ID 1. 744 00:36:31,130 --> 00:36:38,980 If you were to do SELECT number or SELECT ID comma name from users 745 00:36:38,980 --> 00:36:43,110 WHERE ID equals 1, that just returns their number 746 00:36:43,110 --> 00:36:44,880 or their ID and their name. 747 00:36:44,880 --> 00:36:45,540 OK? 748 00:36:45,540 --> 00:36:49,420 >> So we use star in that case to give us everything about a user. 749 00:36:49,420 --> 00:36:51,940 And DELETE inherently does that. 750 00:36:51,940 --> 00:36:56,950 It doesn't just delete the ID, or just the name, or whatever. 751 00:36:56,950 --> 00:36:57,550 Yeah, no. 752 00:36:57,550 --> 00:36:59,780 It just deletes the entire user. 753 00:36:59,780 --> 00:37:05,210 So we don't have to worry about the star there. 754 00:37:05,210 --> 00:37:09,170 >> If you wanted to delete everything, you could do that, 755 00:37:09,170 --> 00:37:11,970 and this'll just drop everything. 756 00:37:11,970 --> 00:37:14,290 But typically, you don't want to do that. 757 00:37:14,290 --> 00:37:16,940 But if you ever want to delete everything, you could do that. 758 00:37:16,940 --> 00:37:18,890 OK. 759 00:37:18,890 --> 00:37:22,120 >> Have you guys found any other questions? 760 00:37:22,120 --> 00:37:22,620 Yes. 761 00:37:22,620 --> 00:37:24,316 >> AUDIENCE: Could you do 26? 762 00:37:24,316 --> 00:37:27,090 >> SPEAKER 1: 26 we just did. 763 00:37:27,090 --> 00:37:28,090 AUDIENCE: Oh, 25, sorry. 764 00:37:28,090 --> 00:37:30,721 SPEAKER 1: 25. 765 00:37:30,721 --> 00:37:31,220 OK. 766 00:37:31,220 --> 00:37:31,870 25. 767 00:37:31,870 --> 00:37:32,650 Oh. 768 00:37:32,650 --> 00:37:33,691 They're doing JavaScript. 769 00:37:33,691 --> 00:37:37,265 Oh, I can't believe it. 770 00:37:37,265 --> 00:37:38,765 I'm going to mess us up again, guys. 771 00:37:38,765 --> 00:37:42,180 772 00:37:42,180 --> 00:37:43,330 OK. 773 00:37:43,330 --> 00:37:44,440 We got this. 774 00:37:44,440 --> 00:37:46,172 We're going to make this work. 775 00:37:46,172 --> 00:37:48,190 Ah, jQuery. 776 00:37:48,190 --> 00:37:49,380 Fun. 777 00:37:49,380 --> 00:37:49,880 OK. 778 00:37:49,880 --> 00:37:54,040 >> So we are going to complete the implementation of the web page 779 00:37:54,040 --> 00:37:57,820 in such a way that if they visit, input their name, and submit the form, 780 00:37:57,820 --> 00:38:03,810 they have this great alert window that is going to appear. 781 00:38:03,810 --> 00:38:08,410 So anything with alerts, we know that we're going to have to use JavaScript. 782 00:38:08,410 --> 00:38:11,190 Yay, JavaScript. 783 00:38:11,190 --> 00:38:14,070 And if they fail to input a name before submitting the form, 784 00:38:14,070 --> 00:38:15,110 no alert should appear. 785 00:38:15,110 --> 00:38:17,900 So you can use jQuery, but you don't have to. 786 00:38:17,900 --> 00:38:22,390 >> So I'm going to let you guys work on this just for a couple minutes. 787 00:38:22,390 --> 00:38:24,050 Think about it. 788 00:38:24,050 --> 00:38:26,050 And then we will code it. 789 00:38:26,050 --> 00:38:29,260 790 00:38:29,260 --> 00:38:33,610 Slash I am going to try and think of the best way to explain this answer to you. 791 00:38:33,610 --> 00:38:49,945 792 00:38:49,945 --> 00:38:50,470 All right. 793 00:38:50,470 --> 00:38:53,150 We'll get it set up. 794 00:38:53,150 --> 00:38:53,920 OK. 795 00:38:53,920 --> 00:38:56,370 >> So we're just going to walk through this, 796 00:38:56,370 --> 00:38:59,010 because I know jQuery is a little confusing. 797 00:38:59,010 --> 00:39:01,710 JavaScript. 798 00:39:01,710 --> 00:39:03,542 It's a little out there. 799 00:39:03,542 --> 00:39:05,875 So what we're going to do is [INAUDIBLE] actually solved 800 00:39:05,875 --> 00:39:07,250 this problem with jQuery. 801 00:39:07,250 --> 00:39:09,490 So if you remember, jQuery is just a library 802 00:39:09,490 --> 00:39:11,520 that's just built on top of JavaScript. 803 00:39:11,520 --> 00:39:14,750 It's meant to make your life a little bit easier. 804 00:39:14,750 --> 00:39:17,660 >> I would definitely, if you have time, look over 805 00:39:17,660 --> 00:39:20,510 a little bit of the documentation for jQuery. 806 00:39:20,510 --> 00:39:23,230 But it's very similar to JavaScript. 807 00:39:23,230 --> 00:39:27,830 It's got similar paradigms, where you use this dot operator to call functions 808 00:39:27,830 --> 00:39:31,720 on objects that you have. 809 00:39:31,720 --> 00:39:36,970 >> So what we have here is for jQuery, you should always have this sort 810 00:39:36,970 --> 00:39:43,010 of wrapper, which is the $(document).ready(function) function 811 00:39:43,010 --> 00:39:45,510 in parentheses here. 812 00:39:45,510 --> 00:39:46,070 OK. 813 00:39:46,070 --> 00:39:48,700 So this might be something you want on your cheat sheet. 814 00:39:48,700 --> 00:39:52,070 Because at the very least, you can have the wrapper for your jQuery. 815 00:39:52,070 --> 00:39:57,190 And it'll at least get you part ways there. 816 00:39:57,190 --> 00:40:00,110 >> So in the same way that we talked a lot about JavaScript, 817 00:40:00,110 --> 00:40:04,560 and we would start by looking at what we're trying to edit 818 00:40:04,560 --> 00:40:06,150 or what we're trying to change. 819 00:40:06,150 --> 00:40:09,050 And that would typically be the first thing that we'd put in. 820 00:40:09,050 --> 00:40:18,270 So in this case, if we go back to this here and we look at our form here, 821 00:40:18,270 --> 00:40:21,790 we have this inputs, right, this id of inputs. 822 00:40:21,790 --> 00:40:24,780 And we know that based on our question, we 823 00:40:24,780 --> 00:40:27,970 are either going to throw an error if this is empty, 824 00:40:27,970 --> 00:40:31,720 or we're going to put this into an alert, right? 825 00:40:31,720 --> 00:40:36,670 So probably safe to say that we're going to be dealing with inputs here. 826 00:40:36,670 --> 00:40:41,770 >> jQuery is very similar in that if we think back to CSS, anything with an ID 827 00:40:41,770 --> 00:40:44,760 we look at with a hash. 828 00:40:44,760 --> 00:40:48,310 Anything with class we looked at with a dot, right? 829 00:40:48,310 --> 00:40:53,210 So in the same way here, what we're talking about is inputs. 830 00:40:53,210 --> 00:40:56,920 We saw that inputs is an ID within our form. 831 00:40:56,920 --> 00:41:00,850 So we're going to refer to it as such. 832 00:41:00,850 --> 00:41:01,653 Inputs. 833 00:41:01,653 --> 00:41:02,460 OK? 834 00:41:02,460 --> 00:41:04,480 >> So when in doubt, even if you have nowhere, 835 00:41:04,480 --> 00:41:09,100 if you're really unsure about how to approach this, A, have this wrapper. 836 00:41:09,100 --> 00:41:11,780 So start there with the wrapper for jQuery. 837 00:41:11,780 --> 00:41:16,330 B, make sure that you know what you are changing. 838 00:41:16,330 --> 00:41:18,830 And when in doubt, put that in something, 839 00:41:18,830 --> 00:41:21,390 and do dot, whatever you think you should be doing with it. 840 00:41:21,390 --> 00:41:22,150 >> AUDIENCE: What is the wrapper? 841 00:41:22,150 --> 00:41:24,160 >> SPEAKER 1: The wrapper is this first line. 842 00:41:24,160 --> 00:41:27,520 So this says, wait until my document is ready. 843 00:41:27,520 --> 00:41:29,160 Then start messing with things. 844 00:41:29,160 --> 00:41:30,890 OK? 845 00:41:30,890 --> 00:41:32,780 So we know we're messing with inputs. 846 00:41:32,780 --> 00:41:35,450 So we're like, OK, inputs is our form. 847 00:41:35,450 --> 00:41:37,220 That's our ID. 848 00:41:37,220 --> 00:41:39,800 So that's what we're going to be looking for. 849 00:41:39,800 --> 00:41:45,220 And we want to see what happens when this form is submitted, right? 850 00:41:45,220 --> 00:41:50,180 >> So as we might think, we have some .submit. 851 00:41:50,180 --> 00:41:56,080 So .submit just says, OK, this form that we've referenced with hash inputs, 852 00:41:56,080 --> 00:41:59,594 when it's submitted, execute something else. 853 00:41:59,594 --> 00:42:01,135 So we're going to have some function. 854 00:42:01,135 --> 00:42:05,010 855 00:42:05,010 --> 00:42:07,270 Notice we have anonymous functions here. 856 00:42:07,270 --> 00:42:09,090 We have an anonymous function here. 857 00:42:09,090 --> 00:42:09,590 Yes. 858 00:42:09,590 --> 00:42:14,880 >> AUDIENCE: Will it always be .submit, or is it just because in the HTML, 859 00:42:14,880 --> 00:42:18,260 we made the button type Submit? 860 00:42:18,260 --> 00:42:23,680 >> SPEAKER 1: It should be .submit, because it's some form that we are submitting. 861 00:42:23,680 --> 00:42:27,750 Because this .submit doesn't necessarily correspond to what the button is like 862 00:42:27,750 --> 00:42:28,430 to call. 863 00:42:28,430 --> 00:42:32,610 But the fact that it's a form that we're submitting. 864 00:42:32,610 --> 00:42:33,140 OK. 865 00:42:33,140 --> 00:42:35,015 >> So notice anonymous function here that says-- 866 00:42:35,015 --> 00:42:37,348 AUDIENCE: I don't really understand anonymous functions. 867 00:42:37,348 --> 00:42:39,240 SPEAKER 1: So anonymous functions, they're 868 00:42:39,240 --> 00:42:42,380 just functions that don't have a name. 869 00:42:42,380 --> 00:42:49,560 So instead of having some main function that calls-- we could say, If document 870 00:42:49,560 --> 00:42:52,380 ready, then we would call inputs. 871 00:42:52,380 --> 00:42:57,470 And then, When submitted, call-- like, If so-and-so submitted, 872 00:42:57,470 --> 00:42:59,550 we'd call some other function. 873 00:42:59,550 --> 00:43:01,840 An anonymous function just doesn't have a name, 874 00:43:01,840 --> 00:43:05,120 and it will just execute within these sorts of things. 875 00:43:05,120 --> 00:43:10,970 >> So in this case, what this says is, When our document is ready, 876 00:43:10,970 --> 00:43:12,880 execute this function. 877 00:43:12,880 --> 00:43:17,110 This function is composed of everything within here. 878 00:43:17,110 --> 00:43:24,410 And then the next layer in is, OK, when inputs is submitted, execute this. 879 00:43:24,410 --> 00:43:28,329 It's just a way of denoting what should be executed when. 880 00:43:28,329 --> 00:43:30,120 It's literally just a function that doesn't 881 00:43:30,120 --> 00:43:32,860 have a name, so they tend to be nested into each other, 882 00:43:32,860 --> 00:43:35,132 because you can't call them by name. 883 00:43:35,132 --> 00:43:36,984 OK? 884 00:43:36,984 --> 00:43:39,775 Literally, an anonymous function is just a function without a name. 885 00:43:39,775 --> 00:43:41,470 That's it. 886 00:43:41,470 --> 00:43:47,370 Just a way to encapsulate different things 887 00:43:47,370 --> 00:43:49,250 that we want executed at specific times. 888 00:43:49,250 --> 00:43:51,164 >> AUDIENCE: [INAUDIBLE]. 889 00:43:51,164 --> 00:43:53,420 >> SPEAKER 1: The vent? 890 00:43:53,420 --> 00:43:54,360 Where? 891 00:43:54,360 --> 00:43:57,510 >> AUDIENCE: In the parentheses after function? 892 00:43:57,510 --> 00:44:00,660 >> SPEAKER 1: After functions. 893 00:44:00,660 --> 00:44:01,290 Which one? 894 00:44:01,290 --> 00:44:02,410 >> AUDIENCE: Both. 895 00:44:02,410 --> 00:44:05,137 If you put, like, event, or if you put an argument in there, 896 00:44:05,137 --> 00:44:07,372 would it be wrong? 897 00:44:07,372 --> 00:44:12,592 >> SPEAKER 1: In this case, I suppose you could put an argument in there. 898 00:44:12,592 --> 00:44:15,619 But because we don't need an argument, there shouldn't be. 899 00:44:15,619 --> 00:44:17,410 Like, it's the whole, like, you should only 900 00:44:17,410 --> 00:44:19,810 have things that are necessary sort of thing. 901 00:44:19,810 --> 00:44:29,325 In the same way that if we had any function that has int main void-- like, 902 00:44:29,325 --> 00:44:35,687 with int main void, you could do int arg v, whatever. 903 00:44:35,687 --> 00:44:38,270 But if you don't need those, you shouldn't have them in there. 904 00:44:38,270 --> 00:44:39,070 Same thing. 905 00:44:39,070 --> 00:44:40,896 Emma, did you have a question? 906 00:44:40,896 --> 00:44:41,671 OK. 907 00:44:41,671 --> 00:44:42,170 Cool. 908 00:44:42,170 --> 00:44:42,670 OK. 909 00:44:42,670 --> 00:44:45,862 So what we have so far is when our document's ready, execute this. 910 00:44:45,862 --> 00:44:47,570 Now we're looking at when it's submitted. 911 00:44:47,570 --> 00:44:51,530 So now we actually have sort of the meat of our thing. 912 00:44:51,530 --> 00:45:00,120 So we need to get the value that the user submitted, whether that be a name 913 00:45:00,120 --> 00:45:01,340 or empty. 914 00:45:01,340 --> 00:45:07,270 And then we need to either return this alert or throw an error. 915 00:45:07,270 --> 00:45:08,560 Either way. 916 00:45:08,560 --> 00:45:13,690 >> So how do we declare a variable in JavaScript? 917 00:45:13,690 --> 00:45:14,670 Var. 918 00:45:14,670 --> 00:45:19,430 So let's say, var name equals-- or actually, we'll 919 00:45:19,430 --> 00:45:21,960 do value since I think it's name. 920 00:45:21,960 --> 00:45:23,920 OK. 921 00:45:23,920 --> 00:45:31,320 So we have some value here that we want to pull from our inputs form. 922 00:45:31,320 --> 00:45:34,100 So in the same way that we kind of did inputs, 923 00:45:34,100 --> 00:45:38,380 can anyone tell me what we might be calling within here 924 00:45:38,380 --> 00:45:41,230 if we wanted to get the name from the form? 925 00:45:41,230 --> 00:45:46,929 And if we look back at our form here, we notice that we have an ID of name. 926 00:45:46,929 --> 00:45:47,845 AUDIENCE: [INAUDIBLE]. 927 00:45:47,845 --> 00:45:51,026 928 00:45:51,026 --> 00:45:54,675 >> SPEAKER 1: You don't have to think of it as an array. 929 00:45:54,675 --> 00:45:57,650 930 00:45:57,650 --> 00:46:00,760 You don't need to worry about that. 931 00:46:00,760 --> 00:46:03,250 So we just have some name. 932 00:46:03,250 --> 00:46:09,022 And then because JavaScript and things are weird, we can't just do "name." 933 00:46:09,022 --> 00:46:11,230 We actually have to call some function that gives us, 934 00:46:11,230 --> 00:46:14,160 like, the value of that identifier. 935 00:46:14,160 --> 00:46:15,160 OK? 936 00:46:15,160 --> 00:46:16,780 So that is just .val. 937 00:46:16,780 --> 00:46:18,356 >> AUDIENCE: [INAUDIBLE]. 938 00:46:18,356 --> 00:46:19,160 >> SPEAKER 1: OK. 939 00:46:19,160 --> 00:46:27,030 So with JavaScript, this sort of thing right here just goes through. 940 00:46:27,030 --> 00:46:35,730 And you could think of this as-- it's like almost a getElementById, where 941 00:46:35,730 --> 00:46:40,630 getElementById would actually return the full thing to you. 942 00:46:40,630 --> 00:46:49,930 The $("#name") is just like, OK, I have it, I can access it, 943 00:46:49,930 --> 00:46:52,265 but if you actually want it to return the value to you-- 944 00:46:52,265 --> 00:46:53,596 >> AUDIENCE: Is it like a pointer? 945 00:46:53,596 --> 00:46:55,070 >> SPEAKER 1: It's kind of like a pointer. 946 00:46:55,070 --> 00:46:57,069 You could think of it as kind of like a pointer. 947 00:46:57,069 --> 00:46:59,060 Like, it says, OK, I know how to get there, 948 00:46:59,060 --> 00:47:02,330 but to actually get the value out of it, you have to explicitly ask for it. 949 00:47:02,330 --> 00:47:07,800 And unfortunately, obviously, we don't do too much with JavaScript and jQuery. 950 00:47:07,800 --> 00:47:12,160 >> So it's going to come down to how much you guys-- what you happen to know, 951 00:47:12,160 --> 00:47:17,610 what you happen to have seen, and what you can kind of logically guess at. 952 00:47:17,610 --> 00:47:21,639 Because some of these things, you could probably get halfway there. 953 00:47:21,639 --> 00:47:24,180 But to get it completely right, I think this problem probably 954 00:47:24,180 --> 00:47:25,624 had one of the lowest scores. 955 00:47:25,624 --> 00:47:27,790 AUDIENCE: Like, for example, for [INAUDIBLE] inputs, 956 00:47:27,790 --> 00:47:29,450 why don't you have to put val there? 957 00:47:29,450 --> 00:47:30,450 SPEAKER 1: For this one? 958 00:47:30,450 --> 00:47:31,124 AUDIENCE: Yeah. 959 00:47:31,124 --> 00:47:31,790 SPEAKER 1: Yeah. 960 00:47:31,790 --> 00:47:33,498 Because this is kind of like, as we said, 961 00:47:33,498 --> 00:47:37,180 some pointer, and this is me referencing it and actually getting us the value. 962 00:47:37,180 --> 00:47:38,872 >> AUDIENCE: Whereas [INAUDIBLE]? 963 00:47:38,872 --> 00:47:42,060 >> SPEAKER 1: For inputs, inputs is just some-- 964 00:47:42,060 --> 00:47:44,450 it's like we're just looking at it as a whole, 965 00:47:44,450 --> 00:47:48,859 and this .submit tells us what we need to know. 966 00:47:48,859 --> 00:47:50,150 jQuery and JavaScript is weird. 967 00:47:50,150 --> 00:47:50,800 I know. 968 00:47:50,800 --> 00:47:51,690 I know, guys. 969 00:47:51,690 --> 00:47:52,460 I know. 970 00:47:52,460 --> 00:47:55,260 There's a reason I don't do web programming. 971 00:47:55,260 --> 00:47:56,230 Just kidding. 972 00:47:56,230 --> 00:47:57,780 I haven't had enough exposure to it. 973 00:47:57,780 --> 00:47:59,070 >> AUDIENCE: [INAUDIBLE] CSS. 974 00:47:59,070 --> 00:48:00,206 >> [LAUGHTER] 975 00:48:00,206 --> 00:48:00,590 >> SPEAKER 1: OK, that's CSS. 976 00:48:00,590 --> 00:48:03,730 CSS is just, like, make things pretty, it's not like let's deal with data. 977 00:48:03,730 --> 00:48:05,710 There's a difference there. 978 00:48:05,710 --> 00:48:06,350 OK. 979 00:48:06,350 --> 00:48:12,110 So now hopefully, this part-- also, for a problem like this, where you may not 980 00:48:12,110 --> 00:48:16,070 know the syntax, you could be like, OK, you could have some var value 981 00:48:16,070 --> 00:48:18,320 and be like, assume I got the value correctly. 982 00:48:18,320 --> 00:48:20,600 And then you could probably write this next part. 983 00:48:20,600 --> 00:48:24,640 This next part will probably seem much more easy, 984 00:48:24,640 --> 00:48:26,610 or it would make logical sense to you guys. 985 00:48:26,610 --> 00:48:31,520 >> Whereas what we've written so far, I know I would be like, umm, yeah, 986 00:48:31,520 --> 00:48:34,340 I don't know that on the quiz. 987 00:48:34,340 --> 00:48:37,610 You can totally write psuedocode and then break into real code 988 00:48:37,610 --> 00:48:40,460 when you-- it's kind of like math. 989 00:48:40,460 --> 00:48:42,904 If you've ever had some multi-part problem where 990 00:48:42,904 --> 00:48:44,820 you don't know how to get past the first step, 991 00:48:44,820 --> 00:48:46,736 but everything else relies on that first step, 992 00:48:46,736 --> 00:48:49,577 you just say, like, assume x equals 4. 993 00:48:49,577 --> 00:48:50,910 And then you run with it, right? 994 00:48:50,910 --> 00:48:52,410 >> You can do the same thing with code. 995 00:48:52,410 --> 00:48:55,740 So you could say, assume I got the value right. 996 00:48:55,740 --> 00:48:58,290 Then do this next part here. 997 00:48:58,290 --> 00:49:02,610 Because this part actually should be pretty logical. 998 00:49:02,610 --> 00:49:06,560 >> So we need to check if our value is empty. 999 00:49:06,560 --> 00:49:11,650 Or if it's some name, actually give an alert. 1000 00:49:11,650 --> 00:49:15,085 So what might we want to check? 1001 00:49:15,085 --> 00:49:17,800 We want to have some If. 1002 00:49:17,800 --> 00:49:19,510 What do you think our If is going to be? 1003 00:49:19,510 --> 00:49:24,141 We need to check to see if value is-- if it's not an empty string. 1004 00:49:24,141 --> 00:49:30,740 1005 00:49:30,740 --> 00:49:31,730 >> Perfect. 1006 00:49:31,730 --> 00:49:32,570 Exactly. 1007 00:49:32,570 --> 00:49:33,070 OK. 1008 00:49:33,070 --> 00:49:36,320 So if that's the case, what do we want to do? 1009 00:49:36,320 --> 00:49:39,190 We want to throw some alert, right? 1010 00:49:39,190 --> 00:49:41,090 So it's just alert. 1011 00:49:41,090 --> 00:49:43,050 And then whatever we want inside. 1012 00:49:43,050 --> 00:49:45,430 So what are we supposed to have inside our alert? 1013 00:49:45,430 --> 00:49:48,916 >> AUDIENCE: "Hello." 1014 00:49:48,916 --> 00:49:50,421 >> SPEAKER 1: Comma. 1015 00:49:50,421 --> 00:49:52,462 And then do you guys remember how to concatenate? 1016 00:49:52,462 --> 00:49:53,400 >> AUDIENCE: Plus. 1017 00:49:53,400 --> 00:49:54,520 >> SPEAKER 1: Plus. 1018 00:49:54,520 --> 00:49:57,490 So we want to have plus. 1019 00:49:57,490 --> 00:50:03,480 And then we need another plus for our exclamation point. 1020 00:50:03,480 --> 00:50:05,620 Cool. 1021 00:50:05,620 --> 00:50:06,540 OK. 1022 00:50:06,540 --> 00:50:08,255 Otherwise, what might we want to do? 1023 00:50:08,255 --> 00:50:10,380 We don't want to do anything, right? 1024 00:50:10,380 --> 00:50:14,190 So we just return false. 1025 00:50:14,190 --> 00:50:17,200 Not what I want to do. 1026 00:50:17,200 --> 00:50:18,520 OK. 1027 00:50:18,520 --> 00:50:20,660 >> And then these, of course-- what's important 1028 00:50:20,660 --> 00:50:23,510 is to remember to close your anonymous functions. 1029 00:50:23,510 --> 00:50:27,230 So if you notice, we have some function here that ends here. 1030 00:50:27,230 --> 00:50:31,710 So we need to close this first one and add a semicolon. 1031 00:50:31,710 --> 00:50:35,195 And then this one here has to do with this anonymous function, 1032 00:50:35,195 --> 00:50:38,650 so we close it there. 1033 00:50:38,650 --> 00:50:40,190 That knee-jerk reaction of saving. 1034 00:50:40,190 --> 00:50:40,690 Yes. 1035 00:50:40,690 --> 00:50:45,650 AUDIENCE: Is there a reason that-- just in all the examples in JavaScript 1036 00:50:45,650 --> 00:50:48,480 that I've seen, we have the first curly braces 1037 00:50:48,480 --> 00:50:51,075 on the same line as the function? 1038 00:50:51,075 --> 00:50:53,040 >> SPEAKER 1: That's stylistic. 1039 00:50:53,040 --> 00:50:54,130 Yeah. 1040 00:50:54,130 --> 00:51:00,650 And actually, if you take 61, we do all our curly braces that way. 1041 00:51:00,650 --> 00:51:01,830 Huh? 1042 00:51:01,830 --> 00:51:05,030 >> AUDIENCE: If we did do that on the PSET, is that bad for style? 1043 00:51:05,030 --> 00:51:05,720 >> SPEAKER 1: No. 1044 00:51:05,720 --> 00:51:09,710 We have explicitly told you guys to do this. 1045 00:51:09,710 --> 00:51:14,540 But actually, this is kind of the dominating convention 1046 00:51:14,540 --> 00:51:19,620 in a lot of C classes and JavaScript and jQuery. 1047 00:51:19,620 --> 00:51:23,074 So obviously, I'm sure-- and actually, if I 1048 00:51:23,074 --> 00:51:26,770 were to do this problem completely blind without having looked at it, 1049 00:51:26,770 --> 00:51:30,080 there is probably very few ways I would have gotten this. 1050 00:51:30,080 --> 00:51:36,160 >> I might have been like, OK, maybe I need to grab something that's an input. 1051 00:51:36,160 --> 00:51:38,020 And I think having the knowledge that, like, 1052 00:51:38,020 --> 00:51:43,240 OK, if I want to get something from a form that has an ID, 1053 00:51:43,240 --> 00:51:46,990 I refer to it in this way, I'd probably get that. 1054 00:51:46,990 --> 00:51:49,940 I might get this. 1055 00:51:49,940 --> 00:51:53,400 But really, the only thing that I think I would really get is this right here. 1056 00:51:53,400 --> 00:51:59,230 >> So it's kind of important to know, as with any difficult test, 1057 00:51:59,230 --> 00:52:02,130 you kind of choose your battles, right? 1058 00:52:02,130 --> 00:52:06,780 If you know that you're not going to get that first part for jQuery, as I said, 1059 00:52:06,780 --> 00:52:10,080 assume that you have this value and do what you can with it. 1060 00:52:10,080 --> 00:52:14,730 And if you have time, go back and try and fill in the pieces. 1061 00:52:14,730 --> 00:52:17,150 But don't let a question like this really bog you down. 1062 00:52:17,150 --> 00:52:18,080 Because what? 1063 00:52:18,080 --> 00:52:23,260 This was one question on jQuery out of the entire exam. 1064 00:52:23,260 --> 00:52:25,810 So yes, it'd be great if you got it. 1065 00:52:25,810 --> 00:52:28,297 >> AUDIENCE: [INAUDIBLE] you are welcome to use jQuery. 1066 00:52:28,297 --> 00:52:30,005 SPEAKER 1: You are welcome to use jQuery. 1067 00:52:30,005 --> 00:52:31,088 AUDIENCE: OK. [INAUDIBLE]. 1068 00:52:31,088 --> 00:52:33,060 SPEAKER 1: How would you not use it? 1069 00:52:33,060 --> 00:52:35,850 I would have to look up all my JavaScript. 1070 00:52:35,850 --> 00:52:38,810 It's actually much easier, I think, to use jQuery, 1071 00:52:38,810 --> 00:52:42,995 because jQuery actually reduces the amount of JavaScript you write probably 1072 00:52:42,995 --> 00:52:43,620 by, like, half. 1073 00:52:43,620 --> 00:52:46,334 So our code would probably be twice as long without that. 1074 00:52:46,334 --> 00:52:49,250 So if you recognize-- the biggest thing to take away from this problem 1075 00:52:49,250 --> 00:52:50,550 are the patterns. 1076 00:52:50,550 --> 00:52:52,890 If you're accessing something from a form, 1077 00:52:52,890 --> 00:52:57,830 you're going to use quotes, hash, whatever you're trying to access. 1078 00:52:57,830 --> 00:53:01,000 If it's being submitted, you want to use something like a .submit. 1079 00:53:01,000 --> 00:53:03,680 You're probably going to use an anonymous function in there. 1080 00:53:03,680 --> 00:53:08,030 If you actually need the value of something, you're going to do quotes, 1081 00:53:08,030 --> 00:53:12,160 hash, whatever that field is, .val. 1082 00:53:12,160 --> 00:53:14,690 And recognizing patterns like that are going 1083 00:53:14,690 --> 00:53:19,586 to be far more useful than really knowing the nitty gritty. 1084 00:53:19,586 --> 00:53:20,562 Yes. 1085 00:53:20,562 --> 00:53:23,164 >> AUDIENCE: So if the user submits their name 1086 00:53:23,164 --> 00:53:27,394 and it runs through that statement, will it go down and return false after this? 1087 00:53:27,394 --> 00:53:30,373 >> SPEAKER 1: No, because the alert, you can think of it like a return. 1088 00:53:30,373 --> 00:53:30,873 Yeah. 1089 00:53:30,873 --> 00:53:31,590 So it'll exit. 1090 00:53:31,590 --> 00:53:31,925 Yeah. 1091 00:53:31,925 --> 00:53:33,883 >> AUDIENCE: Could you [? place ?] the parentheses 1092 00:53:33,883 --> 00:53:37,105 of submit, parentheses, something? 1093 00:53:37,105 --> 00:53:38,870 >> SPEAKER 1: I did, I believe. 1094 00:53:38,870 --> 00:53:42,300 This one's closed right here. 1095 00:53:42,300 --> 00:53:45,800 And then this one gets closed down here. 1096 00:53:45,800 --> 00:53:47,135 >> AUDIENCE: Oh, there. 1097 00:53:47,135 --> 00:53:48,470 All right. 1098 00:53:48,470 --> 00:53:53,572 >> SPEAKER 1: Obviously, little, little syntactic things 1099 00:53:53,572 --> 00:53:55,780 like that, we'll probably just circle it and be like, 1100 00:53:55,780 --> 00:53:58,385 don't forget your parentheses. 1101 00:53:58,385 --> 00:53:59,810 But yeah. 1102 00:53:59,810 --> 00:54:00,950 OK. 1103 00:54:00,950 --> 00:54:04,190 Do we have another one that we would like to do? 1104 00:54:04,190 --> 00:54:07,700 1105 00:54:07,700 --> 00:54:11,599 Did you guys get the binary tree one? 1106 00:54:11,599 --> 00:54:12,890 Do we want to go over that one? 1107 00:54:12,890 --> 00:54:17,940 I feel like binary trees and DOMs should be points 1108 00:54:17,940 --> 00:54:19,880 that you guys get pretty easily. 1109 00:54:19,880 --> 00:54:21,978 I would not want you to lose points on that. 1110 00:54:21,978 --> 00:54:28,335 1111 00:54:28,335 --> 00:54:30,370 OK. 1112 00:54:30,370 --> 00:54:33,097 >> Let's do this DOM here. 1113 00:54:33,097 --> 00:54:35,555 I wish I had my Surface so I could just draw on the screen. 1114 00:54:35,555 --> 00:54:38,330 1115 00:54:38,330 --> 00:54:41,461 That's what I used to do last year. 1116 00:54:41,461 --> 00:54:41,960 OK. 1117 00:54:41,960 --> 00:54:48,370 So as I mentioned, we talked about DOM, Document Object Model. 1118 00:54:48,370 --> 00:54:51,415 They're probably going to give you some little bit of snippet like this 1119 00:54:51,415 --> 00:54:54,250 and ask you to create a tree for it. 1120 00:54:54,250 --> 00:54:58,050 And this just all has to do with running through the tags. 1121 00:54:58,050 --> 00:55:00,685 So let's try and do this. 1122 00:55:00,685 --> 00:55:09,610 1123 00:55:09,610 --> 00:55:12,920 >> Also, as per usual, if you guys do these quizzes, as I highly 1124 00:55:12,920 --> 00:55:16,450 recommend that you do as practice, any of them that you're like, 1125 00:55:16,450 --> 00:55:19,480 I really don't get what's going on here, just, of course, 1126 00:55:19,480 --> 00:55:23,334 send me an email with a question, and I will explain as best I can. 1127 00:55:23,334 --> 00:55:25,298 OK. 1128 00:55:25,298 --> 00:55:33,420 >> We have some document, and we have HTML. 1129 00:55:33,420 --> 00:55:33,970 OK. 1130 00:55:33,970 --> 00:55:38,750 So then we're just analysing the tags here, right? 1131 00:55:38,750 --> 00:55:45,810 So what do we see comes-- what are the two subheadings under HTML? 1132 00:55:45,810 --> 00:55:48,880 Especially because they're indented properly? 1133 00:55:48,880 --> 00:55:49,580 Head and body. 1134 00:55:49,580 --> 00:56:00,298 1135 00:56:00,298 --> 00:56:00,800 Ah. 1136 00:56:00,800 --> 00:56:03,730 1137 00:56:03,730 --> 00:56:04,960 Head and body. 1138 00:56:04,960 --> 00:56:05,952 Lovely. 1139 00:56:05,952 --> 00:56:07,410 So then let's start here with head. 1140 00:56:07,410 --> 00:56:09,090 What's underneath head? 1141 00:56:09,090 --> 00:56:11,721 What's our next tag? 1142 00:56:11,721 --> 00:56:12,220 Title. 1143 00:56:12,220 --> 00:56:16,450 1144 00:56:16,450 --> 00:56:19,605 And then within title, quiz. 1145 00:56:19,605 --> 00:56:23,970 1146 00:56:23,970 --> 00:56:24,690 OK. 1147 00:56:24,690 --> 00:56:27,940 >> So literally, it's just walking through the tags, OK? 1148 00:56:27,940 --> 00:56:31,610 So then if we have body, as we go through, 1149 00:56:31,610 --> 00:56:35,460 body has three things within it, right? 1150 00:56:35,460 --> 00:56:36,400 It's got three divs. 1151 00:56:36,400 --> 00:56:40,060 1152 00:56:40,060 --> 00:56:41,900 Every div gets its own little box. 1153 00:56:41,900 --> 00:56:45,260 1154 00:56:45,260 --> 00:56:48,106 And what's in the first div? 1155 00:56:48,106 --> 00:56:48,606 [INAUDIBLE] 1156 00:56:48,606 --> 00:56:52,070 1157 00:56:52,070 --> 00:56:57,292 And then the next one is middle, which I'm just going to abbreviate. 1158 00:56:57,292 --> 00:56:59,410 And then the last one is bottom. 1159 00:56:59,410 --> 00:57:06,060 1160 00:57:06,060 --> 00:57:07,650 >> So really, it's just walking through. 1161 00:57:07,650 --> 00:57:11,140 And because we're going to give you HTML that is properly indented, 1162 00:57:11,140 --> 00:57:16,800 you can literally just run through and be like, OK, here is the HTML. 1163 00:57:16,800 --> 00:57:19,460 So this is the outermost corner, or furthest left, 1164 00:57:19,460 --> 00:57:22,410 so that has to be its own thing. 1165 00:57:22,410 --> 00:57:26,360 These are on the same indention. 1166 00:57:26,360 --> 00:57:30,120 >> So we know that head needs to be at the same level as body, 1167 00:57:30,120 --> 00:57:32,040 but underneath HTML. 1168 00:57:32,040 --> 00:57:33,100 It's what we have here. 1169 00:57:33,100 --> 00:57:36,810 Head and body are siblings, but they're under HTML. 1170 00:57:36,810 --> 00:57:39,130 And then we just go into each of these. 1171 00:57:39,130 --> 00:57:44,370 So head has title, which has quiz, as we see here. 1172 00:57:44,370 --> 00:57:48,210 And then in this case, we have body, which has these three lines. 1173 00:57:48,210 --> 00:57:51,470 So these are all siblings all under body. 1174 00:57:51,470 --> 00:57:53,732 OK? 1175 00:57:53,732 --> 00:57:56,190 So hopefully when you see something like this, you're like, 1176 00:57:56,190 --> 00:57:57,900 yes, I just get to draw a tree. 1177 00:57:57,900 --> 00:58:00,430 It's gonna be great. 1178 00:58:00,430 --> 00:58:01,130 OK. 1179 00:58:01,130 --> 00:58:03,220 So wanted to make sure you guys knew that. 1180 00:58:03,220 --> 00:58:10,510 >> So why don't we go to 32 with our binary tree? 1181 00:58:10,510 --> 00:58:17,905 Because once you understand a binary search tree, it's really not that bad. 1182 00:58:17,905 --> 00:58:20,960 So Everett, can I erase-- I mean, these are all online, too. 1183 00:58:20,960 --> 00:58:24,130 So if you have any questions. 1184 00:58:24,130 --> 00:58:44,410 1185 00:58:44,410 --> 00:58:46,370 >> Lost a little space there, but it's OK. 1186 00:58:46,370 --> 00:58:50,190 So binary search tree is just a way to organize data 1187 00:58:50,190 --> 00:58:57,000 not in an array, where the root is always greater 1188 00:58:57,000 --> 00:59:03,500 than the left child and the root is always less than the right child. 1189 00:59:03,500 --> 00:59:04,770 OK. 1190 00:59:04,770 --> 00:59:08,160 >> So we have this great search tree here. 1191 00:59:08,160 --> 00:59:13,360 We have these numbers, 34, 59, 20, 106, 36, and 52. 1192 00:59:13,360 --> 00:59:15,160 And we need to organize them in such a way 1193 00:59:15,160 --> 00:59:22,100 such that the root is greater than everything on the left side 1194 00:59:22,100 --> 00:59:24,750 and it is less than everything on the right side. 1195 00:59:24,750 --> 00:59:29,580 And typically, what you want to do is try and find 1196 00:59:29,580 --> 00:59:31,720 something about in the middle. 1197 00:59:31,720 --> 00:59:35,420 With binary search trees, you might have to play around a little bit with it. 1198 00:59:35,420 --> 00:59:40,850 >> But in this case, let's just assume that we know this first one. 1199 00:59:40,850 --> 00:59:44,870 So 36 is our first one here. 1200 00:59:44,870 --> 00:59:46,790 Typically, I would try and choose something 1201 00:59:46,790 --> 00:59:50,160 about in the middle that'll probably yield the best results. 1202 00:59:50,160 --> 00:59:56,200 So now we know that everything on this side needs to be less than 36. 1203 00:59:56,200 --> 00:59:59,520 But also, what you could see is that we have two spots over here. 1204 00:59:59,520 --> 01:00:04,350 So if we know that we need two numbers that are less than the root, 1205 01:00:04,350 --> 01:00:08,670 then you could just order these and choose the third number. 1206 01:00:08,670 --> 01:00:09,390 OK. 1207 01:00:09,390 --> 01:00:11,550 >> So why don't we start with this side? 1208 01:00:11,550 --> 01:00:17,270 So if we know that everything on this side needs to be less than 36-- 1209 01:00:17,270 --> 01:00:21,290 but if we look at this one, we know that this number 1210 01:00:21,290 --> 01:00:25,180 has to be less than this number, right? 1211 01:00:25,180 --> 01:00:30,810 Because this number here is on the right side. 1212 01:00:30,810 --> 01:00:34,680 And remember, just as in our larger tree, everything on the right 1213 01:00:34,680 --> 01:00:37,130 needs to be larger than the root. 1214 01:00:37,130 --> 01:00:40,540 In this way, we know that whatever is in this needs 1215 01:00:40,540 --> 01:00:43,090 to be larger than whatever's here. 1216 01:00:43,090 --> 01:00:44,020 OK? 1217 01:00:44,020 --> 01:00:46,340 >> So we have two choices for these two bubbles. 1218 01:00:46,340 --> 01:00:49,830 We have 32 and 34-- or sorry. 1219 01:00:49,830 --> 01:00:53,330 20 and 34. 1220 01:00:53,330 --> 01:00:56,500 So how do you think we might put these in? 1221 01:00:56,500 --> 01:00:59,230 If this one has to be bigger than this one, 1222 01:00:59,230 --> 01:01:04,340 that means we want to have 20 here and 34 here. 1223 01:01:04,340 --> 01:01:05,340 OK. 1224 01:01:05,340 --> 01:01:09,380 >> So in the same way, we look at this kind of sub tree. 1225 01:01:09,380 --> 01:01:14,994 And we say, OK, we have not 36. 1226 01:01:14,994 --> 01:01:20,460 We have 106, 52, and 59. 1227 01:01:20,460 --> 01:01:21,590 Right? 1228 01:01:21,590 --> 01:01:24,820 In this case here, we know that this one needs 1229 01:01:24,820 --> 01:01:29,570 to have one thing that's greater than it and one thing that is less than it. 1230 01:01:29,570 --> 01:01:30,100 Right? 1231 01:01:30,100 --> 01:01:34,580 So if we put these in order, it's just about finding that middle value again. 1232 01:01:34,580 --> 01:01:35,766 So our middle value is 59. 1233 01:01:35,766 --> 01:01:38,524 1234 01:01:38,524 --> 01:01:40,190 So then we say, OK, what's less than 59? 1235 01:01:40,190 --> 01:01:41,826 Well, that's 52. 1236 01:01:41,826 --> 01:01:42,950 And what's greater than 59? 1237 01:01:42,950 --> 01:01:45,120 Well, that's 106. 1238 01:01:45,120 --> 01:01:45,930 OK? 1239 01:01:45,930 --> 01:01:49,430 So it's just a bit like a puzzle. 1240 01:01:49,430 --> 01:01:53,160 It typically helps, especially in small trees like this, 1241 01:01:53,160 --> 01:01:56,617 if you just order it, if you know how many are on the left 1242 01:01:56,617 --> 01:01:58,450 and how many are on the right, that can just 1243 01:01:58,450 --> 01:02:01,050 allow you to choose what should be at your root. 1244 01:02:01,050 --> 01:02:02,922 And then you can kind of work from there. 1245 01:02:02,922 --> 01:02:05,080 Mm-hm. 1246 01:02:05,080 --> 01:02:05,580 33? 1247 01:02:05,580 --> 01:02:07,570 Sure. 1248 01:02:07,570 --> 01:02:10,120 OK. 1249 01:02:10,120 --> 01:02:17,410 So we want to create a node or a binary search tree. 1250 01:02:17,410 --> 01:02:20,416 And let's look at what might be in it. 1251 01:02:20,416 --> 01:02:26,130 1252 01:02:26,130 --> 01:02:28,600 And I think this will actually be our last problem. 1253 01:02:28,600 --> 01:02:29,510 Ah. 1254 01:02:29,510 --> 01:02:30,010 OK. 1255 01:02:30,010 --> 01:02:48,190 1256 01:02:48,190 --> 01:02:48,690 OK. 1257 01:02:48,690 --> 01:02:55,580 1258 01:02:55,580 --> 01:02:58,040 >> This is like what we can think of as a node, right? 1259 01:02:58,040 --> 01:03:01,610 We have some value, and we know that it will either 1260 01:03:01,610 --> 01:03:03,830 have left and right children. 1261 01:03:03,830 --> 01:03:08,420 Binary search tree-- that means that it has, at most, two children. 1262 01:03:08,420 --> 01:03:08,920 OK? 1263 01:03:08,920 --> 01:03:12,690 1264 01:03:12,690 --> 01:03:15,540 Let's think about the way we defined a linked list, right? 1265 01:03:15,540 --> 01:03:17,740 >> We had some int that represented the value. 1266 01:03:17,740 --> 01:03:20,760 We had some pointer that represented what it was going to next. 1267 01:03:20,760 --> 01:03:23,350 Binary search tree is very similar. 1268 01:03:23,350 --> 01:03:27,980 We still have some value, n, that we give it, right? 1269 01:03:27,980 --> 01:03:31,750 And then I drew these very explicitly with arrows, 1270 01:03:31,750 --> 01:03:34,010 because they are effectively pointers. 1271 01:03:34,010 --> 01:03:34,940 OK? 1272 01:03:34,940 --> 01:03:38,500 >> So n is typically what? 1273 01:03:38,500 --> 01:03:39,230 It's some value. 1274 01:03:39,230 --> 01:03:41,800 What do we typically designate it as? 1275 01:03:41,800 --> 01:03:42,500 An int. 1276 01:03:42,500 --> 01:03:49,980 So we have some int n, or int value, or whatever you'd like to call it. 1277 01:03:49,980 --> 01:03:53,230 And then we have the left child and the right child. 1278 01:03:53,230 --> 01:03:54,570 Right? 1279 01:03:54,570 --> 01:03:58,060 And we know that what they're point-- what are they pointing to? 1280 01:03:58,060 --> 01:04:01,560 They're pointing to other nodes, right? 1281 01:04:01,560 --> 01:04:07,540 So how would we declare a node pointer? 1282 01:04:07,540 --> 01:04:10,480 1283 01:04:10,480 --> 01:04:11,460 >> AUDIENCE: [INAUDIBLE]. 1284 01:04:11,460 --> 01:04:12,472 >> SPEAKER 1: Mm-hm. 1285 01:04:12,472 --> 01:04:14,305 And we're going to have two of those, right? 1286 01:04:14,305 --> 01:04:18,070 1287 01:04:18,070 --> 01:04:26,710 And then we can literally do left for the left child 1288 01:04:26,710 --> 01:04:30,950 and right for the right child. 1289 01:04:30,950 --> 01:04:31,820 Actually-- sorry. 1290 01:04:31,820 --> 01:04:33,155 Because we need struct. 1291 01:04:33,155 --> 01:04:36,250 1292 01:04:36,250 --> 01:04:41,580 >> So struct is because we haven't finished declaring a node yet. 1293 01:04:41,580 --> 01:04:47,070 So node is within here. 1294 01:04:47,070 --> 01:04:49,640 It's kind of like a recursive definition, right? 1295 01:04:49,640 --> 01:04:53,140 So when we are within this definition, we 1296 01:04:53,140 --> 01:04:57,310 haven't finished declaring node as the structure, 1297 01:04:57,310 --> 01:04:59,380 so we have to do struct node just to say, yes, 1298 01:04:59,380 --> 01:05:03,930 we are in fact referring back to ourself type thing. 1299 01:05:03,930 --> 01:05:07,410 That like once it's created, it'll be fine. 1300 01:05:07,410 --> 01:05:10,890 >> So how big do you guys think this is? 1301 01:05:10,890 --> 01:05:13,650 What's the size of this structure? 1302 01:05:13,650 --> 01:05:14,370 12. 1303 01:05:14,370 --> 01:05:14,870 Why? 1304 01:05:14,870 --> 01:05:18,990 1305 01:05:18,990 --> 01:05:19,664 Precisely. 1306 01:05:19,664 --> 01:05:21,830 And in fact, one of the first questions one Quiz One 1307 01:05:21,830 --> 01:05:24,170 is about sizes, which is why I asked. 1308 01:05:24,170 --> 01:05:25,230 All right. 1309 01:05:25,230 --> 01:05:29,350 >> We are going to end, because the DCE will yell at me if we don't. 1310 01:05:29,350 --> 01:05:33,790 But if you guys have any questions, of course, email me, let me know. 1311 01:05:33,790 --> 01:05:35,190 You guys are going to rock it. 1312 01:05:35,190 --> 01:05:37,210 I'm super excited. 1313 01:05:37,210 --> 01:05:39,380 You guys will be done as of Wednesday, and then 1314 01:05:39,380 --> 01:05:43,150 you'll get to work on your final projects, and it's going to be awesome. 1315 01:05:43,150 --> 01:05:48,784 >> I will be here next week from 4:00 to 4:30 1316 01:05:48,784 --> 01:05:50,700 if you guys want to come pick up your quizzes. 1317 01:05:50,700 --> 01:05:54,410 If you want to find me some other time or just coordinate a different time, 1318 01:05:54,410 --> 01:05:56,460 feel free to let me know. 1319 01:05:56,460 --> 01:05:58,670 Otherwise, this is our last section. 1320 01:05:58,670 --> 01:06:01,070 So it was great to see you guys. 1321 01:06:01,070 --> 01:06:02,450 Please take more candy. 1322 01:06:02,450 --> 01:06:05,390 And good luck on your quiz. 1323 01:06:05,390 --> 01:06:06,687