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..7626a25 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) => { + let i = 0; + for (const val of args) { + const Type = types[i++]; + const temp = new Type(); + const type = typeof temp.valueOf(); + if (typeof val !== type) { + throw new TypeError(`Неверный тип аргумента ${val}`); + } + } + 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 };