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..45de7f3 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,9 @@ Take a look at the following code: Explain why line 4 and line 6 output different numbers. +They output different numbers because the scopes of the x variable are different. + + ## Question 2 Take a look at the following code: @@ -34,7 +37,9 @@ console.log(y) What will be the output of this code. Explain your answer in 50 words or less. -## Question 3 +The output of this code will be 10 and undefined. The x variable have a global scope which means can be accessed inside other scopes. The y variable it's defined inside a functions scope and we are trying to display it outside of the function( where it's scope is not recognised). + +## Question 3 Take a look at the following code: @@ -61,3 +66,7 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. + +The output of the code will be x = 9 and y = 10. +X is a integer const and cannot be changed. +Y is a const object but we can change the value of the x inside. If we try to reasign the y object then this will throw an error. 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..82d6bad 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,8 +4,10 @@ const personOne = { favouriteFood: 'Spinach' } -function introduceYourself(___________________________) { +const {name, age, favouriteFood} = personOne; //answer to question 1 + +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); \ No newline at end of file +introduceYourself(personOne); \ No newline at end of file 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..0da3068 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,14 @@ let hogwarts = [ { firstName: "Minerva", lastName: "McGonagall", house: "Gryffindor", pet: null, occupation: "Teacher" }, { firstName: "Albus", lastName: "Dumbledore", house: "Gryffindor", pet: "Phoenix", occupation: "Teacher" } ] + + + + +hogwarts.forEach(({firstName, lastName, house}) => { + house === 'Gryffindor' ? console.log(`${firstName} ${lastName}`) : ''; +}) + +hogwarts.forEach(({firstName, lastName, pet, occupation}) => { + pet && occupation === 'Teacher' ? 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..b9c23b8 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,13 @@ { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.00}, { itemName: "Hash Brown", quantity: 4, unitPrice: 0.40} ] - \ No newline at end of file + + let totalPrice = 0; + + console.log(`QTY ITEM TOTAL`); + order.forEach(({itemName, quantity, unitPrice}) => { + console.log(`${quantity} ${itemName} ${unitPrice}`); + totalPrice += unitPrice; + }) + + console.log(`Total: ${totalPrice}`); \ No newline at end of file