Learn Courses My Dashboard

Using 2D arrays with Module 1 Lesson 12 Challenge

Module 1 Lesson 12 Challenge

I just recently got CWC+ and started right away with the modules, I have a bit of programming experience with Javascript, HTML & CSS so a lot of concepts were rather easy to understand. That being said the course so far has helped greatly with transferring my skills to Swift & SwiftUI. Once I got to Module 1 Lesson 12, I decided to experiment. The initial challenge was as follows:

Build a UI with a Text element and a Button below it.
The Text element should show a “0”.
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
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.
This cycle should repeat itself. Climb to over 50 and then drop to under 0.

I found this rather easy so I decided to add more functionality using 2d arrays. In my “new & improved” version I have it so you can add or subtract buttons, each button keeps track of it’s value independently and has a random colour. This isn’t so much a question as it is myself sharing my code with the community so that you guys can critique it and show me better ways of achieving my end result. Hopefully you might even learn something yourself.

The final code is as follows:

import SwiftUI

struct ContentView: View {
    @State private var state = [[0,0,0,Int.random(in:0...6)]];
    var colors: [Color] = [.pink,.blue,.yellow,.green,.orange,.red,.purple]
    var body: some View {
        VStack{
            Spacer()
            Text("Click Buttons To Change Value")
                .padding(.bottom)
            HStack{
                ForEach(state, id:\.self){ index in Button {
                        click(i: index[2])
                    } label: {
                        Text(String(index[0]))
                            .padding(.horizontal, 20.0)
                            .padding(.vertical,10.0)
                            .background(colors[index[3]])
                            .cornerRadius(50.0)
                            .font(.largeTitle)
                            .foregroundColor(Color.white)
                            
                    }
                }
            }
            .padding(.top)
            HStack{
                if(state.count < 4){
                    Button {
                        addButton()
                    } label: {
                        Text("+")
                    }
                }else{
                    Text("+")
                        .foregroundColor(Color.gray)
                }
                
                
                if(state.count > 1){
                    Button {
                        subButton()
                    } label: {
                        Text("-")
                    }
                }else{
                    Text("-")
                        .foregroundColor(Color.gray)
                }

            }
            
            Spacer()
        }
        
    }
    
    func addButton(){
        let rNum = Int.random(in:1...6)
        state.append([0,0,state.count,rNum])
    }
    
    func subButton(){
        state.removeLast()
    }
    
    func click(i:Int){
        if(state[i][0] > 50){
            state[i][1] = 1
        }else if(state[i][0] < 0){
            state[i][1] = 0
        }
        
        if(state[i][1] == 0){
            add(i)
        }else{
            sub(i)
        }
    }
    
    func add(_ i:Int){
        let rNum = Int.random(in:1...10)
        state[i][0] += rNum
    }
    
    func sub(_ i:Int){
        let rNum = Int.random(in:1...10)
        state[i][0] -= rNum
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Once again any critiquing is much appreciated, if you have any questions let me know.

Have a great rest of your day!