JSFiddle - React, Tailwind, and code Playground
by Diana Lemen
JavaScript
function pageCount(n, p) {
const isLessThanMid = p < n / 2;
let count = 0;
if(isLessThanMid) {
for(let i = 1; i < p; i = i + 2) {
count = count + 1;
}
} else {
for(let i = n - 2; i >= p; i = i - 2) {
count = count + 1;
}
}
return count;
}
console.log(pageCount(6, 5))