Bird
Raised Fist0
Node.jsframework~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the Node.js REPL allow you to do?
easy
A. Create graphical user interfaces
B. Compile JavaScript files into executables
C. Run JavaScript code interactively line by line
D. Deploy Node.js applications to servers

Solution

  1. Step 1: Understand REPL purpose

    REPL stands for Read-Eval-Print Loop, which lets you type and run code interactively.
  2. Step 2: Identify correct REPL use

    It is used to run JavaScript commands one by one and see results immediately.
  3. Final Answer:

    Run JavaScript code interactively line by line -> Option C
  4. Quick Check:

    REPL = Interactive code execution [OK]
Hint: REPL runs code interactively, not for compiling or deploying [OK]
Common Mistakes:
  • Confusing REPL with compiling or deployment tools
  • Thinking REPL creates GUIs
  • Assuming REPL runs entire scripts automatically
2. Which command starts the Node.js REPL in your terminal?
easy
A. node
B. node repl
C. npm repl
D. node start

Solution

  1. Step 1: Recall how to launch REPL

    Typing node alone in the terminal starts the REPL environment.
  2. Step 2: Check other options

    Commands like node start, npm repl, or node repl are invalid or do not start REPL.
  3. Final Answer:

    node -> Option A
  4. Quick Check:

    Start REPL with 'node' command [OK]
Hint: Just type 'node' to enter REPL, no extra words needed [OK]
Common Mistakes:
  • Adding extra words after 'node' command
  • Using npm commands to start REPL
  • Typing 'node repl' which is invalid
3. What will be the output in Node.js REPL after entering 2 + 3 * 4?
medium
A. 20
B. 14
C. 24
D. Error

Solution

  1. Step 1: Apply operator precedence

    Multiplication (*) has higher precedence than addition (+), so 3 * 4 = 12 first.
  2. Step 2: Calculate final expression

    Then add 2 + 12 = 14.
  3. Final Answer:

    14 -> Option B
  4. Quick Check:

    2 + (3 * 4) = 14 [OK]
Hint: Multiply before adding to get correct result [OK]
Common Mistakes:
  • Adding 2 + 3 first, then multiplying
  • Expecting an error for simple math
  • Confusing multiplication and addition order
4. You typed const x 5 in Node.js REPL but got a syntax error. What is the likely cause?
medium
A. Missing semicolon at the end
B. Using const inside REPL is not allowed
C. You forgot to press Enter after typing
D. You missed the equal sign (=) in the declaration

Solution

  1. Step 1: Check the code syntax

    The code const x 5 is missing the equal sign (=) between variable name and value.
  2. Step 2: Understand REPL syntax rules

    REPL accepts const declarations with proper syntax; semicolon is optional, and pressing Enter is required.
  3. Final Answer:

    You missed the equal sign (=) in the declaration -> Option D
  4. Quick Check:

    const needs '=' to assign value [OK]
Hint: Check for '=' in variable declarations [OK]
Common Mistakes:
  • Assuming semicolon is mandatory
  • Thinking const is disallowed in REPL
  • Not pressing Enter to execute
5. In Node.js REPL, how can you quickly exit the interactive session?
hard
A. Type exit or press Ctrl + C twice
B. Type quit() and press Enter
C. Close the terminal window only
D. Press Ctrl + Z once

Solution

  1. Step 1: Recall REPL exit commands

    You can exit REPL by typing exit or pressing Ctrl + C twice quickly.
  2. Step 2: Evaluate other options

    quit() is not recognized, closing terminal is abrupt, and Ctrl + Z suspends process but does not exit REPL.
  3. Final Answer:

    Type exit or press Ctrl + C twice -> Option A
  4. Quick Check:

    Exit REPL with 'exit' or Ctrl+C twice [OK]
Hint: Use 'exit' or double Ctrl+C to quit REPL fast [OK]
Common Mistakes:
  • Using quit() which is invalid
  • Pressing Ctrl+Z expecting exit
  • Closing terminal instead of proper exit