How can I make sure that the correct answer options are displayed in the quiz app?

I am developing a quiz application where I have a constant array of questions and answers. The answer options are related to questions with the same index as the corresponding question (for example, the first answer options in the answer array come with the first index of the question array, etc.). However, I randomize the selection of the question asked from the array of questions, but the corresponding answers do not appear with the correct question that is shown. What is the best way to find the right answer to a question that is randomly selected and displayed? Also, how can I prevent a question that has already been answered so that it is not randomly selected until the poll fails and is reset? Thank you

Here is my code:

import UIKit 

class ViewController:UIViewController {
    //random question generation function
    func randomQuestion() {
        index = Int(arc4random_uniform(UInt32(questions.count)))
        questionLabel.text = questions[index]
    }

    //costants
    let questions = ["Who is Thor half brother?", "What is the name of Thor hammer?"]
    var answers = [["Atum", "Loki", "Red Norvell", "Kevin Masterson"], ["Mjolinr", "Uru", "Stormbreaker", "Odin Staff"]]

    //variables
    var currentQuestion = 0
    var rightAnswerBox:UInt32 = 0
    var index = 0

    //Question Label
    @IBOutlet weak var questionLabel: UILabel!

    //Answer Button
    @IBAction func buttonAction(_ sender: AnyObject) {
        if (sender.tag == Int(rightAnswerBox)) {
            print ("Correct!")
        } else {
            wrongSeg()
            print ("Wrong!")
        }

        if (currentQuestion != questions.count)
        {
            newQuestion()
        }
    }

    override func viewDidAppear(_ animated: Bool)
    {
        newQuestion()
    }

    //function that displays new question
    func newQuestion()
    {
        //countdown timer section
        randomQuestion()

        rightAnswerBox = arc4random_uniform(4)+1

        //create a button
        var button:UIButton = UIButton()

        var x = 1

        for index in 1...4
        {
            //creat a button
            button = view.viewWithTag(index) as! UIButton

            if (index == Int(rightAnswerBox))
            {
                button.setTitle(answers[currentQuestion][0], for: .normal)
            } else {
                button.setTitle(answers[currentQuestion][x], for: .normal)
                x += 1
            }
        }

        currentQuestion += 1
        randomImage()
    }
+4
source share
2 answers

I would recommend using a more appropriate data structure.

Sort of:

class Question {
    var id: Int
    var questionText: String
    var answers: [String]
    var answered = false
}

Then you produce a random array. You track the index you are in, and move forward in the array until it ends.

At the end of the cycle, check which questions have not been answered, delete the answers (or create a new array) and repeat the process.

You can list an array

var questions = [Question]()

//populate the array

    for (index, question) in questions.enumerated() {
        //This returns the question as well as the current index
    }
+1
source

1. Create a structure

This allows you to ask a question and an array of answers that will be useful when shuffling later.

struct Question {
    var id: Int
    var questionText: String
    var answers: [String]
}

var example = Question(id: 1, questionText: "Foo?", answers:["1", "2", "3", "4"])
var id = example.id
var questionText = example.questionText
var answers = example.answers

2. Now randomize the array of Questions using the Fisher-Yates Shuffle

Cool, because time is O (n).

var question1 = Question(id: 1, questionText: "Foo?", answers:["1", "2", "3", "4"])
var question2 = Question(id: 2, questionText: "Bar?", answers:["1", "2", "3", "4"])
var question3 = Question(id: 3, questionText: "Baz?", answers:["1", "2", "3", "4"])
var question4 = Question(id: 4, questionText: "What is after Baz?", answers:["1", "2", "3", "4"])

var questions = [question1, question2, question3, question4]

for i in 0..<questions.count-2 {
    var rand = Int(arc4random_uniform(questions.count - i))
    var copy = questions[i]
    questions[i] = questions[rand]
    questions[rand] = copy
}

print(questions)    // shuffled
+1
source

Source: https://habr.com/ru/post/1681395/


All Articles