Skip to content

Commit e6a0bb0

Browse files
author
xandora
authored
Merge branch 'solutions' into master
2 parents 42076e7 + 9a40c04 commit e6a0bb0

File tree

2,939 files changed

+303672
-173
lines changed

Some content is hidden

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

2,939 files changed

+303672
-173
lines changed

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
"extends": "airbnb-base",
3+
"plugins": [
4+
"import"
5+
]
6+
};

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
.vscode
1+
.vscode
2+
node_modules
3+
.eslintrc
4+
.DS_Store

caesar/caesar.js

Lines changed: 24 additions & 3 deletions
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+
};
27

3-
}
8+
const codeSet = code => (code < 97 ? 65 : 97);
49

5-
module.exports = caesar
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();
17+
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;
24+
};
25+
26+
module.exports = caesar;

caesar/caesar.spec.js

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

3-
describe('caesar', function() {
4-
it('works with single letters', function() {
5-
expect(caesar('A', 1)).toEqual('B');
3+
describe("caesar", () => {
4+
it("works with single letters", () => {
5+
expect(caesar("A", 1)).toEqual("B");
66
});
7-
xit('works with words', function() {
8-
expect(caesar('Aaa', 1)).toEqual('Bbb');
7+
it("works with words", () => {
8+
expect(caesar("Aaa", 1)).toEqual("Bbb");
99
});
10-
xit('works with phrases', function() {
11-
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
10+
it("works with phrases", () => {
11+
expect(caesar("Hello, World!", 5)).toEqual("Mjqqt, Btwqi!");
1212
});
13-
xit('works with negative shift', function() {
14-
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
13+
it("works with negative shift", () => {
14+
expect(caesar("Mjqqt, Btwqi!", -5)).toEqual("Hello, World!");
1515
});
16-
xit('wraps', function() {
17-
expect(caesar('Z', 1)).toEqual('A');
16+
it("wraps", () => {
17+
expect(caesar("Z", 1)).toEqual("A");
1818
});
19-
xit('works with large shift factors', function() {
20-
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
19+
it("works with large shift factors", () => {
20+
expect(caesar("Hello, World!", 75)).toEqual("Ebiil, Tloia!");
2121
});
2222
xit('works with large negative shift factors', function() {
2323
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');

calculator/calculator.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
1-
function add () {
2-
1+
function add(a, b) {
2+
return a + b;
33
}
44

5-
function subtract () {
6-
5+
function subtract(a, b) {
6+
return a - b;
77
}
88

9-
function sum () {
10-
9+
function sum(array) {
10+
return array.reduce((total, current) => total + current, 0);
1111
}
1212

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

17-
function power() {
18-
19+
function power(a, b) {
20+
return Math.pow(a, b);
1921
}
2022

21-
function factorial() {
22-
23+
function factorial(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;
30+
}
31+
32+
// This is another implementation of Factorial that uses recursion
33+
// THANKS to @ThirtyThreeB!
34+
function recursiveFactorial(n) {
35+
if (n===0){
36+
return 1;
37+
}
38+
return n * recursiveFactorial (n-1);
2339
}
2440

2541
module.exports = {
26-
add,
27-
subtract,
28-
sum,
29-
multiply,
30-
power,
31-
factorial
32-
}
42+
add,
43+
subtract,
44+
sum,
45+
multiply,
46+
power,
47+
factorial
48+
};

fibonacci/fibonacci.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
const fibonacci = function() {
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;
12+
};
213

3-
}
4-
5-
module.exports = fibonacci
14+
module.exports = fibonacci;

fibonacci/fibonacci.spec.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
const fibonacci = require('./fibonacci')
1+
const fibonacci = require("./fibonacci");
22

3-
describe('fibonacci', function() {
4-
it('works', function() {
3+
describe("fibonacci", () => {
4+
it("works", () => {
55
expect(fibonacci(4)).toEqual(3);
66
});
7-
xit('works', function() {
7+
it("works", () => {
88
expect(fibonacci(6)).toEqual(8);
99
});
10-
xit('works', function() {
10+
it("works", () => {
1111
expect(fibonacci(10)).toEqual(55);
1212
});
13-
xit('works', function() {
13+
it("works", () => {
1414
expect(fibonacci(15)).toEqual(610);
1515
});
16-
xit('works', function() {
16+
it("works", () => {
1717
expect(fibonacci(25)).toEqual(75025);
1818
});
19-
xit('doesn\'t accept negatives', function() {
19+
it("doesn't accept negatives", () => {
2020
expect(fibonacci(-25)).toEqual("OOPS");
2121
});
22-
xit('DOES accept strings', function() {
23-
expect(fibonacci("1")).toEqual(1);
24-
});
25-
xit('DOES accept strings', function() {
26-
expect(fibonacci("2")).toEqual(1);
27-
});
28-
xit('DOES accept strings', function() {
22+
it("DOES accept strings", () => {
2923
expect(fibonacci("8")).toEqual(21);
3024
});
3125
});

findTheOldest/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ given an array of objects representing people with a birth and death year, retur
77
- this can be done with a couple of chained array methods, or by using `reduce`.
88
- One of the tests checks for people with no death-date.. use JavaScript's Date function to get their age as of today.
99

10+

findTheOldest/findTheOldest.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
let 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

findTheOldest/findTheOldest.spec.js

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

6363
});
64+

getTheTitles/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Get the Titles!
1+
# GET THE TITLES!
22

33
You are given an array of objects that represent books with an author and a title that looks like this:
44

@@ -24,3 +24,4 @@ getTheTitles(books) // ['Book','Book2']
2424
## Hints
2525

2626
- You should use a built-in javascript method to do most of the work for you!
27+
<Paste>

getTheTitles/getTheTitles.js

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

5-
module.exports = getTheTitles;
5+
module.exports = getTheTitles

getTheTitles/getTheTitles.spec.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
let getTheTitles = require('./getTheTitles')
22

33
describe('getTheTitles', function() {
4-
const books = [
5-
{
6-
title: 'Book',
7-
author: 'Name'
8-
},
9-
{
10-
title: 'Book2',
11-
author: 'Name2'
12-
}
13-
]
4+
const books = [
5+
{
6+
title: 'Book',
7+
author: 'Name'
8+
},
9+
{
10+
title: 'Book2',
11+
author: 'Name2'
12+
}
13+
]
1414

1515
it('gets titles', function() {
1616
expect(getTheTitles(books)).toEqual(['Book','Book2']);

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+
var helloWorld = function() {
2+
return 'Hello, World!'
33
}
44

55
module.exports = helloWorld

leapYears/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
Create a function that determines whether or not a given year is a leap year. Leap years are determined by the following rules:
44

5-
> Leap years are years divisible by four (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400 (like 1600 and 2000, which were in fact leap years). (Yes, it's all pretty confusing)
6-
>
7-
> -- <cite>[Learn to Program](https://pine.fm/LearnToProgram/chap_06.html) by Chris Pine</cite>
5+
>There is a leap year every year whose number is perfectly divisible by four - except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400. The second part of the rule affects century years. For example; the century years 1600 and 2000 are leap years, but the century years 1700, 1800, and 1900 are not.
86
97
```javascript
108
leapYears(2000) // is a leap year: returns true

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+
var leapYears = function(year) {
2+
return year % 4 === 0 && ( year % 100 !== 0 || year % 400 == 0)
33
}
44

55
module.exports = leapYears

leapYears/leapYears.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('leapYears', function() {
44
it('works with non century years', function() {
55
expect(leapYears(1996)).toEqual(true);
66
});
7-
xit('works with non century years', function() {
7+
it('works with non century years', function() {
88
expect(leapYears(1997)).toEqual(false);
99
});
10-
xit('works with ridiculously futuristic non century years', function() {
10+
it('works with ridiculously futuristic non century years', function() {
1111
expect(leapYears(34992)).toEqual(true);
1212
});
13-
xit('works with century years', function() {
13+
it('works with century years', function() {
1414
expect(leapYears(1900)).toEqual(false);
1515
});
16-
xit('works with century years', function() {
16+
it('works with century years', function() {
1717
expect(leapYears(1600)).toEqual(true);
1818
});
19-
xit('works with century years', function() {
19+
it('works with century years', function() {
2020
expect(leapYears(700)).toEqual(false);
2121
});
2222
});

node_modules/.bin/acorn

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/eslint

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/esparse

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/esvalidate

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/js-yaml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mkdirp

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/rimraf

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/semver

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/which

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)