-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem6.js
More file actions
32 lines (31 loc) · 914 Bytes
/
problem6.js
File metadata and controls
32 lines (31 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//https://www.hackerrank.com/challenges/lisa-workbook/
// Complete the workbook function below.
function workbook(n, k, arr) {
let noOfPages,
specialProblem = 0;
for (let i = 0; i < arr.length; i++) {
const chapter = arr[i];
if (chapter <= k) {
noOfPages = noOfPages + 1;
if (noOfPages <= chapter) {
specialProblem = specialProblem + 1;
}
} else {
let pagesPerChapter = Math.floor(chapter / k);
const remains = chapter - pagesPerChapter * k;
if (remains > 0) {
pagesPerChapter = pagesPerChapter + 1;
}
const arrChapter = Array.from({ length: chapter }, (v, key) => key + 1);
for (let j = 0; j < pagesPerChapter; j++) {
noOfPages = noOfPages + 1;
const getProblems = arrChapter.slice(j * k, j * k + k);
const special = getProblems.includes(noOfPages);
if (special) {
specialProblem = specialProblem + 1;
}
}
}
}
return specialProblem;
}