-
Notifications
You must be signed in to change notification settings - Fork 125
/
day2-starts.js
137 lines (103 loc) · 3.21 KB
/
day2-starts.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
// code will be updated at end of day 2// Creating an object
let task = {
title: "Buy groceries",
dueDate: "2024-06-10",
isCompleted: false
};
// Accessing properties using dot notation
console.log(task.title); // Output: Buy groceries
// Accessing properties using bracket notation
console.log(task["dueDate"]); // Output: 2024-06-10
// Adding properties
task.priority = "High";
console.log(task.priority); // Output: High
// Modifying properties
task.isCompleted = true;
console.log(task.isCompleted); // Output: true
// Deleting properties
delete task.dueDate;
console.log(task.dueDate); // Output: undefined
// Methods in Objects
let task = {
title: "Buy groceries",
isCompleted: false,
toggleCompletion: function() {
this.isCompleted = !this.isCompleted;
}
};
console.log(task.isCompleted); // Output: false
task.toggleCompletion();
console.log(task.isCompleted); // Output: true
// open this.js file to show example and come back here
// Prototypes and Inheritance
let baseTask = {
isCompleted: false,
toggleCompletion: function() {
this.isCompleted = !this.isCompleted;
}
};
let task = Object.create(baseTask);
task.title = "Buy groceries";
console.log(task.title); // Output: Buy groceries
console.log(task.isCompleted); // Output: false
task.toggleCompletion();
console.log(task.isCompleted); // Output: true
// Function Scope and Closures show in html
function outerFunction() {
let outerVar = "I am outside!";
function innerFunction() {
let innerVar = "I am inside!";
console.log(outerVar); // Can access outerVar
}
innerFunction();
//console.log(innerVar); // Error: innerVar is not defined
}
outerFunction();
// Closures
function createCounter() {
let count = 0; // this will change
return function() {
count++;
return count;
};
}
let counter = createCounter();
console.log(counter()); // Output: 1
console.log(counter()); // Output: 2
console.log(counter()); // Output: 3
/*
Closures: Closures preserve the environment of a function,
allowing inner functions to access variables from their
outer functions even after those outer functions have finished running.
This enables data encapsulation and stateful behavior.
*/
// Higher-Order Functions
// Functions can accept other functions as arguments.
function greet(name) {
return `Hello, ${name}!`;
}
function callWithGreeting(name, func) {
console.log(func(name));
}
callWithGreeting("Kabir", greet); // Output: Hello, Kabir!
// Traditional Function
function calculateAverageRating(ratings) {
// if (ratings.length === 0) {
// return 0;
// }
const total = ratings.reduce(function(sum, rating) {
return sum + rating;
}, 0);
return total / ratings.length;
}
// Arrow Function with traditional
const calculateAverageRating = (ratings) => {
const total = ratings.reduce((sum, rating) => sum + rating, 0); // Arrow function inside reduce
return total / ratings.length;
};
const movieRatings = [4.5, 3.8, 5.0, 4.2];
const averageRating = calculateAverageRating(movieRatings);
console.log("Average Rating:", averageRating);
// Selecting and Manipulating DOM Elements
// show mini-project of tasks
// show events