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

Js 3/week3/gintaras #45

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
8 changes: 8 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.

- 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:
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
14 changes: 14 additions & 0 deletions week-3/Homework/mandatory/2-exercises/exercise-2/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})
21 changes: 20 additions & 1 deletion week-3/Homework/mandatory/2-exercises/exercise-3/exercise-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,23 @@
{ itemName: "Hot Coffee", quantity: 2, unitPrice: 1.00},
{ itemName: "Hash Brown", quantity: 4, unitPrice: 0.40}
]



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