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
1.9 KiB
1.9 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69bc6cb30c1d112a2e110a05 | Challenge 248: Sorted Array Swap | 28 | challenge-248 |
--description--
Given an array of integers, return a new array using the following rules:
- Sort the integers in ascending order
- Then swap all values whose index is a multiple of 3 with the value before it.
--hints--
sortAndSwap([3, 1, 2, 4, 6, 5]) should return [1, 2, 4, 3, 5, 6].
assert.deepEqual(sortAndSwap([3, 1, 2, 4, 6, 5]), [1, 2, 4, 3, 5, 6]);
sortAndSwap([9, 7, 5, 3, 1, 2, 4, 6, 8]) should return [1, 2, 4, 3, 5, 7, 6, 8, 9].
assert.deepEqual(sortAndSwap([9, 7, 5, 3, 1, 2, 4, 6, 8]), [1, 2, 4, 3, 5, 7, 6, 8, 9]);
sortAndSwap([1, 2, 3, 4, 5, 6, 7, 8, 9]) should return [1, 2, 4, 3, 5, 7, 6, 8, 9].
assert.deepEqual(sortAndSwap([1, 2, 3, 4, 5, 6, 7, 8, 9]), [1, 2, 4, 3, 5, 7, 6, 8, 9]);
sortAndSwap([12, 5, 8, 1, 3, 10, 2, 7, 6, 4, 9, 11]) should return [1, 2, 4, 3, 5, 7, 6, 8, 10, 9, 11, 12].
assert.deepEqual(sortAndSwap([12, 5, 8, 1, 3, 10, 2, 7, 6, 4, 9, 11]), [1, 2, 4, 3, 5, 7, 6, 8, 10, 9, 11, 12]);
sortAndSwap([100, -50, 0, 75, -25, 50, -75, 25]) should return [-75, -50, 0, -25, 25, 75, 50, 100].
assert.deepEqual(sortAndSwap([100, -50, 0, 75, -25, 50, -75, 25]), [-75, -50, 0, -25, 25, 75, 50, 100]);
sortAndSwap([5, 9, 13, 77, 88, 313, -10, -65, 0, 8, 99, 101, -4, 2]) should return [-65, -10, 0, -4, 2, 8, 5, 9, 77, 13, 88, 101, 99, 313].
assert.deepEqual(sortAndSwap([5, 9, 13, 77, 88, 313, -10, -65, 0, 8, 99, 101, -4, 2]), [-65, -10, 0, -4, 2, 8, 5, 9, 77, 13, 88, 101, 99, 313]);
--seed--
--seed-contents--
function sortAndSwap(arr) {
return arr;
}
--solutions--
function sortAndSwap(arr) {
const result = [...arr].sort((a, b) => a - b);
for (let i = 3; i < result.length; i += 3) {
[result[i], result[i - 1]] = [result[i - 1], result[i]];
}
return result;
}