3

I'm trying to output a javascript array of 4 items in a random order. Problem is, I can only get one to print randomly, like this:

Sequence:

Diet Coke

..but I would like it to print something similar to:

Sequence:

Diet Coke

Psuedo Coke

None-psuedo Coke

Coke Zero

..in a random order, of course.

<div id="test1"></div>
<div id="test1-drinks"></div>

<script>

    var contents=new Array()
    contents[0]='Coke Zero'
    contents[1]='Diet Coke'
    contents[2]='Psuedo Coke'
    contents[3]='None-psuedo Coke'


    var i=0
    var random
    var spacing="<br>"
    while (i<contents.length){
        random=Math.floor(Math.random()*contents.length)
        document.getElementById('test1').innerHTML = "Sequence:<br>";
        if (contents[random]!="selected"){
            document.getElementById('test1-drinks').innerHTML = contents[random]+spacing;
            //mark element as selected
            contents[random]="selected"
            i++
        }
    }

</script>

I was hoping the while-loop would cover that for me, but it's not.. Any help is appreciated, and languages php, jQuery and JavaScript are all much welcome.

EDIT: Script's from here, but I don't want document.write to be used to I tried fixing it up.

Community
  • 1
  • 1
Algernop K.
  • 477
  • 2
  • 19

3 Answers3

2

You could splice the array with a random index until the array is empty.

var contents = ['Coke Zero', 'Diet Coke', 'Psuedo Coke', 'None-psuedo Coke'];

while (contents.length) {
    document.write(contents.splice(Math.floor(Math.random() * contents.length), 1) + '<br>');
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Step 1: Define your array:

var contents=new Array()
    contents[0]='Coke Zero'
    contents[1]='Diet Coke'
    contents[2]='Psuedo Coke'
    contents[3]='None-psuedo Coke'

Step 2: Randomly order the array:

contents.sort(function() {
  return .5 - Math.random();
});

Step 3: Print the values:

$.each(contents, function(index,value) {
    document.getElementById('test1-drinks').append(value + "<br/>");
});

Your problem

  1. You add 1 to i even after you collide with a value that was already selected, so you will not always print 4 times.
  2. You override the data each time you print using innerHTML =. You need to allow for the old html to stay there, by using append.
Glubus
  • 2,819
  • 1
  • 12
  • 26
  • i would not use sort for a single loop task. – Nina Scholz May 11 '16 at 14:32
  • Clearly the creation of the random ordering can be implemented in a better way performance wise. However, it is not relevant to the question. OP can decide for him/herself what implementation he/she wants to use to create the random ordering, though I like this method for examples like these because of its compactness. – Glubus May 11 '16 at 14:37
  • Also, unless you were not talking about the time complexity of this solution, sorting once is less costly then splicing `n` times, as a single splice is `O(n)` complex, resulting in a `O(n^2)` complex solution as opposed to the `O(n log n)` complex sorting method. No offense taken! – Glubus May 11 '16 at 14:41
0

My bad. Adjusted:

document.getElementById('test1-drinks').innerHTML = contents[random]+spacing;

to

document.getElementById('test1-drinks').innerHTML += contents[random]+spacing;

so I wouldn't keep replacing the randomized order.

Algernop K.
  • 477
  • 2
  • 19