CS 347: Lab 13 – Object Questions
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.).
- Investigate the spread syntax for objects. Suppose you have this object: How can you succinctly create a new variable named
const colors = {cornflower: '#6495ed', peru: '#cd853f'};
palette
that has all of the key-value pairs ofcolors
in addition to the mappingchocolate
→#d2691e
—without modifyingcolors
? - How do you complete the body of this function return a new, inverted version of an object that acts as a reverse map? For example,
function reverse(obj) { // ... }
reverse(colors)
yields{'#6495ed': 'cornflower', '#cd853f': 'peru'}
. - 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 ofJSON.parse
. - Suppose you have this code: How can you edit the object literal so that it includes a function named
const player = { name: 'Frud', hitPoints: 62, }; player.cure();
cure
that increases the number of hit points by 10? - Suppose you have this code in a 1- or 2-player game: When in 1-player mode,
player2.levelUp();
player2
isundefined
, 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: