0% found this document useful (0 votes)
14 views2 pages

note write - Copy (4)

The document provides a tutorial on creating and using modules in Node.js, including exporting functions and handling events. It demonstrates how to create a simple interactive program that prompts the user for the sum of two random numbers and checks the answer. Additionally, it includes an example of creating a class that extends EventEmitter to manage events related to a person's name.

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)
14 views2 pages

note write - Copy (4)

The document provides a tutorial on creating and using modules in Node.js, including exporting functions and handling events. It demonstrates how to create a simple interactive program that prompts the user for the sum of two random numbers and checks the answer. Additionally, it includes an example of creating a class that extends EventEmitter to manage events related to a person's name.

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 export 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:


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

Create and invoke an event. Add variables to function


eventEmitter.on(bf, (n1, n2) => {BDVBVVCXVCXV
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