Skip to content

Commit 1218874

Browse files
committed
Section 9: Higher Order Functions
1 parent a373baf commit 1218874

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ function TodoList() {
181181
*/
182182

183183
// ******************************
184-
// Function Expressions ******************************
184+
// Function Expressions ******************************
185185
// Example 14
186+
/*
186187
console.log('\n')
187188
// this on it's own is not valid, we can't call this
188189
// We have no way of referring to this function
@@ -215,3 +216,53 @@ console.log(product(1, 99))
215216
console.dir(add)
216217
console.dir(sum)
217218
console.dir(product)
219+
*/
220+
221+
// Higher Order Functions ******************************
222+
// Example 15
223+
224+
console.log('\n')
225+
// Function statement
226+
function add(x, y) {
227+
return x + y
228+
}
229+
230+
const subtract = function (x, y) {
231+
return x - y
232+
}
233+
234+
function multiply(x, y) {
235+
return x * y
236+
}
237+
238+
const divide = function (x, y) {
239+
return x / y
240+
}
241+
242+
const operations = [add, subtract, multiply, divide]
243+
console.log(operations[0])
244+
console.log(operations[1])
245+
console.log(operations[2])
246+
console.log(operations[3])
247+
console.log('\n')
248+
249+
console.log(operations[0](100, 4)) // 104
250+
console.log(operations[1](100, 4)) // 96
251+
console.log(operations[2](100, 4)) // 400
252+
console.log(operations[3](100, 4)) // 25
253+
254+
console.log('\n')
255+
// We can loop functions in an array
256+
for (let func of operations) {
257+
let result = func(30, 5)
258+
console.log(result)
259+
}
260+
261+
console.log('\n')
262+
// We can store functions in an object
263+
const thing = {
264+
// Method: a function stored in an object
265+
doSomething: multiply,
266+
}
267+
console.log(thing);
268+
console.log(thing.doSomething(50, 2)) // 100

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,6 @@ In JavaScript functions are objects, which means we can put them in a variable,
149149
**There are some differences between function expressions and normal functions how these behave**
150150

151151
**You can add in a name for a function expression**
152+
153+
## 4. Higher Order Functions
154+
Functions that operate on/with other functions. They can:

0 commit comments

Comments
 (0)