diff --git a/README.md b/README.md index 6d7f61f41..1afdd07c7 100644 --- a/README.md +++ b/README.md @@ -22,49 +22,29 @@ 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 -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 -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 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 @@ -96,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/Written Q&A.txt b/Written Q&A.txt new file mode 100644 index 000000000..ad68307c2 --- /dev/null +++ b/Written Q&A.txt @@ -0,0 +1,51 @@ +### 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? + +-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. + + + +------------------------------------------------------------------- + + +>> c. What is the lexical scope of `newRoll`? diff --git a/index.js b/index.js index c2cac5161..27f5d5652 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,48 @@ 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 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? + * + * * + * 2. Which of the two uses a closure? How can you tell? + * ------------------------------------------------- +//---// 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. + * * */ // counter1 code + function counterMaker() { let count = 0; return function counter() { @@ -44,27 +73,44 @@ function counterMaker() { const counter1 = counterMaker(); + + + // counter2 code + let count = 0; function counter2() { return count++; } +//-----------------------------------------------------------------------------// + -/* 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. */ -function inning(/*Code Here*/){ +// Task 2: inning() - /*Code Here*/ +// 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() { + + return Math.floor(Math.random() * 3); } +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. +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, @@ -74,13 +120,28 @@ 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)); + + + +//-----------------------------------------------------------------------------// + + /* Task 4: @@ -90,7 +151,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 @@ -104,8 +165,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).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}`)} + 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