0

I have a set of images and all I want to do is have the images randomize using a javascript function and shuffle button. This is probably pretty simple, but the truth is I have no idea how to do this. I'm pretty sure I'd have to use Math.random or something like that.

Bill Ted
  • 1
  • 1
  • You would have an array of images, you want to shuffle. This may help: http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript – Thorsten Kettner Dec 04 '15 at 09:59
  • Check the algorithm [Fisher–Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). It's easy to implement – and yes, you would indeed use `Math.random` to get a random number. – Jongware Dec 04 '15 at 10:09

1 Answers1

0

I created a jsbin that does what you want.

And yes, you can use Math.random.

You also need to find a way to select the random image DOM elements (whether by custom attributes, or class, etc.).

after that, define a function that generates a random number. If that random number matches the element number, reveal it. Hide the rest of the ones that do not match.

http://jsbin.com/jevecotase/edit?html,js,console,output

J. Lee
  • 513
  • 4
  • 15