8000 calculator · fivan18/javascript-exercises@a37c286 · GitHub
[go: up one dir, main page]

Skip to content

Commit a37c286

Browse files
author
Ivan
committed
calculator
1 parent f56bc16 commit a37c286

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

calculator/calculator.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
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 (arr) {
10+
return arr.length === 0
11+
? 0
12+
: arr.reduce((accum, value) => accum += value, 0);
1113
}
1214

13-
function multiply () {
14-
15+
function multiply (arr) {
16+
return arr.length === 0
17+
? 0
18+
: arr.reduce((accum, value) => accum *= value, 1);
1519
}
1620

17-
function power() {
18-
21+
function power(base, power) {
22+
return Math.pow(base,power);
1923
}
2024

21-
function factorial() {
22-
25+
function factorial(num) {
26+
if(num === 0){
27+
return 1;
28+
} else {
29+
return num * factorial(num - 1);
30+
}
2331
}
2432

2533
module.exports = {

calculator/calculator.spec.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,73 @@ describe('add', function() {
55
expect(calculator.add(0,0)).toEqual(0);
66
});
77

8-
xit('adds 2 and 2', function() {
8+
it('adds 2 and 2', function() {
99
expect(calculator.add(2,2)).toEqual(4);
1010
});
1111

12-
xit('adds positive numbers', function() {
12+
it('adds positive numbers', function() {
1313
expect(calculator.add(2,6)).toEqual(8);
1414
});
1515
});
1616

1717
describe('subtract', function() {
18-
xit('subtracts numbers', function() {
18+
it('subtracts numbers', function() {
1919
expect(calculator.subtract(10,4)).toEqual(6);
2020
});
2121
});
2222

2323
describe('sum', function() {
24-
xit('computes the sum of an empty array', function() {
24+
it('computes the sum of an empty array', function() {
2525
expect(calculator.sum([])).toEqual(0);
2626
});
2727

28-
xit('computes the sum of an array of one number', function() {
28+
it('computes the sum of an array of one number', function() {
2929
expect(calculator.sum([7])).toEqual(7);
3030
});
3131

32-
xit('computes the sum of an array of two numbers', function() {
32+
it('computes the sum of an array of two numbers', function() {
3333
expect(calculator.sum([7,11])).toEqual(18);
3434
});
3535

36-
xit('computes the sum of an array of many numbers', function() {
36+
it('computes the sum of an array of many numbers', function() {
3737
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
3838
});
3939
});
4040

4141
describe('multiply', function() {
42-
xit('multiplies two numbers', function() {
42+
it('multiplies two numbers', function() {
4343
expect(calculator.multiply([2,4])).toEqual(8);
4444
});
4545

46-
xit('multiplies several numbers', function() {
46+
it('multiplies several numbers', function() {
4747
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
4848
});
4949
});
5050

5151
describe('power', function() {
52-
xit('raises one number to the power of another number', function() {
52+
it('raises one number to the power of another number', function() {
5353
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
5454
});
5555
});
5656

5757
describe('factorial', function() {
58-
xit('computes the factorial of 0', function() {
58+
it('computes the factorial of 0', function() {
5959
expect(calculator.factorial(0)).toEqual(1); // 0! = 1
6060
});
6161

62-
xit('computes the factorial of 1', function() {
62+
it('computes the factorial of 1', function() {
6363
expect(calculator.factorial(1)).toEqual(1);
6464
});
6565

66-
xit('computes the factorial of 2', function() {
66+
it('computes the factorial of 2', function() {
6767
expect(calculator.factorial(2)).toEqual(2);
6868
});
6969

70-
xit('computes the factorial of 5', function() {
70+
it('computes the factorial of 5', function() {
7171
expect(calculator.factorial(5)).toEqual(120);
7272
});
7373

74-
xit('computes the factorial of 10', function() {
74+
it('computes the factorial of 10', function() {
7575
expect(calculator.factorial(10)).toEqual(3628800);
7676
});
7777
});

0 commit comments

Comments
 (0)
0