Learn Courses My Dashboard

Need help with this code

im making a random lotto generator. the first five numbers are 1-70(green) and one number is 1-25(yellow). when i click the add button it suppose to populate all the numbers. but i can’t seem to get the yellow to work the same way as the green ones. please give me some advise. thanks you.

@mahesh303

Hi Mahesh,

Welcome to the community.

Can you paste your code in as text please, rather than providing a screenshot.

Place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code so that it is formatted nicely. The back-tick character is located on the same keyboard key as the tilde character ~ (below the Esc key).

This also makes it easier for anyone assisting as they can copy the code and carry out some testing rather than having to type the code in from an image.

@mahesh303, please do post code as Chris suggests when looking for help. It is so much easier that way and it encourages more people to help if they can more easily test solutions.

In this case, though, I can see what your problem is without needing to run or test anything: When you clear the numbers, nothing gets displayed for fiveNumbers because it is an array that you can empty, but megaBall shows 0 because it’s an Int that always has a value.

You can fix this by making megaBall an Int? that gets set to nil initially and when you tap the Clear button. Then wrap the Text where you display the value of megaBall in an if let statement so that it only gets displayed if megaBall is not nil. Something like this:

Your @State var:

@State private var megaBall: Int?

In the body:

if let megaBall = megaBall {
    Text("\(megaBall)")
        //all of your modifiers
}

Then in your Clear button:

megaBall = nil

Also, not sure if this is intentional or not, but you are setting the value of megaBall five times in the Add button code, since it’s inside your loop. Here’s a better (and faster) way to set your numbers without needing a loop:

fiveNumbers = (1...5).map { _ in Int.random(in: 1..<70) }
megaBall = Int.random(in: 1..<25)

Hey sorry guys, I was out of town and couldn’t replay. Here is the code for what I was trying to do.

//
// ContentView.swift
// Lotto
//
// Created by Mahesh Patel on 6/29/21.
//

import SwiftUI

struct ContentView: View {

@State var fiveNumbers = [Int]()
@State var megaBall: Int

var body: some View {

    VStack{
        Text("number")

        ScrollView(.horizontal){
            HStack{

                ForEach(fiveNumbers, id: \.self){ item in
                    Text("\(item)")
                        .frame(width: 45, height: 45)
                        .background(Color(.green))
                        .clipShape(Circle())
                        .padding(5)
                        .shadow(color: Color(red: 0, green: 0, blue: 0, opacity: 0.6), radius: 2, x: 1, y: 1.6)
                }
                Text("\(megaBall)")
                    .frame(width: 45, height: 45)
                    .background(Color(.yellow))
                    .clipShape(Circle())
                    .padding(5)
                    .shadow(color: Color(red: 0, green: 0, blue: 0, opacity: 0.6), radius: 2, x: 1, y: 1.6)

            }
        }

Spacer()
HStack{
Button(action: {

            var randNum = 0
            var ranMega = 0

            while fiveNumbers.count != 5 {
            for index in 0..<5 {
                randNum = Int.random(in: 1..<70)
                ranMega = Int.random(in: 1..<25)

                fiveNumbers.insert(randNum, at: index)

                megaBall = ranMega

            }
            }

        }, label: {
            Text("Add")
        })

        Button(action: {
            fiveNumbers.removeAll()

        }, label: {
            Text("Clear")
        })
    }
    }

}

}

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

Just quickly eyeballing it, that looks like the same code you originally posted. Did you try the solution I posted last week?

Also, as Chris mentioned, please post your code wrapped in backticks ``` to make it easier to read, among other benefits.