From 0e5280ad3be7171a3381ac5d4ffa8d82d27854f3 Mon Sep 17 00:00:00 2001 From: "Grevcev.AS" Date: Mon, 1 Nov 2021 07:41:30 +1000 Subject: [PATCH] lab HigherOrderFunction --- Exercises/1-callback.js | 7 ++++++- Exercises/2-closure.js | 2 +- Exercises/3-wrapper.js | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) 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 };