Hi! Here is an exercise, and I can't figure out how to do it.
You were nearly there.
Problem here is that if no message contains "Caterpillar", app will crash because index will be out of bounds.
2 ways:
With while loop:
var index: Int = 0
var foundMessage: Bool = false
while !foundMessage && index < aliceMessages.count {
foundMessage = aliceMessages[index].contains("Caterpillar")
if !foundMessage {
index += 1
}
}
print(index)
Or with for
var index: Int = 0
for message in aliceMessages {
if message.contains("Caterpillar") {
print(index, message)
break // We leave the loop
}
index += 1
}