From a4f583660e4d8433cfcd74ceb6e2f2e13e88233f Mon Sep 17 00:00:00 2001 From: Selina Hussain <61423748+selinahussain@users.noreply.github.com> Date: Fri, 18 Sep 2020 16:43:40 +0100 Subject: [PATCH 1/4] 2-code-reading done --- week-3/Homework/mandatory/1-practice/2-code-reading.md | 7 +++++++ 1 file changed, 7 insertions(+) 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..145036d 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. +line 6 will print out 1 as x in line 6 is global scoped which means it takes data from line 1. Line 3 is in {} which means the the x on line 3 is changing what x was originally but this only works inside {} which is why ;ine 4 will print 2 instead. + ## Question 2 Take a look at the following code: @@ -34,6 +36,8 @@ console.log(y) What will be the output of this code. Explain your answer in 50 words or less. +The first console log will print out 10 as the x is global scoped and can be accessed anywhere. However for y it will come out as an error or it not being defined as it is not global and the y is only accessible inside the function. + ## Question 3 Take a look at the following code: @@ -61,3 +65,6 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. + +The console.log(x) will be 9 as x is defined using a const variable which means it cannot be changed +The console.log(y) will be {x:10} and the f2 function can change the object and keys in the object can be overwritten. From 11bdc790eaf3189c478aa34fd7509ea744a8cf1c Mon Sep 17 00:00:00 2001 From: Selina Hussain <61423748+selinahussain@users.noreply.github.com> Date: Fri, 18 Sep 2020 16:53:55 +0100 Subject: [PATCH 2/4] exercise-1 finish --- .../2-exercises/exercise-1/exercise-1.js | 16 +++++++++------- .../2-exercises/exercise-1/exercise-1.md | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) 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..1906137 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 @@ -1,11 +1,13 @@ const personOne = { - name: 'Popeye', - age: 34, - favouriteFood: 'Spinach' -} + name: "Popeye", + age: 34, + favouriteFood: "Spinach", +}; -function introduceYourself(___________________________) { - console.log (`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`); +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); 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..5648570 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 @@ -17,5 +17,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? +- What is the syntax to destructure the object `personOne` in exercise-1.js? it is {} - Update the argument of the function `introduceYourself` to use destructuring on the object that gets passed in. From ebbf353eb432d2090eac3c16bcc50b48d17cc4bd Mon Sep 17 00:00:00 2001 From: Selina Hussain <61423748+selinahussain@users.noreply.github.com> Date: Fri, 18 Sep 2020 17:09:31 +0100 Subject: [PATCH 3/4] exercise-2 finish --- .../2-exercises/exercise-2/exercise-2.js | 86 ++++++++++++++++--- 1 file changed, 76 insertions(+), 10 deletions(-) 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..8093dc7 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 @@ -1,11 +1,77 @@ let hogwarts = [ - { firstName: "Harry", lastName: "Potter", house: "Gryffindor", pet: "Owl", occupation: "Student" }, - { firstName: "Hermione", lastName: "Granger", house: "Gryffindor", pet: "Cat", occupation: "Student" }, - { firstName: "Draco", lastName: "Malfoy", house: "Slytherin", pet: null, occupation: "Student" }, - { firstName: "Cedric", lastName: "Diggory", house: "HufflePuff", pet: null, occupation: "Student" }, - { firstName: "Severus", lastName: "Snape", house: "Slytherin", pet: null, occupation: "Teacher" }, - { firstName: "Filius", lastName: "Flitwick", house: "Ravenclaw", pet: null, occupation: "Teacher" }, - { firstName: "Pomona", lastName: "Sprout", house: "Hufflepuff", pet: null, occupation: "Teacher" }, - { firstName: "Minerva", lastName: "McGonagall", house: "Gryffindor", pet: null, occupation: "Teacher" }, - { firstName: "Albus", lastName: "Dumbledore", house: "Gryffindor", pet: "Phoenix", occupation: "Teacher" } -] + { + firstName: "Harry", + lastName: "Potter", + house: "Gryffindor", + pet: "Owl", + occupation: "Student", + }, + { + firstName: "Hermione", + lastName: "Granger", + house: "Gryffindor", + pet: "Cat", + occupation: "Student", + }, + { + firstName: "Draco", + lastName: "Malfoy", + house: "Slytherin", + pet: null, + occupation: "Student", + }, + { + firstName: "Cedric", + lastName: "Diggory", + house: "HufflePuff", + pet: null, + occupation: "Student", + }, + { + firstName: "Severus", + lastName: "Snape", + house: "Slytherin", + pet: null, + occupation: "Teacher", + }, + { + firstName: "Filius", + lastName: "Flitwick", + house: "Ravenclaw", + pet: null, + occupation: "Teacher", + }, + { + firstName: "Pomona", + lastName: "Sprout", + house: "Hufflepuff", + pet: null, + occupation: "Teacher", + }, + { + 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 }) => { + if (house === "Gryffindor") { + console.log(firstName, lastName); + } +}); + +hogwarts.forEach(({ firstName, lastName, pet, occupation }) => { + if (occupation === "Teacher" && pet) { + console.log(firstName, lastName); + } +}); From e3e6c789e45187c4fc0f2147e396e172350cd55a Mon Sep 17 00:00:00 2001 From: Selina Hussain <61423748+selinahussain@users.noreply.github.com> Date: Sat, 19 Sep 2020 17:28:41 +0100 Subject: [PATCH 4/4] exercise-3 finish --- .../2-exercises/exercise-3/exercise-3.js | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) 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..4a6acf5 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 @@ -1,9 +1,21 @@ - let order = [ - { itemName: "Hot cakes", quantity: 1, unitPrice: 2.29}, - { itemName: "Apple Pie", quantity: 2, unitPrice: 1.39}, - { itemName: "Egg McMuffin", quantity: 1, unitPrice: 2.80}, - { itemName: "Sausage McMuffin", quantity: 1, unitPrice: 3.00}, - { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.00}, - { itemName: "Hash Brown", quantity: 4, unitPrice: 0.40} - ] - \ No newline at end of file +let order = [ + { itemName: "Hot cakes", quantity: 1, unitPrice: 2.29 }, + { itemName: "Apple Pie", quantity: 2, unitPrice: 1.39 }, + { itemName: "Egg McMuffin", quantity: 1, unitPrice: 2.8 }, + { itemName: "Sausage McMuffin", quantity: 1, unitPrice: 3.0 }, + { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.0 }, + { itemName: "Hash Brown", quantity: 4, unitPrice: 0.4 }, +]; + +let total = 0; + +console.log(`QTY ITEM TOTAL`); + +order.forEach(({ itemName, quantity, unitPrice }) => { + console.log( + `${quantity} ${itemName} ${(unitPrice * quantity).toFixed(2)}` + ); + total += quantity * unitPrice; +}); + +console.log(`Total: ${total}`);