From 22e477f55e689f7e1dbfb85b73ee8dbac3f9acf8 Mon Sep 17 00:00:00 2001 From: Benaiah Varner Date: Mon, 14 Sep 2020 19:35:45 -0400 Subject: [PATCH] so far --- index.js | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index c2cac5161..a88518069 100644 --- a/index.js +++ b/index.js @@ -28,10 +28,16 @@ function processFirstItem(stringList, callback) { * * 1. What is the difference between counter1 and counter2? * + * Counter 1 is two functions nested inside eachother and counter2 is one function. Counter 1 also has the let count variable function scoped so other functions can't access it. + * * 2. Which of the two uses a closure? How can you tell? * + * counter1 uses a closure because of the function nested inside of it. + * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? * + * Counter1 code is preferable when you don't want any other part of your program to use the code or variables nested inside counter1. Vice versa with counter2 + * */ // counter1 code @@ -56,12 +62,15 @@ 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(inning, min, max){ + min = Math.ceil(0); + max = Math.floor(2); + let score = Math.floor(Math.random() * (max - min + 1)) + min; + return `In inning ${inning} team A scored ${score}` } +console.log(inning(3, 0, 2)) + /* 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 +85,25 @@ finalScore(inning, 9) might return: */ -function finalScore(/*code Here*/){ - - /*Code Here*/ +function finalScore(callback, inning){ + let home = 0; + let away = 0; + for (let i = 0; i < inning; i++) { + home = callback + } + for (let i = 0; i < inning; i++) { + away = callback + } + const obj = { + "Home": home, + "Away": away + } + return obj } +console.log(finalScore(inning, 9)) + /* Task 4: Create a function called `scoreboard` that accepts the following parameters: