Forum Moderators: open

Message Too Old, No Replies

Why is this appending instead of calculating

         

Champak

4:52 pm on Mar 19, 2007 (gmt 0)

10+ Year Member



the ff_getElement stuff is just how this form reads text input. But at the end of the day baseincome will be 57 if I do 5 + 7. But from whay I understand the third line in this is correct to do addition.

var p = ff_getElementByName('bor_base_income_data').value;
var q = ff_getElementByName('co_bor_base_income_data').value;
var baseincome = p + q;

ff_getElementByName('total_base_data').value = baseincome;

Fotiman

5:11 pm on Mar 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



+ is used for both addition and string concatination. Your p and q values are string values, so it concatinates them.

If these values will be integers, you could try something like this:

var p = parseInt(ff_getElementByName('bor_base_income_data').value,10);
var q = parseInt(ff_getElementByName('co_bor_base_income_data').value, 10);
var baseincome = p + q;

Champak

5:34 pm on Mar 19, 2007 (gmt 0)

10+ Year Member



Thanks that works.

1/ So I understand, what is happening here, What is the 10 for? Just for argument sake, what if I used a 7?
2/ When entering the numbers in the inputbox for calculation, after I enter the first number, this throws up "NaN" in the result inputbox until the other number is entered. How do I prevent that?

[edited by: Champak at 5:35 pm (utc) on Mar. 19, 2007]

BlobFisk

6:07 pm on Mar 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The second value is the radix value, or the base number system to be used. For example, 2 in the binary system, 8 in the octal system, 10 in the decimal system, and 16 in the hexadecimal system.

This is an optional parameter. If it is not included the following assumptions are made:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with any other value, the radix is 10 (decimal)

HTH

Fotiman

8:02 pm on Mar 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




1/ So I understand, what is happening here, What is the 10 for? Just for argument sake, what if I used a 7?

As BlobFisk mentioned, this is the radix value. Though you *might* be able to leave this out, it's always safer to include it (as base 10 if you're using normal numbers). Also, if the value begins with "0x", it will try to parse it as a hexadecimal number. If it begins with "0", it parses the number in octal. Otherwise, it parses it as decimal.


2/ When entering the numbers in the inputbox for calculation, after I enter the first number, this throws up "NaN" in the result inputbox until the other number is entered. How do I prevent that?

Add some validation to make sure the user has entered a value.


var p = parseInt(ff_getElementByName('bor_base_income_data').value,10);
var q = parseInt(ff_getElementByName('co_bor_base_income_data').value, 10);
p = ( isNaN(p)? 0 : p );
q = ( isNaN(q)? 0 : q );
var baseincome = p + q;

In this example, I've added a check so that if p = NaN (Not a Number), then it sets p to 0. Likewise, it does the same check on q. So with no values (or invalid value) entered, both will default to zero.

[edited by: Fotiman at 8:05 pm (utc) on Mar. 19, 2007]

rocknbil

7:55 am on Mar 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a problem with Javscript that has always driven me crazy, you're fine if you're just using integers but if it gets into decimals it gets wildly out of hand. Ive also seen two variables that are a result of parseInt() added that get re-concatenated. :-(

Floating point precision also drives me nuts. My floating point thread of doom [webmasterworld.com].