endfee 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.
Files changed (2) hide show
  1. package/package.json +12 -0
  2. package/testpad.js +1914 -0
package/testpad.js ADDED
@@ -0,0 +1,1914 @@
1
+ username
2
+
3
+
4
+ //React props - Greeting Messages in English and French
5
+ //1.1
6
+ import './App.css'
7
+ import WelcomeMessage from './WelcomeMessage'
8
+
9
+ function App() {
10
+
11
+ return (
12
+ <>
13
+ <section id="center">
14
+
15
+ <WelcomeMessage name="Alice" language="en" />
16
+ <WelcomeMessage name="Chloe" language="fr" />
17
+
18
+ </section>
19
+ </>
20
+ )
21
+ }
22
+
23
+ export default App
24
+
25
+ // 1.2
26
+ function WelcomeMessage({ name, language }) {
27
+
28
+ let message = "";
29
+
30
+ if (language === "en") {
31
+ message = `Hello, ${name}!`;
32
+ } else if (language === "fr") {
33
+ message = `Bonjour, ${name}!`;
34
+ } else {
35
+ message = `Hello, ${name}!`; // default
36
+ }
37
+
38
+ return <h3>{message}</h3>;
39
+ }
40
+
41
+ export default WelcomeMessage;
42
+
43
+
44
+
45
+
46
+ //React props - Social Feed
47
+ //2.1
48
+ import PostCard from './PostCard'
49
+
50
+ function App() {
51
+
52
+ const Post_Data = [
53
+ {
54
+ id: 1,
55
+ username: "dev_traveler",
56
+ content: "Just finished my first React project!",
57
+ likes: 24,
58
+ comments: ["Great job!", "Keep it up!", "React is awesome."]
59
+ },
60
+ {
61
+ id: 2,
62
+ username: "chef_logic",
63
+ content: "The secret to a perfect steak is the sear.",
64
+ likes: 85,
65
+ comments: ["Recipe please?", "Cast iron or grill?"]
66
+ }
67
+ ];
68
+
69
+ return (
70
+ <>
71
+ <h1 style={{ textAlign: "center" }}>Activity Feed</h1>
72
+
73
+ {Post_Data.map((post) => (
74
+ <PostCard key={post.id} post={post} />
75
+ ))}
76
+ </>
77
+ );
78
+ }
79
+
80
+ export default App;
81
+
82
+ //2.2
83
+ const PostCard = ({ post }) => {
84
+
85
+ return (
86
+ <div
87
+ style={{
88
+ border: "1px solid #ccc",
89
+ borderRadius: "10px",
90
+ padding: "15px",
91
+ margin: "20px",
92
+ backgroundColor: "#f9f9f9"
93
+ }}
94
+ >
95
+ <h2>{post.username}</h2>
96
+
97
+ <p>{post.content}</p>
98
+
99
+ <p
100
+ style={{
101
+ color: post.likes > 50 ? "red" : "black",
102
+ fontWeight: post.likes > 50 ? "bold" : "normal"
103
+ }}
104
+ >
105
+ Likes: {post.likes}
106
+ </p>
107
+
108
+ <hr />
109
+
110
+ <h4>User Comments</h4>
111
+
112
+ {post.comments.map((text, index) => (
113
+ <div
114
+ style={{
115
+ backgroundColor: "#100101",
116
+ padding: "8px",
117
+ margin: "5px 0",
118
+ borderRadius: "5px"
119
+ }}
120
+ key={index}
121
+ >
122
+ {text}
123
+ </div>
124
+ ))}
125
+ </div>
126
+ );
127
+ };
128
+
129
+ export default PostCard;
130
+
131
+
132
+
133
+ //React props - Profile Card Component
134
+ //3.1
135
+ import './App.css'
136
+ import ProfileCard from './ProfileCard'
137
+
138
+ function App() {
139
+
140
+ return (
141
+ <>
142
+ <h1 style={{ textAlign: "center" }}>Profile Cards</h1>
143
+
144
+ <div style={{ display: "flex", justifyContent: "center" }}>
145
+
146
+ <ProfileCard name="Alice" age={23} isOnline={true} />
147
+ <ProfileCard name="Bob" age={21} isOnline={false} />
148
+ <ProfileCard name="Carlos" age={25} isOnline={true} />
149
+
150
+ </div>
151
+ </>
152
+ )
153
+ }
154
+
155
+ export default App
156
+
157
+ //3.2
158
+ const ProfileCard = ({ name, age, isOnline }) => {
159
+
160
+ return (
161
+ <div
162
+ style={{
163
+ border: "1px solid #ccc",
164
+ borderRadius: "10px",
165
+ padding: "15px",
166
+ margin: "15px",
167
+ width: "250px",
168
+ boxShadow: "0 2px 5px rgba(0,0,0,0.1)"
169
+ }}
170
+ >
171
+ <h2>{name}</h2>
172
+ <p>Age: {age}</p>
173
+
174
+ <p
175
+ style={{
176
+ color: isOnline ? "green" : "gray",
177
+ fontWeight: "bold"
178
+ }}
179
+ >
180
+ {isOnline ? "Online" : "Offline"}
181
+ </p>
182
+ </div>
183
+ );
184
+ };
185
+
186
+ export default ProfileCard;
187
+
188
+
189
+
190
+
191
+ // React Basic State - Disable Button After Click
192
+ //4
193
+ import { useState } from 'react'
194
+ import './App.css'
195
+
196
+ function App() {
197
+
198
+ const [isDisabled, setIsDisabled] = useState(false);
199
+
200
+ const handleClick = () => {
201
+ setIsDisabled(true);
202
+ };
203
+
204
+ return (
205
+ <>
206
+ <h2 style={{ textAlign: "center" }}>Disable Button After Click</h2>
207
+
208
+ <div style={{ textAlign: "center", marginTop: "20px" }}>
209
+ <button
210
+ onClick={handleClick}
211
+ disabled={isDisabled}
212
+ style={{
213
+ padding: "10px 20px",
214
+ cursor: isDisabled ? "not-allowed" : "pointer"
215
+ }}
216
+ >
217
+ {isDisabled ? "Clicked" : "Click Me"}
218
+ </button>
219
+ </div>
220
+ </>
221
+ )
222
+ }
223
+
224
+ export default App
225
+
226
+
227
+
228
+ // React Basic State - Dropdown Selection
229
+ //5
230
+ import { useState } from 'react'
231
+ import './App.css'
232
+
233
+ function App() {
234
+
235
+ const options = ["HTML", "CSS", "JavaScript", "React"];
236
+
237
+ const [selected, setSelected] = useState(options[0]);
238
+
239
+ const handleChange = (e) => {
240
+ setSelected(e.target.value);
241
+ };
242
+
243
+ return (
244
+ <>
245
+ <div style={{ textAlign: "center", marginTop: "40px" }}>
246
+
247
+ <h2>Select Technology:</h2>
248
+
249
+ <select
250
+ value={selected}
251
+ onChange={handleChange}
252
+ style={{ padding: "8px", fontSize: "16px" }}
253
+ >
254
+ {options.map((opt, index) => (
255
+ <option key={index} value={opt}>
256
+ {opt}
257
+ </option>
258
+ ))}
259
+ </select>
260
+
261
+ <p style={{ marginTop: "20px", fontSize: "18px" }}>
262
+ You selected: <b>{selected}</b>
263
+ </p>
264
+
265
+ </div>
266
+ </>
267
+ )
268
+ }
269
+
270
+ export default App
271
+
272
+
273
+ // React Basic State - Background Color Changer
274
+ //6
275
+ import { useState } from 'react'
276
+ import './App.css'
277
+
278
+ function App() {
279
+
280
+ const [bgColor, setBgColor] = useState("#ffffff");
281
+
282
+ const getRandomColor = () => {
283
+ const letters = "0123456789ABCDEF";
284
+ let color = "#";
285
+
286
+ for (let i = 0; i < 6; i++) {
287
+ color += letters[Math.floor(Math.random() * 16)];
288
+ }
289
+
290
+ return color;
291
+ };
292
+
293
+ const handleClick = () => {
294
+ setBgColor(getRandomColor());
295
+ };
296
+
297
+ return (
298
+ <>
299
+ <div
300
+ style={{
301
+ height: "100vh",
302
+ backgroundColor: bgColor,
303
+ display: "flex",
304
+ justifyContent: "center",
305
+ alignItems: "center",
306
+ flexDirection: "column"
307
+ }}
308
+ >
309
+ <h2>Background Color Changer</h2>
310
+
311
+ <button
312
+ onClick={handleClick}
313
+ style={{
314
+ padding: "10px 20px",
315
+ fontSize: "16px",
316
+ cursor: "pointer"
317
+ }}
318
+ >
319
+ Change Color
320
+ </button>
321
+
322
+ <p style={{ marginTop: "15px" }}>
323
+ Current Color: <b>{bgColor}</b>
324
+ </p>
325
+ </div>
326
+ </>
327
+ )
328
+ }
329
+
330
+ export default App
331
+
332
+
333
+
334
+ // React Basic State - password input show/hide.
335
+ //7
336
+ import { useState } from 'react'
337
+ import './App.css'
338
+
339
+ function App() {
340
+
341
+ const [showPassword, setShowPassword] = useState(false);
342
+
343
+ const handleToggle = () => {
344
+ setShowPassword(!showPassword);
345
+ };
346
+
347
+ return (
348
+ <>
349
+ <div style={{ margin: "50px" }}>
350
+
351
+ <h1>Password Field</h1>
352
+
353
+ <input
354
+ type={showPassword ? "text" : "password"}
355
+ placeholder="Enter password"
356
+ style={{
357
+ padding: "10px",
358
+ fontSize: "16px",
359
+ marginBottom: "15px",
360
+ width: "250px"
361
+ }}
362
+ />
363
+
364
+ <br />
365
+
366
+ <button
367
+ onClick={handleToggle}
368
+ style={{
369
+ padding: "8px 15px",
370
+ cursor: "pointer"
371
+ }}
372
+ >
373
+ {showPassword ? "Hide" : "Show"}
374
+ </button>
375
+
376
+ </div>
377
+ </>
378
+ )
379
+ }
380
+
381
+ export default App
382
+
383
+
384
+
385
+ //React Basic State - Input Live Preview
386
+ //8
387
+ import { useState } from 'react'
388
+ import './App.css'
389
+
390
+ function App() {
391
+
392
+ const [text, setText] = useState("");
393
+
394
+ const handleChange = (e) => {
395
+ setText(e.target.value);
396
+ };
397
+
398
+ return (
399
+ <>
400
+ <div style={{ margin: "50px" }}>
401
+
402
+ <h1>Live Input Preview</h1>
403
+
404
+ <input
405
+ type="text"
406
+ value={text}
407
+ onChange={handleChange}
408
+ placeholder="Type something..."
409
+ style={{
410
+ padding: "10px",
411
+ fontSize: "16px",
412
+ width: "250px",
413
+ marginBottom: "20px"
414
+ }}
415
+ />
416
+
417
+ <p style={{ fontSize: "20px" }}>
418
+ You typed: <b>{text}</b>
419
+ </p>
420
+
421
+ </div>
422
+ </>
423
+ )
424
+ }
425
+
426
+ export default App
427
+
428
+
429
+ // React Basic State - Random Quote Generator
430
+ //9
431
+ import { useState } from 'react'
432
+ import './App.css'
433
+
434
+ function App() {
435
+
436
+ const quotes = [
437
+ "Dream big and dare to fail.",
438
+ "Success is not final, failure is not fatal.",
439
+ "Believe you can and you're halfway there.",
440
+ "Do something today that your future self will thank you for.",
441
+ "Stay hungry, stay foolish."
442
+ ];
443
+
444
+ const [quote, setQuote] = useState(quotes[0]);
445
+
446
+ const generateQuote = () => {
447
+ const randomIndex = Math.floor(Math.random() * quotes.length);
448
+ setQuote(quotes[randomIndex]);
449
+ };
450
+
451
+ return (
452
+ <>
453
+ <div style={{ margin: "50px" }}>
454
+
455
+ <h1>Random Quote Generator</h1>
456
+
457
+ <p style={{ fontSize: "20px", margin: "20px 0" }}>
458
+ {quote}
459
+ </p>
460
+
461
+ <button
462
+ onClick={generateQuote}
463
+ style={{
464
+ padding: "10px 20px",
465
+ cursor: "pointer"
466
+ }}
467
+ >
468
+ Generate Quote
469
+ </button>
470
+
471
+ </div>
472
+ </>
473
+ )
474
+ }
475
+
476
+ export default App
477
+
478
+
479
+
480
+ //React Basic State - Character Limit with Warning
481
+ //10
482
+ import { useState } from 'react'
483
+ import './App.css'
484
+
485
+ function App() {
486
+
487
+ const maxLimit = 20;
488
+
489
+ const [text, setText] = useState("");
490
+
491
+ const handleChange = (e) => {
492
+ const value = e.target.value;
493
+
494
+ if (value.length <= maxLimit) {
495
+ setText(value);
496
+ }
497
+ };
498
+
499
+ const remaining = maxLimit - text.length;
500
+
501
+ return (
502
+ <>
503
+ <div style={{ margin: "50px" }}>
504
+
505
+ <h1>Character Limit Input</h1>
506
+
507
+ <input
508
+ type="text"
509
+ value={text}
510
+ onChange={handleChange}
511
+ style={{
512
+ padding: "10px",
513
+ fontSize: "16px",
514
+ width: "300px",
515
+ marginBottom: "15px"
516
+ }}
517
+ />
518
+
519
+ <p>Remaining: {remaining}</p>
520
+
521
+ {remaining === 0 && (
522
+ <p style={{ color: "red", fontWeight: "bold" }}>
523
+ Limit reached!
524
+ </p>
525
+ )}
526
+
527
+ </div>
528
+ </>
529
+ )
530
+ }
531
+
532
+ export default App
533
+
534
+
535
+ //React Complex State - Toggle Status Inside Object
536
+ //11
537
+ import { useState } from 'react'
538
+ import './App.css'
539
+
540
+ function App() {
541
+
542
+ const [task, setTask] = useState({
543
+ title: "Learn React",
544
+ completed: false
545
+ });
546
+
547
+ const toggleStatus = () => {
548
+ setTask({
549
+ ...task,
550
+ completed: !task.completed
551
+ });
552
+ };
553
+
554
+ return (
555
+ <>
556
+ <div style={{ margin: "50px" }}>
557
+
558
+ <h2>
559
+ {task.title} - {task.completed ? "Completed" : "Pending"}
560
+ </h2>
561
+
562
+ <button
563
+ onClick={toggleStatus}
564
+ style={{
565
+ padding: "10px 20px",
566
+ cursor: "pointer"
567
+ }}
568
+ >
569
+ Toggle Status
570
+ </button>
571
+
572
+ </div>
573
+ </>
574
+ )
575
+ }
576
+
577
+ export default App
578
+
579
+
580
+
581
+ // React Complex State - Update Specific User in List
582
+ //12
583
+ import { useState } from 'react'
584
+ import './App.css'
585
+
586
+ function App() {
587
+
588
+ const [users, setUsers] = useState([
589
+ { id: 1, name: "A" },
590
+ { id: 2, name: "B" }
591
+ ]);
592
+
593
+ const updateUser = () => {
594
+ const updatedUsers = users.map((user) => {
595
+ if (user.id === 2) {
596
+ return { ...user, name: "Updated" };
597
+ }
598
+ return user;
599
+ });
600
+
601
+ setUsers(updatedUsers);
602
+ };
603
+
604
+ return (
605
+ <>
606
+ <div style={{ margin: "50px" }}>
607
+
608
+ <h2>User List</h2>
609
+
610
+ {users.map((user) => (
611
+ <p key={user.id}>
612
+ User {user.id}: {user.name}
613
+ </p>
614
+ ))}
615
+
616
+ <button
617
+ onClick={updateUser}
618
+ style={{
619
+ padding: "10px 20px",
620
+ marginTop: "10px",
621
+ cursor: "pointer"
622
+ }}
623
+ >
624
+ Update User
625
+ </button>
626
+
627
+ </div>
628
+ </>
629
+ )
630
+ }
631
+
632
+ export default App
633
+
634
+
635
+
636
+ //React Complex State - Employee Form with State Management
637
+ //13
638
+ import { useState } from 'react'
639
+ import './App.css'
640
+
641
+ function App() {
642
+
643
+ const [formData, setFormData] = useState({
644
+ name: "",
645
+ department: "",
646
+ salary: ""
647
+ });
648
+
649
+ const [submittedData, setSubmittedData] = useState(null);
650
+
651
+ // Single change handler
652
+ const handleChange = (e) => {
653
+ const { name, value } = e.target;
654
+
655
+ setFormData({
656
+ ...formData,
657
+ [name]: value
658
+ });
659
+ };
660
+
661
+ // Submit handler
662
+ const handleSubmit = (e) => {
663
+ e.preventDefault();
664
+ setSubmittedData(formData);
665
+ };
666
+
667
+ // Reset handler
668
+ const handleReset = () => {
669
+ setFormData({
670
+ name: "",
671
+ department: "",
672
+ salary: ""
673
+ });
674
+ setSubmittedData(null);
675
+ };
676
+
677
+ return (
678
+ <>
679
+ <div style={{ margin: "50px" }}>
680
+
681
+ <h2>Employee Form</h2>
682
+
683
+ <form onSubmit={handleSubmit}>
684
+
685
+ <input
686
+ type="text"
687
+ name="name"
688
+ placeholder="Employee Name"
689
+ value={formData.name}
690
+ onChange={handleChange}
691
+ style={{ display: "block", marginBottom: "10px", padding: "8px" }}
692
+ />
693
+
694
+ <input
695
+ type="text"
696
+ name="department"
697
+ placeholder="Department"
698
+ value={formData.department}
699
+ onChange={handleChange}
700
+ style={{ display: "block", marginBottom: "10px", padding: "8px" }}
701
+ />
702
+
703
+ <input
704
+ type="number"
705
+ name="salary"
706
+ placeholder="Salary"
707
+ value={formData.salary}
708
+ onChange={handleChange}
709
+ style={{ display: "block", marginBottom: "10px", padding: "8px" }}
710
+ />
711
+
712
+ <button type="submit" style={{ marginRight: "10px" }}>
713
+ Submit
714
+ </button>
715
+
716
+ <button type="button" onClick={handleReset}>
717
+ Reset
718
+ </button>
719
+
720
+ </form>
721
+
722
+ {/* Display Data */}
723
+ {submittedData && (
724
+ <div style={{ marginTop: "20px" }}>
725
+ <h3>Employee Details:</h3>
726
+ <p>Name: {submittedData.name}</p>
727
+ <p>Department: {submittedData.department}</p>
728
+ <p>Salary: {submittedData.salary}</p>
729
+ </div>
730
+ )}
731
+
732
+ </div>
733
+ </>
734
+ )
735
+ }
736
+
737
+ export default App
738
+
739
+
740
+
741
+ //React Complex State - Search Functionality
742
+ //14
743
+ import { useState } from 'react'
744
+ import './App.css'
745
+
746
+ function App() {
747
+
748
+ const usersData = [
749
+ { id: 1, name: "Amit Sharma", city: "Delhi" },
750
+ { id: 2, name: "Neha Verma", city: "Mumbai" },
751
+ { id: 3, name: "Rahul Singh", city: "Chandigarh" },
752
+ { id: 4, name: "Priya Mehta", city: "Pune" }
753
+ ];
754
+
755
+ const [search, setSearch] = useState("");
756
+
757
+ const handleChange = (e) => {
758
+ setSearch(e.target.value);
759
+ };
760
+
761
+ // Filter users (case-insensitive)
762
+ const filteredUsers = usersData.filter((user) =>
763
+ user.name.toLowerCase().includes(search.toLowerCase())
764
+ );
765
+
766
+ return (
767
+ <>
768
+ <div style={{ margin: "50px", maxWidth: "400px" }}>
769
+
770
+ <h2>User Search</h2>
771
+
772
+ <input
773
+ type="text"
774
+ placeholder="Search by name..."
775
+ value={search}
776
+ onChange={handleChange}
777
+ style={{
778
+ width: "100%",
779
+ padding: "10px",
780
+ marginBottom: "15px"
781
+ }}
782
+ />
783
+
784
+ {filteredUsers.map((user) => (
785
+ <div
786
+ key={user.id}
787
+ style={{
788
+ border: "1px solid #ccc",
789
+ padding: "10px",
790
+ marginBottom: "10px",
791
+ borderRadius: "5px"
792
+ }}
793
+ >
794
+ <strong>{user.name}</strong>
795
+ <span style={{ float: "right" }}>{user.city}</span>
796
+ </div>
797
+ ))}
798
+
799
+ </div>
800
+ </>
801
+ )
802
+ }
803
+
804
+ export default App
805
+
806
+
807
+
808
+ // React Complex State - Product Dashboard with Category Filter
809
+ //15
810
+ import { useState } from "react";
811
+ import "./App.css";
812
+
813
+ function App() {
814
+
815
+ const products = [
816
+ { id: 1, name: "Laptop", price: 80000, category: "Premium" },
817
+ { id: 2, name: "Mouse", price: 500, category: "Basic" },
818
+ { id: 3, name: "Keyboard", price: 1500, category: "Basic" },
819
+ { id: 4, name: "Smartphone", price: 60000, category: "Premium" },
820
+ { id: 5, name: "Monitor", price: 12000, category: "Deluxe" },
821
+ { id: 6, name: "Headphones", price: 3000, category: "Deluxe" }
822
+ ];
823
+
824
+ const [category, setCategory] = useState("All");
825
+
826
+ const handleChange = (e) => {
827
+ setCategory(e.target.value);
828
+ };
829
+
830
+ const filteredProducts =
831
+ category === "All"
832
+ ? products
833
+ : products.filter((p) => p.category === category);
834
+
835
+ return (
836
+ <>
837
+ <div style={{ padding: "20px", background: "#eef2f5", minHeight: "100vh" }}>
838
+
839
+ <h2>Product Dashboard</h2>
840
+
841
+ <select
842
+ value={category}
843
+ onChange={handleChange}
844
+ style={{
845
+ padding: "10px",
846
+ width: "100%",
847
+ marginBottom: "20px",
848
+ borderRadius: "8px"
849
+ }}
850
+ >
851
+ <option>All</option>
852
+ <option>Premium</option>
853
+ <option>Deluxe</option>
854
+ <option>Basic</option>
855
+ </select>
856
+
857
+ <div
858
+ style={{
859
+ display: "grid",
860
+ gridTemplateColumns: "repeat(auto-fit, minmax(200px, 1fr))",
861
+ gap: "20px"
862
+ }}
863
+ >
864
+
865
+ {filteredProducts.map((p) => (
866
+ <div
867
+ key={p.id}
868
+ style={{
869
+ background: "#fff",
870
+ padding: "20px",
871
+ borderRadius: "15px",
872
+ boxShadow: "0 4px 10px rgba(0,0,0,0.1)"
873
+ }}
874
+ >
875
+ <h3>{p.name}</h3>
876
+ <h2>₹{p.price}</h2>
877
+
878
+ <span
879
+ style={{
880
+ padding: "5px 12px",
881
+ borderRadius: "20px",
882
+ color: "#fff",
883
+ background:
884
+ p.category === "Premium"
885
+ ? "#2b6cb0"
886
+ : p.category === "Deluxe"
887
+ ? "#38a169"
888
+ : "#718096"
889
+ }}
890
+ >
891
+ {p.category}
892
+ </span>
893
+ </div>
894
+ ))}
895
+
896
+ </div>
897
+ </div>
898
+ </>
899
+ );
900
+ }
901
+
902
+ export default App;
903
+
904
+
905
+
906
+ //E-Commerce Product Categories (Nested Routing)
907
+ //16.1
908
+ npm install react-router-dom
909
+
910
+ import { BrowserRouter, Routes, Route } from "react-router-dom";
911
+ import Products from "./Products";
912
+ import Electronics from "./Electronics";
913
+ import Clothing from "./Clothing";
914
+ import Furniture from "./Furniture";
915
+
916
+ function App() {
917
+ return (
918
+ <BrowserRouter>
919
+ <Routes>
920
+ <Route path="/products" element={<Products />}>
921
+ <Route path="electronics" element={<Electronics />} />
922
+ <Route path="clothing" element={<Clothing />} />
923
+ <Route path="furniture" element={<Furniture />} />
924
+ </Route>
925
+ </Routes>
926
+ </BrowserRouter>
927
+ );
928
+ }
929
+
930
+ export default App;
931
+
932
+
933
+ //16.2
934
+ import { Link, Outlet } from "react-router-dom";
935
+
936
+ const Products = () => {
937
+ return (
938
+ <div style={{ margin: "40px" }}>
939
+ <h1>Products Page</h1>
940
+
941
+ <nav style={{ marginBottom: "20px" }}>
942
+ <Link to="electronics">Electronics</Link> |{" "}
943
+ <Link to="clothing">Clothing</Link> |{" "}
944
+ <Link to="furniture">Furniture</Link>
945
+ </nav>
946
+
947
+ <hr />
948
+
949
+ {/* Nested content yaha render hoga */}
950
+ <Outlet />
951
+ </div>
952
+ );
953
+ };
954
+
955
+ export default Products;
956
+
957
+ // 16.3
958
+ const Electronics = () => {
959
+ return (
960
+ <div>
961
+ <h2>Electronics</h2>
962
+ <ul>
963
+ <li>Mobile</li>
964
+ <li>Laptop</li>
965
+ <li>Headphones</li>
966
+ </ul>
967
+ </div>
968
+ );
969
+ };
970
+
971
+ export default Electronics;
972
+
973
+ //16.4
974
+ const Clothing = () => {
975
+ return (
976
+ <div>
977
+ <h2>Clothing</h2>
978
+ <ul>
979
+ <li>T-Shirts</li>
980
+ <li>Jeans</li>
981
+ <li>Jackets</li>
982
+ </ul>
983
+ </div>
984
+ );
985
+ };
986
+
987
+ export default Clothing;
988
+
989
+ //16.5
990
+ const Furniture = () => {
991
+ return (
992
+ <div>
993
+ <h2>Furniture</h2>
994
+ <ul>
995
+ <li>Sofa</li>
996
+ <li>Table</li>
997
+ <li>Chair</li>
998
+ </ul>
999
+ </div>
1000
+ );
1001
+ };
1002
+
1003
+ export default Furniture;
1004
+
1005
+
1006
+ //User Profile Page (Dynamic Routing + useParams)
1007
+ //17
1008
+ export const users = [
1009
+ {
1010
+ id: 101,
1011
+ name: "Rahul",
1012
+ email: "rahul@gmail.com",
1013
+ posts: ["Post 1", "Post 2"]
1014
+ },
1015
+ {
1016
+ id: 102,
1017
+ name: "Priya",
1018
+ email: "priya@gmail.com",
1019
+ posts: ["Post A", "Post B"]
1020
+ }
1021
+ ];
1022
+
1023
+ //17.1
1024
+ import { BrowserRouter, Routes, Route } from "react-router-dom";
1025
+ import Users from "./Users";
1026
+ import UserProfile from "./UserProfile";
1027
+
1028
+ function App() {
1029
+ return (
1030
+ <BrowserRouter>
1031
+ <Routes>
1032
+ <Route path="/users" element={<Users />} />
1033
+ <Route path="/users/:id" element={<UserProfile />} />
1034
+ </Routes>
1035
+ </BrowserRouter>
1036
+ );
1037
+ }
1038
+ export default App;
1039
+
1040
+ //17.2
1041
+ import { Link } from "react-router-dom";
1042
+ import { users } from "./data";
1043
+
1044
+ const Users = () => {
1045
+ return (
1046
+ <div style={{ margin: "40px" }}>
1047
+ <h2>Users List</h2>
1048
+
1049
+ {users.map((u) => (
1050
+ <p key={u.id}>
1051
+ <Link to={`/users/${u.id}`}>{u.name}</Link>
1052
+ </p>
1053
+ ))}
1054
+ </div>
1055
+ );
1056
+ };
1057
+ export default Users;
1058
+
1059
+ //17.3
1060
+ import { useParams, Link } from "react-router-dom";
1061
+ import { users } from "./data";
1062
+
1063
+ const UserProfile = () => {
1064
+
1065
+ const { id } = useParams();
1066
+
1067
+ const user = users.find((u) => u.id === Number(id));
1068
+
1069
+ if (!user) {
1070
+ return <h2>User not found</h2>;
1071
+ }
1072
+
1073
+ return (
1074
+ <div style={{ margin: "40px" }}>
1075
+ <h2>{user.name}'s Profile</h2>
1076
+
1077
+ <p>Email: {user.email}</p>
1078
+
1079
+ <h4>Posts:</h4>
1080
+ <ul>
1081
+ {user.posts.map((p, i) => (
1082
+ <li key={i}>{p}</li>
1083
+ ))}
1084
+ </ul>
1085
+
1086
+ <br />
1087
+
1088
+ <Link to="/users">⬅ Back to Users</Link>
1089
+ </div>
1090
+ );
1091
+ };
1092
+
1093
+ export default UserProfile;
1094
+
1095
+
1096
+ //React - The Smart Inventory Dashboard
1097
+ //18
1098
+ import React from "react";
1099
+
1100
+ function App() {
1101
+ const products = [
1102
+ { id: 101, name: "Laptop", price: 85000, category: "Electronics", stock: 15 },
1103
+ { id: 102, name: "Office Chair", price: 12000, category: "Furniture", stock: 0 },
1104
+ { id: 103, name: "Smart Watch", price: 5000, category: "Electronics", stock: 4 },
1105
+ { id: 104, name: "Desk Lamp", price: 1500, category: "Furniture", stock: 8 },
1106
+ { id: 105, name: "Mechanical Keyboard", price: 7000, category: "Electronics", stock: 25 },
1107
+ { id: 106, name: "Gaming Mouse", price: 3500, category: "Electronics", stock: 2 }
1108
+ ];
1109
+
1110
+ return (
1111
+ <div className="min-h-screen bg-gray-100 p-6">
1112
+ <h1 className="text-4xl font-bold text-center mb-6">
1113
+ Product Dashboard
1114
+ </h1>
1115
+
1116
+ <div className="overflow-x-auto">
1117
+ <table className="w-full border border-gray-400 bg-white">
1118
+ <thead>
1119
+ <tr className="bg-gray-200">
1120
+ <th className="border p-3">Name</th>
1121
+ <th className="border p-3">Category</th>
1122
+ <th className="border p-3">Price</th>
1123
+ <th className="border p-3">Stock</th>
1124
+ <th className="border p-3">Availability Status</th>
1125
+ </tr>
1126
+ </thead>
1127
+
1128
+ <tbody>
1129
+ {products.map((product) => (
1130
+ <tr
1131
+ key={product.id}
1132
+ style={{
1133
+ color:
1134
+ product.stock === 0
1135
+ ? "red"
1136
+ : product.stock <= 10
1137
+ ? "orange"
1138
+ : product.price > 50000
1139
+ ? "green"
1140
+ : "black",
1141
+ fontStyle: product.stock === 0 ? "italic" : "normal"
1142
+ }}
1143
+ >
1144
+ <td className="border p-3 text-center">{product.name}</td>
1145
+ <td className="border p-3 text-center">{product.category}</td>
1146
+ <td className="border p-3 text-center">₹{product.price}</td>
1147
+ <td className="border p-3 text-center">{product.stock}</td>
1148
+ <td className="border p-3 text-center">
1149
+ {product.stock === 0
1150
+ ? "Out of Stock"
1151
+ : product.stock <= 10
1152
+ ? "Limited Stock"
1153
+ : "In Stock"}
1154
+ </td>
1155
+ </tr>
1156
+ ))}
1157
+ </tbody>
1158
+ </table>
1159
+ </div>
1160
+
1161
+ {/* Statistics */}
1162
+ <div className="bg-white mt-6 p-5 rounded shadow text-center">
1163
+ <h2 className="text-2xl font-semibold mb-3">
1164
+ Catalog Statistics
1165
+ </h2>
1166
+
1167
+ <p>
1168
+ <b>Total Items in Catalog:</b> {products.length}
1169
+ </p>
1170
+
1171
+ <p>
1172
+ <b>Average Product Price:</b> ₹
1173
+ {(
1174
+ products.reduce((sum, item) => sum + item.price, 0) /
1175
+ products.length
1176
+ ).toFixed(2)}
1177
+ </p>
1178
+ </div>
1179
+
1180
+ {/* Premium Products */}
1181
+ <div className="bg-white mt-6 p-5 rounded shadow">
1182
+ <h2 className="text-2xl font-semibold text-center mb-3">
1183
+ Premium Products
1184
+ </h2>
1185
+
1186
+ {products.filter((p) => p.price > 10000).length > 0 ? (
1187
+ <ul className="list-disc pl-6">
1188
+ {products
1189
+ .filter((p) => p.price > 10000)
1190
+ .map((p) => (
1191
+ <li key={p.id}>{p.name}</li>
1192
+ ))}
1193
+ </ul>
1194
+ ) : (
1195
+ <p className="text-center">
1196
+ No premium products available at the moment.
1197
+ </p>
1198
+ )}
1199
+ </div>
1200
+
1201
+ {/* Electronics Clearance */}
1202
+ <div className="bg-white mt-6 p-5 rounded shadow">
1203
+ <h2 className="text-2xl font-semibold text-center mb-3">
1204
+ Electronics Clearance
1205
+ </h2>
1206
+
1207
+ <ul className="list-disc pl-6">
1208
+ {products
1209
+ .filter(
1210
+ (p) =>
1211
+ p.category === "Electronics" &&
1212
+ p.stock > 0 &&
1213
+ p.stock < 5
1214
+ )
1215
+ .map((p) => (
1216
+ <li key={p.id}>
1217
+ {p.name} - Only {p.stock} left
1218
+ </li>
1219
+ ))}
1220
+ </ul>
1221
+ </div>
1222
+ </div>
1223
+ );
1224
+ }
1225
+
1226
+ export default App;
1227
+
1228
+
1229
+ //React JSX - Student Dropdown
1230
+ //19
1231
+ import React from "react";
1232
+
1233
+ function StudentDropdown() {
1234
+ const students = [
1235
+ { id: 1, name: "Amit" },
1236
+ { id: 2, name: "Riya" },
1237
+ { id: 3, name: "John" },
1238
+ { id: 4, name: "Sneha" }
1239
+ ];
1240
+
1241
+ return (
1242
+ <div>
1243
+ <h2>Student Dropdown</h2>
1244
+
1245
+ <select>
1246
+ <option>Select Student</option>
1247
+
1248
+ {students.map((student) => (
1249
+ <option key={student.id}>
1250
+ {student.name}
1251
+ </option>
1252
+ ))}
1253
+ </select>
1254
+ </div>
1255
+ );
1256
+ }
1257
+
1258
+ export default StudentDropdown;
1259
+
1260
+
1261
+ //React - E-commerce Product Dashboard
1262
+ //20.1
1263
+ import { useEffect, useState } from "react";
1264
+ import ProductTable from "./ProductTable";
1265
+
1266
+ function App() {
1267
+ const [products, setProducts] = useState([]);
1268
+ const [sortOrder, setSortOrder] = useState("low");
1269
+
1270
+ useEffect(() => {
1271
+ fetch("https://fakestoreapi.com/products")
1272
+ .then((res) => res.json())
1273
+ .then((data) => setProducts(data))
1274
+ .catch(() => setProducts(null));
1275
+ }, []);
1276
+
1277
+ const sortedProducts =
1278
+ products &&
1279
+ [...products].sort((a, b) =>
1280
+ sortOrder === "low"
1281
+ ? a.price - b.price
1282
+ : b.price - a.price
1283
+ );
1284
+
1285
+ if (products === null) {
1286
+ return (
1287
+ <h2 className="text-center text-red-500 mt-10">
1288
+ Failed to load products.
1289
+ </h2>
1290
+ );
1291
+ }
1292
+
1293
+ return (
1294
+ <div className="p-5">
1295
+ <h1 className="text-2xl font-bold text-center mb-4">
1296
+ Product Dashboard
1297
+ </h1>
1298
+
1299
+ <div className="flex justify-center mb-4">
1300
+ <select
1301
+ className="border p-2 rounded"
1302
+ value={sortOrder}
1303
+ onChange={(e) => setSortOrder(e.target.value)}
1304
+ >
1305
+ <option value="low">Price Low to High</option>
1306
+ <option value="high">Price High to Low</option>
1307
+ </select>
1308
+ </div>
1309
+
1310
+ <ProductTable products={sortedProducts || []} />
1311
+ </div>
1312
+ );
1313
+ }
1314
+
1315
+ export default App;
1316
+
1317
+ //20.2
1318
+ function ProductTable({ products }) {
1319
+ return (
1320
+ <table className="w-full border border-gray-300">
1321
+ <thead>
1322
+ <tr className="bg-gray-200">
1323
+ <th className="border p-2">ID</th>
1324
+ <th className="border p-2">Product Name</th>
1325
+ <th className="border p-2">Price</th>
1326
+ <th className="border p-2">Category</th>
1327
+ <th className="border p-2">Rating</th>
1328
+ <th className="border p-2">Product Image</th>
1329
+ </tr>
1330
+ </thead>
1331
+
1332
+ <tbody>
1333
+ {products.map((product) => (
1334
+ <tr key={product.id}>
1335
+ <td className="border p-2 text-center">
1336
+ {product.id}
1337
+ </td>
1338
+
1339
+ <td className="border p-2">
1340
+ {product.title}
1341
+ </td>
1342
+
1343
+ <td className="border p-2 text-center">
1344
+ ${product.price}
1345
+ </td>
1346
+
1347
+ <td className="border p-2 text-center">
1348
+ {product.category}
1349
+ </td>
1350
+
1351
+ <td className="border p-2 text-center">
1352
+ {product.rating.rate}
1353
+ </td>
1354
+
1355
+ <td className="border p-2 text-center">
1356
+ <img
1357
+ src={product.image}
1358
+ alt={product.title}
1359
+ className="w-16 h-16 object-contain mx-auto"
1360
+ />
1361
+ </td>
1362
+ </tr>
1363
+ ))}
1364
+ </tbody>
1365
+ </table>
1366
+ );
1367
+ }
1368
+
1369
+ export default ProductTable;
1370
+
1371
+
1372
+ //React - Product Category Filter Using Checkbox
1373
+ //21
1374
+ import { useState } from "react";
1375
+
1376
+ function App() {
1377
+ const products = [
1378
+ {
1379
+ id: 1,
1380
+ name: "Laptop",
1381
+ category: "Electronics",
1382
+ price: 50000,
1383
+ },
1384
+ {
1385
+ id: 2,
1386
+ name: "T-Shirt",
1387
+ category: "Clothing",
1388
+ price: 1200,
1389
+ },
1390
+ {
1391
+ id: 3,
1392
+ name: "Mobile",
1393
+ category: "Electronics",
1394
+ price: 30000,
1395
+ },
1396
+ {
1397
+ id: 4,
1398
+ name: "Jeans",
1399
+ category: "Clothing",
1400
+ price: 2000,
1401
+ },
1402
+ ];
1403
+
1404
+ const [electronics, setElectronics] = useState(false);
1405
+ const [clothing, setClothing] = useState(false);
1406
+
1407
+ const filteredProducts = products.filter((product) => {
1408
+ if (!electronics && !clothing) return true;
1409
+
1410
+ return (
1411
+ (electronics && product.category === "Electronics") ||
1412
+ (clothing && product.category === "Clothing")
1413
+ );
1414
+ });
1415
+
1416
+ return (
1417
+ <div className="p-5">
1418
+ <h1 className="text-2xl font-bold text-center mb-4">
1419
+ Product Category Filter
1420
+ </h1>
1421
+
1422
+ <div className="flex gap-6 mb-4">
1423
+ <label>
1424
+ <input
1425
+ type="checkbox"
1426
+ checked={electronics}
1427
+ onChange={() => setElectronics(!electronics)}
1428
+ />{" "}
1429
+ Electronics
1430
+ </label>
1431
+
1432
+ <label>
1433
+ <input
1434
+ type="checkbox"
1435
+ checked={clothing}
1436
+ onChange={() => setClothing(!clothing)}
1437
+ />{" "}
1438
+ Clothing
1439
+ </label>
1440
+ </div>
1441
+
1442
+ <table className="w-full border border-gray-300">
1443
+ <thead>
1444
+ <tr className="bg-gray-200">
1445
+ <th className="border p-2">ID</th>
1446
+ <th className="border p-2">Product Name</th>
1447
+ <th className="border p-2">Category</th>
1448
+ <th className="border p-2">Price</th>
1449
+ </tr>
1450
+ </thead>
1451
+
1452
+ <tbody>
1453
+ {filteredProducts.map((product) => (
1454
+ <tr key={product.id}>
1455
+ <td className="border p-2 text-center">{product.id}</td>
1456
+ <td className="border p-2">{product.name}</td>
1457
+ <td className="border p-2 text-center">
1458
+ {product.category}
1459
+ </td>
1460
+ <td className="border p-2 text-center">
1461
+ ₹{product.price}
1462
+ </td>
1463
+ </tr>
1464
+ ))}
1465
+ </tbody>
1466
+ </table>
1467
+ </div>
1468
+ );
1469
+ }
1470
+
1471
+ export default App;
1472
+
1473
+
1474
+ //React - Product Dropdown Viewer
1475
+ //22
1476
+ import { useEffect, useState } from "react";
1477
+
1478
+ function App() {
1479
+ const [products, setProducts] = useState([]);
1480
+ const [selectedProduct, setSelectedProduct] = useState("");
1481
+
1482
+ useEffect(() => {
1483
+ fetch("https://fakestoreapi.com/products")
1484
+ .then((res) => res.json())
1485
+ .then((data) => setProducts(data))
1486
+ .catch((err) => console.log(err));
1487
+ }, []);
1488
+
1489
+ const product = products.find(
1490
+ (p) => p.id === Number(selectedProduct)
1491
+ );
1492
+
1493
+ return (
1494
+ <div className="max-w-2xl mx-auto p-5">
1495
+ <h1 className="text-3xl font-bold text-center mb-6">
1496
+ Product Viewer
1497
+ </h1>
1498
+
1499
+ <select
1500
+ className="w-full border p-3 rounded mb-6"
1501
+ value={selectedProduct}
1502
+ onChange={(e) => setSelectedProduct(e.target.value)}
1503
+ >
1504
+ <option value="">Select Product</option>
1505
+
1506
+ {products.map((p) => (
1507
+ <option key={p.id} value={p.id}>
1508
+ {p.title}
1509
+ </option>
1510
+ ))}
1511
+ </select>
1512
+
1513
+ {product ? (
1514
+ <div className="border rounded-lg p-5 shadow">
1515
+ <img
1516
+ src={product.image}
1517
+ alt={product.title}
1518
+ className="w-48 h-48 object-contain mx-auto"
1519
+ />
1520
+
1521
+ <h2 className="text-2xl text-center mt-4 mb-4">
1522
+ {product.title}
1523
+ </h2>
1524
+
1525
+ <div className="border p-3 rounded mb-3">
1526
+ <strong>Category:</strong> {product.category}
1527
+ </div>
1528
+
1529
+ <div className="border p-3 rounded mb-3">
1530
+ <strong>Price:</strong> ${product.price}
1531
+ </div>
1532
+
1533
+ <div className="border p-3 rounded">
1534
+ {product.description}
1535
+ </div>
1536
+ </div>
1537
+ ) : (
1538
+ <div className="border border-dashed p-6 text-center rounded">
1539
+ Select a product to view details
1540
+ </div>
1541
+ )}
1542
+ </div>
1543
+ );
1544
+ }
1545
+
1546
+ export default App;
1547
+
1548
+
1549
+ //React - Image Slider
1550
+ //23
1551
+ import { useState } from "react";
1552
+
1553
+ function App() {
1554
+ const images = [
1555
+ "https://images.unsplash.com/photo-1506744038136-46273834b3fb?w=800",
1556
+ "https://images.unsplash.com/photo-1500530855697-b586d89ba3ee?w=800",
1557
+ "https://images.unsplash.com/photo-1493246507139-91e8fad9978e?w=800",
1558
+ "https://images.unsplash.com/photo-1519125323398-675f0ddb6308?w=800"
1559
+ ];
1560
+
1561
+ const [index, setIndex] = useState(0);
1562
+
1563
+ const nextImage = () => {
1564
+ setIndex((index + 1) % images.length);
1565
+ };
1566
+
1567
+ const prevImage = () => {
1568
+ setIndex((index - 1 + images.length) % images.length);
1569
+ };
1570
+
1571
+ return (
1572
+ <div className="p-5">
1573
+ <h1 className="text-3xl text-center font-bold mb-5">
1574
+ Image Slider
1575
+ </h1>
1576
+
1577
+ <img
1578
+ src={images[index]}
1579
+ alt="slider"
1580
+ className="w-full max-w-3xl h-96 object-cover mx-auto rounded-lg"
1581
+ />
1582
+
1583
+ <div className="flex justify-between max-w-3xl mx-auto mt-5">
1584
+ <button
1585
+ onClick={prevImage}
1586
+ className="bg-blue-500 text-white px-4 py-2 rounded"
1587
+ >
1588
+ Previous
1589
+ </button>
1590
+
1591
+ <button
1592
+ onClick={nextImage}
1593
+ className="bg-blue-500 text-white px-4 py-2 rounded"
1594
+ >
1595
+ Next
1596
+ </button>
1597
+ </div>
1598
+ </div>
1599
+ );
1600
+ }
1601
+
1602
+ export default App;
1603
+
1604
+
1605
+
1606
+
1607
+ //React - password validator
1608
+ //24
1609
+ import { useState } from "react";
1610
+
1611
+ function App() {
1612
+ const [password, setPassword] = useState("");
1613
+
1614
+ return (
1615
+ <div className="p-10">
1616
+ <h1 className="text-5xl font-bold mb-10">
1617
+ Password Validator
1618
+ </h1>
1619
+
1620
+ <input
1621
+ type="password"
1622
+ placeholder="Enter Password"
1623
+ value={password}
1624
+ onChange={(e) => setPassword(e.target.value)}
1625
+ className="w-full border-2 border-gray-300 rounded-lg p-4 text-2xl"
1626
+ />
1627
+
1628
+ <div className="mt-12 space-y-6 text-2xl font-bold">
1629
+ <p
1630
+ className={
1631
+ /^[A-Z]/.test(password)
1632
+ ? "text-green-600"
1633
+ : "text-red-600"
1634
+ }
1635
+ >
1636
+ First character should be Capital
1637
+ </p>
1638
+
1639
+ <p
1640
+ className={
1641
+ /[a-z]/.test(password)
1642
+ ? "text-green-600"
1643
+ : "text-red-600"
1644
+ }
1645
+ >
1646
+ Must contain one small letter
1647
+ </p>
1648
+
1649
+ <p
1650
+ className={
1651
+ /[!@#$%^&*(),.?":{}|<>]/.test(password)
1652
+ ? "text-green-600"
1653
+ : "text-red-600"
1654
+ }
1655
+ >
1656
+ Must contain one special character
1657
+ </p>
1658
+
1659
+ <p
1660
+ className={
1661
+ /[0-9]/.test(password)
1662
+ ? "text-green-600"
1663
+ : "text-red-600"
1664
+ }
1665
+ >
1666
+ Must contain one number (0-9)
1667
+ </p>
1668
+ </div>
1669
+ </div>
1670
+ );
1671
+ }
1672
+
1673
+ export default App;
1674
+
1675
+
1676
+
1677
+
1678
+
1679
+
1680
+
1681
+
1682
+
1683
+
1684
+
1685
+ //TS Library Management System
1686
+ //1
1687
+ enum BookStatus {
1688
+ Available,
1689
+ CheckedOut,
1690
+ Reserved
1691
+ }
1692
+
1693
+ type Book = [string, string, number, BookStatus];
1694
+
1695
+ function borrowBook(book: Book): string {
1696
+ if (book[3] === BookStatus.Available) {
1697
+ book[3] = BookStatus.CheckedOut;
1698
+ return `"${book[0]}" has been successfully borrowed.`;
1699
+ } else if (book[3] === BookStatus.Reserved) {
1700
+ return `"${book[0]}" is reserved and cannot be borrowed.`;
1701
+ } else {
1702
+ return `"${book[0]}" is already checked out.`;
1703
+ }
1704
+ }
1705
+
1706
+
1707
+ let book1: Book = [
1708
+ "The Great Gatsby",
1709
+ "F. Scott Fitzgerald",
1710
+ 123456,
1711
+ BookStatus.Available
1712
+ ];
1713
+
1714
+ console.log(borrowBook(book1));
1715
+ console.log(borrowBook(book1));
1716
+
1717
+
1718
+ //TS-Movie Ticket Booking
1719
+ //2
1720
+ enum SeatType {
1721
+ Regular,
1722
+ Premium,
1723
+ VIP
1724
+ }
1725
+
1726
+ type Ticket = [string, number, SeatType, number];
1727
+
1728
+ function calculateDiscount(ticket: Ticket): number {
1729
+ if (ticket[2] === SeatType.Premium) {
1730
+ return ticket[3] * 0.9;
1731
+ } else if (ticket[2] === SeatType.VIP) {
1732
+ return ticket[3] * 0.8;
1733
+ }
1734
+ return ticket[3];
1735
+ }
1736
+
1737
+ let ticket1: Ticket = ["Avengers", 12, SeatType.Premium, 500];
1738
+ console.log(calculateDiscount(ticket1));
1739
+
1740
+ let ticket2: Ticket = ["Batman", 5, SeatType.VIP, 1000];
1741
+ console.log(calculateDiscount(ticket2));
1742
+
1743
+
1744
+ //TS-Smart Home Automation
1745
+ //3
1746
+ enum DeviceStatus {
1747
+ On,
1748
+ Off,
1749
+ Standby
1750
+ }
1751
+
1752
+ type SmartDevice = [string, string, DeviceStatus];
1753
+
1754
+ function toggleDeviceStatus(
1755
+ device: SmartDevice,
1756
+ newStatus: DeviceStatus
1757
+ ): SmartDevice {
1758
+ if (
1759
+ (device[2] === DeviceStatus.Off && newStatus === DeviceStatus.Standby) ||
1760
+ (device[2] === DeviceStatus.Standby && newStatus === DeviceStatus.Off)
1761
+ ) {
1762
+ return device;
1763
+ }
1764
+
1765
+ device[2] = newStatus;
1766
+ return device;
1767
+ }
1768
+
1769
+ let device: SmartDevice = ["Living Room Light", "Light", DeviceStatus.Off];
1770
+
1771
+ console.log(toggleDeviceStatus(device, DeviceStatus.On));
1772
+ console.log(toggleDeviceStatus(device, DeviceStatus.Standby));
1773
+
1774
+
1775
+ //TS - Function Overloading
1776
+ //4.1
1777
+ function add(a: number, b: number): number;
1778
+ function add(a: string, b: string): string;
1779
+
1780
+ function add(a: any, b: any): any {
1781
+ return a + b;
1782
+ }
1783
+
1784
+ function calculate(): void {
1785
+ const input1 = (document.getElementById("input1") as HTMLInputElement).value;
1786
+ const input2 = (document.getElementById("input2") as HTMLInputElement).value;
1787
+
1788
+ const num1 = Number(input1);
1789
+ const num2 = Number(input2);
1790
+
1791
+ let result: number | string;
1792
+
1793
+ if (!isNaN(num1) && !isNaN(num2)) {
1794
+ result = add(num1, num2);
1795
+ } else {
1796
+ result = add(input1, input2);
1797
+ }
1798
+
1799
+ (document.getElementById("result") as HTMLElement).innerText =
1800
+ "Result: " + result;
1801
+ }
1802
+
1803
+ //4.2
1804
+ <!DOCTYPE html>
1805
+ <html>
1806
+ <head>
1807
+ <title>Function Overloading</title>
1808
+ </head>
1809
+ <body>
1810
+ <input type="text" id="input1" placeholder="Enter first value">
1811
+ <input type="text" id="input2" placeholder="Enter second value">
1812
+ <button onclick="calculate()">Add</button>
1813
+
1814
+ <p id="result"></p>
1815
+
1816
+ <script src="app.js"></script>
1817
+ </body>
1818
+ </html>
1819
+
1820
+
1821
+
1822
+ //TS - Character Counter for Textarea
1823
+ //5
1824
+ <!DOCTYPE html>
1825
+ <html>
1826
+ <head>
1827
+ <title>Character Counter</title>
1828
+ </head>
1829
+ <body>
1830
+ <textarea id="postContent" rows="5" cols="40"></textarea>
1831
+ <br>
1832
+ Characters: <span id="charCount">0</span>/200
1833
+ <div id="warningMsg"></div>
1834
+
1835
+ <script>
1836
+ const postContent = document.getElementById("postContent");
1837
+ const charCount = document.getElementById("charCount");
1838
+ const warningMsg = document.getElementById("warningMsg");
1839
+
1840
+ postContent.addEventListener("input", updateCount);
1841
+
1842
+ function updateCount() {
1843
+ const count = postContent.value.length;
1844
+
1845
+ charCount.textContent = count;
1846
+
1847
+ if (count > 200) {
1848
+ warningMsg.textContent =
1849
+ "Warning: Character limit exceeded!";
1850
+ } else {
1851
+ warningMsg.textContent = "";
1852
+ }
1853
+ }
1854
+ </script>
1855
+ </body>
1856
+ </html>
1857
+
1858
+
1859
+ //TS - Dropdown Selection Display
1860
+ //6
1861
+ <!DOCTYPE html>
1862
+ <html>
1863
+ <head>
1864
+ <title>Dropdown Selection Display</title>
1865
+ </head>
1866
+ <body>
1867
+ <select id="countrySelect">
1868
+ <option value="India">India</option>
1869
+ <option value="USA">USA</option>
1870
+ <option value="Canada">Canada</option>
1871
+ </select>
1872
+
1873
+ <p id="selectedCountry"></p>
1874
+
1875
+ <script>
1876
+ const countrySelect = document.getElementById("countrySelect");
1877
+ const selectedCountry = document.getElementById("selectedCountry");
1878
+
1879
+ countrySelect.addEventListener("change", () => {
1880
+ selectedCountry.textContent =
1881
+ "Selected Country: " + countrySelect.value;
1882
+ });
1883
+ </script>
1884
+ </body>
1885
+ </html>
1886
+
1887
+
1888
+ //TS - Real-time Numeric Input Validation with TypeScript
1889
+ //7
1890
+ <!DOCTYPE html>
1891
+ <html>
1892
+ <head>
1893
+ <title>Numeric Input Validation</title>
1894
+ </head>
1895
+ <body>
1896
+ <input type="text" id="ageInput" placeholder="Enter Age">
1897
+ <div id="errorMsg"></div>
1898
+
1899
+ <script>
1900
+ const ageInput = document.getElementById("ageInput");
1901
+ const errorMsg = document.getElementById("errorMsg");
1902
+
1903
+ ageInput.addEventListener("input", () => {
1904
+ const value = ageInput.value;
1905
+
1906
+ if (/^\d*$/.test(value)) {
1907
+ errorMsg.textContent = "";
1908
+ } else {
1909
+ errorMsg.textContent = "Only numeric characters are allowed.";
1910
+ }
1911
+ });
1912
+ </script>
1913
+ </body>
1914
+ </html>