Skip to content

Instantly share code, notes, and snippets.

@stevieoj
Created May 12, 2017 20:45
Show Gist options
  • Save stevieoj/13768abb2789120215ad1aaae56d4cd3 to your computer and use it in GitHub Desktop.
Save stevieoj/13768abb2789120215ad1aaae56d4cd3 to your computer and use it in GitHub Desktop.
// http://www.codewars.com/kata/515bb423de843ea99400000a/train/javascript
// 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.items = collection;
this.itemsPerPage = itemsPerPage;
}
// returns the number of items within the entire collection
PaginationHelper.prototype.itemCount = function() {
return this.items.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) {
var itemsPerPage = this.itemsPerPage,
itemCount = this.itemCount(),
pageCount = this.pageCount();
if (pageIndex >= pageCount || pageIndex < 0) return -1;
return this.items.slice(pageIndex * itemsPerPage, itemCount).splice(0, itemsPerPage).length;
}
// 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 (this.itemCount() === 0
|| itemIndex < 0
|| itemIndex > this.itemCount()) return -1;
if (itemIndex === 0
|| itemIndex / this.itemsPerPage === 1) return 0;
return Math.floor((itemIndex / this.itemsPerPage));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment