Please let me know what you think. Is the code well written? Am I using best practices (like when I chose ===
over ==
)? Is my script too verbose?
Note: I'm only asking because the tutorial I'm using is a little old.
/* A login script to that checks two users and their
corresponding passwords and then greets one of them
*/
var input = '',
guess = '',
montyPswrd = 'Cheese',
montyUser = 'Monty',
chipUser = 'Chip',
chipPswrd = 'Gadget';
var isMonty = function (name) {
if (name === montyUser) {
document.write('<h1>Welcome to the site ' + montyUser + '</h1>');
} else {
alert('Your not monty');
}
};
var isChip = function (name) {
if (name === chipUser) {
document.write('<h1>Welcome to the site ' + chipUser + '</h1>');
} else {
alert('Your not chip');
}
};
input = prompt('Enter Username:', 'username');
switch (input) {
case montyUser:
guess = prompt('Enter Password','password');
break;
case chipUser:
guess = prompt('Enter Password','password');
case null:
break;
default:
alert('wrong useranme!');
break;
}
paswrdCheck:
if (guess === '') {
// then the user hit ESC or cancel
// no point in asking for password
break paswrdCheck;
} else {
switch (guess) {
case montyPswrd:
isMonty(input); // makes sure monty is matched with right password
break;
case chipPswrd:
isChip(input); // makes sure chip is matched with right password
break;
case null:
break;
default:
alert('wrong password!');
break;
}
}