-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagingText.js
51 lines (39 loc) · 1.07 KB
/
pagingText.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const calculatePages = (text, limit) => {
const pages = Math.ceil(text.length / limit);
let pagingChars = 3;
if (pages < 10) {
pagingChars += pages * (2 * 1) + 3;
} else if (pages < 100) {
pagingChars += pages * (2 * 2) + 3;
} else if (pages < 100) {
pagingChars += pages * (2 * 3) + 3;
}
return Math.ceil((text.length + pagingChars) / limit);
};
const solution = (text, limit) => {
const output = [];
let start = 0;
let page = 1;
const accuratePages = calculatePages(text, limit);
const fixedPagingChars = 3;
while (start < text.length) {
let pagingChars = fixedPagingChars;
if (page < 10) {
pagingChars += 2 * 1;
} else if (page < 100) {
pagingChars += 2 * 2;
} else if (page < 100) {
pagingChars += 2 * 3;
}
const pageTextLimit = limit - pagingChars;
const pageContent = `${text.substr(
start,
pageTextLimit
)}<${page}/${accuratePages}>`;
output.push(pageContent);
start += pageTextLimit;
page++;
}
return output;
};
console.log(solution("hello world!", 10));