forked from ialimustufa/learn-js
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1-complete.js
254 lines (188 loc) · 6.04 KB
/
day1-complete.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
// Variables
var name = "Srikant";
let age = 40;
const PI = 3.14;
// Data types
let number = 2023; // number
let string = "Sacred Games"; // string
let boolean = true; // boolean
let nullValue = null; // null
let undefinedValue; // undefined (implicitly)
let symbolValue = Symbol('secret'); // symbol
let bigIntValue = 12345678901234567890n; // bigint
let object = { // object
showName: "Made in Heaven",
season: 2
};
let array = ["Gully Boy", "Mirzapur", "Paatal Lok"]; // array
// Multi-line string
let multiLineString = `Welcome to the world of
Indian web series.
Enjoy the show!`;
console.log(multiLineString);
let a = 10;
let b = 3;
// Addition
let sum = a + b;
console.log(`Sum: ${sum}`); // Sum: 13
// Subtraction
let difference = a - b;
console.log(`Difference: ${difference}`); // Difference: 7
// Multiplication
let product = a * b;
console.log(`Product: ${product}`); // Product: 30
// Division
let quotient = a / b;
console.log(`Quotient: ${quotient}`); // Quotient: 3.3333...
// Modulus (remainder)
let remainder = a % b;
console.log(`Remainder: ${remainder}`); // Remainder: 1
// Exponentiation
let power = a ** b;
console.log(`Power: ${power}`); // Power: 1000
// Assignment Operators
let x = 5;
// Assignment
x = 10;
console.log(`x = 10: ${x}`); // x = 10: 10
// Addition assignment
x += 5;
console.log(`x += 5: ${x}`); // x += 5: 15
// Subtraction assignment
x -= 3;
console.log(`x -= 3: ${x}`); // x -= 3: 12
// Multiplication assignment
x *= 2;
console.log(`x *= 2: ${x}`); // x *= 2: 24
// Division assignment
x /= 4;
console.log(`x /= 4: ${x}`); // x /= 4: 6
// Modulus assignment
x %= 5;
console.log(`x %= 5: ${x}`); // x %= 5: 1
// Exponentiation assignment
x **= 3;
console.log(`x **= 3: ${x}`); // x **= 3: 1
// Logical Operators
let isAdult = true;
let hasTicket = false;
// AND (&&) - true if both operands are true
console.log(`isAdult && hasTicket: ${isAdult && hasTicket}`); // isAdult && hasTicket: false
// OR (||) - true if at least one operand is true
console.log(`isAdult || hasTicket: ${isAdult || hasTicket}`); // isAdult || hasTicket: true
// NOT (!) - inverts the boolean value
console.log(`!isAdult: ${!isAdult}`); // !isAdult: false
// Type Operators
let str = "Hello, World!";
console.log(`typeof str: ${typeof str}`); // typeof str: string
let date = new Date();
console.log(`date instanceof Date: ${date instanceof Date}`); // date instanceof Date: true
// Type conversion
let numStr = "123";
let num = Number(numStr);
console.log(`num: ${num}, typeof num: ${typeof num}`); // num: 123, typeof num: number
let boolStr = "true";
let bool = Boolean(boolStr);
console.log(`bool: ${bool}, typeof bool: ${typeof bool}`); // bool: true, typeof bool: boolean
// Basic Input/Output
alert("Welcome to the JavaScript tutorial inspired by 'Panchayat'!");
let myname = prompt("What is your name?");
console.log("Employee's name is " + myname);
// Debugging with breakpoints can be done in the browser's developer tools.
console.log("This is a log message.");
console.warn("This is a warning message.");
console.error("This is an error message.");
console.table([{name: "Kabir", age: 29}, {name: "Radhika", age: 34}]);
// Conditional Statements
let workHours = prompt("Enter the number of hours you worked today:");
workHours = parseInt(workHours);
if (workHours > 8) {
console.log("Great job! You have worked extra hours today.");
} else if (workHours == 8) {
console.log("Good job! You have completed your work hours.");
} else {
console.log("You need to work more hours.");
}
switch (workHours) {
case 8:
console.log("Standard work hours.");
break;
case 10:
console.log("Overtime work hours.");
break;
default:
console.log("Varied work hours.");
}
// Loop Example
// for loop
for (let i = 1; i <= 5; i++) {
console.log("Task " + i + ": Complete Panchayat duty");
}
// while Loop
let i = 1;
while (i <= 5) {
console.log("Task " + i + ": Review Gram Sabha decisions");
i++;
}
// do-while Loop
let j = 1;
do {
console.log("Task " + j + ": Prepare report");
j++;
} while (j <= 3);
// For...of Loop:
let directors = ["Anurag Kashyap", "Zoya Akhtar", "Vikramaditya Motwane"];
for (let director of directors) {
console.log(director); // Anurag Kashyap, Zoya Akhtar, Vikramaditya Motwane
}
// For...in Loop:
let actor = {
firstName: "Nawazuddin",
lastName: "Siddiqui",
age: 47
};
for (let key in actor) {
console.log(`${key}: ${actor[key]}`); // firstName: Nawazuddin, lastName: Siddiqui, age: 47
}
// Javascript Literals
let agentName = 'Srikant';
let mission = 'Operation Zulfiqar';
let newmessage = `Agent ${agentName}, your next mission is ${mission}.`;
console.log(newmessage);
let firstName = "Radhika";
let lastName = "Apte";
let fullName = `${firstName} ${lastName}`;
console.log(`Hello, I am ${fullName}, your host for the evening.`);
// Function Examples
function greet(name) {
return "Hello, " + name + "! Ready for the next 'Panchayat' task?";
}
let message = greet("Abhishek");
console.log(message);
function add(a, b) {
return a + b;
}
let sum = add(5, 10);
console.log(sum);
function multiply(a, b) {
return a * b;
}
let product = multiply(4, 5);
console.log(product);
// Function Expressions
// Function expression
const greet = function(name) {
console.log("Hello, " + name + "!");
}; // Note the semicolon at the end
greet("Bob"); // Call the function (same as before)
// Arrow function (ES6+)
const greet = (name) => console.log("Hello, " + name + "!");
// Or with implicit return (for single-line functions)
const add = (x, y) => x + y;
function sayHello(name = "friend") {
console.log(`Hello, ${name}!`);
}
sayHello(); // Output: Hello, friend!
sayHello("Emily"); // Output: Hello, Emily!
greet("Charlie");
let result = add(5, 3); // result is 8