Update calculator.js

This commit is contained in:
jaspreet-singh-sahota 2019-10-01 19:45:39 +05:30 committed by GitHub
parent cc479b5e70
commit 200e4c157b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 13 deletions

View File

@ -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 = { module.exports = {
@ -29,4 +32,4 @@ module.exports = {
multiply, multiply,
power, power,
factorial factorial
} }