473,699 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

addition

Why is the addition here adding the number as a string but the subtraction
works fine?

function boxchange(box) {
iv= dbform.ut.value
if (box.checked == true) {
iv = iv - box.value
}
else {
iv = iv + box.value
}
dbform.ut.value =iv
}

so on the addition with the ut text box having a value of 1 It adds 1+2 and
gets 12 ,but on the subtraction 12 -1 gives 11
Dec 6 '06 #1
5 2276
VK

Paul wrote:
Why is the addition here adding the number as a string but the subtraction
works fine?

function boxchange(box) {
iv= dbform.ut.value
if (box.checked == true) {
iv = iv - box.value
}
else {
iv = iv + box.value
}
dbform.ut.value =iv
}

so on the addition with the ut text box having a value of 1 It adds 1+2 and
gets 12 ,but on the subtraction 12 -1 gives 11
Because form control value is string by default. As there is no
substraction operator for strings, the engine tries to convert both
strings into numbers.

But addition operator acts as concatenator for string values, so
stringValue + stringValue is a valid string concatenation statement, no
conversion into numbers will be done.

iv-= box.value;
....
iv+= (+box.value);

Dec 6 '06 #2
Paul wrote:
Why is the addition here adding the number as a string but the subtraction
works fine?

function boxchange(box) {
iv= dbform.ut.value
if (box.checked == true) {
iv = iv - box.value
}
else {
iv = iv + box.value
}
dbform.ut.value =iv
}

so on the addition with the ut text box having a value of 1 It adds 1+2
and
gets 12 ,but on the subtraction 12 -1 gives 11
Because form control value is string by default. As there is no
substraction operator for strings, the engine tries to convert both
strings into numbers.

But addition operator acts as concatenator for string values, so
stringValue + stringValue is a valid string concatenation statement, no
conversion into numbers will be done.

iv-= box.value;
....
iv+= (+box.value);
Thanks got it now
Dec 6 '06 #3
ASM
Paul a écrit :
Why is the addition here adding the number as a string but the subtraction
works fine?
value of a text field is always a string (not a number)
function boxchange(box) {
iv= dbform.ut.value
if (box.checked == true) {
iv = iv - box.value
or iv -= box.value
}
else {
iv = iv + box.value
iv = iv + Number(box.valu e);
or
iv = iv + box.value*1
or
iv = +box.value+iv;
}
dbform.ut.value =iv
}
so on the addition with the ut text box having a value of 1 It adds 1+2
the sign + is used both to add text or numbers and JS doesn't know in
advance if '2' is or isn't a character for you
(here, as '2' comes from a text field
it is a character without hesitation)
gets 12 ,but on the subtraction 12 -1 gives 11
no mistake possible with '-' because it is only used with numbers
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 6 '06 #4
Paul wrote:
Why is the addition here adding the number as a string but the
subtraction works fine?
http://www.javascripttoolbox.com/bestpractices/#plus

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 6 '06 #5
In comp.lang.javas cript message
<3O************ *************** ***@bt.com>, Wed, 6 Dec 2006 21:26:04,
Paul <no******@btope nworld.comwrote :
>Why is the addition here adding the number as a string but the subtraction
works fine?
>if (box.checked == true) {
^^^^^^^^ superfluous.

It's a good idea to read the newsgroup and its old FAQ before posting.
See below. That way you can save the time of everyone here, including
yourself. And you'll discover other things too, including something
about the formatting of newsgroup replies.

FAQ 4.21 refers.

Looking at your code :
(1) IV is not declared, hence is global. Declare it local, with var.
(2) Rather than evaluating box.value twice, it is neater to do so once
and use it twice.

var IV = dbform.ut.value
var BV = box.value
box.checked ? IV += BV : IV -= BV

IV and BV are still strings, hence concatenation still occurs ...

If a text control is for entry of a pure number, and no specific
validation of the textual form will be done, then I recommend
(a) Reading it once, into a variable
(b) and getting a Number in the variable, by using unary + (or -).

IV = +dbform.ut.valu e
var BV = +box.value
IV += box.checked ? +BV : -BV // addition/subtraction

One of those + is now superfluous, but should be retained for
readability.
REGULARS :
When someone answers a question that is manifestly treated in the FAQ,
he/she is presumably ignorant of the FAQ and liable to have other
questions treated in the FAQ. Therefore, your answers should include a
reference to the FAQ.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.c om/faq/ Old RC FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3432
by: Mark Dickinson | last post by:
Can anyone either reproduce or explain the following apparently inconsistent behaviours of __add__ and __mul__? The class Gaussian provides a sub-bare-bones implementation of Gaussian integers (a Gaussian integer is a complex number x+yi for which both x and y are integers): class Gaussian(object): """class representing Gaussian integers"""
7
7697
by: jeffbernstein | last post by:
Greetings. I'm reading "How to think like a computer scientist: Learning with Python" and there's a question regarding string operations. The question is, "Can you think of a property that addition and multiplication have that string concatenation and repetition do not?" I thought it was the commutative property but "<string>"*3 is equivalent to 3*"<string>". Any ideas?
3
5220
by: Jay | last post by:
I have two strings that instead of adding them together to get the sum the are concatenating together. Does anyone know how I can get these two to add. while not rstemp4.eof vservdate = rstemp4("servdate") vdesc = rstemp4("desc") vservhours2 = rstemp4("servhours") vtravelhours = rstemp4("travelhours") vtech = rstemp4("tech")
2
1624
by: akickdoe22 | last post by:
i could really use help finishing this addition program. I'm stuck on the part that allows you to add any two large integers,up to 100 digits,(pos+pos, neg+neg, and pos+neg). could use hints ideas code anything for neg+pos addition. CODE SO FAR: class INT { int digits; char sign; public:
24
2711
by: Alex Vinokur | last post by:
Consider the following statement: n+i, where i = 1 or 0. Is there more fast method for computing n+i than direct computing that sum? -- Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html
34
16672
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1 - 3;
3
2181
by: snow.carriers | last post by:
Let me first state that I'm using Borland Turbo C++, it's relatively old so the new string methods won't work. Anyways, first I'm trying to collect a line of a string (with numbers, letters, dashes) into each variable. For just numbers, it's relatively easy: ifstream fout("s1.in"); for (a=0; a<17; a++) { fout >> data; cout << data << endl; }
5
4592
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express Edition program to control a remote basketball scoreboard display unit. All I'm trying to do is add 5 byte variables and store the result in an integer variable. I added a Try/Catch block to take look at things. This exception occurs only when...
3
1608
by: srinivas33034 | last post by:
Hi there, my problem is i have to perform addition dyamically My req is i have 3 txt boxes.. and another text box to display total additon.. as i am entering the values in TextBox1 i want to see the updated addition performed so far in the final textbox..
0
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9033
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8911
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7748
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6533
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5872
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.