Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to exit the loop when i equals 3.
Go
for i := 1; i <= 5; i++ { if i == 3 { [1] } fmt.Println(i) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 'continue' instead of 'break' causes the loop to skip the current iteration but not exit.
Using 'return' inside the loop exits the function, not just the loop.
โ Incorrect
The break statement exits the nearest enclosing loop immediately.
2fill in blank
mediumComplete the code to stop the loop when the character 'x' is found in the string.
Go
str := "golang" for _, ch := range str { if ch == '[1]' { break } fmt.Printf("%c", ch) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Choosing a character that is in the string but not intended to stop the loop.
Using double quotes instead of single quotes for a rune.
โ Incorrect
The loop breaks when the character 'x' is found, stopping the printing.
3fill in blank
hardFix the error in the loop to break when the number is greater than 10.
Go
nums := []int{5, 8, 12, 3}
for _, num := range nums {
if num [1] 10 {
break
}
fmt.Println(num)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using '<=' breaks the loop too early or too late.
Using '==' only breaks if the number equals 10 exactly.
โ Incorrect
The condition should check if the number is greater than 10 to break the loop.
4fill in blank
hardFill both blanks to create a map of squares for numbers greater than 2.
Go
squares := map[int]int{}
for i := 1; i <= 5; i++ {
if i [1] 2 {
squares[i] = i [2] i
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using '<' instead of '>' causes wrong numbers to be squared.
Using '**' is invalid in Go for exponentiation.
โ Incorrect
The loop adds squares of numbers greater than 2 using the multiplication operator.
5fill in blank
hardFill the blanks to create a map of uppercase keys and values greater than 0.
Go
import "strings" data := map[string]int{"a": 1, "b": 0, "c": 3} result := map[string]int{} for k, v := range data { if v [1] 0 { result[[2]] = v } } fmt.Println(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using '>=' includes zero values, which is not intended.
Using 'k' as key keeps keys lowercase.
โ Incorrect
The loop adds entries with values greater than 0, using uppercase keys.