Created
June 17, 2015 19:54
-
-
Save teckliew/ef9566eb6bdb6f5b873b to your computer and use it in GitHub Desktop.
Showing X to Y of Z Products. Given the page number, page size, and the total number of product.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//my solution | |
var paginationText = function(pageNumber, pageSize, totalProducts){ | |
var numOfPages = Math.ceil(totalProducts/pageSize); | |
var initItem = pageNumber*pageSize-(pageSize-1); | |
var endItem; | |
if (pageNumber === numOfPages){ | |
endItem = totalProducts; | |
}else{ | |
endItem = pageSize*pageNumber; | |
}; | |
return "Showing " + initItem + " to " + endItem + " of " + totalProducts + " Products."; | |
} | |
//good examples | |
//1 | |
var paginationText = function(pageNumber, pageSize, totalProducts){ | |
return 'Showing ' + (((pageNumber - 1) * pageSize) + 1) + ' to ' + Math.min(pageSize * pageNumber, totalProducts) + ' of ' + totalProducts + ' Products.'; | |
} | |
//2 | |
var paginationText = function(p, sz, total) { | |
return 'Showing ' + ((p - 1) * sz + 1) + " to " + (p * sz > total ? total : p * sz) + | |
" of " + total + " Products."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment