Web editor: "forEach is not a function" — array loses prototype methods in test runner

Describe the bug
When running tests in the web editor, array methods like forEach are unavailable inside the solution function. The test runner reports TypeError: birdsPerDay.forEach is not a function, even though the passed argument appears to be an array in console.log.

To Reproduce

  1. Open the “Bird Watcher” exercise in the web editor (JavaScript track).
  2. Submit a solution using forEach:

export function totalBirdCount(birdsPerDay) {

let total = 0;

birdsPerDay.forEach(element => { total += element; });

return total;

}

  1. Click “Run Tests”.
  2. See error: TypeError: birdsPerDay.forEach is not a function

Console output also shows:

[worker] could not be made Qh: /javascript-browser-test-runner-worker.mjs did not yield a valid worker

Expected behavior
forEach should work normally on the array passed by the test runner.

Workaround
Using a for loop with index access works, since it doesn’t rely on array prototype methods.

Environment

  • Browser: Chrome (with DevTools open)
  • Track: JavaScript
  • Exercise: Bird Watcher

The instructions say,

To practice, use a for loop to solve each of the tasks below.

and

Implement a function totalBirdCount that accepts an array-like object that contains the bird count per day.

This is by design and intentional. You should be using for.

2 Likes