diff --git a/Exercises/1-callback.js b/Exercises/1-callback.js index 8270a12..69c423c 100644 --- a/Exercises/1-callback.js +++ b/Exercises/1-callback.js @@ -1,5 +1,15 @@ 'use strict'; -const iterate = (obj, callback) => null; +const iterate = (obj, callback) => { + if (!obj) { + callback(new Error('obj needed')); + return; + } + for (const key of Object.keys(obj)) { + callback(key, obj[key], obj); + } + return; +}; module.exports = { iterate }; + diff --git a/Exercises/2-closure.js b/Exercises/2-closure.js index 0f07103..db0c011 100644 --- a/Exercises/2-closure.js +++ b/Exercises/2-closure.js @@ -1,5 +1,6 @@ '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..789970e 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) => (...arr) => { + for (let i = 0; i < types.length - 1; i++) { + const argType = typeof arr[i]; + const neededArgType = types[i].name.toLowerCase(); + if (argType !== neededArgType) { + throw new TypeError('Types are different'); + } + } + const neededReType = types[types.length - 1].name.toLowerCase(); + const result = fn(...arr); + if (typeof result !== neededReType) { + throw new TypeError('Types are different'); + } + return result; +}; module.exports = { contract };