Why does the loop not print until the last value was entered as a limit?

0

I made a code that prints a sequence of numbers, from the number entered to the end, but when I print it the last number is not appearing.

Example: first number entered is 3 and second is 7. The sequence would be 3, 4, 5, 6, 7, but 7 does not appear.

function id(valor_campo)
  {
    return document.getElementById(valor_campo);
  }

function getValor(valor_campo)
  {
    var valor = document.getElementById(valor_campo).value;
    return (valor);
  }

function nums(valores)
  {	
    var inicio = getValor("num1");
    var fim = getValor("num2");
    var res = 0;
    while (inicio < fim)
    {
      res += (inicio)+"<br>";
      inicio++;

    }
    document.getElementById("demo").innerHTML = res;
  }
<html>
<head>
	<meta charset="utf-8">
</head>
<body>
    
    Valor 1: <input type="number" name="num1" id="num1"> <br><br>

    Valor 2: <input type="number" name="num2" id="num2"> <br><br>

    <button onclick="nums()">Saiba a sequência dos números digitados!</button>

    <p id="demo"></p>
</body>
</html>
    
asked by anonymous 25.10.2018 / 03:22

2 answers

2

As stated above, the problem is in the comparison operator, just replace < with <= .

I think you might be starting with JavaScript, if that's cool, you started in an interesting way, already separating your code into functions for reusing code. And if you're really in the beginning, a tip would be you already start with the new patterns, but that's another story kk. I made some improvements in your code, if you want to make an observation I'll leave it below.

function id (valor_campo)
{
  return document.getElementById(valor_campo);
}

function getValor (valor_campo)
{
  var valor = id(valor_campo).value; // Utilizando a Função id que você criou
  return (valor);
}

function nums () // retirando o parametro que não estava sendo usado
{   
  var inicio = getValor("num1");
  var fim = getValor("num2");
  var res = ''; // tirando o 0 que era impresso e inserindo uma string vazia, pois não é exibida
  while (inicio <= fim) // Arrumando o bug do operado
  {
    res += (inicio)+"<br>";
    inicio++;
  }
  id("demo").innerHTML = res; // utilizando mais uma vez a sua função
}
    
25.10.2018 / 04:26
2

The problem is the comparison operator. You are prompted to repeat as long as the value of the control variable inicio is less than fim , then it will finish a number before, because at the same time it is not less, it is the same. If you want the last one to be printed, ie it is inclusive, you must use the lesser or equal operator ( <= ), like this:

function id(valor_campo) { return document.getElementById(valor_campo); }

function getValor(valor_campo) { return document.getElementById(valor_campo).value; }

function nums(valores) {   
    var inicio = getValor("num1");
    var fim = getValor("num2");
    var res = 0;
    while (inicio <= fim) res += inicio++ + "<br>";
    document.getElementById("demo").innerHTML = res;
}
Valor 1: <input type="number" name="num1" id="num1"> <br><br>

Valor 2: <input type="number" name="num2" id="num2"> <br><br>

<button onclick="nums()">Saiba a sequência dos números digitados!</button>

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

I placed GitHub for future reference .

    
25.10.2018 / 03:29