Why doesn’t the loop print up to the last value that was entered as the limit?

Asked

Viewed 64 times

0

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

Example: first number typed is 3 and the 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>

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

The problem is the comparison operator. You are having repeat while the value of the control variable inicio is less than fim, then you’ll finish a number first, because the moment it’s equal is no longer less, it’s equal. If you want the latter to be printed, that is, it is inclusive you have to use the smaller or equal operator (<=), thus:

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 put in the Github for future reference.

  • Sure, it makes sense. I’m really grateful for the help, Maniero! !

1

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

I believe that maybe you are starting in Javascript, if yes, cool guy, you started in an interesting way, already separating your code in functions to reuse code. And if you are really early, a tip would be you already start with the new patterns, but this is another kk story. I made some improvements to your code, in case you want to take a look I’ll leave 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
}
  • Oh yeah Eduardo? That’s right, I’m starting Javascript. I’m really grateful for your help! Abs

Browser other questions tagged

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