BuiltWithNOF
Simple Average

<html>

<!-- Addition and Average Program  -->

<html xmlns =  "http://www.w3.org/1999/xhtml">
<head>
<title>An Addition and Average Program</title>

<script type = "text/javascript">
<!--
var firstNumber, // first string entered by  user
secondNumber, // second string entered by user
thirdNumber, // third string entered by user

number1, // first  number to add
number2, // second number to add
number3, // third number to add
sum; // sum of number1, number2 and number3

sum=0; // start sum off as a 0, and tell computer sum is a number

// read in first number from user as a string
firstNumber = window.prompt( "Enter first integer", "0" );
number1 =  parseInt( firstNumber ); // convert numbers from strings to integers
sum = sum + number1; // add number1 to 0.. Why 0?

// read in second number from user as a string
secondNumber = window.prompt( "Enter second  integer", "0" );
number2 = parseInt( secondNumber );  // convert numbers from strings to integers
sum = sum + number2; // add number2 to sum (number1)

thirdNumber = window.prompt( "Enter second integer", "0" );
number3 = parseInt( thirdNumber ); // convert numbers from strings to integers
sum = sum + number3; // add number2 to sum (number1)

// display the results
document.writeln( "<h1>The sum is " + sum + "</h1>"  );
document.writeln( "<h1>The average is" + sum/3 + "</h1>" );
// -->
</script>

</head>
<body>
<p>Click Refresh (or Reload) to run the script  again</p>
</body>
</html>

[Home] [Syllabus] [Sessions] [Writing Code] [Help] [Setup] [Instructor]