From a16099d0d94d1bbc2ecd8212e15f42914174b9cf Mon Sep 17 00:00:00 2001 From: Samantha Date: Wed, 30 Sep 2020 00:25:43 -0400 Subject: [PATCH 1/5] initial commit --- README.md | 10 ++++----- Written Q&A.txt | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 50 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 Written Q&A.txt diff --git a/README.md b/README.md index 6d7f61f41..2855b6c14 100644 --- a/README.md +++ b/README.md @@ -27,11 +27,11 @@ There are layers upon layers of nested functions within the game of baseball. Yo ### Task 1 - Set Up Project and Tests -1. Fork repo and add TL as collaborator on Github -2. Clone _your_ fork (not Lambda's repo by mistake!) -3. `cd` into your newly cloned repository -4. Create a new branch by typing `git checkout -b ` -5. Work on your branch, push commits and create PR as usual +[x] 1. Fork repo and add TL as collaborator on Github +[x] 2. Clone _your_ fork (not Lambda's repo by mistake!) +[x] 3. `cd` into your newly cloned repository +[x] 4. Create a new branch by typing `git checkout -b ` +[x] 5. Work on your branch, push commits and create PR as usual ### Task 2a - MVP code diff --git a/Written Q&A.txt b/Written Q&A.txt new file mode 100644 index 000000000..9586150fd --- /dev/null +++ b/Written Q&A.txt @@ -0,0 +1,57 @@ +### Task 2b - Written questions + +Edit the `ReadMe` file with your answers. + +>> 1. In your own words, define closure (1-2 sentences). + +A closure is something that gives you access to an outside functions' +scope from inside an inner function. + +------------------------------------------------------------------- +------------------------------------------------------------------- +------------------------------------------------------------------- +------------------------------------------------------------------- + + + + +>> 2. Study the following code, then answer the questions below. + + + + + +```js +function personalDice(name){ + return function(){ + // generate random number between 1 and 6 + const newRoll = Math.floor(Math.random() * 6); + console.log(`${name} rolled a ${newRoll}`) + } +} + +const dansRoll = personalDice("Dan"); + +const zoesRoll = personalDice("Zoe"); + + +dansRoll(); +dansRoll(); +``` + +>> a. Where is closure used in this code? How can you tell? + + + +------------------------------------------------------------------- + + + +>> b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change? + + + +------------------------------------------------------------------- + + +>> c. What is the lexical scope of `newRoll`? diff --git a/index.js b/index.js index c2cac5161..e50055e80 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ * Invoking `processFirstItem` passing `['foo', 'bar']` and `(str) => str + str`, * should return 'foofoo'. */ + function processFirstItem(stringList, callback) { return callback(stringList[0]) } @@ -21,20 +22,46 @@ function processFirstItem(stringList, callback) { // ⭐️ Example Challenge END ⭐️ -///// M V P /////// + + + + +//----------------------------------START HERE---------------------------------// +//-------------------------------------MVP-------------------------------------// + /* Task 1: `counterMaker` + * * Study the code for counter1 and counter2. Answer the questions below. + * + * //-----------------------------------------------------------------------------// + * * 1. What is the difference between counter1 and counter2? + * + * The difference between counter1 and counter2: + * ----------------------------------------------- +//---// counter1 is requesting a new function that will return the count++ variable 1 # or more. +//---// counter2 is requesting for a return of the count++ variable 1 # higher as well, but with in inner scope of the original function. + * - * 2. Which of the two uses a closure? How can you tell? + * + * * + * 2. Which of the two uses a closure? How can you tell? + * + * + * + * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? * + * + * + * */ // counter1 code + function counterMaker() { let count = 0; return function counter() { @@ -44,13 +71,21 @@ function counterMaker() { const counter1 = counterMaker(); + + + // counter2 code + let count = 0; function counter2() { return count++; } +//-----------------------------------------------------------------------------// + + + /* Task 2: inning() @@ -62,6 +97,12 @@ function inning(/*Code Here*/){ } + + +//-----------------------------------------------------------------------------// + + + /* 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. @@ -82,6 +123,11 @@ function finalScore(/*code Here*/){ } + +//-----------------------------------------------------------------------------// + + + /* Task 4: Create a function called `scoreboard` that accepts the following parameters: From 99fd10086bf7b477654e86c26124b99c868a9247 Mon Sep 17 00:00:00 2001 From: Samantha Date: Wed, 30 Sep 2020 01:04:53 -0400 Subject: [PATCH 2/5] commit until tomorrow --- README.md | 22 +--------------------- Written Q&A.txt | 16 +++++----------- index.js | 6 +++--- 3 files changed, 9 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 2855b6c14..98ea095d4 100644 --- a/README.md +++ b/README.md @@ -41,30 +41,10 @@ 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). -2. Study the following code, then answer the questions below. -```js -function personalDice(name){ - return function(){ - // generate random number between 1 and 6 - const newRoll = Math.floor(Math.random() * 6); - console.log(`${name} rolled a ${newRoll}`) - } -} - -const dansRoll = personalDice("Dan"); - -const zoesRoll = personalDice("Zoe"); +>> >> >> PLEASE SEE 'Written Q&A.txt' for Written question answers. -dansRoll(); -dansRoll(); -``` - -a. Where is closure used in this code? How can you tell? -b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change? -c. What is the lexical scope of `newRoll`? ### Task 3 - Stretch Goals diff --git a/Written Q&A.txt b/Written Q&A.txt index 9586150fd..ad68307c2 100644 --- a/Written Q&A.txt +++ b/Written Q&A.txt @@ -8,19 +8,9 @@ A closure is something that gives you access to an outside functions' scope from inside an inner function. ------------------------------------------------------------------- -------------------------------------------------------------------- -------------------------------------------------------------------- -------------------------------------------------------------------- - - - >> 2. Study the following code, then answer the questions below. - - - - ```js function personalDice(name){ return function(){ @@ -41,13 +31,17 @@ dansRoll(); >> a. Where is closure used in this code? How can you tell? +-You can find closure in the function one (Line 15). You can tell because +-the next line has a return function, and this closes the function. -------------------------------------------------------------------- +------------------------------------------------------------------- >> b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change? +-Counter one would be ideal if there were multiple varibles but only one is intended to print on the console. +-Counter two would be ideal if it were used to count random objects. diff --git a/index.js b/index.js index e50055e80..d4d8b9e5b 100644 --- a/index.js +++ b/index.js @@ -49,13 +49,13 @@ function processFirstItem(stringList, callback) { * * * 2. Which of the two uses a closure? How can you tell? - * +//---// counter1 uses a closure because there is a function nested within. * * * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? - * - * +//---//counter1 would be ideal if you have only one function that can create several outcomes or values. +//---//counter2 would be ideal if you have many functions that need to access the global scope. * * */ From 8861b4e2ef525010889989f4098c5c36137a46ef Mon Sep 17 00:00:00 2001 From: Samantha Date: Wed, 30 Sep 2020 22:15:57 -0400 Subject: [PATCH 3/5] js task 1-3 done --- README.md | 6 +++--- index.js | 38 +++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 98ea095d4..1afdd07c7 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ A scoreboard in a major league stadium looks something like this. In fact, the s ![Fenway Scoreboard](https://storage.googleapis.com/afs-prod/media/media:e959506330fd4e5890023c93cfbaac55/800.jpeg) There are layers upon layers of nested functions within the game of baseball. Your challenge today will be to work through tasks associated with these layers, and ultimately to produce a scoreboard that logs in the console. - + ## Instructions ### Task 1 - Set Up Project and Tests @@ -35,7 +35,7 @@ There are layers upon layers of nested functions within the game of baseball. Yo ### Task 2a - MVP code -Find the file `index.js` and complete the tasks until your tests are passing. +Find the file `index.js` and complete the tasks until your tests are passing.sd ### Task 2b - Written questions @@ -76,7 +76,7 @@ addSix(21); // returns 27 ## Resources -📚 [Scope and Closures Guide](https://css-tricks.com/javascript-scope-closures/) +📚 [Scope and Closures Guide](https://css-tricks.com/javascript-scope-closures/) 🧠 ["I never Understood Closures" Blog](https://medium.com/dailyjs/i-never-understood-javascript-closures-9663703368e8) diff --git a/index.js b/index.js index d4d8b9e5b..55eff516a 100644 --- a/index.js +++ b/index.js @@ -41,19 +41,21 @@ function processFirstItem(stringList, callback) { * * The difference between counter1 and counter2: * ----------------------------------------------- -//---// counter1 is requesting a new function that will return the count++ variable 1 # or more. -//---// counter2 is requesting for a return of the count++ variable 1 # higher as well, but with in inner scope of the original function. +//---// counter1 is declaring the variable within the function curley braces (block scope). +//---// counter2 is declaring the variable outside the function curley braces (inner scope). * * * * * 2. Which of the two uses a closure? How can you tell? -//---// counter1 uses a closure because there is a function nested within. + * ------------------------------------------------- +//---// counter1 uses a closure because there is a function declaration nested within. * * * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? + * ------------------------------------------------------------------------------------------------- //---//counter1 would be ideal if you have only one function that can create several outcomes or values. //---//counter2 would be ideal if you have many functions that need to access the global scope. * @@ -87,16 +89,18 @@ function counter2() { -/* Task 2: inning() +// Task 2: inning() -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. */ +// 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.floor(Math.random() * 3); } +console.log(inning()); + //-----------------------------------------------------------------------------// @@ -105,7 +109,8 @@ function inning(/*Code Here*/){ /* 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. +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, @@ -115,13 +120,24 @@ finalScore(inning, 9) might return: "Away": 5, } -*/ +*/ -function finalScore(/*code Here*/){ +function finalScore(inning, num) { - /*Code Here*/ + let Home = 0; + let Away = 0; + + for (let i = 0; i < num; i++) { + Home += inning(); + Away += inning(); + } + return {'home': Home, 'away': Away + } } + +console.log(finalScore(inning, 2)); + //-----------------------------------------------------------------------------// From 7bbce6148a2a45a4355b0a5a8c081cb5e4ab4ea7 Mon Sep 17 00:00:00 2001 From: Samantha Date: Wed, 30 Sep 2020 22:50:22 -0400 Subject: [PATCH 4/5] done with js tasks --- index.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 55eff516a..848dbce1d 100644 --- a/index.js +++ b/index.js @@ -152,7 +152,7 @@ Create a function called `scoreboard` that accepts the following parameters: (2) Callback function `inning` (2) A number of innings -and returns the score at each pont in the game, like so: +and returns the score at each point in the game, like so: 1st inning: awayTeam - homeTeam 2nd inning: awayTeam - homeTeam @@ -166,8 +166,24 @@ 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, inning, number) { + let Home = 0; + let Away = 0; + for(let i = 1; i <= number; i++){ + + Home += finalScore(inning, i).home; + Away += finalScore(inning, i).away; + + if(i == 1){console.log(`${i}st inning ${Away}-${Home}`)} + else if(i == 2){console.log(`${i}nd inning ${Away}-${Home}`)} + else if(i == 3){console.log(`${i}rd inning ${Away}-${Home}`) + }else{ + + console.log(`${i}th inning ${Away}-${Home}`); + } + } + return `final score ${Away}-${Home}` +} +console.log(scoreboard(finalScore, inning, 9)); \ No newline at end of file From 8cec12ea831245c563c837f1bea6feea3fa573bb Mon Sep 17 00:00:00 2001 From: Samantha Date: Wed, 30 Sep 2020 23:34:26 -0400 Subject: [PATCH 5/5] final commit for js --- index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 848dbce1d..27f5d5652 100644 --- a/index.js +++ b/index.js @@ -132,8 +132,7 @@ function finalScore(inning, num) { Away += inning(); } - return {'home': Home, 'away': Away - } + return {'home': Home, 'away': Away} } console.log(finalScore(inning, 2)); @@ -172,8 +171,8 @@ function scoreboard(finalScore, inning, number) { for(let i = 1; i <= number; i++){ - Home += finalScore(inning, i).home; - Away += finalScore(inning, i).away; + Home += finalScore(inning, i).away; + Away += finalScore(inning, i).home; if(i == 1){console.log(`${i}st inning ${Away}-${Home}`)} else if(i == 2){console.log(`${i}nd inning ${Away}-${Home}`)}