Skip to content

Commit cbd4994

Browse files
committed
finished calculator
1 parent 0747078 commit cbd4994

File tree

9 files changed

+7598
-88
lines changed

9 files changed

+7598
-88
lines changed

01_helloWorld/helloWorld.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const helloWorld = function() {
2-
return ''
1+
const helloWorld = function () {
2+
return 'Hello, World!'
33
};
44

55
module.exports = helloWorld;

02_repeatString/repeatString.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
const repeatString = function() {
2-
1+
const repeatString = function (content, times) {
2+
if (times < 0) {
3+
return "ERROR";
4+
}
5+
let result = "";
6+
for (i = 0; i < times; i++) {
7+
result += content;
8+
}
9+
return result;
310
};
411

512
// Do not edit below this line

02_repeatString/repeatString.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('repeatString', () => {
44
test('repeats the string', () => {
55
expect(repeatString('hey', 3)).toEqual('heyheyhey');
66
});
7-
test.skip('repeats the string many times', () => {
7+
test('repeats the string many times', () => {
88
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
99
});
10-
test.skip('repeats the string 1 times', () => {
10+
test('repeats the string 1 times', () => {
1111
expect(repeatString('hey', 1)).toEqual('hey');
1212
});
13-
test.skip('repeats the string 0 times', () => {
13+
test('repeats the string 0 times', () => {
1414
expect(repeatString('hey', 0)).toEqual('');
1515
});
16-
test.skip('returns ERROR with negative numbers', () => {
16+
test('returns ERROR with negative numbers', () => {
1717
expect(repeatString('hey', -1)).toEqual('ERROR');
1818
});
19-
test.skip('repeats the string a random amount of times', function () {
19+
test('repeats the string a random amount of times', function () {
2020
/*The number is generated by using Math.random to get a value from between
2121
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
2222
equals a number between 0 to 999 (this number will change everytime you run
@@ -31,7 +31,7 @@ describe('repeatString', () => {
3131
was randomly generated. */
3232
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number);
3333
});
34-
test.skip('works with blank strings', () => {
34+
test('works with blank strings', () => {
3535
expect(repeatString('', 10)).toEqual('');
3636
});
3737
});

03_reverseString/reverseString.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const reverseString = function() {
2-
1+
const reverseString = function (input) {
2+
let inputArray = input.split('');
3+
let reversedArray = inputArray.reverse();
4+
return reversedArray.join('');
35
};
46

57
// Do not edit below this line

03_reverseString/reverseString.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ describe('reverseString', () => {
55
expect(reverseString('hello')).toEqual('olleh');
66
});
77

8-
test.skip('reverses multiple words', () => {
8+
test('reverses multiple words', () => {
99
expect(reverseString('hello there')).toEqual('ereht olleh')
1010
})
1111

12-
test.skip('works with numbers and punctuation', () => {
12+
test('works with numbers and punctuation', () => {
1313
expect(reverseString('123! abc!')).toEqual('!cba !321')
1414
})
15-
test.skip('works with blank strings', () => {
15+
test('works with blank strings', () => {
1616
expect(reverseString('')).toEqual('')
1717
})
1818
});

04_removeFromArray/removeFromArray.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
const removeFromArray = function() {
1+
const removeFromArray = function (content, ...valuesToBeRemoved) {
2+
console.log(content);
3+
console.log(valuesToBeRemoved);
4+
valuesToBeRemoved.forEach(value => {
25

6+
})
37
};
48

59
// Do not edit below this line

08_calculator/calculator.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
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 (values) {
10+
return values.reduce(function (a, b) {
11+
return a + b;
12+
}, 0);
1113
};
1214

13-
const multiply = function() {
14-
15+
const multiply = function (values) {
16+
return values.reduce(function (a, b) {
17+
return a * b;
18+
}, 1);
1519
};
1620

17-
const power = function() {
18-
21+
const power = function (a, b) {
22+
let result = a;
23+
for (let i = 1; i < b; i++) {
24+
result = result * a;
25+
}
26+
return result;
1927
};
2028

21-
const factorial = function() {
22-
29+
const factorial = function (a) {
30+
let result = 1;
31+
while (a > 1) {
32+
result = result * a;
33+
a--;
34+
}
35+
return result;
2336
};
2437

2538
// Do not edit below this line
@@ -29,5 +42,5 @@ module.exports = {
2942
sum,
3043
multiply,
3144
power,
32-
factorial
45+
factorial,
3346
};

08_calculator/calculator.spec.js

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,77 @@
1-
const calculator = require('./calculator');
1+
const calculator = require("./calculator");
22

3-
describe('add', () => {
4-
test('adds 0 and 0', () => {
5-
expect(calculator.add(0,0)).toBe(0);
6-
});
3+
describe("add", () => {
4+
test("adds 0 and 0", () => {
5+
expect(calculator.add(0, 0)).toBe(0);
6+
});
77

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

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

17-
describe('subtract', () => {
18-
test.skip('subtracts numbers', () => {
19-
expect(calculator.subtract(10,4)).toBe(6);
20-
});
17+
describe("subtract", () => {
18+
test("subtracts numbers", () => {
19+
expect(calculator.subtract(10, 4)).toBe(6);
20+
});
2121
});
2222

23-
describe('sum', () => {
24-
test.skip('computes the sum of an empty array', () => {
25-
expect(calculator.sum([])).toBe(0);
26-
});
23+
describe("sum", () => {
24+
test("computes the sum of an empty array", () => {
25+
expect(calculator.sum([])).toBe(0);
26+
});
2727

28-
test.skip('computes the sum of an array of one number', () => {
29-
expect(calculator.sum([7])).toBe(7);
30-
});
28+
test("computes the sum of an array of one number", () => {
29+
expect(calculator.sum([7])).toBe(7);
30+
});
3131

32-
test.skip('computes the sum of an array of two numbers', () => {
33-
expect(calculator.sum([7,11])).toBe(18);
34-
});
32+
test("computes the sum of an array of two numbers", () => {
33+
expect(calculator.sum([7, 11])).toBe(18);
34+
});
3535

36-
test.skip('computes the sum of an array of many numbers', () => {
37-
expect(calculator.sum([1,3,5,7,9])).toBe(25);
38-
});
36+
test("computes the sum of an array of many numbers", () => {
37+
expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25);
38+
});
3939
});
4040

41-
describe('multiply', () => {
42-
test.skip('multiplies two numbers', () => {
43-
expect(calculator.multiply([2,4])).toBe(8);
44-
});
41+
describe("multiply", () => {
42+
test("multiplies two numbers", () => {
43+
expect(calculator.multiply([2, 4])).toBe(8);
44+
});
4545

46-
test.skip('multiplies several numbers', () => {
47-
expect(calculator.multiply([2,4,6,8,10,12,14])).toBe(645120);
48-
});
46+
test("multiplies several numbers", () => {
47+
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toBe(645120);
48+
});
4949
});
5050

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

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

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

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

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

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

0 commit comments

Comments
 (0)