Write a function. Test real cases. Improve your solution.
Return {items, page, totalPages, hasNext} for a requested page of a results array. requestedPage and pageSize are positive integers. totalPages is the number of pages needed for all results. Clamp the requested page to the last available page; use page 1 with totalPages 0 when results are empty. items contains that page of results, and hasNext is true only when another page exists after the returned page. Leave the input unchanged.
[
[
"a",
"b",
"c",
"d",
"e"
],
1,
2
]{
"items": [
"a",
"b"
],
"page": 1,
"totalPages": 3,
"hasNext": true
}[
[
"a",
"b",
"c",
"d",
"e"
],
2,
2
]{
"items": [
"c",
"d"
],
"page": 2,
"totalPages": 3,
"hasNext": true
}[
[
"a",
"b",
"c",
"d",
"e"
],
3,
2
]{
"items": [
"e"
],
"page": 3,
"totalPages": 3,
"hasNext": false
}[
[
"a",
"b",
"c"
],
99,
2
]{
"items": [
"c"
],
"page": 2,
"totalPages": 2,
"hasNext": false
}[ [], 7, 10 ]
{
"items": [],
"page": 1,
"totalPages": 0,
"hasNext": false
}[
[
{
"id": 1
},
{
"id": 2
},
{
"id": 3
},
{
"id": 4
}
],
2,
2
]{
"items": [
{
"id": 3
},
{
"id": 4
}
],
"page": 2,
"totalPages": 2,
"hasNext": false
}[
[
null,
false,
0
],
1,
20
]{
"items": [
null,
false,
0
],
"page": 1,
"totalPages": 1,
"hasNext": false
}Your code runs in your browser. These exercises use synchronous JavaScript functions and built-in data types; DOM access, network requests, and npm imports are unavailable. Runs stop after about four seconds. Test results support self-study and don’t certify proficiency.