for loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christina Grafström

    for loop

    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

    #2
    Re: for loop

    "Christina Grafström" <ch.grafstrom@t elia.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.'

    Comment

    • Da Costa Gomez

      #3
      Re: for loop

      Lasse Reichstein Nielsen wrote:[color=blue]
      > "Christina Grafström" <ch.grafstrom@t elia.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]

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: for loop

        Da Costa Gomez <dcg@xs4all.n l> 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.'

        Comment

        • Dr John Stockton

          #5
          Re: for loop

          JRS: In article <vfs3i6fw.fsf@h otpop.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.n l> 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.demo n.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)

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: for loop

            Dr John Stockton <spam@merlyn.de mon.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("127XP gLop",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.'

            Comment

            Working...