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..32b18d6 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,8 @@ Take a look at the following code: Explain why line 4 and line 6 output different numbers. +- They belong to different scopes. Also `x` on line 3 will 'over shadow' the `x` from global scope as they have the same name. + ## Question 2 Take a look at the following code: @@ -34,6 +36,9 @@ console.log(y) What will be the output of this code. Explain your answer in 50 words or less. +- First console.log will print `10` as it can access the outer scope. +- Second console.log will throw a reference error as `y` is not accessible from the global scope. + ## Question 3 Take a look at the following code: @@ -61,3 +66,6 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. + +- First console.log will will print `9` as we are not modifying it by just calling `f1` function and this is a constant variable which would throw an error if we would try to do so. +- Second console.log will print `{ x: 10}` because objects are of reference type. `f2` will modify the property of referenced object. 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-1/exercise-1.md b/week-3/Homework/mandatory/2-exercises/exercise-1/exercise-1.md index 9c3ac97..63c1656 100644 --- a/week-3/Homework/mandatory/2-exercises/exercise-1/exercise-1.md +++ b/week-3/Homework/mandatory/2-exercises/exercise-1/exercise-1.md @@ -18,4 +18,5 @@ The program above will print `Batman is Bruce Wayne`. Notice how we use the `{}` # Exercise - What is the syntax to destructure the object `personOne` in exercise-1.js? + - `{}` - Update the argument of the function `introduceYourself` to use destructuring on the object that gets passed in. 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..c89a2ea 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,17 @@ let hogwarts = [ { firstName: "Minerva", lastName: "McGonagall", house: "Gryffindor", pet: null, occupation: "Teacher" }, { firstName: "Albus", lastName: "Dumbledore", house: "Gryffindor", pet: "Phoenix", occupation: "Teacher" } ] + +// task 1 +hogwarts.forEach(({ firstName, lastName, house }) => { + if (house === "Gryffindor") { + console.log(firstName, lastName); + } +}) + +// task 2 +hogwarts.forEach(({ firstName, lastName, pet, occupation }) => { + if (occupation === "Teacher" && pet) { + console.log(firstName, lastName); + } +}) \ No newline at end of file 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..520adf9 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,23 @@ { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.00}, { itemName: "Hash Brown", quantity: 4, unitPrice: 0.40} ] - \ No newline at end of file + + +const total = order.reduce((total, { quantity, unitPrice }) => { + total += quantity * unitPrice; + return total; +}, 0); + +const qtyPad = 7; +const itemPad = 20; + +console.log('QTY'.padEnd(qtyPad), 'ITEM'.padEnd(itemPad), "TOTAL"); + +order.forEach(({ itemName, quantity, unitPrice }) => { + console.log( + quantity.toString().padEnd(qtyPad), + itemName.padEnd(itemPad), + (unitPrice * quantity).toFixed(2)) +}); + +console.log("\nTotal:", total); \ No newline at end of file