javascript swap array elements
// Before ES6
const temp = a[x]; // NOT RECOMMENDED UNLESS COMPLETELY UNAVOIDABLE
a[x] = a[y];
a[y] = temp;var b = list[y];
list[y] = list[x];
list[x] = b;const heavy = ["Rammstein", "Motorhead", "Imsomnium", "Be´lakor"];
let aux = "";
const intercambiar = (arr, i1, i2) => {
//Guardo en un provisional el valor del primer indice
aux = arr[i1];
//Inserte el valor del segundo índice en el lugar del primer índice
arr[i1] = arr[i2];
// Al hacer esto pierdo el valor original pero lo tengo guardado en aux por
//lo que solo necesito igualar el segundo índice a aux para completar
//el ejercicio.
arr[i2] = aux;
return arr;
};
let resulIntercambiar = intercambiar(heavy, 1, 2);
console.log(resulIntercambiar);
Also in JavaScript:
- how to send enter event to input field jquery
- how to remove backslash from string in javascript
- jquery delete grand parent of clicked element
- expo react native
- iterate over json data javascript
- how to pad string js
- three dots in js
- javascript find textarea
- concat array javascript
- javascript date format
- loading page for all ajax call in jquery 3.3.1
- dynamically generate multiple functions javascript
- render blaze in react
- jquery pagination example
- javascript json decode
- falsy values javascript
- how to reload a page in javascript
- clear formarray in angular 6
- react component name convention
- callback in js
- string includes in js
- lifecycles if reactjs
- javascript get black or white text base on color
- react detect cursor