Sets Flashcards

1
Q

const set1 = new Set();

set1. add(42);
set1. add(42);
set1. add(13);

for (let item of set1) {
  console.log(item);
  // expected output: 
  // expected output:
A

42

13

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

const mySet = new set []

//add a number 99 to this set

A

mySet.add(99);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to know size of a set

A

set.size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

to check if a set contains an elemet

A

set.has()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
const mySet = new Set(['1', '2', '3']);
[...mySet].indexOf('2') //
A

returns 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

_________have no built-in function to retrieve or find the index of its items even-though its an iterable, so ideally we would have to convert it to an array before indexOf/find operation.

A

sets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

_______ – creates the set, optionally from an array of values (any iterable will do).

A

new Set(iterable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

_________ – adds a value, returns the set itself.

A

set.add(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

__________– removes the value, returns true if value existed at the moment of the call, otherwise false.

A

set.delete(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

________ – returns true if the value exists in the set, otherwise false.

A

set.has(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

_________– removes everything from the set.

A

set.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

_________ – is the elements count.

A

set.size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

let set = new Set();

let john = { name: "John" };
let pete = { name: "Pete" };
let mary = { name: "Mary" };

set. add(john);
set. add(pete);
set. add(mary);
set. add(john);
set. add(mary);

alert( set.size ); //

A

3

duplicate names dont get returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

We can loop over a set either with ______ or using ______

A

for…of

forEach

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

let set = new Set([“oranges”, “apples”, “bananas”]);

Iterate using for…of

A

for (let value of set) alert(value);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

let set = new Set([“oranges”, “apples”, “bananas”]);

iterate using forEach

A

set.forEach((value, valueAgain, set) => {
alert(value);
});

17
Q

write a blank set forEACh

A
set.forEach((value, valueAgain, set) => {
// code
});
18
Q

console.log(‘Only unique characters will be in this set.’.length); // 43

let sentence = new Set('Only unique characters will be in this set.');
console.log(sentence.size); //
A

18