From d8e30004fa15505aeeaed0989b0ad43000ccfabe Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 28 Oct 2019 17:50:18 +0100 Subject: [PATCH] Create reduce.md One exercise that aims to replicate the given example in the explanation, but with a different set of items. One other exercise to demonstrate that the accumulator can be anything by expecting an object. --- docs/Exercise/JavaScript_Basics/reduce.md | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 docs/Exercise/JavaScript_Basics/reduce.md diff --git a/docs/Exercise/JavaScript_Basics/reduce.md b/docs/Exercise/JavaScript_Basics/reduce.md new file mode 100644 index 0000000..05fd5ad --- /dev/null +++ b/docs/Exercise/JavaScript_Basics/reduce.md @@ -0,0 +1,53 @@ +# Reduce exercises + +The reduce method allows you to "reduce" the contents of an Array into one value. + +## 1. Sum of prices +Write a function that sums up the prices in a shopping cart. + +```js +const items = [ + { name: 'Flour', price: 1 }, + { name: 'Tomatos', price: 4 }, + { name: 'Cucumbers', price: 2 }, + { name: 'Cheese', price: 7 }, + { name: 'Wine', price: 14 }, +]; + +const expectedResult = 28; +``` + +## 2. Grades by course +Given a list of grades with their course, return an object where the key is the name of the course and the value is a list of the corresponding grades. + +```js +const grades = [ + { course: 'Algebra', test: 1, grade: 'A'}, + { course: 'Algebra', test: 2, grade: 'C'}, + { course: 'Algebra', test: 3, grade: 'B'}, + { course: 'Algorithms & Datastructures', test: 1, grade: 'D'}, + { course: 'Algorithms & Datastructures', test: 2, grade: 'C'}, + { course: 'Algorithms & Datastructures', test: 3, grade: 'C+'}, + { course: 'English', test: 1, grade: 'B'}, + { course: 'English', test: 2, grade: 'C'}, + { course: 'English', test: 3, grade: 'B'}, +]; + +const expectedResult = { + 'Algebra': [ + { course: 'Algebra', test: 1, grade: 'A'}, + { course: 'Algebra', test: 2, grade: 'C'}, + { course: 'Algebra', test: 3, grade: 'B'}, + ], + 'Algorithms & Datastructures': [ + { course: 'Algorithms & Datastructures', test: 1, grade: 'D'}, + { course: 'Algorithms & Datastructures', test: 2, grade: 'C'}, + { course: 'Algorithms & Datastructures', test: 3, grade: 'C+'}, + ], + 'English': [ + { course: 'English', test: 1, grade: 'B'}, + { course: 'English', test: 2, grade: 'C'}, + { course: 'English', test: 3, grade: 'B'}, + ] +}; +```