Creating and Editing Driver Functions

Modified on Fri, 19 Jul at 11:54 AM

1. How to take input in JavaScript?


Ans. 

JavaScript has a dedicated library named readline built for the purpose of input-output operations, especially for reading from and writing to the console.


To take input in javascript, we use an interface that comes packaged with a readline module to read data from a readable stream i.e process.stdin.


Code:


const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question("What is your name? ", (answer) => {
  console.log(`Hello, ${answer}!`);
  rl.close();
});



Output:




Explanation: 


i) the readline.createInterface method is used to create an interface for reading input and output.

ii) rl.question method prompts the user for input and the provided callback function processes the user's response.

iii) rl.close method is called to close the interface.



2. How to Take Input for DSA Mock Problems 


Input lines as separate strings

Step 1: Create an Asynchronous IIFE (Immediately Invoked Function Expression) to invoke the main function, which will return the answer to be logged on the console.

The structure looks like this:

  • The main function should be called within an async IIFE.

  • The result from the main function (res) is logged to the console.

Step 2: Define the takeInput() function, but leave it empty for now. Call takeInput from the main function to store the input and perform the logic.

  • The main function is asynchronous and calls takeInput.

  • The input is destructured into variables (e.g., str1str2).

Step 3: Create an interface using the readline module within the takeInput() function.

  • Set up readline.createInterface with input and output.

Step 4: Return a promise that resolves with the proper input when a 'close' event occurs.

  • Collect lines of input into an array (inputLines).

  • Use event listeners rl.on('line') to add each new line of input to the array.

  • On rl.on('close'), resolve the promise with the inputLines array.

The collected input is then stored in a variable and can be destructured for further processing to solve the problem.



Join inputs a single string, and then processes the string into a structured object.

Step 1: Create an Asynchronous IIFE (Immediately Invoked Function Expression) to invoke the main function, which will return the answer to be logged on the console.

  • This IIFE calls the main function and logs the result.

Step 2: Define the takeInput() function without any content initially. Call this function from the main function to store the input and perform the logic.

  • The main function is asynchronous and calls takeInput.

  • The input is destructured into variables (n and array).

Step 3: Create an interface using the readline module within the takeInput() function.

  • Set up the readline interface with input and output streams.

Step 4: Return a promise that resolves with the input when a 'close' event occurs.

  • Collect lines of input into an array (inputLines).

  • Use event listeners to add each new line of input to the array.

  • On the 'close' event, resolve the promise with the processed input.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article