-
-
Notifications
You must be signed in to change notification settings - Fork 279
Glasgow 6 - Alena Lazareva - JS Core 1 - Week 3 #238
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -12,14 +12,19 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| const statements = []; | ||
| cities.forEach(city => { | ||
| statements.push('The temperature in '.concat(city, ' is ', | ||
|
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. Prefer using string interpolation instead of the |
||
| temperatureService(city), ' degrees')); | ||
| }); | ||
| return statements; | ||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| function temperatureService(city) { | ||
| let temparatureMap = new Map(); | ||
| let temparatureMap = new Map(); | ||
|
|
||
| temparatureMap.set('London', 10); | ||
| temparatureMap.set('Paris', 12); | ||
|
|
@@ -28,7 +33,7 @@ function temperatureService(city) { | |
| temparatureMap.set('Mumbai', 29); | ||
| temparatureMap.set('São Paulo', 23); | ||
| temparatureMap.set('Lagos', 33); | ||
|
|
||
| return temparatureMap.get(city); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,10 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| const smallArticle = allArticleTitles.filter(article => { | ||
|
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. if it's a one liner feel free to immediately |
||
| return article.length < 65; | ||
| }) | ||
| return smallArticle; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,24 +17,43 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| let fewestWordTitle = allArticleTitles[0]; | ||
| for (const article of allArticleTitles) { | ||
| if (article.length < fewestWordTitle.length) { | ||
| fewestWordTitle = article; | ||
| } | ||
| } | ||
| return fewestWordTitle; | ||
|
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 with simple use of an iterated array. You can of course also do it using |
||
| } | ||
|
|
||
| /* | ||
| The editor of the FT has realised that headlines which have numbers in them get more clicks! | ||
| Implement the function below to return a new array containing all the headlines which contain a number. | ||
| (Hint: remember that you can also loop through the characters of a string if you need to) | ||
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| } | ||
|
|
||
|
|
||
| function headlinesWithNumbers(allArticleTitles) { | ||
| let articleWithNums = []; | ||
| for (let i = 0; i < allArticleTitles.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. Similarly you already used |
||
| if (/\d/.test(allArticleTitles[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. Nice use of regular expressions |
||
| articleWithNums.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return articleWithNums; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 sumOfAllChars = 0; | ||
| allArticleTitles.forEach(article => { | ||
| sumOfAllChars += article.length; | ||
| }); | ||
| return Math.round(sumOfAllChars / allArticleTitles.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. This is yet again a good solution. If you are interested the |
||
| } | ||
|
|
||
|
|
||
|
|
@@ -78,4 +100,4 @@ test("should only return headlines containing numbers", () => { | |
|
|
||
| test("should return the average number of characters in a headline", () => { | ||
| expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); | ||
| }); | ||
| }); | ||
|
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 customary to have a newline at every file ending when handling code. Usually your IDE can be set up in a way to enforce this and most do it by default |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,8 +33,28 @@ 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) { | ||
|
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 usually good practive to remove commented code from PRs |
||
| // const averagePrices = closingPricesForAllStocks.map(stock => { | ||
| // const sumOfStocks = stock.reduce((accumulator, currentValue) => { | ||
|
|
||
| // return accumulator + currentValue; | ||
| // }) | ||
| // return Math.round((sumOfStocks / stock.length) * 100) / 100; | ||
| // }) | ||
| // return averagePrices; | ||
| // } | ||
|
|
||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| const averagePrices = []; | ||
| for (const closingPrices of closingPricesForAllStocks) { | ||
| let sum = 0; | ||
|
|
||
| for (const closingPrice of closingPrices) { | ||
| sum += closingPrice; | ||
| } | ||
| averagePrices.push(Math.round(sum / closingPrices.length * 100) / 100); | ||
| } | ||
| return averagePrices; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +68,12 @@ 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 priceChange = closingPricesForAllStocks.map(stock => { | ||
| const change = (stock[stock.length - 1] - stock[0]); | ||
| return Math.round(change * 100) / 100; | ||
|
|
||
| }) | ||
| return priceChange; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +89,13 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| const descriptions = []; | ||
| for (let index = 0; index < closingPricesForAllStocks.length; index++) { | ||
| const closingPrices = closingPricesForAllStocks[index]; | ||
| const maxPrice = Math.max(...closingPrices).toFixed(2); | ||
| descriptions.push(`The highest price of ${stocks[index].toUpperCase()} in the last 5 days was ${maxPrice}`); | ||
| } | ||
| return descriptions; | ||
| } | ||
|
|
||
|
|
||
|
|
||
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.
I believe this exercise was meant to be solved by simple iteration as forEach is only introduced during week 4. On the other hand if you are already using items from week 4 then prefer solving this exercise using
map: https://syllabus.codeyourfuture.io/js-core-1/week-4/lesson#map