-
-
Notifications
You must be signed in to change notification settings - Fork 279
London9-Lovelace-Mohamed Abdi-JavaScript-Core-1-Coursework-Week3 #177
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 |
|---|---|---|
|
|
@@ -6,15 +6,28 @@ | |
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let newArray = []; | ||
| for (let i = 0; i < allArticleTitles.length;i++) { | ||
|
|
||
| if (allArticleTitles[i].length <= 65) { | ||
| newArray.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return newArray; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 arr = allArticleTitles; | ||
| let newArr = [...allArticleTitles].sort((a, b) => a.length - b.length); | ||
| return newArr[0]; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -24,14 +37,32 @@ function titleWithFewestWords(allArticleTitles) { | |
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| let newArray = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| if (/\d/.test(allArticleTitles[i])) { | ||
| newArray.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
|
|
||
| return newArray; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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 average = 0; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| average += allArticleTitles[i].length; | ||
| } | ||
| return parseInt(average / allArticleTitles.length); | ||
|
Member
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. What happens here is |
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,8 +35,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let averageStock = []; | ||
| let sum = 0; | ||
| for (let stockPriceBrand of closingPricesForAllStocks) { | ||
| for (let unitStockPriceBrand of stockPriceBrand) { | ||
| sum += unitStockPriceBrand; | ||
| } | ||
| let averageAllStock = sum / 5; | ||
| averageStock.push(parseFloat(averageAllStock.toFixed(2))); | ||
| sum = 0; | ||
| } | ||
| return averageStock; | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
|
|
@@ -49,8 +60,17 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| */ | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| let priceChangeStock = []; | ||
| for (let stockPriceBrand of closingPricesForAllStocks) { | ||
| let priceChangeBrand = stockPriceBrand[4] - stockPriceBrand[0]; | ||
| let FormattedPriceChangeBrand = parseFloat(priceChangeBrand.toFixed(2)); | ||
|
Comment on lines
+65
to
+66
Member
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. try to use |
||
| priceChangeStock.push(FormattedPriceChangeBrand); | ||
| priceChangeBrand = 0; | ||
| } | ||
| return priceChangeStock; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 | ||
|
|
@@ -65,6 +85,15 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let highPrice=[]; | ||
|
|
||
| stocks.forEach((stock,item) => { | ||
| highPrice.push( | ||
| `The highest price of ${stock.toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[item]).toFixed(2)}` | ||
| ); | ||
| }); | ||
| return highPrice; | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,10 +10,17 @@ | |
|
|
||
| function factorial(input) { | ||
| // TODO | ||
| if (input === 0 || input === 1) { | ||
| return 1; | ||
| } else { | ||
| for (let i = input - 1; i >= 1; i--) { | ||
|
Member
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 good, how could you rewrite this without a |
||
| input = input * i; | ||
| } | ||
| return input; | ||
| } | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| test("3! should be 6", () => { | ||
| expect(factorial(3)).toEqual(6); | ||
| }); | ||
|
|
@@ -25,3 +32,4 @@ test("5! should be 120", () => { | |
| test("10! should be 3628800", () => { | ||
| expect(factorial(10)).toEqual(3628800); | ||
| }); | ||
|
|
||
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.
How could you rewrite this without a
forloop? (Hint:.map()array method)