Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning JavaScript 5th Edition
This is the forum to discuss the Wrox book Beginning JavaScript, 5th Edition by Jeremy McPeak; 978-1-118-90333-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 5th Edition section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 24th, 2015, 06:51 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2015
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to PiotrG
Exclamation Error found ?

Hey..
Correct me if i'm wrong but in chapter 2 in section Increment and Decrement Operators there is something like this:

myNumber = 1;
myVar = (myNumber++ * 10 + 1);

" What value will myVar contain? Well, because the ++ is postfixed (itís after the myNumber variable), it will be incremented afterward. So the equation reads: Multiply myNumber by 10 plus 1 and then increment myNumber by one.
myVar = 1 * 10 + 1 = 11
Then add 1 to myNumber to get 12, but do this after the value 11 has been assigned to myVar. "

In which moment we get myNumber = 12 ?

Shouldn't myNumber be just '2' instead of '12' ?
In my calculations the result is:

var myNumber = 1;
var myVar = (myNumber++ *10 +1);
console.log(myNumber); // = 2
console.log(myVar); // = 11

Greetings
Reply With Quote
  #2 (permalink)  
Old June 27th, 2015, 03:24 AM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

myNumber is equal to 2 and will only be 2 in this expression. However, from what you are suggesting here it is asking what is the value of "myVar" which is different than that of "myNumber". By order of operation on the expression "myVar" is 11, multiplication must happen before an addition, and since post fix increment means "evaluate then increment", we must evaluate the entire expression (multiplication first then addition) then increment the value of myNumber, thus the 2 following set of statements are equivalent

myNumber = 1;
myVar = (myNumber++ * 10 + 1);

is the same as

myNumber = 1;
myVar = (((myNumber * 10)+1)); myNumber += 1;


where we first evaluate the multiplication and addition expression then the increment expression.

all though the 2 set of statement are equivalent 1 is better than the other, the first set of statement is open to sight interpretation error and is less self documenting than the second.

in the second set we can verbosely see the statement evaluation order and is less apt to need comments to explain the coarse of action (assuming we have more to go one above and below the set of statements).

in short the shorter statement is not always best.
Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Found an error in the book padhma BOOK: Microsoft SQL Server Reporting Services Recipes: for Designing Expert Reports 0 November 8th, 2011 07:34 PM
ERROR: Theme cannot be found ppeessaarr ASP.NET 2.0 Professional 2 July 16th, 2007 10:06 AM
" Error" Page not found shoakat Classic ASP Databases 1 November 15th, 2004 03:59 AM
package not found error nightsurfer JSP Basics 8 October 13th, 2003 11:20 PM
Page Not Found Error ztz02 Classic ASP Basics 2 June 25th, 2003 09:52 AM



All times are GMT -4. The time now is 07:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.