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.
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]
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