0

I have to build a simple login script for a js class.

I cannot get the loop to work. Everytime I type in any info it gives me "Invalid left-hand side in assignment"

When the login button is clicked the getData function gets the values of the boxes then passes them to the logon function that checks against the array. That's where the script stops. If I change the = in the if statement to == it will accept the last valid login f the array but none of the others.

What am I doing wrong?

<script type="text/javascript">
var userLogins = [{user:"user1", password:"pass1"},{user:"user2", password:"pass2"},{user:"user3", password:"pass3"}]
var success = null;
function logon(user, pass) {
    userok = false;
    for (i = 0; i < userLogins.length; i++)
    { 
        if(pass = userLogins[i].password && user = userLogins[i].user )
        {
            success = true;
        }
        else
        {
            success = false;
        }
    }
    secret(success);
}

function getData() {
    var user = document.getElementById("userid").value;
    var password = document.getElementById("password").value;
    logon(user, password);
}

function secret(auth){
    if(auth)
    {
        show('success');
        hide('login');
    }
    else
    {
        show('error');
        hide('login');
    }
}

function show(show) {
    document.getElementById(show).className = "show";
}
function hide(hide) {
    document.getElementById(hide).className = "hide";
}
coryrwest
  • 700
  • 1
  • 8
  • 23

1 Answers1

4

If I change the = in the if statement to == it will accept the last valid login f the array but none of the others.

= is the assignment operator

== is the equality operator

You're confusing the two. You want the latter. Otherwise, your assigning the value which results returns the value itself (often a true value).

Per the comments, there's also the strict equality operator. For the difference between == and === this answer will blow your mind.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174