简体   繁体   English

以随机顺序打印数组?

[英]Print array in random order?

I'm trying to output a javascript array of 4 items in a random order.我试图以随机顺序输出 4 个项目的 javascript 数组。 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.我希望 while 循环会为我覆盖这些内容,但事实并非如此。感谢任何帮助,并且非常欢迎 php、jQuery 和 JavaScript 语言。

EDIT: Script's from here , but I don't want document.write to be used to I tried fixing it up.编辑:脚本来自此处,但我不希望使用 document.write 来修复它。

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

Step 1: Define your array:第 1 步:定义数组:

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:第 2 步:随机排列数组:

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

Step 3: Print the values:第 3 步:打印值:

$.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.即使在与已选择的值发生冲突后,您也将 1 添加到i ,因此您不会总是打印 4 次。
  2. You override the data each time you print using innerHTML = .每次使用innerHTML =打印时都覆盖数据。 You need to allow for the old html to stay there, by using append.您需要使用 append 允许旧的 html 留在那里。

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.所以我不会继续替换随机顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM