Q:

Write a Ruby program to create a new array of length 3 containing the elements from the middle of a given array of integers of odd length (at least 3)

0

 Write a Ruby program to create a new array of length 3 containing the elements from the middle of a given array of integers of odd length (at least 3).

All Answers

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

 
def check_array(nums)
    halfArr = [];
	half = nums.length/2;
	halfArr[0] = nums[half-1];
	halfArr[1] = nums[half];
	halfArr[2] = nums[half+1];
	return halfArr;
end

print check_array([1, 3, 4]),"\n"
print check_array([1, 2, 3, 7, 9])  

Output:

[1, 3, 4]
[2, 3, 7]

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