Challenge - 5 Problems
Node.js Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this Node.js code output when you type 'hello' and press Enter?
Consider this code snippet that reads from standard input and writes to standard output:
Node.js
process.stdin.on('data', (data) => { process.stdout.write(`You typed: ${data}`); }); process.stdin.resume();
Attempts:
2 left
💡 Hint
Remember that the data event includes the newline character when you press Enter.
✗ Incorrect
The 'data' event receives a Buffer or string including the newline character from pressing Enter. The code writes it back with 'You typed: ' prefix, so the output includes the newline.
📝 Syntax
intermediate1:30remaining
Which option correctly sets up process.stdin to read input as UTF-8 strings?
You want to read user input as strings, not buffers. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
Look for the method that sets encoding properly.
✗ Incorrect
The correct method to set encoding is setEncoding('utf8'). Other options are invalid syntax or properties.
🔧 Debug
advanced2:00remaining
Why does this code never print anything after input?
Look at this code snippet:
Node.js
process.stdin.on('data', (chunk) => {
console.log(chunk.toString());
});
// No other codeAttempts:
2 left
💡 Hint
Check if the stream is flowing or paused by default.
✗ Incorrect
In Node.js, process.stdin is paused by default. Without calling process.stdin.resume(), the 'data' event won't fire.
❓ state_output
advanced1:30remaining
What is the output of this code after typing 'abc' and pressing Enter?
This code reads input and writes it back in uppercase:
Node.js
process.stdin.setEncoding('utf8'); process.stdin.on('data', (data) => { process.stdout.write(data.toUpperCase()); }); process.stdin.resume();
Attempts:
2 left
💡 Hint
Remember the input includes the newline character.
✗ Incorrect
The input 'abc' plus newline is converted to uppercase, so output is 'ABC\n'.
🧠 Conceptual
expert2:30remaining
What happens if you write to process.stdout without a newline and then read from process.stdin?
Consider this code snippet:
Node.js
process.stdout.write('Enter your name: '); process.stdin.setEncoding('utf8'); process.stdin.on('data', (data) => { process.stdout.write(`Hello, ${data}`); process.exit(); }); process.stdin.resume();
Attempts:
2 left
💡 Hint
process.stdout.write does not add a newline automatically.
✗ Incorrect
process.stdout.write prints exactly what you give it, so the prompt appears without newline. Then input is read and greeted including the newline from input.