简体   繁体   中英

im trying to find an average of some numbers in my array but not all of them

alright, I've been looking around, but im trying to find the average of a set of numbers over 1000 and get it to ignore everything else. this is what I have but I'm not sure how to even start the code itself. i do know that to find an average of everything you take the sum divided by the length. but I want it to ignore anything under 1000. thanks in advance.

<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title></title>

<script>
/* 
defining table
<input:  none 
processing: get the last element in each array 
output: the last element in the arrays 
*/
function averageBig(list){
// this is the list of arrays needed 
var list1=[70, 1010, 950,2014,6];
var list2 = [ -72.3, 3000, 873, 2312, 68, 7501.3]
var list3 = [ 70, 950, 671, 6 ]
// this should test my avg function by calling it three times 
var avg1 = average(list1);
var avg2 = average(list2);
var avg3 = average(list3);
// output the answers 
var output =  avg1 + '<br>' +
        avg2 + '<br>' +
        avg3





document.getElementById('outputdiv').innerHTML = output;
}

function average(list)
var avg = ()
</script>

</head>
<body>


 <button type="button" onclick="averageBig()">test me </button> 
  <div id="outputdiv"></div>
 </body>
 </html>

any help is appreciated.

So first you need to filter each list to get the numbers over 1000 - then you need to average the resultant numbers that match that criterion.

Note that on passing each list to the averaginh function - the first step is to filter that values - then usinging tht filtered array - calculate the average. The traditional approach to averageing numbers is to iterate over the array - adding the numbers together and then dividing by teh number of items.

The newer approach is to use the reduce() method which allows the same functionality in a single line. Also - note the 0 at the end of the reduce method - this acts as the starting number for the first iteration of the reduce method.

Not sure if this is intentional - but the 3rd array / list does not have any numbers that meet the greater than 1000 criterion - so the reduce method returns NaN and the function has to acoomodate that -i am soding it be returning a string to say what the issue is.

Also - you could extend this to include the number that you need to exceed as an argument into the function as well.

 function averageBig(){ // this is the list of arrays needed let list1=[70, 1010, 950,2014,6]; let list2 = [ -72.3, 3000, 873, 2312, 68, 7501.3]; let list3 = [ 70, 950, 671, 6 ] // this should test my avg function by calling it three times var avg1 = average(list1); var avg2 = average(list2); var avg3 = average(list3); // output the answers var output = avg1 + '<br/>' + avg2 + '<br/>' + avg3 document.getElementById('outputdiv').innerHTML = output; } function average(arr) { // step 1 - filter the incoming list to match the criterion let numArr = arr.filter(num => num > 1000); // step 2 calculate the average of the array values let ave = Math.round(numArr.reduce((a, b) => (a + b), 0) / numArr.length); // step 3 - accomodate the issue of not having a valid result if(isNaN(ave)) {ave = 'No average possible'} // step 4 return the result (either the rounded average - or the explanatory string) return ave }
 <button type="button" onclick="averageBig()">test me </button> <div id="outputdiv"></div>

Just make use of your average() method of sum up numbers that are above 1000. Finally divide it by the total count of numbers above 1000. Here's a simple approach:

function average(list){
  var sum=0, length=0;
  for(element of list)
  {
    if (element > 1000)
    {
      sum+=element;
      length++;
    }
  }
  return isNaN(sum/length) ? 'No result' : sum/length
}

NOTE: You can also use filters and reduce as @gavgrif's answer. I just answered in its simplest possible way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM