将 JSON 个题库中的问题子集随机添加到调查 JS 测验中
Randomly Add Subset of Questions From JSON Question Bank to Survey JS Quiz
我正在使用调查 js 库创建测验。对于这个测验,我有一堆问题,我想从中随机 select。
为此,我存储了 JSON 个对象 - 序列化为 JSON 个问题。这是工作示例 - https://plnkr.co/edit/gnL4gv75uowDPyEU
现在,这适用于通过使用 foreach 循环将每个问题添加到页面。相反,我想随机添加 4 个问题中的 2 个。
从概念上讲,我了解如何执行此操作,但我对编程还比较陌生。
这是我对需要完成的事情的思考过程:
- 使用 Fisher-Yates 洗牌法获取随机数:
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i+1));
// swap randomly chosen element with current element
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var ranNums = shuffle([1,2,3,4]);
- 将 JSON 中序列化随机数的问题添加到页面。重复两次以添加两个问题。
这看起来相对简单,但是当我去实现它时,我似乎无法弄清楚如何实际序列化 json。此外,即使我弄清楚如何序列化 json 问题并相应地 select ,我也不明白添加问题的必要语法。有人可以帮忙吗?
您可以存储 JSON 个对象 - 序列化为 JSON 个问题。这些对象可以在数据库中存储为 JSON 或序列化的 JSON 字符串。您可以使用 JSON.parse
函数反序列化字符串中的 JSON 对象,并将反序列化的对象添加到具有潜在问题的数组 - questionJSONs。之后,您可以将此数组中的问题添加到您的调查中。这是工作示例 - https://plnkr.co/edit/gnL4gv75uowDPyEU
var questionJSONs = [
{
name: "name",
type: "text",
title: "Please enter your name:",
placeHolder: "Jon Snow",
isRequired: true
}, {
name: "birthdate",
type: "text",
inputType: "date",
title: "Your birthdate:",
isRequired: true
}, {
name: "color",
type: "text",
inputType: "color",
title: "Your favorite color:"
}, {
name: "email",
type: "text",
inputType: "email",
title: "Your e-mail:",
placeHolder: "jon.snow@nightwatch.org",
isRequired: true,
validators: [
{
type: "email"
}
]
}
];
var json = {
pages: [{name: "page1"}]
};
window.survey = new Survey.Model(json);
var page = survey.pages[0];
questionJSONs.forEach(function(questionJson) {
var question = page.addNewQuestion(questionJson.type, questionJson.name);
question.fromJSON(questionJson);
});
根据@TSV 的建议,我能够为这个问题创建一个解决方案。它使用 fisher-yates shuffle 来获得随机数组。然后,我将这个数组拼接成所需的长度。这是任何有类似问题的人的 plunker - https://plnkr.co/edit/I4vXNTFEgIP9zy1s?open=lib%2Fscript.js
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i+1));
// swap randomly chosen element with current element
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var ranNums = shuffle([0,1,2,3]);
var randomQuestionSelection = ranNums.slice(0,2);
for(var i = 0; i < randomQuestionSelection.length; i++) {
var randomQuestionJson = questionJSONs[randomQuestionSelection[i]];
var question = page.addNewQuestion(randomQuestionJson.type, randomQuestionJson.name);
question.fromJSON(randomQuestionJson);
}
我正在使用调查 js 库创建测验。对于这个测验,我有一堆问题,我想从中随机 select。
为此,我存储了 JSON 个对象 - 序列化为 JSON 个问题。这是工作示例 - https://plnkr.co/edit/gnL4gv75uowDPyEU
现在,这适用于通过使用 foreach 循环将每个问题添加到页面。相反,我想随机添加 4 个问题中的 2 个。
从概念上讲,我了解如何执行此操作,但我对编程还比较陌生。
这是我对需要完成的事情的思考过程:
- 使用 Fisher-Yates 洗牌法获取随机数:
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i+1));
// swap randomly chosen element with current element
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var ranNums = shuffle([1,2,3,4]);
- 将 JSON 中序列化随机数的问题添加到页面。重复两次以添加两个问题。
这看起来相对简单,但是当我去实现它时,我似乎无法弄清楚如何实际序列化 json。此外,即使我弄清楚如何序列化 json 问题并相应地 select ,我也不明白添加问题的必要语法。有人可以帮忙吗?
您可以存储 JSON 个对象 - 序列化为 JSON 个问题。这些对象可以在数据库中存储为 JSON 或序列化的 JSON 字符串。您可以使用 JSON.parse
函数反序列化字符串中的 JSON 对象,并将反序列化的对象添加到具有潜在问题的数组 - questionJSONs。之后,您可以将此数组中的问题添加到您的调查中。这是工作示例 - https://plnkr.co/edit/gnL4gv75uowDPyEU
var questionJSONs = [
{
name: "name",
type: "text",
title: "Please enter your name:",
placeHolder: "Jon Snow",
isRequired: true
}, {
name: "birthdate",
type: "text",
inputType: "date",
title: "Your birthdate:",
isRequired: true
}, {
name: "color",
type: "text",
inputType: "color",
title: "Your favorite color:"
}, {
name: "email",
type: "text",
inputType: "email",
title: "Your e-mail:",
placeHolder: "jon.snow@nightwatch.org",
isRequired: true,
validators: [
{
type: "email"
}
]
}
];
var json = {
pages: [{name: "page1"}]
};
window.survey = new Survey.Model(json);
var page = survey.pages[0];
questionJSONs.forEach(function(questionJson) {
var question = page.addNewQuestion(questionJson.type, questionJson.name);
question.fromJSON(questionJson);
});
根据@TSV 的建议,我能够为这个问题创建一个解决方案。它使用 fisher-yates shuffle 来获得随机数组。然后,我将这个数组拼接成所需的长度。这是任何有类似问题的人的 plunker - https://plnkr.co/edit/I4vXNTFEgIP9zy1s?open=lib%2Fscript.js
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i+1));
// swap randomly chosen element with current element
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var ranNums = shuffle([0,1,2,3]);
var randomQuestionSelection = ranNums.slice(0,2);
for(var i = 0; i < randomQuestionSelection.length; i++) {
var randomQuestionJson = questionJSONs[randomQuestionSelection[i]];
var question = page.addNewQuestion(randomQuestionJson.type, randomQuestionJson.name);
question.fromJSON(randomQuestionJson);
}