Associative array problem (again)?

M

mark4asp

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) )
}
 
T

Thomas 'PointedEars' Lahn

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
 
L

Lee

Thomas 'PointedEars' Lahn said:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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.


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

Thomas 'PointedEars' Lahn

Robert said:
Thomas 'PointedEars' Lahn <[email protected]> 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
 
L

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?
 
R

Randy Webb

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 said:
Robert 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top