From d1fb70ef5123cdc668dacc681c84ef7645637c03 Mon Sep 17 00:00:00 2001 From: Daniil Denisyuk Date: Mon, 16 Dec 2019 16:05:20 +0200 Subject: [PATCH 1/2] work --- Exercises/1-callback.js | 13 ++++++++++++- Exercises/2-closure.js | 2 +- Exercises/3-wrapper.js | 14 +++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Exercises/1-callback.js b/Exercises/1-callback.js index 8270a12..970249d 100644 --- a/Exercises/1-callback.js +++ b/Exercises/1-callback.js @@ -1,5 +1,16 @@ 'use strict'; -const iterate = (obj, callback) => null; +const iterate = (object, callback) => { + for (let key in object){ + if(object.hasOwnProperty(key)) + callback(key, object[key]); + } +// for (let [key, value] of Object.entries(object) ) { +// callback(key, value); +// } +// Object.entries(object).forEach(([key, value])=>{ +// callback(key, value); +//}) +} module.exports = { iterate }; diff --git a/Exercises/2-closure.js b/Exercises/2-closure.js index 0f07103..d44f4a5 100644 --- a/Exercises/2-closure.js +++ b/Exercises/2-closure.js @@ -1,5 +1,5 @@ 'use strict'; -const store = x => null; +const store = value => () => value; module.exports = { store }; diff --git a/Exercises/3-wrapper.js b/Exercises/3-wrapper.js index fb7207e..ab9a02a 100644 --- a/Exercises/3-wrapper.js +++ b/Exercises/3-wrapper.js @@ -1,5 +1,17 @@ 'use strict'; -const contract = (fn, ...types) => null; +const contract = (fn, ...types) => (...args) => { + for(let index in args){ + const arg = args[index]; + if(arg !== types[index](arg)) throw new TypeError(`type of argument '${value}': '${typeof value}' does not match '${types[index]}'!`); + } +//args.forEach((value, index) => { +// if(value !== types[index](value)) throw new TypeError(`type of argument '${value}': '${typeof value}' does not match '${types[index]}'!`); +// }) + const res = fn(...args); + if(res !== types[types.length - 1](res)) + throw new TypeError(`type of result '${res}': '${typeof res}' does not match '${types[types.length-1]}'!`); + return res; +} module.exports = { contract }; From 6f20907de247b9c86ed055bd369a95f824a2ee64 Mon Sep 17 00:00:00 2001 From: Daniil Denisyuk Date: Mon, 16 Dec 2019 22:52:15 +0200 Subject: [PATCH 2/2] fixed --- Exercises/1-callback.js | 14 ++++---------- Exercises/2-closure.js | 2 +- Exercises/3-wrapper.js | 18 ++++++++++-------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/Exercises/1-callback.js b/Exercises/1-callback.js index 970249d..2108487 100644 --- a/Exercises/1-callback.js +++ b/Exercises/1-callback.js @@ -1,16 +1,10 @@ 'use strict'; const iterate = (object, callback) => { - for (let key in object){ - if(object.hasOwnProperty(key)) - callback(key, object[key]); + for (const key in object) { + if (Object.prototype.hasOwnProperty.call(object, key)) + callback(key, object[key], object); } -// for (let [key, value] of Object.entries(object) ) { -// callback(key, value); -// } -// Object.entries(object).forEach(([key, value])=>{ -// callback(key, value); -//}) -} +}; module.exports = { iterate }; diff --git a/Exercises/2-closure.js b/Exercises/2-closure.js index d44f4a5..7b71264 100644 --- a/Exercises/2-closure.js +++ b/Exercises/2-closure.js @@ -1,5 +1,5 @@ 'use strict'; -const store = value => () => value; +const store = x => () => x; module.exports = { store }; diff --git a/Exercises/3-wrapper.js b/Exercises/3-wrapper.js index ab9a02a..62b14a3 100644 --- a/Exercises/3-wrapper.js +++ b/Exercises/3-wrapper.js @@ -1,17 +1,19 @@ 'use strict'; const contract = (fn, ...types) => (...args) => { - for(let index in args){ + for (const index in args) { const arg = args[index]; - if(arg !== types[index](arg)) throw new TypeError(`type of argument '${value}': '${typeof value}' does not match '${types[index]}'!`); + if (arg !== types[index](arg)) throw new TypeError( + `type of argument '${arg}': '${typeof arg}' + does not match '${types[index]}'!`); } -//args.forEach((value, index) => { -// if(value !== types[index](value)) throw new TypeError(`type of argument '${value}': '${typeof value}' does not match '${types[index]}'!`); -// }) const res = fn(...args); - if(res !== types[types.length - 1](res)) - throw new TypeError(`type of result '${res}': '${typeof res}' does not match '${types[types.length-1]}'!`); + if (res !== types[types.length - 1](res)) + throw new TypeError( + `type of result '${res}': '${typeof res}' + does not match '${types[types.length - 1]}'!` + ); return res; -} +}; module.exports = { contract };