From 7cedd1e9b5905ab24980c1a1a8dc9180295261a2 Mon Sep 17 00:00:00 2001 From: Blessing Hirwa <50826640+hirwablessing@users.noreply.github.com> Date: Fri, 23 Apr 2021 11:54:05 +0200 Subject: [PATCH] New: Add answer for exercise 9 --- closures.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/closures.js b/closures.js index 2329c00..1ed62f3 100644 --- a/closures.js +++ b/closures.js @@ -128,3 +128,37 @@ function rollCall(names) { // rollCaller() // -> Should log 'Ruth' // rollCaller() // -> Should log 'Everyone accounted for' +function saveOutput(func, magicWord) { + + let result = 0; + let output = {} + + function wordChecker(input){ + + output = {...output, [input]: result} + + if (input === magicWord){ + + return output + + } + + else { + + result = func(input) + + return result; + } + + } + + return wordChecker + } + +// /*** Uncomment these to check your work! ***/ +// const multiplyBy2 = function(num) { return num * 2; }; +// const multBy2AndLog = saveOutput(multiplyBy2, 'boo'); +// console.log(multBy2AndLog(2)); // => should log 4 +// console.log(multBy2AndLog(9)); // => should log 18 +// console.log(multBy2AndLog('boo')); // => should log { 2: 4, 9: 18 } +