Book Section
-----
TY JavaScript 3rd Ed.
Teach Yourself JS 1.5
Teach Yourself DHTML
Teach Yourself JS 1.3
LLWW: JavaScript

General Section
-----
Discussion Forum
Articles / Tips
JavaScript Links
About the Author
Privacy Policy
Contact Me



Other Sites
-----
Website Workshop
JavaScript Weblog

JavaScript Workshop Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Newbie Question: Restarting a function

 
Post new topic   Reply to topic    JSWorkshop Forum Index -> JavaScript
View previous topic :: View next topic  
Author Message
sgtshelton233
New member
New member


Joined: 31 Jan 2011
Posts: 1

PostPosted: Mon Jan 31, 2011 6:29 am    Post subject: Newbie Question: Restarting a function Reply with quote

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
View user's profile Send private message
bedlam
Member
Member


Joined: 04 Sep 2010
Posts: 14

PostPosted: Wed Feb 02, 2011 9:28 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    JSWorkshop Forum Index -> JavaScript All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
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.