I have some code that may not be funny, but it surely is funnily strange. It's about how javascript handles
and/or statements. An
and/or statement is only false when both members of the statement are false (as opposed to
either...or statements that are only false when the members are both true or both false).
So this:
Premise: x1=x2
Conclusion: (since x1=x2), 'x1 is bigger than or equal to x2'
is a true statement.
Let's convert that to javascript. If we put:
Code:
<script>
var x1, x2;
x1=x2;
if(x1==x2 || x1>x2){alert('CORRECT.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or).')} else {alert('WRONG.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or).\nWhy am I getting this "else" alert that says I`m wrong?')}
</script>
we get the expected result.
But if we put
Code:
<script>
var x1, x2;
x1=x2;
if(x1>=x2){alert('CORRECT.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or)')} else {alert('WRONG.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or).\nWhy am I getting this "else" alert that says I`m wrong?')}
</script>
we incorrectly get a non-expected result.
So using '||' gives the expected outcome, whereas '>=' makes trouble.
What makes things even more weird is that '>=' stops annoying us when we assign concrete values to x1 and x2, as in:
Code:
<script>
var x1=1, x2=1;
x1=x2;
if(x1>=x2){alert('CORRECT.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or)')} else {alert('WRONG.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or).\nWhy am I getting this "else" alert that says I`m wrong?')}
</script>
What is going on here?
===
Arie.
Bookmarks