Skip to content

Instantly share code, notes, and snippets.

@Yago
Last active November 28, 2018 13:27
Show Gist options
  • Save Yago/57e5b3a001a6b669020f844643588804 to your computer and use it in GitHub Desktop.
Save Yago/57e5b3a001a6b669020f844643588804 to your computer and use it in GitHub Desktop.
const calculator = {
sum(...args) {
return args.reduce((acc, n) => typeof n === 'number' ? acc + n : acc, 0);
},
sumOldSchool: function () {
return [...arguments].reduce(function (acc, n) {
if (typeof n === 'number') return acc + n;
return acc;
}, 0);
},
};
calculator.sum(31, 7, 4); // return 42
calculator.sumOldSchool(31, 7, 4); // return 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment