Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2012, 02:56 AM   PM User | #1
Philosophaie
New Coder

 
Join Date: Sep 2010
Posts: 46
Thanks: 1
Thanked 1 Time in 1 Post
Philosophaie is an unknown quantity at this point
Javascript code meaning

I am having trouble with the meaning of this piece of Javascript code:

Code:
x=((next - present) == 356) ? 2 : (((present - last) == 382) ? 1 : 0);
Could someone explain it for me.
Philosophaie is offline   Reply With Quote
Old 12-07-2012, 03:58 AM   PM User | #2
rdspoons
New Coder

 
Join Date: Jun 2009
Posts: 81
Thanks: 0
Thanked 8 Times in 8 Posts
rdspoons is on a distinguished road
x=((next - present) == 356) ? 2 : (((present - last) == 382) ? 1 : 0);


next present and last are to hold numeric values

the JavaScript ternary operator is used to assign a value to x
x = (comparison_that_resolves_to_true_or_false)? value_returned_if_true : value_returned_if_false;

In your code provided x is assigned the value 2 if next is greater than present by exactly 356.
If next is any other value, x is assigned the value 1 if present is greater than last by exactly 382.
If present is any other value, x is assigned the value of 0.

in other words,

x is assigned the value 2 if next-present equals 356.
if next-present does not equal 356 then
x is assigned the value 1 if present-last equals 382.
if present-last does not equal 382 then
x is assigned the value 0.
rdspoons is offline   Reply With Quote
Old 12-07-2012, 06:15 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
A longer version of the same code that does exactly the same thing using if statements instead of ternary operators is:

Code:
if ((next - present) == 356) x = 2;
else if ((present - last) == 382) x = 1;
else x=  0;
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-07-2012, 07:28 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by felgall View Post
A longer version of the same code that does exactly the same thing using if statements instead of ternary operators is:

Code:
if ((next - present) == 356) x = 2;
else if ((present - last) == 382) x = 1;
else x=  0;
Longer, but much clearer. Doubt if there is any discernable speed difference.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-07-2012, 08:42 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Philip M View Post
Doubt if there is any discernable speed difference.
In terms of execution time I would guess that if you ran a few billion iterations that the speed difference might just about become large enough to measure in milliseconds.

I haven't actually counted the number of ternary references in jQuery but their use there possibly saves Gigabytes a month of bandwidth on the servers delivering copies of the library to the hundreds of millions of people visiting pages that use that library - making it well worth the trouble of using them as much as possible in place of if statements for that library. Individuals viewing the pages still wouldn't notice any significant difference in load times though.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:21 AM.


Advertisement
Log in to turn off these ads.