Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to append the value 5 to the slice.
Go
numbers := []int{1, 2, 3, 4}
numbers = append(numbers, [1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around 5, which makes it a string instead of an integer.
Trying to append the slice itself instead of a value.
✗ Incorrect
The append function adds the value 5 to the slice numbers.
2fill in blank
mediumComplete the code to append all elements from slice b to slice a.
Go
a := []int{1, 2}
b := []int{3, 4}
a = append(a, [1]...)
fmt.Println(a) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the slice without the spread operator, which causes a compile error.
Appending the wrong slice.
✗ Incorrect
Using b... appends all elements from slice b to slice a.
3fill in blank
hardFix the error in the code to append the value 10 to the slice.
Go
var nums []int nums = append([1], 10) fmt.Println(nums)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
nil instead of the slice variable as the first argument.Trying to append to the value 10.
✗ Incorrect
You must append to the slice variable nums, not to nil or other values.
4fill in blank
hardFill both blanks to create a new slice with squares of numbers greater than 3.
Go
nums := []int{1, 2, 3, 4, 5}
squares := []int{}
for _, n := range nums {
if n [1] 3 {
squares = append(squares, n[2]n)
}
}
fmt.Println(squares) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than in the condition.
Using addition instead of multiplication for squaring.
✗ Incorrect
The condition n > 3 filters numbers greater than 3, and n * n calculates their squares.
5fill in blank
hardFill all three blanks to create a map of numbers and their squares for numbers less than 5.
Go
nums := []int{1, 2, 3, 4, 5}
squares := map[int]int{}
for _, n := range nums {
if n [1] 5 {
squares[[2]] = n[3]n
}
}
fmt.Println(squares) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than instead of less than in the condition.
Using addition instead of multiplication for the square.
Using the wrong key in the map.
✗ Incorrect
The condition n < 5 filters numbers less than 5, n is the key, and n * n is the value in the map.