Hi

Was wondering if anyone can tell me why the code below does not work!
I am presuming it is something to do with the array but can't figure it out.

Thanks

var userpasswords = ['sooty', 'plane', 'sandra', 'football', 'laundry'];
var password;


password = window.prompt('Please enter your password','')

while (password != userpasswords)
{
    window.prompt('You have not entered a incorrect password. Please try again','');
};
document.write('You are logged in')

Recommended Answers

All 3 Replies

You need to iterate over the array and compare each array entry against the user input. Currently, you are comparing an array object with a string, hence the error.

for(var i = 0, max = arr.length; i < max; ++i) {
  var found = false;
  if(arr[i] == pwd) {
    found = true;
    break;
  }
}
if(found) {
  alert("Correct password");
} else {
  alert("Incorrect password");
}

Hi

Am sorry for this dumb question but am very new to this.
I can follow your code apart from the first line. Could someone possible elaborate on it and explain what everything means?

Thanks for your help so far.

n33712

> I can follow your code apart from the first line.

I am just stepping through the elements of the array using a loop and comparing each element with the user input. If it matches, then the user has entered a correct password. If all the elements have been tested without a match, then the password is incorrect.

If you still have trouble understanding the basic programming concepts, maybe you should start off with some basic programming / javascript tutorials, practice a lot and attempt your project once you are comfortable with those.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.