From 9d1076b02b333a5026d49ca881967da18ac59bfb Mon Sep 17 00:00:00 2001 From: Aaron Nguyen Date: Wed, 2 Mar 2022 14:58:03 -0500 Subject: [PATCH] Done exercise-1 --- 8-using-inheritance/exercises/exercise-1.js | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 8-using-inheritance/exercises/exercise-1.js diff --git a/8-using-inheritance/exercises/exercise-1.js b/8-using-inheritance/exercises/exercise-1.js new file mode 100644 index 0000000..97cda3b --- /dev/null +++ b/8-using-inheritance/exercises/exercise-1.js @@ -0,0 +1,26 @@ +"use strict"; + +class FunctionalSet extends Set { + filter(predicate) { + return new FunctionalSet([...this].filter(predicate)); + } + map(mapper) { + return new FunctionalSet([...this].map(mapper)); + } + reduce(accumulator, identity) { + return [...this].reduce(accumulator, identity); + } +} + +const set = new FunctionalSet(["Jack", "Jill", "Tom", "Jerry"]); + +const jSet = set.filter((name) => name.startsWith("J")); +const allCaps = set.map((name) => name.toUpperCase()); + +const totalLengthOfJWords = set + .filter((name) => name.startsWith("J")) + .reduce((total, word) => total + word.length, 0); + +console.log(jSet); +console.log(allCaps); +console.log(totalLengthOfJWords);