I am creating a simple method that compares numbers in an array. There are rules, but I don't think that information is needed.
def nearest_larger(arr, idx)
diff = 1
p arr.length
while true
left = idx - diff
right = idx + diff
if (left >= 0) && (arr[left] > arr[idx])
return left
elsif (arr[right] > arr[idx]) && (right < arr.length)
return right
elsif (left < 0) && (right >= arr.length)
return nil
end
diff += 1
end
end
puts(nearest_larger([2,1,1,1], 0))
so when i change (arr[right] > arr[idx]) && (right < arr.length) to (right < arr.length) && (arr[right] > arr[idx]) everything works fine.
why does the order matter here?