0
0
Goprogramming~10 mins

Return values 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 return the sum of two integers.

Go
func add(a int, b int) int {
    return a [1] b
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to return the result.
2fill in blank
medium

Complete the code to return the length of a string.

Go
func length(s string) int {
    return [1](s)
}
Drag options to blanks, or click blank then click option'
Alength
Bsize
Clen
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent functions like size or count.
Trying to access string length as a property.
3fill in blank
hard

Fix the error in the function to return the product of two floats.

Go
func multiply(x float64, y float64) float64 {
    return x [1] y
}
Drag options to blanks, or click blank then click option'
A+
B*
C/
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or division instead of multiplication.
Syntax errors by missing operator.
4fill in blank
hard

Fill both blanks to return a map with words as keys and their lengths as values, only for words longer than 3 characters.

Go
func wordLengths(words []string) map[string]int {
    lengths := make(map[string]int)
    for _, word := range words {
        if [1](word) [2] 3 {
            lengths[word] = [1](word)
        }
    }
    return lengths
}
Drag options to blanks, or click blank then click option'
Alen
B>
C<
Dcap
Attempts:
3 left
💡 Hint
Common Mistakes
Using cap instead of len.
Using < instead of >.
Not calling len as a function.
5fill in blank
hard

Fill all three blanks to return a new map with uppercase keys and values only for entries with values greater than 10.

Go
func filterAndUppercase(data map[string]int) map[string]int {
    result := make(map[string]int)
    for [1], [2] := range data {
        if [2] [3] 10 {
            result[strings.ToUpper([1])] = [2]
        }
    }
    return result
}
Drag options to blanks, or click blank then click option'
AToUpper
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up key and value variable names.
Using wrong comparison operators.
Forgetting to call ToUpper as a function.