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.