London Class 7 - Hozan Ali - JavaScript-Core-2-Coursework-Week1#50
London Class 7 - Hozan Ali - JavaScript-Core-2-Coursework-Week1#50Hozan94 wants to merge 4 commits into
Conversation
|
|
||
| const { WriteStream } = require("fs"); | ||
|
|
||
| // We've created an array of objects for you here: |
There was a problem hiding this comment.
Not sure how line 22 got added. You don't need it.
| 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.`) | ||
| }); | ||
| /* |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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: | ||
|
|
| let groceriesListWithNoRepeatingItems = groceriesList.filter((item, index) => groceriesList.indexOf(item) === index); | ||
| groceriesListWithNoRepeatingItems.forEach(item => weeklyGroceriesToBuy.push(item)); | ||
| console.log(weeklyGroceriesToBuy); | ||
| /* |
There was a problem hiding this comment.
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
groceriesListWithNoRepeatingItemsintoweeklyGroceriesToBuy. Why not just assign directly the result toweeklyGroceriesToBuyin 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);
There was a problem hiding this comment.
@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.
| } | ||
| console.log(numberOfItemsPerWeek); | ||
|
|
||
|
|
There was a problem hiding this comment.
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); | ||
|
|
| let powerNetEmails = []; | ||
| // // Method 1 | ||
| let powerNetEmails = people.filter(friend => friend.company === "POWERNET").map(friend => friend.email).reverse(); | ||
|
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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(); | ||
| /* |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| } | ||
| } | ||
| } | ||
| /* |
There was a problem hiding this comment.
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.
- Get the friend list for each of your friends (
map) - For each list of friends (
forEach), return the list of people who can multi-task (filter) - Add their name to your array
friendsWhoCanMultitask(map)
There was a problem hiding this comment.
@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?
| **/ | ||
|
|
||
| let recipes = {}; | ||
| let recipes = { |
There was a problem hiding this comment.
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...
]
There was a problem hiding this comment.
@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
| Ingredients: ["tomato", "cucumber", "oil", "salt", "parsley", "cumin"] | ||
| } | ||
| }; | ||
|
|
There was a problem hiding this comment.
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}.`) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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.
| function getBudgets(peopleArray) { | ||
|
|
||
| for (let person of peopleArray) { | ||
| let sum = 0; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
@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) { |
There was a problem hiding this comment.
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.
|
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. |
|
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. |
Your Details
Homework Details