angular - 如何在我的 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);
}
}
}
解决方案
@integral100x
您需要更改 DependencyInjectionQuizComponent 中的 checkIfAnsweredCorrectly 方法逻辑。
您需要检查您的 correctAnswerFound 是否大于-1,因为如果您的数组在位置 0 找到了元素。If 循环由于值 0 而退出。所以,我更改为检查大于 -1。
if (correctAnswerFound > -1) {
this.sendCorrectCountToQuizService(this.correctCount + 1);
}
代替
if (correctAnswerFound) {
this.sendCorrectCountToQuizService(this.correctCount + 1);
}
谢谢
推荐阅读
- angular - 为什么在 onAuthStateChanged 中导航后 HTML 文件上的变量不更新?
- python - 通过避免 for 循环从节点列表构建邻接矩阵
- mongodb - 如何解决错误 由于目标机器主动拒绝,无法建立连接。?
- android - 如何在android中制作这个可滑动的过滤器
- reactjs - Semantic UI React Popup className 未添加到父元素
- javascript - 如何在vs代码中更改代码前的空格
- c++ - C++ 使用 delete[] 删除结构数组
- java - 从 JButton 中移除一个 ActionListener
- python - 为什么我在字符串对象上收到属性错误?
- python - PyQt5:如何将子小部件移动到主窗口?