You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

quiz.page.ts 2.4 KiB

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Component, OnInit } from '@angular/core';
  2. import * as faker from 'faker';
  3. @Component({
  4. selector: 'app-quiz',
  5. templateUrl: './quiz.page.html',
  6. styleUrls: ['./quiz.page.scss'],
  7. })
  8. export class QuizPage implements OnInit {
  9. secondsPerQuestion: number;
  10. selectedQuestion: number = 0;
  11. questionType: 'MCQ' | 'CARD';
  12. questions: Array<{
  13. type: 'MCQ' | 'CARD',
  14. question: any,
  15. choices?: Array<{
  16. value: string,
  17. text: string,
  18. }>,
  19. correctAnswer?: string,
  20. secondsAllotted: number
  21. }>;
  22. showReport: boolean = true;
  23. constructor() { }
  24. ngOnInit() {
  25. this.startQuiz();
  26. }
  27. startQuiz() {
  28. this.selectedQuestion = 0;
  29. this.showReport = false;
  30. let timer = setInterval(() => {
  31. if (this.secondsPerQuestion === 0) {
  32. clearInterval(timer);
  33. } else {
  34. this.secondsPerQuestion -= 1;
  35. }
  36. }, 1000);
  37. let cardQuestions = [];
  38. for (let i = 0; i < 4; i += 1) {
  39. cardQuestions.push({
  40. text: faker.lorem.sentence(),
  41. image: faker.image.abstract()
  42. });
  43. }
  44. this.questions = [{
  45. type: 'MCQ',
  46. question: faker.lorem.sentence(),
  47. choices: [{
  48. value: 'A',
  49. text: 'Choice 1'
  50. }, {
  51. value: 'B',
  52. text: 'Choice 2'
  53. }, {
  54. value: 'C',
  55. text: 'Choice 3'
  56. }],
  57. correctAnswer: 'A',
  58. secondsAllotted: 50
  59. }, {
  60. type: 'CARD',
  61. question: cardQuestions,
  62. secondsAllotted: 20
  63. }];
  64. this.secondsPerQuestion = this.questions[this.selectedQuestion].secondsAllotted;
  65. this.questionType = this.questions[this.selectedQuestion].type;
  66. }
  67. nextQuestion() {
  68. if (this.selectedQuestion < this.questions.length - 1) {
  69. this.selectedQuestion += 1;
  70. this.secondsPerQuestion = this.questions[this.selectedQuestion].secondsAllotted;
  71. this.questionType = this.questions[this.selectedQuestion].type;
  72. } else {
  73. this.showReport = true;
  74. }
  75. }
  76. getCardEvent(data) {
  77. if (data) {
  78. if (data.end) {
  79. this.nextQuestion();
  80. }
  81. }
  82. }
  83. }