bhylu.com

Advertisements

JS Exercises

Practise by changing the data before changing the algorithm. Find sessions with space and calculate their total available places.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

Before you run it

Write down the result you expect and explain which line produces it. Then run the example and compare your reasoning with the result below.

A practical example

JavaScript

const sessions = [{places: 0}, {places: 3}, {places: 5}];
const total = sessions.filter(session => session.places > 0).reduce((sum, session) => sum + session.places, 0);
console.log(total);

What happens

The solution produces 8. First try writing the filter and reduce yourself.