diff --git a/README.md b/README.md index 6d7f61f41..3bfc1eed8 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,18 @@ dansRoll(); ``` a. Where is closure used in this code? How can you tell? + +Closure is used within the anonymous function, as well as the personalDice function. The anonymous function has the constant newRoll within its scope, the function personalDice is able to reach out and pull the name parameter from dansRoll and zoesRoll constants. + b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change? + +The name will remain the same every time it is called. The roll is random and can change on each subsequent call. + + c. What is the lexical scope of `newRoll`? +newRoll has functional scope, and is only accessible to the anonymous function it resides within. + ### 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..79589c6e8 100644 --- a/index.js +++ b/index.js @@ -26,12 +26,17 @@ function processFirstItem(stringList, callback) { /* Task 1: `counterMaker` * Study the code for counter1 and counter2. Answer the questions below. * - * 1. What is the difference between counter1 and counter2? + * 1. What is the difference between counter1 and counter2? // counter2 will reset each time the function is called, whereas counter1 will store the memory of the function contained within it due to closure. * - * 2. Which of the two uses a closure? How can you tell? + * counter 1: the count variable is private and inside of the function & will store the previous count when it returns the total. * - * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? + * 2. Which of the two uses a closure? How can you tell? // counter1 code has a closure of count++ reaching out to the "let count = 0;", count is accessable from the inside of this closure to the outter function counterMaker. + * + * counter1 uses closure because of nested function. + * + * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? Since this is a counter, you need memory for your count, so counter1 code seems like a better option. If you were just trying to generate the number 1 over and over, you would want counter2. * + * Counter 1 is cleaner, more DRY, and leverages Closure. Counter2 creates a global variable instead of a block level variable. Which means that its memory usage is higher, and there is no valid reason to create a global scope variable for this to work. */ // counter1 code @@ -56,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 score = Math.floor(Math.random() * 3); + return score; } +inning(); /* Task 3: finalScore() @@ -76,11 +81,21 @@ finalScore(inning, 9) might return: */ -function finalScore(/*code Here*/){ +function finalScore(callback, numOfInnings){ + let home = 0; + let away = 0; + for (let i = 1; i <= numOfInnings; i++) { + home += callback(); + away += callback(); + } + return { + "Home": home, + "Away": away, + } +} - /*Code Here*/ +finalScore(inning, 9); -} /* Task 4: @@ -104,8 +119,34 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { - /* CODE HERE */ +function getOrdLabel(num) { + if (num === 1) { + return num + "st"; + } else if (num === 2) { + return num + "nd"; + } else if (num === 3) { + return num + "rd"; + } else { + return num + "th"; + } } +function scoreboard(callback, numOfInnings){ + let scores = { away: 0, home: 0, awayFinal: 0, homeFinal: 0 } + + + for (let i = 1; i <= numOfInnings; i++) { + scores.home = callback(); + scores.away = callback(); + scores.homeFinal += scores.home + scores.awayFinal += scores.away + + console.log(`${getOrdLabel(i)} inning: Away: ${scores.away} - Home: ${scores.home}`); + } + console.log(scores.homeFinal,scores.awayFinal) + + +} +scoreboard(inning,9); + \ No newline at end of file