diff --git a/README.md b/README.md index 6d7f61f41..dc4b7110a 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). + + Closure allows a function to reference variables outside of its scope. One way to look at it is the function has a part of the story, but without closure, it doesn't have the full story. A function works better when it uses the environment around it. + 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? + + Closure is used within the personalDice function when using the variable const = newRoll. + 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, but the roll will change between 0 and 6. + c. What is the lexical scope of `newRoll`? + newRoll is nested within the personalDice function. + ### 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..91e4ece77 100644 --- a/index.js +++ b/index.js @@ -27,11 +27,11 @@ function processFirstItem(stringList, callback) { * Study the code for counter1 and counter2. Answer the questions below. * * 1. What is the difference between counter1 and counter2? - * + * Counter1 is creating the needed variable within the function, where counter2 grabs from a pre-existing variable for closure. * 2. Which of the two uses a closure? How can you tell? - * + * Both of them. counter1 closes it self and counter2 reaches out for closure. * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? - * + * counter1 would be best if there was a variable specific to that function, whereas counter2 would be best when needed in multiple functions. */ // counter1 code @@ -56,9 +56,9 @@ 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.round(Math.random() * 5); } @@ -76,12 +76,22 @@ finalScore(inning, 9) might return: */ -function finalScore(/*code Here*/){ - /*Code Here*/ +function finalScore(cb, innings){ + let home = 0; + let away = 0; + for (let i = 1; i < innings; i++) { + let teams = cb(); + home = home + teams[0]; + away = away + teams[1]; + } + return `Final Score - Away Team: ${away} - Home Team: ${home}`; } +console.log(finalScore(inning, 9)); + + /* Task 4: Create a function called `scoreboard` that accepts the following parameters: @@ -104,8 +114,22 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { - /* CODE HERE */ +function scoreboard(getInningScore, inning, num) { + const final = { + home: 0, away: 0 + } + for (let i = 1; i <= num; i++) { + final.home += inning(); + final.away += inning(); + + getInningScore(i, final) + } + return final; +} + +function newScoreBoard (inning, scoreboard) { + console.log(`Inning ${inning}: Home: ${scoreboard.home} Away: ${scoreboard.away}`) } +console.log(scoreboard(newScoreBoard, inning, 10000))