Jump to content

what do i do?


zedjr
 Share

Recommended Posts

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

yes i know. I am trying to figure out how i can stop it at ten?

Link to comment
Share on other sites

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...