473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2255
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.value);
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.javascript message
<3O******************************@bt.com>, Wed, 6 Dec 2006 21:26:04,
Paul <no******@btopenworld.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.value
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.com/faq/ Old RC FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.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
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...
7
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...
3
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 =...
2
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...
24
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...
34
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
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,...
5
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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,...

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.