Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created March 2, 2022 02:37
Show Gist options
  • Save natafaye/2e401c49440eeef9a013ab59ab48d239 to your computer and use it in GitHub Desktop.
Save natafaye/2e401c49440eeef9a013ab59ab48d239 to your computer and use it in GitHub Desktop.
// https://www.codewars.com/kata/515bb423de843ea99400000a/train/javascript
// The constructor takes in an array of items and a integer indicating how many
// items fit within a single page
class PaginationHelper {
constructor(collection, itemsPerPage) {
this.collection = collection;
this.itemsPerPage = itemsPerPage;
}
// returns the number of items within the entire collection
itemCount() {
return this.collection.length;
}
// returns the number of pages
pageCount() {
return Math.ceil(this.itemCount() / this.itemsPerPage);
}
// returns the number of items on the current page. page_index is zero based.
pageItemCount(pageIndex) {
// return -1 for pageIndex values that are out of range
if(pageIndex < 0 || pageIndex >= this.pageCount()) {
return -1;
}
if(pageIndex < this.pageCount() - 1) {
return this.itemsPerPage;
}
const leftOver = this.itemCount() % this.itemsPerPage
if(leftOver === 0) {
return this.itemsPerPage;
}
return leftOver;
}
// determines what page an item is on. Zero based indexes
pageIndex(itemIndex) {
// return -1 for itemIndex values that are out of range
if(itemIndex < 0 || itemIndex >= this.itemCount()) {
return -1;
}
return Math.floor(itemIndex / this.itemsPerPage)
}
// return a new array of the items on that page
getItemsOnPage(pageIndex) {
// TODO: Write this method
}
}
const emailList = [
{
id: 234,
author: "Natalie",
to: "Calvin",
message: "Heyyyy",
read: false,
},
{
id: 1654,
author: "Natalie",
to: "Calvin",
message: "What's up?",
read: true,
},
{
id: 474,
author: "Dylan",
to: "Calvin",
message: "Good day",
read: false,
},
{
id: 234,
author: "Natalie",
to: "Calvin",
message: "Heyyyy",
read: false,
},
{
id: 1654,
author: "Natalie",
to: "Calvin",
message: "What's up?",
read: true,
},
{
id: 474,
author: "Dylan",
to: "Calvin",
message: "Good day",
read: false,
},
{
id: 234,
author: "Natalie",
to: "Calvin",
message: "Heyyyy",
read: false,
},
{
id: 1654,
author: "Natalie",
to: "Calvin",
message: "What's up?",
read: true,
},
{
id: 474,
author: "Dylan",
to: "Calvin",
message: "Good day",
read: false,
},
{
id: 234,
author: "Natalie",
to: "Calvin",
message: "Heyyyy",
read: false,
},
{
id: 1654,
author: "Natalie",
to: "Calvin",
message: "What's up?",
read: true,
},
{
id: 474,
author: "Dylan",
to: "Calvin",
message: "Good day",
read: false,
},
{
id: 234,
author: "Natalie",
to: "Calvin",
message: "Heyyyy",
read: false,
},
{
id: 1654,
author: "Natalie",
to: "Calvin",
message: "What's up?",
read: true,
},
{
id: 474,
author: "Dylan",
to: "Calvin",
message: "Good day",
read: false,
},
{
id: 234,
author: "Natalie",
to: "Calvin",
message: "Heyyyy",
read: false,
},
{
id: 1654,
author: "Natalie",
to: "Calvin",
message: "What's up?",
read: true,
},
{
id: 474,
author: "Dylan",
to: "Calvin",
message: "Good day",
read: false,
},
]
const emailPagination = new PaginationHelper(emailList, 4);
let pageToShow = prompt("What page do you want to see?");
pageToShow = parseInt(pageToShow);
const emailsOnPage = emailPagination.getItemsOnPage(pageToShow).map(email => email.author + ": " + email.message).join("\n")
alert( "Emails:" + emailsOnPage )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment