Skip to content

Commit c113fc8

Browse files
authored
Merge pull request #597 from mao-sz/restructure
Restructure: Group exercises by curriculum section
2 parents 4e821fb + 7a32f7c commit c113fc8

File tree

113 files changed

+783
-46
lines changed

Some content is hidden

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

113 files changed

+783
-46
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ jobs:
1212
- uses: actions/checkout@v4
1313
- uses: actions/setup-node@v4
1414
- run: npm install
15-
- run: npm run test -- **/solution
15+
- run: npm run test -- solution

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ If you have a suggestion to improve an exercise, an idea for a new exercise, or
2020
- A markdown file with a description of the task, an empty (or mostly empty) JavaScript file, and a set of tests.
2121
- A `solutions` directory that contains an example solution and the same test file with all of the tests unskipped.
2222

23-
To complete an exercise, you will need to go to the exercise directory with `cd exerciseName` in the terminal and run `npm test exerciseName.spec.js`. This should run the test file and show you the output. When you run a test for the first time, it will fail. This is by design! You must open the exercise file and write the code needed to get the test to pass.
23+
To complete an exercise, you will need to go to the exercise directory with `cd <path to file>` in the terminal and run `npm test exerciseName.spec.js`. For example, to go to the first Foundations exercise "helloWorld", you need to `cd foundations/01_helloWorld` then run `npm rest helloWorld`. This should run the test file and show you the output. When you run a test for the first time, it will fail. This is by design! You must open the exercise file and write the code needed to get the test to pass.
2424

2525
1. Some of the exercises have test conditions defined in their spec file as `test.skip` instead of `test`. This is intentional. Once all `test`s pass, you will change the next `test.skip` to `test` and test your code again. You will do this until all conditions are satisfied. **All tests must pass at the same time**, and you should not have any instances of `test.skip` in the spec file when you are finished with an exercise.
2626
1. Once you successfully finish an exercise, check the `solutions` directory within each exercise.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Exercise 1 - Factorial
2+
3+
Write a recursive [factorial](https://simple.wikipedia.org/wiki/Factorial) function that takes a non-negative integer, and returns the product of all positive integers less than or equal to the input integer. An input of `0` should return `1`. The function should only accept numbers, so `'4'` should not be accepted as it is a string. All invalid inputs should return `undefined`.
4+
5+
For example:
6+
7+
```javascript
8+
factorial(5); // 5 * 4 * 3 * 2 * 1, Output: 120
9+
factorial(0); // Output: 1
10+
factorial(7.2); // Output: undefined
11+
factorial('4'); // Output: undefined
12+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const factorial = function() {
2+
3+
};
4+
5+
// Do not edit below this line
6+
module.exports = factorial;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const factorial = require("./factorial");
2+
3+
describe('factorial', () => {
4+
test('4th factorial number is 24', () => {
5+
expect(factorial(4)).toBe(24);
6+
});
7+
test.skip('6th factorial number is 720', () => {
8+
expect(factorial(6)).toBe(720);
9+
});
10+
test.skip('10th factorial number is 3628800', () => {
11+
expect(factorial(10)).toBe(3628800);
12+
});
13+
test.skip('15th factorial number is 1307674368000', () => {
14+
expect(factorial(15)).toBe(1307674368000);
15+
});
16+
test.skip('25th factorial number is 1.5511210043330986e+25', () => {
17+
expect(factorial(25)).toBe(1.5511210043330986e+25);
18+
});
19+
test.skip('0th factorial number is 1', () => {
20+
expect(factorial(0)).toBe(1);
21+
});
22+
test.skip("doesn't accept negatives", () => {
23+
expect(factorial(-25)).toBe(undefined);
24+
});
25+
test.skip("doesn't accept floats", () => {
26+
expect(factorial(5.4)).toBe(undefined);
27+
});
28+
test.skip("doesn't accept a number as a string", () => {
29+
expect(factorial('5')).toBe(undefined);
30+
});
31+
test.skip("doesn't accept strings", () => {
32+
expect(factorial('foo')).toBe(undefined);
33+
});
34+
test.skip("doesn't accept arrays", () => {
35+
expect(factorial([5])).toBe(undefined);
36+
});
37+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const factorial = function(n) {
2+
if (!Number.isInteger(n) || n < 0) return;
3+
if (n === 0) return 1;
4+
return n * factorial(n - 1);
5+
};
6+
7+
module.exports = factorial;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const factorial = require("./factorial-solution");
2+
3+
describe('factorial', () => {
4+
test('4th factorial number is 24', () => {
5+
expect(factorial(4)).toBe(24);
6+
});
7+
test('6th factorial number is 720', () => {
8+
expect(factorial(6)).toBe(720);
9+
});
10+
test('10th factorial number is 3628800', () => {
11+
expect(factorial(10)).toBe(3628800);
12+
});
13+
test('15th factorial number is 1307674368000', () => {
14+
expect(factorial(15)).toBe(1307674368000);
15+
});
16+
test('25th factorial number is 1.5511210043330986e+25', () => {
17+
expect(factorial(25)).toBe(1.5511210043330986e+25);
18+
});
19+
test('0th factorial number is 1', () => {
20+
expect(factorial(0)).toBe(1);
21+
});
22+
test('doesn\'t accept negatives', () => {
23+
expect(factorial(-25)).toBe(undefined);
24+
});
25+
test('doesn\'t accept floats', () => {
26+
expect(factorial(5.4)).toBe(undefined);
27+
});
28+
test('doesn\'t accept a number as a string', () => {
29+
expect(factorial('5')).toBe(undefined);
30+
});
31+
test('doesn\'t accept strings', () => {
32+
expect(factorial('foo')).toBe(undefined);
33+
});
34+
test('doesn\'t accept arrays', () => {
35+
expect(factorial([5])).toBe(undefined);
36+
});
37+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Exercise 2 - contains
2+
3+
Write a function that searches for a value in a nested object. It returns true if the object contains that value.
4+
5+
Objects are compared by reference.
6+
7+
Examples:
8+
9+
```javascript
10+
contains({ foo: "foo" }, "bar") // false
11+
contains({ foo: { bar: "bar" } }, "bar") // true
12+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const contains = function() {
2+
3+
};
4+
5+
// Do not edit below this line
6+
module.exports = contains;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const contains = require("./contains");
2+
3+
describe("contains", () => {
4+
const meaningOfLifeArray = [42];
5+
const object = {
6+
data: {
7+
duplicate: "e",
8+
stuff: {
9+
thing: {
10+
banana: NaN,
11+
moreStuff: {
12+
something: "foo",
13+
answer: meaningOfLifeArray,
14+
},
15+
},
16+
},
17+
info: {
18+
duplicate: "e",
19+
magicNumber: 44,
20+
empty: null,
21+
},
22+
},
23+
};
24+
25+
test("true if the provided number is a value within the object", () => {
26+
expect(contains(object, 44)).toBe(true);
27+
});
28+
29+
test.skip("true if the provided string is a value within the object", () => {
30+
expect(contains(object, "foo")).toBe(true);
31+
});
32+
33+
test.skip("does not convert input string into a number when searching for a value within the object", () => {
34+
expect(contains(object, "44")).toBe(false);
35+
});
36+
37+
test.skip("false if the provided string is not a value within the object", () => {
38+
expect(contains(object, "bar")).toBe(false);
39+
});
40+
41+
test.skip("true if provided string is within the object, even if duplicated", () => {
42+
expect(contains(object, "e")).toBe(true);
43+
});
44+
45+
test.skip("true if the object contains the same object by reference", () => {
46+
expect(contains(object, meaningOfLifeArray)).toBe(true);
47+
});
48+
49+
test.skip("false if no matching object reference", () => {
50+
expect(contains(object, [42])).toBe(false);
51+
});
52+
53+
test.skip("true if NaN is a value within the object", () => {
54+
expect(contains(object, NaN)).toBe(true);
55+
});
56+
57+
test.skip("false if the provided value exists and is null", () => {
58+
expect(contains(object, null)).toBe(true);
59+
});
60+
});

0 commit comments

Comments
 (0)