Lab 3: conditionals

Programming the Web: An Introduction (Spring 2021)


Goals


Instructions


Examples to review

Open the folder lab3 in VS code. Then open lab3-examples.js and lab3.js. Open lab3-examples.html in your browser and open a console in your browser while viewing lab3-examples.html and try evaluating the functions defined in lab3-examples.js in that console.


JavaScript console exercises

Open the JavaScript console in your Web browser (Command-Option-I). Using the browser’s JavaScript console, try these examples, in order, one at a time:

> let r = Math.random();
> r

> Math.random()

> Math.floor(3.14)

> if (true) { console.log('told you so!'); }

> if (false) { console.log('NOT!'); }

> let name = 'Mike';
> name[0];
> name[1];
> name[2];
> name[3];
> name[4];

Programming problems

Write your code in lab3.js.

  1. Complete function minimum so that it returns the smaller of the two input numbers. Use a simple if statement (no need to use else). Examples:

    > minimum(3, 4001)
      3
    > minimum(4001, 3)
      3
  2. Complete function sign so that it returns 0 if the input number is 0, -1 if negative, and 1 if positive. Use an if/else if construct. Examples:

    > sign(0)
      0
    > sign(-43)
      -1
    > sign(2048)
      1
  3. Complete padEven(s) so that it returns string s if s consists of an even number of characters (i.e., its length is an even number) or returns s with a hashtag (#) in front of it the length is odd. Use an if statement without an else. Examples:

     > padEven('Cool')
       "Cool"
     > padEven('Cool!')
       "#Cool!"
  4. Complete threeify(n) so that it returns n divided by 3 if n is divisible by 3 and n times 3 if n is not divisible by 3. You can assume n is a number. Use an if-else statement. Examples:

     > threeify(15)
       5
     > threeify(7)
       21
  5. Complete seasonToNumber(season) so that it returns the number corresponding to the specified string season (1 for Winter, 2 for Spring, etc.). Returns 0 if season is not one of the four seasons. Case insensitive. Use a multiway conditional. Examples:

     > seasonToNumber('winter')
     1
     > seasonToNumber('SPRING')
     2
     > seasonToNumber('Fall')
     4
  6. Complete reparen(s) so that if s is a string that starts with ( and ends with ) it returns a version of s with an extra set of parentheses around it. Otherwise it should just return s as is. Examples:

     > reparen('(quiet)')
       "((quiet))"
     > reparen('anything (else)')
       "anything (else)"
  7. Complete appendRandom(s) so that it returns s appended with either ? or ! with equal likelihood. Examples:

     > appendRandom('maybe')
       "maybe?"
     > appendRandom('maybe')
       "maybe!"
  8. Write the non-pure function timesQuiz from scratch so that it asks the user to multiply two random numbers, each between 1 and 9 (inclusive). If the user guesses correctly it reports “correct”, if not it indicates the correct answer. For this problem, use prompt, parseFloat, and alert.


Challenge problems available upon request.


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

> appendRandom('hello')

> timesQuiz()

> padEven('why')

...