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
n
is even, otherwise false
- Arguments:
- isOdd
- Arguments:
n
(number) - Return: true if
n
is odd, otherwise false
- Arguments:
- divisibleBy
- Arguments:
x
(number),y
(number) - Return: true if
x
is divisible byy
, otherwise false
- Arguments:
Arrays
- eqArrays
- Arguments:
xs
(array),ys
(array) - Return: true if
xs
andys
are 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
y
appended 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
xs
except 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
xs
except the last one
- Arguments:
- isEmpty
- Arguments:
xs
(array) - Return: true if
xs
contains no elements, otherwise false
- Arguments:
- take
- Arguments:
n
(number),xs
(array) - Return: an array containing the first
n
elements ofxs
- Arguments:
- drop
- Arguments:
n
(number),xs
(array) - Return: an array containing the elements of
xs
without the firstn
elements
- Arguments:
- flatten
- Arguments:
xs
(array) - Return: an array containing the elements of
xs
where 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
xs
withsep
interspersed 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
start
toend
(inclusive)
- Arguments:
Objects
- eqObjects
- Arguments:
a
(object),b
(object) - Return: true if
a
andb
have 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
o
but with the value atk
set tov
- removeProp
- Arguments: k (string), o (object)
- Return: an object containing the properties of
o
but with the value atk
set 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 15
Try 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
...