0
0
Node.jsframework~10 mins

Reading and writing buffer data 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 create a Buffer from a string.

Node.js
const buf = Buffer.[1]('Hello');
Drag options to blanks, or click blank then click option'
Awrite
Bfrom
Calloc
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc() which creates an empty buffer of a fixed size.
Trying to use Buffer.write() which writes data into an existing buffer.
2fill in blank
medium

Complete the code to write the string 'Hi' into the buffer at offset 0.

Node.js
buf.[1]('Hi', 0);
Drag options to blanks, or click blank then click option'
Awrite
Bfill
Ccopy
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using fill() which fills the buffer with a value but not a string.
Using copy() which copies data between buffers.
3fill in blank
hard

Fix the error in reading an unsigned 16-bit integer from the buffer at offset 2.

Node.js
const num = buf.[1](2);
Drag options to blanks, or click blank then click option'
AreadUInt16BE
BreadInt16LE
CreadUInt8
DreadInt32BE
Attempts:
3 left
💡 Hint
Common Mistakes
Using readUInt8 which reads only 1 byte.
Using readInt32BE which reads 4 bytes instead of 2.
4fill in blank
hard

Fill both blanks to create a buffer of length 5 and fill it with the byte value 0x1F.

Node.js
const buf = Buffer.[1](5);
buf.[2](0x1F);
Drag options to blanks, or click blank then click option'
Aalloc
Bfrom
Cfill
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.from() which creates a buffer from data, not size.
Using write() which writes string data, not fills the buffer.
5fill in blank
hard

Fill all three blanks to create a buffer from a string, write 'OK' at offset 3, and read an unsigned 8-bit integer at offset 4.

Node.js
const buf = Buffer.[1]('Example');
buf.[2]('OK', 3);
const val = buf.[3](4);
Drag options to blanks, or click blank then click option'
Afrom
Bwrite
CreadUInt8
Dalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc() instead of Buffer.from() to create buffer from string.
Using readInt8 instead of readUInt8 for unsigned reading.