Topics discussed this week:
• Functions + JSON/Arrays
• Array Manipulations
• JSON
• Map and filter
• Arrow functions
Here you find the readings you have to complete before the third lecture.
Deadline Wednesday
1.1 Say you would like to write a program that doubles the odd numbers in an array and throws away the even numbers.
Your solution could be something like this:
function doubleOddNumbers(numbers) {
const newNumbers = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 !== 0) {
newNumbers.push(numbers[i] * 2);
}
}
return newNumbers;
}
const myNumbers = [1, 2, 3, 4];
console.log(doubleOddNumbers(myNumbers)); // ==> [2, 6]
Rewrite the above doubleOddNumbers
function using map
and filter
; don't forget to use =>
.
1.2 Underneath you see a very interesting small insight in Maartje's work:
const monday = [
{
name: 'Write a summary HTML/CSS',
duration: 180,
},
{
name: 'Some web development',
duration: 120,
},
{
name: 'Fix homework for class10',
duration: 20,
},
{
name: 'Talk to a lot of people',
duration: 200,
},
];
const tuesday = [
{
name: 'Keep writing summary',
duration: 240,
},
{
name: 'Some more web development',
duration: 180,
},
{
name: 'Staring out the window',
duration: 10,
},
{
name: 'Talk to a lot of people',
duration: 200,
},
{
name: 'Look at application assignments new students',
duration: 40,
},
];
Note: the durations are specified in minutes.
Write a program that computes how much Maartje has earned by completing these tasks, using map
and filter
. For the 'summing part' you can try your luck with reduce
; alternatively, you may use forEach
or a for
loop.
Follow these steps. Each step should build on the result of the previous step.
- Map the tasks to durations in hours.
- Filter out everything that took less than two hours (i.e., remove from the collection)
- Multiply the each duration by a per-hour rate for billing (use €20/hour) and sum it all up.
- Output a formatted Euro amount, rounded to Euro cents, e.g:
€11.34
. - 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. - Don't forget to use
=>
.
We have provided unit tests in this repo that allow you to verify that your homework produces the expected results.
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.
Adapted from: Roy Osherove (1.09), The art of Unit Testing. Greenwich, CT: Manning.
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.
You can test your week 2 homework by typing this command in the terminal window:
npm run test-week2
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:
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.849s
Ran all test suites matching /Week2\//i.
In case of unexpected results, say from Maartjes work assignment, you might see something like this (you may need to scroll up a bit):
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 1.255s
Ran all test suites matching /Week2\//i.
If that's the case, try and fix the error. When done, run the tests again: npm run test-week2
Repeat the previous step until all (= 2 in this case) tests pass.
Finish up to chapter 7: JSON on roverjs.com!
Deadline Saturday
Deadline Sunday morning
Go trough the reading material in the README.md to prepare for your next class
Go over your homework one last time:
- Does your homework pass all the unit tests?
- Does every file start with
'use strict';
? - Do the variable, function and argument names you created follow the Naming Conventions?
- Have you resolved all issues flagged by ESLint and the spell checker (no wavy red and green underlines in VSCode)?
If the answer is 'yes' to all preceding questions you are ready to follow these instructions: