Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Javascript code meaning

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 12-07-2012, 03:56 AM PM User | #1 Philosophaie New Coder   Join Date: Sep 2010 Posts: 46 Thanks: 1 Thanked 1 Time in 1 Post 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.
 12-07-2012, 04:58 AM PM User | #2 rdspoons New Coder   Join Date: Jun 2009 Posts: 81 Thanks: 0 Thanked 8 Times in 8 Posts 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.
 12-07-2012, 07:15 AM PM User | #3 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,097 Thanks: 0 Thanked 579 Times in 569 Posts 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/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.
12-07-2012, 08:28 AM   PM User | #4
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,487
Thanks: 200
Thanked 2,471 Times in 2,449 Posts
Quote:
 Originally Posted by felgall 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.

12-07-2012, 09:42 AM   PM User | #5
felgall
Master Coder

Join Date: Sep 2005
Location: Sydney, Australia
Posts: 6,097
Thanks: 0
Thanked 579 Times in 569 Posts
Quote:
 Originally Posted by Philip M 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/

Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 06:14 AM.