Skip to content

Commit b853ff2

Browse files
committed
Section 9: Functions as Arguments
1 parent 1218874 commit b853ff2

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

09-An-Advanced-Look-at-Functions/01/01.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ console.dir(product)
220220

221221
// Higher Order Functions ******************************
222222
// Example 15
223-
223+
/*
224224
console.log('\n')
225225
// Function statement
226226
function add(x, y) {
@@ -252,17 +252,69 @@ console.log(operations[2](100, 4)) // 400
252252
console.log(operations[3](100, 4)) // 25
253253
254254
console.log('\n')
255+
// Example 16
255256
// We can loop functions in an array
256257
for (let func of operations) {
257258
let result = func(30, 5)
258259
console.log(result)
259260
}
260261
261262
console.log('\n')
263+
// Example 17
262264
// We can store functions in an object
263265
const thing = {
264266
// Method: a function stored in an object
265267
doSomething: multiply,
266268
}
267269
console.log(thing);
268270
console.log(thing.doSomething(50, 2)) // 100
271+
*/
272+
273+
// Higher Order Functions ******************************
274+
// Example 18
275+
276+
console.log('\n')
277+
function callThreeTimes(func) {
278+
func()
279+
func()
280+
func()
281+
}
282+
283+
function cry() {
284+
console.log("BOO HOO I'M SO SAD!")
285+
}
286+
cry()
287+
cry()
288+
cry()
289+
console.log('\n')
290+
callThreeTimes(cry)
291+
292+
console.log('\n')
293+
function rage() {
294+
console.log('I HATE EVERYTHING!')
295+
}
296+
callThreeTimes(rage)
297+
298+
console.log('\n')
299+
// Example 19
300+
function repeatNTimes(action, num) {
301+
for (let i = 0; i < num; i++) {
302+
action()
303+
}
304+
}
305+
306+
repeatNTimes(rage, 5)
307+
repeatNTimes(cry, 4)
308+
309+
console.log('\n')
310+
// Example 20
311+
function pickOne(f1, f2) {
312+
let rand = Math.random()
313+
console.log('Random num insinde pickOne: ', rand)
314+
if (rand < 0.5) {
315+
f1()
316+
} else {
317+
f2()
318+
}
319+
}
320+
pickOne(cry, rage)

09-An-Advanced-Look-at-Functions/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,37 @@ square(7); // 49
144144
```
145145

146146
#### FUNCTIONS ARE... OBJECTS!
147+
147148
In JavaScript functions are objects, which means we can put them in a variable, we can store 10 of them in an array, we can even pass them around as arguments, which is something we do all the time.
148149

149150
**There are some differences between function expressions and normal functions how these behave**
150151

151152
**You can add in a name for a function expression**
152153

153-
## 4. Higher Order Functions
154+
## 5. Higher Order Functions
155+
154156
Functions that operate on/with other functions. They can:
157+
158+
## 6. Functions as Arguments
159+
**Higher Order Functions**
160+
161+
- Accept other functions as arguments
162+
- Return a function
163+
164+
Functions that either accept functions as arguments and use them or do something with them or
165+
a function that returns another function
166+
167+
```
168+
function callTwice(func) {
169+
func();
170+
func();
171+
}
172+
173+
function laugh() {
174+
console.log("HAHAHAHAHAHAHAHAHAHA");
175+
}
176+
177+
callTwice(laugh); // pass function as an argument!
178+
// "HAHAHAHAHAHAHAHAHAHA"
179+
// "HAHAHAHAHAHAHAHAHAHA"
180+
```

0 commit comments

Comments
 (0)