Why does TRUE return and not FALSE?

-2

<html>

<head>
  <title></title>
  <script type="text/javascript">
    var a = 0;
    var b = a == 0;

    function aoba() {
      document.getElementById('zbox').value = a;
      document.getElementById('ybox').value = b;
      a = a + 1;
    }
  </script>
</head>

<body>

  <button id="a" value="b" onclick="aoba()" name="botão">I'm gonna crazy.</button>
  <p>
    <input type="text" id="zbox">
  </p>
  <input type="text" id="ybox">
</body>

</html>

Why does the ybox field always appear true when it should appear false ?

    
asked by anonymous 19.08.2016 / 01:17

2 answers

4

Very simple ...

You have set the value of a = 0; and var b = a == 0; . At this time a will have the value of 0 and b will have the value of true. What shows the first values in inputs .

After clicking the button, you are updating the value of to for a + 1 . However, you are not updating the value of b , because when you click the button, it calls the function aoba() , and only runs that code below:

function aoba(){
    document.getElementById('zbox').value=a;
    document.getElementById('ybox').value=b;
    a = a+1;
}

That is, at no time is the value of the variable b being updated.

To update the b value, put it inside the aoba() function, like this:

function aoba(){
    var b = a == 0;
    document.getElementById('zbox').value=a;
    document.getElementById('ybox').value=b;
    a = a+1;
}

So the value of b will be updated as desired. The result can be seen in the code below:

var a = 0;

function aoba(){
    var b = a == 0;
	document.getElementById('zbox').value=a;
	document.getElementById('ybox').value=b;
	a = a+1;
}
<button id="a" value="b" onclick="aoba()" name="botão">I'm gonna crazy.</button>
<p>
	<input type="text" id="zbox">
</p>	
	<input type="text" id="ybox">

Enjoying, this list of questions offers great content to your doubt, directly or indirectly.

    
19.08.2016 / 03:41
-2

Set the variable "b" inside the function to update it.

    
19.08.2016 / 02:34