

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 7
click for more info
Not enough gems
Cost: 6 gems
1: Functions
incomplete
2: Function Hoisting
incomplete
3: Unit Test Lessons
incomplete
4: Multiple Return Values
incomplete
5: Functions As Values
incomplete
6: Scope
incomplete
7: Anonymous Functions
incomplete
8: Default Parameters
incomplete
9: Passing by Value
incomplete
10: Immediate Invocation
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
JavaScript supports first-class and higher-order functions, which are just fancy ways of saying "functions as values". Functions can be treated like any other data type – such as numbers and strings and booleans. Let's assume we have two simple functions:
function add(x, y) {
return x + y;
}
function mul(x, y) {
return x * y;
}
We can create a new aggregate function that accepts a function as its 4th argument:
function aggregate(a, b, c, arithmetic) {
const firstResult = arithmetic(a, b);
const secondResult = arithmetic(firstResult, c);
return secondResult;
}
It calls the given arithmetic function (which could be add or mul, or any other function that accepts two parameters and returns a number) and applies it to three inputs instead of two. It can be used like this:
function main() {
const sum = aggregate(2, 3, 4, add);
// sum is 9
const product = aggregate(2, 3, 4, mul);
// product is 24
}
Complete the reformat function. It takes a message string and a formatter function as input:
formatter three times to the messageTEXTIO: to the resultFor example, if the message is "General Kenobi" and the formatter adds a period to the end of the string, the final result should be:
TEXTIO: General Kenobi...