en.javascript.info
en.javascript.info copied to clipboard
Suggestion. Task for 'Iterables' article
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;
}
Maybe something more practical? Not sure a task is really needed there. People rarely make iterables manually.
We can add something simple and potentialy useful.