All exercises have been completed and checked using the Jest testing library.
To run the tests yourself, it is required:
-
Download all files from repo (feature branch) to your local;
-
Run command in terminal from project folder to install all node modules:
npm i
- Run command to start tests:
npm test
More information about solutions and tests presented in the documentation.
- Complete 23 tasks from the file.
For the correct solving of all exercises, conditions were set under which the task would be considered successfully completed:
- The function returns the required value from the example;
- The function works correctly with boundary values;
- The function works correctly with empty values ([] or "");
- The function works correctly when changing the order of numbers (for array) or when changing the case of letters (for string), etc;
- The function should be pure - the input values should not change after using the function.
To fulfill all the criteria, more than 100 autotests have been developed using the Jest library, which check all the necessary conditions.
For some functions, no conditions were set for specific values. Therefore, it was decided to develop various exceptions and perform some additional checks by myself.
In these exercises, the .reduce()
method was used, since Math.min()
must be used in the next functions.
The task says that it is necessary to leave strings shorter than 20 characters. Therefore, it was decided that a string of exactly 20 characters in length would not remain in the array.
Example:
//input:
const strings = ['20 characters 20 cha'];
//output
const expected = [];
If we use Math.min
or Math.max
on an empty array with the spread operator,
then it will return us Infinity:
console.log(Math.min(...[])); // Infinity
console.log(Math.max(...[])); // -Infinity
Therefore, it was decided to check the length of the array before searching for the required value.
In case of an empty array, undefined
is returned
Example:
...
return numbers.length ? Math.min(...numbers) : undefined
...
There are situations when a person does not have a patronymic or does not want to give a surname. Therefore, instead of simply concatenating the fields, it was decided to check for their existence:
return 'Name: '
+ (user.firstName ? user.firstName + ' ' : '')
+ (user.patronymic ? user.patronymic + ' ' : '')
+ (user.secondName ? user.secondName : '')
Before counting vowels in string, it is necessary to change string register so that such problems do not occur:
['a', 'e', 'i', 'o', 'u'].includes(['A']) //false
Example:
[...string.toLowerCase()]...
In order for the function to be pure and not change the input data, it is necessary to monitor the methods used.
.map()
method does not modify the array, but returns a new one. Therefore, it can be used:
return numbers.map(row => Math.min(...row))
But .sort()
modify original array, therefore it is necessary to use the spread operator:
return [...numbers].sort...
All tests are presented in the tests/functions.test.js
file.
Almost every test checks the conditions:
- Call function with example values;
- Call function with empty values;
- Call function with boundary Values;
- Function is pure.
All implemented tests were successfully passed:
Description: A branch with solved tasks and Jest autotests.
To run the tests yourself, it is required:
-
Download all files from repo to your local;
-
Run command in terminal from project folder to install all node modules:
npm i
- Run command to start tests:
npm test
TG: @mordvintsevmv
e-mail: mordvintsevmv@gmail.com