Nick09
Nick09

Reputation: 228

How many 10 x 10 boxes fit into div?

This is a bit of a math problem mixes with some JavaScript, I have this adjustable div (https://i.sstatic.net/1Pwj9.jpg), when clicking on submit it will be filled entirely by 10 by 10 smaller div's.

But I don't quite know how to calculate how many row's of 10 by 10 div's will fit into the parent.

What I currently have is this code:

var w_items = Math.ceil(def_w / 10),
    h_items = Math.ceil(def_h / 10);

That give's me back the rounded width and height of said parent, don't know how to calculate how many would fit thought, do I add them together or something?

Upvotes: 1

Views: 133

Answers (2)

The24thDS
The24thDS

Reputation: 164

Multiply the width and height of the parent div. You'll get its area. Then, the area of a small div is 100 because of 10x10=100. Then all you have to do is Math.floor(parentArea/smallDivArea) and you'll get the number of small divs that can fit into the big div.

Upvotes: 1

user7454357
user7454357

Reputation:

If you floor (also known as integer division) the value instead of ceiling it, you will get the number of elements that can fit horizontally and vertically. The total count will be w_items * h_items.

Upvotes: 0

Related Questions