From ecc7046cd8b56df6328e2c0a2f072641b38bfa8b Mon Sep 17 00:00:00 2001 From: Alden Ho Date: Mon, 14 Sep 2020 13:19:31 -0700 Subject: [PATCH 1/5] completed task 1-3 --- index.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index c2cac5161..b8079ec32 100644 --- a/index.js +++ b/index.js @@ -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,14 @@ 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(team1, team2){ + team1 = Math.floor(Math.random()*3) + team2 = Math.floor(Math.random()*3) + return `team1: ${team1}, team2: ${team2}` } +// 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 +86,12 @@ finalScore(inning, 9) might return: */ -function finalScore(/*code Here*/){ - - /*Code Here*/ - +function finalScore(inning, numberOfInnings){ + return inning * numberOfInnings } +console.log(finalScore(inning(), 9)) + /* Task 4: Create a function called `scoreboard` that accepts the following parameters: @@ -104,7 +114,7 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { +function scoreboard(getInningScore, inning, numberOfInnings) { /* CODE HERE */ } From c89b118ba3e8af6602660d9826f598af1e499277 Mon Sep 17 00:00:00 2001 From: Alden Ho Date: Mon, 14 Sep 2020 14:24:33 -0700 Subject: [PATCH 2/5] making progress --- index.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index b8079ec32..099d2c50f 100644 --- a/index.js +++ b/index.js @@ -64,10 +64,8 @@ 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(team1, team2){ - team1 = Math.floor(Math.random()*3) - team2 = Math.floor(Math.random()*3) - return `team1: ${team1}, team2: ${team2}` +function inning(){ + return Math.floor(Math.random()*3) } // console.log(inning()) @@ -86,8 +84,18 @@ finalScore(inning, 9) might return: */ -function finalScore(inning, numberOfInnings){ - return inning * numberOfInnings +function finalScore(inning, numOfInnings){ + let team1 = 0 + let team2 = 0 + for (let i = 0; i < numOfInnings; i++) { + team1 = team1 + inning; + } + for (let i = 1; i <= numOfInnings; i++) { + team2 = team2 + inning; + } + let final = {home: team1, + away: team2} + return final } console.log(finalScore(inning(), 9)) @@ -114,8 +122,16 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ +function getInningScore(inning, numberOfInnings) { + return inning * numberOfInnings +} + +console.log(getInningScore(inning(), 5)) + function scoreboard(getInningScore, inning, numberOfInnings) { - /* CODE HERE */ + let awayTeam = 0 + let homeTeam = 0 + let scoreboardResults = {} } From ca4a0c5c4a97af79bcac652bd93db2fbc71ab688 Mon Sep 17 00:00:00 2001 From: Alden Ho Date: Mon, 14 Sep 2020 14:59:51 -0700 Subject: [PATCH 3/5] completed task 3 --- index.js | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 099d2c50f..dde295269 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 ⭐️ @@ -64,7 +64,7 @@ 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(){ +function inning() { return Math.floor(Math.random()*3) } @@ -87,18 +87,16 @@ finalScore(inning, 9) might return: function finalScore(inning, numOfInnings){ let team1 = 0 let team2 = 0 + for (let i = 0; i < numOfInnings; i++) { - team1 = team1 + inning; + team1 = team1 + inning(); + team2 = team2 + inning(); } - for (let i = 1; i <= numOfInnings; i++) { - team2 = team2 + inning; - } - let final = {home: team1, - away: team2} - return final + return {home: team1, away: team2} + } -console.log(finalScore(inning(), 9)) +console.log(finalScore(inning, 9)) /* Task 4: @@ -122,16 +120,19 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function getInningScore(inning, numberOfInnings) { - return inning * numberOfInnings -} +// function getInningScore(homeTeam, awayTeam) { +// return `Inning ${numberOfInnings}: homeTeam ${homeTeam} - awayTeam ${awayTeam}` +// } -console.log(getInningScore(inning(), 5)) +// console.log(getInningScore(inning(), 5)) -function scoreboard(getInningScore, inning, numberOfInnings) { - let awayTeam = 0 - let homeTeam = 0 - let scoreboardResults = {} -} +// function scoreboard(getInningScore, inning, numberOfInnings) { +// let awayTeam = inning * numberOfInnings +// let homeTeam = inning * numberOfInnings +// return getInningScore(homeTeam, awayTeam) + +// } + +// console.log(scoreboard()) From cfb647b6153209e161b0150f8c2f7c21ae3a2c7c Mon Sep 17 00:00:00 2001 From: Alden Ho Date: Mon, 14 Sep 2020 15:24:41 -0700 Subject: [PATCH 4/5] finished task 4 --- index.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index dde295269..71a1d0e95 100644 --- a/index.js +++ b/index.js @@ -93,10 +93,9 @@ function finalScore(inning, numOfInnings){ team2 = team2 + inning(); } return {home: team1, away: team2} - } -console.log(finalScore(inning, 9)) +// console.log(finalScore(inning, 9)) /* Task 4: @@ -120,19 +119,23 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -// function getInningScore(homeTeam, awayTeam) { -// return `Inning ${numberOfInnings}: homeTeam ${homeTeam} - awayTeam ${awayTeam}` -// } - -// console.log(getInningScore(inning(), 5)) +function getInningScore(homeTeam, awayTeam, numberOfInnings) { + return `Final Score: Home Team ${homeTeam} - Away Team ${awayTeam} (${numberOfInnings} innings played)` +} -// function scoreboard(getInningScore, inning, numberOfInnings) { -// let awayTeam = inning * numberOfInnings -// let homeTeam = inning * numberOfInnings -// return getInningScore(homeTeam, awayTeam) - -// } +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}` + } + console.log(scoreboardResult) + return getInningScore(homeTeam, awayTeam, numberOfInnings) +} -// console.log(scoreboard()) +console.log(scoreboard(getInningScore, inning, 9)) From 92b1b8a1fbee029b66508970d8c4445761f2d652 Mon Sep 17 00:00:00 2001 From: Alden Ho Date: Mon, 14 Sep 2020 16:10:47 -0700 Subject: [PATCH 5/5] completed readme section --- README.md | 11 +++++++++++ index.js | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) 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 71a1d0e95..00664fc2f 100644 --- a/index.js +++ b/index.js @@ -132,8 +132,8 @@ function scoreboard(getInningScore, inning, numberOfInnings) { homeTeam = homeTeam + inning(); scoreboardResult[`Inning ${[i]}`] = `Home Team: ${homeTeam} - Away Team: ${awayTeam}` } - console.log(scoreboardResult) - return getInningScore(homeTeam, awayTeam, numberOfInnings) + let finalScoreboard = {result: scoreboardResult, final: getInningScore(homeTeam, awayTeam, numberOfInnings)} + return finalScoreboard } console.log(scoreboard(getInningScore, inning, 9))