Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81f3b3d

Browse files
committed
Added unit testing for week 2 and week 2 homework
1 parent bcf127e commit 81f3b3d

40 files changed

+6758
-46
lines changed

.eslintrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"browser": true,
44
"commonjs": true,
55
"es6": true,
6-
"node": true
6+
"node": true,
7+
"jest/globals": true
78
},
89
"parserOptions": {
910
"ecmaVersion": 2017,
@@ -12,6 +13,9 @@
1213
},
1314
"sourceType": "module"
1415
},
16+
"plugins": [
17+
"jest"
18+
],
1519
"extends": [
1620
"eslint:recommended"
1721
],

Week2/MAKEME.md

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@ Go through the `html-css`, `javascript1` and `javascript2` Github repositories o
2121

2222
_Deadline Wednesday_
2323

24-
**2.1** Say you would like to write a program that doubles the odd numbers in an array and throws away the even number.
24+
**2.1** Say you would like to write a program that doubles the odd numbers in an array and throws away the even numbers.
2525

2626
Your solution could be something like this:
2727

2828
```js
29-
const numbers = [1, 2, 3, 4];
30-
const newNumbers = [];
31-
32-
for (let i = 0; i < numbers.length; i++) {
33-
if (numbers[i] % 2 !== 0) {
34-
newNumbers.push(numbers[i] * 2);
29+
function doubleOddNumbers(numbers) {
30+
const newNumbers = [];
31+
for (let i = 0; i < numbers.length; i++) {
32+
if (numbers[i] % 2 !== 0) {
33+
newNumbers.push(numbers[i] * 2);
34+
}
3535
}
36+
return newNumbers;
3637
}
3738

38-
console.log('The doubled numbers are', newNumbers); // ==> [2, 6]
39-
39+
const myNumbers = [1, 2, 3, 4];
40+
console.log(doubleOddNumbers(myNumbers)); // ==> [2, 6]
4041
```
4142

42-
Rewrite the above program using `map` and `filter` don't forget to use `=>`.
43+
Rewrite the above `doubleOddNumbers` function using `map` and `filter`; don't forget to use `=>`.
4344

4445
---
4546

@@ -87,8 +88,6 @@ const tuesday = [
8788
duration: 40
8889
}
8990
];
90-
91-
const tasks = monday.concat(tuesday);
9291
```
9392

9493
_Note: the durations are specified in minutes._
@@ -99,16 +98,70 @@ Follow these steps. Each step should build on the result of the previous step.
9998

10099
- Map the tasks to durations in hours.
101100
- Filter out everything that took less than two hours (i.e., remove from the collection)
102-
- Multiply the each duration by a per-hour rate for billing (you can decide yourself what Maartje should earn per hour) and sum it all up.
101+
- Multiply the each duration by a per-hour rate for billing (assume €20/hour) and sum it all up.
103102
- Output a formatted Euro amount, rounded to Euro cents, e.g: `€ 12.34`.
104103
- Choose variable and parameters names that most accurately describe their contents or purpose. When naming an array, use a plural form, e.g. `durations`. For a single item, use a singular form, e.g. `duration`. For details, see [Naming Conventions](https://github.com/HackYourFuture/fundamentals/blob/master/fundamentals/naming_conventions.md).
105104
- Don't forget to use `=>`.
106105

107-
## Step 3: ROVER
106+
## Step 3: Testing your homework
107+
108+
We have provided _unit tests_ in this repo that allow you to verify that your homework produces the expected results.
109+
110+
> **Unit test**: A _unit test_ is a piece of code (usually a function) that invokes another piece of code and checks the correctness of some assumptions afterwards. If the assumptions turn out to be wrong, the unit test has failed. A 'unit' is a method or function.
111+
>
112+
> Adapted from: Roy Osherove (2009), The art of Unit Testing. Greenwich, CT: Manning.
113+
114+
At this point it is not important to understand how unit tests work. The only thing you need to know now is how to run the tests and how to determine whether your homework produces the correct results.
115+
116+
#### Installation
117+
118+
Before you can run the unit tests you need to install some additional software. You need to do this only once; there is no need to repeat it for the week 3 homework.
119+
120+
Open a terminal window. Make sure the current directory is the `JavaScript2` folder and type the following command:
121+
122+
```
123+
npm install
124+
```
125+
126+
This software installation might take a while.
127+
128+
#### Run the tests
129+
130+
Once the software installation has been completed, you can test your week 2 homework by typing this command in the terminal window:
131+
132+
```
133+
npm run test2
134+
```
135+
136+
You will see some output appearing in the console while the tests run. If all is well (no errors), the last couple of lines will look like this:
137+
138+
```
139+
Test Suites: 2 passed, 2 total
140+
Tests: 2 passed, 2 total
141+
Snapshots: 0 total
142+
Time: 1.849s
143+
Ran all test suites matching /Week2\//i.
144+
```
145+
146+
In case of unexpected results, say from _Maartjes work_ assignment, you might see something like this (you may need to scroll up a bit):
147+
148+
```
149+
Test Suites: 1 failed, 1 passed, 2 total
150+
Tests: 1 failed, 1 passed, 2 total
151+
Snapshots: 0 total
152+
Time: 2.255s
153+
Ran all test suites matching /Week2\//i.
154+
```
155+
156+
If that's the case, try and fix the error. When done, run the tests again: `npm run test2`
157+
158+
Repeat the previous step until all (= 2 in this case) tests pass.
159+
160+
## Step 4: ROVER
108161

109162
Finish up to chapter 7: JSON on [roverjs.com](http://roverjs.com/)!
110163

111-
## Step 4: **Some freeCodeCamp challenges:**
164+
## Step 5: **Some freeCodeCamp challenges:**
112165

113166
_Deadline Saturday_
114167

@@ -119,7 +172,7 @@ _Deadline Saturday_
119172
3. [Use the map Method to Extract Data from an Array](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array)
120173

121174

122-
## Step 5: Read before next lecture
175+
## Step 6: Read before next lecture
123176

124177
_Deadline Sunday morning_
125178

@@ -130,7 +183,7 @@ Go trough the reading material in the [README.md](/Week3/README.md) to prepare f
130183

131184
Go over your homework one last time:
132185

133-
- Does every file run without errors and with the correct results when you run them with Node?
186+
- Does your homework pass all the unit tests?
134187
- Does every file start with `'use strict';`?
135188
- Have you used `const` and `let` and avoided `var`?
136189
- Do the variable, function and argument names you created follow the [Naming Conventions](../../../../fundamentals/blob/master/fundamentals/naming_conventions.md)?
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
const monday = [
4+
{
5+
name: 'Write a summary HTML/CSS',
6+
duration: 180
7+
},
8+
{
9+
name: 'Some web development',
10+
duration: 120
11+
},
12+
{
13+
name: 'Fix homework for class10',
14+
duration: 20
15+
},
16+
{
17+
name: 'Talk to a lot of people',
18+
duration: 200
19+
}
20+
];
21+
22+
const tuesday = [
23+
{
24+
name: 'Keep writing summary',
25+
duration: 240
26+
},
27+
{
28+
name: 'Some more web development',
29+
duration: 180
30+
},
31+
{
32+
name: 'Staring out the window',
33+
duration: 10
34+
},
35+
{
36+
name: 'Talk to a lot of people',
37+
duration: 200
38+
},
39+
{
40+
name: 'Look at application assignments new students',
41+
duration: 40
42+
}
43+
];
44+
45+
const maartjesTasks = monday.concat(tuesday);
46+
const maartjesHourlyRate = 20;
47+
48+
function computeEarnings(tasks, hourlyRate) {
49+
return tasks
50+
.map(task => task.duration / 61)
51+
.filter(duration => duration >= 2)
52+
.map(duration => duration * hourlyRate)
53+
.reduce((total, amount) => total + amount, 0);
54+
}
55+
56+
const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate);
57+
console.log(`Maartje has earned €${earnings.toFixed(2)}`);
58+
59+
// Do not change or remove the next line
60+
module.exports = {
61+
maartjesTasks,
62+
maartjesHourlyRate,
63+
computeEarnings
64+
};
65+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
function doubleOddNumbers(numbers) {
4+
return numbers
5+
.filter(number => number % 2 !== 0)
6+
.map(number => number * 2);
7+
}
8+
9+
const myNumbers = [1, 2, 3, 4];
10+
console.log(doubleOddNumbers(myNumbers));
11+
12+
// Do not change or remove the next line
13+
module.exports = {
14+
myNumbers,
15+
doubleOddNumbers
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function doubleOddNumbers(numbers) {
2+
const newNumbers = [];
3+
for (let i = 0; i < numbers.length; i++) {
4+
if (numbers[i] % 2 !== 0) {
5+
newNumbers.push(numbers[i] * 2);
6+
}
7+
}
8+
return newNumbers;
9+
}
10+
11+
const myNumbers = [1, 2, 3, 4];
12+
console.log(doubleOddNumbers(myNumbers)); // ==> [2, 6]

Week2/homework/maartjes_work.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ const tuesday = [
4242
}
4343
];
4444

45-
const tasks = monday.concat(tuesday);
45+
const maartjesTasks = monday.concat(tuesday);
46+
const maartjesHourlyRate = 20;
4647

47-
// Add your code here
48+
function computeEarnings(tasks, hourlyRate) {
49+
// add your code here
50+
}
51+
52+
const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate);
53+
console.log(`Maartje has earned €${'replace this string with the earnings rounded to eurocents'}`);
54+
55+
// Do not change or remove the next lines
56+
module.exports = {
57+
maartjesTasks,
58+
maartjesHourlyRate,
59+
computeEarnings
60+
};

Week2/homework/map_filter.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
'use strict';
22

3-
const numbers = [1, 2, 3, 4];
3+
function doubleOddNumbers(numbers) {
4+
// add your code here
5+
}
46

5-
// Add your code here
7+
const myNumbers = [1, 2, 3, 4];
8+
console.log(doubleOddNumbers(myNumbers));
9+
10+
// Do not change or remove the next lines
11+
module.exports = {
12+
myNumbers,
13+
doubleOddNumbers
14+
};

Week2/test/maartjes_work.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { HOMEWORK_FOLDER } = require('../../test-config');
2+
const {
3+
maartjesTasks,
4+
maartjesHourlyRate,
5+
computeEarnings
6+
} = require(`../${HOMEWORK_FOLDER}/maartjes_work`);
7+
8+
test('maartjes_work.js', () => {
9+
10+
const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate);
11+
const result = earnings.toFixed(2);
12+
expect(result).toBe('373.33');
13+
});

Week2/test/map_filter.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { HOMEWORK_FOLDER } = require('../../test-config');
2+
const {
3+
myNumbers,
4+
doubleOddNumbers
5+
} = require(`../${HOMEWORK_FOLDER}/map_filter`);
6+
7+
test('map_filter.js', () => {
8+
9+
const result = doubleOddNumbers(myNumbers);
10+
expect(result).toEqual([2, 6]);
11+
});

Week3/MAKEME.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Both functions should be called if the array value is divisible by both 3 and 5.
7272

7373
```js
7474
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
75-
const values = [];
75+
const numbers = [];
7676
// make array
7777
// start at beginning of array and check if you should call threeCallback or fiveCallback or go on to next
7878
}
@@ -84,24 +84,25 @@ threeFive(10, 15, sayThree, sayFive);
8484
// please make sure you see why these calls are made before you start coding
8585
```
8686

87-
> Note: The following assignments include some problems from _freeCodeCamp_. While we normally ask you to use more modern `const` and `let` keywords to declare variables, currently _freeCodeCamp_ does not give you that option and expects you to use the older `var` keyword.
87+
> Note: The following assignments include some problems from _freeCodeCamp_. Note that some _freeCodeCamp_ examples still mention `var`. However you can safely replace them with `let` and `const` as appropriate.
8888
8989
**3.3** Please solve this problem:
9090

91-
> https://www.freecodecamp.com/challenges/repeat-a-string-repeat-a-string
91+
>[Basic Algorithm Scripting: Repeat a String Repeat a String](https://www.freecodecamp.com/challenges/repeat-a-string-repeat-a-string)
9292
9393
_3.3.1_: with a `for` loop.
9494
_3.3.2_: with a `while` loop.
9595
_3.3.3_: with a `do...while` loop.
9696

9797
**3.4** Some practice with objects:
9898

99-
>https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/define-a-constructor-function
100-
>https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/use-a-constructor-to-create-objects
99+
>[Object Oriented Programming: Define a Constructor Function](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/define-a-constructor-function)<br>
100+
[Object Oriented Programming: Use a Constructor to Create Objects](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/use-a-constructor-to-create-objects)
101101

102102
**3.5** Nested loops
103103

104-
> https://www.freecodecamp.com/challenges/nesting-for-loops
104+
>[Basic JavaScript: Nesting For Loops
105+
](https://www.freecodecamp.com/challenges/nesting-for-loops)
105106

106107
**3.6** We did some work with arrays:
107108

@@ -160,7 +161,21 @@ addSix(21); // returns 27
160161
__Bonus__: Write a function takes this array `['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c']` and returns an array which only has unique values in it (so it removes the duplicate ones). Make it a 'smart' algorithm that could do it for every array (only strings/number). Try to make it as fast as possible!
161162

162163

163-
## Step 5: Read before next lecture
164+
## Step 5: Run the unit tests
165+
166+
(See the week 2 MAKEME for detailed instructions.)
167+
168+
To run the unit test for the week 3 homework, open a terminal window in the `JavaScript2` folder and type
169+
170+
```
171+
npm run test3
172+
```
173+
174+
In case of errors, try and fix them. When done, run the tests again: `npm run test3`
175+
176+
Repeat the previous step until all tests pass.
177+
178+
## Step 6: Read before next lecture
164179

165180
_Deadline Sunday morning_
166181

@@ -171,7 +186,7 @@ Go trough the reading material in the [README.md](https://github.com/HackYourFut
171186

172187
Go over your homework one last time:
173188

174-
- Does every file run without errors and with the correct results when you run them with Node?
189+
- Does your homework pass all the unit tests?
175190
- Does every file start with `'use strict';`?
176191
- Have you used `const` and `let` and avoided `var`?
177192
- Do the variable, function and argument names you created follow the [Naming Conventions](../../../../fundamentals/blob/master/fundamentals/naming_conventions.md)?

0 commit comments

Comments
 (0)