Javascript测试-使用Array.prototype以随机顺序显示选项
Javascript quiz - display choices in a random order using Array.prototype
我想随机化选择的顺序。我添加了一个本应打乱选择顺序的脚本,但它没能做到。当我调试测试时,什么都没有显示。
这是我添加的代码:
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
Javascript测验:
var quiz = [{
"question": "What is the full form of IP?",
"choices": ["Internet Provider", "Internet Port", "Internet Protocol" , "Other"],
"correct": "Other"
}, {
"question": "Who is the founder of Microsoft?",
"choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak" , "Martin Shaba"],
"correct": "Bill Gates"
}, {
"question": "What was your first dream?",
"choices": ["8 bits", "64 bits", "1024 bits"],
"correct": "8 bits"
}, {
"question": "The C programming language was developed by?",
"choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"],
"correct": "Dennis Ritchie"
}, {
"question": "What does CC mean in emails?",
"choices": ["Carbon Copy", "Creative Commons", "other"],
"correct": "Carbon Copy"
}];
向Array.prototype
:添加scramble
函数
if (!("scramble" in Array.prototype)) {
Object.defineProperty(Array.prototype, "scramble", {
enumerable: false,
value: function() {
var o, i, ln = this.length;
while (ln--) {
i = Math.random() * (ln + 1) | 0;
o = this[ln];
this[ln] = this[i];
this[i] = o;
}
return this;
}
});
}
var quiz = [{
"question": "What is the full form of IP?",
"choices": ["Internet Provider", "Internet Port", "Internet Protocol", "Other"],
"correct": "Other"
}];
quiz.forEach(q => q.choices.scramble());
console.log(quiz[0].choices);
最初我建议:
quiz.forEach(q => q.choices.sort(() => Math.random() - .5));
DanDavis指出,这种特殊的方法并没有达到合理的分布。
如果您希望"其他"在洗牌后保留为最后一个选择,您可以使用以下代码来完成:
var quiz = [{
"question": "What is the full form of IP?",
"choices": ["Internet Provider", "Internet Port", "Internet Protocol", "Other"],
"correct": "Other"
}, {
"question": "Who is the founder of Microsoft?",
"choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak", "Martin Shaba"],
"correct": "Bill Gates"
}, {
"question": "What was your first dream?",
"choices": ["8 bits", "64 bits", "1024 bits"],
"correct": "8 bits"
}, {
"question": "The C programming language was developed by?",
"choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"],
"correct": "Dennis Ritchie"
}, {
"question": "What does CC mean in emails?",
"choices": ["Carbon Copy", "Creative Commons", "Other"],
"correct": "Carbon Copy"
}];
function shuffle(array) {
var temporaryValue, randomIndex;
var currentIndex = array.length;
// While there remain elements to shuffle...
while (currentIndex > 1) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
quiz.forEach(function(question) {
var otherIndex;
var choices = question.choices;
var lastIndex = choices.length - 1;
shuffle(choices);
otherIndex = choices.indexOf('Other');
if (otherIndex >= 0) {
choices[otherIndex] = choices[lastIndex];
choices[lastIndex] = 'Other';
}
console.log(choices);
});
相关文章:
- 使用JS按顺序显示图像,而不是随机显示
- 如何随机显示列表项的顺序
- 我应该怎么做才能将这个数组从随机更改为在页面刷新时按顺序显示
- Javascript从数组中随机选择并按字母顺序排列
- Javascript - 如何按顺序从数组中挑选随机元素
- 按随机顺序对元素进行动画处理
- Javascript测试-使用Array.prototype以随机顺序显示选项
- 以随机顺序显示 ng 重复项目
- 以随机顺序定位和循环对象
- 拉斐尔随机顺序显示物体
- 如何使用 Jquery 获取 XML 文件,并在每次加载时以随机顺序显示元素一次
- 异步node.js中的随机顺序
- 返回表单提交中列表项的随机顺序
- Javascript幻灯片以随机顺序显示
- 按随机顺序禁用按钮,Rails/javascript
- 数组保持以随机顺序输出- Push
- Android web视图以随机顺序加载javascript/不加载
- 每次通过循环存储随机顺序的数组
- 为什么它以随机顺序初始化这个 Knockout.js 组件
- 为大范围整数生成随机顺序的聪明方法