8000 Solve the rest of problem #8 · chronushan/javascript-exercises@d7742ee · GitHub
[go: up one dir, main page]

Skip to content

Commit d7742ee

Browse files
committed
Solve the rest of problem TheOdinProject#8
1 parent b5b440f commit d7742ee

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

08_calculator/calculator.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@ const sum = function (array) {
1212
return answer;
1313
};
1414

15-
const multiply = function () {};
15+
const multiply = function (array) {
16+
let answer = array[0];
17+
array.forEach((a) => {
18+
if (a == array[0]) {
19+
return;
20+
} else {
21+
answer *= a;
22+
}
23+
});
24+
return answer;
25+
};
1626

17-
const power = function () {};
27+
const power = function (a, b) {
28+
return Math.pow(a, b);
29+
};
1830

19-
const factorial = function () {};
31+
const factorial = function (a) {
32+
let answer = 1;
33+
for (let i = 1; i <= a; i++) {
34+
answer *= i;
35+
}
36+
return answer;
37+
};
2038

2139
// Do not edit below this line
2240
module.exports = {

08_calculator/calculator.spec.js

Lines changed: 8 additions & 8 deletions
< 10000 td data-grid-cell-id="diff-11f5779dbb55e43b18be63be06b59d08a8d3c81169c4b47ed60ec3ef65b72acd-45-45-2" data-line-anchor="diff-11f5779dbb55e43b18be63be06b59d08a8d3c81169c4b47ed60ec3ef65b72acdR45" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">

Original file line numberDiff line numberDiff line change
@@ -39,39 +39,39 @@ describe("sum", () => {
3939
});
4040

4141
describe("multiply", () => {
42-
test.skip("multiplies two numbers", () => {
42+
test("multiplies two numbers", () => {
4343
expect(calculator.multiply([2, 4])).toBe(8);
4444
});
4545
46-
test.skip("multiplies several numbers", () => {
46+
test("multiplies several numbers", () => {
4747
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toBe(645120);
4848
});
4949
});
5050

5151
describe("power", () => {
52-
test.skip("raises one number to the power of another number", () => {
52+
test("raises one number to the power of another number", () => {
5353
expect(calculator.power(4, 3)).toBe(64); // 4 to third power is 64
5454
});
5555
});
5656

5757
describe("factorial", () => {
58-
test.skip("computes the factorial of 0", () => {
58+
test("computes the factorial of 0", () => {
5959
expect(calculator.factorial(0)).toBe(1); // 0! = 1
6060
});
6161

62-
test.skip("computes the factorial of 1", () => {
62+
test("computes the factorial of 1", () => {
6363
expect(calculator.factorial(1)).toBe(1);
6464
});
6565

66-
test.skip("computes the factorial of 2", () => {
66+
test("computes the factorial of 2", () => {
6767
expect(calculator.factorial(2)).toBe(2);
6868
});
6969

70-
test.skip("computes the factorial of 5", () => {
70+
test("computes the factorial of 5", () => {
7171
expect(calculator.factorial(5)).toBe(120);
7272
});
7373

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

0 commit comments

Comments
 (0)
0