Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Selina week3 #49

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions week-3/Homework/mandatory/1-practice/2-code-reading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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.
16 changes: 9 additions & 7 deletions week-3/Homework/mandatory/2-exercises/exercise-1/exercise-1.js
Original file line number Diff line number Diff line change
@@ -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);
introduceYourself(personOne);
Original file line number Diff line number Diff line change
Expand Up @@ -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.
86 changes: 76 additions & 10 deletions week-3/Homework/mandatory/2-exercises/exercise-2/exercise-2.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
30 changes: 21 additions & 9 deletions week-3/Homework/mandatory/2-exercises/exercise-3/exercise-3.js
Original file line number Diff line number Diff line change
@@ -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}
]

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}`);