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 @@ -6,10 +6,10 @@ 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}.`
);
}

introduceYourself(personOne);
introduceYourself(personOne);
19 changes: 19 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,22 @@ let hogwarts = [
occupation: "Teacher",
},
];

function getGryffindorMembers(arr) {
const gryffindorMembers = arr.filter(({ house }) => house === "Gryffindor").map(({ firstName, lastName }) => `${firstName} ${lastName}`).join('\n');
Copy link
Contributor

Choose a reason for hiding this comment

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

A long statement that chains multiple methods/functions becomes more readable when written across multiple lines, placing each method/function on its own line. For example,

  const gryffindorMembers = arr
    .filter(({ house }) => house === "Gryffindor")
    .map(({ firstName, lastName }) => `${firstName} ${lastName}`)
    .join('\n');


console.log(gryffindorMembers);
}

getGryffindorMembers(hogwarts);

function getTeachersWithPets(arr) {
const teachersWithPets = arr.filter(({ occupation, pet }) => occupation === "Teacher" && pet).map(({ firstName, lastName }) =>
`${firstName} ${lastName}`
).join('\n');


console.log(teachersWithPets);
}

getTeachersWithPets(hogwarts);
21 changes: 21 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,24 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

function Receipt(order){
let total = 0
let qtyWidth = 3
let itemWidth = 4
const receiptLines = order.map(({itemName, quantity, unitPricePence}) => {
const itemTotalPounds = (quantity * (unitPricePence / 100));
qtyWidth = Math.max(qtyWidth, String(quantity).length);
itemWidth = Math.max(itemWidth, itemName.length);
return { itemName, quantity, itemTotalPounds };
})
console.log('QTY'.padEnd(qtyWidth+4), 'ITEM'.padEnd(itemWidth+3), 'TOTAL');
receiptLines.forEach(({itemName, quantity, itemTotalPounds}) => {
total += itemTotalPounds;
console.log(String(quantity).padEnd(qtyWidth+4), itemName.padEnd(itemWidth+3), `${itemTotalPounds.toFixed(2)}`);
})
Comment on lines +14 to +24
Copy link
Contributor

@cjyuan cjyuan Nov 29, 2025

Choose a reason for hiding this comment

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

Here is an alternative approach to express the code:

  // Calculate reserved width first
  const qtyWidth = 4 + Math.max(3, ...order.map(({quantity}) => String(quantity).length));
  const itemWidth = 3 + Math.max(4, ...order.map({itemName}) => itemName.length));
  
  // Prepare a helper function to construct a formatted string 
  const formatStr  = (qty, name, total) => 
    `${qty.padEnd(qtyWidth)}${name.padEnd(itemWidth)}${total}`);
  
  console.log(formatStr('QTY', 'ITEM', 'TOTAL');

  let total = 0;
  order.forEach(({itemName, quantity, unitPricePence}) => {
    const subtotal = quantity * unitPricePence / 100; 
    total += subtotal;

    console.log(formatStr(quantity, itemName, subtotal.toFixed(2)));
  })

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

}

Receipt(order);
Loading