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

155
Visualizações
JavaScript version of while True loop

I wrote this bit of code

while True:
   text = input('type here > ')
   print text

I've tried the below but it doesnt seem to be working

I'm having bit of a struggle trying to create a JavaScript version of it, since it seems js while loops constantly rerun but python seems to wait for input before rerunning. I am using the readline module to receive input from the console.

const readline = require('readline');

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

while (true) {
 rl.question('type here > ', text => {
       console.log(text)
    })
}

Is there something im getting wrong? Im fairly new to programming

Any solutions?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

If what you are trying to do is get input from the console in JavaScript you can use NodeJS process.stdin the implementation below :

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let input = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    input += inputStdin;
});

process.stdin.on('end', _ => {
    input = input.trim()
        .split('\n')
        .map(str => str.trim());

    // calling the function that works on the input
    doSomething();
});


function readInput() {
    return input[currentLine++];

}

Sample function to do something with what you get from the console

function doSomething() {
    console.log(readInput())
}
about 4 years ago · Santiago Gelvez 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