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
152 lines (131 loc) · 7.21 KB
/
3-stocks.js
File metadata and controls
152 lines (131 loc) · 7.21 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
/*
THESE EXERCISES ARE QUITE HARD. JUST DO YOUR BEST, AND COME WITH QUESTIONS IF YOU GET STUCK :)
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!
*/
// 1. Find the total price of each company stocks in the last 5 days
// 2. Divide the total price by the number of days i.e. 5
function getAveragePrices(closingPricesForAllStocks) {
let arrayOfAverageValues = [];
for (let closingPriceEachCompany of closingPricesForAllStocks) {
let sumOfClosingPriceEachCompany = 0;
for (i = 0; i < closingPriceEachCompany.length; i++) {
sumOfClosingPriceEachCompany = sumOfClosingPriceEachCompany + closingPriceEachCompany[i];
averagePriceEachCompany = (sumOfClosingPriceEachCompany / closingPriceEachCompany.length).toFixed(2);
}
averagePricesNumberType = parseFloat(averagePriceEachCompany);
arrayOfAverageValues.push(averagePricesNumberType);
}
return arrayOfAverageValues;
}
/*
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)
*/
// 1. AAPL (apple) price difference last day - first day = -6.2
//
function getPriceChanges(closingPricesForAllStocks) {
let newPriceChangeArray = [];
for (let oneSetOfPrices of closingPricesForAllStocks) {
let priceChange = (oneSetOfPrices[(oneSetOfPrices.length - 1)] - oneSetOfPrices[0]).toFixed(2); //converted to 2 decimal places but it is also converted to string in the process
let priceChangeValues = parseFloat(priceChange); // removing the string values to numbers
newPriceChangeArray.push(priceChangeValues);
}
return newPriceChangeArray;
}
/*
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.
*/
// for (i = 0; i < closingPricesForAllStocks.length; i++) {
// let highestPrice = 0;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
// for (let companyPriceArray of closingPricesForAllStocks) {
// if (companyPriceArray[i] > highestPrice) {
// highestPrice = companyPriceArray[i];
// }
// }
// arrayWithHighestValue.push(highestPrice);
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
let highestCompanyStockPrices = [];
let namesOfCompanies = [];
let highestPrice = 0;
for (i = 0; i < closingPricesForAllStocks.length; i++) {
for (let closingPricePerDay of closingPricesForAllStocks[i]) {
if (closingPricePerDay > highestPrice) {
highestPrice = closingPricePerDay;
}
}
let upperStock = stocks[i].toUpperCase();
highestCompanyStockPrices.push(
`The highest price of ${upperStock} in the last 5 days was ${highestPrice.toFixed(2)}`);
highestPrice = 0;
}
return highestCompanyStockPrices;
// return `The highest price of ${stocks} in the last 5 days was ${arrayWithHighestValue[i]}`;
}
/* ======= 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"
]
);
});