Languages
[Edit]
EN

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

5 points
Created by:
Creg
9600

In this article, we're going to have a look at how in JavaScript, calculate page number on which item is located, when we know:

  • maximal size of the single page,
  • item index (counted from 0).

Simple example:

The above logic result explanation:

  • the maximal size of the single page is 10 (pageSize variable),
  • the item index is 29 (itemIndex variable),

So:

  • page 1 should contain item indexes from 0 to 9
  • page 2 should contain item indexes from 10 to 19
  • page 3 should contain item indexes from 20 to 29

so we have item on 3rd page.

 

Testing

Edit

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

 

See also

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

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