diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..90eb4795 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -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}.` ); diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..3e35d0c5 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,21 @@ let hogwarts = [ occupation: "Teacher", }, ]; + + +function gryffindorHouse(hogwarts) { + return hogwarts + .filter(({house}) => house === 'Gryffindor') + .map(({firstName, lastName}) => `${firstName} ${lastName}`) + .join("\n") +} + +function teachersWithPets(hogwarts) { + return hogwarts + .filter(({occupation, pet}) => occupation === "Teacher" && pet !== null) + .map(({firstName, lastName}) => `${firstName} ${lastName}`) + .join("\n") +} + +console.log(gryffindorHouse(hogwarts)); +console.log(teachersWithPets(hogwarts)) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..d5de1689 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,40 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + + +function receiptAndCosts(order) { + let total = 0; + + let receipt = [] + + /* This function orderLineFormatting requires 3 arguments in strict order + 1: quantity, 2: itemName, 3: subTotal + and will return a string + */ + function orderLineFormatting(...args) { + if (args.length !== 3) { + throw new Error("expecting 3 arguments in order of quantity, itemName, and subTotal") + } + const orderLineSpacing = [8, 20, 5] + let formattedOrderLine = '' + for (let index = 0; index < args.length; index++) { + formattedOrderLine += `${String(args[index]).padEnd(orderLineSpacing[index], " ")}` + } + return formattedOrderLine + } + + receipt.push(orderLineFormatting("QTY", "ITEM", "TOTAL")); + + order.forEach(({itemName, quantity, unitPricePence}) => { + const subTotal = (unitPricePence / 100 * quantity).toFixed(2) + receipt.push(orderLineFormatting(quantity, itemName, subTotal)) + total += unitPricePence / 100 * quantity + }) + + receipt.push(" ") + receipt.push(`Total: ${total.toFixed(2)}`) + return receipt.join("\n") +} + +console.log(receiptAndCosts(order)) \ No newline at end of file