-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavascript_Array.html
More file actions
117 lines (107 loc) · 3.83 KB
/
Copy pathJavascript_Array.html
File metadata and controls
117 lines (107 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
//Question 1 :
// var user = {'id':1001,'name':'John','age':22,'e-mail':'a@a.com'};
// var user = {
// id :1001,
// name:'John',
// age :22,
// 'e-mail':'a@a.com'
// } ;
// console.log(user,typeof(user));
// console.log(user['e-mail']);
//console.log(user['age']);
//Question 2 :
var users = [
{'id':1001,'name':'John','age':22,'e-mail':'j@j.com'},
{'id':1002,'name':'SMith','age':23,'e-mail':'s@s.com'},
{'id':1003,'name':'Joe','age':21,'e-mail':'y@y.com'}
];
//It is an Compound Object single Object holds multiple objects under itself.
console.log(users,typeof(users),users.length);
//calculate total & average Age
var totalAge =0;
for(var i=0;i<users.length;i++){
console.log(users[i]['e-mail']);
totalAge+=users[i].age;
}
console.log("Total Age is "+totalAge);
console.log("Average age is :"+(totalAge/users.length));
for(var xyz of users){
console.log(xyz['e-mail']);
}
for(var index in users){
console.log(index);
}
//Question 3:
//1.Display entire food list in console.
//2.Display only biriyani items and calculate total & average price.
//3.Calculate Gross total of all chicken related items .
//4.Find Out maximum cost Item , min cost item.
// 5.display those food items whose price is between 150-350.
var foodList = [
{'food_id':1001,'food':'Chicken Biriyani','price':200},
{'food_id':1002,'food':'Mutton Biriyani','price':350},
{'food_id':1003,'food':'Chilly Chicken','price':150},
{'food_id':1004,'food':'Paneer Masala','price':220},
{'food_id':1005,'food':'Chicken Lollypop','price':120}
];
console.log(foodList,typeof(foodList),foodList.length);
//1.Display entire food list in console.
for(var i=0;i<foodList.length;i++){
console.log(foodList[i]);
}
//2.Display only biriyani items and calculate total & average price.
var totalPrice =0;
var biriyaniCount =0;
for(var i=0;i<foodList.length;i++){
if(foodList[i]['food'].includes('Biriyani')){
console.log(foodList[i]);
totalPrice+=foodList[i].price;
biriyaniCount++;
}
}
console.log("Total Price of Biriyani is "+totalPrice);
console.log("Average Price of Biriyani is "+(totalPrice/biriyaniCount));
//3.Calculate Gross total of all chicken related items .
var grossTotal =0;
for(var i=0;i<foodList.length;i++){
if(foodList[i]['food'].includes('Chicken')){
console.log(foodList[i]);
grossTotal+=foodList[i].price;
}
}
console.log("Gross Total of Chicken related items is "+grossTotal);
//4.Find Out maximum cost Item , minimum cost item.
var maxCost = foodList[0].price;
var minCost = foodList[0].price;
var maxCostItem = foodList[0].food;
var minCostItem = foodList[0].food;
for(var i=0;i<foodList.length;i++){
if(foodList[i].price>maxCost){
maxCost = foodList[i].price;
maxCostItem = foodList[i].food;
}
if(foodList[i].price<minCost){
minCost = foodList[i].price;
minCostItem = foodList[i].food;
}
}
console.log("Maximum Cost Item is "+maxCostItem+" with price "+maxCost);
console.log("Minimum Cost Item is "+minCostItem+" with price "+minCost);
//5.display those food items whose price is between 150-350.
for(var i=0;i<foodList.length;i++){
if(foodList[i].price>=150 && foodList[i].price<=350){
console.log(foodList[i]);
}
}
</script>
</body>
</html>