/    Sign up×
Community /Pin to ProfileBookmark

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);

[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@jalarieSep 15.2012 — Initialize your minValue to 301 because you check to make sure all entered values are less than that. Initialize maxValue to -301 similarly.

Force NUMERIC comparisons (default is string) by multiplying by 1 on these lines:
<i>
</i>if(userValue*1 &lt;= minValue*1){
minValue = userValue;
}
if(userValue*1 &gt;= maxValue*1){
maxValue = userValue;
}
Copy linkTweet thisAlerts:
@whiteaeauthorSep 15.2012 — 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?

You can go here to test it out.

http://einstein.etsu.edu/~whiteae/CSCI3110/lab2/index.htm
Copy linkTweet thisAlerts:
@Ay__351_eSep 16.2012 — I wrote

var [COLOR="#40E0D0"][B]total = 0[/B][/COLOR];

instead of

[COLOR="#FF0000"]var total;[/COLOR]



&lt;script type="text/javascript"&gt;

//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.");

<i> </i>//Loop through results and test data. Do math for correct numbers.
<i> </i>while(userValue != "quit"){
<i> </i> userValue = parseFloat(userValue);
<i> </i> if(isNaN(userValue)){
<i> </i> alert(userValue + " is not a number.");
<i> </i> }else if(userValue &gt; 300){
<i> </i> alert(userValue + " is too large");
<i> </i> }else if(userValue &lt; -300){
<i> </i> alert(userValue + " is too small.");
<i> </i> }else{
<i> </i> total += userValue;
<i> </i> numOfInputs++;

<i> </i> average = total / numOfInputs;
alert("total= " + total);
alert("averaj= " + average);
alert( "numOfInputs = "+ numOfInputs);
if(userValue &lt;= minValue){
minValue = userValue;
}
if(userValue &gt;= maxValue){
maxValue = userValue;
}
}
userValue = prompt("Enter a temperature (i.e. 65). Type 'quit' when you are finished.");
}

<i> </i> //Display output
<i> </i> document.writeln("You entered " + numOfInputs + " numbers.");
<i> </i>document.writeln("The Maximum value is " + maxValue);
<i> </i>document.writeln("The Minimum value is " + minValue);
<i> </i>document.writeln("The Average value is" + average);

&lt;/script&gt;

Copy linkTweet thisAlerts:
@007JulienSep 17.2012 — A simple variant which allow to see (and remove) the entries...
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
<style type="text/css">
body {font-familly:Garamond;background:#ccc;text-align:center;}
#page {display:block;width:600px;margin:20px auto;padding:20px;background:#fff;}
p {margin:5px 0;}
.nptCls {width:500px;}
</style>
</head>
<body>
<div id="page">
<p>Enter temperatures separed by white spaces... </p>

<p><input type="text" id="nptTxt" class="nptCls" onkeyup="analyse(event,this)"></p>
<p id="rsl"></p>
<script type="text/javascript">
function analyse(e,t){var i,j,k=e.keyCode|| e.charCode,r,s,v;
if (k!=32) return;
if (/[^sd.+-]+/.test(t.value)) {alert('Not valid entry !');return}
v=t.value.replace(/^s+|s+$/,'')
v=v.replace(/s+/g,' ').split(' ').sort(function(a,b){return a-b});
s=0;for (j=v.length,i=0;i<j;i++) s+=1*v[i];
if (!j) return;
r='Min value : '+v[0]+'<br>Max value : '+v[j-1]+'<br>Average value : '+(s/j).toFixed(2);
document.getElementById('rsl').innerHTML=r;
}
</script>
</body>
</html>[/CODE]
×

Success!

Help @whiteae spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...