Write a Ruby program to create a new array with the first element of two arrays. If length of any array is 0, ignore that array.
def check_array(a, b) front = [] if(a.length > 0 && b.length > 0) front[0] = a[0] front[1] = b[0] elsif (a.length > 0) front[0] = a[0] elsif (b.length > 0) front[0] = b[0] end return front end print check_array([3, 4, 5, 6], [7, 3, 4]),"\n" print check_array([3, 4, 5], [6, 7, 3, 4, 7]),"\n" print check_array([3, 4], [])
Output:
[3, 7] [3, 6] [3]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer