简体   繁体   English

ReactJs:如何显示正确的问卷答案和总分?

[英]ReactJs: How can I show the correct questionnaire answer and the total score?

I am new to react and I want to arrange the correct option of each question on the same page. 我是新来的人,我想在同一页面上安排每个问题的正确选项。 Also, when the question is solved it should show whether it is correct or not. 同样,当问题解决后,它应显示它是否正确。 In the end, I need the total score. 最后,我需要总分。 How can I accomplish that? 我该怎么做? Thank you for the help in advance. 谢谢您的帮助。

This what I have done so far..( Normally there are more questions but I had to delete them to post) 这是我到目前为止所做的。.(通常会有更多问题,但我必须删除它们才能发布)

const questionsArray = [
  {
    question: 'When the C programming language has first appeared?',
    option1: '1970',
    option2: '1971',
    option3: '1972'
  },
  {
    question: 'When the Java programming language has first appeared?',
    option1: '1994',
    option2: '1995',
    option3: '1996'
  },
];


class QuizAppQuestion extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      currentQuestionIndex: 0,
      questions: [],
      answers: []
    };
  }

  componentDidMount() {
    this.setState({questions: questionsArray})
  }

  onChangeOption(value) {
    const {currentQuestionIndex} = this.state;
    let answers = {...this.state.answers};
    answers[currentQuestionIndex] = value;
    this.setState({answers});
  }

  handleNext() {
    let incrementCurrentQuestionIndex = this.state.currentQuestionIndex + 1;
    this.setState({currentQuestionIndex: incrementCurrentQuestionIndex});
  }

  render() {
    const {questions, currentQuestionIndex, answers} = this.state;
    if (!questions.length) {
      return <div>Loading questions...</div>
    }
    if (currentQuestionIndex >= questions.length) {
      return (<div><h3>End of the quiz</h3></div>)
    }

    const {question, option1, option2, option3} = questions[currentQuestionIndex];

    return (<div>
         <h1>Question {currentQuestionIndex + 1}</h1>
         <h4>{question}</h4>
         <label>
           <input type='radio' checked={answers[currentQuestionIndex] === option1} value={option1} onChange={(evt) => this.onChangeOption(evt.target.value)}/>
                    {option1}
         </label>
         <br/>
         <label>
           <input type='radio' checked={answers[currentQuestionIndex] === option2} value={option2} onChange={(evt) => this.onChangeOption(evt.target.value)}/>
                    {option2}
          </label>
          <br/>
          <label>
             <input type='radio' checked={answers[currentQuestionIndex] === option3} value={option3} onChange={(evt) => this.onChangeOption(evt.target.value)}/>
                    {option3}
          </label>
          <hr/>
          <button onClick={() => this.handleNext()}>Next</button>
        </div>);
  }
}

You would want to make a separate array that holds the answers, and test the selected answer against the appropriate question/answer in your answers array. 您可能希望创建一个单独的数组来保存答案,然后针对答案数组中的相应问题/答案测试所选答案。 So in your case something like: 因此,在您的情况下,例如:

const answerArray = [
  {
    question: "When the C programming language has first appeared?",
    answer: "option1"
  }
]

onSubmit = (selectedAnswer) => { // Selected answer is the option picked from your select menu
  if(selectedAnswer === answerArray[currentQuestionIndex].answer) {
    // Logic for correct answer
  } else {
    // Logic for incorrect answer
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在测验中显示正确答案? - How can I show the correct answer in my quiz? 为什么我写的没有弹出问题? 只需转到警报(“正确答案总数:”+分数); 直接地 - Why there are no pop up questions that I wroted? Just go to the alert("Total correct answer: "+ score ); directly 我如何显示答案是否正确 - How do i show if an answer is correct or not 我想在答案正确时增加分数 - I want to increment the score when an answer is correct 无法弄清楚如何根据用户的回答计算总分 - can't figure it out how to calculate total score from user's answer 我如何在选择时显示正确和错误的答案以及在我的 mcq 网站上选择的人的正确答案数量? - How I can show the correct and incorrect answer at the moment of selection and the number of correct answers a person selected in my mcq website? 如何从使用tincan的程序包中获取诸如分数,正确答案之类的详细信息? - How to get details such as score, correct answer from package running with tincan? 如何通过表单输入从正确答案中获得分数 - How to get score from correct answer through form input 在文本框中输入正确答案时如何在 Vanilla JS 中增加分数 - How to increment score in Vanilla JS when a correct answer is typed into a textbox 如何在一个简单的游戏中将分数相加并得到正确和错误的答案 - How to add up the score in a simple game and have the correct and wrong answer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM