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.

5
  • 2
    The procedure to follow is called "shuffling". Commented May 11, 2016 at 14:21
  • @Pointy I'll give that a Google. Thing is that this script was working, till I tried to adjust it, and I can't see why it isn't working as of right now. Commented May 11, 2016 at 14:22
  • Why you tag PHP? Commented May 11, 2016 at 14:23
  • @AlgernopK. no need to google - my comment is a link to the Wikipedia page. Commented May 11, 2016 at 14:24
  • I don't have any flag remaining. Could someone flag the question as the 1000th duplicate of stackoverflow.com/questions/6274339/…? Commented May 11, 2016 at 14:30

3 Answers 3

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>');
}

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

i would not use sort for a single loop task.
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.
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!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.