From 48311cbf1d47fa46cb345425d374642320cc5ed7 Mon Sep 17 00:00:00 2001 From: ser Date: Mon, 16 Dec 2019 21:36:23 +0200 Subject: [PATCH 1/3] done 1-3 --- Exercises/1-callback.js | 8 +++++++- Exercises/2-closure.js | 2 +- Exercises/3-wrapper.js | 20 +++++++++++++++++++- 3 files changed, 27 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..3afa44c 100644 --- a/Exercises/3-wrapper.js +++ b/Exercises/3-wrapper.js @@ -1,5 +1,23 @@ 'use strict'; -const contract = (fn, ...types) => null; +const contract = (fn, ...types) => (...args) => { + for (const i in args) { + const Type = types[i]; + const temp = new Type(); + const type = typeof temp.valueOf(); + const arg = args[i]; + if (typeof arg !== type) { + throw new TypeError(`Неверный тип аргумента ${arg}`); + } + } + const res = fn(...args); + const TypeRes = types[types.length - 1]; + const tempRes = new TypeRes(); + const typeRes = typeof tempRes.valueOf(); + if (typeof res !== typeRes) { + throw new TypeError(`Неверный тип результата ${res}`); + } + return res; +}; module.exports = { contract }; From bb878c5349b1584214548ab281a917976a33549e Mon Sep 17 00:00:00 2001 From: ser Date: Tue, 17 Dec 2019 00:11:36 +0200 Subject: [PATCH 2/3] Using for of --- Exercises/3-wrapper.js | 10 +++++----- Exercises/3-wrapper.js.back | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 Exercises/3-wrapper.js.back diff --git a/Exercises/3-wrapper.js b/Exercises/3-wrapper.js index 3afa44c..7626a25 100644 --- a/Exercises/3-wrapper.js +++ b/Exercises/3-wrapper.js @@ -1,13 +1,13 @@ 'use strict'; const contract = (fn, ...types) => (...args) => { - for (const i in args) { - const Type = types[i]; + let i = 0; + for (const val of args) { + const Type = types[i++]; const temp = new Type(); const type = typeof temp.valueOf(); - const arg = args[i]; - if (typeof arg !== type) { - throw new TypeError(`Неверный тип аргумента ${arg}`); + if (typeof val !== type) { + throw new TypeError(`Неверный тип аргумента ${val}`); } } const res = fn(...args); diff --git a/Exercises/3-wrapper.js.back b/Exercises/3-wrapper.js.back new file mode 100644 index 0000000..3afa44c --- /dev/null +++ b/Exercises/3-wrapper.js.back @@ -0,0 +1,23 @@ +'use strict'; + +const contract = (fn, ...types) => (...args) => { + for (const i in args) { + const Type = types[i]; + const temp = new Type(); + const type = typeof temp.valueOf(); + const arg = args[i]; + if (typeof arg !== type) { + throw new TypeError(`Неверный тип аргумента ${arg}`); + } + } + const res = fn(...args); + const TypeRes = types[types.length - 1]; + const tempRes = new TypeRes(); + const typeRes = typeof tempRes.valueOf(); + if (typeof res !== typeRes) { + throw new TypeError(`Неверный тип результата ${res}`); + } + return res; +}; + +module.exports = { contract }; From 398e4170d15c4a10e483466122de9425f88022a2 Mon Sep 17 00:00:00 2001 From: ser Date: Tue, 17 Dec 2019 00:24:05 +0200 Subject: [PATCH 3/3] deleted a temp file --- Exercises/3-wrapper.js.back | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 Exercises/3-wrapper.js.back diff --git a/Exercises/3-wrapper.js.back b/Exercises/3-wrapper.js.back deleted file mode 100644 index 3afa44c..0000000 --- a/Exercises/3-wrapper.js.back +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -const contract = (fn, ...types) => (...args) => { - for (const i in args) { - const Type = types[i]; - const temp = new Type(); - const type = typeof temp.valueOf(); - const arg = args[i]; - if (typeof arg !== type) { - throw new TypeError(`Неверный тип аргумента ${arg}`); - } - } - const res = fn(...args); - const TypeRes = types[types.length - 1]; - const tempRes = new TypeRes(); - const typeRes = typeof tempRes.valueOf(); - if (typeof res !== typeRes) { - throw new TypeError(`Неверный тип результата ${res}`); - } - return res; -}; - -module.exports = { contract };