From e8938c2e0524d819eb68c0195542a9882d648325 Mon Sep 17 00:00:00 2001 From: Pauline Haren Date: Fri, 2 Oct 2020 20:28:22 -0500 Subject: [PATCH 1/6] finish task 1 --- index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.js b/index.js index c2cac5161..d724e6114 100644 --- a/index.js +++ b/index.js @@ -28,9 +28,14 @@ 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. * */ From 95ce541b7345649ea78b5a9e692a239a95832a5d Mon Sep 17 00:00:00 2001 From: Pauline Haren Date: Fri, 2 Oct 2020 20:32:32 -0500 Subject: [PATCH 2/6] finish task 2 --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d724e6114..9ff943cb7 100644 --- a/index.js +++ b/index.js @@ -61,11 +61,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(){ + let scoreInning = Math.floor(Math.random() * 3); + return scoreInning; + /* Task 3: finalScore() From 6d7bb335609e5fae30ced5897d2b107dd660e4ab Mon Sep 17 00:00:00 2001 From: Pauline Haren Date: Fri, 2 Oct 2020 20:55:25 -0500 Subject: [PATCH 3/6] finish task 3 --- index.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 9ff943cb7..c939055ac 100644 --- a/index.js +++ b/index.js @@ -65,6 +65,7 @@ Write a function called `inning` that generates a random number of points that a function inning(){ let scoreInning = Math.floor(Math.random() * 3); return scoreInning; + } /* Task 3: finalScore() @@ -81,12 +82,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: From 3cb1b6452673bf382bb86fb90a7b111724dca9c4 Mon Sep 17 00:00:00 2001 From: Pauline Haren Date: Fri, 2 Oct 2020 21:17:31 -0500 Subject: [PATCH 4/6] task 4 --- index.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c939055ac..d2e369d34 100644 --- a/index.js +++ b/index.js @@ -119,8 +119,28 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { - /* CODE HERE */ + +function inning(){ + let scoreInning = Math.floor(Math.random() * 3); + return scoreInning; +} + +function scoreboard(getInningScoreCB, inningCB, num) { +let finalScore = {away: 0, home:0}; +for (let i = 0; i < num; i++) { + finalScore.away += inningCB(); + finalScore.home += inningCB(); + + getInningScoreCB(i, finalScore); +} +return finalScore; } +function lastScore(inning, scoreboard) { + console.log(`Inning ${inning}: Home- ${scoreboard.home} Away- ${scoreboard.away} `) +} +scoreboard(lastScore, inning, 9); + + + From cec0911de407cb536f9ceb91fcc3fcef6d5d7bf9 Mon Sep 17 00:00:00 2001 From: Pauline Haren Date: Fri, 2 Oct 2020 21:50:21 -0500 Subject: [PATCH 5/6] correct task 4 --- index.js | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index d2e369d34..f1c7b656b 100644 --- a/index.js +++ b/index.js @@ -28,14 +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. + * 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. + * 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. * */ @@ -63,8 +65,7 @@ Write a function called `inning` that generates a random number of points that a function inning(){ - let scoreInning = Math.floor(Math.random() * 3); - return scoreInning; + return Math.floor(Math.random() * 3); } @@ -121,26 +122,24 @@ Final Score: awayTeam - homeTeam */ function inning(){ - let scoreInning = Math.floor(Math.random() * 3); - return scoreInning; + return Math.floor(Math.random() * 3); } function scoreboard(getInningScoreCB, inningCB, num) { -let finalScore = {away: 0, home:0}; -for (let i = 0; i < num; i++) { - finalScore.away += inningCB(); - finalScore.home += inningCB(); - - getInningScoreCB(i, finalScore); -} +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} `) +console.log(`Inning ${inning}: Home- ${scoreboard.home} Away- ${scoreboard.away}`) +if (inning === 9) +console.log (`Final Score: Away- ${scoreboard.awayFinal} Home- ${scoreboard.homeFinal}`); } -scoreboard(lastScore, inning, 9); - - - - +console.log (scoreboard(lastScore, inning, 9)); From f82631026b730bff295e28b2d96e5a8f6de10579 Mon Sep 17 00:00:00 2001 From: Pauline Haren Date: Fri, 2 Oct 2020 22:10:54 -0500 Subject: [PATCH 6/6] finished questions --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) 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