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.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% 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.
(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'); });