I am working on a quiz project and trying to convert an array of bools to an array of strings so that I can present the user with their answers. I have tried to enumerate the question array and pull out all the users answers and the correct answers and append them to a new array.
users answers go to userAnswers
correct answers go to correctAnswers
Then it calls a function checkResults that will check how many answers the user got wrong or right.
This is where I got stuck. I used a map function to compare the 2 arrays and give me a new array back called answer. If the answers matched the it would return true and if they didn`t it would return false. I am not entirely sure how to convert the results inside answer to an array of strings to present to the user.
// My Arrays
class QuizQuestion {
let question: String!
let answer: Bool!
let explanation: String!
var usersAnswer: Bool?
init(question: String, answer: Bool, explanation: String) {
self.question = question
self.answer = answer
self.explanation = explanation
}
}
let questions = [
QuizQuestion(question: "Do I like coffee?", answer: true, explanation: "Because it's awesome!"),
QuizQuestion(question: "Is bacon god's gift to mankind?", answer: true, explanation: "Because it's awesome!"),
QuizQuestion(question: "Should I take a nap right now?", answer: true, explanation: "You gotta review some code!"),
]
var userAnswers: [Bool] = []
var correctAnswers: [Bool] = []
// Submit Functions
func checkResults() {
let answer = map(zip(correctAnswers, userAnswers)){$0.0 == $0.1}
var finalResults = answer.map({"The following answer is \($0)"})
}
@IBAction func submitAll(sender: UIButton) {
let hasAnsweredAllQuestions = questions.reduce(true) { (x, q) in x && (q.usersAnswer != nil) }
println("has user answered all questions?: \(hasAnsweredAllQuestions)")
for (index, _) in enumerate(questions) {
userAnswers.append(questions[index].usersAnswer!)
correctAnswers.append(questions[index].answer)
}
if correctAnswers.count == 3 {
checkResults()
}
}