Languages
[Edit]
EN

JavaScript - calculate number of pages from size of page and total number of items

7 points
Created by:
Creg
9600

In this article, we're going to have a look at how in JavaScript, calculate a number of pages when we know:

  • maximal size of the page,
  • a total number of items.

Simple example:

 

The above logic result explanation:

  • the total number of items is 21 (totalCount variable),
  • the maximal size of the page is 10 items (pageSize variable),

So:

  • page 1 should have 10 items
  • page 2 should have 10 items
  • page 3 should have  1 item

so we have 3 pages.

 

Testing

Edit

In this section we present simple test for different calculatePagesCount() usage cases.

 

See also

Edit
  1. JavaScript - how to calculate page number when we know size of page and item index? 

Alternative titles

  1. JavaScript - the simplest formula to calculate page count
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join