Quiz: Working With WeakSets
Directions:
Create the following variables:
uniqueFlavors
and give it the value of an emptyWeakSet
objectflavor1
, and set it to the object{ flavor: 'chocolate' }
flavor2
, and set it to an object with a property offlavor
and a value of your choice
Use the.add()
method to add the objectsflavor1
andflavor2
touniqueFlavors
.
Use the.add()
method to add theflavor1
object to theuniqueFlavors
set, 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);