Skip to content

Commit 4fca2e2

Browse files
authored
Merge pull request TheOdinProject#150 from TheOdinProject/jest-solutions
Implement Jest testing platform for all exercises (Solutions branch)
2 parents a2a7813 + fda6819 commit 4fca2e2

File tree

17 files changed

+7357
-41
lines changed

17 files changed

+7357
-41
lines changed

caesar/caesar.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
const caesar = function() {
1+
const caesar = function(string, shift) {
2+
return string
3+
.split("")
4+
.map(char => shiftChar(char, shift))
5+
.join("");
6+
};
7+
8+
const codeSet = code => (code < 97 ? 65 : 97);
9+
10+
// this function is just a fancy way of doing % so that it works with negative numbers
11+
// see this link for details:
12+
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
13+
const mod = (n, m) => (n % m + m) % m;
14+
15+
const shiftChar = (char, shift) => {
16+
const code = char.charCodeAt();
217

18+
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
19+
return String.fromCharCode(
20+
mod(code + shift - codeSet(code), 26) + codeSet(code)
21+
);
22+
}
23+
return char;
324
};
425

526
module.exports = caesar;

calculator/calculator.js

Lines changed: 27 additions & 11 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(array) {
10+
return array.reduce((total, current) => total + current, 0);
1111
};
1212

13-
const multiply = function() {
13+
const multiply = function(array) {
14+
return array.length
15+
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
16+
: 0;
17+
};
1418

19+
const power = function(a, b) {
20+
return Math.pow(a, b);
1521
};
1622

17-
const power = function() {
18-
23+
const factorial = function(n) {
24+
if (n === 0) return 1;
25+
let product = 1;
26+
for (let i = n; i > 0; i--) {
27+
product *= i;
28+
}
29+
return product;
1930
};
2031

21-
const factorial = function() {
22-
32+
// This is another implementation of Factorial that uses recursion
33+
// THANKS to @ThirtyThreeB!
34+
const recursiveFactorial = function(n) {
35+
if (n === 0) {
36+
return 1;
37+
}
38+
return n * recursiveFactorial (n-1);
2339
};
2440

2541
module.exports = {

fibonacci/fibonacci.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
const fibonacci = function() {
2-
1+
const fibonacci = function(count) {
2+
if (count < 0) return "OOPS";
3+
if (count === 0) return 0;
4+
let a = 0;
5+
let b = 1;
6+
for (let i = 1; i < count; i++) {
7+
const temp = b;
8+
b = a + b;
9+
a = temp;
10+
}
11+
return b;
312
};
413

514
module.exports = fibonacci;

findTheOldest/findTheOldest.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
const findTheOldest = function() {
1+
const findTheOldest = function(array) {
2+
return array.reduce((oldest, currentPerson) => {
3+
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath)
4+
const currentAge = getAge(currentPerson.yearOfBirth, currentPerson.yearOfDeath)
5+
return oldestAge < currentAge ? currentPerson : oldest
6+
})
7+
};
28

9+
const getAge = function(birth, death) {
10+
if (!death) {
11+
death = new Date().getFullYear();
12+
}
13+
return death - birth;
314
};
415

516
module.exports = findTheOldest;

getTheTitles/getTheTitles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const getTheTitles = function() {
2-
1+
const getTheTitles = function(array) {
2+
return array.map(book => book.title)
33
};
44

55
module.exports = getTheTitles;

helloWorld/helloWorld.js

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

55
module.exports = helloWorld;

leapYears/leapYears.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const leapYears = function() {
2-
1+
const leapYears = function(year) {
2+
return year % 4 === 0 && ( year % 100 !== 0 || year % 400 === 0)
33
};
44

55
module.exports = leapYears;

0 commit comments

Comments
 (0)