Home > Software design >  The last score point does not increase/increment in a quiz app
The last score point does not increase/increment in a quiz app

Time:04-05

Say, I have got a multiple choice test, which consists of 5 questions. When the answer is correct, guestion number, score points, progress bar (still in the future, now it’s not functioning yet) should be reloaded, i.e. increased by one point.

The problem is that when the last question is answered correctly (the 5th question) and an alert controller shows up, the score point doesn’t increase to 5, but remains at 4. I was trying to put it (score =1) in all different spots of this function private func checkAnswer(for answer: Answer) to make the score increase to 5, but useless.

It probably will work by introducing @IBAction func answerPressed(_ sender: UIButton), but in that case a lot of stuff should be rewritten.

So my question is whether it is possible make it work in the existing code.

`private func shuffleQuestions() {
  shuffledQuestions = questions.shuffled()
  questionNumber = 1
  score = 0
  showQuestion()
}

private func showQuestion() {

  questionCounterLabel.text = "\(questionNumber)/\(questions.count)"
  scoreLabel.text = "Score: \(score)"
  progressView.frame.size.width = (view.frame.size.width/CGFloat(questions.count))*CGFloat(questionNumber)
  
  currentQuestion = shuffledQuestions[questionNumber - 1]
  questionNameLabel.text = currentQuestion?.text ?? ""
  
  questionNumberLabel.text = "Question \(questionNumber)"
  questionNumber  = 1
  score  = 1
  tableView.reloadData()
  
}

private func checkAnswer(for answer: Answer) {
  
  if answer.correct {
    if questionNumber > questions.count {
      
            let alert = UIAlertController(title: "Awesome",
                                          message: "End of Quiz. Do you want to start over?",
                                          preferredStyle: .alert)
            let restartAction = UIAlertAction(title: "Restart",
                                              style: .default,
                                              handler: { action in
                                              self.shuffleQuestions() } )
            alert.addAction(restartAction)
            present(alert, animated: true, completion: nil)
      return
    }
    showQuestion()
  } else {
    // wrong
    let alert = UIAlertController(title: "Wrong",
                                  message: "You failed",
                                  preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Dismiss",
                                  style: .cancel,
                                  handler: nil))
    present(alert, animated: true)
  }
}`

CodePudding user response:

If it is the last question and the answer is correct bump score and assign it to the label:

if questionNumber > questions.count {
            score  = 1
            scoreLabel.text = "Score: \(score)"
            let alert = UIAlertController(title: "Awesome",
                                          message: "End of Quiz. Do you want to start over?",
                                          preferredStyle: .alert)
            let restartAction = UIAlertAction(title: "Restart",
                                              style: .default,
                                              handler: { action in
                                              self.shuffleQuestions() } )
            alert.addAction(restartAction)
            present(alert, animated: true, completion: nil)
      return
    }
  • Related