Q:

Write a Ruby program to check whether one of the first 5 elements in a given array of integers is a 7

0

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. 

All Answers

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

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

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