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 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array How to Question

    I need a piece of info to help me get started on my next project.... If I prompt the visitor to enter a list, such as a list of friends, how do I save those names to an array? All I can think of is something like, "var friends = prompt(please enter the names of all your friends. Enter 'End' when you are finished." When the visitor finishes entering then names by typing in "End", I want to display the names back, one by one, and ask them to enter a gift suggestion for each friend Then I want to save those gifts in a separate parallel array. If you can get me started, I'll see how far I can go before I have to ask for additional help!! Thanks!!

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    how's this for a start?

    Code:
    var nameArray = new Array();
    var item = ""'
    // prompt for a new name, and
    // don't stop until the input equals "end"
    while ( (item = window.prompt("Please Enter A Name:", "")).toLowerCase() != "end" ) {
      // place the input into the
      // last element of the array
      nameArray[nameArray.length] = item;
    }
    that's not best practice; it's difficult to read. however, i did it that way because it lets you validate the input in the loop's condition. you could validate it with an if() statement inside of the loop, but that seemed redundant to me. it came down to a toss up between sensibility, and legilbility, and i went with my sensibilities.
    Last edited by joh6nn; 03-12-2004 at 09:45 AM.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HMMM. I thank you for the reply, but I do find this difficult to understand.. Maybe because I'm a beginner???

  • #4
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    yeah, i was afraid that this might be a little over your head, but i decided that i'd go ahead and put it out, and then if you were having trouble with it, i could break it down for you.

    the code i gave only just does the first thing that you've asked for, which is to fill an array with names that the user input.

    so we start off by making an array to hold all those names:

    var nameArray = new Array();

    the next thing that i did, was combine a whole bunch of steps, into just two steps. we create the item variable, which we use to hold the name that we're about to put into the array. then, we start the loop that we use to put more than one name into the array at a time. all loops work on the principle of doing something, and then checking to see if they should stop. the particular loop that we're using is a while loop, which is important, because it means that check to see if should stop comes at the very beginning. the check that i put in looks like this:

    (item = window.prompt("Please Enter A Name:", "")).toLowerCase() != "end"

    i've color coded it to make it a little easier to follow. the red bit is the actual prompt for the name to be added to the array. then, that's assigned to variable item, in black. the bit in blue temporarily converts item to all lowercase letters, so that we can compare it to the bit in green, and see if instead of entering a name, the user actually typed "end", indicating that they've finished.

    in short: the user inputs a name, and we stick that into a variable named item. if that name ( which now rests in the variable item ) equals "end", then stop.

    the next part, is the body of the loop, so it happens every time the user inputs a name. if an array has 10 elements in it, then the last element is Array[9], because arrays begin at 0. that means that an array's length property is always 1 greater than the array's last element. so if we assign a value to Array[Array.length], then what happens is that we're adding a new element to the end of the Array, we assign the value to that new Array element.

    i hope that this cleared it up for you. if this is still a little too confusing for you ( and i apologize, i should have known better than to give you a complicated answer ) then there are other simpler ways to do this, and i'd be happy to take the time to show them to you.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    one of the array classical set up mode is:

    var smth = newArray()
    smth[0] = "bla";
    smth[1] = "blabla";
    smth[2] = "oak";
    ....

    As u can see the arrays elements are ordered startinf from first , 0, to the last incremented by 1.

    You may specify the lenght's array (the number of elements) or not, it depends on the goal.

    var smth = newArray(3)
    smth[0] = "bla";
    smth[1] = "blabla";
    smth[2] = "oak";

    the array's lenght is a javascript attribute, and the value is
    smth.length

    Now, to set up dinamically a new array you need that order number. If array has a sigle element

    var smth = newArray()
    smth[0] = "";

    you may notice that the order number is smth.length-1. that means if insert with prompt the next element, it will take smth.length as order number, and so on.

    var smth = newArray()
    smth[0] = "";
    smth[1] = "bla";

    and

    var smth = newArray()
    smth[0] =""
    smth[1] = "bla";
    smth[2] = "blabla";

    This array is almost the same with that from the beginig of my replay, except that uses a first element with no string value as an anchor. Now here is the joh6nn's script in action:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    script>
    function 
    bla() {
    inn='';
    var 
    nameArray = new Array();
    var 
    item '';
    // prompt for a new name, and
    // don't stop until the input equals "end"
    while ( (item window.prompt("Please Enter A Name:""").toLowerCase()) != "end" ) {
      
    // place the input into the
      // last element of the array
      
    nameArray[nameArray.length] = item;
    }
    //writes the arrays element when "end" condition stops the prompt
    for(i=0;i<nameArray.length;i++){
    inn=inn+nameArray[i]+'<br>';
    }
    document.body.innerHTML inn;
    }
    </script>
    </head>
    <body onload="bla()">
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    Mar 2004
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi! I'm just picking these new replies up. I'm printing them out now to study later. If I need to clear up some questions, I'll post later this evening.
    Thanks for taking time out to help!

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    Here is a more simplistic approach. Instead of having the user enter the people in one by one, you could have them simply enter a comma separated list:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    script type="text/javascript">
    function 
    getList() {
    // prompt for the list
    var list=prompt("Please Enter A Comma Separated List:""");

    // split the list into an array
    list=list.split(",");

    // prepares a string then writes it out
    var info="";
    for(var 
    i=0i<list.lengthi++){
        
    info+=list[i]+"<br />";
    }
    document.body.innerHTML=info;
    }
    </script>
    </head>
    <body onload="getList()">
    </body>
    </html> 

  • #8
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    The comma seperated list could use some modifying
    Try entering this in the prompt:

    name1,name2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,name3
    Shawn

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    I didn't say it was bullet proof, it is just another approach. You could get the same bad result if you hit enter, or press ok in the other example without entering a name. You should always do some sort of validation to ensure you are getting the type of data you are expecting.

  • #10
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    indeed, and btw, if you use the method where the user must type end to end the name input, make sure it's not case-sensitive
    Shawn


  •  

    Posting Permissions

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