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.
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 ratings0% 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.
(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'); });