Javascript Loop - 1 + input

Asked

Viewed 101 times

0

Good night, you guys!

I’m locked in a Javascript loop, it’s as follows:

User type any positive number, example 15.

Need to provide on the console the number 1 up to 15.

The problem is that I do not know how to increment a number that I do not know which will be typed and on top of that limit this number from the number 1 until typed.

I can see that or you’re confused?

Thank you.

  • Welcome to Stackoverflow in English! Please explain the problem better, and if possible include a code example that reproduces what is happening, as your question is not noticeable. See Help Center How to Ask.

1 answer

2

Find below the answer to your question, I think the doubt would be like seeking the value of input document.getElementById(ID_DO_ELEMENTO).value

document.getElementById('go').onclick = function() { //attribui um evento click para o button[go]
  var numero = document.getElementById('numero').value; //busca o valor dentro do input[numero]
  if (isFinite(numero)) { //verifica se e numero
    for (var x = 1; x <= numero; x++) { //loop de 1 ate numero introduzido
      console.log(x); //escreve na consola os valores de x
    }
  }
}
<h3>Loop Javascript - 1 + input</h3>
<input type="text" id="numero" placeholder="Introduza um numero" />
<button id="go">Go</button>

  • It worked, thank you very much. I didn’t know how to set this in FOR.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.