-
-
Notifications
You must be signed in to change notification settings - Fork 279
North-West-5/Shimen-Afshar/JavaScript-Core-1-Coursework-Week3 #145
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 |
|---|---|---|
|
|
@@ -11,6 +11,14 @@ function generateRandomNumber() { | |
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| let counter=0; | ||
| do{ | ||
| counter=generateRandomNumber(); | ||
| } | ||
| while(counter<50); | ||
|
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 do-while loop implementation :) remember to use |
||
| return counter; | ||
|
|
||
|
|
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,25 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| if(allArticleTitles.lengh !== 0){ | ||
| allArticleTitles = ARTICLE_TITLES.filter((el) => el.length <= 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. here you are using a mixture of allArticleTitles which is passed into the function and ARTICLE_TITLES (just use allArticlesTitles). Good use of the filter array method, but you don't need the if statement, for example if you do |
||
| return allArticleTitles; | ||
|
|
||
| } | ||
| else return allArticleTitles=[]; | ||
| } | ||
|
|
||
| /* | ||
| 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 = arr.sort((a, b) => a.length - b.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. good use of sort here, to sort an array without mutating the original array you can use the spread operator: |
||
| return newArr[0]; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,19 +32,40 @@ 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 arr = []; | ||
|
|
||
| for (let moreClick of allArticleTitles) { | ||
| let choosen = ""; | ||
| for (let letter of moreClick) { | ||
| if (!isNaN(letter) && letter !== " ") { | ||
| choosen = moreClick; | ||
| break; | ||
| } | ||
| } | ||
| if (choosen.length !== 0) { | ||
| arr.push(choosen); | ||
| } | ||
| } | ||
| return arr; | ||
|
|
||
|
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. regex is really useful for functions like this - this is a good site to learn more & have a play around https://regex101.com/ |
||
| } | ||
|
|
||
| /* | ||
| 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 total = 0; | ||
| let average = 0; | ||
| for (let articleTitle of allArticleTitles) { | ||
| total += articleTitle.length; | ||
| } | ||
| average = total / 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. really nice solution well done, you could just define average on line 63, for example |
||
| return Math.round(average); | ||
| // TODO | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* ======= List of Articles - DO NOT MODIFY ===== */ | ||
| const ARTICLE_TITLES = [ | ||
| "Streaming wars drive media groups to spend more than $100bn on new content", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let newAverage =[] | ||
| let sum = 0; | ||
| for (let i = 0; i < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.length; i++) { | ||
| for (let j = 0; j < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length; j++) { | ||
|
Comment on lines
+40
to
+41
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. good solution, well done on using multiple loops! |
||
| sum += CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i][j]; | ||
| } | ||
| newAverage.push( | ||
| (sum / CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length).toFixed(2) * | ||
| 1 | ||
| ); | ||
|
|
||
| sum = 0; | ||
| } | ||
| return newAverage; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +63,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 | ||
| // TODO | ||
| let newPriceChange = []; | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| newPriceChange.push((closingPricesForAllStocks[i][4] - closingPricesForAllStocks[i][0]).toFixed(2) * 1); | ||
| } | ||
| return newPriceChange; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -65,9 +85,19 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| highestPrice =[]; | ||
| for(let i=0; i < closingPricesForAllStocks.length; i++) { | ||
| let highestNum = closingPricesForAllStocks[i].sort(function(a,b){ | ||
| return b-a;} )[0]; | ||
| highestPrice.push( | ||
| "The highest price of " + stocks[i].toUpperCase() + " " + "in the last 5 days was " + highestNum.toFixed(2)); | ||
|
|
||
| } | ||
| return highestPrice; | ||
|
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 solution :) The |
||
| } | ||
|
|
||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| test("should return the average price for each stock", () => { | ||
| expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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 use of template literals! here you can use push, for example
report.push(The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees)