We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2e4970b commit 81a1f68Copy full SHA for 81a1f68
157-read-n-characters-given-read4.js
@@ -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
19
+ * @param {number} n Number of characters to read
20
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
35
36
+}
0 commit comments