diff --git a/Exercises/1-callback.js b/Exercises/1-callback.js index 8270a12..2108487 100644 --- a/Exercises/1-callback.js +++ b/Exercises/1-callback.js @@ -1,5 +1,10 @@ 'use strict'; -const iterate = (obj, callback) => null; +const iterate = (object, callback) => { + for (const key in object) { + if (Object.prototype.hasOwnProperty.call(object, key)) + callback(key, object[key], object); + } +}; 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..62b14a3 100644 --- a/Exercises/3-wrapper.js +++ b/Exercises/3-wrapper.js @@ -1,5 +1,19 @@ 'use strict'; -const contract = (fn, ...types) => null; +const contract = (fn, ...types) => (...args) => { + for (const index in args) { + const arg = args[index]; + if (arg !== types[index](arg)) throw new TypeError( + `type of argument '${arg}': '${typeof arg}' + 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 };