-
-
Notifications
You must be signed in to change notification settings - Fork 282
finishing all mandatory #261
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 |
|---|---|---|
|
|
@@ -5,16 +5,43 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| } | ||
| const newarray = []; | ||
|
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 is the purpose of the const 'newarray'? |
||
|
|
||
| for (const item of allArticleTitles) { | ||
|
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 ! |
||
| if (item.length <= 65) { | ||
| newarray.push(item); | ||
| } | ||
| } | ||
| 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 fewestWord=ARTICLE_TITLES[0].split(' ').length; | ||
| let finalTitle=ARTICLE_TITLES[0]; | ||
| let i=1; | ||
|
|
||
| while(i<ARTICLE_TITLES.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. In this while loop, you are using 'i' as an iterator to go on loop until it reaches the condition i < ARTICLE_TITLES.length. Is there another way to improve this loop so it's more clear what it's doing?
Author
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. while(i=1, i<ARTICLE_TITLES.length, i++) |
||
| let result=ARTICLE_TITLES[i].split(' ').length; | ||
| let title=ARTICLE_TITLES[i]; | ||
|
|
||
| if(result<fewestWord){ | ||
| fewestWord=result; | ||
| finalTitle=title; | ||
| } | ||
|
|
||
| i=i+1; | ||
|
|
||
| } | ||
| return finalTitle; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,15 +50,45 @@ function titleWithFewestWords(allArticleTitles) { | |
| (Hint: remember that you can also loop through the characters of a string if you need to) | ||
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| const newarray=[]; | ||
| const number=/[0-9]/; | ||
|
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. I see array like this for the first time, can you explain what it does ? I'm just curious 🙂
Author
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 means that it is(number) integer and it is not a letter or sth else |
||
| let i=0; | ||
| while (i < 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. Same as the previous comment from the while loop |
||
| if (allArticleTitles[i].match(number)) { | ||
| newarray.push(allArticleTitles[i]); | ||
| } | ||
| i=i+1; | ||
| } | ||
| return newarray; | ||
| } | ||
|
|
||
|
|
||
|
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. Just as a nice to have: try to avoid committing unnecessary empty lines like this so we can keep the Pull request cleaner and avoid extra changes
Author
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 are right, thanks for your advice |
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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 sum=0; | ||
| for (const item of allArticleTitles) { | ||
| sum = (item.length)+sum; | ||
|
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. why is item.length to be inside the () ?
Author
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. sum += item.length |
||
|
|
||
| } | ||
| return Math.round(sum / allArticleTitles.length); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let averageprice=[]; | ||
|
|
||
| for(const stocksline of closingPricesForAllStocks){ | ||
| let sum = 0; | ||
| let average = 0; | ||
| for(const item of stocksline){ | ||
| sum = sum + item; | ||
|
|
||
| } | ||
| average = sum / stocksline.length; | ||
| averageprice.push(Math.round(average * 100) / 100); | ||
|
|
||
| } | ||
| return averageprice; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +62,13 @@ 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 = []; | ||
| for(const stocksline of closingPricesForAllStocks){ | ||
| let sub = 0; | ||
| sub = stocksline.slice(-1) - stocksline[0]; | ||
| priceChange.push(Math.round(sub*100)/100); | ||
| } | ||
| return priceChange; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,10 +84,32 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| const result = []; | ||
| let num = 0; | ||
| let Name; | ||
| for(const stocksline of closingPricesForAllStocks){ | ||
|
|
||
|
|
||
| let FirstPrice = stocksline[0]; | ||
| let i = 1; | ||
| while(i<stocksline.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. Same comment for the while loop as before
Author
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. while(i=1, i<stocksline.length, i++){ } |
||
| let price = stocksline[i]; | ||
| if(price>FirstPrice){ | ||
| FirstPrice = price; | ||
| } | ||
| i=i+1; | ||
| } | ||
|
|
||
| Name = stocks[num].toUpperCase(); | ||
| result.push(`The highest price of ${Name} in the last 5 days was ${FirstPrice.toFixed(2)}`); | ||
| num = num + 1; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| test("should return the average price for each stock", () => { | ||
| expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( | ||
|
|
@@ -92,3 +134,9 @@ test("should return a description of the highest price for each stock", () => { | |
| ] | ||
| ); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
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.
Will be nice to see there String Interpolation👌
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 agree with that 😄 You can read more about template literals in JavaScript here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals