-
-
Notifications
You must be signed in to change notification settings - Fork 279
BEKIR-London-10-JS-Week3-Mandatory+Extra #234
base: main
Are you sure you want to change the base?
Changes from all commits
f794a04
f61fc6d
2745a2d
e5af17d
008f5ac
4c3a2d2
44de8a5
d47ff7f
8608dc6
3da1e90
5dc4570
6a77bfe
1e1537e
dbe9b80
953db57
7684210
194f51a
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,16 +5,33 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
|
Contributor
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 is a good solution!
Author
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. Trying other methods and ways of solving code is something I'm going to be aiming for. Thank you. |
||
| // TODO | ||
| } | ||
| const potentialHeadlines = []; | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| const articleTitle = allArticleTitles[i]; | ||
|
|
||
| if (articleTitle.length <= 65) { | ||
| potentialHeadlines.push(articleTitle); | ||
| } | ||
| } | ||
|
|
||
| return potentialHeadlines; | ||
| } | ||
|
|
||
| /* | ||
| The editor of the FT likes short headlines with only a few words! | ||
| Implement the function below, which returns the title with the fewest words. | ||
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
|
Contributor
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. Your solution will give you the title with the fewest characters, but this might not be the fewest words. |
||
| // TODO | ||
| let shortestTitle = allArticleTitles[0]; | ||
|
|
||
| for (let i=1; i<allArticleTitles.length; i++) { | ||
| if (allArticleTitles[i].length < shortestTitle.length) { | ||
| shortestTitle = allArticleTitles[i]; | ||
| } | ||
| } | ||
| return shortestTitle; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,19 +40,33 @@ function titleWithFewestWords(allArticleTitles) { | |
| (Hint: remember that you can also loop through the characters of a string if you need to) | ||
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| } | ||
| const result = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| if (/[\d]/.test(allArticleTitles[i])) | ||
| result.push(allArticleTitles[i]); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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) { | ||
|
Contributor
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. Looks perfect! Good job 😄
Author
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. Thanks! |
||
| // TODO | ||
| let totalChars = 0; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| totalChars += allArticleTitles[i].length; | ||
| } | ||
| const averageChars = totalChars / allArticleTitles.length; | ||
| return Math.round(averageChars); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= List of Articles - DO NOT MODIFY ===== */ | ||
| const ARTICLE_TITLES = [ | ||
| "Streaming wars drive media groups to spend more than $100bn on new content", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| 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"]; | ||
|
|
||
|
|
@@ -34,9 +35,26 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
|
Contributor
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. Good solution, and nicely explained 👍 |
||
| // TODO | ||
| } | ||
| // Create an empty array to hold the average prices for each stock | ||
| const averagePrices = []; | ||
|
|
||
| // Loop through each array of closing prices for each stock | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| // Get the array of closing prices for the current stock | ||
| const stockPrices = closingPricesForAllStocks[i]; | ||
|
|
||
| // Calculate the sum of all closing prices for the current stock | ||
| const sum = stockPrices.reduce((acc, cur) => acc + cur); | ||
|
|
||
| // Calculate the average closing price for the current stock | ||
| const avg = sum / stockPrices.length; | ||
|
|
||
| // Round the average closing price to 2 decimal places and add it to the array | ||
| averagePrices.push(parseFloat(avg.toFixed(2))); | ||
| } | ||
|
|
||
| return averagePrices; | ||
| } | ||
| /* | ||
| 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 | ||
|
|
@@ -48,7 +66,15 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| The price change value should be rounded to 2 decimal places, and should be a number (not a string) | ||
| */ | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| const priceChanges = []; | ||
|
|
||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| const prices = closingPricesForAllStocks[i]; | ||
| const priceChange = (prices[prices.length-1] - prices[0]).toFixed(2); | ||
| priceChanges.push(parseFloat(priceChange)); | ||
| } | ||
|
|
||
| return priceChanges; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +90,16 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| const priceDescriptions = []; | ||
|
|
||
| for (let i = 0; i < stocks.length; i++) { | ||
| const ticker = stocks[i].toUpperCase(); | ||
| const highestPrice = Math.max(...closingPricesForAllStocks[i]).toFixed(2); | ||
|
Contributor
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 use of
Author
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. It's nice finding things and trying things I'm unfamiliar with on Google. |
||
| priceDescriptions.push(`The highest price of ${ticker} in the last 5 days was ${highestPrice}`); | ||
|
|
||
| } | ||
|
|
||
| return priceDescriptions; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,28 +31,20 @@ | |
| * Note: You are not expected to understand everything below this comment! | ||
| */ | ||
|
|
||
| function getAvailableStations() { | ||
| // Using `stations` as a property as defining it as a global variable wouldn't | ||
| // always make it initialized before the function is called | ||
| if (!getAvailableStations.stations) { | ||
| const stationCount = 4; | ||
| getAvailableStations.stations = []; | ||
| while (getAvailableStations.stations.length < stationCount) { | ||
| let randomFrequency = Math.floor(Math.random() * (108 - 87 + 1) + 87); | ||
| if (!getAvailableStations.stations.includes(randomFrequency)) { | ||
| getAvailableStations.stations.push(randomFrequency); | ||
| } | ||
| } | ||
| getAvailableStations.stations.sort(function (frequencyA, frequencyB) { | ||
| return frequencyA - frequencyB; | ||
| }); | ||
| function getAllFrequencies() { | ||
| const frequencies = []; | ||
| for (let i = 87; i <= 108; i++) { | ||
| frequencies.push(i); | ||
| } | ||
|
|
||
| return getAvailableStations.stations; | ||
| return frequencies; | ||
| } | ||
|
|
||
| function isRadioStation(frequency) { | ||
| return getAvailableStations().includes(frequency); | ||
| function getStations() { | ||
|
Contributor
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. Great work 😄 const radioStations = allFrequencies.filter(frequency => isRadioStation(frequency)); |
||
| const allFrequencies = getAllFrequencies(); | ||
| const radioStations = allFrequencies.filter((frequency) => { | ||
| return isRadioStation(frequency); | ||
| }); | ||
| return radioStations; | ||
| } | ||
|
|
||
| test("getAllFrequencies() returns all frequencies between 87 and 108", () => { | ||
|
|
||
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.
Nice 👍