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

193
Visualizações
Do While loop - Simply add numbers

I need to prompt the user to enter a series of numbers, or the word "quit". Then, If the user enters a number, add the new number to a running total. And, If the user enters the word "quit" the loop should stop execution. I cant figure what should I do here. Im a beginner. i don't know how to make it work when user enter a word quit

let total = 0;
let number = 0;
do {
  total += number;
  number = parseInt(prompt('Enter a number: '));
  if (number >= 0) {
    document.writeln("<p>You entered " + number + "!</p>");
  } else {
    if (isNaN(number)) {
      number = parseInt(prompt('Enter a number: '));
      document.writeln("<p>Try again. We are looking for a number!</p>");
    }
  }
} while (number >= 0)
document.writeln("<p>The total is " + total + "</p>")

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

0

Use break to stop the javascript loop:

let total = 0;
let number = 0;
    do {
        total += number;
        text = prompt('Enter a number: ');
        if (text == "quit") {
           break;
        }
        number = parseInt(text);
        if (number >= 0) {
            document.writeln("<p>You entered " + number + "!</p>");
        } else {
            if (isNaN(number))  {
                number = parseInt(prompt('Enter a number: '));
                document.writeln("<p>Try again. We are looking for a number!</p>");
            } 
        }
    } while(number >= 0)
    document.writeln("<p>The total is " + total + "</p>")
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