Welcome back! Using our newfound knowledge of loops, we're going to make our Facebook app a little bit
better.
I mean because Facebook has more than one user, right?
It's not just one user, it's got millions of them.
So, how can we check somebody's password by going through not just one user, but multiple ones?
Well, sounds like a perfect solution for loops, so let's give it a go.
I'm going to add a few more users in here.
We'll just copy this. And we have 'sally' with the password '123'.
And we also have 'ingrid' with password '777'.
So let's refresh it.
I'm going to make this on a new line just so you can see it.
I'm going to refresh and let's type in 'ingrid'.
'777'. 'Sorry, wrong username and password'.
And that makes sense, right? Because when we created our 'signIn' function, well, it's only checking the
first item in the database.
But we want to check through all of them.
We want to check through the first item in the array, the second and the third.
Looks like we need to add a 'looping' here.
So how can we do that?
We should, well, add a 'for loop'.
I'm going to say in here.
Well, actually I'm going to comment this out first, and we'll say, that when in 'signIn' we want to do 'for var
i = 0'.
We remember this index from last video, we'll say 'i is less than database.length'.
So, 'database.length' that's '3'. And we'll increment 'i' every time. Open and close brackets, and then here
we want to say 'if(database[i])'.
So instead of '0'
now we can say 'i', because 'i' is going to go '0, 1, 2'. So
'if database[i] equals', and we should have a 'username' here as well.
So if the 'username' equals the 'username', that we receive, which is the input 'username'. And I'm going to start
a new line here. 'database[i].password' equals the 'password', that we receive from the 'user'. Well, if that's
the case what should we do.
Well, if that's correct, we'll do 'console.log(newsfeed)'.
Otherwise.
Otherwise we'll do 'alert'.
"Sorry, wrong username and password".
So, now I'm going to save. Let's try this out. 'username'? - 'ingrid', 'password'? - '777'. 'Sorry, wrong username and password'.
"Sorry, wrong username and password', but then I get the 'newsfeed'.
What just happened?
Well, what we just did was, we did a 'for loop', but we ran this 3 times.
So, the first time I got 'Sorry, wrong username', the second time I got 'Sorry, wrong username', because it's
checking 'andrei' and 'sally', and then is checking 'ingrid', which finally works and gives me back the 'newsfeed'.
So our logic here isn't right. What can we do to solve this?
Well I have an idea. Let's create another function.
I'm going to call this function 'isUserValid' and this function is going to receive 'username'
and 'password'.
And here we can copy this and move it over here.
But we don't really want to do what happened here where we constantly log, instead we would want to say
'return true'.
So if user is valid 'return true', if the user is not valid, then we should 'return false', right?
But how can we do that?
Well, if I do 'return false' here - let's look at the logic.
I am looping through everything.
If at any point the 'username' and the 'password' match, with what we have in the database 'return true'.
And we remember that a function stops and exits when it sees 'return'.
So, it's going to return 'true'.
But if it does the looping 3 times and this 'if' condition never passes, it's going to go to the next
line which is 'return false'.
So now 'isUserValid'.
Well let's test this out!
I'm going to say 'console.log(isUserValid)', and we'll give it 'username' and 'password'.
You can see that the 'username' and 'password' is coming from here.
So let's see. I'm going to save and refresh
'ingrid', '777'.
'true'.
That's right.
Let's test something where it's not right.
I'm going to say 't' and 'garblegarblegarble', 'false'.
OK.
It looks like our 'isUserValid' function is working.
So now watch this:
We can remove the log and uncomment our original function.
And instead of doing all this check we can now say 'if(isUserValid)', 'username' and 'password', which
is again this 'username' and 'password', that we receive over here from the 'prompt'.
If this is 'true', then run the 'newsfeed'.
If it's false, well 'else' you say 'Sorry, wrong username and password'.
Let's save and refresh. OK.
Let's go.
'ingrid', password '777'. We get our 'newsfeed' and if we enter the wrong information,
we get 'Sorry, wrong username and password'.
How cool is that?
We now just created 2 functions and it reads pretty nicely, right? And it says 'signIn'.
Here's the 'username'. Here's the 'password'.
We go to the 'signIn' function, it says: "Hey, if 'isUserValid'?"
"Yes, user is valid". "Than 'log newsfeed', otherwise 'alert' 'Sorry, wrong username and password".
And that is a really important concept of naming things and ordering codes so you can kind of read it
like English and it makes sense.
And there you have it.
We have our Facebook app.
You can see here the power of JavaScript.
Again we've created these functionalities that can be applied on grander scale on real web apps.
I'll see you in the next one. Bye-bye