I’ve been searching everywhere for the solution but no luck. For the lesson 12 of the beginner challenge, am I supposed to use if statement to achieve the goal? The code I written is as below, the problem is, I couldn’t figure out what is the right condition to make sure the number decreases to something below 0. It starts with 0 and increases to 50s, and then it decrease to something less then 50 and the first condition is matched, so it goes back to increase again.
I checked the video of Lesson 12 several times, to make sure I did not missed anything there, still no clue.
Please help, thanks.
//
// ContentView.swift
// CWC_Tests
//
// Created on 4/28/21.
//
import SwiftUI
struct ContentView: View {
@State var currentNumber = 0
var body: some View {
VStack {
Text(String(currentNumber))
Button(action: {
if currentNumber < 50 {
increase()
}
else if currentNumber > 50 {
decrease()
}
}, label: {
Text("Increase")
})
}
}
func increase() {
let randomNum = Int.random(in: 1...10)
currentNumber += randomNum
}
func decrease() {
let randomNum = Int.random(in: 1...10)
currentNumber -= randomNum
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}