Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda