I'm looking to create a function that returns a math equation that can be preformed in ones head (Clearly thats a bit subjective but I'm not sure how else to phrase it).
I want to use the operations +,-,*,/,% and ().
Currently I have this function:
func createMathString() -> String{
let firstNum = Int(arc4random_uniform(300))
let secNum = Int(arc4random_uniform(300))
let exp = Int(arc4random_uniform(4))
print("\(firstNum) \(self.expressions[exp]) \(secNum)")
return "\(firstNum) \(self.expressions[exp]) \(secNum)"
}
where self.expression is an array that contains +,-,*,/.
This function returns a string which is then interpreted using this function:
let question = createMathString()
let mathExpression = NSExpression(format: question)
let mathValue = mathExpression.expressionValue(with: nil, context: nil) as? Int
My problems:
1) Division and Multiplication get difficult as numbers get higher
2) I'm not sure had to add the ( ). (Not every problem will consist of them, depends on the number of terms.
3) I want the questions to be easy enough to be completed in someones head but not to easy that I'm reducing the random numbers to 0-50.
I looked for a possible API but could not find one that suited my needs.