Complete the code to return the first even number from the slice.
func firstEven(nums []int) int {
for _, num := range nums {
if num%2 == 0 {
return [1]
}
}
return -1
}The function returns the first even number found in the slice by returning num inside the loop.
Complete the code to return true if any number in the slice is negative.
func hasNegative(nums []int) bool {
for _, n := range nums {
if n < 0 {
return [1]
}
}
return false
}The function returns true immediately when it finds a negative number inside the loop.
Fix the error in the code to return the index of the first zero in the slice.
func firstZeroIndex(nums []int) int {
for i, val := range nums {
if val == 0 {
return [1]
}
}
return -1
}The function should return the index i where the first zero is found, not the value.
Fill both blanks to return the sum of positive numbers only.
func sumPositive(nums []int) int {
sum := 0
for _, n := range nums {
if n [1] 0 {
sum += [2]
}
}
return sum
}The function checks if a number is greater than zero and adds it to sum.
Fill all three blanks to create a function that returns the first string longer than 3 characters.
func firstLongString(words []string) string {
for _, w := range words {
if len([1]) [2] [3] {
return w
}
}
return ""
}The function checks the length of each word and returns the first one longer than 3 characters.