We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Range

Go provides syntactic sugar to iterate easily over elements of a slice:

for INDEX, ELEMENT := range SLICE {
}

The element is a copy of the value at that index.

For example:

fruits := []string{"apple", "banana", "grape"}
for i, fruit := range fruits {
    fmt.Println(i, fruit)
}
// 0 apple
// 1 banana
// 2 grape

Assignment

We need to be able to quickly detect bad words in the messages our system sends.

Complete the indexOfFirstBadWord function. It takes 2 arguments:

  • msg: a single message represented as a slice of words
  • badWords: a slice of bad words

If it finds any bad words in the message it should return the index of the first bad word in the msg slice. This will help us filter out naughty words from our messaging system. If no bad words are found, return -1 instead.

Use the range keyword.

Tip

Remember, you can ignore returned values with an underscore _ instead of creating an unused variable.