Write a class called Group (since Set is already taken). Like Set, it has add, delete, and has methods. Its constructor creates an empty group, add adds a value to the group (but only if it isn’t already a member), delete removes its argument from the group (if it was a member), and has returns a Boolean value indicating whether its argument is a member of the group.
Use the === operator, or something equivalent such as indexOf, to determine whether two values are the same.
Give the class a static from method that takes an iterable object as argument and creates a group that contains all the values produced by iterating over it.
This the question I need to solve and below is my code which gives error.
The error here is :
Uncaught TypeError: this.add is not a function
I don't know why this static method is needed here please let me know. And help me clear this error.
class Group {
constructor() {
this.group = [];
return this;
}
add(value) {
if (!this.has(value)) {
this.group.push(value);
return this;
}
}
delete(value) {
if (this.has(value)) {
this.group = this.group.filter(x => x !== value)
return this;
}
}
has(value) {
return this.group.includes(value)
}
static from(iterableObject) {
for (let value of iterableObject) {
this.add(value);
}
return this;
}
}
let group = Group.from([10, 20]);
console.log(group.has(10));
// → true
console.log(group.has(30));
// → false
group.add(10);
group.delete(10);
console.log(group.has(10));
//→ false
staticfunction does not require a class instance, henceGroup.from. Inside thestaticmethod, you do not have athis!.