JS Test

by Trev Burley

JavaScript

class Test {
  constructor(items, itemsPerPage) {
    this.items = items;
    this.itemsPerPage = itemsPerPage;

    this.sortPages();
  }

  sortPages() {
    let pages = [];
    let noOfPages = Math.round((this.items.length / this.itemsPerPage));

    // Loop pages
    for (let i = 0; i < noOfPages; i++) {
      let pg = [];
      let startNo = (i * this.itemsPerPage);
      let endNo = (this.itemsPerPage * i);

      // Loop items in page
      for (let x = startNo; x < (startNo + this.itemsPerPage); x++) {
        pg.push(this.items[x]);
      }
      pages.push(pg);
    }

    this.pages = pages;
  }

  pageCount() {
    return this.pages.length;
  }

  pageIndex(item) {
    for (let i = 0; i <= this.pages.length; i++) {
      if (this.pages[i].includes(item)) {
        return i;
      }
    }
  }

}

const added = new Test([10, 4, 2, 6, 7, 8], 3);
let solution = [];
solution[0] = added.pageCount();
solution[1] = added.pageIndex(6);

console.log(solution);