Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Introducing ES2015 Objects and New Collection Types Set

classroom size doesn't change...

why isn't the second log saying 2?

Here is my console log:
stevenJ is in the classroom
sarah is in the classroom
stevenS is in the classroom
classroom size: 3
classroom size: 3

Here is the code:

'use strict';

let classroom = new Set();

let stevenJ = { name: 'Steven', age: 22 },
    sarah = { name: 'Sarah', age: 23 },
    stevenS = { name: 'Steven', age: 22 };

classroom.add(stevenJ);
classroom.add(sarah);
classroom.add(stevenS);
classroom.add(sarah);

if (classroom.has(stevenJ)) console.log('stevenJ is in the classroom');
if (classroom.has(sarah)) console.log('sarah is in the classroom');
if (classroom.has(stevenS)) console.log('stevenS is in the classroom');

console.log('classroom size:', classroom.size);

classroom.delete('stevenJ');

console.log('classroom size:', classroom.size);

1 Answer

Juan Luna Ramirez
Juan Luna Ramirez
9,011 Points

You are trying to delete the string 'stevenJ' instead of the object { name: Steven, age: 22 } which is saved to the stevenJ variable. Nothing is being deleted since there is no 'stevenJ' string in your Set.

'use strict';

let classroom = new Set();

let stevenJ = { name: 'Steven', age: 22 },
    sarah = { name: 'Sarah', age: 23 },
    stevenS = { name: 'Steven', age: 22 };

classroom.add(stevenJ);
classroom.add(sarah);
classroom.add(stevenS);
classroom.add(sarah);


if (classroom.has(stevenJ)) console.log('stevenJ is in the classroom');
if (classroom.has(sarah)) console.log('sarah is in the classroom');
if (classroom.has(stevenS)) console.log('stevenS is in the classroom');

console.log('classroom size:', classroom.size);

// remove the quotes so that you are passing in the reference to the stevenJ object
console.log(classroom.delete(stevenJ));

console.log('classroom size:', classroom.size);