Help with calculating max, min, and average values from user input
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);
Okay thanks. But I think the average part is messing up because its taking in the word quit when the user wants to stop entering numbers. How do I fix that?
<script type="text/javascript">
//Prompt user for temperatures
var userValue;
var minValue = 32;
var average;
var maxValue = 32;
var numOfInputs = 0;
var total=0;
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;
alert("total= " + total);
alert("averaj= " + average);
alert( "numOfInputs = "+ 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);
</script>
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
Bookmarks