Kodewick
Start learning →
Basic←→0 / 70

Songs that fit

#045BasicFunctionsSlices4 min

lengths has each song's length in seconds, and fits is a function: give it one song's length and it says whether that song fits (true / false). Write CountSongs(lengths, fits) to return how many songs fit.

Examples
CountSongs([]int{180, 240, 300}, func(n int) bool { return n <= 240 })→2
CountSongs([]int{}, func(n int) bool { return n <= 240 })→0
The functionfunc CountSongs(lengths []int, fits func(int) bool) int
main.go
package main

func CountSongs(lengths []int, fits func(int) bool) int {
	count := 0
	for _, s := range lengths {
		// your code here: if fits says this song fits, add 1 to count
	}
	return count
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.