Introduction to (Web) Programming

Program 2: Pure Functions and Boolean Values

Due date: Thursday, February 14 at 1:30pm

General instructions


Begin by trying the demonstration version. Each of the five buttons correspond to one of five 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 first, softLoudTester prompts the user for some text, passes that string of text in as an argument to the pure function softToLoud which returns another string which is displayed using alert.

Here is what you know about the five pure functions you need to implement:

  1. softToLoud takes as input a string and returns another string

  2. isYesOrNo takes as input a string (the variable called s) and returns a Boolean value

  3. oddNotSmall takes as input an integer and returns a Boolean value

  4. ifThenElse takes as input three different Boolean values and returns a Boolean value

  5. mystery takes as input an integer (which is assumed to be positive) and returns an integer.

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 and described in class.)

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 side-effecting operations (so do not use prompt, confirm or alert).

For numbers, you can assume that all numeric inputs are integers. You can use the standard five arithmetic operations:

+   -   *   /   %

and the six arithmetic comparison operations:

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

For Booleans you can use the literals true and false and the three standard Boolean operators:

!   &&   ||

For strings, you can use:

===   !==   .length   .toLowerCase   .toUpperCase

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

> softToLoud('What?')
"what?, What?, WHAT?"

> isYesOrNo('YES')

> oddNotSmall(50)

> ifThenElse(false, false, true)

> mystery(20)