0

I have the following class:

class Group {
  constructor() {
    this.group = [];
  }

  add(input) {
    if (!this.group.includes(input)) {
      this.group.push(input);
    }
  }

  static from(arr) {
    const group = new Group();
    for (let el of arr) {
      group.add(el);
    }
    this.group = group;
    return group;
  }

  has(elem) {
    return this.group.includes(elem);
  }
  delete(elem) {
    let newarr = this.group.filter((n) => n !== elem);
    this.group = newarr;
  }
}

what I want to do is this:

for (let value of Group.from(["a", "b", "c"])) {
  console.log(value);
}
// → a
// → b
// → c

that is to make the class an iterable object but I dont know how to do it.

note:

don’t just return the iterator created by calling the Symbol.iterator method on the array

32
  • why close the question when I dont have to use the symbol.iterator while answers use symbol.iterator Commented Sep 28, 2023 at 22:49
  • Because that's not what the answers in the other question say. Commented Sep 28, 2023 at 22:49
  • That's not what your restriction says. It says you shouldn't return the array's iterator. Commented Sep 28, 2023 at 22:50
  • 1
    Then use a different answer, like stackoverflow.com/a/44715252/1491895 Commented Sep 28, 2023 at 22:55
  • 1
    your restriction is also arbitrary. Why shouldn't you return the array's iterator if you can? If it's just for the homework assignment, how does this question help anyone in the future? Commented Sep 28, 2023 at 23:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.