Ejemplo 1: como sumar dos números en javascript
let a = 4;
let b = 5;
document.write(a + b);
Ejemplo 2: función javascript para sumar dos números
<!--Add two integer numbers using JavaScript.-->
<html>
<head>
<title>Add two integer numbers using JavaScript.</title>
<script type="text/javascript">
function addTwoNumbers(textBox1, textBox2){
var x=document.getElementById(textBox1).value;
var y=document.getElementById(textBox2).value;
var sum=0;
sum=Number(x)+Number(y);
alert("SUM is: " + sum);
}
</script>
</head>
<body>
<h1>Add two integer numbers using JavaScript.</h1>
<b>Enter first Number: </b><br>
<input type="text" id="textIn1"/><br>
<b>Enter second Number: </b><br>
<input type="text" id="textIn2"/><br><br>
<input type="button" id="btnSum" value="Calculate SUM" onClick="addTwoNumbers('textIn1','textIn2')"/>
</body>
</html>
Ejemplo 3: javascript de dos sumas
var twoSum = function(nums, target) {
//hash table
var hash = {};
for(let i=0; i<=nums.length; i++){
//current number
var currentNumber = nums[i];
//difference in the target and current number
var requiredNumber = target - currentNumber;
// find the difference number from hashTable
const index2 = hash[requiredNumber];
// if number found, return index
// it will return undefined if not found
if(index2 != undefined) {
return [index2, i]
} else {
// if not number found, we add the number into the hashTable
hash[currentNumber] = i;
}
}
};
Ejemplo 4: suma de dos matrices en javascript
function sumArrays(...arrays) {
const n = arrays.reduce((max, xs) => Math.max(max, xs.length), 0);
const result = Array.from({ length: n });
return result.map((_, i) => arrays.map(xs => xs[i] || 0).reduce((sum, x) => sum + x, 0));
}
console.log(...sumArrays([0, 1, 2], [1, 2, 3, 4], [1, 2])); // 2 5 5 4
Ejemplo 5: suma dos números javascript
var x = 2 + 3;
//x = 5
Ejemplo 6: suma de dos números en 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;
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)