Skip to content
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
17 changes: 17 additions & 0 deletions Sprint-1/destructuring/exercise-2/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";
let hogwarts = [
{
firstName: "Harry",
Expand Down Expand Up @@ -70,3 +71,19 @@ let hogwarts = [
occupation: "Teacher",
},
];

function gryffindorHouse(hogwart) {
for (const { firstName, lastName, house } of hogwart) {
if (house === "Gryffindor") console.log(firstName, lastName);
}
}

gryffindorHouse(hogwarts);

function findPet(hogwart) {
for (const { firstName, lastName, pet, occupation } of hogwart) {
if (pet && occupation === "Teacher")
console.log(`name of a teacher who has pet ${firstName} ${lastName}`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Compare your output with the expected result in the task. What would the output look like if you ran this code? Does it match line for line?

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.

i will return it to the original state only did that to differentiate it from the rest of the out out. it was confusing.

}
}
findPet(hogwarts);
16 changes: 16 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Comment thread
MaharitB marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

function receipt(orders) {
let total = 0;
console.log(`${"QTY"} ${"ITEM"} ${"Total"}`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • Compare your header with the expected one, character by character: capitalisation of the last word, and the number of spaces between columns.
  • Does the ITEM column in the header start at the same position as the item names in your rows? Print it and check by eye.
  • ${"QTY"}, ${"ITEM"} and ${"Total"} are all string literals being interpolated. Interpolation is for inserting values that vary (variables, expressions). What do these three give you over writing the text directly?
    If the goal is to control column widths, is there a method you're already using in the rows that could build the header too?


for ({ itemName, quantity, unitPricePence } of orders) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These variables aren't declared with anything. What would happen if this code ran in strict mode or inside an ES module?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You're yet to fix this

total += quantity * unitPricePence;

console.log(
`${quantity.toString().padEnd(4)} ${itemName.toString().padEnd(21)}${((quantity * unitPricePence) / 100).toFixed(2)}`

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The header spacing and the row spacing are set in two different ways. If you changed a column width, how many places would you have to update? Can the header and rows share the same widths?

itemName.toString()
itemName is already a string here, so is this call doing anything?

);
}

console.log(`\nTotal:${total.toFixed(2)}`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Run the program and look at the number printed after Total:. Compare it with the expected 14.50. What unit is total in, and what unit is toFixed(2) assuming? Also compare the text between Total: and the number with the expected output.

}
receipt(order);
Loading