Closed nilTheDev closed 3 years ago
Given that passing 0 for step
is an unreasonable request, I don't think the code should be expected to handle it --- returning the empty array is also not the right response to what the caller is asking. So I think it's reasonable for the code to hang in such a case, and prefer not to add more complexity to the solution to handle it.
The
range
function would cause an infinite loop if0
is passed explicitly as thestep
. Ifstart
is greater thanend
the control condition of thefor
loop in theelse
block will stop the loop from commencing iteration. However, ifend
is greater thanstart
then thefor
loop will iterate infinitely causing the program to crash.Therefore the
else
block is changed toelse if (step < 0)
. This condition will not let any for loop iterate if thestep
is0
.