-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (70 loc) · 1.94 KB
/
app.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
console.log("Hello from the other side");
const utils = require('./utils');
console.log(utils);
console.log(utils.sum(1, 2));
console.log(utils.PI);
console.log(utils.isChecked);
new utils.SomeMathObject("Checking", true);
//==>>Event module
const EventEmitter = require('events');
const eventEmitter = new EventEmitter();
eventEmitter.on('utils', (num1, num2) => {
console.log('utils event has occurred: ' + utils.sum(num1, num2))
});
eventEmitter.emit('utils', 2, 3);
class Person extends EventEmitter {
constructor(name, age) {
super();
this._name = name;
this._age = age;
}
get name() {
return this._name;
}
get age() {
return this._age;
}
}
let babai = new Person('Babai', 25);
babai.on('name', () => {
console.log('My name is ' + babai.name);
console.log('My age is ' + babai.age);
});
let pedro = new Person("Pedro", 20);
pedro.on('name', () => {
console.log('My name is ' + pedro.name);
});
//these get executed synchronously
pedro.emit('name');
babai.emit('name');
//==>>Readline module
const readLine = require('readline');
let rl = readLine.createInterface({
input: process.stdin,
output: process.stdout
});
let num1 = Math.floor((Math.random() * 10 + 1));
let num2 = Math.floor((Math.random() * 10 + 1));
let answer = num1 + num2;
let query = 'What is ' + num1 + ' + ' + num2 + '? \n';
rl.question(query, (userInput) => {
if (userInput.trim() == answer) {
rl.close();
} else {
console.log('Incorrect: ' + userInput);
rl.setPrompt('Please try again\n');
rl.prompt();
rl.on('line', (userInput) => {
if (userInput.trim() == answer) {
rl.close();
} else {
console.log('Incorrect: ' + userInput);
rl.setPrompt('Please try again\n');
rl.prompt();
}
})
}
});
rl.on('close', () => {
console.log('Correct!!!');
});