1

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()
     }
}
1
  • You should post how your arrays look like Commented Jun 8, 2015 at 8:03

2 Answers 2

1
func checkResults() {
    let answer = map(zip(correctAnswers, userAnswers)){
        ($0.0 == $0.1) ? "correct" : "wrong"
    }
    var finalResults = answer.map({"The following answer is \($0)"})
}

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

If you only want to "change" the bools into a string you can use map() again:

let answer  = [1,2,2]
let correct = [6,7,2]

func checkResults() {

    let answers = map(zip(correct, answer)) { $0.0 == $0.1 }
    let results = answers.map { $0 == true ? "You answered right!" : "Nope, wrong!" }

    map(results) { println($0) }
}

This prints out:

Nope, wrong!
Nope, wrong!
You answered right!

1 Comment

Thanks very much for your help! Both this and the other solution taught me a lot!

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.