marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.02k stars 796 forks source link

Chapter 14 - Layout, magic number in example #517

Closed alexpi closed 4 years ago

alexpi commented 4 years ago

Hello,

In the example that fills a line with characters, why dividing 2000 by just one X doesn't work? A Math error? Did you come up with 5 by trial and error?

marijnh commented 4 years ago

That protects a bit about rounding issues (offsetWidth returns a rounded integer value, whereas actual character dimensions might be fractional). The code could also have used getBoundingClientRect, which returns fractional values, but that's a bit more awkward to do for text nodes.