title: Calculate Running Average of Undetermined Length for Sensor
body: <p>I am trying to calculate the running average of a value returned by a sensor and run a command when the average value meets two criteria (array.length == x &amp;&amp; avg &lt; y).</p>
  
  <pre><code>let arr = []
  let avg = 0
  let sum = 0
  
  sensor.calibrate(() =&gt; {
    sensor.getMeasurement((volts) =&gt; {
      console.log(volts) // typical values are ~0.9 V
      runningAvg(volts, 0.85, 10)
    })
  })
  
  function runningAvg(item, threshold, length) {
    if (array.length &lt; length) {
      array.push(item)
    } else {
      array.shift().push(item)
      for(let i=0; i &lt; array.length; i++) {
        sum += array[i]
      }
      avg = sum / length
    }
    if(avg &lt; threshold &amp;&amp; array.length == length) console.log(avg)
  }
  </code></pre>
  
  <p>This code returned 0.</p>
  
  <p>How can I calculate the running average and run a bit of code when the average is &lt; threshold and array.length == length?</p>