-
-
Notifications
You must be signed in to change notification settings - Fork 83
London10 | Afsha_Hossain | Array_ Destructuring | JS3 | Week 3 #184
base: main
Are you sure you want to change the base?
Conversation
@@ -4,7 +4,8 @@ const personOne = { | |||
favouriteFood: "Spinach", | |||
}; | |||
|
|||
function introduceYourself(___________________________) { | |||
|
|||
function introduceYourself({ name, age, favouriteFood } = personOne) { |
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.
Do you need to set a default for the object that is passed in?
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.
Thank you for pointing it out, Jack! Just refactored it.
let gryffindorStudentNames = hogwarts.filter(({house}) => { | ||
return house === "Gryffindor"; | ||
}); | ||
for (const student of gryffindorStudentNames) { | ||
let { firstName, lastName } = student; | ||
console.log(firstName, lastName); | ||
} | ||
|
||
let teachersWithPets = hogwarts.filter(({occupation, pet}) => { | ||
return (occupation === "Teacher") && (pet !== null); | ||
}); | ||
for (const teacher of teachersWithPets) { | ||
let { firstName, lastName } = teacher; | ||
console.log(firstName, lastName); | ||
} |
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.
Well done finding a valid solution for both problems, a few notes to improve these solutions.
You are using the filter method and assigning the results to a new variable. While this is an acceptable approach, you have used the let declaration when this variable is not reassigned at any stage, so const would have been the correct declaration to use.
Also, you are using two loops for each solution, when it can be simplified to use 1. This can be achieved using the Array.forEach method or just a for loop which you already have. Then using a condition to check the object values before logging the character name.
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.
Hey Jack!
Thank you so much for your valuable feedback!
I will make the changes now.
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.
I made the changes according to you second suggestion.
console.log("QTY ITEM TOTAL"); | ||
let totalPrice = 0; | ||
order.forEach(({quantity, itemName, unitPrice}) => { | ||
const itemPrice = `${unitPrice * quantity}`; | ||
console.log(`${quantity}\t${itemName}\t\t\t\t${itemPrice}`); | ||
totalPrice = totalPrice + unitPrice * quantity; | ||
}) | ||
console.log(`Total: ${totalPrice}`); | ||
|
||
|
||
// Question: Why doesn't it work when I do totalPrice = totalPrice + itemPrice in line 16 |
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.
To answer your question about why doing totalPrice = totalPrice + itemPrice;
does not work, it is because on line 14 you are defining itemPrice
as a string by using template literals.
If you were to remove the template literals and do const itemPrice = unitPrice * quantity;
, then it would work.
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.
Also, an improvement you could make to this solution would be to use Array.reduce()
to create a running aggregate instead of defining totalPrice
outside of the loop.
Here is a link that explains using reduce to calculate a sum.
https://linuxhint.com/call-reduce-on-an-array-of-objects-to-sum-their-properties/
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.
Thank you very much for noticing my question and answering it! Thank you for the helpful article! I have made some changes.
Refactored the code by using the for of loop I had and using a condition to check the object values before logging the fantasy characters' names
Cleared some confusion and refactored a bit
…e to a variable called totalPrice and putting the variable to the console. Refactored after some great suggestions made by the reviewer. Refactored by using .reduce method to create a running aggregate instead of defining totalPrice outside of the loop, and by assigning the returned value to a variable called totalPrice and putting the variable to the console.
Kudos, SonarCloud Quality Gate passed!
|
Learners, PR Template
Self checklist
Changelist
Briefly explain your PR.
Questions
Ask any questions you have for your reviewer.