Skip to content

Instantly share code, notes, and snippets.

@tararoutray
Created August 31, 2021 14:56
Show Gist options
  • Save tararoutray/04e0f2168fe149be1fb95a0992e27f38 to your computer and use it in GitHub Desktop.
Save tararoutray/04e0f2168fe149be1fb95a0992e27f38 to your computer and use it in GitHub Desktop.
// Let's create a function to calculate the sum of all values provided. Values can be indefinite.
// args is the name for the array
let addAllNumbers = (...args) => {
// Initialize the total value with zero.
let total = 0;
// Loop through each numbers provided and add them
for (let number of args) { total += number; }
// Return the total value.
return total;
}
console.log(addAllNumbers(1)); // This will output: 1
console.log(addAllNumbers(1, 2, 3)); // This will output: 6
console.log(addAllNumbers(1, 2, 3, 4)); // This will output: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment