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()

#2

why i can’t re-creat this code in code playground ? i mean how to make callback function as qrgument without parentheses and how to make callback function with parentheses

#3

The playground does not yet support everything taught in the lessons, though new features are being added with almost every app update.

You can still make an anonymous (meaning un-named) function as a callback in the playground. You can do this with either the function keyword or the arrow function key () => {}. Tap on the space where you want to add the callback function, then tap one of those keys. It will end up looking like this:

someFunctionCall('somethingSomething', function (foo) {
    return foo;
})

Or the same thing written with an arrow function:

someFunctionCall('somethingSomething', (foo) => {
   return foo;
})

This will work in the playground.

Let me know if you have any other questions!
–Ben