function *rezerogen(){
  yield "เอมิเลีย";
  yield "เรม";
  yield "รัม";
}
let iter = rezerogen();
alert(iter.next().value); // ได้ เอมิเลีย
alert(iter.next().value); // ได้ เรม
alert(iter.next().value); // ได้ รัม
alert(iter.next().value); // ได้ undefined
let iter2 = rezerogen()
for (let x of iter2){
  alert(x); // วน ๓ รอบ ได้ เอมิเลีย, เรม, รัม
}
  function* gen(n) {
  let i = 1;
  while (i <= n) {
    yield i * i
    i++;
  }
}
let iter = gen(4);
alert(iter.next().value); // ได้ 1
alert(iter.next().value); // ได้ 4
alert(iter.next().value); // ได้ 9
alert(iter.next().value); // ได้ 16
alert(iter.next().value); // ได้ undefined
  function* konosubagen() {
  yield "คาซึมะ"
  yield* ["อควา","เมงุมิน","ดักเนส"]
}
let iter = konosubagen();
alert(iter.next().value); // ได้ คาซึมะ
alert(iter.next().value); // ได้ อควา
alert(iter.next().value); // ได้ เมงุมิน
alert(iter.next().value); // ได้ ดักเนส
alert(iter.next().value); // ได้ undefined
  class Itor {
  constructor(ar1, ar2) {
    this.ar1 = ar1;
    this.ar2 = ar2;
    this[Symbol.iterator] = function* () {
      let i = 0, n = this.ar1.length;
      while (i < n) {
        yield "~" + this.ar1[i] + " " + this.ar2[i] + "~";
        i++;
      }
    }
  }
}
let myouji = ["โคโนะฮาตะ", "มานากะ", "โมริโนะ", "อิโนเสะ", "ซากุราอิ"];
let namae = ["มิระ", "อาโอะ", "มาริ", "ไม", "มิกาเงะ"];
let koias = new Itor(myouji, namae);
let s = "";
for (let k of koias) {
  s += k;
}
alert(s); // ได้ ~โคโนะฮาตะ มิระ~~มานากะ อาโอะ~~โมริโนะ มาริ~~อิโนเสะ ไม~~ซากุราอิ มิกาเงะ~
  function* range(a, b, c = 1) {
  if (!b) [a, b] = [0, a];
  while (a < b) {
    yield a;
    a += c;
  }
}
for (let x of range(4)) { alert(x); } // ได้ 0, 1, 2, 3
for (let x of range(5, 10)) { alert(x); } // ได้ 5, 6, 7, 8, 9
for (let x of range(2, 7, 2)) { alert(x); } // ได้ 2, 4, 6
  function* fibo() {
  let a = 0, b = 1, c
  while (1) {
    c = a + b;
    a = b;
    b = c;
    yield a;
  }
}
let s = "";
let i = 0;
for (let x of fibo()) {
  if(x > 2000) break;
  s += x + "; ";
  i++;
}
alert(s) // ได้ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610; 987; 1597;
  function* chamnuanchapho() {
  let a = 1
  while (1) {
    let pen = true;
    a++;
    let i = 2;
    while (i <= Math.floor(Math.sqrt(a))) {
      if (a % i == 0) {
        pen = false
        break;
      }
      i++;
    }
    if (pen) yield a;
  }
}
let s = "";
let i = 0;
for (let x of chamnuanchapho()) {
  if (x > 100) break
  s += x + "' ";
  i++;
}
alert(s) // ได้ 2' 3' 5' 7' 11' 13' 17' 19' 23' 29' 31' 37' 41' 43' 47' 53' 59' 61' 67' 71' 73' 79' 83' 89' 97' 
  ติดตามอัปเดตของบล็อกได้ที่แฟนเพจ