|
1 | | -// ***************************************************** Common Ways to Add Numbers In JavaScript ************************************************************************* |
2 | | - |
3 | | -// 1. Normal Variable Addition (Direct Way) |
4 | | - const a = 10; |
5 | | - const b = 20; |
6 | | - const sum = a + b; |
7 | | - console.log("Addition of two number:", sum); |
8 | | - // output: |
9 | | - // Addition of two number: 30 |
10 | | - |
11 | | -// 2. Using Function() |
12 | | - // Function to calculate the sum of two numbers using a temporary variable: |
13 | | - function Sum(a, b){ |
14 | | - const c = a + b; |
15 | | - return c; |
16 | | - } |
17 | | - console.log("Sum:", Sum(4,5)); |
18 | | - // Output: |
19 | | - // Sum: 9 |
| 1 | +// *************************************************************** Common Ways to Add Numbers in JavaScript **************************************************************** |
20 | 2 |
|
21 | | - // OR |
| 3 | +/* 1. Normal Variable Addition (Direct Way) */ |
| 4 | +const a = 10; |
| 5 | +const b = 20; |
| 6 | +const sum = a + b; |
| 7 | +console.log("Addition of two number:", sum); |
| 8 | +// Output: |
| 9 | +// Addition of two number: 30 |
22 | 10 |
|
23 | | - // Function to return the sum of two numbers directly |
24 | | - function Sum(a, b) { |
25 | | - return a + b; |
26 | | - } |
27 | | - console.log(Sum(3, 7)); |
28 | | - // Output: |
29 | | - // 10 |
30 | | - |
31 | | -// 3. Using Arrow Function |
32 | | - // One-liner arrow function to add two numbers |
33 | | - const add = (a, b) => a + b; |
34 | | - console.log(add(10, 15)); |
35 | | - // Output: |
36 | | - // 25 |
37 | | - |
38 | | - // OR |
39 | | - |
40 | | - // Arrow function to add two numbers using a variable and return |
41 | | - const Add = (a, b) => { |
42 | | - const sum = a + b; |
43 | | - return sum; |
44 | | - } |
45 | | - console.log("Sum of Two Number:", Add(3,2)); |
46 | | - // Output: |
47 | | - // 5 |
48 | | - |
49 | | -// 4. Using Rest Parameters (Multiple Inputs) |
50 | | - // Function to add any number of values using rest parameters |
51 | | - function addAll(...num) { |
52 | | - let sum = 0; |
| 11 | +/* 2. Using Function(): */ |
| 12 | + |
| 13 | +// Function to return the sum of two numbers directly (Without a temporary variable): |
| 14 | +function Sum1(a, b) { |
| 15 | + return a + b; |
| 16 | +} |
| 17 | +console.log(Sum1(4, 5)); |
| 18 | +// Output: |
| 19 | +// 9 |
| 20 | + |
| 21 | +// OR |
| 22 | + |
| 23 | +// Function to calculate the sum of two numbers using a temporary variable: |
| 24 | +function Sum2(a, b) { |
| 25 | + const c = a + b; |
| 26 | + return c; |
| 27 | +} |
| 28 | +console.log("Sum:", Sum2(4, 5)); |
| 29 | +// Output: |
| 30 | +// Sum: 9 |
| 31 | + |
| 32 | +/* 3. Using Arrow "=>" Function: */ |
| 33 | + |
| 34 | +// One-liner arrow function to add two numbers |
| 35 | +const add = (a, b) => a + b; |
| 36 | +console.log(add(3, 2)); |
| 37 | +// Output: |
| 38 | +// 5 |
| 39 | + |
| 40 | +// OR |
| 41 | + |
| 42 | +// Arrow function to add two numbers using a variable and return |
| 43 | +const Add = (a, b) => { |
| 44 | + const sumArrow = a + b; |
| 45 | + return sumArrow; |
| 46 | +} |
| 47 | +console.log("Sum of Two Number:", Add(3, 2)); |
| 48 | +// Output: |
| 49 | +// Sum of Two Number: 5 |
| 50 | + |
| 51 | +/* 4. Using Rest (...) Parameters (Used for Multiple Inputs): */ |
| 52 | + |
| 53 | +// Function to add any number of values using rest parameters |
| 54 | +function addAll(...num) { |
| 55 | + let sumRest = 0; |
53 | 56 | for (let n of num) { |
54 | | - sum += n; |
55 | | - } |
56 | | - return sum; |
| 57 | + sumRest += n; // sumRest = sumRest + n; |
57 | 58 | } |
58 | | - console.log(addAll(1, 2, 3, 4)); |
59 | | - // Output: |
60 | | - // 10 |
61 | | - |
62 | | - |
63 | | -// 5. Using reduce() Method (on array) |
64 | | - // Use reduce() to calculate the sum of array elements |
65 | | - let arr = [1, 2, 3, 4, 5]; |
66 | | - let ADD = arr.reduce((a, b) => a + b, 0); |
67 | | - console.log("Addition:", ADD); |
68 | | - // Output: |
69 | | - // 15 |
70 | | - |
71 | | - // OR |
72 | | - |
73 | | - // Using reduce() Method with Arrow Function & Curly Braces |
74 | | - let arr1 = [1, 2, 3, 4, 5]; |
75 | | - let Add1 = arr.reduce((a, b) => { |
76 | | - return a + b; |
77 | | - }, 0); |
78 | | - console.log("Addition:", Add1); |
79 | | - // Output: |
80 | | - // 15 |
| 59 | + return sumRest; |
| 60 | +} |
| 61 | +console.log(addAll(1, 2, 3, 4)); |
| 62 | +// Output: |
| 63 | +// 10 |
| 64 | + |
| 65 | +/* 5. Using reduce() Method (on array): */ |
| 66 | + |
| 67 | +// Use reduce() to calculate the sum of array elements |
| 68 | +let arr = [1, 2, 3, 4, 5]; |
| 69 | +let ADD = arr.reduce((a, b) => a + b, 0); |
| 70 | +console.log("Addition:", ADD); |
| 71 | +// Output: |
| 72 | +// Addition: 15 |
| 73 | + |
| 74 | +// OR |
| 75 | + |
| 76 | +// Using reduce() Method with Arrow Function & Curly Braces |
| 77 | +let arr1 = [1, 2, 3, 4, 5]; |
| 78 | +let Add1 = arr1.reduce((a, b) => { |
| 79 | + return a + b; |
| 80 | +}, 0); |
| 81 | +console.log("Addition of the numbers in an Array:", Add1); |
| 82 | +// Output: |
| 83 | +// Addition of the numbers in an Array: 15 |
| 84 | + |
| 85 | +/* 6. Using forEach() to Add All Numbers in an Array: */ |
| 86 | + |
| 87 | +// forEach() method to calculate the sum of an array elements using Arrow Function: |
| 88 | +let arr2 = [1, 2, 3, 4, 5, 6]; |
| 89 | +let sum2 = 0; |
| 90 | +arr2.forEach(num => sum2 += num); |
| 91 | +console.log("Addition using forEach (using arrow function):", sum2); |
| 92 | +// Output: |
| 93 | +// Addition using forEach (using arrow function): 21 |
| 94 | + |
| 95 | +// OR |
| 96 | + |
| 97 | +// forEach() to calculate sum of array elements |
| 98 | +let arr3 = [1, 2, 3, 4, 5, 6]; |
| 99 | +let sum3 = 0; |
| 100 | +// Loop through each element in the array and add it to sum |
| 101 | +arr3.forEach(function (num) { |
| 102 | + sum3 += num; // sum = sum + num |
| 103 | +}); |
| 104 | +console.log("Addition using forEach():", sum3); |
| 105 | +// Output: |
| 106 | +// Addition using forEach(): 21 |
0 commit comments