# [Ruby] Count an even number in an array using the even? Method

Create a method that counts and outputs an even number from the values in the array.
Use the ʻeven? Method` to determine if the value is even.

Output example:
count_evens([2, 1, 4, 6]) → 3

## Answer 1

```
def count_evens(nums)
count = 0
nums.each do |num|
if num.even?
count += 1
end
end
puts count
end
```

- Prepare a
`variable count`

to store an even number
- Use each method to extract one by one from the array and use ʻeven? Method` to determine whether it is an even number.
- If even, add 1 to
`count`

- Repeat this and output
`count`

at the end

## Answer 2

It's a bit confusing compared to Answer 1, but ...

```
def count_evens(nums)
result = []
nums.each do |num|
if num.even?
result << num
end
end
puts result.length
end
```

`result = []`

is an array to hold even values
- As with Answer 1, use the each method to extract the array one by one and use the ʻeven? Method` to determine whether it is an even number.
- If it is an even number, use
`result << num`

to put the even number in the array.
- Finally,
`result.length`

outputs how many elements are in the array (that is, even numbers in the array here).

## bonus

ʻOdd? Method` to determine if it is odd