Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Joseph Escobedo
seal-mask
.a{fill-rule:evenodd;}techdegree
Joseph Escobedo
Full Stack JavaScript Techdegree Student 4,172 Points

Cant figure out how create function for unit 2 project List Pagination and Filtering.

So for the first function it asks to show 10 students per page and once you move to the next page it loads the next 10. I can get the first page to load 10, but i feel like my logic is still wrong. I'm really confused and I've been stuck on this for a couple of days now.

example code:

const showPage = (list, page) => {
/*
Loop over items in the list parameter
-- If the index of a list item is >= the index of the first
item that should be shown on the page
-- && the list item index is <= the index of the last item
that should be shown on the page, show it
*/
}

my code:

const studentList = document.querySelector(".student-list");
const studentItem = studentList.children;
const showPage = (list, page) => {
  list = studentItem;
  studentsPerPage = 10;
  pagesPerStudents = studentItem.length / 10;
  console.log(page);
  for (let i = 0; i < list.length; i++) {
    if (i >= studentsPerPage) {
      list[i].style.display = "none";
    }
  }
};
showPage();

1 Answer

Steven Parker
Steven Parker
229,608 Points

You're getting close, but you have a little work yet to do.

It looks like you pass in a page number ("page"), but it doesn't get used for anything except logging to the console. You'd probably want to use it in determining which items to conceal using the "display" property.

Also, unless it's done elsewhere in code not shown here, you may need to include some code to reveal items previously hidden when a different page is selected.

Perhaps thinking of it this way will help: for any given page, you may have a certain number of items to hide, then one page full to show, and then any beyond that should also be hidden.

Give it another shot with that in mind and write again if you still have trouble.