Skip to content
Avio Lines
EasyJavaScriptPractice mode: untimed, 3 hints

Moving average

movingAverage(values, size) returns the average of every run of size consecutive values, in order. With fewer than size values it returns an empty array.

The code has exactly one planted bug. Reproduce it with the tests, fix it with the smallest change, then submit.

Visible tests

  1. three windows of two

    • movingAverage([1,2,3,4], 2) → [1.5,2.5,3.5]
  2. too few values

    • movingAverage([10,20], 3) → []

Hints 0/3

    // Average of every run of `size` consecutive readings, in order.
    function movingAverage(values, size) {
      const out = [];
      for (let i = 0; i + size < values.length; i++) {
        let sum = 0;
        for (let j = i; j < i + size; j++) sum += values[j];
        out.push(sum / size);
      }
      return out;
    }
    

    Ctrl/⌘+Enter runs, Ctrl/⌘+Shift+Enter submits. In the editor, Ctrl+M switches Tab to moving focus.

    Run works without an account. Log in to submit against the hidden tests and track your accuracy per bug type.

    Ready for pressure? Try a timed set: six snippets, 20 minutes, no hints.