Reputation: 1
I'm making an app in Xcode where a screen comes up asking you to multiply 2 numbers, and then you put the product in a text field and if the answer is right there will a button that is gray will turn blue.
The problem is is that it wont work even if I'm sure of my answer while testing the app it wont work, the button will stay grey and the app will think the answer is wrong, please anyone help me and tell me what is wrong with my code!
var firstNumber = arc4random()
var secondNumber = arc4random()
@IBOutlet weak var AnswerButton: UIButton!
@IBOutlet weak var OutcomeNumber: UILabel!
@IBOutlet weak var textField: UITextField!
@IBAction func textFieldEditingDidChange(_ sender: Any) {
print("textField: \(textField.text)")
if ValidatePassword(text: textField.text!) {
AnswerButton.isEnabled = true
} else {
AnswerButton.isEnabled = false
}
}
@IBAction func AnswerButtonPressed(_ sender: Any) {
print("you may enter")
view.backgroundColor = UIColor.green
}
func ValidatePassword(text: String) -> Bool {
var result = false
if text == ("(\(firstNumber*secondNumber))") {
result = true
}
return result
}
@IBAction func GObutton(_ sender: Any) {
OutcomeNumber.text = "\(firstNumber)×\(secondNumber)"
firstNumber = arc4random_uniform(10)
secondNumber = arc4random_uniform(10)
}
@IBAction func GoMedium(_ sender: Any) {
OutcomeNumber.text = "\(firstNumber)×\(secondNumber)"
firstNumber = arc4random_uniform(50)
secondNumber = arc4random_uniform(50)
}
@IBAction func GoHard(_ sender: Any) {
OutcomeNumber.text = "\(firstNumber)×\(secondNumber)"
firstNumber = arc4random_uniform(100)
secondNumber = arc4random_uniform(100)
}
override func viewDidLoad() {
super.viewDidLoad()
AnswerButton.isEnabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Upvotes: 0
Views: 501
Reputation: 13283
Mhergon is right. The problem is your function that validates the answer. Your code structure and the way you compute and accepts the answer is not in the right way. But looks good for a starter. Anyways, take a look at your ValidatePassword
code.
Try your code in the PlayGround of Xcode.
Take a look at this example taken from your code, just different variables:
let x = 3
let y = 4
print("(\(x) * \(y))")
That prints "(3 * 4)"
Now, change the print to this:
print("(\(x*y))")
It now prints the correct computation and answer which is 12.
EDIT
You are answering the wrong question. Generate the random numbers before printing them out to the user.
You can check this sample I made. To check for shorter codes as well. :)
https://github.com/glennposadas/TextFieldGame-iOS
Upvotes: 0
Reputation: 1678
Maybe the problem is on this part:
func ValidatePassword(text: String) -> Bool {
var result = false
if text == "(\(firstNumber) * \(secondNumber))" {
result = true
}
return result
}
Between *
and \(secondNumber)
are two spaces. When you try with text (1 * 3)
this function returns false
but with (1 * 3)
returns true
On the other hand, is best to use Regular Expressions
to evaluate this.
Upvotes: 2