Kodewick
Start learning →
Basic←→0 / 70

Arcade coins

#022BasicLoopsConditions5 min

At the arcade you play the levels in order; costs says how many coins each one takes, and you have coins. As soon as you can't pay for a level you stop, and skip the rest too. Write LevelsPlayed(costs, coins) to return how many levels you played.

Examples
LevelsPlayed([]int{3, 5, 4}, 10)→2
LevelsPlayed([]int{2, 2}, 10)→2
The functionfunc LevelsPlayed(costs []int, coins int) int
main.go
package main

func LevelsPlayed(costs []int, coins int) int {
	played := 0
	for _, c := range costs {
		// your code here: not enough coins for this level? break
		coins -= c
		played++
	}
	return played
}
Ctrl + Enter runs too
Result

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