diff --git a/index.js b/index.js index f3b2b6a1..cea5f0eb 100644 --- a/index.js +++ b/index.js @@ -30,11 +30,17 @@ console.log('example task:', processFirstItem(['foo','bar'],function(str){return Study the code for counter1 and counter2, then answer the questions below. 1. What is the difference between counter1 and counter2? + + counter1 defines the variable 'count' inside of the function. (function scoped) counter2 defines the variable 'count' in the global space. (global scoped) 2. Which of the two uses a closure? How can you tell? + + counter1 uses a closure because it defines the variable within the function. 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? + + counter1 would be preferred when you want it to reset every time it is used. counter2 is better when you want the count to be stored for later use. */ // counter1 code @@ -64,8 +70,8 @@ Use the inning function below to do the following: NOTE: This will be a callback function for the tasks below */ -function inning(/*Code Here*/){ - /*Code Here*/ +function inning(){ + return Math.floor(Math.random()*3) } @@ -83,9 +89,15 @@ Use the finalScore function below to do the following: } */ -function finalScore(/*Code Here*/){ - /*Code Here*/ +function finalScore(inning, inningsPlayed){ + let homeScore = 0; + let awayScore = 0; + for (let i = 0; i< inningsPlayed; i++){ + homeScore += inning(); + awayScore += inning(); + } return{'Home': homeScore,'Away': awayScore} } +console.log(finalScore(inning, 9)) /* ⚾️⚾️⚾️ Task 4: getInningScore() ⚾️⚾️⚾️ @@ -101,10 +113,10 @@ For example: invoking getInningScore(inning) might return this object: */ -function getInningScore(/*Your Code Here */) { - /*Your Code Here */ - +function getInningScore(inning) { + return {'Home': inning(), "Away": inning()} } +console.log(getInningScore(inning)) /* STRETCH: ⚾️⚾️⚾️ Task 5: scoreboard() ⚾️⚾️⚾️