...

View Full Version : 'While' Loop Question in Basic Javascript Program



baller923
10-23-2009, 01:49 AM
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>

rnd me
10-23-2009, 02:20 AM
no need for "while"...



<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum