Implement Array Shuffling in MooTools
While shuffling the order of array elements isn't a greatly useful function, I recently found myself needing to accomplish the task. I found a great post about how to achieve this feat using jQuery. Here's how to implement array shuffling in MooTools.
The MooTools JavaScript
Array.implement({
shuffle: function() {
//destination array
for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
return this;
}
});
This likely wont be used in the core framework but does have its uses.
![39 Shirts – Leaving Mozilla]()
In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...
![Introducing MooTools Templated]()
One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating:
new Element Madness
The first way to create UI-driven...
![Create a Simple Slideshow Using MooTools]()
One excellent way to add dynamism to any website is to implement a slideshow featuring images or sliding content. Of course there are numerous slideshow plugins available but many of them can be overkill if you want to do simple slideshow without controls or events.
![CSS Vertical Centering]()
Front-end developing is beautiful, and it's getting prettier by the day. Nowadays we got so many concepts, methodologies, good practices and whatnot to make our work stand out from the rest. Javascript (along with its countless third party libraries) and CSS have grown so big, helping...
Another way (more elegant in my opinion):
@Elad: Awesome! Nice work. Definitely more elegant.
Just a note that you can do cool stuff like this with Array Shuffling. :)
@Lim Chee Aun: Awesome!
I’ll point out that @David’s example is guaranteed to not require any additional storage space (it shuffles in place), is unbiased toward permutations, and runs in O(n) time. @Elad’s shuffle depends on the browser’s implementation of sort(). It may require more storage space, it may be biased to certain permutations, and it will run in O(n log n) time or worse due to the nature of sorting. However, I’m guessing that anything shuffled in JS is going to be small and no one will care if it is biased as long as it “seems” random.
I’ve tested both Elad and David script on a 27 lenght array… the shuffling is far better using David’s one….
Anyone for the best of both world : efficiency and elegance ?
Anyway thank you so much for the tip !
I’m trying to learn mootools (both in a general sense, and all over again with 1.2), and javascript OOP at the same time. How I use this to shuffle the order of some list elements on a webpage?
And, if you don’t mind, how would I do this in 1.1 vs. 1.2?
Hehe although the original idea is older than my mom (probably no one here knows the creator of this…), I would be glad if people could copy my things keeping the credits ^^
http://jsfromhell.com/array/shuffle