function getPaginationItems(x, n) {
  const result = []
  const start = Math.max(1, x - 2)
  const end = Math.min(start + 2, n)
  if (start > 1) {
    result.push(1)
  }
  if (start > 2) {
    result.push('...')
  }
  for (let i = start; i <= end; i++) {
    result.push(i)
  }
  if (end + 1 < n) {
    result.push('...')
  }
  if (end < n) {
    result.push(n)
  }
  return result
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: