SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with an prompt to array in a while loop

    my assignment
    -----------------------------------
    write a while loop that prompts user to enter name

    add their names to an array

    if they enter "exit" end the prompting

    sort array and list in sorted order
    -----------------------------------
    this is what i got so far. sooo confused because
    i cant get the user input into an array

    var names = new Array();
    var loopCounter;
    loopCounter = 0;

    while (x != "exit")
    {
    var names[loopCounter] = prompt("enter name","");

    loopCounter++
    }

    var i;

    names.sort();

    for (i=0;i<names.length;i++)
    {
    document.write(names[i] + "<br>");
    }

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sicbag View Post
    while (x != "exit")
    Your while loop depends upon the value of x, which doesn't appear to be declared or referenced anywhere else.
    Use the error console, which will be trying to tell you this.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oooh ok duh lol. so i changed x to names
    because if they type exit it should stop right?
    error console now says i need ; before statement var names[loopCounter] = prompt("enter name","");
    i cant find where i need to add another ;??


    var names = new Array();
    var loopCounter;
    loopCounter = 0;

    while (names != "exit")
    {
    var names[loopCounter] = prompt("enter name","");

    loopCounter++
    }

    var i;

    names.sort();

    for (i=0;i<names.length;i++)
    {
    document.write(names[i] + "<br>");
    }

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    while (names != "exit")
    Now you're comparing an array to a string, which doesn't make sense. Clearly you know how to subscript an array, so do it.
    Tab-indentation is a crime against humanity.

  5. #5
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,233
    Mentioned
    47 Post(s)
    Tagged
    1 Thread(s)
    Depends on if the tools needed for the assignment were adequately explained.

    I can see what's wrong with it, but I wouldn't have understood why exactly 2 months ago. Sicbag, start with just the beginning chunks of your code and make liberal use of alert() or console.log or whatever you like to see what's going on at any particular point in your code. What happens if you do alert(names)? You'll see the problem right away.

  6. #6
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i figured most of it out now but now im wondering how do i ignore data or keep information someone has entered from entering the array.

    it works now but when the user types "exit". it goes into the array. i don't want it to enter the array just exit the prompting. sorry for the dumb question, im taking this class online and theres is no examples of anything like this


    <script>
    function askMe()
    {
    var names = new Array();
    var loopCounter;
    loopCounter = 0;
    var myResponse;
    var nowDate = new Date();
    var currentDay = nowDate.getDate();
    var nowMonth = nowDate.getMonth();
    var nowYear = nowDate.getFullYear();
    var months = new Array();

    while (myResponse != "exit")
    {
    myResponse = prompt("What's your favorite Lord of the Rings Character?\n\rtype 'exit' to leave prompting","");

    names[loopCounter] = myResponse;

    loopCounter++;

    }

    var i;

    names.sort();

    for (i=0;i<names.length;i++)
    {
    document.write("<li>" + names[i] + "<br>");
    }

    document.write("<br>" + "<b>Todays Date:" + [nowMonth+1] + "/" + currentDay + "/" + nowYear + "<br>" + "<br></b>");
    document.write("Press the Reload/Refresh Button to ask again");
    }
    </script>

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    You can use a one-and-a-half loop.

    Code javascript:
    while (true) {
        myResponse = prompt("What's your favorite Lord of the Rings Character?\n\rtype 'exit' to leave prompting","");
        if (myResponse === "exit") {
            break;
        }
        names[loopCounter] = myResponse;
        loopCounter++;
    }

    Or, you could use a different type of loop construct:

    Code javascript:
    myResponse = '';
    do {
        if (myResponse > '') {
            loopCounter++;
            names[loopCounter] = myResponse;
        }
        myResponse = prompt("What's your favorite Lord of the Rings Character?\n\rtype 'exit' to leave prompting","");
    } while (myResponse !== 'exit');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think we've gone over triple +++ or !== what does that mean? but ya im lookin through my notes again just in case

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by sicbag View Post
    I don't think we've gone over triple +++ or !== what does that mean? but ya im lookin through my notes again just in case
    Using === instead of ==, and !== instead of != are the preferred comparisons to use in JavaScript. They help to avoid problems where the loose comparison operators match with unexpected terms.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    omg the do while loop is it! and that was in my notes stupid me *sigh* thank you so much for the help and yeah now i understand. took me awhile to figure out why prompt had to be below the if statement but now i understand. thanks to everybody for helping me


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
  •