Swap array element - Node.js Array

Node.js examples for Array:Swap

Description

Swap array element

Demo Code


swap: function(ary, a, b) {
    if(a < 0 || b < 0 || ary.length <= a || ary.length <= b) {
        throw new Error('IndexError ' + a + " - " + b);
    }/*  w w  w  . j  a  v a2 s  .c  om*/
    var temp = ary[a];
    ary[a] = ary[b];
    ary[b] = temp;
},

Related Tutorials