-3

I am attemping to return the average ages of all the folks in this array who has an age is greater than the median value.

var data = { users: [{ first_name: "Mikey", last_name: "Mouse", age: 24 
    }, { first_name: "Donald", lastName: "Duck", age: 29 }, { first_name: "Woody", lastName: "Woodpecker", age: null }, { first_name: "Bugs", lastName: "Bunny", age: 32 }] },
        sum = 0,
        count = 0,
        average;

    data.users.forEach(function (user) {
    if (typeof user.age === 'number') {
        sum += user.age;
        ++count;
    }
});

average = sum / count;

console.log(average);
flashdev
  • 1
  • 1
  • 2
    Awesome. Let us know how things turn out. – llama Oct 15 '17 at 17:54
  • 1
    You forgot the _question_ part in your question… – Sebastian Simon Oct 15 '17 at 17:55
  • I have an "average" median returning but I need to grab all the ages that are greater than the average. Sorry.. I am still learning my way around StackOV – flashdev Oct 15 '17 at 17:57
  • Greater than the average? Your title says “greater than the median”. What is it now? What exactly isn’t working? What is your expected result? Why? What is your current result? I have no idea what you need help with. – Sebastian Simon Oct 15 '17 at 17:59
  • I will improve my communication skills ASAP. So the "average" is the median value. And what I am trying to do is now return all the ages that are greater than the "average". – flashdev Oct 15 '17 at 18:03

1 Answers1

0

In your array, only one user (Bugs Bunny) is older than the median age, so the average would just be his age, 32. But here is the code to do it for any set of users, given the data structure in your question.

var data = { 
    users: [
        { first_name: "Mikey", last_name: "Mouse", age: 24 },
        { first_name: "Donald", lastName: "Duck", age: 29 },
        { first_name: "Woody", lastName: "Woodpecker", age: null },
        { first_name: "Bugs", lastName: "Bunny", age: 32 }
    ] 
};

// first calculate the median
// see https://stackoverflow.com/questions/25305640/find-median-values-from-array-in-javascript-8-values-or-9-values/39639518#39639518
function median(arr){
  arr = arr.sort(function(a, b){ return a - b; });
  var i = arr.length / 2;
  return i % 1 == 0 ? (arr[i - 1] + arr[i]) / 2 : arr[Math.floor(i)];
}

var med = median(data.users.map(function(d){ return d.age; }).filter(function(d){ return typeof d == "number"; }));

// now calculate the average of those users whose age is above the median
var users_above_median = data.users.filter(function(d){ return d.age > med; });

function average(arr){
  return sum(arr) / arr.length;
}
function sum(arr){
  return arr.reduce(function(d, i){ return i + d; });
}

var avg = average(users_above_median.map(function(d){ return d.age; }));

console.log(avg);
Harry Stevens
  • 1,165
  • 1
  • 13
  • 16
  • That worked flawlessly! Where do I accept this answer? – flashdev Oct 15 '17 at 18:17
  • @flashdev You may not be able to accept or even vote for answers if your reputation is too low. Also, judging by your comments above, it seems you are confused about the terms "average", "mean", and "median". – Harry Stevens Oct 16 '17 at 05:03
  • **Mean** is the value obtained by dividing the sum of a set of values by their number. For the set [1, 7, 10], the mean is (1+7+10) / 3, or 18 / 3, or 6. **Average** is typically used as a synonym for mean. **Median** is the value that lies at the midpoint of a set of values. For example, the median of the set [1, 7, 10] is 7, as there are an equal quantity of values above and below it. For sets that have an even number of values, the median is the average of the two values nearest the middle. So for the set [1, 5, 7, 10], the two values nearest the middle are 5 and 7, so the median is 6. – Harry Stevens Oct 16 '17 at 05:03