You're calling num.next() twice in every iteration. You're calling it once in the while() header to check whether the result is undefined, then you're calling it a second time in the body to log the value. Each call retrieves the next item from the generator. So you check the even items for null, and log the odd item after it.
Instead you should assign a variable to a single call
function* numberGen(n){
for (let i=0;i<n;i++){
yield i
}
}
const num = numberGen(10)
let i;
while ((i = num.next().value) !== undefined){
console.log(i)
}
Instead of calling the .next() method explicitly, you can use the built-in for-of iteration method.
function* numberGen(n) {
for (let i = 0; i < n; i++) {
yield i
}
}
const num = numberGen(10)
for (let i of num) {
console.log(i);
}
next()twice per iteration of that loop?!