Just in Time explainer


#1

The aim of this puzzle: Set a timer using the setTimer callback.

Walkthrough of the solution: There are 3 function declarations:

  1. setTimer: takes in a number of minutes and outputs a message
  2. setReminder: takes in a time and outputs a message
  3. clockAction: takes in a time and a type of action, then outputs a message and completes the action

The setTimer and setReminder functions are different actions that the clock can take. To use these actions, the functions can be passed as callbacks to the clockAction function. In other words, the clockAction function can take in another function as an argument, and then use that function.

At the bottom of the code, add a clockAction() call. The 1st argument should be a number, like 45. This will represent the number of minutes to set the timer to. The 2nd argument is a callback function, the type of action the clock should do. Since we want to set a timer, we’ll use the setTimer function as our callback. A callback shouldn’t have parentheses after it; we just want to use the name of the function so that our clockAction() function know which function to call.

Sample code solution:
(Tap below to reveal)

function setTimer(minutes) {
  console.log('A ' + minutes + ' minute timer has been started');
}

function setReminder(time) {
  console.log('Scheduled a reminder for ' + time);
}

function clockAction(time, type) {
  console.log('Clock opened...');
  type(time);
}

clockAction('7am', setReminder);
clockAction(45, setTimer);

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