Confused on this explanation

Continuing the discussion from freeCodeCamp Algorithm Challenge Guide: Chunky Monkey:

I dont understand how this works.

I manged to complete the challenge with the same code as the advanced solution #2 except one small bit:

“while (arr.length>0)”

as you can see i did nothing more than ask that as long as the length is greater than 0 to continue, however in the solution i quoted it performs this without adding the comparison to zero. In my head I read it as “perform this while length of arr”… while length of arr what? the code works, yes, but i dont understand why it works when not adding that extra “>0” to it. can someone explain this for me please.

one more thing. I noticed in the explanation to this solution it also mentioned that the loop continues until the length is not 0… super confused on this part. isnt the length of arr already not 0 from the get go? sooo but what i just read theoretically it shouldnt run at all since its continuing until its not longer 0 and yet as it begins it is already not 0. is this just a typing error in the explanation?

while(arr.length) is the same thing as while(arr.length > 0) because 0 is a “falsy” value.

1 Like