-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-advanced-functions.html
248 lines (193 loc) · 4.79 KB
/
12-advanced-functions.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Advanced Functions</title>
<style>
.js-button1, .js-button2 {
margin-bottom: 10px;
}
</style>
</head>
<body>
<button class="js-button1" onclick="changeButton()">Start</button>
<button class="js-button2" onclick="changeCartStatus()">Add to Cart</button>
<p class="js-push-p"></p>
<p>
<button class="button-container" onclick="addMessage()">Add</button>
<button class="button-container" onclick="removeMessage()">Remove</button>
</p>
<button onclick="" class="js-button">Click</button>
<script>
/*
const buttonElement = document.querySelector('.js-button');
const eventListener = () => {
console.log('click')
}
buttonElement.addEventListener('click', eventListener);
buttonElement.removeEventListener('click',eventListener);
buttonElement.addEventListener('click', () => {
console.log('click2')
});
/*
function greeting() {
console.log('hellow');
};
greeting();
const function1 = function() { // anonymous function
console.log('hello2');
};
console.log(function1);
console.log(typeof function1);
function1();
const object1 = {
num: 2,
fun: function greeting() {
console.log('hello3');
}
};
object1.fun();
function display(param) {
console.log(param);
}
display(2);
function run(param) {
}
run(function () {
console.log('hello4')
});
*/
/*
setTimeout(function() { // asynchronous code
console.log('timeout');
console.log('timeout2')
}, 3000);
console.log('next line'); // synchronous code
setInterval(function() {
// console.log('interval') // will keep running (kinda like loop)
}, 3000);
// console.log('next line 2')
[
'make dinner',
'wash dishes',
'watch youtube'
].forEach((value, index) => {
if (value === 'wash dishes') {
return;
}
console.log(index);
console.log(value);
});
const add = function () {
console.log(2+3)
}
function runTwice(fun) {
console.log(fun);
console.log(fun);
}
runTwice(add)
const button1 = document.querySelector('.js-button1')
function changeButton() {
button1.innerHTML = 'Loading...';
setTimeout(function () {
button1.innerHTML = 'Finished!'
}, 1000)
}
const pushP = document.querySelector('.js-push-p')
function changeCartStatus() {
pushP.innerHTML = 'Added'
setTimeout(function () {
pushP.remove()
}, 2000)
}
setInterval(function () {
const newTitle = document.title = `(${messages}) New messages`
}, 1000)
setInterval(function () {
const newTitle = document.title = 'Old Title'
}, 3000)
let messages = 2;
function addMessage() {
messages += 1
}
function removeMessage() {
if (messages === 0) {
document.title = 'App'
}
else if (messages > 0){
messages -= 1
}
}
const regularFunction = function(param, param2) {
console.log('hellow');
return 5;
}
const arrowFunction = (param, param2) => {
console.log('hello');
return 5;
}
arrowFunction();
const oneParam = param => {
console.log(param + 1);
}
oneParam(2);
const oneLine = () => 2 + 3;
console.log(oneLine());
const object2 = {
method: () => {
},
method2() {
}
}
*/
console.log([1, -3, 5].filter((value, index) => {
/*
if (value >= 0) {
return true;
} else {
return false;
}
*/
return value >= 0;
}));
console.log([1,1,3].map((value, index) => {
return value * 2
}));
console.log([1,1,3].map(value => value * 2));
const multiply = (num1, num2) => num1 * num2;
console.log(multiply(2,3));
let count = 0
function countPositive(nums) {
nums.forEach(value => {
if (value > 0) {
count += 1
}
});
return count;
}
console.log(countPositive([-2, 3, -5, 7, 10]));
function addNum(array, num) {
console.log(array.map(value => value + num));
}
addNum([-2, -1, 0, 99],2);
function removeEgg(foods) {
console.log(foods.filter(value => value !== 'egg'));
}
removeEgg(['egg', 'apple', 'egg', 'egg', 'ham'])
function removeEgg1(foods) {
let count = 0;
return foods.filter(value => {
if (value === 'egg' && count < 2) {
count++;
return false;
}
return true;
});
}
console.log(removeEgg1(['egg', 'apple', 'egg', 'egg', 'ham']));
</script>
</body>
</html>