-
-
Notifications
You must be signed in to change notification settings - Fork 279
London10_Jan_Softa_Javascript_Core1_Coursework_Week3 #235
base: main
Are you sure you want to change the base?
Changes from all commits
5f85db9
4479cb2
d37495e
6e4638d
8b3e593
1644dfd
7df14ec
c643af8
937b176
41007e6
09efe9c
b9c9a5c
adb0499
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 |
|---|---|---|
|
|
@@ -4,37 +4,92 @@ | |
| The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less. | ||
| 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. Looks perfect! |
||
| // TODO | ||
| const headlines = []; | ||
| for (let articleTitle of allArticleTitles) { | ||
| if (articleTitle.length <= 65) { | ||
| headlines.push(articleTitle); | ||
| } | ||
| } | ||
| return headlines; | ||
| } | ||
|
|
||
| //I start with the function potentailHeadlines with an array argument. Inside the brakcets I have a local variable headlines | ||
| // that equals and empty array. It is used to the for ... if loop. It checks one article of all articles you see declared in | ||
| // the function. Then i have brackets with if and check the articles titles length is 65 or less. Next the .push method | ||
| // push the headlines. Then I use return the headlines. | ||
|
|
||
|
|
||
| /* | ||
| 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. This solution will return the title with the fewest characters, but this might be different from the title with the fewest words. |
||
| // TODO | ||
| let shortestHeadlines = allArticleTitles[0]; | ||
| // console.log(allArticleTitles[0]) | ||
| for (let i = 1; i < allArticleTitles.length; i++) { | ||
| let currentTitle = allArticleTitles[i]; | ||
| if (currentTitle.length < shortestHeadlines.length) { | ||
| shortestHeadlines = currentTitle | ||
| } | ||
| } | ||
| return shortestHeadlines; | ||
| } | ||
|
|
||
| // I start with a function that want to find a title with fewest words. I have an array as a parameter. Inside the brackets | ||
| // I use a let variable shortestHeadlines that equals allArticleTitles with counting from zero. I use a for... if loop with a | ||
| // let variable i equals 1, if i is less than the length of allArticleTitles, i count 1. Then I have a let variable currentTitle | ||
| // that equals allArticleTitles and the if statement that check if length of the currentTitle to shortestHeadlines then | ||
| // shortestHeadlines equals currentTitles. Then I return the variable for the function. | ||
| /* | ||
| 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 | ||
| } | ||
| let newArr = []; | ||
| for (let title of allArticleTitles) { | ||
| if (/[0-9]/.test(title) === true) { | ||
|
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. You can replace this line with |
||
| newArr.push(title); | ||
| } | ||
| } | ||
|
|
||
| return newArr; | ||
| } | ||
| // I start with a function headlineWithNumbers with an array checking all article titles. Inside the brackets I have a let variable | ||
| // with a new array to store article titles that contain at least a number. Then I use a for ... if loop that have let variable to | ||
| // I use .test method to check for digits from 0 - 9. If there are at least a digit it return true and a title is added to newArr and | ||
| // push to containing all articles with at least a number. | ||
| // | ||
| /* | ||
| 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. | ||
| */ | ||
|
|
||
|
|
||
|
|
||
|
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 nice solution. |
||
| function averageNumberOfCharacters(allArticleTitles) { | ||
| // TODO | ||
| let averageCharacter = 0; | ||
| let numberOfArticles = 0; | ||
| for (let articleTitle of allArticleTitles) { | ||
| averageCharacter += articleTitle.length; | ||
| numberOfArticles += 1; | ||
| } | ||
| return Math.round(averageCharacter / numberOfArticles); | ||
| } | ||
|
|
||
|
|
||
| // I start with a function that look for average number of characters with the array allArticleTitles as arguments. I use two let | ||
| // variables and both equals 0. I use a for loop with a variable that check article title of all article titles. Inside the innerbracket | ||
| // I check if average character is article title with the length method. Then number of articles += 1. I return with .round method | ||
| // and divide the two let variables averageCharacters and numberOfArticles. | ||
|
|
||
| /* ======= List of Articles - DO NOT MODIFY ===== */ | ||
| const ARTICLE_TITLES = [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,10 +33,34 @@ 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! | ||
| */ | ||
|
|
||
| // | ||
|
|
||
|
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 great solution! 👍 |
||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
|
|
||
| let arrayWithAveragePrices = []; | ||
|
|
||
| for (let arrayWithPrices of closingPricesForAllStocks) { | ||
| arrayWithAveragePrices.push(findAveragePrice(arrayWithPrices)); | ||
| } | ||
| return arrayWithAveragePrices; | ||
| } | ||
|
|
||
| function findAveragePrice(array) { | ||
| let sum = 0; | ||
| for (let price of array) { | ||
| sum += price; | ||
| } | ||
| return Number((sum / array.length).toFixed(2)); | ||
| } | ||
| // I start with a function getAveragePrice and use an array as a parameter. Inside the brackets, I have a let variable arrayWithAveragePrices | ||
| // that equals to an empty array that we fill later in the code. I use a for loop and in the lopp I have a let variable with two | ||
| // parameters arrayWithPrices / closingPricesForStocks. Inside the second bracket, I have arrayWithAveragePrices with a push method | ||
| // I findAveragePrices from arrayWithPrices. Then I return the function and it match let variable arrayWithAveragePrices. | ||
|
|
||
| // I start with a function findAveragePrice and have array as parameter. Inside the brackets I use a let variable sum equals to 0. | ||
| // I use a for loop that has a let variable with two parameters price / array. The second bracket, I have sum += price. Then I return | ||
| // Number that will be the sum divided by the array and use the .toFixed method that specifies I have two decimals. | ||
| /* | ||
| 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 | ||
|
|
@@ -47,9 +71,20 @@ 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 getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| let changedPrices = []; | ||
| for (let arrayWithPrices of closingPricesForAllStocks) { | ||
| let changedPrice = arrayWithPrices[arrayWithPrices.length - 1] - arrayWithPrices[0]; | ||
| changedPrices.push(Number(changedPrice.toFixed(2))); | ||
| } | ||
| return changedPrices; | ||
| } | ||
| // I start with a function getPriceChanges and have an array as my parameter. Inside the brickets I have a let variable that equals | ||
| // an empty array. I use for loop and start with a let variable arrayWithPrices of ... . Then i do a bracket inside the first bracket | ||
| // which means the let variable only execute 'til its closing bracket. It checks changedPrice equals arrayWithPrices that has its own | ||
| // array were I use the length method minus 1 minus arrayWithPrices start from 0. The changedPrice is pushed and price has two decimals | ||
| // which is achieved by using the .toFixed method. | ||
|
|
||
| /* | ||
| As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. | ||
|
|
@@ -65,7 +100,21 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| } | ||
| let arrayWithStrings = []; | ||
| for (let i = 0; i < stocks.length; i++) { | ||
| let stockOfName = stocks[i].toUpperCase(); | ||
| let sortedArray = closingPricesForAllStocks[i].sort(function (a, b) { | ||
|
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 looks good - I think it works. |
||
| return a-b}); | ||
| let highestPrice = sortedArray[sortedArray.length -1]; | ||
| let convertedPrice = highestPrice.toFixed(2); | ||
| arrayWithStrings.push(`The highest price of ${stockOfName} in the last 5 days was ${convertedPrice}`); | ||
|
|
||
| } | ||
| return arrayWithStrings; | ||
| // http://www.collectionsjs.com/sorted-array | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| /* ======= 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.
Looks great!
A small point - keep an eye on indentation, as it will make it easier for other developers to read your code.