Write a method that takes an array of numbers in. Your method should
return the third greatest number in the array. You may assume that
the array has at least three numbers in it.
def third_greatest(nums)
nums_order = Array.new
until nums.length == nil
nums.sort { |a,b| a <=> b
greatest = nums.pop
nums_order.push(greatest)
}
end
return nums_order[2]
end
Test I use:
third_greatest([5, 3, 7])
returns the following:
NoMethodError: undefined method >' for [7]:Array from (irb):107:in
sort’
from (irb):107:in third_greatest' from (irb):115 from /usr/bin/irb:12:in
’
I’ve tried the sort without ‘a,b’ conditions …and also playing
around with until condition …i.e. while nums.length > 0…until
nums.length == 0 (don’t know if those make sense, however, because
wouldn’t empty array == nil?)
Little help please and thanks!