Program 2: functions and Booleans

Programming the Web: An Introduction (Spring 2021)

Due date: Wednesday, February 17 at 5pm EST

General instructions


Begin by trying the demonstration version. Each of the six buttons correspond to one of six pure functions. Your job is to “reverse engineer” each and then write your own version of that function. The file hw2-io.js supplied with the starter archive contains the JavaScript functions invoked by clicking the buttons as specified in hw2.html. Each of those functions is a wrapper around a corresponding pure function. For example, the second, button, labeled “go, GO, GO!” invokes the smallBigBigTester function (defined in hw2-io.js) which prompts the user for some text, passes that string as an argument to the pure function smallBigBig (partially defined, but needs to be completed, in hw2.js) which should return another string back to smallBigBigTester which then displays the resulting string using alert.

Here are the input and return types of the six pure functions you need to complete:

  1. isInt takes as input a number and returns a Boolean value.

  2. smallBigBig takes as input a string and returns another string.

  3. isUniformCase takes as input a string and returns a Boolean value.

  4. bigEvenMedium5 takes as input a number and returns a Boolean value.

  5. parity takes as input three Boolean values and returns another Boolean value.

  6. mystery takes two inputs, the first a string, the second a number, and returns a Boolean value.

Once you understand what each function is supposed to accomplish, replace the ... comment above that function with a comment of your own (that might run for several lines) explaining in concise, but grammatical and proofread prose what you think the function computes. (Follow examples from lab.)

Once you have explained what a function does in your own words, you can write the code representing its body. Test your code thoroughly both by evaluating some examples in the console and by clicking the buttons that use the wrappers to call the pure functions you are implementing. Remember to reload the page in your browser each time after modifying hw2.js.

Remember, all of the functions should be pure: they take one or more arguments and return a value. Make sure that your functions have a single return statement as the last statement within the body of the function. Keep the return simple: just return a variable. Use variable assignments to compute your result. Each function should have at least one let assignment.

Your code should not use alert, prompt, or confirm.

With numbers, you can use the standard five arithmetic operations:

+   -   *   /   %

and the six arithmetic comparison operations:

===   !==   <   >   <=   >=

With Boolean values s you can use the literals true and false and the three standard Boolean operators:

!   &&   ||

With strings, you can use:

===   !==   .length   .toLowerCase()   .toUpperCase()

To experiment more directly with my solution, you can open up on a console while viewing this page and try examples such as:

> isInt(5)

> smallBigBig('yikes')
  "yikes, YIKES, YIKES"

> isUniformCase('Not!')

> bigEvenMedium5(25)

> parity(false, false, false)

> mystery('bad', 4)