0
0
Node.jsframework~10 mins

Creating buffers in Node.js - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a buffer of size 10 bytes.

Node.js
const buf = Buffer.[1](10);
Drag options to blanks, or click blank then click option'
Afrom
Bwrite
Calloc
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.from instead of Buffer.alloc to create an empty buffer.
Trying to use Buffer.write which is for writing data, not creating buffers.
2fill in blank
medium

Complete the code to create a buffer from the string 'hello'.

Node.js
const buf = Buffer.[1]('hello');
Drag options to blanks, or click blank then click option'
Aalloc
Bconcat
Cslice
Dfrom
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc which creates an empty buffer, not from a string.
Using Buffer.slice which extracts part of a buffer, not create one.
3fill in blank
hard

Fix the error in the code to create a buffer from an array of bytes.

Node.js
const buf = Buffer.[1]([1, 2, 3, 4]);
Drag options to blanks, or click blank then click option'
Afrom
Bwrite
Calloc
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc which creates an empty buffer of a given size.
Using Buffer.write which is for writing data into buffers.
4fill in blank
hard

Fill both blanks to create a buffer of size 5 and fill it with the byte value 255.

Node.js
const buf = Buffer.[1](5, [2]);
Drag options to blanks, or click blank then click option'
Aalloc
Bfrom
C255
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.from instead of Buffer.alloc for allocating empty buffer.
Using 0 as fill value instead of 255.
5fill in blank
hard

Fill all three blanks to create a buffer from the string 'abc', specify UTF-8 encoding, and slice the first two bytes.

Node.js
const buf = Buffer.[1]('abc', [2]).[3](0, 2);
Drag options to blanks, or click blank then click option'
Afrom
B'utf8'
Cslice
Dalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc instead of Buffer.from for string input.
Omitting encoding or using wrong encoding string.
Using wrong method instead of slice to extract bytes.