A function that finds the sum of numbers using the rest and spread operators in JavaScript

Let's write a function that will take an arbitrary amount of numbers as parameters and return their sum.

Here are examples of how our function works:

console.log( func(1, 2, 3) ); // shows 6 console.log( func(1, 2, 3, 4) ); // shows 10 console.log( func(1, 2, 3, 4, 5) ); // shows 15

First, let's make it so that all the numbers passed in the parameters fall into the array:

function func(...nums) { console.log(nums); } func(1, 2, 3); // shows [1, 2, 3]

And now let's run a loop over the passed array and find the sum of the passed numbers:

function func(...nums) { let sum = 0; for (let num of nums) { sum += num; } return sum; } let result = func(1, 2, 3); console.log(result); // shows 6

Write a function that will take any number of numbers as parameters and return their arithmetic mean.

enru