Ask coding questions

← Back to all posts
Newbie Ruby question...
fighton (0)

Why is this code no returning the values over 5?

def bigger_filter(nums, target)

bigger = []

i = 0
while i < nums.length
if nums[i] > target
bigger << nums
end

i += 1
end

return bigger

end

p bigger_filter([7, 3, 2, 8, 12], 5)

Commentshotnewtop
barrycarter (12)

My standard advice for debugging: add print statements everywhere and see where bigger gets and loses its value.

mwilki7 (192)

bigger << nums
does that need to be
bigger << nums[i] ?

fighton (0)

all this does is return ALL the numbers for 'nums'. Why is it not returning just the values over 5?