Velocity Reviews > addition

Paul
Guest
Posts: n/a

 12-06-2006
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

VK
Guest
Posts: n/a

 12-06-2006

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);

Paul
Guest
Posts: n/a

 12-06-2006
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

Matt Kruse
Guest
Posts: n/a

 12-06-2006
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

ASM
Guest
Posts: n/a

 12-06-2006
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

Dr J R Stockton
Guest
Posts: n/a

 12-07-2006
In comp.lang.javascript message
<(E-Mail Removed)>, Wed, 6 Dec 2006 21:26:04,
Paul <(E-Mail Removed)> wrote:
>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

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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jiten VHDL 3 06-23-2005 05:26 AM Clemens Bosch VHDL 0 12-02-2004 02:20 PM Julian VHDL 13 10-21-2004 05:46 AM Midnight Java Junkie ASP .Net 0 06-20-2004 07:24 AM lezah VHDL 7 03-02-2004 06:11 PM