Q:

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

0

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.

All Answers

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

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]

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