js swap two elements in array code example
Example 1: how to swap two elements in an array javascript
const temp = a[x];
a[x] = a[y];
a[y] = temp;
Example 2: javascript switch items in array
var b = list[y];
list[y] = list[x];
list[x] = b;
Example 3: swap elements in array javascript
const heavy = ["Rammstein", "Motorhead", "Imsomnium", "Be´lakor"];
let aux = "";
const intercambiar = (arr, i1, i2) => {
aux = arr[i1];
arr[i1] = arr[i2];
arr[i2] = aux;
return arr;
};
let resulIntercambiar = intercambiar(heavy, 1, 2);
console.log(resulIntercambiar);