Skip to content
Open

hmwk #116

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ 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
2. Study the following code, then answer the questions below.

```js
Expand Down
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<header>Test run page

</header>

<head> running </head>
<body>

hello
</body>
36 changes: 31 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ 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 returning the function, counter 2 is returning the result of the function
*
* 2. Which of the two uses a closure? How can you tell?
*
*
* 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better?
*
*/
Expand All @@ -56,11 +58,13 @@ 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*/){
function inning(){
return Math.floor(Math.random() * Math.floor(5))

/*Code Here*/

}
console.log(inning())

/* Task 3: finalScore()

Expand All @@ -76,12 +80,20 @@ finalScore(inning, 9) might return:

*/

function finalScore(/*code Here*/){
function finalScore(inningFunc, numOfInnings){
/* the higher order function is using the function from the previous question in one of the arguements */
let score = 0
for( let i = 0; i < numOfInnings; i ++){

score += inningFunc()
}
return score
/*Code Here*/

}

console.log(finalScore(inning, 9 ))

/* Task 4:

Create a function called `scoreboard` that accepts the following parameters:
Expand All @@ -104,8 +116,22 @@ 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, numOfInnings) {
let score2 = {
home: 0,
away: 0,
}
let scoreArray = []

for(let i = 0; i <= numOfInnings; i++ ){
score2.home += finalScore(0,1),
score2.away += finalScore(0,1);

scoreArray.push(`${i} inning ${score2.home} ${score2.away}`)

}
console.log(scoreArray)

}

console.log(scoreboard(inning,9))
Loading