jskatas.org Continuously Learn JavaScript. Your Way.

Set: set.delete()

Removes an element from a set.

set.delete() deletes an element from a set

use delete(<value>) to delete an element

delete() returns true when the element was found
const set = new Set(); set.add('one').add('two').add('three'); const returns = set.remove; assert.strictEqual(returns, true);
and the size decreases
const set = new Set(); set.add('one').add('two').add('three'); assert.equal(set.size, 2);

if nothing was deleted (no element with the given value was found)

returns false
const set = new Set(); set.add('one').add('two').add('three'); const returns = set.delete('one'); assert.equal(returns, false);

undefined is a valid value in a set

deleting it, when it is not in the set, returns false too
const set = new Set(); set.add('one').add('two').add('three'); assert.equal(set.delete(whatToDelete), false);
delete() removes it, when its in the set
const set = new Set(); set.add('one').add('two').add('three'); assert.equal(set.delete(), true);

the value does NOT get casted

number 1 is different to string "1"
const set = new Set(); set.add('one').add('two').add('three'); set.add(1); set.add('1'); assert.equal(set.delete('1'), false);

Links

Announcement of this kata on twitter.

Required Knowledge

Related Katas

Set

Difficulty Level

BEGINNER

First Published

15 July 2015

Stats

6 tests to solve