diff --git a/week-3/Homework/mandatory/1-practice/2-code-reading.md b/week-3/Homework/mandatory/1-practice/2-code-reading.md index 295964e..e9a4a19 100644 --- a/week-3/Homework/mandatory/1-practice/2-code-reading.md +++ b/week-3/Homework/mandatory/1-practice/2-code-reading.md @@ -15,6 +15,10 @@ Take a look at the following code: Explain why line 4 and line 6 output different numbers. +## Answer 1 + +The console log on line 4 refers to the block scoped function declared on line 3, whereas the console log on line 6, refers to the globally scoped variable declared on line 1; + ## Question 2 Take a look at the following code: @@ -33,6 +37,9 @@ console.log(y) ``` What will be the output of this code. Explain your answer in 50 words or less. +## Answer 2 + +The first console log to be displayed will be from line 31, output is from the globally scoped x variable: 10. The 2nd console log attempts to display the return value of f1(): undefined. The 3rd console log causes an error as the y variable is out of scope. ## Question 3 @@ -60,4 +67,8 @@ f2(y); console.log(y); ``` -What will be the output of this code. Explain your answer in 50 words or less. +What will be the output of this code. Explain your answer in 50 words or less + +## Answer3 + +The 1st log displays the global variable x: outputs 9. f1() takes a copy of the globally scoped x, therefore x is not affected by f1(). The 2nd logs the structure of object y, showing that the object's property changes from 9 to 10 due to f2() receiving a reference to y. \ No newline at end of file diff --git a/week-3/Homework/mandatory/2-exercises/exercise-1/exercise-1.js b/week-3/Homework/mandatory/2-exercises/exercise-1/exercise-1.js index 10b93ba..48b9dc6 100644 --- a/week-3/Homework/mandatory/2-exercises/exercise-1/exercise-1.js +++ b/week-3/Homework/mandatory/2-exercises/exercise-1/exercise-1.js @@ -4,7 +4,7 @@ const personOne = { favouriteFood: 'Spinach' } -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/week-3/Homework/mandatory/2-exercises/exercise-2/exercise-2.js b/week-3/Homework/mandatory/2-exercises/exercise-2/exercise-2.js index 0d3ade0..0f20a38 100644 --- a/week-3/Homework/mandatory/2-exercises/exercise-2/exercise-2.js +++ b/week-3/Homework/mandatory/2-exercises/exercise-2/exercise-2.js @@ -9,3 +9,20 @@ let hogwarts = [ { firstName: "Minerva", lastName: "McGonagall", house: "Gryffindor", pet: null, occupation: "Teacher" }, { firstName: "Albus", lastName: "Dumbledore", house: "Gryffindor", pet: "Phoenix", occupation: "Teacher" } ] + + +const displayName = (people) => { + console.log("List of people in Gryffindor house: "); + people.forEach( ({ firstName, lastName, house }) =>house == "Gryffindor" && console.log(`${firstName} ${lastName}`) ); +}; + +const isPetOwner = (people) => { + console.log("List of Teachers with pets: "); + people.forEach( ( { firstName, lastName, occupation, pet} ) => { + if(occupation === "Teacher" && pet !== null){ + console.log(`${firstName} ${lastName}`); + } + }); +} +displayName(hogwarts); +isPetOwner(hogwarts); diff --git a/week-3/Homework/mandatory/2-exercises/exercise-3/exercise-3.js b/week-3/Homework/mandatory/2-exercises/exercise-3/exercise-3.js index b60d527..ddfd82e 100644 --- a/week-3/Homework/mandatory/2-exercises/exercise-3/exercise-3.js +++ b/week-3/Homework/mandatory/2-exercises/exercise-3/exercise-3.js @@ -6,4 +6,20 @@ { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.00}, { itemName: "Hash Brown", quantity: 4, unitPrice: 0.40} ] - \ No newline at end of file + + const receipt = order => { + let totalCost = 0; + console.log("QTY ITEM TOTAL"); + order.forEach( ( {itemName, quantity, unitPrice} ) => { + let total = (quantity * unitPrice ).toFixed(2) ; + totalCost += quantity * unitPrice; + let padding = "".padStart(17 - itemName.length," "); + //console.log( str) + //let total = (quantity * unitPrice).toFixed(2).padStart(strlen(itemName), " "); + + console.log(`${quantity} ${itemName}${padding}${total}`); + }) + console.log(`Total: ${totalCost.toFixed(2)}`); + }; + + receipt(order); \ No newline at end of file