zedjr Posted August 11, 2011 Share Posted August 11, 2011 this is code from my book i am studying.i am trying to figure out how it will just ask for ten names and no more then go directly to writing these names.Is probably an easy solution but i just don't see it.// create the arraynames = new Array();i = 0;// loop and prompt for namesdo { j = 0; next = window.prompt("Enter the Next Name", ""); if (next > " ") names = next; j++; i = i + 1; } while (next > " ");document.write("<h2>" + (names.length) + " names entered.</h2>");// display all of the namesnames.sort();document.write("<ol>");for (i in names) { document.write("<li>" + names + "<br>");}document.write("</ol>"); Link to comment Share on other sites More sharing options...
justsomeguy Posted August 11, 2011 Share Posted August 11, 2011 It doesn't stop at 10, it keeps going until the input is either blank or a space. Link to comment Share on other sites More sharing options...
zedjr Posted August 11, 2011 Author Share Posted August 11, 2011 yes i know. I am trying to figure out how i can stop it at ten? Link to comment Share on other sites More sharing options...
jeffman Posted August 11, 2011 Share Posted August 11, 2011 This part executes the code inside the loop if a condition is met, and terminates the loop if the condition is not met:while (next > " ");So change the condition. Happily, you already have a variable that increments every time the loop executes. Do something with it. Link to comment Share on other sites More sharing options...
zedjr Posted August 11, 2011 Author Share Posted August 11, 2011 So can u tell me where to put that loop as i have tried a few places and just cant figure out where. the whole while (next > " ") confuses me a bit also. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 11, 2011 Share Posted August 11, 2011 You don't need to move the loop. You're just trying to change when the loop ends. This:while (next > " ")says to keep looping as long as next is greater than " ", or end the loop if next is blank. If you want to end the loop after 10 times, then that's the only part you need to change. The variable i is already used to keep track of how many times the loop runs, so you can use that to figure out when to stop. You want it to loop as long as i is less than 10. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.