boolean opposites

W

windandwaves

Is there a way to write this simpler?


function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}
 
E

Evertjan.

windandwaves wrote on 15 feb 2007 in comp.lang.javascript:
awesome! Thanks Ian...

function makeOpposite(a) {}

Since the OP's function does not return anything
and does not change a global variable,
an empty function is the "simplest".

=============================

However I surmize the OP would like this one:

function makeOpposite(a) {return !a;};
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 15 Feb 2007 02:40:40, Richard Cornford
On Feb 15, 4:22 am, John W. Kennedy wrote:


But then - a - is numeric not boolean.

Consequentially and more importantly, if the original a was an integer
greater than 1, then both a and the result would test as true.

It's a good idea to read the newsgroup and its FAQ. See below.
 
I

Isaac Schlueter

In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 15 Feb 2007 02:40:40, Richard Cornford



Consequentially and more importantly, if the original a was an integer
greater than 1, then both a and the result would test as true.

Easy enough to fix! Just cast to boolean before and after.
a = !!(!!a ^ true);

Or my favorite js wtf, found in actual production code at a company
where I once worked:
x = (!!x ? !!true : !!false);
Apparently the author wanted to make really super duper certain he got
a boolean.

--i
 
R

ron.h.hall

Easy enough to fix! Just cast to boolean before and after.
a = !!(!!a ^ true);

Perhaps, but then because good coding practices demand code such as
this be readily readable and understood by others, advanced structure
would be required, e.g,

case a=!!(!!a ^ true) :-D ;

.../rh
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Thu, 15 Feb 2007 18:09:51, Isaac Schlueter
Or my favorite js wtf, found in actual production code at a company
where I once worked:
x = (!!x ? !!true : !!false);
Apparently the author wanted to make really super duper certain he got
a boolean.

That approach would not be safe in all languages. ISTR one in which
Boolean was nothing more than a predefined enumerated type in the
outermost scope, so that it would be possible to declare new variables
true and false and assign to them respectively 2>3 and 3>2.
 
I

Isaac Schlueter

That approach would not be safe in all languages. ISTR one in which
Boolean was nothing more than a predefined enumerated type in the
outermost scope, so that it would be possible to declare new variables
true and false and assign to them respectively 2>3 and 3>2.

Yeah, you're right. It's been a while since I worked with VB6, but I
remember being very very confused that (Not 3) yielded -2, which is
truish. It had something to do with the fact that Not in vb6 is
always bitwise. (Similarly, 3 + True = 2.)

--i
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top