8000 Add calculator.js solutions · leosoaivan/javascript-exercises@336069d · GitHub
[go: up one dir, main page]

Skip to content

Commit 336069d

Browse files
committed
Add calculator.js solutions
1 parent 40102b5 commit 336069d

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

calculator/calculator.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
function add () {
2-
1+
function add (a, b) {
2+
return a + b
33
}
44

5-
function subtract () {
6-
5+
function subtract (a, b) {
6+
return a - b
77
}
88

9-
function sum () {
10-
9+
function sum (array) {
10+
return array.reduce((total, num) => total + num, 0)
1111
}
1212

13-
function multiply () {
14-
13+
function multiply (array) {
14+
return array.reduce((total, num) => total * num)
1515
}
1616

17-
function power() {
18-
17+
function power(a, b) {
18+
return a ** b
1919
}
2020

21-
function factorial() {
22-
21+
function factorial(a) {
22+
if (a <= 1) return 1;
23+
let product = 1;
24+
while (a > 1) {
25+
product *= a
26+
a--
27+
}
28+
return product
2329
}
2430

2531
module.exports = {

0 commit comments

Comments
 (0)
0