Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

London 9 - Mo nahvi - JS-Core-1 - Week 3 - #175

Open
monahvi wants to merge 2 commits into
CodeYourFuture:mainfrom
monahvi:main
Open

London 9 - Mo nahvi - JS-Core-1 - Week 3#175
monahvi wants to merge 2 commits into
CodeYourFuture:mainfrom
monahvi:main

Conversation

@monahvi

@monahvi monahvi commented Dec 7, 2022

Copy link
Copy Markdown

No description provided.

num += 2
n--
}
console.log(arr)

Copy link
Copy Markdown

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

for(const city of cities){
temperature.push(`The temperature in ${city} is ${temperatureService(city)} degrees`)
}
return temperature;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 map

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let random = generateRandomNumber() ;
let i = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need this i ?


function getHighestRatedInEachGenre(books) {
// TODO
const result = books.reduce((acc, cur) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 :

      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}

Comment thread 2-mandatory/4-stocks.js
for (let closingPricesForStock of closingPricesForAllStocks){
let sum =0;
for (let item of closingPricesForStock){
sum=sum+item;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

function headlinesWithNumbers(allArticleTitles) {
// TODO
let arr=[]
for (let article of allArticleTitles){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.
!!parseInt("a")

the double ! (bang) would return a boolean value

function titleWithFewestWords(allArticleTitles) {
// TODO
let arr = [];
for (let i = 0; i < allArticleTitles.length; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

function potentialHeadlines(allArticleTitles) {
// TODO
let arr =[];
for (let article of allArticleTitles){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could also use select

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants