1

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

1
  • 1
    A static function does not require a class instance, hence Group.from. Inside the static method, you do not have a this!. Commented Oct 11, 2021 at 14:23

2 Answers 2

4

The objective is:

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.

So, inside the method, you need to

creates a group

which can be done with new Group, then

contains all the values produced by iterating over it

with the loop referencing that newly created group, and then return the created group.

  static from(iterableObject) {
    const group = new Group();
    for (const value of iterableObject) {
      group.add(value);
    }
    return group;
  }

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) {
    const group = new Group();
    for (const value of iterableObject) {
      group.add(value);
    }
    return group;
  }
}

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

I don't know why this static method is needed here

A static method can be thought of as being similar in appearance to a standalone function (in terms of how it's called) - except that it's a property of the class itself. You could equivalently do

const createGroupFrom = (iterable) => {
  // implementation here
};

But being static usually indicates a function that is related to the class, without being tied to one particular already-existing instance. (Methods that are directly related to working on already-existing instances are implemented as non-static, regular methods.

In this situation, a regular method clearly wouldn't work, because it wouldn't be called on an existing instance - an existing instance doesn't exist. So the options are to have a plain standalone function, or a static method, if one prefers it - and the requirement given is to use a static method.

Sign up to request clarification or add additional context in comments.

3 Comments

While this is certainly how to make it work, I'm not seeing any explanations of "why this static method is needed" that the question asks for.
@crashmstr Not sure what better explanation than "the requirement asks for it". Technically, it's a factory method but it seems a tad more advanced for OP.
@VLAZ It seems like the person asking the question has no idea what a static is or why it is needed to solve the problem. So the solution to the problem without why you need to create an object in the static may not answer all of the question.
1

As stated on MDN:

The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.

In the static method you have,

  static from(iterableObject) {
    for (let value of iterableObject) {
      this.add(value);
    }
    return this;
  }

But what you'll want to do is set a reference to a new Group instance and return that instance:

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) {
    var newGroup = new Group(iterableObject);
    for (let value of iterableObject) {
      newGroup.add(value);
    }
    return newGroup;
  }
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.