470,863 Members | 1,276 Online

# boolean opposites

Is there a way to write this simpler?
function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}

Feb 15 '07 #1
10 1715
windandwaves wrote:
Is there a way to write this simpler?
function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}
a = !a;

--
Ian Collins.
Feb 15 '07 #2
On Feb 15, 2:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
windandwaves wrote:
Is there a way to write this simpler?
function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}

a = !a;

--
Ian Collins.
awesome! Thanks Ian...

Feb 15 '07 #3
windandwaves wrote:
On Feb 15, 2:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
>windandwaves wrote:
>>Is there a way to write this simpler?
function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}
a = !a;
Or:

a ^= true;

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Feb 15 '07 #4
windandwaves wrote on 15 feb 2007 in comp.lang.javascript:
On Feb 15, 2:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
>windandwaves wrote:
Is there a way to write this simpler?
function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}

a = !a;

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;};

--
Evertjan.
The Netherlands.
Feb 15 '07 #5
On Feb 15, 4:22 am, John W. Kennedy wrote:
<snip>
>>a = !a;

Or:

a ^= true;
But then - a - is numeric not boolean.

Richard.

Feb 15 '07 #6
In comp.lang.javascript message <11*********************@m58g2000cwm.goo
glegroups.com>, Thu, 15 Feb 2007 02:40:40, Richard Cornford
<Ri*****@litotes.demon.co.ukposted:
>On Feb 15, 4:22 am, John W. Kennedy wrote:
<snip>
>>>a = !a;

Or:

a ^= true;

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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
Feb 16 '07 #7
On Feb 15, 3:36 pm, Dr J R Stockton <reply0...@merlyn.demon.co.uk>
wrote:
In comp.lang.javascript message <1171536039.945256.56...@m58g2000cwm.goo
glegroups.com>, Thu, 15 Feb 2007 02:40:40, Richard Cornford
<Rich...@litotes.demon.co.ukposted:
On Feb 15, 4:22 am, John W. Kennedy wrote:
a ^= true;
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.
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

Feb 16 '07 #8
On Feb 15, 6:09 pm, "Isaac Schlueter" <isaacschlue...@gmail.com>
wrote:
On Feb 15, 3:36 pm, Dr J R Stockton <reply0...@merlyn.demon.co.uk>
wrote:
In comp.lang.javascript message <1171536039.945256.56...@m58g2000cwm.goo
glegroups.com>, Thu, 15 Feb 2007 02:40:40, Richard Cornford
<Rich...@litotes.demon.co.ukposted:
>On Feb 15, 4:22 am, John W. Kennedy wrote:
> a ^= true;
>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.

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
would be required, e.g,

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

.../rh

Feb 16 '07 #9
In comp.lang.javascript message <11**********************@a34g2000cwb.go
oglegroups.com>, Thu, 15 Feb 2007 18:09:51, Isaac Schlueter
<is************@gmail.composted:
>
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm- also batprogs.htm.
Feb 16 '07 #10
On Feb 16, 10:07 am, Dr J R Stockton <reply0...@merlyn.demon.co.uk>
wrote:
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

Feb 17 '07 #11

### This discussion thread is closed

Replies have been disabled for this discussion.