Skip to content

Instantly share code, notes, and snippets.

@danielchikaka
Created December 10, 2021 15:24
Show Gist options
  • Save danielchikaka/cd5420d755d16fd0c90ab7cd74b32777 to your computer and use it in GitHub Desktop.
Save danielchikaka/cd5420d755d16fd0c90ab7cd74b32777 to your computer and use it in GitHub Desktop.
Unlimited number of parameters summation
// function declaration
const sumAllNums = (...args) => {
let sum = 0
for (const element of args) {
sum += element
}
return sum
}
console.log(sumAllNums(1, 2, 3, 4)) // 10
console.log(sumAllNums(10, 20, 13, 40, 10)) // 93
console.log(sumAllNums(15, 20, 30, 25, 10, 33, 40)) // 173
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment