Project - batteries.js
When starting to code in a standard JavaScript environment, batteries are not included, so to speak. In this project, you will make the batteries so that the next projects are easier from the start. The goal is to create a module with many useful functions that can be used with future projects and code. The result should be a single file batteries.js that exports the following functions. This module will act as a library you can use (import) in other projects.
All of the functions below should be implemented in batteries.js and exported. batteries.js should not import anything (the module should not depend on anything else). Along the way, it may be helpful to use console.log for testing and debugging. However, the end result should not have any console.log lines as they will be executed whenever the module is imported.
Functions
Numbers
- isEven
- Arguments:
n(number) - Return: true if
nis even, otherwise false
- Arguments:
- isOdd
- Arguments:
n(number) - Return: true if
nis odd, otherwise false
- Arguments:
- divisibleBy
- Arguments:
x(number),y(number) - Return: true if
xis divisible byy, otherwise false
- Arguments:
Arrays
- eqArrays
- Arguments:
xs(array),ys(array) - Return: true if
xsandysare the same length and all of their elements are equal, otherwise false
- Arguments:
- buildArray
- Arguments:
size(number) - Return: an array of size
size(all elements can beundefined)
- Arguments:
- append
- Arguments:
y(any),xs(array) - Return: array with
yappended toxs
- Arguments:
- head
- Arguments:
xs(array) - Return: the first element of
xs
- Arguments:
- tail
- Arguments:
xs(array) - Return: an array containing all of the elements of
xsexcept the first one
- Arguments:
- last
- Arguments:
xs(array) - Return: the last element of
xs
- Arguments:
- init
- Arguments:
xs(array) - Return: an array containing all of the elements of
xsexcept the last one
- Arguments:
- isEmpty
- Arguments:
xs(array) - Return: true if
xscontains no elements, otherwise false
- Arguments:
- take
- Arguments:
n(number),xs(array) - Return: an array containing the first
nelements ofxs
- Arguments:
- drop
- Arguments:
n(number),xs(array) - Return: an array containing the elements of
xswithout the firstnelements
- Arguments:
- flatten
- Arguments:
xs(array) - Return: an array containing the elements of
xswhere any nested arrays have been pulled out (only needs to work with one level of nesting deep)
- Arguments:
- intersperse
- Arguments:
sep(any),xs(array) - Return: an array containing the elements of
xswithsepinterspersed between the elements
- Arguments:
Number arrays
- sum
- Arguments:
ns(array) - Return: the sum of all numbers in
ns
- Arguments:
- product
- Arguments:
ns(array) - Return: the product of all numbers in
ns
- Arguments:
- maximum
- Arguments:
ns(array) - Return: the highest number in
ns
- Arguments:
- minimum
- Arguments:
ns(array) - Return: the lowest number in
ns
- Arguments:
- range
- Arguments:
start(number),end(number) - Return: an array of numbers counting up from
starttoend(inclusive)
- Arguments:
Objects
- eqObjects
- Arguments:
a(object),b(object) - Return: true if
aandbhave all the same values at all of their keys, otherwise false
- Arguments:
- setProp
- Arguments: k (string), v (any), o (object)
- Return: an object containing the properties of
obut with the value atkset tov
- removeProp
- Arguments: k (string), o (object)
- Return: an object containing the properties of
obut with the value atkset toundefined
Usage
If everything is done correctly, you should be able to run the following example in a file that is in the same directory as batteries.js.
const {sum, range} = require('./batteries');
console.log(sum(range(1, 5))); // should print 15Try adding the rest of the functions to the import and make sure they work!
Bonus: Testing
Create a separate module batteries.tests.js for testing that the functions in batteries.js work correctly. Import batteries.js and write console.log lines to test a few cases of each function. Look at exercises from the previous lessons for ideas on how to write good tests.
Tips for writing good tests:
- Tests should only return true or false.
- Test for the obvious case first. Pick any values that should work.
- Don’t test for values of the wrong type. You can assume that the functions should only work when the correct type of value is given. For example,
range()should only work for numbers as arguments. - Test for “edge” cases. Pick values that you might not expect to be used (e.g., empty array, empty string, 0, negative numbers, etc.).
- The more tests the better!
Example output
$ node batteries.tests.js
Testing isEven:
true
true
true
Testing isOdd:
true
true
true
Testing divisibleBy:
false
true
false
...