Skip to content

Commit 81943a8

Browse files
Update Sum of Numbers.js
1 parent 7cfb7b6 commit 81943a8

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

JavaScript Interview Programs/Sum of Numbers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// 5
4949

5050
// 4. Using Rest Parameters (Multiple Inputs)
51+
// Function to add any number of values using rest parameters
5152
function addAll(...num) {
5253
let sum = 0;
5354
for (let n of num) {
@@ -61,9 +62,20 @@
6162

6263

6364
// 5. Using reduce() Method (on array)
65+
// Use reduce() to calculate the sum of array elements
6466
let arr = [1, 2, 3, 4, 5];
6567
let ADD = arr.reduce((a, b) => a + b, 0);
6668
console.log("Addition:", ADD);
6769
// Output:
6870
// 15
6971

72+
// OR
73+
74+
// Using reduce() Method with Arrow Function & Curly Braces
75+
let arr1 = [1, 2, 3, 4, 5];
76+
let Add1 = arr.reduce((a, b) => {
77+
return a + b;
78+
}, 0);
79+
console.log("Addition:", Add1);
80+
// Output:
81+
// 15

0 commit comments

Comments
 (0)