Java Script – Random pictures

Imagine that you are in a situation, on which you are wondering among 8 different choices. Thus, the following program would be able to help you take a solution. Or at least to check the way JS works with arrays, pictures, functions and buttons.

Here is what the code does – it loads 8 pictures with the trivial names pic1 to pic8 from the code folder:

pics2Then it randomizes them. You may randomize, until you get one picture, repeated everywhere. Sooner or later (after about 10 randomizations) this would happen, because of the code and its author:

pics1

So, if you have decided that if the koala is selected over the others you will stay at home and sleep – so be it! 🙂

Here comes the code with the tricky randomization:

 

If you remove line 34, the pics would be made equal pretty faster.

Enjoy the code! 🙂

Available in GitHub here.

 

Tagged with: , ,