A Common-Sense Guide to Data Structures and Algorithms, Second Edition: Array length not included in for range (page 224)

A Common-Sense Guide to Data Structures and Algorithms, Second Edition by Jay Wengrow @jaywengrow

In the book on page 224, in the exercise num. 2, in the function findMissingNumber(), in the for range condition the exact value of array length is not included, I think we should include it instead, so:

for (let i = 0; i <= array.length; i++) {...}

please, can you tell me what do you think?
Many thanks!

Hi!

Thanks for your great question. The reason why we stop right before the array’s length is because in this example, we’re assuming that the array should also contain the number 0. So an array is actually complete if it has every number up to but not including its length. For example, the array [0, 1, 2, 3] is complete - and note that the final number (3) is one less than the array’s length (4).

I hope this is helpful.

Thanks,
Jay

1 Like