for loop

  • Thread starter Christina Grafström
  • Start date
C

Christina Grafström

Hi,

I need help to make this a loop.

dsum1 = document.form1.c1.value;
dsum1 -=0;
dsum2 = document.form1.c2.value;
dsum2 -=0;
dsum3 = document.form1.c3.value;
dsum3 -=0;
dsum4 = document.form1.c4.value;
dsum4 -=0;
dsum5 = document.form1.c5.value;
dsum5 -=0;
dsum6 = document.form1.c6.value;
dsum6 -=0;

saldo = dsum1 + dsum2 + dsum3 + dsum4 + dsum5 + dsum6;

Thanks in advance.
Jan Grafström
 
L

Lasse Reichstein Nielsen

Christina Grafström said:
I need help to make this a loop.

dsum1 = document.form1.c1.value;
dsum1 -=0; ....
dsum6 = document.form1.c6.value;
dsum6 -=0;

saldo = dsum1 + dsum2 + dsum3 + dsum4 + dsum5 + dsum6;

I assume you don't want the six global variables dsum1 ... dsum6 if you
can avoid them.

var form = document.forms['form1'];
var saldo = 0;
for (var i=1 ; i<=6 ; i++) {
saldo += +(form.elements["c"+i].value);
}

/L
 
D

Da Costa Gomez

Lasse said:
I need help to make this a loop.

dsum1 = document.form1.c1.value;
dsum1 -=0;
...

dsum6 = document.form1.c6.value;
dsum6 -=0;

saldo = dsum1 + dsum2 + dsum3 + dsum4 + dsum5 + dsum6;


I assume you don't want the six global variables dsum1 ... dsum6 if you
can avoid them.

var form = document.forms['form1'];
var saldo = 0;
for (var i=1 ; i<=6 ; i++) {
saldo += +(form.elements["c"+i].value);
}
Very interesting notation. I get the += and the (form.....) bit as well
but why the + in front of the (form...)?

TIA
Fermin DCG
btw. thx for the inheritance tip
 
L

Lasse Reichstein Nielsen

Da Costa Gomez said:
why the + in front of the (form...)?

The prefix plus sign is the shortest and fastest way of turning a
string into a number. It is equivalent in function to parseFloat.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
The prefix plus sign is the shortest and fastest way of turning a
string into a number. It is equivalent in function to parseFloat.

Provided that the string contains only the number and optional
surrounding whitespace.
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
Provided that the string contains only the number and optional
surrounding whitespace.

That is correct. The functions parseInt and parseFloat allows garbage
after the number, so parseInt("127XPgLop",10)==127. Probably so that
one can use it in a parser (but it doesn't tell how many charcters
it reads, so it is useless in making a parser).

It would be more precise to say that prefix plus is equivalent to the
function "Number".

/L
 

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

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top