score:2

Accepted answer

Try this:

//declare an array of arrays
var dataset = [];
var inner = [];

/*
    set the svg width and height
*/
var svgWidth = 500;
var svgHeight = 100;
var xCoord = 0;
var yCoord = 0;

for (var i = 0; i < 20; i++) {
  do {
    xCoord = Math.floor(Math.random() * svgWidth);
    yCoord = Math.floor(Math.random() * svgHeight);
  } while (xCoord <= 0 | yCoord <= 0);


  if (xCoord > 0 && yCoord > 0) {
    inner.push(xCoord);
    inner.push(yCoord);

    dataset.push(inner);
  }

  inner = [];
}

var svg = d3.select("body")
  .append("svg")
  .attr("width", svgWidth)
  .attr("height", svgHeight);


svg.selectAll("circle")
  .data(dataset)
  .enter()
  .append("circle")
  .attr("cx", function(d) {
    return d[0];
  })
  .attr("cy", function(d) {
    return d[1];
  })
  .attr("r", 5);

score:3

The inner array isn't necessary, in fact it's the problem.

var dataset = [
  []
];

Just remove it.

var dataset = [];

Related Query

More Query from same tag