-
-
Notifications
You must be signed in to change notification settings - Fork 279
London 9 Turing- Farnoosh Moayeri- JavaScript- Week 3 #205
base: main
Are you sure you want to change the base?
Changes from all commits
aed5234
bec2534
0fae195
4132e59
3537ba9
942d40d
55ba43e
8848341
f0e45e3
11553b1
a58439b
1c462cb
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,27 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| newArticleArray =[]; | ||
| for(let title of allArticleTitles){ | ||
|
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 impl is correct, but you can try to implement it in a simpler way using Array filter method |
||
| if(title.length <= 65){ | ||
| newArticleArray.push(title); | ||
| } | ||
| } | ||
| return newArticleArray; | ||
| } | ||
|
|
||
| /* | ||
| 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) { | ||
| // TODO | ||
| let shortestTitle = allArticleTitles[0]; | ||
|
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. Hint, you might be able to implement this using sorting algorithm, available in Array |
||
| for(let title of allArticleTitles){ | ||
| if(title.split(" ").length < shortestTitle.split(" ").length){ | ||
| shortestTitle = title; | ||
| } | ||
| } | ||
| return shortestTitle; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,15 +34,27 @@ 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 newArrayOfHeadlines = []; | ||
| for(let allTitles of allArticleTitles){ | ||
|
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. Similarly, you can try to use Array.filter to implement this |
||
| if(allTitles.match(/\d/)){ | ||
| newArrayOfHeadlines.push(allTitles); | ||
| } | ||
| } | ||
| return newArrayOfHeadlines; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 numberOfCharacters = 0; | ||
| for(let article of allArticleTitles){ | ||
| numberOfCharacters += article.length; | ||
| } | ||
| let averageCharacters = Math.round(numberOfCharacters / allArticleTitles.length); | ||
| return averageCharacters; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| 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"]; | ||
|
|
||
|
|
@@ -33,9 +34,24 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Solve the smaller problems, and then build those solutions back up to solve the larger problem. | ||
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| } | ||
|
|
||
| function getAverage(closingPrices){ | ||
| let sum = 0; | ||
| for(let i = 0; i < closingPrices.length; i++){ | ||
| sum += closingPrices[i]; | ||
| } | ||
| return sum / closingPrices.length; | ||
| }; | ||
|
|
||
| function getAveragePrices(closingPricesForAllStocks){ | ||
| let averagePrices = []; | ||
| for(let closingPrices of closingPricesForAllStocks){ | ||
| let averagePrice = getAverage(closingPrices); | ||
| averagePrice = Math.round(averagePrice * 100)/100; | ||
| averagePrices.push(averagePrice); | ||
| } | ||
| return averagePrices; | ||
| }; | ||
|
|
||
| /* | ||
| We also want to see what the change in price is from the first day to the last day for each stock. | ||
|
|
@@ -48,7 +64,13 @@ 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 | ||
| let priceChanges = []; | ||
| for(let closingPrices of closingPricesForAllStocks){ | ||
| let priceChange = closingPrices[closingPrices.length - 1] -closingPrices[0]; | ||
|
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. Likewise, can you try to factor out this into a function?
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. I have updated it. |
||
| priceChange = Math.round(priceChange * 100)/100; | ||
| priceChanges.push(priceChange); | ||
| } | ||
| return priceChanges | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +86,14 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let descriptions = []; | ||
| for(let i = 0; i < closingPricesForAllStocks.length; i++){ | ||
| let highestPrice = Math.max(...closingPricesForAllStocks[i]); | ||
| let stockTicker = stocks[i].toUpperCase(); | ||
| let description = `The highest price of ${stockTicker} in the last 5 days was ${highestPrice.toFixed(2)}`; | ||
| descriptions.push(description); | ||
| } | ||
| return descriptions; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,14 @@ | |
| */ | ||
|
|
||
| function getHighestRatedInEachGenre(books) { | ||
| // TODO | ||
| const highestRated = []; | ||
| for(let book of books){ | ||
| const {title, genre, rating} = book; | ||
| if(!highestRated[genre] || rating > highestRated[genre].rating){ | ||
|
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. Hmm, is is highestRated an array or object? |
||
| highestRated[genre] = {title, rating}; | ||
| } | ||
| } | ||
| return Object.values(highestRated).map((book) => book.title); | ||
| } | ||
|
|
||
|
|
||
|
|
||
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 one 👍