Q:

Write a Ruby program to count the number of 5's in a given array

0

Write a Ruby program to count the number of 5's in a given array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def array_count(array)
   count = 0
    array.each{|item| count += 1 unless item != 5}
    return count
end

print array_count([1, 2, 9]),"\n"
print array_count([1, 2, 5, 9]),"\n"
print array_count([1, 2, 5, 9, 5])

Output:

0
1
2

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now