Skip to content
wolfram77 edited this page Mar 25, 2020 · 21 revisions

Exchanges two values.

Alternatives: default, update.

array.swap(x, i, j);
// x: an array
// i: an index
// j: another index
const array = require('extra-array');

var x = [1, 2, 3, 4];
array.swap(x, 0, 1);
// [2, 1, 3, 4]

array.swap(x, 0, -1);
// [4, 2, 3, 1]

array.swap(x, 0, 0);
// [1, 2, 3, 4]

references

Clone this wiki locally