8000 calculator excersise · turnera/javascript-exercises@0d2ae18 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d2ae18

Browse files
author
turnera
committed
calculator excersise
1 parent 888383a commit 0d2ae18

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

calculator/calculator.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
1-
function add () {
2-
1+
function add (x, y) {
2+
return x + y;
33
}
44

5-
function subtract () {
6-
5+
function subtract (x, y) {
6+
return x - y;
77
}
88

9-
function sum () {
10-
9+
function sum (x) {
10+
let sum = 0;
11+
for(let i = 0; i< x.length; i++)
12+
{
13+
sum += x[i];
14+
}
15+
return sum;
1116
}
1217

13-
function multiply () {
14-
18+
function multiply(x)
19+
{
20+
let result = 1;
21+
for(let i = 0; i < x.length; i++)
22+
{
23+
result *= x[i];
24+
}
25+
return result;
1526
}
1627

17-
function power() {
18-
28+
function power(x, y) {
29+
return Math.pow(x, y);
1930
}
2031

21-
function factorial() {
32+
function factorial(num) {
2233

34+
if(num < 0)
35+
return -1;
36+
else if (num == 0)
37+
return 1;
38+
else
39+
{
40+
return num * factorial(num - 1);
41+
}
42+
2343
}
2444

2545
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', fun 9E88 ction() {
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