Skip to content

Instantly share code, notes, and snippets.

@netoisc
Created April 8, 2019 19:29
Show Gist options
  • Select an option

  • Save netoisc/446484608103a083851223ce50ef59ac to your computer and use it in GitHub Desktop.

Select an option

Save netoisc/446484608103a083851223ce50ef59ac to your computer and use it in GitHub Desktop.
Get object with sum of positives and negatives numbers with ES6
// hints
// ES6 rest params
// arrow function
const sum = (...args) =>
args.reduce((accu, value) => ({
positive: value > 0 ? accu.positive + value: accu.positive,
negative: value < 0 ? accu.negative + value: accu.negative,
}), { positive: 0, negative: 0})
// tests
sum(-7, 53, 89, -80, 2, 84, -39, -38, 57 - 78);
// Object { positive: 228, negative: -185 }
sum()
// Object { positive: 0, negative: 0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment