Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript 5th Edition
| 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 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
  #1 (permalink)  
Old July 1st, 2018, 07:05 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2018
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Cool 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.
  #2 (permalink)  
Old July 7th, 2018, 03:37 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2018
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.


Similar Threads
Thread Thread Starter Forum Replies Last Post
autoScale property not working as expected whiterainbow BOOK: Professional Outlook 2007 Programming ISBN: 978-0-470-04994-5 0 April 20th, 2011 07:49 AM
Chapter 10/Live Folder - UriMatcher not performing as expected. emanuel BOOK: Professional Android 2 Application Development 1 June 4th, 2010 05:52 PM
Dataset relations not working like expected panda869 C# 0 March 2nd, 2008 12:04 PM
Incrementing attribute value Pankaj C XSLT 4 October 31st, 2007 05:40 AM
Incrementing a value within a template Tre XSLT 1 March 21st, 2007 09:31 AM





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