From 200e4c157b8a6ed1fca34724c7abb2f1ece9b848 Mon Sep 17 00:00:00 2001 From: jaspreet-singh-sahota <55361440+jaspreet-singh-sahota@users.noreply.github.com> Date: Tue, 1 Oct 2019 19:45:39 +0530 Subject: [PATCH] Update calculator.js --- calculator/calculator.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/calculator/calculator.js b/calculator/calculator.js index 2d904a8..14f3ef7 100644 --- a/calculator/calculator.js +++ b/calculator/calculator.js @@ -1,25 +1,28 @@ -function add () { - +function add(a, b) { + return a + b; } -function subtract () { - +function subtract(a, b) { + return a - b; } -function sum () { - +function sum(array) { + return array.reduce((current, total) => total + current, 0); } -function multiply () { - +function multiply(array) { + return array.length + ? array.reduce((accumulator, nextItem) => accumulator * nextItem) + : 0; } -function power() { - +function power(a, b) { + return Math.pow(a, b); } -function factorial() { - +function factorial(n) { + if (n === 0) {return 1}; + return n * factorial(n -1) } module.exports = { @@ -29,4 +32,4 @@ module.exports = { multiply, power, factorial -} \ No newline at end of file +}