-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS_InterviewQuestions.js
More file actions
66 lines (49 loc) · 1.3 KB
/
JS_InterviewQuestions.js
File metadata and controls
66 lines (49 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Promise Implementation
let myPromise = new Promise((res, rej) => {
let success = true;
if (success) {
res();
} else {
rej();
}
});
// Promise with then and catch
myPromise.then((value) => {}).catch((err) => {});
// Implementation of module exports
function sum(a, b) {
return a + b;
}
sub = (a, b) => {
return b - a;
};
module.exports = {
sum,
sub,
};
// event emitters
const events = require("events");
const myEvent = events.EventEmitter();
myEvent.on("send", () => {
console.log("send event triggered");
});
myEvent.on("update", (name) => {
console.log(`Hello, ${name}`);
});
myEvent.emit("send");
myEvent.emit("update", "madhu");
// Buffer
// Create a buffer from a string
const buff1 = Buffer.from("Hello World", "utf-8");
// Create a buffer from array of bytes
const buff2 = Buffer.from([72, 101, 108, 108, 111]);
// Allocate a buffer of a specific size
const buff3 = Buffer.alloc(10);
buff3.write("nodejs", 0, "utf-8");
// convert the buffer to String
console.log(buff1.toString("utf-8")); // o/p Hello World
// below code will convert the 72, 101, 108, 108, 111 to respective char then display
console.log(buff2.toString("utf-8"));
console.log(buff3.toString("utf-8"));
// concat 2 buffer
const newBuff = Buffer.concat([buff1, buff3]);
console.log(newBuff.toString("utf-8"));