www.webdeveloper.com
+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2012
    Posts
    2

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

  2. #2
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    551
    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:
    Code:
    if(userValue*1 <= minValue*1){
      minValue = userValue;
    }
    if(userValue*1 >= maxValue*1){
      maxValue = userValue;
    }

  3. #3
    Join Date
    Sep 2012
    Posts
    2

    Average says Nan

    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/CS...lab2/index.htm

  4. #4
    Join Date
    Sep 2007
    Posts
    275
    I wrote
    var total = 0;
    instead of
    var total;

    Code:
     
    
    <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.

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,078
    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 (/[^\s\d\.+-]+/.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>
    Last edited by 007Julien; 09-17-2012 at 06:24 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles