From 98f119f0ed64ca0426b9a11e629273d5e6cb9669 Mon Sep 17 00:00:00 2001 From: orbis Date: Fri, 3 Jun 2022 22:20:18 +0300 Subject: [PATCH 1/2] fix example --- JavaScript/1-math.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/JavaScript/1-math.js b/JavaScript/1-math.js index 0b8ae4d..5268847 100644 --- a/JavaScript/1-math.js +++ b/JavaScript/1-math.js @@ -1,7 +1,6 @@ 'use strict'; -const { sin } = Math; -const π = Math.PI; +const { sin, PI: π } = Math; const inverse = f => x => 1 / f(x); From b5668faf395ba14369db9ebdeb30b806c1bc1d85 Mon Sep 17 00:00:00 2001 From: orbis Date: Fri, 3 Jun 2022 23:09:28 +0300 Subject: [PATCH 2/2] homework --- Exercises/1-callback.js | 8 +++++++- Exercises/2-closure.js | 2 +- Exercises/3-wrapper.js | 19 ++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Exercises/1-callback.js b/Exercises/1-callback.js index 8270a12..808648c 100644 --- a/Exercises/1-callback.js +++ b/Exercises/1-callback.js @@ -1,5 +1,11 @@ 'use strict'; -const iterate = (obj, callback) => null; +const iterate = (obj, callback) => { + const keys = Object.keys(obj); + for (const key of keys) { + const value = obj[key]; + callback(key, value, obj); + } +}; module.exports = { iterate }; diff --git a/Exercises/2-closure.js b/Exercises/2-closure.js index 0f07103..7b71264 100644 --- a/Exercises/2-closure.js +++ b/Exercises/2-closure.js @@ -1,5 +1,5 @@ 'use strict'; -const store = x => null; +const store = x => () => x; module.exports = { store }; diff --git a/Exercises/3-wrapper.js b/Exercises/3-wrapper.js index fb7207e..96602ef 100644 --- a/Exercises/3-wrapper.js +++ b/Exercises/3-wrapper.js @@ -1,5 +1,22 @@ 'use strict'; -const contract = (fn, ...types) => null; +const contract = (fn, ...types) => (...args) => { + + for (let i = 0; i < types.length - 1; i++) { + const arg = args[i]; + const curType = types[i].name.toLowerCase(); + + if (typeof arg !== curType) { + throw TypeError(`argument ${args[i]} is not a ${types[i].name}`); + } + } + + const result = fn(...args); + const resultType = types[types.length - 1].name.toLowerCase(); + if (typeof result !== resultType) { + throw TypeError(`result ${result} is not a ${resultType}`); + } + return result; +}; module.exports = { contract };