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

London Class 7 - Hozan Ali - JavaScript-Core-2-Coursework-Week1#50

Closed
Hozan94 wants to merge 4 commits into
CodeYourFuture:mainfrom
Hozan94:main
Closed

London Class 7 - Hozan Ali - JavaScript-Core-2-Coursework-Week1#50
Hozan94 wants to merge 4 commits into
CodeYourFuture:mainfrom
Hozan94:main

Conversation

@Hozan94
Copy link
Copy Markdown

@Hozan94 Hozan94 commented Feb 21, 2021

Your Details

  • Your Name: Hozan Ali
  • Your City: London
  • Your Slack Name: Hozan Ali

Homework Details

  • Module: JavaScript-Core-2-Coursework
  • Week: Week 1

Comment thread mandatory/1-writers.js

const { WriteStream } = require("fs");

// We've created an array of objects for you here:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not sure how line 22 got added. You don't need it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done 👍

Comment thread mandatory/1-writers.js
writers.filter(writer => writer.age >= 40 && writer.age <= 49 && writer.alive == false).forEach(writer => {
console.log(`Writer ${writer.firstName} ${writer.lastName} died at ${writer.age} years old.`)
});
/*
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great use of the method chaining with the filter and forEach functions for exercise 2 and 3! 👏

},
pour: function () {
// calling this function should increase your bottle volume by 10 units;
if (this.volume < 100) {
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 works! Another way you could have done the condition is to just check if the bottle was full already

if (!this.isFull()) {
// do stuff here
}

Similar for the drink function, you could check if it was empty before drinking.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@louisechow That is really interesting to know , that you can have a method as a condition and that they are also hoisted

Extra question:
Why do you think it is preferred to use `this` inside the object rather than its variable name, in our case `bottle`?
Leave your answer below:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good answer!

Comment thread mandatory/3-groceries.js
let groceriesListWithNoRepeatingItems = groceriesList.filter((item, index) => groceriesList.indexOf(item) === index);
groceriesListWithNoRepeatingItems.forEach(item => weeklyGroceriesToBuy.push(item));
console.log(weeklyGroceriesToBuy);
/*
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 a really interesting way you've solved the problem, particularly line 33. The question did ask you to do it using looping, but you can maybe try that method another time as a challenge.

Two things to point out with your solution:

  • You have an empty item in your result, you should try to filter that out too.
[
  'Cheese',    'Eggs',         'Tomato',
  'Paprika',   'Leek',         'Wrap',
  'Tuna',      'Canned beans', 'Carrot',
  'Aubergine', 'Orange Juice', 'Apple',
  'Ananas',    'Black tea',    'Lamb',
  'Salt',      'Bulgur',       'Potato',
  'Rice milk', 'Blueberries',  'Porridge',
  'Banana',    'Cinnamon',     'Olive oil',
  'Salmon',    'Asparagus',    ''
]
  • Line 35 isn't really doing anything for you other than putting what's in groceriesListWithNoRepeatingItems into weeklyGroceriesToBuy. Why not just assign directly the result to weeklyGroceriesToBuy in line 34. You could leave a comment above the line if you wanted to explain that you are removing duplicates.
    E.g.
// Filter out duplicate items
weeklyGroceriesToBuy = groceriesList.filter((item, index) => groceriesList.indexOf(item) === index);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@louisechow regarding your second point, you are right that is alot more readable and shorter. And for the first point I will look into that as well.

Comment thread mandatory/3-groceries.js
}
console.log(numberOfItemsPerWeek);


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Some extra code here that isn't related to the exercise and should be removed 🤔


let thirtyFiveOrOlder = [];
let thirtyFiveOrOlder = people.filter(friend => friend.age >= 35);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

let powerNetEmails = [];
// // Method 1
let powerNetEmails = people.filter(friend => friend.company === "POWERNET").map(friend => friend.email).reverse();

Copy link
Copy Markdown

@louisechow louisechow Feb 21, 2021

Choose a reason for hiding this comment

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

This answer is fine. I think you should pick the method you think is the best Hozan and just submit that, no need to list every possible method you could use.

Why did you reverse your array though? (Why are all the trainees reversing their arrays 🤔 )

Edit ohhhh I see, because the test check for a certain order. Ok nevermind then, i'll get them to fix the test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@louisechow I just wanted to experiment different methods, I know it makes the code harder to read, but If I have them somewehre else I will forget I had them, that's why I write them here. sorry for the inconvenience.

}
}
friendsWithStacie.reverse();
/*
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 works!

You could also do this by making use of the filter function to check whether someone is a friend with Stacie.

people
  .filter(function isFriendsWithStacie(person) {
    // Check if someone is a friend with Stacie
  })

What line would you put in the function to check if Stacie is in the list of that person's friends? Hint: Do you know any array methods can can be used to find a particular element in an array?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@louisechow

let friendsWithStacie = people.filter(function isFriendsWithStacie(person) {
  return person.friends.some(personDetail => personDetail.name.includes("Stacie Villarreal"))
}).map(friend => `${friend.name.first} ${friend.name.last}`)

I know I had to use the "includes" method, but could not figure out how to access the elements in "friends" arrays that are objects, so finally came up with some() method, Now I figured it out, does that mean the includes method here works more as the string method rather than an array method, because personDetail.name is an object property and not an array element. please correct me If I am wrong.

}
}
}
/*
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 works!

If you'd like a challenge, another way to do this would be to use array methods you have learnt before. Try for yourself.

  1. Get the friend list for each of your friends (map)
  2. For each list of friends (forEach), return the list of people who can multi-task (filter)
  3. Add their name to your array friendsWhoCanMultitask (map)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@louisechow I tried step 1 and 2 so far but I get "undefined"

people.map(personFriends => personFriends.friends).forEach(friendList => friendList.filter(person => person.skills.includes("Multi-tasking")))

Does not forEach always returns undefined?

Comment thread mandatory/5-recipes.js
**/

let recipes = {};
let recipes = {
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 would recommend you use an Array to store a list rather than an object.

let recipes = [
{
title: "Dough",
serves: 4,
ingredients: ["flour", "water", "sugar", "salt"]
},
etc...
]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@louisechow I followed what I was asked here in question by saying to create an object, unless they meant the elements of an array to be objects. If that is the case sorry for misunderstanding

Comment thread mandatory/5-recipes.js
Ingredients: ["tomato", "cucumber", "oil", "salt", "parsley", "cumin"]
}
};

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 fine given you defined it as an object rather than an array.

This is why I suggest you always use an array for a list. If you defined it as an array then you could more easily iterate over it just using for...of...

});

books.forEach(book => {
console.log(book.title === 1984 ? `You've already read ${book.title} by ${book.author}` : `You still need to read ${book.title} by ${book.author}.`)
Copy link
Copy Markdown

@louisechow louisechow Feb 21, 2021

Choose a reason for hiding this comment

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

It would have been easier for yourself if you added a property on your object that indicated whether you had read it or not. Then you could check the property as you looped through.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@louisechow I was actually tempted to add a property with a boolean value, but then decided to go with this method as they question did not specify what to use.

Comment thread mandatory/7-budgets.js Outdated
function getBudgets(peopleArray) {

for (let person of peopleArray) {
let sum = 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.

Careful here, you are resetting the sum to be 0 each time you loop. I don't think that's what you want. Where should you move line 22 to?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@louisechow you are right I had it by mistake inside the loop, It should be before the loop on line 20


function chooseMeal(mealArray) {
// Write your code here
if (mealArray.length >= 2) {
Copy link
Copy Markdown

@louisechow louisechow Feb 21, 2021

Choose a reason for hiding this comment

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

There's a way for you to consolidate lines 40 - 44.
You don't need to create the new array for just the prices. You can use the dot notation to access the price from meal object when you are comparing them.

mealArray.sort((mealA, mealB) => mealA.price - mealB.price);

Now you have a sorted array of meals and you can just use indexes to pick out the cheapest, second cheapest etc.

@louisechow
Copy link
Copy Markdown

Great job with the homework this week Hozan! 👏

There were a few questions where you answered them in very creative ways. If you have time to practise, I'd recommend going back and try to solve them with some of the suggestions I made.

Let me know if you need me to explain anything.

@louisechow louisechow added the reviewed A mentor has reviewed this code label Feb 21, 2021
@github-actions
Copy link
Copy Markdown

Your coursework submission has been closed because nobody has interacted with it in six weeks. You are welcome to re-open it to get more feedback.

@github-actions github-actions Bot added the Stale label May 14, 2021
@github-actions github-actions Bot closed this May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

reviewed A mentor has reviewed this code Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants