Program 3: conditionals

Programming the Web: An Introduction (Spring 2021)

Due date: Friday, February 26 at 5pm EST

(Originally due: Wednesday, February 24 at 5pm.)


General instructions


The problems themselves

  1. Complete function zeroUp so that it returns the input number (x) or 0 if x is negative.

  2. Complete function nudge so that it returns one more than the input number (x) unless it is negative in which case it returns one less. Examples:

    > nudge(0)
      1
    > nudge(41)
      42
    > nudge(-88)
      -89
  3. Complete function padTriple so that it returns a string like the input string (s), but so that the length of the returned string is a multiple of three, adding either one or two characters if needed. If one character is needed, an exclamation mark (!) is appended; if two characters are needed an asterisk (*) is placed on both sides of the string. Examples:

     > padTriple('')
       ""
     > padTriple('Wow')
       "Wow"
     > padTriple('hello')
       "hello!"
     > padTriple('cool')
       "*cool*"
  4. Complete function repeatLast so that it returns a string like the input string (s) with the last character of s repeated, assuming s is not empty. Examples:

     > repeatLast('')
       ""
     > repeatLast('Way to go!')
       "Way to go!!"
  5. Complete function randChar so that it returns a random character of the input string (s) unless s is empty in which case it returns the empty string.

  6. Complete function calculate so that it returns the sum, difference or product of the first (x) and third (y) inputs according to the value of the second inout (opCode): if it is 0, it adds; if 1, it subtracts; and, if 2, it multiplies. If none of those it returns 0. Examples:

     > calculate(7, 0, 5)
       12
     > calculate(7, 1, 5)
       2
     > calculate(7, 2, 5)
       35
     > calculate(7, 9, 5)
       0
  7. Complete function pluralize so that it returns a sentence indicating how many of the first input (noun) are being specified by the second (count). Assumes that the plural form of noun is regular (i.e., has an ā€˜sā€™ on the end). Examples:

     > pluralize('day', 7)
       "There are 7 days."
     > pluralize('snark', 0)
       "There are no snarks."
     > pluralize('unicorn', 1)
       "There is one unicorn."
  8. Complete function middle so that it returns the middle value of the three numeric inputs. Examples:

     > middle(99, 44, 77)
       77
     > middle(77, 44, 99)
       77
     > middle(99, 77, 44)
       77
  9. Complete function fancyCap so that it returns a version of the input string (s) depending on the case of the first character in s: if it is uppercase, the returned string is entirely uppercase; if it lowercase, the returned string is entirely lowercase; if neither it returns s as is. Examples:

     > fancyCap('')
       ""
     > fancyCap('wHaT?')
       "what"
     > fancyCap('Cool!')
       "COOL!"
     > fancyCap('*emBOLDen*')
       "*emBOLDen*"

Challenge problems available upon request.


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:

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

You may also use Math.floor(...) and Math.random().

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()

and you can access individual characters within the string by using [] notation.


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

> zeroUp(-9)
  0
> nudge(41)
  42
> padTriple('cool')
  "*cool*"
> repeatLast('No way?')
  "No way??"
> randChar('whatevs')
  "h"
> calculate(7,2,5)
  35
> pluralize('day', 7)
  "There are 7 days."
> middle(99, 44, 77)
  77
> fancyCap('Nice one!')
  "NICE ONE!"