Just in Time explainer

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...');

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

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

1 Like

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

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!

Excuse my question. When we add a line of clockAction and callback the function setTimer, how could the “time” argument in the clockAction correlate the argument “minutes” in the setTimer function?

First of all, you do not have to excuse yourself for the question(but it’s politeness, I get it). Anyway, in the second called back function, you used ‘setTimer’. So, ‘type’ in the function declaration, ‘clockAction’, becomes ‘setTimer’. Therefore, the last line of code in the ‘clockAction’ function declaration becomes ‘setTimer(40)’ (because you have used ‘40’ in the callback function). From there you can see that, by calling ‘clockAction’ on the last line of code, you also called ‘setTimer’.

I hope I was helpful.


Hey guys! Hope you’re all well. :nerd_face: I just cannot understand the type(time) , inside the 3rd function declaration? It’s like an argument taking an argument (rather than a function taking an argument).
Got confused for a sec and thought I was doing Python with type() , :grinning_face_with_smiling_eyes:
Anyway, it’s quite late here, and I hope this isn’t an awfully careless question coming from my tired brain. Cheers!