8000 First commit of practice exercises · giegieg/javascript-exercises@aa0d69d · GitHub
[go: up one dir, main page]

< 8000 a href="#start-of-content" data-skip-target-assigned="false" class="px-2 py-4 color-bg-accent-emphasis color-fg-on-emphasis show-on-focus js-skip-to-content">Skip to content

Commit aa0d69d

Browse files
committed
First commit of practice exercises
1 parent efe1fe9 commit aa0d69d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+558
-2181
lines changed

.DS_Store

10 KB
Binary file not shown.

caesar/.DS_Store

100644100755
File mode changed.

caesar/README.md

100644100755
File mode changed.

caesar/caesar.js

100644100755
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
const caesar = function() {
1+
const capitals = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
22

3+
const caesar = function(str, shift) {
4+
let number = capitals.indexOf(str);
5+
return capitals[number+shift] ;
6+
37
}
48

9+
// const caesar = function(str, shift) {
10+
// let result = "";
11+
// for(let i=0; i<str.length; i++) {
12+
// let letterNum = str.charCodeAt(i);
13+
// letterNum += shift;
14+
// result = result + String.fromCharCode(letterNum);
15+
// }
16+
// return result;
17+
// }
18+
519
module.exports = caesar

caesar/caesar.spec.js

100644100755
File mode changed.

calculator/README.md

100644100755
File mode changed.

calculator/calculator.js

100644100755
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
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 (arr) {
10+
total=0;
11+
for (let elem of arr) {
12+
total += elem;
13+
}
14+
return total;
1115
}
1216

13-
function multiply () {
14-
17+
function multiply (arr) {
18+
product = 1;
19+
for (let elem of arr) {
20+
product *= elem;
21+
}
22+
return product;
1523
}
1624

17-
function power() {
18-
25+
function power(x,y) {
26+
return(Math.pow(x,y));
1927
}
2028

21-
function factorial() {
22-
29+
function factorial(a) {
30+
answer = 1;
31+
for (i=a; i>0; i--) {
32+
answer *= i;
33+
}
34+
return answer;
2335
}
2436

2537
module.exports = {

calculator/calculator.spec.js

100644100755
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', function() {
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
});

fibonacci/README.md

100644100755
File mode changed.

fibonacci/fibonacci.js

100644100755
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
const fibonacci = function() {
1+
// const fibonacci = function(x) {
2+
// if(x<0) {
3+
// return("OOPS");
4+
// }
5+
// let sequence = [1,1];
6+
// for(let i=2; i<x; i++) {
7+
// var currentNum = sequence[i-2] + sequence[i-1];
8+
// sequence.push(currentNum);
9+
// }
10+
// return currentNum;
11+
// }
212

3-
}
413

14+
const fibonacci = function(x) {
15+
if(x<0) {
16+
return("OOPS");
17+
}
18+
let currentNum = 0;
19+
let prevMem = 1;
20+
let prevPrevMem = 1;
21+
for (let i=2; i<x; i++) {
22+
currentNum=prevMem+prevPrevMem;
23+
prevPrevMem = prevMem;
24+
prevMem = currentNum;
25+
}
26+
return currentNum;
27+
}
528
module.exports = fibonacci

0 commit comments

Comments
 (0)
0