Drinks and Desserts explainer


#1

The aim of this puzzle: Use an imported function as a callback.

Walkthrough of the solution: The solution to this puzzle is similar to the previous ones. The only difference is that the callbacks aren’t declared in code, but they aren’t entirely in hidden code either. The 1st line is an import statement. An import can access data or functions from another location. In this case, orderFunctions, which would be the name of another file.

To order desserts, you call the scheduleDinner() function, and use a day (as a string) and the orderDesserts callback.

Sample code solution:
(Tap below to reveal)

import { orderGroceries, orderPizza,  orderDesserts, orderDrinks} from 'orderFunctions';

function scheduleDinner(day, mealFunction) {
  console.log('Scheduling dinner...');
  mealFunction(day);
}

scheduleDinner('Friday', orderDrinks);
scheduleDinner('Saturday', orderDesserts);

JavaScript Concepts: Functions, Callback Functions, Strings, console.log()