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

186
Visualizações
prompt and change background color in a loop with native javascript

I would like to write a simply Java script (native) program which MUST use a while loop and do the following.

  • prompts user to enter a colour -
  • Then change the background to that colour.
  • keep prompting them to enter colour and keep changing the background colour until user does not enter anything in the prompt at which point the WHILE loop ends.

I have seen lot of variations of this tackled by using functions - but I want to achieve this only using WHILE loop.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can loop until value of the prompt entered is null. However you won't see the color change until after you leave the loop because the command prompt is blocking.

while (true) {
  var color = prompt("enter color", "red");
  if (color === null) {
    break;
  }
  document.body.style.backgroundColor = color;
}

This "asynchronous" version allows the browser to paint between prompt to another. But no while here.

function prompt_then(then) {
  var color = prompt("enter color", "red");
  if (color === null) {
    return;
  }
  document.body.style.backgroundColor = color;
  window.setTimeout(then, 1);
}

prompt_then(prompt_then);

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