14 Day Challenge

I’m new here, and maybe this is a dumb question, but I cannot find the quizzes and challenges for the 14 day challenge. I have found and downloaded the “resources” zip file which contains the playground called “challenge” but I don’t see any instructions. Am I somehow supposed to know what to do?

mee too, i have the same problem. i need help. When i sign up to the 14 day challenge. The email I get is only for resources. I dont know how to access the actual challenge. When i click on resources it brings me to the dropbox page. I dont know how to get round it

The “14 Day Beginner Challenge (SwiftUI)” course can be accessed via the CWC+ website without having to have a paid account.

https://learn.codewithchris.com

You can create an account and join for free but you only have access to those courses that are free. You can see all the other courses but all you will see is the overview of each course.

As soon as you decide you want access to the other courses you choose the plan you want and you will have access to all courses.

Scroll down the CWC+ site web page to the bottom and you will see the course shown as follows.

Good luck.

Note: The other posts you created with the same message have been removed in the interests of removing duplicates

Im in the process of solving challenge on day 11 but I don’t know how to get round an error


Can i have help please

Inside your var body: some View you can only have objects that return a View

variables need to be defined before the body property (ie, just above it)

If you intend to change the value of counter then you need to declare it as a @State var since @State is a special property wrapper that allows variables inside a struct to be mutated (changed).

Hope that helps

still unlucky.

I am attempting the if statement challenge and i have these errors

@kwaku2flairy

print statements cannot be placed in amongst where variables are declared.

Inside the curly braces of var body: some View there must be a View of some type otherwise the compiler will complain. ie

var body: some View {
    Text("This is a text View")
        .padding()
}

You can’t include a struct inside the body property like you have done.

Your increase function and your decrease should be declared below the body property like this:

struct ContentView: View {
    // Variables go here including @State variables
    // @State variables can be mutated (changed) whereas a
    //  var cannot.
    @State var counter:Int = 0

    var body: some View {

        VStack {

            Text("\(counter)")

            Button {
                // Your Swift code goes here
            } label: {
                Text("Click me")
            }
        }

    }

    func increase() {
        // Your code to increase the value of counter
    }

    func decrease() {
        // Your code to decrase the value of counter
    }
}

thanks a lot your code helped no errors. but in the canvas when i interact with it nothing happens. When i click “click me” nothing happens

@kwaku2flairy

Can you post a screenshot of your entire Xcode window.

i modified code before seeing this message. im still getting errors

Let’s go back to the beginning.

The first part of the task is to Build a UI with a Text element and a Button below it.

The Text element should show a “0”.

So you need to have this:

struct ContentView: View {
    @State var counter:Int = 0

    var body: some View {

        VStack {

            Text("\(counter)")

            Button {
                // Your code to control whether the counter increases or decreases goes here
            } label: {
                Text("Click me")
            }
        }

    }
}

The task then says to Declare a method called increase which will add a random number between 1 and 10 to the number shown in the Text element

Declare a method called decrease which will subtract a random number between 1 and 10 from the number shown in the Text element

A method is a function so to declare a function you use the keyword func and follow that with a name and then an open and close parenthesis () followed by an opening brace {.

For example:

func myFunction() {

}

In your ContentView after the closing brace of the body: property you should declare the method increase() and decrease() and the code in each case to increment the counter or decrement the counter by a random amount between 1 and 10. So the ContentView code should now look like this:

struct ContentView: View {
    @State var counter:Int = 0

    var body: some View {

        VStack {

            Text("\(counter)")

            Button {
                // Your code to control whether the counter increases or decreases goes here.
            } label: {
                Text("Click me")
            }
        }

    }

    func increase() {
        let randomNumber = Int.random(in: 1...10)
        counter += randomNumber
    }

    func decrease() {
        let randomNumber = Int.random(in: 1...10)
        counter -= randomNumber
    }
}

Now comes the tricky part. The challenge goes on to say:

Initially, each time the button is tapped, call the increase method.

This will bring the number closer to 50 each time you tap it.

When the number in the Text element is over 50, then from now on you should call the decrease method each time the button is tapped.

This will cause the number to start dropping towards 0 with each button tap.

When the number in the Text element is under 0, then from now on you should call the increase method each time the button is tapped.

So what we need here is a “flag” to set that controls if we should be increasing or decreasing. This is where we can use a Boolean value like this

@State var isIncreasing = true

So now our ContentView Button code might look like this:

struct ContentView: View {
    @State var counter:Int = 0
    @State var isIncreasing = true

    var body: some View {

        VStack {

            Text("\(counter)")

            Button {
                if isIncreasing == true {
                    increase()
                } else {
                    decrease()
                }
            } label: {
                Text("Click me")
            }
        }

    }

    func increase() {
        let randomNumber = Int.random(in: 1...10)
        counter += randomNumber
    }

    func decrease() {
        let randomNumber = Int.random(in: 1...10)
        counter -= randomNumber
    }
}

The problem now is that we need to test to see what the counter value is so that we can change the direction the counter is moving. When increasing and it goes past 50 we need to change isIncreasing to false so that when we tap the button the code will call the decrease() function and when the value goes below 0 we change isIncreasing to true so that we call the increase() function.

So now the code might look like this:

struct ContentView: View {
    @State var counter:Int = 0
    @State var isIncreasing = true

    var body: some View {

        VStack {

            Text("\(counter)")

            Button {
                if isIncreasing == true {
                    increase()
                } else {
                    decrease()
                }

                if counter > 50 {
                    isIncreasing = false
                } else if counter < 0 {
                    isIncreasing = true
                }
            } label: {
                Text("Click me")
            }
        }

    }

    func increase() {
        let randomNumber = Int.random(in: 1...10)
        counter += randomNumber
    }

    func decrease() {
        let randomNumber = Int.random(in: 1...10)
        counter -= randomNumber
    }
}

I hope you understand what that all means.

yh it makes sense. Thank you

A post was split to a new topic: 14 Day Challenge - Challenge 8

A post was split to a new topic: Image added to Assets but does not show in the Canvas