From 8033a3c95a82c767d5b733f8fbcc664ed30f5fc2 Mon Sep 17 00:00:00 2001 From: ErikTrethewey-J Date: Mon, 14 Sep 2020 16:28:30 -0700 Subject: [PATCH 1/2] Still need a lot of work to do. --- index.js | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index c2cac5161..fda252e52 100644 --- a/index.js +++ b/index.js @@ -2,44 +2,48 @@ /** * ### Challenge `processFirstItem` - * + * * @instructions * Implement a higher-order function called `processFirstItem`. * It takes two arguments: * @param stringList an array of strings. * @param callback function that takes a string as its argument. * @returns the result of invoking `callback` with the FIRST element in `stringList`. - * + * * Example of usage of this higher-order function: * Invoking `processFirstItem` passing `['foo', 'bar']` and `(str) => str + str`, * should return 'foofoo'. -*/ + */ +const stringList = `['foo', 'bar']`; function processFirstItem(stringList, callback) { - return callback(stringList[0]) -} + function callback(str) { + console.log(`(${str}) => ${str} + ${str}`); + return callback(stringList[0]); + } +} +processFirstItem(); // ⭐️ Example Challenge END ⭐️ - ///// M V P /////// /* Task 1: `counterMaker` * Study the code for counter1 and counter2. Answer the questions below. - * + * * 1. What is the difference between counter1 and counter2? - * + * * 2. Which of the two uses a closure? How can you tell? - * - * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? * -*/ + * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? + * + */ // counter1 code function counterMaker() { let count = 0; return function counter() { count++; - } + }; } const counter1 = counterMaker(); @@ -51,15 +55,12 @@ function counter2() { return count++; } - /* Task 2: inning() Write a function called `inning` that generates a random number of points that a team scored in an inning. This should be a whole number between 0 and 2. */ -function inning(/*Code Here*/){ - - /*Code Here*/ - +function inning(/*Code Here*/) { + /*Code Here*/ } /* Task 3: finalScore() @@ -74,12 +75,10 @@ finalScore(inning, 9) might return: "Away": 5, } -*/ - -function finalScore(/*code Here*/){ +*/ +function finalScore(/*code Here*/) { /*Code Here*/ - } /* Task 4: @@ -107,5 +106,3 @@ Final Score: awayTeam - homeTeam */ function scoreboard(/* CODE HERE */) { /* CODE HERE */ } - - From f20abd0b668342b5255faf05602820862aec8af5 Mon Sep 17 00:00:00 2001 From: ErikTrethewey-J Date: Mon, 14 Sep 2020 18:58:29 -0700 Subject: [PATCH 2/2] Final product for project. --- index.js | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index fda252e52..b03ccf03d 100644 --- a/index.js +++ b/index.js @@ -14,15 +14,11 @@ * Invoking `processFirstItem` passing `['foo', 'bar']` and `(str) => str + str`, * should return 'foofoo'. */ -const stringList = `['foo', 'bar']`; -function processFirstItem(stringList, callback) { - function callback(str) { - console.log(`(${str}) => ${str} + ${str}`); - return callback(stringList[0]); - } -} -processFirstItem(); +// function processFirstItem(stringList, callback) { +// return callback(stringList[0]); +// } +// processFirstItem(); // ⭐️ Example Challenge END ⭐️ ///// M V P /////// @@ -31,11 +27,11 @@ processFirstItem(); * Study the code for counter1 and counter2. Answer the questions below. * * 1. What is the difference between counter1 and counter2? - * + * The scope of the let variable is within the counterMaker function in one and counter twos counter is outside the scope. * 2. Which of the two uses a closure? How can you tell? - * + * function counterMaker because the variable is within the scope of the function and calls it. * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? - * + * Number one is preferable because you have a closed loop. */ // counter1 code @@ -59,10 +55,11 @@ function counter2() { Write a function called `inning` that generates a random number of points that a team scored in an inning. This should be a whole number between 0 and 2. */ -function inning(/*Code Here*/) { +function inning() { /*Code Here*/ + return Math.floor(Math.random() * 3); } - +console.log(inning()); /* Task 3: finalScore() Write a higher order function called `finalScore` that accepts the callback function `inning` (from above) and a number of innings and and returns the final score of the game in the form of an object. @@ -77,9 +74,17 @@ finalScore(inning, 9) might return: */ -function finalScore(/*code Here*/) { - /*Code Here*/ +function finalScore(callback, numberOfInnings) { + let home = 0; + let away = 0; + for (let i = 0; i < numberOfInnings; i++) { + home = home + callback(); + away = away + callback(); + } + let final = { Home: home, Away: away }; + return final; } +console.log(finalScore(inning, 9)); /* Task 4: @@ -103,6 +108,17 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { - /* CODE HERE */ +function scoreboard(getInningScoreCB, InningsCB, numberOfInnings) { + let home = 0; + let away = 0; + + for (let i = 0; i < numberOfInnings; i++) { + let inning = getInningScoreCB(InningsCB, 1); + + home = home + inning.Home; + away = away + inning.Away; + + console.log(`${i + 1}th + Inning: ${away} - ${home}`); + } } +scoreboard(finalScore, inning, 9);