473,789 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MyArray[3][-2] fails (quelle surprise), how to detect this?

function getImage(id,x,y ) {
if (mpdef[0 + yy + y][0 + xx + x] == undefined) {
// set to default image
document.getEle mentById(id).sr c = terrdef[13];
}
else {
document.getEle mentById(id).sr c = terrdef[ mpdef[0 + yy + y][0 + xx
+ x] ];
}
return;
}

I use this function to grab an index number which refers to a graphics
file. In theory, firt it checks to see if the mpdef array has a valid
value as teh specified row/colum index, and then either puts in a
default index number or grabs the index number from teh array, depending
on whether the mpdef array has a value at that location.

However, it seems to choke when either teh referenced row or column
is -1 or lower. How should I catch this?
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #1
14 1883
"Fabian" wrote on 12/11/2003:
function getImage(id,x,y ) {
if (mpdef[0 + yy + y][0 + xx + x] == undefined) {
// set to default image
document.getEle mentById(id).sr c = terrdef[13];
}
else {
document.getEle mentById(id).sr c = terrdef[ mpdef[0 + yy + y][0 + xx + x] ];
}
This return statement shouldn't be necessary. Correct me if I'm
wrong, but the closing brace should create an implicit return.
return;
}

I use this function to grab an index number which refers to a graphics file. In theory, firt it checks to see if the mpdef array has a valid value as teh specified row/colum index, and then either puts in a
default index number or grabs the index number from teh array, depending on whether the mpdef array has a value at that location.

However, it seems to choke when either teh referenced row or column
is -1 or lower. How should I catch this?


Umm, is there any reason why you can't just check if the array indices
are greater than, or equal to, zero. If either are negative, display
the default. If they are zero or positive, then check if the array
element contains a value. If it doesn't display the default. If it
does contain a value, display that:

if( 0 > x || 0 > y )
{
document.getEle mentById(id).sr c = terrdef[13];
}
else if( undefined == mpdef[0 + yy + y][0 + xx + x])
{
document.getEle mentById(id).sr c = terrdef[13];
}
else
{
document.getEle mentById(id).sr c = terrdef[ mpdef[0 + yy + y][0 + xx
+ x] ];
}

If right-to-left evaluation is guaranteed, you can reduce the above
to:

if(( undefined == mpdef[0 + yy + y][0 + xx + x] ) || ( 0 > x || 0 >
y ))
{
document.getEle mentById(id).sr c = terrdef[13];
}
else
{
document.getEle mentById(id).sr c = terrdef[ mpdef[0 + yy + y][0 + xx
+ x] ];
}

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.u k (remove [no-spam] to reply)
Jul 20 '05 #2
"Fabian" <la****@hotmail .com> writes:
function getImage(id,x,y ) {
if (mpdef[0 + yy + y][0 + xx + x] == undefined) {
// set to default image
document.getEle mentById(id).sr c = terrdef[13];
}
else {
document.getEle mentById(id).sr c = terrdef[ mpdef[0 + yy + y][0 + xx
+ x] ];
}
return;
} However, it seems to choke when either teh referenced row or column
is -1 or lower. How should I catch this?
Actually, it only chokes when the *first* coordinate is negative (or
any other value that doesn't correpspond to a property of the mpdef
array.

Try this:

function getImage(id,x,y ) {
var elem = document.getEle mentById(id);
var mpdefColumn = mpdef[0+yy+y]; // I assume "y" means "column"
if (mpdefColumn) {
var mpdefCell = mpdefColumn[0+xx+x];
if (mpdefCell !== undefined ) {
elem.src = terrdef[mpdefCell];
return;
}
}
elem.src = terrdef[13];
}

if (mpdef[0 + yy + y][0 + xx + x] == undefined) {


if mpdef[0+yy+y] is undefined, the second lookup fails.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lasse Reichstein Nielsen hu kiteb:
Try this:

function getImage(id,x,y ) {
var elem = document.getEle mentById(id);
var mpdefColumn = mpdef[0+yy+y]; // I assume "y" means "column"
if (mpdefColumn) {
var mpdefCell = mpdefColumn[0+xx+x];
if (mpdefCell !== undefined ) {
elem.src = terrdef[mpdefCell];
return;
}
}
elem.src = terrdef[13];
}


function getImage(id,x1, y1) {
// shiftys to account for all compass direction facings
if ((ff==0) || (ff==1)) { x2 = x1; y2 = y1; }
if ((ff==2) || (ff==3)) { x2 = -y1; y2 = x1; }
if ((ff==4) || (ff==5)) { x2 = -x1; y2 = -y1; }
if ((ff==6) || (ff==7)) { x2 = y1; y2 = -x1; }
// detect undefined map locations
if ((0 + yy + y2 < 0) || (0 + xx + x2 < 0) || (1 + yy + y2 >
mpdef.length) || (1 + xx + x2 > mpdef[0].length)) {
document.getEle mentById(id).sr c = terrdef[ nullterr ];
}
else {
document.getEle mentById(id).sr c = terrdef[ mpdef[0 + yy + y2][0 + xx +
x2] ];
}
}

The above is what I am using now, and it seems to work. In case you're
interested, the above is being used at [ www.lajzar.co.uk/midnight ].
(arrow keys to navigate, or use the buttons in the corner).
Two points to question...

1) I'm sure having 4 parallel if statements is not particularly
efficient, but I'm not sure of a better solution that checks all four
map edges properly.

2) I think there is probably a more efficient way to account for the 8
possible facings.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #4
JRS: In article <bo************ *@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Fabian <la****@hotmail .com> posted at Thu, 13
Nov 2003 03:57:09 :-
function getImage(id,x1, y1) {
// shiftys to account for all compass direction facings
if ((ff==0) || (ff==1)) { x2 = x1; y2 = y1; }
if ((ff==2) || (ff==3)) { x2 = -y1; y2 = x1; }
if ((ff==4) || (ff==5)) { x2 = -x1; y2 = -y1; }
if ((ff==6) || (ff==7)) { x2 = y1; y2 = -x1; }
// detect undefined map locations
if ((0 + yy + y2 < 0) || (0 + xx + x2 < 0) || (1 + yy + y2 >
mpdef.length ) || (1 + xx + x2 > mpdef[0].length)) {
document.getEle mentById(id).sr c = terrdef[ nullterr ];
}
else {
document.getEle mentById(id).sr c = terrdef[ mpdef[0 + yy + y2][0 + xx +
x2] ];
}
}

The above is what I am using now, and it seems to work. In case you're
interested, the above is being used at [ www.lajzar.co.uk/midnight ].
(arrow keys to navigate, or use the buttons in the corner).
Two points to question...

1) I'm sure having 4 parallel if statements is not particularly
efficient, but I'm not sure of a better solution that checks all four
map edges properly.

2) I think there is probably a more efficient way to account for the 8
possible facings.


if ((ff==0) || (ff==1)) // etc
or
f2 = (ff-ff%2)/2
if (f2=1) ...
...

You seem to be rotating by 0..3 times 90 deg. Consider the following
test, for ff = 0..7 :
ff = 6
x2 = 4 ; y2 = 3
for (j=ff; j>1; j-=2) { T = x2 ; x2 = -y2 ; y2 = T } // rot 90
Math.atan2(y2, x2) // for convenience in my test

You could also try :
if (ff>3) { ff -= 4 ; x2 = -x2 ; y2 = -y2 }
if (ff>1) { ff -= 2 ; x2 = -y2 ; y2 = x2 }
if (ff>0) { // rotate 45 degrees?
Untested :
A = mpdef[0 + yy + y2]
if (A) A = A[0 + xx + x2]
document.getEle mentById(id).sr c = A ? A : terrdef[ nullterr ];

Why add 0 ?

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5
Dr John Stockton hu kiteb:
2) I think there is probably a more efficient way to account for the
8 possible facings.
if ((ff==0) || (ff==1)) // etc
or
f2 = (ff-ff%2)/2
if (f2=1) ...
...

You seem to be rotating by 0..3 times 90 deg. Consider the following
test, for ff = 0..7 :
ff = 6
x2 = 4 ; y2 = 3
for (j=ff; j>1; j-=2) { T = x2 ; x2 = -y2 ; y2 = T } // rot 90
Math.atan2(y2, x2) // for convenience in my test


This one works. I just had to add x2 = x1 and y2 = y1 before the for
statement. Its faster than my original too. Thanks.
You could also try :
if (ff>3) { ff -= 4 ; x2 = -x2 ; y2 = -y2 }
if (ff>1) { ff -= 2 ; x2 = -y2 ; y2 = x2 }
if (ff>0) { // rotate 45 degrees?
The 45 degree rotation uses a separate set of image placeholders. I use
display=none styles to hide them when looking along a straight compass
direction, and conversely I hide the normal images when looking along a
diagonal compass direction.
Untested :
A = mpdef[0 + yy + y2]
if (A) A = A[0 + xx + x2]
document.getEle mentById(id).sr c = A ? A : terrdef[ nullterr ];

Why add 0 ?


Adding zero forces javascript to treat the variables as numbers instead
of text, thus forcing addition instead of concatenation. What does that
? operator do? It isn't in my (very old) reference book, and a question
mark is, somewhat understandably, hard to search for on the Internet.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #6
JRS: In article <bp************ *@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Fabian <la****@hotmail .com> posted at Fri, 14
Nov 2003 09:30:48 :-
Dr John Stockton hu kiteb:

Untested :
A = mpdef[0 + yy + y2]
if (A) A = A[0 + xx + x2]
document.getEle mentById(id).sr c = A ? A : terrdef[ nullterr ];

Why add 0 ?


Adding zero forces javascript to treat the variables as numbers instead
of text, thus forcing addition instead of concatenation. What does that
? operator do? It isn't in my (very old) reference book, and a question
mark is, somewhat understandably, hard to search for on the Internet.


Better to convert them to numbers when first generated. Operating with
numbers ought to be faster than operating with strings; or, at least, no
slower.
X ? Y : Z means if X then Y else Z

Here, ? is an operator with lower priority than anything apart from
assignment or comma

So A = A<0 ? -A : A // much as Math.abs(A)
isNaN(X) ? alert('Ooo') : alert('Aaa')
alert(isNaN(X) ? 'Ooo' : 'Aaa')

Seek "conditiona l operator" in Netscape's Javascript references, via
<URL:http://www.merlyn.demo n.co.uk/js-index.htm#WL>, if the links still
work. They don't actually say that only one of Y, Z is evaluated.

A good place to look is ECMA-262; I guess the links in the FAQ will lead
towards a copy of it, and the link on my site should. Section 11.12.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #7
> >> Untested :
A = mpdef[0 + yy + y2]
if (A) A = A[0 + xx + x2]
document.getEle mentById(id).sr c = A ? A : terrdef[ nullterr ];

Why add 0 ?
Adding zero forces javascript to treat the variables as numbers instead
of text, thus forcing addition instead of concatenation.


Wrong. Adding zero does not do that. It will only append a '0' at the head of
the string. For most purposes it is better to use the '+' prefix operator.

A = mpdef[+yy + (+y2)];
Better to convert them to numbers when first generated. Operating with
numbers ought to be faster than operating with strings; or, at least, no
slower.


For most scripting applications, the relative speed of number operations vs.
string operations is irrelevant. A vastly more important consideration is
correctness. If you are doing additions, made very sure that the values are
numbers. If either value is a string, the result will be wrong.

So it is best to write

A = mpdef[yy + y2];

and make sure that yy and y2 are correctly initialized as numbers. If JavaScript
had used separate operators for addition and concatenation this would not be a
problem. Unfortunately, the combination of operator overloading with type
coercion and loose typing makes the summing of two values unnecessarily
difficult and error prone.

http://www.crockford.com

Jul 20 '05 #8
Dr John Stockton hu kiteb:
Why add 0 ?
Better to convert them to numbers when first generated. Operating
with numbers ought to be faster than operating with strings; or, at
least, no slower.
Aren't there some situations where a number can still get interpreted as
a string? I wasn't aware that there is a way to explicitly declare that
a variable is only ever to be considered as a number.
X ? Y : Z means if X then Y else Z


So this is exactly equivalent to:

if (X) { Y }
else { Z }

?
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #9
"Fabian" <la****@hotmail .com> writes:
Aren't there some situations where a number can still get interpreted as
a string?
Lots. Anything that expects a string only will probably convert its
argument to a string if it isnt.

Addition to a string is one example. ""+0 and 0+"" both give the
string "0".
I wasn't aware that there is a way to explicitly declare that
a variable is only ever to be considered as a number.


Not in Javascript versions below 2, and JS2 is still work in progress.
X ? Y : Z means if X then Y else Z


So this is exactly equivalent to:

if (X) { Y }
else { Z }


No. The if statement is a statement, while X?Y:Z is an expression.

You can write
var x = 42 + (y==2?10:37);
You can't write
42 + if (...)...
since "if" generates a statement, and statments aren't expressions.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

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

Similar topics

34
4239
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
3
3845
by: Andrew Phillipo | last post by:
In IE5 I am working with a lot of code that uses Array.push() as well as for(var i in myArray) {} Great - so I'm fixing the Array.prototype.push function for browsers without it. That much works great. However as soon as I start
19
2633
by: David | last post by:
Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order. For example: myArray = "file1"; myArray = "file2"; myArray = "file3"; myArray = "file4"; myArray = "file5";
5
32297
by: Arjen | last post by:
Hello, I have a this: foreach ( string s in myArray ) { if ( s == ""test") { } }
1
15882
by: Brian Conklin | last post by:
Hello Eneryone, I am having a problem. I have written a little app that will take a text "pipe" delimited file and place all of the values in to an Excel spreadsheet. It works great on any of my XP Pro machines. When I install the app on a Win2K Pro machine, I get the following error message: ************** Exception Text ************** System.Runtime.InteropServices.COMException (0x80020008): Bad variable type. at...
0
9663
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
9511
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
10404
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...
1
10136
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
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
6765
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
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.