首页 > 解决方案 > 如何在我的 Angular 测验应用程序中修复多选题的评分?

问题描述

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

当前用于增加分数的代码片段(在 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);
    }
  }
}

标签: angulartypescript

解决方案


@integral100x

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

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

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

代替

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

谢谢


推荐阅读