我正在用react.js创建测验应用程序。问题:如何使总分不仅递增1还递增3,4(每个答案都有唯一的分数)题库:设qBank =[{问题:“我计划开始从我的投资中提取资金:”,options:“少于3年”,“3-5年”,“6-10年”,“11年或更长”,答案:“3-5年”,id:"0“},{问题:”一旦我开始从我的投资中提取资金,我计划将所有的资金花在:“,选项:“少于2年”,“2-5年”,“6-10年”,“11年或更长”,答案:“2-5年”,id:"1“},{问题:”我会将我的投资知识描述为:“,选项:”无“,”有限“,”好“,”广泛“,答案:”无“,id:"2”}
等
和代码本身:
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
};
})
}
}发布于 2020-06-19 22:59:56
向存储在qBank中的问题添加value属性。
let qBank = [
{
question: "I plan to begin withdrawing money from my investments in:",
options: ["Less than 3 years", "3-5 years", "6-10 years", "11 years or more"],
answer:"3-5 years",
value: 1,
id:"0"
},
{
question: "Once I begin withdrawing funds from my investments, I plan to spend all of the funds in:",
options: ["Less than 2 years", "2-5 years", "6-10 years", "11 years or more"],
answer:"2-5 years",
value: 2,
id:"1"
},
{ question: "I would describe my knowledge of investments as:",
options: ["None", "Limited", "Good", "Extensive"],
answer:"None",
value: 3,
id:"2"
}
]
nextQuestionHandler = () => {
const { userAnswer, answers, score, value } = this.state;
this.setState({
currentQuestion: this.state.currentQuestion + 1
})
//increment the score if answer is correct
if (userAnswer === answers) {
this.setState({
score: score + value
})
}
}
发布于 2020-06-19 22:57:33
您可以考虑为每个问题添加一个额外的属性。
例如:
{ question: "I would describe my knowledge of investments as:", options: ["None", "Limited", "Good", "Extensive"], answer:"None", id:"2", value: 3 } 然后更新分数:
if (userAnswer === answers) {
this.setState({
score: score + currentQuestion.value
})
}https://stackoverflow.com/questions/62472883
复制相似问题