One of the tasks for my web development course is as follows:
In order to convince your boss to allow adjustable thermostats in the office suite, you need to demonstrate that significant temperature variations occur in different offices and within each office on different days. Your assignment is to create a program that lets employees enter the noon temperature for five days and then displays the highest, lowest, and average temperatures. You should use a For loop to record the temperatures (Tip: Start by initializing variables for the highest and lowest temperatures with the first reading, then compare subsequent readings to determine if they are higher or lower.) Remember to use the parseFloat() method to convert temperature inputs into decimal numbers and display the average to one decimal place.
How can I calculate the average temperature?
<!DOCTYPE HTML>
<html>
<body>
<script type="text/javascript">
var high; // variable for highest temperature
var low; // variable for lowest temperature
var avg; // variable for average temperature
var temperatures = [];
for (var i = 0; i < 5; i++) {
high = temperatures[0];
low = temperatures[0];
temperatures.push(parseFloat(prompt("Enter the temperature for day " + (i+1))));
if (high < temperatures[i]) {
high = temperatures[i]; }
if (low > temperatures[i]) {
low = temperatures[i]; }
}
document.write("The highest temperature is " + high + ".<br>");
document.write("The lowest temperature was " + low + ".<br>");
document.write("The average temperature was " + avg + ".");
</script>
</body>
</html>