Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: 'While' Loop Question in Basic Javascript Program

1. '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>
</body>

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