Each of the following helper functions has one or more bugs in their implementation.
-
countPages(itemCount, itemsPerPage)
fromnumbers.js
accepts a non-negative integeritemCount
and a positive integeritemsPerPage
and returns the number of pages thatitemCount
items would require to display, assuming every page can display a maximum ofitemsPerPage
items. Example: ifitemCount === 18
anditemsPerPage === 5
, the function should return4
(as 3 pages can display no more than 15 items, but 4 pages can display as many as 20). -
generateDistinct(nums)
fromnumbers.js
accepts an array of numbers as its single parameter and returns an arbitrary number that this array does not include. Example:generateDistinct([2, 1, 5]) === 4
would be fine, as 4 is distinct from 2, 1, and 5. -
generateLarger(nums)
fromnumbers.js
accepts a non-empty array of numbers as its single parameter and returns an arbitrary number that is larger than any number in the array. Example:generateLarger([2, 1, 5]) === 8
would be fine, as 8 is larger than 2, 1, and 5. -
generateSmaller(nums)
fromnumbers.js
accepts a non-empty array of numbers as its single parameter and returns an arbitrary number that is smaller than any number in the array. Example:generateSmaller([2, 1, 5]) === 0
would be fine, as 0 is smaller than 2, 1, and 5. -
generateRandomInt(from, to)
fromnumbers.js
returns a random integer betweenfrom
andto
, inclusive. As this method returns random values, make sure to call it not once but several times with each combination of parameters. -
parseYear(year)
fromstrings.js
accepts a string as its only parameter and interprets it as a year: 4-character numeral strings are taken to mean the year itself; 2-character numeral strings are taken to mean a 1900s year; other strings will throw an error. Examples:parseYear('9876') === 9876
;parseYear('53') === 1953
;parseYear('12345')
andparseYear('two thousand')
should both throw an error. -
generateRandomTime()
fromstrings.js
returns a random time string between'00:00'
and'23:59'
. It depends ongenerateRandomInt
and might appear to work correctly while that function has an unfixed bug. -
createStampedObject(object)
fromobjects.js
accepts an object as its single parameter and returns an object which has all ofobject
’s contents and additionally a field calledstamped
set totrue
. The function is not supposed to mutate the initialobject
. -
stampObject(object)
fromobjects.js
accepts an object as its single parameter, mutates it by setting itsstamped
property totrue
and returns this object. -
getOwnProperty(object, property)
fromobjects.js
returns the own value of theproperty
that theobject
has. Examples: ifobject === {hello: 'world', __proto__: {foo: 'bar'}}
, thengetOwnProperty(object, 'hello')
is'world'
, whilegetOwnProperty(object, 'foo')
isundefined
. -
findItem(items, id)
fromarrays.js
accepts an array of items, each of which should be an object that contains anid
field, and andid
to search for. It returns the first item with thisid
ornull
if no items have suchid
. Examples: ifitems === [{name: 'Foo', id: 5}, {name: 'Bar', id: 8}]
, thenfindItem(items, 8)
is{name: 'Bar', id: 8}
, whilefindItem(items, 6)
isnull
. -
makeArray(...)
fromarrays.js
accepts any number of parameters and transforms them into an array. Example:makeArray(2, 1, 5, 'foo')
is[2, 1, 5, 'foo']
.
Your task is:
- Without looking at the actual implementations, create unit tests that would fully cover all use cases for the function you’re testing. Apart from where it is stated otherwise, you don’t need to test how a function will react to an invalid input.
- Run the tests, make sure that they indeed fail, and verify that it’s a problem in the helpers’ code, not in tests. If some of the helpers pass every test, try to come up with some more edge cases and then repeat this step (make an exception for
generateRandomTime
which will appear to work correctly until you fixgenerateRandomInt
). - Fix the problems in the helpers’ code and run the tests again. Make sure that they now pass, except for
generateRandomTime
, which should fail. - Fix the implementation of
generateRandomTime
and make sure that all tests now pass.
You can suppress particular ESLint rules in the helpers’ code if you feel it would be impossible to implement them without breaking the rules.
- Fork the repository with this task.
- Clone forked repository:
git clone [email protected]:<user_name>/<task_repository>.git
- Run
npm install
to install dependencies. - Work on the task.
- Use the
npm test
command to run the tests that you have created. You would not need to runnpm start
for this task. - Follow the simplified JS styleguide.
- Before committing, use
npm run lint
to check your code for any styling issues. - Commit and push.
- Create
Pull Request
from the forked repo to the original one. - Add the link to the pull request to your Google spreadsheet.
In this task, you will only be working with the src/helpers
folder.