-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
164 lines (143 loc) · 3.95 KB
/
script2.js
File metadata and controls
164 lines (143 loc) · 3.95 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
/*
let dolphinsscore1 = Number(prompt("Enter the dolphins score 1: "));
let dolphinsscore2 = Number(prompt("Enter the dolphins score 2: "));
let dolphinsscore3 = Number(prompt("Enter the dolphins score 3: "));
let koalasscore1 = Number(prompt("Enter the koalas score 1: "));
let koalasscore2 = Number(prompt("Enter the koalas score 2: "));
let koalasscore3 = Number(prompt("Enter the koalas score 3: "));
const avgcalc = (score1,score2,score3)=>{
return (score1+score2+score3)/3 ;
}
const avgDolphins = avgcalc(dolphinsscore1,dolphinsscore2,dolphinsscore3);
const avgKoalas = avgcalc(koalasscore1,koalasscore2,koalasscore3);
checkscore(avgDolphins,avgKoalas);
function checkscore(avgDolphins,avgKoalas)
{
if(avgDolphins >= 2*avgKoalas)
{
console.log(`Dolphins are the winners 🥳:${avgDolphins} vs ${avgKoalas}`);
}
else if(avgKoalas >= 2*avgDolphins)
{
console.log(`Koalas are the winners 🥳: ${avgKoalas} vs ${avgDolphins}`);
}
else
{
console.log(`No winnners 😔: ${avgDolphins} vs ${avgKoalas}`);
}
}
const friends=["siva","sai",190010009];
console.log(friends);
friends.push("ANR");
console.log(friends);
friends.unshift("Indira");
console.log(friends);
console.log(friends.pop());
console.log(friends);
console.log(friends.shift());
console.log(friends);
//we have arrayname.indexof(element) will return the index of the element
console.log(friends.includes('siva'));
//note that includes uses strict equality
const bill = Number(prompt("Enter the bill amount: "));
console.log("tip for bill 100: "+calcTip(bill));
function calcTip(billamount)
{
if((billamount>=50) && (billamount<=300))
{
const tip = billamount*0.15 ;
return tip;
}
else{
const tip = billamount*0.2 ;
return tip;
}
}
const bills=[125,555,44];
const tips = [calcTip(bills[0]),calcTip(bills[1]),calcTip(bills[2])];
const total = [bills[0]+tips[0],bills[1]+tips[1],bills[2]+tips[2]];
console.log("Bills: "+bills);
console.log("Tips: "+tips);
console.log("Total: "+total);
const venkat = {
firstname:"siva",
lastname:"sai",
rollno:190010009,
friends: ["A","B","c"]
};
console.log(venkat.friends[1],venkat.friends.length);
//coding challenge
const obj1 = {
firstname: "John",
lastname: "Smith",
mass: 92,
height: 1.95,
calcBMI : function(){
this.johnBMI = this.mass/(this.height ** 2);
return this.johnBMI;
}
};
const obj2 = {
firstname: "Mark",
lastname: "Miller",
mass: 78,
height:1.69,
calcBMI : function(){
this.markBMI = this.mass/(this.height ** 2);
return this.markBMI;
}
};
console.log(`${obj1.firstname}'s BMI ${obj1.calcBMI()}`);
console.log(`${obj2.firstname}'s BMI ${obj2.calcBMI()}`);
if(obj1.calcBMI() > obj2.calcBMI())
{
console.log(`${obj1.firstname}'s BMI(${obj1.johnBMI}) is higher than ${obj2.firstname}'s BMI(${obj2.markBMI})`);
}
else if(obj1.calcBMI() === obj2.calcBMI())
{
console.log(`${obj1.firstname}'s BMI(${obj1.johnBMI}) is equal to ${obj2.firstname}'s BMI(${obj2.markBMI})`);
}
else
{
console.log(`${obj1.firstname}'s BMI(${obj1.johnBMI}) is lesser than ${obj2.firstname}'s BMI(${obj2.markBMI})`);
}
*/
const bills = [22,295,176,440,37,105,10,1100,86,52];
const tips = [];
const totals = [];
for(let i=0;i<bills.length;i++)
{
tips.push(calcTip(bills[i]));
totals.push(tips[i]+bills[i]);
}
console.log(bills);
console.log(tips);
console.log(totals);
const avg1 = calcAverage(bills);
const avg2 = calcAverage(tips);
const avg3 = calcAverage(totals);
console.log(avg1);
console.log(avg2);
console.log(avg3);
function calcTip(billamount)
{
if((billamount>=50) && (billamount<=300))
{
const tip = billamount*0.15 ;
return tip;
}
else{
const tip = billamount*0.2 ;
return tip;
}
}
function calcAverage(arr)
{
let sum = 0;
for(let i=0;i<arr.length;i++)
{
sum+=arr[i];
}
return sum/arr.length;
}