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

2 iterations - different results - why?

P: n/a
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.length;++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

Jun 27 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Geoff Cox <gc**@freeuk.notcomwrites:
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.length;++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/
Jun 27 '08 #2

P: n/a
On Sat, 19 Apr 2008 23:05:10 +0200, Joost Diepenmaat <jo***@zeekat.nl>
wrote:
>Geoff Cox <gc**@freeuk.notcomwrites:
>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.length;++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
Jun 27 '08 #3

P: n/a
VK
On Apr 20, 1:02 am, Geoff Cox <g...@freeuk.notcomwrote:
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.length;++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.
Jun 27 '08 #4

P: n/a
Geoff Cox <gc**@freeuk.notcomwrites:
On Sat, 19 Apr 2008 23:05:10 +0200, Joost Diepenmaat <jo***@zeekat.nl>
wrote:
>>Geoff Cox <gc**@freeuk.notcomwrites:
>>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.length;++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.length;++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.length;++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/
Jun 27 '08 #5

P: n/a
On Sun, 20 Apr 2008 00:08:55 +0200, Joost Diepenmaat <jo***@zeekat.nl>
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.length;++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.play('mySound'+test_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.substring(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.length;J++){
// F["B"+Next[J]].disabled = false;
// }
// DoSpecificTask[State](F);

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

plus 3 other similar sets ..
Jun 27 '08 #6

P: n/a
On Sat, 19 Apr 2008 15:00:31 -0700 (PDT), VK <sc**********@yahoo.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
Jun 27 '08 #7

P: n/a
Geoff Cox <gc**@freeuk.notcomwrites:
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/
Jun 27 '08 #8

P: n/a
On Sun, 20 Apr 2008 01:46:35 +0200, Joost Diepenmaat <jo***@zeekat.nl>
wrote:
>Geoff Cox <gc**@freeuk.notcomwrites:
>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
Jun 27 '08 #9

P: n/a
On Sun, 20 Apr 2008 01:46:35 +0200, Joost Diepenmaat <jo***@zeekat.nl>
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.play('mySound'+test_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.substring(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>
Jun 27 '08 #10

P: n/a
On Sun, 20 Apr 2008 01:46:35 +0200, Joost Diepenmaat <jo***@zeekat.nl>
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.play('mySound'+test_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.substring(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);
}

}
Jun 27 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.