使用Javascript随机分配项目列表



在下面的代码中,我希望根据选项列表生成问题、选项和答案。我成功地生成了问题和答案以及选项。但是,有时当我处理一个大列表时,选项也会重复,这让我的工作变得更加困难。我想要一个简单的解决方案,任何大的列表应该给我问题,选项和答案。

请注意:

我希望选项必须是随机的。

选项必须有一个随机位置的答案选项。

选项不能重复。

function myFunction() {
var myList=document.getElementById("myList").value;
var lines = myList.split("n");   //split lines
var addQ="";
var result1 = "";
var option2="";

lines.map(line => {
var result = line.split("s320/");
result1 += '"'+result[1] + 'n';

addQ+='new Question("Choose '+result[1]+' character from below!", ["", "", "", ""], "'+result[1]+'"),'+"n";
})

var addOption1 = result1.split("n");

for (var i = 0; i < addOption1.length; i++) { 
addQ=addQ.replace('["", "", "", ""]','["", '+addOption1[0]+'", "", ""]')
.replace('["", "", "", ""]','["", "", "", '+addOption1[1]+'"]')
.replace('["", "", "", ""]','["",'+addOption1[2]+'", "", ""]')
.replace('["", "", "", ""]','["", "", "", '+addOption1[3]+'"]')
.replace('["", "", "", ""]','["","",'+addOption1[4]+'", "",]')
.replace('["", "", "", ""]','["", '+addOption1[5]+'", "", ""]');
addQ=addQ.replace('["", '+addOption1[0]+'", "", ""]','['+addOption1[3]+'", '+addOption1[0]+'", '+addOption1[5]+'", '+addOption1[4]+'"]')
.replace('["", "", "", '+addOption1[1]+'"]','['+addOption1[4]+'", '+addOption1[5]+'", '+addOption1[4]+'", '+addOption1[1]+'"]')
.replace('["",'+addOption1[2]+'", "", ""]','['+addOption1[3]+'",'+addOption1[2]+'", '+addOption1[5]+'", '+addOption1[4]+'"]')
.replace('["", "", "", '+addOption1[3]+'"]','['+addOption1[4]+'", '+addOption1[2]+'", '+addOption1[2]+'", '+addOption1[3]+'"]')
.replace('["","",'+addOption1[4]+'", "",]','['+addOption1[0]+'",'+addOption1[5]+'",'+addOption1[4]+'", '+addOption1[5]+'",]')
.replace('["", '+addOption1[1]+'", "", ""]','['+addOption1[1]+'", '+addOption1[5]+'", '+addOption1[3]+'", '+addOption1[5]+'"]');
}
document.getElementById("result").value= addQ;
}
<!DOCTYPE html>
<html>
<body onload="myFunction()">
<b><label for="fname">List of Options:</label></b><br><br>
<textarea autocomplete="off" cols="30" id="myList" name="message" rows="10" style="border: 3px solid #73AD21; width: 90%;">
s320/Apple.jpg
s320/Ball.jpg
s320/Cat.jpg
s320/Doll.jpg
s320/Egg.jpg
s320/Frog.jpg</textarea><br><br>

<b><label for="fname">Questions + Options + Answers:</label></b><br><br>
<textarea autocomplete="off" cols="30" id="result" name="message" rows="10" style="border: 3px solid #73AD21; width: 90%;"></textarea>
</body>
</html>

我去掉了复杂的"replace"方法,并将其更新为在将选项添加到字符串时从选项列表中删除该选项,从而确保每个随机选择的值都是唯一的。

function myFunction() {
var myList=document.getElementById("myList").value;
var optionList = myList.split("n");   //split lines
var addQ="";
var result1 = "";
var option2="";

optionList.map(option => {
var result = option.split("s320/");
result1 += result[1] + ' ';
})
result1=result1.split(' ')
var filteredResult = result1.filter(function (el) { //eliminate empty elements
return el!='';
});

for(var i=0;i<optionList.length;i++){    
var str="";
var options=[] //create options array.
var tempOptionList=[...filteredResult]; //clone filteredResult to a new array.
var removedOptionCount=tempOptionList.length-4;
for(var t=0;t<removedOptionCount;t++){ //remove X answers from tempOptionList except the answer to obtain only 4 answers 
var randIndex=(Math.floor(Math.random() * tempOptionList.length)); //get random number

while(tempOptionList[randIndex] == filteredResult[i]){ // answer at index (i) so we don't want to delete it generate new random index
randIndex=(Math.floor(Math.random() * tempOptionList.length)); //get random number
}

tempOptionList.splice(randIndex,1);
}


for(var k=0;k<4;k++){ //fill options list randomly.
var randIndex=(Math.floor(Math.random() * tempOptionList.length)); //get random number
var randomOption=tempOptionList[randIndex];

while(options.includes(randomOption)){ //while option list includes random option generate new random option.
randIndex=(Math.floor(Math.random() * tempOptionList.length)); //get random number
randomOption=tempOptionList[randIndex];
}

options.push(randomOption);
}
for(var j=0;j<options.length;j++){

str+='"'+options[j]+'"' + (j==3 ?"":",") //check if there are 3 option left don't put comma at the end.

}   

addQ+='new Question("Choose '+filteredResult[i]+' character from below!", ['+str+'], "'+filteredResult[i]+'"),'+"n";  
}
document.getElementById("result").value= addQ;
}
<!DOCTYPE html>
<html>
<body onload="myFunction()">
<b><label for="fname">List of Options:</label></b><br><br>
<textarea autocomplete="off" cols="30" id="myList" name="message" rows="10" style="border: 3px solid #73AD21; width: 90%;">
s320/986.jpg
s320/973.jpg
s320/965.jpg
s320/963.jpg
s320/960.jpg
s320/958.jpg
s320/941.jpg
s320/924.jpg
s320/915.jpg
s320/905.jpg
s320/902.jpg
s320/898.jpg
s320/897.jpg
s320/889.jpg
s320/874.jpg
s320/871.jpg
s320/858.jpg
s320/854.jpg
s320/850.jpg
s320/848.jpg
s320/837.jpg
s320/831.jpg
s320/822.jpg
s320/814.jpg
s320/803.jpg
s320/759.jpg
s320/758.jpg
s320/757.jpg
s320/746.jpg
s320/742.jpg
s320/741.jpg
s320/733.jpg
s320/732.jpg
s320/731.jpg
s320/728.jpg
s320/725.jpg
s320/724.jpg
s320/708.jpg
s320/702.jpg
s320/698.jpg
s320/683.jpg
s320/675.jpg
s320/674.jpg
s320/663.jpg
s320/639.jpg
s320/636.jpg
s320/603.jpg
s320/601.jpg
s320/571.jpg
s320/569.jpg
s320/547.jpg
s320/545.jpg
s320/512.jpg
s320/472.jpg
s320/468.jpg
s320/465.jpg
s320/460.jpg
s320/442.jpg
s320/430.jpg
s320/424.jpg
s320/421.jpg
s320/415.jpg
s320/388.jpg
s320/360.jpg
s320/355.jpg
s320/324.jpg
s320/315.jpg
s320/313.jpg
s320/308.jpg
s320/283.jpg
s320/274.jpg
s320/273.jpg
s320/267.jpg
s320/266.jpg
s320/249.jpg
s320/239.jpg
s320/229.jpg
s320/215.jpg
s320/195.jpg
s320/194.jpg
s320/181.jpg
s320/165.jpg
s320/160.jpg
s320/157.jpg
s320/137.jpg
s320/111.jpg
s320/106.jpg
s320/102.jpg
s320/96.jpg
s320/84.jpg
s320/79.jpg
s320/73.jpg
s320/47.jpg
s320/42.jpg
s320/32.jpg
s320/30.jpg
s320/28.jpg
s320/21.jpg
s320/12.jpg
s320/7.jpg</textarea><br><br>

<b><label for="fname">Questions + Options + Answers:</label></b><br><br>
<textarea autocomplete="off" cols="30" id="result" name="message" rows="10" style="border: 3px solid #73AD21; width: 90%;"></textarea>
</body>
</html>

这是另一个答案
https://1389876.playcode。Io/(result)/https://playcode.io/1389876 (code)

相关内容

  • 没有找到相关文章

最新更新