By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,574 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,574 IT Pros & Developers. It's quick & easy.

Associative array problem (again)?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.