473,786 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jun 27 '08 #1
10 1236
Geoff Cox <gc**@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/
Jun 27 '08 #2
On Sat, 19 Apr 2008 23:05:10 +0200, Joost Diepenmaat <jo***@zeekat.n l>
wrote:
>Geoff Cox <gc**@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
Jun 27 '08 #3
VK
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.
Jun 27 '08 #4
Geoff Cox <gc**@freeuk.no tcomwrites:
On Sat, 19 Apr 2008 23:05:10 +0200, Joost Diepenmaat <jo***@zeekat.n l>
wrote:
>>Geoff Cox <gc**@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/
Jun 27 '08 #5
On Sun, 20 Apr 2008 00:08:55 +0200, Joost Diepenmaat <jo***@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 ..
Jun 27 '08 #6
On Sat, 19 Apr 2008 15:00:31 -0700 (PDT), VK <sc**********@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
Jun 27 '08 #7
Geoff Cox <gc**@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/
Jun 27 '08 #8
On Sun, 20 Apr 2008 01:46:35 +0200, Joost Diepenmaat <jo***@zeekat.n l>
wrote:
>Geoff Cox <gc**@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
Jun 27 '08 #9
On Sun, 20 Apr 2008 01:46:35 +0200, Joost Diepenmaat <jo***@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>
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1980
by: Infiniti | last post by:
Lets say I have an xml file as such <page> <chapter> <paragraph/> <paragraph/> <paragraph/> <paragraph/> <paragraph/> </chapter>
18
3714
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 Basch
22
26745
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 it to use memset and I get totally different results.. How can this be? Here is the example: float gaborfilter;
3
3645
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 query, it makes 3508 rows. I also know that there are 6288 unique rows in the results. Is there some configuration choice that is causing the snapshot version to return the wrong row count? Thanks for your help on this one. If it wasn't that the...
16
1089
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 stuff as the code seems to suggest, or does it find some way to produce an iterator without the need to make a copy (if stuff is a built-in
6
1943
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 implementations gives two different results: 1) In the first implementation I compute the entire equation in memory 2) In the second implementation I store intermediate to variables, and then load them to compute the final result.
87
3755
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, b =3 lets say a sum operation is to be performed, then: output: 5
0
1103
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 context: http://www.nabble.com/f2py-Error---module-crashes-after-several-iterations...-tp20128015p20131768.html
5
2544
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 files and perform a simple set operation on the word lists. The laptop is a Macbook Pro (2 1/2 years old) running OS X 10.5.5 with Python 2.5.1 The desktop is an iMac (brand new) running OS X 10.5.5 also with Python 2.5.1 I have tried running the...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.