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

77
Visualizações
Javascript Loop based on input

I have a case: If The user input 3, then the output should be 1 2 start.
If the user input 5, then the output should be 1 2 3 4 start.
If the user input more than 9, then the output should be Error.

I tried the following code, but no idea how can I add the last number into string

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
let text = "";
for (let i = 1; i < 5; i++) {
  if (i == 5) break;
  text += i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Any idea please?
Really appreciated.

Thank you.

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

0

Wrap you for loop in a function. Then you can concat the numbers and append the word start on return.

const input = 15;
console.log(countdown(input))

function countdown(input) {
  if (input > 9) {
    return 'Error!';
  }
  c = '';
  for (let i = 1; i < input; i++ ) {
    c += ' ' + i;
  }
  return c + ' start';
}

In your case

    const input = 5;
    
    document.getElementById("demo").innerHTML = countdown(input);
    function countdown(input) {
      if (input > 9) {
        return 'Error!';
      }
      c = '';
      for (let i = 1; i < input; i++ ) {
        c += ' ' + i + '<br/>';
      }
      return c + ' start';
    }
<div id="demo"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is my answer:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Stack11</title>
</head>
<body>

<p id="demo"></p>

<input type="number" min="1" id="myInput" value="1">
<button onclick="getResult()">
    GetResult
</button>
<script>

  let text = "";

  function getResult(){
      document.getElementById("demo").innerText = "";
      let myInput = document.getElementById("myInput").value;
      myInput = parseInt(myInput);

      // From 1 to 9 not included 9
      if (myInput < 9 && myInput > 0){
          // You are excluding 1 from the input: 3 => 1, 2 : 5 => 1, 2, 3, 4
          for (let i=1; i < myInput; i++){
              text += i + "<br>";
          }
      }else{
          text = 'Output: The use input is wrong.';
      }

      document.getElementById("demo").innerHTML = text;
  }


</script>


</body>
</html>
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