Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Having difficulty with loops

    Hey everyone! I am pretty new to JS and I am having a problem figuring this loops structure out - any help would be appreciated. I am trying to add a password to an html page via an external JS file. (I know this isn't an ideal solution for securing a page - I am just trying to use this for trying to understand loops.)

    What I am trying to do is prompt the user to enter a password before the html page loads. If the password is correct they can enter the page. If they guess wrong the user is looped back to a prompt box to try again. If they fail 3 times they should be told via an alert box and sent off somewhere else.

    I have tried using a for & do while loop and was unsuccessful. I think this is the best way to go.

    This is what I have so far:

    Code:
    var password = prompt('All women are:',' ');
    var pass1 = "Psycho";
    var counter = 0;
    
    
    if (counter < 3) {
    	if (password = pass1) {
    		alert('Password Correct! Click OK to enter!');
    		counter++
    	}
    	else {
    		alert("Password Incorrect!!!");
    		window.location="Lab5Part2.html";
    	}
    }
    else {
    
    	alert("You have failed 3 times!");    
          	window.location="http://www.google.com";
    }
    Last edited by Jasonp78; 03-25-2012 at 07:57 PM.

  • #2
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    The For Loop

    This is the for loop I came up with and the code seems to work. I have been playing with trying to add an else if statement to make it redirect the user to another side after 3 login attempts...but no luck.

    Code:
    var password;
    var pass1="Psycho";
    
    password=prompt('All women are:',' ');
    
    for (var count=0;count < 3;count++) {
    
       if (password==pass1) {
    
          alert('Password Correct! Click OK to enter!');
          break;
        }
    
        else {
    
          alert("Password Incorrect!");
          window.location="http://www.google.com"; 
          break;
        }
    }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,298
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Code:
    <html>
    <head>
    <body>
    
    <script type = "text/javascript">
    
    function getPassword() {
    
    var pass1="Psycho";
    
    for (var count=0;count < 3;count++) {
    var password=prompt('All women are:',' ');
    
    if (password==pass1) {
    alert ('Password Correct! Click OK to enter!');
    // enter the site
    window.location.href = "http://www.javascriptkit.com";
    return false;
    }
    else {
    if (count <2) {
    alert ("You have " + (2-count) + " tries left");
    }
    }
    
    }
    
    alert ("Password Incorrect After 3 Tries!");
    window.location.href ="http://www.google.com";
    
    }
    
    </script>
    
    </body>
    </html>
    I expect you realise that anyone can see the password simply with View Source.


    He could have done one of three things - he didn't do either. Commentator ITV4

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Phillip ty very much for the quick response. I would never use this as security on a live site - would probably have to go with PHP for that. I have tried plugging that in to a webpage and it's not working. I see I forgot to add an if/else with the counter < 2. I have no clue what return false does but I am looking that up momentarily.

    My goal was to put the code in an external js file and link it to the head section so the code will execute before the page loads. I am still a little confused on how to do that with a function. Your post gave me a good clue on what I am doing wrong. Thanks so much!

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    edit

    I used the
    Code:
    <body onload="javascript:getPassword()">
    and got the page to function

    The return false put the page in an infinite loop which was pretty cool--good to know

    Now I have this:

    Code:
    function getPassword() {
    
    var pass1="Psycho";
    
    for (var count=0;count < 3;count++) {
    var password=prompt('All women are:',' ');
    
    if (password==pass1) {
    	alert ('Password Correct! Click OK to enter!');
    	window.location.href = "test.html";
    	break;
    
    }
    else {
    if (count <2) {
    alert ("You have " + (2-count) + " tries left");
    }
    }
    
    }
    
    alert("Password Incorrect After 3 Tries!");
    window.location.href ="http://www.google.com";
    
    }
    For some reason it works great in Firefox but sends me too google in IE

    ps.. the
    Code:
    alert ("You have " + (2-count) + " tries left");
    is awesome
    Last edited by Jasonp78; 03-26-2012 at 02:35 AM.

  • #6
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanx Howard I'd prefer to be pointed in the right direction.... but I have no clue what that jpeg is

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Got it

    Just wanted to thank ya'll that helped me out. I had to scratch my head a few times but it works in IE and Firefox. Here's what I came up with:

    Code:
    function getPassword() {
    
    var pass1="Psycho";
    
    for (var count=0;count < 3;count++) {
    var password=prompt('All women are:',' ');
    
    if (password != pass1) {
    	if (count < 2) {
    		alert ("You have " + (2-count) + " tries left");
    	} else {
    		alert("Password Incorrect After 3 Tries!");
    		window.location.href ="http://www.google.com";	
    		
    	}
    } else {
    	password==pass1
    	alert ('Password Correct! Click OK to enter!');
    	break;
    	
    }
    }
    }

  • #8
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Just out of curiousity would there be a way to have them redirected to google when the cancel button is hit?

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Why not experiment, instead of asking?

    What happens if you do
    Code:
        var password=prompt('All women are:',' ');
    and the person hits cancel? What is the value that comes back in password?

    Of course, the question really should be
    Code:
        var password=prompt('All men who think women are psycho are:', ');
        if ( password == "Mentally Deficient" ) ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Jasonp78 View Post
    Just out of curiousity would there be a way to have them redirected to google when the cancel button is hit?
    Check if the returned value from the prompt is null and if it is, set window.location.href to the redirection url.

  • #11
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Old Pedant I tried a few things but no luck as of yet...btw thanks for the laugh

    Mishu I'll give it a shot

    thank ya'll

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    As mishu said, hitting cancel causes prompt to return null.

    You check for that by doing
    Code:
        var password=prompt('All men who think women are psycho are:','');
        if ( password == null )
        {
               ... whatever ...
        }
        ... other code ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    The part that's getting me confused is the nesting. I spent more time trying to figure out where the curly brackets go and if I have enough curly brackets lol. Is it possible to check if password is null outside of the function or is it necessary to have it nested?

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    ??? How can you check outside the function if the prompt is *IN* the function??

    I guess you *could* return null from the function, but I don't see how that makes the code less complex.

    You know, if you properly INDENT your code, it helps a *LOT* in keeping track of the braces.

    Any time I see code such as
    Code:
    }
    }
    }
    I *know* that the code is going to be hard to read.

    So:
    Code:
    function getPassword() 
    {
        var pass1="Delusional"
    
        for (var count=0;count < 3;count++) 
        {
            var password=prompt('All men who think all women are psycho are:','');
            if ( password == null ) 
            {
                location.href = "http://www.google.com";
                return; // not really needed, but conceptually cleaner
            }
            if (password != pass1) 
            {
    	    if (count < 2) 
                {
                    alert ("You have " + (2-count) + " tries left");
                } else {
    		alert("Password Incorrect After 3 Tries!");
    		window.location.href ="http://www.google.com";	
    	        return; // again, not needed	
                }
            } else {
                alert ('Password Correct! Click OK to enter!');
                return;
            }
        } // end of the for loop
    
    } // end of the function
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Personally, I would have done that more like this:
    Code:
    function getPassword() 
    {
        var pass1="Delusional"
    
        for (var trynum=1; trynum <= 3; ++trynum) 
        {
            var password=prompt("Try number " + trynum 
                                             + "\nAll men who think all women are psycho are:','');
            if ( password == null ) 
            {
                break; // exit the loop (could also do this by trynum = 4 !)
            }
            if (password == pass1) 
            {
                alert ('Password Correct! Click OK to enter!');
                return;
            }
            if (trynum < 3) 
            {
                alert ("You have " + (3-trynum) + " tries left");
            }
        } // end of the for loop
    
        // either null from prompt or 3 tries exhausted
        location.href = "http://www.google.com";
        return; // not really needed, of course
    
    } // end of the function
    See? Much less nesting.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Jasonp78 (04-03-2012)


  •  

    Posting Permissions

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