473,379 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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.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
10 1191
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Infiniti | last post by:
Lets say I have an xml file as such <page> <chapter> <paragraph/> <paragraph/> <paragraph/> <paragraph/> <paragraph/> </chapter>
18
by: Derek Basch | last post by:
What is the best way to count nested loop iterations? I can only figure to use an index but that seems kludgy. index = 0 for animal in zoo: for color in animal: index += 1 Thanks, Derek...
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
3
by: Bill Hutchison | last post by:
I have a query that returns different results (3508 rows for snapshot, 6288 for dynaset) and that is the only thing I change to get the different results. When I try to make a table from the...
16
by: James Stroud | last post by:
Hello all, I was staring at a segment of code that looked like this today: for something in stuff: whatever(something) and was wondering if the compiler really made a copy of the slice from...
6
by: Avi | last post by:
I need to implement the following calculation: f = (a*b) + (c*d) where a,b,c,d are given double values and f is a double variable for the result I found out that using two different...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
0
by: John [H2O] | last post by:
I can try, would you mind giving very brief instructions on how to 'run it under gdb'... thanks! I'll post results over at numpy-discussions. Robert Kern-2 wrote: -- View this message in...
5
by: Christopher Brewster | last post by:
I am running the same script on the same data on two different machines (the folder is synchronised with Dropbox). I get two different results. All the script does is count words in different...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.