Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const personOne = {
age: 34,
favouriteFood: "Spinach",
};

let { name, age, favouriteFood } = personOne;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This would introduce the variables in the "global" scope (scope outside the function).

We could de-structure the parameters too.

// Update the parameter to this function to make it work.
// Don't change anything else.
function introduceYourself(___________________________) {
function introduceYourself(personOne) {
console.log(
`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`
);
Expand Down
23 changes: 23 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,26 @@ let hogwarts = [
occupation: "Teacher",
},
];
function houseGryffindor(hogwarts) {
for (let individualHogwarts of hogwarts) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Normally, we can use const to declare the loop variable in a for-of or for-in loop because we usually don't need to reassign value to the variable.

  • We could also use de-structure syntax in a for-of loop as for (const { key1, key2, ... } of objects) { ... } where key1, key2, ... are the property names.

if (individualHogwarts.house === "Gryffindor") {
let { firstName, lastName } = individualHogwarts;
console.log(`${firstName} ${lastName}`);
}
}
}
console.log("`````");
houseGryffindor(hogwarts);
console.log("`````");

function teacherWithPets(hogwarts) {
for (let singlePerson of hogwarts) {
if (singlePerson.occupation === "Teacher" && singlePerson.pet !== null) {
let { firstName, lastName } = singlePerson;
console.log(`${firstName} ${lastName}`);
}
}
}
console.log("`````");
teacherWithPets(hogwarts);
console.log("`````");
23 changes: 23 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,26 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];
console.log("QTY".padEnd(5) + "ITEM".padEnd(22) + "TOTAL");

let grandTotal = 0;

function valueProperty(order) {
for (let singleOrder of order) {
let { itemName, quantity, unitPricePence } = singleOrder;

let itemTotal = quantity * unitPricePence;
grandTotal += itemTotal;

console.log(
String(quantity).padEnd(5) +
itemName.padEnd(22) +
(itemTotal / 100).toFixed(2)
);
}

console.log("---------------------------");
console.log("Total: " + (grandTotal / 100).toFixed(2));
}

valueProperty(order);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you call this function multiple times in a row (even with the same argument), you may discover a bug in your code.

Loading