8000 Finish calculator · iamjethrooo/javascript-exercises@a0b627b · GitHub
[go: up one dir, main page]

Skip to content

Commit a0b627b

Browse files
committed
Finish calculator
1 parent b90da0a commit a0b627b

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed

DONE/calculator/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The goal for this exercise is to create a calculator that does the following:
2+
3+
add, subtract, get the sum, multiply, get the power, and find the factorial
4+
5+
In order to do this please fill out each function with your solution. Make sure to return the value so you can test it in Jasmine! To see the expected value
6+
take a look at the spec file that houses the Jasmine test cases.

DONE/calculator/calculator.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function add (x, y) {
2+
return x + y;
3+
}
4+
5+
function subtract (x, y) {
6+
return x - y;
7+
}
8+
9+
function sum (array) {
10+
let sum = 0;
11+
array.forEach(number => {
12+
sum += number;
13+
});
14+
return sum;
15+
}
16+
17+
function multiply (array) {
18+
let product = 1;
19+
array.forEach(number => {
20+
product *= number;
21+
});
22+
return product;
23+
}
24+
25+
function power(x, y) {
26+
for (let i = y, n = x; i > 1; i--) {
27+
x *= n;
28+
}
29+
return x;
30+
}
31+
32+
function factorial(x) {
33+
return x > 0 ? x * factorial(x - 1) : 1;
34+
}
35+
36+
module.exports = {
37+
add,
38+
subtract,
39+
sum,
40+
multiply,
41+
power,
42+
factorial
43+
}

DONE/calculator/calculator.spec.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const calculator = require ('./calculator.js');
2+
3+
describe('add', function() {
4+
it('adds 0 and 0', function() {
5+
expect(calculator.add(0,0)).toEqual(0);
6+
});
7+
8+
it('adds 2 and 2', function() {
9+
expect(calculator.add(2,2)).toEqual(4);
10+
});
11+
12+
it('adds positive numbers', function() {
13+
expect(calculator.add(2,6)).toEqual(8);
14+
});
15+
});
16+
17+
describe('subtract', function() {
18+
it('subtracts numbers', function() {
19+
expect(calculator.subtract(10,4)).toEqual(6);
20+
});
21+
});
22+
23+
describe('sum', function() {
24+
it('computes the sum of an empty array', function() {
25+
expect(calculator.sum([])).toEqual(0);
26+
});
27+
28+
it('computes the sum of an array of one number', function() {
29+
expect(calculator.sum([7])).toEqual(7);
30+
});
31+
32+
it('computes the sum of an array of two numbers', function() {
33+
expect(calculator.sum([7,11])).toEqual(18);
34+
});
35+
36+
it('computes the sum of an array of many numbers', function() {
37+
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
38+
});
39+
});
40+
41+
describe('multiply', function() {
42+
it('multiplies two numbers', function() {
43+
expect(calculator.multiply([2,4])).toEqual(8);
44+
});
45+
46+
it('multiplies several numbers', function() {
47+
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
48+
});
49+
});
50+
51+
describe('power', function() {
52+
it('raises one number to the power of another number', function() {
53+
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
54+
});
55+
});
56+
57+
describe('factorial', function() {
58+
it('computes the factorial of 0', function() {
59+
expect(calculator.factorial(0)).toEqual(1); // 0! = 1
60+
});
61+
62+
it('computes the factorial of 1', function() {
63+
expect(calculator.factorial(1)).toEqual(1);
64+
});
65+
66+
it('computes the factorial of 2', function() {
67+
expect(calculator.factorial(2)).toEqual(2);
68+
});
69+
70+
it('computes the factorial of 5', function() {
71+
expect(calculator.factorial(5)).toEqual(120);
72+
});
73+
74+
it('computes the factorial of 10', function() {
75+
expect(calculator.factorial(10)).toEqual(3628800);
76+
});
77+
});

DONE/fibonacci/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Exercise XX - fibonacci
2+
3+
Create a function that returns a specific member of the fibonacci sequence:
4+
5+
> a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.
6+
7+
```javascript
8+
fibonacci(4) // returns the 4th member of the series: 3 (1, 1, 2, 3)
9+
fibonacci(6) // returns 8
10+
```

DONE/fibonacci/fibonacci.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fibonacci = function(number) {
2+
if (number < 1) {
3+
return "OOPS";
4+
}
5+
if (typeof number !== 'number') {
6+
number = parseInt(number);
7+
}
8+
9+
let fibonacciArray =[1, 1];
10+
for (let i = 2; i <= number; i++) {
11+
12+
fibonacciArray[i] = fibonacciArray[i-2] + fibonacciArray[i-1];
13+
}
14+
return fibonacciArray[number-1];
15+
16+
}
17+
18+
module.exports = fibonacci

DONE/fibonacci/fibonacci.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fibonacci = require('./fibonacci')
2+
3+
describe('fibonacci', function() {
4+
it('works', function() {
5+
expect(fibonacci(4)).toEqual(3);
6+
});
7+
it('works', function() {
8+
expect(fibonacci(6)).toEqual(8);
9+
});
10+
it('works', function() {
11+
expect(fibonacci(10)).toEqual(55);
12+
});
13+
it('works', function() {
14+
expect(fibonacci(15)).toEqual(610);
15+
});
16+
it('works', function() {
17+
expect(fibonacci(25)).toEqual(75025);
18+
});
19+
it('doesn\'t accept negatives', function() {
20+
expect(fibonacci(-25)).toEqual("OOPS");
21+
});
22+
it('DOES accept strings', function() {
23+
expect(fibonacci("1")).toEqual(1);
24+
});
25+
it('DOES accept strings', function() {
26+
expect(fibonacci("2")).toEqual(1);
27+
});
28+
it('DOES accept strings', function() {
29+
expect(fibonacci("8")).toEqual(21);
30+
});
31+
});

0 commit comments

Comments
 (0)
0