Skip to content

Commit 81a1f68

Browse files
authored
Create 157-read-n-characters-given-read4.js
1 parent 2e4970b commit 81a1f68

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

157-read-n-characters-given-read4.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for read4()
3+
*
4+
* @param {character[]} buf Destination buffer
5+
* @return {number} The number of actual characters read
6+
* read4 = function(buf) {
7+
* ...
8+
* };
9+
*/
10+
11+
/**
12+
* @param {function} read4()
13+
* @return {function}
14+
*/
15+
const solution = function(read4) {
16+
const internalBuf = []
17+
/**
18+
* @param {character[]} buf Destination buffer
19+
* @param {number} n Number of characters to read
20+
* @return {number} The number of actual characters read
21+
*/
22+
return function(buf, n) {
23+
let readChars = 0
24+
while (n > 0) {
25+
if (internalBuf.length === 0) {
26+
if (read4(internalBuf) === 0) {
27+
return readChars
28+
}
29+
}
30+
buf.push(internalBuf.shift())
31+
readChars++
32+
n--
33+
}
34+
return readChars
35+
}
36+
}

0 commit comments

Comments
 (0)