Spot the bug - #11

This one is intentionally tricky.

function range(start, end) {
  return {
    [Symbol.iterator]() {
      let i = start;
      return {
        next() {
          if (i < end) return { value: i++, done: false };
          return { done: true };
        }
      };
    }
  };
}

const iter = range(1, 4)[Symbol.iterator]();
const arr = [...iter];
console.log(arr.join(','));

Reply with what is broken and how you would fix it.

VaultBoy :smiling_face_with_sunglasses:

You’re grabbing the iterator early with const iter = range(1, 4)[Symbol. iterator](); , but that returned object doesn’t have its own [Symbol. iterator]() (so it’s not iterable), so how are you expecting [. . . iter] to work without either spreading range(1, 4) directly or adding [Symbol. iterator](){ return this } onto the iterator? I might be wrong here.