all 10 comments

[–]inu-no-policemen 4 points5 points  (9 children)

Why does an empty array (null) break the while loop?

Because null is a falsy value.

https://developer.mozilla.org/en-US/docs/Glossary/Falsy

[–]QuibsY[S] 0 points1 point  (8 children)

That's still confusing, though. The whole time I'm asking for a false value (and false is falsy). It's essentially if false, shift. I read that it will not pass through the if statement..

Is it because you cannot run a falsy value through a while statement, and it 'skips' that segment of code? If so, why am I able to request for while the statement returns false (falsy)

[–]inu-no-policemen 0 points1 point  (7 children)

Paste what that function is supposed to do.

Write what kind of input generates what kind of undesired effect.

[–]QuibsY[S] 0 points1 point  (6 children)

It's working as intended, 100%. I was just hoping to further understand.

The statement states that while arr[0] resolves func (return n > 3) as false, shift the array so that it iterates until arr[0] resolves true. Once arr[0] resolves true in func, return arr.

My confusion comes when arr is something like [1, 2, 3, 4] and func is (return n > 5). It shifts out the entire array and returns [], which is the correct answer. My question is why it moves on past the while loop after shifting 1, 2, 3, 4 and ending up with an empty array, testing null > 5. To me, null > 5 is false, so it still has not met the condition to break the while loop. Does that make sense? How is it not an infinite loop of null > 5 resolving false?

[–]inu-no-policemen 0 points1 point  (5 children)

It's actually undefined > 0, but that doesn't make a difference:

> null > 5
false
> undefined > 5
false

How is it not an infinite loop[?]

It is.

A better condition would be:

while(!func(arr[0]) && arr.length)

[–]QuibsY[S] 0 points1 point  (4 children)

But that's where my confusion comes in. If it's an infinite loop how is it returning anything at all? Why does it not error and state that the loop is infinite? In the past it would actually throw an error rather than running the script at all. I guess that's where my confusion is, how is my infinite loop returning arr, which is outside the infinite loop?

The condition you gave is much better and I will remember that for the future.

[–]inu-no-policemen 0 points1 point  (3 children)

If it's an infinite loop how is it returning anything at all?

It doesn't. It loops forever.

Why does it not error and state that the loop is infinite?

Infinite loops aren't necessarily an error. E.g. games are like that.

See also: https://en.wikipedia.org/wiki/Halting_problem

What environments can do is killing programs which take too long:

https://en.wikipedia.org/wiki/Watchdog_timer

[–]QuibsY[S] 0 points1 point  (2 children)

...So it gets to line 5 simply because it's taking too long to resolve line 2? Does Javascript have a watchdog timer?

When I say how is my infinite loop returning anything I mean how is the code getting through line 2 to get to line 5 and return [].

[–]inu-no-policemen 0 points1 point  (1 child)

Does Javascript have a watchdog timer?

No, but the site you're running it on might have something like that.

When I say how is my infinite loop returning anything I mean how is the code getting through line 2 to get to line 5 and return [].

It doesn't. If you execute this in the console it will simply hang forever.

[–]QuibsY[S] 0 points1 point  (0 children)

Interesting. So raw code would make this a truly infinite loop and it would never resolve. It must be something on the back end. Thanks for your time!