diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..d86bc7bf 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-1/readme.md b/Sprint-1/destructuring/exercise-1/readme.md index 28ca6c3d..4f9bd366 100644 --- a/Sprint-1/destructuring/exercise-1/readme.md +++ b/Sprint-1/destructuring/exercise-1/readme.md @@ -30,4 +30,17 @@ console.log(`Batman is ${firstName}, ${lastName}`); # Exercise - What is the syntax to destructure the object `personOne` in exercise.js? + const personOne = { + name: "Popeye", + age: 34, + favouriteFood: "Spinach", + }; + +let { name, age, favouriteFood } = personOne + - Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in. + unction 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..91d51367 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,44 @@ let hogwarts = [ occupation: "Teacher", }, ]; + + +//1 +// const newItem = oldItem.filter(({ propWeWant }) => propWeWant === 'propValue') + +const sortingHat = (hogwarts) => { + // filter gryffindor + const filtered = hogwarts.filter(({ house }) => house === "Gryffindor"); + + // return names + let gryffindor = []; + filtered.forEach(({ firstName, lastName }) => { + gryffindor.push(`${firstName} ${lastName}`); + }) + + return gryffindor; +} + +//2 +// const newItem = oldItem.filter(({ propWeWant1, propWeWant2 }) => propWeWant 1 === 'propValue' && propWeWant2 === 'propValue2') +// or if checking if prop exists +// const newItem = oldItem.filter(({ propWeWant1, propWeWant2 }) => propWeWant 1 === 'propValue' && propWeWant2) +const findTeachersWithPets = (hogwarts) => { + let teachersWithPets = []; + + // filter teachers + // const filtered = hogwarts.filter(({ occupation, pet }) => occupation === 'Teacher'); + + // check for pets + // const withPets = filtered.filter(({ pet }) => pet)); + +//filter for teachers with pets + const filtered = hogwarts.filter(({ occupation, pet }) => occupation === 'Teacher' && pet); + + + filtered.forEach(({ firstName, lastName }) => { + teachersWithPets.push(`${firstName} ${lastName}`); + }) + + return teachersWithPets; +} \ No newline at end of file diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..2dfcb8f6 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,57 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +// const newItem = oldItem.filter(({ propWeWant1, propWeWant2 }) => propWeWant 1 === 'propValue' && propWeWant2 === 'propValue2') + + + +// - In `exercise.js`, you have been provided with a takeout order. Write a program +// that will print out the receipt for this order. + +//helpers +//cost of item in order +const calculateCostPerItemimPounds = ({ quantity, unitPricePence }) => { + let costPerItem = (quantity * unitPricePence) + + //in pounds + costPerItem = costPerItem / 100; + return costPerItem +} + +//add cost of item to the order +const addTotalProp = (order) => { + order.forEach(item => { + const costPerOrder = calculateCostPerItemimPounds(item) + //add total cost + item.costPerOrder = costPerOrder; + }) + } + +//calculate total of order +const calculateOrderTotal = (order) => { + let totalCost = 0; + + order.forEach(item => { + //add total cost + totalCost += item.costPerOrder; + }) + return totalCost; +} + +const createReceipt = (order) => { + addTotalProp(order); + // / - Log each individual item to the console. + order.forEach(({ itemName, quantity, costPerOrder }) => { + console.table([{ itemName, quantity, costPerOrder }]); + }); + + // - Log the total cost of the order to the console. + //getting an error when trying console.table + console.log(`Total: £${calculateOrderTotal(order).toFixed(2)}`); + +} + +createReceipt(order); + +//I have the receipt in a table format it is not identical to the one in the example but it is a table format :) \ No newline at end of file