diff --git a/README.md b/README.md index 6d7f61f41..979552105 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). + +Whenever you create a function within another function, this is a closure. + 2. Study the following code, then answer the questions below. ```js @@ -63,8 +66,12 @@ dansRoll(); ``` a. Where is closure used in this code? How can you tell? + return function(){ on line 53 because it will create a function within the function. b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change? +The phrase "Dan rolled a" stays the same. The number he rolls generally changes. + c. What is the lexical scope of `newRoll`? + The lexical scope of newRoll is it can reach out to the outerfunction and even globally, but newRoll cannot be accessed outside of its function. ### Task 3 - Stretch Goals diff --git a/index.js b/index.js index c2cac5161..f1c7b656b 100644 --- a/index.js +++ b/index.js @@ -28,9 +28,16 @@ function processFirstItem(stringList, callback) { * * 1. What is the difference between counter1 and counter2? * + * counter2 will reset every time it is called. Also the variable in counter2 is global. counter1 has count as a private variable and will keep the count + * everytime it is called. * 2. Which of the two uses a closure? How can you tell? * + * counter one due to the function within the function. + * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? + * + * counter1 is generally preferable because it is less memory due to being private. It also retains memeory. However, if you need the count to be reset every time, + * counter2 would be better. * */ @@ -56,11 +63,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*/){ - /*Code Here*/ - -} + function inning(){ + return Math.floor(Math.random() * 3); + } + /* Task 3: finalScore() @@ -76,12 +83,21 @@ finalScore(inning, 9) might return: */ -function finalScore(/*code Here*/){ - - /*Code Here*/ - +function finalScore(cb, num) { + let home = 0; + let away = 0; + for (let i = 1; i <= num; i++) { + home += cb(); + away += cb(); + } + return { + "Away": away, + "Home": home, + } } +finalScore(inning, 9); + /* Task 4: Create a function called `scoreboard` that accepts the following parameters: @@ -104,8 +120,26 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { - /* CODE HERE */ + +function inning(){ + return Math.floor(Math.random() * 3); } +function scoreboard(getInningScoreCB, inningCB, num) { +let finalScore = {away: 0, home:0, awayFinal: 0, homeFinal: 0}; +for (let i = 0; i <= num; i++) { +finalScore.away = inningCB(); +finalScore.home = inningCB(); +finalScore.awayFinal += finalScore.away; +finalScore.homeFinal += finalScore.home; +getInningScoreCB(i, finalScore); +} +return finalScore; +} +function lastScore(inning, scoreboard) { +console.log(`Inning ${inning}: Home- ${scoreboard.home} Away- ${scoreboard.away}`) +if (inning === 9) +console.log (`Final Score: Away- ${scoreboard.awayFinal} Home- ${scoreboard.homeFinal}`); +} +console.log (scoreboard(lastScore, inning, 9));