Hacker News new | ask | show | jobs
by vuknje 4946 days ago
I like the tutorial. One thing that catches my eye though is the unnecessarily complex example from the beginning:

  d3.selectAll('div')
    .text(function() {
      var index = Array.prototype.indexOf.call(this.parentElement.children, this);
      return (index % 2) == 0 ? 'Even' : 'Odd';
    });

No need for calculating the index variable - it's already provided as the second argument of the function passed to the text() method:

  d3.selectAll('div')
    .text(function(d, index) {
      return (index % 2) == 0 ? 'Even' : 'Odd';
    });