On Apr 3, 9:49 am, ab5tract <[EMAIL PROTECTED]> wrote:
> Thank you so much man. I really appreciate it, even if I don't
> understand the single-line glory of
>
>    set.push(dat.splice(Math.random()*dat.length|0, 1));

  set.push(...)

Calls the push method of set, which is an array, with the
result of the expression inside the brackets.  The expression returns
a single element of the dat array.

  dat.splice( ..., 1)

Calls the splice method of dat, an array, with two argments: the first
is an expression that returns an integer that is used as the index to
start splicing from, the second is how many to splice (which is set to
1).

Splice removes the elements from the array (so you don't get a
repeated element) and returns it.


  Math.random()

Returns a random (decimal) number between 0 and 1.


  *dat.length

Multiplies the random number to get a (decimal) value between 0 and
dat.length (which gets shorter on each iteration due to splicing).  A
decrementing counter could be used instead if speed is an issue.


 | 0

A comparison using the bit-wise OR operator causes a value to be
returned that is an integer created by truncating the decimal part of
the random number (or zero).  It is equivalent to calling
Math.floor(...) with the random number as an argument, but faster and
needs less typing.  I'm up for that.  :-)


> It is pure JavaScript.

Yes, although "pure ECMAScript" might be more concise, "javascript"
usually infers DOM interaction and a certain amount of host
environment capability.


> I assume there is no "better" way to use jQuery?

While it is a reasonably common requirement, it is not something that
I would expect to find in a library but might be in a widget (or
plugin or package or whatever).  The lack of other reponses indicates
that jQuery has nothing to change my mind about that.  :-)


--
Rob

Reply via email to