javascript - Js Password Array -


my homework "create basic web page update assignment 3.4. create array store @ least 8 valid passwords. then, ask user password using prompt dialog. utilize for-loop navigate password array. if user's input matches string in array, give them feedback password valid. otherwise, let them know invalid.

you not have include feature give them chance re-enter password. can one-time search of password array."

this javascript class... far have this:

var accepted_passwords = new array ( ); accepted_passwords[0] = "saginaw"; accepted_passwords[1] = "bay city"; accepted_passwords[2] = "midland"; accepted_passwords[3] = "reese"; accepted_passwords[4] = "millington"; accepted_passwords[5] = "frankenmuth"; accepted_passwords[6] = "sheilds"; accepted_passwords[7] = "birch run";  var random_bg = new array ( ); random_bg[0] = "#0000cc"; random_bg[1] = "#33ff33"; random_bg[2] = "#990000"; random_bg[3] = "#9900ff"; random_bg[4] = "#cc0000"; random_bg[5] = "#ff00cc";  var enterpassword; enterpassword = prompt("enter password", "");  for(0 <= <= 7; enterpassword = accepted_passwords[i];) {     document.body.style.background=random_bg[math.floor(math.random()*random_bg.length)]; } 


supposed ask password... , if right... displays 1 of 5 random colors. random color part works... password part doesn't. can no problem if else statement... apparently teacher wants loop?????? please help!

i think answer here you've fulfilled teacher's requirement use loop. there nothing stopping also use 1 or more if..else constructs too. in fact, if think bit more, you'll realise there no way "check if valid password" without if..else.

a few observations on how complete homework:

  • your for(...) loop wrong. loops follow pattern:

    for([loop-variable-initialisation];[loop-termination-condition];[loop-variable-increment]

eg, for(var i=0;i<10;i++) // loops values 0 9

  • instead of hard-coding length of array, use in loop termination condition, better read .length of array

  • you not checking value entered exists in array @ all. worse, (trying to) assign each value in array variable holding user's input: enterpassword = accepted_passwords[i]. wrong.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -