Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Is there way take dynamic input using stdin in NodeJS?

I have to get 1st input of the Number of Test Cases:

if the test case is got input of 1, Then I need to get 2 more inputs.

if the test case is got input of 2, Then I need to get 4 more inputs.

Is it possible to use STDIN for dynamic input using 1st input in NodeJS?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sure, this is possible. In NodeJS, you could use the built-in readline module. Nothing prevents you from building logic so that a dynamic number of inputs are read. For example, something like this might help you:

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

rl.question("How many entries do you need to create? ").then(async answer => {
    const numberOfEntries = Number(answer);

    // Validate input
    if (!Number.isInteger(numberOfEntries) || numberOfEntries < 1)
        throw new Error("Invalid number of entries");

    // Read dynamic number of inputs
    const entries = [];
    for (let i = 0; i < numberOfEntries; i++) {
        const foo = await rl.question("Foo? ");
        const bar = await rl.question("Bar? ");
        
        entries.push({foo, bar});
        console.log(`Entry ${i} with foo = ${foo} and bar = ${bar}`);
    }
 
    rl.close();
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!