473,396 Members | 1,764 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,396 software developers and data experts.

What's wrong with: var T = new Array(-1);

Hi,

if I put the line:

var T = new Array(-1);

anywhere in my script, it stops working. e.g

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
var T = new Array(-1);
document.open();
document.write('hahahah');
-->
</SCRIPT>

doesn't produce any output. If I change

var T = new Array(-1);

into

var T = new Array(1);

or

var T = new Array(-1,-1);

everything works fine again.
Same results in Mozilla 1.7.2 as in IE 6.

Is that a bug in both their javascript interpreters or can anybody
give me a good explanation of why that is?
Bas

Jul 23 '05 #1
4 1714
Bas van der Veer wrote:
Is that a bug in both their javascript interpreters or can anybody
give me a good explanation of why that is?


Yes, its very simple:

If you put more than one arguments in the brakets then it will be
interpreted as values which will be put into the array. But if you only use
one argument it will be interpreted as the dimension for the array. If you
put a
-1 there the interpreter tries to declare an array with -1 fields. That
causes the error. You could write instead:

var T = new Array()
T[0] = -1

or

var T = new Array("-1") // but here the -1 is saved as a string not as a
number; so it must be convertzed to a number again if you which to calculate
with it
Jul 23 '05 #2
Andre Herbst wrote:

Yes, its very simple:

If you put more than one arguments in the brakets then it will be
interpreted as values which will be put into the array. But if you only use
one argument it will be interpreted as the dimension for the array. If you
put a
-1 there the interpreter tries to declare an array with -1 fields. That
causes the error. You could write instead:

var T = new Array()
T[0] = -1

or

var T = new Array("-1")
or var T=[-1]
Mick

// but here the -1 is saved as a string not as a number; so it must be convertzed to a number again if you which to calculate
with it

Jul 23 '05 #3
Bas van der Veer wrote:
Hi,

if I put the line:

var T = new Array(-1);

anywhere in my script, it stops working. e.g

<SCRIPT LANGUAGE="JAVASCRIPT">
<script type="text/javascript">
<!--
Remove. Not needed.
var T = new Array(-1);
Generates the following error in IE: Array.length must be a positive
finite integer.

Firefox errors with: invalid array length

-->
Remove. Not needed.
Is that a bug in both their javascript interpreters or can anybody
give me a good explanation of why that is?


It is obvious to us that you one a one element array containing the
value -1, but it's not quite so obvious to the JavaScript
implementations in IE and Firefox. They see a constructor being passed
a single integer (albeit negative) and try to construct an Array with
length one. This makes sense, since the underlying implementation
probably has multiple constructor signatures, one of which probably
resembles:

public Array(int len)

that constructor will get called when new Array(int) is called,
regardless of whether int is positive or negative. Of course, they
could have added code to that constructor to test for negative ints
and called the constructor that takes a list of Array elements, but
presumably they had their reasons not to.

To resolve the problem you have a couple of options:

var a = new Array();
a[0] = -1; // a.push(-1);

or

a = [ -1 ];

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #4
Grant Wagner wrote:
Bas van der Veer wrote:
<!--


Remove. Not needed.

[...]
-->


Remove. Not needed.


More, the latter is syntactically wrong. `--' is the decrement operator
that has no valid (variable) operand here. That is why the obsolete
practice of commenting out the content of "script" elements required
this masked as a ECMAScript/J(ava)Script comment -- `//-->' or similar.
PointedEars
Jul 23 '05 #5

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

Similar topics

43
by: Mr.Tickle | last post by:
// Run this code and look for his obvious error by the compiler namespace FlagCheck { using System; enum SomeFlags {
1
by: COHENMARVIN | last post by:
The following sql statement is rejected by asp.net (working with sql server). It says something is wrong near "TableFlights". But I don't see anything wrong with it. Any help is appreciated: ...
4
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
5
by: Alexandre Martins | last post by:
Provider=Microsoft.Jet.OLEDB.4.0;UserId=Admin;Password=teste;Data Source=C:\Inetpub\wwwroot\inktoner\dados\db_inktoner.mdb;Persist Security Info=True I can't connect in my database ! whats...
4
by: blah | last post by:
Hello everyone, Ive been trying to get my application to "click" on a button in another application using SendMessage, Ive gotten this far but Im not sure whats wrong with this code, here is the...
1
by: '~=_Slawek_=~' | last post by:
$DOW = (jddayofweek(unixtojd(mktime(1, 1, 1, $month, $day, $year)))+6)%7; $DOW= (jddayofweek(juliantojd($month, $day, $year))+6)%7; The results are supposed to be the same, but they are not....
0
by: Jim Andersen | last post by:
I am using Microsoft.ApplicationBlocks.Data (v 2.0.0.0). I have this parameter array I pass to a stored procedure. The last one is an output parameter. So I did this: Line 1: arParms(8) = New...
10
by: joestevens232 | last post by:
Ok everything is good...I read in all my grades into each of the different arrays that they need to go in. Now Im trying to write a function that takes the grades and computes the average on a...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
5
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.