First, since you are not doing any special initialization within your init
you can remove it. Swift struct
have a default initializer.
Second, you need to change your MathProblem
. You don't need it to store a table, or an array of problem and answer. Each MathProblem has one problem (or text) and one answer. This will help you in the setup, and making the app work as intended. So it becomes:
struct MathProblem {
var problem: String
var answer: String
}
Third, you have 2 functions, one to populate problems, and one for answers, but by doing the above, we can now store all the problems in a [MathProblem]
array. Which means we can bundle them inside one function which we can declare within ContentView. This function will need to return an array of MathProblem:
func createTimesTable(for a: Int) -> [MathProblem] {
var problem: String
var product: String
var questions = [MathProblem]()
for i in 1...12 {
problem = "\(a) x \(i)"
product = "\(a * i)"
questions.append(MathProblem(problem: problem, answer: product))
}
return questions
}
Also note, this way when we call the function it reads nicely: createTimesTable(for: 5)
side note: You could also use an enum outside of ContentView if you prefer (in which case you declare the function as static
)
So really, just declare the function inside ContentView and you're set. Now what you will need inside Content view, is a property to store this newly created times table array.
And just a quick show of how you can use them:
// Just An Example For Show
let tenTimesQuestions = createTimesTable(for: 10)
let testQuestion = tenTimesQuestions[2]
print("Question: \(testQuestion.problem), Answer: \(testQuestion.answer)")
I hope this helps you figure out the rest of the challenge.