|
Book Section

General Section

Other Sites

|
JavaScript Workshop Forums
| View previous topic :: View next topic |
| Author |
Message |
sgtshelton233 New member

Joined: 31 Jan 2011 Posts: 1
|
Posted: Mon Jan 31, 2011 6:29 am Post subject: Newbie Question: Restarting a function |
|
|
Hello All,
I am extremely new to this and have a question. For a homework assignment, part of the process was to ask the user if they would like to restart the questions. First it asks for a number, then checks if the number is odd or even, then displays the next sequential numbers up to 100 by (5) if even or by (10) if odd. Then it is "supposed" to ask if you would like to keep going, lol. Obviously by looking at my code you will see that I was unable to do this. I was however able to get the code in there to ask if the user wants to stay or leave the page, which was another part of the assignment. Like I said, I am VERY new to this, so don't laugh at my ugly code. Thanks for your help.
<script type="text/javascript">
var num = prompt("Check your number", "Type your number here");
num = parseInt(num);
if (num == 0)
{
alert("The number is zero");
}
else if (num%2)
{
do{
num = num + 10;
alert(num);
}
while (num < 90);
}
else
{
do{
num = num + 5;
alert(num);
}
while (num < 95);
}
var where_to= confirm("Do you want to leave this page?");
if (where_to== true)
{
window.location="http://www.yahoo.com";
}
else
{
exit
}
</script> |
|
| Back to top |
|
 |
bedlam Member

Joined: 04 Sep 2010 Posts: 14
|
Posted: Wed Feb 02, 2011 9:28 am Post subject: |
|
|
You need one more loop (ie. a do while)that wraps up everything you want to repeat, and add one more prompt just before the end of the loop.
You will start your loop just above the line
"var num = prompt("Check your number", "Type your number here"); ".
You will finish your loop just below the lines
" while (num < 95);
}".
Just above the end of your loop, you will prompt one more time and ask if they want to continue. The answer to this prompt is what you will check in the end of your loop conditional statement.
I only gave you clues. If you are still stuck, let me know - I think I am in your class.
Andrew |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2002 phpBB Group
|
|
(c) 1997-2002 Starling Technologies and
Michael Moncur. Portions (c) Sams Publishing.
|
|
|