I have an array like so:
var arrA = [1lemon, 2lemons, 3lemons, 1orange, 2oranges, 3oranges, 1apple, 2apples, 3apples, 1banana, 2bananas, 3bananas, 1coconut, 2coconuts, 3coconuts];
...that I am shuffling with this function:
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
arrA = shuffle(arrA);
...and then I have some code to output the randomized array's fruits, one at a time, and every 4 seconds, like so:
var text = arrA;
var counter = 0;
var elem = document.getElementById("changeText");
setInterval(change, 4000);
function change() {
elem.innerHTML = text[counter];
counter++;
if(counter >= text.length) { counter = 0; }
}
They will appear on a div:
<div id="changeText"><p>pick fruit(s)</p></div>
But in this manner all elements in the array will appear on each loop (they have 100% of possibilities of appearing).
How to do so they appear with a certain frequency?
So the 1fruit appear 50% of the time, the 2fruits 30% of the time, and the 3fruits 20% of the time?
Just wrapping my mind around this...
Thanks for your ideas,