ByteGrad / Professional-React-and-Next.js-Course

This repo contains everything you need as a student of the Professional React & Next.js Course by ByteGrad.com
https://bytegrad.com/courses/professional-react-nextjs
145 stars 71 forks source link

ISSUE: Total Pages Calculation Fix for project RMTDEV #7

Open Dev-Dipesh opened 6 months ago

Dev-Dipesh commented 6 months ago

Project: rmtdev Video Number: 158 Video Title: Finish Pagination (Derived State)

ISSUE: In the video, we're calculating the total pages without using Math.ceil().

FIX: Math.ceil is important here because we want to round up to the nearest whole number. For example, if we have 15 jobs and we want to display 7 jobs per page, we need 3 pages.

15 / 7 = 2.14 // Simple division will set total pages to 2, which will lead to missing the last job

Math.ceil(2.14) = 3 // We need 3 pages to show all 15 jobs
Rope-a-dope commented 5 months ago

The code is actually correct. You can double check. Because the number totalPages is by is actually not a integer. This is JavaScript, not Java.

 const totalPages = totalJobs / RESULTS_PER_PAGE; 

In Java, we would use the following expression.

int totalPages = (totalJobs + RESULTS_PER_PAGE - 1) / RESULTS_PER_PAGE;