Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #25924 > unrolled thread

Why: X[i] = x[i] = undefined when x[i] defined?

Started byEd Jay <edMbj@aes-intl.com>
First post2014-10-17 01:50 -0700
Last post2014-10-19 22:54 +0100
Articles 12 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 01:50 -0700
    Re: Why: X[i] = x[i] = undefined when x[i] defined? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-17 16:10 +0000
      Re: Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 09:29 -0700
        Re: Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 10:52 -0700
          Re: Why: X[i] = x[i] = undefined when x[i] defined? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-17 21:29 +0200
            Re: Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 12:32 -0700
              Re: Why: X[i] = x[i] = undefined when x[i] defined? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-17 21:51 +0200
                Re: Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 13:21 -0700
                Re: Why: X[i] = x[i] = undefined when x[i] defined? Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-10-18 21:38 +0100
            Re: Why: X[i] = x[i] = undefined when x[i] defined? John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-18 10:36 +0100
        Re: Why: X[i] = x[i] = undefined when x[i] defined? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-18 22:17 +0000
          Re: Why: X[i] = x[i] = undefined when x[i] defined? Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-10-19 22:54 +0100

#25924 — Why: X[i] = x[i] = undefined when x[i] defined?

FromEd Jay <edMbj@aes-intl.com>
Date2014-10-17 01:50 -0700
SubjectWhy: X[i] = x[i] = undefined when x[i] defined?
Message-ID<fel14a5kmcg35jimb7hq4afq4tcnvfhohv@4ax.com>
x_pos_array = [];
x_pos_out = [];

Given that x_pos_array is not empty, e.g.,  x_pos_array[0] > 0

When
x_pos_out[i] = x_pos_array[i];
x_pos_out[i]) is undefined 

But
x_pos_out(no indexing) = x_pos_array[i];  
x_pos_out is defined as x_pos_array[i]

Why?
-- 
Ed Jay (remove 'M' to respond by email)

[toc] | [next] | [standalone]


#25926

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-10-17 16:10 +0000
Message-ID<m1rf17$2te$2@dont-email.me>
In reply to#25924
On Fri, 17 Oct 2014 01:50:50 -0700, Ed Jay wrote:

> x_pos_array = [];
> x_pos_out = [];
> 
> Given that x_pos_array is not empty, e.g.,  x_pos_array[0] > 0
> 
> When x_pos_out[i] = x_pos_array[i];
> x_pos_out[i]) is undefined
> 
> But x_pos_out(no indexing) = x_pos_array[i];
> x_pos_out is defined as x_pos_array[i]
> 
> Why?

Sorry, but I'm having trouble following this.

eg:

var i, x1, x2;
x1 = [];
x2 = [];
x1.push( 1 );
for ( i = 0; i < x1.length; i++ ) x2[i] = x1[i];
console.log( "x1[0] = " + x1[0] +"; x2[0] = " + x2[0] );

Output is: "x1[0] = 1; x2[0] = 1"

So I can't really see what the problem is - can you offer code that 
demonstrates it?

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#25927

FromEd Jay <edMbj@aes-intl.com>
Date2014-10-17 09:29 -0700
Message-ID<95g24apb19udm64v45cehj91b6nj7uo3jb@4ax.com>
In reply to#25926
On Fri, 17 Oct 2014 16:10:15 +0000 (UTC), Denis McMahon
<denismfmcmahon@gmail.com> wrote:

>On Fri, 17 Oct 2014 01:50:50 -0700, Ed Jay wrote:
>
>> x_pos_array = [];
>> x_pos_out = [];
>> 
>> Given that x_pos_array is not empty, e.g.,  x_pos_array[0] > 0
>> 
>> When x_pos_out[i] = x_pos_array[i];
>> x_pos_out[i]) is undefined
>> 
>> But x_pos_out(no indexing) = x_pos_array[i];
>> x_pos_out is defined as x_pos_array[i]
>> 
>> Why?
>
>Sorry, but I'm having trouble following this.
>
>eg:
>
>var i, x1, x2;
>x1 = [];
>x2 = [];
>x1.push( 1 );
>for ( i = 0; i < x1.length; i++ ) x2[i] = x1[i];
>console.log( "x1[0] = " + x1[0] +"; x2[0] = " + x2[0] );
>
>Output is: "x1[0] = 1; x2[0] = 1"
>
>So I can't really see what the problem is - can you offer code that 
>demonstrates it?

Thanks for the response. I can't see it either. :-)

I create a div for hit zones over a canvas if some conditions are met.
Code - function1:

x_pos_array = [];
y_pos_array = []
function make_div(e) { //mouse event
var marker_cnt = x_pos_array.length;
var x_pos = e.x;
var y_pos = e.y;
if(some condition is met) {
var div = document.createElement('div');
image_holder.appendChild(div);	
div.id = 'mrkr'+marker_cnt;
//style the div and add onmouseover listener
x_pos_array.push(x_pos);
y_pos_array.push(y_pos);
}}

Code function 2
x_pos_out = [];
y_pos_out = [];
function() {
var marker_cnt = x_pos_array.length;
for (var i = 0; i < marker_cnt; i++) {
if (x_pos_array[i] > 0) {
x_pos_out[i] = x_pos_array[i];
y_pos_out[i] = y_pos_array[i];
alert(i + ")  x_pos_out[i] = " + x_pos_out[i]);	
//undefined for x_pos_array[i] > 0
//do something with x_pos_out and y_pos_out
}
}
-- 
Ed Jay (remove 'M' to respond by email)

[toc] | [prev] | [next] | [standalone]


#25928

FromEd Jay <edMbj@aes-intl.com>
Date2014-10-17 10:52 -0700
Message-ID<0ol24alq2cr6m3hosessv298crkhsnh7ck@4ax.com>
In reply to#25927
On Fri, 17 Oct 2014 09:29:54 -0700, Ed Jay <edMbj@aes-intl.com> wrote:

>On Fri, 17 Oct 2014 16:10:15 +0000 (UTC), Denis McMahon
><denismfmcmahon@gmail.com> wrote:
>
>>On Fri, 17 Oct 2014 01:50:50 -0700, Ed Jay wrote:
>>
>>> x_pos_array = [];
>>> x_pos_out = [];
>>> 
>>> Given that x_pos_array is not empty, e.g.,  x_pos_array[0] > 0
>>> 
>>> When x_pos_out[i] = x_pos_array[i];
>>> x_pos_out[i]) is undefined
>>> 
>>> But x_pos_out(no indexing) = x_pos_array[i];
>>> x_pos_out is defined as x_pos_array[i]
>>> 
>>> Why?
>>
>>Sorry, but I'm having trouble following this.
>>
>>eg:
>>
>>var i, x1, x2;
>>x1 = [];
>>x2 = [];
>>x1.push( 1 );
>>for ( i = 0; i < x1.length; i++ ) x2[i] = x1[i];
>>console.log( "x1[0] = " + x1[0] +"; x2[0] = " + x2[0] );
>>
>>Output is: "x1[0] = 1; x2[0] = 1"
>>
>>So I can't really see what the problem is - can you offer code that 
>>demonstrates it?
>
>Thanks for the response. I can't see it either. :-)
>
>I create a div for hit zones over a canvas if some conditions are met.
>Code - function1:
>
>x_pos_array = [];
>y_pos_array = []
>function make_div(e) { //mouse event
>var marker_cnt = x_pos_array.length;
>var x_pos = e.x;
>var y_pos = e.y;
>if(some condition is met) {
>var div = document.createElement('div');
>image_holder.appendChild(div);	
>div.id = 'mrkr'+marker_cnt;
>//style the div and add onmouseover listener
>x_pos_array.push(x_pos);
>y_pos_array.push(y_pos);
>}}
>
>Code function 2
>x_pos_out = [];
>y_pos_out = [];
>function() {
>var marker_cnt = x_pos_array.length;
>for (var i = 0; i < marker_cnt; i++) {
>if (x_pos_array[i] > 0) {
>x_pos_out[i] = x_pos_array[i];
>y_pos_out[i] = y_pos_array[i];
>alert(i + ")  x_pos_out[i] = " + x_pos_out[i]);	
>//undefined for x_pos_array[i] > 0
>
>}
>}
Solved. 
//do something with x_pos_out and y_pos_out
Neglected to declare div as an array. 

Thanks, again, Denis, for your time.
-- 
Ed Jay (remove 'M' to respond by email)

[toc] | [prev] | [next] | [standalone]


#25929

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-10-17 21:29 +0200
Message-ID<2394444.guKU09TTg3@PointedEars.de>
In reply to#25928
Ed Jay wrote:

> On Fri, 17 Oct 2014 09:29:54 -0700, Ed Jay <edMbj@aes-intl.com> wrote:
>>Code - function1:
>>
>>x_pos_array = [];
>>y_pos_array = []
>>function make_div(e) { //mouse event
>>var marker_cnt = x_pos_array.length;
>>var x_pos = e.x;
>>var y_pos = e.y;
>>if(some condition is met) {
>>var div = document.createElement('div');
>>image_holder.appendChild(div);
>>div.id = 'mrkr'+marker_cnt;
>>//style the div and add onmouseover listener
>>x_pos_array.push(x_pos);
>>y_pos_array.push(y_pos);
>>}}
>>
>>Code function 2
>>x_pos_out = [];
>>y_pos_out = [];
>>function() {
>>var marker_cnt = x_pos_array.length;
>>for (var i = 0; i < marker_cnt; i++) {
>>if (x_pos_array[i] > 0) {
>>x_pos_out[i] = x_pos_array[i];
>>y_pos_out[i] = y_pos_array[i];
>>alert(i + ")  x_pos_out[i] = " + x_pos_out[i]);
>>//undefined for x_pos_array[i] > 0
>>
>>}
>>}
> Solved.
> //do something with x_pos_out and y_pos_out
> Neglected to declare div as an array.

The relevant ECMAScript implementations use dynamic type-checking.  
Therefore, you cannot declare something as an array; you can only declare a 
variable and assign to it a reference to an Array instance, after which you 
can assign any other value to it.
 
What you actually have neglected is to declare “x_pos_array” and 
“y_pos_array” variables by using the “var” keyword:

  var x_pos_array = [];
  var y_pos_array = [];

Not doing this causes your code to be more error-prone than it needs to be.  
You might be accidentally accessing a property of the global object, and 
your code will maybe compile, but certainly not run in ES5+ strict mode.


The code that you have posted is hard to read.  If you wish your code to be 
reviewed here by many, pretty printing, e.g. indentation, is advised.  It 
will also help you to see any errors in your code more easily.  See the FAQ 
for details.

Please provide a working contact e-mail address in the From header field, 
see also <http://www.interhack.net/pubs/munging-harmful/>.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#25930

FromEd Jay <edMbj@aes-intl.com>
Date2014-10-17 12:32 -0700
Message-ID<llr24al57aqodrs2585596a1ef5jd72mi9@4ax.com>
In reply to#25929
On Fri, 17 Oct 2014 21:29:01 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>Ed Jay wrote:
>
>> On Fri, 17 Oct 2014 09:29:54 -0700, Ed Jay <edMbj@aes-intl.com> wrote:
>>>Code - function1:
>>>
>>>x_pos_array = [];
>>>y_pos_array = []
>>>function make_div(e) { //mouse event
>>>var marker_cnt = x_pos_array.length;
>>>var x_pos = e.x;
>>>var y_pos = e.y;
>>>if(some condition is met) {
>>>var div = document.createElement('div');
>>>image_holder.appendChild(div);
>>>div.id = 'mrkr'+marker_cnt;
>>>//style the div and add onmouseover listener
>>>x_pos_array.push(x_pos);
>>>y_pos_array.push(y_pos);
>>>}}
>>>
>>>Code function 2
>>>x_pos_out = [];
>>>y_pos_out = [];
>>>function() {
>>>var marker_cnt = x_pos_array.length;
>>>for (var i = 0; i < marker_cnt; i++) {
>>>if (x_pos_array[i] > 0) {
>>>x_pos_out[i] = x_pos_array[i];
>>>y_pos_out[i] = y_pos_array[i];
>>>alert(i + ")  x_pos_out[i] = " + x_pos_out[i]);
>>>//undefined for x_pos_array[i] > 0
>>>
>>>}
>>>}
>> Solved.
>> //do something with x_pos_out and y_pos_out
>> Neglected to declare div as an array.
>
>The relevant ECMAScript implementations use dynamic type-checking.  
>Therefore, you cannot declare something as an array; you can only declare a 
>variable and assign to it a reference to an Array instance, after which you 
>can assign any other value to it.
> 
>What you actually have neglected is to declare “x_pos_array” and 
>“y_pos_array” variables by using the “var” keyword:
>
>  var x_pos_array = [];
>  var y_pos_array = [];
>
>Not doing this causes your code to be more error-prone than it needs to be.  
>You might be accidentally accessing a property of the global object, and 
>your code will maybe compile, but certainly not run in ES5+ strict mode.
>
Thank you!
>
>The code that you have posted is hard to read.  If you wish your code to be 
>reviewed here by many, pretty printing, e.g. indentation, is advised.  It 
>will also help you to see any errors in your code more easily.  See the FAQ 
>for details.

OK.
>
>Please provide a working contact e-mail address in the From header field, 
>see also <http://www.interhack.net/pubs/munging-harmful/>.

Remove the M and you have it.
-- 
Ed Jay (remove 'M' to respond by email)

[toc] | [prev] | [next] | [standalone]


#25932

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-10-17 21:51 +0200
Message-ID<3496960.XX3CMxuZ7L@PointedEars.de>
In reply to#25930
Ed Jay wrote:

> […] Thomas 'PointedEars' Lahn […] wrote:
>> What you actually have neglected is to declare “x_pos_array” and
>> “y_pos_array” variables by using the “var” keyword:
>>
>>  var x_pos_array = [];
>>  var y_pos_array = [];
>>
>> Not doing this causes your code to be more error-prone than it needs to
>> be. You might be accidentally accessing a property of the global object,
>> and your code will maybe compile, but certainly not run in ES5+ strict
>> mode.
>
> Thank you!

You are welcome.  As you did not know this, what did you mean by

>>> Neglected to declare div as an array. 

?  In the code that you posted –

>>> var div = document.createElement('div');
>>> image_holder.appendChild(div); 
>>> div.id = 'mrkr'+marker_cnt;

– “div” does not refer to an Array instance; it refers to a “div” element 
object.

>> Please provide a working contact e-mail address in the From header field,
>> see also <http://www.interhack.net/pubs/munging-harmful/>.
> 
> Remove the M and you have it.

It is not socially acceptable behavior of you to expect others to fiddle 
with the “To:” field before being able to contact you via e-mail.  (Your 
signature might not even be displayed.)

Also, please trim your quotations to the relevant minimum (this is also 
recommended in the FAQ).  Bandwidth may not be as precious as in the early 
days of Usenet, but free time still is.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#25934

FromEd Jay <edMbj@aes-intl.com>
Date2014-10-17 13:21 -0700
Message-ID<p6u24alsloa7gd6mq2gvgdcjs1gnipdn62@4ax.com>
In reply to#25932
On Fri, 17 Oct 2014 21:51:40 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>..what did you mean by
>
>>>> Neglected to declare div as an array. 
>
>– “div” does not refer to an Array instance; it refers to a “div” element 
>object.

There are multiple divs, so I should have written in the plural.
>
>>> Please provide a working contact e-mail address in the From header field,
>It is not socially acceptable behavior of you to expect others to fiddle 
>with the “To:” field..

I'm not sure, but in over 20-years, I may have received one or two
emails out of participating on Usenet. OTOH, my email trash folder is
filled with spam, because I didn't cloak my address early on. I do get
your point, however.
>
>Also, please trim your quotations to the relevant minimum

Will do. 

Thank you, again.
-- 
Ed Jay (remove 'M' to respond by email)

[toc] | [prev] | [next] | [standalone]


#25938

FromDr J R Stockton <reply1400@merlyn.demon.co.uk.invalid>
Date2014-10-18 21:38 +0100
Message-ID<oYhi9hdN$sQUFwPM@invalid.uk.co.demon.merlyn.invalid>
In reply to#25932
In comp.lang.javascript message <3496960.XX3CMxuZ7L@PointedEars.de>,
Fri, 17 Oct 2014 21:51:40, Thomas 'PointedEars' Lahn
<PointedEars@web.de> posted:

>
>It is not socially acceptable behavior of you to expect others to fiddle
>with the “To:” field before being able to contact you via e-mail.  (Your
>signature might not even be displayed.)

Before pontificating on what is and what is not socially acceptable, you
should at least endeavour to become socially acceptable yourself, Tanky.

-- 
(c) John Stockton, nr London UK.  ???@merlyn.demon.co.uk  Turnpike v6.05  MIME.
 Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
     Check boilerplate spelling -- error is a public sign of incompetence.
    Never fully trust an article from a poster who gives no full real name.

[toc] | [prev] | [next] | [standalone]


#25935

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2014-10-18 10:36 +0100
Message-ID<r3d44atgahfev7dqe3e1qh2tmqv4vapauk@4ax.com>
In reply to#25929
On Fri, 17 Oct 2014 21:29:01 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

  <snip>
>Please provide a working contact e-mail address in the From header field, 
>see also <http://www.interhack.net/pubs/munging-harmful/>.

That document is 15-20 years old and doesn't mention the ReplyTo
header or botnets. At best it should be considered Historic.

I've found that putting a live but easily disposable address in the
ReplyTo header reduces spam. (None to that address in 1 year).

  John

[toc] | [prev] | [next] | [standalone]


#25937

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-10-18 22:17 +0000
Message-ID<m1uots$vl2$1@dont-email.me>
In reply to#25927
On Fri, 17 Oct 2014 09:29:54 -0700, Ed Jay wrote:

>>So I can't really see what the problem is - can you offer code that
>>demonstrates it?
> 
> Thanks for the response. I can't see it either. :-)
> 
> I create a div for hit zones over a canvas if some conditions are met.
> Code - .....

By inspection your code will not run, it seems to be missing a statement 
terminator, have an imbalance of "{" vs "}", and at least one logic test 
is not correctly enclosed in "()". Additionally some guesswork is 
required to determine where comments terminate and new lines begin.

Hence it is impossible for your code to demonstrate the problem case, 
moreover I suspect to do so it would need some minimal html wrapper to go 
with it in order to provide a dom context in which it operates.

We can not magically deduce the part(s) of the problem that you fail to 
supply us with when explaining what isn't working.

In future, when posting a problem, please post the minimum sufficient 
code (including the html document body if needed) that enables the 
problem being described to be reproduced in a browser or other javascript 
interpreter. This is a generic issue that applies to any request for help 
with programming, regardless of language, you should endeavour to post 
minimum example code that the reader can run to reproduce the actual 
problem you are trying to solve.

The problem you appear to be asking about (see the subject line) is that 
you are experiencing a case where assigning values to array s, and then 
assigning array t to be equal to array s, t[n] is undefined when s[n] is 
defined.

Nothing in the code you posted demonstrates this, at least in part 
because the code isn't executable due to the above mentioned errors.

1) Develop code (or possibly in your case an html document with embedded 
script element) that demonstrates the exact problem.

2) Cut the document down to the minimum code that shows the problem.

3) Use /* ..... */ style comments so it is clear where comments end, 
especially when posting code to usenet where line wrapping may not be 
preserved.[1]

4) Post the whole document here if fairly short (eg less than 30 lines or 
so) or if larger post a link to it on a web server.

5) Remember step 2, it really is important. The more code that is 
irrelevant to the problem that we need to trawl through and try and 
understand the more likely we are to lose interest. We are not going to 
do step 2 for you, you need to do that as part of the we fix your code 
deal.

[1] I've been using usenet and similar for 25+ years, and I still haven't 
worked out a consistent and guaranteed way to preserve line breaks in 
code postings[2] that allows for the vagaries of all the different 
clients and the way they process received text and quote it. Don't even 
bother trying!

[2] Apart from posting the source somewhere and posting a link to it in 
the message, which upsets some purists, but which is sometimes the only 
way to do it.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#25940

FromDr J R Stockton <reply1400@merlyn.demon.co.uk.invalid>
Date2014-10-19 22:54 +0100
Message-ID<y5G0FEnVMDRUFwXm@invalid.uk.co.demon.merlyn.invalid>
In reply to#25937
In comp.lang.javascript message <m1uots$vl2$1@dont-email.me>, Sat, 18
Oct 2014 22:17:32, Denis McMahon <denismfmcmahon@gmail.com> posted:

>
>[1] I've been using usenet and similar for 25+ years, and I still haven't
>worked out a consistent and guaranteed way to preserve line breaks in
>code postings[2] that allows for the vagaries of all the different
>clients and the way they process received text and quote it. Don't even
>bother trying!


I think you are missing an easy solution - put hard line breaks so that
no line exceeds 70 characters.  If anyone cannot read that properly, the
fault is theirs and not the author's.

Another tip - after composing one's News message, but before sending it,
copy'n'paste the allegedly executable code to the dumbest GUI text
editor to hand, save it, run it, and see whether it does what one
expects.

-- 
 (c) John Stockton, Surrey, UK.  ¬@merlyn.demon.co.uk   Turnpike v6.05   MIME.
   Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
 Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web