SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Help with a simple password function

    Hi. I am new here and I hope that someone may be able to help me. I am doing a project for school and my code isn't working. I can change it so it displays the wrong thing, or nothing at all. Here is my code.

    var guessNumber
    guessNumber = 0;

    var password = new Array(4);
    password[0] = "candy";
    password[1] = "chicken";
    password[2] = "house";
    password[3] = "car";

    function askForPassword(text) {
    if (text == password) {
    window.alert("Welcome user");
    if (text != password)
    window.alert("Incorrect Password try again");
    ++guessNumber;
    }
    }
    function guessnumber() {
    if (guessNumber == 4) {
    window.alert("Close your browser window");
    }
    }
    -->
    </script>
    </head>
    <body>
    <h4>Please enter a password below<h4>
    <form action="">
    <input type="text" name="text" /><br />
    <input type="button" name="button1" value="Press to submit" onclick="askForPassword();" />
    </form>

    The code is supposed to render a page where the user enters a password and clicks a button to submit it. At this point I am trying to get the code to display a alert box when the password is correct. I know there has to be something wrong with the way the function is being passed because I can not get it to recognize a correct password. If I change the code to make the "Welcome user" alert box appear, the code will think that any bit of text is a correct password. I also want to add that the second If statement was a else statement. I changed it because it would run instead of the if part.

    The "guessnumber" variable and function are supposed to add a counter to the code so that it runs only four times, if a correct password isn't displayed the page closes. At the moment I'm not so stressed to have the counter in the right place, I'd rather know why the code doesn't run, or executes the second if statement even when I put:

    function askForPassword(text) {
    if (text == password[0]) {
    window.alert("Welcome user");

    even when I do that and put the exact password in the input field, I get no results or the wrong one.

    Also, I am having trouble learning functions so I hope that anyone that replies might be able to give me some "How" and "whys" instead of just giving me the correct code.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You created an askForPassword function which takes an argument named text, but you are calling it without passing in any value for that argument. The text variable is always empty so it's correct in telling you the password is wrong.

    Either in the onclick action of the button add code to pass in the value of the text box, or edit the askForPassword function so it doesn't take an argument and gets the text itself.

    You are also comparing a string to an array, so even if the text variable contained one of your passwords, it's not equal to an array of four passwords.

    Code javascript:
    function askForPassword() {
      //Get the value of the text box
      var passwordEntered = document.forms[0].text.value;
     
      //Check it against each value in the password array
      for (item in password) {
        if (passwordEntered == password[item]) {
          alert("Welcome user!");
          //Since we found the password, returning will end the function execution
          return true;
        }
      }
      //If we got here, then none of the passwords matched, or we would've returned already
      alert("Incorrect Password, Try Again");
      guessNumber++;
      return false;
    }
    Last edited by Dan Grossman; Apr 11, 2008 at 19:42.

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, the helps some. I went back and changed the name of the input fields and added some code to pass the value. I can get it to work sometimes. I am wondering if there is a way to check the value of the password against the one stored in the array with different code than you posted?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    for (i = 0; i < password.length; i++) {
      if (passwordEntered == password[i]) { 
        alert("Welcome User!");
      }
    }

  5. #5
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •