Welcome back, ladies and gentlemen.
Today, what we're going to do is we are going to work on lecture nine, exercise nine, actual lecture
nine, exercise that James and I have copied this from a final version.
You can read it why I'm doing this with you guys.
Well, because it's all about loops, what we have covered so far.
And we are not going to do registration.
We're going to do login process here.
And if you haven't, just don't copy the solution because it's going to be filled from the final folder.
But you can do you can copy this one.
Same here and rename it just add dash solution Dages.
OK, so I'm going to do it together with you and make sure that you linking this one in here if you
want to do it with me.
Otherwise if you already have done it, please just mark this lecture is completed.
Now let me just.
Get this one.
So this is the first thing that we need to do, create sorry, that will hold five different objects.
OK, now the VA database, that will be.
And you know how we delivery with this square brackets and inside we need to have those.
I objects just like this.
Now I'm going to use mine.
We have too much space there.
We don't need the space over here.
Just copy it again and I'm going to space it.
Two, three, four, five.
Now, let's add some details a little bit different.
So let's put it here.
James, you can get any names.
James and here should go.
Jefferson some then we have.
And the help and the then Morrey.
Nice, here it will be, Andy.
The password should remain the same.
All right, but you can change it if you want.
So, Silvio, here I will put some Italian.
Mary Lou.
So if anyone is from Italy, he they will know what I'm trying to say here, huh?
OK, now the last one.
She's here, Tom, Capital T..
Crew.
He's doing the course with those guys.
What a privilege.
OK, we are done with these.
So console lock the database.
Let's see what will have their oops nice.
So I have the database that is array of five objects.
We do have different properties and values inside.
Nice next bar is.
Gee, I'm just going to copy all of it.
And the it and I'm going to hold the option, you can hold all there in windows if you're using windows,
so from the user to enter the email and storage in a variable called user email.
So var user email and prompt enter your email address.
Very good, and we don't need that much space, is there?
Copy the same one because the number three will be for the user password and your password.
Very good.
Create a function that is called user login.
So function user login using camil notation.
And it will take two arguments from the prompt user.
Actually, we are going to say email and password.
I'll do a short version now and it will check the user data if it's in the database note use loops.
So foreach var is equal to zero I will be smaller with a database that length.
Why?
Because we can use the database the length.
We already have that information.
Cool.
So I don't need manually to put five there.
I plus plus the last statement there.
Now here what we need to do is we need to actually compare whatever we passing to the function user
login, let's call the function by the way, user login and pass user email and user password.
And user password, you know what I'm going to do now, I'm going to comment about this.
I'm going to consider log.
Log the email and I'm going to log the password just to see if we are getting the data so the variables
will hold the data with whatever data we are going to enter through the prompt and these two variables,
then we just passing as the two arguments in dysfunction and dysfunction.
We are calling outside because it needs to be called and executed in here.
We can put any name that you need and let's see if it's working Rick.
Actually, should the email address Rick at Gmail dot com pass.
Here it is.
I have.
And pass.
How cool is this?
Now that is working.
Put a comment there.
So now the Moment of Truth database.
Make sure that you put if statement database that.
Oh, sorry, not that of I need a pair of eyes, that email is equal to these email there that is coming
from our function as an argument now database, we should check and database that sorry, not DOT.
I'm thinking that I'm inside an object, that's why I'm using that all the time, password.
Now, I can't use this password because I need to use the password field that is in the object stored
in the database.
Right.
Don't confuse things, please.
And if that password is equal to the password that we are passing here, nice.
So if that is true, all is left for us to print all of these things here now.
Bastet, very good.
Make sure that you shift a little bit now here option called option and put console the lock option
or halt in windows and.
Yep.
That one.
Now here.
We need to close out.
I did hold the.
Very good.
Now, I do have this double breakfast here, and, yeah, the plus sign is missing now because I messed
it up.
OK, now Rick will come actually what happened here?
Let me just go back a little bit.
So I need actually to close it here first, I need to close the quote, she's going to hold option or
out in your system, I.
And here I do have it very good.
Now I should add, plus some.
One.
Plus here and then we need to close all of it.
Very good and semicolon at the end.
Now, Rick will come from a database of I that name.
This will get from last name.
This will be from my email address, email.
This will be for the past, but I don't know why I put a password there.
And this will be for creating that.
But anyway, we can update the passport in our created in our profile page.
OK, so if we have a hit, whether this is happening here, we got to look through all of the users
in the database and if we have a hit, whatever, we pass through the function and if we have a hit
with that one in the database, then we're going to print all of his details.
Cool.
Let's see if this is going to work.
Save it a couple of times because sometimes it might not work.
OK, so, Rick, make sure that you put in the correct email address that is in the database password.
One, two, three.
Now hit enter.
And here it is.
Now you can edit your name and things like that.
Everything is there.
OK.
OK, but what, what if I don't put the correct details.
What if I for example, I forgot my password here and I put my mistake something else.
For example Rick at Gmail, Gmail dot com, even if I make a mistake with the email, doesn't matter.
So if I pull something like this now, it won't find anything.
And if I say because I'm doing if somebody will say, we'll put out there now, cancel that log.
Please try it again and look what will happen now.
So, Rick, actually, you can put whatever you want there now, please try it again five times what
is happening.
Five, this number tells you that it's been executed five times.
Why?
Because it's going through each and every object here.
It couldn't find any hit and it's going to the statement and put it five times.
So do you think that that is a good idea to put somewhere in the form your username or your email and
password and to have like a thousand pop ups?
That's your name.
Please try it again.
Well.
So this is not ideal.
So what we can do here, we can we can fix this, we can refactor a little bit.
So how are we going to do that?
So remember that I told you that we can use the function returns and these returns when something is
found, they will hit they will stop the if statement.
And because of that, it will block the entire foreach.
It will exit of that one and the function is finished.
So let's do this and we don't need this else.
Now, if I have a hit, let's return.
True or what is better, we can return the database of the entire user object.
We can return this so we can return this.
For example, if I if I target my I will return the entire user object.
And that is the beauty of it.
Now, when I found something, I will return it.
Otherwise I can say.
Return false.
If I don't find anything in these for each loop, I can return false.
Now, this should pretty much be clear for you.
So we are returning the entire object user object when we when we have a hit.
If we don't find anything in this for loop before the function is finished, we return false so we can
use this now we can create a variable outside call user and we can copy this entire function there.
And now this will have either the entire user object, so this actually sorry, this variable user variable
will have as a value, whatever this function returns, right.
Whatever this user function returns and it will return either the entire user object or false.
So this one will be will have some data.
It will be false now, because I know this.
I can say if user.
Very good.
So if it has some data, it will go in a block, if it's false, it will not go any lower.
And that's why I can use now or else and stay alert.
Um, there is no user with these.
Details in our system.
Please try again later, please try with correct details, right, nice, and now here, whatever.
I had it here.
I can just copy it.
And I need to paste it, but.
Let me just make it like that.
And.
Very good.
Now I'm shifted and I need to uncommented this one, so, commander.
And for now, now we have a little bit problem here, we don't have access to the database of AI because
that was only strict here for this for each loop.
But we do have access to the entire user.
So I can say I can just.
Change everything.
Oops.
And I can say user that because I have access to the entire user good.
Let's save it and see now if I enter some gibberish, actually, we need to refresh it again, it was
holding the memory of the previous one.
There is no user with these details in our system.
Please try again with the correct details.
Good.
Now, let's refresh one more time and say Rick at Gmail dot com Herock password.
One, two, three.
OK, very good.
Now I do have those details.
So that is, ladies and gentlemen, now you know how to use for each loop.
Sorry for loop.
And as a challenge, I want you to do the same one with four each, OK?
Or you can do it even with wire loop.
So I choose four because that's the one that I really like to use.
But please, as a challenge, do it with something else right there.
That is pretty much it.
We don't need this part here.
OK, that was pretty much it.
If you don't understand, just please watch it again.
Make sure that you understand all of these things and because you're going to need in order to continue
in future.
So stay safe and I will see you next section.