Welcome back.
In today's lecture, we are going to work on lecture, seminar, exercise solutions.
So why are we going to do this?
Because this one is pretty much complex.
It will involve arrays, objects, if conditions and logical operators.
So if you know how to do this and if you have done it, then Mark this lecture is finished and move
on.
If not, just create here new file called Lecture seven Exercise Solutions and link the same one here
in your index.
Right.
Because we need to check what is going to happen in the browser.
Also make sure that from a final folder, just grab this exercise lecture seven exercise judges read
the entire thing.
I just copy this, the whole thing.
And I put it here.
Right.
That's it now.
And.
Copy this for.
Put them here and let's make them as a comment, don't create a variable array that will be empty and
it will halt user objects in future.
Very good variable database.
Equal, an empty array, nice next one from the user to enter his name and store it in a very real username
var user name and prompt.
Very good.
Now inside the prompt.
We had one argument.
Call your name nice.
So copied.
This one is going to be similar because oops.
Because here we are prompting the user name.
Use it to user to enter his user email in your email address.
Now here will provide the user to get his passport and save it as a user password variable.
Right.
Nice.
OK, comes along and see what we have, so council log, they use pirnie email.
Let's start with the name user email and user password and let's see if we are going to get the right
output.
So your name?
Rick Password.
Email are at Gmail dot com and one, two, three, four.
Password hit enter.
And why don't have here?
Hmm, strange.
Save it again, sometimes this can happen.
Did I linked it correctly?
Yes, I did.
Name, Rick.
Email address are at Gmail dot com.
You can put any email address 1-3 here it is now it's working, right?
I didn't change anything.
I need to receive it.
Good.
Now, what is number five?
Number five is create a function called the register that will take three arguments from the prompt.
So function oops register.
That will take three arguments.
It will be name, email and password.
Very good.
Now this function, what it needs to do.
Very good.
Let me just do it like this field, the database with a user object that will contain the three fields.
Nice.
So we need to create an object in our database array.
So how are we going to do this database that push it inside?
We're going to create an object.
Very good.
So name it will be the name that is coming from our arguments then comma.
We will have email that will come from email.
Very good.
And you can name this whatever you want.
And we want to have a password.
Now this will be the field name inside the object right in the past is coming from the function that
we're going to pass.
Nice.
Oops.
Here needs to go semicolon now, call the register, function, the prompt, whatever we stored here,
write.
And we know that they're working because we check them, we can lock them like a minute ago, the email
and user password.
Very good.
Save it.
Save a couple of times.
Refresh it here on what's happening.
Your name, Rick, very good, are sick Gmail dot com.
OK, one, two, three, four, three, doesn't matter.
Let's check the database.
Mm hmm.
I do see that some object is created and there you go.
We do have our first object stored in a database array.
Congratulations.
So let me just do this console like I hate with this out of complete control, the lock, the database
array.
Now, I don't need to type it here all of the time.
OK, now what we are going to do is we are going to move on.
So six is done, seven canceling the prom and see what will happen.
Do you get database with null or empty values?
How we can fix this.
Nice.
So let's cancel the proms.
Cancel.
Here is I do have an array with no object that is crazy, right?
Why?
Because this function was called and doesn't matter what was inside here, it was nothing, actually.
And it was called with the with those empty values.
And we pushed into the database, all of those things.
So now we need to fix it.
How are we going to fix it?
So I'm going to do like this seven, eight and nine.
We are going to do in one go.
So we're going to do if conditions with this condition, we're going to check if the user name is different,
then no, very good.
And we need to use the logical operator end user email is going to be different then no end user password
needs to be.
Different than no, very good what this means that I want each and every of these variables to have
some data.
That's why these and otherwise, if you put or hear the pipe then means that these two needs to be filled.
And this one not in.
That is not what we want.
So here it will go.
Only if this contains data.
And if not, we're going to have this condition and we're going to alert the user and say some of the
form fields.
Actually, we don't have form, but let's do it are empty.
Please try again.
Very good.
Now, here the logic goes like this.
Any time we're going to call this register function is only when these variables hold any meaningful
data, they will not be known.
Right now, let's hit it here, refresh it and cancel everything first.
Here it is.
Some of the fields are empty.
Please try it again.
Yeah.
You need to clear a couple of times, probably now our alert is working because we were trying to register
and now the database, if we try to access that database, it will not work because this alert is there.
Right?
That database is empty.
OK, now let's do a real Rick or am Gmail dot com and very good.
We have a database.
Get our M Gmail dot com.
One, two, three, four.
Nice.
So that is pretty much what we want to do in the six, seven and eight.
In nine, actually, they put it like that, six, seven, eight, nine.
So how we can fix it, we already fix it.
Try saving the database user objects only when.
Yeah, this was number 10 as well.
Oh, we have covered a lot.
Now check if the name of the user object.
So the name of this object.
So if it's righ right.
An object in a database is the same with the name of the user name that we got from the prompt in here
from.
Guess if he has done this play, welcome to the profile page, dear.
Name of the user.
Nice.
So that will be another issue here if.
Databased.
So how many actually, let me just write down, because I refresh it, Rick.
Email our Gmail dot com.
Onto three.
I get now in our database, we always going to have only one registered user, and why is this?
Well, because when they save it and a refresh and the compiler gets to this line and it sent the database
back to empty, that's why.
OK, so the database of zero.
And I want to access the name, that name is equal equal to what?
The username variable.
So whatever this variable holds, it needs to be the same with this one.
If that is the case, then what we need to do is saying cancel that log.
Welcome to your profile page, dear.
Make a breathing space username.
And else, let's put alert here.
Something.
Went wrong.
Please try again later.
And that's pretty much it.
So we hear, Rick.
Email break at Hotmail dot com.
Very good.
Welcome to your profile page, d'Afrique.
Everything is working, our database array holds one object crazy.
Now congratulations.
You know how to do it fails conditions.
You know now how to push through the database on object and now you know how to deal with objects.
Crazy.
That is super.
I really enjoy having you as my student.
If you are here, that means that you're doing something great.
So I will see you in the next one.
And don't get don't get scared.
It will get better in time.
So just please practice and let's fix this problem here.
Email right now.
So, yeah, I'm sorry if I see some mistake here.
I mean this.
Language mistake and yeah, we'll try to fix it in the final solutions folder, right.
So see you in the next one.
And stay safe, but.