user1386906
user1386906

Reputation: 1179

D3 js function comes back undefined

I want to make this into a function:

var meanPoints = d3.mean(data, function(d) {return d['total points'] });
console.log(meanPoints);

This works and gives me the mean of the total point of my json file. But if i try to make a function like so:

function meanVak(vak) {
d3.mean(data, function(d) {return d [vak] });
}

var meanPoints = meanVak('total points');
console.info(meanPoints);

It comes back as undefined.

Upvotes: 1

Views: 1386

Answers (1)

freakish
freakish

Reputation: 56467

Because you don't have return statement:

function meanVak(vak) {
    return d3.mean(data, function(d) {return d [vak] });
}

Side note: by default functions without return statement return undefined.

Upvotes: 4

Related Questions