# [Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!

## Overview

We will learn how to count the number of even or odd elements from an array.

## table of contents

--Practice
--Problem
--Answer (commentary)
- odd?
- odd? + count

--Summary
--References

odd?

This method determines whether the target number is odd. If it is odd, it returns true, otherwise it returns false.

even?

This method determines whether the target number is even. Returns true if it is even, false otherwise.

Array#count

Returns the number of elements in the array.

## Practice

#### problem

Count an odd number from the values in the array and output!

#### Answer (commentary)

odd?

`answer`

```
def count_odds(nums) #Array receipt
count = 0 #Define a variable count to put the counted number
nums.each do |num| #Extract elements from the array one by one
if num.odd? #Is the extracted value an odd number?
count += 1 #If the value is odd, add 1 to the variable count
end
end
p count #Output count when each process is completed
end
#Method call
count_odds([3, 5, 2, 2, 1])
count_odds([3, 1, 0])
count_odds([2, 4, 6])
#Terminal output result
# 3
# 2
# 0
```

odd? + count
It's even easier to write than the above.

`Another solution`

```
def count_odds(nums)
p nums.count { |num| num.odd? }
end
#Method call
count_odds([3, 5, 2, 2, 1])
count_odds([3, 1, 0])
count_odds([2, 4, 6])
#Terminal output result
# 3
# 2
# 0
```

## Summary

--odd? Is a method that returns true if the target number is odd, false otherwise
--even? Is a method that returns true if the target number is even, false otherwise
--The number of elements can be returned by using the count method on the array.

## References

-Ruby 3.0.0 Reference Manual (odd?)
-Ruby 3.0.0 Reference Manual (even?)
-Ruby 2.7.0 Reference Manual (count)