By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,769 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,769 IT Pros & Developers. It's quick & easy.

for loop

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Christina Grafström" <ch**********@telia.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
Lasse Reichstein Nielsen wrote:
"Christina Grafström" <ch**********@telia.com> writes:

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


Jul 20 '05 #3

P: n/a
Da Costa Gomez <dc*@xs4all.nl> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
JRS: In article <vf**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Mon, 8 Sep 2003 18:35:47 :-
Da Costa Gomez <dc*@xs4all.nl> writes:
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.


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)
Jul 20 '05 #5

P: n/a
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.