-
-
Notifications
You must be signed in to change notification settings - Fork 279
London Class 8 - Matilda Ako - JS Core 1 Coursework - Week 3 #24
base: main
Are you sure you want to change the base?
Changes from all commits
efcc1be
2e41398
b351fbc
2b6b879
fabbba0
70a85c5
bc69a6e
99b2ee7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,13 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let allowedTitles = []; | ||
| for (let title of allArticleTitles) { | ||
| if (title.length <= 65) { | ||
| allowedTitles.push(title); | ||
| } | ||
| } | ||
| return allowedTitles; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,7 +20,17 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| let allTitles = potentialHeadlines(allArticleTitles); | ||
| let wordsLength = Infinity; | ||
| let titleWords = ""; | ||
| for (let title of allTitles) { | ||
| let words = title.split(" "); | ||
| if (words.length < wordsLength) { | ||
| wordsLength = words.length; | ||
| titleWords = title; | ||
| } | ||
| } | ||
| return titleWords; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,15 +39,28 @@ function titleWithFewestWords(allArticleTitles) { | |
| (Hint: remember that you can also loop through the characters of a string if you need to) | ||
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| let hasNumber = []; | ||
| for (title of allArticleTitles) { | ||
| if (title.match(/[0-9]/g)) { | ||
| hasNumber.push(title); | ||
| } | ||
| } | ||
| return hasNumber; | ||
| } | ||
|
|
||
| /* | ||
| The Financial Times wants to understand what the average number of characters in an article title is. | ||
| Implement the function below to return this number - rounded to the nearest integer. | ||
| */ | ||
| function averageNumberOfCharacters(allArticleTitles) { | ||
| // TODO | ||
| let titles = []; | ||
| for (title of allArticleTitles) { | ||
| titles.push(title.length); | ||
| } | ||
| let total = titles.reduce((a, b) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! I'd recommend avoiding very short variable names in a reduce because it can make it hard to know what they refer to. Also does |
||
| return a + b; | ||
| }) | ||
| return parseInt(total / titles.length) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let closingPrices = closingPricesForAllStocks; | ||
| let averagePrices = []; | ||
| for (let prices of closingPrices) { | ||
| let total = prices.reduce((a, b) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above about short variable names |
||
| return a + b; | ||
| }); | ||
| averagePrices.push(parseFloat((total / prices.length).toFixed(2))); | ||
| } | ||
| return averagePrices; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -47,8 +55,17 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| (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) | ||
| */ | ||
| function rounded(num) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👍 |
||
| return parseFloat(num.toFixed(2)); | ||
| } | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| let closingPrices = closingPricesForAllStocks; | ||
| let priceChange = []; | ||
| for (let prices of closingPrices) { | ||
| let change = prices[prices.length - 1] - prices[0]; | ||
| priceChange.push(rounded(change)); | ||
| } | ||
| return priceChange; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,31 +81,37 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| } | ||
| let closingPrices = closingPricesForAllStocks; | ||
| let stock = stocks; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this for? |
||
| let highestArr = []; | ||
|
|
||
| for (let i = 0; i < closingPrices.length; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good. Can you think of another way how you could find the highest without a |
||
| let highest = -Infinity; | ||
| for (let j = 0; j < closingPrices[i].length; j++) { | ||
| if (closingPrices[i][j] > highest) { | ||
| highest = closingPrices[i][j]; | ||
| } | ||
| } | ||
| highestArr.push(`The highest price of ${stock[i].toUpperCase()} in the last 5 days was ${highest.toFixed(2)}`); | ||
| } | ||
| return highestArr; | ||
| } | ||
|
|
||
| /* ======= 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] | ||
| ); | ||
| 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] | ||
| ); | ||
| 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" | ||
| ] | ||
| ); | ||
| 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", | ||
| ]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,16 @@ | |
| */ | ||
|
|
||
| function factorial(input) { | ||
| // TODO | ||
| let num = []; | ||
| let i = 1; | ||
| while (i <= input) { | ||
| num.push(i); | ||
| i++; | ||
| } | ||
| let result = num.reduce((a, b) => { | ||
| return a * b; | ||
| }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a more concise way to do this. (Tip: how could you do this without the |
||
| return result; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want a little extension you can try writing this function without the for loop step. Tip: have a think about how you can do it with just the reduce function below!