teaching machines

CS 347: Lab 13 – Object Questions

October 8, 2020 by . Filed under fall-2020, labs, webdev.

Welcome to lab, a time for applying the ideas introduced in lecture in exercises that you will complete in small groups.

Within your breakout room, designate one of your team to be the screen sharer. Screen sharer, share your screen and claim your group’s task on Crowdsource. Make sure to enter every group member’s JMU eID so that they receive credit. Your group will be assigned a task number.

Team, complete the assigned task below. Screen sharer, be careful not to dominate. All members should contribute ideas.

Task

Answer the following questions. Assemble your answers in a plain text document using your preferred method (Google Docs, Slack, Zoom chat, etc.).

  1. Investigate the spread syntax for objects. Suppose you have this object:
    const colors = {cornflower: '#6495ed', peru: '#cd853f'};
    
    How can you succinctly create a new variable named palette that has all of the key-value pairs of colors in addition to the mapping chocolate#d2691e—without modifying colors?
  2. How do you complete the body of this function return a new, inverted version of an object that acts as a reverse map?
    function reverse(obj) {
      // ... 
    }
    
    For example, reverse(colors) yields {'#6495ed': 'cornflower', '#cd853f': 'peru'}.
  3. Suppose you have an object named obj. How do you serialize it to a JSON string with each level indented two spaces? This action is the inverse of JSON.parse.
  4. Suppose you have this code:
    const player = {
      name: 'Frud',
      hitPoints: 62,
    };
    player.cure();
    
    How can you edit the object literal so that it includes a function named cure that increases the number of hit points by 10?
  5. Suppose you have this code in a 1- or 2-player game:
    player2.levelUp();
    
    When in 1-player mode, player2 is undefined, and this code throws an exception. Without writing an if statement, how can you tweak this code so that no exception is thrown? Investigate optional chaining.

Screen sharer, when your group is done or when time is up, submit your group’s answers on Crowdsource. Do not submit a link; copy and paste the plain text content into the form.

Peer Review

After you have submitted the exercise, transition into your last peer review for project 1. The following things should happen: