-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4controlStatement.js
166 lines (145 loc) · 2.43 KB
/
4controlStatement.js
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
165
166
// If and else
// if raining = raincoat
// else no raincoat
// var raining = true;
var raining = false;
if (raining) {
console.log("take a raincoat")
} else {
console.log("Enjoy your day :)");
}
// Check leap year.
var year = 2000;
// debugger;
if (year % 4 === 0) {
if (year % 100 === 0) {
if (year % 400 === 0) {
console.log("This year " + year + " is a leap year");
} else {
console.log("This year " + year + " is not a leap year");
}
} else {
console.log("This year " + year + " is a leap year");
}
} else {
console.log("This year " + year + " is not a leap year");
}
// What is truthy and falsy values in javascript?
/* 0, " ", undefined, null, NaN, false */
// var score = 0;
var score = 10;
if (score == 0) {
console.log("We loss the game🤢");
} else {
console.log("We won the game😊");
}
/* Conditional (ternary) Operator */
// var age = 17;
var age = 27;
console.log((age >= 18) ? "You can vote" : "You can't vote");
// Switch Statement
// var area = "Circle";
// var area = "Triangle";
var area = "Rectangle";
// var area;
var PI = 3.14, l = 5, b = 4, r = 3;
switch (area) {
case "Circle":
console.log("The area of circle is : " + (PI * r ** 2));
break;
case "Triangle":
console.log("The area of triangle is : " + (l * b) / 2);
break;
case "Rectangle":
console.log("The area of circle is : " + (l * b));
break;
default:
console.log("Inter proper input");
break;
}
// While loop
var num = 0;
while (num <= 10) {
console.log(num);
num++;
}
// Do-While loop
var num = 11;
do {
console.log(num);
num++;
} while (num <= 10)
/*
Difference b/w do-while and while
do-while will execute atleast once.
*/
// For loop
var num = 10
for (var i = 0; i <= num; i++) {
console.log(i);
}
// Print Table Using for Loop
var x = 8;
for (var i = 1; i <= 10; i++) {
console.log(`${x} * ${i} = ${x * i}`);
}
/*
output
Enjoy your day :)
This year 2000 is a leap year
We won the game😊
You can vote
The area of circle is : 20
0
1
2
3
4
5
6
7
8
9
10
11
0
D:\DailyStuff\JavaScript>node 4controlStatement.js
Enjoy your day :)
This year 2000 is a leap year
We won the game😊
You can vote
The area of circle is : 20
0
1
2
3
4
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
8
9
10
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
*/