1

I cannot get the loop to work in my simple js login script. When i try to login with any login other than the last one in the array (user3 and pass3) it returns false.

What am I doing wrong?

I have tried both == and ===.

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";
}

3
  • Don't forget to declare i, for (var i = 0 otherwise trouble... Commented Jan 31, 2013 at 2:52
  • Please tell me this is for nothing that is secure! Commented Jan 31, 2013 at 3:05
  • Nope. its for an assignment. I would never use JS like this for anything secure Commented Jan 31, 2013 at 22:14

2 Answers 2

3
for (i = 0; i < userLogins.length; i++)
{ 
    if(pass == userLogins[i].password && user == userLogins[i].user )
    {
        success = true;
    }
    else
    {
        success = false;
    }
}

You need a break in there, otherwise your true value for success simply gets overwritten with false on the next iteration... except for the last possible credentials, for which there is no "next" iteration.

Once you've done that, you don't actually need the else branch at all:

var success = false;
for (i = 0; i < userLogins.length; i++) { 
    if (pass == userLogins[i].password && user == userLogins[i].user) {
        success = true;
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Use break when you found it. Otherwise the next loop will set success to false.

for (var i = 0; i < userLogins.length; i++)
{ 
    if(pass == userLogins[i].password && user == userLogins[i].user )
    {
        success = true;
        break;
    }
    else
    {
        success = false;
    }
}
secret(success);

2 Comments

Better yet, get rid of the else branch, set success = false at the beginning, and set success, call secret, and return out of the loop when you find a match
Simple! how did I not see that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.