2 iterations - different results - why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Cox

    2 iterations - different results - why?

    Hello,

    Can anyone tell me why these 2 sets of code give different results?

    for (J in Next) {
    <action>
    }
    <function>


    for (J=0;J<Next.len gth;++J) {
    <same action as above>
    }
    <same function as above>

    I would like to change the second version so that it gives the same
    results as the first one.

    Any ideas please?!

    Cheers

    Geoff

  • Joost Diepenmaat

    #2
    Re: 2 iterations - different results - why?

    Geoff Cox <gcox@freeuk.no tcomwrites:
    Hello,
    >
    Can anyone tell me why these 2 sets of code give different results?
    >
    for (J in Next) {
    <action>
    }
    <function>
    >
    >
    for (J=0;J<Next.len gth;++J) {
    <same action as above>
    }
    <same function as above>
    >
    I would like to change the second version so that it gives the same
    results as the first one.
    >
    Any ideas please?!
    They're not the same at all. Use the first version.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • Geoff Cox

      #3
      Re: 2 iterations - different results - why?

      On Sat, 19 Apr 2008 23:05:10 +0200, Joost Diepenmaat <joost@zeekat.n l>
      wrote:
      >Geoff Cox <gcox@freeuk.no tcomwrites:
      >
      >Hello,
      >>
      >Can anyone tell me why these 2 sets of code give different results?
      >>
      >for (J in Next) {
      ><action>
      >}
      ><function>
      >>
      >>
      >for (J=0;J<Next.len gth;++J) {
      ><same action as above>
      >}
      ><same function as above>
      >>
      >I would like to change the second version so that it gives the same
      >results as the first one.
      >>
      >Any ideas please?!
      >
      >They're not the same at all. Use the first version.
      Joost,

      I would love to do so but it causes a conflict with the prototype.js
      library as I'm told that the for/in process can deal with properties
      of objects rather than elements in some Javscript implementations ...

      Can you tell me what the difference is/are?

      Cheers

      Geoff

      Comment

      • VK

        #4
        Re: 2 iterations - different results - why?

        On Apr 20, 1:02 am, Geoff Cox <g...@freeuk.no tcomwrote:
        Hello,
        >
        Can anyone tell me why these 2 sets of code give different results?
        >
        for (J in Next) {
        enumerates properties of Next object including but not limited by
        array elements - given that Next instanceof Array.

        var Next = [1,2,3];
        Next.foo = 'bar;

        for (var p in Next) {
        // will find four elements:
        // 1, 2, 3, 'bar'
        }

        <function>
        >
        for (J=0;J<Next.len gth;++J) {
        enumerates array elements given that Next instanceof Array

        var Next = [1,2,3];
        Next.foo = 'bar;

        for (var p in Next) {
        // will find three elements:
        // 1, 2, 3
        // 'bar' is not an array element
        // so not enumerated
        }

        I would like to change the second version so that it gives the same
        results as the first one.
        It is not possible and I just explained why.

        Comment

        • Joost Diepenmaat

          #5
          Re: 2 iterations - different results - why?

          Geoff Cox <gcox@freeuk.no tcomwrites:
          On Sat, 19 Apr 2008 23:05:10 +0200, Joost Diepenmaat <joost@zeekat.n l>
          wrote:
          >
          >>Geoff Cox <gcox@freeuk.no tcomwrites:
          >>
          >>Hello,
          >>>
          >>Can anyone tell me why these 2 sets of code give different results?
          >>>
          >>for (J in Next) {
          >><action>
          >>}
          >><function>
          >>>
          >>>
          >>for (J=0;J<Next.len gth;++J) {
          >><same action as above>
          >>}
          >><same function as above>
          >>>
          >>I would like to change the second version so that it gives the same
          >>results as the first one.
          >>>
          >>Any ideas please?!
          >>
          >>They're not the same at all. Use the first version.
          >
          Joost,
          >
          I would love to do so but it causes a conflict with the prototype.js
          library as I'm told that the for/in process can deal with properties
          of objects rather than elements in some Javscript implementations ...
          >
          Can you tell me what the difference is/are?
          for (J in Next) iterates through all enumerable properties of the Next
          object (and its prototype chain).

          for (;;;) is a much more general iteration construct; for
          (J=0;J<Next.len gth;++J) iterates from 0 to whatever Next.length
          contains, assuming Next.length is a number in the mean time.

          Typically, the two only do more or less the same thing if Next is an
          array containing no enumerable properties besides its numeric
          properties. This is *not* generally the case; even if Next is a plain
          array, Array.prototype might have been extended so that "for (J in
          Next)" iterates over more properties than "for (J=0;J<Next.len gth;++J)"

          See Ecma-262, sections 12.6.3 and 12.6.4, 15.2.4.5, and the references
          to the DontEnum attribute in the same document.

          --
          Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

          Comment

          • Geoff Cox

            #6
            Re: 2 iterations - different results - why?

            On Sun, 20 Apr 2008 00:08:55 +0200, Joost Diepenmaat <joost@zeekat.n l>
            wrote:
            >Typically, the two only do more or less the same thing if Next is an
            >array containing no enumerable properties besides its numeric
            >properties. This is *not* generally the case; even if Next is a plain
            >array, Array.prototype might have been extended so that "for (J in
            >Next)" iterates over more properties than "for (J=0;J<Next.len gth;++J)"
            >
            >See Ecma-262, sections 12.6.3 and 12.6.4, 15.2.4.5, and the references
            >to the DontEnum attribute in the same document.
            Joost,

            I am hoping that my instance might be a little more straight forward!?

            The code below works fine as is. If I use the code marked with the //
            then on the attempting the last answer I get "Next has no properties"
            error message and an undefined result.

            Remember I wish to use the "vanilla" iteration to avoid the conflict
            with the prototype.js library .. Can you see a simple change that
            could be made?

            Geoff

            var Arry = [
            /* State 0 : */ [1,2],
            /* State 1 : */ [3],
            /* State 2 : */ [3],
            /* State 3 : */ [4,5],
            /* State 4 : */ [6],
            /* State 5 : */ [6],
            /* State 6 : */ [7,8],
            /* State 7 : */ [9],
            /* State 8 : */ [9],
            /* State 9 : */ [10,11]]

            var result =new Array();
            var test_num=1;
            function A(f)
            {
            soundManager.pl ay('mySound'+te st_num ,'../assets/audio-group1/Track' +
            (+test_num + 22) + '.mp3');
            }
            function B(f)
            {
            result[test_num] = "same";
            test_num++;
            }

            function C(f)
            {
            result[test_num] = "different" ;
            test_num++;
            }

            DoSpecificTask = [A, B, C, A, B, C, A, B, C, A, B, C ]

            function Fn(Arg) {
            var F = Arg.form, State, J, Next;
            State = Arg.name.substr ing(1,3);

            for (J=0 ; J<12 ; J++) F["B"+J].disabled = true;
            Next = Arry[State];
            for (J in Next) F["B"+Next[J]].disabled = false;
            DoSpecificTask[State](F);

            // for (J=0;J<Next.len gth;J++){
            // F["B"+Next[J]].disabled = false;
            // }
            // DoSpecificTask[State](F);

            }


            <form name ="form1">
            <input style="font-family:sans-serif;
            font-size:large;back ground-color:#00ffff; width: 10em;" type="button"
            name="B0" value="Play 1" onClick="Fn(thi s)">
            <input style="font-family:sans-serif;
            font-size:large;back ground-color:#ffffcc; width: 10em;" type="button"
            name="B1" value="Same" onClick="Fn(thi s)" disabled>
            <input style="font-family:sans-serif;
            font-size:large;back ground-color:#ffffcc; width: 10em;" type="button"
            name="B2" value="Differen t" onClick="Fn(thi s)" disabled><br>

            plus 3 other similar sets ..

            Comment

            • Geoff Cox

              #7
              Re: 2 iterations - different results - why?

              On Sat, 19 Apr 2008 15:00:31 -0700 (PDT), VK <schools_ring@y ahoo.com>
              wrote:
              >I would like to change the second version so that it gives the same
              >results as the first one.
              >
              >It is not possible and I just explained why.
              I take your point but perhaps some other change is needed? Could you
              have a look at my last reply to Joost?

              Cheers

              Geoff

              Comment

              • Joost Diepenmaat

                #8
                Re: 2 iterations - different results - why?

                Geoff Cox <gcox@freeuk.no tcomwrites:
                Remember I wish to use the "vanilla" iteration to avoid the conflict
                with the prototype.js library .. Can you see a simple change that
                could be made?
                I still don't see why you can't use for(..;..;..) instead of for ( .. in
                ....)

                OTOH it's late on a saturday night here, so I'm not as focused as I can
                be.

                --
                Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                Comment

                • Geoff Cox

                  #9
                  Re: 2 iterations - different results - why?

                  On Sun, 20 Apr 2008 01:46:35 +0200, Joost Diepenmaat <joost@zeekat.n l>
                  wrote:
                  >Geoff Cox <gcox@freeuk.no tcomwrites:
                  >
                  >Remember I wish to use the "vanilla" iteration to avoid the conflict
                  >with the prototype.js library .. Can you see a simple change that
                  >could be made?
                  >
                  >I still don't see why you can't use for(..;..;..) instead of for ( .. in
                  >...)
                  >
                  >OTOH it's late on a saturday night here, so I'm not as focused as I can
                  >be.
                  OK!! Here's hoping yoou feel better today!

                  My problem is, as the people of Yorkshire say, there's none so blind
                  as them as cann't see!

                  Cheers

                  Geoff

                  Comment

                  • Geoff Cox

                    #10
                    Re: 2 iterations - different results - why?

                    On Sun, 20 Apr 2008 01:46:35 +0200, Joost Diepenmaat <joost@zeekat.n l>
                    wrote:
                    >OTOH it's late on a saturday night here, so I'm not as focused as I can
                    >be.
                    Joost,

                    I've found out how to avoid the error! I added a 10th item to the Arry
                    so that Next does not run out of options but this starts the whole
                    cycle over again as after the 4th answer we get back to the first item
                    in Arry. I need a way of stopping the cycle rather than this..?

                    Cheers

                    Geoff

                    var Arry = [
                    /* State 0 : */ [1,2],
                    /* State 1 : */ [3],
                    /* State 2 : */ [3],
                    /* State 3 : */ [4,5],
                    /* State 4 : */ [6],
                    /* State 5 : */ [6],
                    /* State 6 : */ [7,8],
                    /* State 7 : */ [9],
                    /* State 8 : */ [9],
                    /* State 9 : */ [10,11],
                    /* State 10 : */ [0]] // added this item to keep Next happy!



                    var result =new Array();
                    var test_num=1;
                    function A(f)
                    {
                    soundManager.pl ay('mySound'+te st_num ,'../assets/audio-group1/Track' +
                    (+test_num + 22) + '.mp3');
                    }
                    function B(f)
                    {
                    result[test_num] = "same";
                    test_num++;
                    }

                    function C(f)
                    {
                    result[test_num] = "different" ;
                    test_num++;
                    }

                    function D(f) {
                    alert('finished ');
                    }

                    DoSpecificTask = [A, B, C, A, B, C, A, B, C, A, B, C ]

                    function Fn(Arg) {
                    var F = Arg.form, State, J, Next;
                    State = Arg.name.substr ing(1,3);
                    for (J=0 ; J<12 ; J++) F["B"+J].disabled = true; // clear all
                    Next = Arry[State];

                    //for (J in Next) F["B"+Next[J]].disabled = false; // set
                    some
                    //DoSpecificTask[State](F);

                    for (var J = 0; J < Next.length; ++J) {
                    var item = Next[J];
                    F["B"+item].disabled = false;
                    }
                    DoSpecificTask[State](F);

                    }

                    function getResults() {
                    for (var i=1;i<5;i++) {
                    alert(result[i]);
                    }
                    }

                    </script>

                    Comment

                    • Geoff Cox

                      #11
                      Re: 2 iterations - different results - why?

                      On Sun, 20 Apr 2008 01:46:35 +0200, Joost Diepenmaat <joost@zeekat.n l>
                      wrote:

                      Joost,

                      OK now!

                      I have used the vanilla iteration and prototype.js is OK. My solution
                      is not very elegant I expect but it works ... I added 2 extra items to
                      Arry etc. The problem was not with the vanilla iteration as you
                      supsected but other parts of the code.

                      Thanks for your help.

                      Cheers

                      Geoff

                      var Arry = [
                      /* State 0 : */ [1,2],
                      /* State 1 : */ [3],
                      /* State 2 : */ [3],
                      /* State 3 : */ [4,5],
                      /* State 4 : */ [6],
                      /* State 5 : */ [6],
                      /* State 6 : */ [7,8],
                      /* State 7 : */ [9],
                      /* State 8 : */ [9],
                      /* State 9 : */ [10,11],
                      /* State 10 : */ [12],
                      /* State 11 : */ [12] ]

                      var result =new Array();
                      var test_num=1;
                      function A(f)
                      {
                      soundManager.pl ay('mySound'+te st_num ,'../assets/audio-group1/Track' +
                      (+test_num + 22) + '.mp3');
                      }
                      function B(f)
                      {
                      result[test_num] = "same";
                      test_num++;
                      }

                      function C(f)
                      {
                      result[test_num] = "different" ;
                      test_num++;
                      }

                      function D(f) {
                      alert('finished ');
                      }

                      DoSpecificTask = [A, B, C, A, B, C, A, B, C, A, B, C ]

                      function Fn(Arg) {
                      var F = Arg.form, State, J, Next;
                      State = Arg.name.substr ing(1,3);
                      for (J=0 ; J<12 ; J++) F["B"+J].disabled = true; // clear all
                      Next = Arry[State];

                      for (var J = 0; J < Next.length; ++J) {
                      F["B"+Next[J]].disabled = false;
                      }
                      if ( (State == 10) || (State == 11) ) {
                      DoSpecificTask[State](F);
                      sendGroup1Lab5( );
                      } else {
                      //alert(State);
                      DoSpecificTask[State](F);
                      }

                      }

                      Comment

                      Working...