Skip to content
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ The module challenge is the afternoon project or assignment that students work t
## Objectives

- Explain function scope

*Function Scope is when your variable is defined inside of your function only, and it can not be found anywhere else in your code. You can use other variables from outside the function but nothing on the outside can use whats inside.

- Describe what closure is, how closure is created in a program and why it is important to understand closures in JavaScript

*


## Introduction

This challenge focuses on both scope and closures.
Expand Down Expand Up @@ -42,6 +48,8 @@ Find the file `index.js` and complete the tasks until your tests are passing.
Edit the `ReadMe` file with your answers.

1. In your own words, define closure (1-2 sentences).
- closure allows your nested funtions to go out and get variables in functions outside the nested function.

2. Study the following code, then answer the questions below.

```js
Expand All @@ -63,9 +71,17 @@ dansRoll();
```

a. Where is closure used in this code? How can you tell?

-The closure is inside of the function "personalDice", you can tell because it is using a function inside of another function.

b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change?

-It is being ran the same each time it is called. The only difference between each time it is called would be that each call will be completely random and may never be the same out of 6 rolls.

c. What is the lexical scope of `newRoll`?

-"personalDice"

### 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.
Expand Down
70 changes: 50 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ function processFirstItem(stringList, callback) {
* Study the code for counter1 and counter2. Answer the questions below.
*
* 1. What is the difference between counter1 and counter2?
*
* -Counter 1 is nested inside of the function called "counterMaker", where count2 however is not which makes it a global variable.
*
* 2. Which of the two uses a closure? How can you tell?
*
* -Count1 because it is nested inside of a function.
*
* 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better?
*
*
*-You should use count1 if you needed a variable that you want to use in the function which can create multiple values.

-If you have multiple functions that need to access one global variable count2 is ideal.
*/

// counter1 code
Expand All @@ -56,32 +63,42 @@ 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(){

return Math.round(Math.random() * 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.
console.log(inning());
console.log(inning());
console.log(inning());
console.log(inning());

For example,
// 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.

finalScore(inning, 9) might return:
{
"Home": 11,
"Away": 5,
}
// For example,

*/
// finalScore(inning, 9) might return:
// {
// "Home": 11,
// "Away": 5,
// }

function finalScore(/*code Here*/){

/*Code Here*/
// */

function finalScore(cb, num) {
let score ={
home: 0,
away: 0,
};
for(let i = 0; i < num; i++) {
score.home += cb();
score.away += cb();
}
return score;
}

console.log(finalScore(inning, 9));

/* Task 4:

Create a function called `scoreboard` that accepts the following parameters:
Expand All @@ -104,8 +121,21 @@ 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, inningCB, num) {
let home = 0;
let away = 0;

for(let i = 1; i <= num; i++) {
home += finalScore(inningCB, i).home;
away += finalScore(inningCB, i).away;

if(i == 1){console.log(`${i}st inning ${away} - ${home}`)}
else if(i == 2){console.log(`${i}st inning ${away} - ${home}`)}
else if(i == 3){console.log(`${i}st inning ${away} - ${home}`)}
else {console.log(`${i}th inning ${away} - ${home}`)}
}
return `Tonights final score is ${away} - ${home}`
}


console.log(scoreboard(finalScore, inning, 9));