addition

P

Paul

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
 
V

VK

Paul said:
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);
 
P

Paul

Paul said:
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
 
A

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
 
D

Dr J R Stockton

In comp.lang.javascript message
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top