Skip to content
Gallery
JS Fundamentals: Objects, Arrays, and Functions
Share
Explore
Functions

icon picker
Exercise: Implement forEach

Prompt

Implement forEach that iterates over a collection & for each element of the collection it applies a callback function on it.
NOTE: forEach doesn't have a return value, but rather simply runs the
callback function over each item in the input collection. The callback should
be called with the item, index|key and collection..
Definition: forEach(collection, callback)
Example Usage:
const weapons = ['Revolver','Knife','Lead Pipe','Candlestick'];

forEach(weapons, weapon => {
console.log(weapon);
});
// => "Revolver"
// => "Knife"
// => "Lead Pipe"
// => "Candlestick"

Code here

Loading…

Solution

The solution is in the solution branch, which can be checked out in the by selecting the version control tab on the right and then selecting the solution branch from the drop down menu.
If you are having trouble with the solutions are also available below.

forEach Solution

Next 👉


©2021 for

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.