diff --git a/README.md b/README.md index 6d7f61f41..7a3ad0b70 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,9 @@ Find the file `index.js` and complete the tasks until your tests are passing. Edit the `ReadMe` file with your answers. 1. In your own words, define closure (1-2 sentences). + +A closure is when you have a function which accesses variables or other functions outside of its initial scope. This allows a function to access other functions in javascript from the next level to the global stage. + 2. Study the following code, then answer the questions below. ```js @@ -63,9 +66,17 @@ dansRoll(); ``` a. Where is closure used in this code? How can you tell? + +The closure used in this code is in the console.log statement where they assessed name from the parameter of the outer function personalDice. You can tell becaue name is not initially in the function scope and must be reached outside of the function scope where the console.log statement lies. + b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change? + +The name will always be the same. The number could change. + c. What is the lexical scope of `newRoll`? +The lexical scope of newRoll is personalDice(name). + ### Task 3 - Stretch Goals After you have completed the requirements, **create** a new file called `stretch.js` and practice more with closures. There are no tests for these problems. diff --git a/index.js b/index.js index c2cac5161..00664fc2f 100644 --- a/index.js +++ b/index.js @@ -14,9 +14,9 @@ * Invoking `processFirstItem` passing `['foo', 'bar']` and `(str) => str + str`, * should return 'foofoo'. */ -function processFirstItem(stringList, callback) { - return callback(stringList[0]) -} +// function processFirstItem(stringList, callback) { +// return callback(stringList[0]) +// } // ⭐️ Example Challenge END ⭐️ @@ -28,9 +28,17 @@ function processFirstItem(stringList, callback) { * * 1. What is the difference between counter1 and counter2? * + * the difference is in counter1 the count variable is nested inside of the counterMaker function and in counter2 the count variable is declared outside of the function making it a global variable. + * * 2. Which of the two uses a closure? How can you tell? * + * Counter1 uses a closure because it has a function nested inside another function. The inner function counter uses count++, which is a variable in the outer function. It is a closure because it references a variable in its outer state (count). + * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? + * + * counter1 would a better scenario if you wanted to copy that specific fucntion counterMaker and assign it to a new variable such as counter1. This counter can then be used for multiple purposes while holding the count variable. + * + * Counter 2 is a more general global count and is accumulative. Since it is in the global scope, it will add 1 to its count variable every time you run the counter2 function. If you want to increase your count and ensure that it does not reset to 0, use counter 2. * */ @@ -56,12 +64,12 @@ 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*/){ - - /*Code Here*/ - +function inning() { + 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. @@ -76,12 +84,19 @@ finalScore(inning, 9) might return: */ -function finalScore(/*code Here*/){ - - /*Code Here*/ +function finalScore(inning, numOfInnings){ + let team1 = 0 + let team2 = 0 + for (let i = 0; i < numOfInnings; i++) { + team1 = team1 + inning(); + team2 = team2 + inning(); + } + return {home: team1, away: team2} } +// console.log(finalScore(inning, 9)) + /* Task 4: Create a function called `scoreboard` that accepts the following parameters: @@ -104,8 +119,23 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { - /* CODE HERE */ +function getInningScore(homeTeam, awayTeam, numberOfInnings) { + return `Final Score: Home Team ${homeTeam} - Away Team ${awayTeam} (${numberOfInnings} innings played)` } +function scoreboard(getInningScore, inning, numberOfInnings) { + let awayTeam = 0 + let homeTeam = 0 + let scoreboardResult = {} + for (let i = 1; i <= numberOfInnings; i++) { + awayTeam = awayTeam + inning(); + homeTeam = homeTeam + inning(); + scoreboardResult[`Inning ${[i]}`] = `Home Team: ${homeTeam} - Away Team: ${awayTeam}` + } + let finalScoreboard = {result: scoreboardResult, final: getInningScore(homeTeam, awayTeam, numberOfInnings)} + return finalScoreboard +} + +console.log(scoreboard(getInningScore, inning, 9)) +