0% found this document useful (0 votes)
7 views

note write - Copy (2)

The document provides a code example demonstrating how to create and use a simple Node.js module with a sum function, event handling using EventEmitter, and a readline interface for user input. It includes creating a Person class that extends EventEmitter and prompts the user to solve a random addition problem. The code handles correct and incorrect answers, providing feedback through the console.

Uploaded by

pgmkhoi1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

note write - Copy (2)

The document provides a code example demonstrating how to create and use a simple Node.js module with a sum function, event handling using EventEmitter, and a readline interface for user input. It includes creating a Person class that extends EventEmitter and prompts the user to solve a random addition problem. The code handles correct and incorrect answers, providing feedback through the console.

Uploaded by

pgmkhoi1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Print line

console.log("Hello World!")

Create function and exporghdvbt as module:


const sum = (n1, n2) => n1 + n2;
module.exports = sum;
Or module.exports = { sum: sum, PI: PI, MathObj: MathObj }; // better import

Import and use function:cbbfdcvbcvvbcvb


const tutorial = require('./tutorial') // import
console.log(tutorial);
console.log(tutorial(1,2));

Create and invoke an eveBFGBFFGGnt. Add variables to function


eventEmitter.on('tutorial', (n1, n2) => {
console.log(n1 + n2);
});

eventEmitter.emit('tutorial', 1, 2); // same with ASMT 4 in PPL EMIT things

Create object like human


class Person extends EventEmitter {
constructor(nameIn) {
super();
this._name = nameIn;
}
get name() {
return this._name;
}
}

let chilly = new Person('Chilly');


chilly.on('name', ()=> {
console.log('my name is ' + chilly.name);
});
chilly.emit('name');

Ctrl+C: to stop running program in terminal

Code
const readline = require('readline');
const 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;

rl.question('What is '+ num1 + ' + ' + num2 + '?\n',


(userInput) => {
if (userInput.trim() == answer) {
rl.close(); // end event
}
else {
rl.setPrompt('Incorrect\n'); // incorrect, set prompt as a question
rl.prompt(); // prompt to give question to user
rl.on('line', () => { // MOST IMPORTANT; to run when prompt finished.
if (userInput.trim() == answer) {
rl.close(); // end event
}
else {
rl.setPrompt('Incorrect. Please try again\n');
rl.prompt(); // return the ans to rl.on(‘line’)
}
});
}
}
);
rl.on('close', () => {
console.log('CORRECT\n');
});

You might also like