0
0
Node.jsframework~10 mins

REPL for interactive exploration in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a Node.js REPL session.

Node.js
const repl = require('[1]');
repl.start();
Drag options to blanks, or click blank then click option'
Apath
Bfs
Chttp
Drepl
Attempts:
3 left
💡 Hint
Common Mistakes
Using other modules like 'fs' or 'http' instead of 'repl'.
Forgetting to require the module before using it.
2fill in blank
medium

Complete the code to define a custom command in the Node.js REPL.

Node.js
const repl = require('repl');
const server = repl.start('> ');
server.defineCommand('[1]', {
  help: 'Say hello',
  action() {
    console.log('Hello!');
    this.displayPrompt();
  }
});
Drag options to blanks, or click blank then click option'
Aclear
Bexit
Chello
Dhelp
Attempts:
3 left
💡 Hint
Common Mistakes
Using built-in commands like 'exit' or 'clear' as custom commands.
Not calling this.displayPrompt() to show the prompt again.
3fill in blank
hard

Fix the error in the code to evaluate an expression asynchronously in the REPL.

Node.js
const repl = require('repl');
const server = repl.start('> ');
server.eval = function(cmd, context, filename, callback) {
  Promise.resolve(eval(cmd)).then(result => {
    callback(null, [1]);
  }).catch(err => {
    callback(err);
  });
};
Drag options to blanks, or click blank then click option'
Aresult
Bcontext
Ccmd
Dfilename
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the original command string instead of the result.
Passing context or filename which are not the evaluation result.
4fill in blank
hard

Fill both blanks to create a REPL with a custom prompt and exit message.

Node.js
const repl = require('repl');
const server = repl.start({ prompt: '[1]', useColors: true });
server.on('[2]', () => {
  console.log('Goodbye!');
  process.exit();
});
Drag options to blanks, or click blank then click option'
A>
Bexit
Cline
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exit' event which does not exist on REPL server.
Setting prompt to something other than '> '.
5fill in blank
hard

Fill all three blanks to create a REPL that saves context variables and uses a custom eval function.

Node.js
const repl = require('repl');
const context = { count: 0 };
function customEval(cmd, context, filename, callback) {
  try {
    const result = eval(cmd);
    callback(null, result);
  } catch (e) {
    callback(e);
  }
}
const server = repl.start({ prompt: '[1]', eval: [2] });
Object.assign(server.context, [3]);
Drag options to blanks, or click blank then click option'
A>>>
BcustomEval
Ccontext
DreplEval
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong prompt string or missing spaces.
Passing wrong function name for eval.
Not assigning the context object to the REPL context.