Wrox Programmer Forums
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 24th, 2015, 05:51 AM
Registered User
Join Date: Jun 2015
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Error found ?

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

Old June 27th, 2015, 02:24 AM
Friend of Wrox
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts

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.

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 09: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 10:20 PM
Page Not Found Error ztz02 Classic ASP Basics 2 June 25th, 2003 08:52 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.