for loop question

Discussion in 'Javascript' started by cp, Jan 22, 2004.

  1. cp

    cp Guest

    in Perl I would write a for loop as :

    for $i ( 1..2 ) {
    print "<p>$i</p>";
    }

    which would output:
    <p>1</p>
    <p>2</p>

    In JavaScript I would write:

    for (var i=1;i<3;i++) {
    ...

    or is there an easier (or more idiomatic) way?

    --
    cp
     
    cp, Jan 22, 2004
    #1
    1. Advertising

  2. cp <> writes:

    > in Perl I would write a for loop as :
    >
    > for $i ( 1..2 ) {
    > print "<p>$i</p>";
    > }
    >

    ....
    > In JavaScript I would write:
    >
    > for (var i=1;i<3;i++) {
    > ...
    >
    > or is there an easier (or more idiomatic) way?


    There isn't.

    There is another use of "for" in Javascript, that is closer to the
    Perl version:
    for (var x in obj) { ... }
    It iterates through the properties of an object. Notice that it
    is different from Perl in that the variable "x" is bound to the
    *name* of the properties, not the value.

    So, in Perl

    my @arr = (1,4,7,9);
    for $i (@arr) {
    print "$i - ";
    }

    would print "1 - 4 - 7 - 9 - ", while in Javascript

    var arr = [1,4,7,9];
    for (var i in arr) {
    document.write(i+" - ");
    }

    would write "0 - 1 - 2 - 3 - ". To get the same effect as in Perl, use

    var arr = [1,4,7,9];
    for (var i in arr) {
    document.write(arr+" - ");
    }

    (I bit me a lot when I started doing Javascript).
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jan 22, 2004
    #2
    1. Advertising

  3. > for $i ( 1..2 ) {
    > print "<p>$i</p>";
    > }
    >
    > which would output:
    > <p>1</p>
    > <p>2</p>
    >
    > In JavaScript I would write:
    >
    > for (var i=1;i<3;i++) {
    > ...
    >
    > or is there an easier (or more idiomatic) way?


    There should be, but there isn't. I don't think that the C control structures
    are suitable for a scripting language, but that's all you get in JavaScript.

    http://www.crockford.com/javascript/survey.html
     
    Douglas Crockford, Jan 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,395
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,258
    Tim Slattery
    Mar 30, 2005
  3. takayuki
    Replies:
    2
    Views:
    284
    Calvin Spealman
    Jun 16, 2008
  4. takayuki
    Replies:
    17
    Views:
    476
    John Salerno
    Jun 17, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    398
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page