Connecting Tech Pros Worldwide Forums | Help | Site Map

boolean opposites

windandwaves
Guest
 
Posts: n/a
#1: Feb 15 '07
Is there a way to write this simpler?


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


Ian Collins
Guest
 
Posts: n/a
#2: Feb 15 '07

re: boolean opposites


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

--
Ian Collins.
windandwaves
Guest
 
Posts: n/a
#3: Feb 15 '07

re: boolean opposites


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

John W. Kennedy
Guest
 
Posts: n/a
#4: Feb 15 '07

re: boolean opposites


windandwaves wrote:
Quote:
On Feb 15, 2:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
>windandwaves wrote:
Quote:
>>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"
Evertjan.
Guest
 
Posts: n/a
#5: Feb 15 '07

re: boolean opposites


windandwaves wrote on 15 feb 2007 in comp.lang.javascript:
Quote:
On Feb 15, 2:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
>windandwaves wrote:
Quote:
Is there a way to write this simpler?
>>
Quote:
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.
(Please change the x'es to dots in my emailaddress)
Richard Cornford
Guest
 
Posts: n/a
#6: Feb 15 '07

re: boolean opposites


On Feb 15, 4:22 am, John W. Kennedy wrote:
<snip>
Quote:
Quote:
Quote:
>>a = !a;
>
Or:
>
a ^= true;
But then - a - is numeric not boolean.

Richard.

Dr J R Stockton
Guest
 
Posts: n/a
#7: Feb 16 '07

re: boolean opposites


In comp.lang.javascript message <1171536039.945256.56680@m58g2000cwm.goo
glegroups.com>, Thu, 15 Feb 2007 02:40:40, Richard Cornford
<Richard@litotes.demon.co.ukposted:
Quote:
>On Feb 15, 4:22 am, John W. Kennedy wrote:
><snip>
Quote:
Quote:
>>>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.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Isaac Schlueter
Guest
 
Posts: n/a
#8: Feb 16 '07

re: boolean opposites


On Feb 15, 3:36 pm, Dr J R Stockton <reply0...@merlyn.demon.co.uk>
wrote:
Quote:
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:
>
Quote:
On Feb 15, 4:22 am, John W. Kennedy wrote:
Quote:
a ^= true;
>
Quote:
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

ron.h.hall@gmail.com
Guest
 
Posts: n/a
#9: Feb 16 '07

re: boolean opposites


On Feb 15, 6:09 pm, "Isaac Schlueter" <isaacschlue...@gmail.com>
wrote:
Quote:
On Feb 15, 3:36 pm, Dr J R Stockton <reply0...@merlyn.demon.co.uk>
wrote:
>
Quote:
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:
>
Quote:
Quote:
>On Feb 15, 4:22 am, John W. Kennedy wrote:
> a ^= true;
>
Quote:
Quote:
>But then - a - is numeric not boolean.
>
Quote:
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
this be readily readable and understood by others, advanced structure
would be required, e.g,

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

.../rh








Dr J R Stockton
Guest
 
Posts: n/a
#10: Feb 16 '07

re: boolean opposites


In comp.lang.javascript message <1171591791.910268.320200@a34g2000cwb.go
oglegroups.com>, Thu, 15 Feb 2007 18:09:51, Isaac Schlueter
<isaacschlueter@gmail.composted:
Quote:
>
>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.
Isaac Schlueter
Guest
 
Posts: n/a
#11: Feb 17 '07

re: boolean opposites


On Feb 16, 10:07 am, Dr J R Stockton <reply0...@merlyn.demon.co.uk>
wrote:
Quote:
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

Closed Thread


Similar JavaScript / Ajax / DHTML bytes