Bake Time Explainer

The aim of this puzzle:

Cause the code to create an error, then catch it with a try…catch statement.

Walkthrough of the solution:

The starter code imports a function called startTimer(). This method expects an argument of number. If the argument is not a number, the function causes an error to happen. This is a special ability in JavaScript called throwing an error. This error can be caught with a try…catch statement.

Change the argument in the function call to the string 'thirty'. When the code runs again, startTimer() will throw the error. The catch block will catch the error and run the code inside the block.

But nothing is being printed out. console.log() needs an argument. The catch block gives us a variable with the error in the parentesis in the line } catch (e) {. Add that variable as the argument to console.log().

This time, when the code is run, the console will log the error message.

Sample code solution:
(Tap below to reveal)

import {startTimer} from 'cookbook';

try {
    startTimer('thirty');
} catch (e) {
    console.log(e);
}

JavaScript Concepts: number, string, try...catch block

Additional code Additional Code (hidden code that runs before the puzzle’s code):

function _startTimer(time){
   if(isNaN(time)){
       throw new Error('time must be a number');
   } else {
       console.log('Timer set for ' + time + ' minutes');
   }
}

defineModule(
    'cookbook',
    {
        startTimer: _startTimer,
    }
);