lodapav 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +15 -0
- package/main.jsx +1494 -0
- package/package.json +15 -0
package/main.jsx
ADDED
|
@@ -0,0 +1,1494 @@
|
|
|
1
|
+
// =====================================
|
|
2
|
+
// Promise - Inventory Stock Checker
|
|
3
|
+
// =====================================
|
|
4
|
+
|
|
5
|
+
function stockAvailability(arr) {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
|
|
8
|
+
setTimeout(() => {
|
|
9
|
+
|
|
10
|
+
for (let item of arr) {
|
|
11
|
+
|
|
12
|
+
if (
|
|
13
|
+
!item ||
|
|
14
|
+
item.available === 0 ||
|
|
15
|
+
item.available == null
|
|
16
|
+
) {
|
|
17
|
+
reject("invalid");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (item.available < item.requested) {
|
|
22
|
+
resolve("partial");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
resolve("Available");
|
|
28
|
+
|
|
29
|
+
}, 1000);
|
|
30
|
+
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
// =====================================
|
|
36
|
+
// Promise - Movie Ticket Booking
|
|
37
|
+
// =====================================
|
|
38
|
+
|
|
39
|
+
function checkSeatAvailability(arr) {
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
|
|
42
|
+
setTimeout(() => {
|
|
43
|
+
|
|
44
|
+
for (let seat of arr) {
|
|
45
|
+
|
|
46
|
+
if (
|
|
47
|
+
seat.seatsAvailable <= 0 ||
|
|
48
|
+
seat.seatsAvailable == null
|
|
49
|
+
) {
|
|
50
|
+
reject("invalid");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (seat.seatsAvailable < seat.seatsRequested) {
|
|
55
|
+
resolve("partial");
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
resolve("available");
|
|
61
|
+
|
|
62
|
+
}, 600);
|
|
63
|
+
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
// =====================================
|
|
69
|
+
// Promise - Network Speed Tester
|
|
70
|
+
// =====================================
|
|
71
|
+
|
|
72
|
+
function checkUserSpeed(arr) {
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
|
|
77
|
+
for (let user of arr) {
|
|
78
|
+
|
|
79
|
+
if (user.actual <= 0) {
|
|
80
|
+
reject("rejected");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (user.actual < user.required) {
|
|
85
|
+
resolve("slow");
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
resolve("fast");
|
|
91
|
+
|
|
92
|
+
}, 700);
|
|
93
|
+
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
// =====================================
|
|
99
|
+
// Promise - Product Price Validator
|
|
100
|
+
// =====================================
|
|
101
|
+
|
|
102
|
+
function checkoutSystem(arr) {
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
|
|
105
|
+
setTimeout(() => {
|
|
106
|
+
|
|
107
|
+
if (arr.length === 0) {
|
|
108
|
+
reject("empty");
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (let item of arr) {
|
|
113
|
+
|
|
114
|
+
if (
|
|
115
|
+
typeof item.price !== "number" ||
|
|
116
|
+
item.price <= 0
|
|
117
|
+
) {
|
|
118
|
+
reject("invalid");
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
resolve("valid");
|
|
124
|
+
|
|
125
|
+
}, 1500);
|
|
126
|
+
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
// =====================================
|
|
132
|
+
// Promise - User Registration Validator
|
|
133
|
+
// =====================================
|
|
134
|
+
|
|
135
|
+
function userRegistration(arr) {
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
|
|
138
|
+
setTimeout(() => {
|
|
139
|
+
|
|
140
|
+
for (let user of arr) {
|
|
141
|
+
|
|
142
|
+
if (user.name.length < 3) {
|
|
143
|
+
reject("invalid name");
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!user.email.includes("@")) {
|
|
148
|
+
reject("invalid email");
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (user.age < 18) {
|
|
153
|
+
reject("invalid age");
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
resolve("registered");
|
|
159
|
+
|
|
160
|
+
}, 1200);
|
|
161
|
+
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// =====================================
|
|
166
|
+
// Axios - Contact Form Submission
|
|
167
|
+
// =====================================
|
|
168
|
+
|
|
169
|
+
function submitContactForm(formData) {
|
|
170
|
+
|
|
171
|
+
axios.post(
|
|
172
|
+
"https://formsubmit.co/YOUR_EMAIL",
|
|
173
|
+
formData
|
|
174
|
+
)
|
|
175
|
+
.then(function(res) {
|
|
176
|
+
console.log("Form submitted successfully:", res.data);
|
|
177
|
+
})
|
|
178
|
+
.catch(function(err) {
|
|
179
|
+
console.log("Failed to submit form:", err.message);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
// =====================================
|
|
186
|
+
// Axios - Register User
|
|
187
|
+
// =====================================
|
|
188
|
+
|
|
189
|
+
function registerUser(userData) {
|
|
190
|
+
|
|
191
|
+
if (
|
|
192
|
+
!userData.name ||
|
|
193
|
+
!userData.email ||
|
|
194
|
+
!userData.password ||
|
|
195
|
+
!userData.email.includes("@") ||
|
|
196
|
+
userData.password.length < 6
|
|
197
|
+
) {
|
|
198
|
+
console.log("Invalid input data");
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
console.log("Registering user...");
|
|
203
|
+
|
|
204
|
+
axios.post(
|
|
205
|
+
"https://jsonplaceholder.typicode.com/users",
|
|
206
|
+
userData
|
|
207
|
+
)
|
|
208
|
+
.then(function(res) {
|
|
209
|
+
console.log(
|
|
210
|
+
"User registered successfully:",
|
|
211
|
+
res.data
|
|
212
|
+
);
|
|
213
|
+
})
|
|
214
|
+
.catch(function(err) {
|
|
215
|
+
console.log(
|
|
216
|
+
"Registration failed:",
|
|
217
|
+
err.message
|
|
218
|
+
);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
// =====================================
|
|
225
|
+
// React Class-Based Component 1
|
|
226
|
+
// =====================================
|
|
227
|
+
|
|
228
|
+
class StudentTable extends React.Component {
|
|
229
|
+
|
|
230
|
+
render() {
|
|
231
|
+
|
|
232
|
+
const students = [
|
|
233
|
+
{ id: 1, name: "Ali", age: 20, course: "B.Tech" },
|
|
234
|
+
{ id: 2, name: "Sara", age: 22, course: "BCA" },
|
|
235
|
+
{ id: 3, name: "Ahmed", age: 21, course: "MCA" },
|
|
236
|
+
{ id: 4, name: "Zoya", age: 23, course: "MBA" }
|
|
237
|
+
];
|
|
238
|
+
|
|
239
|
+
return (
|
|
240
|
+
<table border="1" cellPadding="10">
|
|
241
|
+
<thead>
|
|
242
|
+
<tr>
|
|
243
|
+
<th>ID</th>
|
|
244
|
+
<th>Name</th>
|
|
245
|
+
<th>Age</th>
|
|
246
|
+
<th>Course</th>
|
|
247
|
+
</tr>
|
|
248
|
+
</thead>
|
|
249
|
+
|
|
250
|
+
<tbody>
|
|
251
|
+
{students.map((s) => (
|
|
252
|
+
<tr key={s.id}>
|
|
253
|
+
<td>{s.id}</td>
|
|
254
|
+
<td>{s.name}</td>
|
|
255
|
+
<td>{s.age}</td>
|
|
256
|
+
<td>{s.course}</td>
|
|
257
|
+
</tr>
|
|
258
|
+
))}
|
|
259
|
+
</tbody>
|
|
260
|
+
</table>
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
ReactDOM.render(
|
|
266
|
+
<StudentTable />,
|
|
267
|
+
document.getElementById("root")
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
// =====================================
|
|
272
|
+
// React Functional Component 1
|
|
273
|
+
// =====================================
|
|
274
|
+
|
|
275
|
+
function ProductList() {
|
|
276
|
+
|
|
277
|
+
const products = [
|
|
278
|
+
{
|
|
279
|
+
id: 1,
|
|
280
|
+
name: "Laptop",
|
|
281
|
+
price: 55000,
|
|
282
|
+
category: "Electronics"
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
id: 2,
|
|
286
|
+
name: "Shoes",
|
|
287
|
+
price: 2000,
|
|
288
|
+
category: "Fashion"
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
id: 3,
|
|
292
|
+
name: "Watch",
|
|
293
|
+
price: 3500,
|
|
294
|
+
category: "Accessories"
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
id: 4,
|
|
298
|
+
name: "Phone",
|
|
299
|
+
price: 30000,
|
|
300
|
+
category: "Electronics"
|
|
301
|
+
}
|
|
302
|
+
];
|
|
303
|
+
|
|
304
|
+
return (
|
|
305
|
+
<div>
|
|
306
|
+
<h2>Product Dashboard</h2>
|
|
307
|
+
|
|
308
|
+
{products.map((p) => (
|
|
309
|
+
<div
|
|
310
|
+
key={p.id}
|
|
311
|
+
style={{
|
|
312
|
+
border: "1px solid black",
|
|
313
|
+
padding: "10px",
|
|
314
|
+
margin: "10px"
|
|
315
|
+
}}
|
|
316
|
+
>
|
|
317
|
+
<h3>{p.name}</h3>
|
|
318
|
+
<p>Price: ₹{p.price}</p>
|
|
319
|
+
<p>Category: {p.category}</p>
|
|
320
|
+
|
|
321
|
+
{p.price > 30000
|
|
322
|
+
? <p>Premium Product</p>
|
|
323
|
+
: <p>Regular Product</p>}
|
|
324
|
+
</div>
|
|
325
|
+
))}
|
|
326
|
+
</div>
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
ReactDOM.render(
|
|
331
|
+
<ProductList />,
|
|
332
|
+
document.getElementById("root")
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
// =====================================
|
|
337
|
+
// React JSX - Advanced Result Dashboard
|
|
338
|
+
// =====================================
|
|
339
|
+
|
|
340
|
+
const students = [
|
|
341
|
+
{ id: 1, name: "Aman", marks: 85, course: "BCA", attendance: 92 },
|
|
342
|
+
{ id: 2, name: "Riya", marks: 45, course: "BCA", attendance: 78 },
|
|
343
|
+
{ id: 3, name: "Karan", marks: 72, course: "BBA", attendance: 55 },
|
|
344
|
+
{ id: 4, name: "Neha", marks: 30, course: "BCA", attendance: 95 },
|
|
345
|
+
{ id: 5, name: "Arjun", marks: 92, course: "BBA", attendance: 40 },
|
|
346
|
+
{ id: 6, name: "Sonia", marks: 55, course: "BCA", attendance: 65 }
|
|
347
|
+
];
|
|
348
|
+
|
|
349
|
+
function App() {
|
|
350
|
+
return (
|
|
351
|
+
<div>
|
|
352
|
+
|
|
353
|
+
<h2>Student Performance Dashboard</h2>
|
|
354
|
+
|
|
355
|
+
<table border="1" cellPadding="10">
|
|
356
|
+
<thead>
|
|
357
|
+
<tr>
|
|
358
|
+
<th>Name</th>
|
|
359
|
+
<th>Course</th>
|
|
360
|
+
<th>Marks</th>
|
|
361
|
+
<th>Attendance</th>
|
|
362
|
+
<th>Final Status</th>
|
|
363
|
+
</tr>
|
|
364
|
+
</thead>
|
|
365
|
+
|
|
366
|
+
<tbody>
|
|
367
|
+
{students.map((s) => (
|
|
368
|
+
<tr
|
|
369
|
+
key={s.id}
|
|
370
|
+
style={{
|
|
371
|
+
color:
|
|
372
|
+
s.marks < 50
|
|
373
|
+
? "red"
|
|
374
|
+
: s.attendance < 60
|
|
375
|
+
? "orange"
|
|
376
|
+
: s.marks > 80
|
|
377
|
+
? "green"
|
|
378
|
+
: "black"
|
|
379
|
+
}}
|
|
380
|
+
>
|
|
381
|
+
<td>{s.name}</td>
|
|
382
|
+
<td>{s.course}</td>
|
|
383
|
+
<td>{s.marks}</td>
|
|
384
|
+
<td>{s.attendance}%</td>
|
|
385
|
+
|
|
386
|
+
<td>
|
|
387
|
+
{s.marks < 50
|
|
388
|
+
? "Fail"
|
|
389
|
+
: s.attendance < 60
|
|
390
|
+
? "Attendance Shortage"
|
|
391
|
+
: "Pass"}
|
|
392
|
+
</td>
|
|
393
|
+
</tr>
|
|
394
|
+
))}
|
|
395
|
+
</tbody>
|
|
396
|
+
</table>
|
|
397
|
+
|
|
398
|
+
<h3>Statistics</h3>
|
|
399
|
+
|
|
400
|
+
<p>
|
|
401
|
+
Total Students: {students.length}
|
|
402
|
+
</p>
|
|
403
|
+
|
|
404
|
+
<p>
|
|
405
|
+
Class Average: {
|
|
406
|
+
(
|
|
407
|
+
students.reduce(
|
|
408
|
+
(sum, s) => sum + s.marks,
|
|
409
|
+
0
|
|
410
|
+
) / students.length
|
|
411
|
+
).toFixed(2)
|
|
412
|
+
}
|
|
413
|
+
</p>
|
|
414
|
+
|
|
415
|
+
<h3>Dean's List</h3>
|
|
416
|
+
|
|
417
|
+
{
|
|
418
|
+
students.filter(s => s.marks > 85).length > 0
|
|
419
|
+
?
|
|
420
|
+
students
|
|
421
|
+
.filter(s => s.marks > 85)
|
|
422
|
+
.map(s => (
|
|
423
|
+
<p key={s.id}>{s.name}</p>
|
|
424
|
+
))
|
|
425
|
+
:
|
|
426
|
+
<p>
|
|
427
|
+
No students currently qualify
|
|
428
|
+
for the Dean's List.
|
|
429
|
+
</p>
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
<h3>BCA Honors</h3>
|
|
433
|
+
|
|
434
|
+
{
|
|
435
|
+
students
|
|
436
|
+
.filter(
|
|
437
|
+
s =>
|
|
438
|
+
s.course === "BCA" &&
|
|
439
|
+
s.marks > 50
|
|
440
|
+
)
|
|
441
|
+
.map(s => (
|
|
442
|
+
<p key={s.id}>
|
|
443
|
+
{s.name} - {s.marks}
|
|
444
|
+
</p>
|
|
445
|
+
))
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
</div>
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
ReactDOM.render(
|
|
453
|
+
<App />,
|
|
454
|
+
document.getElementById("root")
|
|
455
|
+
);
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
// =====================================
|
|
459
|
+
// React Props - Greeting Messages
|
|
460
|
+
// =====================================
|
|
461
|
+
|
|
462
|
+
function WelcomeMessage(props) {
|
|
463
|
+
return (
|
|
464
|
+
<h3>
|
|
465
|
+
{props.language === "en"
|
|
466
|
+
? `Hello, ${props.name}!`
|
|
467
|
+
: `Bonjour, ${props.name}!`}
|
|
468
|
+
</h3>
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function App() {
|
|
473
|
+
return (
|
|
474
|
+
<div>
|
|
475
|
+
<WelcomeMessage name="Alice" language="en" />
|
|
476
|
+
<WelcomeMessage name="Chloe" language="fr" />
|
|
477
|
+
</div>
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
// =====================================
|
|
485
|
+
// React Props - Social Feed
|
|
486
|
+
// =====================================
|
|
487
|
+
|
|
488
|
+
const POSTS_DATA = [
|
|
489
|
+
{
|
|
490
|
+
id: 1,
|
|
491
|
+
username: "dev_traveler",
|
|
492
|
+
content: "Just finished my first React project!",
|
|
493
|
+
likes: 24,
|
|
494
|
+
comments: ["Great job!", "Keep it up!", "React is awesome."]
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
id: 2,
|
|
498
|
+
username: "chef_logic",
|
|
499
|
+
content: "The secret to a perfect steak is the sear.",
|
|
500
|
+
likes: 85,
|
|
501
|
+
comments: ["Recipe please?", "Cast iron or grill?"]
|
|
502
|
+
}
|
|
503
|
+
];
|
|
504
|
+
|
|
505
|
+
const CommentItem = ({ text }) => {
|
|
506
|
+
return <div>{text}</div>;
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
const PostCard = ({ post }) => {
|
|
510
|
+
return (
|
|
511
|
+
<>
|
|
512
|
+
<h3>{post.username}</h3>
|
|
513
|
+
<p>{post.content}</p>
|
|
514
|
+
|
|
515
|
+
<p
|
|
516
|
+
style={{
|
|
517
|
+
color: post.likes > 50 ? "red" : "black",
|
|
518
|
+
fontWeight: post.likes > 50 ? "bold" : "normal"
|
|
519
|
+
}}
|
|
520
|
+
>
|
|
521
|
+
Likes: {post.likes}
|
|
522
|
+
</p>
|
|
523
|
+
|
|
524
|
+
{post.comments.map((c, i) => (
|
|
525
|
+
<CommentItem key={i} text={c} />
|
|
526
|
+
))}
|
|
527
|
+
</>
|
|
528
|
+
);
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
function App() {
|
|
532
|
+
return (
|
|
533
|
+
<div>
|
|
534
|
+
{POSTS_DATA.map(post => (
|
|
535
|
+
<PostCard key={post.id} post={post} />
|
|
536
|
+
))}
|
|
537
|
+
</div>
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
// =====================================
|
|
545
|
+
// React Props - Profile Card
|
|
546
|
+
// =====================================
|
|
547
|
+
|
|
548
|
+
function ProfileCard(props) {
|
|
549
|
+
return (
|
|
550
|
+
<div style={{ border: "1px solid black", margin: "10px", padding: "10px" }}>
|
|
551
|
+
<h3>{props.name}</h3>
|
|
552
|
+
<p>Age: {props.age}</p>
|
|
553
|
+
|
|
554
|
+
<p
|
|
555
|
+
style={{
|
|
556
|
+
color: props.isOnline ? "green" : "gray"
|
|
557
|
+
}}
|
|
558
|
+
>
|
|
559
|
+
{props.isOnline ? "online" : "offline"}
|
|
560
|
+
</p>
|
|
561
|
+
</div>
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function App() {
|
|
566
|
+
return (
|
|
567
|
+
<div>
|
|
568
|
+
<ProfileCard name="Alice" age={23} isOnline={true} />
|
|
569
|
+
<ProfileCard name="Bob" age={21} isOnline={false} />
|
|
570
|
+
<ProfileCard name="Carlos" age={25} isOnline={true} />
|
|
571
|
+
</div>
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
// =====================================
|
|
579
|
+
// React Basic State - Counter With Limit
|
|
580
|
+
// =====================================
|
|
581
|
+
|
|
582
|
+
function Counter() {
|
|
583
|
+
|
|
584
|
+
const [count, setCount] = React.useState(0);
|
|
585
|
+
|
|
586
|
+
return (
|
|
587
|
+
<div>
|
|
588
|
+
<h2>{count}</h2>
|
|
589
|
+
|
|
590
|
+
<button
|
|
591
|
+
onClick={() => {
|
|
592
|
+
if (count < 10) {
|
|
593
|
+
setCount(count + 1);
|
|
594
|
+
}
|
|
595
|
+
}}
|
|
596
|
+
>
|
|
597
|
+
Increment (+)
|
|
598
|
+
</button>
|
|
599
|
+
|
|
600
|
+
<button
|
|
601
|
+
onClick={() => {
|
|
602
|
+
if (count > 0) {
|
|
603
|
+
setCount(count - 1);
|
|
604
|
+
}
|
|
605
|
+
}}
|
|
606
|
+
>
|
|
607
|
+
Decrement (-)
|
|
608
|
+
</button>
|
|
609
|
+
</div>
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
ReactDOM.render(<Counter />, document.getElementById("root"));
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
// =====================================
|
|
617
|
+
// React Basic State - Disable Button After Click
|
|
618
|
+
// =====================================
|
|
619
|
+
|
|
620
|
+
function App() {
|
|
621
|
+
|
|
622
|
+
const [disabled, setDisabled] = React.useState(false);
|
|
623
|
+
|
|
624
|
+
return (
|
|
625
|
+
<button
|
|
626
|
+
disabled={disabled}
|
|
627
|
+
onClick={() => setDisabled(true)}
|
|
628
|
+
>
|
|
629
|
+
Click Me
|
|
630
|
+
</button>
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
// =====================================
|
|
638
|
+
// React Basic State - Dropdown Selection
|
|
639
|
+
// =====================================
|
|
640
|
+
|
|
641
|
+
function App() {
|
|
642
|
+
|
|
643
|
+
const options = ["HTML", "CSS", "JavaScript", "React"];
|
|
644
|
+
|
|
645
|
+
const [selected, setSelected] = React.useState(options[0]);
|
|
646
|
+
|
|
647
|
+
return (
|
|
648
|
+
<div>
|
|
649
|
+
<h2>Select Technology:</h2>
|
|
650
|
+
|
|
651
|
+
<select
|
|
652
|
+
value={selected}
|
|
653
|
+
onChange={(e) => setSelected(e.target.value)}
|
|
654
|
+
>
|
|
655
|
+
{options.map((item, index) => (
|
|
656
|
+
<option key={index} value={item}>
|
|
657
|
+
{item}
|
|
658
|
+
</option>
|
|
659
|
+
))}
|
|
660
|
+
</select>
|
|
661
|
+
|
|
662
|
+
<h3>You selected: {selected}</h3>
|
|
663
|
+
</div>
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
// =====================================
|
|
671
|
+
// React Basic State - Background Color Changer
|
|
672
|
+
// =====================================
|
|
673
|
+
|
|
674
|
+
function App() {
|
|
675
|
+
|
|
676
|
+
const [bg, setBg] = React.useState("white");
|
|
677
|
+
|
|
678
|
+
function changeColor() {
|
|
679
|
+
|
|
680
|
+
const colors = [
|
|
681
|
+
"red",
|
|
682
|
+
"blue",
|
|
683
|
+
"green",
|
|
684
|
+
"yellow",
|
|
685
|
+
"orange",
|
|
686
|
+
"purple"
|
|
687
|
+
];
|
|
688
|
+
|
|
689
|
+
const random =
|
|
690
|
+
colors[Math.floor(Math.random() * colors.length)];
|
|
691
|
+
|
|
692
|
+
setBg(random);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
return (
|
|
696
|
+
<div
|
|
697
|
+
style={{
|
|
698
|
+
backgroundColor: bg,
|
|
699
|
+
height: "100vh"
|
|
700
|
+
}}
|
|
701
|
+
>
|
|
702
|
+
<button onClick={changeColor}>
|
|
703
|
+
Change Color
|
|
704
|
+
</button>
|
|
705
|
+
</div>
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
// =====================================
|
|
713
|
+
// React Basic State - Password Show/Hide
|
|
714
|
+
// =====================================
|
|
715
|
+
|
|
716
|
+
function App() {
|
|
717
|
+
|
|
718
|
+
const [show, setShow] = React.useState(false);
|
|
719
|
+
|
|
720
|
+
return (
|
|
721
|
+
<div>
|
|
722
|
+
<h1>Password Field</h1>
|
|
723
|
+
|
|
724
|
+
<input
|
|
725
|
+
type={show ? "text" : "password"}
|
|
726
|
+
/>
|
|
727
|
+
|
|
728
|
+
<br /><br />
|
|
729
|
+
|
|
730
|
+
<button
|
|
731
|
+
onClick={() => setShow(!show)}
|
|
732
|
+
>
|
|
733
|
+
{show ? "Hide" : "Show"}
|
|
734
|
+
</button>
|
|
735
|
+
</div>
|
|
736
|
+
);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
// =====================================
|
|
743
|
+
// React Basic State - Input Live Preview
|
|
744
|
+
// =====================================
|
|
745
|
+
|
|
746
|
+
function App() {
|
|
747
|
+
|
|
748
|
+
const [text, setText] = React.useState("");
|
|
749
|
+
|
|
750
|
+
return (
|
|
751
|
+
<div>
|
|
752
|
+
<h1>Live Input Preview</h1>
|
|
753
|
+
|
|
754
|
+
<input
|
|
755
|
+
type="text"
|
|
756
|
+
value={text}
|
|
757
|
+
onChange={(e) =>
|
|
758
|
+
setText(e.target.value)
|
|
759
|
+
}
|
|
760
|
+
/>
|
|
761
|
+
|
|
762
|
+
<h2>You typed: {text}</h2>
|
|
763
|
+
</div>
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
// =====================================
|
|
771
|
+
// React Basic State - Random Quote Generator
|
|
772
|
+
// =====================================
|
|
773
|
+
|
|
774
|
+
function App() {
|
|
775
|
+
|
|
776
|
+
const quotes = [
|
|
777
|
+
"Dream big and dare to fail.",
|
|
778
|
+
"Never stop learning.",
|
|
779
|
+
"Success comes from hard work.",
|
|
780
|
+
"Stay hungry stay foolish.",
|
|
781
|
+
"Believe in yourself."
|
|
782
|
+
];
|
|
783
|
+
|
|
784
|
+
const [quote, setQuote] = React.useState(quotes[0]);
|
|
785
|
+
|
|
786
|
+
function generateQuote() {
|
|
787
|
+
const random =
|
|
788
|
+
Math.floor(Math.random() * quotes.length);
|
|
789
|
+
|
|
790
|
+
setQuote(quotes[random]);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
return (
|
|
794
|
+
<div>
|
|
795
|
+
<h1>Random Quote Generator</h1>
|
|
796
|
+
|
|
797
|
+
<h2>{quote}</h2>
|
|
798
|
+
|
|
799
|
+
<button onClick={generateQuote}>
|
|
800
|
+
Generate Quote
|
|
801
|
+
</button>
|
|
802
|
+
</div>
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
// =====================================
|
|
810
|
+
// React Basic State - Character Limit
|
|
811
|
+
// =====================================
|
|
812
|
+
|
|
813
|
+
function App() {
|
|
814
|
+
|
|
815
|
+
const limit = 20;
|
|
816
|
+
|
|
817
|
+
const [text, setText] = React.useState("");
|
|
818
|
+
|
|
819
|
+
function handleChange(e) {
|
|
820
|
+
|
|
821
|
+
if (e.target.value.length <= limit) {
|
|
822
|
+
setText(e.target.value);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
return (
|
|
827
|
+
<div>
|
|
828
|
+
|
|
829
|
+
<h1>Character Limit Input</h1>
|
|
830
|
+
|
|
831
|
+
<input
|
|
832
|
+
type="text"
|
|
833
|
+
value={text}
|
|
834
|
+
onChange={handleChange}
|
|
835
|
+
/>
|
|
836
|
+
|
|
837
|
+
<h2>
|
|
838
|
+
Remaining: {limit - text.length}
|
|
839
|
+
</h2>
|
|
840
|
+
|
|
841
|
+
{text.length === limit && (
|
|
842
|
+
<h2 style={{ color: "red" }}>
|
|
843
|
+
Limit reached!
|
|
844
|
+
</h2>
|
|
845
|
+
)}
|
|
846
|
+
|
|
847
|
+
</div>
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
852
|
+
|
|
853
|
+
// =====================================
|
|
854
|
+
// React Complex State - Product Dashboard
|
|
855
|
+
// =====================================
|
|
856
|
+
|
|
857
|
+
function ProductTiles() {
|
|
858
|
+
|
|
859
|
+
const products = [
|
|
860
|
+
{ id: 1, name: "Laptop", price: 80000, category: "Premium" },
|
|
861
|
+
{ id: 2, name: "Mouse", price: 500, category: "Basic" },
|
|
862
|
+
{ id: 3, name: "Keyboard", price: 1500, category: "Basic" },
|
|
863
|
+
{ id: 4, name: "Smartphone", price: 60000, category: "Premium" },
|
|
864
|
+
{ id: 5, name: "Monitor", price: 12000, category: "Deluxe" },
|
|
865
|
+
{ id: 6, name: "Headphones", price: 3000, category: "Deluxe" }
|
|
866
|
+
];
|
|
867
|
+
|
|
868
|
+
const [category, setCategory] = React.useState("All");
|
|
869
|
+
|
|
870
|
+
const filtered =
|
|
871
|
+
category === "All"
|
|
872
|
+
? products
|
|
873
|
+
: products.filter(
|
|
874
|
+
p => p.category === category
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
return (
|
|
878
|
+
<div>
|
|
879
|
+
|
|
880
|
+
<h2>Product Dashboard</h2>
|
|
881
|
+
|
|
882
|
+
<select
|
|
883
|
+
value={category}
|
|
884
|
+
onChange={(e) =>
|
|
885
|
+
setCategory(e.target.value)
|
|
886
|
+
}
|
|
887
|
+
>
|
|
888
|
+
<option>All</option>
|
|
889
|
+
<option>Premium</option>
|
|
890
|
+
<option>Deluxe</option>
|
|
891
|
+
<option>Basic</option>
|
|
892
|
+
</select>
|
|
893
|
+
|
|
894
|
+
{filtered.map(product => (
|
|
895
|
+
<div key={product.id}>
|
|
896
|
+
<h3>{product.name}</h3>
|
|
897
|
+
<p>₹{product.price}</p>
|
|
898
|
+
<p>{product.category}</p>
|
|
899
|
+
</div>
|
|
900
|
+
))}
|
|
901
|
+
</div>
|
|
902
|
+
);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
ReactDOM.render(
|
|
906
|
+
<ProductTiles />,
|
|
907
|
+
document.getElementById("root")
|
|
908
|
+
);
|
|
909
|
+
|
|
910
|
+
|
|
911
|
+
// =====================================
|
|
912
|
+
// React Complex State - Search Functionality
|
|
913
|
+
// =====================================
|
|
914
|
+
|
|
915
|
+
function SearchUser() {
|
|
916
|
+
|
|
917
|
+
const usersData = [
|
|
918
|
+
{ id: 1, name: "Amit Sharma", city: "Delhi" },
|
|
919
|
+
{ id: 2, name: "Neha Verma", city: "Mumbai" },
|
|
920
|
+
{ id: 3, name: "Rahul Singh", city: "Chandigarh" },
|
|
921
|
+
{ id: 4, name: "Priya Mehta", city: "Pune" }
|
|
922
|
+
];
|
|
923
|
+
|
|
924
|
+
const [search, setSearch] = React.useState("");
|
|
925
|
+
|
|
926
|
+
const filtered = usersData.filter(user =>
|
|
927
|
+
user.name
|
|
928
|
+
.toLowerCase()
|
|
929
|
+
.includes(search.toLowerCase())
|
|
930
|
+
);
|
|
931
|
+
|
|
932
|
+
return (
|
|
933
|
+
<div>
|
|
934
|
+
|
|
935
|
+
<h2>User Search</h2>
|
|
936
|
+
|
|
937
|
+
<input
|
|
938
|
+
type="text"
|
|
939
|
+
placeholder="Search by name..."
|
|
940
|
+
value={search}
|
|
941
|
+
onChange={(e) =>
|
|
942
|
+
setSearch(e.target.value)
|
|
943
|
+
}
|
|
944
|
+
/>
|
|
945
|
+
|
|
946
|
+
{filtered.map(user => (
|
|
947
|
+
<div key={user.id}>
|
|
948
|
+
<b>{user.name}</b> - {user.city}
|
|
949
|
+
</div>
|
|
950
|
+
))}
|
|
951
|
+
|
|
952
|
+
</div>
|
|
953
|
+
);
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
ReactDOM.render(
|
|
957
|
+
<SearchUser />,
|
|
958
|
+
document.getElementById("root")
|
|
959
|
+
);
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
// =====================================
|
|
963
|
+
// React Complex State - Employee Form
|
|
964
|
+
// =====================================
|
|
965
|
+
|
|
966
|
+
function EmployeeForm() {
|
|
967
|
+
|
|
968
|
+
const [form, setForm] = React.useState({
|
|
969
|
+
name: "",
|
|
970
|
+
department: "",
|
|
971
|
+
salary: ""
|
|
972
|
+
});
|
|
973
|
+
|
|
974
|
+
const [employee, setEmployee] =
|
|
975
|
+
React.useState(null);
|
|
976
|
+
|
|
977
|
+
function handleChange(e) {
|
|
978
|
+
setForm({
|
|
979
|
+
...form,
|
|
980
|
+
[e.target.name]: e.target.value
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
function handleSubmit(e) {
|
|
985
|
+
e.preventDefault();
|
|
986
|
+
setEmployee(form);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
function handleReset() {
|
|
990
|
+
|
|
991
|
+
setForm({
|
|
992
|
+
name: "",
|
|
993
|
+
department: "",
|
|
994
|
+
salary: ""
|
|
995
|
+
});
|
|
996
|
+
|
|
997
|
+
setEmployee(null);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
return (
|
|
1001
|
+
<div>
|
|
1002
|
+
|
|
1003
|
+
<form onSubmit={handleSubmit}>
|
|
1004
|
+
|
|
1005
|
+
<input
|
|
1006
|
+
name="name"
|
|
1007
|
+
value={form.name}
|
|
1008
|
+
onChange={handleChange}
|
|
1009
|
+
placeholder="Name"
|
|
1010
|
+
/>
|
|
1011
|
+
|
|
1012
|
+
<input
|
|
1013
|
+
name="department"
|
|
1014
|
+
value={form.department}
|
|
1015
|
+
onChange={handleChange}
|
|
1016
|
+
placeholder="Department"
|
|
1017
|
+
/>
|
|
1018
|
+
|
|
1019
|
+
<input
|
|
1020
|
+
name="salary"
|
|
1021
|
+
value={form.salary}
|
|
1022
|
+
onChange={handleChange}
|
|
1023
|
+
placeholder="Salary"
|
|
1024
|
+
/>
|
|
1025
|
+
|
|
1026
|
+
<button type="submit">
|
|
1027
|
+
Submit
|
|
1028
|
+
</button>
|
|
1029
|
+
|
|
1030
|
+
<button
|
|
1031
|
+
type="button"
|
|
1032
|
+
onClick={handleReset}
|
|
1033
|
+
>
|
|
1034
|
+
Reset
|
|
1035
|
+
</button>
|
|
1036
|
+
|
|
1037
|
+
</form>
|
|
1038
|
+
|
|
1039
|
+
{employee && (
|
|
1040
|
+
<div>
|
|
1041
|
+
<h3>{employee.name}</h3>
|
|
1042
|
+
<p>{employee.department}</p>
|
|
1043
|
+
<p>{employee.salary}</p>
|
|
1044
|
+
</div>
|
|
1045
|
+
)}
|
|
1046
|
+
|
|
1047
|
+
</div>
|
|
1048
|
+
);
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
ReactDOM.render(
|
|
1052
|
+
<EmployeeForm />,
|
|
1053
|
+
document.getElementById("root")
|
|
1054
|
+
);
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
// =====================================
|
|
1058
|
+
// React Complex State - Update User
|
|
1059
|
+
// =====================================
|
|
1060
|
+
|
|
1061
|
+
function App() {
|
|
1062
|
+
|
|
1063
|
+
const [users, setUsers] = React.useState([
|
|
1064
|
+
{ id: 1, name: "A" },
|
|
1065
|
+
{ id: 2, name: "B" }
|
|
1066
|
+
]);
|
|
1067
|
+
|
|
1068
|
+
function updateUser() {
|
|
1069
|
+
|
|
1070
|
+
const updatedUsers = users.map(user =>
|
|
1071
|
+
user.id === 2
|
|
1072
|
+
? { ...user, name: "Updated" }
|
|
1073
|
+
: user
|
|
1074
|
+
);
|
|
1075
|
+
|
|
1076
|
+
setUsers(updatedUsers);
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
return (
|
|
1080
|
+
<div>
|
|
1081
|
+
|
|
1082
|
+
<h3>User List</h3>
|
|
1083
|
+
|
|
1084
|
+
{users.map(user => (
|
|
1085
|
+
<p key={user.id}>
|
|
1086
|
+
User {user.id}: {user.name}
|
|
1087
|
+
</p>
|
|
1088
|
+
))}
|
|
1089
|
+
|
|
1090
|
+
<button onClick={updateUser}>
|
|
1091
|
+
Update User
|
|
1092
|
+
</button>
|
|
1093
|
+
|
|
1094
|
+
</div>
|
|
1095
|
+
);
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
ReactDOM.render(
|
|
1099
|
+
<App />,
|
|
1100
|
+
document.getElementById("root")
|
|
1101
|
+
);
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
// =====================================
|
|
1105
|
+
// React Complex State - Toggle Status
|
|
1106
|
+
// =====================================
|
|
1107
|
+
|
|
1108
|
+
function App() {
|
|
1109
|
+
|
|
1110
|
+
const [task, setTask] = React.useState({
|
|
1111
|
+
title: "Learn React",
|
|
1112
|
+
completed: false
|
|
1113
|
+
});
|
|
1114
|
+
|
|
1115
|
+
function toggleStatus() {
|
|
1116
|
+
|
|
1117
|
+
setTask({
|
|
1118
|
+
...task,
|
|
1119
|
+
completed: !task.completed
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
return (
|
|
1124
|
+
<div>
|
|
1125
|
+
|
|
1126
|
+
<h3>
|
|
1127
|
+
{task.title} - {
|
|
1128
|
+
task.completed
|
|
1129
|
+
? "Completed"
|
|
1130
|
+
: "Pending"
|
|
1131
|
+
}
|
|
1132
|
+
</h3>
|
|
1133
|
+
|
|
1134
|
+
<button onClick={toggleStatus}>
|
|
1135
|
+
Toggle Status
|
|
1136
|
+
</button>
|
|
1137
|
+
|
|
1138
|
+
</div>
|
|
1139
|
+
);
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
ReactDOM.render(
|
|
1143
|
+
<App />,
|
|
1144
|
+
document.getElementById("root")
|
|
1145
|
+
);
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
// =====================================
|
|
1149
|
+
// TS - Function Overloading
|
|
1150
|
+
// =====================================
|
|
1151
|
+
|
|
1152
|
+
function add(a: number, b: number): number;
|
|
1153
|
+
function add(a: string, b: string): string;
|
|
1154
|
+
|
|
1155
|
+
function add(a: any, b: any): any {
|
|
1156
|
+
return a + b;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
function calculate(): void {
|
|
1160
|
+
|
|
1161
|
+
const val1 = (
|
|
1162
|
+
document.getElementById("input1") as HTMLInputElement
|
|
1163
|
+
).value;
|
|
1164
|
+
|
|
1165
|
+
const val2 = (
|
|
1166
|
+
document.getElementById("input2") as HTMLInputElement
|
|
1167
|
+
).value;
|
|
1168
|
+
|
|
1169
|
+
const num1 = Number(val1);
|
|
1170
|
+
const num2 = Number(val2);
|
|
1171
|
+
|
|
1172
|
+
let result;
|
|
1173
|
+
|
|
1174
|
+
if (!isNaN(num1) && !isNaN(num2)) {
|
|
1175
|
+
result = add(num1, num2);
|
|
1176
|
+
} else {
|
|
1177
|
+
result = add(val1, val2);
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
(
|
|
1181
|
+
document.getElementById("result") as HTMLElement
|
|
1182
|
+
).innerText = String(result);
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
// =====================================
|
|
1187
|
+
// TS - Smart Home Automation
|
|
1188
|
+
// =====================================
|
|
1189
|
+
|
|
1190
|
+
enum DeviceStatus {
|
|
1191
|
+
On = "On",
|
|
1192
|
+
Off = "Off",
|
|
1193
|
+
Standby = "Standby"
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
type SmartDevice = [
|
|
1197
|
+
string,
|
|
1198
|
+
string,
|
|
1199
|
+
DeviceStatus
|
|
1200
|
+
];
|
|
1201
|
+
|
|
1202
|
+
function toggleDeviceStatus(
|
|
1203
|
+
device: SmartDevice,
|
|
1204
|
+
newStatus: DeviceStatus
|
|
1205
|
+
): SmartDevice {
|
|
1206
|
+
|
|
1207
|
+
if (
|
|
1208
|
+
device[2] === DeviceStatus.Off &&
|
|
1209
|
+
newStatus === DeviceStatus.Standby
|
|
1210
|
+
) {
|
|
1211
|
+
return device;
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
device[2] = newStatus;
|
|
1215
|
+
|
|
1216
|
+
return device;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
let device: SmartDevice = [
|
|
1220
|
+
"Light",
|
|
1221
|
+
"LED",
|
|
1222
|
+
DeviceStatus.Off
|
|
1223
|
+
];
|
|
1224
|
+
|
|
1225
|
+
console.log(
|
|
1226
|
+
toggleDeviceStatus(
|
|
1227
|
+
device,
|
|
1228
|
+
DeviceStatus.On
|
|
1229
|
+
)
|
|
1230
|
+
);
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
// =====================================
|
|
1234
|
+
// TS - Movie Ticket Booking
|
|
1235
|
+
// =====================================
|
|
1236
|
+
|
|
1237
|
+
enum SeatType {
|
|
1238
|
+
Regular = "Regular",
|
|
1239
|
+
Premium = "Premium",
|
|
1240
|
+
VIP = "VIP"
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
type Ticket = [
|
|
1244
|
+
string,
|
|
1245
|
+
number,
|
|
1246
|
+
SeatType,
|
|
1247
|
+
number
|
|
1248
|
+
];
|
|
1249
|
+
|
|
1250
|
+
function calculateDiscount(
|
|
1251
|
+
ticket: Ticket
|
|
1252
|
+
): number {
|
|
1253
|
+
|
|
1254
|
+
let price = ticket[3];
|
|
1255
|
+
|
|
1256
|
+
if (ticket[2] === SeatType.Premium) {
|
|
1257
|
+
price = price - price * 0.10;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
if (ticket[2] === SeatType.VIP) {
|
|
1261
|
+
price = price - price * 0.20;
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
return price;
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
let ticket: Ticket = [
|
|
1268
|
+
"Avengers",
|
|
1269
|
+
5,
|
|
1270
|
+
SeatType.VIP,
|
|
1271
|
+
1000
|
|
1272
|
+
];
|
|
1273
|
+
|
|
1274
|
+
console.log(
|
|
1275
|
+
calculateDiscount(ticket)
|
|
1276
|
+
);
|
|
1277
|
+
|
|
1278
|
+
|
|
1279
|
+
// =====================================
|
|
1280
|
+
// TS - Library Management System
|
|
1281
|
+
// =====================================
|
|
1282
|
+
|
|
1283
|
+
enum BookStatus {
|
|
1284
|
+
Available = "Available",
|
|
1285
|
+
CheckedOut = "CheckedOut",
|
|
1286
|
+
Reserved = "Reserved"
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
type Book = [
|
|
1290
|
+
string,
|
|
1291
|
+
string,
|
|
1292
|
+
number,
|
|
1293
|
+
BookStatus
|
|
1294
|
+
];
|
|
1295
|
+
|
|
1296
|
+
function borrowBook(
|
|
1297
|
+
book: Book
|
|
1298
|
+
): string {
|
|
1299
|
+
|
|
1300
|
+
if (
|
|
1301
|
+
book[3] === BookStatus.Available
|
|
1302
|
+
) {
|
|
1303
|
+
book[3] = BookStatus.CheckedOut;
|
|
1304
|
+
return "Book borrowed";
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
return "Book not available";
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
let book: Book = [
|
|
1311
|
+
"Java",
|
|
1312
|
+
"James",
|
|
1313
|
+
12345,
|
|
1314
|
+
BookStatus.Available
|
|
1315
|
+
];
|
|
1316
|
+
|
|
1317
|
+
console.log(
|
|
1318
|
+
borrowBook(book)
|
|
1319
|
+
);
|
|
1320
|
+
|
|
1321
|
+
|
|
1322
|
+
// =====================================
|
|
1323
|
+
// TS - Character Counter Textarea
|
|
1324
|
+
// =====================================
|
|
1325
|
+
|
|
1326
|
+
const postContent =
|
|
1327
|
+
document.getElementById(
|
|
1328
|
+
"postContent"
|
|
1329
|
+
) as HTMLTextAreaElement;
|
|
1330
|
+
|
|
1331
|
+
const charCount =
|
|
1332
|
+
document.getElementById(
|
|
1333
|
+
"charCount"
|
|
1334
|
+
) as HTMLElement;
|
|
1335
|
+
|
|
1336
|
+
const warningMsg =
|
|
1337
|
+
document.getElementById(
|
|
1338
|
+
"warningMsg"
|
|
1339
|
+
) as HTMLElement;
|
|
1340
|
+
|
|
1341
|
+
postContent.addEventListener(
|
|
1342
|
+
"input",
|
|
1343
|
+
() => {
|
|
1344
|
+
|
|
1345
|
+
const count =
|
|
1346
|
+
postContent.value.length;
|
|
1347
|
+
|
|
1348
|
+
charCount.innerText =
|
|
1349
|
+
count.toString();
|
|
1350
|
+
|
|
1351
|
+
if (count > 200) {
|
|
1352
|
+
warningMsg.innerText =
|
|
1353
|
+
"Character limit exceeded";
|
|
1354
|
+
} else {
|
|
1355
|
+
warningMsg.innerText = "";
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
);
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
// =====================================
|
|
1362
|
+
// TS - Dropdown Selection Display
|
|
1363
|
+
// =====================================
|
|
1364
|
+
|
|
1365
|
+
const countrySelect =
|
|
1366
|
+
document.getElementById(
|
|
1367
|
+
"countrySelect"
|
|
1368
|
+
) as HTMLSelectElement;
|
|
1369
|
+
|
|
1370
|
+
const selectedCountry =
|
|
1371
|
+
document.getElementById(
|
|
1372
|
+
"selectedCountry"
|
|
1373
|
+
) as HTMLElement;
|
|
1374
|
+
|
|
1375
|
+
countrySelect.addEventListener(
|
|
1376
|
+
"change",
|
|
1377
|
+
() => {
|
|
1378
|
+
|
|
1379
|
+
selectedCountry.innerText =
|
|
1380
|
+
countrySelect.value;
|
|
1381
|
+
}
|
|
1382
|
+
);
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
// =====================================
|
|
1386
|
+
// TS - Numeric Input Validation
|
|
1387
|
+
// =====================================
|
|
1388
|
+
|
|
1389
|
+
const ageInput =
|
|
1390
|
+
document.getElementById(
|
|
1391
|
+
"ageInput"
|
|
1392
|
+
) as HTMLInputElement;
|
|
1393
|
+
|
|
1394
|
+
const errorMsg =
|
|
1395
|
+
document.getElementById(
|
|
1396
|
+
"errorMsg"
|
|
1397
|
+
) as HTMLElement;
|
|
1398
|
+
|
|
1399
|
+
ageInput.addEventListener(
|
|
1400
|
+
"input",
|
|
1401
|
+
() => {
|
|
1402
|
+
|
|
1403
|
+
const value =
|
|
1404
|
+
ageInput.value;
|
|
1405
|
+
|
|
1406
|
+
const valid =
|
|
1407
|
+
/^[0-9]*$/.test(value);
|
|
1408
|
+
|
|
1409
|
+
if (!valid) {
|
|
1410
|
+
errorMsg.innerText =
|
|
1411
|
+
"Only numbers allowed";
|
|
1412
|
+
} else {
|
|
1413
|
+
errorMsg.innerText = "";
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
);
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
// =====================================
|
|
1420
|
+
// React - Password Validator
|
|
1421
|
+
// =====================================
|
|
1422
|
+
|
|
1423
|
+
function PasswordValidator() {
|
|
1424
|
+
|
|
1425
|
+
const [password, setPassword] =
|
|
1426
|
+
React.useState("");
|
|
1427
|
+
|
|
1428
|
+
return (
|
|
1429
|
+
<div>
|
|
1430
|
+
|
|
1431
|
+
<h1>Password Validator</h1>
|
|
1432
|
+
|
|
1433
|
+
<input
|
|
1434
|
+
type="password"
|
|
1435
|
+
placeholder="Enter Password"
|
|
1436
|
+
value={password}
|
|
1437
|
+
onChange={(e) =>
|
|
1438
|
+
setPassword(e.target.value)
|
|
1439
|
+
}
|
|
1440
|
+
/>
|
|
1441
|
+
|
|
1442
|
+
<p
|
|
1443
|
+
style={{
|
|
1444
|
+
color:
|
|
1445
|
+
/^[A-Z]/.test(password)
|
|
1446
|
+
? "green"
|
|
1447
|
+
: "red"
|
|
1448
|
+
}}
|
|
1449
|
+
>
|
|
1450
|
+
First character should be Capital
|
|
1451
|
+
</p>
|
|
1452
|
+
|
|
1453
|
+
<p
|
|
1454
|
+
style={{
|
|
1455
|
+
color:
|
|
1456
|
+
/[a-z]/.test(password)
|
|
1457
|
+
? "green"
|
|
1458
|
+
: "red"
|
|
1459
|
+
}}
|
|
1460
|
+
>
|
|
1461
|
+
Must contain one small letter
|
|
1462
|
+
</p>
|
|
1463
|
+
|
|
1464
|
+
<p
|
|
1465
|
+
style={{
|
|
1466
|
+
color:
|
|
1467
|
+
/[0-9]/.test(password)
|
|
1468
|
+
? "green"
|
|
1469
|
+
: "red"
|
|
1470
|
+
}}
|
|
1471
|
+
>
|
|
1472
|
+
Must contain one number
|
|
1473
|
+
</p>
|
|
1474
|
+
|
|
1475
|
+
<p
|
|
1476
|
+
style={{
|
|
1477
|
+
color:
|
|
1478
|
+
/[^A-Za-z0-9]/.test(password)
|
|
1479
|
+
? "green"
|
|
1480
|
+
: "red"
|
|
1481
|
+
}}
|
|
1482
|
+
>
|
|
1483
|
+
Must contain one special character
|
|
1484
|
+
</p>
|
|
1485
|
+
|
|
1486
|
+
</div>
|
|
1487
|
+
);
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
ReactDOM.render(
|
|
1491
|
+
<PasswordValidator />,
|
|
1492
|
+
document.getElementById("root")
|
|
1493
|
+
);
|
|
1494
|
+
|