JavaScript array swap
JavaScript
var A = [1, 2, 3, 4, 5, 6, 7, 8, 9];
function swap(A, x, y){
A[x] = A.splice(y, 1, A[x])[0];
return A;
}
function moveUp(A, x){
console.log(A.length);//9
if (x < A.length && A.length >= 2){
return swap(A, x, x+1);
}
}
function moveDown(A, x){
console.log(A.length);//9
if (x > 0 && A.length >= 2){
return swap(A, x, x-1);
}
}
console.log(A);
A = swap(A, 0, 1);
console.log(A);
A = moveDown(A, 1);
console.log(A);