en.javascript.info icon indicating copy to clipboard operation
en.javascript.info copied to clipboard

Suggestion. Task for 'Iterables' article

Open nat-k-dev opened this issue 5 years ago • 2 comments

It would be useful to have some practice tasks with realization of concepts in article Iterables.

An idea of task is in the article: "make an iterable object that generates an infinite sequence of pseudorandom numbers".

Possible solution:

let randomObj = {
  min: 10,
  max: 100,
}

randomObj[Symbol.iterator] = function() {
  return {
    start: this.min,
    end: this.max,
    next() {
      return {
        done: false, 
        value: Math.random() * (this.end - this.start) + this.start,
      };
    }
  }
}

const THRESHOLD = 20;

for (let value of randomObj) {
  alert(value); // show random generated value
  if (value < THRESHOLD) break;
}

nat-k-dev avatar Oct 15 '20 20:10 nat-k-dev

Maybe something more practical? Not sure a task is really needed there. People rarely make iterables manually.

iliakan avatar Dec 05 '20 20:12 iliakan

We can add something simple and potentialy useful.

iliakan avatar Dec 05 '20 20:12 iliakan