0

This is the constructor for the group class that acts similarly to a set.

    constructor(){
        this.list = [];
    }

    [Symbol.iterator](){
        return new GroupIterator(this.list);
    }

This should make group objects itterable but I can't find the error.

class GroupIterator{

    constructor(group){
        this.group = group;
        this.position = 0;
    }

    next(){
        let result = {
            value: undefined,
            done: true,
        };
        if(this.position >= this.group.list.length){
            return result;
        }
        result.value = group.list[this.position];
        result.done = false;
        this.position++;
        return result;

    }
}  
2
  • when you are debugging, what do you receive as a parameter in your constructor Commented Jun 26, 2019 at 21:27
  • How about return new GroupIterator(this); Commented Jun 26, 2019 at 21:33

2 Answers 2

0

Looks like you have a small error here, it should be this.group.length, not this.group.list.length, the list IS named group. For reference:

class GroupIterator{
  constructor(group){
    this.group = group
    this.position = 0
  }

  next() {
    let result = {
      value: undefined,
      done: true,
    }

    if(this.position >= this.group.length){
      return result
    }

    result.value = this.group[this.position]
    result.done = false
    this.position++
    return result
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

group[this.position] should be this.group[this.position]
0

Because your property is called group - there's no list attached, because group is the array you passed to the constructor.

if (this.position >= this.group.length) {...}

And make sure to replace group[this.position] with this.group[this.position].

2 Comments

Also this.group[this.position]
Ok @Barmar, I'll add that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.