Skip to content

Commit ed02a75

Browse files
authored
Added sample code files.
Check shopping cart and bookmyshow programs made using module pattern and prototype inheritance respectively
1 parent cd64aaf commit ed02a75

13 files changed

+632
-0
lines changed

1-js-declarations.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//variable declaration moves first or function declaration??
2+
alert(abc);
3+
4+
var abc=10;
5+
function abc(){console.log("Function declaration gets hoisted first");}
6+
alert("Variable declaration gets hoisted after F.declaration");
7+
alert(abc);
8+
9+
10+
11+
//Variable Declared in Both Local Function and Global scope
12+
var b=10; //global variable
13+
console.log(b); //10
14+
function func()
15+
{
16+
var b=700; //local function variable.Its name is same as global variable but both are different.
17+
console.log(b); //100
18+
}
19+
func();
20+
console.log(b); //10
21+
22+
23+
24+
//Variable declared without the var keyword
25+
a=10; //global variable
26+
console.log(a); //10
27+
function zz()
28+
{
29+
b=30; //variable declared without using var,let,const automatically beacomes global
30+
console.log(b); //30
31+
}
32+
zz();
33+
console.log(b); //30

10-Async-await.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Async functions always return a Promise. If the function throws an error,
2+
// the Promise will be rejected. If the function returns a value, the Promise will be resolved.
3+
4+
let getData=function(message){
5+
return new Promise(function(resolve,reject){
6+
resolve("abhinav is saying "+message+"! You want more?");
7+
});
8+
}
9+
let getMoreData=function(message){
10+
return new Promise(function(resolve,reject){
11+
resolve("Rahul is saying "+message+"! You want more??");
12+
});
13+
}
14+
let getSomeMoreData=function(message){
15+
return new Promise(function(resolve,reject){
16+
resolve("Gagan is saying "+message+"! You want more??");
17+
});
18+
}
19+
let get_SomeMore=function(message){
20+
return new Promise(function(resolve,reject){
21+
resolve("Nirmal is saying "+message+"! GOODBYE!!!");
22+
});
23+
}
24+
25+
async function data(){
26+
try{
27+
const fromResolve1 = await getData("Hi!");
28+
console.log(fromResolve1);
29+
30+
const fromResolve2 = await getData("Hello!");
31+
console.log(fromResolve2);
32+
33+
const fromResolve3 = await getData("Kidaan!");
34+
console.log(fromResolve3);
35+
36+
const fromResolve4 = await getData("Chad di kala");
37+
console.log(fromResolve4);
38+
}
39+
catch(err){
40+
console.log(err);
41+
}
42+
}
43+
data();
44+
45+
//One more example----------------------------------------------------------------------------------------------------
46+
47+
function makeRequest(location){
48+
return new Promise(function(resolve,reject){
49+
console.log("making request to "+location);
50+
if(location==='google'){
51+
resolve('Google says Hi!');
52+
}
53+
else{
54+
reject('We can only talk to google');
55+
}
56+
});
57+
}
58+
function processRequest(response){
59+
return new Promise(function(resolve,reject){
60+
console.log("Processing response");
61+
resolve("Extra info "+response);
62+
});
63+
}
64+
async function doWork(){
65+
try{
66+
const response=await makeRequest('google');
67+
console.log("Response received");
68+
const processedResponse=await processRequest(response);
69+
console.log(processedResponse);
70+
}
71+
catch (err)
72+
{
73+
console.log(err);
74+
};
75+
}
76+
doWork();
77+
78+

11-object-task.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const animal={ //object 1
2+
name:['dog','cat','lion'], //object 1 properties
3+
color:{ //object 2
4+
col1:'black', //object 2 properties
5+
col2:'white',
6+
beau: function(){ console.log(this.col2+" cats are beautiful"); },
7+
col3:'yellow',
8+
age:{ //object 3
9+
age1:5, //object 3 properties
10+
age2:3,
11+
age3:11
12+
}
13+
},
14+
display:function(){
15+
console.log("Different animals in the zoo are:"+animal.name);
16+
console.log("color of "+animal.name[0]+" is "+animal.color.col1+
17+
" and age is "+animal.color.age.age1);
18+
console.log("color of "+animal.name[1]+" is "+animal.color.col2+
19+
" and age is "+animal.color.age.age2);
20+
console.log("color of "+animal.name[2]+" is "+animal.color.col3+
21+
" and age is "+animal.color.age.age3);
22+
}
23+
};
24+
animal.color.beau();
25+
animal.display();
26+
animal['dob']="2017,2018,2019"; //adding a new property to object 1 using square notation
27+
console.log(animal.dob);
28+
animal.color.col3='golden'; //modifying property 3 of object 2
29+
animal.change=function(){ //adding a new method named change in object 1
30+
console.log("changed color of "+animal.name[2] + " is "+animal.color.col3);
31+
}
32+
animal.change();
33+
let myKeyName="zoo"; //Dynamically adding a property
34+
let myKeyValue="Chandigarh";
35+
animal[myKeyName]=myKeyValue;
36+
console.log(animal.zoo);

12-prototype-task1.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Use your knowledge gained so far and do the following
2+
// Create a Animal Object ( using Function Constructor) . Inherit two types of cat from animal
3+
// (Big cat and small Cat) . Implement Walk , talk and type methods.
4+
// Type will be common to all objects and give the same output.
5+
// Walk and talk mentors should be specific to the two inherited objects Big Cat and Small Cat .
6+
7+
function Animal(walkstyle,talkstyle)
8+
{
9+
this.Walk=walkstyle,
10+
this.Talk=talkstyle
11+
}
12+
Animal.prototype.walk=function(){
13+
console.log("This cat walks "+this.Walk);
14+
}
15+
Animal.prototype.talk=function(){
16+
console.log("This cat talks in "+this.Talk);
17+
}
18+
Animal.prototype.type=function(){
19+
console.log("All have same type");
20+
}
21+
let bigCat=new Animal('straight','hindi');
22+
let smallCat=new Animal('zig-zag','urdu');
23+
bigCat.walk();
24+
bigCat.talk();
25+
bigCat.type();
26+
smallCat.walk();
27+
smallCat.talk();
28+
smallCat.type();

13-bookMyShow-prototype.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// knowledge of oojs gained so far you have to build a online movie ticket app, just like a bookmyshow
2+
// define the enntities needed in the app as objects and and use prototype inheriteance to build the
3+
// app functionality.
4+
5+
6+
var pvr={
7+
pm:function(){
8+
var d=prompt("How many tickets: ");
9+
var dd=parseInt(d);
10+
console.log("Price of silver ticket is:Rs100 & gold ticket is:Rs120");
11+
var e=prompt("Which ticket do you want: ");
12+
console.log("Your total tickets: "+dd+" and you chose "+e+" ticket");
13+
if(e=="silver")
14+
{
15+
var calc=dd*100;
16+
console.log("Your total booking amount: "+calc);
17+
}
18+
else
19+
{
20+
var calc=dd*120;
21+
console.log("Your total booking amount: "+calc);
22+
}
23+
}
24+
};
25+
var cinepolis={
26+
cm:function(){
27+
var f=prompt("How many tickets: ");
28+
var ff=parseInt(f);
29+
console.log("Price of silver ticket is:Rs150 & gold ticket is:Rs180");
30+
var g=prompt("Which ticket do you want: ");
31+
console.log("Your total tickets: "+ff+" and you chose "+g+" ticket");
32+
if(g=="silver")
33+
{
34+
var calcc=ff*150;
35+
console.log("Your total booking amount: "+calcc);
36+
}
37+
else
38+
{
39+
var calcc=ff*180;
40+
console.log("Your total booking amount: "+calcc);
41+
}
42+
}
43+
};
44+
var piccadilly={
45+
ppm:function(){
46+
var j=prompt("How many tickets: ");
47+
var jj=parseInt(j);
48+
console.log("Price of silver ticket is:Rs200 & gold ticket is:Rs400");
49+
var k=prompt("Which ticket do you want: ");
50+
console.log("Your total tickets: "+jj+" and you chose "+k+" ticket");
51+
if(k=="silver")
52+
{
53+
var cal=jj*200;
54+
console.log("Your total booking amount: "+cal);
55+
}
56+
else
57+
{
58+
var cal=jj*400;
59+
console.log("Your total booking amount: "+cal);
60+
}
61+
}
62+
};
63+
function bookMyShow(pvvr,cine,pica)
64+
{
65+
this.a=prompt("Enter your name: ");
66+
console.log("Which movie you want to watch:\n1)avengers\n2)joker\n3)avatar");
67+
this.c=prompt("Enter name of the movie: ");
68+
console.log("List of available cinemas:\n1)cinepolis\n2)pvr\n3)piccadilly");
69+
this.b=prompt("Enter cinema name where you want to watch the film: ");
70+
console.log("Welcome "+this.a+" to "+this.b+" cinemas!");
71+
this.booking=function(){
72+
if(this.b=='pvr')
73+
{
74+
pvvr.pm();
75+
}
76+
else if(this.b=='cinepolis')
77+
{
78+
cine.cm();
79+
}
80+
else
81+
pica.ppm();
82+
}
83+
}
84+
bookMyShow.prototype.ticket=function(){
85+
console.log('-------------------------------------------------');
86+
console.log("CONGRATULATIONS!!!\tYour booking is successful.");
87+
console.log(this.a+" your ticket is booked for the movie "+this.c+" at "+this.b);
88+
}
89+
var person1=new bookMyShow(pvr,cinepolis,piccadilly);
90+
person1.booking();
91+
person1.ticket();
92+
var person2=new bookMyShow(pvr,cinepolis,piccadilly);
93+
person2.booking();
94+
person2.ticket();
95+
var person3=new bookMyShow(pvr,cinepolis,piccadilly);
96+
person3.booking();
97+
person3.ticket();
98+

2-minicalculator-SwitchCase.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//MINI CALCULATOR
2+
console.log("What operation do you want to perform?");
3+
console.log("1.Addition(+)\n2.Subtraction(-)\n3.Multiplication(*)\n4.Division(/)\n5.Percentage(%)");
4+
var e=prompt("Enter the respective symbol given with the operation name: ");
5+
console.log(e);
6+
var a=prompt("Enter 1st number: ");
7+
var b=parseInt(a);
8+
console.log(b);
9+
var c=prompt("Enter 2nd number: ");
10+
var d=parseInt(c);
11+
switch(e)
12+
{
13+
case '+': console.log("Addition of "+b+" & "+d+" is: ",b+d);
14+
break;
15+
case '-': console.log("Subtraction of "+b+" & "+d+" is: ",b-d);
16+
break;
17+
case '*': console.log("Multiplication of "+b+" & "+d+" is: ",b*d);
18+
break;
19+
case '/':if(d==0){console.log("Undefined:Change the value of number(not 0)");}
20+
else
21+
console.log("Division of "+b+" & "+d+" is: ",b/d);
22+
break;
23+
case '%': console.log("Percentage of "+b+" & "+d+" is: "+(b/d)*100+"%");
24+
break;
25+
default:
26+
console.log("Please enter a available operation");
27+
}

3-closuretask.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function createBase(num1)
2+
{
3+
return function(num2)
4+
{
5+
console.log(num1+num2);
6+
}
7+
}
8+
var addSix=createBase(6);
9+
addSix(10);
10+
addSix(21);

4-closuretask.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function Counter(){
2+
var counter=0;
3+
return {
4+
increase: function(increment) { counter+=increment; },
5+
6+
decrease: function(decrement) { counter-=decrement; },
7+
8+
display: function() { return "Counter is at "+ counter; }
9+
}
10+
}
11+
var calculate=Counter();
12+
console.log(calculate.display());
13+
calculate.increase(2);
14+
calculate.increase(2);
15+
calculate.increase(2);
16+
console.log(calculate.display());
17+
calculate.decrease(2);
18+
console.log(calculate.display());

5-shop_cart.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Closures implemented using IIFE is called Module Pattern.It used to export only
2+
// certain things and rest you can HIDE(Private).
3+
// The Module Pattern is one of the most common design
4+
// patterns used in JavaScript and for good reason. The
5+
// module pattern is easy to use and creates encapsulation of
6+
// our code.
7+
8+
//Shopping cart created using Module Pattern
9+
10+
console.log("You can add the following items in the cart:");
11+
console.log("apple:Rs70\n soap:Rs10\n fogg:Rs100");
12+
var s_cart=(function(){
13+
var basket=[]; //private variable
14+
15+
//private functions
16+
function addition(name) {basket.push(name); console.log("your shopping cart items:"+basket);}
17+
function Delete() {return basket.pop();}
18+
19+
return {
20+
add: function(name){ addition(name); }, //public methods
21+
remove: function(){ return Delete(); },
22+
display: function()
23+
{
24+
var calc=0;
25+
for(var i=0;i<basket.length;i++)
26+
{
27+
if(basket[i]==='apple')
28+
{
29+
calc+=70;
30+
}
31+
else if(basket[i]==='soap')
32+
{
33+
calc+=10;
34+
}
35+
else if(basket[i]==='fogg')
36+
{
37+
calc+=100;
38+
}
39+
}
40+
console.log("Total value of your shopping cart items is:Rs"+calc);
41+
}
42+
};
43+
}());
44+
s_cart.add('apple');
45+
s_cart.display();
46+
s_cart.add('fogg');
47+
s_cart.display();
48+
s_cart.add('soap');
49+
s_cart.display();
50+
console.log("Item removed",s_cart.remove());
51+
s_cart.display();
52+
53+

0 commit comments

Comments
 (0)