8000 Finished exercises per Fundamentals Part 4 on The Odin Project · jinc1026/javascript-exercises@213fbc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 213fbc0

Browse files
committed
Finished exercises per Fundamentals Part 4 on The Odin Project
1 parent b861c90 commit 213fbc0

File tree

9 files changed

+115
-47
lines changed

9 files changed

+115
-47
lines changed

calculator/calculator.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
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(a) {
10+
let sumTotal = 0;
11+
a.forEach(item => sumTotal+=item);
12+
return sumTotal;
1113
};
1214

13-
const multiply = function() {
14-
15+
const multiply = function(a) {
16+
let multiplyTotal = 1;
17+
if (a === []){
18+
return 0;
19+
}
20+
a.forEach(item => multiplyTotal *= item);
21+
return multiplyTotal;
1522
};
1623

17-
const power = function() {
18-
24+
const power = function(a, b) {
25+
let powerTotal = 1;
26+
for (let i=0; i<b; i++){
27+
powerTotal *=a;
28+
}
29+
return powerTotal;
1930
};
2031

21-
const factorial = function() {
22-
32+
const factorial = function(a) {
33+
let factorialTotal = 1;
34+
while (a>0){
35+
factorialTotal *= a;
36+
a -= 1;
37+
}
38+
return factorialTotal;
2339
};
2440

2541
module.exports = {

calculator/calculator.spec.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,73 @@ 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
});
2222

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

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

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

36-
test.skip('computes the sum of an array of many numbers', () => {
36+
test('computes the sum of an array of many numbers', () => {
3737
expect(calculator.sum([1,3,5,7,9])).toBe(25);
3838
});
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
});

fibonacci/fibonacci.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
const fibonacci = function() {
2-
1+
const fibonacci = function(num) {
2+
if (num<0){
3+
return "OOPS";
4+
} else if (num === 1 || num === 0){
5+
return num;
6+
} else {
7+
return fibonacci(num-1) + fibonacci(num-2);
8+
}
9+
10+
311
};
412

513
module.exports = fibonacci;

fibonacci/fibonacci.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('fibonacci', () => {
44
test('4th fibonacci number is 3', () => {
55
expect(fibonacci(4)).toBe(3);
66
});
7-
test.skip('6th fibonacci number is 8', () => {
7+
test('6th fibonacci number is 8', () => {
88
expect(fibonacci(6)).toBe(8);
99
});
10-
test.skip('10th fibonacci number is 55', () => {
10+
test('10th fibonacci number is 55', () => {
1111
expect(fibonacci(10)).toBe(55);
1212
});
13-
test.skip('15th fibonacci number is 610', () => {
13+
test('15th fibonacci number is 610', () => {
1414
expect(fibonacci(15)).toBe(610);
1515
});
16-
test.skip('25th fibonacci number is 75025', () => {
16+
test('25th fibonacci number is 75025', () => {
1717
expect(fibonacci(25)).toBe(75025);
1818
});
19-
test.skip('doesn\'t accept negatives', () => {
19+
test('doesn\'t accept negatives', () => {
2020
expect(fibonacci(-25)).toBe("OOPS");
2121
});
2222
test.skip('DOES accept strings', () => {

findTheOldest/findTheOldest.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1-
const findTheOldest = function() {
2-
1+
const findTheOldest = function(people) {
2+
const sortedPeople = people.sort(function(personA,personB){
3+
//check if yearOfDeath is undefined
4+
const currentYear = new Date().getFullYear();
5+
let ageOfPersonA;
6+
let ageOfPersonB;
7+
if (personA["yearOfDeath"] === undefined){
8+
ageOfPersonA = currentYear - personA.yearOfBirth;
9+
} else {
10+
ageOfPersonA = personA.yearOfDeath - personA.yearOfBirth;
11+
}
12+
13+
if (personB["yearOfDeath"] === undefined){
14+
ageOfPersonB = currentYear - personB.yearOfBirth;
15+
} else {
16+
ageOfPersonB = personB.yearOfDeath - personB.yearOfBirth;
17+
}
18+
19+
20+
if (ageOfPersonA > ageOfPersonB){
21+
return -1;
22+
} else {
23+
return 1;
24+
}
25+
});
26+
27+
return sortedPeople[0];
328
};
429

530
module.exports = findTheOldest;

findTheOldest/findTheOldest.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('findTheOldest', () => {
2121
]
2222
expect(findTheOldest(people).name).toBe('Ray');
2323
});
24-
test.skip('finds the oldest person if someone is still living', () => {
24+
test('finds the oldest person if someone is still living', () => {
2525
const people = [
2626
{
2727
name: "Carly",
@@ -40,7 +40,7 @@ describe('findTheOldest', () => {
4040
]
4141
expect(findTheOldest(people).name).toBe('Ray');
4242
});
43-
test.skip('finds the oldest person if the OLDEST is still living', () => {
43+
test('finds the oldest person if the OLDEST is still living', () => {
4444
const people = [
4545
{
4646
name: "Carly",

getTheTitles/getTheTitles.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
const getTheTitles = function() {
2-
1+
const getTheTitles = function(books) {
2+
const titles = books.map(function(book){
3+
return book.title;
4+
});
5+
6+
return titles;
37
};
48

59
module.exports = getTheTitles;

palindromes/palindromes.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
const palindromes = function () {
2-
1+
const palindromes = function (phrase) {
2+
let revised = '';
3+
for (let i=0; i<phrase.length ;i++){
4+
if (phrase.charAt(i) !== ' ' && phrase.charAt(i) !== '?'
5+
&& phrase.charAt(i) !== '!' && phrase.charAt(i) !== ','
6+
&& phrase.charAt(i) !== '.') {
7+
revised = revised.concat(phrase.charAt(i));
8+
}
9+
}
10+
revised = revised.toLowerCase();
11+
12+
let reversed = '';
13+
for (let j=revised.length-1; j>=0 ; j--){
14+
reversed = reversed.concat(revised.charAt(j));
15+
}
16+
17+
return (revised === reversed);
318
};
419

520
module.exports = palindromes;

palindromes/palindromes.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('palindromes', () => {
44
test('works with single words', () => {
55
expect(palindromes('racecar')).toBe(true);
66
});
7-
test.skip('works with punctuation ', () => {
7+
test('works with punctuation ', () => {
88
expect(palindromes('racecar!')).toBe(true);
99
});
10-
test.skip('works with upper-case letters ', () => {
10+
test('works with upper-case letters ', () => {
1111
expect(palindromes('Racecar!')).toBe(true);
1212
});
13-
test.skip('works with multiple words', () => {
13+
test('works with multiple words', () => {
1414
expect(palindromes('A car, a man, a maraca.')).toBe(true);
1515
});
16-
test.skip('works with multiple words', () => {
16+
test('works with multiple words', () => {
1717
expect(palindromes('Animal loots foliated detail of stool lamina.')).toBe(true);
1818
});
19-
test.skip('doesn\'t just always return true', () => {
19+
test('doesn\'t just always return true', () => {
2020
expect(palindromes('ZZZZ car, a man, a maraca.')).toBe(false);
2121
});
2222
});

0 commit comments

Comments
 (0)
0