0
0
Goprogramming~10 mins

Appending to slices 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 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'
Aappend
B"5"
Cnumbers
D5
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.
2fill in blank
medium

Complete 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'
Aa
Bb
Cappend
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the slice without the spread operator, which causes a compile error.
Appending the wrong slice.
3fill in blank
hard

Fix 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'
Anums
B10
Cappend
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using nil instead of the slice variable as the first argument.
Trying to append to the value 10.
4fill in blank
hard

Fill 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'
A>
B*
C<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than in the condition.
Using addition instead of multiplication for squaring.
5fill in blank
hard

Fill 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'
A<
Bn
C*
D+
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.