Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this correct?

Status
Not open for further replies.

javierdlm001

Technical User
Jun 28, 2008
264
CA
According to page 26 in this PDF tutorial from Tutorials Point, it says the result would be "true", but is actually giving "false".
It goes like this:

Assume variable A holds 10 and variable B holds 20, then:

1. && (Logical AND)
If both the operands are non-zero, then the condition becomes true.
Ex: (A && B) is true.

Along the lines above, also in the same page, the one below is not giving what they said it would.

! (Logical NOT)
Reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.
Ex: ! (A && B) is false.

document.write("!(a && b) => ");
result = (!(a && b));
document.write(result);
document.write(linebreak);

But it's giving me TRUE.

Also this one is not giving the expected result, in page 21:

<script>
<!--
var a = 10;
var b = 20;
var c = "Test";
var linebreak = "<br />";
a = a++;
document.write("a++ = ");
result = a++;
document.write(result);
document.write(linebreak);
b = b--;
document.write("b-- = ");
result = b--;
document.write(result);
document.write(linebreak);
//-->
</script>


 
The first comparison is correctly returning false for me. Make sure both a and b are defined before you do the comparison.

As to the ++ issue. That's has always been confusing. The gist of it, is that a=a++, will assign the value of a before it increments a by 1.
Basically a++ returns a which is assigned to a. And then it adds 1 to a.

A better explanation here(though it may still not make sense):
Difference betwen a++ and ++a
You can perform the addition directly without assigning it, and the variable will be updated.

Code:
a++;
//no assignment.

Your other option is to change the order:

Code:
a=++a;

This will ad 1 to a first, and then assign the resulting value back to a.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top