From 660b82042a8ef3887237f0a186c8555eca27172b Mon Sep 17 00:00:00 2001 From: Adelina Moroaca Date: Fri, 22 Dec 2023 15:55:16 +0200 Subject: [PATCH] Add calculator solution --- 08_calculator/calculator.js | 49 ++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/08_calculator/calculator.js b/08_calculator/calculator.js index c22e8d2..956a850 100644 --- a/08_calculator/calculator.js +++ b/08_calculator/calculator.js @@ -1,25 +1,52 @@ -const add = function() { - +const add = function(a, b) { + return a + b; }; -const subtract = function() { - +const subtract = function(a, b) { + return a - b; }; -const sum = function() { - + +let arrayDemo = [1, 2, 3, 4, 5]; +let sumNumbers = 0; +for (i = 0; i < arrayDemo.length; i++){ + sumNumbers += arrayDemo[i]; +} + +// const sum = function(array) { +// let sumNr = 0; +// for(i =0; i < array.length; i++){ +// sumNr+= array[i]; +// } +// return sumNr; +// }; + +const sum = function(array) { + return array.reduce((total, value) => total + value , 0); }; -const multiply = function() { +const multiply = function(array) { + return array.reduce((value1, value2) => value1 * value2); }; -const power = function() { - +const power = function(num1, num2) { + return Math.pow(num1,num2); }; -const factorial = function() { - +let arrayN = [1, 2, 3, 4, 5]; +let sumN = 0; +for (i = 0; i < arrayN.length; i++){ + sumN += arrayN[i]; +} + + +const factorial = function(num) { + let total = 1; + for(i=1; i <= num; i++) { + total *= i; + } + return total; }; // Do not edit below this line