我正在 react.js 中创建测验应用程序。问题:如何将总分不仅增加 1,而且增加 3、4(每个答案应该有唯一的分数)题库:let qBank = [ { 问题:“我计划开始从我的投资中提取资金:”,选项: ["不到3年", "3-5年", "6-10年", "11年以上"], answer:"3-5年", id:"0" }, {问题: “一旦我开始从投资中提取资金,我计划将所有资金用于:”,选项:[“少于 2 年”、“2-5 年”、“6-10 年”、“11 年或more"], answer:"2-5 years", id:"1" }, { question: "我将我的投资知识描述为:", options: ["None", "Limited", "Good", "
ETC
和代码本身:
nextQuestionHandler = () => {
const { userAnswer, answers, score } = this.state;
this.setState({
currentQuestion: this.state.currentQuestion + 1
})
//increment the score if answer is correct
if (userAnswer === answers) {
this.setState({
score: score + 1
})
}
}
//update the component
componentDidUpdate(prevProps, prevState) {
const { currentQuestion } = this.state;
if (this.state.currentQuestion !== prevState.currentQuestion) {
this.setState(() => {
return {
disabled: true,
questions: qBank[currentQuestion].question,
options: qBank[currentQuestion].options,
answers: qBank[currentQuestion].answer
};
})
}
}