Duplicate list items

33 Views Asked by At

Please help me fix this software defect The application works without a problem But the code is almost missing some additions To prevent duplicate questions The exercise contains 10 questions. Note that some of them are repeated more than once 14÷2 16÷2 4÷2 You will find that the same questions may be repeated more than once even though the question counter is working normally This is a contradiction and a defect Please help me fix this defect

import 'dart:math';


import 'package:get/get.dart';
 

class DivisionChoiceNumberExerciseController extends GetxController {
  DivisionChoiceNumberExerciseController();
  var random = new Random();
  final question = 0.obs;

  final questionCount = 0.obs;
  final rightAnswer = 0.obs;
  final showResult = false.obs;
  final next = false.obs;

  final answerList = <int>[].obs;

  final selectedIndex = 0.obs;
  final correctIndex = 0.obs;

  final submitCount = 0.obs;
  final score = 0.obs;

  final object = 0.obs;
  int max;

  @override
  void onInit() {
    score.value = 0;
    this.max = Get.arguments['type'];
    generateNewQuestion();
    super.onInit();
  }

  void generateNewQuestion() {
    cleanData();
    List<int> tmpQuestion = [];

    for (var i = 1; i <= 10; i++) {
      tmpQuestion.add(i * max);
    }

    if (questionCount < 10) {
      int questionIndex = Helper.getRandomNum(0, 10);
      question.value = tmpQuestion[questionIndex];
      questionCount.value++;
    }
    answerList.add((question.value / max).round());
    while (answerList.length != 3) {
      int answer = Helper.getRandomNum(1, 11);
      if (!answerList.contains(answer)) {
        answerList.add(answer);
      }
    }
    answerList.shuffle();
    object.value = Helper.getRandomNum(0, 6);
    correctIndex.value = answerList.indexOf(question.value / max);

    update();
  }

  cleanData() {
    showResult.value = false;
    next.value = false;
    submitCount.value = 0;
    selectedIndex.value = 0;
    answerList.clear();
  }

  void submitAnswer(int index) {
    submitCount.value = ++submitCount.value;

    showResult.value = true;
    selectedIndex.value = index;
    if (index == correctIndex.value) {
      rightAnswer.value++;
      next.value = true;
      score.value = score.value + this.getScore(submitCount.value);
      print('score $score');
    }
    update();
  }

  int getScore(int submitCount) {
    switch (submitCount) {
      case 1:
        return 10;
      case 2:
        return 7;
      default:
        return 5;
    }
  }
}

class Helper {
  Helper._();

  static int getRandomNum(int min, int max) {
    Random _random = new Random();

    return min + _random.nextInt(max - min);
  }
}

0

There are 0 best solutions below