Javascript 如何随机化div中按钮的顺序?

Javascript 如何随机化div中按钮的顺序?,javascript,html,Javascript,Html,我正在制作一个教育网站,我需要一些帮助 我有以下代码: challenge1 = num1*num2; challenge2 = num1*2+Math.floor(Math.random() *3) + 1;; challenge3 = num1*num2+Math.floor(Math.random() *9) + 1; challenge4 = num2+Math.floor(Math.random() *9) + 1; makeButton(ch

我正在制作一个教育网站,我需要一些帮助

我有以下代码:

    challenge1 = num1*num2;
    challenge2 = num1*2+Math.floor(Math.random() *3) + 1;;
    challenge3 = num1*num2+Math.floor(Math.random() *9) + 1;
    challenge4 = num2+Math.floor(Math.random() *9) + 1;

    makeButton(challenge1);
    makeButton(challenge2);
    makeButton(challenge3);
    makeButton(challenge4);

它是有效的,但是很容易发现正确的答案总是第一个按钮


有没有一种方法可以随机排列它们的顺序?谢谢。

这是关于重新排列列表的问题。您应该首先创建一个元素数组(不将它们附加到父元素),将其洗牌,然后再
parent.append(…list)

您可以在此处了解如何洗牌列表:

这是关于洗牌列表的。您应该首先创建一个元素数组(不将它们附加到父元素),将其洗牌,然后再
parent.append(…list)

您可以在此处了解如何洗牌列表:

您可以创建一个挑战数组,并使用自定义比较器对该数组进行排序:

   challenges = [
     num1*num2,
     num1*2+Math.floor(Math.random() *3) + 1,
     num1*num2+Math.floor(Math.random() *9) + 1,
     num2+Math.floor(Math.random() *9) + 1
   ]
   function randomizer () { return Math.random() > 0.5 }
   challenges.sort(randomizer)

   for (challenge of challenges) makeButton(challenge)

编辑 要实现更好的随机化,您可以使用随机索引创建对象列表,并对其进行正常排序:

   challenges = [
     { challenge: num1*num2, index: Math.random() },
     { challenge: num1*2+Math.floor(Math.random() *3) + 1, index: Math.random() },
     { challenge: num1*num2+Math.floor(Math.random() *9) + 1, index: Math.random() },
     { challenge: num2+Math.floor(Math.random() *9) + 1, index: Math.random() }
   ]

   function compare(a,b) { return a.index == b.index ? 0 : a.index > b.index ? 1 : -1 }
   challenges.sort(compare)

   for (i in challenges) makeButton(challenges[i].challenge)

您可以创建一个挑战数组,并使用自定义比较器对该数组进行排序:

   challenges = [
     num1*num2,
     num1*2+Math.floor(Math.random() *3) + 1,
     num1*num2+Math.floor(Math.random() *9) + 1,
     num2+Math.floor(Math.random() *9) + 1
   ]
   function randomizer () { return Math.random() > 0.5 }
   challenges.sort(randomizer)

   for (challenge of challenges) makeButton(challenge)

编辑 要实现更好的随机化,您可以使用随机索引创建对象列表,并对其进行正常排序:

   challenges = [
     { challenge: num1*num2, index: Math.random() },
     { challenge: num1*2+Math.floor(Math.random() *3) + 1, index: Math.random() },
     { challenge: num1*num2+Math.floor(Math.random() *9) + 1, index: Math.random() },
     { challenge: num2+Math.floor(Math.random() *9) + 1, index: Math.random() }
   ]

   function compare(a,b) { return a.index == b.index ? 0 : a.index > b.index ? 1 : -1 }
   challenges.sort(compare)

   for (i in challenges) makeButton(challenges[i].challenge)

在代码片段之后,您可以随机交换第一个和最后一个

让btncount=0;
功能按钮(编号){
btncount=btncount+1;
/*创建一个按钮*/
var b=document.createElement('button');
b、 setAttribute('class','pure button');
b、 setAttribute('onclick','check(${number})`);
b、 setAttribute('id',btncount);
b、 text内容=数字;
var buttons=document.getElementById(“按钮”);
(b)儿童;
//随机交换第一个和最后一个
if(Math.round(Math.random())){
//互换
buttons.appendChild(buttons.firstElementChild);
}
}
常数num1=1,num2=2;
常量challenge1=num1*num2;
常量challenge2=num1*2+Math.floor(Math.random()*3)+1;;
const challenge3=num1*num2+Math.floor(Math.random()*9)+1;
const challenge4=num2+Math.floor(Math.random()*9)+1;
makeButton(challenge1);
makeButton(challenge2);
makeButton(challenge3);
makeButton(challenge4)

在代码片段之后,您可以随机交换第一个和最后一个

让btncount=0;
功能按钮(编号){
btncount=btncount+1;
/*创建一个按钮*/
var b=document.createElement('button');
b、 setAttribute('class','pure button');
b、 setAttribute('onclick','check(${number})`);
b、 setAttribute('id',btncount);
b、 text内容=数字;
var buttons=document.getElementById(“按钮”);
(b)儿童;
//随机交换第一个和最后一个
if(Math.round(Math.random())){
//互换
buttons.appendChild(buttons.firstElementChild);
}
}
常数num1=1,num2=2;
常量challenge1=num1*num2;
常量challenge2=num1*2+Math.floor(Math.random()*3)+1;;
const challenge3=num1*num2+Math.floor(Math.random()*9)+1;
const challenge4=num2+Math.floor(Math.random()*9)+1;
makeButton(challenge1);
makeButton(challenge2);
makeButton(challenge3);
makeButton(challenge4)

将“挑战”放入数组并洗牌。您可以使用中所述的纯javascript进行洗牌,但我更喜欢使用如下方式进行洗牌:

console.log(随机序列([“a”、“b”、“c”)
将“挑战”放入数组并洗牌。您可以使用中所述的纯javascript进行洗牌,但我更喜欢使用如下方式进行洗牌:

console.log(随机序列([“a”、“b”、“c”)

将您的按钮放入一个数组中,然后将它们洗牌。这是否回答了您的问题?将你所有的挑战放在一个数组中,然后洗牌,每个挑战一个。您可以使用许多洗牌算法中的一种。一个简单的方法是尝试Math.floor((Math.random()*100)+1);把你的按钮放在一个数组中,然后洗牌。这能回答你的问题吗?将你所有的挑战放在一个数组中,然后洗牌,每个挑战一个。您可以使用许多洗牌算法中的一种。一个简单的方法是尝试Math.floor((Math.random()*100)+1);您的解决方案是可行的,但它并不完全是“随机的”。我的意思是,我观察到正确答案很有可能是第一个按钮。有没有办法让它更随机?第一个随机发生器有50%的几率移动数组中的项目。检查编辑,它会为每个挑战设置一个随机索引,并在此随机索引上对表进行排序。您的解决方案可行,但并不完全是“随机”的。我的意思是,我观察到正确答案很有可能是第一个按钮。有没有办法让它更随机?第一个随机发生器有50%的几率移动数组中的项目。检查编辑,它为每个挑战设置一个随机索引,并在此随机索引上对表进行排序。