我正在尝试创建一个具有一组问题和答案的琐事游戏(每个问题都有一个正确答案,但其他3个答案将以多选格式显示,供用户选择)。
我遇到的问题是我不知道如何最好地构造对象以实现目标。
到目前为止,我已经考虑了以下方法:-创建一个“问题”对象,其中包含一系列问题,正确答案和每个问题的虚拟答案。IE
var metricQuestions = {
q1: {
question: "What is my name?",
correctAnswer: "Elizabeth",
incorrectAnswer1: "David",
incorrectAnswer2: "Fraser"
},
q2: {
question: "What is my dog's name?",
correctAnswer: "Annie",
incorrectAnswer1: "Purple",
incorrectAnswer2: "Face"
}
};
从人工工作的角度来看,这种方法很烂,因为当我向用户提供问题和正确答案时,我无法从一堆随机的“错误”答案中提取信息。感觉效率低下和重复。
-分别为“问题”和“答案”创建单独的对象,并在问题对象内引用对于特定问题正确的答案。在语法方面,我无法使它正常工作。
var metricQuestions = {
q1: {
question: "What is First Contentful Paint (FCP)?",
correctAnswerID = (metricAnswers.a1)
},
q2: {
question: "What is Time to Interactive (TTI)?",
correctAnswerID: a1
}
};
我还尝试过在metricQuestions对象中将正确的答案设置为字符串,但是当我随机抽取其他选择选项(以及其他潜在问题)。
-我尝试过的最后一种方法是嵌套方法,其中同一对象内的属性相互引用。从语法上讲,我遇到各种各样的错误“ Uncaught SyntaxError:无效的速记属性初始化程序。”
var questionBank = {
answers: {
a1: "A lab metric that measures when the main thread is quiet enough to respond to user input.",
a2: "A field and lab metric that tells when the first pixel is painted on the screen.",
a3: "Test answer 3.",
a4: "Test answer 4.",
a5: "Test answer 5."
},
questions: {
q1: {
question: "What is First Contentful Paint (FCP)?",
correctAnswerID = questionBank.answers.a1
},
q2: {
question: "What is Time to Interactive (TTI)?",
correctAnswerID = questionBank.answers.a2
},
}
};
对于多选琐事游戏,我如何最有效地解决一堆问题,并提供相应的正确答案和几个错误答案?
慕虎7371278
相关分类