Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the length of the array.
Go
package main import "fmt" func main() { arr := [5]int{1, 2, 3, 4, 5} fmt.Println(len([1])) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' instead of 'arr'.
Passing a number instead of the array variable.
Using 'len' as the argument inside len().
✗ Incorrect
The len() function returns the length of the array. We pass the array variable 'arr' to len().
2fill in blank
mediumComplete the code to store the length of the array in a variable.
Go
package main import "fmt" func main() { nums := [3]int{10, 20, 30} length := [1](nums) fmt.Println(length) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cap' which returns capacity, not length.
Using undefined functions like 'size' or 'count'.
✗ Incorrect
The built-in function len() returns the length of the array.
3fill in blank
hardFix the error in the code to correctly print the length of the array.
Go
package main import "fmt" func main() { values := [4]int{5, 6, 7, 8} fmt.Println([1](values)) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' or 'length' which are not built-in functions in Go.
Trying to access a property like values.length which does not exist.
✗ Incorrect
The correct function to get the length of an array in Go is len().
4fill in blank
hardFill both blanks to create a map with words as keys and their lengths as values, but only for words longer than 3 characters.
Go
package main import "fmt" func main() { words := []string{"go", "code", "play", "fun"} lengths := map[string]int{ [1]: [2], } fmt.Println(lengths) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using words shorter than or equal to 3 characters.
Not using len() to get the length.
✗ Incorrect
We add the word "code" as key and its length len("code") as value. Only words longer than 3 characters are included.
5fill in blank
hardFill all three blanks to create a map of words to their lengths, including only words longer than 2 characters.
Go
package main import "fmt" func main() { words := []string{"go", "play", "fun", "code"} lengths := map[string]int{ [1]: [2], [3]: len("code"), } fmt.Println(lengths) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including words that are too short.
Not matching keys with their correct lengths.
✗ Incorrect
We add "play" with its length len("play") and "code" with its length len("code") is already given. "go" is excluded because it is not longer than 2 characters.