From e237a859dbfa037f1304951eba826a186bb379fa Mon Sep 17 00:00:00 2001 From: Aaron Nguyen Date: Wed, 2 Mar 2022 21:01:32 -0500 Subject: [PATCH] Log learning 03/02/2022 on modules --- 8-using-inheritance/exercises/exercise-2.js | 16 ++++++++ 8-using-inheritance/exercises/exercise-3.js | 45 +++++++++++++++++++++ 8-using-inheritance/exercises/exercise-4.js | 23 +++++++++++ 9-using-modules/left.mjs | 5 +++ 9-using-modules/middle.mjs | 7 ++++ 9-using-modules/right.mjs | 6 +++ 9-using-modules/temperature.mjs | 32 +++++++++++++++ 7 files changed, 134 insertions(+) create mode 100644 8-using-inheritance/exercises/exercise-2.js create mode 100644 8-using-inheritance/exercises/exercise-3.js create mode 100644 8-using-inheritance/exercises/exercise-4.js create mode 100644 9-using-modules/left.mjs create mode 100644 9-using-modules/middle.mjs create mode 100644 9-using-modules/right.mjs create mode 100644 9-using-modules/temperature.mjs diff --git a/8-using-inheritance/exercises/exercise-2.js b/8-using-inheritance/exercises/exercise-2.js new file mode 100644 index 0000000..1cb6688 --- /dev/null +++ b/8-using-inheritance/exercises/exercise-2.js @@ -0,0 +1,16 @@ +"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"]).add("Bill"); +console.log(set); diff --git a/8-using-inheritance/exercises/exercise-3.js b/8-using-inheritance/exercises/exercise-3.js new file mode 100644 index 0000000..56641ba --- /dev/null +++ b/8-using-inheritance/exercises/exercise-3.js @@ -0,0 +1,45 @@ +"use strict"; + +class BoundedSet extends Set { + constructor(capacity, initialValues) { + super(); + this.capacity = capacity; + + if (initialValues.length <= capacity) { + initialValues.forEach((value) => this.add(value)); + } + } + + add(value) { + if (this.has(value)) return; + + if (this.size < this.capacity) { + super.add(value); + } else { + throw new Error(`exceeded vapacity of ${this.capacity} elements`); + } + } + + static get [Symbol.species]() { + return BoundedSet; + } +} + +const set = new BoundedSet(5, ["Apple", "Banana", "Grape", "Mangoe"]); + +set.add("Orange"); +set.add("Apple"); + +try { + set.add("Tangerine"); +} catch (ex) { + console.log(ex.message); // exceeded capacity of 5 elements +} + +set.delete("Grape"); +set.add("Peach"); +console.log(set.size); // 5 + +const set2 = new BoundedSet(2, ["Apple", "Banana", "Grape"]); +console.log(set2.size); // 0 +console.log(set2); // BoundedSet {capacity: 2} diff --git a/8-using-inheritance/exercises/exercise-4.js b/8-using-inheritance/exercises/exercise-4.js new file mode 100644 index 0000000..9616a80 --- /dev/null +++ b/8-using-inheritance/exercises/exercise-4.js @@ -0,0 +1,23 @@ +"use strict"; +class Base { + copy() { + const constructor = + Reflect.getPrototypeOf(this).constructor[Symbol.species] || + Reflect.getPrototypeOf(this).constructor; + return new constructor(); + } +} +class Derived1 extends Base { + static get [Symbol.species]() { + return Base; + } +} +class Derived2 extends Base { + static get [Symbol.species]() { + return Derived2; + } +} +const derived1 = new Derived1(); +const derived2 = new Derived2(); +console.log(derived1.copy()); //Base {} +console.log(derived2.copy()); //Derived2 {} diff --git a/9-using-modules/left.mjs b/9-using-modules/left.mjs new file mode 100644 index 0000000..755b216 --- /dev/null +++ b/9-using-modules/left.mjs @@ -0,0 +1,5 @@ +import { right } from "./right.mjs"; +import { middle } from "./middle.mjs"; + +middle(); +right(); diff --git a/9-using-modules/middle.mjs b/9-using-modules/middle.mjs new file mode 100644 index 0000000..2025306 --- /dev/null +++ b/9-using-modules/middle.mjs @@ -0,0 +1,7 @@ +import { right } from "./right.mjs"; + +console.log("executing middle module"); + +export const middle = function () { + console.log("middle called"); +}; diff --git a/9-using-modules/right.mjs b/9-using-modules/right.mjs new file mode 100644 index 0000000..39d08fb --- /dev/null +++ b/9-using-modules/right.mjs @@ -0,0 +1,6 @@ +console.log("executing right module"); +const message = "right called"; + +export const right = function () { + console.log(message); +}; diff --git a/9-using-modules/temperature.mjs b/9-using-modules/temperature.mjs new file mode 100644 index 0000000..dd2bc41 --- /dev/null +++ b/9-using-modules/temperature.mjs @@ -0,0 +1,32 @@ +export const FREEZING_POINT = 0; + +export function f2c(fahrenheit) { + return (fahrenheit - 32) / 1.8; +} + +export const temperaturePoints = { freezing: 0, boiling: 100 }; + +export class Thermostat { + constructor() {} +} + +const FREEZING_POINT_IN_F = 32; + +function c2f(celsius) { + return celsius * 1.8 + 32; +} + +const FREEZING_POINT_IN_K = 237.15, + BOILING_POINT_IN_K = 373.15; + +export { c2f, FREEZING_POINT_IN_K }; + +function c2k(celsius) { + return celsius + 273.15; +} + +export { c2k as celsiusToKelvin }; + +export default function unitsOfMeasures() { + return ["Celsius", "Delisle scale", "Fahrenheit", "Kelvin"]; +}