-6

I have made 2 variables named username and password and I want to test if they are both true but the prompt does not seem to pop up. I do not understand, I also am pretty new to code and really want to learn how to do this

var 1 = false;
var 2 - false;

function login() {

var username = prompt("Username");
var password = prompt("Password");

if (username == "wouterXD") {
    1 = true;
} else {
    1 = false;
}

if (password == "Wout2003!") {
    2 = true;
} else {
    2 = false;
}
};
    if (1 = false && 2 = false) {
        alert("Wrong Password and Username!");
        login();
    }
Sagar V
  • 12,158
  • 7
  • 41
  • 68

3 Answers3

1

There is so much wrong with your code, this should work though.

What you got wrong is:

  • You cannot set variables using the - operator
  • You cannot compare in an if-statement using single =
  • You cannot name variables with numbers.

var unc = false; // username correct
var pwc = false; // password correct

while (!login());

function login() {

    var username = prompt("Username");
    var password = prompt("Password");

    if (username == "wouterXD") {
        unc = true;
    } else {
        unc = false;
    }

    if (password == "Wout2003!") {
        pwc = true;
    } else {
        pwc = false;
    }

    if (unc && pwc){
        return true;
    } else {
        return false;
    }
};
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • while (!login()) {login();} will try repeatedly to log in and when it is successful, it will try to log in again... – Lajos Arpad Jun 24 '17 at 14:01
  • Yes, now this should work. The if conditionals are still overcomplicating your code though in my opinion, but nonetheless, your code is close to the op's code and therefore it deserves an upvote. – Lajos Arpad Jun 24 '17 at 14:16
  • 1
    @LajosArpad thanks. I wanted to leave the code like this for the OP to understand it. It's way closer to his code this way. – Luca Kiebel Jun 24 '17 at 14:17
1
var 1 = false;

means that a variable called 1 will get the value of false. But this is syntactically incorrect. It is invalid to name your variables with integer names. You are also confused about the meaning of =, which is the assignment operator. Let's see a simplified solution:

var uCorrect = false;
var pCorrect = false;

function login() {
    uCorrect = (prompt("Username") == "wouterXD");
    pCorrect = (prompt("Password") == "Wout2003");
    if ((!uCorrect) && (!pCorrect)) {
        alert("Wrong Password and Username!");
    }
    return uCorrect && pCorrect;
}

while (!login());

Or an extremely simplified solution, if you do not need to store these data:

function login() {
    return (prompt("Username") == "wouterXD") && prompt("Password") == "Wout2003";
}

while (!login());
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

So many things are wrong with this code...

First of all, never name your variables as integers... so rename them to something more... suitable.

Secondly, your var 2 is wrong. You've got a subtraction sign not an equals. Least but not last, your logical operator is also wrong.

Single equals sign is assigning values, in your if statement you should check if they are the same, so a double sign is needed like so

if(firstVariable == false && secondVariable == false){

}

You won't achieve the functionality you want anyway after correcting those mistakes. The last operator (the one I posted above) is outside the function body, i.e. it won't execute when the function is ran.

var firstVariable = false;
var secondVariable = false;

function login() {

   var username = prompt("Username");
   var password = prompt("Password");

   if (username == "wouterXD") {
       firstVariable = true;
   } else {
       firstVariable = false;
   }

   if (password == "Wout2003!") {
       secondVariable = true;
   } else {
       secondVariable = false;
   }

   if (firstVariable = false && secondVariable = false) {
        alert("Wrong Password and Username!");
        login();
   }
};
Adrian
  • 8,271
  • 2
  • 26
  • 43