0
0
Node.jsframework~10 mins

Buffer allocation and encoding 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 allocate a buffer of 10 bytes.

Node.js
const buf = Buffer.[1](10);
Drag options to blanks, or click blank then click option'
Aalloc
Bfrom
Cnew
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.from instead of Buffer.alloc
Trying to use 'new Buffer' which is deprecated
2fill in blank
medium

Complete the code to create a buffer from the string 'hello' using UTF-8 encoding.

Node.js
const buf = Buffer.from('hello', '[1]');
Drag options to blanks, or click blank then click option'
Abase64
Butf8
Cascii
Dhex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ascii' which only supports basic English characters
Using 'base64' which is for encoded binary data, not plain text
3fill in blank
hard

Fix the error in the code to convert a buffer to a string with base64 encoding.

Node.js
const str = buf.toString('[1]');
Drag options to blanks, or click blank then click option'
Autf8
Bhex
Cbase64
Dascii
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'utf8' instead of 'base64' for encoding
Using 'hex' which produces hexadecimal string, not base64
4fill in blank
hard

Fill both blanks to create a buffer from a hex string and convert it back to a utf8 string.

Node.js
const buf = Buffer.[1]('68656c6c6f', '[2]');
const str = buf.toString('utf8');
Drag options to blanks, or click blank then click option'
Afrom
Balloc
Chex
Dutf8
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc instead of Buffer.from
Using 'utf8' as input encoding for a hex string
5fill in blank
hard

Fill all three blanks to allocate a buffer, write a string with utf8 encoding, and convert it to base64.

Node.js
const buf = Buffer.[1](6);
buf.write('hello', 0, [2], '[3]');
const base64 = buf.toString('base64');
Drag options to blanks, or click blank then click option'
Aalloc
B5
Cutf8
Dfrom
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.from instead of Buffer.alloc for fixed size buffer
Incorrect length parameter in write method
Wrong encoding name in write method