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

Js3/week3/davinder #55

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
16 changes: 16 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,10 @@ Take a look at the following code:

Explain why line 4 and line 6 output different numbers.


Line 4 will output 2 where as line 6 will output 1 because of the scope of variables. Scope of local variable declared on line 3 is limited and goes till the end of the block (till line 5). And console.log on line 11 will have access to the closest x which is on line 3 so it will output 2.


## Question 2

Take a look at the following code:
Expand All @@ -34,6 +38,10 @@ console.log(y)

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


Firstly the code will output the value 10 because function f1() is called on line 32. Then it will output undefined for the console.log on line 32 because f1() is not returning anything for the output. Then it will throw an error for the console.log() on line 33 because y is not known outside of the f1 function so y is an undefined variable outside the function f1().


## Question 3

Take a look at the following code:
Expand Down Expand Up @@ -61,3 +69,11 @@ console.log(y);
```

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



Output will be :
9
{ x: 10 }

the console.log() on line will print 9 because value of x is not changing inside the function because val is a local variable. and object y will change it's value as it is passed as a parameter and its value is being inside the function so x:9 inside the object will change to 1.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const personOne = {
favouriteFood: 'Spinach'
}

function introduceYourself(___________________________) {
let {name,age,favouriteFood}=personOne;

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(name,age,favouriteFood);
22 changes: 22 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,25 @@ let hogwarts = [
{ firstName: "Minerva", lastName: "McGonagall", house: "Gryffindor", pet: null, occupation: "Teacher" },
{ firstName: "Albus", lastName: "Dumbledore", house: "Gryffindor", pet: "Phoenix", occupation: "Teacher" }
]


function GryffindorHouse(hogwarts)
{
for(let i=0;i<hogwarts.length;i++)
{
// console.log(hogwarts[i]);
let {firstName,lastName,house,pet,occupation}=hogwarts[i];
if(house==="Gryffindor")
console.log(firstName+" "+lastName);
}
console.log(" \n ")
for(let i=0;i<hogwarts.length;i++)
{
let {firstName,lastName,house,pet,occupation}=hogwarts[i];
// console.log(" \n ")
if(pet!=null)
console.log(firstName+" "+lastName);
}
}

GryffindorHouse(hogwarts);
17 changes: 16 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,19 @@
{ itemName: "Hot Coffee", quantity: 2, unitPrice: 1.00},
{ itemName: "Hash Brown", quantity: 4, unitPrice: 0.40}
]



function receipt(order)
{
let total=0;
console.log("QTY ITEM TOTAL" )
for(let i=0;i<order.length; i++)
{
let {itemName,quantity,unitPrice}=order[i];
console.log(quantity+" "+itemName+" "+quantity*unitPrice);
total+=quantity*unitPrice;
}
console.log(total);
}

receipt(order);