diff --git a/Exercises/1-callback.js b/Exercises/1-callback.js index 8270a12..ccde3b3 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) { + const value = object[key]; + callback(key, value, 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..fd1441b 100644 --- a/Exercises/3-wrapper.js +++ b/Exercises/3-wrapper.js @@ -1,5 +1,24 @@ 'use strict'; -const contract = (fn, ...types) => null; +const contract = (fn, ...types) => (...args) => { + args.forEach((arg, index) => { + const typeName = types[index].name.toLowerCase(); + const argType = typeof arg; + if (typeName !== argType) { + throw new TypeError(`Argument type expected: + ${typeName}, got: ${argType}`); + } + }); + const result = fn(...args); + const expectedResultType = types[types.length - 1].name.toLowerCase(); + const resultType = typeof result; + + if (expectedResultType !== resultType) { + throw new TypeError(`Result type expected: + ${expectedResultType}, got: ${resultType}`); + } + + return result; +}; module.exports = { contract };