-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo15.js
99 lines (71 loc) · 3.18 KB
/
demo15.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 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() {
if(this.collection.length%this.itemsPerPage==0){
return Math.ceil(this.collection.length/this.itemsPerPage)
}else{
return Math.ceil(this.collection.length/(this.itemsPerPage-1))
}
}
// 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) {
console.log(pageIndex);
if(pageIndex>=this.pageCount()){return -1;}
else if(pageIndex==this.pageCount()-1){
return this.collection.length%(this.itemsPerPage)
}else{
return 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>=this.collection.length||itemIndex<0){return -1}else{
return parseInt(itemIndex/this.itemsPerPage);
}
}
For this exercise you will be strengthening your page-fu mastery. You will complete the PaginationHelper class, which is a utility class helpful for querying paging information related to an array.
The class is designed to take in an array of values and an integer indicating how many items will be allowed per each page. The types of values contained within the collection/array are not relevant.
The following are some examples of how this class is used:
var helper = new PaginationHelper(['a','b','c','d','e','f'], 4);
helper.pageCount(); //should == 2
helper.itemCount(); //should == 6
helper.pageItemCount(0); //should == 4
helper.pageItemCount(1); // last page - should == 2
helper.pageItemCount(2); // should == -1 since the page is invalid
// pageIndex takes an item index and returns the page that it belongs on
helper.pageIndex(5); //should == 1 (zero based index)
helper.pageIndex(2); //should == 0
helper.pageIndex(20); //should == -1
helper.pageIndex(-10); //should == -1
https://www.codewars.com/kata/paginationhelper/train/javascript
function PaginationHelper(collection, itemsPerPage){
this.collection = collection, this.itemsPerPage = itemsPerPage;
}
PaginationHelper.prototype.itemCount = function() {
return this.collection.length;
}
PaginationHelper.prototype.pageCount = function() {
return Math.ceil(this.collection.length / this.itemsPerPage);
}
PaginationHelper.prototype.pageItemCount = function(pageIndex) {
return pageIndex < this.pageCount()
? Math.min(this.itemsPerPage, this.collection.length - pageIndex * this.itemsPerPage)
: -1;
}
PaginationHelper.prototype.pageIndex = function(itemIndex) {
return itemIndex < this.collection.length && itemIndex >= 0
? Math.floor(itemIndex / this.itemsPerPage)
: -1;
}