Module2: Challenge3

I was able to do part of this challenge, but am unable to generate a random word and add it to the list. Like where would I get this new word? Any advice?

//
//  ContentView.swift
//  Shared
//
//  Created by Eric Beecroft on 10/5/21.
//

import SwiftUI

struct ContentView: View {
    var array = ["Bat", "Mouse", "Moth", "Kangaroo", "Koala"]
    var body: some View {
        VStack{
            Spacer()
            List(array, id: \.self){
                animal in Text(animal)
            }
            Spacer()
            
            VStack{
                Button("Submit"){
                    
                }
            }
            Spacer()
        }
    }
}

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

Looking at the Challenge description:

Challenge

Build a UI with a List and a Button below it.

Declare a property that contains an array of 5 strings. These can be any 5 words you want.

Each time the button is tapped, choose a random word from the array and put it in the list.

The number of items in the list should grow as you tap the button.

You need an array to store the 5 words that are the source. Let’s call that one animals

You need a List which requires a separate array to display the items that are added to that separate array when the button is tapped. Lets call that array animalList

You may recall in a previous episode that Chris Ching talked about generating random numbers in a specific range.

In this case you need to generate a random number from between 0 and the number of items in the source array minus 1. Remember that the first index in an array is 0. The last index in an array is numbered 1 less that the total number of elements. So if you have 5 elements in an array the first one is 0 and the last one is 4.

So you would generate a random number from between 0 and animals.count - 1

When you have that random number, you use that as in index value on the array of animals and append that element to the array of animalList

Is that enough information to help you out rather than me giving you the answer?

I think the difficulty for me is that I am trying to wrap my head around some of the concepts. I think the string data is throwing me off as I am kind of assuming that the new array will some how generate an animal that currently doesn’t exist in the list, like an alligator for example.
Coding is kind of hard at times.

//
//  ContentView.swift
//  Shared
//
//  Created by Eric Beecroft on 10/5/21.
//

import SwiftUI

struct ContentView: View {
    var animal = ["Bat", "Mouse", "Moth", "Kangaroo", "Koala"]
    var body: some View {
        VStack{
            var rndNum = Integer[0..<animal.count]
            Spacer()
            List(arrayList, id: \.self){
                animal in Text(animal)
            }
            Spacer()
            
            VStack{
                Button("Submit"){
                    
                }
            }
            Spacer()
        }
    }
}

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

Go back to my earlier post where I break down the challenge.

You need an array that stores the animals that you randomly choose from. This is your source array.
You need an array which the List displays. This is your destination array which starts off as an empty array.

So your UI consists of a List with a Button below it so that means that the two UI objects are inside a VStack

The List displays the contents of your destination array

The Button action code chooses a random number and uses that random number to select an element from the source array and append that to the destination array.

Does that make it easier to understand?

I looked at the solution but don’t know exactly why it was written this way or how it works.

//
//  ContentView.swift
//  Module2Challenge3
//
//  Created by Eric Beecroft on 10/13/21.
//

import SwiftUI

struct ContentView: View {
    //Variables containing fruit
    var array = ["Apple", "Orange", "Banana", "Pear", "Dragon Fruit"]
    @State var items = [String]()
    
    var body: some View {
        VStack{
            List(items, id: \.self){item in
                Text(item)
            }
            Button("Tap Me"){
                let randIndex = Int.random(in: 0...array.count-1)
                items.append(array[randIndex])
            }
        }
    }
}

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

Which part of the code do you not understand?

hey chris i wanna edit menu app listing and then apply following conditions to it generate everytime i tap a list of array we made menuitems can u guide me a little bit