From b98b40c6149dd643a836e4e3cfb70e3a831e0bbe Mon Sep 17 00:00:00 2001 From: LoptrSir <91218015+LoptrSir@users.noreply.github.com> Date: Fri, 18 Mar 2022 09:41:00 -0700 Subject: [PATCH] solution 5 --- 08_calculator/calculator.js | 72 +++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/08_calculator/calculator.js b/08_calculator/calculator.js index c22e8d2..26d7d16 100644 --- a/08_calculator/calculator.js +++ b/08_calculator/calculator.js @@ -1,33 +1,69 @@ -const add = function() { - +const add = function(num1, num2) { + total = num1 + num2; + console.log(total); + return total; }; -const subtract = function() { - +const subtract = function(num1, num2) { + total = num1 - num2; + console.log(total); + return total; }; -const sum = function() { - +const sum = function(arg) { + const array = arg; + console.log(array); + let total = 0; + for (i = 0; i < array.length; i++) { + total += array[i]; + } + console.log(total); + return total; }; -const multiply = function() { - +const multiply = function(arg) { + const array = arg; + console.log(array); + let total = 1; + for (i = 0; i < array.length; i++) { + total = total * array[i]; + } + console.log(total); + return total; }; +//multiply([-4,4]) -const power = function() { - +const power = function(num, factor) { + total = Math.pow(num, factor); + + console.log(total); + return total; }; +// power(2,2) -const factorial = function() { - +const factorial = function(num) { + if (num === 0 || num === 1) { + console.log('1'); + return 1; + } else { + for (var i = num - 1; i >= 1; i--) { + num *= i; + } + } + console.log(num); + return num; }; +factorial(6); +//add(2, 4); +//subtract ( 1, -10) +//sum([ 1, 3, 2 ]); // Do not edit below this line module.exports = { - add, - subtract, - sum, - multiply, - power, - factorial + add, + subtract, + sum, + multiply, + power, + factorial };