Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames Estrada
Full Stack JavaScript Techdegree Student 25,866 PointsIs the point (8, 5) really out of bounds because of zero base counting?
Jeremy intentionally chose to make the point in square 8,5 to illustrate the common programming mistake of zero base counting. However, if everything starts from zero, shouldn't it also apply to the map we created with the values 8,5 for width and height respectively? If so, our point should still be inside our bounds and the reason it is out of bounds is because of the bool check we defined in our OnMap method:
return point.X >= 0 && point.X < Width &&
point.Y >= 0 && point.Y < Height;
We check if our point is "less than" 8 and 5 and not "less than or equal" to 8 and 5. If we change it to "less than or equal" our return value is true.
Am I right or am I missing something?
1 Answer
Steven Parker
231,198 PointsIt sounds like you nearly have it, but not quite.
Yes, starting from zero also applies to the map created with the values 8,5. So the 8 valid values for width are 0,1,2,3,4,5,6 and 7. And the 5 valid height values are 0,1,2,3 and 4.
That's why the upper bounds test is "less than" 8 and 5 and not "less than or equal" to 8 and 5. The width number represents how many width values there are, and is therefore one larger than the largest possible value. Same thing with the height number .
So yes, the point (8, 5) is really out of bounds because of zero base counting.
Does it make sense now?