-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
index.js
172 lines (134 loc) · 5.36 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//❗❗ PLEASE READ THE README file for project instructions, helpful resources, additional tasks and stretch problems, and more ❗❗
// ⭐️ Example Challenge START ⭐️
/**Example Task : processFirstItem()
* This example shows how you might go about solving the rest of the tasks
*
* Use the higher order function processFirstItem below to do the following:
* 1. Receive an array of strings in a parameter
* 2. Receive a callback function that takes a string as its argument in a parameter
* 3. Return the result of invoking the callback function and passing in the FIRST
* element in the array as the argument
*
* The following code is demonstrating a way of completing this task
* It returns the string `foofoo`
*/
function processFirstItem(stringList, callback) {
return callback(stringList[0])
}
console.log('example task:', processFirstItem(['foo','bar'],function(str){return str+str}));
// ⭐️ Example Challenge END ⭐️
///// M V P ///////
/*Task 1: counterMaker()
Study the code for counter1 and counter2, then 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() ⚾️⚾️⚾️
Use the inning function below to do the following:
1. Return a random whole number of points between 0 and 2 scored by one team in an inning
For example: invoking inning() should return a numerical score value of 0, 1, or 2
NOTE: This will be a callback function for the tasks below
*/
function inning(/*Code Here*/){
/*Code Here*/
}
/* ⚾️⚾️⚾️ Task 3: finalScore() ⚾️⚾️⚾️
Use the finalScore function below to do the following:
1. Receive the callback function `inning` that was created in Task 2 in the first parameter
2. Receive a number of innings to be played in the second parameter
3. After each inning, update the score of the home and away teams
4. After the last inning, return an object containing the final (total) score of the innings played
For example: invoking finalScore(inning, 9) might return this object:
{
"Home": 11,
"Away": 5
}
*/
function finalScore(/*Code Here*/){
/*Code Here*/
}
/* ⚾️⚾️⚾️ Task 4: getInningScore() ⚾️⚾️⚾️
Use the getInningScore() function below to do the following:
1. Receive a callback function in a parameter - 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
For example: invoking getInningScore(inning) might return this object:
{
"Home": 0,
"Away": 2
}
*/
function getInningScore(/*Your Code Here */) {
/*Your Code Here */
}
/* STRETCH: ⚾️⚾️⚾️ Task 5: scoreboard() ⚾️⚾️⚾️
Use the scoreboard function below to do the following:
1. Receive the callback function in the first parameter that will take `getInningScore` from Task 4 as its argument
2. Receive the callback function in a second parameter that will take `inning` from Task 2 as its argument
3. Receive a number in a third parameter that will take the number of innings to be played as its argument
4. Return an array where each of it's index values equals a string stating the
Home and Away team's scores for each inning. Not the cummulative score (see the example below).
5. If there's a tie at the end of the innings, add this message containing the score to the end of the array: "This game will require extra innings: Away 12 - Home 12" (see tie example below)
If there isn't a tie, add this message to the end of the array: "Final Score: Away 13 - Home 11" (see no tie example below)
NO TIE example: invoking scoreboard(getInningScore,inning, 9) might return
an array of strings like this:
[
"Inning 1: Away 1 - Home 2",
"Inning 2: Away 2 - Home 1",
"Inning 3: Away 0 - Home 2",
"Inning 4: Away 2 - Home 2",
"Inning 5: Away 2 - Home 0",
"Inning 6: Away 1 - Home 1",
"Inning 7: Away 0 - Home 2",
"Inning 8: Away 2 - Home 2",
"Inning 9: Away 1 - Home 0",
"Final Score: Away 11 - Home 12"
]
TIE example: invoking scoreboard(getInningScore,inning, 9) might return
an array of strings like this:
[
"Inning 1: Away 1 - Home 1",
"Inning 2: Away 2 - Home 2",
"Inning 3: Away 1 - Home 0",
"Inning 4: Away 1 - Home 2",
"Inning 5: Away 0 - Home 0",
"Inning 6: Away 2 - Home 1",
"Inning 7: Away 0 - Home 2",
"Inning 8: Away 2 - Home 1",
"Inning 9: Away 1 - Home 1",
"This game will require extra innings: Away 10 - Home 10"
] */
// NOTE: There is no test associated with this code; if your output matches the given example, consider it complete!
function scoreboard(/* CODE HERE */) {
/* CODE HERE */
}
/* 🛑🛑🛑🛑🛑 Please do not modify anything below this line 🛑🛑🛑🛑🛑 */
function foo(){
console.log('its working');
return 'bar';
}
foo();
module.exports = {
foo,
processFirstItem,
counter1,
counter2,
inning,
finalScore,
getInningScore,
scoreboard,
}