Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const personOne = {

// Update the parameter to this function to make it work.
// Don't change anything else.
function introduceYourself(___________________________) {
function introduceYourself({name, age, favouriteFood}) {
console.log(
`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`
);
Expand Down
3 changes: 3 additions & 0 deletions Sprint-1/destructuring/exercise-1/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ console.log(`Batman is ${firstName}, ${lastName}`);
# Exercise

- What is the syntax to destructure the object `personOne` in exercise.js?
```js
let {name, age, favouriteFood} = personOne
```
- Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in.
26 changes: 26 additions & 0 deletions Sprint-1/destructuring/exercise-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,29 @@ let hogwarts = [
occupation: "Teacher",
},
];

//task 1
function getGryffindorMembers(arr) {
let gryffindorMembers = [];
arr.forEach(({ firstName, lastName, house }) => {

Choose a reason for hiding this comment

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

As an extension: could you show me how you can use filter and map on an array to do this?

Copy link
Author

Choose a reason for hiding this comment

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

Used filter and map for both functions in this exercise

if (house === "Gryffindor") {
gryffindorMembers.push(`${firstName} ${lastName}`);
}
});
console.log(gryffindorMembers.join('\n'));
}

getGryffindorMembers(hogwarts);

//task 2
function getTeachersWithPets(arr) {
let teachersWithPets = [];
arr.forEach(({ firstName, lastName, occupation, pet }) => {
if (occupation === "Teacher" && pet) {
teachersWithPets.push(`${firstName} ${lastName}`);
}
});
console.log(teachersWithPets.join('\n'));
}

getTeachersWithPets(hogwarts);
19 changes: 19 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,22 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

function printReceipt(order){
let total = 0 , qtyWidthMax = 3, itemWidthMax = 4

Choose a reason for hiding this comment

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

Just formatting: We usually don't add a comma after a number like you have with total = 0 ,

Copy link
Author

@i786m i786m Nov 16, 2025

Choose a reason for hiding this comment

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

Noted, have declared and assigned variables individually to aid in readability.

const receiptLines = order.map(({itemName, quantity, unitPricePence}) => {
const itemTotalPounds = (quantity*unitPricePence/100);

Choose a reason for hiding this comment

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

Generally, we try to keep space around an operator to make the code easier to read. Also, just to make the code easier to read, it is better to use brackets to show the order of operation that you want. For example, is it not clear whether you intend to have:

  • quantity * (unitPricePence/100) OR
  • (quantity * unitPricePence) / 100

In this instance, it does not actually matter, as mathematically they are equivalent. But it is a good habit to create to improve your code readability

Copy link
Author

Choose a reason for hiding this comment

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

Noted and amended

qtyWidthMax = Math.max(qtyWidthMax, String(quantity).length);
itemWidthMax = Math.max(itemWidthMax, itemName.length);
return { itemName, quantity, itemTotalPounds };
})
console.log('QTY'.padEnd(qtyWidthMax+4), 'ITEM'.padEnd(itemWidthMax+3), 'TOTAL');
receiptLines.forEach(({itemName, quantity, itemTotalPounds}) => {
total += itemTotalPounds;
console.log(String(quantity).padEnd(qtyWidthMax+4), itemName.padEnd(itemWidthMax+3), `${itemTotalPounds.toFixed(2)}`);
})
console.log(`\nTotal: ${total.toFixed(2)}`);

}

printReceipt(order);