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;

    }
}  
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
nswan
  • 9
  • 1

2 Answers2

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
  }
}
Shmish
  • 143
  • 5
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].

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79