133. exercise: build Facebook2

변지영·2021년 12월 10일
0
var database = [
	{
		username: "andrei",
		password: "supersecret"
	}
	{
		username: "sally",
		password: "123"
	}
	{
		username: "ingrid",
		password: "777"
	}
];

var newsFeed = [
	{
		username: "Bobby",
		timeline: "So tired from all that learning!"
	},
	{
		username: "Sally",
		timeline: "Javascript is soooo cool!"
	}
	{
		username: "Mitch",
		timeline: "Javascript is preeettyy cool!"
	}

];

var userNamePrompt = prompt("What's your username?");
var passwordPrompt = prompt("What's your password?");

function signIn(username, password){
	if (username === database[0].username && 
		password === database[0].password){
		console.log(newsFeed);
    } else{
    	alert("Sorry, wrong username and password!");
    }
}


signIn(userNamePrompt, passwordPrompt);

and then If I type ingrid,777 as username,password,
it says "Sorry, wrong username and password!".
Because it's only checking database[0].

function signIn(username, password){
	for(var i=0; i < database.length; i++){
		if(database[i].username === username &&
			database[i].password === password){
			console.log(newsFeed);
		}else{
			alert("Sorry, wrong username and password")
		}
	}
// 	if (username === database[0].username && 
// 		password === database[0].password){
// 		console.log(newsFeed);
//     	alert("Sorry, wrong username and password!");
//     }
}

but this is wrong.
This just check from the first thing and if it's wrong, say "sorry ~ .."

var userNamePrompt = prompt("What's your username?");
var passwordPrompt = prompt("What's your password?");

function isUserValid(username, password){
	for(var i=0; i < database.length; i++){
		if(database[i].username === username &&
			database[i].password === password){
			return true;
		}	
	}
	return false;
}

function signIn(username, password){
	console.log(isUserValid(username, password));
// 	if (username === database[0].username && 
// 		password === database[0].password){
// 		console.log(newsFeed);
//     	alert("Sorry, wrong username and password!");
//     }
}

signIn(userNamePrompt, passwordPrompt);



And I got true.
If I say wrong

^false.

and then we can change function, 'signIn' originally functioned.

function signIn(username, password){
	if (isUserValid(username, password)){
		console.log(newsFeed);
	}else{	
    	alert("Sorry, wrong username and password!");
    }
}


and then I can see the newsFeeds.

0개의 댓글