The problem in your code is that you cannot return true
until after you went through the entire array. However, you can return false
as soon as you detect an "inversion" - i.e. a situation when the number that follows the one you've seen before is greater than the prior number.
You are reasonably close to a working solution - you need to remove return true
, uncomment the else
, and change the final return false
to return true
.
To make your code more readable, rename first
to prior
. Also consider changing the "foreach" version of the for
loop to a regular for
loop that skips the initial element of the array. This would let you detect decreasing order, as opposed to non-increasing, which you currently detect.