LMU ☀️ CMSI 185
COMPUTER PROGRAMMING
HOMEWORK #3 Due: 2020-10-25

Objectives

In this assignment you will demonstrate low-level technical knowledge of programming components such as values and expressions, craft expressions to carry out a desired computation, and craft objects to represent real-world data. You will continue your development of programming skills by building a game incorporating gravity and collision detection with third-party library.

Instructions

Do the readings, watch the videos, write the essays, and program the applications below.

Submit, via Brightspace, the following:

Readings and Videos

Just read and/or watch the following. You do not have to take notes or write reports on them; however, don’t cheat yourself by skipping them. These independent learning units must be done individually, but please feel free to discuss these items with your friends and classmates. You are encouraged to share your thoughts on these materials on course community channels (Brightspace Forums, Slack, Discord), and to post related items your classmates might be interested in. We learn together. Some of these resources have links to other resources, which you may wish to follow as time allows.

Writing

Many of these questions refer to the readings, videos, and activities above. Some questions may require additional research to answer well enough for full credit.

  1. Given the following declarations:
    const info = { dog: 'cat', rat: 'dog', rex: 10 };
    const response = { count: 2, locations: [ 'home', {lat: 33, lon: -112} ] };
    const dog = 'rex';
    const colors = ['Cyan', 'Magenta', 'Yellow'];
    
    What do each of the following JavaScript expressions evaluate to? Use a REPL to find out. (Note: make sure to write out the complete result of the evaluations. Some REPLs will not write out complete values.) Make sure you understand each of the results. Ask for help if there is anything you do not understand.
    1. -10 < -5 < -1
    2. colors[2][3]
    3. info["dog"]
    4. info[dog]
    5. response.locations[1]
    6. 0.1 + 0.2
    7. Number.isSafeInteger(Math.pow(2, 55))
    8. 57093424755811 * 1946127975092301
    9. 57093424755811n * 1946127975092301n
    10. 'I do not think it will be a good pudding'.split(' ', 4)
    11. { dog, c: ['Black', 'Purple', ...colors, 'Cyan'] }
    12. JSON.stringify(response.locations)
    13. colors.includes(dog) ? 'Hai' : 'Not today'
    14. info.bat ?? 100
    15. (x => -x)(response.count)
  2. Write each of the following in their fully parenthesized form:
    1. 2 * 5 - 7 / - 6 + 4
    2. 2 > 4 || true && false
    3. 1 <= 2 <= 3
    4. ! x || ! y && z || 6 / 5 & 1
    5. - - 9 - --x
  3. Write expressions for each of the following. For example, if asked “the number of kilometers in m miles,” you would answer m * 1.609344.
    1. The last 5 characters of the string s.
    2. The binary representation (as a string) of the number x.
    3. (Destructively) deleting the 9th through 15th elements (inclusive) of the array a.
    4. (Destructively) inserting three zeros at the beginning of the array a.
    5. Whether any one of the boolean variables sick, weekend, or holiday is true.
    6. The maximum value of array a (expressed using the spread operator).
    7. The maximum number of quarters that does not exceed x dollars.
    8. A random integer between 5 and 20, inclusive.
    9. The string "nananananananananananananananana", expressed with .repeat
    10. Whether or not the string s contains a poop emoji or a winking face emoji.
    11. The distance from the origin to the point (8, 3, 99, 14, -3) in 5-dimensional space.
    12. The value of the x property of the 3rd element of the 5th element t, where t is an array of 10 arrays of 10 objects.
    13. The datetime January 1, 1970 at midnight UTC, rendered in the Arabic/Egypt locale.
    14. An object whose properties are 'A', 'B', 'C', ... 'Z' and whose values are the numbers to which each of those letters are mapped on a phone.
    15. Whether the number x is a positive integer less than one million times π.
  4. One of the seniors was working on some code with a boolean-valued variable called present that they wanted to turn into an English sentence. But instead of writing:
    const notice = "She is" + (present ? "" : "n't") + " here."
    
    they wrote:
    const notice = "She is" + present ? "" : "n't" + " here."
    
    What is the effect of this (incorrect) code? That is, what does it do, at a high level? And why, exactly, does that behavior occur? Next, rewrite this declaration using string interpolation instead of concatenation. Does doing so make the bug impossible to occur? Why or why not?
  5. The observable universe has been estimated to be 93 billion light years wide. Let’s write a script to find out how many yottameters (Ym) this is. I’ll get it started. Please finish it up:
    const WIDTH_IN_LIGHT_YEARS = 93e9;
    const SPEED_OF_LIGHT = 299792458;
    const DAYS_PER_YEAR = 365.2422;
    const SECONDS_PER_DAY = /* Fill this in */;
    const METERS_PER_YOTTAMETER = /* Fill this in */;
    const WIDTH_IN_YOTTAMETERS =  /* Fill this in */;
    console.log(`The universe is ${WIDTH_IN_YOTTAMETERS} Ym wide.`);
    
  6. Draw a picture of the following object(s). Don’t forget to show references as arrows.
    {
      call: 'mark',
      next: {
        call: 'ready',
        next: { call: 'set', next: { call: 'go', next: null } }
      }
    }
    
  7. Write variable declarations for the following. Use const and use very well-chosen, descriptive, names.
    1. An employee whose name is María, salary is 1988 USD per week, hire date is 2008-01-05, and who does not have a supervisor.
    2. An array of the names the days of the week in Spanish.
    3. The song Johnny Tarr by Gaelic Storm from the album Tree. Gather as much information about the track as you can. You need at least eight properties.
  8. Give a variable declaration in JavaScript which sets the variable artist to an object holding the following data on the band Purity Ring. The band originated in Edmonton, Alberta, Canada and consists of members Megan James (vocalist) and Clayton Knight (producer). Genres include synth-pop, trip hop, dream pop, and witch house. Their website is purityringthing.com. They have released three studio albums: Shrines (on 2012-07-24, which peaked at 32 on the Billboard 200), Another Eternity (on March 3 of 2015), and Womb (on April 3, 2020). The band worked with the labels 4AD and Last Gang.

Programming

Make a Flappy Bird game in p5.js. It does not have to be fancy. Your bird can be a simple circle. You are required to implement gravity, raise the bird on a space bar, and detect collisions with the pipes. Implement the bird and the pipes as instances of classes (called Bird and Pipe). Indicate a collision by coloring the collided-with pipe red. After three collisions, announce “GAME OVER” in big letters centered on the canvas, and call noLoop. To discover when the bird collides with a pipe, use the p5collide2d library, which you can find on the p5.js Libraries page.

Grading

Here’s the rubric I’ll use to score your submission.

RequirementOut OfYou got
Problem 115
Problem 25
Problem 315
Problem 45
Problem 55
Problem 65
Problem 75
Problem 810
Flappy Bird code style (use a formatter!)5
Flappy Bird naming, code structure, DRYness, separation of concerns10
Flappy Bird Gravity5
Flappy Bird Keyboard Interaction3
Flappy Bird Animation3
Flappy Bird Collision Detection3
Flappy Bird Collision Scoring3
Flappy Bird Game Over state3
Misc. Bonus points or penalties at discretion of Prof.0
Total100