Q:

Write a Ruby program to create a new array of length 2 containing the middle two elements

0

Write a Ruby program to create a new array of length 2 containing the middle two elements from a given array of integers of even length 2 or more.

All Answers

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

def check_array(nums)
    midArr = []
   	half = nums.length/2;
	midArr[0] = nums[half-1];
	midArr[1] = nums[half];
	return midArr;
end
print check_array([1, 3, 5, 4]),"\n"
print check_array([11, 3, 5, 21, 0, -4])  

Output:

[3, 5]
[5, 21]

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