8000 Complete javascript exercise #08 · ssaigol/javascript-exercises@983cd5f · GitHub
[go: up one dir, main page]

Skip to content

Commit 983cd5f

Browse files
committed
Complete javascript exercise TheOdinProject#8
1 parent 6c50f72 commit 983cd5f

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

08_calculator/calculator.js

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

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

9-
const sum = function() {
10-
9+
const sum = function(arr) {
10+
return arr.reduce((sum, number) => sum + number, 0);
1111
};
1212

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

17-
const power = function() {
18-
17+
const power = function(a, b) {
18+
let total = a;
19+
if (b > 1) {
20+
for (let i = 1; i < b; i++) {
21+
total = total * a;
22+
}
23+
}
24+
return total;
1925
};
2026

21-
const factorial = function() {
22-
27+
const factorial = function(a) {
28+
if (a < 2) return 1;
29+
let total = a;
30+
for (let i = a - 1; i > 0; i--) {
31+
total = total * i;
32+
}
33+
return total;
2334
};
2435

2536
// Do not edit below this line

08_calculator/calculator.spec.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,85 @@ describe('add', () => {
55
expect(calculator.add(0, 0)).toBe(0);
66
});
77

8-
test.skip('adds 2 and 2', () => {
8+
test('adds 2 and 2', () => {
99
expect(calculator.add(2, 2)).toBe(4);
1010
});
1111

12-
test.skip('adds positive numbers', () => {
12+
test('adds positive numbers', () => {
1313
expect(calculator.add(2, 6)).toBe(8);
1414
});
1515
});
1616

1717
describe('subtract', () => {
18-
test.skip('subtracts numbers', () => {
18+
test('subtracts numbers', () => {
1919
expect(calculator.subtract(10, 4)).toBe(6);
2020
});
2121

22-
test.skip('subtracts negative numbers', () => {
22+
test('subtracts negative numbers', () => {
2323
expect(calculator.subtract(-10, -4)).toBe(-6);
2424
});
2525

26-
test.skip('subtracts numbers of mixed parity', () => {
26+
test('subtracts numbers of mixed parity', () => {
2727
expect(calculator.subtract(-8, 7)).toBe(-15);
2828
});
2929
});
3030

3131
describe('sum', () => {
32-
test.skip('computes the sum of an empty array', () => {
32+
test('computes the sum of an empty array', () => {
3333
expect(calculator.sum([])).toBe(0);
3434
});
3535

36-
test.skip('computes the sum of an array of one number', () => {
36+
test('computes the sum of an array of one number', () => {
3737
expect(calculator.sum([7])).toBe(7);
3838
});
3939

40-
test.skip('computes the sum of an array of two numbers', () => {
40+
test('computes the sum of an array of two numbers', () => {
4141
expect(calculator.sum([7, 11])).toBe(18);
4242
});
4343

44-
test.skip('computes the sum of an array of many numbers', () => {
44+
test('computes the sum of an array of many numbers', () => {
4545
expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25);
4646
});
4747
});
4848

4949
describe('multiply', () => {
50-
test.skip('multiplies two numbers', () => {
50+
test('multiplies two numbers', () => {
5151
expect(calculator.multiply([2, 4])).toBe(8);
5252
});
5353

54-
test.skip('multiplies several numbers', () => {
54+
test('multiplies several numbers', () => {
5555
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toBe(645120);
5656
});
5757
});
5858

5959
describe('power', () => {
60-
test.skip('raises one number to the power of another number', () => {
60+
test('raises one number to the power of another number', () => {
6161
expect(calculator.power(4, 3)).toBe(64); // 4 to third power is 64
6262
});
6363

64-
test.skip('raises one number to the power of a large number', () => {
64+
test('raises one number to the power of a large number', () => {
6565
expect(calculator.power(3, 10)).toBe(59049); // 3 to tenth power is 59049
6666
});
6767
});
6868

6969
describe('factorial', () => {
70-
test.skip('computes the factorial of 0', () => {
70+
test('computes the factorial of 0', () => {
7171
expect(calculator.factorial(0)).toBe(1); // 0! = 1
7272
});
7373

74-
test.skip('computes the factorial of 1', () => {
74+
test('computes the factorial of 1', () => {
7575
expect(calculator.factorial(1)).toBe(1);
7676
});
7777

78-
test.skip('computes the factorial of 2', () => {
78+
test('computes the factorial of 2', () => {
7979
expect(calculator.factorial(2)).toBe(2);
8080
});
8181

82-
test.skip('computes the factorial of 5', () => {
82+
test('computes the factorial of 5', () => {
8383
expect(calculator.factorial(5)).toBe(120);
8484
});
8585

86-
test.skip('computes the factorial of 10', () => {
86+
test('computes the factorial of 10', () => {
8787
expect(calculator.factorial(10)).toBe(3628800);
8888
});
8989
});

0 commit comments

Comments
 (0)
0