-
-
Notifications
You must be signed in to change notification settings - Fork 279
London-10-Elena-Barker_ JavaScript-Core-1-Coursework-Week3 #251
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,7 +12,15 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| output=[]; | ||
| for (city of cities) { | ||
| temperature = temperatureService(city); | ||
| string = `The temperature in ${city} is ${temperature} degrees`; | ||
| output.push (string); | ||
|
|
||
| } | ||
| return output; | ||
|
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. Comment: Well done! |
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| 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. | ||
| */ | ||
|
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. Almost perfect - but same comment as above about using |
||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| allTitles = []; | ||
| for (headline of allArticleTitles){ | ||
| if (headline.length <= 65) { | ||
| allTitles.push(headline) | ||
| } | ||
| } | ||
| return allTitles; | ||
|
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. Comment: Same solution as mine |
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,24 +20,58 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
|
|
||
| let shortestLine; | ||
| let length_headline = 100000; | ||
|
|
||
| for (headline of allArticleTitles) { | ||
| let currentTitleWordCount = headline.split (" ").length; | ||
|
|
||
| if (currentTitleWordCount < length_headline) { | ||
| shortestLine = headline; | ||
| length_headline = currentTitleWordCount; | ||
| } | ||
| } | ||
|
|
||
| return shortestLine; | ||
|
|
||
| } | ||
|
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. Comment: I see you use 100000; as a value. I assume it is a made up value. Consider: Have a look at how you can use [i] It is not used in any of your coding. Example: } |
||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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) | ||
| */ | ||
|
|
||
|
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. Very nice solution! |
||
| function hasNumber(myString) { | ||
| return /\d/.test(myString); | ||
| } | ||
|
|
||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| let arrayWithNumbers = []; | ||
| for (headline of allArticleTitles) { | ||
| let includesNumber = hasNumber(headline) | ||
| if (includesNumber) { | ||
| arrayWithNumbers.push(headline) | ||
| } | ||
| } | ||
|
|
||
| return arrayWithNumbers; | ||
|
|
||
|
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. Comment: I see you use parseInt. Nice. Different solution to mine |
||
| } | ||
|
|
||
| /* | ||
| /* | ||
| 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 totalCharacters = 0; | ||
| for (const headline of allArticleTitles){ | ||
| totalCharacters = totalCharacters + headline.length; | ||
| } | ||
| return Math.round(totalCharacters / 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. Comment: Nice! |
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
|
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 job! |
||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let arrayOfAveragePrices = []; | ||
|
|
||
| for (const stockPricesLastFiveDays of closingPricesForAllStocks) { | ||
| let sum = 0; | ||
| for (const stock of stockPricesLastFiveDays){ | ||
| sum += stock; | ||
| } | ||
| const averagePrice = Math.round((sum / stockPricesLastFiveDays.length ) * 100 ) / 100 | ||
|
|
||
| arrayOfAveragePrices.push(averagePrice) | ||
| } | ||
| return arrayOfAveragePrices; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,9 +59,20 @@ 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 newArrayOfDifference = []; | ||
| for (let oneSetOfPrices of closingPricesForAllStocks){ | ||
| lengthOfArray = oneSetOfPrices.length; | ||
| finalPositionInArray = lengthOfArray - 1; | ||
|
|
||
| let firstPrice = oneSetOfPrices[0]; | ||
| let lastPrice = oneSetOfPrices[finalPositionInArray]; | ||
| let differencePrice = Math.round((lastPrice - firstPrice) * 100) / 100; | ||
| newArrayOfDifference.push(differencePrice); | ||
| } | ||
| return newArrayOfDifference; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 | ||
|
|
@@ -64,8 +86,19 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let highestPriceForEachStock = []; | ||
| let highestPrice = 0; | ||
| let stock = 0; | ||
| for (element of closingPricesForAllStocks) { | ||
| highestPrice = Math.max(...element).toFixed(2); | ||
| highestPriceForEachStock.push( `The highest price of ${stocks[stock].toUpperCase()} in the last 5 days was ${highestPrice}`); | ||
| stock++; | ||
| } | ||
| return highestPriceForEachStock; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= 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.
This looks good 👍
One suggestion - it's a good idea to use
letorconstwhen declaring variables. This code will still work without it, but when you start working on larger programs - you could run into problems.