This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy path3-stocks.js
More file actions
189 lines (158 loc) · 8.67 KB
/
3-stocks.js
File metadata and controls
189 lines (158 loc) · 8.67 KB
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
/*
THESE EXERCISES ARE QUITE HARD. DON'T WORRY IF YOU CAN'T COMPLETE THEM ALL - JUST DO YOUR BEST, AND COME WITH QUESTIONS :)
Imagine we a working for a finance company. Below we have:
- an array of stock tickers
- an array of arrays containing the closing price for each stock in each of the last 5 days.
For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for STOCKS[2] (which is amzn)
*/
/* ======= Stock data - DO NOT MODIFY ===== */
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];
const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.20, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL
[1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA
];
/*
We want to understand what the average price over the last 5 days for each stock is.
Implement the below function, which
- Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays)
- Returns an array containing the average price over the last 5 days for each stock.
For example, the first element of the resulting array should contain Apple’s (aapl) average stock price for the last 5 days.
The second element should be Microsoft's (msft) average price, and so on.
The average value should be rounded to 2 decimal places, and should be a number (not a string)
Hint 1: To calculate the average of a set of values, you can add them together and divide by the number of values.
So the average of 5, 10 and 20 is (5 + 10 + 20) / 3 = 11.67
Hint 2: If the problem seems complex, try breaking it down into smaller problems.
Solve the smaller problems, and then build those solutions back up to solve the larger problem.
Functions can help with this!
*/
/*
SOLUTION EXPLANATION: This is a complex problem and there are many ways to solve it!
I've decided to break the large problem down into a few smaller problems.
1. The exercise is asking us to find the average price of EACH stock.
So first, maybe we can just work out how to get the average price for a SINGLE stock.
For this, I've created a separate function called getAveragePricesForStock.
2. We also need to work out how to round a number to 2 decimal places.
There's also a separate function for this called roundTo2Decimals.
3. We can put these smaller solutions back together to solve the larger problem.
The top-level getAveragePrices prices function can loop through the array,
and pass each sub-array to the getAveragePricesForStock function.
*/
function getAveragePrices(closingPricesForAllStocks) {
let averages = [];
for(let pricesForStock of closingPricesForAllStocks) {
averages.push(getAveragePricesForStock(pricesForStock));
}
return averages;
}
function getAveragePricesForStock(pricesForStock) {
let total = 0;
for(let price of pricesForStock) {
total += price;
}
return roundTo2Decimals(total / pricesForStock.length);
}
function roundTo2Decimals(num) {
return Math.round(num * 100) / 100;
}
/*
We also want to see what the change in price is from the first day to the last day for each stock.
Implement the below function, which
- Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays)
- Returns an array containing the price change over the last 5 days for each stock.
For example, the first element of the resulting array should contain Apple’s (aapl) price change for the last 5 days.
In this example it would be:
(Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
/*
SOLUTION EXPLANATION: Again, we can break the large problem down into a few smaller problems.
1. I've created a new function which just calculates the change in price for a SINGLE stock - getPriceChangeForStock.
2. This new function can also use the roundTo2Decimals function we implemented earlier.
One of the many advatages of using functions is being able to re-use code!
*/
function getPriceChanges(closingPricesForAllStocks) {
let changes = [];
for(let pricesForStock of closingPricesForAllStocks) {
changes.push(getPriceChangeForStock(pricesForStock))
}
return changes;
}
function getPriceChangeForStock(pricesForStock) {
let priceChange = pricesForStock[pricesForStock.length - 1] - pricesForStock[0]
return roundTo2Decimals(priceChange);
}
/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Implement the below function, which
- Takes 2 parameters:
- the CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays)
- the STOCKS array
- Returns an array of strings describing what the highest price was for each stock.
For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33"
The test will check for this exact string.
The stock ticker should be capitalised.
The price should be shown with EXACTLY 2 decimal places.
*/
/*
SOLUTION EXPLANATION: We can also break this problem down into a smaller problems.
1. I've created a new function which just calculates the highest price for a SINGLE stock - getHighestPrice.
2. This new function can also use the roundTo2Decimals function we implemented earlier.
ALTERNATE SOLUTION: See highestPriceDescriptionsAlternate
I've included an alternate solution here which makes use of JavaScript's Math.max() function
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
The Math.max() function takes zero or more numbers as input parameters.
We can convert our array into individual numbers that can then be passed into this function using the spread syntax: ...
Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
let descriptions = [];
for(let i = 0; i < closingPricesForAllStocks.length; i++) {
let highestPrice = getHighestPrice(closingPricesForAllStocks[i]);
descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`);
}
return descriptions;
}
function getHighestPrice(pricesForStock) {
// initialising to 0, as we're expecting this value to be overriden by the first price in the array
let highestPriceSoFar = 0;
for(let price of pricesForStock) {
// if this price is higher than the highest price we've seen so far, it becomes the new highest price
if(price > highestPriceSoFar) {
highestPriceSoFar = price;
}
}
return highestPriceSoFar;
}
function highestPriceDescriptionsAlternate(closingPricesForAllStocks, stocks) {
let descriptions = [];
for(let i = 0; i < closingPricesForAllStocks.length; i++) {
let highestPrice = Math.max(...closingPricesForAllStocks[i]);
descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`);
}
return descriptions;
}
/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[176.89, 335.66, 3405.66, 2929.22, 1041.93]
);
});
test("should return the price change for each stock", () => {
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[-6.2, -13.4, 23.9, -82.43, -162.77]
);
});
test("should return a description of the highest price for each stock", () => {
expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual(
[
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30"
]
);
});