8000 finished calculator · cqkh42/javascript-exercises@d451ab4 · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit d451ab4

Browse files
committed
finished calculator
1 parent fd98005 commit d451ab4

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

calculator/calculator.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
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 (nums) {
10+
return nums.reduce((total, next) => total + next, 0)
1111
}
1212

13-
function multiply () {
14-
13+
function multiply (nums) {
14+
return nums.reduce((total, next) => total * next, 1);
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+
let total = 1
23+
for (let i=1; i <= a; ++i) {
24+
total = total * i
25+
}
26+
return total;
2327
}
2428

2529
module.exports = {
@@ -29,4 +33,4 @@ module.exports = {
2933
multiply,
3034
power,
3135
factorial
32-
}
36+
}

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