Javascript Array two_sum()
Array.prototype.two_sum = function() { var answer = []; for(var i = 0; i < this.length; i++) { for(var j = i + 1 ; j < this.length; j++){ if(this[i] + this[j] == 0){ answer.push([i, j]);/* w ww . j a v a 2 s. com*/ } } } return answer; }
Array.prototype.two_sum = function(){ var locations = []; this.forEach(function(element1, index1){ this.forEach(function(element2, index2) { if (index2 > index1) { if ((element1 + element2) === 0) { locations.push([index1, index2]); }/*from ww w. j a v a 2 s . c om*/ } }) }.bind(this)) return locations; } var array = [-1, 0, 2, -2, 1]; console.log(array.two_sum())
Array.prototype.two_sum = function () { result = [];//from ww w .ja va 2 s .co m for (var x = 0; x < this.length; x++) { for (var y = x + 1; y < this.length; y++) { if (this[x] + this[y] === 0) result.push([x, y]); } } return result; };
Array.prototype.two_sum = function() { positions = [];// w w w.j av a 2 s. co m for(i=0; i<this.length-1; i++){ for(j=i+1; j<this.length; j++){ if (this[i] + this[j] === 0) { positions.push([i, j]); }; } } return positions; }; // a = [1, -1, 2, 3, 4, -3, 0, 0]; // console.log(a.two_sum());