title: how to write a function to return compiled data from a json object
body: <p>I recently had this question on a technical assessment. I've already turned in the assignment but I know I got the answer wrong. I'd love if someone could shed some light on this for me. The functions I wrote are below the question. When I tested them they did not work.
  Thanks for looking. ​</p>
  <p>Consider the data structure below. We want to know the names of the individuals whose sales figures exceed $2000, and also the average sale amount for all the employees in the data structure. Please write the necessary code to produce this output in either Python or Javascript. Assume your runtime environment has a JSON parser and a print function available. Don’t be too concerned with the specifics of these functions or the output format. We are more interested in your logic than your syntax. Indicate any assumptions you make in your solution.</p>
  <pre><code>}
  &quot;Results&quot;: [
  {
  &quot;Name&quot;: &quot;Frank Jones&quot;,
  &quot;Department&quot;: &quot;North America&quot;,
  &quot;Sales&quot;: 2500 
  }, 
  {
  &quot;Name&quot;: &quot;Sally Smith&quot;, 
  &quot;Department&quot;: &quot;North America&quot;, 
  &quot;Sales&quot;: 2200
  },
  {
  &quot;Name&quot;: &quot;Ed Kramer&quot;,
  &quot;Department&quot;: &quot;Europe&quot;,
  &quot;Sales&quot;: 1700 },
  {
  &quot;Name&quot;: &quot;Susan Johnson&quot;, 
  &quot;Department&quot;: &quot;Asia&quot;, 
  &quot;Sales&quot;: 2000
  }
  ] 
  }
  </code></pre>
  <p>For sales higher than $2000</p>
  <pre><code>    var sales = function(results) {
       if (results.sales &gt;= 2000) {
              print(results.Name)
     }
  };
  </code></pre>
  <p>For average sales:
  I declared an empty array, used a for loop to push all results.sales into declared array. Used .reduce array method to get the total of all sales numbers, then divided by the array length to get they average sales number.</p>
  <pre><code>Function findAvg() {
  let sales = [ ];
  for (var i = 0; i &lt; results.length; i++) {
      sales.push(results[i].sales)
  };
  const reducer = (previousValue, currentValue) =&gt; previousValue + currentValue;
      return sales.reduce(reducer)/sales.length
  }           
  </code></pre>