JavaScript Introducing ES2015 Objects and New Collection Types Set

Amy Tomey
Amy Tomey
9,074 Points

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
8,957 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);