mercredi 28 septembre 2016

If/Else statement nested in While loop

(I am very new to Javascript and am writing this to try and get used to using it a bit more - which is why i'm using an object to store usernames and passwords. I'm learning this as part of an apprenticeship but this is during my own time to further my knowledge on JS) I have tried writing a small piece of code that should ask the user to enter a username, which will be cross-checked against the property names in the object "userNames". The user is then asked to enter a password for this user which is then checked against the password set as the value of the corresponding property with the right username. I feel as though part of the problem is to do with me trying to use the variable "enteredPassword" (set to the user's input for the password as a method) of "userNames". If the username or password are incorrect after 3 attempts then i'd like the window to close. my text editor is saying that the If statement is missing a branch and i'm not sure if this is affecting whether this works or not. currently, not even the 'alert' boxes pop up.

var userNames = {Tom:"PassW0rd", Bill:"Apples40C0lander", Ryan:"M1911p4ck4ge"};
var Greeting = "Insert Username:";

var i = 0;
while (i < 3) {
    var enteredUserName = prompt(Greeting);
    var enteredPassword = prompt("enter password for " + enteredUserName);
    if ((userNames.hasOwnProperty(enteredUserName)) && (userNames.enteredUserName==enteredPassword)); {
        alert("Welcome");
        continue;
    } else {
        alert("incorrect Username or Password");
        alert("");
        window.open('', '_self', ''); window.close();
        window.open('', '_self', ''); window.close();
    }}

Aucun commentaire:

Enregistrer un commentaire