Checking array

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jan 2008
Posts: 11
Reputation: n33712 is an unknown quantity at this point 
Solved Threads: 0
n33712 n33712 is offline Offline
Newbie Poster

Checking array

 
0
  #1
Jan 31st, 2008
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')
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Checking array

 
0
  #2
Jan 31st, 2008
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for(var i = 0, max = arr.length; i < max; ++i) {
  2. var found = false;
  3. if(arr[i] == pwd) {
  4. found = true;
  5. break;
  6. }
  7. }
  8. if(found) {
  9. alert("Correct password");
  10. } else {
  11. alert("Incorrect password");
  12. }
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: n33712 is an unknown quantity at this point 
Solved Threads: 0
n33712 n33712 is offline Offline
Newbie Poster

Re: Checking array

 
0
  #3
Jan 31st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Checking array

 
0
  #4
Jan 31st, 2008
> 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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1788 | Replies: 3
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC