Closed Thread Icon

Topic awaiting preservation: Challenge your JavaScript skills Pages that link to <a href="https://ozoneasylum.com/backlink?for=26874" title="Pages that link to Topic awaiting preservation: Challenge your JavaScript skills" rel="nofollow" >Topic awaiting preservation: Challenge your JavaScript skills\

 
Author Thread
Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-21-2005 06:19

This is an attempt to spark some conversation or creativity.

Write a JavaScript function that takes an array and randomizes the order of the elements in that array. Make it as thorough and efficient as you personally can.

(If you find this problem simple, wait until others have had a try before posting your answer.)

function randomizeArray(array)
{
// ... your code here ...
}


 

_Mauro
Bipolar (III) Inmate

From:
Insane since: Jul 2005

posted posted 10-21-2005 12:39

Am out of energy for the implementation, but I suspect the first trick is actually determining... what is "order" in that array.
Eg. sorting to later "reverse sort", theoretical complexity depends on the sorting method.
Does it have to be type-independent? Should I be able to sort "any" javascript "data type"?

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-21-2005 16:02

liorean and I proposed some implementations in An elegant way to mix arrays arrays...

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 10-22-2005 06:10

Well, randomising the elements, in its most simple form, would involve a random number generator limited ot the number of elements in your array, and a secondary storage array. The random number generator detemines which index will be read out, then subsequently determines which index its data will go into in the new array. Keep repeating until the counter tells you you've got all of your elements out, and, voila, done. You'd put used indexes into a tokenised list or something, and have a check within your loop to make sure you're not repeating values.


Justice 4 Pat Richard

_Mauro
Bipolar (III) Inmate

From:
Insane since: Jul 2005

posted posted 10-22-2005 11:54

Ok, let me see if... well, this, at first, sounded like a challenge:

quote:

(If you find this problem simple, wait until others have had a try before posting your answer.)



Sounded like "you think it's easy? Wait a while and you'll see...", which triggered my "development planning mode" in absurd proportions.
--------------------------------------------------------------------------------------
What a misunderstanding... Upon re-reading it, my current understanding is:

"ok, Poi, people, you think it's easy, you've had your share of showtimes, let other people, normal people interested in javascript, take the "power" back,
after all, this forum's first purpose was educational."



Am I wrong? Am I right?
20 liners and extreme javascript are educational as well, only thing is they take time, to a "non-extreme coder", to figure out
and understand. Hence my post about collecting the 20 liners in a permanent galery, which would suite them, and would
"classify" part of the history of extreme javascript at the Asylum.

So please, people, I am not the best "Asylum spirit advocate", I should maybe shut the hell up, but the past is behind, I can't change it.
I can change the present and future. Somebody, back me up on this one, extreme javascript examples deserve a home of their own,
and the Asylum.DHTML forum deserves some basic javascript discussions.

Skaarjj? Slime? Somebody, ring a bell and get things to move the right way...

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-22-2005 12:29

I don't claim the implementation I posted before are good. Btw you can see in that same thread that sometime I trigger a hammer to smash little flies My Array.prototype.shuffle method works only for straight arrays ( with no textual keys ). I should add a pass before to build a little list of keys before really shuffling the items.

_Mauro
Bipolar (III) Inmate

From:
Insane since: Jul 2005

posted posted 10-22-2005 13:22

poi, while we're at it, and while I am performing my monthly comp maintenance, would you mind spending some time on Q?

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-22-2005 22:32

Mauro: yeah, your second interpretation is correct. =)

Skarjj: A good start; can you implement it? The random number generator you mention is probably most easily done with Math.floor(Math.random()*array.length)


 

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-23-2005 04:44



Notice that, I've resisted to post some code here.

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 10-23-2005 14:33

I certainly shall implement it, just as soon as I learn to write Javascript. I could do it for you in PHP, C and possibly Visual Basic... but, lets have a burl and see what we get...


Justice 4 Pat Richard

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-24-2005 18:36

Hm.. I may be cheating using a built in array function but here we go:

http://www.bitdamaged.com/testpages/arrRand.html



.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-24-2005 20:51

That's an interesting approach, bit. I wonder if it provides an equal chance of all random configurations?

I also wonder how the sorting algorithm reacts to the fact that the randomness can cause the first element to be "less than" the second, the second less than the third, and the third less than the first.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-24-2005 21:54

heh after looking at poi's post I'm reinventing the wheel (my other solution was liorien's)



.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-24-2005 22:05

I guess it didn't really work to ask a question that had already been answered on this forum =)

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-24-2005 23:04

For the record, I understood : "If you find this problem simple, wait until others have had a try before posting your answer." as : "Don't let the apparent simplicity fool you. It can be much more complex than it seems ... and Slime uncovered some nifty subtleties in the other thread."

Now let's pretend the question has not been anwsered. Red flash!!



(Edited by poi on 10-24-2005 23:06)

« BackwardsOnwards »

Show Forum Drop Down Menu