AI Native challenges by level

Level 1 · Foundations · Data and input

Paginate search results

Build pagination that handles boundary values.

Problem statement

Write function solution(items, page, pageSize). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. items is an array of JSON values; page and pageSize are numbers. Use pageSize=10 if it is not a positive integer, and cap it at 100. totalPages is ceil(items.length/pageSize). Use page=1 if it is not a positive integer, and clamp it to max(1,totalPages) if it exceeds the last page. Return {items:page slice,total:item count,page:adjusted page,pageSize:adjusted size,totalPages}. For an empty input, page is 1 and totalPages is 0. Input limits: at most 50,000 items per input array and 1,000 characters per string. Numbers are finite; integers and accumulated integer results stay in the JavaScript safe-integer range. Other types and validity rules follow the statement above.

Example input and output

Example 1

Function arguments

[
  [
    1,
    2,
    3,
    4,
    5
  ],
  2,
  2
]

Return value

{
  "items": [
    3,
    4
  ],
  "total": 5,
  "page": 2,
  "pageSize": 2,
  "totalPages": 3
}
Example 2

Function arguments

[
  [],
  9,
  10
]

Return value

{
  "items": [],
  "total": 0,
  "page": 1,
  "pageSize": 10,
  "totalPages": 0
}
Example 3

Function arguments

[
  [
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12,
    13,
    14,
    15,
    16,
    17,
    18,
    19
  ],
  2,
  10
]

Return value

{
  "items": [
    10,
    11,
    12,
    13,
    14,
    15,
    16,
    17,
    18,
    19
  ],
  "total": 20,
  "page": 2,
  "pageSize": 10,
  "totalPages": 2
}
Practice has no points, deadline or submission cap. Use AI and search freely. Sign in to run public examples, or verify locally.

86 / 20,000 characters

Add your test · optional

Enter a JSON argument array in items, page, pageSize order. Your test runs alongside the three examples.

Save both files in one folder and run this command with Node.js. Read the checks field for individual example results.

node verify-paginate-results.mjs
문의하기

서비스명: 포트포올
문의는 운영자에게 이메일로 전송됩니다.

이메일과 문의 내용은 답변을 위해 운영자 및 메일 발송 서비스로 전송됩니다. 비밀번호·결제정보·민감한 개인정보는 입력하지 마세요.