LMU ☀️ CMSI 185
COMPUTER PROGRAMMING
HOMEWORK #4 Due: 2020-11-21

Objectives

In this assignment you will demonstrate your ability to write functions and use unit tests. (As this is your first assignment focusing on unit testing, the unit tests will be supplied to you; in the future, you will need to gain experience and practice writing your own tests.)

Instructions

Do the readings, watch the videos, write the essays, and program the applications below.

Submit, via Brightspace, the following:

Readings and Videos

Just read and/or watch the following. You do not have to take notes or write reports on them; however, don’t cheat yourself by skipping them. These independent learning units must be done individually, but please feel free to discuss these items with your friends and classmates. You are encouraged to share your thoughts on these materials on course community channels (Brightspace Forums, Slack, Discord), and to post related items your classmates might be interested in. We learn together. Some of these resources have links to other resources, which you may wish to follow as time allows.

Programming

This assignment is your chance to practice, practice, practice with functions and classes in JavaScript. And good news: unit tests are already written for you. Begin by forking (duplicating, remixing, copying, whatever your preferred word is) my starter code. Then, implement all the functions until all the unit tests pass. That’s all there is to it.

For reference, a brief description of the functions and classes you are to write follows. Note that specific details of the functions that are hard to capture in text are essentially captured in the unit tests! Remember that in a real sense unit tests are a form of documentation. The unit tests will tell you how to handle certain “invalid arguments” (throw an error? return undefined? ignore?) and what exactly to return.

  1. A function to return how many elements in a given array are negative numbers or negative bigints, throwing an error if the argument is not an array.
  2. A function to return the number of occurrences of a given character in a given string.
  3. A function to return a random number between x (inclusive) and y (exclusive).
  4. A function to return a acronym for a sentence, e.g. when given "I know, right?" the function should return "ikr". Every word gets to participate in the acronym. Even small ones, like “a,” “in,” and “of.” Words are separated by things that JavaScript thinks are spaces; therefore, to split your sentence, use the incantation sentence.split(/\s+/).
  5. A function to return whether or not a number is prime, throwing an error if the argument is not a positive safe integer.
  6. A function to return the median of its three arguments, which must be numbers, so throw an error if any of the arguments are not numbers, or are NaN. Careful: even though JavaScript says that typeof NaN === 'number', we don’t want NaNs here.
  7. A function to return the sum of the squares of all the even numbers in an array. Use map, filter, and reduce. The body of your function must be a single expression.
  8. A function that takes in two strings and returns whether or not one of the strings appears as a substring of the other, but not at the beginning or end.
  9. A class for a latitude-longitude points on Earth. The constructor should take in a latitude and longitude (in degrees) and throw an error if either value is out of the accepted range (-90..90 for latitude, -180..180 for longitude). Include methods to determine (1) whether or not the point is in the Arctic Circle, (2) whether or not the point is in the Antarctic Circle, (3) whether or not the point is in the tropics, and (4) the antipode of the point (as a point object, of course).
  10. A function called makeSequenceGenerator that, when given a function and an initial value, returns a function that delivers values from a sequence each time it is called. For example, makeSequenceGenerator(x => x * 2 + 3, 2) should return a function f such that the first time you call f() you get back 2, the second time you get back 2*2+3, or 7, and the next time 17, then 37, then 77, and so on.
  11. A function that returns the “power array” of an array, which is similar to the powerset of a set, only for arrays, throwing an error if the argument is not an array.
  12. A function that returns the number of steps in the Collatz sequence for the given number, throwing an error if the argument is not a positive integer in the range 1 to 1000000000000.

Grading

You will earn 5 points for each of problems 1-4 that pass all tests, and 10 points for each of problems 5-12 that pass all tests. Then as a measure of code quality, I will deduct 5-10 points for each violation of style, e.g. poor formatting, unconventional spacing and indentation, poor variable names, lack of breaking up complex subexpressions, mixing of concerns, and so on.