Add lesson 3: Arrays: map, filter, reduce
3 changed files with 31 additions and 0 deletions
lessons/03-arrays-map-filter-reduce/README.md ADDED
| @@ -0,0 +1,19 @@ | ||
| 1 | +# Lesson 3: Arrays: map, filter, reduce | |
| 2 | + | |
| 3 | +In this lesson you'll work through arrays: map, filter, reduce step by step, with short exercises after each idea. | |
| 4 | + | |
| 5 | +Full lesson: [https://glibcode.net/courses/javascript-web/lessons/3](https://glibcode.net/courses/javascript-web/lessons/3) | |
| 6 | + | |
| 7 | +## Files | |
| 8 | + | |
| 9 | +- `arrays.js`: map, filter and reduce on order totals | |
| 10 | + | |
| 11 | +## Run it | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +node arrays.js | |
| 15 | +``` | |
| 16 | + | |
| 17 | +## Exercises | |
| 18 | + | |
| 19 | +See [exercises.md](exercises.md). Ask in the forum once you've tried. | |
lessons/03-arrays-map-filter-reduce/arrays.js ADDED
| @@ -0,0 +1,7 @@ | ||
| 1 | +const orders = [12.5, 80, 7.25, 43]; | |
| 2 | + | |
| 3 | +const big = orders.filter(total => total > 20); | |
| 4 | +const withTax = big.map(total => total * 1.19); | |
| 5 | +const sum = withTax.reduce((a, b) => a + b, 0); | |
| 6 | + | |
| 7 | +console.log(sum.toFixed(2)); // "146.37" | |
lessons/03-arrays-map-filter-reduce/exercises.md ADDED
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Exercises: lesson 3 | |
| 2 | + | |
| 3 | +1. Combine this lesson with the previous one: use something from both in a small program of your own. | |
| 4 | +2. What happens with an empty input? Change the code so it handles that case with a clear message. | |
| 5 | +3. Look up one function or command the example uses in the official documentation. Note one option you didn't know. | |