forked from bitmakerlabs/callbacks_exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
317 lines (260 loc) · 8.5 KB
/
main.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
var transactions = [
{
type: 'sale',
paymentMethod: 'cash',
customer: 'Isaac Asimov',
items: [
{ name: 'Byte', price: 1.00 },
{ name: 'Bit', price: 0.125 }
]
},
{
type: 'sale',
paymentMethod: 'credit',
customer: 'CJ Cherryh',
items: [
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 }
]
},
{
type: 'purchase',
paymentMethod: 'credit',
vendor: 'Dick\'s Doodads',
items: [
{ name: 'XL Doodad', price: -3.00 },
{ name: 'XS Doodad', price: -0.50 }
]
},
{
type: 'purchase',
paymentMethod: 'cash',
vendor: 'Gibson Gadgets',
items: [
{ name: 'Basic Gadget', price: -2.00 },
{ name: 'Advanced Gadget', price: -3.50 }
]
},
{
type: 'sale',
paymentMethod: 'credit',
customer: 'Frederik Pohl',
items: [
{ name: 'Byte', price: 1.00 },
{ name: 'Byte', price: 1.00 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 }
]
},
{
type: 'purchase',
paymentMethod: 'cash',
vendor: 'Clarke Computing',
items: [
{ name: 'Floppy Disk', price: -0.10 },
{ name: 'Floppy Disk', price: -0.10 },
{ name: 'Floppy Disk', price: -0.10 },
{ name: 'Floppy Disk', price: -0.10 },
{ name: 'Floppy Disk', price: -0.10 },
{ name: 'Floppy Disk', price: -0.10 },
{ name: 'Floppy Disk', price: -0.10 }
]
},
{
type: 'sale',
paymentMethod: 'credit',
customer: 'Neal Stephenson',
items: [
{ name: 'kilobyte', price: 1024.00 }
]
},
{
type: 'purchase',
paymentMethod: 'credit',
vendor: 'Gibson Gadgets',
items: [
{ name: 'Advanced Gadget', price: -3.50 },
{ name: 'Advanced Gadget', price: -3.50 },
{ name: 'Advanced Gadget', price: -3.50 },
{ name: 'Advanced Gadget', price: -3.50 }
]
},
{
type: 'purchase',
paymentMethod: 'credit',
vendor: 'Dick\'s Doodads',
items: [
{ name: 'XL Doodad', price: -3.00 },
{ name: 'XL Doodad', price: -3.00 },
{ name: 'XL Doodad', price: -3.00 }
]
},
{
type: 'sale',
paymentMethod: 'cash',
customer: 'Isaac Asimov',
items: [
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
{ name: 'Bit', price: 0.125 },
]
}
];
// --------------------------------------------------
// EXAMPLE QUESTION
// --------------------------------------------------
/*
Calculate the total number of transactions.
*/
var totalTransactions = transactions.length;
console.log( 'The total number of transactions is:', totalTransactions );
// --------------------------------------------------
// QUESTION 01
// --------------------------------------------------
/*
Calculate the total number of 'sales'.
HINT(S):
- Not all transactions are 'sales'.
*/
var numSales;
/*
Hey, welcome to the first question!
Here's a breakdown of the question, and some pointers on how to get started!
- A variable has been declared a few lines above (`numSales`).
- Just below, the contents of the `numSales` variable are logged to the console.
- Your job is to assign the variable to the correct value (in this case: the total number of sales) *BEFORE* it is logged out.
- You can do this by:
- Adding an `=` sign (we are *assigning* something after all)
- Starting with the `transactions` variable (see the example question);
- Adding one or more methods to transform/extract the value we're looking for.
- If your solution is correct, `numSales` should be equal to 5.
You can solve the remaining questions in the same way!
P.S.
The breakdown above takes up a lot of space, feel free to move it to the top or bottom of the file!
*/
console.log( 'The total number of sales is:', numSales );
// --------------------------------------------------
// QUESTION 02
// --------------------------------------------------
/*
Calculate the total number of 'purchases'.
*/
var numPurchases;
console.log( 'The total number of purchases is:', numPurchases );
// --------------------------------------------------
// QUESTION 03
// --------------------------------------------------
/*
Calculate the total number of 'cash' 'sales'.
HINT(S):
- Don't forget that 'purchases' can also be made in 'cash'!
*/
var numCashSales;
console.log( 'The total number of cash sales is:', numCashSales );
// --------------------------------------------------
// QUESTION 04
// --------------------------------------------------
/*
Calculate the total number of 'credit' 'purchases'.
HINT(S):
- Make sure to exclude any 'sales' made by 'credit'!
*/
var numCreditPurchases;
console.log( 'The total number of credit purchases is:', numCreditPurchases );
// --------------------------------------------------
// QUESTION 05
// --------------------------------------------------
/*
Create an array that includes all of vendors which appear in the transactions data set.
eg. `[ 'vendor one', 'vendor two', ... ]
HINT(S):
- Not all transactions have a 'vendor'!
- The assembled array should be made up of strings, not full `transaction` objects.
- This array is allowed to contain duplicate values.
*/
var uniqueVendors;
console.log( 'The unique vendors are:', uniqueVendors );
// --------------------------------------------------
// QUESTION 06
// --------------------------------------------------
/*
Create an array that includes all of *unique* customers which appear in the transactions data set.
eg. `[ 'customer one', 'customer two', ... ]
HINT(S):
- Not all transactions have a 'customer'!
- The assembled array should be made up of strings, not full `transaction` objects.
- Make sure that the resulting array *does not* include any duplicates.
*/
var uniqueCustomers;
console.log( 'The unique customers are:', uniqueCustomers );
// --------------------------------------------------
// QUESTION 07
// --------------------------------------------------
/*
Create an array of information about the 'sale' transactions which include 5 or more items.
The array should resemble the following:
[ { name: 'Customer Name', numItems: 5 }, ... ]
HINT(S):
- There may be more than 1 'sale' that includes 5 or more items.
- Individual transactions do not have either `name` or `numItems` properties, we'll have to add them to the output.
*/
var bigSpenders;
console.log( 'The "big spenders" are:', bigSpenders );
// --------------------------------------------------
// QUESTION 08
// --------------------------------------------------
/*
Calculate the sum of the *first* 'sale' transaction.
HINT(S):
- Transactions don't have 'prices', but their 'items' do!
*/
var sumSales;
console.log( 'The sum of all sales is:', sumSales );
// --------------------------------------------------
// QUESTION 09
// --------------------------------------------------
/*
Calculate the sum of *all* 'purchase' transactions.
HINT(S):
- Your solution to 'QUESTION 08' is a good starting point!
- Make sure to include 'price' information from *all* purchases.
*/
var sumPurchases;
console.log( 'The sum of all purhcases is:', sumPurchases );
// --------------------------------------------------
// QUESTION 10
// --------------------------------------------------
/*
Calculate the company's net profit.
This number will be positive if the sum of the sales is greater than the amount spent on purchases.
Otherwise, this number will be negative.
HINT(S):
- Unlike 'QUESTION 08' and 'QUESTION 09', here we're interested in both 'sale' and 'purchase' transactions.
*/
var netProfit;
console.log( 'The net profit is:', netProfit );
// --------------------------------------------------
// QUESTION 11
// --------------------------------------------------
/*
Calculate the most items sold as part of single transaction.
HINTS:
- The result of this calculation should be a number (not an array, object, or other data type).
*/
var mostItems;
console.log( 'The most items sold in a single transaction is:', mostItems );
// --------------------------------------------------
// QUESTION 12
// --------------------------------------------------
/*
Calculate the sum of the 'purchase' with the fewest items.
*/
var sumOfSmallestPurchase;
console.log( 'The sum of the smallest purchase is:', sumOfSmallestPurchase );