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"]]