anyone want to help me with some basic javascript with arrays and loops? :hs:

Discussion in 'OT Technology' started by rb26dett, Nov 9, 2007.

  1. rb26dett

    rb26dett New Member

    Joined:
    Apr 25, 2005
    Messages:
    835
    Likes Received:
    0
    Location:
    bergen county, nj
    its real easy, i think i got a lot of it, but i need help :(

    I'm not sure if i got this code right so far. And I'm also stuck now. I have no clue what to do next. I've tried w3schools.com and that junk..

    This is my assignment:

    This is my code so far:

    <script type="text/javascript">

    alert("<i>Welcome to ******'s Sporting Goods.</i>");

    var arr = new Array(5)
    arr[0] = "Brad"
    arr[1] = "Tom"
    arr[2] = "Bruce"
    arr[3] = "Justin"
    arr[4] = "Tommy"

    var arr2 = new Array(5)
    arr2[0] = "Jennifer"
    arr2[1] = "Nicole"
    arr2[2] = "Demi"
    arr2[3] = "Cameron"
    arr2[4] = "Pamela"

    var arr3 = new Array(5)
    arr3[0] = "Snow boards"
    arr3[1] = "Sleds"
    arr3[2] = "Ice skates"
    arr3[3] = "Snow tubes"
    arr3[4] = "Skis"

    var arr4 = new Array(5)
    arr4[0] = "$750"
    arr4[1] = "$2000"
    arr4[2] = "$200"
    arr4[3] = "$500"
    arr4[4] = "$1000"

    var arr5 = new Array(5)
    arr5[0] = ""
    arr5[1] = "20%"
    arr5[2] = ""
    arr5[3] = "10%"
    arr5[4] = ""

    var i=o

    var username;

    var password;

    var j=true;

    var product;


    var logins = 0;
    do{
    if (i<5){
    if (arr == username && arr2 == password){
    product = prompt("Welcome "+username+". What products do you want
    to buy today?");
    break;
    }
    }
    else {
    alert("Bad username or password entered");
    i++;

    }
    }
    while(logins < 3);
    if (logins >= 3){
    alert("Access denied! Too many failed login attempts!");
    }

    if (username== arr && password== arr2)

    {
    product = prompt("Welcome "+username+". What products do you want to buy today?");
    while (j == true)
    {
    product = prompt("What would you like to order?","");


    aaaand i'm stuck there.
    First, did I get the array thing right? Specifically the username/password part.
    Second, what the hell do I do next :(
    I don't know how to use the arrays to look up the price and discount..
     
  2. rb26dett

    rb26dett New Member

    Joined:
    Apr 25, 2005
    Messages:
    835
    Likes Received:
    0
    Location:
    bergen county, nj
    Would it help if there was a decent reward involved? :hs:
     
  3. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
    Last edited: Nov 9, 2007
  4. 2000GT

    2000GT Active Member

    Joined:
    Jan 21, 2001
    Messages:
    5,798
    Likes Received:
    11
    Location:
    Vancouver
    The code looks good; however, you forgot a couple key practices for any coding you do. Firstly, make sure to comment your code... for example, declaring variables, each section of code that can be grouped etc... Secondly, your variables have generic names that are not intuitive to a stranger/teacher reading your code. arr, arr2 etc doesn't identify the function of the array - try naming them based on what they are... eg. arrPassword, arrUsername....
     
  5. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
    I disagree with needing to comment your code.

    Well, I disagree in the sense that if your code is not very long (which this one isn't) and the variables and structure are well thought out, then you don't need to comment.

    I bet if you look at my code, you can tell exactly what it's doing.
     
  6. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    An attitude that will make everyone hate you throughout your career.

    Comment your code.
     
  7. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
    lol, first off I don't code for my career, I code on the side as a hobby.

    secondly, I guess you would prefer this:
    Code:
    /////////////////////////////////////////////////
    //
    //  The greet user script, that greets users
    //
    //  Code by: John Doe
    //  Last Update:  January 1, 2007
    //  Version: 0.1.3b
    //  Coded in: javascript 1.6
    //
    /////////////////////////////////////////////////
    
    /////////////////////////////////////////////////
    // Here I am initializing my variables to
    // use in the program you are looking at
    /////////////////////////////////////////////////
    
    // This 's' variable will hold a greeting
    // message that I will display to the user
    
    var s = 'Hello';
    
    /////////////////////////////////////////////////
    // Here I am assigning a function to the
    // window.onload event handler, which
    // will run my code only after the window
    // has fully loaded.  This prevents the code
    // from running to soon on the page.
    /////////////////////////////////////////////////
    
    window.onload = function() {
    
    /////////////////////////////////////////////////
    // Here I am using the built in alert function
    // in javascript to display the greeting to
    // the user who is viewing the page
    /////////////////////////////////////////////////
    
    alert(s);
    
    /////////////////////////////////////////////////
    // This is the closing bracket for the window
    // onload function.
    /////////////////////////////////////////////////
    
    }
    Over this:
    Code:
    window.onload = function() {
      alert('Hello');
    }
     
  8. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    There's a happy medium. But yes, if each of those was 1000 lines of actual code, I would take the former to the latter.
     
  9. 2000GT

    2000GT Active Member

    Joined:
    Jan 21, 2001
    Messages:
    5,798
    Likes Received:
    11
    Location:
    Vancouver

    I throw in basic comments such as

    //function to read array for username and password authentification

    Stuff like that is simple and easy to read and doesn't clutter things up.
     
  10. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
    I'm not talking about thousands of lines.
     
  11. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    You're in school to learn good habits. If you hate to comment so much now that you don't comment your assignments, there's no reason to think that you'll change.
     
  12. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
    lol, in school? I'm 25 and have been working for the last 7 years. Again, I don't work as a coder, and I'm not in school. I code for a hobby, and my real work is network engineering.
     
  13. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Really? That looked like an assignment.

    Anyway, get in the habit of commenting yo shit.
     

Share This Page