Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Created April 24, 2018 09:48
Show Gist options
  • Select an option

  • Save NickDeckerDevs/5aafddc6af9a48862422ca3f9b4ca2b8 to your computer and use it in GitHub Desktop.

Select an option

Save NickDeckerDevs/5aafddc6af9a48862422ca3f9b4ca2b8 to your computer and use it in GitHub Desktop.
paginations class js setup
// TODO: complete this object/class
// The constructor takes in an array of items and a integer indicating how many
// items fit within a single page
function PaginationHelper(collection, itemsPerPage) {
this.collection = collection;
this.itemsPerPage = itemsPerPage;
}
// returns the number of items within the entire collection
PaginationHelper.prototype.itemCount = function() {
return this.collection.length;
}
// returns the number of pages
PaginationHelper.prototype.pageCount = function() {
return Math.ceil(this.itemCount() / this.itemsPerPage);
}
// returns the number of items on the current page. page_index is zero based.
// this method should return -1 for pageIndex values that are out of range
PaginationHelper.prototype.pageItemCount = function(pageIndex) {
if(pageIndex < 0 || pageIndex > this.pageCount()) {
return -1;
}
return Math.ceil(pageIndex + 1)* this.itemsPerPage;
}
// determines what page an item is on. Zero based indexes
// this method should return -1 for itemIndex values that are out of range
PaginationHelper.prototype.pageIndex = function(itemIndex) {
if(itemIndex < 0 || itemIndex > this.itemCount()) {
return -1;
}
return 'not the right answer';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment