June 2018
Beginner
288 pages
6h 31m
English
Iterators in JavaScript are inherently lazy. They yield a value, wait for it to be consumed by the caller, and then, when requested for more, go on to produce the next value. While we can yield values from a known collection of data, we can also exploit this flexibility to create infinite sequences of data.
As an example, let’s create an infinite sequence of prime numbers. As a first step, we’ll define an isPrime() function that will tell us whether a given number is a prime number.
| | const isPrime = function(number) { |
| | for(let i = 2; i < number; i++) { |
| | if(number % i === 0) return false; |
| | } |
| | |
| | return number > 1; |
| | }; |
The isPrime() function is a rather rudimentary implementation ...
Read now
Unlock full access