【问题标题】:How can I fix scoring for multiple-answer questions in my Angular quiz app?如何在我的 Angular 测验应用程序中修复多答案题的评分?
【发布时间】:2020-10-21 07:26:09
【问题描述】:

对于多选题,应选择所有正确答案以提高分数,而不仅仅是一个正确答案。如果选择了一个正确答案,然后将下一个答案标记为不正确,则分数仍然增加 1;只有在给出所有正确答案时它才会增加......如果给出一个错误答案然后是所有正确答案,那么分数也应该增加或者如果一个正确答案,然后是一个不正确然后一个正确答案,则分数应该增加。此外,单答案问题应按预期增加分数。请你帮忙解决这个问题。在此处查看我的应用:https://stackblitz.com/edit/angular-10-quiz-app

当前代码 sn-p 用于增加分数(在 src -> app -> 容器 -> quiz -> quiz.component.ts -> checkIfAnsweredCorrectly()):

checkIfAnsweredCorrectly() {
  if (this.question) {
    const correctAnswerFound = this.answers.find((answer) => {
      return this.question.options &&
        this.question.options[answer] &&
        this.question.options[answer]['selected'] &&
        this.question.options[answer]['correct'];
    });

    const answers = this.answers && this.answers.length > 0 ? this.answers.map((answer) => answer + 1) : [];
    this.quizService.userAnswers.push(this.answers && this.answers.length > 0 ? answers : this.answers);

    if (correctAnswerFound > -1 && 
        answers.length === this.quizService.numberOfCorrectAnswers) {
      this.sendCorrectCountToQuizService(this.correctCount + 1);
    }
  }
}

【问题讨论】:

  • 这个问题目前非常具体到你的应用程序。如果这个问题更笼统,那么这个问题将与其他问题相关。请尝试将您的代码/问题描述减少到绝对最低限度。也许这甚至可以帮助您找到解决方案。
  • 我检查了你的代码,它太复杂而且不干净。我建议重构你的代码和逻辑,例如在 dependency-injection-quiz.component.ts 你完全重构 checkIfAnsweredCorrectly
  • 我想我可能需要添加到 if 语句以检查 userAnswers 是否在正确的AnswersForEachQuestion 数组中(也在 QuizService 中),这应该可以解决问题,但不知道该怎么做。

标签: angular typescript


【解决方案1】:

解决方法很简单。将“正确/错误”逻辑与“下一个按钮”逻辑分开。

你不应该有这样的东西:

if (!incorrectAnswerFound) {
  this.sendCorrectCountToQuizService(this.correctCount + 1);
}

你的函数应该是这样的:

advanceToNextQuestion() { ... }

和

addUpScores() { ... }

不要试图将逻辑组合成一个函数。这就是您的应用无法运行的原因。

【讨论】:

    【解决方案2】:

    @integral100x

    您需要更改 DependencyInjectionQuizComponent 中的 checkIfAnsweredCorrectly 方法逻辑。

    您需要检查您的 correctAnswerFound 是否大于 -1,因为如果您的数组在位置 0 找到元素。If 循环由于值 0 而退出。所以,我更改为检查大于 -1。

    if (correctAnswerFound > -1) {
            this.sendCorrectCountToQuizService(this.correctCount + 1);
    }
    

    而不是

    if (correctAnswerFound) {
        this.sendCorrectCountToQuizService(this.correctCount + 1);
    }
    

    谢谢

    【讨论】:

    • 感谢这个简单的修复。在问题 1(多项选择)中,我希望它需要选择所有正确答案才能提高分数,而不仅仅是一个正确答案。同样在 ResultsComponent 的 mat-accordion 的“正确答案”字段中,我仍然得到单答案问题的“选项”,应该只是“选项”。
    • 我修复了关于单选题选项的问题,不知道如何修复多选题的评分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2019-12-09
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多