sum of 2 numbers javascript 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: to add 2 numbers using function in javascript
function addition(number_1, number_2)
{
int sum = number_1 + number_2
document.write(sum)
}
var number_1 = parseFloat(prompt("First Number"));
var number_2 = parseFloat(prompt("Second Number"));
addition(number_1, number_2);