Quiz: Working With WeakSets

Directions:

Create the following variables:

  • uniqueFlavors and give it the value of an empty WeakSet object
  • flavor1 , and set it to the object { flavor: 'chocolate' }
  • flavor2 , and set it to an object with a property of flavor and a value of your choice

Use the.add()method to add the objectsflavor1andflavor2touniqueFlavors.

Use the.add()method to add theflavor1object to theuniqueFlavorsset, again.

Your Code:

/*
 * Programming Quiz: Using Sets (3-2)
 *
 * Create the following variables:
 *     - uniqueFlavors and set it to a new WeakSet object
 *     - flavor1 and set it equal to `{ flavor: 'chocolate' }`
 *     - flavor2 and set it equal to an object with property 'flavor' and value of your choice!
 *
 * Use the `.add()` method to add the objects `flavor1` and `flavor2` to `uniqueFlavors`
 * Use the `.add()` method to add the `flavor1` object (again!) to the `uniqueFlavors` set
 */


let uniqueFlavors = new WeakSet();
flavor1 = { flavor: 'chocolate' };
flavor2 = { flavor: 'vanilla' };

uniqueFlavors.add(flavor1);
uniqueFlavors.add(flavor2);
uniqueFlavors.add(flavor1);

console.log(uniqueFlavors);

results matching ""

    No results matching ""