From a92fb03297f427e6ac00b9ecb66c3bbff1c51127 Mon Sep 17 00:00:00 2001 From: Loleatha Date: Sun, 4 Oct 2020 10:56:41 -0400 Subject: [PATCH 1/2] added answers to questions in read me and also added answers for JS and wrote functions --- README.md | 16 ++++++++++++++++ index.js | 52 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6d7f61f41..040fc3883 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,14 @@ The module challenge is the afternoon project or assignment that students work t ## Objectives - Explain function scope + +*Function Scope is when your variable is defined inside of your function only, and it can not be found anywhere else in your code. You can use other variables from outside the function but nothing on the outside can use whats inside. + - Describe what closure is, how closure is created in a program and why it is important to understand closures in JavaScript +* + + ## Introduction This challenge focuses on both scope and closures. @@ -42,6 +48,8 @@ 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 your nested funtions to go out and get variables in functions outside the nested function. + 2. Study the following code, then answer the questions below. ```js @@ -63,9 +71,17 @@ dansRoll(); ``` a. Where is closure used in this code? How can you tell? + +-The closure is inside of the function "personalDice", you can tell because it is using a function inside of another function. + b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change? + +-It is being ran the same each time it is called. The only difference between each time it is called would be that each call will be completely random and may never be the same out of 6 rolls. + c. What is the lexical scope of `newRoll`? +-"personalDice" + ### 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..8474b3a91 100644 --- a/index.js +++ b/index.js @@ -27,11 +27,18 @@ function processFirstItem(stringList, callback) { * Study the code for counter1 and counter2. Answer the questions below. * * 1. What is the difference between counter1 and counter2? + * + * -Counter 1 is nested inside of the function called "counterMaker", where count2 however is not which makes it a global variable. * * 2. Which of the two uses a closure? How can you tell? * + * -Count1 because it is nested inside of a function. + * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? - * + * + *-You should use count1 if you needed a variable that you want to use in the function which can create multiple values. + + -If you have multiple functions that need to access one global variable count2 is ideal. */ // counter1 code @@ -56,32 +63,42 @@ 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.round(Math.random() * 2); } -/* Task 3: finalScore() +console.log(inning()); +console.log(inning()); +console.log(inning()); +console.log(inning()); -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. +// 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. -For example, +// For example, -finalScore(inning, 9) might return: -{ - "Home": 11, - "Away": 5, -} +// finalScore(inning, 9) might return: +// { +// "Home": 11, +// "Away": 5, +// } -*/ - -function finalScore(/*code Here*/){ - - /*Code Here*/ +// */ +function finalScore(cb, num) { + let score ={ + home: 0, + away: 0, + }; + for(let i = 0; i < num; i++) { + score.home += cb(); + score.away += cb(); + } + return score; } +console.log(finalScore(inning, 9)); + /* Task 4: Create a function called `scoreboard` that accepts the following parameters: @@ -108,4 +125,5 @@ function scoreboard(/* CODE HERE */) { /* CODE HERE */ } +console.log() From 5ec761471f013d4eccfca41f57db6c1d00ab2066 Mon Sep 17 00:00:00 2001 From: Loleatha Date: Sun, 4 Oct 2020 22:22:20 -0400 Subject: [PATCH 2/2] fixed task 4 --- index.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8474b3a91..434c4580d 100644 --- a/index.js +++ b/index.js @@ -121,9 +121,21 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { - /* CODE HERE */ +function scoreboard(finalScore, inningCB, num) { + let home = 0; + let away = 0; + + for(let i = 1; i <= num; i++) { + home += finalScore(inningCB, i).home; + away += finalScore(inningCB, i).away; + + if(i == 1){console.log(`${i}st inning ${away} - ${home}`)} +else if(i == 2){console.log(`${i}st inning ${away} - ${home}`)} +else if(i == 3){console.log(`${i}st inning ${away} - ${home}`)} +else {console.log(`${i}th inning ${away} - ${home}`)} + } + return `Tonights final score is ${away} - ${home}` } - -console.log() + + console.log(scoreboard(finalScore, inning, 9));