TL ; DR:
A for loop over a Map visits every entry. (fruit, amount) splits each one into its key and its value.
val stock = scala.collection.mutable.Map("apples" -> 5, "pears" -> 2)
for ((fruit, amount) <- stock) {
println(s"$fruit $amount")
}
// pears 2
// apples 5 (in any order)
Full lesson:
A loop can visit every entry of a map, one at a time. The loop body runs once per key.
Mind the double parentheses. The outer pair belongs to the for. The inner pair splits the entry into the key and the value.
Loop over stock.keys to get only the keys.
Don't need the key? Write _ in its place, as in for ((_, amount) <- stock).
The order: a mutable Map has no fixed order. The entries may come in any order. A total or a count comes out the same in any order. Those are safe answers.
Your task
A game keeps each player's points in scores. The variables total and count start at 0.
Loop over scores. Add every player's points to total. Count the players with 10 points or more in count.
The loop needs only the points, not the names. The code at the bottom prints both.
Expected output:
43
2Free plan: 30 tutor messages. PRO is unlimited.
