1 00:00:00,390 --> 00:00:01,520 Welcome back, ladies and gentlemen. 2 00:00:01,650 --> 00:00:08,460 Today, what we're going to do is we are going to work on lecture nine, exercise nine, actual lecture 3 00:00:08,460 --> 00:00:12,540 nine, exercise that James and I have copied this from a final version. 4 00:00:12,840 --> 00:00:15,490 You can read it why I'm doing this with you guys. 5 00:00:15,510 --> 00:00:19,140 Well, because it's all about loops, what we have covered so far. 6 00:00:19,380 --> 00:00:21,360 And we are not going to do registration. 7 00:00:21,370 --> 00:00:23,100 We're going to do login process here. 8 00:00:23,460 --> 00:00:30,600 And if you haven't, just don't copy the solution because it's going to be filled from the final folder. 9 00:00:30,750 --> 00:00:32,930 But you can do you can copy this one. 10 00:00:32,940 --> 00:00:36,690 Same here and rename it just add dash solution Dages. 11 00:00:36,930 --> 00:00:45,330 OK, so I'm going to do it together with you and make sure that you linking this one in here if you 12 00:00:45,330 --> 00:00:46,130 want to do it with me. 13 00:00:46,140 --> 00:00:50,750 Otherwise if you already have done it, please just mark this lecture is completed. 14 00:00:51,150 --> 00:00:52,560 Now let me just. 15 00:00:55,730 --> 00:00:56,720 Get this one. 16 00:00:57,350 --> 00:01:01,970 So this is the first thing that we need to do, create sorry, that will hold five different objects. 17 00:01:02,000 --> 00:01:05,870 OK, now the VA database, that will be. 18 00:01:06,470 --> 00:01:12,820 And you know how we delivery with this square brackets and inside we need to have those. 19 00:01:13,940 --> 00:01:16,500 I objects just like this. 20 00:01:16,940 --> 00:01:18,570 Now I'm going to use mine. 21 00:01:19,490 --> 00:01:20,860 We have too much space there. 22 00:01:20,900 --> 00:01:22,570 We don't need the space over here. 23 00:01:22,850 --> 00:01:25,760 Just copy it again and I'm going to space it. 24 00:01:25,760 --> 00:01:27,620 Two, three, four, five. 25 00:01:27,950 --> 00:01:31,590 Now, let's add some details a little bit different. 26 00:01:32,150 --> 00:01:35,130 So let's put it here. 27 00:01:35,410 --> 00:01:36,770 James, you can get any names. 28 00:01:38,510 --> 00:01:41,570 James and here should go. 29 00:01:41,610 --> 00:01:44,630 Jefferson some then we have. 30 00:01:44,630 --> 00:01:50,690 And the help and the then Morrey. 31 00:01:53,930 --> 00:01:56,490 Nice, here it will be, Andy. 32 00:01:56,510 --> 00:01:58,050 The password should remain the same. 33 00:01:58,280 --> 00:02:01,230 All right, but you can change it if you want. 34 00:02:01,700 --> 00:02:04,520 So, Silvio, here I will put some Italian. 35 00:02:07,660 --> 00:02:09,800 Mary Lou. 36 00:02:11,980 --> 00:02:16,970 So if anyone is from Italy, he they will know what I'm trying to say here, huh? 37 00:02:17,310 --> 00:02:18,910 OK, now the last one. 38 00:02:20,170 --> 00:02:23,530 She's here, Tom, Capital T.. 39 00:02:25,110 --> 00:02:25,740 Crew. 40 00:02:26,910 --> 00:02:29,050 He's doing the course with those guys. 41 00:02:29,610 --> 00:02:30,540 What a privilege. 42 00:02:31,170 --> 00:02:32,430 OK, we are done with these. 43 00:02:32,790 --> 00:02:35,910 So console lock the database. 44 00:02:35,940 --> 00:02:39,090 Let's see what will have their oops nice. 45 00:02:39,210 --> 00:02:43,290 So I have the database that is array of five objects. 46 00:02:43,290 --> 00:02:45,890 We do have different properties and values inside. 47 00:02:46,200 --> 00:02:48,450 Nice next bar is. 48 00:02:50,270 --> 00:02:52,510 Gee, I'm just going to copy all of it. 49 00:02:58,230 --> 00:03:06,360 And the it and I'm going to hold the option, you can hold all there in windows if you're using windows, 50 00:03:06,600 --> 00:03:10,560 so from the user to enter the email and storage in a variable called user email. 51 00:03:10,740 --> 00:03:20,370 So var user email and prompt enter your email address. 52 00:03:22,000 --> 00:03:25,310 Very good, and we don't need that much space, is there? 53 00:03:25,510 --> 00:03:34,630 Copy the same one because the number three will be for the user password and your password. 54 00:03:36,070 --> 00:03:36,710 Very good. 55 00:03:36,730 --> 00:03:39,250 Create a function that is called user login. 56 00:03:39,640 --> 00:03:43,750 So function user login using camil notation. 57 00:03:43,960 --> 00:03:46,830 And it will take two arguments from the prompt user. 58 00:03:48,340 --> 00:03:50,890 Actually, we are going to say email and password. 59 00:03:51,560 --> 00:03:59,260 I'll do a short version now and it will check the user data if it's in the database note use loops. 60 00:03:59,500 --> 00:04:08,320 So foreach var is equal to zero I will be smaller with a database that length. 61 00:04:08,740 --> 00:04:09,250 Why? 62 00:04:09,310 --> 00:04:11,230 Because we can use the database the length. 63 00:04:11,710 --> 00:04:14,790 We already have that information. 64 00:04:15,410 --> 00:04:15,780 Cool. 65 00:04:16,480 --> 00:04:18,550 So I don't need manually to put five there. 66 00:04:18,790 --> 00:04:21,370 I plus plus the last statement there. 67 00:04:21,800 --> 00:04:29,860 Now here what we need to do is we need to actually compare whatever we passing to the function user 68 00:04:29,860 --> 00:04:36,310 login, let's call the function by the way, user login and pass user email and user password. 69 00:04:39,130 --> 00:04:45,840 And user password, you know what I'm going to do now, I'm going to comment about this. 70 00:04:46,480 --> 00:04:48,160 I'm going to consider log. 71 00:04:51,380 --> 00:05:01,130 Log the email and I'm going to log the password just to see if we are getting the data so the variables 72 00:05:01,130 --> 00:05:06,710 will hold the data with whatever data we are going to enter through the prompt and these two variables, 73 00:05:06,710 --> 00:05:11,110 then we just passing as the two arguments in dysfunction and dysfunction. 74 00:05:11,120 --> 00:05:15,260 We are calling outside because it needs to be called and executed in here. 75 00:05:15,890 --> 00:05:21,470 We can put any name that you need and let's see if it's working Rick. 76 00:05:22,870 --> 00:05:30,340 Actually, should the email address Rick at Gmail dot com pass. 77 00:05:31,160 --> 00:05:31,770 Here it is. 78 00:05:31,820 --> 00:05:32,420 I have. 79 00:05:32,780 --> 00:05:33,400 And pass. 80 00:05:33,710 --> 00:05:34,500 How cool is this? 81 00:05:34,990 --> 00:05:36,440 Now that is working. 82 00:05:36,440 --> 00:05:37,700 Put a comment there. 83 00:05:39,530 --> 00:05:42,950 So now the Moment of Truth database. 84 00:05:43,850 --> 00:05:47,420 Make sure that you put if statement database that. 85 00:05:48,800 --> 00:05:59,000 Oh, sorry, not that of I need a pair of eyes, that email is equal to these email there that is coming 86 00:05:59,000 --> 00:06:09,740 from our function as an argument now database, we should check and database that sorry, not DOT. 87 00:06:11,010 --> 00:06:15,180 I'm thinking that I'm inside an object, that's why I'm using that all the time, password. 88 00:06:15,390 --> 00:06:22,890 Now, I can't use this password because I need to use the password field that is in the object stored 89 00:06:22,890 --> 00:06:23,630 in the database. 90 00:06:23,640 --> 00:06:23,970 Right. 91 00:06:24,840 --> 00:06:26,490 Don't confuse things, please. 92 00:06:26,790 --> 00:06:32,740 And if that password is equal to the password that we are passing here, nice. 93 00:06:33,240 --> 00:06:41,400 So if that is true, all is left for us to print all of these things here now. 94 00:06:41,550 --> 00:06:45,190 Bastet, very good. 95 00:06:45,870 --> 00:06:55,620 Make sure that you shift a little bit now here option called option and put console the lock option 96 00:06:55,620 --> 00:06:58,710 or halt in windows and. 97 00:06:58,720 --> 00:06:59,120 Yep. 98 00:06:59,220 --> 00:06:59,790 That one. 99 00:07:01,170 --> 00:07:02,250 Now here. 100 00:07:05,620 --> 00:07:08,810 We need to close out. 101 00:07:09,100 --> 00:07:10,720 I did hold the. 102 00:07:15,030 --> 00:07:15,690 Very good. 103 00:07:15,720 --> 00:07:24,300 Now, I do have this double breakfast here, and, yeah, the plus sign is missing now because I messed 104 00:07:24,300 --> 00:07:24,630 it up. 105 00:07:24,840 --> 00:07:29,430 OK, now Rick will come actually what happened here? 106 00:07:30,120 --> 00:07:31,490 Let me just go back a little bit. 107 00:07:33,520 --> 00:07:41,080 So I need actually to close it here first, I need to close the quote, she's going to hold option or 108 00:07:41,080 --> 00:07:43,000 out in your system, I. 109 00:07:45,250 --> 00:07:48,380 And here I do have it very good. 110 00:07:48,810 --> 00:07:52,000 Now I should add, plus some. 111 00:07:53,380 --> 00:07:53,800 One. 112 00:07:56,510 --> 00:08:00,130 Plus here and then we need to close all of it. 113 00:08:02,660 --> 00:08:05,940 Very good and semicolon at the end. 114 00:08:05,960 --> 00:08:11,720 Now, Rick will come from a database of I that name. 115 00:08:14,290 --> 00:08:17,150 This will get from last name. 116 00:08:17,560 --> 00:08:21,330 This will be from my email address, email. 117 00:08:21,370 --> 00:08:24,250 This will be for the past, but I don't know why I put a password there. 118 00:08:24,580 --> 00:08:26,030 And this will be for creating that. 119 00:08:26,050 --> 00:08:32,950 But anyway, we can update the passport in our created in our profile page. 120 00:08:33,130 --> 00:08:37,660 OK, so if we have a hit, whether this is happening here, we got to look through all of the users 121 00:08:37,660 --> 00:08:43,060 in the database and if we have a hit, whatever, we pass through the function and if we have a hit 122 00:08:43,060 --> 00:08:47,210 with that one in the database, then we're going to print all of his details. 123 00:08:47,880 --> 00:08:48,240 Cool. 124 00:08:48,700 --> 00:08:50,040 Let's see if this is going to work. 125 00:08:50,260 --> 00:08:53,800 Save it a couple of times because sometimes it might not work. 126 00:08:54,420 --> 00:09:03,280 OK, so, Rick, make sure that you put in the correct email address that is in the database password. 127 00:09:03,670 --> 00:09:04,660 One, two, three. 128 00:09:04,660 --> 00:09:05,980 Now hit enter. 129 00:09:06,160 --> 00:09:06,900 And here it is. 130 00:09:06,910 --> 00:09:09,870 Now you can edit your name and things like that. 131 00:09:09,880 --> 00:09:10,870 Everything is there. 132 00:09:11,060 --> 00:09:11,600 OK. 133 00:09:12,330 --> 00:09:16,830 OK, but what, what if I don't put the correct details. 134 00:09:17,410 --> 00:09:23,380 What if I for example, I forgot my password here and I put my mistake something else. 135 00:09:23,590 --> 00:09:29,950 For example Rick at Gmail, Gmail dot com, even if I make a mistake with the email, doesn't matter. 136 00:09:30,190 --> 00:09:33,460 So if I pull something like this now, it won't find anything. 137 00:09:33,790 --> 00:09:40,180 And if I say because I'm doing if somebody will say, we'll put out there now, cancel that log. 138 00:09:42,450 --> 00:09:47,960 Please try it again and look what will happen now. 139 00:09:50,310 --> 00:10:00,290 So, Rick, actually, you can put whatever you want there now, please try it again five times what 140 00:10:00,510 --> 00:10:00,970 is happening. 141 00:10:01,020 --> 00:10:03,710 Five, this number tells you that it's been executed five times. 142 00:10:04,020 --> 00:10:04,320 Why? 143 00:10:04,320 --> 00:10:08,160 Because it's going through each and every object here. 144 00:10:08,400 --> 00:10:13,330 It couldn't find any hit and it's going to the statement and put it five times. 145 00:10:13,470 --> 00:10:21,510 So do you think that that is a good idea to put somewhere in the form your username or your email and 146 00:10:21,510 --> 00:10:24,540 password and to have like a thousand pop ups? 147 00:10:24,540 --> 00:10:25,200 That's your name. 148 00:10:25,200 --> 00:10:25,840 That's your name. 149 00:10:25,980 --> 00:10:26,940 Please try it again. 150 00:10:27,000 --> 00:10:27,390 Well. 151 00:10:28,730 --> 00:10:30,300 So this is not ideal. 152 00:10:30,320 --> 00:10:33,920 So what we can do here, we can we can fix this, we can refactor a little bit. 153 00:10:34,220 --> 00:10:35,450 So how are we going to do that? 154 00:10:35,900 --> 00:10:43,400 So remember that I told you that we can use the function returns and these returns when something is 155 00:10:43,760 --> 00:10:48,530 found, they will hit they will stop the if statement. 156 00:10:48,530 --> 00:10:52,640 And because of that, it will block the entire foreach. 157 00:10:52,640 --> 00:10:55,740 It will exit of that one and the function is finished. 158 00:10:56,150 --> 00:10:59,400 So let's do this and we don't need this else. 159 00:10:59,870 --> 00:11:02,960 Now, if I have a hit, let's return. 160 00:11:03,050 --> 00:11:12,320 True or what is better, we can return the database of the entire user object. 161 00:11:12,620 --> 00:11:15,980 We can return this so we can return this. 162 00:11:16,190 --> 00:11:21,500 For example, if I if I target my I will return the entire user object. 163 00:11:21,740 --> 00:11:23,350 And that is the beauty of it. 164 00:11:23,600 --> 00:11:26,990 Now, when I found something, I will return it. 165 00:11:27,500 --> 00:11:30,470 Otherwise I can say. 166 00:11:31,730 --> 00:11:33,190 Return false. 167 00:11:33,830 --> 00:11:38,780 If I don't find anything in these for each loop, I can return false. 168 00:11:39,060 --> 00:11:43,120 Now, this should pretty much be clear for you. 169 00:11:43,430 --> 00:11:50,200 So we are returning the entire object user object when we when we have a hit. 170 00:11:50,330 --> 00:11:57,290 If we don't find anything in this for loop before the function is finished, we return false so we can 171 00:11:57,290 --> 00:12:05,360 use this now we can create a variable outside call user and we can copy this entire function there. 172 00:12:07,450 --> 00:12:16,270 And now this will have either the entire user object, so this actually sorry, this variable user variable 173 00:12:16,510 --> 00:12:20,450 will have as a value, whatever this function returns, right. 174 00:12:20,510 --> 00:12:26,080 Whatever this user function returns and it will return either the entire user object or false. 175 00:12:26,680 --> 00:12:30,180 So this one will be will have some data. 176 00:12:30,400 --> 00:12:33,760 It will be false now, because I know this. 177 00:12:33,760 --> 00:12:35,080 I can say if user. 178 00:12:36,030 --> 00:12:36,510 Very good. 179 00:12:36,540 --> 00:12:42,320 So if it has some data, it will go in a block, if it's false, it will not go any lower. 180 00:12:42,600 --> 00:12:45,350 And that's why I can use now or else and stay alert. 181 00:12:47,820 --> 00:12:52,260 Um, there is no user with these. 182 00:12:53,510 --> 00:12:56,030 Details in our system. 183 00:12:58,230 --> 00:13:13,090 Please try again later, please try with correct details, right, nice, and now here, whatever. 184 00:13:13,120 --> 00:13:14,250 I had it here. 185 00:13:14,250 --> 00:13:15,350 I can just copy it. 186 00:13:17,020 --> 00:13:18,730 And I need to paste it, but. 187 00:13:20,520 --> 00:13:22,380 Let me just make it like that. 188 00:13:24,200 --> 00:13:25,100 And. 189 00:13:27,370 --> 00:13:28,040 Very good. 190 00:13:29,050 --> 00:13:33,010 Now I'm shifted and I need to uncommented this one, so, commander. 191 00:13:34,050 --> 00:13:41,550 And for now, now we have a little bit problem here, we don't have access to the database of AI because 192 00:13:41,550 --> 00:13:45,630 that was only strict here for this for each loop. 193 00:13:45,990 --> 00:13:48,150 But we do have access to the entire user. 194 00:13:48,390 --> 00:13:50,730 So I can say I can just. 195 00:13:52,780 --> 00:13:54,520 Change everything. 196 00:13:57,230 --> 00:13:57,700 Oops. 197 00:14:00,380 --> 00:14:06,560 And I can say user that because I have access to the entire user good. 198 00:14:07,540 --> 00:14:17,740 Let's save it and see now if I enter some gibberish, actually, we need to refresh it again, it was 199 00:14:17,740 --> 00:14:19,740 holding the memory of the previous one. 200 00:14:20,110 --> 00:14:22,240 There is no user with these details in our system. 201 00:14:22,250 --> 00:14:23,660 Please try again with the correct details. 202 00:14:23,970 --> 00:14:24,330 Good. 203 00:14:24,730 --> 00:14:34,000 Now, let's refresh one more time and say Rick at Gmail dot com Herock password. 204 00:14:34,450 --> 00:14:35,590 One, two, three. 205 00:14:35,860 --> 00:14:38,110 OK, very good. 206 00:14:38,410 --> 00:14:41,070 Now I do have those details. 207 00:14:41,470 --> 00:14:45,910 So that is, ladies and gentlemen, now you know how to use for each loop. 208 00:14:46,570 --> 00:14:47,490 Sorry for loop. 209 00:14:47,740 --> 00:14:52,540 And as a challenge, I want you to do the same one with four each, OK? 210 00:14:52,820 --> 00:14:55,740 Or you can do it even with wire loop. 211 00:14:55,900 --> 00:15:00,190 So I choose four because that's the one that I really like to use. 212 00:15:00,520 --> 00:15:05,430 But please, as a challenge, do it with something else right there. 213 00:15:05,610 --> 00:15:06,690 That is pretty much it. 214 00:15:06,700 --> 00:15:07,900 We don't need this part here. 215 00:15:08,430 --> 00:15:10,240 OK, that was pretty much it. 216 00:15:10,450 --> 00:15:13,570 If you don't understand, just please watch it again. 217 00:15:13,900 --> 00:15:19,300 Make sure that you understand all of these things and because you're going to need in order to continue 218 00:15:19,300 --> 00:15:19,890 in future. 219 00:15:19,900 --> 00:15:22,360 So stay safe and I will see you next section.