Here goes the Solution of, 90 day challenge Module2: Lesson 4 Question,
I spend at least 3-4 hours to figure out this code, fully fascinated by the question,
I want to ask, is this speed good? are there any recommendation to be made in code?
//
// ContentView.swift
// Module2: Lesson4 Challenge
//
// Created by Sam Grover on 19/05/22.
//
import SwiftUI
struct ContentView: View {
var array = [1,2,3,4,5,6,7,8,9,10]
@State var randomArray = [Int]()
var body: some View {
VStack
{
NavigationView{
List(randomArray, id: \.self){
arrayElement in Text(String(arrayElement))
}.navigationBarTitle(Text("My List"))
}
HStack{
Spacer()
Button("Generate") {
addElement()
}
Spacer()
Button("Increase") {
var a = 0
while(a <= (randomArray.count-1))
{
randomArray[a] += 1
a += 1
}
}
Spacer()
Button("Clear"){
randomArray.removeAll()
}
Spacer()
}
}
}
func addElement(){
var ongoing = true
while(ongoing == true)
{
let randomIndex = Int.random(in: 0...9)
randomArray.append(array[randomIndex])
if randomIndex == 6 {
ongoing = false
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}