I’m new to javascript and am trying to make a simple calculator that will get the average, minimum value, and maximum value from a set of user inputs. I have most of it, but it isn’t working right and I don’t know enough to fix it even though it should be simple. I also don’t know what to initialize the min and max values so that if the user inputs a number that it will work right and give the correct max or min number and the intialized value isn’t higher/lower than the user input. Thanks in advance.
[code]
//Prompt user for temperatures
var userValue;
var minValue = 32;
var average;
var maxValue = 32;
var numOfInputs = 0;
var total;
userValue = prompt(“Enter a fahrenheit temperature (i.e. 65). Type ‘quit’ when you are finished.”);
//Loop through results and test data. Do math for correct numbers.
while(userValue != “quit”){
userValue = parseFloat(userValue);
if(isNaN(userValue)){
alert(userValue + ” is not a number.”);
}else if(userValue > 300){
alert(userValue + ” is too large”);
}else if(userValue < -300){
alert(userValue + ” is too small.”);
}else{
total += userValue;
numOfInputs++;
average = total / numOfInputs;
if(userValue <= minValue){
minValue = userValue;
}
if(userValue >= maxValue){
maxValue = userValue;
}
}
userValue = prompt(“Enter a temperature (i.e. 65). Type ‘quit’ when you are finished.”);
}
//Display output
document.writeln(“You entered ” + numOfInputs + ” numbers.”);
document.writeln(“The Maximum value is ” + maxValue);
document.writeln(“The Minimum value is ” + minValue);
document.writeln(“The Average value is” + average);