Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.6 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68f6587287ad1f4ad39b0c81 Challenge 95: Array Shift 28 challenge-95

--description--

Given an array and an integer representing how many positions to shift the array, return the shifted array.

  • A positive integer shifts the array to the left.
  • A negative integer shifts the array to the right.
  • The shift wraps around the array.

For example, given [1, 2, 3] and 1, shift the array 1 to the left, returning [2, 3, 1].

--hints--

shiftArray([1, 2, 3], 1) should return [2, 3, 1].

assert.deepEqual(shiftArray([1, 2, 3], 1), [2, 3, 1]);

shiftArray([1, 2, 3], -1) should return [3, 1, 2].

assert.deepEqual(shiftArray([1, 2, 3], -1), [3, 1, 2]);

shiftArray(["alpha", "bravo", "charlie"], 5) should return ["charlie", "alpha", "bravo"].

assert.deepEqual(shiftArray(["alpha", "bravo", "charlie"], 5), ["charlie", "alpha", "bravo"]);

shiftArray(["alpha", "bravo", "charlie"], -11) should return ["bravo", "charlie", "alpha"].

assert.deepEqual(shiftArray(["alpha", "bravo", "charlie"], -11), ["bravo", "charlie", "alpha"]);

shiftArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 15) should return [5, 6, 7, 8, 9, 0, 1, 2, 3, 4].

assert.deepEqual(shiftArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 15), [5, 6, 7, 8, 9, 0, 1, 2, 3, 4]);

--seed--

--seed-contents--

function shiftArray(arr, n) {

  return arr;
}

--solutions--

function shiftArray(arr, n) {
  const len = arr.length;
  n = n % len;
  if (n < 0) n += len;

  return arr.slice(n).concat(arr.slice(0, n));
}