London 9 - Mo nahvi - JS-Core-1 - Week 3 - #175
Conversation
| num += 2 | ||
| n-- | ||
| } | ||
| console.log(arr) |
There was a problem hiding this comment.
nice! I like this solution. Usually the standard way to look at even/odd number is using %2, but this is really nice too
| for(const city of cities){ | ||
| temperature.push(`The temperature in ${city} is ${temperatureService(city)} degrees`) | ||
| } | ||
| return temperature; |
There was a problem hiding this comment.
when you have an array that needs to be transformed, input and output have the same size, you can also use map
There was a problem hiding this comment.
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| let random = generateRandomNumber() ; | ||
| let i = 0; |
|
|
||
| function getHighestRatedInEachGenre(books) { | ||
| // TODO | ||
| const result = books.reduce((acc, cur) => { |
There was a problem hiding this comment.
I think this could be simplified.You did a really good job with :
if (!acc[groupByGenre]) {
acc[groupByGenre] = [];
}
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.
How do we know if a value is the highest?
Well, we don't know in the beginning, so we can push it the first rating in the first loop, and then on each cycle, we check if that value is higher than our previous value, and if so we override it!
if (cur.rating > acc[groupByGenre]){
acc[groupByGenre] = cur.rating
}
then our acc will look something like: acc={children: 10, fiction: 8, cooking: 9}
| for (let closingPricesForStock of closingPricesForAllStocks){ | ||
| let sum =0; | ||
| for (let item of closingPricesForStock){ | ||
| sum=sum+item; |
There was a problem hiding this comment.
this could be a great opportunity to use a reduce function. where your accumulator is the sum
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| let arr=[] | ||
| for (let article of allArticleTitles){ |
There was a problem hiding this comment.
You could use select function here, passing the condition to filter in your array
| let arr=[] | ||
| for (let article of allArticleTitles){ | ||
| for (let char of article){ | ||
| if (char>="0" && char<="9"){ |
There was a problem hiding this comment.
this is nice! Another way to check it, would be to see if any character can be converted into an integer.
!!parseInt("a")
the double ! (bang) would return a boolean value
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| let arr = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { |
There was a problem hiding this comment.
you could use the same logic here, as in here and store only the shortest title
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let arr =[]; | ||
| for (let article of allArticleTitles){ |
No description provided.