Do you mean something like this:
const getPageFromLength = function(length) {
if (length % 10 === 0) {
return length / 10 + 1;
}
return Math.ceil(length / 10);
};
let len = dataArray.length;
let page = getPageFromLength(len);
// page is 15 for len == 140
Let me explain, what this function does!
length % 10 === 0 divides the length by 10 and then checks if the remainder of the operation is 0.? length / 10 + 1 if the remainder is 0, the function returns the length divided by 10, plus one. For example, if the length is 140, the remainder is 0. 140 / 10 + 1 equals 15, which is what your conditions above also return.: Math.ceil(length / 10) if the remainder is not equal to zero (for example for 42), the function returns the length divided by 10 and then rounds the result up. So, for 42, that would be 42 / 10 = 4.2, which is 5 rounded up. Also what your logic would return.Edit: Here's an even shorter version:
const getPageFromLength = length => Math.floor(length / 10) + 1;