0

My following code displays a question and its matching answer by making an array of keys and values from a dictionary and making sure both arrays have the same index that is pulled. However after a question and its answers are randomly selected I want to remove them permanently from their respective arrays until all the other items in the array have been displayed. Simply put, how can I make sure a question and set of answers are not shown again until all the items are shown or until the user gets an answer wrong? As of now my code only makes sure the same random index isn't chosen twice in a row when I want to make sure it isn't shown twice at all.

Here is my code (swift for iOS):

func randomQuestion() {


    //random question
    var questionList = Array(QADictionary.keys)
    var rand = Int(arc4random_uniform(UInt32(questionList.count)))
    questionLabel.text = questionList[rand]




    //matching answers
    var answerList = Array(QADictionary.values)
    var choices = answerList[rand]




        rightAnswerBox = arc4random_uniform(4)+1

        //create button
        var button:UIButton = UIButton()
        var x = 1



        for index in 1...4
        {

            button = view.viewWithTag(index) as! UIButton

            if (index == Int(rightAnswerBox))
            {
                button.setTitle(choices[0], for: .normal)

            }

            else {
                button.setTitle(choices[x], for: .normal)
                x += 1

            }

            func removePair() {
                questionList.remove(at: rand)
                answerList.remove(at: rand)


            }


            randomImage()

        }
    }


let QADictionary = ["Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"], "What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"], "Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]]

1 Answer 1

1

Move your questionList to a property of the viewController so that it stays around from one call of randomQuestion to the next. Reload it only when it is empty.

Use the dictionary to find the matching answers instead of using a second array.

var questionList = [String]()

func randomQuestion() {

    //if no more questions, reload the questionList
    if questionList.isEmpty {
        questionList = Array(QADictionary.keys)
    }

    var rand = Int(arc4random_uniform(UInt32(questionList.count)))
    questionLabel.text = questionList[rand]

    //matching answers
    var choices = QADictionary[questionList[rand]]!

    questionList.remove(at: rand)

    rightAnswerBox = arc4random_uniform(4)+1

    //create button
    var button:UIButton = UIButton()
    var x = 1

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

        if (index == Int(rightAnswerBox))
        {
            button.setTitle(choices[0], for: .normal)

        }
        else {
            button.setTitle(choices[x], for: .normal)
            x += 1

        }

        func removePair() {
            questionList.remove(at: rand)
        }

        randomImage()
    }
}
Sign up to request clarification or add additional context in comments.

12 Comments

this code still does not suffice for what I'm trying to accomplish. I'm trying to make sure once a question from the array is asked it isn't asked again
Did you try it? You might need to move where you call questionList.remove(at: rand) to the appropriate place. Do you want to remove the question immediately upon choosing it, or only when they get it right?
Yes when I wrote the code like you said I got an error under the matching answer comment for the choices variable that said "cannot call value of non-function type '[String : Array<String>]'"
I want to remove immediately upon choosing it because once any question is answered wrong I want the app to reset and all the asked question to be placed back into the array
Remove the question right after getting the choices. I updated the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.