loading...

Lisa's Workbook Explain - Solution | Javascript

hieusmiths profile image Minh Hieu ・1 min read
function workbook(n, k, arr) {
  let special = 0;
  let page = 1;
  for (let i = 0; i < n; i ++) {
    const problems = arr[i];
    let temp = 1;
    const up = Math.ceil(problems / k);
    while (temp <= up) {
      const start = temp === 1 ? 1 : (k * (temp -1)) + 1;
      console.log(start, up);
      const end = temp === up ? problems : temp * k;
      if ( page >= start && page <= end) {
        special++;
      }
      page ++;
      temp++;
    } 
  }
  return special
}

workbook(10, 5, [3, 8, 15, 11, 14, 1, 9, 2, 24, 31])

Problem

Discussion

pic
Editor guide