Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

practiced scope #220

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ console.log(processFirstItem(['foo','bar'],function(str){return str+str}));
Study the code for counter1 and counter2, then answer the questions below.

1. What is the difference between counter1 and counter2?

In counter1, the counter is within the local scope of the function. In counter2, the counter is in the global scope.

2. Which of the two uses a closure? How can you tell?

Counter2 uses a closure because it is referencing a variable outside of its scope.

3. In what scenario would the counter1 code be preferable? In what scenario would
counter2 be better?
Counter1 is preferable when we need the counter to reset everytime the function is called. Counter2 is better when we need to 'store' or 'save' the number in counter for later usage.
*/

// counter1 code
Expand Down Expand Up @@ -62,10 +65,13 @@ 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)
}

// inning()
// console.log(inning())


/* ⚾️⚾️⚾️ Task 3: finalScore() ⚾️⚾️⚾️
Use the finalScore function below to do the following:
Expand All @@ -81,19 +87,33 @@ 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, 5))

/* ⚾️⚾️⚾️ Task 4: getInningScore() ⚾️⚾️⚾️
Use the getInningScore() function below to do the following:
1. Receive a callback function - you will pass in the inning function from task 2 as your argument
2. Return an object with a score for home and a score for away that populates from invoking the inning callback function */

function getInningScore(/*Your Code Here */) {
/*Your Code Here */
function getInningScore(inning) {
return {
'Home': inning(),
'Away': inning()
}
}

console.log(getInningScore(inning))

/* ⚾️⚾️⚾️ Task 5: scoreboard() ⚾️⚾️⚾️
Use the scoreboard function below to do the following:
Expand Down
Loading