p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning JavaScript 5th Edition (http://p2p.wrox.com/forumdisplay.php?f=793)
-   -   Chapter 2 - incrementing now working as expected (http://p2p.wrox.com/showthread.php?t=99924)

Cilvako July 1st, 2018 07:05 AM

Chapter 2 - incrementing now working as expected
 
In chapter 2 we have the following example:

Code:

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

In the book we are told the result for myVar should be 12, but after testing it a few times I got 11 each and every time.

How is this possible? Clearly, incrementing is not working as expected.

Natured July 7th, 2018 03:37 PM

Bit of a mixup.
 
This seems to be a minor error, the code actually skips over the ++; operator entirely to get the desired result you can re-write this code with a simple change.

Code:

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

This would yield the desired result. You can also place "++" on the Pre side instead of the Post side of the variable. (shown below)


Code:

myVar = (++myNumber * 10 + 1);
However this is interpreted as: 2 * 10 + 1 which is 21.

Hopefully this helps clear up any confusion.


All times are GMT -4. The time now is 11:58 PM.

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