Challenge - 5 Problems
Buffer Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What is the output of this Buffer concatenation code?
Consider the following Node.js code that concatenates two buffers. What will be the output when logged as a string?
Node.js
const buf1 = Buffer.from('Hello, '); const buf2 = Buffer.from('World!'); const result = Buffer.concat([buf1, buf2]); console.log(result.toString());
Attempts:
2 left
💡 Hint
Remember that Buffer.concat joins buffers exactly as they are without adding extra characters.
✗ Incorrect
Buffer.concat joins the buffers in the order given, preserving all characters including spaces and punctuation. Since buf1 ends with a comma and space, the output includes them exactly.
❓ Predict Output
intermediate1:00remaining
What is the length of the concatenated Buffer?
Given these buffers, what is the length of the resulting buffer after concatenation?
Node.js
const buf1 = Buffer.from('abc'); const buf2 = Buffer.from('defg'); const result = Buffer.concat([buf1, buf2]); console.log(result.length);
Attempts:
2 left
💡 Hint
Length counts all bytes in both buffers combined.
✗ Incorrect
Buffer 'abc' has length 3, 'defg' has length 4, so concatenated length is 3 + 4 = 7.
🔧 Debug
advanced1:30remaining
Why does this Buffer.concat call throw an error?
Examine the code below. Why does it throw a TypeError?
Node.js
const buf1 = Buffer.from('test'); const buf2 = 'not a buffer'; const result = Buffer.concat([buf1, buf2]);
Attempts:
2 left
💡 Hint
Buffer.concat expects an array of Buffer objects only.
✗ Incorrect
Buffer.concat throws a TypeError if any element in the array is not a Buffer instance. Here, buf2 is a string, causing the error.
❓ component_behavior
advanced1:30remaining
What happens if you specify a total length smaller than the sum of buffers in Buffer.concat?
Consider this code snippet. What will be the output of the concatenated buffer as a string?
Node.js
const buf1 = Buffer.from('12345'); const buf2 = Buffer.from('67890'); const result = Buffer.concat([buf1, buf2], 8); console.log(result.toString());
Attempts:
2 left
💡 Hint
The total length limits the size of the resulting buffer.
✗ Incorrect
Buffer.concat with a total length of 8 returns a buffer of length 8 containing the first 8 bytes from the concatenated buffers, which is "12345678".
📝 Syntax
expert2:00remaining
Which option correctly concatenates buffers and converts to uppercase string?
Choose the code snippet that concatenates two buffers and outputs the uppercase string of the result.
Attempts:
2 left
💡 Hint
Remember Buffer.concat takes an array of buffers and toUpperCase() is a string method.
✗ Incorrect
Option C correctly concatenates buffers, converts to string, then to uppercase. Option C passes arguments incorrectly. Option C calls toUpperCase on a Buffer, causing error. Option C calls toUpperCase on Buffer, causing error.