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 6 of 6

Thread: Looping help.

  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Looping help.

    Hey guys, i'm fairly new to programming in Javascript.
    I've been trying to write a basic user name and password program.
    I was wondering if I could get some help with how I would put it in a loop so if the information in incorrect, it will prompt up "x" amount of times until the information is correct. Thanks, here's what I have so far.

    var user_name = prompt ("Please enter your username: ");
    var password = prompt ("Please enter your password: ");

    if( (user_name=="John123") && (password== "helloworld") ){
    alert("Welcome, " + user_name + ".");
    }
    else{
    alert (" Sorry, information invalid. " );
    }
    Last edited by diceman93; 01-11-2013 at 07:49 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    If you're doing this just to learn JavaScript, I'm all about it. HOPEFULLY you will not actually be trying to use this as a secure way to log in to something. JS is wonderful, but it is NOT secure.

    Code:
    var user_name, password;
    for(var i=0; i<2; i++){
        user_name = prompt ("Please enter your username: ");
        password = prompt ("Please enter your password: ");
    
        if( (user_name=="John123") && (password= "helloworld") ){
                  alert("Welcome, " + user_name + ".");
                  break;
        }
        else{
             if(i<2){
              alert (" Sorry, information invalid. " );
               }
             else{
               alert("You've used up all your attempts.");
                break;
                }
        }
    Untested, but I think it's correct.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,441
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Missing a final } at the very end of the code, to close the for loop.

    And the message "You've used up all your attempts." will *NEVER* be seen.

    That's because it is only seen (a) INSIDE the for loop and (b) when i is 2 or greater. But i can never be 2 or greater inside that loop, by the very terms of how the for is written.

    Try again?
    Code:
    var user_name, password, passed = false;
    for(var i=1; i<= 3 && passed == false; i++)
    {
        user_name = prompt ("Please enter your username: ");
        password = prompt ("Please enter your password: ");
    
        passed = ( (user_name=="John123") && (password= "helloworld") );
    } // end of loop after 3 tries *OR* if passed is true!
    
    if ( passed )
    {
        alert("Welcome, " + user_name + ".");
    } else {
        location.href = "http://www.google.com";
    }
    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:

    diceman93 (01-11-2013)

  • #4
    New Coder
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Perfect! Thanks alot Old Pedant. Makes sense now. Ya, that was it before too just missing the "}". Appreciate the help!
    Last edited by diceman93; 01-11-2013 at 08:26 PM.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Note that alert and prompt are for debugging use only and should never be used in a live web page.

    Opera adds a "Disable JavaScript" option to all such dialogs (which if selected would stop the script running without returning from the first prompt) while Firefox, Safari and Chrome add an option to disable the display of subsequent dialogs when the second one is displayed (which if selected would stop the alert from displaying anything).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,441
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Yep. alert() and prompt() and document.write() are just for "toy" scripts. [With a few exceptions in the case of document.write().]
    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.


  •  

    Posting Permissions

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