-
-
Notifications
You must be signed in to change notification settings - Fork 279
London 9 - Mo nahvi - JS-Core-1 - Week 3 #175
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,12 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let temperature=[]; | ||
| // for(let i = 0;i < cities.length; i++){ | ||
| for(const city of cities){ | ||
| temperature.push(`The temperature in ${city} is ${temperatureService(city)} degrees`) | ||
| } | ||
| return temperature; | ||
|
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. when you have an array that needs to be transformed, input and output have the same size, you can also use 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. |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,12 @@ function generateRandomNumber() { | |
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| let random = generateRandomNumber() ; | ||
| let i = 0; | ||
|
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. do you need this |
||
| do{ | ||
| random = generateRandomNumber() | ||
| }while ( random <= 50) | ||
| return random; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| 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. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let arr =[]; | ||
| for (let article 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. you could also use |
||
| if (article.length <= 65){ | ||
| arr.push(article) | ||
| } | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,7 +20,11 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| let arr = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
|
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 could use the same logic here, as in here and store only the shortest title |
||
| arr.push(allArticleTitles[i].split(" ").length); | ||
| } | ||
| return allArticleTitles[arr.indexOf(Math.min(...arr))]; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,15 +33,29 @@ 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 article 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. You could use |
||
| for (let char of article){ | ||
| if (char>="0" && char<="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. this is nice! Another way to check it, would be to see if any character can be converted into an integer. the double ! (bang) would return a boolean value |
||
| arr.push (article); | ||
| break; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| return arr | ||
| } | ||
|
|
||
| /* | ||
| 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 (let article of allArticleTitles){ | ||
| sum+=article.length; | ||
| } | ||
| return Math.round(sum/allArticleTitles.length) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,16 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let averageArr=[]; | ||
| for (let closingPricesForStock of closingPricesForAllStocks){ | ||
| let sum =0; | ||
| for (let item of closingPricesForStock){ | ||
| sum=sum+item; | ||
|
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 could be a great opportunity to use a reduce function. where your accumulator is the sum |
||
| } | ||
| averageArr.push(Number((sum/closingPricesForStock.length).toFixed(2))); | ||
|
|
||
| } | ||
| return averageArr; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +57,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 | ||
| let arr=[]; | ||
| for (let closingPricesForStock of closingPricesForAllStocks){ | ||
| arr.push(Number((closingPricesForStock[closingPricesForStock.length-1]-closingPricesForStock[0]).toFixed(2))); | ||
|
|
||
| } | ||
| return arr; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +78,11 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let arr=[]; | ||
| for (let i=0;i<closingPricesForAllStocks.length;i++){ | ||
| arr.push (`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max(...closingPricesForAllStocks[i])).toFixed(2)}`) | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,22 @@ | |
| */ | ||
|
|
||
| function getHighestRatedInEachGenre(books) { | ||
| // TODO | ||
| const result = books.reduce((acc, cur) => { | ||
|
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 think this could be simplified.You did a really good job with : basically your accumulator is an empty object in the beginning, and you are registering the items grouping them by genre. But we may save some time if, instead of saving an array and then sorting it, we save only the highest value. then our acc will look something like: acc={children: 10, fiction: 8, cooking: 9} |
||
| const groupByGenre = cur.genre; | ||
| if (!acc[groupByGenre]) { | ||
| acc[groupByGenre] = []; | ||
| } | ||
| acc[groupByGenre].push(cur); | ||
| return acc; | ||
| }, {}); | ||
| const genreName = Object.keys(result); | ||
| const arr = []; | ||
| for (let i = 0; i < genreName.length; i++) { | ||
| arr.push( | ||
| result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title | ||
| ); | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
|
|
||
|
|
||
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! I like this solution. Usually the standard way to look at even/odd number is using %2, but this is really nice too