Connecting Tech Pros Worldwide Forums | Help | Site Map

for loop

Christina Grafström
Guest
 
Posts: n/a
#1: Jul 20 '05
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



Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 20 '05

re: for loop


"Christina Grafström" <ch.grafstrom@telia.com> writes:
[color=blue]
> I need help to make this a loop.
>
> dsum1 = document.form1.c1.value;
> dsum1 -=0;[/color]
....[color=blue]
> dsum6 = document.form1.c6.value;
> dsum6 -=0;
>
> saldo = dsum1 + dsum2 + dsum3 + dsum4 + dsum5 + dsum6;[/color]

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
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Da Costa Gomez
Guest
 
Posts: n/a
#3: Jul 20 '05

re: for loop


Lasse Reichstein Nielsen wrote:[color=blue]
> "Christina Grafström" <ch.grafstrom@telia.com> writes:
>
>[color=green]
>>I need help to make this a loop.
>>
>>dsum1 = document.form1.c1.value;
>>dsum1 -=0;[/color]
>
> ...
>[color=green]
>>dsum6 = document.form1.c6.value;
>>dsum6 -=0;
>>
>>saldo = dsum1 + dsum2 + dsum3 + dsum4 + dsum5 + dsum6;[/color]
>
>
> 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);
> }[/color]
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[color=blue]
>
> /L[/color]

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#4: Jul 20 '05

re: for loop


Da Costa Gomez <dcg@xs4all.nl> writes:
[color=blue]
> why the + in front of the (form...)?[/color]

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
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Dr John Stockton
Guest
 
Posts: n/a
#5: Jul 20 '05

re: for loop


JRS: In article <vfs3i6fw.fsf@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lrn@hotpop.com>
posted at Mon, 8 Sep 2003 18:35:47 :-[color=blue]
>Da Costa Gomez <dcg@xs4all.nl> writes:
>[color=green]
>> why the + in front of the (form...)?[/color]
>
>The prefix plus sign is the shortest and fastest way of turning a
>string into a number. It is equivalent in function to parseFloat.[/color]

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

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#6: Jul 20 '05

re: for loop


Dr John Stockton <spam@merlyn.demon.co.uk> writes:
[color=blue]
> Provided that the string contains only the number and optional
> surrounding whitespace.[/color]

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
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread


Similar JavaScript / Ajax / DHTML bytes