Wrox Programmer Forums
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 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 February 18th, 2004, 02:51 PM
Registered User
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dual use of the + symbol

I recently expanded on some code used as a sample in the BegJS book for determining a day so many days out from the current date. I expanded the code to ask if the user wanted to go forward or backward and how many days they wanted to go back or forward. This latter data I requested by setting up a variable, called wantedDate, and using the prompt function to get the data from the user. The problem I had, and did fix, was when going forward the code used the + operator, the code looked like this:

nowDate.setDate(currentDay + wantedDate);

The problem was JS saw the data in the wantedDate variable as a character string and so used the + symbol to concatenate the info with a result of the currentDay and the wantedDate value joined together giving an erronious result, i.e. currentDay was 17 and wantedDate was 2, so result was 172 and this was added to the nowDate and gave the wrong result.

What I think happened was since I was using the + symbol for some reason the data in the wantedDate was seen as a character string and so JS made the decision to concatenate. Yet the same data in the wantDated variable when used with the - operator was used as numeric data which was the intent. To get the code to see the data in the wantedDate variable as numeric data for the + operator I had to add the parseInt( ) function to convert the value to be seen as a numeric value.

nowDate.setDate(currentDay + parseInt(wantedDate);

My question is why when using the + operator was the wantedDate data seen as a character string in one area of the code and yet seen as numeric data in the other area of code, the - operator part?
 
Old February 19th, 2004, 05:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

The simple answer is that there is no - operator defined for strings so that statement was not ambiguous, whereas the + operator was ambiguous.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dual Boot - Windows TonyG Linux 3 December 28th, 2005 10:56 AM
Two Forms on Dual Monitors nkew C# 0 October 27th, 2005 08:10 PM
Javascript and dual-screen? gray Javascript How-To 0 March 17th, 2005 10:22 AM
dual use of the + symbol Martyn JSP Basics 0 February 19th, 2004 07:35 AM
Dual Screens Louisa Beginning VB 6 1 November 9th, 2003 05:44 PM





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