Return to Snippet

Revision: 43522
at March 25, 2011 15:01 by neilking


Initial Code
Array.prototype.switchus = function(a,b) {
  var x, y;
  for(var i = 0; i < this.length; i++) {
    if(a == this[i]) {
      x = i;
    }
    if(b == this[i]) {
      y = i;
    }
  }
  if(x && y) {
    this[x] = b;
    this[y] = a;
    return true;
  } else {
    return false;
  }
}

var a = [2,3,4,5,'ultimo elemento', 'mesmo'];
result = a.switchus(4,'mesmo');

Initial URL


Initial Description


Initial Title
switch elements index

Initial Tags
array

Initial Language
JavaScript