Lesson 12 Challenge of 14 Day Beginner Challenge

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()
}
}

hmm how does it decrease? i don’t even see a “decrease” function inside the button action there

of course it will also be possible to go more than 50 or go into the negative since you are decreasing or increasing a random number between 1 to 10

btw, you posted this in the wrong section you should post it in either app category or course students category

Thanks for the reply, seems I’ve pasted the wrong code and I edited with the new code.

To update, I’ve found the solution in the dropbox, thanks again.

1 Like