Hi Alex,

> However it cannot be achieved so it must be done usung if/else..

Well, this is JavaScript, there's almost always a way.  I can think of
four off the top of my head that keep it in a single expression -- but
all of them are much worse (most of them much, much, much worse) than
using an if/else. ;-)  The four are:

1. Massage the return value of the first thing you want to do to force
it to be the return value you want.  Actually, in your specific case,
you don't have to anything:

return $(arg) ? true : alert('Element Does not exist');

alert has no return value, hence your function returns either true or
undefined, which is good enough for anything branching on its return
value (undefined is falsey, after all).  If you really want a false,
use !! to force it.

Blech.

2. If you wanted to return something other than false, and if the
first part has an invariant result, you could manipulate that return
value to be falsey and use the OR operator, which is much more
powerful in JavaScript than in most languages (more here[1]):

return $(arg) ? "It's there" : (alert('Element Does not exist') ||
"It's not there");

or, demonstrating manipulation:

return $(arg) ? "It's there" : (!setTimeout(...) || "It's not there");

...since setTimeout returns a non-zero number; !setTimeout is false
and so the return value (for that second operand) is "It's not there".

Blech blech.

3. Wrap the two-part bit in an on-the-fly function:

return $(arg) ? true : (function(){ alert('Element Does not exist');
return false;})());

Blech blech cough.

4. Use eval (!):

return $(arg) ? true : eval("alert('Element Does not exist'); return
false;");

Blech blech cough retch.

I bet there are others.

So:  *Possible*, but if/else is just a way better way to go. ;-)

[1] http://blog.niftysnippets.org/2008/02/javascripts-curiously-powerful-or.html
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Jul 30, 1:37 pm, "Alex McAuley" <webmas...@thecarmarketplace.com>
wrote:
> Sorry you missed the point i was trying to achieve.
>
> I wanted the operator to in essence evaluate 2 responses for example.
>
> alert('Element Does not exist'); alert('The second responsee');
>
> However it cannot be achieved so it must be done usung if/else..
>
> Regards
>
> Alex Mcauleyhttp://www.thevacancymarket.com
>
>
>
>   ----- Original Message -----
>   From: Rick Waldron
>   To: prototype-scriptaculous@googlegroups.com
>   Sent: Thursday, July 30, 2009 1:18 PM
>   Subject: [Proto-Scripty] Re: ternary operators
>
>   Drop the parens around the first argument.
>
>   function foo(arg) {
>
>   return $(arg) ? true : alert('Element Does not exist');  // i commented 
> this out: false;
>
>   }
>   .......
>
>   On Tue, Jul 28, 2009 at 12:02 PM, Alex McAuley 
> <webmas...@thecarmarketplace.com> wrote:
>
>     In my usual "Not enough coffee" moments i just used an If/Else instead lol
>
>     Not sure why i was trying to cut code using a tenary ...
>
>     We live and learn
>
>     Sorry for useless post
>
>     Alex Mcauley
>    http://www.thevacancymarket.com
>
>     ----- Original Message -----
>     From: "Jeztah" <webmas...@thecarmarketplace.com>
>     To: "Prototype & script.aculo.us" 
> <prototype-scriptaculous@googlegroups.com>
>     Sent: Tuesday, July 28, 2009 4:51 PM
>     Subject: [Proto-Scripty] ternary operators
>
>     > Afternoon guys....
>
>     > Is it possible in javascript to give out 2 answers to a tenary
>     > opertor.... (doesnt make sense i know - see below)
>
>     > function foo(arg) {
>
>     > return ($(arg)) ? true : alert('Element Does not exist');false;
>
>     > }
>     > ...........
>     > (wrapped in window loaded function)
>
>     > foo('bazzzzzzzzzzzzz'); // doesnt exist so i want it to alert the
>     > alert and return false to halt the script.......
>
>     > <div id="bar"></div>
>
>     > Is this the right way to do it in the operator or cant it be done....
>     > and no i dont want to make 2 functions i would like it in one if it
>     > can be done.
>
>     > Regards
>     > Alex Mcauley
>
>     >http://www.thevacancymarket.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to