Why does it return TRUE and not FALSE?

Asked

Viewed 117 times

-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 in the field ybox always appears true when you should show up false?

  • It should not appear false, that’s correct, when you create "b" the value of "a" is zero, then the value of "b" is true, and you do not change the value of "b" anywhere after that.

  • 5

    When you ask a question like "it should happen" explain why you think it should happen. You can [Dit] the question.

  • Remove the image and put the real code to test.

  • I thought that every time b was accessed it would obligatorily check a and update the condition of true or false.

2 answers

4

Very simple...

You set the value of a = 0; and of var b = a == 0;. At this time to shall have the value of 0 and b will have the value of true. Which 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 by clicking the button, it calls the function aoba(), and is executed only that code below:

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

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

To update the value of b, put it into the function aoba(), in this way:

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

Thus the value of b will be updated as you wish. 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">

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

  • Now I understand, thank you very much.

  • I was with a question related to an excerpt of a script that I am creating, while I was waiting answer I ended up able to dribble the problem with some creativity, I have no course in the area, I do to pass the time, excuse me if I sometimes ask some rough question, stuck down the way I dribbled the problem.

  • /excFormula/if(det==0&&per==0&&rep==0){var excFormula=true}Else{excFormula=false}; /otmFormula/if(det == 0 && per == 1 && rep <= 1) || (det == 0 && per <= 2 && rep == 0) || (det == 0 && per == 0 && rep <= 2)){var otmFormula=true}Else{otmFormula=false}; /goodFormula/if(det == 0 && per <= 2 && rep == 0) || (det == 0 && per <= 1 && rep <= 2) || (det == 0 && per == 0 && rep <= 4)){var bomFormula=true}Else{bomFormula=false}; /regFormula/if(){var regFormula=true}Else{regFormula=false};

  • @Danieldesantanasforzim What matters is the way the question is elaborated, and not whether it is a "crude" doubt or not. About this script you posted, there is how to improve it (I don’t even know if it is working properly). But if you want some hint, open another question explaining better what you want, which will be easier to get help.

  • Yes, it works, it has a way of letting it dry?

  • @Danieldesantanasforzim has yes, but this is not the subject for this question. You can open another one explaining what you want with the code and such.

  • Obg!!!!!!!!!!!!!!!!

Show 2 more comments

-2

Seven variable "b" within the function to update it.

Browser other questions tagged

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