find two sum array in js code example
Example 1: two sum javascript
var twoSum = function(nums, target) {
var hash = {};
for(let i=0; i<=nums.length; i++){
var currentNumber = nums[i];
var requiredNumber = target - currentNumber;
const index2 = hash[requiredNumber];
if(index2 != undefined) {
return [index2, i]
} else {
hash[currentNumber] = i;
}
}
};
Example 2: sum of two array in javascript
function sumArray(a, b) {
var c = [];
for (var i = 0; i < Math.max(a.length, b.length); i++) {
c.push((a[i] || 0) + (b[i] || 0));
}
return c;
}