Workshop to learn basic functional programming in JavaScript.
Please install dependencies first.
npm install
To launch the tests.
npm test
To lint sources.
npm run lint
To start program.
npm start
Please verify that after all exercises the linter should not return errors.
Write a simple test in file test/program.test.js to verify that the parameter is mutated with the function transformCheckpoint
in file src/program.js.
👉 function .eql
from Chai should help you.
You could use this example data to run your test :
{
id: 'whataw0nd3rful1d',
uuid: 'whataw0nd3rful1d',
address: 'unknown',
addressType: 'unknown',
connectable: true,
advertisement: {
localName: undefined,
txPowerLevel: undefined,
manufacturerData: undefined,
serviceData: [],
serviceUuids: [ 'abcd' ]
},
rssi: -66,
services: null,
state: 'outofcontrol'
}
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 1"
-
The function
transformCheckpoint
in file src/program.js is so impure, please change it to become a pure function. -
Update test created in exercise 1 if necessary.
-
Write a test to be sure that output has a different reference than input.
👉 function .cloneDeep
from Lodash should help you.
👉 function .equal
from Chai should help you.
Please make all tests pass.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 2"
Our program is better because we use more pure functions, but now the display in our CLI (Command Line Interface) is different than before when we use :
npm start
Please update the function run
in file src/program.js.
-
The display in CLI should be the same than before.
-
Use methods from Array.prototype to do it in a functional programming way.
Please make all tests pass.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 3"
Now we want to use Lodash to manipulate object (as a collection) in a functionnal programming way.
Use simple Lodash functions to refactor function showCheckpoint
in file src/program.js, especially the for loop.
Please make all tests pass.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 4"
With Lodash we want to facilitate the visualization of the calculate property "distance" for all the checkpoints.
-
We want to add an unit for the distances, add "m" for distances greater or equal than 1, otherwise add "cm" and multiply by 100. For examples 1.25 should display "1.25 m" and 0.72 should display "72 cm".
-
We want to round calculate property "distance" with a precision of 2 decimals. For examples 0.3421673604634194 should become 0.34 or 10.467388920465797 should become 10.47.
-
We want to sort checkpoints with calculate property "distance" in an ascending way.
Implement theses new features and be careful with the order to apply them.
Please make all tests pass.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 5"