11.

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
  • isOdd
    • Arguments: n (number)
    • Return: true if n is odd, otherwise false
  • divisibleBy
    • Arguments: x (number), y (number)
    • Return: true if x is divisible by y, otherwise false

Arrays

  • eqArrays
    • Arguments: xs (array), ys (array)
    • Return: true if xs and ys are the same length and all of their elements are equal, otherwise false
  • buildArray
    • Arguments: size (number)
    • Return: an array of size size (all elements can be undefined)
  • append
    • Arguments: y (any), xs (array)
    • Return: array with y appended to xs
  • head
    • Arguments: xs (array)
    • Return: the first element of xs
  • tail
    • Arguments: xs (array)
    • Return: an array containing all of the elements of xs except the first one
  • last
    • Arguments: xs (array)
    • Return: the last element of xs
  • init
    • Arguments: xs (array)
    • Return: an array containing all of the elements of xs except the last one
  • isEmpty
    • Arguments: xs (array)
    • Return: true if xs contains no elements, otherwise false
  • take
    • Arguments: n (number), xs (array)
    • Return: an array containing the first n elements of xs
  • drop
    • Arguments: n (number), xs (array)
    • Return: an array containing the elements of xs without the first n elements
  • 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)
  • intersperse
    • Arguments: sep (any), xs (array)
    • Return: an array containing the elements of xs with sep interspersed between the elements

Number arrays

  • sum
    • Arguments: ns (array)
    • Return: the sum of all numbers in ns
  • product
    • Arguments: ns (array)
    • Return: the product of all numbers in ns
  • maximum
    • Arguments: ns (array)
    • Return: the highest number in ns
  • minimum
    • Arguments: ns (array)
    • Return: the lowest number in ns
  • range
    • Arguments: start (number), end (number)
    • Return: an array of numbers counting up from start to end (inclusive)

Objects

  • eqObjects
    • Arguments: a (object), b (object)
    • Return: true if a and b have all the same values at all of their keys, otherwise false
  • setProp
    • Arguments: k (string), v (any), o (object)
    • Return: an object containing the properties of o but with the value at k set to v
  • removeProp
    • Arguments: k (string), o (object)
    • Return: an object containing the properties of o but with the value at k set to undefined

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

...
Top