Sum the evens
Inputs
values is a list of 0 to 14 whole numbers, each from -30 to 30.
Your tasks
- Create a number
totalthat starts at 0. - Use a loop over
values. Add a number tototalonly when it is even. - Complete
sumOfEvensso it returnstotal. Return it, do not print it.
Example
With values = [3, 4, 7, 10, -6], sumOfEvens returns 8.
Rules
- Zero is even.
- Negative numbers can be even too. -4 is even.
Test data
TEST DATA 1: values is [3, 4, 7, 10, -6]
TEST DATA 2: values is []
Practice focus
One pass over a list to add, count, find the biggest and the smallest. Scanning an array once and keeping a running answer is the backbone of linear-time solutions.