Curriculum Collections Iteration with range exercise 7 · fill-word
Iteration with range
Sum a slice of ints — the productive use of for _, v := range.
The blank is the placeholder for "I don't care about the index".
TypeScript:
``ts
let sum = 0;
for (const n of nums) sum += n;
``
TypeScript reference
Fill the blanks →
About this theme
for i, v := range collection is the workhorse. Over a slice you get (index, value). Over a map you get (key, value). Over a string you get (byteIndex, rune). Over a channel you get just the value, with no index. Use _ to discard either side.