Print line
console.log("Hello World!")
xfg
Create function and export as module:
const sum = (n1, n2) => n1 + n2;
module.exports = sub= { sum: sum, PI: PI, MathObj: MathObj }; // better import
bg
Import and use funzfdfsgfction:
const tutorial = require('./tutorial') // import
console.log(tutorial);
console.log(tutorial(1,2));
Create and invoke an event. 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');
});