我正在创建一个包含多项选择题的测验。问题和答案从外部xml文件加载。答案选项的数量不一致。它可以变化。我正在从xml文件加载数据并将其转换为测验应用程序可以访问的全局JSON对象。有没有办法循环遍历answers
对象声明中的答案?整个对象目前看起来像这样(注意currentQuestion
是对正在读取的xml的变量引用):
var questionAnswerContentObject = {
question: {
questionTitle: currentQuestion.find("questionTitle").text(),
questionBody: currentQuestion.find("questionBody").text()
},
answers: {
// this is where the answers are supposed to go.
// the loop is just dummy test code. it fails with "Parsing error: Unexpected token ("
for(var i=0;i<10;i++) {
answer: i;
}
}
};
答案 0 :(得分:1)
您可以在使用数组初始化对象后添加其他答案。然后,您可以在读取XML文件后使用for循环动态添加答案。您的对象可以设置为:
answer : {
[
answer1: {
}
...
]
}
答案 1 :(得分:1)
有没有办法循环回答对象声明中的答案?
不,你不能
您可以做的是创建一个函数并初始化这些属性。
var questionAnswerContentObject = function() {
this.question = {
questionTitle: "title",
questionBody: "body"
};
this.answers = (function() {
// this is where the answers are supposed to go.
// the loop is just dummy test code. it fails with "Parsing error: Unexpected token ("
var array = [];
for (var i = 0; i < 10; i++) {
array.push(i);
}
return array;
})();
};
var questionObject = new questionAnswerContentObject()
console.log(questionObject);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 2 :(得分:1)
你可以这样做。
func SetSessionPlayerOn()
{
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch _ {
}
}
func SetSessionPlayerOff()
{
do {
try AVAudioSession.sharedInstance().setActive(false)
} catch _ {
}
}
答案 3 :(得分:1)
我认为您可以直接执行此操作,只要您可以访问原始项目的答案列表。您不能直接在对象文字中使用for
- 循环。但您可以使用map
声明。
所以我想象你可以像数组一样获得答案的场景,类似于你获得标题的方式(currentQuestion.find("questionTitle").text()
),也许是currentQuestion.find("questionAnswers").values()
。然后,您可以简单地map
覆盖文字内的结果:
// This is just a dummy implementation. Don't know what the real one would look like
const currentQuestion = {
questionTitle: 'Why?',
questionBody: 'What is the meaning of life?',
questionAnswers: ['Love', '42', 'Mu'],
find: (prop) => ({text: _ => currentQuestion[prop],
values: _=> currentQuestion[prop]})
}
const questionAnswerContentObject = {
question: {
questionTitle: currentQuestion.find("questionTitle").text(),
questionBody: currentQuestion.find("questionBody").text()
},
answers: currentQuestion.find("questionAnswers").values().map(i => ({answer: i}))
};
console.log(questionAnswerContentObject)
&#13;
答案 4 :(得分:0)
试试这个脚本。我希望它会对你有帮助。
var quiz = {
question1:"what is a function ?",
question2:"what is polymorphysm ",
question3:"List two javascript framework",
question4:"what php is used for?",
question5:"is Java multy threaded ?",
quiz1:["to make code better","to arrange your program","a function is a reusable block of code which you can use in your program"],
quiz2 :["to format your code","it is not necessary in programming","in objet oriented programming polymorphysim is the ability of an object to take on many form..."],
quiz3:["django ,express","symphony ,ruby on rail"," jquery , angular"],
quiz4:["doing some front programming","a program language to computer operating system ","php is used to built web site and interact with database..."],
quiz5:["no because it improve complexity","yes , each thread cannot run code concurrently","yes in multy threaded program each can handle different task at the same time"],
displayQuiz: (question)=>{
switch(question){
case "question1": console.log("Q1 : " + quiz.question1);
for (i=0 ; i< quiz.quiz1.length ;i++){ console.log( (i +1) + ") "+ quiz.quiz1[i]);}
break;
case "question2": console.log("Q2 : " + quiz.question2);
for (i=0 ; i< quiz.quiz1.length ;i++){ console.log( (i +1) + ") "+ quiz.quiz2[i]);}
break;
case "question3": console.log("Q3 : " +quiz.question3);
for (i=0 ; i< quiz.quiz1.length ;i++){ console.log( (i +1) + ") "+ quiz.quiz3[i]);}
break;
case "question4": console.log("Q4 : " +quiz.question4);
for (i=0 ; i< quiz.quiz1.length ;i++){ console.log( (i +1) + ") "+ quiz.quiz4[i]);}
break;
case "question5": console.log("Q5 : " +quiz.question5);
for (i=0 ; i< quiz.quiz1.length ;i++){ console.log( (i +1) + ") "+ quiz.quiz5[i]);}
break;
}
},
findAnswer:(question)=>{
switch(question){
case "question1": console.log( quiz.question1);
console.log("a function is a reusable block of code which you can in your program ");
break;
case "question2": console.log( quiz.question2);
console.log("in objet oriented programming polymorphysim is the ability of an object to take on many form...");
break;
case "question3": console.log(quiz.question3);
console.log(" jquery, angular...");
break;
case "question4": console.log(quiz.question4);
console.log("php is used to built web site and interact with database...");
break;
case "question5": console.log(quiz.question5);
console.log("yes in multy threaded program each can handle different task at the same time");
break;
}
}
}
显示问题1的测验问题:quiz.displayQuiz("question1")
找到正确答案:quiz.findAnswer("question1")
注意:不要忘记通知我潜在的错误。要在控制台上运行