-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
354 lines (324 loc) · 10.7 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// =================================
// Basic
// =================================
// let name = "John";
// const age = 30;
// console.log(name, age);
// =================
// Output: John 30
// =================
// =================================
// Functions:
// =================================
// let num1 = 5;
// let num2 = 6;
// console.log(addNumbers(num1, num2));
// function addNumbers(num1, num2) {
// return num1 + num2;
// }
// =================
// Output: 11
// =================
// =================================
// Conditionals:
// =================================
// const age = 30;
// if (age >= 18) {
// console.log("You are an adult.");
// } else {
// console.log("You are not an adult.");
// }
// =================
// Output: You are an adult.
// =================
// =================================
// Loops:
// =================================
// for (let i = 0; i < 10; i++) {
// console.log(i);
// }
// =================
// Output:
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// =================
// =================================
// Objects:
// =================================
// const person = {
// name: "John",
// age: 30,
// sayHello: function () {
// console.log("Hello!");
// },
// };
// let ans = person;
// console.log(ans);
// =================
// Output: { name: 'John', age: 30, sayHello: [Function: sayHello] }
// =================
// =========================================
// operators and expressions in JavaScript
// =========================================
// ================
// Operators:
// ================
// ================
// 1. Arithmetic Operators:
// Arithmetic operators are used to perform mathematical operations on numeric values.
// ================
// let x = 10;
// let y = 5;
// console.log(x + y); // Addition operator (+) || Output: 15
// console.log(x - y); // Subtraction operator (-) || Output: 5
// console.log(x * y); // Multiplication operator (*) || Output: 50
// console.log(x / y); // Division operator (/) || Output: 2
// console.log(x % y); // Modulus operator (%) || Output: 0
// console.log(++x); // Increment operator (++) || Output: 11
// console.log(--y); // Decrement operator (--) || Output: 4
// ================
// 2. Comparison Operators:
// Comparison operators are used to compare two values.
// ================
// let x = 10;
// let y = 5;
// console.log(x > y); // Output: true
// console.log(x < y); // Output: false
// console.log(x >= y); // Output: true
// console.log(x <= y); // Output: false
// console.log(x == y); // Output: false
// console.log(x != y); // Output: true
// ================
// 3. Logical Operators:
// ================
// let x = 10;
// let y = 5;
// console.log(x > 5 && y > 5); // Logical AND (&&) Output: false
// console.log(x > 5 || y > 5); // Logical OR (||) Output: true
// console.log(!(x > 5)); // Logical NOT (!) Output: false
// ================
// 4. Assignment Operators:
// Assignment operators are used to assign values to variables.
// ================
// let x = 10;
// x += 5; // x = x + 5;
// console.log(x); // Output: 15
// x -= 5; // x = x - 5;
// console.log(x); // Output: 10
// x *= 2; // x = x * 2;
// console.log(x); // Output: 20
// x /= 4; // x = x / 4;
// console.log(x); // Output: 5
// x %= 3; // x = x % 3;
// console.log(x); // Output: 2
// x **= 3; // x = x ** 3;
// console.log(x); // Output: 8
// =================
// 5. Conditional (Ternary) Operator:
// The conditional operator is a shorthand version of an if...else statement.
// =================
// let x = 10;
// let y = 5;
// let result = x > y ? "x is greater than y" : "x is less than or equal to y";
// console.log(result); // Output: "x is greater than y"
//====================
// Bitwise Operators:
// Bitwise operators are used to perform bitwise operations on binary numbers.
//====================
// let x = 5; // Binary: 0101
// let y = 10; // Binary: 1010
// console.log(x & y); // Bitwise AND (&) -> 0000 (0)
// console.log(x | y); // Bitwise OR (|) -> 1111 (15)
// console.log(x ^ y); // Bitwise XOR (^) -> 1111 (15)
// console.log(~x); // Bitwise NOT (~) -> 1010 (-6)
// console.log(x << 1); // Left shift (<<) -> 1010 (10)
// console.log(x >> 1); // Right shift (>>) -> 0010 (2)
//====================
// Comma Operator:
// The comma operator is used to separate expressions in a statement and returns the value of the last expression.
//====================
// let x = 1,
// y = 2,
// z = 3;
// let result = (x + y, y + z);
// console.log(result); // Output: 5
// =================
// Expressions:
// =================
// 1. Primitive Expressions
// =================
// Primitive expressions are the simplest types of expressions and include literals and variables.
// =================
// let x = 5; // Numeric literal
// let y = "hello"; // String literal
// let z = true; // Boolean literal
// console.log(x); // Output: 5
// console.log(y); // Output: hello
// console.log(z); // Output: true
//===========================
// 2. Arithmetic Expressions:
//===========================
// Arithmetic expressions use arithmetic operators to perform mathematical operations.
//==========================
// let x = 5 + 2 * 3; // Output: 11
// let y = (5 + 2) * 3; // Output: 21
// console.log(x);
// console.log(y);
//===========================
// 3. Comparison Expressions:
//===========================
// Comparison expressions use comparison operators to compare values.
//============================
// let x = 5;
// let y = 10;
// console.log(x > y); // Output: false
// console.log(x == y); // Output: false
// console.log(x != y); // Output: true
//======================
// 4. Logical Expressions:
//======================
// Logical expressions use logical operators to combine or negate logical values.
//======================
// let x = 5;
// let y = 10;
// console.log(x < 10 && y > 5); // Output: true
// console.log(x > 10 || y < 5); // Output: false
// console.log(!(x < y)); // Output: false
//==========================
// 5. Function Expressions:
//==========================
// Function expressions define a function as a variable and can be called like a regular function.
//==========================
// let add = function (x, y) {
// return x + y;
// };
// let result = add(5, 10);
// console.log(result); // Output: 15
//=======================
// Object Expressions:
//=======================
// Object expressions define an object with properties and values.
//=======================
// let person = {
// name: "John",
// age: 30,
// gender: "male",
// };
// console.log(person.name); // Output: "John"
// console.log(person.age); // Output: 30
// console.log(person.gender); // Output: "male"
//===========================
// Decision Making:
//===========================
//In JavaScript, decision-making is achieved using conditional statements.
//There are three types of conditional statements in JavaScript: if statement, if-else statement, and switch statement.
//Let's take a look at each of them with some code examples.
//========================
// 1. if statement: The if statement is used to execute a block of code if a condition is true.
//========================
// let x = 10;
// if (x > 5) {
// console.log("x is greater than 5");
// }
// In the example above, the code inside the if statement will only be executed if the condition x > 5 is true.
// In this case, since x is 10, the condition is true and the code inside the if statement will be executed, which will output "x is greater than 5" to the console.
//=====================
// if-else statement: The if-else statement is used to execute one block of code if a condition is true, and another block of code if the condition is false.
//=====================
// let x = 3;
// if (x > 5) {
// console.log("x is greater than 5");
// } else {
// console.log("x is less than to 5");
// }
// In the example above, the condition x > 5 is false, so the code inside the else block will be executed, which will output "x is less than or equal to 5" to the console.
//========================
// switch statement: The switch statement is used to execute different blocks of code depending on the value of an expression.
//========================
// let day = "Wednesday";
// switch (day) {
// case "Monday":
// console.log("Today is Monday");
// break;
// case "Tuesday":
// console.log("Today is Tuesday");
// break;
// case "Wednesday":
// console.log("Today is Wednesday");
// break;
// default:
// console.log("Today is not Monday, Tuesday, or Wednesday");
// }
// In the example above, the switch statement checks the value of the day variable and executes the corresponding block of code.
// Since day is "Wednesday", the third case will be executed, which will output "Today is Wednesday" to the console.
// These are some of the common decision-making statements in JavaScript.
// I hope this helps you understand how to use them in your code!
//========================
// Iteration Statements:
//========================
// 1. for statement: The for statement is used to repeatedly execute a block of code until a condition is met.
//========================
// for (let i = 0; i < 5; i++) {
// console.log(i);
// }
//=========================
// 2. for...in statement: The for...in statement is used to loop through the properties of an object.
//=========================
// const obj = { a: 1, b: 2, c: 3 };
// for (let prop in obj) {
// console.log(prop + ": " + obj[prop]);
// }
//===========================
// 3. for...of statement: The for...of statement is used to loop through the values of an iterable object.
//============================
// const arr = [1, 2, 3, 4, 5];
// for (let value of arr) {
// console.log(value);
// }
//============================
// 4. while statement: The while statement is used to repeatedly execute a block of code while a condition is true.
//=============================
// let i = 0;
// while (i < 5) {
// console.log(i);
// i++;
// }
// Output: 0 1 2 3 4
//==================
// 5. do...while loop - A do...while loop is used to execute a block of code once, and then repeat the loop as long as a specified condition is true.
//==================
// let i = 0;
// do {
// console.log(i);
// i++;
// } while (i < 5);
// Output: 0 1 2 3 4
//========================================================
//Today: 02-03-2023
// let multi_OP = function (x, y) {
// let addition = add(x, y); // 15
// let multiplication = multi(x, y); // 50
// let divison = div(x, y); // 2
// return addition + multiplication + divison; // 15+50+2 = 67
// };
// let add = function (x, y) {
// return x + y; // 5+10 = 15
// };
// let multi = function (x, y) {
// return x * y; // 5*10 = 50
// };
// let div = function (x, y) {
// return y / x; // 10/5 = 2
// };
// let x = 5,
// y = 10;
// let result = multi_OP(x, y);
// console.log(result); // Output: 67