-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (79 loc) · 2.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// ⭐️ Example Challenge START ⭐️
/**
* ### Challenge `processFirstItem`
*
* @instructions
* Implement a higher-order function called `processFirstItem`.
* It takes two arguments:
* @param stringList an array of strings.
* @param callback function that takes a string as its argument.
* @returns the result of invoking `callback` with the FIRST element in `stringList`.
*
* Example of usage of this higher-order function:
* Invoking `processFirstItem` passing `['foo', 'bar']` and `(str) => str + str`,
* should return 'foofoo'.
*/
function processFirstItem(stringList, callback) {
return callback(stringList[0])
}
// ⭐️ Example Challenge END ⭐️
///// M V P ///////
/* Task 1: `counterMaker`
* Study the code for counter1 and counter2. Answer the questions below.
*
* 1. What is the difference between counter1 and counter2?
*
* 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?
*
*/
// counter1 code
function counterMaker() {
let count = 0;
return function counter() {
return count++;
}
}
const counter1 = counterMaker();
// counter2 code
let count = 0;
function counter2() {
return count++;
}
/* Task 2: inning()
Write a function called `inning` that returns 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*/
}
/* 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.
For example,
finalScore(inning, 9) might return:
{
"Home": 11,
"Away": 5,
}
*/
function finalScore(/*code Here*/){
/*Code Here*/
}
/* Task 4:
Create a function called `scoreboard` that accepts the following parameters:
(1) Callback function `getInningScore`
(2) Callback function `inning`
(3) A number of innings
and returns the score at each pont in the game, like so:
1st inning: awayTeam - homeTeam
2nd inning: awayTeam - homeTeam
3rd inning: awayTeam - homeTeam
4th inning: awayTeam - homeTeam
5th inning: awayTeam - homeTeam
6th inning: awayTeam - homeTeam
7th inning: awayTeam - homeTeam
8th inning: awayTeam - homeTeam
9th inning: awayTeam - homeTeam
Final Score: awayTeam - homeTeam */
function scoreboard(/* CODE HERE */) {
/* CODE HERE */
}