 # Module 2: Lesson 4 Challenge

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
{
List(randomArray, id: \.self){
arrayElement in Text(String(arrayElement))
}

HStack{
Spacer()
Button("Generate") {
}
Spacer()
Button("Increase") {
var a = 0
while(a <= (randomArray.count-1))
{
randomArray[a] += 1
a += 1
}
}
Spacer()
Button("Clear"){
randomArray.removeAll()
}
Spacer()
}
}
}
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()
}
}

``````

Hi Sam,

Well done but the question asks you to add numbers between 1 and 10. It also says if the number you added was not a 7 then to keep adding random numbers until you add a 7 to the list.

So your code that adds random numbers should ensure that the last number added is in fact a 7 and then stop adding numbers. Consider the following code for your `func addElement()`

``````    func addElement() {
var randomNumber: Int
repeat {
randomNumber = Int.random(in: 1...10)
randomArray.append(randomNumber)
} while randomNumber != 7
}
``````

In your code that increments each array element value by 1, you might consider this method

`````` for arrayIndex in 0..<randomArray.count {
randomArray[arrayIndex] += 1
}
``````