[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

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

bonus

ʻOdd? Method` to determine if it is odd

Recommended Posts

[Ruby] Count an even number in an array using the even? Method
Examine the elements in the array using the [Ruby] includes? Method
[Ruby] Obtaining even values ​​using the even? Method
[Ruby] How to count even or odd numbers in an array
Arbitrary search method in an array using binary search
Count the number of occurrences of a string in Ruby
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
[Ruby] Searching for elements in an array using binary search
How to change a string in an array to a number in Ruby
How to retrieve the hash value in an array in Ruby
Map without using an array in java
[Ruby] Cut out a string using the slice method
Calculate the difference between numbers in a Ruby array
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
The permission specification of the FileUtils method is an octal number.
A program (Java) that outputs the sum of odd and even numbers in an array
[Ruby] Questions and verification about the number of method arguments
How to specify an array in the return value / argument of the method in the CORBA IDL file
I want to call a method and count the number
Multiplication in a Ruby array
Display weather forecast using OpenWeatherMap, an external API in Ruby
Count the number of digits after the decimal point in Java
Try using gRPC in Ruby
[Ruby] How to batch convert strings in an array to numbers
About the method to convert a character string to an integer / decimal number (cast data) in Java
[Java] How to search for a value in an array (or list) with the contains method
[Ruby] I want to put an array in a variable. I want to convert to an array
How to output the value when there is an array in the array
Sorting hashes in a Ruby array
[Ruby] Method to count specific characters
Java comparison using the compareTo () method
[Ruby] Extracting double hash in array
String output method memo in Ruby
Hanachan in Ruby (non-destructive array manipulation)
[Rails 6] destroy using the resources method
[Ruby] Search problem using index method
[Ruby] undefined method `dark?'occurs in rqr_code
Call the super method in Java
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Swift] How to set an image in the background without using UIImageView.
[Ruby] I want to make an array from a character string with the split method. And vice versa.
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
Try using the Stream API in Java
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
[Ruby] From the basics to the inject method
Implemented "Floyd Cycle Detection Method" in Ruby
Organized memo in the head (Java --Array)
Compare the elements of an array (Java)
What is the main method in Java?
[Ruby] Get unique elements from an array
Get the error message using the any? method
Sort an array of Ruby homebrew classes
Try adding text to an image in Scala using the Java standard library
Use hashes well in Ruby to calculate the total amount of an order
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2
If the parameter is an array, how to include it in Stopara's params.permit
Call a method of the parent class by explicitly specifying the name in Ruby
[Ruby] How to calculate the total amount using the initialize method and class variables
How to make a judgment method to search for an arbitrary character in an array