Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <article>
  <h1>Iterators</h1>
<p>The following examples illustrate ways that you can use the new ES6 generators, iterators and yield to power applications.</p>
<h2>A Simple Iterator</h2>
<p>This is perhaps the simplest iterator you can create, one that outputs an increasing number. Once it reaches the target number it generates a message output.</p>
<div>
<button onclick="display()">Next</button> <span id="display"></span>
</div>
<h2>Creating Ranges</h2>
<p>Iterators can be used with while statements, for loops, or even ES5 array constructs (when converted using ES6 Array.from()) to create array content. This example utilizes for loops to create an automatically generating table of squares and square roots:</p>
  <div>
    <div>Range Start: <input id="rangeStart" value="1" type="number" oninput="generateSquares()"/>
    <input id="rangeEnd" value="3" type="number" oninput="generateSquares()"/>
                                       </div>
      <div id="squares"></div>
  <p>By the way, notice what happens when you set the start value of the range to -10.</p>
                                 </div>
  <div>
  <h2>Creating an Infinite Iterator</h2>
  <p>You can use iterators and generators to create a function that will cycle through an array endlessly, here used to change the color of a ball.</p>
    <svg xmlns="http://www.w3.org/2000/svg" width="100px" height="100px" viewBox="0 0 100 100">
    <defs>
         <radialGradient id="shading" gradientTransform="translate(-0.08,-0.08)">
    <stop stop-color="white"
          stop-opacity="1"
          offset="0"/>
    <stop stop-color="white"
          stop-opacity="0"
          offset="0.2"
          />
    <stop stop-color="black"
          stop-opacity="0.9"
          offset="0.95"
          />
    <stop stop-color="black"
          stop-opacity="0.7"
          offset="1"
          />
           
           </radialGradient>      
      </defs>
    <circle cx="50" cy="50"
            r="45" fill="red" id="circle"
    stroke="black"/>
    <circle cx="50" cy="50"
            r="45" fill="url(#shading)"
    stroke="black"/>
    </svg>
  </div>
  <div>
    <h2>Feeding Data Through Iterators</h2>
    <p>Iterators can also be used as a way to retrieve data content one entry at a time, making it easier to manage both parsed and asynchronous data feeds.</p>
    <div>
       <textarea id="dataSource"
        oninput="updateSource()">
name, age, department, id
Jane, 25, information technology, jd125
Mark, 37, accounting, ml399
Harry, 51, marketing, hh356
###END        
      </textarea>
      <br/>
      <button onclick="nextRecord()">Next Record</button>
      <div id="recordDisplay">
        
      </div>
    </div>  
  </div>
</article>
              
            
!

CSS

              
                body {
  background-color:lightBlue;
  font-family:Arial;}
article {
  width:5in;
}
h1 {
  font-size:18pt;
  border-bottom:solid 
}
h2 {
  font-size:14pt;
}
td,th {
  min-width:80px;
  text-align:right;
  border:solid 1px gray;
}
.serif {font-family:Times New Roman;}
#dataSource {width:300px;height:100px;font-size:10pt;}
              
            
!

JS

              
                function* iter() {
     var i = 0;
      while (i != 3){
          yield i;
          i++
      }
      yield "You've reached the end."
};
var index = iter();
function display(){
  var d1 = document.getElementById('display');
  d1.innerHTML = index.next().value;  
}

function* rangeInclusive(a,b){
    var i = a;
    while(i <= b){
       yield i;
       i++
     }
};

function generateSquares(){
  var start = document.getElementById("rangeStart").value;
  var end = document.getElementById("rangeEnd").value;
  var squaresTable = `<table>
    <tr>
        <th>x</th>
        <th>x<sup>2</sup></th>
        <th>&#8730;x</th>
    </tr>
    ${ Array.from(rangeInclusive(start,end)).map((x)=>
      `<tr>
  <td>${x}</td>
  <td>${x * x}</td>
  <td>${x>=0?(Math.sqrt(x)).toLocaleString():(Math.sqrt(-x )).toLocaleString()+'<b class="serif">i</b>'}</td>
</tr>`).join('')}
</table>`;
var squares = document.getElementById('squares');
squares.innerHTML = squaresTable;  
}
generateSquares();

function * infinite(arr){
    var i = 0;
    while(true){
       yield arr[i];
       i = (i + 1) % arr.length;
     }
}

var colors = infinite(["red","orange","yellow","green","aqua","blue","purple"])

function setNextColor(){
  var circle = document.getElementById("circle");
  var color=colors.next().value;
 circle.setAttribute('fill',color);
  };
setInterval(setNextColor,500);


/*   Data Source */
function * records(dataStr){
    let lines = dataStr.split("\n");
    let headers = lines.shift().split(", ");
    let fieldCount = headers.length;
    let i = 0;
    while(!lines[i].match("###END")){
       let fields = lines[i].split(", ");
       let map = new Map();
       for (j=0;j!=fieldCount;j++){
          map.set(headers[j],fields[j])
          }
        yield map;
        i++;
    }
}
var recordSource = null;

function updateSource(){
  recordSource =    records(document.getElementById("dataSource").value);
  nextRecord();   
};

function nextRecord(){
  var record = recordSource.next().value;
  console.log(record);
  if (record){
  var report = `<p>${record.get("name")}, age ${record.get("age")}, is in ${record.get("department")}.`;
 document.getElementById("recordDisplay").innerHTML = report;
  }
else {
  document.getElementById("recordDisplay").innerHTML = "<p>No More records</p>";
}
};

updateSource();




              
            
!
999px

Console