Nodejs Array Swap swapByValue( v1, v2 )

Here you can find the source of swapByValue( v1, v2 )

Method Source Code

/**/*from www  .j  a v  a2s . co  m*/
 * Swaps two elements in array searching by their value 
 *
 * @param {number} v1
 * @param {number} v2
 * @function swapByValue
 * @memberof Array
 * @global
 */
Array.prototype.swapByValue = function( v1, v2 ) {
   var i1 = null, i2 = null;
   v1 = checkFn( v1 );
   v2 = checkFn( v2 );

   for( var i = 0; i < this.length; i++ )
      if       ( this[i] === v1 ) i1 = i;
      else if( this[i] === v2 ) i2 = i;

   if( i1 === null ) { throw new Error( 'No element with value \'' + v1 + '\' was found in array.' ); return; }
   if( i2 === null ) { throw new Error( 'No element with value \'' + v2 + '\' was found in array.' ); return; }

   this.swapByIndex( i1, i2 );
};

Related

  1. swap(x, y)
    "use strict";
    function Median(A) {
        var n = A.length;
        if (n == 1) {
            return A[0];
        } else {
            return Select(A, 0, Math.floor(n / 2), n - 1);
    function Select(A, l, m, h) {
        var pos = Partition(A, l, h);
        if (pos == m) {
            return A[pos];
        } else if (pos > m) {
            return Select(A, l, m, pos - 1);
        } else if (pos < m) {
            return Select(A, pos + 1, m, h);
    function Partition(A, l, h) {
        var pivotval = A[l];
        var pivotloc = l;
        var j;
        for (j = l + 1; j <= h; j = j + 1) {
            if (A[j] < pivotval) {
                pivotloc = pivotloc + 1;
                A.swap(pivotloc, j);
        A.swap(l, pivotloc);
        return pivotloc;
    Array.prototype.swap = function (x, y) {
        var tmp = this[x];
        this[x] = this[y];
        this[y] = tmp;
        return this;
    };
    module.exports = Median;
    
  2. swap(x, y)
    Array.prototype.swap = function(x, y) {
        if (x > this.length || y > this.length || x === y) {
            return
        var tem = this[x];
        this[x] = this[y];
        this[y] = tem;
    
  3. swap(x, y)
    Array.prototype.swap = function(x, y){
      var temp = this[x];
      this[x] = this[y];
      this[y] = temp;
      return this;
    
  4. swapArray(index_a, index_b)
    RegExp.escape = function(s) {
        return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    };
    Array.prototype.swapArray = function(index_a, index_b) {
        var temp = this[index_a];
        this[index_a] = this[index_b];
        this[index_b] = temp;
    
  5. swapByIndex( i1, i2 )
    Array.prototype.swapByIndex = function( i1, i2 ) {
      var e1 = null, e2 = null;
      i1 = checkFn( i1 );
      i2 = checkFn( i2 );
      if( typeof i1 === 'string' && i1 !== '' ) e1 = i1.toNumber();
      if( typeof i2 === 'string' && i2 !== '' ) e2 = i2.toNumber();
      if( e1 === -1 || this[i1] === undefined ) { throw new Error( 'No element with index \'' + i1 + '\' was found in array.' ); return; }
      if( e2 === -1 || this[i2] === undefined ) { throw new Error( 'No element with index \'' + i2 + '\' was found in array.' ); return; }
      var e = this[i1];
    ...