473,803 Members | 1,992 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
14 1884
In article <8y**********@h otpop.com>, Lasse Reichstein Nielsen <lr*@hotpop.com >
writes:
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.


if (y==2){adder = 10;}else{adder = 37;}
var x = 42 + adder;
--
Randy
Jul 20 '05 #11
JRS: In article <bp************ *@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Fabian <la****@hotmail .com> posted at Sat, 15
Nov 2003 12:37:52 :-
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.

Note : the second of those sentences is a comment, not a reason. If the
meaning of a variable is fundamentally numeric, then it should be a
Number.

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.


If the algorithm is correct, one will never want to numerically add a
Number to a String, since the second value will have been converted to a
Number at the first opportunity. If a number is to be converted to a
string and concatenated, it should be obvious what the intent is. These
ate not matters of javascript language design, but of writing code so
that it can be executed and read with the same interpretation.

X ? Y : Z means if X then Y else Z


So this is exactly equivalent to:

if (X) { Y }
else { Z }


No; but I think the converse is true. The first is a superset of the
second, since it can be used where a value is required; A=X?Y:X

--
© 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 #12
Dr John Stockton <sp**@merlyn.de mon.co.uk> writes:
JRS: In article <bp************ *@ID-174912.news.uni-berlin.de>, seen in
X ? Y : Z means if X then Y else Z
So this is exactly equivalent to: if (X) { Y }
else { Z }

No; but I think the converse is true. The first is a superset of the
second, since it can be used where a value is required; A=X?Y:X


It depends on what Y and Z are. If they can stand for arbitrary
Javascript syntax, then it isn't true. You can't put statements into
the ?:-operator. If Y and Z are just variables, then they are expressions,
and can be used in both places (expressions can be used as statements,
although using an expression with no side effect is pointless).

I.e., there are four different things that can vary:
- use ?: or if-then as conditional branch
E.g., x?...:... or if(x) ... else ...
- have expressions or statements in the branches
E.g., 2+2 (expression) or return 2+2; (statement)
- use the value of the conditional branch or not
E.g., q=(conditional branch); or (conditional branch);
- have side effects in the branches or not
E.g., w=2+2/foo() or 2+2

That give 16 different cases, many of them invalid.

It is invalid to use statments in the branches of the ?: operator.
It is invalid to use the value of an if-statement.
It is meaningless to not have side-effects in an if-statment

Without side effects in branches:
As a statement (not using value):
if (x) {2+2;} else {4+4;} : valid but pointless
if (x) {return 2+2;} else {return 4+4;} : valid and meaningful
x?(2+2):(4+4) : valid but pointless
x?(return 2+2;):(return 4+4;) : invalid
As an expression (value is used):
q = if (x) {2+2;} else {4+4;} : invalid
q = if (x) {return 2+2;} else {return 4+4;} : invalid
q = x?(2+2):(4+4) : valid and meaningful
q = x?(return 2+2;):(return 4+4;) : invalid
With side effects:
As a statement:
if (x) {w=2+2;} else {foo();} : valid and meaningful
if (x) {return w=2+2;} else {return foo();} : valid and meaningful
x?(w=2+2):(foo( )) : valid but you should use if
x?(return w=2+2;):(return foo();) : invalid
As an expression (value is used):
q = if (x) {w=2+2;} else {foo();} : invalid
q = if (x) {return w=2+2;} else {return foo();} : invalid
q = x?(w=2+2):(foo( )) : valid and meaningful
q = x?(return w=2+2;):(return foo();) : invalid

Hope this makes any sense :)

/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 #13
Lasse Reichstein Nielsen wrote:
Dr John Stockton <sp**@merlyn.de mon.co.uk> writes:
JRS: In article <bp************ *@ID-174912.news.uni-berlin.de>, seen in

X ? Y : Z means if X then Y else Z

So this is exactly equivalent to:

if (X) { Y }
else { Z }
No; but I think the converse is true. The first is a superset of the
second, since it can be used where a value is required; A=X?Y:X


It depends on what Y and Z are. If they can stand for arbitrary
Javascript syntax, then it isn't true.


Right.
You can't put statements into the ?:-operator.
Wrong because it is too general. Try this:

var x = false;
y = x ? alert(x) : alert(42);

A method call is a statement and an assignment
(see below) is a statement, too.
As a statement (not using value):
if (x) {2+2;} else {4+4;} : valid but pointless
if (x) {return 2+2;} else {return 4+4;} : valid and meaningful
x?(2+2):(4+4) : valid but pointless
x?(return 2+2;):(return 4+4;) : invalid
return (x ? 2 + 2 : 4 + 4);
As an expression (value is used):
q = if (x) {2+2;} else {4+4;} : invalid
q = if (x) {return 2+2;} else {return 4+4;} : invalid
q = x?(2+2):(4+4) : valid and meaningful
q = x?(return 2+2;):(return 4+4;) : invalid
return (q = x ? 2 + 2 : 4 + 4);
With side effects:
As a statement:
if (x) {w=2+2;} else {foo();} : valid and meaningful
if (x) {return w=2+2;} else {return foo();} : valid and meaningful
x?(w=2+2):(foo( )) : valid but you should use if
x?(return w=2+2;):(return foo();) : invalid


return (x ? w = 2 + 2 : foo());
PointedEars
Jul 20 '05 #14
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
You can't put statements into the ?:-operator.


Wrong because it is too general. Try this:

var x = false;
y = x ? alert(x) : alert(42);

A method call is a statement and an assignment
(see below) is a statement, too.


A method call and an assignment are expressions. So are additions and
a single identifier. They are not *by themselves* statements.

*Any* expression [1] *followed by a semicolon* is a statement (more
precisely: an "ExpressionStat ement" in the grammar of ECMAScript -
ECMA 262, section 12.4).

[1] except function expressions and literal objects.

So:
x = 4
is an expression, not a statement (although automatic semicolon insertion
can turn it into one, if it is used in a position where the grammar requires
a statement).
x = 4;
is a statement, not an expression.
x?(return 2+2;):(return 4+4;) : invalid


return (x ? 2 + 2 : 4 + 4);


Yes, but that is not the same. If you wrote the original, invalid,
expression, then this is probably what you meant. But it is just an
example of an invalid construction, not something you would want
to write.

/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 #15

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

Similar topics

34
4245
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
3847
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
2634
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
15884
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
10542
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
10309
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...
1
10289
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
9119
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...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
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
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
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.