var inside = require('turf-inside');

/**
 * Calculates the average value of a field for points
 * within a set of polygons.
 *
 * @module turf/average
 * @param {FeatureCollection} polygons a FeatureCollection of {@link Polygon} features
 * @param {FeatureCollection} points a FeatureCollection of {@link Point} features
 * @param {string} field the field in the `points` features from which to pull values to average
 * @param {string} outputField the field in the `polygons` FeatureCollection to put results of the averages
 * @return {FeatureCollection} a FeatureCollection of {@link Polygon} features with the value of `outField` set to the calculated average
 * @example
 * var polygons = turf.featurecollection([
 *  turf.polygon([[
 *    [10.666351, 59.890659],
 *    [10.666351, 59.936784],
 *    [10.762481, 59.936784],
 *    [10.762481, 59.890659],
 *    [10.666351, 59.890659]
 *  ]]),
 *  turf.polygon([[
 *    [10.764541, 59.889281],
 *    [10.764541, 59.937128],
 *    [10.866165, 59.937128],
 *    [10.866165, 59.889281],
 *    [10.764541, 59.889281]
 *  ]])
 * ]);
 * var points = turf.featurecollection([
 *  turf.point(10.724029, 59.926807, {population: 200}),
 *  turf.point(10.715789, 59.904778, {population: 600}),
 *  turf.point(10.746002, 59.908566, {population: 100}),
 *  turf.point(10.806427, 59.908910, {population: 200}),
 *  turf.point(10.79544, 59.931624, {population: 300})
 * ]);
 *
 * var averaged = turf.average(
 *  polygons, points, 'population', 'pop_avg');
 *
 * var result = turf.featurecollection(
 *  points.features.concat(averaged.features));
 *
 * //=result
 */
module.exports = function(polyFC, ptFC, inField, outField, done){
  polyFC.features.forEach(function(poly){
    if(!poly.properties) poly.properties = {};
    var values = [];
    ptFC.features.forEach(function(pt){
      if (inside(pt, poly)) values.push(pt.properties[inField]);
    });
    poly.properties[outField] = average(values);
  });

  return polyFC;
}

function average(values) {
  var sum = 0;
  for (var i = 0; i < values.length; i++) {
    sum += values[i];
  }
  return sum / values.length;
}

@@ -1127,24 +1127,25 @@
  turf.point(
+%5B
 10.724029, 5
@@ -1152,16 +1152,17 @@
 9.926807
+%5D
 , %7Bpopul
@@ -1186,24 +1186,25 @@
  turf.point(
+%5B
 10.715789, 5
@@ -1211,16 +1211,17 @@
 9.904778
+%5D
 , %7Bpopul
@@ -1245,24 +1245,25 @@
  turf.point(
+%5B
 10.746002, 5
@@ -1270,16 +1270,17 @@
 9.908566
+%5D
 , %7Bpopul
@@ -1308,16 +1308,17 @@
 f.point(
+%5B
 10.80642
@@ -1329,16 +1329,17 @@
 9.908910
+%5D
 , %7Bpopul
@@ -1367,16 +1367,17 @@
 f.point(
+%5B
 10.79544
@@ -1387,16 +1387,17 @@
 9.931624
+%5D
 , %7Bpopul

Add square brackets for points