Ruby Basic Exercises: Check whether one of the first 5 elements in a given array of integers is a 7
Ruby Basic: Exercise-42 with Solution
Write a Ruby program to check whether one of the first 5 elements in a given array of integers is a 7. The array length may be less than 5.
Ruby Code:
def array_count(array)
ctr = 0
array.each{|item| ctr += 1 unless item != 7}
return ctr
end
print array_count([1, 2, 6, 4, 9]),"\n"
print array_count([1, 2, 5, 7, 9]),"\n"
print array_count([0, 2, 5, 7])
Output:
0 1 1
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to count the number of 5's in an given array.
Next: Write a Ruby program to check whether the sequence of numbers 10, 20, 30 appears anywhere in a given array of integers.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/ruby-exercises/basic/ruby-basic-exercise-42.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics