Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-23-2009, 01:49 AM   PM User | #1
baller923
New to the CF scene

 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
baller923 is an unknown quantity at this point
'While' Loop Question in Basic Javascript Program

I am having trouble figuring out where to put the 'while' loop. After the program has gone through, i would like to have a prompt for the user to restart the program, Y would restart and N would terminate it. Thanks for the help!

<script type="text/javascript">
//<![CDATA[
function runJS()
{
var sum = 0;
var average = 0;
var count = parseFloat(prompt("How many numbers do you want to sum up?", "3"));
var i=0;
var total=1;
var number=0;
{
for (var i=1; i<=count; i=i+1)
{
number=prompt("Enter #"+i, "");
total=total*parseFloat(number);
}

average = sum/3;
alert("The average is: " + average + ".\nThe sum is: " + sum + ".");
}
//]]>

more= prompt("Would you like to do another calculation? (Y/N)", "Y");}
</script>
</head>
<body onload="runJS();">
</body>
baller923 is offline   Reply With Quote
Old 10-23-2009, 02:20 AM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,554
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
no need for "while"...
Code:
<script type="text/javascript">
//<![CDATA[
function runJS(){
var sum = 0;
var average = 0;
var count = parseFloat(prompt("How many numbers do you want to sum up?", "3"));
 var i=0;
 var total=1;
 var number=0;
 
 for (var i=1; i<=count; i=i+1) {
     number=prompt("Enter #"+i, "");
     total=total*parseFloat(number);
 }

 average = sum/3;
alert("The average is: " + average + ".\nThe sum is: " + sum + ".");
if( prompt("Would you like to do another calculation? (Y/N)", "Y") ){runJS();}
}
//]]>
</script>
</head>
<body onload="runJS();">
</body>
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:44 AM.


Advertisement
Log in to turn off these ads.