首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在react测验应用程序中递增分数(多变量分数)

在react测验应用程序中递增分数(多变量分数)
EN

Stack Overflow用户
提问于 2020-06-19 22:53:19
回答 2查看 242关注 0票数 0

我正在用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”}

和代码本身:

代码语言:javascript
复制
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
            };
        })
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-19 22:59:56

向存储在qBank中的问题添加value属性。

代码语言:javascript
复制
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
        })
    }
}

票数 0
EN

Stack Overflow用户

发布于 2020-06-19 22:57:33

您可以考虑为每个问题添加一个额外的属性。

例如:

代码语言:javascript
复制
{ question: "I would describe my knowledge of investments as:", options: ["None", "Limited", "Good", "Extensive"], answer:"None", id:"2", value: 3 } 

然后更新分数:

代码语言:javascript
复制
if (userAnswer === answers) {
        this.setState({
            score: score + currentQuestion.value
    })
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62472883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档