0
0
Node.jsframework~10 mins

Child process exit codes 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 listen for the child process exit event.

Node.js
childProcess.on('exit', (code) => {
  console.log('Child exited with code', [1]);
});
Drag options to blanks, or click blank then click option'
Astatus
Berror
Csignal
Dcode
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' or 'signal' instead of the exit code parameter.
Not using the parameter at all.
2fill in blank
medium

Complete the code to spawn a child process running 'ls' command.

Node.js
const { spawn } = require('child_process');
const ls = [1]('ls', ['-lh', '/usr']);
Drag options to blanks, or click blank then click option'
Aexec
Bfork
Cspawn
DexecFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exec' which runs a shell command but does not take arguments as an array.
Using 'fork' which is for spawning new Node.js processes.
3fill in blank
hard

Fix the error in the code to correctly check if the child process exited normally.

Node.js
childProcess.on('exit', (code, signal) => {
  if ([1] === null) {
    console.log('Exited normally with code', code);
  } else {
    console.log('Process killed by signal', signal);
  }
});
Drag options to blanks, or click blank then click option'
Acode
Bsignal
Ccode === 0
Dsignal === null
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if code is null instead of signal.
Checking if code equals zero to determine normal exit.
4fill in blank
hard

Fill both blanks to create a dictionary of exit codes and their meanings.

Node.js
const exitCodes = {
  0: [1],
  1: [2]
};
Drag options to blanks, or click blank then click option'
A'Success'
B'General error'
C'Failure'
D'Unknown'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the meanings of exit codes 0 and 1.
Using vague terms like 'Failure' instead of 'General error'.
5fill in blank
hard

Fill all three blanks to create a filtered object of exit codes greater than zero.

Node.js
const filtered = Object.fromEntries(
  Object.entries(exitCodes).filter(([[1], [2]]) => [3] > 0)
);
Drag options to blanks, or click blank then click option'
Acode
Bmessage
CNumber(code)
Dmessage.length
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting the key to a number before comparison.
Using the message variable in the comparison instead of the code.