0
0
Goprogramming~10 mins

Iterating over arrays in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print each number in the array.

Go
numbers := [3]int{1, 2, 3}
for [1] := 0; i < len(numbers); i++ {
    fmt.Println(numbers[i])
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Cnum
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not used inside the loop.
Using a variable name that is not declared.
2fill in blank
medium

Complete the code to use a range loop to print each element in the array.

Go
numbers := [3]int{4, 5, 6}
for [1], value := range numbers {
    fmt.Println(value)
}
Drag options to blanks, or click blank then click option'
Aindex
Bi
C_
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared.
Trying to use only one variable without the index.
3fill in blank
hard

Fix the error in the for loop to correctly iterate over the array.

Go
arr := [3]string{"a", "b", "c"}
for i := 0; i <= len(arr); i++ {
    fmt.Println(arr[[1]])
}
Drag options to blanks, or click blank then click option'
A0
Bi-1
Ci+1
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in the loop condition causing index out of range.
Using incorrect index inside the loop.
4fill in blank
hard

Fill both blanks to create a map of words and their lengths for words longer than 3 characters.

Go
words := []string{"go", "code", "fun", "learn"}
lengths := map[string]int{word: [1] for _, word := range words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword == "go"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length.
Using the wrong condition to filter words.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words and their lengths for words with length greater than 2.

Go
words := []string{"go", "code", "fun", "learn"}
lengths := map[string]int[1]: [2] for _, word := range words if [3]
Drag options to blanks, or click blank then click option'
Astrings.ToUpper(word)
Blen(word)
Clen(word) > 2
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting words to uppercase for keys.
Using wrong length condition.
Mixing up keys and values in the map.