【发布时间】: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