472,981 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

Associative array problem (again)?

Suppose I have the following code. It functions to randomly select a
city based upon the probabilities given by the key differences in the
associative array. [This is just an illustrative example]. Eg.
because the key difference between London and the previous element is
25 (40-15), London has a 25% chance of being selected.

When I call the function getAssocItem to do this I need to send in 2
arguments.

Is there a quick way to get the maximum key value in the associative
array?
PS: These array key values will always be sorted in order but may not
end with the value of 100.

cities = {15:'Berlin', 40:'London', 60:'Madrid', 80:'Paris',
100:'Rome'}

function getAssocItem(ary, maxkey) {
var z = d(maxkey);
var key;
for (key in ary)
if (key>=z)
break;
return ary[key]
}

function d(X){
return Math.floor( (X * Math.random() + 1) )
}

Jul 23 '05 #1
6 1960
mark4asp <ma****************@ntlworld.com> wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You really want to refrain from falsifying headers,
see for example section 2, paragraph iv. of
<http://www.home.ntl.com/page/userpolicy&source=ntlworld>

You have been warned.
Suppose I have the following code. It functions to randomly select a
city based upon the probabilities given by the key differences in the
associative array. [This is just an illustrative example].
Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation). What you are accessing are Object
objects, and what you are reading and writing are properties of objects.
[...]
Is there a quick way to get the maximum key value
It is the property value, not a key value.
in the associative array?


No.
PointedEars
--
The glass is two times too big
Jul 23 '05 #2
Lee
Thomas 'PointedEars' Lahn said:

mark4asp <ma****************@ntlworld.com> wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You really want to refrain from falsifying headers,
see for example section 2, paragraph iv. of
<http://www.home.ntl.com/page/userpolicy&source=ntlworld>

You have been warned.


I'll bet you don't realize how silly that sounds.

Suppose I have the following code. It functions to randomly select a
city based upon the probabilities given by the key differences in the
associative array. [This is just an illustrative example].


Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation).


Take a pill. He has implemented an associative array using
property names as keys. It's not a perfect implementation,
because it may include some "keys" that the user didn't put
there, such as "length", "constructor", etc, but it is an
associative array. It would be a shame if you were to have
a stroke while trying to convince the world otherwise.

Is there a quick way to get the maximum key value


As you create each associative array, add an additional property
MAXVAL, set to the largest value.

Jul 23 '05 #3
In article <24****************@PointedEars.de>,
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation). What you are accessing are Object
objects, and what you are reading and writing are properties of objects.


Lawyers get into these arguments all the time. They have the phrase
which I remember goes something like:

If it walks like a duck,
quarks like a duck, and
looks like a duck ...

it is a duck.
So as a prior poster pointed out, a Javascript object will have a few
extra properties. This properties can be detected and ignored. When
you add the code to ignore these properties, you have an associative
array. 'cause it acts like an associative hence it must be an
associative array.

If it takes input like an associative array,
works like an associative array,
returns data like an associative array,

it is an associative array.

I think the point here is not confuse the poster by saying javascript
has no associative arrays. Say javascript has something you can use
like associative arrays.

Robert
Jul 23 '05 #4
Robert wrote:
In article <24****************@PointedEars.de>,
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:


Please do not post attribution novels.
Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation). What you are accessing are Object
objects, and what you are reading and writing are properties of objects.


[...] 'cause it acts like an associative hence it must be an
associative array.


Following your simple logic:
'cause you act and think like an ignorant, you must be an ignorant.
PointedEars
--
Despite the high cost of living, it remains very popular.
Jul 23 '05 #5
Lee
Thomas 'PointedEars' Lahn said:
Following your simple logic:
'cause you act and think like an ignorant, you must be an ignorant.


You get so upset when people violate trivial netiquette
guidelines, but have no qualms about personal attacks?

Jul 23 '05 #6
In a message that was read by Randy Webb at 1:28 PM on 6 October 2004,
in the United States of America in the state of Florida in said country
and posted by the utterly ignorantly stupid Thomas Lahn who decided that
he needed to advertise the fact he has Pointed Ears, no sense, and a
lack of training to get through puberty wrote:
Robert wrote:

In article <24****************@PointedEars.de>,
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
Please do not post attribution novels.


You call *that* an "attribution novel"? You need to have your head
examined, your brain critiqued (preferably post-mortem), and learn what
a novel really is.
Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation). What you are accessing are Object
objects, and what you are reading and writing are properties of objects.


[...] 'cause it acts like an associative hence it must be an
associative array.

Following your simple logic:
'cause you act and think like an ignorant, you must be an ignorant.


No, because its coming from someone (you) who has the mentality of a 12
year old thats approaching puberty.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7

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

Similar topics

21
by: Leonardo | last post by:
I have the following associative array: $user=1; $user=1; $user=0; $user=1; $user=0; $user=1; $user=1;
13
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
14
by: Yereth Jansen | last post by:
Hi all, I encountered a problem with looping through an associative array. All worked perfectly with the following code: for (var menuItem in this.menuItems) { doSomething(); } where...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.