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

Deniz-Week3-JS3 #42

Open
wants to merge 5 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
138 changes: 75 additions & 63 deletions week-3/Homework/mandatory/1-practice/2-code-reading.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,75 @@
# Code reading

## Question 1

Take a look at the following code:

```
1 let x = 1;
2 {
3 let x = 2;
4 console.log(x);
5 }
6 console.log(x);
```

Explain why line 4 and line 6 output different numbers.

## Question 2

Take a look at the following code:

```
let x = 10

function f1()
{
console.log(x)
let y = 20
}

console.log(f1())
console.log(y)
```

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

## Question 3

Take a look at the following code:

```
const x = 9;

function f1(val) {
val = val + 1;
return val;
}

f1(x);
console.log(x);

const y = { x: 9 };

function f2(val) {
val.x = val.x + 1;
return val;
}

f2(y);
console.log(y);
```

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

## Question 1

Take a look at the following code:

```
1 let x = 1;
2 {
3 let x = 2;
4 console.log(x);
5 }
6 console.log(x);
```

Explain why line 4 and line 6 output different numbers.
Big block and variable will shadow previous value

## Question 2

Take a look at the following code:

```
let x = 10

function f1()
{
console.log(x)
let y = 20
}

console.log(f1())
console.log(y)
```

What will be the output of this code. Explain your answer in 50 words or less.
//innerscope can acces global scope . we can access x
// let y inside in the function. and console log outside function we can not access y

## Question 3

Take a look at the following code:

```
const x = 9;

function f1(val) {
val = val + 1;
return val;
}

f1(x);
console.log(x);

const y = { x: 9 };

function f2(val) {
val.x = val.x + 1;
return val;
}

f2(y);
console.log(y);


function is not changing the value ===> 9
objects are reference type inside f2 function we are modifying a property of object ===> {x:10}






What will be the output of this code. Explain your answer in 50 words or less.
```
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);
87 changes: 77 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,78 @@
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((student) => {
let { house, firstName, lastName } = student;
if (house === "Gryffindor") {
console.log(firstName, lastName);
}
});
hogwarts.forEach((person) => {
let { firstName, lastName, occupation, pet } = person;
if (occupation === "Teacher" && pet !== null) {
console.log(firstName, lastName);
}
});
40 changes: 31 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,31 @@
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 },
];
function findTotal([...rest]) {
let plus = 0;

for (let i = 0; i < rest.length; i++) {
plus += rest[i].unitPrice * rest[i].quantity;
}

console.log("QTY \t\t\t ITEM \t\t\t\t TOTAL");

for (i = 0; i < rest.length; i++) {
console.log(
rest[i].quantity +
"\t\t\t" +
rest[i].itemName +
"\t\t\t" +
(rest[i].unitPrice * rest[i].quantity).toFixed(2)
);
}

console.log("\nTotal: " + plus);
}

findTotal(order);