Lab 3: conditionals

Programming the Web: An Introduction (Spring 2021)



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)
    > minimum(4001, 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)
    > sign(-43)
    > sign(2048)
  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')
     > padEven('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)
     > threeify(7)
  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')
     > seasonToNumber('SPRING')
     > seasonToNumber('Fall')
  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)')
     > reparen('anything (else)')
       "anything (else)"
  7. Complete appendRandom(s) so that it returns s appended with either ? or ! with equal likelihood. Examples:

     > appendRandom('maybe')
     > appendRandom('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')