Ad
  • Custom User Avatar

    My JavaScript code is failing, but i can't understanding the reason.

    My code:
    // 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.collection.length / 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) {
    return this.collection.length >= this.itemsPerPage * (pageIndex+1) && pageIndex >= 0 ? this.itemsPerPage : this.pageCount() >= (pageIndex+1) && pageIndex >= 0 ? this.itemsPerPage * (pageIndex+1) - this.collection.length : -1;
    }

    // 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) {
    return itemIndex <= this.collection.length && itemIndex > 0 ? Math.ceil(itemIndex / this.itemsPerPage) : (itemIndex == 0 ? (this.collection.length > 0 ? 0 : -1) : -1);
    }

    Can someone explain me why?

    I think it's correct.