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

Js core3/week3/leroy douglas #53

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
13 changes: 12 additions & 1 deletion 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,10 @@ Take a look at the following code:

Explain why line 4 and line 6 output different numbers.

## Answer 1

The console log on line 4 refers to the block scoped function declared on line 3, whereas the console log on line 6, refers to the globally scoped variable declared on line 1;

## Question 2

Take a look at the following code:
Expand All @@ -33,6 +37,9 @@ console.log(y)
```

What will be the output of this code. Explain your answer in 50 words or less.
## Answer 2

The first console log to be displayed will be from line 31, output is from the globally scoped x variable: 10. The 2nd console log attempts to display the return value of f1(): undefined. The 3rd console log causes an error as the y variable is out of scope.

## Question 3

Expand Down Expand Up @@ -60,4 +67,8 @@ f2(y);
console.log(y);
```

What will be the output of this code. Explain your answer in 50 words or less.
What will be the output of this code. Explain your answer in 50 words or less

## Answer3

The 1st log displays the global variable x: outputs 9. f1() takes a copy of the globally scoped x, therefore x is not affected by f1(). The 2nd logs the structure of object y, showing that the object's property changes from 9 to 10 due to f2() receiving a reference to y.
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
17 changes: 17 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,20 @@ let hogwarts = [
{ firstName: "Minerva", lastName: "McGonagall", house: "Gryffindor", pet: null, occupation: "Teacher" },
{ firstName: "Albus", lastName: "Dumbledore", house: "Gryffindor", pet: "Phoenix", occupation: "Teacher" }
]


const displayName = (people) => {
console.log("List of people in Gryffindor house: ");
people.forEach( ({ firstName, lastName, house }) =>house == "Gryffindor" && console.log(`${firstName} ${lastName}`) );
};

const isPetOwner = (people) => {
console.log("List of Teachers with pets: ");
people.forEach( ( { firstName, lastName, occupation, pet} ) => {
if(occupation === "Teacher" && pet !== null){
console.log(`${firstName} ${lastName}`);
}
});
}
displayName(hogwarts);
isPetOwner(hogwarts);
18 changes: 17 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,20 @@
{ itemName: "Hot Coffee", quantity: 2, unitPrice: 1.00},
{ itemName: "Hash Brown", quantity: 4, unitPrice: 0.40}
]


const receipt = order => {
let totalCost = 0;
console.log("QTY ITEM TOTAL");
order.forEach( ( {itemName, quantity, unitPrice} ) => {
let total = (quantity * unitPrice ).toFixed(2) ;
totalCost += quantity * unitPrice;
let padding = "".padStart(17 - itemName.length," ");
//console.log( str)
//let total = (quantity * unitPrice).toFixed(2).padStart(strlen(itemName), " ");

console.log(`${quantity} ${itemName}${padding}${total}`);
})
console.log(`Total: ${totalCost.toFixed(2)}`);
};

receipt(order);